annotate src/pk/ecc/ecc_sys.c @ 212:a3e31d3983cb libtomcrypt

merge of 24ccd6df75086f1028c185f4ad5946f449bf2e05 and cc138b6aed8d554f52f041a11afecb95e95ed3b8
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 13:24:05 +0000
parents 39d5d58461d6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 * LibTomCrypt is a library that provides various cryptographic
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 * algorithms in a highly modular and flexible manner.
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * The library is free for all purposes without any express
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 * guarantee it works.
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 *
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * Tom St Denis, [email protected], http://libtomcrypt.org
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 @file ecc_sys.c
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 ECC Crypto, Tom St Denis
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 Encrypt a symmetric key with ECC
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 @param in The symmetric key you want to encrypt
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 @param inlen The length of the key to encrypt (octets)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 @param out [out] The destination for the ciphertext
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 @param outlen [in/out] The max size and resulting size of the ciphertext
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 @param prng An active PRNG state
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 @param wprng The index of the PRNG you wish to use
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 @param hash The index of the hash you want to use
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @param key The ECC key you want to encrypt to
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 @return CRYPT_OK if successful
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 unsigned char *out, unsigned long *outlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 prng_state *prng, int wprng, int hash,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 ecc_key *key)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 unsigned char *pub_expt, *ecc_shared, *skey;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 ecc_key pubkey;
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
36 unsigned long x, y, pubkeysize;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 int err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 LTC_ARGCHK(in != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 LTC_ARGCHK(out != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 LTC_ARGCHK(outlen != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 LTC_ARGCHK(key != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 /* check that wprng/cipher/hash are not invalid */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if (inlen > hash_descriptor[hash].hashsize) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 return CRYPT_INVALID_HASH;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 /* make a random key and export the public copy */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 if ((err = ecc_make_key(prng, wprng, ecc_get_size(key), &pubkey)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 pub_expt = XMALLOC(ECC_BUF_SIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 ecc_shared = XMALLOC(ECC_BUF_SIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 skey = XMALLOC(MAXBLOCKSIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if (pub_expt != NULL) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 XFREE(pub_expt);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 if (ecc_shared != NULL) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 XFREE(ecc_shared);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 if (skey != NULL) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 XFREE(skey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 return CRYPT_MEM;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 pubkeysize = ECC_BUF_SIZE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if ((err = ecc_export(pub_expt, &pubkeysize, PK_PUBLIC, &pubkey)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 /* make random key */
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
86 x = ECC_BUF_SIZE;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 if ((err = ecc_shared_secret(&pubkey, key, ecc_shared, &x)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 ecc_free(&pubkey);
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
92 y = MAXBLOCKSIZE;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
93 if ((err = hash_memory(hash, ecc_shared, x, skey, &y)) != CRYPT_OK) {
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
97 /* Encrypt key */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
98 for (x = 0; x < inlen; x++) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
99 skey[x] ^= in[x];
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
102 err = der_encode_sequence_multi(out, outlen,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
103 LTC_ASN1_OBJECT_IDENTIFIER, hash_descriptor[hash].OIDlen, hash_descriptor[hash].OID,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
104 LTC_ASN1_OCTET_STRING, pubkeysize, pub_expt,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
105 LTC_ASN1_OCTET_STRING, inlen, skey,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
106 LTC_ASN1_EOL, 0UL, NULL);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 LBL_ERR:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 #ifdef LTC_CLEAN_STACK
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 /* clean up */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 zeromem(pub_expt, ECC_BUF_SIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 zeromem(ecc_shared, ECC_BUF_SIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 zeromem(skey, MAXBLOCKSIZE);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #endif
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 XFREE(skey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 XFREE(ecc_shared);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 XFREE(pub_expt);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 Decrypt an ECC encrypted key
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 @param in The ciphertext
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 @param inlen The length of the ciphertext (octets)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 @param out [out] The plaintext
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 @param outlen [in/out] The max size and resulting size of the plaintext
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 @param key The corresponding private ECC key
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 @return CRYPT_OK if successful
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 unsigned char *out, unsigned long *outlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 ecc_key *key)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 {
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
136 unsigned char *ecc_shared, *skey, *pub_expt;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
137 unsigned long x, y, hashOID[32];
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 int hash, err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 ecc_key pubkey;
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
140 ltc_asn1_list decode[3];
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 LTC_ARGCHK(in != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 LTC_ARGCHK(out != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 LTC_ARGCHK(outlen != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 LTC_ARGCHK(key != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 /* right key type? */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 if (key->type != PK_PRIVATE) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 return CRYPT_PK_NOT_PRIVATE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
152 /* decode to find out hash */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
153 LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
154
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
155 if ((err = der_decode_sequence(in, inlen, decode, 1)) != CRYPT_OK) {
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 }
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
158 for (hash = 0; hash_descriptor[hash].name != NULL &&
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
159 (hash_descriptor[hash].OIDlen != decode[0].size ||
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
160 memcmp(hash_descriptor[hash].OID, hashOID, sizeof(unsigned long)*decode[0].size)); hash++);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
162 if (hash_descriptor[hash].name == NULL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
163 return CRYPT_INVALID_PACKET;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
166 /* we now have the hash! */
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 /* allocate memory */
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
169 pub_expt = XMALLOC(ECC_BUF_SIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
170 ecc_shared = XMALLOC(ECC_BUF_SIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
171 skey = XMALLOC(MAXBLOCKSIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
172 if (pub_expt == NULL || ecc_shared == NULL || skey == NULL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
173 if (pub_expt != NULL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
174 XFREE(pub_expt);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
175 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
176 if (ecc_shared != NULL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
177 XFREE(ecc_shared);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 if (skey != NULL) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 XFREE(skey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182 return CRYPT_MEM;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 }
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
184 LTC_SET_ASN1(decode, 1, LTC_ASN1_OCTET_STRING, pub_expt, ECC_BUF_SIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
185 LTC_SET_ASN1(decode, 2, LTC_ASN1_OCTET_STRING, skey, MAXBLOCKSIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
186
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
187 /* read the structure in now */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
188 if ((err = der_decode_sequence(in, inlen, decode, 3)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
189 goto LBL_ERR;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
190 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
191
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
192 /* import ECC key from packet */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
193 if ((err = ecc_import(decode[1].data, decode[1].size, &pubkey)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
194 goto LBL_ERR;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
195 }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 /* make shared key */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 x = ECC_BUF_SIZE;
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
199 if ((err = ecc_shared_secret(key, &pubkey, ecc_shared, &x)) != CRYPT_OK) {
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
205 y = MAXBLOCKSIZE;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
206 if ((err = hash_memory(hash, ecc_shared, x, ecc_shared, &y)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
207 goto LBL_ERR;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
208 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
210 /* ensure the hash of the shared secret is at least as big as the encrypt itself */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
211 if (decode[2].size > y) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
212 err = CRYPT_INVALID_PACKET;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
216 /* avoid buffer overflow */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
217 if (*outlen < decode[2].size) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
218 err = CRYPT_BUFFER_OVERFLOW;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 /* Decrypt the key */
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
223 for (x = 0; x < decode[2].size; x++) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
224 out[x] = skey[x] ^ ecc_shared[x];
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 }
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
226 *outlen = x;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
228 err = CRYPT_OK;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
229 LBL_ERR:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
230 #ifdef LTC_CLEAN_STACK
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
231 zeromem(pub_expt, ECC_BUF_SIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
232 zeromem(ecc_shared, ECC_BUF_SIZE);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
233 zeromem(skey, MAXBLOCKSIZE);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 #endif
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
236 XFREE(pub_expt);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
237 XFREE(ecc_shared);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
238 XFREE(skey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
239
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
240 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
241 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
242
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 Sign a message digest
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 @param in The message digest to sign
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 @param inlen The length of the digest
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 @param out [out] The destination for the signature
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 @param outlen [in/out] The max size and resulting size of the signature
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 @param prng An active PRNG state
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250 @param wprng The index of the PRNG you wish to use
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 @param key A private ECC key
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 @return CRYPT_OK if successful
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 unsigned char *out, unsigned long *outlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 prng_state *prng, int wprng, ecc_key *key)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257 {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 ecc_key pubkey;
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
259 mp_int r, s, e, p;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 int err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 LTC_ARGCHK(in != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 LTC_ARGCHK(out != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 LTC_ARGCHK(outlen != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 LTC_ARGCHK(key != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 /* is this a private key? */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 if (key->type != PK_PRIVATE) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 return CRYPT_PK_NOT_PRIVATE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 /* is the IDX valid ? */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 if (is_valid_idx(key->idx) != 1) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 return CRYPT_PK_INVALID_TYPE;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
281 /* get the hash and load it as a bignum into 'e' */
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 /* init the bignums */
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
283 if ((err = mp_init_multi(&r, &s, &p, &e, NULL)) != MP_OKAY) {
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 err = mpi_to_ltc_error(err);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 if ((err = mp_read_radix(&p, (char *)sets[key->idx].order, 64)) != MP_OKAY) { goto error; }
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
289 if ((err = mp_read_unsigned_bin(&e, (unsigned char *)in, (int)inlen)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
291 /* make up a key and export the public copy */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
292 for (;;) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
293 if ((err = ecc_make_key(prng, wprng, ecc_get_size(key), &pubkey)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
294 return err;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
295 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
296
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
297 /* find r = x1 mod n */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
298 if ((err = mp_mod(&pubkey.pubkey.x, &p, &r)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
300 if (mp_iszero(&r)) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
301 ecc_free(&pubkey);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
302 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
303 /* find s = (e + xr)/k */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
304 if ((err = mp_invmod(&pubkey.k, &p, &pubkey.k)) != MP_OKAY) { goto error; } /* k = 1/k */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
305 if ((err = mp_mulmod(&key->k, &r, &p, &s)) != MP_OKAY) { goto error; } /* s = xr */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
306 if ((err = mp_addmod(&e, &s, &p, &s)) != MP_OKAY) { goto error; } /* s = e + xr */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
307 if ((err = mp_mulmod(&s, &pubkey.k, &p, &s)) != MP_OKAY) { goto error; } /* s = (e + xr)/k */
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
309 if (mp_iszero(&s)) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
310 ecc_free(&pubkey);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
311 } else {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
312 break;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
313 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
314 }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
317 /* store as SEQUENCE { r, s -- integer } */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
318 err = der_encode_sequence_multi(out, outlen,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
319 LTC_ASN1_INTEGER, 1UL, &r,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
320 LTC_ASN1_INTEGER, 1UL, &s,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
321 LTC_ASN1_EOL, 0UL, NULL);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 goto LBL_ERR;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 error:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 err = mpi_to_ltc_error(err);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 LBL_ERR:
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
326 mp_clear_multi(&r, &s, &p, &e, NULL);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 ecc_free(&pubkey);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
332 /* verify
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 *
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
334 * w = s^-1 mod n
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
335 * u1 = xw
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
336 * u2 = rw
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
337 * X = u1*G + u2*Q
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
338 * v = X_x1 mod n
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
339 * accept if v == r
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342 /**
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 Verify an ECC signature
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 @param sig The signature to verify
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 @param siglen The length of the signature (octets)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346 @param hash The hash (message digest) that was signed
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 @param hashlen The length of the hash (octets)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 @param stat Result of signature, 1==valid, 0==invalid
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 @param key The corresponding public ECC key
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350 @return CRYPT_OK if successful (even if the signature is not valid)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 const unsigned char *hash, unsigned long hashlen,
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 int *stat, ecc_key *key)
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355 {
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
356 ecc_point *mG, *mQ;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
357 mp_int r, s, v, w, u1, u2, e, p, m;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
358 mp_digit mp;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 int err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361 LTC_ARGCHK(sig != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 LTC_ARGCHK(hash != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 LTC_ARGCHK(stat != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364 LTC_ARGCHK(key != NULL);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 /* default to invalid signature */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 *stat = 0;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
369 /* is the IDX valid ? */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
370 if (is_valid_idx(key->idx) != 1) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
371 return CRYPT_PK_INVALID_TYPE;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
372 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
373
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
374 /* allocate ints */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
375 if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &p, &e, &m, NULL)) != MP_OKAY) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
376 return CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
377 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
378
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
379 /* allocate points */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
380 mG = new_point();
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
381 mQ = new_point();
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
382 if (mQ == NULL || mG == NULL) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
383 err = CRYPT_MEM;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
384 goto done;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
385 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
386
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
387 /* parse header */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
388 if ((err = der_decode_sequence_multi(sig, siglen,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
389 LTC_ASN1_INTEGER, 1UL, &r,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
390 LTC_ASN1_INTEGER, 1UL, &s,
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
391 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
392 goto done;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
393 }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
394
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
395 /* get the order */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
396 if ((err = mp_read_radix(&p, (char *)sets[key->idx].order, 64)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
397
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
398 /* get the modulus */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
399 if ((err = mp_read_radix(&m, (char *)sets[key->idx].prime, 64)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
400
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
401 /* check for zero */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
402 if (mp_iszero(&r) || mp_iszero(&s) || mp_cmp(&r, &p) != MP_LT || mp_cmp(&s, &p) != MP_LT) {
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
403 err = CRYPT_INVALID_PACKET;
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
404 goto done;
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
407 /* read hash */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
408 if ((err = mp_read_unsigned_bin(&e, (unsigned char *)hash, (int)hashlen)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
410 /* w = s^-1 mod n */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
411 if ((err = mp_invmod(&s, &p, &w)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
413 /* u1 = ew */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
414 if ((err = mp_mulmod(&e, &w, &p, &u1)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
416 /* u2 = rw */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
417 if ((err = mp_mulmod(&r, &w, &p, &u2)) != MP_OKAY) { goto error; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
419 /* find mG = u1*G */
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 if ((err = mp_read_radix(&mG->x, (char *)sets[key->idx].Gx, 64)) != MP_OKAY) { goto error; }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421 if ((err = mp_read_radix(&mG->y, (char *)sets[key->idx].Gy, 64)) != MP_OKAY) { goto error; }
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
422 mp_set(&mG->z, 1);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
423 if ((err = ecc_mulmod(&u1, mG, mG, &m, 0)) != CRYPT_OK) { goto done; }
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
425 /* find mQ = u2*Q */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
426 if ((err = mp_copy(&key->pubkey.x, &mQ->x)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
427 if ((err = mp_copy(&key->pubkey.y, &mQ->y)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
428 if ((err = mp_copy(&key->pubkey.z, &mQ->z)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
429 if ((err = ecc_mulmod(&u2, mQ, mQ, &m, 0)) != CRYPT_OK) { goto done; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
430
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
431 /* find the montgomery mp */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
432 if ((err = mp_montgomery_setup(&m, &mp)) != MP_OKAY) { goto error; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
433 /* add them */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
434 if ((err = add_point(mQ, mG, mG, &m, mp)) != CRYPT_OK) { goto done; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
435
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
436 /* reduce */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
437 if ((err = ecc_map(mG, &m, mp)) != CRYPT_OK) { goto done; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
438
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
439 /* v = X_x1 mod n */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
440 if ((err = mp_mod(&mG->x, &p, &v)) != CRYPT_OK) { goto done; }
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
441
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
442 /* does v == r */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
443 if (mp_cmp(&v, &r) == MP_EQ) {
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 *stat = 1;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 /* clear up and return */
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448 err = CRYPT_OK;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 goto done;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 error:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 err = mpi_to_ltc_error(err);
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 done:
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453 del_point(mG);
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
454 del_point(mQ);
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
455 mp_clear_multi(&r, &s, &v, &w, &u1, &u2, &p, &e, &m, NULL);
191
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 return err;
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 }
1c15b283127b Import of libtomcrypt 1.02 with manual path rename rearrangement etc
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458
209
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
459
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
460 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_sys.c,v $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
461 /* $Revision: 1.18 $ */
39d5d58461d6 Import of libtomcrypt 1.05
Matt Johnston <matt@ucc.asn.au>
parents: 191
diff changeset
462 /* $Date: 2005/06/14 20:47:55 $ */