Skip to content

Commit de3b5ec

Browse files
authored
Merge pull request libbitcoin#1773 from evoskuil/master
Update AES, add tests.
2 parents 62dd8e3 + 7a650cb commit de3b5ec

File tree

9 files changed

+229
-145
lines changed

9 files changed

+229
-145
lines changed

include/bitcoin/system/crypto/aes256.hpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,34 @@
2424

2525
namespace libbitcoin {
2626
namespace system {
27-
namespace aes256 {
28-
29-
/// This is an implementation of AES256 (in ECB mode).
30-
/// ECB mode is the simplest block cipher mode but is insecure for most
31-
/// applications because it doesn't hide patterns in the plaintext.
32-
/// NIST selected three members of the Rijndael family, each with a block
33-
/// size of 128 bits, but three different key lengths: 128, 192 and 256 bits.
3427

35-
constexpr size_t block_size = bytes<128>;
36-
typedef data_array<block_size> block;
28+
/// Advanced Encryption Standard (AES) 256.
29+
class BC_API aes256 final
30+
{
31+
public:
32+
/// AES block is always 128 bits.
33+
typedef data_array<bytes<128>> block;
3734

38-
constexpr size_t secret_size = bytes<256>;
39-
typedef data_array<secret_size> secret;
35+
/// AES-256 secret is always 256 bits.
36+
typedef data_array<bytes<256>> secret;
4037

41-
/// Perform aes256 encryption/decryption on a data block.
42-
void encrypt(block& bytes, const secret& key) NOEXCEPT;
43-
void decrypt(block& bytes, const secret& key) NOEXCEPT;
38+
/// nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf
39+
static void encrypt_ecb(block& bytes, const secret& key) NOEXCEPT;
40+
static void decrypt_ecb(block& bytes, const secret& key) NOEXCEPT;
41+
42+
private:
43+
struct context
44+
{
45+
secret key;
46+
secret enckey;
47+
secret deckey;
48+
};
49+
50+
static void initialize(context& context, const secret& key) NOEXCEPT;
51+
static void encrypt_ecb(context& context, block& bytes) NOEXCEPT;
52+
static void decrypt_ecb(context& context, block& bytes) NOEXCEPT;
53+
};
4454

45-
} // namespace aes256
4655
} // namespace system
4756
} // namespace libbitcoin
4857

include/bitcoin/system/hash/pbkd.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct pbkd
5656

5757
protected:
5858
template <size_t Length>
59-
static constexpr auto xor_n(data_array<Length>& to,
59+
static constexpr void xor_n(data_array<Length>& to,
6060
const data_array<Length>& from) NOEXCEPT;
6161
};
6262

include/bitcoin/system/impl/hash/hmac.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ xor_n(block_t& pad, const byte_t* key, size_t size) NOEXCEPT
9090
// append zeros to the end of K to create a B byte string [skip].
9191
// XOR (bitwise exclusive-OR) the B byte string ... with [ipad/opad].
9292
// [not xoring beyond K length is the same as xoring that with zero].
93-
for (size_t i = 0; i < size; ++i)
93+
for (size_t i{}; i < size; ++i)
9494
pad[i] ^= key[i];
9595

9696
return pad;

include/bitcoin/system/impl/hash/pbkd.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ BC_PUSH_WARNING(NO_ARRAY_INDEXING)
3636
// static/protected
3737
TEMPLATE
3838
template <size_t Length>
39-
constexpr auto CLASS::
39+
constexpr void CLASS::
4040
xor_n(data_array<Length>& to, const data_array<Length>& from) NOEXCEPT
4141
{
4242
// rfc8018
4343
// F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
44-
for (size_t i = 0; i < Length; ++i)
44+
for (size_t i{}; i < Length; ++i)
4545
to[i] ^= from[i];
4646
};
4747

include/bitcoin/system/impl/math/bits.ipp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ template <typename Value, if_unsigned_integral_integer<Value>>
306306
constexpr void shift_left_into(Value& value, size_t shift, bool overflow) NOEXCEPT
307307
{
308308
constexpr auto span = bits<Value>;
309-
overflow && shift >= span ? value = 0 : depromote<Value>(
310-
value <<= (shift % span));
309+
overflow && shift >= span ? value = 0 : value <<= (shift % span);
311310
}
312311

313312
// signed overloads (shift left of negative is undefined behavior).
@@ -337,8 +336,7 @@ template <typename Value, if_unsigned_integral_integer<Value>>
337336
constexpr void shift_right_into(Value& value, size_t shift, bool overflow) NOEXCEPT
338337
{
339338
constexpr auto span = bits<Value>;
340-
overflow && shift >= span ? value = 0 : depromote<Value>(
341-
value >>= (shift % span));
339+
overflow && shift >= span ? value = 0 : value >>= (shift % span);
342340
}
343341

344342
// signed overloads (shift right of negative is unspecified behavior).

include/bitcoin/system/impl/math/cast.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ constexpr Restored depromote(Common value) NOEXCEPT
4848

4949
template <typename Restored, typename Common,
5050
if_integer<Restored>,
51-
if_non_integral_integer<Common> >
51+
if_non_integral_integer<Common>>
5252
constexpr Restored depromote(Common value) NOEXCEPT
5353
{
5454
BC_PUSH_WARNING(NO_IDENTITY_CAST)

0 commit comments

Comments
 (0)