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 static constexpr auto commit_secret_label_ = "Commit Secret Label";
17
19 {
20 std::vector<uint8_t> raw_key;
21 std::shared_ptr<ccf::crypto::KeyAesGcm> key;
22 std::optional<ccf::kv::Version> previous_secret_stored_version =
23 std::nullopt;
24 std::optional<ccf::crypto::HashBytes> commit_secret = std::nullopt;
25
27 {
28 if (!commit_secret.has_value())
29 {
32 raw_key,
33 {commit_secret_label_,
34 commit_secret_label_ + sizeof(commit_secret_label_)});
35 }
36 return commit_secret.value();
37 }
38
39 bool operator==(const LedgerSecret& other) const
40 {
41 return raw_key == other.raw_key &&
43 }
44
45 LedgerSecret() = default;
46
48 {
49 OPENSSL_cleanse(raw_key.data(), raw_key.size());
50 }
51
52 // The copy constructor is used for serialising a LedgerSecret. However,
53 // only the raw_key is serialised and other.key is nullptr so use raw_key to
54 // seed key.
55 LedgerSecret(const LedgerSecret& other) :
56 raw_key(other.raw_key),
57 key(ccf::crypto::make_key_aes_gcm(other.raw_key)),
59 {}
60
62 std::vector<uint8_t>&& raw_key_,
63 std::optional<ccf::kv::Version> previous_secret_stored_version_ =
64 std::nullopt) :
65 raw_key(raw_key_),
66 key(ccf::crypto::make_key_aes_gcm(std::move(raw_key_))),
67 previous_secret_stored_version(previous_secret_stored_version_)
68 {}
69 };
70
72 DECLARE_JSON_REQUIRED_FIELDS(LedgerSecret, raw_key)
73 DECLARE_JSON_OPTIONAL_FIELDS(LedgerSecret, previous_secret_stored_version)
74
75 using LedgerSecretPtr = std::shared_ptr<LedgerSecret>;
76
78 {
79 return std::make_shared<LedgerSecret>(
81 }
82
83 inline std::vector<uint8_t> decrypt_previous_ledger_secret_raw(
84 const LedgerSecretPtr& ledger_secret,
85 const std::vector<uint8_t>& encrypted_previous_secret_raw)
86 {
87 ccf::crypto::GcmCipher encrypted_ls;
88 encrypted_ls.deserialise(encrypted_previous_secret_raw);
89 std::vector<uint8_t> decrypted_ls_raw;
90
91 if (!ledger_secret->key->decrypt(
92 encrypted_ls.hdr.get_iv(),
93 encrypted_ls.hdr.tag,
94 encrypted_ls.cipher,
95 {},
96 decrypted_ls_raw))
97 {
98 throw std::logic_error("Decryption of previous ledger secret failed");
99 }
100
101 return decrypted_ls_raw;
102 }
103}
104
105namespace nlohmann
106{
107 template <>
108 struct adl_serializer<ccf::LedgerSecretPtr>
109 {
110 static void to_json(json& j, const ccf::LedgerSecretPtr& s)
111 {
112 if (s.get())
113 {
114 j = *s;
115 }
116 else
117 {
118 j = nullptr;
119 }
120 }
121
122 static void from_json(const json& j, ccf::LedgerSecretPtr& s)
123 {
124 if (j.is_null())
125 {
126 s = nullptr;
127 }
128 else
129 {
130 ccf::LedgerSecret ls = j;
131 s = std::make_shared<ccf::LedgerSecret>(ls);
132 }
133 }
134 };
135}
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:714
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:690
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:786
HashBytes hmac(MDType type, const std::vector< uint8_t > &key, const std::vector< 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:14
LedgerSecretPtr make_ledger_secret()
Definition ledger_secret.h:77
std::shared_ptr< LedgerSecret > LedgerSecretPtr
Definition ledger_secret.h:75
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:83
Definition json_schema.h:15
Definition ledger_secret.h:106
STL namespace.
Definition ledger_secret.h:19
LedgerSecret(const LedgerSecret &other)
Definition ledger_secret.h:55
LedgerSecret(std::vector< uint8_t > &&raw_key_, std::optional< ccf::kv::Version > previous_secret_stored_version_=std::nullopt)
Definition ledger_secret.h:61
LedgerSecret()=default
std::optional< ccf::crypto::HashBytes > commit_secret
Definition ledger_secret.h:24
const ccf::crypto::HashBytes & get_commit_secret()
Definition ledger_secret.h:26
std::optional< ccf::kv::Version > previous_secret_stored_version
Definition ledger_secret.h:22
std::shared_ptr< ccf::crypto::KeyAesGcm > key
Definition ledger_secret.h:21
~LedgerSecret()
Definition ledger_secret.h:47
bool operator==(const LedgerSecret &other) const
Definition ledger_secret.h:39
std::vector< uint8_t > raw_key
Definition ledger_secret.h:20
Definition symmetric_key.h:57
void deserialise(const std::vector< uint8_t > &serial)
Definition symmetric_key.cpp:93
StandardGcmHeader hdr
Definition symmetric_key.h:58
std::vector< uint8_t > cipher
Definition symmetric_key.h:59
uint8_t tag[GCM_SIZE_TAG]
Definition symmetric_key.h:18
std::span< const uint8_t > get_iv() const
Definition symmetric_key.cpp:34
static void to_json(json &j, const ccf::LedgerSecretPtr &s)
Definition ledger_secret.h:110
static void from_json(const json &j, ccf::LedgerSecretPtr &s)
Definition ledger_secret.h:122