monotone

monotone Mtn Source Tree

Root/botan/buf_es.cpp

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

Archive Download this file

Branches

Tags

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