1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
12 * All curves taken from NIST recommendation paper of July 1999
13 * Available at http://csrc.nist.gov/cryptval/dss.htm
18 @file ecc_shared_secret.c
19 ECC Crypto, Tom St Denis
25 Create an ECC shared secret between two keys
26 @param private_key The private ECC key
27 @param public_key The public key
28 @param out [out] Destination of the shared secret (Conforms to EC-DH from ANSI X9.63)
29 @param outlen [in/out] The max size and resulting size of the shared secret
30 @return CRYPT_OK if successful
32 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
33 unsigned char *out, unsigned long *outlen)
40 LTC_ARGCHK(private_key != NULL);
41 LTC_ARGCHK(public_key != NULL);
42 LTC_ARGCHK(out != NULL);
43 LTC_ARGCHK(outlen != NULL);
46 if (private_key->type != PK_PRIVATE) {
47 return CRYPT_PK_NOT_PRIVATE;
50 if (ltc_ecc_is_valid_idx(private_key->idx) == 0 || ltc_ecc_is_valid_idx(public_key->idx) == 0) {
51 return CRYPT_INVALID_ARG;
54 if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) {
55 return CRYPT_PK_TYPE_MISMATCH;
59 result = ltc_ecc_new_point();
64 if ((err = mp_init(&prime)) != CRYPT_OK) {
65 ltc_ecc_del_point(result);
69 if ((err = mp_read_radix(prime, (char *)private_key->dp->prime, 16)) != CRYPT_OK) { goto done; }
70 if ((err = ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1)) != CRYPT_OK) { goto done; }
72 x = (unsigned long)mp_unsigned_bin_size(prime);
75 err = CRYPT_BUFFER_OVERFLOW;
79 if ((err = mp_to_unsigned_bin(result->x, out + (x - mp_unsigned_bin_size(result->x)))) != CRYPT_OK) { goto done; }
85 ltc_ecc_del_point(result);
90 /* ref: $Format:%D$ */
91 /* git commit: $Format:%H$ */
92 /* commit time: $Format:%ai$ */