Bläddra i källkod

[crypto] Add ECB block cipher mode (for debug and self-tests only)

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 år sedan
förälder
incheckning
69891db8e2
4 ändrade filer med 142 tillägg och 1 borttagningar
  1. 6
    1
      src/crypto/axtls_aes.c
  2. 80
    0
      src/crypto/ecb.c
  3. 1
    0
      src/include/ipxe/aes.h
  4. 55
    0
      src/include/ipxe/ecb.h

+ 6
- 1
src/crypto/axtls_aes.c Visa fil

@@ -24,6 +24,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
24 24
 #include <assert.h>
25 25
 #include <byteswap.h>
26 26
 #include <ipxe/crypto.h>
27
+#include <ipxe/ecb.h>
27 28
 #include <ipxe/cbc.h>
28 29
 #include <ipxe/aes.h>
29 30
 #include "crypto/axtls/crypto.h"
@@ -155,6 +156,10 @@ struct cipher_algorithm aes_algorithm = {
155 156
 	.decrypt = aes_decrypt,
156 157
 };
157 158
 
158
-/* AES with cipher-block chaining */
159
+/* AES in Electronic Codebook mode */
160
+ECB_CIPHER ( aes_ecb, aes_ecb_algorithm,
161
+	     aes_algorithm, struct aes_context, AES_BLOCKSIZE );
162
+
163
+/* AES in Cipher Block Chaining mode */
159 164
 CBC_CIPHER ( aes_cbc, aes_cbc_algorithm,
160 165
 	     aes_algorithm, struct aes_context, AES_BLOCKSIZE );

+ 80
- 0
src/crypto/ecb.c Visa fil

@@ -0,0 +1,80 @@
1
+/*
2
+ * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
+ * 02110-1301, USA.
18
+ *
19
+ * You can also choose to distribute this program under the terms of
20
+ * the Unmodified Binary Distribution Licence (as given in the file
21
+ * COPYING.UBDL), provided that you have satisfied its requirements.
22
+ */
23
+
24
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
+
26
+#include <assert.h>
27
+#include <ipxe/crypto.h>
28
+#include <ipxe/ecb.h>
29
+
30
+/** @file
31
+ *
32
+ * Electronic codebook (ECB)
33
+ *
34
+ */
35
+
36
+/**
37
+ * Encrypt data
38
+ *
39
+ * @v ctx		Context
40
+ * @v src		Data to encrypt
41
+ * @v dst		Buffer for encrypted data
42
+ * @v len		Length of data
43
+ * @v raw_cipher	Underlying cipher algorithm
44
+ */
45
+void ecb_encrypt ( void *ctx, const void *src, void *dst, size_t len,
46
+		   struct cipher_algorithm *raw_cipher ) {
47
+	size_t blocksize = raw_cipher->blocksize;
48
+
49
+	assert ( ( len % blocksize ) == 0 );
50
+
51
+	while ( len ) {
52
+		cipher_encrypt ( raw_cipher, ctx, src, dst, blocksize );
53
+		dst += blocksize;
54
+		src += blocksize;
55
+		len -= blocksize;
56
+	}
57
+}
58
+
59
+/**
60
+ * Decrypt data
61
+ *
62
+ * @v ctx		Context
63
+ * @v src		Data to decrypt
64
+ * @v dst		Buffer for decrypted data
65
+ * @v len		Length of data
66
+ * @v raw_cipher	Underlying cipher algorithm
67
+ */
68
+void ecb_decrypt ( void *ctx, const void *src, void *dst, size_t len,
69
+		   struct cipher_algorithm *raw_cipher ) {
70
+	size_t blocksize = raw_cipher->blocksize;
71
+
72
+	assert ( ( len % blocksize ) == 0 );
73
+
74
+	while ( len ) {
75
+		cipher_decrypt ( raw_cipher, ctx, src, dst, blocksize );
76
+		dst += blocksize;
77
+		src += blocksize;
78
+		len -= blocksize;
79
+	}
80
+}

+ 1
- 0
src/include/ipxe/aes.h Visa fil

@@ -26,6 +26,7 @@ extern void axtls_aes_encrypt ( const AES_CTX *ctx, uint32_t *data );
26 26
 extern void axtls_aes_decrypt ( const AES_CTX *ctx, uint32_t *data );
27 27
 
28 28
 extern struct cipher_algorithm aes_algorithm;
29
+extern struct cipher_algorithm aes_ecb_algorithm;
29 30
 extern struct cipher_algorithm aes_cbc_algorithm;
30 31
 
31 32
 int aes_wrap ( const void *kek, const void *src, void *dest, int nblk );

+ 55
- 0
src/include/ipxe/ecb.h Visa fil

@@ -0,0 +1,55 @@
1
+#ifndef _IPXE_ECB_H
2
+#define _IPXE_ECB_H
3
+
4
+/** @file
5
+ *
6
+ * Electronic codebook (ECB)
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
+
12
+#include <ipxe/crypto.h>
13
+
14
+extern void ecb_encrypt ( void *ctx, const void *src, void *dst,
15
+			  size_t len, struct cipher_algorithm *raw_cipher );
16
+extern void ecb_decrypt ( void *ctx, const void *src, void *dst,
17
+			  size_t len, struct cipher_algorithm *raw_cipher );
18
+
19
+/**
20
+ * Create a cipher-block chaining mode of behaviour of an existing cipher
21
+ *
22
+ * @v _ecb_name		Name for the new ECB cipher
23
+ * @v _ecb_cipher	New cipher algorithm
24
+ * @v _raw_cipher	Underlying cipher algorithm
25
+ * @v _raw_context	Context structure for the underlying cipher
26
+ * @v _blocksize	Cipher block size
27
+ */
28
+#define ECB_CIPHER( _ecb_name, _ecb_cipher, _raw_cipher, _raw_context,	\
29
+		    _blocksize )					\
30
+static int _ecb_name ## _setkey ( void *ctx, const void *key,		\
31
+				  size_t keylen ) {			\
32
+	return cipher_setkey ( &_raw_cipher, ctx, key, keylen );	\
33
+}									\
34
+static void _ecb_name ## _setiv ( void *ctx, const void *iv ) {		\
35
+	cipher_setiv ( &_raw_cipher, ctx, iv );				\
36
+}									\
37
+static void _ecb_name ## _encrypt ( void *ctx, const void *src,		\
38
+				    void *dst, size_t len ) {		\
39
+	ecb_encrypt ( ctx, src, dst, len, &_raw_cipher );		\
40
+}									\
41
+static void _ecb_name ## _decrypt ( void *ctx, const void *src,		\
42
+				    void *dst, size_t len ) {		\
43
+	ecb_decrypt ( ctx, src, dst, len, &_raw_cipher );		\
44
+}									\
45
+struct cipher_algorithm _ecb_cipher = {					\
46
+	.name		= #_ecb_name,					\
47
+	.ctxsize	= sizeof ( _raw_context ),			\
48
+	.blocksize	= _blocksize,					\
49
+	.setkey		= _ecb_name ## _setkey,				\
50
+	.setiv		= _ecb_name ## _setiv,				\
51
+	.encrypt	= _ecb_name ## _encrypt,			\
52
+	.decrypt	= _ecb_name ## _decrypt,			\
53
+};
54
+
55
+#endif /* _IPXE_ECB_H */

Laddar…
Avbryt
Spara