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 return nullptr;
37 }
38
39 std::vector<uint8_t> hkdf(
40 MDType md_type,
41 size_t length,
42 const std::span<const uint8_t>& ikm,
43 const std::span<const uint8_t>& salt = {},
44 const std::span<const uint8_t>& info = {});
45 }
46
47 // Hash Provider (OpenSSL)
49 {
50 public:
56 virtual HashBytes Hash(const uint8_t* data, size_t size, MDType type) const
57 {
58 auto o_md_type = OpenSSL::get_md_type(type);
59 HashBytes r(EVP_MD_size(o_md_type));
60 unsigned int len = 0;
61
62 if (EVP_Digest(data, size, r.data(), &len, o_md_type, NULL) != 1)
63 throw std::runtime_error("OpenSSL hash update error");
64
65 return r;
66 }
67 };
68
70 {
71 public:
74 virtual void update_hash(std::span<const uint8_t> data);
75 virtual Sha256Hash finalise();
76
77 protected:
78 EVP_MD_CTX* ctx = nullptr;
79 };
80
81 void openssl_sha256(const std::span<const uint8_t>& data, uint8_t* h);
82}
Definition hash_provider.h:17
Definition hash_provider.h:35
Definition hash.h:70
EVP_MD_CTX * ctx
Definition hash.h:78
virtual Sha256Hash finalise()
Definition hash.cpp:168
virtual void update_hash(std::span< const uint8_t > data)
Definition hash.cpp:154
~ISha256OpenSSL()
Definition hash.cpp:146
ISha256OpenSSL()
Definition hash.cpp:132
virtual HashBytes Hash(const uint8_t *data, size_t size, MDType type) const
Definition hash.h:56
Definition sha256_hash.h:16
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:17
const EVP_MD * get_md_type(MDType type)
Definition hash.h:19
Definition base64.h:10
void openssl_sha256(const std::span< const uint8_t > &data, uint8_t *h)
Definition hash.cpp:101
std::vector< uint8_t > HashBytes
Definition hash_bytes.h:10
MDType
Definition md_type.h:10