1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 
6 #ifdef LTC_MDSA
7 
8 /**
9   Import DSA's p, q & g from dsaparam
10 
11       dsaparam data: openssl dsaparam -outform DER -out dsaparam.der 2048
12 
13   @param dsaparam    The DSA param DER encoded data
14   @param dsaparamlen The length of dhparam data
15   @param key         [out] the destination for the imported key
16   @return CRYPT_OK if successful.
17 */
dsa_set_pqg_dsaparam(const unsigned char * dsaparam,unsigned long dsaparamlen,dsa_key * key)18 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen,
19                          dsa_key *key)
20 {
21    int err, stat;
22 
23    LTC_ARGCHK(dsaparam    != NULL);
24    LTC_ARGCHK(key         != NULL);
25    LTC_ARGCHK(ltc_mp.name != NULL);
26 
27    /* init key */
28    err = mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, LTC_NULL);
29    if (err != CRYPT_OK) return err;
30 
31    if ((err = der_decode_sequence_multi(dsaparam, dsaparamlen,
32                                         LTC_ASN1_INTEGER, 1UL, key->p,
33                                         LTC_ASN1_INTEGER, 1UL, key->q,
34                                         LTC_ASN1_INTEGER, 1UL, key->g,
35                                         LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
36       goto LBL_ERR;
37    }
38 
39    key->qord = mp_unsigned_bin_size(key->q);
40 
41    /* quick p, q, g validation, without primality testing */
42    if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) {
43       goto LBL_ERR;
44    }
45    if (stat == 0) {
46       err = CRYPT_INVALID_PACKET;
47       goto LBL_ERR;
48    }
49 
50    return CRYPT_OK;
51 
52 LBL_ERR:
53    dsa_free(key);
54    return err;
55 }
56 
57 #endif
58