rbnacl-libsodium 1.0.15.1 → 1.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +4 -6
  3. data/CHANGES.md +50 -42
  4. data/Gemfile +1 -1
  5. data/README.md +3 -1
  6. data/Rakefile +46 -0
  7. data/ext/rbnacl/extconf.rb +16 -1
  8. data/lib/rbnacl/libsodium.rb +16 -8
  9. data/lib/rbnacl/libsodium/version.rb +1 -1
  10. data/rbnacl-libsodium.gemspec +3 -0
  11. data/vendor/libsodium/AUTHORS +20 -5
  12. data/vendor/libsodium/ChangeLog +25 -0
  13. data/vendor/libsodium/Makefile.in +3 -1
  14. data/vendor/libsodium/README.markdown +2 -1
  15. data/vendor/libsodium/aclocal.m4 +1 -0
  16. data/vendor/libsodium/autom4te.cache/output.1 +836 -123
  17. data/vendor/libsodium/autom4te.cache/output.4 +21342 -0
  18. data/vendor/libsodium/autom4te.cache/requests +801 -554
  19. data/vendor/libsodium/autom4te.cache/traces.1 +717 -596
  20. data/vendor/libsodium/autom4te.cache/traces.4 +4355 -0
  21. data/vendor/libsodium/builds/Makefile.in +3 -1
  22. data/vendor/libsodium/builds/msvc/resource.h +1 -1
  23. data/vendor/libsodium/builds/msvc/resource.rc +2 -2
  24. data/vendor/libsodium/builds/msvc/version.h +2 -2
  25. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj +17 -8
  26. data/vendor/libsodium/builds/msvc/vs2010/libsodium/libsodium.vcxproj.filters +60 -24
  27. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj +17 -8
  28. data/vendor/libsodium/builds/msvc/vs2012/libsodium/libsodium.vcxproj.filters +60 -24
  29. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj +17 -8
  30. data/vendor/libsodium/builds/msvc/vs2013/libsodium/libsodium.vcxproj.filters +60 -24
  31. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj +17 -8
  32. data/vendor/libsodium/builds/msvc/vs2015/libsodium/libsodium.vcxproj.filters +60 -24
  33. data/vendor/libsodium/builds/msvc/vs2017/libsodium/libsodium.vcxproj +17 -8
  34. data/vendor/libsodium/builds/msvc/vs2017/libsodium/libsodium.vcxproj.filters +60 -24
  35. data/vendor/libsodium/configure +834 -121
  36. data/vendor/libsodium/configure.ac +55 -13
  37. data/vendor/libsodium/contrib/Findsodium.cmake +22 -3
  38. data/vendor/libsodium/contrib/Makefile.in +3 -1
  39. data/vendor/libsodium/dist-build/Makefile.in +3 -1
  40. data/vendor/libsodium/dist-build/android-build.sh +2 -2
  41. data/vendor/libsodium/dist-build/emscripten-symbols.def +38 -26
  42. data/vendor/libsodium/dist-build/emscripten.sh +23 -8
  43. data/vendor/libsodium/dist-build/msys2-win32.sh +1 -1
  44. data/vendor/libsodium/dist-build/msys2-win64.sh +1 -1
  45. data/vendor/libsodium/libsodium.vcxproj +17 -8
  46. data/vendor/libsodium/libsodium.vcxproj.filters +41 -14
  47. data/vendor/libsodium/m4/ax_tls.m4 +74 -0
  48. data/vendor/libsodium/msvc-scripts/Makefile.in +3 -1
  49. data/vendor/libsodium/msvc-scripts/process.bat +2 -2
  50. data/vendor/libsodium/packaging/dotnet-core/README.md +5 -5
  51. data/vendor/libsodium/packaging/dotnet-core/prepare.py +7 -7
  52. data/vendor/libsodium/packaging/nuget/package.config +1 -1
  53. data/vendor/libsodium/regen-msvc/libsodium.vcxproj +326 -0
  54. data/vendor/libsodium/regen-msvc/libsodium.vcxproj.filters +23 -0
  55. data/vendor/libsodium/regen-msvc/libsodium.vcxproj.filters.tpl +35 -0
  56. data/vendor/libsodium/regen-msvc/libsodium.vcxproj.tpl +93 -0
  57. data/vendor/libsodium/regen-msvc/regen-msvc.py +136 -0
  58. data/vendor/libsodium/regen-msvc/tl_libsodium.vcxproj.filters.tpl +23 -0
  59. data/vendor/libsodium/regen-msvc/tl_libsodium.vcxproj.tpl +331 -0
  60. data/vendor/libsodium/src/Makefile.in +3 -1
  61. data/vendor/libsodium/src/libsodium/Makefile.am +40 -24
  62. data/vendor/libsodium/src/libsodium/Makefile.in +238 -180
  63. data/vendor/libsodium/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +10 -2
  64. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/core_ed25519.c +79 -0
  65. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/ed25519_ref10.c +2031 -0
  66. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_25_5/base.h +1344 -0
  67. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_25_5/base2.h +40 -0
  68. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_25_5/constants.h +20 -0
  69. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_25_5/fe.h +220 -0
  70. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_51/base.h +1344 -0
  71. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_51/base2.h +40 -0
  72. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_51/constants.h +21 -0
  73. data/vendor/libsodium/src/libsodium/crypto_core/ed25519/ref10/fe_51/fe.h +116 -0
  74. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2.h +1 -1
  75. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-compress-ref.c +2 -1
  76. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2b/ref/blake2b-ref.c +14 -82
  77. data/vendor/libsodium/src/libsodium/crypto_generichash/blake2b/ref/generichash_blake2b.c +1 -0
  78. data/vendor/libsodium/src/libsodium/crypto_hash/sha256/cp/hash_sha256_cp.c +3 -3
  79. data/vendor/libsodium/src/libsodium/crypto_hash/sha512/cp/hash_sha512_cp.c +3 -3
  80. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/donna/poly1305_donna64.h +1 -7
  81. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/onetimeauth_poly1305.c +1 -0
  82. data/vendor/libsodium/src/libsodium/crypto_onetimeauth/poly1305/sse2/poly1305_sse2.c +0 -6
  83. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.c +19 -92
  84. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2-core.h +72 -4
  85. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.c +5 -1
  86. data/vendor/libsodium/src/libsodium/crypto_pwhash/argon2/argon2.h +1 -1
  87. data/vendor/libsodium/src/libsodium/crypto_pwhash/scryptsalsa208sha256/scrypt_platform.c +10 -7
  88. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.c +108 -231
  89. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/ref10/x25519_ref10.h +1 -1
  90. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/curve25519_sandy2x.c +27 -27
  91. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe51_invert.c +2 -1
  92. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/sandy2x/fe_frombytes_sandy2x.c +6 -3
  93. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/scalarmult_curve25519.c +3 -11
  94. data/vendor/libsodium/src/libsodium/crypto_scalarmult/ed25519/ref10/scalarmult_ed25519_ref10.c +86 -0
  95. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/keypair.c +28 -26
  96. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/obsolete.c +32 -30
  97. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/open.c +14 -115
  98. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/sign.c +56 -13
  99. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/ref10/{ed25519_ref10.h → sign_ed25519_ref10.h} +2 -5
  100. data/vendor/libsodium/src/libsodium/crypto_sign/ed25519/sign_ed25519.c +1 -1
  101. data/vendor/libsodium/src/libsodium/crypto_stream/chacha20/stream_chacha20.c +1 -0
  102. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/stream_salsa20.c +1 -0
  103. data/vendor/libsodium/src/libsodium/crypto_stream/salsa20/xmm6int/u8.h +1 -1
  104. data/vendor/libsodium/src/libsodium/include/Makefile.am +2 -0
  105. data/vendor/libsodium/src/libsodium/include/Makefile.in +13 -9
  106. data/vendor/libsodium/src/libsodium/include/sodium.h +2 -0
  107. data/vendor/libsodium/src/libsodium/include/sodium/crypto_core_ed25519.h +37 -0
  108. data/vendor/libsodium/src/libsodium/include/sodium/crypto_scalarmult.h +8 -0
  109. data/vendor/libsodium/src/libsodium/include/sodium/crypto_scalarmult_curve25519.h +8 -0
  110. data/vendor/libsodium/src/libsodium/include/sodium/crypto_scalarmult_ed25519.h +41 -0
  111. data/vendor/libsodium/src/libsodium/include/sodium/private/common.h +18 -0
  112. data/vendor/libsodium/src/libsodium/include/sodium/private/ed25519_ref10.h +125 -0
  113. data/vendor/libsodium/src/libsodium/include/sodium/private/ed25519_ref10_fe_25_5.h +1050 -0
  114. data/vendor/libsodium/src/libsodium/include/sodium/private/ed25519_ref10_fe_51.h +518 -0
  115. data/vendor/libsodium/src/libsodium/include/sodium/runtime.h +3 -0
  116. data/vendor/libsodium/src/libsodium/include/sodium/utils.h +3 -0
  117. data/vendor/libsodium/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +182 -102
  118. data/vendor/libsodium/src/libsodium/sodium/core.c +30 -2
  119. data/vendor/libsodium/src/libsodium/sodium/runtime.c +14 -0
  120. data/vendor/libsodium/src/libsodium/sodium/utils.c +46 -0
  121. data/vendor/libsodium/test/Makefile.in +3 -1
  122. data/vendor/libsodium/test/default/Makefile.am +16 -0
  123. data/vendor/libsodium/test/default/Makefile.in +71 -23
  124. data/vendor/libsodium/test/default/aead_aes256gcm.c +1 -1
  125. data/vendor/libsodium/test/default/cmptest.h +4 -0
  126. data/vendor/libsodium/test/default/core3.c +44 -4
  127. data/vendor/libsodium/test/default/core3.exp +2 -0
  128. data/vendor/libsodium/test/default/core4.c +1 -1
  129. data/vendor/libsodium/test/default/core_ed25519.c +151 -0
  130. data/vendor/libsodium/test/default/core_ed25519.exp +1 -0
  131. data/vendor/libsodium/test/default/ed25519_convert.c +9 -1
  132. data/vendor/libsodium/test/default/index.html.tpl +17 -3
  133. data/vendor/libsodium/test/default/kdf.c +4 -2
  134. data/vendor/libsodium/test/default/metamorphic.c +8 -8
  135. data/vendor/libsodium/test/default/misuse.c +29 -1
  136. data/vendor/libsodium/test/default/pwhash_argon2i.c +9 -3
  137. data/vendor/libsodium/test/default/pwhash_argon2i.exp +2 -2
  138. data/vendor/libsodium/test/default/pwhash_argon2id.c +7 -2
  139. data/vendor/libsodium/test/default/pwhash_argon2id.exp +2 -2
  140. data/vendor/libsodium/test/default/scalarmult.c +0 -2
  141. data/vendor/libsodium/test/default/scalarmult.exp +0 -1
  142. data/vendor/libsodium/test/default/scalarmult_ed25519.c +90 -0
  143. data/vendor/libsodium/test/default/scalarmult_ed25519.exp +1 -0
  144. data/vendor/libsodium/test/default/secretbox_easy2.c +1 -1
  145. data/vendor/libsodium/test/default/secretstream.c +52 -3
  146. data/vendor/libsodium/test/default/sign.c +16 -0
  147. data/vendor/libsodium/test/default/sodium_core.c +1 -0
  148. data/vendor/libsodium/test/default/sodium_utils.c +2 -1
  149. data/vendor/libsodium/test/default/xchacha20.c +2 -1
  150. metadata +63 -12
  151. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/base.h +0 -1344
  152. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/base2.h +0 -40
  153. data/vendor/libsodium/src/libsodium/crypto_core/curve25519/ref10/curve25519_ref10.c +0 -2797
  154. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.c +0 -545
  155. data/vendor/libsodium/src/libsodium/crypto_scalarmult/curve25519/donna_c64/curve25519_donna_c64.h +0 -10
  156. data/vendor/libsodium/src/libsodium/include/sodium/private/curve25519_ref10.h +0 -132
  157. data/vendor/libsodium/test/default/index-wasm.html.tpl +0 -118
@@ -1,545 +0,0 @@
1
- /* Copyright 2008, Google Inc.
2
- * All rights reserved.
3
- *
4
- * Code released into the public domain.
5
- *
6
- * curve25519-donna: Curve25519 elliptic curve, public key function
7
- *
8
- * http://code.google.com/p/curve25519-donna/
9
- *
10
- * Adam Langley <agl@imperialviolet.org>
11
- * Parts optimised by floodyberry
12
- * Derived from public domain C code by Daniel J. Bernstein <djb@cr.yp.to>
13
- *
14
- * More information about curve25519 can be found here
15
- * http://cr.yp.to/ecdh.html
16
- *
17
- * djb's sample implementation of curve25519 is written in a special assembly
18
- * language called qhasm and uses the floating point registers.
19
- *
20
- * This is, almost, a clean room reimplementation from the curve25519 paper. It
21
- * uses many of the tricks described therein. Only the crecip function is taken
22
- * from the sample implementation.
23
- */
24
-
25
- #include <stdint.h>
26
- #include <string.h>
27
-
28
- #ifdef HAVE_TI_MODE
29
-
30
- #include "../scalarmult_curve25519.h"
31
- #include "curve25519_donna_c64.h"
32
- #include "utils.h"
33
-
34
- typedef uint8_t u8;
35
- typedef uint64_t limb;
36
- typedef limb felem[5];
37
- /* Special gcc mode for 128-bit integers */
38
- typedef unsigned uint128_t __attribute__((mode(TI)));
39
-
40
- /* Sum two numbers: output += in */
41
- static inline void
42
- fsum(limb *output, const limb *in)
43
- {
44
- output[0] += in[0];
45
- output[1] += in[1];
46
- output[2] += in[2];
47
- output[3] += in[3];
48
- output[4] += in[4];
49
- }
50
-
51
- /* Find the difference of two numbers: output = in - output
52
- * (note the order of the arguments!)
53
- *
54
- * Assumes that out[i] < 2**52
55
- * On return, out[i] < 2**55
56
- */
57
- static inline void
58
- fdifference_backwards(felem out, const felem in)
59
- {
60
- /* 152 is 19 << 3 */
61
- static const limb two54m152 = (((limb)1) << 54) - 152;
62
- static const limb two54m8 = (((limb)1) << 54) - 8;
63
-
64
- out[0] = in[0] + two54m152 - out[0];
65
- out[1] = in[1] + two54m8 - out[1];
66
- out[2] = in[2] + two54m8 - out[2];
67
- out[3] = in[3] + two54m8 - out[3];
68
- out[4] = in[4] + two54m8 - out[4];
69
- }
70
-
71
- /* Multiply a number by a scalar: output = in * scalar */
72
- static inline void
73
- fscalar_product(felem output, const felem in, const limb scalar)
74
- {
75
- uint128_t a;
76
-
77
- a = in[0] * (uint128_t)scalar;
78
- output[0] = ((limb)a) & 0x7ffffffffffff;
79
-
80
- a = in[1] * (uint128_t)scalar + ((limb)(a >> 51));
81
- output[1] = ((limb)a) & 0x7ffffffffffff;
82
-
83
- a = in[2] * (uint128_t)scalar + ((limb)(a >> 51));
84
- output[2] = ((limb)a) & 0x7ffffffffffff;
85
-
86
- a = in[3] * (uint128_t)scalar + ((limb)(a >> 51));
87
- output[3] = ((limb)a) & 0x7ffffffffffff;
88
-
89
- a = in[4] * (uint128_t)scalar + ((limb)(a >> 51));
90
- output[4] = ((limb)a) & 0x7ffffffffffff;
91
-
92
- output[0] += (a >> 51) * 19;
93
- }
94
-
95
- /* Multiply two numbers: output = in2 * in
96
- *
97
- * output must be distinct to both inputs. The inputs are reduced coefficient
98
- * form, the output is not.
99
- *
100
- * Assumes that in[i] < 2**55 and likewise for in2.
101
- * On return, output[i] < 2**52
102
- */
103
- static inline void
104
- fmul(felem output, const felem in2, const felem in)
105
- {
106
- uint128_t t[5];
107
- limb r0, r1, r2, r3, r4, s0, s1, s2, s3, s4, c;
108
-
109
- r0 = in[0];
110
- r1 = in[1];
111
- r2 = in[2];
112
- r3 = in[3];
113
- r4 = in[4];
114
-
115
- s0 = in2[0];
116
- s1 = in2[1];
117
- s2 = in2[2];
118
- s3 = in2[3];
119
- s4 = in2[4];
120
-
121
- t[0] = ((uint128_t)r0) * s0;
122
- t[1] = ((uint128_t)r0) * s1 + ((uint128_t)r1) * s0;
123
- t[2] = ((uint128_t)r0) * s2 + ((uint128_t)r2) * s0 + ((uint128_t)r1) * s1;
124
- t[3] = ((uint128_t)r0) * s3 + ((uint128_t)r3) * s0 + ((uint128_t)r1) * s2
125
- + ((uint128_t)r2) * s1;
126
- t[4] = ((uint128_t)r0) * s4 + ((uint128_t)r4) * s0 + ((uint128_t)r3) * s1
127
- + ((uint128_t)r1) * s3 + ((uint128_t)r2) * s2;
128
-
129
- r4 *= 19;
130
- r1 *= 19;
131
- r2 *= 19;
132
- r3 *= 19;
133
-
134
- t[0] += ((uint128_t)r4) * s1 + ((uint128_t)r1) * s4 + ((uint128_t)r2) * s3
135
- + ((uint128_t)r3) * s2;
136
- t[1] += ((uint128_t)r4) * s2 + ((uint128_t)r2) * s4 + ((uint128_t)r3) * s3;
137
- t[2] += ((uint128_t)r4) * s3 + ((uint128_t)r3) * s4;
138
- t[3] += ((uint128_t)r4) * s4;
139
-
140
- r0 = (limb)t[0] & 0x7ffffffffffff;
141
- c = (limb)(t[0] >> 51);
142
- t[1] += c;
143
- r1 = (limb)t[1] & 0x7ffffffffffff;
144
- c = (limb)(t[1] >> 51);
145
- t[2] += c;
146
- r2 = (limb)t[2] & 0x7ffffffffffff;
147
- c = (limb)(t[2] >> 51);
148
- t[3] += c;
149
- r3 = (limb)t[3] & 0x7ffffffffffff;
150
- c = (limb)(t[3] >> 51);
151
- t[4] += c;
152
- r4 = (limb)t[4] & 0x7ffffffffffff;
153
- c = (limb)(t[4] >> 51);
154
- r0 += c * 19;
155
- c = r0 >> 51;
156
- r0 = r0 & 0x7ffffffffffff;
157
- r1 += c;
158
- c = r1 >> 51;
159
- r1 = r1 & 0x7ffffffffffff;
160
- r2 += c;
161
-
162
- output[0] = r0;
163
- output[1] = r1;
164
- output[2] = r2;
165
- output[3] = r3;
166
- output[4] = r4;
167
- }
168
-
169
- static inline void
170
- fsquare_times(felem output, const felem in, limb count)
171
- {
172
- uint128_t t[5];
173
- limb r0, r1, r2, r3, r4, c;
174
- limb d0, d1, d2, d4, d419;
175
-
176
- r0 = in[0];
177
- r1 = in[1];
178
- r2 = in[2];
179
- r3 = in[3];
180
- r4 = in[4];
181
-
182
- do {
183
- d0 = r0 * 2;
184
- d1 = r1 * 2;
185
- d2 = r2 * 2 * 19;
186
- d419 = r4 * 19;
187
- d4 = d419 * 2;
188
-
189
- t[0] = ((uint128_t)r0) * r0 + ((uint128_t)d4) * r1
190
- + (((uint128_t)d2) * (r3));
191
- t[1] = ((uint128_t)d0) * r1 + ((uint128_t)d4) * r2
192
- + (((uint128_t)r3) * (r3 * 19));
193
- t[2] = ((uint128_t)d0) * r2 + ((uint128_t)r1) * r1
194
- + (((uint128_t)d4) * (r3));
195
- t[3] = ((uint128_t)d0) * r3 + ((uint128_t)d1) * r2
196
- + (((uint128_t)r4) * (d419));
197
- t[4] = ((uint128_t)d0) * r4 + ((uint128_t)d1) * r3
198
- + (((uint128_t)r2) * (r2));
199
-
200
- r0 = (limb)t[0] & 0x7ffffffffffff;
201
- c = (limb)(t[0] >> 51);
202
- t[1] += c;
203
- r1 = (limb)t[1] & 0x7ffffffffffff;
204
- c = (limb)(t[1] >> 51);
205
- t[2] += c;
206
- r2 = (limb)t[2] & 0x7ffffffffffff;
207
- c = (limb)(t[2] >> 51);
208
- t[3] += c;
209
- r3 = (limb)t[3] & 0x7ffffffffffff;
210
- c = (limb)(t[3] >> 51);
211
- t[4] += c;
212
- r4 = (limb)t[4] & 0x7ffffffffffff;
213
- c = (limb)(t[4] >> 51);
214
- r0 += c * 19;
215
- c = r0 >> 51;
216
- r0 = r0 & 0x7ffffffffffff;
217
- r1 += c;
218
- c = r1 >> 51;
219
- r1 = r1 & 0x7ffffffffffff;
220
- r2 += c;
221
- } while (--count);
222
-
223
- output[0] = r0;
224
- output[1] = r1;
225
- output[2] = r2;
226
- output[3] = r3;
227
- output[4] = r4;
228
- }
229
-
230
- #ifdef NATIVE_LITTLE_ENDIAN
231
- static inline limb
232
- load_limb(const u8 *in)
233
- {
234
- limb out;
235
-
236
- memcpy(&out, in, sizeof(limb));
237
- return out;
238
- }
239
- static inline void
240
- store_limb(u8 *out, limb in)
241
- {
242
- memcpy(out, &in, sizeof(limb));
243
- }
244
- #else
245
- static inline limb
246
- load_limb(const u8 *in)
247
- {
248
- return ((limb)in[0]) | (((limb)in[1]) << 8) | (((limb)in[2]) << 16)
249
- | (((limb)in[3]) << 24) | (((limb)in[4]) << 32)
250
- | (((limb)in[5]) << 40) | (((limb)in[6]) << 48)
251
- | (((limb)in[7]) << 56);
252
- }
253
-
254
- static inline void
255
- store_limb(u8 *out, limb in)
256
- {
257
- out[0] = in & 0xff;
258
- out[1] = (in >> 8) & 0xff;
259
- out[2] = (in >> 16) & 0xff;
260
- out[3] = (in >> 24) & 0xff;
261
- out[4] = (in >> 32) & 0xff;
262
- out[5] = (in >> 40) & 0xff;
263
- out[6] = (in >> 48) & 0xff;
264
- out[7] = (in >> 56) & 0xff;
265
- }
266
- #endif
267
-
268
- /* Take a little-endian, 32-byte number and expand it into polynomial form */
269
- static void
270
- fexpand(limb *output, const u8 *in)
271
- {
272
- output[0] = load_limb(in) & 0x7ffffffffffff;
273
- output[1] = (load_limb(in + 6) >> 3) & 0x7ffffffffffff;
274
- output[2] = (load_limb(in + 12) >> 6) & 0x7ffffffffffff;
275
- output[3] = (load_limb(in + 19) >> 1) & 0x7ffffffffffff;
276
- output[4] = (load_limb(in + 24) >> 12) & 0x7ffffffffffff;
277
- }
278
-
279
- /* Take a fully reduced polynomial form number and contract it into a
280
- * little-endian, 32-byte array
281
- */
282
- static void
283
- fcontract(u8 *output, const felem input)
284
- {
285
- uint128_t t[5];
286
-
287
- t[0] = input[0];
288
- t[1] = input[1];
289
- t[2] = input[2];
290
- t[3] = input[3];
291
- t[4] = input[4];
292
-
293
- t[1] += t[0] >> 51;
294
- t[0] &= 0x7ffffffffffff;
295
- t[2] += t[1] >> 51;
296
- t[1] &= 0x7ffffffffffff;
297
- t[3] += t[2] >> 51;
298
- t[2] &= 0x7ffffffffffff;
299
- t[4] += t[3] >> 51;
300
- t[3] &= 0x7ffffffffffff;
301
- t[0] += 19 * (t[4] >> 51);
302
- t[4] &= 0x7ffffffffffff;
303
-
304
- t[1] += t[0] >> 51;
305
- t[0] &= 0x7ffffffffffff;
306
- t[2] += t[1] >> 51;
307
- t[1] &= 0x7ffffffffffff;
308
- t[3] += t[2] >> 51;
309
- t[2] &= 0x7ffffffffffff;
310
- t[4] += t[3] >> 51;
311
- t[3] &= 0x7ffffffffffff;
312
- t[0] += 19 * (t[4] >> 51);
313
- t[4] &= 0x7ffffffffffff;
314
-
315
- /* now t is between 0 and 2^255-1, properly carried. */
316
- /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
317
-
318
- t[0] += 19;
319
-
320
- t[1] += t[0] >> 51;
321
- t[0] &= 0x7ffffffffffff;
322
- t[2] += t[1] >> 51;
323
- t[1] &= 0x7ffffffffffff;
324
- t[3] += t[2] >> 51;
325
- t[2] &= 0x7ffffffffffff;
326
- t[4] += t[3] >> 51;
327
- t[3] &= 0x7ffffffffffff;
328
- t[0] += 19 * (t[4] >> 51);
329
- t[4] &= 0x7ffffffffffff;
330
-
331
- /* now between 19 and 2^255-1 in both cases, and offset by 19. */
332
-
333
- t[0] += 0x8000000000000 - 19;
334
- t[1] += 0x8000000000000 - 1;
335
- t[2] += 0x8000000000000 - 1;
336
- t[3] += 0x8000000000000 - 1;
337
- t[4] += 0x8000000000000 - 1;
338
-
339
- /* now between 2^255 and 2^256-20, and offset by 2^255. */
340
-
341
- t[1] += t[0] >> 51;
342
- t[0] &= 0x7ffffffffffff;
343
- t[2] += t[1] >> 51;
344
- t[1] &= 0x7ffffffffffff;
345
- t[3] += t[2] >> 51;
346
- t[2] &= 0x7ffffffffffff;
347
- t[4] += t[3] >> 51;
348
- t[3] &= 0x7ffffffffffff;
349
- t[4] &= 0x7ffffffffffff;
350
-
351
- store_limb(output, t[0] | (t[1] << 51));
352
- store_limb(output + 8, (t[1] >> 13) | (t[2] << 38));
353
- store_limb(output + 16, (t[2] >> 26) | (t[3] << 25));
354
- store_limb(output + 24, (t[3] >> 39) | (t[4] << 12));
355
- }
356
-
357
- /* Input: Q, Q', Q-Q'
358
- * Output: 2Q, Q+Q'
359
- *
360
- * x2 z2: long form
361
- * x3 z3: long form
362
- * x z: short form, destroyed
363
- * xprime zprime: short form, destroyed
364
- * qmqp: short form, preserved
365
- */
366
- static void
367
- fmonty(limb *x2, limb *z2, /* output 2Q */
368
- limb *x3, limb *z3, /* output Q + Q' */
369
- limb *x, limb *z, /* input Q */
370
- limb *xprime, limb *zprime, /* input Q' */
371
- const limb *qmqp /* input Q - Q' */)
372
- {
373
- limb origx[5], origxprime[5], zzz[5], xx[5], zz[5], xxprime[5], zzprime[5],
374
- zzzprime[5];
375
-
376
- memcpy(origx, x, 5 * sizeof(limb));
377
- fsum(x, z);
378
- fdifference_backwards(z, origx); /* does x - z */
379
-
380
- memcpy(origxprime, xprime, sizeof(limb) * 5);
381
- fsum(xprime, zprime);
382
- fdifference_backwards(zprime, origxprime);
383
- fmul(xxprime, xprime, z);
384
- fmul(zzprime, x, zprime);
385
- memcpy(origxprime, xxprime, sizeof(limb) * 5);
386
- fsum(xxprime, zzprime);
387
- fdifference_backwards(zzprime, origxprime);
388
- fsquare_times(x3, xxprime, 1);
389
- fsquare_times(zzzprime, zzprime, 1);
390
- fmul(z3, zzzprime, qmqp);
391
-
392
- fsquare_times(xx, x, 1);
393
- fsquare_times(zz, z, 1);
394
- fmul(x2, xx, zz);
395
- fdifference_backwards(zz, xx); /* does zz = xx - zz */
396
- fscalar_product(zzz, zz, 121665);
397
- fsum(zzz, xx);
398
- fmul(z2, zz, zzz);
399
- }
400
-
401
- /* -----------------------------------------------------------------------------
402
- Maybe swap the contents of two limb arrays (@a and @b), each @len elements
403
- long. Perform the swap iff @swap is non-zero.
404
-
405
- This function performs the swap without leaking any side-channel
406
- information.
407
- -----------------------------------------------------------------------------
408
- */
409
- static void
410
- swap_conditional(limb a[5], limb b[5], limb iswap)
411
- {
412
- const limb swap = -iswap;
413
- unsigned i;
414
-
415
- for (i = 0; i < 5; ++i) {
416
- const limb x = swap & (a[i] ^ b[i]);
417
- a[i] ^= x;
418
- b[i] ^= x;
419
- }
420
- }
421
-
422
- /* Calculates nQ where Q is the x-coordinate of a point on the curve
423
- *
424
- * resultx/resultz: the x coordinate of the resulting curve point (short form)
425
- * n: a little endian, 32-byte number
426
- * q: a point of the curve (short form)
427
- */
428
- static void
429
- cmult(limb *resultx, limb *resultz, const u8 *n, const limb *q)
430
- {
431
- limb a[5] = { 0 }, b[5] = { 1 }, c[5] = { 1 }, d[5] = { 0 };
432
- limb *nqpqx = a, *nqpqz = b, *nqx = c, *nqz = d, *t;
433
- limb e[5] = { 0 }, f[5] = { 1 }, g[5] = { 0 }, h[5] = { 1 };
434
- limb *nqpqx2 = e, *nqpqz2 = f, *nqx2 = g, *nqz2 = h;
435
-
436
- unsigned i, j;
437
-
438
- memcpy(nqpqx, q, sizeof(limb) * 5);
439
-
440
- for (i = 0; i < 32; ++i) {
441
- u8 byte = n[31 - i];
442
- for (j = 0; j < 8; ++j) {
443
- const limb bit = byte >> 7;
444
-
445
- swap_conditional(nqx, nqpqx, bit);
446
- swap_conditional(nqz, nqpqz, bit);
447
- fmonty(nqx2, nqz2, nqpqx2, nqpqz2, nqx, nqz, nqpqx, nqpqz, q);
448
- swap_conditional(nqx2, nqpqx2, bit);
449
- swap_conditional(nqz2, nqpqz2, bit);
450
-
451
- t = nqx;
452
- nqx = nqx2;
453
- nqx2 = t;
454
- t = nqz;
455
- nqz = nqz2;
456
- nqz2 = t;
457
- t = nqpqx;
458
- nqpqx = nqpqx2;
459
- nqpqx2 = t;
460
- t = nqpqz;
461
- nqpqz = nqpqz2;
462
- nqpqz2 = t;
463
-
464
- byte <<= 1;
465
- }
466
- }
467
-
468
- memcpy(resultx, nqx, sizeof(limb) * 5);
469
- memcpy(resultz, nqz, sizeof(limb) * 5);
470
- }
471
-
472
- /* -----------------------------------------------------------------------------
473
- Shamelessly copied from djb's code, tightened a little
474
- -----------------------------------------------------------------------------
475
- */
476
- static void
477
- crecip(felem out, const felem z)
478
- {
479
- felem a, t0, b, c;
480
-
481
- /* 2 */ fsquare_times(a, z, 1); /* a = 2 */
482
- /* 8 */ fsquare_times(t0, a, 2);
483
- /* 9 */ fmul(b, t0, z); /* b = 9 */
484
- /* 11 */ fmul(a, b, a); /* a = 11 */
485
- /* 22 */ fsquare_times(t0, a, 1);
486
- /* 2^5 - 2^0 = 31 */ fmul(b, t0, b);
487
- /* 2^10 - 2^5 */ fsquare_times(t0, b, 5);
488
- /* 2^10 - 2^0 */ fmul(b, t0, b);
489
- /* 2^20 - 2^10 */ fsquare_times(t0, b, 10);
490
- /* 2^20 - 2^0 */ fmul(c, t0, b);
491
- /* 2^40 - 2^20 */ fsquare_times(t0, c, 20);
492
- /* 2^40 - 2^0 */ fmul(t0, t0, c);
493
- /* 2^50 - 2^10 */ fsquare_times(t0, t0, 10);
494
- /* 2^50 - 2^0 */ fmul(b, t0, b);
495
- /* 2^100 - 2^50 */ fsquare_times(t0, b, 50);
496
- /* 2^100 - 2^0 */ fmul(c, t0, b);
497
- /* 2^200 - 2^100 */ fsquare_times(t0, c, 100);
498
- /* 2^200 - 2^0 */ fmul(t0, t0, c);
499
- /* 2^250 - 2^50 */ fsquare_times(t0, t0, 50);
500
- /* 2^250 - 2^0 */ fmul(t0, t0, b);
501
- /* 2^255 - 2^5 */ fsquare_times(t0, t0, 5);
502
- /* 2^255 - 21 */ fmul(out, t0, a);
503
- }
504
-
505
- static const unsigned char basepoint[32] = { 9 };
506
-
507
- static int
508
- crypto_scalarmult_curve25519_donna_c64(unsigned char *mypublic,
509
- const unsigned char *secret,
510
- const unsigned char *basepoint)
511
- {
512
- limb bp[5], x[5], z[5], zmone[5];
513
- uint8_t e[32];
514
- int i;
515
-
516
- for (i = 0; i < 32; ++i) {
517
- e[i] = secret[i];
518
- }
519
- e[0] &= 248;
520
- e[31] &= 127;
521
- e[31] |= 64;
522
-
523
- fexpand(bp, basepoint);
524
- cmult(x, z, e, bp);
525
- crecip(zmone, z);
526
- fmul(z, x, zmone);
527
- fcontract(mypublic, z);
528
-
529
- return 0;
530
- }
531
-
532
- static int
533
- crypto_scalarmult_curve25519_donna_c64_base(unsigned char *q,
534
- const unsigned char *n)
535
- {
536
- return crypto_scalarmult_curve25519_donna_c64(q, n, basepoint);
537
- }
538
-
539
- struct crypto_scalarmult_curve25519_implementation
540
- crypto_scalarmult_curve25519_donna_c64_implementation = {
541
- SODIUM_C99(.mult =) crypto_scalarmult_curve25519_donna_c64,
542
- SODIUM_C99(.mult_base =) crypto_scalarmult_curve25519_donna_c64_base
543
- };
544
-
545
- #endif