20
|
1 ???using System; |
|
2 |
|
3 namespace UCIS.NaCl.crypto_core { |
|
4 unsafe static class hsalsa20 { |
|
5 static Boolean UseNativeFunctions = false; |
|
6 static unsafe internal Boolean EnableNativeImplementation() { |
|
7 UseNativeFunctions = false; |
|
8 Byte* dummy = stackalloc Byte[32]; |
|
9 try { |
|
10 if (Native.crypto_core_hsalsa20(dummy, dummy, dummy, dummy) != 0) return false; |
|
11 } catch (Exception) { |
|
12 return false; |
|
13 } |
|
14 return UseNativeFunctions = true; |
|
15 } |
|
16 |
|
17 const int ROUNDS = 20; |
|
18 |
|
19 static UInt32 rotate(UInt32 u, int c) { |
|
20 return (u << c) | (u >> (32 - c)); |
|
21 } |
|
22 |
|
23 static UInt32 load_littleendian(Byte* x) { |
|
24 return (UInt32)(x[0] | (x[1] << 8) | (x[2] << 16) | (x[3] << 24)); |
|
25 } |
|
26 |
|
27 static void store_littleendian(Byte* x, UInt32 u) { |
|
28 x[0] = (Byte)u; u >>= 8; |
|
29 x[1] = (Byte)u; u >>= 8; |
|
30 x[2] = (Byte)u; u >>= 8; |
|
31 x[3] = (Byte)u; |
|
32 } |
|
33 |
|
34 public static void crypto_core(Byte* outv, Byte* inv, Byte* k, Byte[] c) { |
|
35 fixed (byte* cp = c) crypto_core(outv, inv, k, cp); |
|
36 } |
|
37 public static void crypto_core(Byte* outv, Byte* inv, Byte* k, Byte* c) { |
|
38 if (UseNativeFunctions) { |
|
39 UInt64* invp = stackalloc UInt64[2]; |
|
40 invp[0] = invp[1] = 0; |
|
41 if (inv == null) inv = (Byte*)invp; |
|
42 Native.crypto_core_hsalsa20(outv, inv, k, c); |
|
43 return; |
|
44 } |
|
45 |
|
46 UInt32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; |
|
47 |
|
48 x0 = load_littleendian(c + 0); |
|
49 x1 = load_littleendian(k + 0); |
|
50 x2 = load_littleendian(k + 4); |
|
51 x3 = load_littleendian(k + 8); |
|
52 x4 = load_littleendian(k + 12); |
|
53 x5 = load_littleendian(c + 4); |
|
54 if (inv != null) { |
|
55 x6 = load_littleendian(inv + 0); |
|
56 x7 = load_littleendian(inv + 4); |
|
57 x8 = load_littleendian(inv + 8); |
|
58 x9 = load_littleendian(inv + 12); |
|
59 } else { |
|
60 x6 = x7 = x8 = x9 = 0; |
|
61 } |
|
62 x10 = load_littleendian(c + 8); |
|
63 x11 = load_littleendian(k + 16); |
|
64 x12 = load_littleendian(k + 20); |
|
65 x13 = load_littleendian(k + 24); |
|
66 x14 = load_littleendian(k + 28); |
|
67 x15 = load_littleendian(c + 12); |
|
68 |
|
69 for (int i = ROUNDS; i > 0; i -= 2) { |
|
70 x4 ^= rotate(x0 + x12, 7); |
|
71 x8 ^= rotate(x4 + x0, 9); |
|
72 x12 ^= rotate(x8 + x4, 13); |
|
73 x0 ^= rotate(x12 + x8, 18); |
|
74 x9 ^= rotate(x5 + x1, 7); |
|
75 x13 ^= rotate(x9 + x5, 9); |
|
76 x1 ^= rotate(x13 + x9, 13); |
|
77 x5 ^= rotate(x1 + x13, 18); |
|
78 x14 ^= rotate(x10 + x6, 7); |
|
79 x2 ^= rotate(x14 + x10, 9); |
|
80 x6 ^= rotate(x2 + x14, 13); |
|
81 x10 ^= rotate(x6 + x2, 18); |
|
82 x3 ^= rotate(x15 + x11, 7); |
|
83 x7 ^= rotate(x3 + x15, 9); |
|
84 x11 ^= rotate(x7 + x3, 13); |
|
85 x15 ^= rotate(x11 + x7, 18); |
|
86 x1 ^= rotate(x0 + x3, 7); |
|
87 x2 ^= rotate(x1 + x0, 9); |
|
88 x3 ^= rotate(x2 + x1, 13); |
|
89 x0 ^= rotate(x3 + x2, 18); |
|
90 x6 ^= rotate(x5 + x4, 7); |
|
91 x7 ^= rotate(x6 + x5, 9); |
|
92 x4 ^= rotate(x7 + x6, 13); |
|
93 x5 ^= rotate(x4 + x7, 18); |
|
94 x11 ^= rotate(x10 + x9, 7); |
|
95 x8 ^= rotate(x11 + x10, 9); |
|
96 x9 ^= rotate(x8 + x11, 13); |
|
97 x10 ^= rotate(x9 + x8, 18); |
|
98 x12 ^= rotate(x15 + x14, 7); |
|
99 x13 ^= rotate(x12 + x15, 9); |
|
100 x14 ^= rotate(x13 + x12, 13); |
|
101 x15 ^= rotate(x14 + x13, 18); |
|
102 } |
|
103 |
|
104 store_littleendian(outv + 0, x0); |
|
105 store_littleendian(outv + 4, x5); |
|
106 store_littleendian(outv + 8, x10); |
|
107 store_littleendian(outv + 12, x15); |
|
108 store_littleendian(outv + 16, x6); |
|
109 store_littleendian(outv + 20, x7); |
|
110 store_littleendian(outv + 24, x8); |
|
111 store_littleendian(outv + 28, x9); |
|
112 } |
|
113 } |
|
114 } |