aboutsummaryrefslogtreecommitdiffstats
path: root/src/kdf/pgps2k
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/kdf/pgps2k
parent9822a701516396b7de4e41339faecd48ff8dc8ff (diff)
Move all modules into src/ directory
Diffstat (limited to 'src/kdf/pgps2k')
-rw-r--r--src/kdf/pgps2k/modinfo.txt10
-rw-r--r--src/kdf/pgps2k/pgp_s2k.cpp82
-rw-r--r--src/kdf/pgps2k/pgp_s2k.h30
3 files changed, 122 insertions, 0 deletions
diff --git a/src/kdf/pgps2k/modinfo.txt b/src/kdf/pgps2k/modinfo.txt
new file mode 100644
index 000000000..a3d5a146f
--- /dev/null
+++ b/src/kdf/pgps2k/modinfo.txt
@@ -0,0 +1,10 @@
+realname "Pgps2k"
+
+define PGPS2K
+
+load_on auto
+
+<add>
+pgp_s2k.cpp
+pgp_s2k.h
+</add>
diff --git a/src/kdf/pgps2k/pgp_s2k.cpp b/src/kdf/pgps2k/pgp_s2k.cpp
new file mode 100644
index 000000000..66a243e45
--- /dev/null
+++ b/src/kdf/pgps2k/pgp_s2k.cpp
@@ -0,0 +1,82 @@
+/*************************************************
+* OpenPGP S2K Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/pgp_s2k.h>
+#include <botan/lookup.h>
+#include <algorithm>
+#include <memory>
+
+namespace Botan {
+
+/*************************************************
+* Derive a key using the OpenPGP S2K algorithm *
+*************************************************/
+OctetString OpenPGP_S2K::derive(u32bit key_len, const std::string& passphrase,
+ const byte salt_buf[], u32bit salt_size,
+ u32bit iterations) const
+ {
+ SecureVector<byte> key(key_len), hash_buf;
+
+ u32bit pass = 0, generated = 0,
+ total_size = passphrase.size() + salt_size;
+ u32bit to_hash = std::max(iterations, total_size);
+
+ std::auto_ptr<HashFunction> hash(get_hash(hash_name));
+
+ hash->clear();
+ while(key_len > generated)
+ {
+ for(u32bit j = 0; j != pass; ++j)
+ hash->update(0);
+
+ u32bit left = to_hash;
+ while(left >= total_size)
+ {
+ hash->update(salt_buf, salt_size);
+ hash->update(passphrase);
+ left -= total_size;
+ }
+ if(left <= salt_size)
+ hash->update(salt_buf, left);
+ else
+ {
+ hash->update(salt_buf, salt_size);
+ left -= salt_size;
+ hash->update(reinterpret_cast<const byte*>(passphrase.data()), left);
+ }
+
+ hash_buf = hash->final();
+ key.copy(generated, hash_buf, hash->OUTPUT_LENGTH);
+ generated += hash->OUTPUT_LENGTH;
+ ++pass;
+ }
+
+ return key;
+ }
+
+/*************************************************
+* Return the name of this type *
+*************************************************/
+std::string OpenPGP_S2K::name() const
+ {
+ return "OpenPGP-S2K(" + hash_name + ")";
+ }
+
+/*************************************************
+* Return a clone of this object *
+*************************************************/
+S2K* OpenPGP_S2K::clone() const
+ {
+ return new OpenPGP_S2K(hash_name);
+ }
+
+/*************************************************
+* OpenPGP S2K Constructor *
+*************************************************/
+OpenPGP_S2K::OpenPGP_S2K(const std::string& h) : hash_name(h)
+ {
+ }
+
+}
diff --git a/src/kdf/pgps2k/pgp_s2k.h b/src/kdf/pgps2k/pgp_s2k.h
new file mode 100644
index 000000000..cd263a735
--- /dev/null
+++ b/src/kdf/pgps2k/pgp_s2k.h
@@ -0,0 +1,30 @@
+/*************************************************
+* OpenPGP S2K Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_OPENPGP_S2K_H__
+#define BOTAN_OPENPGP_S2K_H__
+
+#include <botan/s2k.h>
+
+namespace Botan {
+
+/*************************************************
+* OpenPGP S2K *
+*************************************************/
+class BOTAN_DLL OpenPGP_S2K : public S2K
+ {
+ public:
+ std::string name() const;
+ S2K* clone() const;
+ OpenPGP_S2K(const std::string&);
+ private:
+ OctetString derive(u32bit, const std::string&,
+ const byte[], u32bit, u32bit) const;
+ const std::string hash_name;
+ };
+
+}
+
+#endif