monotone

monotone Mtn Source Tree

Root/botan/base.h

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

Archive Download this file

Branches

Tags

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