monotone

monotone Mtn Source Tree

Root/botan/base.h

1/*************************************************
2* Base Classes Header File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_BASE_H__
7#define BOTAN_BASE_H__
8
9#include <botan/exceptn.h>
10#include <botan/symkey.h>
11
12namespace Botan {
13
14/*************************************************
15* Constants *
16*************************************************/
17static const u32bit DEFAULT_BUFFERSIZE = BOTAN_DEFAULT_BUFFER_SIZE;
18
19/*************************************************
20* Symmetric Algorithm *
21*************************************************/
22class SymmetricAlgorithm
23 {
24 public:
25 const u32bit MAXIMUM_KEYLENGTH, MINIMUM_KEYLENGTH, KEYLENGTH_MULTIPLE;
26
27 virtual std::string name() const = 0;
28
29 void set_key(const SymmetricKey&) throw(Invalid_Key_Length);
30 void set_key(const byte[], u32bit) throw(Invalid_Key_Length);
31 bool valid_keylength(u32bit) const;
32 SymmetricAlgorithm(u32bit, u32bit, u32bit);
33 virtual ~SymmetricAlgorithm() {}
34 private:
35 virtual void key(const byte[], u32bit) = 0;
36 };
37
38/*************************************************
39* Block Cipher *
40*************************************************/
41class BlockCipher : public SymmetricAlgorithm
42 {
43 public:
44 const u32bit BLOCK_SIZE;
45
46 void encrypt(const byte in[], byte out[]) const { enc(in, out); }
47 void decrypt(const byte in[], byte out[]) const { dec(in, out); }
48 void encrypt(byte block[]) const { enc(block, block); }
49 void decrypt(byte block[]) const { dec(block, block); }
50
51 virtual BlockCipher* clone() const = 0;
52 virtual void clear() throw() {};
53
54 BlockCipher(u32bit, u32bit, u32bit = 0, u32bit = 1);
55 virtual ~BlockCipher() {}
56 private:
57 virtual void enc(const byte[], byte[]) const = 0;
58 virtual void dec(const byte[], byte[]) const = 0;
59 };
60
61/*************************************************
62* Stream Cipher *
63*************************************************/
64class StreamCipher : public SymmetricAlgorithm
65 {
66 public:
67 const u32bit IV_LENGTH;
68 void encrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
69 void decrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
70 void encrypt(byte in[], u32bit len) { cipher(in, in, len); }
71 void decrypt(byte in[], u32bit len) { cipher(in, in, len); }
72
73 virtual void resync(const byte[], u32bit);
74 virtual void seek(u32bit);
75
76 virtual StreamCipher* clone() const = 0;
77 virtual void clear() throw() {};
78
79 StreamCipher(u32bit, u32bit = 0, u32bit = 1, u32bit = 0);
80 virtual ~StreamCipher() {}
81 private:
82 virtual void cipher(const byte[], byte[], u32bit) = 0;
83 };
84
85/*************************************************
86* Buffered Computation *
87*************************************************/
88class BufferedComputation
89 {
90 public:
91 const u32bit OUTPUT_LENGTH;
92 void update(const byte[], u32bit);
93 void update(const MemoryRegion<byte>&);
94 void update(const std::string&);
95 void update(byte);
96 void final(byte out[]) { final_result(out); }
97 SecureVector<byte> final();
98 SecureVector<byte> process(const byte[], u32bit);
99 SecureVector<byte> process(const MemoryRegion<byte>&);
100 SecureVector<byte> process(const std::string&);
101 BufferedComputation(u32bit);
102 virtual ~BufferedComputation() {}
103 private:
104 virtual void add_data(const byte[], u32bit) = 0;
105 virtual void final_result(byte[]) = 0;
106 };
107
108/*************************************************
109* Hash Function *
110*************************************************/
111class HashFunction : public BufferedComputation
112 {
113 public:
114 const u32bit HASH_BLOCK_SIZE;
115
116 virtual HashFunction* clone() const = 0;
117 virtual std::string name() const = 0;
118 virtual void clear() throw() {};
119
120 HashFunction(u32bit, u32bit = 0);
121 virtual ~HashFunction() {}
122 };
123
124/*************************************************
125* Message Authentication Code *
126*************************************************/
127class MessageAuthenticationCode : public BufferedComputation,
128 public SymmetricAlgorithm
129 {
130 public:
131 virtual bool verify_mac(const byte[], u32bit);
132
133 virtual MessageAuthenticationCode* clone() const = 0;
134 virtual std::string name() const = 0;
135 virtual void clear() throw() {};
136
137 MessageAuthenticationCode(u32bit, u32bit, u32bit = 0, u32bit = 1);
138 virtual ~MessageAuthenticationCode() {}
139 };
140
141/*************************************************
142* Entropy Source *
143*************************************************/
144class EntropySource
145 {
146 public:
147 virtual u32bit slow_poll(byte[], u32bit) = 0;
148 virtual u32bit fast_poll(byte[], u32bit);
149 virtual ~EntropySource() {}
150 };
151
152/*************************************************
153* Random Number Generator *
154*************************************************/
155class RandomNumberGenerator
156 {
157 public:
158 virtual void randomize(byte[], u32bit) throw(PRNG_Unseeded) = 0;
159 virtual bool is_seeded() const { return true; }
160 virtual void clear() throw() {};
161
162 void add_entropy(const byte[], u32bit);
163 u32bit add_entropy(EntropySource&, bool = true);
164
165 virtual ~RandomNumberGenerator() {}
166 private:
167 virtual void add_randomness(const byte[], u32bit) = 0;
168 };
169
170}
171
172#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status