PageRenderTime 1285ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/common/crypto/md4/md4.c

https://github.com/richlowe/illumos-gate
C | 305 lines | 180 code | 41 blank | 84 comment | 9 complexity | 1bfe061112d483f48d6eec17cc141d21 MD5 | raw file
  1. /*
  2. * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  3. * Use is subject to license terms.
  4. */
  5. /*
  6. * MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
  7. */
  8. /*
  9. * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  10. *
  11. * License to copy and use this software is granted provided that it
  12. * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  13. * Algorithm" in all material mentioning or referencing this software
  14. * or this function.
  15. *
  16. * License is also granted to make and use derivative works provided
  17. * that such works are identified as "derived from the RSA Data
  18. * Security, Inc. MD4 Message-Digest Algorithm" in all material
  19. * mentioning or referencing the derived work.
  20. *
  21. * RSA Data Security, Inc. makes no representations concerning either
  22. * the merchantability of this software or the suitability of this
  23. * software for any particular purpose. It is provided "as is"
  24. * without express or implied warranty of any kind.
  25. *
  26. * These notices must be retained in any copies of any part of this
  27. * documentation and/or software.
  28. */
  29. #include <sys/types.h>
  30. #ifdef _KERNEL
  31. #include <sys/sunddi.h>
  32. #else
  33. #include <strings.h>
  34. #endif /* _KERNEL */
  35. #if defined(__i386) || defined(__amd64)
  36. #define UNALIGNED_POINTERS_PERMITTED
  37. #endif
  38. #include <sys/md4.h>
  39. /*
  40. * Constants for MD4Transform routine.
  41. */
  42. #define S11 3
  43. #define S12 7
  44. #define S13 11
  45. #define S14 19
  46. #define S21 3
  47. #define S22 5
  48. #define S23 9
  49. #define S24 13
  50. #define S31 3
  51. #define S32 9
  52. #define S33 11
  53. #define S34 15
  54. static void MD4Transform(uint32_t [4], unsigned char [64]);
  55. static void Encode(unsigned char *, uint32_t *, unsigned int);
  56. static void Decode(uint32_t *, unsigned char *, unsigned int);
  57. static unsigned char PADDING[64] = {
  58. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  61. };
  62. /*
  63. * F, G and H are basic MD4 functions.
  64. */
  65. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  66. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  67. #define H(x, y, z) ((x) ^ (y) ^ (z))
  68. /*
  69. * ROTATE_LEFT rotates x left n bits.
  70. */
  71. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  72. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  73. /* Rotation is separate from addition to prevent recomputation */
  74. #define FF(a, b, c, d, x, s) { \
  75. (a) += F((b), (c), (d)) + (x); \
  76. (a) = ROTATE_LEFT((a), (s)); \
  77. }
  78. #define GG(a, b, c, d, x, s) { \
  79. (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5a827999; \
  80. (a) = ROTATE_LEFT((a), (s)); \
  81. }
  82. #define HH(a, b, c, d, x, s) { \
  83. (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ed9eba1; \
  84. (a) = ROTATE_LEFT((a), (s)); \
  85. }
  86. /*
  87. * MD4 initialization. Begins an MD4 operation, writing a new context.
  88. */
  89. void
  90. MD4Init(MD4_CTX *context)
  91. {
  92. context->count[0] = context->count[1] = 0;
  93. /*
  94. * Load magic initialization constants.
  95. */
  96. context->state[0] = 0x67452301UL;
  97. context->state[1] = 0xefcdab89UL;
  98. context->state[2] = 0x98badcfeUL;
  99. context->state[3] = 0x10325476UL;
  100. }
  101. /*
  102. * MD4 block update operation. Continues an MD4 message-digest
  103. * operation, processing another message block, and updating the
  104. * context.
  105. */
  106. void
  107. MD4Update(MD4_CTX *context, const void *_RESTRICT_KYWD inptr, size_t inputLen)
  108. {
  109. unsigned int i, index, partLen;
  110. uchar_t *input = (uchar_t *)inptr;
  111. /* Compute number of bytes mod 64 */
  112. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  113. /* Update number of bits */
  114. if ((context->count[0] += ((uint32_t)inputLen << 3))
  115. < ((uint32_t)inputLen << 3))
  116. context->count[1]++;
  117. context->count[1] += ((uint32_t)inputLen >> 29);
  118. partLen = 64 - index;
  119. /*
  120. * Transform as many times as possible.
  121. */
  122. if (inputLen >= partLen) {
  123. bcopy(input, &context->buffer[index], partLen);
  124. MD4Transform(context->state, (uchar_t *)context->buffer);
  125. for (i = partLen; i + 63 < inputLen; i += 64) {
  126. MD4Transform(context->state, (uchar_t *)&input[i]);
  127. }
  128. index = 0;
  129. } else {
  130. i = 0;
  131. }
  132. /* Buffer remaining input */
  133. bcopy(&input[i], &context->buffer[index], inputLen - i);
  134. }
  135. /*
  136. * MD4 finalization. Ends an MD4 message-digest operation, writing the
  137. * the message digest and zeroizing the context.
  138. */
  139. void
  140. MD4Final(void *digest, MD4_CTX *context)
  141. {
  142. unsigned char bits[8];
  143. unsigned int index, padLen;
  144. /* Save number of bits */
  145. Encode(bits, context->count, 8);
  146. /*
  147. * Pad out to 56 mod 64.
  148. */
  149. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  150. padLen = (index < 56) ? (56 - index) : (120 - index);
  151. MD4Update(context, PADDING, padLen);
  152. /* Append length (before padding) */
  153. MD4Update(context, bits, 8);
  154. /* Store state in digest */
  155. Encode(digest, context->state, 16);
  156. /* zeroize sensitive information */
  157. bzero(context, sizeof (*context));
  158. }
  159. /*
  160. * MD4 basic transformation. Transforms state based on block.
  161. */
  162. static void
  163. MD4Transform(uint32_t state[4], unsigned char block[64])
  164. {
  165. uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  166. Decode(x, block, 64);
  167. /* Round 1 */
  168. FF(a, b, c, d, x[ 0], S11); /* 1 */
  169. FF(d, a, b, c, x[ 1], S12); /* 2 */
  170. FF(c, d, a, b, x[ 2], S13); /* 3 */
  171. FF(b, c, d, a, x[ 3], S14); /* 4 */
  172. FF(a, b, c, d, x[ 4], S11); /* 5 */
  173. FF(d, a, b, c, x[ 5], S12); /* 6 */
  174. FF(c, d, a, b, x[ 6], S13); /* 7 */
  175. FF(b, c, d, a, x[ 7], S14); /* 8 */
  176. FF(a, b, c, d, x[ 8], S11); /* 9 */
  177. FF(d, a, b, c, x[ 9], S12); /* 10 */
  178. FF(c, d, a, b, x[10], S13); /* 11 */
  179. FF(b, c, d, a, x[11], S14); /* 12 */
  180. FF(a, b, c, d, x[12], S11); /* 13 */
  181. FF(d, a, b, c, x[13], S12); /* 14 */
  182. FF(c, d, a, b, x[14], S13); /* 15 */
  183. FF(b, c, d, a, x[15], S14); /* 16 */
  184. /* Round 2 */
  185. GG(a, b, c, d, x[ 0], S21); /* 17 */
  186. GG(d, a, b, c, x[ 4], S22); /* 18 */
  187. GG(c, d, a, b, x[ 8], S23); /* 19 */
  188. GG(b, c, d, a, x[12], S24); /* 20 */
  189. GG(a, b, c, d, x[ 1], S21); /* 21 */
  190. GG(d, a, b, c, x[ 5], S22); /* 22 */
  191. GG(c, d, a, b, x[ 9], S23); /* 23 */
  192. GG(b, c, d, a, x[13], S24); /* 24 */
  193. GG(a, b, c, d, x[ 2], S21); /* 25 */
  194. GG(d, a, b, c, x[ 6], S22); /* 26 */
  195. GG(c, d, a, b, x[10], S23); /* 27 */
  196. GG(b, c, d, a, x[14], S24); /* 28 */
  197. GG(a, b, c, d, x[ 3], S21); /* 29 */
  198. GG(d, a, b, c, x[ 7], S22); /* 30 */
  199. GG(c, d, a, b, x[11], S23); /* 31 */
  200. GG(b, c, d, a, x[15], S24); /* 32 */
  201. /* Round 3 */
  202. HH(a, b, c, d, x[ 0], S31); /* 33 */
  203. HH(d, a, b, c, x[ 8], S32); /* 34 */
  204. HH(c, d, a, b, x[ 4], S33); /* 35 */
  205. HH(b, c, d, a, x[12], S34); /* 36 */
  206. HH(a, b, c, d, x[ 2], S31); /* 37 */
  207. HH(d, a, b, c, x[10], S32); /* 38 */
  208. HH(c, d, a, b, x[ 6], S33); /* 39 */
  209. HH(b, c, d, a, x[14], S34); /* 40 */
  210. HH(a, b, c, d, x[ 1], S31); /* 41 */
  211. HH(d, a, b, c, x[ 9], S32); /* 42 */
  212. HH(c, d, a, b, x[ 5], S33); /* 43 */
  213. HH(b, c, d, a, x[13], S34); /* 44 */
  214. HH(a, b, c, d, x[ 3], S31); /* 45 */
  215. HH(d, a, b, c, x[11], S32); /* 46 */
  216. HH(c, d, a, b, x[ 7], S33); /* 47 */
  217. HH(b, c, d, a, x[15], S34); /* 48 */
  218. state[0] += a;
  219. state[1] += b;
  220. state[2] += c;
  221. state[3] += d;
  222. /* zeroize sensitive information */
  223. bzero(x, sizeof (*x));
  224. }
  225. /*
  226. * Encodes input (uint32_t) into output (unsigned char). Assumes len is
  227. * a multiple of 4.
  228. */
  229. static void
  230. Encode(unsigned char *output, uint32_t *input, unsigned int len)
  231. {
  232. unsigned int i, j;
  233. for (i = 0, j = 0; j < len; i++, j += 4) {
  234. #if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
  235. *(uint32_t *)(void *)&output[j] = input[i];
  236. #else
  237. /* endian-independent code */
  238. output[j] = (unsigned char)(input[i] & 0xff);
  239. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  240. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  241. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  242. #endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
  243. }
  244. }
  245. /*
  246. * Decodes input (unsigned char) into output (uint32_t). Assumes len is
  247. * a multiple of 4.
  248. */
  249. static void
  250. Decode(uint32_t *output, unsigned char *input, unsigned int len)
  251. {
  252. unsigned int i, j;
  253. for (i = 0, j = 0; j < len; i++, j += 4) {
  254. #if defined(_LITTLE_ENDIAN) && defined(UNALIGNED_POINTERS_PERMITTED)
  255. output[i] = *(uint32_t *)(void *)&input[j];
  256. #else
  257. /* endian-independent code */
  258. output[i] = ((uint32_t)input[j]) |
  259. (((uint32_t)input[j+1]) << 8) |
  260. (((uint32_t)input[j+2]) << 16) |
  261. (((uint32_t)input[j+3]) << 24);
  262. #endif /* _LITTLE_ENDIAN && UNALIGNED_POINTERS_PERMITTED */
  263. }
  264. }