Skip to content

Commit

Permalink
tests: crypto: Fully initialize variables using named initializers
Browse files Browse the repository at this point in the history
This should get rid of Valgrind warnings in zephyrproject-rtos#7478.

Signed-off-by: Leandro Pereira <[email protected]>
  • Loading branch information
lpereira authored and nashif committed Jun 5, 2018
1 parent c5cb8e9 commit a026b9e
Showing 1 changed file with 81 additions and 80 deletions.
161 changes: 81 additions & 80 deletions samples/drivers/crypto/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
#error "You need to enable one crypto device"
#endif

u8_t key[16] = {
static u8_t key[16] = {
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88,
0x09, 0xcf, 0x4f, 0x3c
};
u8_t iv[16] = {
static u8_t iv[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f
};
u8_t plaintext[64] = {
static u8_t plaintext[64] = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b,
0xe6, 0x6c, 0x37, 0x10
};
u8_t ciphertext[80] = {
static u8_t ciphertext[80] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f, 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d, 0x50, 0x86, 0xcb, 0x9b,
Expand Down Expand Up @@ -114,11 +114,25 @@ int validate_hw_compatibility(struct device *dev)
void cbc_mode(void)
{
struct device *dev;
struct cipher_ctx ini;
struct cipher_pkt encrypt;
struct cipher_pkt decrypt;
u8_t encrypted[80];
u8_t decrypted[64];
u8_t encrypted[80] = {0};
u8_t decrypted[64] = {0};
struct cipher_ctx ini = {
.keylen = sizeof(key),
.key.bit_stream = key,
.flags = cap_flags
};
struct cipher_pkt encrypt = {
.in_buf = plaintext,
.in_len = sizeof(plaintext),
.out_buf_max = sizeof(encrypted),
.out_buf = encrypted,
};
struct cipher_pkt decrypt = {
.in_buf = encrypt.out_buf,
.in_len = sizeof(encrypted),
.out_buf = decrypted,
.out_buf_max = sizeof(decrypted),
};

SYS_LOG_INF("CBC Mode");

Expand All @@ -133,21 +147,12 @@ void cbc_mode(void)
return;
}

ini.keylen = 16;
ini.key.bit_stream = key;
ini.flags = cap_flags;

if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
CRYPTO_CIPHER_MODE_CBC,
CRYPTO_CIPHER_OP_ENCRYPT)) {
return;
}

encrypt.in_buf = plaintext;
encrypt.in_len = sizeof(plaintext);
encrypt.out_buf_max = sizeof(encrypted);
encrypt.out_buf = encrypted;

if (cipher_cbc_op(&ini, &encrypt, iv)) {
SYS_LOG_ERR("CBC mode ENCRYPT - Failed");
goto out;
Expand All @@ -172,11 +177,6 @@ void cbc_mode(void)
return;
}

decrypt.in_buf = encrypt.out_buf; /* encrypted */
decrypt.in_len = sizeof(encrypted);
decrypt.out_buf = decrypted;
decrypt.out_buf_max = sizeof(decrypted);

/* TinyCrypt keeps IV at the start of encrypted buffer */
if (cipher_cbc_op(&ini, &decrypt, encrypted)) {
SYS_LOG_ERR("CBC mode DECRYPT - Failed");
Expand All @@ -198,7 +198,7 @@ void cbc_mode(void)
cipher_free_session(dev, &ini);
}

u8_t ctr_ciphertext[64] = {
static u8_t ctr_ciphertext[64] = {
0x22, 0xe5, 0x2f, 0xb1, 0x77, 0xd8, 0x65, 0xb2,
0xf7, 0xc6, 0xb5, 0x12, 0x69, 0x2d, 0x11, 0x4d,
0xed, 0x6c, 0x1c, 0x72, 0x25, 0xda, 0xf6, 0xa2,
Expand All @@ -212,11 +212,27 @@ u8_t ctr_ciphertext[64] = {
void ctr_mode(void)
{
struct device *dev;
struct cipher_ctx ini;
struct cipher_pkt encrypt;
struct cipher_pkt decrypt;
u8_t encrypted[64] = {0};
u8_t decrypted[64] = {0};
struct cipher_ctx ini = {
.keylen = sizeof(key),
.key.bit_stream = key,
.flags = cap_flags,
/* ivlen + ctrlen = keylen , so ctrlen is 128 - 96 = 32 bits */
.mode_params.ctr_info.ctr_len = 32,
};
struct cipher_pkt encrypt = {
.in_buf = plaintext,
.in_len = sizeof(plaintext),
.out_buf_max = sizeof(encrypted),
.out_buf = encrypted,
};
struct cipher_pkt decrypt = {
.in_buf = encrypted,
.in_len = sizeof(encrypted),
.out_buf = decrypted,
.out_buf_max = sizeof(decrypted),
};
u8_t iv[12] = {
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb
Expand All @@ -235,24 +251,12 @@ void ctr_mode(void)
return;
}

ini.keylen = 16;
ini.key.bit_stream = key;
ini.flags = cap_flags;
/* ivlen + ctrlen = keylen , so ctrlen is 128 - 96 = 32 bits */
ini.mode_params.ctr_info.ctr_len = 32;

if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
CRYPTO_CIPHER_MODE_CTR,
CRYPTO_CIPHER_OP_ENCRYPT)) {
return;
}

encrypt.in_buf = plaintext;

encrypt.in_len = sizeof(plaintext);
encrypt.out_buf_max = sizeof(encrypted);
encrypt.out_buf = encrypted;

if (cipher_ctr_op(&ini, &encrypt, iv)) {
SYS_LOG_ERR("CTR mode ENCRYPT - Failed");
goto out;
Expand All @@ -277,11 +281,6 @@ void ctr_mode(void)
return;
}

decrypt.in_buf = encrypted;
decrypt.in_len = sizeof(encrypted);
decrypt.out_buf = decrypted;
decrypt.out_buf_max = sizeof(decrypted);

if (cipher_ctr_op(&ini, &decrypt, iv)) {
SYS_LOG_ERR("CTR mode DECRYPT - Failed");
goto out;
Expand All @@ -303,22 +302,22 @@ void ctr_mode(void)
}

/* RFC 3610 test vector #1 */
u8_t ccm_key[16] = {
static u8_t ccm_key[16] = {
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
0xcc, 0xcd, 0xce, 0xcf
};
u8_t ccm_nonce[13] = {
static u8_t ccm_nonce[13] = {
0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
0xa5
};
u8_t ccm_hdr[8] = {
static u8_t ccm_hdr[8] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
};
u8_t ccm_data[23] = {
static u8_t ccm_data[23] = {
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
};
u8_t ccm_expected[31] = {
static u8_t ccm_expected[31] = {
0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, 0xf0, 0x66, 0xd0, 0xc2,
0xc0, 0xf9, 0x89, 0x80, 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17,
0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
Expand All @@ -327,12 +326,41 @@ u8_t ccm_expected[31] = {
void ccm_mode(void)
{
struct device *dev;
struct cipher_ctx ini;
struct cipher_pkt encrypt;
struct cipher_aead_pkt ccm_op;
struct cipher_pkt decrypt;
u8_t encrypted[50];
u8_t decrypted[25];
struct cipher_ctx ini = {
.keylen = sizeof(ccm_key),
.key.bit_stream = ccm_key,
.mode_params.ccm_info = {
.nonce_len = sizeof(ccm_nonce),
.tag_len = 8,
},
.flags = cap_flags,
};
struct cipher_pkt encrypt = {
.in_buf = ccm_data,
.in_len = sizeof(ccm_data),
.out_buf_max = sizeof(encrypted),
.out_buf = encrypted,
};
struct cipher_aead_pkt ccm_op = {
.ad = ccm_hdr,
.ad_len = sizeof(ccm_hdr),
.pkt = &encrypt,
/* TinyCrypt always puts the tag at the end of the ciphered
* text, but other library such as mbedtls might be more
* flexible and can take a different buffer for it. So to
* make sure test passes on all backends: enforcing the tag
* buffer to be after the ciphered text.
*/
.tag = encrypted + sizeof(ccm_data),
};
struct cipher_pkt decrypt = {
.in_buf = encrypted,
.in_len = sizeof(ccm_data),
.out_buf = decrypted,
.out_buf_max = sizeof(decrypted),
};

SYS_LOG_INF("CCM Mode");

Expand All @@ -347,34 +375,13 @@ void ccm_mode(void)
return;
}

ini.keylen = sizeof(ccm_key);
ini.key.bit_stream = ccm_key;
ini.mode_params.ccm_info.nonce_len = sizeof(ccm_nonce);
ini.mode_params.ccm_info.tag_len = 8;
ini.flags = cap_flags;

if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
CRYPTO_CIPHER_MODE_CCM,
CRYPTO_CIPHER_OP_ENCRYPT)) {
return;
}

encrypt.in_buf = ccm_data;
encrypt.in_len = sizeof(ccm_data);
encrypt.out_buf_max = sizeof(encrypted);
encrypt.out_buf = encrypted;

ccm_op.ad = ccm_hdr;
ccm_op.ad_len = sizeof(ccm_hdr);
ccm_op.pkt = &encrypt;

/* TinyCrypt always puts the tag at the end of the ciphered text,
* but other library such as mbedtls might be more flexible and can
* take a different buffer for it. So to make sure test passes on
* all backends: enforcing the tag buffer to be after the ciphered
* text. */
ccm_op.tag = encrypted + sizeof(ccm_data);

if (cipher_ccm_op(&ini, &ccm_op, ccm_nonce)) {
SYS_LOG_ERR("CCM mode ENCRYPT - Failed");
goto out;
Expand All @@ -399,13 +406,7 @@ void ccm_mode(void)
return;
}

decrypt.in_buf = encrypted;
decrypt.in_len = sizeof(ccm_data);
decrypt.out_buf = decrypted;
decrypt.out_buf_max = sizeof(decrypted);

ccm_op.pkt = &decrypt;

if (cipher_ccm_op(&ini, &ccm_op, ccm_nonce)) {
SYS_LOG_ERR("CCM mode DECRYPT - Failed");
goto out;
Expand Down

0 comments on commit a026b9e

Please sign in to comment.