Botan  1.10.17
ossl_md.cpp
Go to the documentation of this file.
1 /*
2 * OpenSSL Hash Functions
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/internal/openssl_engine.h>
9 #include <openssl/evp.h>
10 
11 #if OPENSSL_VERSION_NUMBER >= 0x10100000
12  #error "OpenSSL 1.1 API not supported in Botan 1.10, upgrade to 2.x"
13 #endif
14 
15 namespace Botan {
16 
17 namespace {
18 
19 /*
20 * EVP Hash Function
21 */
22 class EVP_HashFunction : public HashFunction
23  {
24  public:
25  void clear();
26  std::string name() const { return algo_name; }
27  HashFunction* clone() const;
28 
29  size_t output_length() const
30  {
31  return EVP_MD_size(EVP_MD_CTX_md(&md));
32  }
33 
34  size_t hash_block_size() const
35  {
36  return EVP_MD_block_size(EVP_MD_CTX_md(&md));
37  }
38 
39  EVP_HashFunction(const EVP_MD*, const std::string&);
40  ~EVP_HashFunction();
41  private:
42  void add_data(const byte[], size_t);
43  void final_result(byte[]);
44 
45  size_t block_size;
46  std::string algo_name;
47  EVP_MD_CTX md;
48  };
49 
50 /*
51 * Update an EVP Hash Calculation
52 */
53 void EVP_HashFunction::add_data(const byte input[], size_t length)
54  {
55  EVP_DigestUpdate(&md, input, length);
56  }
57 
58 /*
59 * Finalize an EVP Hash Calculation
60 */
61 void EVP_HashFunction::final_result(byte output[])
62  {
63  EVP_DigestFinal_ex(&md, output, 0);
64  const EVP_MD* algo = EVP_MD_CTX_md(&md);
65  EVP_DigestInit_ex(&md, algo, 0);
66  }
67 
68 /*
69 * Clear memory of sensitive data
70 */
71 void EVP_HashFunction::clear()
72  {
73  const EVP_MD* algo = EVP_MD_CTX_md(&md);
74  EVP_DigestInit_ex(&md, algo, 0);
75  }
76 
77 /*
78 * Return a clone of this object
79 */
80 HashFunction* EVP_HashFunction::clone() const
81  {
82  const EVP_MD* algo = EVP_MD_CTX_md(&md);
83  return new EVP_HashFunction(algo, name());
84  }
85 
86 /*
87 * Create an EVP hash function
88 */
89 EVP_HashFunction::EVP_HashFunction(const EVP_MD* algo,
90  const std::string& name) :
91  algo_name(name)
92  {
93  EVP_MD_CTX_init(&md);
94  EVP_DigestInit_ex(&md, algo, 0);
95  }
96 
97 /*
98 * Destroy an EVP hash function
99 */
100 EVP_HashFunction::~EVP_HashFunction()
101  {
102  EVP_MD_CTX_cleanup(&md);
103  }
104 
105 }
106 
107 /*
108 * Look for an algorithm with this name
109 */
111  Algorithm_Factory&) const
112  {
113 #if !defined(OPENSSL_NO_SHA)
114  if(request.algo_name() == "SHA-160")
115  return new EVP_HashFunction(EVP_sha1(), "SHA-160");
116 #endif
117 
118 #if !defined(OPENSSL_NO_SHA256)
119  if(request.algo_name() == "SHA-224")
120  return new EVP_HashFunction(EVP_sha224(), "SHA-224");
121  if(request.algo_name() == "SHA-256")
122  return new EVP_HashFunction(EVP_sha256(), "SHA-256");
123 #endif
124 
125 #if !defined(OPENSSL_NO_SHA512)
126  if(request.algo_name() == "SHA-384")
127  return new EVP_HashFunction(EVP_sha384(), "SHA-384");
128  if(request.algo_name() == "SHA-512")
129  return new EVP_HashFunction(EVP_sha512(), "SHA-512");
130 #endif
131 
132 #if !defined(OPENSSL_NO_MD2)
133  if(request.algo_name() == "MD2")
134  return new EVP_HashFunction(EVP_md2(), "MD2");
135 #endif
136 
137 #if !defined(OPENSSL_NO_MD4)
138  if(request.algo_name() == "MD4")
139  return new EVP_HashFunction(EVP_md4(), "MD4");
140 #endif
141 
142 #if !defined(OPENSSL_NO_MD5)
143  if(request.algo_name() == "MD5")
144  return new EVP_HashFunction(EVP_md5(), "MD5");
145 #endif
146 
147 #if !defined(OPENSSL_NO_RIPEMD)
148  if(request.algo_name() == "RIPEMD-160")
149  return new EVP_HashFunction(EVP_ripemd160(), "RIPEMD-160");
150 #endif
151 
152  return 0;
153  }
154 
155 }
HashFunction * find_hash(const SCAN_Name &, Algorithm_Factory &) const
Definition: ossl_md.cpp:110
unsigned char byte
Definition: types.h:22
std::string algo_name() const
Definition: scan_name.h:37