Browse Source

tuning base32 stuff

Karel Miko 8 years ago
parent
commit
3fc98adaf5
4 changed files with 37 additions and 27 deletions
  1. 10 9
      doc/crypt.tex
  2. 8 2
      src/headers/tomcrypt_misc.h
  3. 11 9
      src/misc/base32/base32_decode.c
  4. 8 7
      src/misc/base32/base32_encode.c

+ 10 - 9
doc/crypt.tex

@@ -6370,12 +6370,12 @@ int base64url_strict_decode(const unsigned char *in,  unsigned long len,
 The library provides functions to encode and decode a Base32 coding scheme. The supported mappings are:
 
 \begin{center}
-\begin{tabular}{|c|c|c|}
-     \hline \textbf{alphabet\_idx} & \textbf{Mapping} & \textbf{Name} \\
-     \hline 0 & \textit{ABCDEFGHIJKLMNOPQRSTUVWXYZ234567} & RFC-4648 \\
-     \hline 1 & \textit{0123456789ABCDEFGHIJKLMNOPQRSTUV} & Base32hex \\
-     \hline 2 & \textit{YBNDRFG8EJKMCPQXOT1UWISZA345H769} & ZBase32 \\
-     \hline 3 & \textit{0123456789ABCDEFGHJKMNPQRSTVWXYZ} & Crockford \\
+\begin{tabular}{|l|l|l|}
+     \hline \textbf{alpha\_id} & \textbf{Mapping} & \textbf{Name} \\
+     \hline BASE32\_RFC4648 & ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 & RFC-4648 \\
+     \hline BASE32\_BASE32HEX & 0123456789ABCDEFGHIJKLMNOPQRSTUV & Base32hex \\
+     \hline BASE32\_ZBASE32 & YBNDRFG8EJKMCPQXOT1UWISZA345H769 & ZBase32 \\
+     \hline BASE32\_CROCKFORD & 0123456789ABCDEFGHJKMNPQRSTVWXYZ & Crockford \\
      \hline
 \end{tabular}
 \end{center}
@@ -6388,10 +6388,11 @@ int base32_encode(const unsigned char *in,
                         unsigned long  len,
                         unsigned char *out,
                         unsigned long *outlen,
-                        unsigned int   alphabet_idx);
+                        unsigned int   alpha_id);
 \end{verbatim}
 
-Where \textit{in} is the binary string, \textit{out} is where the ASCII output is placed and \textit{alphabet\_idx} is the alphabet index from the table above.
+Where \textit{in} is the binary string, \textit{out} is where the ASCII output is placed and \textit{alpha\_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:
 
@@ -6401,7 +6402,7 @@ int base32_decode(const unsigned char *in,
                         unsigned long  len,
                         unsigned char *out,
                         unsigned long *outlen,
-                        unsigned int   alphabet_idx);
+                        unsigned int   alpha_id);
 \end{verbatim}
 
 \mysection{Primality Testing}

+ 8 - 2
src/headers/tomcrypt_misc.h

@@ -32,12 +32,18 @@ int base64url_strict_decode(const unsigned char *in,  unsigned long len,
 
 /* ---- BASE32 Routines ---- */
 #ifdef LTC_BASE32
+typedef enum {
+   BASE32_RFC4648   = 0,
+   BASE32_BASE32HEX = 1,
+   BASE32_ZBASE32   = 2,
+   BASE32_CROCKFORD = 3
+} base32_alphabet;
 int base32_encode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        unsigned int alphabet_idx);
+                        base32_alphabet alpha_id);
 int base32_decode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        unsigned int alphabet_idx);
+                        base32_alphabet alpha_id);
 #endif
 
 /* ===> LTC_HKDF -- RFC5869 HMAC-based Key Derivation Function <=== */

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

@@ -17,36 +17,38 @@
    @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
    @return CRYPT_OK if successful
 */
 int base32_decode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        unsigned int alphabet_idx)
+                        base32_alphabet alpha_id)
 {
    unsigned long x;
    int y = 0;
    ulong64 t = 0;
-   unsigned char c, *map;
-   unsigned char tables[4][43] = {
-      {  /* alphabet_idx 0 = rfc4648 ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
+   unsigned char c;
+   const unsigned char *map;
+   const unsigned char tables[4][43] = {
+      {  /* alpha_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*/
       },
-      {  /* alphabet_idx 1 = base32hex 0123456789ABCDEFGHIJKLMNOPQRSTUV */
+      {  /* alpha_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*/
       },
-      {  /* alphabet_idx 2 = zbase32 YBNDRFG8EJKMCPQXOT1UWISZA345H769 */
+      {  /* alpha_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*/
       },
-      {  /* alphabet_idx 3 = crockford 0123456789ABCDEFGHJKMNPQRSTVWXYZ + O=>0 + IL=>1 */
+      {  /* alpha_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*/,
@@ -57,7 +59,7 @@ int base32_decode(const unsigned char *in,  unsigned long inlen,
    LTC_ARGCHK(in     != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
-   LTC_ARGCHK(alphabet_idx < 4);
+   LTC_ARGCHK(alpha_id < 4);
 
    /* ignore all trailing = */
    while (inlen > 0 && in[inlen-1] == '=') inlen--;
@@ -82,7 +84,7 @@ int base32_decode(const unsigned char *in,  unsigned long inlen,
       return CRYPT_INVALID_PACKET;
    }
 
-   map = tables[alphabet_idx];
+   map = tables[alpha_id];
    for (x = 0; x < inlen; x++) {
       c = in[x];
       /* convert to upper case */

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

@@ -17,25 +17,26 @@
    @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
    @return CRYPT_OK if successful
 */
 int base32_encode(const unsigned char *in,  unsigned long inlen,
                         unsigned char *out, unsigned long *outlen,
-                        unsigned int alphabet_idx)
+                        base32_alphabet alpha_id)
 {
    unsigned long i, x;
    unsigned char *codes;
    const char *alphabet[4] = {
-      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",     /* alphabet_idx 0 = rfc4648   */
-      "0123456789ABCDEFGHIJKLMNOPQRSTUV",     /* alphabet_idx 1 = base32hex */
-      "ybndrfg8ejkmcpqxot1uwisza345h769",     /* alphabet_idx 2 = zbase32   */
-      "0123456789ABCDEFGHJKMNPQRSTVWXYZ"      /* alphabet_idx 3 = crockford */
+      "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",     /* alpha_id BASE32_RFC4648   */
+      "0123456789ABCDEFGHIJKLMNOPQRSTUV",     /* alpha_id BASE32_BASE32HEX */
+      "ybndrfg8ejkmcpqxot1uwisza345h769",     /* alpha_id BASE32_ZBASE32   */
+      "0123456789ABCDEFGHJKMNPQRSTVWXYZ"      /* alpha_id BASE32_CROCKFORD */
    };
 
    LTC_ARGCHK(in     != NULL);
    LTC_ARGCHK(out    != NULL);
    LTC_ARGCHK(outlen != NULL);
-   LTC_ARGCHK(alphabet_idx < 4);
+   LTC_ARGCHK(alpha_id < 4);
 
    /* no input, nothing to do */
    if (inlen == 0) {
@@ -51,7 +52,7 @@ int base32_encode(const unsigned char *in,  unsigned long inlen,
    }
    *outlen = x;
 
-   codes = (unsigned char*)alphabet[alphabet_idx];
+   codes = (unsigned char*)alphabet[alpha_id];
    x = 5 * (inlen / 5);
    for (i = 0; i < x; i += 5) {
       *out++ = codes[(in[0] >> 3) & 0x1F];