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  * Copyright (C) Maxim Dounin
0004  * Copyright (C) Nginx, Inc.
0005  *
0006  * An internal SHA1 implementation.
0007  */
0008 
0009 
0010 #include <ngx_config.h>
0011 #include <ngx_core.h>
0012 #include <ngx_sha1.h>
0013 
0014 
0015 static const u_char *ngx_sha1_body(ngx_sha1_t *ctx, const u_char *data,
0016     size_t size);
0017 
0018 
0019 void
0020 ngx_sha1_init(ngx_sha1_t *ctx)
0021 {
0022     ctx->a = 0x67452301;
0023     ctx->b = 0xefcdab89;
0024     ctx->c = 0x98badcfe;
0025     ctx->d = 0x10325476;
0026     ctx->e = 0xc3d2e1f0;
0027 
0028     ctx->bytes = 0;
0029 }
0030 
0031 
0032 void
0033 ngx_sha1_update(ngx_sha1_t *ctx, const void *data, size_t size)
0034 {
0035     size_t  used, free;
0036 
0037     used = (size_t) (ctx->bytes & 0x3f);
0038     ctx->bytes += size;
0039 
0040     if (used) {
0041         free = 64 - used;
0042 
0043         if (size < free) {
0044             ngx_memcpy(&ctx->buffer[used], data, size);
0045             return;
0046         }
0047 
0048         ngx_memcpy(&ctx->buffer[used], data, free);
0049         data = (u_char *) data + free;
0050         size -= free;
0051         (void) ngx_sha1_body(ctx, ctx->buffer, 64);
0052     }
0053 
0054     if (size >= 64) {
0055         data = ngx_sha1_body(ctx, data, size & ~(size_t) 0x3f);
0056         size &= 0x3f;
0057     }
0058 
0059     ngx_memcpy(ctx->buffer, data, size);
0060 }
0061 
0062 
0063 void
0064 ngx_sha1_final(u_char result[20], ngx_sha1_t *ctx)
0065 {
0066     size_t  used, free;
0067 
0068     used = (size_t) (ctx->bytes & 0x3f);
0069 
0070     ctx->buffer[used++] = 0x80;
0071 
0072     free = 64 - used;
0073 
0074     if (free < 8) {
0075         ngx_memzero(&ctx->buffer[used], free);
0076         (void) ngx_sha1_body(ctx, ctx->buffer, 64);
0077         used = 0;
0078         free = 64;
0079     }
0080 
0081     ngx_memzero(&ctx->buffer[used], free - 8);
0082 
0083     ctx->bytes <<= 3;
0084     ctx->buffer[56] = (u_char) (ctx->bytes >> 56);
0085     ctx->buffer[57] = (u_char) (ctx->bytes >> 48);
0086     ctx->buffer[58] = (u_char) (ctx->bytes >> 40);
0087     ctx->buffer[59] = (u_char) (ctx->bytes >> 32);
0088     ctx->buffer[60] = (u_char) (ctx->bytes >> 24);
0089     ctx->buffer[61] = (u_char) (ctx->bytes >> 16);
0090     ctx->buffer[62] = (u_char) (ctx->bytes >> 8);
0091     ctx->buffer[63] = (u_char) ctx->bytes;
0092 
0093     (void) ngx_sha1_body(ctx, ctx->buffer, 64);
0094 
0095     result[0] = (u_char) (ctx->a >> 24);
0096     result[1] = (u_char) (ctx->a >> 16);
0097     result[2] = (u_char) (ctx->a >> 8);
0098     result[3] = (u_char) ctx->a;
0099     result[4] = (u_char) (ctx->b >> 24);
0100     result[5] = (u_char) (ctx->b >> 16);
0101     result[6] = (u_char) (ctx->b >> 8);
0102     result[7] = (u_char) ctx->b;
0103     result[8] = (u_char) (ctx->c >> 24);
0104     result[9] = (u_char) (ctx->c >> 16);
0105     result[10] = (u_char) (ctx->c >> 8);
0106     result[11] = (u_char) ctx->c;
0107     result[12] = (u_char) (ctx->d >> 24);
0108     result[13] = (u_char) (ctx->d >> 16);
0109     result[14] = (u_char) (ctx->d >> 8);
0110     result[15] = (u_char) ctx->d;
0111     result[16] = (u_char) (ctx->e >> 24);
0112     result[17] = (u_char) (ctx->e >> 16);
0113     result[18] = (u_char) (ctx->e >> 8);
0114     result[19] = (u_char) ctx->e;
0115 
0116     ngx_memzero(ctx, sizeof(*ctx));
0117 }
0118 
0119 
0120 /*
0121  * Helper functions.
0122  */
0123 
0124 #define ROTATE(bits, word)  (((word) << (bits)) | ((word) >> (32 - (bits))))
0125 
0126 #define F1(b, c, d)  (((b) & (c)) | ((~(b)) & (d)))
0127 #define F2(b, c, d)  ((b) ^ (c) ^ (d))
0128 #define F3(b, c, d)  (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
0129 
0130 #define STEP(f, a, b, c, d, e, w, t)                                          \
0131     temp = ROTATE(5, (a)) + f((b), (c), (d)) + (e) + (w) + (t);               \
0132     (e) = (d);                                                                \
0133     (d) = (c);                                                                \
0134     (c) = ROTATE(30, (b));                                                    \
0135     (b) = (a);                                                                \
0136     (a) = temp;
0137 
0138 
0139 /*
0140  * GET() reads 4 input bytes in big-endian byte order and returns
0141  * them as uint32_t.
0142  */
0143 
0144 #define GET(n)                                                                \
0145     ((uint32_t) p[n * 4 + 3] |                                                \
0146     ((uint32_t) p[n * 4 + 2] << 8) |                                          \
0147     ((uint32_t) p[n * 4 + 1] << 16) |                                         \
0148     ((uint32_t) p[n * 4] << 24))
0149 
0150 
0151 /*
0152  * This processes one or more 64-byte data blocks, but does not update
0153  * the bit counters.  There are no alignment requirements.
0154  */
0155 
0156 static const u_char *
0157 ngx_sha1_body(ngx_sha1_t *ctx, const u_char *data, size_t size)
0158 {
0159     uint32_t       a, b, c, d, e, temp;
0160     uint32_t       saved_a, saved_b, saved_c, saved_d, saved_e;
0161     uint32_t       words[80];
0162     ngx_uint_t     i;
0163     const u_char  *p;
0164 
0165     p = data;
0166 
0167     a = ctx->a;
0168     b = ctx->b;
0169     c = ctx->c;
0170     d = ctx->d;
0171     e = ctx->e;
0172 
0173     do {
0174         saved_a = a;
0175         saved_b = b;
0176         saved_c = c;
0177         saved_d = d;
0178         saved_e = e;
0179 
0180         /* Load data block into the words array */
0181 
0182         for (i = 0; i < 16; i++) {
0183             words[i] = GET(i);
0184         }
0185 
0186         for (i = 16; i < 80; i++) {
0187             words[i] = ROTATE(1, words[i - 3] ^ words[i - 8] ^ words[i - 14]
0188                                  ^ words[i - 16]);
0189         }
0190 
0191         /* Transformations */
0192 
0193         STEP(F1, a, b, c, d, e, words[0],  0x5a827999);
0194         STEP(F1, a, b, c, d, e, words[1],  0x5a827999);
0195         STEP(F1, a, b, c, d, e, words[2],  0x5a827999);
0196         STEP(F1, a, b, c, d, e, words[3],  0x5a827999);
0197         STEP(F1, a, b, c, d, e, words[4],  0x5a827999);
0198         STEP(F1, a, b, c, d, e, words[5],  0x5a827999);
0199         STEP(F1, a, b, c, d, e, words[6],  0x5a827999);
0200         STEP(F1, a, b, c, d, e, words[7],  0x5a827999);
0201         STEP(F1, a, b, c, d, e, words[8],  0x5a827999);
0202         STEP(F1, a, b, c, d, e, words[9],  0x5a827999);
0203         STEP(F1, a, b, c, d, e, words[10], 0x5a827999);
0204         STEP(F1, a, b, c, d, e, words[11], 0x5a827999);
0205         STEP(F1, a, b, c, d, e, words[12], 0x5a827999);
0206         STEP(F1, a, b, c, d, e, words[13], 0x5a827999);
0207         STEP(F1, a, b, c, d, e, words[14], 0x5a827999);
0208         STEP(F1, a, b, c, d, e, words[15], 0x5a827999);
0209         STEP(F1, a, b, c, d, e, words[16], 0x5a827999);
0210         STEP(F1, a, b, c, d, e, words[17], 0x5a827999);
0211         STEP(F1, a, b, c, d, e, words[18], 0x5a827999);
0212         STEP(F1, a, b, c, d, e, words[19], 0x5a827999);
0213 
0214         STEP(F2, a, b, c, d, e, words[20], 0x6ed9eba1);
0215         STEP(F2, a, b, c, d, e, words[21], 0x6ed9eba1);
0216         STEP(F2, a, b, c, d, e, words[22], 0x6ed9eba1);
0217         STEP(F2, a, b, c, d, e, words[23], 0x6ed9eba1);
0218         STEP(F2, a, b, c, d, e, words[24], 0x6ed9eba1);
0219         STEP(F2, a, b, c, d, e, words[25], 0x6ed9eba1);
0220         STEP(F2, a, b, c, d, e, words[26], 0x6ed9eba1);
0221         STEP(F2, a, b, c, d, e, words[27], 0x6ed9eba1);
0222         STEP(F2, a, b, c, d, e, words[28], 0x6ed9eba1);
0223         STEP(F2, a, b, c, d, e, words[29], 0x6ed9eba1);
0224         STEP(F2, a, b, c, d, e, words[30], 0x6ed9eba1);
0225         STEP(F2, a, b, c, d, e, words[31], 0x6ed9eba1);
0226         STEP(F2, a, b, c, d, e, words[32], 0x6ed9eba1);
0227         STEP(F2, a, b, c, d, e, words[33], 0x6ed9eba1);
0228         STEP(F2, a, b, c, d, e, words[34], 0x6ed9eba1);
0229         STEP(F2, a, b, c, d, e, words[35], 0x6ed9eba1);
0230         STEP(F2, a, b, c, d, e, words[36], 0x6ed9eba1);
0231         STEP(F2, a, b, c, d, e, words[37], 0x6ed9eba1);
0232         STEP(F2, a, b, c, d, e, words[38], 0x6ed9eba1);
0233         STEP(F2, a, b, c, d, e, words[39], 0x6ed9eba1);
0234 
0235         STEP(F3, a, b, c, d, e, words[40], 0x8f1bbcdc);
0236         STEP(F3, a, b, c, d, e, words[41], 0x8f1bbcdc);
0237         STEP(F3, a, b, c, d, e, words[42], 0x8f1bbcdc);
0238         STEP(F3, a, b, c, d, e, words[43], 0x8f1bbcdc);
0239         STEP(F3, a, b, c, d, e, words[44], 0x8f1bbcdc);
0240         STEP(F3, a, b, c, d, e, words[45], 0x8f1bbcdc);
0241         STEP(F3, a, b, c, d, e, words[46], 0x8f1bbcdc);
0242         STEP(F3, a, b, c, d, e, words[47], 0x8f1bbcdc);
0243         STEP(F3, a, b, c, d, e, words[48], 0x8f1bbcdc);
0244         STEP(F3, a, b, c, d, e, words[49], 0x8f1bbcdc);
0245         STEP(F3, a, b, c, d, e, words[50], 0x8f1bbcdc);
0246         STEP(F3, a, b, c, d, e, words[51], 0x8f1bbcdc);
0247         STEP(F3, a, b, c, d, e, words[52], 0x8f1bbcdc);
0248         STEP(F3, a, b, c, d, e, words[53], 0x8f1bbcdc);
0249         STEP(F3, a, b, c, d, e, words[54], 0x8f1bbcdc);
0250         STEP(F3, a, b, c, d, e, words[55], 0x8f1bbcdc);
0251         STEP(F3, a, b, c, d, e, words[56], 0x8f1bbcdc);
0252         STEP(F3, a, b, c, d, e, words[57], 0x8f1bbcdc);
0253         STEP(F3, a, b, c, d, e, words[58], 0x8f1bbcdc);
0254         STEP(F3, a, b, c, d, e, words[59], 0x8f1bbcdc);
0255 
0256         STEP(F2, a, b, c, d, e, words[60], 0xca62c1d6);
0257         STEP(F2, a, b, c, d, e, words[61], 0xca62c1d6);
0258         STEP(F2, a, b, c, d, e, words[62], 0xca62c1d6);
0259         STEP(F2, a, b, c, d, e, words[63], 0xca62c1d6);
0260         STEP(F2, a, b, c, d, e, words[64], 0xca62c1d6);
0261         STEP(F2, a, b, c, d, e, words[65], 0xca62c1d6);
0262         STEP(F2, a, b, c, d, e, words[66], 0xca62c1d6);
0263         STEP(F2, a, b, c, d, e, words[67], 0xca62c1d6);
0264         STEP(F2, a, b, c, d, e, words[68], 0xca62c1d6);
0265         STEP(F2, a, b, c, d, e, words[69], 0xca62c1d6);
0266         STEP(F2, a, b, c, d, e, words[70], 0xca62c1d6);
0267         STEP(F2, a, b, c, d, e, words[71], 0xca62c1d6);
0268         STEP(F2, a, b, c, d, e, words[72], 0xca62c1d6);
0269         STEP(F2, a, b, c, d, e, words[73], 0xca62c1d6);
0270         STEP(F2, a, b, c, d, e, words[74], 0xca62c1d6);
0271         STEP(F2, a, b, c, d, e, words[75], 0xca62c1d6);
0272         STEP(F2, a, b, c, d, e, words[76], 0xca62c1d6);
0273         STEP(F2, a, b, c, d, e, words[77], 0xca62c1d6);
0274         STEP(F2, a, b, c, d, e, words[78], 0xca62c1d6);
0275         STEP(F2, a, b, c, d, e, words[79], 0xca62c1d6);
0276 
0277         a += saved_a;
0278         b += saved_b;
0279         c += saved_c;
0280         d += saved_d;
0281         e += saved_e;
0282 
0283         p += 64;
0284 
0285     } while (size -= 64);
0286 
0287     ctx->a = a;
0288     ctx->b = b;
0289     ctx->c = c;
0290     ctx->d = d;
0291     ctx->e = e;
0292 
0293     return p;
0294 }