2014-03-06 15:46:01 -08:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*/
|
|
|
|
#include "tomcrypt.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
@file crypt_constants.c
|
2014-07-15 13:58:48 +02:00
|
|
|
|
|
|
|
Make various constants available to dynamic languages
|
2014-03-06 15:46:01 -08:00
|
|
|
like Python - Larry Bugbee, February 2013
|
2014-07-15 13:58:48 +02:00
|
|
|
|
2014-03-06 15:46:01 -08:00
|
|
|
LB - Dec 2013 - revised to include compiler define options
|
2014-03-16 00:14:43 -07:00
|
|
|
LB - Mar 2014 - added endianness and word size
|
2014-03-06 15:46:01 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *name;
|
2014-09-29 23:30:02 +02:00
|
|
|
const int value;
|
2014-03-06 15:46:01 -08:00
|
|
|
} crypt_constant;
|
|
|
|
|
2014-07-15 15:27:31 +02:00
|
|
|
#define _C_STRINGIFY(s) { #s, s }
|
2014-03-16 00:14:43 -07:00
|
|
|
|
2014-07-15 15:27:31 +02:00
|
|
|
static const crypt_constant _crypt_constants[] = {
|
2017-08-08 19:02:11 +02:00
|
|
|
|
2017-08-09 11:18:13 +02:00
|
|
|
_C_STRINGIFY(CRYPT_OK),
|
|
|
|
_C_STRINGIFY(CRYPT_ERROR),
|
|
|
|
_C_STRINGIFY(CRYPT_NOP),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_KEYSIZE),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_ROUNDS),
|
|
|
|
_C_STRINGIFY(CRYPT_FAIL_TESTVECTOR),
|
|
|
|
_C_STRINGIFY(CRYPT_BUFFER_OVERFLOW),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_PACKET),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_PRNGSIZE),
|
|
|
|
_C_STRINGIFY(CRYPT_ERROR_READPRNG),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_CIPHER),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_HASH),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_PRNG),
|
|
|
|
_C_STRINGIFY(CRYPT_MEM),
|
|
|
|
_C_STRINGIFY(CRYPT_PK_TYPE_MISMATCH),
|
|
|
|
_C_STRINGIFY(CRYPT_PK_NOT_PRIVATE),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_ARG),
|
|
|
|
_C_STRINGIFY(CRYPT_FILE_NOTFOUND),
|
|
|
|
_C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
|
|
|
|
_C_STRINGIFY(CRYPT_OVERFLOW),
|
|
|
|
_C_STRINGIFY(CRYPT_UNUSED1),
|
2017-09-25 21:58:50 +02:00
|
|
|
_C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
|
2017-08-09 11:18:13 +02:00
|
|
|
_C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
|
|
|
|
_C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
|
|
|
|
_C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
|
|
|
|
_C_STRINGIFY(CRYPT_HASH_OVERFLOW),
|
|
|
|
|
2014-07-15 15:27:31 +02:00
|
|
|
_C_STRINGIFY(PK_PUBLIC),
|
|
|
|
_C_STRINGIFY(PK_PRIVATE),
|
|
|
|
|
2017-08-08 19:26:19 +02:00
|
|
|
_C_STRINGIFY(LTC_ENCRYPT),
|
|
|
|
_C_STRINGIFY(LTC_DECRYPT),
|
2017-08-08 19:02:11 +02:00
|
|
|
|
2014-07-15 15:27:31 +02:00
|
|
|
#ifdef LTC_PKCS_1
|
|
|
|
{"LTC_PKCS_1", 1},
|
|
|
|
/* Block types */
|
|
|
|
_C_STRINGIFY(LTC_PKCS_1_EMSA),
|
|
|
|
_C_STRINGIFY(LTC_PKCS_1_EME),
|
|
|
|
|
|
|
|
/* Padding types */
|
|
|
|
_C_STRINGIFY(LTC_PKCS_1_V1_5),
|
|
|
|
_C_STRINGIFY(LTC_PKCS_1_OAEP),
|
|
|
|
_C_STRINGIFY(LTC_PKCS_1_PSS),
|
2017-08-08 19:02:11 +02:00
|
|
|
_C_STRINGIFY(LTC_PKCS_1_V1_5_NA1),
|
2014-07-15 15:27:31 +02:00
|
|
|
#else
|
|
|
|
{"LTC_PKCS_1", 0},
|
2014-03-16 00:14:43 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LTC_MRSA
|
2014-07-15 15:27:31 +02:00
|
|
|
{"LTC_MRSA", 1},
|
|
|
|
_C_STRINGIFY(MIN_RSA_SIZE),
|
|
|
|
_C_STRINGIFY(MAX_RSA_SIZE),
|
|
|
|
#else
|
|
|
|
{"LTC_MRSA", 0},
|
2014-03-16 00:14:43 -07:00
|
|
|
#endif
|
2014-07-15 13:58:48 +02:00
|
|
|
|
2014-07-17 10:49:05 +02:00
|
|
|
#ifdef LTC_MKAT
|
|
|
|
{"LTC_MKAT", 1},
|
2014-07-15 15:27:31 +02:00
|
|
|
_C_STRINGIFY(MIN_KAT_SIZE),
|
|
|
|
_C_STRINGIFY(MAX_KAT_SIZE),
|
|
|
|
#else
|
2014-07-17 10:49:05 +02:00
|
|
|
{"LTC_MKAT", 0},
|
2014-07-15 15:27:31 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LTC_MECC
|
|
|
|
{"LTC_MECC", 1},
|
|
|
|
_C_STRINGIFY(ECC_BUF_SIZE),
|
|
|
|
_C_STRINGIFY(ECC_MAXSIZE),
|
|
|
|
#else
|
|
|
|
{"LTC_MECC", 0},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LTC_MDSA
|
|
|
|
{"LTC_MDSA", 1},
|
|
|
|
_C_STRINGIFY(LTC_MDSA_DELTA),
|
|
|
|
_C_STRINGIFY(LTC_MDSA_MAX_GROUP),
|
|
|
|
#else
|
|
|
|
{"LTC_MDSA", 0},
|
|
|
|
#endif
|
|
|
|
|
2017-06-12 12:29:16 +02:00
|
|
|
#ifdef LTC_MILLER_RABIN_REPS
|
|
|
|
_C_STRINGIFY(LTC_MILLER_RABIN_REPS),
|
|
|
|
#endif
|
|
|
|
|
2017-08-08 19:02:11 +02:00
|
|
|
#ifdef LTC_DER
|
|
|
|
/* DER handling */
|
|
|
|
_C_STRINGIFY(LTC_ASN1_EOL),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_BOOLEAN),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_INTEGER),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_BIT_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_OCTET_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_NULL),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_IA5_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_UTF8_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_UTCTIME),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_CHOICE),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_SEQUENCE),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_SET),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_SETOF),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_CONSTRUCTED),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC),
|
|
|
|
_C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
|
|
|
|
#endif
|
|
|
|
|
2014-07-15 15:27:31 +02:00
|
|
|
#ifdef LTC_CTR_MODE
|
|
|
|
{"LTC_CTR_MODE", 1},
|
|
|
|
_C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
|
|
|
|
_C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
|
|
|
|
_C_STRINGIFY(LTC_CTR_RFC3686),
|
|
|
|
#else
|
|
|
|
{"LTC_CTR_MODE", 0},
|
|
|
|
#endif
|
2017-08-08 19:02:11 +02:00
|
|
|
#ifdef LTC_GCM_MODE
|
|
|
|
_C_STRINGIFY(LTC_GCM_MODE_IV),
|
|
|
|
_C_STRINGIFY(LTC_GCM_MODE_AAD),
|
|
|
|
_C_STRINGIFY(LTC_GCM_MODE_TEXT),
|
|
|
|
#endif
|
|
|
|
|
2017-09-23 11:42:05 +02:00
|
|
|
_C_STRINGIFY(LTC_MP_LT),
|
|
|
|
_C_STRINGIFY(LTC_MP_EQ),
|
|
|
|
_C_STRINGIFY(LTC_MP_GT),
|
|
|
|
|
2017-08-08 19:02:11 +02:00
|
|
|
_C_STRINGIFY(LTC_MP_NO),
|
|
|
|
_C_STRINGIFY(LTC_MP_YES),
|
2014-07-15 15:27:31 +02:00
|
|
|
|
|
|
|
_C_STRINGIFY(MAXBLOCKSIZE),
|
|
|
|
_C_STRINGIFY(TAB_SIZE),
|
|
|
|
_C_STRINGIFY(ARGTYPE),
|
|
|
|
|
|
|
|
#ifdef LTM_DESC
|
|
|
|
{"LTM_DESC", 1},
|
|
|
|
#else
|
|
|
|
{"LTM_DESC", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef TFM_DESC
|
|
|
|
{"TFM_DESC", 1},
|
|
|
|
#else
|
|
|
|
{"TFM_DESC", 0},
|
|
|
|
#endif
|
|
|
|
#ifdef GMP_DESC
|
|
|
|
{"GMP_DESC", 1},
|
|
|
|
#else
|
|
|
|
{"GMP_DESC", 0},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LTC_FAST
|
|
|
|
{"LTC_FAST", 1},
|
|
|
|
#else
|
|
|
|
{"LTC_FAST", 0},
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LTC_NO_FILE
|
|
|
|
{"LTC_NO_FILE", 1},
|
|
|
|
#else
|
|
|
|
{"LTC_NO_FILE", 0},
|
2014-03-16 00:14:43 -07:00
|
|
|
#endif
|
|
|
|
|
2014-03-13 21:07:25 -07:00
|
|
|
#ifdef ENDIAN_LITTLE
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_LITTLE", 1},
|
2014-03-13 21:07:25 -07:00
|
|
|
#else
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_LITTLE", 0},
|
2014-03-13 21:07:25 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENDIAN_BIG
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_BIG", 1},
|
2014-03-13 21:07:25 -07:00
|
|
|
#else
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_BIG", 0},
|
2014-03-13 21:07:25 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENDIAN_32BITWORD
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_32BITWORD", 1},
|
2014-03-13 21:07:25 -07:00
|
|
|
#else
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_32BITWORD", 0},
|
2014-03-13 21:07:25 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENDIAN_64BITWORD
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_64BITWORD", 1},
|
2014-03-13 21:07:25 -07:00
|
|
|
#else
|
2014-07-15 13:58:48 +02:00
|
|
|
{"ENDIAN_64BITWORD", 0},
|
2014-03-13 21:07:25 -07:00
|
|
|
#endif
|
2014-07-15 15:27:31 +02:00
|
|
|
|
|
|
|
#ifdef ENDIAN_NEUTRAL
|
|
|
|
{"ENDIAN_NEUTRAL", 1},
|
|
|
|
#else
|
|
|
|
{"ENDIAN_NEUTRAL", 0},
|
|
|
|
#endif
|
2014-03-06 15:46:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* crypt_get_constant()
|
2014-03-16 00:14:43 -07:00
|
|
|
* valueout will be the value of the named constant
|
2014-03-06 15:46:01 -08:00
|
|
|
* return -1 if named item not found
|
|
|
|
*/
|
|
|
|
int crypt_get_constant(const char* namein, int *valueout) {
|
|
|
|
int i;
|
2014-07-15 14:09:50 +02:00
|
|
|
int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
|
2014-03-06 15:46:01 -08:00
|
|
|
for (i=0; i<_crypt_constants_len; i++) {
|
2017-03-15 21:29:09 +01:00
|
|
|
if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
|
2014-03-06 15:46:01 -08:00
|
|
|
*valueout = _crypt_constants[i].value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* crypt_list_all_constants()
|
2014-07-15 13:58:48 +02:00
|
|
|
* if names_list is NULL, names_list_size will be the minimum
|
2014-03-16 00:14:43 -07:00
|
|
|
* number of bytes needed to receive the complete names_list
|
2014-07-15 13:58:48 +02:00
|
|
|
* if names_list is NOT NULL, names_list must be the addr of
|
|
|
|
* sufficient memory allocated into which the names_list
|
|
|
|
* is to be written. Also, the value in names_list_size
|
|
|
|
* sets the upper bound of the number of characters to be
|
2014-03-06 15:46:01 -08:00
|
|
|
* written.
|
|
|
|
* a -1 return value signifies insufficient space made available
|
|
|
|
*/
|
2014-09-29 23:30:02 +02:00
|
|
|
int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
|
2014-03-06 15:46:01 -08:00
|
|
|
int i;
|
2014-09-29 23:30:02 +02:00
|
|
|
unsigned int total_len = 0;
|
2017-03-06 19:51:46 +01:00
|
|
|
char number[32], *ptr;
|
2014-03-06 15:46:01 -08:00
|
|
|
int number_len;
|
2014-07-15 13:58:48 +02:00
|
|
|
int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
|
|
|
|
|
2014-03-06 15:46:01 -08:00
|
|
|
/* calculate amount of memory required for the list */
|
|
|
|
for (i=0; i<count; i++) {
|
2017-03-14 23:09:14 +01:00
|
|
|
total_len += (unsigned int)strlen(_crypt_constants[i].name) + 1;
|
2014-07-15 13:58:48 +02:00
|
|
|
/* the above +1 is for the commas */
|
2014-09-29 23:30:02 +02:00
|
|
|
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
|
2014-07-15 14:09:50 +02:00
|
|
|
if ((number_len < 0) ||
|
|
|
|
((unsigned int)number_len >= sizeof(number)))
|
|
|
|
return -1;
|
|
|
|
total_len += number_len + 1;
|
2014-07-15 13:58:48 +02:00
|
|
|
/* this last +1 is for newlines (and ending NULL) */
|
2014-03-06 15:46:01 -08:00
|
|
|
}
|
2014-07-15 13:58:48 +02:00
|
|
|
|
2014-03-06 15:46:01 -08:00
|
|
|
if (names_list == NULL) {
|
|
|
|
*names_list_size = total_len;
|
|
|
|
} else {
|
|
|
|
if (total_len > *names_list_size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* build the names list */
|
2017-03-06 19:51:46 +01:00
|
|
|
ptr = names_list;
|
2014-03-06 15:46:01 -08:00
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
strcpy(ptr, _crypt_constants[i].name);
|
|
|
|
ptr += strlen(_crypt_constants[i].name);
|
|
|
|
strcpy(ptr, ",");
|
|
|
|
ptr += 1;
|
2014-07-15 13:58:48 +02:00
|
|
|
|
2014-09-29 23:30:02 +02:00
|
|
|
number_len = snprintf(number, sizeof(number), "%d", _crypt_constants[i].value);
|
2014-03-06 15:46:01 -08:00
|
|
|
strcpy(ptr, number);
|
|
|
|
ptr += number_len;
|
|
|
|
strcpy(ptr, "\n");
|
|
|
|
ptr += 1;
|
|
|
|
}
|
2014-07-15 13:58:48 +02:00
|
|
|
/* to remove the trailing new-line */
|
|
|
|
ptr -= 1;
|
2014-03-06 15:46:01 -08:00
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-19 13:43:49 +02:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|