Debian Codegraph @

src/lib/crypto/OSSLEVPHashAlgorithm.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 OSSLEVPHashAlgorithm.cpp
29
30 Base class for OpenSSL hash algorithm classes
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "OSSLEVPHashAlgorithm.h"
35 #include "OSSLComp.h"
36
37 // Destructor
38 OSSLEVPHashAlgorithm::~OSSLEVPHashAlgorithm()
39 {
40 EVP_MD_CTX_free(curCTX);
41 }
42
43 // Hashing functions
44 bool OSSLEVPHashAlgorithm::hashInit()
45 {
46 if (!HashAlgorithm::hashInit())
47 {
48 return false;
49 }
50
51 // Initialize the context
52 curCTX = EVP_MD_CTX_new();
53 if (curCTX == NULL)
54 {
55 ERROR_MSG("Failed to allocate space for EVP_MD_CTX");
56
57 return false;
58 }
59
60 // Initialize EVP digesting
61 if (!EVP_DigestInit_ex(curCTX, getEVPHash(), NULL))
62 {
63 ERROR_MSG("EVP_DigestInit failed");
64
65 EVP_MD_CTX_free(curCTX);
66 curCTX = NULL;
67
68 ByteString dummy;
69 HashAlgorithm::hashFinal(dummy);
70
71 return false;
72 }
73
74 return true;
75 }
76
77 bool OSSLEVPHashAlgorithm::hashUpdate(const ByteString& data)
78 {
79 if (!HashAlgorithm::hashUpdate(data))
80 {
81 return false;
82 }
83
84 // Continue digesting
85 if (data.size() == 0)
86 {
87 return true;
88 }
89
90 if (!EVP_DigestUpdate(curCTX, (unsigned char*) data.const_byte_str(), data.size()))
91 {
92 ERROR_MSG("EVP_DigestUpdate failed");
93
94 EVP_MD_CTX_free(curCTX);
95 curCTX = NULL;
96
97 ByteString dummy;
98 HashAlgorithm::hashFinal(dummy);
99
100 return false;
101 }
102
103 return true;
104 }
105
106 bool OSSLEVPHashAlgorithm::hashFinal(ByteString& hashedData)
107 {
108 if (!HashAlgorithm::hashFinal(hashedData))
109 {
110 return false;
111 }
112
113 hashedData.resize(EVP_MD_size(getEVPHash()));
114 unsigned int outLen = hashedData.size();
115
116 if (!EVP_DigestFinal_ex(curCTX, &hashedData[0], &outLen))
117 {
118 ERROR_MSG("EVP_DigestFinal failed");
119
120 EVP_MD_CTX_free(curCTX);
121 curCTX = NULL;
122
123 return false;
124 }
125
126 hashedData.resize(outLen);
127
128 EVP_MD_CTX_free(curCTX);
129 curCTX = NULL;
130
131 return true;
132 }
133