2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * MD5, SHA-1 and HMAC digest abstractions.
8 #include "wvserialize.h"
9 #include <openssl/evp.h>
10 #include <openssl/hmac.h>
14 /***** WvEVPMDDigest *****/
16 WvEVPMDDigest::WvEVPMDDigest(const env_md_st
*_evpmd
) :
17 evpmd(_evpmd
), active(false)
19 evpctx
= new EVP_MD_CTX
;
24 WvEVPMDDigest::~WvEVPMDDigest()
31 bool WvEVPMDDigest::_encode(WvBuf
&inbuf
, WvBuf
&outbuf
,
35 while ((len
= inbuf
.optgettable()) != 0)
37 const unsigned char *data
= inbuf
.get(len
);
38 EVP_DigestUpdate(evpctx
, data
, len
);
44 bool WvEVPMDDigest::_finish(WvBuf
&outbuf
)
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
);
51 outbuf
.put(digest
, size
);
56 bool WvEVPMDDigest::_reset()
60 // the typecast is necessary for API compatibility with different
61 // versions of openssl. None of them *actually* change the contents of
63 EVP_DigestInit(evpctx
, (env_md_st
*)evpmd
);
69 void WvEVPMDDigest::cleanup()
74 unsigned char digest
[EVP_MAX_MD_SIZE
];
75 EVP_DigestFinal(evpctx
, digest
, NULL
);
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
;
111 WvHMACDigest::~WvHMACDigest()
120 bool WvHMACDigest::_encode(WvBuf
&inbuf
, WvBuf
&outbuf
,
124 while ((len
= inbuf
.optgettable()) != 0)
126 const unsigned char *data
= inbuf
.get(len
);
127 HMAC_Update(hmacctx
, data
, len
);
133 bool WvHMACDigest::_finish(WvBuf
&outbuf
)
136 unsigned char digest
[EVP_MAX_MD_SIZE
];
138 HMAC_Final(hmacctx
, digest
, & size
);
140 outbuf
.put(digest
, size
);
145 bool WvHMACDigest::_reset()
148 HMAC_Init(hmacctx
, key
, keysize
, (env_md_st
*)digest
->getevpmd());
154 void WvHMACDigest::cleanup()
159 unsigned char digest
[EVP_MAX_MD_SIZE
];
160 HMAC_Final(hmacctx
, digest
, NULL
);
166 size_t WvHMACDigest::digestsize() const
168 return digest
->digestsize();
172 WvCrc32Digest::WvCrc32Digest()
178 bool WvCrc32Digest::_encode(WvBuf
&inbuf
, WvBuf
&outbuf
, bool flush
)
181 while ((len
= inbuf
.optgettable()) != 0)
182 crc
= crc32(crc
, inbuf
.get(len
), len
);
187 bool WvCrc32Digest::_finish(WvBuf
&outbuf
)
189 wv_serialize(outbuf
, crc
);
194 bool WvCrc32Digest::_reset()
196 crc
= crc32(0, NULL
, 0);
201 size_t WvCrc32Digest::digestsize() const
207 WvAdler32Digest::WvAdler32Digest()
213 bool WvAdler32Digest::_encode(WvBuf
&inbuf
, WvBuf
&outbuf
, bool flush
)
216 while ((len
= inbuf
.optgettable()) != 0)
217 crc
= adler32(crc
, inbuf
.get(len
), len
);
222 bool WvAdler32Digest::_finish(WvBuf
&outbuf
)
224 wv_serialize(outbuf
, crc
);
229 bool WvAdler32Digest::_reset()
231 crc
= adler32(0, NULL
, 0);
236 size_t WvAdler32Digest::digestsize() const