175 lines
5.7 KiB
C++
175 lines
5.7 KiB
C++
/**
|
|
* @file tests/crypt_aes.cpp
|
|
* rfc3602 + di spec
|
|
* @todo use test library and verify against expected digest
|
|
* @todo change rfc3602 test so we strip of the pkcs5 padding, this fails now
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <di/errno.h>
|
|
#include <di/encryption/aes.h>
|
|
#include <di/misc/hexstr.h>
|
|
|
|
struct _crypt_aes_test {
|
|
unsigned int test_case;
|
|
const char *key;
|
|
const char *iv;
|
|
const char *plaintext;
|
|
size_t plaintext_len;
|
|
const char *ciphertext;
|
|
} crypt_aes_test_list[] = {
|
|
{
|
|
.test_case = 1,
|
|
.plaintext_len = 16,
|
|
.key = "06a9214036b8a15b512e03d534120006",
|
|
.iv = "3dafba429d9eb430b422da802c9fac41",
|
|
/* "Single block msg" */
|
|
.plaintext = "53696e676c6520626c6f636b206d7367",
|
|
.ciphertext = "e353779c1079aeb82708942dbe77181a"
|
|
},
|
|
{
|
|
.test_case = 2,
|
|
.plaintext_len = 32,
|
|
.key = "c286696d887c9aa0611bbb3e2025a45a",
|
|
.iv = "562e17996d093d28ddb3ba695a2e6f58",
|
|
.plaintext = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
|
.ciphertext = "d296cd94c2cccf8a3a863028b5e1dc0a7586602d253cfff91b8266bea6d61ab1"
|
|
},
|
|
{
|
|
.test_case = 3,
|
|
.plaintext_len = 48,
|
|
.key = "6c3ea0477630ce21a2ce334aa746c2cd",
|
|
.iv = "c782dc4c098c66cbd9cd27d825682c81",
|
|
/* "This is a 48-byte message (exactly 3 AES blocks)" */
|
|
.plaintext = "5468697320697320612034382d62797465206d657373616765202865786163746c7920332041455320626c6f636b7329",
|
|
.ciphertext = "d0a02b3836451753d493665d33f0e8862dea54cdb293abc7506939276772f8d5021c19216bad525c8579695d83ba2684"
|
|
},
|
|
{
|
|
.test_case = 4,
|
|
.plaintext_len = 64,
|
|
.key = "6c3ea0477630ce21a2ce334aa746c2cd",
|
|
.iv = "c782dc4c098c66cbd9cd27d825682c81",
|
|
.plaintext = "5468697320697320612034382d62797465206d657373616765202865786163746c7920332041455320626c6f636b7329",
|
|
.ciphertext = "d0a02b3836451753d493665d33f0e8862dea54cdb293abc7506939276772f8d5021c19216bad525c8579695d83ba2684"
|
|
},
|
|
{ .test_case = 0, .key = NULL, .iv = NULL, .plaintext = NULL, .ciphertext = NULL }
|
|
};
|
|
|
|
void aes_cbc_128_rfc3602_encrypt(struct _crypt_aes_test *t)
|
|
{
|
|
uint8_t key[16];
|
|
uint8_t iv[16];
|
|
uint8_t plaintext[32];
|
|
uint16_t plaintext_len = 32;
|
|
uint8_t ciphertext[256];
|
|
uint16_t ciphertext_len = 256;
|
|
char ciphertext_str[256];
|
|
|
|
di_hexstr_to_bin((void *)key, 16, (void *)t->key, (uint16_t)strlen(t->key));
|
|
di_hexstr_to_bin((void *)iv, 16, (void *)t->iv, (uint16_t)strlen(t->iv));
|
|
di_hexstr_to_bin((void *)plaintext, plaintext_len, (void *)t->plaintext, (uint16_t)strlen(t->plaintext));
|
|
|
|
aes128cbc_encrypt(ciphertext, &ciphertext_len,
|
|
(const uint8_t *)plaintext, plaintext_len,
|
|
(const uint8_t *)key, (const uint8_t *)iv);
|
|
|
|
di_hexstr_from_bin((void *)ciphertext_str, sizeof(ciphertext_str),
|
|
ciphertext, ciphertext_len);
|
|
|
|
printf("=== %s case #%u ===\n", __func__, t->test_case);
|
|
printf("plaintext: %s\n", t->plaintext);
|
|
printf("iv: 0x%s\n", t->iv);
|
|
printf("ciphertext_exp: 0x%s\n", t->ciphertext);
|
|
printf("ciphertext_out: 0x%s\n", ciphertext_str);
|
|
};
|
|
|
|
void aes_cbc_128_rfc3602(void)
|
|
{
|
|
struct _crypt_aes_test *t = &crypt_aes_test_list[0];
|
|
|
|
while (t->key != NULL && t->iv != NULL) {
|
|
aes_cbc_128_rfc3602_encrypt(t);
|
|
t++;
|
|
}
|
|
}
|
|
|
|
void aes_cbc_128_di_encrypt(void)
|
|
{
|
|
char *plaintext_str = "796f752d6172652d617765736f6d652992ccb1cd4122b64d5b0416fa265125";
|
|
char *iv_str = "2a947add5ea5d898b69a57e862012ac4";
|
|
char *key_str = "bcb6e605f77fc34bcd400d234830826a";
|
|
char *ciphertext_exp_str = "61e2775287860dde86b89483f42b440b6b172972ef0f7c8a516d06f5750bfb5e";
|
|
|
|
uint8_t key[16];
|
|
uint8_t iv[16];
|
|
uint8_t plaintext[256];
|
|
uint16_t plaintext_len = strlen(plaintext_str) / 2;
|
|
uint8_t ciphertext[256];
|
|
char ciphertext_str[256];
|
|
uint16_t ciphertext_len = 256;
|
|
|
|
di_hexstr_to_bin((void *)plaintext, 256, plaintext_str, (uint16_t)strlen(plaintext_str));
|
|
di_hexstr_to_bin((void *)key, 16, key_str, (uint16_t)strlen(key_str));
|
|
di_hexstr_to_bin((void *)iv, 16, iv_str, (uint16_t)strlen(iv_str));
|
|
|
|
aes128cbc_encrypt(ciphertext, &ciphertext_len,
|
|
(const uint8_t *)plaintext, plaintext_len,
|
|
(const uint8_t *)key, (const uint8_t *)iv);
|
|
|
|
di_hexstr_from_bin((void *)ciphertext_str, sizeof(ciphertext_str),
|
|
ciphertext, ciphertext_len);
|
|
|
|
printf("=== %s ===\n", __func__);
|
|
printf("plaintext: 0x%s\n", plaintext_str);
|
|
printf("iv: 0x%s\n", iv_str);
|
|
printf("ciphertext_exp: 0x%s\n", ciphertext_exp_str);
|
|
printf("ciphertext_out: 0x%s\n", ciphertext_str);
|
|
};
|
|
|
|
void aes_cbc_128_di_decrypt(void)
|
|
{
|
|
char *ciphertext_str = "61e2775287860dde86b89483f42b440b6b172972ef0f7c8a516d06f5750bfb5e";
|
|
char *iv_str = "2a947add5ea5d898b69a57e862012ac4";
|
|
char *key_str = "bcb6e605f77fc34bcd400d234830826a";
|
|
char *plaintext_exp_str = "796f752d6172652d617765736f6d652992ccb1cd4122b64d5b0416fa265125";
|
|
|
|
uint8_t key[16];
|
|
uint8_t iv[16];
|
|
uint8_t ciphertext[256];
|
|
uint16_t ciphertext_len = 32; /** @todo hardcoded for now */
|
|
char plaintext_str[256];
|
|
uint8_t plaintext[256];
|
|
uint16_t plaintext_len = 256;
|
|
|
|
di_hexstr_to_bin((void *)ciphertext, 256, ciphertext_str, (uint16_t)strlen(ciphertext_str));
|
|
di_hexstr_to_bin((void *)key, 16, key_str, (uint16_t)strlen(key_str));
|
|
di_hexstr_to_bin((void *)iv, 16, iv_str, (uint16_t)strlen(iv_str));
|
|
|
|
aes128cbc_decrypt(plaintext, &plaintext_len,
|
|
(const uint8_t *)ciphertext, ciphertext_len,
|
|
(const uint8_t *)key, (const uint8_t *)iv);
|
|
|
|
di_hexstr_from_bin((void *)plaintext_str, sizeof(plaintext_str),
|
|
plaintext, plaintext_len);
|
|
|
|
printf("=== %s ===\n", __func__);
|
|
printf("ciphertext: 0x%s\n", ciphertext_str);
|
|
printf("iv: 0x%s\n", iv_str);
|
|
printf("key: 0x%s\n", key_str);
|
|
printf("plaintext_exp: 0x%s\n", plaintext_exp_str);
|
|
printf("plaintext_out: 0x%s\n", plaintext_str);
|
|
};
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
aes_cbc_128_rfc3602();
|
|
|
|
aes_cbc_128_di_encrypt();
|
|
aes_cbc_128_di_decrypt();
|
|
|
|
return 0;
|
|
}
|