aboutsummaryrefslogtreecommitdiffstats
path: root/src/pk_pad/emsa1
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-28 19:29:24 +0000
committerlloyd <[email protected]>2008-09-28 19:29:24 +0000
commit9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch)
treefe5e8ae9813b853549558b59833022e87e83981b /src/pk_pad/emsa1
parent9822a701516396b7de4e41339faecd48ff8dc8ff (diff)
Move all modules into src/ directory
Diffstat (limited to 'src/pk_pad/emsa1')
-rw-r--r--src/pk_pad/emsa1/emsa1.cpp112
-rw-r--r--src/pk_pad/emsa1/emsa1.h36
-rw-r--r--src/pk_pad/emsa1/modinfo.txt10
3 files changed, 158 insertions, 0 deletions
diff --git a/src/pk_pad/emsa1/emsa1.cpp b/src/pk_pad/emsa1/emsa1.cpp
new file mode 100644
index 000000000..15d53f6e5
--- /dev/null
+++ b/src/pk_pad/emsa1/emsa1.cpp
@@ -0,0 +1,112 @@
+/*************************************************
+* EMSA1 Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa1.h>
+#include <botan/lookup.h>
+
+namespace Botan {
+
+namespace {
+
+SecureVector<byte> emsa1_encoding(const MemoryRegion<byte>& msg,
+ u32bit output_bits)
+ {
+ if(8*msg.size() <= output_bits)
+ return msg;
+
+ u32bit shift = 8*msg.size() - output_bits;
+
+ u32bit byte_shift = shift / 8, bit_shift = shift % 8;
+ SecureVector<byte> digest(msg.size() - byte_shift);
+
+ for(u32bit j = 0; j != msg.size() - byte_shift; ++j)
+ digest[j] = msg[j];
+
+ if(bit_shift)
+ {
+ byte carry = 0;
+ for(u32bit j = 0; j != digest.size(); ++j)
+ {
+ byte temp = digest[j];
+ digest[j] = (temp >> bit_shift) | carry;
+ carry = (temp << (8 - bit_shift));
+ }
+ }
+ return digest;
+ }
+
+}
+
+/*************************************************
+* EMSA1 Update Operation *
+*************************************************/
+void EMSA1::update(const byte input[], u32bit length)
+ {
+ hash->update(input, length);
+ }
+
+/*************************************************
+* Return the raw (unencoded) data *
+*************************************************/
+SecureVector<byte> EMSA1::raw_data()
+ {
+ return hash->final();
+ }
+
+/*************************************************
+* EMSA1 Encode Operation *
+*************************************************/
+SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ RandomNumberGenerator&)
+ {
+ if(msg.size() != hash->OUTPUT_LENGTH)
+ throw Encoding_Error("EMSA1::encoding_of: Invalid size for input");
+ return emsa1_encoding(msg, output_bits);
+ }
+
+/*************************************************
+* EMSA1 Decode/Verify Operation *
+*************************************************/
+bool EMSA1::verify(const MemoryRegion<byte>& coded,
+ const MemoryRegion<byte>& raw, u32bit key_bits) throw()
+ {
+ try {
+ if(raw.size() != hash->OUTPUT_LENGTH)
+ throw Encoding_Error("EMSA1::encoding_of: Invalid size for input");
+
+ SecureVector<byte> our_coding = emsa1_encoding(raw, key_bits);
+
+ if(our_coding == coded) return true;
+ if(our_coding[0] != 0) return false;
+ if(our_coding.size() <= coded.size()) return false;
+
+ u32bit offset = 0;
+ while(our_coding[offset] == 0 && offset < our_coding.size())
+ ++offset;
+ if(our_coding.size() - offset != coded.size())
+ return false;
+
+ for(u32bit j = 0; j != coded.size(); ++j)
+ if(coded[j] != our_coding[j+offset])
+ return false;
+
+ return true;
+ }
+ catch(Invalid_Argument)
+ {
+ return false;
+ }
+ }
+
+/*************************************************
+* EMSA1 Constructor *
+*************************************************/
+EMSA1::EMSA1(const std::string& hash_name) :
+ hash(get_hash(hash_name))
+ {
+ }
+
+}
diff --git a/src/pk_pad/emsa1/emsa1.h b/src/pk_pad/emsa1/emsa1.h
new file mode 100644
index 000000000..f8d7ac043
--- /dev/null
+++ b/src/pk_pad/emsa1/emsa1.h
@@ -0,0 +1,36 @@
+/*************************************************
+* EMSA1 Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_EMSA1_H__
+#define BOTAN_EMSA1_H__
+
+#include <botan/pk_util.h>
+
+namespace Botan {
+
+/*************************************************
+* EMSA1 *
+*************************************************/
+class BOTAN_DLL EMSA1 : public EMSA
+ {
+ public:
+ EMSA1(const std::string&);
+ ~EMSA1() { delete hash; }
+ private:
+ void update(const byte[], u32bit);
+ SecureVector<byte> raw_data();
+
+ SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
+ RandomNumberGenerator& rng);
+
+ bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
+ u32bit) throw();
+
+ HashFunction* hash;
+ };
+
+}
+
+#endif
diff --git a/src/pk_pad/emsa1/modinfo.txt b/src/pk_pad/emsa1/modinfo.txt
new file mode 100644
index 000000000..4040dd580
--- /dev/null
+++ b/src/pk_pad/emsa1/modinfo.txt
@@ -0,0 +1,10 @@
+realname "EMSA1"
+
+define EMSA1
+
+load_on auto
+
+<add>
+emsa1.h
+emsa1.cpp
+</add>