Merge pull request #678 from libtom/some-improvements
[libtomcrypt.git] / tests / modes_test.c
blob67b728f58cb453b5e632bbaf5410d24b7c57f395
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 /* test CFB/OFB/CBC modes */
4 #include <tomcrypt_test.h>
6 #ifdef LTC_CFB_MODE
7 static const struct {
8 int width;
9 const char *key, *iv, *pt, *ct;
10 } cfb_testvectors[] = {
13 "2b7e151628aed2a6abf7158809cf4f3c",
14 "000102030405060708090a0b0c0d0e0f",
15 "6bc1",
16 "68b3",
20 "2b7e151628aed2a6abf7158809cf4f3c",
21 "000102030405060708090a0b0c0d0e0f",
22 "6bc1b3e22e409f96e93d7e117393172aae2d",
23 "3b79424c9c0dd436bace9e0ed4586a4f32b9",
26 #endif
28 int modes_test(void)
30 int ret = CRYPT_NOP;
31 #ifdef LTC_CBC_MODE
32 symmetric_CBC cbc;
33 #endif
34 #ifdef LTC_OFB_MODE
35 symmetric_OFB ofb;
36 #endif
37 #ifdef LTC_CFB_MODE
38 symmetric_CFB cfb;
39 unsigned char tmp2[64];
40 unsigned long n;
41 #endif
42 #if defined(LTC_CBC_MODE) || defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE)
43 unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16];
44 int cipher_idx;
45 unsigned long l;
47 /* make a random pt, key and iv */
48 ENSURE(yarrow_read(pt, 64, &yarrow_prng) == 64);
49 ENSURE(yarrow_read(key, 16, &yarrow_prng) == 16);
50 ENSURE(yarrow_read(iv, 16, &yarrow_prng) == 16);
52 /* get idx of AES handy */
53 cipher_idx = find_cipher("aes");
54 if (cipher_idx == -1) {
55 fprintf(stderr, "test requires AES");
56 return 1;
58 #endif
60 #ifdef LTC_F8_MODE
61 DO(ret = f8_test_mode());
62 #endif
64 #ifdef LTC_LRW_MODE
65 DO(ret = lrw_test());
66 #endif
68 #ifdef LTC_CBC_MODE
69 /* test CBC mode */
70 /* encode the block */
71 DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
72 l = sizeof(iv2);
73 DO(ret = cbc_getiv(iv2, &l, &cbc));
74 if (l != 16 || memcmp(iv2, iv, 16)) {
75 fprintf(stderr, "cbc_getiv failed");
76 return 1;
78 DO(ret = cbc_encrypt(pt, ct, 64, &cbc));
80 /* decode the block */
81 DO(ret = cbc_setiv(iv2, l, &cbc));
82 zeromem(tmp, sizeof(tmp));
83 DO(ret = cbc_decrypt(ct, tmp, 64, &cbc));
84 if (memcmp(tmp, pt, 64) != 0) {
85 fprintf(stderr, "CBC failed");
86 return 1;
88 #endif
90 #ifdef LTC_CFB_MODE
91 /* test CFB mode */
92 /* encode the block */
93 DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
94 l = sizeof(iv2);
95 DO(ret = cfb_getiv(iv2, &l, &cfb));
96 /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
97 ENSURE(l == 16);
98 DO(ret = cfb_encrypt(pt, ct, 64, &cfb));
100 /* decode the block */
101 DO(ret = cfb_setiv(iv, l, &cfb));
102 zeromem(tmp, sizeof(tmp));
103 DO(ret = cfb_decrypt(ct, tmp, 64, &cfb));
104 COMPARE_TESTVECTOR(tmp, 64, pt, 64, "cfb128-enc-dec", 0);
105 cfb_done(&cfb);
106 XMEMSET(&cfb, 0, sizeof(cfb));
107 #define b16(e, w) do { \
108 l = sizeof(w); \
109 DO(base16_decode(e . w, XSTRLEN(e . w), w, &l)); \
110 } while(0)
111 for (n = 0; n < sizeof(cfb_testvectors)/sizeof(cfb_testvectors[0]); ++n) {
112 b16(cfb_testvectors[n], key);
113 b16(cfb_testvectors[n], iv);
114 b16(cfb_testvectors[n], pt);
115 b16(cfb_testvectors[n], ct);
117 DO(cfb_start_ex(cipher_idx, iv, key, 16, 0, cfb_testvectors[n].width, &cfb));
118 l = sizeof(iv2);
119 DO(cfb_getiv(iv2, &l, &cfb));
120 ENSURE(l == 16);
121 DO(ret = cfb_encrypt(pt, tmp, 2, &cfb));
122 COMPARE_TESTVECTOR(tmp, 2, ct, 2, "cfb-enc", n);
123 DO(cfb_setiv(iv2, l, &cfb));
124 DO(ret = cfb_decrypt(tmp, tmp2, 2, &cfb));
125 COMPARE_TESTVECTOR(tmp2, 2, pt, 2, "cfb-dec", n);
127 #endif
129 #ifdef LTC_OFB_MODE
130 /* test OFB mode */
131 /* encode the block */
132 DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
133 l = sizeof(iv2);
134 DO(ret = ofb_getiv(iv2, &l, &ofb));
135 if (l != 16 || memcmp(iv2, iv, 16)) {
136 fprintf(stderr, "ofb_getiv failed");
137 return 1;
139 DO(ret = ofb_encrypt(pt, ct, 64, &ofb));
141 /* decode the block */
142 DO(ret = ofb_setiv(iv2, l, &ofb));
143 zeromem(tmp, sizeof(tmp));
144 DO(ret = ofb_decrypt(ct, tmp, 64, &ofb));
145 if (memcmp(tmp, pt, 64) != 0) {
146 fprintf(stderr, "OFB failed");
147 return 1;
149 #endif
151 #if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL)
152 DO(ret = ctr_test());
153 #endif
155 #ifdef LTC_XTS_MODE
156 DO(ret = xts_test());
157 #endif
159 return 0;