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