10
10
#ifndef CRYPTO3_CAST_FUNCTIONS_CPP_HPP
11
11
#define CRYPTO3_CAST_FUNCTIONS_CPP_HPP
12
12
13
- #include < nil/crypto3/block/detail/cast/basic_cast_policy .hpp>
13
+ #include < boost/endian/arithmetic .hpp>
14
14
15
- #include < nil/crypto3/utilities/loadstore .hpp>
15
+ #include < nil/crypto3/block/detail/cast/basic_cast_policy .hpp>
16
16
17
17
namespace nil {
18
18
namespace crypto3 {
@@ -70,32 +70,32 @@ namespace nil {
70
70
*/
71
71
inline static word_type f1 (word_type R, word_type MK, byte_type RK) {
72
72
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 )];
77
77
}
78
78
79
79
/*
80
80
* CAST-128 Round Type 2
81
81
*/
82
82
inline static word_type f2 (word_type R, word_type MK, byte_type RK) {
83
83
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 )];
88
88
}
89
89
90
90
/*
91
91
* CAST-128 Round Type 3
92
92
*/
93
93
inline static word_type f3 (word_type R, word_type MK, byte_type RK) {
94
94
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 )];
99
99
}
100
100
101
101
inline static void cast_ks (key_schedule_type &K, std::array<word_type, 4 > &X) {
@@ -301,10 +301,8 @@ namespace nil {
301
301
inline static block_type encrypt_block (const block_type &plaintext,
302
302
const key_schedule_type &key_schedule,
303
303
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 ]);
308
306
309
307
L ^= policy_type::f1 (R, key_schedule[0 ], rkey_schedule[0 ]);
310
308
R ^= policy_type::f2 (L, key_schedule[1 ], rkey_schedule[1 ]);
@@ -323,17 +321,14 @@ namespace nil {
323
321
L ^= policy_type::f3 (R, key_schedule[14 ], rkey_schedule[14 ]);
324
322
R ^= policy_type::f1 (L, key_schedule[15 ], rkey_schedule[15 ]);
325
323
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)};
329
325
}
330
326
331
327
inline static block_type decrypt_block (const block_type &ciphertext,
332
328
const key_schedule_type &key_schedule,
333
329
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 ]);
337
332
338
333
L ^= policy_type::f1 (R, key_schedule[15 ], rkey_schedule[15 ]);
339
334
R ^= policy_type::f3 (L, key_schedule[14 ], rkey_schedule[14 ]);
@@ -352,8 +347,7 @@ namespace nil {
352
347
L ^= policy_type::f2 (R, key_schedule[1 ], rkey_schedule[1 ]);
353
348
R ^= policy_type::f1 (L, key_schedule[0 ], rkey_schedule[0 ]);
354
349
355
- store_be (out.data (), R, L);
356
- return out;
350
+ return {boost::endian::big_to_native (R), boost::endian::big_to_native (L)};
357
351
}
358
352
359
353
inline static void schedule_key (const key_type &key, key_schedule_type &key_schedule,
@@ -408,43 +402,41 @@ namespace nil {
408
402
*/
409
403
static inline void round1 (word_type &out, word_type in, word_type MK, word_type RK) {
410
404
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 )];
415
409
}
416
410
417
411
/*
418
412
* CAST-256 Round Type 2
419
413
*/
420
414
static inline void round2 (word_type &out, word_type in, word_type MK, word_type RK) {
421
415
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 )];
426
420
}
427
421
428
422
/*
429
423
* CAST-256 Round Type 3
430
424
*/
431
425
static inline void round3 (word_type &out, word_type in, word_type MK, word_type RK) {
432
426
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 )];
437
431
}
438
432
439
433
inline static block_type encrypt_block (const block_type &plaintext,
440
434
const key_schedule_type &key_schedule,
441
435
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 ]);
448
440
449
441
round1 (C, D, key_schedule[0 ], rkey_schedule[0 ]);
450
442
round2 (B, C, key_schedule[1 ], rkey_schedule[1 ]);
@@ -495,20 +487,19 @@ namespace nil {
495
487
round2 (B, C, key_schedule[45 ], rkey_schedule[45 ]);
496
488
round1 (C, D, key_schedule[44 ], rkey_schedule[44 ]);
497
489
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
+ };
501
494
}
502
495
503
496
inline static block_type decrypt_block (const block_type &ciphertext,
504
497
const key_schedule_type &key_schedule,
505
498
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 ]);
512
503
513
504
round1 (C, D, key_schedule[44 ], rkey_schedule[44 ]);
514
505
round2 (B, C, key_schedule[45 ], rkey_schedule[45 ]);
@@ -559,9 +550,10 @@ namespace nil {
559
550
round2 (B, C, key_schedule[1 ], rkey_schedule[1 ]);
560
551
round1 (C, D, key_schedule[0 ], rkey_schedule[0 ]);
561
552
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
+ };
565
557
}
566
558
567
559
static inline void schedule_key (const key_type &key, key_schedule_type &key_schedule,
0 commit comments