Saner handling if config.mk doesn't exist: use a default config.defaults.mk.
[wvstreams.git] / crypto / wvdigest.cc
blob150edeea6e06060e2374ec79937ffec6a5ffc8f1
1 /*
2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * MD5, SHA-1 and HMAC digest abstractions.
6 */
7 #include "wvdigest.h"
8 #include "wvserialize.h"
9 #include <openssl/evp.h>
10 #include <openssl/hmac.h>
11 #include <assert.h>
12 #include <zlib.h>
14 /***** WvEVPMDDigest *****/
16 WvEVPMDDigest::WvEVPMDDigest(const env_md_st *_evpmd) :
17 evpmd(_evpmd), active(false)
19 evpctx = new EVP_MD_CTX;
20 _reset();
24 WvEVPMDDigest::~WvEVPMDDigest()
26 cleanup();
27 delete evpctx;
31 bool WvEVPMDDigest::_encode(WvBuf &inbuf, WvBuf &outbuf,
32 bool flush)
34 size_t len;
35 while ((len = inbuf.optgettable()) != 0)
37 const unsigned char *data = inbuf.get(len);
38 EVP_DigestUpdate(evpctx, data, len);
40 return true;
44 bool WvEVPMDDigest::_finish(WvBuf &outbuf)
46 assert(active);
47 unsigned char digest[EVP_MAX_MD_SIZE];
48 unsigned int size; // size_t is not an unsigned int on many 64 bit systems
49 EVP_DigestFinal(evpctx, digest, & size);
50 active = false;
51 outbuf.put(digest, size);
52 return true;
56 bool WvEVPMDDigest::_reset()
58 cleanup();
60 // the typecast is necessary for API compatibility with different
61 // versions of openssl. None of them *actually* change the contents of
62 // the pointer.
63 EVP_DigestInit(evpctx, (env_md_st *)evpmd);
64 active = true;
65 return true;
69 void WvEVPMDDigest::cleanup()
71 if (active)
73 // discard digest
74 unsigned char digest[EVP_MAX_MD_SIZE];
75 EVP_DigestFinal(evpctx, digest, NULL);
76 active = false;
80 size_t WvEVPMDDigest::digestsize() const
82 return EVP_MD_size((env_md_st *)evpmd);
86 /***** WvMD5Digest *****/
88 WvMD5Digest::WvMD5Digest() : WvEVPMDDigest(EVP_md5())
93 /***** WvSHA1Digest *****/
95 WvSHA1Digest::WvSHA1Digest() : WvEVPMDDigest(EVP_sha1())
99 /***** WvHMACDigest *****/
101 WvHMACDigest::WvHMACDigest(WvEVPMDDigest *_digest,
102 const void *_key, size_t _keysize) :
103 digest(_digest), keysize(_keysize), active(false)
105 key = new unsigned char[keysize];
106 memcpy(key, _key, keysize);
107 hmacctx = new HMAC_CTX;
108 _reset();
111 WvHMACDigest::~WvHMACDigest()
113 cleanup();
114 delete hmacctx;
115 deletev key;
116 delete digest;
120 bool WvHMACDigest::_encode(WvBuf &inbuf, WvBuf &outbuf,
121 bool flush)
123 size_t len;
124 while ((len = inbuf.optgettable()) != 0)
126 const unsigned char *data = inbuf.get(len);
127 HMAC_Update(hmacctx, data, len);
129 return true;
133 bool WvHMACDigest::_finish(WvBuf &outbuf)
135 assert(active);
136 unsigned char digest[EVP_MAX_MD_SIZE];
137 unsigned int size;
138 HMAC_Final(hmacctx, digest, & size);
139 active = false;
140 outbuf.put(digest, size);
141 return true;
145 bool WvHMACDigest::_reset()
147 cleanup();
148 HMAC_Init(hmacctx, key, keysize, (env_md_st *)digest->getevpmd());
149 active = true;
150 return true;
154 void WvHMACDigest::cleanup()
156 if (active)
158 // discard digest
159 unsigned char digest[EVP_MAX_MD_SIZE];
160 HMAC_Final(hmacctx, digest, NULL);
161 active = false;
166 size_t WvHMACDigest::digestsize() const
168 return digest->digestsize();
172 WvCrc32Digest::WvCrc32Digest()
174 _reset();
178 bool WvCrc32Digest::_encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
180 size_t len;
181 while ((len = inbuf.optgettable()) != 0)
182 crc = crc32(crc, inbuf.get(len), len);
183 return true;
187 bool WvCrc32Digest::_finish(WvBuf &outbuf)
189 wv_serialize(outbuf, crc);
190 return true;
194 bool WvCrc32Digest::_reset()
196 crc = crc32(0, NULL, 0);
197 return true;
201 size_t WvCrc32Digest::digestsize() const
203 return sizeof(crc);
207 WvAdler32Digest::WvAdler32Digest()
209 _reset();
213 bool WvAdler32Digest::_encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
215 size_t len;
216 while ((len = inbuf.optgettable()) != 0)
217 crc = adler32(crc, inbuf.get(len), len);
218 return true;
222 bool WvAdler32Digest::_finish(WvBuf &outbuf)
224 wv_serialize(outbuf, crc);
225 return true;
229 bool WvAdler32Digest::_reset()
231 crc = adler32(0, NULL, 0);
232 return true;
236 size_t WvAdler32Digest::digestsize() const
238 return sizeof(crc);