Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 84aa172

Browse files
committed
Endiannes management changed to Boost.Endian #3
1 parent 9932c6d commit 84aa172

21 files changed

+377
-359
lines changed

include/nil/crypto3/block/aria.hpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
#ifndef CRYPTO3_ARIA_H_
1111
#define CRYPTO3_ARIA_H_
1212

13+
#include <boost/endian/arithmetic.hpp>
14+
1315
#include <nil/crypto3/block/detail/aria/aria_policy.hpp>
1416

1517
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
1618
#include <nil/crypto3/block/detail/stream_endian.hpp>
1719

18-
#include <nil/crypto3/block/detail/utilities/cpuid/cpuid.hpp>
20+
#include <nil/crypto3/utilities/cpuid/cpuid.hpp>
1921

2022
namespace nil {
2123
namespace crypto3 {
@@ -107,10 +109,10 @@ namespace nil {
107109
word_type w2[4];
108110
word_type w3[4];
109111

110-
w0[0] = load_be<uint32_t>(key, 0);
111-
w0[1] = load_be<uint32_t>(key, 1);
112-
w0[2] = load_be<uint32_t>(key, 2);
113-
w0[3] = load_be<uint32_t>(key, 3);
112+
w0[0] = boost::endian::native_to_big(key[0]);
113+
w0[1] = boost::endian::native_to_big(key[1]);
114+
w0[2] = boost::endian::native_to_big(key[2]);
115+
w0[3] = boost::endian::native_to_big(key[3]);
114116

115117
w1[0] = w0[0] ^ policy_type::round_constants[CK0][0];
116118
w1[1] = w0[1] ^ policy_type::round_constants[CK0][1];
@@ -120,12 +122,12 @@ namespace nil {
120122
policy_type::fo(w1[0], w1[1], w1[2], w1[3]);
121123

122124
if (policy_type::key_bits / 8 == 24 || policy_type::key_bits / 8 == 32) {
123-
w1[0] ^= load_be<uint32_t>(key, 4);
124-
w1[1] ^= load_be<uint32_t>(key, 5);
125+
w1[0] ^= boost::endian::native_to_big(key[4]);
126+
w1[1] ^= boost::endian::native_to_big(key[5]);
125127
}
126128
if (policy_type::key_bits / 8 == 32) {
127-
w1[2] ^= load_be<uint32_t>(key, 6);
128-
w1[3] ^= load_be<uint32_t>(key, 7);
129+
w1[2] ^= boost::endian::native_to_big(key[6]);
130+
w1[3] ^= boost::endian::native_to_big(key[7]);
129131
}
130132

131133
w2[0] = w1[0] ^ policy_type::round_constants[CK1][0];
@@ -234,7 +236,10 @@ namespace nil {
234236
Z |= policy_type::s1[i] | policy_type::s2[i];
235237
}
236238

237-
word_type t0 = plaintext[0], t1 = plaintext[1], t2 = plaintext[2], t3 = plaintext[3];
239+
word_type t0 = boost::endian::native_to_big(plaintext[0]);
240+
word_type t1 = boost::endian::native_to_big(plaintext[1]);
241+
word_type t2 = boost::endian::native_to_big(plaintext[2]);
242+
word_type t3 = boost::endian::native_to_big(plaintext[3]);
238243

239244
t0 &= Z;
240245

include/nil/crypto3/block/blowfish.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace nil {
112112

113113
const size_t p_salt_offset = (!salt.empty()) ? policy_type::permutations_size % salt.size() : 0;
114114

115-
uint32_t L = 0, R = 0;
115+
word_type L = 0, R = 0;
116116
generate_sbox(permutations, L, R, salt, 0);
117117
generate_sbox(constants, L, R, salt, p_salt_offset);
118118
}
@@ -134,7 +134,7 @@ namespace nil {
134134
L ^= policy_type::bff(R, constants);
135135
}
136136

137-
uint32_t T = R;
137+
word_type T = R;
138138
R = L ^ permutations[16];
139139
L = T ^ permutations[17];
140140
box[i] = L;

include/nil/crypto3/block/camellia.hpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef CRYPTO3_CAMELLIA_H_
1111
#define CRYPTO3_CAMELLIA_H_
1212

13+
#include <boost/endian/arithmetic.hpp>
14+
1315
#include <nil/crypto3/block/detail/camellia/camellia_policy.hpp>
1416

1517
#include <nil/crypto3/block/detail/block_state_preprocessor.hpp>
@@ -85,10 +87,8 @@ namespace nil {
8587
key_schedule_type key_schedule;
8688

8789
inline block_type encrypt_block(const block_type &plaintext) {
88-
block_type out = {0};
89-
90-
word_type d1, d2;
91-
load_be(plaintext.data(), d1, d2);
90+
word_type d1 = boost::endian::native_to_big(plaintext[0]);
91+
word_type d2 = boost::endian::native_to_big(plaintext[1]);
9292

9393
const uint64_t *K = key_schedule.data();
9494

@@ -114,14 +114,12 @@ namespace nil {
114114
d2 ^= *K++;
115115
d1 ^= *K++;
116116

117-
store_be(out.data() + 16, d2, d1);
117+
return {boost::endian::big_to_native(d2), boost::endian::big_to_native(d1)};
118118
}
119119

120120
inline block_type decrypt_block(const block_type &ciphertext) {
121-
block_type out = {0};
122-
123-
word_type d1, d2;
124-
load_be(ciphertext.data(), d1, d2);
121+
word_type d1 = boost::endian::native_to_big(ciphertext[0]);
122+
word_type d2 = boost::endian::native_to_big(ciphertext[1]);
125123

126124
const uint64_t *K = &key_schedule[key_schedule.size() - 1];
127125

@@ -147,7 +145,7 @@ namespace nil {
147145
d1 ^= *K--;
148146
d2 ^= *K;
149147

150-
store_be(out.data(), d2, d1);
148+
return {boost::endian::big_to_native(d2), boost::endian::big_to_native(d1)};
151149
}
152150

153151
inline uint64_t left_rot_hi(uint64_t h, uint64_t l, size_t shift) {
@@ -159,8 +157,8 @@ namespace nil {
159157
}
160158

161159
void schedule_key(const key_type &key) {
162-
const word_type KL_H = load_be<uint64_t>(key, 0);
163-
const word_type KL_L = load_be<uint64_t>(key, 1);
160+
const word_type KL_H = boost::endian::native_to_big(key[0]);
161+
const word_type KL_L = boost::endian::native_to_big(key[1]);
164162

165163
const word_type KR_H = (key.size() >= 24) ? load_be<uint64_t>(key, 2) : 0;
166164
const word_type KR_L = (key.size() == 32) ? load_be<uint64_t>(key, 3) : ((key.size() == 24) ? ~KR_H

include/nil/crypto3/block/des.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ namespace nil {
5454
typename StateAccumulator, std::size_t ValueBits,
5555
typename Padding>
5656
struct stream_cipher {
57-
typedef cipher_state<Mode<des, Padding>, StateAccumulator, stream_endian::little_octet_big_bit,
58-
ValueBits, policy_type::word_bits * 2> type_;
57+
typedef block_state_preprocessor<Mode<des, Padding>, StateAccumulator,
58+
stream_endian::little_octet_big_bit, ValueBits,
59+
policy_type::word_bits * 2> type_;
5960
#ifdef CRYPTO3_HASH_NO_HIDE_INTERNAL_TYPES
6061
typedef type_ type;
6162
#else

include/nil/crypto3/block/detail/cast/cast_functions.hpp

+48-56
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#ifndef CRYPTO3_CAST_FUNCTIONS_CPP_HPP
1111
#define CRYPTO3_CAST_FUNCTIONS_CPP_HPP
1212

13-
#include <nil/crypto3/block/detail/cast/basic_cast_policy.hpp>
13+
#include <boost/endian/arithmetic.hpp>
1414

15-
#include <nil/crypto3/utilities/loadstore.hpp>
15+
#include <nil/crypto3/block/detail/cast/basic_cast_policy.hpp>
1616

1717
namespace nil {
1818
namespace crypto3 {
@@ -70,32 +70,32 @@ namespace nil {
7070
*/
7171
inline static word_type f1(word_type R, word_type MK, byte_type RK) {
7272
const word_type T = rotl_var(MK + R, RK);
73-
return (policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] ^
74-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)]) -
75-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)] +
76-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
73+
return (policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] ^
74+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)]) -
75+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)] +
76+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
7777
}
7878

7979
/*
8080
* CAST-128 Round Type 2
8181
*/
8282
inline static word_type f2(word_type R, word_type MK, byte_type RK) {
8383
const uint32_t T = rotl_var(MK ^ R, RK);
84-
return (policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] -
85-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)] +
86-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)]) ^
87-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
84+
return (policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] -
85+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)] +
86+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)]) ^
87+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
8888
}
8989

9090
/*
9191
* CAST-128 Round Type 3
9292
*/
9393
inline static word_type f3(word_type R, word_type MK, byte_type RK) {
9494
const uint32_t T = rotl_var(MK - R, RK);
95-
return ((policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] +
96-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)]) ^
97-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)]) -
98-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
95+
return ((policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] +
96+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)]) ^
97+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)]) -
98+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
9999
}
100100

101101
inline static void cast_ks(key_schedule_type &K, std::array<word_type, 4> &X) {
@@ -301,10 +301,8 @@ namespace nil {
301301
inline static block_type encrypt_block(const block_type &plaintext,
302302
const key_schedule_type &key_schedule,
303303
const rotation_key_schedule_type &rkey_schedule) {
304-
block_type out = {0};
305-
306-
word_type L, R;
307-
load_be(plaintext.data(), L, R);
304+
word_type L = boost::endian::native_to_big(plaintext[0]);
305+
word_type R = boost::endian::native_to_big(plaintext[1]);
308306

309307
L ^= policy_type::f1(R, key_schedule[0], rkey_schedule[0]);
310308
R ^= policy_type::f2(L, key_schedule[1], rkey_schedule[1]);
@@ -323,17 +321,14 @@ namespace nil {
323321
L ^= policy_type::f3(R, key_schedule[14], rkey_schedule[14]);
324322
R ^= policy_type::f1(L, key_schedule[15], rkey_schedule[15]);
325323

326-
store_be(out.data(), R, L);
327-
328-
return out;
324+
return {boost::endian::big_to_native(R), boost::endian::big_to_native(L)};
329325
}
330326

331327
inline static block_type decrypt_block(const block_type &ciphertext,
332328
const key_schedule_type &key_schedule,
333329
const rotation_key_schedule_type &rkey_schedule) {
334-
block_type out = {0};
335-
word_type L, R;
336-
load_be(ciphertext.data(), L, R);
330+
word_type L = boost::endian::native_to_big(ciphertext[0]);
331+
word_type R = boost::endian::native_to_big(ciphertext[1]);
337332

338333
L ^= policy_type::f1(R, key_schedule[15], rkey_schedule[15]);
339334
R ^= policy_type::f3(L, key_schedule[14], rkey_schedule[14]);
@@ -352,8 +347,7 @@ namespace nil {
352347
L ^= policy_type::f2(R, key_schedule[1], rkey_schedule[1]);
353348
R ^= policy_type::f1(L, key_schedule[0], rkey_schedule[0]);
354349

355-
store_be(out.data(), R, L);
356-
return out;
350+
return {boost::endian::big_to_native(R), boost::endian::big_to_native(L)};
357351
}
358352

359353
inline static void schedule_key(const key_type &key, key_schedule_type &key_schedule,
@@ -408,43 +402,41 @@ namespace nil {
408402
*/
409403
static inline void round1(word_type &out, word_type in, word_type MK, word_type RK) {
410404
const word_type T = rotl_var(MK + in, RK);
411-
out ^= (policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] ^
412-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)]) -
413-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)] +
414-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
405+
out ^= (policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] ^
406+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)]) -
407+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)] +
408+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
415409
}
416410

417411
/*
418412
* CAST-256 Round Type 2
419413
*/
420414
static inline void round2(word_type &out, word_type in, word_type MK, word_type RK) {
421415
const word_type T = rotl_var(MK ^ in, RK);
422-
out ^= (policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] -
423-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)] +
424-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)]) ^
425-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
416+
out ^= (policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] -
417+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)] +
418+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)]) ^
419+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
426420
}
427421

428422
/*
429423
* CAST-256 Round Type 3
430424
*/
431425
static inline void round3(word_type &out, word_type in, word_type MK, word_type RK) {
432426
const word_type T = rotl_var(MK - in, RK);
433-
out ^= ((policy_type::sbox1[extract_uint_t<CHAR_BIT>(T, 0)] +
434-
policy_type::sbox2[extract_uint_t<CHAR_BIT>(T, 1)]) ^
435-
policy_type::sbox3[extract_uint_t<CHAR_BIT>(T, 2)]) -
436-
policy_type::sbox4[extract_uint_t<CHAR_BIT>(T, 3)];
427+
out ^= ((policy_type::sbox1[policy_type::template extract_uint_t<CHAR_BIT>(T, 0)] +
428+
policy_type::sbox2[policy_type::template extract_uint_t<CHAR_BIT>(T, 1)]) ^
429+
policy_type::sbox3[policy_type::template extract_uint_t<CHAR_BIT>(T, 2)]) -
430+
policy_type::sbox4[policy_type::template extract_uint_t<CHAR_BIT>(T, 3)];
437431
}
438432

439433
inline static block_type encrypt_block(const block_type &plaintext,
440434
const key_schedule_type &key_schedule,
441435
const rotation_key_schedule_type &rkey_schedule) {
442-
block_type out = {0};
443-
444-
word_type A = load_be<uint32_t>(plaintext.data(), 0);
445-
word_type B = load_be<uint32_t>(plaintext.data(), 1);
446-
word_type C = load_be<uint32_t>(plaintext.data(), 2);
447-
word_type D = load_be<uint32_t>(plaintext.data(), 3);
436+
word_type A = boost::endian::native_to_big(plaintext[0]);
437+
word_type B = boost::endian::native_to_big(plaintext[1]);
438+
word_type C = boost::endian::native_to_big(plaintext[2]);
439+
word_type D = boost::endian::native_to_big(plaintext[3]);
448440

449441
round1(C, D, key_schedule[0], rkey_schedule[0]);
450442
round2(B, C, key_schedule[1], rkey_schedule[1]);
@@ -495,20 +487,19 @@ namespace nil {
495487
round2(B, C, key_schedule[45], rkey_schedule[45]);
496488
round1(C, D, key_schedule[44], rkey_schedule[44]);
497489

498-
store_be(out.data(), A, B, C, D);
499-
500-
return out;
490+
return {
491+
boost::endian::big_to_native(A), boost::endian::big_to_native(B),
492+
boost::endian::big_to_native(C), boost::endian::big_to_native(D)
493+
};
501494
}
502495

503496
inline static block_type decrypt_block(const block_type &ciphertext,
504497
const key_schedule_type &key_schedule,
505498
const rotation_key_schedule_type &rkey_schedule) {
506-
block_type out = {0};
507-
508-
word_type A = load_be<uint32_t>(ciphertext.data(), 0);
509-
word_type B = load_be<uint32_t>(ciphertext.data(), 1);
510-
word_type C = load_be<uint32_t>(ciphertext.data(), 2);
511-
word_type D = load_be<uint32_t>(ciphertext.data(), 3);
499+
word_type A = boost::endian::native_to_big(ciphertext[0]);
500+
word_type B = boost::endian::native_to_big(ciphertext[1]);
501+
word_type C = boost::endian::native_to_big(ciphertext[2]);
502+
word_type D = boost::endian::native_to_big(ciphertext[3]);
512503

513504
round1(C, D, key_schedule[44], rkey_schedule[44]);
514505
round2(B, C, key_schedule[45], rkey_schedule[45]);
@@ -559,9 +550,10 @@ namespace nil {
559550
round2(B, C, key_schedule[1], rkey_schedule[1]);
560551
round1(C, D, key_schedule[0], rkey_schedule[0]);
561552

562-
store_be(out.data(), A, B, C, D);
563-
564-
return out;
553+
return {
554+
boost::endian::big_to_native(A), boost::endian::big_to_native(B),
555+
boost::endian::big_to_native(C), boost::endian::big_to_native(D)
556+
};
565557
}
566558

567559
static inline void schedule_key(const key_type &key, key_schedule_type &key_schedule,

include/nil/crypto3/block/detail/idea/idea_policy.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace nil {
1919
namespace block {
2020
namespace detail {
2121
struct idea_policy : idea_functions<16> {
22-
static const std::size_t rounds = 8;
22+
constexpr static const std::size_t rounds = 8;
2323

2424
constexpr static const std::size_t block_bits = 64;
2525
constexpr static const std::size_t block_words = block_bits / word_bits;

0 commit comments

Comments
 (0)