Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_encrypt_key.c
4396 views
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
10
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11
*
12
* All curves taken from NIST recommendation paper of July 1999
13
* Available at http://csrc.nist.gov/cryptval/dss.htm
14
*/
15
#include "tomcrypt.h"
16
17
/**
18
@file ecc_encrypt_key.c
19
ECC Crypto, Tom St Denis
20
*/
21
22
#ifdef LTC_MECC
23
24
/**
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
35
*/
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,
39
ecc_key *key)
40
{
41
unsigned char *pub_expt, *ecc_shared, *skey;
42
ecc_key pubkey;
43
unsigned long x, y, pubkeysize;
44
int err;
45
46
LTC_ARGCHK(in != NULL);
47
LTC_ARGCHK(out != NULL);
48
LTC_ARGCHK(outlen != NULL);
49
LTC_ARGCHK(key != NULL);
50
51
/* check that wprng/cipher/hash are not invalid */
52
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
53
return err;
54
}
55
56
if ((err = hash_is_valid(hash)) != CRYPT_OK) {
57
return err;
58
}
59
60
if (inlen > hash_descriptor[hash].hashsize) {
61
return CRYPT_INVALID_HASH;
62
}
63
64
/* make a random key and export the public copy */
65
if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {
66
return err;
67
}
68
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) {
74
XFREE(pub_expt);
75
}
76
if (ecc_shared != NULL) {
77
XFREE(ecc_shared);
78
}
79
if (skey != NULL) {
80
XFREE(skey);
81
}
82
ecc_free(&pubkey);
83
return CRYPT_MEM;
84
}
85
86
pubkeysize = ECC_BUF_SIZE;
87
if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
88
ecc_free(&pubkey);
89
goto LBL_ERR;
90
}
91
92
/* make random key */
93
x = ECC_BUF_SIZE;
94
if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
95
ecc_free(&pubkey);
96
goto LBL_ERR;
97
}
98
ecc_free(&pubkey);
99
y = MAXBLOCKSIZE;
100
if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {
101
goto LBL_ERR;
102
}
103
104
/* Encrypt key */
105
for (x = 0; x < inlen; x++) {
106
skey[x] ^= in[x];
107
}
108
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);
114
115
LBL_ERR:
116
#ifdef LTC_CLEAN_STACK
117
/* clean up */
118
zeromem(pub_expt, ECC_BUF_SIZE);
119
zeromem(ecc_shared, ECC_BUF_SIZE);
120
zeromem(skey, MAXBLOCKSIZE);
121
#endif
122
123
XFREE(skey);
124
XFREE(ecc_shared);
125
XFREE(pub_expt);
126
127
return err;
128
}
129
130
#endif
131
132