|
From: creecode <icr...@us...> - 2006-03-12 20:37:23
|
Update of /cvsroot/frontierkernel/Frontier/Common/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24151 Modified Files: langcrypt.c Log Message: changed hashWhirlpool to whirlpool changed hashMd5 to MD5 note: normally we wouldn't be changing names of tokens like this but there was a bit of a foul-up in communication about naming. we decided that hash is redundant in the context of the cyrpt verb category. added SHA1 and hmacSHA1 Index: langcrypt.c =================================================================== RCS file: /cvsroot/frontierkernel/Frontier/Common/source/langcrypt.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** langcrypt.c 11 Mar 2006 04:52:12 -0000 1.2 --- langcrypt.c 12 Mar 2006 20:37:18 -0000 1.3 *************** *** 1,2 **** --- 1,29 ---- + + /* $Id$ */ + + /****************************************************************************** + + UserLand Frontier(tm) -- High performance Web content management, + object database, system-level and Internet scripting environment, + including source code editing and debugging. + + Copyright (C) 1992-2004 UserLand Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + ******************************************************************************/ + #include "frontier.h" #include "standard.h" *************** *** 14,26 **** #include "md5.h" #include "whirlpool.h" typedef enum tycryptverbtoken { /* verbs that are processed by crypt.c */ ! hashwhirlpoolfunc, hmacmd5func, ! hashmd5func, ctcryptverbs --- 41,59 ---- #include "md5.h" #include "whirlpool.h" + #include "sha.h" + #include "sha_locl.h" typedef enum tycryptverbtoken { /* verbs that are processed by crypt.c */ ! whirlpoolfunc, hmacmd5func, ! md5func, ! ! sha1func, ! ! hmacsha1func, ctcryptverbs *************** *** 41,45 **** switch (token) { ! case hashwhirlpoolfunc: { /* 2006-03-04 creedon: Whirlpool hash */ Handle h; --- 74,78 ---- switch (token) { ! case whirlpoolfunc: { /* 2006-03-04 creedon: Whirlpool hash */ Handle h; *************** *** 79,83 **** } /* for */ ! setstringlength (bs, 128); if (!newtexthandle (bs, &hresult)) --- 112,116 ---- } /* for */ ! setstringlength (bs, DIGESTBYTES * 2); if (!newtexthandle (bs, &hresult)) *************** *** 90,94 **** Handle hresult; ! if (!newfilledhandle (digest, 64, &hresult)) return (false); --- 123,127 ---- Handle hresult; ! if (!newfilledhandle (digest, DIGESTBYTES, &hresult)) return (false); *************** *** 97,101 **** return (true); ! } case hmacmd5func: { /* 2006-03-05 creedon: keyed-hashing for message authentication using md5 */ --- 130,134 ---- return (true); ! } /* whirlpoolfunc */ case hmacmd5func: { /* 2006-03-05 creedon: keyed-hashing for message authentication using md5 */ *************** *** 157,163 **** return (true); ! } ! case hashmd5func: { /* 2006-03-10 creedon: cribbed from stringverbs.c */ Handle x; MD5_CTX hashcontext; /* MD5 context. */ --- 190,196 ---- return (true); ! } /* hmacmd5func */ ! case md5func: { /* 2006-03-10 creedon: cribbed from stringverbs.c */ Handle x; MD5_CTX hashcontext; /* MD5 context. */ *************** *** 216,221 **** return (true); ! } default: getstringlist (langerrorlist, unimplementedverberror, bserror); --- 249,373 ---- return (true); ! } /* md5func */ + case sha1func: { /* 2006-03-11 creedon: SHA1 hash */ + Handle h; + SHA_CTX hashcontext; /* SHA context */ + unsigned char digest [SHA_DIGEST_LENGTH]; + short ctconsumed = 1; + short ctpositional = 1; + tyvaluerecord vtranslate; + + if (!getreadonlytextvalue (hp1, 1, &h)) + return (false); + + setbooleanvalue (true, &vtranslate); + + flnextparamislast = true; + + if (!getoptionalparamvalue (hp1, &ctconsumed, &ctpositional, "\x0b""flTranslate", &vtranslate)) + return (false); + + lockhandle (h); + + SHA1_Init (&hashcontext); + + SHA1_Update (&hashcontext, (unsigned char *)(*h), gethandlesize (h)); + + SHA1_Final (digest, &hashcontext); + + unlockhandle (h); + + if (vtranslate.data.flvalue) { /* return a hex string */ + bigstring bs; + unsigned char enc [] = "0123456789abcdef"; + long ix; + Handle hresult; + + for(ix = 0; ix < SHA_DIGEST_LENGTH; ix++) { + setstringcharacter (bs, 2 * ix, enc [(int)((digest [ix]) / 16)]); + setstringcharacter (bs, 2 * ix + 1, enc [(int)((digest [ix]) % 16)]); + } /* for */ + + setstringlength (bs, SHA_DIGEST_LENGTH * 2); + + if (!newtexthandle (bs, &hresult)) + return (false); + + return (setheapvalue (hresult, stringvaluetype, v)); + } + + else { /* return a binary of unknown type */ + Handle hresult; + + if (!newfilledhandle (digest, SHA_DIGEST_LENGTH, &hresult)) + return (false); + + return (setbinaryvalue (hresult, typeunknown, v)); + } + + return (true); + } /* sha1func */ + + case hmacsha1func: { /* 2006-03-11 creedon: keyed-hashing for message authentication using sha1 */ + + Handle hdata, hkey; + unsigned char digest [SHA_DIGEST_LENGTH]; + short ctconsumed = 2; + short ctpositional = 2; + tyvaluerecord vtranslate; + + setbooleanvalue (true, &vtranslate); + + if (!getreadonlytextvalue (hp1, 1, &hdata)) + return (false); + + if (!getreadonlytextvalue (hp1, 2, &hkey)) + return (false); + + flnextparamislast = true; + + if (!getoptionalparamvalue (hp1, &ctconsumed, &ctpositional, "\x0b""flTranslate", &vtranslate)) + return (false); + + lockhandle (hkey); + lockhandle (hdata); + + hmacsha1 ((unsigned char *)(*hdata), gethandlesize (hdata), (unsigned char *)(*hkey), gethandlesize (hkey), (unsigned char *) digest); + + unlockhandle (hkey); + unlockhandle (hdata); + + if (vtranslate.data.flvalue) { /* return a hex string */ + bigstring bs; + unsigned char enc [] = "0123456789abcdef"; + long ix; + Handle hresult; + + for (ix = 0; ix < SHA_DIGEST_LENGTH; ix++) { + setstringcharacter (bs, 2 * ix, enc [(int) ((digest [ix]) / 16)]); + setstringcharacter (bs, 2 * ix + 1, enc [(int) ((digest [ix]) % 16)]); + } /* for */ + + setstringlength (bs, SHA_DIGEST_LENGTH * 2); + + if (!newtexthandle (bs, &hresult)) + return (false); + + return (setheapvalue (hresult, stringvaluetype, v)); + } + + else { /* return a binary of unknown type */ + Handle hresult; + + if (!newfilledhandle (digest, SHA_DIGEST_LENGTH, &hresult)) + return (false); + + return (setbinaryvalue (hresult, typeunknown, v)); + } + + return (true); + } /* hmacsha1func */ + default: getstringlist (langerrorlist, unimplementedverberror, bserror); *************** *** 283,287 **** memcpy (k_opad, key, key_len); ! for (i=0; i < 64; i++) { /* XOR key with ipad and opad values */ k_ipad [i] ^= 0x36; k_opad [i] ^= 0x5c; --- 435,439 ---- memcpy (k_opad, key, key_len); ! for (i = 0; i < 64; i++) { /* XOR key with ipad and opad values */ k_ipad [i] ^= 0x36; k_opad [i] ^= 0x5c; *************** *** 305,306 **** --- 457,527 ---- } /* hmacmd5 */ + + boolean hmacsha1 (unsigned char * text, int text_len, unsigned char * key, int key_len, unsigned char * digest) { + + /* + 2006-03-10 creedon: created, cribbed from hmacmd5 function + + text; pointer to data stream + text_len; length of data stream + key; pointer to authentication key + key_len; length of authentication key + digest; caller digest to be filled in + */ + + SHA_CTX context; + unsigned char k_ipad [SHA_CBLOCK + 1]; /* inner padding - key XORd with ipad */ + unsigned char k_opad [SHA_CBLOCK + 1]; /* outer padding - key XORd with opad */ + unsigned char tk [SHA_DIGEST_LENGTH]; + int i; + + if (key_len > SHA_CBLOCK) { /* if key is longer than 64 bytes reset it to key=SHA1(key) */ + + SHA_CTX tctx; + + SHA1_Init (&tctx); + SHA1_Update (&tctx, key, key_len); + SHA1_Final (tk, &tctx); + + key = tk; + key_len = SHA_DIGEST_LENGTH; + } + + /* + the HMAC_SHA1 transform looks like: + + SHA1(K XOR opad, SHA1(K XOR ipad, text)) + + where K is an n byte key + ipad is the byte 0x36 repeated 64 times + opad is the byte 0x5c repeated 64 times + and text is the data being protected + */ + + memset (k_ipad, 0, sizeof (k_ipad)); /* start out by storing key in pads */ + memset (k_opad, 0, sizeof (k_opad)); + memcpy (k_ipad, key, key_len); + memcpy (k_opad, key, key_len); + + for (i = 0; i < SHA_CBLOCK; i++) { /* XOR key with ipad and opad values */ + k_ipad [i] ^= 0x36; + k_opad [i] ^= 0x5c; + } + + /* perform inner SHA1 */ + + SHA1_Init (&context); /* init context for 1st pass */ + SHA1_Update (&context, k_ipad, SHA_CBLOCK); /* start with inner pad */ + SHA1_Update (&context, text, text_len); /* then text of datagram */ + SHA1_Final (digest, &context); /* finish up 1st pass */ + + /* perform outer SHA1 */ + + SHA1_Init (&context); /* init context for 2nd pass */ + SHA1_Update (&context, k_opad, SHA_CBLOCK); /* start with outer pad */ + SHA1_Update (&context, digest, SHA_DIGEST_LENGTH); /* then results of 1st hash */ + SHA1_Final (digest, &context); /* finish up 2nd pass */ + + return (true); + } /* hmacsha1 */ + |