1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 
13 #ifdef LTC_MDSA
14 
15 /**
16   Import DSA's p, q & g from dsaparam
17 
18       dsaparam data: openssl dsaparam -outform DER -out dsaparam.der 2048
19 
20   @param dsaparam    The DSA param DER encoded data
21   @param dsaparamlen The length of dhparam data
22   @param key         [out] the destination for the imported key
23   @return CRYPT_OK if successful.
24 */
dsa_set_pqg_dsaparam(const unsigned char * dsaparam,unsigned long dsaparamlen,dsa_key * key)25 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen,
26                          dsa_key *key)
27 {
28    int err, stat;
29 
30    LTC_ARGCHK(dsaparam    != NULL);
31    LTC_ARGCHK(key         != NULL);
32    LTC_ARGCHK(ltc_mp.name != NULL);
33 
34    /* init key */
35    err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
36    if (err != CRYPT_OK) return err;
37 
38    if ((err = der_decode_sequence_multi(dsaparam, dsaparamlen,
39                                         LTC_ASN1_INTEGER, 1UL, key->p,
40                                         LTC_ASN1_INTEGER, 1UL, key->q,
41                                         LTC_ASN1_INTEGER, 1UL, key->g,
42                                         LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
43       goto LBL_ERR;
44    }
45 
46    key->qord = mp_unsigned_bin_size(key->q);
47 
48    /* quick p, q, g validation, without primality testing */
49    if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) {
50       goto LBL_ERR;
51    }
52    if (stat == 0) {
53       err = CRYPT_INVALID_PACKET;
54       goto LBL_ERR;
55    }
56 
57    return CRYPT_OK;
58 
59 LBL_ERR:
60    dsa_free(key);
61    return err;
62 }
63 
64 #endif
65 
66 /* ref:         $Format:%D$ */
67 /* git commit:  $Format:%H$ */
68 /* commit time: $Format:%ai$ */
69