Haraka512_X86.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. #if NETCOREAPP3_0_OR_GREATER
  4. using System;
  5. using System.Buffers.Binary;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.Intrinsics;
  9. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  10. {
  11. using Aes = System.Runtime.Intrinsics.X86.Aes;
  12. using Sse2 = System.Runtime.Intrinsics.X86.Sse2;
  13. public static class Haraka512_X86
  14. {
  15. public static bool IsSupported => Aes.IsSupported;
  16. // Haraka round constants
  17. internal static readonly Vector128<byte>[] DefaultRoundConstants = new Vector128<byte>[]
  18. {
  19. Vector128.Create(0x9D, 0x7B, 0x81, 0x75, 0xF0, 0xFE, 0xC5, 0xB2, 0x0A, 0xC0, 0x20, 0xE6, 0x4C, 0x70, 0x84, 0x06),
  20. Vector128.Create(0x17, 0xF7, 0x08, 0x2F, 0xA4, 0x6B, 0x0F, 0x64, 0x6B, 0xA0, 0xF3, 0x88, 0xE1, 0xB4, 0x66, 0x8B),
  21. Vector128.Create(0x14, 0x91, 0x02, 0x9F, 0x60, 0x9D, 0x02, 0xCF, 0x98, 0x84, 0xF2, 0x53, 0x2D, 0xDE, 0x02, 0x34),
  22. Vector128.Create(0x79, 0x4F, 0x5B, 0xFD, 0xAF, 0xBC, 0xF3, 0xBB, 0x08, 0x4F, 0x7B, 0x2E, 0xE6, 0xEA, 0xD6, 0x0E),
  23. Vector128.Create(0x44, 0x70, 0x39, 0xBE, 0x1C, 0xCD, 0xEE, 0x79, 0x8B, 0x44, 0x72, 0x48, 0xCB, 0xB0, 0xCF, 0xCB),
  24. Vector128.Create(0x7B, 0x05, 0x8A, 0x2B, 0xED, 0x35, 0x53, 0x8D, 0xB7, 0x32, 0x90, 0x6E, 0xEE, 0xCD, 0xEA, 0x7E),
  25. Vector128.Create(0x1B, 0xEF, 0x4F, 0xDA, 0x61, 0x27, 0x41, 0xE2, 0xD0, 0x7C, 0x2E, 0x5E, 0x43, 0x8F, 0xC2, 0x67),
  26. Vector128.Create(0x3B, 0x0B, 0xC7, 0x1F, 0xE2, 0xFD, 0x5F, 0x67, 0x07, 0xCC, 0xCA, 0xAF, 0xB0, 0xD9, 0x24, 0x29),
  27. Vector128.Create(0xEE, 0x65, 0xD4, 0xB9, 0xCA, 0x8F, 0xDB, 0xEC, 0xE9, 0x7F, 0x86, 0xE6, 0xF1, 0x63, 0x4D, 0xAB),
  28. Vector128.Create(0x33, 0x7E, 0x03, 0xAD, 0x4F, 0x40, 0x2A, 0x5B, 0x64, 0xCD, 0xB7, 0xD4, 0x84, 0xBF, 0x30, 0x1C),
  29. Vector128.Create(0x00, 0x98, 0xF6, 0x8D, 0x2E, 0x8B, 0x02, 0x69, 0xBF, 0x23, 0x17, 0x94, 0xB9, 0x0B, 0xCC, 0xB2),
  30. Vector128.Create(0x8A, 0x2D, 0x9D, 0x5C, 0xC8, 0x9E, 0xAA, 0x4A, 0x72, 0x55, 0x6F, 0xDE, 0xA6, 0x78, 0x04, 0xFA),
  31. Vector128.Create(0xD4, 0x9F, 0x12, 0x29, 0x2E, 0x4F, 0xFA, 0x0E, 0x12, 0x2A, 0x77, 0x6B, 0x2B, 0x9F, 0xB4, 0xDF),
  32. Vector128.Create(0xEE, 0x12, 0x6A, 0xBB, 0xAE, 0x11, 0xD6, 0x32, 0x36, 0xA2, 0x49, 0xF4, 0x44, 0x03, 0xA1, 0x1E),
  33. Vector128.Create(0xA6, 0xEC, 0xA8, 0x9C, 0xC9, 0x00, 0x96, 0x5F, 0x84, 0x00, 0x05, 0x4B, 0x88, 0x49, 0x04, 0xAF),
  34. Vector128.Create(0xEC, 0x93, 0xE5, 0x27, 0xE3, 0xC7, 0xA2, 0x78, 0x4F, 0x9C, 0x19, 0x9D, 0xD8, 0x5E, 0x02, 0x21),
  35. Vector128.Create(0x73, 0x01, 0xD4, 0x82, 0xCD, 0x2E, 0x28, 0xB9, 0xB7, 0xC9, 0x59, 0xA7, 0xF8, 0xAA, 0x3A, 0xBF),
  36. Vector128.Create(0x6B, 0x7D, 0x30, 0x10, 0xD9, 0xEF, 0xF2, 0x37, 0x17, 0xB0, 0x86, 0x61, 0x0D, 0x70, 0x60, 0x62),
  37. Vector128.Create(0xC6, 0x9A, 0xFC, 0xF6, 0x53, 0x91, 0xC2, 0x81, 0x43, 0x04, 0x30, 0x21, 0xC2, 0x45, 0xCA, 0x5A),
  38. Vector128.Create(0x3A, 0x94, 0xD1, 0x36, 0xE8, 0x92, 0xAF, 0x2C, 0xBB, 0x68, 0x6B, 0x22, 0x3C, 0x97, 0x23, 0x92),
  39. Vector128.Create(0xB4, 0x71, 0x10, 0xE5, 0x58, 0xB9, 0xBA, 0x6C, 0xEB, 0x86, 0x58, 0x22, 0x38, 0x92, 0xBF, 0xD3),
  40. Vector128.Create(0x8D, 0x12, 0xE1, 0x24, 0xDD, 0xFD, 0x3D, 0x93, 0x77, 0xC6, 0xF0, 0xAE, 0xE5, 0x3C, 0x86, 0xDB),
  41. Vector128.Create(0xB1, 0x12, 0x22, 0xCB, 0xE3, 0x8D, 0xE4, 0x83, 0x9C, 0xA0, 0xEB, 0xFF, 0x68, 0x62, 0x60, 0xBB),
  42. Vector128.Create(0x7D, 0xF7, 0x2B, 0xC7, 0x4E, 0x1A, 0xB9, 0x2D, 0x9C, 0xD1, 0xE4, 0xE2, 0xDC, 0xD3, 0x4B, 0x73),
  43. Vector128.Create(0x4E, 0x92, 0xB3, 0x2C, 0xC4, 0x15, 0x14, 0x4B, 0x43, 0x1B, 0x30, 0x61, 0xC3, 0x47, 0xBB, 0x43),
  44. Vector128.Create(0x99, 0x68, 0xEB, 0x16, 0xDD, 0x31, 0xB2, 0x03, 0xF6, 0xEF, 0x07, 0xE7, 0xA8, 0x75, 0xA7, 0xDB),
  45. Vector128.Create(0x2C, 0x47, 0xCA, 0x7E, 0x02, 0x23, 0x5E, 0x8E, 0x77, 0x59, 0x75, 0x3C, 0x4B, 0x61, 0xF3, 0x6D),
  46. Vector128.Create(0xF9, 0x17, 0x86, 0xB8, 0xB9, 0xE5, 0x1B, 0x6D, 0x77, 0x7D, 0xDE, 0xD6, 0x17, 0x5A, 0xA7, 0xCD),
  47. Vector128.Create(0x5D, 0xEE, 0x46, 0xA9, 0x9D, 0x06, 0x6C, 0x9D, 0xAA, 0xE9, 0xA8, 0x6B, 0xF0, 0x43, 0x6B, 0xEC),
  48. Vector128.Create(0xC1, 0x27, 0xF3, 0x3B, 0x59, 0x11, 0x53, 0xA2, 0x2B, 0x33, 0x57, 0xF9, 0x50, 0x69, 0x1E, 0xCB),
  49. Vector128.Create(0xD9, 0xD0, 0x0E, 0x60, 0x53, 0x03, 0xED, 0xE4, 0x9C, 0x61, 0xDA, 0x00, 0x75, 0x0C, 0xEE, 0x2C),
  50. Vector128.Create(0x50, 0xA3, 0xA4, 0x63, 0xBC, 0xBA, 0xBB, 0x80, 0xAB, 0x0C, 0xE9, 0x96, 0xA1, 0xA5, 0xB1, 0xF0),
  51. Vector128.Create(0x39, 0xCA, 0x8D, 0x93, 0x30, 0xDE, 0x0D, 0xAB, 0x88, 0x29, 0x96, 0x5E, 0x02, 0xB1, 0x3D, 0xAE),
  52. Vector128.Create(0x42, 0xB4, 0x75, 0x2E, 0xA8, 0xF3, 0x14, 0x88, 0x0B, 0xA4, 0x54, 0xD5, 0x38, 0x8F, 0xBB, 0x17),
  53. Vector128.Create(0xF6, 0x16, 0x0A, 0x36, 0x79, 0xB7, 0xB6, 0xAE, 0xD7, 0x7F, 0x42, 0x5F, 0x5B, 0x8A, 0xBB, 0x34),
  54. Vector128.Create(0xDE, 0xAF, 0xBA, 0xFF, 0x18, 0x59, 0xCE, 0x43, 0x38, 0x54, 0xE5, 0xCB, 0x41, 0x52, 0xF6, 0x26),
  55. Vector128.Create(0x78, 0xC9, 0x9E, 0x83, 0xF7, 0x9C, 0xCA, 0xA2, 0x6A, 0x02, 0xF3, 0xB9, 0x54, 0x9A, 0xE9, 0x4C),
  56. Vector128.Create(0x35, 0x12, 0x90, 0x22, 0x28, 0x6E, 0xC0, 0x40, 0xBE, 0xF7, 0xDF, 0x1B, 0x1A, 0xA5, 0x51, 0xAE),
  57. Vector128.Create(0xCF, 0x59, 0xA6, 0x48, 0x0F, 0xBC, 0x73, 0xC1, 0x2B, 0xD2, 0x7E, 0xBA, 0x3C, 0x61, 0xC1, 0xA0),
  58. Vector128.Create(0xA1, 0x9D, 0xC5, 0xE9, 0xFD, 0xBD, 0xD6, 0x4A, 0x88, 0x82, 0x28, 0x02, 0x03, 0xCC, 0x6A, 0x75),
  59. };
  60. public static void Hash(ReadOnlySpan<byte> input, Span<byte> output)
  61. {
  62. if (!IsSupported)
  63. throw new PlatformNotSupportedException(nameof(Haraka512_X86));
  64. var s0 = Load128(input[ ..16]);
  65. var s1 = Load128(input[16..32]);
  66. var s2 = Load128(input[32..48]);
  67. var s3 = Load128(input[48..64]);
  68. ImplRounds(ref s0, ref s1, ref s2, ref s3, DefaultRoundConstants.AsSpan(0, 40));
  69. s0 = Sse2.Xor(s0, Load128(input[ ..16]));
  70. s1 = Sse2.Xor(s1, Load128(input[16..32]));
  71. s2 = Sse2.Xor(s2, Load128(input[32..48]));
  72. s3 = Sse2.Xor(s3, Load128(input[48..64]));
  73. Store64(s0.GetUpper(), output[ .. 8]);
  74. Store64(s1.GetUpper(), output[ 8..16]);
  75. Store64(s2.GetLower(), output[16..24]);
  76. Store64(s3.GetLower(), output[24..32]);
  77. }
  78. public static void Hash(ReadOnlySpan<byte> input, Span<byte> output,
  79. ReadOnlySpan<Vector128<byte>> roundConstants)
  80. {
  81. if (!IsSupported)
  82. throw new PlatformNotSupportedException(nameof(Haraka512_X86));
  83. var s0 = Load128(input[ ..16]);
  84. var s1 = Load128(input[16..32]);
  85. var s2 = Load128(input[32..48]);
  86. var s3 = Load128(input[48..64]);
  87. ImplRounds(ref s0, ref s1, ref s2, ref s3, roundConstants[..40]);
  88. s0 = Sse2.Xor(s0, Load128(input[ ..16]));
  89. s1 = Sse2.Xor(s1, Load128(input[16..32]));
  90. s2 = Sse2.Xor(s2, Load128(input[32..48]));
  91. s3 = Sse2.Xor(s3, Load128(input[48..64]));
  92. Store64(s0.GetUpper(), output[ .. 8]);
  93. Store64(s1.GetUpper(), output[ 8..16]);
  94. Store64(s2.GetLower(), output[16..24]);
  95. Store64(s3.GetLower(), output[24..32]);
  96. }
  97. public static void Permute(ReadOnlySpan<byte> input, Span<byte> output)
  98. {
  99. if (!IsSupported)
  100. throw new PlatformNotSupportedException(nameof(Haraka512_X86));
  101. var s0 = Load128(input[ ..16]);
  102. var s1 = Load128(input[16..32]);
  103. var s2 = Load128(input[32..48]);
  104. var s3 = Load128(input[48..64]);
  105. ImplRounds(ref s0, ref s1, ref s2, ref s3, DefaultRoundConstants.AsSpan(0, 40));
  106. Store128(s0, output[ ..16]);
  107. Store128(s1, output[16..32]);
  108. Store128(s2, output[32..48]);
  109. Store128(s3, output[48..64]);
  110. }
  111. public static void Permute(ReadOnlySpan<byte> input, Span<byte> output,
  112. ReadOnlySpan<Vector128<byte>> roundConstants)
  113. {
  114. if (!IsSupported)
  115. throw new PlatformNotSupportedException(nameof(Haraka512_X86));
  116. var s0 = Load128(input[ ..16]);
  117. var s1 = Load128(input[16..32]);
  118. var s2 = Load128(input[32..48]);
  119. var s3 = Load128(input[48..64]);
  120. ImplRounds(ref s0, ref s1, ref s2, ref s3, roundConstants[..40]);
  121. Store128(s0, output[ ..16]);
  122. Store128(s1, output[16..32]);
  123. Store128(s2, output[32..48]);
  124. Store128(s3, output[48..64]);
  125. }
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. private static void ImplRounds(ref Vector128<byte> s0, ref Vector128<byte> s1, ref Vector128<byte> s2,
  128. ref Vector128<byte> s3, ReadOnlySpan<Vector128<byte>> rc)
  129. {
  130. ImplRound(ref s0, ref s1, ref s2, ref s3, rc[ .. 8]);
  131. ImplRound(ref s0, ref s1, ref s2, ref s3, rc[ 8..16]);
  132. ImplRound(ref s0, ref s1, ref s2, ref s3, rc[16..24]);
  133. ImplRound(ref s0, ref s1, ref s2, ref s3, rc[24..32]);
  134. ImplRound(ref s0, ref s1, ref s2, ref s3, rc[32..40]);
  135. }
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. private static void ImplRound(ref Vector128<byte> s0, ref Vector128<byte> s1, ref Vector128<byte> s2,
  138. ref Vector128<byte> s3, ReadOnlySpan<Vector128<byte>> rc)
  139. {
  140. ImplAes(ref s0, ref s1, ref s2, ref s3, rc[..8]);
  141. ImplMix(ref s0, ref s1, ref s2, ref s3);
  142. }
  143. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  144. private static void ImplAes(ref Vector128<byte> s0, ref Vector128<byte> s1, ref Vector128<byte> s2,
  145. ref Vector128<byte> s3, ReadOnlySpan<Vector128<byte>> rc)
  146. {
  147. s0 = Aes.Encrypt(s0, rc[0]);
  148. s1 = Aes.Encrypt(s1, rc[1]);
  149. s2 = Aes.Encrypt(s2, rc[2]);
  150. s3 = Aes.Encrypt(s3, rc[3]);
  151. s0 = Aes.Encrypt(s0, rc[4]);
  152. s1 = Aes.Encrypt(s1, rc[5]);
  153. s2 = Aes.Encrypt(s2, rc[6]);
  154. s3 = Aes.Encrypt(s3, rc[7]);
  155. }
  156. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  157. private static void ImplMix(ref Vector128<byte> s0, ref Vector128<byte> s1, ref Vector128<byte> s2,
  158. ref Vector128<byte> s3)
  159. {
  160. var t0 = s0.AsUInt32();
  161. var t1 = s1.AsUInt32();
  162. var t2 = s2.AsUInt32();
  163. var t3 = s3.AsUInt32();
  164. var u0 = Sse2.UnpackLow(t0, t1);
  165. var u1 = Sse2.UnpackHigh(t0, t1);
  166. var u2 = Sse2.UnpackLow(t2, t3);
  167. var u3 = Sse2.UnpackHigh(t2, t3);
  168. s0 = Sse2.UnpackHigh(u1, u3).AsByte();
  169. s1 = Sse2.UnpackLow(u2, u0).AsByte();
  170. s2 = Sse2.UnpackHigh(u2, u0).AsByte();
  171. s3 = Sse2.UnpackLow(u1, u3).AsByte();
  172. }
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. private static Vector128<byte> Load128(ReadOnlySpan<byte> t)
  175. {
  176. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<byte>>() == 16)
  177. return MemoryMarshal.Read<Vector128<byte>>(t);
  178. return Vector128.Create(
  179. BinaryPrimitives.ReadUInt64LittleEndian(t[..8]),
  180. BinaryPrimitives.ReadUInt64LittleEndian(t[8..])
  181. ).AsByte();
  182. }
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. private static void Store128(Vector128<byte> s, Span<byte> t)
  185. {
  186. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<byte>>() == 16)
  187. {
  188. MemoryMarshal.Write(t, ref s);
  189. return;
  190. }
  191. var u = s.AsUInt64();
  192. BinaryPrimitives.WriteUInt64LittleEndian(t[..8], u.GetElement(0));
  193. BinaryPrimitives.WriteUInt64LittleEndian(t[8..], u.GetElement(1));
  194. }
  195. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  196. private static void Store64(Vector64<byte> s, Span<byte> t)
  197. {
  198. if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector64<byte>>() == 8)
  199. {
  200. MemoryMarshal.Write(t, ref s);
  201. return;
  202. }
  203. var u = s.AsUInt64();
  204. BinaryPrimitives.WriteUInt64LittleEndian(t, u.ToScalar());
  205. }
  206. }
  207. }
  208. #endif
  209. #pragma warning restore
  210. #endif