gnunet-android

GNUnet for Android
Log | Files | Refs | README

curve25519.h (7139B)


      1 /* curve25519.h
      2  *
      3  * Copyright (C) 2006-2025 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 3 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 #define CURVE25519_PUB_KEY_SIZE 32
     47 #define CURVE25519_MAX_KEY_TO_DER_SZ 82 /* for exported DER keys temp buffer */
     48 
     49 #ifdef WOLFSSL_NAMES_STATIC
     50 typedef char curve25519_str[12];
     51 #else
     52 typedef const char* curve25519_str;
     53 #endif
     54 
     55 /* curve25519 set type */
     56 typedef struct {
     57     int size;             /* The size of the curve in octets */
     58     curve25519_str name; /* name of this curve */
     59 } curve25519_set_type;
     60 
     61 
     62 /* ECC point, the internal structure is Little endian
     63  * the mathematical functions used the endianness */
     64 typedef struct ECPoint {
     65     ALIGN16 byte point[CURVE25519_KEYSIZE];
     66 #ifdef FREESCALE_LTC_ECC
     67     ALIGN16 byte pointY[CURVE25519_KEYSIZE];
     68 #endif
     69     byte pointSz;
     70 } ECPoint;
     71 
     72 #ifndef WC_CURVE25519KEY_TYPE_DEFINED
     73     typedef struct curve25519_key curve25519_key;
     74     #define WC_CURVE25519KEY_TYPE_DEFINED
     75 #endif
     76 
     77 /* A CURVE25519 Key */
     78 struct curve25519_key {
     79     int idx;            /* Index into the ecc_sets[] for the parameters of
     80                            this curve if -1, this key is using user supplied
     81                            curve in dp */
     82     const curve25519_set_type* dp;   /* domain parameters, either points to
     83                                    curves (idx >= 0) or user supplied */
     84     ECPoint      p;                     /* public point for key  */
     85     ALIGN16 byte k[CURVE25519_KEYSIZE]; /* private scaler for key */
     86 
     87 #ifdef WOLFSSL_ASYNC_CRYPT
     88     WC_ASYNC_DEV asyncDev;
     89 #endif
     90 #if defined(WOLF_CRYPTO_CB)
     91     void* devCtx;
     92     int devId;
     93 #endif
     94     void *heap;
     95 #ifdef WOLFSSL_CURVE25519_BLINDING
     96     WC_RNG* rng;
     97 #endif
     98 #ifdef WOLFSSL_SE050
     99     word32 keyId;
    100     byte   keyIdSet;
    101 #endif
    102 
    103     /* bit fields */
    104     WC_BITFIELD pubSet:1;
    105     WC_BITFIELD privSet:1;
    106 };
    107 
    108 enum {
    109     EC25519_LITTLE_ENDIAN=0,
    110     EC25519_BIG_ENDIAN=1
    111 };
    112 
    113 WOLFSSL_API
    114 int wc_curve25519_make_pub(int public_size, byte* pub, int private_size,
    115                            const byte* priv);
    116 #ifdef WOLFSSL_CURVE25519_BLINDING
    117 WOLFSSL_API
    118 int wc_curve25519_make_pub_blind(int public_size, byte* pub, int private_size,
    119                                  const byte* priv, WC_RNG* rng);
    120 #endif
    121 
    122 WOLFSSL_API
    123 int wc_curve25519_generic(int public_size, byte* pub,
    124                           int private_size, const byte* priv,
    125                           int basepoint_size, const byte* basepoint);
    126 #ifdef WOLFSSL_CURVE25519_BLINDING
    127 WOLFSSL_API
    128 int wc_curve25519_generic_blind(int public_size, byte* pub,
    129                                 int private_size, const byte* priv,
    130                                 int basepoint_size, const byte* basepoint,
    131                                 WC_RNG* rng);
    132 #endif
    133 
    134 WOLFSSL_API
    135 int wc_curve25519_make_priv(WC_RNG* rng, int keysize, byte* priv);
    136 
    137 WOLFSSL_API
    138 int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);
    139 
    140 WOLFSSL_API
    141 int wc_curve25519_shared_secret(curve25519_key* private_key,
    142                                 curve25519_key* public_key,
    143                                 byte* out, word32* outlen);
    144 
    145 WOLFSSL_API
    146 int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
    147                                    curve25519_key* public_key,
    148                                    byte* out, word32* outlen, int endian);
    149 
    150 WOLFSSL_API
    151 int wc_curve25519_init(curve25519_key* key);
    152 WOLFSSL_API
    153 int wc_curve25519_init_ex(curve25519_key* key, void* heap, int devId);
    154 
    155 WOLFSSL_API
    156 void wc_curve25519_free(curve25519_key* key);
    157 
    158 #ifdef WOLFSSL_CURVE25519_BLINDING
    159 WOLFSSL_API
    160 int wc_curve25519_set_rng(curve25519_key* key, WC_RNG* rng);
    161 #endif
    162 
    163 #ifndef WC_NO_CONSTRUCTORS
    164 WOLFSSL_API
    165 curve25519_key* wc_curve25519_new(void* heap, int devId, int *result_code);
    166 WOLFSSL_API
    167 int wc_curve25519_delete(curve25519_key* key, curve25519_key** key_p);
    168 #endif
    169 
    170 /* raw key helpers */
    171 WOLFSSL_API
    172 int wc_curve25519_import_private(const byte* priv, word32 privSz,
    173                                  curve25519_key* key);
    174 WOLFSSL_API
    175 int wc_curve25519_import_private_ex(const byte* priv, word32 privSz,
    176                                     curve25519_key* key, int endian);
    177 
    178 WOLFSSL_API
    179 int wc_curve25519_import_private_raw(const byte* priv, word32 privSz,
    180                             const byte* pub, word32 pubSz, curve25519_key* key);
    181 WOLFSSL_API
    182 int wc_curve25519_import_private_raw_ex(const byte* priv, word32 privSz,
    183                                         const byte* pub, word32 pubSz,
    184                                         curve25519_key* key, int endian);
    185 WOLFSSL_API
    186 int wc_curve25519_export_private_raw(curve25519_key* key, byte* out,
    187                                      word32* outLen);
    188 WOLFSSL_API
    189 int wc_curve25519_export_private_raw_ex(curve25519_key* key, byte* out,
    190                                         word32* outLen, int endian);
    191 
    192 WOLFSSL_API
    193 int wc_curve25519_import_public(const byte* in, word32 inLen,
    194                                 curve25519_key* key);
    195 WOLFSSL_API
    196 int wc_curve25519_import_public_ex(const byte* in, word32 inLen,
    197                                    curve25519_key* key, int endian);
    198 WOLFSSL_API
    199 int wc_curve25519_check_public(const byte* pub, word32 pubSz, int endian);
    200 
    201 WOLFSSL_API
    202 int wc_curve25519_export_public(curve25519_key* key, byte* out, word32* outLen);
    203 WOLFSSL_API
    204 int wc_curve25519_export_public_ex(curve25519_key* key, byte* out,
    205                                    word32* outLen, int endian);
    206 
    207 WOLFSSL_API
    208 int wc_curve25519_export_key_raw(curve25519_key* key,
    209                                  byte* priv, word32 *privSz,
    210                                  byte* pub, word32 *pubSz);
    211 WOLFSSL_API
    212 int wc_curve25519_export_key_raw_ex(curve25519_key* key,
    213                                     byte* priv, word32 *privSz,
    214                                     byte* pub, word32 *pubSz,
    215                                     int endian);
    216 /* size helper */
    217 WOLFSSL_API
    218 int wc_curve25519_size(curve25519_key* key);
    219 
    220 #ifdef __cplusplus
    221     }    /* extern "C" */
    222 #endif
    223 
    224 #endif /* HAVE_CURVE25519 */
    225 #endif /* WOLF_CRYPT_CURVE25519_H */