Skip to content

Commit

Permalink
Renamed files and classes according to new revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
FAlbertDev committed Jun 14, 2024
1 parent 58d1004 commit bab8300
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 97 deletions.
16 changes: 8 additions & 8 deletions src/lib/kdf/kdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
#endif

#if defined(BOTAN_HAS_SP800_56A)
#include <botan/internal/sp800_56a.h>
#include <botan/internal/sp800_56c_one_step.h>
#endif

#if defined(BOTAN_HAS_SP800_56C)
#include <botan/internal/sp800_56c.h>
#include <botan/internal/sp800_56c_two_step.h>
#endif

namespace Botan {
Expand Down Expand Up @@ -160,16 +160,16 @@ std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view pr
#if defined(BOTAN_HAS_SP800_56A)
if(req.algo_name() == "SP800-56A" && req.arg_count() == 1) {
if(auto hash = HashFunction::create(req.arg(0))) {
return std::make_unique<SP800_56A_Hash>(std::move(hash));
return std::make_unique<SP800_56C_One_Step_Hash>(std::move(hash));
}
if(req.arg(0) == "KMAC-128") {
return std::make_unique<SP800_56A_KMAC128>();
return std::make_unique<SP800_56C_One_Step_KMAC128>();
}
if(req.arg(0) == "KMAC-256") {
return std::make_unique<SP800_56A_KMAC256>();
return std::make_unique<SP800_56C_One_Step_KMAC256>();
}
if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
return std::make_unique<SP800_56A_HMAC>(std::move(mac));
return std::make_unique<SP800_56C_One_Step_HMAC>(std::move(mac));
}
}
#endif
Expand All @@ -179,11 +179,11 @@ std::unique_ptr<KDF> KDF::create(std::string_view algo_spec, std::string_view pr
std::unique_ptr<KDF> exp(kdf_create_mac_or_hash<SP800_108_Feedback>(req.arg(0)));
if(exp) {
if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
return std::make_unique<SP800_56C>(std::move(mac), std::move(exp));
return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
}

if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", req.arg(0)))) {
return std::make_unique<SP800_56C>(std::move(mac), std::move(exp));
return std::make_unique<SP800_56C_Two_Step>(std::move(mac), std::move(exp));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/sp800_56a.h>
#include <botan/internal/sp800_56c_one_step.h>

#include <botan/exceptn.h>
#include <botan/internal/bit_ops.h>
Expand Down Expand Up @@ -77,14 +77,14 @@ void kdm_internal(std::span<uint8_t> output_buffer,

} // namespace

void SP800_56A_Hash::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
void SP800_56C_One_Step_Hash::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
BOTAN_UNUSED(salt);
BOTAN_ARG_CHECK(salt_len == 0, "SP800_56A_Hash does not support a non-empty salt");

Expand All @@ -95,29 +95,30 @@ void SP800_56A_Hash::kdf(uint8_t key[],
});
}

std::string SP800_56A_Hash::name() const {
std::string SP800_56C_One_Step_Hash::name() const {
return fmt("SP800-56A({})", m_hash->name());
}

std::unique_ptr<KDF> SP800_56A_Hash::new_object() const {
return std::make_unique<SP800_56A_Hash>(m_hash->new_object());
std::unique_ptr<KDF> SP800_56C_One_Step_Hash::new_object() const {
return std::make_unique<SP800_56C_One_Step_Hash>(m_hash->new_object());
}

SP800_56A_HMAC::SP800_56A_HMAC(std::unique_ptr<MessageAuthenticationCode> mac) : m_mac(std::move(mac)) {
SP800_56C_One_Step_HMAC::SP800_56C_One_Step_HMAC(std::unique_ptr<MessageAuthenticationCode> mac) :
m_mac(std::move(mac)) {
// TODO: we need a MessageAuthenticationCode::is_hmac
if(!m_mac->name().starts_with("HMAC(")) {
throw Algorithm_Not_Found("Only HMAC can be used with SP800_56A_HMAC");
}
}

void SP800_56A_HMAC::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
void SP800_56C_One_Step_HMAC::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
kdm_internal({key, key_len}, {secret, secret_len}, {label, label_len}, *m_mac, [&](Buffered_Computation* kdf) {
MessageAuthenticationCode* kdf_mac = dynamic_cast<MessageAuthenticationCode*>(kdf);
BOTAN_ASSERT_NONNULL(kdf_mac);
Expand All @@ -134,23 +135,23 @@ void SP800_56A_HMAC::kdf(uint8_t key[],
});
}

std::string SP800_56A_HMAC::name() const {
std::string SP800_56C_One_Step_HMAC::name() const {
return fmt("SP800-56A({})", m_mac->name());
}

std::unique_ptr<KDF> SP800_56A_HMAC::new_object() const {
return std::make_unique<SP800_56A_HMAC>(m_mac->new_object());
std::unique_ptr<KDF> SP800_56C_One_Step_HMAC::new_object() const {
return std::make_unique<SP800_56C_One_Step_HMAC>(m_mac->new_object());
}

// Option 3 - KMAC
void SP800_56A_KMAC_Abstract::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
void SP800_56A_One_Step_KMAC_Abstract::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
auto mac = create_kmac_instance(key_len);
kdm_internal({key, key_len}, {secret, secret_len}, {label, label_len}, *mac, [&](Buffered_Computation* kdf) {
MessageAuthenticationCode* kdf_mac = dynamic_cast<MessageAuthenticationCode*>(kdf);
Expand Down Expand Up @@ -178,11 +179,13 @@ void SP800_56A_KMAC_Abstract::kdf(uint8_t key[],
});
}

std::unique_ptr<MessageAuthenticationCode> SP800_56A_KMAC128::create_kmac_instance(size_t output_byte_len) const {
std::unique_ptr<MessageAuthenticationCode> SP800_56C_One_Step_KMAC128::create_kmac_instance(
size_t output_byte_len) const {
return std::make_unique<KMAC128>(output_byte_len * 8);
}

std::unique_ptr<MessageAuthenticationCode> SP800_56A_KMAC256::create_kmac_instance(size_t output_byte_len) const {
std::unique_ptr<MessageAuthenticationCode> SP800_56C_One_Step_KMAC256::create_kmac_instance(
size_t output_byte_len) const {
return std::make_unique<KMAC256>(output_byte_len * 8);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Botan {
* NIST SP 800-56Cr2 One-Step KDF using hash function
* @warning This KDF ignores the provided salt value
*/
class SP800_56A_Hash final : public KDF {
class SP800_56C_One_Step_Hash final : public KDF {
public:
std::string name() const override;

Expand Down Expand Up @@ -56,7 +56,7 @@ class SP800_56A_Hash final : public KDF {
/**
* @param hash the hash function to use as the auxiliary function
*/
explicit SP800_56A_Hash(std::unique_ptr<HashFunction> hash) : m_hash(std::move(hash)) {}
explicit SP800_56C_One_Step_Hash(std::unique_ptr<HashFunction> hash) : m_hash(std::move(hash)) {}

private:
std::unique_ptr<HashFunction> m_hash;
Expand All @@ -65,7 +65,7 @@ class SP800_56A_Hash final : public KDF {
/**
* NIST SP800-56Cr2 One-Step KDF using HMAC
*/
class SP800_56A_HMAC final : public KDF {
class SP800_56C_One_Step_HMAC final : public KDF {
public:
std::string name() const override;

Expand Down Expand Up @@ -100,7 +100,7 @@ class SP800_56A_HMAC final : public KDF {
/**
* @param mac the HMAC to use as the auxiliary function
*/
explicit SP800_56A_HMAC(std::unique_ptr<MessageAuthenticationCode> mac);
explicit SP800_56C_One_Step_HMAC(std::unique_ptr<MessageAuthenticationCode> mac);

private:
std::unique_ptr<MessageAuthenticationCode> m_mac;
Expand All @@ -109,7 +109,7 @@ class SP800_56A_HMAC final : public KDF {
/**
* NIST SP800-56Cr2 One-Step KDF using KMAC (Abstract class)
*/
class SP800_56A_KMAC_Abstract : public KDF {
class SP800_56A_One_Step_KMAC_Abstract : public KDF {
public:
/**
* Derive a key using the SP800-56A KDF.
Expand Down Expand Up @@ -144,11 +144,11 @@ class SP800_56A_KMAC_Abstract : public KDF {
/**
* NIST SP800-56Cr2 One-Step KDF using KMAC-128
*/
class SP800_56A_KMAC128 final : public SP800_56A_KMAC_Abstract {
class SP800_56C_One_Step_KMAC128 final : public SP800_56A_One_Step_KMAC_Abstract {
public:
std::string name() const override { return "SP800-56A(KMAC-128)"; }

std::unique_ptr<KDF> new_object() const override { return std::make_unique<SP800_56A_KMAC128>(); }
std::unique_ptr<KDF> new_object() const override { return std::make_unique<SP800_56C_One_Step_KMAC128>(); }

protected:
std::unique_ptr<MessageAuthenticationCode> create_kmac_instance(size_t output_byte_len) const override;
Expand All @@ -157,11 +157,11 @@ class SP800_56A_KMAC128 final : public SP800_56A_KMAC_Abstract {
/**
* NIST SP800-56Cr2 One-Step KDF using KMAC-256
*/
class SP800_56A_KMAC256 final : public SP800_56A_KMAC_Abstract {
class SP800_56C_One_Step_KMAC256 final : public SP800_56A_One_Step_KMAC_Abstract {
public:
std::string name() const override { return "SP800-56A(KMAC-256)"; }

std::unique_ptr<KDF> new_object() const override { return std::make_unique<SP800_56A_KMAC256>(); }
std::unique_ptr<KDF> new_object() const override { return std::make_unique<SP800_56C_One_Step_KMAC256>(); }

protected:
std::unique_ptr<MessageAuthenticationCode> create_kmac_instance(size_t output_byte_len) const override;
Expand Down
41 changes: 0 additions & 41 deletions src/lib/kdf/sp800_56c/sp800_56c.cpp

This file was deleted.

41 changes: 41 additions & 0 deletions src/lib/kdf/sp800_56c/sp800_56c_two_step.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Two-Step KDF defined in NIST SP 800-56Cr2 (Section 5)
* (C) 2016 Kai Michaelis
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/sp800_56c_two_step.h>

#include <botan/internal/fmt.h>

namespace Botan {

std::string SP800_56C_Two_Step::name() const {
return fmt("SP800-56C({})", m_prf->name());
}

std::unique_ptr<KDF> SP800_56C_Two_Step::new_object() const {
return std::make_unique<SP800_56C_Two_Step>(m_prf->new_object(), m_exp->new_object());
}

void SP800_56C_Two_Step::kdf(uint8_t key[],
size_t key_len,
const uint8_t secret[],
size_t secret_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t label[],
size_t label_len) const {
// Randomness Extraction
secure_vector<uint8_t> k_dk;

m_prf->set_key(salt, salt_len);
m_prf->update(secret, secret_len);
m_prf->final(k_dk);

// Key Expansion
m_exp->kdf(key, key_len, k_dk.data(), k_dk.size(), nullptr, 0, label, label_len);
}

} // namespace Botan
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* KDF defined in NIST SP 800-56c
* Two-Step KDF defined in NIST SP 800-56Cr2 (Section 5)
* (C) 2016 Kai Michaelis
*
* Botan is released under the Simplified BSD License (see license.txt)
Expand All @@ -14,16 +14,16 @@
namespace Botan {

/**
* NIST SP 800-56C KDF
* NIST SP 800-56C Two-Step KDF (Section 5)
*/
class SP800_56C final : public KDF {
class SP800_56C_Two_Step final : public KDF {
public:
std::string name() const override;

std::unique_ptr<KDF> new_object() const override;

/**
* Derive a key using the SP800-56C KDF.
* Derive a key using the SP800-56C Two-Step KDF.
*
* The implementation hard codes the context value for the
* expansion step to the empty string.
Expand All @@ -50,7 +50,7 @@ class SP800_56C final : public KDF {
* @param mac MAC algorithm used for randomness extraction
* @param exp KDF used for key expansion
*/
SP800_56C(std::unique_ptr<MessageAuthenticationCode> mac, std::unique_ptr<KDF> exp) :
SP800_56C_Two_Step(std::unique_ptr<MessageAuthenticationCode> mac, std::unique_ptr<KDF> exp) :
m_prf(std::move(mac)), m_exp(std::move(exp)) {}

private:
Expand Down

0 comments on commit bab8300

Please sign in to comment.