8 #include <botan/rfc3394.h>     9 #include <botan/algo_factory.h>    10 #include <botan/block_cipher.h>    11 #include <botan/loadstor.h>    12 #include <botan/exceptn.h>    13 #include <botan/internal/xor_buf.h>    20 BlockCipher* make_aes(
size_t keylength,
    21                       Algorithm_Factory& af)
    24       return af.make_block_cipher(
"AES-128");
    25    else if(keylength == 24)
    26       return af.make_block_cipher(
"AES-192");
    27    else if(keylength == 32)
    28       return af.make_block_cipher(
"AES-256");
    30       throw std::invalid_argument(
"Bad KEK length for NIST keywrap");
    39    if(key.
size() % 8 != 0)
    40       throw std::invalid_argument(
"Bad input key size for NIST key wrap");
    42    std::auto_ptr<BlockCipher> aes(make_aes(kek.
length(), af));
    45    const size_t n = key.
size() / 8;
    50    for(
size_t i = 0; i != 8; ++i)
    55    for(
size_t j = 0; j <= 5; ++j)
    57       for(
size_t i = 1; i <= n; ++i)
    59          const u32bit t = (n * j) + i;
    66          byte t_buf[4] = { 0 };
    81    if(key.
size() < 16 || key.
size() % 8 != 0)
    82       throw std::invalid_argument(
"Bad input key size for NIST key unwrap");
    84    std::auto_ptr<BlockCipher> aes(make_aes(kek.
length(), af));
    87    const size_t n = (key.
size() - 8) / 8;
    92    for(
size_t i = 0; i != 8; ++i)
    97    for(
size_t j = 0; j <= 5; ++j)
    99       for(
size_t i = n; i != 0; --i)
   101          const u32bit t = (5 - j) * n + i;
   103          byte t_buf[4] = { 0 };
 SecureVector< byte > rfc3394_keywrap(const MemoryRegion< byte > &key, const SymmetricKey &kek, Algorithm_Factory &af)
SecureVector< byte > rfc3394_keyunwrap(const MemoryRegion< byte > &key, const SymmetricKey &kek, Algorithm_Factory &af)
void copy_mem(T *out, const T *in, size_t n)
u64bit load_be< u64bit >(const byte in[], size_t off)
void store_be(u16bit in, byte out[2])
void xor_buf(byte out[], const byte in[], size_t length)