curve25519.h 5.9 KB

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