CCF
Loading...
Searching...
No Matches
secret_share.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
7#include <array>
8#define FMT_HEADER_ONLY
9#include <fmt/format.h>
10#include <iostream>
11#include <openssl/crypto.h>
12#include <optional>
13#include <vector>
14
15extern "C"
16{
18 int randombytes(void* buf, size_t n)
19 {
21 entropy->random((unsigned char*)buf, n);
22 return 0;
23 }
24
25#include <sss/sss.h>
26}
27
28namespace ccf
29{
30 // The SecretSharing class provides static functions to split a secret into
31 // shares and (re-)combine those shares into the original secret.
32 // The size of the secret to share is fixed (SECRET_TO_SPLIT_LENGTH, 64
33 // bytes). It is up to the caller to either shrink the secret if it is too
34 // long. If the secret to split is shorter than SECRET_TO_SPLIT_LENGTH bytes,
35 // the caller should ignore the extra bytes.
37 {
38 public:
39 static constexpr size_t SECRET_TO_SPLIT_LENGTH = sss_MLEN;
40 static constexpr size_t SHARE_LENGTH = sss_SHARE_LEN;
41 static constexpr size_t MAX_NUMBER_SHARES = 255; // As per sss documentation
42
43 using Share = std::array<uint8_t, SHARE_LENGTH>;
44 using SplitSecret = std::array<uint8_t, SECRET_TO_SPLIT_LENGTH>;
45
46 static std::vector<Share> split(
47 const SplitSecret& secret_to_split, size_t n, size_t k)
48 {
49 if (n == 0 || n > MAX_NUMBER_SHARES)
50 {
51 throw std::logic_error(fmt::format(
52 "Share creation failed: n ({}) not in 1-{} range",
53 n,
55 }
56
57 if (k == 0 || k > n)
58 {
59 throw std::logic_error(fmt::format(
60 "Share creation failed: k not in 1-n range (k: {}, n: {})", k, n));
61 }
62
63 std::vector<Share> shares(n);
64
65 sss_create_shares(
66 reinterpret_cast<sss_Share*>(shares.data()),
67 secret_to_split.data(),
68 n,
69 k);
70
71 return shares;
72 }
73
74 static SplitSecret combine(std::vector<Share>& shares, size_t k)
75 {
76 if (k == 0 || k > shares.size())
77 {
78 throw std::logic_error(fmt::format(
79 "Share combination failed: k not in 1-n range (k: {}, n: {})",
80 k,
81 shares.size()));
82 }
83
84 SplitSecret restored_secret;
85
86 if (
87 sss_combine_shares(
88 restored_secret.data(), (sss_Share*)shares.data(), k) != 0)
89 {
90 throw std::logic_error(fmt::format(
91 "Share combination failed: {} shares may be corrupted", k));
92 }
93
94 for (auto& s : shares)
95 {
96 OPENSSL_cleanse(s.data(), s.size());
97 }
98
99 return restored_secret;
100 }
101 };
102}
Definition secret_share.h:37
std::array< uint8_t, SHARE_LENGTH > Share
Definition secret_share.h:43
static std::vector< Share > split(const SplitSecret &secret_to_split, size_t n, size_t k)
Definition secret_share.h:46
static SplitSecret combine(std::vector< Share > &shares, size_t k)
Definition secret_share.h:74
static constexpr size_t MAX_NUMBER_SHARES
Definition secret_share.h:41
static constexpr size_t SECRET_TO_SPLIT_LENGTH
Definition secret_share.h:39
static constexpr size_t SHARE_LENGTH
Definition secret_share.h:40
std::array< uint8_t, SECRET_TO_SPLIT_LENGTH > SplitSecret
Definition secret_share.h:44
EntropyPtr get_entropy()
Definition entropy.cpp:10
std::shared_ptr< Entropy > EntropyPtr
Definition entropy.h:303
Definition app_interface.h:15
int randombytes(void *buf, size_t n)
SSS assumes that there is a function of this prototype.
Definition secret_share.h:18