aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/openssl/ossl_md.cpp
blob: 4e28c515e8c4f19ef3ee3e16d55a45f42fe76f59 (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
/*************************************************
* OpenSSL Hash Functions Source File             *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/eng_ossl.h>
#include <openssl/evp.h>

namespace Botan {

namespace {

/*************************************************
* EVP Hash Function                              *
*************************************************/
class EVP_HashFunction : public HashFunction
   {
   public:
      void clear() throw();
      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() throw()
   {
   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(request.algo_name() == "SHA-160")
      return new EVP_HashFunction(EVP_sha1(), "SHA-160");

   if(request.algo_name() == "MD2")
      return new EVP_HashFunction(EVP_md2(), "MD2");

   if(request.algo_name() == "MD4")
      return new EVP_HashFunction(EVP_md4(), "MD4");

   if(request.algo_name() == "MD5")
      return new EVP_HashFunction(EVP_md5(), "MD5");

   if(request.algo_name() == "RIPEMD-160")
      return new EVP_HashFunction(EVP_ripemd160(), "RIPEMD-160");

   return 0;
   }

}