comparison libtomcrypt/src/pk/asn1/der/teletex_string/der_length_teletex_string.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
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_teletex_string.c
13 ASN.1 DER, get length of teletex STRING
14 */
15
16 #ifdef LTC_DER
17
18 static const struct {
19 int code, value;
20 } teletex_table[] = {
21 { '\0', 0 },
22 { '\a', 7 },
23 { '\b', 8 },
24 { '\t', 9 },
25 { '\n', 10 },
26 { '\v', 11 },
27 { '\f', 12 },
28 { '\r', 13 },
29 { ' ', 32 },
30 { '!', 33 },
31 { '"', 34 },
32 { '%', 37 },
33 { '&', 38 },
34 { '\'', 39 },
35 { '(', 40 },
36 { ')', 41 },
37 { '+', 43 },
38 { ',', 44 },
39 { '-', 45 },
40 { '.', 46 },
41 { '/', 47 },
42 { '0', 48 },
43 { '1', 49 },
44 { '2', 50 },
45 { '3', 51 },
46 { '4', 52 },
47 { '5', 53 },
48 { '6', 54 },
49 { '7', 55 },
50 { '8', 56 },
51 { '9', 57 },
52 { ':', 58 },
53 { ';', 59 },
54 { '<', 60 },
55 { '=', 61 },
56 { '>', 62 },
57 { '?', 63 },
58 { '@', 64 },
59 { 'A', 65 },
60 { 'B', 66 },
61 { 'C', 67 },
62 { 'D', 68 },
63 { 'E', 69 },
64 { 'F', 70 },
65 { 'G', 71 },
66 { 'H', 72 },
67 { 'I', 73 },
68 { 'J', 74 },
69 { 'K', 75 },
70 { 'L', 76 },
71 { 'M', 77 },
72 { 'N', 78 },
73 { 'O', 79 },
74 { 'P', 80 },
75 { 'Q', 81 },
76 { 'R', 82 },
77 { 'S', 83 },
78 { 'T', 84 },
79 { 'U', 85 },
80 { 'V', 86 },
81 { 'W', 87 },
82 { 'X', 88 },
83 { 'Y', 89 },
84 { 'Z', 90 },
85 { '[', 91 },
86 { ']', 93 },
87 { '_', 95 },
88 { 'a', 97 },
89 { 'b', 98 },
90 { 'c', 99 },
91 { 'd', 100 },
92 { 'e', 101 },
93 { 'f', 102 },
94 { 'g', 103 },
95 { 'h', 104 },
96 { 'i', 105 },
97 { 'j', 106 },
98 { 'k', 107 },
99 { 'l', 108 },
100 { 'm', 109 },
101 { 'n', 110 },
102 { 'o', 111 },
103 { 'p', 112 },
104 { 'q', 113 },
105 { 'r', 114 },
106 { 's', 115 },
107 { 't', 116 },
108 { 'u', 117 },
109 { 'v', 118 },
110 { 'w', 119 },
111 { 'x', 120 },
112 { 'y', 121 },
113 { 'z', 122 },
114 { '|', 124 },
115 { ' ', 160 },
116 { 0xa1, 161 },
117 { 0xa2, 162 },
118 { 0xa3, 163 },
119 { '$', 164 },
120 { 0xa5, 165 },
121 { '#', 166 },
122 { 0xa7, 167 },
123 { 0xa4, 168 },
124 { 0xab, 171 },
125 { 0xb0, 176 },
126 { 0xb1, 177 },
127 { 0xb2, 178 },
128 { 0xb3, 179 },
129 { 0xd7, 180 },
130 { 0xb5, 181 },
131 { 0xb6, 182 },
132 { 0xb7, 183 },
133 { 0xf7, 184 },
134 { 0xbb, 187 },
135 { 0xbc, 188 },
136 { 0xbd, 189 },
137 { 0xbe, 190 },
138 { 0xbf, 191 },
139 };
140
141 int der_teletex_char_encode(int c)
142 {
143 int x;
144 for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
145 if (teletex_table[x].code == c) {
146 return teletex_table[x].value;
147 }
148 }
149 return -1;
150 }
151
152 int der_teletex_value_decode(int v)
153 {
154 int x;
155 for (x = 0; x < (int)(sizeof(teletex_table)/sizeof(teletex_table[0])); x++) {
156 if (teletex_table[x].value == v) {
157 return teletex_table[x].code;
158 }
159 }
160 return -1;
161 }
162
163 /**
164 Gets length of DER encoding of teletex STRING
165 @param octets The values you want to encode
166 @param noctets The number of octets in the string to encode
167 @param outlen [out] The length of the DER encoding for the given string
168 @return CRYPT_OK if successful
169 */
170 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen)
171 {
172 unsigned long x;
173
174 LTC_ARGCHK(outlen != NULL);
175 LTC_ARGCHK(octets != NULL);
176
177 /* scan string for validity */
178 for (x = 0; x < noctets; x++) {
179 if (der_teletex_char_encode(octets[x]) == -1) {
180 return CRYPT_INVALID_ARG;
181 }
182 }
183
184 if (noctets < 128) {
185 /* 16 LL DD DD DD ... */
186 *outlen = 2 + noctets;
187 } else if (noctets < 256) {
188 /* 16 81 LL DD DD DD ... */
189 *outlen = 3 + noctets;
190 } else if (noctets < 65536UL) {
191 /* 16 82 LL LL DD DD DD ... */
192 *outlen = 4 + noctets;
193 } else if (noctets < 16777216UL) {
194 /* 16 83 LL LL LL DD DD DD ... */
195 *outlen = 5 + noctets;
196 } else {
197 return CRYPT_INVALID_ARG;
198 }
199
200 return CRYPT_OK;
201 }
202
203 #endif
204
205
206 /* ref: $Format:%D$ */
207 /* git commit: $Format:%H$ */
208 /* commit time: $Format:%ai$ */