sha1-sw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Software implementation of SHA-1.
  3. */
  4. #include "ssh.h"
  5. #include "sha1.h"
  6. static bool sha1_sw_available(void)
  7. {
  8. /* Software SHA-1 is always available */
  9. return true;
  10. }
  11. static inline uint32_t rol(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 Par(uint32_t x, uint32_t y, uint32_t z)
  24. {
  25. return (x ^ y ^ z);
  26. }
  27. static inline void sha1_sw_round(
  28. unsigned round_index, const uint32_t *schedule,
  29. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e,
  30. uint32_t f, uint32_t constant)
  31. {
  32. *e = rol(*a, 5) + f + *e + schedule[round_index] + constant;
  33. *b = rol(*b, 30);
  34. }
  35. static void sha1_sw_block(uint32_t *core, const uint8_t *block)
  36. {
  37. uint32_t w[SHA1_ROUNDS];
  38. uint32_t a,b,c,d,e;
  39. for (size_t t = 0; t < 16; t++)
  40. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  41. for (size_t t = 16; t < SHA1_ROUNDS; t++)
  42. w[t] = rol(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
  43. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  44. e = core[4];
  45. size_t t = 0;
  46. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  47. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Ch(b,c,d), SHA1_STAGE0_CONSTANT);
  48. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Ch(a,b,c), SHA1_STAGE0_CONSTANT);
  49. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Ch(e,a,b), SHA1_STAGE0_CONSTANT);
  50. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Ch(d,e,a), SHA1_STAGE0_CONSTANT);
  51. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Ch(c,d,e), SHA1_STAGE0_CONSTANT);
  52. }
  53. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  54. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE1_CONSTANT);
  55. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE1_CONSTANT);
  56. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE1_CONSTANT);
  57. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE1_CONSTANT);
  58. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE1_CONSTANT);
  59. }
  60. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  61. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Maj(b,c,d), SHA1_STAGE2_CONSTANT);
  62. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Maj(a,b,c), SHA1_STAGE2_CONSTANT);
  63. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Maj(e,a,b), SHA1_STAGE2_CONSTANT);
  64. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Maj(d,e,a), SHA1_STAGE2_CONSTANT);
  65. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Maj(c,d,e), SHA1_STAGE2_CONSTANT);
  66. }
  67. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  68. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE3_CONSTANT);
  69. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE3_CONSTANT);
  70. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE3_CONSTANT);
  71. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE3_CONSTANT);
  72. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE3_CONSTANT);
  73. }
  74. core[0] += a; core[1] += b; core[2] += c; core[3] += d; core[4] += e;
  75. smemclr(w, sizeof(w));
  76. }
  77. typedef struct sha1_sw {
  78. uint32_t core[5];
  79. sha1_block blk;
  80. BinarySink_IMPLEMENTATION;
  81. ssh_hash hash;
  82. } sha1_sw;
  83. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len);
  84. static ssh_hash *sha1_sw_new(const ssh_hashalg *alg)
  85. {
  86. sha1_sw *s = snew(sha1_sw);
  87. s->hash.vt = alg;
  88. BinarySink_INIT(s, sha1_sw_write);
  89. BinarySink_DELEGATE_INIT(&s->hash, s);
  90. return &s->hash;
  91. }
  92. static void sha1_sw_reset(ssh_hash *hash)
  93. {
  94. sha1_sw *s = container_of(hash, sha1_sw, hash);
  95. memcpy(s->core, sha1_initial_state, sizeof(s->core));
  96. sha1_block_setup(&s->blk);
  97. }
  98. static void sha1_sw_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  99. {
  100. sha1_sw *copy = container_of(hcopy, sha1_sw, hash);
  101. sha1_sw *orig = container_of(horig, sha1_sw, hash);
  102. memcpy(copy, orig, sizeof(*copy));
  103. BinarySink_COPIED(copy);
  104. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  105. }
  106. static void sha1_sw_free(ssh_hash *hash)
  107. {
  108. sha1_sw *s = container_of(hash, sha1_sw, hash);
  109. smemclr(s, sizeof(*s));
  110. sfree(s);
  111. }
  112. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len)
  113. {
  114. sha1_sw *s = BinarySink_DOWNCAST(bs, sha1_sw);
  115. while (len > 0)
  116. if (sha1_block_write(&s->blk, &vp, &len))
  117. sha1_sw_block(s->core, s->blk.block);
  118. }
  119. static void sha1_sw_digest(ssh_hash *hash, uint8_t *digest)
  120. {
  121. sha1_sw *s = container_of(hash, sha1_sw, hash);
  122. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  123. for (size_t i = 0; i < 5; i++)
  124. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  125. }
  126. SHA1_VTABLE(sw, "unaccelerated");