aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/bit_ops.h89
-rw-r--r--src/utils/bswap.h39
-rw-r--r--src/utils/charset.h33
-rw-r--r--src/utils/util.cpp99
-rw-r--r--src/utils/util.h38
-rw-r--r--src/utils/version.cpp28
-rw-r--r--src/utils/version.h33
-rw-r--r--src/utils/xor_buf.h65
8 files changed, 424 insertions, 0 deletions
diff --git a/src/utils/bit_ops.h b/src/utils/bit_ops.h
new file mode 100644
index 000000000..594d130a4
--- /dev/null
+++ b/src/utils/bit_ops.h
@@ -0,0 +1,89 @@
+/*************************************************
+* Bit/Word Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_BIT_OPS_H__
+#define BOTAN_BIT_OPS_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* Return true iff arg is 2**n for some n > 0 *
+* T should be an unsigned integer type *
+*************************************************/
+template<typename T>
+inline bool power_of_2(T arg)
+ {
+ return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0));
+ }
+
+/*************************************************
+* Return the index of the highest set bit
+* T is an unsigned integer type
+*************************************************/
+template<typename T>
+inline u32bit high_bit(T n)
+ {
+ for(u32bit i = 8*sizeof(T); i > 0; --i)
+ if((n >> (i - 1)) & 0x01)
+ return i;
+ return 0;
+ }
+
+/*************************************************
+* Return the index of the lowest set bit *
+*************************************************/
+template<typename T>
+inline u32bit low_bit(T n)
+ {
+ for(u32bit i = 0; i != 8*sizeof(T); ++i)
+ if((n >> i) & 0x01)
+ return (i + 1);
+ return 0;
+ }
+
+/*************************************************
+* Return the number of significant bytes in n *
+*************************************************/
+template<typename T>
+inline u32bit significant_bytes(T n)
+ {
+ for(u32bit j = 0; j != sizeof(T); ++j)
+ if(get_byte(j, n))
+ return sizeof(T)-j;
+ return 0;
+ }
+
+/*************************************************
+* Return the Hamming weight of n *
+*************************************************/
+template<typename T>
+inline u32bit hamming_weight(T n)
+ {
+ const byte NIBBLE_WEIGHTS[] = {
+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
+
+ u32bit weight = 0;
+ for(u32bit i = 0; i != 2*sizeof(T); ++i)
+ weight += NIBBLE_WEIGHTS[(n >> (4*i)) & 0x0F];
+ return weight;
+ }
+
+/*************************************************
+* Count the trailing zero bits in n *
+*************************************************/
+template<typename T>
+inline u32bit ctz(T n)
+ {
+ for(int i = 0; i != 8*sizeof(T); ++i)
+ if((n >> i) & 0x01)
+ return i;
+ return 8*sizeof(T);
+ }
+
+}
+
+#endif
diff --git a/src/utils/bswap.h b/src/utils/bswap.h
new file mode 100644
index 000000000..e38d3c6fa
--- /dev/null
+++ b/src/utils/bswap.h
@@ -0,0 +1,39 @@
+/*************************************************
+* Byte Swapping Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_BSWAP_H__
+#define BOTAN_BSWAP_H__
+
+#include <botan/types.h>
+#include <botan/rotate.h>
+
+namespace Botan {
+
+/*************************************************
+* Byte Swapping Functions *
+*************************************************/
+inline u16bit reverse_bytes(u16bit input)
+ {
+ return rotate_left(input, 8);
+ }
+
+inline u32bit reverse_bytes(u32bit input)
+ {
+ input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
+ return rotate_left(input, 16);
+ }
+
+inline u64bit reverse_bytes(u64bit input)
+ {
+ u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00);
+ u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
+ hi = (hi << 16) | (hi >> 16);
+ lo = (lo << 16) | (lo >> 16);
+ return (static_cast<u64bit>(lo) << 32) | hi;
+ }
+
+}
+
+#endif
diff --git a/src/utils/charset.h b/src/utils/charset.h
new file mode 100644
index 000000000..ac9f501a0
--- /dev/null
+++ b/src/utils/charset.h
@@ -0,0 +1,33 @@
+/*************************************************
+* Character Set Handling Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_CHARSET_H__
+#define BOTAN_CHARSET_H__
+
+#include <botan/types.h>
+#include <botan/enums.h>
+#include <string>
+
+namespace Botan {
+
+namespace Charset {
+
+/*************************************************
+* Character Set Handling *
+*************************************************/
+std::string transcode(const std::string&, Character_Set, Character_Set);
+
+bool is_digit(char);
+bool is_space(char);
+bool caseless_cmp(char, char);
+
+byte char2digit(char);
+char digit2char(byte);
+
+}
+
+}
+
+#endif
diff --git a/src/utils/util.cpp b/src/utils/util.cpp
new file mode 100644
index 000000000..af1c62ebd
--- /dev/null
+++ b/src/utils/util.cpp
@@ -0,0 +1,99 @@
+/*************************************************
+* Utility Functions Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/util.h>
+#include <botan/bit_ops.h>
+#include <algorithm>
+#include <cmath>
+
+namespace Botan {
+
+/*************************************************
+* Round up n to multiple of align_to *
+*************************************************/
+u32bit round_up(u32bit n, u32bit align_to)
+ {
+ if(n % align_to || n == 0)
+ n += align_to - (n % align_to);
+ return n;
+ }
+
+/*************************************************
+* Round down n to multiple of align_to *
+*************************************************/
+u32bit round_down(u32bit n, u32bit align_to)
+ {
+ return (n - (n % align_to));
+ }
+
+/*************************************************
+* Choose the exponent size for a DL group
+*************************************************/
+u32bit dl_work_factor(u32bit bits)
+ {
+#if 0
+ /*
+ These values were taken from RFC 3526
+ */
+ if(bits <= 1536)
+ return 90;
+ else if(bits <= 2048)
+ return 110;
+ else if(bits <= 3072)
+ return 130;
+ else if(bits <= 4096)
+ return 150;
+ else if(bits <= 6144)
+ return 170;
+ else if(bits <= 8192)
+ return 190;
+ return 256;
+#else
+ const u32bit MIN_ESTIMATE = 64;
+
+ const double log_x = bits / 1.44;
+
+ const double strength =
+ 2.76 * std::pow(log_x, 1.0/3.0) * std::pow(std::log(log_x), 2.0/3.0);
+
+ if(strength > MIN_ESTIMATE)
+ return static_cast<u32bit>(strength);
+ return MIN_ESTIMATE;
+#endif
+ }
+
+/*************************************************
+* Estimate the entropy of the buffer *
+*************************************************/
+u32bit entropy_estimate(const byte buffer[], u32bit length)
+ {
+ if(length <= 4)
+ return 0;
+
+ u32bit estimate = 0;
+ byte last = 0, last_delta = 0, last_delta2 = 0;
+
+ for(u32bit j = 0; j != length; ++j)
+ {
+ byte delta = last ^ buffer[j];
+ last = buffer[j];
+
+ byte delta2 = delta ^ last_delta;
+ last_delta = delta;
+
+ byte delta3 = delta2 ^ last_delta2;
+ last_delta2 = delta2;
+
+ byte min_delta = delta;
+ if(min_delta > delta2) min_delta = delta2;
+ if(min_delta > delta3) min_delta = delta3;
+
+ estimate += hamming_weight(min_delta);
+ }
+
+ return (estimate / 2);
+ }
+
+}
diff --git a/src/utils/util.h b/src/utils/util.h
new file mode 100644
index 000000000..879acd1db
--- /dev/null
+++ b/src/utils/util.h
@@ -0,0 +1,38 @@
+/*************************************************
+* Utility Functions Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_UTIL_H__
+#define BOTAN_UTIL_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* Time Access Functions *
+*************************************************/
+BOTAN_DLL u64bit system_time();
+
+/*************************************************
+* Memory Locking Functions *
+*************************************************/
+BOTAN_DLL void lock_mem(void*, u32bit);
+BOTAN_DLL void unlock_mem(void*, u32bit);
+
+/*************************************************
+* Misc Utility Functions *
+*************************************************/
+BOTAN_DLL u32bit round_up(u32bit, u32bit);
+BOTAN_DLL u32bit round_down(u32bit, u32bit);
+
+/*************************************************
+* Work Factor Estimates *
+*************************************************/
+BOTAN_DLL u32bit entropy_estimate(const byte[], u32bit);
+BOTAN_DLL u32bit dl_work_factor(u32bit);
+
+}
+
+#endif
diff --git a/src/utils/version.cpp b/src/utils/version.cpp
new file mode 100644
index 000000000..09deb53d6
--- /dev/null
+++ b/src/utils/version.cpp
@@ -0,0 +1,28 @@
+/*************************************************
+* Version Information Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/version.h>
+#include <botan/parsing.h>
+
+namespace Botan {
+
+/*************************************************
+* Return the version as a string *
+*************************************************/
+std::string version_string()
+ {
+ return "Botan " + to_string(version_major()) + "." +
+ to_string(version_minor()) + "." +
+ to_string(version_patch());
+ }
+
+/*************************************************
+* Return parts of the version as integers *
+*************************************************/
+u32bit version_major() { return BOTAN_VERSION_MAJOR; }
+u32bit version_minor() { return BOTAN_VERSION_MINOR; }
+u32bit version_patch() { return BOTAN_VERSION_PATCH; }
+
+}
diff --git a/src/utils/version.h b/src/utils/version.h
new file mode 100644
index 000000000..6604b1885
--- /dev/null
+++ b/src/utils/version.h
@@ -0,0 +1,33 @@
+/*************************************************
+* Version Information Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_VERSION_H__
+#define BOTAN_VERSION_H__
+
+#include <botan/types.h>
+#include <string>
+
+namespace Botan {
+
+/*************************************************
+* Get information describing the version *
+*************************************************/
+BOTAN_DLL std::string version_string();
+BOTAN_DLL u32bit version_major();
+BOTAN_DLL u32bit version_minor();
+BOTAN_DLL u32bit version_patch();
+
+/*************************************************
+* Macros for compile-time version checks *
+*************************************************/
+#define BOTAN_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c))
+
+#define BOTAN_VERSION_CODE BOTAN_VERSION_CODE_FOR(BOTAN_VERSION_MAJOR, \
+ BOTAN_VERSION_MINOR, \
+ BOTAN_VERSION_PATCH)
+
+}
+
+#endif
diff --git a/src/utils/xor_buf.h b/src/utils/xor_buf.h
new file mode 100644
index 000000000..0a71aef3e
--- /dev/null
+++ b/src/utils/xor_buf.h
@@ -0,0 +1,65 @@
+/*************************************************
+* Xor Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_XOR_BUF_H__
+#define BOTAN_XOR_BUF_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* XOR Arrays *
+*************************************************/
+inline void xor_buf(byte out[], const byte in[], u32bit length)
+ {
+ while(length >= 8)
+ {
+#if BOTAN_UNALIGNED_LOADSTOR_OK
+ *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in);
+#else
+ out[0] ^= in[0]; out[1] ^= in[1];
+ out[2] ^= in[2]; out[3] ^= in[3];
+ out[4] ^= in[4]; out[5] ^= in[5];
+ out[6] ^= in[6]; out[7] ^= in[7];
+#endif
+
+ out += 8; in += 8; length -= 8;
+ }
+ for(u32bit j = 0; j != length; ++j)
+ out[j] ^= in[j];
+ }
+
+/*************************************************
+* XOR Arrays *
+*************************************************/
+inline void xor_buf(byte out[],
+ const byte in[],
+ const byte in2[],
+ u32bit length)
+ {
+ while(length >= 8)
+ {
+#if BOTAN_UNALIGNED_LOADSTOR_OK
+ *reinterpret_cast<u64bit*>(out) =
+ *reinterpret_cast<const u64bit*>(in) ^
+ *reinterpret_cast<const u64bit*>(in2);
+#else
+ out[0] = in[0] ^ in2[0]; out[1] = in[1] ^ in2[1];
+ out[2] = in[2] ^ in2[2]; out[3] = in[3] ^ in2[3];
+ out[4] = in[4] ^ in2[4]; out[5] = in[5] ^ in2[5];
+ out[6] = in[6] ^ in2[6]; out[7] = in[7] ^ in2[7];
+#endif
+
+ in += 8; in2 += 8; out += 8; length -= 8;
+ }
+
+ for(u32bit j = 0; j != length; ++j)
+ out[j] = in[j] ^ in2[j];
+ }
+
+}
+
+#endif