sl_md5.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sl_md5.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/24 00:10:37 by bchanot #+# #+# */
  9. /* Updated: 2018/10/10 12:53:46 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. #include "sl_md5.h"
  14. static const t_uint32 g_k[64] = {
  15. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, \
  16. 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, \
  17. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, \
  18. 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, \
  19. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, \
  20. 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, \
  21. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, \
  22. 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, \
  23. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, \
  24. 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, \
  25. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  26. };
  27. static const t_uint32 g_r[64] = {
  28. 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, \
  29. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, \
  30. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, \
  31. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
  32. };
  33. static void sl_md5_get_registers(t_process_md5 *t, size_t cpt,
  34. t_uint32 bytes[2])
  35. {
  36. if (cpt < 16)
  37. {
  38. bytes[0] = (t->b & t->c) | ((~t->b) & t->d);
  39. bytes[1] = cpt;
  40. }
  41. else if (cpt < 32)
  42. {
  43. bytes[0] = (t->d & t->b) | ((~t->d) & t->c);
  44. bytes[1] = (5 * cpt + 1) % 16;
  45. }
  46. else if (cpt < 48)
  47. {
  48. bytes[0] = t->b ^ t->c ^ t->d;
  49. bytes[1] = (3 * cpt + 5) % 16;
  50. }
  51. else
  52. {
  53. bytes[0] = t->c ^ (t->b | (~t->d));
  54. bytes[1] = (7 * cpt) % 16;
  55. }
  56. }
  57. static void sl_md5_process(t_md5 *ctx, t_uint32 *data)
  58. {
  59. t_process_md5 t;
  60. t_uint32 bytes[2];
  61. size_t cpt;
  62. t.a = ctx->state[0];
  63. t.b = ctx->state[1];
  64. t.c = ctx->state[2];
  65. t.d = ctx->state[3];
  66. cpt = -1;
  67. while (++cpt < 64)
  68. {
  69. sl_md5_get_registers(&t, cpt, bytes);
  70. bytes[0] = bytes[0] + t.a + g_k[cpt] + data[bytes[1]];
  71. t.a = t.d;
  72. t.d = t.c;
  73. t.c = t.b;
  74. t.b = t.b + ROL32(bytes[0], g_r[cpt]);
  75. }
  76. ctx->state[0] += t.a;
  77. ctx->state[1] += t.b;
  78. ctx->state[2] += t.c;
  79. ctx->state[3] += t.d;
  80. }
  81. void sl_md5_update(t_md5 *ctx, const t_uint8 *data, size_t len)
  82. {
  83. t_uint32 avail;
  84. avail = sizeof(ctx->buff) - (ctx->count & 0x3f);
  85. ctx->count += len;
  86. if (avail > len)
  87. {
  88. ft_memcpy((char *)ctx->buff + (sizeof(ctx->buff) - avail), data, len);
  89. return ;
  90. }
  91. ft_memcpy((char *)ctx->buff + (sizeof(ctx->buff) - avail), data, avail);
  92. sl_md5_process(ctx, ctx->buff);
  93. data += avail;
  94. len -= avail;
  95. while (len >= sizeof(ctx->buff))
  96. {
  97. ft_memcpy(ctx->buff, data, sizeof(ctx->buff));
  98. sl_md5_process(ctx, ctx->buff);
  99. data += sizeof(ctx->buff);
  100. len -= sizeof(ctx->buff);
  101. }
  102. ft_memcpy(ctx->buff, data, len);
  103. }
  104. void sl_md5_final(t_md5 *ctx, t_uint8 *hash)
  105. {
  106. size_t offset;
  107. char *padding;
  108. int pad_len;
  109. offset = ctx->count & 0x3f;
  110. padding = (char *)ctx->buff + offset;
  111. pad_len = 56 - (offset + 1);
  112. *padding = 0x80;
  113. padding++;
  114. if (pad_len < 0)
  115. {
  116. ft_bzero(padding, pad_len + 8);
  117. sl_md5_process(ctx, ctx->buff);
  118. padding = (char *)ctx->buff;
  119. pad_len = 56;
  120. }
  121. ft_bzero(padding, pad_len);
  122. ctx->buff[14] = ctx->count << 3;
  123. ctx->buff[15] = ctx->count >> 29;
  124. sl_md5_process(ctx, ctx->buff);
  125. ft_memcpy(hash, ctx->state, sizeof(ctx->state));
  126. }
  127. void sl_md5_init(t_md5 *ctx)
  128. {
  129. ctx->state[0] = 0x67452301;
  130. ctx->state[1] = 0xefcdab89;
  131. ctx->state[2] = 0x98badcfe;
  132. ctx->state[3] = 0x10325476;
  133. ctx->count = 0;
  134. }