SkMD5.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. *
  7. * The following code is based on the description in RFC 1321.
  8. * http://www.ietf.org/rfc/rfc1321.txt
  9. */
  10. //The following macros can be defined to affect the MD5 code generated.
  11. //SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's.
  12. //SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned).
  13. //SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_LENDIAN.
  14. #include "src/core/SkMD5.h"
  15. #include <string.h>
  16. /** MD5 basic transformation. Transforms state based on block. */
  17. static void transform(uint32_t state[4], const uint8_t block[64]);
  18. /** Encodes input into output (4 little endian 32 bit values). */
  19. static void encode(uint8_t output[16], const uint32_t input[4]);
  20. /** Encodes input into output (little endian 64 bit value). */
  21. static void encode(uint8_t output[8], const uint64_t input);
  22. /** Decodes input (4 little endian 32 bit values) into storage, if required. */
  23. static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]);
  24. SkMD5::SkMD5() : byteCount(0) {
  25. // These are magic numbers from the specification.
  26. this->state[0] = 0x67452301;
  27. this->state[1] = 0xefcdab89;
  28. this->state[2] = 0x98badcfe;
  29. this->state[3] = 0x10325476;
  30. }
  31. bool SkMD5::write(const void* buf, size_t inputLength) {
  32. const uint8_t* input = reinterpret_cast<const uint8_t*>(buf);
  33. unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F);
  34. unsigned int bufferAvailable = 64 - bufferIndex;
  35. unsigned int inputIndex;
  36. if (inputLength >= bufferAvailable) {
  37. if (bufferIndex) {
  38. memcpy(&this->buffer[bufferIndex], input, bufferAvailable);
  39. transform(this->state, this->buffer);
  40. inputIndex = bufferAvailable;
  41. } else {
  42. inputIndex = 0;
  43. }
  44. for (; inputIndex + 63 < inputLength; inputIndex += 64) {
  45. transform(this->state, &input[inputIndex]);
  46. }
  47. bufferIndex = 0;
  48. } else {
  49. inputIndex = 0;
  50. }
  51. memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIndex);
  52. this->byteCount += inputLength;
  53. return true;
  54. }
  55. SkMD5::Digest SkMD5::finish() {
  56. SkMD5::Digest digest;
  57. // Get the number of bits before padding.
  58. uint8_t bits[8];
  59. encode(bits, this->byteCount << 3);
  60. // Pad out to 56 mod 64.
  61. unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F);
  62. unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex);
  63. static uint8_t PADDING[64] = {
  64. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. };
  69. (void)this->write(PADDING, paddingLength);
  70. // Append length (length before padding, will cause final update).
  71. (void)this->write(bits, 8);
  72. // Write out digest.
  73. encode(digest.data, this->state);
  74. #if defined(SK_MD5_CLEAR_DATA)
  75. // Clear state.
  76. memset(this, 0, sizeof(*this));
  77. #endif
  78. return digest;
  79. }
  80. struct F { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) {
  81. //return (x & y) | ((~x) & z);
  82. return ((y ^ z) & x) ^ z; //equivelent but faster
  83. }};
  84. struct G { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) {
  85. return (x & z) | (y & (~z));
  86. //return ((x ^ y) & z) ^ y; //equivelent but slower
  87. }};
  88. struct H { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) {
  89. return x ^ y ^ z;
  90. }};
  91. struct I { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) {
  92. return y ^ (x | (~z));
  93. }};
  94. /** Rotates x left n bits. */
  95. static inline uint32_t rotate_left(uint32_t x, uint8_t n) {
  96. return (x << n) | (x >> (32 - n));
  97. }
  98. template <typename T>
  99. static inline void operation(T operation, uint32_t& a, uint32_t b, uint32_t c, uint32_t d,
  100. uint32_t x, uint8_t s, uint32_t t) {
  101. a = b + rotate_left(a + operation(b, c, d) + x + t, s);
  102. }
  103. static void transform(uint32_t state[4], const uint8_t block[64]) {
  104. uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
  105. uint32_t storage[16];
  106. const uint32_t* X = decode(storage, block);
  107. // Round 1
  108. operation(F(), a, b, c, d, X[ 0], 7, 0xd76aa478); // 1
  109. operation(F(), d, a, b, c, X[ 1], 12, 0xe8c7b756); // 2
  110. operation(F(), c, d, a, b, X[ 2], 17, 0x242070db); // 3
  111. operation(F(), b, c, d, a, X[ 3], 22, 0xc1bdceee); // 4
  112. operation(F(), a, b, c, d, X[ 4], 7, 0xf57c0faf); // 5
  113. operation(F(), d, a, b, c, X[ 5], 12, 0x4787c62a); // 6
  114. operation(F(), c, d, a, b, X[ 6], 17, 0xa8304613); // 7
  115. operation(F(), b, c, d, a, X[ 7], 22, 0xfd469501); // 8
  116. operation(F(), a, b, c, d, X[ 8], 7, 0x698098d8); // 9
  117. operation(F(), d, a, b, c, X[ 9], 12, 0x8b44f7af); // 10
  118. operation(F(), c, d, a, b, X[10], 17, 0xffff5bb1); // 11
  119. operation(F(), b, c, d, a, X[11], 22, 0x895cd7be); // 12
  120. operation(F(), a, b, c, d, X[12], 7, 0x6b901122); // 13
  121. operation(F(), d, a, b, c, X[13], 12, 0xfd987193); // 14
  122. operation(F(), c, d, a, b, X[14], 17, 0xa679438e); // 15
  123. operation(F(), b, c, d, a, X[15], 22, 0x49b40821); // 16
  124. // Round 2
  125. operation(G(), a, b, c, d, X[ 1], 5, 0xf61e2562); // 17
  126. operation(G(), d, a, b, c, X[ 6], 9, 0xc040b340); // 18
  127. operation(G(), c, d, a, b, X[11], 14, 0x265e5a51); // 19
  128. operation(G(), b, c, d, a, X[ 0], 20, 0xe9b6c7aa); // 20
  129. operation(G(), a, b, c, d, X[ 5], 5, 0xd62f105d); // 21
  130. operation(G(), d, a, b, c, X[10], 9, 0x2441453); // 22
  131. operation(G(), c, d, a, b, X[15], 14, 0xd8a1e681); // 23
  132. operation(G(), b, c, d, a, X[ 4], 20, 0xe7d3fbc8); // 24
  133. operation(G(), a, b, c, d, X[ 9], 5, 0x21e1cde6); // 25
  134. operation(G(), d, a, b, c, X[14], 9, 0xc33707d6); // 26
  135. operation(G(), c, d, a, b, X[ 3], 14, 0xf4d50d87); // 27
  136. operation(G(), b, c, d, a, X[ 8], 20, 0x455a14ed); // 28
  137. operation(G(), a, b, c, d, X[13], 5, 0xa9e3e905); // 29
  138. operation(G(), d, a, b, c, X[ 2], 9, 0xfcefa3f8); // 30
  139. operation(G(), c, d, a, b, X[ 7], 14, 0x676f02d9); // 31
  140. operation(G(), b, c, d, a, X[12], 20, 0x8d2a4c8a); // 32
  141. // Round 3
  142. operation(H(), a, b, c, d, X[ 5], 4, 0xfffa3942); // 33
  143. operation(H(), d, a, b, c, X[ 8], 11, 0x8771f681); // 34
  144. operation(H(), c, d, a, b, X[11], 16, 0x6d9d6122); // 35
  145. operation(H(), b, c, d, a, X[14], 23, 0xfde5380c); // 36
  146. operation(H(), a, b, c, d, X[ 1], 4, 0xa4beea44); // 37
  147. operation(H(), d, a, b, c, X[ 4], 11, 0x4bdecfa9); // 38
  148. operation(H(), c, d, a, b, X[ 7], 16, 0xf6bb4b60); // 39
  149. operation(H(), b, c, d, a, X[10], 23, 0xbebfbc70); // 40
  150. operation(H(), a, b, c, d, X[13], 4, 0x289b7ec6); // 41
  151. operation(H(), d, a, b, c, X[ 0], 11, 0xeaa127fa); // 42
  152. operation(H(), c, d, a, b, X[ 3], 16, 0xd4ef3085); // 43
  153. operation(H(), b, c, d, a, X[ 6], 23, 0x4881d05); // 44
  154. operation(H(), a, b, c, d, X[ 9], 4, 0xd9d4d039); // 45
  155. operation(H(), d, a, b, c, X[12], 11, 0xe6db99e5); // 46
  156. operation(H(), c, d, a, b, X[15], 16, 0x1fa27cf8); // 47
  157. operation(H(), b, c, d, a, X[ 2], 23, 0xc4ac5665); // 48
  158. // Round 4
  159. operation(I(), a, b, c, d, X[ 0], 6, 0xf4292244); // 49
  160. operation(I(), d, a, b, c, X[ 7], 10, 0x432aff97); // 50
  161. operation(I(), c, d, a, b, X[14], 15, 0xab9423a7); // 51
  162. operation(I(), b, c, d, a, X[ 5], 21, 0xfc93a039); // 52
  163. operation(I(), a, b, c, d, X[12], 6, 0x655b59c3); // 53
  164. operation(I(), d, a, b, c, X[ 3], 10, 0x8f0ccc92); // 54
  165. operation(I(), c, d, a, b, X[10], 15, 0xffeff47d); // 55
  166. operation(I(), b, c, d, a, X[ 1], 21, 0x85845dd1); // 56
  167. operation(I(), a, b, c, d, X[ 8], 6, 0x6fa87e4f); // 57
  168. operation(I(), d, a, b, c, X[15], 10, 0xfe2ce6e0); // 58
  169. operation(I(), c, d, a, b, X[ 6], 15, 0xa3014314); // 59
  170. operation(I(), b, c, d, a, X[13], 21, 0x4e0811a1); // 60
  171. operation(I(), a, b, c, d, X[ 4], 6, 0xf7537e82); // 61
  172. operation(I(), d, a, b, c, X[11], 10, 0xbd3af235); // 62
  173. operation(I(), c, d, a, b, X[ 2], 15, 0x2ad7d2bb); // 63
  174. operation(I(), b, c, d, a, X[ 9], 21, 0xeb86d391); // 64
  175. state[0] += a;
  176. state[1] += b;
  177. state[2] += c;
  178. state[3] += d;
  179. #if defined(SK_MD5_CLEAR_DATA)
  180. // Clear sensitive information.
  181. if (X == &storage) {
  182. memset(storage, 0, sizeof(storage));
  183. }
  184. #endif
  185. }
  186. static void encode(uint8_t output[16], const uint32_t input[4]) {
  187. for (size_t i = 0, j = 0; i < 4; i++, j += 4) {
  188. output[j ] = (uint8_t) (input[i] & 0xff);
  189. output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
  190. output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
  191. output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
  192. }
  193. }
  194. static void encode(uint8_t output[8], const uint64_t input) {
  195. output[0] = (uint8_t) (input & 0xff);
  196. output[1] = (uint8_t)((input >> 8) & 0xff);
  197. output[2] = (uint8_t)((input >> 16) & 0xff);
  198. output[3] = (uint8_t)((input >> 24) & 0xff);
  199. output[4] = (uint8_t)((input >> 32) & 0xff);
  200. output[5] = (uint8_t)((input >> 40) & 0xff);
  201. output[6] = (uint8_t)((input >> 48) & 0xff);
  202. output[7] = (uint8_t)((input >> 56) & 0xff);
  203. }
  204. static inline bool is_aligned(const void *pointer, size_t byte_count) {
  205. return reinterpret_cast<uintptr_t>(pointer) % byte_count == 0;
  206. }
  207. static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]) {
  208. #if defined(SK_CPU_LENDIAN) && defined(SK_CPU_FAST_UNALIGNED_ACCESS)
  209. return reinterpret_cast<const uint32_t*>(input);
  210. #else
  211. #if defined(SK_CPU_LENDIAN)
  212. if (is_aligned(input, 4)) {
  213. return reinterpret_cast<const uint32_t*>(input);
  214. }
  215. #endif
  216. for (size_t i = 0, j = 0; j < 64; i++, j += 4) {
  217. storage[i] = ((uint32_t)input[j ]) |
  218. (((uint32_t)input[j+1]) << 8) |
  219. (((uint32_t)input[j+2]) << 16) |
  220. (((uint32_t)input[j+3]) << 24);
  221. }
  222. return storage;
  223. #endif
  224. }