diff options
-rw-r--r-- | networking/tls.c | 30 | ||||
-rw-r--r-- | networking/tls_aes.c | 54 | ||||
-rw-r--r-- | networking/tls_aes.h | 14 |
3 files changed, 64 insertions, 34 deletions
diff --git a/networking/tls.c b/networking/tls.c index 4e9187d4f..30afd9ea9 100644 --- a/networking/tls.c +++ b/networking/tls.c | |||
@@ -722,17 +722,12 @@ static void xwrite_encrypted(tls_state_t *tls, unsigned size, unsigned type) | |||
722 | } while ((size & (AES_BLOCKSIZE - 1)) != 0); | 722 | } while ((size & (AES_BLOCKSIZE - 1)) != 0); |
723 | 723 | ||
724 | /* Encrypt content+MAC+padding in place */ | 724 | /* Encrypt content+MAC+padding in place */ |
725 | { | 725 | aes_cbc_encrypt( |
726 | psCipherContext_t ctx; | 726 | tls->client_write_key, tls->key_size, /* selects 128/256 */ |
727 | psAesInit(&ctx, buf - AES_BLOCKSIZE, /* IV */ | 727 | buf - AES_BLOCKSIZE, /* IV */ |
728 | tls->client_write_key, tls->key_size /* selects 128/256 */ | 728 | buf, size, /* plaintext */ |
729 | ); | 729 | buf /* ciphertext */ |
730 | psAesEncrypt(&ctx, | 730 | ); |
731 | buf, /* plaintext */ | ||
732 | buf, /* ciphertext */ | ||
733 | size | ||
734 | ); | ||
735 | } | ||
736 | 731 | ||
737 | /* Write out */ | 732 | /* Write out */ |
738 | dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n", | 733 | dbg("writing 5 + %u IV + %u encrypted bytes, padding_length:0x%02x\n", |
@@ -875,7 +870,6 @@ static int tls_xread_record(tls_state_t *tls) | |||
875 | 870 | ||
876 | /* Needs to be decrypted? */ | 871 | /* Needs to be decrypted? */ |
877 | if (tls->min_encrypted_len_on_read > tls->MAC_size) { | 872 | if (tls->min_encrypted_len_on_read > tls->MAC_size) { |
878 | psCipherContext_t ctx; | ||
879 | uint8_t *p = tls->inbuf + RECHDR_LEN; | 873 | uint8_t *p = tls->inbuf + RECHDR_LEN; |
880 | int padding_len; | 874 | int padding_len; |
881 | 875 | ||
@@ -886,14 +880,12 @@ static int tls_xread_record(tls_state_t *tls) | |||
886 | sz, tls->min_encrypted_len_on_read); | 880 | sz, tls->min_encrypted_len_on_read); |
887 | } | 881 | } |
888 | /* Decrypt content+MAC+padding, moving it over IV in the process */ | 882 | /* Decrypt content+MAC+padding, moving it over IV in the process */ |
889 | psAesInit(&ctx, p, /* IV */ | ||
890 | tls->server_write_key, tls->key_size /* selects 128/256 */ | ||
891 | ); | ||
892 | sz -= AES_BLOCKSIZE; /* we will overwrite IV now */ | 883 | sz -= AES_BLOCKSIZE; /* we will overwrite IV now */ |
893 | psAesDecrypt(&ctx, | 884 | aes_cbc_decrypt( |
894 | p + AES_BLOCKSIZE, /* ciphertext */ | 885 | tls->server_write_key, tls->key_size, /* selects 128/256 */ |
895 | p, /* plaintext */ | 886 | p, /* IV */ |
896 | sz | 887 | p + AES_BLOCKSIZE, sz, /* ciphertext */ |
888 | p /* plaintext */ | ||
897 | ); | 889 | ); |
898 | padding_len = p[sz - 1]; | 890 | padding_len = p[sz - 1]; |
899 | dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[0], padding_len); | 891 | dbg("encrypted size:%u type:0x%02x padding_length:0x%02x\n", sz, p[0], padding_len); |
diff --git a/networking/tls_aes.c b/networking/tls_aes.c index 6c3c39373..ebaab15b1 100644 --- a/networking/tls_aes.c +++ b/networking/tls_aes.c | |||
@@ -5,6 +5,46 @@ | |||
5 | */ | 5 | */ |
6 | #include "tls.h" | 6 | #include "tls.h" |
7 | 7 | ||
8 | static | ||
9 | int32 psAesInitKey(const unsigned char *key, uint32 keylen, psAesKey_t *skey); | ||
10 | static | ||
11 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | ||
12 | psAesKey_t *skey); | ||
13 | static | ||
14 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, | ||
15 | psAesKey_t *skey); | ||
16 | static | ||
17 | int32 psAesInit(psCipherContext_t *ctx, unsigned char *IV, | ||
18 | const unsigned char *key, uint32 keylen); | ||
19 | static | ||
20 | int32 psAesEncrypt(psCipherContext_t *ctx, const unsigned char *pt, | ||
21 | unsigned char *ct, uint32 len); | ||
22 | static | ||
23 | int32 psAesDecrypt(psCipherContext_t *ctx, const unsigned char *ct, | ||
24 | unsigned char *pt, uint32 len); | ||
25 | |||
26 | void aes_cbc_encrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst) | ||
27 | { | ||
28 | psCipherContext_t ctx; | ||
29 | psAesInit(&ctx, iv, key, klen); | ||
30 | psAesEncrypt(&ctx, | ||
31 | data, /* plaintext */ | ||
32 | dst, /* ciphertext */ | ||
33 | len | ||
34 | ); | ||
35 | } | ||
36 | |||
37 | void aes_cbc_decrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst) | ||
38 | { | ||
39 | psCipherContext_t ctx; | ||
40 | psAesInit(&ctx, iv, key, klen); | ||
41 | psAesDecrypt(&ctx, | ||
42 | data, /* ciphertext */ | ||
43 | dst, /* plaintext */ | ||
44 | len | ||
45 | ); | ||
46 | } | ||
47 | |||
8 | /* The file is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/. | 48 | /* The file is taken almost verbatim from matrixssl-3-7-2b-open/crypto/symmetric/. |
9 | * Changes are flagged with //bbox | 49 | * Changes are flagged with //bbox |
10 | */ | 50 | */ |
@@ -1079,8 +1119,9 @@ static uint32 setup_mix2(uint32 temp) | |||
1079 | Software implementation of AES CBC APIs | 1119 | Software implementation of AES CBC APIs |
1080 | */ | 1120 | */ |
1081 | #ifndef USE_AES_CBC_EXTERNAL | 1121 | #ifndef USE_AES_CBC_EXTERNAL |
1122 | static //bbox | ||
1082 | int32 psAesInit(psCipherContext_t *ctx, unsigned char *IV, | 1123 | int32 psAesInit(psCipherContext_t *ctx, unsigned char *IV, |
1083 | unsigned char *key, uint32 keylen) | 1124 | const unsigned char *key, uint32 keylen) |
1084 | { | 1125 | { |
1085 | int32 x, err; | 1126 | int32 x, err; |
1086 | 1127 | ||
@@ -1106,7 +1147,8 @@ int32 psAesInit(psCipherContext_t *ctx, unsigned char *IV, | |||
1106 | return PS_SUCCESS; | 1147 | return PS_SUCCESS; |
1107 | } | 1148 | } |
1108 | 1149 | ||
1109 | int32 psAesEncrypt(psCipherContext_t *ctx, unsigned char *pt, | 1150 | static //bbox |
1151 | int32 psAesEncrypt(psCipherContext_t *ctx, const unsigned char *pt, | ||
1110 | unsigned char *ct, uint32 len) | 1152 | unsigned char *ct, uint32 len) |
1111 | { | 1153 | { |
1112 | int32 x; | 1154 | int32 x; |
@@ -1156,7 +1198,8 @@ int32 psAesEncrypt(psCipherContext_t *ctx, unsigned char *pt, | |||
1156 | return len; | 1198 | return len; |
1157 | } | 1199 | } |
1158 | 1200 | ||
1159 | int32 psAesDecrypt(psCipherContext_t *ctx, unsigned char *ct, | 1201 | static //bbox |
1202 | int32 psAesDecrypt(psCipherContext_t *ctx, const unsigned char *ct, | ||
1160 | unsigned char *pt, uint32 len) | 1203 | unsigned char *pt, uint32 len) |
1161 | { | 1204 | { |
1162 | int32 x; | 1205 | int32 x; |
@@ -1223,6 +1266,7 @@ int32 psAesDecrypt(psCipherContext_t *ctx, unsigned char *ct, | |||
1223 | skey: The key in as scheduled by this function. | 1266 | skey: The key in as scheduled by this function. |
1224 | */ | 1267 | */ |
1225 | 1268 | ||
1269 | static //bbox | ||
1226 | int32 psAesInitKey(const unsigned char *key, uint32 keylen, psAesKey_t *skey) | 1270 | int32 psAesInitKey(const unsigned char *key, uint32 keylen, psAesKey_t *skey) |
1227 | { | 1271 | { |
1228 | int32 i, j; | 1272 | int32 i, j; |
@@ -1390,6 +1434,7 @@ int32 psAesInitKey(const unsigned char *key, uint32 keylen, psAesKey_t *skey) | |||
1390 | 1434 | ||
1391 | 1435 | ||
1392 | #ifdef USE_BURN_STACK | 1436 | #ifdef USE_BURN_STACK |
1437 | static //bbox | ||
1393 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | 1438 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, |
1394 | psAesKey_t *skey) | 1439 | psAesKey_t *skey) |
1395 | { | 1440 | { |
@@ -1399,6 +1444,7 @@ void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | |||
1399 | static void _aes_ecb_encrypt(const unsigned char *pt, unsigned char *ct, | 1444 | static void _aes_ecb_encrypt(const unsigned char *pt, unsigned char *ct, |
1400 | psAesKey_t *skey) | 1445 | psAesKey_t *skey) |
1401 | #else | 1446 | #else |
1447 | static //bbox | ||
1402 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | 1448 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, |
1403 | psAesKey_t *skey) | 1449 | psAesKey_t *skey) |
1404 | #endif /* USE_BURN_STACK */ | 1450 | #endif /* USE_BURN_STACK */ |
@@ -1555,6 +1601,7 @@ void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | |||
1555 | } | 1601 | } |
1556 | 1602 | ||
1557 | #ifdef USE_BURN_STACK | 1603 | #ifdef USE_BURN_STACK |
1604 | static //bbox | ||
1558 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, | 1605 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, |
1559 | psAesKey_t *skey) | 1606 | psAesKey_t *skey) |
1560 | { | 1607 | { |
@@ -1564,6 +1611,7 @@ void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, | |||
1564 | static void _aes_ecb_decrypt(const unsigned char *ct, unsigned char *pt, | 1611 | static void _aes_ecb_decrypt(const unsigned char *ct, unsigned char *pt, |
1565 | psAesKey_t *skey) | 1612 | psAesKey_t *skey) |
1566 | #else | 1613 | #else |
1614 | static //bbox | ||
1567 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, | 1615 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, |
1568 | psAesKey_t *skey) | 1616 | psAesKey_t *skey) |
1569 | #endif /* USE_BURN_STACK */ | 1617 | #endif /* USE_BURN_STACK */ |
diff --git a/networking/tls_aes.h b/networking/tls_aes.h index ea8ed7ea9..c6791866a 100644 --- a/networking/tls_aes.h +++ b/networking/tls_aes.h | |||
@@ -6,15 +6,5 @@ | |||
6 | * Selected few declarations for AES. | 6 | * Selected few declarations for AES. |
7 | */ | 7 | */ |
8 | 8 | ||
9 | int32 psAesInitKey(const unsigned char *key, uint32 keylen, psAesKey_t *skey); | 9 | void aes_cbc_encrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst); |
10 | void psAesEncryptBlock(const unsigned char *pt, unsigned char *ct, | 10 | void aes_cbc_decrypt(const void *key, int klen, void *iv, const void *data, size_t len, void *dst); |
11 | psAesKey_t *skey); | ||
12 | void psAesDecryptBlock(const unsigned char *ct, unsigned char *pt, | ||
13 | psAesKey_t *skey); | ||
14 | |||
15 | int32 psAesInit(psCipherContext_t *ctx, unsigned char *IV, | ||
16 | unsigned char *key, uint32 keylen); | ||
17 | int32 psAesEncrypt(psCipherContext_t *ctx, unsigned char *pt, | ||
18 | unsigned char *ct, uint32 len); | ||
19 | int32 psAesDecrypt(psCipherContext_t *ctx, unsigned char *ct, | ||
20 | unsigned char *pt, uint32 len); | ||