Botan 3.9.0
Crypto and TLS for C&
Botan::SM4 Class Referencefinal

#include <sm4.h>

Inheritance diagram for Botan::SM4:
Botan::Block_Cipher_Fixed_Params< 16, 16 > Botan::BlockCipher Botan::SymmetricAlgorithm

Public Types

enum  

Public Member Functions

size_t block_size () const final
void clear () override
BlockCipherclone () const
BlockCipherclone () const
void decrypt (const uint8_t in[], uint8_t out[]) const
void decrypt (std::span< const uint8_t > in, std::span< uint8_t > out) const
void decrypt (std::span< uint8_t > block) const
void decrypt (uint8_t block[]) const
void decrypt (const uint8_t in[], uint8_t out[]) const
void decrypt (std::span< const uint8_t > in, std::span< uint8_t > out) const
void decrypt (std::span< uint8_t > block) const
void decrypt (uint8_t block[]) const
void decrypt_n (const uint8_t in[], uint8_t out[], size_t blocks) const override
void decrypt_n_xex (uint8_t data[], const uint8_t mask[], size_t blocks) const
void decrypt_n_xex (uint8_t data[], const uint8_t mask[], size_t blocks) const
void encrypt (const uint8_t in[], uint8_t out[]) const
void encrypt (std::span< const uint8_t > in, std::span< uint8_t > out) const
void encrypt (std::span< uint8_t > block) const
void encrypt (uint8_t block[]) const
void encrypt (const uint8_t in[], uint8_t out[]) const
void encrypt (std::span< const uint8_t > in, std::span< uint8_t > out) const
void encrypt (std::span< uint8_t > block) const
void encrypt (uint8_t block[]) const
void encrypt_n (const uint8_t in[], uint8_t out[], size_t blocks) const override
void encrypt_n_xex (uint8_t data[], const uint8_t mask[], size_t blocks) const
void encrypt_n_xex (uint8_t data[], const uint8_t mask[], size_t blocks) const
bool has_keying_material () const override
Key_Length_Specification key_spec () const final
size_t maximum_keylength () const
size_t maximum_keylength () const
size_t minimum_keylength () const
size_t minimum_keylength () const
std::string name () const override
std::unique_ptr< BlockCiphernew_object () const override
size_t parallel_bytes () const
size_t parallel_bytes () const
size_t parallelism () const override
std::string provider () const override
void set_key (const OctetString &key)
void set_key (const uint8_t key[], size_t length)
void set_key (std::span< const uint8_t > key)
void set_key (const OctetString &key)
void set_key (const uint8_t key[], size_t length)
void set_key (std::span< const uint8_t > key)
bool valid_keylength (size_t length) const
bool valid_keylength (size_t length) const

Static Public Member Functions

static std::unique_ptr< BlockCiphercreate (std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< BlockCiphercreate (std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< BlockCiphercreate_or_throw (std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< BlockCiphercreate_or_throw (std::string_view algo_spec, std::string_view provider="")
static std::vector< std::string > providers (std::string_view algo_spec)
static std::vector< std::string > providers (std::string_view algo_spec)

Static Public Attributes

static constexpr size_t ParallelismMult
static constexpr size_t ParallelismMult = 4

Protected Member Functions

void assert_key_material_set () const
void assert_key_material_set (bool predicate) const
void assert_key_material_set () const
void assert_key_material_set (bool predicate) const

Detailed Description

SM4

Definition at line 19 of file sm4.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited

Definition at line 212 of file block_cipher.h.

212{ BLOCK_SIZE = BS }; /* NOLINT(*-enum-size) */

Member Function Documentation

◆ assert_key_material_set() [1/4]

◆ assert_key_material_set() [2/4]

void Botan::SymmetricAlgorithm::assert_key_material_set ( bool predicate) const
inlineprotectedinherited

Definition at line 148 of file sym_algo.h.

148 {
149 if(!predicate) {
151 }
152 }

◆ assert_key_material_set() [3/4]

◆ assert_key_material_set() [4/4]

void Botan::SymmetricAlgorithm::assert_key_material_set ( bool predicate) const
inlineprotectedinherited

Definition at line 148 of file sym_algo.h.

148 {
149 if(!predicate) {
150 throw_key_not_set_error();
151 }
152 }

◆ block_size()

size_t Botan::Block_Cipher_Fixed_Params< BS, KMIN, 0, 1, BlockCipher >::block_size ( ) const
inlinefinalvirtualinherited
Returns
block size of this algorithm

Implements Botan::BlockCipher.

Definition at line 214 of file block_cipher.h.

214{ return BS; }

◆ clear()

void Botan::SM4::clear ( )
overridevirtual

Reset the internal state. This includes not just the key, but any partial message that may have been in process.

Implements Botan::SymmetricAlgorithm.

Definition at line 337 of file sm4.cpp.

337 {
338 zap(m_RK);
339}
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:134

References Botan::zap().

◆ clone() [1/2]

BlockCipher * Botan::BlockCipher::clone ( ) const
inlineinherited

Definition at line 188 of file block_cipher.h.

188{ return this->new_object().release(); }
virtual std::unique_ptr< BlockCipher > new_object() const=0

◆ clone() [2/2]

BlockCipher * Botan::BlockCipher::clone ( ) const
inlineinherited

Definition at line 188 of file block_cipher.h.

188{ return this->new_object().release(); }

References new_object().

◆ create() [1/2]

std::unique_ptr< BlockCipher > Botan::BlockCipher::create ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to choose
Returns
a null pointer if the algo/provider combination cannot be found

Definition at line 31 of file block_cipher.cpp.

92 {
93#if defined(BOTAN_HAS_COMMONCRYPTO)
94 if(provider.empty() || provider == "commoncrypto") {
96 return bc;
97
98 if(!provider.empty())
99 return nullptr;
100 }
101#endif
102
103 // TODO: CryptoAPI
104 // TODO: /dev/crypto
105
106 // Only base providers from here on out
107 if(provider.empty() == false && provider != "base") {
108 return nullptr;
109 }
110
111#if defined(BOTAN_HAS_AES)
112 if(algo == "AES-128") {
114 }
115
116 if(algo == "AES-192") {
118 }
119
120 if(algo == "AES-256") {
122 }
123#endif
124
125#if defined(BOTAN_HAS_ARIA)
126 if(algo == "ARIA-128") {
128 }
129
130 if(algo == "ARIA-192") {
132 }
133
134 if(algo == "ARIA-256") {
136 }
137#endif
138
139#if defined(BOTAN_HAS_SERPENT)
140 if(algo == "Serpent") {
142 }
143#endif
144
145#if defined(BOTAN_HAS_SHACAL2)
146 if(algo == "SHACAL2") {
148 }
149#endif
150
151#if defined(BOTAN_HAS_TWOFISH)
152 if(algo == "Twofish") {
154 }
155#endif
156
157#if defined(BOTAN_HAS_THREEFISH_512)
158 if(algo == "Threefish-512") {
160 }
161#endif
162
163#if defined(BOTAN_HAS_BLOWFISH)
164 if(algo == "Blowfish") {
166 }
167#endif
168
169#if defined(BOTAN_HAS_CAMELLIA)
170 if(algo == "Camellia-128") {
172 }
173
174 if(algo == "Camellia-192") {
176 }
177
178 if(algo == "Camellia-256") {
180 }
181#endif
182
183#if defined(BOTAN_HAS_DES)
184 if(algo == "DES") {
185 return std::make_unique<DES>();
186 }
187
188 if(algo == "TripleDES" || algo == "3DES" || algo == "DES-EDE") {
190 }
191#endif
192
193#if defined(BOTAN_HAS_NOEKEON)
194 if(algo == "Noekeon") {
196 }
197#endif
198
199#if defined(BOTAN_HAS_CAST_128)
200 if(algo == "CAST-128" || algo == "CAST5") {
202 }
203#endif
204
205#if defined(BOTAN_HAS_IDEA)
206 if(algo == "IDEA") {
207 return std::make_unique<IDEA>();
208 }
209#endif
210
211#if defined(BOTAN_HAS_KUZNYECHIK)
212 if(algo == "Kuznyechik") {
214 }
215#endif
216
217#if defined(BOTAN_HAS_SEED)
218 if(algo == "SEED") {
219 return std::make_unique<SEED>();
220 }
221#endif
222
223#if defined(BOTAN_HAS_SM4)
224 if(algo == "SM4") {
225 return std::make_unique<SM4>();
226 }
227#endif
228
229 const SCAN_Name req(algo);
230
231#if defined(BOTAN_HAS_GOST_28147_89)
232 if(req.algo_name() == "GOST-28147-89") {
233 return std::make_unique<GOST_28147_89>(req.arg(0, "R3411_94_TestParam"));
234 }
235#endif
236
237#if defined(BOTAN_HAS_CASCADE)
238 if(req.algo_name() == "Cascade" && req.arg_count() == 2) {
239 auto c1 = BlockCipher::create(req.arg(0));
240 auto c2 = BlockCipher::create(req.arg(1));
241
242 if(c1 && c2) {
244 }
245 }
246#endif
247
248#if defined(BOTAN_HAS_LION)
249 if(req.algo_name() == "Lion" && req.arg_count_between(2, 3)) {
250 auto hash = HashFunction::create(req.arg(0));
251 auto stream = StreamCipher::create(req.arg(1));
252
253 if(hash && stream) {
254 const size_t block_size = req.arg_as_integer(2, 1024);
256 }
257 }
258#endif
259
262
263 return nullptr;
264}
#define BOTAN_UNUSED
Definition assert.h:144
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:107
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
std::unique_ptr< BlockCipher > make_commoncrypto_block_cipher(std::string_view name)

◆ create() [2/2]

std::unique_ptr< BlockCipher > Botan::BlockCipher::create ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to choose
Returns
a null pointer if the algo/provider combination cannot be found

Definition at line 92 of file block_cipher.cpp.

92 {
93#if defined(BOTAN_HAS_COMMONCRYPTO)
94 if(provider.empty() || provider == "commoncrypto") {
95 if(auto bc = make_commoncrypto_block_cipher(algo))
96 return bc;
97
98 if(!provider.empty())
99 return nullptr;
100 }
101#endif
102
103 // TODO: CryptoAPI
104 // TODO: /dev/crypto
105
106 // Only base providers from here on out
107 if(provider.empty() == false && provider != "base") {
108 return nullptr;
109 }
110
111#if defined(BOTAN_HAS_AES)
112 if(algo == "AES-128") {
113 return std::make_unique<AES_128>();
114 }
115
116 if(algo == "AES-192") {
117 return std::make_unique<AES_192>();
118 }
119
120 if(algo == "AES-256") {
121 return std::make_unique<AES_256>();
122 }
123#endif
124
125#if defined(BOTAN_HAS_ARIA)
126 if(algo == "ARIA-128") {
127 return std::make_unique<ARIA_128>();
128 }
129
130 if(algo == "ARIA-192") {
131 return std::make_unique<ARIA_192>();
132 }
133
134 if(algo == "ARIA-256") {
135 return std::make_unique<ARIA_256>();
136 }
137#endif
138
139#if defined(BOTAN_HAS_SERPENT)
140 if(algo == "Serpent") {
141 return std::make_unique<Serpent>();
142 }
143#endif
144
145#if defined(BOTAN_HAS_SHACAL2)
146 if(algo == "SHACAL2") {
147 return std::make_unique<SHACAL2>();
148 }
149#endif
150
151#if defined(BOTAN_HAS_TWOFISH)
152 if(algo == "Twofish") {
153 return std::make_unique<Twofish>();
154 }
155#endif
156
157#if defined(BOTAN_HAS_THREEFISH_512)
158 if(algo == "Threefish-512") {
159 return std::make_unique<Threefish_512>();
160 }
161#endif
162
163#if defined(BOTAN_HAS_BLOWFISH)
164 if(algo == "Blowfish") {
165 return std::make_unique<Blowfish>();
166 }
167#endif
168
169#if defined(BOTAN_HAS_CAMELLIA)
170 if(algo == "Camellia-128") {
171 return std::make_unique<Camellia_128>();
172 }
173
174 if(algo == "Camellia-192") {
175 return std::make_unique<Camellia_192>();
176 }
177
178 if(algo == "Camellia-256") {
179 return std::make_unique<Camellia_256>();
180 }
181#endif
182
183#if defined(BOTAN_HAS_DES)
184 if(algo == "DES") {
185 return std::make_unique<DES>();
186 }
187
188 if(algo == "TripleDES" || algo == "3DES" || algo == "DES-EDE") {
189 return std::make_unique<TripleDES>();
190 }
191#endif
192
193#if defined(BOTAN_HAS_NOEKEON)
194 if(algo == "Noekeon") {
195 return std::make_unique<Noekeon>();
196 }
197#endif
198
199#if defined(BOTAN_HAS_CAST_128)
200 if(algo == "CAST-128" || algo == "CAST5") {
201 return std::make_unique<CAST_128>();
202 }
203#endif
204
205#if defined(BOTAN_HAS_IDEA)
206 if(algo == "IDEA") {
207 return std::make_unique<IDEA>();
208 }
209#endif
210
211#if defined(BOTAN_HAS_KUZNYECHIK)
212 if(algo == "Kuznyechik") {
213 return std::make_unique<Kuznyechik>();
214 }
215#endif
216
217#if defined(BOTAN_HAS_SEED)
218 if(algo == "SEED") {
219 return std::make_unique<SEED>();
220 }
221#endif
222
223#if defined(BOTAN_HAS_SM4)
224 if(algo == "SM4") {
225 return std::make_unique<SM4>();
226 }
227#endif
228
229 const SCAN_Name req(algo);
230
231#if defined(BOTAN_HAS_GOST_28147_89)
232 if(req.algo_name() == "GOST-28147-89") {
233 return std::make_unique<GOST_28147_89>(req.arg(0, "R3411_94_TestParam"));
234 }
235#endif
236
237#if defined(BOTAN_HAS_CASCADE)
238 if(req.algo_name() == "Cascade" && req.arg_count() == 2) {
239 auto c1 = BlockCipher::create(req.arg(0));
240 auto c2 = BlockCipher::create(req.arg(1));
241
242 if(c1 && c2) {
243 return std::make_unique<Cascade_Cipher>(std::move(c1), std::move(c2));
244 }
245 }
246#endif
247
248#if defined(BOTAN_HAS_LION)
249 if(req.algo_name() == "Lion" && req.arg_count_between(2, 3)) {
250 auto hash = HashFunction::create(req.arg(0));
251 auto stream = StreamCipher::create(req.arg(1));
252
253 if(hash && stream) {
254 const size_t block_size = req.arg_as_integer(2, 1024);
255 return std::make_unique<Lion>(std::move(hash), std::move(stream), block_size);
256 }
257 }
258#endif
259
260 BOTAN_UNUSED(req);
262
263 return nullptr;
264}
virtual size_t block_size() const =0
virtual std::string provider() const

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), Botan::SCAN_Name::arg_count_between(), block_size(), BOTAN_UNUSED, create(), Botan::HashFunction::create(), Botan::StreamCipher::create(), Botan::make_commoncrypto_block_cipher(), and provider().

Referenced by botan_block_cipher_init(), Botan::AEAD_Mode::create(), create(), Botan::Cipher_Mode::create(), Botan::MessageAuthenticationCode::create(), Botan::StreamCipher::create(), and create_or_throw().

◆ create_or_throw() [1/2]

std::unique_ptr< BlockCipher > Botan::BlockCipher::create_or_throw ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name, or throw if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 38 of file block_cipher.cpp.

267 {
268 if(auto bc = BlockCipher::create(algo, provider)) {
269 return bc;
270 }
271 throw Lookup_Error("Block cipher", algo, provider);
272}

◆ create_or_throw() [2/2]

std::unique_ptr< BlockCipher > Botan::BlockCipher::create_or_throw ( std::string_view algo_spec,
std::string_view provider = "" )
staticinherited

Create an instance based on a name, or throw if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 267 of file block_cipher.cpp.

267 {
268 if(auto bc = BlockCipher::create(algo, provider)) {
269 return bc;
270 }
271 throw Lookup_Error("Block cipher", algo, provider);
272}

References create(), and provider().

Referenced by botan_nist_kw_dec(), botan_nist_kw_enc(), Botan::TLS::Connection_Cipher_State::Connection_Cipher_State(), Botan::Encrypted_PSK_Database::Encrypted_PSK_Database(), Botan::rfc3394_keyunwrap(), and Botan::rfc3394_keywrap().

◆ decrypt() [1/8]

void Botan::BlockCipher::decrypt ( const uint8_t in[],
uint8_t out[] ) const
inlineinherited

Decrypt a block.

Parameters
inThe ciphertext block to be decypted as a byte array. Must be of length block_size().
outThe byte array designated to hold the decrypted block. Must be of length block_size().

Definition at line 91 of file block_cipher.h.

91{ decrypt_n(in, out, 1); }
virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const=0

◆ decrypt() [2/8]

void Botan::BlockCipher::decrypt ( std::span< const uint8_t > in,
std::span< uint8_t > out ) const
inlineinherited

Decrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)

Definition at line 139 of file block_cipher.h.

139 {
140 return decrypt_n(in.data(), out.data(), in.size() / block_size());
141 }

◆ decrypt() [3/8]

void Botan::BlockCipher::decrypt ( std::span< uint8_t > block) const
inlineinherited

Decrypt one or more blocks

Parameters
blockthe input/output buffer (multiple of block_size())

Definition at line 121 of file block_cipher.h.

121 {
122 return decrypt_n(block.data(), block.data(), block.size() / block_size());
123 }

◆ decrypt() [4/8]

void Botan::BlockCipher::decrypt ( uint8_t block[]) const
inlineinherited

Decrypt a block.

Parameters
blockthe ciphertext block to be decrypted Must be of length block_size(). Will hold the result when the function has finished.

Definition at line 107 of file block_cipher.h.

107{ decrypt_n(block, block, 1); }

◆ decrypt() [5/8]

void Botan::BlockCipher::decrypt ( const uint8_t in[],
uint8_t out[] ) const
inlineinherited

Decrypt a block.

Parameters
inThe ciphertext block to be decypted as a byte array. Must be of length block_size().
outThe byte array designated to hold the decrypted block. Must be of length block_size().

Definition at line 91 of file block_cipher.h.

91{ decrypt_n(in, out, 1); }

References decrypt_n().

Referenced by Botan::nist_key_unwrap(), and Botan::nist_key_unwrap_padded().

◆ decrypt() [6/8]

void Botan::BlockCipher::decrypt ( std::span< const uint8_t > in,
std::span< uint8_t > out ) const
inlineinherited

Decrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)

Definition at line 139 of file block_cipher.h.

139 {
140 return decrypt_n(in.data(), out.data(), in.size() / block_size());
141 }

References block_size(), and decrypt_n().

◆ decrypt() [7/8]

void Botan::BlockCipher::decrypt ( std::span< uint8_t > block) const
inlineinherited

Decrypt one or more blocks

Parameters
blockthe input/output buffer (multiple of block_size())

Definition at line 121 of file block_cipher.h.

121 {
122 return decrypt_n(block.data(), block.data(), block.size() / block_size());
123 }

References block_size(), and decrypt_n().

◆ decrypt() [8/8]

void Botan::BlockCipher::decrypt ( uint8_t block[]) const
inlineinherited

Decrypt a block.

Parameters
blockthe ciphertext block to be decrypted Must be of length block_size(). Will hold the result when the function has finished.

Definition at line 107 of file block_cipher.h.

107{ decrypt_n(block, block, 1); }

References decrypt_n().

◆ decrypt_n()

void Botan::SM4::decrypt_n ( const uint8_t in[],
uint8_t out[],
size_t blocks ) const
overridevirtual

Decrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)
blocksthe number of blocks to process

Implements Botan::BlockCipher.

Definition at line 237 of file sm4.cpp.

237 {
239
240#if defined(BOTAN_HAS_SM4_ARMV8)
242 return sm4_armv8_decrypt(in, out, blocks);
243 }
244#endif
245
246#if defined(BOTAN_HAS_SM4_X86)
248 return sm4_x86_decrypt(in, out, blocks);
249 }
250#endif
251
252#if defined(BOTAN_HAS_SM4_GFNI)
254 return sm4_gfni_decrypt(in, out, blocks);
255 }
256#endif
257
258 while(blocks >= 2) {
259 uint32_t B0 = load_be<uint32_t>(in, 0);
260 uint32_t B1 = load_be<uint32_t>(in, 1);
261 uint32_t B2 = load_be<uint32_t>(in, 2);
262 uint32_t B3 = load_be<uint32_t>(in, 3);
263
264 uint32_t C0 = load_be<uint32_t>(in, 4);
265 uint32_t C1 = load_be<uint32_t>(in, 5);
266 uint32_t C2 = load_be<uint32_t>(in, 6);
267 uint32_t C3 = load_be<uint32_t>(in, 7);
268
269 SM4_D<7>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T_slow);
270 SM4_D<6>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
271 SM4_D<5>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
272 SM4_D<4>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
273 SM4_D<3>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
274 SM4_D<2>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
275 SM4_D<1>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
276 SM4_D<0>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T_slow);
277
278 store_be(out, B3, B2, B1, B0, C3, C2, C1, C0);
279
280 in += 2 * BLOCK_SIZE;
281 out += 2 * BLOCK_SIZE;
282 blocks -= 2;
283 }
284
285 for(size_t i = 0; i != blocks; ++i) {
286 uint32_t B0 = load_be<uint32_t>(in, 0);
287 uint32_t B1 = load_be<uint32_t>(in, 1);
288 uint32_t B2 = load_be<uint32_t>(in, 2);
289 uint32_t B3 = load_be<uint32_t>(in, 3);
290
291 SM4_D<7>(B0, B1, B2, B3, m_RK, SM4_T_slow);
292 SM4_D<6>(B0, B1, B2, B3, m_RK, SM4_T);
293 SM4_D<5>(B0, B1, B2, B3, m_RK, SM4_T);
294 SM4_D<4>(B0, B1, B2, B3, m_RK, SM4_T);
295 SM4_D<3>(B0, B1, B2, B3, m_RK, SM4_T);
296 SM4_D<2>(B0, B1, B2, B3, m_RK, SM4_T);
297 SM4_D<1>(B0, B1, B2, B3, m_RK, SM4_T);
298 SM4_D<0>(B0, B1, B2, B3, m_RK, SM4_T_slow);
299
300 store_be(out, B3, B2, B1, B0);
301
302 in += BLOCK_SIZE;
303 out += BLOCK_SIZE;
304 }
305}
static bool has(CPUID::Feature feat)
Definition cpuid.h:94
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::Block_Cipher_Fixed_Params< 16, 16 >::assert_key_material_set(), Botan::Block_Cipher_Fixed_Params< 16, 16 >::BLOCK_SIZE, Botan::CPUFeature::GFNI, Botan::CPUID::has(), Botan::load_be(), Botan::CPUFeature::SM4, and Botan::store_be().

◆ decrypt_n_xex() [1/2]

void Botan::BlockCipher::decrypt_n_xex ( uint8_t data[],
const uint8_t mask[],
size_t blocks ) const
inlineinherited

Definition at line 172 of file block_cipher.h.

172 {
173 const size_t BS = block_size();
174 for(size_t i = 0; i != blocks * BS; ++i) {
175 data[i] ^= mask[i];
176 }
178 for(size_t i = 0; i != blocks * BS; ++i) {
179 data[i] ^= mask[i];
180 }
181 }

◆ decrypt_n_xex() [2/2]

void Botan::BlockCipher::decrypt_n_xex ( uint8_t data[],
const uint8_t mask[],
size_t blocks ) const
inlineinherited

Definition at line 172 of file block_cipher.h.

172 {
173 const size_t BS = block_size();
174 for(size_t i = 0; i != blocks * BS; ++i) {
175 data[i] ^= mask[i];
176 }
177 decrypt_n(data, data, blocks);
178 for(size_t i = 0; i != blocks * BS; ++i) {
179 data[i] ^= mask[i];
180 }
181 }

References block_size(), decrypt_n(), and decrypt_n_xex().

Referenced by decrypt_n_xex().

◆ encrypt() [1/8]

void Botan::BlockCipher::encrypt ( const uint8_t in[],
uint8_t out[] ) const
inlineinherited

Encrypt a block.

Parameters
inThe plaintext block to be encrypted as a byte array. Must be of length block_size().
outThe byte array designated to hold the encrypted block. Must be of length block_size().

Definition at line 82 of file block_cipher.h.

82{ encrypt_n(in, out, 1); }
virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const=0

◆ encrypt() [2/8]

void Botan::BlockCipher::encrypt ( std::span< const uint8_t > in,
std::span< uint8_t > out ) const
inlineinherited

Encrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)

Definition at line 130 of file block_cipher.h.

130 {
131 return encrypt_n(in.data(), out.data(), in.size() / block_size());
132 }

◆ encrypt() [3/8]

void Botan::BlockCipher::encrypt ( std::span< uint8_t > block) const
inlineinherited

Encrypt one or more blocks

Parameters
blockthe input/output buffer (multiple of block_size())

Definition at line 113 of file block_cipher.h.

113 {
114 return encrypt_n(block.data(), block.data(), block.size() / block_size());
115 }

◆ encrypt() [4/8]

void Botan::BlockCipher::encrypt ( uint8_t block[]) const
inlineinherited

Encrypt a block.

Parameters
blockthe plaintext block to be encrypted Must be of length block_size(). Will hold the result when the function has finished.

Definition at line 99 of file block_cipher.h.

99{ encrypt_n(block, block, 1); }

◆ encrypt() [5/8]

void Botan::BlockCipher::encrypt ( const uint8_t in[],
uint8_t out[] ) const
inlineinherited

Encrypt a block.

Parameters
inThe plaintext block to be encrypted as a byte array. Must be of length block_size().
outThe byte array designated to hold the encrypted block. Must be of length block_size().

Definition at line 82 of file block_cipher.h.

82{ encrypt_n(in, out, 1); }

References encrypt_n().

Referenced by Botan::nist_key_wrap(), Botan::nist_key_wrap_padded(), and Botan::CFB_Mode::shift_register().

◆ encrypt() [6/8]

void Botan::BlockCipher::encrypt ( std::span< const uint8_t > in,
std::span< uint8_t > out ) const
inlineinherited

Encrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)

Definition at line 130 of file block_cipher.h.

130 {
131 return encrypt_n(in.data(), out.data(), in.size() / block_size());
132 }

References block_size(), and encrypt_n().

◆ encrypt() [7/8]

void Botan::BlockCipher::encrypt ( std::span< uint8_t > block) const
inlineinherited

Encrypt one or more blocks

Parameters
blockthe input/output buffer (multiple of block_size())

Definition at line 113 of file block_cipher.h.

113 {
114 return encrypt_n(block.data(), block.data(), block.size() / block_size());
115 }

References block_size(), and encrypt_n().

◆ encrypt() [8/8]

void Botan::BlockCipher::encrypt ( uint8_t block[]) const
inlineinherited

Encrypt a block.

Parameters
blockthe plaintext block to be encrypted Must be of length block_size(). Will hold the result when the function has finished.

Definition at line 99 of file block_cipher.h.

99{ encrypt_n(block, block, 1); }

References encrypt_n().

◆ encrypt_n()

void Botan::SM4::encrypt_n ( const uint8_t in[],
uint8_t out[],
size_t blocks ) const
overridevirtual

Encrypt one or more blocks

Parameters
inthe input buffer (multiple of block_size())
outthe output buffer (same size as in)
blocksthe number of blocks to process

Implements Botan::BlockCipher.

Definition at line 164 of file sm4.cpp.

164 {
166
167#if defined(BOTAN_HAS_SM4_ARMV8)
169 return sm4_armv8_encrypt(in, out, blocks);
170 }
171#endif
172
173#if defined(BOTAN_HAS_SM4_X86)
175 return sm4_x86_encrypt(in, out, blocks);
176 }
177#endif
178
179#if defined(BOTAN_HAS_SM4_GFNI)
181 return sm4_gfni_encrypt(in, out, blocks);
182 }
183#endif
184
185 while(blocks >= 2) {
186 uint32_t B0 = load_be<uint32_t>(in, 0);
187 uint32_t B1 = load_be<uint32_t>(in, 1);
188 uint32_t B2 = load_be<uint32_t>(in, 2);
189 uint32_t B3 = load_be<uint32_t>(in, 3);
190
191 uint32_t C0 = load_be<uint32_t>(in, 4);
192 uint32_t C1 = load_be<uint32_t>(in, 5);
193 uint32_t C2 = load_be<uint32_t>(in, 6);
194 uint32_t C3 = load_be<uint32_t>(in, 7);
195
196 SM4_E<0>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T_slow);
197 SM4_E<1>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
198 SM4_E<2>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
199 SM4_E<3>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
200 SM4_E<4>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
201 SM4_E<5>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
202 SM4_E<6>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T);
203 SM4_E<7>(B0, B1, B2, B3, C0, C1, C2, C3, m_RK, SM4_T_slow);
204
205 store_be(out, B3, B2, B1, B0, C3, C2, C1, C0);
206
207 in += 2 * BLOCK_SIZE;
208 out += 2 * BLOCK_SIZE;
209 blocks -= 2;
210 }
211
212 for(size_t i = 0; i != blocks; ++i) {
213 uint32_t B0 = load_be<uint32_t>(in, 0);
214 uint32_t B1 = load_be<uint32_t>(in, 1);
215 uint32_t B2 = load_be<uint32_t>(in, 2);
216 uint32_t B3 = load_be<uint32_t>(in, 3);
217
218 SM4_E<0>(B0, B1, B2, B3, m_RK, SM4_T_slow);
219 SM4_E<1>(B0, B1, B2, B3, m_RK, SM4_T);
220 SM4_E<2>(B0, B1, B2, B3, m_RK, SM4_T);
221 SM4_E<3>(B0, B1, B2, B3, m_RK, SM4_T);
222 SM4_E<4>(B0, B1, B2, B3, m_RK, SM4_T);
223 SM4_E<5>(B0, B1, B2, B3, m_RK, SM4_T);
224 SM4_E<6>(B0, B1, B2, B3, m_RK, SM4_T);
225 SM4_E<7>(B0, B1, B2, B3, m_RK, SM4_T_slow);
226
227 store_be(out, B3, B2, B1, B0);
228
229 in += BLOCK_SIZE;
230 out += BLOCK_SIZE;
231 }
232}

References Botan::Block_Cipher_Fixed_Params< 16, 16 >::assert_key_material_set(), Botan::Block_Cipher_Fixed_Params< 16, 16 >::BLOCK_SIZE, Botan::CPUFeature::GFNI, Botan::CPUID::has(), Botan::load_be(), Botan::CPUFeature::SM4, and Botan::store_be().

◆ encrypt_n_xex() [1/2]

void Botan::BlockCipher::encrypt_n_xex ( uint8_t data[],
const uint8_t mask[],
size_t blocks ) const
inlineinherited

Definition at line 160 of file block_cipher.h.

160 {
161 const size_t BS = block_size();
162 for(size_t i = 0; i != blocks * BS; ++i) {
163 data[i] ^= mask[i];
164 }
166 for(size_t i = 0; i != blocks * BS; ++i) {
167 data[i] ^= mask[i];
168 }
169 }

◆ encrypt_n_xex() [2/2]

void Botan::BlockCipher::encrypt_n_xex ( uint8_t data[],
const uint8_t mask[],
size_t blocks ) const
inlineinherited

Definition at line 160 of file block_cipher.h.

160 {
161 const size_t BS = block_size();
162 for(size_t i = 0; i != blocks * BS; ++i) {
163 data[i] ^= mask[i];
164 }
165 encrypt_n(data, data, blocks);
166 for(size_t i = 0; i != blocks * BS; ++i) {
167 data[i] ^= mask[i];
168 }
169 }

References block_size(), encrypt_n(), and encrypt_n_xex().

Referenced by encrypt_n_xex().

◆ has_keying_material()

bool Botan::SM4::has_keying_material ( ) const
overridevirtual
Returns
true if a key has been set on this object

Implements Botan::SymmetricAlgorithm.

Definition at line 307 of file sm4.cpp.

307 {
308 return !m_RK.empty();
309}

◆ key_spec()

Key_Length_Specification Botan::Block_Cipher_Fixed_Params< BS, KMIN, 0, 1, BlockCipher >::key_spec ( ) const
inlinefinalvirtualinherited
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 216 of file block_cipher.h.

◆ maximum_keylength() [1/2]

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 102 of file sym_algo.h.

102{ return key_spec().maximum_keylength(); }
Key_Length_Specification key_spec() const final
size_t maximum_keylength() const
Definition sym_algo.h:56

◆ maximum_keylength() [2/2]

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 102 of file sym_algo.h.

102{ return key_spec().maximum_keylength(); }
virtual Key_Length_Specification key_spec() const =0

References key_spec().

◆ minimum_keylength() [1/2]

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 107 of file sym_algo.h.

107{ return key_spec().minimum_keylength(); }
size_t minimum_keylength() const
Definition sym_algo.h:51

◆ minimum_keylength() [2/2]

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 107 of file sym_algo.h.

107{ return key_spec().minimum_keylength(); }

References key_spec().

◆ name()

std::string Botan::SM4::name ( ) const
inlineoverridevirtual
Returns
the algorithm name

Implements Botan::SymmetricAlgorithm.

Definition at line 26 of file sm4.h.

26{ return "SM4"; }

◆ new_object()

std::unique_ptr< BlockCipher > Botan::SM4::new_object ( ) const
inlineoverridevirtual
Returns
new object representing the same algorithm as *this

Implements Botan::BlockCipher.

Definition at line 28 of file sm4.h.

28{ return std::make_unique<SM4>(); }

◆ parallel_bytes() [1/2]

size_t Botan::BlockCipher::parallel_bytes ( ) const
inlineinherited
Returns
prefererred parallelism of this cipher in bytes

Definition at line 67 of file block_cipher.h.

◆ parallel_bytes() [2/2]

size_t Botan::BlockCipher::parallel_bytes ( ) const
inlineinherited
Returns
prefererred parallelism of this cipher in bytes

Definition at line 67 of file block_cipher.h.

static constexpr size_t ParallelismMult

References block_size(), parallelism(), and ParallelismMult.

Referenced by Botan::CBC_Mode::ideal_granularity().

◆ parallelism()

size_t Botan::SM4::parallelism ( ) const
overridevirtual
Returns
native parallelism of this cipher in blocks

Reimplemented from Botan::BlockCipher.

Definition at line 341 of file sm4.cpp.

341 {
342#if defined(BOTAN_HAS_SM4_ARMV8)
344 return 4;
345 }
346#endif
347
348#if defined(BOTAN_HAS_SM4_GFNI)
350 return 8;
351 }
352#endif
353
354 return 1;
355}

References Botan::CPUFeature::GFNI, Botan::CPUID::has(), and Botan::CPUFeature::SM4.

◆ provider()

std::string Botan::SM4::provider ( ) const
overridevirtual
Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2", "openssl", or some other arbitrary string.

Reimplemented from Botan::BlockCipher.

Definition at line 357 of file sm4.cpp.

357 {
358#if defined(BOTAN_HAS_SM4_ARMV8)
359 if(auto feat = CPUID::check(CPUID::Feature::SM4)) {
360 return *feat;
361 }
362#endif
363
364#if defined(BOTAN_HAS_SM4_GFNI)
365 if(auto feat = CPUID::check(CPUID::Feature::GFNI)) {
366 return *feat;
367 }
368#endif
369
370 return "base";
371}
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:67

References Botan::CPUID::check(), Botan::CPUFeature::GFNI, and Botan::CPUFeature::SM4.

◆ providers() [1/2]

std::vector< std::string > Botan::BlockCipher::providers ( std::string_view algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 44 of file block_cipher.cpp.

274 {
275 return probe_providers_of<BlockCipher>(algo, {"base", "commoncrypto"});
276}
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:105

◆ providers() [2/2]

std::vector< std::string > Botan::BlockCipher::providers ( std::string_view algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 274 of file block_cipher.cpp.

274 {
275 return probe_providers_of<BlockCipher>(algo, {"base", "commoncrypto"});
276}

References Botan::probe_providers_of().

◆ set_key() [1/6]

void Botan::SymmetricAlgorithm::set_key ( const OctetString & key)
inherited

Set the symmetric key of this object.

Parameters
keythe SymmetricKey to be set.

Definition at line 120 of file sym_algo.cpp.

14 {
15 set_key(std::span{key.begin(), key.length()});
16}
void set_key(const OctetString &key)
Definition sym_algo.cpp:14

◆ set_key() [2/6]

void Botan::SymmetricAlgorithm::set_key ( const uint8_t key[],
size_t length )
inlineinherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 133 of file sym_algo.h.

◆ set_key() [3/6]

void Botan::SymmetricAlgorithm::set_key ( std::span< const uint8_t > key)
inherited

Set the symmetric key of this object.

Parameters
keythe contiguous byte range to be set.

Definition at line 126 of file sym_algo.cpp.

22 {
23 if(!valid_keylength(key.size())) {
24 throw Invalid_Key_Length(name(), key.size());
25 }
27}
bool valid_keylength(size_t length) const
Definition sym_algo.h:114
virtual std::string name() const=0

◆ set_key() [4/6]

◆ set_key() [5/6]

void Botan::SymmetricAlgorithm::set_key ( const uint8_t key[],
size_t length )
inlineinherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 133 of file sym_algo.h.

133{ set_key(std::span{key, length}); }

References set_key().

Referenced by set_key().

◆ set_key() [6/6]

void Botan::SymmetricAlgorithm::set_key ( std::span< const uint8_t > key)
inherited

Set the symmetric key of this object.

Parameters
keythe contiguous byte range to be set.

Definition at line 22 of file sym_algo.cpp.

22 {
23 if(!valid_keylength(key.size())) {
24 throw Invalid_Key_Length(name(), key.size());
25 }
26 key_schedule(key);
27}

References name(), and valid_keylength().

◆ valid_keylength() [1/2]

bool Botan::SymmetricAlgorithm::valid_keylength ( size_t length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 114 of file sym_algo.h.

114{ return key_spec().valid_keylength(length); }
bool valid_keylength(size_t length) const
Definition sym_algo.h:44

◆ valid_keylength() [2/2]

bool Botan::SymmetricAlgorithm::valid_keylength ( size_t length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 114 of file sym_algo.h.

114{ return key_spec().valid_keylength(length); }

References key_spec().

Referenced by set_key().

Member Data Documentation

◆ ParallelismMult [1/2]

size_t Botan::BlockCipher::ParallelismMult
staticconstexprinherited

Multiplier on a block cipher's native parallelism

Usually notable performance gains come from further loop blocking, at least for 2 or 4x

Definition at line 52 of file block_cipher.h.

◆ ParallelismMult [2/2]

size_t Botan::BlockCipher::ParallelismMult = 4
staticconstexprinherited

Multiplier on a block cipher's native parallelism

Usually notable performance gains come from further loop blocking, at least for 2 or 4x

Definition at line 52 of file block_cipher.h.

Referenced by Botan::GCM_Mode::ideal_granularity(), and parallel_bytes().


The documentation for this class was generated from the following files: