Skip to content

Commit dda1c7e

Browse files
committed
Re-factor modes_test().
... also funny that no static analyzer complained about `ret` being assigned, but never read ... Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
1 parent 6daff83 commit dda1c7e

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

tests/modes_test.c

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ static const struct {
2727

2828
int modes_test(void)
2929
{
30-
int ret = CRYPT_NOP;
3130
#ifdef LTC_CBC_MODE
3231
symmetric_CBC cbc;
3332
#endif
@@ -50,57 +49,47 @@ int modes_test(void)
5049
ENSURE(yarrow_read(iv, 16, &yarrow_prng) == 16);
5150

5251
/* 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;
57-
}
52+
ENSURE((cipher_idx = find_cipher("aes")) != -1);
5853
#endif
5954

6055
#ifdef LTC_F8_MODE
61-
DO(ret = f8_test_mode());
56+
DO(f8_test_mode());
6257
#endif
6358

6459
#ifdef LTC_LRW_MODE
65-
DO(ret = lrw_test());
60+
DO(lrw_test());
6661
#endif
6762

6863
#ifdef LTC_CBC_MODE
6964
/* test CBC mode */
7065
/* encode the block */
71-
DO(ret = cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
66+
DO(cbc_start(cipher_idx, iv, key, 16, 0, &cbc));
7267
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;
77-
}
78-
DO(ret = cbc_encrypt(pt, ct, 64, &cbc));
68+
DO(cbc_getiv(iv2, &l, &cbc));
69+
COMPARE_TESTVECTOR(iv2, l, iv, 16, "cbc_getiv", 0);
70+
DO(cbc_encrypt(pt, ct, 64, &cbc));
7971

8072
/* decode the block */
81-
DO(ret = cbc_setiv(iv2, l, &cbc));
73+
DO(cbc_setiv(iv2, l, &cbc));
8274
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;
87-
}
75+
DO(cbc_decrypt(ct, tmp, 64, &cbc));
76+
COMPARE_TESTVECTOR(pt, 64, tmp, 64, "CBC", 0);
8877
#endif
8978

9079
#ifdef LTC_CFB_MODE
9180
/* test CFB mode */
9281
/* encode the block */
93-
DO(ret = cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
82+
DO(cfb_start(cipher_idx, iv, key, 16, 0, &cfb));
9483
l = sizeof(iv2);
95-
DO(ret = cfb_getiv(iv2, &l, &cfb));
84+
DO(cfb_getiv(iv2, &l, &cfb));
9685
/* note we don't memcmp iv2/iv since cfb_start processes the IV for the first block */
9786
ENSURE(l == 16);
98-
DO(ret = cfb_encrypt(pt, ct, 64, &cfb));
87+
DO(cfb_encrypt(pt, ct, 64, &cfb));
9988

10089
/* decode the block */
101-
DO(ret = cfb_setiv(iv, l, &cfb));
90+
DO(cfb_setiv(iv, l, &cfb));
10291
zeromem(tmp, sizeof(tmp));
103-
DO(ret = cfb_decrypt(ct, tmp, 64, &cfb));
92+
DO(cfb_decrypt(ct, tmp, 64, &cfb));
10493
COMPARE_TESTVECTOR(tmp, 64, pt, 64, "cfb128-enc-dec", 0);
10594
cfb_done(&cfb);
10695
XMEMSET(&cfb, 0, sizeof(cfb));
@@ -118,36 +107,36 @@ int modes_test(void)
118107
l = sizeof(iv2);
119108
DO(cfb_getiv(iv2, &l, &cfb));
120109
ENSURE(l == 16);
121-
DO(ret = cfb_encrypt(pt, tmp, 2, &cfb));
110+
DO(cfb_encrypt(pt, tmp, 2, &cfb));
122111
COMPARE_TESTVECTOR(tmp, 2, ct, 2, "cfb-enc", n);
123112
DO(cfb_setiv(iv2, l, &cfb));
124-
DO(ret = cfb_decrypt(tmp, tmp2, 2, &cfb));
113+
DO(cfb_decrypt(tmp, tmp2, 2, &cfb));
125114
COMPARE_TESTVECTOR(tmp2, 2, pt, 2, "cfb-dec", n);
126115
}
127116
#endif
128117

129118
#ifdef LTC_OFB_MODE
130119
/* test OFB mode */
131120
/* encode the block */
132-
DO(ret = ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
121+
DO(ofb_start(cipher_idx, iv, key, 16, 0, &ofb));
133122
l = sizeof(iv2);
134123
DO(ofb_getiv(iv2, &l, &ofb));
135124
COMPARE_TESTVECTOR(iv2, l, iv, 16, "ofb_getiv", 0);
136-
DO(ret = ofb_encrypt(pt, ct, 64, &ofb));
125+
DO(ofb_encrypt(pt, ct, 64, &ofb));
137126

138127
/* decode the block */
139-
DO(ret = ofb_setiv(iv2, l, &ofb));
128+
DO(ofb_setiv(iv2, l, &ofb));
140129
zeromem(tmp, sizeof(tmp));
141-
DO(ret = ofb_decrypt(ct, tmp, 64, &ofb));
130+
DO(ofb_decrypt(ct, tmp, 64, &ofb));
142131
COMPARE_TESTVECTOR(tmp, 64, pt, 64, "OFB", 0);
143132
#endif
144133

145134
#if defined(LTC_CTR_MODE) && defined(LTC_RIJNDAEL)
146-
DO(ret = ctr_test());
135+
DO(ctr_test());
147136
#endif
148137

149138
#ifdef LTC_XTS_MODE
150-
DO(ret = xts_test());
139+
DO(xts_test());
151140
#endif
152141

153142
return 0;

0 commit comments

Comments
 (0)