sha256-sw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Software implementation of SHA-256.
  3. */
  4. #include "ssh.h"
  5. #include "sha256.h"
  6. static bool sha256_sw_available(void)
  7. {
  8. /* Software SHA-256 is always available */
  9. return true;
  10. }
  11. static inline uint32_t ror(uint32_t x, unsigned y)
  12. {
  13. return (x << (31 & -y)) | (x >> (31 & y));
  14. }
  15. static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
  16. {
  17. return if0 ^ (ctrl & (if1 ^ if0));
  18. }
  19. static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
  20. {
  21. return (x & y) | (z & (x | y));
  22. }
  23. static inline uint32_t Sigma_0(uint32_t x)
  24. {
  25. return ror(x,2) ^ ror(x,13) ^ ror(x,22);
  26. }
  27. static inline uint32_t Sigma_1(uint32_t x)
  28. {
  29. return ror(x,6) ^ ror(x,11) ^ ror(x,25);
  30. }
  31. static inline uint32_t sigma_0(uint32_t x)
  32. {
  33. return ror(x,7) ^ ror(x,18) ^ (x >> 3);
  34. }
  35. static inline uint32_t sigma_1(uint32_t x)
  36. {
  37. return ror(x,17) ^ ror(x,19) ^ (x >> 10);
  38. }
  39. static inline void sha256_sw_round(
  40. unsigned round_index, const uint32_t *schedule,
  41. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
  42. uint32_t *e, uint32_t *f, uint32_t *g, uint32_t *h)
  43. {
  44. uint32_t t1 = *h + Sigma_1(*e) + Ch(*e,*f,*g) +
  45. sha256_round_constants[round_index] + schedule[round_index];
  46. uint32_t t2 = Sigma_0(*a) + Maj(*a,*b,*c);
  47. *d += t1;
  48. *h = t1 + t2;
  49. }
  50. static void sha256_sw_block(uint32_t *core, const uint8_t *block)
  51. {
  52. uint32_t w[SHA256_ROUNDS];
  53. uint32_t a,b,c,d,e,f,g,h;
  54. for (size_t t = 0; t < 16; t++)
  55. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  56. for (size_t t = 16; t < SHA256_ROUNDS; t++)
  57. w[t] = sigma_1(w[t-2]) + w[t-7] + sigma_0(w[t-15]) + w[t-16];
  58. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  59. e = core[4]; f = core[5]; g = core[6]; h = core[7];
  60. for (size_t t = 0; t < SHA256_ROUNDS; t += 8) {
  61. sha256_sw_round(t+0, w, &a,&b,&c,&d,&e,&f,&g,&h);
  62. sha256_sw_round(t+1, w, &h,&a,&b,&c,&d,&e,&f,&g);
  63. sha256_sw_round(t+2, w, &g,&h,&a,&b,&c,&d,&e,&f);
  64. sha256_sw_round(t+3, w, &f,&g,&h,&a,&b,&c,&d,&e);
  65. sha256_sw_round(t+4, w, &e,&f,&g,&h,&a,&b,&c,&d);
  66. sha256_sw_round(t+5, w, &d,&e,&f,&g,&h,&a,&b,&c);
  67. sha256_sw_round(t+6, w, &c,&d,&e,&f,&g,&h,&a,&b);
  68. sha256_sw_round(t+7, w, &b,&c,&d,&e,&f,&g,&h,&a);
  69. }
  70. core[0] += a; core[1] += b; core[2] += c; core[3] += d;
  71. core[4] += e; core[5] += f; core[6] += g; core[7] += h;
  72. smemclr(w, sizeof(w));
  73. }
  74. typedef struct sha256_sw {
  75. uint32_t core[8];
  76. sha256_block blk;
  77. BinarySink_IMPLEMENTATION;
  78. ssh_hash hash;
  79. } sha256_sw;
  80. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len);
  81. static ssh_hash *sha256_sw_new(const ssh_hashalg *alg)
  82. {
  83. sha256_sw *s = snew(sha256_sw);
  84. s->hash.vt = alg;
  85. BinarySink_INIT(s, sha256_sw_write);
  86. BinarySink_DELEGATE_INIT(&s->hash, s);
  87. return &s->hash;
  88. }
  89. static void sha256_sw_reset(ssh_hash *hash)
  90. {
  91. sha256_sw *s = container_of(hash, sha256_sw, hash);
  92. memcpy(s->core, sha256_initial_state, sizeof(s->core));
  93. sha256_block_setup(&s->blk);
  94. }
  95. static void sha256_sw_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  96. {
  97. sha256_sw *copy = container_of(hcopy, sha256_sw, hash);
  98. sha256_sw *orig = container_of(horig, sha256_sw, hash);
  99. memcpy(copy, orig, sizeof(*copy));
  100. BinarySink_COPIED(copy);
  101. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  102. }
  103. static void sha256_sw_free(ssh_hash *hash)
  104. {
  105. sha256_sw *s = container_of(hash, sha256_sw, hash);
  106. smemclr(s, sizeof(*s));
  107. sfree(s);
  108. }
  109. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len)
  110. {
  111. sha256_sw *s = BinarySink_DOWNCAST(bs, sha256_sw);
  112. while (len > 0)
  113. if (sha256_block_write(&s->blk, &vp, &len))
  114. sha256_sw_block(s->core, s->blk.block);
  115. }
  116. static void sha256_sw_digest(ssh_hash *hash, uint8_t *digest)
  117. {
  118. sha256_sw *s = container_of(hash, sha256_sw, hash);
  119. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  120. for (size_t i = 0; i < 8; i++)
  121. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  122. }
  123. SHA256_VTABLE(sw, "unaccelerated");