Mercurial > dropbear
comparison src/pk/dsa/dsa_import.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05
Import of libtomcrypt 1.05
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 06 Jul 2005 03:53:40 +0000 |
parents | 1c15b283127b |
children |
comparison
equal
deleted
inserted
replaced
191:1c15b283127b | 209:39d5d58461d6 |
---|---|
24 @param key [out] Where to store the imported key | 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 | 25 @return CRYPT_OK if successful, upon error this function will free all allocated memory |
26 */ | 26 */ |
27 int 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 { | 28 { |
29 unsigned long x, y; | 29 unsigned char flags[1]; |
30 int err; | 30 int err; |
31 | 31 |
32 LTC_ARGCHK(in != NULL); | 32 LTC_ARGCHK(in != NULL); |
33 LTC_ARGCHK(key != NULL); | 33 LTC_ARGCHK(key != NULL); |
34 | |
35 /* check length */ | |
36 if ((1+2+PACKET_SIZE) > inlen) { | |
37 return CRYPT_INVALID_PACKET; | |
38 } | |
39 | |
40 /* check type */ | |
41 if ((err = packet_valid_header((unsigned char *)in, PACKET_SECT_DSA, PACKET_SUB_KEY)) != CRYPT_OK) { | |
42 return err; | |
43 } | |
44 y = PACKET_SIZE; | |
45 | 34 |
46 /* init key */ | 35 /* init key */ |
47 if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != MP_OKAY) { | 36 if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != MP_OKAY) { |
48 return CRYPT_MEM; | 37 return CRYPT_MEM; |
49 } | 38 } |
50 | 39 |
51 /* read type/qord */ | 40 /* get key type */ |
52 key->type = in[y++]; | 41 if ((err = der_decode_sequence_multi(in, inlen, |
53 key->qord = ((unsigned)in[y]<<8)|((unsigned)in[y+1]); | 42 LTC_ASN1_BIT_STRING, 1UL, flags, |
54 y += 2; | 43 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { |
55 | 44 goto error; |
56 /* input publics */ | |
57 INPUT_BIGNUM(&key->g,in,x,y, inlen); | |
58 INPUT_BIGNUM(&key->p,in,x,y, inlen); | |
59 INPUT_BIGNUM(&key->q,in,x,y, inlen); | |
60 INPUT_BIGNUM(&key->y,in,x,y, inlen); | |
61 if (key->type == PK_PRIVATE) { | |
62 INPUT_BIGNUM(&key->x,in,x,y, inlen); | |
63 } | 45 } |
64 | 46 |
65 return CRYPT_OK; | 47 if (flags[0] == 1) { |
48 if ((err = der_decode_sequence_multi(in, inlen, | |
49 LTC_ASN1_BIT_STRING, 1UL, flags, | |
50 LTC_ASN1_INTEGER, 1UL, &key->g, | |
51 LTC_ASN1_INTEGER, 1UL, &key->p, | |
52 LTC_ASN1_INTEGER, 1UL, &key->q, | |
53 LTC_ASN1_INTEGER, 1UL, &key->y, | |
54 LTC_ASN1_INTEGER, 1UL, &key->x, | |
55 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
56 goto error; | |
57 } | |
58 key->type = PK_PRIVATE; | |
59 } else { | |
60 if ((err = der_decode_sequence_multi(in, inlen, | |
61 LTC_ASN1_BIT_STRING, 1UL, flags, | |
62 LTC_ASN1_INTEGER, 1UL, &key->g, | |
63 LTC_ASN1_INTEGER, 1UL, &key->p, | |
64 LTC_ASN1_INTEGER, 1UL, &key->q, | |
65 LTC_ASN1_INTEGER, 1UL, &key->y, | |
66 LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { | |
67 goto error; | |
68 } | |
69 key->type = PK_PUBLIC; | |
70 } | |
71 key->qord = mp_unsigned_bin_size(&key->q); | |
72 | |
73 if (key->qord >= MDSA_MAX_GROUP || key->qord <= 15 || | |
74 key->qord >= mp_unsigned_bin_size(&key->p) || (mp_unsigned_bin_size(&key->p) - key->qord) >= MDSA_DELTA) { | |
75 err = CRYPT_INVALID_PACKET; | |
76 goto error; | |
77 } | |
78 | |
79 return CRYPT_OK; | |
66 error: | 80 error: |
67 mp_clear_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL); | 81 mp_clear_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL); |
68 return err; | 82 return err; |
69 } | 83 } |
70 | 84 |
71 #endif | 85 #endif |
86 | |
87 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_import.c,v $ */ | |
88 /* $Revision: 1.7 $ */ | |
89 /* $Date: 2005/06/08 23:31:17 $ */ |