1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* Transformations of data
* (C) 2013 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_TRANSFORM_H__
#define BOTAN_TRANSFORM_H__
#include <botan/secmem.h>
#include <botan/key_spec.h>
#include <botan/exceptn.h>
#include <botan/symkey.h>
#include <string>
#include <vector>
namespace Botan {
/**
* Interface for general transformations on data
*/
class BOTAN_DLL Transformation
{
public:
/**
* Begin processing a message.
* @param nonce the per message nonce
*/
template<typename Alloc>
secure_vector<byte> start_vec(const std::vector<byte, Alloc>& nonce)
{
return start(&nonce[0], nonce.size());
}
/**
* Begin processing a message.
* @param nonce the per message nonce
* @param nonce_len length of nonce
*/
virtual secure_vector<byte> start(const byte nonce[], size_t nonce_len) = 0;
/**
* Process some data. Input must be in size update_granularity() byte blocks.
* @param blocks in/out paramter which will possibly be resized
* @param offset an offset into blocks to begin processing
*/
virtual void update(secure_vector<byte>& blocks, size_t offset = 0) = 0;
/**
* Complete processing of a message.
*
* @param final_block in/out parameter which must be at least
* minimum_final_size() bytes, and will be set to any final output
* @param offset an offset into final_block to begin processing
*/
virtual void finish(secure_vector<byte>& final_block, size_t offset = 0) = 0;
/**
* Returns the size of the output if this transform is used to process a
* message with input_length bytes. Will throw if unable to give a precise
* answer.
*/
virtual size_t output_length(size_t input_length) const = 0;
/**
* @return size of required blocks to update
*/
virtual size_t update_granularity() const = 0;
/**
* @return required minimium size to finalize() - may be any
* length larger than this.
*/
virtual size_t minimum_final_size() const = 0;
/**
* Return the default size for a nonce
*/
virtual size_t default_nonce_length() const = 0;
/**
* Return true iff nonce_len is a valid length for the nonce
*/
virtual bool valid_nonce_length(size_t nonce_len) const = 0;
/**
* Return some short name describing the provider of this tranformation.
* Useful in cases where multiple implementations are available (eg,
* different implementations of AES). Default "core" is used for the
* 'standard' implementation included in the library.
*/
virtual std::string provider() const { return "core"; }
virtual std::string name() const = 0;
virtual void clear() = 0;
virtual ~Transformation() {}
};
class BOTAN_DLL Keyed_Transform : public Transformation
{
public:
/**
* @return object describing limits on key size
*/
virtual Key_Length_Specification key_spec() const = 0;
/**
* Check whether a given key length is valid for this algorithm.
* @param length the key length to be checked.
* @return true if the key length is valid.
*/
bool valid_keylength(size_t length) const
{
return key_spec().valid_keylength(length);
}
template<typename Alloc>
void set_key(const std::vector<byte, Alloc>& key)
{
set_key(&key[0], key.size());
}
void set_key(const SymmetricKey& key)
{
set_key(key.begin(), key.length());
}
/**
* Set the symmetric key of this transform
* @param key contains the key material
* @param length in bytes of key param
*/
void set_key(const byte key[], size_t length)
{
if(!valid_keylength(length))
throw Invalid_Key_Length(name(), length);
key_schedule(key, length);
}
private:
virtual void key_schedule(const byte key[], size_t length) = 0;
};
}
#endif
|