CCF
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
7
8#include <openssl/evp.h>
9#include <openssl/kdf.h>
10#include <span>
11
12#define FMT_HEADER_ONLY
13#include <fmt/format.h>
14
15namespace ccf::crypto
16{
17 namespace OpenSSL
18 {
19 inline const EVP_MD* get_md_type(MDType type)
20 {
21 switch (type)
22 {
23 case MDType::NONE:
24 return nullptr;
25 case MDType::SHA1:
26 return EVP_sha1();
27 case MDType::SHA256:
28 return EVP_sha256();
29 case MDType::SHA384:
30 return EVP_sha384();
31 case MDType::SHA512:
32 return EVP_sha512();
33 default:
34 throw std::runtime_error("Unsupported hash algorithm");
35 }
36 }
37
38 std::vector<uint8_t> hkdf(
39 MDType md_type,
40 size_t length,
41 const std::span<const uint8_t>& ikm,
42 const std::span<const uint8_t>& salt = {},
43 const std::span<const uint8_t>& info = {});
44 }
45
46 // Hash Provider (OpenSSL)
48 {
49 public:
55 HashBytes hash(const uint8_t* data, size_t size, MDType type) const override
56 {
57 const auto* o_md_type = OpenSSL::get_md_type(type);
58 HashBytes r(EVP_MD_size(o_md_type));
59 unsigned int len = 0;
60
61 if (EVP_Digest(data, size, r.data(), &len, o_md_type, nullptr) != 1)
62 {
63 throw std::runtime_error("OpenSSL hash update error");
64 }
65
66 return r;
67 }
68 };
69
71 {
72 public:
74 ~ISha256OpenSSL() override;
75 void update_hash(std::span<const uint8_t> data) override;
76 Sha256Hash finalise() override;
77
78 protected:
79 EVP_MD_CTX* ctx = nullptr;
80 };
81
82 void openssl_sha256(const std::span<const uint8_t>& data, uint8_t* h);
83}
Definition hash_provider.h:17
Definition hash_provider.h:35
Definition hash.h:71
Sha256Hash finalise() override
Definition hash.cpp:210
EVP_MD_CTX * ctx
Definition hash.h:79
void update_hash(std::span< const uint8_t > data) override
Definition hash.cpp:196
~ISha256OpenSSL() override
Definition hash.cpp:188
ISha256OpenSSL()
Definition hash.cpp:174
HashBytes hash(const uint8_t *data, size_t size, MDType type) const override
Definition hash.h:55
Definition sha256_hash.h:16
const EVP_MD * get_md_type(MDType type)
Definition hash.h:19
Definition base64.h:11
std::vector< uint8_t > hkdf(MDType md_type, size_t length, const std::span< const uint8_t > &ikm, const std::span< const uint8_t > &salt={}, const std::span< const uint8_t > &info={})
Definition hash.cpp:51
MDType
Definition md_type.h:10
std::vector< uint8_t > HashBytes
Definition hash_bytes.h:10