You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.c 6.5 KiB

Add PPC64LE assembly for AES-GCM. This change adds AES and GHASH assembly from upstream, with the aim of speeding up AES-GCM. The PPC64LE assembly matches the interface of the ARMv8 assembly so I've changed the prefix of both sets of asm functions to be the same ("aes_hw_"). Otherwise, the new assmebly files and Perlasm match exactly those from upstream's c536b6be1a (from their master branch). Before: Did 1879000 AES-128-GCM (16 bytes) seal operations in 1000428us (1878196.1 ops/sec): 30.1 MB/s Did 61000 AES-128-GCM (1350 bytes) seal operations in 1006660us (60596.4 ops/sec): 81.8 MB/s Did 11000 AES-128-GCM (8192 bytes) seal operations in 1072649us (10255.0 ops/sec): 84.0 MB/s Did 1665000 AES-256-GCM (16 bytes) seal operations in 1000591us (1664016.6 ops/sec): 26.6 MB/s Did 52000 AES-256-GCM (1350 bytes) seal operations in 1006971us (51640.0 ops/sec): 69.7 MB/s Did 8840 AES-256-GCM (8192 bytes) seal operations in 1013294us (8724.0 ops/sec): 71.5 MB/s After: Did 4994000 AES-128-GCM (16 bytes) seal operations in 1000017us (4993915.1 ops/sec): 79.9 MB/s Did 1389000 AES-128-GCM (1350 bytes) seal operations in 1000073us (1388898.6 ops/sec): 1875.0 MB/s Did 319000 AES-128-GCM (8192 bytes) seal operations in 1000101us (318967.8 ops/sec): 2613.0 MB/s Did 4668000 AES-256-GCM (16 bytes) seal operations in 1000149us (4667304.6 ops/sec): 74.7 MB/s Did 1202000 AES-256-GCM (1350 bytes) seal operations in 1000646us (1201224.0 ops/sec): 1621.7 MB/s Did 269000 AES-256-GCM (8192 bytes) seal operations in 1002804us (268247.8 ops/sec): 2197.5 MB/s Change-Id: Id848562bd4e1aa79a4683012501dfa5e6c08cfcc Reviewed-on: https://boringssl-review.googlesource.com/11262 Reviewed-by: Adam Langley <agl@google.com> Commit-Queue: Adam Langley <agl@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
8 vuotta sitten
Make the POWER hardware capability value a global in crypto.c. (Thanks to Sam Panzer for the patch.) At least some linkers will drop constructor functions if no symbols from that translation unit are used elsewhere in the program. On POWER, since the cached capability value isn't a global in crypto.o (like other platforms), the constructor function is getting discarded. The C++11 spec says (3.6.2, paragraph 4): It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized. Compilers appear to interpret that to mean they are allowed to drop (i.e. indefinitely defer) constructors that occur in translation units that are never used, so they can avoid initializing some part of a library if it's dropped on the floor. This change makes the hardware capability value for POWER a global in crypto.c, which should prevent the constructor function from being ignored. Change-Id: I43ebe492d0ac1491f6f6c2097971a277f923dd3e Reviewed-on: https://boringssl-review.googlesource.com/14664 Commit-Queue: Adam Langley <agl@google.com> Commit-Queue: David Benjamin <davidben@google.com> Reviewed-by: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
7 vuotta sitten
Make the POWER hardware capability value a global in crypto.c. (Thanks to Sam Panzer for the patch.) At least some linkers will drop constructor functions if no symbols from that translation unit are used elsewhere in the program. On POWER, since the cached capability value isn't a global in crypto.o (like other platforms), the constructor function is getting discarded. The C++11 spec says (3.6.2, paragraph 4): It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable defined in the same translation unit as the variable to be initialized. Compilers appear to interpret that to mean they are allowed to drop (i.e. indefinitely defer) constructors that occur in translation units that are never used, so they can avoid initializing some part of a library if it's dropped on the floor. This change makes the hardware capability value for POWER a global in crypto.c, which should prevent the constructor function from being ignored. Change-Id: I43ebe492d0ac1491f6f6c2097971a277f923dd3e Reviewed-on: https://boringssl-review.googlesource.com/14664 Commit-Queue: Adam Langley <agl@google.com> Commit-Queue: David Benjamin <davidben@google.com> Reviewed-by: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
7 vuotta sitten
Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
8 vuotta sitten
Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
8 vuotta sitten
Switch OPENSSL_VERSION_NUMBER to 1.1.0. Although we are derived from 1.0.2, we mimic 1.1.0 in some ways around our FOO_up_ref functions and opaque libssl types. This causes some difficulties when porting third-party code as any OPENSSL_VERSION_NUMBER checks for 1.1.0 APIs we have will be wrong. Moreover, adding accessors without changing OPENSSL_VERSION_NUMBER can break external projects. It is common to implement a compatibility version of an accessor under #ifdef as a static function. This then conflicts with our headers if we, unlike OpenSSL 1.0.2, have this function. This change switches OPENSSL_VERSION_NUMBER to 1.1.0 and atomically adds enough accessors for software with 1.1.0 support already. The hope is this will unblock hiding SSL_CTX and SSL_SESSION, which will be especially useful with C++-ficiation. The cost is we will hit some growing pains as more 1.1.0 consumers enter the ecosystem and we converge on the right set of APIs to import from upstream. It does not remove any 1.0.2 APIs, so we will not require that all projects support 1.1.0. The exception is APIs which changed in 1.1.0 but did not change the function signature. Those are breaking changes. Specifically: - SSL_CTX_sess_set_get_cb is now const-correct. - X509_get0_signature is now const-correct. For C++ consumers only, this change temporarily includes an overload hack for SSL_CTX_sess_set_get_cb that keeps the old callback working. This is a workaround for Node not yet supporting OpenSSL 1.1.0. The version number is set at (the as yet unreleased) 1.1.0g to denote that this change includes https://github.com/openssl/openssl/pull/4384. Bug: 91 Change-Id: I5eeb27448a6db4c25c244afac37f9604d9608a76 Reviewed-on: https://boringssl-review.googlesource.com/10340 Commit-Queue: David Benjamin <davidben@google.com> CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> Reviewed-by: Adam Langley <agl@google.com>
8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include <openssl/crypto.h>
  15. #include <openssl/cpu.h>
  16. #include "internal.h"
  17. #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
  18. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  19. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
  20. defined(OPENSSL_PPC64LE))
  21. // x86, x86_64, the ARMs and ppc64le need to record the result of a
  22. // cpuid/getauxval call for the asm to work correctly, unless compiled without
  23. // asm code.
  24. #define NEED_CPUID
  25. #else
  26. // Otherwise, don't emit a static initialiser.
  27. #if !defined(BORINGSSL_NO_STATIC_INITIALIZER)
  28. #define BORINGSSL_NO_STATIC_INITIALIZER
  29. #endif
  30. #endif // !NO_ASM && !STATIC_ARMCAP &&
  31. // (X86 || X86_64 || ARM || AARCH64 || PPC64LE)
  32. // Our assembly does not use the GOT to reference symbols, which means
  33. // references to visible symbols will often require a TEXTREL. This is
  34. // undesirable, so all assembly-referenced symbols should be hidden. CPU
  35. // capabilities are the only such symbols defined in C. Explicitly hide them,
  36. // rather than rely on being built with -fvisibility=hidden.
  37. #if defined(OPENSSL_WINDOWS)
  38. #define HIDDEN
  39. #else
  40. #define HIDDEN __attribute__((visibility("hidden")))
  41. #endif
  42. // The capability variables are defined in this file in order to work around a
  43. // linker bug. When linking with a .a, if no symbols in a .o are referenced
  44. // then the .o is discarded, even if it has constructor functions.
  45. //
  46. // This still means that any binaries that don't include some functionality
  47. // that tests the capability values will still skip the constructor but, so
  48. // far, the init constructor function only sets the capability variables.
  49. #if !defined(NDEBUG) && !defined(BORINGSSL_FIPS)
  50. // This value must be explicitly initialised to zero in order to work around a
  51. // bug in libtool or the linker on OS X.
  52. //
  53. // If not initialised then it becomes a "common symbol". When put into an
  54. // archive, linking on OS X will fail to resolve common symbols. By
  55. // initialising it to zero, it becomes a "data symbol", which isn't so
  56. // affected.
  57. HIDDEN uint8_t BORINGSSL_function_hit[7] = {0};
  58. #endif
  59. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  60. // This value must be explicitly initialized to zero. See similar comment above.
  61. HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
  62. #elif defined(OPENSSL_PPC64LE)
  63. HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
  64. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  65. #include <openssl/arm_arch.h>
  66. #if defined(OPENSSL_STATIC_ARMCAP)
  67. HIDDEN uint32_t OPENSSL_armcap_P =
  68. #if defined(OPENSSL_STATIC_ARMCAP_NEON) || \
  69. (defined(__ARM_NEON__) || defined(__ARM_NEON))
  70. ARMV7_NEON |
  71. #endif
  72. #if defined(OPENSSL_STATIC_ARMCAP_AES) || defined(__ARM_FEATURE_CRYPTO)
  73. ARMV8_AES |
  74. #endif
  75. #if defined(OPENSSL_STATIC_ARMCAP_SHA1) || defined(__ARM_FEATURE_CRYPTO)
  76. ARMV8_SHA1 |
  77. #endif
  78. #if defined(OPENSSL_STATIC_ARMCAP_SHA256) || defined(__ARM_FEATURE_CRYPTO)
  79. ARMV8_SHA256 |
  80. #endif
  81. #if defined(OPENSSL_STATIC_ARMCAP_PMULL) || defined(__ARM_FEATURE_CRYPTO)
  82. ARMV8_PMULL |
  83. #endif
  84. 0;
  85. #else
  86. HIDDEN uint32_t OPENSSL_armcap_P = 0;
  87. uint32_t *OPENSSL_get_armcap_pointer_for_test(void) {
  88. return &OPENSSL_armcap_P;
  89. }
  90. #endif
  91. #endif
  92. #if defined(BORINGSSL_FIPS)
  93. // In FIPS mode, the power-on self-test function calls |CRYPTO_library_init|
  94. // because we have to ensure that CPUID detection occurs first.
  95. #define BORINGSSL_NO_STATIC_INITIALIZER
  96. #endif
  97. #if defined(OPENSSL_WINDOWS) && !defined(BORINGSSL_NO_STATIC_INITIALIZER)
  98. #define OPENSSL_CDECL __cdecl
  99. #else
  100. #define OPENSSL_CDECL
  101. #endif
  102. #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
  103. static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
  104. #elif defined(_MSC_VER)
  105. #pragma section(".CRT$XCU", read)
  106. static void __cdecl do_library_init(void);
  107. __declspec(allocate(".CRT$XCU")) void(*library_init_constructor)(void) =
  108. do_library_init;
  109. #else
  110. static void do_library_init(void) __attribute__ ((constructor));
  111. #endif
  112. // do_library_init is the actual initialization function. If
  113. // BORINGSSL_NO_STATIC_INITIALIZER isn't defined, this is set as a static
  114. // initializer. Otherwise, it is called by CRYPTO_library_init.
  115. static void OPENSSL_CDECL do_library_init(void) {
  116. // WARNING: this function may only configure the capability variables. See the
  117. // note above about the linker bug.
  118. #if defined(NEED_CPUID)
  119. OPENSSL_cpuid_setup();
  120. #endif
  121. }
  122. void CRYPTO_library_init(void) {
  123. // TODO(davidben): It would be tidier if this build knob could be replaced
  124. // with an internal lazy-init mechanism that would handle things correctly
  125. // in-library. https://crbug.com/542879
  126. #if defined(BORINGSSL_NO_STATIC_INITIALIZER)
  127. CRYPTO_once(&once, do_library_init);
  128. #endif
  129. }
  130. int CRYPTO_is_confidential_build(void) {
  131. #if defined(BORINGSSL_CONFIDENTIAL)
  132. return 1;
  133. #else
  134. return 0;
  135. #endif
  136. }
  137. int CRYPTO_has_asm(void) {
  138. #if defined(OPENSSL_NO_ASM)
  139. return 0;
  140. #else
  141. return 1;
  142. #endif
  143. }
  144. const char *SSLeay_version(int which) { return OpenSSL_version(which); }
  145. const char *OpenSSL_version(int which) {
  146. switch (which) {
  147. case OPENSSL_VERSION:
  148. return "BoringSSL";
  149. case OPENSSL_CFLAGS:
  150. return "compiler: n/a";
  151. case OPENSSL_BUILT_ON:
  152. return "built on: n/a";
  153. case OPENSSL_PLATFORM:
  154. return "platform: n/a";
  155. case OPENSSL_DIR:
  156. return "OPENSSLDIR: n/a";
  157. default:
  158. return "not available";
  159. }
  160. }
  161. unsigned long SSLeay(void) { return OPENSSL_VERSION_NUMBER; }
  162. unsigned long OpenSSL_version_num(void) { return OPENSSL_VERSION_NUMBER; }
  163. int CRYPTO_malloc_init(void) { return 1; }
  164. int OPENSSL_malloc_init(void) { return 1; }
  165. void ENGINE_load_builtin_engines(void) {}
  166. int ENGINE_register_all_complete(void) { return 1; }
  167. void OPENSSL_load_builtin_modules(void) {}
  168. int OPENSSL_init_crypto(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  169. CRYPTO_library_init();
  170. return 1;
  171. }
  172. void OPENSSL_cleanup(void) {}