hpke.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* hpke.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/hpke.h
  23. */
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/wolfcrypt/types.h>
  26. #include <wolfssl/wolfcrypt/ecc.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #if defined(HAVE_HPKE) && defined(HAVE_ECC)
  31. #ifndef WOLFCRYPT_HPKE
  32. #define WOLFCRYPT_HPKE
  33. /* KEM enum */
  34. enum {
  35. DHKEM_P256_HKDF_SHA256 = 0x0010,
  36. DHKEM_P384_HKDF_SHA384 = 0x0011,
  37. DHKEM_P521_HKDF_SHA512 = 0x0012,
  38. DHKEM_X25519_HKDF_SHA256 = 0x0020,
  39. DHKEM_X448_HKDF_SHA512 = 0x0021,
  40. };
  41. #define DHKEM_P256_ENC_LEN 65
  42. #define DHKEM_P384_ENC_LEN 97
  43. #define DHKEM_P521_ENC_LEN 133
  44. #define DHKEM_X25519_ENC_LEN 32
  45. #define DHKEM_X448_ENC_LEN 56
  46. /* KDF enum */
  47. enum {
  48. HKDF_SHA256 = 0x0001,
  49. HKDF_SHA384 = 0x0002,
  50. HKDF_SHA512 = 0x0003,
  51. };
  52. /* AEAD enum */
  53. enum {
  54. HPKE_AES_128_GCM = 0x0001,
  55. HPKE_AES_256_GCM = 0x0002,
  56. };
  57. /* TODO better way of doing this */
  58. #define HPKE_SUPPORTED_KEM_LEN 4
  59. #define HPKE_SUPPORTED_KDF_LEN 3
  60. #define HPKE_SUPPORTED_AEAD_LEN 2
  61. extern const int hpkeSupportedKem[HPKE_SUPPORTED_KEM_LEN];
  62. extern const int hpkeSupportedKdf[HPKE_SUPPORTED_KDF_LEN];
  63. extern const int hpkeSupportedAead[HPKE_SUPPORTED_AEAD_LEN];
  64. #define HPKE_Nh_MAX 64
  65. #define HPKE_Nk_MAX 32
  66. #define HPKE_Nn_MAX 12
  67. #define HPKE_Nt_MAX 16
  68. #define HPKE_Ndh_MAX 66
  69. #define HPKE_Npk_MAX 133
  70. #define HPKE_Nsecret_MAX 64
  71. #define KEM_SUITE_ID_LEN 5
  72. #define HPKE_SUITE_ID_LEN 10
  73. #ifndef MAX_HPKE_LABEL_SZ
  74. #define MAX_HPKE_LABEL_SZ 512
  75. #endif
  76. typedef struct {
  77. void* heap;
  78. word32 kem;
  79. word32 kdf;
  80. word32 aead;
  81. word32 Nh;
  82. word32 Nk;
  83. word32 Nn;
  84. word32 Nt;
  85. word32 Ndh;
  86. word32 Npk;
  87. word32 Nsecret;
  88. int kdf_digest;
  89. int curve_id;
  90. byte kem_suite_id[KEM_SUITE_ID_LEN];
  91. byte hpke_suite_id[HPKE_SUITE_ID_LEN];
  92. } Hpke;
  93. typedef struct {
  94. int seq;
  95. byte key[HPKE_Nk_MAX];
  96. byte base_nonce[HPKE_Nn_MAX];
  97. byte exporter_secret[HPKE_Nsecret_MAX];
  98. } HpkeBaseContext;
  99. WOLFSSL_API int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap);
  100. WOLFSSL_API int wc_HpkeGenerateKeyPair(Hpke* hpke, void** keypair, WC_RNG* rng);
  101. WOLFSSL_API int wc_HpkeSerializePublicKey(Hpke* hpke, void* key, byte* out,
  102. word16* outSz);
  103. WOLFSSL_API int wc_HpkeDeserializePublicKey(Hpke* hpke, void** key,
  104. const byte* in, word16 inSz);
  105. WOLFSSL_API void wc_HpkeFreeKey(Hpke* hpke, word16 kem, void* keypair,
  106. void* heap);
  107. WOLFSSL_API int wc_HpkeSealBase(Hpke* hpke, void* ephemeralKey,
  108. void* receiverKey, byte* info, word32 infoSz, byte* aad, word32 aadSz,
  109. byte* plaintext, word32 ptSz, byte* ciphertext);
  110. WOLFSSL_API int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey,
  111. const byte* pubKey, word16 pubKeySz, byte* info, word32 infoSz, byte* aad,
  112. word32 aadSz, byte* ciphertext, word32 ctSz, byte* plaintext);
  113. #endif
  114. #endif /* HAVE_HPKE && HAVE_ECC */
  115. #ifdef __cplusplus
  116. } /* extern "C" */
  117. #endif