]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ecc_shared_secret.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ecc_shared_secret.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11  *
12  * All curves taken from NIST recommendation paper of July 1999
13  * Available at http://csrc.nist.gov/cryptval/dss.htm
14  */
15 #include "tomcrypt.h"
16
17 /**
18   @file ecc_shared_secret.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
24 /**
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
31 */
32 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
33                       unsigned char *out, unsigned long *outlen)
34 {
35    unsigned long  x;
36    ecc_point     *result;
37    void          *prime;
38    int            err;
39
40    LTC_ARGCHK(private_key != NULL);
41    LTC_ARGCHK(public_key  != NULL);
42    LTC_ARGCHK(out         != NULL);
43    LTC_ARGCHK(outlen      != NULL);
44
45    /* type valid? */
46    if (private_key->type != PK_PRIVATE) {
47       return CRYPT_PK_NOT_PRIVATE;
48    }
49
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;
52    }
53
54    if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) {
55       return CRYPT_PK_TYPE_MISMATCH;
56    }
57
58    /* make new point */
59    result = ltc_ecc_new_point();
60    if (result == NULL) {
61       return CRYPT_MEM;
62    }
63
64    if ((err = mp_init(&prime)) != CRYPT_OK) {
65       ltc_ecc_del_point(result);
66       return err;
67    }
68
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; }
71
72    x = (unsigned long)mp_unsigned_bin_size(prime);
73    if (*outlen < x) {
74       *outlen = x;
75       err = CRYPT_BUFFER_OVERFLOW;
76       goto done;
77    }
78    zeromem(out, x);
79    if ((err = mp_to_unsigned_bin(result->x, out + (x - mp_unsigned_bin_size(result->x))))   != CRYPT_OK)           { goto done; }
80
81    err     = CRYPT_OK;
82    *outlen = x;
83 done:
84    mp_clear(prime);
85    ltc_ecc_del_point(result);
86    return err;
87 }
88
89 #endif
90 /* ref:         $Format:%D$ */
91 /* git commit:  $Format:%H$ */
92 /* commit time: $Format:%ai$ */
93