comparison libtomcrypt/src/pk/ecc/ecc_shared_secret.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
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 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */
11
12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
13 *
14 * All curves taken from NIST recommendation paper of July 1999
15 * Available at http://csrc.nist.gov/cryptval/dss.htm
16 */
17 #include "tomcrypt.h"
18
19 /**
20 @file ecc_shared_secret.c
21 ECC Crypto, Tom St Denis
22 */
23
24 #ifdef MECC
25
26 /**
27 Create an ECC shared secret between two keys
28 @param private_key The private ECC key
29 @param public_key The public key
30 @param out [out] Destination of the shared secret (Conforms to EC-DH from ANSI X9.63)
31 @param outlen [in/out] The max size and resulting size of the shared secret
32 @return CRYPT_OK if successful
33 */
34 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
35 unsigned char *out, unsigned long *outlen)
36 {
37 unsigned long x;
38 ecc_point *result;
39 void *prime;
40 int err;
41
42 LTC_ARGCHK(private_key != NULL);
43 LTC_ARGCHK(public_key != NULL);
44 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(outlen != NULL);
46
47 /* type valid? */
48 if (private_key->type != PK_PRIVATE) {
49 return CRYPT_PK_NOT_PRIVATE;
50 }
51
52 if (ltc_ecc_is_valid_idx(private_key->idx) == 0 || ltc_ecc_is_valid_idx(public_key->idx) == 0) {
53 return CRYPT_INVALID_ARG;
54 }
55
56 if (XSTRCMP(private_key->dp->name, public_key->dp->name) != 0) {
57 return CRYPT_PK_TYPE_MISMATCH;
58 }
59
60 /* make new point */
61 result = ltc_ecc_new_point();
62 if (result == NULL) {
63 return CRYPT_MEM;
64 }
65
66 if ((err = mp_init(&prime)) != CRYPT_OK) {
67 ltc_ecc_del_point(result);
68 return err;
69 }
70
71 if ((err = mp_read_radix(prime, (char *)private_key->dp->prime, 16)) != CRYPT_OK) { goto done; }
72 if ((err = ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, prime, 1)) != CRYPT_OK) { goto done; }
73
74 x = (unsigned long)mp_unsigned_bin_size(prime);
75 if (*outlen < x) {
76 *outlen = x;
77 err = CRYPT_BUFFER_OVERFLOW;
78 goto done;
79 }
80 zeromem(out, x);
81 if ((err = mp_to_unsigned_bin(result->x, out + (x - mp_unsigned_bin_size(result->x)))) != CRYPT_OK) { goto done; }
82
83 err = CRYPT_OK;
84 *outlen = x;
85 done:
86 mp_clear(prime);
87 ltc_ecc_del_point(result);
88 return err;
89 }
90
91 #endif
92 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_shared_secret.c,v $ */
93 /* $Revision: 1.8 $ */
94 /* $Date: 2006/12/04 02:19:48 $ */
95