ring-native 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,108 @@
1
+ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
+ * All rights reserved.
3
+ *
4
+ * This package is an SSL implementation written
5
+ * by Eric Young (eay@cryptsoft.com).
6
+ * The implementation was written so as to conform with Netscapes SSL.
7
+ *
8
+ * This library is free for commercial and non-commercial use as long as
9
+ * the following conditions are aheared to. The following conditions
10
+ * apply to all code found in this distribution, be it the RC4, RSA,
11
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
+ * included with this distribution is covered by the same copyright terms
13
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
+ *
15
+ * Copyright remains Eric Young's, and as such any Copyright notices in
16
+ * the code are not to be removed.
17
+ * If this package is used in a product, Eric Young should be given attribution
18
+ * as the author of the parts of the library used.
19
+ * This can be in the form of a textual message at program startup or
20
+ * in documentation (online or textual) provided with the package.
21
+ *
22
+ * Redistribution and use in source and binary forms, with or without
23
+ * modification, are permitted provided that the following conditions
24
+ * are met:
25
+ * 1. Redistributions of source code must retain the copyright
26
+ * notice, this list of conditions and the following disclaimer.
27
+ * 2. Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ * 3. All advertising materials mentioning features or use of this software
31
+ * must display the following acknowledgement:
32
+ * "This product includes cryptographic software written by
33
+ * Eric Young (eay@cryptsoft.com)"
34
+ * The word 'cryptographic' can be left out if the rouines from the library
35
+ * being used are not cryptographic related :-).
36
+ * 4. If you include any Windows specific code (or a derivative thereof) from
37
+ * the apps directory (application code) you must include an acknowledgement:
38
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ *
52
+ * The licence and distribution terms for any publically available version or
53
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
54
+ * copied and put under another distribution licence
55
+ * [including the GNU Public Licence.] */
56
+
57
+ #ifndef OPENSSL_HEADER_RSA_INTERNAL_H
58
+ #define OPENSSL_HEADER_RSA_INTERNAL_H
59
+
60
+ #include <openssl/base.h>
61
+
62
+
63
+ #if defined(__cplusplus)
64
+ extern "C" {
65
+ #endif
66
+
67
+
68
+ #define RSA_PKCS1_PADDING_SIZE 11
69
+
70
+
71
+ /* BN_BLINDING flags */
72
+ #define BN_BLINDING_NO_UPDATE 0x00000001
73
+ #define BN_BLINDING_NO_RECREATE 0x00000002
74
+
75
+ BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
76
+ void BN_BLINDING_free(BN_BLINDING *b);
77
+ int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
78
+ int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
79
+ int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
80
+ int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
81
+ int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
82
+ unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
83
+ void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
84
+ BN_BLINDING *BN_BLINDING_create_param(
85
+ BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
86
+ int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
87
+ const BIGNUM *m, BN_CTX *ctx, const BN_MONT_CTX *mont),
88
+ const BN_MONT_CTX *mont);
89
+ BN_BLINDING *rsa_setup_blinding(RSA *rsa, BN_CTX *in_ctx);
90
+
91
+
92
+ int RSA_padding_add_PKCS1_type_1(uint8_t *to, unsigned to_len,
93
+ const uint8_t *from, unsigned from_len);
94
+ int RSA_padding_check_PKCS1_type_1(uint8_t *to, unsigned to_len,
95
+ const uint8_t *from, unsigned from_len);
96
+ int RSA_padding_add_PKCS1_type_2(uint8_t *to, unsigned to_len,
97
+ const uint8_t *from, unsigned from_len);
98
+ int RSA_padding_check_PKCS1_type_2(uint8_t *to, unsigned to_len,
99
+ const uint8_t *from, unsigned from_len);
100
+ int RSA_padding_add_none(uint8_t *to, unsigned to_len, const uint8_t *from,
101
+ unsigned from_len);
102
+
103
+
104
+ #if defined(__cplusplus)
105
+ } /* extern C */
106
+ #endif
107
+
108
+ #endif /* OPENSSL_HEADER_RSA_INTERNAL_H */
@@ -0,0 +1,300 @@
1
+ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2
+ * project 2005.
3
+ */
4
+ /* ====================================================================
5
+ * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions
9
+ * are met:
10
+ *
11
+ * 1. Redistributions of source code must retain the above copyright
12
+ * notice, this list of conditions and the following disclaimer.
13
+ *
14
+ * 2. Redistributions in binary form must reproduce the above copyright
15
+ * notice, this list of conditions and the following disclaimer in
16
+ * the documentation and/or other materials provided with the
17
+ * distribution.
18
+ *
19
+ * 3. All advertising materials mentioning features or use of this
20
+ * software must display the following acknowledgment:
21
+ * "This product includes software developed by the OpenSSL Project
22
+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23
+ *
24
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25
+ * endorse or promote products derived from this software without
26
+ * prior written permission. For written permission, please contact
27
+ * licensing@OpenSSL.org.
28
+ *
29
+ * 5. Products derived from this software may not be called "OpenSSL"
30
+ * nor may "OpenSSL" appear in their names without prior written
31
+ * permission of the OpenSSL Project.
32
+ *
33
+ * 6. Redistributions of any form whatsoever must retain the following
34
+ * acknowledgment:
35
+ * "This product includes software developed by the OpenSSL Project
36
+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37
+ *
38
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
50
+ * ====================================================================
51
+ *
52
+ * This product includes cryptographic software written by Eric Young
53
+ * (eay@cryptsoft.com). This product includes software written by Tim
54
+ * Hudson (tjh@cryptsoft.com). */
55
+
56
+ #include <openssl/rsa.h>
57
+
58
+ #include <assert.h>
59
+ #include <string.h>
60
+
61
+ #include <openssl/bn.h>
62
+ #include <openssl/err.h>
63
+ #include <openssl/mem.h>
64
+ #include <openssl/rand.h>
65
+
66
+ #include "internal.h"
67
+
68
+ /* TODO(fork): don't the check functions have to be constant time? */
69
+
70
+ int RSA_padding_add_PKCS1_type_1(uint8_t *to, unsigned tlen,
71
+ const uint8_t *from, unsigned flen) {
72
+ unsigned j;
73
+ uint8_t *p;
74
+
75
+ if (tlen < RSA_PKCS1_PADDING_SIZE) {
76
+ OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
77
+ return 0;
78
+ }
79
+
80
+ if (flen > tlen - RSA_PKCS1_PADDING_SIZE) {
81
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
82
+ return 0;
83
+ }
84
+
85
+ p = (uint8_t *)to;
86
+
87
+ *(p++) = 0;
88
+ *(p++) = 1; /* Private Key BT (Block Type) */
89
+
90
+ /* pad out with 0xff data */
91
+ j = tlen - 3 - flen;
92
+ memset(p, 0xff, j);
93
+ p += j;
94
+ *(p++) = 0;
95
+ memcpy(p, from, (unsigned int)flen);
96
+ return 1;
97
+ }
98
+
99
+ int RSA_padding_check_PKCS1_type_1(uint8_t *to, unsigned tlen,
100
+ const uint8_t *from, unsigned flen) {
101
+ unsigned i, j;
102
+ const uint8_t *p;
103
+
104
+ if (flen < 2) {
105
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL);
106
+ return -1;
107
+ }
108
+
109
+ p = from;
110
+ if ((*(p++) != 0) || (*(p++) != 1)) {
111
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01);
112
+ return -1;
113
+ }
114
+
115
+ /* scan over padding data */
116
+ j = flen - 2; /* one for leading 00, one for type. */
117
+ for (i = 0; i < j; i++) {
118
+ /* should decrypt to 0xff */
119
+ if (*p != 0xff) {
120
+ if (*p == 0) {
121
+ p++;
122
+ break;
123
+ } else {
124
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT);
125
+ return -1;
126
+ }
127
+ }
128
+ p++;
129
+ }
130
+
131
+ if (i == j) {
132
+ OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING);
133
+ return -1;
134
+ }
135
+
136
+ if (i < 8) {
137
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT);
138
+ return -1;
139
+ }
140
+ i++; /* Skip over the '\0' */
141
+ j -= i;
142
+ if (j > tlen) {
143
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
144
+ return -1;
145
+ }
146
+ memcpy(to, p, j);
147
+
148
+ return j;
149
+ }
150
+
151
+ int RSA_padding_add_PKCS1_type_2(uint8_t *to, unsigned tlen,
152
+ const uint8_t *from, unsigned flen) {
153
+ unsigned i, j;
154
+ uint8_t *p;
155
+
156
+ if (tlen < RSA_PKCS1_PADDING_SIZE) {
157
+ OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
158
+ return 0;
159
+ }
160
+
161
+ if (flen > tlen - RSA_PKCS1_PADDING_SIZE) {
162
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
163
+ return 0;
164
+ }
165
+
166
+ p = (unsigned char *)to;
167
+
168
+ *(p++) = 0;
169
+ *(p++) = 2; /* Public Key BT (Block Type) */
170
+
171
+ /* pad out with non-zero random data */
172
+ j = tlen - 3 - flen;
173
+
174
+ if (!RAND_bytes(p, j)) {
175
+ return 0;
176
+ }
177
+
178
+ for (i = 0; i < j; i++) {
179
+ while (*p == 0) {
180
+ if (!RAND_bytes(p, 1)) {
181
+ return 0;
182
+ }
183
+ }
184
+ p++;
185
+ }
186
+
187
+ *(p++) = 0;
188
+
189
+ memcpy(p, from, (unsigned int)flen);
190
+ return 1;
191
+ }
192
+
193
+ /* constant_time_byte_eq returns 1 if |x| == |y| and 0 otherwise. */
194
+ static int constant_time_byte_eq(unsigned char a, unsigned char b) {
195
+ unsigned char z = ~(a ^ b);
196
+ z &= z >> 4;
197
+ z &= z >> 2;
198
+ z &= z >> 1;
199
+
200
+ return z;
201
+ }
202
+
203
+ /* constant_time_select returns |x| if |v| is 1 and |y| if |v| is 0.
204
+ * Its behavior is undefined if |v| takes any other value. */
205
+ static int constant_time_select(int v, int x, int y) {
206
+ return ((~(v - 1)) & x) | ((v - 1) & y);
207
+ }
208
+
209
+ /* constant_time_le returns 1 if |x| <= |y| and 0 otherwise.
210
+ * |x| and |y| must be positive. */
211
+ static int constant_time_le(int x, int y) {
212
+ return ((x - y - 1) >> (sizeof(int) * 8 - 1)) & 1;
213
+ }
214
+
215
+ int RSA_message_index_PKCS1_type_2(const uint8_t *from, size_t from_len,
216
+ size_t *out_index) {
217
+ size_t i;
218
+ int first_byte_is_zero, second_byte_is_two, looking_for_index;
219
+ int valid_index, zero_index = 0;
220
+
221
+ /* PKCS#1 v1.5 decryption. See "PKCS #1 v2.2: RSA Cryptography
222
+ * Standard", section 7.2.2. */
223
+ if (from_len < RSA_PKCS1_PADDING_SIZE) {
224
+ /* |from| is zero-padded to the size of the RSA modulus, a public value, so
225
+ * this can be rejected in non-constant time. */
226
+ *out_index = 0;
227
+ return 0;
228
+ }
229
+
230
+ first_byte_is_zero = constant_time_byte_eq(from[0], 0);
231
+ second_byte_is_two = constant_time_byte_eq(from[1], 2);
232
+
233
+ looking_for_index = 1;
234
+ for (i = 2; i < from_len; i++) {
235
+ int equals0 = constant_time_byte_eq(from[i], 0);
236
+ zero_index =
237
+ constant_time_select(looking_for_index & equals0, i, zero_index);
238
+ looking_for_index = constant_time_select(equals0, 0, looking_for_index);
239
+ }
240
+
241
+ /* The input must begin with 00 02. */
242
+ valid_index = first_byte_is_zero;
243
+ valid_index &= second_byte_is_two;
244
+
245
+ /* We must have found the end of PS. */
246
+ valid_index &= ~looking_for_index;
247
+
248
+ /* PS must be at least 8 bytes long, and it starts two bytes into |from|. */
249
+ valid_index &= constant_time_le(2 + 8, zero_index);
250
+
251
+ /* Skip the zero byte. */
252
+ zero_index++;
253
+
254
+ *out_index = constant_time_select(valid_index, zero_index, 0);
255
+ return valid_index;
256
+ }
257
+
258
+ int RSA_padding_check_PKCS1_type_2(uint8_t *to, unsigned tlen,
259
+ const uint8_t *from, unsigned flen) {
260
+ size_t msg_index, msg_len;
261
+
262
+ if (flen == 0) {
263
+ OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
264
+ return -1;
265
+ }
266
+
267
+ /* NOTE: Although |RSA_message_index_PKCS1_type_2| itself is constant time,
268
+ * the API contracts of this function and |RSA_decrypt| with
269
+ * |RSA_PKCS1_PADDING| make it impossible to completely avoid Bleichenbacher's
270
+ * attack. */
271
+ if (!RSA_message_index_PKCS1_type_2(from, flen, &msg_index)) {
272
+ OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR);
273
+ return -1;
274
+ }
275
+
276
+ msg_len = flen - msg_index;
277
+ if (msg_len > tlen) {
278
+ /* This shouldn't happen because this function is always called with |tlen|
279
+ * the key size and |flen| is bounded by the key size. */
280
+ OPENSSL_PUT_ERROR(RSA, RSA_R_PKCS_DECODING_ERROR);
281
+ return -1;
282
+ }
283
+ memcpy(to, &from[msg_index], msg_len);
284
+ return msg_len;
285
+ }
286
+
287
+ int RSA_padding_add_none(uint8_t *to, unsigned tlen, const uint8_t *from, unsigned flen) {
288
+ if (flen > tlen) {
289
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
290
+ return 0;
291
+ }
292
+
293
+ if (flen < tlen) {
294
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE);
295
+ return 0;
296
+ }
297
+
298
+ memcpy(to, from, (unsigned int)flen);
299
+ return 1;
300
+ }
@@ -0,0 +1,450 @@
1
+ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
+ * All rights reserved.
3
+ *
4
+ * This package is an SSL implementation written
5
+ * by Eric Young (eay@cryptsoft.com).
6
+ * The implementation was written so as to conform with Netscapes SSL.
7
+ *
8
+ * This library is free for commercial and non-commercial use as long as
9
+ * the following conditions are aheared to. The following conditions
10
+ * apply to all code found in this distribution, be it the RC4, RSA,
11
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
+ * included with this distribution is covered by the same copyright terms
13
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
+ *
15
+ * Copyright remains Eric Young's, and as such any Copyright notices in
16
+ * the code are not to be removed.
17
+ * If this package is used in a product, Eric Young should be given attribution
18
+ * as the author of the parts of the library used.
19
+ * This can be in the form of a textual message at program startup or
20
+ * in documentation (online or textual) provided with the package.
21
+ *
22
+ * Redistribution and use in source and binary forms, with or without
23
+ * modification, are permitted provided that the following conditions
24
+ * are met:
25
+ * 1. Redistributions of source code must retain the copyright
26
+ * notice, this list of conditions and the following disclaimer.
27
+ * 2. Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ * 3. All advertising materials mentioning features or use of this software
31
+ * must display the following acknowledgement:
32
+ * "This product includes cryptographic software written by
33
+ * Eric Young (eay@cryptsoft.com)"
34
+ * The word 'cryptographic' can be left out if the rouines from the library
35
+ * being used are not cryptographic related :-).
36
+ * 4. If you include any Windows specific code (or a derivative thereof) from
37
+ * the apps directory (application code) you must include an acknowledgement:
38
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ *
52
+ * The licence and distribution terms for any publically available version or
53
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
54
+ * copied and put under another distribution licence
55
+ * [including the GNU Public Licence.] */
56
+
57
+ #include <openssl/rsa.h>
58
+
59
+ #include <limits.h>
60
+ #include <string.h>
61
+
62
+ #include <openssl/bn.h>
63
+ #include <openssl/err.h>
64
+ #include <openssl/mem.h>
65
+ #include <openssl/obj_mac.h>
66
+ #include <openssl/thread.h>
67
+
68
+ #include "internal.h"
69
+ #include "../internal.h"
70
+
71
+
72
+ int RSA_verify_pkcs1_signed_digest(size_t min_bits, size_t max_bits,
73
+ int hash_nid, const uint8_t *digest,
74
+ size_t digest_len, const uint8_t *sig,
75
+ size_t sig_len, const uint8_t *rsa_key,
76
+ const size_t rsa_key_len) {
77
+ /* TODO(perf): Avoid all the overhead of allocating an |RSA| on the heap and
78
+ * dealing with the mutex and whatnot. */
79
+
80
+ RSA *key = RSA_public_key_from_bytes(rsa_key, rsa_key_len);
81
+ if (key == NULL) {
82
+ return 0;
83
+ }
84
+
85
+ int ret = 0;
86
+
87
+ size_t len = RSA_size(key);
88
+ if (len > SIZE_MAX / 8 || len * 8 < min_bits || len * 8 > max_bits) {
89
+ OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL); /* XXX: Or too big. */
90
+ goto err;
91
+ }
92
+
93
+ /* Don't cache the intermediate values since we're not reusing the key. */
94
+ key->flags &= ~RSA_FLAG_CACHE_PUBLIC;
95
+
96
+ ret = RSA_verify(hash_nid, digest, digest_len, sig, sig_len, key);
97
+
98
+ err:
99
+ RSA_free(key);
100
+ return ret;
101
+ }
102
+
103
+ RSA *RSA_new(void) { return RSA_new_method(NULL); }
104
+
105
+ RSA *RSA_new_method(const ENGINE *engine) {
106
+ if (engine != NULL) {
107
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE); /* TODO: error code */
108
+ return NULL;
109
+ }
110
+
111
+ RSA *rsa = (RSA *)OPENSSL_malloc(sizeof(RSA));
112
+ if (rsa == NULL) {
113
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
114
+ return NULL;
115
+ }
116
+
117
+ memset(rsa, 0, sizeof(RSA));
118
+
119
+ rsa->references = 1;
120
+ rsa->flags = RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
121
+ CRYPTO_MUTEX_init(&rsa->lock);
122
+
123
+ return rsa;
124
+ }
125
+
126
+ void RSA_free(RSA *rsa) {
127
+ unsigned u;
128
+
129
+ if (rsa == NULL) {
130
+ return;
131
+ }
132
+
133
+ if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
134
+ return;
135
+ }
136
+
137
+ BN_clear_free(rsa->n);
138
+ BN_clear_free(rsa->e);
139
+ BN_clear_free(rsa->d);
140
+ BN_clear_free(rsa->p);
141
+ BN_clear_free(rsa->q);
142
+ BN_clear_free(rsa->dmp1);
143
+ BN_clear_free(rsa->dmq1);
144
+ BN_clear_free(rsa->iqmp);
145
+ BN_MONT_CTX_free(rsa->mont_n);
146
+ BN_MONT_CTX_free(rsa->mont_p);
147
+ BN_MONT_CTX_free(rsa->mont_q);
148
+ for (u = 0; u < rsa->num_blindings; u++) {
149
+ BN_BLINDING_free(rsa->blindings[u]);
150
+ }
151
+ OPENSSL_free(rsa->blindings);
152
+ OPENSSL_free(rsa->blindings_inuse);
153
+ CRYPTO_MUTEX_cleanup(&rsa->lock);
154
+ OPENSSL_free(rsa);
155
+ }
156
+
157
+ int RSA_up_ref(RSA *rsa) {
158
+ CRYPTO_refcount_inc(&rsa->references);
159
+ return 1;
160
+ }
161
+
162
+ /* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
163
+ * the length of an MD5 and SHA1 hash. */
164
+ static const unsigned SSL_SIG_LENGTH = 36;
165
+
166
+ /* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
167
+ * to be signed with PKCS#1. */
168
+ struct pkcs1_sig_prefix {
169
+ /* nid identifies the hash function. */
170
+ int nid;
171
+ /* len is the number of bytes of |bytes| which are valid. */
172
+ uint8_t len;
173
+ /* bytes contains the DER bytes. */
174
+ uint8_t bytes[19];
175
+ };
176
+
177
+ /* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
178
+ * different hash functions. */
179
+ static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
180
+ {
181
+ NID_sha1,
182
+ 15,
183
+ {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
184
+ 0x00, 0x04, 0x14},
185
+ },
186
+ {
187
+ NID_sha256,
188
+ 19,
189
+ {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
190
+ 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
191
+ },
192
+ {
193
+ NID_sha384,
194
+ 19,
195
+ {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
196
+ 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
197
+ },
198
+ {
199
+ NID_sha512,
200
+ 19,
201
+ {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
202
+ 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
203
+ },
204
+ {
205
+ NID_undef, 0, {0},
206
+ },
207
+ };
208
+
209
+ int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
210
+ int *is_alloced, int hash_nid, const uint8_t *msg,
211
+ size_t msg_len) {
212
+ unsigned i;
213
+
214
+ if (hash_nid == NID_md5_sha1) {
215
+ /* Special case: SSL signature, just check the length. */
216
+ if (msg_len != SSL_SIG_LENGTH) {
217
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
218
+ return 0;
219
+ }
220
+
221
+ *out_msg = (uint8_t*) msg;
222
+ *out_msg_len = SSL_SIG_LENGTH;
223
+ *is_alloced = 0;
224
+ return 1;
225
+ }
226
+
227
+ for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
228
+ const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
229
+ if (sig_prefix->nid != hash_nid) {
230
+ continue;
231
+ }
232
+
233
+ const uint8_t* prefix = sig_prefix->bytes;
234
+ unsigned prefix_len = sig_prefix->len;
235
+ unsigned signed_msg_len;
236
+ uint8_t *signed_msg;
237
+
238
+ signed_msg_len = prefix_len + msg_len;
239
+ if (signed_msg_len < prefix_len) {
240
+ OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
241
+ return 0;
242
+ }
243
+
244
+ signed_msg = OPENSSL_malloc(signed_msg_len);
245
+ if (!signed_msg) {
246
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
247
+ return 0;
248
+ }
249
+
250
+ memcpy(signed_msg, prefix, prefix_len);
251
+ memcpy(signed_msg + prefix_len, msg, msg_len);
252
+
253
+ *out_msg = signed_msg;
254
+ *out_msg_len = signed_msg_len;
255
+ *is_alloced = 1;
256
+
257
+ return 1;
258
+ }
259
+
260
+ OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
261
+ return 0;
262
+ }
263
+
264
+ int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
265
+ unsigned *out_len, RSA *rsa) {
266
+ const unsigned rsa_size = RSA_size(rsa);
267
+ int ret = 0;
268
+ uint8_t *signed_msg;
269
+ size_t signed_msg_len;
270
+ int signed_msg_is_alloced = 0;
271
+ size_t size_t_out_len;
272
+
273
+ if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
274
+ &signed_msg_is_alloced, hash_nid, in, in_len)) {
275
+ return 0;
276
+ }
277
+
278
+ if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
279
+ signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
280
+ OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
281
+ goto finish;
282
+ }
283
+
284
+ if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
285
+ signed_msg_len, RSA_PKCS1_PADDING)) {
286
+ *out_len = size_t_out_len;
287
+ ret = 1;
288
+ }
289
+
290
+ finish:
291
+ if (signed_msg_is_alloced) {
292
+ OPENSSL_free(signed_msg);
293
+ }
294
+ return ret;
295
+ }
296
+
297
+ int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
298
+ const uint8_t *sig, size_t sig_len, RSA *rsa) {
299
+ const size_t rsa_size = RSA_size(rsa);
300
+ uint8_t *buf = NULL;
301
+ int ret = 0;
302
+ uint8_t *signed_msg = NULL;
303
+ size_t signed_msg_len, len;
304
+ int signed_msg_is_alloced = 0;
305
+
306
+ if (sig_len != rsa_size) {
307
+ OPENSSL_PUT_ERROR(RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
308
+ return 0;
309
+ }
310
+
311
+ if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
312
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
313
+ return 0;
314
+ }
315
+
316
+ buf = OPENSSL_malloc(rsa_size);
317
+ if (!buf) {
318
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
319
+ return 0;
320
+ }
321
+
322
+ if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
323
+ RSA_PKCS1_PADDING)) {
324
+ goto out;
325
+ }
326
+
327
+ if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
328
+ &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
329
+ goto out;
330
+ }
331
+
332
+ if (len != signed_msg_len || CRYPTO_memcmp(buf, signed_msg, len) != 0) {
333
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
334
+ goto out;
335
+ }
336
+
337
+ ret = 1;
338
+
339
+ out:
340
+ OPENSSL_free(buf);
341
+ if (signed_msg_is_alloced) {
342
+ OPENSSL_free(signed_msg);
343
+ }
344
+ return ret;
345
+ }
346
+
347
+ int RSA_check_key(const RSA *key) {
348
+ BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp;
349
+ BN_CTX *ctx;
350
+ int ok = 0, has_crt_values;
351
+
352
+ if ((key->p != NULL) != (key->q != NULL)) {
353
+ OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
354
+ return 0;
355
+ }
356
+
357
+ if (!key->n || !key->e) {
358
+ OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
359
+ return 0;
360
+ }
361
+
362
+ if (!key->d || !key->p) {
363
+ /* For a public key, or without p and q, there's nothing that can be
364
+ * checked. */
365
+ return 1;
366
+ }
367
+
368
+ ctx = BN_CTX_new();
369
+ if (ctx == NULL) {
370
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
371
+ return 0;
372
+ }
373
+
374
+ BN_init(&n);
375
+ BN_init(&pm1);
376
+ BN_init(&qm1);
377
+ BN_init(&lcm);
378
+ BN_init(&gcd);
379
+ BN_init(&de);
380
+ BN_init(&dmp1);
381
+ BN_init(&dmq1);
382
+ BN_init(&iqmp);
383
+
384
+ if (/* n = pq */
385
+ !BN_mul(&n, key->p, key->q, ctx) ||
386
+ /* lcm = lcm(p-1, q-1) */
387
+ !BN_sub(&pm1, key->p, BN_value_one()) ||
388
+ !BN_sub(&qm1, key->q, BN_value_one()) ||
389
+ !BN_mul(&lcm, &pm1, &qm1, ctx) ||
390
+ !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
391
+ !BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
392
+ /* de = d*e mod lcm(p-1, q-1) */
393
+ !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
394
+ OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
395
+ goto out;
396
+ }
397
+
398
+ if (BN_cmp(&n, key->n) != 0) {
399
+ OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
400
+ goto out;
401
+ }
402
+
403
+ if (!BN_is_one(&de)) {
404
+ OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
405
+ goto out;
406
+ }
407
+
408
+ has_crt_values = key->dmp1 != NULL;
409
+ if (has_crt_values != (key->dmq1 != NULL) ||
410
+ has_crt_values != (key->iqmp != NULL)) {
411
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
412
+ goto out;
413
+ }
414
+
415
+ if (has_crt_values) {
416
+ if (/* dmp1 = d mod (p-1) */
417
+ !BN_mod(&dmp1, key->d, &pm1, ctx) ||
418
+ /* dmq1 = d mod (q-1) */
419
+ !BN_mod(&dmq1, key->d, &qm1, ctx) ||
420
+ /* iqmp = q^-1 mod p */
421
+ !BN_mod_inverse(&iqmp, key->q, key->p, ctx)) {
422
+ OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
423
+ goto out;
424
+ }
425
+
426
+ if (BN_cmp(&dmp1, key->dmp1) != 0 ||
427
+ BN_cmp(&dmq1, key->dmq1) != 0 ||
428
+ BN_cmp(&iqmp, key->iqmp) != 0) {
429
+ OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
430
+ goto out;
431
+ }
432
+ }
433
+
434
+ ok = 1;
435
+
436
+ out:
437
+ BN_free(&n);
438
+ BN_free(&pm1);
439
+ BN_free(&qm1);
440
+ BN_free(&lcm);
441
+ BN_free(&gcd);
442
+ BN_free(&de);
443
+ BN_free(&dmp1);
444
+ BN_free(&dmq1);
445
+ BN_free(&iqmp);
446
+ BN_CTX_free(ctx);
447
+
448
+ return ok;
449
+ }
450
+