@@ -24,36 +24,32 @@ static const mbedtls_cipher_info_t *rsm_ucast_cipher;
2424
2525/* This contains both ADD and plaintext for encryption */
2626static const uint8_t initial_data [110 ] = {
27- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x11 ,
28- 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1A , 0x21 , 0x22 ,
29- 0x23 , 0x24 , 0x25 , 0x26 , 0x27 , 0x28 , 0x29 , 0x2A , 0x31 , 0x32 , 0x33 ,
30- 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39 , 0x3A , 0x41 , 0x42 , 0x43 , 0x44 ,
31- 0x45 , 0x46 , 0x47 , 0x48 , 0x49 , 0x4A , 0x51 , 0x52 , 0x53 , 0x54 , 0x55 ,
32- 0x56 , 0x57 , 0x58 , 0x59 , 0x5A , 0x61 , 0x62 , 0x63 , 0x64 , 0x65 , 0x66 ,
33- 0x67 , 0x68 , 0x69 , 0x6A , 0x71 , 0x72 , 0x73 , 0x74 , 0x75 , 0x76 , 0x77 ,
34- 0x78 , 0x79 , 0x7A , 0x81 , 0x82 , 0x83 , 0x84 , 0x85 , 0x86 , 0x87 , 0x88 ,
35- 0x89 , 0x8A , 0x91 , 0x92 , 0x93 , 0x94 , 0x95 , 0x96 , 0x97 , 0x98 , 0x99 ,
36- 0x9A , 0xA1 , 0xA2 , 0xA3 , 0xA4 , 0xA5 , 0xA6 , 0xA7 , 0xA8 , 0xA9 , 0xAA
27+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A ,
28+ 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 , 0x18 , 0x19 , 0x1A ,
29+ 0x21 , 0x22 , 0x23 , 0x24 , 0x25 , 0x26 , 0x27 , 0x28 , 0x29 , 0x2A ,
30+ 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39 , 0x3A ,
31+ 0x41 , 0x42 , 0x43 , 0x44 , 0x45 , 0x46 , 0x47 , 0x48 , 0x49 , 0x4A ,
32+ 0x51 , 0x52 , 0x53 , 0x54 , 0x55 , 0x56 , 0x57 , 0x58 , 0x59 , 0x5A ,
33+ 0x61 , 0x62 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0x68 , 0x69 , 0x6A ,
34+ 0x71 , 0x72 , 0x73 , 0x74 , 0x75 , 0x76 , 0x77 , 0x78 , 0x79 , 0x7A ,
35+ 0x81 , 0x82 , 0x83 , 0x84 , 0x85 , 0x86 , 0x87 , 0x88 , 0x89 , 0x8A ,
36+ 0x91 , 0x92 , 0x93 , 0x94 , 0x95 , 0x96 , 0x97 , 0x98 , 0x99 , 0x9A ,
37+ 0xA1 , 0xA2 , 0xA3 , 0xA4 , 0xA5 , 0xA6 , 0xA7 , 0xA8 , 0xA9 , 0xAA
3738};
38-
39- static const uint8_t key [32 ] = { 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 ,
40- 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA ,
41- 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 ,
42- 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A ,
43- 0xC0 , 0xCA , 0xC0 , 0x1A };
44-
45- static const uint8_t iv [12 ] = { 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 ,
46- 0x6 , 0x7 , 0x8 , 0x9 , 0xA , 0xB };
47-
48- static const uint8_t expected_tag [16 ] = { 0x05 , 0x5D , 0x8E , 0xD4 , 0xF9 , 0x2A ,
49- 0x87 , 0x87 , 0x6F , 0x23 , 0xF2 , 0xE6 ,
50- 0xF0 , 0x1D , 0x6D , 0x5C };
39+ static const uint8_t key [32 ] = {
40+ 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A ,
41+ 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A ,
42+ 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A ,
43+ 0xC0 , 0xCA , 0xC0 , 0x1A , 0xC0 , 0xCA , 0xC0 , 0x1A
44+ };
45+ static const uint8_t iv [12 ] = { 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0x8 , 0x9 , 0xA , 0xB };
46+ static const uint8_t expected_tag [16 ] = { 0x05 , 0x5D , 0x8E , 0xD4 , 0xF9 , 0x2A , 0x87 , 0x87 ,
47+ 0x6F , 0x23 , 0xF2 , 0xE6 , 0xF0 , 0x1D , 0x6D , 0x5C };
5148
5249static uint8_t test_tag [16 ];
5350static uint8_t test_buf [110 ];
5451
55- static int
56- mbedtls_gcm_mynewt_test_crypt (uint8_t enc )
52+ static int mbedtls_gcm_mynewt_test_crypt (uint8_t enc )
5753{
5854 int add_len = 40 ;
5955 mbedtls_gcm_context ctx ;
@@ -63,22 +59,25 @@ mbedtls_gcm_mynewt_test_crypt(uint8_t enc)
6359 uint16_t off ;
6460 uint16_t blklen ;
6561 uint16_t totlen ;
62+ size_t len_check ;
6663 int rc ;
6764
6865 if (rsm_ucast_cipher == NULL ) {
69- rsm_ucast_cipher = mbedtls_cipher_info_from_values (MBEDTLS_CIPHER_ID_AES ,
70- 256 , MBEDTLS_MODE_ECB );
66+ rsm_ucast_cipher =
67+ mbedtls_cipher_info_from_values (MBEDTLS_CIPHER_ID_AES , 256 ,
68+ MBEDTLS_MODE_ECB );
7169 }
7270
7371 memset (& ctx , 0 , sizeof (ctx ));
7472 mbedtls_aes_init (& aes_ctx );
75- rc = mbedtls_gcm_setkey_noalloc (& ctx , rsm_ucast_cipher , key , & aes_ctx );
73+ rc = mbedtls_gcm_setkey_noalloc (& ctx , rsm_ucast_cipher , key , 256 , & aes_ctx );
7674 if (rc ) {
7775 goto out ;
7876 }
7977
80- rc = mbedtls_gcm_starts (& ctx , enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT ,
81- iv , sizeof (iv ), NULL , 0 );
78+ rc = mbedtls_gcm_starts (& ctx ,
79+ enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT ,
80+ iv , sizeof (iv ));
8281 if (rc ) {
8382 goto out ;
8483 }
@@ -105,18 +104,26 @@ mbedtls_gcm_mynewt_test_crypt(uint8_t enc)
105104 }
106105
107106 if (off < add_len ) {
108- mbedtls_gcm_update_add (& ctx , blklen , ptr );
107+ mbedtls_gcm_update_ad (& ctx , ptr , blklen );
109108 } else {
110- rc = mbedtls_gcm_update (& ctx , blklen , ptr , ptr );
109+ rc = mbedtls_gcm_update (& ctx , ptr , blklen , ptr , blklen , & len_check );
111110 if (rc ) {
112111 goto out ;
113112 }
113+ if (len_check != blklen ) {
114+ rc = 1 ;
115+ goto out ;
116+ }
114117 }
115118
116119 off += blklen ;
117120 }
118121
119- rc = mbedtls_gcm_finish (& ctx , test_tag , sizeof (test_tag ));
122+ rc = mbedtls_gcm_finish (& ctx , NULL , 0 , & len_check , test_tag , sizeof (test_tag ));
123+ if (len_check != 0 ) {
124+ rc = 1 ;
125+ goto out ;
126+ }
120127out :
121128 memset (& ctx , 0 , sizeof (ctx ));
122129 mbedtls_aes_free (& aes_ctx );
@@ -138,5 +145,5 @@ TEST_CASE_SELF(gcm_mynewt_test)
138145 rc = mbedtls_gcm_mynewt_test_crypt (0 );
139146 TEST_ASSERT (rc == 0 );
140147 TEST_ASSERT (memcmp (test_tag , expected_tag , sizeof (test_tag )) == 0 );
141- TEST_ASSERT (memcmp (test_buf , initial_data , sizeof (test_buf )) == 0 );
148+ TEST_ASSERT (memcmp (test_buf , initial_data , sizeof (initial_data )) == 0 );
142149}
0 commit comments