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