X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilccrypt%2Ftests%2Ftest_aes.c;h=7b5877a5a1073bc533a0b0a403422df0ea7b8d20;hb=9f20f0382b6229eca740925a73f96294f6dcedc6;hp=6a802ece007e7d827b9bb7f477308ec6ddfbcdd6;hpb=336a16227b9d8ec20339f0984e9507969cf82bf9;p=crypto.git diff --git a/lib/silccrypt/tests/test_aes.c b/lib/silccrypt/tests/test_aes.c index 6a802ece..7b5877a5 100644 --- a/lib/silccrypt/tests/test_aes.c +++ b/lib/silccrypt/tests/test_aes.c @@ -1,5 +1,5 @@ -#include "silcincludes.h" +#include "silccrypto.h" /* Test vectors from RFC3602. */ @@ -19,11 +19,46 @@ const unsigned char p2[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c int p2_len = 32; const unsigned char c2[] = "\xd2\x96\xcd\x94\xc2\xcc\xcf\x8a\x3a\x86\x30\x28\xb5\xe1\xdc\x0a\x75\x86\x60\x2d\x25\x3c\xff\xf9\x1b\x82\x66\xbe\xa6\xd6\x1a\xb1"; +/* CTR test vectors from RFC3686. */ + +/* 16 bytes plaintext, 128 bits key */ +const unsigned char key3[] = "\xAE\x68\x52\xF8\x12\x10\x67\xCC\x4B\xF7\xA5\x76\x55\x77\xF3\x9E"; +int key3_len = 16 * 8; +const unsigned char iv3[] = "\x00\x00\x00\x30\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; +const unsigned char p3[] = "Single block msg"; +int p3_len = 16; +const unsigned char c3[] = "\xE4\x09\x5D\x4F\xB7\xA7\xB3\x79\x2D\x61\x75\xA3\x26\x13\x11\xB8"; + +/* 32 bytes plaintext, 128 bits key */ +const unsigned char key4[] = "\x7E\x24\x06\x78\x17\xFA\xE0\xD7\x43\xD6\xCE\x1F\x32\x53\x91\x63"; +int key4_len = 16 * 8; +const unsigned char iv4[] = "\x00\x6C\xB6\xDB\xC0\x54\x3B\x59\xDA\x48\xD9\x0B\x00\x00\x00\x00"; +const unsigned char p4[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"; +int p4_len = 32; +const unsigned char c4[] = "\x51\x04\xA1\x06\x16\x8A\x72\xD9\x79\x0D\x41\xEE\x8E\xDA\xD3\x88\xEB\x2E\x1E\xFC\x46\xDA\x57\xC8\xFC\xE6\x30\xDF\x91\x41\xBE\x28"; + +/* 36 bytes plaintext, 256 bits key */ +const unsigned char key5[] = "\xFF\x7A\x61\x7C\xE6\x91\x48\xE4\xF1\x72\x6E\x2F\x43\x58\x1D\xE2\xAA\x62\xD9\xF8\x05\x53\x2E\xDF\xF1\xEE\xD6\x87\xFB\x54\x15\x3D"; +int key5_len = 32 * 8; +const unsigned char iv5[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00"; +const unsigned char p5[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23"; +int p5_len = 36; +const unsigned char c5[] = "\xEB\x6C\x52\x82\x1D\x0B\xBB\xF7\xCE\x75\x94\x46\x2A\xCA\x4F\xAA\xB4\x07\xDF\x86\x65\x69\xFD\x07\xF4\x8C\xC0\xB5\x83\xD6\x07\x1F\x1E\xC0\xE6\xB8"; + +/* CFB */ + +/* 36 bytes plaintext, 256 bits key */ +const unsigned char key6[] = "\xFF\x7A\x61\x7C\xE6\x91\x48\xE4\xF1\x72\x6E\x2F\x43\x58\x1D\xE2\xAA\x62\xD9\xF8\x05\x53\x2E\xDF\xF1\xEE\xD6\x87\xFB\x54\x15\x3D"; +int key6_len = 32 * 8; +const unsigned char iv6[] = "\x00\x1C\xC5\xB7\x51\xA5\x1D\x70\xA1\xC1\x11\x48\x00\x00\x00\x00"; +const unsigned char p6[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23"; +int p6_len = 36; +const unsigned char c6[] = "\x7C\x4C\x1D\xB1\x25\x20\x76\x4E\x86\x57\x16\xEF\x82\x93\x53\x62\xE8\x99\x98\x11\xCB\x83\x1A\xEA\x61\x0D\xC7\xE3\x91\x19\x5F\x5D\xD8\xA6\x7B\xD5"; int main(int argc, char **argv) { - bool success = FALSE; - SilcCipher cipher; + SilcBool success = FALSE; + SilcCipher cipher, cipher2; unsigned char dst[256], pdst[256]; int i; @@ -36,23 +71,23 @@ int main(int argc, char **argv) SILC_LOG_DEBUG(("Registering builtin hash functions")); silc_cipher_register_default(); - if (!silc_cipher_is_supported("aes-128-cbc")) { - SILC_LOG_DEBUG(("aes-128-cbc is not supported")); - goto err; - } - SILC_LOG_DEBUG(("Allocating AES-CBC cipher")); if (!silc_cipher_alloc("aes-128-cbc", &cipher)) { SILC_LOG_DEBUG(("Allocating AES-CBC cipher failed")); goto err; } + if (!silc_cipher_alloc("aes-128-cbc", &cipher2)) { + SILC_LOG_DEBUG(("Allocating AES-CBC cipher failed")); + goto err; + } /* First test vector */ SILC_LOG_DEBUG(("First test vector")); memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv1); - assert(silc_cipher_set_key(cipher, key1, key1_len)); + assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE)); + assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE)); assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), @@ -66,8 +101,8 @@ int main(int argc, char **argv) goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); - silc_cipher_set_iv(cipher, iv1); - assert(silc_cipher_decrypt(cipher, dst, pdst, p1_len, NULL)); + silc_cipher_set_iv(cipher2, iv1); + assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len); if (memcmp(pdst, p1, p1_len)) { @@ -82,7 +117,8 @@ int main(int argc, char **argv) memset(dst, 0, sizeof(dst)); memset(pdst, 0, sizeof(pdst)); silc_cipher_set_iv(cipher, iv2); - assert(silc_cipher_set_key(cipher, key2, key2_len)); + assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE)); + assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE)); assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL)); SILC_LOG_DEBUG(("block len %d, key len %d, name %s", silc_cipher_get_block_len(cipher), @@ -96,8 +132,8 @@ int main(int argc, char **argv) goto err; } SILC_LOG_DEBUG(("Encrypt is successful")); - silc_cipher_set_iv(cipher, iv2); - assert(silc_cipher_decrypt(cipher, dst, pdst, p2_len, NULL)); + silc_cipher_set_iv(cipher2, iv2); + assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL)); SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len); SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len); if (memcmp(pdst, p2, p2_len)) { @@ -105,6 +141,157 @@ int main(int argc, char **argv) goto err; } SILC_LOG_DEBUG(("Decrypt is successful")); + silc_cipher_free(cipher); + silc_cipher_free(cipher2); + + + SILC_LOG_DEBUG(("Allocating aes-128-ctr cipher")); + if (!silc_cipher_alloc("aes-128-ctr", &cipher)) { + SILC_LOG_DEBUG(("Allocating aes-128-ctr cipher failed")); + goto err; + } + + /* Third test vector */ + SILC_LOG_DEBUG(("Third test vector")); + memset(dst, 0, sizeof(dst)); + memset(pdst, 0, sizeof(pdst)); + silc_cipher_set_iv(cipher, iv3); + assert(silc_cipher_set_key(cipher, key3, key3_len, TRUE)); + assert(silc_cipher_encrypt(cipher, p3, dst, p3_len, NULL)); + SILC_LOG_DEBUG(("block len %d, key len %d, name %s", + silc_cipher_get_block_len(cipher), + silc_cipher_get_key_len(cipher), + silc_cipher_get_name(cipher))); + SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p3, p3_len); + SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p3_len); + SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c3, p3_len); + if (memcmp(dst, c3, p3_len)) { + SILC_LOG_DEBUG(("Encrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Encrypt is successful")); + silc_cipher_set_iv(cipher, iv3); + assert(silc_cipher_decrypt(cipher, dst, pdst, p3_len, NULL)); + SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p3_len); + SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p3, p3_len); + if (memcmp(pdst, p3, p3_len)) { + SILC_LOG_DEBUG(("Decrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Decrypt is successful")); + + + /* Fourth test vector */ + SILC_LOG_DEBUG(("Fourth test vector")); + memset(dst, 0, sizeof(dst)); + memset(pdst, 0, sizeof(pdst)); + silc_cipher_set_iv(cipher, iv4); + assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE)); + assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL)); + SILC_LOG_DEBUG(("block len %d, key len %d, name %s", + silc_cipher_get_block_len(cipher), + silc_cipher_get_key_len(cipher), + silc_cipher_get_name(cipher))); + SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len); + SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len); + SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len); + if (memcmp(dst, c4, p4_len)) { + SILC_LOG_DEBUG(("Encrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Encrypt is successful")); + silc_cipher_set_iv(cipher, iv4); + assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL)); + SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len); + SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len); + if (memcmp(pdst, p4, p4_len)) { + SILC_LOG_DEBUG(("Decrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Decrypt is successful")); + silc_cipher_free(cipher); + + SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher")); + if (!silc_cipher_alloc("aes-256-ctr", &cipher)) { + SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher failed")); + goto err; + } + if (!silc_cipher_alloc("aes-256-ctr", &cipher2)) { + SILC_LOG_DEBUG(("Allocating aes-256-ctr cipher failed")); + goto err; + } + + /* Fifth test vector */ + SILC_LOG_DEBUG(("Fifth test vector")); + memset(dst, 0, sizeof(dst)); + memset(pdst, 0, sizeof(pdst)); + silc_cipher_set_iv(cipher, iv5); + assert(silc_cipher_set_key(cipher, key5, key5_len, TRUE)); + assert(silc_cipher_set_key(cipher2, key5, key5_len, FALSE)); + assert(silc_cipher_encrypt(cipher, p5, dst, p5_len, NULL)); + SILC_LOG_DEBUG(("block len %d, key len %d, name %s", + silc_cipher_get_block_len(cipher), + silc_cipher_get_key_len(cipher), + silc_cipher_get_name(cipher))); + SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p5, p5_len); + SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p5_len); + SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c5, p5_len); + if (memcmp(dst, c5, p5_len)) { + SILC_LOG_DEBUG(("Encrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Encrypt is successful")); + silc_cipher_set_iv(cipher2, iv5); + assert(silc_cipher_decrypt(cipher2, dst, pdst, p5_len, NULL)); + SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p5_len); + SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p5, p5_len); + if (memcmp(pdst, p5, p5_len)) { + SILC_LOG_DEBUG(("Decrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Decrypt is successful")); + silc_cipher_free(cipher2); + + + SILC_LOG_DEBUG(("Allocating aes-256-cfb cipher")); + if (!silc_cipher_alloc("aes-256-cfb", &cipher)) { + SILC_LOG_DEBUG(("Allocating aes-256-cfb cipher failed")); + goto err; + } + if (!silc_cipher_alloc("aes-256-cfb", &cipher2)) { + SILC_LOG_DEBUG(("Allocating aes-256-cfb cipher failed")); + goto err; + } + + SILC_LOG_DEBUG(("CFB test vector")); + memset(dst, 0, sizeof(dst)); + memset(pdst, 0, sizeof(pdst)); + silc_cipher_set_iv(cipher, iv6); + assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE)); + assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE)); + assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL)); + SILC_LOG_DEBUG(("block len %d, key len %d, name %s", + silc_cipher_get_block_len(cipher), + silc_cipher_get_key_len(cipher), + silc_cipher_get_name(cipher))); + SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len); + SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len); + SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len); + if (memcmp(dst, c6, p6_len)) { + SILC_LOG_DEBUG(("Encrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Encrypt is successful")); + silc_cipher_set_iv(cipher2, iv6); + assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL)); + SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len); + SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len); + if (memcmp(pdst, p6, p6_len)) { + SILC_LOG_DEBUG(("Decrypt failed")); + goto err; + } + SILC_LOG_DEBUG(("Decrypt is successful")); + silc_cipher_free(cipher2); success = TRUE;