Path: blob/master/libs/tomcrypt/src/pk/dsa/dsa_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*/8#include "tomcrypt.h"910/**11@file dsa_encrypt_key.c12DSA Crypto, Tom St Denis13*/1415#ifdef LTC_MDSA1617/**18Encrypt a symmetric key with DSA19@param in The symmetric key you want to encrypt20@param inlen The length of the key to encrypt (octets)21@param out [out] The destination for the ciphertext22@param outlen [in/out] The max size and resulting size of the ciphertext23@param prng An active PRNG state24@param wprng The index of the PRNG you wish to use25@param hash The index of the hash you want to use26@param key The DSA key you want to encrypt to27@return CRYPT_OK if successful28*/29int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,30unsigned char *out, unsigned long *outlen,31prng_state *prng, int wprng, int hash,32dsa_key *key)33{34unsigned char *expt, *skey;35void *g_pub, *g_priv;36unsigned long x, y;37int err;3839LTC_ARGCHK(in != NULL);40LTC_ARGCHK(out != NULL);41LTC_ARGCHK(outlen != NULL);42LTC_ARGCHK(key != NULL);4344/* check that wprng/cipher/hash are not invalid */45if ((err = prng_is_valid(wprng)) != CRYPT_OK) {46return err;47}4849if ((err = hash_is_valid(hash)) != CRYPT_OK) {50return err;51}5253if (inlen > hash_descriptor[hash].hashsize) {54return CRYPT_INVALID_HASH;55}5657/* make a random key and export the public copy */58if ((err = mp_init_multi(&g_pub, &g_priv, NULL)) != CRYPT_OK) {59return err;60}6162expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1);63skey = XMALLOC(MAXBLOCKSIZE);64if (expt == NULL || skey == NULL) {65if (expt != NULL) {66XFREE(expt);67}68if (skey != NULL) {69XFREE(skey);70}71mp_clear_multi(g_pub, g_priv, NULL);72return CRYPT_MEM;73}7475/* make a random g_priv, g_pub = g^x pair76private key x should be in range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2)77*/78if ((err = rand_bn_upto(g_priv, key->q, prng, wprng)) != CRYPT_OK) {79goto LBL_ERR;80}8182/* compute y */83if ((err = mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) {84goto LBL_ERR;85}8687/* make random key */88x = mp_unsigned_bin_size(key->p) + 1;89if ((err = dsa_shared_secret(g_priv, key->y, key, expt, &x)) != CRYPT_OK) {90goto LBL_ERR;91}9293y = MAXBLOCKSIZE;94if ((err = hash_memory(hash, expt, x, skey, &y)) != CRYPT_OK) {95goto LBL_ERR;96}9798/* Encrypt key */99for (x = 0; x < inlen; x++) {100skey[x] ^= in[x];101}102103err = der_encode_sequence_multi(out, outlen,104LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,105LTC_ASN1_INTEGER, 1UL, g_pub,106LTC_ASN1_OCTET_STRING, inlen, skey,107LTC_ASN1_EOL, 0UL, NULL);108109LBL_ERR:110#ifdef LTC_CLEAN_STACK111/* clean up */112zeromem(expt, mp_unsigned_bin_size(key->p) + 1);113zeromem(skey, MAXBLOCKSIZE);114#endif115116XFREE(skey);117XFREE(expt);118119mp_clear_multi(g_pub, g_priv, NULL);120return err;121}122123#endif124125126