comparison libtomcrypt/src/misc/crypt/crypt_constants.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 e9dba7abd939
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 crypt_constants.c
13
14 Make various constants available to dynamic languages
15 like Python - Larry Bugbee, February 2013
16
17 LB - Dec 2013 - revised to include compiler define options
18 LB - Mar 2014 - added endianness and word size
19 */
20
21 typedef struct {
22 const char *name;
23 const int value;
24 } crypt_constant;
25
26 #define _C_STRINGIFY(s) { #s, s }
27
28 static const crypt_constant _crypt_constants[] = {
29
30 _C_STRINGIFY(CRYPT_OK),
31 _C_STRINGIFY(CRYPT_ERROR),
32 _C_STRINGIFY(CRYPT_NOP),
33 _C_STRINGIFY(CRYPT_INVALID_KEYSIZE),
34 _C_STRINGIFY(CRYPT_INVALID_ROUNDS),
35 _C_STRINGIFY(CRYPT_FAIL_TESTVECTOR),
36 _C_STRINGIFY(CRYPT_BUFFER_OVERFLOW),
37 _C_STRINGIFY(CRYPT_INVALID_PACKET),
38 _C_STRINGIFY(CRYPT_INVALID_PRNGSIZE),
39 _C_STRINGIFY(CRYPT_ERROR_READPRNG),
40 _C_STRINGIFY(CRYPT_INVALID_CIPHER),
41 _C_STRINGIFY(CRYPT_INVALID_HASH),
42 _C_STRINGIFY(CRYPT_INVALID_PRNG),
43 _C_STRINGIFY(CRYPT_MEM),
44 _C_STRINGIFY(CRYPT_PK_TYPE_MISMATCH),
45 _C_STRINGIFY(CRYPT_PK_NOT_PRIVATE),
46 _C_STRINGIFY(CRYPT_INVALID_ARG),
47 _C_STRINGIFY(CRYPT_FILE_NOTFOUND),
48 _C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
49 _C_STRINGIFY(CRYPT_OVERFLOW),
50 _C_STRINGIFY(CRYPT_UNUSED1),
51 _C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
52 _C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
53 _C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
54 _C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
55 _C_STRINGIFY(CRYPT_HASH_OVERFLOW),
56
57 _C_STRINGIFY(PK_PUBLIC),
58 _C_STRINGIFY(PK_PRIVATE),
59
60 _C_STRINGIFY(LTC_ENCRYPT),
61 _C_STRINGIFY(LTC_DECRYPT),
62
63 #ifdef LTC_PKCS_1
64 {"LTC_PKCS_1", 1},
65 /* Block types */
66 _C_STRINGIFY(LTC_PKCS_1_EMSA),
67 _C_STRINGIFY(LTC_PKCS_1_EME),
68
69 /* Padding types */
70 _C_STRINGIFY(LTC_PKCS_1_V1_5),
71 _C_STRINGIFY(LTC_PKCS_1_OAEP),
72 _C_STRINGIFY(LTC_PKCS_1_PSS),
73 _C_STRINGIFY(LTC_PKCS_1_V1_5_NA1),
74 #else
75 {"LTC_PKCS_1", 0},
76 #endif
77
78 #ifdef LTC_MRSA
79 {"LTC_MRSA", 1},
80 #else
81 {"LTC_MRSA", 0},
82 #endif
83
84 #ifdef LTC_MKAT
85 {"LTC_MKAT", 1},
86 _C_STRINGIFY(MIN_KAT_SIZE),
87 _C_STRINGIFY(MAX_KAT_SIZE),
88 #else
89 {"LTC_MKAT", 0},
90 #endif
91
92 #ifdef LTC_MECC
93 {"LTC_MECC", 1},
94 _C_STRINGIFY(ECC_BUF_SIZE),
95 _C_STRINGIFY(ECC_MAXSIZE),
96 #else
97 {"LTC_MECC", 0},
98 #endif
99
100 #ifdef LTC_MDSA
101 {"LTC_MDSA", 1},
102 _C_STRINGIFY(LTC_MDSA_DELTA),
103 _C_STRINGIFY(LTC_MDSA_MAX_GROUP),
104 #else
105 {"LTC_MDSA", 0},
106 #endif
107
108 #ifdef LTC_MILLER_RABIN_REPS
109 _C_STRINGIFY(LTC_MILLER_RABIN_REPS),
110 #endif
111
112 #ifdef LTC_DER
113 /* DER handling */
114 _C_STRINGIFY(LTC_ASN1_EOL),
115 _C_STRINGIFY(LTC_ASN1_BOOLEAN),
116 _C_STRINGIFY(LTC_ASN1_INTEGER),
117 _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
118 _C_STRINGIFY(LTC_ASN1_BIT_STRING),
119 _C_STRINGIFY(LTC_ASN1_OCTET_STRING),
120 _C_STRINGIFY(LTC_ASN1_NULL),
121 _C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
122 _C_STRINGIFY(LTC_ASN1_IA5_STRING),
123 _C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
124 _C_STRINGIFY(LTC_ASN1_UTF8_STRING),
125 _C_STRINGIFY(LTC_ASN1_UTCTIME),
126 _C_STRINGIFY(LTC_ASN1_CHOICE),
127 _C_STRINGIFY(LTC_ASN1_SEQUENCE),
128 _C_STRINGIFY(LTC_ASN1_SET),
129 _C_STRINGIFY(LTC_ASN1_SETOF),
130 _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
131 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
132 _C_STRINGIFY(LTC_ASN1_CONSTRUCTED),
133 _C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC),
134 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
135 #endif
136
137 #ifdef LTC_CTR_MODE
138 {"LTC_CTR_MODE", 1},
139 _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
140 _C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
141 _C_STRINGIFY(LTC_CTR_RFC3686),
142 #else
143 {"LTC_CTR_MODE", 0},
144 #endif
145 #ifdef LTC_GCM_MODE
146 _C_STRINGIFY(LTC_GCM_MODE_IV),
147 _C_STRINGIFY(LTC_GCM_MODE_AAD),
148 _C_STRINGIFY(LTC_GCM_MODE_TEXT),
149 #endif
150
151 _C_STRINGIFY(LTC_MP_LT),
152 _C_STRINGIFY(LTC_MP_EQ),
153 _C_STRINGIFY(LTC_MP_GT),
154
155 _C_STRINGIFY(LTC_MP_NO),
156 _C_STRINGIFY(LTC_MP_YES),
157
158 _C_STRINGIFY(MAXBLOCKSIZE),
159 _C_STRINGIFY(TAB_SIZE),
160 _C_STRINGIFY(ARGTYPE),
161
162 #ifdef LTM_DESC
163 {"LTM_DESC", 1},
164 #else
165 {"LTM_DESC", 0},
166 #endif
167 #ifdef TFM_DESC
168 {"TFM_DESC", 1},
169 #else
170 {"TFM_DESC", 0},
171 #endif
172 #ifdef GMP_DESC
173 {"GMP_DESC", 1},
174 #else
175 {"GMP_DESC", 0},
176 #endif
177
178 #ifdef LTC_FAST
179 {"LTC_FAST", 1},
180 #else
181 {"LTC_FAST", 0},
182 #endif
183
184 #ifdef LTC_NO_FILE
185 {"LTC_NO_FILE", 1},
186 #else
187 {"LTC_NO_FILE", 0},
188 #endif
189
190 #ifdef ENDIAN_LITTLE
191 {"ENDIAN_LITTLE", 1},
192 #else
193 {"ENDIAN_LITTLE", 0},
194 #endif
195
196 #ifdef ENDIAN_BIG
197 {"ENDIAN_BIG", 1},
198 #else
199 {"ENDIAN_BIG", 0},
200 #endif
201
202 #ifdef ENDIAN_32BITWORD
203 {"ENDIAN_32BITWORD", 1},
204 #else
205 {"ENDIAN_32BITWORD", 0},
206 #endif
207
208 #ifdef ENDIAN_64BITWORD
209 {"ENDIAN_64BITWORD", 1},
210 #else
211 {"ENDIAN_64BITWORD", 0},
212 #endif
213
214 #ifdef ENDIAN_NEUTRAL
215 {"ENDIAN_NEUTRAL", 1},
216 #else
217 {"ENDIAN_NEUTRAL", 0},
218 #endif
219 };
220
221
222 /* crypt_get_constant()
223 * valueout will be the value of the named constant
224 * return -1 if named item not found
225 */
226 int crypt_get_constant(const char* namein, int *valueout) {
227 int i;
228 int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
229 for (i=0; i<_crypt_constants_len; i++) {
230 if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
231 *valueout = _crypt_constants[i].value;
232 return 0;
233 }
234 }
235 return 1;
236 }
237
238 /* crypt_list_all_constants()
239 * if names_list is NULL, names_list_size will be the minimum
240 * number of bytes needed to receive the complete names_list
241 * if names_list is NOT NULL, names_list must be the addr of
242 * sufficient memory allocated into which the names_list
243 * is to be written. Also, the value in names_list_size
244 * sets the upper bound of the number of characters to be
245 * written.
246 * a -1 return value signifies insufficient space made available
247 */
248 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
249 int i;
250 unsigned int total_len = 0;
251 char number[32], *ptr;
252 int number_len;
253 int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
254
255 /* calculate amount of memory required for the list */
256 for (i=0; i<count; i++) {
257 total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
258 /* the above +1 is for the commas */
259 number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
260 if ((number_len < 0) ||
261 ((unsigned int)number_len >= sizeof(number)))
262 return -1;
263 total_len += number_len + 1;
264 /* this last +1 is for newlines (and ending NULL) */
265 }
266
267 if (names_list == NULL) {
268 *names_list_size = total_len;
269 } else {
270 if (total_len > *names_list_size) {
271 return -1;
272 }
273 /* build the names list */
274 ptr = names_list;
275 for (i=0; i<count; i++) {
276 strcpy(ptr, _crypt_constants[i].name);
277 ptr += strlen(_crypt_constants[i].name);
278 strcpy(ptr, ",");
279 ptr += 1;
280
281 number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
282 strcpy(ptr, number);
283 ptr += number_len;
284 strcpy(ptr, "\n");
285 ptr += 1;
286 }
287 /* to remove the trailing new-line */
288 ptr -= 1;
289 *ptr = 0;
290 }
291 return 0;
292 }
293
294
295 /* ref: $Format:%D$ */
296 /* git commit: $Format:%H$ */
297 /* commit time: $Format:%ai$ */