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