[457] | 1 | /* curve25519.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2020 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 | \file wolfssl/wolfcrypt/curve25519.h
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | #ifndef WOLF_CRYPT_CURVE25519_H
|
---|
| 28 | #define WOLF_CRYPT_CURVE25519_H
|
---|
| 29 |
|
---|
| 30 | #include <wolfssl/wolfcrypt/types.h>
|
---|
| 31 |
|
---|
| 32 | #ifdef HAVE_CURVE25519
|
---|
| 33 |
|
---|
| 34 | #include <wolfssl/wolfcrypt/fe_operations.h>
|
---|
| 35 | #include <wolfssl/wolfcrypt/random.h>
|
---|
| 36 |
|
---|
| 37 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 38 | #include <wolfssl/wolfcrypt/async.h>
|
---|
| 39 | #endif
|
---|
| 40 |
|
---|
| 41 | #ifdef __cplusplus
|
---|
| 42 | extern "C" {
|
---|
| 43 | #endif
|
---|
| 44 |
|
---|
| 45 | #define CURVE25519_KEYSIZE 32
|
---|
| 46 |
|
---|
| 47 | #ifdef WOLFSSL_NAMES_STATIC
|
---|
| 48 | typedef char curve25519_str[12];
|
---|
| 49 | #else
|
---|
| 50 | typedef const char* curve25519_str;
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | /* curve25519 set type */
|
---|
| 54 | typedef struct {
|
---|
| 55 | int size; /* The size of the curve in octets */
|
---|
| 56 | curve25519_str name; /* name of this curve */
|
---|
| 57 | } curve25519_set_type;
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | /* ECC point, the internal structure is Little endian
|
---|
| 61 | * the mathematical functions used the endianness */
|
---|
| 62 | typedef struct {
|
---|
| 63 | byte point[CURVE25519_KEYSIZE];
|
---|
| 64 | #ifdef FREESCALE_LTC_ECC
|
---|
| 65 | byte pointY[CURVE25519_KEYSIZE];
|
---|
| 66 | #endif
|
---|
| 67 | } ECPoint;
|
---|
| 68 |
|
---|
| 69 | /* A CURVE25519 Key */
|
---|
| 70 | typedef struct curve25519_key {
|
---|
| 71 | int idx; /* Index into the ecc_sets[] for the parameters of
|
---|
| 72 | this curve if -1, this key is using user supplied
|
---|
| 73 | curve in dp */
|
---|
| 74 | const curve25519_set_type* dp; /* domain parameters, either points to
|
---|
| 75 | curves (idx >= 0) or user supplied */
|
---|
| 76 | ECPoint p; /* public key */
|
---|
| 77 | ECPoint k; /* private key */
|
---|
| 78 |
|
---|
| 79 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 80 | WC_ASYNC_DEV asyncDev;
|
---|
| 81 | #endif
|
---|
| 82 | } curve25519_key;
|
---|
| 83 |
|
---|
| 84 | enum {
|
---|
| 85 | EC25519_LITTLE_ENDIAN=0,
|
---|
| 86 | EC25519_BIG_ENDIAN=1
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | WOLFSSL_API
|
---|
[464] | 90 | int wc_curve25519_make_pub(int public_size, byte* pub, int private_size,
|
---|
| 91 | const byte* priv);
|
---|
| 92 |
|
---|
| 93 | WOLFSSL_API
|
---|
| 94 | int wc_curve25519_generic(int public_size, byte* pub,
|
---|
| 95 | int private_size, const byte* priv,
|
---|
| 96 | int basepoint_size, const byte* basepoint);
|
---|
| 97 |
|
---|
| 98 | WOLFSSL_API
|
---|
| 99 | int wc_curve25519_make_priv(WC_RNG* rng, int keysize, byte* priv);
|
---|
| 100 |
|
---|
| 101 | WOLFSSL_API
|
---|
[457] | 102 | int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);
|
---|
| 103 |
|
---|
| 104 | WOLFSSL_API
|
---|
| 105 | int wc_curve25519_shared_secret(curve25519_key* private_key,
|
---|
| 106 | curve25519_key* public_key,
|
---|
| 107 | byte* out, word32* outlen);
|
---|
| 108 |
|
---|
| 109 | WOLFSSL_API
|
---|
| 110 | int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
|
---|
| 111 | curve25519_key* public_key,
|
---|
| 112 | byte* out, word32* outlen, int endian);
|
---|
| 113 |
|
---|
| 114 | WOLFSSL_API
|
---|
| 115 | int wc_curve25519_init(curve25519_key* key);
|
---|
| 116 |
|
---|
| 117 | WOLFSSL_API
|
---|
| 118 | void wc_curve25519_free(curve25519_key* key);
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | /* raw key helpers */
|
---|
| 122 | WOLFSSL_API
|
---|
| 123 | int wc_curve25519_import_private(const byte* priv, word32 privSz,
|
---|
| 124 | curve25519_key* key);
|
---|
| 125 | WOLFSSL_API
|
---|
| 126 | int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
|
---|
| 127 | curve25519_key* key, int endian);
|
---|
| 128 |
|
---|
| 129 | WOLFSSL_API
|
---|
| 130 | int wc_curve25519_import_private_raw(const byte* priv, word32 privSz,
|
---|
| 131 | const byte* pub, word32 pubSz, curve25519_key* key);
|
---|
| 132 | WOLFSSL_API
|
---|
| 133 | int wc_curve25519_import_private_raw_ex(const byte* priv, word32 privSz,
|
---|
| 134 | const byte* pub, word32 pubSz,
|
---|
| 135 | curve25519_key* key, int endian);
|
---|
| 136 | WOLFSSL_API
|
---|
| 137 | int wc_curve25519_export_private_raw(curve25519_key* key, byte* out,
|
---|
| 138 | word32* outLen);
|
---|
| 139 | WOLFSSL_API
|
---|
| 140 | int wc_curve25519_export_private_raw_ex(curve25519_key* key, byte* out,
|
---|
| 141 | word32* outLen, int endian);
|
---|
| 142 |
|
---|
| 143 | WOLFSSL_API
|
---|
| 144 | int wc_curve25519_import_public(const byte* in, word32 inLen,
|
---|
| 145 | curve25519_key* key);
|
---|
| 146 | WOLFSSL_API
|
---|
| 147 | int wc_curve25519_import_public_ex(const byte* in, word32 inLen,
|
---|
| 148 | curve25519_key* key, int endian);
|
---|
| 149 | WOLFSSL_API
|
---|
| 150 | int wc_curve25519_check_public(const byte* pub, word32 pubSz, int endian);
|
---|
| 151 |
|
---|
| 152 | WOLFSSL_API
|
---|
| 153 | int wc_curve25519_export_public(curve25519_key* key, byte* out, word32* outLen);
|
---|
| 154 | WOLFSSL_API
|
---|
| 155 | int wc_curve25519_export_public_ex(curve25519_key* key, byte* out,
|
---|
| 156 | word32* outLen, int endian);
|
---|
| 157 |
|
---|
| 158 | WOLFSSL_API
|
---|
| 159 | int wc_curve25519_export_key_raw(curve25519_key* key,
|
---|
| 160 | byte* priv, word32 *privSz,
|
---|
| 161 | byte* pub, word32 *pubSz);
|
---|
| 162 | WOLFSSL_API
|
---|
| 163 | int wc_curve25519_export_key_raw_ex(curve25519_key* key,
|
---|
| 164 | byte* priv, word32 *privSz,
|
---|
| 165 | byte* pub, word32 *pubSz,
|
---|
| 166 | int endian);
|
---|
| 167 | /* size helper */
|
---|
| 168 | WOLFSSL_API
|
---|
| 169 | int wc_curve25519_size(curve25519_key* key);
|
---|
| 170 |
|
---|
| 171 | #ifdef __cplusplus
|
---|
| 172 | } /* extern "C" */
|
---|
| 173 | #endif
|
---|
| 174 |
|
---|
| 175 | #endif /* HAVE_CURVE25519 */
|
---|
| 176 | #endif /* WOLF_CRYPT_CURVE25519_H */
|
---|
| 177 |
|
---|