123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- #include "php.h"
- #include "sha1.h"
- #include "md5.h"
- PHPAPI void make_sha1_digest(char *sha1str, const unsigned char *digest)
- {
- make_digest_ex(sha1str, digest, 20);
- }
- PHP_FUNCTION(sha1)
- {
- zend_string *arg;
- bool raw_output = 0;
- PHP_SHA1_CTX context;
- unsigned char digest[20];
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_STR(arg)
- Z_PARAM_OPTIONAL
- Z_PARAM_BOOL(raw_output)
- ZEND_PARSE_PARAMETERS_END();
- PHP_SHA1Init(&context);
- PHP_SHA1Update(&context, (unsigned char *) ZSTR_VAL(arg), ZSTR_LEN(arg));
- PHP_SHA1Final(digest, &context);
- if (raw_output) {
- RETURN_STRINGL((char *) digest, 20);
- } else {
- RETVAL_NEW_STR(zend_string_alloc(40, 0));
- make_digest_ex(Z_STRVAL_P(return_value), digest, 20);
- }
- }
- PHP_FUNCTION(sha1_file)
- {
- char *arg;
- size_t arg_len;
- bool raw_output = 0;
- unsigned char buf[1024];
- unsigned char digest[20];
- PHP_SHA1_CTX context;
- ssize_t n;
- php_stream *stream;
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_PATH(arg, arg_len)
- Z_PARAM_OPTIONAL
- Z_PARAM_BOOL(raw_output)
- ZEND_PARSE_PARAMETERS_END();
- stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
- if (!stream) {
- RETURN_FALSE;
- }
- PHP_SHA1Init(&context);
- while ((n = php_stream_read(stream, (char *) buf, sizeof(buf))) > 0) {
- PHP_SHA1Update(&context, buf, n);
- }
- PHP_SHA1Final(digest, &context);
- php_stream_close(stream);
- if (raw_output) {
- RETURN_STRINGL((char *) digest, 20);
- } else {
- RETVAL_NEW_STR(zend_string_alloc(40, 0));
- make_digest_ex(Z_STRVAL_P(return_value), digest, 20);
- }
- }
- static void SHA1Transform(uint32_t[5], const unsigned char[64]);
- static void SHA1Encode(unsigned char *, uint32_t *, unsigned int);
- static void SHA1Decode(uint32_t *, const unsigned char *, unsigned int);
- static const unsigned char PADDING[64] =
- {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- };
- #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
- #define G(x, y, z) ((x) ^ (y) ^ (z))
- #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
- #define I(x, y, z) ((x) ^ (y) ^ (z))
- #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
- #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
- (x[i&15]=ROTATE_LEFT(tmp, 1)) )
- #define FF(a, b, c, d, e, w) { \
- (e) += F ((b), (c), (d)) + (w) + (uint32_t)(0x5A827999); \
- (e) += ROTATE_LEFT ((a), 5); \
- (b) = ROTATE_LEFT((b), 30); \
- }
- #define GG(a, b, c, d, e, w) { \
- (e) += G ((b), (c), (d)) + (w) + (uint32_t)(0x6ED9EBA1); \
- (e) += ROTATE_LEFT ((a), 5); \
- (b) = ROTATE_LEFT((b), 30); \
- }
- #define HH(a, b, c, d, e, w) { \
- (e) += H ((b), (c), (d)) + (w) + (uint32_t)(0x8F1BBCDC); \
- (e) += ROTATE_LEFT ((a), 5); \
- (b) = ROTATE_LEFT((b), 30); \
- }
- #define II(a, b, c, d, e, w) { \
- (e) += I ((b), (c), (d)) + (w) + (uint32_t)(0xCA62C1D6); \
- (e) += ROTATE_LEFT ((a), 5); \
- (b) = ROTATE_LEFT((b), 30); \
- }
- PHPAPI void PHP_SHA1InitArgs(PHP_SHA1_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
- {
- context->count[0] = context->count[1] = 0;
-
- context->state[0] = 0x67452301;
- context->state[1] = 0xefcdab89;
- context->state[2] = 0x98badcfe;
- context->state[3] = 0x10325476;
- context->state[4] = 0xc3d2e1f0;
- }
- PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
- size_t inputLen)
- {
- unsigned int i, index, partLen;
-
- index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
-
- if ((context->count[0] += ((uint32_t) inputLen << 3))
- < ((uint32_t) inputLen << 3))
- context->count[1]++;
- context->count[1] += ((uint32_t) inputLen >> 29);
- partLen = 64 - index;
-
- if (inputLen >= partLen) {
- memcpy
- ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
- SHA1Transform(context->state, context->buffer);
- for (i = partLen; i + 63 < inputLen; i += 64)
- SHA1Transform(context->state, &input[i]);
- index = 0;
- } else
- i = 0;
-
- memcpy
- ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
- inputLen - i);
- }
- PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
- {
- unsigned char bits[8];
- unsigned int index, padLen;
-
- bits[7] = context->count[0] & 0xFF;
- bits[6] = (context->count[0] >> 8) & 0xFF;
- bits[5] = (context->count[0] >> 16) & 0xFF;
- bits[4] = (context->count[0] >> 24) & 0xFF;
- bits[3] = context->count[1] & 0xFF;
- bits[2] = (context->count[1] >> 8) & 0xFF;
- bits[1] = (context->count[1] >> 16) & 0xFF;
- bits[0] = (context->count[1] >> 24) & 0xFF;
-
- index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
- padLen = (index < 56) ? (56 - index) : (120 - index);
- PHP_SHA1Update(context, PADDING, padLen);
-
- PHP_SHA1Update(context, bits, 8);
-
- SHA1Encode(digest, context->state, 20);
-
- ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
- }
- static void SHA1Transform(state, block)
- uint32_t state[5];
- const unsigned char block[64];
- {
- uint32_t a = state[0], b = state[1], c = state[2];
- uint32_t d = state[3], e = state[4], x[16], tmp;
- SHA1Decode(x, block, 64);
-
- FF(a, b, c, d, e, x[0]);
- FF(e, a, b, c, d, x[1]);
- FF(d, e, a, b, c, x[2]);
- FF(c, d, e, a, b, x[3]);
- FF(b, c, d, e, a, x[4]);
- FF(a, b, c, d, e, x[5]);
- FF(e, a, b, c, d, x[6]);
- FF(d, e, a, b, c, x[7]);
- FF(c, d, e, a, b, x[8]);
- FF(b, c, d, e, a, x[9]);
- FF(a, b, c, d, e, x[10]);
- FF(e, a, b, c, d, x[11]);
- FF(d, e, a, b, c, x[12]);
- FF(c, d, e, a, b, x[13]);
- FF(b, c, d, e, a, x[14]);
- FF(a, b, c, d, e, x[15]);
- FF(e, a, b, c, d, W(16));
- FF(d, e, a, b, c, W(17));
- FF(c, d, e, a, b, W(18));
- FF(b, c, d, e, a, W(19));
-
- GG(a, b, c, d, e, W(20));
- GG(e, a, b, c, d, W(21));
- GG(d, e, a, b, c, W(22));
- GG(c, d, e, a, b, W(23));
- GG(b, c, d, e, a, W(24));
- GG(a, b, c, d, e, W(25));
- GG(e, a, b, c, d, W(26));
- GG(d, e, a, b, c, W(27));
- GG(c, d, e, a, b, W(28));
- GG(b, c, d, e, a, W(29));
- GG(a, b, c, d, e, W(30));
- GG(e, a, b, c, d, W(31));
- GG(d, e, a, b, c, W(32));
- GG(c, d, e, a, b, W(33));
- GG(b, c, d, e, a, W(34));
- GG(a, b, c, d, e, W(35));
- GG(e, a, b, c, d, W(36));
- GG(d, e, a, b, c, W(37));
- GG(c, d, e, a, b, W(38));
- GG(b, c, d, e, a, W(39));
-
- HH(a, b, c, d, e, W(40));
- HH(e, a, b, c, d, W(41));
- HH(d, e, a, b, c, W(42));
- HH(c, d, e, a, b, W(43));
- HH(b, c, d, e, a, W(44));
- HH(a, b, c, d, e, W(45));
- HH(e, a, b, c, d, W(46));
- HH(d, e, a, b, c, W(47));
- HH(c, d, e, a, b, W(48));
- HH(b, c, d, e, a, W(49));
- HH(a, b, c, d, e, W(50));
- HH(e, a, b, c, d, W(51));
- HH(d, e, a, b, c, W(52));
- HH(c, d, e, a, b, W(53));
- HH(b, c, d, e, a, W(54));
- HH(a, b, c, d, e, W(55));
- HH(e, a, b, c, d, W(56));
- HH(d, e, a, b, c, W(57));
- HH(c, d, e, a, b, W(58));
- HH(b, c, d, e, a, W(59));
-
- II(a, b, c, d, e, W(60));
- II(e, a, b, c, d, W(61));
- II(d, e, a, b, c, W(62));
- II(c, d, e, a, b, W(63));
- II(b, c, d, e, a, W(64));
- II(a, b, c, d, e, W(65));
- II(e, a, b, c, d, W(66));
- II(d, e, a, b, c, W(67));
- II(c, d, e, a, b, W(68));
- II(b, c, d, e, a, W(69));
- II(a, b, c, d, e, W(70));
- II(e, a, b, c, d, W(71));
- II(d, e, a, b, c, W(72));
- II(c, d, e, a, b, W(73));
- II(b, c, d, e, a, W(74));
- II(a, b, c, d, e, W(75));
- II(e, a, b, c, d, W(76));
- II(d, e, a, b, c, W(77));
- II(c, d, e, a, b, W(78));
- II(b, c, d, e, a, W(79));
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
- state[4] += e;
-
- ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
- }
- static void SHA1Encode(output, input, len)
- unsigned char *output;
- uint32_t *input;
- unsigned int len;
- {
- unsigned int i, j;
- for (i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
- output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
- output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
- output[j + 3] = (unsigned char) (input[i] & 0xff);
- }
- }
- static void SHA1Decode(output, input, len)
- uint32_t *output;
- const unsigned char *input;
- unsigned int len;
- {
- unsigned int i, j;
- for (i = 0, j = 0; j < len; i++, j += 4)
- output[i] = ((uint32_t) input[j + 3]) | (((uint32_t) input[j + 2]) << 8) |
- (((uint32_t) input[j + 1]) << 16) | (((uint32_t) input[j]) << 24);
- }
|