Debian Codegraph @

mlspp/lib/hpke/src/digest.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mlspp/lib/hpke/src/digest.cpp`/include <hpke/digest.h>cxx . . $ `<file>/mlspp/lib/hpke/include/hpke/digest.h`/
2
3 #include <openssl/evp.h>
4 #include <openssl/hmac.h>
5 #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!)
6 #include <openssl/core_names.h>
7 #endif
8
9 #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/
10
11 namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ {
12
13 static const EVP_MD*
14 openssl_digest_typecxx . . $ mlspp/hpke/openssl_digest_type(1f8ed912f6c8613e).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# digest)
15 {
16 switch (digest) {
17 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.:
18 return EVP_sha256();
19
20 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.:
21 return EVP_sha384();
22
23 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.:
24 return EVP_sha512();
25
26 default:
27 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported ciphersuite");
28 }
29 }
30
31 #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!)
32 static stdcxx . . $ std/::stringcxx . . $ std/string#
33 openssl_digest_namecxx . . $ mlspp/hpke/openssl_digest_name(4b9ac78cbe4896d0).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# digest)
34 {
35 switch (digest) {
36 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.:
37 return OSSL_DIGEST_NAME_SHA2_256cxx . . $ `<external>/16618910896700259571/core_names.h:39:9`!;
38
39 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.:
40 return OSSL_DIGEST_NAME_SHA2_384cxx . . $ `<external>/16618910896700259571/core_names.h:41:9`!;
41
42 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.:
43 return OSSL_DIGEST_NAME_SHA2_512cxx . . $ `<external>/16618910896700259571/core_names.h:42:9`!;
44
45 default:
46 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported digest algorithm");
47 }
48 }
49 #endif
50
51 template<>
52 const Digestcxx . . $ mlspp/hpke/Digest#&
53 Digestcxx . . $ mlspp/hpke/Digest#::getcxx . . $ mlspp/hpke/Digest#get(48440c38233383f7).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.>()
54 {
55 static const Digestcxx . . $ mlspp/hpke/Digest# instancecxx . . $ mlspp/hpke/Digest#Digest(d9440e2b4bfeadf0).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.);
56 return instance;
57 }
58
59 template<>
60 const Digestcxx . . $ mlspp/hpke/Digest#&
61 Digestcxx . . $ mlspp/hpke/Digest#::getcxx . . $ mlspp/hpke/Digest#get(48440c38233383f7).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.>()
62 {
63 static const Digestcxx . . $ mlspp/hpke/Digest# instancecxx . . $ mlspp/hpke/Digest#Digest(d9440e2b4bfeadf0).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.);
64 return instance;
65 }
66
67 template<>
68 const Digestcxx . . $ mlspp/hpke/Digest#&
69 Digestcxx . . $ mlspp/hpke/Digest#::getcxx . . $ mlspp/hpke/Digest#get(48440c38233383f7).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.>()
70 {
71 static const Digestcxx . . $ mlspp/hpke/Digest# instancecxx . . $ mlspp/hpke/Digest#Digest(d9440e2b4bfeadf0).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.);
72 return instance;
73 }
74
75 Digestcxx . . $ mlspp/hpke/Digest#::Digestcxx . . $ mlspp/hpke/Digest#Digest(d9440e2b4bfeadf0).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# id_in)
76 : idcxx . . $ mlspp/hpke/Digest#id.(id_in)
77 , hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.(EVP_MD_sizecxx . . $ `<external>/2497497914425334111/evp.h:566:9`!(openssl_digest_typecxx . . $ mlspp/hpke/openssl_digest_type(1f8ed912f6c8613e).(id_in)))
78 {
79 }
80
81 bytescxx . . $ mlspp/bytes_ns/bytes#
82 Digestcxx . . $ mlspp/hpke/Digest#::hashcxx . . $ mlspp/hpke/Digest#hash(c5e1b85e16e94921).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data) const
83 {
84 auto md = bytescxx . . $ mlspp/bytes_ns/bytes#(hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.);
85 unsigned int size = 0;
86 const auto* type = openssl_digest_typecxx . . $ mlspp/hpke/openssl_digest_type(1f8ed912f6c8613e).(idcxx . . $ mlspp/hpke/Digest#id.);
87 if (1 !=
88 EVP_Digest(data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(), md.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), &size, type, nullptr)) {
89 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
90 }
91
92 return mdcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
93 }
94
95 bytescxx . . $ mlspp/bytes_ns/bytes#
96 Digestcxx . . $ mlspp/hpke/Digest#::hmaccxx . . $ mlspp/hpke/Digest#hmac(802fe03ef4f7e693).(const bytescxx . . $ mlspp/bytes_ns/bytes#& key, const bytescxx . . $ mlspp/bytes_ns/bytes#& data) const
97 {
98 auto md = bytescxx . . $ mlspp/bytes_ns/bytes#(hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.);
99 unsigned int size = 0;
100 const auto* type = openssl_digest_typecxx . . $ mlspp/hpke/openssl_digest_type(1f8ed912f6c8613e).(idcxx . . $ mlspp/hpke/Digest#id.);
101 if (nullptr == HMAC(type,
102 key.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(),
103 static_cast<int>(key.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()),
104 data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(),
105 static_cast<int>(data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()),
106 md.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(),
107 &size)) {
108 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
109 }
110
111 return mdcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
112 }
113
114 bytescxx . . $ mlspp/bytes_ns/bytes#
115 Digestcxx . . $ mlspp/hpke/Digest#::hmac_for_hkdf_extractcxx . . $ mlspp/hpke/Digest#hmac_for_hkdf_extract(802fe03ef4f7e693).(const bytescxx . . $ mlspp/bytes_ns/bytes#& key, const bytescxx . . $ mlspp/bytes_ns/bytes#& data) const
116 {
117 #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!)
118 auto digest_name = openssl_digest_namecxx . . $ mlspp/hpke/openssl_digest_name(4b9ac78cbe4896d0).(idcxx . . $ mlspp/hpke/Digest#id.);
119 stdcxx . . $ std/::arraycxx . . $ std/array#<OSSL_PARAM, 2> params = {
120 OSSL_PARAM_construct_utf8_string(
121 OSSL_ALG_PARAM_DIGESTcxx . . $ `<external>/16618910896700259571/core_names.h:128:10`!, digest_name.datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).(), 0),
122 OSSL_PARAM_construct_end()
123 };
124 const auto mac =
125 make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MAC_fetch(nullptr, OSSL_MAC_NAME_HMACcxx . . $ `<external>/16618910896700259571/core_names.h:62:9`!, nullptr));
126 const auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MAC_CTX_new(mac.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()));
127 #else
128 const auto* type = openssl_digest_type(id);
129 auto ctx = make_typed_unique(HMAC_CTX_new());
130 #endif
131 if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) {
132 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
133 }
134
135 // Some FIPS-enabled libraries are overly conservative in their interpretation
136 // of NIST SP 800-131A, which requires HMAC keys to be at least 112 bits long.
137 // That document does not impose that requirement on HKDF, so we disable FIPS
138 // enforcement for purposes of HKDF.
139 //
140 // https://doi.org/10.6028/NIST.SP.800-131Ar2
141 auto key_size = static_cast<int>(key.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).());
142 // OpenSSL 3 does not support the flag EVP_MD_CTX_FLAG_NON_FIPS_ALLOW anymore.
143 // However, OpenSSL 3 in FIPS mode doesn't seem to check the HMAC key size
144 // constraint.
145 #if !defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) && !defined(WITH_BORINGSSL)
146 static const auto fips_min_hmac_key_len = 14;
147 if (FIPS_mode() != 0 && key_size < fips_min_hmac_key_len) {
148 HMAC_CTX_set_flags(ctx.get(), EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
149 }
150 #endif
151
152 // Guard against sending nullptr to HMAC_Init_ex
153 const auto* key_data = key.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).();
154 const auto non_null_zero_length_key = uint8_tcxx . . $ uint8_t#(0);
155 if (key_data == nullptr) {
156 key_data = &non_null_zero_length_key;
157 }
158
159 auto md = bytescxx . . $ mlspp/bytes_ns/bytes#(hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.);
160 #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!)
161 if (1 != EVP_MAC_init(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), key_data, key_size, params.datacxx . . $ std/array#data(258ee1c9e0a18a5c).())) {
162 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
163 }
164 if (1 != EVP_MAC_update(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).())) {
165 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
166 }
167 size_t size = 0;
168 if (1 != EVP_MAC_final(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), md.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), &size, hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.)) {
169 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
170 }
171 #else
172 if (1 != HMAC_Init_ex(ctx.get(), key_data, key_size, type, nullptr)) {
173 throw openssl_error();
174 }
175 if (1 != HMAC_Update(ctx.get(), data.data(), data.size())) {
176 throw openssl_error();
177 }
178 unsigned int size = 0;
179 if (1 != HMAC_Final(ctx.get(), md.data(), &size)) {
180 throw openssl_error();
181 }
182 #endif
183
184 return mdcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
185 }
186
187 } // namespace mlspp::hpke