Botan: src/engine/openssl/ossl_md.cpp Source File

Botan 1.10.16

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