[libntl] 05/07: replace md5 implementation

Julien Puydt julien.puydt at laposte.net
Fri Aug 15 08:23:03 UTC 2014


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch Snark_NTL6
in repository libntl.

commit 2e2a20eb19d4ce4c18869a9bd6f1cb6df247a717
Author: Bernhard R. Link <brlink at debian.org>
Date:   Sat Feb 11 10:35:46 2012 +0100

    replace md5 implementation
    
    Replace RSA's md5 implementation with a public domain one to
    get rid of the advertisement clause.
---
 src/ZZ.c | 316 +++++++++++++++++++++++++++------------------------------------
 1 file changed, 133 insertions(+), 183 deletions(-)

diff --git a/src/ZZ.c b/src/ZZ.c
index 86340a7..3a38177 100644
--- a/src/ZZ.c
+++ b/src/ZZ.c
@@ -1353,207 +1353,157 @@ long power_long(long a, long e)
 // which I've modified to work on 64-bit machines
 
 
+#ifndef uint32
+# if NTL_BITS_PER_INT == 32
+typedef unsigned int uint32;
+# elif NTL_BITS_PER_LONG == 32
+typedef unsigned long uint32;
+# else
+#  error unable to find 32 bit type
+# endif
+#endif
 /*
- *  BEGIN RSA's md5 stuff
+ *  BEGIN md5 stuff
  *
  */
 
 /*
- **********************************************************************
- ** md5.c                                                            **
- ** RSA Data Security, Inc. MD5 Message Digest Algorithm             **
- ** Created: 2/17/90 RLR                                             **
- ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version                  **
- **********************************************************************
+ * This code implements the MD5 message-digest algorithm.
+ * The algorithm is due to Ron Rivest.  This code was
+ * written by Colin Plumb in 1993, no copyright is claimed.
+ * This code is in the public domain; do with it what you wish.
+ *
+ * Equivalent code is available from RSA Data Security, Inc.
+ * This code has been tested against that, and is equivalent,
+ * except that you don't need to include two pages of legalese
+ * with every copy.
+ *
+ Modified to only contain the functions needed here.
  */
 
 /*
- **********************************************************************
- ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
- **                                                                  **
- ** License to copy and use this software is granted provided that   **
- ** it is identified as the "RSA Data Security, Inc. MD5 Message     **
- ** Digest Algorithm" in all material mentioning or referencing this **
- ** software or this function.                                       **
- **                                                                  **
- ** License is also granted to make and use derivative works         **
- ** provided that such works are identified as "derived from the RSA **
- ** Data Security, Inc. MD5 Message Digest Algorithm" in all         **
- ** material mentioning or referencing the derived work.             **
- **                                                                  **
- ** RSA Data Security, Inc. makes no representations concerning      **
- ** either the merchantability of this software or the suitability   **
- ** of this software for any particular purpose.  It is provided "as **
- ** is" without express or implied warranty of any kind.             **
- **                                                                  **
- ** These notices must be retained in any copies of any part of this **
- ** documentation and/or software.                                   **
- **********************************************************************
+ * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
+ * initialization constants.
  */
-
-
-#if (NTL_BITS_PER_LONG <= 32)
-#define TRUNC32(x) (x)
-#else
-#define TRUNC32(x) ((x) & ((1UL << 32)-1UL))
-#endif
-
-/* F, G and H are basic MD5 functions: selection, majority, parity */
-#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) (TRUNC32((y) ^ ((x) | (~z)))) 
-
-/* ROTATE_LEFT rotates x left n bits */
-#define ROTATE_LEFT(x, n) (TRUNC32(((x) << (n)) | ((x) >> (32-(n)))))
-
-/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
-/* Rotation is separate from addition to prevent recomputation */
-#define FF(a, b, c, d, x, s, ac) \
-  {(a) = TRUNC32((a) + F((b), (c), (d)) + (x) + (ac)); \
-   (a) = ROTATE_LEFT((a), (s)); \
-   (a) = TRUNC32((a) + (b)); \
-  }
-#define GG(a, b, c, d, x, s, ac) \
-  {(a) = TRUNC32((a) + G((b), (c), (d)) + (x) + (ac)); \
-   (a) = ROTATE_LEFT((a), (s)); \
-   (a) = TRUNC32((a) + (b)); \
-  }
-#define HH(a, b, c, d, x, s, ac) \
-  {(a) = TRUNC32((a) + H((b), (c), (d)) + (x) + (ac)); \
-   (a) = ROTATE_LEFT((a), (s)); \
-   (a) = TRUNC32((a) + (b)); \
-  }
-#define II(a, b, c, d, x, s, ac) \
-  {(a) = TRUNC32((a) + I((b), (c), (d)) + (x) + (ac)); \
-   (a) = ROTATE_LEFT((a), (s)); \
-   (a) = TRUNC32((a) + (b)); \
-  }
-
-
-
 static
-void MD5_default_IV(unsigned long *buf)
+void MD5Init(uint32 buf[4])
 {
-   buf[0] = 0x67452301UL;
-   buf[1] = 0xefcdab89UL;
-   buf[2] = 0x98badcfeUL;
-   buf[3] = 0x10325476UL;
+    buf[0] = 0x67452301U;
+    buf[1] = 0xefcdab89U;
+    buf[2] = 0x98badcfeU;
+    buf[3] = 0x10325476U;
 }
 
+/* The four core functions - F1 is optimized somewhat */
 
+/* #define F1(x, y, z) (x & y | ~x & z) */
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
 
-/* Basic MD5 step. Transform buf based on in.
- */
+/* This is the central step in the MD5 algorithm. */
+#define MD5STEP(f, w, x, y, z, data, s) \
+	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
 
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data.  MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
 static
-void MD5_compress(unsigned long *buf, unsigned long *in)
-{
-  unsigned long a = buf[0], b = buf[1], c = buf[2], d = buf[3];
-
-  /* Round 1 */
-#define S11 7
-#define S12 12
-#define S13 17
-#define S14 22
-  FF ( a, b, c, d, in[ 0], S11, 3614090360UL); /* 1 */
-  FF ( d, a, b, c, in[ 1], S12, 3905402710UL); /* 2 */
-  FF ( c, d, a, b, in[ 2], S13,  606105819UL); /* 3 */
-  FF ( b, c, d, a, in[ 3], S14, 3250441966UL); /* 4 */
-  FF ( a, b, c, d, in[ 4], S11, 4118548399UL); /* 5 */
-  FF ( d, a, b, c, in[ 5], S12, 1200080426UL); /* 6 */
-  FF ( c, d, a, b, in[ 6], S13, 2821735955UL); /* 7 */
-  FF ( b, c, d, a, in[ 7], S14, 4249261313UL); /* 8 */
-  FF ( a, b, c, d, in[ 8], S11, 1770035416UL); /* 9 */
-  FF ( d, a, b, c, in[ 9], S12, 2336552879UL); /* 10 */
-  FF ( c, d, a, b, in[10], S13, 4294925233UL); /* 11 */
-  FF ( b, c, d, a, in[11], S14, 2304563134UL); /* 12 */
-  FF ( a, b, c, d, in[12], S11, 1804603682UL); /* 13 */
-  FF ( d, a, b, c, in[13], S12, 4254626195UL); /* 14 */
-  FF ( c, d, a, b, in[14], S13, 2792965006UL); /* 15 */
-  FF ( b, c, d, a, in[15], S14, 1236535329UL); /* 16 */
-
-  /* Round 2 */
-#define S21 5
-#define S22 9
-#define S23 14
-#define S24 20
-  GG ( a, b, c, d, in[ 1], S21, 4129170786UL); /* 17 */
-  GG ( d, a, b, c, in[ 6], S22, 3225465664UL); /* 18 */
-  GG ( c, d, a, b, in[11], S23,  643717713UL); /* 19 */
-  GG ( b, c, d, a, in[ 0], S24, 3921069994UL); /* 20 */
-  GG ( a, b, c, d, in[ 5], S21, 3593408605UL); /* 21 */
-  GG ( d, a, b, c, in[10], S22,   38016083UL); /* 22 */
-  GG ( c, d, a, b, in[15], S23, 3634488961UL); /* 23 */
-  GG ( b, c, d, a, in[ 4], S24, 3889429448UL); /* 24 */
-  GG ( a, b, c, d, in[ 9], S21,  568446438UL); /* 25 */
-  GG ( d, a, b, c, in[14], S22, 3275163606UL); /* 26 */
-  GG ( c, d, a, b, in[ 3], S23, 4107603335UL); /* 27 */
-  GG ( b, c, d, a, in[ 8], S24, 1163531501UL); /* 28 */
-  GG ( a, b, c, d, in[13], S21, 2850285829UL); /* 29 */
-  GG ( d, a, b, c, in[ 2], S22, 4243563512UL); /* 30 */
-  GG ( c, d, a, b, in[ 7], S23, 1735328473UL); /* 31 */
-  GG ( b, c, d, a, in[12], S24, 2368359562UL); /* 32 */
-
-  /* Round 3 */
-#define S31 4
-#define S32 11
-#define S33 16
-#define S34 23
-  HH ( a, b, c, d, in[ 5], S31, 4294588738UL); /* 33 */
-  HH ( d, a, b, c, in[ 8], S32, 2272392833UL); /* 34 */
-  HH ( c, d, a, b, in[11], S33, 1839030562UL); /* 35 */
-  HH ( b, c, d, a, in[14], S34, 4259657740UL); /* 36 */
-  HH ( a, b, c, d, in[ 1], S31, 2763975236UL); /* 37 */
-  HH ( d, a, b, c, in[ 4], S32, 1272893353UL); /* 38 */
-  HH ( c, d, a, b, in[ 7], S33, 4139469664UL); /* 39 */
-  HH ( b, c, d, a, in[10], S34, 3200236656UL); /* 40 */
-  HH ( a, b, c, d, in[13], S31,  681279174UL); /* 41 */
-  HH ( d, a, b, c, in[ 0], S32, 3936430074UL); /* 42 */
-  HH ( c, d, a, b, in[ 3], S33, 3572445317UL); /* 43 */
-  HH ( b, c, d, a, in[ 6], S34,   76029189UL); /* 44 */
-  HH ( a, b, c, d, in[ 9], S31, 3654602809UL); /* 45 */
-  HH ( d, a, b, c, in[12], S32, 3873151461UL); /* 46 */
-  HH ( c, d, a, b, in[15], S33,  530742520UL); /* 47 */
-  HH ( b, c, d, a, in[ 2], S34, 3299628645UL); /* 48 */
-
-  /* Round 4 */
-#define S41 6
-#define S42 10
-#define S43 15
-#define S44 21
-  II ( a, b, c, d, in[ 0], S41, 4096336452UL); /* 49 */
-  II ( d, a, b, c, in[ 7], S42, 1126891415UL); /* 50 */
-  II ( c, d, a, b, in[14], S43, 2878612391UL); /* 51 */
-  II ( b, c, d, a, in[ 5], S44, 4237533241UL); /* 52 */
-  II ( a, b, c, d, in[12], S41, 1700485571UL); /* 53 */
-  II ( d, a, b, c, in[ 3], S42, 2399980690UL); /* 54 */
-  II ( c, d, a, b, in[10], S43, 4293915773UL); /* 55 */
-  II ( b, c, d, a, in[ 1], S44, 2240044497UL); /* 56 */
-  II ( a, b, c, d, in[ 8], S41, 1873313359UL); /* 57 */
-  II ( d, a, b, c, in[15], S42, 4264355552UL); /* 58 */
-  II ( c, d, a, b, in[ 6], S43, 2734768916UL); /* 59 */
-  II ( b, c, d, a, in[13], S44, 1309151649UL); /* 60 */
-  II ( a, b, c, d, in[ 4], S41, 4149444226UL); /* 61 */
-  II ( d, a, b, c, in[11], S42, 3174756917UL); /* 62 */
-  II ( c, d, a, b, in[ 2], S43,  718787259UL); /* 63 */
-  II ( b, c, d, a, in[ 9], S44, 3951481745UL); /* 64 */
-
-  buf[0] = TRUNC32(buf[0] + a);
-  buf[1] = TRUNC32(buf[1] + b);
-  buf[2] = TRUNC32(buf[2] + c);
-  buf[3] = TRUNC32(buf[3] + d);
+void MD5Transform(uint32 buf[4], uint32 const in[16])
+{
+    register uint32 a, b, c, d;
+
+    a = buf[0];
+    b = buf[1];
+    c = buf[2];
+    d = buf[3];
+
+    MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478U, 7);
+    MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756U, 12);
+    MD5STEP(F1, c, d, a, b, in[2] + 0x242070dbU, 17);
+    MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceeeU, 22);
+    MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0fafU, 7);
+    MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62aU, 12);
+    MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613U, 17);
+    MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501U, 22);
+    MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8U, 7);
+    MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7afU, 12);
+    MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1U, 17);
+    MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7beU, 22);
+    MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122U, 7);
+    MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193U, 12);
+    MD5STEP(F1, c, d, a, b, in[14] + 0xa679438eU, 17);
+    MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821U, 22);
+
+    MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562U, 5);
+    MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340U, 9);
+    MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51U, 14);
+    MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aaU, 20);
+    MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105dU, 5);
+    MD5STEP(F2, d, a, b, c, in[10] + 0x02441453U, 9);
+    MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+    MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8U, 20);
+    MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6U, 5);
+    MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6U, 9);
+    MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87U, 14);
+    MD5STEP(F2, b, c, d, a, in[8] + 0x455a14edU, 20);
+    MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905U, 5);
+    MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8U, 9);
+    MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9U, 14);
+    MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8aU, 20);
+
+    MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942U, 4);
+    MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681U, 11);
+    MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122U, 16);
+    MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380cU, 23);
+    MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44U, 4);
+    MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9U, 11);
+    MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60U, 16);
+    MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70U, 23);
+    MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6U, 4);
+    MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127faU, 11);
+    MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085U, 16);
+    MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05U, 23);
+    MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039U, 4);
+    MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5U, 11);
+    MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8U, 16);
+    MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665U, 23);
+
+    MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244U, 6);
+    MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97U, 10);
+    MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7U, 15);
+    MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039U, 21);
+    MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3U, 6);
+    MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92U, 10);
+    MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47dU, 15);
+    MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1U, 21);
+    MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4fU, 6);
+    MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0U, 10);
+    MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314U, 15);
+    MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1U, 21);
+    MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82U, 6);
+    MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235U, 10);
+    MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bbU, 15);
+    MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391U, 21);
+
+    buf[0] += a;
+    buf[1] += b;
+    buf[2] += c;
+    buf[3] += d;
 }
 
 
 /*
- *  END RSA's md5 stuff
+ *  END md5 stuff
  *
  */
 
 
 static
-void words_from_bytes(unsigned long *txtl, unsigned char *txtc, long n)
+void words_from_bytes(uint32 *txtl, unsigned char *txtc, long n)
 {
    long i;
    unsigned long v;
@@ -1568,7 +1518,7 @@ void words_from_bytes(unsigned long *txtl, unsigned char *txtc, long n)
 }
 
 static 
-void bytes_from_words(unsigned char *txtc, unsigned long *txtl, long n)
+void bytes_from_words(unsigned char *txtc, uint32 *txtl, long n)
 {
    long i;
    unsigned long v;
@@ -1587,9 +1537,9 @@ void bytes_from_words(unsigned char *txtc, unsigned long *txtl, long n)
 
 
 static
-void MD5_compress1(unsigned long *buf, unsigned char *in, long n)
+void MD5_compress1(uint32 *buf, unsigned char *in, long n)
 {
-   unsigned long txtl[16];
+   uint32 txtl[16];
    unsigned char txtc[64]; 
    long i, j, k;
 
@@ -1604,7 +1554,7 @@ void MD5_compress1(unsigned long *buf, unsigned char *in, long n)
       for (; j < 64; j++)
          txtc[j] = 0;
       words_from_bytes(txtl, txtc, 16);
-      MD5_compress(buf, txtl);
+      MD5Transform(buf, txtl);
       i += k;
    }
 }
@@ -1689,7 +1639,7 @@ void arc4(unsigned char *buffer_ptr, long buffer_len, _ZZ_arc4_key *key)
 static long ran_initialized = 0;
 static _ZZ_arc4_key ran_key;
 
-static unsigned long default_md5_tab[16] = {
+static uint32 default_md5_tab[16] = {
 744663023UL, 1011602954UL, 3163087192UL, 3383838527UL, 
 3305324122UL, 3197458079UL, 2266495600UL, 2760303563UL, 
 346234297UL, 1919920720UL, 1896169861UL, 2192176675UL, 
@@ -1713,11 +1663,11 @@ void build_arc4_tab(unsigned char *seed_bytes, const ZZ& s)
 
    bytes_from_words(txt + nb + 4, default_md5_tab, 16);
 
-   unsigned long buf[4];
+   uint32 buf[4];
 
-   unsigned long i;
+   uint32 i;
    for (i = 0; i < 16; i++) {
-      MD5_default_IV(buf);
+      MD5Init(buf);
       bytes_from_words(txt, &i, 1);
 
       MD5_compress1(buf, txt, nb + 68);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/libntl.git



More information about the debian-science-commits mailing list