Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_encrypt_key.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b10*11* All curves taken from NIST recommendation paper of July 199912* Available at http://csrc.nist.gov/cryptval/dss.htm13*/14#include "tomcrypt.h"1516/**17@file ecc_encrypt_key.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC2223/**24Encrypt a symmetric key with ECC25@param in The symmetric key you want to encrypt26@param inlen The length of the key to encrypt (octets)27@param out [out] The destination for the ciphertext28@param outlen [in/out] The max size and resulting size of the ciphertext29@param prng An active PRNG state30@param wprng The index of the PRNG you wish to use31@param hash The index of the hash you want to use32@param key The ECC key you want to encrypt to33@return CRYPT_OK if successful34*/35int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,36unsigned char *out, unsigned long *outlen,37prng_state *prng, int wprng, int hash,38ecc_key *key)39{40unsigned char *pub_expt, *ecc_shared, *skey;41ecc_key pubkey;42unsigned long x, y, pubkeysize;43int err;4445LTC_ARGCHK(in != NULL);46LTC_ARGCHK(out != NULL);47LTC_ARGCHK(outlen != NULL);48LTC_ARGCHK(key != NULL);4950/* check that wprng/cipher/hash are not invalid */51if ((err = prng_is_valid(wprng)) != CRYPT_OK) {52return err;53}5455if ((err = hash_is_valid(hash)) != CRYPT_OK) {56return err;57}5859if (inlen > hash_descriptor[hash].hashsize) {60return CRYPT_INVALID_HASH;61}6263/* make a random key and export the public copy */64if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {65return err;66}6768pub_expt = XMALLOC(ECC_BUF_SIZE);69ecc_shared = XMALLOC(ECC_BUF_SIZE);70skey = XMALLOC(MAXBLOCKSIZE);71if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {72if (pub_expt != NULL) {73XFREE(pub_expt);74}75if (ecc_shared != NULL) {76XFREE(ecc_shared);77}78if (skey != NULL) {79XFREE(skey);80}81ecc_free(&pubkey);82return CRYPT_MEM;83}8485pubkeysize = ECC_BUF_SIZE;86if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {87ecc_free(&pubkey);88goto LBL_ERR;89}9091/* make random key */92x = ECC_BUF_SIZE;93if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {94ecc_free(&pubkey);95goto LBL_ERR;96}97ecc_free(&pubkey);98y = MAXBLOCKSIZE;99if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {100goto LBL_ERR;101}102103/* Encrypt key */104for (x = 0; x < inlen; x++) {105skey[x] ^= in[x];106}107108err = der_encode_sequence_multi(out, outlen,109LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,110LTC_ASN1_OCTET_STRING, pubkeysize, pub_expt,111LTC_ASN1_OCTET_STRING, inlen, skey,112LTC_ASN1_EOL, 0UL, NULL);113114LBL_ERR:115#ifdef LTC_CLEAN_STACK116/* clean up */117zeromem(pub_expt, ECC_BUF_SIZE);118zeromem(ecc_shared, ECC_BUF_SIZE);119zeromem(skey, MAXBLOCKSIZE);120#endif121122XFREE(skey);123XFREE(ecc_shared);124XFREE(pub_expt);125126return err;127}128129#endif130131132