Mercurial > dropbear
comparison libtomcrypt/tests/modes_test.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 */ | |
9 /* test CFB/OFB/CBC modes */ | |
10 #include <tomcrypt_test.h> | |
11 | |
12 int modes_test(void) | |
13 { | |
14 int ret = CRYPT_NOP; | |
15 #ifdef LTC_CBC_MODE | |
16 symmetric_CBC cbc; | |
17 #endif | |
18 #ifdef LTC_CFB_MODE | |
19 symmetric_CFB cfb; | |
20 #endif | |
21 #ifdef LTC_OFB_MODE | |
22 symmetric_OFB ofb; | |
23 #endif | |
24 #if defined(LTC_CBC_MODE) || defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) | |
25 unsigned char pt[64], ct[64], tmp[64], key[16], iv[16], iv2[16]; | |
26 int cipher_idx; | |
27 unsigned long l; | |
28 | |
29 /* make a random pt, key and iv */ | |
30 yarrow_read(pt, 64, &yarrow_prng); | |
31 yarrow_read(key, 16, &yarrow_prng); | |
32 yarrow_read(iv, 16, &yarrow_prng); | |
33 | |
34 /* get idx of AES handy */ | |
35 cipher_idx = find_cipher("aes"); | |
36 if (cipher_idx == -1) { | |
37 fprintf(stderr, "test requires AES"); | |
38 return 1; | |
39 } | |
40 #endif | |
41 | |
42 #ifdef LTC_F8_MODE | |
43 DO(ret = f8_test_mode()); | |
44 #endif | |
45 | |
46 #ifdef LTC_LRW_MODE | |
47 DO(ret = lrw_test()); | |
48 #endif | |
49 | |
50 #ifdef LTC_CBC_MODE | |
51 /* test CBC mode */ | |
52 /* encode the block */ | |
53 DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc)); | |
54 l = sizeof(iv2); | |
55 DO(ret = cbc_getiv(iv2, &l, &cbc)); | |
56 if (l != 16 || memcmp(iv2, iv, 16)) { | |
57 fprintf(stderr, "cbc_getiv failed"); | |
58 return 1; | |
59 } | |
60 DO(ret = cbc_encrypt(pt, ct, 64, &cbc)); | |
61 | |
62 /* decode the block */ | |
63 DO(ret = cbc_setiv(iv2, l, &cbc)); | |
64 zeromem(tmp, sizeof(tmp)); | |
65 DO(ret = cbc_decrypt(ct, tmp, 64, &cbc)); | |
66 if (memcmp(tmp, pt, 64) != 0) { | |
67 fprintf(stderr, "CBC failed"); | |
68 return 1; | |
69 } | |
70 #endif | |
71 | |
72 #ifdef LTC_CFB_MODE | |
73 /* test CFB mode */ | |
74 /* encode the block */ | |
75 DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb)); | |
76 l = sizeof(iv2); | |
77 DO(ret = cfb_getiv(iv2, &l, &cfb)); | |
78 /* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */ | |
79 if (l != 16) { | |
80 fprintf(stderr, "cfb_getiv failed"); | |
81 return 1; | |
82 } | |
83 DO(ret = cfb_encrypt(pt, ct, 64, &cfb)); | |
84 | |
85 /* decode the block */ | |
86 DO(ret = cfb_setiv(iv, l, &cfb)); | |
87 zeromem(tmp, sizeof(tmp)); | |
88 DO(ret = cfb_decrypt(ct, tmp, 64, &cfb)); | |
89 if (memcmp(tmp, pt, 64) != 0) { | |
90 fprintf(stderr, "CFB failed"); | |
91 return 1; | |
92 } | |
93 #endif | |
94 | |
95 #ifdef LTC_OFB_MODE | |
96 /* test OFB mode */ | |
97 /* encode the block */ | |
98 DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb)); | |
99 l = sizeof(iv2); | |
100 DO(ret = ofb_getiv(iv2, &l, &ofb)); | |
101 if (l != 16 || memcmp(iv2, iv, 16)) { | |
102 fprintf(stderr, "ofb_getiv failed"); | |
103 return 1; | |
104 } | |
105 DO(ret = ofb_encrypt(pt, ct, 64, &ofb)); | |
106 | |
107 /* decode the block */ | |
108 DO(ret = ofb_setiv(iv2, l, &ofb)); | |
109 zeromem(tmp, sizeof(tmp)); | |
110 DO(ret = ofb_decrypt(ct, tmp, 64, &ofb)); | |
111 if (memcmp(tmp, pt, 64) != 0) { | |
112 fprintf(stderr, "OFB failed"); | |
113 return 1; | |
114 } | |
115 #endif | |
116 | |
117 #if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL) | |
118 DO(ret = ctr_test()); | |
119 #endif | |
120 | |
121 #ifdef LTC_XTS_MODE | |
122 DO(ret = xts_test()); | |
123 #endif | |
124 | |
125 return 0; | |
126 } | |
127 | |
128 /* ref: $Format:%D$ */ | |
129 /* git commit: $Format:%H$ */ | |
130 /* commit time: $Format:%ai$ */ |