Codebase list libcryptx-perl / d12f081
ltc 1.18.0 Karel Miko 6 years ago
26 changed file(s) with 94 addition(s) and 108 deletion(s). Raw diff Collapse all Expand all
254254 RETVAL = newSVpvn((char*)out, out_len);
255255 }
256256 else if (strnEQ(type, "public_compressed", 17)) {
257 rv = ecc_export_raw(out, &out_len, PK_PUBLIC_COMPRESSED, &self->key);
257 rv = ecc_export_raw(out, &out_len, PK_PUBLIC|PK_COMPRESSED, &self->key);
258258 if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(public_compressed) failed: %s", error_to_string(rv));
259259 RETVAL = newSVpvn((char*)out, out_len);
260260 }
2626
2727 /* version */
2828 #define CRYPT 0x0118
29 #define SCRYPT "1.18.0-rc3"
29 #define SCRYPT "1.18.0"
3030
3131 /* max size of either a cipher/hash block or symmetric key [largest of the two] */
3232 #define MAXBLOCKSIZE 128
6767 CRYPT_OVERFLOW, /* An overflow of a value was detected/prevented */
6868
6969 CRYPT_UNUSED1, /* UNUSED1 */
70 CRYPT_UNUSED2, /* UNUSED2 */
70
71 CRYPT_INPUT_TOO_LONG, /* The input was longer than expected. */
7172
7273 CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */
7374
218218 #endif
219219 #endif
220220
221 #if defined(ENDIAN_64BITWORD) && !defined(_MSC_VER)
222 typedef unsigned long long ltc_mp_digit;
223 #else
224 typedef unsigned long ltc_mp_digit;
225 #endif
226
227 /* No asm is a quick way to disable anything "not portable" */
228 #ifdef LTC_NO_ASM
229 #define ENDIAN_NEUTRAL
230 #undef ENDIAN_32BITWORD
231 #undef ENDIAN_64BITWORD
232 #undef LTC_FAST
233 #define LTC_NO_ROLC
234 #define LTC_NO_BSWAP
235 #endif
236
221237 /* No LTC_FAST if: explicitly disabled OR non-gcc/non-clang compiler OR old gcc OR using -ansi -std=c99 */
222238 #if defined(LTC_NO_FAST) || (__GNUC__ < 4) || defined(__STRICT_ANSI__)
223239 #undef LTC_FAST
232248 #endif
233249 #endif
234250
235 #ifdef ENDIAN_64BITWORD
236 typedef ulong64 ltc_mp_digit;
237 #else
238 typedef ulong32 ltc_mp_digit;
239 #endif
240
241 /* No asm is a quick way to disable anything "not portable" */
242 #ifdef LTC_NO_ASM
243 #define ENDIAN_NEUTRAL
244 #undef ENDIAN_32BITWORD
245 #undef ENDIAN_64BITWORD
246 #undef LTC_FAST
247 #undef LTC_FAST_TYPE
248 #define LTC_NO_ROLC
249 #define LTC_NO_BSWAP
250 #endif
251
252251 #if !defined(ENDIAN_NEUTRAL) && (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
253 #error You must specify a word size as well as endianess in tomcrypt_cfg.h
252 #error You must specify a word size as well as endianess in tomcrypt_cfg.h
254253 #endif
255254
256255 #if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
258257 #endif
259258
260259 #if (defined(ENDIAN_32BITWORD) && defined(ENDIAN_64BITWORD))
261 #error Cannot be 32 and 64 bit words...
260 #error Cannot be 32 and 64 bit words...
262261 #endif
263262
264263 /* gcc 4.3 and up has a bswap builtin; detect it by gcc version.
6161
6262 /* shortcut to disable automatic inclusion */
6363 #if defined LTC_NOTHING && !defined LTC_EASY
64 #define LTC_NO_MATH
6564 #define LTC_NO_CIPHERS
6665 #define LTC_NO_MODES
6766 #define LTC_NO_HASHES
423422 /* Enable ECC timing resistant version by default */
424423 #define LTC_ECC_TIMING_RESISTANT
425424 #endif
426
427 /* define these PK sizes out of LTC_NO_PK
428 * to have them always defined
429 */
430 #if defined(LTC_MRSA)
431 /* Min and Max RSA key sizes (in bits) */
432 #ifndef MIN_RSA_SIZE
433 #define MIN_RSA_SIZE 1024
434 #endif
435 #ifndef MAX_RSA_SIZE
436 #define MAX_RSA_SIZE 4096
437 #endif
438 #endif
439
440 /* in cases where you want ASN.1/DER functionality, but no
441 * RSA, you can define this externally if 1024 is not enough
442 */
443 #if defined(LTC_MRSA)
444 #define LTC_DER_MAX_PUBKEY_SIZE MAX_RSA_SIZE
445 #elif !defined(LTC_DER_MAX_PUBKEY_SIZE)
446 /* this includes DSA */
447 #define LTC_DER_MAX_PUBKEY_SIZE 1024
448 #endif
449
450425
451426 /* PKCS #1 (RSA) and #5 (Password Handling) stuff */
452427 #ifndef LTC_NO_PKCS
2525
2626 #ifndef LTC_MILLER_RABIN_REPS
2727 /* Number of rounds of the Miller-Rabin test
28 * "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p() */
29 #define LTC_MILLER_RABIN_REPS 35
28 * "Reasonable values of reps are between 15 and 50." c.f. gmp doc of mpz_probab_prime_p()
29 * As of https://security.stackexchange.com/a/4546 we should use 40 rounds */
30 #define LTC_MILLER_RABIN_REPS 40
3031 #endif
3132
3233 int radix_to_bin(const void *in, int radix, void *out, unsigned long *len);
7777 #ifdef TFM_DESC
7878 void init_TFM(void);
7979 #endif
80 /* *** use of GMP is untested ***
8180 #ifdef GMP_DESC
8281 void init_GMP(void);
8382 #endif
84 */
8583
8684 #ifdef LTC_ADLER32
8785 typedef struct adler32_state_s
1010
1111 enum {
1212 PK_PUBLIC=0,
13 PK_PRIVATE=1,
14 PK_PUBLIC_COMPRESSED=2, /* used only when exporting public ECC key */
15 PK_CURVEOID=4 /* used only when exporting public ECC key */
13 PK_PRIVATE=1
1614 };
1715
1816 /* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
1917 #define PK_STD 0x1000
18 /* Indicates compressed public ECC key */
19 #define PK_COMPRESSED 0x2000
20 /* Indicates ECC key with the curve specified by OID */
21 #define PK_CURVEOID 0x4000
2022
2123 int rand_prime(void *N, long len, prng_state *prng, int wprng);
2224
398398 #if defined(LTC_DER)
399399 " DER "
400400 #endif
401 #if defined(LTC_DER_MAX_PUBKEY_SIZE)
402 " " NAME_VALUE(LTC_DER_MAX_PUBKEY_SIZE) " "
403 #endif
404401 #if defined(LTC_PKCS_1)
405402 " PKCS#1 "
406403 #endif
4747 _C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
4848 _C_STRINGIFY(CRYPT_OVERFLOW),
4949 _C_STRINGIFY(CRYPT_UNUSED1),
50 _C_STRINGIFY(CRYPT_UNUSED2),
50 _C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
5151 _C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
5252 _C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
5353 _C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
7676
7777 #ifdef LTC_MRSA
7878 {"LTC_MRSA", 1},
79 _C_STRINGIFY(MIN_RSA_SIZE),
80 _C_STRINGIFY(MAX_RSA_SIZE),
8179 #else
8280 {"LTC_MRSA", 0},
8381 #endif
106104 {"LTC_MDSA", 0},
107105 #endif
108106
109 #ifdef LTC_DER_MAX_PUBKEY_SIZE
110 _C_STRINGIFY(LTC_DER_MAX_PUBKEY_SIZE),
111 #endif
112107 #ifdef LTC_MILLER_RABIN_REPS
113108 _C_STRINGIFY(LTC_MILLER_RABIN_REPS),
114109 #endif
152147 _C_STRINGIFY(LTC_GCM_MODE_TEXT),
153148 #endif
154149
150 _C_STRINGIFY(LTC_MP_LT),
151 _C_STRINGIFY(LTC_MP_EQ),
152 _C_STRINGIFY(LTC_MP_GT),
153
155154 _C_STRINGIFY(LTC_MP_NO),
156155 _C_STRINGIFY(LTC_MP_YES),
157156
272272 _SZ_STRINGIFY_T(crc32_state),
273273 #endif
274274
275 _SZ_STRINGIFY_T(ltc_mp_digit),
276 _SZ_STRINGIFY_T(ltc_math_descriptor)
275277
276278 };
277279
4646 "An overflow of a value was detected/prevented.",
4747
4848 "UNUSED1.",
49 "UNUSED2.",
49
50 "The input was longer than expected.",
5051
5152 "Invalid sized parameter.",
5253
1616 #ifdef LTC_DER
1717
1818 #define SETBIT(v, n) (v=((unsigned char)(v) | (1U << (unsigned char)(n))))
19 #define CLRBIT(v, n) (v=((unsigned char)(v) & ~(1U << (unsigned char)(n))))
1920
2021 /**
2122 Store a BIT STRING
8384
8485 /* decode/store the bits */
8586 for (y = 0; y < blen; y++) {
86 if (in[x] & (1 << (7 - (y & 7)))) {
87 SETBIT(out[y/8], 7-(y%8));
88 }
89 if ((y & 7) == 7) {
90 ++x;
91 }
87 if (in[x] & (1 << (7 - (y & 7)))) {
88 SETBIT(out[y/8], 7-(y%8));
89 } else {
90 CLRBIT(out[y/8], 7-(y%8));
91 }
92 if ((y & 7) == 7) {
93 ++x;
94 }
9295 }
9396
9497 /* we done */
2020 /**
2121 Store a BIT STRING
2222 @param in The array of bits to store (8 per char)
23 @param inlen The number of bits tostore
23 @param inlen The number of bits to store
2424 @param out [out] The destination for the DER encoded BIT STRING
2525 @param outlen [in/out] The max size and resulting size of the DER BIT STRING
2626 @return CRYPT_OK if successful
6767
6868 /* store the bits in big endian format */
6969 for (y = buf = 0; y < inlen; y++) {
70 buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
71 if ((y & 7) == 7) {
72 out[x++] = buf;
73 buf = 0;
74 }
70 buf |= (getbit(in[y/8],7-y%8)?1:0) << (7 - (y & 7));
71 if ((y & 7) == 7) {
72 out[x++] = buf;
73 buf = 0;
74 }
7575 }
7676 /* store last byte */
7777 if (inlen & 7) {
327327 if (inlen == 0) {
328328 err = CRYPT_OK;
329329 } else {
330 err = CRYPT_PK_INVALID_SIZE;
330 err = CRYPT_INPUT_TOO_LONG;
331331 }
332332
333333 LBL_ERR:
6666 }
6767
6868 /* see if the OpenSSL DER format RSA public key will work */
69 tmpbuf = XCALLOC(1, LTC_DER_MAX_PUBKEY_SIZE*8);
69 tmpbuf = XCALLOC(1, inlen);
7070 if (tmpbuf == NULL) {
7171 err = CRYPT_MEM;
7272 goto LBL_ERR;
8080 * in a **BIT** string ... so we have to extract it then proceed to convert bit to octet
8181 */
8282 LTC_SET_ASN1(subject_pubkey, 0, LTC_ASN1_SEQUENCE, alg_id, 2);
83 LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, LTC_DER_MAX_PUBKEY_SIZE*8);
83 LTC_SET_ASN1(subject_pubkey, 1, LTC_ASN1_RAW_BIT_STRING, tmpbuf, inlen*8U);
8484
8585 err=der_decode_sequence(in, inlen, subject_pubkey, 2UL);
8686 if (err != CRYPT_OK) {
5757
5858 return der_encode_sequence_multi(out, outlen,
5959 LTC_ASN1_SEQUENCE, (unsigned long)sizeof(alg_id)/sizeof(alg_id[0]), alg_id,
60 LTC_ASN1_RAW_BIT_STRING, (unsigned long)(public_key_len*8), public_key,
60 LTC_ASN1_RAW_BIT_STRING, public_key_len*8U, public_key,
6161 LTC_ASN1_EOL, 0UL, NULL);
6262
6363 }
3636 LTC_ASN1_SHORT_INTEGER, 1UL, &version,
3737 LTC_ASN1_BIT_STRING, 1UL, &flags,
3838 LTC_ASN1_EOL, 0UL, NULL);
39 if (err != CRYPT_OK && err != CRYPT_PK_INVALID_SIZE) {
39 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
4040 goto error;
4141 }
4242
5757 goto error;
5858 }
5959 }
60 else {
60 else if (flags[0] == 0) {
6161 key->type = PK_PUBLIC;
6262 if ((err = der_decode_sequence_multi(in, inlen,
6363 LTC_ASN1_SHORT_INTEGER, 1UL, &version,
6868 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
6969 goto error;
7070 }
71 mp_clear(key->x);
72 key->x = NULL;
71 }
72 else {
73 err = CRYPT_INVALID_PACKET;
74 goto error;
7375 }
7476 }
7577 else {
2929 {
3030 unsigned char *skey, *expt;
3131 void *g_pub;
32 unsigned long x, y, hashOID[32];
32 unsigned long x, y;
33 unsigned long hashOID[32] = { 0 };
3334 int hash, err;
3435 ltc_asn1_list decode[3];
3536
4647 /* decode to find out hash */
4748 LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
4849 err = der_decode_sequence(in, inlen, decode, 1);
49 if (err != CRYPT_OK && err != CRYPT_PK_INVALID_SIZE) {
50 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
5051 return err;
5152 }
5253
4141 err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
4242 LTC_ASN1_EOL, 0UL, NULL);
4343
44 if (err == CRYPT_OK || err == CRYPT_PK_INVALID_SIZE) {
44 if (err == CRYPT_OK || err == CRYPT_INPUT_TOO_LONG) {
4545 /* private key */
46 if (flags[0]) {
46 if (flags[0] == 1) {
4747 if ((err = der_decode_sequence_multi(in, inlen,
4848 LTC_ASN1_BIT_STRING, 1UL, flags,
4949 LTC_ASN1_INTEGER, 1UL, key->g,
5858 goto LBL_OK;
5959 }
6060 /* public key */
61 else {
61 else if (flags[0] == 0) {
6262 if ((err = der_decode_sequence_multi(in, inlen,
6363 LTC_ASN1_BIT_STRING, 1UL, flags,
6464 LTC_ASN1_INTEGER, 1UL, key->g,
7070 }
7171 key->type = PK_PUBLIC;
7272 goto LBL_OK;
73 }
74 else {
75 err = CRYPT_INVALID_PACKET;
76 goto LBL_ERR;
7377 }
7478 }
7579 /* get key type */
8589 key->type = PK_PRIVATE;
8690 } else { /* public */
8791 ltc_asn1_list params[3];
88 unsigned long tmpbuf_len = LTC_DER_MAX_PUBKEY_SIZE*8;
92 unsigned long tmpbuf_len = inlen;
8993
9094 LTC_SET_ASN1(params, 0, LTC_ASN1_INTEGER, key->p, 1UL);
9195 LTC_SET_ASN1(params, 1, LTC_ASN1_INTEGER, key->q, 1UL);
5050 /* decode to find out hash */
5151 LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
5252 err = der_decode_sequence(in, inlen, decode, 1);
53 if (err != CRYPT_OK && err != CRYPT_PK_INVALID_SIZE) {
53 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
5454 return err;
5555 }
5656
8181 }
8282
8383 pubkeysize = ECC_BUF_SIZE;
84 if ((err = ecc_export_raw(pub_expt, &pubkeysize, PK_PUBLIC_COMPRESSED, &pubkey)) != CRYPT_OK) {
84 if ((err = ecc_export_raw(pub_expt, &pubkeysize, PK_PUBLIC|PK_COMPRESSED, &pubkey)) != CRYPT_OK) {
8585 ecc_free(&pubkey);
8686 goto LBL_ERR;
8787 }
1818 /** Export raw public or private key (public keys = ANS X9.63 compressed or uncompressed; private keys = raw bytes)
1919 @param out [out] destination of export
2020 @param outlen [in/out] Length of destination and final output size
21 @param type PK_PRIVATE, PK_PUBLIC or PK_PUBLIC_COMPRESSED
21 @param type PK_PRIVATE, PK_PUBLIC or PK_PUBLIC|PK_COMPRESSED
2222 @param key Key to export
2323 Return CRYPT_OK on success
2424 */
2626 int ecc_export_raw(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
2727 {
2828 unsigned long size, ksize;
29 int err;
29 int err, compressed;
3030
3131 LTC_ARGCHK(key != NULL);
3232 LTC_ARGCHK(out != NULL);
3737 }
3838 size = key->dp->size;
3939
40 if (type == PK_PUBLIC_COMPRESSED) {
40 compressed = type & PK_COMPRESSED;
41 type &= ~PK_COMPRESSED;
42
43 if (type == PK_PUBLIC && compressed) {
4144 if ((err = ltc_ecc_export_point(out, outlen, key->pubkey.x, key->pubkey.y, size, 1)) != CRYPT_OK) return err;
4245 }
4346 else if (type == PK_PUBLIC) {
5656 /* find out what type of key it is */
5757 err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,
5858 LTC_ASN1_EOL, 0UL, NULL);
59 if (err != CRYPT_OK && err != CRYPT_PK_INVALID_SIZE) {
59 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
6060 goto done;
6161 }
62
6263
6364 if (flags[0] == 1) {
6465 /* private key */
7273 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
7374 goto done;
7475 }
75 } else {
76 } else if (flags[0] == 0) {
7677 /* public key */
7778 key->type = PK_PUBLIC;
7879 if ((err = der_decode_sequence_multi(in, inlen,
8485 goto done;
8586 }
8687 }
88 else {
89 err = CRYPT_INVALID_PACKET;
90 goto done;
91 }
8792
8893 if (dp == NULL) {
89 /* BEWARE: Here we are looking up the curve params by keysize (neither curve name nor curve oid),
90 * which might be ambiguous (there can more than one curve for given keysize).
91 * Thus the chosen curve depends on order of items in ltc_ecc_sets[] - see ecc.c file.
92 */
9394 /* find the idx */
9495 for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);
9596 if (ltc_ecc_sets[key->idx].size == 0) {
3939 }
4040
4141 /* see if the OpenSSL DER format RSA public key will work */
42 tmpbuf_len = MAX_RSA_SIZE * 8;
42 tmpbuf_len = inlen;
4343 tmpbuf = XCALLOC(1, tmpbuf_len);
4444 if (tmpbuf == NULL) {
4545 err = CRYPT_MEM;
6868 err = der_decode_sequence_multi(in, inlen, LTC_ASN1_INTEGER, 1UL, key->N,
6969 LTC_ASN1_EOL, 0UL, NULL);
7070
71 if (err != CRYPT_OK && err != CRYPT_PK_INVALID_SIZE) {
71 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
7272 goto LBL_ERR;
7373 }
7474
3838 return err;
3939 }
4040
41 tmpbuf_len = MAX_RSA_SIZE * 8;
41 tmpbuf_len = inlen;
4242 tmpbuf = XCALLOC(1, tmpbuf_len);
4343 if (tmpbuf == NULL) {
4444 err = CRYPT_MEM;
3030
3131 LTC_ARGCHK(ltc_mp.name != NULL);
3232 LTC_ARGCHK(key != NULL);
33
34 if ((size < (MIN_RSA_SIZE/8)) || (size > (MAX_RSA_SIZE/8))) {
35 return CRYPT_INVALID_KEYSIZE;
36 }
33 LTC_ARGCHK(size > 0);
3734
3835 if ((e < 3) || ((e & 1) == 0)) {
3936 return CRYPT_INVALID_ARG;