Mercurial > dropbear
comparison libtomcrypt/src/pk/dh/dh_import.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
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 #include "tomcrypt.h" | |
11 | |
12 #ifdef LTC_MDH | |
13 | |
14 /** | |
15 Import a DH key from a binary packet | |
16 @param in The packet to read | |
17 @param inlen The length of the input packet | |
18 @param key [out] Where to import the key to | |
19 @return CRYPT_OK if successful, on error all allocated memory is freed automatically | |
20 */ | |
21 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key) | |
22 { | |
23 unsigned char flags[1]; | |
24 int err; | |
25 unsigned long version; | |
26 | |
27 LTC_ARGCHK(in != NULL); | |
28 LTC_ARGCHK(key != NULL); | |
29 | |
30 /* init */ | |
31 if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) { | |
32 return err; | |
33 } | |
34 | |
35 /* find out what type of key it is */ | |
36 err = der_decode_sequence_multi(in, inlen, | |
37 LTC_ASN1_SHORT_INTEGER, 1UL, &version, | |
38 LTC_ASN1_BIT_STRING, 1UL, &flags, | |
39 LTC_ASN1_EOL, 0UL, NULL); | |
40 if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) { | |
41 goto error; | |
42 } | |
43 | |
44 if (version == 0) { | |
45 if (flags[0] == 1) { | |
46 key->type = PK_PRIVATE; | |
47 if ((err = der_decode_sequence_multi(in, inlen, | |
48 LTC_ASN1_SHORT_INTEGER, 1UL, &version, | |
49 LTC_ASN1_BIT_STRING, 1UL, flags, | |
50 LTC_ASN1_INTEGER, 1UL, key->prime, | |
51 LTC_ASN1_INTEGER, 1UL, key->base, | |
52 LTC_ASN1_INTEGER, 1UL, key->x, | |
53 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
54 goto error; | |
55 } | |
56 /* compute public key: y = (base ^ x) mod prime */ | |
57 if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { | |
58 goto error; | |
59 } | |
60 } | |
61 else if (flags[0] == 0) { | |
62 key->type = PK_PUBLIC; | |
63 if ((err = der_decode_sequence_multi(in, inlen, | |
64 LTC_ASN1_SHORT_INTEGER, 1UL, &version, | |
65 LTC_ASN1_BIT_STRING, 1UL, flags, | |
66 LTC_ASN1_INTEGER, 1UL, key->prime, | |
67 LTC_ASN1_INTEGER, 1UL, key->base, | |
68 LTC_ASN1_INTEGER, 1UL, key->y, | |
69 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
70 goto error; | |
71 } | |
72 } | |
73 else { | |
74 err = CRYPT_INVALID_PACKET; | |
75 goto error; | |
76 } | |
77 } | |
78 else { | |
79 err = CRYPT_INVALID_PACKET; | |
80 goto error; | |
81 } | |
82 | |
83 /* check public key */ | |
84 if ((err = dh_check_pubkey(key)) != CRYPT_OK) { | |
85 goto error; | |
86 } | |
87 | |
88 return CRYPT_OK; | |
89 | |
90 error: | |
91 dh_free(key); | |
92 return err; | |
93 } | |
94 | |
95 #endif /* LTC_MDH */ | |
96 | |
97 /* ref: $Format:%D$ */ | |
98 /* git commit: $Format:%H$ */ | |
99 /* commit time: $Format:%ai$ */ |