comparison libtomcrypt/src/pk/ecc/ecc_encrypt_key.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 ac2158e3e403
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_encrypt_key.c
21 ECC Crypto, Tom St Denis
22 */
23
24 #ifdef MECC
25
26 /**
27 Encrypt a symmetric key with ECC
28 @param in The symmetric key you want to encrypt
29 @param inlen The length of the key to encrypt (octets)
30 @param out [out] The destination for the ciphertext
31 @param outlen [in/out] The max size and resulting size of the ciphertext
32 @param prng An active PRNG state
33 @param wprng The index of the PRNG you wish to use
34 @param hash The index of the hash you want to use
35 @param key The ECC key you want to encrypt to
36 @return CRYPT_OK if successful
37 */
38 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
39 unsigned char *out, unsigned long *outlen,
40 prng_state *prng, int wprng, int hash,
41 ecc_key *key)
42 {
43 unsigned char *pub_expt, *ecc_shared, *skey;
44 ecc_key pubkey;
45 unsigned long x, y, pubkeysize;
46 int err;
47
48 LTC_ARGCHK(in != NULL);
49 LTC_ARGCHK(out != NULL);
50 LTC_ARGCHK(outlen != NULL);
51 LTC_ARGCHK(key != NULL);
52
53 /* check that wprng/cipher/hash are not invalid */
54 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
55 return err;
56 }
57
58 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
59 return err;
60 }
61
62 if (inlen > hash_descriptor[hash].hashsize) {
63 return CRYPT_INVALID_HASH;
64 }
65
66 /* make a random key and export the public copy */
67 if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
68 return err;
69 }
70
71 pub_expt = XMALLOC(ECC_BUF_SIZE);
72 ecc_shared = XMALLOC(ECC_BUF_SIZE);
73 skey = XMALLOC(MAXBLOCKSIZE);
74 if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
75 if (pub_expt != NULL) {
76 XFREE(pub_expt);
77 }
78 if (ecc_shared != NULL) {
79 XFREE(ecc_shared);
80 }
81 if (skey != NULL) {
82 XFREE(skey);
83 }
84 ecc_free(&pubkey);
85 return CRYPT_MEM;
86 }
87
88 pubkeysize = ECC_BUF_SIZE;
89 if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
90 ecc_free(&pubkey);
91 goto LBL_ERR;
92 }
93
94 /* make random key */
95 x = ECC_BUF_SIZE;
96 if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
97 ecc_free(&pubkey);
98 goto LBL_ERR;
99 }
100 ecc_free(&pubkey);
101 y = MAXBLOCKSIZE;
102 if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {
103 goto LBL_ERR;
104 }
105
106 /* Encrypt key */
107 for (x = 0; x < inlen; x++) {
108 skey[x] ^= in[x];
109 }
110
111 err = der_encode_sequence_multi(out, outlen,
112 LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,
113 LTC_ASN1_OCTET_STRING, pubkeysize, pub_expt,
114 LTC_ASN1_OCTET_STRING, inlen, skey,
115 LTC_ASN1_EOL, 0UL, NULL);
116
117 LBL_ERR:
118 #ifdef LTC_CLEAN_STACK
119 /* clean up */
120 zeromem(pub_expt, ECC_BUF_SIZE);
121 zeromem(ecc_shared, ECC_BUF_SIZE);
122 zeromem(skey, MAXBLOCKSIZE);
123 #endif
124
125 XFREE(skey);
126 XFREE(ecc_shared);
127 XFREE(pub_expt);
128
129 return err;
130 }
131
132 #endif
133 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_encrypt_key.c,v $ */
134 /* $Revision: 1.4 $ */
135 /* $Date: 2006/11/21 00:10:18 $ */
136