sl_sha_init.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* sl_sha_init.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@student.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2018/09/27 00:59:20 by bchanot #+# #+# */
  9. /* Updated: 2018/10/10 12:54:19 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "ft_ssl.h"
  13. #include "sl_sha2.h"
  14. #include "sl_sha3_5.h"
  15. void sl_sha2_init(t_sha2 *ctx, t_uint8 sha)
  16. {
  17. if (sha == SHA256)
  18. {
  19. ctx->state[0] = 0x6a09e667;
  20. ctx->state[1] = 0xbb67ae85;
  21. ctx->state[2] = 0x3c6ef372;
  22. ctx->state[3] = 0xa54ff53a;
  23. ctx->state[4] = 0x510e527f;
  24. ctx->state[5] = 0x9b05688c;
  25. ctx->state[6] = 0x1f83d9ab;
  26. ctx->state[7] = 0x5be0cd19;
  27. }
  28. else if (sha == SHA224)
  29. {
  30. ctx->state[0] = 0xc1059ed8;
  31. ctx->state[1] = 0x367cd507;
  32. ctx->state[2] = 0x3070dd17;
  33. ctx->state[3] = 0xf70e5939;
  34. ctx->state[4] = 0xffc00b31;
  35. ctx->state[5] = 0x68581511;
  36. ctx->state[6] = 0x64f98fa7;
  37. ctx->state[7] = 0xbefa4fa4;
  38. }
  39. ft_bzero(ctx->count, sizeof(ctx->count));
  40. ft_bzero(ctx->buff, sizeof(ctx->buff));
  41. }
  42. void sl_sha2_process_init_add(t_sha2 *ctx, t_process_sha2 *t,
  43. t_bool init)
  44. {
  45. if (init)
  46. {
  47. t->a = ctx->state[0];
  48. t->b = ctx->state[1];
  49. t->c = ctx->state[2];
  50. t->d = ctx->state[3];
  51. t->e = ctx->state[4];
  52. t->f = ctx->state[5];
  53. t->g = ctx->state[6];
  54. t->h = ctx->state[7];
  55. }
  56. else
  57. {
  58. ctx->state[0] += t->a;
  59. ctx->state[1] += t->b;
  60. ctx->state[2] += t->c;
  61. ctx->state[3] += t->d;
  62. ctx->state[4] += t->e;
  63. ctx->state[5] += t->f;
  64. ctx->state[6] += t->g;
  65. ctx->state[7] += t->h;
  66. }
  67. }
  68. void sl_sha3_5_init(t_sha3_5 *ctx, t_uint8 sha)
  69. {
  70. if (sha == SHA512)
  71. {
  72. ctx->state[0] = 0x6a09e667f3bcc908;
  73. ctx->state[1] = 0xbb67ae8584caa73b;
  74. ctx->state[2] = 0x3c6ef372fe94f82b;
  75. ctx->state[3] = 0xa54ff53a5f1d36f1;
  76. ctx->state[4] = 0x510e527fade682d1;
  77. ctx->state[5] = 0x9b05688c2b3e6c1f;
  78. ctx->state[6] = 0x1f83d9abfb41bd6b;
  79. ctx->state[7] = 0x5be0cd19137e2179;
  80. }
  81. else if (sha == SHA384)
  82. {
  83. ctx->state[0] = 0xcbbb9d5dc1059ed8;
  84. ctx->state[1] = 0x629a292a367cd507;
  85. ctx->state[2] = 0x9159015a3070dd17;
  86. ctx->state[3] = 0x152fecd8f70e5939;
  87. ctx->state[4] = 0x67332667ffc00b31;
  88. ctx->state[5] = 0x8eb44a8768581511;
  89. ctx->state[6] = 0xdb0c2e0d64f98fa7;
  90. ctx->state[7] = 0x47b5481dbefa4fa4;
  91. }
  92. ft_bzero(ctx->count, sizeof(ctx->count));
  93. ft_bzero(ctx->buff, sizeof(ctx->buff));
  94. }
  95. void sl_sha3_5_process_init_add(t_sha3_5 *ctx, t_process_sha3_5 *t,
  96. t_bool init)
  97. {
  98. if (init)
  99. {
  100. t->a = ctx->state[0];
  101. t->b = ctx->state[1];
  102. t->c = ctx->state[2];
  103. t->d = ctx->state[3];
  104. t->e = ctx->state[4];
  105. t->f = ctx->state[5];
  106. t->g = ctx->state[6];
  107. t->h = ctx->state[7];
  108. }
  109. else
  110. {
  111. ctx->state[0] += t->a;
  112. ctx->state[1] += t->b;
  113. ctx->state[2] += t->c;
  114. ctx->state[3] += t->d;
  115. ctx->state[4] += t->e;
  116. ctx->state[5] += t->f;
  117. ctx->state[6] += t->g;
  118. ctx->state[7] += t->h;
  119. }
  120. }