Codebase list libcryptx-perl / ff3726c
ltc 1.18-rc2 Karel Miko 6 years ago
13 changed file(s) with 57 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
152152 return CRYPT_OK;
153153 }
154154
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 */
156162 static int _chc_process(hash_state * md, const unsigned char *buf, unsigned long len);
157163 static HASH_PROCESS(_chc_process, chc_compress, chc, (unsigned long)cipher_blocksize)
158164
2525 #endif
2626
2727 /* version */
28 #define CRYPT 0x0117
29 #define SCRYPT "1.17"
28 #define CRYPT 0x0118
29 #define SCRYPT "1.18.0-rc2"
3030
3131 /* max size of either a cipher/hash block or symmetric key [largest of the two] */
3232 #define MAXBLOCKSIZE 128
2929 #define LTC_MILLER_RABIN_REPS 35
3030 #endif
3131
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);
3333
3434 /** math descriptor */
3535 typedef struct {
224224 int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
225225 int dh_set_pg_groupsize(int groupsize, dh_key *key);
226226
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);
230228 int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
231229
232230 int dh_shared_secret(dh_key *private_key, dh_key *public_key,
234232
235233 void dh_free(dh_key *key);
236234
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);
239236
240237 #ifdef LTC_SOURCE
241238 /* internal helper functions */
479476 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
480477 int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
481478
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);
485480 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
486481
487482 void dsa_free(dsa_key *key);
3030
3131 @return CRYPT_OK on success.
3232 */
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)
3434 {
3535 size_t l;
3636 void* mpi;
99 #include "tomcrypt.h"
1010
1111 /**
12 @file compare_testvecotr.c
12 @file compare_testvector.c
1313 Function to compare two testvectors and print a (detailed) error-message if required, Steffen Jaeckel
1414 */
1515
4848 }
4949 #endif
5050
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 */
5162 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which)
5263 {
5364 int res = 0;
5465 if(is_len != should_len)
5566 res = is_len > should_len ? -1 : 1;
5667 else
57 res = XMEMCMP(is, should, MAX(is_len, should_len));
68 res = XMEMCMP(is, should, is_len);
5869
5970 #if defined(LTC_TEST) && defined(LTC_TEST_DBG)
6071 if (res != 0) {
127127 XFREE(extracted);
128128 return err;
129129 }
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
142130 err = hkdf_expand(hash_idx, info, infolen, extracted, hashsize, out, outlen);
143131 zeromem(extracted, hashsize);
144132 XFREE(extracted);
3131
3232 /**
3333 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
3636 @param outlen [in/out] The length of the DER encoding
3737 @return CRYPT_OK if successful
3838 */
1717
1818 /**
1919 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
2121 @param outlen [out] The length of the DER encoding
2222 @return CRYPT_OK if successful
2323 */
2424 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
2525 unsigned long *outlen)
2626 {
27 return der_length_sequence_ex(list, inlen, outlen, NULL);
27 return der_length_sequence_ex(list, inlen, outlen, NULL);
2828 }
2929
3030 int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
7777 }
7878
7979 /**
80 Import DH key parts pub and priv from raw numbers
80 Import DH public or private key part from raw numbers
8181
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)
8687 @param key [out] the destination for the imported key
8788 @return CRYPT_OK if successful
8889 */
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)
9291 {
9392 int err;
9493
9594 LTC_ARGCHK(key != NULL);
9695 LTC_ARGCHK(ltc_mp.name != NULL);
9796
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; }
103101 }
104102 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; }
114105 }
115106
116107 /* check public key */
5757 }
5858
5959 /**
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)
6567 @param key [out] the destination for the imported key
6668 @return CRYPT_OK if successful.
6769 */
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)
7171 {
7272 int err;
7373
7979 LTC_ARGCHK(key->q != NULL);
8080 LTC_ARGCHK(ltc_mp.name != NULL);
8181
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) {
8483 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; }
8686 }
8787 else {
8888 key->type = PK_PUBLIC;
89 if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
8990 }
9091
9192 return CRYPT_OK;
9696 err = CRYPT_OK;
9797 goto cleanup;
9898 errkey:
99 mp_clear_multi(key->q, key->p, key->qP, key->dP, key->dQ, key->N, key->d, key->e, NULL);
99 rsa_free(key);
100100 cleanup:
101101 mp_clear_multi(tmp3, tmp2, tmp1, q, p, NULL);
102102 return err;