]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/dh/dh_export.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / dh / dh_export.c
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
10 #include "tomcrypt.h"
11
12 #ifdef LTC_MDH
13
14 /**
15   Export a DH key to a binary packet
16   @param out    [out] The destination for the key
17   @param outlen [in/out] The max size and resulting size of the DH key
18   @param type   Which type of key (PK_PRIVATE or PK_PUBLIC)
19   @param key    The key you wish to export
20   @return CRYPT_OK if successful
21 */
22 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key)
23 {
24    unsigned char flags[1];
25    int err;
26    unsigned long version = 0;
27
28    LTC_ARGCHK(out    != NULL);
29    LTC_ARGCHK(outlen != NULL);
30    LTC_ARGCHK(key    != NULL);
31
32    if (type == PK_PRIVATE) {
33       /* export x - private key */
34       flags[0] = 1;
35       err = der_encode_sequence_multi(out, outlen,
36                                 LTC_ASN1_SHORT_INTEGER, 1UL, &version,
37                                 LTC_ASN1_BIT_STRING,    1UL, flags,
38                                 LTC_ASN1_INTEGER,       1UL, key->prime,
39                                 LTC_ASN1_INTEGER,       1UL, key->base,
40                                 LTC_ASN1_INTEGER,       1UL, key->x,
41                                 LTC_ASN1_EOL,           0UL, NULL);
42    }
43    else {
44       /* export y - public key */
45       flags[0] = 0;
46       err = der_encode_sequence_multi(out, outlen,
47                                 LTC_ASN1_SHORT_INTEGER, 1UL, &version,
48                                 LTC_ASN1_BIT_STRING,    1UL, flags,
49                                 LTC_ASN1_INTEGER,       1UL, key->prime,
50                                 LTC_ASN1_INTEGER,       1UL, key->base,
51                                 LTC_ASN1_INTEGER,       1UL, key->y,
52                                 LTC_ASN1_EOL,           0UL, NULL);
53    }
54
55    return err;
56 }
57
58 #endif /* LTC_MDH */
59
60 /* ref:         $Format:%D$ */
61 /* git commit:  $Format:%H$ */
62 /* commit time: $Format:%ai$ */