📄 es_unix.cpp
字号:
/************************************************** Unix EntropySource Source File ** (C) 1999-2002 The Botan Project **************************************************/#ifndef _XOPEN_SOURCE #define _XOPEN_SOURCE 500#endif#ifndef _XOPEN_SOURCE_EXTENDED #define _XOPEN_SOURCE_EXTENDED 1#endif#include <botan/es_unix.h>#include <botan/util.h>#include <stdio.h>namespace Botan {namespace {bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b) { return (a.priority < b.priority); }}/************************************************** Unix_EntropySource Constructor **************************************************/Unix_EntropySource::Unix_EntropySource(bool add_defaults) { if(add_defaults) add_default_sources(); position = 0; }/************************************************** Unix_EntropySource Constructor **************************************************/Unix_EntropySource::Unix_EntropySource(const Unix_Program srcs[], u32bit count, bool add_defaults) { add_sources(srcs, count); if(add_defaults) add_default_sources(); position = 0; }/************************************************** Add the default sources **************************************************/void Unix_EntropySource::add_default_sources() { u32bit count = 0; while(true) { if(DEFAULT_SOURCES[count].name == "") break; count++; } add_sources(DEFAULT_SOURCES, count); }/************************************************** Fast Poll **************************************************/u32bit Unix_EntropySource::fast_poll(byte out[], u32bit length) { static const u32bit MAX_PRIORITY = 2; return gather(out, length, MAX_PRIORITY); }/************************************************** Slow Poll **************************************************/u32bit Unix_EntropySource::slow_poll(byte out[], u32bit length) { static const u32bit MAX_PRIORITY = 4; return gather(out, length, MAX_PRIORITY); }/************************************************** Gather Entropy From Several Unix_Programs **************************************************/u32bit Unix_EntropySource::gather(byte out[], u32bit length, u32bit prio) { u32bit got = 0; for(u32bit j = 0; j != sources.size(); j++) { if(got >= 2*length || sources[j].priority > prio) break; if(sources[j].working) got += gather_entropy(sources[j]); } u32bit copied = std::min(buffer.size() - position, length); copy_mem(out, buffer + position, copied); position = (position + copied) % buffer.size(); return copied; }/************************************************** Gather Entropy from a Unix_Program **************************************************/u32bit Unix_EntropySource::gather_entropy(Unix_Program& src) { static const u32bit MIN_OUTPUT = 10; std::string command = src.name + " " + src.args + " 2>/dev/null"; SecureVector<byte> output(buffer.size()); u32bit total_output = 0; timestamp(); FILE* pipe = popen(command.c_str(), "r"); if(!pipe) throw Exception("Unix_EntropySource: popen failed"); while(!ferror(pipe) && !feof(pipe)) { u32bit got = fread(output, 1, output.size(), pipe); total_output += got; xor_buf(buffer, output, got); } if(pclose(pipe) == 127*256) src.working = false; if(total_output < MIN_OUTPUT) src.working = false; timestamp(); return total_output; }/************************************************** Add a timestamp value to the buffer **************************************************/void Unix_EntropySource::timestamp() { u64bit time = system_clock(); xor_buf(buffer, (byte*)&time, 8); }/************************************************** Add sources to the list **************************************************/void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count) { sources.insert(sources.end(), srcs, srcs + count); std::sort(sources.begin(), sources.end(), Unix_Program_Cmp); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -