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