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