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 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file dsa_import.c
15 DSA implementation, import a DSA key, Tom St Denis
16 */
17
18 #ifdef MDSA
19
20 /**
21 Import a DSA key
22 @param in The binary packet to import from
23 @param inlen The length of the binary packet
24 @param key [out] Where to store the imported key
25 @return CRYPT_OK if successful, upon error this function will free all allocated memory
26 */
dsa_import(const unsigned char * in,unsigned long inlen,dsa_key * key)27 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key)
28 {
29 unsigned char flags[1];
30 int err;
31
32 LTC_ARGCHK(in != NULL);
33 LTC_ARGCHK(key != NULL);
34 LTC_ARGCHK(ltc_mp.name != NULL);
35
36 /* init key */
37 if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != CRYPT_OK) {
38 return CRYPT_MEM;
39 }
40
41 /* get key type */
42 if ((err = der_decode_sequence_multi(in, inlen,
43 LTC_ASN1_BIT_STRING, 1UL, flags,
44 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
45 goto error;
46 }
47
48 if (flags[0] == 1) {
49 if ((err = der_decode_sequence_multi(in, inlen,
50 LTC_ASN1_BIT_STRING, 1UL, flags,
51 LTC_ASN1_INTEGER, 1UL, key->g,
52 LTC_ASN1_INTEGER, 1UL, key->p,
53 LTC_ASN1_INTEGER, 1UL, key->q,
54 LTC_ASN1_INTEGER, 1UL, key->y,
55 LTC_ASN1_INTEGER, 1UL, key->x,
56 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
57 goto error;
58 }
59 key->type = PK_PRIVATE;
60 } else {
61 if ((err = der_decode_sequence_multi(in, inlen,
62 LTC_ASN1_BIT_STRING, 1UL, flags,
63 LTC_ASN1_INTEGER, 1UL, key->g,
64 LTC_ASN1_INTEGER, 1UL, key->p,
65 LTC_ASN1_INTEGER, 1UL, key->q,
66 LTC_ASN1_INTEGER, 1UL, key->y,
67 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
68 goto error;
69 }
70 key->type = PK_PUBLIC;
71 }
72 key->qord = mp_unsigned_bin_size(key->q);
73
74 if (key->qord >= MDSA_MAX_GROUP || key->qord <= 15 ||
75 (unsigned long)key->qord >= mp_unsigned_bin_size(key->p) || (mp_unsigned_bin_size(key->p) - key->qord) >= MDSA_DELTA) {
76 err = CRYPT_INVALID_PACKET;
77 goto error;
78 }
79
80 return CRYPT_OK;
81 error:
82 mp_clear_multi(key->p, key->g, key->q, key->x, key->y, NULL);
83 return err;
84 }
85
86 #endif
87
88 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_import.c,v $ */
89 /* $Revision: 1.12 $ */
90 /* $Date: 2006/03/31 14:15:35 $ */
91