Skip to content

Commit

Permalink
span: update SymmetricAlgorithm::key_schedule()
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Aug 30, 2023
1 parent 23bdc9c commit 5d06bb0
Show file tree
Hide file tree
Showing 92 changed files with 246 additions and 246 deletions.
8 changes: 4 additions & 4 deletions src/lib/base/sym_algo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ void SymmetricAlgorithm::throw_key_not_set_error() const {
throw Key_Not_Set(name());
}

void SymmetricAlgorithm::set_key(const uint8_t key[], size_t length) {
if(!valid_keylength(length)) {
throw Invalid_Key_Length(name(), length);
void SymmetricAlgorithm::set_key(std::span<const uint8_t> key) {
if(!valid_keylength(key.size())) {
throw Invalid_Key_Length(name(), key.size());
}
key_schedule(key, length);
key_schedule(key);
}

} // namespace Botan
9 changes: 4 additions & 5 deletions src/lib/base/sym_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm {
* Set the symmetric key of this object.
* @param key the SymmetricKey to be set.
*/
void set_key(const SymmetricKey& key) { set_key(key.begin(), key.length()); }
void set_key(const SymmetricKey& key) { set_key(std::span{key.begin(), key.length()}); }

/**
* Set the symmetric key of this object.
* @param key the contiguous byte range to be set.
*/
void set_key(std::span<const uint8_t> key) { set_key(key.data(), key.size()); }
void set_key(std::span<const uint8_t> key);

/**
* Set the symmetric key of this object.
* @param key the to be set as a byte array.
* @param length in bytes of key param
*/
void set_key(const uint8_t key[], size_t length);
void set_key(const uint8_t key[], size_t length) { set_key(std::span{key, length}); }

/**
* @return the algorithm name
Expand All @@ -150,9 +150,8 @@ class BOTAN_PUBLIC_API(2, 0) SymmetricAlgorithm {
/**
* Run the key schedule
* @param key the key
* @param length of key
*/
virtual void key_schedule(const uint8_t key[], size_t length) = 0;
virtual void key_schedule(std::span<const uint8_t> key) = 0;
};

} // namespace Botan
Expand Down
30 changes: 15 additions & 15 deletions src/lib/block/aes/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,26 +845,26 @@ void AES_128::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
aes_decrypt_n(in, out, blocks, m_DK);
}

void AES_128::key_schedule(const uint8_t key[], size_t length) {
void AES_128::key_schedule(std::span<const uint8_t> key) {
#if defined(BOTAN_HAS_AES_NI)
if(CPUID::has_aes_ni()) {
return aesni_key_schedule(key, length);
return aesni_key_schedule(key.data(), key.size());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key, length, m_EK, m_DK, CPUID::is_little_endian());
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_AES_VPERM)
if(CPUID::has_vperm()) {
return vperm_key_schedule(key, length);
return vperm_key_schedule(key.data(), key.size());
}
#endif

aes_key_schedule(key, length, m_EK, m_DK);
aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
}

void AES_128::clear() {
Expand Down Expand Up @@ -908,26 +908,26 @@ void AES_192::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
aes_decrypt_n(in, out, blocks, m_DK);
}

void AES_192::key_schedule(const uint8_t key[], size_t length) {
void AES_192::key_schedule(std::span<const uint8_t> key) {
#if defined(BOTAN_HAS_AES_NI)
if(CPUID::has_aes_ni()) {
return aesni_key_schedule(key, length);
return aesni_key_schedule(key.data(), key.size());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key, length, m_EK, m_DK, CPUID::is_little_endian());
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_AES_VPERM)
if(CPUID::has_vperm()) {
return vperm_key_schedule(key, length);
return vperm_key_schedule(key.data(), key.size());
}
#endif

aes_key_schedule(key, length, m_EK, m_DK);
aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
}

void AES_192::clear() {
Expand Down Expand Up @@ -971,26 +971,26 @@ void AES_256::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const
aes_decrypt_n(in, out, blocks, m_DK);
}

void AES_256::key_schedule(const uint8_t key[], size_t length) {
void AES_256::key_schedule(std::span<const uint8_t> key) {
#if defined(BOTAN_HAS_AES_NI)
if(CPUID::has_aes_ni()) {
return aesni_key_schedule(key, length);
return aesni_key_schedule(key.data(), key.size());
}
#endif

#if defined(BOTAN_HAS_HW_AES_SUPPORT)
if(CPUID::has_hw_aes()) {
return aes_key_schedule(key, length, m_EK, m_DK, CPUID::is_little_endian());
return aes_key_schedule(key.data(), key.size(), m_EK, m_DK, CPUID::is_little_endian());
}
#endif

#if defined(BOTAN_HAS_AES_VPERM)
if(CPUID::has_vperm()) {
return vperm_key_schedule(key, length);
return vperm_key_schedule(key.data(), key.size());
}
#endif

aes_key_schedule(key, length, m_EK, m_DK);
aes_key_schedule(key.data(), key.size(), m_EK, m_DK);
}

void AES_256::clear() {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/block/aes/aes.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AES_128 final : public Block_Cipher_Fixed_Params<16, 16> {
bool has_keying_material() const override;

private:
void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

#if defined(BOTAN_HAS_AES_VPERM)
void vperm_encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
Expand Down Expand Up @@ -88,7 +88,7 @@ class AES_192 final : public Block_Cipher_Fixed_Params<16, 24> {
void hw_aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

secure_vector<uint32_t> m_EK, m_DK;
};
Expand Down Expand Up @@ -128,7 +128,7 @@ class AES_256 final : public Block_Cipher_Fixed_Params<16, 32> {
void hw_aes_decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const;
#endif

void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

secure_vector<uint32_t> m_EK, m_DK;
};
Expand Down
46 changes: 23 additions & 23 deletions src/lib/block/aria/aria.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ void aria_ks_dk_transform(uint32_t& K0, uint32_t& K1, uint32_t& K2, uint32_t& K3
/*
* ARIA Key Schedule
*/
void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, const uint8_t key[], size_t length) {
void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, std::span<const uint8_t> key) {
const uint32_t KRK[3][4] = {{0x517cc1b7, 0x27220a94, 0xfe13abe8, 0xfa9a6ee0},
{0x6db14acc, 0x9e21c820, 0xff28b1d5, 0xef5de2b0},
{0xdb92371d, 0x2126e970, 0x03249775, 0x04e8c90e}};

const size_t CK0 = (length / 8) - 2;
const size_t CK0 = (key.size() / 8) - 2;
const size_t CK1 = (CK0 + 1) % 3;
const size_t CK2 = (CK1 + 1) % 3;

Expand All @@ -263,10 +263,10 @@ void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, co
uint32_t w2[4];
uint32_t w3[4];

w0[0] = load_be<uint32_t>(key, 0);
w0[1] = load_be<uint32_t>(key, 1);
w0[2] = load_be<uint32_t>(key, 2);
w0[3] = load_be<uint32_t>(key, 3);
w0[0] = load_be<uint32_t>(key.data(), 0);
w0[1] = load_be<uint32_t>(key.data(), 1);
w0[2] = load_be<uint32_t>(key.data(), 2);
w0[3] = load_be<uint32_t>(key.data(), 3);

w1[0] = w0[0] ^ KRK[CK0][0];
w1[1] = w0[1] ^ KRK[CK0][1];
Expand All @@ -275,13 +275,13 @@ void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, co

ARIA_FO(w1[0], w1[1], w1[2], w1[3]);

if(length == 24 || length == 32) {
w1[0] ^= load_be<uint32_t>(key, 4);
w1[1] ^= load_be<uint32_t>(key, 5);
if(key.size() == 24 || key.size() == 32) {
w1[0] ^= load_be<uint32_t>(key.data(), 4);
w1[1] ^= load_be<uint32_t>(key.data(), 5);
}
if(length == 32) {
w1[2] ^= load_be<uint32_t>(key, 6);
w1[3] ^= load_be<uint32_t>(key, 7);
if(key.size() == 32) {
w1[2] ^= load_be<uint32_t>(key.data(), 6);
w1[3] ^= load_be<uint32_t>(key.data(), 7);
}

w2[0] = w1[0] ^ KRK[CK1][0];
Expand All @@ -308,11 +308,11 @@ void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, co
w3[2] ^= w1[2];
w3[3] ^= w1[3];

if(length == 16) {
if(key.size() == 16) {
ERK.resize(4 * 13);
} else if(length == 24) {
} else if(key.size() == 24) {
ERK.resize(4 * 15);
} else if(length == 32) {
} else if(key.size() == 32) {
ERK.resize(4 * 17);
}

Expand All @@ -330,11 +330,11 @@ void key_schedule(secure_vector<uint32_t>& ERK, secure_vector<uint32_t>& DRK, co
ARIA_ROL128<67>(w3, w0, &ERK[44]);
ARIA_ROL128<97>(w0, w1, &ERK[48]);

if(length == 24 || length == 32) {
if(key.size() == 24 || key.size() == 32) {
ARIA_ROL128<97>(w1, w2, &ERK[52]);
ARIA_ROL128<97>(w2, w3, &ERK[56]);

if(length == 32) {
if(key.size() == 32) {
ARIA_ROL128<97>(w3, w0, &ERK[60]);
ARIA_ROL128<109>(w0, w1, &ERK[64]);
}
Expand Down Expand Up @@ -401,16 +401,16 @@ bool ARIA_256::has_keying_material() const {
return !m_ERK.empty();
}

void ARIA_128::key_schedule(const uint8_t key[], size_t length) {
ARIA_F::key_schedule(m_ERK, m_DRK, key, length);
void ARIA_128::key_schedule(std::span<const uint8_t> key) {
ARIA_F::key_schedule(m_ERK, m_DRK, key);
}

void ARIA_192::key_schedule(const uint8_t key[], size_t length) {
ARIA_F::key_schedule(m_ERK, m_DRK, key, length);
void ARIA_192::key_schedule(std::span<const uint8_t> key) {
ARIA_F::key_schedule(m_ERK, m_DRK, key);
}

void ARIA_256::key_schedule(const uint8_t key[], size_t length) {
ARIA_F::key_schedule(m_ERK, m_DRK, key, length);
void ARIA_256::key_schedule(std::span<const uint8_t> key) {
ARIA_F::key_schedule(m_ERK, m_DRK, key);
}

void ARIA_128::clear() {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/block/aria/aria.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ARIA_128 final : public Block_Cipher_Fixed_Params<16, 16> {
bool has_keying_material() const override;

private:
void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

// Encryption and Decryption round keys.
secure_vector<uint32_t> m_ERK, m_DRK;
Expand All @@ -60,7 +60,7 @@ class ARIA_192 final : public Block_Cipher_Fixed_Params<16, 24> {
bool has_keying_material() const override;

private:
void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

// Encryption and Decryption round keys.
secure_vector<uint32_t> m_ERK, m_DRK;
Expand All @@ -83,7 +83,7 @@ class ARIA_256 final : public Block_Cipher_Fixed_Params<16, 32> {
bool has_keying_material() const override;

private:
void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

// Encryption and Decryption round keys.
secure_vector<uint32_t> m_ERK, m_DRK;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/block/blowfish/blowfish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ bool Blowfish::has_keying_material() const {
/*
* Blowfish Key Schedule
*/
void Blowfish::key_schedule(const uint8_t key[], size_t length) {
void Blowfish::key_schedule(std::span<const uint8_t> key) {
m_P.resize(18);
copy_mem(m_P.data(), P_INIT, 18);

m_S.resize(1024);
copy_mem(m_S.data(), S_INIT, 1024);

key_expansion(key, length, nullptr, 0);
key_expansion(key.data(), key.size(), nullptr, 0);
}

void Blowfish::key_expansion(const uint8_t key[], size_t length, const uint8_t salt[], size_t salt_length) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/block/blowfish/blowfish.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BOTAN_TEST_API Blowfish final : public Block_Cipher_Fixed_Params<8, 1, 56>
bool has_keying_material() const override;

private:
void key_schedule(const uint8_t key[], size_t length) override;
void key_schedule(std::span<const uint8_t> key) override;

void key_expansion(const uint8_t key[], size_t key_length, const uint8_t salt[], size_t salt_length);

Expand Down
24 changes: 12 additions & 12 deletions src/lib/block/camellia/camellia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,19 @@ inline uint64_t left_rot_lo(uint64_t h, uint64_t l, size_t shift) {
/*
* Camellia Key Schedule
*/
void key_schedule(secure_vector<uint64_t>& SK, const uint8_t key[], size_t length) {
void key_schedule(secure_vector<uint64_t>& SK, std::span<const uint8_t> key) {
const uint64_t Sigma1 = 0xA09E667F3BCC908B;
const uint64_t Sigma2 = 0xB67AE8584CAA73B2;
const uint64_t Sigma3 = 0xC6EF372FE94F82BE;
const uint64_t Sigma4 = 0x54FF53A5F1D36F1C;
const uint64_t Sigma5 = 0x10E527FADE682D1D;
const uint64_t Sigma6 = 0xB05688C2B3E6C1FD;

const uint64_t KL_H = load_be<uint64_t>(key, 0);
const uint64_t KL_L = load_be<uint64_t>(key, 1);
const uint64_t KL_H = load_be<uint64_t>(key.data(), 0);
const uint64_t KL_L = load_be<uint64_t>(key.data(), 1);

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

uint64_t D1 = KL_H ^ KR_H;
uint64_t D2 = KL_L ^ KR_L;
Expand All @@ -261,7 +261,7 @@ void key_schedule(secure_vector<uint64_t>& SK, const uint8_t key[], size_t lengt
const uint64_t KB_H = D1;
const uint64_t KB_L = D2;

if(length == 16) {
if(key.size() == 16) {
SK.resize(26);

SK[0] = KL_H;
Expand Down Expand Up @@ -382,16 +382,16 @@ bool Camellia_256::has_keying_material() const {
return !m_SK.empty();
}

void Camellia_128::key_schedule(const uint8_t key[], size_t length) {
Camellia_F::key_schedule(m_SK, key, length);
void Camellia_128::key_schedule(std::span<const uint8_t> key) {
Camellia_F::key_schedule(m_SK, key);
}

void Camellia_192::key_schedule(const uint8_t key[], size_t length) {
Camellia_F::key_schedule(m_SK, key, length);
void Camellia_192::key_schedule(std::span<const uint8_t> key) {
Camellia_F::key_schedule(m_SK, key);
}

void Camellia_256::key_schedule(const uint8_t key[], size_t length) {
Camellia_F::key_schedule(m_SK, key, length);
void Camellia_256::key_schedule(std::span<const uint8_t> key) {
Camellia_F::key_schedule(m_SK, key);
}

void Camellia_128::clear() {
Expand Down
Loading

0 comments on commit 5d06bb0

Please sign in to comment.