sl_sha2.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sl_sha225_256.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/10/03 19:33:51 by bchanot #+# #+# */
  9. /* Updated: 2018/10/10 12:45:45 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. #include "sl_sha2.h"
  14. static void sl_sha2_get_state(t_sha2 *ctx, t_uint32 m[64])
  15. {
  16. t_process_sha2 t;
  17. size_t i;
  18. sl_sha2_process_init_add(ctx, &t, true);
  19. i = -1;
  20. while (++i < 64)
  21. sl_sha2_get_registers(m, &t, i);
  22. sl_sha2_process_init_add(ctx, &t, false);
  23. }
  24. static void sl_sha2_process(t_sha2 *ctx, const t_uint8 *data)
  25. {
  26. t_uint32 m[64];
  27. size_t i;
  28. size_t j;
  29. i = -1;
  30. while (++i < 64)
  31. {
  32. if (i < 16)
  33. {
  34. m[i] = data[i * 4] & 0xff;
  35. j = 0;
  36. while (++j < 4)
  37. {
  38. m[i] <<= 8;
  39. m[i] |= data[i * 4 + j] & 0xff;
  40. }
  41. }
  42. else
  43. m[i] = SIG1_SHA2(m[i - 2]) + m[i - 7] +
  44. SIG0_SHA2(m[i - 15]) + m[i - 16];
  45. }
  46. sl_sha2_get_state(ctx, m);
  47. }
  48. void sl_sha2_update(t_sha2 *ctx, const t_uint8 *data, size_t len)
  49. {
  50. size_t index;
  51. size_t i;
  52. index = (size_t)((ctx->count[0] >> 3) & 0x3f);
  53. if ((ctx->count[0] += (len << 3)) < (len << 3))
  54. {
  55. ctx->count[1]++;
  56. ctx->count[1] += (len >> 29);
  57. }
  58. i = 0;
  59. if (len >= (sizeof(ctx->buff) - index))
  60. {
  61. ft_memcpy(&ctx->buff[index], data, (sizeof(ctx->buff) - index));
  62. sl_sha2_process(ctx, ctx->buff);
  63. i = (sizeof(ctx->buff) - index);
  64. while (i + (sizeof(ctx->buff) - 1) < len)
  65. {
  66. sl_sha2_process(ctx, &data[i]);
  67. i += sizeof(ctx->buff);
  68. }
  69. index = 0;
  70. }
  71. ft_memcpy(&ctx->buff[index], &data[i], len - i);
  72. }
  73. void sl_sha2_get_hash(t_sha2 *ctx, t_uint8 hash[32])
  74. {
  75. size_t state;
  76. size_t i;
  77. size_t j;
  78. int k;
  79. i = -1;
  80. j = 0;
  81. while (++i < 8)
  82. {
  83. state = ctx->state[i];
  84. k = 4;
  85. while (--k >= 0)
  86. {
  87. hash[j + k] = state;
  88. state >>= 8;
  89. }
  90. j += 4;
  91. }
  92. }
  93. void sl_sha2_final(t_sha2 *ctx)
  94. {
  95. static const t_uint8 padding[64] = {0x80, };
  96. t_uint8 bits[8];
  97. size_t pad_len;
  98. size_t t;
  99. int i;
  100. t = ctx->count[0];
  101. pad_len = 0;
  102. i = 8;
  103. while (--i >= 0)
  104. {
  105. bits[i] = t;
  106. t = !(i % 4) ? ctx->count[++pad_len] : (t >> 8);
  107. }
  108. t = (ctx->count[0] >> 3) & 0x3f;
  109. pad_len = (t < 56) ? (56 - t) : ((sizeof(ctx->buff) + 56) - t);
  110. sl_sha2_update(ctx, padding, pad_len);
  111. sl_sha2_update(ctx, bits, sizeof(bits));
  112. }