| 1 | #cxx . . $ `<file>/mlspp/lib/hpke/src/hkdf.cpp`/include "hkdf.h"cxx . . $ `<file>/mlspp/lib/hpke/src/hkdf.h`/ |
| 2 | #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/ |
| 3 | |
| 4 | #include <openssl/err.h> |
| 5 | #include <openssl/evp.h> |
| 6 | #include <stdexcept> |
| 7 | |
| 8 | namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ { |
| 9 | |
| 10 | template<> |
| 11 | const HKDFcxx . . $ mlspp/hpke/HKDF#& |
| 12 | HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.>() |
| 13 | { |
| 14 | static const HKDFcxx . . $ mlspp/hpke/HKDF# instancecxx . . $ mlspp/hpke/HKDF#HKDF(98e255edfb4acc71).(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.>()); |
| 15 | return instance; |
| 16 | } |
| 17 | |
| 18 | template<> |
| 19 | const HKDFcxx . . $ mlspp/hpke/HKDF#& |
| 20 | HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.>() |
| 21 | { |
| 22 | static const HKDFcxx . . $ mlspp/hpke/HKDF# instancecxx . . $ mlspp/hpke/HKDF#HKDF(98e255edfb4acc71).(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.>()); |
| 23 | return instance; |
| 24 | } |
| 25 | |
| 26 | template<> |
| 27 | const HKDFcxx . . $ mlspp/hpke/HKDF#& |
| 28 | HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.>() |
| 29 | { |
| 30 | static const HKDFcxx . . $ mlspp/hpke/HKDF# instancecxx . . $ mlspp/hpke/HKDF#HKDF(98e255edfb4acc71).(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.>()); |
| 31 | return instance; |
| 32 | } |
| 33 | |
| 34 | static KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID# |
| 35 | digest_to_kdfcxx . . $ mlspp/hpke/digest_to_kdf(ef12f7a01fe00ee7).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# digest_id) |
| 36 | { |
| 37 | switch (digest_id) { |
| 38 | case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.: |
| 39 | return KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA256cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA256.; |
| 40 | case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.: |
| 41 | return KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA384cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA384.; |
| 42 | case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.: |
| 43 | return KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA512cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA512.; |
| 44 | } |
| 45 | |
| 46 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 47 | } |
| 48 | |
| 49 | HKDFcxx . . $ mlspp/hpke/HKDF#::HKDFcxx . . $ mlspp/hpke/HKDF#HKDF(98e255edfb4acc71).(const Digestcxx . . $ mlspp/hpke/Digest#& digest_in) |
| 50 | : KDFcxx . . $ mlspp/hpke/KDF#(digest_to_kdfcxx . . $ mlspp/hpke/digest_to_kdf(ef12f7a01fe00ee7).(digest_in.idcxx . . $ mlspp/hpke/Digest#id.), digest_in.hash_sizecxx . . $ mlspp/hpke/Digest#hash_size.) |
| 51 | , digestcxx . . $ mlspp/hpke/HKDF#digest.(digest_in) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 56 | HKDFcxx . . $ mlspp/hpke/HKDF#::extractcxx . . $ mlspp/hpke/HKDF#extract(802fe03ef4f7e693).(const bytescxx . . $ mlspp/bytes_ns/bytes#& salt, const bytescxx . . $ mlspp/bytes_ns/bytes#& ikm) const |
| 57 | { |
| 58 | return digestcxx . . $ mlspp/hpke/HKDF#digest..hmac_for_hkdf_extractcxx . . $ mlspp/hpke/Digest#hmac_for_hkdf_extract(802fe03ef4f7e693).(salt, ikm); |
| 59 | } |
| 60 | |
| 61 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 62 | HKDFcxx . . $ mlspp/hpke/HKDF#::expandcxx . . $ mlspp/hpke/HKDF#expand(fc2505b904ce7e90).(const bytescxx . . $ mlspp/bytes_ns/bytes#& prk, const bytescxx . . $ mlspp/bytes_ns/bytes#& info, size_t size) const |
| 63 | { |
| 64 | auto okm = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{}; |
| 65 | auto i = uint8_tcxx . . $ uint8_t#(0x00); |
| 66 | auto Ti = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).{}; |
| 67 | while (okm.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() < size) { |
| 68 | i += 1; |
| 69 | auto block = Ti +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). info +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). bytescxx . . $ mlspp/bytes_ns/bytes#bytes(162c049f8cb8078a).{ i }; |
| 70 | |
| 71 | Ti =cxx . . $ mlspp/bytes_ns/bytes#`operator=`(463b53a2545785ed). digestcxx . . $ mlspp/hpke/HKDF#digest..hmaccxx . . $ mlspp/hpke/Digest#hmac(802fe03ef4f7e693).(prk, block); |
| 72 | okm +=cxx . . $ mlspp/bytes_ns/bytes#`operator+=`(89adb9997e4a49c). Ti; |
| 73 | } |
| 74 | |
| 75 | okm.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(size); |
| 76 | return okmcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 77 | } |
| 78 | |
| 79 | } // namespace mlspp::hpke |