1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6    @file dsa_export.c
7    DSA implementation, export key, Tom St Denis
8 */
9 
10 #ifdef LTC_MDSA
11 
12 /**
13   Export a DSA key to a binary packet
14   @param out    [out] Where to store the packet
15   @param outlen [in/out] The max size and resulting size of the packet
16   @param type   The type of key to export (PK_PRIVATE or PK_PUBLIC)
17   @param key    The key to export
18   @return CRYPT_OK if successful
19 */
dsa_export(unsigned char * out,unsigned long * outlen,int type,const dsa_key * key)20 int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key)
21 {
22    unsigned long zero=0;
23    unsigned char flags[1];
24    int err, std;
25 
26    LTC_ARGCHK(out    != NULL);
27    LTC_ARGCHK(outlen != NULL);
28    LTC_ARGCHK(key    != NULL);
29 
30    std = type & PK_STD;
31    type &= ~PK_STD;
32 
33    if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
34       return CRYPT_PK_TYPE_MISMATCH;
35    }
36 
37    if (type == PK_PRIVATE) {
38       if (std) {
39           return der_encode_sequence_multi(out, outlen,
40                                          LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
41                                          LTC_ASN1_INTEGER,      1UL, key->p,
42                                          LTC_ASN1_INTEGER,      1UL, key->q,
43                                          LTC_ASN1_INTEGER,      1UL, key->g,
44                                          LTC_ASN1_INTEGER,      1UL, key->y,
45                                          LTC_ASN1_INTEGER,      1UL, key->x,
46                                          LTC_ASN1_EOL,          0UL, NULL);
47       }
48       flags[0] = 1;
49       return der_encode_sequence_multi(out, outlen,
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);
57    }
58 
59    if (type == PK_PUBLIC) {
60       if (std) {
61           unsigned long tmplen = (unsigned long)(mp_count_bits(key->y) / 8) + 8;
62           unsigned char* tmp = XMALLOC(tmplen);
63           ltc_asn1_list int_list[3];
64 
65           if (tmp == NULL) {
66               return CRYPT_MEM;
67           }
68 
69           err = der_encode_integer(key->y, tmp, &tmplen);
70           if (err != CRYPT_OK) {
71               goto error;
72           }
73 
74           LTC_SET_ASN1(int_list, 0, LTC_ASN1_INTEGER, key->p, 1UL);
75           LTC_SET_ASN1(int_list, 1, LTC_ASN1_INTEGER, key->q, 1UL);
76           LTC_SET_ASN1(int_list, 2, LTC_ASN1_INTEGER, key->g, 1UL);
77 
78           err = x509_encode_subject_public_key_info(out, outlen, LTC_OID_DSA, tmp,
79               tmplen, LTC_ASN1_SEQUENCE, int_list,
80               sizeof(int_list) / sizeof(int_list[0]));
81 
82 error:
83           XFREE(tmp);
84           return err;
85       }
86       flags[0] = 0;
87       return der_encode_sequence_multi(out, outlen,
88                                      LTC_ASN1_BIT_STRING,   1UL, flags,
89                                      LTC_ASN1_INTEGER,      1UL, key->g,
90                                      LTC_ASN1_INTEGER,      1UL, key->p,
91                                      LTC_ASN1_INTEGER,      1UL, key->q,
92                                      LTC_ASN1_INTEGER,      1UL, key->y,
93                                      LTC_ASN1_EOL,          0UL, NULL);
94    }
95 
96    return CRYPT_INVALID_ARG;
97 }
98 
99 #endif
100 
101