1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 /* test CFB/OFB/CBC modes */
4 #include <tomcrypt_test.h>
9 const char *key
, *iv
, *pt
, *ct
;
10 } cfb_testvectors
[] = {
13 "2b7e151628aed2a6abf7158809cf4f3c",
14 "000102030405060708090a0b0c0d0e0f",
20 "2b7e151628aed2a6abf7158809cf4f3c",
21 "000102030405060708090a0b0c0d0e0f",
22 "6bc1b3e22e409f96e93d7e117393172aae2d",
23 "3b79424c9c0dd436bace9e0ed4586a4f32b9",
39 unsigned char tmp2
[64];
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];
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");
61 DO(ret
= f8_test_mode());
70 /* encode the block */
71 DO(ret
= cbc_start(cipher_idx
, iv
, key
, 16, 0, &cbc
));
73 DO(ret
= cbc_getiv(iv2
, &l
, &cbc
));
74 if (l
!= 16 || memcmp(iv2
, iv
, 16)) {
75 fprintf(stderr
, "cbc_getiv failed");
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");
92 /* encode the block */
93 DO(ret
= cfb_start(cipher_idx
, iv
, key
, 16, 0, &cfb
));
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 */
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);
106 XMEMSET(&cfb
, 0, sizeof(cfb
));
107 #define b16(e, w) do { \
109 DO(base16_decode(e . w, XSTRLEN(e . w), w, &l)); \
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
));
119 DO(cfb_getiv(iv2
, &l
, &cfb
));
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
);
131 /* encode the block */
132 DO(ret
= ofb_start(cipher_idx
, iv
, key
, 16, 0, &ofb
));
134 DO(ret
= ofb_getiv(iv2
, &l
, &ofb
));
135 if (l
!= 16 || memcmp(iv2
, iv
, 16)) {
136 fprintf(stderr
, "ofb_getiv failed");
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");
151 #if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL)
152 DO(ret
= ctr_test());
156 DO(ret
= xts_test());