aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/openssl/ossl_md.cpp
blob: f41e0f9502444c0ff7e7fd3e9675af378d0066fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* OpenSSL Hash Functions
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/openssl_engine.h>
#include <openssl/evp.h>

namespace Botan {

namespace {

/*
* EVP Hash Function
*/
class EVP_HashFunction : public HashFunction
   {
   public:
      void clear();
      std::string name() const { return algo_name; }
      HashFunction* clone() const;
      EVP_HashFunction(const EVP_MD*, const std::string&);
      ~EVP_HashFunction();
   private:
      void add_data(const byte[], u32bit);
      void final_result(byte[]);

      std::string algo_name;
      EVP_MD_CTX md;
   };

/*
* Update an EVP Hash Calculation
*/
void EVP_HashFunction::add_data(const byte input[], u32bit length)
   {
   EVP_DigestUpdate(&md, input, length);
   }

/*
* Finalize an EVP Hash Calculation
*/
void EVP_HashFunction::final_result(byte output[])
   {
   EVP_DigestFinal_ex(&md, output, 0);
   const EVP_MD* algo = EVP_MD_CTX_md(&md);
   EVP_DigestInit_ex(&md, algo, 0);
   }

/*
* Clear memory of sensitive data
*/
void EVP_HashFunction::clear()
   {
   const EVP_MD* algo = EVP_MD_CTX_md(&md);
   EVP_DigestInit_ex(&md, algo, 0);
   }

/*
* Return a clone of this object
*/
HashFunction* EVP_HashFunction::clone() const
   {
   const EVP_MD* algo = EVP_MD_CTX_md(&md);
   return new EVP_HashFunction(algo, name());
   }

/*
* Create an EVP hash function
*/
EVP_HashFunction::EVP_HashFunction(const EVP_MD* algo,
                                   const std::string& name) :
   HashFunction(EVP_MD_size(algo), EVP_MD_block_size(algo)),
   algo_name(name)
   {
   EVP_MD_CTX_init(&md);
   EVP_DigestInit_ex(&md, algo, 0);
   }

/*
* Destroy an EVP hash function
*/
EVP_HashFunction::~EVP_HashFunction()
   {
   EVP_MD_CTX_cleanup(&md);
   }

}

/*
* Look for an algorithm with this name
*/
HashFunction* OpenSSL_Engine::find_hash(const SCAN_Name& request,
                                        Algorithm_Factory&) const
   {
#if !defined(OPENSSL_NO_SHA)
   if(request.algo_name() == "SHA-160")
      return new EVP_HashFunction(EVP_sha1(), "SHA-160");
#endif

#if !defined(OPENSSL_NO_SHA256)
   if(request.algo_name() == "SHA-224")
      return new EVP_HashFunction(EVP_sha224(), "SHA-224");
   if(request.algo_name() == "SHA-256")
      return new EVP_HashFunction(EVP_sha256(), "SHA-256");
#endif

#if !defined(OPENSSL_NO_SHA512)
   if(request.algo_name() == "SHA-384")
      return new EVP_HashFunction(EVP_sha384(), "SHA-384");
   if(request.algo_name() == "SHA-512")
      return new EVP_HashFunction(EVP_sha512(), "SHA-512");
#endif

#if !defined(OPENSSL_NO_MD2)
   if(request.algo_name() == "MD2")
      return new EVP_HashFunction(EVP_md2(), "MD2");
#endif

#if !defined(OPENSSL_NO_MD4)
   if(request.algo_name() == "MD4")
      return new EVP_HashFunction(EVP_md4(), "MD4");
#endif

#if !defined(OPENSSL_NO_MD5)
   if(request.algo_name() == "MD5")
      return new EVP_HashFunction(EVP_md5(), "MD5");
#endif

#if !defined(OPENSSL_NO_RIPEMD)
   if(request.algo_name() == "RIPEMD-160")
      return new EVP_HashFunction(EVP_ripemd160(), "RIPEMD-160");
#endif

   return 0;
   }

}