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