|
@@ -15,15 +15,15 @@
|
15
|
15
|
* @v ctx Context
|
16
|
16
|
* @v key Key
|
17
|
17
|
* @v keylen Key length
|
18
|
|
- * @v cipher Underlying cipher algorithm
|
|
18
|
+ * @v raw_cipher Underlying cipher algorithm
|
19
|
19
|
* @v cbc_ctx CBC context
|
20
|
20
|
* @ret rc Return status code
|
21
|
21
|
*/
|
22
|
22
|
static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
|
23
|
|
- struct cipher_algorithm *cipher,
|
|
23
|
+ struct cipher_algorithm *raw_cipher,
|
24
|
24
|
void *cbc_ctx __unused ) {
|
25
|
25
|
|
26
|
|
- return cipher_setkey ( cipher, ctx, key, keylen );
|
|
26
|
+ return cipher_setkey ( raw_cipher, ctx, key, keylen );
|
27
|
27
|
}
|
28
|
28
|
|
29
|
29
|
/**
|
|
@@ -31,20 +31,68 @@ static inline int cbc_setkey ( void *ctx, const void *key, size_t keylen,
|
31
|
31
|
*
|
32
|
32
|
* @v ctx Context
|
33
|
33
|
* @v iv Initialisation vector
|
34
|
|
- * @v cipher Underlying cipher algorithm
|
|
34
|
+ * @v raw_cipher Underlying cipher algorithm
|
35
|
35
|
* @v cbc_ctx CBC context
|
36
|
36
|
*/
|
37
|
37
|
static inline void cbc_setiv ( void *ctx __unused, const void *iv,
|
38
|
|
- struct cipher_algorithm *cipher,
|
|
38
|
+ struct cipher_algorithm *raw_cipher,
|
39
|
39
|
void *cbc_ctx ) {
|
40
|
|
- memcpy ( cbc_ctx, iv, cipher->blocksize );
|
|
40
|
+ memcpy ( cbc_ctx, iv, raw_cipher->blocksize );
|
41
|
41
|
}
|
42
|
42
|
|
43
|
43
|
extern void cbc_encrypt ( void *ctx, const void *src, void *dst,
|
44
|
|
- size_t len, struct cipher_algorithm *cipher,
|
|
44
|
+ size_t len, struct cipher_algorithm *raw_cipher,
|
45
|
45
|
void *cbc_ctx );
|
46
|
46
|
extern void cbc_decrypt ( void *ctx, const void *src, void *dst,
|
47
|
|
- size_t len, struct cipher_algorithm *cipher,
|
|
47
|
+ size_t len, struct cipher_algorithm *raw_cipher,
|
48
|
48
|
void *cbc_ctx );
|
49
|
49
|
|
|
50
|
+/**
|
|
51
|
+ * Create a cipher-block chaining mode of behaviour of an existing cipher
|
|
52
|
+ *
|
|
53
|
+ * @v _cbc_name Name for the new CBC cipher
|
|
54
|
+ * @v _cbc_cipher New cipher algorithm
|
|
55
|
+ * @v _raw_cipher Underlying cipher algorithm
|
|
56
|
+ * @v _raw_context Context structure for the underlying cipher
|
|
57
|
+ * @v _blocksize Cipher block size
|
|
58
|
+ */
|
|
59
|
+#define CBC_CIPHER( _cbc_name, _cbc_cipher, _raw_cipher, _raw_context, \
|
|
60
|
+ _blocksize ) \
|
|
61
|
+struct _cbc_name ## _context { \
|
|
62
|
+ _raw_context raw_ctx; \
|
|
63
|
+ uint8_t cbc_ctx[_blocksize]; \
|
|
64
|
+}; \
|
|
65
|
+static int _cbc_name ## _setkey ( void *ctx, const void *key, \
|
|
66
|
+ size_t keylen ) { \
|
|
67
|
+ struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
|
|
68
|
+ return cbc_setkey ( &_cbc_name ## _ctx->raw_ctx, key, keylen, \
|
|
69
|
+ &_raw_cipher, &_cbc_name ## _ctx->cbc_ctx );\
|
|
70
|
+} \
|
|
71
|
+static void _cbc_name ## _setiv ( void *ctx, const void *iv ) { \
|
|
72
|
+ struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
|
|
73
|
+ cbc_setiv ( &_cbc_name ## _ctx->raw_ctx, iv, \
|
|
74
|
+ &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
|
|
75
|
+} \
|
|
76
|
+static void _cbc_name ## _encrypt ( void *ctx, const void *src, \
|
|
77
|
+ void *dst, size_t len ) { \
|
|
78
|
+ struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
|
|
79
|
+ cbc_encrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
|
|
80
|
+ &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
|
|
81
|
+} \
|
|
82
|
+static void _cbc_name ## _decrypt ( void *ctx, const void *src, \
|
|
83
|
+ void *dst, size_t len ) { \
|
|
84
|
+ struct _cbc_name ## _context * _cbc_name ## _ctx = ctx; \
|
|
85
|
+ cbc_decrypt ( &_cbc_name ## _ctx->raw_ctx, src, dst, len, \
|
|
86
|
+ &_raw_cipher, &aes_cbc_ctx->cbc_ctx ); \
|
|
87
|
+} \
|
|
88
|
+struct cipher_algorithm _cbc_cipher = { \
|
|
89
|
+ .name = #_cbc_name, \
|
|
90
|
+ .ctxsize = sizeof ( struct _cbc_name ## _context ), \
|
|
91
|
+ .blocksize = _blocksize, \
|
|
92
|
+ .setkey = _cbc_name ## _setkey, \
|
|
93
|
+ .setiv = _cbc_name ## _setiv, \
|
|
94
|
+ .encrypt = _cbc_name ## _encrypt, \
|
|
95
|
+ .decrypt = _cbc_name ## _decrypt, \
|
|
96
|
+};
|
|
97
|
+
|
50
|
98
|
#endif /* _GPXE_CBC_H */
|