monotone

monotone Mtn Source Tree

Root/botan/x931_rng.cpp

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

Archive Download this file

Branches

Tags

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