Kaynağa Gözat

[crypto] Move AES_convert_key() hack into axtls_aes.c

Although the nature of the hack is essentially unchanged, this allows
us to remove the hardcoded assumption in tls.c that the RX cipher is
AES.
tags/v0.9.7
Michael Brown 15 yıl önce
ebeveyn
işleme
5de8305feb
2 değiştirilmiş dosya ile 25 ekleme ve 13 silme
  1. 25
    9
      src/crypto/axtls_aes.c
  2. 0
    4
      src/net/tls.c

+ 25
- 9
src/crypto/axtls_aes.c Dosyayı Görüntüle

@@ -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,

+ 0
- 4
src/net/tls.c Dosyayı Görüntüle

@@ -372,10 +372,6 @@ static int tls_generate_keys ( struct tls_session *tls ) {
372 372
 		       tls, strerror ( rc ) );
373 373
 		return rc;
374 374
 	}
375
-
376
-	/* FIXME: AES needs to be fixed to not require this */
377
-	AES_convert_key ( rx_cipherspec->cipher_ctx );
378
-
379 375
 	DBGC ( tls, "TLS %p RX key:\n", tls );
380 376
 	DBGC_HD ( tls, key, key_size );
381 377
 	key += key_size;

Loading…
İptal
Kaydet