Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_export.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b10*11* All curves taken from NIST recommendation paper of July 199912* Available at http://csrc.nist.gov/cryptval/dss.htm13*/14#include "tomcrypt.h"1516/**17@file ecc_export.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC2223/**24Export an ECC key as a binary packet25@param out [out] Destination for the key26@param outlen [in/out] Max size and resulting size of the exported key27@param type The type of key you want to export (PK_PRIVATE or PK_PUBLIC)28@param key The key to export29@return CRYPT_OK if successful30*/31int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key)32{33int err;34unsigned char flags[1];35unsigned long key_size;3637LTC_ARGCHK(out != NULL);38LTC_ARGCHK(outlen != NULL);39LTC_ARGCHK(key != NULL);4041/* type valid? */42if (key->type != PK_PRIVATE && type == PK_PRIVATE) {43return CRYPT_PK_TYPE_MISMATCH;44}4546if (ltc_ecc_is_valid_idx(key->idx) == 0) {47return CRYPT_INVALID_ARG;48}4950/* we store the NIST byte size */51key_size = key->dp->size;5253if (type == PK_PRIVATE) {54flags[0] = 1;55err = der_encode_sequence_multi(out, outlen,56LTC_ASN1_BIT_STRING, 1UL, flags,57LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,58LTC_ASN1_INTEGER, 1UL, key->pubkey.x,59LTC_ASN1_INTEGER, 1UL, key->pubkey.y,60LTC_ASN1_INTEGER, 1UL, key->k,61LTC_ASN1_EOL, 0UL, NULL);62} else {63flags[0] = 0;64err = der_encode_sequence_multi(out, outlen,65LTC_ASN1_BIT_STRING, 1UL, flags,66LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,67LTC_ASN1_INTEGER, 1UL, key->pubkey.x,68LTC_ASN1_INTEGER, 1UL, key->pubkey.y,69LTC_ASN1_EOL, 0UL, NULL);70}7172return err;73}7475#endif767778