Browse Source

make sure to check `yarrow_read()` return values

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 3 years ago
parent
commit
2c51ae81ac
8 changed files with 28 additions and 46 deletions
  1. 1 1
      tests/base16_test.c
  2. 1 1
      tests/base32_test.c
  3. 1 1
      tests/base64_test.c
  4. 6 24
      tests/der_test.c
  5. 4 4
      tests/ecc_test.c
  6. 3 3
      tests/modes_test.c
  7. 3 3
      tests/rsa_test.c
  8. 9 9
      tests/store_test.c

+ 1 - 1
tests/base16_test.c

@@ -20,7 +20,7 @@ int base16_test(void)
 
    for (idx = 0; idx < 2; idx++) {
       for (x = 0; x < 100; x++) {
-         yarrow_read(in, x, &yarrow_prng);
+         ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
          l1 = sizeof(out);
          DO(base16_encode(in, x, out, &l1, idx));
          l2 = sizeof(tmp);

+ 1 - 1
tests/base32_test.c

@@ -27,7 +27,7 @@ int base32_test(void)
 
    for (idx = 0; idx < 4; idx++) {
       for (x = 0; x < 100; x++) {
-         yarrow_read(in, x, &yarrow_prng);
+         ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
          l1 = sizeof(out);
          DO(base32_encode(in, x, out, &l1, testid[idx]));
          l2 = sizeof(tmp);

+ 1 - 1
tests/base64_test.c

@@ -130,7 +130,7 @@ int base64_test(void)
    }
 
    for  (x = 0; x < 64; x++) {
-       yarrow_read(in, x, &yarrow_prng);
+       ENSURE(yarrow_read(in, x, &yarrow_prng) == x);
        l1 = sizeof(out);
        DO(base64_encode(in, x, out, &l1));
        l2 = sizeof(tmp);

+ 6 - 24
tests/der_test.c

@@ -1700,10 +1700,7 @@ int der_test(void)
 #else
       for (z = 0; z < 1024; z++) {
 #endif
-         if (yarrow_read(buf[0], z, &yarrow_prng) != z) {
-            fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, z);
-            return 1;
-         }
+         ENSURE(yarrow_read(buf[0], z, &yarrow_prng) == z);
          DO(mp_read_unsigned_bin(a, buf[0], z));
 /*          if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */
          x = sizeof(buf[0]);
@@ -1723,10 +1720,7 @@ int der_test(void)
 /* test short integer */
    for (zz = 0; zz < 256; zz++) {
       for (z = 1; z < 4; z++) {
-         if (yarrow_read(buf[2], z, &yarrow_prng) != z) {
-            fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, z);
-            return 1;
-         }
+         ENSURE(yarrow_read(buf[2], z, &yarrow_prng) == z);
          /* encode with normal */
          DO(mp_read_unsigned_bin(a, buf[2], z));
 
@@ -1763,10 +1757,7 @@ int der_test(void)
 
 /* Test bit string */
    for (zz = 1; zz < 1536; zz++) {
-       if (yarrow_read(buf[0], zz, &yarrow_prng) != zz) {
-          fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, zz);
-          return 1;
-       }
+       ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
        for (z = 0; z < zz; z++) {
            buf[0][z] &= 0x01;
        }
@@ -1788,10 +1779,7 @@ int der_test(void)
 
 /* Test octet string */
    for (zz = 1; zz < 1536; zz++) {
-       if (yarrow_read(buf[0], zz, &yarrow_prng) != zz) {
-          fprintf(stderr, "%d: Failed to read %lu bytes from yarrow\n", __LINE__, zz);
-          return 1;
-       }
+       ENSURE(yarrow_read(buf[0], zz, &yarrow_prng) == zz);
        x = sizeof(buf[1]);
        DO(der_encode_octet_string(buf[0], zz, buf[1], &x));
        DO(der_length_octet_string(zz, &y));
@@ -1829,10 +1817,7 @@ int der_test(void)
    /* do random strings */
    for (zz = 0; zz < 5000; zz++) {
        /* pick a random number of words */
-       if (yarrow_read(buf[0], 4, &yarrow_prng) != 4) {
-          fprintf(stderr, "%d: Failed to read %d bytes from yarrow\n", __LINE__, 4);
-          return 1;
-       }
+       ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
        LOAD32L(z, buf[0]);
        z = 2 + (z % ((sizeof(oid[0])/sizeof(oid[0][0])) - 2));
 
@@ -1841,10 +1826,7 @@ int der_test(void)
        oid[0][1] = buf[0][1] % 40;
 
        for (y = 2; y < z; y++) {
-          if (yarrow_read(buf[0], 4, &yarrow_prng) != 4) {
-             fprintf(stderr, "%d: Failed to read %d bytes from yarrow\n", __LINE__, 4);
-             return 1;
-          }
+          ENSURE(yarrow_read(buf[0], 4, &yarrow_prng) == 4);
           LOAD32L(oid[0][y], buf[0]);
        }
 

+ 4 - 4
tests/ecc_test.c

@@ -154,9 +154,9 @@ static int s_ecc_test_shamir(void)
        /* do 100 random tests */
        for (y = 0; y < 100; y++) {
           /* pick a random r1, r2 */
-          LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
+          ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
           DO(mp_read_unsigned_bin(rA, buf, sizes[x]));
-          LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
+          ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
           DO(mp_read_unsigned_bin(rB, buf, sizes[x]));
 
           /* compute rA * G = A */
@@ -166,9 +166,9 @@ static int s_ecc_test_shamir(void)
           DO(ltc_mp.ecc_ptmul(rB, G, B, a, modulus, 1));
 
           /* pick a random kA, kB */
-          LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
+          ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
           DO(mp_read_unsigned_bin(kA, buf, sizes[x]));
-          LTC_ARGCHK(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
+          ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]);
           DO(mp_read_unsigned_bin(kB, buf, sizes[x]));
 
           /* now, compute kA*A + kB*B = C1 using the older method */

+ 3 - 3
tests/modes_test.c

@@ -21,9 +21,9 @@ int modes_test(void)
    unsigned long l;
 
    /* make a random pt, key and iv */
-   yarrow_read(pt,  64, &yarrow_prng);
-   yarrow_read(key, 16, &yarrow_prng);
-   yarrow_read(iv,  16, &yarrow_prng);
+   ENSURE(yarrow_read(pt,  64, &yarrow_prng) == 64);
+   ENSURE(yarrow_read(key, 16, &yarrow_prng) == 16);
+   ENSURE(yarrow_read(iv,  16, &yarrow_prng) == 16);
 
    /* get idx of AES handy */
    cipher_idx = find_cipher("aes");

+ 3 - 3
tests/rsa_test.c

@@ -385,7 +385,7 @@ static int s_rsa_public_ubin_e(int prng_idx)
     * overflow.
     */
    DO(rng_make_prng(elen * 8, prng_idx, &yarrow_prng, NULL));
-   LTC_ARGCHK(yarrow_read(e, elen, &yarrow_prng) == elen);
+   ENSURE(yarrow_read(e, elen, &yarrow_prng) == elen);
 
    /* Ensure that public exponent is:
     *  - odd value
@@ -503,7 +503,7 @@ print_hex("q", tmp, len);
    for (cnt = 0; cnt < 4; cnt++) {
    for (rsa_msgsize = 1; rsa_msgsize <= 86; rsa_msgsize++) {
       /* make a random key/msg */
-      yarrow_read(in, rsa_msgsize, &yarrow_prng);
+      ENSURE(yarrow_read(in, rsa_msgsize, &yarrow_prng) == rsa_msgsize);
 
       len  = sizeof(out);
       len2 = rsa_msgsize;
@@ -547,7 +547,7 @@ print_hex("q", tmp, len);
       len  = sizeof(out);
       len2 = rsa_msgsize;
       /* make a random key/msg */
-      yarrow_read(in, rsa_msgsize, &yarrow_prng);
+      ENSURE(yarrow_read(in, rsa_msgsize, &yarrow_prng) == rsa_msgsize);
       DO(rsa_encrypt_key_ex(in, rsa_msgsize, out, &len, NULL, 0, &yarrow_prng, prng_idx, 0, LTC_PKCS_1_V1_5, &key));
 
       len2 = rsa_msgsize;

+ 9 - 9
tests/store_test.c

@@ -6,11 +6,11 @@
 int store_test(void)
 {
   unsigned char buf[256];
-  int y;
+  unsigned long y;
   ulong32 L, L1;
   ulong64 LL, LL1;
 #ifdef LTC_FAST
-  int x, z;
+  unsigned long x, z;
 #endif
 
   for (y = 0; y < 4; y++) {
@@ -19,13 +19,13 @@ int store_test(void)
       STORE32L(L, buf + y);
       LOAD32L(L1, buf + y);
       if (L1 != L) {
-         fprintf(stderr, "\n32L failed at offset %d\n", y);
+         fprintf(stderr, "\n32L failed at offset %lu\n", y);
          return 1;
       }
       STORE32H(L, buf + y);
       LOAD32H(L1, buf + y);
       if (L1 != L) {
-         fprintf(stderr, "\n32H failed at offset %d\n", y);
+         fprintf(stderr, "\n32H failed at offset %lu\n", y);
          return 1;
       }
   }
@@ -36,13 +36,13 @@ int store_test(void)
       STORE64L(LL, buf + y);
       LOAD64L(LL1, buf + y);
       if (LL1 != LL) {
-         fprintf(stderr, "\n64L failed at offset %d\n", y);
+         fprintf(stderr, "\n64L failed at offset %lu\n", y);
          return 1;
       }
       STORE64H(LL, buf + y);
       LOAD64H(LL1, buf + y);
       if (LL1 != LL) {
-         fprintf(stderr, "\n64H failed at offset %d\n", y);
+         fprintf(stderr, "\n64H failed at offset %lu\n", y);
          return 1;
       }
   }
@@ -53,8 +53,8 @@ int store_test(void)
 
   for (z = 0; z < y; z++) {
      /* fill y bytes with random */
-     yarrow_read(buf+z,   y, &yarrow_prng);
-     yarrow_read(buf+z+y, y, &yarrow_prng);
+     ENSURE(yarrow_read(buf+z,   y, &yarrow_prng) == y);
+     ENSURE(yarrow_read(buf+z+y, y, &yarrow_prng) == y);
 
      /* now XOR it byte for byte */
      for (x = 0; x < y; x++) {
@@ -67,7 +67,7 @@ int store_test(void)
      }
 
      if (memcmp(&buf[2*y+z], &buf[3*y+z], y)) {
-        fprintf(stderr, "\nLTC_FAST failed at offset %d\n", z);
+        fprintf(stderr, "\nLTC_FAST failed at offset %lu\n", z);
         return 1;
      }
   }