Skip to content

Commit 85cada0

Browse files
committed
samples: drivers: crypto: Add AES CFB and OFB mode
Extend the crypto sample to include support for CFB and OFB modes of operation. Signed-off-by: Girinandha Manivelpandiyan <[email protected]>
1 parent 56f2f14 commit 85cada0

File tree

1 file changed

+169
-0
lines changed
  • samples/drivers/crypto/src

1 file changed

+169
-0
lines changed

samples/drivers/crypto/src/main.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ LOG_MODULE_REGISTER(main);
3232
#define CRYPTO_DEV_COMPAT ti_cc23x0_aes
3333
#elif CONFIG_CRYPTO_SI32
3434
#define CRYPTO_DEV_COMPAT silabs_si32_aes
35+
#elif CONFIG_CRYPTO_MSPM0_AES
36+
#define CRYPTO_DEV_COMPAT ti_mspm0_aes
3537
#else
3638
#error "You need to enable one crypto device"
3739
#endif
@@ -282,6 +284,171 @@ void cbc_mode(const struct device *dev)
282284
cipher_free_session(dev, &ini);
283285
}
284286

287+
static const uint8_t cfb_ciphertext[64] = {
288+
0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8,
289+
0xe8, 0x3c, 0xfb, 0x4a, 0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f,
290+
0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b, 0x26, 0x75, 0x1f, 0x67,
291+
0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
292+
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f,
293+
0x9f, 0xf7, 0xf2, 0xe6
294+
};
295+
296+
void cfb_mode(const struct device *dev)
297+
{
298+
uint8_t encrypted[64] __aligned(IO_ALIGNMENT_BYTES) = {0};
299+
uint8_t decrypted[64] __aligned(IO_ALIGNMENT_BYTES) = {0};
300+
struct cipher_ctx ini = {
301+
.keylen = sizeof(key),
302+
.key.bit_stream = key,
303+
.flags = cap_flags,
304+
};
305+
struct cipher_pkt encrypt = {
306+
.in_buf = plaintext,
307+
.in_len = sizeof(plaintext),
308+
.out_buf_max = sizeof(encrypted),
309+
.out_buf = encrypted,
310+
};
311+
struct cipher_pkt decrypt = {
312+
.in_buf = encrypt.out_buf,
313+
.in_len = sizeof(encrypted),
314+
.out_buf = decrypted,
315+
.out_buf_max = sizeof(decrypted),
316+
};
317+
318+
static uint8_t iv[16] = {
319+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
320+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
321+
};
322+
323+
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES, CRYPTO_CIPHER_MODE_CFB,
324+
CRYPTO_CIPHER_OP_ENCRYPT)) {
325+
return;
326+
}
327+
328+
if (cipher_cfb_op(&ini, &encrypt, iv)) {
329+
LOG_ERR("CFB mode ENCRYPT - Failed");
330+
goto out;
331+
}
332+
333+
LOG_INF("Output length (encryption): %d", encrypt.out_len);
334+
335+
if (memcmp(encrypt.out_buf, cfb_ciphertext, sizeof(cfb_ciphertext))) {
336+
LOG_ERR("CFB mode ENCRYPT - Mismatch between expected and "
337+
"returned cipher text");
338+
print_buffer_comparison(cfb_ciphertext, encrypt.out_buf,
339+
sizeof(cfb_ciphertext));
340+
goto out;
341+
}
342+
343+
LOG_INF("CFB mode ENCRYPT - Match");
344+
cipher_free_session(dev, &ini);
345+
346+
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES, CRYPTO_CIPHER_MODE_CFB,
347+
CRYPTO_CIPHER_OP_DECRYPT)) {
348+
return;
349+
}
350+
351+
if (cipher_cfb_op(&ini, &decrypt, iv)) {
352+
LOG_ERR("CFB mode DECRYPT - Failed");
353+
goto out;
354+
}
355+
356+
LOG_INF("Output length (decryption): %d", decrypt.out_len);
357+
358+
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
359+
LOG_ERR("CFB mode DECRYPT - Mismatch between plaintext and "
360+
"decrypted cipher text");
361+
print_buffer_comparison(plaintext, decrypt.out_buf, sizeof(plaintext));
362+
goto out;
363+
}
364+
365+
LOG_INF("CFB mode DECRYPT - Match");
366+
out:
367+
cipher_free_session(dev, &ini);
368+
}
369+
370+
static const uint8_t ofb_ciphertext[64] = {
371+
0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8,
372+
0xe8, 0x3c, 0xfb, 0x4a, 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
373+
0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25, 0x97, 0x40, 0x05, 0x1e,
374+
0x9c, 0x5f, 0xec, 0xf6, 0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc,
375+
0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78, 0x66, 0xa5, 0x10, 0xd9,
376+
0xc1, 0xd6, 0xae, 0x5e
377+
};
378+
379+
void ofb_mode(const struct device *dev)
380+
{
381+
uint8_t encrypted[64] __aligned(IO_ALIGNMENT_BYTES) = {0};
382+
uint8_t decrypted[64] __aligned(IO_ALIGNMENT_BYTES) = {0};
383+
struct cipher_ctx ini = {
384+
.keylen = sizeof(key),
385+
.key.bit_stream = key,
386+
.flags = cap_flags,
387+
};
388+
struct cipher_pkt encrypt = {
389+
.in_buf = plaintext,
390+
.in_len = sizeof(plaintext),
391+
.out_buf_max = sizeof(encrypted),
392+
.out_buf = encrypted,
393+
};
394+
struct cipher_pkt decrypt = {
395+
.in_buf = encrypt.out_buf,
396+
.in_len = sizeof(encrypted),
397+
.out_buf = decrypted,
398+
.out_buf_max = sizeof(decrypted),
399+
};
400+
401+
static uint8_t iv[16] = {
402+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
403+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
404+
};
405+
406+
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES, CRYPTO_CIPHER_MODE_OFB,
407+
CRYPTO_CIPHER_OP_ENCRYPT)) {
408+
return;
409+
}
410+
411+
if (cipher_ofb_op(&ini, &encrypt, iv)) {
412+
LOG_ERR("OFB mode ENCRYPT - Failed");
413+
goto out;
414+
}
415+
416+
LOG_INF("Output length (encryption): %d", encrypt.out_len);
417+
418+
if (memcmp(encrypt.out_buf, ofb_ciphertext, sizeof(ofb_ciphertext))) {
419+
LOG_ERR("OFB mode ENCRYPT - Mismatch between expected and "
420+
"returned cipher text");
421+
print_buffer_comparison(ofb_ciphertext, encrypt.out_buf, sizeof(ofb_ciphertext));
422+
goto out;
423+
}
424+
425+
LOG_INF("OFB mode ENCRYPT - Match");
426+
cipher_free_session(dev, &ini);
427+
428+
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES, CRYPTO_CIPHER_MODE_OFB,
429+
CRYPTO_CIPHER_OP_DECRYPT)) {
430+
return;
431+
}
432+
433+
if (cipher_cbc_op(&ini, &decrypt, iv)) {
434+
LOG_ERR("OFB mode DECRYPT - Failed");
435+
goto out;
436+
}
437+
438+
LOG_INF("Output length (decryption): %d", decrypt.out_len);
439+
440+
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
441+
LOG_ERR("OFB mode DECRYPT - Mismatch between plaintext and "
442+
"decrypted cipher text");
443+
print_buffer_comparison(plaintext, decrypt.out_buf, sizeof(plaintext));
444+
goto out;
445+
}
446+
447+
LOG_INF("OFB mode DECRYPT - Match");
448+
out:
449+
cipher_free_session(dev, &ini);
450+
}
451+
285452
static const uint8_t ctr_ciphertext[64] = {
286453
0x22, 0xe5, 0x2f, 0xb1, 0x77, 0xd8, 0x65, 0xb2,
287454
0xf7, 0xc6, 0xb5, 0x12, 0x69, 0x2d, 0x11, 0x4d,
@@ -606,6 +773,8 @@ int main(void)
606773
const struct mode_test modes[] = {
607774
{ .mode = "ECB Mode", .mode_func = ecb_mode },
608775
{ .mode = "CBC Mode", .mode_func = cbc_mode },
776+
{ .mode = "CFB Mode", .mode_func = cfb_mode },
777+
{ .mode = "OFB Mode", .mode_func = ofb_mode },
609778
{ .mode = "CTR Mode", .mode_func = ctr_mode },
610779
{ .mode = "CCM Mode", .mode_func = ccm_mode },
611780
{ .mode = "GCM Mode", .mode_func = gcm_mode },

0 commit comments

Comments
 (0)