Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
16 void aont_package(RandomNumberGenerator& rng,
17 BlockCipher* cipher,
18 const uint8_t input[], size_t input_len,
19 uint8_t 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<uint8_t> buf(BLOCK_SIZE);
41
42 const size_t blocks =
43 (input_len + BLOCK_SIZE - 1) / BLOCK_SIZE;
44
45 uint8_t* 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
69 void aont_unpackage(BlockCipher* cipher,
70 const uint8_t input[], size_t input_len,
71 uint8_t 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<uint8_t> package_key(BLOCK_SIZE);
87 secure_vector<uint8_t> 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 }
Botan::aont_package
void aont_package(RandomNumberGenerator &rng, BlockCipher *cipher, const uint8_t input[], size_t input_len, uint8_t output[])
Definition: package.cpp:16
Botan::xor_buf
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Botan::BlockCipher::encrypt
void encrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:80
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::StreamCipher_Filter
Definition: filters.h:31
Botan::OctetString
Definition: symkey.h:19
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::Pipe::remaining
size_t remaining(message_id msg=DEFAULT_MESSAGE) const BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:130
Botan::clear_mem
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
Botan::CTR_BE
Definition: ctr.h:19
Botan::Pipe::read
size_t read(uint8_t output[], size_t length) override BOTAN_WARN_UNUSED_RESULT
Definition: pipe_rw.cpp:81
Botan::SymmetricAlgorithm::valid_keylength
bool valid_keylength(size_t length) const
Definition: sym_algo.h:57
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
BOTAN_ASSERT_EQUAL
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:53
Botan::Pipe::process_msg
void process_msg(const uint8_t in[], size_t length)
Definition: pipe.cpp:117
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Botan
Definition: alg_id.cpp:13
Botan::SymmetricKey
OctetString SymmetricKey
Definition: symkey.h:136
Botan::aont_unpackage
void aont_unpackage(BlockCipher *cipher, const uint8_t input[], size_t input_len, uint8_t output[])
Definition: package.cpp:69
Botan::get_byte
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
Botan::Pipe
Definition: pipe.h:27
Botan::OctetString::begin
const uint8_t * begin() const
Definition: symkey.h:36
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::BlockCipher
Definition: block_cipher.h:19
Botan::zeroise
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:211