aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-28 17:35:11 +0000
committerlloyd <[email protected]>2008-09-28 17:35:11 +0000
commit8c085b3c40c30607ca5cac10d04062355fcad187 (patch)
tree4fe7387db56f4422c997b88f2a92e52f669c2de4 /modules
parentda412e1f1169d4b51bbef84eb2948b73416bd48a (diff)
Modularize EMSA
Diffstat (limited to 'modules')
-rw-r--r--modules/pk_pad/emsa1/emsa1.cpp112
-rw-r--r--modules/pk_pad/emsa2/emsa2.cpp106
-rw-r--r--modules/pk_pad/emsa3/emsa3.cpp97
-rw-r--r--modules/pk_pad/emsa4/emsa4.cpp144
-rw-r--r--modules/pk_pad/emsa_raw/emsa_raw.cpp48
5 files changed, 507 insertions, 0 deletions
diff --git a/modules/pk_pad/emsa1/emsa1.cpp b/modules/pk_pad/emsa1/emsa1.cpp
new file mode 100644
index 000000000..8977356e8
--- /dev/null
+++ b/modules/pk_pad/emsa1/emsa1.cpp
@@ -0,0 +1,112 @@
+/*************************************************
+* EMSA1 Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa.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/modules/pk_pad/emsa2/emsa2.cpp b/modules/pk_pad/emsa2/emsa2.cpp
new file mode 100644
index 000000000..825329e7e
--- /dev/null
+++ b/modules/pk_pad/emsa2/emsa2.cpp
@@ -0,0 +1,106 @@
+/*************************************************
+* EMSA2 Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa.h>
+#include <botan/hash_id.h>
+#include <botan/lookup.h>
+
+namespace Botan {
+
+namespace {
+
+/*************************************************
+* EMSA2 Encode Operation *
+*************************************************/
+SecureVector<byte> emsa2_encoding(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ const MemoryRegion<byte>& empty_hash,
+ byte hash_id)
+ {
+ const u32bit HASH_SIZE = empty_hash.size();
+
+ u32bit 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(u32bit j = 0; j != HASH_SIZE; ++j)
+ if(empty_hash[j] != msg[j])
+ empty = false;
+
+ SecureVector<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);
+ output.copy(output_length - (HASH_SIZE + 2), msg, msg.size());
+ output[output_length-2] = hash_id;
+ output[output_length-1] = 0xCC;
+
+ return output;
+ }
+
+}
+
+/*************************************************
+* EMSA2 Update Operation *
+*************************************************/
+void EMSA2::update(const byte input[], u32bit length)
+ {
+ hash->update(input, length);
+ }
+
+/*************************************************
+* Return the raw (unencoded) data *
+*************************************************/
+SecureVector<byte> EMSA2::raw_data()
+ {
+ return hash->final();
+ }
+
+/*************************************************
+* EMSA2 Encode Operation *
+*************************************************/
+SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ RandomNumberGenerator&)
+ {
+ return emsa2_encoding(msg, output_bits, empty_hash, hash_id);
+ }
+
+/*************************************************
+* EMSA2 Verify Operation *
+*************************************************/
+bool EMSA2::verify(const MemoryRegion<byte>& coded,
+ const MemoryRegion<byte>& raw,
+ u32bit key_bits) throw()
+ {
+ try
+ {
+ return (coded == emsa2_encoding(raw, key_bits,
+ empty_hash, hash_id));
+ }
+ catch(...)
+ {
+ return false;
+ }
+ }
+
+/*************************************************
+* EMSA2 Constructor *
+*************************************************/
+EMSA2::EMSA2(const std::string& hash_name)
+ {
+ hash_id = ieee1363_hash_id(hash_name);
+ if(hash_id == 0)
+ throw Encoding_Error("EMSA2 cannot be used with " + hash->name());
+ hash = get_hash(hash_name);
+ empty_hash = hash->final();
+ }
+
+}
diff --git a/modules/pk_pad/emsa3/emsa3.cpp b/modules/pk_pad/emsa3/emsa3.cpp
new file mode 100644
index 000000000..35a9f6fe3
--- /dev/null
+++ b/modules/pk_pad/emsa3/emsa3.cpp
@@ -0,0 +1,97 @@
+/*************************************************
+* EMSA3 Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa.h>
+#include <botan/hash_id.h>
+#include <botan/lookup.h>
+
+namespace Botan {
+
+namespace {
+
+/*************************************************
+* EMSA3 Encode Operation *
+*************************************************/
+SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ const MemoryRegion<byte>& hash_id,
+ u32bit hash_size)
+ {
+ if(msg.size() != hash_size)
+ throw Encoding_Error("EMSA3::encoding_of: Bad input length");
+
+ u32bit output_length = output_bits / 8;
+ if(output_length < hash_id.size() + hash_size + 10)
+ throw Encoding_Error("EMSA3::pad: Output length is too small");
+
+ SecureVector<byte> T(output_length);
+ const u32bit P_LENGTH = output_length - hash_size - hash_id.size() - 2;
+
+ T[0] = 0x01;
+ set_mem(T+1, P_LENGTH, 0xFF);
+ T[P_LENGTH+1] = 0x00;
+ T.copy(P_LENGTH+2, hash_id, hash_id.size());
+ T.copy(output_length-hash_size, msg, msg.size());
+ return T;
+ }
+
+}
+
+/*************************************************
+* EMSA3 Update Operation *
+*************************************************/
+void EMSA3::update(const byte input[], u32bit length)
+ {
+ hash->update(input, length);
+ }
+
+/*************************************************
+* Return the raw (unencoded) data *
+*************************************************/
+SecureVector<byte> EMSA3::raw_data()
+ {
+ return hash->final();
+ }
+
+/*************************************************
+* EMSA3 Encode Operation *
+*************************************************/
+SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ RandomNumberGenerator&)
+ {
+ return emsa3_encoding(msg, output_bits, hash_id,
+ hash->OUTPUT_LENGTH);
+ }
+
+/*************************************************
+* Default signature decoding *
+*************************************************/
+bool EMSA3::verify(const MemoryRegion<byte>& coded,
+ const MemoryRegion<byte>& raw,
+ u32bit key_bits) throw()
+ {
+ try
+ {
+ return (coded == emsa3_encoding(raw, key_bits,
+ hash_id,
+ hash->OUTPUT_LENGTH));
+ }
+ catch(...)
+ {
+ return false;
+ }
+ }
+
+/*************************************************
+* EMSA3 Constructor *
+*************************************************/
+EMSA3::EMSA3(const std::string& hash_name)
+ {
+ hash_id = pkcs_hash_id(hash_name);
+ hash = get_hash(hash_name);
+ }
+
+}
diff --git a/modules/pk_pad/emsa4/emsa4.cpp b/modules/pk_pad/emsa4/emsa4.cpp
new file mode 100644
index 000000000..6fb63fe0a
--- /dev/null
+++ b/modules/pk_pad/emsa4/emsa4.cpp
@@ -0,0 +1,144 @@
+/*************************************************
+* EMSA4 Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa.h>
+#include <botan/lookup.h>
+#include <botan/look_pk.h>
+#include <botan/bit_ops.h>
+
+namespace Botan {
+
+/*************************************************
+* EMSA4 Update Operation *
+*************************************************/
+void EMSA4::update(const byte input[], u32bit length)
+ {
+ hash->update(input, length);
+ }
+
+/*************************************************
+* Return the raw (unencoded) data *
+*************************************************/
+SecureVector<byte> EMSA4::raw_data()
+ {
+ return hash->final();
+ }
+
+/*************************************************
+* EMSA4 Encode Operation *
+*************************************************/
+SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg,
+ u32bit output_bits,
+ RandomNumberGenerator& rng)
+ {
+ const u32bit HASH_SIZE = hash->OUTPUT_LENGTH;
+
+ if(msg.size() != HASH_SIZE)
+ throw Encoding_Error("EMSA4::encoding_of: Bad input length");
+ if(output_bits < 8*HASH_SIZE + 8*SALT_SIZE + 9)
+ throw Encoding_Error("EMSA4::encoding_of: Output length is too small");
+
+ const u32bit output_length = (output_bits + 7) / 8;
+
+ SecureVector<byte> salt(SALT_SIZE);
+ rng.randomize(salt, SALT_SIZE);
+
+ for(u32bit j = 0; j != 8; ++j)
+ hash->update(0);
+ hash->update(msg);
+ hash->update(salt, SALT_SIZE);
+ SecureVector<byte> H = hash->final();
+
+ SecureVector<byte> EM(output_length);
+
+ EM[output_length - HASH_SIZE - SALT_SIZE - 2] = 0x01;
+ EM.copy(output_length - 1 - HASH_SIZE - SALT_SIZE, salt, SALT_SIZE);
+ mgf->mask(H, HASH_SIZE, EM, output_length - HASH_SIZE - 1);
+ EM[0] &= 0xFF >> (8 * ((output_bits + 7) / 8) - output_bits);
+ EM.copy(output_length - 1 - HASH_SIZE, H, HASH_SIZE);
+ EM[output_length-1] = 0xBC;
+
+ return EM;
+ }
+
+/*************************************************
+* EMSA4 Decode/Verify Operation *
+*************************************************/
+bool EMSA4::verify(const MemoryRegion<byte>& const_coded,
+ const MemoryRegion<byte>& raw, u32bit key_bits) throw()
+ {
+ const u32bit HASH_SIZE = hash->OUTPUT_LENGTH;
+ const u32bit KEY_BYTES = (key_bits + 7) / 8;
+
+ if(key_bits < 8*HASH_SIZE + 9)
+ return false;
+ if(raw.size() != HASH_SIZE)
+ return false;
+ if(const_coded.size() > KEY_BYTES)
+ return false;
+ if(const_coded[const_coded.size()-1] != 0xBC)
+ return false;
+
+ SecureVector<byte> coded = const_coded;
+ if(coded.size() < KEY_BYTES)
+ {
+ SecureVector<byte> temp(KEY_BYTES);
+ temp.copy(KEY_BYTES - coded.size(), coded, coded.size());
+ coded = temp;
+ }
+
+ const u32bit TOP_BITS = 8 * ((key_bits + 7) / 8) - key_bits;
+ if(TOP_BITS > 8 - high_bit(coded[0]))
+ return false;
+
+ SecureVector<byte> DB(coded.begin(), coded.size() - HASH_SIZE - 1);
+ SecureVector<byte> H(coded + coded.size() - HASH_SIZE - 1, HASH_SIZE);
+
+ mgf->mask(H, H.size(), DB, coded.size() - H.size() - 1);
+ DB[0] &= 0xFF >> TOP_BITS;
+
+ u32bit salt_offset = 0;
+ for(u32bit j = 0; j != DB.size(); ++j)
+ {
+ if(DB[j] == 0x01)
+ { salt_offset = j + 1; break; }
+ if(DB[j])
+ return false;
+ }
+ if(salt_offset == 0)
+ return false;
+
+ SecureVector<byte> salt(DB + salt_offset, DB.size() - salt_offset);
+
+ for(u32bit j = 0; j != 8; ++j)
+ hash->update(0);
+ hash->update(raw);
+ hash->update(salt);
+ SecureVector<byte> H2 = hash->final();
+
+ return (H == H2);
+ }
+
+/*************************************************
+* EMSA4 Constructor *
+*************************************************/
+EMSA4::EMSA4(const std::string& hash_name, const std::string& mgf_name) :
+ SALT_SIZE(output_length_of(hash_name))
+ {
+ hash = get_hash(hash_name);
+ mgf = get_mgf(mgf_name + "(" + hash_name + ")");
+ }
+
+/*************************************************
+* EMSA4 Constructor *
+*************************************************/
+EMSA4::EMSA4(const std::string& hash_name, const std::string& mgf_name,
+ u32bit salt_size) : SALT_SIZE(salt_size)
+ {
+ hash = get_hash(hash_name);
+ mgf = get_mgf(mgf_name + "(" + hash_name + ")");
+ }
+
+}
diff --git a/modules/pk_pad/emsa_raw/emsa_raw.cpp b/modules/pk_pad/emsa_raw/emsa_raw.cpp
new file mode 100644
index 000000000..037e22375
--- /dev/null
+++ b/modules/pk_pad/emsa_raw/emsa_raw.cpp
@@ -0,0 +1,48 @@
+/*************************************************
+* EMSA-Raw Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/emsa.h>
+
+namespace Botan {
+
+/*************************************************
+* EMSA-Raw Encode Operation *
+*************************************************/
+void EMSA_Raw::update(const byte input[], u32bit length)
+ {
+ message.append(input, length);
+ }
+
+/*************************************************
+* Return the raw (unencoded) data *
+*************************************************/
+SecureVector<byte> EMSA_Raw::raw_data()
+ {
+ SecureVector<byte> buf = message;
+ message.destroy();
+ return buf;
+ }
+
+/*************************************************
+* EMSA-Raw Encode Operation *
+*************************************************/
+SecureVector<byte> EMSA_Raw::encoding_of(const MemoryRegion<byte>& msg,
+ u32bit,
+ RandomNumberGenerator&)
+ {
+ return msg;
+ }
+
+/*************************************************
+* EMSA-Raw Verify Operation *
+*************************************************/
+bool EMSA_Raw::verify(const MemoryRegion<byte>& coded,
+ const MemoryRegion<byte>& raw,
+ u32bit) throw()
+ {
+ return (coded == raw);
+ }
+
+}