Botan  1.11.34
Crypto and TLS for C++11
package.cpp
Go to the documentation of this file.
1 /*
2 * Rivest's Package Tranform
3 *
4 * (C) 2009 Jack Lloyd
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/package.h>
10 #include <botan/filters.h>
11 #include <botan/ctr.h>
12 #include <botan/loadstor.h>
13 
14 namespace Botan {
15 
17  BlockCipher* cipher,
18  const byte input[], size_t input_len,
19  byte output[])
20  {
21  const size_t BLOCK_SIZE = cipher->block_size();
22 
23  if(!cipher->valid_keylength(BLOCK_SIZE))
24  throw Invalid_Argument("AONT::package: Invalid cipher");
25 
26  // The all-zero string which is used both as the CTR IV and as K0
27  const std::string all_zeros(BLOCK_SIZE*2, '0');
28 
29  SymmetricKey package_key(rng, BLOCK_SIZE);
30 
31  Pipe pipe(new StreamCipher_Filter(new CTR_BE(cipher), package_key));
32 
33  pipe.process_msg(input, input_len);
34  const size_t remaining = pipe.remaining();
35  BOTAN_ASSERT_EQUAL(remaining, pipe.read(output, remaining), "Expected read size");
36 
37  // Set K0 (the all zero key)
38  cipher->set_key(SymmetricKey(all_zeros));
39 
40  secure_vector<byte> buf(BLOCK_SIZE);
41 
42  const size_t blocks =
43  (input_len + BLOCK_SIZE - 1) / BLOCK_SIZE;
44 
45  byte* final_block = output + input_len;
46  clear_mem(final_block, BLOCK_SIZE);
47 
48  // XOR the hash blocks into the final block
49  for(size_t i = 0; i != blocks; ++i)
50  {
51  const size_t left = std::min<size_t>(BLOCK_SIZE,
52  input_len - BLOCK_SIZE * i);
53 
54  zeroise(buf);
55  copy_mem(buf.data(), output + (BLOCK_SIZE * i), left);
56 
57  for(size_t j = 0; j != sizeof(i); ++j)
58  buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i);
59 
60  cipher->encrypt(buf.data());
61 
62  xor_buf(final_block, buf.data(), BLOCK_SIZE);
63  }
64 
65  // XOR the random package key into the final block
66  xor_buf(final_block, package_key.begin(), BLOCK_SIZE);
67  }
68 
70  const byte input[], size_t input_len,
71  byte output[])
72  {
73  const size_t BLOCK_SIZE = cipher->block_size();
74 
75  if(!cipher->valid_keylength(BLOCK_SIZE))
76  throw Invalid_Argument("AONT::unpackage: Invalid cipher");
77 
78  if(input_len < BLOCK_SIZE)
79  throw Invalid_Argument("AONT::unpackage: Input too short");
80 
81  // The all-zero string which is used both as the CTR IV and as K0
82  const std::string all_zeros(BLOCK_SIZE*2, '0');
83 
84  cipher->set_key(SymmetricKey(all_zeros));
85 
86  secure_vector<byte> package_key(BLOCK_SIZE);
87  secure_vector<byte> buf(BLOCK_SIZE);
88 
89  // Copy the package key (masked with the block hashes)
90  copy_mem(package_key.data(),
91  input + (input_len - BLOCK_SIZE),
92  BLOCK_SIZE);
93 
94  const size_t blocks = ((input_len - 1) / BLOCK_SIZE);
95 
96  // XOR the blocks into the package key bits
97  for(size_t i = 0; i != blocks; ++i)
98  {
99  const size_t left = std::min<size_t>(BLOCK_SIZE,
100  input_len - BLOCK_SIZE * (i+1));
101 
102  zeroise(buf);
103  copy_mem(buf.data(), input + (BLOCK_SIZE * i), left);
104 
105  for(size_t j = 0; j != sizeof(i); ++j)
106  buf[BLOCK_SIZE - 1 - j] ^= get_byte(sizeof(i)-1-j, i);
107 
108  cipher->encrypt(buf.data());
109 
110  xor_buf(package_key.data(), buf.data(), BLOCK_SIZE);
111  }
112 
113  Pipe pipe(new StreamCipher_Filter(new CTR_BE(cipher), package_key));
114 
115  pipe.process_msg(input, input_len - BLOCK_SIZE);
116 
117  const size_t remaining = pipe.remaining();
118  BOTAN_ASSERT_EQUAL(remaining, pipe.read(output, remaining), "Expected read size");
119  }
120 
121 }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
size_t read(byte output[], size_t length) override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:81
size_t remaining(message_id msg=DEFAULT_MESSAGE) const BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:130
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
byte get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
const byte * begin() const
Definition: symkey.h:36
bool valid_keylength(size_t length) const
Definition: sym_algo.h:57
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:80
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:53
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
OctetString SymmetricKey
Definition: symkey.h:136
void aont_package(RandomNumberGenerator &rng, BlockCipher *cipher, const byte input[], size_t input_len, byte output[])
Definition: package.cpp:16
void aont_unpackage(BlockCipher *cipher, const byte input[], size_t input_len, byte output[])
Definition: package.cpp:69
virtual size_t block_size() const =0
void process_msg(const byte in[], size_t length)
Definition: pipe.cpp:117
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:203
std::uint8_t byte
Definition: types.h:31