eccsi.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eccsi.h
  2. *
  3. * Copyright (C) 2006-2023 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/eccsi.h
  23. */
  24. #ifndef WOLF_CRYPT_ECCSI_H
  25. #define WOLF_CRYPT_ECCSI_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef WOLFCRYPT_HAVE_ECCSI
  28. #include <wolfssl/wolfcrypt/wolfmath.h>
  29. #include <wolfssl/wolfcrypt/ecc.h>
  30. #include <wolfssl/wolfcrypt/hash.h>
  31. #include <wolfssl/wolfcrypt/hmac.h>
  32. #define WOLFCRYPT_ECCSI_KMS
  33. #define WOLFCRYPT_ECCSI_CLIENT
  34. #define MAX_ECCSI_BYTES (256 / 8)
  35. /* Maximum number of loops of attempting to generate key pairs and signatures.
  36. */
  37. #ifndef ECCSI_MAX_GEN_COUNT
  38. #define ECCSI_MAX_GEN_COUNT 10
  39. #endif
  40. typedef struct EccsiKeyParams {
  41. /** Order (q) of elliptic curve as an MP integer. */
  42. mp_int order;
  43. #ifdef WOLFCRYPT_ECCSI_CLIENT
  44. /** A parameter of elliptic curve as an MP integer. */
  45. mp_int a;
  46. /** P parameter of elliptic curve as an MP integer. */
  47. mp_int b;
  48. /** Prime of elliptic curve as an MP integer. */
  49. mp_int prime;
  50. #endif
  51. /** Base point for elliptic curve operations as an ECC point. */
  52. ecc_point* base;
  53. /** Bit indicates order (q) is set as an MP integer in ECCSI key. */
  54. byte haveOrder:1;
  55. /** Bit indicates A is set as an MP integer in ECCSI key. */
  56. byte haveA:1;
  57. /** Bit indicates B is set as an MP integer in ECCSI key. */
  58. byte haveB:1;
  59. /** Bit indicates prime is set as an MP integer in ECCSI key. */
  60. byte havePrime:1;
  61. /** Bit indicates base point is set as an MP integer in ECCSI key. */
  62. byte haveBase:1;
  63. } EccsiKeyParams;
  64. /**
  65. * ECCSI key.
  66. */
  67. typedef struct EccsiKey {
  68. /** ECC key to perform elliptic curve operations with. */
  69. ecc_key ecc;
  70. /** ECC key to perform public key elliptic curve operations with. */
  71. ecc_key pubkey;
  72. /** ECC parameter in forms that can be used in computation. */
  73. EccsiKeyParams params;
  74. #ifdef WOLFCRYPT_ECCSI_CLIENT
  75. /** Temporary MP integer used during operations.. */
  76. mp_int tmp;
  77. /** Secret Signing Key */
  78. mp_int ssk;
  79. /** Public Validation Token (PVT) */
  80. ecc_point* pvt;
  81. #endif
  82. /** Generic hash algorithm object. */
  83. wc_HashAlg hash;
  84. /** Temporary buffer for use in operations. */
  85. byte data[(MAX_ECCSI_BYTES * 2) + 1];
  86. #ifdef WOLFCRYPT_ECCSI_CLIENT
  87. /** Hash of identity - used in signing/verification. */
  88. byte idHash[WC_MAX_DIGEST_SIZE];
  89. /** Size of hash of identity in bytes. */
  90. byte idHashSz;
  91. #endif
  92. /** Heap hint for dynamic memory allocation. */
  93. void* heap;
  94. /** Bit indicates KPAK (public key) is in montgomery form. */
  95. word16 kpakMont:1;
  96. } EccsiKey;
  97. #ifdef __cplusplus
  98. extern "C" {
  99. #endif
  100. WOLFSSL_API int wc_InitEccsiKey(EccsiKey* key, void* heap, int devId);
  101. WOLFSSL_API int wc_InitEccsiKey_ex(EccsiKey* key, int keySz, int curveId,
  102. void* heap, int devId);
  103. WOLFSSL_API void wc_FreeEccsiKey(EccsiKey* key);
  104. WOLFSSL_API int wc_MakeEccsiKey(EccsiKey* key, WC_RNG* rng);
  105. WOLFSSL_API int wc_MakeEccsiPair(EccsiKey* key, WC_RNG* rng,
  106. enum wc_HashType hashType, const byte* id, word32 idSz, mp_int* ssk,
  107. ecc_point* pvt);
  108. WOLFSSL_API int wc_ValidateEccsiPair(EccsiKey* key, enum wc_HashType hashType,
  109. const byte* id, word32 idSz, const mp_int* ssk, ecc_point* pvt,
  110. int* valid);
  111. WOLFSSL_API int wc_ValidateEccsiPvt(EccsiKey* key, const ecc_point* pvt,
  112. int* valid);
  113. WOLFSSL_API int wc_EncodeEccsiPair(const EccsiKey* key, mp_int* ssk,
  114. ecc_point* pvt, byte* data, word32* sz);
  115. WOLFSSL_API int wc_EncodeEccsiSsk(const EccsiKey* key, mp_int* ssk, byte* data,
  116. word32* sz);
  117. WOLFSSL_API int wc_EncodeEccsiPvt(const EccsiKey* key, ecc_point* pvt,
  118. byte* data, word32* sz, int raw);
  119. WOLFSSL_API int wc_DecodeEccsiPair(const EccsiKey* key, const byte* data,
  120. word32 sz, mp_int* ssk, ecc_point* pvt);
  121. WOLFSSL_API int wc_DecodeEccsiSsk(const EccsiKey* key, const byte* data,
  122. word32 sz, mp_int* ssk);
  123. WOLFSSL_API int wc_DecodeEccsiPvt(const EccsiKey* key, const byte* data,
  124. word32 sz, ecc_point* pvt);
  125. WOLFSSL_API int wc_DecodeEccsiPvtFromSig(const EccsiKey* key, const byte* sig,
  126. word32 sz, ecc_point* pvt);
  127. WOLFSSL_API int wc_ExportEccsiKey(EccsiKey* key, byte* data, word32* sz);
  128. WOLFSSL_API int wc_ImportEccsiKey(EccsiKey* key, const byte* data, word32 sz);
  129. WOLFSSL_API int wc_ExportEccsiPrivateKey(EccsiKey* key, byte* data, word32* sz);
  130. WOLFSSL_API int wc_ImportEccsiPrivateKey(EccsiKey* key, const byte* data,
  131. word32 sz);
  132. WOLFSSL_API int wc_ExportEccsiPublicKey(EccsiKey* key, byte* data, word32* sz,
  133. int raw);
  134. WOLFSSL_API int wc_ImportEccsiPublicKey(EccsiKey* key, const byte* data,
  135. word32 sz, int trusted);
  136. WOLFSSL_API int wc_HashEccsiId(EccsiKey* key, enum wc_HashType hashType,
  137. const byte* id, word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz);
  138. WOLFSSL_API int wc_SetEccsiHash(EccsiKey* key, const byte* hash, byte hashSz);
  139. WOLFSSL_API int wc_SetEccsiPair(EccsiKey* key, const mp_int* ssk,
  140. const ecc_point* pvt);
  141. WOLFSSL_API int wc_SignEccsiHash(EccsiKey* key, WC_RNG* rng,
  142. enum wc_HashType hashType, const byte* msg, word32 msgSz, byte* sig,
  143. word32* sigSz);
  144. WOLFSSL_API int wc_VerifyEccsiHash(EccsiKey* key, enum wc_HashType hashType,
  145. const byte* msg, word32 msgSz, const byte* sig, word32 sigSz,
  146. int* verified);
  147. #ifdef __cplusplus
  148. } /* extern "C" */
  149. #endif
  150. #endif /* WOLFCRYPT_HAVE_ECCSI */
  151. #endif /* WOLF_CRYPT_ECCSI_H */