]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ecc_export.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ecc_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 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11  *
12  * All curves taken from NIST recommendation paper of July 1999
13  * Available at http://csrc.nist.gov/cryptval/dss.htm
14  */
15 #include "tomcrypt.h"
16
17 /**
18   @file ecc_export.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
24 /**
25   Export an ECC key as a binary packet
26   @param out     [out] Destination for the key
27   @param outlen  [in/out] Max size and resulting size of the exported key
28   @param type    The type of key you want to export (PK_PRIVATE or PK_PUBLIC)
29   @param key     The key to export
30   @return CRYPT_OK if successful
31 */
32 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)
33 {
34    int           err;
35    unsigned char flags[1];
36    unsigned long key_size;
37
38    LTC_ARGCHK(out    != NULL);
39    LTC_ARGCHK(outlen != NULL);
40    LTC_ARGCHK(key    != NULL);
41
42    /* type valid? */
43    if (key->type != PK_PRIVATE && type == PK_PRIVATE) {
44       return CRYPT_PK_TYPE_MISMATCH;
45    }
46
47    if (ltc_ecc_is_valid_idx(key->idx) == 0) {
48       return CRYPT_INVALID_ARG;
49    }
50
51    /* we store the NIST byte size */
52    key_size = key->dp->size;
53
54    if (type == PK_PRIVATE) {
55        flags[0] = 1;
56        err = der_encode_sequence_multi(out, outlen,
57                                  LTC_ASN1_BIT_STRING,      1UL, flags,
58                                  LTC_ASN1_SHORT_INTEGER,   1UL, &key_size,
59                                  LTC_ASN1_INTEGER,         1UL, key->pubkey.x,
60                                  LTC_ASN1_INTEGER,         1UL, key->pubkey.y,
61                                  LTC_ASN1_INTEGER,         1UL, key->k,
62                                  LTC_ASN1_EOL,             0UL, NULL);
63    } else {
64        flags[0] = 0;
65        err = der_encode_sequence_multi(out, outlen,
66                                  LTC_ASN1_BIT_STRING,      1UL, flags,
67                                  LTC_ASN1_SHORT_INTEGER,   1UL, &key_size,
68                                  LTC_ASN1_INTEGER,         1UL, key->pubkey.x,
69                                  LTC_ASN1_INTEGER,         1UL, key->pubkey.y,
70                                  LTC_ASN1_EOL,             0UL, NULL);
71    }
72
73    return err;
74 }
75
76 #endif
77 /* ref:         $Format:%D$ */
78 /* git commit:  $Format:%H$ */
79 /* commit time: $Format:%ai$ */
80