Botan 1.10.17
lion.cpp
Go to the documentation of this file.
1/*
2* Lion
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/lion.h>
9#include <botan/internal/xor_buf.h>
10#include <botan/parsing.h>
11
12namespace Botan {
13
14/*
15* Lion Encryption
16*/
17void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const
18 {
19 SecureVector<byte> buffer_vec(LEFT_SIZE);
20 byte* buffer = &buffer_vec[0];
21
22 for(size_t i = 0; i != blocks; ++i)
23 {
24 xor_buf(buffer, in, &key1[0], LEFT_SIZE);
25 cipher->set_key(buffer, LEFT_SIZE);
26 cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
27
28 hash->update(out + LEFT_SIZE, RIGHT_SIZE);
29 hash->final(buffer);
30 xor_buf(out, in, buffer, LEFT_SIZE);
31
32 xor_buf(buffer, out, &key2[0], LEFT_SIZE);
33 cipher->set_key(buffer, LEFT_SIZE);
34 cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
35
36 in += BLOCK_SIZE;
37 out += BLOCK_SIZE;
38 }
39 }
40
41/*
42* Lion Decryption
43*/
44void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const
45 {
46 SecureVector<byte> buffer_vec(LEFT_SIZE);
47 byte* buffer = &buffer_vec[0];
48
49 for(size_t i = 0; i != blocks; ++i)
50 {
51 xor_buf(buffer, in, &key2[0], LEFT_SIZE);
52 cipher->set_key(buffer, LEFT_SIZE);
53 cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
54
55 hash->update(out + LEFT_SIZE, RIGHT_SIZE);
56 hash->final(buffer);
57 xor_buf(out, in, buffer, LEFT_SIZE);
58
59 xor_buf(buffer, out, &key1[0], LEFT_SIZE);
60 cipher->set_key(buffer, LEFT_SIZE);
61 cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
62
63 in += BLOCK_SIZE;
64 out += BLOCK_SIZE;
65 }
66 }
67
68/*
69* Lion Key Schedule
70*/
71void Lion::key_schedule(const byte key[], size_t length)
72 {
73 clear();
74
75 key1.copy(key, length / 2);
76 key2.copy(key + length / 2, length / 2);
77 }
78
79/*
80* Return the name of this type
81*/
82std::string Lion::name() const
83 {
84 return "Lion(" + hash->name() + "," +
85 cipher->name() + "," +
86 to_string(BLOCK_SIZE) + ")";
87 }
88
89/*
90* Return a clone of this object
91*/
93 {
94 return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE);
95 }
96
97/*
98* Clear memory of sensitive data
99*/
101 {
102 hash->clear();
103 cipher->clear();
104 zeroise(key1);
105 zeroise(key2);
106 }
107
108/*
109* Lion Constructor
110*/
111Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) :
112 BLOCK_SIZE(std::max<size_t>(2*hash_in->output_length() + 1, block_len)),
113 LEFT_SIZE(hash_in->output_length()),
114 RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE),
115 hash(hash_in),
116 cipher(sc_in)
117 {
118 if(2*LEFT_SIZE + 1 > BLOCK_SIZE)
119 throw Invalid_Argument(name() + ": Chosen block size is too small");
120
121 if(!cipher->valid_keylength(LEFT_SIZE))
122 throw Invalid_Argument(name() + ": This stream/hash combo is invalid");
123
124 key1.resize(LEFT_SIZE);
125 key2.resize(LEFT_SIZE);
126 }
127
128}
Lion(HashFunction *hash, StreamCipher *cipher, size_t block_size)
Definition lion.cpp:111
std::string name() const
Definition lion.cpp:82
void clear()
Definition lion.cpp:100
BlockCipher * clone() const
Definition lion.cpp:92
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition lion.cpp:44
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition lion.cpp:17
void copy(const T in[], size_t n)
Definition secmem.h:126
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428
std::string to_string(u64bit n, size_t min_len)
Definition parsing.cpp:42
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
Definition secmem.h:435