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