diff options
author | Jack Lloyd <[email protected]> | 2017-05-27 07:43:02 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-05-27 07:43:02 -0400 |
commit | 360769bc65057ac44013f93a25ff9a06d971acae (patch) | |
tree | 691dfd44c0462093280a8df45081b0b990eb3803 /src/lib/pbkdf | |
parent | a293c21ba83fb9c4d59237f8fa0fd5d852c27992 (diff) |
Add (back) OpenPGP-S2K
It was removed somewhere along the line in 1.11, with the logic
that it is a funky PGP-specific scheme and (quoting the commit
that removed it) "not really useful outside of a full PGP implementation".
This assumed that the PGP implementation would be in Botan itself, but
PGP is implemented in https://github.com/evpo/EncryptPad/ (which is
a PGP implementation using 1.10), and RNP (https://github.com/riboseinc/rnp)
would like to use it also.
This work was sponsored by Ribose Inc (@riboseinc).
Diffstat (limited to 'src/lib/pbkdf')
-rw-r--r-- | src/lib/pbkdf/pbkdf.cpp | 12 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/info.txt | 7 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp | 90 | ||||
-rw-r--r-- | src/lib/pbkdf/pgp_s2k/pgp_s2k.h | 67 |
4 files changed, 176 insertions, 0 deletions
diff --git a/src/lib/pbkdf/pbkdf.cpp b/src/lib/pbkdf/pbkdf.cpp index a60c93d5c..40610998b 100644 --- a/src/lib/pbkdf/pbkdf.cpp +++ b/src/lib/pbkdf/pbkdf.cpp @@ -16,6 +16,10 @@ #include <botan/pbkdf2.h> #endif +#if defined(BOTAN_HAS_PGP_S2K) +#include <botan/pgp_s2k.h> +#endif + namespace Botan { std::unique_ptr<PBKDF> PBKDF::create(const std::string& algo_spec, @@ -50,6 +54,14 @@ std::unique_ptr<PBKDF> PBKDF::create(const std::string& algo_spec, } #endif +#if defined(BOTAN_HAS_PGP_S2K) + if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) + { + if(auto hash = HashFunction::create(req.arg(0))) + return std::unique_ptr<PBKDF>(new OpenPGP_S2K(hash.release())); + } +#endif + BOTAN_UNUSED(req); BOTAN_UNUSED(provider); diff --git a/src/lib/pbkdf/pgp_s2k/info.txt b/src/lib/pbkdf/pgp_s2k/info.txt new file mode 100644 index 000000000..5aabc0919 --- /dev/null +++ b/src/lib/pbkdf/pgp_s2k/info.txt @@ -0,0 +1,7 @@ +<defines> +PGP_S2K -> 20170527 +</defines> + +<requires> +hash +</requires> diff --git a/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp new file mode 100644 index 000000000..d8b5d770e --- /dev/null +++ b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp @@ -0,0 +1,90 @@ +/* +* OpenPGP S2K +* (C) 1999-2007,2017 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/pgp_s2k.h> + +namespace Botan { + +//static +uint8_t OpenPGP_S2K::encode_count(size_t desired_iterations) + { + /* + Only 256 different iterations are actually representable in OpenPGP format ... + */ + for(size_t c = 0; c < 256; ++c) + { + size_t decoded_iter = OpenPGP_S2K::decode_count(c); + if(decoded_iter >= desired_iterations) + return c; + } + } + +//static +size_t OpenPGP_S2K::decode_count(uint8_t iter) + { + // See RFC 4880 section 3.7.1.3 + return (16 + (iter & 0x0F)) << ((iter >> 4) + 6); + } + +size_t OpenPGP_S2K::pbkdf(uint8_t output_buf[], size_t output_len, + const std::string& passphrase, + const uint8_t salt[], size_t salt_len, + size_t iterations, + std::chrono::milliseconds msec) const + { + if(iterations == 0 && msec.count() > 0) // FIXME + throw Not_Implemented("OpenPGP_S2K does not implemented timed KDF"); + + secure_vector<uint8_t> input_buf(salt_len + passphrase.size()); + if(salt_len > 0) + { + copy_mem(&input_buf[0], salt, salt_len); + } + if(passphrase.empty() == false) + { + copy_mem(&input_buf[salt_len], + reinterpret_cast<const uint8_t*>(passphrase.data()), + passphrase.size()); + } + + /* + The input is always fully processed even if iterations is very small + */ + const size_t to_hash = std::max(iterations, input_buf.size()); + + secure_vector<uint8_t> hash_buf(m_hash->output_length()); + + size_t pass = 0; + size_t generated = 0; + + while(generated != output_len) + { + const size_t output_this_pass = + std::min(hash_buf.size(), output_len - generated); + + // Preload some number of zero bytes (empty first iteration) + std::vector<uint8_t> zero_padding(pass); + m_hash->update(zero_padding); + + size_t left = to_hash; + while(left > 0) + { + const size_t input_to_take = std::min(left, input_buf.size()); + m_hash->update(input_buf.data(), input_to_take); + left -= input_to_take; + } + + m_hash->final(hash_buf.data()); + copy_mem(output_buf + generated, hash_buf.data(), output_this_pass); + generated += output_this_pass; + ++pass; + } + + return iterations; + } + +} diff --git a/src/lib/pbkdf/pgp_s2k/pgp_s2k.h b/src/lib/pbkdf/pgp_s2k/pgp_s2k.h new file mode 100644 index 000000000..3d85ba306 --- /dev/null +++ b/src/lib/pbkdf/pgp_s2k/pgp_s2k.h @@ -0,0 +1,67 @@ +/* +* OpenPGP PBKDF +* (C) 1999-2007,2017 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_OPENPGP_S2K_H__ +#define BOTAN_OPENPGP_S2K_H__ + +#include <botan/pbkdf.h> +#include <botan/hash.h> + +namespace Botan { + +/** +* OpenPGP's S2K +* +* See RFC 4880 sections 3.7.1.1, 3.7.1.2, and 3.7.1.3 +* If the salt is empty and iterations == 1, "simple" S2K is used +* If the salt is non-empty and iterations == 1, "salted" S2K is used +* If the salt is non-empty and iterations > 1, "iterated" S2K is used +* +* If iterations == 0 and msec.count() > 0, "iterated" S2K is assumed, +* and the number of iterations performed is returned. +* +* Note that unlike PBKDF2, OpenPGP S2K's "iterations" are defined as +* the number of bytes hashed. +*/ +class BOTAN_DLL OpenPGP_S2K final : public PBKDF + { + public: + /** + * @param hash_in the hash function to use + */ + explicit OpenPGP_S2K(HashFunction* hash) : m_hash(hash) {} + + std::string name() const override + { + return "OpenPGP-S2K(" + m_hash->name() + ")"; + } + + PBKDF* clone() const + { + return new OpenPGP_S2K(m_hash->clone()); + } + + size_t pbkdf(uint8_t output_buf[], size_t output_len, + const std::string& passphrase, + const uint8_t salt[], size_t salt_len, + size_t iterations, + std::chrono::milliseconds msec) const override; + + /** + * RFC 4880 encodes the iteration count to a single-byte value + */ + static uint8_t encode_count(size_t iterations); + + static size_t decode_count(uint8_t encoded_iter); + + private: + std::unique_ptr<HashFunction> m_hash; + }; + +} + +#endif |