]> pd.if.org Git - zpackage/blob - libtomcrypt/src/misc/crypt/crypt_constants.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / misc / crypt / crypt_constants.c
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     {"LTC_DER", 1},
115     _C_STRINGIFY(LTC_ASN1_EOL),
116     _C_STRINGIFY(LTC_ASN1_BOOLEAN),
117     _C_STRINGIFY(LTC_ASN1_INTEGER),
118     _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
119     _C_STRINGIFY(LTC_ASN1_BIT_STRING),
120     _C_STRINGIFY(LTC_ASN1_OCTET_STRING),
121     _C_STRINGIFY(LTC_ASN1_NULL),
122     _C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
123     _C_STRINGIFY(LTC_ASN1_IA5_STRING),
124     _C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
125     _C_STRINGIFY(LTC_ASN1_UTF8_STRING),
126     _C_STRINGIFY(LTC_ASN1_UTCTIME),
127     _C_STRINGIFY(LTC_ASN1_CHOICE),
128     _C_STRINGIFY(LTC_ASN1_SEQUENCE),
129     _C_STRINGIFY(LTC_ASN1_SET),
130     _C_STRINGIFY(LTC_ASN1_SETOF),
131     _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
132     _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
133     _C_STRINGIFY(LTC_ASN1_CONSTRUCTED),
134     _C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC),
135     _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
136     _C_STRINGIFY(LTC_DER_MAX_RECURSION),
137 #else
138     {"LTC_DER", 0},
139 #endif
140
141 #ifdef LTC_CTR_MODE
142     {"LTC_CTR_MODE", 1},
143     _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
144     _C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
145     _C_STRINGIFY(LTC_CTR_RFC3686),
146 #else
147     {"LTC_CTR_MODE", 0},
148 #endif
149 #ifdef LTC_GCM_MODE
150     _C_STRINGIFY(LTC_GCM_MODE_IV),
151     _C_STRINGIFY(LTC_GCM_MODE_AAD),
152     _C_STRINGIFY(LTC_GCM_MODE_TEXT),
153 #endif
154
155     _C_STRINGIFY(LTC_MP_LT),
156     _C_STRINGIFY(LTC_MP_EQ),
157     _C_STRINGIFY(LTC_MP_GT),
158
159     _C_STRINGIFY(LTC_MP_NO),
160     _C_STRINGIFY(LTC_MP_YES),
161
162     _C_STRINGIFY(MAXBLOCKSIZE),
163     _C_STRINGIFY(TAB_SIZE),
164     _C_STRINGIFY(ARGTYPE),
165
166 #ifdef LTM_DESC
167     {"LTM_DESC", 1},
168 #else
169     {"LTM_DESC", 0},
170 #endif
171 #ifdef TFM_DESC
172     {"TFM_DESC", 1},
173 #else
174     {"TFM_DESC", 0},
175 #endif
176 #ifdef GMP_DESC
177     {"GMP_DESC", 1},
178 #else
179     {"GMP_DESC", 0},
180 #endif
181
182 #ifdef LTC_FAST
183     {"LTC_FAST", 1},
184 #else
185     {"LTC_FAST", 0},
186 #endif
187
188 #ifdef LTC_NO_FILE
189     {"LTC_NO_FILE", 1},
190 #else
191     {"LTC_NO_FILE", 0},
192 #endif
193
194 #ifdef ENDIAN_LITTLE
195     {"ENDIAN_LITTLE",             1},
196 #else
197     {"ENDIAN_LITTLE",             0},
198 #endif
199
200 #ifdef ENDIAN_BIG
201     {"ENDIAN_BIG",                1},
202 #else
203     {"ENDIAN_BIG",                0},
204 #endif
205
206 #ifdef ENDIAN_32BITWORD
207     {"ENDIAN_32BITWORD",          1},
208 #else
209     {"ENDIAN_32BITWORD",          0},
210 #endif
211
212 #ifdef ENDIAN_64BITWORD
213     {"ENDIAN_64BITWORD",          1},
214 #else
215     {"ENDIAN_64BITWORD",          0},
216 #endif
217
218 #ifdef ENDIAN_NEUTRAL
219     {"ENDIAN_NEUTRAL",            1},
220 #else
221     {"ENDIAN_NEUTRAL",            0},
222 #endif
223 };
224
225
226 /* crypt_get_constant()
227  * valueout will be the value of the named constant
228  * return -1 if named item not found
229  */
230 int crypt_get_constant(const char* namein, int *valueout) {
231     int i;
232     int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
233     for (i=0; i<_crypt_constants_len; i++) {
234         if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
235             *valueout = _crypt_constants[i].value;
236             return 0;
237         }
238     }
239     return 1;
240 }
241
242 /* crypt_list_all_constants()
243  * if names_list is NULL, names_list_size will be the minimum
244  *     number of bytes needed to receive the complete names_list
245  * if names_list is NOT NULL, names_list must be the addr of
246  *     sufficient memory allocated into which the names_list
247  *     is to be written.  Also, the value in names_list_size
248  *     sets the upper bound of the number of characters to be
249  *     written.
250  * a -1 return value signifies insufficient space made available
251  */
252 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
253     int i;
254     unsigned int total_len = 0;
255     char *ptr;
256     int number_len;
257     int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
258
259     /* calculate amount of memory required for the list */
260     for (i=0; i<count; i++) {
261         number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
262         if (number_len < 0)
263           return -1;
264         total_len += number_len;
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             number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
277             if (number_len < 0) return -1;
278             if ((unsigned int)number_len > total_len) return -1;
279             total_len -= number_len;
280             ptr += number_len;
281         }
282         /* to remove the trailing new-line */
283         ptr -= 1;
284         *ptr = 0;
285     }
286     return 0;
287 }
288
289
290 /* ref:         $Format:%D$ */
291 /* git commit:  $Format:%H$ */
292 /* commit time: $Format:%ai$ */