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