Home | History | Annotate | Download | only in ia5
      1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
      2  *
      3  * LibTomCrypt is a library that provides various cryptographic
      4  * algorithms in a highly modular and flexible manner.
      5  *
      6  * The library is free for all purposes without any express
      7  * guarantee it works.
      8  *
      9  * Tom St Denis, tomstdenis (at) gmail.com, http://libtomcrypt.com
     10  */
     11 #include "tomcrypt.h"
     12 
     13 /**
     14   @file der_length_ia5_string.c
     15   ASN.1 DER, get length of IA5 STRING, Tom St Denis
     16 */
     17 
     18 #ifdef LTC_DER
     19 
     20 static const struct {
     21    int code, value;
     22 } ia5_table[] = {
     23 { '\0', 0 },
     24 { '\a', 7 },
     25 { '\b', 8 },
     26 { '\t', 9 },
     27 { '\n', 10 },
     28 { '\f', 12 },
     29 { '\r', 13 },
     30 { ' ', 32 },
     31 { '!', 33 },
     32 { '"', 34 },
     33 { '#', 35 },
     34 { '$', 36 },
     35 { '%', 37 },
     36 { '&', 38 },
     37 { '\'', 39 },
     38 { '(', 40 },
     39 { ')', 41 },
     40 { '*', 42 },
     41 { '+', 43 },
     42 { ',', 44 },
     43 { '-', 45 },
     44 { '.', 46 },
     45 { '/', 47 },
     46 { '0', 48 },
     47 { '1', 49 },
     48 { '2', 50 },
     49 { '3', 51 },
     50 { '4', 52 },
     51 { '5', 53 },
     52 { '6', 54 },
     53 { '7', 55 },
     54 { '8', 56 },
     55 { '9', 57 },
     56 { ':', 58 },
     57 { ';', 59 },
     58 { '<', 60 },
     59 { '=', 61 },
     60 { '>', 62 },
     61 { '?', 63 },
     62 { '@', 64 },
     63 { 'A', 65 },
     64 { 'B', 66 },
     65 { 'C', 67 },
     66 { 'D', 68 },
     67 { 'E', 69 },
     68 { 'F', 70 },
     69 { 'G', 71 },
     70 { 'H', 72 },
     71 { 'I', 73 },
     72 { 'J', 74 },
     73 { 'K', 75 },
     74 { 'L', 76 },
     75 { 'M', 77 },
     76 { 'N', 78 },
     77 { 'O', 79 },
     78 { 'P', 80 },
     79 { 'Q', 81 },
     80 { 'R', 82 },
     81 { 'S', 83 },
     82 { 'T', 84 },
     83 { 'U', 85 },
     84 { 'V', 86 },
     85 { 'W', 87 },
     86 { 'X', 88 },
     87 { 'Y', 89 },
     88 { 'Z', 90 },
     89 { '[', 91 },
     90 { '\\', 92 },
     91 { ']', 93 },
     92 { '^', 94 },
     93 { '_', 95 },
     94 { '`', 96 },
     95 { 'a', 97 },
     96 { 'b', 98 },
     97 { 'c', 99 },
     98 { 'd', 100 },
     99 { 'e', 101 },
    100 { 'f', 102 },
    101 { 'g', 103 },
    102 { 'h', 104 },
    103 { 'i', 105 },
    104 { 'j', 106 },
    105 { 'k', 107 },
    106 { 'l', 108 },
    107 { 'm', 109 },
    108 { 'n', 110 },
    109 { 'o', 111 },
    110 { 'p', 112 },
    111 { 'q', 113 },
    112 { 'r', 114 },
    113 { 's', 115 },
    114 { 't', 116 },
    115 { 'u', 117 },
    116 { 'v', 118 },
    117 { 'w', 119 },
    118 { 'x', 120 },
    119 { 'y', 121 },
    120 { 'z', 122 },
    121 { '{', 123 },
    122 { '|', 124 },
    123 { '}', 125 },
    124 { '~', 126 }
    125 };
    126 
    127 int der_ia5_char_encode(int c)
    128 {
    129    int x;
    130    for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
    131        if (ia5_table[x].code == c) {
    132           return ia5_table[x].value;
    133        }
    134    }
    135    return -1;
    136 }
    137 
    138 int der_ia5_value_decode(int v)
    139 {
    140    int x;
    141    for (x = 0; x < (int)(sizeof(ia5_table)/sizeof(ia5_table[0])); x++) {
    142        if (ia5_table[x].value == v) {
    143           return ia5_table[x].code;
    144        }
    145    }
    146    return -1;
    147 }
    148 
    149 /**
    150   Gets length of DER encoding of IA5 STRING
    151   @param octets   The values you want to encode
    152   @param noctets  The number of octets in the string to encode
    153   @param outlen   [out] The length of the DER encoding for the given string
    154   @return CRYPT_OK if successful
    155 */
    156 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
    157 {
    158    unsigned long x;
    159 
    160    LTC_ARGCHK(outlen != NULL);
    161    LTC_ARGCHK(octets != NULL);
    162 
    163    /* scan string for validity */
    164    for (x = 0; x < noctets; x++) {
    165        if (der_ia5_char_encode(octets[x]) == -1) {
    166           return CRYPT_INVALID_ARG;
    167        }
    168    }
    169 
    170    if (noctets < 128) {
    171       /* 16 LL DD DD DD ... */
    172       *outlen = 2 + noctets;
    173    } else if (noctets < 256) {
    174       /* 16 81 LL DD DD DD ... */
    175       *outlen = 3 + noctets;
    176    } else if (noctets < 65536UL) {
    177       /* 16 82 LL LL DD DD DD ... */
    178       *outlen = 4 + noctets;
    179    } else if (noctets < 16777216UL) {
    180       /* 16 83 LL LL LL DD DD DD ... */
    181       *outlen = 5 + noctets;
    182    } else {
    183       return CRYPT_INVALID_ARG;
    184    }
    185 
    186    return CRYPT_OK;
    187 }
    188 
    189 #endif
    190 
    191 
    192 /* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/ia5/der_length_ia5_string.c,v $ */
    193 /* $Revision: 1.2 $ */
    194 /* $Date: 2006/03/31 14:15:35 $ */
    195