Browse Source

add inlen parameter to base16_decode()

Steffen Jaeckel 7 years ago
parent
commit
73e5330c47
3 changed files with 9 additions and 8 deletions
  1. 1 1
      src/headers/tomcrypt_misc.h
  2. 4 5
      src/misc/base16/base16_decode.c
  3. 4 2
      tests/base16_test.c

+ 1 - 1
src/headers/tomcrypt_misc.h

@@ -51,7 +51,7 @@ int base32_decode(const          char *in,  unsigned long inlen,
 int base16_encode(const unsigned char *in,  unsigned long  inlen,
 int base16_encode(const unsigned char *in,  unsigned long  inlen,
                                  char *out, unsigned long *outlen,
                                  char *out, unsigned long *outlen,
                                  int  caps);
                                  int  caps);
-int base16_decode(const          char *in,
+int base16_decode(const          char *in,  unsigned long  inlen,
                         unsigned char *out, unsigned long *outlen);
                         unsigned char *out, unsigned long *outlen);
 #endif
 #endif
 
 

+ 4 - 5
src/misc/base16/base16_decode.c

@@ -25,10 +25,10 @@
    @param outlen   [in/out] The max size and resulting size of the decoded data
    @param outlen   [in/out] The max size and resulting size of the decoded data
    @return CRYPT_OK if successful
    @return CRYPT_OK if successful
 */
 */
-int base16_decode(const          char *in,
+int base16_decode(const          char *in,  unsigned long  inlen,
                         unsigned char *out, unsigned long *outlen)
                         unsigned char *out, unsigned long *outlen)
 {
 {
-   unsigned long pos, in_len, out_len;
+   unsigned long pos, out_len;
    unsigned char idx0;
    unsigned char idx0;
    unsigned char idx1;
    unsigned char idx1;
 
 
@@ -46,10 +46,9 @@ int base16_decode(const          char *in,
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
    LTC_ARGCHK(outlen != NULL);
 
 
-   in_len = strlen(in);
-   if ((in_len % 2) == 1) return CRYPT_INVALID_PACKET;
+   if ((inlen % 2) == 1) return CRYPT_INVALID_PACKET;
    out_len = *outlen * 2;
    out_len = *outlen * 2;
-   for (pos = 0; ((pos + 1 < out_len) && (pos + 1 < in_len)); pos += 2) {
+   for (pos = 0; ((pos + 1 < out_len) && (pos + 1 < inlen)); pos += 2) {
       idx0 = (unsigned char) (in[pos + 0] & 0x1F) ^ 0x10;
       idx0 = (unsigned char) (in[pos + 0] & 0x1F) ^ 0x10;
       idx1 = (unsigned char) (in[pos + 1] & 0x1F) ^ 0x10;
       idx1 = (unsigned char) (in[pos + 1] & 0x1F) ^ 0x10;
       out[pos / 2] = (unsigned char) (hashmap[idx0] << 4) | hashmap[idx1];
       out[pos / 2] = (unsigned char) (hashmap[idx0] << 4) | hashmap[idx1];

+ 4 - 2
tests/base16_test.c

@@ -28,8 +28,9 @@ int base16_test(void)
          yarrow_read(in, x, &yarrow_prng);
          yarrow_read(in, x, &yarrow_prng);
          l1 = sizeof(out);
          l1 = sizeof(out);
          DO(base16_encode(in, x, out, &l1, idx));
          DO(base16_encode(in, x, out, &l1, idx));
+         l1--;
          l2 = sizeof(tmp);
          l2 = sizeof(tmp);
-         DO(base16_decode(out, tmp, &l2));
+         DO(base16_decode(out, l1, tmp, &l2));
          DO(do_compare_testvector(tmp, l2, in, x, "random base16", idx * 100 + x));
          DO(do_compare_testvector(tmp, l2, in, x, "random base16", idx * 100 + x));
       }
       }
    }
    }
@@ -38,8 +39,9 @@ int base16_test(void)
       l1 = sizeof(out);
       l1 = sizeof(out);
       DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
       DO(base16_encode(testin, sizeof(testin), out, &l1, idx));
       DO(do_compare_testvector(out, strlen(out), testout[idx], strlen(testout[idx]), "testout base16", idx));
       DO(do_compare_testvector(out, strlen(out), testout[idx], strlen(testout[idx]), "testout base16", idx));
+      l1--;
       l2 = sizeof(tmp);
       l2 = sizeof(tmp);
-      DO(base16_decode(out, tmp, &l2));
+      DO(base16_decode(out, l1, tmp, &l2));
       DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base16", idx));
       DO(do_compare_testvector(tmp, l2, testin, sizeof(testin), "testin base16", idx));
    }
    }