Skip to content

Commit 2c51ae8

Browse files
committed
make sure to check yarrow_read() return values
Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 69843a4 commit 2c51ae8

File tree

8 files changed

+28
-46
lines changed

8 files changed

+28
-46
lines changed

tests/base16_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int base16_test(void)
2020

2121
for (idx = 0; idx < 2; idx++) {
2222
for (x = 0; x < 100; x++) {
23-
yarrow_read(in, x, &yarrow_prng);
23+
ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
2424
l1 = sizeof(out);
2525
DO(base16_encode(in, x, out, &l1, idx));
2626
l2 = sizeof(tmp);

tests/base32_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int base32_test(void)
2727

2828
for (idx = 0; idx < 4; idx++) {
2929
for (x = 0; x < 100; x++) {
30-
yarrow_read(in, x, &yarrow_prng);
30+
ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
3131
l1 = sizeof(out);
3232
DO(base32_encode(in, x, out, &l1, testid[idx]));
3333
l2 = sizeof(tmp);

tests/base64_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int base64_test(void)
130130
}
131131

132132
for (x = 0; x < 64; x++) {
133-
yarrow_read(in, x, &yarrow_prng);
133+
ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
134134
l1 = sizeof(out);
135135
DO(base64_encode(in, x, out, &l1));
136136
l2 = sizeof(tmp);

tests/der_test.c

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,10 +1700,7 @@ int der_test(void)
17001700
#else
17011701
for (z = 0; z < 1024; z++) {
17021702
#endif
1703-
if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
1704-
fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, z);
1705-
return 1;
1706-
}
1703+
ENSURE(yarrow_read(buf[0], z, &yarrow_prng) == z);
17071704
DO(mp_read_unsigned_bin(a, buf[0], z));
17081705
/* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
17091706
x = sizeof(buf[0]);
@@ -1723,10 +1720,7 @@ int der_test(void)
17231720
/* test short integer */
17241721
for (zz = 0; zz < 256; zz++) {
17251722
for (z = 1; z < 4; z++) {
1726-
if (yarrow_read(buf[2], z, &yarrow_prng) != z) {
1727-
fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, z);
1728-
return 1;
1729-
}
1723+
ENSURE(yarrow_read(buf[2], z, &yarrow_prng) == z);
17301724
/* encode with normal */
17311725
DO(mp_read_unsigned_bin(a, buf[2], z));
17321726

@@ -1763,10 +1757,7 @@ int der_test(void)
17631757

17641758
/* Test bit string */
17651759
for (zz = 1; zz < 1536; zz++) {
1766-
if (yarrow_read(buf[0], zz, &yarrow_prng) != zz) {
1767-
fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, zz);
1768-
return 1;
1769-
}
1760+
ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
17701761
for (z = 0; z < zz; z++) {
17711762
buf[0][z] &= 0x01;
17721763
}
@@ -1788,10 +1779,7 @@ int der_test(void)
17881779

17891780
/* Test octet string */
17901781
for (zz = 1; zz < 1536; zz++) {
1791-
if (yarrow_read(buf[0], zz, &yarrow_prng) != zz) {
1792-
fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, zz);
1793-
return 1;
1794-
}
1782+
ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
17951783
x = sizeof(buf[1]);
17961784
DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
17971785
DO(der_length_octet_string(zz, &y));
@@ -1829,10 +1817,7 @@ int der_test(void)
18291817
/* do random strings */
18301818
for (zz = 0; zz < 5000; zz++) {
18311819
/* pick a random number of words */
1832-
if (yarrow_read(buf[0], 4, &yarrow_prng) != 4) {
1833-
fprintf(stderr, "%d: Failed to read %d bytes from yarrow\n", __LINE__, 4);
1834-
return 1;
1835-
}
1820+
ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
18361821
LOAD32L(z, buf[0]);
18371822
z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
18381823

@@ -1841,10 +1826,7 @@ int der_test(void)
18411826
oid[0][1] = buf[0][1] % 40;
18421827

18431828
for (y = 2; y < z; y++) {
1844-
if (yarrow_read(buf[0], 4, &yarrow_prng) != 4) {
1845-
fprintf(stderr, "%d: Failed to read %d bytes from yarrow\n", __LINE__, 4);
1846-
return 1;
1847-
}
1829+
ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
18481830
LOAD32L(oid[0][y], buf[0]);
18491831
}
18501832

tests/ecc_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ static int s_ecc_test_shamir(void)
154154
/* do 100 random tests */
155155
for (y = 0; y < 100; y++) {
156156
/* pick a random r1, r2 */
157-
LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
157+
ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
158158
DO(mp_read_unsigned_bin(rA, buf, sizes[x]));
159-
LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
159+
ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
160160
DO(mp_read_unsigned_bin(rB, buf, sizes[x]));
161161

162162
/* compute rA * G = A */
@@ -166,9 +166,9 @@ static int s_ecc_test_shamir(void)
166166
DO(ltc_mp.ecc_ptmul(rB, G, B, a, modulus, 1));
167167

168168
/* pick a random kA, kB */
169-
LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
169+
ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
170170
DO(mp_read_unsigned_bin(kA, buf, sizes[x]));
171-
LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
171+
ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
172172
DO(mp_read_unsigned_bin(kB, buf, sizes[x]));
173173

174174
/* now, compute kA*A + kB*B = C1 using the older method */

tests/modes_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ int modes_test(void)
2121
unsigned long l;
2222

2323
/* make a random pt, key and iv */
24-
yarrow_read(pt, 64, &yarrow_prng);
25-
yarrow_read(key, 16, &yarrow_prng);
26-
yarrow_read(iv, 16, &yarrow_prng);
24+
ENSURE(yarrow_read(pt, 64, &yarrow_prng) == 64);
25+
ENSURE(yarrow_read(key, 16, &yarrow_prng) == 16);
26+
ENSURE(yarrow_read(iv, 16, &yarrow_prng) == 16);
2727

2828
/* get idx of AES handy */
2929
cipher_idx = find_cipher("aes");

tests/rsa_test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static int s_rsa_public_ubin_e(int prng_idx)
385385
* overflow.
386386
*/
387387
DO(rng_make_prng(elen * 8, prng_idx, &yarrow_prng, NULL));
388-
LTC_ARGCHK(yarrow_read(e, elen, &yarrow_prng) == elen);
388+
ENSURE(yarrow_read(e, elen, &yarrow_prng) == elen);
389389

390390
/* Ensure that public exponent is:
391391
* - odd value
@@ -503,7 +503,7 @@ print_hex("q", tmp, len);
503503
for (cnt = 0; cnt < 4; cnt++) {
504504
for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
505505
/* make a random key/msg */
506-
yarrow_read(in, rsa_msgsize, &yarrow_prng);
506+
ENSURE(yarrow_read(in, rsa_msgsize, &yarrow_prng) == rsa_msgsize);
507507

508508
len = sizeof(out);
509509
len2 = rsa_msgsize;
@@ -547,7 +547,7 @@ print_hex("q", tmp, len);
547547
len = sizeof(out);
548548
len2 = rsa_msgsize;
549549
/* make a random key/msg */
550-
yarrow_read(in, rsa_msgsize, &yarrow_prng);
550+
ENSURE(yarrow_read(in, rsa_msgsize, &yarrow_prng) == rsa_msgsize);
551551
DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
552552

553553
len2 = rsa_msgsize;

tests/store_test.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
int store_test(void)
77
{
88
unsigned char buf[256];
9-
int y;
9+
unsigned long y;
1010
ulong32 L, L1;
1111
ulong64 LL, LL1;
1212
#ifdef LTC_FAST
13-
int x, z;
13+
unsigned long x, z;
1414
#endif
1515

1616
for (y = 0; y < 4; y++) {
@@ -19,13 +19,13 @@ int store_test(void)
1919
STORE32L(L, buf + y);
2020
LOAD32L(L1, buf + y);
2121
if (L1 != L) {
22-
fprintf(stderr, "\n32L failed at offset %d\n", y);
22+
fprintf(stderr, "\n32L failed at offset %lu\n", y);
2323
return 1;
2424
}
2525
STORE32H(L, buf + y);
2626
LOAD32H(L1, buf + y);
2727
if (L1 != L) {
28-
fprintf(stderr, "\n32H failed at offset %d\n", y);
28+
fprintf(stderr, "\n32H failed at offset %lu\n", y);
2929
return 1;
3030
}
3131
}
@@ -36,13 +36,13 @@ int store_test(void)
3636
STORE64L(LL, buf + y);
3737
LOAD64L(LL1, buf + y);
3838
if (LL1 != LL) {
39-
fprintf(stderr, "\n64L failed at offset %d\n", y);
39+
fprintf(stderr, "\n64L failed at offset %lu\n", y);
4040
return 1;
4141
}
4242
STORE64H(LL, buf + y);
4343
LOAD64H(LL1, buf + y);
4444
if (LL1 != LL) {
45-
fprintf(stderr, "\n64H failed at offset %d\n", y);
45+
fprintf(stderr, "\n64H failed at offset %lu\n", y);
4646
return 1;
4747
}
4848
}
@@ -53,8 +53,8 @@ int store_test(void)
5353

5454
for (z = 0; z < y; z++) {
5555
/* fill y bytes with random */
56-
yarrow_read(buf+z, y, &yarrow_prng);
57-
yarrow_read(buf+z+y, y, &yarrow_prng);
56+
ENSURE(yarrow_read(buf+z, y, &yarrow_prng) == y);
57+
ENSURE(yarrow_read(buf+z+y, y, &yarrow_prng) == y);
5858

5959
/* now XOR it byte for byte */
6060
for (x = 0; x < y; x++) {
@@ -67,7 +67,7 @@ int store_test(void)
6767
}
6868

6969
if (memcmp(&buf[2*y+z], &buf[3*y+z], y)) {
70-
fprintf(stderr, "\nLTC_FAST failed at offset %d\n", z);
70+
fprintf(stderr, "\nLTC_FAST failed at offset %lu\n", z);
7171
return 1;
7272
}
7373
}

0 commit comments

Comments
 (0)