sl_sha3_5.c 3.0 KB

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