ring-native 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +3 -0
  4. data/README.md +22 -0
  5. data/Rakefile +1 -0
  6. data/ext/ring/extconf.rb +29 -0
  7. data/lib/ring/native.rb +8 -0
  8. data/lib/ring/native/version.rb +5 -0
  9. data/ring-native.gemspec +25 -0
  10. data/vendor/ring/BUILDING.md +40 -0
  11. data/vendor/ring/Cargo.toml +43 -0
  12. data/vendor/ring/LICENSE +185 -0
  13. data/vendor/ring/Makefile +35 -0
  14. data/vendor/ring/PORTING.md +163 -0
  15. data/vendor/ring/README.md +113 -0
  16. data/vendor/ring/STYLE.md +197 -0
  17. data/vendor/ring/appveyor.yml +27 -0
  18. data/vendor/ring/build.rs +108 -0
  19. data/vendor/ring/crypto/aes/aes.c +1142 -0
  20. data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +25 -0
  21. data/vendor/ring/crypto/aes/aes_test.cc +93 -0
  22. data/vendor/ring/crypto/aes/asm/aes-586.pl +2368 -0
  23. data/vendor/ring/crypto/aes/asm/aes-armv4.pl +1249 -0
  24. data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +2246 -0
  25. data/vendor/ring/crypto/aes/asm/aesni-x86.pl +1318 -0
  26. data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +2084 -0
  27. data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +675 -0
  28. data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +1364 -0
  29. data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +1565 -0
  30. data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +841 -0
  31. data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +1116 -0
  32. data/vendor/ring/crypto/aes/internal.h +87 -0
  33. data/vendor/ring/crypto/aes/mode_wrappers.c +61 -0
  34. data/vendor/ring/crypto/bn/add.c +394 -0
  35. data/vendor/ring/crypto/bn/asm/armv4-mont.pl +694 -0
  36. data/vendor/ring/crypto/bn/asm/armv8-mont.pl +1503 -0
  37. data/vendor/ring/crypto/bn/asm/bn-586.pl +774 -0
  38. data/vendor/ring/crypto/bn/asm/co-586.pl +287 -0
  39. data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +1882 -0
  40. data/vendor/ring/crypto/bn/asm/x86-mont.pl +592 -0
  41. data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +599 -0
  42. data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +1393 -0
  43. data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +3507 -0
  44. data/vendor/ring/crypto/bn/bn.c +352 -0
  45. data/vendor/ring/crypto/bn/bn_asn1.c +74 -0
  46. data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +25 -0
  47. data/vendor/ring/crypto/bn/bn_test.cc +1696 -0
  48. data/vendor/ring/crypto/bn/cmp.c +200 -0
  49. data/vendor/ring/crypto/bn/convert.c +433 -0
  50. data/vendor/ring/crypto/bn/ctx.c +311 -0
  51. data/vendor/ring/crypto/bn/div.c +594 -0
  52. data/vendor/ring/crypto/bn/exponentiation.c +1335 -0
  53. data/vendor/ring/crypto/bn/gcd.c +711 -0
  54. data/vendor/ring/crypto/bn/generic.c +1019 -0
  55. data/vendor/ring/crypto/bn/internal.h +316 -0
  56. data/vendor/ring/crypto/bn/montgomery.c +516 -0
  57. data/vendor/ring/crypto/bn/mul.c +888 -0
  58. data/vendor/ring/crypto/bn/prime.c +829 -0
  59. data/vendor/ring/crypto/bn/random.c +334 -0
  60. data/vendor/ring/crypto/bn/rsaz_exp.c +262 -0
  61. data/vendor/ring/crypto/bn/rsaz_exp.h +53 -0
  62. data/vendor/ring/crypto/bn/shift.c +276 -0
  63. data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +25 -0
  64. data/vendor/ring/crypto/bytestring/bytestring_test.cc +421 -0
  65. data/vendor/ring/crypto/bytestring/cbb.c +399 -0
  66. data/vendor/ring/crypto/bytestring/cbs.c +227 -0
  67. data/vendor/ring/crypto/bytestring/internal.h +46 -0
  68. data/vendor/ring/crypto/chacha/chacha_generic.c +140 -0
  69. data/vendor/ring/crypto/chacha/chacha_vec.c +323 -0
  70. data/vendor/ring/crypto/chacha/chacha_vec_arm.S +1447 -0
  71. data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +153 -0
  72. data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +25 -0
  73. data/vendor/ring/crypto/cipher/e_aes.c +390 -0
  74. data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +208 -0
  75. data/vendor/ring/crypto/cipher/internal.h +173 -0
  76. data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +543 -0
  77. data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +9 -0
  78. data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +475 -0
  79. data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +23 -0
  80. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +422 -0
  81. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +484 -0
  82. data/vendor/ring/crypto/cipher/test/cipher_test.txt +100 -0
  83. data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +25 -0
  84. data/vendor/ring/crypto/constant_time_test.c +304 -0
  85. data/vendor/ring/crypto/cpu-arm-asm.S +32 -0
  86. data/vendor/ring/crypto/cpu-arm.c +199 -0
  87. data/vendor/ring/crypto/cpu-intel.c +261 -0
  88. data/vendor/ring/crypto/crypto.c +151 -0
  89. data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +2118 -0
  90. data/vendor/ring/crypto/curve25519/curve25519.c +4888 -0
  91. data/vendor/ring/crypto/curve25519/x25519_test.cc +128 -0
  92. data/vendor/ring/crypto/digest/md32_common.h +181 -0
  93. data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +2725 -0
  94. data/vendor/ring/crypto/ec/ec.c +193 -0
  95. data/vendor/ring/crypto/ec/ec_curves.c +61 -0
  96. data/vendor/ring/crypto/ec/ec_key.c +228 -0
  97. data/vendor/ring/crypto/ec/ec_montgomery.c +114 -0
  98. data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +25 -0
  99. data/vendor/ring/crypto/ec/internal.h +243 -0
  100. data/vendor/ring/crypto/ec/oct.c +253 -0
  101. data/vendor/ring/crypto/ec/p256-64.c +1794 -0
  102. data/vendor/ring/crypto/ec/p256-x86_64-table.h +9548 -0
  103. data/vendor/ring/crypto/ec/p256-x86_64.c +509 -0
  104. data/vendor/ring/crypto/ec/simple.c +1007 -0
  105. data/vendor/ring/crypto/ec/util-64.c +183 -0
  106. data/vendor/ring/crypto/ec/wnaf.c +508 -0
  107. data/vendor/ring/crypto/ecdh/ecdh.c +155 -0
  108. data/vendor/ring/crypto/ecdsa/ecdsa.c +304 -0
  109. data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +193 -0
  110. data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +25 -0
  111. data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +327 -0
  112. data/vendor/ring/crypto/header_removed.h +17 -0
  113. data/vendor/ring/crypto/internal.h +495 -0
  114. data/vendor/ring/crypto/libring.Windows.vcxproj +101 -0
  115. data/vendor/ring/crypto/mem.c +98 -0
  116. data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +1045 -0
  117. data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +517 -0
  118. data/vendor/ring/crypto/modes/asm/ghash-x86.pl +1393 -0
  119. data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +1741 -0
  120. data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +422 -0
  121. data/vendor/ring/crypto/modes/ctr.c +226 -0
  122. data/vendor/ring/crypto/modes/gcm.c +1206 -0
  123. data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +25 -0
  124. data/vendor/ring/crypto/modes/gcm_test.c +348 -0
  125. data/vendor/ring/crypto/modes/internal.h +299 -0
  126. data/vendor/ring/crypto/perlasm/arm-xlate.pl +170 -0
  127. data/vendor/ring/crypto/perlasm/readme +100 -0
  128. data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +1164 -0
  129. data/vendor/ring/crypto/perlasm/x86asm.pl +292 -0
  130. data/vendor/ring/crypto/perlasm/x86gas.pl +263 -0
  131. data/vendor/ring/crypto/perlasm/x86masm.pl +200 -0
  132. data/vendor/ring/crypto/perlasm/x86nasm.pl +187 -0
  133. data/vendor/ring/crypto/poly1305/poly1305.c +331 -0
  134. data/vendor/ring/crypto/poly1305/poly1305_arm.c +301 -0
  135. data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +2015 -0
  136. data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +25 -0
  137. data/vendor/ring/crypto/poly1305/poly1305_test.cc +80 -0
  138. data/vendor/ring/crypto/poly1305/poly1305_test.txt +52 -0
  139. data/vendor/ring/crypto/poly1305/poly1305_vec.c +892 -0
  140. data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +75 -0
  141. data/vendor/ring/crypto/rand/internal.h +32 -0
  142. data/vendor/ring/crypto/rand/rand.c +189 -0
  143. data/vendor/ring/crypto/rand/urandom.c +219 -0
  144. data/vendor/ring/crypto/rand/windows.c +56 -0
  145. data/vendor/ring/crypto/refcount_c11.c +66 -0
  146. data/vendor/ring/crypto/refcount_lock.c +53 -0
  147. data/vendor/ring/crypto/refcount_test.Windows.vcxproj +25 -0
  148. data/vendor/ring/crypto/refcount_test.c +58 -0
  149. data/vendor/ring/crypto/rsa/blinding.c +462 -0
  150. data/vendor/ring/crypto/rsa/internal.h +108 -0
  151. data/vendor/ring/crypto/rsa/padding.c +300 -0
  152. data/vendor/ring/crypto/rsa/rsa.c +450 -0
  153. data/vendor/ring/crypto/rsa/rsa_asn1.c +261 -0
  154. data/vendor/ring/crypto/rsa/rsa_impl.c +944 -0
  155. data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +25 -0
  156. data/vendor/ring/crypto/rsa/rsa_test.cc +437 -0
  157. data/vendor/ring/crypto/sha/asm/sha-armv8.pl +436 -0
  158. data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +2390 -0
  159. data/vendor/ring/crypto/sha/asm/sha256-586.pl +1275 -0
  160. data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +735 -0
  161. data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +14 -0
  162. data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +14 -0
  163. data/vendor/ring/crypto/sha/asm/sha512-586.pl +911 -0
  164. data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +666 -0
  165. data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +14 -0
  166. data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +14 -0
  167. data/vendor/ring/crypto/sha/sha1.c +271 -0
  168. data/vendor/ring/crypto/sha/sha256.c +204 -0
  169. data/vendor/ring/crypto/sha/sha512.c +355 -0
  170. data/vendor/ring/crypto/test/file_test.cc +326 -0
  171. data/vendor/ring/crypto/test/file_test.h +181 -0
  172. data/vendor/ring/crypto/test/malloc.cc +150 -0
  173. data/vendor/ring/crypto/test/scoped_types.h +95 -0
  174. data/vendor/ring/crypto/test/test.Windows.vcxproj +35 -0
  175. data/vendor/ring/crypto/test/test_util.cc +46 -0
  176. data/vendor/ring/crypto/test/test_util.h +41 -0
  177. data/vendor/ring/crypto/thread_none.c +55 -0
  178. data/vendor/ring/crypto/thread_pthread.c +165 -0
  179. data/vendor/ring/crypto/thread_test.Windows.vcxproj +25 -0
  180. data/vendor/ring/crypto/thread_test.c +200 -0
  181. data/vendor/ring/crypto/thread_win.c +282 -0
  182. data/vendor/ring/examples/checkdigest.rs +103 -0
  183. data/vendor/ring/include/openssl/aes.h +121 -0
  184. data/vendor/ring/include/openssl/arm_arch.h +129 -0
  185. data/vendor/ring/include/openssl/base.h +156 -0
  186. data/vendor/ring/include/openssl/bn.h +794 -0
  187. data/vendor/ring/include/openssl/buffer.h +18 -0
  188. data/vendor/ring/include/openssl/bytestring.h +235 -0
  189. data/vendor/ring/include/openssl/chacha.h +37 -0
  190. data/vendor/ring/include/openssl/cmac.h +76 -0
  191. data/vendor/ring/include/openssl/cpu.h +184 -0
  192. data/vendor/ring/include/openssl/crypto.h +43 -0
  193. data/vendor/ring/include/openssl/curve25519.h +88 -0
  194. data/vendor/ring/include/openssl/ec.h +225 -0
  195. data/vendor/ring/include/openssl/ec_key.h +129 -0
  196. data/vendor/ring/include/openssl/ecdh.h +110 -0
  197. data/vendor/ring/include/openssl/ecdsa.h +156 -0
  198. data/vendor/ring/include/openssl/err.h +201 -0
  199. data/vendor/ring/include/openssl/mem.h +101 -0
  200. data/vendor/ring/include/openssl/obj_mac.h +71 -0
  201. data/vendor/ring/include/openssl/opensslfeatures.h +68 -0
  202. data/vendor/ring/include/openssl/opensslv.h +18 -0
  203. data/vendor/ring/include/openssl/ossl_typ.h +18 -0
  204. data/vendor/ring/include/openssl/poly1305.h +51 -0
  205. data/vendor/ring/include/openssl/rand.h +70 -0
  206. data/vendor/ring/include/openssl/rsa.h +399 -0
  207. data/vendor/ring/include/openssl/thread.h +133 -0
  208. data/vendor/ring/include/openssl/type_check.h +71 -0
  209. data/vendor/ring/mk/Common.props +63 -0
  210. data/vendor/ring/mk/Windows.props +42 -0
  211. data/vendor/ring/mk/WindowsTest.props +18 -0
  212. data/vendor/ring/mk/appveyor.bat +62 -0
  213. data/vendor/ring/mk/bottom_of_makefile.mk +54 -0
  214. data/vendor/ring/mk/ring.mk +266 -0
  215. data/vendor/ring/mk/top_of_makefile.mk +214 -0
  216. data/vendor/ring/mk/travis.sh +40 -0
  217. data/vendor/ring/mk/update-travis-yml.py +229 -0
  218. data/vendor/ring/ring.sln +153 -0
  219. data/vendor/ring/src/aead.rs +682 -0
  220. data/vendor/ring/src/agreement.rs +248 -0
  221. data/vendor/ring/src/c.rs +129 -0
  222. data/vendor/ring/src/constant_time.rs +37 -0
  223. data/vendor/ring/src/der.rs +96 -0
  224. data/vendor/ring/src/digest.rs +690 -0
  225. data/vendor/ring/src/digest_tests.txt +57 -0
  226. data/vendor/ring/src/ecc.rs +28 -0
  227. data/vendor/ring/src/ecc_build.rs +279 -0
  228. data/vendor/ring/src/ecc_curves.rs +117 -0
  229. data/vendor/ring/src/ed25519_tests.txt +2579 -0
  230. data/vendor/ring/src/exe_tests.rs +46 -0
  231. data/vendor/ring/src/ffi.rs +29 -0
  232. data/vendor/ring/src/file_test.rs +187 -0
  233. data/vendor/ring/src/hkdf.rs +153 -0
  234. data/vendor/ring/src/hkdf_tests.txt +59 -0
  235. data/vendor/ring/src/hmac.rs +414 -0
  236. data/vendor/ring/src/hmac_tests.txt +97 -0
  237. data/vendor/ring/src/input.rs +312 -0
  238. data/vendor/ring/src/lib.rs +41 -0
  239. data/vendor/ring/src/pbkdf2.rs +265 -0
  240. data/vendor/ring/src/pbkdf2_tests.txt +113 -0
  241. data/vendor/ring/src/polyfill.rs +57 -0
  242. data/vendor/ring/src/rand.rs +28 -0
  243. data/vendor/ring/src/signature.rs +314 -0
  244. data/vendor/ring/third-party/NIST/README.md +9 -0
  245. data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +263 -0
  246. data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +309 -0
  247. data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +267 -0
  248. data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +263 -0
  249. data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +309 -0
  250. data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +267 -0
  251. data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +263 -0
  252. data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +309 -0
  253. data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +267 -0
  254. data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +519 -0
  255. data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +309 -0
  256. data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +523 -0
  257. data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +519 -0
  258. data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +309 -0
  259. data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +523 -0
  260. data/vendor/ring/third-party/NIST/sha256sums.txt +1 -0
  261. metadata +333 -0
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env perl
2
+
3
+ # Copyright (c) 2015, Google Inc.
4
+ #
5
+ # Permission to use, copy, modify, and/or distribute this software for any
6
+ # purpose with or without fee is hereby granted, provided that the above
7
+ # copyright notice and this permission notice appear in all copies.
8
+ #
9
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12
+ # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14
+ # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15
+ # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
16
+
17
+ $flavour = shift;
18
+ $output = shift;
19
+ if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
20
+
21
+ $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
22
+ ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
23
+ die "can't locate x86_64-xlate.pl";
24
+
25
+ open OUT,"| \"$^X\" $xlate $flavour $output";
26
+ *STDOUT=*OUT;
27
+
28
+ print<<___;
29
+ .text
30
+
31
+ # CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to
32
+ # |out|. It returns one on success or zero on hardware failure.
33
+ # int CRYPTO_rdrand(uint8_t out[8]);
34
+ .globl CRYPTO_rdrand
35
+ .type CRYPTO_rdrand,\@function,1
36
+ .align 16
37
+ CRYPTO_rdrand:
38
+ xorq %rax, %rax
39
+ # This is rdrand %rcx. It sets rcx to a random value and sets the carry
40
+ # flag on success.
41
+ .byte 0x48, 0x0f, 0xc7, 0xf1
42
+ # An add-with-carry of zero effectively sets %rax to the carry flag.
43
+ adcq %rax, %rax
44
+ movq %rcx, 0(%rdi)
45
+ retq
46
+
47
+ # CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from
48
+ # the hardware RNG. The |len| argument must be a multiple of eight. It returns
49
+ # one on success and zero on hardware failure.
50
+ # int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
51
+ .globl CRYPTO_rdrand_multiple8_buf
52
+ .type CRYPTO_rdrand_multiple8_buf,\@function,2
53
+ .align 16
54
+ CRYPTO_rdrand_multiple8_buf:
55
+ test %rsi, %rsi
56
+ jz .Lout
57
+ movq \$8, %rdx
58
+ .Lloop:
59
+ # This is rdrand %rcx. It sets rcx to a random value and sets the carry
60
+ # flag on success.
61
+ .byte 0x48, 0x0f, 0xc7, 0xf1
62
+ jnc .Lerr
63
+ movq %rcx, 0(%rdi)
64
+ addq %rdx, %rdi
65
+ subq %rdx, %rsi
66
+ jnz .Lloop
67
+ .Lout:
68
+ movq \$1, %rax
69
+ retq
70
+ .Lerr:
71
+ xorq %rax, %rax
72
+ retq
73
+ ___
74
+
75
+ close STDOUT; # flush
@@ -0,0 +1,32 @@
1
+ /* Copyright (c) 2015, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #ifndef OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
16
+ #define OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H
17
+
18
+ #if defined(__cplusplus)
19
+ extern "C" {
20
+ #endif
21
+
22
+
23
+ /* CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating
24
+ * system. */
25
+ void CRYPTO_sysrand(uint8_t *buf, size_t len);
26
+
27
+
28
+ #if defined(__cplusplus)
29
+ } /* extern C */
30
+ #endif
31
+
32
+ #endif /* OPENSSL_HEADER_CRYPTO_RAND_INTERNAL_H */
@@ -0,0 +1,189 @@
1
+ /* Copyright (c) 2014, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/rand.h>
16
+
17
+ #include <assert.h>
18
+ #include <limits.h>
19
+ #include <string.h>
20
+
21
+ #include <openssl/chacha.h>
22
+ #include <openssl/cpu.h>
23
+ #include <openssl/mem.h>
24
+
25
+ #include "internal.h"
26
+ #include "../internal.h"
27
+
28
+
29
+ /* It's assumed that the operating system always has an unfailing source of
30
+ * entropy which is accessed via |CRYPTO_sysrand|. (If the operating system
31
+ * entropy source fails, it's up to |CRYPTO_sysrand| to abort the process—we
32
+ * don't try to handle it.)
33
+ *
34
+ * In addition, the hardware may provide a low-latency RNG. Intel's rdrand
35
+ * instruction is the canonical example of this. When a hardware RNG is
36
+ * available we don't need to worry about an RNG failure arising from fork()ing
37
+ * the process or moving a VM, so we can keep thread-local RNG state and XOR
38
+ * the hardware entropy in.
39
+ *
40
+ * (We assume that the OS entropy is safe from fork()ing and VM duplication.
41
+ * This might be a bit of a leap of faith, esp on Windows, but there's nothing
42
+ * that we can do about it.) */
43
+
44
+ /* rand_thread_state contains the per-thread state for the RNG. This is only
45
+ * used if the system has support for a hardware RNG. */
46
+ struct rand_thread_state {
47
+ uint8_t key[32];
48
+ uint64_t calls_used;
49
+ size_t bytes_used;
50
+ uint8_t partial_block[64];
51
+ unsigned partial_block_used;
52
+ };
53
+
54
+ /* kMaxCallsPerRefresh is the maximum number of |RAND_bytes| calls that we'll
55
+ * serve before reading a new key from the operating system. This only applies
56
+ * if we have a hardware RNG. */
57
+ static const unsigned kMaxCallsPerRefresh = 1024;
58
+
59
+ /* kMaxBytesPerRefresh is the maximum number of bytes that we'll return from
60
+ * |RAND_bytes| before reading a new key from the operating system. This only
61
+ * applies if we have a hardware RNG. */
62
+ static const uint64_t kMaxBytesPerRefresh = 1024 * 1024;
63
+
64
+ /* rand_thread_state_free frees a |rand_thread_state|. This is called when a
65
+ * thread exits. */
66
+ static void rand_thread_state_free(void *state) {
67
+ if (state == NULL) {
68
+ return;
69
+ }
70
+
71
+ OPENSSL_cleanse(state, sizeof(struct rand_thread_state));
72
+ OPENSSL_free(state);
73
+ }
74
+
75
+ #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
76
+
77
+ /* These functions are defined in asm/rdrand-x86_64.pl */
78
+ extern int CRYPTO_rdrand(uint8_t out[8]);
79
+ extern int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
80
+
81
+ static int have_rdrand(void) {
82
+ return (OPENSSL_ia32cap_P[1] & (1u << 30)) != 0;
83
+ }
84
+
85
+ static int hwrand(uint8_t *buf, size_t len) {
86
+ if (!have_rdrand()) {
87
+ return 0;
88
+ }
89
+
90
+ const size_t len_multiple8 = len & ~7;
91
+ if (!CRYPTO_rdrand_multiple8_buf(buf, len_multiple8)) {
92
+ return 0;
93
+ }
94
+ len -= len_multiple8;
95
+
96
+ if (len != 0) {
97
+ assert(len < 8);
98
+
99
+ uint8_t rand_buf[8];
100
+ if (!CRYPTO_rdrand(rand_buf)) {
101
+ return 0;
102
+ }
103
+ memcpy(buf + len_multiple8, rand_buf, len);
104
+ }
105
+
106
+ return 1;
107
+ }
108
+
109
+ #else
110
+
111
+ static int hwrand(uint8_t *buf, size_t len) {
112
+ return 0;
113
+ }
114
+
115
+ #endif
116
+
117
+ int RAND_bytes(uint8_t *buf, size_t len) {
118
+ if (len == 0) {
119
+ return 1;
120
+ }
121
+
122
+ if (!hwrand(buf, len)) {
123
+ /* Without a hardware RNG to save us from address-space duplication, the OS
124
+ * entropy is used directly. */
125
+ CRYPTO_sysrand(buf, len);
126
+ return 1;
127
+ }
128
+
129
+ struct rand_thread_state *state =
130
+ CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
131
+ if (state == NULL) {
132
+ state = OPENSSL_malloc(sizeof(struct rand_thread_state));
133
+ if (state == NULL ||
134
+ !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
135
+ rand_thread_state_free)) {
136
+ CRYPTO_sysrand(buf, len);
137
+ return 1;
138
+ }
139
+
140
+ memset(state->partial_block, 0, sizeof(state->partial_block));
141
+ state->calls_used = kMaxCallsPerRefresh;
142
+ }
143
+
144
+ if (state->calls_used >= kMaxCallsPerRefresh ||
145
+ state->bytes_used >= kMaxBytesPerRefresh) {
146
+ CRYPTO_sysrand(state->key, sizeof(state->key));
147
+ state->calls_used = 0;
148
+ state->bytes_used = 0;
149
+ state->partial_block_used = sizeof(state->partial_block);
150
+ }
151
+
152
+ if (len >= sizeof(state->partial_block)) {
153
+ size_t remaining = len;
154
+ while (remaining > 0) {
155
+ /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
156
+ * is sufficient and easier on 32-bit. */
157
+ static const size_t kMaxBytesPerCall = 0x80000000;
158
+ size_t todo = remaining;
159
+ if (todo > kMaxBytesPerCall) {
160
+ todo = kMaxBytesPerCall;
161
+ }
162
+ uint8_t nonce[12];
163
+ memset(nonce, 0, 4);
164
+ memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
165
+ CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0);
166
+ buf += todo;
167
+ remaining -= todo;
168
+ state->calls_used++;
169
+ }
170
+ } else {
171
+ if (sizeof(state->partial_block) - state->partial_block_used < len) {
172
+ uint8_t nonce[12];
173
+ memset(nonce, 0, 4);
174
+ memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
175
+ CRYPTO_chacha_20(state->partial_block, state->partial_block,
176
+ sizeof(state->partial_block), state->key, nonce, 0);
177
+ state->partial_block_used = 0;
178
+ }
179
+
180
+ unsigned i;
181
+ for (i = 0; i < len; i++) {
182
+ buf[i] ^= state->partial_block[state->partial_block_used++];
183
+ }
184
+ state->calls_used++;
185
+ }
186
+ state->bytes_used += len;
187
+
188
+ return 1;
189
+ }
@@ -0,0 +1,219 @@
1
+ /* Copyright (c) 2014, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/rand.h>
16
+
17
+ #if !defined(OPENSSL_WINDOWS)
18
+
19
+ #include <assert.h>
20
+ #include <errno.h>
21
+ #include <fcntl.h>
22
+ #include <string.h>
23
+ #include <unistd.h>
24
+
25
+ #include <openssl/thread.h>
26
+ #include <openssl/mem.h>
27
+
28
+ #include "internal.h"
29
+ #include "../internal.h"
30
+
31
+
32
+ /* This file implements a PRNG by reading from /dev/urandom, optionally with a
33
+ * buffer, which is unsafe across |fork|. */
34
+
35
+ #define BUF_SIZE 4096
36
+
37
+ /* rand_buffer contains unused, random bytes, some of which may have been
38
+ * consumed already. */
39
+ struct rand_buffer {
40
+ size_t used;
41
+ uint8_t rand[BUF_SIZE];
42
+ };
43
+
44
+ /* requested_lock is used to protect the |*_requested| variables. */
45
+ static struct CRYPTO_STATIC_MUTEX requested_lock = CRYPTO_STATIC_MUTEX_INIT;
46
+
47
+ /* urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
48
+ * |requested_lock|. */
49
+ static int urandom_fd_requested = -2;
50
+
51
+ /* urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. */
52
+ static int urandom_fd = -2;
53
+
54
+ /* urandom_buffering_requested is set by |RAND_enable_fork_unsafe_buffering|.
55
+ * It's protected by |requested_lock|. */
56
+ static int urandom_buffering_requested = 0;
57
+
58
+ /* urandom_buffering controls whether buffering is enabled (1) or not (0). This
59
+ * is protected by |once|. */
60
+ static int urandom_buffering = 0;
61
+
62
+ static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
63
+
64
+ /* init_once initializes the state of this module to values previously
65
+ * requested. This is the only function that modifies |urandom_fd| and
66
+ * |urandom_buffering|, whose values may be read safely after calling the
67
+ * once. */
68
+ static void init_once(void) {
69
+ CRYPTO_STATIC_MUTEX_lock_read(&requested_lock);
70
+ urandom_buffering = urandom_buffering_requested;
71
+ int fd = urandom_fd_requested;
72
+ CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
73
+
74
+ if (fd == -2) {
75
+ do {
76
+ fd = open("/dev/urandom", O_RDONLY);
77
+ } while (fd == -1 && errno == EINTR);
78
+ }
79
+
80
+ if (fd < 0) {
81
+ abort();
82
+ }
83
+
84
+ int flags = fcntl(fd, F_GETFD);
85
+ if (flags == -1) {
86
+ abort();
87
+ }
88
+ flags |= FD_CLOEXEC;
89
+ if (fcntl(fd, F_SETFD, flags) == -1) {
90
+ abort();
91
+ }
92
+ urandom_fd = fd;
93
+ }
94
+
95
+ void RAND_cleanup(void) {}
96
+
97
+ void RAND_set_urandom_fd(int fd) {
98
+ fd = dup(fd);
99
+ if (fd < 0) {
100
+ abort();
101
+ }
102
+
103
+ CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
104
+ urandom_fd_requested = fd;
105
+ CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
106
+
107
+ CRYPTO_once(&once, init_once);
108
+ if (urandom_fd != fd) {
109
+ abort(); // Already initialized.
110
+ }
111
+ }
112
+
113
+ void RAND_enable_fork_unsafe_buffering(int fd) {
114
+ if (fd >= 0) {
115
+ fd = dup(fd);
116
+ if (fd < 0) {
117
+ abort();
118
+ }
119
+ } else {
120
+ fd = -2;
121
+ }
122
+
123
+ CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
124
+ urandom_buffering_requested = 1;
125
+ urandom_fd_requested = fd;
126
+ CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
127
+
128
+ CRYPTO_once(&once, init_once);
129
+ if (urandom_buffering != 1 || (fd >= 0 && urandom_fd != fd)) {
130
+ abort(); // Already initialized.
131
+ }
132
+ }
133
+
134
+ static struct rand_buffer *get_thread_local_buffer(void) {
135
+ struct rand_buffer *buf =
136
+ CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF);
137
+ if (buf != NULL) {
138
+ return buf;
139
+ }
140
+
141
+ buf = OPENSSL_malloc(sizeof(struct rand_buffer));
142
+ if (buf == NULL) {
143
+ return NULL;
144
+ }
145
+ buf->used = BUF_SIZE; /* To trigger a |read_full| on first use. */
146
+ if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF, buf,
147
+ OPENSSL_free)) {
148
+ OPENSSL_free(buf);
149
+ return NULL;
150
+ }
151
+
152
+ return buf;
153
+ }
154
+
155
+ /* read_full reads exactly |len| bytes from |fd| into |out| and returns 1. In
156
+ * the case of an error it returns 0. */
157
+ static char read_full(int fd, uint8_t *out, size_t len) {
158
+ ssize_t r;
159
+
160
+ while (len > 0) {
161
+ do {
162
+ r = read(fd, out, len);
163
+ } while (r == -1 && errno == EINTR);
164
+
165
+ if (r <= 0) {
166
+ return 0;
167
+ }
168
+ out += r;
169
+ len -= r;
170
+ }
171
+
172
+ return 1;
173
+ }
174
+
175
+ /* read_from_buffer reads |requested| random bytes from the buffer into |out|,
176
+ * refilling it if necessary to satisfy the request. */
177
+ static void read_from_buffer(struct rand_buffer *buf,
178
+ uint8_t *out, size_t requested) {
179
+ size_t remaining = BUF_SIZE - buf->used;
180
+
181
+ while (requested > remaining) {
182
+ memcpy(out, &buf->rand[buf->used], remaining);
183
+ buf->used += remaining;
184
+ out += remaining;
185
+ requested -= remaining;
186
+
187
+ if (!read_full(urandom_fd, buf->rand, BUF_SIZE)) {
188
+ abort();
189
+ return;
190
+ }
191
+ buf->used = 0;
192
+ remaining = BUF_SIZE;
193
+ }
194
+
195
+ memcpy(out, &buf->rand[buf->used], requested);
196
+ buf->used += requested;
197
+ }
198
+
199
+ /* CRYPTO_sysrand puts |requested| random bytes into |out|. */
200
+ void CRYPTO_sysrand(uint8_t *out, size_t requested) {
201
+ if (requested == 0) {
202
+ return;
203
+ }
204
+
205
+ CRYPTO_once(&once, init_once);
206
+ if (urandom_buffering && requested < BUF_SIZE) {
207
+ struct rand_buffer *buf = get_thread_local_buffer();
208
+ if (buf != NULL) {
209
+ read_from_buffer(buf, out, requested);
210
+ return;
211
+ }
212
+ }
213
+
214
+ if (!read_full(urandom_fd, out, requested)) {
215
+ abort();
216
+ }
217
+ }
218
+
219
+ #endif /* !OPENSSL_WINDOWS */