@@ -40,15 +40,56 @@ void mb_sha256_buffer(const uint8_t *data, size_t len, message_digest_t *digest_
40
40
mbedtls_sha256 (data , len , digest_out -> bytes , 0 );
41
41
}
42
42
43
+ #if IV0_XOR
44
+ // Taken from mbedtls_aes_crypt_ctr, but with XOR instead of adding to IV0
45
+ int mb_aes_crypt_ctr_xor (mbedtls_aes_context * ctx ,
46
+ size_t length ,
47
+ unsigned char iv0 [16 ],
48
+ unsigned char nonce_xor [16 ],
49
+ unsigned char stream_block [16 ],
50
+ const unsigned char * input ,
51
+ unsigned char * output )
52
+ {
53
+ int c ;
54
+ int ret = 0 ;
55
+ size_t n = 0 ;
56
+ size_t counter = 0 ;
57
+
58
+ while (length -- ) {
59
+ if (n == 0 ) {
60
+ for (int i = 16 ; i > 16 - sizeof (counter ); i -- ) {
61
+ nonce_xor [i - 1 ] == iv0 [i - 1 ] ^ (unsigned char )(counter >> (i * 8 ));
62
+ }
63
+
64
+ ret = mbedtls_aes_crypt_ecb (ctx , MBEDTLS_AES_ENCRYPT , nonce_xor , stream_block );
65
+ if (ret != 0 ) {
66
+ break ;
67
+ }
68
+ }
69
+ c = * input ++ ;
70
+ * output ++ = (unsigned char ) (c ^ stream_block [n ]);
71
+
72
+ n = (n + 1 ) & 0x0F ;
73
+ }
74
+
75
+ return ret ;
76
+ }
77
+ #endif
78
+
43
79
void mb_aes256_buffer (const uint8_t * data , size_t len , uint8_t * data_out , const aes_key_t * key , iv_t * iv ) {
44
80
mbedtls_aes_context aes ;
45
81
46
82
assert (len % 16 == 0 );
47
83
48
84
mbedtls_aes_setkey_enc (& aes , key -> bytes , 256 );
85
+ uint8_t xor_working_block [16 ] = {0 };
49
86
uint8_t stream_block [16 ] = {0 };
50
87
size_t nc_off = 0 ;
88
+ #if IV0_XOR
89
+ mb_aes_crypt_ctr_xor (& aes , len , iv -> bytes , xor_working_block , stream_block , data , data_out );
90
+ #else
51
91
mbedtls_aes_crypt_ctr (& aes , len , & nc_off , iv -> bytes , stream_block , data , data_out );
92
+ #endif
52
93
}
53
94
54
95
void raw_to_der (signature_t * sig ) {
0 commit comments