CCF
Loading...
Searching...
No Matches
ledger_secret.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
6#include "ccf/crypto/hmac.h"
8#include "kv/kv_types.h"
11
12#include <openssl/crypto.h>
13
14namespace ccf
15{
16 // Unique label for deriving commit secrets from ledger secrets.
17 // See logic in get_commit_secret() below for derivation implementation.
18 // It is important that this label is _not_ re-used for other purposes,
19 // nor changed during the lifetime of a ledger, to preserve the semantics of
20 // commit evidence (i.e. their reveal implies an entry is committed).
21 static constexpr uint8_t commit_secret_label_[] = {
22 'C', 'o', 'm', 'm', 'i', 't', ' ', 'S'};
23 static std::span<const uint8_t> commit_secret_label{
24 commit_secret_label_, sizeof(commit_secret_label_)};
26 {
27 std::vector<uint8_t> raw_key;
28 std::shared_ptr<ccf::crypto::KeyAesGcm> key;
29 std::optional<ccf::kv::Version> previous_secret_stored_version =
30 std::nullopt;
32
34 std::span<const uint8_t> raw_key)
35 {
36 return ccf::crypto::hmac(
37 ccf::crypto::MDType::SHA256, raw_key, commit_secret_label);
38 }
39
40 [[nodiscard]] const ccf::crypto::HashBytes& get_commit_secret() const
41 {
42 return commit_secret;
43 }
44
45 bool operator==(const LedgerSecret& other) const
46 {
47 return raw_key == other.raw_key &&
49 }
50
52
54 {
55 OPENSSL_cleanse(raw_key.data(), raw_key.size());
56 OPENSSL_cleanse(commit_secret.data(), commit_secret.size());
57 }
58
59 // The copy constructor is used for serialising a LedgerSecret. However,
60 // only the raw_key is serialised and other.key is nullptr so use raw_key to
61 // seed key.
68
70 std::vector<uint8_t>&& raw_key_,
71 std::optional<ccf::kv::Version> previous_secret_stored_version_ =
72 std::nullopt) :
73 raw_key(raw_key_),
74 key(ccf::crypto::make_key_aes_gcm(std::move(raw_key_))),
75 previous_secret_stored_version(previous_secret_stored_version_),
77 {}
78 };
79
82 DECLARE_JSON_OPTIONAL_FIELDS(LedgerSecret, previous_secret_stored_version);
83
84 using LedgerSecretPtr = std::shared_ptr<LedgerSecret>;
85
87 {
88 return std::make_shared<LedgerSecret>(
90 }
91
92 inline std::vector<uint8_t> decrypt_previous_ledger_secret_raw(
93 const LedgerSecretPtr& ledger_secret,
94 const std::vector<uint8_t>& encrypted_previous_secret_raw)
95 {
96 ccf::crypto::GcmCipher encrypted_ls;
97 encrypted_ls.deserialise(encrypted_previous_secret_raw);
98 std::vector<uint8_t> decrypted_ls_raw;
99
100 if (!ledger_secret->key->decrypt(
101 encrypted_ls.hdr.get_iv(),
102 encrypted_ls.hdr.tag,
103 encrypted_ls.cipher,
104 {},
105 decrypted_ls_raw))
106 {
107 throw std::logic_error("Decryption of previous ledger secret failed");
108 }
109
110 return decrypted_ls_raw;
111 }
112}
113
114namespace nlohmann
115{
116 template <>
117 struct adl_serializer<ccf::LedgerSecretPtr>
118 {
119 static void to_json(json& j, const ccf::LedgerSecretPtr& s)
120 {
121 if (s.get() != nullptr)
122 {
123 j = *s;
124 }
125 else
126 {
127 j = nullptr;
128 }
129 }
130
131 static void from_json(const json& j, ccf::LedgerSecretPtr& s)
132 {
133 if (j.is_null())
134 {
135 s = nullptr;
136 }
137 else
138 {
139 ccf::LedgerSecret ls = j;
140 s = std::make_shared<ccf::LedgerSecret>(ls);
141 }
142 }
143 };
144}
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:736
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:712
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:811
HashBytes hmac(MDType type, const std::span< const uint8_t > &key, const std::span< const uint8_t > &data)
Definition hmac.cpp:43
std::vector< uint8_t > HashBytes
Definition hash_bytes.h:10
EntropyPtr get_entropy()
Definition entropy.cpp:10
constexpr size_t GCM_DEFAULT_KEY_SIZE
Definition symmetric_key.h:12
Definition app_interface.h:13
LedgerSecretPtr make_ledger_secret()
Definition ledger_secret.h:86
std::shared_ptr< LedgerSecret > LedgerSecretPtr
Definition ledger_secret.h:84
std::vector< uint8_t > decrypt_previous_ledger_secret_raw(const LedgerSecretPtr &ledger_secret, const std::vector< uint8_t > &encrypted_previous_secret_raw)
Definition ledger_secret.h:92
Definition json_schema.h:15
Definition ledger_secret.h:115
STL namespace.
Definition ledger_secret.h:26
LedgerSecret(const LedgerSecret &other)
Definition ledger_secret.h:62
LedgerSecret(std::vector< uint8_t > &&raw_key_, std::optional< ccf::kv::Version > previous_secret_stored_version_=std::nullopt)
Definition ledger_secret.h:69
std::optional< ccf::kv::Version > previous_secret_stored_version
Definition ledger_secret.h:29
const ccf::crypto::HashBytes & get_commit_secret() const
Definition ledger_secret.h:40
std::shared_ptr< ccf::crypto::KeyAesGcm > key
Definition ledger_secret.h:28
LedgerSecret()
Definition ledger_secret.h:51
~LedgerSecret()
Definition ledger_secret.h:53
bool operator==(const LedgerSecret &other) const
Definition ledger_secret.h:45
std::vector< uint8_t > raw_key
Definition ledger_secret.h:27
ccf::crypto::HashBytes commit_secret
Definition ledger_secret.h:31
static ccf::crypto::HashBytes derive_commit_secret(std::span< const uint8_t > raw_key)
Definition ledger_secret.h:33
Definition symmetric_key.h:58
void deserialise(const std::vector< uint8_t > &serial)
Definition symmetric_key.cpp:91
StandardGcmHeader hdr
Definition symmetric_key.h:59
std::vector< uint8_t > cipher
Definition symmetric_key.h:60
uint8_t tag[GCM_SIZE_TAG]
Definition symmetric_key.h:18
std::span< const uint8_t > get_iv() const
Definition symmetric_key.cpp:32
static void to_json(json &j, const ccf::LedgerSecretPtr &s)
Definition ledger_secret.h:119
static void from_json(const json &j, ccf::LedgerSecretPtr &s)
Definition ledger_secret.h:131