20
20
status_t aes_decrypt (ta_cipher_ctx * cipher_ctx ) {
21
21
// FIXME: Add logger and some checks here
22
22
mbedtls_aes_context ctx ;
23
+ mbedtls_md_context_t sha_ctx ;
23
24
int status ;
24
- char * err ;
25
+ char * err = NULL ;
25
26
uint8_t buf [AES_BLOCK_SIZE ];
26
-
27
+ uint8_t digest [AES_BLOCK_SIZE * 2 ];
28
+ uint8_t nonce [IMSI_LEN + MAX_TIMESTAMP_LEN + 1 ] = {0 };
27
29
/* Create and initialise the context */
28
30
mbedtls_aes_init (& ctx );
31
+ mbedtls_md_init (& sha_ctx );
32
+ if (mbedtls_md_setup (& sha_ctx , mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ), 1 ) != 0 ) {
33
+ err = "Failed to set up message-digest information" ;
34
+ status = SC_UTILS_CIPHER_ERROR ;
35
+ goto exit ;
36
+ }
29
37
mbedtls_platform_zeroize (cipher_ctx -> plaintext , sizeof (cipher_ctx -> plaintext ));
30
38
mbedtls_platform_zeroize (buf , AES_BLOCK_SIZE );
39
+ mbedtls_platform_zeroize (digest , AES_BLOCK_SIZE * 2 );
31
40
32
41
/* set decryption key */
33
42
if ((status = mbedtls_aes_setkey_dec (& ctx , cipher_ctx -> key , TA_AES_KEY_BITS )) != EXIT_SUCCESS ) {
34
- err = "set aes key failed" ;
43
+ err = "Failed to set AES key" ;
44
+ status = SC_UTILS_CIPHER_ERROR ;
35
45
goto exit ;
36
46
}
37
47
48
+ // concatenate (Device_ID, timestamp)
49
+ snprintf ((char * )nonce , IMSI_LEN + MAX_TIMESTAMP_LEN + 1 , "%s-%ld" , cipher_ctx -> device_id , cipher_ctx -> timestamp );
50
+ // hash base data
51
+ mbedtls_md_starts (& sha_ctx );
52
+ mbedtls_md_update (& sha_ctx , digest , AES_BLOCK_SIZE * 2 );
53
+ mbedtls_md_update (& sha_ctx , nonce , IMSI_LEN + MAX_TIMESTAMP_LEN );
54
+ mbedtls_md_update (& sha_ctx , cipher_ctx -> key , TA_AES_KEY_BITS / 8 );
55
+ mbedtls_md_finish (& sha_ctx , digest );
56
+
57
+ mbedtls_md_hmac_starts (& sha_ctx , digest , TA_AES_HMAC_SIZE );
58
+
38
59
// Provide the message to be decrypted, and obtain the plaintext output.
39
60
const size_t ciphertext_len = cipher_ctx -> ciphertext_len ;
40
61
uint8_t * ciphertext = cipher_ctx -> ciphertext ;
@@ -43,21 +64,30 @@ status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
43
64
memset (buf , 0 , AES_BLOCK_SIZE );
44
65
int n = (ciphertext_len - i > AES_BLOCK_SIZE ) ? AES_BLOCK_SIZE : (int )(ciphertext_len - i );
45
66
memcpy (buf , ciphertext + i , n );
67
+ mbedtls_md_hmac_update (& sha_ctx , buf , AES_BLOCK_SIZE );
46
68
if ((status = mbedtls_aes_crypt_cbc (& ctx , MBEDTLS_AES_DECRYPT , AES_BLOCK_SIZE , cipher_ctx -> iv , buf , buf )) != 0 ) {
47
- err = "aes decrpyt failed" ;
69
+ err = "Failed to decrypt AES message" ;
70
+ status = SC_UTILS_CIPHER_ERROR ;
48
71
goto exit ;
49
72
}
50
73
memcpy (plaintext , buf , AES_BLOCK_SIZE );
51
74
plaintext += AES_BLOCK_SIZE ;
52
75
}
53
76
54
- /* Clean up */
55
- mbedtls_aes_free (& ctx );
56
- return SC_OK ;
77
+ // compare hmac
78
+ mbedtls_md_hmac_finish (& sha_ctx , digest );
79
+ if (memcmp (digest , cipher_ctx -> hmac , TA_AES_HMAC_SIZE ) != 0 ) {
80
+ err = "Failed to validate HMAC" ;
81
+ status = SC_UTILS_CIPHER_ERROR ;
82
+ goto exit ;
83
+ }
84
+ status = SC_OK ;
57
85
exit :
58
- fprintf (stderr , "%s\n" , err );
86
+ // FIXME: Use default logger instead
87
+ if (!err ) fprintf (stderr , "%s\n" , err );
59
88
mbedtls_aes_free (& ctx );
60
- return SC_UTILS_CIPHER_ERROR ;
89
+ mbedtls_md_free (& sha_ctx );
90
+ return status ;
61
91
}
62
92
63
93
status_t aes_encrypt (ta_cipher_ctx * cipher_ctx ) {
@@ -79,29 +109,29 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
79
109
mbedtls_md_init (& sha_ctx );
80
110
mbedtls_aes_init (& ctx );
81
111
if (mbedtls_md_setup (& sha_ctx , mbedtls_md_info_from_type (MBEDTLS_MD_SHA256 ), 1 ) != 0 ) {
82
- err = "mbedtls_md_setup error " ;
112
+ err = "Failed to set up message-digest information " ;
83
113
goto exit ;
84
114
}
85
115
86
116
// Check ciphertext has enough space
87
117
size_t new_len = plaintext_len + (AES_BLOCK_SIZE - plaintext_len % 16 );
88
118
if (new_len > ciphertext_len ) {
89
- err = "ciphertext has not enough space" ;
119
+ err = "Failed to get enough space inside ciphertext buffer" ;
120
+ status = SC_UTILS_CIPHER_ERROR ;
90
121
goto exit ;
91
122
}
92
123
cipher_ctx -> ciphertext_len = new_len ;
93
124
mbedtls_platform_zeroize (tmp , sizeof (tmp ));
94
125
mbedtls_platform_zeroize (digest , sizeof (digest ));
95
126
mbedtls_platform_zeroize (ciphertext , sizeof (ciphertext ));
96
127
97
- // fetch timestamp
98
- uint64_t timestamp = time (NULL );
99
128
// concatenate (Device_ID, timestamp)
100
- snprintf ((char * )nonce , IMSI_LEN + MAX_TIMESTAMP_LEN + 1 , "%s-%ld" , cipher_ctx -> device_id , timestamp );
129
+ snprintf ((char * )nonce , IMSI_LEN + MAX_TIMESTAMP_LEN + 1 , "%s-%ld" , cipher_ctx -> device_id , cipher_ctx -> timestamp );
101
130
// hash base data
102
131
mbedtls_md_starts (& sha_ctx );
103
132
mbedtls_md_update (& sha_ctx , digest , AES_BLOCK_SIZE * 2 );
104
133
mbedtls_md_update (& sha_ctx , nonce , IMSI_LEN + MAX_TIMESTAMP_LEN );
134
+ mbedtls_md_update (& sha_ctx , cipher_ctx -> key , TA_AES_KEY_BITS / 8 );
105
135
mbedtls_md_finish (& sha_ctx , digest );
106
136
107
137
for (int i = 0 ; i < AES_BLOCK_SIZE ; ++ i ) {
@@ -111,7 +141,14 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
111
141
112
142
/* set encryption key */
113
143
if ((status = mbedtls_aes_setkey_enc (& ctx , cipher_ctx -> key , TA_AES_KEY_BITS )) != 0 ) {
114
- err = "set aes key failed" ;
144
+ err = "Failed to set AES key" ;
145
+ status = SC_UTILS_CIPHER_ERROR ;
146
+ goto exit ;
147
+ }
148
+
149
+ if ((status = mbedtls_md_hmac_starts (& sha_ctx , digest , TA_AES_HMAC_SIZE )) != 0 ) {
150
+ err = "Failed to initialize HMAC context" ;
151
+ status = SC_UTILS_CIPHER_ERROR ;
115
152
goto exit ;
116
153
}
117
154
@@ -121,19 +158,22 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
121
158
int n = (plaintext_len - i > AES_BLOCK_SIZE ) ? AES_BLOCK_SIZE : (int )(plaintext_len - i );
122
159
memcpy (buf , plaintext + i , n );
123
160
if ((status = mbedtls_aes_crypt_cbc (& ctx , MBEDTLS_AES_ENCRYPT , AES_BLOCK_SIZE , tmp , buf , buf )) != 0 ) {
124
- err = "aes decrpyt failed" ;
161
+ err = "Failed to encrypt AES message" ;
162
+ status = SC_UTILS_CIPHER_ERROR ;
125
163
goto exit ;
126
164
}
165
+ mbedtls_md_hmac_update (& sha_ctx , buf , AES_BLOCK_SIZE );
127
166
memcpy (ciphertext , buf , AES_BLOCK_SIZE );
128
167
ciphertext += AES_BLOCK_SIZE ;
129
168
}
130
169
131
- mbedtls_aes_free ( & ctx );
132
- mbedtls_md_free ( & sha_ctx );
133
- return SC_OK ;
170
+ mbedtls_md_hmac_finish ( & sha_ctx , digest );
171
+ memcpy ( cipher_ctx -> hmac , digest , TA_AES_HMAC_SIZE );
172
+ status = SC_OK ;
134
173
exit :
135
- fprintf (stderr , "%s" , err );
174
+ // FIXME: Use default logger instead
175
+ if (!err ) fprintf (stderr , "%s\n" , err );
136
176
mbedtls_aes_free (& ctx );
137
177
mbedtls_md_free (& sha_ctx );
138
- return SC_UTILS_CIPHER_ERROR ;
178
+ return status ;
139
179
}
0 commit comments