1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
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
21 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key)
23 unsigned char flags[1];
25 unsigned long version;
27 LTC_ARGCHK(in != NULL);
28 LTC_ARGCHK(key != NULL);
31 if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, NULL)) != CRYPT_OK) {
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) {
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) {
56 /* compute public key: y = (base ^ x) mod prime */
57 if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) {
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) {
74 err = CRYPT_INVALID_PACKET;
79 err = CRYPT_INVALID_PACKET;
83 /* check public key */
84 if ((err = dh_check_pubkey(key)) != CRYPT_OK) {
97 /* ref: $Format:%D$ */
98 /* git commit: $Format:%H$ */
99 /* commit time: $Format:%ai$ */