1*e7be843bSPierre Pronchery /* 2*e7be843bSPierre Pronchery * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. 3*e7be843bSPierre Pronchery * 4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use 5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy 6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at 7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html 8*e7be843bSPierre Pronchery */ 9*e7be843bSPierre Pronchery 10*e7be843bSPierre Pronchery #ifndef OSSL_INTERNAL_HPKE_UTIL_H 11*e7be843bSPierre Pronchery # define OSSL_INTERNAL_HPKE_UTIL_H 12*e7be843bSPierre Pronchery # pragma once 13*e7be843bSPierre Pronchery 14*e7be843bSPierre Pronchery /* Constants from RFC 9180 Section 7.1 and 7.3 */ 15*e7be843bSPierre Pronchery # define OSSL_HPKE_MAX_SECRET 64 16*e7be843bSPierre Pronchery # define OSSL_HPKE_MAX_PUBLIC 133 17*e7be843bSPierre Pronchery # define OSSL_HPKE_MAX_PRIVATE 66 18*e7be843bSPierre Pronchery # define OSSL_HPKE_MAX_KDF_INPUTLEN 64 19*e7be843bSPierre Pronchery 20*e7be843bSPierre Pronchery /* 21*e7be843bSPierre Pronchery * max length of a base-nonce (the Nn field from OSSL_HPKE_AEAD_INFO), this 22*e7be843bSPierre Pronchery * is used for a local stack array size 23*e7be843bSPierre Pronchery */ 24*e7be843bSPierre Pronchery # define OSSL_HPKE_MAX_NONCELEN 12 25*e7be843bSPierre Pronchery 26*e7be843bSPierre Pronchery /* 27*e7be843bSPierre Pronchery * @brief info about a KEM 28*e7be843bSPierre Pronchery * Used to store constants from Section 7.1 "Table 2 KEM IDs" 29*e7be843bSPierre Pronchery * and the bitmask for EC curves described in Section 7.1.3 DeriveKeyPair 30*e7be843bSPierre Pronchery */ 31*e7be843bSPierre Pronchery typedef struct { 32*e7be843bSPierre Pronchery uint16_t kem_id; /* code point for key encipherment method */ 33*e7be843bSPierre Pronchery const char *keytype; /* string form of algtype "EC"/"X25519"/"X448" */ 34*e7be843bSPierre Pronchery const char *groupname; /* string form of EC group for NIST curves */ 35*e7be843bSPierre Pronchery const char *mdname; /* hash alg name for the HKDF */ 36*e7be843bSPierre Pronchery size_t Nsecret; /* size of secrets */ 37*e7be843bSPierre Pronchery size_t Nenc; /* length of encapsulated key */ 38*e7be843bSPierre Pronchery size_t Npk; /* length of public key */ 39*e7be843bSPierre Pronchery size_t Nsk; /* length of raw private key */ 40*e7be843bSPierre Pronchery uint8_t bitmask; 41*e7be843bSPierre Pronchery } OSSL_HPKE_KEM_INFO; 42*e7be843bSPierre Pronchery 43*e7be843bSPierre Pronchery /* 44*e7be843bSPierre Pronchery * @brief info about a KDF 45*e7be843bSPierre Pronchery */ 46*e7be843bSPierre Pronchery typedef struct { 47*e7be843bSPierre Pronchery uint16_t kdf_id; /* code point for KDF */ 48*e7be843bSPierre Pronchery const char *mdname; /* hash alg name for the HKDF */ 49*e7be843bSPierre Pronchery size_t Nh; /* length of hash/extract output */ 50*e7be843bSPierre Pronchery } OSSL_HPKE_KDF_INFO; 51*e7be843bSPierre Pronchery 52*e7be843bSPierre Pronchery /* 53*e7be843bSPierre Pronchery * @brief info about an AEAD 54*e7be843bSPierre Pronchery */ 55*e7be843bSPierre Pronchery typedef struct { 56*e7be843bSPierre Pronchery uint16_t aead_id; /* code point for aead alg */ 57*e7be843bSPierre Pronchery const char *name; /* alg name */ 58*e7be843bSPierre Pronchery size_t taglen; /* aead tag len */ 59*e7be843bSPierre Pronchery size_t Nk; /* size of a key for this aead */ 60*e7be843bSPierre Pronchery size_t Nn; /* length of a nonce for this aead */ 61*e7be843bSPierre Pronchery } OSSL_HPKE_AEAD_INFO; 62*e7be843bSPierre Pronchery 63*e7be843bSPierre Pronchery const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_curve(const char *curve); 64*e7be843bSPierre Pronchery const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_id(uint16_t kemid); 65*e7be843bSPierre Pronchery const OSSL_HPKE_KEM_INFO *ossl_HPKE_KEM_INFO_find_random(OSSL_LIB_CTX *ctx); 66*e7be843bSPierre Pronchery const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_id(uint16_t kdfid); 67*e7be843bSPierre Pronchery const OSSL_HPKE_KDF_INFO *ossl_HPKE_KDF_INFO_find_random(OSSL_LIB_CTX *ctx); 68*e7be843bSPierre Pronchery const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_id(uint16_t aeadid); 69*e7be843bSPierre Pronchery const OSSL_HPKE_AEAD_INFO *ossl_HPKE_AEAD_INFO_find_random(OSSL_LIB_CTX *ctx); 70*e7be843bSPierre Pronchery 71*e7be843bSPierre Pronchery int ossl_hpke_kdf_extract(EVP_KDF_CTX *kctx, 72*e7be843bSPierre Pronchery unsigned char *prk, size_t prklen, 73*e7be843bSPierre Pronchery const unsigned char *salt, size_t saltlen, 74*e7be843bSPierre Pronchery const unsigned char *ikm, size_t ikmlen); 75*e7be843bSPierre Pronchery 76*e7be843bSPierre Pronchery int ossl_hpke_kdf_expand(EVP_KDF_CTX *kctx, 77*e7be843bSPierre Pronchery unsigned char *okm, size_t okmlen, 78*e7be843bSPierre Pronchery const unsigned char *prk, size_t prklen, 79*e7be843bSPierre Pronchery const unsigned char *info, size_t infolen); 80*e7be843bSPierre Pronchery 81*e7be843bSPierre Pronchery int ossl_hpke_labeled_extract(EVP_KDF_CTX *kctx, 82*e7be843bSPierre Pronchery unsigned char *prk, size_t prklen, 83*e7be843bSPierre Pronchery const unsigned char *salt, size_t saltlen, 84*e7be843bSPierre Pronchery const char *protocol_label, 85*e7be843bSPierre Pronchery const unsigned char *suiteid, size_t suiteidlen, 86*e7be843bSPierre Pronchery const char *label, 87*e7be843bSPierre Pronchery const unsigned char *ikm, size_t ikmlen); 88*e7be843bSPierre Pronchery int ossl_hpke_labeled_expand(EVP_KDF_CTX *kctx, 89*e7be843bSPierre Pronchery unsigned char *okm, size_t okmlen, 90*e7be843bSPierre Pronchery const unsigned char *prk, size_t prklen, 91*e7be843bSPierre Pronchery const char *protocol_label, 92*e7be843bSPierre Pronchery const unsigned char *suiteid, size_t suiteidlen, 93*e7be843bSPierre Pronchery const char *label, 94*e7be843bSPierre Pronchery const unsigned char *info, size_t infolen); 95*e7be843bSPierre Pronchery 96*e7be843bSPierre Pronchery EVP_KDF_CTX *ossl_kdf_ctx_create(const char *kdfname, const char *mdname, 97*e7be843bSPierre Pronchery OSSL_LIB_CTX *libctx, const char *propq); 98*e7be843bSPierre Pronchery 99*e7be843bSPierre Pronchery int ossl_hpke_str2suite(const char *suitestr, OSSL_HPKE_SUITE *suite); 100*e7be843bSPierre Pronchery #endif 101