monotone

monotone Mtn Source Tree

Root/botan/buf_es.cpp

1/*************************************************
2* Buffered EntropySource Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/buf_es.h>
7#include <botan/bit_ops.h>
8#include <botan/util.h>
9
10namespace Botan {
11
12/*************************************************
13* Buffered_EntropySource Constructor *
14*************************************************/
15Buffered_EntropySource::Buffered_EntropySource() : buffer(256)
16 {
17 read_pos = write_pos = 0;
18 done_slow_poll = false;
19 }
20
21/*************************************************
22* Fast Poll *
23*************************************************/
24u32bit Buffered_EntropySource::fast_poll(byte out[], u32bit length)
25 {
26 if(!done_slow_poll) { do_slow_poll(); done_slow_poll = true; }
27
28 do_fast_poll();
29 return copy_out(out, length, buffer.size() / 4);
30 }
31
32/*************************************************
33* Slow Poll *
34*************************************************/
35u32bit Buffered_EntropySource::slow_poll(byte out[], u32bit length)
36 {
37 do_slow_poll();
38 return copy_out(out, length, buffer.size());
39 }
40
41/*************************************************
42* Default fast poll operation *
43*************************************************/
44void Buffered_EntropySource::do_fast_poll()
45 {
46 return do_slow_poll();
47 }
48
49/*************************************************
50* Add entropy to the internal buffer *
51*************************************************/
52void Buffered_EntropySource::add_bytes(const void* entropy_ptr, u32bit length)
53 {
54 const byte* bytes = (const byte*)entropy_ptr;
55 while(length)
56 {
57 u32bit copied = std::min(length, buffer.size() - write_pos);
58 xor_buf(buffer + write_pos, bytes, copied);
59 bytes += copied;
60 length -= copied;
61 write_pos = (write_pos + copied) % buffer.size();
62 }
63 }
64
65/*************************************************
66* Add entropy to the internal buffer *
67*************************************************/
68void Buffered_EntropySource::add_bytes(u64bit entropy)
69 {
70 add_bytes((const void*)&entropy, 8);
71 }
72
73/*************************************************
74* Add entropy to the internal buffer *
75*************************************************/
76void Buffered_EntropySource::add_timestamp()
77 {
78 add_bytes(system_clock());
79 }
80
81/*************************************************
82* Take entropy from the internal buffer *
83*************************************************/
84u32bit Buffered_EntropySource::copy_out(byte out[], u32bit length,
85 u32bit max_read)
86 {
87 length = std::min(length, max_read);
88 u32bit copied = std::min(length, buffer.size() - read_pos);
89 xor_buf(out, buffer + read_pos, copied);
90 read_pos = (read_pos + copied) % buffer.size();
91 return copied;
92 }
93
94}

Archive Download this file

Branches

Tags

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