Skip to content

Commit 0873540

Browse files
Tomasz BursztykaAnas Nashif
authored andcommitted
samples/crypto: Add mbedtls shim driver support
Normalizing variables names and make sure tag handling behavior, which might be different among backends, does not make the test failing. Also, improving debug logs in case of error. Change-Id: Ic317948aab459bfa75c9a72ac48cb2d12a0d0706 Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 7abf3c5 commit 0873540

File tree

2 files changed

+101
-29
lines changed

2 files changed

+101
-29
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
#CONFIG_TEST_RANDOM_GENERATOR=y
3+
CONFIG_DEBUG=y
4+
CONFIG_SYS_LOG=y
5+
CONFIG_SYS_LOG_SHOW_COLOR=y
6+
#CONFIG_ASSERT=y
7+
8+
CONFIG_MBEDTLS=y
9+
CONFIG_MBEDTLS_BUILTIN=y
10+
CONFIG_MBEDTLS_CFG_FILE="config-threadnet.h"
11+
CONFIG_MBEDTLS_HEAP_SIZE=512
12+
13+
CONFIG_CRYPTO=y
14+
CONFIG_CRYPTO_MBEDTLS_SHIM=y
15+
CONFIG_SYS_LOG_CRYPTO_LEVEL=4

samples/drivers/crypto/src/main.c

Lines changed: 86 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_CRYPTO_LEVEL
1717
#include <logging/sys_log.h>
1818

19+
#ifdef CONFIG_CRYPTO_TINYCRYPT_SHIM
20+
#define CRYPTO_DRV_NAME CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME
21+
#elif CONFIG_CRYPTO_MBEDTLS_SHIM
22+
#define CRYPTO_DRV_NAME CONFIG_CRYPTO_MBEDTLS_SHIM_DRV_NAME
23+
#else
24+
#error "You need to enable one crypto device"
25+
#endif
26+
1927
u8_t key[16] = {
2028
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88,
2129
0x09, 0xcf, 0x4f, 0x3c
@@ -44,6 +52,36 @@ u8_t ciphertext[80] = {
4452

4553
u32_t cap_flags;
4654

55+
static void print_buffer_comparison(u8_t *wanted_result,
56+
u8_t *result, size_t length)
57+
{
58+
int i, j;
59+
60+
printk("Was waiting for: \n");
61+
62+
for (i = 0, j = 1; i < length; i++, j++) {
63+
printk("0x%02x ", wanted_result[i]);
64+
65+
if (j == 10) {
66+
printk("\n");
67+
j = 0;
68+
}
69+
}
70+
71+
printk("\n But got:\n");
72+
73+
for (i = 0, j = 1; i < length; i++, j++) {
74+
printk("0x%02x ", result[i]);
75+
76+
if (j == 10) {
77+
printk("\n");
78+
j = 0;
79+
}
80+
}
81+
82+
printk("\n");
83+
}
84+
4785
int validate_hw_compatibility(struct device *dev)
4886
{
4987
u32_t flags = 0;
@@ -77,16 +115,16 @@ void cbc_mode(void)
77115
{
78116
struct device *dev;
79117
struct cipher_ctx ini;
80-
struct cipher_pkt encrpt;
118+
struct cipher_pkt encrypt;
81119
struct cipher_pkt decrypt;
82120
u8_t encrypted[80];
83121
u8_t decrypted[64];
84122

85123
SYS_LOG_INF("CBC Mode");
86124

87-
dev = device_get_binding(CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME);
125+
dev = device_get_binding(CRYPTO_DRV_NAME);
88126
if (!dev) {
89-
SYS_LOG_ERR("TinyCrypt pseudo device not found");
127+
SYS_LOG_ERR("%s pseudo device not found", CRYPTO_DRV_NAME);
90128
return;
91129
}
92130

@@ -105,19 +143,21 @@ void cbc_mode(void)
105143
return;
106144
}
107145

108-
encrpt.in_buf = plaintext;
109-
encrpt.in_len = sizeof(plaintext);
110-
encrpt.out_buf_max = sizeof(encrypted);
111-
encrpt.out_buf = encrypted;
146+
encrypt.in_buf = plaintext;
147+
encrypt.in_len = sizeof(plaintext);
148+
encrypt.out_buf_max = sizeof(encrypted);
149+
encrypt.out_buf = encrypted;
112150

113-
if (cipher_cbc_op(&ini, &encrpt, iv)) {
151+
if (cipher_cbc_op(&ini, &encrypt, iv)) {
114152
SYS_LOG_ERR("CBC mode ENCRYPT - Failed");
115153
goto out;
116154
}
117155

118-
if (memcmp(encrpt.out_buf, ciphertext, sizeof(ciphertext))) {
156+
if (memcmp(encrypt.out_buf, ciphertext, sizeof(ciphertext))) {
119157
SYS_LOG_ERR("CBC mode ENCRYPT - Mismatch between expected and "
120158
"returned cipher text");
159+
print_buffer_comparison(ciphertext,
160+
encrypt.out_buf, sizeof(ciphertext));
121161
goto out;
122162
}
123163

@@ -130,7 +170,7 @@ void cbc_mode(void)
130170
return;
131171
}
132172

133-
decrypt.in_buf = encrpt.out_buf; /* encrypted */
173+
decrypt.in_buf = encrypt.out_buf; /* encrypted */
134174
decrypt.in_len = sizeof(encrypted);
135175
decrypt.out_buf = decrypted;
136176
decrypt.out_buf_max = sizeof(decrypted);
@@ -144,6 +184,8 @@ void cbc_mode(void)
144184
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
145185
SYS_LOG_ERR("CBC mode DECRYPT - Mismatch between plaintext and "
146186
"decrypted cipher text");
187+
print_buffer_comparison(plaintext,
188+
decrypt.out_buf, sizeof(plaintext));
147189
goto out;
148190
}
149191

@@ -167,7 +209,7 @@ void ctr_mode(void)
167209
{
168210
struct device *dev;
169211
struct cipher_ctx ini;
170-
struct cipher_pkt encrpt;
212+
struct cipher_pkt encrypt;
171213
struct cipher_pkt decrypt;
172214
u8_t encrypted[64] = {0};
173215
u8_t decrypted[64] = {0};
@@ -178,9 +220,9 @@ void ctr_mode(void)
178220

179221
SYS_LOG_INF("CTR Mode");
180222

181-
dev = device_get_binding(CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME);
223+
dev = device_get_binding(CRYPTO_DRV_NAME);
182224
if (!dev) {
183-
SYS_LOG_ERR("TinyCrypt pseudo device not found");
225+
SYS_LOG_ERR("%s crypto device not found", CRYPTO_DRV_NAME);
184226
return;
185227
}
186228

@@ -201,20 +243,22 @@ void ctr_mode(void)
201243
return;
202244
}
203245

204-
encrpt.in_buf = plaintext;
246+
encrypt.in_buf = plaintext;
205247

206-
encrpt.in_len = sizeof(plaintext);
207-
encrpt.out_buf_max = sizeof(encrypted);
208-
encrpt.out_buf = encrypted;
248+
encrypt.in_len = sizeof(plaintext);
249+
encrypt.out_buf_max = sizeof(encrypted);
250+
encrypt.out_buf = encrypted;
209251

210-
if (cipher_ctr_op(&ini, &encrpt, iv)) {
252+
if (cipher_ctr_op(&ini, &encrypt, iv)) {
211253
SYS_LOG_ERR("CTR mode ENCRYPT - Failed");
212254
goto out;
213255
}
214256

215-
if (memcmp(encrpt.out_buf, ctr_ciphertext, sizeof(ctr_ciphertext))) {
257+
if (memcmp(encrypt.out_buf, ctr_ciphertext, sizeof(ctr_ciphertext))) {
216258
SYS_LOG_ERR("CTR mode ENCRYPT - Mismatch between expected "
217259
"and returned cipher text");
260+
print_buffer_comparison(ctr_ciphertext, encrypt.out_buf,
261+
sizeof(ctr_ciphertext));
218262
goto out;
219263
}
220264

@@ -240,6 +284,8 @@ void ctr_mode(void)
240284
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
241285
SYS_LOG_ERR("CTR mode DECRYPT - Mismatch between plaintext "
242286
"and decypted cipher text");
287+
print_buffer_comparison(plaintext,
288+
encrypt.out_buf, sizeof(plaintext));
243289
goto out;
244290
}
245291

@@ -274,17 +320,17 @@ void ccm_mode(void)
274320
{
275321
struct device *dev;
276322
struct cipher_ctx ini;
277-
struct cipher_pkt encrpt;
323+
struct cipher_pkt encrypt;
278324
struct cipher_aead_pkt ccm_op;
279325
struct cipher_pkt decrypt;
280326
u8_t encrypted[50];
281327
u8_t decrypted[25];
282328

283329
SYS_LOG_INF("CCM Mode");
284330

285-
dev = device_get_binding(CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME);
331+
dev = device_get_binding(CRYPTO_DRV_NAME);
286332
if (!dev) {
287-
SYS_LOG_ERR("TinyCrypt pseudo device not found");
333+
SYS_LOG_ERR("%s crypto device not found", CRYPTO_DRV_NAME);
288334
return;
289335
}
290336

@@ -297,31 +343,40 @@ void ccm_mode(void)
297343
ini.key.bit_stream = ccm_key;
298344
ini.mode_params.ccm_info.nonce_len = sizeof(ccm_nonce);
299345
ini.mode_params.ccm_info.tag_len = 8;
300-
ini.flags = cap_flags;
346+
ini.flags = cap_flags;
301347

302348
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
303349
CRYPTO_CIPHER_MODE_CCM,
304350
CRYPTO_CIPHER_OP_ENCRYPT)) {
305351
return;
306352
}
307353

308-
encrpt.in_buf = ccm_data;
309-
encrpt.in_len = sizeof(ccm_data);
310-
encrpt.out_buf_max = sizeof(encrypted);
311-
encrpt.out_buf = encrypted;
354+
encrypt.in_buf = ccm_data;
355+
encrypt.in_len = sizeof(ccm_data);
356+
encrypt.out_buf_max = sizeof(encrypted);
357+
encrypt.out_buf = encrypted;
312358

313359
ccm_op.ad = ccm_hdr;
314360
ccm_op.ad_len = sizeof(ccm_hdr);
315-
ccm_op.pkt = &encrpt;
361+
ccm_op.pkt = &encrypt;
362+
363+
/* TinyCrypt always puts the tag at the end of the ciphered text,
364+
* but other library such as mbedtls might be more flexible and can
365+
* take a different buffer for it. So to make sure test passes on
366+
* all backends: enforcing the tag buffer to be after the ciphered
367+
* text. */
368+
ccm_op.tag = encrypted + sizeof(ccm_data);
316369

317370
if (cipher_ccm_op(&ini, &ccm_op, ccm_nonce)) {
318371
SYS_LOG_ERR("CCM mode ENCRYPT - Failed");
319372
goto out;
320373
}
321374

322-
if (memcmp(encrpt.out_buf, ccm_expected, sizeof(ccm_expected))) {
375+
if (memcmp(encrypt.out_buf, ccm_expected, sizeof(ccm_expected))) {
323376
SYS_LOG_ERR("CCM mode ENCRYPT - Mismatch between expected "
324377
"and returned cipher text");
378+
print_buffer_comparison(ccm_expected,
379+
encrypt.out_buf, sizeof(ccm_expected));
325380
goto out;
326381
}
327382

@@ -349,6 +404,8 @@ void ccm_mode(void)
349404
if (memcmp(decrypt.out_buf, ccm_data, sizeof(ccm_data))) {
350405
SYS_LOG_ERR("CCM mode DECRYPT - Mismatch between plaintext "
351406
"and decrypted cipher text");
407+
print_buffer_comparison(ccm_data,
408+
decrypt.out_buf, sizeof(ccm_data));
352409
goto out;
353410
}
354411

0 commit comments

Comments
 (0)