diff options
author | Jack Lloyd <[email protected]> | 2017-11-19 21:38:37 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-11-19 21:38:37 -0500 |
commit | 30367246fce5615b3a0f2b9da0a3d614509cbbb6 (patch) | |
tree | daf6b5a425fe5abdb77b37301b950c3422fb1937 /src/lib/misc/rfc3394 | |
parent | 148f43b60917d5c6b8d0ad1204cd51e1841a2855 (diff) | |
parent | fea9c14d9696615f9d1cf52e0bb578c8a54c2c6a (diff) |
Merge #1301 Add AES key wrap with padding
Diffstat (limited to 'src/lib/misc/rfc3394')
-rw-r--r-- | src/lib/misc/rfc3394/info.txt | 1 | ||||
-rw-r--r-- | src/lib/misc/rfc3394/rfc3394.cpp | 76 | ||||
-rw-r--r-- | src/lib/misc/rfc3394/rfc3394.h | 8 |
3 files changed, 11 insertions, 74 deletions
diff --git a/src/lib/misc/rfc3394/info.txt b/src/lib/misc/rfc3394/info.txt index ac56ee833..075834219 100644 --- a/src/lib/misc/rfc3394/info.txt +++ b/src/lib/misc/rfc3394/info.txt @@ -4,4 +4,5 @@ RFC3394_KEYWRAP -> 20131128 <requires> aes +nist_keywrap </requires> diff --git a/src/lib/misc/rfc3394/rfc3394.cpp b/src/lib/misc/rfc3394/rfc3394.cpp index 3bb792723..8e69933c7 100644 --- a/src/lib/misc/rfc3394/rfc3394.cpp +++ b/src/lib/misc/rfc3394/rfc3394.cpp @@ -6,18 +6,14 @@ */ #include <botan/rfc3394.h> +#include <botan/nist_keywrap.h> #include <botan/block_cipher.h> -#include <botan/loadstor.h> -#include <botan/exceptn.h> namespace Botan { secure_vector<uint8_t> rfc3394_keywrap(const secure_vector<uint8_t>& key, - const SymmetricKey& kek) + const SymmetricKey& kek) { - if(key.size() % 8 != 0) - throw Invalid_Argument("Bad input key size for NIST key wrap"); - if(kek.size() != 16 && kek.size() != 24 && kek.size() != 32) throw Invalid_Argument("Bad KEK length " + std::to_string(kek.size()) + " for NIST key wrap"); @@ -25,40 +21,12 @@ secure_vector<uint8_t> rfc3394_keywrap(const secure_vector<uint8_t>& key, std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name)); aes->set_key(kek); - const size_t n = key.size() / 8; - - secure_vector<uint8_t> R((n + 1) * 8); - secure_vector<uint8_t> A(16); - - for(size_t i = 0; i != 8; ++i) - A[i] = 0xA6; - - copy_mem(&R[8], key.data(), key.size()); - - for(size_t j = 0; j <= 5; ++j) - { - for(size_t i = 1; i <= n; ++i) - { - const uint32_t t = static_cast<uint32_t>((n * j) + i); - - copy_mem(&A[8], &R[8*i], 8); - - aes->encrypt(A.data()); - copy_mem(&R[8*i], &A[8], 8); - - uint8_t t_buf[4] = { 0 }; - store_be(t, t_buf); - xor_buf(&A[4], t_buf, 4); - } - } - - copy_mem(R.data(), A.data(), 8); - - return R; + std::vector<uint8_t> wrapped = nist_key_wrap(key.data(), key.size(), *aes); + return secure_vector<uint8_t>(wrapped.begin(), wrapped.end()); } secure_vector<uint8_t> rfc3394_keyunwrap(const secure_vector<uint8_t>& key, - const SymmetricKey& kek) + const SymmetricKey& kek) { if(key.size() < 16 || key.size() % 8 != 0) throw Invalid_Argument("Bad input key size for NIST key unwrap"); @@ -70,39 +38,7 @@ secure_vector<uint8_t> rfc3394_keyunwrap(const secure_vector<uint8_t>& key, std::unique_ptr<BlockCipher> aes(BlockCipher::create_or_throw(cipher_name)); aes->set_key(kek); - const size_t n = (key.size() - 8) / 8; - - secure_vector<uint8_t> R(n * 8); - secure_vector<uint8_t> A(16); - - for(size_t i = 0; i != 8; ++i) - A[i] = key[i]; - - copy_mem(R.data(), &key[8], key.size() - 8); - - for(size_t j = 0; j <= 5; ++j) - { - for(size_t i = n; i != 0; --i) - { - const uint32_t t = static_cast<uint32_t>((5 - j) * n + i); - - uint8_t t_buf[4] = { 0 }; - store_be(t, t_buf); - - xor_buf(&A[4], t_buf, 4); - - copy_mem(&A[8], &R[8*(i-1)], 8); - - aes->decrypt(A.data()); - - copy_mem(&R[8*(i-1)], &A[8], 8); - } - } - - if(load_be<uint64_t>(A.data(), 0) != 0xA6A6A6A6A6A6A6A6) - throw Integrity_Failure("NIST key unwrap failed"); - - return R; + return nist_key_unwrap(key.data(), key.size(), *aes); } } diff --git a/src/lib/misc/rfc3394/rfc3394.h b/src/lib/misc/rfc3394/rfc3394.h index 77e602fda..9cfcfaaf6 100644 --- a/src/lib/misc/rfc3394/rfc3394.h +++ b/src/lib/misc/rfc3394/rfc3394.h @@ -5,8 +5,8 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#ifndef BOTAN_AES_KEY_WRAP_H_ -#define BOTAN_AES_KEY_WRAP_H_ +#ifndef BOTAN_RFC3394_H_ +#define BOTAN_RFC3394_H_ #include <botan/symkey.h> @@ -21,7 +21,7 @@ namespace Botan { * @return key encrypted under kek */ secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) rfc3394_keywrap(const secure_vector<uint8_t>& key, - const SymmetricKey& kek); + const SymmetricKey& kek); /** * Decrypt a key under a key encryption key using the algorithm @@ -32,7 +32,7 @@ secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) rfc3394_keywrap(const secure_vector * @return key decrypted under kek */ secure_vector<uint8_t> BOTAN_PUBLIC_API(2,0) rfc3394_keyunwrap(const secure_vector<uint8_t>& key, - const SymmetricKey& kek); + const SymmetricKey& kek); } |