aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pk_pad/emsa2
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-01 21:20:55 +0000
committerlloyd <[email protected]>2014-01-01 21:20:55 +0000
commit197dc467dec28a04c3b2f30da7cef122dfbb13e9 (patch)
treecdbd3ddaec051c72f0a757db461973d90c37b97a /lib/pk_pad/emsa2
parent62faac373c07cfe10bc8c309e89ebdd30d8e5eaa (diff)
Shuffle things around. Add NIST X.509 test to build.
Diffstat (limited to 'lib/pk_pad/emsa2')
-rw-r--r--lib/pk_pad/emsa2/emsa2.cpp112
-rw-r--r--lib/pk_pad/emsa2/emsa2.h45
-rw-r--r--lib/pk_pad/emsa2/info.txt6
3 files changed, 163 insertions, 0 deletions
diff --git a/lib/pk_pad/emsa2/emsa2.cpp b/lib/pk_pad/emsa2/emsa2.cpp
new file mode 100644
index 000000000..02a3dbe72
--- /dev/null
+++ b/lib/pk_pad/emsa2/emsa2.cpp
@@ -0,0 +1,112 @@
+/*
+* EMSA2
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/emsa2.h>
+#include <botan/hash_id.h>
+
+namespace Botan {
+
+namespace {
+
+/*
+* EMSA2 Encode Operation
+*/
+secure_vector<byte> emsa2_encoding(const secure_vector<byte>& msg,
+ size_t output_bits,
+ const secure_vector<byte>& empty_hash,
+ byte hash_id)
+ {
+ const size_t HASH_SIZE = empty_hash.size();
+
+ size_t output_length = (output_bits + 1) / 8;
+
+ if(msg.size() != HASH_SIZE)
+ throw Encoding_Error("EMSA2::encoding_of: Bad input length");
+ if(output_length < HASH_SIZE + 4)
+ throw Encoding_Error("EMSA2::encoding_of: Output length is too small");
+
+ bool empty = true;
+ for(size_t j = 0; j != HASH_SIZE; ++j)
+ if(empty_hash[j] != msg[j])
+ empty = false;
+
+ secure_vector<byte> output(output_length);
+
+ output[0] = (empty ? 0x4B : 0x6B);
+ output[output_length - 3 - HASH_SIZE] = 0xBA;
+ set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB);
+ buffer_insert(output, output_length - (HASH_SIZE + 2), &msg[0], msg.size());
+ output[output_length-2] = hash_id;
+ output[output_length-1] = 0xCC;
+
+ return output;
+ }
+
+}
+
+/*
+* EMSA2 Update Operation
+*/
+void EMSA2::update(const byte input[], size_t length)
+ {
+ hash->update(input, length);
+ }
+
+/*
+* Return the raw (unencoded) data
+*/
+secure_vector<byte> EMSA2::raw_data()
+ {
+ return hash->final();
+ }
+
+/*
+* EMSA2 Encode Operation
+*/
+secure_vector<byte> EMSA2::encoding_of(const secure_vector<byte>& msg,
+ size_t output_bits,
+ RandomNumberGenerator&)
+ {
+ return emsa2_encoding(msg, output_bits, empty_hash, hash_id);
+ }
+
+/*
+* EMSA2 Verify Operation
+*/
+bool EMSA2::verify(const secure_vector<byte>& coded,
+ const secure_vector<byte>& raw,
+ size_t key_bits)
+ {
+ try
+ {
+ return (coded == emsa2_encoding(raw, key_bits,
+ empty_hash, hash_id));
+ }
+ catch(...)
+ {
+ return false;
+ }
+ }
+
+/*
+* EMSA2 Constructor
+*/
+EMSA2::EMSA2(HashFunction* hash_in) : hash(hash_in)
+ {
+ empty_hash = hash->final();
+
+ const std::string hash_name = hash->name();
+ hash_id = ieee1363_hash_id(hash_name);
+
+ if(hash_id == 0)
+ {
+ delete hash;
+ throw Encoding_Error("EMSA2 no hash identifier for " + hash_name);
+ }
+ }
+
+}
diff --git a/lib/pk_pad/emsa2/emsa2.h b/lib/pk_pad/emsa2/emsa2.h
new file mode 100644
index 000000000..fb0cecb21
--- /dev/null
+++ b/lib/pk_pad/emsa2/emsa2.h
@@ -0,0 +1,45 @@
+/*
+* EMSA2
+* (C) 1999-2007 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_EMSA2_H__
+#define BOTAN_EMSA2_H__
+
+#include <botan/emsa.h>
+#include <botan/hash.h>
+
+namespace Botan {
+
+/**
+* EMSA2 from IEEE 1363
+* Useful for Rabin-Williams
+*/
+class BOTAN_DLL EMSA2 : public EMSA
+ {
+ public:
+ /**
+ * @param hash the hash object to use
+ */
+ EMSA2(HashFunction* hash);
+ ~EMSA2() { delete hash; }
+ private:
+ void update(const byte[], size_t);
+ secure_vector<byte> raw_data();
+
+ secure_vector<byte> encoding_of(const secure_vector<byte>&, size_t,
+ RandomNumberGenerator& rng);
+
+ bool verify(const secure_vector<byte>&, const secure_vector<byte>&,
+ size_t);
+
+ secure_vector<byte> empty_hash;
+ HashFunction* hash;
+ byte hash_id;
+ };
+
+}
+
+#endif
diff --git a/lib/pk_pad/emsa2/info.txt b/lib/pk_pad/emsa2/info.txt
new file mode 100644
index 000000000..0c9bd2289
--- /dev/null
+++ b/lib/pk_pad/emsa2/info.txt
@@ -0,0 +1,6 @@
+define EMSA2 20131128
+
+<requires>
+hash
+hash_id
+</requires>