123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #include "libbb.h"
- #if CONFIG_MD5_SMALL < 1
- # define MD5_SMALL 1
- #elif CONFIG_MD5_SMALL > 3
- # define MD5_SMALL 3
- #else
- # define MD5_SMALL CONFIG_MD5_SMALL
- #endif
- #if BB_LITTLE_ENDIAN
- #define memcpy32_cpu2le memcpy
- #define memcpy32_le2cpu memcpy
- #else
- static void
- memcpy32_cpu2le(unsigned char *output, uint32_t *input, unsigned len)
- {
- unsigned i, j;
- for (i = 0, j = 0; j < len; i++, j += 4) {
- output[j] = input[i];
- output[j+1] = (input[i] >> 8);
- output[j+2] = (input[i] >> 16);
- output[j+3] = (input[i] >> 24);
- }
- }
- static void
- memcpy32_le2cpu(uint32_t *output, const unsigned char *input, unsigned len)
- {
- unsigned i, j;
- for (i = 0, j = 0; j < len; i++, j += 4)
- output[i] = ((uint32_t)input[j])
- | (((uint32_t)input[j+1]) << 8)
- | (((uint32_t)input[j+2]) << 16)
- | (((uint32_t)input[j+3]) << 24);
- }
- #endif
- #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
- #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
- #define H(x, y, z) ((x) ^ (y) ^ (z))
- #define I(x, y, z) ((y) ^ ((x) | ~(z)))
- #define rotl32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
- #define FF(a, b, c, d, x, s, ac) { \
- (a) += F((b), (c), (d)) + (x) + (uint32_t)(ac); \
- (a) = rotl32((a), (s)); \
- (a) += (b); \
- }
- #define GG(a, b, c, d, x, s, ac) { \
- (a) += G((b), (c), (d)) + (x) + (uint32_t)(ac); \
- (a) = rotl32((a), (s)); \
- (a) += (b); \
- }
- #define HH(a, b, c, d, x, s, ac) { \
- (a) += H((b), (c), (d)) + (x) + (uint32_t)(ac); \
- (a) = rotl32((a), (s)); \
- (a) += (b); \
- }
- #define II(a, b, c, d, x, s, ac) { \
- (a) += I((b), (c), (d)) + (x) + (uint32_t)(ac); \
- (a) = rotl32((a), (s)); \
- (a) += (b); \
- }
- static void md5_transform(uint32_t state[4], const unsigned char block[64])
- {
- uint32_t a, b, c, d, x[16];
- #if MD5_SMALL > 1
- uint32_t temp;
- const unsigned char *ps;
- static const unsigned char S[] = {
- 7, 12, 17, 22,
- 5, 9, 14, 20,
- 4, 11, 16, 23,
- 6, 10, 15, 21
- };
- #endif
- #if MD5_SMALL > 0
- const uint32_t *pc;
- const unsigned char *pp;
- int i;
- static const uint32_t C[] = {
-
- 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
- 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
- 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
- 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
-
- 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
- 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
- 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
- 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
-
- 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
- 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
- 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
- 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
-
- 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
- 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
- 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
- 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
- };
- static const unsigned char P[] = {
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
- 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
- 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
- 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9
- };
- #endif
- memcpy32_le2cpu(x, block, 64);
- a = state[0];
- b = state[1];
- c = state[2];
- d = state[3];
- #if MD5_SMALL > 2
- pc = C;
- pp = P;
- ps = S - 4;
- for (i = 0; i < 64; i++) {
- if ((i & 0x0f) == 0) ps += 4;
- temp = a;
- switch (i>>4) {
- case 0:
- temp += F(b, c, d);
- break;
- case 1:
- temp += G(b, c, d);
- break;
- case 2:
- temp += H(b, c, d);
- break;
- case 3:
- temp += I(b, c, d);
- break;
- }
- temp += x[*pp++] + *pc++;
- temp = rotl32(temp, ps[i & 3]);
- temp += b;
- a = d; d = c; c = b; b = temp;
- }
- #elif MD5_SMALL > 1
- pc = C;
- pp = P;
- ps = S;
-
- for (i = 0; i < 16; i++) {
- FF(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
- temp = d; d = c; c = b; b = a; a = temp;
- }
-
- ps += 4;
- for (; i < 32; i++) {
- GG(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
- temp = d; d = c; c = b; b = a; a = temp;
- }
-
- ps += 4;
- for (; i < 48; i++) {
- HH(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
- temp = d; d = c; c = b; b = a; a = temp;
- }
-
- ps += 4;
- for (; i < 64; i++) {
- II(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
- temp = d; d = c; c = b; b = a; a = temp;
- }
- #elif MD5_SMALL > 0
- pc = C;
- pp = P;
-
- for (i = 0; i < 4; i++) {
- FF(a, b, c, d, x[*pp], 7, *pc); pp++; pc++;
- FF(d, a, b, c, x[*pp], 12, *pc); pp++; pc++;
- FF(c, d, a, b, x[*pp], 17, *pc); pp++; pc++;
- FF(b, c, d, a, x[*pp], 22, *pc); pp++; pc++;
- }
-
- for (i = 0; i < 4; i++) {
- GG(a, b, c, d, x[*pp], 5, *pc); pp++; pc++;
- GG(d, a, b, c, x[*pp], 9, *pc); pp++; pc++;
- GG(c, d, a, b, x[*pp], 14, *pc); pp++; pc++;
- GG(b, c, d, a, x[*pp], 20, *pc); pp++; pc++;
- }
-
- for (i = 0; i < 4; i++) {
- HH(a, b, c, d, x[*pp], 4, *pc); pp++; pc++;
- HH(d, a, b, c, x[*pp], 11, *pc); pp++; pc++;
- HH(c, d, a, b, x[*pp], 16, *pc); pp++; pc++;
- HH(b, c, d, a, x[*pp], 23, *pc); pp++; pc++;
- }
-
- for (i = 0; i < 4; i++) {
- II(a, b, c, d, x[*pp], 6, *pc); pp++; pc++;
- II(d, a, b, c, x[*pp], 10, *pc); pp++; pc++;
- II(c, d, a, b, x[*pp], 15, *pc); pp++; pc++;
- II(b, c, d, a, x[*pp], 21, *pc); pp++; pc++;
- }
- #else
-
- #define S11 7
- #define S12 12
- #define S13 17
- #define S14 22
- FF(a, b, c, d, x[ 0], S11, 0xd76aa478);
- FF(d, a, b, c, x[ 1], S12, 0xe8c7b756);
- FF(c, d, a, b, x[ 2], S13, 0x242070db);
- FF(b, c, d, a, x[ 3], S14, 0xc1bdceee);
- FF(a, b, c, d, x[ 4], S11, 0xf57c0faf);
- FF(d, a, b, c, x[ 5], S12, 0x4787c62a);
- FF(c, d, a, b, x[ 6], S13, 0xa8304613);
- FF(b, c, d, a, x[ 7], S14, 0xfd469501);
- FF(a, b, c, d, x[ 8], S11, 0x698098d8);
- FF(d, a, b, c, x[ 9], S12, 0x8b44f7af);
- FF(c, d, a, b, x[10], S13, 0xffff5bb1);
- FF(b, c, d, a, x[11], S14, 0x895cd7be);
- FF(a, b, c, d, x[12], S11, 0x6b901122);
- FF(d, a, b, c, x[13], S12, 0xfd987193);
- FF(c, d, a, b, x[14], S13, 0xa679438e);
- FF(b, c, d, a, x[15], S14, 0x49b40821);
-
- #define S21 5
- #define S22 9
- #define S23 14
- #define S24 20
- GG(a, b, c, d, x[ 1], S21, 0xf61e2562);
- GG(d, a, b, c, x[ 6], S22, 0xc040b340);
- GG(c, d, a, b, x[11], S23, 0x265e5a51);
- GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
- GG(a, b, c, d, x[ 5], S21, 0xd62f105d);
- GG(d, a, b, c, x[10], S22, 0x2441453);
- GG(c, d, a, b, x[15], S23, 0xd8a1e681);
- GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
- GG(a, b, c, d, x[ 9], S21, 0x21e1cde6);
- GG(d, a, b, c, x[14], S22, 0xc33707d6);
- GG(c, d, a, b, x[ 3], S23, 0xf4d50d87);
- GG(b, c, d, a, x[ 8], S24, 0x455a14ed);
- GG(a, b, c, d, x[13], S21, 0xa9e3e905);
- GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8);
- GG(c, d, a, b, x[ 7], S23, 0x676f02d9);
- GG(b, c, d, a, x[12], S24, 0x8d2a4c8a);
-
- #define S31 4
- #define S32 11
- #define S33 16
- #define S34 23
- HH(a, b, c, d, x[ 5], S31, 0xfffa3942);
- HH(d, a, b, c, x[ 8], S32, 0x8771f681);
- HH(c, d, a, b, x[11], S33, 0x6d9d6122);
- HH(b, c, d, a, x[14], S34, 0xfde5380c);
- HH(a, b, c, d, x[ 1], S31, 0xa4beea44);
- HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9);
- HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60);
- HH(b, c, d, a, x[10], S34, 0xbebfbc70);
- HH(a, b, c, d, x[13], S31, 0x289b7ec6);
- HH(d, a, b, c, x[ 0], S32, 0xeaa127fa);
- HH(c, d, a, b, x[ 3], S33, 0xd4ef3085);
- HH(b, c, d, a, x[ 6], S34, 0x4881d05);
- HH(a, b, c, d, x[ 9], S31, 0xd9d4d039);
- HH(d, a, b, c, x[12], S32, 0xe6db99e5);
- HH(c, d, a, b, x[15], S33, 0x1fa27cf8);
- HH(b, c, d, a, x[ 2], S34, 0xc4ac5665);
-
- #define S41 6
- #define S42 10
- #define S43 15
- #define S44 21
- II(a, b, c, d, x[ 0], S41, 0xf4292244);
- II(d, a, b, c, x[ 7], S42, 0x432aff97);
- II(c, d, a, b, x[14], S43, 0xab9423a7);
- II(b, c, d, a, x[ 5], S44, 0xfc93a039);
- II(a, b, c, d, x[12], S41, 0x655b59c3);
- II(d, a, b, c, x[ 3], S42, 0x8f0ccc92);
- II(c, d, a, b, x[10], S43, 0xffeff47d);
- II(b, c, d, a, x[ 1], S44, 0x85845dd1);
- II(a, b, c, d, x[ 8], S41, 0x6fa87e4f);
- II(d, a, b, c, x[15], S42, 0xfe2ce6e0);
- II(c, d, a, b, x[ 6], S43, 0xa3014314);
- II(b, c, d, a, x[13], S44, 0x4e0811a1);
- II(a, b, c, d, x[ 4], S41, 0xf7537e82);
- II(d, a, b, c, x[11], S42, 0xbd3af235);
- II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
- II(b, c, d, a, x[ 9], S44, 0xeb86d391);
- #endif
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
-
- memset(x, 0, sizeof(x));
- }
- void FAST_FUNC md5_begin(md5_ctx_t *context)
- {
- context->count[0] = context->count[1] = 0;
-
- context->state[0] = 0x67452301;
- context->state[1] = 0xefcdab89;
- context->state[2] = 0x98badcfe;
- context->state[3] = 0x10325476;
- }
- void FAST_FUNC md5_hash(const void *buffer, size_t inputLen, md5_ctx_t *context)
- {
- unsigned i, idx, partLen;
- const unsigned char *input = buffer;
-
- idx = (context->count[0] >> 3) & 0x3F;
-
- context->count[0] += (inputLen << 3);
- if (context->count[0] < (inputLen << 3))
- context->count[1]++;
- context->count[1] += (inputLen >> 29);
-
- i = 0;
- partLen = 64 - idx;
- if (inputLen >= partLen) {
- memcpy(&context->buffer[idx], input, partLen);
- md5_transform(context->state, context->buffer);
- for (i = partLen; i + 63 < inputLen; i += 64)
- md5_transform(context->state, &input[i]);
- idx = 0;
- }
-
- memcpy(&context->buffer[idx], &input[i], inputLen - i);
- }
- unsigned FAST_FUNC md5_end(void *digest, md5_ctx_t *context)
- {
- unsigned idx, padLen;
- unsigned char bits[8];
- unsigned char padding[64];
-
- memset(padding, 0, sizeof(padding));
- padding[0] = 0x80;
-
- memcpy32_cpu2le(bits, context->count, 8);
-
- idx = (context->count[0] >> 3) & 0x3f;
- padLen = (idx < 56) ? (56 - idx) : (120 - idx);
- md5_hash(padding, padLen, context);
-
- md5_hash(bits, 8, context);
-
- memcpy32_cpu2le(digest, context->state, 16);
- return 16;
- }
|