[352] | 1 | /* curve25519.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2017 wolfSSL Inc.
|
---|
| 4 | *
|
---|
| 5 | * This file is part of wolfSSL.
|
---|
| 6 | *
|
---|
| 7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with this program; if not, write to the Free Software
|
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | #ifndef WOLF_CRYPT_CURVE25519_H
|
---|
| 24 | #define WOLF_CRYPT_CURVE25519_H
|
---|
| 25 |
|
---|
| 26 | #include <wolfssl/wolfcrypt/types.h>
|
---|
| 27 |
|
---|
| 28 | #ifdef HAVE_CURVE25519
|
---|
| 29 |
|
---|
| 30 | #include <wolfssl/wolfcrypt/fe_operations.h>
|
---|
| 31 | #include <wolfssl/wolfcrypt/random.h>
|
---|
| 32 |
|
---|
| 33 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 34 | #include <wolfssl/wolfcrypt/async.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #ifdef __cplusplus
|
---|
| 38 | extern "C" {
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | #define CURVE25519_KEYSIZE 32
|
---|
| 42 |
|
---|
| 43 | /* curve25519 set type */
|
---|
| 44 | typedef struct {
|
---|
| 45 | int size; /* The size of the curve in octets */
|
---|
| 46 | const char* name; /* name of this curve */
|
---|
| 47 | } curve25519_set_type;
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | /* ECC point, the internal structure is Little endian
|
---|
| 51 | * the mathematical functions used the endianess */
|
---|
| 52 | typedef struct {
|
---|
| 53 | byte point[CURVE25519_KEYSIZE];
|
---|
| 54 | #ifdef FREESCALE_LTC_ECC
|
---|
| 55 | byte pointY[CURVE25519_KEYSIZE];
|
---|
| 56 | #endif
|
---|
| 57 | } ECPoint;
|
---|
| 58 |
|
---|
| 59 | /* A CURVE25519 Key */
|
---|
| 60 | typedef struct curve25519_key {
|
---|
| 61 | int idx; /* Index into the ecc_sets[] for the parameters of
|
---|
| 62 | this curve if -1, this key is using user supplied
|
---|
| 63 | curve in dp */
|
---|
| 64 | const curve25519_set_type* dp; /* domain parameters, either points to
|
---|
| 65 | curves (idx >= 0) or user supplied */
|
---|
| 66 | ECPoint p; /* public key */
|
---|
| 67 | ECPoint k; /* private key */
|
---|
| 68 |
|
---|
| 69 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 70 | WC_ASYNC_DEV asyncDev;
|
---|
| 71 | #endif
|
---|
| 72 | } curve25519_key;
|
---|
| 73 |
|
---|
| 74 | enum {
|
---|
| 75 | EC25519_LITTLE_ENDIAN=0,
|
---|
| 76 | EC25519_BIG_ENDIAN=1
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | WOLFSSL_API
|
---|
| 80 | int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);
|
---|
| 81 |
|
---|
| 82 | WOLFSSL_API
|
---|
| 83 | int wc_curve25519_shared_secret(curve25519_key* private_key,
|
---|
| 84 | curve25519_key* public_key,
|
---|
| 85 | byte* out, word32* outlen);
|
---|
| 86 |
|
---|
| 87 | WOLFSSL_API
|
---|
| 88 | int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
|
---|
| 89 | curve25519_key* public_key,
|
---|
| 90 | byte* out, word32* outlen, int endian);
|
---|
| 91 |
|
---|
| 92 | WOLFSSL_API
|
---|
| 93 | int wc_curve25519_init(curve25519_key* key);
|
---|
| 94 |
|
---|
| 95 | WOLFSSL_API
|
---|
| 96 | void wc_curve25519_free(curve25519_key* key);
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | /* raw key helpers */
|
---|
| 100 | WOLFSSL_API
|
---|
| 101 | int wc_curve25519_import_private(const byte* priv, word32 privSz,
|
---|
| 102 | curve25519_key* key);
|
---|
| 103 | WOLFSSL_API
|
---|
| 104 | int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
|
---|
| 105 | curve25519_key* key, int endian);
|
---|
| 106 |
|
---|
| 107 | WOLFSSL_API
|
---|
| 108 | int wc_curve25519_import_private_raw(const byte* priv, word32 privSz,
|
---|
| 109 | const byte* pub, word32 pubSz, curve25519_key* key);
|
---|
| 110 | WOLFSSL_API
|
---|
| 111 | int wc_curve25519_import_private_raw_ex(const byte* priv, word32 privSz,
|
---|
| 112 | const byte* pub, word32 pubSz,
|
---|
| 113 | curve25519_key* key, int endian);
|
---|
| 114 | WOLFSSL_API
|
---|
| 115 | int wc_curve25519_export_private_raw(curve25519_key* key, byte* out,
|
---|
| 116 | word32* outLen);
|
---|
| 117 | WOLFSSL_API
|
---|
| 118 | int wc_curve25519_export_private_raw_ex(curve25519_key* key, byte* out,
|
---|
| 119 | word32* outLen, int endian);
|
---|
| 120 |
|
---|
| 121 | WOLFSSL_API
|
---|
| 122 | int wc_curve25519_import_public(const byte* in, word32 inLen,
|
---|
| 123 | curve25519_key* key);
|
---|
| 124 | WOLFSSL_API
|
---|
| 125 | int wc_curve25519_import_public_ex(const byte* in, word32 inLen,
|
---|
| 126 | curve25519_key* key, int endian);
|
---|
| 127 |
|
---|
| 128 | WOLFSSL_API
|
---|
| 129 | int wc_curve25519_export_public(curve25519_key* key, byte* out, word32* outLen);
|
---|
| 130 | WOLFSSL_API
|
---|
| 131 | int wc_curve25519_export_public_ex(curve25519_key* key, byte* out,
|
---|
| 132 | word32* outLen, int endian);
|
---|
| 133 |
|
---|
| 134 | WOLFSSL_API
|
---|
| 135 | int wc_curve25519_export_key_raw(curve25519_key* key,
|
---|
| 136 | byte* priv, word32 *privSz,
|
---|
| 137 | byte* pub, word32 *pubSz);
|
---|
| 138 | WOLFSSL_API
|
---|
| 139 | int wc_curve25519_export_key_raw_ex(curve25519_key* key,
|
---|
| 140 | byte* priv, word32 *privSz,
|
---|
| 141 | byte* pub, word32 *pubSz,
|
---|
| 142 | int endian);
|
---|
| 143 | /* size helper */
|
---|
| 144 | WOLFSSL_API
|
---|
| 145 | int wc_curve25519_size(curve25519_key* key);
|
---|
| 146 |
|
---|
| 147 | #ifdef __cplusplus
|
---|
| 148 | } /* extern "C" */
|
---|
| 149 | #endif
|
---|
| 150 |
|
---|
| 151 | #endif /* HAVE_CURVE25519 */
|
---|
| 152 | #endif /* WOLF_CRYPT_CURVE25519_H */
|
---|
| 153 |
|
---|