Merge lp:~zorba-coders/zorba/security-module-v2 into lp:zorba/security-module

Proposed by Matthias Brantner on 2012-07-20
Status: Merged
Approved by: Chris Hillery on 2012-07-27
Approved revision: 26
Merged at revision: 23
Proposed branch: lp:~zorba-coders/zorba/security-module-v2
Merge into: lp:zorba/security-module
Diff against target: 2809 lines (+668/-1838)
24 files modified
CMakeLists.txt (+14/-2)
src/CMakeLists.txt (+12/-2)
src/hash.xq (+74/-19)
src/hash.xq.src/hash.cpp (+81/-57)
src/hash.xq.src/hash.h (+135/-29)
src/hash.xq.src/md5_impl.cpp (+0/-371)
src/hash.xq.src/md5_impl.h (+0/-93)
src/hash.xq.src/sha1.cpp (+0/-264)
src/hash.xq.src/sha1.h (+0/-259)
src/hmac.xq (+116/-15)
src/hmac.xq.src/hmac.cpp (+176/-54)
src/hmac.xq.src/hmac.h (+40/-20)
src/hmac.xq.src/hmac_sha1.cpp (+0/-69)
src/hmac.xq.src/hmac_sha1.h (+0/-59)
src/hmac.xq.src/sha1.cpp (+0/-264)
src/hmac.xq.src/sha1.h (+0/-259)
test/ExpQueryResults/security/hash-binary.xml.res (+1/-0)
test/ExpQueryResults/security/hash-md5.xml.res (+1/-1)
test/ExpQueryResults/security/hash-sha256.xml.res (+1/-0)
test/ExpQueryResults/security/hmac-sha256.xml.res (+1/-0)
test/Queries/security/hash-binary.xq (+4/-0)
test/Queries/security/hash-md5.xq (+1/-1)
test/Queries/security/hash-sha256.xq (+6/-0)
test/Queries/security/hmac-sha256.xq (+5/-0)
To merge this branch: bzr merge lp:~zorba-coders/zorba/security-module-v2
Reviewer Review Type Date Requested Status
Chris Hillery 2012-07-20 Approve on 2012-07-27
Matthias Brantner Approve on 2012-07-20
Review via email: mp+115881@code.launchpad.net

Commit message

v2 of hash and hmac modules
- based on openssl
- handling streamable strings
- hashing and hmac of binaries
- sha256 support

To post a comment you must log in.
review: Approve
Zorba Build Bot (zorba-buildbot) wrote :

The attempt to merge lp:~zorba-coders/zorba/security-module-v2 into lp:zorba/security-module failed. Below is the output from the failed tests.

CMake Error at /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake:274 (message):
  Validation queue job security-module-v2-2012-07-20T03-41-52.741Z is
  finished. The final status was:

  2 tests did not succeed - changes not commited.

Error in read script: /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake

Zorba Build Bot (zorba-buildbot) wrote :

The attempt to merge lp:~zorba-coders/zorba/security-module-v2 into lp:zorba/security-module failed. Below is the output from the failed tests.

CMake Error at /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake:274 (message):
  Validation queue job security-module-v2-2012-07-20T16-16-49.584Z is
  finished. The final status was:

  1 tests did not succeed - changes not commited.

Error in read script: /home/ceej/zo/testing/zorbatest/tester/TarmacLander.cmake

Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job security-module-v2-2012-07-24T17-40-46.381Z is finished. The final status was:

All tests succeeded!

Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 1 Approve, 1 Pending.

Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job security-module-v2-2012-07-27T03-36-52.673Z is finished. The final status was:

All tests succeeded!

Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 1 Approve, 1 Pending.

Chris Hillery (ceejatec) :
review: Approve
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job security-module-v2-2012-07-27T06-41-24.798Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2011-08-10 14:05:21 +0000
3+++ CMakeLists.txt 2012-07-27 03:35:22 +0000
4@@ -21,7 +21,19 @@
5 FIND_PACKAGE (Zorba REQUIRED HINTS "${ZORBA_BUILD_DIR}")
6 INCLUDE ("${Zorba_USE_FILE}")
7
8-ADD_TEST_DIRECTORY("${PROJECT_SOURCE_DIR}/test")
9-ADD_SUBDIRECTORY("src")
10+FIND_PACKAGE(OpenSSL)
11+IF (NOT OPENSSL_FOUND)
12+
13+ MESSAGE(WARNING "Could not find the OpenSSL library and development headers")
14+ MESSAGE(WARNING "Not building security modules")
15+
16+ELSE (NOT OPENSSL_FOUND)
17+
18+ INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
19+
20+ ADD_TEST_DIRECTORY("${PROJECT_SOURCE_DIR}/test")
21+ ADD_SUBDIRECTORY("src")
22+
23+ENDIF (NOT OPENSSL_FOUND)
24
25 DONE_DECLARING_ZORBA_URIS()
26
27=== modified file 'src/CMakeLists.txt'
28--- src/CMakeLists.txt 2011-08-02 08:51:47 +0000
29+++ src/CMakeLists.txt 2012-07-27 03:35:22 +0000
30@@ -12,5 +12,15 @@
31 # See the License for the specific language governing permissions and
32 # limitations under the License.
33
34-DECLARE_ZORBA_MODULE (URI "http://www.zorba-xquery.com/modules/cryptography/hmac" VERSION 1.0 FILE "hmac.xq")
35-DECLARE_ZORBA_MODULE (URI "http://www.zorba-xquery.com/modules/cryptography/hash" VERSION 1.0 FILE "hash.xq")
36+DECLARE_ZORBA_MODULE (
37+ URI "http://www.zorba-xquery.com/modules/cryptography/hmac"
38+ VERSION 2.0
39+ FILE "hmac.xq"
40+ LINK_LIBRARIES ${OPENSSL_LIBRARIES}
41+)
42+DECLARE_ZORBA_MODULE (
43+ URI "http://www.zorba-xquery.com/modules/cryptography/hash"
44+ VERSION 2.0
45+ FILE "hash.xq"
46+ LINK_LIBRARIES ${OPENSSL_LIBRARIES}
47+)
48
49=== modified file 'src/hash.xq'
50--- src/hash.xq 2011-08-13 00:08:45 +0000
51+++ src/hash.xq 2012-07-27 03:35:22 +0000
52@@ -1,7 +1,7 @@
53 xquery version "1.0";
54
55 (:
56- : Copyright 2006-2009 The FLWOR Foundation.
57+ : Copyright 2006-2012 The FLWOR Foundation.
58 :
59 : Licensed under the Apache License, Version 2.0 (the "License");
60 : you may not use this file except in compliance with the License.
61@@ -18,35 +18,69 @@
62
63 (:~
64 : This module provides functions that perform different hash operations.
65+ : For example, they compute MD5 and various SHA functions on either
66+ : strings or binary. The result is the base64 encoded value of the hash.
67 :
68- : @author Gabriel Petrovay, Markus Pilman
69+ : @author Gabriel Petrovay, Markus Pilman, Matthias Brantner
70 : @project cryptography
71 :)
72 module namespace hash = "http://www.zorba-xquery.com/modules/cryptography/hash";
73
74 declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
75-declare option ver:module-version "1.0";
76+declare option ver:module-version "2.0";
77
78 (:~
79 : Computes the MD5 hash of the string provided as parameter.
80 :
81- : @param $value The string to hash.
82- : @return The MD5 hash of the provided string.
83+ : @param $value The string to hash
84+ :
85+ : @return The MD5 hash as xs:base64Binary
86 :)
87-declare function hash:md5($value as xs:string) as xs:string
88+declare function hash:md5($value as xs:string)
89+ as xs:base64Binary
90 {
91- hash:hash-impl($value, "md5")
92+ hash:hash($value, "md5")
93 };
94
95 (:~
96 : Computes the SHA1 hash of the string provided as parameter.
97 :
98 : @param $value The string to hash.
99- : @return The base64 encoded SHA1 hash of the provided string.
100- :)
101-declare function hash:sha1($value as xs:string) as xs:string
102-{
103- hash:hash-impl($value, "sha1")
104+ :
105+ : @return The SHA1 hash as xs:base64Binary
106+ :)
107+declare function hash:sha1($value as xs:string)
108+ as xs:base64Binary
109+{
110+ hash:hash($value, "sha1")
111+};
112+
113+(:~
114+ : This function computes the MD5 hash value of the binary form of the given
115+ : base64Binary item, i.e. the item is base64-decoded before hashing.
116+ :
117+ : @param $value The binary item to hash.
118+ :
119+ : @return The MD5 hash of the provided binary.
120+ :)
121+declare function hash:md5-binary($value as xs:base64Binary)
122+ as xs:base64Binary
123+{
124+ hash:hash-binary($value, "md5")
125+};
126+
127+(:~
128+ : This function computes the SHA1 hash value of the binary form of the given
129+ : base64Binary item, i.e. the item is base64-decoded before hashing.
130+ :
131+ : @param $value The binary item to hash.
132+ :
133+ : @return The base64 encoded SHA1 hash of the provided binary.
134+ :)
135+declare function hash:sha1-binary($value as xs:base64Binary)
136+ as xs:base64Binary
137+{
138+ hash:hash-binary($value, "sha1")
139 };
140
141 (:~
142@@ -54,10 +88,31 @@
143 : The function expects the hash algorithm to be used as parameter.
144 :
145 : @param $value The string to be hashed.
146- : @param $alg The algorithm to use for this hashing operation. Currently only
147- : "md5" and "sha1" algorithms are available. If no valid algorithm
148- : name is given, md5 will be used.
149- : @return The hash of the provided string. In case SHA1 is used, the resulting
150- : hash value is base64 encoded.
151- :)
152-declare function hash:hash-impl($value as xs:string, $alg as xs:string) as xs:string external;
153+ :
154+ : @param $alg The algorithm to use for this hashing operation. Supported
155+ : algorithms are "md5", "sha1", and "sha256".
156+ :
157+ : @return The hash as xs:base64binary of the provided string
158+ :
159+ : @error hash:unsupported-algorithm if the given hash algorithm is not
160+ : supported
161+ :)
162+declare function hash:hash($value as xs:string, $alg as xs:string)
163+ as xs:base64Binary external;
164+
165+(:~
166+ : This function computes a hash value of the binary form of the given
167+ : base64Binary item, i.e. the item is base64-decoded before hashing.
168+ :
169+ : @param $value The binary item to be hashed.
170+ :
171+ : @param $alg The algorithm to use for this hashing operation. Supported
172+ : algorithms are "md5", "sha1", and "sha256".
173+ :
174+ : @return The hash as xs:base64Binary of the provided binary
175+ :
176+ : @error hash:unsupported-algorithm if the given hash algorithm is not
177+ : supported
178+ :)
179+declare function hash:hash-binary($value as xs:base64Binary, $alg as xs:string)
180+ as xs:base64Binary external;
181
182=== modified file 'src/hash.xq.src/hash.cpp'
183--- src/hash.xq.src/hash.cpp 2011-11-29 00:56:02 +0000
184+++ src/hash.xq.src/hash.cpp 2012-07-27 03:35:22 +0000
185@@ -23,29 +23,46 @@
186 #include <zorba/user_exception.h>
187 #include <zorba/item_factory.h>
188 #include <zorba/singleton_item_sequence.h>
189+#include <zorba/empty_sequence.h>
190 #include <zorba/xquery_exception.h>
191 #include <zorba/zorba.h>
192 #include "hash.h"
193
194-#include "md5_impl.h"
195-#include "sha1.h"
196+#include "openssl/md5.h"
197+#include "openssl/sha.h"
198+
199
200 namespace zorba { namespace security {
201
202-zorba::String getOneStringArgument(
203- const HashModule* aModule,
204+/******************************************************************************
205+ ******************************************************************************/
206+zorba::String
207+HashModule::getStringArgument(
208 const ExternalFunction::Arguments_t& aArgs,
209 int aIndex)
210 {
211 zorba::Item lItem;
212 Iterator_t args_iter = aArgs[aIndex]->getIterator();
213 args_iter->open();
214- args_iter->next(lItem); // must have one because the signature is defined like this
215+ args_iter->next(lItem);
216 zorba::String lTmpString = lItem.getStringValue();
217 args_iter->close();
218 return lTmpString;
219 }
220
221+zorba::Item
222+HashModule::getItemArgument(
223+ const ExternalFunction::Arguments_t& aArgs,
224+ int aIndex)
225+{
226+ zorba::Item lItem;
227+ Iterator_t args_iter = aArgs[aIndex]->getIterator();
228+ args_iter->open();
229+ args_iter->next(lItem);
230+ args_iter->close();
231+ return lItem;
232+}
233+
234 HashModule::~HashModule()
235 {
236 for (FuncMap_t::const_iterator lIter = theFunctions.begin();
237@@ -65,63 +82,16 @@
238 delete this;
239 }
240
241-class HashFunction : public NonContextualExternalFunction
242-{
243-protected:
244- const HashModule* theModule;
245-
246-public:
247- HashFunction(const HashModule* aModule): theModule(aModule){}
248- ~HashFunction(){}
249-
250- virtual String
251- getLocalName() const { return "hash-impl"; }
252-
253- virtual zorba::ItemSequence_t
254- evaluate(const Arguments_t& aArgs) const
255- {
256- zorba::String lText = getOneStringArgument(theModule, aArgs, 0);
257- zorba::String lAlg = getOneStringArgument(theModule, aArgs, 1);
258- zorba::String lHash;
259- if (lAlg == "sha1") {
260- CSHA1 lSha1;
261- const unsigned char* lData = (const unsigned char*) lText.c_str();
262- lSha1.Update(lData, lText.size());
263- lSha1.Final();
264- char lRes[65];
265- lSha1.GetHash((UINT_8 *)lRes);
266-
267- // SHA1 is always 20bytes long
268- // avoid using a stream here because it might contain 0's
269- // (i.e. be null terminated)
270- std::stringstream lTmp;
271- lTmp.write(lRes, 20);
272-
273- lHash = zorba::encoding::Base64::encode(lTmp);
274- } else {
275- lHash = md5(lText.str());
276- }
277- // implement here
278- zorba::Item lItem;
279- lItem = theModule->getItemFactory()->createString(lHash);
280- return zorba::ItemSequence_t(new zorba::SingletonItemSequence(lItem));
281- }
282-
283- virtual String
284- getURI() const
285- {
286- return theModule->getURI();
287- }
288-
289-};
290-
291-ExternalFunction* HashModule::getExternalFunction(const
292+ExternalFunction*
293+HashModule::getExternalFunction(const
294 String& aLocalname)
295 {
296 ExternalFunction*& lFunc = theFunctions[aLocalname];
297 if (!lFunc) {
298- if (!aLocalname.compare("hash-impl")) {
299+ if (!aLocalname.compare("hash")) {
300 lFunc = new HashFunction(this);
301+ } else if (!aLocalname.compare("hash-binary")) {
302+ lFunc = new HashBinaryFunction(this);
303 }
304 }
305 return lFunc;
306@@ -129,6 +99,60 @@
307
308 ItemFactory* HashModule::theFactory = 0;
309
310+/******************************************************************************
311+ ******************************************************************************/
312+zorba::ItemSequence_t
313+HashModule::hash(const ExternalFunction::Arguments_t& aArgs) const
314+{
315+ zorba::Item lMessage = getItemArgument(aArgs, 0);
316+ zorba::String lAlg = getStringArgument(aArgs, 1);
317+
318+ bool lDecode = lMessage.getTypeCode() == store::XS_BASE64BINARY &&
319+ lMessage.isEncoded();
320+
321+ if (lAlg == "sha1" || lAlg == "SHA1")
322+ {
323+ return hash<SHA_CTX, SHA_DIGEST_LENGTH>
324+ (&SHA1_Init, &SHA1_Update, &SHA1_Final, &SHA1, lMessage, lDecode);
325+ }
326+ else if (lAlg == "sha256" || lAlg == "SHA256")
327+ {
328+ return hash<SHA256_CTX, SHA256_DIGEST_LENGTH>
329+ (&SHA256_Init, &SHA256_Update, &SHA256_Final, &SHA256, lMessage, lDecode);
330+ }
331+ else if (lAlg == "md5" || lAlg == "MD5")
332+ {
333+ return hash<MD5_CTX, MD5_DIGEST_LENGTH>
334+ (&MD5_Init, &MD5_Update, &MD5_Final, &MD5, lMessage, lDecode);
335+ }
336+ else
337+ {
338+ std::ostringstream lMsg;
339+ lMsg << lAlg << ": unsupported hash algorithm";
340+ throw USER_EXCEPTION(
341+ getItemFactory()->createQName(
342+ getURI(), "unsupported-algorithm"),
343+ lMsg.str());
344+ }
345+ return zorba::ItemSequence_t(new EmptySequence());
346+}
347+
348+/******************************************************************************
349+ ******************************************************************************/
350+zorba::ItemSequence_t
351+HashFunction::evaluate(const Arguments_t& aArgs) const
352+{
353+ return theModule->hash(aArgs);
354+}
355+
356+/******************************************************************************
357+ ******************************************************************************/
358+zorba::ItemSequence_t
359+HashBinaryFunction::evaluate(const Arguments_t& aArgs) const
360+{
361+ return theModule->hash(aArgs);
362+}
363+
364 } /* namespace security */
365 } /* namespace zorba */
366
367
368=== modified file 'src/hash.xq.src/hash.h'
369--- src/hash.xq.src/hash.h 2011-08-03 08:33:59 +0000
370+++ src/hash.xq.src/hash.h 2012-07-27 03:35:22 +0000
371@@ -22,6 +22,7 @@
372 #include <zorba/zorba.h>
373 #include <zorba/external_module.h>
374 #include <zorba/function.h>
375+#include <zorba/base64_stream.h>
376
377 namespace zorba { namespace security {
378
379@@ -33,7 +34,7 @@
380 protected:
381 class ltstr
382 {
383- public:
384+ public:
385 bool
386 operator()(const String& s1, const String& s2) const
387 {
388@@ -55,6 +56,12 @@
389
390 virtual void
391 destroy();
392+
393+ static String
394+ getStringArgument(const ExternalFunction::Arguments_t& aArgs, int aIndex);
395+
396+ static Item
397+ getItemArgument(const ExternalFunction::Arguments_t& aArgs, int aIndex);
398
399 static ItemFactory*
400 getItemFactory()
401@@ -63,36 +70,135 @@
402 theFactory = Zorba::getInstance(0)->getItemFactory();
403 return theFactory;
404 }
405-
406+
407+ zorba::ItemSequence_t
408+ hash(const ExternalFunction::Arguments_t& aArgs) const;
409+
410+ template <class CONTEXT, int DIGEST_LENGTH> zorba::ItemSequence_t
411+ hash(
412+ int(*init)(CONTEXT*),
413+ int(*update)(CONTEXT*, const void*, size_t),
414+ int(*final)(unsigned char*, CONTEXT*),
415+ unsigned char*(hash)(const unsigned char*, size_t, unsigned char*),
416+ zorba::Item& aMessage,
417+ bool aDecode = false
418+ ) const
419+ {
420+ unsigned char lBuf[DIGEST_LENGTH];
421+
422+ CONTEXT lCtx;
423+
424+ if (aMessage.isStreamable())
425+ {
426+ std::istream& lStream = aMessage.getStream();
427+
428+ bool lDecoderAttached = false;
429+
430+ if (aDecode)
431+ {
432+ base64::attach(lStream);
433+ lDecoderAttached = true;
434+ }
435+
436+ (*init)(&lCtx);
437+
438+ char lBuf2[1024];
439+ while (lStream.good())
440+ {
441+ lStream.read(lBuf2, 1024);
442+ (*update)(&lCtx, &lBuf2[0], lStream.gcount());
443+ }
444+
445+ if (lDecoderAttached)
446+ {
447+ base64::detach(lStream);
448+ }
449+
450+ (*final)(&lBuf[0], &lCtx);
451+ }
452+ else
453+ {
454+ if (aMessage.getTypeCode() == store::XS_BASE64BINARY)
455+ {
456+ String lTmpDecodedBuf;
457+ size_t lLen;
458+ const char* lTmp = aMessage.getBase64BinaryValue(lLen);
459+ if (aDecode)
460+ {
461+ String lTmpEncoded(lTmp, lLen);
462+ // lTmpDecodedBuf is used to make sure lMsg is still alive during HMAC_Update
463+ lTmpDecodedBuf = encoding::Base64::decode(lTmpEncoded);
464+ lTmp = lTmpDecodedBuf.c_str();
465+ lLen = lTmpDecodedBuf.size();
466+ }
467+ (*hash)(
468+ reinterpret_cast<const unsigned char*>(lTmp),
469+ lLen,
470+ &lBuf[0]
471+ );
472+ }
473+ else
474+ {
475+ String lTmp = aMessage.getStringValue();
476+ (*hash)(
477+ reinterpret_cast<const unsigned char*>(lTmp.data()),
478+ lTmp.size(),
479+ &lBuf[0]
480+ );
481+ }
482+ }
483+ return
484+ zorba::ItemSequence_t(new zorba::SingletonItemSequence(
485+ getItemFactory()->createBase64Binary(&lBuf[0], DIGEST_LENGTH)));
486+ }
487 };
488+
489+ class HashFunction : public NonContextualExternalFunction
490+ {
491+ protected:
492+ const HashModule* theModule;
493+
494+ public:
495+ HashFunction(const HashModule* aModule): theModule(aModule){}
496+ ~HashFunction(){}
497+
498+ virtual String
499+ getLocalName() const { return "hash"; }
500+
501+ virtual zorba::ItemSequence_t
502+ evaluate(const Arguments_t& aArgs) const;
503+
504+ virtual String
505+ getURI() const
506+ {
507+ return theModule->getURI();
508+ }
509+
510+ };
511+
512+ class HashBinaryFunction : public NonContextualExternalFunction
513+ {
514+ protected:
515+ const HashModule* theModule;
516+
517+ public:
518+ HashBinaryFunction(const HashModule* aModule): theModule(aModule){}
519+ ~HashBinaryFunction(){}
520+
521+ virtual String
522+ getLocalName() const { return "hash-binary"; }
523+
524+ virtual zorba::ItemSequence_t
525+ evaluate(const Arguments_t& aArgs) const;
526+
527+ virtual String
528+ getURI() const
529+ {
530+ return theModule->getURI();
531+ }
532+
533+ };
534
535- class HashSHA1Function : public NonContextualExternalFunction
536- {
537- protected:
538- const HashModule* theModule;
539-
540- static void
541- throwError(
542- const std::string aErrorMessage,
543- const Error& aErrorType);
544-
545- private:
546-
547- public:
548- HashSHA1Function(const HashModule* aModule): theModule(aModule){}
549- ~HashSHA1Function(){}
550-
551- virtual String
552- getLocalName() const { return "sha1"; }
553-
554- virtual zorba::ItemSequence_t
555- evaluate(const Arguments_t&) const;
556-
557- virtual String
558- getURI() const;
559-
560- };
561-
562 } /* namespace security */
563 } /* namespace zorba */
564
565
566=== removed file 'src/hash.xq.src/md5_impl.cpp'
567--- src/hash.xq.src/md5_impl.cpp 2011-08-02 08:51:47 +0000
568+++ src/hash.xq.src/md5_impl.cpp 1970-01-01 00:00:00 +0000
569@@ -1,371 +0,0 @@
570-/* MD5
571- converted to C++ class by Frank Thilo (thilo@unix-ag.org)
572- for bzflag (http://www.bzflag.org)
573-
574- based on:
575-
576- md5.h and md5.c
577- reference implemantion of RFC 1321
578-
579- Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
580-rights reserved.
581-
582-License to copy and use this software is granted provided that it
583-is identified as the "RSA Data Security, Inc. MD5 Message-Digest
584-Algorithm" in all material mentioning or referencing this software
585-or this function.
586-
587-License is also granted to make and use derivative works provided
588-that such works are identified as "derived from the RSA Data
589-Security, Inc. MD5 Message-Digest Algorithm" in all material
590-mentioning or referencing the derived work.
591-
592-RSA Data Security, Inc. makes no representations concerning either
593-the merchantability of this software or the suitability of this
594-software for any particular purpose. It is provided "as is"
595-without express or implied warranty of any kind.
596-
597-These notices must be retained in any copies of any part of this
598-documentation and/or software.
599-
600-*/
601-
602-/* system implementation headers */
603-#include <stdio.h>
604-#include <string.h>
605-
606-//md5 class include
607-#include "md5_impl.h"
608-
609-namespace zorba { namespace security {
610-
611-// Constants for MD5Transform routine.
612-#define S11 7
613-#define S12 12
614-#define S13 17
615-#define S14 22
616-#define S21 5
617-#define S22 9
618-#define S23 14
619-#define S24 20
620-#define S31 4
621-#define S32 11
622-#define S33 16
623-#define S34 23
624-#define S41 6
625-#define S42 10
626-#define S43 15
627-#define S44 21
628-
629-///////////////////////////////////////////////
630-
631-// F, G, H and I are basic MD5 functions.
632-inline MD5::uint4 MD5::F(uint4 x, uint4 y, uint4 z) {
633- return (x&y) | (~x&z);
634-}
635-
636-inline MD5::uint4 MD5::G(uint4 x, uint4 y, uint4 z) {
637- return (x&z) | (y&~z);
638-}
639-
640-inline MD5::uint4 MD5::H(uint4 x, uint4 y, uint4 z) {
641- return x^y^z;
642-}
643-
644-inline MD5::uint4 MD5::I(uint4 x, uint4 y, uint4 z) {
645- return y ^ (x | ~z);
646-}
647-
648-// rotate_left rotates x left n bits.
649-inline MD5::uint4 MD5::rotate_left(uint4 x, int n) {
650- return (x << n) | (x >> (32-n));
651-}
652-
653-// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
654-// Rotation is separate from addition to prevent recomputation.
655-inline void MD5::FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
656- a = rotate_left(a+ F(b,c,d) + x + ac, s) + b;
657-}
658-
659-inline void MD5::GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
660- a = rotate_left(a + G(b,c,d) + x + ac, s) + b;
661-}
662-
663-inline void MD5::HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
664- a = rotate_left(a + H(b,c,d) + x + ac, s) + b;
665-}
666-
667-inline void MD5::II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac) {
668- a = rotate_left(a + I(b,c,d) + x + ac, s) + b;
669-}
670-
671-//////////////////////////////////////////////
672-
673-// default ctor, just initailize
674-MD5::MD5()
675-{
676- init();
677-}
678-
679-//////////////////////////////////////////////
680-
681-// nifty shortcut ctor, compute MD5 for string and finalize it right away
682-MD5::MD5(const std::string &text)
683-{
684- init();
685- update(text.c_str(), text.length());
686- finalize();
687-}
688-
689-//////////////////////////////
690-
691-void MD5::init()
692-{
693- finalized=false;
694-
695- count[0] = 0;
696- count[1] = 0;
697-
698- // load magic initialization constants.
699- state[0] = 0x67452301;
700- state[1] = 0xefcdab89;
701- state[2] = 0x98badcfe;
702- state[3] = 0x10325476;
703-}
704-
705-//////////////////////////////
706-
707-// decodes input (unsigned char) into output (uint4). Assumes len is a multiple of 4.
708-void MD5::decode(uint4 output[], const uint1 input[], size_type len)
709-{
710- for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
711- output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
712- (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
713-}
714-
715-//////////////////////////////
716-
717-// encodes input (uint4) into output (unsigned char). Assumes len is
718-// a multiple of 4.
719-void MD5::encode(uint1 output[], const uint4 input[], size_type len)
720-{
721- for (size_type i = 0, j = 0; j < len; i++, j += 4) {
722- output[j] = input[i] & 0xff;
723- output[j+1] = (input[i] >> 8) & 0xff;
724- output[j+2] = (input[i] >> 16) & 0xff;
725- output[j+3] = (input[i] >> 24) & 0xff;
726- }
727-}
728-
729-//////////////////////////////
730-
731-// apply MD5 algo on a block
732-void MD5::transform(const uint1 block[blocksize])
733-{
734- uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
735- decode (x, block, blocksize);
736-
737- /* Round 1 */
738- FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
739- FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
740- FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
741- FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
742- FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
743- FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
744- FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
745- FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
746- FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
747- FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
748- FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
749- FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
750- FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
751- FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
752- FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
753- FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
754-
755- /* Round 2 */
756- GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
757- GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
758- GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
759- GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
760- GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
761- GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
762- GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
763- GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
764- GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
765- GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
766- GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
767- GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
768- GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
769- GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
770- GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
771- GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
772-
773- /* Round 3 */
774- HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
775- HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
776- HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
777- HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
778- HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
779- HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
780- HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
781- HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
782- HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
783- HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
784- HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
785- HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
786- HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
787- HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
788- HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
789- HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
790-
791- /* Round 4 */
792- II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
793- II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
794- II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
795- II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
796- II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
797- II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
798- II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
799- II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
800- II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
801- II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
802- II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
803- II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
804- II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
805- II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
806- II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
807- II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
808-
809- state[0] += a;
810- state[1] += b;
811- state[2] += c;
812- state[3] += d;
813-
814- // Zeroize sensitive information.
815- memset(x, 0, sizeof x);
816-}
817-
818-//////////////////////////////
819-
820-// MD5 block update operation. Continues an MD5 message-digest
821-// operation, processing another message block
822-void MD5::update(const unsigned char input[], size_type length)
823-{
824- // compute number of bytes mod 64
825- size_type index = count[0] / 8 % blocksize;
826-
827- // Update number of bits
828- if ((count[0] += (length << 3)) < (length << 3))
829- count[1]++;
830- count[1] += (length >> 29);
831-
832- // number of bytes we need to fill in buffer
833- size_type firstpart = 64 - index;
834-
835- size_type i;
836-
837- // transform as many times as possible.
838- if (length >= firstpart)
839- {
840- // fill buffer first, transform
841- memcpy(&buffer[index], input, firstpart);
842- transform(buffer);
843-
844- // transform chunks of blocksize (64 bytes)
845- for (i = firstpart; i + blocksize <= length; i += blocksize)
846- transform(&input[i]);
847-
848- index = 0;
849- }
850- else
851- i = 0;
852-
853- // buffer remaining input
854- memcpy(&buffer[index], &input[i], length-i);
855-}
856-
857-//////////////////////////////
858-
859-// for convenience provide a verson with signed char
860-void MD5::update(const char input[], size_type length)
861-{
862- update((const unsigned char*)input, length);
863-}
864-
865-//////////////////////////////
866-
867-// MD5 finalization. Ends an MD5 message-digest operation, writing the
868-// the message digest and zeroizing the context.
869-MD5& MD5::finalize()
870-{
871- static unsigned char padding[64] = {
872- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
873- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
874- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
875- };
876-
877- if (!finalized) {
878- // Save number of bits
879- unsigned char bits[8];
880- encode(bits, count, 8);
881-
882- // pad out to 56 mod 64.
883- size_type index = count[0] / 8 % 64;
884- size_type padLen = (index < 56) ? (56 - index) : (120 - index);
885- update(padding, padLen);
886-
887- // Append length (before padding)
888- update(bits, 8);
889-
890- // Store state in digest
891- encode(digest, state, 16);
892-
893- // Zeroize sensitive information.
894- memset(buffer, 0, sizeof buffer);
895- memset(count, 0, sizeof count);
896-
897- finalized=true;
898- }
899-
900- return *this;
901-}
902-
903-//////////////////////////////
904-
905-// return hex representation of digest as string
906-std::string MD5::hexdigest() const
907-{
908- if (!finalized)
909- return "";
910-
911- char buf[33];
912- for (int i=0; i<16; i++)
913- sprintf(buf+i*2, "%02x", digest[i]);
914- buf[32]=0;
915-
916- return std::string(buf);
917-}
918-
919-//////////////////////////////
920-
921-std::ostream& operator<<(std::ostream& out, MD5 md5)
922-{
923- return out << md5.hexdigest();
924-}
925-
926-//////////////////////////////
927-
928-std::string md5(const std::string str)
929-{
930- MD5 md5 = MD5(str);
931-
932- return md5.hexdigest();
933-}
934-
935-} // namespace zorba
936-} // namespace security
937-
938-/*
939- * EOF
940- */
941
942=== removed file 'src/hash.xq.src/md5_impl.h'
943--- src/hash.xq.src/md5_impl.h 2011-08-02 08:51:47 +0000
944+++ src/hash.xq.src/md5_impl.h 1970-01-01 00:00:00 +0000
945@@ -1,93 +0,0 @@
946-/* MD5
947- converted to C++ class by Frank Thilo (thilo@unix-ag.org)
948- for bzflag (http://www.bzflag.org)
949-
950- based on:
951-
952- md5.h and md5.c
953- reference implementation of RFC 1321
954-
955- Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
956-rights reserved.
957-
958-License to copy and use this software is granted provided that it
959-is identified as the "RSA Data Security, Inc. MD5 Message-Digest
960-Algorithm" in all material mentioning or referencing this software
961-or this function.
962-
963-License is also granted to make and use derivative works provided
964-that such works are identified as "derived from the RSA Data
965-Security, Inc. MD5 Message-Digest Algorithm" in all material
966-mentioning or referencing the derived work.
967-
968-RSA Data Security, Inc. makes no representations concerning either
969-the merchantability of this software or the suitability of this
970-software for any particular purpose. It is provided "as is"
971-without express or implied warranty of any kind.
972-
973-These notices must be retained in any copies of any part of this
974-documentation and/or software.
975-
976-*/
977-
978-//----------------------------------------------------------------------
979-//include protection
980-#ifndef SAUSALITO_SECURITYMODULE_MD5_IMPL_H
981-#define SAUSALITO_SECURITYMODULE_MD5_IMPL_H
982-
983-//----------------------------------------------------------------------
984-//STL includes
985-#include <string>
986-#include <iostream>
987-
988-namespace zorba { namespace security {
989-
990- class MD5
991- {
992- public:
993- typedef unsigned int size_type; // must be 32bit
994-
995- MD5();
996- MD5(const std::string& text);
997- void update(const unsigned char *buf, size_type length);
998- void update(const char *buf, size_type length);
999- MD5& finalize();
1000- std::string hexdigest() const;
1001- friend std::ostream& operator<<(std::ostream&, MD5 md5);
1002-
1003- private:
1004- void init();
1005- typedef unsigned char uint1; // 8bit
1006- typedef unsigned int uint4; // 32bit
1007- enum {blocksize = 64}; // VC6 won't eat a const static int here
1008-
1009- void transform(const uint1 block[blocksize]);
1010- static void decode(uint4 output[], const uint1 input[], size_type len);
1011- static void encode(uint1 output[], const uint4 input[], size_type len);
1012-
1013- bool finalized;
1014- uint1 buffer[blocksize]; // bytes that didn't fit in last 64 byte chunk
1015- uint4 count[2]; // 64bit counter for number of bits (lo, hi)
1016- uint4 state[4]; // digest so far
1017- uint1 digest[16]; // the result
1018-
1019- // low level logic operations
1020- static inline uint4 F(uint4 x, uint4 y, uint4 z);
1021- static inline uint4 G(uint4 x, uint4 y, uint4 z);
1022- static inline uint4 H(uint4 x, uint4 y, uint4 z);
1023- static inline uint4 I(uint4 x, uint4 y, uint4 z);
1024- static inline uint4 rotate_left(uint4 x, int n);
1025- static inline void FF(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
1026- static inline void GG(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
1027- static inline void HH(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
1028- static inline void II(uint4 &a, uint4 b, uint4 c, uint4 d, uint4 x, uint4 s, uint4 ac);
1029- };
1030-
1031- std::string md5(const std::string str);
1032-
1033-
1034-} // namespace zorba
1035-} // namespace security
1036-//----------------------------------------------------------------------
1037-//End of include protection
1038-#endif
1039
1040=== removed file 'src/hash.xq.src/sha1.cpp'
1041--- src/hash.xq.src/sha1.cpp 2011-08-02 08:51:47 +0000
1042+++ src/hash.xq.src/sha1.cpp 1970-01-01 00:00:00 +0000
1043@@ -1,264 +0,0 @@
1044-/*
1045- 100% free public domain implementation of the SHA-1 algorithm
1046- by Dominik Reichl <dominik.reichl@t-online.de>
1047- Web: http://www.dominik-reichl.de/
1048-
1049- See header file for version history.
1050-*/
1051-
1052-// If compiling with MFC, you might want to add #include "StdAfx.h"
1053-
1054-#define _CRT_SECURE_NO_WARNINGS
1055-//#include "security_sha1.h"
1056-#include "sha1.h"
1057-
1058-#ifdef SHA1_UTILITY_FUNCTIONS
1059-#define SHA1_MAX_FILE_BUFFER 8000
1060-#endif
1061-
1062-// Rotate x bits to the left
1063-#ifndef ROL32
1064-#ifdef _MSC_VER
1065-#define ROL32(_val32,_nBits) _rotl(_val32,_nBits)
1066-#else
1067-#define ROL32(_val32,_nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
1068-#endif
1069-#endif
1070-
1071-#ifdef SHA1_LITTLE_ENDIAN
1072-#define SHABLK0(i) (m_block->l[i] = \
1073- (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
1074-#else
1075-#define SHABLK0(i) (m_block->l[i])
1076-#endif
1077-
1078-#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
1079- ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
1080-
1081-// SHA-1 rounds
1082-#define _R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
1083-#define _R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
1084-#define _R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
1085-#define _R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);}
1086-#define _R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
1087-
1088-namespace zorba { namespace security {
1089-
1090-CSHA1::CSHA1()
1091-{
1092- m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace;
1093-
1094- Reset();
1095-}
1096-
1097-CSHA1::~CSHA1()
1098-{
1099- Reset();
1100-}
1101-
1102-void CSHA1::Reset()
1103-{
1104- // SHA1 initialization constants
1105- m_state[0] = 0x67452301;
1106- m_state[1] = 0xEFCDAB89;
1107- m_state[2] = 0x98BADCFE;
1108- m_state[3] = 0x10325476;
1109- m_state[4] = 0xC3D2E1F0;
1110-
1111- m_count[0] = 0;
1112- m_count[1] = 0;
1113-}
1114-
1115-void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer)
1116-{
1117- UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
1118-
1119- memcpy(m_block, pBuffer, 64);
1120-
1121- // 4 rounds of 20 operations each. Loop unrolled.
1122- _R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
1123- _R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
1124- _R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
1125- _R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
1126- _R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
1127- _R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
1128- _R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
1129- _R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
1130- _R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
1131- _R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
1132- _R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
1133- _R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
1134- _R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
1135- _R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
1136- _R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
1137- _R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
1138- _R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
1139- _R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
1140- _R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
1141- _R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
1142-
1143- // Add the working vars back into state
1144- pState[0] += a;
1145- pState[1] += b;
1146- pState[2] += c;
1147- pState[3] += d;
1148- pState[4] += e;
1149-
1150- // Wipe variables
1151-#ifdef SHA1_WIPE_VARIABLES
1152- a = b = c = d = e = 0;
1153-#endif
1154-}
1155-
1156-// Use this function to hash in binary data and strings
1157-void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
1158-{
1159- UINT_32 j = ((m_count[0] >> 3) & 0x3F);
1160-
1161- if((m_count[0] += (uLen << 3)) < (uLen << 3))
1162- ++m_count[1]; // Overflow
1163-
1164- m_count[1] += (uLen >> 29);
1165-
1166- UINT_32 i;
1167- if((j + uLen) > 63)
1168- {
1169- i = 64 - j;
1170- memcpy(&m_buffer[j], pbData, i);
1171- Transform(m_state, m_buffer);
1172-
1173- for( ; (i + 63) < uLen; i += 64)
1174- Transform(m_state, &pbData[i]);
1175-
1176- j = 0;
1177- }
1178- else i = 0;
1179-
1180- if((uLen - i) != 0)
1181- memcpy(&m_buffer[j], &pbData[i], uLen - i);
1182-}
1183-
1184-#ifdef SHA1_UTILITY_FUNCTIONS
1185-// Hash in file contents
1186-bool CSHA1::HashFile(const TCHAR* tszFileName)
1187-{
1188- if(tszFileName == NULL) return false;
1189-
1190- FILE* fpIn = _tfopen(tszFileName, _T("rb"));
1191- if(fpIn == NULL) return false;
1192-
1193- _fseeki64(fpIn, 0, SEEK_END);
1194- const INT_64 lFileSize = _ftelli64(fpIn);
1195- _fseeki64(fpIn, 0, SEEK_SET);
1196-
1197- const INT_64 lMaxBuf = SHA1_MAX_FILE_BUFFER;
1198- UINT_8 vData[SHA1_MAX_FILE_BUFFER];
1199- INT_64 lRemaining = lFileSize;
1200-
1201- while(lRemaining > 0)
1202- {
1203- const size_t uMaxRead = static_cast<size_t>((lRemaining > lMaxBuf) ?
1204- lMaxBuf : lRemaining);
1205-
1206- const size_t uRead = fread(vData, 1, uMaxRead, fpIn);
1207- if(uRead == 0)
1208- {
1209- fclose(fpIn);
1210- return false;
1211- }
1212-
1213- Update(vData, static_cast<UINT_32>(uRead));
1214-
1215- lRemaining -= static_cast<INT_64>(uRead);
1216- }
1217-
1218- fclose(fpIn);
1219- return (lRemaining == 0);
1220-}
1221-#endif
1222-
1223-void CSHA1::Final()
1224-{
1225- UINT_32 i;
1226-
1227- UINT_8 finalcount[8];
1228- for(i = 0; i < 8; ++i)
1229- finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
1230- >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
1231-
1232- Update((UINT_8*)"\200", 1);
1233-
1234- while ((m_count[0] & 504) != 448)
1235- Update((UINT_8*)"\0", 1);
1236-
1237- Update(finalcount, 8); // Cause a SHA1Transform()
1238-
1239- for(i = 0; i < 20; ++i)
1240- m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 0xFF);
1241-
1242- // Wipe variables for security reasons
1243-#ifdef SHA1_WIPE_VARIABLES
1244- memset(m_buffer, 0, 64);
1245- memset(m_state, 0, 20);
1246- memset(m_count, 0, 8);
1247- memset(finalcount, 0, 8);
1248- Transform(m_state, m_buffer);
1249-#endif
1250-}
1251-
1252-#ifdef SHA1_UTILITY_FUNCTIONS
1253-// Get the final hash as a pre-formatted string
1254-bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const
1255-{
1256- if(tszReport == NULL) return false;
1257-
1258- TCHAR tszTemp[16];
1259-
1260- if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT))
1261- {
1262- _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]);
1263- _tcscpy(tszReport, tszTemp);
1264-
1265- const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X"));
1266- for(size_t i = 1; i < 20; ++i)
1267- {
1268- _sntprintf(tszTemp, 15, lpFmt, m_digest[i]);
1269- _tcscat(tszReport, tszTemp);
1270- }
1271- }
1272- else if(rtReportType == REPORT_DIGIT)
1273- {
1274- _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]);
1275- _tcscpy(tszReport, tszTemp);
1276-
1277- for(size_t i = 1; i < 20; ++i)
1278- {
1279- _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]);
1280- _tcscat(tszReport, tszTemp);
1281- }
1282- }
1283- else return false;
1284-
1285- return true;
1286-}
1287-#endif
1288-
1289-#ifdef SHA1_STL_FUNCTIONS
1290-bool CSHA1::ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType) const
1291-{
1292- TCHAR tszOut[84];
1293- const bool bResult = ReportHash(tszOut, rtReportType);
1294- if(bResult) strOut = tszOut;
1295- return bResult;
1296-}
1297-#endif
1298-
1299-// Get the raw message digest
1300-bool CSHA1::GetHash(UINT_8* pbDest) const
1301-{
1302- if(pbDest == NULL) return false;
1303- memcpy(pbDest, m_digest, 20);
1304- return true;
1305-}
1306-
1307-}}
1308
1309=== removed file 'src/hash.xq.src/sha1.h'
1310--- src/hash.xq.src/sha1.h 2011-08-02 08:51:47 +0000
1311+++ src/hash.xq.src/sha1.h 1970-01-01 00:00:00 +0000
1312@@ -1,259 +0,0 @@
1313-/*
1314- 100% free public domain implementation of the SHA-1 algorithm
1315- by Dominik Reichl <dominik.reichl@t-online.de>
1316- Web: http://www.dominik-reichl.de/
1317-
1318- Version 1.8 - 2008-03-16
1319- - Converted project files to Visual Studio 2008 format.
1320- - Added Unicode support for HashFile utility method.
1321- - Added support for hashing files using the HashFile method that are
1322- larger than 2 GB.
1323- - HashFile now returns an error code instead of copying an error
1324- message into the output buffer.
1325- - GetHash now returns an error code and validates the input parameter.
1326- - Added ReportHashStl STL utility method.
1327- - Added REPORT_HEX_SHORT reporting mode.
1328- - Improved Linux compatibility of test program.
1329-
1330- Version 1.7 - 2006-12-21
1331- - Fixed buffer underrun warning that appeared when compiling with
1332- Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the
1333- patch).
1334- - Breaking change: ReportHash writes the final hash to the start
1335- of the buffer, i.e. it's not appending it to the string anymore.
1336- - Made some function parameters const.
1337- - Added Visual Studio 2005 project files to demo project.
1338-
1339- Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
1340- - You can set the endianness in your files, no need to modify the
1341- header file of the CSHA1 class anymore.
1342- - Aligned data support.
1343- - Made support/compilation of the utility functions (ReportHash and
1344- HashFile) optional (useful when bytes count, for example in embedded
1345- environments).
1346-
1347- Version 1.5 - 2005-01-01
1348- - 64-bit compiler compatibility added.
1349- - Made variable wiping optional (define SHA1_WIPE_VARIABLES).
1350- - Removed unnecessary variable initializations.
1351- - ROL32 improvement for the Microsoft compiler (using _rotl).
1352-
1353- Version 1.4 - 2004-07-22
1354- - CSHA1 now compiles fine with GCC 3.3 under MacOS X (thanks to Larry
1355- Hastings).
1356-
1357- Version 1.3 - 2003-08-17
1358- - Fixed a small memory bug and made a buffer array a class member to
1359- ensure correct working when using multiple CSHA1 class instances at
1360- one time.
1361-
1362- Version 1.2 - 2002-11-16
1363- - Borlands C++ compiler seems to have problems with string addition
1364- using sprintf. Fixed the bug which caused the digest report function
1365- not to work properly. CSHA1 is now Borland compatible.
1366-
1367- Version 1.1 - 2002-10-11
1368- - Removed two unnecessary header file includes and changed BOOL to
1369- bool. Fixed some minor bugs in the web page contents.
1370-
1371- Version 1.0 - 2002-06-20
1372- - First official release.
1373-
1374- ======== Test Vectors (from FIPS PUB 180-1) ========
1375-
1376- SHA1("abc") =
1377- A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
1378-
1379- SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
1380- 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
1381-
1382- SHA1(A million repetitions of "a") =
1383- 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
1384-*/
1385-
1386-#ifndef SAUSA_UTILS_SHA1_HDR
1387-#define SAUSA_UTILS_SHA1_HDR
1388-
1389-#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
1390-#define SHA1_UTILITY_FUNCTIONS
1391-#endif
1392-
1393-#if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
1394-#define SHA1_STL_FUNCTIONS
1395-#if !defined(SHA1_UTILITY_FUNCTIONS)
1396-#error STL functions require SHA1_UTILITY_FUNCTIONS.
1397-#endif
1398-#endif
1399-
1400-#include <memory.h>
1401-
1402-#ifdef SHA1_UTILITY_FUNCTIONS
1403-#include <stdio.h>
1404-#include <string.h>
1405-#endif
1406-
1407-#ifdef SHA1_STL_FUNCTIONS
1408-#include <string>
1409-#endif
1410-
1411-#ifdef _MSC_VER
1412-#include <stdlib.h>
1413-#endif
1414-
1415-// You can define the endian mode in your files without modifying the SHA-1
1416-// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
1417-// in your files, before including the SHA1.h header file. If you don't
1418-// define anything, the class defaults to little endian.
1419-#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
1420-#define SHA1_LITTLE_ENDIAN
1421-#endif
1422-
1423-// If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
1424-// #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
1425-// defaults to wiping.
1426-#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
1427-#define SHA1_WIPE_VARIABLES
1428-#endif
1429-
1430-#if defined(SHA1_HAS_TCHAR)
1431-#include <tchar.h>
1432-#else
1433-#ifdef _MSC_VER
1434-#include <tchar.h>
1435-#else
1436-#ifndef TCHAR
1437-#define TCHAR char
1438-#endif
1439-#ifndef _T
1440-#define _T(__x) (__x)
1441-#define _tmain main
1442-#define _tprintf printf
1443-#define _getts gets
1444-#define _tcslen strlen
1445-#define _tfopen fopen
1446-#define _tcscpy strcpy
1447-#define _tcscat strcat
1448-#define _sntprintf snprintf
1449-#endif
1450-#endif
1451-#endif
1452-
1453-// Fallback, if no 64-bit support
1454-#ifndef _fseeki64
1455-#define _fseeki64 fseek
1456-#endif
1457-#ifndef _ftelli64
1458-#define _ftelli64 ftell
1459-#endif
1460-
1461-///////////////////////////////////////////////////////////////////////////
1462-// Define variable types
1463-
1464-#ifndef UINT_8
1465-#ifdef _MSC_VER // Compiling with Microsoft compiler
1466-#define UINT_8 unsigned __int8
1467-#else // !_MSC_VER
1468-#define UINT_8 unsigned char
1469-#endif // _MSC_VER
1470-#endif
1471-
1472-#ifndef UINT_32
1473-#ifdef _MSC_VER // Compiling with Microsoft compiler
1474-#define UINT_32 unsigned __int32
1475-#else // !_MSC_VER
1476-#if (ULONG_MAX == 0xFFFFFFFF)
1477-#define UINT_32 unsigned long
1478-#else
1479-#define UINT_32 unsigned int
1480-#endif
1481-#endif // _MSC_VER
1482-#endif // UINT_32
1483-
1484-#ifndef INT_64
1485-#ifdef _MSC_VER // Compiling with Microsoft compiler
1486-#define INT_64 __int64
1487-#else // !_MSC_VER
1488-#define INT_64 long long
1489-#endif // _MSC_VER
1490-#endif // INT_64
1491-
1492-#ifndef UINT_64
1493-#ifdef _MSC_VER // Compiling with Microsoft compiler
1494-#define UINT_64 unsigned __int64
1495-#else // !_MSC_VER
1496-#define UINT_64 unsigned long long
1497-#endif // _MSC_VER
1498-#endif // UINT_64
1499-
1500-///////////////////////////////////////////////////////////////////////////
1501-// Declare SHA-1 workspace
1502-
1503-namespace zorba { namespace security {
1504-
1505-typedef union
1506-{
1507- UINT_8 c[64];
1508- UINT_32 l[16];
1509-} SHA1_WORKSPACE_BLOCK;
1510-
1511-class CSHA1
1512-{
1513-public:
1514-#ifdef SHA1_UTILITY_FUNCTIONS
1515- // Different formats for ReportHash
1516- enum REPORT_TYPE
1517- {
1518- REPORT_HEX = 0,
1519- REPORT_DIGIT = 1,
1520- REPORT_HEX_SHORT = 2
1521- };
1522-#endif
1523-
1524- // Constructor and destructor
1525- CSHA1();
1526- ~CSHA1();
1527-
1528- UINT_32 m_state[5];
1529- UINT_32 m_count[2];
1530- UINT_32 m_reserved0[1]; // Memory alignment padding
1531- UINT_8 m_buffer[64];
1532- UINT_8 m_digest[20];
1533- UINT_32 m_reserved1[3]; // Memory alignment padding
1534-
1535- void Reset();
1536-
1537- // Update the hash value
1538- void Update(const UINT_8* pbData, UINT_32 uLen);
1539-
1540-#ifdef SHA1_UTILITY_FUNCTIONS
1541- // Hash in file contents
1542- bool HashFile(const TCHAR* tszFileName);
1543-#endif
1544-
1545- // Finalize hash, call before using ReportHash(Stl)
1546- void Final();
1547-
1548-#ifdef SHA1_UTILITY_FUNCTIONS
1549- bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
1550-#endif
1551-
1552-#ifdef SHA1_STL_FUNCTIONS
1553- bool ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType =
1554- REPORT_HEX) const;
1555-#endif
1556-
1557- bool GetHash(UINT_8* pbDest) const;
1558-
1559-private:
1560- // Private SHA-1 transformation
1561- void Transform(UINT_32* pState, const UINT_8* pBuffer);
1562-
1563- // Member variables
1564- UINT_8 m_workspace[64];
1565- SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
1566-};
1567-
1568-}}//end of namespace
1569-
1570-#endif // ZORBA_SECURITY_MODULE_SHA1_HDR
1571-
1572
1573=== modified file 'src/hmac.xq'
1574--- src/hmac.xq 2011-08-13 00:08:45 +0000
1575+++ src/hmac.xq 2012-07-27 03:35:22 +0000
1576@@ -1,7 +1,7 @@
1577 xquery version "1.0";
1578
1579 (:
1580- : Copyright 2006-2009 The FLWOR Foundation.
1581+ : Copyright 2006-2012 The FLWOR Foundation.
1582 :
1583 : Licensed under the Apache License, Version 2.0 (the "License");
1584 : you may not use this file except in compliance with the License.
1585@@ -17,28 +17,129 @@
1586 :)
1587
1588 (:~
1589- : This module provides a function to generate hash-based
1590- : message authentication codes (HMAC) involving a cryptographic
1591- : hash function (e.g. SHA1).
1592+ : This module provides functions that perform HMAC
1593+ : (hash-based message authentication code) operations.
1594+ : For example, they calculate message codes involving hash functions such
1595+ : as MD5 and various SHA variants. The result is the base64 encoded value
1596+ : of the hash. A hash may be used to verify the data integrity and
1597+ : the authenticity of a message.
1598 :
1599- : @author William Candillon
1600+ : @author William Candillon, Matthias Brantner
1601 : @project cryptography
1602 :
1603 :)
1604 module namespace hmac = "http://www.zorba-xquery.com/modules/cryptography/hmac";
1605
1606 declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
1607-declare option ver:module-version "1.0";
1608-
1609-(:~
1610- : This function provides hash-based message authentication code using
1611- : the SHA1 algorithm.
1612+declare option ver:module-version "2.0";
1613+
1614+(:~
1615+ : Calculate the HMAC for the given message and secret-key involving
1616+ : the MD5 hash function.
1617+ :
1618+ : @param $message the message to be authenticated
1619+ : @param $secret-key the secret key used for calculating the authentication
1620+ :
1621+ : @return the base64 encoded message authentication code
1622+ :)
1623+declare function hmac:md5(
1624+ $message as xs:string,
1625+ $secret-key as xs:string
1626+) as xs:base64Binary
1627+{
1628+ hmac:compute($message, $secret-key, "md5")
1629+};
1630+
1631+(:~
1632+ : Calculate the HMAC for the given message and secret-key involving
1633+ : the SHA1 hash function.
1634+ :
1635+ : @param $message the message to be authenticated
1636+ : @param $secret-key the secret key used for calculating the authentication
1637+ :
1638+ : @return the base64 encoded message authentication code
1639+ :)
1640+declare function hmac:sha1(
1641+ $message as xs:string,
1642+ $secret-key as xs:string
1643+) as xs:base64Binary
1644+{
1645+ hmac:compute($message, $secret-key, "sha1")
1646+};
1647+
1648+(:~
1649+ : Calculate the HMAC for the given message and secret-key involving
1650+ : the MD5 hash function. Before calculating the code, the given
1651+ : base64-encoded message is base64-decoded.
1652+ :
1653+ : @param $message the message to be authenticated
1654+ : @param $secret-key the secret key used for calculating the authentication
1655+ :
1656+ : @return the base64 encoded message authentication code
1657+ :)
1658+declare function hmac:md5-binary(
1659+ $message as xs:base64Binary,
1660+ $secret-key as xs:string
1661+) as xs:base64Binary
1662+{
1663+ hmac:compute-binary($message, $secret-key, "md5")
1664+};
1665+
1666+(:~
1667+ : Calculate the HMAC for the given message and secret-key involving
1668+ : the SHA1 hash function. Before calculating the code, the given
1669+ : base64-encoded message is base64-decoded.
1670 :
1671 : @param $message the message to be authenticated
1672 : @param $secret-key the secret key used for calculating the authentication
1673- : @return hash-based base64 encoded message authentication code
1674- :)
1675-declare function hmac:sha1(
1676+ :
1677+ : @return the base64 encoded message authentication code
1678+ :)
1679+declare function hmac:sha1-binary(
1680+ $message as xs:base64Binary,
1681+ $secret-key as xs:string
1682+) as xs:base64Binary
1683+{
1684+ hmac:compute-binary($message, $secret-key, "sha1")
1685+};
1686+
1687+(:~
1688+ : Calculate the HMAC for the given message and secret-key involving
1689+ : an custom hash function.
1690+ :
1691+ : @param $message the message to be authenticated
1692+ : @param $secret-key the secret key used for calculating the authentication
1693+ : @param $alg The algorithm to use for the hashing operation. Supported
1694+ : algorithms are "md5", "sha1", and "sha256".
1695+ :
1696+ : @return the base64 encoded message authentication code
1697+ :
1698+ : @error hash:unsupported-algorithm if the given hash algorithm is not
1699+ : supported
1700+ :)
1701+declare function hmac:compute(
1702 $message as xs:string,
1703- $secret-key as xs:string
1704-) as xs:string external;
1705+ $secret-key as xs:string,
1706+ $alg as xs:string
1707+) as xs:base64Binary external;
1708+
1709+(:~
1710+ : Calculate the HMAC for the given message and secret-key involving
1711+ : an custom hash function. Before calculating the code, the given
1712+ : base64-encoded message is base64-decoded.
1713+ :
1714+ : @param $message the message to be authenticated
1715+ : @param $secret-key the secret key used for calculating the authentication
1716+ : @param $alg The algorithm to use for the hashing operation. Supported
1717+ : algorithms are "md5", "sha1", and "sha256".
1718+ :
1719+ : @return the base64 encoded message authentication code
1720+ :
1721+ : @error hash:unsupported-algorithm if the given hash algorithm is not
1722+ : supported
1723+ :)
1724+declare function hmac:compute-binary(
1725+ $message as xs:base64Binary,
1726+ $secret-key as xs:string,
1727+ $hash-algo as xs:string
1728+) as xs:base64Binary external;
1729
1730=== modified file 'src/hmac.xq.src/hmac.cpp'
1731--- src/hmac.xq.src/hmac.cpp 2011-08-13 00:08:45 +0000
1732+++ src/hmac.xq.src/hmac.cpp 2012-07-27 03:35:22 +0000
1733@@ -17,29 +17,39 @@
1734 #include "hmac.h"
1735
1736 #include <sstream>
1737-
1738 #include <zorba/base64.h>
1739+#include <zorba/base64_stream.h>
1740 #include <zorba/diagnostic_list.h>
1741 #include <zorba/item_factory.h>
1742 #include <zorba/singleton_item_sequence.h>
1743 #include <zorba/user_exception.h>
1744
1745-#include "hmac_sha1.h"
1746+#include <openssl/hmac.h>
1747
1748 namespace zorba { namespace security {
1749
1750- ItemFactory* HMACModule::theFactory = 0;
1751-
1752- zorba::String getOneStringArgument(const ExternalFunction::Arguments_t& aArgs, int aIndex)
1753- {
1754- zorba::Item lItem;
1755- Iterator_t args_iter = aArgs[aIndex]->getIterator();
1756- args_iter->open();
1757- args_iter->next(lItem); // must have one because the signature is defined like this
1758- zorba::String lTmpString = lItem.getStringValue();
1759- args_iter->close();
1760- return lTmpString;
1761- }
1762+ItemFactory* HMACModule::theFactory = 0;
1763+
1764+zorba::Item
1765+getOneItemArgument(
1766+ const ExternalFunction::Arguments_t& aArgs,
1767+ int aIndex)
1768+{
1769+ zorba::Item lItem;
1770+ Iterator_t args_iter = aArgs[aIndex]->getIterator();
1771+ args_iter->open();
1772+ args_iter->next(lItem);
1773+ return lItem;
1774+}
1775+
1776+zorba::String
1777+getOneStringArgument(
1778+ const ExternalFunction::Arguments_t& aArgs,
1779+ int aIndex)
1780+{
1781+ zorba::Item lItem = getOneItemArgument(aArgs, aIndex);
1782+ return lItem.getStringValue();
1783+}
1784
1785
1786 HMACModule::~HMACModule()
1787@@ -55,9 +65,14 @@
1788 HMACModule::getExternalFunction(const String& aLocalname)
1789 {
1790 ExternalFunction*& lFunc = theFunctions[aLocalname];
1791- if (!lFunc) {
1792- if (!aLocalname.compare("sha1")) {
1793- lFunc = new HMACSHA1Function(this);
1794+ if (!lFunc)
1795+ {
1796+ if (!aLocalname.compare("compute"))
1797+ {
1798+ lFunc = new HMACComputeFunction(this);
1799+ }
1800+ else if (!aLocalname.compare("compute-binary")) {
1801+ lFunc = new HMACComputeBinaryFunction(this);
1802 }
1803 }
1804 return lFunc;
1805@@ -72,43 +87,150 @@
1806 delete this;
1807 }
1808
1809-std::string
1810-hmacSHA1(const std::string& aData, const std::string& aKey)
1811-{
1812- unsigned char digest[20] ;
1813-
1814- char *message = new char[aData.length() + 1] ;
1815- memset(message, aData.length() + 1, 0);
1816- strcpy(message, aData.c_str());
1817-
1818- char *key = new char[aKey.length() + 1];
1819- memset(key, aKey.length() + 1, 0);
1820- strcpy(key, aKey.c_str());
1821-
1822- zorba::securitymodule::CHMAC_SHA1 HMAC_SHA1 ;
1823- HMAC_SHA1.HMAC_SHA1((unsigned char*) message, strlen(message), (unsigned char*)key, strlen(key), digest) ;
1824-
1825- zorba::String lDigest(std::string((char const*) digest, 20));
1826- zorba::String lSignature = zorba::encoding::Base64::encode(lDigest);
1827- return lSignature.c_str(); // uri-encoding is done automatically within zorba rest call
1828-}
1829-
1830-String
1831-HMACSHA1Function::getURI() const
1832-{
1833- return theModule->getURI();
1834-}
1835-
1836-zorba::ItemSequence_t
1837-HMACSHA1Function::evaluate(const Arguments_t& aArgs) const
1838-{
1839- zorba::Item lItem;
1840-
1841- std::string lBaseString = (getOneStringArgument(aArgs, 0)).c_str();
1842- std::string lKey = (getOneStringArgument(aArgs, 1)).c_str();
1843- lItem = theModule->getItemFactory()->createString(hmacSHA1(lBaseString, lKey));
1844- return zorba::ItemSequence_t(new zorba::SingletonItemSequence(lItem));
1845-}
1846+String
1847+HMACComputeFunction::getURI() const
1848+{
1849+ return theModule->getURI();
1850+}
1851+
1852+String
1853+HMACComputeBinaryFunction::getURI() const
1854+{
1855+ return theModule->getURI();
1856+}
1857+
1858+static void
1859+initContext(HMAC_CTX* aCtx, const String& aKey, const String& aAlg)
1860+{
1861+ if (aAlg == "sha1" || aAlg == "SHA1")
1862+ {
1863+ HMAC_Init(aCtx, aKey.c_str(), aKey.length(), EVP_sha1());
1864+ }
1865+ else if (aAlg == "sha256" || aAlg == "SHA256")
1866+ {
1867+ HMAC_Init(aCtx, aKey.c_str(), aKey.length(), EVP_sha256());
1868+ }
1869+ else if (aAlg == "md5" || aAlg == "MD5")
1870+ {
1871+ HMAC_Init(aCtx, aKey.c_str(), aKey.length(), EVP_md5());
1872+ }
1873+ else
1874+ {
1875+ std::ostringstream lMsg;
1876+ lMsg << aAlg << ": unsupported hash algorithm";
1877+ throw USER_EXCEPTION(
1878+ HMACModule::getItemFactory()->createQName(
1879+ "http://www.zorba-xquery.com/modules/cryptography/hmac", "unsupported-algorithm"),
1880+ lMsg.str());
1881+ }
1882+}
1883+
1884+zorba::ItemSequence_t
1885+HMACComputeFunction::evaluate(const Arguments_t& aArgs) const
1886+{
1887+ zorba::Item lItem = getOneItemArgument(aArgs, 0);
1888+
1889+ String lKey = getOneStringArgument(aArgs, 1);
1890+ String lAlg = getOneStringArgument(aArgs, 2);
1891+
1892+ HMAC_CTX ctx;
1893+ unsigned int len;
1894+ unsigned char out[32]; // reserve max digest length for sha256
1895+
1896+ initContext(&ctx, lKey, lAlg);
1897+
1898+ if (lItem.isStreamable())
1899+ {
1900+ std::istream& lStream = lItem.getStream();
1901+ char lBuf[1024];
1902+ while (lStream.good())
1903+ {
1904+ lStream.read(lBuf, 1024);
1905+ HMAC_Update(
1906+ &ctx,
1907+ reinterpret_cast<const unsigned char*>(&lBuf[0]),
1908+ lStream.gcount()
1909+ );
1910+ }
1911+ }
1912+ else
1913+ {
1914+ String lString = lItem.getStringValue();
1915+ HMAC_Update(
1916+ &ctx,
1917+ reinterpret_cast<const unsigned char*>(lString.c_str()),
1918+ lString.length());
1919+ }
1920+ HMAC_Final(&ctx, out, &len);
1921+ HMAC_cleanup(&ctx);
1922+
1923+ return zorba::ItemSequence_t(new zorba::SingletonItemSequence(
1924+ theModule->getItemFactory()->createBase64Binary(&out[0], len)));
1925+}
1926+
1927+zorba::ItemSequence_t
1928+HMACComputeBinaryFunction::evaluate(const Arguments_t& aArgs) const
1929+{
1930+ zorba::Item lItem = getOneItemArgument(aArgs, 0);
1931+
1932+ String lKey = getOneStringArgument(aArgs, 1);
1933+ String lAlg = getOneStringArgument(aArgs, 2);
1934+
1935+ HMAC_CTX ctx;
1936+ unsigned int len;
1937+ unsigned char out[32]; // reserve max digest length for sha256
1938+
1939+ initContext(&ctx, lKey, lAlg);
1940+
1941+ if (lItem.isStreamable())
1942+ {
1943+ std::istream& lStream = lItem.getStream();
1944+ bool lDecoderAttached = false;
1945+
1946+ if (lItem.isEncoded())
1947+ {
1948+ base64::attach(lStream);
1949+ lDecoderAttached = true;
1950+ }
1951+ char lBuf[1024];
1952+ while (lStream.good())
1953+ {
1954+ lStream.read(lBuf, 1024);
1955+ HMAC_Update(
1956+ &ctx,
1957+ reinterpret_cast<const unsigned char*>(&lBuf[0]),
1958+ lStream.gcount());
1959+ }
1960+ if (lDecoderAttached)
1961+ {
1962+ base64::detach(lStream);
1963+ }
1964+ }
1965+ else
1966+ {
1967+ String lTmpDecodedBuf;
1968+ size_t lSize;
1969+ const char* lMsg = lItem.getBase64BinaryValue(lSize);
1970+ if (lItem.isEncoded())
1971+ {
1972+ String lTmpEncoded(lMsg, lSize);
1973+ // lTmpDecodedBuf is used to make sure lMsg is still alive during HMAC_Update
1974+ lTmpDecodedBuf = encoding::Base64::decode(lTmpEncoded);
1975+ lMsg = lTmpDecodedBuf.c_str();
1976+ lSize = lTmpDecodedBuf.size();
1977+ }
1978+ HMAC_Update(
1979+ &ctx,
1980+ reinterpret_cast<const unsigned char*>(lMsg),
1981+ lSize);
1982+ }
1983+ HMAC_Final(&ctx, out, &len);
1984+ HMAC_cleanup(&ctx);
1985+
1986+ return zorba::ItemSequence_t(new zorba::SingletonItemSequence(
1987+ theModule->getItemFactory()->createBase64Binary(&out[0], len)));
1988+}
1989+
1990 } /* namespace security */ } /* namespace zorba */
1991
1992 #ifdef WIN32
1993
1994=== modified file 'src/hmac.xq.src/hmac.h'
1995--- src/hmac.xq.src/hmac.h 2011-08-02 08:51:47 +0000
1996+++ src/hmac.xq.src/hmac.h 2012-07-27 03:35:22 +0000
1997@@ -60,32 +60,52 @@
1998 static ItemFactory*
1999 getItemFactory()
2000 {
2001- if(!theFactory)
2002+ if (!theFactory)
2003 theFactory = Zorba::getInstance(0)->getItemFactory();
2004 return theFactory;
2005 }
2006
2007 };
2008
2009- class HMACSHA1Function : public NonContextualExternalFunction
2010- {
2011- protected:
2012- const HMACModule* theModule;
2013- private:
2014-
2015- public:
2016- HMACSHA1Function(const HMACModule* aModule): theModule(aModule){}
2017- ~HMACSHA1Function(){}
2018-
2019- virtual String
2020- getLocalName() const { return "sha1"; }
2021-
2022- virtual zorba::ItemSequence_t
2023- evaluate(const Arguments_t&) const;
2024-
2025- virtual String
2026- getURI() const;
2027-
2028+ class HMACComputeFunction : public NonContextualExternalFunction
2029+ {
2030+ protected:
2031+ const HMACModule* theModule;
2032+
2033+ public:
2034+ HMACComputeFunction(const HMACModule* aModule)
2035+ : theModule(aModule) {}
2036+ ~HMACComputeFunction() {}
2037+
2038+ virtual String
2039+ getLocalName() const { return "compute"; }
2040+
2041+ virtual zorba::ItemSequence_t
2042+ evaluate(const Arguments_t&) const;
2043+
2044+ virtual String
2045+ getURI() const;
2046+ };
2047+
2048+ class HMACComputeBinaryFunction : public NonContextualExternalFunction
2049+ {
2050+ protected:
2051+ const HMACModule* theModule;
2052+
2053+ public:
2054+ HMACComputeBinaryFunction(const HMACModule* aModule)
2055+ : theModule(aModule) {}
2056+
2057+ ~HMACComputeBinaryFunction() {}
2058+
2059+ virtual String
2060+ getLocalName() const { return "compute-binary"; }
2061+
2062+ virtual zorba::ItemSequence_t
2063+ evaluate(const Arguments_t&) const;
2064+
2065+ virtual String
2066+ getURI() const;
2067 };
2068 } /* namespace security */ } /* namespace zorba */
2069
2070
2071=== removed file 'src/hmac.xq.src/hmac_sha1.cpp'
2072--- src/hmac.xq.src/hmac_sha1.cpp 2011-08-02 08:51:47 +0000
2073+++ src/hmac.xq.src/hmac_sha1.cpp 1970-01-01 00:00:00 +0000
2074@@ -1,69 +0,0 @@
2075-//******************************************************************************
2076-//* HMAC_SHA1.cpp : Implementation of HMAC SHA1 algorithm
2077-//* Comfort to RFC 2104
2078-//*
2079-//******************************************************************************
2080-#include <iostream>
2081-#include <memory>
2082-#include "hmac_sha1.h"
2083-
2084-namespace zorba { namespace securitymodule {
2085-
2086-void CHMAC_SHA1::HMAC_SHA1(unsigned char *text, int text_len,
2087- unsigned char *key, int key_len,
2088- unsigned char *digest)
2089-{
2090- memset(SHA1_Key, 0, SHA1_BLOCK_SIZE);
2091-
2092- /* repeated 64 times for values in ipad and opad */
2093- memset(m_ipad, 0x36, sizeof(m_ipad));
2094- memset(m_opad, 0x5c, sizeof(m_opad));
2095-
2096- /* STEP 1 */
2097- if (key_len > SHA1_BLOCK_SIZE) // was SHA1_DIGEST_LENGTH
2098- {
2099- CSHA1::Reset();
2100- CSHA1::Update((UINT_8 *)key, key_len);
2101- CSHA1::Final();
2102-
2103- CSHA1::GetHash((UINT_8 *)SHA1_Key);
2104- }
2105- else
2106- memcpy(SHA1_Key, key, key_len);
2107-
2108- /* STEP 2 */
2109- for (size_t i=0; i<sizeof(m_ipad); i++)
2110- {
2111- m_ipad[i] ^= SHA1_Key[i];
2112- }
2113-
2114- /* STEP 3 */
2115- memcpy(AppendBuf1, m_ipad, sizeof(m_ipad));
2116- memcpy(AppendBuf1 + sizeof(m_ipad), text, text_len);
2117-
2118- /* STEP 4 */
2119- CSHA1::Reset();
2120- CSHA1::Update((UINT_8 *)AppendBuf1, sizeof(m_ipad) + text_len);
2121- CSHA1::Final();
2122-
2123- CSHA1::GetHash((UINT_8 *)szReport);
2124-
2125- /* STEP 5 */
2126- for (size_t j=0; j<sizeof(m_opad); j++)
2127- {
2128- m_opad[j] ^= SHA1_Key[j];
2129- }
2130-
2131- /* STEP 6 */
2132- memcpy(AppendBuf2, m_opad, sizeof(m_opad));
2133- memcpy(AppendBuf2 + sizeof(m_opad), szReport, SHA1_DIGEST_LENGTH);
2134-
2135- /*STEP 7 */
2136- CSHA1::Reset();
2137- CSHA1::Update((UINT_8 *)AppendBuf2, sizeof(m_opad) + SHA1_DIGEST_LENGTH);
2138- CSHA1::Final();
2139-
2140- CSHA1::GetHash((UINT_8 *)digest);
2141-}
2142-
2143-}}
2144
2145=== removed file 'src/hmac.xq.src/hmac_sha1.h'
2146--- src/hmac.xq.src/hmac_sha1.h 2011-08-02 08:51:47 +0000
2147+++ src/hmac.xq.src/hmac_sha1.h 1970-01-01 00:00:00 +0000
2148@@ -1,59 +0,0 @@
2149-/*
2150- 100% free public domain implementation of the HMAC-SHA1 algorithm
2151- by Chien-Chung, Chung (Jim Chung) <jimchung1221@gmail.com>
2152-*/
2153-
2154-
2155-#ifndef ZORBA_HMAC_SHA1_H
2156-#define ZORBA_HMAC_SHA1_H
2157-
2158-#include "sha1.h"
2159-
2160-namespace zorba { namespace securitymodule {
2161-
2162-class CHMAC_SHA1 : public CSHA1
2163-{
2164- private:
2165- unsigned char m_ipad[64];
2166- unsigned char m_opad[64];
2167-
2168- char * szReport ;
2169- char * SHA1_Key ;
2170- char * AppendBuf1 ;
2171- char * AppendBuf2 ;
2172-
2173-
2174- public:
2175-
2176- enum {
2177- SHA1_DIGEST_LENGTH = 20,
2178- SHA1_BLOCK_SIZE = 64,
2179- HMAC_BUF_LEN = 4096
2180- } ;
2181-
2182- CHMAC_SHA1()
2183- :szReport(new char[HMAC_BUF_LEN]),
2184- SHA1_Key(new char[HMAC_BUF_LEN]),
2185- AppendBuf1(new char[HMAC_BUF_LEN]),
2186- AppendBuf2(new char[HMAC_BUF_LEN])
2187- {}
2188-
2189- ~CHMAC_SHA1()
2190- {
2191- delete[] szReport ;
2192- delete[] AppendBuf1 ;
2193- delete[] AppendBuf2 ;
2194- delete[] SHA1_Key ;
2195- }
2196-
2197- void HMAC_SHA1(unsigned char *text,
2198- int text_len,
2199- unsigned char *key,
2200- int key_len,
2201- unsigned char *digest);
2202-};
2203-
2204-}}
2205-
2206-#endif /* ZORBA_HMAC_SHA1_H */
2207-
2208
2209=== removed file 'src/hmac.xq.src/sha1.cpp'
2210--- src/hmac.xq.src/sha1.cpp 2011-08-02 08:51:47 +0000
2211+++ src/hmac.xq.src/sha1.cpp 1970-01-01 00:00:00 +0000
2212@@ -1,264 +0,0 @@
2213-/*
2214- 100% free public domain implementation of the SHA-1 algorithm
2215- by Dominik Reichl <dominik.reichl@t-online.de>
2216- Web: http://www.dominik-reichl.de/
2217-
2218- See header file for version history.
2219-*/
2220-
2221-// If compiling with MFC, you might want to add #include "StdAfx.h"
2222-
2223-#define _CRT_SECURE_NO_WARNINGS
2224-//#include "security_sha1.h"
2225-#include "sha1.h"
2226-
2227-#ifdef SHA1_UTILITY_FUNCTIONS
2228-#define SHA1_MAX_FILE_BUFFER 8000
2229-#endif
2230-
2231-// Rotate x bits to the left
2232-#ifndef ROL32
2233-#ifdef _MSC_VER
2234-#define ROL32(_val32,_nBits) _rotl(_val32,_nBits)
2235-#else
2236-#define ROL32(_val32,_nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
2237-#endif
2238-#endif
2239-
2240-#ifdef SHA1_LITTLE_ENDIAN
2241-#define SHABLK0(i) (m_block->l[i] = \
2242- (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
2243-#else
2244-#define SHABLK0(i) (m_block->l[i])
2245-#endif
2246-
2247-#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
2248- ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
2249-
2250-// SHA-1 rounds
2251-#define _R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
2252-#define _R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5);w=ROL32(w,30);}
2253-#define _R2(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5);w=ROL32(w,30);}
2254-#define _R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5);w=ROL32(w,30);}
2255-#define _R4(v,w,x,y,z,i) {z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5);w=ROL32(w,30);}
2256-
2257-namespace zorba { namespace securitymodule {
2258-
2259-CSHA1::CSHA1()
2260-{
2261- m_block = (SHA1_WORKSPACE_BLOCK*)m_workspace;
2262-
2263- Reset();
2264-}
2265-
2266-CSHA1::~CSHA1()
2267-{
2268- Reset();
2269-}
2270-
2271-void CSHA1::Reset()
2272-{
2273- // SHA1 initialization constants
2274- m_state[0] = 0x67452301;
2275- m_state[1] = 0xEFCDAB89;
2276- m_state[2] = 0x98BADCFE;
2277- m_state[3] = 0x10325476;
2278- m_state[4] = 0xC3D2E1F0;
2279-
2280- m_count[0] = 0;
2281- m_count[1] = 0;
2282-}
2283-
2284-void CSHA1::Transform(UINT_32* pState, const UINT_8* pBuffer)
2285-{
2286- UINT_32 a = pState[0], b = pState[1], c = pState[2], d = pState[3], e = pState[4];
2287-
2288- memcpy(m_block, pBuffer, 64);
2289-
2290- // 4 rounds of 20 operations each. Loop unrolled.
2291- _R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
2292- _R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
2293- _R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
2294- _R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
2295- _R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
2296- _R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
2297- _R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
2298- _R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
2299- _R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
2300- _R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
2301- _R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
2302- _R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
2303- _R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
2304- _R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
2305- _R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
2306- _R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
2307- _R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
2308- _R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
2309- _R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
2310- _R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
2311-
2312- // Add the working vars back into state
2313- pState[0] += a;
2314- pState[1] += b;
2315- pState[2] += c;
2316- pState[3] += d;
2317- pState[4] += e;
2318-
2319- // Wipe variables
2320-#ifdef SHA1_WIPE_VARIABLES
2321- a = b = c = d = e = 0;
2322-#endif
2323-}
2324-
2325-// Use this function to hash in binary data and strings
2326-void CSHA1::Update(const UINT_8* pbData, UINT_32 uLen)
2327-{
2328- UINT_32 j = ((m_count[0] >> 3) & 0x3F);
2329-
2330- if((m_count[0] += (uLen << 3)) < (uLen << 3))
2331- ++m_count[1]; // Overflow
2332-
2333- m_count[1] += (uLen >> 29);
2334-
2335- UINT_32 i;
2336- if((j + uLen) > 63)
2337- {
2338- i = 64 - j;
2339- memcpy(&m_buffer[j], pbData, i);
2340- Transform(m_state, m_buffer);
2341-
2342- for( ; (i + 63) < uLen; i += 64)
2343- Transform(m_state, &pbData[i]);
2344-
2345- j = 0;
2346- }
2347- else i = 0;
2348-
2349- if((uLen - i) != 0)
2350- memcpy(&m_buffer[j], &pbData[i], uLen - i);
2351-}
2352-
2353-#ifdef SHA1_UTILITY_FUNCTIONS
2354-// Hash in file contents
2355-bool CSHA1::HashFile(const TCHAR* tszFileName)
2356-{
2357- if(tszFileName == NULL) return false;
2358-
2359- FILE* fpIn = _tfopen(tszFileName, _T("rb"));
2360- if(fpIn == NULL) return false;
2361-
2362- _fseeki64(fpIn, 0, SEEK_END);
2363- const INT_64 lFileSize = _ftelli64(fpIn);
2364- _fseeki64(fpIn, 0, SEEK_SET);
2365-
2366- const INT_64 lMaxBuf = SHA1_MAX_FILE_BUFFER;
2367- UINT_8 vData[SHA1_MAX_FILE_BUFFER];
2368- INT_64 lRemaining = lFileSize;
2369-
2370- while(lRemaining > 0)
2371- {
2372- const size_t uMaxRead = static_cast<size_t>((lRemaining > lMaxBuf) ?
2373- lMaxBuf : lRemaining);
2374-
2375- const size_t uRead = fread(vData, 1, uMaxRead, fpIn);
2376- if(uRead == 0)
2377- {
2378- fclose(fpIn);
2379- return false;
2380- }
2381-
2382- Update(vData, static_cast<UINT_32>(uRead));
2383-
2384- lRemaining -= static_cast<INT_64>(uRead);
2385- }
2386-
2387- fclose(fpIn);
2388- return (lRemaining == 0);
2389-}
2390-#endif
2391-
2392-void CSHA1::Final()
2393-{
2394- UINT_32 i;
2395-
2396- UINT_8 finalcount[8];
2397- for(i = 0; i < 8; ++i)
2398- finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
2399- >> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
2400-
2401- Update((UINT_8*)"\200", 1);
2402-
2403- while ((m_count[0] & 504) != 448)
2404- Update((UINT_8*)"\0", 1);
2405-
2406- Update(finalcount, 8); // Cause a SHA1Transform()
2407-
2408- for(i = 0; i < 20; ++i)
2409- m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 0xFF);
2410-
2411- // Wipe variables for security reasons
2412-#ifdef SHA1_WIPE_VARIABLES
2413- memset(m_buffer, 0, 64);
2414- memset(m_state, 0, 20);
2415- memset(m_count, 0, 8);
2416- memset(finalcount, 0, 8);
2417- Transform(m_state, m_buffer);
2418-#endif
2419-}
2420-
2421-#ifdef SHA1_UTILITY_FUNCTIONS
2422-// Get the final hash as a pre-formatted string
2423-bool CSHA1::ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType) const
2424-{
2425- if(tszReport == NULL) return false;
2426-
2427- TCHAR tszTemp[16];
2428-
2429- if((rtReportType == REPORT_HEX) || (rtReportType == REPORT_HEX_SHORT))
2430- {
2431- _sntprintf(tszTemp, 15, _T("%02X"), m_digest[0]);
2432- _tcscpy(tszReport, tszTemp);
2433-
2434- const TCHAR* lpFmt = ((rtReportType == REPORT_HEX) ? _T(" %02X") : _T("%02X"));
2435- for(size_t i = 1; i < 20; ++i)
2436- {
2437- _sntprintf(tszTemp, 15, lpFmt, m_digest[i]);
2438- _tcscat(tszReport, tszTemp);
2439- }
2440- }
2441- else if(rtReportType == REPORT_DIGIT)
2442- {
2443- _sntprintf(tszTemp, 15, _T("%u"), m_digest[0]);
2444- _tcscpy(tszReport, tszTemp);
2445-
2446- for(size_t i = 1; i < 20; ++i)
2447- {
2448- _sntprintf(tszTemp, 15, _T(" %u"), m_digest[i]);
2449- _tcscat(tszReport, tszTemp);
2450- }
2451- }
2452- else return false;
2453-
2454- return true;
2455-}
2456-#endif
2457-
2458-#ifdef SHA1_STL_FUNCTIONS
2459-bool CSHA1::ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType) const
2460-{
2461- TCHAR tszOut[84];
2462- const bool bResult = ReportHash(tszOut, rtReportType);
2463- if(bResult) strOut = tszOut;
2464- return bResult;
2465-}
2466-#endif
2467-
2468-// Get the raw message digest
2469-bool CSHA1::GetHash(UINT_8* pbDest) const
2470-{
2471- if(pbDest == NULL) return false;
2472- memcpy(pbDest, m_digest, 20);
2473- return true;
2474-}
2475-
2476-}}
2477
2478=== removed file 'src/hmac.xq.src/sha1.h'
2479--- src/hmac.xq.src/sha1.h 2011-08-02 08:51:47 +0000
2480+++ src/hmac.xq.src/sha1.h 1970-01-01 00:00:00 +0000
2481@@ -1,259 +0,0 @@
2482-/*
2483- 100% free public domain implementation of the SHA-1 algorithm
2484- by Dominik Reichl <dominik.reichl@t-online.de>
2485- Web: http://www.dominik-reichl.de/
2486-
2487- Version 1.8 - 2008-03-16
2488- - Converted project files to Visual Studio 2008 format.
2489- - Added Unicode support for HashFile utility method.
2490- - Added support for hashing files using the HashFile method that are
2491- larger than 2 GB.
2492- - HashFile now returns an error code instead of copying an error
2493- message into the output buffer.
2494- - GetHash now returns an error code and validates the input parameter.
2495- - Added ReportHashStl STL utility method.
2496- - Added REPORT_HEX_SHORT reporting mode.
2497- - Improved Linux compatibility of test program.
2498-
2499- Version 1.7 - 2006-12-21
2500- - Fixed buffer underrun warning that appeared when compiling with
2501- Borland C Builder (thanks to Rex Bloom and Tim Gallagher for the
2502- patch).
2503- - Breaking change: ReportHash writes the final hash to the start
2504- of the buffer, i.e. it's not appending it to the string anymore.
2505- - Made some function parameters const.
2506- - Added Visual Studio 2005 project files to demo project.
2507-
2508- Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches)
2509- - You can set the endianness in your files, no need to modify the
2510- header file of the CSHA1 class anymore.
2511- - Aligned data support.
2512- - Made support/compilation of the utility functions (ReportHash and
2513- HashFile) optional (useful when bytes count, for example in embedded
2514- environments).
2515-
2516- Version 1.5 - 2005-01-01
2517- - 64-bit compiler compatibility added.
2518- - Made variable wiping optional (define SHA1_WIPE_VARIABLES).
2519- - Removed unnecessary variable initializations.
2520- - ROL32 improvement for the Microsoft compiler (using _rotl).
2521-
2522- Version 1.4 - 2004-07-22
2523- - CSHA1 now compiles fine with GCC 3.3 under MacOS X (thanks to Larry
2524- Hastings).
2525-
2526- Version 1.3 - 2003-08-17
2527- - Fixed a small memory bug and made a buffer array a class member to
2528- ensure correct working when using multiple CSHA1 class instances at
2529- one time.
2530-
2531- Version 1.2 - 2002-11-16
2532- - Borlands C++ compiler seems to have problems with string addition
2533- using sprintf. Fixed the bug which caused the digest report function
2534- not to work properly. CSHA1 is now Borland compatible.
2535-
2536- Version 1.1 - 2002-10-11
2537- - Removed two unnecessary header file includes and changed BOOL to
2538- bool. Fixed some minor bugs in the web page contents.
2539-
2540- Version 1.0 - 2002-06-20
2541- - First official release.
2542-
2543- ======== Test Vectors (from FIPS PUB 180-1) ========
2544-
2545- SHA1("abc") =
2546- A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
2547-
2548- SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
2549- 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
2550-
2551- SHA1(A million repetitions of "a") =
2552- 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
2553-*/
2554-
2555-#ifndef SAUSA_UTILS_SHA1_HDR
2556-#define SAUSA_UTILS_SHA1_HDR
2557-
2558-#if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS)
2559-#define SHA1_UTILITY_FUNCTIONS
2560-#endif
2561-
2562-#if !defined(SHA1_STL_FUNCTIONS) && !defined(SHA1_NO_STL_FUNCTIONS)
2563-#define SHA1_STL_FUNCTIONS
2564-#if !defined(SHA1_UTILITY_FUNCTIONS)
2565-#error STL functions require SHA1_UTILITY_FUNCTIONS.
2566-#endif
2567-#endif
2568-
2569-#include <memory.h>
2570-
2571-#ifdef SHA1_UTILITY_FUNCTIONS
2572-#include <stdio.h>
2573-#include <string.h>
2574-#endif
2575-
2576-#ifdef SHA1_STL_FUNCTIONS
2577-#include <string>
2578-#endif
2579-
2580-#ifdef _MSC_VER
2581-#include <stdlib.h>
2582-#endif
2583-
2584-// You can define the endian mode in your files without modifying the SHA-1
2585-// source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN
2586-// in your files, before including the SHA1.h header file. If you don't
2587-// define anything, the class defaults to little endian.
2588-#if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN)
2589-#define SHA1_LITTLE_ENDIAN
2590-#endif
2591-
2592-// If you want variable wiping, #define SHA1_WIPE_VARIABLES, if not,
2593-// #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it
2594-// defaults to wiping.
2595-#if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES)
2596-#define SHA1_WIPE_VARIABLES
2597-#endif
2598-
2599-#if defined(SHA1_HAS_TCHAR)
2600-#include <tchar.h>
2601-#else
2602-#ifdef _MSC_VER
2603-#include <tchar.h>
2604-#else
2605-#ifndef TCHAR
2606-#define TCHAR char
2607-#endif
2608-#ifndef _T
2609-#define _T(__x) (__x)
2610-#define _tmain main
2611-#define _tprintf printf
2612-#define _getts gets
2613-#define _tcslen strlen
2614-#define _tfopen fopen
2615-#define _tcscpy strcpy
2616-#define _tcscat strcat
2617-#define _sntprintf snprintf
2618-#endif
2619-#endif
2620-#endif
2621-
2622-// Fallback, if no 64-bit support
2623-#ifndef _fseeki64
2624-#define _fseeki64 fseek
2625-#endif
2626-#ifndef _ftelli64
2627-#define _ftelli64 ftell
2628-#endif
2629-
2630-///////////////////////////////////////////////////////////////////////////
2631-// Define variable types
2632-
2633-#ifndef UINT_8
2634-#ifdef _MSC_VER // Compiling with Microsoft compiler
2635-#define UINT_8 unsigned __int8
2636-#else // !_MSC_VER
2637-#define UINT_8 unsigned char
2638-#endif // _MSC_VER
2639-#endif
2640-
2641-#ifndef UINT_32
2642-#ifdef _MSC_VER // Compiling with Microsoft compiler
2643-#define UINT_32 unsigned __int32
2644-#else // !_MSC_VER
2645-#if (ULONG_MAX == 0xFFFFFFFF)
2646-#define UINT_32 unsigned long
2647-#else
2648-#define UINT_32 unsigned int
2649-#endif
2650-#endif // _MSC_VER
2651-#endif // UINT_32
2652-
2653-#ifndef INT_64
2654-#ifdef _MSC_VER // Compiling with Microsoft compiler
2655-#define INT_64 __int64
2656-#else // !_MSC_VER
2657-#define INT_64 long long
2658-#endif // _MSC_VER
2659-#endif // INT_64
2660-
2661-#ifndef UINT_64
2662-#ifdef _MSC_VER // Compiling with Microsoft compiler
2663-#define UINT_64 unsigned __int64
2664-#else // !_MSC_VER
2665-#define UINT_64 unsigned long long
2666-#endif // _MSC_VER
2667-#endif // UINT_64
2668-
2669-///////////////////////////////////////////////////////////////////////////
2670-// Declare SHA-1 workspace
2671-
2672-namespace zorba { namespace securitymodule {
2673-
2674-typedef union
2675-{
2676- UINT_8 c[64];
2677- UINT_32 l[16];
2678-} SHA1_WORKSPACE_BLOCK;
2679-
2680-class CSHA1
2681-{
2682-public:
2683-#ifdef SHA1_UTILITY_FUNCTIONS
2684- // Different formats for ReportHash
2685- enum REPORT_TYPE
2686- {
2687- REPORT_HEX = 0,
2688- REPORT_DIGIT = 1,
2689- REPORT_HEX_SHORT = 2
2690- };
2691-#endif
2692-
2693- // Constructor and destructor
2694- CSHA1();
2695- ~CSHA1();
2696-
2697- UINT_32 m_state[5];
2698- UINT_32 m_count[2];
2699- UINT_32 m_reserved0[1]; // Memory alignment padding
2700- UINT_8 m_buffer[64];
2701- UINT_8 m_digest[20];
2702- UINT_32 m_reserved1[3]; // Memory alignment padding
2703-
2704- void Reset();
2705-
2706- // Update the hash value
2707- void Update(const UINT_8* pbData, UINT_32 uLen);
2708-
2709-#ifdef SHA1_UTILITY_FUNCTIONS
2710- // Hash in file contents
2711- bool HashFile(const TCHAR* tszFileName);
2712-#endif
2713-
2714- // Finalize hash, call before using ReportHash(Stl)
2715- void Final();
2716-
2717-#ifdef SHA1_UTILITY_FUNCTIONS
2718- bool ReportHash(TCHAR* tszReport, REPORT_TYPE rtReportType = REPORT_HEX) const;
2719-#endif
2720-
2721-#ifdef SHA1_STL_FUNCTIONS
2722- bool ReportHashStl(std::basic_string<TCHAR>& strOut, REPORT_TYPE rtReportType =
2723- REPORT_HEX) const;
2724-#endif
2725-
2726- bool GetHash(UINT_8* pbDest) const;
2727-
2728-private:
2729- // Private SHA-1 transformation
2730- void Transform(UINT_32* pState, const UINT_8* pBuffer);
2731-
2732- // Member variables
2733- UINT_8 m_workspace[64];
2734- SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above
2735-};
2736-
2737-}}//end of namespace
2738-
2739-#endif // ZORBA_SECURITY_MODULE_SHA1_HDR
2740-
2741
2742=== added file 'test/ExpQueryResults/security/hash-binary.xml.res'
2743--- test/ExpQueryResults/security/hash-binary.xml.res 1970-01-01 00:00:00 +0000
2744+++ test/ExpQueryResults/security/hash-binary.xml.res 2012-07-27 03:35:22 +0000
2745@@ -0,0 +1,1 @@
2746+C7C73C51A659682EBA186FD13D9F25814CD46273A0E4E14A0AFFA266B4DD80DD
2747
2748=== modified file 'test/ExpQueryResults/security/hash-md5.xml.res'
2749--- test/ExpQueryResults/security/hash-md5.xml.res 2011-10-06 08:19:34 +0000
2750+++ test/ExpQueryResults/security/hash-md5.xml.res 2012-07-27 03:35:22 +0000
2751@@ -1,1 +1,1 @@
2752-900150983cd24fb0d6963f7d28e17f72 8215ef0796a20bcaaae116d3876c664a
2753+900150983CD24FB0D6963F7D28E17F72 ghXvB5aiC8qq4RbTh2xmSg==
2754
2755=== added file 'test/ExpQueryResults/security/hash-sha256.xml.res'
2756--- test/ExpQueryResults/security/hash-sha256.xml.res 1970-01-01 00:00:00 +0000
2757+++ test/ExpQueryResults/security/hash-sha256.xml.res 2012-07-27 03:35:22 +0000
2758@@ -0,0 +1,1 @@
2759+ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0= JI1qYdIGOLjlwCaTDD5gOaM85Flk/yFn9uzt1BnbBsE= tbudgBSg+bHWHiHnlteNzN8TUvI80ygS9IULh4rklEw=
2760
2761=== added file 'test/ExpQueryResults/security/hmac-sha256.xml.res'
2762--- test/ExpQueryResults/security/hmac-sha256.xml.res 1970-01-01 00:00:00 +0000
2763+++ test/ExpQueryResults/security/hmac-sha256.xml.res 2012-07-27 03:35:22 +0000
2764@@ -0,0 +1,1 @@
2765+zT9Ze00vfD2bgGhwOCOsaT0/TEaeTrTHlISYevBWImg= FA9F6BE67ED57FE915152F3D4C07A2EB1FAC236C2175DF9B79C3A2905F3BF94A
2766
2767=== added file 'test/Queries/security/hash-binary.xq'
2768--- test/Queries/security/hash-binary.xq 1970-01-01 00:00:00 +0000
2769+++ test/Queries/security/hash-binary.xq 2012-07-27 03:35:22 +0000
2770@@ -0,0 +1,4 @@
2771+import module namespace hash = "http://www.zorba-xquery.com/modules/cryptography/hash";
2772+import module namespace f = "http://expath.org/ns/file";
2773+
2774+xs:hexBinary(hash:hash-binary(f:read-binary(resolve-uri("ls")), "sha256"))
2775
2776=== modified file 'test/Queries/security/hash-md5.xq'
2777--- test/Queries/security/hash-md5.xq 2011-07-05 03:24:29 +0000
2778+++ test/Queries/security/hash-md5.xq 2012-07-27 03:35:22 +0000
2779@@ -1,5 +1,5 @@
2780 (: Values compared to php -r 'echo base64_encode(sha1($string, true));' :)
2781 import module namespace hash = "http://www.zorba-xquery.com/modules/cryptography/hash";
2782
2783-hash:md5("abc"),
2784+xs:hexBinary(hash:md5("abc")),
2785 hash:md5("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
2786
2787=== added file 'test/Queries/security/hash-sha256.xq'
2788--- test/Queries/security/hash-sha256.xq 1970-01-01 00:00:00 +0000
2789+++ test/Queries/security/hash-sha256.xq 2012-07-27 03:35:22 +0000
2790@@ -0,0 +1,6 @@
2791+(: Values compared to php -r 'echo base64_encode(sha1($string, true));' :)
2792+import module namespace hash = "http://www.zorba-xquery.com/modules/cryptography/hash";
2793+
2794+hash:hash("abc", "sha256"),
2795+hash:hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "sha256"),
2796+hash:hash-binary(xs:base64Binary("Zm9vCg=="), "sha256")
2797
2798=== added file 'test/Queries/security/hmac-sha256.xq'
2799--- test/Queries/security/hmac-sha256.xq 1970-01-01 00:00:00 +0000
2800+++ test/Queries/security/hmac-sha256.xq 2012-07-27 03:35:22 +0000
2801@@ -0,0 +1,5 @@
2802+import module namespace hash = "http://www.zorba-xquery.com/modules/cryptography/hmac";
2803+import module namespace f = "http://expath.org/ns/file";
2804+
2805+hash:compute-binary(xs:base64Binary("Zm9vCg=="), "bar", "sha256"),
2806+xs:hexBinary(hash:compute-binary(f:read-binary(resolve-uri("ls")), "bar", "sha256"))
2807
2808=== added file 'test/Queries/security/ls'
2809Binary files test/Queries/security/ls 1970-01-01 00:00:00 +0000 and test/Queries/security/ls 2012-07-27 03:35:22 +0000 differ

Subscribers

People subscribed via source and target branches

to all changes: