|
@@ -4,8 +4,13 @@
|
4
|
4
|
#include <gpxe/crypto.h>
|
5
|
5
|
#include <gpxe/aes.h>
|
6
|
6
|
|
|
7
|
+struct aes_cbc_context {
|
|
8
|
+ AES_CTX ctx;
|
|
9
|
+ int decrypting;
|
|
10
|
+};
|
|
11
|
+
|
7
|
12
|
static int aes_cbc_setkey ( void *ctx, const void *key, size_t keylen ) {
|
8
|
|
- AES_CTX *aesctx = ctx;
|
|
13
|
+ struct aes_cbc_context *aesctx = ctx;
|
9
|
14
|
AES_MODE mode;
|
10
|
15
|
|
11
|
16
|
switch ( keylen ) {
|
|
@@ -19,33 +24,44 @@ static int aes_cbc_setkey ( void *ctx, const void *key, size_t keylen ) {
|
19
|
24
|
return -EINVAL;
|
20
|
25
|
}
|
21
|
26
|
|
22
|
|
- AES_set_key ( aesctx, key, aesctx->iv, mode );
|
|
27
|
+ AES_set_key ( &aesctx->ctx, key, aesctx->ctx.iv, mode );
|
|
28
|
+
|
|
29
|
+ aesctx->decrypting = 0;
|
|
30
|
+
|
23
|
31
|
return 0;
|
24
|
32
|
}
|
25
|
33
|
|
26
|
34
|
static void aes_cbc_setiv ( void *ctx, const void *iv ) {
|
27
|
|
- AES_CTX *aesctx = ctx;
|
|
35
|
+ struct aes_cbc_context *aesctx = ctx;
|
28
|
36
|
|
29
|
|
- memcpy ( aesctx->iv, iv, sizeof ( aesctx->iv ) );
|
|
37
|
+ memcpy ( aesctx->ctx.iv, iv, sizeof ( aesctx->ctx.iv ) );
|
30
|
38
|
}
|
31
|
39
|
|
32
|
40
|
static void aes_cbc_encrypt ( void *ctx, const void *data, void *dst,
|
33
|
41
|
size_t len ) {
|
34
|
|
- AES_CTX *aesctx = ctx;
|
|
42
|
+ struct aes_cbc_context *aesctx = ctx;
|
35
|
43
|
|
36
|
|
- AES_cbc_encrypt ( aesctx, data, dst, len );
|
|
44
|
+ if ( aesctx->decrypting )
|
|
45
|
+ assert ( 0 );
|
|
46
|
+
|
|
47
|
+ AES_cbc_encrypt ( &aesctx->ctx, data, dst, len );
|
37
|
48
|
}
|
38
|
49
|
|
39
|
50
|
static void aes_cbc_decrypt ( void *ctx, const void *data, void *dst,
|
40
|
51
|
size_t len ) {
|
41
|
|
- AES_CTX *aesctx = ctx;
|
|
52
|
+ struct aes_cbc_context *aesctx = ctx;
|
|
53
|
+
|
|
54
|
+ if ( ! aesctx->decrypting ) {
|
|
55
|
+ AES_convert_key ( &aesctx->ctx );
|
|
56
|
+ aesctx->decrypting = 1;
|
|
57
|
+ }
|
42
|
58
|
|
43
|
|
- AES_cbc_decrypt ( aesctx, data, dst, len );
|
|
59
|
+ AES_cbc_decrypt ( &aesctx->ctx, data, dst, len );
|
44
|
60
|
}
|
45
|
61
|
|
46
|
62
|
struct crypto_algorithm aes_cbc_algorithm = {
|
47
|
63
|
.name = "aes_cbc",
|
48
|
|
- .ctxsize = sizeof ( AES_CTX ),
|
|
64
|
+ .ctxsize = sizeof ( struct aes_cbc_context ),
|
49
|
65
|
.blocksize = 16,
|
50
|
66
|
.setkey = aes_cbc_setkey,
|
51
|
67
|
.setiv = aes_cbc_setiv,
|