monotone

monotone Mtn Source Tree

Root/botan/x931_rng.cpp

1/*************************************************
2* ANSI X9.31 RNG Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/x931_rng.h>
7#include <botan/lookup.h>
8#include <botan/randpool.h>
9#include <botan/bit_ops.h>
10#include <algorithm>
11
12namespace Botan {
13
14/*************************************************
15* Generate a buffer of random bytes *
16*************************************************/
17void ANSI_X931_RNG::randomize(byte out[], u32bit length) throw(PRNG_Unseeded)
18 {
19 if(!is_seeded())
20 throw PRNG_Unseeded(name());
21
22 while(length)
23 {
24 const u32bit copied = std::min(length, R.size() - position);
25
26 copy_mem(out, R + position, copied);
27 out += copied;
28 length -= copied;
29 position += copied;
30
31 if(position == R.size())
32 {
33 update_buffer();
34 position = 0;
35 }
36 }
37 }
38
39/*************************************************
40* Refill the internal state *
41*************************************************/
42void ANSI_X931_RNG::update_buffer()
43 {
44 const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE;
45
46 SecureVector<byte> DT(BLOCK_SIZE);
47
48 prng->randomize(DT, DT.size());
49 cipher->encrypt(DT);
50
51 xor_buf(R, V, DT, BLOCK_SIZE);
52 cipher->encrypt(R);
53
54 xor_buf(V, R, DT, BLOCK_SIZE);
55 cipher->encrypt(V);
56 }
57
58/*************************************************
59* Add entropy to internal state *
60*************************************************/
61void ANSI_X931_RNG::add_randomness(const byte data[], u32bit length)
62 {
63 prng->add_entropy(data, length);
64
65 if(is_seeded())
66 {
67 SecureVector<byte> key(cipher->MAXIMUM_KEYLENGTH);
68 prng->randomize(key, key.size());
69 cipher->set_key(key, key.size());
70
71 prng->randomize(V, V.size());
72
73 update_buffer();
74 }
75 }
76
77/*************************************************
78* Check if the the PRNG is seeded *
79*************************************************/
80bool ANSI_X931_RNG::is_seeded() const
81 {
82 return prng->is_seeded();
83 }
84
85/*************************************************
86* Clear memory of sensitive data *
87*************************************************/
88void ANSI_X931_RNG::clear() throw()
89 {
90 cipher->clear();
91 prng->clear();
92 R.clear();
93 V.clear();
94
95 position = 0;
96 }
97
98/*************************************************
99* Return the name of this type *
100*************************************************/
101std::string ANSI_X931_RNG::name() const
102 {
103 return "X9.31(" + cipher->name() + ")";
104 }
105
106/*************************************************
107* ANSI X931 RNG Constructor *
108*************************************************/
109ANSI_X931_RNG::ANSI_X931_RNG(const std::string& cipher_name,
110 RandomNumberGenerator* prng_ptr)
111 {
112 if(cipher_name == "")
113 cipher = get_block_cipher("AES-256");
114 else
115 cipher = get_block_cipher(cipher_name);
116
117 const u32bit BLOCK_SIZE = cipher->BLOCK_SIZE;
118
119 V.create(BLOCK_SIZE);
120 R.create(BLOCK_SIZE);
121
122 prng = (prng_ptr ? prng_ptr : new Randpool);
123
124 position = 0;
125 }
126
127/*************************************************
128* ANSI X931 RNG Destructor *
129*************************************************/
130ANSI_X931_RNG::~ANSI_X931_RNG()
131 {
132 delete cipher;
133 delete prng;
134 }
135
136}

Archive Download this file

Branches

Tags

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