Skip to content

Commit c17bbb5

Browse files
committed
crypto/mbedtls: Add unit test for mynewt GCM
This adds unit test for additional mynewt GCM related APIs
1 parent 7bc61fb commit c17bbb5

3 files changed

Lines changed: 146 additions & 0 deletions

File tree

crypto/mbedtls/selftest/src/mbedtls_test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ TEST_CASE_DECL(sha512_test)
8585
TEST_CASE_DECL(timing_test)
8686
TEST_CASE_DECL(x509_test)
8787
TEST_CASE_DECL(xtea_test)
88+
TEST_CASE_DECL(gcm_mynewt_test)
8889

8990
TEST_SUITE(mbedtls_test_all)
9091
{
@@ -120,6 +121,7 @@ TEST_SUITE(mbedtls_test_all)
120121
timing_test();
121122
x509_test();
122123
xtea_test();
124+
gcm_mynewt_test();
123125
}
124126

125127
int

crypto/mbedtls/selftest/src/mbedtls_test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "mbedtls/timing.h"
5757
#include "mbedtls/x509.h"
5858
#include "mbedtls/xtea.h"
59+
#include "gcm_mynewt.h"
5960

6061
#ifdef __cplusplus
6162
extern "C" {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#include "mbedtls_test.h"
20+
21+
#define AES_BLK_SZ 16
22+
23+
static const mbedtls_cipher_info_t *rsm_ucast_cipher;
24+
25+
/* This contains both ADD and plaintext for encryption */
26+
static const uint8_t initial_data[110] = {
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
38+
};
39+
40+
static const uint8_t key[32] = { 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0,
41+
0x1A, 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA,
42+
0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, 0xC0,
43+
0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A,
44+
0xC0, 0xCA, 0xC0, 0x1A };
45+
46+
static const uint8_t iv[12] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5,
47+
0x6, 0x7, 0x8, 0x9, 0xA, 0xB };
48+
49+
static const uint8_t expected_tag[16] = { 0x05, 0x5D, 0x8E, 0xD4, 0xF9, 0x2A,
50+
0x87, 0x87, 0x6F, 0x23, 0xF2, 0xE6,
51+
0xF0, 0x1D, 0x6D, 0x5C };
52+
53+
static uint8_t test_tag[16];
54+
static uint8_t test_buf[110];
55+
56+
static int
57+
mbedtls_gcm_mynewt_test_crypt(uint8_t enc)
58+
{
59+
int add_len = 40;
60+
mbedtls_gcm_context ctx;
61+
mbedtls_aes_context aes_ctx;
62+
uint8_t *ptr;
63+
64+
uint16_t off;
65+
uint16_t blklen;
66+
uint16_t totlen;
67+
int rc;
68+
69+
if (rsm_ucast_cipher == NULL) {
70+
rsm_ucast_cipher = mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES,
71+
256, MBEDTLS_MODE_ECB);
72+
}
73+
74+
memset(&ctx, 0, sizeof(ctx));
75+
mbedtls_aes_init(&aes_ctx);
76+
rc = mbedtls_gcm_setkey_noalloc(&ctx, rsm_ucast_cipher, key, &aes_ctx);
77+
if (rc) {
78+
goto out;
79+
}
80+
81+
rc = mbedtls_gcm_starts(&ctx, enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
82+
iv, sizeof(iv), NULL, 0);
83+
if (rc) {
84+
goto out;
85+
}
86+
87+
off = 0;
88+
totlen = 110;
89+
90+
while (off < totlen) {
91+
ptr = test_buf + off;
92+
blklen = sizeof(test_buf) - off;
93+
if (blklen < AES_BLK_SZ) {
94+
blklen = AES_BLK_SZ;
95+
} else {
96+
blklen &= ~(AES_BLK_SZ - 1);
97+
}
98+
if (off < add_len) {
99+
if (blklen + off > add_len) {
100+
blklen = add_len - off;
101+
}
102+
} else {
103+
if (blklen + off > totlen) {
104+
blklen = totlen - off;
105+
}
106+
}
107+
108+
if (off < add_len) {
109+
mbedtls_gcm_update_add(&ctx, blklen, ptr);
110+
} else {
111+
rc = mbedtls_gcm_update(&ctx, blklen, ptr, ptr);
112+
if (rc) {
113+
goto out;
114+
}
115+
}
116+
117+
off += blklen;
118+
}
119+
120+
rc = mbedtls_gcm_finish(&ctx, test_tag, sizeof(test_tag));
121+
out:
122+
memset(&ctx, 0, sizeof(ctx));
123+
mbedtls_aes_free(&aes_ctx);
124+
if (rc) {
125+
return 1;
126+
}
127+
return 0;
128+
}
129+
130+
TEST_CASE_SELF(gcm_mynewt_test)
131+
{
132+
int rc;
133+
134+
memcpy(test_buf, initial_data, sizeof(initial_data));
135+
136+
rc = mbedtls_gcm_mynewt_test_crypt(1);
137+
TEST_ASSERT(rc == 0);
138+
139+
rc = mbedtls_gcm_mynewt_test_crypt(0);
140+
TEST_ASSERT(rc == 0);
141+
TEST_ASSERT(memcmp(test_tag, expected_tag, sizeof(test_tag)) == 0);
142+
TEST_ASSERT(memcmp(test_buf, initial_data, sizeof(test_buf)) == 0);
143+
}

0 commit comments

Comments
 (0)