Botan  2.6.0
Crypto and TLS for C++11
pgp_s2k.h
Go to the documentation of this file.
1 /*
2 * OpenPGP PBKDF
3 * (C) 1999-2007,2017 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_OPENPGP_S2K_H_
9 #define BOTAN_OPENPGP_S2K_H_
10 
11 #include <botan/pbkdf.h>
12 #include <botan/hash.h>
13 
14 namespace Botan {
15 
16 /**
17 * OpenPGP's S2K
18 *
19 * See RFC 4880 sections 3.7.1.1, 3.7.1.2, and 3.7.1.3
20 * If the salt is empty and iterations == 1, "simple" S2K is used
21 * If the salt is non-empty and iterations == 1, "salted" S2K is used
22 * If the salt is non-empty and iterations > 1, "iterated" S2K is used
23 *
24 * Due to complexities of the PGP S2K algorithm, time-based derivation
25 * is not supported. So if iterations == 0 and msec.count() > 0, an
26 * exception is thrown. In the future this may be supported, in which
27 * case "iterated" S2K will be used and the number of iterations
28 * performed is returned.
29 *
30 * Note that unlike PBKDF2, OpenPGP S2K's "iterations" are defined as
31 * the number of bytes hashed.
32 */
33 class BOTAN_PUBLIC_API(2,2) OpenPGP_S2K final : public PBKDF
34  {
35  public:
36  /**
37  * @param hash the hash function to use
38  */
39  explicit OpenPGP_S2K(HashFunction* hash) : m_hash(hash) {}
40 
41  std::string name() const override
42  {
43  return "OpenPGP-S2K(" + m_hash->name() + ")";
44  }
45 
46  PBKDF* clone() const override
47  {
48  return new OpenPGP_S2K(m_hash->clone());
49  }
50 
51  size_t pbkdf(uint8_t output_buf[], size_t output_len,
52  const std::string& passphrase,
53  const uint8_t salt[], size_t salt_len,
54  size_t iterations,
55  std::chrono::milliseconds msec) const override;
56 
57  /**
58  * RFC 4880 encodes the iteration count to a single-byte value
59  */
60  static uint8_t encode_count(size_t iterations);
61 
62  static size_t decode_count(uint8_t encoded_iter);
63 
64  private:
65  std::unique_ptr<HashFunction> m_hash;
66  };
67 
68 }
69 
70 #endif
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
std::string name() const override
Definition: pgp_s2k.h:41
PBKDF * clone() const override
Definition: pgp_s2k.h:46
size_t salt_len
Definition: x509_obj.cpp:26
Definition: alg_id.cpp:13
OpenPGP_S2K(HashFunction *hash)
Definition: pgp_s2k.h:39
MechanismType hash