Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers curve25519.h Source File

curve25519.h

00001 /* curve25519.h
00002  *
00003  * Copyright (C) 2006-2017 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 /*!
00023     \file wolfssl/wolfcrypt/curve25519.h
00024 */
00025 
00026 
00027 #ifndef WOLF_CRYPT_CURVE25519_H
00028 #define WOLF_CRYPT_CURVE25519_H
00029 
00030 #include <wolfcrypt/types.h>
00031 
00032 #ifdef HAVE_CURVE25519
00033 
00034 #include <wolfcrypt/fe_operations.h>
00035 #include <wolfcrypt/random.h>
00036 
00037 #ifdef WOLFSSL_ASYNC_CRYPT
00038     #include <wolfcrypt/async.h>
00039 #endif
00040 
00041 #ifdef __cplusplus
00042     extern "C" {
00043 #endif
00044 
00045 #define CURVE25519_KEYSIZE 32
00046 
00047 /* curve25519 set type */
00048 typedef struct {
00049     int size;       /* The size of the curve in octets */
00050     const char* name;     /* name of this curve */
00051 } curve25519_set_type;
00052 
00053 
00054 /* ECC point, the internal structure is Little endian
00055  * the mathematical functions used the endianess */
00056 typedef struct {
00057     byte point[CURVE25519_KEYSIZE];
00058     #ifdef FREESCALE_LTC_ECC
00059         byte pointY[CURVE25519_KEYSIZE];
00060     #endif
00061 } ECPoint;
00062 
00063 /* A CURVE25519 Key */
00064 typedef struct curve25519_key {
00065     int idx;            /* Index into the ecc_sets[] for the parameters of
00066                            this curve if -1, this key is using user supplied
00067                            curve in dp */
00068     const curve25519_set_type* dp;   /* domain parameters, either points to
00069                                    curves (idx >= 0) or user supplied */
00070     ECPoint   p;        /* public key  */
00071     ECPoint   k;        /* private key */
00072 
00073 #ifdef WOLFSSL_ASYNC_CRYPT
00074     WC_ASYNC_DEV asyncDev;
00075 #endif
00076 } curve25519_key;
00077 
00078 enum {
00079     EC25519_LITTLE_ENDIAN=0,
00080     EC25519_BIG_ENDIAN=1
00081 };
00082 
00083 WOLFSSL_API
00084 int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);
00085 
00086 WOLFSSL_API
00087 int wc_curve25519_shared_secret(curve25519_key* private_key,
00088                                 curve25519_key* public_key,
00089                                 byte* out, word32* outlen);
00090 
00091 WOLFSSL_API
00092 int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
00093                                    curve25519_key* public_key,
00094                                    byte* out, word32* outlen, int endian);
00095 
00096 WOLFSSL_API
00097 int wc_curve25519_init(curve25519_key* key);
00098 
00099 WOLFSSL_API
00100 void wc_curve25519_free(curve25519_key* key);
00101 
00102 
00103 /* raw key helpers */
00104 WOLFSSL_API
00105 int wc_curve25519_import_private(const byte* priv, word32 privSz,
00106                                  curve25519_key* key);
00107 WOLFSSL_API
00108 int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
00109                                     curve25519_key* key, int endian);
00110 
00111 WOLFSSL_API
00112 int wc_curve25519_import_private_raw(const byte* priv, word32 privSz,
00113                             const byte* pub, word32 pubSz, curve25519_key* key);
00114 WOLFSSL_API
00115 int wc_curve25519_import_private_raw_ex(const byte* priv, word32 privSz,
00116                                         const byte* pub, word32 pubSz,
00117                                         curve25519_key* key, int endian);
00118 WOLFSSL_API
00119 int wc_curve25519_export_private_raw(curve25519_key* key, byte* out,
00120                                      word32* outLen);
00121 WOLFSSL_API
00122 int wc_curve25519_export_private_raw_ex(curve25519_key* key, byte* out,
00123                                         word32* outLen, int endian);
00124 
00125 WOLFSSL_API
00126 int wc_curve25519_import_public(const byte* in, word32 inLen,
00127                                 curve25519_key* key);
00128 WOLFSSL_API
00129 int wc_curve25519_import_public_ex(const byte* in, word32 inLen,
00130                                    curve25519_key* key, int endian);
00131 
00132 WOLFSSL_API
00133 int wc_curve25519_export_public(curve25519_key* key, byte* out, word32* outLen);
00134 WOLFSSL_API
00135 int wc_curve25519_export_public_ex(curve25519_key* key, byte* out,
00136                                    word32* outLen, int endian);
00137 
00138 WOLFSSL_API
00139 int wc_curve25519_export_key_raw(curve25519_key* key,
00140                                  byte* priv, word32 *privSz,
00141                                  byte* pub, word32 *pubSz);
00142 WOLFSSL_API
00143 int wc_curve25519_export_key_raw_ex(curve25519_key* key,
00144                                     byte* priv, word32 *privSz,
00145                                     byte* pub, word32 *pubSz,
00146                                     int endian);
00147 /* size helper */
00148 WOLFSSL_API
00149 int wc_curve25519_size(curve25519_key* key);
00150 
00151 #ifdef __cplusplus
00152     }    /* extern "C" */
00153 #endif
00154 
00155 #endif /* HAVE_CURVE25519 */
00156 #endif /* WOLF_CRYPT_CURVE25519_H */
00157 
00158