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