GCC Wikia
Advertisement

このページを編集する際は,編集に関する方針に従ってください.[]

概要[]

引数[]

実装[]

  257: /* These are the four functions used in the four steps of the MD5 algorithm
  258:    and defined in the RFC 1321.  The first function is a little bit optimized
  259:    (as found in Colin Plumbs public domain implementation).  */
  260: /* #define FF(b, c, d) ((b & c) | (~b & d)) */
>最初に、3つの32ビットワードを入力し、1つの32ビットワードを出力
>する 4つの補助関数を定義する。

261: #define FF(b, c, d) (d ^ (b & (c ^ d)))
262: #define FG(b, c, d) FF (d, b, c)
263: #define FH(b, c, d) (b ^ c ^ d)
264: #define FI(b, c, d) (c ^ (b | ~d))
265: 

~
~

  266: /* Process LEN bytes of BUFFER, accumulating context into CTX.
  267:    It is assumed that LEN % 64 == 0.  */
268: 
269: void
270: md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
271: {
272:   md5_uint32 correct_words[16];
273:   const md5_uint32 *words = (const md5_uint32 *) buffer;
274:   size_t nwords = len / sizeof (md5_uint32);
275:   const md5_uint32 *endp = words + nwords;
276:   md5_uint32 A = ctx->A;
277:   md5_uint32 B = ctx->B;
278:   md5_uint32 C = ctx->C;
279:   md5_uint32 D = ctx->D;
280: 
  281:   /* First increment the byte count.  RFC 1321 specifies the possible
  282:      length of the file up to 2^64 bits.  Here we only compute the
  283:      number of bytes.  Do a double word increment.  */
284:   ctx->total[0] += len;
285:   if (ctx->total[0] < len)
286:     ++ctx->total[1];
287: 
  288:   /* Process all bytes in the buffer with 64 bytes in each round of
  289:      the loop.  */
290:   while (words < endp)
291:     {
292:       md5_uint32 *cwp = correct_words;

>AをAA、BをBB、CをCC、DをDDとして保存

293:       md5_uint32 A_save = A;
294:       md5_uint32 B_save = B;
295:       md5_uint32 C_save = C;
296:       md5_uint32 D_save = D;
297: 

~
~

  298:       /* First round: using the given function, the context and a constant
  299:          the next context is computed.  Because the algorithms processing
  300:          unit is a 32-bit word and it is determined to work on words in
  301:          little endian byte order we perhaps have to change the byte order
  302:          before the computation.  To reduce the work for the next steps
  303:          we store the swapped words in the array CORRECT_WORDS.  */
304: 
305: #define OP(a, b, c, d, s, T)                                            \
306:       do                                                                \
307:         {                                                               \
308:           a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;            \
309:           ++words;                                                     \
310:           CYCLIC (a, s);                                               \
311:           a += b;                                                      \
312:         }                                                               \
313:       while (0)
314: 
  315:       /* It is unfortunate that C does not provide an operator for
  316:          cyclic rotation.  Hope the C compiler is smart enough.  */
317: #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
318: 
  319:       /* Before we start, one word to the strange constants.
  320:          They are defined in RFC 1321 as
  321: 
  322:          T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  323:        */
324: 
  325:       /* Round 1.  */
326:       OP (A, B, C, D,  7, (md5_uint32) 0xd76aa478);
327:       OP (D, A, B, C, 12, (md5_uint32) 0xe8c7b756);
328:       OP (C, D, A, B, 17, (md5_uint32) 0x242070db);
329:       OP (B, C, D, A, 22, (md5_uint32) 0xc1bdceee);
330:       OP (A, B, C, D,  7, (md5_uint32) 0xf57c0faf);
331:       OP (D, A, B, C, 12, (md5_uint32) 0x4787c62a);
332:       OP (C, D, A, B, 17, (md5_uint32) 0xa8304613);
333:       OP (B, C, D, A, 22, (md5_uint32) 0xfd469501);
334:       OP (A, B, C, D,  7, (md5_uint32) 0x698098d8);
335:       OP (D, A, B, C, 12, (md5_uint32) 0x8b44f7af);
336:       OP (C, D, A, B, 17, (md5_uint32) 0xffff5bb1);
337:       OP (B, C, D, A, 22, (md5_uint32) 0x895cd7be);
338:       OP (A, B, C, D,  7, (md5_uint32) 0x6b901122);
339:       OP (D, A, B, C, 12, (md5_uint32) 0xfd987193);
340:       OP (C, D, A, B, 17, (md5_uint32) 0xa679438e);
341:       OP (B, C, D, A, 22, (md5_uint32) 0x49b40821);
342: 

~
~

  343:       /* For the second to fourth round we have the possibly swapped words
  344:          in CORRECT_WORDS.  Redefine the macro to take an additional first
  345:          argument specifying the function to use.  */
346: #undef OP
347: #define OP(a, b, c, d, k, s, T)                                         \
348:       do                                                                \
349:         {                                                              \
350:           a += FX (b, c, d) + correct_words[k] + T;                    \
351:           CYCLIC (a, s);                                               \
352:           a += b;                                                      \
353:         }                                                              \
354:       while (0)
355: 
356: #define FX(b, c, d) FG (b, c, d)
357: 
  358:       /* Round 2.  */
359:       OP (A, B, C, D,  1,  5, (md5_uint32) 0xf61e2562);
360:       OP (D, A, B, C,  6,  9, (md5_uint32) 0xc040b340);
361:       OP (C, D, A, B, 11, 14, (md5_uint32) 0x265e5a51);
362:       OP (B, C, D, A,  0, 20, (md5_uint32) 0xe9b6c7aa);
363:       OP (A, B, C, D,  5,  5, (md5_uint32) 0xd62f105d);
364:       OP (D, A, B, C, 10,  9, (md5_uint32) 0x02441453);
365:       OP (C, D, A, B, 15, 14, (md5_uint32) 0xd8a1e681);
366:       OP (B, C, D, A,  4, 20, (md5_uint32) 0xe7d3fbc8);
367:       OP (A, B, C, D,  9,  5, (md5_uint32) 0x21e1cde6);
368:       OP (D, A, B, C, 14,  9, (md5_uint32) 0xc33707d6);
369:       OP (C, D, A, B,  3, 14, (md5_uint32) 0xf4d50d87);
370:       OP (B, C, D, A,  8, 20, (md5_uint32) 0x455a14ed);
371:       OP (A, B, C, D, 13,  5, (md5_uint32) 0xa9e3e905);
372:       OP (D, A, B, C,  2,  9, (md5_uint32) 0xfcefa3f8);
373:       OP (C, D, A, B,  7, 14, (md5_uint32) 0x676f02d9);
374:       OP (B, C, D, A, 12, 20, (md5_uint32) 0x8d2a4c8a);
375:

~
~
 
376: #undef FX
377: #define FX(b, c, d) FH (b, c, d)
378: 
  379:       /* Round 3.  */
380:       OP (A, B, C, D,  5,  4, (md5_uint32) 0xfffa3942);
381:       OP (D, A, B, C,  8, 11, (md5_uint32) 0x8771f681);
382:       OP (C, D, A, B, 11, 16, (md5_uint32) 0x6d9d6122);
383:       OP (B, C, D, A, 14, 23, (md5_uint32) 0xfde5380c);
384:       OP (A, B, C, D,  1,  4, (md5_uint32) 0xa4beea44);
385:       OP (D, A, B, C,  4, 11, (md5_uint32) 0x4bdecfa9);
386:       OP (C, D, A, B,  7, 16, (md5_uint32) 0xf6bb4b60);
387:       OP (B, C, D, A, 10, 23, (md5_uint32) 0xbebfbc70);
388:       OP (A, B, C, D, 13,  4, (md5_uint32) 0x289b7ec6);
389:       OP (D, A, B, C,  0, 11, (md5_uint32) 0xeaa127fa);
390:       OP (C, D, A, B,  3, 16, (md5_uint32) 0xd4ef3085);
391:       OP (B, C, D, A,  6, 23, (md5_uint32) 0x04881d05);
392:       OP (A, B, C, D,  9,  4, (md5_uint32) 0xd9d4d039);
393:       OP (D, A, B, C, 12, 11, (md5_uint32) 0xe6db99e5);
394:       OP (C, D, A, B, 15, 16, (md5_uint32) 0x1fa27cf8);
395:       OP (B, C, D, A,  2, 23, (md5_uint32) 0xc4ac5665);
396: 

~
~

397: #undef FX
398: #define FX(b, c, d) FI (b, c, d)
399: 
  400:       /* Round 4.  */
401:       OP (A, B, C, D,  0,  6, (md5_uint32) 0xf4292244);
402:       OP (D, A, B, C,  7, 10, (md5_uint32) 0x432aff97);
403:       OP (C, D, A, B, 14, 15, (md5_uint32) 0xab9423a7);
404:       OP (B, C, D, A,  5, 21, (md5_uint32) 0xfc93a039);
405:       OP (A, B, C, D, 12,  6, (md5_uint32) 0x655b59c3);
406:       OP (D, A, B, C,  3, 10, (md5_uint32) 0x8f0ccc92);
407:       OP (C, D, A, B, 10, 15, (md5_uint32) 0xffeff47d);
408:       OP (B, C, D, A,  1, 21, (md5_uint32) 0x85845dd1);
409:       OP (A, B, C, D,  8,  6, (md5_uint32) 0x6fa87e4f);
410:       OP (D, A, B, C, 15, 10, (md5_uint32) 0xfe2ce6e0);
411:       OP (C, D, A, B,  6, 15, (md5_uint32) 0xa3014314);
412:       OP (B, C, D, A, 13, 21, (md5_uint32) 0x4e0811a1);
413:       OP (A, B, C, D,  4,  6, (md5_uint32) 0xf7537e82);
414:       OP (D, A, B, C, 11, 10, (md5_uint32) 0xbd3af235);
415:       OP (C, D, A, B,  2, 15, (md5_uint32) 0x2ad7d2bb);
416:       OP (B, C, D, A,  9, 21, (md5_uint32) 0xeb86d391);
417: 
  418:       /* Add the starting values of the context.  */

>そして、次の追加分の実行を行う.
>(それは、それぞれこのブロックが始められる前に、それが持った値による4つのレジスタの増分である。)

419:       A += A_save;
420:       B += B_save;
421:       C += C_save;
422:       D += D_save;
423:     }
424: 

~

  425:   /* Put checksum in context given as argument.  */
426:   ctx->A = A;
427:   ctx->B = B;
428:   ctx->C = C;
429:   ctx->D = D;
430: }


リンク元

Advertisement