]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ecc_ansi_x963_export.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ecc_ansi_x963_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_ansi_x963_export.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
24 /** ECC X9.63 (Sec. 4.3.6) uncompressed export
25   @param key     Key to export
26   @param out     [out] destination of export
27   @param outlen  [in/out]  Length of destination and final output size
28   Return CRYPT_OK on success
29 */
30 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen)
31 {
32    unsigned char buf[ECC_BUF_SIZE];
33    unsigned long numlen, xlen, ylen;
34
35    LTC_ARGCHK(key    != NULL);
36    LTC_ARGCHK(outlen != NULL);
37
38    if (ltc_ecc_is_valid_idx(key->idx) == 0) {
39       return CRYPT_INVALID_ARG;
40    }
41    numlen = key->dp->size;
42    xlen = mp_unsigned_bin_size(key->pubkey.x);
43    ylen = mp_unsigned_bin_size(key->pubkey.y);
44
45    if (xlen > numlen || ylen > numlen || sizeof(buf) < numlen) {
46       return CRYPT_BUFFER_OVERFLOW;
47    }
48
49    if (*outlen < (1 + 2*numlen)) {
50       *outlen = 1 + 2*numlen;
51       return CRYPT_BUFFER_OVERFLOW;
52    }
53
54    LTC_ARGCHK(out    != NULL);
55
56    /* store byte 0x04 */
57    out[0] = 0x04;
58
59    /* pad and store x */
60    zeromem(buf, sizeof(buf));
61    mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - xlen));
62    XMEMCPY(out+1, buf, numlen);
63
64    /* pad and store y */
65    zeromem(buf, sizeof(buf));
66    mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - ylen));
67    XMEMCPY(out+1+numlen, buf, numlen);
68
69    *outlen = 1 + 2*numlen;
70    return CRYPT_OK;
71 }
72
73 #endif
74
75 /* ref:         $Format:%D$ */
76 /* git commit:  $Format:%H$ */
77 /* commit time: $Format:%ai$ */