ltc 1.18-rc2
Karel Miko
6 years ago
152 | 152 | return CRYPT_OK; |
153 | 153 | } |
154 | 154 | |
155 | /* function for processing blocks */ | |
155 | /** | |
156 | Function for processing blocks | |
157 | @param md The hash state | |
158 | @param buf The data to hash | |
159 | @param len The length of the data (octets) | |
160 | @return CRYPT_OK if successful | |
161 | */ | |
156 | 162 | static int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len); |
157 | 163 | static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize) |
158 | 164 |
25 | 25 | #endif |
26 | 26 | |
27 | 27 | /* version */ |
28 | #define CRYPT 0x0117 | |
29 | #define SCRYPT "1.17" | |
28 | #define CRYPT 0x0118 | |
29 | #define SCRYPT "1.18.0-rc2" | |
30 | 30 | |
31 | 31 | /* max size of either a cipher/hash block or symmetric key [largest of the two] */ |
32 | 32 | #define MAXBLOCKSIZE 128 |
29 | 29 | #define LTC_MILLER_RABIN_REPS 35 |
30 | 30 | #endif |
31 | 31 | |
32 | int radix_to_bin(const void *in, int radix, void *out, size_t* len); | |
32 | int radix_to_bin(const void *in, int radix, void *out, unsigned long *len); | |
33 | 33 | |
34 | 34 | /** math descriptor */ |
35 | 35 | typedef struct { |
224 | 224 | int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key); |
225 | 225 | int dh_set_pg_groupsize(int groupsize, dh_key *key); |
226 | 226 | |
227 | int dh_set_key(const unsigned char *pub, unsigned long publen, | |
228 | const unsigned char *priv, unsigned long privlen, | |
229 | dh_key *key); | |
227 | int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key); | |
230 | 228 | int dh_generate_key(prng_state *prng, int wprng, dh_key *key); |
231 | 229 | |
232 | 230 | int dh_shared_secret(dh_key *private_key, dh_key *public_key, |
234 | 232 | |
235 | 233 | void dh_free(dh_key *key); |
236 | 234 | |
237 | int dh_export_key(void *out, unsigned long *outlen, | |
238 | int type, dh_key *key); | |
235 | int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key); | |
239 | 236 | |
240 | 237 | #ifdef LTC_SOURCE |
241 | 238 | /* internal helper functions */ |
479 | 476 | int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key); |
480 | 477 | int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); |
481 | 478 | |
482 | int dsa_set_key(const unsigned char *pub, unsigned long publen, | |
483 | const unsigned char *priv, unsigned long privlen, | |
484 | dsa_key *key); | |
479 | int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key); | |
485 | 480 | int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key); |
486 | 481 | |
487 | 482 | void dsa_free(dsa_key *key); |
30 | 30 | |
31 | 31 | @return CRYPT_OK on success. |
32 | 32 | */ |
33 | int radix_to_bin(const void *in, int radix, void *out, size_t* len) | |
33 | int radix_to_bin(const void *in, int radix, void *out, unsigned long *len) | |
34 | 34 | { |
35 | 35 | size_t l; |
36 | 36 | void* mpi; |
9 | 9 | #include "tomcrypt.h" |
10 | 10 | |
11 | 11 | /** |
12 | @file compare_testvecotr.c | |
12 | @file compare_testvector.c | |
13 | 13 | Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel |
14 | 14 | */ |
15 | 15 | |
48 | 48 | } |
49 | 49 | #endif |
50 | 50 | |
51 | /** | |
52 | Compare two test-vectors | |
53 | ||
54 | @param is The data as it is | |
55 | @param is_len The length of is | |
56 | @param should The data as it should | |
57 | @param should_len The length of should | |
58 | @param what The type of the data | |
59 | @param which The iteration count | |
60 | @return 0 on equality, -1 or 1 on difference | |
61 | */ | |
51 | 62 | int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which) |
52 | 63 | { |
53 | 64 | int res = 0; |
54 | 65 | if(is_len != should_len) |
55 | 66 | res = is_len > should_len ? -1 : 1; |
56 | 67 | else |
57 | res = XMEMCMP(is, should, MAX(is_len, should_len)); | |
68 | res = XMEMCMP(is, should, is_len); | |
58 | 69 | |
59 | 70 | #if defined(LTC_TEST) && defined(LTC_TEST_DBG) |
60 | 71 | if (res != 0) { |
127 | 127 | XFREE(extracted); |
128 | 128 | return err; |
129 | 129 | } |
130 | #if 0 | |
131 | { | |
132 | int j; | |
133 | printf("\nPRK: 0x"); | |
134 | for(j=0; j < hashsize; j++) { | |
135 | printf("%02x ", extracted[j]); | |
136 | } | |
137 | for(j=0; j < hashsize; j++) { | |
138 | printf("%02x ", extracted[j]); | |
139 | } | |
140 | } | |
141 | #endif | |
142 | 130 | err = hkdf_expand(hash_idx, info, infolen, extracted, hashsize, out, outlen); |
143 | 131 | zeromem(extracted, hashsize); |
144 | 132 | XFREE(extracted); |
31 | 31 | |
32 | 32 | /** |
33 | 33 | Encodes a Generalized time structure in DER format |
34 | @param utctime The UTC time structure to encode | |
35 | @param out The destination of the DER encoding of the UTC time structure | |
34 | @param gtime The GeneralizedTime structure to encode | |
35 | @param out The destination of the DER encoding of the GeneralizedTime structure | |
36 | 36 | @param outlen [in/out] The length of the DER encoding |
37 | 37 | @return CRYPT_OK if successful |
38 | 38 | */ |
17 | 17 | |
18 | 18 | /** |
19 | 19 | Gets length of DER encoding of GeneralizedTime |
20 | @param utctime The UTC time structure to get the size of | |
20 | @param gtime The GeneralizedTime structure to get the size of | |
21 | 21 | @param outlen [out] The length of the DER encoding |
22 | 22 | @return CRYPT_OK if successful |
23 | 23 | */ |
24 | 24 | int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, |
25 | 25 | unsigned long *outlen) |
26 | 26 | { |
27 | return der_length_sequence_ex(list, inlen, outlen, NULL); | |
27 | return der_length_sequence_ex(list, inlen, outlen, NULL); | |
28 | 28 | } |
29 | 29 | |
30 | 30 | int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen, |
77 | 77 | } |
78 | 78 | |
79 | 79 | /** |
80 | Import DH key parts pub and priv from raw numbers | |
80 | Import DH public or private key part from raw numbers | |
81 | 81 | |
82 | @param pub DH's pub (public key) (can be NULL if priv is valid) | |
83 | @param publen DH's pub's length | |
84 | @param priv DH's priv (private key) (can be NULL if pub is valid) | |
85 | @param privlen DH's priv's length | |
82 | NB: The p & g parts must be set beforehand | |
83 | ||
84 | @param in The key-part to import, either public or private. | |
85 | @param inlen The key-part's length | |
86 | @param type Which type of key (PK_PRIVATE or PK_PUBLIC) | |
86 | 87 | @param key [out] the destination for the imported key |
87 | 88 | @return CRYPT_OK if successful |
88 | 89 | */ |
89 | int dh_set_key(const unsigned char *pub, unsigned long publen, | |
90 | const unsigned char *priv, unsigned long privlen, | |
91 | dh_key *key) | |
90 | int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key) | |
92 | 91 | { |
93 | 92 | int err; |
94 | 93 | |
95 | 94 | LTC_ARGCHK(key != NULL); |
96 | 95 | LTC_ARGCHK(ltc_mp.name != NULL); |
97 | 96 | |
98 | if(priv == NULL) { | |
99 | if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; } | |
100 | key->type = PK_PUBLIC; | |
101 | mp_clear(key->x); | |
102 | key->x = NULL; | |
97 | if (type == PK_PRIVATE) { | |
98 | key->type = PK_PRIVATE; | |
99 | if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } | |
100 | if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; } | |
103 | 101 | } |
104 | 102 | else { |
105 | if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)priv, privlen)) != CRYPT_OK) { goto LBL_ERR; } | |
106 | if (pub != NULL) { | |
107 | if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)pub, publen)) != CRYPT_OK) { goto LBL_ERR; } | |
108 | } | |
109 | else { | |
110 | /* compute y value */ | |
111 | if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; } | |
112 | } | |
113 | key->type = PK_PRIVATE; | |
103 | key->type = PK_PUBLIC; | |
104 | if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } | |
114 | 105 | } |
115 | 106 | |
116 | 107 | /* check public key */ |
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
60 | Import DSA public or private key from raw numbers | |
61 | @param pub DSA's y (public key) in binary representation | |
62 | @param publen The length of pub | |
63 | @param priv DSA's x (private key) in binary representation (can be NULL when importing public key) | |
64 | @param privlen The length of priv | |
60 | Import DSA public or private key-part from raw numbers | |
61 | ||
62 | NB: The p, q & g parts must be set beforehand | |
63 | ||
64 | @param in The key-part to import, either public or private. | |
65 | @param inlen The key-part's length | |
66 | @param type Which type of key (PK_PRIVATE or PK_PUBLIC) | |
65 | 67 | @param key [out] the destination for the imported key |
66 | 68 | @return CRYPT_OK if successful. |
67 | 69 | */ |
68 | int dsa_set_key(const unsigned char *pub, unsigned long publen, | |
69 | const unsigned char *priv, unsigned long privlen, | |
70 | dsa_key *key) | |
70 | int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key) | |
71 | 71 | { |
72 | 72 | int err; |
73 | 73 | |
79 | 79 | LTC_ARGCHK(key->q != NULL); |
80 | 80 | LTC_ARGCHK(ltc_mp.name != NULL); |
81 | 81 | |
82 | if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)pub , publen)) != CRYPT_OK) { goto LBL_ERR; } | |
83 | if (priv != NULL) { | |
82 | if (type == PK_PRIVATE) { | |
84 | 83 | key->type = PK_PRIVATE; |
85 | if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)priv , privlen)) != CRYPT_OK) { goto LBL_ERR; } | |
84 | if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } | |
85 | if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; } | |
86 | 86 | } |
87 | 87 | else { |
88 | 88 | key->type = PK_PUBLIC; |
89 | if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } | |
89 | 90 | } |
90 | 91 | |
91 | 92 | return CRYPT_OK; |