1 | // |
---|
2 | // HMACEngine.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/HMACEngine.h#3 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Crypt |
---|
8 | // Module: HMACEngine |
---|
9 | // |
---|
10 | // Definition of the HMACEngine class. |
---|
11 | // |
---|
12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Foundation_HMACEngine_INCLUDED |
---|
40 | #define Foundation_HMACEngine_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/DigestEngine.h" |
---|
45 | #include <cstring> |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | |
---|
50 | |
---|
51 | template <class Engine> |
---|
52 | class HMACEngine: public DigestEngine |
---|
53 | /// This class implementes the HMAC message |
---|
54 | /// authentication code algorithm, as specified |
---|
55 | /// in RFC 2104. The underlying DigestEngine |
---|
56 | /// (MD5Engine, SHA1Engine, etc.) must be given as |
---|
57 | /// template argument. |
---|
58 | /// Since the HMACEngine is a DigestEngine, it can |
---|
59 | /// be used with the DigestStream class to create |
---|
60 | /// a HMAC for a stream. |
---|
61 | { |
---|
62 | public: |
---|
63 | enum |
---|
64 | { |
---|
65 | BLOCK_SIZE = Engine::BLOCK_SIZE, |
---|
66 | DIGEST_SIZE = Engine::DIGEST_SIZE |
---|
67 | }; |
---|
68 | |
---|
69 | HMACEngine(const std::string& passphrase) |
---|
70 | { |
---|
71 | init(passphrase.data(), (unsigned) passphrase.length()); |
---|
72 | } |
---|
73 | |
---|
74 | HMACEngine(const char* passphrase, unsigned length) |
---|
75 | { |
---|
76 | poco_check_ptr (passphrase); |
---|
77 | |
---|
78 | init(passphrase, length); |
---|
79 | } |
---|
80 | |
---|
81 | ~HMACEngine() |
---|
82 | { |
---|
83 | std::memset(_ipad, 0, BLOCK_SIZE); |
---|
84 | std::memset(_opad, 0, BLOCK_SIZE); |
---|
85 | delete [] _ipad; |
---|
86 | delete [] _opad; |
---|
87 | } |
---|
88 | |
---|
89 | unsigned digestLength() const |
---|
90 | { |
---|
91 | return DIGEST_SIZE; |
---|
92 | } |
---|
93 | |
---|
94 | void reset() |
---|
95 | { |
---|
96 | _engine.reset(); |
---|
97 | _engine.update(_ipad, BLOCK_SIZE); |
---|
98 | } |
---|
99 | |
---|
100 | const DigestEngine::Digest& digest() |
---|
101 | { |
---|
102 | const DigestEngine::Digest& d = _engine.digest(); |
---|
103 | char db[DIGEST_SIZE]; |
---|
104 | char* pdb = db; |
---|
105 | for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end(); ++it) |
---|
106 | *pdb++ = *it; |
---|
107 | _engine.reset(); |
---|
108 | _engine.update(_opad, BLOCK_SIZE); |
---|
109 | _engine.update(db, DIGEST_SIZE); |
---|
110 | const DigestEngine::Digest& result = _engine.digest(); |
---|
111 | reset(); |
---|
112 | return result; |
---|
113 | } |
---|
114 | |
---|
115 | protected: |
---|
116 | void init(const char* passphrase, unsigned length) |
---|
117 | { |
---|
118 | _ipad = new char[BLOCK_SIZE]; |
---|
119 | _opad = new char[BLOCK_SIZE]; |
---|
120 | std::memset(_ipad, 0, BLOCK_SIZE); |
---|
121 | std::memset(_opad, 0, BLOCK_SIZE); |
---|
122 | if (length > BLOCK_SIZE) |
---|
123 | { |
---|
124 | _engine.reset(); |
---|
125 | _engine.update(passphrase, length); |
---|
126 | const DigestEngine::Digest& d = _engine.digest(); |
---|
127 | char* ipad = _ipad; |
---|
128 | char* opad = _opad; |
---|
129 | int n = BLOCK_SIZE; |
---|
130 | for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it) |
---|
131 | { |
---|
132 | *ipad++ = *it; |
---|
133 | *opad++ = *it; |
---|
134 | } |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | std::memcpy(_ipad, passphrase, length); |
---|
139 | std::memcpy(_opad, passphrase, length); |
---|
140 | } |
---|
141 | for (int i = 0; i < BLOCK_SIZE; ++i) |
---|
142 | { |
---|
143 | _ipad[i] ^= 0x36; |
---|
144 | _opad[i] ^= 0x5c; |
---|
145 | } |
---|
146 | reset(); |
---|
147 | } |
---|
148 | |
---|
149 | void updateImpl(const void* data, unsigned length) |
---|
150 | { |
---|
151 | _engine.update(data, length); |
---|
152 | } |
---|
153 | |
---|
154 | private: |
---|
155 | HMACEngine(); |
---|
156 | HMACEngine(const HMACEngine&); |
---|
157 | HMACEngine& operator = (const HMACEngine&); |
---|
158 | |
---|
159 | Engine _engine; |
---|
160 | char* _ipad; |
---|
161 | char* _opad; |
---|
162 | }; |
---|
163 | |
---|
164 | |
---|
165 | } // namespace Poco |
---|
166 | |
---|
167 | |
---|
168 | #endif // Foundation_HMACEngine_INCLUDED |
---|