sha1-neon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Hardware-accelerated implementation of SHA-1 using Arm NEON.
  3. */
  4. #include "ssh.h"
  5. #include "sha1.h"
  6. #if USE_ARM64_NEON_H
  7. #include <arm64_neon.h>
  8. #else
  9. #include <arm_neon.h>
  10. #endif
  11. static bool sha1_neon_available(void)
  12. {
  13. /*
  14. * For Arm, we delegate to a per-platform detection function (see
  15. * explanation in aes-neon.c).
  16. */
  17. return platform_sha1_neon_available();
  18. }
  19. typedef struct sha1_neon_core sha1_neon_core;
  20. struct sha1_neon_core {
  21. uint32x4_t abcd;
  22. uint32_t e;
  23. };
  24. static inline uint32x4_t sha1_neon_load_input(const uint8_t *p)
  25. {
  26. return vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(p)));
  27. }
  28. static inline uint32x4_t sha1_neon_schedule_update(
  29. uint32x4_t m4, uint32x4_t m3, uint32x4_t m2, uint32x4_t m1)
  30. {
  31. return vsha1su1q_u32(vsha1su0q_u32(m4, m3, m2), m1);
  32. }
  33. /*
  34. * SHA-1 has three different kinds of round, differing in whether they
  35. * use the Ch, Maj or Par functions defined above. Each one uses a
  36. * separate NEON instruction, so we define three inline functions for
  37. * the different round types using this macro.
  38. *
  39. * The two batches of Par-type rounds also use a different constant,
  40. * but that's passed in as an operand, so we don't need a fourth
  41. * inline function just for that.
  42. */
  43. #define SHA1_NEON_ROUND_FN(type) \
  44. static inline sha1_neon_core sha1_neon_round4_##type( \
  45. sha1_neon_core old, uint32x4_t sched, uint32x4_t constant) \
  46. { \
  47. sha1_neon_core new; \
  48. uint32x4_t round_input = vaddq_u32(sched, constant); \
  49. new.abcd = vsha1##type##q_u32(old.abcd, old.e, round_input); \
  50. new.e = vsha1h_u32(vget_lane_u32(vget_low_u32(old.abcd), 0)); \
  51. return new; \
  52. }
  53. SHA1_NEON_ROUND_FN(c)
  54. SHA1_NEON_ROUND_FN(p)
  55. SHA1_NEON_ROUND_FN(m)
  56. static inline void sha1_neon_block(sha1_neon_core *core, const uint8_t *p)
  57. {
  58. uint32x4_t constant, s0, s1, s2, s3;
  59. sha1_neon_core cr = *core;
  60. constant = vdupq_n_u32(SHA1_STAGE0_CONSTANT);
  61. s0 = sha1_neon_load_input(p);
  62. cr = sha1_neon_round4_c(cr, s0, constant);
  63. s1 = sha1_neon_load_input(p + 16);
  64. cr = sha1_neon_round4_c(cr, s1, constant);
  65. s2 = sha1_neon_load_input(p + 32);
  66. cr = sha1_neon_round4_c(cr, s2, constant);
  67. s3 = sha1_neon_load_input(p + 48);
  68. cr = sha1_neon_round4_c(cr, s3, constant);
  69. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  70. cr = sha1_neon_round4_c(cr, s0, constant);
  71. constant = vdupq_n_u32(SHA1_STAGE1_CONSTANT);
  72. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  73. cr = sha1_neon_round4_p(cr, s1, constant);
  74. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  75. cr = sha1_neon_round4_p(cr, s2, constant);
  76. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  77. cr = sha1_neon_round4_p(cr, s3, constant);
  78. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  79. cr = sha1_neon_round4_p(cr, s0, constant);
  80. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  81. cr = sha1_neon_round4_p(cr, s1, constant);
  82. constant = vdupq_n_u32(SHA1_STAGE2_CONSTANT);
  83. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  84. cr = sha1_neon_round4_m(cr, s2, constant);
  85. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  86. cr = sha1_neon_round4_m(cr, s3, constant);
  87. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  88. cr = sha1_neon_round4_m(cr, s0, constant);
  89. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  90. cr = sha1_neon_round4_m(cr, s1, constant);
  91. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  92. cr = sha1_neon_round4_m(cr, s2, constant);
  93. constant = vdupq_n_u32(SHA1_STAGE3_CONSTANT);
  94. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  95. cr = sha1_neon_round4_p(cr, s3, constant);
  96. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  97. cr = sha1_neon_round4_p(cr, s0, constant);
  98. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  99. cr = sha1_neon_round4_p(cr, s1, constant);
  100. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  101. cr = sha1_neon_round4_p(cr, s2, constant);
  102. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  103. cr = sha1_neon_round4_p(cr, s3, constant);
  104. core->abcd = vaddq_u32(core->abcd, cr.abcd);
  105. core->e += cr.e;
  106. }
  107. typedef struct sha1_neon {
  108. sha1_neon_core core;
  109. sha1_block blk;
  110. BinarySink_IMPLEMENTATION;
  111. ssh_hash hash;
  112. } sha1_neon;
  113. static void sha1_neon_write(BinarySink *bs, const void *vp, size_t len);
  114. static ssh_hash *sha1_neon_new(const ssh_hashalg *alg)
  115. {
  116. const struct sha1_extra *extra = (const struct sha1_extra *)alg->extra;
  117. if (!check_availability(extra))
  118. return NULL;
  119. sha1_neon *s = snew(sha1_neon);
  120. s->hash.vt = alg;
  121. BinarySink_INIT(s, sha1_neon_write);
  122. BinarySink_DELEGATE_INIT(&s->hash, s);
  123. return &s->hash;
  124. }
  125. static void sha1_neon_reset(ssh_hash *hash)
  126. {
  127. sha1_neon *s = container_of(hash, sha1_neon, hash);
  128. s->core.abcd = vld1q_u32(sha1_initial_state);
  129. s->core.e = sha1_initial_state[4];
  130. sha1_block_setup(&s->blk);
  131. }
  132. static void sha1_neon_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  133. {
  134. sha1_neon *copy = container_of(hcopy, sha1_neon, hash);
  135. sha1_neon *orig = container_of(horig, sha1_neon, hash);
  136. *copy = *orig; /* structure copy */
  137. BinarySink_COPIED(copy);
  138. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  139. }
  140. static void sha1_neon_free(ssh_hash *hash)
  141. {
  142. sha1_neon *s = container_of(hash, sha1_neon, hash);
  143. smemclr(s, sizeof(*s));
  144. sfree(s);
  145. }
  146. static void sha1_neon_write(BinarySink *bs, const void *vp, size_t len)
  147. {
  148. sha1_neon *s = BinarySink_DOWNCAST(bs, sha1_neon);
  149. while (len > 0)
  150. if (sha1_block_write(&s->blk, &vp, &len))
  151. sha1_neon_block(&s->core, s->blk.block);
  152. }
  153. static void sha1_neon_digest(ssh_hash *hash, uint8_t *digest)
  154. {
  155. sha1_neon *s = container_of(hash, sha1_neon, hash);
  156. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  157. vst1q_u8(digest, vrev32q_u8(vreinterpretq_u8_u32(s->core.abcd)));
  158. PUT_32BIT_MSB_FIRST(digest + 16, s->core.e);
  159. }
  160. SHA1_VTABLE(neon, "NEON accelerated");