2020status_t aes_decrypt (ta_cipher_ctx * cipher_ctx ) {
2121 // FIXME: Add logger and some checks here
2222 mbedtls_aes_context ctx ;
23+ mbedtls_md_context_t sha_ctx ;
2324 int status ;
24- char * err ;
25+ char * err = NULL ;
2526 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 };
2729 /* Create and initialise the context */
2830 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+ }
2937 mbedtls_platform_zeroize (cipher_ctx -> plaintext , sizeof (cipher_ctx -> plaintext ));
3038 mbedtls_platform_zeroize (buf , AES_BLOCK_SIZE );
39+ mbedtls_platform_zeroize (digest , AES_BLOCK_SIZE * 2 );
3140
3241 /* set decryption key */
3342 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 ;
3545 goto exit ;
3646 }
3747
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+
3859 // Provide the message to be decrypted, and obtain the plaintext output.
3960 const size_t ciphertext_len = cipher_ctx -> ciphertext_len ;
4061 uint8_t * ciphertext = cipher_ctx -> ciphertext ;
@@ -43,21 +64,30 @@ status_t aes_decrypt(ta_cipher_ctx* cipher_ctx) {
4364 memset (buf , 0 , AES_BLOCK_SIZE );
4465 int n = (ciphertext_len - i > AES_BLOCK_SIZE ) ? AES_BLOCK_SIZE : (int )(ciphertext_len - i );
4566 memcpy (buf , ciphertext + i , n );
67+ mbedtls_md_hmac_update (& sha_ctx , buf , AES_BLOCK_SIZE );
4668 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 ;
4871 goto exit ;
4972 }
5073 memcpy (plaintext , buf , AES_BLOCK_SIZE );
5174 plaintext += AES_BLOCK_SIZE ;
5275 }
5376
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 ;
5785exit :
58- fprintf (stderr , "%s\n" , err );
86+ // FIXME: Use default logger instead
87+ if (!err ) fprintf (stderr , "%s\n" , err );
5988 mbedtls_aes_free (& ctx );
60- return SC_UTILS_CIPHER_ERROR ;
89+ mbedtls_md_free (& sha_ctx );
90+ return status ;
6191}
6292
6393status_t aes_encrypt (ta_cipher_ctx * cipher_ctx ) {
@@ -79,29 +109,29 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
79109 mbedtls_md_init (& sha_ctx );
80110 mbedtls_aes_init (& ctx );
81111 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 " ;
83113 goto exit ;
84114 }
85115
86116 // Check ciphertext has enough space
87117 size_t new_len = plaintext_len + (AES_BLOCK_SIZE - plaintext_len % 16 );
88118 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 ;
90121 goto exit ;
91122 }
92123 cipher_ctx -> ciphertext_len = new_len ;
93124 mbedtls_platform_zeroize (tmp , sizeof (tmp ));
94125 mbedtls_platform_zeroize (digest , sizeof (digest ));
95126 mbedtls_platform_zeroize (ciphertext , sizeof (ciphertext ));
96127
97- // fetch timestamp
98- uint64_t timestamp = time (NULL );
99128 // 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 );
101130 // hash base data
102131 mbedtls_md_starts (& sha_ctx );
103132 mbedtls_md_update (& sha_ctx , digest , AES_BLOCK_SIZE * 2 );
104133 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 );
105135 mbedtls_md_finish (& sha_ctx , digest );
106136
107137 for (int i = 0 ; i < AES_BLOCK_SIZE ; ++ i ) {
@@ -111,7 +141,14 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
111141
112142 /* set encryption key */
113143 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 ;
115152 goto exit ;
116153 }
117154
@@ -121,19 +158,22 @@ status_t aes_encrypt(ta_cipher_ctx* cipher_ctx) {
121158 int n = (plaintext_len - i > AES_BLOCK_SIZE ) ? AES_BLOCK_SIZE : (int )(plaintext_len - i );
122159 memcpy (buf , plaintext + i , n );
123160 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 ;
125163 goto exit ;
126164 }
165+ mbedtls_md_hmac_update (& sha_ctx , buf , AES_BLOCK_SIZE );
127166 memcpy (ciphertext , buf , AES_BLOCK_SIZE );
128167 ciphertext += AES_BLOCK_SIZE ;
129168 }
130169
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 ;
134173exit :
135- fprintf (stderr , "%s" , err );
174+ // FIXME: Use default logger instead
175+ if (!err ) fprintf (stderr , "%s\n" , err );
136176 mbedtls_aes_free (& ctx );
137177 mbedtls_md_free (& sha_ctx );
138- return SC_UTILS_CIPHER_ERROR ;
178+ return status ;
139179}
0 commit comments