1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file ctr_test.c
14 CTR implementation, Tests again RFC 3686, Tom St Denis
15 */
16
17 #ifdef LTC_CTR_MODE
18
ctr_test(void)19 int ctr_test(void)
20 {
21 #ifdef LTC_NO_TEST
22 return CRYPT_NOP;
23 #else
24 static const struct {
25 int keylen, msglen;
26 unsigned char key[32], IV[16], pt[64], ct[64];
27 } tests[] = {
28 /* 128-bit key, 16-byte pt */
29 {
30 16, 16,
31 {0xAE,0x68,0x52,0xF8,0x12,0x10,0x67,0xCC,0x4B,0xF7,0xA5,0x76,0x55,0x77,0xF3,0x9E },
32 {0x00,0x00,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
33 {0x53,0x69,0x6E,0x67,0x6C,0x65,0x20,0x62,0x6C,0x6F,0x63,0x6B,0x20,0x6D,0x73,0x67 },
34 {0xE4,0x09,0x5D,0x4F,0xB7,0xA7,0xB3,0x79,0x2D,0x61,0x75,0xA3,0x26,0x13,0x11,0xB8 },
35 },
36
37 /* 128-bit key, 36-byte pt */
38 {
39 16, 36,
40 {0x76,0x91,0xBE,0x03,0x5E,0x50,0x20,0xA8,0xAC,0x6E,0x61,0x85,0x29,0xF9,0xA0,0xDC },
41 {0x00,0xE0,0x01,0x7B,0x27,0x77,0x7F,0x3F,0x4A,0x17,0x86,0xF0,0x00,0x00,0x00,0x00 },
42 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
43 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
44 0x20,0x21,0x22,0x23},
45 {0xC1,0xCF,0x48,0xA8,0x9F,0x2F,0xFD,0xD9,0xCF,0x46,0x52,0xE9,0xEF,0xDB,0x72,0xD7,
46 0x45,0x40,0xA4,0x2B,0xDE,0x6D,0x78,0x36,0xD5,0x9A,0x5C,0xEA,0xAE,0xF3,0x10,0x53,
47 0x25,0xB2,0x07,0x2F },
48 },
49 };
50 int idx, err, x;
51 unsigned char buf[64];
52 symmetric_CTR ctr;
53
54 /* AES can be under rijndael or aes... try to find it */
55 if ((idx = find_cipher("aes")) == -1) {
56 if ((idx = find_cipher("rijndael")) == -1) {
57 return CRYPT_NOP;
58 }
59 }
60
61 for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
62 if ((err = ctr_start(idx, tests[x].IV, tests[x].key, tests[x].keylen, 0, CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686, &ctr)) != CRYPT_OK) {
63 return err;
64 }
65 if ((err = ctr_encrypt(tests[x].pt, buf, tests[x].msglen, &ctr)) != CRYPT_OK) {
66 return err;
67 }
68 ctr_done(&ctr);
69 if (compare_testvector(buf, tests[x].msglen, tests[x].ct, tests[x].msglen, "CTR", x)) {
70 return CRYPT_FAIL_TESTVECTOR;
71 }
72 }
73 return CRYPT_OK;
74 #endif
75 }
76
77 #endif
78
79 /* ref: $Format:%D$ */
80 /* git commit: $Format:%H$ */
81 /* commit time: $Format:%ai$ */
82
83
84
85