Skip to content

Commit b41dd7d

Browse files
committed
crypto/mbedtls: Add unit test for mynewt GCM
This adds unit test for additional mynewt GCM related APIs
1 parent 165790f commit b41dd7d

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

Diff for: crypto/mbedtls/selftest/src/mbedtls_test.c

+2
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

Diff for: crypto/mbedtls/selftest/src/mbedtls_test.h

+1
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" {
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
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 };
48+
49+
static uint8_t test_tag[16];
50+
static uint8_t test_buf[110];
51+
52+
static int mbedtls_gcm_mynewt_test_crypt(uint8_t enc)
53+
{
54+
int add_len = 40;
55+
mbedtls_gcm_context ctx;
56+
mbedtls_aes_context aes_ctx;
57+
uint8_t *ptr;
58+
59+
uint16_t off;
60+
uint16_t blklen;
61+
uint16_t totlen;
62+
int rc;
63+
64+
if (rsm_ucast_cipher == NULL) {
65+
rsm_ucast_cipher =
66+
mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 256,
67+
MBEDTLS_MODE_ECB);
68+
}
69+
70+
memset(&ctx, 0, sizeof(ctx));
71+
mbedtls_aes_init(&aes_ctx);
72+
rc = mbedtls_gcm_setkey_noalloc(&ctx, rsm_ucast_cipher, key, &aes_ctx);
73+
if (rc) {
74+
goto out;
75+
}
76+
77+
rc = mbedtls_gcm_starts(&ctx,
78+
enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT,
79+
iv, sizeof(iv), NULL, 0);
80+
if (rc) {
81+
goto out;
82+
}
83+
84+
off = 0;
85+
totlen = 110;
86+
87+
while (off < totlen) {
88+
ptr = test_buf + off;
89+
blklen = sizeof(test_buf) - off;
90+
if (blklen < AES_BLK_SZ) {
91+
blklen = AES_BLK_SZ;
92+
} else {
93+
blklen &= ~(AES_BLK_SZ - 1);
94+
}
95+
if (off < add_len) {
96+
if (blklen + off > add_len) {
97+
blklen = add_len - off;
98+
}
99+
} else {
100+
if (blklen + off > totlen) {
101+
blklen = totlen - off;
102+
}
103+
}
104+
105+
if (off < add_len) {
106+
mbedtls_gcm_update_add(&ctx, blklen, ptr);
107+
} else {
108+
rc = mbedtls_gcm_update(&ctx, blklen, ptr, ptr);
109+
if (rc) {
110+
goto out;
111+
}
112+
}
113+
114+
off += blklen;
115+
}
116+
117+
rc = mbedtls_gcm_finish(&ctx, test_tag, sizeof(test_tag));
118+
out:
119+
memset(&ctx, 0, sizeof(ctx));
120+
mbedtls_aes_free(&aes_ctx);
121+
if (rc) {
122+
return 1;
123+
}
124+
return 0;
125+
}
126+
127+
TEST_CASE_SELF(gcm_mynewt_test)
128+
{
129+
int rc;
130+
131+
memcpy(test_buf, initial_data, sizeof(initial_data));
132+
133+
rc = mbedtls_gcm_mynewt_test_crypt(1);
134+
TEST_ASSERT(rc == 0);
135+
136+
rc = mbedtls_gcm_mynewt_test_crypt(0);
137+
TEST_ASSERT(rc == 0);
138+
TEST_ASSERT(memcmp(test_tag, expected_tag, sizeof(test_tag)) == 0);
139+
TEST_ASSERT(memcmp(test_buf, initial_data, sizeof(test_buf)) == 0);
140+
}

0 commit comments

Comments
 (0)