Botan 3.6.1
Crypto and TLS for C&
dilithium_keys.h
Go to the documentation of this file.
1/*
2 * Crystals Dilithium Internal Key Types
3 *
4 * (C) 2021-2024 Jack Lloyd
5 * 2021-2023 Jack Lloyd
6 * 2021-2022 Manuel Glaser - Rohde & Schwarz Cybersecurity
7 * 2021-2023 Michael Boric, René Meusel - Rohde & Schwarz Cybersecurity
8 * 2024 René Meusel - Rohde & Schwarz Cybersecurity
9 *
10 * Botan is released under the Simplified BSD License (see license.txt)
11 */
12
13#ifndef BOTAN_DILITHIUM_INTERNAL_KEYS_H_
14#define BOTAN_DILITHIUM_INTERNAL_KEYS_H_
15
16#include <botan/internal/dilithium_types.h>
17
18#include <botan/internal/dilithium_algos.h>
19#include <botan/internal/dilithium_symmetric_primitives.h>
20
21namespace Botan {
22
24 public:
25 static std::unique_ptr<Dilithium_Keypair_Codec> create(DilithiumMode mode);
26
27 virtual ~Dilithium_Keypair_Codec() = default;
29 virtual DilithiumInternalKeypair decode_keypair(std::span<const uint8_t> private_key,
30 DilithiumConstants mode) const = 0;
31};
32
34 public:
35 static std::shared_ptr<Dilithium_PublicKeyInternal> decode(
38 return std::make_shared<Dilithium_PublicKeyInternal>(std::move(mode), std::move(rho), std::move(t1));
39 }
40
42 m_mode(std::move(mode)),
43 m_rho(std::move(rho)),
44 m_t1(std::move(t1)),
45 m_tr(m_mode.symmetric_primitives().H(raw_pk())) {
46 BOTAN_ASSERT_NOMSG(!m_rho.empty());
47 BOTAN_ASSERT_NOMSG(m_t1.size() > 0);
48 }
49
50 public:
52
53 const DilithiumHashedPublicKey& tr() const { return m_tr; }
54
55 const DilithiumPolyVec& t1() const { return m_t1; }
56
57 const DilithiumSeedRho& rho() const { return m_rho; }
58
59 const DilithiumConstants& mode() const { return m_mode; }
60
61 private:
62 const DilithiumConstants m_mode;
63 DilithiumSeedRho m_rho;
66};
67
69 public:
71 std::optional<DilithiumSeedRandomness> seed,
76 m_mode(std::move(mode)),
77 m_seed(std::move(seed)),
78 m_signing_seed(std::move(signing_seed)),
79 m_t0(std::move(t0)),
80 m_s1(std::move(s1)),
81 m_s2(std::move(s2)) {}
82
83 public:
84 const DilithiumConstants& mode() const { return m_mode; }
85
86 const std::optional<DilithiumSeedRandomness>& seed() const { return m_seed; }
87
88 const DilithiumSigningSeedK& signing_seed() const { return m_signing_seed; }
89
90 const DilithiumPolyVec& s1() const { return m_s1; }
91
92 const DilithiumPolyVec& s2() const { return m_s2; }
93
94 const DilithiumPolyVec& t0() const { return m_t0; }
95
96 void _const_time_poison() const {
97 // Note: m_rho and m_tr is public knowledge
98 CT::poison_all(m_signing_seed, m_s1, m_s2, m_t0);
99 if(m_seed.has_value()) {
100 CT::poison(m_seed.value());
101 }
102 }
103
104 void _const_time_unpoison() const {
105 CT::unpoison_all(m_signing_seed, m_s1, m_s2, m_t0);
106 if(m_seed.has_value()) {
107 CT::unpoison(m_seed.value());
108 }
109 }
110
111 private:
112 const DilithiumConstants m_mode;
113 std::optional<DilithiumSeedRandomness> m_seed;
114 DilithiumSigningSeedK m_signing_seed;
115 DilithiumPolyVec m_t0;
116 DilithiumPolyVec m_s1;
117 DilithiumPolyVec m_s2;
118};
119
120} // namespace Botan
121
122#endif
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
virtual secure_vector< uint8_t > encode_keypair(DilithiumInternalKeypair keypair) const =0
virtual DilithiumInternalKeypair decode_keypair(std::span< const uint8_t > private_key, DilithiumConstants mode) const =0
virtual ~Dilithium_Keypair_Codec()=default
static std::unique_ptr< Dilithium_Keypair_Codec > create(DilithiumMode mode)
const std::optional< DilithiumSeedRandomness > & seed() const
const DilithiumPolyVec & s1() const
const DilithiumPolyVec & s2() const
Dilithium_PrivateKeyInternal(DilithiumConstants mode, std::optional< DilithiumSeedRandomness > seed, DilithiumSigningSeedK signing_seed, DilithiumPolyVec s1, DilithiumPolyVec s2, DilithiumPolyVec t0)
const DilithiumConstants & mode() const
const DilithiumPolyVec & t0() const
const DilithiumSigningSeedK & signing_seed() const
static std::shared_ptr< Dilithium_PublicKeyInternal > decode(DilithiumConstants mode, StrongSpan< const DilithiumSerializedPublicKey > raw_pk)
Dilithium_PublicKeyInternal(DilithiumConstants mode, DilithiumSeedRho rho, DilithiumPolyVec t1)
const DilithiumSeedRho & rho() const
const DilithiumPolyVec & t1() const
const DilithiumConstants & mode() const
const DilithiumHashedPublicKey & tr() const
DilithiumSerializedPublicKey raw_pk() const
constexpr void unpoison_all(Ts &&... ts)
Definition ct_utils.h:201
constexpr void poison_all(Ts &&... ts)
Definition ct_utils.h:195
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53
DilithiumSerializedPublicKey encode_public_key(StrongSpan< const DilithiumSeedRho > rho, const DilithiumPolyVec &t1, const DilithiumConstants &mode)
std::pair< DilithiumSeedRho, DilithiumPolyVec > decode_public_key(StrongSpan< const DilithiumSerializedPublicKey > pk, const DilithiumConstants &mode)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
std::pair< std::shared_ptr< Dilithium_PublicKeyInternal >, std::shared_ptr< Dilithium_PrivateKeyInternal > > DilithiumInternalKeypair
Internal representation of a Dilithium key pair.