Browse Source

Base32: alpha_id >> id

Karel Miko 8 years ago
parent
commit
854a145c92

+ 4 - 4
doc/crypt.tex

@@ -6371,7 +6371,7 @@ The library provides functions to encode and decode a Base32 coding scheme. The
 
 \begin{center}
 \begin{tabular}{|l|l|l|}
-     \hline \textbf{alpha\_id} & \textbf{Mapping} & \textbf{Name} \\
+     \hline \textbf{id} & \textbf{Mapping} & \textbf{Name} \\
      \hline BASE32\_RFC4648 & ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 & RFC-4648 \\
      \hline BASE32\_BASE32HEX & 0123456789ABCDEFGHIJKLMNOPQRSTUV & Base32hex \\
      \hline BASE32\_ZBASE32 & YBNDRFG8EJKMCPQXOT1UWISZA345H769 & ZBase32 \\
@@ -6388,10 +6388,10 @@ int base32_encode(const unsigned char *in,
                         unsigned long  len,
                         unsigned char *out,
                         unsigned long *outlen,
-                        unsigned int   alpha_id);
+                        base32_alphabet id);
 \end{verbatim}
 
-Where \textit{in} is the binary string, \textit{out} is where the ASCII output is placed and \textit{alpha\_id} is
+Where \textit{in} is the binary string, \textit{out} is where the ASCII output is placed and \textit{id} is
 \textit{BASE32\_RFC4648}, \textit{BASE32\_BASE32HEX}, \textit{BASE32\_ZBASE32} or \textit{BASE32\_CROCKFORD} according the table above.
 
 To decode a base32 string call:
@@ -6402,7 +6402,7 @@ int base32_decode(const unsigned char *in,
                         unsigned long  len,
                         unsigned char *out,
                         unsigned long *outlen,
-                        unsigned int   alpha_id);
+                        base32_alphabet id);
 \end{verbatim}
 
 \mysection{Primality Testing}

+ 2 - 2
src/headers/tomcrypt_misc.h

@@ -40,10 +40,10 @@ typedef enum {
 } base32_alphabet;
 int base32_encode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        base32_alphabet alpha_id);
+                        base32_alphabet id);
 int base32_decode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        base32_alphabet alpha_id);
+                        base32_alphabet id);
 #endif
 
 /* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */

+ 9 - 8
src/misc/base32/base32_decode.c

@@ -17,12 +17,12 @@
    @param inlen    The length of the Base32 data
    @param out      [out] The destination of the binary decoded data
    @param outlen   [in/out] The max size and resulting size of the decoded data
-   @param alpha_id Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
+   @param id       Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
    @return CRYPT_OK if successful
 */
 int base32_decode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        base32_alphabet alpha_id)
+                        base32_alphabet id)
 {
    unsigned long x;
    int y = 0;
@@ -30,25 +30,25 @@ int base32_decode(const unsigned char *in,  unsigned long inlen,
    unsigned char c;
    const unsigned char *map;
    const unsigned char tables[4][43] = {
-      {  /* alpha_id BASE32_RFC4648 : ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
+      {  /* id = BASE32_RFC4648 : ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
          99/*0*/,99/*1*/,26/*2*/,27/*3*/,28/*4*/,29/*5*/,30/*6*/,31/*7*/,99/*8*/,99/*9*/,
          99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
           0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, 7/*H*/, 8/*I*/, 9/*J*/,10/*K*/,11/*L*/,12/*M*/,
          13/*N*/,14/*O*/,15/*P*/,16/*Q*/,17/*R*/,18/*S*/,19/*T*/,20/*U*/,21/*V*/,22/*W*/,23/*X*/,24/*Y*/,25/*Z*/
       },
-      {  /* alpha_id BASE32_BASE32HEX : 0123456789ABCDEFGHIJKLMNOPQRSTUV */
+      {  /* id = BASE32_BASE32HEX : 0123456789ABCDEFGHIJKLMNOPQRSTUV */
            0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
           99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
           10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/,18/*I*/,19/*J*/,20/*K*/,21/*L*/,22/*M*/,
           23/*N*/,24/*O*/,25/*P*/,26/*Q*/,27/*R*/,28/*S*/,29/*T*/,30/*U*/,31/*V*/,99/*W*/,99/*X*/,99/*Y*/,99/*Z*/
       },
-      {  /* alpha_id BASE32_ZBASE32 : YBNDRFG8EJKMCPQXOT1UWISZA345H769 */
+      {  /* id = BASE32_ZBASE32 : YBNDRFG8EJKMCPQXOT1UWISZA345H769 */
          99/*0*/,18/*1*/,99/*2*/,25/*3*/,26/*4*/,27/*5*/,30/*6*/,29/*7*/, 7/*8*/,31/*9*/,
          99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
          24/*A*/, 1/*B*/,12/*C*/, 3/*D*/, 8/*E*/, 5/*F*/, 6/*G*/,28/*H*/,21/*I*/, 9/*J*/,10/*K*/,99/*L*/,11/*M*/,
           2/*N*/,16/*O*/,13/*P*/,14/*Q*/, 4/*R*/,22/*S*/,17/*T*/,19/*U*/,99/*V*/,20/*W*/,15/*X*/, 0/*Y*/,23/*Z*/
       },
-      {  /* alpha_id BASE32_CROCKFORD : 0123456789ABCDEFGHJKMNPQRSTVWXYZ + O=>0 + IL=>1 */
+      {  /* id = BASE32_CROCKFORD : 0123456789ABCDEFGHJKMNPQRSTVWXYZ + O=>0 + IL=>1 */
           0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
          99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
          10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/, 1/*I*/,18/*J*/,19/*K*/, 1/*L*/,20/*M*/,
@@ -59,7 +59,8 @@ int base32_decode(const unsigned char *in,  unsigned long inlen,
    LTC_ARGCHK(in     != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
-   LTC_ARGCHK(alpha_id < 4);
+   LTC_ARGCHK(id >= BASE32_RFC4648);
+   LTC_ARGCHK(id <= BASE32_CROCKFORD);
 
    /* ignore all trailing = */
    while (inlen > 0 && in[inlen-1] == '=') inlen--;
@@ -84,7 +85,7 @@ int base32_decode(const unsigned char *in,  unsigned long inlen,
       return CRYPT_INVALID_PACKET;
    }
 
-   map = tables[alpha_id];
+   map = tables[id];
    for (x = 0; x < inlen; x++) {
       c = in[x];
       /* convert to upper case */

+ 9 - 8
src/misc/base32/base32_encode.c

@@ -17,26 +17,27 @@
    @param inlen    The length of the input buffer
    @param out      [out] The destination of the Base32 encoded data
    @param outlen   [in/out] The max size and resulting size of the encoded data
-   @param alpha_id Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
+   @param id       Alphabet to use BASE32_RFC4648, BASE32_BASE32HEX, BASE32_ZBASE32 or BASE32_CROCKFORD
    @return CRYPT_OK if successful
 */
 int base32_encode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        base32_alphabet alpha_id)
+                        base32_alphabet id)
 {
    unsigned long i, x;
    unsigned char *codes;
    const char *alphabet[4] = {
-      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",     /* alpha_id BASE32_RFC4648   */
-      "0123456789ABCDEFGHIJKLMNOPQRSTUV",     /* alpha_id BASE32_BASE32HEX */
-      "ybndrfg8ejkmcpqxot1uwisza345h769",     /* alpha_id BASE32_ZBASE32   */
-      "0123456789ABCDEFGHJKMNPQRSTVWXYZ"      /* alpha_id BASE32_CROCKFORD */
+      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",     /* id = BASE32_RFC4648   */
+      "0123456789ABCDEFGHIJKLMNOPQRSTUV",     /* id = BASE32_BASE32HEX */
+      "ybndrfg8ejkmcpqxot1uwisza345h769",     /* id = BASE32_ZBASE32   */
+      "0123456789ABCDEFGHJKMNPQRSTVWXYZ"      /* id = BASE32_CROCKFORD */
    };
 
    LTC_ARGCHK(in     != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
-   LTC_ARGCHK(alpha_id < 4);
+   LTC_ARGCHK(id >= BASE32_RFC4648);
+   LTC_ARGCHK(id <= BASE32_CROCKFORD);
 
    /* no input, nothing to do */
    if (inlen == 0) {
@@ -52,7 +53,7 @@ int base32_encode(const unsigned char *in,  unsigned long inlen,
    }
    *outlen = x;
 
-   codes = (unsigned char*)alphabet[alpha_id];
+   codes = (unsigned char*)alphabet[id];
    x = 5 * (inlen / 5);
    for (i = 0; i < x; i += 5) {
       *out++ = codes[(in[0] >> 3) & 0x1F];

+ 10 - 4
tests/base32_test.c

@@ -15,6 +15,12 @@ int base32_test(void)
 {
    unsigned char in[100], out[160], tmp[100];
    unsigned char testin[] = { 0x61,0xc2,0xcb,0xbc,0x5e,0x6d,0x2a,0x7a,0x1a,0x19,0x1a,0xae,0xc9,0x02,0xd4,0xbf,0x7d };
+   const int testid[4] = {
+      BASE32_RFC4648,
+      BASE32_BASE32HEX,
+      BASE32_ZBASE32,
+      BASE32_CROCKFORD
+   };
    const char *testout[4] = {
       "MHBMXPC6NUVHUGQZDKXMSAWUX56Q",
       "C71CNF2UDKL7K6GP3ANCI0MKNTUG",
@@ -28,9 +34,9 @@ int base32_test(void)
       for (x = 0; x < 100; x++) {
          yarrow_read(in, x, &yarrow_prng);
          l1 = sizeof(out);
-         DO(base32_encode(in, x, out, &l1, idx));
+         DO(base32_encode(in, x, out, &l1, testid[idx]));
          l2 = sizeof(tmp);
-         DO(base32_decode(out, l1, tmp, &l2, idx));
+         DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
          if (compare_testvector(tmp, l2, in, x, "random base32", idx * 100 + x)) {
             return CRYPT_FAIL_TESTVECTOR;
          }
@@ -39,12 +45,12 @@ int base32_test(void)
 
    for (idx = 0; idx < 4; idx++) {
       l1 = sizeof(out);
-      DO(base32_encode(testin, sizeof(testin), out, &l1, idx));
+      DO(base32_encode(testin, sizeof(testin), out, &l1, testid[idx]));
       if (compare_testvector(out, l1, testout[idx], strlen(testout[idx]), "testout base32", idx)) {
          return CRYPT_FAIL_TESTVECTOR;
       }
       l2 = sizeof(tmp);
-      DO(base32_decode(out, l1, tmp, &l2, idx));
+      DO(base32_decode(out, l1, tmp, &l2, testid[idx]));
       if (compare_testvector(tmp, l2, testin, sizeof(testin), "testin base32", idx)) {
          return CRYPT_FAIL_TESTVECTOR;
       }