|
|
| version 1.64, 2012/09/25 11:31:03 | version 1.65, 2013/07/12 21:23:29 |
|---|---|
| Line 14 | Line 14 |
| #include "pa_vmath.h" | #include "pa_vmath.h" |
| #include "pa_request.h" | #include "pa_request.h" |
| #include "pa_md5.h" | #include "pa_md5.h" |
| #include "pa_sha2.h" | |
| #include "pa_random.h" | #include "pa_random.h" |
| #ifdef WIN32 | #ifdef WIN32 |
| Line 342 void memxor(char *dest, const char *src, | Line 343 void memxor(char *dest, const char *src, |
| #define IPAD 0x36 | #define IPAD 0x36 |
| #define OPAD 0x5c | #define OPAD 0x5c |
| #define HMAC(key,init,update,final,digestlen){ \ | #define HMAC(key,init,update,final,blocklen,digestlen){ \ |
| unsigned char tempdigest[digestlen], keydigest[digestlen]; \ | unsigned char tempdigest[digestlen], keydigest[digestlen]; \ |
| size_t keylen=strlen(key); \ | size_t keylen=strlen(key); \ |
| /* Reduce the key's size, so that it becomes <= 64 bytes. */ \ | /* Reduce the key's size, so that it becomes <= blocklen bytes. */ \ |
| if (keylen > 64){ \ | if (keylen > blocklen){ \ |
| init(&c); \ | init(&c); \ |
| update(&c,(const unsigned char*)hmac, keylen); \ | update(&c,(const unsigned char*)hmac, keylen); \ |
| final(keydigest, &c); \ | final(keydigest, &c); \ |
| Line 354 void memxor(char *dest, const char *src, | Line 355 void memxor(char *dest, const char *src, |
| keylen = digestlen; \ | keylen = digestlen; \ |
| } \ | } \ |
| /* Compute TEMP from KEY and STRING. */ \ | /* Compute TEMP from KEY and STRING. */ \ |
| char block[64]; \ | char block[blocklen]; \ |
| memset (block, IPAD, sizeof (block)); \ | memset (block, IPAD, blocklen); \ |
| memxor (block, key, keylen); \ | memxor (block, key, keylen); \ |
| init(&c); \ | init(&c); \ |
| update(&c, (const unsigned char*)block, 64); \ | update(&c, (const unsigned char*)block, blocklen); \ |
| update(&c, (const unsigned char*)string, strlen(string)); \ | update(&c, (const unsigned char*)string, strlen(string)); \ |
| final(tempdigest, &c); \ | final(tempdigest, &c); \ |
| /* Compute result from KEY and TEMP. */ \ | /* Compute result from KEY and TEMP. */ \ |
| memset (block, OPAD, sizeof (block)); \ | memset (block, OPAD, blocklen); \ |
| memxor (block, key, keylen); \ | memxor (block, key, keylen); \ |
| init(&c); \ | init(&c); \ |
| update(&c, (const unsigned char*)block, 64); \ | update(&c, (const unsigned char*)block, blocklen); \ |
| update(&c, (const unsigned char*)tempdigest, digestlen); \ | update(&c, (const unsigned char*)tempdigest, digestlen); \ |
| } | } |
| Line 373 static void _digest(Request& r, MethodPa | Line 374 static void _digest(Request& r, MethodPa |
| const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING); | const String &smethod = params.as_string(0, PARAMETER_MUST_BE_STRING); |
| const char *string = params.as_string(1, PARAMETER_MUST_BE_STRING).cstr(); | const char *string = params.as_string(1, PARAMETER_MUST_BE_STRING).cstr(); |
| enum Method { M_MD5, M_SHA1 } method; | enum Method { M_MD5, M_SHA1, M_SHA256, M_SHA512 } method; |
| if (smethod == "md5") method = M_MD5; | if (smethod == "md5") method = M_MD5; |
| else if (smethod == "sha1" ) method = M_SHA1; | else if (smethod == "sha1" ) method = M_SHA1; |
| else if (smethod == "sha256" ) method = M_SHA256; | |
| else if (smethod == "sha512" ) method = M_SHA512; | |
| else throw Exception(PARSER_RUNTIME, &smethod, "must be 'md5' or 'sha1'"); | else throw Exception(PARSER_RUNTIME, &smethod, "must be 'md5' or 'sha1'"); |
| const char *hmac=0; | const char *hmac=0; |
| Line 405 static void _digest(Request& r, MethodPa | Line 408 static void _digest(Request& r, MethodPa |
| if(method == M_MD5){ | if(method == M_MD5){ |
| PA_MD5_CTX c; | PA_MD5_CTX c; |
| if(hmac){ | if(hmac){ |
| HMAC(hmac, pa_MD5Init, pa_MD5Update, pa_MD5Final, 16); | HMAC(hmac, pa_MD5Init, pa_MD5Update, pa_MD5Final, 64, 16); |
| } else { | } else { |
| pa_MD5Init(&c); | pa_MD5Init(&c); |
| pa_MD5Update(&c, (const unsigned char*)string, strlen(string)); | pa_MD5Update(&c, (const unsigned char*)string, strlen(string)); |
| Line 418 static void _digest(Request& r, MethodPa | Line 421 static void _digest(Request& r, MethodPa |
| if(method == M_SHA1){ | if(method == M_SHA1){ |
| SHA1Context c; | SHA1Context c; |
| if(hmac){ | if(hmac){ |
| HMAC(hmac, SHA1Reset, SHA1Input, SHA1ReadDigest, 20); | HMAC(hmac, SHA1Reset, SHA1Input, SHA1ReadDigest, 64, 20); |
| } else { | } else { |
| SHA1Reset(&c); | SHA1Reset(&c); |
| SHA1Input(&c, (const unsigned char*)string, strlen(string)); | SHA1Input(&c, (const unsigned char*)string, strlen(string)); |
| Line 428 static void _digest(Request& r, MethodPa | Line 431 static void _digest(Request& r, MethodPa |
| digest = String::C(str, 20); | digest = String::C(str, 20); |
| } | } |
| if(method == M_SHA256){ | |
| SHA256_CTX c; | |
| if(hmac){ | |
| HMAC(hmac, pa_SHA256_Init, pa_SHA256_Update, pa_SHA256_Final, 64, SHA256_DIGEST_LENGTH); | |
| } else { | |
| pa_SHA256_Init(&c); | |
| pa_SHA256_Update(&c, (const unsigned char*)string, strlen(string)); | |
| } | |
| char *str=(char *)pa_malloc(SHA256_DIGEST_LENGTH); | |
| pa_SHA256_Final((unsigned char *)str, &c); | |
| digest = String::C(str, SHA256_DIGEST_LENGTH); | |
| } | |
| if(method == M_SHA512){ | |
| SHA512_CTX c; | |
| if(hmac){ | |
| HMAC(hmac, pa_SHA512_Init, pa_SHA512_Update, pa_SHA512_Final, 128, SHA512_DIGEST_LENGTH); | |
| } else { | |
| pa_SHA512_Init(&c); | |
| pa_SHA512_Update(&c, (const unsigned char*)string, strlen(string)); | |
| } | |
| char *str=(char *)pa_malloc(SHA512_DIGEST_LENGTH); | |
| pa_SHA512_Final((unsigned char *)str, &c); | |
| digest = String::C(str, SHA512_DIGEST_LENGTH); | |
| } | |
| if(format == F_HEX){ | if(format == F_HEX){ |
| r.write_pass_lang(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); | r.write_pass_lang(*new String(hex_string((unsigned char *)digest.str, digest.length, false))); |
| } | } |