• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
10  */
11 #include "tomcrypt.h"
12 
13 /**
14    @file dsa_export.c
15    DSA implementation, export key, Tom St Denis
16 */
17 
18 #ifdef MDSA
19 
20 /**
21   Export a DSA key to a binary packet
22   @param out    [out] Where to store the packet
23   @param outlen [in/out] The max size and resulting size of the packet
24   @param type   The type of key to export (PK_PRIVATE or PK_PUBLIC)
25   @param key    The key to export
26   @return CRYPT_OK if successful
27 */
dsa_export(unsigned char * out,unsigned long * outlen,int type,dsa_key * key)28 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key)
29 {
30    unsigned char flags[1];
31 
32    LTC_ARGCHK(out    != NULL);
33    LTC_ARGCHK(outlen != NULL);
34    LTC_ARGCHK(key    != NULL);
35 
36    /* can we store the static header?  */
37    if (type == PK_PRIVATE && key->type != PK_PRIVATE) {
38       return CRYPT_PK_TYPE_MISMATCH;
39    }
40 
41    if (type != PK_PUBLIC && type != PK_PRIVATE) {
42       return CRYPT_INVALID_ARG;
43    }
44 
45    flags[0] = (type != PK_PUBLIC) ? 1 : 0;
46 
47    if (type == PK_PRIVATE) {
48       return der_encode_sequence_multi(out, outlen,
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);
56    } else {
57       return der_encode_sequence_multi(out, outlen,
58                                  LTC_ASN1_BIT_STRING,   1UL, flags,
59                                  LTC_ASN1_INTEGER,      1UL, key->g,
60                                  LTC_ASN1_INTEGER,      1UL, key->p,
61                                  LTC_ASN1_INTEGER,      1UL, key->q,
62                                  LTC_ASN1_INTEGER,      1UL, key->y,
63                                  LTC_ASN1_EOL,          0UL, NULL);
64    }
65 }
66 
67 #endif
68 
69 
70 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_export.c,v $ */
71 /* $Revision: 1.8 $ */
72 /* $Date: 2006/03/31 14:15:35 $ */
73