sakke.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* sakke.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/sakke.h
  23. */
  24. #ifndef WOLF_CRYPT_SAKKE_H
  25. #define WOLF_CRYPT_SAKKE_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef WOLFCRYPT_HAVE_SAKKE
  28. #include <wolfssl/wolfcrypt/integer.h>
  29. #include <wolfssl/wolfcrypt/ecc.h>
  30. #include <wolfssl/wolfcrypt/hash.h>
  31. #include <wolfssl/wolfcrypt/hmac.h>
  32. #define WOLFCRYPT_SAKKE_KMS
  33. #define WOLFCRYPT_SAKKE_CLIENT
  34. #define SAKKE_ID_MAX_SIZE 128
  35. /* Maximum number of loops of attempting to generate a key. */
  36. #ifndef SAKKE_MAX_GEN_COUNT
  37. #define SAKKE_MAX_GEN_COUNT 10
  38. #endif
  39. /** MP integer in projective form. */
  40. typedef ecc_point mp_proj;
  41. /** SAKKE ECC parameters in usable format. */
  42. typedef struct SakkeKeyParams {
  43. /** Prime as an MP integer. */
  44. mp_int prime;
  45. /** Q (order) as an MP integer. */
  46. mp_int q;
  47. /** G (pairing base) as an MP integer. */
  48. mp_int g;
  49. /** Temporary MP integer used during operations. */
  50. mp_int a;
  51. /** Base point for elliptic curve operations as an ECC point. */
  52. ecc_point* base;
  53. /** Bit indicate prime is set as an MP integer in SAKKE key. */
  54. byte havePrime:1;
  55. /** Bit indicates q (order) is set as an MP integer in SAKKE key. */
  56. byte haveQ:1;
  57. /** Bit indicates g (pairing base) is set as an MP integer in SAKKE key. */
  58. byte haveG:1;
  59. /** Bit indicates a is set as an MP integer in SAKKE key. */
  60. byte haveA:1;
  61. /** Bit indicates base point is set as an ECC point in SAKKE key. */
  62. byte haveBase:1;
  63. } SakkeKeyParams;
  64. /** Temporary values to use in SAKKE calculations. */
  65. typedef struct SakkeKeyTmp {
  66. /** Temporary MP integer used during operations. */
  67. mp_int m1;
  68. /** Temporary MP integer used during operations. */
  69. mp_int m2;
  70. #ifdef WOLFCRYPT_SAKKE_CLIENT
  71. /** Temporary elliptic curve point for use in operations. */
  72. ecc_point* p1;
  73. /** Temporary elliptic curve point for use in operations. */
  74. ecc_point* p2;
  75. /** Temporary MP projective integer for use in operations. */
  76. mp_proj* p3;
  77. #endif
  78. } SakkeKeyTmp;
  79. #ifdef WOLFCRYPT_SAKKE_CLIENT
  80. /** SAKKE data for the intermediate point I. */
  81. typedef struct SakkeKeyPointI {
  82. /** Temporary elliptic curve point for use in operations. */
  83. ecc_point* i;
  84. /** Table associated with point I. */
  85. byte* table;
  86. /** Length of table */
  87. int tableLen;
  88. /** Identity associated with point I. */
  89. byte id[SAKKE_ID_MAX_SIZE];
  90. /** Size of identity associated with point I. */
  91. word16 idSz;
  92. } SakkeKeyPointI;
  93. /** SAKKE data for the Receiver Secret Key (RSK). */
  94. typedef struct SakkeKeyRsk {
  95. /** RSK (Receiver Secret Key). */
  96. ecc_point* rsk;
  97. /** Table associated with point I. */
  98. byte* table;
  99. /** Length of table */
  100. int tableLen;
  101. /** Indicates whether an RSK value has been set. */
  102. byte set:1;
  103. } SakkeKeyRsk;
  104. #endif
  105. /**
  106. * SAKKE key.
  107. */
  108. typedef struct SakkeKey {
  109. /** ECC key to perform elliptic curve operations with. */
  110. ecc_key ecc;
  111. /** ECC parameter in forms that can be used in computation. */
  112. SakkeKeyParams params;
  113. /** Temporaries used during calculations. */
  114. SakkeKeyTmp tmp;
  115. #ifdef WOLFCRYPT_SAKKE_CLIENT
  116. /** Data relating to the RSK (Receiver Secret Key). */
  117. SakkeKeyRsk rsk;
  118. /** Identity to perform operations with. */
  119. byte id[SAKKE_ID_MAX_SIZE];
  120. /** Size of identity in bytes. */
  121. word16 idSz;
  122. /** Data relating to the intermediate point I. */
  123. SakkeKeyPointI i;
  124. /** Generic hash algorithm object. */
  125. wc_HashAlg hash;
  126. /** Temporary buffer for use in operations. */
  127. byte data[(MAX_ECC_BYTES * 2) + 1];
  128. #endif
  129. /** Heap hint for dynamic memory allocation. */
  130. void* heap;
  131. /** Bit indicates Z, public key, is in montgomery form. */
  132. byte zMont:1;
  133. /** Bit indicate MP integers have been initialized. */
  134. byte mpInit:1;
  135. } SakkeKey;
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. WOLFSSL_API int wc_InitSakkeKey(SakkeKey* key, void* heap, int devId);
  140. WOLFSSL_API int wc_InitSakkeKey_ex(SakkeKey* key, int keySize, int curveId,
  141. void* heap, int devId);
  142. WOLFSSL_API void wc_FreeSakkeKey(SakkeKey* key);
  143. WOLFSSL_API int wc_MakeSakkeKey(SakkeKey* key, WC_RNG* rng);
  144. WOLFSSL_API int wc_MakeSakkePublicKey(SakkeKey* key, ecc_point* pub);
  145. WOLFSSL_API int wc_MakeSakkeRsk(SakkeKey* key, const byte* id, word16 idSz,
  146. ecc_point* rsk);
  147. WOLFSSL_API int wc_ValidateSakkeRsk(SakkeKey* key, const byte* id, word16 idSz,
  148. ecc_point* rsk, int* valid);
  149. WOLFSSL_API int wc_ExportSakkeKey(SakkeKey* key, byte* data, word32* sz);
  150. WOLFSSL_API int wc_ImportSakkeKey(SakkeKey* key, const byte* data, word32 sz);
  151. WOLFSSL_API int wc_ExportSakkePrivateKey(SakkeKey* key, byte* data, word32* sz);
  152. WOLFSSL_API int wc_ImportSakkePrivateKey(SakkeKey* key, const byte* data,
  153. word32 sz);
  154. WOLFSSL_API int wc_ExportSakkePublicKey(SakkeKey* key, byte* data,
  155. word32* sz, int raw);
  156. WOLFSSL_API int wc_ImportSakkePublicKey(SakkeKey* key, const byte* data,
  157. word32 sz, int trusted);
  158. WOLFSSL_API int wc_EncodeSakkeRsk(const SakkeKey* key, ecc_point* rsk,
  159. byte* out, word32* sz, int raw);
  160. WOLFSSL_API int wc_DecodeSakkeRsk(const SakkeKey* key, const byte* data,
  161. word32 sz, ecc_point* rsk);
  162. WOLFSSL_API int wc_ImportSakkeRsk(SakkeKey* key, const byte* data, word32 sz);
  163. WOLFSSL_API int wc_GetSakkeAuthSize(SakkeKey* key, word16* authSz);
  164. WOLFSSL_API int wc_SetSakkeIdentity(SakkeKey* key, const byte* id, word16 idSz);
  165. WOLFSSL_API int wc_MakeSakkePointI(SakkeKey* key, const byte* id, word16 idSz);
  166. WOLFSSL_API int wc_GetSakkePointI(SakkeKey* key, byte* data, word32* sz);
  167. WOLFSSL_API int wc_SetSakkePointI(SakkeKey* key, const byte* id, word16 idSz,
  168. const byte* data, word32 sz);
  169. WOLFSSL_API int wc_GenerateSakkePointITable(SakkeKey* key, byte* table,
  170. word32* len);
  171. WOLFSSL_API int wc_SetSakkePointITable(SakkeKey* key, byte* table, word32 len);
  172. WOLFSSL_API int wc_ClearSakkePointITable(SakkeKey* key);
  173. WOLFSSL_API int wc_MakeSakkeEncapsulatedSSV(SakkeKey* key,
  174. enum wc_HashType hashType, byte* ssv, word16 ssvSz, byte* auth,
  175. word16* authSz);
  176. WOLFSSL_API int wc_GenerateSakkeRskTable(const SakkeKey* key,
  177. const ecc_point* rsk, byte* table, word32* len);
  178. WOLFSSL_API int wc_SetSakkeRsk(SakkeKey* key, const ecc_point* rsk, byte* table,
  179. word32 len);
  180. WOLFSSL_API int wc_GenerateSakkeSSV(SakkeKey* key, WC_RNG* rng, byte* ssv,
  181. word16* ssvSz);
  182. WOLFSSL_API int wc_DeriveSakkeSSV(SakkeKey* key, enum wc_HashType hashType,
  183. byte* ssv, word16 ssvSz, const byte* auth,
  184. word16 authSz);
  185. #ifdef __cplusplus
  186. } /* extern "C" */
  187. #endif
  188. #endif /* WOLFCRYPT_HAVE_SAKKE */
  189. #endif /* WOLF_CRYPT_SAKKE_H */