Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * An internal implementation, based on Alexander Peslyak's
0004  * public domain implementation:
0005  * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
0006  */
0007 
0008 
0009 #include <ngx_config.h>
0010 #include <ngx_core.h>
0011 #include <ngx_md5.h>
0012 
0013 
0014 static const u_char *ngx_md5_body(ngx_md5_t *ctx, const u_char *data,
0015     size_t size);
0016 
0017 
0018 void
0019 ngx_md5_init(ngx_md5_t *ctx)
0020 {
0021     ctx->a = 0x67452301;
0022     ctx->b = 0xefcdab89;
0023     ctx->c = 0x98badcfe;
0024     ctx->d = 0x10325476;
0025 
0026     ctx->bytes = 0;
0027 }
0028 
0029 
0030 void
0031 ngx_md5_update(ngx_md5_t *ctx, const void *data, size_t size)
0032 {
0033     size_t  used, free;
0034 
0035     used = (size_t) (ctx->bytes & 0x3f);
0036     ctx->bytes += size;
0037 
0038     if (used) {
0039         free = 64 - used;
0040 
0041         if (size < free) {
0042             ngx_memcpy(&ctx->buffer[used], data, size);
0043             return;
0044         }
0045 
0046         ngx_memcpy(&ctx->buffer[used], data, free);
0047         data = (u_char *) data + free;
0048         size -= free;
0049         (void) ngx_md5_body(ctx, ctx->buffer, 64);
0050     }
0051 
0052     if (size >= 64) {
0053         data = ngx_md5_body(ctx, data, size & ~(size_t) 0x3f);
0054         size &= 0x3f;
0055     }
0056 
0057     ngx_memcpy(ctx->buffer, data, size);
0058 }
0059 
0060 
0061 void
0062 ngx_md5_final(u_char result[16], ngx_md5_t *ctx)
0063 {
0064     size_t  used, free;
0065 
0066     used = (size_t) (ctx->bytes & 0x3f);
0067 
0068     ctx->buffer[used++] = 0x80;
0069 
0070     free = 64 - used;
0071 
0072     if (free < 8) {
0073         ngx_memzero(&ctx->buffer[used], free);
0074         (void) ngx_md5_body(ctx, ctx->buffer, 64);
0075         used = 0;
0076         free = 64;
0077     }
0078 
0079     ngx_memzero(&ctx->buffer[used], free - 8);
0080 
0081     ctx->bytes <<= 3;
0082     ctx->buffer[56] = (u_char) ctx->bytes;
0083     ctx->buffer[57] = (u_char) (ctx->bytes >> 8);
0084     ctx->buffer[58] = (u_char) (ctx->bytes >> 16);
0085     ctx->buffer[59] = (u_char) (ctx->bytes >> 24);
0086     ctx->buffer[60] = (u_char) (ctx->bytes >> 32);
0087     ctx->buffer[61] = (u_char) (ctx->bytes >> 40);
0088     ctx->buffer[62] = (u_char) (ctx->bytes >> 48);
0089     ctx->buffer[63] = (u_char) (ctx->bytes >> 56);
0090 
0091     (void) ngx_md5_body(ctx, ctx->buffer, 64);
0092 
0093     result[0] = (u_char) ctx->a;
0094     result[1] = (u_char) (ctx->a >> 8);
0095     result[2] = (u_char) (ctx->a >> 16);
0096     result[3] = (u_char) (ctx->a >> 24);
0097     result[4] = (u_char) ctx->b;
0098     result[5] = (u_char) (ctx->b >> 8);
0099     result[6] = (u_char) (ctx->b >> 16);
0100     result[7] = (u_char) (ctx->b >> 24);
0101     result[8] = (u_char) ctx->c;
0102     result[9] = (u_char) (ctx->c >> 8);
0103     result[10] = (u_char) (ctx->c >> 16);
0104     result[11] = (u_char) (ctx->c >> 24);
0105     result[12] = (u_char) ctx->d;
0106     result[13] = (u_char) (ctx->d >> 8);
0107     result[14] = (u_char) (ctx->d >> 16);
0108     result[15] = (u_char) (ctx->d >> 24);
0109 
0110     ngx_memzero(ctx, sizeof(*ctx));
0111 }
0112 
0113 
0114 /*
0115  * The basic MD5 functions.
0116  *
0117  * F and G are optimized compared to their RFC 1321 definitions for
0118  * architectures that lack an AND-NOT instruction, just like in
0119  * Colin Plumb's implementation.
0120  */
0121 
0122 #define F(x, y, z)  ((z) ^ ((x) & ((y) ^ (z))))
0123 #define G(x, y, z)  ((y) ^ ((z) & ((x) ^ (y))))
0124 #define H(x, y, z)  ((x) ^ (y) ^ (z))
0125 #define I(x, y, z)  ((y) ^ ((x) | ~(z)))
0126 
0127 /*
0128  * The MD5 transformation for all four rounds.
0129  */
0130 
0131 #define STEP(f, a, b, c, d, x, t, s)                                          \
0132     (a) += f((b), (c), (d)) + (x) + (t);                                      \
0133     (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));                \
0134     (a) += (b)
0135 
0136 /*
0137  * SET() reads 4 input bytes in little-endian byte order and stores them
0138  * in a properly aligned word in host byte order.
0139  *
0140  * The check for little-endian architectures that tolerate unaligned
0141  * memory accesses is just an optimization.  Nothing will break if it
0142  * does not work.
0143  */
0144 
0145 #if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
0146 
0147 #define SET(n)      (*(uint32_t *) &p[n * 4])
0148 #define GET(n)      (*(uint32_t *) &p[n * 4])
0149 
0150 #else
0151 
0152 #define SET(n)                                                                \
0153     (block[n] =                                                               \
0154     (uint32_t) p[n * 4] |                                                     \
0155     ((uint32_t) p[n * 4 + 1] << 8) |                                          \
0156     ((uint32_t) p[n * 4 + 2] << 16) |                                         \
0157     ((uint32_t) p[n * 4 + 3] << 24))
0158 
0159 #define GET(n)      block[n]
0160 
0161 #endif
0162 
0163 
0164 /*
0165  * This processes one or more 64-byte data blocks, but does not update
0166  * the bit counters.  There are no alignment requirements.
0167  */
0168 
0169 static const u_char *
0170 ngx_md5_body(ngx_md5_t *ctx, const u_char *data, size_t size)
0171 {
0172     uint32_t       a, b, c, d;
0173     uint32_t       saved_a, saved_b, saved_c, saved_d;
0174     const u_char  *p;
0175 #if !(NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
0176     uint32_t       block[16];
0177 #endif
0178 
0179     p = data;
0180 
0181     a = ctx->a;
0182     b = ctx->b;
0183     c = ctx->c;
0184     d = ctx->d;
0185 
0186     do {
0187         saved_a = a;
0188         saved_b = b;
0189         saved_c = c;
0190         saved_d = d;
0191 
0192         /* Round 1 */
0193 
0194         STEP(F, a, b, c, d, SET(0),  0xd76aa478, 7);
0195         STEP(F, d, a, b, c, SET(1),  0xe8c7b756, 12);
0196         STEP(F, c, d, a, b, SET(2),  0x242070db, 17);
0197         STEP(F, b, c, d, a, SET(3),  0xc1bdceee, 22);
0198         STEP(F, a, b, c, d, SET(4),  0xf57c0faf, 7);
0199         STEP(F, d, a, b, c, SET(5),  0x4787c62a, 12);
0200         STEP(F, c, d, a, b, SET(6),  0xa8304613, 17);
0201         STEP(F, b, c, d, a, SET(7),  0xfd469501, 22);
0202         STEP(F, a, b, c, d, SET(8),  0x698098d8, 7);
0203         STEP(F, d, a, b, c, SET(9),  0x8b44f7af, 12);
0204         STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17);
0205         STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22);
0206         STEP(F, a, b, c, d, SET(12), 0x6b901122, 7);
0207         STEP(F, d, a, b, c, SET(13), 0xfd987193, 12);
0208         STEP(F, c, d, a, b, SET(14), 0xa679438e, 17);
0209         STEP(F, b, c, d, a, SET(15), 0x49b40821, 22);
0210 
0211         /* Round 2 */
0212 
0213         STEP(G, a, b, c, d, GET(1),  0xf61e2562, 5);
0214         STEP(G, d, a, b, c, GET(6),  0xc040b340, 9);
0215         STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14);
0216         STEP(G, b, c, d, a, GET(0),  0xe9b6c7aa, 20);
0217         STEP(G, a, b, c, d, GET(5),  0xd62f105d, 5);
0218         STEP(G, d, a, b, c, GET(10), 0x02441453, 9);
0219         STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14);
0220         STEP(G, b, c, d, a, GET(4),  0xe7d3fbc8, 20);
0221         STEP(G, a, b, c, d, GET(9),  0x21e1cde6, 5);
0222         STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9);
0223         STEP(G, c, d, a, b, GET(3),  0xf4d50d87, 14);
0224         STEP(G, b, c, d, a, GET(8),  0x455a14ed, 20);
0225         STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5);
0226         STEP(G, d, a, b, c, GET(2),  0xfcefa3f8, 9);
0227         STEP(G, c, d, a, b, GET(7),  0x676f02d9, 14);
0228         STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20);
0229 
0230         /* Round 3 */
0231 
0232         STEP(H, a, b, c, d, GET(5),  0xfffa3942, 4);
0233         STEP(H, d, a, b, c, GET(8),  0x8771f681, 11);
0234         STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16);
0235         STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23);
0236         STEP(H, a, b, c, d, GET(1),  0xa4beea44, 4);
0237         STEP(H, d, a, b, c, GET(4),  0x4bdecfa9, 11);
0238         STEP(H, c, d, a, b, GET(7),  0xf6bb4b60, 16);
0239         STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23);
0240         STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4);
0241         STEP(H, d, a, b, c, GET(0),  0xeaa127fa, 11);
0242         STEP(H, c, d, a, b, GET(3),  0xd4ef3085, 16);
0243         STEP(H, b, c, d, a, GET(6),  0x04881d05, 23);
0244         STEP(H, a, b, c, d, GET(9),  0xd9d4d039, 4);
0245         STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11);
0246         STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16);
0247         STEP(H, b, c, d, a, GET(2),  0xc4ac5665, 23);
0248 
0249         /* Round 4 */
0250 
0251         STEP(I, a, b, c, d, GET(0),  0xf4292244, 6);
0252         STEP(I, d, a, b, c, GET(7),  0x432aff97, 10);
0253         STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15);
0254         STEP(I, b, c, d, a, GET(5),  0xfc93a039, 21);
0255         STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6);
0256         STEP(I, d, a, b, c, GET(3),  0x8f0ccc92, 10);
0257         STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15);
0258         STEP(I, b, c, d, a, GET(1),  0x85845dd1, 21);
0259         STEP(I, a, b, c, d, GET(8),  0x6fa87e4f, 6);
0260         STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10);
0261         STEP(I, c, d, a, b, GET(6),  0xa3014314, 15);
0262         STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21);
0263         STEP(I, a, b, c, d, GET(4),  0xf7537e82, 6);
0264         STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10);
0265         STEP(I, c, d, a, b, GET(2),  0x2ad7d2bb, 15);
0266         STEP(I, b, c, d, a, GET(9),  0xeb86d391, 21);
0267 
0268         a += saved_a;
0269         b += saved_b;
0270         c += saved_c;
0271         d += saved_d;
0272 
0273         p += 64;
0274 
0275     } while (size -= 64);
0276 
0277     ctx->a = a;
0278     ctx->b = b;
0279     ctx->c = c;
0280     ctx->d = d;
0281 
0282     return p;
0283 }