diff options
Diffstat (limited to 'src/lib/kdf/sp800_108')
-rw-r--r-- | src/lib/kdf/sp800_108/sp800_108.cpp | 208 |
1 files changed, 104 insertions, 104 deletions
diff --git a/src/lib/kdf/sp800_108/sp800_108.cpp b/src/lib/kdf/sp800_108/sp800_108.cpp index 0f9eed1f2..137ade106 100644 --- a/src/lib/kdf/sp800_108/sp800_108.cpp +++ b/src/lib/kdf/sp800_108/sp800_108.cpp @@ -16,39 +16,39 @@ size_t SP800_108_Counter::kdf(uint8_t key[], size_t key_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const { - const std::size_t prf_len = m_prf->output_length(); - const uint8_t delim = 0; - uint8_t *p = key; - uint32_t counter = 1; - uint32_t length = key_len * 8; - uint8_t be_len[4] = { 0 }; - secure_vector<uint8_t> tmp; + const std::size_t prf_len = m_prf->output_length(); + const uint8_t delim = 0; + const uint32_t length = static_cast<uint32_t>(key_len * 8); - store_be(length, be_len); - m_prf->set_key(secret, secret_len); + uint8_t *p = key; + uint32_t counter = 1; + uint8_t be_len[4] = { 0 }; + secure_vector<uint8_t> tmp; - while(p < key + key_len && counter != 0) - { - const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); - uint8_t be_cnt[4] = { 0 }; + store_be(length, be_len); + m_prf->set_key(secret, secret_len); - store_be(counter, be_cnt); + while(p < key + key_len && counter != 0) + { + const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); + uint8_t be_cnt[4] = { 0 }; - m_prf->update(be_cnt,4); - m_prf->update(label,label_len); - m_prf->update(delim); - m_prf->update(salt,salt_len); - m_prf->update(be_len,4); - m_prf->final(tmp); + store_be(counter, be_cnt); - std::move(tmp.begin(), tmp.begin() + to_copy, p); - ++counter; + m_prf->update(be_cnt,4); + m_prf->update(label,label_len); + m_prf->update(delim); + m_prf->update(salt,salt_len); + m_prf->update(be_len,4); + m_prf->final(tmp); - if (counter == 0) - throw Invalid_Argument("Can't process more than 4GB"); + copy_mem(p, tmp.data(), to_copy); + p += to_copy; - p += to_copy; - } + ++counter; + if(counter == 0) + throw Invalid_Argument("Can't process more than 4GB"); + } return key_len; } @@ -58,98 +58,98 @@ size_t SP800_108_Feedback::kdf(uint8_t key[], size_t key_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const { - const std::size_t prf_len = m_prf->output_length(); - const std::size_t iv_len = (salt_len >= prf_len ? prf_len : 0); - const uint8_t delim = 0; + const uint32_t length = static_cast<uint32_t>(key_len * 8); + const std::size_t prf_len = m_prf->output_length(); + const std::size_t iv_len = (salt_len >= prf_len ? prf_len : 0); + const uint8_t delim = 0; - uint8_t *p = key; - uint32_t counter = 1; - uint32_t length = key_len * 8; - uint8_t be_len[4] = { 0 }; - secure_vector< uint8_t > prev(salt, salt + iv_len); - secure_vector< uint8_t > ctx(salt + iv_len, salt + salt_len); + uint8_t *p = key; + uint32_t counter = 1; + uint8_t be_len[4] = { 0 }; + secure_vector< uint8_t > prev(salt, salt + iv_len); + secure_vector< uint8_t > ctx(salt + iv_len, salt + salt_len); - store_be(length, be_len); - m_prf->set_key(secret, secret_len); + store_be(length, be_len); + m_prf->set_key(secret, secret_len); - while(p < key + key_len && counter != 0) - { - const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); - uint8_t be_cnt[4] = { 0 }; + while(p < key + key_len && counter != 0) + { + const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); + uint8_t be_cnt[4] = { 0 }; - store_be(counter, be_cnt); + store_be(counter, be_cnt); - m_prf->update(prev); - m_prf->update(be_cnt,4); - m_prf->update(label,label_len); - m_prf->update(delim); - m_prf->update(ctx); - m_prf->update(be_len,4); - m_prf->final(prev); + m_prf->update(prev); + m_prf->update(be_cnt,4); + m_prf->update(label,label_len); + m_prf->update(delim); + m_prf->update(ctx); + m_prf->update(be_len,4); + m_prf->final(prev); - std::copy(prev.begin(), prev.begin() + to_copy, p); - ++counter; + copy_mem(p, prev.data(), to_copy); + p += to_copy; - if (counter == 0) - throw Invalid_Argument("Can't process more than 4GB"); + ++counter; - p += to_copy; - } + if(counter == 0) + throw Invalid_Argument("Can't process more than 4GB"); + } return key_len; } size_t SP800_108_Pipeline::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 + 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 { - const std::size_t prf_len = m_prf->output_length(); - const uint8_t delim = 0; - - uint8_t *p = key; - uint32_t counter = 1; - uint32_t length = key_len * 8; - uint8_t be_len[4] = { 0 }; - secure_vector<uint8_t> ai, ki; - - store_be(length, be_len); - m_prf->set_key(secret,secret_len); - - // A(0) - std::copy(label,label + label_len,std::back_inserter(ai)); - ai.emplace_back(delim); - std::copy(salt,salt + salt_len,std::back_inserter(ai)); - std::copy(be_len,be_len + 4,std::back_inserter(ai)); - - while(p < key + key_len && counter != 0) - { - // A(i) - m_prf->update(ai); - m_prf->final(ai); - - // K(i) - const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); - uint8_t be_cnt[4] = { 0 }; - - store_be(counter, be_cnt); - - m_prf->update(ai); - m_prf->update(be_cnt,4); - m_prf->update(label, label_len); - m_prf->update(delim); - m_prf->update(salt, salt_len); - m_prf->update(be_len,4); - m_prf->final(ki); - - std::copy(ki.begin(), ki.begin() + to_copy, p); - ++counter; - - if (counter == 0) - throw Invalid_Argument("Can't process more than 4GB"); - - p += to_copy; - } + const uint32_t length = static_cast<uint32_t>(key_len * 8); + const std::size_t prf_len = m_prf->output_length(); + const uint8_t delim = 0; + + uint8_t *p = key; + uint32_t counter = 1; + uint8_t be_len[4] = { 0 }; + secure_vector<uint8_t> ai, ki; + + store_be(length, be_len); + m_prf->set_key(secret,secret_len); + + // A(0) + std::copy(label,label + label_len,std::back_inserter(ai)); + ai.emplace_back(delim); + std::copy(salt,salt + salt_len,std::back_inserter(ai)); + std::copy(be_len,be_len + 4,std::back_inserter(ai)); + + while(p < key + key_len && counter != 0) + { + // A(i) + m_prf->update(ai); + m_prf->final(ai); + + // K(i) + const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len); + uint8_t be_cnt[4] = { 0 }; + + store_be(counter, be_cnt); + + m_prf->update(ai); + m_prf->update(be_cnt,4); + m_prf->update(label, label_len); + m_prf->update(delim); + m_prf->update(salt, salt_len); + m_prf->update(be_len,4); + m_prf->final(ki); + + copy_mem(p, ki.data(), to_copy); + p += to_copy; + + ++counter; + + if(counter == 0) + throw Invalid_Argument("Can't process more than 4GB"); + } return key_len; } |