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,355 @@
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/base.h"
58
+
59
+
60
+ /* The 32-bit hash algorithms' |*_block_data_order| functions operate on
61
+ * unaligned data. |sha512_block_data_order| does not. */
62
+
63
+ #include "../internal.h"
64
+
65
+
66
+ #if !defined(OPENSSL_NO_ASM) && \
67
+ (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
68
+ defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
69
+ #define SHA512_ASM
70
+ #endif
71
+
72
+ #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
73
+ defined(__ARM_FEATURE_UNALIGNED)
74
+ #define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
75
+ #else
76
+ #error "ring::digest does pre-align data for sha512_block_data_order"
77
+ #endif
78
+
79
+ #ifndef SHA512_ASM
80
+ static const uint64_t K512[80] = {
81
+ 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
82
+ 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
83
+ 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
84
+ 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
85
+ 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
86
+ 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
87
+ 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
88
+ 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
89
+ 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
90
+ 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
91
+ 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
92
+ 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
93
+ 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
94
+ 0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
95
+ 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
96
+ 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
97
+ 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
98
+ 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
99
+ 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
100
+ 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
101
+ 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
102
+ 0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
103
+ 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
104
+ 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
105
+ 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
106
+ 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
107
+ 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
108
+
109
+ #if defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM)
110
+ #if defined(__x86_64) || defined(__x86_64__)
111
+ #define ROTR(a, n) \
112
+ ({ \
113
+ uint64_t ret; \
114
+ asm("rorq %1,%0" : "=r"(ret) : "J"(n), "0"(a) : "cc"); \
115
+ ret; \
116
+ })
117
+ #define PULL64(x) \
118
+ ({ \
119
+ uint64_t ret = *((const uint64_t *)(&(x))); \
120
+ asm("bswapq %0" : "=r"(ret) : "0"(ret)); \
121
+ ret; \
122
+ })
123
+ #elif(defined(__i386) || defined(__i386__))
124
+ #define PULL64(x) \
125
+ ({ \
126
+ const unsigned int *p = (const unsigned int *)(&(x)); \
127
+ unsigned int hi = p[0], lo = p[1]; \
128
+ asm("bswapl %0; bswapl %1;" : "=r"(lo), "=r"(hi) : "0"(lo), "1"(hi)); \
129
+ ((uint64_t)hi) << 32 | lo; \
130
+ })
131
+ #elif(defined(_ARCH_PPC) && defined(__64BIT__)) || defined(_ARCH_PPC64)
132
+ #define ROTR(a, n) \
133
+ ({ \
134
+ uint64_t ret; \
135
+ asm("rotrdi %0,%1,%2" : "=r"(ret) : "r"(a), "K"(n)); \
136
+ ret; \
137
+ })
138
+ #elif defined(__aarch64__)
139
+ #define ROTR(a, n) \
140
+ ({ \
141
+ uint64_t ret; \
142
+ asm("ror %0,%1,%2" : "=r"(ret) : "r"(a), "I"(n)); \
143
+ ret; \
144
+ })
145
+ #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
146
+ __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
147
+ #define PULL64(x) \
148
+ ({ \
149
+ uint64_t ret; \
150
+ asm("rev %0,%1" : "=r"(ret) : "r"(*((const uint64_t *)(&(x))))); \
151
+ ret; \
152
+ })
153
+ #endif
154
+ #endif
155
+ #elif defined(_MSC_VER)
156
+ #if defined(_WIN64) /* applies to both IA-64 and AMD64 */
157
+ #pragma intrinsic(_rotr64)
158
+ #define ROTR(a, n) _rotr64((a), n)
159
+ #endif
160
+ #if defined(_M_IX86) && !defined(OPENSSL_NO_ASM)
161
+ static uint64_t __fastcall __pull64be(const void *x) {
162
+ _asm mov edx, [ecx + 0]
163
+ _asm mov eax, [ecx + 4]
164
+ _asm bswap edx
165
+ _asm bswap eax
166
+ }
167
+ #define PULL64(x) __pull64be(&(x))
168
+ #if _MSC_VER <= 1200
169
+ #pragma inline_depth(0)
170
+ #endif
171
+ #endif
172
+ #endif
173
+
174
+ #ifndef PULL64
175
+ #define B(x, j) \
176
+ (((uint64_t)(*(((const uint8_t *)(&x)) + j))) << ((7 - j) * 8))
177
+ #define PULL64(x) \
178
+ (B(x, 0) | B(x, 1) | B(x, 2) | B(x, 3) | B(x, 4) | B(x, 5) | B(x, 6) | \
179
+ B(x, 7))
180
+ #endif
181
+
182
+ #ifndef ROTR
183
+ #define ROTR(x, s) (((x) >> s) | (x) << (64 - s))
184
+ #endif
185
+
186
+ #define Sigma0(x) (ROTR((x), 28) ^ ROTR((x), 34) ^ ROTR((x), 39))
187
+ #define Sigma1(x) (ROTR((x), 14) ^ ROTR((x), 18) ^ ROTR((x), 41))
188
+ #define sigma0(x) (ROTR((x), 1) ^ ROTR((x), 8) ^ ((x) >> 7))
189
+ #define sigma1(x) (ROTR((x), 19) ^ ROTR((x), 61) ^ ((x) >> 6))
190
+
191
+ #define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & (z)))
192
+ #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
193
+
194
+
195
+ #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
196
+ /*
197
+ * This code should give better results on 32-bit CPU with less than
198
+ * ~24 registers, both size and performance wise...
199
+ */
200
+ void sha512_block_data_order(uint64_t *state, const uint64_t *W, size_t num) {
201
+ uint64_t A, E, T;
202
+ uint64_t X[9 + 80], *F;
203
+ int i;
204
+
205
+ while (num--) {
206
+ F = X + 80;
207
+ A = state[0];
208
+ F[1] = state[1];
209
+ F[2] = state[2];
210
+ F[3] = state[3];
211
+ E = state[4];
212
+ F[5] = state[5];
213
+ F[6] = state[6];
214
+ F[7] = state[7];
215
+
216
+ for (i = 0; i < 16; i++, F--) {
217
+ T = PULL64(W[i]);
218
+ F[0] = A;
219
+ F[4] = E;
220
+ F[8] = T;
221
+ T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i];
222
+ E = F[3] + T;
223
+ A = T + Sigma0(A) + Maj(A, F[1], F[2]);
224
+ }
225
+
226
+ for (; i < 80; i++, F--) {
227
+ T = sigma0(F[8 + 16 - 1]);
228
+ T += sigma1(F[8 + 16 - 14]);
229
+ T += F[8 + 16] + F[8 + 16 - 9];
230
+
231
+ F[0] = A;
232
+ F[4] = E;
233
+ F[8] = T;
234
+ T += F[7] + Sigma1(E) + Ch(E, F[5], F[6]) + K512[i];
235
+ E = F[3] + T;
236
+ A = T + Sigma0(A) + Maj(A, F[1], F[2]);
237
+ }
238
+
239
+ state[0] += A;
240
+ state[1] += F[1];
241
+ state[2] += F[2];
242
+ state[3] += F[3];
243
+ state[4] += E;
244
+ state[5] += F[5];
245
+ state[6] += F[6];
246
+ state[7] += F[7];
247
+
248
+ W += 16;
249
+ }
250
+ }
251
+
252
+ #else
253
+
254
+ #define ROUND_00_15(i, a, b, c, d, e, f, g, h) \
255
+ do { \
256
+ T1 += h + Sigma1(e) + Ch(e, f, g) + K512[i]; \
257
+ h = Sigma0(a) + Maj(a, b, c); \
258
+ d += T1; \
259
+ h += T1; \
260
+ } while (0)
261
+
262
+ #define ROUND_16_80(i, j, a, b, c, d, e, f, g, h, X) \
263
+ do { \
264
+ s0 = X[(j + 1) & 0x0f]; \
265
+ s0 = sigma0(s0); \
266
+ s1 = X[(j + 14) & 0x0f]; \
267
+ s1 = sigma1(s1); \
268
+ T1 = X[(j) & 0x0f] += s0 + s1 + X[(j + 9) & 0x0f]; \
269
+ ROUND_00_15(i + j, a, b, c, d, e, f, g, h); \
270
+ } while (0)
271
+
272
+ void sha512_block_data_order(uint64_t *state, const uint64_t *W, size_t num) {
273
+ uint64_t a, b, c, d, e, f, g, h, s0, s1, T1;
274
+ uint64_t X[16];
275
+ int i;
276
+
277
+ while (num--) {
278
+
279
+ a = state[0];
280
+ b = state[1];
281
+ c = state[2];
282
+ d = state[3];
283
+ e = state[4];
284
+ f = state[5];
285
+ g = state[6];
286
+ h = state[7];
287
+
288
+ T1 = X[0] = PULL64(W[0]);
289
+ ROUND_00_15(0, a, b, c, d, e, f, g, h);
290
+ T1 = X[1] = PULL64(W[1]);
291
+ ROUND_00_15(1, h, a, b, c, d, e, f, g);
292
+ T1 = X[2] = PULL64(W[2]);
293
+ ROUND_00_15(2, g, h, a, b, c, d, e, f);
294
+ T1 = X[3] = PULL64(W[3]);
295
+ ROUND_00_15(3, f, g, h, a, b, c, d, e);
296
+ T1 = X[4] = PULL64(W[4]);
297
+ ROUND_00_15(4, e, f, g, h, a, b, c, d);
298
+ T1 = X[5] = PULL64(W[5]);
299
+ ROUND_00_15(5, d, e, f, g, h, a, b, c);
300
+ T1 = X[6] = PULL64(W[6]);
301
+ ROUND_00_15(6, c, d, e, f, g, h, a, b);
302
+ T1 = X[7] = PULL64(W[7]);
303
+ ROUND_00_15(7, b, c, d, e, f, g, h, a);
304
+ T1 = X[8] = PULL64(W[8]);
305
+ ROUND_00_15(8, a, b, c, d, e, f, g, h);
306
+ T1 = X[9] = PULL64(W[9]);
307
+ ROUND_00_15(9, h, a, b, c, d, e, f, g);
308
+ T1 = X[10] = PULL64(W[10]);
309
+ ROUND_00_15(10, g, h, a, b, c, d, e, f);
310
+ T1 = X[11] = PULL64(W[11]);
311
+ ROUND_00_15(11, f, g, h, a, b, c, d, e);
312
+ T1 = X[12] = PULL64(W[12]);
313
+ ROUND_00_15(12, e, f, g, h, a, b, c, d);
314
+ T1 = X[13] = PULL64(W[13]);
315
+ ROUND_00_15(13, d, e, f, g, h, a, b, c);
316
+ T1 = X[14] = PULL64(W[14]);
317
+ ROUND_00_15(14, c, d, e, f, g, h, a, b);
318
+ T1 = X[15] = PULL64(W[15]);
319
+ ROUND_00_15(15, b, c, d, e, f, g, h, a);
320
+
321
+ for (i = 16; i < 80; i += 16) {
322
+ ROUND_16_80(i, 0, a, b, c, d, e, f, g, h, X);
323
+ ROUND_16_80(i, 1, h, a, b, c, d, e, f, g, X);
324
+ ROUND_16_80(i, 2, g, h, a, b, c, d, e, f, X);
325
+ ROUND_16_80(i, 3, f, g, h, a, b, c, d, e, X);
326
+ ROUND_16_80(i, 4, e, f, g, h, a, b, c, d, X);
327
+ ROUND_16_80(i, 5, d, e, f, g, h, a, b, c, X);
328
+ ROUND_16_80(i, 6, c, d, e, f, g, h, a, b, X);
329
+ ROUND_16_80(i, 7, b, c, d, e, f, g, h, a, X);
330
+ ROUND_16_80(i, 8, a, b, c, d, e, f, g, h, X);
331
+ ROUND_16_80(i, 9, h, a, b, c, d, e, f, g, X);
332
+ ROUND_16_80(i, 10, g, h, a, b, c, d, e, f, X);
333
+ ROUND_16_80(i, 11, f, g, h, a, b, c, d, e, X);
334
+ ROUND_16_80(i, 12, e, f, g, h, a, b, c, d, X);
335
+ ROUND_16_80(i, 13, d, e, f, g, h, a, b, c, X);
336
+ ROUND_16_80(i, 14, c, d, e, f, g, h, a, b, X);
337
+ ROUND_16_80(i, 15, b, c, d, e, f, g, h, a, X);
338
+ }
339
+
340
+ state[0] += a;
341
+ state[1] += b;
342
+ state[2] += c;
343
+ state[3] += d;
344
+ state[4] += e;
345
+ state[5] += f;
346
+ state[6] += g;
347
+ state[7] += h;
348
+
349
+ W += 16;
350
+ }
351
+ }
352
+
353
+ #endif
354
+
355
+ #endif /* SHA512_ASM */
@@ -0,0 +1,326 @@
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
+ #include "file_test.h"
16
+
17
+ #include <ctype.h>
18
+ #include <errno.h>
19
+ #include <stdarg.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+
23
+ #include <openssl/err.h>
24
+
25
+
26
+ FileTest::FileTest(const char *path) {
27
+ file_ = fopen(path, "r");
28
+ if (file_ == nullptr) {
29
+ fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
30
+ }
31
+ }
32
+
33
+ FileTest::~FileTest() {
34
+ if (file_ != nullptr) {
35
+ fclose(file_);
36
+ }
37
+ }
38
+
39
+ // FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
40
+ // if there is none.
41
+ static const char *FindDelimiter(const char *str) {
42
+ while (*str) {
43
+ if (*str == ':' || *str == '=') {
44
+ return str;
45
+ }
46
+ str++;
47
+ }
48
+ return nullptr;
49
+ }
50
+
51
+ // StripSpace returns a string containing up to |len| characters from |str| with
52
+ // leading and trailing whitespace removed.
53
+ static std::string StripSpace(const char *str, size_t len) {
54
+ // Remove leading space.
55
+ while (len > 0 && isspace(*str)) {
56
+ str++;
57
+ len--;
58
+ }
59
+ while (len > 0 && isspace(str[len-1])) {
60
+ len--;
61
+ }
62
+ return std::string(str, len);
63
+ }
64
+
65
+ FileTest::ReadResult FileTest::ReadNext() {
66
+ // If the previous test had unused attributes or block, it is an error.
67
+ if (!unused_attributes_.empty()) {
68
+ for (const std::string &key : unused_attributes_) {
69
+ PrintLine("Unused attribute: %s", key.c_str());
70
+ }
71
+ return kReadError;
72
+ }
73
+ if (!block_.empty() && !used_block_) {
74
+ PrintLine("Unused block");
75
+ return kReadError;
76
+ }
77
+
78
+ ClearTest();
79
+
80
+ bool in_block = false;
81
+ while (true) {
82
+ // Read the next line.
83
+ char buf[4096];
84
+ if (fgets(buf, sizeof(buf), file_) == nullptr) {
85
+ if (feof(file_)) {
86
+ if (in_block) {
87
+ fprintf(stderr, "Unterminated block.\n");
88
+ return kReadError;
89
+ }
90
+ // EOF is a valid terminator for a test.
91
+ return start_line_ > 0 ? kReadSuccess : kReadEOF;
92
+ }
93
+ fprintf(stderr, "Error reading from input.\n");
94
+ return kReadError;
95
+ }
96
+
97
+ line_++;
98
+ size_t len = strlen(buf);
99
+ // Check for truncation.
100
+ if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
101
+ fprintf(stderr, "Line %u too long.\n", line_);
102
+ return kReadError;
103
+ }
104
+
105
+ bool is_delimiter = strncmp(buf, "---", 3) == 0;
106
+ if (in_block) {
107
+ block_ += buf;
108
+ if (is_delimiter) {
109
+ // Ending the block completes the test.
110
+ return kReadSuccess;
111
+ }
112
+ } else if (is_delimiter) {
113
+ if (start_line_ == 0) {
114
+ fprintf(stderr, "Line %u: Unexpected block.\n", line_);
115
+ return kReadError;
116
+ }
117
+ in_block = true;
118
+ block_ += buf;
119
+ } else if (buf[0] == '\n' || buf[0] == '\0') {
120
+ // Empty lines delimit tests.
121
+ if (start_line_ > 0) {
122
+ return kReadSuccess;
123
+ }
124
+ } else if (buf[0] != '#') { // Comment lines are ignored.
125
+ // Parse the line as an attribute.
126
+ const char *delimiter = FindDelimiter(buf);
127
+ if (delimiter == nullptr) {
128
+ fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
129
+ return kReadError;
130
+ }
131
+ std::string key = StripSpace(buf, delimiter - buf);
132
+ std::string value = StripSpace(delimiter + 1,
133
+ buf + len - delimiter - 1);
134
+
135
+ unused_attributes_.insert(key);
136
+ attributes_[key] = value;
137
+ if (start_line_ == 0) {
138
+ // This is the start of a test.
139
+ type_ = key;
140
+ parameter_ = value;
141
+ start_line_ = line_;
142
+ }
143
+ }
144
+ }
145
+ }
146
+
147
+ void FileTest::PrintLine(const char *format, ...) {
148
+ va_list args;
149
+ va_start(args, format);
150
+
151
+ fprintf(stderr, "Line %u: ", start_line_);
152
+ vfprintf(stderr, format, args);
153
+ fprintf(stderr, "\n");
154
+
155
+ va_end(args);
156
+ }
157
+
158
+ const std::string &FileTest::GetType() {
159
+ OnKeyUsed(type_);
160
+ return type_;
161
+ }
162
+
163
+ const std::string &FileTest::GetParameter() {
164
+ OnKeyUsed(type_);
165
+ return parameter_;
166
+ }
167
+
168
+ const std::string &FileTest::GetBlock() {
169
+ used_block_ = true;
170
+ return block_;
171
+ }
172
+
173
+ bool FileTest::HasAttribute(const std::string &key) {
174
+ OnKeyUsed(key);
175
+ return attributes_.count(key) > 0;
176
+ }
177
+
178
+ bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
179
+ OnKeyUsed(key);
180
+ auto iter = attributes_.find(key);
181
+ if (iter == attributes_.end()) {
182
+ PrintLine("Missing attribute '%s'.", key.c_str());
183
+ return false;
184
+ }
185
+ *out_value = iter->second;
186
+ return true;
187
+ }
188
+
189
+ const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
190
+ if (!HasAttribute(key)) {
191
+ abort();
192
+ }
193
+ return attributes_[key];
194
+ }
195
+
196
+ static bool FromHexDigit(uint8_t *out, char c) {
197
+ if ('0' <= c && c <= '9') {
198
+ *out = c - '0';
199
+ return true;
200
+ }
201
+ if ('a' <= c && c <= 'f') {
202
+ *out = c - 'a' + 10;
203
+ return true;
204
+ }
205
+ if ('A' <= c && c <= 'F') {
206
+ *out = c - 'A' + 10;
207
+ return true;
208
+ }
209
+ return false;
210
+ }
211
+
212
+ bool FileTest::GetBytesOrDefault(std::vector<uint8_t> *out, bool *is_default,
213
+ const std::string &key) {
214
+ std::string value;
215
+ if (!GetAttribute(&value, key)) {
216
+ return false;
217
+ }
218
+
219
+ *is_default = value == "DEFAULT";
220
+ if (*is_default) {
221
+ return true;
222
+ }
223
+
224
+ if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
225
+ out->assign(value.begin() + 1, value.end() - 1);
226
+ return true;
227
+ }
228
+
229
+ if (value.size() % 2 != 0) {
230
+ PrintLine("Error decoding value: %s", value.c_str());
231
+ return false;
232
+ }
233
+ out->reserve(value.size() / 2);
234
+ for (size_t i = 0; i < value.size(); i += 2) {
235
+ uint8_t hi, lo;
236
+ if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
237
+ PrintLine("Error decoding value: %s", value.c_str());
238
+ return false;
239
+ }
240
+ out->push_back((hi << 4) | lo);
241
+ }
242
+ return true;
243
+ }
244
+
245
+ bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
246
+ bool is_default;
247
+ if (!GetBytesOrDefault(out, &is_default, key)) {
248
+ return false;
249
+ }
250
+ if (is_default) {
251
+ return false;
252
+ }
253
+ return true;
254
+ }
255
+
256
+ static std::string EncodeHex(const uint8_t *in, size_t in_len) {
257
+ static const char kHexDigits[] = "0123456789abcdef";
258
+ std::string ret;
259
+ ret.reserve(in_len * 2);
260
+ for (size_t i = 0; i < in_len; i++) {
261
+ ret += kHexDigits[in[i] >> 4];
262
+ ret += kHexDigits[in[i] & 0xf];
263
+ }
264
+ return ret;
265
+ }
266
+
267
+ bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
268
+ const uint8_t *actual, size_t actual_len) {
269
+ if (expected_len == actual_len &&
270
+ memcmp(expected, actual, expected_len) == 0) {
271
+ return true;
272
+ }
273
+
274
+ std::string expected_hex = EncodeHex(expected, expected_len);
275
+ std::string actual_hex = EncodeHex(actual, actual_len);
276
+ PrintLine("Expected: %s", expected_hex.c_str());
277
+ PrintLine("Actual: %s", actual_hex.c_str());
278
+ return false;
279
+ }
280
+
281
+ void FileTest::ClearTest() {
282
+ start_line_ = 0;
283
+ type_.clear();
284
+ parameter_.clear();
285
+ attributes_.clear();
286
+ block_.clear();
287
+ unused_attributes_.clear();
288
+ used_block_ = false;
289
+ }
290
+
291
+ void FileTest::OnKeyUsed(const std::string &key) {
292
+ unused_attributes_.erase(key);
293
+ }
294
+
295
+ int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
296
+ const char *path) {
297
+ FileTest t(path);
298
+ if (!t.is_open()) {
299
+ return 1;
300
+ }
301
+
302
+ bool failed = false;
303
+ while (true) {
304
+ FileTest::ReadResult ret = t.ReadNext();
305
+ if (ret == FileTest::kReadError) {
306
+ return 1;
307
+ } else if (ret == FileTest::kReadEOF) {
308
+ break;
309
+ }
310
+
311
+ bool result = run_test(&t, arg);
312
+ if (!result) {
313
+ // In case the test itself doesn't print output, print something so the
314
+ // line number is reported.
315
+ t.PrintLine("Test failed");
316
+ failed = true;
317
+ continue;
318
+ }
319
+ }
320
+
321
+ if (failed) {
322
+ return 1;
323
+ }
324
+
325
+ return 0;
326
+ }