16
16
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_CRYPTO_LEVEL
17
17
#include <logging/sys_log.h>
18
18
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
+
19
27
u8_t key [16 ] = {
20
28
0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 ,
21
29
0x09 , 0xcf , 0x4f , 0x3c
@@ -44,6 +52,36 @@ u8_t ciphertext[80] = {
44
52
45
53
u32_t cap_flags ;
46
54
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
+
47
85
int validate_hw_compatibility (struct device * dev )
48
86
{
49
87
u32_t flags = 0 ;
@@ -77,16 +115,16 @@ void cbc_mode(void)
77
115
{
78
116
struct device * dev ;
79
117
struct cipher_ctx ini ;
80
- struct cipher_pkt encrpt ;
118
+ struct cipher_pkt encrypt ;
81
119
struct cipher_pkt decrypt ;
82
120
u8_t encrypted [80 ];
83
121
u8_t decrypted [64 ];
84
122
85
123
SYS_LOG_INF ("CBC Mode" );
86
124
87
- dev = device_get_binding (CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME );
125
+ dev = device_get_binding (CRYPTO_DRV_NAME );
88
126
if (!dev ) {
89
- SYS_LOG_ERR ("TinyCrypt pseudo device not found" );
127
+ SYS_LOG_ERR ("%s pseudo device not found" , CRYPTO_DRV_NAME );
90
128
return ;
91
129
}
92
130
@@ -105,19 +143,21 @@ void cbc_mode(void)
105
143
return ;
106
144
}
107
145
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 ;
112
150
113
- if (cipher_cbc_op (& ini , & encrpt , iv )) {
151
+ if (cipher_cbc_op (& ini , & encrypt , iv )) {
114
152
SYS_LOG_ERR ("CBC mode ENCRYPT - Failed" );
115
153
goto out ;
116
154
}
117
155
118
- if (memcmp (encrpt .out_buf , ciphertext , sizeof (ciphertext ))) {
156
+ if (memcmp (encrypt .out_buf , ciphertext , sizeof (ciphertext ))) {
119
157
SYS_LOG_ERR ("CBC mode ENCRYPT - Mismatch between expected and "
120
158
"returned cipher text" );
159
+ print_buffer_comparison (ciphertext ,
160
+ encrypt .out_buf , sizeof (ciphertext ));
121
161
goto out ;
122
162
}
123
163
@@ -130,7 +170,7 @@ void cbc_mode(void)
130
170
return ;
131
171
}
132
172
133
- decrypt .in_buf = encrpt .out_buf ; /* encrypted */
173
+ decrypt .in_buf = encrypt .out_buf ; /* encrypted */
134
174
decrypt .in_len = sizeof (encrypted );
135
175
decrypt .out_buf = decrypted ;
136
176
decrypt .out_buf_max = sizeof (decrypted );
@@ -144,6 +184,8 @@ void cbc_mode(void)
144
184
if (memcmp (decrypt .out_buf , plaintext , sizeof (plaintext ))) {
145
185
SYS_LOG_ERR ("CBC mode DECRYPT - Mismatch between plaintext and "
146
186
"decrypted cipher text" );
187
+ print_buffer_comparison (plaintext ,
188
+ decrypt .out_buf , sizeof (plaintext ));
147
189
goto out ;
148
190
}
149
191
@@ -167,7 +209,7 @@ void ctr_mode(void)
167
209
{
168
210
struct device * dev ;
169
211
struct cipher_ctx ini ;
170
- struct cipher_pkt encrpt ;
212
+ struct cipher_pkt encrypt ;
171
213
struct cipher_pkt decrypt ;
172
214
u8_t encrypted [64 ] = {0 };
173
215
u8_t decrypted [64 ] = {0 };
@@ -178,9 +220,9 @@ void ctr_mode(void)
178
220
179
221
SYS_LOG_INF ("CTR Mode" );
180
222
181
- dev = device_get_binding (CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME );
223
+ dev = device_get_binding (CRYPTO_DRV_NAME );
182
224
if (!dev ) {
183
- SYS_LOG_ERR ("TinyCrypt pseudo device not found" );
225
+ SYS_LOG_ERR ("%s crypto device not found" , CRYPTO_DRV_NAME );
184
226
return ;
185
227
}
186
228
@@ -201,20 +243,22 @@ void ctr_mode(void)
201
243
return ;
202
244
}
203
245
204
- encrpt .in_buf = plaintext ;
246
+ encrypt .in_buf = plaintext ;
205
247
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 ;
209
251
210
- if (cipher_ctr_op (& ini , & encrpt , iv )) {
252
+ if (cipher_ctr_op (& ini , & encrypt , iv )) {
211
253
SYS_LOG_ERR ("CTR mode ENCRYPT - Failed" );
212
254
goto out ;
213
255
}
214
256
215
- if (memcmp (encrpt .out_buf , ctr_ciphertext , sizeof (ctr_ciphertext ))) {
257
+ if (memcmp (encrypt .out_buf , ctr_ciphertext , sizeof (ctr_ciphertext ))) {
216
258
SYS_LOG_ERR ("CTR mode ENCRYPT - Mismatch between expected "
217
259
"and returned cipher text" );
260
+ print_buffer_comparison (ctr_ciphertext , encrypt .out_buf ,
261
+ sizeof (ctr_ciphertext ));
218
262
goto out ;
219
263
}
220
264
@@ -240,6 +284,8 @@ void ctr_mode(void)
240
284
if (memcmp (decrypt .out_buf , plaintext , sizeof (plaintext ))) {
241
285
SYS_LOG_ERR ("CTR mode DECRYPT - Mismatch between plaintext "
242
286
"and decypted cipher text" );
287
+ print_buffer_comparison (plaintext ,
288
+ encrypt .out_buf , sizeof (plaintext ));
243
289
goto out ;
244
290
}
245
291
@@ -274,17 +320,17 @@ void ccm_mode(void)
274
320
{
275
321
struct device * dev ;
276
322
struct cipher_ctx ini ;
277
- struct cipher_pkt encrpt ;
323
+ struct cipher_pkt encrypt ;
278
324
struct cipher_aead_pkt ccm_op ;
279
325
struct cipher_pkt decrypt ;
280
326
u8_t encrypted [50 ];
281
327
u8_t decrypted [25 ];
282
328
283
329
SYS_LOG_INF ("CCM Mode" );
284
330
285
- dev = device_get_binding (CONFIG_CRYPTO_TINYCRYPT_SHIM_DRV_NAME );
331
+ dev = device_get_binding (CRYPTO_DRV_NAME );
286
332
if (!dev ) {
287
- SYS_LOG_ERR ("TinyCrypt pseudo device not found" );
333
+ SYS_LOG_ERR ("%s crypto device not found" , CRYPTO_DRV_NAME );
288
334
return ;
289
335
}
290
336
@@ -297,31 +343,40 @@ void ccm_mode(void)
297
343
ini .key .bit_stream = ccm_key ;
298
344
ini .mode_params .ccm_info .nonce_len = sizeof (ccm_nonce );
299
345
ini .mode_params .ccm_info .tag_len = 8 ;
300
- ini .flags = cap_flags ;
346
+ ini .flags = cap_flags ;
301
347
302
348
if (cipher_begin_session (dev , & ini , CRYPTO_CIPHER_ALGO_AES ,
303
349
CRYPTO_CIPHER_MODE_CCM ,
304
350
CRYPTO_CIPHER_OP_ENCRYPT )) {
305
351
return ;
306
352
}
307
353
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 ;
312
358
313
359
ccm_op .ad = ccm_hdr ;
314
360
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 );
316
369
317
370
if (cipher_ccm_op (& ini , & ccm_op , ccm_nonce )) {
318
371
SYS_LOG_ERR ("CCM mode ENCRYPT - Failed" );
319
372
goto out ;
320
373
}
321
374
322
- if (memcmp (encrpt .out_buf , ccm_expected , sizeof (ccm_expected ))) {
375
+ if (memcmp (encrypt .out_buf , ccm_expected , sizeof (ccm_expected ))) {
323
376
SYS_LOG_ERR ("CCM mode ENCRYPT - Mismatch between expected "
324
377
"and returned cipher text" );
378
+ print_buffer_comparison (ccm_expected ,
379
+ encrypt .out_buf , sizeof (ccm_expected ));
325
380
goto out ;
326
381
}
327
382
@@ -349,6 +404,8 @@ void ccm_mode(void)
349
404
if (memcmp (decrypt .out_buf , ccm_data , sizeof (ccm_data ))) {
350
405
SYS_LOG_ERR ("CCM mode DECRYPT - Mismatch between plaintext "
351
406
"and decrypted cipher text" );
407
+ print_buffer_comparison (ccm_data ,
408
+ decrypt .out_buf , sizeof (ccm_data ));
352
409
goto out ;
353
410
}
354
411
0 commit comments