monotone

monotone Mtn Source Tree

Root/botan/buf_es.cpp

1/*************************************************
2* Buffered EntropySource Source File *
3* (C) 1999-2006 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 return copy_out(out, length, buffer.size());
40 }
41
42/*************************************************
43* Default fast poll operation *
44*************************************************/
45void Buffered_EntropySource::do_fast_poll()
46 {
47 return do_slow_poll();
48 }
49
50/*************************************************
51* Add entropy to the internal buffer *
52*************************************************/
53void Buffered_EntropySource::add_bytes(const void* entropy_ptr, u32bit length)
54 {
55 const byte* bytes = (const byte*)entropy_ptr;
56 while(length)
57 {
58 u32bit copied = std::min(length, buffer.size() - write_pos);
59 xor_buf(buffer + write_pos, bytes, copied);
60 bytes += copied;
61 length -= copied;
62 write_pos = (write_pos + copied) % buffer.size();
63 }
64 }
65
66/*************************************************
67* Add entropy to the internal buffer *
68*************************************************/
69void Buffered_EntropySource::add_bytes(u64bit entropy)
70 {
71 add_bytes((const void*)&entropy, 8);
72 }
73
74/*************************************************
75* Add entropy to the internal buffer *
76*************************************************/
77void Buffered_EntropySource::add_timestamp()
78 {
79 add_bytes(system_clock());
80 }
81
82/*************************************************
83* Take entropy from the internal buffer *
84*************************************************/
85u32bit Buffered_EntropySource::copy_out(byte out[], u32bit length,
86 u32bit max_read)
87 {
88 length = std::min(length, max_read);
89 u32bit copied = std::min(length, buffer.size() - read_pos);
90 xor_buf(out, buffer + read_pos, copied);
91 read_pos = (read_pos + copied) % buffer.size();
92 return copied;
93 }
94
95}

Archive Download this file

Branches

Tags

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