1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10 #include "tomcrypt_private.h"
11
12 /**
13 @file dsa_encrypt_key.c
14 DSA Crypto, Tom St Denis
15 */
16
17 #ifdef LTC_MDSA
18
19 /**
20 Encrypt a symmetric key with DSA
21 @param in The symmetric key you want to encrypt
22 @param inlen The length of the key to encrypt (octets)
23 @param out [out] The destination for the ciphertext
24 @param outlen [in/out] The max size and resulting size of the ciphertext
25 @param prng An active PRNG state
26 @param wprng The index of the PRNG you wish to use
27 @param hash The index of the hash you want to use
28 @param key The DSA key you want to encrypt to
29 @return CRYPT_OK if successful
30 */
dsa_encrypt_key(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen,prng_state * prng,int wprng,int hash,const dsa_key * key)31 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
32 unsigned char *out, unsigned long *outlen,
33 prng_state *prng, int wprng, int hash,
34 const dsa_key *key)
35 {
36 unsigned char *expt, *skey;
37 void *g_pub, *g_priv;
38 unsigned long x, y;
39 int err;
40
41 LTC_ARGCHK(in != NULL);
42 LTC_ARGCHK(out != NULL);
43 LTC_ARGCHK(outlen != NULL);
44 LTC_ARGCHK(key != NULL);
45
46 /* check that wprng/cipher/hash are not invalid */
47 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
48 return err;
49 }
50
51 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
52 return err;
53 }
54
55 if (inlen > hash_descriptor[hash]->hashsize) {
56 return CRYPT_INVALID_HASH;
57 }
58
59 /* make a random key and export the public copy */
60 if ((err = mp_init_multi(&g_pub, &g_priv, NULL)) != CRYPT_OK) {
61 return err;
62 }
63
64 expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1);
65 skey = XMALLOC(MAXBLOCKSIZE);
66 if (expt == NULL || skey == NULL) {
67 if (expt != NULL) {
68 XFREE(expt);
69 }
70 if (skey != NULL) {
71 XFREE(skey);
72 }
73 mp_clear_multi(g_pub, g_priv, NULL);
74 return CRYPT_MEM;
75 }
76
77 /* make a random g_priv, g_pub = g^x pair
78 private key x should be in range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2)
79 */
80 if ((err = rand_bn_upto(g_priv, key->q, prng, wprng)) != CRYPT_OK) {
81 goto LBL_ERR;
82 }
83
84 /* compute y */
85 if ((err = mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) {
86 goto LBL_ERR;
87 }
88
89 /* make random key */
90 x = mp_unsigned_bin_size(key->p) + 1;
91 if ((err = dsa_shared_secret(g_priv, key->y, key, expt, &x)) != CRYPT_OK) {
92 goto LBL_ERR;
93 }
94
95 y = MAXBLOCKSIZE;
96 if ((err = hash_memory(hash, expt, x, skey, &y)) != CRYPT_OK) {
97 goto LBL_ERR;
98 }
99
100 /* Encrypt key */
101 for (x = 0; x < inlen; x++) {
102 skey[x] ^= in[x];
103 }
104
105 err = der_encode_sequence_multi(out, outlen,
106 LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash]->OIDlen, hash_descriptor[hash]->OID,
107 LTC_ASN1_INTEGER, 1UL, g_pub,
108 LTC_ASN1_OCTET_STRING, inlen, skey,
109 LTC_ASN1_EOL, 0UL, NULL);
110
111 LBL_ERR:
112 #ifdef LTC_CLEAN_STACK
113 /* clean up */
114 zeromem(expt, mp_unsigned_bin_size(key->p) + 1);
115 zeromem(skey, MAXBLOCKSIZE);
116 #endif
117
118 XFREE(skey);
119 XFREE(expt);
120
121 mp_clear_multi(g_pub, g_priv, NULL);
122 return err;
123 }
124
125 #endif
126 /* ref: $Format:%D$ */
127 /* git commit: $Format:%H$ */
128 /* commit time: $Format:%ai$ */
129
130