comparison libtomcrypt/src/pk/dsa/dsa_set.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 #include "tomcrypt.h"
10
11
12 #ifdef LTC_MDSA
13
14 /**
15 Import DSA's p, q & g from raw numbers
16 @param p DSA's p in binary representation
17 @param plen The length of p
18 @param q DSA's q in binary representation
19 @param qlen The length of q
20 @param g DSA's g in binary representation
21 @param glen The length of g
22 @param key [out] the destination for the imported key
23 @return CRYPT_OK if successful.
24 */
25 int dsa_set_pqg(const unsigned char *p, unsigned long plen,
26 const unsigned char *q, unsigned long qlen,
27 const unsigned char *g, unsigned long glen,
28 dsa_key *key)
29 {
30 int err, stat;
31
32 LTC_ARGCHK(p != NULL);
33 LTC_ARGCHK(q != NULL);
34 LTC_ARGCHK(g != NULL);
35 LTC_ARGCHK(key != NULL);
36 LTC_ARGCHK(ltc_mp.name != NULL);
37
38 /* init key */
39 err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
40 if (err != CRYPT_OK) return err;
41
42 if ((err = mp_read_unsigned_bin(key->p, (unsigned char *)p , plen)) != CRYPT_OK) { goto LBL_ERR; }
43 if ((err = mp_read_unsigned_bin(key->g, (unsigned char *)g , glen)) != CRYPT_OK) { goto LBL_ERR; }
44 if ((err = mp_read_unsigned_bin(key->q, (unsigned char *)q , qlen)) != CRYPT_OK) { goto LBL_ERR; }
45
46 key->qord = mp_unsigned_bin_size(key->q);
47
48 /* do only a quick validation, without primality testing */
49 if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) { goto LBL_ERR; }
50 if (stat == 0) {
51 err = CRYPT_INVALID_PACKET;
52 goto LBL_ERR;
53 }
54
55 return CRYPT_OK;
56
57 LBL_ERR:
58 dsa_free(key);
59 return err;
60 }
61
62 /**
63 Import DSA public or private key-part from raw numbers
64
65 NB: The p, q & g parts must be set beforehand
66
67 @param in The key-part to import, either public or private.
68 @param inlen The key-part's length
69 @param type Which type of key (PK_PRIVATE or PK_PUBLIC)
70 @param key [out] the destination for the imported key
71 @return CRYPT_OK if successful.
72 */
73 int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key)
74 {
75 int err, stat = 0;
76
77 LTC_ARGCHK(key != NULL);
78 LTC_ARGCHK(key->x != NULL);
79 LTC_ARGCHK(key->y != NULL);
80 LTC_ARGCHK(key->p != NULL);
81 LTC_ARGCHK(key->g != NULL);
82 LTC_ARGCHK(key->q != NULL);
83 LTC_ARGCHK(ltc_mp.name != NULL);
84
85 if (type == PK_PRIVATE) {
86 key->type = PK_PRIVATE;
87 if ((err = mp_read_unsigned_bin(key->x, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
88 if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; }
89 }
90 else {
91 key->type = PK_PUBLIC;
92 if ((err = mp_read_unsigned_bin(key->y, (unsigned char *)in, inlen)) != CRYPT_OK) { goto LBL_ERR; }
93 }
94
95 if ((err = dsa_int_validate_xy(key, &stat)) != CRYPT_OK) { goto LBL_ERR; }
96 if (stat == 0) {
97 err = CRYPT_INVALID_PACKET;
98 goto LBL_ERR;
99 }
100
101 return CRYPT_OK;
102
103 LBL_ERR:
104 dsa_free(key);
105 return err;
106 }
107
108 #endif
109
110 /* ref: $Format:%D$ */
111 /* git commit: $Format:%H$ */
112 /* commit time: $Format:%ai$ */