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_encrypt_key.c
19 ECC Crypto, Tom St Denis
25 Encrypt a symmetric key with ECC
26 @param in The symmetric key you want to encrypt
27 @param inlen The length of the key to encrypt (octets)
28 @param out [out] The destination for the ciphertext
29 @param outlen [in/out] The max size and resulting size of the ciphertext
30 @param prng An active PRNG state
31 @param wprng The index of the PRNG you wish to use
32 @param hash The index of the hash you want to use
33 @param key The ECC key you want to encrypt to
34 @return CRYPT_OK if successful
36 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
37 unsigned char *out, unsigned long *outlen,
38 prng_state *prng, int wprng, int hash,
41 unsigned char *pub_expt, *ecc_shared, *skey;
43 unsigned long x, y, pubkeysize;
46 LTC_ARGCHK(in != NULL);
47 LTC_ARGCHK(out != NULL);
48 LTC_ARGCHK(outlen != NULL);
49 LTC_ARGCHK(key != NULL);
51 /* check that wprng/cipher/hash are not invalid */
52 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
56 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
60 if (inlen > hash_descriptor[hash].hashsize) {
61 return CRYPT_INVALID_HASH;
64 /* make a random key and export the public copy */
65 if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
69 pub_expt = XMALLOC(ECC_BUF_SIZE);
70 ecc_shared = XMALLOC(ECC_BUF_SIZE);
71 skey = XMALLOC(MAXBLOCKSIZE);
72 if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
73 if (pub_expt != NULL) {
76 if (ecc_shared != NULL) {
86 pubkeysize = ECC_BUF_SIZE;
87 if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
94 if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
100 if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {
105 for (x = 0; x < inlen; x++) {
109 err = der_encode_sequence_multi(out, outlen,
110 LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,
111 LTC_ASN1_OCTET_STRING, pubkeysize, pub_expt,
112 LTC_ASN1_OCTET_STRING, inlen, skey,
113 LTC_ASN1_EOL, 0UL, NULL);
116 #ifdef LTC_CLEAN_STACK
118 zeromem(pub_expt, ECC_BUF_SIZE);
119 zeromem(ecc_shared, ECC_BUF_SIZE);
120 zeromem(skey, MAXBLOCKSIZE);
131 /* ref: $Format:%D$ */
132 /* git commit: $Format:%H$ */
133 /* commit time: $Format:%ai$ */