Index: vendor-crypto/openssl/dist/CHANGES =================================================================== --- vendor-crypto/openssl/dist/CHANGES (revision 296272) +++ vendor-crypto/openssl/dist/CHANGES (revision 296273) @@ -1,10982 +1,11114 @@ OpenSSL CHANGES _______________ + Changes between 1.0.2f and 1.0.2g [1 Mar 2016] + + * Disable weak ciphers in SSLv3 and up in default builds of OpenSSL. + Builds that are not configured with "enable-weak-ssl-ciphers" will not + provide any "EXPORT" or "LOW" strength ciphers. + [Viktor Dukhovni] + + * Disable SSLv2 default build, default negotiation and weak ciphers. SSLv2 + is by default disabled at build-time. Builds that are not configured with + "enable-ssl2" will not support SSLv2. Even if "enable-ssl2" is used, + users who want to negotiate SSLv2 via the version-flexible SSLv23_method() + will need to explicitly call either of: + + SSL_CTX_clear_options(ctx, SSL_OP_NO_SSLv2); + or + SSL_clear_options(ssl, SSL_OP_NO_SSLv2); + + as appropriate. Even if either of those is used, or the application + explicitly uses the version-specific SSLv2_method() or its client and + server variants, SSLv2 ciphers vulnerable to exhaustive search key + recovery have been removed. Specifically, the SSLv2 40-bit EXPORT + ciphers, and SSLv2 56-bit DES are no longer available. + (CVE-2016-0800) + [Viktor Dukhovni] + + *) Fix a double-free in DSA code + + A double free bug was discovered when OpenSSL parses malformed DSA private + keys and could lead to a DoS attack or memory corruption for applications + that receive DSA private keys from untrusted sources. This scenario is + considered rare. + + This issue was reported to OpenSSL by Adam Langley(Google/BoringSSL) using + libFuzzer. + (CVE-2016-0705) + [Stephen Henson] + + *) Disable SRP fake user seed to address a server memory leak. + + Add a new method SRP_VBASE_get1_by_user that handles the seed properly. + + SRP_VBASE_get_by_user had inconsistent memory management behaviour. + In order to fix an unavoidable memory leak, SRP_VBASE_get_by_user + was changed to ignore the "fake user" SRP seed, even if the seed + is configured. + + Users should use SRP_VBASE_get1_by_user instead. Note that in + SRP_VBASE_get1_by_user, caller must free the returned value. Note + also that even though configuring the SRP seed attempts to hide + invalid usernames by continuing the handshake with fake + credentials, this behaviour is not constant time and no strong + guarantees are made that the handshake is indistinguishable from + that of a valid user. + (CVE-2016-0798) + [Emilia Käsper] + + *) Fix BN_hex2bn/BN_dec2bn NULL pointer deref/heap corruption + + In the BN_hex2bn function the number of hex digits is calculated using an + int value |i|. Later |bn_expand| is called with a value of |i * 4|. For + large values of |i| this can result in |bn_expand| not allocating any + memory because |i * 4| is negative. This can leave the internal BIGNUM data + field as NULL leading to a subsequent NULL ptr deref. For very large values + of |i|, the calculation |i * 4| could be a positive value smaller than |i|. + In this case memory is allocated to the internal BIGNUM data field, but it + is insufficiently sized leading to heap corruption. A similar issue exists + in BN_dec2bn. This could have security consequences if BN_hex2bn/BN_dec2bn + is ever called by user applications with very large untrusted hex/dec data. + This is anticipated to be a rare occurrence. + + All OpenSSL internal usage of these functions use data that is not expected + to be untrusted, e.g. config file data or application command line + arguments. If user developed applications generate config file data based + on untrusted data then it is possible that this could also lead to security + consequences. This is also anticipated to be rare. + + This issue was reported to OpenSSL by Guido Vranken. + (CVE-2016-0797) + [Matt Caswell] + + *) Fix memory issues in BIO_*printf functions + + The internal |fmtstr| function used in processing a "%s" format string in + the BIO_*printf functions could overflow while calculating the length of a + string and cause an OOB read when printing very long strings. + + Additionally the internal |doapr_outch| function can attempt to write to an + OOB memory location (at an offset from the NULL pointer) in the event of a + memory allocation failure. In 1.0.2 and below this could be caused where + the size of a buffer to be allocated is greater than INT_MAX. E.g. this + could be in processing a very long "%s" format string. Memory leaks can + also occur. + + The first issue may mask the second issue dependent on compiler behaviour. + These problems could enable attacks where large amounts of untrusted data + is passed to the BIO_*printf functions. If applications use these functions + in this way then they could be vulnerable. OpenSSL itself uses these + functions when printing out human-readable dumps of ASN.1 data. Therefore + applications that print this data could be vulnerable if the data is from + untrusted sources. OpenSSL command line applications could also be + vulnerable where they print out ASN.1 data, or if untrusted data is passed + as command line arguments. + + Libssl is not considered directly vulnerable. Additionally certificates etc + received via remote connections via libssl are also unlikely to be able to + trigger these issues because of message size limits enforced within libssl. + + This issue was reported to OpenSSL Guido Vranken. + (CVE-2016-0799) + [Matt Caswell] + + *) Side channel attack on modular exponentiation + + A side-channel attack was found which makes use of cache-bank conflicts on + the Intel Sandy-Bridge microarchitecture which could lead to the recovery + of RSA keys. The ability to exploit this issue is limited as it relies on + an attacker who has control of code in a thread running on the same + hyper-threaded core as the victim thread which is performing decryptions. + + This issue was reported to OpenSSL by Yuval Yarom, The University of + Adelaide and NICTA, Daniel Genkin, Technion and Tel Aviv University, and + Nadia Heninger, University of Pennsylvania with more information at + http://cachebleed.info. + (CVE-2016-0702) + [Andy Polyakov] + + *) Change the req app to generate a 2048-bit RSA/DSA key by default, + if no keysize is specified with default_bits. This fixes an + omission in an earlier change that changed all RSA/DSA key generation + apps to use 2048 bits by default. + [Emilia Käsper] + Changes between 1.0.2e and 1.0.2f [28 Jan 2016] *) DH small subgroups Historically OpenSSL only ever generated DH parameters based on "safe" primes. More recently (in version 1.0.2) support was provided for generating X9.42 style parameter files such as those required for RFC 5114 support. The primes used in such files may not be "safe". Where an application is using DH configured with parameters based on primes that are not "safe" then an attacker could use this fact to find a peer's private DH exponent. This attack requires that the attacker complete multiple handshakes in which the peer uses the same private DH exponent. For example this could be used to discover a TLS server's private DH exponent if it's reusing the private DH exponent or it's using a static DH ciphersuite. OpenSSL provides the option SSL_OP_SINGLE_DH_USE for ephemeral DH (DHE) in TLS. It is not on by default. If the option is not set then the server reuses the same private DH exponent for the life of the server process and would be vulnerable to this attack. It is believed that many popular applications do set this option and would therefore not be at risk. The fix for this issue adds an additional check where a "q" parameter is available (as is the case in X9.42 based parameters). This detects the only known attack, and is the only possible defense for static DH ciphersuites. This could have some performance impact. Additionally the SSL_OP_SINGLE_DH_USE option has been switched on by default and cannot be disabled. This could have some performance impact. This issue was reported to OpenSSL by Antonio Sanso (Adobe). (CVE-2016-0701) [Matt Caswell] *) SSLv2 doesn't block disabled ciphers A malicious client can negotiate SSLv2 ciphers that have been disabled on the server and complete SSLv2 handshakes even if all SSLv2 ciphers have been disabled, provided that the SSLv2 protocol was not also disabled via SSL_OP_NO_SSLv2. This issue was reported to OpenSSL on 26th December 2015 by Nimrod Aviram and Sebastian Schinzel. (CVE-2015-3197) [Viktor Dukhovni] *) Reject DH handshakes with parameters shorter than 1024 bits. [Kurt Roeckx] Changes between 1.0.2d and 1.0.2e [3 Dec 2015] *) BN_mod_exp may produce incorrect results on x86_64 There is a carry propagating bug in the x86_64 Montgomery squaring procedure. No EC algorithms are affected. Analysis suggests that attacks against RSA and DSA as a result of this defect would be very difficult to perform and are not believed likely. Attacks against DH are considered just feasible (although very difficult) because most of the work necessary to deduce information about a private key may be performed offline. The amount of resources required for such an attack would be very significant and likely only accessible to a limited number of attackers. An attacker would additionally need online access to an unpatched system using the target private key in a scenario with persistent DH parameters and a private key that is shared between multiple clients. For example this can occur by default in OpenSSL DHE based SSL/TLS ciphersuites. This issue was reported to OpenSSL by Hanno Böck. (CVE-2015-3193) [Andy Polyakov] *) Certificate verify crash with missing PSS parameter The signature verification routines will crash with a NULL pointer dereference if presented with an ASN.1 signature using the RSA PSS algorithm and absent mask generation function parameter. Since these routines are used to verify certificate signature algorithms this can be used to crash any certificate verification operation and exploited in a DoS attack. Any application which performs certificate verification is vulnerable including OpenSSL clients and servers which enable client authentication. This issue was reported to OpenSSL by Loïc Jonas Etienne (Qnective AG). (CVE-2015-3194) [Stephen Henson] *) X509_ATTRIBUTE memory leak When presented with a malformed X509_ATTRIBUTE structure OpenSSL will leak memory. This structure is used by the PKCS#7 and CMS routines so any application which reads PKCS#7 or CMS data from untrusted sources is affected. SSL/TLS is not affected. This issue was reported to OpenSSL by Adam Langley (Google/BoringSSL) using libFuzzer. (CVE-2015-3195) [Stephen Henson] *) Rewrite EVP_DecodeUpdate (base64 decoding) to fix several bugs. This changes the decoding behaviour for some invalid messages, though the change is mostly in the more lenient direction, and legacy behaviour is preserved as much as possible. [Emilia Käsper] *) In DSA_generate_parameters_ex, if the provided seed is too short, - return an error + use a random seed, as already documented. [Rich Salz and Ismo Puustinen ] Changes between 1.0.2c and 1.0.2d [9 Jul 2015] *) Alternate chains certificate forgery During certificate verfification, OpenSSL will attempt to find an alternative certificate chain if the first attempt to build such a chain fails. An error in the implementation of this logic can mean that an attacker could cause certain checks on untrusted certificates to be bypassed, such as the CA flag, enabling them to use a valid leaf certificate to act as a CA and "issue" an invalid certificate. This issue was reported to OpenSSL by Adam Langley/David Benjamin (Google/BoringSSL). (CVE-2015-1793) [Matt Caswell] *) Race condition handling PSK identify hint If PSK identity hints are received by a multi-threaded client then the values are wrongly updated in the parent SSL_CTX structure. This can result in a race condition potentially leading to a double free of the identify hint data. (CVE-2015-3196) [Stephen Henson] Changes between 1.0.2b and 1.0.2c [12 Jun 2015] *) Fix HMAC ABI incompatibility. The previous version introduced an ABI incompatibility in the handling of HMAC. The previous ABI has now been restored. Changes between 1.0.2a and 1.0.2b [11 Jun 2015] *) Malformed ECParameters causes infinite loop When processing an ECParameters structure OpenSSL enters an infinite loop if the curve specified is over a specially malformed binary polynomial field. This can be used to perform denial of service against any system which processes public keys, certificate requests or certificates. This includes TLS clients and TLS servers with client authentication enabled. This issue was reported to OpenSSL by Joseph Barr-Pixton. (CVE-2015-1788) [Andy Polyakov] *) Exploitable out-of-bounds read in X509_cmp_time X509_cmp_time does not properly check the length of the ASN1_TIME string and can read a few bytes out of bounds. In addition, X509_cmp_time accepts an arbitrary number of fractional seconds in the time string. An attacker can use this to craft malformed certificates and CRLs of various sizes and potentially cause a segmentation fault, resulting in a DoS on applications that verify certificates or CRLs. TLS clients that verify CRLs are affected. TLS clients and servers with client authentication enabled may be affected if they use custom verification callbacks. This issue was reported to OpenSSL by Robert Swiecki (Google), and independently by Hanno Böck. (CVE-2015-1789) [Emilia Käsper] *) PKCS7 crash with missing EnvelopedContent The PKCS#7 parsing code does not handle missing inner EncryptedContent correctly. An attacker can craft malformed ASN.1-encoded PKCS#7 blobs with missing content and trigger a NULL pointer dereference on parsing. Applications that decrypt PKCS#7 data or otherwise parse PKCS#7 structures from untrusted sources are affected. OpenSSL clients and servers are not affected. This issue was reported to OpenSSL by Michal Zalewski (Google). (CVE-2015-1790) [Emilia Käsper] *) CMS verify infinite loop with unknown hash function When verifying a signedData message the CMS code can enter an infinite loop if presented with an unknown hash function OID. This can be used to perform denial of service against any system which verifies signedData messages using the CMS code. This issue was reported to OpenSSL by Johannes Bauer. (CVE-2015-1792) [Stephen Henson] *) Race condition handling NewSessionTicket If a NewSessionTicket is received by a multi-threaded client when attempting to reuse a previous ticket then a race condition can occur potentially leading to a double free of the ticket data. (CVE-2015-1791) [Matt Caswell] *) Removed support for the two export grade static DH ciphersuites EXP-DH-RSA-DES-CBC-SHA and EXP-DH-DSS-DES-CBC-SHA. These two ciphersuites were newly added (along with a number of other static DH ciphersuites) to 1.0.2. However the two export ones have *never* worked since they were introduced. It seems strange in any case to be adding new export ciphersuites, and given "logjam" it also does not seem correct to fix them. [Matt Caswell] *) Only support 256-bit or stronger elliptic curves with the 'ecdh_auto' setting (server) or by default (client). Of supported curves, prefer P-256 (both). [Emilia Kasper] *) Reject DH handshakes with parameters shorter than 768 bits. [Kurt Roeckx and Emilia Kasper] Changes between 1.0.2 and 1.0.2a [19 Mar 2015] *) ClientHello sigalgs DoS fix If a client connects to an OpenSSL 1.0.2 server and renegotiates with an invalid signature algorithms extension a NULL pointer dereference will occur. This can be exploited in a DoS attack against the server. This issue was was reported to OpenSSL by David Ramos of Stanford University. (CVE-2015-0291) [Stephen Henson and Matt Caswell] *) Multiblock corrupted pointer fix OpenSSL 1.0.2 introduced the "multiblock" performance improvement. This feature only applies on 64 bit x86 architecture platforms that support AES NI instructions. A defect in the implementation of "multiblock" can cause OpenSSL's internal write buffer to become incorrectly set to NULL when using non-blocking IO. Typically, when the user application is using a socket BIO for writing, this will only result in a failed connection. However if some other BIO is used then it is likely that a segmentation fault will be triggered, thus enabling a potential DoS attack. This issue was reported to OpenSSL by Daniel Danner and Rainer Mueller. (CVE-2015-0290) [Matt Caswell] *) Segmentation fault in DTLSv1_listen fix The DTLSv1_listen function is intended to be stateless and processes the initial ClientHello from many peers. It is common for user code to loop over the call to DTLSv1_listen until a valid ClientHello is received with an associated cookie. A defect in the implementation of DTLSv1_listen means that state is preserved in the SSL object from one invocation to the next that can lead to a segmentation fault. Errors processing the initial ClientHello can trigger this scenario. An example of such an error could be that a DTLS1.0 only client is attempting to connect to a DTLS1.2 only server. This issue was reported to OpenSSL by Per Allansson. (CVE-2015-0207) [Matt Caswell] *) Segmentation fault in ASN1_TYPE_cmp fix The function ASN1_TYPE_cmp will crash with an invalid read if an attempt is made to compare ASN.1 boolean types. Since ASN1_TYPE_cmp is used to check certificate signature algorithm consistency this can be used to crash any certificate verification operation and exploited in a DoS attack. Any application which performs certificate verification is vulnerable including OpenSSL clients and servers which enable client authentication. (CVE-2015-0286) [Stephen Henson] *) Segmentation fault for invalid PSS parameters fix The signature verification routines will crash with a NULL pointer dereference if presented with an ASN.1 signature using the RSA PSS algorithm and invalid parameters. Since these routines are used to verify certificate signature algorithms this can be used to crash any certificate verification operation and exploited in a DoS attack. Any application which performs certificate verification is vulnerable including OpenSSL clients and servers which enable client authentication. This issue was was reported to OpenSSL by Brian Carpenter. (CVE-2015-0208) [Stephen Henson] *) ASN.1 structure reuse memory corruption fix Reusing a structure in ASN.1 parsing may allow an attacker to cause memory corruption via an invalid write. Such reuse is and has been strongly discouraged and is believed to be rare. Applications that parse structures containing CHOICE or ANY DEFINED BY components may be affected. Certificate parsing (d2i_X509 and related functions) are however not affected. OpenSSL clients and servers are not affected. (CVE-2015-0287) [Stephen Henson] *) PKCS7 NULL pointer dereferences fix The PKCS#7 parsing code does not handle missing outer ContentInfo correctly. An attacker can craft malformed ASN.1-encoded PKCS#7 blobs with missing content and trigger a NULL pointer dereference on parsing. Applications that verify PKCS#7 signatures, decrypt PKCS#7 data or otherwise parse PKCS#7 structures from untrusted sources are affected. OpenSSL clients and servers are not affected. This issue was reported to OpenSSL by Michal Zalewski (Google). (CVE-2015-0289) [Emilia Käsper] *) DoS via reachable assert in SSLv2 servers fix A malicious client can trigger an OPENSSL_assert (i.e., an abort) in servers that both support SSLv2 and enable export cipher suites by sending a specially crafted SSLv2 CLIENT-MASTER-KEY message. This issue was discovered by Sean Burford (Google) and Emilia Käsper (OpenSSL development team). (CVE-2015-0293) [Emilia Käsper] *) Empty CKE with client auth and DHE fix If client auth is used then a server can seg fault in the event of a DHE ciphersuite being selected and a zero length ClientKeyExchange message being sent by the client. This could be exploited in a DoS attack. (CVE-2015-1787) [Matt Caswell] *) Handshake with unseeded PRNG fix Under certain conditions an OpenSSL 1.0.2 client can complete a handshake with an unseeded PRNG. The conditions are: - The client is on a platform where the PRNG has not been seeded automatically, and the user has not seeded manually - A protocol specific client method version has been used (i.e. not SSL_client_methodv23) - A ciphersuite is used that does not require additional random data from the PRNG beyond the initial ClientHello client random (e.g. PSK-RC4-SHA). If the handshake succeeds then the client random that has been used will have been generated from a PRNG with insufficient entropy and therefore the output may be predictable. For example using the following command with an unseeded openssl will succeed on an unpatched platform: openssl s_client -psk 1a2b3c4d -tls1_2 -cipher PSK-RC4-SHA (CVE-2015-0285) [Matt Caswell] *) Use After Free following d2i_ECPrivatekey error fix A malformed EC private key file consumed via the d2i_ECPrivateKey function could cause a use after free condition. This, in turn, could cause a double free in several private key parsing functions (such as d2i_PrivateKey or EVP_PKCS82PKEY) and could lead to a DoS attack or memory corruption for applications that receive EC private keys from untrusted sources. This scenario is considered rare. This issue was discovered by the BoringSSL project and fixed in their commit 517073cd4b. (CVE-2015-0209) [Matt Caswell] *) X509_to_X509_REQ NULL pointer deref fix The function X509_to_X509_REQ will crash with a NULL pointer dereference if the certificate key is invalid. This function is rarely used in practice. This issue was discovered by Brian Carpenter. (CVE-2015-0288) [Stephen Henson] *) Removed the export ciphers from the DEFAULT ciphers [Kurt Roeckx] Changes between 1.0.1l and 1.0.2 [22 Jan 2015] *) Change RSA and DH/DSA key generation apps to generate 2048-bit keys by default. [Kurt Roeckx] *) Facilitate "universal" ARM builds targeting range of ARM ISAs, e.g. ARMv5 through ARMv8, as opposite to "locking" it to single one. So far those who have to target multiple plaforms would compromise and argue that binary targeting say ARMv5 would still execute on ARMv8. "Universal" build resolves this compromise by providing near-optimal performance even on newer platforms. [Andy Polyakov] *) Accelerated NIST P-256 elliptic curve implementation for x86_64 (other platforms pending). [Shay Gueron & Vlad Krasnov (Intel Corp), Andy Polyakov] *) Add support for the SignedCertificateTimestampList certificate and OCSP response extensions from RFC6962. [Rob Stradling] *) Fix ec_GFp_simple_points_make_affine (thus, EC_POINTs_mul etc.) for corner cases. (Certain input points at infinity could lead to bogus results, with non-infinity inputs mapped to infinity too.) [Bodo Moeller] *) Initial support for PowerISA 2.0.7, first implemented in POWER8. This covers AES, SHA256/512 and GHASH. "Initial" means that most common cases are optimized and there still is room for further improvements. Vector Permutation AES for Altivec is also added. [Andy Polyakov] *) Add support for little-endian ppc64 Linux target. [Marcelo Cerri (IBM)] *) Initial support for AMRv8 ISA crypto extensions. This covers AES, SHA1, SHA256 and GHASH. "Initial" means that most common cases are optimized and there still is room for further improvements. Both 32- and 64-bit modes are supported. [Andy Polyakov, Ard Biesheuvel (Linaro)] *) Improved ARMv7 NEON support. [Andy Polyakov] *) Support for SPARC Architecture 2011 crypto extensions, first implemented in SPARC T4. This covers AES, DES, Camellia, SHA1, SHA256/512, MD5, GHASH and modular exponentiation. [Andy Polyakov, David Miller] *) Accelerated modular exponentiation for Intel processors, a.k.a. RSAZ. [Shay Gueron & Vlad Krasnov (Intel Corp)] *) Support for new and upcoming Intel processors, including AVX2, BMI and SHA ISA extensions. This includes additional "stitched" implementations, AESNI-SHA256 and GCM, and multi-buffer support for TLS encrypt. This work was sponsored by Intel Corp. [Andy Polyakov] *) Support for DTLS 1.2. This adds two sets of DTLS methods: DTLS_*_method() supports both DTLS 1.2 and 1.0 and should use whatever version the peer supports and DTLSv1_2_*_method() which supports DTLS 1.2 only. [Steve Henson] *) Use algorithm specific chains in SSL_CTX_use_certificate_chain_file(): this fixes a limiation in previous versions of OpenSSL. [Steve Henson] *) Extended RSA OAEP support via EVP_PKEY API. Options to specify digest, MGF1 digest and OAEP label. [Steve Henson] *) Add EVP support for key wrapping algorithms, to avoid problems with existing code the flag EVP_CIPHER_CTX_WRAP_ALLOW has to be set in the EVP_CIPHER_CTX or an error is returned. Add AES and DES3 wrap algorithms and include tests cases. [Steve Henson] *) Add functions to allocate and set the fields of an ECDSA_METHOD structure. [Douglas E. Engert, Steve Henson] *) New functions OPENSSL_gmtime_diff and ASN1_TIME_diff to find the difference in days and seconds between two tm or ASN1_TIME structures. [Steve Henson] *) Add -rev test option to s_server to just reverse order of characters received by client and send back to server. Also prints an abbreviated summary of the connection parameters. [Steve Henson] *) New option -brief for s_client and s_server to print out a brief summary of connection parameters. [Steve Henson] *) Add callbacks for arbitrary TLS extensions. [Trevor Perrin and Ben Laurie] *) New option -crl_download in several openssl utilities to download CRLs from CRLDP extension in certificates. [Steve Henson] *) New options -CRL and -CRLform for s_client and s_server for CRLs. [Steve Henson] *) New function X509_CRL_diff to generate a delta CRL from the difference of two full CRLs. Add support to "crl" utility. [Steve Henson] *) New functions to set lookup_crls function and to retrieve X509_STORE from X509_STORE_CTX. [Steve Henson] *) Print out deprecated issuer and subject unique ID fields in certificates. [Steve Henson] *) Extend OCSP I/O functions so they can be used for simple general purpose HTTP as well as OCSP. New wrapper function which can be used to download CRLs using the OCSP API. [Steve Henson] *) Delegate command line handling in s_client/s_server to SSL_CONF APIs. [Steve Henson] *) SSL_CONF* functions. These provide a common framework for application configuration using configuration files or command lines. [Steve Henson] *) SSL/TLS tracing code. This parses out SSL/TLS records using the message callback and prints the results. Needs compile time option "enable-ssl-trace". New options to s_client and s_server to enable tracing. [Steve Henson] *) New ctrl and macro to retrieve supported points extensions. Print out extension in s_server and s_client. [Steve Henson] *) New functions to retrieve certificate signature and signature OID NID. [Steve Henson] *) Add functions to retrieve and manipulate the raw cipherlist sent by a client to OpenSSL. [Steve Henson] *) New Suite B modes for TLS code. These use and enforce the requirements of RFC6460: restrict ciphersuites, only permit Suite B algorithms and only use Suite B curves. The Suite B modes can be set by using the strings "SUITEB128", "SUITEB192" or "SUITEB128ONLY" for the cipherstring. [Steve Henson] *) New chain verification flags for Suite B levels of security. Check algorithms are acceptable when flags are set in X509_verify_cert. [Steve Henson] *) Make tls1_check_chain return a set of flags indicating checks passed by a certificate chain. Add additional tests to handle client certificates: checks for matching certificate type and issuer name comparison. [Steve Henson] *) If an attempt is made to use a signature algorithm not in the peer preference list abort the handshake. If client has no suitable signature algorithms in response to a certificate request do not use the certificate. [Steve Henson] *) If server EC tmp key is not in client preference list abort handshake. [Steve Henson] *) Add support for certificate stores in CERT structure. This makes it possible to have different stores per SSL structure or one store in the parent SSL_CTX. Include distint stores for certificate chain verification and chain building. New ctrl SSL_CTRL_BUILD_CERT_CHAIN to build and store a certificate chain in CERT structure: returing an error if the chain cannot be built: this will allow applications to test if a chain is correctly configured. Note: if the CERT based stores are not set then the parent SSL_CTX store is used to retain compatibility with existing behaviour. [Steve Henson] *) New function ssl_set_client_disabled to set a ciphersuite disabled mask based on the current session, check mask when sending client hello and checking the requested ciphersuite. [Steve Henson] *) New ctrls to retrieve and set certificate types in a certificate request message. Print out received values in s_client. If certificate types is not set with custom values set sensible values based on supported signature algorithms. [Steve Henson] *) Support for distinct client and server supported signature algorithms. [Steve Henson] *) Add certificate callback. If set this is called whenever a certificate is required by client or server. An application can decide which certificate chain to present based on arbitrary criteria: for example supported signature algorithms. Add very simple example to s_server. This fixes many of the problems and restrictions of the existing client certificate callback: for example you can now clear an existing certificate and specify the whole chain. [Steve Henson] *) Add new "valid_flags" field to CERT_PKEY structure which determines what the certificate can be used for (if anything). Set valid_flags field in new tls1_check_chain function. Simplify ssl_set_cert_masks which used to have similar checks in it. Add new "cert_flags" field to CERT structure and include a "strict mode". This enforces some TLS certificate requirements (such as only permitting certificate signature algorithms contained in the supported algorithms extension) which some implementations ignore: this option should be used with caution as it could cause interoperability issues. [Steve Henson] *) Update and tidy signature algorithm extension processing. Work out shared signature algorithms based on preferences and peer algorithms and print them out in s_client and s_server. Abort handshake if no shared signature algorithms. [Steve Henson] *) Add new functions to allow customised supported signature algorithms for SSL and SSL_CTX structures. Add options to s_client and s_server to support them. [Steve Henson] *) New function SSL_certs_clear() to delete all references to certificates from an SSL structure. Before this once a certificate had been added it couldn't be removed. [Steve Henson] *) Integrate hostname, email address and IP address checking with certificate verification. New verify options supporting checking in opensl utility. [Steve Henson] *) Fixes and wildcard matching support to hostname and email checking functions. Add manual page. [Florian Weimer (Red Hat Product Security Team)] *) New functions to check a hostname email or IP address against a certificate. Add options x509 utility to print results of checks against a certificate. [Steve Henson] *) Fix OCSP checking. [Rob Stradling and Ben Laurie] *) Initial experimental support for explicitly trusted non-root CAs. OpenSSL still tries to build a complete chain to a root but if an intermediate CA has a trust setting included that is used. The first setting is used: whether to trust (e.g., -addtrust option to the x509 utility) or reject. [Steve Henson] *) Add -trusted_first option which attempts to find certificates in the trusted store even if an untrusted chain is also supplied. [Steve Henson] *) MIPS assembly pack updates: support for MIPS32r2 and SmartMIPS ASE, platform support for Linux and Android. [Andy Polyakov] *) Support for linux-x32, ILP32 environment in x86_64 framework. [Andy Polyakov] *) Experimental multi-implementation support for FIPS capable OpenSSL. When in FIPS mode the approved implementations are used as normal, when not in FIPS mode the internal unapproved versions are used instead. This means that the FIPS capable OpenSSL isn't forced to use the (often lower perfomance) FIPS implementations outside FIPS mode. [Steve Henson] *) Transparently support X9.42 DH parameters when calling PEM_read_bio_DHparameters. This means existing applications can handle the new parameter format automatically. [Steve Henson] *) Initial experimental support for X9.42 DH parameter format: mainly to support use of 'q' parameter for RFC5114 parameters. [Steve Henson] *) Add DH parameters from RFC5114 including test data to dhtest. [Steve Henson] *) Support for automatic EC temporary key parameter selection. If enabled the most preferred EC parameters are automatically used instead of hardcoded fixed parameters. Now a server just has to call: SSL_CTX_set_ecdh_auto(ctx, 1) and the server will automatically support ECDH and use the most appropriate parameters. [Steve Henson] *) Enhance and tidy EC curve and point format TLS extension code. Use static structures instead of allocation if default values are used. New ctrls to set curves we wish to support and to retrieve shared curves. Print out shared curves in s_server. New options to s_server and s_client to set list of supported curves. [Steve Henson] *) New ctrls to retrieve supported signature algorithms and supported curve values as an array of NIDs. Extend openssl utility to print out received values. [Steve Henson] *) Add new APIs EC_curve_nist2nid and EC_curve_nid2nist which convert between NIDs and the more common NIST names such as "P-256". Enhance ecparam utility and ECC method to recognise the NIST names for curves. [Steve Henson] *) Enhance SSL/TLS certificate chain handling to support different chains for each certificate instead of one chain in the parent SSL_CTX. [Steve Henson] *) Support for fixed DH ciphersuite client authentication: where both server and client use DH certificates with common parameters. [Steve Henson] *) Support for fixed DH ciphersuites: those requiring DH server certificates. [Steve Henson] *) New function i2d_re_X509_tbs for re-encoding the TBS portion of the certificate. Note: Related 1.0.2-beta specific macros X509_get_cert_info, X509_CINF_set_modified, X509_CINF_get_issuer, X509_CINF_get_extensions and X509_CINF_get_signature were reverted post internal team review. Changes between 1.0.1k and 1.0.1l [15 Jan 2015] *) Build fixes for the Windows and OpenVMS platforms [Matt Caswell and Richard Levitte] Changes between 1.0.1j and 1.0.1k [8 Jan 2015] *) Fix DTLS segmentation fault in dtls1_get_record. A carefully crafted DTLS message can cause a segmentation fault in OpenSSL due to a NULL pointer dereference. This could lead to a Denial Of Service attack. Thanks to Markus Stenberg of Cisco Systems, Inc. for reporting this issue. (CVE-2014-3571) [Steve Henson] *) Fix DTLS memory leak in dtls1_buffer_record. A memory leak can occur in the dtls1_buffer_record function under certain conditions. In particular this could occur if an attacker sent repeated DTLS records with the same sequence number but for the next epoch. The memory leak could be exploited by an attacker in a Denial of Service attack through memory exhaustion. Thanks to Chris Mueller for reporting this issue. (CVE-2015-0206) [Matt Caswell] *) Fix issue where no-ssl3 configuration sets method to NULL. When openssl is built with the no-ssl3 option and a SSL v3 ClientHello is received the ssl method would be set to NULL which could later result in a NULL pointer dereference. Thanks to Frank Schmirler for reporting this issue. (CVE-2014-3569) [Kurt Roeckx] *) Abort handshake if server key exchange message is omitted for ephemeral ECDH ciphersuites. Thanks to Karthikeyan Bhargavan of the PROSECCO team at INRIA for reporting this issue. (CVE-2014-3572) [Steve Henson] *) Remove non-export ephemeral RSA code on client and server. This code violated the TLS standard by allowing the use of temporary RSA keys in non-export ciphersuites and could be used by a server to effectively downgrade the RSA key length used to a value smaller than the server certificate. Thanks for Karthikeyan Bhargavan of the PROSECCO team at INRIA or reporting this issue. (CVE-2015-0204) [Steve Henson] *) Fixed issue where DH client certificates are accepted without verification. An OpenSSL server will accept a DH certificate for client authentication without the certificate verify message. This effectively allows a client to authenticate without the use of a private key. This only affects servers which trust a client certificate authority which issues certificates containing DH keys: these are extremely rare and hardly ever encountered. Thanks for Karthikeyan Bhargavan of the PROSECCO team at INRIA or reporting this issue. (CVE-2015-0205) [Steve Henson] *) Ensure that the session ID context of an SSL is updated when its SSL_CTX is updated via SSL_set_SSL_CTX. The session ID context is typically set from the parent SSL_CTX, and can vary with the CTX. [Adam Langley] *) Fix various certificate fingerprint issues. By using non-DER or invalid encodings outside the signed portion of a certificate the fingerprint can be changed without breaking the signature. Although no details of the signed portion of the certificate can be changed this can cause problems with some applications: e.g. those using the certificate fingerprint for blacklists. 1. Reject signatures with non zero unused bits. If the BIT STRING containing the signature has non zero unused bits reject the signature. All current signature algorithms require zero unused bits. 2. Check certificate algorithm consistency. Check the AlgorithmIdentifier inside TBS matches the one in the certificate signature. NB: this will result in signature failure errors for some broken certificates. Thanks to Konrad Kraszewski from Google for reporting this issue. 3. Check DSA/ECDSA signatures use DER. Reencode DSA/ECDSA signatures and compare with the original received signature. Return an error if there is a mismatch. This will reject various cases including garbage after signature (thanks to Antti Karjalainen and Tuomo Untinen from the Codenomicon CROSS program for discovering this case) and use of BER or invalid ASN.1 INTEGERs (negative or with leading zeroes). Further analysis was conducted and fixes were developed by Stephen Henson of the OpenSSL core team. (CVE-2014-8275) [Steve Henson] *) Correct Bignum squaring. Bignum squaring (BN_sqr) may produce incorrect results on some platforms, including x86_64. This bug occurs at random with a very low probability, and is not known to be exploitable in any way, though its exact impact is difficult to determine. Thanks to Pieter Wuille (Blockstream) who reported this issue and also suggested an initial fix. Further analysis was conducted by the OpenSSL development team and Adam Langley of Google. The final fix was developed by Andy Polyakov of the OpenSSL core team. (CVE-2014-3570) [Andy Polyakov] *) Do not resume sessions on the server if the negotiated protocol version does not match the session's version. Resuming with a different version, while not strictly forbidden by the RFC, is of questionable sanity and breaks all known clients. [David Benjamin, Emilia Käsper] *) Tighten handling of the ChangeCipherSpec (CCS) message: reject early CCS messages during renegotiation. (Note that because renegotiation is encrypted, this early CCS was not exploitable.) [Emilia Käsper] *) Tighten client-side session ticket handling during renegotiation: ensure that the client only accepts a session ticket if the server sends the extension anew in the ServerHello. Previously, a TLS client would reuse the old extension state and thus accept a session ticket if one was announced in the initial ServerHello. Similarly, ensure that the client requires a session ticket if one was advertised in the ServerHello. Previously, a TLS client would ignore a missing NewSessionTicket message. [Emilia Käsper] Changes between 1.0.1i and 1.0.1j [15 Oct 2014] *) SRTP Memory Leak. A flaw in the DTLS SRTP extension parsing code allows an attacker, who sends a carefully crafted handshake message, to cause OpenSSL to fail to free up to 64k of memory causing a memory leak. This could be exploited in a Denial Of Service attack. This issue affects OpenSSL 1.0.1 server implementations for both SSL/TLS and DTLS regardless of whether SRTP is used or configured. Implementations of OpenSSL that have been compiled with OPENSSL_NO_SRTP defined are not affected. The fix was developed by the OpenSSL team. (CVE-2014-3513) [OpenSSL team] *) Session Ticket Memory Leak. When an OpenSSL SSL/TLS/DTLS server receives a session ticket the integrity of that ticket is first verified. In the event of a session ticket integrity check failing, OpenSSL will fail to free memory causing a memory leak. By sending a large number of invalid session tickets an attacker could exploit this issue in a Denial Of Service attack. (CVE-2014-3567) [Steve Henson] *) Build option no-ssl3 is incomplete. When OpenSSL is configured with "no-ssl3" as a build option, servers could accept and complete a SSL 3.0 handshake, and clients could be configured to send them. (CVE-2014-3568) [Akamai and the OpenSSL team] *) Add support for TLS_FALLBACK_SCSV. Client applications doing fallback retries should call SSL_set_mode(s, SSL_MODE_SEND_FALLBACK_SCSV). (CVE-2014-3566) [Adam Langley, Bodo Moeller] *) Add additional DigestInfo checks. Reencode DigestInto in DER and check against the original when verifying RSA signature: this will reject any improperly encoded DigestInfo structures. Note: this is a precautionary measure and no attacks are currently known. [Steve Henson] Changes between 1.0.1h and 1.0.1i [6 Aug 2014] *) Fix SRP buffer overrun vulnerability. Invalid parameters passed to the SRP code can be overrun an internal buffer. Add sanity check that g, A, B < N to SRP code. Thanks to Sean Devlin and Watson Ladd of Cryptography Services, NCC Group for discovering this issue. (CVE-2014-3512) [Steve Henson] *) A flaw in the OpenSSL SSL/TLS server code causes the server to negotiate TLS 1.0 instead of higher protocol versions when the ClientHello message is badly fragmented. This allows a man-in-the-middle attacker to force a downgrade to TLS 1.0 even if both the server and the client support a higher protocol version, by modifying the client's TLS records. Thanks to David Benjamin and Adam Langley (Google) for discovering and researching this issue. (CVE-2014-3511) [David Benjamin] *) OpenSSL DTLS clients enabling anonymous (EC)DH ciphersuites are subject to a denial of service attack. A malicious server can crash the client with a null pointer dereference (read) by specifying an anonymous (EC)DH ciphersuite and sending carefully crafted handshake messages. Thanks to Felix Gröbert (Google) for discovering and researching this issue. (CVE-2014-3510) [Emilia Käsper] *) By sending carefully crafted DTLS packets an attacker could cause openssl to leak memory. This can be exploited through a Denial of Service attack. Thanks to Adam Langley for discovering and researching this issue. (CVE-2014-3507) [Adam Langley] *) An attacker can force openssl to consume large amounts of memory whilst processing DTLS handshake messages. This can be exploited through a Denial of Service attack. Thanks to Adam Langley for discovering and researching this issue. (CVE-2014-3506) [Adam Langley] *) An attacker can force an error condition which causes openssl to crash whilst processing DTLS packets due to memory being freed twice. This can be exploited through a Denial of Service attack. Thanks to Adam Langley and Wan-Teh Chang for discovering and researching this issue. (CVE-2014-3505) [Adam Langley] *) If a multithreaded client connects to a malicious server using a resumed session and the server sends an ec point format extension it could write up to 255 bytes to freed memory. Thanks to Gabor Tyukasz (LogMeIn Inc) for discovering and researching this issue. (CVE-2014-3509) [Gabor Tyukasz] *) A malicious server can crash an OpenSSL client with a null pointer dereference (read) by specifying an SRP ciphersuite even though it was not properly negotiated with the client. This can be exploited through a Denial of Service attack. Thanks to Joonas Kuorilehto and Riku Hietamäki (Codenomicon) for discovering and researching this issue. (CVE-2014-5139) [Steve Henson] *) A flaw in OBJ_obj2txt may cause pretty printing functions such as X509_name_oneline, X509_name_print_ex et al. to leak some information from the stack. Applications may be affected if they echo pretty printing output to the attacker. Thanks to Ivan Fratric (Google) for discovering this issue. (CVE-2014-3508) [Emilia Käsper, and Steve Henson] *) Fix ec_GFp_simple_points_make_affine (thus, EC_POINTs_mul etc.) for corner cases. (Certain input points at infinity could lead to bogus results, with non-infinity inputs mapped to infinity too.) [Bodo Moeller] Changes between 1.0.1g and 1.0.1h [5 Jun 2014] *) Fix for SSL/TLS MITM flaw. An attacker using a carefully crafted handshake can force the use of weak keying material in OpenSSL SSL/TLS clients and servers. Thanks to KIKUCHI Masashi (Lepidum Co. Ltd.) for discovering and researching this issue. (CVE-2014-0224) [KIKUCHI Masashi, Steve Henson] *) Fix DTLS recursion flaw. By sending an invalid DTLS handshake to an OpenSSL DTLS client the code can be made to recurse eventually crashing in a DoS attack. Thanks to Imre Rad (Search-Lab Ltd.) for discovering this issue. (CVE-2014-0221) [Imre Rad, Steve Henson] *) Fix DTLS invalid fragment vulnerability. A buffer overrun attack can be triggered by sending invalid DTLS fragments to an OpenSSL DTLS client or server. This is potentially exploitable to run arbitrary code on a vulnerable client or server. Thanks to Jüri Aedla for reporting this issue. (CVE-2014-0195) [Jüri Aedla, Steve Henson] *) Fix bug in TLS code where clients enable anonymous ECDH ciphersuites are subject to a denial of service attack. Thanks to Felix Gröbert and Ivan Fratric at Google for discovering this issue. (CVE-2014-3470) [Felix Gröbert, Ivan Fratric, Steve Henson] *) Harmonize version and its documentation. -f flag is used to display compilation flags. [mancha ] *) Fix eckey_priv_encode so it immediately returns an error upon a failure in i2d_ECPrivateKey. Thanks to Ted Unangst for feedback on this issue. [mancha ] *) Fix some double frees. These are not thought to be exploitable. [mancha ] Changes between 1.0.1f and 1.0.1g [7 Apr 2014] *) A missing bounds check in the handling of the TLS heartbeat extension can be used to reveal up to 64k of memory to a connected client or server. Thanks for Neel Mehta of Google Security for discovering this bug and to Adam Langley and Bodo Moeller for preparing the fix (CVE-2014-0160) [Adam Langley, Bodo Moeller] *) Fix for the attack described in the paper "Recovering OpenSSL ECDSA Nonces Using the FLUSH+RELOAD Cache Side-channel Attack" by Yuval Yarom and Naomi Benger. Details can be obtained from: http://eprint.iacr.org/2014/140 Thanks to Yuval Yarom and Naomi Benger for discovering this flaw and to Yuval Yarom for supplying a fix (CVE-2014-0076) [Yuval Yarom and Naomi Benger] *) TLS pad extension: draft-agl-tls-padding-03 Workaround for the "TLS hang bug" (see FAQ and PR#2771): if the TLS client Hello record length value would otherwise be > 255 and less that 512 pad with a dummy extension containing zeroes so it is at least 512 bytes long. [Adam Langley, Steve Henson] Changes between 1.0.1e and 1.0.1f [6 Jan 2014] *) Fix for TLS record tampering bug. A carefully crafted invalid handshake could crash OpenSSL with a NULL pointer exception. Thanks to Anton Johansson for reporting this issues. (CVE-2013-4353) *) Keep original DTLS digest and encryption contexts in retransmission structures so we can use the previous session parameters if they need to be resent. (CVE-2013-6450) [Steve Henson] *) Add option SSL_OP_SAFARI_ECDHE_ECDSA_BUG (part of SSL_OP_ALL) which avoids preferring ECDHE-ECDSA ciphers when the client appears to be Safari on OS X. Safari on OS X 10.8..10.8.3 advertises support for several ECDHE-ECDSA ciphers, but fails to negotiate them. The bug is fixed in OS X 10.8.4, but Apple have ruled out both hot fixing 10.8..10.8.3 and forcing users to upgrade to 10.8.4 or newer. [Rob Stradling, Adam Langley] Changes between 1.0.1d and 1.0.1e [11 Feb 2013] *) Correct fix for CVE-2013-0169. The original didn't work on AES-NI supporting platforms or when small records were transferred. [Andy Polyakov, Steve Henson] Changes between 1.0.1c and 1.0.1d [5 Feb 2013] *) Make the decoding of SSLv3, TLS and DTLS CBC records constant time. This addresses the flaw in CBC record processing discovered by Nadhem Alfardan and Kenny Paterson. Details of this attack can be found at: http://www.isg.rhul.ac.uk/tls/ Thanks go to Nadhem Alfardan and Kenny Paterson of the Information Security Group at Royal Holloway, University of London (www.isg.rhul.ac.uk) for discovering this flaw and Adam Langley and Emilia Käsper for the initial patch. (CVE-2013-0169) [Emilia Käsper, Adam Langley, Ben Laurie, Andy Polyakov, Steve Henson] *) Fix flaw in AESNI handling of TLS 1.2 and 1.1 records for CBC mode ciphersuites which can be exploited in a denial of service attack. Thanks go to and to Adam Langley for discovering and detecting this bug and to Wolfgang Ettlinger for independently discovering this issue. (CVE-2012-2686) [Adam Langley] *) Return an error when checking OCSP signatures when key is NULL. This fixes a DoS attack. (CVE-2013-0166) [Steve Henson] *) Make openssl verify return errors. [Chris Palmer and Ben Laurie] *) Call OCSP Stapling callback after ciphersuite has been chosen, so the right response is stapled. Also change SSL_get_certificate() so it returns the certificate actually sent. See http://rt.openssl.org/Ticket/Display.html?id=2836. [Rob Stradling ] *) Fix possible deadlock when decoding public keys. [Steve Henson] *) Don't use TLS 1.0 record version number in initial client hello if renegotiating. [Steve Henson] Changes between 1.0.1b and 1.0.1c [10 May 2012] *) Sanity check record length before skipping explicit IV in TLS 1.2, 1.1 and DTLS to fix DoS attack. Thanks to Codenomicon for discovering this issue using Fuzz-o-Matic fuzzing as a service testing platform. (CVE-2012-2333) [Steve Henson] *) Initialise tkeylen properly when encrypting CMS messages. Thanks to Solar Designer of Openwall for reporting this issue. [Steve Henson] *) In FIPS mode don't try to use composite ciphers as they are not approved. [Steve Henson] Changes between 1.0.1a and 1.0.1b [26 Apr 2012] *) OpenSSL 1.0.0 sets SSL_OP_ALL to 0x80000FFFL and OpenSSL 1.0.1 and 1.0.1a set SSL_OP_NO_TLSv1_1 to 0x00000400L which would unfortunately mean any application compiled against OpenSSL 1.0.0 headers setting SSL_OP_ALL would also set SSL_OP_NO_TLSv1_1, unintentionally disablng TLS 1.1 also. Fix this by changing the value of SSL_OP_NO_TLSv1_1 to 0x10000000L Any application which was previously compiled against OpenSSL 1.0.1 or 1.0.1a headers and which cares about SSL_OP_NO_TLSv1_1 will need to be recompiled as a result. Letting be results in inability to disable specifically TLS 1.1 and in client context, in unlike event, limit maximum offered version to TLS 1.0 [see below]. [Steve Henson] *) In order to ensure interoperabilty SSL_OP_NO_protocolX does not disable just protocol X, but all protocols above X *if* there are protocols *below* X still enabled. In more practical terms it means that if application wants to disable TLS1.0 in favor of TLS1.1 and above, it's not sufficient to pass SSL_OP_NO_TLSv1, one has to pass SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3|SSL_OP_NO_SSLv2. This applies to client side. [Andy Polyakov] Changes between 1.0.1 and 1.0.1a [19 Apr 2012] *) Check for potentially exploitable overflows in asn1_d2i_read_bio BUF_mem_grow and BUF_mem_grow_clean. Refuse attempts to shrink buffer in CRYPTO_realloc_clean. Thanks to Tavis Ormandy, Google Security Team, for discovering this issue and to Adam Langley for fixing it. (CVE-2012-2110) [Adam Langley (Google), Tavis Ormandy, Google Security Team] *) Don't allow TLS 1.2 SHA-256 ciphersuites in TLS 1.0, 1.1 connections. [Adam Langley] *) Workarounds for some broken servers that "hang" if a client hello record length exceeds 255 bytes. 1. Do not use record version number > TLS 1.0 in initial client hello: some (but not all) hanging servers will now work. 2. If we set OPENSSL_MAX_TLS1_2_CIPHER_LENGTH this will truncate the number of ciphers sent in the client hello. This should be set to an even number, such as 50, for example by passing: -DOPENSSL_MAX_TLS1_2_CIPHER_LENGTH=50 to config or Configure. Most broken servers should now work. 3. If all else fails setting OPENSSL_NO_TLS1_2_CLIENT will disable TLS 1.2 client support entirely. [Steve Henson] *) Fix SEGV in Vector Permutation AES module observed in OpenSSH. [Andy Polyakov] Changes between 1.0.0h and 1.0.1 [14 Mar 2012] *) Add compatibility with old MDC2 signatures which use an ASN1 OCTET STRING form instead of a DigestInfo. [Steve Henson] *) The format used for MDC2 RSA signatures is inconsistent between EVP and the RSA_sign/RSA_verify functions. This was made more apparent when OpenSSL used RSA_sign/RSA_verify for some RSA signatures in particular those which went through EVP_PKEY_METHOD in 1.0.0 and later. Detect the correct format in RSA_verify so both forms transparently work. [Steve Henson] *) Some servers which support TLS 1.0 can choke if we initially indicate support for TLS 1.2 and later renegotiate using TLS 1.0 in the RSA encrypted premaster secret. As a workaround use the maximum pemitted client version in client hello, this should keep such servers happy and still work with previous versions of OpenSSL. [Steve Henson] *) Add support for TLS/DTLS heartbeats. [Robin Seggelmann ] *) Add support for SCTP. [Robin Seggelmann ] *) Improved PRNG seeding for VOS. [Paul Green ] *) Extensive assembler packs updates, most notably: - x86[_64]: AES-NI, PCLMULQDQ, RDRAND support; - x86[_64]: SSSE3 support (SHA1, vector-permutation AES); - x86_64: bit-sliced AES implementation; - ARM: NEON support, contemporary platforms optimizations; - s390x: z196 support; - *: GHASH and GF(2^m) multiplication implementations; [Andy Polyakov] *) Make TLS-SRP code conformant with RFC 5054 API cleanup (removal of unnecessary code) [Peter Sylvester ] *) Add TLS key material exporter from RFC 5705. [Eric Rescorla] *) Add DTLS-SRTP negotiation from RFC 5764. [Eric Rescorla] *) Add Next Protocol Negotiation, http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-00. Can be disabled with a no-npn flag to config or Configure. Code donated by Google. [Adam Langley and Ben Laurie] *) Add optional 64-bit optimized implementations of elliptic curves NIST-P224, NIST-P256, NIST-P521, with constant-time single point multiplication on typical inputs. Compiler support for the nonstandard type __uint128_t is required to use this (present in gcc 4.4 and later, for 64-bit builds). Code made available under Apache License version 2.0. Specify "enable-ec_nistp_64_gcc_128" on the Configure (or config) command line to include this in your build of OpenSSL, and run "make depend" (or "make update"). This enables the following EC_METHODs: EC_GFp_nistp224_method() EC_GFp_nistp256_method() EC_GFp_nistp521_method() EC_GROUP_new_by_curve_name() will automatically use these (while EC_GROUP_new_curve_GFp() currently prefers the more flexible implementations). [Emilia Käsper, Adam Langley, Bodo Moeller (Google)] *) Use type ossl_ssize_t instad of ssize_t which isn't available on all platforms. Move ssize_t definition from e_os.h to the public header file e_os2.h as it now appears in public header file cms.h [Steve Henson] *) New -sigopt option to the ca, req and x509 utilities. Additional signature parameters can be passed using this option and in particular PSS. [Steve Henson] *) Add RSA PSS signing function. This will generate and set the appropriate AlgorithmIdentifiers for PSS based on those in the corresponding EVP_MD_CTX structure. No application support yet. [Steve Henson] *) Support for companion algorithm specific ASN1 signing routines. New function ASN1_item_sign_ctx() signs a pre-initialised EVP_MD_CTX structure and sets AlgorithmIdentifiers based on the appropriate parameters. [Steve Henson] *) Add new algorithm specific ASN1 verification initialisation function to EVP_PKEY_ASN1_METHOD: this is not in EVP_PKEY_METHOD since the ASN1 handling will be the same no matter what EVP_PKEY_METHOD is used. Add a PSS handler to support verification of PSS signatures: checked against a number of sample certificates. [Steve Henson] *) Add signature printing for PSS. Add PSS OIDs. [Steve Henson, Martin Kaiser ] *) Add algorithm specific signature printing. An individual ASN1 method can now print out signatures instead of the standard hex dump. More complex signatures (e.g. PSS) can print out more meaningful information. Include DSA version that prints out the signature parameters r, s. [Steve Henson] *) Password based recipient info support for CMS library: implementing RFC3211. [Steve Henson] *) Split password based encryption into PBES2 and PBKDF2 functions. This neatly separates the code into cipher and PBE sections and is required for some algorithms that split PBES2 into separate pieces (such as password based CMS). [Steve Henson] *) Session-handling fixes: - Fix handling of connections that are resuming with a session ID, but also support Session Tickets. - Fix a bug that suppressed issuing of a new ticket if the client presented a ticket with an expired session. - Try to set the ticket lifetime hint to something reasonable. - Make tickets shorter by excluding irrelevant information. - On the client side, don't ignore renewed tickets. [Adam Langley, Bodo Moeller (Google)] *) Fix PSK session representation. [Bodo Moeller] *) Add RC4-MD5 and AESNI-SHA1 "stitched" implementations. This work was sponsored by Intel. [Andy Polyakov] *) Add GCM support to TLS library. Some custom code is needed to split the IV between the fixed (from PRF) and explicit (from TLS record) portions. This adds all GCM ciphersuites supported by RFC5288 and RFC5289. Generalise some AES* cipherstrings to inlclude GCM and add a special AESGCM string for GCM only. [Steve Henson] *) Expand range of ctrls for AES GCM. Permit setting invocation field on decrypt and retrieval of invocation field only on encrypt. [Steve Henson] *) Add HMAC ECC ciphersuites from RFC5289. Include SHA384 PRF support. As required by RFC5289 these ciphersuites cannot be used if for versions of TLS earlier than 1.2. [Steve Henson] *) For FIPS capable OpenSSL interpret a NULL default public key method as unset and return the appopriate default but do *not* set the default. This means we can return the appopriate method in applications that swicth between FIPS and non-FIPS modes. [Steve Henson] *) Redirect HMAC and CMAC operations to FIPS module in FIPS mode. If an ENGINE is used then we cannot handle that in the FIPS module so we keep original code iff non-FIPS operations are allowed. [Steve Henson] *) Add -attime option to openssl utilities. [Peter Eckersley , Ben Laurie and Steve Henson] *) Redirect DSA and DH operations to FIPS module in FIPS mode. [Steve Henson] *) Redirect ECDSA and ECDH operations to FIPS module in FIPS mode. Also use FIPS EC methods unconditionally for now. [Steve Henson] *) New build option no-ec2m to disable characteristic 2 code. [Steve Henson] *) Backport libcrypto audit of return value checking from 1.1.0-dev; not all cases can be covered as some introduce binary incompatibilities. [Steve Henson] *) Redirect RSA operations to FIPS module including keygen, encrypt, decrypt, sign and verify. Block use of non FIPS RSA methods. [Steve Henson] *) Add similar low level API blocking to ciphers. [Steve Henson] *) Low level digest APIs are not approved in FIPS mode: any attempt to use these will cause a fatal error. Applications that *really* want to use them can use the private_* version instead. [Steve Henson] *) Redirect cipher operations to FIPS module for FIPS builds. [Steve Henson] *) Redirect digest operations to FIPS module for FIPS builds. [Steve Henson] *) Update build system to add "fips" flag which will link in fipscanister.o for static and shared library builds embedding a signature if needed. [Steve Henson] *) Output TLS supported curves in preference order instead of numerical order. This is currently hardcoded for the highest order curves first. This should be configurable so applications can judge speed vs strength. [Steve Henson] *) Add TLS v1.2 server support for client authentication. [Steve Henson] *) Add support for FIPS mode in ssl library: disable SSLv3, non-FIPS ciphers and enable MD5. [Steve Henson] *) Functions FIPS_mode_set() and FIPS_mode() which call the underlying FIPS modules versions. [Steve Henson] *) Add TLS v1.2 client side support for client authentication. Keep cache of handshake records longer as we don't know the hash algorithm to use until after the certificate request message is received. [Steve Henson] *) Initial TLS v1.2 client support. Add a default signature algorithms extension including all the algorithms we support. Parse new signature format in client key exchange. Relax some ECC signing restrictions for TLS v1.2 as indicated in RFC5246. [Steve Henson] *) Add server support for TLS v1.2 signature algorithms extension. Switch to new signature format when needed using client digest preference. All server ciphersuites should now work correctly in TLS v1.2. No client support yet and no support for client certificates. [Steve Henson] *) Initial TLS v1.2 support. Add new SHA256 digest to ssl code, switch to SHA256 for PRF when using TLS v1.2 and later. Add new SHA256 based ciphersuites. At present only RSA key exchange ciphersuites work with TLS v1.2. Add new option for TLS v1.2 replacing the old and obsolete SSL_OP_PKCS1_CHECK flags with SSL_OP_NO_TLSv1_2. New TLSv1.2 methods and version checking. [Steve Henson] *) New option OPENSSL_NO_SSL_INTERN. If an application can be compiled with this defined it will not be affected by any changes to ssl internal structures. Add several utility functions to allow openssl application to work with OPENSSL_NO_SSL_INTERN defined. [Steve Henson] *) Add SRP support. [Tom Wu and Ben Laurie] *) Add functions to copy EVP_PKEY_METHOD and retrieve flags and id. [Steve Henson] *) Permit abbreviated handshakes when renegotiating using the function SSL_renegotiate_abbreviated(). [Robin Seggelmann ] *) Add call to ENGINE_register_all_complete() to ENGINE_load_builtin_engines(), so some implementations get used automatically instead of needing explicit application support. [Steve Henson] *) Add support for TLS key exporter as described in RFC5705. [Robin Seggelmann , Steve Henson] *) Initial TLSv1.1 support. Since TLSv1.1 is very similar to TLS v1.0 only a few changes are required: Add SSL_OP_NO_TLSv1_1 flag. Add TLSv1_1 methods. Update version checking logic to handle version 1.1. Add explicit IV handling (ported from DTLS code). Add command line options to s_client/s_server. [Steve Henson] Changes between 1.0.0g and 1.0.0h [12 Mar 2012] *) Fix MMA (Bleichenbacher's attack on PKCS #1 v1.5 RSA padding) weakness in CMS and PKCS7 code. When RSA decryption fails use a random key for content decryption and always return the same error. Note: this attack needs on average 2^20 messages so it only affects automated senders. The old behaviour can be reenabled in the CMS code by setting the CMS_DEBUG_DECRYPT flag: this is useful for debugging and testing where an MMA defence is not necessary. Thanks to Ivan Nestlerode for discovering this issue. (CVE-2012-0884) [Steve Henson] *) Fix CVE-2011-4619: make sure we really are receiving a client hello before rejecting multiple SGC restarts. Thanks to Ivan Nestlerode for discovering this bug. [Steve Henson] Changes between 1.0.0f and 1.0.0g [18 Jan 2012] *) Fix for DTLS DoS issue introduced by fix for CVE-2011-4109. Thanks to Antonio Martin, Enterprise Secure Access Research and Development, Cisco Systems, Inc. for discovering this bug and preparing a fix. (CVE-2012-0050) [Antonio Martin] Changes between 1.0.0e and 1.0.0f [4 Jan 2012] *) Nadhem Alfardan and Kenny Paterson have discovered an extension of the Vaudenay padding oracle attack on CBC mode encryption which enables an efficient plaintext recovery attack against the OpenSSL implementation of DTLS. Their attack exploits timing differences arising during decryption processing. A research paper describing this attack can be found at: http://www.isg.rhul.ac.uk/~kp/dtls.pdf Thanks go to Nadhem Alfardan and Kenny Paterson of the Information Security Group at Royal Holloway, University of London (www.isg.rhul.ac.uk) for discovering this flaw and to Robin Seggelmann and Michael Tuexen for preparing the fix. (CVE-2011-4108) [Robin Seggelmann, Michael Tuexen] *) Clear bytes used for block padding of SSL 3.0 records. (CVE-2011-4576) [Adam Langley (Google)] *) Only allow one SGC handshake restart for SSL/TLS. Thanks to George Kadianakis for discovering this issue and Adam Langley for preparing the fix. (CVE-2011-4619) [Adam Langley (Google)] *) Check parameters are not NULL in GOST ENGINE. (CVE-2012-0027) [Andrey Kulikov ] *) Prevent malformed RFC3779 data triggering an assertion failure. Thanks to Andrew Chi, BBN Technologies, for discovering the flaw and Rob Austein for fixing it. (CVE-2011-4577) [Rob Austein ] *) Improved PRNG seeding for VOS. [Paul Green ] *) Fix ssl_ciph.c set-up race. [Adam Langley (Google)] *) Fix spurious failures in ecdsatest.c. [Emilia Käsper (Google)] *) Fix the BIO_f_buffer() implementation (which was mixing different interpretations of the '..._len' fields). [Adam Langley (Google)] *) Fix handling of BN_BLINDING: now BN_BLINDING_invert_ex (rather than BN_BLINDING_invert_ex) calls BN_BLINDING_update, ensuring that concurrent threads won't reuse the same blinding coefficients. This also avoids the need to obtain the CRYPTO_LOCK_RSA_BLINDING lock to call BN_BLINDING_invert_ex, and avoids one use of BN_BLINDING_update for each BN_BLINDING structure (previously, the last update always remained unused). [Emilia Käsper (Google)] *) In ssl3_clear, preserve s3->init_extra along with s3->rbuf. [Bob Buckholz (Google)] Changes between 1.0.0d and 1.0.0e [6 Sep 2011] *) Fix bug where CRLs with nextUpdate in the past are sometimes accepted by initialising X509_STORE_CTX properly. (CVE-2011-3207) [Kaspar Brand ] *) Fix SSL memory handling for (EC)DH ciphersuites, in particular for multi-threaded use of ECDH. (CVE-2011-3210) [Adam Langley (Google)] *) Fix x509_name_ex_d2i memory leak on bad inputs. [Bodo Moeller] *) Remove hard coded ecdsaWithSHA1 signature tests in ssl code and check signature public key algorithm by using OID xref utilities instead. Before this you could only use some ECC ciphersuites with SHA1 only. [Steve Henson] *) Add protection against ECDSA timing attacks as mentioned in the paper by Billy Bob Brumley and Nicola Tuveri, see: http://eprint.iacr.org/2011/232.pdf [Billy Bob Brumley and Nicola Tuveri] Changes between 1.0.0c and 1.0.0d [8 Feb 2011] *) Fix parsing of OCSP stapling ClientHello extension. CVE-2011-0014 [Neel Mehta, Adam Langley, Bodo Moeller (Google)] *) Fix bug in string printing code: if *any* escaping is enabled we must escape the escape character (backslash) or the resulting string is ambiguous. [Steve Henson] Changes between 1.0.0b and 1.0.0c [2 Dec 2010] *) Disable code workaround for ancient and obsolete Netscape browsers and servers: an attacker can use it in a ciphersuite downgrade attack. Thanks to Martin Rex for discovering this bug. CVE-2010-4180 [Steve Henson] *) Fixed J-PAKE implementation error, originally discovered by Sebastien Martini, further info and confirmation from Stefan Arentz and Feng Hao. Note that this fix is a security fix. CVE-2010-4252 [Ben Laurie] Changes between 1.0.0a and 1.0.0b [16 Nov 2010] *) Fix extension code to avoid race conditions which can result in a buffer overrun vulnerability: resumed sessions must not be modified as they can be shared by multiple threads. CVE-2010-3864 [Steve Henson] *) Fix WIN32 build system to correctly link an ENGINE directory into a DLL. [Steve Henson] Changes between 1.0.0 and 1.0.0a [01 Jun 2010] *) Check return value of int_rsa_verify in pkey_rsa_verifyrecover (CVE-2010-1633) [Steve Henson, Peter-Michael Hager ] Changes between 0.9.8n and 1.0.0 [29 Mar 2010] *) Add "missing" function EVP_CIPHER_CTX_copy(). This copies a cipher context. The operation can be customised via the ctrl mechanism in case ENGINEs want to include additional functionality. [Steve Henson] *) Tolerate yet another broken PKCS#8 key format: private key value negative. [Steve Henson] *) Add new -subject_hash_old and -issuer_hash_old options to x509 utility to output hashes compatible with older versions of OpenSSL. [Willy Weisz ] *) Fix compression algorithm handling: if resuming a session use the compression algorithm of the resumed session instead of determining it from client hello again. Don't allow server to change algorithm. [Steve Henson] *) Add load_crls() function to apps tidying load_certs() too. Add option to verify utility to allow additional CRLs to be included. [Steve Henson] *) Update OCSP request code to permit adding custom headers to the request: some responders need this. [Steve Henson] *) The function EVP_PKEY_sign() returns <=0 on error: check return code correctly. [Julia Lawall ] *) Update verify callback code in apps/s_cb.c and apps/verify.c, it needlessly dereferenced structures, used obsolete functions and didn't handle all updated verify codes correctly. [Steve Henson] *) Disable MD2 in the default configuration. [Steve Henson] *) In BIO_pop() and BIO_push() use the ctrl argument (which was NULL) to indicate the initial BIO being pushed or popped. This makes it possible to determine whether the BIO is the one explicitly called or as a result of the ctrl being passed down the chain. Fix BIO_pop() and SSL BIOs so it handles reference counts correctly and doesn't zero out the I/O bio when it is not being explicitly popped. WARNING: applications which included workarounds for the old buggy behaviour will need to be modified or they could free up already freed BIOs. [Steve Henson] *) Extend the uni2asc/asc2uni => OPENSSL_uni2asc/OPENSSL_asc2uni renaming to all platforms (within the 0.9.8 branch, this was done conditionally on Netware platforms to avoid a name clash). [Guenter ] *) Add ECDHE and PSK support to DTLS. [Michael Tuexen ] *) Add CHECKED_STACK_OF macro to safestack.h, otherwise safestack can't be used on C++. [Steve Henson] *) Add "missing" function EVP_MD_flags() (without this the only way to retrieve a digest flags is by accessing the structure directly. Update EVP_MD_do_all*() and EVP_CIPHER_do_all*() to include the name a digest or cipher is registered as in the "from" argument. Print out all registered digests in the dgst usage message instead of manually attempting to work them out. [Steve Henson] *) If no SSLv2 ciphers are used don't use an SSLv2 compatible client hello: this allows the use of compression and extensions. Change default cipher string to remove SSLv2 ciphersuites. This effectively avoids ancient SSLv2 by default unless an application cipher string requests it. [Steve Henson] *) Alter match criteria in PKCS12_parse(). It used to try to use local key ids to find matching certificates and keys but some PKCS#12 files don't follow the (somewhat unwritten) rules and this strategy fails. Now just gather all certificates together and the first private key then look for the first certificate that matches the key. [Steve Henson] *) Support use of registered digest and cipher names for dgst and cipher commands instead of having to add each one as a special case. So now you can do: openssl sha256 foo as well as: openssl dgst -sha256 foo and this works for ENGINE based algorithms too. [Steve Henson] *) Update Gost ENGINE to support parameter files. [Victor B. Wagner ] *) Support GeneralizedTime in ca utility. [Oliver Martin , Steve Henson] *) Enhance the hash format used for certificate directory links. The new form uses the canonical encoding (meaning equivalent names will work even if they aren't identical) and uses SHA1 instead of MD5. This form is incompatible with the older format and as a result c_rehash should be used to rebuild symbolic links. [Steve Henson] *) Make PKCS#8 the default write format for private keys, replacing the traditional format. This form is standardised, more secure and doesn't include an implicit MD5 dependency. [Steve Henson] *) Add a $gcc_devteam_warn option to Configure. The idea is that any code committed to OpenSSL should pass this lot as a minimum. [Steve Henson] *) Add session ticket override functionality for use by EAP-FAST. [Jouni Malinen ] *) Modify HMAC functions to return a value. Since these can be implemented in an ENGINE errors can occur. [Steve Henson] *) Type-checked OBJ_bsearch_ex. [Ben Laurie] *) Type-checked OBJ_bsearch. Also some constification necessitated by type-checking. Still to come: TXT_DB, bsearch(?), OBJ_bsearch_ex, qsort, CRYPTO_EX_DATA, ASN1_VALUE, ASN1_STRING, CONF_VALUE. [Ben Laurie] *) New function OPENSSL_gmtime_adj() to add a specific number of days and seconds to a tm structure directly, instead of going through OS specific date routines. This avoids any issues with OS routines such as the year 2038 bug. New *_adj() functions for ASN1 time structures and X509_time_adj_ex() to cover the extended range. The existing X509_time_adj() is still usable and will no longer have any date issues. [Steve Henson] *) Delta CRL support. New use deltas option which will attempt to locate and search any appropriate delta CRLs available. This work was sponsored by Google. [Steve Henson] *) Support for CRLs partitioned by reason code. Reorganise CRL processing code and add additional score elements. Validate alternate CRL paths as part of the CRL checking and indicate a new error "CRL path validation error" in this case. Applications wanting additional details can use the verify callback and check the new "parent" field. If this is not NULL CRL path validation is taking place. Existing applications wont see this because it requires extended CRL support which is off by default. This work was sponsored by Google. [Steve Henson] *) Support for freshest CRL extension. This work was sponsored by Google. [Steve Henson] *) Initial indirect CRL support. Currently only supported in the CRLs passed directly and not via lookup. Process certificate issuer CRL entry extension and lookup CRL entries by bother issuer name and serial number. Check and process CRL issuer entry in IDP extension. This work was sponsored by Google. [Steve Henson] *) Add support for distinct certificate and CRL paths. The CRL issuer certificate is validated separately in this case. Only enabled if an extended CRL support flag is set: this flag will enable additional CRL functionality in future. This work was sponsored by Google. [Steve Henson] *) Add support for policy mappings extension. This work was sponsored by Google. [Steve Henson] *) Fixes to pathlength constraint, self issued certificate handling, policy processing to align with RFC3280 and PKITS tests. This work was sponsored by Google. [Steve Henson] *) Support for name constraints certificate extension. DN, email, DNS and URI types are currently supported. This work was sponsored by Google. [Steve Henson] *) To cater for systems that provide a pointer-based thread ID rather than numeric, deprecate the current numeric thread ID mechanism and replace it with a structure and associated callback type. This mechanism allows a numeric "hash" to be extracted from a thread ID in either case, and on platforms where pointers are larger than 'long', mixing is done to help ensure the numeric 'hash' is usable even if it can't be guaranteed unique. The default mechanism is to use "&errno" as a pointer-based thread ID to distinguish between threads. Applications that want to provide their own thread IDs should now use CRYPTO_THREADID_set_callback() to register a callback that will call either CRYPTO_THREADID_set_numeric() or CRYPTO_THREADID_set_pointer(). Note that ERR_remove_state() is now deprecated, because it is tied to the assumption that thread IDs are numeric. ERR_remove_state(0) to free the current thread's error state should be replaced by ERR_remove_thread_state(NULL). (This new approach replaces the functions CRYPTO_set_idptr_callback(), CRYPTO_get_idptr_callback(), and CRYPTO_thread_idptr() that existed in OpenSSL 0.9.9-dev between June 2006 and August 2008. Also, if an application was previously providing a numeric thread callback that was inappropriate for distinguishing threads, then uniqueness might have been obtained with &errno that happened immediately in the intermediate development versions of OpenSSL; this is no longer the case, the numeric thread callback will now override the automatic use of &errno.) [Geoff Thorpe, with help from Bodo Moeller] *) Initial support for different CRL issuing certificates. This covers a simple case where the self issued certificates in the chain exist and the real CRL issuer is higher in the existing chain. This work was sponsored by Google. [Steve Henson] *) Removed effectively defunct crypto/store from the build. [Ben Laurie] *) Revamp of STACK to provide stronger type-checking. Still to come: TXT_DB, bsearch(?), OBJ_bsearch, qsort, CRYPTO_EX_DATA, ASN1_VALUE, ASN1_STRING, CONF_VALUE. [Ben Laurie] *) Add a new SSL_MODE_RELEASE_BUFFERS mode flag to release unused buffer RAM on SSL connections. This option can save about 34k per idle SSL. [Nick Mathewson] *) Revamp of LHASH to provide stronger type-checking. Still to come: STACK, TXT_DB, bsearch, qsort. [Ben Laurie] *) Initial support for Cryptographic Message Syntax (aka CMS) based on RFC3850, RFC3851 and RFC3852. New cms directory and cms utility, support for data, signedData, compressedData, digestedData and encryptedData, envelopedData types included. Scripts to check against RFC4134 examples draft and interop and consistency checks of many content types and variants. [Steve Henson] *) Add options to enc utility to support use of zlib compression BIO. [Steve Henson] *) Extend mk1mf to support importing of options and assembly language files from Configure script, currently only included in VC-WIN32. The assembly language rules can now optionally generate the source files from the associated perl scripts. [Steve Henson] *) Implement remaining functionality needed to support GOST ciphersuites. Interop testing has been performed using CryptoPro implementations. [Victor B. Wagner ] *) s390x assembler pack. [Andy Polyakov] *) ARMv4 assembler pack. ARMv4 refers to v4 and later ISA, not CPU "family." [Andy Polyakov] *) Implement Opaque PRF Input TLS extension as specified in draft-rescorla-tls-opaque-prf-input-00.txt. Since this is not an official specification yet and no extension type assignment by IANA exists, this extension (for now) will have to be explicitly enabled when building OpenSSL by providing the extension number to use. For example, specify an option -DTLSEXT_TYPE_opaque_prf_input=0x9527 to the "config" or "Configure" script to enable the extension, assuming extension number 0x9527 (which is a completely arbitrary and unofficial assignment based on the MD5 hash of the Internet Draft). Note that by doing so, you potentially lose interoperability with other TLS implementations since these might be using the same extension number for other purposes. SSL_set_tlsext_opaque_prf_input(ssl, src, len) is used to set the opaque PRF input value to use in the handshake. This will create an interal copy of the length-'len' string at 'src', and will return non-zero for success. To get more control and flexibility, provide a callback function by using SSL_CTX_set_tlsext_opaque_prf_input_callback(ctx, cb) SSL_CTX_set_tlsext_opaque_prf_input_callback_arg(ctx, arg) where int (*cb)(SSL *, void *peerinput, size_t len, void *arg); void *arg; Callback function 'cb' will be called in handshakes, and is expected to use SSL_set_tlsext_opaque_prf_input() as appropriate. Argument 'arg' is for application purposes (the value as given to SSL_CTX_set_tlsext_opaque_prf_input_callback_arg() will directly be provided to the callback function). The callback function has to return non-zero to report success: usually 1 to use opaque PRF input just if possible, or 2 to enforce use of the opaque PRF input. In the latter case, the library will abort the handshake if opaque PRF input is not successfully negotiated. Arguments 'peerinput' and 'len' given to the callback function will always be NULL and 0 in the case of a client. A server will see the client's opaque PRF input through these variables if available (NULL and 0 otherwise). Note that if the server provides an opaque PRF input, the length must be the same as the length of the client's opaque PRF input. Note that the callback function will only be called when creating a new session (session resumption can resume whatever was previously negotiated), and will not be called in SSL 2.0 handshakes; thus, SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2) or SSL_set_options(ssl, SSL_OP_NO_SSLv2) is especially recommended for applications that need to enforce opaque PRF input. [Bodo Moeller] *) Update ssl code to support digests other than SHA1+MD5 for handshake MAC. [Victor B. Wagner ] *) Add RFC4507 support to OpenSSL. This includes the corrections in RFC4507bis. The encrypted ticket format is an encrypted encoded SSL_SESSION structure, that way new session features are automatically supported. If a client application caches session in an SSL_SESSION structure support is transparent because tickets are now stored in the encoded SSL_SESSION. The SSL_CTX structure automatically generates keys for ticket protection in servers so again support should be possible with no application modification. If a client or server wishes to disable RFC4507 support then the option SSL_OP_NO_TICKET can be set. Add a TLS extension debugging callback to allow the contents of any client or server extensions to be examined. This work was sponsored by Google. [Steve Henson] *) Final changes to avoid use of pointer pointer casts in OpenSSL. OpenSSL should now compile cleanly on gcc 4.2 [Peter Hartley , Steve Henson] *) Update SSL library to use new EVP_PKEY MAC API. Include generic MAC support including streaming MAC support: this is required for GOST ciphersuite support. [Victor B. Wagner , Steve Henson] *) Add option -stream to use PKCS#7 streaming in smime utility. New function i2d_PKCS7_bio_stream() and PEM_write_PKCS7_bio_stream() to output in BER and PEM format. [Steve Henson] *) Experimental support for use of HMAC via EVP_PKEY interface. This allows HMAC to be handled via the EVP_DigestSign*() interface. The EVP_PKEY "key" in this case is the HMAC key, potentially allowing ENGINE support for HMAC keys which are unextractable. New -mac and -macopt options to dgst utility. [Steve Henson] *) New option -sigopt to dgst utility. Update dgst to use EVP_Digest{Sign,Verify}*. These two changes make it possible to use alternative signing paramaters such as X9.31 or PSS in the dgst utility. [Steve Henson] *) Change ssl_cipher_apply_rule(), the internal function that does the work each time a ciphersuite string requests enabling ("foo+bar"), moving ("+foo+bar"), disabling ("-foo+bar", or removing ("!foo+bar") a class of ciphersuites: Now it maintains the order of disabled ciphersuites such that those ciphersuites that most recently went from enabled to disabled not only stay in order with respect to each other, but also have higher priority than other disabled ciphersuites the next time ciphersuites are enabled again. This means that you can now say, e.g., "PSK:-PSK:HIGH" to enable the same ciphersuites as with "HIGH" alone, but in a specific order where the PSK ciphersuites come first (since they are the most recently disabled ciphersuites when "HIGH" is parsed). Also, change ssl_create_cipher_list() (using this new funcionality) such that between otherwise identical cihpersuites, ephemeral ECDH is preferred over ephemeral DH in the default order. [Bodo Moeller] *) Change ssl_create_cipher_list() so that it automatically arranges the ciphersuites in reasonable order before starting to process the rule string. Thus, the definition for "DEFAULT" (SSL_DEFAULT_CIPHER_LIST) now is just "ALL:!aNULL:!eNULL", but remains equivalent to "AES:ALL:!aNULL:!eNULL:+aECDH:+kRSA:+RC4:@STRENGTH". This makes it much easier to arrive at a reasonable default order in applications for which anonymous ciphers are OK (meaning that you can't actually use DEFAULT). [Bodo Moeller; suggested by Victor Duchovni] *) Split the SSL/TLS algorithm mask (as used for ciphersuite string processing) into multiple integers instead of setting "SSL_MKEY_MASK" bits, "SSL_AUTH_MASK" bits, "SSL_ENC_MASK", "SSL_MAC_MASK", and "SSL_SSL_MASK" bits all in a single integer. (These masks as well as the individual bit definitions are hidden away into the non-exported interface ssl/ssl_locl.h, so this change to the definition of the SSL_CIPHER structure shouldn't affect applications.) This give us more bits for each of these categories, so there is no longer a need to coagulate AES128 and AES256 into a single algorithm bit, and to coagulate Camellia128 and Camellia256 into a single algorithm bit, which has led to all kinds of kludges. Thus, among other things, the kludge introduced in 0.9.7m and 0.9.8e for masking out AES256 independently of AES128 or masking out Camellia256 independently of AES256 is not needed here in 0.9.9. With the change, we also introduce new ciphersuite aliases that so far were missing: "AES128", "AES256", "CAMELLIA128", and "CAMELLIA256". [Bodo Moeller] *) Add support for dsa-with-SHA224 and dsa-with-SHA256. Use the leftmost N bytes of the signature input if the input is larger than the prime q (with N being the size in bytes of q). [Nils Larsch] *) Very *very* experimental PKCS#7 streaming encoder support. Nothing uses it yet and it is largely untested. [Steve Henson] *) Add support for the ecdsa-with-SHA224/256/384/512 signature types. [Nils Larsch] *) Initial incomplete changes to avoid need for function casts in OpenSSL some compilers (gcc 4.2 and later) reject their use. Safestack is reimplemented. Update ASN1 to avoid use of legacy functions. [Steve Henson] *) Win32/64 targets are linked with Winsock2. [Andy Polyakov] *) Add an X509_CRL_METHOD structure to allow CRL processing to be redirected to external functions. This can be used to increase CRL handling efficiency especially when CRLs are very large by (for example) storing the CRL revoked certificates in a database. [Steve Henson] *) Overhaul of by_dir code. Add support for dynamic loading of CRLs so new CRLs added to a directory can be used. New command line option -verify_return_error to s_client and s_server. This causes real errors to be returned by the verify callback instead of carrying on no matter what. This reflects the way a "real world" verify callback would behave. [Steve Henson] *) GOST engine, supporting several GOST algorithms and public key formats. Kindly donated by Cryptocom. [Cryptocom] *) Partial support for Issuing Distribution Point CRL extension. CRLs partitioned by DP are handled but no indirect CRL or reason partitioning (yet). Complete overhaul of CRL handling: now the most suitable CRL is selected via a scoring technique which handles IDP and AKID in CRLs. [Steve Henson] *) New X509_STORE_CTX callbacks lookup_crls() and lookup_certs() which will ultimately be used for all verify operations: this will remove the X509_STORE dependency on certificate verification and allow alternative lookup methods. X509_STORE based implementations of these two callbacks. [Steve Henson] *) Allow multiple CRLs to exist in an X509_STORE with matching issuer names. Modify get_crl() to find a valid (unexpired) CRL if possible. [Steve Henson] *) New function X509_CRL_match() to check if two CRLs are identical. Normally this would be called X509_CRL_cmp() but that name is already used by a function that just compares CRL issuer names. Cache several CRL extensions in X509_CRL structure and cache CRLDP in X509. [Steve Henson] *) Store a "canonical" representation of X509_NAME structure (ASN1 Name) this maps equivalent X509_NAME structures into a consistent structure. Name comparison can then be performed rapidly using memcmp(). [Steve Henson] *) Non-blocking OCSP request processing. Add -timeout option to ocsp utility. [Steve Henson] *) Allow digests to supply their own micalg string for S/MIME type using the ctrl EVP_MD_CTRL_MICALG. [Steve Henson] *) During PKCS7 signing pass the PKCS7 SignerInfo structure to the EVP_PKEY_METHOD before and after signing via the EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure before and/or after signing if necessary. [Steve Henson] *) New function OBJ_add_sigid() to allow application defined signature OIDs to be added to OpenSSLs internal tables. New function OBJ_sigid_free() to free up any added signature OIDs. [Steve Henson] *) New functions EVP_CIPHER_do_all(), EVP_CIPHER_do_all_sorted(), EVP_MD_do_all() and EVP_MD_do_all_sorted() to enumerate internal digest and cipher tables. New options added to openssl utility: list-message-digest-algorithms and list-cipher-algorithms. [Steve Henson] *) Change the array representation of binary polynomials: the list of degrees of non-zero coefficients is now terminated with -1. Previously it was terminated with 0, which was also part of the value; thus, the array representation was not applicable to polynomials where t^0 has coefficient zero. This change makes the array representation useful in a more general context. [Douglas Stebila] *) Various modifications and fixes to SSL/TLS cipher string handling. For ECC, the code now distinguishes between fixed ECDH with RSA certificates on the one hand and with ECDSA certificates on the other hand, since these are separate ciphersuites. The unused code for Fortezza ciphersuites has been removed. For consistency with EDH, ephemeral ECDH is now called "EECDH" (not "ECDHE"). For consistency with the code for DH certificates, use of ECDH certificates is now considered ECDH authentication, not RSA or ECDSA authentication (the latter is merely the CA's signing algorithm and not actively used in the protocol). The temporary ciphersuite alias "ECCdraft" is no longer available, and ECC ciphersuites are no longer excluded from "ALL" and "DEFAULT". The following aliases now exist for RFC 4492 ciphersuites, most of these by analogy with the DH case: kECDHr - ECDH cert, signed with RSA kECDHe - ECDH cert, signed with ECDSA kECDH - ECDH cert (signed with either RSA or ECDSA) kEECDH - ephemeral ECDH ECDH - ECDH cert or ephemeral ECDH aECDH - ECDH cert aECDSA - ECDSA cert ECDSA - ECDSA cert AECDH - anonymous ECDH EECDH - non-anonymous ephemeral ECDH (equivalent to "kEECDH:-AECDH") [Bodo Moeller] *) Add additional S/MIME capabilities for AES and GOST ciphers if supported. Use correct micalg parameters depending on digest(s) in signed message. [Steve Henson] *) Add engine support for EVP_PKEY_ASN1_METHOD. Add functions to process an ENGINE asn1 method. Support ENGINE lookups in the ASN1 code. [Steve Henson] *) Initial engine support for EVP_PKEY_METHOD. New functions to permit an engine to register a method. Add ENGINE lookups for methods and functional reference processing. [Steve Henson] *) New functions EVP_Digest{Sign,Verify)*. These are enchance versions of EVP_{Sign,Verify}* which allow an application to customise the signature process. [Steve Henson] *) New -resign option to smime utility. This adds one or more signers to an existing PKCS#7 signedData structure. Also -md option to use an alternative message digest algorithm for signing. [Steve Henson] *) Tidy up PKCS#7 routines and add new functions to make it easier to create PKCS7 structures containing multiple signers. Update smime application to support multiple signers. [Steve Henson] *) New -macalg option to pkcs12 utility to allow setting of an alternative digest MAC. [Steve Henson] *) Initial support for PKCS#5 v2.0 PRFs other than default SHA1 HMAC. Reorganize PBE internals to lookup from a static table using NIDs, add support for HMAC PBE OID translation. Add a EVP_CIPHER ctrl: EVP_CTRL_PBE_PRF_NID this allows a cipher to specify an alternative PRF which will be automatically used with PBES2. [Steve Henson] *) Replace the algorithm specific calls to generate keys in "req" with the new API. [Steve Henson] *) Update PKCS#7 enveloped data routines to use new API. This is now supported by any public key method supporting the encrypt operation. A ctrl is added to allow the public key algorithm to examine or modify the PKCS#7 RecipientInfo structure if it needs to: for RSA this is a no op. [Steve Henson] *) Add a ctrl to asn1 method to allow a public key algorithm to express a default digest type to use. In most cases this will be SHA1 but some algorithms (such as GOST) need to specify an alternative digest. The return value indicates how strong the prefernce is 1 means optional and 2 is mandatory (that is it is the only supported type). Modify ASN1_item_sign() to accept a NULL digest argument to indicate it should use the default md. Update openssl utilities to use the default digest type for signing if it is not explicitly indicated. [Steve Henson] *) Use OID cross reference table in ASN1_sign() and ASN1_verify(). New EVP_MD flag EVP_MD_FLAG_PKEY_METHOD_SIGNATURE. This uses the relevant signing method from the key type. This effectively removes the link between digests and public key types. [Steve Henson] *) Add an OID cross reference table and utility functions. Its purpose is to translate between signature OIDs such as SHA1WithrsaEncryption and SHA1, rsaEncryption. This will allow some of the algorithm specific hackery needed to use the correct OID to be removed. [Steve Henson] *) Remove algorithm specific dependencies when setting PKCS7_SIGNER_INFO structures for PKCS7_sign(). They are now set up by the relevant public key ASN1 method. [Steve Henson] *) Add provisional EC pkey method with support for ECDSA and ECDH. [Steve Henson] *) Add support for key derivation (agreement) in the API, DH method and pkeyutl. [Steve Henson] *) Add DSA pkey method and DH pkey methods, extend DH ASN1 method to support public and private key formats. As a side effect these add additional command line functionality not previously available: DSA signatures can be generated and verified using pkeyutl and DH key support and generation in pkey, genpkey. [Steve Henson] *) BeOS support. [Oliver Tappe ] *) New make target "install_html_docs" installs HTML renditions of the manual pages. [Oliver Tappe ] *) New utility "genpkey" this is analagous to "genrsa" etc except it can generate keys for any algorithm. Extend and update EVP_PKEY_METHOD to support key and parameter generation and add initial key generation functionality for RSA. [Steve Henson] *) Add functions for main EVP_PKEY_method operations. The undocumented functions EVP_PKEY_{encrypt,decrypt} have been renamed to EVP_PKEY_{encrypt,decrypt}_old. [Steve Henson] *) Initial definitions for EVP_PKEY_METHOD. This will be a high level public key API, doesn't do much yet. [Steve Henson] *) New function EVP_PKEY_asn1_get0_info() to retrieve information about public key algorithms. New option to openssl utility: "list-public-key-algorithms" to print out info. [Steve Henson] *) Implement the Supported Elliptic Curves Extension for ECC ciphersuites from draft-ietf-tls-ecc-12.txt. [Douglas Stebila] *) Don't free up OIDs in OBJ_cleanup() if they are in use by EVP_MD or EVP_CIPHER structures to avoid later problems in EVP_cleanup(). [Steve Henson] *) New utilities pkey and pkeyparam. These are similar to algorithm specific utilities such as rsa, dsa, dsaparam etc except they process any key type. [Steve Henson] *) Transfer public key printing routines to EVP_PKEY_ASN1_METHOD. New functions EVP_PKEY_print_public(), EVP_PKEY_print_private(), EVP_PKEY_print_param() to print public key data from an EVP_PKEY structure. [Steve Henson] *) Initial support for pluggable public key ASN1. De-spaghettify the public key ASN1 handling. Move public and private key ASN1 handling to a new EVP_PKEY_ASN1_METHOD structure. Relocate algorithm specific handling to a single module within the relevant algorithm directory. Add functions to allow (near) opaque processing of public and private key structures. [Steve Henson] *) Implement the Supported Point Formats Extension for ECC ciphersuites from draft-ietf-tls-ecc-12.txt. [Douglas Stebila] *) Add initial support for RFC 4279 PSK TLS ciphersuites. Add members for the psk identity [hint] and the psk callback functions to the SSL_SESSION, SSL and SSL_CTX structure. New ciphersuites: PSK-RC4-SHA, PSK-3DES-EDE-CBC-SHA, PSK-AES128-CBC-SHA, PSK-AES256-CBC-SHA New functions: SSL_CTX_use_psk_identity_hint SSL_get_psk_identity_hint SSL_get_psk_identity SSL_use_psk_identity_hint [Mika Kousa and Pasi Eronen of Nokia Corporation] *) Add RFC 3161 compliant time stamp request creation, response generation and response verification functionality. [Zoltán Glózik , The OpenTSA Project] *) Add initial support for TLS extensions, specifically for the server_name extension so far. The SSL_SESSION, SSL_CTX, and SSL data structures now have new members for a host name. The SSL data structure has an additional member SSL_CTX *initial_ctx so that new sessions can be stored in that context to allow for session resumption, even after the SSL has been switched to a new SSL_CTX in reaction to a client's server_name extension. New functions (subject to change): SSL_get_servername() SSL_get_servername_type() SSL_set_SSL_CTX() New CTRL codes and macros (subject to change): SSL_CTRL_SET_TLSEXT_SERVERNAME_CB - SSL_CTX_set_tlsext_servername_callback() SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG - SSL_CTX_set_tlsext_servername_arg() SSL_CTRL_SET_TLSEXT_HOSTNAME - SSL_set_tlsext_host_name() openssl s_client has a new '-servername ...' option. openssl s_server has new options '-servername_host ...', '-cert2 ...', '-key2 ...', '-servername_fatal' (subject to change). This allows testing the HostName extension for a specific single host name ('-cert' and '-key' remain fallbacks for handshakes without HostName negotiation). If the unrecogninzed_name alert has to be sent, this by default is a warning; it becomes fatal with the '-servername_fatal' option. [Peter Sylvester, Remy Allais, Christophe Renou] *) Whirlpool hash implementation is added. [Andy Polyakov] *) BIGNUM code on 64-bit SPARCv9 targets is switched from bn(64,64) to bn(64,32). Because of instruction set limitations it doesn't have any negative impact on performance. This was done mostly in order to make it possible to share assembler modules, such as bn_mul_mont implementations, between 32- and 64-bit builds without hassle. [Andy Polyakov] *) Move code previously exiled into file crypto/ec/ec2_smpt.c to ec2_smpl.c, and no longer require the OPENSSL_EC_BIN_PT_COMP macro. [Bodo Moeller] *) New candidate for BIGNUM assembler implementation, bn_mul_mont, dedicated Montgomery multiplication procedure, is introduced. BN_MONT_CTX is modified to allow bn_mul_mont to reach for higher "64-bit" performance on certain 32-bit targets. [Andy Polyakov] *) New option SSL_OP_NO_COMP to disable use of compression selectively in SSL structures. New SSL ctrl to set maximum send fragment size. Save memory by seeting the I/O buffer sizes dynamically instead of using the maximum available value. [Steve Henson] *) New option -V for 'openssl ciphers'. This prints the ciphersuite code in addition to the text details. [Bodo Moeller] *) Very, very preliminary EXPERIMENTAL support for printing of general ASN1 structures. This currently produces rather ugly output and doesn't handle several customised structures at all. [Steve Henson] *) Integrated support for PVK file format and some related formats such as MS PUBLICKEYBLOB and PRIVATEKEYBLOB. Command line switches to support these in the 'rsa' and 'dsa' utilities. [Steve Henson] *) Support for PKCS#1 RSAPublicKey format on rsa utility command line. [Steve Henson] *) Remove the ancient ASN1_METHOD code. This was only ever used in one place for the (very old) "NETSCAPE" format certificates which are now handled using new ASN1 code equivalents. [Steve Henson] *) Let the TLSv1_method() etc. functions return a 'const' SSL_METHOD pointer and make the SSL_METHOD parameter in SSL_CTX_new, SSL_CTX_set_ssl_version and SSL_set_ssl_method 'const'. [Nils Larsch] *) Modify CRL distribution points extension code to print out previously unsupported fields. Enhance extension setting code to allow setting of all fields. [Steve Henson] *) Add print and set support for Issuing Distribution Point CRL extension. [Steve Henson] *) Change 'Configure' script to enable Camellia by default. [NTT] Changes between 0.9.8m and 0.9.8n [24 Mar 2010] *) When rejecting SSL/TLS records due to an incorrect version number, never update s->server with a new major version number. As of - OpenSSL 0.9.8m if 'short' is a 16-bit type, - OpenSSL 0.9.8f if 'short' is longer than 16 bits, the previous behavior could result in a read attempt at NULL when receiving specific incorrect SSL/TLS records once record payload protection is active. (CVE-2010-0740) [Bodo Moeller, Adam Langley ] *) Fix for CVE-2010-0433 where some kerberos enabled versions of OpenSSL could be crashed if the relevant tables were not present (e.g. chrooted). [Tomas Hoger ] Changes between 0.9.8l and 0.9.8m [25 Feb 2010] *) Always check bn_wexpend() return values for failure. (CVE-2009-3245) [Martin Olsson, Neel Mehta] *) Fix X509_STORE locking: Every 'objs' access requires a lock (to accommodate for stack sorting, always a write lock!). [Bodo Moeller] *) On some versions of WIN32 Heap32Next is very slow. This can cause excessive delays in the RAND_poll(): over a minute. As a workaround include a time check in the inner Heap32Next loop too. [Steve Henson] *) The code that handled flushing of data in SSL/TLS originally used the BIO_CTRL_INFO ctrl to see if any data was pending first. This caused the problem outlined in PR#1949. The fix suggested there however can trigger problems with buggy BIO_CTRL_WPENDING (e.g. some versions of Apache). So instead simplify the code to flush unconditionally. This should be fine since flushing with no data to flush is a no op. [Steve Henson] *) Handle TLS versions 2.0 and later properly and correctly use the highest version of TLS/SSL supported. Although TLS >= 2.0 is some way off ancient servers have a habit of sticking around for a while... [Steve Henson] *) Modify compression code so it frees up structures without using the ex_data callbacks. This works around a problem where some applications call CRYPTO_cleanup_all_ex_data() before application exit (e.g. when restarting) then use compression (e.g. SSL with compression) later. This results in significant per-connection memory leaks and has caused some security issues including CVE-2008-1678 and CVE-2009-4355. [Steve Henson] *) Constify crypto/cast (i.e., ): a CAST_KEY doesn't change when encrypting or decrypting. [Bodo Moeller] *) Add option SSL_OP_LEGACY_SERVER_CONNECT which will allow clients to connect and renegotiate with servers which do not support RI. Until RI is more widely deployed this option is enabled by default. [Steve Henson] *) Add "missing" ssl ctrls to clear options and mode. [Steve Henson] *) If client attempts to renegotiate and doesn't support RI respond with a no_renegotiation alert as required by RFC5746. Some renegotiating TLS clients will continue a connection gracefully when they receive the alert. Unfortunately OpenSSL mishandled this alert and would hang waiting for a server hello which it will never receive. Now we treat a received no_renegotiation alert as a fatal error. This is because applications requesting a renegotiation might well expect it to succeed and would have no code in place to handle the server denying it so the only safe thing to do is to terminate the connection. [Steve Henson] *) Add ctrl macro SSL_get_secure_renegotiation_support() which returns 1 if peer supports secure renegotiation and 0 otherwise. Print out peer renegotiation support in s_client/s_server. [Steve Henson] *) Replace the highly broken and deprecated SPKAC certification method with the updated NID creation version. This should correctly handle UTF8. [Steve Henson] *) Implement RFC5746. Re-enable renegotiation but require the extension as needed. Unfortunately, SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION turns out to be a bad idea. It has been replaced by SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION which can be set with SSL_CTX_set_options(). This is really not recommended unless you know what you are doing. [Eric Rescorla , Ben Laurie, Steve Henson] *) Fixes to stateless session resumption handling. Use initial_ctx when issuing and attempting to decrypt tickets in case it has changed during servername handling. Use a non-zero length session ID when attempting stateless session resumption: this makes it possible to determine if a resumption has occurred immediately after receiving server hello (several places in OpenSSL subtly assume this) instead of later in the handshake. [Steve Henson] *) The functions ENGINE_ctrl(), OPENSSL_isservice(), CMS_get1_RecipientRequest() and RAND_bytes() can return <=0 on error fixes for a few places where the return code is not checked correctly. [Julia Lawall ] *) Add --strict-warnings option to Configure script to include devteam warnings in other configurations. [Steve Henson] *) Add support for --libdir option and LIBDIR variable in makefiles. This makes it possible to install openssl libraries in locations which have names other than "lib", for example "/usr/lib64" which some systems need. [Steve Henson, based on patch from Jeremy Utley] *) Don't allow the use of leading 0x80 in OIDs. This is a violation of X690 8.9.12 and can produce some misleading textual output of OIDs. [Steve Henson, reported by Dan Kaminsky] *) Delete MD2 from algorithm tables. This follows the recommendation in several standards that it is not used in new applications due to several cryptographic weaknesses. For binary compatibility reasons the MD2 API is still compiled in by default. [Steve Henson] *) Add compression id to {d2i,i2d}_SSL_SESSION so it is correctly saved and restored. [Steve Henson] *) Rename uni2asc and asc2uni functions to OPENSSL_uni2asc and OPENSSL_asc2uni conditionally on Netware platforms to avoid a name clash. [Guenter ] *) Fix the server certificate chain building code to use X509_verify_cert(), it used to have an ad-hoc builder which was unable to cope with anything other than a simple chain. [David Woodhouse , Steve Henson] *) Don't check self signed certificate signatures in X509_verify_cert() by default (a flag can override this): it just wastes time without adding any security. As a useful side effect self signed root CAs with non-FIPS digests are now usable in FIPS mode. [Steve Henson] *) In dtls1_process_out_of_seq_message() the check if the current message is already buffered was missing. For every new message was memory allocated, allowing an attacker to perform an denial of service attack with sending out of seq handshake messages until there is no memory left. Additionally every future messege was buffered, even if the sequence number made no sense and would be part of another handshake. So only messages with sequence numbers less than 10 in advance will be buffered. (CVE-2009-1378) [Robin Seggelmann, discovered by Daniel Mentz] *) Records are buffered if they arrive with a future epoch to be processed after finishing the corresponding handshake. There is currently no limitation to this buffer allowing an attacker to perform a DOS attack with sending records with future epochs until there is no memory left. This patch adds the pqueue_size() function to detemine the size of a buffer and limits the record buffer to 100 entries. (CVE-2009-1377) [Robin Seggelmann, discovered by Daniel Mentz] *) Keep a copy of frag->msg_header.frag_len so it can be used after the parent structure is freed. (CVE-2009-1379) [Daniel Mentz] *) Handle non-blocking I/O properly in SSL_shutdown() call. [Darryl Miles ] *) Add 2.5.4.* OIDs [Ilya O. ] Changes between 0.9.8k and 0.9.8l [5 Nov 2009] *) Disable renegotiation completely - this fixes a severe security problem (CVE-2009-3555) at the cost of breaking all renegotiation. Renegotiation can be re-enabled by setting SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION in s3->flags at run-time. This is really not recommended unless you know what you're doing. [Ben Laurie] Changes between 0.9.8j and 0.9.8k [25 Mar 2009] *) Don't set val to NULL when freeing up structures, it is freed up by underlying code. If sizeof(void *) > sizeof(long) this can result in zeroing past the valid field. (CVE-2009-0789) [Paolo Ganci ] *) Fix bug where return value of CMS_SignerInfo_verify_content() was not checked correctly. This would allow some invalid signed attributes to appear to verify correctly. (CVE-2009-0591) [Ivan Nestlerode ] *) Reject UniversalString and BMPString types with invalid lengths. This prevents a crash in ASN1_STRING_print_ex() which assumes the strings have a legal length. (CVE-2009-0590) [Steve Henson] *) Set S/MIME signing as the default purpose rather than setting it unconditionally. This allows applications to override it at the store level. [Steve Henson] *) Permit restricted recursion of ASN1 strings. This is needed in practice to handle some structures. [Steve Henson] *) Improve efficiency of mem_gets: don't search whole buffer each time for a '\n' [Jeremy Shapiro ] *) New -hex option for openssl rand. [Matthieu Herrb] *) Print out UTF8String and NumericString when parsing ASN1. [Steve Henson] *) Support NumericString type for name components. [Steve Henson] *) Allow CC in the environment to override the automatically chosen compiler. Note that nothing is done to ensure flags work with the chosen compiler. [Ben Laurie] Changes between 0.9.8i and 0.9.8j [07 Jan 2009] *) Properly check EVP_VerifyFinal() and similar return values (CVE-2008-5077). [Ben Laurie, Bodo Moeller, Google Security Team] *) Enable TLS extensions by default. [Ben Laurie] *) Allow the CHIL engine to be loaded, whether the application is multithreaded or not. (This does not release the developer from the obligation to set up the dynamic locking callbacks.) [Sander Temme ] *) Use correct exit code if there is an error in dgst command. [Steve Henson; problem pointed out by Roland Dirlewanger] *) Tweak Configure so that you need to say "experimental-jpake" to enable JPAKE, and need to use -DOPENSSL_EXPERIMENTAL_JPAKE in applications. [Bodo Moeller] *) Add experimental JPAKE support, including demo authentication in s_client and s_server. [Ben Laurie] *) Set the comparison function in v3_addr_canonize(). [Rob Austein ] *) Add support for XMPP STARTTLS in s_client. [Philip Paeps ] *) Change the server-side SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG behavior to ensure that even with this option, only ciphersuites in the server's preference list will be accepted. (Note that the option applies only when resuming a session, so the earlier behavior was just about the algorithm choice for symmetric cryptography.) [Bodo Moeller] Changes between 0.9.8h and 0.9.8i [15 Sep 2008] *) Fix NULL pointer dereference if a DTLS server received ChangeCipherSpec as first record (CVE-2009-1386). [PR #1679] *) Fix a state transitition in s3_srvr.c and d1_srvr.c (was using SSL3_ST_CW_CLNT_HELLO_B, should be ..._ST_SW_SRVR_...). [Nagendra Modadugu] *) The fix in 0.9.8c that supposedly got rid of unsafe double-checked locking was incomplete for RSA blinding, addressing just one layer of what turns out to have been doubly unsafe triple-checked locking. So now fix this for real by retiring the MONT_HELPER macro in crypto/rsa/rsa_eay.c. [Bodo Moeller; problem pointed out by Marius Schilder] *) Various precautionary measures: - Avoid size_t integer overflow in HASH_UPDATE (md32_common.h). - Avoid a buffer overflow in d2i_SSL_SESSION() (ssl_asn1.c). (NB: This would require knowledge of the secret session ticket key to exploit, in which case you'd be SOL either way.) - Change bn_nist.c so that it will properly handle input BIGNUMs outside the expected range. - Enforce the 'num' check in BN_div() (bn_div.c) for non-BN_DEBUG builds. [Neel Mehta, Bodo Moeller] *) Allow engines to be "soft loaded" - i.e. optionally don't die if the load fails. Useful for distros. [Ben Laurie and the FreeBSD team] *) Add support for Local Machine Keyset attribute in PKCS#12 files. [Steve Henson] *) Fix BN_GF2m_mod_arr() top-bit cleanup code. [Huang Ying] *) Expand ENGINE to support engine supplied SSL client certificate functions. This work was sponsored by Logica. [Steve Henson] *) Add CryptoAPI ENGINE to support use of RSA and DSA keys held in Windows keystores. Support for SSL/TLS client authentication too. Not compiled unless enable-capieng specified to Configure. This work was sponsored by Logica. [Steve Henson] *) Fix bug in X509_ATTRIBUTE creation: dont set attribute using ASN1_TYPE_set1 if MBSTRING flag set. This bug would crash certain attribute creation routines such as certifcate requests and PKCS#12 files. [Steve Henson] Changes between 0.9.8g and 0.9.8h [28 May 2008] *) Fix flaw if 'Server Key exchange message' is omitted from a TLS handshake which could lead to a cilent crash as found using the Codenomicon TLS test suite (CVE-2008-1672) [Steve Henson, Mark Cox] *) Fix double free in TLS server name extensions which could lead to a remote crash found by Codenomicon TLS test suite (CVE-2008-0891) [Joe Orton] *) Clear error queue in SSL_CTX_use_certificate_chain_file() Clear the error queue to ensure that error entries left from older function calls do not interfere with the correct operation. [Lutz Jaenicke, Erik de Castro Lopo] *) Remove root CA certificates of commercial CAs: The OpenSSL project does not recommend any specific CA and does not have any policy with respect to including or excluding any CA. Therefore it does not make any sense to ship an arbitrary selection of root CA certificates with the OpenSSL software. [Lutz Jaenicke] *) RSA OAEP patches to fix two separate invalid memory reads. The first one involves inputs when 'lzero' is greater than 'SHA_DIGEST_LENGTH' (it would read about SHA_DIGEST_LENGTH bytes before the beginning of from). The second one involves inputs where the 'db' section contains nothing but zeroes (there is a one-byte invalid read after the end of 'db'). [Ivan Nestlerode ] *) Partial backport from 0.9.9-dev: Introduce bn_mul_mont (dedicated Montgomery multiplication procedure) as a candidate for BIGNUM assembler implementation. While 0.9.9-dev uses assembler for various architectures, only x86_64 is available by default here in the 0.9.8 branch, and 32-bit x86 is available through a compile-time setting. To try the 32-bit x86 assembler implementation, use Configure option "enable-montasm" (which exists only for this backport). As "enable-montasm" for 32-bit x86 disclaims code stability anyway, in this constellation we activate additional code backported from 0.9.9-dev for further performance improvements, namely BN_from_montgomery_word. (To enable this otherwise, e.g. x86_64, try "-DMONT_FROM_WORD___NON_DEFAULT_0_9_8_BUILD".) [Andy Polyakov (backport partially by Bodo Moeller)] *) Add TLS session ticket callback. This allows an application to set TLS ticket cipher and HMAC keys rather than relying on hardcoded fixed values. This is useful for key rollover for example where several key sets may exist with different names. [Steve Henson] *) Reverse ENGINE-internal logic for caching default ENGINE handles. This was broken until now in 0.9.8 releases, such that the only way a registered ENGINE could be used (assuming it initialises successfully on the host) was to explicitly set it as the default for the relevant algorithms. This is in contradiction with 0.9.7 behaviour and the documentation. With this fix, when an ENGINE is registered into a given algorithm's table of implementations, the 'uptodate' flag is reset so that auto-discovery will be used next time a new context for that algorithm attempts to select an implementation. [Ian Lister (tweaked by Geoff Thorpe)] *) Backport of CMS code to OpenSSL 0.9.8. This differs from the 0.9.9 implemention in the following ways: Lack of EVP_PKEY_ASN1_METHOD means algorithm parameters have to be hard coded. Lack of BER streaming support means one pass streaming processing is only supported if data is detached: setting the streaming flag is ignored for embedded content. CMS support is disabled by default and must be explicitly enabled with the enable-cms configuration option. [Steve Henson] *) Update the GMP engine glue to do direct copies between BIGNUM and mpz_t when openssl and GMP use the same limb size. Otherwise the existing "conversion via a text string export" trick is still used. [Paul Sheer ] *) Zlib compression BIO. This is a filter BIO which compressed and uncompresses any data passed through it. [Steve Henson] *) Add AES_wrap_key() and AES_unwrap_key() functions to implement RFC3394 compatible AES key wrapping. [Steve Henson] *) Add utility functions to handle ASN1 structures. ASN1_STRING_set0(): sets string data without copying. X509_ALGOR_set0() and X509_ALGOR_get0(): set and retrieve X509_ALGOR (AlgorithmIdentifier) data. Attribute function X509at_get0_data_by_OBJ(): retrieves data from an X509_ATTRIBUTE structure optionally checking it occurs only once. ASN1_TYPE_set1(): set and ASN1_TYPE structure copying supplied data. [Steve Henson] *) Fix BN flag handling in RSA_eay_mod_exp() and BN_MONT_CTX_set() to get the expected BN_FLG_CONSTTIME behavior. [Bodo Moeller (Google)] *) Netware support: - fixed wrong usage of ioctlsocket() when build for LIBC BSD sockets - fixed do_tests.pl to run the test suite with CLIB builds too (CLIB_OPT) - added some more tests to do_tests.pl - fixed RunningProcess usage so that it works with newer LIBC NDKs too - removed usage of BN_LLONG for CLIB builds to avoid runtime dependency - added new Configure targets netware-clib-bsdsock, netware-clib-gcc, netware-clib-bsdsock-gcc, netware-libc-bsdsock-gcc - various changes to netware.pl to enable gcc-cross builds on Win32 platform - changed crypto/bio/b_sock.c to work with macro functions (CLIB BSD) - various changes to fix missing prototype warnings - fixed x86nasm.pl to create correct asm files for NASM COFF output - added AES, WHIRLPOOL and CPUID assembler code to build files - added missing AES assembler make rules to mk1mf.pl - fixed order of includes in apps/ocsp.c so that e_os.h settings apply [Guenter Knauf ] *) Implement certificate status request TLS extension defined in RFC3546. A client can set the appropriate parameters and receive the encoded OCSP response via a callback. A server can query the supplied parameters and set the encoded OCSP response in the callback. Add simplified examples to s_client and s_server. [Steve Henson] Changes between 0.9.8f and 0.9.8g [19 Oct 2007] *) Fix various bugs: + Binary incompatibility of ssl_ctx_st structure + DTLS interoperation with non-compliant servers + Don't call get_session_cb() without proposed session + Fix ia64 assembler code [Andy Polyakov, Steve Henson] Changes between 0.9.8e and 0.9.8f [11 Oct 2007] *) DTLS Handshake overhaul. There were longstanding issues with OpenSSL DTLS implementation, which were making it impossible for RFC 4347 compliant client to communicate with OpenSSL server. Unfortunately just fixing these incompatibilities would "cut off" pre-0.9.8f clients. To allow for hassle free upgrade post-0.9.8e server keeps tolerating non RFC compliant syntax. The opposite is not true, 0.9.8f client can not communicate with earlier server. This update even addresses CVE-2007-4995. [Andy Polyakov] *) Changes to avoid need for function casts in OpenSSL: some compilers (gcc 4.2 and later) reject their use. [Kurt Roeckx , Peter Hartley , Steve Henson] *) Add RFC4507 support to OpenSSL. This includes the corrections in RFC4507bis. The encrypted ticket format is an encrypted encoded SSL_SESSION structure, that way new session features are automatically supported. If a client application caches session in an SSL_SESSION structure support is transparent because tickets are now stored in the encoded SSL_SESSION. The SSL_CTX structure automatically generates keys for ticket protection in servers so again support should be possible with no application modification. If a client or server wishes to disable RFC4507 support then the option SSL_OP_NO_TICKET can be set. Add a TLS extension debugging callback to allow the contents of any client or server extensions to be examined. This work was sponsored by Google. [Steve Henson] *) Add initial support for TLS extensions, specifically for the server_name extension so far. The SSL_SESSION, SSL_CTX, and SSL data structures now have new members for a host name. The SSL data structure has an additional member SSL_CTX *initial_ctx so that new sessions can be stored in that context to allow for session resumption, even after the SSL has been switched to a new SSL_CTX in reaction to a client's server_name extension. New functions (subject to change): SSL_get_servername() SSL_get_servername_type() SSL_set_SSL_CTX() New CTRL codes and macros (subject to change): SSL_CTRL_SET_TLSEXT_SERVERNAME_CB - SSL_CTX_set_tlsext_servername_callback() SSL_CTRL_SET_TLSEXT_SERVERNAME_ARG - SSL_CTX_set_tlsext_servername_arg() SSL_CTRL_SET_TLSEXT_HOSTNAME - SSL_set_tlsext_host_name() openssl s_client has a new '-servername ...' option. openssl s_server has new options '-servername_host ...', '-cert2 ...', '-key2 ...', '-servername_fatal' (subject to change). This allows testing the HostName extension for a specific single host name ('-cert' and '-key' remain fallbacks for handshakes without HostName negotiation). If the unrecogninzed_name alert has to be sent, this by default is a warning; it becomes fatal with the '-servername_fatal' option. [Peter Sylvester, Remy Allais, Christophe Renou, Steve Henson] *) Add AES and SSE2 assembly language support to VC++ build. [Steve Henson] *) Mitigate attack on final subtraction in Montgomery reduction. [Andy Polyakov] *) Fix crypto/ec/ec_mult.c to work properly with scalars of value 0 (which previously caused an internal error). [Bodo Moeller] *) Squeeze another 10% out of IGE mode when in != out. [Ben Laurie] *) AES IGE mode speedup. [Dean Gaudet (Google)] *) Add the Korean symmetric 128-bit cipher SEED (see http://www.kisa.or.kr/kisa/seed/jsp/seed_eng.jsp) and add SEED ciphersuites from RFC 4162: TLS_RSA_WITH_SEED_CBC_SHA = "SEED-SHA" TLS_DHE_DSS_WITH_SEED_CBC_SHA = "DHE-DSS-SEED-SHA" TLS_DHE_RSA_WITH_SEED_CBC_SHA = "DHE-RSA-SEED-SHA" TLS_DH_anon_WITH_SEED_CBC_SHA = "ADH-SEED-SHA" To minimize changes between patchlevels in the OpenSSL 0.9.8 series, SEED remains excluded from compilation unless OpenSSL is configured with 'enable-seed'. [KISA, Bodo Moeller] *) Mitigate branch prediction attacks, which can be practical if a single processor is shared, allowing a spy process to extract information. For detailed background information, see http://eprint.iacr.org/2007/039 (O. Aciicmez, S. Gueron, J.-P. Seifert, "New Branch Prediction Vulnerabilities in OpenSSL and Necessary Software Countermeasures"). The core of the change are new versions BN_div_no_branch() and BN_mod_inverse_no_branch() of BN_div() and BN_mod_inverse(), respectively, which are slower, but avoid the security-relevant conditional branches. These are automatically called by BN_div() and BN_mod_inverse() if the flag BN_FLG_CONSTTIME is set for one of the input BIGNUMs. Also, BN_is_bit_set() has been changed to remove a conditional branch. BN_FLG_CONSTTIME is the new name for the previous BN_FLG_EXP_CONSTTIME flag, since it now affects more than just modular exponentiation. (Since OpenSSL 0.9.7h, setting this flag in the exponent causes BN_mod_exp_mont() to use the alternative implementation in BN_mod_exp_mont_consttime().) The old name remains as a deprecated alias. Similary, RSA_FLAG_NO_EXP_CONSTTIME is replaced by a more general RSA_FLAG_NO_CONSTTIME flag since the RSA implementation now uses constant-time implementations for more than just exponentiation. Here too the old name is kept as a deprecated alias. BN_BLINDING_new() will now use BN_dup() for the modulus so that the BN_BLINDING structure gets an independent copy of the modulus. This means that the previous "BIGNUM *m" argument to BN_BLINDING_new() and to BN_BLINDING_create_param() now essentially becomes "const BIGNUM *m", although we can't actually change this in the header file before 0.9.9. It allows RSA_setup_blinding() to use BN_with_flags() on the modulus to enable BN_FLG_CONSTTIME. [Matthew D Wood (Intel Corp)] *) In the SSL/TLS server implementation, be strict about session ID context matching (which matters if an application uses a single external cache for different purposes). Previously, out-of-context reuse was forbidden only if SSL_VERIFY_PEER was set. This did ensure strict client verification, but meant that, with applications using a single external cache for quite different requirements, clients could circumvent ciphersuite restrictions for a given session ID context by starting a session in a different context. [Bodo Moeller] *) Include "!eNULL" in SSL_DEFAULT_CIPHER_LIST to make sure that a ciphersuite string such as "DEFAULT:RSA" cannot enable authentication-only ciphersuites. [Bodo Moeller] *) Update the SSL_get_shared_ciphers() fix CVE-2006-3738 which was not complete and could lead to a possible single byte overflow (CVE-2007-5135) [Ben Laurie] Changes between 0.9.8d and 0.9.8e [23 Feb 2007] *) Since AES128 and AES256 (and similarly Camellia128 and Camellia256) share a single mask bit in the logic of ssl/ssl_ciph.c, the code for masking out disabled ciphers needs a kludge to work properly if AES128 is available and AES256 isn't (or if Camellia128 is available and Camellia256 isn't). [Victor Duchovni] *) Fix the BIT STRING encoding generated by crypto/ec/ec_asn1.c (within i2d_ECPrivateKey, i2d_ECPKParameters, i2d_ECParameters): When a point or a seed is encoded in a BIT STRING, we need to prevent the removal of trailing zero bits to get the proper DER encoding. (By default, crypto/asn1/a_bitstr.c assumes the case of a NamedBitList, for which trailing 0 bits need to be removed.) [Bodo Moeller] *) Have SSL/TLS server implementation tolerate "mismatched" record protocol version while receiving ClientHello even if the ClientHello is fragmented. (The server can't insist on the particular protocol version it has chosen before the ServerHello message has informed the client about his choice.) [Bodo Moeller] *) Add RFC 3779 support. [Rob Austein for ARIN, Ben Laurie] *) Load error codes if they are not already present instead of using a static variable. This allows them to be cleanly unloaded and reloaded. Improve header file function name parsing. [Steve Henson] *) extend SMTP and IMAP protocol emulation in s_client to use EHLO or CAPABILITY handshake as required by RFCs. [Goetz Babin-Ebell] Changes between 0.9.8c and 0.9.8d [28 Sep 2006] *) Introduce limits to prevent malicious keys being able to cause a denial of service. (CVE-2006-2940) [Steve Henson, Bodo Moeller] *) Fix ASN.1 parsing of certain invalid structures that can result in a denial of service. (CVE-2006-2937) [Steve Henson] *) Fix buffer overflow in SSL_get_shared_ciphers() function. (CVE-2006-3738) [Tavis Ormandy and Will Drewry, Google Security Team] *) Fix SSL client code which could crash if connecting to a malicious SSLv2 server. (CVE-2006-4343) [Tavis Ormandy and Will Drewry, Google Security Team] *) Since 0.9.8b, ciphersuite strings naming explicit ciphersuites match only those. Before that, "AES256-SHA" would be interpreted as a pattern and match "AES128-SHA" too (since AES128-SHA got the same strength classification in 0.9.7h) as we currently only have a single AES bit in the ciphersuite description bitmap. That change, however, also applied to ciphersuite strings such as "RC4-MD5" that intentionally matched multiple ciphersuites -- namely, SSL 2.0 ciphersuites in addition to the more common ones from SSL 3.0/TLS 1.0. So we change the selection algorithm again: Naming an explicit ciphersuite selects this one ciphersuite, and any other similar ciphersuite (same bitmap) from *other* protocol versions. Thus, "RC4-MD5" again will properly select both the SSL 2.0 ciphersuite and the SSL 3.0/TLS 1.0 ciphersuite. Since SSL 2.0 does not have any ciphersuites for which the 128/256 bit distinction would be relevant, this works for now. The proper fix will be to use different bits for AES128 and AES256, which would have avoided the problems from the beginning; however, bits are scarce, so we can only do this in a new release (not just a patchlevel) when we can change the SSL_CIPHER definition to split the single 'unsigned long mask' bitmap into multiple values to extend the available space. [Bodo Moeller] Changes between 0.9.8b and 0.9.8c [05 Sep 2006] *) Avoid PKCS #1 v1.5 signature attack discovered by Daniel Bleichenbacher (CVE-2006-4339) [Ben Laurie and Google Security Team] *) Add AES IGE and biIGE modes. [Ben Laurie] *) Change the Unix randomness entropy gathering to use poll() when possible instead of select(), since the latter has some undesirable limitations. [Darryl Miles via Richard Levitte and Bodo Moeller] *) Disable "ECCdraft" ciphersuites more thoroughly. Now special treatment in ssl/ssl_ciph.s makes sure that these ciphersuites cannot be implicitly activated as part of, e.g., the "AES" alias. However, please upgrade to OpenSSL 0.9.9[-dev] for non-experimental use of the ECC ciphersuites to get TLS extension support, which is required for curve and point format negotiation to avoid potential handshake problems. [Bodo Moeller] *) Disable rogue ciphersuites: - SSLv2 0x08 0x00 0x80 ("RC4-64-MD5") - SSLv3/TLSv1 0x00 0x61 ("EXP1024-RC2-CBC-MD5") - SSLv3/TLSv1 0x00 0x60 ("EXP1024-RC4-MD5") The latter two were purportedly from draft-ietf-tls-56-bit-ciphersuites-0[01].txt, but do not really appear there. Also deactivate the remaining ciphersuites from draft-ietf-tls-56-bit-ciphersuites-01.txt. These are just as unofficial, and the ID has long expired. [Bodo Moeller] *) Fix RSA blinding Heisenbug (problems sometimes occured on dual-core machines) and other potential thread-safety issues. [Bodo Moeller] *) Add the symmetric cipher Camellia (128-bit, 192-bit, 256-bit key versions), which is now available for royalty-free use (see http://info.isl.ntt.co.jp/crypt/eng/info/chiteki.html). Also, add Camellia TLS ciphersuites from RFC 4132. To minimize changes between patchlevels in the OpenSSL 0.9.8 series, Camellia remains excluded from compilation unless OpenSSL is configured with 'enable-camellia'. [NTT] *) Disable the padding bug check when compression is in use. The padding bug check assumes the first packet is of even length, this is not necessarily true if compresssion is enabled and can result in false positives causing handshake failure. The actual bug test is ancient code so it is hoped that implementations will either have fixed it by now or any which still have the bug do not support compression. [Steve Henson] Changes between 0.9.8a and 0.9.8b [04 May 2006] *) When applying a cipher rule check to see if string match is an explicit cipher suite and only match that one cipher suite if it is. [Steve Henson] *) Link in manifests for VC++ if needed. [Austin Ziegler ] *) Update support for ECC-based TLS ciphersuites according to draft-ietf-tls-ecc-12.txt with proposed changes (but without TLS extensions, which are supported starting with the 0.9.9 branch, not in the OpenSSL 0.9.8 branch). [Douglas Stebila] *) New functions EVP_CIPHER_CTX_new() and EVP_CIPHER_CTX_free() to support opaque EVP_CIPHER_CTX handling. [Steve Henson] *) Fixes and enhancements to zlib compression code. We now only use "zlib1.dll" and use the default __cdecl calling convention on Win32 to conform with the standards mentioned here: http://www.zlib.net/DLL_FAQ.txt Static zlib linking now works on Windows and the new --with-zlib-include --with-zlib-lib options to Configure can be used to supply the location of the headers and library. Gracefully handle case where zlib library can't be loaded. [Steve Henson] *) Several fixes and enhancements to the OID generation code. The old code sometimes allowed invalid OIDs (1.X for X >= 40 for example), couldn't handle numbers larger than ULONG_MAX, truncated printing and had a non standard OBJ_obj2txt() behaviour. [Steve Henson] *) Add support for building of engines under engine/ as shared libraries under VC++ build system. [Steve Henson] *) Corrected the numerous bugs in the Win32 path splitter in DSO. Hopefully, we will not see any false combination of paths any more. [Richard Levitte] Changes between 0.9.8 and 0.9.8a [11 Oct 2005] *) Remove the functionality of SSL_OP_MSIE_SSLV2_RSA_PADDING (part of SSL_OP_ALL). This option used to disable the countermeasure against man-in-the-middle protocol-version rollback in the SSL 2.0 server implementation, which is a bad idea. (CVE-2005-2969) [Bodo Moeller; problem pointed out by Yutaka Oiwa (Research Center for Information Security, National Institute of Advanced Industrial Science and Technology [AIST], Japan)] *) Add two function to clear and return the verify parameter flags. [Steve Henson] *) Keep cipherlists sorted in the source instead of sorting them at runtime, thus removing the need for a lock. [Nils Larsch] *) Avoid some small subgroup attacks in Diffie-Hellman. [Nick Mathewson and Ben Laurie] *) Add functions for well-known primes. [Nick Mathewson] *) Extended Windows CE support. [Satoshi Nakamura and Andy Polyakov] *) Initialize SSL_METHOD structures at compile time instead of during runtime, thus removing the need for a lock. [Steve Henson] *) Make PKCS7_decrypt() work even if no certificate is supplied by attempting to decrypt each encrypted key in turn. Add support to smime utility. [Steve Henson] Changes between 0.9.7h and 0.9.8 [05 Jul 2005] [NB: OpenSSL 0.9.7i and later 0.9.7 patch levels were released after OpenSSL 0.9.8.] *) Add libcrypto.pc and libssl.pc for those who feel they need them. [Richard Levitte] *) Change CA.sh and CA.pl so they don't bundle the CSR and the private key into the same file any more. [Richard Levitte] *) Add initial support for Win64, both IA64 and AMD64/x64 flavors. [Andy Polyakov] *) Add -utf8 command line and config file option to 'ca'. [Stefan and Geoff Thorpe] *) Add attribute functions to EVP_PKEY structure. Modify PKCS12_create() to recognize a CSP name attribute and use it. Make -CSP option work again in pkcs12 utility. [Steve Henson] *) Add new functionality to the bn blinding code: - automatic re-creation of the BN_BLINDING parameters after a fixed number of uses (currently 32) - add new function for parameter creation - introduce flags to control the update behaviour of the BN_BLINDING parameters - hide BN_BLINDING structure Add a second BN_BLINDING slot to the RSA structure to improve performance when a single RSA object is shared among several threads. [Nils Larsch] *) Add support for DTLS. [Nagendra Modadugu and Ben Laurie] *) Add support for DER encoded private keys (SSL_FILETYPE_ASN1) to SSL_CTX_use_PrivateKey_file() and SSL_use_PrivateKey_file() [Walter Goulet] *) Remove buggy and incompletet DH cert support from ssl/ssl_rsa.c and ssl/s3_both.c [Nils Larsch] *) Use SHA-1 instead of MD5 as the default digest algorithm for the apps/openssl applications. [Nils Larsch] *) Compile clean with "-Wall -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -Werror". Currently DEBUG_SAFESTACK must also be set. [Ben Laurie] *) Change ./Configure so that certain algorithms can be disabled by default. The new counterpiece to "no-xxx" is "enable-xxx". The patented RC5 and MDC2 algorithms will now be disabled unless "enable-rc5" and "enable-mdc2", respectively, are specified. (IDEA remains enabled despite being patented. This is because IDEA is frequently required for interoperability, and there is no license fee for non-commercial use. As before, "no-idea" can be used to avoid this algorithm.) [Bodo Moeller] *) Add processing of proxy certificates (see RFC 3820). This work was sponsored by KTH (The Royal Institute of Technology in Stockholm) and EGEE (Enabling Grids for E-science in Europe). [Richard Levitte] *) RC4 performance overhaul on modern architectures/implementations, such as Intel P4, IA-64 and AMD64. [Andy Polyakov] *) New utility extract-section.pl. This can be used specify an alternative section number in a pod file instead of having to treat each file as a separate case in Makefile. This can be done by adding two lines to the pod file: =for comment openssl_section:XXX The blank line is mandatory. [Steve Henson] *) New arguments -certform, -keyform and -pass for s_client and s_server to allow alternative format key and certificate files and passphrase sources. [Steve Henson] *) New structure X509_VERIFY_PARAM which combines current verify parameters, update associated structures and add various utility functions. Add new policy related verify parameters, include policy checking in standard verify code. Enhance 'smime' application with extra parameters to support policy checking and print out. [Steve Henson] *) Add a new engine to support VIA PadLock ACE extensions in the VIA C3 Nehemiah processors. These extensions support AES encryption in hardware as well as RNG (though RNG support is currently disabled). [Michal Ludvig , with help from Andy Polyakov] *) Deprecate BN_[get|set]_params() functions (they were ignored internally). [Geoff Thorpe] *) New FIPS 180-2 algorithms, SHA-224/-256/-384/-512 are implemented. [Andy Polyakov and a number of other people] *) Improved PowerPC platform support. Most notably BIGNUM assembler implementation contributed by IBM. [Suresh Chari, Peter Waltenberg, Andy Polyakov] *) The new 'RSA_generate_key_ex' function now takes a BIGNUM for the public exponent rather than 'unsigned long'. There is a corresponding change to the new 'rsa_keygen' element of the RSA_METHOD structure. [Jelte Jansen, Geoff Thorpe] *) Functionality for creating the initial serial number file is now moved from CA.pl to the 'ca' utility with a new option -create_serial. (Before OpenSSL 0.9.7e, CA.pl used to initialize the serial number file to 1, which is bound to cause problems. To avoid the problems while respecting compatibility between different 0.9.7 patchlevels, 0.9.7e employed 'openssl x509 -next_serial' in CA.pl for serial number initialization. With the new release 0.9.8, we can fix the problem directly in the 'ca' utility.) [Steve Henson] *) Reduced header interdepencies by declaring more opaque objects in ossl_typ.h. As a consequence, including some headers (eg. engine.h) will give fewer recursive includes, which could break lazy source code - so this change is covered by the OPENSSL_NO_DEPRECATED symbol. As always, developers should define this symbol when building and using openssl to ensure they track the recommended behaviour, interfaces, [etc], but backwards-compatible behaviour prevails when this isn't defined. [Geoff Thorpe] *) New function X509_POLICY_NODE_print() which prints out policy nodes. [Steve Henson] *) Add new EVP function EVP_CIPHER_CTX_rand_key and associated functionality. This will generate a random key of the appropriate length based on the cipher context. The EVP_CIPHER can provide its own random key generation routine to support keys of a specific form. This is used in the des and 3des routines to generate a key of the correct parity. Update S/MIME code to use new functions and hence generate correct parity DES keys. Add EVP_CHECK_DES_KEY #define to return an error if the key is not valid (weak or incorrect parity). [Steve Henson] *) Add a local set of CRLs that can be used by X509_verify_cert() as well as looking them up. This is useful when the verified structure may contain CRLs, for example PKCS#7 signedData. Modify PKCS7_verify() to use any CRLs present unless the new PKCS7_NO_CRL flag is asserted. [Steve Henson] *) Extend ASN1 oid configuration module. It now additionally accepts the syntax: shortName = some long name, 1.2.3.4 [Steve Henson] *) Reimplemented the BN_CTX implementation. There is now no more static limitation on the number of variables it can handle nor the depth of the "stack" handling for BN_CTX_start()/BN_CTX_end() pairs. The stack information can now expand as required, and rather than having a single static array of bignums, BN_CTX now uses a linked-list of such arrays allowing it to expand on demand whilst maintaining the usefulness of BN_CTX's "bundling". [Geoff Thorpe] *) Add a missing BN_CTX parameter to the 'rsa_mod_exp' callback in RSA_METHOD to allow all RSA operations to function using a single BN_CTX. [Geoff Thorpe] *) Preliminary support for certificate policy evaluation and checking. This is initially intended to pass the tests outlined in "Conformance Testing of Relying Party Client Certificate Path Processing Logic" v1.07. [Steve Henson] *) bn_dup_expand() has been deprecated, it was introduced in 0.9.7 and remained unused and not that useful. A variety of other little bignum tweaks and fixes have also been made continuing on from the audit (see below). [Geoff Thorpe] *) Constify all or almost all d2i, c2i, s2i and r2i functions, along with associated ASN1, EVP and SSL functions and old ASN1 macros. [Richard Levitte] *) BN_zero() only needs to set 'top' and 'neg' to zero for correct results, and this should never fail. So the return value from the use of BN_set_word() (which can fail due to needless expansion) is now deprecated; if OPENSSL_NO_DEPRECATED is defined, BN_zero() is a void macro. [Geoff Thorpe] *) BN_CTX_get() should return zero-valued bignums, providing the same initialised value as BN_new(). [Geoff Thorpe, suggested by Ulf Möller] *) Support for inhibitAnyPolicy certificate extension. [Steve Henson] *) An audit of the BIGNUM code is underway, for which debugging code is enabled when BN_DEBUG is defined. This makes stricter enforcements on what is considered valid when processing BIGNUMs, and causes execution to assert() when a problem is discovered. If BN_DEBUG_RAND is defined, further steps are taken to deliberately pollute unused data in BIGNUM structures to try and expose faulty code further on. For now, openssl will (in its default mode of operation) continue to tolerate the inconsistent forms that it has tolerated in the past, but authors and packagers should consider trying openssl and their own applications when compiled with these debugging symbols defined. It will help highlight potential bugs in their own code, and will improve the test coverage for OpenSSL itself. At some point, these tighter rules will become openssl's default to improve maintainability, though the assert()s and other overheads will remain only in debugging configurations. See bn.h for more details. [Geoff Thorpe, Nils Larsch, Ulf Möller] *) BN_CTX_init() has been deprecated, as BN_CTX is an opaque structure that can only be obtained through BN_CTX_new() (which implicitly initialises it). The presence of this function only made it possible to overwrite an existing structure (and cause memory leaks). [Geoff Thorpe] *) Because of the callback-based approach for implementing LHASH as a template type, lh_insert() adds opaque objects to hash-tables and lh_doall() or lh_doall_arg() are typically used with a destructor callback to clean up those corresponding objects before destroying the hash table (and losing the object pointers). So some over-zealous constifications in LHASH have been relaxed so that lh_insert() does not take (nor store) the objects as "const" and the lh_doall[_arg] callback wrappers are not prototyped to have "const" restrictions on the object pointers they are given (and so aren't required to cast them away any more). [Geoff Thorpe] *) The tmdiff.h API was so ugly and minimal that our own timing utility (speed) prefers to use its own implementation. The two implementations haven't been consolidated as yet (volunteers?) but the tmdiff API has had its object type properly exposed (MS_TM) instead of casting to/from "char *". This may still change yet if someone realises MS_TM and "ms_time_***" aren't necessarily the greatest nomenclatures - but this is what was used internally to the implementation so I've used that for now. [Geoff Thorpe] *) Ensure that deprecated functions do not get compiled when OPENSSL_NO_DEPRECATED is defined. Some "openssl" subcommands and a few of the self-tests were still using deprecated key-generation functions so these have been updated also. [Geoff Thorpe] *) Reorganise PKCS#7 code to separate the digest location functionality into PKCS7_find_digest(), digest addtion into PKCS7_bio_add_digest(). New function PKCS7_set_digest() to set the digest type for PKCS#7 digestedData type. Add additional code to correctly generate the digestedData type and add support for this type in PKCS7 initialization functions. [Steve Henson] *) New function PKCS7_set0_type_other() this initializes a PKCS7 structure of type "other". [Steve Henson] *) Fix prime generation loop in crypto/bn/bn_prime.pl by making sure the loop does correctly stop and breaking ("division by zero") modulus operations are not performed. The (pre-generated) prime table crypto/bn/bn_prime.h was already correct, but it could not be re-generated on some platforms because of the "division by zero" situation in the script. [Ralf S. Engelschall] *) Update support for ECC-based TLS ciphersuites according to draft-ietf-tls-ecc-03.txt: the KDF1 key derivation function with SHA-1 now is only used for "small" curves (where the representation of a field element takes up to 24 bytes); for larger curves, the field element resulting from ECDH is directly used as premaster secret. [Douglas Stebila (Sun Microsystems Laboratories)] *) Add code for kP+lQ timings to crypto/ec/ectest.c, and add SEC2 curve secp160r1 to the tests. [Douglas Stebila (Sun Microsystems Laboratories)] *) Add the possibility to load symbols globally with DSO. [Götz Babin-Ebell via Richard Levitte] *) Add the functions ERR_set_mark() and ERR_pop_to_mark() for better control of the error stack. [Richard Levitte] *) Add support for STORE in ENGINE. [Richard Levitte] *) Add the STORE type. The intention is to provide a common interface to certificate and key stores, be they simple file-based stores, or HSM-type store, or LDAP stores, or... NOTE: The code is currently UNTESTED and isn't really used anywhere. [Richard Levitte] *) Add a generic structure called OPENSSL_ITEM. This can be used to pass a list of arguments to any function as well as provide a way for a function to pass data back to the caller. [Richard Levitte] *) Add the functions BUF_strndup() and BUF_memdup(). BUF_strndup() works like BUF_strdup() but can be used to duplicate a portion of a string. The copy gets NUL-terminated. BUF_memdup() duplicates a memory area. [Richard Levitte] *) Add the function sk_find_ex() which works like sk_find(), but will return an index to an element even if an exact match couldn't be found. The index is guaranteed to point at the element where the searched-for key would be inserted to preserve sorting order. [Richard Levitte] *) Add the function OBJ_bsearch_ex() which works like OBJ_bsearch() but takes an extra flags argument for optional functionality. Currently, the following flags are defined: OBJ_BSEARCH_VALUE_ON_NOMATCH This one gets OBJ_bsearch_ex() to return a pointer to the first element where the comparing function returns a negative or zero number. OBJ_BSEARCH_FIRST_VALUE_ON_MATCH This one gets OBJ_bsearch_ex() to return a pointer to the first element where the comparing function returns zero. This is useful if there are more than one element where the comparing function returns zero. [Richard Levitte] *) Make it possible to create self-signed certificates with 'openssl ca' in such a way that the self-signed certificate becomes part of the CA database and uses the same mechanisms for serial number generation as all other certificate signing. The new flag '-selfsign' enables this functionality. Adapt CA.sh and CA.pl.in. [Richard Levitte] *) Add functionality to check the public key of a certificate request against a given private. This is useful to check that a certificate request can be signed by that key (self-signing). [Richard Levitte] *) Make it possible to have multiple active certificates with the same subject in the CA index file. This is done only if the keyword 'unique_subject' is set to 'no' in the main CA section (default if 'CA_default') of the configuration file. The value is saved with the database itself in a separate index attribute file, named like the index file with '.attr' appended to the name. [Richard Levitte] *) Generate muti valued AVAs using '+' notation in config files for req and dirName. [Steve Henson] *) Support for nameConstraints certificate extension. [Steve Henson] *) Support for policyConstraints certificate extension. [Steve Henson] *) Support for policyMappings certificate extension. [Steve Henson] *) Make sure the default DSA_METHOD implementation only uses its dsa_mod_exp() and/or bn_mod_exp() handlers if they are non-NULL, and change its own handlers to be NULL so as to remove unnecessary indirection. This lets alternative implementations fallback to the default implementation more easily. [Geoff Thorpe] *) Support for directoryName in GeneralName related extensions in config files. [Steve Henson] *) Make it possible to link applications using Makefile.shared. Make that possible even when linking against static libraries! [Richard Levitte] *) Support for single pass processing for S/MIME signing. This now means that S/MIME signing can be done from a pipe, in addition cleartext signing (multipart/signed type) is effectively streaming and the signed data does not need to be all held in memory. This is done with a new flag PKCS7_STREAM. When this flag is set PKCS7_sign() only initializes the PKCS7 structure and the actual signing is done after the data is output (and digests calculated) in SMIME_write_PKCS7(). [Steve Henson] *) Add full support for -rpath/-R, both in shared libraries and applications, at least on the platforms where it's known how to do it. [Richard Levitte] *) In crypto/ec/ec_mult.c, implement fast point multiplication with precomputation, based on wNAF splitting: EC_GROUP_precompute_mult() will now compute a table of multiples of the generator that makes subsequent invocations of EC_POINTs_mul() or EC_POINT_mul() faster (notably in the case of a single point multiplication, scalar * generator). [Nils Larsch, Bodo Moeller] *) IPv6 support for certificate extensions. The various extensions which use the IP:a.b.c.d can now take IPv6 addresses using the formats of RFC1884 2.2 . IPv6 addresses are now also displayed correctly. [Steve Henson] *) Added an ENGINE that implements RSA by performing private key exponentiations with the GMP library. The conversions to and from GMP's mpz_t format aren't optimised nor are any montgomery forms cached, and on x86 it appears OpenSSL's own performance has caught up. However there are likely to be other architectures where GMP could provide a boost. This ENGINE is not built in by default, but it can be specified at Configure time and should be accompanied by the necessary linker additions, eg; ./config -DOPENSSL_USE_GMP -lgmp [Geoff Thorpe] *) "openssl engine" will not display ENGINE/DSO load failure errors when testing availability of engines with "-t" - the old behaviour is produced by increasing the feature's verbosity with "-tt". [Geoff Thorpe] *) ECDSA routines: under certain error conditions uninitialized BN objects could be freed. Solution: make sure initialization is performed early enough. (Reported and fix supplied by Nils Larsch via PR#459) [Lutz Jaenicke] *) Key-generation can now be implemented in RSA_METHOD, DSA_METHOD and DH_METHOD (eg. by ENGINE implementations) to override the normal software implementations. For DSA and DH, parameter generation can also be overriden by providing the appropriate method callbacks. [Geoff Thorpe] *) Change the "progress" mechanism used in key-generation and primality testing to functions that take a new BN_GENCB pointer in place of callback/argument pairs. The new API functions have "_ex" postfixes and the older functions are reimplemented as wrappers for the new ones. The OPENSSL_NO_DEPRECATED symbol can be used to hide declarations of the old functions to help (graceful) attempts to migrate to the new functions. Also, the new key-generation API functions operate on a caller-supplied key-structure and return success/failure rather than returning a key or NULL - this is to help make "keygen" another member function of RSA_METHOD etc. Example for using the new callback interface: int (*my_callback)(int a, int b, BN_GENCB *cb) = ...; void *my_arg = ...; BN_GENCB my_cb; BN_GENCB_set(&my_cb, my_callback, my_arg); return BN_is_prime_ex(some_bignum, BN_prime_checks, NULL, &cb); /* For the meaning of a, b in calls to my_callback(), see the * documentation of the function that calls the callback. * cb will point to my_cb; my_arg can be retrieved as cb->arg. * my_callback should return 1 if it wants BN_is_prime_ex() * to continue, or 0 to stop. */ [Geoff Thorpe] *) Change the ZLIB compression method to be stateful, and make it available to TLS with the number defined in draft-ietf-tls-compression-04.txt. [Richard Levitte] *) Add the ASN.1 structures and functions for CertificatePair, which is defined as follows (according to X.509_4thEditionDraftV6.pdf): CertificatePair ::= SEQUENCE { forward [0] Certificate OPTIONAL, reverse [1] Certificate OPTIONAL, -- at least one of the pair shall be present -- } Also implement the PEM functions to read and write certificate pairs, and defined the PEM tag as "CERTIFICATE PAIR". This needed to be defined, mostly for the sake of the LDAP attribute crossCertificatePair, but may prove useful elsewhere as well. [Richard Levitte] *) Make it possible to inhibit symlinking of shared libraries in Makefile.shared, for Cygwin's sake. [Richard Levitte] *) Extend the BIGNUM API by creating a function void BN_set_negative(BIGNUM *a, int neg); and a macro that behave like int BN_is_negative(const BIGNUM *a); to avoid the need to access 'a->neg' directly in applications. [Nils Larsch] *) Implement fast modular reduction for pseudo-Mersenne primes used in NIST curves (crypto/bn/bn_nist.c, crypto/ec/ecp_nist.c). EC_GROUP_new_curve_GFp() will now automatically use this if applicable. [Nils Larsch ] *) Add new lock type (CRYPTO_LOCK_BN). [Bodo Moeller] *) Change the ENGINE framework to automatically load engines dynamically from specific directories unless they could be found to already be built in or loaded. Move all the current engines except for the cryptodev one to a new directory engines/. The engines in engines/ are built as shared libraries if the "shared" options was given to ./Configure or ./config. Otherwise, they are inserted in libcrypto.a. /usr/local/ssl/engines is the default directory for dynamic engines, but that can be overriden at configure time through the usual use of --prefix and/or --openssldir, and at run time with the environment variable OPENSSL_ENGINES. [Geoff Thorpe and Richard Levitte] *) Add Makefile.shared, a helper makefile to build shared libraries. Addapt Makefile.org. [Richard Levitte] *) Add version info to Win32 DLLs. [Peter 'Luna' Runestig" ] *) Add new 'medium level' PKCS#12 API. Certificates and keys can be added using this API to created arbitrary PKCS#12 files while avoiding the low level API. New options to PKCS12_create(), key or cert can be NULL and will then be omitted from the output file. The encryption algorithm NIDs can be set to -1 for no encryption, the mac iteration count can be set to 0 to omit the mac. Enhance pkcs12 utility by making the -nokeys and -nocerts options work when creating a PKCS#12 file. New option -nomac to omit the mac, NONE can be set for an encryption algorithm. New code is modified to use the enhanced PKCS12_create() instead of the low level API. [Steve Henson] *) Extend ASN1 encoder to support indefinite length constructed encoding. This can output sequences tags and octet strings in this form. Modify pk7_asn1.c to support indefinite length encoding. This is experimental and needs additional code to be useful, such as an ASN1 bio and some enhanced streaming PKCS#7 code. Extend template encode functionality so that tagging is passed down to the template encoder. [Steve Henson] *) Let 'openssl req' fail if an argument to '-newkey' is not recognized instead of using RSA as a default. [Bodo Moeller] *) Add support for ECC-based ciphersuites from draft-ietf-tls-ecc-01.txt. As these are not official, they are not included in "ALL"; the "ECCdraft" ciphersuite group alias can be used to select them. [Vipul Gupta and Sumit Gupta (Sun Microsystems Laboratories)] *) Add ECDH engine support. [Nils Gura and Douglas Stebila (Sun Microsystems Laboratories)] *) Add ECDH in new directory crypto/ecdh/. [Douglas Stebila (Sun Microsystems Laboratories)] *) Let BN_rand_range() abort with an error after 100 iterations without success (which indicates a broken PRNG). [Bodo Moeller] *) Change BN_mod_sqrt() so that it verifies that the input value is really the square of the return value. (Previously, BN_mod_sqrt would show GIGO behaviour.) [Bodo Moeller] *) Add named elliptic curves over binary fields from X9.62, SECG, and WAP/WTLS; add OIDs that were still missing. [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) Extend the EC library for elliptic curves over binary fields (new files ec2_smpl.c, ec2_smpt.c, ec2_mult.c in crypto/ec/). New EC_METHOD: EC_GF2m_simple_method New API functions: EC_GROUP_new_curve_GF2m EC_GROUP_set_curve_GF2m EC_GROUP_get_curve_GF2m EC_POINT_set_affine_coordinates_GF2m EC_POINT_get_affine_coordinates_GF2m EC_POINT_set_compressed_coordinates_GF2m Point compression for binary fields is disabled by default for patent reasons (compile with OPENSSL_EC_BIN_PT_COMP defined to enable it). As binary polynomials are represented as BIGNUMs, various members of the EC_GROUP and EC_POINT data structures can be shared between the implementations for prime fields and binary fields; the above ..._GF2m functions (except for EX_GROUP_new_curve_GF2m) are essentially identical to their ..._GFp counterparts. (For simplicity, the '..._GFp' prefix has been dropped from various internal method names.) An internal 'field_div' method (similar to 'field_mul' and 'field_sqr') has been added; this is used only for binary fields. [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) Optionally dispatch EC_POINT_mul(), EC_POINT_precompute_mult() through methods ('mul', 'precompute_mult'). The generic implementations (now internally called 'ec_wNAF_mul' and 'ec_wNAF_precomputed_mult') remain the default if these methods are undefined. [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) New function EC_GROUP_get_degree, which is defined through EC_METHOD. For curves over prime fields, this returns the bit length of the modulus. [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) New functions EC_GROUP_dup, EC_POINT_dup. (These simply call ..._new and ..._copy). [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) Add binary polynomial arithmetic software in crypto/bn/bn_gf2m.c. Polynomials are represented as BIGNUMs (where the sign bit is not used) in the following functions [macros]: BN_GF2m_add BN_GF2m_sub [= BN_GF2m_add] BN_GF2m_mod [wrapper for BN_GF2m_mod_arr] BN_GF2m_mod_mul [wrapper for BN_GF2m_mod_mul_arr] BN_GF2m_mod_sqr [wrapper for BN_GF2m_mod_sqr_arr] BN_GF2m_mod_inv BN_GF2m_mod_exp [wrapper for BN_GF2m_mod_exp_arr] BN_GF2m_mod_sqrt [wrapper for BN_GF2m_mod_sqrt_arr] BN_GF2m_mod_solve_quad [wrapper for BN_GF2m_mod_solve_quad_arr] BN_GF2m_cmp [= BN_ucmp] (Note that only the 'mod' functions are actually for fields GF(2^m). BN_GF2m_add() is misnomer, but this is for the sake of consistency.) For some functions, an the irreducible polynomial defining a field can be given as an 'unsigned int[]' with strictly decreasing elements giving the indices of those bits that are set; i.e., p[] represents the polynomial f(t) = t^p[0] + t^p[1] + ... + t^p[k] where p[0] > p[1] > ... > p[k] = 0. This applies to the following functions: BN_GF2m_mod_arr BN_GF2m_mod_mul_arr BN_GF2m_mod_sqr_arr BN_GF2m_mod_inv_arr [wrapper for BN_GF2m_mod_inv] BN_GF2m_mod_div_arr [wrapper for BN_GF2m_mod_div] BN_GF2m_mod_exp_arr BN_GF2m_mod_sqrt_arr BN_GF2m_mod_solve_quad_arr BN_GF2m_poly2arr BN_GF2m_arr2poly Conversion can be performed by the following functions: BN_GF2m_poly2arr BN_GF2m_arr2poly bntest.c has additional tests for binary polynomial arithmetic. Two implementations for BN_GF2m_mod_div() are available. The default algorithm simply uses BN_GF2m_mod_inv() and BN_GF2m_mod_mul(). The alternative algorithm is compiled in only if OPENSSL_SUN_GF2M_DIV is defined (patent pending; read the copyright notice in crypto/bn/bn_gf2m.c before enabling it). [Sheueling Chang Shantz and Douglas Stebila (Sun Microsystems Laboratories)] *) Add new error code 'ERR_R_DISABLED' that can be used when some functionality is disabled at compile-time. [Douglas Stebila ] *) Change default behaviour of 'openssl asn1parse' so that more information is visible when viewing, e.g., a certificate: Modify asn1_parse2 (crypto/asn1/asn1_par.c) so that in non-'dump' mode the content of non-printable OCTET STRINGs is output in a style similar to INTEGERs, but with '[HEX DUMP]' prepended to avoid the appearance of a printable string. [Nils Larsch ] *) Add 'asn1_flag' and 'asn1_form' member to EC_GROUP with access functions EC_GROUP_set_asn1_flag() EC_GROUP_get_asn1_flag() EC_GROUP_set_point_conversion_form() EC_GROUP_get_point_conversion_form() These control ASN1 encoding details: - Curves (i.e., groups) are encoded explicitly unless asn1_flag has been set to OPENSSL_EC_NAMED_CURVE. - Points are encoded in uncompressed form by default; options for asn1_for are as for point2oct, namely POINT_CONVERSION_COMPRESSED POINT_CONVERSION_UNCOMPRESSED POINT_CONVERSION_HYBRID Also add 'seed' and 'seed_len' members to EC_GROUP with access functions EC_GROUP_set_seed() EC_GROUP_get0_seed() EC_GROUP_get_seed_len() This is used only for ASN1 purposes (so far). [Nils Larsch ] *) Add 'field_type' member to EC_METHOD, which holds the NID of the appropriate field type OID. The new function EC_METHOD_get_field_type() returns this value. [Nils Larsch ] *) Add functions EC_POINT_point2bn() EC_POINT_bn2point() EC_POINT_point2hex() EC_POINT_hex2point() providing useful interfaces to EC_POINT_point2oct() and EC_POINT_oct2point(). [Nils Larsch ] *) Change internals of the EC library so that the functions EC_GROUP_set_generator() EC_GROUP_get_generator() EC_GROUP_get_order() EC_GROUP_get_cofactor() are implemented directly in crypto/ec/ec_lib.c and not dispatched to methods, which would lead to unnecessary code duplication when adding different types of curves. [Nils Larsch with input by Bodo Moeller] *) Implement compute_wNAF (crypto/ec/ec_mult.c) without BIGNUM arithmetic, and such that modified wNAFs are generated (which avoid length expansion in many cases). [Bodo Moeller] *) Add a function EC_GROUP_check_discriminant() (defined via EC_METHOD) that verifies that the curve discriminant is non-zero. Add a function EC_GROUP_check() that makes some sanity tests on a EC_GROUP, its generator and order. This includes EC_GROUP_check_discriminant(). [Nils Larsch ] *) Add ECDSA in new directory crypto/ecdsa/. Add applications 'openssl ecparam' and 'openssl ecdsa' (these are based on 'openssl dsaparam' and 'openssl dsa'). ECDSA support is also included in various other files across the library. Most notably, - 'openssl req' now has a '-newkey ecdsa:file' option; - EVP_PKCS82PKEY (crypto/evp/evp_pkey.c) now can handle ECDSA; - X509_PUBKEY_get (crypto/asn1/x_pubkey.c) and d2i_PublicKey (crypto/asn1/d2i_pu.c) have been modified to make them suitable for ECDSA where domain parameters must be extracted before the specific public key; - ECDSA engine support has been added. [Nils Larsch ] *) Include some named elliptic curves, and add OIDs from X9.62, SECG, and WAP/WTLS. Each curve can be obtained from the new function EC_GROUP_new_by_curve_name(), and the list of available named curves can be obtained with EC_get_builtin_curves(). Also add a 'curve_name' member to EC_GROUP objects, which can be accessed via EC_GROUP_set_curve_name() EC_GROUP_get_curve_name() [Nils Larsch ] *) Include "!eNULL" in SSL_DEFAULT_CIPHER_LIST to make sure that a ciphersuite string such as "DEFAULT:RSA" cannot enable authentication-only ciphersuites. [Bodo Moeller] *) Since AES128 and AES256 share a single mask bit in the logic of ssl/ssl_ciph.c, the code for masking out disabled ciphers needs a kludge to work properly if AES128 is available and AES256 isn't. [Victor Duchovni] *) Expand security boundary to match 1.1.1 module. [Steve Henson] *) Remove redundant features: hash file source, editing of test vectors modify fipsld to use external fips_premain.c signature. [Steve Henson] *) New perl script mkfipsscr.pl to create shell scripts or batch files to run algorithm test programs. [Steve Henson] *) Make algorithm test programs more tolerant of whitespace. [Steve Henson] *) Have SSL/TLS server implementation tolerate "mismatched" record protocol version while receiving ClientHello even if the ClientHello is fragmented. (The server can't insist on the particular protocol version it has chosen before the ServerHello message has informed the client about his choice.) [Bodo Moeller] *) Load error codes if they are not already present instead of using a static variable. This allows them to be cleanly unloaded and reloaded. [Steve Henson] Changes between 0.9.7k and 0.9.7l [28 Sep 2006] *) Introduce limits to prevent malicious keys being able to cause a denial of service. (CVE-2006-2940) [Steve Henson, Bodo Moeller] *) Fix ASN.1 parsing of certain invalid structures that can result in a denial of service. (CVE-2006-2937) [Steve Henson] *) Fix buffer overflow in SSL_get_shared_ciphers() function. (CVE-2006-3738) [Tavis Ormandy and Will Drewry, Google Security Team] *) Fix SSL client code which could crash if connecting to a malicious SSLv2 server. (CVE-2006-4343) [Tavis Ormandy and Will Drewry, Google Security Team] *) Change ciphersuite string processing so that an explicit ciphersuite selects this one ciphersuite (so that "AES256-SHA" will no longer include "AES128-SHA"), and any other similar ciphersuite (same bitmap) from *other* protocol versions (so that "RC4-MD5" will still include both the SSL 2.0 ciphersuite and the SSL 3.0/TLS 1.0 ciphersuite). This is a backport combining changes from 0.9.8b and 0.9.8d. [Bodo Moeller] Changes between 0.9.7j and 0.9.7k [05 Sep 2006] *) Avoid PKCS #1 v1.5 signature attack discovered by Daniel Bleichenbacher (CVE-2006-4339) [Ben Laurie and Google Security Team] *) Change the Unix randomness entropy gathering to use poll() when possible instead of select(), since the latter has some undesirable limitations. [Darryl Miles via Richard Levitte and Bodo Moeller] *) Disable rogue ciphersuites: - SSLv2 0x08 0x00 0x80 ("RC4-64-MD5") - SSLv3/TLSv1 0x00 0x61 ("EXP1024-RC2-CBC-MD5") - SSLv3/TLSv1 0x00 0x60 ("EXP1024-RC4-MD5") The latter two were purportedly from draft-ietf-tls-56-bit-ciphersuites-0[01].txt, but do not really appear there. Also deactive the remaining ciphersuites from draft-ietf-tls-56-bit-ciphersuites-01.txt. These are just as unofficial, and the ID has long expired. [Bodo Moeller] *) Fix RSA blinding Heisenbug (problems sometimes occured on dual-core machines) and other potential thread-safety issues. [Bodo Moeller] Changes between 0.9.7i and 0.9.7j [04 May 2006] *) Adapt fipsld and the build system to link against the validated FIPS module in FIPS mode. [Steve Henson] *) Fixes for VC++ 2005 build under Windows. [Steve Henson] *) Add new Windows build target VC-32-GMAKE for VC++. This uses GNU make from a Windows bash shell such as MSYS. It is autodetected from the "config" script when run from a VC++ environment. Modify standard VC++ build to use fipscanister.o from the GNU make build. [Steve Henson] Changes between 0.9.7h and 0.9.7i [14 Oct 2005] *) Wrapped the definition of EVP_MAX_MD_SIZE in a #ifdef OPENSSL_FIPS. The value now differs depending on if you build for FIPS or not. BEWARE! A program linked with a shared FIPSed libcrypto can't be safely run with a non-FIPSed libcrypto, as it may crash because of the difference induced by this change. [Andy Polyakov] Changes between 0.9.7g and 0.9.7h [11 Oct 2005] *) Remove the functionality of SSL_OP_MSIE_SSLV2_RSA_PADDING (part of SSL_OP_ALL). This option used to disable the countermeasure against man-in-the-middle protocol-version rollback in the SSL 2.0 server implementation, which is a bad idea. (CVE-2005-2969) [Bodo Moeller; problem pointed out by Yutaka Oiwa (Research Center for Information Security, National Institute of Advanced Industrial Science and Technology [AIST], Japan)] *) Minimal support for X9.31 signatures and PSS padding modes. This is mainly for FIPS compliance and not fully integrated at this stage. [Steve Henson] *) For DSA signing, unless DSA_FLAG_NO_EXP_CONSTTIME is set, perform the exponentiation using a fixed-length exponent. (Otherwise, the information leaked through timing could expose the secret key after many signatures; cf. Bleichenbacher's attack on DSA with biased k.) [Bodo Moeller] *) Make a new fixed-window mod_exp implementation the default for RSA, DSA, and DH private-key operations so that the sequence of squares and multiplies and the memory access pattern are independent of the particular secret key. This will mitigate cache-timing and potential related attacks. BN_mod_exp_mont_consttime() is the new exponentiation implementation, and this is automatically used by BN_mod_exp_mont() if the new flag BN_FLG_EXP_CONSTTIME is set for the exponent. RSA, DSA, and DH will use this BN flag for private exponents unless the flag RSA_FLAG_NO_EXP_CONSTTIME, DSA_FLAG_NO_EXP_CONSTTIME, or DH_FLAG_NO_EXP_CONSTTIME, respectively, is set. [Matthew D Wood (Intel Corp), with some changes by Bodo Moeller] *) Change the client implementation for SSLv23_method() and SSLv23_client_method() so that is uses the SSL 3.0/TLS 1.0 Client Hello message format if the SSL_OP_NO_SSLv2 option is set. (Previously, the SSL 2.0 backwards compatible Client Hello message format would be used even with SSL_OP_NO_SSLv2.) [Bodo Moeller] *) Add support for smime-type MIME parameter in S/MIME messages which some clients need. [Steve Henson] *) New function BN_MONT_CTX_set_locked() to set montgomery parameters in a threadsafe manner. Modify rsa code to use new function and add calls to dsa and dh code (which had race conditions before). [Steve Henson] *) Include the fixed error library code in the C error file definitions instead of fixing them up at runtime. This keeps the error code structures constant. [Steve Henson] Changes between 0.9.7f and 0.9.7g [11 Apr 2005] [NB: OpenSSL 0.9.7h and later 0.9.7 patch levels were released after OpenSSL 0.9.8.] *) Fixes for newer kerberos headers. NB: the casts are needed because the 'length' field is signed on one version and unsigned on another with no (?) obvious way to tell the difference, without these VC++ complains. Also the "definition" of FAR (blank) is no longer included nor is the error ENOMEM. KRB5_PRIVATE has to be set to 1 to pick up some needed definitions. [Steve Henson] *) Undo Cygwin change. [Ulf Möller] *) Added support for proxy certificates according to RFC 3820. Because they may be a security thread to unaware applications, they must be explicitely allowed in run-time. See docs/HOWTO/proxy_certificates.txt for further information. [Richard Levitte] Changes between 0.9.7e and 0.9.7f [22 Mar 2005] *) Use (SSL_RANDOM_VALUE - 4) bytes of pseudo random data when generating server and client random values. Previously (SSL_RANDOM_VALUE - sizeof(time_t)) would be used which would result in less random data when sizeof(time_t) > 4 (some 64 bit platforms). This change has negligible security impact because: 1. Server and client random values still have 24 bytes of pseudo random data. 2. Server and client random values are sent in the clear in the initial handshake. 3. The master secret is derived using the premaster secret (48 bytes in size for static RSA ciphersuites) as well as client server and random values. The OpenSSL team would like to thank the UK NISCC for bringing this issue to our attention. [Stephen Henson, reported by UK NISCC] *) Use Windows randomness collection on Cygwin. [Ulf Möller] *) Fix hang in EGD/PRNGD query when communication socket is closed prematurely by EGD/PRNGD. [Darren Tucker via Lutz Jänicke, resolves #1014] *) Prompt for pass phrases when appropriate for PKCS12 input format. [Steve Henson] *) Back-port of selected performance improvements from development branch, as well as improved support for PowerPC platforms. [Andy Polyakov] *) Add lots of checks for memory allocation failure, error codes to indicate failure and freeing up memory if a failure occurs. [Nauticus Networks SSL Team , Steve Henson] *) Add new -passin argument to dgst. [Steve Henson] *) Perform some character comparisons of different types in X509_NAME_cmp: this is needed for some certificates that reencode DNs into UTF8Strings (in violation of RFC3280) and can't or wont issue name rollover certificates. [Steve Henson] *) Make an explicit check during certificate validation to see that the CA setting in each certificate on the chain is correct. As a side effect always do the following basic checks on extensions, not just when there's an associated purpose to the check: - if there is an unhandled critical extension (unless the user has chosen to ignore this fault) - if the path length has been exceeded (if one is set at all) - that certain extensions fit the associated purpose (if one has been given) [Richard Levitte] Changes between 0.9.7d and 0.9.7e [25 Oct 2004] *) Avoid a race condition when CRLs are checked in a multi threaded environment. This would happen due to the reordering of the revoked entries during signature checking and serial number lookup. Now the encoding is cached and the serial number sort performed under a lock. Add new STACK function sk_is_sorted(). [Steve Henson] *) Add Delta CRL to the extension code. [Steve Henson] *) Various fixes to s3_pkt.c so alerts are sent properly. [David Holmes ] *) Reduce the chances of duplicate issuer name and serial numbers (in violation of RFC3280) using the OpenSSL certificate creation utilities. This is done by creating a random 64 bit value for the initial serial number when a serial number file is created or when a self signed certificate is created using 'openssl req -x509'. The initial serial number file is created using 'openssl x509 -next_serial' in CA.pl rather than being initialized to 1. [Steve Henson] Changes between 0.9.7c and 0.9.7d [17 Mar 2004] *) Fix null-pointer assignment in do_change_cipher_spec() revealed by using the Codenomicon TLS Test Tool (CVE-2004-0079) [Joe Orton, Steve Henson] *) Fix flaw in SSL/TLS handshaking when using Kerberos ciphersuites (CVE-2004-0112) [Joe Orton, Steve Henson] *) Make it possible to have multiple active certificates with the same subject in the CA index file. This is done only if the keyword 'unique_subject' is set to 'no' in the main CA section (default if 'CA_default') of the configuration file. The value is saved with the database itself in a separate index attribute file, named like the index file with '.attr' appended to the name. [Richard Levitte] *) X509 verify fixes. Disable broken certificate workarounds when X509_V_FLAGS_X509_STRICT is set. Check CRL issuer has cRLSign set if keyUsage extension present. Don't accept CRLs with unhandled critical extensions: since verify currently doesn't process CRL extensions this rejects a CRL with *any* critical extensions. Add new verify error codes for these cases. [Steve Henson] *) When creating an OCSP nonce use an OCTET STRING inside the extnValue. A clarification of RFC2560 will require the use of OCTET STRINGs and some implementations cannot handle the current raw format. Since OpenSSL copies and compares OCSP nonces as opaque blobs without any attempt at parsing them this should not create any compatibility issues. [Steve Henson] *) New md flag EVP_MD_CTX_FLAG_REUSE this allows md_data to be reused when calling EVP_MD_CTX_copy_ex() to avoid calling OPENSSL_malloc(). Without this HMAC (and other) operations are several times slower than OpenSSL < 0.9.7. [Steve Henson] *) Print out GeneralizedTime and UTCTime in ASN1_STRING_print_ex(). [Peter Sylvester ] *) Use the correct content when signing type "other". [Steve Henson] Changes between 0.9.7b and 0.9.7c [30 Sep 2003] *) Fix various bugs revealed by running the NISCC test suite: Stop out of bounds reads in the ASN1 code when presented with invalid tags (CVE-2003-0543 and CVE-2003-0544). Free up ASN1_TYPE correctly if ANY type is invalid (CVE-2003-0545). If verify callback ignores invalid public key errors don't try to check certificate signature with the NULL public key. [Steve Henson] *) New -ignore_err option in ocsp application to stop the server exiting on the first error in a request. [Steve Henson] *) In ssl3_accept() (ssl/s3_srvr.c) only accept a client certificate if the server requested one: as stated in TLS 1.0 and SSL 3.0 specifications. [Steve Henson] *) In ssl3_get_client_hello() (ssl/s3_srvr.c), tolerate additional extra data after the compression methods not only for TLS 1.0 but also for SSL 3.0 (as required by the specification). [Bodo Moeller; problem pointed out by Matthias Loepfe] *) Change X509_certificate_type() to mark the key as exported/exportable when it's 512 *bits* long, not 512 bytes. [Richard Levitte] *) Change AES_cbc_encrypt() so it outputs exact multiple of blocks during encryption. [Richard Levitte] *) Various fixes to base64 BIO and non blocking I/O. On write flushes were not handled properly if the BIO retried. On read data was not being buffered properly and had various logic bugs. This also affects blocking I/O when the data being decoded is a certain size. [Steve Henson] *) Various S/MIME bugfixes and compatibility changes: output correct application/pkcs7 MIME type if PKCS7_NOOLDMIMETYPE is set. Tolerate some broken signatures. Output CR+LF for EOL if PKCS7_CRLFEOL is set (this makes opening of files as .eml work). Correctly handle very long lines in MIME parser. [Steve Henson] Changes between 0.9.7a and 0.9.7b [10 Apr 2003] *) Countermeasure against the Klima-Pokorny-Rosa extension of Bleichbacher's attack on PKCS #1 v1.5 padding: treat a protocol version number mismatch like a decryption error in ssl3_get_client_key_exchange (ssl/s3_srvr.c). [Bodo Moeller] *) Turn on RSA blinding by default in the default implementation to avoid a timing attack. Applications that don't want it can call RSA_blinding_off() or use the new flag RSA_FLAG_NO_BLINDING. They would be ill-advised to do so in most cases. [Ben Laurie, Steve Henson, Geoff Thorpe, Bodo Moeller] *) Change RSA blinding code so that it works when the PRNG is not seeded (in this case, the secret RSA exponent is abused as an unpredictable seed -- if it is not unpredictable, there is no point in blinding anyway). Make RSA blinding thread-safe by remembering the creator's thread ID in rsa->blinding and having all other threads use local one-time blinding factors (this requires more computation than sharing rsa->blinding, but avoids excessive locking; and if an RSA object is not shared between threads, blinding will still be very fast). [Bodo Moeller] *) Fixed a typo bug that would cause ENGINE_set_default() to set an ENGINE as defaults for all supported algorithms irrespective of the 'flags' parameter. 'flags' is now honoured, so applications should make sure they are passing it correctly. [Geoff Thorpe] *) Target "mingw" now allows native Windows code to be generated in the Cygwin environment as well as with the MinGW compiler. [Ulf Moeller] Changes between 0.9.7 and 0.9.7a [19 Feb 2003] *) In ssl3_get_record (ssl/s3_pkt.c), minimize information leaked via timing by performing a MAC computation even if incorrrect block cipher padding has been found. This is a countermeasure against active attacks where the attacker has to distinguish between bad padding and a MAC verification error. (CVE-2003-0078) [Bodo Moeller; problem pointed out by Brice Canvel (EPFL), Alain Hiltgen (UBS), Serge Vaudenay (EPFL), and Martin Vuagnoux (EPFL, Ilion)] *) Make the no-err option work as intended. The intention with no-err is not to have the whole error stack handling routines removed from libcrypto, it's only intended to remove all the function name and reason texts, thereby removing some of the footprint that may not be interesting if those errors aren't displayed anyway. NOTE: it's still possible for any application or module to have it's own set of error texts inserted. The routines are there, just not used by default when no-err is given. [Richard Levitte] *) Add support for FreeBSD on IA64. [dirk.meyer@dinoex.sub.org via Richard Levitte, resolves #454] *) Adjust DES_cbc_cksum() so it returns the same value as the MIT Kerberos function mit_des_cbc_cksum(). Before this change, the value returned by DES_cbc_cksum() was like the one from mit_des_cbc_cksum(), except the bytes were swapped. [Kevin Greaney and Richard Levitte] *) Allow an application to disable the automatic SSL chain building. Before this a rather primitive chain build was always performed in ssl3_output_cert_chain(): an application had no way to send the correct chain if the automatic operation produced an incorrect result. Now the chain builder is disabled if either: 1. Extra certificates are added via SSL_CTX_add_extra_chain_cert(). 2. The mode flag SSL_MODE_NO_AUTO_CHAIN is set. The reasoning behind this is that an application would not want the auto chain building to take place if extra chain certificates are present and it might also want a means of sending no additional certificates (for example the chain has two certificates and the root is omitted). [Steve Henson] *) Add the possibility to build without the ENGINE framework. [Steven Reddie via Richard Levitte] *) Under Win32 gmtime() can return NULL: check return value in OPENSSL_gmtime(). Add error code for case where gmtime() fails. [Steve Henson] *) DSA routines: under certain error conditions uninitialized BN objects could be freed. Solution: make sure initialization is performed early enough. (Reported and fix supplied by Ivan D Nestlerode , Nils Larsch via PR#459) [Lutz Jaenicke] *) Another fix for SSLv2 session ID handling: the session ID was incorrectly checked on reconnect on the client side, therefore session resumption could still fail with a "ssl session id is different" error. This behaviour is masked when SSL_OP_ALL is used due to SSL_OP_MICROSOFT_SESS_ID_BUG being set. Behaviour observed by Crispin Flowerday as followup to PR #377. [Lutz Jaenicke] *) IA-32 assembler support enhancements: unified ELF targets, support for SCO/Caldera platforms, fix for Cygwin shared build. [Andy Polyakov] *) Add support for FreeBSD on sparc64. As a consequence, support for FreeBSD on non-x86 processors is separate from x86 processors on the config script, much like the NetBSD support. [Richard Levitte & Kris Kennaway ] Changes between 0.9.6h and 0.9.7 [31 Dec 2002] [NB: OpenSSL 0.9.6i and later 0.9.6 patch levels were released after OpenSSL 0.9.7.] *) Fix session ID handling in SSLv2 client code: the SERVER FINISHED code (06) was taken as the first octet of the session ID and the last octet was ignored consequently. As a result SSLv2 client side session caching could not have worked due to the session ID mismatch between client and server. Behaviour observed by Crispin Flowerday as PR #377. [Lutz Jaenicke] *) Change the declaration of needed Kerberos libraries to use EX_LIBS instead of the special (and badly supported) LIBKRB5. LIBKRB5 is removed entirely. [Richard Levitte] *) The hw_ncipher.c engine requires dynamic locks. Unfortunately, it seems that in spite of existing for more than a year, many application author have done nothing to provide the necessary callbacks, which means that this particular engine will not work properly anywhere. This is a very unfortunate situation which forces us, in the name of usability, to give the hw_ncipher.c a static lock, which is part of libcrypto. NOTE: This is for the 0.9.7 series ONLY. This hack will never appear in 0.9.8 or later. We EXPECT application authors to have dealt properly with this when 0.9.8 is released (unless we actually make such changes in the libcrypto locking code that changes will have to be made anyway). [Richard Levitte] *) In asn1_d2i_read_bio() repeatedly call BIO_read() until all content octets have been read, EOF or an error occurs. Without this change some truncated ASN1 structures will not produce an error. [Steve Henson] *) Disable Heimdal support, since it hasn't been fully implemented. Still give the possibility to force the use of Heimdal, but with warnings and a request that patches get sent to openssl-dev. [Richard Levitte] *) Add the VC-CE target, introduce the WINCE sysname, and add INSTALL.WCE and appropriate conditionals to make it build. [Steven Reddie via Richard Levitte] *) Change the DLL names for Cygwin to cygcrypto-x.y.z.dll and cygssl-x.y.z.dll, where x, y and z are the major, minor and edit numbers of the version. [Corinna Vinschen and Richard Levitte] *) Introduce safe string copy and catenation functions (BUF_strlcpy() and BUF_strlcat()). [Ben Laurie (CHATS) and Richard Levitte] *) Avoid using fixed-size buffers for one-line DNs. [Ben Laurie (CHATS)] *) Add BUF_MEM_grow_clean() to avoid information leakage when resizing buffers containing secrets, and use where appropriate. [Ben Laurie (CHATS)] *) Avoid using fixed size buffers for configuration file location. [Ben Laurie (CHATS)] *) Avoid filename truncation for various CA files. [Ben Laurie (CHATS)] *) Use sizeof in preference to magic numbers. [Ben Laurie (CHATS)] *) Avoid filename truncation in cert requests. [Ben Laurie (CHATS)] *) Add assertions to check for (supposedly impossible) buffer overflows. [Ben Laurie (CHATS)] *) Don't cache truncated DNS entries in the local cache (this could potentially lead to a spoofing attack). [Ben Laurie (CHATS)] *) Fix various buffers to be large enough for hex/decimal representations in a platform independent manner. [Ben Laurie (CHATS)] *) Add CRYPTO_realloc_clean() to avoid information leakage when resizing buffers containing secrets, and use where appropriate. [Ben Laurie (CHATS)] *) Add BIO_indent() to avoid much slightly worrying code to do indents. [Ben Laurie (CHATS)] *) Convert sprintf()/BIO_puts() to BIO_printf(). [Ben Laurie (CHATS)] *) buffer_gets() could terminate with the buffer only half full. Fixed. [Ben Laurie (CHATS)] *) Add assertions to prevent user-supplied crypto functions from overflowing internal buffers by having large block sizes, etc. [Ben Laurie (CHATS)] *) New OPENSSL_assert() macro (similar to assert(), but enabled unconditionally). [Ben Laurie (CHATS)] *) Eliminate unused copy of key in RC4. [Ben Laurie (CHATS)] *) Eliminate unused and incorrectly sized buffers for IV in pem.h. [Ben Laurie (CHATS)] *) Fix off-by-one error in EGD path. [Ben Laurie (CHATS)] *) If RANDFILE path is too long, ignore instead of truncating. [Ben Laurie (CHATS)] *) Eliminate unused and incorrectly sized X.509 structure CBCParameter. [Ben Laurie (CHATS)] *) Eliminate unused and dangerous function knumber(). [Ben Laurie (CHATS)] *) Eliminate unused and dangerous structure, KSSL_ERR. [Ben Laurie (CHATS)] *) Protect against overlong session ID context length in an encoded session object. Since these are local, this does not appear to be exploitable. [Ben Laurie (CHATS)] *) Change from security patch (see 0.9.6e below) that did not affect the 0.9.6 release series: Remote buffer overflow in SSL3 protocol - an attacker could supply an oversized master key in Kerberos-enabled versions. (CVE-2002-0657) [Ben Laurie (CHATS)] *) Change the SSL kerb5 codes to match RFC 2712. [Richard Levitte] *) Make -nameopt work fully for req and add -reqopt switch. [Michael Bell , Steve Henson] *) The "block size" for block ciphers in CFB and OFB mode should be 1. [Steve Henson, reported by Yngve Nysaeter Pettersen ] *) Make sure tests can be performed even if the corresponding algorithms have been removed entirely. This was also the last step to make OpenSSL compilable with DJGPP under all reasonable conditions. [Richard Levitte, Doug Kaufman ] *) Add cipher selection rules COMPLEMENTOFALL and COMPLEMENTOFDEFAULT to allow version independent disabling of normally unselected ciphers, which may be activated as a side-effect of selecting a single cipher. (E.g., cipher list string "RSA" enables ciphersuites that are left out of "ALL" because they do not provide symmetric encryption. "RSA:!COMPLEMEMENTOFALL" avoids these unsafe ciphersuites.) [Lutz Jaenicke, Bodo Moeller] *) Add appropriate support for separate platform-dependent build directories. The recommended way to make a platform-dependent build directory is the following (tested on Linux), maybe with some local tweaks: # Place yourself outside of the OpenSSL source tree. In # this example, the environment variable OPENSSL_SOURCE # is assumed to contain the absolute OpenSSL source directory. mkdir -p objtree/"`uname -s`-`uname -r`-`uname -m`" cd objtree/"`uname -s`-`uname -r`-`uname -m`" (cd $OPENSSL_SOURCE; find . -type f) | while read F; do mkdir -p `dirname $F` ln -s $OPENSSL_SOURCE/$F $F done To be absolutely sure not to disturb the source tree, a "make clean" is a good thing. If it isn't successfull, don't worry about it, it probably means the source directory is very clean. [Richard Levitte] *) Make sure any ENGINE control commands make local copies of string pointers passed to them whenever necessary. Otherwise it is possible the caller may have overwritten (or deallocated) the original string data when a later ENGINE operation tries to use the stored values. [Götz Babin-Ebell ] *) Improve diagnostics in file reading and command-line digests. [Ben Laurie aided and abetted by Solar Designer ] *) Add AES modes CFB and OFB to the object database. Correct an error in AES-CFB decryption. [Richard Levitte] *) Remove most calls to EVP_CIPHER_CTX_cleanup() in evp_enc.c, this allows existing EVP_CIPHER_CTX structures to be reused after calling EVP_*Final(). This behaviour is used by encryption BIOs and some applications. This has the side effect that applications must explicitly clean up cipher contexts with EVP_CIPHER_CTX_cleanup() or they will leak memory. [Steve Henson] *) Check the values of dna and dnb in bn_mul_recursive before calling bn_mul_comba (a non zero value means the a or b arrays do not contain n2 elements) and fallback to bn_mul_normal if either is not zero. [Steve Henson] *) Fix escaping of non-ASCII characters when using the -subj option of the "openssl req" command line tool. (Robert Joop ) [Lutz Jaenicke] *) Make object definitions compliant to LDAP (RFC2256): SN is the short form for "surname", serialNumber has no short form. Use "mail" as the short name for "rfc822Mailbox" according to RFC2798; therefore remove "mail" short name for "internet 7". The OID for unique identifiers in X509 certificates is x500UniqueIdentifier, not uniqueIdentifier. Some more OID additions. (Michael Bell ) [Lutz Jaenicke] *) Add an "init" command to the ENGINE config module and auto initialize ENGINEs. Without any "init" command the ENGINE will be initialized after all ctrl commands have been executed on it. If init=1 the ENGINE is initailized at that point (ctrls before that point are run on the uninitialized ENGINE and after on the initialized one). If init=0 then the ENGINE will not be iniatialized at all. [Steve Henson] *) Fix the 'app_verify_callback' interface so that the user-defined argument is actually passed to the callback: In the SSL_CTX_set_cert_verify_callback() prototype, the callback declaration has been changed from int (*cb)() into int (*cb)(X509_STORE_CTX *,void *); in ssl_verify_cert_chain (ssl/ssl_cert.c), the call i=s->ctx->app_verify_callback(&ctx) has been changed into i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg). To update applications using SSL_CTX_set_cert_verify_callback(), a dummy argument can be added to their callback functions. [D. K. Smetters ] *) Added the '4758cca' ENGINE to support IBM 4758 cards. [Maurice Gittens , touchups by Geoff Thorpe] *) Add and OPENSSL_LOAD_CONF define which will cause OpenSSL_add_all_algorithms() to load the openssl.cnf config file. This allows older applications to transparently support certain OpenSSL features: such as crypto acceleration and dynamic ENGINE loading. Two new functions OPENSSL_add_all_algorithms_noconf() which will never load the config file and OPENSSL_add_all_algorithms_conf() which will always load it have also been added. [Steve Henson] *) Add the OFB, CFB and CTR (all with 128 bit feedback) to AES. Adjust NIDs and EVP layer. [Stephen Sprunk and Richard Levitte] *) Config modules support in openssl utility. Most commands now load modules from the config file, though in a few (such as version) this isn't done because it couldn't be used for anything. In the case of ca and req the config file used is the same as the utility itself: that is the -config command line option can be used to specify an alternative file. [Steve Henson] *) Move default behaviour from OPENSSL_config(). If appname is NULL use "openssl_conf" if filename is NULL use default openssl config file. [Steve Henson] *) Add an argument to OPENSSL_config() to allow the use of an alternative config section name. Add a new flag to tolerate a missing config file and move code to CONF_modules_load_file(). [Steve Henson] *) Support for crypto accelerator cards from Accelerated Encryption Processing, www.aep.ie. (Use engine 'aep') The support was copied from 0.9.6c [engine] and adapted/corrected to work with the new engine framework. [AEP Inc. and Richard Levitte] *) Support for SureWare crypto accelerator cards from Baltimore Technologies. (Use engine 'sureware') The support was copied from 0.9.6c [engine] and adapted to work with the new engine framework. [Richard Levitte] *) Have the CHIL engine fork-safe (as defined by nCipher) and actually make the newer ENGINE framework commands for the CHIL engine work. [Toomas Kiisk and Richard Levitte] *) Make it possible to produce shared libraries on ReliantUNIX. [Robert Dahlem via Richard Levitte] *) Add the configuration target debug-linux-ppro. Make 'openssl rsa' use the general key loading routines implemented in apps.c, and make those routines able to handle the key format FORMAT_NETSCAPE and the variant FORMAT_IISSGC. [Toomas Kiisk via Richard Levitte] *) Fix a crashbug and a logic bug in hwcrhk_load_pubkey(). [Toomas Kiisk via Richard Levitte] *) Add -keyform to rsautl, and document -engine. [Richard Levitte, inspired by Toomas Kiisk ] *) Change BIO_new_file (crypto/bio/bss_file.c) to use new BIO_R_NO_SUCH_FILE error code rather than the generic ERR_R_SYS_LIB error code if fopen() fails with ENOENT. [Ben Laurie] *) Add new functions ERR_peek_last_error ERR_peek_last_error_line ERR_peek_last_error_line_data. These are similar to ERR_peek_error ERR_peek_error_line ERR_peek_error_line_data, but report on the latest error recorded rather than the first one still in the error queue. [Ben Laurie, Bodo Moeller] *) default_algorithms option in ENGINE config module. This allows things like: default_algorithms = ALL default_algorithms = RSA, DSA, RAND, CIPHERS, DIGESTS [Steve Henson] *) Prelminary ENGINE config module. [Steve Henson] *) New experimental application configuration code. [Steve Henson] *) Change the AES code to follow the same name structure as all other symmetric ciphers, and behave the same way. Move everything to the directory crypto/aes, thereby obsoleting crypto/rijndael. [Stephen Sprunk and Richard Levitte] *) SECURITY: remove unsafe setjmp/signal interaction from ui_openssl.c. [Ben Laurie and Theo de Raadt] *) Add option to output public keys in req command. [Massimiliano Pala madwolf@openca.org] *) Use wNAFs in EC_POINTs_mul() for improved efficiency (up to about 10% better than before for P-192 and P-224). [Bodo Moeller] *) New functions/macros SSL_CTX_set_msg_callback(ctx, cb) SSL_CTX_set_msg_callback_arg(ctx, arg) SSL_set_msg_callback(ssl, cb) SSL_set_msg_callback_arg(ssl, arg) to request calling a callback function void cb(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) whenever a protocol message has been completely received (write_p == 0) or sent (write_p == 1). Here 'version' is the protocol version according to which the SSL library interprets the current protocol message (SSL2_VERSION, SSL3_VERSION, or TLS1_VERSION). 'content_type' is 0 in the case of SSL 2.0, or the content type as defined in the SSL 3.0/TLS 1.0 protocol specification (change_cipher_spec(20), alert(21), handshake(22)). 'buf' and 'len' point to the actual message, 'ssl' to the SSL object, and 'arg' is the application-defined value set by SSL[_CTX]_set_msg_callback_arg(). 'openssl s_client' and 'openssl s_server' have new '-msg' options to enable a callback that displays all protocol messages. [Bodo Moeller] *) Change the shared library support so shared libraries are built as soon as the corresponding static library is finished, and thereby get openssl and the test programs linked against the shared library. This still only happens when the keyword "shard" has been given to the configuration scripts. NOTE: shared library support is still an experimental thing, and backward binary compatibility is still not guaranteed. ["Maciej W. Rozycki" and Richard Levitte] *) Add support for Subject Information Access extension. [Peter Sylvester ] *) Make BUF_MEM_grow() behaviour more consistent: Initialise to zero additional bytes when new memory had to be allocated, not just when reusing an existing buffer. [Bodo Moeller] *) New command line and configuration option 'utf8' for the req command. This allows field values to be specified as UTF8 strings. [Steve Henson] *) Add -multi and -mr options to "openssl speed" - giving multiple parallel runs for the former and machine-readable output for the latter. [Ben Laurie] *) Add '-noemailDN' option to 'openssl ca'. This prevents inclusion of the e-mail address in the DN (i.e., it will go into a certificate extension only). The new configuration file option 'email_in_dn = no' has the same effect. [Massimiliano Pala madwolf@openca.org] *) Change all functions with names starting with des_ to be starting with DES_ instead. Add wrappers that are compatible with libdes, but are named _ossl_old_des_*. Finally, add macros that map the des_* symbols to the corresponding _ossl_old_des_* if libdes compatibility is desired. If OpenSSL 0.9.6c compatibility is desired, the des_* symbols will be mapped to DES_*, with one exception. Since we provide two compatibility mappings, the user needs to define the macro OPENSSL_DES_LIBDES_COMPATIBILITY if libdes compatibility is desired. The default (i.e., when that macro isn't defined) is OpenSSL 0.9.6c compatibility. There are also macros that enable and disable the support of old des functions altogether. Those are OPENSSL_ENABLE_OLD_DES_SUPPORT and OPENSSL_DISABLE_OLD_DES_SUPPORT. If none or both of those are defined, the default will apply: to support the old des routines. In either case, one must include openssl/des.h to get the correct definitions. Do not try to just include openssl/des_old.h, that won't work. NOTE: This is a major break of an old API into a new one. Software authors are encouraged to switch to the DES_ style functions. Some time in the future, des_old.h and the libdes compatibility functions will be disable (i.e. OPENSSL_DISABLE_OLD_DES_SUPPORT will be the default), and then completely removed. [Richard Levitte] *) Test for certificates which contain unsupported critical extensions. If such a certificate is found during a verify operation it is rejected by default: this behaviour can be overridden by either handling the new error X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION or by setting the verify flag X509_V_FLAG_IGNORE_CRITICAL. A new function X509_supported_extension() has also been added which returns 1 if a particular extension is supported. [Steve Henson] *) Modify the behaviour of EVP cipher functions in similar way to digests to retain compatibility with existing code. [Steve Henson] *) Modify the behaviour of EVP_DigestInit() and EVP_DigestFinal() to retain compatibility with existing code. In particular the 'ctx' parameter does not have to be to be initialized before the call to EVP_DigestInit() and it is tidied up after a call to EVP_DigestFinal(). New function EVP_DigestFinal_ex() which does not tidy up the ctx. Similarly function EVP_MD_CTX_copy() changed to not require the destination to be initialized valid and new function EVP_MD_CTX_copy_ex() added which requires the destination to be valid. Modify all the OpenSSL digest calls to use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and EVP_MD_CTX_copy_ex(). [Steve Henson] *) Change ssl3_get_message (ssl/s3_both.c) and the functions using it so that complete 'Handshake' protocol structures are kept in memory instead of overwriting 'msg_type' and 'length' with 'body' data. [Bodo Moeller] *) Add an implementation of SSL_add_dir_cert_subjects_to_stack for Win32. [Massimo Santin via Richard Levitte] *) Major restructuring to the underlying ENGINE code. This includes reduction of linker bloat, separation of pure "ENGINE" manipulation (initialisation, etc) from functionality dealing with implementations of specific crypto iterfaces. This change also introduces integrated support for symmetric ciphers and digest implementations - so ENGINEs can now accelerate these by providing EVP_CIPHER and EVP_MD implementations of their own. This is detailed in crypto/engine/README as it couldn't be adequately described here. However, there are a few API changes worth noting - some RSA, DSA, DH, and RAND functions that were changed in the original introduction of ENGINE code have now reverted back - the hooking from this code to ENGINE is now a good deal more passive and at run-time, operations deal directly with RSA_METHODs, DSA_METHODs (etc) as they did before, rather than dereferencing through an ENGINE pointer any more. Also, the ENGINE functions dealing with BN_MOD_EXP[_CRT] handlers have been removed - they were not being used by the framework as there is no concept of a BIGNUM_METHOD and they could not be generalised to the new 'ENGINE_TABLE' mechanism that underlies the new code. Similarly, ENGINE_cpy() has been removed as it cannot be consistently defined in the new code. [Geoff Thorpe] *) Change ASN1_GENERALIZEDTIME_check() to allow fractional seconds. [Steve Henson] *) Change mkdef.pl to sort symbols that get the same entry number, and make sure the automatically generated functions ERR_load_* become part of libeay.num as well. [Richard Levitte] *) New function SSL_renegotiate_pending(). This returns true once renegotiation has been requested (either SSL_renegotiate() call or HelloRequest/ClientHello receveived from the peer) and becomes false once a handshake has been completed. (For servers, SSL_renegotiate() followed by SSL_do_handshake() sends a HelloRequest, but does not ensure that a handshake takes place. SSL_renegotiate_pending() is useful for checking if the client has followed the request.) [Bodo Moeller] *) New SSL option SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION. By default, clients may request session resumption even during renegotiation (if session ID contexts permit); with this option, session resumption is possible only in the first handshake. SSL_OP_ALL is now 0x00000FFFL instead of 0x000FFFFFL. This makes more bits available for options that should not be part of SSL_OP_ALL (such as SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION). [Bodo Moeller] *) Add some demos for certificate and certificate request creation. [Steve Henson] *) Make maximum certificate chain size accepted from the peer application settable (SSL*_get/set_max_cert_list()), as proposed by "Douglas E. Engert" . [Lutz Jaenicke] *) Add support for shared libraries for Unixware-7 (Boyd Lynn Gerber ). [Lutz Jaenicke] *) Add a "destroy" handler to ENGINEs that allows structural cleanup to be done prior to destruction. Use this to unload error strings from ENGINEs that load their own error strings. NB: This adds two new API functions to "get" and "set" this destroy handler in an ENGINE. [Geoff Thorpe] *) Alter all existing ENGINE implementations (except "openssl" and "openbsd") to dynamically instantiate their own error strings. This makes them more flexible to be built both as statically-linked ENGINEs and self-contained shared-libraries loadable via the "dynamic" ENGINE. Also, add stub code to each that makes building them as self-contained shared-libraries easier (see README.ENGINE). [Geoff Thorpe] *) Add a "dynamic" ENGINE that provides a mechanism for binding ENGINE implementations into applications that are completely implemented in self-contained shared-libraries. The "dynamic" ENGINE exposes control commands that can be used to configure what shared-library to load and to control aspects of the way it is handled. Also, made an update to the README.ENGINE file that brings its information up-to-date and provides some information and instructions on the "dynamic" ENGINE (ie. how to use it, how to build "dynamic"-loadable ENGINEs, etc). [Geoff Thorpe] *) Make it possible to unload ranges of ERR strings with a new "ERR_unload_strings" function. [Geoff Thorpe] *) Add a copy() function to EVP_MD. [Ben Laurie] *) Make EVP_MD routines take a context pointer instead of just the md_data void pointer. [Ben Laurie] *) Add flags to EVP_MD and EVP_MD_CTX. EVP_MD_FLAG_ONESHOT indicates that the digest can only process a single chunk of data (typically because it is provided by a piece of hardware). EVP_MD_CTX_FLAG_ONESHOT indicates that the application is only going to provide a single chunk of data, and hence the framework needn't accumulate the data for oneshot drivers. [Ben Laurie] *) As with "ERR", make it possible to replace the underlying "ex_data" functions. This change also alters the storage and management of global ex_data state - it's now all inside ex_data.c and all "class" code (eg. RSA, BIO, SSL_CTX, etc) no longer stores its own STACKS and per-class index counters. The API functions that use this state have been changed to take a "class_index" rather than pointers to the class's local STACK and counter, and there is now an API function to dynamically create new classes. This centralisation allows us to (a) plug a lot of the thread-safety problems that existed, and (b) makes it possible to clean up all allocated state using "CRYPTO_cleanup_all_ex_data()". W.r.t. (b) such data would previously have always leaked in application code and workarounds were in place to make the memory debugging turn a blind eye to it. Application code that doesn't use this new function will still leak as before, but their memory debugging output will announce it now rather than letting it slide. Besides the addition of CRYPTO_cleanup_all_ex_data(), another API change induced by the "ex_data" overhaul is that X509_STORE_CTX_init() now has a return value to indicate success or failure. [Geoff Thorpe] *) Make it possible to replace the underlying "ERR" functions such that the global state (2 LHASH tables and 2 locks) is only used by the "default" implementation. This change also adds two functions to "get" and "set" the implementation prior to it being automatically set the first time any other ERR function takes place. Ie. an application can call "get", pass the return value to a module it has just loaded, and that module can call its own "set" function using that value. This means the module's "ERR" operations will use (and modify) the error state in the application and not in its own statically linked copy of OpenSSL code. [Geoff Thorpe] *) Give DH, DSA, and RSA types their own "**_up_ref()" function to increment reference counts. This performs normal REF_PRINT/REF_CHECK macros on the operation, and provides a more encapsulated way for external code (crypto/evp/ and ssl/) to do this. Also changed the evp and ssl code to use these functions rather than manually incrementing the counts. Also rename "DSO_up()" function to more descriptive "DSO_up_ref()". [Geoff Thorpe] *) Add EVP test program. [Ben Laurie] *) Add symmetric cipher support to ENGINE. Expect the API to change! [Ben Laurie] *) New CRL functions: X509_CRL_set_version(), X509_CRL_set_issuer_name() X509_CRL_set_lastUpdate(), X509_CRL_set_nextUpdate(), X509_CRL_sort(), X509_REVOKED_set_serialNumber(), and X509_REVOKED_set_revocationDate(). These allow a CRL to be built without having to access X509_CRL fields directly. Modify 'ca' application to use new functions. [Steve Henson] *) Move SSL_OP_TLS_ROLLBACK_BUG out of the SSL_OP_ALL list of recommended bug workarounds. Rollback attack detection is a security feature. The problem will only arise on OpenSSL servers when TLSv1 is not available (sslv3_server_method() or SSL_OP_NO_TLSv1). Software authors not wanting to support TLSv1 will have special reasons for their choice and can explicitly enable this option. [Bodo Moeller, Lutz Jaenicke] *) Rationalise EVP so it can be extended: don't include a union of cipher/digest structures, add init/cleanup functions for EVP_MD_CTX (similar to those existing for EVP_CIPHER_CTX). Usage example: EVP_MD_CTX md; EVP_MD_CTX_init(&md); /* new function call */ EVP_DigestInit(&md, EVP_sha1()); EVP_DigestUpdate(&md, in, len); EVP_DigestFinal(&md, out, NULL); EVP_MD_CTX_cleanup(&md); /* new function call */ [Ben Laurie] *) Make DES key schedule conform to the usual scheme, as well as correcting its structure. This means that calls to DES functions now have to pass a pointer to a des_key_schedule instead of a plain des_key_schedule (which was actually always a pointer anyway): E.g., des_key_schedule ks; des_set_key_checked(..., &ks); des_ncbc_encrypt(..., &ks, ...); (Note that a later change renames 'des_...' into 'DES_...'.) [Ben Laurie] *) Initial reduction of linker bloat: the use of some functions, such as PEM causes large amounts of unused functions to be linked in due to poor organisation. For example pem_all.c contains every PEM function which has a knock on effect of linking in large amounts of (unused) ASN1 code. Grouping together similar functions and splitting unrelated functions prevents this. [Steve Henson] *) Cleanup of EVP macros. [Ben Laurie] *) Change historical references to {NID,SN,LN}_des_ede and ede3 to add the correct _ecb suffix. [Ben Laurie] *) Add initial OCSP responder support to ocsp application. The revocation information is handled using the text based index use by the ca application. The responder can either handle requests generated internally, supplied in files (for example via a CGI script) or using an internal minimal server. [Steve Henson] *) Add configuration choices to get zlib compression for TLS. [Richard Levitte] *) Changes to Kerberos SSL for RFC 2712 compliance: 1. Implemented real KerberosWrapper, instead of just using KRB5 AP_REQ message. [Thanks to Simon Wilkinson ] 2. Implemented optional authenticator field of KerberosWrapper. Added openssl-style ASN.1 macros for Kerberos ticket, ap_req, and authenticator structs; see crypto/krb5/. Generalized Kerberos calls to support multiple Kerberos libraries. [Vern Staats , Jeffrey Altman via Richard Levitte] *) Cause 'openssl speed' to use fully hard-coded DSA keys as it already does with RSA. testdsa.h now has 'priv_key/pub_key' values for each of the key sizes rather than having just parameters (and 'speed' generating keys each time). [Geoff Thorpe] *) Speed up EVP routines. Before: encrypt type 8 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes des-cbc 4408.85k 5560.51k 5778.46k 5862.20k 5825.16k des-cbc 4389.55k 5571.17k 5792.23k 5846.91k 5832.11k des-cbc 4394.32k 5575.92k 5807.44k 5848.37k 5841.30k decrypt des-cbc 3482.66k 5069.49k 5496.39k 5614.16k 5639.28k des-cbc 3480.74k 5068.76k 5510.34k 5609.87k 5635.52k des-cbc 3483.72k 5067.62k 5504.60k 5708.01k 5724.80k After: encrypt des-cbc 4660.16k 5650.19k 5807.19k 5827.13k 5783.32k decrypt des-cbc 3624.96k 5258.21k 5530.91k 5624.30k 5628.26k [Ben Laurie] *) Added the OS2-EMX target. ["Brian Havard" and Richard Levitte] *) Rewrite apps to use NCONF routines instead of the old CONF. New functions to support NCONF routines in extension code. New function CONF_set_nconf() to allow functions which take an NCONF to also handle the old LHASH structure: this means that the old CONF compatible routines can be retained (in particular wrt extensions) without having to duplicate the code. New function X509V3_add_ext_nconf_sk to add extensions to a stack. [Steve Henson] *) Enhance the general user interface with mechanisms for inner control and with possibilities to have yes/no kind of prompts. [Richard Levitte] *) Change all calls to low level digest routines in the library and applications to use EVP. Add missing calls to HMAC_cleanup() and don't assume HMAC_CTX can be copied using memcpy(). [Verdon Walker , Steve Henson] *) Add the possibility to control engines through control names but with arbitrary arguments instead of just a string. Change the key loaders to take a UI_METHOD instead of a callback function pointer. NOTE: this breaks binary compatibility with earlier versions of OpenSSL [engine]. Adapt the nCipher code for these new conditions and add a card insertion callback. [Richard Levitte] *) Enhance the general user interface with mechanisms to better support dialog box interfaces, application-defined prompts, the possibility to use defaults (for example default passwords from somewhere else) and interrupts/cancellations. [Richard Levitte] *) Tidy up PKCS#12 attribute handling. Add support for the CSP name attribute in PKCS#12 files, add new -CSP option to pkcs12 utility. [Steve Henson] *) Fix a memory leak in 'sk_dup()' in the case reallocation fails. (Also tidy up some unnecessarily weird code in 'sk_new()'). [Geoff, reported by Diego Tartara ] *) Change the key loading routines for ENGINEs to use the same kind callback (pem_password_cb) as all other routines that need this kind of callback. [Richard Levitte] *) Increase ENTROPY_NEEDED to 32 bytes, as Rijndael can operate with 256 bit (=32 byte) keys. Of course seeding with more entropy bytes than this minimum value is recommended. [Lutz Jaenicke] *) New random seeder for OpenVMS, using the system process statistics that are easily reachable. [Richard Levitte] *) Windows apparently can't transparently handle global variables defined in DLLs. Initialisations such as: const ASN1_ITEM *it = &ASN1_INTEGER_it; wont compile. This is used by the any applications that need to declare their own ASN1 modules. This was fixed by adding the option EXPORT_VAR_AS_FN to all Win32 platforms, although this isn't strictly needed for static libraries under Win32. [Steve Henson] *) New functions X509_PURPOSE_set() and X509_TRUST_set() to handle setting of purpose and trust fields. New X509_STORE trust and purpose functions and tidy up setting in other SSL functions. [Steve Henson] *) Add copies of X509_STORE_CTX fields and callbacks to X509_STORE structure. These are inherited by X509_STORE_CTX when it is initialised. This allows various defaults to be set in the X509_STORE structure (such as flags for CRL checking and custom purpose or trust settings) for functions which only use X509_STORE_CTX internally such as S/MIME. Modify X509_STORE_CTX_purpose_inherit() so it only sets purposes and trust settings if they are not set in X509_STORE. This allows X509_STORE purposes and trust (in S/MIME for example) to override any set by default. Add command line options for CRL checking to smime, s_client and s_server applications. [Steve Henson] *) Initial CRL based revocation checking. If the CRL checking flag(s) are set then the CRL is looked up in the X509_STORE structure and its validity and signature checked, then if the certificate is found in the CRL the verify fails with a revoked error. Various new CRL related callbacks added to X509_STORE_CTX structure. Command line options added to 'verify' application to support this. This needs some additional work, such as being able to handle multiple CRLs with different times, extension based lookup (rather than just by subject name) and ultimately more complete V2 CRL extension handling. [Steve Henson] *) Add a general user interface API (crypto/ui/). This is designed to replace things like des_read_password and friends (backward compatibility functions using this new API are provided). The purpose is to remove prompting functions from the DES code section as well as provide for prompting through dialog boxes in a window system and the like. [Richard Levitte] *) Add "ex_data" support to ENGINE so implementations can add state at a per-structure level rather than having to store it globally. [Geoff] *) Make it possible for ENGINE structures to be copied when retrieved by ENGINE_by_id() if the ENGINE specifies a new flag: ENGINE_FLAGS_BY_ID_COPY. This causes the "original" ENGINE structure to act like a template, analogous to the RSA vs. RSA_METHOD type of separation. Because of this operational state can be localised to each ENGINE structure, despite the fact they all share the same "methods". New ENGINE structures returned in this case have no functional references and the return value is the single structural reference. This matches the single structural reference returned by ENGINE_by_id() normally, when it is incremented on the pre-existing ENGINE structure. [Geoff] *) Fix ASN1 decoder when decoding type ANY and V_ASN1_OTHER: since this needs to match any other type at all we need to manually clear the tag cache. [Steve Henson] *) Changes to the "openssl engine" utility to include; - verbosity levels ('-v', '-vv', and '-vvv') that provide information about an ENGINE's available control commands. - executing control commands from command line arguments using the '-pre' and '-post' switches. '-post' is only used if '-t' is specified and the ENGINE is successfully initialised. The syntax for the individual commands are colon-separated, for example; openssl engine chil -pre FORK_CHECK:0 -pre SO_PATH:/lib/test.so [Geoff] *) New dynamic control command support for ENGINEs. ENGINEs can now declare their own commands (numbers), names (strings), descriptions, and input types for run-time discovery by calling applications. A subset of these commands are implicitly classed as "executable" depending on their input type, and only these can be invoked through the new string-based API function ENGINE_ctrl_cmd_string(). (Eg. this can be based on user input, config files, etc). The distinction is that "executable" commands cannot return anything other than a boolean result and can only support numeric or string input, whereas some discoverable commands may only be for direct use through ENGINE_ctrl(), eg. supporting the exchange of binary data, function pointers, or other custom uses. The "executable" commands are to support parameterisations of ENGINE behaviour that can be unambiguously defined by ENGINEs and used consistently across any OpenSSL-based application. Commands have been added to all the existing hardware-supporting ENGINEs, noticeably "SO_PATH" to allow control over shared-library paths without source code alterations. [Geoff] *) Changed all ENGINE implementations to dynamically allocate their ENGINEs rather than declaring them statically. Apart from this being necessary with the removal of the ENGINE_FLAGS_MALLOCED distinction, this also allows the implementations to compile without using the internal engine_int.h header. [Geoff] *) Minor adjustment to "rand" code. RAND_get_rand_method() now returns a 'const' value. Any code that should be able to modify a RAND_METHOD should already have non-const pointers to it (ie. they should only modify their own ones). [Geoff] *) Made a variety of little tweaks to the ENGINE code. - "atalla" and "ubsec" string definitions were moved from header files to C code. "nuron" string definitions were placed in variables rather than hard-coded - allowing parameterisation of these values later on via ctrl() commands. - Removed unused "#if 0"'d code. - Fixed engine list iteration code so it uses ENGINE_free() to release structural references. - Constified the RAND_METHOD element of ENGINE structures. - Constified various get/set functions as appropriate and added missing functions (including a catch-all ENGINE_cpy that duplicates all ENGINE values onto a new ENGINE except reference counts/state). - Removed NULL parameter checks in get/set functions. Setting a method or function to NULL is a way of cancelling out a previously set value. Passing a NULL ENGINE parameter is just plain stupid anyway and doesn't justify the extra error symbols and code. - Deprecate the ENGINE_FLAGS_MALLOCED define and move the area for flags from engine_int.h to engine.h. - Changed prototypes for ENGINE handler functions (init(), finish(), ctrl(), key-load functions, etc) to take an (ENGINE*) parameter. [Geoff] *) Implement binary inversion algorithm for BN_mod_inverse in addition to the algorithm using long division. The binary algorithm can be used only if the modulus is odd. On 32-bit systems, it is faster only for relatively small moduli (roughly 20-30% for 128-bit moduli, roughly 5-15% for 256-bit moduli), so we use it only for moduli up to 450 bits. In 64-bit environments, the binary algorithm appears to be advantageous for much longer moduli; here we use it for moduli up to 2048 bits. [Bodo Moeller] *) Rewrite CHOICE field setting in ASN1_item_ex_d2i(). The old code could not support the combine flag in choice fields. [Steve Henson] *) Add a 'copy_extensions' option to the 'ca' utility. This copies extensions from a certificate request to the certificate. [Steve Henson] *) Allow multiple 'certopt' and 'nameopt' options to be separated by commas. Add 'namopt' and 'certopt' options to the 'ca' config file: this allows the display of the certificate about to be signed to be customised, to allow certain fields to be included or excluded and extension details. The old system didn't display multicharacter strings properly, omitted fields not in the policy and couldn't display additional details such as extensions. [Steve Henson] *) Function EC_POINTs_mul for multiple scalar multiplication of an arbitrary number of elliptic curve points \sum scalars[i]*points[i], optionally including the generator defined for the EC_GROUP: scalar*generator + \sum scalars[i]*points[i]. EC_POINT_mul is a simple wrapper function for the typical case that the point list has just one item (besides the optional generator). [Bodo Moeller] *) First EC_METHODs for curves over GF(p): EC_GFp_simple_method() uses the basic BN_mod_mul and BN_mod_sqr operations and provides various method functions that can also operate with faster implementations of modular arithmetic. EC_GFp_mont_method() reuses most functions that are part of EC_GFp_simple_method, but uses Montgomery arithmetic. [Bodo Moeller; point addition and point doubling implementation directly derived from source code provided by Lenka Fibikova ] *) Framework for elliptic curves (crypto/ec/ec.h, crypto/ec/ec_lcl.h, crypto/ec/ec_lib.c): Curves are EC_GROUP objects (with an optional group generator) based on EC_METHODs that are built into the library. Points are EC_POINT objects based on EC_GROUP objects. Most of the framework would be able to handle curves over arbitrary finite fields, but as there are no obvious types for fields other than GF(p), some functions are limited to that for now. [Bodo Moeller] *) Add the -HTTP option to s_server. It is similar to -WWW, but requires that the file contains a complete HTTP response. [Richard Levitte] *) Add the ec directory to mkdef.pl and mkfiles.pl. In mkdef.pl change the def and num file printf format specifier from "%-40sXXX" to "%-39s XXX". The latter will always guarantee a space after the field while the former will cause them to run together if the field is 40 of more characters long. [Steve Henson] *) Constify the cipher and digest 'method' functions and structures and modify related functions to take constant EVP_MD and EVP_CIPHER pointers. [Steve Henson] *) Hide BN_CTX structure details in bn_lcl.h instead of publishing them in . Also further increase BN_CTX_NUM to 32. [Bodo Moeller] *) Modify EVP_Digest*() routines so they now return values. Although the internal software routines can never fail additional hardware versions might. [Steve Henson] *) Clean up crypto/err/err.h and change some error codes to avoid conflicts: Previously ERR_R_FATAL was too small and coincided with ERR_LIB_PKCS7 (= ERR_R_PKCS7_LIB); it is now 64 instead of 32. ASN1 error codes ERR_R_NESTED_ASN1_ERROR ... ERR_R_MISSING_ASN1_EOS were 4 .. 9, conflicting with ERR_LIB_RSA (= ERR_R_RSA_LIB) ... ERR_LIB_PEM (= ERR_R_PEM_LIB). They are now 58 .. 63 (i.e., just below ERR_R_FATAL). Add new error code 'ERR_R_INTERNAL_ERROR'. [Bodo Moeller] *) Don't overuse locks in crypto/err/err.c: For data retrieval, CRYPTO_r_lock suffices. [Bodo Moeller] *) New option '-subj arg' for 'openssl req' and 'openssl ca'. This sets the subject name for a new request or supersedes the subject name in a given request. Formats that can be parsed are 'CN=Some Name, OU=myOU, C=IT' and 'CN=Some Name/OU=myOU/C=IT'. Add options '-batch' and '-verbose' to 'openssl req'. [Massimiliano Pala ] *) Introduce the possibility to access global variables through functions on platform were that's the best way to handle exporting global variables in shared libraries. To enable this functionality, one must configure with "EXPORT_VAR_AS_FN" or defined the C macro "OPENSSL_EXPORT_VAR_AS_FUNCTION" in crypto/opensslconf.h (the latter is normally done by Configure or something similar). To implement a global variable, use the macro OPENSSL_IMPLEMENT_GLOBAL in the source file (foo.c) like this: OPENSSL_IMPLEMENT_GLOBAL(int,foo)=1; OPENSSL_IMPLEMENT_GLOBAL(double,bar); To declare a global variable, use the macros OPENSSL_DECLARE_GLOBAL and OPENSSL_GLOBAL_REF in the header file (foo.h) like this: OPENSSL_DECLARE_GLOBAL(int,foo); #define foo OPENSSL_GLOBAL_REF(foo) OPENSSL_DECLARE_GLOBAL(double,bar); #define bar OPENSSL_GLOBAL_REF(bar) The #defines are very important, and therefore so is including the header file everywhere where the defined globals are used. The macro OPENSSL_EXPORT_VAR_AS_FUNCTION also affects the definition of ASN.1 items, but that structure is a bit different. The largest change is in util/mkdef.pl which has been enhanced with better and easier to understand logic to choose which symbols should go into the Windows .def files as well as a number of fixes and code cleanup (among others, algorithm keywords are now sorted lexicographically to avoid constant rewrites). [Richard Levitte] *) In BN_div() keep a copy of the sign of 'num' before writing the result to 'rm' because if rm==num the value will be overwritten and produce the wrong result if 'num' is negative: this caused problems with BN_mod() and BN_nnmod(). [Steve Henson] *) Function OCSP_request_verify(). This checks the signature on an OCSP request and verifies the signer certificate. The signer certificate is just checked for a generic purpose and OCSP request trust settings. [Steve Henson] *) Add OCSP_check_validity() function to check the validity of OCSP responses. OCSP responses are prepared in real time and may only be a few seconds old. Simply checking that the current time lies between thisUpdate and nextUpdate max reject otherwise valid responses caused by either OCSP responder or client clock inaccuracy. Instead we allow thisUpdate and nextUpdate to fall within a certain period of the current time. The age of the response can also optionally be checked. Two new options -validity_period and -status_age added to ocsp utility. [Steve Henson] *) If signature or public key algorithm is unrecognized print out its OID rather that just UNKNOWN. [Steve Henson] *) Change OCSP_cert_to_id() to tolerate a NULL subject certificate and OCSP_cert_id_new() a NULL serialNumber. This allows a partial certificate ID to be generated from the issuer certificate alone which can then be passed to OCSP_id_issuer_cmp(). [Steve Henson] *) New compilation option ASN1_ITEM_FUNCTIONS. This causes the new ASN1 modules to export functions returning ASN1_ITEM pointers instead of the ASN1_ITEM structures themselves. This adds several new macros which allow the underlying ASN1 function/structure to be accessed transparently. As a result code should not use ASN1_ITEM references directly (such as &X509_it) but instead use the relevant macros (such as ASN1_ITEM_rptr(X509)). This option is to allow use of the new ASN1 code on platforms where exporting structures is problematical (for example in shared libraries) but exporting functions returning pointers to structures is not. [Steve Henson] *) Add support for overriding the generation of SSL/TLS session IDs. These callbacks can be registered either in an SSL_CTX or per SSL. The purpose of this is to allow applications to control, if they wish, the arbitrary values chosen for use as session IDs, particularly as it can be useful for session caching in multiple-server environments. A command-line switch for testing this (and any client code that wishes to use such a feature) has been added to "s_server". [Geoff Thorpe, Lutz Jaenicke] *) Modify mkdef.pl to recognise and parse preprocessor conditionals of the form '#if defined(...) || defined(...) || ...' and '#if !defined(...) && !defined(...) && ...'. This also avoids the growing number of special cases it was previously handling. [Richard Levitte] *) Make all configuration macros available for application by making sure they are available in opensslconf.h, by giving them names starting with "OPENSSL_" to avoid conflicts with other packages and by making sure e_os2.h will cover all platform-specific cases together with opensslconf.h. Additionally, it is now possible to define configuration/platform- specific names (called "system identities"). In the C code, these are prefixed with "OPENSSL_SYSNAME_". e_os2.h will create another macro with the name beginning with "OPENSSL_SYS_", which is determined from "OPENSSL_SYSNAME_*" or compiler-specific macros depending on what is available. [Richard Levitte] *) New option -set_serial to 'req' and 'x509' this allows the serial number to use to be specified on the command line. Previously self signed certificates were hard coded with serial number 0 and the CA options of 'x509' had to use a serial number in a file which was auto incremented. [Steve Henson] *) New options to 'ca' utility to support V2 CRL entry extensions. Currently CRL reason, invalidity date and hold instruction are supported. Add new CRL extensions to V3 code and some new objects. [Steve Henson] *) New function EVP_CIPHER_CTX_set_padding() this is used to disable standard block padding (aka PKCS#5 padding) in the EVP API, which was previously mandatory. This means that the data is not padded in any way and so the total length much be a multiple of the block size, otherwise an error occurs. [Steve Henson] *) Initial (incomplete) OCSP SSL support. [Steve Henson] *) New function OCSP_parse_url(). This splits up a URL into its host, port and path components: primarily to parse OCSP URLs. New -url option to ocsp utility. [Steve Henson] *) New nonce behavior. The return value of OCSP_check_nonce() now reflects the various checks performed. Applications can decide whether to tolerate certain situations such as an absent nonce in a response when one was present in a request: the ocsp application just prints out a warning. New function OCSP_add1_basic_nonce() this is to allow responders to include a nonce in a response even if the request is nonce-less. [Steve Henson] *) Disable stdin buffering in load_cert (apps/apps.c) so that no certs are skipped when using openssl x509 multiple times on a single input file, e.g. "(openssl x509 -out cert1; openssl x509 -out cert2) ] *) New OCSP verify flag OCSP_TRUSTOTHER. When set the "other" certificates passed by the function are trusted implicitly. If any of them signed the response then it is assumed to be valid and is not verified. [Steve Henson] *) In PKCS7_set_type() initialise content_type in PKCS7_ENC_CONTENT to data. This was previously part of the PKCS7 ASN1 code. This was causing problems with OpenSSL created PKCS#12 and PKCS#7 structures. [Steve Henson, reported by Kenneth R. Robinette ] *) Add CRYPTO_push_info() and CRYPTO_pop_info() calls to new ASN1 routines: without these tracing memory leaks is very painful. Fix leaks in PKCS12 and PKCS7 routines. [Steve Henson] *) Make X509_time_adj() cope with the new behaviour of ASN1_TIME_new(). Previously it initialised the 'type' argument to V_ASN1_UTCTIME which effectively meant GeneralizedTime would never be used. Now it is initialised to -1 but X509_time_adj() now has to check the value and use ASN1_TIME_set() if the value is not V_ASN1_UTCTIME or V_ASN1_GENERALIZEDTIME, without this it always uses GeneralizedTime. [Steve Henson, reported by Kenneth R. Robinette ] *) Fixes to BN_to_ASN1_INTEGER when bn is zero. This would previously result in a zero length in the ASN1_INTEGER structure which was not consistent with the structure when d2i_ASN1_INTEGER() was used and would cause ASN1_INTEGER_cmp() to fail. Enhance s2i_ASN1_INTEGER() to cope with hex and negative integers. Fix bug in i2a_ASN1_INTEGER() where it did not print out a minus for negative ASN1_INTEGER. [Steve Henson] *) Add summary printout to ocsp utility. The various functions which convert status values to strings have been renamed to: OCSP_response_status_str(), OCSP_cert_status_str() and OCSP_crl_reason_str() and are no longer static. New options to verify nonce values and to disable verification. OCSP response printout format cleaned up. [Steve Henson] *) Add additional OCSP certificate checks. These are those specified in RFC2560. This consists of two separate checks: the CA of the certificate being checked must either be the OCSP signer certificate or the issuer of the OCSP signer certificate. In the latter case the OCSP signer certificate must contain the OCSP signing extended key usage. This check is performed by attempting to match the OCSP signer or the OCSP signer CA to the issuerNameHash and issuerKeyHash in the OCSP_CERTID structures of the response. [Steve Henson] *) Initial OCSP certificate verification added to OCSP_basic_verify() and related routines. This uses the standard OpenSSL certificate verify routines to perform initial checks (just CA validity) and to obtain the certificate chain. Then additional checks will be performed on the chain. Currently the root CA is checked to see if it is explicitly trusted for OCSP signing. This is used to set a root CA as a global signing root: that is any certificate that chains to that CA is an acceptable OCSP signing certificate. [Steve Henson] *) New '-extfile ...' option to 'openssl ca' for reading X.509v3 extensions from a separate configuration file. As when reading extensions from the main configuration file, the '-extensions ...' option may be used for specifying the section to use. [Massimiliano Pala ] *) New OCSP utility. Allows OCSP requests to be generated or read. The request can be sent to a responder and the output parsed, outputed or printed in text form. Not complete yet: still needs to check the OCSP response validity. [Steve Henson] *) New subcommands for 'openssl ca': 'openssl ca -status ' prints the status of the cert with the given serial number (according to the index file). 'openssl ca -updatedb' updates the expiry status of certificates in the index file. [Massimiliano Pala ] *) New '-newreq-nodes' command option to CA.pl. This is like '-newreq', but calls 'openssl req' with the '-nodes' option so that the resulting key is not encrypted. [Damien Miller ] *) New configuration for the GNU Hurd. [Jonathan Bartlett via Richard Levitte] *) Initial code to implement OCSP basic response verify. This is currently incomplete. Currently just finds the signer's certificate and verifies the signature on the response. [Steve Henson] *) New SSLeay_version code SSLEAY_DIR to determine the compiled-in value of OPENSSLDIR. This is available via the new '-d' option to 'openssl version', and is also included in 'openssl version -a'. [Bodo Moeller] *) Allowing defining memory allocation callbacks that will be given file name and line number information in additional arguments (a const char* and an int). The basic functionality remains, as well as the original possibility to just replace malloc(), realloc() and free() by functions that do not know about these additional arguments. To register and find out the current settings for extended allocation functions, the following functions are provided: CRYPTO_set_mem_ex_functions CRYPTO_set_locked_mem_ex_functions CRYPTO_get_mem_ex_functions CRYPTO_get_locked_mem_ex_functions These work the same way as CRYPTO_set_mem_functions and friends. CRYPTO_get_[locked_]mem_functions now writes 0 where such an extended allocation function is enabled. Similarly, CRYPTO_get_[locked_]mem_ex_functions writes 0 where a conventional allocation function is enabled. [Richard Levitte, Bodo Moeller] *) Finish off removing the remaining LHASH function pointer casts. There should no longer be any prototype-casting required when using the LHASH abstraction, and any casts that remain are "bugs". See the callback types and macros at the head of lhash.h for details (and "OBJ_cleanup" in crypto/objects/obj_dat.c as an example). [Geoff Thorpe] *) Add automatic query of EGD sockets in RAND_poll() for the unix variant. If /dev/[u]random devices are not available or do not return enough entropy, EGD style sockets (served by EGD or PRNGD) will automatically be queried. The locations /var/run/egd-pool, /dev/egd-pool, /etc/egd-pool, and /etc/entropy will be queried once each in this sequence, quering stops when enough entropy was collected without querying more sockets. [Lutz Jaenicke] *) Change the Unix RAND_poll() variant to be able to poll several random devices, as specified by DEVRANDOM, until a sufficient amount of data has been collected. We spend at most 10 ms on each file (select timeout) and read in non-blocking mode. DEVRANDOM now defaults to the list "/dev/urandom", "/dev/random", "/dev/srandom" (previously it was just the string "/dev/urandom"), so on typical platforms the 10 ms delay will never occur. Also separate out the Unix variant to its own file, rand_unix.c. For VMS, there's a currently-empty rand_vms.c. [Richard Levitte] *) Move OCSP client related routines to ocsp_cl.c. These provide utility functions which an application needing to issue a request to an OCSP responder and analyse the response will typically need: as opposed to those which an OCSP responder itself would need which will be added later. OCSP_request_sign() signs an OCSP request with an API similar to PKCS7_sign(). OCSP_response_status() returns status of OCSP response. OCSP_response_get1_basic() extracts basic response from response. OCSP_resp_find_status(): finds and extracts status information from an OCSP_CERTID structure (which will be created when the request structure is built). These are built from lower level functions which work on OCSP_SINGLERESP structures but wont normally be used unless the application wishes to examine extensions in the OCSP response for example. Replace nonce routines with a pair of functions. OCSP_request_add1_nonce() adds a nonce value and optionally generates a random value. OCSP_check_nonce() checks the validity of the nonce in an OCSP response. [Steve Henson] *) Change function OCSP_request_add() to OCSP_request_add0_id(). This doesn't copy the supplied OCSP_CERTID and avoids the need to free up the newly created id. Change return type to OCSP_ONEREQ to return the internal OCSP_ONEREQ structure. This can then be used to add extensions to the request. Deleted OCSP_request_new(), since most of its functionality is now in OCSP_REQUEST_new() (and the case insensitive name clash) apart from the ability to set the request name which will be added elsewhere. [Steve Henson] *) Update OCSP API. Remove obsolete extensions argument from various functions. Extensions are now handled using the new OCSP extension code. New simple OCSP HTTP function which can be used to send requests and parse the response. [Steve Henson] *) Fix the PKCS#7 (S/MIME) code to work with new ASN1. Two new ASN1_ITEM structures help with sign and verify. PKCS7_ATTR_SIGN uses the special reorder version of SET OF to sort the attributes and reorder them to match the encoded order. This resolves a long standing problem: a verify on a PKCS7 structure just after signing it used to fail because the attribute order did not match the encoded order. PKCS7_ATTR_VERIFY does not reorder the attributes: it uses the received order. This is necessary to tolerate some broken software that does not order SET OF. This is handled by encoding as a SEQUENCE OF but using implicit tagging (with UNIVERSAL class) to produce the required SET OF. [Steve Henson] *) Have mk1mf.pl generate the macros OPENSSL_BUILD_SHLIBCRYPTO and OPENSSL_BUILD_SHLIBSSL and use them appropriately in the header files to get correct declarations of the ASN.1 item variables. [Richard Levitte] *) Rewrite of PKCS#12 code to use new ASN1 functionality. Replace many PKCS#12 macros with real functions. Fix two unrelated ASN1 bugs: asn1_check_tlen() would sometimes attempt to use 'ctx' when it was NULL and ASN1_TYPE was not dereferenced properly in asn1_ex_c2i(). New ASN1 macro: DECLARE_ASN1_ITEM() which just declares the relevant ASN1_ITEM and no wrapper functions. [Steve Henson] *) New functions or ASN1_item_d2i_fp() and ASN1_item_d2i_bio(). These replace the old function pointer based I/O routines. Change most of the *_d2i_bio() and *_d2i_fp() functions to use these. [Steve Henson] *) Enhance mkdef.pl to be more accepting about spacing in C preprocessor lines, recognice more "algorithms" that can be deselected, and make it complain about algorithm deselection that isn't recognised. [Richard Levitte] *) New ASN1 functions to handle dup, sign, verify, digest, pack and unpack operations in terms of ASN1_ITEM. Modify existing wrappers to use new functions. Add NO_ASN1_OLD which can be set to remove some old style ASN1 functions: this can be used to determine if old code will still work when these eventually go away. [Steve Henson] *) New extension functions for OCSP structures, these follow the same conventions as certificates and CRLs. [Steve Henson] *) New function X509V3_add1_i2d(). This automatically encodes and adds an extension. Its behaviour can be customised with various flags to append, replace or delete. Various wrappers added for certifcates and CRLs. [Steve Henson] *) Fix to avoid calling the underlying ASN1 print routine when an extension cannot be parsed. Correct a typo in the OCSP_SERVICELOC extension. Tidy up print OCSP format. [Steve Henson] *) Make mkdef.pl parse some of the ASN1 macros and add apropriate entries for variables. [Steve Henson] *) Add functionality to apps/openssl.c for detecting locking problems: As the program is single-threaded, all we have to do is register a locking callback using an array for storing which locks are currently held by the program. [Bodo Moeller] *) Use a lock around the call to CRYPTO_get_ex_new_index() in SSL_get_ex_data_X509_STORE_idx(), which is used in ssl_verify_cert_chain() and thus can be called at any time during TLS/SSL handshakes so that thread-safety is essential. Unfortunately, the ex_data design is not at all suited for multi-threaded use, so it probably should be abolished. [Bodo Moeller] *) Added Broadcom "ubsec" ENGINE to OpenSSL. [Broadcom, tweaked and integrated by Geoff Thorpe] *) Move common extension printing code to new function X509V3_print_extensions(). Reorganise OCSP print routines and implement some needed OCSP ASN1 functions. Add OCSP extensions. [Steve Henson] *) New function X509_signature_print() to remove duplication in some print routines. [Steve Henson] *) Add a special meaning when SET OF and SEQUENCE OF flags are both set (this was treated exactly the same as SET OF previously). This is used to reorder the STACK representing the structure to match the encoding. This will be used to get round a problem where a PKCS7 structure which was signed could not be verified because the STACK order did not reflect the encoded order. [Steve Henson] *) Reimplement the OCSP ASN1 module using the new code. [Steve Henson] *) Update the X509V3 code to permit the use of an ASN1_ITEM structure for its ASN1 operations. The old style function pointers still exist for now but they will eventually go away. [Steve Henson] *) Merge in replacement ASN1 code from the ASN1 branch. This almost completely replaces the old ASN1 functionality with a table driven encoder and decoder which interprets an ASN1_ITEM structure describing the ASN1 module. Compatibility with the existing ASN1 API (i2d,d2i) is largely maintained. Almost all of the old asn1_mac.h macro based ASN1 has also been converted to the new form. [Steve Henson] *) Change BN_mod_exp_recp so that negative moduli are tolerated (the sign is ignored). Similarly, ignore the sign in BN_MONT_CTX_set so that BN_mod_exp_mont and BN_mod_exp_mont_word work for negative moduli. [Bodo Moeller] *) Fix BN_uadd and BN_usub: Always return non-negative results instead of not touching the result's sign bit. [Bodo Moeller] *) BN_div bugfix: If the result is 0, the sign (res->neg) must not be set. [Bodo Moeller] *) Changed the LHASH code to use prototypes for callbacks, and created macros to declare and implement thin (optionally static) functions that provide type-safety and avoid function pointer casting for the type-specific callbacks. [Geoff Thorpe] *) Added Kerberos Cipher Suites to be used with TLS, as written in RFC 2712. [Veers Staats , Jeffrey Altman , via Richard Levitte] *) Reformat the FAQ so the different questions and answers can be divided in sections depending on the subject. [Richard Levitte] *) Have the zlib compression code load ZLIB.DLL dynamically under Windows. [Richard Levitte] *) New function BN_mod_sqrt for computing square roots modulo a prime (using the probabilistic Tonelli-Shanks algorithm unless p == 3 (mod 4) or p == 5 (mod 8), which are cases that can be handled deterministically). [Lenka Fibikova , Bodo Moeller] *) Make BN_mod_inverse faster by explicitly handling small quotients in the Euclid loop. (Speed gain about 20% for small moduli [256 or 512 bits], about 30% for larger ones [1024 or 2048 bits].) [Bodo Moeller] *) New function BN_kronecker. [Bodo Moeller] *) Fix BN_gcd so that it works on negative inputs; the result is positive unless both parameters are zero. Previously something reasonably close to an infinite loop was possible because numbers could be growing instead of shrinking in the implementation of Euclid's algorithm. [Bodo Moeller] *) Fix BN_is_word() and BN_is_one() macros to take into account the sign of the number in question. Fix BN_is_word(a,w) to work correctly for w == 0. The old BN_is_word(a,w) macro is now called BN_abs_is_word(a,w) because its test if the absolute value of 'a' equals 'w'. Note that BN_abs_is_word does *not* handle w == 0 reliably; it exists mostly for use in the implementations of BN_is_zero(), BN_is_one(), and BN_is_word(). [Bodo Moeller] *) New function BN_swap. [Bodo Moeller] *) Use BN_nnmod instead of BN_mod in crypto/bn/bn_exp.c so that the exponentiation functions are more likely to produce reasonable results on negative inputs. [Bodo Moeller] *) Change BN_mod_mul so that the result is always non-negative. Previously, it could be negative if one of the factors was negative; I don't think anyone really wanted that behaviour. [Bodo Moeller] *) Move BN_mod_... functions into new file crypto/bn/bn_mod.c (except for exponentiation, which stays in crypto/bn/bn_exp.c, and BN_mod_mul_reciprocal, which stays in crypto/bn/bn_recp.c) and add new functions: BN_nnmod BN_mod_sqr BN_mod_add BN_mod_add_quick BN_mod_sub BN_mod_sub_quick BN_mod_lshift1 BN_mod_lshift1_quick BN_mod_lshift BN_mod_lshift_quick These functions always generate non-negative results. BN_nnmod otherwise is like BN_mod (if BN_mod computes a remainder r such that |m| < r < 0, BN_nnmod will output rem + |m| instead). BN_mod_XXX_quick(r, a, [b,] m) generates the same result as BN_mod_XXX(r, a, [b,] m, ctx), but requires that a [and b] be reduced modulo m. [Lenka Fibikova , Bodo Moeller] #if 0 The following entry accidentily appeared in the CHANGES file distributed with OpenSSL 0.9.7. The modifications described in it do *not* apply to OpenSSL 0.9.7. *) Remove a few calls to bn_wexpand() in BN_sqr() (the one in there was actually never needed) and in BN_mul(). The removal in BN_mul() required a small change in bn_mul_part_recursive() and the addition of the functions bn_cmp_part_words(), bn_sub_part_words() and bn_add_part_words(), which do the same thing as bn_cmp_words(), bn_sub_words() and bn_add_words() except they take arrays with differing sizes. [Richard Levitte] #endif *) In 'openssl passwd', verify passwords read from the terminal unless the '-salt' option is used (which usually means that verification would just waste user's time since the resulting hash is going to be compared with some given password hash) or the new '-noverify' option is used. This is an incompatible change, but it does not affect non-interactive use of 'openssl passwd' (passwords on the command line, '-stdin' option, '-in ...' option) and thus should not cause any problems. [Bodo Moeller] *) Remove all references to RSAref, since there's no more need for it. [Richard Levitte] *) Make DSO load along a path given through an environment variable (SHLIB_PATH) with shl_load(). [Richard Levitte] *) Constify the ENGINE code as a result of BIGNUM constification. Also constify the RSA code and most things related to it. In a few places, most notable in the depth of the ASN.1 code, ugly casts back to non-const were required (to be solved at a later time) [Richard Levitte] *) Make it so the openssl application has all engines loaded by default. [Richard Levitte] *) Constify the BIGNUM routines a little more. [Richard Levitte] *) Add the following functions: ENGINE_load_cswift() ENGINE_load_chil() ENGINE_load_atalla() ENGINE_load_nuron() ENGINE_load_builtin_engines() That way, an application can itself choose if external engines that are built-in in OpenSSL shall ever be used or not. The benefit is that applications won't have to be linked with libdl or other dso libraries unless it's really needed. Changed 'openssl engine' to load all engines on demand. Changed the engine header files to avoid the duplication of some declarations (they differed!). [Richard Levitte] *) 'openssl engine' can now list capabilities. [Richard Levitte] *) Better error reporting in 'openssl engine'. [Richard Levitte] *) Never call load_dh_param(NULL) in s_server. [Bodo Moeller] *) Add engine application. It can currently list engines by name and identity, and test if they are actually available. [Richard Levitte] *) Improve RPM specification file by forcing symbolic linking and making sure the installed documentation is also owned by root.root. [Damien Miller ] *) Give the OpenSSL applications more possibilities to make use of keys (public as well as private) handled by engines. [Richard Levitte] *) Add OCSP code that comes from CertCo. [Richard Levitte] *) Add VMS support for the Rijndael code. [Richard Levitte] *) Added untested support for Nuron crypto accelerator. [Ben Laurie] *) Add support for external cryptographic devices. This code was previously distributed separately as the "engine" branch. [Geoff Thorpe, Richard Levitte] *) Rework the filename-translation in the DSO code. It is now possible to have far greater control over how a "name" is turned into a filename depending on the operating environment and any oddities about the different shared library filenames on each system. [Geoff Thorpe] *) Support threads on FreeBSD-elf in Configure. [Richard Levitte] *) Fix for SHA1 assembly problem with MASM: it produces warnings about corrupt line number information when assembling with debugging information. This is caused by the overlapping of two sections. [Bernd Matthes , Steve Henson] *) NCONF changes. NCONF_get_number() has no error checking at all. As a replacement, NCONF_get_number_e() is defined (_e for "error checking") and is promoted strongly. The old NCONF_get_number is kept around for binary backward compatibility. Make it possible for methods to load from something other than a BIO, by providing a function pointer that is given a name instead of a BIO. For example, this could be used to load configuration data from an LDAP server. [Richard Levitte] *) Fix for non blocking accept BIOs. Added new I/O special reason BIO_RR_ACCEPT to cover this case. Previously use of accept BIOs with non blocking I/O was not possible because no retry code was implemented. Also added new SSL code SSL_WANT_ACCEPT to cover this case. [Steve Henson] *) Added the beginnings of Rijndael support. [Ben Laurie] *) Fix for bug in DirectoryString mask setting. Add support for X509_NAME_print_ex() in 'req' and X509_print_ex() function to allow certificate printing to more controllable, additional 'certopt' option to 'x509' to allow new printing options to be set. [Steve Henson] *) Clean old EAY MD5 hack from e_os.h. [Richard Levitte] Changes between 0.9.6l and 0.9.6m [17 Mar 2004] *) Fix null-pointer assignment in do_change_cipher_spec() revealed by using the Codenomicon TLS Test Tool (CVE-2004-0079) [Joe Orton, Steve Henson] Changes between 0.9.6k and 0.9.6l [04 Nov 2003] *) Fix additional bug revealed by the NISCC test suite: Stop bug triggering large recursion when presented with certain ASN.1 tags (CVE-2003-0851) [Steve Henson] Changes between 0.9.6j and 0.9.6k [30 Sep 2003] *) Fix various bugs revealed by running the NISCC test suite: Stop out of bounds reads in the ASN1 code when presented with invalid tags (CVE-2003-0543 and CVE-2003-0544). If verify callback ignores invalid public key errors don't try to check certificate signature with the NULL public key. [Steve Henson] *) In ssl3_accept() (ssl/s3_srvr.c) only accept a client certificate if the server requested one: as stated in TLS 1.0 and SSL 3.0 specifications. [Steve Henson] *) In ssl3_get_client_hello() (ssl/s3_srvr.c), tolerate additional extra data after the compression methods not only for TLS 1.0 but also for SSL 3.0 (as required by the specification). [Bodo Moeller; problem pointed out by Matthias Loepfe] *) Change X509_certificate_type() to mark the key as exported/exportable when it's 512 *bits* long, not 512 bytes. [Richard Levitte] Changes between 0.9.6i and 0.9.6j [10 Apr 2003] *) Countermeasure against the Klima-Pokorny-Rosa extension of Bleichbacher's attack on PKCS #1 v1.5 padding: treat a protocol version number mismatch like a decryption error in ssl3_get_client_key_exchange (ssl/s3_srvr.c). [Bodo Moeller] *) Turn on RSA blinding by default in the default implementation to avoid a timing attack. Applications that don't want it can call RSA_blinding_off() or use the new flag RSA_FLAG_NO_BLINDING. They would be ill-advised to do so in most cases. [Ben Laurie, Steve Henson, Geoff Thorpe, Bodo Moeller] *) Change RSA blinding code so that it works when the PRNG is not seeded (in this case, the secret RSA exponent is abused as an unpredictable seed -- if it is not unpredictable, there is no point in blinding anyway). Make RSA blinding thread-safe by remembering the creator's thread ID in rsa->blinding and having all other threads use local one-time blinding factors (this requires more computation than sharing rsa->blinding, but avoids excessive locking; and if an RSA object is not shared between threads, blinding will still be very fast). [Bodo Moeller] Changes between 0.9.6h and 0.9.6i [19 Feb 2003] *) In ssl3_get_record (ssl/s3_pkt.c), minimize information leaked via timing by performing a MAC computation even if incorrrect block cipher padding has been found. This is a countermeasure against active attacks where the attacker has to distinguish between bad padding and a MAC verification error. (CVE-2003-0078) [Bodo Moeller; problem pointed out by Brice Canvel (EPFL), Alain Hiltgen (UBS), Serge Vaudenay (EPFL), and Martin Vuagnoux (EPFL, Ilion)] Changes between 0.9.6g and 0.9.6h [5 Dec 2002] *) New function OPENSSL_cleanse(), which is used to cleanse a section of memory from it's contents. This is done with a counter that will place alternating values in each byte. This can be used to solve two issues: 1) the removal of calls to memset() by highly optimizing compilers, and 2) cleansing with other values than 0, since those can be read through on certain media, for example a swap space on disk. [Geoff Thorpe] *) Bugfix: client side session caching did not work with external caching, because the session->cipher setting was not restored when reloading from the external cache. This problem was masked, when SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG (part of SSL_OP_ALL) was set. (Found by Steve Haslam .) [Lutz Jaenicke] *) Fix client_certificate (ssl/s2_clnt.c): The permissible total length of the REQUEST-CERTIFICATE message is 18 .. 34, not 17 .. 33. [Zeev Lieber ] *) Undo an undocumented change introduced in 0.9.6e which caused repeated calls to OpenSSL_add_all_ciphers() and OpenSSL_add_all_digests() to be ignored, even after calling EVP_cleanup(). [Richard Levitte] *) Change the default configuration reader to deal with last line not being properly terminated. [Richard Levitte] *) Change X509_NAME_cmp() so it applies the special rules on handling DN values that are of type PrintableString, as well as RDNs of type emailAddress where the value has the type ia5String. [stefank@valicert.com via Richard Levitte] *) Add a SSL_SESS_CACHE_NO_INTERNAL_STORE flag to take over half the job SSL_SESS_CACHE_NO_INTERNAL_LOOKUP was inconsistently doing, define a new flag (SSL_SESS_CACHE_NO_INTERNAL) to be the bitwise-OR of the two for use by the majority of applications wanting this behaviour, and update the docs. The documented behaviour and actual behaviour were inconsistent and had been changing anyway, so this is more a bug-fix than a behavioural change. [Geoff Thorpe, diagnosed by Nadav Har'El] *) Don't impose a 16-byte length minimum on session IDs in ssl/s3_clnt.c (the SSL 3.0 and TLS 1.0 specifications allow any length up to 32 bytes). [Bodo Moeller] *) Fix initialization code race conditions in SSLv23_method(), SSLv23_client_method(), SSLv23_server_method(), SSLv2_method(), SSLv2_client_method(), SSLv2_server_method(), SSLv3_method(), SSLv3_client_method(), SSLv3_server_method(), TLSv1_method(), TLSv1_client_method(), TLSv1_server_method(), ssl2_get_cipher_by_char(), ssl3_get_cipher_by_char(). [Patrick McCormick , Bodo Moeller] *) Reorder cleanup sequence in SSL_CTX_free(): only remove the ex_data after the cached sessions are flushed, as the remove_cb() might use ex_data contents. Bug found by Sam Varshavchik (see [openssl.org #212]). [Geoff Thorpe, Lutz Jaenicke] *) Fix typo in OBJ_txt2obj which incorrectly passed the content length, instead of the encoding length to d2i_ASN1_OBJECT. [Steve Henson] Changes between 0.9.6f and 0.9.6g [9 Aug 2002] *) [In 0.9.6g-engine release:] Fix crypto/engine/vendor_defns/cswift.h for WIN32 (use '_stdcall'). [Lynn Gazis ] Changes between 0.9.6e and 0.9.6f [8 Aug 2002] *) Fix ASN1 checks. Check for overflow by comparing with LONG_MAX and get fix the header length calculation. [Florian Weimer , Alon Kantor (and others), Steve Henson] *) Use proper error handling instead of 'assertions' in buffer overflow checks added in 0.9.6e. This prevents DoS (the assertions could call abort()). [Arne Ansper , Bodo Moeller] Changes between 0.9.6d and 0.9.6e [30 Jul 2002] *) Add various sanity checks to asn1_get_length() to reject the ASN1 length bytes if they exceed sizeof(long), will appear negative or the content length exceeds the length of the supplied buffer. [Steve Henson, Adi Stav , James Yonan ] *) Fix cipher selection routines: ciphers without encryption had no flags for the cipher strength set and where therefore not handled correctly by the selection routines (PR #130). [Lutz Jaenicke] *) Fix EVP_dsa_sha macro. [Nils Larsch] *) New option SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS for disabling the SSL 3.0/TLS 1.0 CBC vulnerability countermeasure that was added in OpenSSL 0.9.6d. As the countermeasure turned out to be incompatible with some broken SSL implementations, the new option is part of SSL_OP_ALL. SSL_OP_ALL is usually employed when compatibility with weird SSL implementations is desired (e.g. '-bugs' option to 's_client' and 's_server'), so the new option is automatically set in many applications. [Bodo Moeller] *) Changes in security patch: Changes marked "(CHATS)" were sponsored by the Defense Advanced Research Projects Agency (DARPA) and Air Force Research Laboratory, Air Force Materiel Command, USAF, under agreement number F30602-01-2-0537. *) Add various sanity checks to asn1_get_length() to reject the ASN1 length bytes if they exceed sizeof(long), will appear negative or the content length exceeds the length of the supplied buffer. (CVE-2002-0659) [Steve Henson, Adi Stav , James Yonan ] *) Assertions for various potential buffer overflows, not known to happen in practice. [Ben Laurie (CHATS)] *) Various temporary buffers to hold ASCII versions of integers were too small for 64 bit platforms. (CVE-2002-0655) [Matthew Byng-Maddick and Ben Laurie (CHATS)> *) Remote buffer overflow in SSL3 protocol - an attacker could supply an oversized session ID to a client. (CVE-2002-0656) [Ben Laurie (CHATS)] *) Remote buffer overflow in SSL2 protocol - an attacker could supply an oversized client master key. (CVE-2002-0656) [Ben Laurie (CHATS)] Changes between 0.9.6c and 0.9.6d [9 May 2002] *) Fix crypto/asn1/a_sign.c so that 'parameters' is omitted (not encoded as NULL) with id-dsa-with-sha1. [Nils Larsch ; problem pointed out by Bodo Moeller] *) Check various X509_...() return values in apps/req.c. [Nils Larsch ] *) Fix BASE64 decode (EVP_DecodeUpdate) for data with CR/LF ended lines: an end-of-file condition would erronously be flagged, when the CRLF was just at the end of a processed block. The bug was discovered when processing data through a buffering memory BIO handing the data to a BASE64-decoding BIO. Bug fund and patch submitted by Pavel Tsekov and Nedelcho Stanev. [Lutz Jaenicke] *) Implement a countermeasure against a vulnerability recently found in CBC ciphersuites in SSL 3.0/TLS 1.0: Send an empty fragment before application data chunks to avoid the use of known IVs with data potentially chosen by the attacker. [Bodo Moeller] *) Fix length checks in ssl3_get_client_hello(). [Bodo Moeller] *) TLS/SSL library bugfix: use s->s3->in_read_app_data differently to prevent ssl3_read_internal() from incorrectly assuming that ssl3_read_bytes() found application data while handshake processing was enabled when in fact s->s3->in_read_app_data was merely automatically cleared during the initial handshake. [Bodo Moeller; problem pointed out by Arne Ansper ] *) Fix object definitions for Private and Enterprise: they were not recognized in their shortname (=lowercase) representation. Extend obj_dat.pl to issue an error when using undefined keywords instead of silently ignoring the problem (Svenning Sorensen ). [Lutz Jaenicke] *) Fix DH_generate_parameters() so that it works for 'non-standard' generators, i.e. generators other than 2 and 5. (Previously, the code did not properly initialise the 'add' and 'rem' values to BN_generate_prime().) In the new general case, we do not insist that 'generator' is actually a primitive root: This requirement is rather pointless; a generator of the order-q subgroup is just as good, if not better. [Bodo Moeller] *) Map new X509 verification errors to alerts. Discovered and submitted by Tom Wu . [Lutz Jaenicke] *) Fix ssl3_pending() (ssl/s3_lib.c) to prevent SSL_pending() from returning non-zero before the data has been completely received when using non-blocking I/O. [Bodo Moeller; problem pointed out by John Hughes] *) Some of the ciphers missed the strength entry (SSL_LOW etc). [Ben Laurie, Lutz Jaenicke] *) Fix bug in SSL_clear(): bad sessions were not removed (found by Yoram Zahavi ). [Lutz Jaenicke] *) Add information about CygWin 1.3 and on, and preserve proper configuration for the versions before that. [Corinna Vinschen and Richard Levitte] *) Make removal from session cache (SSL_CTX_remove_session()) more robust: check whether we deal with a copy of a session and do not delete from the cache in this case. Problem reported by "Izhar Shoshani Levi" . [Lutz Jaenicke] *) Do not store session data into the internal session cache, if it is never intended to be looked up (SSL_SESS_CACHE_NO_INTERNAL_LOOKUP flag is set). Proposed by Aslam . [Lutz Jaenicke] *) Have ASN1_BIT_STRING_set_bit() really clear a bit when the requested value is 0. [Richard Levitte] *) [In 0.9.6d-engine release:] Fix a crashbug and a logic bug in hwcrhk_load_pubkey(). [Toomas Kiisk via Richard Levitte] *) Add the configuration target linux-s390x. [Neale Ferguson via Richard Levitte] *) The earlier bugfix for the SSL3_ST_SW_HELLO_REQ_C case of ssl3_accept (ssl/s3_srvr.c) incorrectly used a local flag variable as an indication that a ClientHello message has been received. As the flag value will be lost between multiple invocations of ssl3_accept when using non-blocking I/O, the function may not be aware that a handshake has actually taken place, thus preventing a new session from being added to the session cache. To avoid this problem, we now set s->new_session to 2 instead of using a local variable. [Lutz Jaenicke, Bodo Moeller] *) Bugfix: Return -1 from ssl3_get_server_done (ssl3/s3_clnt.c) if the SSL_R_LENGTH_MISMATCH error is detected. [Geoff Thorpe, Bodo Moeller] *) New 'shared_ldflag' column in Configure platform table. [Richard Levitte] *) Fix EVP_CIPHER_mode macro. ["Dan S. Camper" ] *) Fix ssl3_read_bytes (ssl/s3_pkt.c): To ignore messages of unknown type, we must throw them away by setting rr->length to 0. [D P Chang ] Changes between 0.9.6b and 0.9.6c [21 dec 2001] *) Fix BN_rand_range bug pointed out by Dominikus Scherkl . (The previous implementation worked incorrectly for those cases where range = 10..._2 and 3*range is two bits longer than range.) [Bodo Moeller] *) Only add signing time to PKCS7 structures if it is not already present. [Steve Henson] *) Fix crypto/objects/objects.h: "ld-ce" should be "id-ce", OBJ_ld_ce should be OBJ_id_ce. Also some ip-pda OIDs in crypto/objects/objects.txt were incorrect (cf. RFC 3039). [Matt Cooper, Frederic Giudicelli, Bodo Moeller] *) Release CRYPTO_LOCK_DYNLOCK when CRYPTO_destroy_dynlockid() returns early because it has nothing to do. [Andy Schneider ] *) [In 0.9.6c-engine release:] Fix mutex callback return values in crypto/engine/hw_ncipher.c. [Andy Schneider ] *) [In 0.9.6c-engine release:] Add support for Cryptographic Appliance's keyserver technology. (Use engine 'keyclient') [Cryptographic Appliances and Geoff Thorpe] *) Add a configuration entry for OS/390 Unix. The C compiler 'c89' is called via tools/c89.sh because arguments have to be rearranged (all '-L' options must appear before the first object modules). [Richard Shapiro ] *) [In 0.9.6c-engine release:] Add support for Broadcom crypto accelerator cards, backported from 0.9.7. [Broadcom, Nalin Dahyabhai , Mark Cox] *) [In 0.9.6c-engine release:] Add support for SureWare crypto accelerator cards from Baltimore Technologies. (Use engine 'sureware') [Baltimore Technologies and Mark Cox] *) [In 0.9.6c-engine release:] Add support for crypto accelerator cards from Accelerated Encryption Processing, www.aep.ie. (Use engine 'aep') [AEP Inc. and Mark Cox] *) Add a configuration entry for gcc on UnixWare. [Gary Benson ] *) Change ssl/s2_clnt.c and ssl/s2_srvr.c so that received handshake messages are stored in a single piece (fixed-length part and variable-length part combined) and fix various bugs found on the way. [Bodo Moeller] *) Disable caching in BIO_gethostbyname(), directly use gethostbyname() instead. BIO_gethostbyname() does not know what timeouts are appropriate, so entries would stay in cache even when they have become invalid. [Bodo Moeller; problem pointed out by Rich Salz *) Change ssl23_get_client_hello (ssl/s23_srvr.c) behaviour when faced with a pathologically small ClientHello fragment that does not contain client_version: Instead of aborting with an error, simply choose the highest available protocol version (i.e., TLS 1.0 unless it is disabled). In practice, ClientHello messages are never sent like this, but this change gives us strictly correct behaviour at least for TLS. [Bodo Moeller] *) Fix SSL handshake functions and SSL_clear() such that SSL_clear() never resets s->method to s->ctx->method when called from within one of the SSL handshake functions. [Bodo Moeller; problem pointed out by Niko Baric] *) In ssl3_get_client_hello (ssl/s3_srvr.c), generate a fatal alert (sent using the client's version number) if client_version is smaller than the protocol version in use. Also change ssl23_get_client_hello (ssl/s23_srvr.c) to select TLS 1.0 if the client demanded SSL 3.0 but only TLS 1.0 is enabled; then the client will at least see that alert. [Bodo Moeller] *) Fix ssl3_get_message (ssl/s3_both.c) to handle message fragmentation correctly. [Bodo Moeller] *) Avoid infinite loop in ssl3_get_message (ssl/s3_both.c) if a client receives HelloRequest while in a handshake. [Bodo Moeller; bug noticed by Andy Schneider ] *) Bugfix in ssl3_accept (ssl/s3_srvr.c): Case SSL3_ST_SW_HELLO_REQ_C should end in 'break', not 'goto end' which circuments various cleanups done in state SSL_ST_OK. But session related stuff must be disabled for SSL_ST_OK in the case that we just sent a HelloRequest. Also avoid some overhead by not calling ssl_init_wbio_buffer() before just sending a HelloRequest. [Bodo Moeller, Eric Rescorla ] *) Fix ssl/s3_enc.c, ssl/t1_enc.c and ssl/s3_pkt.c so that we don't reveal whether illegal block cipher padding was found or a MAC verification error occured. (Neither SSLerr() codes nor alerts are directly visible to potential attackers, but the information may leak via logfiles.) Similar changes are not required for the SSL 2.0 implementation because the number of padding bytes is sent in clear for SSL 2.0, and the extra bytes are just ignored. However ssl/s2_pkt.c failed to verify that the purported number of padding bytes is in the legal range. [Bodo Moeller] *) Add OpenUNIX-8 support including shared libraries (Boyd Lynn Gerber ). [Lutz Jaenicke] *) Improve RSA_padding_check_PKCS1_OAEP() check again to avoid 'wristwatch attack' using huge encoding parameters (cf. James H. Manger's CRYPTO 2001 paper). Note that the RSA_PKCS1_OAEP_PADDING case of RSA_private_decrypt() does not use encoding parameters and hence was not vulnerable. [Bodo Moeller] *) BN_sqr() bug fix. [Ulf Möller, reported by Jim Ellis ] *) Rabin-Miller test analyses assume uniformly distributed witnesses, so use BN_pseudo_rand_range() instead of using BN_pseudo_rand() followed by modular reduction. [Bodo Moeller; pointed out by Adam Young ] *) Add BN_pseudo_rand_range() with obvious functionality: BN_rand_range() equivalent based on BN_pseudo_rand() instead of BN_rand(). [Bodo Moeller] *) s3_srvr.c: allow sending of large client certificate lists (> 16 kB). This function was broken, as the check for a new client hello message to handle SGC did not allow these large messages. (Tracked down by "Douglas E. Engert" .) [Lutz Jaenicke] *) Add alert descriptions for TLSv1 to SSL_alert_desc_string[_long](). [Lutz Jaenicke] *) Fix buggy behaviour of BIO_get_num_renegotiates() and BIO_ctrl() for BIO_C_GET_WRITE_BUF_SIZE ("Stephen Hinton" ). [Lutz Jaenicke] *) Rework the configuration and shared library support for Tru64 Unix. The configuration part makes use of modern compiler features and still retains old compiler behavior for those that run older versions of the OS. The shared library support part includes a variant that uses the RPATH feature, and is available through the special configuration target "alpha-cc-rpath", which will never be selected automatically. [Tim Mooney via Richard Levitte] *) In ssl3_get_key_exchange (ssl/s3_clnt.c), call ssl3_get_message() with the same message size as in ssl3_get_certificate_request(). Otherwise, if no ServerKeyExchange message occurs, CertificateRequest messages might inadvertently be reject as too long. [Petr Lampa ] *) Enhanced support for IA-64 Unix platforms (well, Linux and HP-UX). [Andy Polyakov] *) Modified SSL library such that the verify_callback that has been set specificly for an SSL object with SSL_set_verify() is actually being used. Before the change, a verify_callback set with this function was ignored and the verify_callback() set in the SSL_CTX at the time of the call was used. New function X509_STORE_CTX_set_verify_cb() introduced to allow the necessary settings. [Lutz Jaenicke] *) Initialize static variable in crypto/dsa/dsa_lib.c and crypto/dh/dh_lib.c explicitly to NULL, as at least on Solaris 8 this seems not always to be done automatically (in contradiction to the requirements of the C standard). This made problems when used from OpenSSH. [Lutz Jaenicke] *) In OpenSSL 0.9.6a and 0.9.6b, crypto/dh/dh_key.c ignored dh->length and always used BN_rand_range(priv_key, dh->p). BN_rand_range() is not necessary for Diffie-Hellman, and this specific range makes Diffie-Hellman unnecessarily inefficient if dh->length (recommended exponent length) is much smaller than the length of dh->p. We could use BN_rand_range() if the order of the subgroup was stored in the DH structure, but we only have dh->length. So switch back to BN_rand(priv_key, l, ...) where 'l' is dh->length if this is defined, or BN_num_bits(dh->p)-1 otherwise. [Bodo Moeller] *) In RSA_eay_public_encrypt RSA_eay_private_decrypt RSA_eay_private_encrypt (signing) RSA_eay_public_decrypt (signature verification) (default implementations for RSA_public_encrypt, RSA_private_decrypt, RSA_private_encrypt, RSA_public_decrypt), always reject numbers >= n. [Bodo Moeller] *) In crypto/rand/md_rand.c, use a new short-time lock CRYPTO_LOCK_RAND2 to synchronize access to 'locking_thread'. This is necessary on systems where access to 'locking_thread' (an 'unsigned long' variable) is not atomic. [Bodo Moeller] *) In crypto/rand/md_rand.c, set 'locking_thread' to current thread's ID *before* setting the 'crypto_lock_rand' flag. The previous code had a race condition if 0 is a valid thread ID. [Travis Vitek ] *) Add support for shared libraries under Irix. [Albert Chin-A-Young ] *) Add configuration option to build on Linux on both big-endian and little-endian MIPS. [Ralf Baechle ] *) Add the possibility to create shared libraries on HP-UX. [Richard Levitte] Changes between 0.9.6a and 0.9.6b [9 Jul 2001] *) Change ssleay_rand_bytes (crypto/rand/md_rand.c) to avoid a SSLeay/OpenSSL PRNG weakness pointed out by Markku-Juhani O. Saarinen : PRNG state recovery was possible based on the output of one PRNG request appropriately sized to gain knowledge on 'md' followed by enough consecutive 1-byte PRNG requests to traverse all of 'state'. 1. When updating 'md_local' (the current thread's copy of 'md') during PRNG output generation, hash all of the previous 'md_local' value, not just the half used for PRNG output. 2. Make the number of bytes from 'state' included into the hash independent from the number of PRNG bytes requested. The first measure alone would be sufficient to avoid Markku-Juhani's attack. (Actually it had never occurred to me that the half of 'md_local' used for chaining was the half from which PRNG output bytes were taken -- I had always assumed that the secret half would be used.) The second measure makes sure that additional data from 'state' is never mixed into 'md_local' in small portions; this heuristically further strengthens the PRNG. [Bodo Moeller] *) Fix crypto/bn/asm/mips3.s. [Andy Polyakov] *) When only the key is given to "enc", the IV is undefined. Print out an error message in this case. [Lutz Jaenicke] *) Handle special case when X509_NAME is empty in X509 printing routines. [Steve Henson] *) In dsa_do_verify (crypto/dsa/dsa_ossl.c), verify that r and s are positive and less than q. [Bodo Moeller] *) Don't change *pointer in CRYPTO_add_lock() is add_lock_callback is used: it isn't thread safe and the add_lock_callback should handle that itself. [Paul Rose ] *) Verify that incoming data obeys the block size in ssl3_enc (ssl/s3_enc.c) and tls1_enc (ssl/t1_enc.c). [Bodo Moeller] *) Fix OAEP check. [Ulf Möller, Bodo Möller] *) The countermeasure against Bleichbacher's attack on PKCS #1 v1.5 RSA encryption was accidentally removed in s3_srvr.c in OpenSSL 0.9.5 when fixing the server behaviour for backwards-compatible 'client hello' messages. (Note that the attack is impractical against SSL 3.0 and TLS 1.0 anyway because length and version checking means that the probability of guessing a valid ciphertext is around 2^-40; see section 5 in Bleichenbacher's CRYPTO '98 paper.) Before 0.9.5, the countermeasure (hide the error by generating a random 'decryption result') did not work properly because ERR_clear_error() was missing, meaning that SSL_get_error() would detect the supposedly ignored error. Both problems are now fixed. [Bodo Moeller] *) In crypto/bio/bf_buff.c, increase DEFAULT_BUFFER_SIZE to 4096 (previously it was 1024). [Bodo Moeller] *) Fix for compatibility mode trust settings: ignore trust settings unless some valid trust or reject settings are present. [Steve Henson] *) Fix for blowfish EVP: its a variable length cipher. [Steve Henson] *) Fix various bugs related to DSA S/MIME verification. Handle missing parameters in DSA public key structures and return an error in the DSA routines if parameters are absent. [Steve Henson] *) In versions up to 0.9.6, RAND_file_name() resorted to file ".rnd" in the current directory if neither $RANDFILE nor $HOME was set. RAND_file_name() in 0.9.6a returned NULL in this case. This has caused some confusion to Windows users who haven't defined $HOME. Thus RAND_file_name() is changed again: e_os.h can define a DEFAULT_HOME, which will be used if $HOME is not set. For Windows, we use "C:"; on other platforms, we still require environment variables. *) Move 'if (!initialized) RAND_poll()' into regions protected by CRYPTO_LOCK_RAND. This is not strictly necessary, but avoids having multiple threads call RAND_poll() concurrently. [Bodo Moeller] *) In crypto/rand/md_rand.c, replace 'add_do_not_lock' flag by a combination of a flag and a thread ID variable. Otherwise while one thread is in ssleay_rand_bytes (which sets the flag), *other* threads can enter ssleay_add_bytes without obeying the CRYPTO_LOCK_RAND lock (and may even illegally release the lock that they do not hold after the first thread unsets add_do_not_lock). [Bodo Moeller] *) Change bctest again: '-x' expressions are not available in all versions of 'test'. [Bodo Moeller] Changes between 0.9.6 and 0.9.6a [5 Apr 2001] *) Fix a couple of memory leaks in PKCS7_dataDecode() [Steve Henson, reported by Heyun Zheng ] *) Change Configure and Makefiles to provide EXE_EXT, which will contain the default extension for executables, if any. Also, make the perl scripts that use symlink() to test if it really exists and use "cp" if it doesn't. All this made OpenSSL compilable and installable in CygWin. [Richard Levitte] *) Fix for asn1_GetSequence() for indefinite length constructed data. If SEQUENCE is length is indefinite just set c->slen to the total amount of data available. [Steve Henson, reported by shige@FreeBSD.org] [This change does not apply to 0.9.7.] *) Change bctest to avoid here-documents inside command substitution (workaround for FreeBSD /bin/sh bug). For compatibility with Ultrix, avoid shell functions (introduced in the bctest version that searches along $PATH). [Bodo Moeller] *) Rename 'des_encrypt' to 'des_encrypt1'. This avoids the clashes with des_encrypt() defined on some operating systems, like Solaris and UnixWare. [Richard Levitte] *) Check the result of RSA-CRT (see D. Boneh, R. DeMillo, R. Lipton: On the Importance of Eliminating Errors in Cryptographic Computations, J. Cryptology 14 (2001) 2, 101-119, http://theory.stanford.edu/~dabo/papers/faults.ps.gz). [Ulf Moeller] *) MIPS assembler BIGNUM division bug fix. [Andy Polyakov] *) Disabled incorrect Alpha assembler code. [Richard Levitte] *) Fix PKCS#7 decode routines so they correctly update the length after reading an EOC for the EXPLICIT tag. [Steve Henson] [This change does not apply to 0.9.7.] *) Fix bug in PKCS#12 key generation routines. This was triggered if a 3DES key was generated with a 0 initial byte. Include PKCS12_BROKEN_KEYGEN compilation option to retain the old (but broken) behaviour. [Steve Henson] *) Enhance bctest to search for a working bc along $PATH and print it when found. [Tim Rice via Richard Levitte] *) Fix memory leaks in err.c: free err_data string if necessary; don't write to the wrong index in ERR_set_error_data. [Bodo Moeller] *) Implement ssl23_peek (analogous to ssl23_read), which previously did not exist. [Bodo Moeller] *) Replace rdtsc with _emit statements for VC++ version 5. [Jeremy Cooper ] *) Make it possible to reuse SSLv2 sessions. [Richard Levitte] *) In copy_email() check for >= 0 as a return value for X509_NAME_get_index_by_NID() since 0 is a valid index. [Steve Henson reported by Massimiliano Pala ] *) Avoid coredump with unsupported or invalid public keys by checking if X509_get_pubkey() fails in PKCS7_verify(). Fix memory leak when PKCS7_verify() fails with non detached data. [Steve Henson] *) Don't use getenv in library functions when run as setuid/setgid. New function OPENSSL_issetugid(). [Ulf Moeller] *) Avoid false positives in memory leak detection code (crypto/mem_dbg.c) due to incorrect handling of multi-threading: 1. Fix timing glitch in the MemCheck_off() portion of CRYPTO_mem_ctrl(). 2. Fix logical glitch in is_MemCheck_on() aka CRYPTO_is_mem_check_on(). 3. Count how many times MemCheck_off() has been called so that nested use can be treated correctly. This also avoids inband-signalling in the previous code (which relied on the assumption that thread ID 0 is impossible). [Bodo Moeller] *) Add "-rand" option also to s_client and s_server. [Lutz Jaenicke] *) Fix CPU detection on Irix 6.x. [Kurt Hockenbury and "Bruce W. Forsberg" ] *) Fix X509_NAME bug which produced incorrect encoding if X509_NAME was empty. [Steve Henson] [This change does not apply to 0.9.7.] *) Use the cached encoding of an X509_NAME structure rather than copying it. This is apparently the reason for the libsafe "errors" but the code is actually correct. [Steve Henson] *) Add new function BN_rand_range(), and fix DSA_sign_setup() to prevent Bleichenbacher's DSA attack. Extend BN_[pseudo_]rand: As before, top=1 forces the highest two bits to be set and top=0 forces the highest bit to be set; top=-1 is new and leaves the highest bit random. [Ulf Moeller, Bodo Moeller] *) In the NCONF_...-based implementations for CONF_... queries (crypto/conf/conf_lib.c), if the input LHASH is NULL, avoid using a temporary CONF structure with the data component set to NULL (which gives segmentation faults in lh_retrieve). Instead, use NULL for the CONF pointer in CONF_get_string and CONF_get_number (which may use environment variables) and directly return NULL from CONF_get_section. [Bodo Moeller] *) Fix potential buffer overrun for EBCDIC. [Ulf Moeller] *) Tolerate nonRepudiation as being valid for S/MIME signing and certSign keyUsage if basicConstraints absent for a CA. [Steve Henson] *) Make SMIME_write_PKCS7() write mail header values with a format that is more generally accepted (no spaces before the semicolon), since some programs can't parse those values properly otherwise. Also make sure BIO's that break lines after each write do not create invalid headers. [Richard Levitte] *) Make the CRL encoding routines work with empty SEQUENCE OF. The macros previously used would not encode an empty SEQUENCE OF and break the signature. [Steve Henson] [This change does not apply to 0.9.7.] *) Zero the premaster secret after deriving the master secret in DH ciphersuites. [Steve Henson] *) Add some EVP_add_digest_alias registrations (as found in OpenSSL_add_all_digests()) to SSL_library_init() aka OpenSSL_add_ssl_algorithms(). This provides improved compatibility with peers using X.509 certificates with unconventional AlgorithmIdentifier OIDs. [Bodo Moeller] *) Fix for Irix with NO_ASM. ["Bruce W. Forsberg" ] *) ./config script fixes. [Ulf Moeller, Richard Levitte] *) Fix 'openssl passwd -1'. [Bodo Moeller] *) Change PKCS12_key_gen_asc() so it can cope with non null terminated strings whose length is passed in the passlen parameter, for example from PEM callbacks. This was done by adding an extra length parameter to asc2uni(). [Steve Henson, reported by ] *) Fix C code generated by 'openssl dsaparam -C': If a BN_bin2bn call failed, free the DSA structure. [Bodo Moeller] *) Fix to uni2asc() to cope with zero length Unicode strings. These are present in some PKCS#12 files. [Steve Henson] *) Increase s2->wbuf allocation by one byte in ssl2_new (ssl/s2_lib.c). Otherwise do_ssl_write (ssl/s2_pkt.c) will write beyond buffer limits when writing a 32767 byte record. [Bodo Moeller; problem reported by Eric Day ] *) In RSA_eay_public_{en,ed}crypt and RSA_eay_mod_exp (rsa_eay.c), obtain lock CRYPTO_LOCK_RSA before setting rsa->_method_mod_{n,p,q}. (RSA objects have a reference count access to which is protected by CRYPTO_LOCK_RSA [see rsa_lib.c, s3_srvr.c, ssl_cert.c, ssl_rsa.c], so they are meant to be shared between threads.) [Bodo Moeller, Geoff Thorpe; original patch submitted by "Reddie, Steven" ] *) Fix a deadlock in CRYPTO_mem_leaks(). [Bodo Moeller] *) Use better test patterns in bntest. [Ulf Möller] *) rand_win.c fix for Borland C. [Ulf Möller] *) BN_rshift bugfix for n == 0. [Bodo Moeller] *) Add a 'bctest' script that checks for some known 'bc' bugs so that 'make test' does not abort just because 'bc' is broken. [Bodo Moeller] *) Store verify_result within SSL_SESSION also for client side to avoid potential security hole. (Re-used sessions on the client side always resulted in verify_result==X509_V_OK, not using the original result of the server certificate verification.) [Lutz Jaenicke] *) Fix ssl3_pending: If the record in s->s3->rrec is not of type SSL3_RT_APPLICATION_DATA, return 0. Similarly, change ssl2_pending to return 0 if SSL_in_init(s) is true. [Bodo Moeller] *) Fix SSL_peek: Both ssl2_peek and ssl3_peek, which were totally broken in earlier releases, have been re-implemented by renaming the previous implementations of ssl2_read and ssl3_read to ssl2_read_internal and ssl3_read_internal, respectively, and adding 'peek' parameters to them. The new ssl[23]_{read,peek} functions are calls to ssl[23]_read_internal with the 'peek' flag set appropriately. A 'peek' parameter has also been added to ssl3_read_bytes, which does the actual work for ssl3_read_internal. [Bodo Moeller] *) Initialise "ex_data" member of RSA/DSA/DH structures prior to calling the method-specific "init()" handler. Also clean up ex_data after calling the method-specific "finish()" handler. Previously, this was happening the other way round. [Geoff Thorpe] *) Increase BN_CTX_NUM (the number of BIGNUMs in a BN_CTX) to 16. The previous value, 12, was not always sufficient for BN_mod_exp(). [Bodo Moeller] *) Make sure that shared libraries get the internal name engine with the full version number and not just 0. This should mark the shared libraries as not backward compatible. Of course, this should be changed again when we can guarantee backward binary compatibility. [Richard Levitte] *) Fix typo in get_cert_by_subject() in by_dir.c [Jean-Marc Desperrier ] *) Rework the system to generate shared libraries: - Make note of the expected extension for the shared libraries and if there is a need for symbolic links from for example libcrypto.so.0 to libcrypto.so.0.9.7. There is extended info in Configure for that. - Make as few rebuilds of the shared libraries as possible. - Still avoid linking the OpenSSL programs with the shared libraries. - When installing, install the shared libraries separately from the static ones. [Richard Levitte] *) Fix SSL_CTX_set_read_ahead macro to actually use its argument. Copy SSL_CTX's read_ahead flag to SSL object directly in SSL_new and not in SSL_clear because the latter is also used by the accept/connect functions; previously, the settings made by SSL_set_read_ahead would be lost during the handshake. [Bodo Moeller; problems reported by Anders Gertz ] *) Correct util/mkdef.pl to be selective about disabled algorithms. Previously, it would create entries for disableed algorithms no matter what. [Richard Levitte] *) Added several new manual pages for SSL_* function. [Lutz Jaenicke] Changes between 0.9.5a and 0.9.6 [24 Sep 2000] *) In ssl23_get_client_hello, generate an error message when faced with an initial SSL 3.0/TLS record that is too small to contain the first two bytes of the ClientHello message, i.e. client_version. (Note that this is a pathologic case that probably has never happened in real life.) The previous approach was to use the version number from the record header as a substitute; but our protocol choice should not depend on that one because it is not authenticated by the Finished messages. [Bodo Moeller] *) More robust randomness gathering functions for Windows. [Jeffrey Altman ] *) For compatibility reasons if the flag X509_V_FLAG_ISSUER_CHECK is not set then we don't setup the error code for issuer check errors to avoid possibly overwriting other errors which the callback does handle. If an application does set the flag then we assume it knows what it is doing and can handle the new informational codes appropriately. [Steve Henson] *) Fix for a nasty bug in ASN1_TYPE handling. ASN1_TYPE is used for a general "ANY" type, as such it should be able to decode anything including tagged types. However it didn't check the class so it would wrongly interpret tagged types in the same way as their universal counterpart and unknown types were just rejected. Changed so that the tagged and unknown types are handled in the same way as a SEQUENCE: that is the encoding is stored intact. There is also a new type "V_ASN1_OTHER" which is used when the class is not universal, in this case we have no idea what the actual type is so we just lump them all together. [Steve Henson] *) On VMS, stdout may very well lead to a file that is written to in a record-oriented fashion. That means that every write() will write a separate record, which will be read separately by the programs trying to read from it. This can be very confusing. The solution is to put a BIO filter in the way that will buffer text until a linefeed is reached, and then write everything a line at a time, so every record written will be an actual line, not chunks of lines and not (usually doesn't happen, but I've seen it once) several lines in one record. BIO_f_linebuffer() is the answer. Currently, it's a VMS-only method, because that's where it has been tested well enough. [Richard Levitte] *) Remove 'optimized' squaring variant in BN_mod_mul_montgomery, it can return incorrect results. (Note: The buggy variant was not enabled in OpenSSL 0.9.5a, but it was in 0.9.6-beta[12].) [Bodo Moeller] *) Disable the check for content being present when verifying detached signatures in pk7_smime.c. Some versions of Netscape (wrongly) include zero length content when signing messages. [Steve Henson] *) New BIO_shutdown_wr macro, which invokes the BIO_C_SHUTDOWN_WR BIO_ctrl (for BIO pairs). [Bodo Möller] *) Add DSO method for VMS. [Richard Levitte] *) Bug fix: Montgomery multiplication could produce results with the wrong sign. [Ulf Möller] *) Add RPM specification openssl.spec and modify it to build three packages. The default package contains applications, application documentation and run-time libraries. The devel package contains include files, static libraries and function documentation. The doc package contains the contents of the doc directory. The original openssl.spec was provided by Damien Miller . [Richard Levitte] *) Add a large number of documentation files for many SSL routines. [Lutz Jaenicke ] *) Add a configuration entry for Sony News 4. [NAKAJI Hiroyuki ] *) Don't set the two most significant bits to one when generating a random number < q in the DSA library. [Ulf Möller] *) New SSL API mode 'SSL_MODE_AUTO_RETRY'. This disables the default behaviour that SSL_read may result in SSL_ERROR_WANT_READ (even if the underlying transport is blocking) if a handshake took place. (The default behaviour is needed by applications such as s_client and s_server that use select() to determine when to use SSL_read; but for applications that know in advance when to expect data, it just makes things more complicated.) [Bodo Moeller] *) Add RAND_egd_bytes(), which gives control over the number of bytes read from EGD. [Ben Laurie] *) Add a few more EBCDIC conditionals that make `req' and `x509' work better on such systems. [Martin Kraemer ] *) Add two demo programs for PKCS12_parse() and PKCS12_create(). Update PKCS12_parse() so it copies the friendlyName and the keyid to the certificates aux info. [Steve Henson] *) Fix bug in PKCS7_verify() which caused an infinite loop if there was more than one signature. [Sven Uszpelkat ] *) Major change in util/mkdef.pl to include extra information about each symbol, as well as presentig variables as well as functions. This change means that there's n more need to rebuild the .num files when some algorithms are excluded. [Richard Levitte] *) Allow the verify time to be set by an application, rather than always using the current time. [Steve Henson] *) Phase 2 verify code reorganisation. The certificate verify code now looks up an issuer certificate by a number of criteria: subject name, authority key id and key usage. It also verifies self signed certificates by the same criteria. The main comparison function is X509_check_issued() which performs these checks. Lot of changes were necessary in order to support this without completely rewriting the lookup code. Authority and subject key identifier are now cached. The LHASH 'certs' is X509_STORE has now been replaced by a STACK_OF(X509_OBJECT). This is mainly because an LHASH can't store or retrieve multiple objects with the same hash value. As a result various functions (which were all internal use only) have changed to handle the new X509_STORE structure. This will break anything that messed round with X509_STORE internally. The functions X509_STORE_add_cert() now checks for an exact match, rather than just subject name. The X509_STORE API doesn't directly support the retrieval of multiple certificates matching a given criteria, however this can be worked round by performing a lookup first (which will fill the cache with candidate certificates) and then examining the cache for matches. This is probably the best we can do without throwing out X509_LOOKUP entirely (maybe later...). The X509_VERIFY_CTX structure has been enhanced considerably. All certificate lookup operations now go via a get_issuer() callback. Although this currently uses an X509_STORE it can be replaced by custom lookups. This is a simple way to bypass the X509_STORE hackery necessary to make this work and makes it possible to use more efficient techniques in future. A very simple version which uses a simple STACK for its trusted certificate store is also provided using X509_STORE_CTX_trusted_stack(). The verify_cb() and verify() callbacks now have equivalents in the X509_STORE_CTX structure. X509_STORE_CTX also has a 'flags' field which can be used to customise the verify behaviour. [Steve Henson] *) Add new PKCS#7 signing option PKCS7_NOSMIMECAP which excludes S/MIME capabilities. [Steve Henson] *) When a certificate request is read in keep a copy of the original encoding of the signed data and use it when outputing again. Signatures then use the original encoding rather than a decoded, encoded version which may cause problems if the request is improperly encoded. [Steve Henson] *) For consistency with other BIO_puts implementations, call buffer_write(b, ...) directly in buffer_puts instead of calling BIO_write(b, ...). In BIO_puts, increment b->num_write as in BIO_write. [Peter.Sylvester@EdelWeb.fr] *) Fix BN_mul_word for the case where the word is 0. (We have to use BN_zero, we may not return a BIGNUM with an array consisting of words set to zero.) [Bodo Moeller] *) Avoid calling abort() from within the library when problems are detected, except if preprocessor symbols have been defined (such as REF_CHECK, BN_DEBUG etc.). [Bodo Moeller] *) New openssl application 'rsautl'. This utility can be used for low level RSA operations. DER public key BIO/fp routines also added. [Steve Henson] *) New Configure entry and patches for compiling on QNX 4. [Andreas Schneider ] *) A demo state-machine implementation was sponsored by Nuron (http://www.nuron.com/) and is now available in demos/state_machine. [Ben Laurie] *) New options added to the 'dgst' utility for signature generation and verification. [Steve Henson] *) Unrecognized PKCS#7 content types are now handled via a catch all ASN1_TYPE structure. This allows unsupported types to be stored as a "blob" and an application can encode and decode it manually. [Steve Henson] *) Fix various signed/unsigned issues to make a_strex.c compile under VC++. [Oscar Jacobsson ] *) ASN1 fixes. i2d_ASN1_OBJECT was not returning the correct length if passed a buffer. ASN1_INTEGER_to_BN failed if passed a NULL BN and its argument was negative. [Steve Henson, pointed out by Sven Heiberg ] *) Modification to PKCS#7 encoding routines to output definite length encoding. Since currently the whole structures are in memory there's not real point in using indefinite length constructed encoding. However if OpenSSL is compiled with the flag PKCS7_INDEFINITE_ENCODING the old form is used. [Steve Henson] *) Added BIO_vprintf() and BIO_vsnprintf(). [Richard Levitte] *) Added more prefixes to parse for in the the strings written through a logging bio, to cover all the levels that are available through syslog. The prefixes are now: PANIC, EMERG, EMR => LOG_EMERG ALERT, ALR => LOG_ALERT CRIT, CRI => LOG_CRIT ERROR, ERR => LOG_ERR WARNING, WARN, WAR => LOG_WARNING NOTICE, NOTE, NOT => LOG_NOTICE INFO, INF => LOG_INFO DEBUG, DBG => LOG_DEBUG and as before, if none of those prefixes are present at the beginning of the string, LOG_ERR is chosen. On Win32, the LOG_* levels are mapped according to this: LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR => EVENTLOG_ERROR_TYPE LOG_WARNING => EVENTLOG_WARNING_TYPE LOG_NOTICE, LOG_INFO, LOG_DEBUG => EVENTLOG_INFORMATION_TYPE [Richard Levitte] *) Made it possible to reconfigure with just the configuration argument "reconf" or "reconfigure". The command line arguments are stored in Makefile.ssl in the variable CONFIGURE_ARGS, and are retrieved from there when reconfiguring. [Richard Levitte] *) MD4 implemented. [Assar Westerlund , Richard Levitte] *) Add the arguments -CAfile and -CApath to the pkcs12 utility. [Richard Levitte] *) The obj_dat.pl script was messing up the sorting of object names. The reason was that it compared the quoted version of strings as a result "OCSP" > "OCSP Signing" because " > SPACE. Changed script to store unquoted versions of names and add quotes on output. It was also omitting some names from the lookup table if they were given a default value (that is if SN is missing it is given the same value as LN and vice versa), these are now added on the grounds that if an object has a name we should be able to look it up. Finally added warning output when duplicate short or long names are found. [Steve Henson] *) Changes needed for Tandem NSK. [Scott Uroff ] *) Fix SSL 2.0 rollback checking: Due to an off-by-one error in RSA_padding_check_SSLv23(), special padding was never detected and thus the SSL 3.0/TLS 1.0 countermeasure against protocol version rollback attacks was not effective. In s23_clnt.c, don't use special rollback-attack detection padding (RSA_SSLV23_PADDING) if SSL 2.0 is the only protocol enabled in the client; similarly, in s23_srvr.c, don't do the rollback check if SSL 2.0 is the only protocol enabled in the server. [Bodo Moeller] *) Make it possible to get hexdumps of unprintable data with 'openssl asn1parse'. By implication, the functions ASN1_parse_dump() and BIO_dump_indent() are added. [Richard Levitte] *) New functions ASN1_STRING_print_ex() and X509_NAME_print_ex() these print out strings and name structures based on various flags including RFC2253 support and proper handling of multibyte characters. Added options to the 'x509' utility to allow the various flags to be set. [Steve Henson] *) Various fixes to use ASN1_TIME instead of ASN1_UTCTIME. Also change the functions X509_cmp_current_time() and X509_gmtime_adj() work with an ASN1_TIME structure, this will enable certificates using GeneralizedTime in validity dates to be checked. [Steve Henson] *) Make the NEG_PUBKEY_BUG code (which tolerates invalid negative public key encodings) on by default, NO_NEG_PUBKEY_BUG can be set to disable it. [Steve Henson] *) New function c2i_ASN1_OBJECT() which acts on ASN1_OBJECT content octets. An i2c_ASN1_OBJECT is unnecessary because the encoding can be trivially obtained from the structure. [Steve Henson] *) crypto/err.c locking bugfix: Use write locks (CRYPTO_w_[un]lock), not read locks (CRYPTO_r_[un]lock). [Bodo Moeller] *) A first attempt at creating official support for shared libraries through configuration. I've kept it so the default is static libraries only, and the OpenSSL programs are always statically linked for now, but there are preparations for dynamic linking in place. This has been tested on Linux and Tru64. [Richard Levitte] *) Randomness polling function for Win9x, as described in: Peter Gutmann, Software Generation of Practically Strong Random Numbers. [Ulf Möller] *) Fix so PRNG is seeded in req if using an already existing DSA key. [Steve Henson] *) New options to smime application. -inform and -outform allow alternative formats for the S/MIME message including PEM and DER. The -content option allows the content to be specified separately. This should allow things like Netscape form signing output easier to verify. [Steve Henson] *) Fix the ASN1 encoding of tags using the 'long form'. [Steve Henson] *) New ASN1 functions, i2c_* and c2i_* for INTEGER and BIT STRING types. These convert content octets to and from the underlying type. The actual tag and length octets are already assumed to have been read in and checked. These are needed because all other string types have virtually identical handling apart from the tag. By having versions of the ASN1 functions that just operate on content octets IMPLICIT tagging can be handled properly. It also allows the ASN1_ENUMERATED code to be cut down because ASN1_ENUMERATED and ASN1_INTEGER are identical apart from the tag. [Steve Henson] *) Change the handling of OID objects as follows: - New object identifiers are inserted in objects.txt, following the syntax given in objects.README. - objects.pl is used to process obj_mac.num and create a new obj_mac.h. - obj_dat.pl is used to create a new obj_dat.h, using the data in obj_mac.h. This is currently kind of a hack, and the perl code in objects.pl isn't very elegant, but it works as I intended. The simplest way to check that it worked correctly is to look in obj_dat.h and check the array nid_objs and make sure the objects haven't moved around (this is important!). Additions are OK, as well as consistent name changes. [Richard Levitte] *) Add BSD-style MD5-based passwords to 'openssl passwd' (option '-1'). [Bodo Moeller] *) Addition of the command line parameter '-rand file' to 'openssl req'. The given file adds to whatever has already been seeded into the random pool through the RANDFILE configuration file option or environment variable, or the default random state file. [Richard Levitte] *) mkstack.pl now sorts each macro group into lexical order. Previously the output order depended on the order the files appeared in the directory, resulting in needless rewriting of safestack.h . [Steve Henson] *) Patches to make OpenSSL compile under Win32 again. Mostly work arounds for the VC++ problem that it treats func() as func(void). Also stripped out the parts of mkdef.pl that added extra typesafe functions: these no longer exist. [Steve Henson] *) Reorganisation of the stack code. The macros are now all collected in safestack.h . Each macro is defined in terms of a "stack macro" of the form SKM_(type, a, b). The DEBUG_SAFESTACK is now handled in terms of function casts, this has the advantage of retaining type safety without the use of additional functions. If DEBUG_SAFESTACK is not defined then the non typesafe macros are used instead. Also modified the mkstack.pl script to handle the new form. Needs testing to see if which (if any) compilers it chokes and maybe make DEBUG_SAFESTACK the default if no major problems. Similar behaviour for ASN1_SET_OF and PKCS12_STACK_OF. [Steve Henson] *) When some versions of IIS use the 'NET' form of private key the key derivation algorithm is different. Normally MD5(password) is used as a 128 bit RC4 key. In the modified case MD5(MD5(password) + "SGCKEYSALT") is used insted. Added some new functions i2d_RSA_NET(), d2i_RSA_NET() etc which are the same as the old Netscape_RSA functions except they have an additional 'sgckey' parameter which uses the modified algorithm. Also added an -sgckey command line option to the rsa utility. Thanks to Adrian Peck for posting details of the modified algorithm to openssl-dev. [Steve Henson] *) The evp_local.h macros were using 'c.##kname' which resulted in invalid expansion on some systems (SCO 5.0.5 for example). Corrected to 'c.kname'. [Phillip Porch ] *) New X509_get1_email() and X509_REQ_get1_email() functions that return a STACK of email addresses from a certificate or request, these look in the subject name and the subject alternative name extensions and omit any duplicate addresses. [Steve Henson] *) Re-implement BN_mod_exp2_mont using independent (and larger) windows. This makes DSA verification about 2 % faster. [Bodo Moeller] *) Increase maximum window size in BN_mod_exp_... to 6 bits instead of 5 (meaning that now 2^5 values will be precomputed, which is only 4 KB plus overhead for 1024 bit moduli). This makes exponentiations about 0.5 % faster for 1024 bit exponents (as measured by "openssl speed rsa2048"). [Bodo Moeller] *) Rename memory handling macros to avoid conflicts with other software: Malloc => OPENSSL_malloc Malloc_locked => OPENSSL_malloc_locked Realloc => OPENSSL_realloc Free => OPENSSL_free [Richard Levitte] *) New function BN_mod_exp_mont_word for small bases (roughly 15% faster than BN_mod_exp_mont, i.e. 7% for a full DH exchange). [Bodo Moeller] *) CygWin32 support. [John Jarvie ] *) The type-safe stack code has been rejigged. It is now only compiled in when OpenSSL is configured with the DEBUG_SAFESTACK option and by default all type-specific stack functions are "#define"d back to standard stack functions. This results in more streamlined output but retains the type-safety checking possibilities of the original approach. [Geoff Thorpe] *) The STACK code has been cleaned up, and certain type declarations that didn't make a lot of sense have been brought in line. This has also involved a cleanup of sorts in safestack.h to more correctly map type-safe stack functions onto their plain stack counterparts. This work has also resulted in a variety of "const"ifications of lots of the code, especially "_cmp" operations which should normally be prototyped with "const" parameters anyway. [Geoff Thorpe] *) When generating bytes for the first time in md_rand.c, 'stir the pool' by seeding with STATE_SIZE dummy bytes (with zero entropy count). (The PRNG state consists of two parts, the large pool 'state' and 'md', where all of 'md' is used each time the PRNG is used, but 'state' is used only indexed by a cyclic counter. As entropy may not be well distributed from the beginning, 'md' is important as a chaining variable. However, the output function chains only half of 'md', i.e. 80 bits. ssleay_rand_add, on the other hand, chains all of 'md', and seeding with STATE_SIZE dummy bytes will result in all of 'state' being rewritten, with the new values depending on virtually all of 'md'. This overcomes the 80 bit limitation.) [Bodo Moeller] *) In ssl/s2_clnt.c and ssl/s3_clnt.c, call ERR_clear_error() when the handshake is continued after ssl_verify_cert_chain(); otherwise, if SSL_VERIFY_NONE is set, remaining error codes can lead to 'unexplainable' connection aborts later. [Bodo Moeller; problem tracked down by Lutz Jaenicke] *) Major EVP API cipher revision. Add hooks for extra EVP features. This allows various cipher parameters to be set in the EVP interface. Support added for variable key length ciphers via the EVP_CIPHER_CTX_set_key_length() function and setting of RC2 and RC5 parameters. Modify EVP_OpenInit() and EVP_SealInit() to cope with variable key length ciphers. Remove lots of duplicated code from the EVP library. For example *every* cipher init() function handles the 'iv' in the same way according to the cipher mode. They also all do nothing if the 'key' parameter is NULL and for CFB and OFB modes they zero ctx->num. New functionality allows removal of S/MIME code RC2 hack. Most of the routines have the same form and so can be declared in terms of macros. By shifting this to the top level EVP_CipherInit() it can be removed from all individual ciphers. If the cipher wants to handle IVs or keys differently it can set the EVP_CIPH_CUSTOM_IV or EVP_CIPH_ALWAYS_CALL_INIT flags. Change lots of functions like EVP_EncryptUpdate() to now return a value: although software versions of the algorithms cannot fail any installed hardware versions can. [Steve Henson] *) Implement SSL_OP_TLS_ROLLBACK_BUG: In ssl3_get_client_key_exchange, if this option is set, tolerate broken clients that send the negotiated protocol version number instead of the requested protocol version number. [Bodo Moeller] *) Call dh_tmp_cb (set by ..._TMP_DH_CB) with correct 'is_export' flag; i.e. non-zero for export ciphersuites, zero otherwise. Previous versions had this flag inverted, inconsistent with rsa_tmp_cb (..._TMP_RSA_CB). [Bodo Moeller; problem reported by Amit Chopra] *) Add missing DSA library text string. Work around for some IIS key files with invalid SEQUENCE encoding. [Steve Henson] *) Add a document (doc/standards.txt) that list all kinds of standards and so on that are implemented in OpenSSL. [Richard Levitte] *) Enhance c_rehash script. Old version would mishandle certificates with the same subject name hash and wouldn't handle CRLs at all. Added -fingerprint option to crl utility, to support new c_rehash features. [Steve Henson] *) Eliminate non-ANSI declarations in crypto.h and stack.h. [Ulf Möller] *) Fix for SSL server purpose checking. Server checking was rejecting certificates which had extended key usage present but no ssl client purpose. [Steve Henson, reported by Rene Grosser ] *) Make PKCS#12 code work with no password. The PKCS#12 spec is a little unclear about how a blank password is handled. Since the password in encoded as a BMPString with terminating double NULL a zero length password would end up as just the double NULL. However no password at all is different and is handled differently in the PKCS#12 key generation code. NS treats a blank password as zero length. MSIE treats it as no password on export: but it will try both on import. We now do the same: PKCS12_parse() tries zero length and no password if the password is set to "" or NULL (NULL is now a valid password: it wasn't before) as does the pkcs12 application. [Steve Henson] *) Bugfixes in apps/x509.c: Avoid a memory leak; and don't use perror when PEM_read_bio_X509_REQ fails, the error message must be obtained from the error queue. [Bodo Moeller] *) Avoid 'thread_hash' memory leak in crypto/err/err.c by freeing it in ERR_remove_state if appropriate, and change ERR_get_state accordingly to avoid race conditions (this is necessary because thread_hash is no longer constant once set). [Bodo Moeller] *) Bugfix for linux-elf makefile.one. [Ulf Möller] *) RSA_get_default_method() will now cause a default RSA_METHOD to be chosen if one doesn't exist already. Previously this was only set during a call to RSA_new() or RSA_new_method(NULL) meaning it was possible for RSA_get_default_method() to return NULL. [Geoff Thorpe] *) Added native name translation to the existing DSO code that will convert (if the flag to do so is set) filenames that are sufficiently small and have no path information into a canonical native form. Eg. "blah" converted to "libblah.so" or "blah.dll" etc. [Geoff Thorpe] *) New function ERR_error_string_n(e, buf, len) which is like ERR_error_string(e, buf), but writes at most 'len' bytes including the 0 terminator. For ERR_error_string_n, 'buf' may not be NULL. [Damien Miller , Bodo Moeller] *) CONF library reworked to become more general. A new CONF configuration file reader "class" is implemented as well as a new functions (NCONF_*, for "New CONF") to handle it. The now old CONF_* functions are still there, but are reimplemented to work in terms of the new functions. Also, a set of functions to handle the internal storage of the configuration data is provided to make it easier to write new configuration file reader "classes" (I can definitely see something reading a configuration file in XML format, for example), called _CONF_*, or "the configuration storage API"... The new configuration file reading functions are: NCONF_new, NCONF_free, NCONF_load, NCONF_load_fp, NCONF_load_bio, NCONF_get_section, NCONF_get_string, NCONF_get_numbre NCONF_default, NCONF_WIN32 NCONF_dump_fp, NCONF_dump_bio NCONF_default and NCONF_WIN32 are method (or "class") choosers, NCONF_new creates a new CONF object. This works in the same way as other interfaces in OpenSSL, like the BIO interface. NCONF_dump_* dump the internal storage of the configuration file, which is useful for debugging. All other functions take the same arguments as the old CONF_* functions wth the exception of the first that must be a `CONF *' instead of a `LHASH *'. To make it easer to use the new classes with the old CONF_* functions, the function CONF_set_default_method is provided. [Richard Levitte] *) Add '-tls1' option to 'openssl ciphers', which was already mentioned in the documentation but had not been implemented. (This option is not yet really useful because even the additional experimental TLS 1.0 ciphers are currently treated as SSL 3.0 ciphers.) [Bodo Moeller] *) Initial DSO code added into libcrypto for letting OpenSSL (and OpenSSL-based applications) load shared libraries and bind to them in a portable way. [Geoff Thorpe, with contributions from Richard Levitte] Changes between 0.9.5 and 0.9.5a [1 Apr 2000] *) Make sure _lrotl and _lrotr are only used with MSVC. *) Use lock CRYPTO_LOCK_RAND correctly in ssleay_rand_status (the default implementation of RAND_status). *) Rename openssl x509 option '-crlext', which was added in 0.9.5, to '-clrext' (= clear extensions), as intended and documented. [Bodo Moeller; inconsistency pointed out by Michael Attili ] *) Fix for HMAC. It wasn't zeroing the rest of the block if the key length was larger than the MD block size. [Steve Henson, pointed out by Yost William ] *) Modernise PKCS12_parse() so it uses STACK_OF(X509) for its ca argument fix a leak when the ca argument was passed as NULL. Stop X509_PUBKEY_set() using the passed key: if the passed key was a private key the result of X509_print(), for example, would be to print out all the private key components. [Steve Henson] *) des_quad_cksum() byte order bug fix. [Ulf Möller, using the problem description in krb4-0.9.7, where the solution is attributed to Derrick J Brashear ] *) Fix so V_ASN1_APP_CHOOSE works again: however its use is strongly discouraged. [Steve Henson, pointed out by Brian Korver ] *) For easily testing in shell scripts whether some command 'openssl XXX' exists, the new pseudo-command 'openssl no-XXX' returns with exit code 0 iff no command of the given name is available. 'no-XXX' is printed in this case, 'XXX' otherwise. In both cases, the output goes to stdout and nothing is printed to stderr. Additional arguments are always ignored. Since for each cipher there is a command of the same name, the 'no-cipher' compilation switches can be tested this way. ('openssl no-XXX' is not able to detect pseudo-commands such as 'quit', 'list-XXX-commands', or 'no-XXX' itself.) [Bodo Moeller] *) Update test suite so that 'make test' succeeds in 'no-rsa' configuration. [Bodo Moeller] *) For SSL_[CTX_]set_tmp_dh, don't create a DH key if SSL_OP_SINGLE_DH_USE is set; it will be thrown away anyway because each handshake creates its own key. ssl_cert_dup, which is used by SSL_new, now copies DH keys in addition to parameters -- in previous versions (since OpenSSL 0.9.3) the 'default key' from SSL_CTX_set_tmp_dh would always be lost, meanining you effectivly got SSL_OP_SINGLE_DH_USE when using this macro. [Bodo Moeller] *) New s_client option -ign_eof: EOF at stdin is ignored, and 'Q' and 'R' lose their special meanings (quit/renegotiate). This is part of what -quiet does; unlike -quiet, -ign_eof does not suppress any output. [Richard Levitte] *) Add compatibility options to the purpose and trust code. The purpose X509_PURPOSE_ANY is "any purpose" which automatically accepts a certificate or CA, this was the previous behaviour, with all the associated security issues. X509_TRUST_COMPAT is the old trust behaviour: only and automatically trust self signed roots in certificate store. A new trust setting X509_TRUST_DEFAULT is used to specify that a purpose has no associated trust setting and it should instead use the value in the default purpose. [Steve Henson] *) Fix the PKCS#8 DSA private key code so it decodes keys again and fix a memory leak. [Steve Henson] *) In util/mkerr.pl (which implements 'make errors'), preserve reason strings from the previous version of the .c file, as the default to have only downcase letters (and digits) in automatically generated reasons codes is not always appropriate. [Bodo Moeller] *) In ERR_load_ERR_strings(), build an ERR_LIB_SYS error reason table using strerror. Previously, ERR_reason_error_string() returned library names as reason strings for SYSerr; but SYSerr is a special case where small numbers are errno values, not library numbers. [Bodo Moeller] *) Add '-dsaparam' option to 'openssl dhparam' application. This converts DSA parameters into DH parameters. (When creating parameters, DSA_generate_parameters is used.) [Bodo Moeller] *) Include 'length' (recommended exponent length) in C code generated by 'openssl dhparam -C'. [Bodo Moeller] *) The second argument to set_label in perlasm was already being used so couldn't be used as a "file scope" flag. Moved to third argument which was free. [Steve Henson] *) In PEM_ASN1_write_bio and some other functions, use RAND_pseudo_bytes instead of RAND_bytes for encryption IVs and salts. [Bodo Moeller] *) Include RAND_status() into RAND_METHOD instead of implementing it only for md_rand.c Otherwise replacing the PRNG by calling RAND_set_rand_method would be impossible. [Bodo Moeller] *) Don't let DSA_generate_key() enter an infinite loop if the random number generation fails. [Bodo Moeller] *) New 'rand' application for creating pseudo-random output. [Bodo Moeller] *) Added configuration support for Linux/IA64 [Rolf Haberrecker ] *) Assembler module support for Mingw32. [Ulf Möller] *) Shared library support for HPUX (in shlib/). [Lutz Jaenicke and Anonymous] *) Shared library support for Solaris gcc. [Lutz Behnke ] Changes between 0.9.4 and 0.9.5 [28 Feb 2000] *) PKCS7_encrypt() was adding text MIME headers twice because they were added manually and by SMIME_crlf_copy(). [Steve Henson] *) In bntest.c don't call BN_rand with zero bits argument. [Steve Henson, pointed out by Andrew W. Gray ] *) BN_mul bugfix: In bn_mul_part_recursion() only the a>a[n] && b>b[n] case was implemented. This caused BN_div_recp() to fail occasionally. [Ulf Möller] *) Add an optional second argument to the set_label() in the perl assembly language builder. If this argument exists and is set to 1 it signals that the assembler should use a symbol whose scope is the entire file, not just the current function. This is needed with MASM which uses the format label:: for this scope. [Steve Henson, pointed out by Peter Runestig ] *) Change the ASN1 types so they are typedefs by default. Before almost all types were #define'd to ASN1_STRING which was causing STACK_OF() problems: you couldn't declare STACK_OF(ASN1_UTF8STRING) for example. [Steve Henson] *) Change names of new functions to the new get1/get0 naming convention: After 'get1', the caller owns a reference count and has to call ..._free; 'get0' returns a pointer to some data structure without incrementing reference counters. (Some of the existing 'get' functions increment a reference counter, some don't.) Similarly, 'set1' and 'add1' functions increase reference counters or duplicate objects. [Steve Henson] *) Allow for the possibility of temp RSA key generation failure: the code used to assume it always worked and crashed on failure. [Steve Henson] *) Fix potential buffer overrun problem in BIO_printf(). [Ulf Möller, using public domain code by Patrick Powell; problem pointed out by David Sacerdote ] *) Support EGD . New functions RAND_egd() and RAND_status(). In the command line application, the EGD socket can be specified like a seed file using RANDFILE or -rand. [Ulf Möller] *) Allow the string CERTIFICATE to be tolerated in PKCS#7 structures. Some CAs (e.g. Verisign) distribute certificates in this form. [Steve Henson] *) Remove the SSL_ALLOW_ADH compile option and set the default cipher list to exclude them. This means that no special compilation option is needed to use anonymous DH: it just needs to be included in the cipher list. [Steve Henson] *) Change the EVP_MD_CTX_type macro so its meaning consistent with EVP_MD_type. The old functionality is available in a new macro called EVP_MD_md(). Change code that uses it and update docs. [Steve Henson] *) ..._ctrl functions now have corresponding ..._callback_ctrl functions where the 'void *' argument is replaced by a function pointer argument. Previously 'void *' was abused to point to functions, which works on many platforms, but is not correct. As these functions are usually called by macros defined in OpenSSL header files, most source code should work without changes. [Richard Levitte] *) (which is created by Configure) now contains sections with information on -D... compiler switches used for compiling the library so that applications can see them. To enable one of these sections, a pre-processor symbol OPENSSL_..._DEFINES must be defined. E.g., #define OPENSSL_ALGORITHM_DEFINES #include defines all pertinent NO_ symbols, such as NO_IDEA, NO_RSA, etc. [Richard Levitte, Ulf and Bodo Möller] *) Bugfix: Tolerate fragmentation and interleaving in the SSL 3/TLS record layer. [Bodo Moeller] *) Change the 'other' type in certificate aux info to a STACK_OF X509_ALGOR. Although not an AlgorithmIdentifier as such it has the required ASN1 format: arbitrary types determined by an OID. [Steve Henson] *) Add some PEM_write_X509_REQ_NEW() functions and a command line argument to 'req'. This is not because the function is newer or better than others it just uses the work 'NEW' in the certificate request header lines. Some software needs this. [Steve Henson] *) Reorganise password command line arguments: now passwords can be obtained from various sources. Delete the PEM_cb function and make it the default behaviour: i.e. if the callback is NULL and the usrdata argument is not NULL interpret it as a null terminated pass phrase. If usrdata and the callback are NULL then the pass phrase is prompted for as usual. [Steve Henson] *) Add support for the Compaq Atalla crypto accelerator. If it is installed, the support is automatically enabled. The resulting binaries will autodetect the card and use it if present. [Ben Laurie and Compaq Inc.] *) Work around for Netscape hang bug. This sends certificate request and server done in one record. Since this is perfectly legal in the SSL/TLS protocol it isn't a "bug" option and is on by default. See the bugs/SSLv3 entry for more info. [Steve Henson] *) HP-UX tune-up: new unified configs, HP C compiler bug workaround. [Andy Polyakov] *) Add -rand argument to smime and pkcs12 applications and read/write of seed file. [Steve Henson] *) New 'passwd' tool for crypt(3) and apr1 password hashes. [Bodo Moeller] *) Add command line password options to the remaining applications. [Steve Henson] *) Bug fix for BN_div_recp() for numerators with an even number of bits. [Ulf Möller] *) More tests in bntest.c, and changed test_bn output. [Ulf Möller] *) ./config recognizes MacOS X now. [Andy Polyakov] *) Bug fix for BN_div() when the first words of num and divsor are equal (it gave wrong results if (rem=(n1-q*d0)&BN_MASK2) < d0). [Ulf Möller] *) Add support for various broken PKCS#8 formats, and command line options to produce them. [Steve Henson] *) New functions BN_CTX_start(), BN_CTX_get() and BT_CTX_end() to get temporary BIGNUMs from a BN_CTX. [Ulf Möller] *) Correct return values in BN_mod_exp_mont() and BN_mod_exp2_mont() for p == 0. [Ulf Möller] *) Change the SSLeay_add_all_*() functions to OpenSSL_add_all_*() and include a #define from the old name to the new. The original intent was that statically linked binaries could for example just call SSLeay_add_all_ciphers() to just add ciphers to the table and not link with digests. This never worked becayse SSLeay_add_all_digests() and SSLeay_add_all_ciphers() were in the same source file so calling one would link with the other. They are now in separate source files. [Steve Henson] *) Add a new -notext option to 'ca' and a -pubkey option to 'spkac'. [Steve Henson] *) Use a less unusual form of the Miller-Rabin primality test (it used a binary algorithm for exponentiation integrated into the Miller-Rabin loop, our standard modexp algorithms are faster). [Bodo Moeller] *) Support for the EBCDIC character set completed. [Martin Kraemer ] *) Source code cleanups: use const where appropriate, eliminate casts, use void * instead of char * in lhash. [Ulf Möller] *) Bugfix: ssl3_send_server_key_exchange was not restartable (the state was not changed to SSL3_ST_SW_KEY_EXCH_B, and because of this the server could overwrite ephemeral keys that the client has already seen). [Bodo Moeller] *) Turn DSA_is_prime into a macro that calls BN_is_prime, using 50 iterations of the Rabin-Miller test. DSA_generate_parameters now uses BN_is_prime_fasttest (with 50 iterations of the Rabin-Miller test as required by the appendix to FIPS PUB 186[-1]) instead of DSA_is_prime. As BN_is_prime_fasttest includes trial division, DSA parameter generation becomes much faster. This implies a change for the callback functions in DSA_is_prime and DSA_generate_parameters: The callback function is called once for each positive witness in the Rabin-Miller test, not just occasionally in the inner loop; and the parameters to the callback function now provide an iteration count for the outer loop rather than for the current invocation of the inner loop. DSA_generate_parameters additionally can call the callback function with an 'iteration count' of -1, meaning that a candidate has passed the trial division test (when q is generated from an application-provided seed, trial division is skipped). [Bodo Moeller] *) New function BN_is_prime_fasttest that optionally does trial division before starting the Rabin-Miller test and has an additional BN_CTX * argument (whereas BN_is_prime always has to allocate at least one BN_CTX). 'callback(1, -1, cb_arg)' is called when a number has passed the trial division stage. [Bodo Moeller] *) Fix for bug in CRL encoding. The validity dates weren't being handled as ASN1_TIME. [Steve Henson] *) New -pkcs12 option to CA.pl script to write out a PKCS#12 file. [Steve Henson] *) New function BN_pseudo_rand(). [Ulf Möller] *) Clean up BN_mod_mul_montgomery(): replace the broken (and unreadable) bignum version of BN_from_montgomery() with the working code from SSLeay 0.9.0 (the word based version is faster anyway), and clean up the comments. [Ulf Möller] *) Avoid a race condition in s2_clnt.c (function get_server_hello) that made it impossible to use the same SSL_SESSION data structure in SSL2 clients in multiple threads. [Bodo Moeller] *) The return value of RAND_load_file() no longer counts bytes obtained by stat(). RAND_load_file(..., -1) is new and uses the complete file to seed the PRNG (previously an explicit byte count was required). [Ulf Möller, Bodo Möller] *) Clean up CRYPTO_EX_DATA functions, some of these didn't have prototypes used (char *) instead of (void *) and had casts all over the place. [Steve Henson] *) Make BN_generate_prime() return NULL on error if ret!=NULL. [Ulf Möller] *) Retain source code compatibility for BN_prime_checks macro: BN_is_prime(..., BN_prime_checks, ...) now uses BN_prime_checks_for_size to determine the appropriate number of Rabin-Miller iterations. [Ulf Möller] *) Diffie-Hellman uses "safe" primes: DH_check() return code renamed to DH_CHECK_P_NOT_SAFE_PRIME. (Check if this is true? OpenPGP calls them "strong".) [Ulf Möller] *) Merge the functionality of "dh" and "gendh" programs into a new program "dhparam". The old programs are retained for now but will handle DH keys (instead of parameters) in future. [Steve Henson] *) Make the ciphers, s_server and s_client programs check the return values when a new cipher list is set. [Steve Henson] *) Enhance the SSL/TLS cipher mechanism to correctly handle the TLS 56bit ciphers. Before when the 56bit ciphers were enabled the sorting was wrong. The syntax for the cipher sorting has been extended to support sorting by cipher-strength (using the strength_bits hard coded in the tables). The new command is "@STRENGTH" (see also doc/apps/ciphers.pod). Fix a bug in the cipher-command parser: when supplying a cipher command string with an "undefined" symbol (neither command nor alphanumeric [A-Za-z0-9], ssl_set_cipher_list used to hang in an endless loop. Now an error is flagged. Due to the strength-sorting extension, the code of the ssl_create_cipher_list() function was completely rearranged. I hope that the readability was also increased :-) [Lutz Jaenicke ] *) Minor change to 'x509' utility. The -CAcreateserial option now uses 1 for the first serial number and places 2 in the serial number file. This avoids problems when the root CA is created with serial number zero and the first user certificate has the same issuer name and serial number as the root CA. [Steve Henson] *) Fixes to X509_ATTRIBUTE utilities, change the 'req' program so it uses the new code. Add documentation for this stuff. [Steve Henson] *) Changes to X509_ATTRIBUTE utilities. These have been renamed from X509_*() to X509at_*() on the grounds that they don't handle X509 structures and behave in an analagous way to the X509v3 functions: they shouldn't be called directly but wrapper functions should be used instead. So we also now have some wrapper functions that call the X509at functions when passed certificate requests. (TO DO: similar things can be done with PKCS#7 signed and unsigned attributes, PKCS#12 attributes and a few other things. Some of these need some d2i or i2d and print functionality because they handle more complex structures.) [Steve Henson] *) Add missing #ifndefs that caused missing symbols when building libssl as a shared library without RSA. Use #ifndef NO_SSL2 instead of NO_RSA in ssl/s2*.c. [Kris Kennaway , modified by Ulf Möller] *) Precautions against using the PRNG uninitialized: RAND_bytes() now has a return value which indicates the quality of the random data (1 = ok, 0 = not seeded). Also an error is recorded on the thread's error queue. New function RAND_pseudo_bytes() generates output that is guaranteed to be unique but not unpredictable. RAND_add is like RAND_seed, but takes an extra argument for an entropy estimate (RAND_seed always assumes full entropy). [Ulf Möller] *) Do more iterations of Rabin-Miller probable prime test (specifically, 3 for 1024-bit primes, 6 for 512-bit primes, 12 for 256-bit primes instead of only 2 for all lengths; see BN_prime_checks_for_size definition in crypto/bn/bn_prime.c for the complete table). This guarantees a false-positive rate of at most 2^-80 for random input. [Bodo Moeller] *) Rewrite ssl3_read_n (ssl/s3_pkt.c) avoiding a couple of bugs. [Bodo Moeller] *) New function X509_CTX_rget_chain() (renamed to X509_CTX_get1_chain in the 0.9.5 release), this returns the chain from an X509_CTX structure with a dup of the stack and all the X509 reference counts upped: so the stack will exist after X509_CTX_cleanup() has been called. Modify pkcs12.c to use this. Also make SSL_SESSION_print() print out the verify return code. [Steve Henson] *) Add manpage for the pkcs12 command. Also change the default behaviour so MAC iteration counts are used unless the new -nomaciter option is used. This improves file security and only older versions of MSIE (4.0 for example) need it. [Steve Henson] *) Honor the no-xxx Configure options when creating .DEF files. [Ulf Möller] *) Add PKCS#10 attributes to field table: challengePassword, unstructuredName and unstructuredAddress. These are taken from draft PKCS#9 v2.0 but are compatible with v1.2 provided no international characters are used. More changes to X509_ATTRIBUTE code: allow the setting of types based on strings. Remove the 'loc' parameter when adding attributes because these will be a SET OF encoding which is sorted in ASN1 order. [Steve Henson] *) Initial changes to the 'req' utility to allow request generation automation. This will allow an application to just generate a template file containing all the field values and have req construct the request. Initial support for X509_ATTRIBUTE handling. Stacks of these are used all over the place including certificate requests and PKCS#7 structures. They are currently handled manually where necessary with some primitive wrappers for PKCS#7. The new functions behave in a manner analogous to the X509 extension functions: they allow attributes to be looked up by NID and added. Later something similar to the X509V3 code would be desirable to automatically handle the encoding, decoding and printing of the more complex types. The string types like challengePassword can be handled by the string table functions. Also modified the multi byte string table handling. Now there is a 'global mask' which masks out certain types. The table itself can use the flag STABLE_NO_MASK to ignore the mask setting: this is useful when for example there is only one permissible type (as in countryName) and using the mask might result in no valid types at all. [Steve Henson] *) Clean up 'Finished' handling, and add functions SSL_get_finished and SSL_get_peer_finished to allow applications to obtain the latest Finished messages sent to the peer or expected from the peer, respectively. (SSL_get_peer_finished is usually the Finished message actually received from the peer, otherwise the protocol will be aborted.) As the Finished message are message digests of the complete handshake (with a total of 192 bits for TLS 1.0 and more for SSL 3.0), they can be used for external authentication procedures when the authentication provided by SSL/TLS is not desired or is not enough. [Bodo Moeller] *) Enhanced support for Alpha Linux is added. Now ./config checks if the host supports BWX extension and if Compaq C is present on the $PATH. Just exploiting of the BWX extension results in 20-30% performance kick for some algorithms, e.g. DES and RC4 to mention a couple. Compaq C in turn generates ~20% faster code for MD5 and SHA1. [Andy Polyakov] *) Add support for MS "fast SGC". This is arguably a violation of the SSL3/TLS protocol. Netscape SGC does two handshakes: the first with weak crypto and after checking the certificate is SGC a second one with strong crypto. MS SGC stops the first handshake after receiving the server certificate message and sends a second client hello. Since a server will typically do all the time consuming operations before expecting any further messages from the client (server key exchange is the most expensive) there is little difference between the two. To get OpenSSL to support MS SGC we have to permit a second client hello message after we have sent server done. In addition we have to reset the MAC if we do get this second client hello. [Steve Henson] *) Add a function 'd2i_AutoPrivateKey()' this will automatically decide if a DER encoded private key is RSA or DSA traditional format. Changed d2i_PrivateKey_bio() to use it. This is only needed for the "traditional" format DER encoded private key. Newer code should use PKCS#8 format which has the key type encoded in the ASN1 structure. Added DER private key support to pkcs8 application. [Steve Henson] *) SSL 3/TLS 1 servers now don't request certificates when an anonymous ciphersuites has been selected (as required by the SSL 3/TLS 1 specifications). Exception: When SSL_VERIFY_FAIL_IF_NO_PEER_CERT is set, we interpret this as a request to violate the specification (the worst that can happen is a handshake failure, and 'correct' behaviour would result in a handshake failure anyway). [Bodo Moeller] *) In SSL_CTX_add_session, take into account that there might be multiple SSL_SESSION structures with the same session ID (e.g. when two threads concurrently obtain them from an external cache). The internal cache can handle only one SSL_SESSION with a given ID, so if there's a conflict, we now throw out the old one to achieve consistency. [Bodo Moeller] *) Add OIDs for idea and blowfish in CBC mode. This will allow both to be used in PKCS#5 v2.0 and S/MIME. Also add checking to some routines that use cipher OIDs: some ciphers do not have OIDs defined and so they cannot be used for S/MIME and PKCS#5 v2.0 for example. [Steve Henson] *) Simplify the trust setting structure and code. Now we just have two sequences of OIDs for trusted and rejected settings. These will typically have values the same as the extended key usage extension and any application specific purposes. The trust checking code now has a default behaviour: it will just check for an object with the same NID as the passed id. Functions can be provided to override either the default behaviour or the behaviour for a given id. SSL client, server and email already have functions in place for compatibility: they check the NID and also return "trusted" if the certificate is self signed. [Steve Henson] *) Add d2i,i2d bio/fp functions for PrivateKey: these convert the traditional format into an EVP_PKEY structure. [Steve Henson] *) Add a password callback function PEM_cb() which either prompts for a password if usr_data is NULL or otherwise assumes it is a null terminated password. Allow passwords to be passed on command line environment or config files in a few more utilities. [Steve Henson] *) Add a bunch of DER and PEM functions to handle PKCS#8 format private keys. Add some short names for PKCS#8 PBE algorithms and allow them to be specified on the command line for the pkcs8 and pkcs12 utilities. Update documentation. [Steve Henson] *) Support for ASN1 "NULL" type. This could be handled before by using ASN1_TYPE but there wasn't any function that would try to read a NULL and produce an error if it couldn't. For compatibility we also have ASN1_NULL_new() and ASN1_NULL_free() functions but these are faked and don't allocate anything because they don't need to. [Steve Henson] *) Initial support for MacOS is now provided. Examine INSTALL.MacOS for details. [Andy Polyakov, Roy Woods ] *) Rebuild of the memory allocation routines used by OpenSSL code and possibly others as well. The purpose is to make an interface that provide hooks so anyone can build a separate set of allocation and deallocation routines to be used by OpenSSL, for example memory pool implementations, or something else, which was previously hard since Malloc(), Realloc() and Free() were defined as macros having the values malloc, realloc and free, respectively (except for Win32 compilations). The same is provided for memory debugging code. OpenSSL already comes with functionality to find memory leaks, but this gives people a chance to debug other memory problems. With these changes, a new set of functions and macros have appeared: CRYPTO_set_mem_debug_functions() [F] CRYPTO_get_mem_debug_functions() [F] CRYPTO_dbg_set_options() [F] CRYPTO_dbg_get_options() [F] CRYPTO_malloc_debug_init() [M] The memory debug functions are NULL by default, unless the library is compiled with CRYPTO_MDEBUG or friends is defined. If someone wants to debug memory anyway, CRYPTO_malloc_debug_init() (which gives the standard debugging functions that come with OpenSSL) or CRYPTO_set_mem_debug_functions() (tells OpenSSL to use functions provided by the library user) must be used. When the standard debugging functions are used, CRYPTO_dbg_set_options can be used to request additional information: CRYPTO_dbg_set_options(V_CYRPTO_MDEBUG_xxx) corresponds to setting the CRYPTO_MDEBUG_xxx macro when compiling the library. Also, things like CRYPTO_set_mem_functions will always give the expected result (the new set of functions is used for allocation and deallocation) at all times, regardless of platform and compiler options. To finish it up, some functions that were never use in any other way than through macros have a new API and new semantic: CRYPTO_dbg_malloc() CRYPTO_dbg_realloc() CRYPTO_dbg_free() All macros of value have retained their old syntax. [Richard Levitte and Bodo Moeller] *) Some S/MIME fixes. The OID for SMIMECapabilities was wrong, the ordering of SMIMECapabilities wasn't in "strength order" and there was a missing NULL in the AlgorithmIdentifier for the SHA1 signature algorithm. [Steve Henson] *) Some ASN1 types with illegal zero length encoding (INTEGER, ENUMERATED and OBJECT IDENTIFIER) choked the ASN1 routines. [Frans Heymans , modified by Steve Henson] *) Merge in my S/MIME library for OpenSSL. This provides a simple S/MIME API on top of the PKCS#7 code, a MIME parser (with enough functionality to handle multipart/signed properly) and a utility called 'smime' to call all this stuff. This is based on code I originally wrote for Celo who have kindly allowed it to be included in OpenSSL. [Steve Henson] *) Add variants des_set_key_checked and des_set_key_unchecked of des_set_key (aka des_key_sched). Global variable des_check_key decides which of these is called by des_set_key; this way des_check_key behaves as it always did, but applications and the library itself, which was buggy for des_check_key == 1, have a cleaner way to pick the version they need. [Bodo Moeller] *) New function PKCS12_newpass() which changes the password of a PKCS12 structure. [Steve Henson] *) Modify X509_TRUST and X509_PURPOSE so it also uses a static and dynamic mix. In both cases the ids can be used as an index into the table. Also modified the X509_TRUST_add() and X509_PURPOSE_add() functions so they accept a list of the field values and the application doesn't need to directly manipulate the X509_TRUST structure. [Steve Henson] *) Modify the ASN1_STRING_TABLE stuff so it also uses bsearch and doesn't need initialising. [Steve Henson] *) Modify the way the V3 extension code looks up extensions. This now works in a similar way to the object code: we have some "standard" extensions in a static table which is searched with OBJ_bsearch() and the application can add dynamic ones if needed. The file crypto/x509v3/ext_dat.h now has the info: this file needs to be updated whenever a new extension is added to the core code and kept in ext_nid order. There is a simple program 'tabtest.c' which checks this. New extensions are not added too often so this file can readily be maintained manually. There are two big advantages in doing things this way. The extensions can be looked up immediately and no longer need to be "added" using X509V3_add_standard_extensions(): this function now does nothing. [Side note: I get *lots* of email saying the extension code doesn't work because people forget to call this function] Also no dynamic allocation is done unless new extensions are added: so if we don't add custom extensions there is no need to call X509V3_EXT_cleanup(). [Steve Henson] *) Modify enc utility's salting as follows: make salting the default. Add a magic header, so unsalted files fail gracefully instead of just decrypting to garbage. This is because not salting is a big security hole, so people should be discouraged from doing it. [Ben Laurie] *) Fixes and enhancements to the 'x509' utility. It allowed a message digest to be passed on the command line but it only used this parameter when signing a certificate. Modified so all relevant operations are affected by the digest parameter including the -fingerprint and -x509toreq options. Also -x509toreq choked if a DSA key was used because it didn't fix the digest. [Steve Henson] *) Initial certificate chain verify code. Currently tests the untrusted certificates for consistency with the verify purpose (which is set when the X509_STORE_CTX structure is set up) and checks the pathlength. There is a NO_CHAIN_VERIFY compilation option to keep the old behaviour: this is because it will reject chains with invalid extensions whereas every previous version of OpenSSL and SSLeay made no checks at all. Trust code: checks the root CA for the relevant trust settings. Trust settings have an initial value consistent with the verify purpose: e.g. if the verify purpose is for SSL client use it expects the CA to be trusted for SSL client use. However the default value can be changed to permit custom trust settings: one example of this would be to only trust certificates from a specific "secure" set of CAs. Also added X509_STORE_CTX_new() and X509_STORE_CTX_free() functions which should be used for version portability: especially since the verify structure is likely to change more often now. SSL integration. Add purpose and trust to SSL_CTX and SSL and functions to set them. If not set then assume SSL clients will verify SSL servers and vice versa. Two new options to the verify program: -untrusted allows a set of untrusted certificates to be passed in and -purpose which sets the intended purpose of the certificate. If a purpose is set then the new chain verify code is used to check extension consistency. [Steve Henson] *) Support for the authority information access extension. [Steve Henson] *) Modify RSA and DSA PEM read routines to transparently handle PKCS#8 format private keys. New *_PUBKEY_* functions that handle public keys in a format compatible with certificate SubjectPublicKeyInfo structures. Unfortunately there were already functions called *_PublicKey_* which used various odd formats so these are retained for compatibility: however the DSA variants were never in a public release so they have been deleted. Changed dsa/rsa utilities to handle the new format: note no releases ever handled public keys so we should be OK. The primary motivation for this change is to avoid the same fiasco that dogs private keys: there are several incompatible private key formats some of which are standard and some OpenSSL specific and require various evil hacks to allow partial transparent handling and even then it doesn't work with DER formats. Given the option anything other than PKCS#8 should be dumped: but the other formats have to stay in the name of compatibility. With public keys and the benefit of hindsight one standard format is used which works with EVP_PKEY, RSA or DSA structures: though it clearly returns an error if you try to read the wrong kind of key. Added a -pubkey option to the 'x509' utility to output the public key. Also rename the EVP_PKEY_get_*() to EVP_PKEY_rget_*() (renamed to EVP_PKEY_get1_*() in the OpenSSL 0.9.5 release) and add EVP_PKEY_rset_*() functions (renamed to EVP_PKEY_set1_*()) that do the same as the EVP_PKEY_assign_*() except they up the reference count of the added key (they don't "swallow" the supplied key). [Steve Henson] *) Fixes to crypto/x509/by_file.c the code to read in certificates and CRLs would fail if the file contained no certificates or no CRLs: added a new function to read in both types and return the number read: this means that if none are read it will be an error. The DER versions of the certificate and CRL reader would always fail because it isn't possible to mix certificates and CRLs in DER format without choking one or the other routine. Changed this to just read a certificate: this is the best we can do. Also modified the code in apps/verify.c to take notice of return codes: it was previously attempting to read in certificates from NULL pointers and ignoring any errors: this is one reason why the cert and CRL reader seemed to work. It doesn't check return codes from the default certificate routines: these may well fail if the certificates aren't installed. [Steve Henson] *) Code to support otherName option in GeneralName. [Steve Henson] *) First update to verify code. Change the verify utility so it warns if it is passed a self signed certificate: for consistency with the normal behaviour. X509_verify has been modified to it will now verify a self signed certificate if *exactly* the same certificate appears in the store: it was previously impossible to trust a single self signed certificate. This means that: openssl verify ss.pem now gives a warning about a self signed certificate but openssl verify -CAfile ss.pem ss.pem is OK. [Steve Henson] *) For servers, store verify_result in SSL_SESSION data structure (and add it to external session representation). This is needed when client certificate verifications fails, but an application-provided verification callback (set by SSL_CTX_set_cert_verify_callback) allows accepting the session anyway (i.e. leaves x509_store_ctx->error != X509_V_OK but returns 1): When the session is reused, we have to set ssl->verify_result to the appropriate error code to avoid security holes. [Bodo Moeller, problem pointed out by Lutz Jaenicke] *) Fix a bug in the new PKCS#7 code: it didn't consider the case in PKCS7_dataInit() where the signed PKCS7 structure didn't contain any existing data because it was being created. [Po-Cheng Chen , slightly modified by Steve Henson] *) Add a salt to the key derivation routines in enc.c. This forms the first 8 bytes of the encrypted file. Also add a -S option to allow a salt to be input on the command line. [Steve Henson] *) New function X509_cmp(). Oddly enough there wasn't a function to compare two certificates. We do this by working out the SHA1 hash and comparing that. X509_cmp() will be needed by the trust code. [Steve Henson] *) SSL_get1_session() is like SSL_get_session(), but increments the reference count in the SSL_SESSION returned. [Geoff Thorpe ] *) Fix for 'req': it was adding a null to request attributes. Also change the X509_LOOKUP and X509_INFO code to handle certificate auxiliary information. [Steve Henson] *) Add support for 40 and 64 bit RC2 and RC4 algorithms: document the 'enc' command. [Steve Henson] *) Add the possibility to add extra information to the memory leak detecting output, to form tracebacks, showing from where each allocation was originated: CRYPTO_push_info("constant string") adds the string plus current file name and line number to a per-thread stack, CRYPTO_pop_info() does the obvious, CRYPTO_remove_all_info() is like calling CYRPTO_pop_info() until the stack is empty. Also updated memory leak detection code to be multi-thread-safe. [Richard Levitte] *) Add options -text and -noout to pkcs7 utility and delete the encryption options which never did anything. Update docs. [Steve Henson] *) Add options to some of the utilities to allow the pass phrase to be included on either the command line (not recommended on OSes like Unix) or read from the environment. Update the manpages and fix a few bugs. [Steve Henson] *) Add a few manpages for some of the openssl commands. [Steve Henson] *) Fix the -revoke option in ca. It was freeing up memory twice, leaking and not finding already revoked certificates. [Steve Henson] *) Extensive changes to support certificate auxiliary information. This involves the use of X509_CERT_AUX structure and X509_AUX functions. An X509_AUX function such as PEM_read_X509_AUX() can still read in a certificate file in the usual way but it will also read in any additional "auxiliary information". By doing things this way a fair degree of compatibility can be retained: existing certificates can have this information added using the new 'x509' options. Current auxiliary information includes an "alias" and some trust settings. The trust settings will ultimately be used in enhanced certificate chain verification routines: currently a certificate can only be trusted if it is self signed and then it is trusted for all purposes. [Steve Henson] *) Fix assembler for Alpha (tested only on DEC OSF not Linux or *BSD). The problem was that one of the replacement routines had not been working since SSLeay releases. For now the offending routine has been replaced with non-optimised assembler. Even so, this now gives around 95% performance improvement for 1024 bit RSA signs. [Mark Cox] *) Hack to fix PKCS#7 decryption when used with some unorthodox RC2 handling. Most clients have the effective key size in bits equal to the key length in bits: so a 40 bit RC2 key uses a 40 bit (5 byte) key. A few however don't do this and instead use the size of the decrypted key to determine the RC2 key length and the AlgorithmIdentifier to determine the effective key length. In this case the effective key length can still be 40 bits but the key length can be 168 bits for example. This is fixed by manually forcing an RC2 key into the EVP_PKEY structure because the EVP code can't currently handle unusual RC2 key sizes: it always assumes the key length and effective key length are equal. [Steve Henson] *) Add a bunch of functions that should simplify the creation of X509_NAME structures. Now you should be able to do: X509_NAME_add_entry_by_txt(nm, "CN", MBSTRING_ASC, "Steve", -1, -1, 0); and have it automatically work out the correct field type and fill in the structures. The more adventurous can try: X509_NAME_add_entry_by_txt(nm, field, MBSTRING_UTF8, str, -1, -1, 0); and it will (hopefully) work out the correct multibyte encoding. [Steve Henson] *) Change the 'req' utility to use the new field handling and multibyte copy routines. Before the DN field creation was handled in an ad hoc way in req, ca, and x509 which was rather broken and didn't support BMPStrings or UTF8Strings. Since some software doesn't implement BMPStrings or UTF8Strings yet, they can be enabled using the config file using the dirstring_type option. See the new comment in the default openssl.cnf for more info. [Steve Henson] *) Make crypto/rand/md_rand.c more robust: - Assure unique random numbers after fork(). - Make sure that concurrent threads access the global counter and md serializably so that we never lose entropy in them or use exactly the same state in multiple threads. Access to the large state is not always serializable because the additional locking could be a performance killer, and md should be large enough anyway. [Bodo Moeller] *) New file apps/app_rand.c with commonly needed functionality for handling the random seed file. Use the random seed file in some applications that previously did not: ca, dsaparam -genkey (which also ignored its '-rand' option), s_client, s_server, x509 (when signing). Except on systems with /dev/urandom, it is crucial to have a random seed file at least for key creation, DSA signing, and for DH exchanges; for RSA signatures we could do without one. gendh and gendsa (unlike genrsa) used to read only the first byte of each file listed in the '-rand' option. The function as previously found in genrsa is now in app_rand.c and is used by all programs that support '-rand'. [Bodo Moeller] *) In RAND_write_file, use mode 0600 for creating files; don't just chmod when it may be too late. [Bodo Moeller] *) Report an error from X509_STORE_load_locations when X509_LOOKUP_load_file or X509_LOOKUP_add_dir failed. [Bill Perry] *) New function ASN1_mbstring_copy() this copies a string in either ASCII, Unicode, Universal (4 bytes per character) or UTF8 format into an ASN1_STRING type. A mask of permissible types is passed and it chooses the "minimal" type to use or an error if not type is suitable. [Steve Henson] *) Add function equivalents to the various macros in asn1.h. The old macros are retained with an M_ prefix. Code inside the library can use the M_ macros. External code (including the openssl utility) should *NOT* in order to be "shared library friendly". [Steve Henson] *) Add various functions that can check a certificate's extensions to see if it usable for various purposes such as SSL client, server or S/MIME and CAs of these types. This is currently VERY EXPERIMENTAL but will ultimately be used for certificate chain verification. Also added a -purpose flag to x509 utility to print out all the purposes. [Steve Henson] *) Add a CRYPTO_EX_DATA to X509 certificate structure and associated functions. [Steve Henson] *) New X509V3_{X509,CRL,REVOKED}_get_d2i() functions. These will search for, obtain and decode and extension and obtain its critical flag. This allows all the necessary extension code to be handled in a single function call. [Steve Henson] *) RC4 tune-up featuring 30-40% performance improvement on most RISC platforms. See crypto/rc4/rc4_enc.c for further details. [Andy Polyakov] *) New -noout option to asn1parse. This causes no output to be produced its main use is when combined with -strparse and -out to extract data from a file (which may not be in ASN.1 format). [Steve Henson] *) Fix for pkcs12 program. It was hashing an invalid certificate pointer when producing the local key id. [Richard Levitte ] *) New option -dhparam in s_server. This allows a DH parameter file to be stated explicitly. If it is not stated then it tries the first server certificate file. The previous behaviour hard coded the filename "server.pem". [Steve Henson] *) Add -pubin and -pubout options to the rsa and dsa commands. These allow a public key to be input or output. For example: openssl rsa -in key.pem -pubout -out pubkey.pem Also added necessary DSA public key functions to handle this. [Steve Henson] *) Fix so PKCS7_dataVerify() doesn't crash if no certificates are contained in the message. This was handled by allowing X509_find_by_issuer_and_serial() to tolerate a NULL passed to it. [Steve Henson, reported by Sampo Kellomaki ] *) Fix for bug in d2i_ASN1_bytes(): other ASN1 functions add an extra null to the end of the strings whereas this didn't. This would cause problems if strings read with d2i_ASN1_bytes() were later modified. [Steve Henson, reported by Arne Ansper ] *) Fix for base64 decode bug. When a base64 bio reads only one line of data and it contains EOF it will end up returning an error. This is caused by input 46 bytes long. The cause is due to the way base64 BIOs find the start of base64 encoded data. They do this by trying a trial decode on each line until they find one that works. When they do a flag is set and it starts again knowing it can pass all the data directly through the decoder. Unfortunately it doesn't reset the context it uses. This means that if EOF is reached an attempt is made to pass two EOFs through the context and this causes the resulting error. This can also cause other problems as well. As is usual with these problems it takes *ages* to find and the fix is trivial: move one line. [Steve Henson, reported by ian@uns.ns.ac.yu (Ivan Nejgebauer) ] *) Ugly workaround to get s_client and s_server working under Windows. The old code wouldn't work because it needed to select() on sockets and the tty (for keypresses and to see if data could be written). Win32 only supports select() on sockets so we select() with a 1s timeout on the sockets and then see if any characters are waiting to be read, if none are present then we retry, we also assume we can always write data to the tty. This isn't nice because the code then blocks until we've received a complete line of data and it is effectively polling the keyboard at 1s intervals: however it's quite a bit better than not working at all :-) A dedicated Windows application might handle this with an event loop for example. [Steve Henson] *) Enhance RSA_METHOD structure. Now there are two extra methods, rsa_sign and rsa_verify. When the RSA_FLAGS_SIGN_VER option is set these functions will be called when RSA_sign() and RSA_verify() are used. This is useful if rsa_pub_dec() and rsa_priv_enc() equivalents are not available. For this to work properly RSA_public_decrypt() and RSA_private_encrypt() should *not* be used: RSA_sign() and RSA_verify() must be used instead. This necessitated the support of an extra signature type NID_md5_sha1 for SSL signatures and modifications to the SSL library to use it instead of calling RSA_public_decrypt() and RSA_private_encrypt(). [Steve Henson] *) Add new -verify -CAfile and -CApath options to the crl program, these will lookup a CRL issuers certificate and verify the signature in a similar way to the verify program. Tidy up the crl program so it no longer accesses structures directly. Make the ASN1 CRL parsing a bit less strict. It will now permit CRL extensions even if it is not a V2 CRL: this will allow it to tolerate some broken CRLs. [Steve Henson] *) Initialize all non-automatic variables each time one of the openssl sub-programs is started (this is necessary as they may be started multiple times from the "OpenSSL>" prompt). [Lennart Bang, Bodo Moeller] *) Preliminary compilation option RSA_NULL which disables RSA crypto without removing all other RSA functionality (this is what NO_RSA does). This is so (for example) those in the US can disable those operations covered by the RSA patent while allowing storage and parsing of RSA keys and RSA key generation. [Steve Henson] *) Non-copying interface to BIO pairs. (still largely untested) [Bodo Moeller] *) New function ANS1_tag2str() to convert an ASN1 tag to a descriptive ASCII string. This was handled independently in various places before. [Steve Henson] *) New functions UTF8_getc() and UTF8_putc() that parse and generate UTF8 strings a character at a time. [Steve Henson] *) Use client_version from client hello to select the protocol (s23_srvr.c) and for RSA client key exchange verification (s3_srvr.c), as required by the SSL 3.0/TLS 1.0 specifications. [Bodo Moeller] *) Add various utility functions to handle SPKACs, these were previously handled by poking round in the structure internals. Added new function NETSCAPE_SPKI_print() to print out SPKAC and a new utility 'spkac' to print, verify and generate SPKACs. Based on an original idea from Massimiliano Pala but extensively modified. [Steve Henson] *) RIPEMD160 is operational on all platforms and is back in 'make test'. [Andy Polyakov] *) Allow the config file extension section to be overwritten on the command line. Based on an original idea from Massimiliano Pala . The new option is called -extensions and can be applied to ca, req and x509. Also -reqexts to override the request extensions in req and -crlexts to override the crl extensions in ca. [Steve Henson] *) Add new feature to the SPKAC handling in ca. Now you can include the same field multiple times by preceding it by "XXXX." for example: 1.OU="Unit name 1" 2.OU="Unit name 2" this is the same syntax as used in the req config file. [Steve Henson] *) Allow certificate extensions to be added to certificate requests. These are specified in a 'req_extensions' option of the req section of the config file. They can be printed out with the -text option to req but are otherwise ignored at present. [Steve Henson] *) Fix a horrible bug in enc_read() in crypto/evp/bio_enc.c: if the first data read consists of only the final block it would not decrypted because EVP_CipherUpdate() would correctly report zero bytes had been decrypted. A misplaced 'break' also meant the decrypted final block might not be copied until the next read. [Steve Henson] *) Initial support for DH_METHOD. Again based on RSA_METHOD. Also added a few extra parameters to the DH structure: these will be useful if for example we want the value of 'q' or implement X9.42 DH. [Steve Henson] *) Initial support for DSA_METHOD. This is based on the RSA_METHOD and provides hooks that allow the default DSA functions or functions on a "per key" basis to be replaced. This allows hardware acceleration and hardware key storage to be handled without major modification to the library. Also added low level modexp hooks and CRYPTO_EX structure and associated functions. [Steve Henson] *) Add a new flag to memory BIOs, BIO_FLAG_MEM_RDONLY. This marks the BIO as "read only": it can't be written to and the buffer it points to will not be freed. Reading from a read only BIO is much more efficient than a normal memory BIO. This was added because there are several times when an area of memory needs to be read from a BIO. The previous method was to create a memory BIO and write the data to it, this results in two copies of the data and an O(n^2) reading algorithm. There is a new function BIO_new_mem_buf() which creates a read only memory BIO from an area of memory. Also modified the PKCS#7 routines to use read only memory BIOs. [Steve Henson] *) Bugfix: ssl23_get_client_hello did not work properly when called in state SSL23_ST_SR_CLNT_HELLO_B, i.e. when the first 7 bytes of a SSLv2-compatible client hello for SSLv3 or TLSv1 could be read, but a retry condition occured while trying to read the rest. [Bodo Moeller] *) The PKCS7_ENC_CONTENT_new() function was setting the content type as NID_pkcs7_encrypted by default: this was wrong since this should almost always be NID_pkcs7_data. Also modified the PKCS7_set_type() to handle the encrypted data type: this is a more sensible place to put it and it allows the PKCS#12 code to be tidied up that duplicated this functionality. [Steve Henson] *) Changed obj_dat.pl script so it takes its input and output files on the command line. This should avoid shell escape redirection problems under Win32. [Steve Henson] *) Initial support for certificate extension requests, these are included in things like Xenroll certificate requests. Included functions to allow extensions to be obtained and added. [Steve Henson] *) -crlf option to s_client and s_server for sending newlines as CRLF (as required by many protocols). [Bodo Moeller] Changes between 0.9.3a and 0.9.4 [09 Aug 1999] *) Install libRSAglue.a when OpenSSL is built with RSAref. [Ralf S. Engelschall] *) A few more ``#ifndef NO_FP_API / #endif'' pairs for consistency. [Andrija Antonijevic ] *) Fix -startdate and -enddate (which was missing) arguments to 'ca' program. [Steve Henson] *) New function DSA_dup_DH, which duplicates DSA parameters/keys as DH parameters/keys (q is lost during that conversion, but the resulting DH parameters contain its length). For 1024-bit p, DSA_generate_parameters followed by DSA_dup_DH is much faster than DH_generate_parameters (which creates parameters where p = 2*q + 1), and also the smaller q makes DH computations much more efficient (160-bit exponentiation instead of 1024-bit exponentiation); so this provides a convenient way to support DHE ciphersuites in SSL/TLS servers (see ssl/ssltest.c). It is of utter importance to use SSL_CTX_set_options(s_ctx, SSL_OP_SINGLE_DH_USE); or SSL_set_options(s_ctx, SSL_OP_SINGLE_DH_USE); when such DH parameters are used, because otherwise small subgroup attacks may become possible! [Bodo Moeller] *) Avoid memory leak in i2d_DHparams. [Bodo Moeller] *) Allow the -k option to be used more than once in the enc program: this allows the same encrypted message to be read by multiple recipients. [Steve Henson] *) New function OBJ_obj2txt(buf, buf_len, a, no_name), this converts an ASN1_OBJECT to a text string. If the "no_name" parameter is set then it will always use the numerical form of the OID, even if it has a short or long name. [Steve Henson] *) Added an extra RSA flag: RSA_FLAG_EXT_PKEY. Previously the rsa_mod_exp method only got called if p,q,dmp1,dmq1,iqmp components were present, otherwise bn_mod_exp was called. In the case of hardware keys for example no private key components need be present and it might store extra data in the RSA structure, which cannot be accessed from bn_mod_exp. By setting RSA_FLAG_EXT_PKEY rsa_mod_exp will always be called for private key operations. [Steve Henson] *) Added support for SPARC Linux. [Andy Polyakov] *) pem_password_cb function type incompatibly changed from typedef int pem_password_cb(char *buf, int size, int rwflag); to ....(char *buf, int size, int rwflag, void *userdata); so that applications can pass data to their callbacks: The PEM[_ASN1]_{read,write}... functions and macros now take an additional void * argument, which is just handed through whenever the password callback is called. [Damien Miller ; tiny changes by Bodo Moeller] New function SSL_CTX_set_default_passwd_cb_userdata. Compatibility note: As many C implementations push function arguments onto the stack in reverse order, the new library version is likely to interoperate with programs that have been compiled with the old pem_password_cb definition (PEM_whatever takes some data that happens to be on the stack as its last argument, and the callback just ignores this garbage); but there is no guarantee whatsoever that this will work. *) The -DPLATFORM="\"$(PLATFORM)\"" definition and the similar -DCFLAGS=... (both in crypto/Makefile.ssl for use by crypto/cversion.c) caused problems not only on Windows, but also on some Unix platforms. To avoid problematic command lines, these definitions are now in an auto-generated file crypto/buildinf.h (created by crypto/Makefile.ssl for standard "make" builds, by util/mk1mf.pl for "mk1mf" builds). [Bodo Moeller] *) MIPS III/IV assembler module is reimplemented. [Andy Polyakov] *) More DES library cleanups: remove references to srand/rand and delete an unused file. [Ulf Möller] *) Add support for the the free Netwide assembler (NASM) under Win32, since not many people have MASM (ml) and it can be hard to obtain. This is currently experimental but it seems to work OK and pass all the tests. Check out INSTALL.W32 for info. [Steve Henson] *) Fix memory leaks in s3_clnt.c: All non-anonymous SSL3/TLS1 connections without temporary keys kept an extra copy of the server key, and connections with temporary keys did not free everything in case of an error. [Bodo Moeller] *) New function RSA_check_key and new openssl rsa option -check for verifying the consistency of RSA keys. [Ulf Moeller, Bodo Moeller] *) Various changes to make Win32 compile work: 1. Casts to avoid "loss of data" warnings in p5_crpt2.c 2. Change unsigned int to int in b_dump.c to avoid "signed/unsigned comparison" warnings. 3. Add sk__sort to DEF file generator and do make update. [Steve Henson] *) Add a debugging option to PKCS#5 v2 key generation function: when you #define DEBUG_PKCS5V2 passwords, salts, iteration counts and derived keys are printed to stderr. [Steve Henson] *) Copy the flags in ASN1_STRING_dup(). [Roman E. Pavlov ] *) The x509 application mishandled signing requests containing DSA keys when the signing key was also DSA and the parameters didn't match. It was supposed to omit the parameters when they matched the signing key: the verifying software was then supposed to automatically use the CA's parameters if they were absent from the end user certificate. Omitting parameters is no longer recommended. The test was also the wrong way round! This was probably due to unusual behaviour in EVP_cmp_parameters() which returns 1 if the parameters match. This meant that parameters were omitted when they *didn't* match and the certificate was useless. Certificates signed with 'ca' didn't have this bug. [Steve Henson, reported by Doug Erickson ] *) Memory leak checking (-DCRYPTO_MDEBUG) had some problems. The interface is as follows: Applications can use CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON) aka MemCheck_start(), CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF) aka MemCheck_stop(); "off" is now the default. The library internally uses CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE) aka MemCheck_off(), CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE) aka MemCheck_on() to disable memory-checking temporarily. Some inconsistent states that previously were possible (and were even the default) are now avoided. -DCRYPTO_MDEBUG_TIME is new and additionally stores the current time with each memory chunk allocated; this is occasionally more helpful than just having a counter. -DCRYPTO_MDEBUG_THREAD is also new and adds the thread ID. -DCRYPTO_MDEBUG_ALL enables all of the above, plus any future extensions. [Bodo Moeller] *) Introduce "mode" for SSL structures (with defaults in SSL_CTX), which largely parallels "options", but is for changing API behaviour, whereas "options" are about protocol behaviour. Initial "mode" flags are: SSL_MODE_ENABLE_PARTIAL_WRITE Allow SSL_write to report success when a single record has been written. SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER Don't insist that SSL_write retries use the same buffer location. (But all of the contents must be copied!) [Bodo Moeller] *) Bugfix: SSL_set_options ignored its parameter, only SSL_CTX_set_options worked. *) Fix problems with no-hmac etc. [Ulf Möller, pointed out by Brian Wellington ] *) New functions RSA_get_default_method(), RSA_set_method() and RSA_get_method(). These allows replacement of RSA_METHODs without having to mess around with the internals of an RSA structure. [Steve Henson] *) Fix memory leaks in DSA_do_sign and DSA_is_prime. Also really enable memory leak checks in openssl.c and in some test programs. [Chad C. Mulligan, Bodo Moeller] *) Fix a bug in d2i_ASN1_INTEGER() and i2d_ASN1_INTEGER() which can mess up the length of negative integers. This has now been simplified to just store the length when it is first determined and use it later, rather than trying to keep track of where data is copied and updating it to point to the end. [Steve Henson, reported by Brien Wheeler ] *) Add a new function PKCS7_signatureVerify. This allows the verification of a PKCS#7 signature but with the signing certificate passed to the function itself. This contrasts with PKCS7_dataVerify which assumes the certificate is present in the PKCS#7 structure. This isn't always the case: certificates can be omitted from a PKCS#7 structure and be distributed by "out of band" means (such as a certificate database). [Steve Henson] *) Complete the PEM_* macros with DECLARE_PEM versions to replace the function prototypes in pem.h, also change util/mkdef.pl to add the necessary function names. [Steve Henson] *) mk1mf.pl (used by Windows builds) did not properly read the options set by Configure in the top level Makefile, and Configure was not even able to write more than one option correctly. Fixed, now "no-idea no-rc5 -DCRYPTO_MDEBUG" etc. works as intended. [Bodo Moeller] *) New functions CONF_load_bio() and CONF_load_fp() to allow a config file to be loaded from a BIO or FILE pointer. The BIO version will for example allow memory BIOs to contain config info. [Steve Henson] *) New function "CRYPTO_num_locks" that returns CRYPTO_NUM_LOCKS. Whoever hopes to achieve shared-library compatibility across versions must use this, not the compile-time macro. (Exercise 0.9.4: Which is the minimum library version required by such programs?) Note: All this applies only to multi-threaded programs, others don't need locks. [Bodo Moeller] *) Add missing case to s3_clnt.c state machine -- one of the new SSL tests through a BIO pair triggered the default case, i.e. SSLerr(...,SSL_R_UNKNOWN_STATE). [Bodo Moeller] *) New "BIO pair" concept (crypto/bio/bss_bio.c) so that applications can use the SSL library even if none of the specific BIOs is appropriate. [Bodo Moeller] *) Fix a bug in i2d_DSAPublicKey() which meant it returned the wrong value for the encoded length. [Jeon KyoungHo ] *) Add initial documentation of the X509V3 functions. [Steve Henson] *) Add a new pair of functions PEM_write_PKCS8PrivateKey() and PEM_write_bio_PKCS8PrivateKey() that are equivalent to PEM_write_PrivateKey() and PEM_write_bio_PrivateKey() but use the more secure PKCS#8 private key format with a high iteration count. [Steve Henson] *) Fix determination of Perl interpreter: A perl or perl5 _directory_ in $PATH was also accepted as the interpreter. [Ralf S. Engelschall] *) Fix demos/sign/sign.c: well there wasn't anything strictly speaking wrong with it but it was very old and did things like calling PEM_ASN1_read() directly and used MD5 for the hash not to mention some unusual formatting. [Steve Henson] *) Fix demos/selfsign.c: it used obsolete and deleted functions, changed to use the new extension code. [Steve Henson] *) Implement the PEM_read/PEM_write functions in crypto/pem/pem_all.c with macros. This should make it easier to change their form, add extra arguments etc. Fix a few PEM prototypes which didn't have cipher as a constant. [Steve Henson] *) Add to configuration table a new entry that can specify an alternative name for unistd.h (for pre-POSIX systems); we need this for NeXTstep, according to Mark Crispin . [Bodo Moeller] #if 0 *) DES CBC did not update the IV. Weird. [Ben Laurie] #else des_cbc_encrypt does not update the IV, but des_ncbc_encrypt does. Changing the behaviour of the former might break existing programs -- where IV updating is needed, des_ncbc_encrypt can be used. #endif *) When bntest is run from "make test" it drives bc to check its calculations, as well as internally checking them. If an internal check fails, it needs to cause bc to give a non-zero result or make test carries on without noticing the failure. Fixed. [Ben Laurie] *) DES library cleanups. [Ulf Möller] *) Add support for PKCS#5 v2.0 PBE algorithms. This will permit PKCS#8 to be used with any cipher unlike PKCS#5 v1.5 which can at most handle 64 bit ciphers. NOTE: although the key derivation function has been verified against some published test vectors it has not been extensively tested yet. Added a -v2 "cipher" option to pkcs8 application to allow the use of v2.0. [Steve Henson] *) Instead of "mkdir -p", which is not fully portable, use new Perl script "util/mkdir-p.pl". [Bodo Moeller] *) Rewrite the way password based encryption (PBE) is handled. It used to assume that the ASN1 AlgorithmIdentifier parameter was a PBEParameter structure. This was true for the PKCS#5 v1.5 and PKCS#12 PBE algorithms but doesn't apply to PKCS#5 v2.0 where it can be something else. Now the 'parameter' field of the AlgorithmIdentifier is passed to the underlying key generation function so it must do its own ASN1 parsing. This has also changed the EVP_PBE_CipherInit() function which now has a 'parameter' argument instead of literal salt and iteration count values and the function EVP_PBE_ALGOR_CipherInit() has been deleted. [Steve Henson] *) Support for PKCS#5 v1.5 compatible password based encryption algorithms and PKCS#8 functionality. New 'pkcs8' application linked to openssl. Needed to change the PEM_STRING_EVP_PKEY value which was just "PRIVATE KEY" because this clashed with PKCS#8 unencrypted string. Since this value was just used as a "magic string" and not used directly its value doesn't matter. [Steve Henson] *) Introduce some semblance of const correctness to BN. Shame C doesn't support mutable. [Ben Laurie] *) "linux-sparc64" configuration (ultrapenguin). [Ray Miller ] "linux-sparc" configuration. [Christian Forster ] *) config now generates no-xxx options for missing ciphers. [Ulf Möller] *) Support the EBCDIC character set (work in progress). File ebcdic.c not yet included because it has a different license. [Martin Kraemer ] *) Support BS2000/OSD-POSIX. [Martin Kraemer ] *) Make callbacks for key generation use void * instead of char *. [Ben Laurie] *) Make S/MIME samples compile (not yet tested). [Ben Laurie] *) Additional typesafe stacks. [Ben Laurie] *) New configuration variants "bsdi-elf-gcc" (BSD/OS 4.x). [Bodo Moeller] Changes between 0.9.3 and 0.9.3a [29 May 1999] *) New configuration variant "sco5-gcc". *) Updated some demos. [Sean O Riordain, Wade Scholine] *) Add missing BIO_free at exit of pkcs12 application. [Wu Zhigang] *) Fix memory leak in conf.c. [Steve Henson] *) Updates for Win32 to assembler version of MD5. [Steve Henson] *) Set #! path to perl in apps/der_chop to where we found it instead of using a fixed path. [Bodo Moeller] *) SHA library changes for irix64-mips4-cc. [Andy Polyakov] *) Improvements for VMS support. [Richard Levitte] Changes between 0.9.2b and 0.9.3 [24 May 1999] *) Bignum library bug fix. IRIX 6 passes "make test" now! This also avoids the problems with SC4.2 and unpatched SC5. [Andy Polyakov ] *) New functions sk_num, sk_value and sk_set to replace the previous macros. These are required because of the typesafe stack would otherwise break existing code. If old code used a structure member which used to be STACK and is now STACK_OF (for example cert in a PKCS7_SIGNED structure) with sk_num or sk_value it would produce an error because the num, data members are not present in STACK_OF. Now it just produces a warning. sk_set replaces the old method of assigning a value to sk_value (e.g. sk_value(x, i) = y) which the library used in a few cases. Any code that does this will no longer work (and should use sk_set instead) but this could be regarded as a "questionable" behaviour anyway. [Steve Henson] *) Fix most of the other PKCS#7 bugs. The "experimental" code can now correctly handle encrypted S/MIME data. [Steve Henson] *) Change type of various DES function arguments from des_cblock (which means, in function argument declarations, pointer to char) to des_cblock * (meaning pointer to array with 8 char elements), which allows the compiler to do more typechecking; it was like that back in SSLeay, but with lots of ugly casts. Introduce new type const_des_cblock. [Bodo Moeller] *) Reorganise the PKCS#7 library and get rid of some of the more obvious problems: find RecipientInfo structure that matches recipient certificate and initialise the ASN1 structures properly based on passed cipher. [Steve Henson] *) Belatedly make the BN tests actually check the results. [Ben Laurie] *) Fix the encoding and decoding of negative ASN1 INTEGERS and conversion to and from BNs: it was completely broken. New compilation option NEG_PUBKEY_BUG to allow for some broken certificates that encode public key elements as negative integers. [Steve Henson] *) Reorganize and speed up MD5. [Andy Polyakov ] *) VMS support. [Richard Levitte ] *) New option -out to asn1parse to allow the parsed structure to be output to a file. This is most useful when combined with the -strparse option to examine the output of things like OCTET STRINGS. [Steve Henson] *) Make SSL library a little more fool-proof by not requiring any longer that SSL_set_{accept,connect}_state be called before SSL_{accept,connect} may be used (SSL_set_..._state is omitted in many applications because usually everything *appeared* to work as intended anyway -- now it really works as intended). [Bodo Moeller] *) Move openssl.cnf out of lib/. [Ulf Möller] *) Fix various things to let OpenSSL even pass ``egcc -pipe -O2 -Wall -Wshadow -Wpointer-arith -Wcast-align -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Winline'' with EGCS 1.1.2+ [Ralf S. Engelschall] *) Various fixes to the EVP and PKCS#7 code. It may now be able to handle PKCS#7 enveloped data properly. [Sebastian Akerman , modified by Steve] *) Create a duplicate of the SSL_CTX's CERT in SSL_new instead of copying pointers. The cert_st handling is changed by this in various ways (and thus what used to be known as ctx->default_cert is now called ctx->cert, since we don't resort to s->ctx->[default_]cert any longer when s->cert does not give us what we need). ssl_cert_instantiate becomes obsolete by this change. As soon as we've got the new code right (possibly it already is?), we have solved a couple of bugs of the earlier code where s->cert was used as if it could not have been shared with other SSL structures. Note that using the SSL API in certain dirty ways now will result in different behaviour than observed with earlier library versions: Changing settings for an SSL_CTX *ctx after having done s = SSL_new(ctx) does not influence s as it used to. In order to clean up things more thoroughly, inside SSL_SESSION we don't use CERT any longer, but a new structure SESS_CERT that holds per-session data (if available); currently, this is the peer's certificate chain and, for clients, the server's certificate and temporary key. CERT holds only those values that can have meaningful defaults in an SSL_CTX. [Bodo Moeller] *) New function X509V3_EXT_i2d() to create an X509_EXTENSION structure from the internal representation. Various PKCS#7 fixes: remove some evil casts and set the enc_dig_alg field properly based on the signing key type. [Steve Henson] *) Allow PKCS#12 password to be set from the command line or the environment. Let 'ca' get its config file name from the environment variables "OPENSSL_CONF" or "SSLEAY_CONF" (for consistency with 'req' and 'x509'). [Steve Henson] *) Allow certificate policies extension to use an IA5STRING for the organization field. This is contrary to the PKIX definition but VeriSign uses it and IE5 only recognises this form. Document 'x509' extension option. [Steve Henson] *) Add PEDANTIC compiler flag to allow compilation with gcc -pedantic, without disallowing inline assembler and the like for non-pedantic builds. [Ben Laurie] *) Support Borland C++ builder. [Janez Jere , modified by Ulf Möller] *) Support Mingw32. [Ulf Möller] *) SHA-1 cleanups and performance enhancements. [Andy Polyakov ] *) Sparc v8plus assembler for the bignum library. [Andy Polyakov ] *) Accept any -xxx and +xxx compiler options in Configure. [Ulf Möller] *) Update HPUX configuration. [Anonymous] *) Add missing sk__unshift() function to safestack.h [Ralf S. Engelschall] *) New function SSL_CTX_use_certificate_chain_file that sets the "extra_cert"s in addition to the certificate. (This makes sense only for "PEM" format files, as chains as a whole are not DER-encoded.) [Bodo Moeller] *) Support verify_depth from the SSL API. x509_vfy.c had what can be considered an off-by-one-error: Its depth (which was not part of the external interface) was actually counting the number of certificates in a chain; now it really counts the depth. [Bodo Moeller] *) Bugfix in crypto/x509/x509_cmp.c: The SSLerr macro was used instead of X509err, which often resulted in confusing error messages since the error codes are not globally unique (e.g. an alleged error in ssl3_accept when a certificate didn't match the private key). *) New function SSL_CTX_set_session_id_context that allows to set a default value (so that you don't need SSL_set_session_id_context for each connection using the SSL_CTX). [Bodo Moeller] *) OAEP decoding bug fix. [Ulf Möller] *) Support INSTALL_PREFIX for package builders, as proposed by David Harris. [Bodo Moeller] *) New Configure options "threads" and "no-threads". For systems where the proper compiler options are known (currently Solaris and Linux), "threads" is the default. [Bodo Moeller] *) New script util/mklink.pl as a faster substitute for util/mklink.sh. [Bodo Moeller] *) Install various scripts to $(OPENSSLDIR)/misc, not to $(INSTALLTOP)/bin -- they shouldn't clutter directories such as /usr/local/bin. [Bodo Moeller] *) "make linux-shared" to build shared libraries. [Niels Poppe ] *) New Configure option no- (rsa, idea, rc5, ...). [Ulf Möller] *) Add the PKCS#12 API documentation to openssl.txt. Preliminary support for extension adding in x509 utility. [Steve Henson] *) Remove NOPROTO sections and error code comments. [Ulf Möller] *) Partial rewrite of the DEF file generator to now parse the ANSI prototypes. [Steve Henson] *) New Configure options --prefix=DIR and --openssldir=DIR. [Ulf Möller] *) Complete rewrite of the error code script(s). It is all now handled by one script at the top level which handles error code gathering, header rewriting and C source file generation. It should be much better than the old method: it now uses a modified version of Ulf's parser to read the ANSI prototypes in all header files (thus the old K&R definitions aren't needed for error creation any more) and do a better job of translating function codes into names. The old 'ASN1 error code imbedded in a comment' is no longer necessary and it doesn't use .err files which have now been deleted. Also the error code call doesn't have to appear all on one line (which resulted in some large lines...). [Steve Henson] *) Change #include filenames from to . [Bodo Moeller] *) Change behaviour of ssl2_read when facing length-0 packets: Don't return 0 (which usually indicates a closed connection), but continue reading. [Bodo Moeller] *) Fix some race conditions. [Bodo Moeller] *) Add support for CRL distribution points extension. Add Certificate Policies and CRL distribution points documentation. [Steve Henson] *) Move the autogenerated header file parts to crypto/opensslconf.h. [Ulf Möller] *) Fix new 56-bit DES export ciphersuites: they were using 7 bytes instead of 8 of keying material. Merlin has also confirmed interop with this fix between OpenSSL and Baltimore C/SSL 2.0 and J/SSL 2.0. [Merlin Hughes ] *) Fix lots of warnings. [Richard Levitte ] *) In add_cert_dir() in crypto/x509/by_dir.c, break out of the loop if the directory spec didn't end with a LIST_SEPARATOR_CHAR. [Richard Levitte ] *) Fix problems with sizeof(long) == 8. [Andy Polyakov ] *) Change functions to ANSI C. [Ulf Möller] *) Fix typos in error codes. [Martin Kraemer , Ulf Möller] *) Remove defunct assembler files from Configure. [Ulf Möller] *) SPARC v8 assembler BIGNUM implementation. [Andy Polyakov ] *) Support for Certificate Policies extension: both print and set. Various additions to support the r2i method this uses. [Steve Henson] *) A lot of constification, and fix a bug in X509_NAME_oneline() that could return a const string when you are expecting an allocated buffer. [Ben Laurie] *) Add support for ASN1 types UTF8String and VISIBLESTRING, also the CHOICE types DirectoryString and DisplayText. [Steve Henson] *) Add code to allow r2i extensions to access the configuration database, add an LHASH database driver and add several ctx helper functions. [Steve Henson] *) Fix an evil bug in bn_expand2() which caused various BN functions to fail when they extended the size of a BIGNUM. [Steve Henson] *) Various utility functions to handle SXNet extension. Modify mkdef.pl to support typesafe stack. [Steve Henson] *) Fix typo in SSL_[gs]et_options(). [Nils Frostberg ] *) Delete various functions and files that belonged to the (now obsolete) old X509V3 handling code. [Steve Henson] *) New Configure option "rsaref". [Ulf Möller] *) Don't auto-generate pem.h. [Bodo Moeller] *) Introduce type-safe ASN.1 SETs. [Ben Laurie] *) Convert various additional casted stacks to type-safe STACK_OF() variants. [Ben Laurie, Ralf S. Engelschall, Steve Henson] *) Introduce type-safe STACKs. This will almost certainly break lots of code that links with OpenSSL (well at least cause lots of warnings), but fear not: the conversion is trivial, and it eliminates loads of evil casts. A few STACKed things have been converted already. Feel free to convert more. In the fullness of time, I'll do away with the STACK type altogether. [Ben Laurie] *) Add `openssl ca -revoke ' facility which revokes a certificate specified in by updating the entry in the index.txt file. This way one no longer has to edit the index.txt file manually for revoking a certificate. The -revoke option does the gory details now. [Massimiliano Pala , Ralf S. Engelschall] *) Fix `openssl crl -noout -text' combination where `-noout' killed the `-text' option at all and this way the `-noout -text' combination was inconsistent in `openssl crl' with the friends in `openssl x509|rsa|dsa'. [Ralf S. Engelschall] *) Make sure a corresponding plain text error message exists for the X509_V_ERR_CERT_REVOKED/23 error number which can occur when a verify callback function determined that a certificate was revoked. [Ralf S. Engelschall] *) Bugfix: In test/testenc, don't test "openssl " for ciphers that were excluded, e.g. by -DNO_IDEA. Also, test all available cipers including rc5, which was forgotten until now. In order to let the testing shell script know which algorithms are available, a new (up to now undocumented) command "openssl list-cipher-commands" is used. [Bodo Moeller] *) Bugfix: s_client occasionally would sleep in select() when it should have checked SSL_pending() first. [Bodo Moeller] *) New functions DSA_do_sign and DSA_do_verify to provide access to the raw DSA values prior to ASN.1 encoding. [Ulf Möller] *) Tweaks to Configure [Niels Poppe ] *) Add support for PKCS#5 v2.0 ASN1 PBES2 structures. No other support, yet... [Steve Henson] *) New variables $(RANLIB) and $(PERL) in the Makefiles. [Ulf Möller] *) New config option to avoid instructions that are illegal on the 80386. The default code is faster, but requires at least a 486. [Ulf Möller] *) Got rid of old SSL2_CLIENT_VERSION (inconsistently used) and SSL2_SERVER_VERSION (not used at all) macros, which are now the same as SSL2_VERSION anyway. [Bodo Moeller] *) New "-showcerts" option for s_client. [Bodo Moeller] *) Still more PKCS#12 integration. Add pkcs12 application to openssl application. Various cleanups and fixes. [Steve Henson] *) More PKCS#12 integration. Add new pkcs12 directory with Makefile.ssl and modify error routines to work internally. Add error codes and PBE init to library startup routines. [Steve Henson] *) Further PKCS#12 integration. Added password based encryption, PKCS#8 and packing functions to asn1 and evp. Changed function names and error codes along the way. [Steve Henson] *) PKCS12 integration: and so it begins... First of several patches to slowly integrate PKCS#12 functionality into OpenSSL. Add PKCS#12 objects to objects.h [Steve Henson] *) Add a new 'indent' option to some X509V3 extension code. Initial ASN1 and display support for Thawte strong extranet extension. [Steve Henson] *) Add LinuxPPC support. [Jeff Dubrule ] *) Get rid of redundant BN file bn_mulw.c, and rename bn_div64 to bn_div_words in alpha.s. [Hannes Reinecke and Ben Laurie] *) Make sure the RSA OAEP test is skipped under -DRSAref because OAEP isn't supported when OpenSSL is built with RSAref. [Ulf Moeller ] *) Move definitions of IS_SET/IS_SEQUENCE inside crypto/asn1/asn1.h so they no longer are missing under -DNOPROTO. [Soren S. Jorvang ] Changes between 0.9.1c and 0.9.2b [22 Mar 1999] *) Make SSL_get_peer_cert_chain() work in servers. Unfortunately, it still doesn't work when the session is reused. Coming soon! [Ben Laurie] *) Fix a security hole, that allows sessions to be reused in the wrong context thus bypassing client cert protection! All software that uses client certs and session caches in multiple contexts NEEDS PATCHING to allow session reuse! A fuller solution is in the works. [Ben Laurie, problem pointed out by Holger Reif, Bodo Moeller (and ???)] *) Some more source tree cleanups (removed obsolete files crypto/bf/asm/bf586.pl, test/test.txt and crypto/sha/asm/f.s; changed permission on "config" script to be executable) and a fix for the INSTALL document. [Ulf Moeller ] *) Remove some legacy and erroneous uses of malloc, free instead of Malloc, Free. [Lennart Bang , with minor changes by Steve] *) Make rsa_oaep_test return non-zero on error. [Ulf Moeller ] *) Add support for native Solaris shared libraries. Configure solaris-sparc-sc4-pic, make, then run shlib/solaris-sc4.sh. It'd be nice if someone would make that last step automatic. [Matthias Loepfe ] *) ctx_size was not built with the right compiler during "make links". Fixed. [Ben Laurie] *) Change the meaning of 'ALL' in the cipher list. It now means "everything except NULL ciphers". This means the default cipher list will no longer enable NULL ciphers. They need to be specifically enabled e.g. with the string "DEFAULT:eNULL". [Steve Henson] *) Fix to RSA private encryption routines: if p < q then it would occasionally produce an invalid result. This will only happen with externally generated keys because OpenSSL (and SSLeay) ensure p > q. [Steve Henson] *) Be less restrictive and allow also `perl util/perlpath.pl /path/to/bin/perl' in addition to `perl util/perlpath.pl /path/to/bin', because this way one can also use an interpreter named `perl5' (which is usually the name of Perl 5.xxx on platforms where an Perl 4.x is still installed as `perl'). [Matthias Loepfe ] *) Let util/clean-depend.pl work also with older Perl 5.00x versions. [Matthias Loepfe ] *) Fix Makefile.org so CC,CFLAG etc are passed to 'make links' add advapi32.lib to Win32 build and change the pem test comparision to fc.exe (thanks to Ulrich Kroener for the suggestion). Fix misplaced ASNI prototypes and declarations in evp.h and crypto/des/ede_cbcm_enc.c. [Steve Henson] *) DES quad checksum was broken on big-endian architectures. Fixed. [Ben Laurie] *) Comment out two functions in bio.h that aren't implemented. Fix up the Win32 test batch file so it (might) work again. The Win32 test batch file is horrible: I feel ill.... [Steve Henson] *) Move various #ifdefs around so NO_SYSLOG, NO_DIRENT etc are now selected in e_os.h. Audit of header files to check ANSI and non ANSI sections: 10 functions were absent from non ANSI section and not exported from Windows DLLs. Fixed up libeay.num for new functions. [Steve Henson] *) Make `openssl version' output lines consistent. [Ralf S. Engelschall] *) Fix Win32 symbol export lists for BIO functions: Added BIO_get_ex_new_index, BIO_get_ex_num, BIO_get_ex_data and BIO_set_ex_data to ms/libeay{16,32}.def. [Ralf S. Engelschall] *) Second round of fixing the OpenSSL perl/ stuff. It now at least compiled fine under Unix and passes some trivial tests I've now added. But the whole stuff is horribly incomplete, so a README.1ST with a disclaimer was added to make sure no one expects that this stuff really works in the OpenSSL 0.9.2 release. Additionally I've started to clean the XS sources up and fixed a few little bugs and inconsistencies in OpenSSL.{pm,xs} and openssl_bio.xs. [Ralf S. Engelschall] *) Fix the generation of two part addresses in perl. [Kenji Miyake , integrated by Ben Laurie] *) Add config entry for Linux on MIPS. [John Tobey ] *) Make links whenever Configure is run, unless we are on Windoze. [Ben Laurie] *) Permit extensions to be added to CRLs using crl_section in openssl.cnf. Currently only issuerAltName and AuthorityKeyIdentifier make any sense in CRLs. [Steve Henson] *) Add a useful kludge to allow package maintainers to specify compiler and other platforms details on the command line without having to patch the Configure script everytime: One now can use ``perl Configure :
'', i.e. platform ids are allowed to have details appended to them (seperated by colons). This is treated as there would be a static pre-configured entry in Configure's %table under key with value
and ``perl Configure '' is called. So, when you want to perform a quick test-compile under FreeBSD 3.1 with pgcc and without assembler stuff you can use ``perl Configure "FreeBSD-elf:pgcc:-O6:::"'' now, which overrides the FreeBSD-elf entry on-the-fly. [Ralf S. Engelschall] *) Disable new TLS1 ciphersuites by default: they aren't official yet. [Ben Laurie] *) Allow DSO flags like -fpic, -fPIC, -KPIC etc. to be specified on the `perl Configure ...' command line. This way one can compile OpenSSL libraries with Position Independent Code (PIC) which is needed for linking it into DSOs. [Ralf S. Engelschall] *) Remarkably, export ciphers were totally broken and no-one had noticed! Fixed. [Ben Laurie] *) Cleaned up the LICENSE document: The official contact for any license questions now is the OpenSSL core team under openssl-core@openssl.org. And add a paragraph about the dual-license situation to make sure people recognize that _BOTH_ the OpenSSL license _AND_ the SSLeay license apply to the OpenSSL toolkit. [Ralf S. Engelschall] *) General source tree makefile cleanups: Made `making xxx in yyy...' display consistent in the source tree and replaced `/bin/rm' by `rm'. Additonally cleaned up the `make links' target: Remove unnecessary semicolons, subsequent redundant removes, inline point.sh into mklink.sh to speed processing and no longer clutter the display with confusing stuff. Instead only the actually done links are displayed. [Ralf S. Engelschall] *) Permit null encryption ciphersuites, used for authentication only. It used to be necessary to set the preprocessor define SSL_ALLOW_ENULL to do this. It is now necessary to set SSL_FORBID_ENULL to prevent the use of null encryption. [Ben Laurie] *) Add a bunch of fixes to the PKCS#7 stuff. It used to sometimes reorder signed attributes when verifying signatures (this would break them), the detached data encoding was wrong and public keys obtained using X509_get_pubkey() weren't freed. [Steve Henson] *) Add text documentation for the BUFFER functions. Also added a work around to a Win95 console bug. This was triggered by the password read stuff: the last character typed gets carried over to the next fread(). If you were generating a new cert request using 'req' for example then the last character of the passphrase would be CR which would then enter the first field as blank. [Steve Henson] *) Added the new `Includes OpenSSL Cryptography Software' button as doc/openssl_button.{gif,html} which is similar in style to the old SSLeay button and can be used by applications based on OpenSSL to show the relationship to the OpenSSL project. [Ralf S. Engelschall] *) Remove confusing variables in function signatures in files ssl/ssl_lib.c and ssl/ssl.h. [Lennart Bong ] *) Don't install bss_file.c under PREFIX/include/ [Lennart Bong ] *) Get the Win32 compile working again. Modify mkdef.pl so it can handle functions that return function pointers and has support for NT specific stuff. Fix mk1mf.pl and VC-32.pl to support NT differences also. Various #ifdef WIN32 and WINNTs sprinkled about the place and some changes from unsigned to signed types: this was killing the Win32 compile. [Steve Henson] *) Add new certificate file to stack functions, SSL_add_dir_cert_subjects_to_stack() and SSL_add_file_cert_subjects_to_stack(). These largely supplant SSL_load_client_CA_file(), and can be used to add multiple certs easily to a stack (usually this is then handed to SSL_CTX_set_client_CA_list()). This means that Apache-SSL and similar packages don't have to mess around to add as many CAs as they want to the preferred list. [Ben Laurie] *) Experiment with doxygen documentation. Currently only partially applied to ssl/ssl_lib.c. See http://www.stack.nl/~dimitri/doxygen/index.html, and run doxygen with openssl.doxy as the configuration file. [Ben Laurie] *) Get rid of remaining C++-style comments which strict C compilers hate. [Ralf S. Engelschall, pointed out by Carlos Amengual] *) Changed BN_RECURSION in bn_mont.c to BN_RECURSION_MONT so it is not compiled in by default: it has problems with large keys. [Steve Henson] *) Add a bunch of SSL_xxx() functions for configuring the temporary RSA and DH private keys and/or callback functions which directly correspond to their SSL_CTX_xxx() counterparts but work on a per-connection basis. This is needed for applications which have to configure certificates on a per-connection basis (e.g. Apache+mod_ssl) instead of a per-context basis (e.g. s_server). For the RSA certificate situation is makes no difference, but for the DSA certificate situation this fixes the "no shared cipher" problem where the OpenSSL cipher selection procedure failed because the temporary keys were not overtaken from the context and the API provided no way to reconfigure them. The new functions now let applications reconfigure the stuff and they are in detail: SSL_need_tmp_RSA, SSL_set_tmp_rsa, SSL_set_tmp_dh, SSL_set_tmp_rsa_callback and SSL_set_tmp_dh_callback. Additionally a new non-public-API function ssl_cert_instantiate() is used as a helper function and also to reduce code redundancy inside ssl_rsa.c. [Ralf S. Engelschall] *) Move s_server -dcert and -dkey options out of the undocumented feature area because they are useful for the DSA situation and should be recognized by the users. [Ralf S. Engelschall] *) Fix the cipher decision scheme for export ciphers: the export bits are *not* within SSL_MKEY_MASK or SSL_AUTH_MASK, they are within SSL_EXP_MASK. So, the original variable has to be used instead of the already masked variable. [Richard Levitte ] *) Fix 'port' variable from `int' to `unsigned int' in crypto/bio/b_sock.c [Richard Levitte ] *) Change type of another md_len variable in pk7_doit.c:PKCS7_dataFinal() from `int' to `unsigned int' because it's a length and initialized by EVP_DigestFinal() which expects an `unsigned int *'. [Richard Levitte ] *) Don't hard-code path to Perl interpreter on shebang line of Configure script. Instead use the usual Shell->Perl transition trick. [Ralf S. Engelschall] *) Make `openssl x509 -noout -modulus' functional also for DSA certificates (in addition to RSA certificates) to match the behaviour of `openssl dsa -noout -modulus' as it's already the case for `openssl rsa -noout -modulus'. For RSA the -modulus is the real "modulus" while for DSA currently the public key is printed (a decision which was already done by `openssl dsa -modulus' in the past) which serves a similar purpose. Additionally the NO_RSA no longer completely removes the whole -modulus option; it now only avoids using the RSA stuff. Same applies to NO_DSA now, too. [Ralf S. Engelschall] *) Add Arne Ansper's reliable BIO - this is an encrypted, block-digested BIO. See the source (crypto/evp/bio_ok.c) for more info. [Arne Ansper ] *) Dump the old yucky req code that tried (and failed) to allow raw OIDs to be added. Now both 'req' and 'ca' can use new objects defined in the config file. [Steve Henson] *) Add cool BIO that does syslog (or event log on NT). [Arne Ansper , integrated by Ben Laurie] *) Add support for new TLS ciphersuites, TLS_RSA_EXPORT56_WITH_RC4_56_MD5, TLS_RSA_EXPORT56_WITH_RC2_CBC_56_MD5 and TLS_RSA_EXPORT56_WITH_DES_CBC_SHA, as specified in "56-bit Export Cipher Suites For TLS", draft-ietf-tls-56-bit-ciphersuites-00.txt. [Ben Laurie] *) Add preliminary config info for new extension code. [Steve Henson] *) Make RSA_NO_PADDING really use no padding. [Ulf Moeller ] *) Generate errors when private/public key check is done. [Ben Laurie] *) Overhaul for 'crl' utility. New function X509_CRL_print. Partial support for some CRL extensions and new objects added. [Steve Henson] *) Really fix the ASN1 IMPLICIT bug this time... Partial support for private key usage extension and fuller support for authority key id. [Steve Henson] *) Add OAEP encryption for the OpenSSL crypto library. OAEP is the improved padding method for RSA, which is recommended for new applications in PKCS #1 v2.0 (RFC 2437, October 1998). OAEP (Optimal Asymmetric Encryption Padding) has better theoretical foundations than the ad-hoc padding used in PKCS #1 v1.5. It is secure against Bleichbacher's attack on RSA. [Ulf Moeller , reformatted, corrected and integrated by Ben Laurie] *) Updates to the new SSL compression code [Eric A. Young, (from changes to C2Net SSLeay, integrated by Mark Cox)] *) Fix so that the version number in the master secret, when passed via RSA, checks that if TLS was proposed, but we roll back to SSLv3 (because the server will not accept higher), that the version number is 0x03,0x01, not 0x03,0x00 [Eric A. Young, (from changes to C2Net SSLeay, integrated by Mark Cox)] *) Run extensive memory leak checks on SSL apps. Fixed *lots* of memory leaks in ssl/ relating to new X509_get_pubkey() behaviour. Also fixes in apps/ and an unrelated leak in crypto/dsa/dsa_vrf.c [Steve Henson] *) Support for RAW extensions where an arbitrary extension can be created by including its DER encoding. See apps/openssl.cnf for an example. [Steve Henson] *) Make sure latest Perl versions don't interpret some generated C array code as Perl array code in the crypto/err/err_genc.pl script. [Lars Weber <3weber@informatik.uni-hamburg.de>] *) Modify ms/do_ms.bat to not generate assembly language makefiles since not many people have the assembler. Various Win32 compilation fixes and update to the INSTALL.W32 file with (hopefully) more accurate Win32 build instructions. [Steve Henson] *) Modify configure script 'Configure' to automatically create crypto/date.h file under Win32 and also build pem.h from pem.org. New script util/mkfiles.pl to create the MINFO file on environments that can't do a 'make files': perl util/mkfiles.pl >MINFO should work. [Steve Henson] *) Major rework of DES function declarations, in the pursuit of correctness and purity. As a result, many evil casts evaporated, and some weirdness, too. You may find this causes warnings in your code. Zapping your evil casts will probably fix them. Mostly. [Ben Laurie] *) Fix for a typo in asn1.h. Bug fix to object creation script obj_dat.pl. It considered a zero in an object definition to mean "end of object": none of the objects in objects.h have any zeros so it wasn't spotted. [Steve Henson, reported by Erwann ABALEA ] *) Add support for Triple DES Cipher Block Chaining with Output Feedback Masking (CBCM). In the absence of test vectors, the best I have been able to do is check that the decrypt undoes the encrypt, so far. Send me test vectors if you have them. [Ben Laurie] *) Correct calculation of key length for export ciphers (too much space was allocated for null ciphers). This has not been tested! [Ben Laurie] *) Modifications to the mkdef.pl for Win32 DEF file creation. The usage message is now correct (it understands "crypto" and "ssl" on its command line). There is also now an "update" option. This will update the util/ssleay.num and util/libeay.num files with any new functions. If you do a: perl util/mkdef.pl crypto ssl update it will update them. [Steve Henson] *) Overhauled the Perl interface (perl/*): - ported BN stuff to OpenSSL's different BN library - made the perl/ source tree CVS-aware - renamed the package from SSLeay to OpenSSL (the files still contain their history because I've copied them in the repository) - removed obsolete files (the test scripts will be replaced by better Test::Harness variants in the future) [Ralf S. Engelschall] *) First cut for a very conservative source tree cleanup: 1. merge various obsolete readme texts into doc/ssleay.txt where we collect the old documents and readme texts. 2. remove the first part of files where I'm already sure that we no longer need them because of three reasons: either they are just temporary files which were left by Eric or they are preserved original files where I've verified that the diff is also available in the CVS via "cvs diff -rSSLeay_0_8_1b" or they were renamed (as it was definitely the case for the crypto/md/ stuff). [Ralf S. Engelschall] *) More extension code. Incomplete support for subject and issuer alt name, issuer and authority key id. Change the i2v function parameters and add an extra 'crl' parameter in the X509V3_CTX structure: guess what that's for :-) Fix to ASN1 macro which messed up IMPLICIT tag and add f_enum.c which adds a2i, i2a for ENUMERATED. [Steve Henson] *) Preliminary support for ENUMERATED type. This is largely copied from the INTEGER code. [Steve Henson] *) Add new function, EVP_MD_CTX_copy() to replace frequent use of memcpy. [Eric A. Young, (from changes to C2Net SSLeay, integrated by Mark Cox)] *) Make sure `make rehash' target really finds the `openssl' program. [Ralf S. Engelschall, Matthias Loepfe ] *) Squeeze another 7% of speed out of MD5 assembler, at least on a P2. I'd like to hear about it if this slows down other processors. [Ben Laurie] *) Add CygWin32 platform information to Configure script. [Alan Batie ] *) Fixed ms/32all.bat script: `no_asm' -> `no-asm' [Rainer W. Gerling ] *) New program nseq to manipulate netscape certificate sequences [Steve Henson] *) Modify crl2pkcs7 so it supports multiple -certfile arguments. Fix a few typos. [Steve Henson] *) Fixes to BN code. Previously the default was to define BN_RECURSION but the BN code had some problems that would cause failures when doing certificate verification and some other functions. [Eric A. Young, (from changes to C2Net SSLeay, integrated by Mark Cox)] *) Add ASN1 and PEM code to support netscape certificate sequences. [Steve Henson] *) Add ASN1 and PEM code to support netscape certificate sequences. [Steve Henson] *) Add several PKIX and private extended key usage OIDs. [Steve Henson] *) Modify the 'ca' program to handle the new extension code. Modify openssl.cnf for new extension format, add comments. [Steve Henson] *) More X509 V3 changes. Fix typo in v3_bitstr.c. Add support to 'req' and add a sample to openssl.cnf so req -x509 now adds appropriate CA extensions. [Steve Henson] *) Continued X509 V3 changes. Add to other makefiles, integrate with the error code, add initial support to X509_print() and x509 application. [Steve Henson] *) Takes a deep breath and start addding X509 V3 extension support code. Add files in crypto/x509v3. Move original stuff to crypto/x509v3/old. All this stuff is currently isolated and isn't even compiled yet. [Steve Henson] *) Continuing patches for GeneralizedTime. Fix up certificate and CRL ASN1 to use ASN1_TIME and modify print routines to use ASN1_TIME_print. Removed the versions check from X509 routines when loading extensions: this allows certain broken certificates that don't set the version properly to be processed. [Steve Henson] *) Deal with irritating shit to do with dependencies, in YAAHW (Yet Another Ad Hoc Way) - Makefile.ssls now all contain local dependencies, which can still be regenerated with "make depend". [Ben Laurie] *) Spelling mistake in C version of CAST-128. [Ben Laurie, reported by Jeremy Hylton ] *) Changes to the error generation code. The perl script err-code.pl now reads in the old error codes and retains the old numbers, only adding new ones if necessary. It also only changes the .err files if new codes are added. The makefiles have been modified to only insert errors when needed (to avoid needlessly modifying header files). This is done by only inserting errors if the .err file is newer than the auto generated C file. To rebuild all the error codes from scratch (the old behaviour) either modify crypto/Makefile.ssl to pass the -regen flag to err_code.pl or delete all the .err files. [Steve Henson] *) CAST-128 was incorrectly implemented for short keys. The C version has been fixed, but is untested. The assembler versions are also fixed, but new assembler HAS NOT BEEN GENERATED FOR WIN32 - the Makefile needs fixing to regenerate it if needed. [Ben Laurie, reported (with fix for C version) by Jun-ichiro itojun Hagino ] *) File was opened incorrectly in randfile.c. [Ulf Möller ] *) Beginning of support for GeneralizedTime. d2i, i2d, check and print functions. Also ASN1_TIME suite which is a CHOICE of UTCTime or GeneralizedTime. ASN1_TIME is the proper type used in certificates et al: it's just almost always a UTCTime. Note this patch adds new error codes so do a "make errors" if there are problems. [Steve Henson] *) Correct Linux 1 recognition in config. [Ulf Möller ] *) Remove pointless MD5 hash when using DSA keys in ca. [Anonymous ] *) Generate an error if given an empty string as a cert directory. Also generate an error if handed NULL (previously returned 0 to indicate an error, but didn't set one). [Ben Laurie, reported by Anonymous ] *) Add prototypes to SSL methods. Make SSL_write's buffer const, at last. [Ben Laurie] *) Fix the dummy function BN_ref_mod_exp() in rsaref.c to have the correct parameters. This was causing a warning which killed off the Win32 compile. [Steve Henson] *) Remove C++ style comments from crypto/bn/bn_local.h. [Neil Costigan ] *) The function OBJ_txt2nid was broken. It was supposed to return a nid based on a text string, looking up short and long names and finally "dot" format. The "dot" format stuff didn't work. Added new function OBJ_txt2obj to do the same but return an ASN1_OBJECT and rewrote OBJ_txt2nid to use it. OBJ_txt2obj can also return objects even if the OID is not part of the table. [Steve Henson] *) Add prototypes to X509 lookup/verify methods, fixing a bug in X509_LOOKUP_by_alias(). [Ben Laurie] *) Sort openssl functions by name. [Ben Laurie] *) Get the gendsa program working (hopefully) and add it to app list. Remove encryption from sample DSA keys (in case anyone is interested the password was "1234"). [Steve Henson] *) Make _all_ *_free functions accept a NULL pointer. [Frans Heymans ] *) If a DH key is generated in s3_srvr.c, don't blow it by trying to use NULL pointers. [Anonymous ] *) s_server should send the CAfile as acceptable CAs, not its own cert. [Bodo Moeller <3moeller@informatik.uni-hamburg.de>] *) Don't blow it for numeric -newkey arguments to apps/req. [Bodo Moeller <3moeller@informatik.uni-hamburg.de>] *) Temp key "for export" tests were wrong in s3_srvr.c. [Anonymous ] *) Add prototype for temp key callback functions SSL_CTX_set_tmp_{rsa,dh}_callback(). [Ben Laurie] *) Make DH_free() tolerate being passed a NULL pointer (like RSA_free() and DSA_free()). Make X509_PUBKEY_set() check for errors in d2i_PublicKey(). [Steve Henson] *) X509_name_add_entry() freed the wrong thing after an error. [Arne Ansper ] *) rsa_eay.c would attempt to free a NULL context. [Arne Ansper ] *) BIO_s_socket() had a broken should_retry() on Windoze. [Arne Ansper ] *) BIO_f_buffer() didn't pass on BIO_CTRL_FLUSH. [Arne Ansper ] *) Make sure the already existing X509_STORE->depth variable is initialized in X509_STORE_new(), but document the fact that this variable is still unused in the certificate verification process. [Ralf S. Engelschall] *) Fix the various library and apps files to free up pkeys obtained from X509_PUBKEY_get() et al. Also allow x509.c to handle netscape extensions. [Steve Henson] *) Fix reference counting in X509_PUBKEY_get(). This makes demos/maurice/example2.c work, amongst others, probably. [Steve Henson and Ben Laurie] *) First cut of a cleanup for apps/. First the `ssleay' program is now named `openssl' and second, the shortcut symlinks for the `openssl ' are no longer created. This way we have a single and consistent command line interface `openssl ', similar to `cvs '. [Ralf S. Engelschall, Paul Sutton and Ben Laurie] *) ca.c: move test for DSA keys inside #ifndef NO_DSA. Make pubkey BIT STRING wrapper always have zero unused bits. [Steve Henson] *) Add CA.pl, perl version of CA.sh, add extended key usage OID. [Steve Henson] *) Make the top-level INSTALL documentation easier to understand. [Paul Sutton] *) Makefiles updated to exit if an error occurs in a sub-directory make (including if user presses ^C) [Paul Sutton] *) Make Montgomery context stuff explicit in RSA data structure. [Ben Laurie] *) Fix build order of pem and err to allow for generated pem.h. [Ben Laurie] *) Fix renumbering bug in X509_NAME_delete_entry(). [Ben Laurie] *) Enhanced the err-ins.pl script so it makes the error library number global and can add a library name. This is needed for external ASN1 and other error libraries. [Steve Henson] *) Fixed sk_insert which never worked properly. [Steve Henson] *) Fix ASN1 macros so they can handle indefinite length construted EXPLICIT tags. Some non standard certificates use these: they can now be read in. [Steve Henson] *) Merged the various old/obsolete SSLeay documentation files (doc/xxx.doc) into a single doc/ssleay.txt bundle. This way the information is still preserved but no longer messes up this directory. Now it's new room for the new set of documenation files. [Ralf S. Engelschall] *) SETs were incorrectly DER encoded. This was a major pain, because they shared code with SEQUENCEs, which aren't coded the same. This means that almost everything to do with SETs or SEQUENCEs has either changed name or number of arguments. [Ben Laurie, based on a partial fix by GP Jayan ] *) Fix test data to work with the above. [Ben Laurie] *) Fix the RSA header declarations that hid a bug I fixed in 0.9.0b but was already fixed by Eric for 0.9.1 it seems. [Ben Laurie - pointed out by Ulf Möller ] *) Autodetect FreeBSD3. [Ben Laurie] *) Fix various bugs in Configure. This affects the following platforms: nextstep ncr-scde unixware-2.0 unixware-2.0-pentium sco5-cc. [Ben Laurie] *) Eliminate generated files from CVS. Reorder tests to regenerate files before they are needed. [Ben Laurie] *) Generate Makefile.ssl from Makefile.org (to keep CVS happy). [Ben Laurie] Changes between 0.9.1b and 0.9.1c [23-Dec-1998] *) Added OPENSSL_VERSION_NUMBER to crypto/crypto.h and changed SSLeay to OpenSSL in version strings. [Ralf S. Engelschall] *) Some fixups to the top-level documents. [Paul Sutton] *) Fixed the nasty bug where rsaref.h was not found under compile-time because the symlink to include/ was missing. [Ralf S. Engelschall] *) Incorporated the popular no-RSA/DSA-only patches which allow to compile a RSA-free SSLeay. [Andrew Cooke / Interrader Ldt., Ralf S. Engelschall] *) Fixed nasty rehash problem under `make -f Makefile.ssl links' when "ssleay" is still not found. [Ralf S. Engelschall] *) Added more platforms to Configure: Cray T3E, HPUX 11, [Ralf S. Engelschall, Beckmann ] *) Updated the README file. [Ralf S. Engelschall] *) Added various .cvsignore files in the CVS repository subdirs to make a "cvs update" really silent. [Ralf S. Engelschall] *) Recompiled the error-definition header files and added missing symbols to the Win32 linker tables. [Ralf S. Engelschall] *) Cleaned up the top-level documents; o new files: CHANGES and LICENSE o merged VERSION, HISTORY* and README* files a CHANGES.SSLeay o merged COPYRIGHT into LICENSE o removed obsolete TODO file o renamed MICROSOFT to INSTALL.W32 [Ralf S. Engelschall] *) Removed dummy files from the 0.9.1b source tree: crypto/asn1/x crypto/bio/cd crypto/bio/fg crypto/bio/grep crypto/bio/vi crypto/bn/asm/......add.c crypto/bn/asm/a.out crypto/dsa/f crypto/md5/f crypto/pem/gmon.out crypto/perlasm/f crypto/pkcs7/build crypto/rsa/f crypto/sha/asm/f crypto/threads/f ms/zzz ssl/f ssl/f.mak test/f util/f.mak util/pl/f util/pl/f.mak crypto/bf/bf_locl.old apps/f [Ralf S. Engelschall] *) Added various platform portability fixes. [Mark J. Cox] *) The Genesis of the OpenSSL rpject: We start with the latest (unreleased) SSLeay version 0.9.1b which Eric A. Young and Tim J. Hudson created while they were working for C2Net until summer 1998. [The OpenSSL Project] Changes between 0.9.0b and 0.9.1b [not released] *) Updated a few CA certificates under certs/ [Eric A. Young] *) Changed some BIGNUM api stuff. [Eric A. Young] *) Various platform ports: OpenBSD, Ultrix, IRIX 64bit, NetBSD, DGUX x86, Linux Alpha, etc. [Eric A. Young] *) New COMP library [crypto/comp/] for SSL Record Layer Compression: RLE (dummy implemented) and ZLIB (really implemented when ZLIB is available). [Eric A. Young] *) Add -strparse option to asn1pars program which parses nested binary structures [Dr Stephen Henson ] *) Added "oid_file" to ssleay.cnf for "ca" and "req" programs. [Eric A. Young] *) DSA fix for "ca" program. [Eric A. Young] *) Added "-genkey" option to "dsaparam" program. [Eric A. Young] *) Added RIPE MD160 (rmd160) message digest. [Eric A. Young] *) Added -a (all) option to "ssleay version" command. [Eric A. Young] *) Added PLATFORM define which is the id given to Configure. [Eric A. Young] *) Added MemCheck_XXXX functions to crypto/mem.c for memory checking. [Eric A. Young] *) Extended the ASN.1 parser routines. [Eric A. Young] *) Extended BIO routines to support REUSEADDR, seek, tell, etc. [Eric A. Young] *) Added a BN_CTX to the BN library. [Eric A. Young] *) Fixed the weak key values in DES library [Eric A. Young] *) Changed API in EVP library for cipher aliases. [Eric A. Young] *) Added support for RC2/64bit cipher. [Eric A. Young] *) Converted the lhash library to the crypto/mem.c functions. [Eric A. Young] *) Added more recognized ASN.1 object ids. [Eric A. Young] *) Added more RSA padding checks for SSL/TLS. [Eric A. Young] *) Added BIO proxy/filter functionality. [Eric A. Young] *) Added extra_certs to SSL_CTX which can be used send extra CA certificates to the client in the CA cert chain sending process. It can be configured with SSL_CTX_add_extra_chain_cert(). [Eric A. Young] *) Now Fortezza is denied in the authentication phase because this is key exchange mechanism is not supported by SSLeay at all. [Eric A. Young] *) Additional PKCS1 checks. [Eric A. Young] *) Support the string "TLSv1" for all TLS v1 ciphers. [Eric A. Young] *) Added function SSL_get_ex_data_X509_STORE_CTX_idx() which gives the ex_data index of the SSL context in the X509_STORE_CTX ex_data. [Eric A. Young] *) Fixed a few memory leaks. [Eric A. Young] *) Fixed various code and comment typos. [Eric A. Young] *) A minor bug in ssl/s3_clnt.c where there would always be 4 0 bytes sent in the client random. [Edward Bishop ] Index: vendor-crypto/openssl/dist/Configure =================================================================== --- vendor-crypto/openssl/dist/Configure (revision 296272) +++ vendor-crypto/openssl/dist/Configure (revision 296273) @@ -1,2280 +1,2286 @@ : eval 'exec perl -S $0 ${1+"$@"}' if $running_under_some_shell; ## ## Configure -- OpenSSL source tree configuration script ## require 5.000; use strict; # see INSTALL for instructions. my $usage="Usage: Configure [no- ...] [enable- ...] [experimental- ...] [-Dxxx] [-lxxx] [-Lxxx] [-fxxx] [-Kxxx] [no-hw-xxx|no-hw] [[no-]threads] [[no-]shared] [[no-]zlib|zlib-dynamic] [no-asm] [no-dso] [no-krb5] [sctp] [386] [--prefix=DIR] [--openssldir=OPENSSLDIR] [--with-xxx[=vvv]] [--test-sanity] os/compiler[:flags]\n"; # Options: # # --openssldir install OpenSSL in OPENSSLDIR (Default: DIR/ssl if the # --prefix option is given; /usr/local/ssl otherwise) # --prefix prefix for the OpenSSL include, lib and bin directories # (Default: the OPENSSLDIR directory) # # --install_prefix Additional prefix for package builders (empty by # default). This needn't be set in advance, you can # just as well use "make INSTALL_PREFIX=/whatever install". # # --with-krb5-dir Declare where Kerberos 5 lives. The libraries are expected # to live in the subdirectory lib/ and the header files in # include/. A value is required. # --with-krb5-lib Declare where the Kerberos 5 libraries live. A value is # required. # (Default: KRB5_DIR/lib) # --with-krb5-include Declare where the Kerberos 5 header files live. A # value is required. # (Default: KRB5_DIR/include) # --with-krb5-flavor Declare what flavor of Kerberos 5 is used. Currently # supported values are "MIT" and "Heimdal". A value is required. # # --test-sanity Make a number of sanity checks on the data in this file. # This is a debugging tool for OpenSSL developers. # # --cross-compile-prefix Add specified prefix to binutils components. # # no-hw-xxx do not compile support for specific crypto hardware. # Generic OpenSSL-style methods relating to this support # are always compiled but return NULL if the hardware # support isn't compiled. # no-hw do not compile support for any crypto hardware. # [no-]threads [don't] try to create a library that is suitable for # multithreaded applications (default is "threads" if we # know how to do it) # [no-]shared [don't] try to create shared libraries when supported. # no-asm do not use assembler # no-dso do not compile in any native shared-library methods. This # will ensure that all methods just return NULL. # no-krb5 do not compile in any KRB5 library or code. # [no-]zlib [don't] compile support for zlib compression. # zlib-dynamic Like "zlib", but the zlib library is expected to be a shared # library and will be loaded in run-time by the OpenSSL library. # sctp include SCTP support # 386 generate 80386 code +# enable-weak-ssl-ciphers +# Enable EXPORT and LOW SSLv3 ciphers that are disabled by +# default. Note, weak SSLv2 ciphers are unconditionally +# disabled. # no-sse2 disables IA-32 SSE2 code, above option implies no-sse2 # no- build without specified algorithm (rsa, idea, rc5, ...) # - + compiler options are passed through # # DEBUG_SAFESTACK use type-safe stacks to enforce type-safety on stack items # provided to stack calls. Generates unique stack functions for # each possible stack type. # DES_PTR use pointer lookup vs arrays in the DES in crypto/des/des_locl.h # DES_RISC1 use different DES_ENCRYPT macro that helps reduce register # dependancies but needs to more registers, good for RISC CPU's # DES_RISC2 A different RISC variant. # DES_UNROLL unroll the inner DES loop, sometimes helps, somtimes hinders. # DES_INT use 'int' instead of 'long' for DES_LONG in crypto/des/des.h # This is used on the DEC Alpha where long is 8 bytes # and int is 4 # BN_LLONG use the type 'long long' in crypto/bn/bn.h # MD2_CHAR use 'char' instead of 'int' for MD2_INT in crypto/md2/md2.h # MD2_LONG use 'long' instead of 'int' for MD2_INT in crypto/md2/md2.h # IDEA_SHORT use 'short' instead of 'int' for IDEA_INT in crypto/idea/idea.h # IDEA_LONG use 'long' instead of 'int' for IDEA_INT in crypto/idea/idea.h # RC2_SHORT use 'short' instead of 'int' for RC2_INT in crypto/rc2/rc2.h # RC2_LONG use 'long' instead of 'int' for RC2_INT in crypto/rc2/rc2.h # RC4_CHAR use 'char' instead of 'int' for RC4_INT in crypto/rc4/rc4.h # RC4_LONG use 'long' instead of 'int' for RC4_INT in crypto/rc4/rc4.h # RC4_INDEX define RC4_INDEX in crypto/rc4/rc4_locl.h. This turns on # array lookups instead of pointer use. # RC4_CHUNK enables code that handles data aligned at long (natural CPU # word) boundary. # RC4_CHUNK_LL enables code that handles data aligned at long long boundary # (intended for 64-bit CPUs running 32-bit OS). # BF_PTR use 'pointer arithmatic' for Blowfish (unsafe on Alpha). # BF_PTR2 intel specific version (generic version is more efficient). # # Following are set automatically by this script # # MD5_ASM use some extra md5 assember, # SHA1_ASM use some extra sha1 assember, must define L_ENDIAN for x86 # RMD160_ASM use some extra ripemd160 assember, # SHA256_ASM sha256_block is implemented in assembler # SHA512_ASM sha512_block is implemented in assembler # AES_ASM ASE_[en|de]crypt is implemented in assembler # Minimum warning options... any contributions to OpenSSL should at least get # past these. my $gcc_devteam_warn = "-Wall -pedantic -DPEDANTIC -Wno-long-long -Wsign-compare -Wmissing-prototypes -Wshadow -Wformat -Werror -DCRYPTO_MDEBUG_ALL -DCRYPTO_MDEBUG_ABORT -DREF_CHECK -DOPENSSL_NO_DEPRECATED"; # TODO(openssl-team): fix problems and investigate if (at least) the following # warnings can also be enabled: # -Wconditional-uninitialized, -Wswitch-enum, -Wunused-macros, # -Wmissing-field-initializers, -Wmissing-variable-declarations, # -Wincompatible-pointer-types-discards-qualifiers, -Wcast-align, # -Wunreachable-code -Wunused-parameter -Wlanguage-extension-token # -Wextended-offsetof my $clang_disabled_warnings = "-Wno-unused-parameter -Wno-missing-field-initializers -Wno-language-extension-token -Wno-extended-offsetof"; # These are used in addition to $gcc_devteam_warn when the compiler is clang. # TODO(openssl-team): fix problems and investigate if (at least) the # following warnings can also be enabled: -Wconditional-uninitialized, # -Wswitch-enum, -Wunused-macros, -Wmissing-field-initializers, # -Wmissing-variable-declarations, # -Wincompatible-pointer-types-discards-qualifiers, -Wcast-align, # -Wunreachable-code -Wunused-parameter -Wlanguage-extension-token # -Wextended-offsetof my $clang_devteam_warn = "-Wno-unused-parameter -Wno-missing-field-initializers -Wno-language-extension-token -Wno-extended-offsetof -Qunused-arguments"; # Warn that "make depend" should be run? my $warn_make_depend = 0; my $strict_warnings = 0; my $x86_gcc_des="DES_PTR DES_RISC1 DES_UNROLL"; # MD2_CHAR slags pentium pros my $x86_gcc_opts="RC4_INDEX MD2_INT"; # MODIFY THESE PARAMETERS IF YOU ARE GOING TO USE THE 'util/speed.sh SCRIPT # Don't worry about these normally my $tcc="cc"; my $tflags="-fast -Xa"; my $tbn_mul=""; my $tlib="-lnsl -lsocket"; #$bits1="SIXTEEN_BIT "; #$bits2="THIRTY_TWO_BIT "; my $bits1="THIRTY_TWO_BIT "; my $bits2="SIXTY_FOUR_BIT "; my $x86_asm="x86cpuid.o:bn-586.o co-586.o x86-mont.o x86-gf2m.o::des-586.o crypt586.o:aes-586.o vpaes-x86.o aesni-x86.o:bf-586.o:md5-586.o:sha1-586.o sha256-586.o sha512-586.o:cast-586.o:rc4-586.o:rmd-586.o:rc5-586.o:wp_block.o wp-mmx.o:cmll-x86.o:ghash-x86.o:"; my $x86_elf_asm="$x86_asm:elf"; my $x86_64_asm="x86_64cpuid.o:x86_64-gcc.o x86_64-mont.o x86_64-mont5.o x86_64-gf2m.o rsaz_exp.o rsaz-x86_64.o rsaz-avx2.o:ecp_nistz256.o ecp_nistz256-x86_64.o::aes-x86_64.o vpaes-x86_64.o bsaes-x86_64.o aesni-x86_64.o aesni-sha1-x86_64.o aesni-sha256-x86_64.o aesni-mb-x86_64.o::md5-x86_64.o:sha1-x86_64.o sha256-x86_64.o sha512-x86_64.o sha1-mb-x86_64.o sha256-mb-x86_64.o::rc4-x86_64.o rc4-md5-x86_64.o:::wp-x86_64.o:cmll-x86_64.o cmll_misc.o:ghash-x86_64.o aesni-gcm-x86_64.o:"; my $ia64_asm="ia64cpuid.o:bn-ia64.o ia64-mont.o:::aes_core.o aes_cbc.o aes-ia64.o::md5-ia64.o:sha1-ia64.o sha256-ia64.o sha512-ia64.o::rc4-ia64.o rc4_skey.o:::::ghash-ia64.o::void"; my $sparcv9_asm="sparcv9cap.o sparccpuid.o:bn-sparcv9.o sparcv9-mont.o sparcv9a-mont.o vis3-mont.o sparct4-mont.o sparcv9-gf2m.o::des_enc-sparc.o fcrypt_b.o dest4-sparcv9.o:aes_core.o aes_cbc.o aes-sparcv9.o aest4-sparcv9.o::md5-sparcv9.o:sha1-sparcv9.o sha256-sparcv9.o sha512-sparcv9.o::::::camellia.o cmll_misc.o cmll_cbc.o cmllt4-sparcv9.o:ghash-sparcv9.o::void"; my $sparcv8_asm=":sparcv8.o::des_enc-sparc.o fcrypt_b.o:::::::::::::void"; my $alpha_asm="alphacpuid.o:bn_asm.o alpha-mont.o::::::sha1-alpha.o:::::::ghash-alpha.o::void"; my $mips64_asm=":bn-mips.o mips-mont.o:::aes_cbc.o aes-mips.o:::sha1-mips.o sha256-mips.o sha512-mips.o::::::::"; my $mips32_asm=$mips64_asm; $mips32_asm =~ s/\s*sha512\-mips\.o//; my $s390x_asm="s390xcap.o s390xcpuid.o:bn-s390x.o s390x-mont.o s390x-gf2m.o:::aes-s390x.o aes-ctr.o aes-xts.o:::sha1-s390x.o sha256-s390x.o sha512-s390x.o::rc4-s390x.o:::::ghash-s390x.o:"; my $armv4_asm="armcap.o armv4cpuid.o:bn_asm.o armv4-mont.o armv4-gf2m.o:::aes_cbc.o aes-armv4.o bsaes-armv7.o aesv8-armx.o:::sha1-armv4-large.o sha256-armv4.o sha512-armv4.o:::::::ghash-armv4.o ghashv8-armx.o::void"; my $aarch64_asm="armcap.o arm64cpuid.o mem_clr.o::::aes_core.o aes_cbc.o aesv8-armx.o:::sha1-armv8.o sha256-armv8.o sha512-armv8.o:::::::ghashv8-armx.o:"; my $parisc11_asm="pariscid.o:bn_asm.o parisc-mont.o:::aes_core.o aes_cbc.o aes-parisc.o:::sha1-parisc.o sha256-parisc.o sha512-parisc.o::rc4-parisc.o:::::ghash-parisc.o::32"; my $parisc20_asm="pariscid.o:pa-risc2W.o parisc-mont.o:::aes_core.o aes_cbc.o aes-parisc.o:::sha1-parisc.o sha256-parisc.o sha512-parisc.o::rc4-parisc.o:::::ghash-parisc.o::64"; my $ppc64_asm="ppccpuid.o ppccap.o:bn-ppc.o ppc-mont.o ppc64-mont.o:::aes_core.o aes_cbc.o aes-ppc.o vpaes-ppc.o aesp8-ppc.o:::sha1-ppc.o sha256-ppc.o sha512-ppc.o sha256p8-ppc.o sha512p8-ppc.o:::::::ghashp8-ppc.o:"; my $ppc32_asm=$ppc64_asm; my $no_asm="::::::::::::::::void"; # As for $BSDthreads. Idea is to maintain "collective" set of flags, # which would cover all BSD flavors. -pthread applies to them all, # but is treated differently. OpenBSD expands is as -D_POSIX_THREAD # -lc_r, which is sufficient. FreeBSD 4.x expands it as -lc_r, # which has to be accompanied by explicit -D_THREAD_SAFE and # sometimes -D_REENTRANT. FreeBSD 5.x expands it as -lc_r, which # seems to be sufficient? my $BSDthreads="-pthread -D_THREAD_SAFE -D_REENTRANT"; #config-string $cc : $cflags : $unistd : $thread_cflag : $sys_id : $lflags : $bn_ops : $cpuid_obj : $bn_obj : $ec_obj : $des_obj : $aes_obj : $bf_obj : $md5_obj : $sha1_obj : $cast_obj : $rc4_obj : $rmd160_obj : $rc5_obj : $wp_obj : $cmll_obj : $modes_obj : $engines_obj : $dso_scheme : $shared_target : $shared_cflag : $shared_ldflag : $shared_extension : $ranlib : $arflags : $multilib my %table=( # File 'TABLE' (created by 'make TABLE') contains the data from this list, # formatted for better readability. #"b", "${tcc}:${tflags}::${tlib}:${bits1}:${tbn_mul}::", #"bl-4c-2c", "${tcc}:${tflags}::${tlib}:${bits1}BN_LLONG RC4_CHAR MD2_CHAR:${tbn_mul}::", #"bl-4c-ri", "${tcc}:${tflags}::${tlib}:${bits1}BN_LLONG RC4_CHAR RC4_INDEX:${tbn_mul}::", #"b2-is-ri-dp", "${tcc}:${tflags}::${tlib}:${bits2}IDEA_SHORT RC4_INDEX DES_PTR:${tbn_mul}::", # Our development configs "purify", "purify gcc:-g -DPURIFY -Wall::(unknown)::-lsocket -lnsl::::", "debug", "gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DOPENSSL_NO_ASM -ggdb -g2 -Wformat -Wshadow -Wmissing-prototypes -Wmissing-declarations -Werror::(unknown)::-lefence::::", "debug-ben", "gcc:$gcc_devteam_warn -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DDEBUG_SAFESTACK -O2 -pipe::(unknown):::::", "debug-ben-openbsd","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DPEDANTIC -DDEBUG_SAFESTACK -DOPENSSL_OPENBSD_DEV_CRYPTO -DOPENSSL_NO_ASM -O2 -pedantic -Wall -Wshadow -Werror -pipe::(unknown)::::", "debug-ben-openbsd-debug","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DPEDANTIC -DDEBUG_SAFESTACK -DOPENSSL_OPENBSD_DEV_CRYPTO -DOPENSSL_NO_ASM -g3 -O2 -pedantic -Wall -Wshadow -Werror -pipe::(unknown)::::", "debug-ben-debug", "gcc44:$gcc_devteam_warn -DBN_DEBUG -DCONF_DEBUG -DDEBUG_SAFESTACK -DDEBUG_UNUSED -g3 -O2 -pipe::(unknown)::::::", "debug-ben-debug-64", "gcc:$gcc_devteam_warn -Wno-error=overlength-strings -DBN_DEBUG -DCONF_DEBUG -DDEBUG_SAFESTACK -DDEBUG_UNUSED -g3 -O3 -pipe::${BSDthreads}:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-ben-macos", "cc:$gcc_devteam_warn -arch i386 -DBN_DEBUG -DCONF_DEBUG -DDEBUG_SAFESTACK -DDEBUG_UNUSED -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -O3 -DL_ENDIAN -g3 -pipe::(unknown)::-Wl,-search_paths_first::::", "debug-ben-macos-gcc46", "gcc-mp-4.6:$gcc_devteam_warn -Wconversion -DBN_DEBUG -DCONF_DEBUG -DDEBUG_SAFESTACK -DDEBUG_UNUSED -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -O3 -DL_ENDIAN -g3 -pipe::(unknown)::::::", "debug-ben-darwin64","cc:$gcc_devteam_warn -g -Wno-language-extension-token -Wno-extended-offsetof -arch x86_64 -O3 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "debug-ben-debug-64-clang", "clang:$gcc_devteam_warn -Wno-error=overlength-strings -Wno-error=extended-offsetof -Qunused-arguments -DBN_DEBUG -DCONF_DEBUG -DDEBUG_SAFESTACK -DDEBUG_UNUSED -g3 -O3 -pipe::${BSDthreads}:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-ben-no-opt", "gcc: -Wall -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations -DDEBUG_SAFESTACK -DCRYPTO_MDEBUG -Werror -DL_ENDIAN -DTERMIOS -Wall -g3::(unknown)::::::", "debug-ben-strict", "gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DCONST_STRICT -O2 -Wall -Wshadow -Werror -Wpointer-arith -Wcast-qual -Wwrite-strings -pipe::(unknown)::::::", "debug-rse","cc:-DTERMIOS -DL_ENDIAN -pipe -O -g -ggdb3 -Wall::(unknown):::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}", "debug-bodo", "gcc:$gcc_devteam_warn -Wno-error=overlength-strings -DBN_DEBUG -DBN_DEBUG_RAND -DCONF_DEBUG -DBIO_PAIR_DEBUG -m64 -DL_ENDIAN -DTERMIO -g -DMD32_REG_T=int::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "debug-steve64", "gcc:$gcc_devteam_warn -m64 -DL_ENDIAN -DTERMIO -DCONF_DEBUG -DDEBUG_SAFESTACK -Wno-overlength-strings -g::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-steve32", "gcc:$gcc_devteam_warn -m32 -DL_ENDIAN -DCONF_DEBUG -DDEBUG_SAFESTACK -Wno-overlength-strings -g -pipe::-D_REENTRANT::-rdynamic -ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC:-m32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-steve-opt", "gcc:$gcc_devteam_warn -m64 -O3 -DL_ENDIAN -DTERMIO -DCONF_DEBUG -DDEBUG_SAFESTACK -g::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-levitte-linux-elf","gcc:-DLEVITTE_DEBUG -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -DL_ENDIAN -ggdb -g3 -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-levitte-linux-noasm","gcc:-DLEVITTE_DEBUG -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -DOPENSSL_NO_ASM -DL_ENDIAN -ggdb -g3 -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-levitte-linux-elf-extreme","gcc:-DLEVITTE_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_DEBUG -DBN_DEBUG_RAND -DCRYPTO_MDEBUG -DENGINE_CONF_DEBUG -DL_ENDIAN -DPEDANTIC -ggdb -g3 -pedantic -ansi -Wall -W -Wundef -Wshadow -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wno-long-long -Wundef -Wconversion -pipe::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-levitte-linux-noasm-extreme","gcc:-DLEVITTE_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_DEBUG -DBN_DEBUG_RAND -DCRYPTO_MDEBUG -DENGINE_CONF_DEBUG -DOPENSSL_NO_ASM -DL_ENDIAN -DPEDANTIC -ggdb -g3 -pedantic -ansi -Wall -W -Wundef -Wshadow -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wno-long-long -Wundef -Wconversion -pipe::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-geoff32","gcc:-DBN_DEBUG -DBN_DEBUG_RAND -DBN_STRICT -DPURIFY -DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_ASM -DOPENSSL_NO_INLINE_ASM -DL_ENDIAN -DTERMIO -DPEDANTIC -O1 -ggdb2 -Wall -Werror -Wundef -pedantic -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-align -Wsign-compare -Wmissing-prototypes -Wmissing-declarations -Wno-long-long::-D_REENTRANT::-ldl:BN_LLONG:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-geoff64","gcc:-DBN_DEBUG -DBN_DEBUG_RAND -DBN_STRICT -DPURIFY -DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_ASM -DOPENSSL_NO_INLINE_ASM -DL_ENDIAN -DTERMIO -DPEDANTIC -O1 -ggdb2 -Wall -Werror -Wundef -pedantic -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-align -Wsign-compare -Wmissing-prototypes -Wmissing-declarations -Wno-long-long::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-pentium","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DL_ENDIAN -g -mcpu=pentium -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn", "debug-linux-ppro","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DL_ENDIAN -g -mcpu=pentiumpro -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn", "debug-linux-elf","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DL_ENDIAN -g -march=i486 -Wall::-D_REENTRANT::-lefence -ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-elf-noefence","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DL_ENDIAN -g -march=i486 -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-ia32-aes", "gcc:-DAES_EXPERIMENTAL -DL_ENDIAN -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:x86cpuid.o:bn-586.o co-586.o x86-mont.o::des-586.o crypt586.o:aes_x86core.o aes_cbc.o aesni-x86.o:bf-586.o:md5-586.o:sha1-586.o sha256-586.o sha512-586.o:cast-586.o:rc4-586.o:rmd-586.o:rc5-586.o:wp_block.o wp-mmx.o::ghash-x86.o::elf:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-generic32","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -g -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-generic64","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -DTERMIO -g -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-linux-x86_64","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -m64 -DL_ENDIAN -g -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "dist", "cc:-O::(unknown)::::::", # Basic configs that should work on any (32 and less bit) box "gcc", "gcc:-O3::(unknown):::BN_LLONG:::", "cc", "cc:-O::(unknown)::::::", ####VOS Configurations "vos-gcc","gcc:-O3 -Wall -DOPENSSL_SYSNAME_VOS -D_POSIX_C_SOURCE=200112L -D_BSD -D_VOS_EXTENDED_NAMES -DB_ENDIAN::(unknown):VOS:-Wl,-map:BN_LLONG:${no_asm}:::::.so:", "debug-vos-gcc","gcc:-O0 -g -Wall -DOPENSSL_SYSNAME_VOS -D_POSIX_C_SOURCE=200112L -D_BSD -D_VOS_EXTENDED_NAMES -DB_ENDIAN -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG::(unknown):VOS:-Wl,-map:BN_LLONG:${no_asm}:::::.so:", #### Solaris x86 with GNU C setups # -DOPENSSL_NO_INLINE_ASM switches off inline assembler. We have to do it # here because whenever GNU C instantiates an assembler template it # surrounds it with #APP #NO_APP comment pair which (at least Solaris # 7_x86) /usr/ccs/bin/as fails to assemble with "Illegal mnemonic" # error message. "solaris-x86-gcc","gcc:-O3 -fomit-frame-pointer -march=pentium -Wall -DL_ENDIAN -DOPENSSL_NO_INLINE_ASM::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # -shared -static-libgcc might appear controversial, but modules taken # from static libgcc do not have relocations and linking them into our # shared objects doesn't have any negative side-effects. On the contrary, # doing so makes it possible to use gcc shared build with Sun C. Given # that gcc generates faster code [thanks to inline assembler], I would # actually recommend to consider using gcc shared build even with vendor # compiler:-) # "solaris64-x86_64-gcc","gcc:-m64 -O3 -Wall -DL_ENDIAN::-D_REENTRANT::-lsocket -lnsl -ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:solaris-shared:-fPIC:-m64 -shared -static-libgcc:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/64", #### Solaris x86 with Sun C setups "solaris-x86-cc","cc:-fast -xarch=generic -O -Xa::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_UNROLL BF_PTR:${no_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris64-x86_64-cc","cc:-fast -xarch=amd64 -xstrconst -Xa -DL_ENDIAN::-D_REENTRANT::-lsocket -lnsl -ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:solaris-shared:-KPIC:-xarch=amd64 -G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/64", #### SPARC Solaris with GNU C setups "solaris-sparcv7-gcc","gcc:-O3 -fomit-frame-pointer -Wall -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${no_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris-sparcv8-gcc","gcc:-mcpu=v8 -O3 -fomit-frame-pointer -Wall -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv8_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # -m32 should be safe to add as long as driver recognizes -mcpu=ultrasparc "solaris-sparcv9-gcc","gcc:-m32 -mcpu=ultrasparc -O3 -fomit-frame-pointer -Wall -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris64-sparcv9-gcc","gcc:-m64 -mcpu=ultrasparc -O3 -Wall -DB_ENDIAN::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-fPIC:-m64 -shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/64", #### "debug-solaris-sparcv8-gcc","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG_ALL -O -g -mcpu=v8 -Wall -DB_ENDIAN::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv8_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-solaris-sparcv9-gcc","gcc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG_ALL -DPEDANTIC -O -g -mcpu=ultrasparc -pedantic -ansi -Wall -Wshadow -Wno-long-long -D__EXTENSIONS__ -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### SPARC Solaris with Sun C setups # SC4.0 doesn't pass 'make test', upgrade to SC5.0 or SC4.2. # SC4.2 is ok, better than gcc even on bn as long as you tell it -xarch=v8 # SC5.0 note: Compiler common patch 107357-01 or later is required! "solaris-sparcv7-cc","cc:-xO5 -xstrconst -xdepend -Xa -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${no_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris-sparcv8-cc","cc:-xarch=v8 -xO5 -xstrconst -xdepend -Xa -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv8_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris-sparcv9-cc","cc:-xtarget=ultra -xarch=v8plus -xO5 -xstrconst -xdepend -Xa -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK_LL DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "solaris64-sparcv9-cc","cc:-xtarget=ultra -xarch=v9 -xO5 -xstrconst -xdepend -Xa -DB_ENDIAN::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-KPIC:-xarch=v9 -G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/64", #### "debug-solaris-sparcv8-cc","cc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG_ALL -xarch=v8 -g -O -xstrconst -Xa -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv8_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-solaris-sparcv9-cc","cc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG_ALL -xtarget=ultra -xarch=v8plus -g -O -xstrconst -Xa -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT:ULTRASPARC:-lsocket -lnsl -ldl:BN_LLONG RC4_CHAR RC4_CHUNK_LL DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:solaris-shared:-KPIC:-G -dy -z text:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### SunOS configs, assuming sparc for the gcc one. #"sunos-cc", "cc:-O4 -DNOPROTO -DNOCONST::(unknown):SUNOS::DES_UNROLL:${no_asm}::", "sunos-gcc","gcc:-O3 -mcpu=v8 -Dssize_t=int::(unknown):SUNOS::BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL DES_PTR DES_RISC1:${no_asm}::", #### IRIX 5.x configs # -mips2 flag is added by ./config when appropriate. "irix-gcc","gcc:-O3 -DB_ENDIAN::(unknown):::BN_LLONG MD2_CHAR RC4_INDEX RC4_CHAR RC4_CHUNK DES_UNROLL DES_RISC2 DES_PTR BF_PTR:${mips32_asm}:o32:dlfcn:irix-shared:::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "irix-cc", "cc:-O2 -use_readonly_const -DB_ENDIAN::(unknown):::BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_RISC2 DES_UNROLL BF_PTR:${mips32_asm}:o32:dlfcn:irix-shared:::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### IRIX 6.x configs # Only N32 and N64 ABIs are supported. If you need O32 ABI build, invoke # './Configure irix-cc -o32' manually. "irix-mips3-gcc","gcc:-mabi=n32 -O3 -DB_ENDIAN -DBN_DIV3W::-D_SGI_MP_SOURCE:::MD2_CHAR RC4_INDEX RC4_CHAR RC4_CHUNK_LL DES_UNROLL DES_RISC2 DES_PTR BF_PTR SIXTY_FOUR_BIT:${mips64_asm}:n32:dlfcn:irix-shared::-mabi=n32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::32", "irix-mips3-cc", "cc:-n32 -mips3 -O2 -use_readonly_const -G0 -rdata_shared -DB_ENDIAN -DBN_DIV3W::-D_SGI_MP_SOURCE:::DES_PTR RC4_CHAR RC4_CHUNK_LL DES_RISC2 DES_UNROLL BF_PTR SIXTY_FOUR_BIT:${mips64_asm}:n32:dlfcn:irix-shared::-n32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::32", # N64 ABI builds. "irix64-mips4-gcc","gcc:-mabi=64 -mips4 -O3 -DB_ENDIAN -DBN_DIV3W::-D_SGI_MP_SOURCE:::RC4_CHAR RC4_CHUNK DES_RISC2 DES_UNROLL SIXTY_FOUR_BIT_LONG:${mips64_asm}:64:dlfcn:irix-shared::-mabi=64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "irix64-mips4-cc", "cc:-64 -mips4 -O2 -use_readonly_const -G0 -rdata_shared -DB_ENDIAN -DBN_DIV3W::-D_SGI_MP_SOURCE:::RC4_CHAR RC4_CHUNK DES_RISC2 DES_UNROLL SIXTY_FOUR_BIT_LONG:${mips64_asm}:64:dlfcn:irix-shared::-64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", #### Unified HP-UX ANSI C configs. # Special notes: # - Originally we were optimizing at +O4 level. It should be noted # that the only difference between +O3 and +O4 is global inter- # procedural analysis. As it has to be performed during the link # stage the compiler leaves behind certain pseudo-code in lib*.a # which might be release or even patch level specific. Generating # the machine code for and analyzing the *whole* program appears # to be *extremely* memory demanding while the performance gain is # actually questionable. The situation is intensified by the default # HP-UX data set size limit (infamous 'maxdsiz' tunable) of 64MB # which is way too low for +O4. In other words, doesn't +O3 make # more sense? # - Keep in mind that the HP compiler by default generates code # suitable for execution on the host you're currently compiling at. # If the toolkit is ment to be used on various PA-RISC processors # consider './config +DAportable'. # - +DD64 is chosen in favour of +DA2.0W because it's meant to be # compatible with *future* releases. # - If you run ./Configure hpux-parisc-[g]cc manually don't forget to # pass -D_REENTRANT on HP-UX 10 and later. # - -DMD32_XARRAY triggers workaround for compiler bug we ran into in # 32-bit message digests. (For the moment of this writing) HP C # doesn't seem to "digest" too many local variables (they make "him" # chew forever:-). For more details look-up MD32_XARRAY comment in # crypto/sha/sha_lcl.h. # # # Since there is mention of this in shlib/hpux10-cc.sh "hpux-parisc-cc-o4","cc:-Ae +O4 +ESlit -z -DB_ENDIAN -DBN_DIV2W -DMD32_XARRAY::-D_REENTRANT::-ldld:BN_LLONG DES_PTR DES_UNROLL DES_RISC1:${no_asm}:dl:hpux-shared:+Z:-b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "hpux-parisc-gcc","gcc:-O3 -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-Wl,+s -ldld:BN_LLONG DES_PTR DES_UNROLL DES_RISC1:${no_asm}:dl:hpux-shared:-fPIC:-shared:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "hpux-parisc1_1-gcc","gcc:-O3 -DB_ENDIAN -DBN_DIV2W::-D_REENTRANT::-Wl,+s -ldld:BN_LLONG DES_PTR DES_UNROLL DES_RISC1:${parisc11_asm}:dl:hpux-shared:-fPIC:-shared:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa1.1", "hpux-parisc2-gcc","gcc:-march=2.0 -O3 -DB_ENDIAN -D_REENTRANT::::-Wl,+s -ldld:SIXTY_FOUR_BIT RC4_CHAR RC4_CHUNK DES_PTR DES_UNROLL DES_RISC1:".eval{my $asm=$parisc20_asm;$asm=~s/2W\./2\./;$asm=~s/:64/:32/;$asm}.":dl:hpux-shared:-fPIC:-shared:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa20_32", "hpux64-parisc2-gcc","gcc:-O3 -DB_ENDIAN -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT_LONG MD2_CHAR RC4_INDEX RC4_CHAR DES_UNROLL DES_RISC1 DES_INT::pa-risc2W.o:::::::::::::::void:dlfcn:hpux-shared:-fpic:-shared:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa20_64", # More attempts at unified 10.X and 11.X targets for HP C compiler. # # Chris Ruemmler # Kevin Steves "hpux-parisc-cc","cc:+O3 +Optrs_strongly_typed -Ae +ESlit -DB_ENDIAN -DBN_DIV2W -DMD32_XARRAY::-D_REENTRANT::-Wl,+s -ldld:MD2_CHAR RC4_INDEX RC4_CHAR DES_UNROLL DES_RISC1 DES_INT:${no_asm}:dl:hpux-shared:+Z:-b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "hpux-parisc1_1-cc","cc:+DA1.1 +O3 +Optrs_strongly_typed -Ae +ESlit -DB_ENDIAN -DMD32_XARRAY::-D_REENTRANT::-Wl,+s -ldld:MD2_CHAR RC4_INDEX RC4_CHAR DES_UNROLL DES_RISC1 DES_INT:${parisc11_asm}:dl:hpux-shared:+Z:-b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa1.1", "hpux-parisc2-cc","cc:+DA2.0 +DS2.0 +O3 +Optrs_strongly_typed -Ae +ESlit -DB_ENDIAN -DMD32_XARRAY -D_REENTRANT::::-Wl,+s -ldld:SIXTY_FOUR_BIT MD2_CHAR RC4_INDEX RC4_CHAR DES_UNROLL DES_RISC1 DES_INT:".eval{my $asm=$parisc20_asm;$asm=~s/2W\./2\./;$asm=~s/:64/:32/;$asm}.":dl:hpux-shared:+Z:-b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa20_32", "hpux64-parisc2-cc","cc:+DD64 +O3 +Optrs_strongly_typed -Ae +ESlit -DB_ENDIAN -DMD32_XARRAY -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT_LONG MD2_CHAR RC4_INDEX RC4_CHAR DES_UNROLL DES_RISC1 DES_INT:${parisc20_asm}:dlfcn:hpux-shared:+Z:+DD64 -b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/pa20_64", # HP/UX IA-64 targets "hpux-ia64-cc","cc:-Ae +DD32 +O2 +Olit=all -z -DB_ENDIAN -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT MD2_CHAR RC4_INDEX DES_UNROLL DES_RISC1 DES_INT:${ia64_asm}:dlfcn:hpux-shared:+Z:+DD32 -b:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/hpux32", # Frank Geurts has patiently assisted with # with debugging of the following config. "hpux64-ia64-cc","cc:-Ae +DD64 +O3 +Olit=all -z -DB_ENDIAN -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT_LONG MD2_CHAR RC4_INDEX DES_UNROLL DES_RISC1 DES_INT:${ia64_asm}:dlfcn:hpux-shared:+Z:+DD64 -b:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/hpux64", # GCC builds... "hpux-ia64-gcc","gcc:-O3 -DB_ENDIAN -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT MD2_CHAR RC4_INDEX DES_UNROLL DES_RISC1 DES_INT:${ia64_asm}:dlfcn:hpux-shared:-fpic:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/hpux32", "hpux64-ia64-gcc","gcc:-mlp64 -O3 -DB_ENDIAN -D_REENTRANT::::-ldl:SIXTY_FOUR_BIT_LONG MD2_CHAR RC4_INDEX DES_UNROLL DES_RISC1 DES_INT:${ia64_asm}:dlfcn:hpux-shared:-fpic:-mlp64 -shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/hpux64", # Legacy HPUX 9.X configs... "hpux-cc", "cc:-DB_ENDIAN -DBN_DIV2W -DMD32_XARRAY -Ae +ESlit +O2 -z::(unknown)::-Wl,+s -ldld:DES_PTR DES_UNROLL DES_RISC1:${no_asm}:dl:hpux-shared:+Z:-b:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "hpux-gcc", "gcc:-DB_ENDIAN -DBN_DIV2W -O3::(unknown)::-Wl,+s -ldld:DES_PTR DES_UNROLL DES_RISC1:${no_asm}:dl:hpux-shared:-fPIC:-shared:.sl.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### HP MPE/iX http://jazz.external.hp.com/src/openssl/ "MPE/iX-gcc", "gcc:-D_ENDIAN -DBN_DIV2W -O3 -D_POSIX_SOURCE -D_SOCKET_SOURCE -I/SYSLOG/PUB::(unknown):MPE:-L/SYSLOG/PUB -lsyslog -lsocket -lcurses:BN_LLONG DES_PTR DES_UNROLL DES_RISC1:::", # DEC Alpha OSF/1/Tru64 targets. # # "What's in a name? That which we call a rose # By any other word would smell as sweet." # # - William Shakespeare, "Romeo & Juliet", Act II, scene II. # # For gcc, the following gave a %50 speedup on a 164 over the 'DES_INT' version # "osf1-alpha-gcc", "gcc:-O3::(unknown):::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_UNROLL DES_RISC1:${alpha_asm}:dlfcn:alpha-osf1-shared:::.so", "osf1-alpha-cc", "cc:-std1 -tune host -O4 -readonly_strings::(unknown):::SIXTY_FOUR_BIT_LONG RC4_CHUNK:${alpha_asm}:dlfcn:alpha-osf1-shared:::.so", "tru64-alpha-cc", "cc:-std1 -tune host -fast -readonly_strings::-pthread:::SIXTY_FOUR_BIT_LONG RC4_CHUNK:${alpha_asm}:dlfcn:alpha-osf1-shared::-msym:.so", #### #### Variety of LINUX:-) #### # *-generic* is endian-neutral target, but ./config is free to # throw in -D[BL]_ENDIAN, whichever appropriate... "linux-generic32","gcc:-O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-ppc", "gcc:-DB_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_RISC1 DES_UNROLL:${ppc32_asm}:linux32:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", ####################################################################### # Note that -march is not among compiler options in below linux-armv4 # target line. Not specifying one is intentional to give you choice to: # # a) rely on your compiler default by not specifying one; # b) specify your target platform explicitly for optimal performance, # e.g. -march=armv6 or -march=armv7-a; # c) build "universal" binary that targets *range* of platforms by # specifying minimum and maximum supported architecture; # # As for c) option. It actually makes no sense to specify maximum to be # less than ARMv7, because it's the least requirement for run-time # switch between platform-specific code paths. And without run-time # switch performance would be equivalent to one for minimum. Secondly, # there are some natural limitations that you'd have to accept and # respect. Most notably you can *not* build "universal" binary for # big-endian platform. This is because ARMv7 processor always picks # instructions in little-endian order. Another similar limitation is # that -mthumb can't "cross" -march=armv6t2 boundary, because that's # where it became Thumb-2. Well, this limitation is a bit artificial, # because it's not really impossible, but it's deemed too tricky to # support. And of course you have to be sure that your binutils are # actually up to the task of handling maximum target platform. With all # this in mind here is an example of how to configure "universal" build: # # ./Configure linux-armv4 -march=armv6 -D__ARM_MAX_ARCH__=8 # "linux-armv4", "gcc: -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${armv4_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-aarch64","gcc: -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${aarch64_asm}:linux64:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # Configure script adds minimally required -march for assembly support, # if no -march was specified at command line. mips32 and mips64 below # refer to contemporary MIPS Architecture specifications, MIPS32 and # MIPS64, rather than to kernel bitness. "linux-mips32", "gcc:-mabi=32 -O3 -Wall -DBN_DIV3W::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${mips32_asm}:o32:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-mips64", "gcc:-mabi=n32 -O3 -Wall -DBN_DIV3W::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${mips64_asm}:n32:dlfcn:linux-shared:-fPIC:-mabi=n32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::32", "linux64-mips64", "gcc:-mabi=64 -O3 -Wall -DBN_DIV3W::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${mips64_asm}:64:dlfcn:linux-shared:-fPIC:-mabi=64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", #### IA-32 targets... "linux-ia32-icc", "icc:-DL_ENDIAN -O2::-D_REENTRANT::-ldl -no_cpprt:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-KPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-elf", "gcc:-DL_ENDIAN -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-aout", "gcc:-DL_ENDIAN -O3 -fomit-frame-pointer -march=i486 -Wall::(unknown):::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_asm}:a.out", #### "linux-generic64","gcc:-O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-ppc64", "gcc:-m64 -DB_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_RISC1 DES_UNROLL:${ppc64_asm}:linux64:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "linux-ppc64le","gcc:-m64 -DL_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_RISC1 DES_UNROLL:$ppc64_asm:linux64le:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::", "linux-ia64", "gcc:-DL_ENDIAN -DTERMIO -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_UNROLL DES_INT:${ia64_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-ia64-icc","icc:-DL_ENDIAN -O2 -Wall::-D_REENTRANT::-ldl -no_cpprt:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_RISC1 DES_INT:${ia64_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-x86_64", "gcc:-m64 -DL_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "linux-x86_64-clang", "clang: -m64 -DL_ENDIAN -O3 -Wall -Wextra $clang_disabled_warnings -Qunused-arguments::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "debug-linux-x86_64-clang", "clang: -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -m64 -DL_ENDIAN -g -Wall -Wextra $clang_disabled_warnings -Qunused-arguments::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "linux-x86_64-icc", "icc:-DL_ENDIAN -O2::-D_REENTRANT::-ldl -no_cpprt:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", "linux-x32", "gcc:-mx32 -DL_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:linux-shared:-fPIC:-mx32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::x32", "linux64-s390x", "gcc:-m64 -DB_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL:${s390x_asm}:64:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", #### So called "highgprs" target for z/Architecture CPUs # "Highgprs" is kernel feature first implemented in Linux 2.6.32, see # /proc/cpuinfo. The idea is to preserve most significant bits of # general purpose registers not only upon 32-bit process context # switch, but even on asynchronous signal delivery to such process. # This makes it possible to deploy 64-bit instructions even in legacy # application context and achieve better [or should we say adequate] # performance. The build is binary compatible with linux-generic32, # and the idea is to be able to install the resulting libcrypto.so # alongside generic one, e.g. as /lib/highgprs/libcrypto.so.x.y, for # ldconfig and run-time linker to autodiscover. Unfortunately it # doesn't work just yet, because of couple of bugs in glibc # sysdeps/s390/dl-procinfo.c affecting ldconfig and ld.so.1... "linux32-s390x", "gcc:-m31 -Wa,-mzarch -DB_ENDIAN -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$s390x_asm;$asm=~s/bn\-s390x\.o/bn_asm.o/;$asm}.":31:dlfcn:linux-shared:-fPIC:-m31:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::/highgprs", #### SPARC Linux setups # Ray Miller has patiently # assisted with debugging of following two configs. "linux-sparcv8","gcc:-mcpu=v8 -DB_ENDIAN -O3 -fomit-frame-pointer -Wall -DBN_DIV2W::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv8_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # it's a real mess with -mcpu=ultrasparc option under Linux, but # -Wa,-Av8plus should do the trick no matter what. "linux-sparcv9","gcc:-m32 -mcpu=ultrasparc -DB_ENDIAN -O3 -fomit-frame-pointer -Wall -Wa,-Av8plus -DBN_DIV2W::-D_REENTRANT:ULTRASPARC:-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:linux-shared:-fPIC:-m32:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # GCC 3.1 is a requirement "linux64-sparcv9","gcc:-m64 -mcpu=ultrasparc -DB_ENDIAN -O3 -fomit-frame-pointer -Wall::-D_REENTRANT:ULTRASPARC:-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_PTR DES_RISC1 DES_UNROLL BF_PTR:${sparcv9_asm}:dlfcn:linux-shared:-fPIC:-m64:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):::64", #### Alpha Linux with GNU C and Compaq C setups # Special notes: # - linux-alpha+bwx-gcc is ment to be used from ./config only. If you # ought to run './Configure linux-alpha+bwx-gcc' manually, do # complement the command line with -mcpu=ev56, -mcpu=ev6 or whatever # which is appropriate. # - If you use ccc keep in mind that -fast implies -arch host and the # compiler is free to issue instructions which gonna make elder CPU # choke. If you wish to build "blended" toolkit, add -arch generic # *after* -fast and invoke './Configure linux-alpha-ccc' manually. # # # "linux-alpha-gcc","gcc:-O3 -DL_ENDIAN::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_RISC1 DES_UNROLL:${alpha_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-alpha+bwx-gcc","gcc:-O3 -DL_ENDIAN::-D_REENTRANT::-ldl:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_RISC1 DES_UNROLL:${alpha_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "linux-alpha-ccc","ccc:-fast -readonly_strings -DL_ENDIAN::-D_REENTRANT:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_PTR DES_RISC1 DES_UNROLL:${alpha_asm}", "linux-alpha+bwx-ccc","ccc:-fast -readonly_strings -DL_ENDIAN::-D_REENTRANT:::SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_INT DES_PTR DES_RISC1 DES_UNROLL:${alpha_asm}", # Android: linux-* but without pointers to headers and libs. "android","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${no_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "android-x86","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:".eval{my $asm=${x86_elf_asm};$asm=~s/:elf/:android/;$asm}.":dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "android-armv7","gcc:-march=armv7-a -mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -fomit-frame-pointer -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${armv4_asm}:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "android-mips","gcc:-mandroid -I\$(ANDROID_DEV)/include -B\$(ANDROID_DEV)/lib -O3 -Wall::-D_REENTRANT::-ldl:BN_LLONG RC4_CHAR RC4_CHUNK DES_INT DES_UNROLL BF_PTR:${mips32_asm}:o32:dlfcn:linux-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### *BSD [do see comment about ${BSDthreads} above!] "BSD-generic32","gcc:-O3 -fomit-frame-pointer -Wall::${BSDthreads}:::BN_LLONG RC2_CHAR RC4_INDEX DES_INT DES_UNROLL:${no_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-x86", "gcc:-DL_ENDIAN -O3 -fomit-frame-pointer -Wall::${BSDthreads}:::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_asm}:a.out:dlfcn:bsd-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-x86-elf", "gcc:-DL_ENDIAN -O3 -fomit-frame-pointer -Wall::${BSDthreads}:::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:bsd-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "debug-BSD-x86-elf", "gcc:-DL_ENDIAN -O3 -Wall -g::${BSDthreads}:::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:bsd-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-sparcv8", "gcc:-DB_ENDIAN -O3 -mcpu=v8 -Wall::${BSDthreads}:::BN_LLONG RC2_CHAR RC4_INDEX DES_INT DES_UNROLL:${sparcv8_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-generic64","gcc:-O3 -Wall::${BSDthreads}:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${no_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # -DMD32_REG_T=int doesn't actually belong in sparc64 target, it # simply *happens* to work around a compiler bug in gcc 3.3.3, # triggered by RIPEMD160 code. "BSD-sparc64", "gcc:-DB_ENDIAN -O3 -DMD32_REG_T=int -Wall::${BSDthreads}:::BN_LLONG RC2_CHAR RC4_CHUNK DES_INT DES_PTR DES_RISC2 BF_PTR:${sparcv9_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-ia64", "gcc:-DL_ENDIAN -O3 -Wall::${BSDthreads}:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_UNROLL DES_INT:${ia64_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "BSD-x86_64", "cc:-DL_ENDIAN -O3 -Wall::${BSDthreads}:::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:elf:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "bsdi-elf-gcc", "gcc:-DPERL5 -DL_ENDIAN -fomit-frame-pointer -O3 -march=i486 -Wall::(unknown)::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "nextstep", "cc:-O -Wall::(unknown):::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:::", "nextstep3.3", "cc:-O3 -Wall::(unknown):::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:::", # NCR MP-RAS UNIX ver 02.03.01 "ncr-scde","cc:-O6 -Xa -Hoff=BEHAVED -686 -Hwide -Hiw::(unknown)::-lsocket -lnsl -lc89:${x86_gcc_des} ${x86_gcc_opts}:::", # QNX "qnx4", "cc:-DL_ENDIAN -DTERMIO::(unknown):::${x86_gcc_des} ${x86_gcc_opts}:", "QNX6", "gcc:::::-lsocket::${no_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "QNX6-i386", "gcc:-DL_ENDIAN -O2 -Wall::::-lsocket:${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:bsd-gcc-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # BeOS "beos-x86-r5", "gcc:-DL_ENDIAN -DTERMIOS -O3 -fomit-frame-pointer -mcpu=pentium -Wall::-D_REENTRANT:BEOS:-lbe -lnet:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:beos:beos-shared:-fPIC -DPIC:-shared:.so", "beos-x86-bone", "gcc:-DL_ENDIAN -DTERMIOS -O3 -fomit-frame-pointer -mcpu=pentium -Wall::-D_REENTRANT:BEOS:-lbe -lbind -lsocket:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:beos:beos-shared:-fPIC:-shared:.so", #### SCO/Caldera targets. # # Originally we had like unixware-*, unixware-*-pentium, unixware-*-p6, etc. # Now we only have blended unixware-* as it's the only one used by ./config. # If you want to optimize for particular microarchitecture, bypass ./config # and './Configure unixware-7 -Kpentium_pro' or whatever appropriate. # Note that not all targets include assembler support. Mostly because of # lack of motivation to support out-of-date platforms with out-of-date # compiler drivers and assemblers. Tim Rice has # patiently assisted to debug most of it. # # UnixWare 2.0x fails destest with -O. "unixware-2.0","cc:-DFILIO_H -DNO_STRINGS_H::-Kthread::-lsocket -lnsl -lresolv -lx:${x86_gcc_des} ${x86_gcc_opts}:::", "unixware-2.1","cc:-O -DFILIO_H::-Kthread::-lsocket -lnsl -lresolv -lx:${x86_gcc_des} ${x86_gcc_opts}:::", "unixware-7","cc:-O -DFILIO_H -Kalloca::-Kthread::-lsocket -lnsl:BN_LLONG MD2_CHAR RC4_INDEX ${x86_gcc_des}:${x86_elf_asm}-1:dlfcn:svr5-shared:-Kpic::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "unixware-7-gcc","gcc:-DL_ENDIAN -DFILIO_H -O3 -fomit-frame-pointer -march=pentium -Wall::-D_REENTRANT::-lsocket -lnsl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}-1:dlfcn:gnu-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", # SCO 5 - Ben Laurie says the -O breaks the SCO cc. "sco5-cc", "cc:-belf::(unknown)::-lsocket -lnsl:${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}-1:dlfcn:svr3-shared:-Kpic::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "sco5-gcc", "gcc:-O3 -fomit-frame-pointer::(unknown)::-lsocket -lnsl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}-1:dlfcn:svr3-shared:-fPIC::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", #### IBM's AIX. "aix3-cc", "cc:-O -DB_ENDIAN -qmaxmem=16384::(unknown):AIX::BN_LLONG RC4_CHAR:::", "aix-gcc", "gcc:-O -DB_ENDIAN::-pthread:AIX::BN_LLONG RC4_CHAR:${ppc32_asm}:aix32:dlfcn:aix-shared::-shared -Wl,-G:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)::-X32", "aix64-gcc","gcc:-maix64 -O -DB_ENDIAN::-pthread:AIX::SIXTY_FOUR_BIT_LONG RC4_CHAR:${ppc64_asm}:aix64:dlfcn:aix-shared::-maix64 -shared -Wl,-G:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)::-X64", # Below targets assume AIX 5. Idea is to effectively disregard $OBJECT_MODE # at build time. $OBJECT_MODE is respected at ./config stage! "aix-cc", "cc:-q32 -O -DB_ENDIAN -qmaxmem=16384 -qro -qroconst::-qthreaded -D_THREAD_SAFE:AIX::BN_LLONG RC4_CHAR:${ppc32_asm}:aix32:dlfcn:aix-shared::-q32 -G:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)::-X 32", "aix64-cc", "cc:-q64 -O -DB_ENDIAN -qmaxmem=16384 -qro -qroconst::-qthreaded -D_THREAD_SAFE:AIX::SIXTY_FOUR_BIT_LONG RC4_CHAR:${ppc64_asm}:aix64:dlfcn:aix-shared::-q64 -G:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)::-X 64", # # Cray T90 and similar (SDSC) # It's Big-endian, but the algorithms work properly when B_ENDIAN is NOT # defined. The T90 ints and longs are 8 bytes long, and apparently the # B_ENDIAN code assumes 4 byte ints. Fortunately, the non-B_ENDIAN and # non L_ENDIAN code aligns the bytes in each word correctly. # # The BIT_FIELD_LIMITS define is to avoid two fatal compiler errors: #'Taking the address of a bit field is not allowed. ' #'An expression with bit field exists as the operand of "sizeof" ' # (written by Wayne Schroeder ) # # j90 is considered the base machine type for unicos machines, # so this configuration is now called "cray-j90" ... "cray-j90", "cc: -DBIT_FIELD_LIMITS -DTERMIOS::(unknown):CRAY::SIXTY_FOUR_BIT_LONG DES_INT:::", # # Cray T3E (Research Center Juelich, beckman@acl.lanl.gov) # # The BIT_FIELD_LIMITS define was written for the C90 (it seems). I added # another use. Basically, the problem is that the T3E uses some bit fields # for some st_addr stuff, and then sizeof and address-of fails # I could not use the ams/alpha.o option because the Cray assembler, 'cam' # did not like it. "cray-t3e", "cc: -DBIT_FIELD_LIMITS -DTERMIOS::(unknown):CRAY::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT:::", # DGUX, 88100. "dgux-R3-gcc", "gcc:-O3 -fomit-frame-pointer::(unknown):::RC4_INDEX DES_UNROLL:::", "dgux-R4-gcc", "gcc:-O3 -fomit-frame-pointer::(unknown)::-lnsl -lsocket:RC4_INDEX DES_UNROLL:::", "dgux-R4-x86-gcc", "gcc:-O3 -fomit-frame-pointer -DL_ENDIAN::(unknown)::-lnsl -lsocket:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}", # Sinix/ReliantUNIX RM400 # NOTE: The CDS++ Compiler up to V2.0Bsomething has the IRIX_CC_BUG optimizer problem. Better use -g */ "ReliantUNIX","cc:-KPIC -g -DTERMIOS -DB_ENDIAN::-Kthread:SNI:-lsocket -lnsl -lc -L/usr/ucblib -lucb:BN_LLONG DES_PTR DES_RISC2 DES_UNROLL BF_PTR:${no_asm}:dlfcn:reliantunix-shared:::.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR)", "SINIX","cc:-O::(unknown):SNI:-lsocket -lnsl -lc -L/usr/ucblib -lucb:RC4_INDEX RC4_CHAR:::", "SINIX-N","/usr/ucb/cc:-O2 -misaligned::(unknown)::-lucb:RC4_INDEX RC4_CHAR:::", # SIEMENS BS2000/OSD: an EBCDIC-based mainframe "BS2000-OSD","c89:-O -XLLML -XLLMK -XL -DB_ENDIAN -DCHARSET_EBCDIC::(unknown)::-lsocket -lnsl:THIRTY_TWO_BIT DES_PTR DES_UNROLL MD2_CHAR RC4_INDEX RC4_CHAR BF_PTR:::", # OS/390 Unix an EBCDIC-based Unix system on IBM mainframe # You need to compile using the c89.sh wrapper in the tools directory, because the # IBM compiler does not like the -L switch after any object modules. # "OS390-Unix","c89.sh:-O -DB_ENDIAN -DCHARSET_EBCDIC -DNO_SYS_PARAM_H -D_ALL_SOURCE::(unknown):::THIRTY_TWO_BIT DES_PTR DES_UNROLL MD2_CHAR RC4_INDEX RC4_CHAR BF_PTR:::", # Visual C targets # # Win64 targets, WIN64I denotes IA-64 and WIN64A - AMD64 "VC-WIN64I","cl:-W3 -Gs0 -Gy -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_DEPRECATE:::WIN64I::SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT EXPORT_VAR_AS_FN:ia64cpuid.o:ia64.o ia64-mont.o:::aes_core.o aes_cbc.o aes-ia64.o::md5-ia64.o:sha1-ia64.o sha256-ia64.o sha512-ia64.o:::::::ghash-ia64.o::ias:win32", "VC-WIN64A","cl:-W3 -Gs0 -Gy -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_DEPRECATE:::WIN64A::SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT EXPORT_VAR_AS_FN:".eval{my $asm=$x86_64_asm;$asm=~s/x86_64-gcc\.o/bn_asm.o/;$asm}.":auto:win32", "debug-VC-WIN64I","cl:-W3 -Gs0 -Gy -Zi -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_DEPRECATE:::WIN64I::SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT EXPORT_VAR_AS_FN:ia64cpuid.o:ia64.o:::aes_core.o aes_cbc.o aes-ia64.o::md5-ia64.o:sha1-ia64.o sha256-ia64.o sha512-ia64.o:::::::ghash-ia64.o::ias:win32", "debug-VC-WIN64A","cl:-W3 -Gs0 -Gy -Zi -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_DEPRECATE:::WIN64A::SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT EXPORT_VAR_AS_FN:".eval{my $asm=$x86_64_asm;$asm=~s/x86_64-gcc\.o/bn_asm.o/;$asm}.":auto:win32", # x86 Win32 target defaults to ANSI API, if you want UNICODE, complement # 'perl Configure VC-WIN32' with '-DUNICODE -D_UNICODE' "VC-WIN32","cl:-W3 -Gs0 -GF -Gy -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -D_CRT_SECURE_NO_DEPRECATE:::WIN32::BN_LLONG RC4_INDEX EXPORT_VAR_AS_FN ${x86_gcc_opts}:${x86_asm}:win32n:win32", # Unified CE target "debug-VC-WIN32","cl:-W3 -Gs0 -GF -Gy -Zi -nologo -DOPENSSL_SYSNAME_WIN32 -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -D_CRT_SECURE_NO_DEPRECATE:::WIN32::BN_LLONG RC4_INDEX EXPORT_VAR_AS_FN ${x86_gcc_opts}:${x86_asm}:win32n:win32", "VC-CE","cl::::WINCE::BN_LLONG RC4_INDEX EXPORT_VAR_AS_FN ${x86_gcc_opts}:${no_asm}:win32", # Borland C++ 4.5 "BC-32","bcc32::::WIN32::BN_LLONG DES_PTR RC4_INDEX EXPORT_VAR_AS_FN:${no_asm}:win32", # MinGW "mingw", "gcc:-mno-cygwin -DL_ENDIAN -DWIN32_LEAN_AND_MEAN -fomit-frame-pointer -O3 -march=i486 -Wall::-D_MT:MINGW32:-lws2_32 -lgdi32 -lcrypt32:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts} EXPORT_VAR_AS_FN:${x86_asm}:coff:win32:cygwin-shared:-D_WINDLL -DOPENSSL_USE_APPLINK:-mno-cygwin:.dll.a", # As for OPENSSL_USE_APPLINK. Applink makes it possible to use .dll # compiled with one compiler with application compiled with another # compiler. It's possible to engage Applink support in mingw64 build, # but it's not done, because till mingw64 supports structured exception # handling, one can't seriously consider its binaries for using with # non-mingw64 run-time environment. And as mingw64 is always consistent # with itself, Applink is never engaged and can as well be omitted. "mingw64", "gcc:-mno-cygwin -DL_ENDIAN -O3 -Wall -DWIN32_LEAN_AND_MEAN -DUNICODE -D_UNICODE::-D_MT:MINGW64:-lws2_32 -lgdi32 -lcrypt32:SIXTY_FOUR_BIT RC4_CHUNK_LL DES_INT EXPORT_VAR_AS_FN:${x86_64_asm}:mingw64:win32:cygwin-shared:-D_WINDLL:-mno-cygwin:.dll.a", # UWIN "UWIN", "cc:-DTERMIOS -DL_ENDIAN -O -Wall:::UWIN::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${no_asm}:win32", # Cygwin "Cygwin", "gcc:-DTERMIOS -DL_ENDIAN -fomit-frame-pointer -O3 -march=i486 -Wall:::CYGWIN::BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_asm}:coff:dlfcn:cygwin-shared:-D_WINDLL:-shared:.dll.a", "Cygwin-x86_64", "gcc:-DTERMIOS -DL_ENDIAN -O3 -Wall:::CYGWIN::SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:${x86_64_asm}:mingw64:dlfcn:cygwin-shared:-D_WINDLL:-shared:.dll.a", # NetWare from David Ward (dsward@novell.com) # requires either MetroWerks NLM development tools, or gcc / nlmconv # NetWare defaults socket bio to WinSock sockets. However, # the builds can be configured to use BSD sockets instead. # netware-clib => legacy CLib c-runtime support "netware-clib", "mwccnlm::::::${x86_gcc_opts}::", "netware-clib-bsdsock", "mwccnlm::::::${x86_gcc_opts}::", "netware-clib-gcc", "i586-netware-gcc:-nostdinc -I/ndk/nwsdk/include/nlm -I/ndk/ws295sdk/include -DL_ENDIAN -DNETWARE_CLIB -DOPENSSL_SYSNAME_NETWARE -O2 -Wall:::::${x86_gcc_opts}::", "netware-clib-bsdsock-gcc", "i586-netware-gcc:-nostdinc -I/ndk/nwsdk/include/nlm -DNETWARE_BSDSOCK -DNETDB_USE_INTERNET -DL_ENDIAN -DNETWARE_CLIB -DOPENSSL_SYSNAME_NETWARE -O2 -Wall:::::${x86_gcc_opts}::", # netware-libc => LibC/NKS support "netware-libc", "mwccnlm::::::BN_LLONG ${x86_gcc_opts}::", "netware-libc-bsdsock", "mwccnlm::::::BN_LLONG ${x86_gcc_opts}::", "netware-libc-gcc", "i586-netware-gcc:-nostdinc -I/ndk/libc/include -I/ndk/libc/include/winsock -DL_ENDIAN -DNETWARE_LIBC -DOPENSSL_SYSNAME_NETWARE -DTERMIO -O2 -Wall:::::BN_LLONG ${x86_gcc_opts}::", "netware-libc-bsdsock-gcc", "i586-netware-gcc:-nostdinc -I/ndk/libc/include -DNETWARE_BSDSOCK -DL_ENDIAN -DNETWARE_LIBC -DOPENSSL_SYSNAME_NETWARE -DTERMIO -O2 -Wall:::::BN_LLONG ${x86_gcc_opts}::", # DJGPP "DJGPP", "gcc:-I/dev/env/WATT_ROOT/inc -DTERMIO -DL_ENDIAN -fomit-frame-pointer -O2 -Wall:::MSDOS:-L/dev/env/WATT_ROOT/lib -lwatt:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_asm}:a.out:", # Ultrix from Bernhard Simon "ultrix-cc","cc:-std1 -O -Olimit 2500 -DL_ENDIAN::(unknown):::::::", "ultrix-gcc","gcc:-O3 -DL_ENDIAN::(unknown):::BN_LLONG::::", # K&R C is no longer supported; you need gcc on old Ultrix installations ##"ultrix","cc:-O2 -DNOPROTO -DNOCONST -DL_ENDIAN::(unknown):::::::", ##### MacOS X (a.k.a. Rhapsody or Darwin) setup "rhapsody-ppc-cc","cc:-O3 -DB_ENDIAN::(unknown):MACOSX_RHAPSODY::BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${no_asm}::", "darwin-ppc-cc","cc:-arch ppc -O3 -DB_ENDIAN -Wa,-force_cpusubtype_ALL::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${ppc32_asm}:osx32:dlfcn:darwin-shared:-fPIC -fno-common:-arch ppc -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "darwin64-ppc-cc","cc:-arch ppc64 -O3 -DB_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${ppc64_asm}:osx64:dlfcn:darwin-shared:-fPIC -fno-common:-arch ppc64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "darwin-i386-cc","cc:-arch i386 -O3 -fomit-frame-pointer -DL_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:BN_LLONG RC4_INT RC4_CHUNK DES_UNROLL BF_PTR:".eval{my $asm=$x86_asm;$asm=~s/cast\-586\.o//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch i386 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "debug-darwin-i386-cc","cc:-arch i386 -g3 -DL_ENDIAN::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:BN_LLONG RC4_INT RC4_CHUNK DES_UNROLL BF_PTR:${x86_asm}:macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch i386 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "darwin64-x86_64-cc","cc:-arch x86_64 -O3 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "debug-darwin64-x86_64-cc","cc:-arch x86_64 -ggdb -g2 -O0 -DL_ENDIAN -Wall::-D_REENTRANT:MACOSX:-Wl,-search_paths_first%:SIXTY_FOUR_BIT_LONG RC4_CHUNK DES_INT DES_UNROLL:".eval{my $asm=$x86_64_asm;$asm=~s/rc4\-[^:]+//;$asm}.":macosx:dlfcn:darwin-shared:-fPIC -fno-common:-arch x86_64 -dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", "debug-darwin-ppc-cc","cc:-DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DCRYPTO_MDEBUG -DB_ENDIAN -g -Wall -O::-D_REENTRANT:MACOSX::BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${ppc32_asm}:osx32:dlfcn:darwin-shared:-fPIC:-dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", # iPhoneOS/iOS "iphoneos-cross","llvm-gcc:-O3 -isysroot \$(CROSS_TOP)/SDKs/\$(CROSS_SDK) -fomit-frame-pointer -fno-common::-D_REENTRANT:iOS:-Wl,-search_paths_first%:BN_LLONG RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:${no_asm}:dlfcn:darwin-shared:-fPIC -fno-common:-dynamiclib:.\$(SHLIB_MAJOR).\$(SHLIB_MINOR).dylib", ##### A/UX "aux3-gcc","gcc:-O2 -DTERMIO::(unknown):AUX:-lbsd:RC4_CHAR RC4_CHUNK DES_UNROLL BF_PTR:::", ##### Sony NEWS-OS 4.x "newsos4-gcc","gcc:-O -DB_ENDIAN::(unknown):NEWS4:-lmld -liberty:BN_LLONG RC4_CHAR RC4_CHUNK DES_PTR DES_RISC1 DES_UNROLL BF_PTR::::", ##### GNU Hurd "hurd-x86", "gcc:-DL_ENDIAN -O3 -fomit-frame-pointer -march=i486 -Wall::-D_REENTRANT::-ldl:BN_LLONG ${x86_gcc_des} ${x86_gcc_opts}:${x86_elf_asm}:dlfcn:linux-shared:-fPIC", ##### OS/2 EMX "OS2-EMX", "gcc::::::::", ##### VxWorks for various targets "vxworks-ppc60x","ccppc:-D_REENTRANT -mrtp -mhard-float -mstrict-align -fno-implicit-fp -DPPC32_fp60x -O2 -fstrength-reduce -fno-builtin -fno-strict-aliasing -Wall -DCPU=PPC32 -DTOOL_FAMILY=gnu -DTOOL=gnu -I\$(WIND_BASE)/target/usr/h -I\$(WIND_BASE)/target/usr/h/wrn/coreip:::VXWORKS:-Wl,--defsym,__wrs_rtp_base=0xe0000000 -L \$(WIND_BASE)/target/usr/lib/ppc/PPC32/common:::::", "vxworks-ppcgen","ccppc:-D_REENTRANT -mrtp -msoft-float -mstrict-align -O1 -fno-builtin -fno-strict-aliasing -Wall -DCPU=PPC32 -DTOOL_FAMILY=gnu -DTOOL=gnu -I\$(WIND_BASE)/target/usr/h -I\$(WIND_BASE)/target/usr/h/wrn/coreip:::VXWORKS:-Wl,--defsym,__wrs_rtp_base=0xe0000000 -L \$(WIND_BASE)/target/usr/lib/ppc/PPC32/sfcommon:::::", "vxworks-ppc405","ccppc:-g -msoft-float -mlongcall -DCPU=PPC405 -I\$(WIND_BASE)/target/h:::VXWORKS:-r:::::", "vxworks-ppc750","ccppc:-ansi -nostdinc -DPPC750 -D_REENTRANT -fvolatile -fno-builtin -fno-for-scope -fsigned-char -Wall -msoft-float -mlongcall -DCPU=PPC604 -I\$(WIND_BASE)/target/h \$(DEBUG_FLAG):::VXWORKS:-r:::::", "vxworks-ppc750-debug","ccppc:-ansi -nostdinc -DPPC750 -D_REENTRANT -fvolatile -fno-builtin -fno-for-scope -fsigned-char -Wall -msoft-float -mlongcall -DCPU=PPC604 -I\$(WIND_BASE)/target/h -DBN_DEBUG -DREF_CHECK -DCONF_DEBUG -DBN_CTX_DEBUG -DCRYPTO_MDEBUG -DPEDANTIC -DDEBUG_SAFESTACK -DDEBUG -g:::VXWORKS:-r:::::", "vxworks-ppc860","ccppc:-nostdinc -msoft-float -DCPU=PPC860 -DNO_STRINGS_H -I\$(WIND_BASE)/target/h:::VXWORKS:-r:::::", "vxworks-simlinux","ccpentium:-B\$(WIND_BASE)/host/\$(WIND_HOST_TYPE)/lib/gcc-lib/ -D_VSB_CONFIG_FILE=\"\$(WIND_BASE)/target/lib/h/config/vsbConfig.h\" -DL_ENDIAN -DCPU=SIMLINUX -DTOOL_FAMILY=gnu -DTOOL=gnu -fno-builtin -fno-defer-pop -DNO_STRINGS_H -I\$(WIND_BASE)/target/h -I\$(WIND_BASE)/target/h/wrn/coreip -DOPENSSL_NO_HW_PADLOCK:::VXWORKS:-r::${no_asm}::::::ranlibpentium:", "vxworks-mips","ccmips:-mrtp -mips2 -O -G 0 -B\$(WIND_BASE)/host/\$(WIND_HOST_TYPE)/lib/gcc-lib/ -D_VSB_CONFIG_FILE=\"\$(WIND_BASE)/target/lib/h/config/vsbConfig.h\" -DCPU=MIPS32 -msoft-float -mno-branch-likely -DTOOL_FAMILY=gnu -DTOOL=gnu -fno-builtin -fno-defer-pop -DNO_STRINGS_H -I\$(WIND_BASE)/target/usr/h -I\$(WIND_BASE)/target/h/wrn/coreip::-D_REENTRANT:VXWORKS:-Wl,--defsym,__wrs_rtp_base=0xe0000000 -L \$(WIND_BASE)/target/usr/lib/mips/MIPSI32/sfcommon::${mips32_asm}:o32::::::ranlibmips:", ##### Compaq Non-Stop Kernel (Tandem) "tandem-c89","c89:-Ww -D__TANDEM -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED=1 -D_TANDEM_SOURCE -DB_ENDIAN::(unknown):::THIRTY_TWO_BIT:::", # uClinux "uClinux-dist","$ENV{'CC'}:\$(CFLAGS)::-D_REENTRANT::\$(LDFLAGS) \$(LDLIBS):BN_LLONG:${no_asm}:$ENV{'LIBSSL_dlfcn'}:linux-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):$ENV{'RANLIB'}::", "uClinux-dist64","$ENV{'CC'}:\$(CFLAGS)::-D_REENTRANT::\$(LDFLAGS) \$(LDLIBS):SIXTY_FOUR_BIT_LONG:${no_asm}:$ENV{'LIBSSL_dlfcn'}:linux-shared:-fPIC:-shared:.so.\$(SHLIB_MAJOR).\$(SHLIB_MINOR):$ENV{'RANLIB'}::", ); my @MK1MF_Builds=qw(VC-WIN64I VC-WIN64A debug-VC-WIN64I debug-VC-WIN64A VC-NT VC-CE VC-WIN32 debug-VC-WIN32 BC-32 netware-clib netware-clib-bsdsock netware-libc netware-libc-bsdsock); my $idx = 0; my $idx_cc = $idx++; my $idx_cflags = $idx++; my $idx_unistd = $idx++; my $idx_thread_cflag = $idx++; my $idx_sys_id = $idx++; my $idx_lflags = $idx++; my $idx_bn_ops = $idx++; my $idx_cpuid_obj = $idx++; my $idx_bn_obj = $idx++; my $idx_ec_obj = $idx++; my $idx_des_obj = $idx++; my $idx_aes_obj = $idx++; my $idx_bf_obj = $idx++; my $idx_md5_obj = $idx++; my $idx_sha1_obj = $idx++; my $idx_cast_obj = $idx++; my $idx_rc4_obj = $idx++; my $idx_rmd160_obj = $idx++; my $idx_rc5_obj = $idx++; my $idx_wp_obj = $idx++; my $idx_cmll_obj = $idx++; my $idx_modes_obj = $idx++; my $idx_engines_obj = $idx++; my $idx_perlasm_scheme = $idx++; my $idx_dso_scheme = $idx++; my $idx_shared_target = $idx++; my $idx_shared_cflag = $idx++; my $idx_shared_ldflag = $idx++; my $idx_shared_extension = $idx++; my $idx_ranlib = $idx++; my $idx_arflags = $idx++; my $idx_multilib = $idx++; my $prefix=""; my $libdir=""; my $openssldir=""; my $exe_ext=""; my $install_prefix= "$ENV{'INSTALL_PREFIX'}"; my $cross_compile_prefix=""; my $fipsdir="/usr/local/ssl/fips-2.0"; my $fipslibdir=""; my $baseaddr="0xFB00000"; my $no_threads=0; my $threads=0; my $no_shared=0; # but "no-shared" is default my $zlib=1; # but "no-zlib" is default my $no_krb5=0; # but "no-krb5" is implied unless "--with-krb5-..." is used my $no_rfc3779=1; # but "no-rfc3779" is default my $no_asm=0; my $no_dso=0; my $no_gmp=0; my @skip=(); my $Makefile="Makefile"; my $des_locl="crypto/des/des_locl.h"; my $des ="crypto/des/des.h"; my $bn ="crypto/bn/bn.h"; my $md2 ="crypto/md2/md2.h"; my $rc4 ="crypto/rc4/rc4.h"; my $rc4_locl="crypto/rc4/rc4_locl.h"; my $idea ="crypto/idea/idea.h"; my $rc2 ="crypto/rc2/rc2.h"; my $bf ="crypto/bf/bf_locl.h"; my $bn_asm ="bn_asm.o"; my $des_enc="des_enc.o fcrypt_b.o"; my $aes_enc="aes_core.o aes_cbc.o"; my $bf_enc ="bf_enc.o"; my $cast_enc="c_enc.o"; my $rc4_enc="rc4_enc.o rc4_skey.o"; my $rc5_enc="rc5_enc.o"; my $md5_obj=""; my $sha1_obj=""; my $rmd160_obj=""; my $cmll_enc="camellia.o cmll_misc.o cmll_cbc.o"; my $processor=""; my $default_ranlib; my $perl; my $fips=0; if (exists $ENV{FIPSDIR}) { $fipsdir = $ENV{FIPSDIR}; $fipsdir =~ s/\/$//; } # All of the following is disabled by default (RC5 was enabled before 0.9.8): my %disabled = ( # "what" => "comment" [or special keyword "experimental"] "ec_nistp_64_gcc_128" => "default", "gmp" => "default", "jpake" => "experimental", "libunbound" => "experimental", "md2" => "default", "rc5" => "default", "rfc3779" => "default", - "sctp" => "default", + "sctp" => "default", "shared" => "default", "ssl-trace" => "default", + "ssl2" => "default", "store" => "experimental", "unit-test" => "default", + "weak-ssl-ciphers" => "default", "zlib" => "default", "zlib-dynamic" => "default" ); my @experimental = (); # This is what $depflags will look like with the above defaults # (we need this to see if we should advise the user to run "make depend"): my $default_depflags = " -DOPENSSL_NO_EC_NISTP_64_GCC_128 -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_LIBUNBOUND -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SCTP -DOPENSSL_NO_SSL_TRACE -DOPENSSL_NO_STORE -DOPENSSL_NO_UNIT_TEST"; # Explicit "no-..." options will be collected in %disabled along with the defaults. # To remove something from %disabled, use "enable-foo" (unless it's experimental). # For symmetry, "disable-foo" is a synonym for "no-foo". # For features called "experimental" here, a more explicit "experimental-foo" is needed to enable. # We will collect such requests in @experimental. # To avoid accidental use of experimental features, applications will have to use -DOPENSSL_EXPERIMENTAL_FOO. my $no_sse2=0; &usage if ($#ARGV < 0); my $flags; my $depflags; my $openssl_experimental_defines; my $openssl_algorithm_defines; my $openssl_thread_defines; my $openssl_sys_defines=""; my $openssl_other_defines; my $libs; my $libkrb5=""; my $target; my $options; my $symlink; my $make_depend=0; my %withargs=(); my @argvcopy=@ARGV; my $argvstring=""; my $argv_unprocessed=1; while($argv_unprocessed) { $flags=""; $depflags=""; $openssl_experimental_defines=""; $openssl_algorithm_defines=""; $openssl_thread_defines=""; $openssl_sys_defines=""; $openssl_other_defines=""; $libs=""; $target=""; $options=""; $symlink=1; $argv_unprocessed=0; $argvstring=join(' ',@argvcopy); PROCESS_ARGS: foreach (@argvcopy) { s /^-no-/no-/; # some people just can't read the instructions # rewrite some options in "enable-..." form s /^-?-?shared$/enable-shared/; s /^sctp$/enable-sctp/; s /^threads$/enable-threads/; s /^zlib$/enable-zlib/; s /^zlib-dynamic$/enable-zlib-dynamic/; if (/^no-(.+)$/ || /^disable-(.+)$/) { if (!($disabled{$1} eq "experimental")) { if ($1 eq "ssl") { $disabled{"ssl2"} = "option(ssl)"; $disabled{"ssl3"} = "option(ssl)"; } elsif ($1 eq "tls") { $disabled{"tls1"} = "option(tls)" } elsif ($1 eq "ssl3-method") { $disabled{"ssl3-method"} = "option(ssl)"; $disabled{"ssl3"} = "option(ssl)"; } else { $disabled{$1} = "option"; } } } elsif (/^enable-(.+)$/ || /^experimental-(.+)$/) { my $algo = $1; if ($disabled{$algo} eq "experimental") { die "You are requesting an experimental feature; please say 'experimental-$algo' if you are sure\n" unless (/^experimental-/); push @experimental, $algo; } delete $disabled{$algo}; $threads = 1 if ($algo eq "threads"); } elsif (/^--test-sanity$/) { exit(&test_sanity()); } elsif (/^--strict-warnings/) { $strict_warnings = 1; } elsif (/^reconfigure/ || /^reconf/) { if (open(IN,"<$Makefile")) { while () { chomp; if (/^CONFIGURE_ARGS=(.*)/) { $argvstring=$1; @argvcopy=split(' ',$argvstring); die "Incorrect data to reconfigure, please do a normal configuration\n" if (grep(/^reconf/,@argvcopy)); print "Reconfiguring with: $argvstring\n"; $argv_unprocessed=1; close(IN); last PROCESS_ARGS; } } close(IN); } die "Insufficient data to reconfigure, please do a normal configuration\n"; } elsif (/^386$/) { $processor=386; } elsif (/^fips$/) { $fips=1; } elsif (/^rsaref$/) { # No RSAref support any more since it's not needed. # The check for the option is there so scripts aren't # broken } elsif (/^[-+]/) { if (/^--prefix=(.*)$/) { $prefix=$1; } elsif (/^--libdir=(.*)$/) { $libdir=$1; } elsif (/^--openssldir=(.*)$/) { $openssldir=$1; } elsif (/^--install.prefix=(.*)$/) { $install_prefix=$1; } elsif (/^--with-krb5-(dir|lib|include|flavor)=(.*)$/) { $withargs{"krb5-".$1}=$2; } elsif (/^--with-zlib-lib=(.*)$/) { $withargs{"zlib-lib"}=$1; } elsif (/^--with-zlib-include=(.*)$/) { $withargs{"zlib-include"}="-I$1"; } elsif (/^--with-fipsdir=(.*)$/) { $fipsdir="$1"; } elsif (/^--with-fipslibdir=(.*)$/) { $fipslibdir="$1"; } elsif (/^--with-baseaddr=(.*)$/) { $baseaddr="$1"; } elsif (/^--cross-compile-prefix=(.*)$/) { $cross_compile_prefix=$1; } elsif (/^-[lL](.*)$/ or /^-Wl,/) { $libs.=$_." "; } else # common if (/^[-+]/), just pass down... { $_ =~ s/%([0-9a-f]{1,2})/chr(hex($1))/gei; $flags.=$_." "; } } elsif ($_ =~ /^([^:]+):(.+)$/) { eval "\$table{\$1} = \"$2\""; # allow $xxx constructs in the string $target=$1; } else { die "target already defined - $target (offending arg: $_)\n" if ($target ne ""); $target=$_; } unless ($_ eq $target || /^no-/ || /^disable-/) { # "no-..." follows later after implied disactivations # have been derived. (Don't take this too seroiusly, # we really only write OPTIONS to the Makefile out of # nostalgia.) if ($options eq "") { $options = $_; } else { $options .= " ".$_; } } } } if ($processor eq "386") { $disabled{"sse2"} = "forced"; } if (!defined($withargs{"krb5-flavor"}) || $withargs{"krb5-flavor"} eq "") { $disabled{"krb5"} = "krb5-flavor not specified"; } if (!defined($disabled{"zlib-dynamic"})) { # "zlib-dynamic" was specifically enabled, so enable "zlib" delete $disabled{"zlib"}; } if (defined($disabled{"rijndael"})) { $disabled{"aes"} = "forced"; } if (defined($disabled{"des"})) { $disabled{"mdc2"} = "forced"; } if (defined($disabled{"ec"})) { $disabled{"ecdsa"} = "forced"; $disabled{"ecdh"} = "forced"; } # SSL 2.0 requires MD5 and RSA if (defined($disabled{"md5"}) || defined($disabled{"rsa"})) { $disabled{"ssl2"} = "forced"; } if ($fips && $fipslibdir eq "") { $fipslibdir = $fipsdir . "/lib/"; } # RSAX ENGINE sets default non-FIPS RSA method. if ($fips) { $disabled{"rsax"} = "forced"; } # SSL 3.0 and TLS requires MD5 and SHA and either RSA or DSA+DH if (defined($disabled{"md5"}) || defined($disabled{"sha"}) || (defined($disabled{"rsa"}) && (defined($disabled{"dsa"}) || defined($disabled{"dh"})))) { $disabled{"ssl3"} = "forced"; $disabled{"tls1"} = "forced"; } if (defined($disabled{"tls1"})) { $disabled{"tlsext"} = "forced"; } if (defined($disabled{"ec"}) || defined($disabled{"dsa"}) || defined($disabled{"dh"})) { $disabled{"gost"} = "forced"; } # SRP and HEARTBEATS require TLSEXT if (defined($disabled{"tlsext"})) { $disabled{"srp"} = "forced"; $disabled{"heartbeats"} = "forced"; } if ($target eq "TABLE") { foreach $target (sort keys %table) { print_table_entry($target); } exit 0; } if ($target eq "LIST") { foreach (sort keys %table) { print; print "\n"; } exit 0; } if ($target =~ m/^CygWin32(-.*)$/) { $target = "Cygwin".$1; } print "Configuring for $target\n"; &usage if (!defined($table{$target})); foreach (sort (keys %disabled)) { $options .= " no-$_"; printf " no-%-12s %-10s", $_, "[$disabled{$_}]"; if (/^dso$/) { $no_dso = 1; } elsif (/^threads$/) { $no_threads = 1; } elsif (/^shared$/) { $no_shared = 1; } elsif (/^zlib$/) { $zlib = 0; } elsif (/^static-engine$/) { } elsif (/^zlib-dynamic$/) { } elsif (/^symlinks$/) { $symlink = 0; } elsif (/^sse2$/) { $no_sse2 = 1; } else { my ($ALGO, $algo); ($ALGO = $algo = $_) =~ tr/[\-a-z]/[_A-Z]/; if (/^asm$/ || /^err$/ || /^hw$/ || /^hw-/) { $openssl_other_defines .= "#define OPENSSL_NO_$ALGO\n"; print " OPENSSL_NO_$ALGO"; if (/^err$/) { $flags .= "-DOPENSSL_NO_ERR "; } elsif (/^asm$/) { $no_asm = 1; } } else { $openssl_algorithm_defines .= "#define OPENSSL_NO_$ALGO\n"; print " OPENSSL_NO_$ALGO"; if (/^krb5$/) { $no_krb5 = 1; } else { push @skip, $algo; # fix-up crypto/directory name(s) @skip[$#skip]="whrlpool" if $algo eq "whirlpool"; print " (skip dir)"; $depflags .= " -DOPENSSL_NO_$ALGO"; } } } print "\n"; } my $exp_cflags = ""; foreach (sort @experimental) { my $ALGO; ($ALGO = $_) =~ tr/[a-z]/[A-Z]/; # opensslconf.h will set OPENSSL_NO_... unless OPENSSL_EXPERIMENTAL_... is defined $openssl_experimental_defines .= "#define OPENSSL_NO_$ALGO\n"; $exp_cflags .= " -DOPENSSL_EXPERIMENTAL_$ALGO"; } my $IsMK1MF=scalar grep /^$target$/,@MK1MF_Builds; $exe_ext=".exe" if ($target eq "Cygwin" || $target eq "DJGPP" || $target =~ /^mingw/); $exe_ext=".nlm" if ($target =~ /netware/); $exe_ext=".pm" if ($target =~ /vos/); $openssldir="/usr/local/ssl" if ($openssldir eq "" and $prefix eq ""); $prefix=$openssldir if $prefix eq ""; $default_ranlib= &which("ranlib") or $default_ranlib="true"; $perl=$ENV{'PERL'} or $perl=&which("perl5") or $perl=&which("perl") or $perl="perl"; my $make = $ENV{'MAKE'} || "make"; $cross_compile_prefix=$ENV{'CROSS_COMPILE'} if $cross_compile_prefix eq ""; chop $openssldir if $openssldir =~ /\/$/; chop $prefix if $prefix =~ /.\/$/; $openssldir=$prefix . "/ssl" if $openssldir eq ""; $openssldir=$prefix . "/" . $openssldir if $openssldir !~ /(^\/|^[a-zA-Z]:[\\\/])/; print "IsMK1MF=$IsMK1MF\n"; my @fields = split(/\s*:\s*/,$table{$target} . ":" x 30 , -1); my $cc = $fields[$idx_cc]; # Allow environment CC to override compiler... if($ENV{CC}) { $cc = $ENV{CC}; } my $cflags = $fields[$idx_cflags]; my $unistd = $fields[$idx_unistd]; my $thread_cflag = $fields[$idx_thread_cflag]; my $sys_id = $fields[$idx_sys_id]; my $lflags = $fields[$idx_lflags]; my $bn_ops = $fields[$idx_bn_ops]; my $cpuid_obj = $fields[$idx_cpuid_obj]; my $bn_obj = $fields[$idx_bn_obj]; my $ec_obj = $fields[$idx_ec_obj]; my $des_obj = $fields[$idx_des_obj]; my $aes_obj = $fields[$idx_aes_obj]; my $bf_obj = $fields[$idx_bf_obj]; my $md5_obj = $fields[$idx_md5_obj]; my $sha1_obj = $fields[$idx_sha1_obj]; my $cast_obj = $fields[$idx_cast_obj]; my $rc4_obj = $fields[$idx_rc4_obj]; my $rmd160_obj = $fields[$idx_rmd160_obj]; my $rc5_obj = $fields[$idx_rc5_obj]; my $wp_obj = $fields[$idx_wp_obj]; my $cmll_obj = $fields[$idx_cmll_obj]; my $modes_obj = $fields[$idx_modes_obj]; my $engines_obj = $fields[$idx_engines_obj]; my $perlasm_scheme = $fields[$idx_perlasm_scheme]; my $dso_scheme = $fields[$idx_dso_scheme]; my $shared_target = $fields[$idx_shared_target]; my $shared_cflag = $fields[$idx_shared_cflag]; my $shared_ldflag = $fields[$idx_shared_ldflag]; my $shared_extension = $fields[$idx_shared_extension]; my $ranlib = $ENV{'RANLIB'} || $fields[$idx_ranlib]; my $ar = $ENV{'AR'} || "ar"; my $arflags = $fields[$idx_arflags]; my $multilib = $fields[$idx_multilib]; # if $prefix/lib$multilib is not an existing directory, then # assume that it's not searched by linker automatically, in # which case adding $multilib suffix causes more grief than # we're ready to tolerate, so don't... $multilib="" if !-d "$prefix/lib$multilib"; $libdir="lib$multilib" if $libdir eq ""; $cflags = "$cflags$exp_cflags"; # '%' in $lflags is used to split flags to "pre-" and post-flags my ($prelflags,$postlflags)=split('%',$lflags); if (defined($postlflags)) { $lflags=$postlflags; } else { $lflags=$prelflags; undef $prelflags; } if ($target =~ /^mingw/ && `$cc --target-help 2>&1` !~ m/\-mno\-cygwin/m) { $cflags =~ s/\-mno\-cygwin\s*//; $shared_ldflag =~ s/\-mno\-cygwin\s*//; } if ($target =~ /linux.*\-mips/ && !$no_asm && $flags !~ /\-m(ips|arch=)/) { # minimally required architecture flags for assembly modules $cflags="-mips2 $cflags" if ($target =~ /mips32/); $cflags="-mips3 $cflags" if ($target =~ /mips64/); } my $no_shared_warn=0; my $no_user_cflags=0; if ($flags ne "") { $cflags="$flags$cflags"; } else { $no_user_cflags=1; } # Kerberos settings. The flavor must be provided from outside, either through # the script "config" or manually. if (!$no_krb5) { my ($lresolv, $lpath, $lext); if ($withargs{"krb5-flavor"} =~ /^[Hh]eimdal$/) { die "Sorry, Heimdal is currently not supported\n"; } ##### HACK to force use of Heimdal. ##### WARNING: Since we don't really have adequate support for Heimdal, ##### using this will break the build. You'll have to make ##### changes to the source, and if you do, please send ##### patches to openssl-dev@openssl.org if ($withargs{"krb5-flavor"} =~ /^force-[Hh]eimdal$/) { warn "Heimdal isn't really supported. Your build WILL break\n"; warn "If you fix the problems, please send a patch to openssl-dev\@openssl.org\n"; $withargs{"krb5-dir"} = "/usr/heimdal" if $withargs{"krb5-dir"} eq ""; $withargs{"krb5-lib"} = "-L".$withargs{"krb5-dir"}. "/lib -lgssapi -lkrb5 -lcom_err" if $withargs{"krb5-lib"} eq "" && !$IsMK1MF; $cflags="-DKRB5_HEIMDAL $cflags"; } if ($withargs{"krb5-flavor"} =~ /^[Mm][Ii][Tt]/) { $withargs{"krb5-dir"} = "/usr/kerberos" if $withargs{"krb5-dir"} eq ""; $withargs{"krb5-lib"} = "-L".$withargs{"krb5-dir"}. "/lib -lgssapi_krb5 -lkrb5 -lcom_err -lk5crypto" if $withargs{"krb5-lib"} eq "" && !$IsMK1MF; $cflags="-DKRB5_MIT $cflags"; $withargs{"krb5-flavor"} =~ s/^[Mm][Ii][Tt][._-]*//; if ($withargs{"krb5-flavor"} =~ /^1[._-]*[01]/) { $cflags="-DKRB5_MIT_OLD11 $cflags"; } } LRESOLV: foreach $lpath ("/lib", "/usr/lib") { foreach $lext ("a", "so") { $lresolv = "$lpath/libresolv.$lext"; last LRESOLV if (-r "$lresolv"); $lresolv = ""; } } $withargs{"krb5-lib"} .= " -lresolv" if ("$lresolv" ne ""); $withargs{"krb5-include"} = "-I".$withargs{"krb5-dir"}."/include" if $withargs{"krb5-include"} eq "" && $withargs{"krb5-dir"} ne ""; } # The DSO code currently always implements all functions so that no # applications will have to worry about that from a compilation point # of view. However, the "method"s may return zero unless that platform # has support compiled in for them. Currently each method is enabled # by a define "DSO_" ... we translate the "dso_scheme" config # string entry into using the following logic; my $dso_cflags; if (!$no_dso && $dso_scheme ne "") { $dso_scheme =~ tr/[a-z]/[A-Z]/; if ($dso_scheme eq "DLFCN") { $dso_cflags = "-DDSO_DLFCN -DHAVE_DLFCN_H"; } elsif ($dso_scheme eq "DLFCN_NO_H") { $dso_cflags = "-DDSO_DLFCN"; } else { $dso_cflags = "-DDSO_$dso_scheme"; } $cflags = "$dso_cflags $cflags"; } my $thread_cflags; my $thread_defines; if ($thread_cflag ne "(unknown)" && !$no_threads) { # If we know how to do it, support threads by default. $threads = 1; } if ($thread_cflag eq "(unknown)" && $threads) { # If the user asked for "threads", [s]he is also expected to # provide any system-dependent compiler options that are # necessary. if ($no_user_cflags) { print "You asked for multi-threading support, but didn't\n"; print "provide any system-specific compiler options\n"; exit(1); } $thread_cflags="-DOPENSSL_THREADS $cflags" ; $thread_defines .= "#define OPENSSL_THREADS\n"; } else { $thread_cflags="-DOPENSSL_THREADS $thread_cflag $cflags"; $thread_defines .= "#define OPENSSL_THREADS\n"; # my $def; # foreach $def (split ' ',$thread_cflag) # { # if ($def =~ s/^-D// && $def !~ /^_/) # { # $thread_defines .= "#define $def\n"; # } # } } $lflags="$libs$lflags" if ($libs ne ""); if ($no_asm) { $cpuid_obj=$bn_obj=$ec_obj= $des_obj=$aes_obj=$bf_obj=$cast_obj=$rc4_obj=$rc5_obj=$cmll_obj= $modes_obj=$sha1_obj=$md5_obj=$rmd160_obj=$wp_obj=$engines_obj=""; } if (!$no_shared) { $cast_obj=""; # CAST assembler is not PIC } if ($threads) { $cflags=$thread_cflags; $openssl_thread_defines .= $thread_defines; } if ($zlib) { $cflags = "-DZLIB $cflags"; if (defined($disabled{"zlib-dynamic"})) { if (defined($withargs{"zlib-lib"})) { $lflags = "$lflags -L" . $withargs{"zlib-lib"} . " -lz"; } else { $lflags = "$lflags -lz"; } } else { $cflags = "-DZLIB_SHARED $cflags"; } } # You will find shlib_mark1 and shlib_mark2 explained in Makefile.org my $shared_mark = ""; if ($shared_target eq "") { $no_shared_warn = 1 if !$no_shared; $no_shared = 1; } if (!$no_shared) { if ($shared_cflag ne "") { $cflags = "$shared_cflag -DOPENSSL_PIC $cflags"; } } if (!$IsMK1MF) { # add {no-}static-engine to options to allow mkdef.pl to work without extra arguments if ($no_shared) { $openssl_other_defines.="#define OPENSSL_NO_DYNAMIC_ENGINE\n"; $options.=" static-engine"; } else { $openssl_other_defines.="#define OPENSSL_NO_STATIC_ENGINE\n"; $options.=" no-static-engine"; } } $cpuid_obj.=" uplink.o uplink-x86.o" if ($cflags =~ /\-DOPENSSL_USE_APPLINK/); # # Platform fix-ups # if ($target =~ /\-icc$/) # Intel C compiler { my $iccver=0; if (open(FD,"$cc -V 2>&1 |")) { while() { $iccver=$1 if (/Version ([0-9]+)\./); } close(FD); } if ($iccver>=8) { $cflags=~s/\-KPIC/-fPIC/; # Eliminate unnecessary dependency from libirc.a. This is # essential for shared library support, as otherwise # apps/openssl can end up in endless loop upon startup... $cflags.=" -Dmemcpy=__builtin_memcpy -Dmemset=__builtin_memset"; } if ($iccver>=9) { $lflags.=" -i-static"; $lflags=~s/\-no_cpprt/-no-cpprt/; } if ($iccver>=10) { $lflags=~s/\-i\-static/-static-intel/; } if ($iccver>=11) { $cflags.=" -no-intel-extensions"; # disable Cilk $lflags=~s/\-no\-cpprt/-no-cxxlib/; } } # Unlike other OSes (like Solaris, Linux, Tru64, IRIX) BSD run-time # linkers (tested OpenBSD, NetBSD and FreeBSD) "demand" RPATH set on # .so objects. Apparently application RPATH is not global and does # not apply to .so linked with other .so. Problem manifests itself # when libssl.so fails to load libcrypto.so. One can argue that we # should engrave this into Makefile.shared rules or into BSD-* config # lines above. Meanwhile let's try to be cautious and pass -rpath to # linker only when --prefix is not /usr. if ($target =~ /^BSD\-/) { $shared_ldflag.=" -Wl,-rpath,\$\$(LIBRPATH)" if ($prefix !~ m|^/usr[/]*$|); } if ($sys_id ne "") { #$cflags="-DOPENSSL_SYSNAME_$sys_id $cflags"; $openssl_sys_defines="#define OPENSSL_SYSNAME_$sys_id\n"; } if ($ranlib eq "") { $ranlib = $default_ranlib; } #my ($bn1)=split(/\s+/,$bn_obj); #$bn1 = "" unless defined $bn1; #$bn1=$bn_asm unless ($bn1 =~ /\.o$/); #$bn_obj="$bn1"; $cpuid_obj="" if ($processor eq "386"); $bn_obj = $bn_asm unless $bn_obj ne ""; # bn-586 is the only one implementing bn_*_part_words $cflags.=" -DOPENSSL_BN_ASM_PART_WORDS" if ($bn_obj =~ /bn-586/); $cflags.=" -DOPENSSL_IA32_SSE2" if (!$no_sse2 && $bn_obj =~ /86/); $cflags.=" -DOPENSSL_BN_ASM_MONT" if ($bn_obj =~ /-mont/); $cflags.=" -DOPENSSL_BN_ASM_MONT5" if ($bn_obj =~ /-mont5/); $cflags.=" -DOPENSSL_BN_ASM_GF2m" if ($bn_obj =~ /-gf2m/); if ($fips) { $openssl_other_defines.="#define OPENSSL_FIPS\n"; $cflags .= " -I\$(FIPSDIR)/include"; } $cpuid_obj="mem_clr.o" unless ($cpuid_obj =~ /\.o$/); $des_obj=$des_enc unless ($des_obj =~ /\.o$/); $bf_obj=$bf_enc unless ($bf_obj =~ /\.o$/); $cast_obj=$cast_enc unless ($cast_obj =~ /\.o$/); $rc4_obj=$rc4_enc unless ($rc4_obj =~ /\.o$/); $rc5_obj=$rc5_enc unless ($rc5_obj =~ /\.o$/); if ($sha1_obj =~ /\.o$/) { # $sha1_obj=$sha1_enc; $cflags.=" -DSHA1_ASM" if ($sha1_obj =~ /sx86/ || $sha1_obj =~ /sha1/); $cflags.=" -DSHA256_ASM" if ($sha1_obj =~ /sha256/); $cflags.=" -DSHA512_ASM" if ($sha1_obj =~ /sha512/); if ($sha1_obj =~ /sse2/) { if ($no_sse2) { $sha1_obj =~ s/\S*sse2\S+//; } elsif ($cflags !~ /OPENSSL_IA32_SSE2/) { $cflags.=" -DOPENSSL_IA32_SSE2"; } } } if ($md5_obj =~ /\.o$/) { # $md5_obj=$md5_enc; $cflags.=" -DMD5_ASM"; } if ($rmd160_obj =~ /\.o$/) { # $rmd160_obj=$rmd160_enc; $cflags.=" -DRMD160_ASM"; } if ($aes_obj =~ /\.o$/) { $cflags.=" -DAES_ASM" if ($aes_obj =~ m/\baes\-/);; # aes-ctr.o is not a real file, only indication that assembler # module implements AES_ctr32_encrypt... $cflags.=" -DAES_CTR_ASM" if ($aes_obj =~ s/\s*aes\-ctr\.o//); # aes-xts.o indicates presense of AES_xts_[en|de]crypt... $cflags.=" -DAES_XTS_ASM" if ($aes_obj =~ s/\s*aes\-xts\.o//); $aes_obj =~ s/\s*(vpaes|aesni)\-x86\.o//g if ($no_sse2); $cflags.=" -DVPAES_ASM" if ($aes_obj =~ m/vpaes/); $cflags.=" -DBSAES_ASM" if ($aes_obj =~ m/bsaes/); } else { $aes_obj=$aes_enc; } $wp_obj="" if ($wp_obj =~ /mmx/ && $processor eq "386"); if ($wp_obj =~ /\.o$/ && !$disabled{"whirlpool"}) { $cflags.=" -DWHIRLPOOL_ASM"; } else { $wp_obj="wp_block.o"; } $cmll_obj=$cmll_enc unless ($cmll_obj =~ /.o$/); if ($modes_obj =~ /ghash\-/) { $cflags.=" -DGHASH_ASM"; } if ($ec_obj =~ /ecp_nistz256/) { $cflags.=" -DECP_NISTZ256_ASM"; } # "Stringify" the C flags string. This permits it to be made part of a string # and works as well on command lines. $cflags =~ s/([\\\"])/\\\1/g; my $version = "unknown"; my $version_num = "unknown"; my $major = "unknown"; my $minor = "unknown"; my $shlib_version_number = "unknown"; my $shlib_version_history = "unknown"; my $shlib_major = "unknown"; my $shlib_minor = "unknown"; open(IN,') { $version=$1 if /OPENSSL.VERSION.TEXT.*OpenSSL (\S+) /; $version_num=$1 if /OPENSSL.VERSION.NUMBER.*0x(\S+)/; $shlib_version_number=$1 if /SHLIB_VERSION_NUMBER *"([^"]+)"/; $shlib_version_history=$1 if /SHLIB_VERSION_HISTORY *"([^"]*)"/; } close(IN); if ($shlib_version_history ne "") { $shlib_version_history .= ":"; } if ($version =~ /(^[0-9]*)\.([0-9\.]*)/) { $major=$1; $minor=$2; } if ($shlib_version_number =~ /(^[0-9]*)\.([0-9\.]*)/) { $shlib_major=$1; $shlib_minor=$2; } my $ecc = $cc; $ecc = "clang" if `$cc --version 2>&1` =~ /clang/; if ($strict_warnings) { my $wopt; die "ERROR --strict-warnings requires gcc or clang" unless ($ecc =~ /gcc$/ or $ecc =~ /clang$/); foreach $wopt (split /\s+/, $gcc_devteam_warn) { $cflags .= " $wopt" unless ($cflags =~ /(^|\s)$wopt(\s|$)/) } if ($ecc eq "clang") { foreach $wopt (split /\s+/, $clang_devteam_warn) { $cflags .= " $wopt" unless ($cflags =~ /(^|\s)$wopt(\s|$)/) } } } open(IN,'$Makefile.new") || die "unable to create $Makefile.new:$!\n"; print OUT "### Generated automatically from Makefile.org by Configure.\n\n"; my $sdirs=0; while () { chomp; $sdirs = 1 if /^SDIRS=/; if ($sdirs) { my $dir; foreach $dir (@skip) { s/(\s)$dir /$1/; s/\s$dir$//; } } $sdirs = 0 unless /\\$/; s/engines // if (/^DIRS=/ && $disabled{"engine"}); s/ccgost// if (/^ENGDIRS=/ && $disabled{"gost"}); s/^VERSION=.*/VERSION=$version/; s/^MAJOR=.*/MAJOR=$major/; s/^MINOR=.*/MINOR=$minor/; s/^SHLIB_VERSION_NUMBER=.*/SHLIB_VERSION_NUMBER=$shlib_version_number/; s/^SHLIB_VERSION_HISTORY=.*/SHLIB_VERSION_HISTORY=$shlib_version_history/; s/^SHLIB_MAJOR=.*/SHLIB_MAJOR=$shlib_major/; s/^SHLIB_MINOR=.*/SHLIB_MINOR=$shlib_minor/; s/^SHLIB_EXT=.*/SHLIB_EXT=$shared_extension/; s/^INSTALLTOP=.*$/INSTALLTOP=$prefix/; s/^MULTILIB=.*$/MULTILIB=$multilib/; s/^OPENSSLDIR=.*$/OPENSSLDIR=$openssldir/; s/^LIBDIR=.*$/LIBDIR=$libdir/; s/^INSTALL_PREFIX=.*$/INSTALL_PREFIX=$install_prefix/; s/^PLATFORM=.*$/PLATFORM=$target/; s/^OPTIONS=.*$/OPTIONS=$options/; s/^CONFIGURE_ARGS=.*$/CONFIGURE_ARGS=$argvstring/; if ($cross_compile_prefix) { s/^CC=.*$/CROSS_COMPILE= $cross_compile_prefix\nCC= \$\(CROSS_COMPILE\)$cc/; s/^AR=\s*/AR= \$\(CROSS_COMPILE\)/; s/^NM=\s*/NM= \$\(CROSS_COMPILE\)/; s/^RANLIB=\s*/RANLIB= \$\(CROSS_COMPILE\)/; s/^MAKEDEPPROG=.*$/MAKEDEPPROG= \$\(CROSS_COMPILE\)$cc/ if $cc eq "gcc"; } else { s/^CC=.*$/CC= $cc/; s/^AR=\s*ar/AR= $ar/; s/^RANLIB=.*/RANLIB= $ranlib/; s/^MAKEDEPPROG=.*$/MAKEDEPPROG= $cc/ if $cc eq "gcc"; s/^MAKEDEPPROG=.*$/MAKEDEPPROG= $cc/ if $ecc eq "gcc" || $ecc eq "clang"; } s/^CFLAG=.*$/CFLAG= $cflags/; s/^DEPFLAG=.*$/DEPFLAG=$depflags/; s/^PEX_LIBS=.*$/PEX_LIBS= $prelflags/; s/^EX_LIBS=.*$/EX_LIBS= $lflags/; s/^EXE_EXT=.*$/EXE_EXT= $exe_ext/; s/^CPUID_OBJ=.*$/CPUID_OBJ= $cpuid_obj/; s/^BN_ASM=.*$/BN_ASM= $bn_obj/; s/^EC_ASM=.*$/EC_ASM= $ec_obj/; s/^DES_ENC=.*$/DES_ENC= $des_obj/; s/^AES_ENC=.*$/AES_ENC= $aes_obj/; s/^BF_ENC=.*$/BF_ENC= $bf_obj/; s/^CAST_ENC=.*$/CAST_ENC= $cast_obj/; s/^RC4_ENC=.*$/RC4_ENC= $rc4_obj/; s/^RC5_ENC=.*$/RC5_ENC= $rc5_obj/; s/^MD5_ASM_OBJ=.*$/MD5_ASM_OBJ= $md5_obj/; s/^SHA1_ASM_OBJ=.*$/SHA1_ASM_OBJ= $sha1_obj/; s/^RMD160_ASM_OBJ=.*$/RMD160_ASM_OBJ= $rmd160_obj/; s/^WP_ASM_OBJ=.*$/WP_ASM_OBJ= $wp_obj/; s/^CMLL_ENC=.*$/CMLL_ENC= $cmll_obj/; s/^MODES_ASM_OBJ.=*$/MODES_ASM_OBJ= $modes_obj/; s/^ENGINES_ASM_OBJ.=*$/ENGINES_ASM_OBJ= $engines_obj/; s/^PERLASM_SCHEME=.*$/PERLASM_SCHEME= $perlasm_scheme/; s/^PROCESSOR=.*/PROCESSOR= $processor/; s/^ARFLAGS=.*/ARFLAGS= $arflags/; s/^PERL=.*/PERL= $perl/; s/^KRB5_INCLUDES=.*/KRB5_INCLUDES=$withargs{"krb5-include"}/; s/^LIBKRB5=.*/LIBKRB5=$withargs{"krb5-lib"}/; s/^LIBZLIB=.*/LIBZLIB=$withargs{"zlib-lib"}/; s/^ZLIB_INCLUDE=.*/ZLIB_INCLUDE=$withargs{"zlib-include"}/; s/^FIPSDIR=.*/FIPSDIR=$fipsdir/; s/^FIPSLIBDIR=.*/FIPSLIBDIR=$fipslibdir/; s/^FIPSCANLIB=.*/FIPSCANLIB=libcrypto/ if $fips; s/^BASEADDR=.*/BASEADDR=$baseaddr/; s/^SHLIB_TARGET=.*/SHLIB_TARGET=$shared_target/; s/^SHLIB_MARK=.*/SHLIB_MARK=$shared_mark/; s/^SHARED_LIBS=.*/SHARED_LIBS=\$(SHARED_CRYPTO) \$(SHARED_SSL)/ if (!$no_shared); if ($shared_extension ne "" && $shared_extension =~ /^\.s([ol])\.[^\.]*$/) { my $sotmp = $1; s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.s$sotmp/; } elsif ($shared_extension ne "" && $shared_extension =~ /^\.[^\.]*\.dylib$/) { s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.dylib/; } elsif ($shared_extension ne "" && $shared_extension =~ /^\.s([ol])\.[^\.]*\.[^\.]*$/) { my $sotmp = $1; s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.s$sotmp.\$(SHLIB_MAJOR) .s$sotmp/; } elsif ($shared_extension ne "" && $shared_extension =~ /^\.[^\.]*\.[^\.]*\.dylib$/) { s/^SHARED_LIBS_LINK_EXTS=.*/SHARED_LIBS_LINK_EXTS=.\$(SHLIB_MAJOR).dylib .dylib/; } s/^SHARED_LDFLAGS=.*/SHARED_LDFLAGS=$shared_ldflag/; print OUT $_."\n"; } close(IN); close(OUT); rename($Makefile,"$Makefile.bak") || die "unable to rename $Makefile\n" if -e $Makefile; rename("$Makefile.new",$Makefile) || die "unable to rename $Makefile.new\n"; print "CC =$cc\n"; print "CFLAG =$cflags\n"; print "EX_LIBS =$lflags\n"; print "CPUID_OBJ =$cpuid_obj\n"; print "BN_ASM =$bn_obj\n"; print "EC_ASM =$ec_obj\n"; print "DES_ENC =$des_obj\n"; print "AES_ENC =$aes_obj\n"; print "BF_ENC =$bf_obj\n"; print "CAST_ENC =$cast_obj\n"; print "RC4_ENC =$rc4_obj\n"; print "RC5_ENC =$rc5_obj\n"; print "MD5_OBJ_ASM =$md5_obj\n"; print "SHA1_OBJ_ASM =$sha1_obj\n"; print "RMD160_OBJ_ASM=$rmd160_obj\n"; print "CMLL_ENC =$cmll_obj\n"; print "MODES_OBJ =$modes_obj\n"; print "ENGINES_OBJ =$engines_obj\n"; print "PROCESSOR =$processor\n"; print "RANLIB =$ranlib\n"; print "ARFLAGS =$arflags\n"; print "PERL =$perl\n"; print "KRB5_INCLUDES =",$withargs{"krb5-include"},"\n" if $withargs{"krb5-include"} ne ""; my $des_ptr=0; my $des_risc1=0; my $des_risc2=0; my $des_unroll=0; my $bn_ll=0; my $def_int=2; my $rc4_int=$def_int; my $md2_int=$def_int; my $idea_int=$def_int; my $rc2_int=$def_int; my $rc4_idx=0; my $rc4_chunk=0; my $bf_ptr=0; my @type=("char","short","int","long"); my ($b64l,$b64,$b32,$b16,$b8)=(0,0,1,0,0); my $export_var_as_fn=0; my $des_int; foreach (sort split(/\s+/,$bn_ops)) { $des_ptr=1 if /DES_PTR/; $des_risc1=1 if /DES_RISC1/; $des_risc2=1 if /DES_RISC2/; $des_unroll=1 if /DES_UNROLL/; $des_int=1 if /DES_INT/; $bn_ll=1 if /BN_LLONG/; $rc4_int=0 if /RC4_CHAR/; $rc4_int=3 if /RC4_LONG/; $rc4_idx=1 if /RC4_INDEX/; $rc4_chunk=1 if /RC4_CHUNK/; $rc4_chunk=2 if /RC4_CHUNK_LL/; $md2_int=0 if /MD2_CHAR/; $md2_int=3 if /MD2_LONG/; $idea_int=1 if /IDEA_SHORT/; $idea_int=3 if /IDEA_LONG/; $rc2_int=1 if /RC2_SHORT/; $rc2_int=3 if /RC2_LONG/; $bf_ptr=1 if $_ eq "BF_PTR"; $bf_ptr=2 if $_ eq "BF_PTR2"; ($b64l,$b64,$b32,$b16,$b8)=(0,1,0,0,0) if /SIXTY_FOUR_BIT/; ($b64l,$b64,$b32,$b16,$b8)=(1,0,0,0,0) if /SIXTY_FOUR_BIT_LONG/; ($b64l,$b64,$b32,$b16,$b8)=(0,0,1,0,0) if /THIRTY_TWO_BIT/; ($b64l,$b64,$b32,$b16,$b8)=(0,0,0,1,0) if /SIXTEEN_BIT/; ($b64l,$b64,$b32,$b16,$b8)=(0,0,0,0,1) if /EIGHT_BIT/; $export_var_as_fn=1 if /EXPORT_VAR_AS_FN/; } open(IN,'crypto/opensslconf.h.new') || die "unable to create crypto/opensslconf.h.new:$!\n"; print OUT "/* opensslconf.h */\n"; print OUT "/* WARNING: Generated automatically from opensslconf.h.in by Configure. */\n\n"; print OUT "#ifdef __cplusplus\n"; print OUT "extern \"C\" {\n"; print OUT "#endif\n"; print OUT "/* OpenSSL was configured with the following options: */\n"; my $openssl_algorithm_defines_trans = $openssl_algorithm_defines; $openssl_experimental_defines =~ s/^\s*#\s*define\s+OPENSSL_NO_(.*)/#ifndef OPENSSL_EXPERIMENTAL_$1\n# ifndef OPENSSL_NO_$1\n# define OPENSSL_NO_$1\n# endif\n#endif/mg; $openssl_algorithm_defines_trans =~ s/^\s*#\s*define\s+OPENSSL_(.*)/# if defined(OPENSSL_$1) \&\& !defined($1)\n# define $1\n# endif/mg; $openssl_algorithm_defines =~ s/^\s*#\s*define\s+(.*)/#ifndef $1\n# define $1\n#endif/mg; $openssl_algorithm_defines = " /* no ciphers excluded */\n" if $openssl_algorithm_defines eq ""; $openssl_thread_defines =~ s/^\s*#\s*define\s+(.*)/#ifndef $1\n# define $1\n#endif/mg; $openssl_sys_defines =~ s/^\s*#\s*define\s+(.*)/#ifndef $1\n# define $1\n#endif/mg; $openssl_other_defines =~ s/^\s*#\s*define\s+(.*)/#ifndef $1\n# define $1\n#endif/mg; print OUT $openssl_sys_defines; print OUT "#ifndef OPENSSL_DOING_MAKEDEPEND\n\n"; print OUT $openssl_experimental_defines; print OUT "\n"; print OUT $openssl_algorithm_defines; print OUT "\n#endif /* OPENSSL_DOING_MAKEDEPEND */\n\n"; print OUT $openssl_thread_defines; print OUT $openssl_other_defines,"\n"; print OUT "/* The OPENSSL_NO_* macros are also defined as NO_* if the application\n"; print OUT " asks for it. This is a transient feature that is provided for those\n"; print OUT " who haven't had the time to do the appropriate changes in their\n"; print OUT " applications. */\n"; print OUT "#ifdef OPENSSL_ALGORITHM_DEFINES\n"; print OUT $openssl_algorithm_defines_trans; print OUT "#endif\n\n"; print OUT "#define OPENSSL_CPUID_OBJ\n\n" if ($cpuid_obj ne "mem_clr.o"); while () { if (/^#define\s+OPENSSLDIR/) { my $foo = $openssldir; $foo =~ s/\\/\\\\/g; print OUT "#define OPENSSLDIR \"$foo\"\n"; } elsif (/^#define\s+ENGINESDIR/) { my $foo = "$prefix/$libdir/engines"; $foo =~ s/\\/\\\\/g; print OUT "#define ENGINESDIR \"$foo\"\n"; } elsif (/^#((define)|(undef))\s+OPENSSL_EXPORT_VAR_AS_FUNCTION/) { printf OUT "#undef OPENSSL_EXPORT_VAR_AS_FUNCTION\n" if $export_var_as_fn; printf OUT "#%s OPENSSL_EXPORT_VAR_AS_FUNCTION\n", ($export_var_as_fn)?"define":"undef"; } elsif (/^#define\s+OPENSSL_UNISTD/) { $unistd = "" if $unistd eq ""; print OUT "#define OPENSSL_UNISTD $unistd\n"; } elsif (/^#((define)|(undef))\s+SIXTY_FOUR_BIT_LONG/) { printf OUT "#%s SIXTY_FOUR_BIT_LONG\n",($b64l)?"define":"undef"; } elsif (/^#((define)|(undef))\s+SIXTY_FOUR_BIT/) { printf OUT "#%s SIXTY_FOUR_BIT\n",($b64)?"define":"undef"; } elsif (/^#((define)|(undef))\s+THIRTY_TWO_BIT/) { printf OUT "#%s THIRTY_TWO_BIT\n",($b32)?"define":"undef"; } elsif (/^#((define)|(undef))\s+SIXTEEN_BIT/) { printf OUT "#%s SIXTEEN_BIT\n",($b16)?"define":"undef"; } elsif (/^#((define)|(undef))\s+EIGHT_BIT/) { printf OUT "#%s EIGHT_BIT\n",($b8)?"define":"undef"; } elsif (/^#((define)|(undef))\s+BN_LLONG\s*$/) { printf OUT "#%s BN_LLONG\n",($bn_ll)?"define":"undef"; } elsif (/^\#define\s+DES_LONG\s+.*/) { printf OUT "#define DES_LONG unsigned %s\n", ($des_int)?'int':'long'; } elsif (/^\#(define|undef)\s+DES_PTR/) { printf OUT "#%s DES_PTR\n",($des_ptr)?'define':'undef'; } elsif (/^\#(define|undef)\s+DES_RISC1/) { printf OUT "#%s DES_RISC1\n",($des_risc1)?'define':'undef'; } elsif (/^\#(define|undef)\s+DES_RISC2/) { printf OUT "#%s DES_RISC2\n",($des_risc2)?'define':'undef'; } elsif (/^\#(define|undef)\s+DES_UNROLL/) { printf OUT "#%s DES_UNROLL\n",($des_unroll)?'define':'undef'; } elsif (/^#define\s+RC4_INT\s/) { printf OUT "#define RC4_INT unsigned %s\n",$type[$rc4_int]; } elsif (/^#undef\s+RC4_CHUNK/) { printf OUT "#undef RC4_CHUNK\n" if $rc4_chunk==0; printf OUT "#define RC4_CHUNK unsigned long\n" if $rc4_chunk==1; printf OUT "#define RC4_CHUNK unsigned long long\n" if $rc4_chunk==2; } elsif (/^#((define)|(undef))\s+RC4_INDEX/) { printf OUT "#%s RC4_INDEX\n",($rc4_idx)?"define":"undef"; } elsif (/^#(define|undef)\s+I386_ONLY/) { printf OUT "#%s I386_ONLY\n", ($processor eq "386")? "define":"undef"; } elsif (/^#define\s+MD2_INT\s/) { printf OUT "#define MD2_INT unsigned %s\n",$type[$md2_int]; } elsif (/^#define\s+IDEA_INT\s/) {printf OUT "#define IDEA_INT unsigned %s\n",$type[$idea_int];} elsif (/^#define\s+RC2_INT\s/) {printf OUT "#define RC2_INT unsigned %s\n",$type[$rc2_int];} elsif (/^#(define|undef)\s+BF_PTR/) { printf OUT "#undef BF_PTR\n" if $bf_ptr == 0; printf OUT "#define BF_PTR\n" if $bf_ptr == 1; printf OUT "#define BF_PTR2\n" if $bf_ptr == 2; } else { print OUT $_; } } close(IN); print OUT "#ifdef __cplusplus\n"; print OUT "}\n"; print OUT "#endif\n"; close(OUT); rename("crypto/opensslconf.h","crypto/opensslconf.h.bak") || die "unable to rename crypto/opensslconf.h\n" if -e "crypto/opensslconf.h"; rename("crypto/opensslconf.h.new","crypto/opensslconf.h") || die "unable to rename crypto/opensslconf.h.new\n"; # Fix the date print "SIXTY_FOUR_BIT_LONG mode\n" if $b64l; print "SIXTY_FOUR_BIT mode\n" if $b64; print "THIRTY_TWO_BIT mode\n" if $b32; print "SIXTEEN_BIT mode\n" if $b16; print "EIGHT_BIT mode\n" if $b8; print "DES_PTR used\n" if $des_ptr; print "DES_RISC1 used\n" if $des_risc1; print "DES_RISC2 used\n" if $des_risc2; print "DES_UNROLL used\n" if $des_unroll; print "DES_INT used\n" if $des_int; print "BN_LLONG mode\n" if $bn_ll; print "RC4 uses u$type[$rc4_int]\n" if $rc4_int != $def_int; print "RC4_INDEX mode\n" if $rc4_idx; print "RC4_CHUNK is undefined\n" if $rc4_chunk==0; print "RC4_CHUNK is unsigned long\n" if $rc4_chunk==1; print "RC4_CHUNK is unsigned long long\n" if $rc4_chunk==2; print "MD2 uses u$type[$md2_int]\n" if $md2_int != $def_int; print "IDEA uses u$type[$idea_int]\n" if $idea_int != $def_int; print "RC2 uses u$type[$rc2_int]\n" if $rc2_int != $def_int; print "BF_PTR used\n" if $bf_ptr == 1; print "BF_PTR2 used\n" if $bf_ptr == 2; if($IsMK1MF) { open (OUT,">crypto/buildinf.h") || die "Can't open buildinf.h"; printf OUT <ms/version32.rc") || die "Can't open ms/version32.rc"; print OUT < LANGUAGE 0x09,0x01 1 VERSIONINFO FILEVERSION $v1,$v2,$v3,$v4 PRODUCTVERSION $v1,$v2,$v3,$v4 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x01L #else FILEFLAGS 0x00L #endif FILEOS VOS__WINDOWS32 FILETYPE VFT_DLL FILESUBTYPE 0x0L BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040904b0" BEGIN // Required: VALUE "CompanyName", "The OpenSSL Project, http://www.openssl.org/\\0" VALUE "FileDescription", "OpenSSL Shared Library\\0" VALUE "FileVersion", "$version\\0" #if defined(CRYPTO) VALUE "InternalName", "libeay32\\0" VALUE "OriginalFilename", "libeay32.dll\\0" #elif defined(SSL) VALUE "InternalName", "ssleay32\\0" VALUE "OriginalFilename", "ssleay32.dll\\0" #endif VALUE "ProductName", "The OpenSSL Toolkit\\0" VALUE "ProductVersion", "$version\\0" // Optional: //VALUE "Comments", "\\0" VALUE "LegalCopyright", "Copyright © 1998-2005 The OpenSSL Project. Copyright © 1995-1998 Eric A. Young, Tim J. Hudson. All rights reserved.\\0" //VALUE "LegalTrademarks", "\\0" //VALUE "PrivateBuild", "\\0" //VALUE "SpecialBuild", "\\0" END END BLOCK "VarFileInfo" BEGIN VALUE "Translation", 0x409, 0x4b0 END END EOF close(OUT); } print < 78) { print STDERR "\n"; $k=length($i); } print STDERR $i . " "; } foreach $i (sort keys %table) { next if $i !~ /^debug/; $k += length($i) + 1; if ($k > 78) { print STDERR "\n"; $k=length($i); } print STDERR $i . " "; } print STDERR "\n\nNOTE: If in doubt, on Unix-ish systems use './config'.\n"; exit(1); } sub which { my($name)=@_; my $path; foreach $path (split /:/, $ENV{PATH}) { if (-f "$path/$name$exe_ext" and -x _) { return "$path/$name$exe_ext" unless ($name eq "perl" and system("$path/$name$exe_ext -e " . '\'exit($]<5.0);\'')); } } } sub dofile { my $f; my $p; my %m; my @a; my $k; my $ff; ($f,$p,%m)=@_; open(IN,"<$f.in") || open(IN,"<$f") || die "unable to open $f:$!\n"; @a=; close(IN); foreach $k (keys %m) { grep(/$k/ && ($_=sprintf($m{$k}."\n",$p)),@a); } open(OUT,">$f.new") || die "unable to open $f.new:$!\n"; print OUT @a; close(OUT); rename($f,"$f.bak") || die "unable to rename $f\n" if -e $f; rename("$f.new",$f) || die "unable to rename $f.new\n"; } sub print_table_entry { my $target = shift; my ($cc, $cflags, $unistd, $thread_cflag, $sys_id, $lflags, $bn_ops, $cpuid_obj, $bn_obj, $ec_obj, $des_obj, $aes_obj, $bf_obj, $md5_obj, $sha1_obj, $cast_obj, $rc4_obj, $rmd160_obj, $rc5_obj, $wp_obj, $cmll_obj, $modes_obj, $engines_obj, $perlasm_scheme, $dso_scheme, $shared_target, $shared_cflag, $shared_ldflag, $shared_extension, $ranlib, $arflags, $multilib)= split(/\s*:\s*/,$table{$target} . ":" x 30 , -1); print </dev/null 2>&1; then \ $(BUILD_CMD); \ fi reflect: @[ -n "$(THIS)" ] && $(CLEARENV) && $(MAKE) $(THIS) -e $(BUILDENV) sub_all: build_all build_all: build_libs build_apps build_tests build_tools build_libs: build_libcrypto build_libssl openssl.pc build_libcrypto: build_crypto build_engines libcrypto.pc build_libssl: build_ssl libssl.pc build_crypto: @dir=crypto; target=all; $(BUILD_ONE_CMD) build_ssl: build_crypto @dir=ssl; target=all; $(BUILD_ONE_CMD) build_engines: build_crypto @dir=engines; target=all; $(BUILD_ONE_CMD) build_apps: build_libs @dir=apps; target=all; $(BUILD_ONE_CMD) build_tests: build_libs @dir=test; target=all; $(BUILD_ONE_CMD) build_tools: build_libs @dir=tools; target=all; $(BUILD_ONE_CMD) all_testapps: build_libs build_testapps build_testapps: @dir=crypto; target=testapps; $(BUILD_ONE_CMD) fips_premain_dso$(EXE_EXT): libcrypto.a [ -z "$(FIPSCANLIB)" ] || $(CC) $(CFLAG) -Iinclude \ -DFINGERPRINT_PREMAIN_DSO_LOAD -o $@ \ $(FIPSLIBDIR)fips_premain.c $(FIPSLIBDIR)fipscanister.o \ libcrypto.a $(EX_LIBS) libcrypto$(SHLIB_EXT): libcrypto.a fips_premain_dso$(EXE_EXT) @if [ "$(SHLIB_TARGET)" != "" ]; then \ if [ "$(FIPSCANLIB)" = "libcrypto" ]; then \ FIPSLD_LIBCRYPTO=libcrypto.a ; \ FIPSLD_CC="$(CC)"; CC=$(FIPSDIR)/bin/fipsld; \ export CC FIPSLD_CC FIPSLD_LIBCRYPTO; \ fi; \ $(MAKE) -e SHLIBDIRS=crypto CC="$${CC:-$(CC)}" build-shared && \ (touch -c fips_premain_dso$(EXE_EXT) || :); \ else \ echo "There's no support for shared libraries on this platform" >&2; \ exit 1; \ fi libssl$(SHLIB_EXT): libcrypto$(SHLIB_EXT) libssl.a @if [ "$(SHLIB_TARGET)" != "" ]; then \ $(MAKE) SHLIBDIRS=ssl SHLIBDEPS='-lcrypto' build-shared; \ else \ echo "There's no support for shared libraries on this platform" >&2; \ exit 1; \ fi clean-shared: @set -e; for i in $(SHLIBDIRS); do \ if [ -n "$(SHARED_LIBS_LINK_EXTS)" ]; then \ tmp="$(SHARED_LIBS_LINK_EXTS)"; \ for j in $${tmp:-x}; do \ ( set -x; rm -f lib$$i$$j ); \ done; \ fi; \ ( set -x; rm -f lib$$i$(SHLIB_EXT) ); \ if expr "$(PLATFORM)" : "Cygwin" >/dev/null; then \ ( set -x; rm -f cyg$$i$(SHLIB_EXT) lib$$i$(SHLIB_EXT).a ); \ fi; \ done link-shared: @ set -e; for i in $(SHLIBDIRS); do \ $(MAKE) -f $(HERE)/Makefile.shared -e $(BUILDENV) \ LIBNAME=$$i LIBVERSION=$(SHLIB_MAJOR).$(SHLIB_MINOR) \ LIBCOMPATVERSIONS=";$(SHLIB_VERSION_HISTORY)" \ symlink.$(SHLIB_TARGET); \ libs="$$libs -l$$i"; \ done build-shared: do_$(SHLIB_TARGET) link-shared do_$(SHLIB_TARGET): @ set -e; libs='-L. $(SHLIBDEPS)'; for i in $(SHLIBDIRS); do \ if [ "$$i" = "ssl" -a -n "$(LIBKRB5)" ]; then \ libs="$(LIBKRB5) $$libs"; \ fi; \ $(CLEARENV) && $(MAKE) -f Makefile.shared -e $(BUILDENV) \ LIBNAME=$$i LIBVERSION=$(SHLIB_MAJOR).$(SHLIB_MINOR) \ LIBCOMPATVERSIONS=";$(SHLIB_VERSION_HISTORY)" \ LIBDEPS="$$libs $(EX_LIBS)" \ link_a.$(SHLIB_TARGET); \ libs="-l$$i $$libs"; \ done libcrypto.pc: Makefile @ ( echo 'prefix=$(INSTALLTOP)'; \ echo 'exec_prefix=$${prefix}'; \ echo 'libdir=$${exec_prefix}/$(LIBDIR)'; \ echo 'includedir=$${prefix}/include'; \ echo ''; \ echo 'Name: OpenSSL-libcrypto'; \ echo 'Description: OpenSSL cryptography library'; \ echo 'Version: '$(VERSION); \ echo 'Requires: '; \ echo 'Libs: -L$${libdir} -lcrypto'; \ echo 'Libs.private: $(EX_LIBS)'; \ echo 'Cflags: -I$${includedir} $(KRB5_INCLUDES)' ) > libcrypto.pc libssl.pc: Makefile @ ( echo 'prefix=$(INSTALLTOP)'; \ echo 'exec_prefix=$${prefix}'; \ echo 'libdir=$${exec_prefix}/$(LIBDIR)'; \ echo 'includedir=$${prefix}/include'; \ echo ''; \ echo 'Name: OpenSSL-libssl'; \ echo 'Description: Secure Sockets Layer and cryptography libraries'; \ echo 'Version: '$(VERSION); \ echo 'Requires.private: libcrypto'; \ echo 'Libs: -L$${libdir} -lssl'; \ echo 'Libs.private: $(EX_LIBS)'; \ echo 'Cflags: -I$${includedir} $(KRB5_INCLUDES)' ) > libssl.pc openssl.pc: Makefile @ ( echo 'prefix=$(INSTALLTOP)'; \ echo 'exec_prefix=$${prefix}'; \ echo 'libdir=$${exec_prefix}/$(LIBDIR)'; \ echo 'includedir=$${prefix}/include'; \ echo ''; \ echo 'Name: OpenSSL'; \ echo 'Description: Secure Sockets Layer and cryptography libraries and tools'; \ echo 'Version: '$(VERSION); \ echo 'Requires: libssl libcrypto' ) > openssl.pc Makefile: Makefile.org Configure config @echo "Makefile is older than Makefile.org, Configure or config." @echo "Reconfigure the source tree (via './config' or 'perl Configure'), please." @false libclean: rm -f *.map *.so *.so.* *.dylib *.dll engines/*.so engines/*.dll engines/*.dylib *.a engines/*.a */lib */*/lib clean: libclean rm -f shlib/*.o *.o core a.out fluff rehash.time testlog make.log cctest cctest.c @set -e; target=clean; $(RECURSIVE_BUILD_CMD) rm -f $(LIBS) rm -f openssl.pc libssl.pc libcrypto.pc rm -f speed.* .pure rm -f $(TARFILE) @set -e; for i in $(ONEDIRS) ;\ do \ rm -fr $$i/*; \ done makefile.one: files $(PERL) util/mk1mf.pl >makefile.one; \ sh util/do_ms.sh files: $(PERL) $(TOP)/util/files.pl Makefile > $(TOP)/MINFO @set -e; target=files; $(RECURSIVE_BUILD_CMD) links: @$(PERL) $(TOP)/util/mkdir-p.pl include/openssl @$(PERL) $(TOP)/util/mklink.pl include/openssl $(EXHEADER) @set -e; target=links; $(RECURSIVE_BUILD_CMD) gentests: @(cd test && echo "generating dummy tests (if needed)..." && \ $(CLEARENV) && $(MAKE) -e $(BUILDENV) TESTS='$(TESTS)' OPENSSL_DEBUG_MEMORY=on generate ); dclean: rm -rf *.bak include/openssl certs/.0 @set -e; target=dclean; $(RECURSIVE_BUILD_CMD) rehash: rehash.time rehash.time: certs apps @if [ -z "$(CROSS_COMPILE)" ]; then \ (OPENSSL="`pwd`/util/opensslwrap.sh"; \ [ -x "apps/openssl.exe" ] && OPENSSL="apps/openssl.exe" || :; \ OPENSSL_DEBUG_MEMORY=on; \ export OPENSSL OPENSSL_DEBUG_MEMORY; \ $(PERL) tools/c_rehash certs/demo) && \ touch rehash.time; \ else :; fi test: tests tests: rehash @(cd test && echo "testing..." && \ $(CLEARENV) && $(MAKE) -e $(BUILDENV) TOP=.. TESTS='$(TESTS)' OPENSSL_DEBUG_MEMORY=on OPENSSL_CONF=../apps/openssl.cnf tests ); OPENSSL_CONF=apps/openssl.cnf util/opensslwrap.sh version -a report: @$(PERL) util/selftest.pl update: errors stacks util/libeay.num util/ssleay.num TABLE @set -e; target=update; $(RECURSIVE_BUILD_CMD) depend: @set -e; target=depend; $(RECURSIVE_BUILD_CMD) lint: @set -e; target=lint; $(RECURSIVE_BUILD_CMD) tags: rm -f TAGS find . -name '[^.]*.[ch]' | xargs etags -a errors: $(PERL) util/ck_errf.pl -strict */*.c */*/*.c $(PERL) util/mkerr.pl -recurse -write (cd engines; $(MAKE) PERL=$(PERL) errors) stacks: $(PERL) util/mkstack.pl -write util/libeay.num:: $(PERL) util/mkdef.pl crypto update util/ssleay.num:: $(PERL) util/mkdef.pl ssl update TABLE: Configure (echo 'Output of `Configure TABLE'"':"; \ $(PERL) Configure TABLE) > TABLE # Build distribution tar-file. As the list of files returned by "find" is # pretty long, on several platforms a "too many arguments" error or similar # would occur. Therefore the list of files is temporarily stored into a file # and read directly, requiring GNU-Tar. Call "make TAR=gtar dist" if the normal # tar does not support the --files-from option. TAR_COMMAND=$(TAR) $(TARFLAGS) --files-from $(TARFILE).list \ --owner 0 --group 0 \ --transform 's|^|$(NAME)/|' \ -cvf - $(TARFILE).list: find * \! -name STATUS \! -name TABLE \! -name '*.o' \! -name '*.a' \ \! -name '*.so' \! -name '*.so.*' \! -name 'openssl' \ \( \! -name '*test' -o -name bctest -o -name pod2mantest \) \ \! -name '.#*' \! -name '*~' \! -type l \ | sort > $(TARFILE).list tar: $(TARFILE).list find . -type d -print | xargs chmod 755 find . -type f -print | xargs chmod a+r find . -type f -perm -0100 -print | xargs chmod a+x $(TAR_COMMAND) | gzip --best > $(TARFILE).gz rm -f $(TARFILE).list ls -l $(TARFILE).gz tar-snap: $(TARFILE).list $(TAR_COMMAND) > $(TARFILE) rm -f $(TARFILE).list ls -l $(TARFILE) dist: $(PERL) Configure dist @$(MAKE) SDIRS='$(SDIRS)' clean @$(MAKE) TAR='$(TAR)' TARFLAGS='$(TARFLAGS)' $(DISTTARVARS) tar install: all install_docs install_sw install_sw: @$(PERL) $(TOP)/util/mkdir-p.pl $(INSTALL_PREFIX)$(INSTALLTOP)/bin \ $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR) \ $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/engines \ $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig \ $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl \ $(INSTALL_PREFIX)$(OPENSSLDIR)/misc \ $(INSTALL_PREFIX)$(OPENSSLDIR)/certs \ $(INSTALL_PREFIX)$(OPENSSLDIR)/private @set -e; headerlist="$(EXHEADER)"; for i in $$headerlist;\ do \ (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ done; @set -e; target=install; $(RECURSIVE_BUILD_CMD) @set -e; liblist="$(LIBS)"; for i in $$liblist ;\ do \ if [ -f "$$i" ]; then \ ( echo installing $$i; \ cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ $(RANLIB) $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i ); \ fi; \ done; @set -e; if [ -n "$(SHARED_LIBS)" ]; then \ tmp="$(SHARED_LIBS)"; \ for i in $${tmp:-x}; \ do \ if [ -f "$$i" -o -f "$$i.a" ]; then \ ( echo installing $$i; \ if expr "$(PLATFORM)" : "Cygwin" >/dev/null; then \ c=`echo $$i | sed 's/^lib\(.*\)\.dll\.a/cyg\1-$(SHLIB_VERSION_NUMBER).dll/'`; \ cp $$c $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$c.new; \ chmod 755 $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$c.new; \ mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$c.new $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$c; \ cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i; \ else \ cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ chmod 555 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new; \ mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i.new $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/$$i; \ fi ); \ if expr $(PLATFORM) : 'mingw' > /dev/null; then \ ( case $$i in \ *crypto*) i=libeay32.dll;; \ *ssl*) i=ssleay32.dll;; \ esac; \ echo installing $$i; \ cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$i.new; \ chmod 755 $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$i.new; \ mv -f $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$i.new $(INSTALL_PREFIX)$(INSTALLTOP)/bin/$$i ); \ fi; \ fi; \ done; \ ( here="`pwd`"; \ cd $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR); \ $(MAKE) -f $$here/Makefile HERE="$$here" link-shared ); \ if [ "$(INSTALLTOP)" != "/usr" ]; then \ echo 'OpenSSL shared libraries have been installed in:'; \ echo ' $(INSTALLTOP)'; \ echo ''; \ sed -e '1,/^$$/d' doc/openssl-shared.txt; \ fi; \ fi cp libcrypto.pc $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig/libcrypto.pc cp libssl.pc $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig/libssl.pc cp openssl.pc $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/$(LIBDIR)/pkgconfig/openssl.pc install_html_docs: here="`pwd`"; \ filecase=; \ case "$(PLATFORM)" in DJGPP|Cygwin*|mingw*|darwin*-*-cc) \ filecase=-i; \ esac; \ for subdir in apps crypto ssl; do \ mkdir -p $(INSTALL_PREFIX)$(HTMLDIR)/$$subdir; \ for i in doc/$$subdir/*.pod; do \ fn=`basename $$i .pod`; \ echo "installing html/$$fn.$(HTMLSUFFIX)"; \ cat $$i \ | sed -r 's/L<([^)]*)(\([0-9]\))?\|([^)]*)(\([0-9]\))?>/L<\1|\3>/g' \ | pod2html --podroot=doc --htmlroot=.. --podpath=apps:crypto:ssl \ | sed -r 's/ $(INSTALL_PREFIX)$(HTMLDIR)/$$subdir/$$fn.$(HTMLSUFFIX); \ $(PERL) util/extract-names.pl < $$i | \ grep -v $$filecase "^$$fn\$$" | \ (cd $(INSTALL_PREFIX)$(HTMLDIR)/$$subdir; \ while read n; do \ PLATFORM=$(PLATFORM) $$here/util/point.sh $$fn.$(HTMLSUFFIX) "$$n".$(HTMLSUFFIX); \ done); \ done; \ done install_docs: @$(PERL) $(TOP)/util/mkdir-p.pl \ $(INSTALL_PREFIX)$(MANDIR)/man1 \ $(INSTALL_PREFIX)$(MANDIR)/man3 \ $(INSTALL_PREFIX)$(MANDIR)/man5 \ $(INSTALL_PREFIX)$(MANDIR)/man7 @pod2man="`cd ./util; ./pod2mantest $(PERL)`"; \ here="`pwd`"; \ filecase=; \ case "$(PLATFORM)" in DJGPP|Cygwin*|mingw*|darwin*-*-cc) \ filecase=-i; \ esac; \ set -e; for i in doc/apps/*.pod; do \ fn=`basename $$i .pod`; \ sec=`$(PERL) util/extract-section.pl 1 < $$i`; \ echo "installing man$$sec/$$fn.$${sec}$(MANSUFFIX)"; \ (cd `$(PERL) util/dirname.pl $$i`; \ sh -c "$$pod2man \ --section=$$sec --center=OpenSSL \ --release=$(VERSION) `basename $$i`") \ > $(INSTALL_PREFIX)$(MANDIR)/man$$sec/$$fn.$${sec}$(MANSUFFIX); \ $(PERL) util/extract-names.pl < $$i | \ (grep -v $$filecase "^$$fn\$$"; true) | \ (grep -v "[ ]"; true) | \ (cd $(INSTALL_PREFIX)$(MANDIR)/man$$sec/; \ while read n; do \ PLATFORM=$(PLATFORM) $$here/util/point.sh $$fn.$${sec}$(MANSUFFIX) "$$n".$${sec}$(MANSUFFIX); \ done); \ done; \ set -e; for i in doc/crypto/*.pod doc/ssl/*.pod; do \ fn=`basename $$i .pod`; \ sec=`$(PERL) util/extract-section.pl 3 < $$i`; \ echo "installing man$$sec/$$fn.$${sec}$(MANSUFFIX)"; \ (cd `$(PERL) util/dirname.pl $$i`; \ sh -c "$$pod2man \ --section=$$sec --center=OpenSSL \ --release=$(VERSION) `basename $$i`") \ > $(INSTALL_PREFIX)$(MANDIR)/man$$sec/$$fn.$${sec}$(MANSUFFIX); \ $(PERL) util/extract-names.pl < $$i | \ (grep -v $$filecase "^$$fn\$$"; true) | \ (grep -v "[ ]"; true) | \ (cd $(INSTALL_PREFIX)$(MANDIR)/man$$sec/; \ while read n; do \ PLATFORM=$(PLATFORM) $$here/util/point.sh $$fn.$${sec}$(MANSUFFIX) "$$n".$${sec}$(MANSUFFIX); \ done); \ done # DO NOT DELETE THIS LINE -- make depend depends on it. Index: vendor-crypto/openssl/dist/Makefile.shared =================================================================== --- vendor-crypto/openssl/dist/Makefile.shared (revision 296272) +++ vendor-crypto/openssl/dist/Makefile.shared (revision 296273) @@ -1,655 +1,655 @@ # # Helper makefile to link shared libraries in a portable way. # This is much simpler than libtool, and hopefully not too error-prone. # # The following variables need to be set on the command line to build # properly # CC contains the current compiler. This one MUST be defined CC=cc CFLAGS=$(CFLAG) # LDFLAGS contains flags to be used when temporary object files (when building # shared libraries) are created, or when an application is linked. # SHARED_LDFLAGS contains flags to be used when the shared library is created. LDFLAGS= SHARED_LDFLAGS= NM=nm # LIBNAME contains just the name of the library, without prefix ("lib" # on Unix, "cyg" for certain forms under Cygwin...) or suffix (.a, .so, # .dll, ...). This one MUST have a value when using this makefile to # build shared libraries. # For example, to build libfoo.so, you need to do the following: #LIBNAME=foo LIBNAME= # APPNAME contains just the name of the application, without suffix ("" # on Unix, ".exe" on Windows, ...). This one MUST have a value when using # this makefile to build applications. # For example, to build foo, you need to do the following: #APPNAME=foo APPNAME= # OBJECTS contains all the object files to link together into the application. # This must contain at least one object file. #OBJECTS=foo.o OBJECTS= # LIBEXTRAS contains extra modules to link together with the library. # For example, if a second library, say libbar.a needs to be linked into # libfoo.so, you need to do the following: #LIBEXTRAS=libbar.a # Note that this MUST be used when using the link_o targets, to hold the # names of all object files that go into the target library. LIBEXTRAS= # LIBVERSION contains the current version of the library. # For example, to build libfoo.so.1.2, you need to do the following: #LIBVERSION=1.2 LIBVERSION= # LIBCOMPATVERSIONS contains the compatibility versions (a list) of # the library. They MUST be in decreasing order. # For example, if libfoo.so.1.2.1 is backward compatible with libfoo.so.1.2 # and libfoo.so.1, you need to do the following: #LIBCOMPATVERSIONS=1.2 1 # Note that on systems that use sonames, the last number will appear as # part of it. # It's also possible, for systems that support it (Tru64, for example), # to add extra compatibility info with more precision, by adding a second # list of versions, separated from the first with a semicolon, like this: #LIBCOMPATVERSIONS=1.2 1;1.2.0 1.1.2 1.1.1 1.1.0 1.0.0 LIBCOMPATVERSIONS= # LIBDEPS contains all the flags necessary to cover all necessary # dependencies to other libraries. LIBDEPS= #------------------------------------------------------------------------------ # The rest is private to this makefile. SET_X=: #SET_X=set -x top: echo "Trying to use this makefile interactively? Don't." CALC_VERSIONS= \ SHLIB_COMPAT=; SHLIB_SOVER=; \ if [ -n "$(LIBVERSION)$(LIBCOMPATVERSIONS)" ]; then \ prev=""; \ for v in `echo "$(LIBVERSION) $(LIBCOMPATVERSIONS)" | cut -d';' -f1`; do \ SHLIB_SOVER_NODOT=$$v; \ SHLIB_SOVER=.$$v; \ if [ -n "$$prev" ]; then \ SHLIB_COMPAT="$$SHLIB_COMPAT .$$prev"; \ fi; \ prev=$$v; \ done; \ fi LINK_APP= \ ( $(SET_X); \ LIBDEPS="$${LIBDEPS:-$(LIBDEPS)}"; \ LDCMD="$${LDCMD:-$(CC)}"; LDFLAGS="$${LDFLAGS:-$(CFLAGS)}"; \ LIBPATH=`for x in $$LIBDEPS; do echo $$x; done | sed -e 's/^ *-L//;t' -e d | uniq`; \ LIBPATH=`echo $$LIBPATH | sed -e 's/ /:/g'`; \ LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \ $${LDCMD} $${LDFLAGS} -o $${APPNAME:=$(APPNAME)} $(OBJECTS) $${LIBDEPS} ) LINK_SO= \ ( $(SET_X); \ LIBDEPS="$${LIBDEPS:-$(LIBDEPS)}"; \ SHAREDCMD="$${SHAREDCMD:-$(CC)}"; \ SHAREDFLAGS="$${SHAREDFLAGS:-$(CFLAGS) $(SHARED_LDFLAGS)}"; \ LIBPATH=`for x in $$LIBDEPS; do echo $$x; done | sed -e 's/^ *-L//;t' -e d | uniq`; \ LIBPATH=`echo $$LIBPATH | sed -e 's/ /:/g'`; \ LD_LIBRARY_PATH=$$LIBPATH:$$LD_LIBRARY_PATH \ $${SHAREDCMD} $${SHAREDFLAGS} \ -o $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX \ $$ALLSYMSFLAGS $$SHOBJECTS $$NOALLSYMSFLAGS $$LIBDEPS \ ) && $(SYMLINK_SO) SYMLINK_SO= \ if [ -n "$$INHIBIT_SYMLINKS" ]; then :; else \ prev=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX; \ if [ -n "$$SHLIB_COMPAT" ]; then \ for x in $$SHLIB_COMPAT; do \ ( $(SET_X); rm -f $$SHLIB$$x$$SHLIB_SUFFIX; \ ln -s $$prev $$SHLIB$$x$$SHLIB_SUFFIX ); \ prev=$$SHLIB$$x$$SHLIB_SUFFIX; \ done; \ fi; \ if [ -n "$$SHLIB_SOVER" ]; then \ ( $(SET_X); rm -f $$SHLIB$$SHLIB_SUFFIX; \ ln -s $$prev $$SHLIB$$SHLIB_SUFFIX ); \ fi; \ fi LINK_SO_A= SHOBJECTS="lib$(LIBNAME).a $(LIBEXTRAS)"; $(LINK_SO) LINK_SO_O= SHOBJECTS="$(LIBEXTRAS)"; $(LINK_SO) LINK_SO_A_VIA_O= \ SHOBJECTS=lib$(LIBNAME).o; \ ALL=$$ALLSYMSFLAGS; ALLSYMSFLAGS=; NOALLSYMSFLAGS=; \ ( $(SET_X); \ ld $(LDFLAGS) -r -o lib$(LIBNAME).o $$ALL lib$(LIBNAME).a $(LIBEXTRAS) ); \ $(LINK_SO) && rm -f lib$(LIBNAME).o LINK_SO_A_UNPACKED= \ UNPACKDIR=link_tmp.$$$$; rm -rf $$UNPACKDIR; mkdir $$UNPACKDIR; \ (cd $$UNPACKDIR; ar x ../lib$(LIBNAME).a) && \ ([ -z "$(LIBEXTRAS)" ] || cp $(LIBEXTRAS) $$UNPACKDIR) && \ SHOBJECTS=$$UNPACKDIR/*.o; \ $(LINK_SO) && rm -rf $$UNPACKDIR DETECT_GNU_LD=($(CC) -Wl,-V /dev/null 2>&1 | grep '^GNU ld' )>/dev/null DO_GNU_SO=$(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS='-Wl,--whole-archive'; \ NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-Bsymbolic -Wl,-soname=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX" DO_GNU_APP=LDFLAGS="$(CFLAGS) -Wl,-rpath,$(LIBRPATH)" #This is rather special. It's a special target with which one can link #applications without bothering with any features that have anything to #do with shared libraries, for example when linking against static #libraries. It's mostly here to avoid a lot of conditionals everywhere #else... link_app.: $(LINK_APP) link_o.gnu: @ $(DO_GNU_SO); $(LINK_SO_O) link_a.gnu: @ $(DO_GNU_SO); $(LINK_SO_A) link_app.gnu: @ $(DO_GNU_APP); $(LINK_APP) DO_BEOS_SO= SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS='-Wl,--whole-archive'; \ NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-Bsymbolic -Wl,-soname=$$SHLIB$$SHLIB_SUFFIX" link_o.beos: @ $(DO_BEOS_SO); $(LINK_SO_O) link_a.beos: @ $(DO_BEOS_SO); $(LINK_SO_A) link_o.bsd: @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ LIBDEPS=" "; \ ALLSYMSFLAGS="-Wl,-Bforcearchive"; \ NOALLSYMSFLAGS=; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -nostdlib"; \ fi; $(LINK_SO_O) link_a.bsd: @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ LIBDEPS=" "; \ ALLSYMSFLAGS="-Wl,-Bforcearchive"; \ NOALLSYMSFLAGS=; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -nostdlib"; \ fi; $(LINK_SO_A) link_app.bsd: @if $(DETECT_GNU_LD); then $(DO_GNU_APP); else \ LDFLAGS="$(CFLAGS) -Wl,-rpath,$(LIBPATH)"; \ fi; $(LINK_APP) # For Darwin AKA Mac OS/X (dyld) # Originally link_o.darwin produced .so, because it was hard-coded # in dso_dlfcn module. At later point dso_dlfcn switched to .dylib # extension in order to allow for run-time linking with vendor- # supplied shared libraries such as libz, so that link_o.darwin had # to be harmonized with it. This caused minor controversy, because # it was believed that dlopen can't be used to dynamically load # .dylib-s, only so called bundle modules (ones linked with -bundle # flag). The belief seems to be originating from pre-10.4 release, # where dlfcn functionality was emulated by dlcompat add-on. In # 10.4 dlopen was rewritten as native part of dyld and is documented # to be capable of loading both dynamic libraries and bundles. In # order to provide compatibility with pre-10.4 dlopen, modules are # linked with -bundle flag, which makes .dylib extension misleading. # It works, because dlopen is [and always was] extension-agnostic. # Alternative to this heuristic approach is to develop specific # MacOS X dso module relying on whichever "native" dyld interface. link_o.darwin: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME); \ SHLIB_SUFFIX=.dylib; \ ALLSYMSFLAGS='-all_load'; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) `echo $(SHARED_LDFLAGS) | sed s/dynamiclib/bundle/`"; \ if [ -n "$(LIBVERSION)" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -current_version $(LIBVERSION)"; \ fi; \ if [ -n "$$SHLIB_SOVER_NODOT" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \ fi; \ $(LINK_SO_O) link_a.darwin: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME); \ SHLIB_SUFFIX=.dylib; \ ALLSYMSFLAGS='-all_load'; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS)"; \ if [ -n "$(LIBVERSION)" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -current_version $(LIBVERSION)"; \ fi; \ if [ -n "$$SHLIB_SOVER_NODOT" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -compatibility_version $$SHLIB_SOVER_NODOT"; \ fi; \ SHAREDFLAGS="$$SHAREDFLAGS -install_name $(INSTALLTOP)/$(LIBDIR)/$$SHLIB$(SHLIB_EXT)"; \ $(LINK_SO_A) link_app.darwin: # is there run-path on darwin? $(LINK_APP) link_o.cygwin: @ $(CALC_VERSIONS); \ INHIBIT_SYMLINKS=yes; \ SHLIB=cyg$(LIBNAME); \ base=-Wl,--enable-auto-image-base; \ deffile=; \ if expr $(PLATFORM) : 'mingw' > /dev/null; then \ SHLIB=$(LIBNAME)eay32; base=; \ if test -f $(LIBNAME)eay32.def; then \ deffile=$(LIBNAME)eay32.def; \ fi; \ fi; \ SHLIB_SUFFIX=.dll; \ LIBVERSION="$(LIBVERSION)"; \ SHLIB_SOVER=${LIBVERSION:+"-$(LIBVERSION)"}; \ ALLSYMSFLAGS='-Wl,--whole-archive'; \ NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \ - SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base $$deffile -Wl,-s,-Bsymbolic"; \ + SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base $$deffile -Wl,-Bsymbolic"; \ $(LINK_SO_O) #for mingw target if def-file is in use dll-name should match library-name link_a.cygwin: @ $(CALC_VERSIONS); \ INHIBIT_SYMLINKS=yes; \ SHLIB=cyg$(LIBNAME); SHLIB_SOVER=-$(LIBVERSION); SHLIB_SUFFIX=.dll; \ dll_name=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX; extras=; \ base=-Wl,--enable-auto-image-base; \ if expr $(PLATFORM) : 'mingw' > /dev/null; then \ case $(LIBNAME) in \ crypto) SHLIB=libeay;; \ ssl) SHLIB=ssleay;; \ esac; \ SHLIB_SOVER=32; \ extras="$(LIBNAME).def"; \ $(PERL) util/mkdef.pl 32 $$SHLIB > $$extras; \ - base=; [ $(LIBNAME) = "crypto" ] && base=-Wl,--image-base,0x63000000; \ + base=; [ $(LIBNAME) = "crypto" -a -n "$(FIPSCANLIB)" ] && base=-Wl,--image-base,0x63000000; \ fi; \ dll_name=$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX; \ $(PERL) util/mkrc.pl $$dll_name | \ $(CROSS_COMPILE)windres -o rc.o; \ extras="$$extras rc.o"; \ ALLSYMSFLAGS='-Wl,--whole-archive'; \ NOALLSYMSFLAGS='-Wl,--no-whole-archive'; \ - SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base -Wl,-s,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a $$extras"; \ + SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared $$base -Wl,-Bsymbolic -Wl,--out-implib,lib$(LIBNAME).dll.a $$extras"; \ [ -f apps/$$dll_name ] && rm apps/$$dll_name; \ [ -f test/$$dll_name ] && rm test/$$dll_name; \ $(LINK_SO_A) || exit 1; \ rm $$extras; \ cp -p $$dll_name apps/; \ cp -p $$dll_name test/ link_app.cygwin: @if expr "$(CFLAGS)" : '.*OPENSSL_USE_APPLINK' > /dev/null; then \ LIBDEPS="$(TOP)/crypto/applink.o $${LIBDEPS:-$(LIBDEPS)}"; \ export LIBDEPS; \ fi; \ $(LINK_APP) link_o.alpha-osf1: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \ if [ -n "$$SHLIB_HIST" ]; then \ SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \ else \ SHLIB_HIST="$(LIBVERSION)"; \ fi; \ SHLIB_SOVER=; \ ALLSYMSFLAGS='-all'; \ NOALLSYMSFLAGS='-none'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-B,symbolic"; \ if [ -n "$$SHLIB_HIST" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -set_version $$SHLIB_HIST"; \ fi; \ fi; \ $(LINK_SO_O) link_a.alpha-osf1: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ SHLIB_HIST=`echo "$(LIBCOMPATVERSIONS)" | cut -d';' -f2 | sed -e 's/ */:/'`; \ if [ -n "$$SHLIB_HIST" ]; then \ SHLIB_HIST="$${SHLIB_HIST}:$(LIBVERSION)"; \ else \ SHLIB_HIST="$(LIBVERSION)"; \ fi; \ SHLIB_SOVER=; \ ALLSYMSFLAGS='-all'; \ NOALLSYMSFLAGS='-none'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-B,symbolic"; \ if [ -n "$$SHLIB_HIST" ]; then \ SHAREDFLAGS="$$SHAREDFLAGS -set_version $$SHLIB_HIST"; \ fi; \ fi; \ $(LINK_SO_A) link_app.alpha-osf1: @if $(DETECT_GNU_LD); then \ $(DO_GNU_APP); \ else \ LDFLAGS="$(CFLAGS) -rpath $(LIBRPATH)"; \ fi; \ $(LINK_APP) link_o.solaris: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ MINUSZ='-z '; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && MINUSZ='-Wl,-z,'; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS="$${MINUSZ}allextract"; \ NOALLSYMSFLAGS="$${MINUSZ}defaultextract"; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX -Wl,-Bsymbolic"; \ fi; \ $(LINK_SO_O) link_a.solaris: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ MINUSZ='-z '; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && MINUSZ='-Wl,-z,'; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=;\ ALLSYMSFLAGS="$${MINUSZ}allextract"; \ NOALLSYMSFLAGS="$${MINUSZ}defaultextract"; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX -Wl,-Bsymbolic"; \ fi; \ $(LINK_SO_A) link_app.solaris: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_APP); \ else \ LDFLAGS="$(CFLAGS) -R $(LIBRPATH)"; \ fi; \ $(LINK_APP) # OpenServer 5 native compilers used link_o.svr3: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=''; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) -G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX"; \ fi; \ $(LINK_SO_O) link_a.svr3: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=''; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) -G -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX"; \ fi; \ $(LINK_SO_A_UNPACKED) link_app.svr3: @$(DETECT_GNU_LD) && $(DO_GNU_APP); \ $(LINK_APP) # UnixWare 7 and OpenUNIX 8 native compilers used link_o.svr5: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHARE_FLAG='-G'; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && SHARE_FLAG='-shared'; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=''; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) $${SHARE_FLAG} -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX"; \ fi; \ $(LINK_SO_O) link_a.svr5: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHARE_FLAG='-G'; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && SHARE_FLAG='-shared'; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=''; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS="$(CFLAGS) $${SHARE_FLAG} -h $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX"; \ fi; \ $(LINK_SO_A_UNPACKED) link_app.svr5: @$(DETECT_GNU_LD) && $(DO_GNU_APP); \ $(LINK_APP) link_o.irix: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ MINUSWL=""; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && MINUSWL="-Wl,"; \ ALLSYMSFLAGS="$${MINUSWL}-all"; \ NOALLSYMSFLAGS="$${MINUSWL}-none"; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX,-B,symbolic"; \ fi; \ $(LINK_SO_O) link_a.irix: @ if $(DETECT_GNU_LD); then \ $(DO_GNU_SO); \ else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ MINUSWL=""; \ ($(CC) -v 2>&1 | grep gcc) > /dev/null && MINUSWL="-Wl,"; \ ALLSYMSFLAGS="$${MINUSWL}-all"; \ NOALLSYMSFLAGS="$${MINUSWL}-none"; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -shared -Wl,-soname,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX,-B,symbolic"; \ fi; \ $(LINK_SO_A) link_app.irix: @LDFLAGS="$(CFLAGS) -Wl,-rpath,$(LIBRPATH)"; \ $(LINK_APP) # 32-bit PA-RISC HP-UX embeds the -L pathname of libs we link with, so # we compensate for it with +cdp ../: and +cdp ./:. Yes, these rewrite # rules imply that we can only link one level down in catalog structure, # but that's what takes place for the moment of this writing. +cdp option # was introduced in HP-UX 11.x and applies in 32-bit PA-RISC link # editor context only [it's simply ignored in other cases, which are all # ELFs by the way]. # link_o.hpux: @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).sl; \ expr "$(CFLAGS)" : '.*DSO_DLFCN' > /dev/null && SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS='-Wl,-Fl'; \ NOALLSYMSFLAGS=''; \ expr $(PLATFORM) : 'hpux64' > /dev/null && ALLSYMSFLAGS='-Wl,+forceload'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-B,symbolic,+vnocompatwarnings,-z,+s,+h,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX,+cdp,../:,+cdp,./:"; \ fi; \ rm -f $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX || :; \ $(LINK_SO_O) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX link_a.hpux: @if $(DETECT_GNU_LD); then $(DO_GNU_SO); else \ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).sl; \ expr $(PLATFORM) : '.*ia64' > /dev/null && SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS='-Wl,-Fl'; \ NOALLSYMSFLAGS=''; \ expr $(PLATFORM) : 'hpux64' > /dev/null && ALLSYMSFLAGS='-Wl,+forceload'; \ SHAREDFLAGS="$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-B,symbolic,+vnocompatwarnings,-z,+s,+h,$$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX,+cdp,../:,+cdp,./:"; \ fi; \ rm -f $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX || :; \ $(LINK_SO_A) && chmod a=rx $$SHLIB$$SHLIB_SOVER$$SHLIB_SUFFIX link_app.hpux: @if $(DETECT_GNU_LD); then $(DO_GNU_APP); else \ LDFLAGS="$(CFLAGS) -Wl,+s,+cdp,../:,+cdp,./:,+b,$(LIBRPATH)"; \ fi; \ $(LINK_APP) link_o.aix: @ $(CALC_VERSIONS); \ OBJECT_MODE=`expr "x$(SHARED_LDFLAGS)" : 'x\-[a-z]*\(64\)'` || :; \ OBJECT_MODE=$${OBJECT_MODE:-32}; export OBJECT_MODE; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=''; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS='$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-bexpall,-bnolibpath,-bM:SRE'; \ $(LINK_SO_O); link_a.aix: @ $(CALC_VERSIONS); \ OBJECT_MODE=`expr "x$(SHARED_LDFLAGS)" : 'x\-[a-z]*\(64\)'` || : ; \ OBJECT_MODE=$${OBJECT_MODE:-32}; export OBJECT_MODE; \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS='-bnogc'; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS='$(CFLAGS) $(SHARED_LDFLAGS) -Wl,-bexpall,-bnolibpath,-bM:SRE'; \ $(LINK_SO_A_VIA_O) link_app.aix: LDFLAGS="$(CFLAGS) -Wl,-brtl,-blibpath:$(LIBRPATH):$${LIBPATH:-/usr/lib:/lib}"; \ $(LINK_APP) link_o.reliantunix: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS='$(CFLAGS) -G'; \ $(LINK_SO_O) link_a.reliantunix: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ SHLIB_SUFFIX=; \ ALLSYMSFLAGS=; \ NOALLSYMSFLAGS=''; \ SHAREDFLAGS='$(CFLAGS) -G'; \ $(LINK_SO_A_UNPACKED) link_app.reliantunix: $(LINK_APP) # Targets to build symbolic links when needed symlink.gnu symlink.solaris symlink.svr3 symlink.svr5 symlink.irix \ symlink.aix symlink.reliantunix: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).so; \ $(SYMLINK_SO) symlink.darwin: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME); \ SHLIB_SUFFIX=.dylib; \ $(SYMLINK_SO) symlink.hpux: @ $(CALC_VERSIONS); \ SHLIB=lib$(LIBNAME).sl; \ expr $(PLATFORM) : '.*ia64' > /dev/null && SHLIB=lib$(LIBNAME).so; \ $(SYMLINK_SO) # The following lines means those specific architectures do no symlinks symlink.cygwin symlink.alpha-osf1 symlink.tru64 symlink.tru64-rpath symlink.beos: # Compatibility targets link_o.bsd-gcc-shared link_o.linux-shared link_o.gnu-shared: link_o.gnu link_a.bsd-gcc-shared link_a.linux-shared link_a.gnu-shared: link_a.gnu link_app.bsd-gcc-shared link_app.linux-shared link_app.gnu-shared: link_app.gnu symlink.bsd-gcc-shared symlink.bsd-shared symlink.linux-shared symlink.gnu-shared: symlink.gnu link_o.bsd-shared: link_o.bsd link_a.bsd-shared: link_a.bsd link_app.bsd-shared: link_app.bsd link_o.darwin-shared: link_o.darwin link_a.darwin-shared: link_a.darwin link_app.darwin-shared: link_app.darwin symlink.darwin-shared: symlink.darwin link_o.cygwin-shared: link_o.cygwin link_a.cygwin-shared: link_a.cygwin link_app.cygwin-shared: link_app.cygwin symlink.cygwin-shared: symlink.cygwin link_o.alpha-osf1-shared: link_o.alpha-osf1 link_a.alpha-osf1-shared: link_a.alpha-osf1 link_app.alpha-osf1-shared: link_app.alpha-osf1 symlink.alpha-osf1-shared: symlink.alpha-osf1 link_o.tru64-shared: link_o.tru64 link_a.tru64-shared: link_a.tru64 link_app.tru64-shared: link_app.tru64 symlink.tru64-shared: symlink.tru64 link_o.tru64-shared-rpath: link_o.tru64-rpath link_a.tru64-shared-rpath: link_a.tru64-rpath link_app.tru64-shared-rpath: link_app.tru64-rpath symlink.tru64-shared-rpath: symlink.tru64-rpath link_o.solaris-shared: link_o.solaris link_a.solaris-shared: link_a.solaris link_app.solaris-shared: link_app.solaris symlink.solaris-shared: symlink.solaris link_o.svr3-shared: link_o.svr3 link_a.svr3-shared: link_a.svr3 link_app.svr3-shared: link_app.svr3 symlink.svr3-shared: symlink.svr3 link_o.svr5-shared: link_o.svr5 link_a.svr5-shared: link_a.svr5 link_app.svr5-shared: link_app.svr5 symlink.svr5-shared: symlink.svr5 link_o.irix-shared: link_o.irix link_a.irix-shared: link_a.irix link_app.irix-shared: link_app.irix symlink.irix-shared: symlink.irix link_o.hpux-shared: link_o.hpux link_a.hpux-shared: link_a.hpux link_app.hpux-shared: link_app.hpux symlink.hpux-shared: symlink.hpux link_o.aix-shared: link_o.aix link_a.aix-shared: link_a.aix link_app.aix-shared: link_app.aix symlink.aix-shared: symlink.aix link_o.reliantunix-shared: link_o.reliantunix link_a.reliantunix-shared: link_a.reliantunix link_app.reliantunix-shared: link_app.reliantunix symlink.reliantunix-shared: symlink.reliantunix link_o.beos-shared: link_o.beos link_a.beos-shared: link_a.beos link_app.beos-shared: link_app.gnu symlink.beos-shared: symlink.beos Index: vendor-crypto/openssl/dist/NEWS =================================================================== --- vendor-crypto/openssl/dist/NEWS (revision 296272) +++ vendor-crypto/openssl/dist/NEWS (revision 296273) @@ -1,749 +1,762 @@ NEWS ==== This file gives a brief overview of the major changes between each OpenSSL release. For more details please read the CHANGES file. + Major changes between OpenSSL 1.0.2f and OpenSSL 1.0.2g [1 Mar 2016] + + o Disable weak ciphers in SSLv3 and up in default builds of OpenSSL. + o Disable SSLv2 default build, default negotiation and weak ciphers + (CVE-2016-0800) + o Fix a double-free in DSA code (CVE-2016-0705) + o Disable SRP fake user seed to address a server memory leak + (CVE-2016-0798) + o Fix BN_hex2bn/BN_dec2bn NULL pointer deref/heap corruption + (CVE-2016-0797) + o Fix memory issues in BIO_*printf functions (CVE-2016-0799) + o Fix side channel attack on modular exponentiation (CVE-2016-0702) + Major changes between OpenSSL 1.0.2e and OpenSSL 1.0.2f [28 Jan 2016] o DH small subgroups (CVE-2016-0701) o SSLv2 doesn't block disabled ciphers (CVE-2015-3197) Major changes between OpenSSL 1.0.2d and OpenSSL 1.0.2e [3 Dec 2015] o BN_mod_exp may produce incorrect results on x86_64 (CVE-2015-3193) o Certificate verify crash with missing PSS parameter (CVE-2015-3194) o X509_ATTRIBUTE memory leak (CVE-2015-3195) o Rewrite EVP_DecodeUpdate (base64 decoding) to fix several bugs o In DSA_generate_parameters_ex, if the provided seed is too short, return an error Major changes between OpenSSL 1.0.2c and OpenSSL 1.0.2d [9 Jul 2015] o Alternate chains certificate forgery (CVE-2015-1793) o Race condition handling PSK identify hint (CVE-2015-3196) Major changes between OpenSSL 1.0.2b and OpenSSL 1.0.2c [12 Jun 2015] o Fix HMAC ABI incompatibility Major changes between OpenSSL 1.0.2a and OpenSSL 1.0.2b [11 Jun 2015] o Malformed ECParameters causes infinite loop (CVE-2015-1788) o Exploitable out-of-bounds read in X509_cmp_time (CVE-2015-1789) o PKCS7 crash with missing EnvelopedContent (CVE-2015-1790) o CMS verify infinite loop with unknown hash function (CVE-2015-1792) o Race condition handling NewSessionTicket (CVE-2015-1791) Major changes between OpenSSL 1.0.2 and OpenSSL 1.0.2a [19 Mar 2015] o OpenSSL 1.0.2 ClientHello sigalgs DoS fix (CVE-2015-0291) o Multiblock corrupted pointer fix (CVE-2015-0290) o Segmentation fault in DTLSv1_listen fix (CVE-2015-0207) o Segmentation fault in ASN1_TYPE_cmp fix (CVE-2015-0286) o Segmentation fault for invalid PSS parameters fix (CVE-2015-0208) o ASN.1 structure reuse memory corruption fix (CVE-2015-0287) o PKCS7 NULL pointer dereferences fix (CVE-2015-0289) o DoS via reachable assert in SSLv2 servers fix (CVE-2015-0293) o Empty CKE with client auth and DHE fix (CVE-2015-1787) o Handshake with unseeded PRNG fix (CVE-2015-0285) o Use After Free following d2i_ECPrivatekey error fix (CVE-2015-0209) o X509_to_X509_REQ NULL pointer deref fix (CVE-2015-0288) o Removed the export ciphers from the DEFAULT ciphers Major changes between OpenSSL 1.0.1l and OpenSSL 1.0.2 [22 Jan 2015]: o Suite B support for TLS 1.2 and DTLS 1.2 o Support for DTLS 1.2 o TLS automatic EC curve selection. o API to set TLS supported signature algorithms and curves o SSL_CONF configuration API. o TLS Brainpool support. o ALPN support. o CMS support for RSA-PSS, RSA-OAEP, ECDH and X9.42 DH. Major changes between OpenSSL 1.0.1k and OpenSSL 1.0.1l [15 Jan 2015] o Build fixes for the Windows and OpenVMS platforms Major changes between OpenSSL 1.0.1j and OpenSSL 1.0.1k [8 Jan 2015] o Fix for CVE-2014-3571 o Fix for CVE-2015-0206 o Fix for CVE-2014-3569 o Fix for CVE-2014-3572 o Fix for CVE-2015-0204 o Fix for CVE-2015-0205 o Fix for CVE-2014-8275 o Fix for CVE-2014-3570 Major changes between OpenSSL 1.0.1i and OpenSSL 1.0.1j [15 Oct 2014] o Fix for CVE-2014-3513 o Fix for CVE-2014-3567 o Mitigation for CVE-2014-3566 (SSL protocol vulnerability) o Fix for CVE-2014-3568 Major changes between OpenSSL 1.0.1h and OpenSSL 1.0.1i [6 Aug 2014] o Fix for CVE-2014-3512 o Fix for CVE-2014-3511 o Fix for CVE-2014-3510 o Fix for CVE-2014-3507 o Fix for CVE-2014-3506 o Fix for CVE-2014-3505 o Fix for CVE-2014-3509 o Fix for CVE-2014-5139 o Fix for CVE-2014-3508 Major changes between OpenSSL 1.0.1g and OpenSSL 1.0.1h [5 Jun 2014] o Fix for CVE-2014-0224 o Fix for CVE-2014-0221 o Fix for CVE-2014-0198 o Fix for CVE-2014-0195 o Fix for CVE-2014-3470 o Fix for CVE-2010-5298 Major changes between OpenSSL 1.0.1f and OpenSSL 1.0.1g [7 Apr 2014] o Fix for CVE-2014-0160 o Add TLS padding extension workaround for broken servers. o Fix for CVE-2014-0076 Major changes between OpenSSL 1.0.1e and OpenSSL 1.0.1f [6 Jan 2014] o Don't include gmt_unix_time in TLS server and client random values o Fix for TLS record tampering bug CVE-2013-4353 o Fix for TLS version checking bug CVE-2013-6449 o Fix for DTLS retransmission bug CVE-2013-6450 Major changes between OpenSSL 1.0.1d and OpenSSL 1.0.1e [11 Feb 2013]: o Corrected fix for CVE-2013-0169 Major changes between OpenSSL 1.0.1c and OpenSSL 1.0.1d [4 Feb 2013]: o Fix renegotiation in TLS 1.1, 1.2 by using the correct TLS version. o Include the fips configuration module. o Fix OCSP bad key DoS attack CVE-2013-0166 o Fix for SSL/TLS/DTLS CBC plaintext recovery attack CVE-2013-0169 o Fix for TLS AESNI record handling flaw CVE-2012-2686 Major changes between OpenSSL 1.0.1b and OpenSSL 1.0.1c [10 May 2012]: o Fix TLS/DTLS record length checking bug CVE-2012-2333 o Don't attempt to use non-FIPS composite ciphers in FIPS mode. Major changes between OpenSSL 1.0.1a and OpenSSL 1.0.1b [26 Apr 2012]: o Fix compilation error on non-x86 platforms. o Make FIPS capable OpenSSL ciphers work in non-FIPS mode. o Fix SSL_OP_NO_TLSv1_1 clash with SSL_OP_ALL in OpenSSL 1.0.0 Major changes between OpenSSL 1.0.1 and OpenSSL 1.0.1a [19 Apr 2012]: o Fix for ASN1 overflow bug CVE-2012-2110 o Workarounds for some servers that hang on long client hellos. o Fix SEGV in AES code. Major changes between OpenSSL 1.0.0h and OpenSSL 1.0.1 [14 Mar 2012]: o TLS/DTLS heartbeat support. o SCTP support. o RFC 5705 TLS key material exporter. o RFC 5764 DTLS-SRTP negotiation. o Next Protocol Negotiation. o PSS signatures in certificates, requests and CRLs. o Support for password based recipient info for CMS. o Support TLS v1.2 and TLS v1.1. o Preliminary FIPS capability for unvalidated 2.0 FIPS module. o SRP support. Major changes between OpenSSL 1.0.0g and OpenSSL 1.0.0h [12 Mar 2012]: o Fix for CMS/PKCS#7 MMA CVE-2012-0884 o Corrected fix for CVE-2011-4619 o Various DTLS fixes. Major changes between OpenSSL 1.0.0f and OpenSSL 1.0.0g [18 Jan 2012]: o Fix for DTLS DoS issue CVE-2012-0050 Major changes between OpenSSL 1.0.0e and OpenSSL 1.0.0f [4 Jan 2012]: o Fix for DTLS plaintext recovery attack CVE-2011-4108 o Clear block padding bytes of SSL 3.0 records CVE-2011-4576 o Only allow one SGC handshake restart for SSL/TLS CVE-2011-4619 o Check parameters are not NULL in GOST ENGINE CVE-2012-0027 o Check for malformed RFC3779 data CVE-2011-4577 Major changes between OpenSSL 1.0.0d and OpenSSL 1.0.0e [6 Sep 2011]: o Fix for CRL vulnerability issue CVE-2011-3207 o Fix for ECDH crashes CVE-2011-3210 o Protection against EC timing attacks. o Support ECDH ciphersuites for certificates using SHA2 algorithms. o Various DTLS fixes. Major changes between OpenSSL 1.0.0c and OpenSSL 1.0.0d [8 Feb 2011]: o Fix for security issue CVE-2011-0014 Major changes between OpenSSL 1.0.0b and OpenSSL 1.0.0c [2 Dec 2010]: o Fix for security issue CVE-2010-4180 o Fix for CVE-2010-4252 o Fix mishandling of absent EC point format extension. o Fix various platform compilation issues. o Corrected fix for security issue CVE-2010-3864. Major changes between OpenSSL 1.0.0a and OpenSSL 1.0.0b [16 Nov 2010]: o Fix for security issue CVE-2010-3864. o Fix for CVE-2010-2939 o Fix WIN32 build system for GOST ENGINE. Major changes between OpenSSL 1.0.0 and OpenSSL 1.0.0a [1 Jun 2010]: o Fix for security issue CVE-2010-1633. o GOST MAC and CFB fixes. Major changes between OpenSSL 0.9.8n and OpenSSL 1.0.0 [29 Mar 2010]: o RFC3280 path validation: sufficient to process PKITS tests. o Integrated support for PVK files and keyblobs. o Change default private key format to PKCS#8. o CMS support: able to process all examples in RFC4134 o Streaming ASN1 encode support for PKCS#7 and CMS. o Multiple signer and signer add support for PKCS#7 and CMS. o ASN1 printing support. o Whirlpool hash algorithm added. o RFC3161 time stamp support. o New generalised public key API supporting ENGINE based algorithms. o New generalised public key API utilities. o New ENGINE supporting GOST algorithms. o SSL/TLS GOST ciphersuite support. o PKCS#7 and CMS GOST support. o RFC4279 PSK ciphersuite support. o Supported points format extension for ECC ciphersuites. o ecdsa-with-SHA224/256/384/512 signature types. o dsa-with-SHA224 and dsa-with-SHA256 signature types. o Opaque PRF Input TLS extension support. o Updated time routines to avoid OS limitations. Major changes between OpenSSL 0.9.8m and OpenSSL 0.9.8n [24 Mar 2010]: o CFB cipher definition fixes. o Fix security issues CVE-2010-0740 and CVE-2010-0433. Major changes between OpenSSL 0.9.8l and OpenSSL 0.9.8m [25 Feb 2010]: o Cipher definition fixes. o Workaround for slow RAND_poll() on some WIN32 versions. o Remove MD2 from algorithm tables. o SPKAC handling fixes. o Support for RFC5746 TLS renegotiation extension. o Compression memory leak fixed. o Compression session resumption fixed. o Ticket and SNI coexistence fixes. o Many fixes to DTLS handling. Major changes between OpenSSL 0.9.8k and OpenSSL 0.9.8l [5 Nov 2009]: o Temporary work around for CVE-2009-3555: disable renegotiation. Major changes between OpenSSL 0.9.8j and OpenSSL 0.9.8k [25 Mar 2009]: o Fix various build issues. o Fix security issues (CVE-2009-0590, CVE-2009-0591, CVE-2009-0789) Major changes between OpenSSL 0.9.8i and OpenSSL 0.9.8j [7 Jan 2009]: o Fix security issue (CVE-2008-5077) o Merge FIPS 140-2 branch code. Major changes between OpenSSL 0.9.8g and OpenSSL 0.9.8h [28 May 2008]: o CryptoAPI ENGINE support. o Various precautionary measures. o Fix for bugs affecting certificate request creation. o Support for local machine keyset attribute in PKCS#12 files. Major changes between OpenSSL 0.9.8f and OpenSSL 0.9.8g [19 Oct 2007]: o Backport of CMS functionality to 0.9.8. o Fixes for bugs introduced with 0.9.8f. Major changes between OpenSSL 0.9.8e and OpenSSL 0.9.8f [11 Oct 2007]: o Add gcc 4.2 support. o Add support for AES and SSE2 assembly lanugauge optimization for VC++ build. o Support for RFC4507bis and server name extensions if explicitly selected at compile time. o DTLS improvements. o RFC4507bis support. o TLS Extensions support. Major changes between OpenSSL 0.9.8d and OpenSSL 0.9.8e [23 Feb 2007]: o Various ciphersuite selection fixes. o RFC3779 support. Major changes between OpenSSL 0.9.8c and OpenSSL 0.9.8d [28 Sep 2006]: o Introduce limits to prevent malicious key DoS (CVE-2006-2940) o Fix security issues (CVE-2006-2937, CVE-2006-3737, CVE-2006-4343) o Changes to ciphersuite selection algorithm Major changes between OpenSSL 0.9.8b and OpenSSL 0.9.8c [5 Sep 2006]: o Fix Daniel Bleichenbacher forged signature attack, CVE-2006-4339 o New cipher Camellia Major changes between OpenSSL 0.9.8a and OpenSSL 0.9.8b [4 May 2006]: o Cipher string fixes. o Fixes for VC++ 2005. o Updated ECC cipher suite support. o New functions EVP_CIPHER_CTX_new() and EVP_CIPHER_CTX_free(). o Zlib compression usage fixes. o Built in dynamic engine compilation support on Win32. o Fixes auto dynamic engine loading in Win32. Major changes between OpenSSL 0.9.8 and OpenSSL 0.9.8a [11 Oct 2005]: o Fix potential SSL 2.0 rollback, CVE-2005-2969 o Extended Windows CE support Major changes between OpenSSL 0.9.7g and OpenSSL 0.9.8 [5 Jul 2005]: o Major work on the BIGNUM library for higher efficiency and to make operations more streamlined and less contradictory. This is the result of a major audit of the BIGNUM library. o Addition of BIGNUM functions for fields GF(2^m) and NIST curves, to support the Elliptic Crypto functions. o Major work on Elliptic Crypto; ECDH and ECDSA added, including the use through EVP, X509 and ENGINE. o New ASN.1 mini-compiler that's usable through the OpenSSL configuration file. o Added support for ASN.1 indefinite length constructed encoding. o New PKCS#12 'medium level' API to manipulate PKCS#12 files. o Complete rework of shared library construction and linking programs with shared or static libraries, through a separate Makefile.shared. o Rework of the passing of parameters from one Makefile to another. o Changed ENGINE framework to load dynamic engine modules automatically from specifically given directories. o New structure and ASN.1 functions for CertificatePair. o Changed the ZLIB compression method to be stateful. o Changed the key-generation and primality testing "progress" mechanism to take a structure that contains the ticker function and an argument. o New engine module: GMP (performs private key exponentiation). o New engine module: VIA PadLOck ACE extension in VIA C3 Nehemiah processors. o Added support for IPv6 addresses in certificate extensions. See RFC 1884, section 2.2. o Added support for certificate policy mappings, policy constraints and name constraints. o Added support for multi-valued AVAs in the OpenSSL configuration file. o Added support for multiple certificates with the same subject in the 'openssl ca' index file. o Make it possible to create self-signed certificates using 'openssl ca -selfsign'. o Make it possible to generate a serial number file with 'openssl ca -create_serial'. o New binary search functions with extended functionality. o New BUF functions. o New STORE structure and library to provide an interface to all sorts of data repositories. Supports storage of public and private keys, certificates, CRLs, numbers and arbitrary blobs. This library is unfortunately unfinished and unused withing OpenSSL. o New control functions for the error stack. o Changed the PKCS#7 library to support one-pass S/MIME processing. o Added the possibility to compile without old deprecated functionality with the OPENSSL_NO_DEPRECATED macro or the 'no-deprecated' argument to the config and Configure scripts. o Constification of all ASN.1 conversion functions, and other affected functions. o Improved platform support for PowerPC. o New FIPS 180-2 algorithms (SHA-224, -256, -384 and -512). o New X509_VERIFY_PARAM structure to support parametrisation of X.509 path validation. o Major overhaul of RC4 performance on Intel P4, IA-64 and AMD64. o Changed the Configure script to have some algorithms disabled by default. Those can be explicitely enabled with the new argument form 'enable-xxx'. o Change the default digest in 'openssl' commands from MD5 to SHA-1. o Added support for DTLS. o New BIGNUM blinding. o Added support for the RSA-PSS encryption scheme o Added support for the RSA X.931 padding. o Added support for BSD sockets on NetWare. o Added support for files larger than 2GB. o Added initial support for Win64. o Added alternate pkg-config files. Major changes between OpenSSL 0.9.7l and OpenSSL 0.9.7m [23 Feb 2007]: o FIPS 1.1.1 module linking. o Various ciphersuite selection fixes. Major changes between OpenSSL 0.9.7k and OpenSSL 0.9.7l [28 Sep 2006]: o Introduce limits to prevent malicious key DoS (CVE-2006-2940) o Fix security issues (CVE-2006-2937, CVE-2006-3737, CVE-2006-4343) Major changes between OpenSSL 0.9.7j and OpenSSL 0.9.7k [5 Sep 2006]: o Fix Daniel Bleichenbacher forged signature attack, CVE-2006-4339 Major changes between OpenSSL 0.9.7i and OpenSSL 0.9.7j [4 May 2006]: o Visual C++ 2005 fixes. o Update Windows build system for FIPS. Major changes between OpenSSL 0.9.7h and OpenSSL 0.9.7i [14 Oct 2005]: o Give EVP_MAX_MD_SIZE it's old value, except for a FIPS build. Major changes between OpenSSL 0.9.7g and OpenSSL 0.9.7h [11 Oct 2005]: o Fix SSL 2.0 Rollback, CVE-2005-2969 o Allow use of fixed-length exponent on DSA signing o Default fixed-window RSA, DSA, DH private-key operations Major changes between OpenSSL 0.9.7f and OpenSSL 0.9.7g [11 Apr 2005]: o More compilation issues fixed. o Adaptation to more modern Kerberos API. o Enhanced or corrected configuration for Solaris64, Mingw and Cygwin. o Enhanced x86_64 assembler BIGNUM module. o More constification. o Added processing of proxy certificates (RFC 3820). Major changes between OpenSSL 0.9.7e and OpenSSL 0.9.7f [22 Mar 2005]: o Several compilation issues fixed. o Many memory allocation failure checks added. o Improved comparison of X509 Name type. o Mandatory basic checks on certificates. o Performance improvements. Major changes between OpenSSL 0.9.7d and OpenSSL 0.9.7e [25 Oct 2004]: o Fix race condition in CRL checking code. o Fixes to PKCS#7 (S/MIME) code. Major changes between OpenSSL 0.9.7c and OpenSSL 0.9.7d [17 Mar 2004]: o Security: Fix Kerberos ciphersuite SSL/TLS handshaking bug o Security: Fix null-pointer assignment in do_change_cipher_spec() o Allow multiple active certificates with same subject in CA index o Multiple X509 verification fixes o Speed up HMAC and other operations Major changes between OpenSSL 0.9.7b and OpenSSL 0.9.7c [30 Sep 2003]: o Security: fix various ASN1 parsing bugs. o New -ignore_err option to OCSP utility. o Various interop and bug fixes in S/MIME code. o SSL/TLS protocol fix for unrequested client certificates. Major changes between OpenSSL 0.9.7a and OpenSSL 0.9.7b [10 Apr 2003]: o Security: counter the Klima-Pokorny-Rosa extension of Bleichbacher's attack o Security: make RSA blinding default. o Configuration: Irix fixes, AIX fixes, better mingw support. o Support for new platforms: linux-ia64-ecc. o Build: shared library support fixes. o ASN.1: treat domainComponent correctly. o Documentation: fixes and additions. Major changes between OpenSSL 0.9.7 and OpenSSL 0.9.7a [19 Feb 2003]: o Security: Important security related bugfixes. o Enhanced compatibility with MIT Kerberos. o Can be built without the ENGINE framework. o IA32 assembler enhancements. o Support for new platforms: FreeBSD/IA64 and FreeBSD/Sparc64. o Configuration: the no-err option now works properly. o SSL/TLS: now handles manual certificate chain building. o SSL/TLS: certain session ID malfunctions corrected. Major changes between OpenSSL 0.9.6 and OpenSSL 0.9.7 [30 Dec 2002]: o New library section OCSP. o Complete rewrite of ASN1 code. o CRL checking in verify code and openssl utility. o Extension copying in 'ca' utility. o Flexible display options in 'ca' utility. o Provisional support for international characters with UTF8. o Support for external crypto devices ('engine') is no longer a separate distribution. o New elliptic curve library section. o New AES (Rijndael) library section. o Support for new platforms: Windows CE, Tandem OSS, A/UX, AIX 64-bit, Linux x86_64, Linux 64-bit on Sparc v9 o Extended support for some platforms: VxWorks o Enhanced support for shared libraries. o Now only builds PIC code when shared library support is requested. o Support for pkg-config. o Lots of new manuals. o Makes symbolic links to or copies of manuals to cover all described functions. o Change DES API to clean up the namespace (some applications link also against libdes providing similar functions having the same name). Provide macros for backward compatibility (will be removed in the future). o Unify handling of cryptographic algorithms (software and engine) to be available via EVP routines for asymmetric and symmetric ciphers. o NCONF: new configuration handling routines. o Change API to use more 'const' modifiers to improve error checking and help optimizers. o Finally remove references to RSAref. o Reworked parts of the BIGNUM code. o Support for new engines: Broadcom ubsec, Accelerated Encryption Processing, IBM 4758. o A few new engines added in the demos area. o Extended and corrected OID (object identifier) table. o PRNG: query at more locations for a random device, automatic query for EGD style random sources at several locations. o SSL/TLS: allow optional cipher choice according to server's preference. o SSL/TLS: allow server to explicitly set new session ids. o SSL/TLS: support Kerberos cipher suites (RFC2712). Only supports MIT Kerberos for now. o SSL/TLS: allow more precise control of renegotiations and sessions. o SSL/TLS: add callback to retrieve SSL/TLS messages. o SSL/TLS: support AES cipher suites (RFC3268). Major changes between OpenSSL 0.9.6j and OpenSSL 0.9.6k [30 Sep 2003]: o Security: fix various ASN1 parsing bugs. o SSL/TLS protocol fix for unrequested client certificates. Major changes between OpenSSL 0.9.6i and OpenSSL 0.9.6j [10 Apr 2003]: o Security: counter the Klima-Pokorny-Rosa extension of Bleichbacher's attack o Security: make RSA blinding default. o Build: shared library support fixes. Major changes between OpenSSL 0.9.6h and OpenSSL 0.9.6i [19 Feb 2003]: o Important security related bugfixes. Major changes between OpenSSL 0.9.6g and OpenSSL 0.9.6h [5 Dec 2002]: o New configuration targets for Tandem OSS and A/UX. o New OIDs for Microsoft attributes. o Better handling of SSL session caching. o Better comparison of distinguished names. o Better handling of shared libraries in a mixed GNU/non-GNU environment. o Support assembler code with Borland C. o Fixes for length problems. o Fixes for uninitialised variables. o Fixes for memory leaks, some unusual crashes and some race conditions. o Fixes for smaller building problems. o Updates of manuals, FAQ and other instructive documents. Major changes between OpenSSL 0.9.6f and OpenSSL 0.9.6g [9 Aug 2002]: o Important building fixes on Unix. Major changes between OpenSSL 0.9.6e and OpenSSL 0.9.6f [8 Aug 2002]: o Various important bugfixes. Major changes between OpenSSL 0.9.6d and OpenSSL 0.9.6e [30 Jul 2002]: o Important security related bugfixes. o Various SSL/TLS library bugfixes. Major changes between OpenSSL 0.9.6c and OpenSSL 0.9.6d [9 May 2002]: o Various SSL/TLS library bugfixes. o Fix DH parameter generation for 'non-standard' generators. Major changes between OpenSSL 0.9.6b and OpenSSL 0.9.6c [21 Dec 2001]: o Various SSL/TLS library bugfixes. o BIGNUM library fixes. o RSA OAEP and random number generation fixes. o Object identifiers corrected and added. o Add assembler BN routines for IA64. o Add support for OS/390 Unix, UnixWare with gcc, OpenUNIX 8, MIPS Linux; shared library support for Irix, HP-UX. o Add crypto accelerator support for AEP, Baltimore SureWare, Broadcom and Cryptographic Appliance's keyserver [in 0.9.6c-engine release]. Major changes between OpenSSL 0.9.6a and OpenSSL 0.9.6b [9 Jul 2001]: o Security fix: PRNG improvements. o Security fix: RSA OAEP check. o Security fix: Reinsert and fix countermeasure to Bleichbacher's attack. o MIPS bug fix in BIGNUM. o Bug fix in "openssl enc". o Bug fix in X.509 printing routine. o Bug fix in DSA verification routine and DSA S/MIME verification. o Bug fix to make PRNG thread-safe. o Bug fix in RAND_file_name(). o Bug fix in compatibility mode trust settings. o Bug fix in blowfish EVP. o Increase default size for BIO buffering filter. o Compatibility fixes in some scripts. Major changes between OpenSSL 0.9.6 and OpenSSL 0.9.6a [5 Apr 2001]: o Security fix: change behavior of OpenSSL to avoid using environment variables when running as root. o Security fix: check the result of RSA-CRT to reduce the possibility of deducing the private key from an incorrectly calculated signature. o Security fix: prevent Bleichenbacher's DSA attack. o Security fix: Zero the premaster secret after deriving the master secret in DH ciphersuites. o Reimplement SSL_peek(), which had various problems. o Compatibility fix: the function des_encrypt() renamed to des_encrypt1() to avoid clashes with some Unixen libc. o Bug fixes for Win32, HP/UX and Irix. o Bug fixes in BIGNUM, SSL, PKCS#7, PKCS#12, X.509, CONF and memory checking routines. o Bug fixes for RSA operations in threaded environments. o Bug fixes in misc. openssl applications. o Remove a few potential memory leaks. o Add tighter checks of BIGNUM routines. o Shared library support has been reworked for generality. o More documentation. o New function BN_rand_range(). o Add "-rand" option to openssl s_client and s_server. Major changes between OpenSSL 0.9.5a and OpenSSL 0.9.6 [10 Oct 2000]: o Some documentation for BIO and SSL libraries. o Enhanced chain verification using key identifiers. o New sign and verify options to 'dgst' application. o Support for DER and PEM encoded messages in 'smime' application. o New 'rsautl' application, low level RSA utility. o MD4 now included. o Bugfix for SSL rollback padding check. o Support for external crypto devices [1]. o Enhanced EVP interface. [1] The support for external crypto devices is currently a separate distribution. See the file README.ENGINE. Major changes between OpenSSL 0.9.5 and OpenSSL 0.9.5a [1 Apr 2000]: o Bug fixes for Win32, SuSE Linux, NeXTSTEP and FreeBSD 2.2.8 o Shared library support for HPUX and Solaris-gcc o Support of Linux/IA64 o Assembler support for Mingw32 o New 'rand' application o New way to check for existence of algorithms from scripts Major changes between OpenSSL 0.9.4 and OpenSSL 0.9.5 [25 May 2000]: o S/MIME support in new 'smime' command o Documentation for the OpenSSL command line application o Automation of 'req' application o Fixes to make s_client, s_server work under Windows o Support for multiple fieldnames in SPKACs o New SPKAC command line utilty and associated library functions o Options to allow passwords to be obtained from various sources o New public key PEM format and options to handle it o Many other fixes and enhancements to command line utilities o Usable certificate chain verification o Certificate purpose checking o Certificate trust settings o Support of authority information access extension o Extensions in certificate requests o Simplified X509 name and attribute routines o Initial (incomplete) support for international character sets o New DH_METHOD, DSA_METHOD and enhanced RSA_METHOD o Read only memory BIOs and simplified creation function o TLS/SSL protocol bugfixes: Accept TLS 'client hello' in SSL 3.0 record; allow fragmentation and interleaving of handshake and other data o TLS/SSL code now "tolerates" MS SGC o Work around for Netscape client certificate hang bug o RSA_NULL option that removes RSA patent code but keeps other RSA functionality o Memory leak detection now allows applications to add extra information via a per-thread stack o PRNG robustness improved o EGD support o BIGNUM library bug fixes o Faster DSA parameter generation o Enhanced support for Alpha Linux o Experimental MacOS support Major changes between OpenSSL 0.9.3 and OpenSSL 0.9.4 [9 Aug 1999]: o Transparent support for PKCS#8 format private keys: these are used by several software packages and are more secure than the standard form o PKCS#5 v2.0 implementation o Password callbacks have a new void * argument for application data o Avoid various memory leaks o New pipe-like BIO that allows using the SSL library when actual I/O must be handled by the application (BIO pair) Major changes between OpenSSL 0.9.2b and OpenSSL 0.9.3 [24 May 1999]: o Lots of enhancements and cleanups to the Configuration mechanism o RSA OEAP related fixes o Added `openssl ca -revoke' option for revoking a certificate o Source cleanups: const correctness, type-safe stacks and ASN.1 SETs o Source tree cleanups: removed lots of obsolete files o Thawte SXNet, certificate policies and CRL distribution points extension support o Preliminary (experimental) S/MIME support o Support for ASN.1 UTF8String and VisibleString o Full integration of PKCS#12 code o Sparc assembler bignum implementation, optimized hash functions o Option to disable selected ciphers Major changes between OpenSSL 0.9.1c and OpenSSL 0.9.2b [22 Mar 1999]: o Fixed a security hole related to session resumption o Fixed RSA encryption routines for the p < q case o "ALL" in cipher lists now means "everything except NULL ciphers" o Support for Triple-DES CBCM cipher o Support of Optimal Asymmetric Encryption Padding (OAEP) for RSA o First support for new TLSv1 ciphers o Added a few new BIOs (syslog BIO, reliable BIO) o Extended support for DSA certificate/keys. o Extended support for Certificate Signing Requests (CSR) o Initial support for X.509v3 extensions o Extended support for compression inside the SSL record layer o Overhauled Win32 builds o Cleanups and fixes to the Big Number (BN) library o Support for ASN.1 GeneralizedTime o Splitted ASN.1 SETs from SEQUENCEs o ASN1 and PEM support for Netscape Certificate Sequences o Overhauled Perl interface o Lots of source tree cleanups. o Lots of memory leak fixes. o Lots of bug fixes. Major changes between SSLeay 0.9.0b and OpenSSL 0.9.1c [23 Dec 1998]: o Integration of the popular NO_RSA/NO_DSA patches o Initial support for compression inside the SSL record layer o Added BIO proxy and filtering functionality o Extended Big Number (BN) library o Added RIPE MD160 message digest o Addeed support for RC2/64bit cipher o Extended ASN.1 parser routines o Adjustations of the source tree for CVS o Support for various new platforms Index: vendor-crypto/openssl/dist/README =================================================================== --- vendor-crypto/openssl/dist/README (revision 296272) +++ vendor-crypto/openssl/dist/README (revision 296273) @@ -1,119 +1,119 @@ - OpenSSL 1.0.2f 28 Jan 2016 + OpenSSL 1.0.2g 1 Mar 2016 Copyright (c) 1998-2015 The OpenSSL Project Copyright (c) 1995-1998 Eric A. Young, Tim J. Hudson All rights reserved. DESCRIPTION ----------- The OpenSSL Project is a collaborative effort to develop a robust, commercial-grade, fully featured, and Open Source toolkit implementing the Secure Sockets Layer (SSLv3) and Transport Layer Security (TLS) protocols as well as a full-strength general purpose cryptograpic library. The project is managed by a worldwide community of volunteers that use the Internet to communicate, plan, and develop the OpenSSL toolkit and its related documentation. OpenSSL is descended from the SSLeay library developed by Eric A. Young and Tim J. Hudson. The OpenSSL toolkit is licensed under a dual-license (the OpenSSL license plus the SSLeay license), which means that you are free to get and use it for commercial and non-commercial purposes as long as you fulfill the conditions of both licenses. OVERVIEW -------- The OpenSSL toolkit includes: libssl.a: Provides the client and server-side implementations for SSLv3 and TLS. libcrypto.a: Provides general cryptographic and X.509 support needed by SSL/TLS but not logically part of it. openssl: A command line tool that can be used for: Creation of key parameters Creation of X.509 certificates, CSRs and CRLs Calculation of message digests Encryption and decryption SSL/TLS client and server tests Handling of S/MIME signed or encrypted mail And more... INSTALLATION ------------ See the appropriate file: INSTALL Linux, Unix, etc. INSTALL.DJGPP DOS platform with DJGPP INSTALL.NW Netware INSTALL.OS2 OS/2 INSTALL.VMS VMS INSTALL.W32 Windows (32bit) INSTALL.W64 Windows (64bit) INSTALL.WCE Windows CE SUPPORT ------- See the OpenSSL website www.openssl.org for details on how to obtain commercial technical support. If you have any problems with OpenSSL then please take the following steps first: - Download the current snapshot from ftp://ftp.openssl.org/snapshot/ to see if the problem has already been addressed - Remove ASM versions of libraries - Remove compiler optimisation flags If you wish to report a bug then please include the following information in any bug report: - On Unix systems: Self-test report generated by 'make report' - On other systems: OpenSSL version: output of 'openssl version -a' OS Name, Version, Hardware platform Compiler Details (name, version) - Application Details (name, version) - Problem Description (steps that will reproduce the problem, if known) - Stack Traceback (if the application dumps core) Email the report to: rt@openssl.org In order to avoid spam, this is a moderated mailing list, and it might take a day for the ticket to show up. (We also scan posts to make sure that security disclosures aren't publically posted by mistake.) Mail to this address is recorded in the public RT (request tracker) database (see https://www.openssl.org/community/index.html#bugs for details) and also forwarded the public openssl-dev mailing list. Confidential mail may be sent to openssl-security@openssl.org (PGP key available from the key servers). Please do NOT use this for general assistance or support queries. Just because something doesn't work the way you expect does not mean it is necessarily a bug in OpenSSL. You can also make GitHub pull requests. If you do this, please also send mail to rt@openssl.org with a link to the PR so that we can more easily keep track of it. HOW TO CONTRIBUTE TO OpenSSL ---------------------------- See CONTRIBUTING LEGALITIES ---------- A number of nations, in particular the U.S., restrict the use or export of cryptography. If you are potentially subject to such restrictions you should seek competent professional legal advice before attempting to develop or distribute cryptographic code. Index: vendor-crypto/openssl/dist/apps/apps.c =================================================================== --- vendor-crypto/openssl/dist/apps/apps.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/apps.c (revision 296273) @@ -1,3224 +1,3228 @@ /* apps/apps.c */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* ==================================================================== * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) /* * On VMS, you need to define this to get the declaration of fileno(). The * value 2 is to make sure no function defined in POSIX-2 is left undefined. */ # define _POSIX_C_SOURCE 2 #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef OPENSSL_NO_ENGINE # include #endif #ifndef OPENSSL_NO_RSA # include #endif #include #ifndef OPENSSL_NO_JPAKE # include #endif #define NON_MAIN #include "apps.h" #undef NON_MAIN #ifdef _WIN32 static int WIN32_rename(const char *from, const char *to); # define rename(from,to) WIN32_rename((from),(to)) #endif typedef struct { const char *name; unsigned long flag; unsigned long mask; } NAME_EX_TBL; static UI_METHOD *ui_method = NULL; static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL * in_tbl); static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL * in_tbl); #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) /* Looks like this stuff is worth moving into separate function */ static EVP_PKEY *load_netscape_key(BIO *err, BIO *key, const char *file, const char *key_descrip, int format); #endif int app_init(long mesgwin); #ifdef undef /* never finished - probably never will be * :-) */ int args_from_file(char *file, int *argc, char **argv[]) { FILE *fp; int num, i; unsigned int len; static char *buf = NULL; static char **arg = NULL; char *p; fp = fopen(file, "r"); if (fp == NULL) return (0); if (fseek(fp, 0, SEEK_END) == 0) len = ftell(fp), rewind(fp); else len = -1; if (len <= 0) { fclose(fp); return (0); } *argc = 0; *argv = NULL; if (buf != NULL) OPENSSL_free(buf); buf = (char *)OPENSSL_malloc(len + 1); if (buf == NULL) return (0); len = fread(buf, 1, len, fp); if (len <= 1) return (0); buf[len] = '\0'; i = 0; for (p = buf; *p; p++) if (*p == '\n') i++; if (arg != NULL) OPENSSL_free(arg); arg = (char **)OPENSSL_malloc(sizeof(char *) * (i * 2)); *argv = arg; num = 0; p = buf; for (;;) { if (!*p) break; if (*p == '#') { /* comment line */ while (*p && (*p != '\n')) p++; continue; } /* else we have a line */ *(arg++) = p; num++; while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n'))) p++; if (!*p) break; if (*p == '\n') { *(p++) = '\0'; continue; } /* else it is a tab or space */ p++; while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) p++; if (!*p) break; if (*p == '\n') { p++; continue; } *(arg++) = p++; num++; while (*p && (*p != '\n')) p++; if (!*p) break; /* else *p == '\n' */ *(p++) = '\0'; } *argc = num; return (1); } #endif int str2fmt(char *s) { if (s == NULL) return FORMAT_UNDEF; if ((*s == 'D') || (*s == 'd')) return (FORMAT_ASN1); else if ((*s == 'T') || (*s == 't')) return (FORMAT_TEXT); else if ((*s == 'N') || (*s == 'n')) return (FORMAT_NETSCAPE); else if ((*s == 'S') || (*s == 's')) return (FORMAT_SMIME); else if ((*s == 'M') || (*s == 'm')) return (FORMAT_MSBLOB); else if ((*s == '1') || (strcmp(s, "PKCS12") == 0) || (strcmp(s, "pkcs12") == 0) || (strcmp(s, "P12") == 0) || (strcmp(s, "p12") == 0)) return (FORMAT_PKCS12); else if ((*s == 'E') || (*s == 'e')) return (FORMAT_ENGINE); else if ((*s == 'H') || (*s == 'h')) return FORMAT_HTTP; else if ((*s == 'P') || (*s == 'p')) { if (s[1] == 'V' || s[1] == 'v') return FORMAT_PVK; else return (FORMAT_PEM); } else return (FORMAT_UNDEF); } #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE) void program_name(char *in, char *out, int size) { int i, n; char *p = NULL; n = strlen(in); /* find the last '/', '\' or ':' */ for (i = n - 1; i > 0; i--) { if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':')) { p = &(in[i + 1]); break; } } if (p == NULL) p = in; n = strlen(p); # if defined(OPENSSL_SYS_NETWARE) /* strip off trailing .nlm if present. */ if ((n > 4) && (p[n - 4] == '.') && ((p[n - 3] == 'n') || (p[n - 3] == 'N')) && ((p[n - 2] == 'l') || (p[n - 2] == 'L')) && ((p[n - 1] == 'm') || (p[n - 1] == 'M'))) n -= 4; # else /* strip off trailing .exe if present. */ if ((n > 4) && (p[n - 4] == '.') && ((p[n - 3] == 'e') || (p[n - 3] == 'E')) && ((p[n - 2] == 'x') || (p[n - 2] == 'X')) && ((p[n - 1] == 'e') || (p[n - 1] == 'E'))) n -= 4; # endif if (n > size - 1) n = size - 1; for (i = 0; i < n; i++) { if ((p[i] >= 'A') && (p[i] <= 'Z')) out[i] = p[i] - 'A' + 'a'; else out[i] = p[i]; } out[n] = '\0'; } #else # ifdef OPENSSL_SYS_VMS void program_name(char *in, char *out, int size) { char *p = in, *q; char *chars = ":]>"; while (*chars != '\0') { q = strrchr(p, *chars); if (q > p) p = q + 1; chars++; } q = strrchr(p, '.'); if (q == NULL) q = p + strlen(p); strncpy(out, p, size - 1); if (q - p >= size) { out[size - 1] = '\0'; } else { out[q - p] = '\0'; } } # else void program_name(char *in, char *out, int size) { char *p; p = strrchr(in, '/'); if (p != NULL) p++; else p = in; BUF_strlcpy(out, p, size); } # endif #endif int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[]) { int num, i; char *p; *argc = 0; *argv = NULL; i = 0; if (arg->count == 0) { arg->count = 20; arg->data = (char **)OPENSSL_malloc(sizeof(char *) * arg->count); if (arg->data == NULL) return 0; } for (i = 0; i < arg->count; i++) arg->data[i] = NULL; num = 0; p = buf; for (;;) { /* first scan over white space */ if (!*p) break; while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n'))) p++; if (!*p) break; /* The start of something good :-) */ if (num >= arg->count) { char **tmp_p; int tlen = arg->count + 20; tmp_p = (char **)OPENSSL_realloc(arg->data, sizeof(char *) * tlen); if (tmp_p == NULL) return 0; arg->data = tmp_p; arg->count = tlen; /* initialize newly allocated data */ for (i = num; i < arg->count; i++) arg->data[i] = NULL; } arg->data[num++] = p; /* now look for the end of this */ if ((*p == '\'') || (*p == '\"')) { /* scan for closing quote */ i = *(p++); arg->data[num - 1]++; /* jump over quote */ while (*p && (*p != i)) p++; *p = '\0'; } else { while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n'))) p++; if (*p == '\0') p--; else *p = '\0'; } p++; } *argc = num; *argv = arg->data; return (1); } #ifndef APP_INIT int app_init(long mesgwin) { return (1); } #endif int dump_cert_text(BIO *out, X509 *x) { char *p; p = X509_NAME_oneline(X509_get_subject_name(x), NULL, 0); BIO_puts(out, "subject="); BIO_puts(out, p); OPENSSL_free(p); p = X509_NAME_oneline(X509_get_issuer_name(x), NULL, 0); BIO_puts(out, "\nissuer="); BIO_puts(out, p); BIO_puts(out, "\n"); OPENSSL_free(p); return 0; } static int ui_open(UI *ui) { return UI_method_get_opener(UI_OpenSSL())(ui); } static int ui_read(UI *ui, UI_STRING *uis) { if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD && UI_get0_user_data(ui)) { switch (UI_get_string_type(uis)) { case UIT_PROMPT: case UIT_VERIFY: { const char *password = ((PW_CB_DATA *)UI_get0_user_data(ui))->password; if (password && password[0] != '\0') { UI_set_result(ui, uis, password); return 1; } } default: break; } } return UI_method_get_reader(UI_OpenSSL())(ui, uis); } static int ui_write(UI *ui, UI_STRING *uis) { if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD && UI_get0_user_data(ui)) { switch (UI_get_string_type(uis)) { case UIT_PROMPT: case UIT_VERIFY: { const char *password = ((PW_CB_DATA *)UI_get0_user_data(ui))->password; if (password && password[0] != '\0') return 1; } default: break; } } return UI_method_get_writer(UI_OpenSSL())(ui, uis); } static int ui_close(UI *ui) { return UI_method_get_closer(UI_OpenSSL())(ui); } int setup_ui_method(void) { ui_method = UI_create_method("OpenSSL application user interface"); UI_method_set_opener(ui_method, ui_open); UI_method_set_reader(ui_method, ui_read); UI_method_set_writer(ui_method, ui_write); UI_method_set_closer(ui_method, ui_close); return 0; } void destroy_ui_method(void) { if (ui_method) { UI_destroy_method(ui_method); ui_method = NULL; } } int password_callback(char *buf, int bufsiz, int verify, PW_CB_DATA *cb_tmp) { UI *ui = NULL; int res = 0; const char *prompt_info = NULL; const char *password = NULL; PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp; if (cb_data) { if (cb_data->password) password = cb_data->password; if (cb_data->prompt_info) prompt_info = cb_data->prompt_info; } if (password) { res = strlen(password); if (res > bufsiz) res = bufsiz; memcpy(buf, password, res); return res; } ui = UI_new_method(ui_method); if (ui) { int ok = 0; char *buff = NULL; int ui_flags = 0; char *prompt = NULL; prompt = UI_construct_prompt(ui, "pass phrase", prompt_info); if (!prompt) { BIO_printf(bio_err, "Out of memory\n"); UI_free(ui); return 0; } ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD; UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0); if (ok >= 0) ok = UI_add_input_string(ui, prompt, ui_flags, buf, PW_MIN_LENGTH, bufsiz - 1); if (ok >= 0 && verify) { buff = (char *)OPENSSL_malloc(bufsiz); if (!buff) { BIO_printf(bio_err, "Out of memory\n"); UI_free(ui); OPENSSL_free(prompt); return 0; } ok = UI_add_verify_string(ui, prompt, ui_flags, buff, PW_MIN_LENGTH, bufsiz - 1, buf); } if (ok >= 0) do { ok = UI_process(ui); } while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0)); if (buff) { OPENSSL_cleanse(buff, (unsigned int)bufsiz); OPENSSL_free(buff); } if (ok >= 0) res = strlen(buf); if (ok == -1) { BIO_printf(bio_err, "User interface error\n"); ERR_print_errors(bio_err); OPENSSL_cleanse(buf, (unsigned int)bufsiz); res = 0; } if (ok == -2) { BIO_printf(bio_err, "aborted!\n"); OPENSSL_cleanse(buf, (unsigned int)bufsiz); res = 0; } UI_free(ui); OPENSSL_free(prompt); } return res; } static char *app_get_pass(BIO *err, char *arg, int keepbio); int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2) { int same; if (!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0; else same = 1; if (arg1) { *pass1 = app_get_pass(err, arg1, same); if (!*pass1) return 0; } else if (pass1) *pass1 = NULL; if (arg2) { *pass2 = app_get_pass(err, arg2, same ? 2 : 0); if (!*pass2) return 0; } else if (pass2) *pass2 = NULL; return 1; } static char *app_get_pass(BIO *err, char *arg, int keepbio) { char *tmp, tpass[APP_PASS_LEN]; static BIO *pwdbio = NULL; int i; if (!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5); if (!strncmp(arg, "env:", 4)) { tmp = getenv(arg + 4); if (!tmp) { BIO_printf(err, "Can't read environment variable %s\n", arg + 4); return NULL; } return BUF_strdup(tmp); } if (!keepbio || !pwdbio) { if (!strncmp(arg, "file:", 5)) { pwdbio = BIO_new_file(arg + 5, "r"); if (!pwdbio) { BIO_printf(err, "Can't open file %s\n", arg + 5); return NULL; } #if !defined(_WIN32) /* * Under _WIN32, which covers even Win64 and CE, file * descriptors referenced by BIO_s_fd are not inherited * by child process and therefore below is not an option. * It could have been an option if bss_fd.c was operating * on real Windows descriptors, such as those obtained * with CreateFile. */ } else if (!strncmp(arg, "fd:", 3)) { BIO *btmp; i = atoi(arg + 3); if (i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE); if ((i < 0) || !pwdbio) { BIO_printf(err, "Can't access file descriptor %s\n", arg + 3); return NULL; } /* * Can't do BIO_gets on an fd BIO so add a buffering BIO */ btmp = BIO_new(BIO_f_buffer()); pwdbio = BIO_push(btmp, pwdbio); #endif } else if (!strcmp(arg, "stdin")) { pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE); if (!pwdbio) { BIO_printf(err, "Can't open BIO for stdin\n"); return NULL; } } else { BIO_printf(err, "Invalid password argument \"%s\"\n", arg); return NULL; } } i = BIO_gets(pwdbio, tpass, APP_PASS_LEN); if (keepbio != 1) { BIO_free_all(pwdbio); pwdbio = NULL; } if (i <= 0) { BIO_printf(err, "Error reading password from BIO\n"); return NULL; } tmp = strchr(tpass, '\n'); if (tmp) *tmp = 0; return BUF_strdup(tpass); } int add_oid_section(BIO *err, CONF *conf) { char *p; STACK_OF(CONF_VALUE) *sktmp; CONF_VALUE *cnf; int i; if (!(p = NCONF_get_string(conf, NULL, "oid_section"))) { ERR_clear_error(); return 1; } if (!(sktmp = NCONF_get_section(conf, p))) { BIO_printf(err, "problem loading oid section %s\n", p); return 0; } for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { cnf = sk_CONF_VALUE_value(sktmp, i); if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) { BIO_printf(err, "problem creating object %s=%s\n", cnf->name, cnf->value); return 0; } } return 1; } static int load_pkcs12(BIO *err, BIO *in, const char *desc, pem_password_cb *pem_cb, void *cb_data, EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca) { const char *pass; char tpass[PEM_BUFSIZE]; int len, ret = 0; PKCS12 *p12; p12 = d2i_PKCS12_bio(in, NULL); if (p12 == NULL) { BIO_printf(err, "Error loading PKCS12 file for %s\n", desc); goto die; } /* See if an empty password will do */ if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0)) pass = ""; else { if (!pem_cb) pem_cb = (pem_password_cb *)password_callback; len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data); if (len < 0) { BIO_printf(err, "Passpharse callback error for %s\n", desc); goto die; } if (len < PEM_BUFSIZE) tpass[len] = 0; if (!PKCS12_verify_mac(p12, tpass, len)) { BIO_printf(err, "Mac verify error (wrong password?) in PKCS12 file for %s\n", desc); goto die; } pass = tpass; } ret = PKCS12_parse(p12, pass, pkey, cert, ca); die: if (p12) PKCS12_free(p12); return ret; } int load_cert_crl_http(const char *url, BIO *err, X509 **pcert, X509_CRL **pcrl) { char *host = NULL, *port = NULL, *path = NULL; BIO *bio = NULL; OCSP_REQ_CTX *rctx = NULL; int use_ssl, rv = 0; if (!OCSP_parse_url(url, &host, &port, &path, &use_ssl)) goto err; if (use_ssl) { if (err) BIO_puts(err, "https not supported\n"); goto err; } bio = BIO_new_connect(host); if (!bio || !BIO_set_conn_port(bio, port)) goto err; rctx = OCSP_REQ_CTX_new(bio, 1024); if (!rctx) goto err; if (!OCSP_REQ_CTX_http(rctx, "GET", path)) goto err; if (!OCSP_REQ_CTX_add1_header(rctx, "Host", host)) goto err; if (pcert) { do { rv = X509_http_nbio(rctx, pcert); } while (rv == -1); } else { do { rv = X509_CRL_http_nbio(rctx, pcrl); } while (rv == -1); } err: if (host) OPENSSL_free(host); if (path) OPENSSL_free(path); if (port) OPENSSL_free(port); if (bio) BIO_free_all(bio); if (rctx) OCSP_REQ_CTX_free(rctx); if (rv != 1) { if (bio && err) BIO_printf(bio_err, "Error loading %s from %s\n", pcert ? "certificate" : "CRL", url); ERR_print_errors(bio_err); } return rv; } X509 *load_cert(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *cert_descrip) { X509 *x = NULL; BIO *cert; if (format == FORMAT_HTTP) { load_cert_crl_http(file, err, &x, NULL); return x; } if ((cert = BIO_new(BIO_s_file())) == NULL) { ERR_print_errors(err); goto end; } if (file == NULL) { #ifdef _IONBF # ifndef OPENSSL_NO_SETVBUF_IONBF setvbuf(stdin, NULL, _IONBF, 0); # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */ #endif BIO_set_fp(cert, stdin, BIO_NOCLOSE); } else { if (BIO_read_filename(cert, file) <= 0) { BIO_printf(err, "Error opening %s %s\n", cert_descrip, file); ERR_print_errors(err); goto end; } } if (format == FORMAT_ASN1) x = d2i_X509_bio(cert, NULL); else if (format == FORMAT_NETSCAPE) { NETSCAPE_X509 *nx; nx = ASN1_item_d2i_bio(ASN1_ITEM_rptr(NETSCAPE_X509), cert, NULL); if (nx == NULL) goto end; if ((strncmp(NETSCAPE_CERT_HDR, (char *)nx->header->data, nx->header->length) != 0)) { NETSCAPE_X509_free(nx); BIO_printf(err, "Error reading header on certificate\n"); goto end; } x = nx->cert; nx->cert = NULL; NETSCAPE_X509_free(nx); } else if (format == FORMAT_PEM) x = PEM_read_bio_X509_AUX(cert, NULL, (pem_password_cb *)password_callback, NULL); else if (format == FORMAT_PKCS12) { if (!load_pkcs12(err, cert, cert_descrip, NULL, NULL, NULL, &x, NULL)) goto end; } else { BIO_printf(err, "bad input format specified for %s\n", cert_descrip); goto end; } end: if (x == NULL) { BIO_printf(err, "unable to load certificate\n"); ERR_print_errors(err); } if (cert != NULL) BIO_free(cert); return (x); } X509_CRL *load_crl(const char *infile, int format) { X509_CRL *x = NULL; BIO *in = NULL; if (format == FORMAT_HTTP) { load_cert_crl_http(infile, bio_err, NULL, &x); return x; } in = BIO_new(BIO_s_file()); if (in == NULL) { ERR_print_errors(bio_err); goto end; } if (infile == NULL) BIO_set_fp(in, stdin, BIO_NOCLOSE); else { if (BIO_read_filename(in, infile) <= 0) { perror(infile); goto end; } } if (format == FORMAT_ASN1) x = d2i_X509_CRL_bio(in, NULL); else if (format == FORMAT_PEM) x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL); else { BIO_printf(bio_err, "bad input format specified for input crl\n"); goto end; } if (x == NULL) { BIO_printf(bio_err, "unable to load CRL\n"); ERR_print_errors(bio_err); goto end; } end: BIO_free(in); return (x); } EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin, const char *pass, ENGINE *e, const char *key_descrip) { BIO *key = NULL; EVP_PKEY *pkey = NULL; PW_CB_DATA cb_data; cb_data.password = pass; cb_data.prompt_info = file; if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { BIO_printf(err, "no keyfile specified\n"); goto end; } #ifndef OPENSSL_NO_ENGINE if (format == FORMAT_ENGINE) { if (!e) BIO_printf(err, "no engine specified\n"); else { pkey = ENGINE_load_private_key(e, file, ui_method, &cb_data); if (!pkey) { BIO_printf(err, "cannot load %s from engine\n", key_descrip); ERR_print_errors(err); } } goto end; } #endif key = BIO_new(BIO_s_file()); if (key == NULL) { ERR_print_errors(err); goto end; } if (file == NULL && maybe_stdin) { #ifdef _IONBF # ifndef OPENSSL_NO_SETVBUF_IONBF setvbuf(stdin, NULL, _IONBF, 0); # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */ #endif BIO_set_fp(key, stdin, BIO_NOCLOSE); } else if (BIO_read_filename(key, file) <= 0) { BIO_printf(err, "Error opening %s %s\n", key_descrip, file); ERR_print_errors(err); goto end; } if (format == FORMAT_ASN1) { pkey = d2i_PrivateKey_bio(key, NULL); } else if (format == FORMAT_PEM) { pkey = PEM_read_bio_PrivateKey(key, NULL, (pem_password_cb *)password_callback, &cb_data); } #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC) pkey = load_netscape_key(err, key, file, key_descrip, format); #endif else if (format == FORMAT_PKCS12) { if (!load_pkcs12(err, key, key_descrip, (pem_password_cb *)password_callback, &cb_data, &pkey, NULL, NULL)) goto end; } #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) && !defined (OPENSSL_NO_RC4) else if (format == FORMAT_MSBLOB) pkey = b2i_PrivateKey_bio(key); else if (format == FORMAT_PVK) pkey = b2i_PVK_bio(key, (pem_password_cb *)password_callback, &cb_data); #endif else { BIO_printf(err, "bad input format specified for key file\n"); goto end; } end: if (key != NULL) BIO_free(key); if (pkey == NULL) { BIO_printf(err, "unable to load %s\n", key_descrip); ERR_print_errors(err); } return (pkey); } EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin, const char *pass, ENGINE *e, const char *key_descrip) { BIO *key = NULL; EVP_PKEY *pkey = NULL; PW_CB_DATA cb_data; cb_data.password = pass; cb_data.prompt_info = file; if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE)) { BIO_printf(err, "no keyfile specified\n"); goto end; } #ifndef OPENSSL_NO_ENGINE if (format == FORMAT_ENGINE) { if (!e) BIO_printf(bio_err, "no engine specified\n"); else pkey = ENGINE_load_public_key(e, file, ui_method, &cb_data); goto end; } #endif key = BIO_new(BIO_s_file()); if (key == NULL) { ERR_print_errors(err); goto end; } if (file == NULL && maybe_stdin) { #ifdef _IONBF # ifndef OPENSSL_NO_SETVBUF_IONBF setvbuf(stdin, NULL, _IONBF, 0); # endif /* ndef OPENSSL_NO_SETVBUF_IONBF */ #endif BIO_set_fp(key, stdin, BIO_NOCLOSE); } else if (BIO_read_filename(key, file) <= 0) { BIO_printf(err, "Error opening %s %s\n", key_descrip, file); ERR_print_errors(err); goto end; } if (format == FORMAT_ASN1) { pkey = d2i_PUBKEY_bio(key, NULL); } #ifndef OPENSSL_NO_RSA else if (format == FORMAT_ASN1RSA) { RSA *rsa; rsa = d2i_RSAPublicKey_bio(key, NULL); if (rsa) { pkey = EVP_PKEY_new(); if (pkey) EVP_PKEY_set1_RSA(pkey, rsa); RSA_free(rsa); } else pkey = NULL; } else if (format == FORMAT_PEMRSA) { RSA *rsa; rsa = PEM_read_bio_RSAPublicKey(key, NULL, (pem_password_cb *)password_callback, &cb_data); if (rsa) { pkey = EVP_PKEY_new(); if (pkey) EVP_PKEY_set1_RSA(pkey, rsa); RSA_free(rsa); } else pkey = NULL; } #endif else if (format == FORMAT_PEM) { pkey = PEM_read_bio_PUBKEY(key, NULL, (pem_password_cb *)password_callback, &cb_data); } #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC) pkey = load_netscape_key(err, key, file, key_descrip, format); #endif #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DSA) else if (format == FORMAT_MSBLOB) pkey = b2i_PublicKey_bio(key); #endif else { BIO_printf(err, "bad input format specified for key file\n"); goto end; } end: if (key != NULL) BIO_free(key); if (pkey == NULL) BIO_printf(err, "unable to load %s\n", key_descrip); return (pkey); } #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA) static EVP_PKEY *load_netscape_key(BIO *err, BIO *key, const char *file, const char *key_descrip, int format) { EVP_PKEY *pkey; BUF_MEM *buf; RSA *rsa; const unsigned char *p; int size, i; buf = BUF_MEM_new(); pkey = EVP_PKEY_new(); size = 0; if (buf == NULL || pkey == NULL) goto error; for (;;) { if (!BUF_MEM_grow_clean(buf, size + 1024 * 10)) goto error; i = BIO_read(key, &(buf->data[size]), 1024 * 10); size += i; if (i == 0) break; if (i < 0) { BIO_printf(err, "Error reading %s %s", key_descrip, file); goto error; } } p = (unsigned char *)buf->data; rsa = d2i_RSA_NET(NULL, &p, (long)size, NULL, (format == FORMAT_IISSGC ? 1 : 0)); if (rsa == NULL) goto error; BUF_MEM_free(buf); EVP_PKEY_set1_RSA(pkey, rsa); return pkey; error: BUF_MEM_free(buf); EVP_PKEY_free(pkey); return NULL; } #endif /* ndef OPENSSL_NO_RC4 */ static int load_certs_crls(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *desc, STACK_OF(X509) **pcerts, STACK_OF(X509_CRL) **pcrls) { int i; BIO *bio; STACK_OF(X509_INFO) *xis = NULL; X509_INFO *xi; PW_CB_DATA cb_data; int rv = 0; cb_data.password = pass; cb_data.prompt_info = file; if (format != FORMAT_PEM) { BIO_printf(err, "bad input format specified for %s\n", desc); return 0; } if (file == NULL) bio = BIO_new_fp(stdin, BIO_NOCLOSE); else bio = BIO_new_file(file, "r"); if (bio == NULL) { BIO_printf(err, "Error opening %s %s\n", desc, file ? file : "stdin"); ERR_print_errors(err); return 0; } xis = PEM_X509_INFO_read_bio(bio, NULL, (pem_password_cb *)password_callback, &cb_data); BIO_free(bio); if (pcerts) { *pcerts = sk_X509_new_null(); if (!*pcerts) goto end; } if (pcrls) { *pcrls = sk_X509_CRL_new_null(); if (!*pcrls) goto end; } for (i = 0; i < sk_X509_INFO_num(xis); i++) { xi = sk_X509_INFO_value(xis, i); if (xi->x509 && pcerts) { if (!sk_X509_push(*pcerts, xi->x509)) goto end; xi->x509 = NULL; } if (xi->crl && pcrls) { if (!sk_X509_CRL_push(*pcrls, xi->crl)) goto end; xi->crl = NULL; } } if (pcerts && sk_X509_num(*pcerts) > 0) rv = 1; if (pcrls && sk_X509_CRL_num(*pcrls) > 0) rv = 1; end: if (xis) sk_X509_INFO_pop_free(xis, X509_INFO_free); if (rv == 0) { if (pcerts) { sk_X509_pop_free(*pcerts, X509_free); *pcerts = NULL; } if (pcrls) { sk_X509_CRL_pop_free(*pcrls, X509_CRL_free); *pcrls = NULL; } BIO_printf(err, "unable to load %s\n", pcerts ? "certificates" : "CRLs"); ERR_print_errors(err); } return rv; } STACK_OF(X509) *load_certs(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *desc) { STACK_OF(X509) *certs; if (!load_certs_crls(err, file, format, pass, e, desc, &certs, NULL)) return NULL; return certs; } STACK_OF(X509_CRL) *load_crls(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *desc) { STACK_OF(X509_CRL) *crls; if (!load_certs_crls(err, file, format, pass, e, desc, NULL, &crls)) return NULL; return crls; } #define X509V3_EXT_UNKNOWN_MASK (0xfL << 16) /* Return error for unknown extensions */ #define X509V3_EXT_DEFAULT 0 /* Print error for unknown extensions */ #define X509V3_EXT_ERROR_UNKNOWN (1L << 16) /* ASN1 parse unknown extensions */ #define X509V3_EXT_PARSE_UNKNOWN (2L << 16) /* BIO_dump unknown extensions */ #define X509V3_EXT_DUMP_UNKNOWN (3L << 16) #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \ X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION) int set_cert_ex(unsigned long *flags, const char *arg) { static const NAME_EX_TBL cert_tbl[] = { {"compatible", X509_FLAG_COMPAT, 0xffffffffl}, {"ca_default", X509_FLAG_CA, 0xffffffffl}, {"no_header", X509_FLAG_NO_HEADER, 0}, {"no_version", X509_FLAG_NO_VERSION, 0}, {"no_serial", X509_FLAG_NO_SERIAL, 0}, {"no_signame", X509_FLAG_NO_SIGNAME, 0}, {"no_validity", X509_FLAG_NO_VALIDITY, 0}, {"no_subject", X509_FLAG_NO_SUBJECT, 0}, {"no_issuer", X509_FLAG_NO_ISSUER, 0}, {"no_pubkey", X509_FLAG_NO_PUBKEY, 0}, {"no_extensions", X509_FLAG_NO_EXTENSIONS, 0}, {"no_sigdump", X509_FLAG_NO_SIGDUMP, 0}, {"no_aux", X509_FLAG_NO_AUX, 0}, {"no_attributes", X509_FLAG_NO_ATTRIBUTES, 0}, {"ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK}, {"ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, {"ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, {"ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK}, {NULL, 0, 0} }; return set_multi_opts(flags, arg, cert_tbl); } int set_name_ex(unsigned long *flags, const char *arg) { static const NAME_EX_TBL ex_tbl[] = { {"esc_2253", ASN1_STRFLGS_ESC_2253, 0}, {"esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0}, {"esc_msb", ASN1_STRFLGS_ESC_MSB, 0}, {"use_quote", ASN1_STRFLGS_ESC_QUOTE, 0}, {"utf8", ASN1_STRFLGS_UTF8_CONVERT, 0}, {"ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0}, {"show_type", ASN1_STRFLGS_SHOW_TYPE, 0}, {"dump_all", ASN1_STRFLGS_DUMP_ALL, 0}, {"dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0}, {"dump_der", ASN1_STRFLGS_DUMP_DER, 0}, {"compat", XN_FLAG_COMPAT, 0xffffffffL}, {"sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK}, {"sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK}, {"sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK}, {"sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK}, {"dn_rev", XN_FLAG_DN_REV, 0}, {"nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK}, {"sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK}, {"lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK}, {"align", XN_FLAG_FN_ALIGN, 0}, {"oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK}, {"space_eq", XN_FLAG_SPC_EQ, 0}, {"dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0}, {"RFC2253", XN_FLAG_RFC2253, 0xffffffffL}, {"oneline", XN_FLAG_ONELINE, 0xffffffffL}, {"multiline", XN_FLAG_MULTILINE, 0xffffffffL}, {"ca_default", XN_FLAG_MULTILINE, 0xffffffffL}, {NULL, 0, 0} }; if (set_multi_opts(flags, arg, ex_tbl) == 0) return 0; if ((*flags & XN_FLAG_SEP_MASK) == 0) *flags |= XN_FLAG_SEP_CPLUS_SPC; return 1; } int set_ext_copy(int *copy_type, const char *arg) { if (!strcasecmp(arg, "none")) *copy_type = EXT_COPY_NONE; else if (!strcasecmp(arg, "copy")) *copy_type = EXT_COPY_ADD; else if (!strcasecmp(arg, "copyall")) *copy_type = EXT_COPY_ALL; else return 0; return 1; } int copy_extensions(X509 *x, X509_REQ *req, int copy_type) { STACK_OF(X509_EXTENSION) *exts = NULL; X509_EXTENSION *ext, *tmpext; ASN1_OBJECT *obj; int i, idx, ret = 0; if (!x || !req || (copy_type == EXT_COPY_NONE)) return 1; exts = X509_REQ_get_extensions(req); for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { ext = sk_X509_EXTENSION_value(exts, i); obj = X509_EXTENSION_get_object(ext); idx = X509_get_ext_by_OBJ(x, obj, -1); /* Does extension exist? */ if (idx != -1) { /* If normal copy don't override existing extension */ if (copy_type == EXT_COPY_ADD) continue; /* Delete all extensions of same type */ do { tmpext = X509_get_ext(x, idx); X509_delete_ext(x, idx); X509_EXTENSION_free(tmpext); idx = X509_get_ext_by_OBJ(x, obj, -1); } while (idx != -1); } if (!X509_add_ext(x, ext, -1)) goto end; } ret = 1; end: sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); return ret; } static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL * in_tbl) { STACK_OF(CONF_VALUE) *vals; CONF_VALUE *val; int i, ret = 1; if (!arg) return 0; vals = X509V3_parse_list(arg); for (i = 0; i < sk_CONF_VALUE_num(vals); i++) { val = sk_CONF_VALUE_value(vals, i); if (!set_table_opts(flags, val->name, in_tbl)) ret = 0; } sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); return ret; } static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL * in_tbl) { char c; const NAME_EX_TBL *ptbl; c = arg[0]; if (c == '-') { c = 0; arg++; } else if (c == '+') { c = 1; arg++; } else c = 1; for (ptbl = in_tbl; ptbl->name; ptbl++) { if (!strcasecmp(arg, ptbl->name)) { *flags &= ~ptbl->mask; if (c) *flags |= ptbl->flag; else *flags &= ~ptbl->flag; return 1; } } return 0; } void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags) { char *buf; char mline = 0; int indent = 0; if (title) BIO_puts(out, title); if ((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { mline = 1; indent = 4; } if (lflags == XN_FLAG_COMPAT) { buf = X509_NAME_oneline(nm, 0, 0); BIO_puts(out, buf); BIO_puts(out, "\n"); OPENSSL_free(buf); } else { if (mline) BIO_puts(out, "\n"); X509_NAME_print_ex(out, nm, indent, lflags); BIO_puts(out, "\n"); } } X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath) { X509_STORE *store; X509_LOOKUP *lookup; if (!(store = X509_STORE_new())) goto end; lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()); if (lookup == NULL) goto end; if (CAfile) { if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) { BIO_printf(bp, "Error loading file %s\n", CAfile); goto end; } } else X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir()); if (lookup == NULL) goto end; if (CApath) { if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) { BIO_printf(bp, "Error loading directory %s\n", CApath); goto end; } } else X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); ERR_clear_error(); return store; end: X509_STORE_free(store); return NULL; } #ifndef OPENSSL_NO_ENGINE /* Try to load an engine in a shareable library */ static ENGINE *try_load_engine(BIO *err, const char *engine, int debug) { ENGINE *e = ENGINE_by_id("dynamic"); if (e) { if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0) || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) { ENGINE_free(e); e = NULL; } } return e; } ENGINE *setup_engine(BIO *err, const char *engine, int debug) { ENGINE *e = NULL; if (engine) { if (strcmp(engine, "auto") == 0) { BIO_printf(err, "enabling auto ENGINE support\n"); ENGINE_register_all_complete(); return NULL; } if ((e = ENGINE_by_id(engine)) == NULL && (e = try_load_engine(err, engine, debug)) == NULL) { BIO_printf(err, "invalid engine \"%s\"\n", engine); ERR_print_errors(err); return NULL; } if (debug) { ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, 0, err, 0); } ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1); if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { BIO_printf(err, "can't use that engine\n"); ERR_print_errors(err); ENGINE_free(e); return NULL; } BIO_printf(err, "engine \"%s\" set.\n", ENGINE_get_id(e)); /* Free our "structural" reference. */ ENGINE_free(e); } return e; } #endif int load_config(BIO *err, CONF *cnf) { static int load_config_called = 0; if (load_config_called) return 1; load_config_called = 1; if (!cnf) cnf = config; if (!cnf) return 1; OPENSSL_load_builtin_modules(); if (CONF_modules_load(cnf, NULL, 0) <= 0) { BIO_printf(err, "Error configuring OpenSSL\n"); ERR_print_errors(err); return 0; } return 1; } char *make_config_name() { const char *t = X509_get_default_cert_area(); size_t len; char *p; len = strlen(t) + strlen(OPENSSL_CONF) + 2; p = OPENSSL_malloc(len); if (p == NULL) return NULL; BUF_strlcpy(p, t, len); #ifndef OPENSSL_SYS_VMS BUF_strlcat(p, "/", len); #endif BUF_strlcat(p, OPENSSL_CONF, len); return p; } static unsigned long index_serial_hash(const OPENSSL_CSTRING *a) { const char *n; n = a[DB_serial]; while (*n == '0') n++; return (lh_strhash(n)); } static int index_serial_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b) { const char *aa, *bb; for (aa = a[DB_serial]; *aa == '0'; aa++) ; for (bb = b[DB_serial]; *bb == '0'; bb++) ; return (strcmp(aa, bb)); } static int index_name_qual(char **a) { return (a[0][0] == 'V'); } static unsigned long index_name_hash(const OPENSSL_CSTRING *a) { return (lh_strhash(a[DB_name])); } int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b) { return (strcmp(a[DB_name], b[DB_name])); } static IMPLEMENT_LHASH_HASH_FN(index_serial, OPENSSL_CSTRING) static IMPLEMENT_LHASH_COMP_FN(index_serial, OPENSSL_CSTRING) static IMPLEMENT_LHASH_HASH_FN(index_name, OPENSSL_CSTRING) static IMPLEMENT_LHASH_COMP_FN(index_name, OPENSSL_CSTRING) #undef BSIZE #define BSIZE 256 BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai) { BIO *in = NULL; BIGNUM *ret = NULL; MS_STATIC char buf[1024]; ASN1_INTEGER *ai = NULL; ai = ASN1_INTEGER_new(); if (ai == NULL) goto err; if ((in = BIO_new(BIO_s_file())) == NULL) { ERR_print_errors(bio_err); goto err; } if (BIO_read_filename(in, serialfile) <= 0) { if (!create) { perror(serialfile); goto err; } else { ret = BN_new(); if (ret == NULL || !rand_serial(ret, ai)) BIO_printf(bio_err, "Out of memory\n"); } } else { if (!a2i_ASN1_INTEGER(in, ai, buf, 1024)) { BIO_printf(bio_err, "unable to load number from %s\n", serialfile); goto err; } ret = ASN1_INTEGER_to_BN(ai, NULL); if (ret == NULL) { BIO_printf(bio_err, "error converting number from bin to BIGNUM\n"); goto err; } } if (ret && retai) { *retai = ai; ai = NULL; } err: if (in != NULL) BIO_free(in); if (ai != NULL) ASN1_INTEGER_free(ai); return (ret); } int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai) { char buf[1][BSIZE]; BIO *out = NULL; int ret = 0; ASN1_INTEGER *ai = NULL; int j; if (suffix == NULL) j = strlen(serialfile); else j = strlen(serialfile) + strlen(suffix) + 1; if (j >= BSIZE) { BIO_printf(bio_err, "file name too long\n"); goto err; } if (suffix == NULL) BUF_strlcpy(buf[0], serialfile, BSIZE); else { #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix); #else j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix); #endif } #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]); #endif out = BIO_new(BIO_s_file()); if (out == NULL) { ERR_print_errors(bio_err); goto err; } if (BIO_write_filename(out, buf[0]) <= 0) { perror(serialfile); goto err; } if ((ai = BN_to_ASN1_INTEGER(serial, NULL)) == NULL) { BIO_printf(bio_err, "error converting serial to ASN.1 format\n"); goto err; } i2a_ASN1_INTEGER(out, ai); BIO_puts(out, "\n"); ret = 1; if (retai) { *retai = ai; ai = NULL; } err: if (out != NULL) BIO_free_all(out); if (ai != NULL) ASN1_INTEGER_free(ai); return (ret); } int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix) { char buf[5][BSIZE]; int i, j; i = strlen(serialfile) + strlen(old_suffix); j = strlen(serialfile) + strlen(new_suffix); if (i > j) j = i; if (j + 1 >= BSIZE) { BIO_printf(bio_err, "file name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, new_suffix); #else j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, new_suffix); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", serialfile, old_suffix); #else j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", serialfile, old_suffix); #endif #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", serialfile, buf[1]); #endif if (rename(serialfile, buf[1]) < 0 && errno != ENOENT #ifdef ENOTDIR && errno != ENOTDIR #endif ) { BIO_printf(bio_err, "unable to rename %s to %s\n", serialfile, buf[1]); perror("reason"); goto err; } #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[0], serialfile); #endif if (rename(buf[0], serialfile) < 0) { BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], serialfile); perror("reason"); rename(buf[1], serialfile); goto err; } return 1; err: return 0; } int rand_serial(BIGNUM *b, ASN1_INTEGER *ai) { BIGNUM *btmp; int ret = 0; if (b) btmp = b; else btmp = BN_new(); if (!btmp) return 0; if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0)) goto error; if (ai && !BN_to_ASN1_INTEGER(btmp, ai)) goto error; ret = 1; error: if (!b) BN_free(btmp); return ret; } CA_DB *load_index(char *dbfile, DB_ATTR *db_attr) { CA_DB *retdb = NULL; TXT_DB *tmpdb = NULL; BIO *in = BIO_new(BIO_s_file()); CONF *dbattr_conf = NULL; char buf[1][BSIZE]; long errorline = -1; if (in == NULL) { ERR_print_errors(bio_err); goto err; } if (BIO_read_filename(in, dbfile) <= 0) { perror(dbfile); BIO_printf(bio_err, "unable to open '%s'\n", dbfile); goto err; } if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL) goto err; #ifndef OPENSSL_SYS_VMS BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile); #else BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile); #endif dbattr_conf = NCONF_new(NULL); if (NCONF_load(dbattr_conf, buf[0], &errorline) <= 0) { if (errorline > 0) { BIO_printf(bio_err, "error on line %ld of db attribute file '%s'\n", errorline, buf[0]); goto err; } else { NCONF_free(dbattr_conf); dbattr_conf = NULL; } } if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL) { fprintf(stderr, "Out of memory\n"); goto err; } retdb->db = tmpdb; tmpdb = NULL; if (db_attr) retdb->attributes = *db_attr; else { retdb->attributes.unique_subject = 1; } if (dbattr_conf) { char *p = NCONF_get_string(dbattr_conf, NULL, "unique_subject"); if (p) { #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p); #endif retdb->attributes.unique_subject = parse_yesno(p, 1); } } err: if (dbattr_conf) NCONF_free(dbattr_conf); if (tmpdb) TXT_DB_free(tmpdb); if (in) BIO_free_all(in); return retdb; } int index_index(CA_DB *db) { if (!TXT_DB_create_index(db->db, DB_serial, NULL, LHASH_HASH_FN(index_serial), LHASH_COMP_FN(index_serial))) { BIO_printf(bio_err, "error creating serial number index:(%ld,%ld,%ld)\n", db->db->error, db->db->arg1, db->db->arg2); return 0; } if (db->attributes.unique_subject && !TXT_DB_create_index(db->db, DB_name, index_name_qual, LHASH_HASH_FN(index_name), LHASH_COMP_FN(index_name))) { BIO_printf(bio_err, "error creating name index:(%ld,%ld,%ld)\n", db->db->error, db->db->arg1, db->db->arg2); return 0; } return 1; } int save_index(const char *dbfile, const char *suffix, CA_DB *db) { char buf[3][BSIZE]; BIO *out = BIO_new(BIO_s_file()); int j; if (out == NULL) { ERR_print_errors(bio_err); goto err; } j = strlen(dbfile) + strlen(suffix); if (j + 6 >= BSIZE) { BIO_printf(bio_err, "file name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile); #else j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix); #else j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix); #else j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix); #endif #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]); #endif if (BIO_write_filename(out, buf[0]) <= 0) { perror(dbfile); BIO_printf(bio_err, "unable to open '%s'\n", dbfile); goto err; } j = TXT_DB_write(out, db->db); if (j <= 0) goto err; BIO_free(out); out = BIO_new(BIO_s_file()); #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]); #endif if (BIO_write_filename(out, buf[1]) <= 0) { perror(buf[2]); BIO_printf(bio_err, "unable to open '%s'\n", buf[2]); goto err; } BIO_printf(out, "unique_subject = %s\n", db->attributes.unique_subject ? "yes" : "no"); BIO_free(out); return 1; err: return 0; } int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix) { char buf[5][BSIZE]; int i, j; i = strlen(dbfile) + strlen(old_suffix); j = strlen(dbfile) + strlen(new_suffix); if (i > j) j = i; if (j + 6 >= BSIZE) { BIO_printf(bio_err, "file name too long\n"); goto err; } #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile); #else j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s", dbfile, new_suffix); #else j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s", dbfile, new_suffix); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, new_suffix); #else j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, new_suffix); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s", dbfile, old_suffix); #else j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s", dbfile, old_suffix); #endif #ifndef OPENSSL_SYS_VMS j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s", dbfile, old_suffix); #else j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s", dbfile, old_suffix); #endif #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", dbfile, buf[1]); #endif if (rename(dbfile, buf[1]) < 0 && errno != ENOENT #ifdef ENOTDIR && errno != ENOTDIR #endif ) { BIO_printf(bio_err, "unable to rename %s to %s\n", dbfile, buf[1]); perror("reason"); goto err; } #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[0], dbfile); #endif if (rename(buf[0], dbfile) < 0) { BIO_printf(bio_err, "unable to rename %s to %s\n", buf[0], dbfile); perror("reason"); rename(buf[1], dbfile); goto err; } #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[4], buf[3]); #endif if (rename(buf[4], buf[3]) < 0 && errno != ENOENT #ifdef ENOTDIR && errno != ENOTDIR #endif ) { BIO_printf(bio_err, "unable to rename %s to %s\n", buf[4], buf[3]); perror("reason"); rename(dbfile, buf[0]); rename(buf[1], dbfile); goto err; } #ifdef RL_DEBUG BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n", buf[2], buf[4]); #endif if (rename(buf[2], buf[4]) < 0) { BIO_printf(bio_err, "unable to rename %s to %s\n", buf[2], buf[4]); perror("reason"); rename(buf[3], buf[4]); rename(dbfile, buf[0]); rename(buf[1], dbfile); goto err; } return 1; err: return 0; } void free_index(CA_DB *db) { if (db) { if (db->db) TXT_DB_free(db->db); OPENSSL_free(db); } } int parse_yesno(const char *str, int def) { int ret = def; if (str) { switch (*str) { case 'f': /* false */ case 'F': /* FALSE */ case 'n': /* no */ case 'N': /* NO */ case '0': /* 0 */ ret = 0; break; case 't': /* true */ case 'T': /* TRUE */ case 'y': /* yes */ case 'Y': /* YES */ case '1': /* 1 */ ret = 1; break; default: ret = def; break; } } return ret; } /* * subject is expected to be in the format /type0=value0/type1=value1/type2=... * where characters may be escaped by \ */ X509_NAME *parse_name(char *subject, long chtype, int multirdn) { size_t buflen = strlen(subject) + 1; /* to copy the types and values * into. due to escaping, the copy * can only become shorter */ char *buf = OPENSSL_malloc(buflen); size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */ char **ne_types = OPENSSL_malloc(max_ne * sizeof(char *)); char **ne_values = OPENSSL_malloc(max_ne * sizeof(char *)); int *mval = OPENSSL_malloc(max_ne * sizeof(int)); char *sp = subject, *bp = buf; int i, ne_num = 0; X509_NAME *n = NULL; int nid; if (!buf || !ne_types || !ne_values || !mval) { BIO_printf(bio_err, "malloc error\n"); goto error; } if (*subject != '/') { BIO_printf(bio_err, "Subject does not start with '/'.\n"); goto error; } sp++; /* skip leading / */ /* no multivalued RDN by default */ mval[ne_num] = 0; while (*sp) { /* collect type */ ne_types[ne_num] = bp; while (*sp) { if (*sp == '\\') { /* is there anything to escape in the * type...? */ if (*++sp) *bp++ = *sp++; else { BIO_printf(bio_err, "escape character at end of string\n"); goto error; } } else if (*sp == '=') { sp++; *bp++ = '\0'; break; } else *bp++ = *sp++; } if (!*sp) { BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num); goto error; } ne_values[ne_num] = bp; while (*sp) { if (*sp == '\\') { if (*++sp) *bp++ = *sp++; else { BIO_printf(bio_err, "escape character at end of string\n"); goto error; } } else if (*sp == '/') { sp++; /* no multivalued RDN by default */ mval[ne_num + 1] = 0; break; } else if (*sp == '+' && multirdn) { /* * a not escaped + signals a mutlivalued RDN */ sp++; mval[ne_num + 1] = -1; break; } else *bp++ = *sp++; } *bp++ = '\0'; ne_num++; } if (!(n = X509_NAME_new())) goto error; for (i = 0; i < ne_num; i++) { if ((nid = OBJ_txt2nid(ne_types[i])) == NID_undef) { BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]); continue; } if (!*ne_values[i]) { BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]); continue; } if (!X509_NAME_add_entry_by_NID (n, nid, chtype, (unsigned char *)ne_values[i], -1, -1, mval[i])) goto error; } OPENSSL_free(ne_values); OPENSSL_free(ne_types); OPENSSL_free(buf); OPENSSL_free(mval); return n; error: X509_NAME_free(n); if (ne_values) OPENSSL_free(ne_values); if (ne_types) OPENSSL_free(ne_types); if (mval) OPENSSL_free(mval); if (buf) OPENSSL_free(buf); return NULL; } int args_verify(char ***pargs, int *pargc, int *badarg, BIO *err, X509_VERIFY_PARAM **pm) { ASN1_OBJECT *otmp = NULL; unsigned long flags = 0; int i; int purpose = 0, depth = -1; char **oldargs = *pargs; char *arg = **pargs, *argn = (*pargs)[1]; time_t at_time = 0; char *hostname = NULL; char *email = NULL; char *ipasc = NULL; if (!strcmp(arg, "-policy")) { if (!argn) *badarg = 1; else { otmp = OBJ_txt2obj(argn, 0); if (!otmp) { BIO_printf(err, "Invalid Policy \"%s\"\n", argn); *badarg = 1; } } (*pargs)++; } else if (strcmp(arg, "-purpose") == 0) { X509_PURPOSE *xptmp; if (!argn) *badarg = 1; else { i = X509_PURPOSE_get_by_sname(argn); if (i < 0) { BIO_printf(err, "unrecognized purpose\n"); *badarg = 1; } else { xptmp = X509_PURPOSE_get0(i); purpose = X509_PURPOSE_get_id(xptmp); } } (*pargs)++; } else if (strcmp(arg, "-verify_depth") == 0) { if (!argn) *badarg = 1; else { depth = atoi(argn); if (depth < 0) { BIO_printf(err, "invalid depth\n"); *badarg = 1; } } (*pargs)++; } else if (strcmp(arg, "-attime") == 0) { if (!argn) *badarg = 1; else { long timestamp; /* * interpret the -attime argument as seconds since Epoch */ if (sscanf(argn, "%li", ×tamp) != 1) { BIO_printf(bio_err, "Error parsing timestamp %s\n", argn); *badarg = 1; } /* on some platforms time_t may be a float */ at_time = (time_t)timestamp; } (*pargs)++; } else if (strcmp(arg, "-verify_hostname") == 0) { if (!argn) *badarg = 1; hostname = argn; (*pargs)++; } else if (strcmp(arg, "-verify_email") == 0) { if (!argn) *badarg = 1; email = argn; (*pargs)++; } else if (strcmp(arg, "-verify_ip") == 0) { if (!argn) *badarg = 1; ipasc = argn; (*pargs)++; } else if (!strcmp(arg, "-ignore_critical")) flags |= X509_V_FLAG_IGNORE_CRITICAL; else if (!strcmp(arg, "-issuer_checks")) flags |= X509_V_FLAG_CB_ISSUER_CHECK; else if (!strcmp(arg, "-crl_check")) flags |= X509_V_FLAG_CRL_CHECK; else if (!strcmp(arg, "-crl_check_all")) flags |= X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL; else if (!strcmp(arg, "-policy_check")) flags |= X509_V_FLAG_POLICY_CHECK; else if (!strcmp(arg, "-explicit_policy")) flags |= X509_V_FLAG_EXPLICIT_POLICY; else if (!strcmp(arg, "-inhibit_any")) flags |= X509_V_FLAG_INHIBIT_ANY; else if (!strcmp(arg, "-inhibit_map")) flags |= X509_V_FLAG_INHIBIT_MAP; else if (!strcmp(arg, "-x509_strict")) flags |= X509_V_FLAG_X509_STRICT; else if (!strcmp(arg, "-extended_crl")) flags |= X509_V_FLAG_EXTENDED_CRL_SUPPORT; else if (!strcmp(arg, "-use_deltas")) flags |= X509_V_FLAG_USE_DELTAS; else if (!strcmp(arg, "-policy_print")) flags |= X509_V_FLAG_NOTIFY_POLICY; else if (!strcmp(arg, "-check_ss_sig")) flags |= X509_V_FLAG_CHECK_SS_SIGNATURE; else if (!strcmp(arg, "-trusted_first")) flags |= X509_V_FLAG_TRUSTED_FIRST; else if (!strcmp(arg, "-suiteB_128_only")) flags |= X509_V_FLAG_SUITEB_128_LOS_ONLY; else if (!strcmp(arg, "-suiteB_128")) flags |= X509_V_FLAG_SUITEB_128_LOS; else if (!strcmp(arg, "-suiteB_192")) flags |= X509_V_FLAG_SUITEB_192_LOS; else if (!strcmp(arg, "-partial_chain")) flags |= X509_V_FLAG_PARTIAL_CHAIN; else if (!strcmp(arg, "-no_alt_chains")) flags |= X509_V_FLAG_NO_ALT_CHAINS; else return 0; if (*badarg) { if (*pm) X509_VERIFY_PARAM_free(*pm); *pm = NULL; goto end; } if (!*pm && !(*pm = X509_VERIFY_PARAM_new())) { *badarg = 1; goto end; } if (otmp) X509_VERIFY_PARAM_add0_policy(*pm, otmp); if (flags) X509_VERIFY_PARAM_set_flags(*pm, flags); if (purpose) X509_VERIFY_PARAM_set_purpose(*pm, purpose); if (depth >= 0) X509_VERIFY_PARAM_set_depth(*pm, depth); if (at_time) X509_VERIFY_PARAM_set_time(*pm, at_time); if (hostname && !X509_VERIFY_PARAM_set1_host(*pm, hostname, 0)) *badarg = 1; if (email && !X509_VERIFY_PARAM_set1_email(*pm, email, 0)) *badarg = 1; if (ipasc && !X509_VERIFY_PARAM_set1_ip_asc(*pm, ipasc)) *badarg = 1; end: (*pargs)++; if (pargc) *pargc -= *pargs - oldargs; return 1; } /* * Read whole contents of a BIO into an allocated memory buffer and return * it. */ int bio_to_mem(unsigned char **out, int maxlen, BIO *in) { BIO *mem; int len, ret; unsigned char tbuf[1024]; mem = BIO_new(BIO_s_mem()); if (!mem) return -1; for (;;) { if ((maxlen != -1) && maxlen < 1024) len = maxlen; else len = 1024; len = BIO_read(in, tbuf, len); - if (len <= 0) + if (len < 0) { + BIO_free(mem); + return -1; + } + if (len == 0) break; if (BIO_write(mem, tbuf, len) != len) { BIO_free(mem); return -1; } maxlen -= len; if (maxlen == 0) break; } ret = BIO_get_mem_data(mem, (char **)out); BIO_set_flags(mem, BIO_FLAGS_MEM_RDONLY); BIO_free(mem); return ret; } -int pkey_ctrl_string(EVP_PKEY_CTX *ctx, char *value) +int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value) { int rv; char *stmp, *vtmp = NULL; stmp = BUF_strdup(value); if (!stmp) return -1; vtmp = strchr(stmp, ':'); if (vtmp) { *vtmp = 0; vtmp++; } rv = EVP_PKEY_CTX_ctrl_str(ctx, stmp, vtmp); OPENSSL_free(stmp); return rv; } static void nodes_print(BIO *out, const char *name, STACK_OF(X509_POLICY_NODE) *nodes) { X509_POLICY_NODE *node; int i; BIO_printf(out, "%s Policies:", name); if (nodes) { BIO_puts(out, "\n"); for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++) { node = sk_X509_POLICY_NODE_value(nodes, i); X509_POLICY_NODE_print(out, node, 2); } } else BIO_puts(out, " \n"); } void policies_print(BIO *out, X509_STORE_CTX *ctx) { X509_POLICY_TREE *tree; int explicit_policy; int free_out = 0; if (out == NULL) { out = BIO_new_fp(stderr, BIO_NOCLOSE); free_out = 1; } tree = X509_STORE_CTX_get0_policy_tree(ctx); explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx); BIO_printf(out, "Require explicit Policy: %s\n", explicit_policy ? "True" : "False"); nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree)); nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree)); if (free_out) BIO_free(out); } #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) static JPAKE_CTX *jpake_init(const char *us, const char *them, const char *secret) { BIGNUM *p = NULL; BIGNUM *g = NULL; BIGNUM *q = NULL; BIGNUM *bnsecret = BN_new(); JPAKE_CTX *ctx; /* Use a safe prime for p (that we found earlier) */ BN_hex2bn(&p, "F9E5B365665EA7A05A9C534502780FEE6F1AB5BD4F49947FD036DBD7E905269AF46EF28B0FC07487EE4F5D20FB3C0AF8E700F3A2FA3414970CBED44FEDFF80CE78D800F184BB82435D137AADA2C6C16523247930A63B85661D1FC817A51ACD96168E95898A1F83A79FFB529368AA7833ABD1B0C3AEDDB14D2E1A2F71D99F763F"); g = BN_new(); BN_set_word(g, 2); q = BN_new(); BN_rshift1(q, p); BN_bin2bn((const unsigned char *)secret, strlen(secret), bnsecret); ctx = JPAKE_CTX_new(us, them, p, g, q, bnsecret); BN_free(bnsecret); BN_free(q); BN_free(g); BN_free(p); return ctx; } static void jpake_send_part(BIO *conn, const JPAKE_STEP_PART *p) { BN_print(conn, p->gx); BIO_puts(conn, "\n"); BN_print(conn, p->zkpx.gr); BIO_puts(conn, "\n"); BN_print(conn, p->zkpx.b); BIO_puts(conn, "\n"); } static void jpake_send_step1(BIO *bconn, JPAKE_CTX *ctx) { JPAKE_STEP1 s1; JPAKE_STEP1_init(&s1); JPAKE_STEP1_generate(&s1, ctx); jpake_send_part(bconn, &s1.p1); jpake_send_part(bconn, &s1.p2); (void)BIO_flush(bconn); JPAKE_STEP1_release(&s1); } static void jpake_send_step2(BIO *bconn, JPAKE_CTX *ctx) { JPAKE_STEP2 s2; JPAKE_STEP2_init(&s2); JPAKE_STEP2_generate(&s2, ctx); jpake_send_part(bconn, &s2); (void)BIO_flush(bconn); JPAKE_STEP2_release(&s2); } static void jpake_send_step3a(BIO *bconn, JPAKE_CTX *ctx) { JPAKE_STEP3A s3a; JPAKE_STEP3A_init(&s3a); JPAKE_STEP3A_generate(&s3a, ctx); BIO_write(bconn, s3a.hhk, sizeof s3a.hhk); (void)BIO_flush(bconn); JPAKE_STEP3A_release(&s3a); } static void jpake_send_step3b(BIO *bconn, JPAKE_CTX *ctx) { JPAKE_STEP3B s3b; JPAKE_STEP3B_init(&s3b); JPAKE_STEP3B_generate(&s3b, ctx); BIO_write(bconn, s3b.hk, sizeof s3b.hk); (void)BIO_flush(bconn); JPAKE_STEP3B_release(&s3b); } static void readbn(BIGNUM **bn, BIO *bconn) { char buf[10240]; int l; l = BIO_gets(bconn, buf, sizeof buf); assert(l > 0); assert(buf[l - 1] == '\n'); buf[l - 1] = '\0'; BN_hex2bn(bn, buf); } static void jpake_receive_part(JPAKE_STEP_PART *p, BIO *bconn) { readbn(&p->gx, bconn); readbn(&p->zkpx.gr, bconn); readbn(&p->zkpx.b, bconn); } static void jpake_receive_step1(JPAKE_CTX *ctx, BIO *bconn) { JPAKE_STEP1 s1; JPAKE_STEP1_init(&s1); jpake_receive_part(&s1.p1, bconn); jpake_receive_part(&s1.p2, bconn); if (!JPAKE_STEP1_process(ctx, &s1)) { ERR_print_errors(bio_err); exit(1); } JPAKE_STEP1_release(&s1); } static void jpake_receive_step2(JPAKE_CTX *ctx, BIO *bconn) { JPAKE_STEP2 s2; JPAKE_STEP2_init(&s2); jpake_receive_part(&s2, bconn); if (!JPAKE_STEP2_process(ctx, &s2)) { ERR_print_errors(bio_err); exit(1); } JPAKE_STEP2_release(&s2); } static void jpake_receive_step3a(JPAKE_CTX *ctx, BIO *bconn) { JPAKE_STEP3A s3a; int l; JPAKE_STEP3A_init(&s3a); l = BIO_read(bconn, s3a.hhk, sizeof s3a.hhk); assert(l == sizeof s3a.hhk); if (!JPAKE_STEP3A_process(ctx, &s3a)) { ERR_print_errors(bio_err); exit(1); } JPAKE_STEP3A_release(&s3a); } static void jpake_receive_step3b(JPAKE_CTX *ctx, BIO *bconn) { JPAKE_STEP3B s3b; int l; JPAKE_STEP3B_init(&s3b); l = BIO_read(bconn, s3b.hk, sizeof s3b.hk); assert(l == sizeof s3b.hk); if (!JPAKE_STEP3B_process(ctx, &s3b)) { ERR_print_errors(bio_err); exit(1); } JPAKE_STEP3B_release(&s3b); } void jpake_client_auth(BIO *out, BIO *conn, const char *secret) { JPAKE_CTX *ctx; BIO *bconn; BIO_puts(out, "Authenticating with JPAKE\n"); ctx = jpake_init("client", "server", secret); bconn = BIO_new(BIO_f_buffer()); BIO_push(bconn, conn); jpake_send_step1(bconn, ctx); jpake_receive_step1(ctx, bconn); jpake_send_step2(bconn, ctx); jpake_receive_step2(ctx, bconn); jpake_send_step3a(bconn, ctx); jpake_receive_step3b(ctx, bconn); BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n"); if (psk_key) OPENSSL_free(psk_key); psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx)); BIO_pop(bconn); BIO_free(bconn); JPAKE_CTX_free(ctx); } void jpake_server_auth(BIO *out, BIO *conn, const char *secret) { JPAKE_CTX *ctx; BIO *bconn; BIO_puts(out, "Authenticating with JPAKE\n"); ctx = jpake_init("server", "client", secret); bconn = BIO_new(BIO_f_buffer()); BIO_push(bconn, conn); jpake_receive_step1(ctx, bconn); jpake_send_step1(bconn, ctx); jpake_receive_step2(ctx, bconn); jpake_send_step2(bconn, ctx); jpake_receive_step3a(ctx, bconn); jpake_send_step3b(bconn, ctx); BIO_puts(out, "JPAKE authentication succeeded, setting PSK\n"); if (psk_key) OPENSSL_free(psk_key); psk_key = BN_bn2hex(JPAKE_get_shared_key(ctx)); BIO_pop(bconn); BIO_free(bconn); JPAKE_CTX_free(ctx); } #endif #ifndef OPENSSL_NO_TLSEXT /*- * next_protos_parse parses a comma separated list of strings into a string * in a format suitable for passing to SSL_CTX_set_next_protos_advertised. * outlen: (output) set to the length of the resulting buffer on success. * err: (maybe NULL) on failure, an error message line is written to this BIO. * in: a NUL termianted string like "abc,def,ghi" * * returns: a malloced buffer or NULL on failure. */ unsigned char *next_protos_parse(unsigned short *outlen, const char *in) { size_t len; unsigned char *out; size_t i, start = 0; len = strlen(in); if (len >= 65535) return NULL; out = OPENSSL_malloc(strlen(in) + 1); if (!out) return NULL; for (i = 0; i <= len; ++i) { if (i == len || in[i] == ',') { if (i - start > 255) { OPENSSL_free(out); return NULL; } out[start] = i - start; start = i + 1; } else out[i + 1] = in[i]; } *outlen = len + 1; return out; } #endif /* ndef OPENSSL_NO_TLSEXT */ void print_cert_checks(BIO *bio, X509 *x, const char *checkhost, const char *checkemail, const char *checkip) { if (x == NULL) return; if (checkhost) { BIO_printf(bio, "Hostname %s does%s match certificate\n", checkhost, X509_check_host(x, checkhost, 0, 0, NULL) == 1 ? "" : " NOT"); } if (checkemail) { BIO_printf(bio, "Email %s does%s match certificate\n", checkemail, X509_check_email(x, checkemail, 0, 0) ? "" : " NOT"); } if (checkip) { BIO_printf(bio, "IP %s does%s match certificate\n", checkip, X509_check_ip_asc(x, checkip, 0) ? "" : " NOT"); } } /* Get first http URL from a DIST_POINT structure */ static const char *get_dp_url(DIST_POINT *dp) { GENERAL_NAMES *gens; GENERAL_NAME *gen; int i, gtype; ASN1_STRING *uri; if (!dp->distpoint || dp->distpoint->type != 0) return NULL; gens = dp->distpoint->name.fullname; for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { gen = sk_GENERAL_NAME_value(gens, i); uri = GENERAL_NAME_get0_value(gen, >ype); if (gtype == GEN_URI && ASN1_STRING_length(uri) > 6) { char *uptr = (char *)ASN1_STRING_data(uri); if (!strncmp(uptr, "http://", 7)) return uptr; } } return NULL; } /* * Look through a CRLDP structure and attempt to find an http URL to * downloads a CRL from. */ static X509_CRL *load_crl_crldp(STACK_OF(DIST_POINT) *crldp) { int i; const char *urlptr = NULL; for (i = 0; i < sk_DIST_POINT_num(crldp); i++) { DIST_POINT *dp = sk_DIST_POINT_value(crldp, i); urlptr = get_dp_url(dp); if (urlptr) return load_crl(urlptr, FORMAT_HTTP); } return NULL; } /* * Example of downloading CRLs from CRLDP: not usable for real world as it * always downloads, doesn't support non-blocking I/O and doesn't cache * anything. */ static STACK_OF(X509_CRL) *crls_http_cb(X509_STORE_CTX *ctx, X509_NAME *nm) { X509 *x; STACK_OF(X509_CRL) *crls = NULL; X509_CRL *crl; STACK_OF(DIST_POINT) *crldp; x = X509_STORE_CTX_get_current_cert(ctx); crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL); crl = load_crl_crldp(crldp); sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); if (!crl) return NULL; crls = sk_X509_CRL_new_null(); sk_X509_CRL_push(crls, crl); /* Try to download delta CRL */ crldp = X509_get_ext_d2i(x, NID_freshest_crl, NULL, NULL); crl = load_crl_crldp(crldp); sk_DIST_POINT_pop_free(crldp, DIST_POINT_free); if (crl) sk_X509_CRL_push(crls, crl); return crls; } void store_setup_crl_download(X509_STORE *st) { X509_STORE_set_lookup_crls_cb(st, crls_http_cb); } /* * Platform-specific sections */ #if defined(_WIN32) # ifdef fileno # undef fileno # define fileno(a) (int)_fileno(a) # endif # include # include static int WIN32_rename(const char *from, const char *to) { TCHAR *tfrom = NULL, *tto; DWORD err; int ret = 0; if (sizeof(TCHAR) == 1) { tfrom = (TCHAR *)from; tto = (TCHAR *)to; } else { /* UNICODE path */ size_t i, flen = strlen(from) + 1, tlen = strlen(to) + 1; tfrom = (TCHAR *)malloc(sizeof(TCHAR) * (flen + tlen)); if (tfrom == NULL) goto err; tto = tfrom + flen; # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 if (!MultiByteToWideChar(CP_ACP, 0, from, flen, (WCHAR *)tfrom, flen)) # endif for (i = 0; i < flen; i++) tfrom[i] = (TCHAR)from[i]; # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 if (!MultiByteToWideChar(CP_ACP, 0, to, tlen, (WCHAR *)tto, tlen)) # endif for (i = 0; i < tlen; i++) tto[i] = (TCHAR)to[i]; } if (MoveFile(tfrom, tto)) goto ok; err = GetLastError(); if (err == ERROR_ALREADY_EXISTS || err == ERROR_FILE_EXISTS) { if (DeleteFile(tto) && MoveFile(tfrom, tto)) goto ok; err = GetLastError(); } if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) errno = ENOENT; else if (err == ERROR_ACCESS_DENIED) errno = EACCES; else errno = EINVAL; /* we could map more codes... */ err: ret = -1; ok: if (tfrom != NULL && tfrom != (TCHAR *)from) free(tfrom); return ret; } #endif /* app_tminterval section */ #if defined(_WIN32) double app_tminterval(int stop, int usertime) { FILETIME now; double ret = 0; static ULARGE_INTEGER tmstart; static int warning = 1; # ifdef _WIN32_WINNT static HANDLE proc = NULL; if (proc == NULL) { if (check_winnt()) proc = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId()); if (proc == NULL) proc = (HANDLE) - 1; } if (usertime && proc != (HANDLE) - 1) { FILETIME junk; GetProcessTimes(proc, &junk, &junk, &junk, &now); } else # endif { SYSTEMTIME systime; if (usertime && warning) { BIO_printf(bio_err, "To get meaningful results, run " "this program on idle system.\n"); warning = 0; } GetSystemTime(&systime); SystemTimeToFileTime(&systime, &now); } if (stop == TM_START) { tmstart.u.LowPart = now.dwLowDateTime; tmstart.u.HighPart = now.dwHighDateTime; } else { ULARGE_INTEGER tmstop; tmstop.u.LowPart = now.dwLowDateTime; tmstop.u.HighPart = now.dwHighDateTime; ret = (__int64)(tmstop.QuadPart - tmstart.QuadPart) * 1e-7; } return (ret); } #elif defined(OPENSSL_SYS_NETWARE) # include double app_tminterval(int stop, int usertime) { double ret = 0; static clock_t tmstart; static int warning = 1; if (usertime && warning) { BIO_printf(bio_err, "To get meaningful results, run " "this program on idle system.\n"); warning = 0; } if (stop == TM_START) tmstart = clock(); else ret = (clock() - tmstart) / (double)CLOCKS_PER_SEC; return (ret); } #elif defined(OPENSSL_SYSTEM_VXWORKS) # include double app_tminterval(int stop, int usertime) { double ret = 0; # ifdef CLOCK_REALTIME static struct timespec tmstart; struct timespec now; # else static unsigned long tmstart; unsigned long now; # endif static int warning = 1; if (usertime && warning) { BIO_printf(bio_err, "To get meaningful results, run " "this program on idle system.\n"); warning = 0; } # ifdef CLOCK_REALTIME clock_gettime(CLOCK_REALTIME, &now); if (stop == TM_START) tmstart = now; else ret = ((now.tv_sec + now.tv_nsec * 1e-9) - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9)); # else now = tickGet(); if (stop == TM_START) tmstart = now; else ret = (now - tmstart) / (double)sysClkRateGet(); # endif return (ret); } #elif defined(OPENSSL_SYSTEM_VMS) # include # include double app_tminterval(int stop, int usertime) { static clock_t tmstart; double ret = 0; clock_t now; # ifdef __TMS struct tms rus; now = times(&rus); if (usertime) now = rus.tms_utime; # else if (usertime) now = clock(); /* sum of user and kernel times */ else { struct timeval tv; gettimeofday(&tv, NULL); now = (clock_t)((unsigned long long)tv.tv_sec * CLK_TCK + (unsigned long long)tv.tv_usec * (1000000 / CLK_TCK) ); } # endif if (stop == TM_START) tmstart = now; else ret = (now - tmstart) / (double)(CLK_TCK); return (ret); } #elif defined(_SC_CLK_TCK) /* by means of unistd.h */ # include double app_tminterval(int stop, int usertime) { double ret = 0; struct tms rus; clock_t now = times(&rus); static clock_t tmstart; if (usertime) now = rus.tms_utime; if (stop == TM_START) tmstart = now; else { long int tck = sysconf(_SC_CLK_TCK); ret = (now - tmstart) / (double)tck; } return (ret); } #else # include # include double app_tminterval(int stop, int usertime) { double ret = 0; struct rusage rus; struct timeval now; static struct timeval tmstart; if (usertime) getrusage(RUSAGE_SELF, &rus), now = rus.ru_utime; else gettimeofday(&now, NULL); if (stop == TM_START) tmstart = now; else ret = ((now.tv_sec + now.tv_usec * 1e-6) - (tmstart.tv_sec + tmstart.tv_usec * 1e-6)); return ret; } #endif /* app_isdir section */ #ifdef _WIN32 int app_isdir(const char *name) { HANDLE hList; WIN32_FIND_DATA FileData; # if defined(UNICODE) || defined(_UNICODE) size_t i, len_0 = strlen(name) + 1; if (len_0 > sizeof(FileData.cFileName) / sizeof(FileData.cFileName[0])) return -1; # if !defined(_WIN32_WCE) || _WIN32_WCE>=101 if (!MultiByteToWideChar (CP_ACP, 0, name, len_0, FileData.cFileName, len_0)) # endif for (i = 0; i < len_0; i++) FileData.cFileName[i] = (WCHAR)name[i]; hList = FindFirstFile(FileData.cFileName, &FileData); # else hList = FindFirstFile(name, &FileData); # endif if (hList == INVALID_HANDLE_VALUE) return -1; FindClose(hList); return ((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0); } #else # include # ifndef S_ISDIR # if defined(_S_IFMT) && defined(_S_IFDIR) # define S_ISDIR(a) (((a) & _S_IFMT) == _S_IFDIR) # else # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) # endif # endif int app_isdir(const char *name) { # if defined(S_ISDIR) struct stat st; if (stat(name, &st) == 0) return S_ISDIR(st.st_mode); else return -1; # else return -1; # endif } #endif /* raw_read|write section */ #if defined(_WIN32) && defined(STD_INPUT_HANDLE) int raw_read_stdin(void *buf, int siz) { DWORD n; if (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, siz, &n, NULL)) return (n); else return (-1); } #else int raw_read_stdin(void *buf, int siz) { return read(fileno(stdin), buf, siz); } #endif #if defined(_WIN32) && defined(STD_OUTPUT_HANDLE) int raw_write_stdout(const void *buf, int siz) { DWORD n; if (WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, siz, &n, NULL)) return (n); else return (-1); } #else int raw_write_stdout(const void *buf, int siz) { return write(fileno(stdout), buf, siz); } #endif Index: vendor-crypto/openssl/dist/apps/apps.h =================================================================== --- vendor-crypto/openssl/dist/apps/apps.h (revision 296272) +++ vendor-crypto/openssl/dist/apps/apps.h (revision 296273) @@ -1,387 +1,387 @@ /* apps/apps.h */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* ==================================================================== * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #ifndef HEADER_APPS_H # define HEADER_APPS_H # include "e_os.h" # include # include # include # include # include # ifndef OPENSSL_NO_ENGINE # include # endif # ifndef OPENSSL_NO_OCSP # include # endif # include int app_RAND_load_file(const char *file, BIO *bio_e, int dont_warn); int app_RAND_write_file(const char *file, BIO *bio_e); /* * When `file' is NULL, use defaults. `bio_e' is for error messages. */ void app_RAND_allow_write_file(void); long app_RAND_load_files(char *file); /* `file' is a list of files to read, * separated by LIST_SEPARATOR_CHAR * (see e_os.h). The string is * destroyed! */ # ifndef MONOLITH # define MAIN(a,v) main(a,v) # ifndef NON_MAIN CONF *config = NULL; BIO *bio_err = NULL; # else extern CONF *config; extern BIO *bio_err; # endif # else # define MAIN(a,v) PROG(a,v) extern CONF *config; extern char *default_config_file; extern BIO *bio_err; # endif # ifndef OPENSSL_SYS_NETWARE # include # endif # ifdef SIGPIPE # define do_pipe_sig() signal(SIGPIPE,SIG_IGN) # else # define do_pipe_sig() # endif # ifdef OPENSSL_NO_COMP # define zlib_cleanup() # else # define zlib_cleanup() COMP_zlib_cleanup() # endif # if defined(MONOLITH) && !defined(OPENSSL_C) # define apps_startup() \ do_pipe_sig() # define apps_shutdown() # else # ifndef OPENSSL_NO_ENGINE # define apps_startup() \ do { do_pipe_sig(); CRYPTO_malloc_init(); \ ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); \ ENGINE_load_builtin_engines(); setup_ui_method(); } while(0) # define apps_shutdown() \ do { CONF_modules_unload(1); destroy_ui_method(); \ OBJ_cleanup(); EVP_cleanup(); ENGINE_cleanup(); \ CRYPTO_cleanup_all_ex_data(); ERR_remove_thread_state(NULL); \ RAND_cleanup(); \ ERR_free_strings(); zlib_cleanup();} while(0) # else # define apps_startup() \ do { do_pipe_sig(); CRYPTO_malloc_init(); \ ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); \ setup_ui_method(); } while(0) # define apps_shutdown() \ do { CONF_modules_unload(1); destroy_ui_method(); \ OBJ_cleanup(); EVP_cleanup(); \ CRYPTO_cleanup_all_ex_data(); ERR_remove_thread_state(NULL); \ RAND_cleanup(); \ ERR_free_strings(); zlib_cleanup(); } while(0) # endif # endif # if defined(OPENSSL_SYSNAME_WIN32) || defined(OPENSSL_SYSNAME_WINCE) # define openssl_fdset(a,b) FD_SET((unsigned int)a, b) # else # define openssl_fdset(a,b) FD_SET(a, b) # endif typedef struct args_st { char **data; int count; } ARGS; # define PW_MIN_LENGTH 4 typedef struct pw_cb_data { const void *password; const char *prompt_info; } PW_CB_DATA; int password_callback(char *buf, int bufsiz, int verify, PW_CB_DATA *cb_data); int setup_ui_method(void); void destroy_ui_method(void); int should_retry(int i); int args_from_file(char *file, int *argc, char **argv[]); int str2fmt(char *s); void program_name(char *in, char *out, int size); int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[]); # ifdef HEADER_X509_H int dump_cert_text(BIO *out, X509 *x); void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags); # endif int set_cert_ex(unsigned long *flags, const char *arg); int set_name_ex(unsigned long *flags, const char *arg); int set_ext_copy(int *copy_type, const char *arg); int copy_extensions(X509 *x, X509_REQ *req, int copy_type); int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2); int add_oid_section(BIO *err, CONF *conf); X509 *load_cert(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *cert_descrip); X509_CRL *load_crl(const char *infile, int format); int load_cert_crl_http(const char *url, BIO *err, X509 **pcert, X509_CRL **pcrl); EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin, const char *pass, ENGINE *e, const char *key_descrip); EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin, const char *pass, ENGINE *e, const char *key_descrip); STACK_OF(X509) *load_certs(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *cert_descrip); STACK_OF(X509_CRL) *load_crls(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *cert_descrip); X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath); # ifndef OPENSSL_NO_ENGINE ENGINE *setup_engine(BIO *err, const char *engine, int debug); # endif # ifndef OPENSSL_NO_OCSP OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req, const char *host, const char *path, const char *port, int use_ssl, const STACK_OF(CONF_VALUE) *headers, int req_timeout); # endif int load_config(BIO *err, CONF *cnf); char *make_config_name(void); /* Functions defined in ca.c and also used in ocsp.c */ int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str); # define DB_type 0 # define DB_exp_date 1 # define DB_rev_date 2 # define DB_serial 3 /* index - unique */ # define DB_file 4 # define DB_name 5 /* index - unique when active and not * disabled */ # define DB_NUMBER 6 # define DB_TYPE_REV 'R' # define DB_TYPE_EXP 'E' # define DB_TYPE_VAL 'V' typedef struct db_attr_st { int unique_subject; } DB_ATTR; typedef struct ca_db_st { DB_ATTR attributes; TXT_DB *db; } CA_DB; BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai); int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai); int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix); int rand_serial(BIGNUM *b, ASN1_INTEGER *ai); CA_DB *load_index(char *dbfile, DB_ATTR *dbattr); int index_index(CA_DB *db); int save_index(const char *dbfile, const char *suffix, CA_DB *db); int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix); void free_index(CA_DB *db); # define index_name_cmp_noconst(a, b) \ index_name_cmp((const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, a), \ (const OPENSSL_CSTRING *)CHECKED_PTR_OF(OPENSSL_STRING, b)) int index_name_cmp(const OPENSSL_CSTRING *a, const OPENSSL_CSTRING *b); int parse_yesno(const char *str, int def); X509_NAME *parse_name(char *str, long chtype, int multirdn); int args_verify(char ***pargs, int *pargc, int *badarg, BIO *err, X509_VERIFY_PARAM **pm); void policies_print(BIO *out, X509_STORE_CTX *ctx); int bio_to_mem(unsigned char **out, int maxlen, BIO *in); -int pkey_ctrl_string(EVP_PKEY_CTX *ctx, char *value); +int pkey_ctrl_string(EVP_PKEY_CTX *ctx, const char *value); int init_gen_str(BIO *err, EVP_PKEY_CTX **pctx, const char *algname, ENGINE *e, int do_param); int do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts); int do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts); int do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts); # ifndef OPENSSL_NO_PSK extern char *psk_key; # endif # ifndef OPENSSL_NO_JPAKE void jpake_client_auth(BIO *out, BIO *conn, const char *secret); void jpake_server_auth(BIO *out, BIO *conn, const char *secret); # endif # ifndef OPENSSL_NO_TLSEXT unsigned char *next_protos_parse(unsigned short *outlen, const char *in); # endif /* ndef OPENSSL_NO_TLSEXT */ void print_cert_checks(BIO *bio, X509 *x, const char *checkhost, const char *checkemail, const char *checkip); void store_setup_crl_download(X509_STORE *st); # define FORMAT_UNDEF 0 # define FORMAT_ASN1 1 # define FORMAT_TEXT 2 # define FORMAT_PEM 3 # define FORMAT_NETSCAPE 4 # define FORMAT_PKCS12 5 # define FORMAT_SMIME 6 # define FORMAT_ENGINE 7 # define FORMAT_IISSGC 8 /* XXX this stupid macro helps us to avoid * adding yet another param to load_*key() */ # define FORMAT_PEMRSA 9 /* PEM RSAPubicKey format */ # define FORMAT_ASN1RSA 10 /* DER RSAPubicKey format */ # define FORMAT_MSBLOB 11 /* MS Key blob format */ # define FORMAT_PVK 12 /* MS PVK file format */ # define FORMAT_HTTP 13 /* Download using HTTP */ # define EXT_COPY_NONE 0 # define EXT_COPY_ADD 1 # define EXT_COPY_ALL 2 # define NETSCAPE_CERT_HDR "certificate" # define APP_PASS_LEN 1024 # define SERIAL_RAND_BITS 64 int app_isdir(const char *); int raw_read_stdin(void *, int); int raw_write_stdout(const void *, int); # define TM_START 0 # define TM_STOP 1 double app_tminterval(int stop, int usertime); # define OPENSSL_NO_SSL_INTERN #endif Index: vendor-crypto/openssl/dist/apps/pkeyutl.c =================================================================== --- vendor-crypto/openssl/dist/apps/pkeyutl.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/pkeyutl.c (revision 296273) @@ -1,539 +1,555 @@ /* * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project * 2006. */ /* ==================================================================== * Copyright (c) 2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #include "apps.h" #include #include #include #include #define KEY_PRIVKEY 1 #define KEY_PUBKEY 2 #define KEY_CERT 3 static void usage(void); #undef PROG #define PROG pkeyutl_main static EVP_PKEY_CTX *init_ctx(int *pkeysize, - char *keyfile, int keyform, int key_type, + const char *keyfile, int keyform, int key_type, char *passargin, int pkey_op, ENGINE *e, int impl); static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform, const char *file, ENGINE* e); static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, unsigned char *out, size_t *poutlen, unsigned char *in, size_t inlen); int MAIN(int argc, char **); int MAIN(int argc, char **argv) { BIO *in = NULL, *out = NULL; char *infile = NULL, *outfile = NULL, *sigfile = NULL; ENGINE *e = NULL; int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY; int keyform = FORMAT_PEM, peerform = FORMAT_PEM; char badarg = 0, rev = 0; char hexdump = 0, asn1parse = 0; EVP_PKEY_CTX *ctx = NULL; char *passargin = NULL; int keysize = -1; int engine_impl = 0; - unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL; - size_t buf_outlen; + size_t buf_outlen = 0; int buf_inlen = 0, siglen = -1; + const char *inkey = NULL; + const char *peerkey = NULL; + STACK_OF(OPENSSL_STRING) *pkeyopts = NULL; int ret = 1, rv = -1; argc--; argv++; if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); while (argc >= 1) { if (!strcmp(*argv, "-in")) { if (--argc < 1) badarg = 1; else infile = *(++argv); } else if (!strcmp(*argv, "-out")) { if (--argc < 1) badarg = 1; else outfile = *(++argv); } else if (!strcmp(*argv, "-sigfile")) { if (--argc < 1) badarg = 1; else sigfile = *(++argv); } else if (!strcmp(*argv, "-inkey")) { if (--argc < 1) badarg = 1; - else { - ctx = init_ctx(&keysize, - *(++argv), keyform, key_type, - passargin, pkey_op, e, engine_impl); - if (!ctx) { - BIO_puts(bio_err, "Error initializing context\n"); - ERR_print_errors(bio_err); - badarg = 1; - } - } + else + inkey = *++argv; } else if (!strcmp(*argv, "-peerkey")) { if (--argc < 1) badarg = 1; - else if (!setup_peer(bio_err, ctx, peerform, *(++argv), e)) - badarg = 1; + else + peerkey = *++argv; } else if (!strcmp(*argv, "-passin")) { if (--argc < 1) badarg = 1; else passargin = *(++argv); } else if (strcmp(*argv, "-peerform") == 0) { if (--argc < 1) badarg = 1; else peerform = str2fmt(*(++argv)); } else if (strcmp(*argv, "-keyform") == 0) { if (--argc < 1) badarg = 1; else keyform = str2fmt(*(++argv)); } #ifndef OPENSSL_NO_ENGINE else if (!strcmp(*argv, "-engine")) { if (--argc < 1) badarg = 1; else e = setup_engine(bio_err, *(++argv), 0); } else if (!strcmp(*argv, "-engine_impl")) { engine_impl = 1; } #endif else if (!strcmp(*argv, "-pubin")) key_type = KEY_PUBKEY; else if (!strcmp(*argv, "-certin")) key_type = KEY_CERT; else if (!strcmp(*argv, "-asn1parse")) asn1parse = 1; else if (!strcmp(*argv, "-hexdump")) hexdump = 1; else if (!strcmp(*argv, "-sign")) pkey_op = EVP_PKEY_OP_SIGN; else if (!strcmp(*argv, "-verify")) pkey_op = EVP_PKEY_OP_VERIFY; else if (!strcmp(*argv, "-verifyrecover")) pkey_op = EVP_PKEY_OP_VERIFYRECOVER; - else if (!strcmp(*argv, "-rev")) - rev = 1; else if (!strcmp(*argv, "-encrypt")) pkey_op = EVP_PKEY_OP_ENCRYPT; else if (!strcmp(*argv, "-decrypt")) pkey_op = EVP_PKEY_OP_DECRYPT; else if (!strcmp(*argv, "-derive")) pkey_op = EVP_PKEY_OP_DERIVE; + else if (!strcmp(*argv, "-rev")) + rev = 1; else if (strcmp(*argv, "-pkeyopt") == 0) { if (--argc < 1) badarg = 1; - else if (!ctx) { - BIO_puts(bio_err, "-pkeyopt command before -inkey\n"); - badarg = 1; - } else if (pkey_ctrl_string(ctx, *(++argv)) <= 0) { - BIO_puts(bio_err, "parameter setting error\n"); - ERR_print_errors(bio_err); + else if ((pkeyopts == NULL && + (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) || + sk_OPENSSL_STRING_push(pkeyopts, *++argv) == 0) { + BIO_puts(bio_err, "out of memory\n"); goto end; } } else badarg = 1; if (badarg) { usage(); goto end; } argc--; argv++; } - if (!ctx) { + if (inkey == NULL || + (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) { usage(); goto end; } + ctx = init_ctx(&keysize, inkey, keyform, key_type, + passargin, pkey_op, e, engine_impl); + if (!ctx) { + BIO_puts(bio_err, "Error initializing context\n"); + ERR_print_errors(bio_err); + goto end; + } + if (peerkey != NULL && !setup_peer(bio_err, ctx, peerform, peerkey, e)) { + BIO_puts(bio_err, "Error setting up peer key\n"); + ERR_print_errors(bio_err); + goto end; + } + if (pkeyopts != NULL) { + int num = sk_OPENSSL_STRING_num(pkeyopts); + int i; + for (i = 0; i < num; ++i) { + const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i); + + if (pkey_ctrl_string(ctx, opt) <= 0) { + BIO_puts(bio_err, "parameter setting error\n"); + ERR_print_errors(bio_err); + goto end; + } + } + } + if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) { BIO_puts(bio_err, "Signature file specified for non verify\n"); goto end; } if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) { BIO_puts(bio_err, "No signature file specified for verify\n"); goto end; } /* FIXME: seed PRNG only if needed */ app_RAND_load_file(NULL, bio_err, 0); if (pkey_op != EVP_PKEY_OP_DERIVE) { if (infile) { if (!(in = BIO_new_file(infile, "rb"))) { BIO_puts(bio_err, "Error Opening Input File\n"); ERR_print_errors(bio_err); goto end; } } else in = BIO_new_fp(stdin, BIO_NOCLOSE); } if (outfile) { if (!(out = BIO_new_file(outfile, "wb"))) { BIO_printf(bio_err, "Error Creating Output File\n"); ERR_print_errors(bio_err); goto end; } } else { out = BIO_new_fp(stdout, BIO_NOCLOSE); #ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } #endif } if (sigfile) { BIO *sigbio = BIO_new_file(sigfile, "rb"); if (!sigbio) { BIO_printf(bio_err, "Can't open signature file %s\n", sigfile); goto end; } siglen = bio_to_mem(&sig, keysize * 10, sigbio); BIO_free(sigbio); - if (siglen <= 0) { + if (siglen < 0) { BIO_printf(bio_err, "Error reading signature data\n"); goto end; } } if (in) { /* Read the input data */ buf_inlen = bio_to_mem(&buf_in, keysize * 10, in); - if (buf_inlen <= 0) { + if (buf_inlen < 0) { BIO_printf(bio_err, "Error reading input Data\n"); exit(1); } if (rev) { size_t i; unsigned char ctmp; size_t l = (size_t)buf_inlen; for (i = 0; i < l / 2; i++) { ctmp = buf_in[i]; buf_in[i] = buf_in[l - 1 - i]; buf_in[l - 1 - i] = ctmp; } } } if (pkey_op == EVP_PKEY_OP_VERIFY) { rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen, buf_in, (size_t)buf_inlen); if (rv == 0) BIO_puts(out, "Signature Verification Failure\n"); else if (rv == 1) BIO_puts(out, "Signature Verified Successfully\n"); if (rv >= 0) goto end; } else { rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen, buf_in, (size_t)buf_inlen); - if (rv > 0) { + if (rv > 0 && buf_outlen != 0) { buf_out = OPENSSL_malloc(buf_outlen); if (!buf_out) rv = -1; else rv = do_keyop(ctx, pkey_op, buf_out, (size_t *)&buf_outlen, buf_in, (size_t)buf_inlen); } } if (rv <= 0) { BIO_printf(bio_err, "Public Key operation error\n"); ERR_print_errors(bio_err); goto end; } ret = 0; if (asn1parse) { if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1)) ERR_print_errors(bio_err); } else if (hexdump) BIO_dump(out, (char *)buf_out, buf_outlen); else BIO_write(out, buf_out, buf_outlen); end: if (ctx) EVP_PKEY_CTX_free(ctx); BIO_free(in); BIO_free_all(out); - if (buf_in) + if (buf_in != NULL) OPENSSL_free(buf_in); - if (buf_out) + if (buf_out != NULL) OPENSSL_free(buf_out); - if (sig) + if (sig != NULL) OPENSSL_free(sig); + if (pkeyopts != NULL) + sk_OPENSSL_STRING_free(pkeyopts); return ret; } static void usage() { BIO_printf(bio_err, "Usage: pkeyutl [options]\n"); BIO_printf(bio_err, "-in file input file\n"); BIO_printf(bio_err, "-out file output file\n"); BIO_printf(bio_err, "-sigfile file signature file (verify operation only)\n"); BIO_printf(bio_err, "-inkey file input key\n"); BIO_printf(bio_err, "-keyform arg private key format - default PEM\n"); BIO_printf(bio_err, "-pubin input is a public key\n"); BIO_printf(bio_err, "-certin input is a certificate carrying a public key\n"); BIO_printf(bio_err, "-pkeyopt X:Y public key options\n"); BIO_printf(bio_err, "-sign sign with private key\n"); BIO_printf(bio_err, "-verify verify with public key\n"); BIO_printf(bio_err, "-verifyrecover verify with public key, recover original data\n"); BIO_printf(bio_err, "-encrypt encrypt with public key\n"); BIO_printf(bio_err, "-decrypt decrypt with private key\n"); BIO_printf(bio_err, "-derive derive shared secret\n"); BIO_printf(bio_err, "-hexdump hex dump output\n"); #ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, "-engine e use engine e, maybe a hardware device, for loading keys.\n"); BIO_printf(bio_err, "-engine_impl also use engine given by -engine for crypto operations\n"); #endif BIO_printf(bio_err, "-passin arg pass phrase source\n"); } static EVP_PKEY_CTX *init_ctx(int *pkeysize, - char *keyfile, int keyform, int key_type, + const char *keyfile, int keyform, int key_type, char *passargin, int pkey_op, ENGINE *e, int engine_impl) { EVP_PKEY *pkey = NULL; EVP_PKEY_CTX *ctx = NULL; ENGINE *impl = NULL; char *passin = NULL; int rv = -1; X509 *x; if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT) || (pkey_op == EVP_PKEY_OP_DERIVE)) && (key_type != KEY_PRIVKEY)) { BIO_printf(bio_err, "A private key is needed for this operation\n"); goto end; } if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } switch (key_type) { case KEY_PRIVKEY: pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, "Private Key"); break; case KEY_PUBKEY: pkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL, e, "Public Key"); break; case KEY_CERT: x = load_cert(bio_err, keyfile, keyform, NULL, e, "Certificate"); if (x) { pkey = X509_get_pubkey(x); X509_free(x); } break; } *pkeysize = EVP_PKEY_size(pkey); if (!pkey) goto end; #ifndef OPENSSL_NO_ENGINE if (engine_impl) impl = e; #endif ctx = EVP_PKEY_CTX_new(pkey, impl); EVP_PKEY_free(pkey); if (!ctx) goto end; switch (pkey_op) { case EVP_PKEY_OP_SIGN: rv = EVP_PKEY_sign_init(ctx); break; case EVP_PKEY_OP_VERIFY: rv = EVP_PKEY_verify_init(ctx); break; case EVP_PKEY_OP_VERIFYRECOVER: rv = EVP_PKEY_verify_recover_init(ctx); break; case EVP_PKEY_OP_ENCRYPT: rv = EVP_PKEY_encrypt_init(ctx); break; case EVP_PKEY_OP_DECRYPT: rv = EVP_PKEY_decrypt_init(ctx); break; case EVP_PKEY_OP_DERIVE: rv = EVP_PKEY_derive_init(ctx); break; } if (rv <= 0) { EVP_PKEY_CTX_free(ctx); ctx = NULL; } end: if (passin) OPENSSL_free(passin); return ctx; } static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform, const char *file, ENGINE* e) { EVP_PKEY *peer = NULL; ENGINE* engine = NULL; int ret; - if (!ctx) { - BIO_puts(err, "-peerkey command before -inkey\n"); - return 0; - } if (peerform == FORMAT_ENGINE) - engine = e; - + engine = e; peer = load_pubkey(bio_err, file, peerform, 0, NULL, engine, "Peer Key"); if (!peer) { BIO_printf(bio_err, "Error reading peer key %s\n", file); ERR_print_errors(err); return 0; } ret = EVP_PKEY_derive_set_peer(ctx, peer); EVP_PKEY_free(peer); if (ret <= 0) ERR_print_errors(err); return ret; } static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, unsigned char *out, size_t *poutlen, unsigned char *in, size_t inlen) { int rv = 0; switch (pkey_op) { case EVP_PKEY_OP_VERIFYRECOVER: rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen); break; case EVP_PKEY_OP_SIGN: rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen); break; case EVP_PKEY_OP_ENCRYPT: rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen); break; case EVP_PKEY_OP_DECRYPT: rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen); break; case EVP_PKEY_OP_DERIVE: rv = EVP_PKEY_derive(ctx, out, poutlen); break; } return rv; } Index: vendor-crypto/openssl/dist/apps/req.c =================================================================== --- vendor-crypto/openssl/dist/apps/req.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/req.c (revision 296273) @@ -1,1732 +1,1732 @@ /* apps/req.c */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* * Until the key-gen callbacks are modified to use newer prototypes, we allow * deprecated functions for openssl-internal code */ #ifdef OPENSSL_NO_DEPRECATED # undef OPENSSL_NO_DEPRECATED #endif #include #include #include #include #ifdef OPENSSL_NO_STDIO # define APPS_WIN16 #endif #include "apps.h" #include #include #include #include #include #include #include #include #include #include #ifndef OPENSSL_NO_RSA # include #endif #ifndef OPENSSL_NO_DSA # include #endif #define SECTION "req" #define BITS "default_bits" #define KEYFILE "default_keyfile" #define PROMPT "prompt" #define DISTINGUISHED_NAME "distinguished_name" #define ATTRIBUTES "attributes" #define V3_EXTENSIONS "x509_extensions" #define REQ_EXTENSIONS "req_extensions" #define STRING_MASK "string_mask" #define UTF8_IN "utf8" -#define DEFAULT_KEY_LENGTH 512 -#define MIN_KEY_LENGTH 384 +#define DEFAULT_KEY_LENGTH 2048 +#define MIN_KEY_LENGTH 512 #undef PROG #define PROG req_main /*- * -inform arg - input format - default PEM (DER or PEM) * -outform arg - output format - default PEM * -in arg - input file - default stdin * -out arg - output file - default stdout * -verify - check request signature * -noout - don't print stuff out. * -text - print out human readable text. * -nodes - no des encryption * -config file - Load configuration file. * -key file - make a request using key in file (or use it for verification). * -keyform arg - key file format. * -rand file(s) - load the file(s) into the PRNG. * -newkey - make a key and a request. * -modulus - print RSA modulus. * -pubkey - output Public Key. * -x509 - output a self signed X509 structure instead. * -asn1-kludge - output new certificate request in a format that some CA's * require. This format is wrong */ static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *dn, int mutlirdn, int attribs, unsigned long chtype); static int build_subject(X509_REQ *req, char *subj, unsigned long chtype, int multirdn); static int prompt_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, unsigned long chtype); static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk, STACK_OF(CONF_VALUE) *attr, int attribs, unsigned long chtype); static int add_attribute_object(X509_REQ *req, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype); static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype, int mval); static int genpkey_cb(EVP_PKEY_CTX *ctx); static int req_check_len(int len, int n_min, int n_max); static int check_end(const char *str, const char *end); static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type, long *pkeylen, char **palgnam, ENGINE *keygen_engine); #ifndef MONOLITH static char *default_config_file = NULL; #endif static CONF *req_conf = NULL; static int batch = 0; int MAIN(int, char **); int MAIN(int argc, char **argv) { ENGINE *e = NULL, *gen_eng = NULL; unsigned long nmflag = 0, reqflag = 0; int ex = 1, x509 = 0, days = 30; X509 *x509ss = NULL; X509_REQ *req = NULL; EVP_PKEY_CTX *genctx = NULL; const char *keyalg = NULL; char *keyalgstr = NULL; STACK_OF(OPENSSL_STRING) *pkeyopts = NULL, *sigopts = NULL; EVP_PKEY *pkey = NULL; int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = -1; long newkey = -1; BIO *in = NULL, *out = NULL; int informat, outformat, verify = 0, noout = 0, text = 0, keyform = FORMAT_PEM; int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0; char *infile, *outfile, *prog, *keyfile = NULL, *template = NULL, *keyout = NULL; #ifndef OPENSSL_NO_ENGINE char *engine = NULL; #endif char *extensions = NULL; char *req_exts = NULL; const EVP_CIPHER *cipher = NULL; ASN1_INTEGER *serial = NULL; int modulus = 0; char *inrand = NULL; char *passargin = NULL, *passargout = NULL; char *passin = NULL, *passout = NULL; char *p; char *subj = NULL; int multirdn = 0; const EVP_MD *md_alg = NULL, *digest = NULL; unsigned long chtype = MBSTRING_ASC; #ifndef MONOLITH char *to_free; long errline; #endif req_conf = NULL; #ifndef OPENSSL_NO_DES cipher = EVP_des_ede3_cbc(); #endif apps_startup(); if (bio_err == NULL) if ((bio_err = BIO_new(BIO_s_file())) != NULL) BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); infile = NULL; outfile = NULL; informat = FORMAT_PEM; outformat = FORMAT_PEM; prog = argv[0]; argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-inform") == 0) { if (--argc < 1) goto bad; informat = str2fmt(*(++argv)); } else if (strcmp(*argv, "-outform") == 0) { if (--argc < 1) goto bad; outformat = str2fmt(*(++argv)); } #ifndef OPENSSL_NO_ENGINE else if (strcmp(*argv, "-engine") == 0) { if (--argc < 1) goto bad; engine = *(++argv); } else if (strcmp(*argv, "-keygen_engine") == 0) { if (--argc < 1) goto bad; gen_eng = ENGINE_by_id(*(++argv)); if (gen_eng == NULL) { BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv); goto end; } } #endif else if (strcmp(*argv, "-key") == 0) { if (--argc < 1) goto bad; keyfile = *(++argv); } else if (strcmp(*argv, "-pubkey") == 0) { pubkey = 1; } else if (strcmp(*argv, "-new") == 0) { newreq = 1; } else if (strcmp(*argv, "-config") == 0) { if (--argc < 1) goto bad; template = *(++argv); } else if (strcmp(*argv, "-keyform") == 0) { if (--argc < 1) goto bad; keyform = str2fmt(*(++argv)); } else if (strcmp(*argv, "-in") == 0) { if (--argc < 1) goto bad; infile = *(++argv); } else if (strcmp(*argv, "-out") == 0) { if (--argc < 1) goto bad; outfile = *(++argv); } else if (strcmp(*argv, "-keyout") == 0) { if (--argc < 1) goto bad; keyout = *(++argv); } else if (strcmp(*argv, "-passin") == 0) { if (--argc < 1) goto bad; passargin = *(++argv); } else if (strcmp(*argv, "-passout") == 0) { if (--argc < 1) goto bad; passargout = *(++argv); } else if (strcmp(*argv, "-rand") == 0) { if (--argc < 1) goto bad; inrand = *(++argv); } else if (strcmp(*argv, "-newkey") == 0) { if (--argc < 1) goto bad; keyalg = *(++argv); newreq = 1; } else if (strcmp(*argv, "-pkeyopt") == 0) { if (--argc < 1) goto bad; if (!pkeyopts) pkeyopts = sk_OPENSSL_STRING_new_null(); if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv))) goto bad; } else if (strcmp(*argv, "-sigopt") == 0) { if (--argc < 1) goto bad; if (!sigopts) sigopts = sk_OPENSSL_STRING_new_null(); if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv))) goto bad; } else if (strcmp(*argv, "-batch") == 0) batch = 1; else if (strcmp(*argv, "-newhdr") == 0) newhdr = 1; else if (strcmp(*argv, "-modulus") == 0) modulus = 1; else if (strcmp(*argv, "-verify") == 0) verify = 1; else if (strcmp(*argv, "-nodes") == 0) nodes = 1; else if (strcmp(*argv, "-noout") == 0) noout = 1; else if (strcmp(*argv, "-verbose") == 0) verbose = 1; else if (strcmp(*argv, "-utf8") == 0) chtype = MBSTRING_UTF8; else if (strcmp(*argv, "-nameopt") == 0) { if (--argc < 1) goto bad; if (!set_name_ex(&nmflag, *(++argv))) goto bad; } else if (strcmp(*argv, "-reqopt") == 0) { if (--argc < 1) goto bad; if (!set_cert_ex(&reqflag, *(++argv))) goto bad; } else if (strcmp(*argv, "-subject") == 0) subject = 1; else if (strcmp(*argv, "-text") == 0) text = 1; else if (strcmp(*argv, "-x509") == 0) x509 = 1; else if (strcmp(*argv, "-asn1-kludge") == 0) kludge = 1; else if (strcmp(*argv, "-no-asn1-kludge") == 0) kludge = 0; else if (strcmp(*argv, "-subj") == 0) { if (--argc < 1) goto bad; subj = *(++argv); } else if (strcmp(*argv, "-multivalue-rdn") == 0) multirdn = 1; else if (strcmp(*argv, "-days") == 0) { if (--argc < 1) goto bad; days = atoi(*(++argv)); if (days == 0) days = 30; } else if (strcmp(*argv, "-set_serial") == 0) { if (--argc < 1) goto bad; serial = s2i_ASN1_INTEGER(NULL, *(++argv)); if (!serial) goto bad; } else if (strcmp(*argv, "-extensions") == 0) { if (--argc < 1) goto bad; extensions = *(++argv); } else if (strcmp(*argv, "-reqexts") == 0) { if (--argc < 1) goto bad; req_exts = *(++argv); } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) { /* ok */ digest = md_alg; } else { BIO_printf(bio_err, "unknown option %s\n", *argv); badops = 1; break; } argc--; argv++; } if (badops) { bad: BIO_printf(bio_err, "%s [options] outfile\n", prog); BIO_printf(bio_err, "where options are\n"); BIO_printf(bio_err, " -inform arg input format - DER or PEM\n"); BIO_printf(bio_err, " -outform arg output format - DER or PEM\n"); BIO_printf(bio_err, " -in arg input file\n"); BIO_printf(bio_err, " -out arg output file\n"); BIO_printf(bio_err, " -text text form of request\n"); BIO_printf(bio_err, " -pubkey output public key\n"); BIO_printf(bio_err, " -noout do not output REQ\n"); BIO_printf(bio_err, " -verify verify signature on REQ\n"); BIO_printf(bio_err, " -modulus RSA modulus\n"); BIO_printf(bio_err, " -nodes don't encrypt the output key\n"); #ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device\n"); #endif BIO_printf(bio_err, " -subject output the request's subject\n"); BIO_printf(bio_err, " -passin private key password source\n"); BIO_printf(bio_err, " -key file use the private key contained in file\n"); BIO_printf(bio_err, " -keyform arg key file format\n"); BIO_printf(bio_err, " -keyout arg file to send the key to\n"); BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); BIO_printf(bio_err, " the random number generator\n"); BIO_printf(bio_err, " -newkey rsa:bits generate a new RSA key of 'bits' in size\n"); BIO_printf(bio_err, " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n"); #ifndef OPENSSL_NO_ECDSA BIO_printf(bio_err, " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n"); #endif BIO_printf(bio_err, " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n"); BIO_printf(bio_err, " -config file request template file.\n"); BIO_printf(bio_err, " -subj arg set or modify request subject\n"); BIO_printf(bio_err, " -multivalue-rdn enable support for multivalued RDNs\n"); BIO_printf(bio_err, " -new new request.\n"); BIO_printf(bio_err, " -batch do not ask anything during request generation\n"); BIO_printf(bio_err, " -x509 output a x509 structure instead of a cert. req.\n"); BIO_printf(bio_err, " -days number of days a certificate generated by -x509 is valid for.\n"); BIO_printf(bio_err, " -set_serial serial number to use for a certificate generated by -x509.\n"); BIO_printf(bio_err, " -newhdr output \"NEW\" in the header lines\n"); BIO_printf(bio_err, " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n"); BIO_printf(bio_err, " have been reported as requiring\n"); BIO_printf(bio_err, " -extensions .. specify certificate extension section (override value in config file)\n"); BIO_printf(bio_err, " -reqexts .. specify request extension section (override value in config file)\n"); BIO_printf(bio_err, " -utf8 input characters are UTF8 (default ASCII)\n"); BIO_printf(bio_err, " -nameopt arg - various certificate name options\n"); BIO_printf(bio_err, " -reqopt arg - various request text options\n\n"); goto end; } ERR_load_crypto_strings(); if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); goto end; } #ifndef MONOLITH /* else this has happened in openssl.c * (global `config') */ /* Lets load up our environment a little */ p = getenv("OPENSSL_CONF"); if (p == NULL) p = getenv("SSLEAY_CONF"); if (p == NULL) p = to_free = make_config_name(); default_config_file = p; config = NCONF_new(NULL); i = NCONF_load(config, p, &errline); #endif if (template != NULL) { long errline = -1; if (verbose) BIO_printf(bio_err, "Using configuration from %s\n", template); req_conf = NCONF_new(NULL); i = NCONF_load(req_conf, template, &errline); if (i == 0) { BIO_printf(bio_err, "error on line %ld of %s\n", errline, template); goto end; } } else { req_conf = config; if (req_conf == NULL) { BIO_printf(bio_err, "Unable to load config info from %s\n", default_config_file); if (newreq) goto end; } else if (verbose) BIO_printf(bio_err, "Using configuration from %s\n", default_config_file); } if (req_conf != NULL) { if (!load_config(bio_err, req_conf)) goto end; p = NCONF_get_string(req_conf, NULL, "oid_file"); if (p == NULL) ERR_clear_error(); if (p != NULL) { BIO *oid_bio; oid_bio = BIO_new_file(p, "r"); if (oid_bio == NULL) { /*- BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); ERR_print_errors(bio_err); */ } else { OBJ_create_objects(oid_bio); BIO_free(oid_bio); } } } if (!add_oid_section(bio_err, req_conf)) goto end; if (md_alg == NULL) { p = NCONF_get_string(req_conf, SECTION, "default_md"); if (p == NULL) ERR_clear_error(); if (p != NULL) { if ((md_alg = EVP_get_digestbyname(p)) != NULL) digest = md_alg; } } if (!extensions) { extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS); if (!extensions) ERR_clear_error(); } if (extensions) { /* Check syntax of file */ X509V3_CTX ctx; X509V3_set_ctx_test(&ctx); X509V3_set_nconf(&ctx, req_conf); if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) { BIO_printf(bio_err, "Error Loading extension section %s\n", extensions); goto end; } } if (!passin) { passin = NCONF_get_string(req_conf, SECTION, "input_password"); if (!passin) ERR_clear_error(); } if (!passout) { passout = NCONF_get_string(req_conf, SECTION, "output_password"); if (!passout) ERR_clear_error(); } p = NCONF_get_string(req_conf, SECTION, STRING_MASK); if (!p) ERR_clear_error(); if (p && !ASN1_STRING_set_default_mask_asc(p)) { BIO_printf(bio_err, "Invalid global string mask setting %s\n", p); goto end; } if (chtype != MBSTRING_UTF8) { p = NCONF_get_string(req_conf, SECTION, UTF8_IN); if (!p) ERR_clear_error(); else if (!strcmp(p, "yes")) chtype = MBSTRING_UTF8; } if (!req_exts) { req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS); if (!req_exts) ERR_clear_error(); } if (req_exts) { /* Check syntax of file */ X509V3_CTX ctx; X509V3_set_ctx_test(&ctx); X509V3_set_nconf(&ctx, req_conf); if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) { BIO_printf(bio_err, "Error Loading request extension section %s\n", req_exts); goto end; } } in = BIO_new(BIO_s_file()); out = BIO_new(BIO_s_file()); if ((in == NULL) || (out == NULL)) goto end; #ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine, 0); #endif if (keyfile != NULL) { pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, "Private Key"); if (!pkey) { /* * load_key() has already printed an appropriate message */ goto end; } else { char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE"); if (randfile == NULL) ERR_clear_error(); app_RAND_load_file(randfile, bio_err, 0); } } if (newreq && (pkey == NULL)) { char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE"); if (randfile == NULL) ERR_clear_error(); app_RAND_load_file(randfile, bio_err, 0); if (inrand) app_RAND_load_files(inrand); if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) { newkey = DEFAULT_KEY_LENGTH; } if (keyalg) { genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey, &keyalgstr, gen_eng); if (!genctx) goto end; } if (newkey < MIN_KEY_LENGTH && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) { BIO_printf(bio_err, "private key length is too short,\n"); BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n", MIN_KEY_LENGTH, newkey); goto end; } if (!genctx) { genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey, &keyalgstr, gen_eng); if (!genctx) goto end; } if (pkeyopts) { char *genopt; for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) { genopt = sk_OPENSSL_STRING_value(pkeyopts, i); if (pkey_ctrl_string(genctx, genopt) <= 0) { BIO_printf(bio_err, "parameter error \"%s\"\n", genopt); ERR_print_errors(bio_err); goto end; } } } BIO_printf(bio_err, "Generating a %ld bit %s private key\n", newkey, keyalgstr); EVP_PKEY_CTX_set_cb(genctx, genpkey_cb); EVP_PKEY_CTX_set_app_data(genctx, bio_err); if (EVP_PKEY_keygen(genctx, &pkey) <= 0) { BIO_puts(bio_err, "Error Generating Key\n"); goto end; } EVP_PKEY_CTX_free(genctx); genctx = NULL; app_RAND_write_file(randfile, bio_err); if (keyout == NULL) { keyout = NCONF_get_string(req_conf, SECTION, KEYFILE); if (keyout == NULL) ERR_clear_error(); } if (keyout == NULL) { BIO_printf(bio_err, "writing new private key to stdout\n"); BIO_set_fp(out, stdout, BIO_NOCLOSE); #ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } #endif } else { BIO_printf(bio_err, "writing new private key to '%s'\n", keyout); if (BIO_write_filename(out, keyout) <= 0) { perror(keyout); goto end; } } p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key"); if (p == NULL) { ERR_clear_error(); p = NCONF_get_string(req_conf, SECTION, "encrypt_key"); if (p == NULL) ERR_clear_error(); } if ((p != NULL) && (strcmp(p, "no") == 0)) cipher = NULL; if (nodes) cipher = NULL; i = 0; loop: if (!PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, passout)) { if ((ERR_GET_REASON(ERR_peek_error()) == PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) { ERR_clear_error(); i++; goto loop; } goto end; } BIO_printf(bio_err, "-----\n"); } if (!newreq) { /* * Since we are using a pre-existing certificate request, the kludge * 'format' info should not be changed. */ kludge = -1; if (infile == NULL) BIO_set_fp(in, stdin, BIO_NOCLOSE); else { if (BIO_read_filename(in, infile) <= 0) { perror(infile); goto end; } } if (informat == FORMAT_ASN1) req = d2i_X509_REQ_bio(in, NULL); else if (informat == FORMAT_PEM) req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); else { BIO_printf(bio_err, "bad input format specified for X509 request\n"); goto end; } if (req == NULL) { BIO_printf(bio_err, "unable to load X509 request\n"); goto end; } } if (newreq || x509) { if (pkey == NULL) { BIO_printf(bio_err, "you need to specify a private key\n"); goto end; } if (req == NULL) { req = X509_REQ_new(); if (req == NULL) { goto end; } i = make_REQ(req, pkey, subj, multirdn, !x509, chtype); subj = NULL; /* done processing '-subj' option */ if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) { sk_X509_ATTRIBUTE_free(req->req_info->attributes); req->req_info->attributes = NULL; } if (!i) { BIO_printf(bio_err, "problems making Certificate Request\n"); goto end; } } if (x509) { EVP_PKEY *tmppkey; X509V3_CTX ext_ctx; if ((x509ss = X509_new()) == NULL) goto end; /* Set version to V3 */ if (extensions && !X509_set_version(x509ss, 2)) goto end; if (serial) { if (!X509_set_serialNumber(x509ss, serial)) goto end; } else { if (!rand_serial(NULL, X509_get_serialNumber(x509ss))) goto end; } if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end; if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0)) goto end; if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL)) goto end; if (!X509_set_subject_name (x509ss, X509_REQ_get_subject_name(req))) goto end; tmppkey = X509_REQ_get_pubkey(req); if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey)) goto end; EVP_PKEY_free(tmppkey); /* Set up V3 context struct */ X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0); X509V3_set_nconf(&ext_ctx, req_conf); /* Add extensions */ if (extensions && !X509V3_EXT_add_nconf(req_conf, &ext_ctx, extensions, x509ss)) { BIO_printf(bio_err, "Error Loading extension section %s\n", extensions); goto end; } i = do_X509_sign(bio_err, x509ss, pkey, digest, sigopts); if (!i) { ERR_print_errors(bio_err); goto end; } } else { X509V3_CTX ext_ctx; /* Set up V3 context struct */ X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0); X509V3_set_nconf(&ext_ctx, req_conf); /* Add extensions */ if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, &ext_ctx, req_exts, req)) { BIO_printf(bio_err, "Error Loading extension section %s\n", req_exts); goto end; } i = do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts); if (!i) { ERR_print_errors(bio_err); goto end; } } } if (subj && x509) { BIO_printf(bio_err, "Cannot modifiy certificate subject\n"); goto end; } if (subj && !x509) { if (verbose) { BIO_printf(bio_err, "Modifying Request's Subject\n"); print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag); } if (build_subject(req, subj, chtype, multirdn) == 0) { BIO_printf(bio_err, "ERROR: cannot modify subject\n"); ex = 1; goto end; } req->req_info->enc.modified = 1; if (verbose) { print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag); } } if (verify && !x509) { int tmp = 0; if (pkey == NULL) { pkey = X509_REQ_get_pubkey(req); tmp = 1; if (pkey == NULL) goto end; } i = X509_REQ_verify(req, pkey); if (tmp) { EVP_PKEY_free(pkey); pkey = NULL; } if (i < 0) { goto end; } else if (i == 0) { BIO_printf(bio_err, "verify failure\n"); ERR_print_errors(bio_err); } else /* if (i > 0) */ BIO_printf(bio_err, "verify OK\n"); } if (noout && !text && !modulus && !subject && !pubkey) { ex = 0; goto end; } if (outfile == NULL) { BIO_set_fp(out, stdout, BIO_NOCLOSE); #ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } #endif } else { if ((keyout != NULL) && (strcmp(outfile, keyout) == 0)) i = (int)BIO_append_filename(out, outfile); else i = (int)BIO_write_filename(out, outfile); if (!i) { perror(outfile); goto end; } } if (pubkey) { EVP_PKEY *tpubkey; tpubkey = X509_REQ_get_pubkey(req); if (tpubkey == NULL) { BIO_printf(bio_err, "Error getting public key\n"); ERR_print_errors(bio_err); goto end; } PEM_write_bio_PUBKEY(out, tpubkey); EVP_PKEY_free(tpubkey); } if (text) { if (x509) X509_print_ex(out, x509ss, nmflag, reqflag); else X509_REQ_print_ex(out, req, nmflag, reqflag); } if (subject) { if (x509) print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag); else print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag); } if (modulus) { EVP_PKEY *tpubkey; if (x509) tpubkey = X509_get_pubkey(x509ss); else tpubkey = X509_REQ_get_pubkey(req); if (tpubkey == NULL) { fprintf(stdout, "Modulus=unavailable\n"); goto end; } fprintf(stdout, "Modulus="); #ifndef OPENSSL_NO_RSA if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA) BN_print(out, tpubkey->pkey.rsa->n); else #endif fprintf(stdout, "Wrong Algorithm type"); EVP_PKEY_free(tpubkey); fprintf(stdout, "\n"); } if (!noout && !x509) { if (outformat == FORMAT_ASN1) i = i2d_X509_REQ_bio(out, req); else if (outformat == FORMAT_PEM) { if (newhdr) i = PEM_write_bio_X509_REQ_NEW(out, req); else i = PEM_write_bio_X509_REQ(out, req); } else { BIO_printf(bio_err, "bad output format specified for outfile\n"); goto end; } if (!i) { BIO_printf(bio_err, "unable to write X509 request\n"); goto end; } } if (!noout && x509 && (x509ss != NULL)) { if (outformat == FORMAT_ASN1) i = i2d_X509_bio(out, x509ss); else if (outformat == FORMAT_PEM) i = PEM_write_bio_X509(out, x509ss); else { BIO_printf(bio_err, "bad output format specified for outfile\n"); goto end; } if (!i) { BIO_printf(bio_err, "unable to write X509 certificate\n"); goto end; } } ex = 0; end: #ifndef MONOLITH if (to_free) OPENSSL_free(to_free); #endif if (ex) { ERR_print_errors(bio_err); } if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf); BIO_free(in); BIO_free_all(out); EVP_PKEY_free(pkey); if (genctx) EVP_PKEY_CTX_free(genctx); if (pkeyopts) sk_OPENSSL_STRING_free(pkeyopts); if (sigopts) sk_OPENSSL_STRING_free(sigopts); #ifndef OPENSSL_NO_ENGINE if (gen_eng) ENGINE_free(gen_eng); #endif if (keyalgstr) OPENSSL_free(keyalgstr); X509_REQ_free(req); X509_free(x509ss); ASN1_INTEGER_free(serial); if (passargin && passin) OPENSSL_free(passin); if (passargout && passout) OPENSSL_free(passout); OBJ_cleanup(); apps_shutdown(); OPENSSL_EXIT(ex); } static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn, int attribs, unsigned long chtype) { int ret = 0, i; char no_prompt = 0; STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL; char *tmp, *dn_sect, *attr_sect; tmp = NCONF_get_string(req_conf, SECTION, PROMPT); if (tmp == NULL) ERR_clear_error(); if ((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1; dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME); if (dn_sect == NULL) { BIO_printf(bio_err, "unable to find '%s' in config\n", DISTINGUISHED_NAME); goto err; } dn_sk = NCONF_get_section(req_conf, dn_sect); if (dn_sk == NULL) { BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect); goto err; } attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES); if (attr_sect == NULL) { ERR_clear_error(); attr_sk = NULL; } else { attr_sk = NCONF_get_section(req_conf, attr_sect); if (attr_sk == NULL) { BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect); goto err; } } /* setup version number */ if (!X509_REQ_set_version(req, 0L)) goto err; /* version 1 */ if (no_prompt) i = auto_info(req, dn_sk, attr_sk, attribs, chtype); else { if (subj) i = build_subject(req, subj, chtype, multirdn); else i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype); } if (!i) goto err; if (!X509_REQ_set_pubkey(req, pkey)) goto err; ret = 1; err: return (ret); } /* * subject is expected to be in the format /type0=value0/type1=value1/type2=... * where characters may be escaped by \ */ static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn) { X509_NAME *n; if (!(n = parse_name(subject, chtype, multirdn))) return 0; if (!X509_REQ_set_subject_name(req, n)) { X509_NAME_free(n); return 0; } X509_NAME_free(n); return 1; } static int prompt_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect, STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs, unsigned long chtype) { int i; char *p, *q; char buf[100]; int nid, mval; long n_min, n_max; char *type, *value; const char *def; CONF_VALUE *v; X509_NAME *subj; subj = X509_REQ_get_subject_name(req); if (!batch) { BIO_printf(bio_err, "You are about to be asked to enter information that will be incorporated\n"); BIO_printf(bio_err, "into your certificate request.\n"); BIO_printf(bio_err, "What you are about to enter is what is called a Distinguished Name or a DN.\n"); BIO_printf(bio_err, "There are quite a few fields but you can leave some blank\n"); BIO_printf(bio_err, "For some fields there will be a default value,\n"); BIO_printf(bio_err, "If you enter '.', the field will be left blank.\n"); BIO_printf(bio_err, "-----\n"); } if (sk_CONF_VALUE_num(dn_sk)) { i = -1; start:for (;;) { i++; if (sk_CONF_VALUE_num(dn_sk) <= i) break; v = sk_CONF_VALUE_value(dn_sk, i); p = q = NULL; type = v->name; if (!check_end(type, "_min") || !check_end(type, "_max") || !check_end(type, "_default") || !check_end(type, "_value")) continue; /* * Skip past any leading X. X: X, etc to allow for multiple * instances */ for (p = v->name; *p; p++) if ((*p == ':') || (*p == ',') || (*p == '.')) { p++; if (*p) type = p; break; } if (*type == '+') { mval = -1; type++; } else mval = 0; /* If OBJ not recognised ignore it */ if ((nid = OBJ_txt2nid(type)) == NID_undef) goto start; if (BIO_snprintf(buf, sizeof buf, "%s_default", v->name) >= (int)sizeof(buf)) { BIO_printf(bio_err, "Name '%s' too long\n", v->name); return 0; } if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) { ERR_clear_error(); def = ""; } BIO_snprintf(buf, sizeof buf, "%s_value", v->name); if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) { ERR_clear_error(); value = NULL; } BIO_snprintf(buf, sizeof buf, "%s_min", v->name); if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) { ERR_clear_error(); n_min = -1; } BIO_snprintf(buf, sizeof buf, "%s_max", v->name); if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) { ERR_clear_error(); n_max = -1; } if (!add_DN_object(subj, v->value, def, value, nid, n_min, n_max, chtype, mval)) return 0; } if (X509_NAME_entry_count(subj) == 0) { BIO_printf(bio_err, "error, no objects specified in config file\n"); return 0; } if (attribs) { if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch)) { BIO_printf(bio_err, "\nPlease enter the following 'extra' attributes\n"); BIO_printf(bio_err, "to be sent with your certificate request\n"); } i = -1; start2: for (;;) { i++; if ((attr_sk == NULL) || (sk_CONF_VALUE_num(attr_sk) <= i)) break; v = sk_CONF_VALUE_value(attr_sk, i); type = v->name; if ((nid = OBJ_txt2nid(type)) == NID_undef) goto start2; if (BIO_snprintf(buf, sizeof buf, "%s_default", type) >= (int)sizeof(buf)) { BIO_printf(bio_err, "Name '%s' too long\n", v->name); return 0; } if ((def = NCONF_get_string(req_conf, attr_sect, buf)) == NULL) { ERR_clear_error(); def = ""; } BIO_snprintf(buf, sizeof buf, "%s_value", type); if ((value = NCONF_get_string(req_conf, attr_sect, buf)) == NULL) { ERR_clear_error(); value = NULL; } BIO_snprintf(buf, sizeof buf, "%s_min", type); if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) { ERR_clear_error(); n_min = -1; } BIO_snprintf(buf, sizeof buf, "%s_max", type); if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) { ERR_clear_error(); n_max = -1; } if (!add_attribute_object(req, v->value, def, value, nid, n_min, n_max, chtype)) return 0; } } } else { BIO_printf(bio_err, "No template, please set one up.\n"); return 0; } return 1; } static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk, STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype) { int i; char *p, *q; char *type; CONF_VALUE *v; X509_NAME *subj; subj = X509_REQ_get_subject_name(req); for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { int mval; v = sk_CONF_VALUE_value(dn_sk, i); p = q = NULL; type = v->name; /* * Skip past any leading X. X: X, etc to allow for multiple instances */ for (p = v->name; *p; p++) #ifndef CHARSET_EBCDIC if ((*p == ':') || (*p == ',') || (*p == '.')) { #else if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) { #endif p++; if (*p) type = p; break; } #ifndef CHARSET_EBCDIC if (*p == '+') #else if (*p == os_toascii['+']) #endif { p++; mval = -1; } else mval = 0; if (!X509_NAME_add_entry_by_txt(subj, type, chtype, (unsigned char *)v->value, -1, -1, mval)) return 0; } if (!X509_NAME_entry_count(subj)) { BIO_printf(bio_err, "error, no objects specified in config file\n"); return 0; } if (attribs) { for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) { v = sk_CONF_VALUE_value(attr_sk, i); if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype, (unsigned char *)v->value, -1)) return 0; } } return 1; } static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype, int mval) { int i, ret = 0; MS_STATIC char buf[1024]; start: if (!batch) BIO_printf(bio_err, "%s [%s]:", text, def); (void)BIO_flush(bio_err); if (value != NULL) { BUF_strlcpy(buf, value, sizeof buf); BUF_strlcat(buf, "\n", sizeof buf); BIO_printf(bio_err, "%s\n", value); } else { buf[0] = '\0'; if (!batch) { if (!fgets(buf, sizeof buf, stdin)) return 0; } else { buf[0] = '\n'; buf[1] = '\0'; } } if (buf[0] == '\0') return (0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return (1); BUF_strlcpy(buf, def, sizeof buf); BUF_strlcat(buf, "\n", sizeof buf); } else if ((buf[0] == '.') && (buf[1] == '\n')) return (1); i = strlen(buf); if (buf[i - 1] != '\n') { BIO_printf(bio_err, "weird input :-(\n"); return (0); } buf[--i] = '\0'; #ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, i); #endif if (!req_check_len(i, n_min, n_max)) { if (batch || value) return 0; goto start; } if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char *)buf, -1, -1, mval)) goto err; ret = 1; err: return (ret); } static int add_attribute_object(X509_REQ *req, char *text, const char *def, char *value, int nid, int n_min, int n_max, unsigned long chtype) { int i; static char buf[1024]; start: if (!batch) BIO_printf(bio_err, "%s [%s]:", text, def); (void)BIO_flush(bio_err); if (value != NULL) { BUF_strlcpy(buf, value, sizeof buf); BUF_strlcat(buf, "\n", sizeof buf); BIO_printf(bio_err, "%s\n", value); } else { buf[0] = '\0'; if (!batch) { if (!fgets(buf, sizeof buf, stdin)) return 0; } else { buf[0] = '\n'; buf[1] = '\0'; } } if (buf[0] == '\0') return (0); else if (buf[0] == '\n') { if ((def == NULL) || (def[0] == '\0')) return (1); BUF_strlcpy(buf, def, sizeof buf); BUF_strlcat(buf, "\n", sizeof buf); } else if ((buf[0] == '.') && (buf[1] == '\n')) return (1); i = strlen(buf); if (buf[i - 1] != '\n') { BIO_printf(bio_err, "weird input :-(\n"); return (0); } buf[--i] = '\0'; #ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, i); #endif if (!req_check_len(i, n_min, n_max)) { if (batch || value) return 0; goto start; } if (!X509_REQ_add1_attr_by_NID(req, nid, chtype, (unsigned char *)buf, -1)) { BIO_printf(bio_err, "Error adding attribute\n"); ERR_print_errors(bio_err); goto err; } return (1); err: return (0); } static int req_check_len(int len, int n_min, int n_max) { if ((n_min > 0) && (len < n_min)) { BIO_printf(bio_err, "string is too short, it needs to be at least %d bytes long\n", n_min); return (0); } if ((n_max >= 0) && (len > n_max)) { BIO_printf(bio_err, "string is too long, it needs to be less than %d bytes long\n", n_max); return (0); } return (1); } /* Check if the end of a string matches 'end' */ static int check_end(const char *str, const char *end) { int elen, slen; const char *tmp; elen = strlen(end); slen = strlen(str); if (elen > slen) return 1; tmp = str + slen - elen; return strcmp(tmp, end); } static EVP_PKEY_CTX *set_keygen_ctx(BIO *err, const char *gstr, int *pkey_type, long *pkeylen, char **palgnam, ENGINE *keygen_engine) { EVP_PKEY_CTX *gctx = NULL; EVP_PKEY *param = NULL; long keylen = -1; BIO *pbio = NULL; const char *paramfile = NULL; if (gstr == NULL) { *pkey_type = EVP_PKEY_RSA; keylen = *pkeylen; } else if (gstr[0] >= '0' && gstr[0] <= '9') { *pkey_type = EVP_PKEY_RSA; keylen = atol(gstr); *pkeylen = keylen; } else if (!strncmp(gstr, "param:", 6)) paramfile = gstr + 6; else { const char *p = strchr(gstr, ':'); int len; ENGINE *tmpeng; const EVP_PKEY_ASN1_METHOD *ameth; if (p) len = p - gstr; else len = strlen(gstr); /* * The lookup of a the string will cover all engines so keep a note * of the implementation. */ ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len); if (!ameth) { BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr); return NULL; } EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, ameth); #ifndef OPENSSL_NO_ENGINE if (tmpeng) ENGINE_finish(tmpeng); #endif if (*pkey_type == EVP_PKEY_RSA) { if (p) { keylen = atol(p + 1); *pkeylen = keylen; } else keylen = *pkeylen; } else if (p) paramfile = p + 1; } if (paramfile) { pbio = BIO_new_file(paramfile, "r"); if (!pbio) { BIO_printf(err, "Can't open parameter file %s\n", paramfile); return NULL; } param = PEM_read_bio_Parameters(pbio, NULL); if (!param) { X509 *x; (void)BIO_reset(pbio); x = PEM_read_bio_X509(pbio, NULL, NULL, NULL); if (x) { param = X509_get_pubkey(x); X509_free(x); } } BIO_free(pbio); if (!param) { BIO_printf(err, "Error reading parameter file %s\n", paramfile); return NULL; } if (*pkey_type == -1) *pkey_type = EVP_PKEY_id(param); else if (*pkey_type != EVP_PKEY_base_id(param)) { BIO_printf(err, "Key Type does not match parameters\n"); EVP_PKEY_free(param); return NULL; } } if (palgnam) { const EVP_PKEY_ASN1_METHOD *ameth; ENGINE *tmpeng; const char *anam; ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type); if (!ameth) { BIO_puts(err, "Internal error: can't find key algorithm\n"); return NULL; } EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth); *palgnam = BUF_strdup(anam); #ifndef OPENSSL_NO_ENGINE if (tmpeng) ENGINE_finish(tmpeng); #endif } if (param) { gctx = EVP_PKEY_CTX_new(param, keygen_engine); *pkeylen = EVP_PKEY_bits(param); EVP_PKEY_free(param); } else gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine); if (!gctx) { BIO_puts(err, "Error allocating keygen context\n"); ERR_print_errors(err); return NULL; } if (EVP_PKEY_keygen_init(gctx) <= 0) { BIO_puts(err, "Error initializing keygen context\n"); ERR_print_errors(err); return NULL; } #ifndef OPENSSL_NO_RSA if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) { if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) { BIO_puts(err, "Error setting RSA keysize\n"); ERR_print_errors(err); EVP_PKEY_CTX_free(gctx); return NULL; } } #endif return gctx; } static int genpkey_cb(EVP_PKEY_CTX *ctx) { char c = '*'; BIO *b = EVP_PKEY_CTX_get_app_data(ctx); int p; p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); if (p == 0) c = '.'; if (p == 1) c = '+'; if (p == 2) c = '*'; if (p == 3) c = '\n'; BIO_write(b, &c, 1); (void)BIO_flush(b); #ifdef LINT p = n; #endif return 1; } static int do_sign_init(BIO *err, EVP_MD_CTX *ctx, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts) { EVP_PKEY_CTX *pkctx = NULL; int i; EVP_MD_CTX_init(ctx); if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey)) return 0; for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) { char *sigopt = sk_OPENSSL_STRING_value(sigopts, i); if (pkey_ctrl_string(pkctx, sigopt) <= 0) { BIO_printf(err, "parameter error \"%s\"\n", sigopt); ERR_print_errors(bio_err); return 0; } } return 1; } int do_X509_sign(BIO *err, X509 *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts) { int rv; EVP_MD_CTX mctx; EVP_MD_CTX_init(&mctx); rv = do_sign_init(err, &mctx, pkey, md, sigopts); if (rv > 0) rv = X509_sign_ctx(x, &mctx); EVP_MD_CTX_cleanup(&mctx); return rv > 0 ? 1 : 0; } int do_X509_REQ_sign(BIO *err, X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts) { int rv; EVP_MD_CTX mctx; EVP_MD_CTX_init(&mctx); rv = do_sign_init(err, &mctx, pkey, md, sigopts); if (rv > 0) rv = X509_REQ_sign_ctx(x, &mctx); EVP_MD_CTX_cleanup(&mctx); return rv > 0 ? 1 : 0; } int do_X509_CRL_sign(BIO *err, X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md, STACK_OF(OPENSSL_STRING) *sigopts) { int rv; EVP_MD_CTX mctx; EVP_MD_CTX_init(&mctx); rv = do_sign_init(err, &mctx, pkey, md, sigopts); if (rv > 0) rv = X509_CRL_sign_ctx(x, &mctx); EVP_MD_CTX_cleanup(&mctx); return rv > 0 ? 1 : 0; } Index: vendor-crypto/openssl/dist/apps/rsautl.c =================================================================== --- vendor-crypto/openssl/dist/apps/rsautl.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/rsautl.c (revision 296273) @@ -1,375 +1,375 @@ /* rsautl.c */ /* * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project * 2000. */ /* ==================================================================== * Copyright (c) 2000 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * licensing@OpenSSL.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ #include #ifndef OPENSSL_NO_RSA # include "apps.h" # include # include # include # include # define RSA_SIGN 1 # define RSA_VERIFY 2 # define RSA_ENCRYPT 3 # define RSA_DECRYPT 4 # define KEY_PRIVKEY 1 # define KEY_PUBKEY 2 # define KEY_CERT 3 static void usage(void); # undef PROG # define PROG rsautl_main int MAIN(int argc, char **); int MAIN(int argc, char **argv) { ENGINE *e = NULL; BIO *in = NULL, *out = NULL; char *infile = NULL, *outfile = NULL; # ifndef OPENSSL_NO_ENGINE char *engine = NULL; # endif char *keyfile = NULL; char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY; int keyform = FORMAT_PEM; char need_priv = 0, badarg = 0, rev = 0; char hexdump = 0, asn1parse = 0; X509 *x; EVP_PKEY *pkey = NULL; RSA *rsa = NULL; unsigned char *rsa_in = NULL, *rsa_out = NULL, pad; char *passargin = NULL, *passin = NULL; int rsa_inlen, rsa_outlen = 0; int keysize; int ret = 1; argc--; argv++; if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); pad = RSA_PKCS1_PADDING; while (argc >= 1) { if (!strcmp(*argv, "-in")) { if (--argc < 1) badarg = 1; else infile = *(++argv); } else if (!strcmp(*argv, "-out")) { if (--argc < 1) badarg = 1; else outfile = *(++argv); } else if (!strcmp(*argv, "-inkey")) { if (--argc < 1) badarg = 1; else keyfile = *(++argv); } else if (!strcmp(*argv, "-passin")) { if (--argc < 1) badarg = 1; else passargin = *(++argv); } else if (strcmp(*argv, "-keyform") == 0) { if (--argc < 1) badarg = 1; else keyform = str2fmt(*(++argv)); # ifndef OPENSSL_NO_ENGINE } else if (!strcmp(*argv, "-engine")) { if (--argc < 1) badarg = 1; else engine = *(++argv); # endif } else if (!strcmp(*argv, "-pubin")) { key_type = KEY_PUBKEY; } else if (!strcmp(*argv, "-certin")) { key_type = KEY_CERT; } else if (!strcmp(*argv, "-asn1parse")) asn1parse = 1; else if (!strcmp(*argv, "-hexdump")) hexdump = 1; else if (!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING; else if (!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING; else if (!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING; else if (!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING; else if (!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING; else if (!strcmp(*argv, "-sign")) { rsa_mode = RSA_SIGN; need_priv = 1; } else if (!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY; else if (!strcmp(*argv, "-rev")) rev = 1; else if (!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT; else if (!strcmp(*argv, "-decrypt")) { rsa_mode = RSA_DECRYPT; need_priv = 1; } else badarg = 1; if (badarg) { usage(); goto end; } argc--; argv++; } if (need_priv && (key_type != KEY_PRIVKEY)) { BIO_printf(bio_err, "A private key is needed for this operation\n"); goto end; } # ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine, 0); # endif if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } /* FIXME: seed PRNG only if needed */ app_RAND_load_file(NULL, bio_err, 0); switch (key_type) { case KEY_PRIVKEY: pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, "Private Key"); break; case KEY_PUBKEY: pkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL, e, "Public Key"); break; case KEY_CERT: x = load_cert(bio_err, keyfile, keyform, NULL, e, "Certificate"); if (x) { pkey = X509_get_pubkey(x); X509_free(x); } break; } if (!pkey) { return 1; } rsa = EVP_PKEY_get1_RSA(pkey); EVP_PKEY_free(pkey); if (!rsa) { BIO_printf(bio_err, "Error getting RSA key\n"); ERR_print_errors(bio_err); goto end; } if (infile) { if (!(in = BIO_new_file(infile, "rb"))) { BIO_printf(bio_err, "Error Reading Input File\n"); ERR_print_errors(bio_err); goto end; } } else in = BIO_new_fp(stdin, BIO_NOCLOSE); if (outfile) { if (!(out = BIO_new_file(outfile, "wb"))) { - BIO_printf(bio_err, "Error Reading Output File\n"); + BIO_printf(bio_err, "Error Writing Output File\n"); ERR_print_errors(bio_err); goto end; } } else { out = BIO_new_fp(stdout, BIO_NOCLOSE); # ifdef OPENSSL_SYS_VMS { BIO *tmpbio = BIO_new(BIO_f_linebuffer()); out = BIO_push(tmpbio, out); } # endif } keysize = RSA_size(rsa); rsa_in = OPENSSL_malloc(keysize * 2); rsa_out = OPENSSL_malloc(keysize); if (!rsa_in || !rsa_out) { BIO_printf(bio_err, "Out of memory\n"); ERR_print_errors(bio_err); goto end; } /* Read the input data */ rsa_inlen = BIO_read(in, rsa_in, keysize * 2); - if (rsa_inlen <= 0) { + if (rsa_inlen < 0) { BIO_printf(bio_err, "Error reading input Data\n"); exit(1); } if (rev) { int i; unsigned char ctmp; for (i = 0; i < rsa_inlen / 2; i++) { ctmp = rsa_in[i]; rsa_in[i] = rsa_in[rsa_inlen - 1 - i]; rsa_in[rsa_inlen - 1 - i] = ctmp; } } switch (rsa_mode) { case RSA_VERIFY: rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); break; case RSA_SIGN: rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); break; case RSA_ENCRYPT: rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); break; case RSA_DECRYPT: rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); break; } - if (rsa_outlen <= 0) { + if (rsa_outlen < 0) { BIO_printf(bio_err, "RSA operation error\n"); ERR_print_errors(bio_err); goto end; } ret = 0; if (asn1parse) { if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) { ERR_print_errors(bio_err); } } else if (hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen); else BIO_write(out, rsa_out, rsa_outlen); end: RSA_free(rsa); BIO_free(in); BIO_free_all(out); if (rsa_in) OPENSSL_free(rsa_in); if (rsa_out) OPENSSL_free(rsa_out); if (passin) OPENSSL_free(passin); return ret; } static void usage() { BIO_printf(bio_err, "Usage: rsautl [options]\n"); BIO_printf(bio_err, "-in file input file\n"); BIO_printf(bio_err, "-out file output file\n"); BIO_printf(bio_err, "-inkey file input key\n"); BIO_printf(bio_err, "-keyform arg private key format - default PEM\n"); BIO_printf(bio_err, "-pubin input is an RSA public\n"); BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n"); BIO_printf(bio_err, "-ssl use SSL v2 padding\n"); BIO_printf(bio_err, "-raw use no padding\n"); BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n"); BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n"); BIO_printf(bio_err, "-sign sign with private key\n"); BIO_printf(bio_err, "-verify verify with public key\n"); BIO_printf(bio_err, "-encrypt encrypt with public key\n"); BIO_printf(bio_err, "-decrypt decrypt with private key\n"); BIO_printf(bio_err, "-hexdump hex dump output\n"); # ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); BIO_printf(bio_err, "-passin arg pass phrase source\n"); # endif } #else /* !OPENSSL_NO_RSA */ # if PEDANTIC static void *dummy = &dummy; # endif #endif Index: vendor-crypto/openssl/dist/apps/s_client.c =================================================================== --- vendor-crypto/openssl/dist/apps/s_client.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/s_client.c (revision 296273) @@ -1,2335 +1,2333 @@ /* apps/s_client.c */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* ==================================================================== * Copyright 2005 Nokia. All rights reserved. * * The portions of the attached software ("Contribution") is developed by * Nokia Corporation and is licensed pursuant to the OpenSSL open source * license. * * The Contribution, originally written by Mika Kousa and Pasi Eronen of * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites * support (see RFC 4279) to OpenSSL. * * No patent licenses or other rights except those expressly stated in * the OpenSSL open source license shall be deemed granted or received * expressly, by implication, estoppel, or otherwise. * * No assurances are provided by Nokia that the Contribution does not * infringe the patent or other intellectual property rights of any third * party or that the license provides you with all the necessary rights * to make use of the Contribution. * * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR * OTHERWISE. */ #include #include #include #include #include #include #ifdef OPENSSL_NO_STDIO # define APPS_WIN16 #endif /* * With IPv6, it looks like Digital has mixed up the proper order of * recursive header file inclusion, resulting in the compiler complaining * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is * needed to have fileno() declared correctly... So let's define u_int */ #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) # define __U_INT typedef unsigned int u_int; #endif #define USE_SOCKETS #include "apps.h" #include #include #include #include #include #include #include #ifndef OPENSSL_NO_SRP # include #endif #include "s_apps.h" #include "timeouts.h" #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ # undef FIONBIO #endif #if defined(OPENSSL_SYS_BEOS_R5) # include #endif #undef PROG #define PROG s_client_main /* * #define SSL_HOST_NAME "www.netscape.com" */ /* * #define SSL_HOST_NAME "193.118.187.102" */ #define SSL_HOST_NAME "localhost" /* no default cert. */ /* * #define TEST_CERT "client.pem" */ #undef BUFSIZZ #define BUFSIZZ 1024*8 extern int verify_depth; extern int verify_error; extern int verify_return_error; extern int verify_quiet; #ifdef FIONBIO static int c_nbio = 0; #endif static int c_Pause = 0; static int c_debug = 0; #ifndef OPENSSL_NO_TLSEXT static int c_tlsextdebug = 0; static int c_status_req = 0; #endif static int c_msg = 0; static int c_showcerts = 0; static char *keymatexportlabel = NULL; static int keymatexportlen = 20; static void sc_usage(void); static void print_stuff(BIO *berr, SSL *con, int full); #ifndef OPENSSL_NO_TLSEXT static int ocsp_resp_cb(SSL *s, void *arg); #endif static BIO *bio_c_out = NULL; static BIO *bio_c_msg = NULL; static int c_quiet = 0; static int c_ign_eof = 0; static int c_brief = 0; #ifndef OPENSSL_NO_PSK /* Default PSK identity and key */ static char *psk_identity = "Client_identity"; /* * char *psk_key=NULL; by default PSK is not used */ static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len) { unsigned int psk_len = 0; int ret; BIGNUM *bn = NULL; if (c_debug) BIO_printf(bio_c_out, "psk_client_cb\n"); if (!hint) { /* no ServerKeyExchange message */ if (c_debug) BIO_printf(bio_c_out, "NULL received PSK identity hint, continuing anyway\n"); } else if (c_debug) BIO_printf(bio_c_out, "Received PSK identity hint '%s'\n", hint); /* * lookup PSK identity and PSK key based on the given identity hint here */ ret = BIO_snprintf(identity, max_identity_len, "%s", psk_identity); if (ret < 0 || (unsigned int)ret > max_identity_len) goto out_err; if (c_debug) BIO_printf(bio_c_out, "created identity '%s' len=%d\n", identity, ret); ret = BN_hex2bn(&bn, psk_key); if (!ret) { BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", psk_key); if (bn) BN_free(bn); return 0; } if ((unsigned int)BN_num_bytes(bn) > max_psk_len) { BIO_printf(bio_err, "psk buffer of callback is too small (%d) for key (%d)\n", max_psk_len, BN_num_bytes(bn)); BN_free(bn); return 0; } psk_len = BN_bn2bin(bn, psk); BN_free(bn); if (psk_len == 0) goto out_err; if (c_debug) BIO_printf(bio_c_out, "created PSK len=%d\n", psk_len); return psk_len; out_err: if (c_debug) BIO_printf(bio_err, "Error in PSK client callback\n"); return 0; } #endif static void sc_usage(void) { BIO_printf(bio_err, "usage: s_client args\n"); BIO_printf(bio_err, "\n"); BIO_printf(bio_err, " -host host - use -connect instead\n"); BIO_printf(bio_err, " -port port - use -connect instead\n"); BIO_printf(bio_err, " -connect host:port - who to connect to (default is %s:%s)\n", SSL_HOST_NAME, PORT_STR); BIO_printf(bio_err, " -verify_hostname host - check peer certificate matches \"host\"\n"); BIO_printf(bio_err, " -verify_email email - check peer certificate matches \"email\"\n"); BIO_printf(bio_err, " -verify_ip ipaddr - check peer certificate matches \"ipaddr\"\n"); BIO_printf(bio_err, " -verify arg - turn on peer certificate verification\n"); BIO_printf(bio_err, " -verify_return_error - return verification errors\n"); BIO_printf(bio_err, " -cert arg - certificate file to use, PEM format assumed\n"); BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n"); BIO_printf(bio_err, " -key arg - Private key file to use, in cert file if\n"); BIO_printf(bio_err, " not specified but cert file is.\n"); BIO_printf(bio_err, " -keyform arg - key format (PEM or DER) PEM default\n"); BIO_printf(bio_err, " -pass arg - private key file pass phrase source\n"); BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n"); BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n"); BIO_printf(bio_err, " -no_alt_chains - only ever use the first certificate chain found\n"); BIO_printf(bio_err, " -reconnect - Drop and re-make the connection with the same Session-ID\n"); BIO_printf(bio_err, " -pause - sleep(1) after each read(2) and write(2) system call\n"); BIO_printf(bio_err, " -prexit - print session information even on connection failure\n"); BIO_printf(bio_err, " -showcerts - show all certificates in the chain\n"); BIO_printf(bio_err, " -debug - extra output\n"); #ifdef WATT32 BIO_printf(bio_err, " -wdebug - WATT-32 tcp debugging\n"); #endif BIO_printf(bio_err, " -msg - Show protocol messages\n"); BIO_printf(bio_err, " -nbio_test - more ssl protocol testing\n"); BIO_printf(bio_err, " -state - print the 'ssl' states\n"); #ifdef FIONBIO BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n"); #endif BIO_printf(bio_err, " -crlf - convert LF from terminal into CRLF\n"); BIO_printf(bio_err, " -quiet - no s_client output\n"); BIO_printf(bio_err, " -ign_eof - ignore input eof (default when -quiet)\n"); BIO_printf(bio_err, " -no_ign_eof - don't ignore input eof\n"); #ifndef OPENSSL_NO_PSK BIO_printf(bio_err, " -psk_identity arg - PSK identity\n"); BIO_printf(bio_err, " -psk arg - PSK in hex (without 0x)\n"); # ifndef OPENSSL_NO_JPAKE BIO_printf(bio_err, " -jpake arg - JPAKE secret to use\n"); # endif #endif #ifndef OPENSSL_NO_SRP BIO_printf(bio_err, " -srpuser user - SRP authentification for 'user'\n"); BIO_printf(bio_err, " -srppass arg - password for 'user'\n"); BIO_printf(bio_err, " -srp_lateuser - SRP username into second ClientHello message\n"); BIO_printf(bio_err, " -srp_moregroups - Tolerate other than the known g N values.\n"); BIO_printf(bio_err, " -srp_strength int - minimal length in bits for N (default %d).\n", SRP_MINIMAL_N); #endif BIO_printf(bio_err, " -ssl2 - just use SSLv2\n"); #ifndef OPENSSL_NO_SSL3_METHOD BIO_printf(bio_err, " -ssl3 - just use SSLv3\n"); #endif BIO_printf(bio_err, " -tls1_2 - just use TLSv1.2\n"); BIO_printf(bio_err, " -tls1_1 - just use TLSv1.1\n"); BIO_printf(bio_err, " -tls1 - just use TLSv1\n"); BIO_printf(bio_err, " -dtls1 - just use DTLSv1\n"); BIO_printf(bio_err, " -fallback_scsv - send TLS_FALLBACK_SCSV\n"); BIO_printf(bio_err, " -mtu - set the link layer MTU\n"); BIO_printf(bio_err, " -no_tls1_2/-no_tls1_1/-no_tls1/-no_ssl3/-no_ssl2 - turn off that protocol\n"); BIO_printf(bio_err, " -bugs - Switch on all SSL implementation bug workarounds\n"); BIO_printf(bio_err, - " -serverpref - Use server's cipher preferences (only SSLv2)\n"); - BIO_printf(bio_err, " -cipher - preferred cipher to use, use the 'openssl ciphers'\n"); BIO_printf(bio_err, " command to see what is available\n"); BIO_printf(bio_err, " -starttls prot - use the STARTTLS command before starting TLS\n"); BIO_printf(bio_err, " for those protocols that support it, where\n"); BIO_printf(bio_err, " 'prot' defines which one to assume. Currently,\n"); BIO_printf(bio_err, " only \"smtp\", \"pop3\", \"imap\", \"ftp\" and \"xmpp\"\n"); BIO_printf(bio_err, " are supported.\n"); #ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, " -engine id - Initialise and use the specified engine\n"); #endif BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); BIO_printf(bio_err, " -sess_out arg - file to write SSL session to\n"); BIO_printf(bio_err, " -sess_in arg - file to read SSL session from\n"); #ifndef OPENSSL_NO_TLSEXT BIO_printf(bio_err, " -servername host - Set TLS extension servername in ClientHello\n"); BIO_printf(bio_err, " -tlsextdebug - hex dump of all TLS extensions received\n"); BIO_printf(bio_err, " -status - request certificate status from server\n"); BIO_printf(bio_err, " -no_ticket - disable use of RFC4507bis session tickets\n"); BIO_printf(bio_err, " -serverinfo types - send empty ClientHello extensions (comma-separated numbers)\n"); BIO_printf(bio_err, " -curves arg - Elliptic curves to advertise (colon-separated list)\n"); BIO_printf(bio_err, " -sigalgs arg - Signature algorithms to support (colon-separated list)\n"); BIO_printf(bio_err, " -client_sigalgs arg - Signature algorithms to support for client\n"); BIO_printf(bio_err, " certificate authentication (colon-separated list)\n"); #endif #ifndef OPENSSL_NO_NEXTPROTONEG BIO_printf(bio_err, " -nextprotoneg arg - enable NPN extension, considering named protocols supported (comma-separated list)\n"); #endif BIO_printf(bio_err, " -alpn arg - enable ALPN extension, considering named protocols supported (comma-separated list)\n"); BIO_printf(bio_err, " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n"); #ifndef OPENSSL_NO_SRTP BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n"); #endif BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n"); BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n"); } #ifndef OPENSSL_NO_TLSEXT /* This is a context that we pass to callbacks */ typedef struct tlsextctx_st { BIO *biodebug; int ack; } tlsextctx; static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg) { tlsextctx *p = (tlsextctx *) arg; const char *hn = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (SSL_get_servername_type(s) != -1) p->ack = !SSL_session_reused(s) && hn != NULL; else BIO_printf(bio_err, "Can't use SSL_get_servername\n"); return SSL_TLSEXT_ERR_OK; } # ifndef OPENSSL_NO_SRP /* This is a context that we pass to all callbacks */ typedef struct srp_arg_st { char *srppassin; char *srplogin; int msg; /* copy from c_msg */ int debug; /* copy from c_debug */ int amp; /* allow more groups */ int strength /* minimal size for N */ ; } SRP_ARG; # define SRP_NUMBER_ITERATIONS_FOR_PRIME 64 static int srp_Verify_N_and_g(BIGNUM *N, BIGNUM *g) { BN_CTX *bn_ctx = BN_CTX_new(); BIGNUM *p = BN_new(); BIGNUM *r = BN_new(); int ret = g != NULL && N != NULL && bn_ctx != NULL && BN_is_odd(N) && BN_is_prime_ex(N, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) && p != NULL && BN_rshift1(p, N) && /* p = (N-1)/2 */ BN_is_prime_ex(p, SRP_NUMBER_ITERATIONS_FOR_PRIME, bn_ctx, NULL) && r != NULL && /* verify g^((N-1)/2) == -1 (mod N) */ BN_mod_exp(r, g, p, N, bn_ctx) && BN_add_word(r, 1) && BN_cmp(r, N) == 0; if (r) BN_free(r); if (p) BN_free(p); if (bn_ctx) BN_CTX_free(bn_ctx); return ret; } /*- * This callback is used here for two purposes: * - extended debugging * - making some primality tests for unknown groups * The callback is only called for a non default group. * * An application does not need the call back at all if * only the stanard groups are used. In real life situations, * client and server already share well known groups, * thus there is no need to verify them. * Furthermore, in case that a server actually proposes a group that * is not one of those defined in RFC 5054, it is more appropriate * to add the group to a static list and then compare since * primality tests are rather cpu consuming. */ static int MS_CALLBACK ssl_srp_verify_param_cb(SSL *s, void *arg) { SRP_ARG *srp_arg = (SRP_ARG *)arg; BIGNUM *N = NULL, *g = NULL; if (!(N = SSL_get_srp_N(s)) || !(g = SSL_get_srp_g(s))) return 0; if (srp_arg->debug || srp_arg->msg || srp_arg->amp == 1) { BIO_printf(bio_err, "SRP parameters:\n"); BIO_printf(bio_err, "\tN="); BN_print(bio_err, N); BIO_printf(bio_err, "\n\tg="); BN_print(bio_err, g); BIO_printf(bio_err, "\n"); } if (SRP_check_known_gN_param(g, N)) return 1; if (srp_arg->amp == 1) { if (srp_arg->debug) BIO_printf(bio_err, "SRP param N and g are not known params, going to check deeper.\n"); /* * The srp_moregroups is a real debugging feature. Implementors * should rather add the value to the known ones. The minimal size * has already been tested. */ if (BN_num_bits(g) <= BN_BITS && srp_Verify_N_and_g(N, g)) return 1; } BIO_printf(bio_err, "SRP param N and g rejected.\n"); return 0; } # define PWD_STRLEN 1024 static char *MS_CALLBACK ssl_give_srp_client_pwd_cb(SSL *s, void *arg) { SRP_ARG *srp_arg = (SRP_ARG *)arg; char *pass = (char *)OPENSSL_malloc(PWD_STRLEN + 1); PW_CB_DATA cb_tmp; int l; if (!pass) { BIO_printf(bio_err, "Malloc failure\n"); return NULL; } cb_tmp.password = (char *)srp_arg->srppassin; cb_tmp.prompt_info = "SRP user"; if ((l = password_callback(pass, PWD_STRLEN, 0, &cb_tmp)) < 0) { BIO_printf(bio_err, "Can't read Password\n"); OPENSSL_free(pass); return NULL; } *(pass + l) = '\0'; return pass; } # endif # ifndef OPENSSL_NO_SRTP char *srtp_profiles = NULL; # endif # ifndef OPENSSL_NO_NEXTPROTONEG /* This the context that we pass to next_proto_cb */ typedef struct tlsextnextprotoctx_st { unsigned char *data; unsigned short len; int status; } tlsextnextprotoctx; static tlsextnextprotoctx next_proto; static int next_proto_cb(SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { tlsextnextprotoctx *ctx = arg; if (!c_quiet) { /* We can assume that |in| is syntactically valid. */ unsigned i; BIO_printf(bio_c_out, "Protocols advertised by server: "); for (i = 0; i < inlen;) { if (i) BIO_write(bio_c_out, ", ", 2); BIO_write(bio_c_out, &in[i + 1], in[i]); i += in[i] + 1; } BIO_write(bio_c_out, "\n", 1); } ctx->status = SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len); return SSL_TLSEXT_ERR_OK; } # endif /* ndef OPENSSL_NO_NEXTPROTONEG */ static int serverinfo_cli_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *arg) { char pem_name[100]; unsigned char ext_buf[4 + 65536]; /* Reconstruct the type/len fields prior to extension data */ ext_buf[0] = ext_type >> 8; ext_buf[1] = ext_type & 0xFF; ext_buf[2] = inlen >> 8; ext_buf[3] = inlen & 0xFF; memcpy(ext_buf + 4, in, inlen); BIO_snprintf(pem_name, sizeof(pem_name), "SERVERINFO FOR EXTENSION %d", ext_type); PEM_write_bio(bio_c_out, pem_name, "", ext_buf, 4 + inlen); return 1; } #endif enum { PROTO_OFF = 0, PROTO_SMTP, PROTO_POP3, PROTO_IMAP, PROTO_FTP, PROTO_XMPP }; int MAIN(int, char **); int MAIN(int argc, char **argv) { int build_chain = 0; SSL *con = NULL; #ifndef OPENSSL_NO_KRB5 KSSL_CTX *kctx; #endif int s, k, width, state = 0; char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL; int cbuf_len, cbuf_off; int sbuf_len, sbuf_off; fd_set readfds, writefds; short port = PORT; int full_log = 1; char *host = SSL_HOST_NAME; char *cert_file = NULL, *key_file = NULL, *chain_file = NULL; int cert_format = FORMAT_PEM, key_format = FORMAT_PEM; char *passarg = NULL, *pass = NULL; X509 *cert = NULL; EVP_PKEY *key = NULL; STACK_OF(X509) *chain = NULL; char *CApath = NULL, *CAfile = NULL; char *chCApath = NULL, *chCAfile = NULL; char *vfyCApath = NULL, *vfyCAfile = NULL; int reconnect = 0, badop = 0, verify = SSL_VERIFY_NONE; int crlf = 0; int write_tty, read_tty, write_ssl, read_ssl, tty_on, ssl_pending; SSL_CTX *ctx = NULL; int ret = 1, in_init = 1, i, nbio_test = 0; int starttls_proto = PROTO_OFF; int prexit = 0; X509_VERIFY_PARAM *vpm = NULL; int badarg = 0; const SSL_METHOD *meth = NULL; int socket_type = SOCK_STREAM; BIO *sbio; char *inrand = NULL; int mbuf_len = 0; struct timeval timeout, *timeoutp; #ifndef OPENSSL_NO_ENGINE char *engine_id = NULL; char *ssl_client_engine_id = NULL; ENGINE *ssl_client_engine = NULL; #endif ENGINE *e = NULL; #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5) struct timeval tv; # if defined(OPENSSL_SYS_BEOS_R5) int stdin_set = 0; # endif #endif #ifndef OPENSSL_NO_TLSEXT char *servername = NULL; tlsextctx tlsextcbp = { NULL, 0 }; # ifndef OPENSSL_NO_NEXTPROTONEG const char *next_proto_neg_in = NULL; # endif const char *alpn_in = NULL; # define MAX_SI_TYPES 100 unsigned short serverinfo_types[MAX_SI_TYPES]; int serverinfo_types_count = 0; #endif char *sess_in = NULL; char *sess_out = NULL; struct sockaddr peer; int peerlen = sizeof(peer); int fallback_scsv = 0; int enable_timeouts = 0; long socket_mtu = 0; #ifndef OPENSSL_NO_JPAKE static char *jpake_secret = NULL; # define no_jpake !jpake_secret #else # define no_jpake 1 #endif #ifndef OPENSSL_NO_SRP char *srppass = NULL; int srp_lateuser = 0; SRP_ARG srp_arg = { NULL, NULL, 0, 0, 0, 1024 }; #endif SSL_EXCERT *exc = NULL; SSL_CONF_CTX *cctx = NULL; STACK_OF(OPENSSL_STRING) *ssl_args = NULL; char *crl_file = NULL; int crl_format = FORMAT_PEM; int crl_download = 0; STACK_OF(X509_CRL) *crls = NULL; meth = SSLv23_client_method(); apps_startup(); c_Pause = 0; c_quiet = 0; c_ign_eof = 0; c_debug = 0; c_msg = 0; c_showcerts = 0; if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; cctx = SSL_CONF_CTX_new(); if (!cctx) goto end; SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CLIENT); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE); if (((cbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) || ((sbuf = OPENSSL_malloc(BUFSIZZ)) == NULL) || ((mbuf = OPENSSL_malloc(BUFSIZZ)) == NULL)) { BIO_printf(bio_err, "out of memory\n"); goto end; } verify_depth = 0; verify_error = X509_V_OK; #ifdef FIONBIO c_nbio = 0; #endif argc--; argv++; while (argc >= 1) { if (strcmp(*argv, "-host") == 0) { if (--argc < 1) goto bad; host = *(++argv); } else if (strcmp(*argv, "-port") == 0) { if (--argc < 1) goto bad; port = atoi(*(++argv)); if (port == 0) goto bad; } else if (strcmp(*argv, "-connect") == 0) { if (--argc < 1) goto bad; if (!extract_host_port(*(++argv), &host, NULL, &port)) goto bad; } else if (strcmp(*argv, "-verify") == 0) { verify = SSL_VERIFY_PEER; if (--argc < 1) goto bad; verify_depth = atoi(*(++argv)); if (!c_quiet) BIO_printf(bio_err, "verify depth is %d\n", verify_depth); } else if (strcmp(*argv, "-cert") == 0) { if (--argc < 1) goto bad; cert_file = *(++argv); } else if (strcmp(*argv, "-CRL") == 0) { if (--argc < 1) goto bad; crl_file = *(++argv); } else if (strcmp(*argv, "-crl_download") == 0) crl_download = 1; else if (strcmp(*argv, "-sess_out") == 0) { if (--argc < 1) goto bad; sess_out = *(++argv); } else if (strcmp(*argv, "-sess_in") == 0) { if (--argc < 1) goto bad; sess_in = *(++argv); } else if (strcmp(*argv, "-certform") == 0) { if (--argc < 1) goto bad; cert_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-CRLform") == 0) { if (--argc < 1) goto bad; crl_format = str2fmt(*(++argv)); } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) { if (badarg) goto bad; continue; } else if (strcmp(*argv, "-verify_return_error") == 0) verify_return_error = 1; else if (strcmp(*argv, "-verify_quiet") == 0) verify_quiet = 1; else if (strcmp(*argv, "-brief") == 0) { c_brief = 1; verify_quiet = 1; c_quiet = 1; } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) { if (badarg) goto bad; continue; } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args)) { if (badarg) goto bad; continue; } else if (strcmp(*argv, "-prexit") == 0) prexit = 1; else if (strcmp(*argv, "-crlf") == 0) crlf = 1; else if (strcmp(*argv, "-quiet") == 0) { c_quiet = 1; c_ign_eof = 1; } else if (strcmp(*argv, "-ign_eof") == 0) c_ign_eof = 1; else if (strcmp(*argv, "-no_ign_eof") == 0) c_ign_eof = 0; else if (strcmp(*argv, "-pause") == 0) c_Pause = 1; else if (strcmp(*argv, "-debug") == 0) c_debug = 1; #ifndef OPENSSL_NO_TLSEXT else if (strcmp(*argv, "-tlsextdebug") == 0) c_tlsextdebug = 1; else if (strcmp(*argv, "-status") == 0) c_status_req = 1; #endif #ifdef WATT32 else if (strcmp(*argv, "-wdebug") == 0) dbug_init(); #endif else if (strcmp(*argv, "-msg") == 0) c_msg = 1; else if (strcmp(*argv, "-msgfile") == 0) { if (--argc < 1) goto bad; bio_c_msg = BIO_new_file(*(++argv), "w"); } #ifndef OPENSSL_NO_SSL_TRACE else if (strcmp(*argv, "-trace") == 0) c_msg = 2; #endif else if (strcmp(*argv, "-showcerts") == 0) c_showcerts = 1; else if (strcmp(*argv, "-nbio_test") == 0) nbio_test = 1; else if (strcmp(*argv, "-state") == 0) state = 1; #ifndef OPENSSL_NO_PSK else if (strcmp(*argv, "-psk_identity") == 0) { if (--argc < 1) goto bad; psk_identity = *(++argv); } else if (strcmp(*argv, "-psk") == 0) { size_t j; if (--argc < 1) goto bad; psk_key = *(++argv); for (j = 0; j < strlen(psk_key); j++) { if (isxdigit((unsigned char)psk_key[j])) continue; BIO_printf(bio_err, "Not a hex number '%s'\n", *argv); goto bad; } } #endif #ifndef OPENSSL_NO_SRP else if (strcmp(*argv, "-srpuser") == 0) { if (--argc < 1) goto bad; srp_arg.srplogin = *(++argv); meth = TLSv1_client_method(); } else if (strcmp(*argv, "-srppass") == 0) { if (--argc < 1) goto bad; srppass = *(++argv); meth = TLSv1_client_method(); } else if (strcmp(*argv, "-srp_strength") == 0) { if (--argc < 1) goto bad; srp_arg.strength = atoi(*(++argv)); BIO_printf(bio_err, "SRP minimal length for N is %d\n", srp_arg.strength); meth = TLSv1_client_method(); } else if (strcmp(*argv, "-srp_lateuser") == 0) { srp_lateuser = 1; meth = TLSv1_client_method(); } else if (strcmp(*argv, "-srp_moregroups") == 0) { srp_arg.amp = 1; meth = TLSv1_client_method(); } #endif #ifndef OPENSSL_NO_SSL2 else if (strcmp(*argv, "-ssl2") == 0) meth = SSLv2_client_method(); #endif #ifndef OPENSSL_NO_SSL3_METHOD else if (strcmp(*argv, "-ssl3") == 0) meth = SSLv3_client_method(); #endif #ifndef OPENSSL_NO_TLS1 else if (strcmp(*argv, "-tls1_2") == 0) meth = TLSv1_2_client_method(); else if (strcmp(*argv, "-tls1_1") == 0) meth = TLSv1_1_client_method(); else if (strcmp(*argv, "-tls1") == 0) meth = TLSv1_client_method(); #endif #ifndef OPENSSL_NO_DTLS1 else if (strcmp(*argv, "-dtls") == 0) { meth = DTLS_client_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-dtls1") == 0) { meth = DTLSv1_client_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-dtls1_2") == 0) { meth = DTLSv1_2_client_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-timeout") == 0) enable_timeouts = 1; else if (strcmp(*argv, "-mtu") == 0) { if (--argc < 1) goto bad; socket_mtu = atol(*(++argv)); } #endif else if (strcmp(*argv, "-fallback_scsv") == 0) { fallback_scsv = 1; } else if (strcmp(*argv, "-keyform") == 0) { if (--argc < 1) goto bad; key_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-pass") == 0) { if (--argc < 1) goto bad; passarg = *(++argv); } else if (strcmp(*argv, "-cert_chain") == 0) { if (--argc < 1) goto bad; chain_file = *(++argv); } else if (strcmp(*argv, "-key") == 0) { if (--argc < 1) goto bad; key_file = *(++argv); } else if (strcmp(*argv, "-reconnect") == 0) { reconnect = 5; } else if (strcmp(*argv, "-CApath") == 0) { if (--argc < 1) goto bad; CApath = *(++argv); } else if (strcmp(*argv, "-chainCApath") == 0) { if (--argc < 1) goto bad; chCApath = *(++argv); } else if (strcmp(*argv, "-verifyCApath") == 0) { if (--argc < 1) goto bad; vfyCApath = *(++argv); } else if (strcmp(*argv, "-build_chain") == 0) build_chain = 1; else if (strcmp(*argv, "-CAfile") == 0) { if (--argc < 1) goto bad; CAfile = *(++argv); } else if (strcmp(*argv, "-chainCAfile") == 0) { if (--argc < 1) goto bad; chCAfile = *(++argv); } else if (strcmp(*argv, "-verifyCAfile") == 0) { if (--argc < 1) goto bad; vfyCAfile = *(++argv); } #ifndef OPENSSL_NO_TLSEXT # ifndef OPENSSL_NO_NEXTPROTONEG else if (strcmp(*argv, "-nextprotoneg") == 0) { if (--argc < 1) goto bad; next_proto_neg_in = *(++argv); } # endif else if (strcmp(*argv, "-alpn") == 0) { if (--argc < 1) goto bad; alpn_in = *(++argv); } else if (strcmp(*argv, "-serverinfo") == 0) { char *c; int start = 0; int len; if (--argc < 1) goto bad; c = *(++argv); serverinfo_types_count = 0; len = strlen(c); for (i = 0; i <= len; ++i) { if (i == len || c[i] == ',') { serverinfo_types[serverinfo_types_count] = atoi(c + start); serverinfo_types_count++; start = i + 1; } if (serverinfo_types_count == MAX_SI_TYPES) break; } } #endif #ifdef FIONBIO else if (strcmp(*argv, "-nbio") == 0) { c_nbio = 1; } #endif else if (strcmp(*argv, "-starttls") == 0) { if (--argc < 1) goto bad; ++argv; if (strcmp(*argv, "smtp") == 0) starttls_proto = PROTO_SMTP; else if (strcmp(*argv, "pop3") == 0) starttls_proto = PROTO_POP3; else if (strcmp(*argv, "imap") == 0) starttls_proto = PROTO_IMAP; else if (strcmp(*argv, "ftp") == 0) starttls_proto = PROTO_FTP; else if (strcmp(*argv, "xmpp") == 0) starttls_proto = PROTO_XMPP; else goto bad; } #ifndef OPENSSL_NO_ENGINE else if (strcmp(*argv, "-engine") == 0) { if (--argc < 1) goto bad; engine_id = *(++argv); } else if (strcmp(*argv, "-ssl_client_engine") == 0) { if (--argc < 1) goto bad; ssl_client_engine_id = *(++argv); } #endif else if (strcmp(*argv, "-rand") == 0) { if (--argc < 1) goto bad; inrand = *(++argv); } #ifndef OPENSSL_NO_TLSEXT else if (strcmp(*argv, "-servername") == 0) { if (--argc < 1) goto bad; servername = *(++argv); /* meth=TLSv1_client_method(); */ } #endif #ifndef OPENSSL_NO_JPAKE else if (strcmp(*argv, "-jpake") == 0) { if (--argc < 1) goto bad; jpake_secret = *++argv; } #endif #ifndef OPENSSL_NO_SRTP else if (strcmp(*argv, "-use_srtp") == 0) { if (--argc < 1) goto bad; srtp_profiles = *(++argv); } #endif else if (strcmp(*argv, "-keymatexport") == 0) { if (--argc < 1) goto bad; keymatexportlabel = *(++argv); } else if (strcmp(*argv, "-keymatexportlen") == 0) { if (--argc < 1) goto bad; keymatexportlen = atoi(*(++argv)); if (keymatexportlen == 0) goto bad; } else { BIO_printf(bio_err, "unknown option %s\n", *argv); badop = 1; break; } argc--; argv++; } if (badop) { bad: sc_usage(); goto end; } #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) if (jpake_secret) { if (psk_key) { BIO_printf(bio_err, "Can't use JPAKE and PSK together\n"); goto end; } psk_identity = "JPAKE"; } #endif OpenSSL_add_ssl_algorithms(); SSL_load_error_strings(); #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) next_proto.status = -1; if (next_proto_neg_in) { next_proto.data = next_protos_parse(&next_proto.len, next_proto_neg_in); if (next_proto.data == NULL) { BIO_printf(bio_err, "Error parsing -nextprotoneg argument\n"); goto end; } } else next_proto.data = NULL; #endif #ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine_id, 1); if (ssl_client_engine_id) { ssl_client_engine = ENGINE_by_id(ssl_client_engine_id); if (!ssl_client_engine) { BIO_printf(bio_err, "Error getting client auth engine\n"); goto end; } } #endif if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } if (key_file == NULL) key_file = cert_file; if (key_file) { key = load_key(bio_err, key_file, key_format, 0, pass, e, "client certificate private key file"); if (!key) { ERR_print_errors(bio_err); goto end; } } if (cert_file) { cert = load_cert(bio_err, cert_file, cert_format, NULL, e, "client certificate file"); if (!cert) { ERR_print_errors(bio_err); goto end; } } if (chain_file) { chain = load_certs(bio_err, chain_file, FORMAT_PEM, NULL, e, "client certificate chain"); if (!chain) goto end; } if (crl_file) { X509_CRL *crl; crl = load_crl(crl_file, crl_format); if (!crl) { BIO_puts(bio_err, "Error loading CRL\n"); ERR_print_errors(bio_err); goto end; } crls = sk_X509_CRL_new_null(); if (!crls || !sk_X509_CRL_push(crls, crl)) { BIO_puts(bio_err, "Error adding CRL\n"); ERR_print_errors(bio_err); X509_CRL_free(crl); goto end; } } if (!load_excert(&exc, bio_err)) goto end; if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL && !RAND_status()) { BIO_printf(bio_err, "warning, not much extra random data, consider using the -rand option\n"); } if (inrand != NULL) BIO_printf(bio_err, "%ld semi-random bytes loaded\n", app_RAND_load_files(inrand)); if (bio_c_out == NULL) { if (c_quiet && !c_debug) { bio_c_out = BIO_new(BIO_s_null()); if (c_msg && !bio_c_msg) bio_c_msg = BIO_new_fp(stdout, BIO_NOCLOSE); } else { if (bio_c_out == NULL) bio_c_out = BIO_new_fp(stdout, BIO_NOCLOSE); } } #ifndef OPENSSL_NO_SRP if (!app_passwd(bio_err, srppass, NULL, &srp_arg.srppassin, NULL)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } #endif ctx = SSL_CTX_new(meth); if (ctx == NULL) { ERR_print_errors(bio_err); goto end; } if (vpm) SSL_CTX_set1_param(ctx, vpm); if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, 1, no_jpake)) { ERR_print_errors(bio_err); goto end; } if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile, crls, crl_download)) { BIO_printf(bio_err, "Error loading store locations\n"); ERR_print_errors(bio_err); goto end; } #ifndef OPENSSL_NO_ENGINE if (ssl_client_engine) { if (!SSL_CTX_set_client_cert_engine(ctx, ssl_client_engine)) { BIO_puts(bio_err, "Error setting client auth engine\n"); ERR_print_errors(bio_err); ENGINE_free(ssl_client_engine); goto end; } ENGINE_free(ssl_client_engine); } #endif #ifndef OPENSSL_NO_PSK # ifdef OPENSSL_NO_JPAKE if (psk_key != NULL) # else if (psk_key != NULL || jpake_secret) # endif { if (c_debug) BIO_printf(bio_c_out, "PSK key given or JPAKE in use, setting client callback\n"); SSL_CTX_set_psk_client_callback(ctx, psk_client_cb); } #endif #ifndef OPENSSL_NO_SRTP if (srtp_profiles != NULL) SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles); #endif if (exc) ssl_ctx_set_excert(ctx, exc); #if !defined(OPENSSL_NO_TLSEXT) # if !defined(OPENSSL_NO_NEXTPROTONEG) if (next_proto.data) SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &next_proto); # endif if (alpn_in) { unsigned short alpn_len; unsigned char *alpn = next_protos_parse(&alpn_len, alpn_in); if (alpn == NULL) { BIO_printf(bio_err, "Error parsing -alpn argument\n"); goto end; } SSL_CTX_set_alpn_protos(ctx, alpn, alpn_len); OPENSSL_free(alpn); } #endif #ifndef OPENSSL_NO_TLSEXT for (i = 0; i < serverinfo_types_count; i++) { SSL_CTX_add_client_custom_ext(ctx, serverinfo_types[i], NULL, NULL, NULL, serverinfo_cli_parse_cb, NULL); } #endif if (state) SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); #if 0 else SSL_CTX_set_cipher_list(ctx, getenv("SSL_CIPHER")); #endif SSL_CTX_set_verify(ctx, verify, verify_callback); if ((CAfile || CApath) && !SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) { ERR_print_errors(bio_err); } if (!SSL_CTX_set_default_verify_paths(ctx)) { ERR_print_errors(bio_err); } ssl_ctx_add_crls(ctx, crls, crl_download); if (!set_cert_key_stuff(ctx, cert, key, chain, build_chain)) goto end; #ifndef OPENSSL_NO_TLSEXT if (servername != NULL) { tlsextcbp.biodebug = bio_err; SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); } # ifndef OPENSSL_NO_SRP if (srp_arg.srplogin) { if (!srp_lateuser && !SSL_CTX_set_srp_username(ctx, srp_arg.srplogin)) { BIO_printf(bio_err, "Unable to set SRP username\n"); goto end; } srp_arg.msg = c_msg; srp_arg.debug = c_debug; SSL_CTX_set_srp_cb_arg(ctx, &srp_arg); SSL_CTX_set_srp_client_pwd_callback(ctx, ssl_give_srp_client_pwd_cb); SSL_CTX_set_srp_strength(ctx, srp_arg.strength); if (c_msg || c_debug || srp_arg.amp == 0) SSL_CTX_set_srp_verify_param_callback(ctx, ssl_srp_verify_param_cb); } # endif #endif con = SSL_new(ctx); if (sess_in) { SSL_SESSION *sess; BIO *stmp = BIO_new_file(sess_in, "r"); if (!stmp) { BIO_printf(bio_err, "Can't open session file %s\n", sess_in); ERR_print_errors(bio_err); goto end; } sess = PEM_read_bio_SSL_SESSION(stmp, NULL, 0, NULL); BIO_free(stmp); if (!sess) { BIO_printf(bio_err, "Can't open session file %s\n", sess_in); ERR_print_errors(bio_err); goto end; } SSL_set_session(con, sess); SSL_SESSION_free(sess); } if (fallback_scsv) SSL_set_mode(con, SSL_MODE_SEND_FALLBACK_SCSV); #ifndef OPENSSL_NO_TLSEXT if (servername != NULL) { if (!SSL_set_tlsext_host_name(con, servername)) { BIO_printf(bio_err, "Unable to set TLS servername extension.\n"); ERR_print_errors(bio_err); goto end; } } #endif #ifndef OPENSSL_NO_KRB5 if (con && (kctx = kssl_ctx_new()) != NULL) { SSL_set0_kssl_ctx(con, kctx); kssl_ctx_setstring(kctx, KSSL_SERVER, host); } #endif /* OPENSSL_NO_KRB5 */ /* SSL_set_cipher_list(con,"RC4-MD5"); */ #if 0 # ifdef TLSEXT_TYPE_opaque_prf_input SSL_set_tlsext_opaque_prf_input(con, "Test client", 11); # endif #endif re_start: if (init_client(&s, host, port, socket_type) == 0) { BIO_printf(bio_err, "connect:errno=%d\n", get_last_socket_error()); SHUTDOWN(s); goto end; } BIO_printf(bio_c_out, "CONNECTED(%08X)\n", s); #ifdef FIONBIO if (c_nbio) { unsigned long l = 1; BIO_printf(bio_c_out, "turning on non blocking io\n"); if (BIO_socket_ioctl(s, FIONBIO, &l) < 0) { ERR_print_errors(bio_err); goto end; } } #endif if (c_Pause & 0x01) SSL_set_debug(con, 1); if (socket_type == SOCK_DGRAM) { sbio = BIO_new_dgram(s, BIO_NOCLOSE); if (getsockname(s, &peer, (void *)&peerlen) < 0) { BIO_printf(bio_err, "getsockname:errno=%d\n", get_last_socket_error()); SHUTDOWN(s); goto end; } (void)BIO_ctrl_set_connected(sbio, 1, &peer); if (enable_timeouts) { timeout.tv_sec = 0; timeout.tv_usec = DGRAM_RCV_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); timeout.tv_sec = 0; timeout.tv_usec = DGRAM_SND_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); } if (socket_mtu) { if (socket_mtu < DTLS_get_link_min_mtu(con)) { BIO_printf(bio_err, "MTU too small. Must be at least %ld\n", DTLS_get_link_min_mtu(con)); BIO_free(sbio); goto shut; } SSL_set_options(con, SSL_OP_NO_QUERY_MTU); if (!DTLS_set_link_mtu(con, socket_mtu)) { BIO_printf(bio_err, "Failed to set MTU\n"); BIO_free(sbio); goto shut; } } else /* want to do MTU discovery */ BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); } else sbio = BIO_new_socket(s, BIO_NOCLOSE); if (nbio_test) { BIO *test; test = BIO_new(BIO_f_nbio_test()); sbio = BIO_push(test, sbio); } if (c_debug) { SSL_set_debug(con, 1); BIO_set_callback(sbio, bio_dump_callback); BIO_set_callback_arg(sbio, (char *)bio_c_out); } if (c_msg) { #ifndef OPENSSL_NO_SSL_TRACE if (c_msg == 2) SSL_set_msg_callback(con, SSL_trace); else #endif SSL_set_msg_callback(con, msg_cb); SSL_set_msg_callback_arg(con, bio_c_msg ? bio_c_msg : bio_c_out); } #ifndef OPENSSL_NO_TLSEXT if (c_tlsextdebug) { SSL_set_tlsext_debug_callback(con, tlsext_cb); SSL_set_tlsext_debug_arg(con, bio_c_out); } if (c_status_req) { SSL_set_tlsext_status_type(con, TLSEXT_STATUSTYPE_ocsp); SSL_CTX_set_tlsext_status_cb(ctx, ocsp_resp_cb); SSL_CTX_set_tlsext_status_arg(ctx, bio_c_out); # if 0 { STACK_OF(OCSP_RESPID) *ids = sk_OCSP_RESPID_new_null(); OCSP_RESPID *id = OCSP_RESPID_new(); id->value.byKey = ASN1_OCTET_STRING_new(); id->type = V_OCSP_RESPID_KEY; ASN1_STRING_set(id->value.byKey, "Hello World", -1); sk_OCSP_RESPID_push(ids, id); SSL_set_tlsext_status_ids(con, ids); } # endif } #endif #ifndef OPENSSL_NO_JPAKE if (jpake_secret) jpake_client_auth(bio_c_out, sbio, jpake_secret); #endif SSL_set_bio(con, sbio, sbio); SSL_set_connect_state(con); /* ok, lets connect */ width = SSL_get_fd(con) + 1; read_tty = 1; write_tty = 0; tty_on = 0; read_ssl = 1; write_ssl = 1; cbuf_len = 0; cbuf_off = 0; sbuf_len = 0; sbuf_off = 0; /* This is an ugly hack that does a lot of assumptions */ /* * We do have to handle multi-line responses which may come in a single * packet or not. We therefore have to use BIO_gets() which does need a * buffering BIO. So during the initial chitchat we do push a buffering * BIO into the chain that is removed again later on to not disturb the * rest of the s_client operation. */ if (starttls_proto == PROTO_SMTP) { int foundit = 0; BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); /* wait for multi-line response to end from SMTP */ do { mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); } while (mbuf_len > 3 && mbuf[3] == '-'); /* STARTTLS command requires EHLO... */ BIO_printf(fbio, "EHLO openssl.client.net\r\n"); (void)BIO_flush(fbio); /* wait for multi-line response to end EHLO SMTP response */ do { mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); if (strstr(mbuf, "STARTTLS")) foundit = 1; } while (mbuf_len > 3 && mbuf[3] == '-'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); if (!foundit) BIO_printf(bio_err, "didn't found starttls in server response," " try anyway...\n"); BIO_printf(sbio, "STARTTLS\r\n"); BIO_read(sbio, sbuf, BUFSIZZ); } else if (starttls_proto == PROTO_POP3) { BIO_read(sbio, mbuf, BUFSIZZ); BIO_printf(sbio, "STLS\r\n"); BIO_read(sbio, sbuf, BUFSIZZ); } else if (starttls_proto == PROTO_IMAP) { int foundit = 0; BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); BIO_gets(fbio, mbuf, BUFSIZZ); /* STARTTLS command requires CAPABILITY... */ BIO_printf(fbio, ". CAPABILITY\r\n"); (void)BIO_flush(fbio); /* wait for multi-line CAPABILITY response */ do { mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); if (strstr(mbuf, "STARTTLS")) foundit = 1; } while (mbuf_len > 3 && mbuf[0] != '.'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); if (!foundit) BIO_printf(bio_err, "didn't found STARTTLS in server response," " try anyway...\n"); BIO_printf(sbio, ". STARTTLS\r\n"); BIO_read(sbio, sbuf, BUFSIZZ); } else if (starttls_proto == PROTO_FTP) { BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); /* wait for multi-line response to end from FTP */ do { mbuf_len = BIO_gets(fbio, mbuf, BUFSIZZ); } while (mbuf_len > 3 && mbuf[3] == '-'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); BIO_printf(sbio, "AUTH TLS\r\n"); BIO_read(sbio, sbuf, BUFSIZZ); } if (starttls_proto == PROTO_XMPP) { int seen = 0; BIO_printf(sbio, "", host); seen = BIO_read(sbio, mbuf, BUFSIZZ); mbuf[seen] = 0; while (!strstr (mbuf, "")) goto shut; seen = BIO_read(sbio, mbuf, BUFSIZZ); mbuf[seen] = 0; } BIO_printf(sbio, ""); seen = BIO_read(sbio, sbuf, BUFSIZZ); sbuf[seen] = 0; if (!strstr(sbuf, " 0) full_log--; if (starttls_proto) { BIO_printf(bio_err, "%s", mbuf); /* We don't need to know any more */ starttls_proto = PROTO_OFF; } if (reconnect) { reconnect--; BIO_printf(bio_c_out, "drop connection and then reconnect\n"); SSL_shutdown(con); SSL_set_connect_state(con); SHUTDOWN(SSL_get_fd(con)); goto re_start; } } } ssl_pending = read_ssl && SSL_pending(con); if (!ssl_pending) { #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined (OPENSSL_SYS_BEOS_R5) if (tty_on) { if (read_tty) openssl_fdset(fileno(stdin), &readfds); if (write_tty) openssl_fdset(fileno(stdout), &writefds); } if (read_ssl) openssl_fdset(SSL_get_fd(con), &readfds); if (write_ssl) openssl_fdset(SSL_get_fd(con), &writefds); #else if (!tty_on || !write_tty) { if (read_ssl) openssl_fdset(SSL_get_fd(con), &readfds); if (write_ssl) openssl_fdset(SSL_get_fd(con), &writefds); } #endif /*- printf("mode tty(%d %d%d) ssl(%d%d)\n", tty_on,read_tty,write_tty,read_ssl,write_ssl);*/ /* * Note: under VMS with SOCKETSHR the second parameter is * currently of type (int *) whereas under other systems it is * (void *) if you don't have a cast it will choke the compiler: * if you do have a cast then you can either go for (int *) or * (void *). */ #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) /* * Under Windows/DOS we make the assumption that we can always * write to the tty: therefore if we need to write to the tty we * just fall through. Otherwise we timeout the select every * second and see if there are any keypresses. Note: this is a * hack, in a proper Windows application we wouldn't do this. */ i = 0; if (!write_tty) { if (read_tty) { tv.tv_sec = 1; tv.tv_usec = 0; i = select(width, (void *)&readfds, (void *)&writefds, NULL, &tv); # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) if (!i && (!_kbhit() || !read_tty)) continue; # else if (!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle (STD_INPUT_HANDLE), 0))) || !read_tty)) continue; # endif } else i = select(width, (void *)&readfds, (void *)&writefds, NULL, timeoutp); } #elif defined(OPENSSL_SYS_NETWARE) if (!write_tty) { if (read_tty) { tv.tv_sec = 1; tv.tv_usec = 0; i = select(width, (void *)&readfds, (void *)&writefds, NULL, &tv); } else i = select(width, (void *)&readfds, (void *)&writefds, NULL, timeoutp); } #elif defined(OPENSSL_SYS_BEOS_R5) /* Under BeOS-R5 the situation is similar to DOS */ i = 0; stdin_set = 0; (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); if (!write_tty) { if (read_tty) { tv.tv_sec = 1; tv.tv_usec = 0; i = select(width, (void *)&readfds, (void *)&writefds, NULL, &tv); if (read(fileno(stdin), sbuf, 0) >= 0) stdin_set = 1; if (!i && (stdin_set != 1 || !read_tty)) continue; } else i = select(width, (void *)&readfds, (void *)&writefds, NULL, timeoutp); } (void)fcntl(fileno(stdin), F_SETFL, 0); #else i = select(width, (void *)&readfds, (void *)&writefds, NULL, timeoutp); #endif if (i < 0) { BIO_printf(bio_err, "bad select %d\n", get_last_socket_error()); goto shut; /* goto end; */ } } if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) { BIO_printf(bio_err, "TIMEOUT occured\n"); } if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) { k = SSL_write(con, &(cbuf[cbuf_off]), (unsigned int)cbuf_len); switch (SSL_get_error(con, k)) { case SSL_ERROR_NONE: cbuf_off += k; cbuf_len -= k; if (k <= 0) goto end; /* we have done a write(con,NULL,0); */ if (cbuf_len <= 0) { read_tty = 1; write_ssl = 0; } else { /* if (cbuf_len > 0) */ read_tty = 0; write_ssl = 1; } break; case SSL_ERROR_WANT_WRITE: BIO_printf(bio_c_out, "write W BLOCK\n"); write_ssl = 1; read_tty = 0; break; case SSL_ERROR_WANT_READ: BIO_printf(bio_c_out, "write R BLOCK\n"); write_tty = 0; read_ssl = 1; write_ssl = 0; break; case SSL_ERROR_WANT_X509_LOOKUP: BIO_printf(bio_c_out, "write X BLOCK\n"); break; case SSL_ERROR_ZERO_RETURN: if (cbuf_len != 0) { BIO_printf(bio_c_out, "shutdown\n"); ret = 0; goto shut; } else { read_tty = 1; write_ssl = 0; break; } case SSL_ERROR_SYSCALL: if ((k != 0) || (cbuf_len != 0)) { BIO_printf(bio_err, "write:errno=%d\n", get_last_socket_error()); goto shut; } else { read_tty = 1; write_ssl = 0; } break; case SSL_ERROR_SSL: ERR_print_errors(bio_err); goto shut; } } #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5) /* Assume Windows/DOS/BeOS can always write */ else if (!ssl_pending && write_tty) #else else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds)) #endif { #ifdef CHARSET_EBCDIC ascii2ebcdic(&(sbuf[sbuf_off]), &(sbuf[sbuf_off]), sbuf_len); #endif i = raw_write_stdout(&(sbuf[sbuf_off]), sbuf_len); if (i <= 0) { BIO_printf(bio_c_out, "DONE\n"); ret = 0; goto shut; /* goto end; */ } sbuf_len -= i;; sbuf_off += i; if (sbuf_len <= 0) { read_ssl = 1; write_tty = 0; } } else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) { #ifdef RENEG { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii = 0; } } #endif #if 1 k = SSL_read(con, sbuf, 1024 /* BUFSIZZ */ ); #else /* Demo for pending and peek :-) */ k = SSL_read(con, sbuf, 16); { char zbuf[10240]; printf("read=%d pending=%d peek=%d\n", k, SSL_pending(con), SSL_peek(con, zbuf, 10240)); } #endif switch (SSL_get_error(con, k)) { case SSL_ERROR_NONE: if (k <= 0) goto end; sbuf_off = 0; sbuf_len = k; read_ssl = 0; write_tty = 1; break; case SSL_ERROR_WANT_WRITE: BIO_printf(bio_c_out, "read W BLOCK\n"); write_ssl = 1; read_tty = 0; break; case SSL_ERROR_WANT_READ: BIO_printf(bio_c_out, "read R BLOCK\n"); write_tty = 0; read_ssl = 1; if ((read_tty == 0) && (write_ssl == 0)) write_ssl = 1; break; case SSL_ERROR_WANT_X509_LOOKUP: BIO_printf(bio_c_out, "read X BLOCK\n"); break; case SSL_ERROR_SYSCALL: ret = get_last_socket_error(); if (c_brief) BIO_puts(bio_err, "CONNECTION CLOSED BY SERVER\n"); else BIO_printf(bio_err, "read:errno=%d\n", ret); goto shut; case SSL_ERROR_ZERO_RETURN: BIO_printf(bio_c_out, "closed\n"); ret = 0; goto shut; case SSL_ERROR_SSL: ERR_print_errors(bio_err); goto shut; /* break; */ } } #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) # if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) else if (_kbhit()) # else else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) # endif #elif defined (OPENSSL_SYS_NETWARE) else if (_kbhit()) #elif defined(OPENSSL_SYS_BEOS_R5) else if (stdin_set) #else else if (FD_ISSET(fileno(stdin), &readfds)) #endif { if (crlf) { int j, lf_num; i = raw_read_stdin(cbuf, BUFSIZZ / 2); lf_num = 0; /* both loops are skipped when i <= 0 */ for (j = 0; j < i; j++) if (cbuf[j] == '\n') lf_num++; for (j = i - 1; j >= 0; j--) { cbuf[j + lf_num] = cbuf[j]; if (cbuf[j] == '\n') { lf_num--; i++; cbuf[j + lf_num] = '\r'; } } assert(lf_num == 0); } else i = raw_read_stdin(cbuf, BUFSIZZ); if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) { BIO_printf(bio_err, "DONE\n"); ret = 0; goto shut; } if ((!c_ign_eof) && (cbuf[0] == 'R')) { BIO_printf(bio_err, "RENEGOTIATING\n"); SSL_renegotiate(con); cbuf_len = 0; } #ifndef OPENSSL_NO_HEARTBEATS else if ((!c_ign_eof) && (cbuf[0] == 'B')) { BIO_printf(bio_err, "HEARTBEATING\n"); SSL_heartbeat(con); cbuf_len = 0; } #endif else { cbuf_len = i; cbuf_off = 0; #ifdef CHARSET_EBCDIC ebcdic2ascii(cbuf, cbuf, i); #endif } write_ssl = 1; read_tty = 0; } } ret = 0; shut: if (in_init) print_stuff(bio_c_out, con, full_log); SSL_shutdown(con); SHUTDOWN(SSL_get_fd(con)); end: if (con != NULL) { if (prexit != 0) print_stuff(bio_c_out, con, 1); SSL_free(con); } #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) if (next_proto.data) OPENSSL_free(next_proto.data); #endif if (ctx != NULL) SSL_CTX_free(ctx); if (cert) X509_free(cert); if (crls) sk_X509_CRL_pop_free(crls, X509_CRL_free); if (key) EVP_PKEY_free(key); if (chain) sk_X509_pop_free(chain, X509_free); if (pass) OPENSSL_free(pass); #ifndef OPENSSL_NO_SRP OPENSSL_free(srp_arg.srppassin); #endif if (vpm) X509_VERIFY_PARAM_free(vpm); ssl_excert_free(exc); if (ssl_args) sk_OPENSSL_STRING_free(ssl_args); if (cctx) SSL_CONF_CTX_free(cctx); #ifndef OPENSSL_NO_JPAKE if (jpake_secret && psk_key) OPENSSL_free(psk_key); #endif if (cbuf != NULL) { OPENSSL_cleanse(cbuf, BUFSIZZ); OPENSSL_free(cbuf); } if (sbuf != NULL) { OPENSSL_cleanse(sbuf, BUFSIZZ); OPENSSL_free(sbuf); } if (mbuf != NULL) { OPENSSL_cleanse(mbuf, BUFSIZZ); OPENSSL_free(mbuf); } if (bio_c_out != NULL) { BIO_free(bio_c_out); bio_c_out = NULL; } if (bio_c_msg != NULL) { BIO_free(bio_c_msg); bio_c_msg = NULL; } apps_shutdown(); OPENSSL_EXIT(ret); } static void print_stuff(BIO *bio, SSL *s, int full) { X509 *peer = NULL; char *p; static const char *space = " "; char buf[BUFSIZ]; STACK_OF(X509) *sk; STACK_OF(X509_NAME) *sk2; const SSL_CIPHER *c; X509_NAME *xn; int j, i; #ifndef OPENSSL_NO_COMP const COMP_METHOD *comp, *expansion; #endif unsigned char *exportedkeymat; if (full) { int got_a_chain = 0; sk = SSL_get_peer_cert_chain(s); if (sk != NULL) { got_a_chain = 1; /* we don't have it for SSL2 (yet) */ BIO_printf(bio, "---\nCertificate chain\n"); for (i = 0; i < sk_X509_num(sk); i++) { X509_NAME_oneline(X509_get_subject_name(sk_X509_value(sk, i)), buf, sizeof buf); BIO_printf(bio, "%2d s:%s\n", i, buf); X509_NAME_oneline(X509_get_issuer_name(sk_X509_value(sk, i)), buf, sizeof buf); BIO_printf(bio, " i:%s\n", buf); if (c_showcerts) PEM_write_bio_X509(bio, sk_X509_value(sk, i)); } } BIO_printf(bio, "---\n"); peer = SSL_get_peer_certificate(s); if (peer != NULL) { BIO_printf(bio, "Server certificate\n"); /* Redundant if we showed the whole chain */ if (!(c_showcerts && got_a_chain)) PEM_write_bio_X509(bio, peer); X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf); BIO_printf(bio, "subject=%s\n", buf); X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf); BIO_printf(bio, "issuer=%s\n", buf); } else BIO_printf(bio, "no peer certificate available\n"); sk2 = SSL_get_client_CA_list(s); if ((sk2 != NULL) && (sk_X509_NAME_num(sk2) > 0)) { BIO_printf(bio, "---\nAcceptable client certificate CA names\n"); for (i = 0; i < sk_X509_NAME_num(sk2); i++) { xn = sk_X509_NAME_value(sk2, i); X509_NAME_oneline(xn, buf, sizeof(buf)); BIO_write(bio, buf, strlen(buf)); BIO_write(bio, "\n", 1); } } else { BIO_printf(bio, "---\nNo client certificate CA names sent\n"); } p = SSL_get_shared_ciphers(s, buf, sizeof buf); if (p != NULL) { /* * This works only for SSL 2. In later protocol versions, the * client does not know what other ciphers (in addition to the * one to be used in the current connection) the server supports. */ BIO_printf(bio, "---\nCiphers common between both SSL endpoints:\n"); j = i = 0; while (*p) { if (*p == ':') { BIO_write(bio, space, 15 - j % 25); i++; j = 0; BIO_write(bio, ((i % 3) ? " " : "\n"), 1); } else { BIO_write(bio, p, 1); j++; } p++; } BIO_write(bio, "\n", 1); } ssl_print_sigalgs(bio, s); ssl_print_tmp_key(bio, s); BIO_printf(bio, "---\nSSL handshake has read %ld bytes and written %ld bytes\n", BIO_number_read(SSL_get_rbio(s)), BIO_number_written(SSL_get_wbio(s))); } BIO_printf(bio, (SSL_cache_hit(s) ? "---\nReused, " : "---\nNew, ")); c = SSL_get_current_cipher(s); BIO_printf(bio, "%s, Cipher is %s\n", SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c)); if (peer != NULL) { EVP_PKEY *pktmp; pktmp = X509_get_pubkey(peer); BIO_printf(bio, "Server public key is %d bit\n", EVP_PKEY_bits(pktmp)); EVP_PKEY_free(pktmp); } BIO_printf(bio, "Secure Renegotiation IS%s supported\n", SSL_get_secure_renegotiation_support(s) ? "" : " NOT"); #ifndef OPENSSL_NO_COMP comp = SSL_get_current_compression(s); expansion = SSL_get_current_expansion(s); BIO_printf(bio, "Compression: %s\n", comp ? SSL_COMP_get_name(comp) : "NONE"); BIO_printf(bio, "Expansion: %s\n", expansion ? SSL_COMP_get_name(expansion) : "NONE"); #endif #ifdef SSL_DEBUG { /* Print out local port of connection: useful for debugging */ int sock; struct sockaddr_in ladd; socklen_t ladd_size = sizeof(ladd); sock = SSL_get_fd(s); getsockname(sock, (struct sockaddr *)&ladd, &ladd_size); BIO_printf(bio_c_out, "LOCAL PORT is %u\n", ntohs(ladd.sin_port)); } #endif #if !defined(OPENSSL_NO_TLSEXT) # if !defined(OPENSSL_NO_NEXTPROTONEG) if (next_proto.status != -1) { const unsigned char *proto; unsigned int proto_len; SSL_get0_next_proto_negotiated(s, &proto, &proto_len); BIO_printf(bio, "Next protocol: (%d) ", next_proto.status); BIO_write(bio, proto, proto_len); BIO_write(bio, "\n", 1); } # endif { const unsigned char *proto; unsigned int proto_len; SSL_get0_alpn_selected(s, &proto, &proto_len); if (proto_len > 0) { BIO_printf(bio, "ALPN protocol: "); BIO_write(bio, proto, proto_len); BIO_write(bio, "\n", 1); } else BIO_printf(bio, "No ALPN negotiated\n"); } #endif #ifndef OPENSSL_NO_SRTP { SRTP_PROTECTION_PROFILE *srtp_profile = SSL_get_selected_srtp_profile(s); if (srtp_profile) BIO_printf(bio, "SRTP Extension negotiated, profile=%s\n", srtp_profile->name); } #endif SSL_SESSION_print(bio, SSL_get_session(s)); if (keymatexportlabel != NULL) { BIO_printf(bio, "Keying material exporter:\n"); BIO_printf(bio, " Label: '%s'\n", keymatexportlabel); BIO_printf(bio, " Length: %i bytes\n", keymatexportlen); exportedkeymat = OPENSSL_malloc(keymatexportlen); if (exportedkeymat != NULL) { if (!SSL_export_keying_material(s, exportedkeymat, keymatexportlen, keymatexportlabel, strlen(keymatexportlabel), NULL, 0, 0)) { BIO_printf(bio, " Error\n"); } else { BIO_printf(bio, " Keying material: "); for (i = 0; i < keymatexportlen; i++) BIO_printf(bio, "%02X", exportedkeymat[i]); BIO_printf(bio, "\n"); } OPENSSL_free(exportedkeymat); } } BIO_printf(bio, "---\n"); if (peer != NULL) X509_free(peer); /* flush, or debugging output gets mixed with http response */ (void)BIO_flush(bio); } #ifndef OPENSSL_NO_TLSEXT static int ocsp_resp_cb(SSL *s, void *arg) { const unsigned char *p; int len; OCSP_RESPONSE *rsp; len = SSL_get_tlsext_status_ocsp_resp(s, &p); BIO_puts(arg, "OCSP response: "); if (!p) { BIO_puts(arg, "no response sent\n"); return 1; } rsp = d2i_OCSP_RESPONSE(NULL, &p, len); if (!rsp) { BIO_puts(arg, "response parse error\n"); BIO_dump_indent(arg, (char *)p, len, 4); return 0; } BIO_puts(arg, "\n======================================\n"); OCSP_RESPONSE_print(arg, rsp, 0); BIO_puts(arg, "======================================\n"); OCSP_RESPONSE_free(rsp); return 1; } #endif Index: vendor-crypto/openssl/dist/apps/s_server.c =================================================================== --- vendor-crypto/openssl/dist/apps/s_server.c (revision 296272) +++ vendor-crypto/openssl/dist/apps/s_server.c (revision 296273) @@ -1,3492 +1,3505 @@ /* apps/s_server.c */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * * This package is an SSL implementation written * by Eric Young (eay@cryptsoft.com). * The implementation was written so as to conform with Netscapes SSL. * * This library is free for commercial and non-commercial use as long as * the following conditions are aheared to. The following conditions * apply to all code found in this distribution, be it the RC4, RSA, * lhash, DES, etc., code; not just the SSL code. The SSL documentation * included with this distribution is covered by the same copyright terms * except that the holder is Tim Hudson (tjh@cryptsoft.com). * * Copyright remains Eric Young's, and as such any Copyright notices in * the code are not to be removed. * If this package is used in a product, Eric Young should be given attribution * as the author of the parts of the library used. * This can be in the form of a textual message at program startup or * in documentation (online or textual) provided with the package. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * "This product includes cryptographic software written by * Eric Young (eay@cryptsoft.com)" * The word 'cryptographic' can be left out if the rouines from the library * being used are not cryptographic related :-). * 4. If you include any Windows specific code (or a derivative thereof) from * the apps directory (application code) you must include an acknowledgement: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * The licence and distribution terms for any publically available version or * derivative of this code cannot be changed. i.e. this code cannot simply be * copied and put under another distribution licence * [including the GNU Public Licence.] */ /* ==================================================================== * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. All advertising materials mentioning features or use of this * software must display the following acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please contact * openssl-core@openssl.org. * * 5. Products derived from this software may not be called "OpenSSL" * nor may "OpenSSL" appear in their names without prior written * permission of the OpenSSL Project. * * 6. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the OpenSSL Project * for use in the OpenSSL Toolkit (http://www.openssl.org/)" * * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This product includes cryptographic software written by Eric Young * (eay@cryptsoft.com). This product includes software written by Tim * Hudson (tjh@cryptsoft.com). * */ /* ==================================================================== * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. * ECC cipher suite support in OpenSSL originally developed by * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ /* ==================================================================== * Copyright 2005 Nokia. All rights reserved. * * The portions of the attached software ("Contribution") is developed by * Nokia Corporation and is licensed pursuant to the OpenSSL open source * license. * * The Contribution, originally written by Mika Kousa and Pasi Eronen of * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites * support (see RFC 4279) to OpenSSL. * * No patent licenses or other rights except those expressly stated in * the OpenSSL open source license shall be deemed granted or received * expressly, by implication, estoppel, or otherwise. * * No assurances are provided by Nokia that the Contribution does not * infringe the patent or other intellectual property rights of any third * party or that the license provides you with all the necessary rights * to make use of the Contribution. * * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR * OTHERWISE. */ /* * Until the key-gen callbacks are modified to use newer prototypes, we allow * deprecated functions for openssl-internal code */ #ifdef OPENSSL_NO_DEPRECATED # undef OPENSSL_NO_DEPRECATED #endif #include #include #include #include #include #include #ifdef OPENSSL_NO_STDIO # define APPS_WIN16 #endif /* conflicts with winsock2 stuff on netware */ #if !defined(OPENSSL_SYS_NETWARE) # include #endif /* * With IPv6, it looks like Digital has mixed up the proper order of * recursive header file inclusion, resulting in the compiler complaining * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is * needed to have fileno() declared correctly... So let's define u_int */ #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) # define __U_INT typedef unsigned int u_int; #endif #include #include #define USE_SOCKETS #include "apps.h" #include #include #include #include #include #include #ifndef OPENSSL_NO_DH # include #endif #ifndef OPENSSL_NO_RSA # include #endif #ifndef OPENSSL_NO_SRP # include #endif #include "s_apps.h" #include "timeouts.h" #if (defined(OPENSSL_SYS_VMS) && __VMS_VER < 70000000) /* FIONBIO used as a switch to enable ioctl, and that isn't in VMS < 7.0 */ # undef FIONBIO #endif #if defined(OPENSSL_SYS_BEOS_R5) # include #endif #ifndef OPENSSL_NO_RSA static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength); #endif static int sv_body(char *hostname, int s, int stype, unsigned char *context); static int www_body(char *hostname, int s, int stype, unsigned char *context); static int rev_body(char *hostname, int s, int stype, unsigned char *context); static void close_accept_socket(void); static void sv_usage(void); static int init_ssl_connection(SSL *s); static void print_stats(BIO *bp, SSL_CTX *ctx); static int generate_session_id(const SSL *ssl, unsigned char *id, unsigned int *id_len); static void init_session_cache_ctx(SSL_CTX *sctx); static void free_sessions(void); #ifndef OPENSSL_NO_DH static DH *load_dh_param(const char *dhfile); static DH *get_dh2048(void); #endif #ifdef MONOLITH static void s_server_init(void); #endif #ifndef OPENSSL_NO_DH static unsigned char dh2048_p[] = { 0xF6,0x42,0x57,0xB7,0x08,0x7F,0x08,0x17,0x72,0xA2,0xBA,0xD6, 0xA9,0x42,0xF3,0x05,0xE8,0xF9,0x53,0x11,0x39,0x4F,0xB6,0xF1, 0x6E,0xB9,0x4B,0x38,0x20,0xDA,0x01,0xA7,0x56,0xA3,0x14,0xE9, 0x8F,0x40,0x55,0xF3,0xD0,0x07,0xC6,0xCB,0x43,0xA9,0x94,0xAD, 0xF7,0x4C,0x64,0x86,0x49,0xF8,0x0C,0x83,0xBD,0x65,0xE9,0x17, 0xD4,0xA1,0xD3,0x50,0xF8,0xF5,0x59,0x5F,0xDC,0x76,0x52,0x4F, 0x3D,0x3D,0x8D,0xDB,0xCE,0x99,0xE1,0x57,0x92,0x59,0xCD,0xFD, 0xB8,0xAE,0x74,0x4F,0xC5,0xFC,0x76,0xBC,0x83,0xC5,0x47,0x30, 0x61,0xCE,0x7C,0xC9,0x66,0xFF,0x15,0xF9,0xBB,0xFD,0x91,0x5E, 0xC7,0x01,0xAA,0xD3,0x5B,0x9E,0x8D,0xA0,0xA5,0x72,0x3A,0xD4, 0x1A,0xF0,0xBF,0x46,0x00,0x58,0x2B,0xE5,0xF4,0x88,0xFD,0x58, 0x4E,0x49,0xDB,0xCD,0x20,0xB4,0x9D,0xE4,0x91,0x07,0x36,0x6B, 0x33,0x6C,0x38,0x0D,0x45,0x1D,0x0F,0x7C,0x88,0xB3,0x1C,0x7C, 0x5B,0x2D,0x8E,0xF6,0xF3,0xC9,0x23,0xC0,0x43,0xF0,0xA5,0x5B, 0x18,0x8D,0x8E,0xBB,0x55,0x8C,0xB8,0x5D,0x38,0xD3,0x34,0xFD, 0x7C,0x17,0x57,0x43,0xA3,0x1D,0x18,0x6C,0xDE,0x33,0x21,0x2C, 0xB5,0x2A,0xFF,0x3C,0xE1,0xB1,0x29,0x40,0x18,0x11,0x8D,0x7C, 0x84,0xA7,0x0A,0x72,0xD6,0x86,0xC4,0x03,0x19,0xC8,0x07,0x29, 0x7A,0xCA,0x95,0x0C,0xD9,0x96,0x9F,0xAB,0xD0,0x0A,0x50,0x9B, 0x02,0x46,0xD3,0x08,0x3D,0x66,0xA4,0x5D,0x41,0x9F,0x9C,0x7C, 0xBD,0x89,0x4B,0x22,0x19,0x26,0xBA,0xAB,0xA2,0x5E,0xC3,0x55, 0xE9,0x32,0x0B,0x3B, }; static unsigned char dh2048_g[] = { 0x02, }; DH *get_dh2048() { DH *dh; if ((dh = DH_new()) == NULL) return NULL; dh->p=BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL); dh->g=BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL); if (dh->p == NULL || dh->g == NULL) { DH_free(dh); return NULL; } return dh; } #endif /* static int load_CA(SSL_CTX *ctx, char *file);*/ #undef BUFSIZZ #define BUFSIZZ 16*1024 static int bufsize = BUFSIZZ; static int accept_socket = -1; #define TEST_CERT "server.pem" #ifndef OPENSSL_NO_TLSEXT # define TEST_CERT2 "server2.pem" #endif #undef PROG #define PROG s_server_main extern int verify_depth, verify_return_error, verify_quiet; static int s_server_verify = SSL_VERIFY_NONE; static int s_server_session_id_context = 1; /* anything will do */ static const char *s_cert_file = TEST_CERT, *s_key_file = NULL, *s_chain_file = NULL; #ifndef OPENSSL_NO_TLSEXT static const char *s_cert_file2 = TEST_CERT2, *s_key_file2 = NULL; #endif static char *s_dcert_file = NULL, *s_dkey_file = NULL, *s_dchain_file = NULL; #ifdef FIONBIO static int s_nbio = 0; #endif static int s_nbio_test = 0; int s_crlf = 0; static SSL_CTX *ctx = NULL; #ifndef OPENSSL_NO_TLSEXT static SSL_CTX *ctx2 = NULL; #endif static int www = 0; static BIO *bio_s_out = NULL; static BIO *bio_s_msg = NULL; static int s_debug = 0; #ifndef OPENSSL_NO_TLSEXT static int s_tlsextdebug = 0; static int s_tlsextstatus = 0; static int cert_status_cb(SSL *s, void *arg); #endif static int no_resume_ephemeral = 0; static int s_msg = 0; static int s_quiet = 0; static int s_ign_eof = 0; static int s_brief = 0; static char *keymatexportlabel = NULL; static int keymatexportlen = 20; static int hack = 0; #ifndef OPENSSL_NO_ENGINE static char *engine_id = NULL; #endif static const char *session_id_prefix = NULL; static int enable_timeouts = 0; static long socket_mtu; #ifndef OPENSSL_NO_DTLS1 static int cert_chain = 0; #endif #ifndef OPENSSL_NO_TLSEXT static BIO *serverinfo_in = NULL; static const char *s_serverinfo_file = NULL; #endif #ifndef OPENSSL_NO_PSK static char *psk_identity = "Client_identity"; char *psk_key = NULL; /* by default PSK is not used */ static unsigned int psk_server_cb(SSL *ssl, const char *identity, unsigned char *psk, unsigned int max_psk_len) { unsigned int psk_len = 0; int ret; BIGNUM *bn = NULL; if (s_debug) BIO_printf(bio_s_out, "psk_server_cb\n"); if (!identity) { BIO_printf(bio_err, "Error: client did not send PSK identity\n"); goto out_err; } if (s_debug) BIO_printf(bio_s_out, "identity_len=%d identity=%s\n", (int)strlen(identity), identity); /* here we could lookup the given identity e.g. from a database */ if (strcmp(identity, psk_identity) != 0) { BIO_printf(bio_s_out, "PSK error: client identity not found" " (got '%s' expected '%s')\n", identity, psk_identity); goto out_err; } if (s_debug) BIO_printf(bio_s_out, "PSK client identity found\n"); /* convert the PSK key to binary */ ret = BN_hex2bn(&bn, psk_key); if (!ret) { BIO_printf(bio_err, "Could not convert PSK key '%s' to BIGNUM\n", psk_key); if (bn) BN_free(bn); return 0; } if (BN_num_bytes(bn) > (int)max_psk_len) { BIO_printf(bio_err, "psk buffer of callback is too small (%d) for key (%d)\n", max_psk_len, BN_num_bytes(bn)); BN_free(bn); return 0; } ret = BN_bn2bin(bn, psk); BN_free(bn); if (ret < 0) goto out_err; psk_len = (unsigned int)ret; if (s_debug) BIO_printf(bio_s_out, "fetched PSK len=%d\n", psk_len); return psk_len; out_err: if (s_debug) BIO_printf(bio_err, "Error in PSK server callback\n"); return 0; } #endif #ifndef OPENSSL_NO_SRP /* This is a context that we pass to callbacks */ typedef struct srpsrvparm_st { char *login; SRP_VBASE *vb; SRP_user_pwd *user; } srpsrvparm; /* * This callback pretends to require some asynchronous logic in order to * obtain a verifier. When the callback is called for a new connection we * return with a negative value. This will provoke the accept etc to return * with an LOOKUP_X509. The main logic of the reinvokes the suspended call * (which would normally occur after a worker has finished) and we set the * user parameters. */ static int MS_CALLBACK ssl_srp_server_param_cb(SSL *s, int *ad, void *arg) { srpsrvparm *p = (srpsrvparm *) arg; + int ret = SSL3_AL_FATAL; + if (p->login == NULL && p->user == NULL) { p->login = SSL_get_srp_username(s); BIO_printf(bio_err, "SRP username = \"%s\"\n", p->login); return (-1); } if (p->user == NULL) { BIO_printf(bio_err, "User %s doesn't exist\n", p->login); - return SSL3_AL_FATAL; + goto err; } + if (SSL_set_srp_server_param (s, p->user->N, p->user->g, p->user->s, p->user->v, p->user->info) < 0) { *ad = SSL_AD_INTERNAL_ERROR; - return SSL3_AL_FATAL; + goto err; } BIO_printf(bio_err, "SRP parameters set: username = \"%s\" info=\"%s\" \n", p->login, p->user->info); - /* need to check whether there are memory leaks */ + ret = SSL_ERROR_NONE; + +err: + SRP_user_pwd_free(p->user); p->user = NULL; p->login = NULL; - return SSL_ERROR_NONE; + return ret; } #endif #ifdef MONOLITH static void s_server_init(void) { accept_socket = -1; s_server_verify = SSL_VERIFY_NONE; s_dcert_file = NULL; s_dkey_file = NULL; s_dchain_file = NULL; s_cert_file = TEST_CERT; s_key_file = NULL; s_chain_file = NULL; # ifndef OPENSSL_NO_TLSEXT s_cert_file2 = TEST_CERT2; s_key_file2 = NULL; ctx2 = NULL; # endif # ifdef FIONBIO s_nbio = 0; # endif s_nbio_test = 0; ctx = NULL; www = 0; bio_s_out = NULL; s_debug = 0; s_msg = 0; s_quiet = 0; s_brief = 0; hack = 0; # ifndef OPENSSL_NO_ENGINE engine_id = NULL; # endif } #endif static void sv_usage(void) { BIO_printf(bio_err, "usage: s_server [args ...]\n"); BIO_printf(bio_err, "\n"); BIO_printf(bio_err, " -accept arg - port to accept on (default is %d)\n", PORT); BIO_printf(bio_err, " -verify_hostname host - check peer certificate matches \"host\"\n"); BIO_printf(bio_err, " -verify_email email - check peer certificate matches \"email\"\n"); BIO_printf(bio_err, " -verify_ip ipaddr - check peer certificate matches \"ipaddr\"\n"); BIO_printf(bio_err, " -context arg - set session ID context\n"); BIO_printf(bio_err, " -verify arg - turn on peer certificate verification\n"); BIO_printf(bio_err, " -Verify arg - turn on peer certificate verification, must have a cert.\n"); BIO_printf(bio_err, " -verify_return_error - return verification errors\n"); BIO_printf(bio_err, " -cert arg - certificate file to use\n"); BIO_printf(bio_err, " (default is %s)\n", TEST_CERT); #ifndef OPENSSL_NO_TLSEXT BIO_printf(bio_err, " -serverinfo arg - PEM serverinfo file for certificate\n"); BIO_printf(bio_err, " -auth - send and receive RFC 5878 TLS auth extensions and supplemental data\n"); BIO_printf(bio_err, " -auth_require_reneg - Do not send TLS auth extensions until renegotiation\n"); #endif BIO_printf(bio_err, " -no_resumption_on_reneg - set SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION flag\n"); BIO_printf(bio_err, " -crl_check - check the peer certificate has not been revoked by its CA.\n" " The CRL(s) are appended to the certificate file\n"); BIO_printf(bio_err, " -crl_check_all - check the peer certificate has not been revoked by its CA\n" " or any other CRL in the CA chain. CRL(s) are appened to the\n" " the certificate file.\n"); BIO_printf(bio_err, " -certform arg - certificate format (PEM or DER) PEM default\n"); BIO_printf(bio_err, " -key arg - Private Key file to use, in cert file if\n"); BIO_printf(bio_err, " not specified (default is %s)\n", TEST_CERT); BIO_printf(bio_err, " -keyform arg - key format (PEM, DER or ENGINE) PEM default\n"); BIO_printf(bio_err, " -pass arg - private key file pass phrase source\n"); BIO_printf(bio_err, " -dcert arg - second certificate file to use (usually for DSA)\n"); BIO_printf(bio_err, " -dcertform x - second certificate format (PEM or DER) PEM default\n"); BIO_printf(bio_err, " -dkey arg - second private key file to use (usually for DSA)\n"); BIO_printf(bio_err, " -dkeyform arg - second key format (PEM, DER or ENGINE) PEM default\n"); BIO_printf(bio_err, " -dpass arg - second private key file pass phrase source\n"); BIO_printf(bio_err, " -dhparam arg - DH parameter file to use, in cert file if not specified\n"); BIO_printf(bio_err, " or a default set of parameters is used\n"); #ifndef OPENSSL_NO_ECDH BIO_printf(bio_err, " -named_curve arg - Elliptic curve name to use for ephemeral ECDH keys.\n" " Use \"openssl ecparam -list_curves\" for all names\n" " (default is nistp256).\n"); #endif #ifdef FIONBIO BIO_printf(bio_err, " -nbio - Run with non-blocking IO\n"); #endif BIO_printf(bio_err, " -nbio_test - test with the non-blocking test bio\n"); BIO_printf(bio_err, " -crlf - convert LF from terminal into CRLF\n"); BIO_printf(bio_err, " -debug - Print more output\n"); BIO_printf(bio_err, " -msg - Show protocol messages\n"); BIO_printf(bio_err, " -state - Print the SSL states\n"); BIO_printf(bio_err, " -CApath arg - PEM format directory of CA's\n"); BIO_printf(bio_err, " -CAfile arg - PEM format file of CA's\n"); BIO_printf(bio_err, " -no_alt_chains - only ever use the first certificate chain found\n"); BIO_printf(bio_err, " -nocert - Don't use any certificates (Anon-DH)\n"); BIO_printf(bio_err, " -cipher arg - play with 'openssl ciphers' to see what goes here\n"); BIO_printf(bio_err, " -serverpref - Use server's cipher preferences\n"); BIO_printf(bio_err, " -quiet - No server output\n"); BIO_printf(bio_err, " -no_tmp_rsa - Do not generate a tmp RSA key\n"); #ifndef OPENSSL_NO_PSK BIO_printf(bio_err, " -psk_hint arg - PSK identity hint to use\n"); BIO_printf(bio_err, " -psk arg - PSK in hex (without 0x)\n"); # ifndef OPENSSL_NO_JPAKE BIO_printf(bio_err, " -jpake arg - JPAKE secret to use\n"); # endif #endif #ifndef OPENSSL_NO_SRP BIO_printf(bio_err, " -srpvfile file - The verifier file for SRP\n"); BIO_printf(bio_err, " -srpuserseed string - A seed string for a default user salt.\n"); #endif BIO_printf(bio_err, " -ssl2 - Just talk SSLv2\n"); #ifndef OPENSSL_NO_SSL3_METHOD BIO_printf(bio_err, " -ssl3 - Just talk SSLv3\n"); #endif BIO_printf(bio_err, " -tls1_2 - Just talk TLSv1.2\n"); BIO_printf(bio_err, " -tls1_1 - Just talk TLSv1.1\n"); BIO_printf(bio_err, " -tls1 - Just talk TLSv1\n"); BIO_printf(bio_err, " -dtls1 - Just talk DTLSv1\n"); BIO_printf(bio_err, " -dtls1_2 - Just talk DTLSv1.2\n"); BIO_printf(bio_err, " -timeout - Enable timeouts\n"); BIO_printf(bio_err, " -mtu - Set link layer MTU\n"); BIO_printf(bio_err, " -chain - Read a certificate chain\n"); BIO_printf(bio_err, " -no_ssl2 - Just disable SSLv2\n"); BIO_printf(bio_err, " -no_ssl3 - Just disable SSLv3\n"); BIO_printf(bio_err, " -no_tls1 - Just disable TLSv1\n"); BIO_printf(bio_err, " -no_tls1_1 - Just disable TLSv1.1\n"); BIO_printf(bio_err, " -no_tls1_2 - Just disable TLSv1.2\n"); #ifndef OPENSSL_NO_DH BIO_printf(bio_err, " -no_dhe - Disable ephemeral DH\n"); #endif #ifndef OPENSSL_NO_ECDH BIO_printf(bio_err, " -no_ecdhe - Disable ephemeral ECDH\n"); #endif BIO_printf(bio_err, " -bugs - Turn on SSL bug compatibility\n"); BIO_printf(bio_err, " -hack - workaround for early Netscape code\n"); BIO_printf(bio_err, " -www - Respond to a 'GET /' with a status page\n"); BIO_printf(bio_err, " -WWW - Respond to a 'GET / HTTP/1.0' with file ./\n"); BIO_printf(bio_err, " -HTTP - Respond to a 'GET / HTTP/1.0' with file ./\n"); BIO_printf(bio_err, " with the assumption it contains a complete HTTP response.\n"); #ifndef OPENSSL_NO_ENGINE BIO_printf(bio_err, " -engine id - Initialise and use the specified engine\n"); #endif BIO_printf(bio_err, " -id_prefix arg - Generate SSL/TLS session IDs prefixed by 'arg'\n"); BIO_printf(bio_err, " -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); #ifndef OPENSSL_NO_TLSEXT BIO_printf(bio_err, " -servername host - servername for HostName TLS extension\n"); BIO_printf(bio_err, " -servername_fatal - on mismatch send fatal alert (default warning alert)\n"); BIO_printf(bio_err, " -cert2 arg - certificate file to use for servername\n"); BIO_printf(bio_err, " (default is %s)\n", TEST_CERT2); BIO_printf(bio_err, " -key2 arg - Private Key file to use for servername, in cert file if\n"); BIO_printf(bio_err, " not specified (default is %s)\n", TEST_CERT2); BIO_printf(bio_err, " -tlsextdebug - hex dump of all TLS extensions received\n"); BIO_printf(bio_err, " -no_ticket - disable use of RFC4507bis session tickets\n"); BIO_printf(bio_err, " -legacy_renegotiation - enable use of legacy renegotiation (dangerous)\n"); BIO_printf(bio_err, " -sigalgs arg - Signature algorithms to support (colon-separated list)\n"); BIO_printf(bio_err, " -client_sigalgs arg - Signature algorithms to support for client \n"); BIO_printf(bio_err, " certificate authentication (colon-separated list)\n"); # ifndef OPENSSL_NO_NEXTPROTONEG BIO_printf(bio_err, " -nextprotoneg arg - set the advertised protocols for the NPN extension (comma-separated list)\n"); # endif # ifndef OPENSSL_NO_SRTP BIO_printf(bio_err, " -use_srtp profiles - Offer SRTP key management with a colon-separated profile list\n"); # endif BIO_printf(bio_err, " -alpn arg - set the advertised protocols for the ALPN extension (comma-separated list)\n"); #endif BIO_printf(bio_err, " -keymatexport label - Export keying material using label\n"); BIO_printf(bio_err, " -keymatexportlen len - Export len bytes of keying material (default 20)\n"); BIO_printf(bio_err, " -status - respond to certificate status requests\n"); BIO_printf(bio_err, " -status_verbose - enable status request verbose printout\n"); BIO_printf(bio_err, " -status_timeout n - status request responder timeout\n"); BIO_printf(bio_err, " -status_url URL - status request fallback URL\n"); } static int local_argc = 0; static char **local_argv; #ifdef CHARSET_EBCDIC static int ebcdic_new(BIO *bi); static int ebcdic_free(BIO *a); static int ebcdic_read(BIO *b, char *out, int outl); static int ebcdic_write(BIO *b, const char *in, int inl); static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr); static int ebcdic_gets(BIO *bp, char *buf, int size); static int ebcdic_puts(BIO *bp, const char *str); # define BIO_TYPE_EBCDIC_FILTER (18|0x0200) static BIO_METHOD methods_ebcdic = { BIO_TYPE_EBCDIC_FILTER, "EBCDIC/ASCII filter", ebcdic_write, ebcdic_read, ebcdic_puts, ebcdic_gets, ebcdic_ctrl, ebcdic_new, ebcdic_free, }; typedef struct { size_t alloced; char buff[1]; } EBCDIC_OUTBUFF; BIO_METHOD *BIO_f_ebcdic_filter() { return (&methods_ebcdic); } static int ebcdic_new(BIO *bi) { EBCDIC_OUTBUFF *wbuf; wbuf = (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + 1024); if (!wbuf) return 0; wbuf->alloced = 1024; wbuf->buff[0] = '\0'; bi->ptr = (char *)wbuf; bi->init = 1; bi->flags = 0; return (1); } static int ebcdic_free(BIO *a) { if (a == NULL) return (0); if (a->ptr != NULL) OPENSSL_free(a->ptr); a->ptr = NULL; a->init = 0; a->flags = 0; return (1); } static int ebcdic_read(BIO *b, char *out, int outl) { int ret = 0; if (out == NULL || outl == 0) return (0); if (b->next_bio == NULL) return (0); ret = BIO_read(b->next_bio, out, outl); if (ret > 0) ascii2ebcdic(out, out, ret); return (ret); } static int ebcdic_write(BIO *b, const char *in, int inl) { EBCDIC_OUTBUFF *wbuf; int ret = 0; int num; unsigned char n; if ((in == NULL) || (inl <= 0)) return (0); if (b->next_bio == NULL) return (0); wbuf = (EBCDIC_OUTBUFF *) b->ptr; if (inl > (num = wbuf->alloced)) { num = num + num; /* double the size */ if (num < inl) num = inl; wbuf = (EBCDIC_OUTBUFF *) OPENSSL_malloc(sizeof(EBCDIC_OUTBUFF) + num); if (!wbuf) return 0; OPENSSL_free(b->ptr); wbuf->alloced = num; wbuf->buff[0] = '\0'; b->ptr = (char *)wbuf; } ebcdic2ascii(wbuf->buff, in, inl); ret = BIO_write(b->next_bio, wbuf->buff, inl); return (ret); } static long ebcdic_ctrl(BIO *b, int cmd, long num, void *ptr) { long ret; if (b->next_bio == NULL) return (0); switch (cmd) { case BIO_CTRL_DUP: ret = 0L; break; default: ret = BIO_ctrl(b->next_bio, cmd, num, ptr); break; } return (ret); } static int ebcdic_gets(BIO *bp, char *buf, int size) { int i, ret = 0; if (bp->next_bio == NULL) return (0); /* return(BIO_gets(bp->next_bio,buf,size));*/ for (i = 0; i < size - 1; ++i) { ret = ebcdic_read(bp, &buf[i], 1); if (ret <= 0) break; else if (buf[i] == '\n') { ++i; break; } } if (i < size) buf[i] = '\0'; return (ret < 0 && i == 0) ? ret : i; } static int ebcdic_puts(BIO *bp, const char *str) { if (bp->next_bio == NULL) return (0); return ebcdic_write(bp, str, strlen(str)); } #endif #ifndef OPENSSL_NO_TLSEXT /* This is a context that we pass to callbacks */ typedef struct tlsextctx_st { char *servername; BIO *biodebug; int extension_error; } tlsextctx; static int MS_CALLBACK ssl_servername_cb(SSL *s, int *ad, void *arg) { tlsextctx *p = (tlsextctx *) arg; const char *servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (servername && p->biodebug) BIO_printf(p->biodebug, "Hostname in TLS extension: \"%s\"\n", servername); if (!p->servername) return SSL_TLSEXT_ERR_NOACK; if (servername) { if (strcasecmp(servername, p->servername)) return p->extension_error; if (ctx2) { BIO_printf(p->biodebug, "Switching server context.\n"); SSL_set_SSL_CTX(s, ctx2); } } return SSL_TLSEXT_ERR_OK; } /* Structure passed to cert status callback */ typedef struct tlsextstatusctx_st { /* Default responder to use */ char *host, *path, *port; int use_ssl; int timeout; BIO *err; int verbose; } tlsextstatusctx; static tlsextstatusctx tlscstatp = { NULL, NULL, NULL, 0, -1, NULL, 0 }; /* * Certificate Status callback. This is called when a client includes a * certificate status request extension. This is a simplified version. It * examines certificates each time and makes one OCSP responder query for * each request. A full version would store details such as the OCSP * certificate IDs and minimise the number of OCSP responses by caching them * until they were considered "expired". */ static int cert_status_cb(SSL *s, void *arg) { tlsextstatusctx *srctx = arg; BIO *err = srctx->err; char *host, *port, *path; int use_ssl; unsigned char *rspder = NULL; int rspderlen; STACK_OF(OPENSSL_STRING) *aia = NULL; X509 *x = NULL; X509_STORE_CTX inctx; X509_OBJECT obj; OCSP_REQUEST *req = NULL; OCSP_RESPONSE *resp = NULL; OCSP_CERTID *id = NULL; STACK_OF(X509_EXTENSION) *exts; int ret = SSL_TLSEXT_ERR_NOACK; int i; # if 0 STACK_OF(OCSP_RESPID) *ids; SSL_get_tlsext_status_ids(s, &ids); BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids)); # endif if (srctx->verbose) BIO_puts(err, "cert_status: callback called\n"); /* Build up OCSP query from server certificate */ x = SSL_get_certificate(s); aia = X509_get1_ocsp(x); if (aia) { if (!OCSP_parse_url(sk_OPENSSL_STRING_value(aia, 0), &host, &port, &path, &use_ssl)) { BIO_puts(err, "cert_status: can't parse AIA URL\n"); goto err; } if (srctx->verbose) BIO_printf(err, "cert_status: AIA URL: %s\n", sk_OPENSSL_STRING_value(aia, 0)); } else { if (!srctx->host) { BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n"); goto done; } host = srctx->host; path = srctx->path; port = srctx->port; use_ssl = srctx->use_ssl; } if (!X509_STORE_CTX_init(&inctx, SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)), NULL, NULL)) goto err; if (X509_STORE_get_by_subject(&inctx, X509_LU_X509, X509_get_issuer_name(x), &obj) <= 0) { BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n"); X509_STORE_CTX_cleanup(&inctx); goto done; } req = OCSP_REQUEST_new(); if (!req) goto err; id = OCSP_cert_to_id(NULL, x, obj.data.x509); X509_free(obj.data.x509); X509_STORE_CTX_cleanup(&inctx); if (!id) goto err; if (!OCSP_request_add0_id(req, id)) goto err; id = NULL; /* Add any extensions to the request */ SSL_get_tlsext_status_exts(s, &exts); for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); if (!OCSP_REQUEST_add_ext(req, ext, -1)) goto err; } resp = process_responder(err, req, host, path, port, use_ssl, NULL, srctx->timeout); if (!resp) { BIO_puts(err, "cert_status: error querying responder\n"); goto done; } rspderlen = i2d_OCSP_RESPONSE(resp, &rspder); if (rspderlen <= 0) goto err; SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen); if (srctx->verbose) { BIO_puts(err, "cert_status: ocsp response sent:\n"); OCSP_RESPONSE_print(err, resp, 2); } ret = SSL_TLSEXT_ERR_OK; done: if (ret != SSL_TLSEXT_ERR_OK) ERR_print_errors(err); if (aia) { OPENSSL_free(host); OPENSSL_free(path); OPENSSL_free(port); X509_email_free(aia); } if (id) OCSP_CERTID_free(id); if (req) OCSP_REQUEST_free(req); if (resp) OCSP_RESPONSE_free(resp); return ret; err: ret = SSL_TLSEXT_ERR_ALERT_FATAL; goto done; } # ifndef OPENSSL_NO_NEXTPROTONEG /* This is the context that we pass to next_proto_cb */ typedef struct tlsextnextprotoctx_st { unsigned char *data; unsigned int len; } tlsextnextprotoctx; static int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len, void *arg) { tlsextnextprotoctx *next_proto = arg; *data = next_proto->data; *len = next_proto->len; return SSL_TLSEXT_ERR_OK; } # endif /* ndef OPENSSL_NO_NEXTPROTONEG */ /* This the context that we pass to alpn_cb */ typedef struct tlsextalpnctx_st { unsigned char *data; unsigned short len; } tlsextalpnctx; static int alpn_cb(SSL *s, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg) { tlsextalpnctx *alpn_ctx = arg; if (!s_quiet) { /* We can assume that |in| is syntactically valid. */ unsigned i; BIO_printf(bio_s_out, "ALPN protocols advertised by the client: "); for (i = 0; i < inlen;) { if (i) BIO_write(bio_s_out, ", ", 2); BIO_write(bio_s_out, &in[i + 1], in[i]); i += in[i] + 1; } BIO_write(bio_s_out, "\n", 1); } if (SSL_select_next_proto ((unsigned char **)out, outlen, alpn_ctx->data, alpn_ctx->len, in, inlen) != OPENSSL_NPN_NEGOTIATED) { return SSL_TLSEXT_ERR_NOACK; } if (!s_quiet) { BIO_printf(bio_s_out, "ALPN protocols selected: "); BIO_write(bio_s_out, *out, *outlen); BIO_write(bio_s_out, "\n", 1); } return SSL_TLSEXT_ERR_OK; } #endif /* ndef OPENSSL_NO_TLSEXT */ int MAIN(int, char **); #ifndef OPENSSL_NO_JPAKE static char *jpake_secret = NULL; # define no_jpake !jpake_secret #else # define no_jpake 1 #endif #ifndef OPENSSL_NO_SRP static srpsrvparm srp_callback_parm; #endif #ifndef OPENSSL_NO_SRTP static char *srtp_profiles = NULL; #endif int MAIN(int argc, char *argv[]) { X509_VERIFY_PARAM *vpm = NULL; int badarg = 0; short port = PORT; char *CApath = NULL, *CAfile = NULL; char *chCApath = NULL, *chCAfile = NULL; char *vfyCApath = NULL, *vfyCAfile = NULL; unsigned char *context = NULL; char *dhfile = NULL; int badop = 0; int ret = 1; int build_chain = 0; int no_tmp_rsa = 0, no_dhe = 0, no_ecdhe = 0, nocert = 0; int state = 0; const SSL_METHOD *meth = NULL; int socket_type = SOCK_STREAM; ENGINE *e = NULL; char *inrand = NULL; int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM; char *passarg = NULL, *pass = NULL; char *dpassarg = NULL, *dpass = NULL; int s_dcert_format = FORMAT_PEM, s_dkey_format = FORMAT_PEM; X509 *s_cert = NULL, *s_dcert = NULL; STACK_OF(X509) *s_chain = NULL, *s_dchain = NULL; EVP_PKEY *s_key = NULL, *s_dkey = NULL; int no_cache = 0, ext_cache = 0; int rev = 0, naccept = -1; #ifndef OPENSSL_NO_TLSEXT EVP_PKEY *s_key2 = NULL; X509 *s_cert2 = NULL; tlsextctx tlsextcbp = { NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING }; # ifndef OPENSSL_NO_NEXTPROTONEG const char *next_proto_neg_in = NULL; tlsextnextprotoctx next_proto = { NULL, 0 }; # endif const char *alpn_in = NULL; tlsextalpnctx alpn_ctx = { NULL, 0 }; #endif #ifndef OPENSSL_NO_PSK /* by default do not send a PSK identity hint */ static char *psk_identity_hint = NULL; #endif #ifndef OPENSSL_NO_SRP char *srpuserseed = NULL; char *srp_verifier_file = NULL; #endif SSL_EXCERT *exc = NULL; SSL_CONF_CTX *cctx = NULL; STACK_OF(OPENSSL_STRING) *ssl_args = NULL; char *crl_file = NULL; int crl_format = FORMAT_PEM; int crl_download = 0; STACK_OF(X509_CRL) *crls = NULL; meth = SSLv23_server_method(); local_argc = argc; local_argv = argv; apps_startup(); #ifdef MONOLITH s_server_init(); #endif if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; cctx = SSL_CONF_CTX_new(); if (!cctx) goto end; SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_SERVER); SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_CMDLINE); verify_depth = 0; #ifdef FIONBIO s_nbio = 0; #endif s_nbio_test = 0; argc--; argv++; while (argc >= 1) { if ((strcmp(*argv, "-port") == 0) || (strcmp(*argv, "-accept") == 0)) { if (--argc < 1) goto bad; if (!extract_port(*(++argv), &port)) goto bad; } else if (strcmp(*argv, "-naccept") == 0) { if (--argc < 1) goto bad; naccept = atol(*(++argv)); if (naccept <= 0) { BIO_printf(bio_err, "bad accept value %s\n", *argv); goto bad; } } else if (strcmp(*argv, "-verify") == 0) { s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE; if (--argc < 1) goto bad; verify_depth = atoi(*(++argv)); if (!s_quiet) BIO_printf(bio_err, "verify depth is %d\n", verify_depth); } else if (strcmp(*argv, "-Verify") == 0) { s_server_verify = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE; if (--argc < 1) goto bad; verify_depth = atoi(*(++argv)); if (!s_quiet) BIO_printf(bio_err, "verify depth is %d, must return a certificate\n", verify_depth); } else if (strcmp(*argv, "-context") == 0) { if (--argc < 1) goto bad; context = (unsigned char *)*(++argv); } else if (strcmp(*argv, "-cert") == 0) { if (--argc < 1) goto bad; s_cert_file = *(++argv); } else if (strcmp(*argv, "-CRL") == 0) { if (--argc < 1) goto bad; crl_file = *(++argv); } else if (strcmp(*argv, "-crl_download") == 0) crl_download = 1; #ifndef OPENSSL_NO_TLSEXT else if (strcmp(*argv, "-serverinfo") == 0) { if (--argc < 1) goto bad; s_serverinfo_file = *(++argv); } #endif else if (strcmp(*argv, "-certform") == 0) { if (--argc < 1) goto bad; s_cert_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-key") == 0) { if (--argc < 1) goto bad; s_key_file = *(++argv); } else if (strcmp(*argv, "-keyform") == 0) { if (--argc < 1) goto bad; s_key_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-pass") == 0) { if (--argc < 1) goto bad; passarg = *(++argv); } else if (strcmp(*argv, "-cert_chain") == 0) { if (--argc < 1) goto bad; s_chain_file = *(++argv); } else if (strcmp(*argv, "-dhparam") == 0) { if (--argc < 1) goto bad; dhfile = *(++argv); } else if (strcmp(*argv, "-dcertform") == 0) { if (--argc < 1) goto bad; s_dcert_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-dcert") == 0) { if (--argc < 1) goto bad; s_dcert_file = *(++argv); } else if (strcmp(*argv, "-dkeyform") == 0) { if (--argc < 1) goto bad; s_dkey_format = str2fmt(*(++argv)); } else if (strcmp(*argv, "-dpass") == 0) { if (--argc < 1) goto bad; dpassarg = *(++argv); } else if (strcmp(*argv, "-dkey") == 0) { if (--argc < 1) goto bad; s_dkey_file = *(++argv); } else if (strcmp(*argv, "-dcert_chain") == 0) { if (--argc < 1) goto bad; s_dchain_file = *(++argv); } else if (strcmp(*argv, "-nocert") == 0) { nocert = 1; } else if (strcmp(*argv, "-CApath") == 0) { if (--argc < 1) goto bad; CApath = *(++argv); } else if (strcmp(*argv, "-chainCApath") == 0) { if (--argc < 1) goto bad; chCApath = *(++argv); } else if (strcmp(*argv, "-verifyCApath") == 0) { if (--argc < 1) goto bad; vfyCApath = *(++argv); } else if (strcmp(*argv, "-no_cache") == 0) no_cache = 1; else if (strcmp(*argv, "-ext_cache") == 0) ext_cache = 1; else if (strcmp(*argv, "-CRLform") == 0) { if (--argc < 1) goto bad; crl_format = str2fmt(*(++argv)); } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) { if (badarg) goto bad; continue; } else if (args_excert(&argv, &argc, &badarg, bio_err, &exc)) { if (badarg) goto bad; continue; } else if (args_ssl(&argv, &argc, cctx, &badarg, bio_err, &ssl_args)) { if (badarg) goto bad; continue; } else if (strcmp(*argv, "-verify_return_error") == 0) verify_return_error = 1; else if (strcmp(*argv, "-verify_quiet") == 0) verify_quiet = 1; else if (strcmp(*argv, "-build_chain") == 0) build_chain = 1; else if (strcmp(*argv, "-CAfile") == 0) { if (--argc < 1) goto bad; CAfile = *(++argv); } else if (strcmp(*argv, "-chainCAfile") == 0) { if (--argc < 1) goto bad; chCAfile = *(++argv); } else if (strcmp(*argv, "-verifyCAfile") == 0) { if (--argc < 1) goto bad; vfyCAfile = *(++argv); } #ifdef FIONBIO else if (strcmp(*argv, "-nbio") == 0) { s_nbio = 1; } #endif else if (strcmp(*argv, "-nbio_test") == 0) { #ifdef FIONBIO s_nbio = 1; #endif s_nbio_test = 1; } else if (strcmp(*argv, "-ign_eof") == 0) s_ign_eof = 1; else if (strcmp(*argv, "-no_ign_eof") == 0) s_ign_eof = 0; else if (strcmp(*argv, "-debug") == 0) { s_debug = 1; } #ifndef OPENSSL_NO_TLSEXT else if (strcmp(*argv, "-tlsextdebug") == 0) s_tlsextdebug = 1; else if (strcmp(*argv, "-status") == 0) s_tlsextstatus = 1; else if (strcmp(*argv, "-status_verbose") == 0) { s_tlsextstatus = 1; tlscstatp.verbose = 1; } else if (!strcmp(*argv, "-status_timeout")) { s_tlsextstatus = 1; if (--argc < 1) goto bad; tlscstatp.timeout = atoi(*(++argv)); } else if (!strcmp(*argv, "-status_url")) { s_tlsextstatus = 1; if (--argc < 1) goto bad; if (!OCSP_parse_url(*(++argv), &tlscstatp.host, &tlscstatp.port, &tlscstatp.path, &tlscstatp.use_ssl)) { BIO_printf(bio_err, "Error parsing URL\n"); goto bad; } } #endif else if (strcmp(*argv, "-msg") == 0) { s_msg = 1; } else if (strcmp(*argv, "-msgfile") == 0) { if (--argc < 1) goto bad; bio_s_msg = BIO_new_file(*(++argv), "w"); } #ifndef OPENSSL_NO_SSL_TRACE else if (strcmp(*argv, "-trace") == 0) { s_msg = 2; } #endif else if (strcmp(*argv, "-hack") == 0) { hack = 1; } else if (strcmp(*argv, "-state") == 0) { state = 1; } else if (strcmp(*argv, "-crlf") == 0) { s_crlf = 1; } else if (strcmp(*argv, "-quiet") == 0) { s_quiet = 1; } else if (strcmp(*argv, "-brief") == 0) { s_quiet = 1; s_brief = 1; verify_quiet = 1; } else if (strcmp(*argv, "-no_tmp_rsa") == 0) { no_tmp_rsa = 1; } else if (strcmp(*argv, "-no_dhe") == 0) { no_dhe = 1; } else if (strcmp(*argv, "-no_ecdhe") == 0) { no_ecdhe = 1; } else if (strcmp(*argv, "-no_resume_ephemeral") == 0) { no_resume_ephemeral = 1; } #ifndef OPENSSL_NO_PSK else if (strcmp(*argv, "-psk_hint") == 0) { if (--argc < 1) goto bad; psk_identity_hint = *(++argv); } else if (strcmp(*argv, "-psk") == 0) { size_t i; if (--argc < 1) goto bad; psk_key = *(++argv); for (i = 0; i < strlen(psk_key); i++) { if (isxdigit((unsigned char)psk_key[i])) continue; BIO_printf(bio_err, "Not a hex number '%s'\n", *argv); goto bad; } } #endif #ifndef OPENSSL_NO_SRP else if (strcmp(*argv, "-srpvfile") == 0) { if (--argc < 1) goto bad; srp_verifier_file = *(++argv); meth = TLSv1_server_method(); } else if (strcmp(*argv, "-srpuserseed") == 0) { if (--argc < 1) goto bad; srpuserseed = *(++argv); meth = TLSv1_server_method(); } #endif else if (strcmp(*argv, "-rev") == 0) { rev = 1; } else if (strcmp(*argv, "-www") == 0) { www = 1; } else if (strcmp(*argv, "-WWW") == 0) { www = 2; } else if (strcmp(*argv, "-HTTP") == 0) { www = 3; } #ifndef OPENSSL_NO_SSL2 else if (strcmp(*argv, "-ssl2") == 0) { no_ecdhe = 1; meth = SSLv2_server_method(); } #endif #ifndef OPENSSL_NO_SSL3_METHOD else if (strcmp(*argv, "-ssl3") == 0) { meth = SSLv3_server_method(); } #endif #ifndef OPENSSL_NO_TLS1 else if (strcmp(*argv, "-tls1") == 0) { meth = TLSv1_server_method(); } else if (strcmp(*argv, "-tls1_1") == 0) { meth = TLSv1_1_server_method(); } else if (strcmp(*argv, "-tls1_2") == 0) { meth = TLSv1_2_server_method(); } #endif #ifndef OPENSSL_NO_DTLS1 else if (strcmp(*argv, "-dtls") == 0) { meth = DTLS_server_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-dtls1") == 0) { meth = DTLSv1_server_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-dtls1_2") == 0) { meth = DTLSv1_2_server_method(); socket_type = SOCK_DGRAM; } else if (strcmp(*argv, "-timeout") == 0) enable_timeouts = 1; else if (strcmp(*argv, "-mtu") == 0) { if (--argc < 1) goto bad; socket_mtu = atol(*(++argv)); } else if (strcmp(*argv, "-chain") == 0) cert_chain = 1; #endif else if (strcmp(*argv, "-id_prefix") == 0) { if (--argc < 1) goto bad; session_id_prefix = *(++argv); } #ifndef OPENSSL_NO_ENGINE else if (strcmp(*argv, "-engine") == 0) { if (--argc < 1) goto bad; engine_id = *(++argv); } #endif else if (strcmp(*argv, "-rand") == 0) { if (--argc < 1) goto bad; inrand = *(++argv); } #ifndef OPENSSL_NO_TLSEXT else if (strcmp(*argv, "-servername") == 0) { if (--argc < 1) goto bad; tlsextcbp.servername = *(++argv); } else if (strcmp(*argv, "-servername_fatal") == 0) { tlsextcbp.extension_error = SSL_TLSEXT_ERR_ALERT_FATAL; } else if (strcmp(*argv, "-cert2") == 0) { if (--argc < 1) goto bad; s_cert_file2 = *(++argv); } else if (strcmp(*argv, "-key2") == 0) { if (--argc < 1) goto bad; s_key_file2 = *(++argv); } # ifndef OPENSSL_NO_NEXTPROTONEG else if (strcmp(*argv, "-nextprotoneg") == 0) { if (--argc < 1) goto bad; next_proto_neg_in = *(++argv); } # endif else if (strcmp(*argv, "-alpn") == 0) { if (--argc < 1) goto bad; alpn_in = *(++argv); } #endif #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) else if (strcmp(*argv, "-jpake") == 0) { if (--argc < 1) goto bad; jpake_secret = *(++argv); } #endif #ifndef OPENSSL_NO_SRTP else if (strcmp(*argv, "-use_srtp") == 0) { if (--argc < 1) goto bad; srtp_profiles = *(++argv); } #endif else if (strcmp(*argv, "-keymatexport") == 0) { if (--argc < 1) goto bad; keymatexportlabel = *(++argv); } else if (strcmp(*argv, "-keymatexportlen") == 0) { if (--argc < 1) goto bad; keymatexportlen = atoi(*(++argv)); if (keymatexportlen == 0) goto bad; } else { BIO_printf(bio_err, "unknown option %s\n", *argv); badop = 1; break; } argc--; argv++; } if (badop) { bad: sv_usage(); goto end; } #ifndef OPENSSL_NO_DTLS1 if (www && socket_type == SOCK_DGRAM) { BIO_printf(bio_err, "Can't use -HTTP, -www or -WWW with DTLS\n"); goto end; } #endif #if !defined(OPENSSL_NO_JPAKE) && !defined(OPENSSL_NO_PSK) if (jpake_secret) { if (psk_key) { BIO_printf(bio_err, "Can't use JPAKE and PSK together\n"); goto end; } psk_identity = "JPAKE"; } #endif SSL_load_error_strings(); OpenSSL_add_ssl_algorithms(); #ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine_id, 1); #endif if (!app_passwd(bio_err, passarg, dpassarg, &pass, &dpass)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } if (s_key_file == NULL) s_key_file = s_cert_file; #ifndef OPENSSL_NO_TLSEXT if (s_key_file2 == NULL) s_key_file2 = s_cert_file2; #endif if (!load_excert(&exc, bio_err)) goto end; if (nocert == 0) { s_key = load_key(bio_err, s_key_file, s_key_format, 0, pass, e, "server certificate private key file"); if (!s_key) { ERR_print_errors(bio_err); goto end; } s_cert = load_cert(bio_err, s_cert_file, s_cert_format, NULL, e, "server certificate file"); if (!s_cert) { ERR_print_errors(bio_err); goto end; } if (s_chain_file) { s_chain = load_certs(bio_err, s_chain_file, FORMAT_PEM, NULL, e, "server certificate chain"); if (!s_chain) goto end; } #ifndef OPENSSL_NO_TLSEXT if (tlsextcbp.servername) { s_key2 = load_key(bio_err, s_key_file2, s_key_format, 0, pass, e, "second server certificate private key file"); if (!s_key2) { ERR_print_errors(bio_err); goto end; } s_cert2 = load_cert(bio_err, s_cert_file2, s_cert_format, NULL, e, "second server certificate file"); if (!s_cert2) { ERR_print_errors(bio_err); goto end; } } #endif /* OPENSSL_NO_TLSEXT */ } #if !defined(OPENSSL_NO_TLSEXT) # if !defined(OPENSSL_NO_NEXTPROTONEG) if (next_proto_neg_in) { unsigned short len; next_proto.data = next_protos_parse(&len, next_proto_neg_in); if (next_proto.data == NULL) goto end; next_proto.len = len; } else { next_proto.data = NULL; } # endif alpn_ctx.data = NULL; if (alpn_in) { unsigned short len; alpn_ctx.data = next_protos_parse(&len, alpn_in); if (alpn_ctx.data == NULL) goto end; alpn_ctx.len = len; } #endif if (crl_file) { X509_CRL *crl; crl = load_crl(crl_file, crl_format); if (!crl) { BIO_puts(bio_err, "Error loading CRL\n"); ERR_print_errors(bio_err); goto end; } crls = sk_X509_CRL_new_null(); if (!crls || !sk_X509_CRL_push(crls, crl)) { BIO_puts(bio_err, "Error adding CRL\n"); ERR_print_errors(bio_err); X509_CRL_free(crl); goto end; } } if (s_dcert_file) { if (s_dkey_file == NULL) s_dkey_file = s_dcert_file; s_dkey = load_key(bio_err, s_dkey_file, s_dkey_format, 0, dpass, e, "second certificate private key file"); if (!s_dkey) { ERR_print_errors(bio_err); goto end; } s_dcert = load_cert(bio_err, s_dcert_file, s_dcert_format, NULL, e, "second server certificate file"); if (!s_dcert) { ERR_print_errors(bio_err); goto end; } if (s_dchain_file) { s_dchain = load_certs(bio_err, s_dchain_file, FORMAT_PEM, NULL, e, "second server certificate chain"); if (!s_dchain) goto end; } } if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL && !RAND_status()) { BIO_printf(bio_err, "warning, not much extra random data, consider using the -rand option\n"); } if (inrand != NULL) BIO_printf(bio_err, "%ld semi-random bytes loaded\n", app_RAND_load_files(inrand)); if (bio_s_out == NULL) { if (s_quiet && !s_debug) { bio_s_out = BIO_new(BIO_s_null()); if (s_msg && !bio_s_msg) bio_s_msg = BIO_new_fp(stdout, BIO_NOCLOSE); } else { if (bio_s_out == NULL) bio_s_out = BIO_new_fp(stdout, BIO_NOCLOSE); } } #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) if (nocert) #endif { s_cert_file = NULL; s_key_file = NULL; s_dcert_file = NULL; s_dkey_file = NULL; #ifndef OPENSSL_NO_TLSEXT s_cert_file2 = NULL; s_key_file2 = NULL; #endif } ctx = SSL_CTX_new(meth); if (ctx == NULL) { ERR_print_errors(bio_err); goto end; } if (session_id_prefix) { if (strlen(session_id_prefix) >= 32) BIO_printf(bio_err, "warning: id_prefix is too long, only one new session will be possible\n"); else if (strlen(session_id_prefix) >= 16) BIO_printf(bio_err, "warning: id_prefix is too long if you use SSLv2\n"); if (!SSL_CTX_set_generate_session_id(ctx, generate_session_id)) { BIO_printf(bio_err, "error setting 'id_prefix'\n"); ERR_print_errors(bio_err); goto end; } BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); } SSL_CTX_set_quiet_shutdown(ctx, 1); if (hack) SSL_CTX_set_options(ctx, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); if (exc) ssl_ctx_set_excert(ctx, exc); if (state) SSL_CTX_set_info_callback(ctx, apps_ssl_info_callback); if (no_cache) SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); else if (ext_cache) init_session_cache_ctx(ctx); else SSL_CTX_sess_set_cache_size(ctx, 128); #ifndef OPENSSL_NO_SRTP if (srtp_profiles != NULL) SSL_CTX_set_tlsext_use_srtp(ctx, srtp_profiles); #endif #if 0 if (cipher == NULL) cipher = getenv("SSL_CIPHER"); #endif #if 0 if (s_cert_file == NULL) { BIO_printf(bio_err, "You must specify a certificate file for the server to use\n"); goto end; } #endif if ((!SSL_CTX_load_verify_locations(ctx, CAfile, CApath)) || (!SSL_CTX_set_default_verify_paths(ctx))) { /* BIO_printf(bio_err,"X509_load_verify_locations\n"); */ ERR_print_errors(bio_err); /* goto end; */ } if (vpm) SSL_CTX_set1_param(ctx, vpm); ssl_ctx_add_crls(ctx, crls, 0); if (!args_ssl_call(ctx, bio_err, cctx, ssl_args, no_ecdhe, no_jpake)) goto end; if (!ssl_load_stores(ctx, vfyCApath, vfyCAfile, chCApath, chCAfile, crls, crl_download)) { BIO_printf(bio_err, "Error loading store locations\n"); ERR_print_errors(bio_err); goto end; } #ifndef OPENSSL_NO_TLSEXT if (s_cert2) { ctx2 = SSL_CTX_new(meth); if (ctx2 == NULL) { ERR_print_errors(bio_err); goto end; } } if (ctx2) { BIO_printf(bio_s_out, "Setting secondary ctx parameters\n"); if (session_id_prefix) { if (strlen(session_id_prefix) >= 32) BIO_printf(bio_err, "warning: id_prefix is too long, only one new session will be possible\n"); else if (strlen(session_id_prefix) >= 16) BIO_printf(bio_err, "warning: id_prefix is too long if you use SSLv2\n"); if (!SSL_CTX_set_generate_session_id(ctx2, generate_session_id)) { BIO_printf(bio_err, "error setting 'id_prefix'\n"); ERR_print_errors(bio_err); goto end; } BIO_printf(bio_err, "id_prefix '%s' set.\n", session_id_prefix); } SSL_CTX_set_quiet_shutdown(ctx2, 1); if (hack) SSL_CTX_set_options(ctx2, SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG); if (exc) ssl_ctx_set_excert(ctx2, exc); if (state) SSL_CTX_set_info_callback(ctx2, apps_ssl_info_callback); if (no_cache) SSL_CTX_set_session_cache_mode(ctx2, SSL_SESS_CACHE_OFF); else if (ext_cache) init_session_cache_ctx(ctx2); else SSL_CTX_sess_set_cache_size(ctx2, 128); if ((!SSL_CTX_load_verify_locations(ctx2, CAfile, CApath)) || (!SSL_CTX_set_default_verify_paths(ctx2))) { ERR_print_errors(bio_err); } if (vpm) SSL_CTX_set1_param(ctx2, vpm); ssl_ctx_add_crls(ctx2, crls, 0); if (!args_ssl_call(ctx2, bio_err, cctx, ssl_args, no_ecdhe, no_jpake)) goto end; } # ifndef OPENSSL_NO_NEXTPROTONEG if (next_proto.data) SSL_CTX_set_next_protos_advertised_cb(ctx, next_proto_cb, &next_proto); # endif if (alpn_ctx.data) SSL_CTX_set_alpn_select_cb(ctx, alpn_cb, &alpn_ctx); #endif #ifndef OPENSSL_NO_DH if (!no_dhe) { DH *dh = NULL; if (dhfile) dh = load_dh_param(dhfile); else if (s_cert_file) dh = load_dh_param(s_cert_file); if (dh != NULL) { BIO_printf(bio_s_out, "Setting temp DH parameters\n"); } else { BIO_printf(bio_s_out, "Using default temp DH parameters\n"); dh = get_dh2048(); if (dh == NULL) { ERR_print_errors(bio_err); goto end; } } (void)BIO_flush(bio_s_out); SSL_CTX_set_tmp_dh(ctx, dh); # ifndef OPENSSL_NO_TLSEXT if (ctx2) { if (!dhfile) { DH *dh2 = load_dh_param(s_cert_file2); if (dh2 != NULL) { BIO_printf(bio_s_out, "Setting temp DH parameters\n"); (void)BIO_flush(bio_s_out); DH_free(dh); dh = dh2; } } SSL_CTX_set_tmp_dh(ctx2, dh); } # endif DH_free(dh); } #endif if (!set_cert_key_stuff(ctx, s_cert, s_key, s_chain, build_chain)) goto end; #ifndef OPENSSL_NO_TLSEXT if (s_serverinfo_file != NULL && !SSL_CTX_use_serverinfo_file(ctx, s_serverinfo_file)) { ERR_print_errors(bio_err); goto end; } #endif #ifndef OPENSSL_NO_TLSEXT if (ctx2 && !set_cert_key_stuff(ctx2, s_cert2, s_key2, NULL, build_chain)) goto end; #endif if (s_dcert != NULL) { if (!set_cert_key_stuff(ctx, s_dcert, s_dkey, s_dchain, build_chain)) goto end; } #ifndef OPENSSL_NO_RSA # if 1 if (!no_tmp_rsa) { SSL_CTX_set_tmp_rsa_callback(ctx, tmp_rsa_cb); # ifndef OPENSSL_NO_TLSEXT if (ctx2) SSL_CTX_set_tmp_rsa_callback(ctx2, tmp_rsa_cb); # endif } # else if (!no_tmp_rsa && SSL_CTX_need_tmp_RSA(ctx)) { RSA *rsa; BIO_printf(bio_s_out, "Generating temp (512 bit) RSA key..."); BIO_flush(bio_s_out); rsa = RSA_generate_key(512, RSA_F4, NULL); if (!SSL_CTX_set_tmp_rsa(ctx, rsa)) { ERR_print_errors(bio_err); goto end; } # ifndef OPENSSL_NO_TLSEXT if (ctx2) { if (!SSL_CTX_set_tmp_rsa(ctx2, rsa)) { ERR_print_errors(bio_err); goto end; } } # endif RSA_free(rsa); BIO_printf(bio_s_out, "\n"); } # endif #endif #ifndef OPENSSL_NO_PSK # ifdef OPENSSL_NO_JPAKE if (psk_key != NULL) # else if (psk_key != NULL || jpake_secret) # endif { if (s_debug) BIO_printf(bio_s_out, "PSK key given or JPAKE in use, setting server callback\n"); SSL_CTX_set_psk_server_callback(ctx, psk_server_cb); } if (!SSL_CTX_use_psk_identity_hint(ctx, psk_identity_hint)) { BIO_printf(bio_err, "error setting PSK identity hint to context\n"); ERR_print_errors(bio_err); goto end; } #endif SSL_CTX_set_verify(ctx, s_server_verify, verify_callback); SSL_CTX_set_session_id_context(ctx, (void *)&s_server_session_id_context, sizeof s_server_session_id_context); /* Set DTLS cookie generation and verification callbacks */ SSL_CTX_set_cookie_generate_cb(ctx, generate_cookie_callback); SSL_CTX_set_cookie_verify_cb(ctx, verify_cookie_callback); #ifndef OPENSSL_NO_TLSEXT if (ctx2) { SSL_CTX_set_verify(ctx2, s_server_verify, verify_callback); SSL_CTX_set_session_id_context(ctx2, (void *)&s_server_session_id_context, sizeof s_server_session_id_context); tlsextcbp.biodebug = bio_s_out; SSL_CTX_set_tlsext_servername_callback(ctx2, ssl_servername_cb); SSL_CTX_set_tlsext_servername_arg(ctx2, &tlsextcbp); SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb); SSL_CTX_set_tlsext_servername_arg(ctx, &tlsextcbp); } #endif #ifndef OPENSSL_NO_SRP if (srp_verifier_file != NULL) { srp_callback_parm.vb = SRP_VBASE_new(srpuserseed); srp_callback_parm.user = NULL; srp_callback_parm.login = NULL; if ((ret = SRP_VBASE_init(srp_callback_parm.vb, srp_verifier_file)) != SRP_NO_ERROR) { BIO_printf(bio_err, "Cannot initialize SRP verifier file \"%s\":ret=%d\n", srp_verifier_file, ret); goto end; } SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, verify_callback); SSL_CTX_set_srp_cb_arg(ctx, &srp_callback_parm); SSL_CTX_set_srp_username_callback(ctx, ssl_srp_server_param_cb); } else #endif if (CAfile != NULL) { SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CAfile)); #ifndef OPENSSL_NO_TLSEXT if (ctx2) SSL_CTX_set_client_CA_list(ctx2, SSL_load_client_CA_file(CAfile)); #endif } BIO_printf(bio_s_out, "ACCEPT\n"); (void)BIO_flush(bio_s_out); if (rev) do_server(port, socket_type, &accept_socket, rev_body, context, naccept); else if (www) do_server(port, socket_type, &accept_socket, www_body, context, naccept); else do_server(port, socket_type, &accept_socket, sv_body, context, naccept); print_stats(bio_s_out, ctx); ret = 0; end: if (ctx != NULL) SSL_CTX_free(ctx); if (s_cert) X509_free(s_cert); if (crls) sk_X509_CRL_pop_free(crls, X509_CRL_free); if (s_dcert) X509_free(s_dcert); if (s_key) EVP_PKEY_free(s_key); if (s_dkey) EVP_PKEY_free(s_dkey); if (s_chain) sk_X509_pop_free(s_chain, X509_free); if (s_dchain) sk_X509_pop_free(s_dchain, X509_free); if (pass) OPENSSL_free(pass); if (dpass) OPENSSL_free(dpass); if (vpm) X509_VERIFY_PARAM_free(vpm); free_sessions(); #ifndef OPENSSL_NO_TLSEXT if (tlscstatp.host) OPENSSL_free(tlscstatp.host); if (tlscstatp.port) OPENSSL_free(tlscstatp.port); if (tlscstatp.path) OPENSSL_free(tlscstatp.path); if (ctx2 != NULL) SSL_CTX_free(ctx2); if (s_cert2) X509_free(s_cert2); if (s_key2) EVP_PKEY_free(s_key2); if (serverinfo_in != NULL) BIO_free(serverinfo_in); # ifndef OPENSSL_NO_NEXTPROTONEG if (next_proto.data) OPENSSL_free(next_proto.data); # endif if (alpn_ctx.data) OPENSSL_free(alpn_ctx.data); #endif ssl_excert_free(exc); if (ssl_args) sk_OPENSSL_STRING_free(ssl_args); if (cctx) SSL_CONF_CTX_free(cctx); #ifndef OPENSSL_NO_JPAKE if (jpake_secret && psk_key) OPENSSL_free(psk_key); #endif if (bio_s_out != NULL) { BIO_free(bio_s_out); bio_s_out = NULL; } if (bio_s_msg != NULL) { BIO_free(bio_s_msg); bio_s_msg = NULL; } apps_shutdown(); OPENSSL_EXIT(ret); } static void print_stats(BIO *bio, SSL_CTX *ssl_ctx) { BIO_printf(bio, "%4ld items in the session cache\n", SSL_CTX_sess_number(ssl_ctx)); BIO_printf(bio, "%4ld client connects (SSL_connect())\n", SSL_CTX_sess_connect(ssl_ctx)); BIO_printf(bio, "%4ld client renegotiates (SSL_connect())\n", SSL_CTX_sess_connect_renegotiate(ssl_ctx)); BIO_printf(bio, "%4ld client connects that finished\n", SSL_CTX_sess_connect_good(ssl_ctx)); BIO_printf(bio, "%4ld server accepts (SSL_accept())\n", SSL_CTX_sess_accept(ssl_ctx)); BIO_printf(bio, "%4ld server renegotiates (SSL_accept())\n", SSL_CTX_sess_accept_renegotiate(ssl_ctx)); BIO_printf(bio, "%4ld server accepts that finished\n", SSL_CTX_sess_accept_good(ssl_ctx)); BIO_printf(bio, "%4ld session cache hits\n", SSL_CTX_sess_hits(ssl_ctx)); BIO_printf(bio, "%4ld session cache misses\n", SSL_CTX_sess_misses(ssl_ctx)); BIO_printf(bio, "%4ld session cache timeouts\n", SSL_CTX_sess_timeouts(ssl_ctx)); BIO_printf(bio, "%4ld callback cache hits\n", SSL_CTX_sess_cb_hits(ssl_ctx)); BIO_printf(bio, "%4ld cache full overflows (%ld allowed)\n", SSL_CTX_sess_cache_full(ssl_ctx), SSL_CTX_sess_get_cache_size(ssl_ctx)); } static int sv_body(char *hostname, int s, int stype, unsigned char *context) { char *buf = NULL; fd_set readfds; int ret = 1, width; int k, i; unsigned long l; SSL *con = NULL; BIO *sbio; #ifndef OPENSSL_NO_KRB5 KSSL_CTX *kctx; #endif struct timeval timeout; #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS_R5) struct timeval tv; #else struct timeval *timeoutp; #endif if ((buf = OPENSSL_malloc(bufsize)) == NULL) { BIO_printf(bio_err, "out of memory\n"); goto err; } #ifdef FIONBIO if (s_nbio) { unsigned long sl = 1; if (!s_quiet) BIO_printf(bio_err, "turning on non blocking io\n"); if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0) ERR_print_errors(bio_err); } #endif if (con == NULL) { con = SSL_new(ctx); #ifndef OPENSSL_NO_TLSEXT if (s_tlsextdebug) { SSL_set_tlsext_debug_callback(con, tlsext_cb); SSL_set_tlsext_debug_arg(con, bio_s_out); } if (s_tlsextstatus) { SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb); tlscstatp.err = bio_err; SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp); } #endif #ifndef OPENSSL_NO_KRB5 if ((kctx = kssl_ctx_new()) != NULL) { SSL_set0_kssl_ctx(con, kctx); kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC); kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB); } #endif /* OPENSSL_NO_KRB5 */ if (context) SSL_set_session_id_context(con, context, strlen((char *)context)); } SSL_clear(con); #if 0 # ifdef TLSEXT_TYPE_opaque_prf_input SSL_set_tlsext_opaque_prf_input(con, "Test server", 11); # endif #endif if (stype == SOCK_DGRAM) { sbio = BIO_new_dgram(s, BIO_NOCLOSE); if (enable_timeouts) { timeout.tv_sec = 0; timeout.tv_usec = DGRAM_RCV_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); timeout.tv_sec = 0; timeout.tv_usec = DGRAM_SND_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); } if (socket_mtu) { if (socket_mtu < DTLS_get_link_min_mtu(con)) { BIO_printf(bio_err, "MTU too small. Must be at least %ld\n", DTLS_get_link_min_mtu(con)); ret = -1; BIO_free(sbio); goto err; } SSL_set_options(con, SSL_OP_NO_QUERY_MTU); if (!DTLS_set_link_mtu(con, socket_mtu)) { BIO_printf(bio_err, "Failed to set MTU\n"); ret = -1; BIO_free(sbio); goto err; } } else /* want to do MTU discovery */ BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); /* turn on cookie exchange */ SSL_set_options(con, SSL_OP_COOKIE_EXCHANGE); } else sbio = BIO_new_socket(s, BIO_NOCLOSE); if (s_nbio_test) { BIO *test; test = BIO_new(BIO_f_nbio_test()); sbio = BIO_push(test, sbio); } #ifndef OPENSSL_NO_JPAKE if (jpake_secret) jpake_server_auth(bio_s_out, sbio, jpake_secret); #endif SSL_set_bio(con, sbio, sbio); SSL_set_accept_state(con); /* SSL_set_fd(con,s); */ if (s_debug) { SSL_set_debug(con, 1); BIO_set_callback(SSL_get_rbio(con), bio_dump_callback); BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); } if (s_msg) { #ifndef OPENSSL_NO_SSL_TRACE if (s_msg == 2) SSL_set_msg_callback(con, SSL_trace); else #endif SSL_set_msg_callback(con, msg_cb); SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); } #ifndef OPENSSL_NO_TLSEXT if (s_tlsextdebug) { SSL_set_tlsext_debug_callback(con, tlsext_cb); SSL_set_tlsext_debug_arg(con, bio_s_out); } #endif width = s + 1; for (;;) { int read_from_terminal; int read_from_sslcon; read_from_terminal = 0; read_from_sslcon = SSL_pending(con); if (!read_from_sslcon) { FD_ZERO(&readfds); #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) && !defined(OPENSSL_SYS_BEOS_R5) openssl_fdset(fileno(stdin), &readfds); #endif openssl_fdset(s, &readfds); /* * Note: under VMS with SOCKETSHR the second parameter is * currently of type (int *) whereas under other systems it is * (void *) if you don't have a cast it will choke the compiler: * if you do have a cast then you can either go for (int *) or * (void *). */ #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) /* * Under DOS (non-djgpp) and Windows we can't select on stdin: * only on sockets. As a workaround we timeout the select every * second and check for any keypress. In a proper Windows * application we wouldn't do this because it is inefficient. */ tv.tv_sec = 1; tv.tv_usec = 0; i = select(width, (void *)&readfds, NULL, NULL, &tv); if ((i < 0) || (!i && !_kbhit())) continue; if (_kbhit()) read_from_terminal = 1; #elif defined(OPENSSL_SYS_BEOS_R5) /* Under BeOS-R5 the situation is similar to DOS */ tv.tv_sec = 1; tv.tv_usec = 0; (void)fcntl(fileno(stdin), F_SETFL, O_NONBLOCK); i = select(width, (void *)&readfds, NULL, NULL, &tv); if ((i < 0) || (!i && read(fileno(stdin), buf, 0) < 0)) continue; if (read(fileno(stdin), buf, 0) >= 0) read_from_terminal = 1; (void)fcntl(fileno(stdin), F_SETFL, 0); #else if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_get_timeout(con, &timeout)) timeoutp = &timeout; else timeoutp = NULL; i = select(width, (void *)&readfds, NULL, NULL, timeoutp); if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) { BIO_printf(bio_err, "TIMEOUT occured\n"); } if (i <= 0) continue; if (FD_ISSET(fileno(stdin), &readfds)) read_from_terminal = 1; #endif if (FD_ISSET(s, &readfds)) read_from_sslcon = 1; } if (read_from_terminal) { if (s_crlf) { int j, lf_num; i = raw_read_stdin(buf, bufsize / 2); lf_num = 0; /* both loops are skipped when i <= 0 */ for (j = 0; j < i; j++) if (buf[j] == '\n') lf_num++; for (j = i - 1; j >= 0; j--) { buf[j + lf_num] = buf[j]; if (buf[j] == '\n') { lf_num--; i++; buf[j + lf_num] = '\r'; } } assert(lf_num == 0); } else i = raw_read_stdin(buf, bufsize); if (!s_quiet && !s_brief) { if ((i <= 0) || (buf[0] == 'Q')) { BIO_printf(bio_s_out, "DONE\n"); SHUTDOWN(s); close_accept_socket(); ret = -11; goto err; } if ((i <= 0) || (buf[0] == 'q')) { BIO_printf(bio_s_out, "DONE\n"); if (SSL_version(con) != DTLS1_VERSION) SHUTDOWN(s); /* * close_accept_socket(); ret= -11; */ goto err; } #ifndef OPENSSL_NO_HEARTBEATS if ((buf[0] == 'B') && ((buf[1] == '\n') || (buf[1] == '\r'))) { BIO_printf(bio_err, "HEARTBEATING\n"); SSL_heartbeat(con); i = 0; continue; } #endif if ((buf[0] == 'r') && ((buf[1] == '\n') || (buf[1] == '\r'))) { SSL_renegotiate(con); i = SSL_do_handshake(con); printf("SSL_do_handshake -> %d\n", i); i = 0; /* 13; */ continue; /* * strcpy(buf,"server side RE-NEGOTIATE\n"); */ } if ((buf[0] == 'R') && ((buf[1] == '\n') || (buf[1] == '\r'))) { SSL_set_verify(con, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL); SSL_renegotiate(con); i = SSL_do_handshake(con); printf("SSL_do_handshake -> %d\n", i); i = 0; /* 13; */ continue; /* * strcpy(buf,"server side RE-NEGOTIATE asking for client * cert\n"); */ } if (buf[0] == 'P') { static const char *str = "Lets print some clear text\n"; BIO_write(SSL_get_wbio(con), str, strlen(str)); } if (buf[0] == 'S') { print_stats(bio_s_out, SSL_get_SSL_CTX(con)); } } #ifdef CHARSET_EBCDIC ebcdic2ascii(buf, buf, i); #endif l = k = 0; for (;;) { /* should do a select for the write */ #ifdef RENEG { static count = 0; if (++count == 100) { count = 0; SSL_renegotiate(con); } } #endif k = SSL_write(con, &(buf[l]), (unsigned int)i); #ifndef OPENSSL_NO_SRP while (SSL_get_error(con, k) == SSL_ERROR_WANT_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP renego during write\n"); + SRP_user_pwd_free(srp_callback_parm.user); srp_callback_parm.user = - SRP_VBASE_get_by_user(srp_callback_parm.vb, - srp_callback_parm.login); + SRP_VBASE_get1_by_user(srp_callback_parm.vb, + srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); k = SSL_write(con, &(buf[l]), (unsigned int)i); } #endif switch (SSL_get_error(con, k)) { case SSL_ERROR_NONE: break; case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_X509_LOOKUP: BIO_printf(bio_s_out, "Write BLOCK\n"); break; case SSL_ERROR_SYSCALL: case SSL_ERROR_SSL: BIO_printf(bio_s_out, "ERROR\n"); ERR_print_errors(bio_err); ret = 1; goto err; /* break; */ case SSL_ERROR_ZERO_RETURN: BIO_printf(bio_s_out, "DONE\n"); ret = 1; goto err; } if (k > 0) { l += k; i -= k; } if (i <= 0) break; } } if (read_from_sslcon) { if (!SSL_is_init_finished(con)) { i = init_ssl_connection(con); if (i < 0) { ret = 0; goto err; } else if (i == 0) { ret = 1; goto err; } } else { again: i = SSL_read(con, (char *)buf, bufsize); #ifndef OPENSSL_NO_SRP while (SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP renego during read\n"); + SRP_user_pwd_free(srp_callback_parm.user); srp_callback_parm.user = - SRP_VBASE_get_by_user(srp_callback_parm.vb, - srp_callback_parm.login); + SRP_VBASE_get1_by_user(srp_callback_parm.vb, + srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); i = SSL_read(con, (char *)buf, bufsize); } #endif switch (SSL_get_error(con, i)) { case SSL_ERROR_NONE: #ifdef CHARSET_EBCDIC ascii2ebcdic(buf, buf, i); #endif raw_write_stdout(buf, (unsigned int)i); if (SSL_pending(con)) goto again; break; case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_READ: BIO_printf(bio_s_out, "Read BLOCK\n"); break; case SSL_ERROR_SYSCALL: case SSL_ERROR_SSL: BIO_printf(bio_s_out, "ERROR\n"); ERR_print_errors(bio_err); ret = 1; goto err; case SSL_ERROR_ZERO_RETURN: BIO_printf(bio_s_out, "DONE\n"); ret = 1; goto err; } } } } err: if (con != NULL) { BIO_printf(bio_s_out, "shutting down SSL\n"); #if 1 SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); #else SSL_shutdown(con); #endif SSL_free(con); } BIO_printf(bio_s_out, "CONNECTION CLOSED\n"); if (buf != NULL) { OPENSSL_cleanse(buf, bufsize); OPENSSL_free(buf); } if (ret >= 0) BIO_printf(bio_s_out, "ACCEPT\n"); return (ret); } static void close_accept_socket(void) { BIO_printf(bio_err, "shutdown accept socket\n"); if (accept_socket >= 0) { SHUTDOWN2(accept_socket); } } static int init_ssl_connection(SSL *con) { int i; const char *str; X509 *peer; long verify_error; MS_STATIC char buf[BUFSIZ]; #ifndef OPENSSL_NO_KRB5 char *client_princ; #endif #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) const unsigned char *next_proto_neg; unsigned next_proto_neg_len; #endif unsigned char *exportedkeymat; i = SSL_accept(con); #ifdef CERT_CB_TEST_RETRY { while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP && SSL_state(con) == SSL3_ST_SR_CLNT_HELLO_C) { fprintf(stderr, "LOOKUP from certificate callback during accept\n"); i = SSL_accept(con); } } #endif #ifndef OPENSSL_NO_SRP while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP during accept %s\n", srp_callback_parm.login); + SRP_user_pwd_free(srp_callback_parm.user); srp_callback_parm.user = - SRP_VBASE_get_by_user(srp_callback_parm.vb, - srp_callback_parm.login); + SRP_VBASE_get1_by_user(srp_callback_parm.vb, + srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); i = SSL_accept(con); } #endif if (i <= 0) { if (BIO_sock_should_retry(i)) { BIO_printf(bio_s_out, "DELAY\n"); return (1); } BIO_printf(bio_err, "ERROR\n"); verify_error = SSL_get_verify_result(con); if (verify_error != X509_V_OK) { BIO_printf(bio_err, "verify error:%s\n", X509_verify_cert_error_string(verify_error)); } /* Always print any error messages */ ERR_print_errors(bio_err); return (0); } if (s_brief) print_ssl_summary(bio_err, con); PEM_write_bio_SSL_SESSION(bio_s_out, SSL_get_session(con)); peer = SSL_get_peer_certificate(con); if (peer != NULL) { BIO_printf(bio_s_out, "Client certificate\n"); PEM_write_bio_X509(bio_s_out, peer); X509_NAME_oneline(X509_get_subject_name(peer), buf, sizeof buf); BIO_printf(bio_s_out, "subject=%s\n", buf); X509_NAME_oneline(X509_get_issuer_name(peer), buf, sizeof buf); BIO_printf(bio_s_out, "issuer=%s\n", buf); X509_free(peer); } if (SSL_get_shared_ciphers(con, buf, sizeof buf) != NULL) BIO_printf(bio_s_out, "Shared ciphers:%s\n", buf); str = SSL_CIPHER_get_name(SSL_get_current_cipher(con)); ssl_print_sigalgs(bio_s_out, con); #ifndef OPENSSL_NO_EC ssl_print_point_formats(bio_s_out, con); ssl_print_curves(bio_s_out, con, 0); #endif BIO_printf(bio_s_out, "CIPHER is %s\n", (str != NULL) ? str : "(NONE)"); #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) SSL_get0_next_proto_negotiated(con, &next_proto_neg, &next_proto_neg_len); if (next_proto_neg) { BIO_printf(bio_s_out, "NEXTPROTO is "); BIO_write(bio_s_out, next_proto_neg, next_proto_neg_len); BIO_printf(bio_s_out, "\n"); } #endif #ifndef OPENSSL_NO_SRTP { SRTP_PROTECTION_PROFILE *srtp_profile = SSL_get_selected_srtp_profile(con); if (srtp_profile) BIO_printf(bio_s_out, "SRTP Extension negotiated, profile=%s\n", srtp_profile->name); } #endif if (SSL_cache_hit(con)) BIO_printf(bio_s_out, "Reused session-id\n"); if (SSL_ctrl(con, SSL_CTRL_GET_FLAGS, 0, NULL) & TLS1_FLAGS_TLS_PADDING_BUG) BIO_printf(bio_s_out, "Peer has incorrect TLSv1 block padding\n"); #ifndef OPENSSL_NO_KRB5 client_princ = kssl_ctx_get0_client_princ(SSL_get0_kssl_ctx(con)); if (client_princ != NULL) { BIO_printf(bio_s_out, "Kerberos peer principal is %s\n", client_princ); } #endif /* OPENSSL_NO_KRB5 */ BIO_printf(bio_s_out, "Secure Renegotiation IS%s supported\n", SSL_get_secure_renegotiation_support(con) ? "" : " NOT"); if (keymatexportlabel != NULL) { BIO_printf(bio_s_out, "Keying material exporter:\n"); BIO_printf(bio_s_out, " Label: '%s'\n", keymatexportlabel); BIO_printf(bio_s_out, " Length: %i bytes\n", keymatexportlen); exportedkeymat = OPENSSL_malloc(keymatexportlen); if (exportedkeymat != NULL) { if (!SSL_export_keying_material(con, exportedkeymat, keymatexportlen, keymatexportlabel, strlen(keymatexportlabel), NULL, 0, 0)) { BIO_printf(bio_s_out, " Error\n"); } else { BIO_printf(bio_s_out, " Keying material: "); for (i = 0; i < keymatexportlen; i++) BIO_printf(bio_s_out, "%02X", exportedkeymat[i]); BIO_printf(bio_s_out, "\n"); } OPENSSL_free(exportedkeymat); } } return (1); } #ifndef OPENSSL_NO_DH static DH *load_dh_param(const char *dhfile) { DH *ret = NULL; BIO *bio; if ((bio = BIO_new_file(dhfile, "r")) == NULL) goto err; ret = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); err: if (bio != NULL) BIO_free(bio); return (ret); } #endif #ifndef OPENSSL_NO_KRB5 char *client_princ; #endif #if 0 static int load_CA(SSL_CTX *ctx, char *file) { FILE *in; X509 *x = NULL; if ((in = fopen(file, "r")) == NULL) return (0); for (;;) { if (PEM_read_X509(in, &x, NULL) == NULL) break; SSL_CTX_add_client_CA(ctx, x); } if (x != NULL) X509_free(x); fclose(in); return (1); } #endif static int www_body(char *hostname, int s, int stype, unsigned char *context) { char *buf = NULL; int ret = 1; int i, j, k, dot; SSL *con; const SSL_CIPHER *c; BIO *io, *ssl_bio, *sbio; #ifndef OPENSSL_NO_KRB5 KSSL_CTX *kctx; #endif buf = OPENSSL_malloc(bufsize); if (buf == NULL) return (0); io = BIO_new(BIO_f_buffer()); ssl_bio = BIO_new(BIO_f_ssl()); if ((io == NULL) || (ssl_bio == NULL)) goto err; #ifdef FIONBIO if (s_nbio) { unsigned long sl = 1; if (!s_quiet) BIO_printf(bio_err, "turning on non blocking io\n"); if (BIO_socket_ioctl(s, FIONBIO, &sl) < 0) ERR_print_errors(bio_err); } #endif /* lets make the output buffer a reasonable size */ if (!BIO_set_write_buffer_size(io, bufsize)) goto err; if ((con = SSL_new(ctx)) == NULL) goto err; #ifndef OPENSSL_NO_TLSEXT if (s_tlsextdebug) { SSL_set_tlsext_debug_callback(con, tlsext_cb); SSL_set_tlsext_debug_arg(con, bio_s_out); } #endif #ifndef OPENSSL_NO_KRB5 if ((kctx = kssl_ctx_new()) != NULL) { kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC); kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB); } #endif /* OPENSSL_NO_KRB5 */ if (context) SSL_set_session_id_context(con, context, strlen((char *)context)); sbio = BIO_new_socket(s, BIO_NOCLOSE); if (s_nbio_test) { BIO *test; test = BIO_new(BIO_f_nbio_test()); sbio = BIO_push(test, sbio); } SSL_set_bio(con, sbio, sbio); SSL_set_accept_state(con); /* SSL_set_fd(con,s); */ BIO_set_ssl(ssl_bio, con, BIO_CLOSE); BIO_push(io, ssl_bio); #ifdef CHARSET_EBCDIC io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io); #endif if (s_debug) { SSL_set_debug(con, 1); BIO_set_callback(SSL_get_rbio(con), bio_dump_callback); BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out); } if (s_msg) { #ifndef OPENSSL_NO_SSL_TRACE if (s_msg == 2) SSL_set_msg_callback(con, SSL_trace); else #endif SSL_set_msg_callback(con, msg_cb); SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out); } for (;;) { if (hack) { i = SSL_accept(con); #ifndef OPENSSL_NO_SRP while (i <= 0 && SSL_get_error(con, i) == SSL_ERROR_WANT_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP during accept %s\n", srp_callback_parm.login); + SRP_user_pwd_free(srp_callback_parm.user); srp_callback_parm.user = - SRP_VBASE_get_by_user(srp_callback_parm.vb, - srp_callback_parm.login); + SRP_VBASE_get1_by_user(srp_callback_parm.vb, + srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); i = SSL_accept(con); } #endif switch (SSL_get_error(con, i)) { case SSL_ERROR_NONE: break; case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_X509_LOOKUP: continue; case SSL_ERROR_SYSCALL: case SSL_ERROR_SSL: case SSL_ERROR_ZERO_RETURN: ret = 1; goto err; /* break; */ } SSL_renegotiate(con); SSL_write(con, NULL, 0); } i = BIO_gets(io, buf, bufsize - 1); if (i < 0) { /* error */ if (!BIO_should_retry(io)) { if (!s_quiet) ERR_print_errors(bio_err); goto err; } else { BIO_printf(bio_s_out, "read R BLOCK\n"); #ifndef OPENSSL_NO_SRP if (BIO_should_io_special(io) && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) { BIO_printf(bio_s_out, "LOOKUP renego during read\n"); + SRP_user_pwd_free(srp_callback_parm.user); srp_callback_parm.user = - SRP_VBASE_get_by_user(srp_callback_parm.vb, - srp_callback_parm.login); + SRP_VBASE_get1_by_user(srp_callback_parm.vb, + srp_callback_parm.login); if (srp_callback_parm.user) BIO_printf(bio_s_out, "LOOKUP done %s\n", srp_callback_parm.user->info); else BIO_printf(bio_s_out, "LOOKUP not successful\n"); continue; } #endif #if defined(OPENSSL_SYS_NETWARE) delay(1000); #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__) sleep(1); #endif continue; } } else if (i == 0) { /* end of input */ ret = 1; goto end; } /* else we have data */ if (((www == 1) && (strncmp("GET ", buf, 4) == 0)) || ((www == 2) && (strncmp("GET /stats ", buf, 11) == 0))) { char *p; X509 *peer; STACK_OF(SSL_CIPHER) *sk; static const char *space = " "; BIO_puts(io, "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n"); BIO_puts(io, "\n"); BIO_puts(io, "
\n");
 /*                      BIO_puts(io,SSLeay_version(SSLEAY_VERSION));*/
             BIO_puts(io, "\n");
             for (i = 0; i < local_argc; i++) {
                 BIO_puts(io, local_argv[i]);
                 BIO_write(io, " ", 1);
             }
             BIO_puts(io, "\n");
 
             BIO_printf(io,
                        "Secure Renegotiation IS%s supported\n",
                        SSL_get_secure_renegotiation_support(con) ?
                        "" : " NOT");
 
             /*
              * The following is evil and should not really be done
              */
             BIO_printf(io, "Ciphers supported in s_server binary\n");
             sk = SSL_get_ciphers(con);
             j = sk_SSL_CIPHER_num(sk);
             for (i = 0; i < j; i++) {
                 c = sk_SSL_CIPHER_value(sk, i);
                 BIO_printf(io, "%-11s:%-25s",
                            SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
                 if ((((i + 1) % 2) == 0) && (i + 1 != j))
                     BIO_puts(io, "\n");
             }
             BIO_puts(io, "\n");
             p = SSL_get_shared_ciphers(con, buf, bufsize);
             if (p != NULL) {
                 BIO_printf(io,
                            "---\nCiphers common between both SSL end points:\n");
                 j = i = 0;
                 while (*p) {
                     if (*p == ':') {
                         BIO_write(io, space, 26 - j);
                         i++;
                         j = 0;
                         BIO_write(io, ((i % 3) ? " " : "\n"), 1);
                     } else {
                         BIO_write(io, p, 1);
                         j++;
                     }
                     p++;
                 }
                 BIO_puts(io, "\n");
             }
             ssl_print_sigalgs(io, con);
 #ifndef OPENSSL_NO_EC
             ssl_print_curves(io, con, 0);
 #endif
             BIO_printf(io, (SSL_cache_hit(con)
                             ? "---\nReused, " : "---\nNew, "));
             c = SSL_get_current_cipher(con);
             BIO_printf(io, "%s, Cipher is %s\n",
                        SSL_CIPHER_get_version(c), SSL_CIPHER_get_name(c));
             SSL_SESSION_print(io, SSL_get_session(con));
             BIO_printf(io, "---\n");
             print_stats(io, SSL_get_SSL_CTX(con));
             BIO_printf(io, "---\n");
             peer = SSL_get_peer_certificate(con);
             if (peer != NULL) {
                 BIO_printf(io, "Client certificate\n");
                 X509_print(io, peer);
                 PEM_write_bio_X509(io, peer);
             } else
                 BIO_puts(io, "no client certificate available\n");
             BIO_puts(io, "\r\n\r\n");
             break;
         } else if ((www == 2 || www == 3)
                    && (strncmp("GET /", buf, 5) == 0)) {
             BIO *file;
             char *p, *e;
             static const char *text =
                 "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n";
 
             /* skip the '/' */
             p = &(buf[5]);
 
             dot = 1;
             for (e = p; *e != '\0'; e++) {
                 if (e[0] == ' ')
                     break;
 
                 switch (dot) {
                 case 1:
                     dot = (e[0] == '.') ? 2 : 0;
                     break;
                 case 2:
                     dot = (e[0] == '.') ? 3 : 0;
                     break;
                 case 3:
                     dot = (e[0] == '/') ? -1 : 0;
                     break;
                 }
                 if (dot == 0)
                     dot = (e[0] == '/') ? 1 : 0;
             }
             dot = (dot == 3) || (dot == -1); /* filename contains ".."
                                               * component */
 
             if (*e == '\0') {
                 BIO_puts(io, text);
                 BIO_printf(io, "'%s' is an invalid file name\r\n", p);
                 break;
             }
             *e = '\0';
 
             if (dot) {
                 BIO_puts(io, text);
                 BIO_printf(io, "'%s' contains '..' reference\r\n", p);
                 break;
             }
 
             if (*p == '/') {
                 BIO_puts(io, text);
                 BIO_printf(io, "'%s' is an invalid path\r\n", p);
                 break;
             }
 #if 0
             /* append if a directory lookup */
             if (e[-1] == '/')
                 strcat(p, "index.html");
 #endif
 
             /* if a directory, do the index thang */
             if (app_isdir(p) > 0) {
 #if 0                           /* must check buffer size */
                 strcat(p, "/index.html");
 #else
                 BIO_puts(io, text);
                 BIO_printf(io, "'%s' is a directory\r\n", p);
                 break;
 #endif
             }
 
             if ((file = BIO_new_file(p, "r")) == NULL) {
                 BIO_puts(io, text);
                 BIO_printf(io, "Error opening '%s'\r\n", p);
                 ERR_print_errors(io);
                 break;
             }
 
             if (!s_quiet)
                 BIO_printf(bio_err, "FILE:%s\n", p);
 
             if (www == 2) {
                 i = strlen(p);
                 if (((i > 5) && (strcmp(&(p[i - 5]), ".html") == 0)) ||
                     ((i > 4) && (strcmp(&(p[i - 4]), ".php") == 0)) ||
                     ((i > 4) && (strcmp(&(p[i - 4]), ".htm") == 0)))
                     BIO_puts(io,
                              "HTTP/1.0 200 ok\r\nContent-type: text/html\r\n\r\n");
                 else
                     BIO_puts(io,
                              "HTTP/1.0 200 ok\r\nContent-type: text/plain\r\n\r\n");
             }
             /* send the file */
             for (;;) {
                 i = BIO_read(file, buf, bufsize);
                 if (i <= 0)
                     break;
 
 #ifdef RENEG
                 total_bytes += i;
                 fprintf(stderr, "%d\n", i);
                 if (total_bytes > 3 * 1024) {
                     total_bytes = 0;
                     fprintf(stderr, "RENEGOTIATE\n");
                     SSL_renegotiate(con);
                 }
 #endif
 
                 for (j = 0; j < i;) {
 #ifdef RENEG
                     {
                         static count = 0;
                         if (++count == 13) {
                             SSL_renegotiate(con);
                         }
                     }
 #endif
                     k = BIO_write(io, &(buf[j]), i - j);
                     if (k <= 0) {
                         if (!BIO_should_retry(io))
                             goto write_error;
                         else {
                             BIO_printf(bio_s_out, "rwrite W BLOCK\n");
                         }
                     } else {
                         j += k;
                     }
                 }
             }
  write_error:
             BIO_free(file);
             break;
         }
     }
 
     for (;;) {
         i = (int)BIO_flush(io);
         if (i <= 0) {
             if (!BIO_should_retry(io))
                 break;
         } else
             break;
     }
  end:
 #if 1
     /* make sure we re-use sessions */
     SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
 #else
     /* This kills performance */
     /*
      * SSL_shutdown(con); A shutdown gets sent in the BIO_free_all(io)
      * procession
      */
 #endif
 
  err:
 
     if (ret >= 0)
         BIO_printf(bio_s_out, "ACCEPT\n");
 
     if (buf != NULL)
         OPENSSL_free(buf);
     if (io != NULL)
         BIO_free_all(io);
 /*      if (ssl_bio != NULL) BIO_free(ssl_bio);*/
     return (ret);
 }
 
 static int rev_body(char *hostname, int s, int stype, unsigned char *context)
 {
     char *buf = NULL;
     int i;
     int ret = 1;
     SSL *con;
     BIO *io, *ssl_bio, *sbio;
 #ifndef OPENSSL_NO_KRB5
     KSSL_CTX *kctx;
 #endif
 
     buf = OPENSSL_malloc(bufsize);
     if (buf == NULL)
         return (0);
     io = BIO_new(BIO_f_buffer());
     ssl_bio = BIO_new(BIO_f_ssl());
     if ((io == NULL) || (ssl_bio == NULL))
         goto err;
 
     /* lets make the output buffer a reasonable size */
     if (!BIO_set_write_buffer_size(io, bufsize))
         goto err;
 
     if ((con = SSL_new(ctx)) == NULL)
         goto err;
 #ifndef OPENSSL_NO_TLSEXT
     if (s_tlsextdebug) {
         SSL_set_tlsext_debug_callback(con, tlsext_cb);
         SSL_set_tlsext_debug_arg(con, bio_s_out);
     }
 #endif
 #ifndef OPENSSL_NO_KRB5
     if ((kctx = kssl_ctx_new()) != NULL) {
         kssl_ctx_setstring(kctx, KSSL_SERVICE, KRB5SVC);
         kssl_ctx_setstring(kctx, KSSL_KEYTAB, KRB5KEYTAB);
     }
 #endif                          /* OPENSSL_NO_KRB5 */
     if (context)
         SSL_set_session_id_context(con, context, strlen((char *)context));
 
     sbio = BIO_new_socket(s, BIO_NOCLOSE);
     SSL_set_bio(con, sbio, sbio);
     SSL_set_accept_state(con);
 
     BIO_set_ssl(ssl_bio, con, BIO_CLOSE);
     BIO_push(io, ssl_bio);
 #ifdef CHARSET_EBCDIC
     io = BIO_push(BIO_new(BIO_f_ebcdic_filter()), io);
 #endif
 
     if (s_debug) {
         SSL_set_debug(con, 1);
         BIO_set_callback(SSL_get_rbio(con), bio_dump_callback);
         BIO_set_callback_arg(SSL_get_rbio(con), (char *)bio_s_out);
     }
     if (s_msg) {
 #ifndef OPENSSL_NO_SSL_TRACE
         if (s_msg == 2)
             SSL_set_msg_callback(con, SSL_trace);
         else
 #endif
             SSL_set_msg_callback(con, msg_cb);
         SSL_set_msg_callback_arg(con, bio_s_msg ? bio_s_msg : bio_s_out);
     }
 
     for (;;) {
         i = BIO_do_handshake(io);
         if (i > 0)
             break;
         if (!BIO_should_retry(io)) {
             BIO_puts(bio_err, "CONNECTION FAILURE\n");
             ERR_print_errors(bio_err);
             goto end;
         }
 #ifndef OPENSSL_NO_SRP
         if (BIO_should_io_special(io)
             && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
             BIO_printf(bio_s_out, "LOOKUP renego during accept\n");
+            SRP_user_pwd_free(srp_callback_parm.user);
             srp_callback_parm.user =
-                SRP_VBASE_get_by_user(srp_callback_parm.vb,
-                                      srp_callback_parm.login);
+                SRP_VBASE_get1_by_user(srp_callback_parm.vb,
+                                       srp_callback_parm.login);
             if (srp_callback_parm.user)
                 BIO_printf(bio_s_out, "LOOKUP done %s\n",
                            srp_callback_parm.user->info);
             else
                 BIO_printf(bio_s_out, "LOOKUP not successful\n");
             continue;
         }
 #endif
     }
     BIO_printf(bio_err, "CONNECTION ESTABLISHED\n");
     print_ssl_summary(bio_err, con);
 
     for (;;) {
         i = BIO_gets(io, buf, bufsize - 1);
         if (i < 0) {            /* error */
             if (!BIO_should_retry(io)) {
                 if (!s_quiet)
                     ERR_print_errors(bio_err);
                 goto err;
             } else {
                 BIO_printf(bio_s_out, "read R BLOCK\n");
 #ifndef OPENSSL_NO_SRP
                 if (BIO_should_io_special(io)
                     && BIO_get_retry_reason(io) == BIO_RR_SSL_X509_LOOKUP) {
                     BIO_printf(bio_s_out, "LOOKUP renego during read\n");
+                    SRP_user_pwd_free(srp_callback_parm.user);
                     srp_callback_parm.user =
-                        SRP_VBASE_get_by_user(srp_callback_parm.vb,
-                                              srp_callback_parm.login);
+                        SRP_VBASE_get1_by_user(srp_callback_parm.vb,
+                                               srp_callback_parm.login);
                     if (srp_callback_parm.user)
                         BIO_printf(bio_s_out, "LOOKUP done %s\n",
                                    srp_callback_parm.user->info);
                     else
                         BIO_printf(bio_s_out, "LOOKUP not successful\n");
                     continue;
                 }
 #endif
 #if defined(OPENSSL_SYS_NETWARE)
                 delay(1000);
 #elif !defined(OPENSSL_SYS_MSDOS) && !defined(__DJGPP__)
                 sleep(1);
 #endif
                 continue;
             }
         } else if (i == 0) {    /* end of input */
             ret = 1;
             BIO_printf(bio_err, "CONNECTION CLOSED\n");
             goto end;
         } else {
             char *p = buf + i - 1;
             while (i && (*p == '\n' || *p == '\r')) {
                 p--;
                 i--;
             }
             if (!s_ign_eof && i == 5 && !strncmp(buf, "CLOSE", 5)) {
                 ret = 1;
                 BIO_printf(bio_err, "CONNECTION CLOSED\n");
                 goto end;
             }
             BUF_reverse((unsigned char *)buf, NULL, i);
             buf[i] = '\n';
             BIO_write(io, buf, i + 1);
             for (;;) {
                 i = BIO_flush(io);
                 if (i > 0)
                     break;
                 if (!BIO_should_retry(io))
                     goto end;
             }
         }
     }
  end:
     /* make sure we re-use sessions */
     SSL_set_shutdown(con, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
 
  err:
 
     if (buf != NULL)
         OPENSSL_free(buf);
     if (io != NULL)
         BIO_free_all(io);
     return (ret);
 }
 
 #ifndef OPENSSL_NO_RSA
 static RSA MS_CALLBACK *tmp_rsa_cb(SSL *s, int is_export, int keylength)
 {
     BIGNUM *bn = NULL;
     static RSA *rsa_tmp = NULL;
 
     if (!rsa_tmp && ((bn = BN_new()) == NULL))
         BIO_printf(bio_err, "Allocation error in generating RSA key\n");
     if (!rsa_tmp && bn) {
         if (!s_quiet) {
             BIO_printf(bio_err, "Generating temp (%d bit) RSA key...",
                        keylength);
             (void)BIO_flush(bio_err);
         }
         if (!BN_set_word(bn, RSA_F4) || ((rsa_tmp = RSA_new()) == NULL) ||
             !RSA_generate_key_ex(rsa_tmp, keylength, bn, NULL)) {
             if (rsa_tmp)
                 RSA_free(rsa_tmp);
             rsa_tmp = NULL;
         }
         if (!s_quiet) {
             BIO_printf(bio_err, "\n");
             (void)BIO_flush(bio_err);
         }
         BN_free(bn);
     }
     return (rsa_tmp);
 }
 #endif
 
 #define MAX_SESSION_ID_ATTEMPTS 10
 static int generate_session_id(const SSL *ssl, unsigned char *id,
                                unsigned int *id_len)
 {
     unsigned int count = 0;
     do {
         if (RAND_pseudo_bytes(id, *id_len) < 0)
             return 0;
         /*
          * Prefix the session_id with the required prefix. NB: If our prefix
          * is too long, clip it - but there will be worse effects anyway, eg.
          * the server could only possibly create 1 session ID (ie. the
          * prefix!) so all future session negotiations will fail due to
          * conflicts.
          */
         memcpy(id, session_id_prefix,
                (strlen(session_id_prefix) < *id_len) ?
                strlen(session_id_prefix) : *id_len);
     }
     while (SSL_has_matching_session_id(ssl, id, *id_len) &&
            (++count < MAX_SESSION_ID_ATTEMPTS));
     if (count >= MAX_SESSION_ID_ATTEMPTS)
         return 0;
     return 1;
 }
 
 /*
  * By default s_server uses an in-memory cache which caches SSL_SESSION
  * structures without any serialisation. This hides some bugs which only
  * become apparent in deployed servers. By implementing a basic external
  * session cache some issues can be debugged using s_server.
  */
 
 typedef struct simple_ssl_session_st {
     unsigned char *id;
     unsigned int idlen;
     unsigned char *der;
     int derlen;
     struct simple_ssl_session_st *next;
 } simple_ssl_session;
 
 static simple_ssl_session *first = NULL;
 
 static int add_session(SSL *ssl, SSL_SESSION *session)
 {
     simple_ssl_session *sess;
     unsigned char *p;
 
     sess = OPENSSL_malloc(sizeof(simple_ssl_session));
     if (!sess) {
         BIO_printf(bio_err, "Out of memory adding session to external cache\n");
         return 0;
     }
 
     SSL_SESSION_get_id(session, &sess->idlen);
     sess->derlen = i2d_SSL_SESSION(session, NULL);
 
     sess->id = BUF_memdup(SSL_SESSION_get_id(session, NULL), sess->idlen);
 
     sess->der = OPENSSL_malloc(sess->derlen);
     if (!sess->id || !sess->der) {
         BIO_printf(bio_err, "Out of memory adding session to external cache\n");
 
         if (sess->id)
             OPENSSL_free(sess->id);
         if (sess->der)
             OPENSSL_free(sess->der);
         OPENSSL_free(sess);
         return 0;
     }
     p = sess->der;
     i2d_SSL_SESSION(session, &p);
 
     sess->next = first;
     first = sess;
     BIO_printf(bio_err, "New session added to external cache\n");
     return 0;
 }
 
 static SSL_SESSION *get_session(SSL *ssl, unsigned char *id, int idlen,
                                 int *do_copy)
 {
     simple_ssl_session *sess;
     *do_copy = 0;
     for (sess = first; sess; sess = sess->next) {
         if (idlen == (int)sess->idlen && !memcmp(sess->id, id, idlen)) {
             const unsigned char *p = sess->der;
             BIO_printf(bio_err, "Lookup session: cache hit\n");
             return d2i_SSL_SESSION(NULL, &p, sess->derlen);
         }
     }
     BIO_printf(bio_err, "Lookup session: cache miss\n");
     return NULL;
 }
 
 static void del_session(SSL_CTX *sctx, SSL_SESSION *session)
 {
     simple_ssl_session *sess, *prev = NULL;
     const unsigned char *id;
     unsigned int idlen;
     id = SSL_SESSION_get_id(session, &idlen);
     for (sess = first; sess; sess = sess->next) {
         if (idlen == sess->idlen && !memcmp(sess->id, id, idlen)) {
             if (prev)
                 prev->next = sess->next;
             else
                 first = sess->next;
             OPENSSL_free(sess->id);
             OPENSSL_free(sess->der);
             OPENSSL_free(sess);
             return;
         }
         prev = sess;
     }
 }
 
 static void init_session_cache_ctx(SSL_CTX *sctx)
 {
     SSL_CTX_set_session_cache_mode(sctx,
                                    SSL_SESS_CACHE_NO_INTERNAL |
                                    SSL_SESS_CACHE_SERVER);
     SSL_CTX_sess_set_new_cb(sctx, add_session);
     SSL_CTX_sess_set_get_cb(sctx, get_session);
     SSL_CTX_sess_set_remove_cb(sctx, del_session);
 }
 
 static void free_sessions(void)
 {
     simple_ssl_session *sess, *tsess;
     for (sess = first; sess;) {
         OPENSSL_free(sess->id);
         OPENSSL_free(sess->der);
         tsess = sess;
         sess = sess->next;
         OPENSSL_free(tsess);
     }
     first = NULL;
 }
Index: vendor-crypto/openssl/dist/config
===================================================================
--- vendor-crypto/openssl/dist/config	(revision 296272)
+++ vendor-crypto/openssl/dist/config	(revision 296273)
@@ -1,986 +1,987 @@
 #!/bin/sh
 #
 # OpenSSL config: determine the operating system and run ./Configure
 #
 # "config -h" for usage information.
 #
 #          this is a merge of minarch and GuessOS from the Apache Group.
 #          Originally written by Tim Hudson .
 
 # Original Apache Group comments on GuessOS
 
 # Simple OS/Platform guesser. Similar to config.guess but
 # much, much smaller. Since it was developed for use with
 # Apache, it follows under Apache's regular licensing
 # with one specific addition: Any changes or additions
 # to this script should be Emailed to the Apache
 # group (apache@apache.org) in general and to
 # Jim Jagielski (jim@jaguNET.com) in specific.
 #
 # Be as similar to the output of config.guess/config.sub
 # as possible.
 
 PREFIX=""
 SUFFIX=""
 TEST="false"
 EXE=""
 
 # pick up any command line args to config
 for i
 do
 case "$i" in 
 -d*) PREFIX="debug-";;
 -t*) TEST="true";;
 -h*) TEST="true"; cat </dev/null` || MACHINE="unknown"
 [ "$RELEASE" ] || RELEASE=`(uname -r) 2>/dev/null` || RELEASE="unknown"
 [ "$SYSTEM" ] || SYSTEM=`(uname -s) 2>/dev/null`  || SYSTEM="unknown"
 [ "$BUILD" ] || VERSION=`(uname -v) 2>/dev/null` || VERSION="unknown"
 
 
 # Now test for ISC and SCO, since it is has a braindamaged uname.
 #
 # We need to work around FreeBSD 1.1.5.1 
 (
 XREL=`uname -X 2>/dev/null | grep "^Release" | awk '{print $3}'`
 if [ "x$XREL" != "x" ]; then
     if [ -f /etc/kconfig ]; then
 	case "$XREL" in
 	    4.0|4.1)
 		    echo "${MACHINE}-whatever-isc4"; exit 0
 		;;
 	esac
     else
 	case "$XREL" in
 	    3.2v4.2)
 		echo "whatever-whatever-sco3"; exit 0
 		;;
 	    3.2v5.0*)
 		echo "whatever-whatever-sco5"; exit 0
 		;;
 	    4.2MP)
 		case "x${VERSION}" in
 		    x2.0*) echo "whatever-whatever-unixware20"; exit 0 ;;
 		    x2.1*) echo "whatever-whatever-unixware21"; exit 0 ;;
 		    x2*)   echo "whatever-whatever-unixware2";  exit 0 ;;
 		esac
 		;;
 	    4.2)
 		echo "whatever-whatever-unixware1"; exit 0
 		;;
 	    5*)
 		case "x${VERSION}" in
 		    # We hardcode i586 in place of ${MACHINE} for the
 		    # following reason. The catch is that even though Pentium
 		    # is minimum requirement for platforms in question,
 		    # ${MACHINE} gets always assigned to i386. Now, problem
 		    # with i386 is that it makes ./config pass 386 to
 		    # ./Configure, which in turn makes make generate
 		    # inefficient SHA-1 (for this moment) code.
 		    x[678]*)  echo "i586-sco-unixware7"; exit 0 ;;
 		esac
 		;;
 	esac
     fi
 fi
 # Now we simply scan though... In most cases, the SYSTEM info is enough
 #
 case "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}" in
     MPE/iX:*)
 	MACHINE=`echo "$MACHINE" | sed -e 's/-/_/g'`
 	echo "parisc-hp-MPE/iX"; exit 0
 	;;
     A/UX:*)
 	echo "m68k-apple-aux3"; exit 0
 	;;
 
     AIX:[3-9]:4:*)
 	echo "${MACHINE}-ibm-aix"; exit 0
 	;;
 
     AIX:*:[5-9]:*)
 	echo "${MACHINE}-ibm-aix"; exit 0
 	;;
 
     AIX:*)
 	echo "${MACHINE}-ibm-aix3"; exit 0
 	;;
 
     BeOS:*:BePC)
     if [ -e /boot/develop/headers/be/bone ]; then
 		echo "beos-x86-bone"; exit 0
 	else
 		echo "beos-x86-r5"; exit 0
 	fi
 	;;
 
     dgux:*)
 	echo "${MACHINE}-dg-dgux"; exit 0
 	;;
 
     HI-UX:*)
 	echo "${MACHINE}-hi-hiux"; exit 0
 	;;
 
     HP-UX:*)
 	HPUXVER=`echo ${RELEASE}|sed -e 's/[^.]*.[0B]*//'`
 	case "$HPUXVER" in
 	    1[0-9].*)	# HPUX 10 and 11 targets are unified
 		echo "${MACHINE}-hp-hpux1x"; exit 0
 		;;
 	    *)
 		echo "${MACHINE}-hp-hpux"; exit 0
 		;;
 	esac
 	;;
 
     IRIX:5.*)
 	echo "mips2-sgi-irix"; exit 0
 	;;
 
     IRIX:6.*)
 	echo "mips3-sgi-irix"; exit 0
 	;;
 
     IRIX64:*)
 	echo "mips4-sgi-irix64"; exit 0
 	;;
 
     Linux:[2-9].*)
 	echo "${MACHINE}-whatever-linux2"; exit 0
 	;;
 
     Linux:1.*)
 	echo "${MACHINE}-whatever-linux1"; exit 0
 	;;
 
     GNU*)
 	echo "hurd-x86"; exit 0;
 	;;
 
     LynxOS:*)
 	echo "${MACHINE}-lynx-lynxos"; exit 0
 	;;
 
     BSD/OS:4.*)  # BSD/OS always says 386
 	echo "i486-whatever-bsdi4"; exit 0
 	;;
 
     BSD/386:*:*:*486*|BSD/OS:*:*:*:*486*)
         case `/sbin/sysctl -n hw.model` in
 	    Pentium*)
                 echo "i586-whatever-bsdi"; exit 0
                 ;;
             *)
                 echo "i386-whatever-bsdi"; exit 0
                 ;;
             esac;
 	;;
 
     BSD/386:*|BSD/OS:*)
 	echo "${MACHINE}-whatever-bsdi"; exit 0
 	;;
 
     FreeBSD:*:*:*386*)
         VERS=`echo ${RELEASE} | sed -e 's/[-(].*//'`
         MACH=`sysctl -n hw.model`
         ARCH='whatever'
         case ${MACH} in
            *386*       ) MACH="i386"     ;;
            *486*       ) MACH="i486"     ;;
            Pentium\ II*) MACH="i686"     ;;
            Pentium*    ) MACH="i586"     ;;
            *           ) MACH="$MACHINE" ;;
         esac
         case ${MACH} in
            i[0-9]86 ) ARCH="pc" ;;
         esac
         echo "${MACH}-${ARCH}-freebsd${VERS}"; exit 0
         ;;
 
     FreeBSD:*)
 	echo "${MACHINE}-whatever-freebsd"; exit 0
 	;;
 
     NetBSD:*:*:*386*)
         echo "`(/usr/sbin/sysctl -n hw.model || /sbin/sysctl -n hw.model) | sed 's,.*\(.\)86-class.*,i\186,'`-whatever-netbsd"; exit 0
 	;;
 
     NetBSD:*)
 	echo "${MACHINE}-whatever-netbsd"; exit 0
 	;;
 
     OpenBSD:*)
 	echo "${MACHINE}-whatever-openbsd"; exit 0
 	;;
 
     OpenUNIX:*)
 	echo "${MACHINE}-unknown-OpenUNIX${VERSION}"; exit 0
 	;;
 
     OSF1:*:*:*alpha*)
 	OSFMAJOR=`echo ${RELEASE}| sed -e 's/^V\([0-9]*\)\..*$/\1/'`
 	case "$OSFMAJOR" in
 	    4|5)
 		echo "${MACHINE}-dec-tru64"; exit 0
 		;;
 	    1|2|3)
 		echo "${MACHINE}-dec-osf"; exit 0
 		;;
 	    *)
 		echo "${MACHINE}-dec-osf"; exit 0
 		;;
 	esac
 	;;
 
     QNX:*)
 	case "$RELEASE" in
 	    4*)
 		echo "${MACHINE}-whatever-qnx4"
 		;;
 	    6*)
 		echo "${MACHINE}-whatever-qnx6"
 		;;
 	    *)
 		echo "${MACHINE}-whatever-qnx"
 		;;
 	esac
 	exit 0
 	;;
 
     Paragon*:*:*:*)
 	echo "i860-intel-osf1"; exit 0
 	;;
 
     Rhapsody:*)
 	echo "ppc-apple-rhapsody"; exit 0
 	;;
 
     Darwin:*)
 	case "$MACHINE" in
 	    Power*)
 		echo "ppc-apple-darwin${VERSION}"
 		;;
 	    *)
 		echo "i686-apple-darwin${VERSION}"
 		;;
 	esac
 	exit 0
 	;;
 
     SunOS:5.*)
 	echo "${MACHINE}-whatever-solaris2"; exit 0
 	;;
 
     SunOS:*)
 	echo "${MACHINE}-sun-sunos4"; exit 0
 	;;
 
     UNIX_System_V:4.*:*)
 	echo "${MACHINE}-whatever-sysv4"; exit 0
 	;;
 
     VOS:*:*:i786)
      echo "i386-stratus-vos"; exit 0
      ;;
 
     VOS:*:*:*)
      echo "hppa1.1-stratus-vos"; exit 0
      ;;
 
     *:4*:R4*:m88k)
 	echo "${MACHINE}-whatever-sysv4"; exit 0
 	;;
 
     DYNIX/ptx:4*:*)
 	echo "${MACHINE}-whatever-sysv4"; exit 0
 	;;
 
     *:4.0:3.0:3[34]?? | *:4.0:3.0:3[34]??,*)
 	echo "i486-ncr-sysv4"; exit 0
 	;;
 
     ULTRIX:*)
 	echo "${MACHINE}-unknown-ultrix"; exit 0
 	;;
 
     SINIX*|ReliantUNIX*)
 	echo "${MACHINE}-siemens-sysv4"; exit 0
 	;;
 
     POSIX-BC*)
 	echo "${MACHINE}-siemens-sysv4"; exit 0   # Here, $MACHINE == "BS2000"
 	;;
 
     machten:*)
        echo "${MACHINE}-tenon-${SYSTEM}"; exit 0;
        ;;
 
     library:*)
 	echo "${MACHINE}-ncr-sysv4"; exit 0
 	;;
 
     ConvexOS:*:11.0:*)
 	echo "${MACHINE}-v11-${SYSTEM}"; exit 0;
 	;;
 
     NEWS-OS:4.*)
 	echo "mips-sony-newsos4"; exit 0;
 	;;
 
     MINGW*)
 	echo "${MACHINE}-whatever-mingw"; exit 0;
 	;;
     CYGWIN*)
 	case "$RELEASE" in
 	    [bB]*|1.0|1.[12].*)
 		echo "${MACHINE}-whatever-cygwin_pre1.3"
 		;;
 	    *)
 		echo "${MACHINE}-whatever-cygwin"
 		;;
 	esac
 	exit 0
 	;;
 
     *"CRAY T3E")
        echo "t3e-cray-unicosmk"; exit 0;
        ;;
 
     *CRAY*)
        echo "j90-cray-unicos"; exit 0;
        ;;
 
     NONSTOP_KERNEL*)
        echo "nsr-tandem-nsk"; exit 0;
        ;;
 
     vxworks*)
        echo "${MACHINE}-whatever-vxworks"; exit 0;
        ;;
 esac
 
 #
 # Ugg. These are all we can determine by what we know about
 # the output of uname. Be more creative:
 #
 
 # Do the Apollo stuff first. Here, we just simply assume
 # that the existance of the /usr/apollo directory is proof
 # enough
 if [ -d /usr/apollo ]; then
     echo "whatever-apollo-whatever"
     exit 0
 fi
 
 # Now NeXT
 ISNEXT=`hostinfo 2>/dev/null`
 case "$ISNEXT" in
     *'NeXT Mach 3.3'*)
 	echo "whatever-next-nextstep3.3"; exit 0
 	;;
     *NeXT*)
 	echo "whatever-next-nextstep"; exit 0
 	;;
 esac
 
 # At this point we gone through all the one's
 # we know of: Punt
 
 echo "${MACHINE}-whatever-${SYSTEM}" 
 exit 0
 ) 2>/dev/null | (
 
 # ---------------------------------------------------------------------------
 # this is where the translation occurs into SSLeay terms
 # ---------------------------------------------------------------------------
 
 # Only set CC if not supplied already
 if [ -z "$CROSS_COMPILE$CC" ]; then
   GCCVER=`sh -c "gcc -dumpversion" 2>/dev/null`
   if [ "$GCCVER" != "" ]; then
     # then strip off whatever prefix egcs prepends the number with...
     # Hopefully, this will work for any future prefixes as well.
     GCCVER=`echo $GCCVER | LC_ALL=C sed 's/^[a-zA-Z]*\-//'`
     # Since gcc 3.1 gcc --version behaviour has changed.  gcc -dumpversion
     # does give us what we want though, so we use that.  We just just the
     # major and minor version numbers.
     # peak single digit before and after first dot, e.g. 2.95.1 gives 29
     GCCVER=`echo $GCCVER | sed 's/\([0-9]\)\.\([0-9]\).*/\1\2/'`
     CC=gcc
   else
     CC=cc
   fi
 fi
 GCCVER=${GCCVER:-0}
 if [ "$SYSTEM" = "HP-UX" ];then
   # By default gcc is a ILP32 compiler (with long long == 64).
   GCC_BITS="32"
   if [ $GCCVER -ge 30 ]; then
     # PA64 support only came in with gcc 3.0.x.
     # We check if the preprocessor symbol __LP64__ is defined...
     if echo "__LP64__" | gcc -v -E -x c - 2>/dev/null | grep "^__LP64__" 2>&1 > /dev/null; then
       : # __LP64__ has slipped through, it therefore is not defined
     else
       GCC_BITS="64"
     fi
   fi
 fi
 if [ "$SYSTEM" = "SunOS" ]; then
   if [ $GCCVER -ge 30 ]; then
     # 64-bit ABI isn't officially supported in gcc 3.0, but it appears
     # to be working, at the very least 'make test' passes...
     if gcc -v -E -x c /dev/null 2>&1 | grep __arch64__ > /dev/null; then
       GCC_ARCH="-m64"
     else
       GCC_ARCH="-m32"
     fi
   fi
   # check for WorkShop C, expected output is "cc: blah-blah C x.x"
   CCVER=`(cc -V 2>&1) 2>/dev/null | \
   	egrep -e '^cc: .* C [0-9]\.[0-9]' | \
 	sed 's/.* C \([0-9]\)\.\([0-9]\).*/\1\2/'`
   CCVER=${CCVER:-0}
   if [ $MACHINE != i86pc -a $CCVER -gt 40 ]; then
     CC=cc	# overrides gcc!!!
     if [ $CCVER -eq 50 ]; then
       echo "WARNING! Detected WorkShop C 5.0. Do make sure you have"
       echo "         patch #107357-01 or later applied."
       sleep 5
     fi
   fi
 fi
 
 if [ "${SYSTEM}-${MACHINE}" = "Linux-alpha" ]; then
   # check for Compaq C, expected output is "blah-blah C Vx.x"
   CCCVER=`(ccc -V 2>&1) 2>/dev/null | \
 	egrep -e '.* C V[0-9]\.[0-9]' | \
 	sed 's/.* C V\([0-9]\)\.\([0-9]\).*/\1\2/'`
   CCCVER=${CCCVER:-0}
   if [ $CCCVER -gt 60 ]; then
     CC=ccc	# overrides gcc!!! well, ccc outperforms inoticeably
 		# only on hash routines and des, otherwise gcc (2.95)
 		# keeps along rather tight...
   fi
 fi
 
 if [ "${SYSTEM}" = "AIX" ]; then	# favor vendor cc over gcc
     (cc) 2>&1 | grep -iv "not found" > /dev/null && CC=cc
 fi
 
 CCVER=${CCVER:-0}
 
 # read the output of the embedded GuessOS 
 read GUESSOS
 
 echo Operating system: $GUESSOS
 
 # now map the output into SSLeay terms ... really should hack into the
 # script above so we end up with values in vars but that would take
 # more time that I want to waste at the moment
 case "$GUESSOS" in
   uClinux*64*)
     OUT=uClinux-dist64
 	;;
   uClinux*)
     OUT=uClinux-dist
 	;;
   mips2-sgi-irix)
 	CPU=`(hinv -t cpu) 2>/dev/null | head -1 | sed 's/^CPU:[^R]*R\([0-9]*\).*/\1/'`
 	CPU=${CPU:-0}
 	if [ $CPU -ge 4000 ]; then
 		options="$options -mips2"
 	fi
 	OUT="irix-$CC"
 	;;
   mips3-sgi-irix)
 	#CPU=`(hinv -t cpu) 2>/dev/null | head -1 | sed 's/^CPU:[^R]*R\([0-9]*\).*/\1/'`
 	#CPU=${CPU:-0}
 	#if [ $CPU -ge 5000 ]; then
 	#	options="$options -mips4"
 	#else
 	#	options="$options -mips3"
 	#fi
 	OUT="irix-mips3-$CC"
 	;;
   mips4-sgi-irix64)
 	echo "WARNING! If you wish to build 64-bit library, then you have to"
 	echo "         invoke './Configure irix64-mips4-$CC' *manually*."
 	if [ "$TEST" = "false" -a -t 1 ]; then
 	  echo "         You have about 5 seconds to press Ctrl-C to abort."
 	  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	fi
         #CPU=`(hinv -t cpu) 2>/dev/null | head -1 | sed 's/^CPU:[^R]*R\([0-9]*\).*/\1/'`
         #CPU=${CPU:-0}
         #if [ $CPU -ge 5000 ]; then
         #        options="$options -mips4"
         #else
         #        options="$options -mips3"
         #fi
 	OUT="irix-mips3-$CC"
 	;;
   ppc-apple-rhapsody) OUT="rhapsody-ppc-cc" ;;
   ppc-apple-darwin*)
 	ISA64=`(sysctl -n hw.optional.64bitops) 2>/dev/null`
 	if [ "$ISA64" = "1" -a -z "$KERNEL_BITS" ]; then
 	    echo "WARNING! If you wish to build 64-bit library, then you have to"
 	    echo "         invoke './Configure darwin64-ppc-cc' *manually*."
 	    if [ "$TEST" = "false" -a -t 1 ]; then
 	      echo "         You have about 5 seconds to press Ctrl-C to abort."
 	      (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	    fi
 	fi
 	if [ "$ISA64" = "1" -a "$KERNEL_BITS" = "64" ]; then
 	    OUT="darwin64-ppc-cc"
 	else
 	    OUT="darwin-ppc-cc"
 	fi ;;
   i?86-apple-darwin*)
 	ISA64=`(sysctl -n hw.optional.x86_64) 2>/dev/null`
 	if [ "$ISA64" = "1" -a -z "$KERNEL_BITS" ]; then
 	    echo "WARNING! If you wish to build 64-bit library, then you have to"
 	    echo "         invoke './Configure darwin64-x86_64-cc' *manually*."
 	    if [ "$TEST" = "false" -a -t 1 ]; then
 	      echo "         You have about 5 seconds to press Ctrl-C to abort."
 	      (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	    fi
 	fi
 	if [ "$ISA64" = "1" -a "$KERNEL_BITS" = "64" ]; then
 	    OUT="darwin64-x86_64-cc"
 	else
 	    OUT="darwin-i386-cc"
 	fi ;;
   armv6+7-*-iphoneos)
 	options="$options -arch%20armv6 -arch%20armv7"
 	OUT="iphoneos-cross" ;;
   *-*-iphoneos)
 	options="$options -arch%20${MACHINE}"
 	OUT="iphoneos-cross" ;;
   alpha-*-linux2)
         ISA=`awk '/cpu model/{print$4;exit(0);}' /proc/cpuinfo`
 	case ${ISA:-generic} in
 	*[678])	OUT="linux-alpha+bwx-$CC" ;;
 	*)	OUT="linux-alpha-$CC" ;;
 	esac
 	if [ "$CC" = "gcc" ]; then
 	    case ${ISA:-generic} in
 	    EV5|EV45)		options="$options -mcpu=ev5";;
 	    EV56|PCA56)		options="$options -mcpu=ev56";;
 	    *)			options="$options -mcpu=ev6";;
 	    esac
 	fi
 	;;
   ppc64-*-linux2)
 	if [ -z "$KERNEL_BITS" ]; then
 	    echo "WARNING! If you wish to build 64-bit library, then you have to"
 	    echo "         invoke './Configure linux-ppc64' *manually*."
 	    if [ "$TEST" = "false" -a -t 1 ]; then
 		echo "         You have about 5 seconds to press Ctrl-C to abort."
 		(trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	    fi
 	fi
 	if [ "$KERNEL_BITS" = "64" ]; then
 	    OUT="linux-ppc64"
 	else
 	    OUT="linux-ppc"
 	    (echo "__LP64__" | gcc -E -x c - 2>/dev/null | grep "^__LP64__" 2>&1 > /dev/null) || options="$options -m32"
 	fi
 	;;
   ppc64le-*-linux2) OUT="linux-ppc64le" ;;
   ppc-*-linux2) OUT="linux-ppc" ;;
   mips64*-*-linux2)
 	echo "WARNING! If you wish to build 64-bit library, then you have to"
 	echo "         invoke './Configure linux64-mips64' *manually*."
 	if [ "$TEST" = "false" -a -t 1 ]; then
 	    echo "         You have about 5 seconds to press Ctrl-C to abort."
 	    (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	fi
 	OUT="linux-mips64"
 	;;
   mips*-*-linux2) OUT="linux-mips32" ;;
   ppc60x-*-vxworks*) OUT="vxworks-ppc60x" ;;
   ppcgen-*-vxworks*) OUT="vxworks-ppcgen" ;;
   pentium-*-vxworks*) OUT="vxworks-pentium" ;;
   simlinux-*-vxworks*) OUT="vxworks-simlinux" ;;
   mips-*-vxworks*) OUT="vxworks-mips";;
   ia64-*-linux?) OUT="linux-ia64" ;;
   sparc64-*-linux2)
 	echo "WARNING! If you *know* that your GNU C supports 64-bit/V9 ABI"
 	echo "         and wish to build 64-bit library, then you have to"
 	echo "         invoke './Configure linux64-sparcv9' *manually*."
 	if [ "$TEST" = "false" -a -t 1 ]; then
 	  echo "          You have about 5 seconds to press Ctrl-C to abort."
 	  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	fi
 	OUT="linux-sparcv9" ;;
   sparc-*-linux2)
 	KARCH=`awk '/^type/{print$3;exit(0);}' /proc/cpuinfo`
 	case ${KARCH:-sun4} in
 	sun4u*)	OUT="linux-sparcv9" ;;
 	sun4m)	OUT="linux-sparcv8" ;;
 	sun4d)	OUT="linux-sparcv8" ;;
 	*)	OUT="linux-generic32"; options="$options -DB_ENDIAN" ;;
 	esac ;;
   parisc*-*-linux2)
 	# 64-bit builds under parisc64 linux are not supported and
 	# compiler is expected to generate 32-bit objects...
 	CPUARCH=`awk '/cpu family/{print substr($5,1,3); exit(0);}' /proc/cpuinfo`
 	CPUSCHEDULE=`awk '/^cpu.[ 	]*: PA/{print substr($3,3); exit(0);}' /proc/cpuinfo`
 
 	# ??TODO ??  Model transformations
 	# 0. CPU Architecture for the 1.1 processor has letter suffixes. We strip that off
 	#    assuming no further arch. identification will ever be used by GCC.
 	# 1. I'm most concerned about whether is a 7300LC is closer to a 7100 versus a 7100LC.
 	# 2. The variant 64-bit processors cause concern should GCC support explicit schedulers
 	#    for these chips in the future.
 	#         PA7300LC -> 7100LC (1.1)
 	#         PA8200   -> 8000   (2.0)
 	#         PA8500   -> 8000   (2.0)
 	#         PA8600   -> 8000   (2.0)
 
 	CPUSCHEDULE=`echo $CPUSCHEDULE|sed -e 's/7300LC/7100LC/' -e 's/8.00/8000/'`
 	# Finish Model transformations
 
 	options="$options -DB_ENDIAN -mschedule=$CPUSCHEDULE -march=$CPUARCH"
 	OUT="linux-generic32" ;;
   armv[1-3]*-*-linux2) OUT="linux-generic32" ;;
   armv[7-9]*-*-linux2) OUT="linux-armv4"; options="$options -march=armv7-a" ;;
   arm*-*-linux2) OUT="linux-armv4" ;;
   aarch64-*-linux2) OUT="linux-aarch64" ;;
   sh*b-*-linux2) OUT="linux-generic32"; options="$options -DB_ENDIAN" ;;
   sh*-*-linux2)  OUT="linux-generic32"; options="$options -DL_ENDIAN" ;;
   m68k*-*-linux2) OUT="linux-generic32"; options="$options -DB_ENDIAN" ;;
   s390-*-linux2) OUT="linux-generic32"; options="$options -DB_ENDIAN" ;;
   s390x-*-linux2)
 	# To be uncommented when glibc bug is fixed, see Configure...
 	#if egrep -e '^features.* highgprs' /proc/cpuinfo >/dev/null ; then
 	#  echo "WARNING! If you wish to build \"highgprs\" 32-bit library, then you"
 	#  echo "         have to invoke './Configure linux32-s390x' *manually*."
 	#  if [ "$TEST" = "false" -a -t -1 ]; then
 	#    echo "         You have about 5 seconds to press Ctrl-C to abort."
 	#    (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 	#  fi
 	#fi
 	OUT="linux64-s390x"
 	;;
   x86_64-*-linux?) OUT="linux-x86_64" ;;
   *86-*-linux2) OUT="linux-elf"
 	if [ "$GCCVER" -gt 28 ]; then
           if grep '^model.*Pentium' /proc/cpuinfo >/dev/null ; then
 	    options="$options -march=pentium"
           fi
           if grep '^model.*Pentium Pro' /proc/cpuinfo >/dev/null ; then
 	    options="$options -march=pentiumpro"
           fi
           if grep '^model.*K6' /proc/cpuinfo >/dev/null ; then
 	    options="$options -march=k6"
           fi
         fi ;;
   *-*-linux1) OUT="linux-aout" ;;
   *-*-linux2) OUT="linux-generic32" ;;
   sun4[uv]*-*-solaris2)
 	OUT="solaris-sparcv9-$CC"
 	ISA64=`(isalist) 2>/dev/null | grep sparcv9`
 	if [ "$ISA64" != "" -a "$KERNEL_BITS" = "" ]; then
 	    if [ "$CC" = "cc" -a $CCVER -ge 50 ]; then
 		echo "WARNING! If you wish to build 64-bit library, then you have to"
 		echo "         invoke './Configure solaris64-sparcv9-cc' *manually*."
 		if [ "$TEST" = "false" -a -t 1 ]; then
 		  echo "         You have about 5 seconds to press Ctrl-C to abort."
 		  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 		fi
 	    elif [ "$CC" = "gcc" -a "$GCC_ARCH" = "-m64" ]; then
 		# $GCC_ARCH denotes default ABI chosen by compiler driver
 		# (first one found on the $PATH). I assume that user
 		# expects certain consistency with the rest of his builds
 		# and therefore switch over to 64-bit. 
 		OUT="solaris64-sparcv9-gcc"
 		echo "WARNING! If you wish to build 32-bit library, then you have to"
 		echo "         invoke './Configure solaris-sparcv9-gcc' *manually*."
 		if [ "$TEST" = "false" -a -t 1 ]; then
 		  echo "         You have about 5 seconds to press Ctrl-C to abort."
 		  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 		fi
 	    elif [ "$GCC_ARCH" = "-m32" ]; then
 		echo "NOTICE! If you *know* that your GNU C supports 64-bit/V9 ABI"
 		echo "        and wish to build 64-bit library, then you have to"
 		echo "        invoke './Configure solaris64-sparcv9-gcc' *manually*."
 		if [ "$TEST" = "false" -a -t 1 ]; then
 		  echo "         You have about 5 seconds to press Ctrl-C to abort."
 		  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 		fi
 	    fi
 	fi
 	if [ "$ISA64" != "" -a "$KERNEL_BITS" = "64" ]; then
 	    OUT="solaris64-sparcv9-$CC"
 	fi
 	;;
   sun4m-*-solaris2)	OUT="solaris-sparcv8-$CC" ;;
   sun4d-*-solaris2)	OUT="solaris-sparcv8-$CC" ;;
   sun4*-*-solaris2)	OUT="solaris-sparcv7-$CC" ;;
   *86*-*-solaris2)
 	ISA64=`(isalist) 2>/dev/null | grep amd64`
 	if [ "$ISA64" != "" -a ${KERNEL_BITS:-64} -eq 64 ]; then
 	    OUT="solaris64-x86_64-$CC"
 	else
 	    OUT="solaris-x86-$CC"
 	    if [ `uname -r | sed -e 's/5\.//'` -lt 10 ]; then
 		options="$options no-sse2"
 	    fi
 	fi
 	;;
   *-*-sunos4)		OUT="sunos-$CC" ;;
 
   *86*-*-bsdi4)		OUT="BSD-x86-elf"; options="$options no-sse2 -ldl" ;;
   alpha*-*-*bsd*)	OUT="BSD-generic64"; options="$options -DL_ENDIAN" ;;
   powerpc64-*-*bsd*)	OUT="BSD-generic64"; options="$options -DB_ENDIAN" ;;
   sparc64-*-*bsd*)	OUT="BSD-sparc64" ;;
   ia64-*-*bsd*)		OUT="BSD-ia64" ;;
   amd64-*-*bsd*)	OUT="BSD-x86_64" ;;
   *86*-*-*bsd*)		# mimic ld behaviour when it's looking for libc...
 			if [ -L /usr/lib/libc.so ]; then	# [Free|Net]BSD
 			    libc=/usr/lib/libc.so
 			else					# OpenBSD
 			    # ld searches for highest libc.so.* and so do we
 			    libc=`(ls /usr/lib/libc.so.* /lib/libc.so.* | tail -1) 2>/dev/null`
 			fi
 			case "`(file -L $libc) 2>/dev/null`" in
 			*ELF*)	OUT="BSD-x86-elf" ;;
 			*)	OUT="BSD-x86"; options="$options no-sse2" ;;
 			esac ;;
   *-*-*bsd*)		OUT="BSD-generic32" ;;
 
   *-*-osf)		OUT="osf1-alpha-cc" ;;
   *-*-tru64)		OUT="tru64-alpha-cc" ;;
   *-*-[Uu]nix[Ww]are7)
 	if [ "$CC" = "gcc" ]; then
 	  OUT="unixware-7-gcc" ; options="$options no-sse2"
 	else    
 	  OUT="unixware-7" ; options="$options no-sse2 -D__i386__"
 	fi
 	;;
   *-*-[Uu]nix[Ww]are20*) OUT="unixware-2.0"; options="$options no-sse2 no-sha512" ;;
   *-*-[Uu]nix[Ww]are21*) OUT="unixware-2.1"; options="$options no-sse2 no-sha512" ;;
   *-*-vos)
 	options="$options no-threads no-shared no-asm no-dso"
 	EXE=".pm"
 	OUT="vos-$CC" ;;
   BS2000-siemens-sysv4) OUT="BS2000-OSD" ;;
   RM*-siemens-sysv4) OUT="ReliantUNIX" ;;
   *-siemens-sysv4) OUT="SINIX" ;;
   *-hpux1*)
 	if [ $CC = "gcc" -a $GCC_BITS = "64" ]; then
 	    OUT="hpux64-parisc2-gcc"
 	fi
 	[ "$KERNEL_BITS" ] || KERNEL_BITS=`(getconf KERNEL_BITS) 2>/dev/null`
 	KERNEL_BITS=${KERNEL_BITS:-32}
 	CPU_VERSION=`(getconf CPU_VERSION) 2>/dev/null`
 	CPU_VERSION=${CPU_VERSION:-0}
 	# See  for further info on CPU_VERSION.
 	if   [ $CPU_VERSION -ge 768 ]; then	# IA-64 CPU
 	     if [ $KERNEL_BITS -eq 64 -a "$CC" = "cc" ]; then
 	        OUT="hpux64-ia64-cc"
              else
 	        OUT="hpux-ia64-cc"
              fi
 	elif [ $CPU_VERSION -ge 532 ]; then	# PA-RISC 2.x CPU
 	     OUT=${OUT:-"hpux-parisc2-${CC}"}
 	     if [ $KERNEL_BITS -eq 64 -a "$CC" = "cc" ]; then
 		echo "WARNING! If you wish to build 64-bit library then you have to"
 		echo "         invoke './Configure hpux64-parisc2-cc' *manually*."
 		if [ "$TEST" = "false" -a -t 1 ]; then
 		  echo "         You have about 5 seconds to press Ctrl-C to abort."
 		  (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 		fi
 	     fi
 	elif [ $CPU_VERSION -ge 528 ]; then	# PA-RISC 1.1+ CPU
 	     OUT="hpux-parisc-${CC}"
 	elif [ $CPU_VERSION -ge 523 ]; then	# PA-RISC 1.0 CPU
 	     OUT="hpux-parisc-${CC}"
 	else					# Motorola(?) CPU
 	     OUT="hpux-$CC"
 	fi
 	options="$options -D_REENTRANT" ;;
   *-hpux)	OUT="hpux-parisc-$CC" ;;
   *-aix)
 	[ "$KERNEL_BITS" ] || KERNEL_BITS=`(getconf KERNEL_BITMODE) 2>/dev/null`
 	KERNEL_BITS=${KERNEL_BITS:-32}
 	OBJECT_MODE=${OBJECT_MODE:-32}
 	if [ "$CC" = "gcc" ]; then
 	    OUT="aix-gcc"
           if [ $OBJECT_MODE -eq 64 ]; then
             echo 'Your $OBJECT_MODE was found to be set to 64'
             OUT="aix64-gcc"
           fi
 	elif [ $OBJECT_MODE -eq 64 ]; then
 	    echo 'Your $OBJECT_MODE was found to be set to 64' 
 	    OUT="aix64-cc"
 	else
 	    OUT="aix-cc"
 	    if [ $KERNEL_BITS -eq 64 ]; then
 		echo "WARNING! If you wish to build 64-bit kit, then you have to"
 		echo "         invoke './Configure aix64-cc' *manually*."
 		if [ "$TEST" = "false" -a -t 1 ]; then
 		    echo "         You have ~5 seconds to press Ctrl-C to abort."
 		    (trap "stty `stty -g`" 2 0; stty -icanon min 0 time 50; read waste) <&1
 		fi
 	    fi
 	fi
 	if (lsattr -E -O -l `lsdev -c processor|awk '{print$1;exit}'` | grep -i powerpc) >/dev/null 2>&1; then
 	    :	# this applies even to Power3 and later, as they return PowerPC_POWER[345]
 	else
 	    options="$options no-asm"
 	fi
 	;;
   # these are all covered by the catchall below
   # *-dgux) OUT="dgux" ;;
   mips-sony-newsos4) OUT="newsos4-gcc" ;;
   *-*-cygwin_pre1.3) OUT="Cygwin-pre1.3" ;;
-  *-*-cygwin) OUT="Cygwin" ;;
+  i[3456]86-*-cygwin) OUT="Cygwin" ;;
+  *-*-cygwin) OUT="Cygwin-${MACHINE}" ;;
   t3e-cray-unicosmk) OUT="cray-t3e" ;;
   j90-cray-unicos) OUT="cray-j90" ;;
   nsr-tandem-nsk) OUT="tandem-c89" ;;
   beos-*) OUT="$GUESSOS" ;;
   x86pc-*-qnx6) OUT="QNX6-i386" ;;
   *-*-qnx6) OUT="QNX6" ;;
   x86-*-android|i?86-*-android) OUT="android-x86" ;;
   armv[7-9]*-*-android) OUT="android-armv7" ;;
   *) OUT=`echo $GUESSOS | awk -F- '{print $3}'`;;
 esac
 
 # NB: This atalla support has been superceded by the ENGINE support
 # That contains its own header and definitions anyway. Support can
 # be enabled or disabled on any supported platform without external
 # headers, eg. by adding the "hw-atalla" switch to ./config or
 # perl Configure
 #
 # See whether we can compile Atalla support
 #if [ -f /usr/include/atasi.h ]
 #then
 #  options="$options -DATALLA"
 #fi
 
 if expr "$options" : '.*no\-asm' > /dev/null; then :; else
   sh -c "$CROSS_COMPILE${CC:-gcc} -Wa,--help -c -o /tmp/null.$$.o -x assembler /dev/null && rm /tmp/null.$$.o" 2>&1 | \
   grep \\--noexecstack >/dev/null && \
   options="$options -Wa,--noexecstack"
 fi
 
 # gcc < 2.8 does not support -march=ultrasparc
 if [ "$OUT" = solaris-sparcv9-gcc -a $GCCVER -lt 28 ]
 then
   echo "WARNING! Falling down to 'solaris-sparcv8-gcc'."
   echo "         Upgrade to gcc-2.8 or later."
   sleep 5
   OUT=solaris-sparcv8-gcc
 fi
 if [ "$OUT" = "linux-sparcv9" -a $GCCVER -lt 28 ]
 then
   echo "WARNING! Falling down to 'linux-sparcv8'."
   echo "         Upgrade to gcc-2.8 or later."
   sleep 5
   OUT=linux-sparcv8
 fi
 
 case "$GUESSOS" in
   i386-*) options="$options 386" ;;
 esac
 
 for i in aes bf camellia cast des dh dsa ec hmac idea md2 md5 mdc2 rc2 rc4 rc5 ripemd rsa seed sha
 do
   if [ ! -d crypto/$i ]
   then
     options="$options no-$i"
   fi
 done
 
 # Discover Kerberos 5 (since it's still a prototype, we don't
 # do any guesses yet, that's why this section is commented away.
 #if [ -d /usr/kerberos ]; then
 #    krb5_dir=/usr/kerberos
 #    if [ \( -f $krb5_dir/lib/libgssapi_krb5.a -o -f $krb5_dir/lib/libgssapi_krb5.so* \)\
 #	-a \( -f $krb5_dir/lib/libkrb5.a -o -f $krb5_dir/lib/libkrb5.so* \)\
 #	-a \( -f $krb5_dir/lib/libcom_err.a -o -f $krb5_dir/lib/libcom_err.so* \)\
 #	-a \( -f $krb5_dir/lib/libk5crypto.a -o -f $krb5_dir/lib/libk5crypto.so* \)\
 #	-a \( -f $krb5_dir/include/krb5.h \) ]; then
 #	options="$options --with-krb5-flavor=MIT"
 #    fi
 #elif [ -d /usr/heimdal ]; then
 #    krb5_dir=/usr/heimdal
 #    if [ \( -f $krb5_dir/lib/libgssapi.a -o -f $krb5_dir/lib/libgssapi.so* \)\
 #	-a \( -f $krb5_dir/lib/libkrb5.a -o -f $krb5_dir/lib/libkrb5.so* \)\
 #	-a \( -f $krb5_dir/lib/libcom_err.a -o -f $krb5_dir/lib/libcom_err.so* \)\
 #	-a \( -f $krb5_dir/include/krb5.h \) ]; then
 #	options="$options --with-krb5-flavor=Heimdal"
 #    fi
 #fi
 
 if [ -z "$OUT" ]; then
   OUT="$CC"
 fi
 
 if [ ".$PERL" = . ] ; then
 	for i in . `echo $PATH | sed 's/:/ /g'`; do
 		if [ -f "$i/perl5$EXE" ] ; then
 			PERL="$i/perl5$EXE"
 			break;
 		fi;
 	done
 fi
 
 if [ ".$PERL" = . ] ; then
 	for i in . `echo $PATH | sed 's/:/ /g'`; do
 		if [ -f "$i/perl$EXE" ] ; then
 			if "$i/perl$EXE" -e 'exit($]<5.0)'; then
 				PERL="$i/perl$EXE"
 				break;
 			fi;
 		fi;
 	done
 fi
 
 if [ ".$PERL" = . ] ; then
 	echo "You need Perl 5."
 	exit 1
 fi
 
 # run Configure to check to see if we need to specify the 
 # compiler for the platform ... in which case we add it on
 # the end ... otherwise we leave it off
 
 $PERL ./Configure LIST | grep "$OUT-$CC" > /dev/null
 if [ $? = "0" ]; then
   OUT="$OUT-$CC"
 fi
 
 OUT="$PREFIX$OUT"
 
 $PERL ./Configure LIST | grep "$OUT" > /dev/null
 if [ $? = "0" ]; then
   echo Configuring for $OUT
 
   if [ "$TEST" = "true" ]; then
     echo $PERL ./Configure $OUT $options
   else
     $PERL ./Configure $OUT $options
   fi
 else
   echo "This system ($OUT) is not supported. See file INSTALL for details."
 fi
 )
Index: vendor-crypto/openssl/dist/crypto/asn1/tasn_dec.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/asn1/tasn_dec.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/asn1/tasn_dec.c	(revision 296273)
@@ -1,1231 +1,1229 @@
 /* tasn_dec.c */
 /*
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  * 2000.
  */
 /* ====================================================================
  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 
 static int asn1_check_eoc(const unsigned char **in, long len);
 static int asn1_find_end(const unsigned char **in, long len, char inf);
 
 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
                         char inf, int tag, int aclass, int depth);
 
 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
 
 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
                            char *inf, char *cst,
                            const unsigned char **in, long len,
                            int exptag, int expclass, char opt, ASN1_TLC *ctx);
 
 static int asn1_template_ex_d2i(ASN1_VALUE **pval,
                                 const unsigned char **in, long len,
                                 const ASN1_TEMPLATE *tt, char opt,
                                 ASN1_TLC *ctx);
 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                    const unsigned char **in, long len,
                                    const ASN1_TEMPLATE *tt, char opt,
                                    ASN1_TLC *ctx);
 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
                                  const unsigned char **in, long len,
                                  const ASN1_ITEM *it,
                                  int tag, int aclass, char opt,
                                  ASN1_TLC *ctx);
 
 /* Table to convert tags to bit values, used for MSTRING type */
 static const unsigned long tag2bit[32] = {
     /* tags  0 -  3 */
     0, 0, 0, B_ASN1_BIT_STRING,
     /* tags  4- 7 */
     B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
     /* tags  8-11 */
     B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
     /* tags 12-15 */
     B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
     /* tags 16-19 */
     B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
     /* tags 20-22 */
     B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
     /* tags 23-24 */
     B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
     /* tags 25-27 */
     B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
     /* tags 28-31 */
     B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
 };
 
 unsigned long ASN1_tag2bit(int tag)
 {
     if ((tag < 0) || (tag > 30))
         return 0;
     return tag2bit[tag];
 }
 
 /* Macro to initialize and invalidate the cache */
 
 #define asn1_tlc_clear(c)       if (c) (c)->valid = 0
 /* Version to avoid compiler warning about 'c' always non-NULL */
 #define asn1_tlc_clear_nc(c)    (c)->valid = 0
 
 /*
  * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
  * function. 'in' points to a buffer to read the data from, in future we
  * will have more advanced versions that can input data a piece at a time and
  * this will simply be a special case.
  */
 
 ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
                           const unsigned char **in, long len,
                           const ASN1_ITEM *it)
 {
     ASN1_TLC c;
     ASN1_VALUE *ptmpval = NULL;
     if (!pval)
         pval = &ptmpval;
     asn1_tlc_clear_nc(&c);
     if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
         return *pval;
     return NULL;
 }
 
 int ASN1_template_d2i(ASN1_VALUE **pval,
                       const unsigned char **in, long len,
                       const ASN1_TEMPLATE *tt)
 {
     ASN1_TLC c;
     asn1_tlc_clear_nc(&c);
     return asn1_template_ex_d2i(pval, in, len, tt, 0, &c);
 }
 
 /*
  * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
  * tag mismatch return -1 to handle OPTIONAL
  */
 
 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
                      const ASN1_ITEM *it,
                      int tag, int aclass, char opt, ASN1_TLC *ctx)
 {
     const ASN1_TEMPLATE *tt, *errtt = NULL;
     const ASN1_COMPAT_FUNCS *cf;
     const ASN1_EXTERN_FUNCS *ef;
     const ASN1_AUX *aux = it->funcs;
     ASN1_aux_cb *asn1_cb;
     const unsigned char *p = NULL, *q;
     unsigned char *wp = NULL;   /* BIG FAT WARNING! BREAKS CONST WHERE USED */
     unsigned char imphack = 0, oclass;
     char seq_eoc, seq_nolen, cst, isopt;
     long tmplen;
     int i;
     int otag;
     int ret = 0;
     ASN1_VALUE **pchptr, *ptmpval;
     int combine = aclass & ASN1_TFLG_COMBINE;
     aclass &= ~ASN1_TFLG_COMBINE;
     if (!pval)
         return 0;
     if (aux && aux->asn1_cb)
         asn1_cb = aux->asn1_cb;
     else
         asn1_cb = 0;
 
     switch (it->itype) {
     case ASN1_ITYPE_PRIMITIVE:
         if (it->templates) {
             /*
              * tagging or OPTIONAL is currently illegal on an item template
              * because the flags can't get passed down. In practice this
              * isn't a problem: we include the relevant flags from the item
              * template in the template itself.
              */
             if ((tag != -1) || opt) {
                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
                         ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
                 goto err;
             }
             return asn1_template_ex_d2i(pval, in, len,
                                         it->templates, opt, ctx);
         }
         return asn1_d2i_ex_primitive(pval, in, len, it,
                                      tag, aclass, opt, ctx);
         break;
 
     case ASN1_ITYPE_MSTRING:
         p = *in;
         /* Just read in tag and class */
         ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
                               &p, len, -1, 0, 1, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         }
 
         /* Must be UNIVERSAL class */
         if (oclass != V_ASN1_UNIVERSAL) {
             /* If OPTIONAL, assume this is OK */
             if (opt)
                 return -1;
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
             goto err;
         }
         /* Check tag matches bit map */
         if (!(ASN1_tag2bit(otag) & it->utype)) {
             /* If OPTIONAL, assume this is OK */
             if (opt)
                 return -1;
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MSTRING_WRONG_TAG);
             goto err;
         }
         return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
 
     case ASN1_ITYPE_EXTERN:
         /* Use new style d2i */
         ef = it->funcs;
         return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
 
     case ASN1_ITYPE_COMPAT:
         /* we must resort to old style evil hackery */
         cf = it->funcs;
 
         /* If OPTIONAL see if it is there */
         if (opt) {
             int exptag;
             p = *in;
             if (tag == -1)
                 exptag = it->utype;
             else
                 exptag = tag;
             /*
              * Don't care about anything other than presence of expected tag
              */
 
             ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
                                   &p, len, exptag, aclass, 1, ctx);
             if (!ret) {
                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
                 goto err;
             }
             if (ret == -1)
                 return -1;
         }
 
         /*
          * This is the old style evil hack IMPLICIT handling: since the
          * underlying code is expecting a tag and class other than the one
          * present we change the buffer temporarily then change it back
          * afterwards. This doesn't and never did work for tags > 30. Yes
          * this is *horrible* but it is only needed for old style d2i which
          * will hopefully not be around for much longer. FIXME: should copy
          * the buffer then modify it so the input buffer can be const: we
          * should *always* copy because the old style d2i might modify the
          * buffer.
          */
 
         if (tag != -1) {
             wp = *(unsigned char **)in;
             imphack = *wp;
             if (p == NULL) {
                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
                 goto err;
             }
             *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED)
                                   | it->utype);
         }
 
         ptmpval = cf->asn1_d2i(pval, in, len);
 
         if (tag != -1)
             *wp = imphack;
 
         if (ptmpval)
             return 1;
 
         ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
         goto err;
 
     case ASN1_ITYPE_CHOICE:
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
             goto auxerr;
         if (*pval) {
             /* Free up and zero CHOICE value if initialised */
             i = asn1_get_choice_selector(pval, it);
             if ((i >= 0) && (i < it->tcount)) {
                 tt = it->templates + i;
                 pchptr = asn1_get_field_ptr(pval, tt);
                 ASN1_template_free(pchptr, tt);
                 asn1_set_choice_selector(pval, -1, it);
             }
         } else if (!ASN1_item_ex_new(pval, it)) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         }
         /* CHOICE type, try each possibility in turn */
         p = *in;
         for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
             pchptr = asn1_get_field_ptr(pval, tt);
             /*
              * We mark field as OPTIONAL so its absence can be recognised.
              */
             ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx);
             /* If field not present, try the next one */
             if (ret == -1)
                 continue;
             /* If positive return, read OK, break loop */
             if (ret > 0)
                 break;
             /* Otherwise must be an ASN1 parsing error */
             errtt = tt;
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         }
 
         /* Did we fall off the end without reading anything? */
         if (i == it->tcount) {
             /* If OPTIONAL, this is OK */
             if (opt) {
                 /* Free and zero it */
                 ASN1_item_ex_free(pval, it);
                 return -1;
             }
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
             goto err;
         }
 
         asn1_set_choice_selector(pval, i, it);
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
             goto auxerr;
         *in = p;
         return 1;
 
     case ASN1_ITYPE_NDEF_SEQUENCE:
     case ASN1_ITYPE_SEQUENCE:
         p = *in;
         tmplen = len;
 
         /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
         if (tag == -1) {
             tag = V_ASN1_SEQUENCE;
             aclass = V_ASN1_UNIVERSAL;
         }
         /* Get SEQUENCE length and update len, p */
         ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
                               &p, len, tag, aclass, opt, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         } else if (ret == -1)
             return -1;
         if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
             len = tmplen - (p - *in);
             seq_nolen = 1;
         }
         /* If indefinite we don't do a length check */
         else
             seq_nolen = seq_eoc;
         if (!cst) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
             goto err;
         }
 
         if (!*pval && !ASN1_item_ex_new(pval, it)) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         }
 
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
             goto auxerr;
 
         /* Free up and zero any ADB found */
         for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
             if (tt->flags & ASN1_TFLG_ADB_MASK) {
                 const ASN1_TEMPLATE *seqtt;
                 ASN1_VALUE **pseqval;
                 seqtt = asn1_do_adb(pval, tt, 1);
                 pseqval = asn1_get_field_ptr(pval, seqtt);
                 ASN1_template_free(pseqval, seqtt);
             }
         }
 
         /* Get each field entry */
         for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
             const ASN1_TEMPLATE *seqtt;
             ASN1_VALUE **pseqval;
             seqtt = asn1_do_adb(pval, tt, 1);
             if (!seqtt)
                 goto err;
             pseqval = asn1_get_field_ptr(pval, seqtt);
             /* Have we ran out of data? */
             if (!len)
                 break;
             q = p;
             if (asn1_check_eoc(&p, len)) {
                 if (!seq_eoc) {
                     ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_UNEXPECTED_EOC);
                     goto err;
                 }
                 len -= p - q;
                 seq_eoc = 0;
                 q = p;
                 break;
             }
             /*
              * This determines the OPTIONAL flag value. The field cannot be
              * omitted if it is the last of a SEQUENCE and there is still
              * data to be read. This isn't strictly necessary but it
              * increases efficiency in some cases.
              */
             if (i == (it->tcount - 1))
                 isopt = 0;
             else
                 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
             /*
              * attempt to read in field, allowing each to be OPTIONAL
              */
 
             ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx);
             if (!ret) {
                 errtt = seqtt;
                 goto err;
             } else if (ret == -1) {
                 /*
                  * OPTIONAL component absent. Free and zero the field.
                  */
                 ASN1_template_free(pseqval, seqtt);
                 continue;
             }
             /* Update length */
             len -= p - q;
         }
 
         /* Check for EOC if expecting one */
         if (seq_eoc && !asn1_check_eoc(&p, len)) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_MISSING_EOC);
             goto err;
         }
         /* Check all data read */
         if (!seq_nolen && len) {
             ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
             goto err;
         }
 
         /*
          * If we get here we've got no more data in the SEQUENCE, however we
          * may not have read all fields so check all remaining are OPTIONAL
          * and clear any that are.
          */
         for (; i < it->tcount; tt++, i++) {
             const ASN1_TEMPLATE *seqtt;
             seqtt = asn1_do_adb(pval, tt, 1);
             if (!seqtt)
                 goto err;
             if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
                 ASN1_VALUE **pseqval;
                 pseqval = asn1_get_field_ptr(pval, seqtt);
                 ASN1_template_free(pseqval, seqtt);
             } else {
                 errtt = seqtt;
                 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_FIELD_MISSING);
                 goto err;
             }
         }
         /* Save encoding */
         if (!asn1_enc_save(pval, *in, p - *in, it))
             goto auxerr;
         if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
             goto auxerr;
         *in = p;
         return 1;
 
     default:
         return 0;
     }
  auxerr:
     ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
  err:
     if (combine == 0)
         ASN1_item_ex_free(pval, it);
     if (errtt)
         ERR_add_error_data(4, "Field=", errtt->field_name,
                            ", Type=", it->sname);
     else
         ERR_add_error_data(2, "Type=", it->sname);
     return 0;
 }
 
 /*
  * Templates are handled with two separate functions. One handles any
  * EXPLICIT tag and the other handles the rest.
  */
 
 static int asn1_template_ex_d2i(ASN1_VALUE **val,
                                 const unsigned char **in, long inlen,
                                 const ASN1_TEMPLATE *tt, char opt,
                                 ASN1_TLC *ctx)
 {
     int flags, aclass;
     int ret;
     long len;
     const unsigned char *p, *q;
     char exp_eoc;
     if (!val)
         return 0;
     flags = tt->flags;
     aclass = flags & ASN1_TFLG_TAG_CLASS;
 
     p = *in;
 
     /* Check if EXPLICIT tag expected */
     if (flags & ASN1_TFLG_EXPTAG) {
         char cst;
         /*
          * Need to work out amount of data available to the inner content and
          * where it starts: so read in EXPLICIT header to get the info.
          */
         ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
                               &p, inlen, tt->tag, aclass, opt, ctx);
         q = p;
         if (!ret) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         } else if (ret == -1)
             return -1;
         if (!cst) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
                     ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
             return 0;
         }
         /* We've found the field so it can't be OPTIONAL now */
         ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         }
         /* We read the field in OK so update length */
         len -= p - q;
         if (exp_eoc) {
             /* If NDEF we must have an EOC here */
             if (!asn1_check_eoc(&p, len)) {
                 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
                 goto err;
             }
         } else {
             /*
              * Otherwise we must hit the EXPLICIT tag end or its an error
              */
             if (len) {
                 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
                         ASN1_R_EXPLICIT_LENGTH_MISMATCH);
                 goto err;
             }
         }
     } else
         return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
 
     *in = p;
     return 1;
 
  err:
     ASN1_template_free(val, tt);
     return 0;
 }
 
 static int asn1_template_noexp_d2i(ASN1_VALUE **val,
                                    const unsigned char **in, long len,
                                    const ASN1_TEMPLATE *tt, char opt,
                                    ASN1_TLC *ctx)
 {
     int flags, aclass;
     int ret;
     const unsigned char *p, *q;
     if (!val)
         return 0;
     flags = tt->flags;
     aclass = flags & ASN1_TFLG_TAG_CLASS;
 
     p = *in;
     q = p;
 
     if (flags & ASN1_TFLG_SK_MASK) {
         /* SET OF, SEQUENCE OF */
         int sktag, skaclass;
         char sk_eoc;
         /* First work out expected inner tag value */
         if (flags & ASN1_TFLG_IMPTAG) {
             sktag = tt->tag;
             skaclass = aclass;
         } else {
             skaclass = V_ASN1_UNIVERSAL;
             if (flags & ASN1_TFLG_SET_OF)
                 sktag = V_ASN1_SET;
             else
                 sktag = V_ASN1_SEQUENCE;
         }
         /* Get the tag */
         ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
                               &p, len, sktag, skaclass, opt, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         } else if (ret == -1)
             return -1;
         if (!*val)
             *val = (ASN1_VALUE *)sk_new_null();
         else {
             /*
              * We've got a valid STACK: free up any items present
              */
             STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
             ASN1_VALUE *vtmp;
             while (sk_ASN1_VALUE_num(sktmp) > 0) {
                 vtmp = sk_ASN1_VALUE_pop(sktmp);
                 ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
             }
         }
 
         if (!*val) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
             goto err;
         }
 
         /* Read as many items as we can */
         while (len > 0) {
             ASN1_VALUE *skfield;
             q = p;
             /* See if EOC found */
             if (asn1_check_eoc(&p, len)) {
                 if (!sk_eoc) {
                     ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                             ASN1_R_UNEXPECTED_EOC);
                     goto err;
                 }
                 len -= p - q;
                 sk_eoc = 0;
                 break;
             }
             skfield = NULL;
             if (!ASN1_item_ex_d2i(&skfield, &p, len,
                                   ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
                         ERR_R_NESTED_ASN1_ERROR);
                 goto err;
             }
             len -= p - q;
             if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
                 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
                 goto err;
             }
         }
         if (sk_eoc) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
             goto err;
         }
     } else if (flags & ASN1_TFLG_IMPTAG) {
         /* IMPLICIT tagging */
         ret = ASN1_item_ex_d2i(val, &p, len,
                                ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
                                ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         } else if (ret == -1)
             return -1;
     } else {
         /* Nothing special */
         ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
                                -1, tt->flags & ASN1_TFLG_COMBINE, opt, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
             goto err;
         } else if (ret == -1)
             return -1;
     }
 
     *in = p;
     return 1;
 
  err:
     ASN1_template_free(val, tt);
     return 0;
 }
 
 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
                                  const unsigned char **in, long inlen,
                                  const ASN1_ITEM *it,
                                  int tag, int aclass, char opt, ASN1_TLC *ctx)
 {
     int ret = 0, utype;
     long plen;
     char cst, inf, free_cont = 0;
     const unsigned char *p;
-    BUF_MEM buf;
+    BUF_MEM buf = { 0, NULL, 0 };
     const unsigned char *cont = NULL;
     long len;
     if (!pval) {
         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
         return 0;               /* Should never happen */
     }
 
     if (it->itype == ASN1_ITYPE_MSTRING) {
         utype = tag;
         tag = -1;
     } else
         utype = it->utype;
 
     if (utype == V_ASN1_ANY) {
         /* If type is ANY need to figure out type from tag */
         unsigned char oclass;
         if (tag >= 0) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
             return 0;
         }
         if (opt) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
                     ASN1_R_ILLEGAL_OPTIONAL_ANY);
             return 0;
         }
         p = *in;
         ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
                               &p, inlen, -1, 0, 0, ctx);
         if (!ret) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         }
         if (oclass != V_ASN1_UNIVERSAL)
             utype = V_ASN1_OTHER;
     }
     if (tag == -1) {
         tag = utype;
         aclass = V_ASN1_UNIVERSAL;
     }
     p = *in;
     /* Check header */
     ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
                           &p, inlen, tag, aclass, opt, ctx);
     if (!ret) {
         ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
         return 0;
     } else if (ret == -1)
         return -1;
     ret = 0;
     /* SEQUENCE, SET and "OTHER" are left in encoded form */
     if ((utype == V_ASN1_SEQUENCE)
         || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
         /*
          * Clear context cache for type OTHER because the auto clear when we
          * have a exact match wont work
          */
         if (utype == V_ASN1_OTHER) {
             asn1_tlc_clear(ctx);
         }
         /* SEQUENCE and SET must be constructed */
         else if (!cst) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
                     ASN1_R_TYPE_NOT_CONSTRUCTED);
             return 0;
         }
 
         cont = *in;
         /* If indefinite length constructed find the real end */
         if (inf) {
             if (!asn1_find_end(&p, plen, inf))
                 goto err;
             len = p - cont;
         } else {
             len = p - cont + plen;
             p += plen;
-            buf.data = NULL;
         }
     } else if (cst) {
         if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
             || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
             || utype == V_ASN1_ENUMERATED) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
             return 0;
         }
-        buf.length = 0;
-        buf.max = 0;
-        buf.data = NULL;
+
+        /* Free any returned 'buf' content */
+        free_cont = 1;
         /*
          * Should really check the internal tags are correct but some things
          * may get this wrong. The relevant specs say that constructed string
          * types should be OCTET STRINGs internally irrespective of the type.
          * So instead just check for UNIVERSAL class and ignore the tag.
          */
         if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
-            free_cont = 1;
             goto err;
         }
         len = buf.length;
         /* Append a final null to string */
         if (!BUF_MEM_grow_clean(&buf, len + 1)) {
             ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
-            return 0;
+            goto err;
         }
         buf.data[len] = 0;
         cont = (const unsigned char *)buf.data;
-        free_cont = 1;
     } else {
         cont = p;
         len = plen;
         p += plen;
     }
 
     /* We now have content length and type: translate into a structure */
+    /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
     if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
         goto err;
 
     *in = p;
     ret = 1;
  err:
     if (free_cont && buf.data)
         OPENSSL_free(buf.data);
     return ret;
 }
 
 /* Translate ASN1 content octets into a structure */
 
 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
                 int utype, char *free_cont, const ASN1_ITEM *it)
 {
     ASN1_VALUE **opval = NULL;
     ASN1_STRING *stmp;
     ASN1_TYPE *typ = NULL;
     int ret = 0;
     const ASN1_PRIMITIVE_FUNCS *pf;
     ASN1_INTEGER **tint;
     pf = it->funcs;
 
     if (pf && pf->prim_c2i)
         return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
     /* If ANY type clear type and set pointer to internal value */
     if (it->utype == V_ASN1_ANY) {
         if (!*pval) {
             typ = ASN1_TYPE_new();
             if (typ == NULL)
                 goto err;
             *pval = (ASN1_VALUE *)typ;
         } else
             typ = (ASN1_TYPE *)*pval;
 
         if (utype != typ->type)
             ASN1_TYPE_set(typ, utype, NULL);
         opval = pval;
         pval = &typ->value.asn1_value;
     }
     switch (utype) {
     case V_ASN1_OBJECT:
         if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
             goto err;
         break;
 
     case V_ASN1_NULL:
         if (len) {
             ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
             goto err;
         }
         *pval = (ASN1_VALUE *)1;
         break;
 
     case V_ASN1_BOOLEAN:
         if (len != 1) {
             ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
             goto err;
         } else {
             ASN1_BOOLEAN *tbool;
             tbool = (ASN1_BOOLEAN *)pval;
             *tbool = *cont;
         }
         break;
 
     case V_ASN1_BIT_STRING:
         if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
             goto err;
         break;
 
     case V_ASN1_INTEGER:
     case V_ASN1_NEG_INTEGER:
     case V_ASN1_ENUMERATED:
     case V_ASN1_NEG_ENUMERATED:
         tint = (ASN1_INTEGER **)pval;
         if (!c2i_ASN1_INTEGER(tint, &cont, len))
             goto err;
         /* Fixup type to match the expected form */
         (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
         break;
 
     case V_ASN1_OCTET_STRING:
     case V_ASN1_NUMERICSTRING:
     case V_ASN1_PRINTABLESTRING:
     case V_ASN1_T61STRING:
     case V_ASN1_VIDEOTEXSTRING:
     case V_ASN1_IA5STRING:
     case V_ASN1_UTCTIME:
     case V_ASN1_GENERALIZEDTIME:
     case V_ASN1_GRAPHICSTRING:
     case V_ASN1_VISIBLESTRING:
     case V_ASN1_GENERALSTRING:
     case V_ASN1_UNIVERSALSTRING:
     case V_ASN1_BMPSTRING:
     case V_ASN1_UTF8STRING:
     case V_ASN1_OTHER:
     case V_ASN1_SET:
     case V_ASN1_SEQUENCE:
     default:
         if (utype == V_ASN1_BMPSTRING && (len & 1)) {
             ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
             goto err;
         }
         if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
             ASN1err(ASN1_F_ASN1_EX_C2I,
                     ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
             goto err;
         }
         /* All based on ASN1_STRING and handled the same */
         if (!*pval) {
             stmp = ASN1_STRING_type_new(utype);
             if (!stmp) {
                 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
                 goto err;
             }
             *pval = (ASN1_VALUE *)stmp;
         } else {
             stmp = (ASN1_STRING *)*pval;
             stmp->type = utype;
         }
         /* If we've already allocated a buffer use it */
         if (*free_cont) {
             if (stmp->data)
                 OPENSSL_free(stmp->data);
             stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
             stmp->length = len;
             *free_cont = 0;
         } else {
             if (!ASN1_STRING_set(stmp, cont, len)) {
                 ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
                 ASN1_STRING_free(stmp);
                 *pval = NULL;
                 goto err;
             }
         }
         break;
     }
     /* If ASN1_ANY and NULL type fix up value */
     if (typ && (utype == V_ASN1_NULL))
         typ->value.ptr = NULL;
 
     ret = 1;
  err:
     if (!ret) {
         ASN1_TYPE_free(typ);
         if (opval)
             *opval = NULL;
     }
     return ret;
 }
 
 /*
  * This function finds the end of an ASN1 structure when passed its maximum
  * length, whether it is indefinite length and a pointer to the content. This
  * is more efficient than calling asn1_collect because it does not recurse on
  * each indefinite length header.
  */
 
 static int asn1_find_end(const unsigned char **in, long len, char inf)
 {
     int expected_eoc;
     long plen;
     const unsigned char *p = *in, *q;
     /* If not indefinite length constructed just add length */
     if (inf == 0) {
         *in += len;
         return 1;
     }
     expected_eoc = 1;
     /*
      * Indefinite length constructed form. Find the end when enough EOCs are
      * found. If more indefinite length constructed headers are encountered
      * increment the expected eoc count otherwise just skip to the end of the
      * data.
      */
     while (len > 0) {
         if (asn1_check_eoc(&p, len)) {
             expected_eoc--;
             if (expected_eoc == 0)
                 break;
             len -= 2;
             continue;
         }
         q = p;
         /* Just read in a header: only care about the length */
         if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
                              -1, 0, 0, NULL)) {
             ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         }
         if (inf)
             expected_eoc++;
         else
             p += plen;
         len -= p - q;
     }
     if (expected_eoc) {
         ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
         return 0;
     }
     *in = p;
     return 1;
 }
 
 /*
  * This function collects the asn1 data from a constructred string type into
  * a buffer. The values of 'in' and 'len' should refer to the contents of the
  * constructed type and 'inf' should be set if it is indefinite length.
  */
 
 #ifndef ASN1_MAX_STRING_NEST
 /*
  * This determines how many levels of recursion are permitted in ASN1 string
  * types. If it is not limited stack overflows can occur. If set to zero no
  * recursion is allowed at all. Although zero should be adequate examples
  * exist that require a value of 1. So 5 should be more than enough.
  */
 # define ASN1_MAX_STRING_NEST 5
 #endif
 
 static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
                         char inf, int tag, int aclass, int depth)
 {
     const unsigned char *p, *q;
     long plen;
     char cst, ininf;
     p = *in;
     inf &= 1;
     /*
      * If no buffer and not indefinite length constructed just pass over the
      * encoded data
      */
     if (!buf && !inf) {
         *in += len;
         return 1;
     }
     while (len > 0) {
         q = p;
         /* Check for EOC */
         if (asn1_check_eoc(&p, len)) {
             /*
              * EOC is illegal outside indefinite length constructed form
              */
             if (!inf) {
                 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
                 return 0;
             }
             inf = 0;
             break;
         }
 
         if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
                              len, tag, aclass, 0, NULL)) {
             ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
             return 0;
         }
 
         /* If indefinite length constructed update max length */
         if (cst) {
             if (depth >= ASN1_MAX_STRING_NEST) {
                 ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
                 return 0;
             }
             if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
                 return 0;
         } else if (plen && !collect_data(buf, &p, plen))
             return 0;
         len -= p - q;
     }
     if (inf) {
         ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
         return 0;
     }
     *in = p;
     return 1;
 }
 
 static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
 {
     int len;
     if (buf) {
         len = buf->length;
         if (!BUF_MEM_grow_clean(buf, len + plen)) {
             ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
             return 0;
         }
         memcpy(buf->data + len, *p, plen);
     }
     *p += plen;
     return 1;
 }
 
 /* Check for ASN1 EOC and swallow it if found */
 
 static int asn1_check_eoc(const unsigned char **in, long len)
 {
     const unsigned char *p;
     if (len < 2)
         return 0;
     p = *in;
     if (!p[0] && !p[1]) {
         *in += 2;
         return 1;
     }
     return 0;
 }
 
 /*
  * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
  * length for indefinite length constructed form, we don't know the exact
  * length but we can set an upper bound to the amount of data available minus
  * the header length just read.
  */
 
 static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
                            char *inf, char *cst,
                            const unsigned char **in, long len,
                            int exptag, int expclass, char opt, ASN1_TLC *ctx)
 {
     int i;
     int ptag, pclass;
     long plen;
     const unsigned char *p, *q;
     p = *in;
     q = p;
 
     if (ctx && ctx->valid) {
         i = ctx->ret;
         plen = ctx->plen;
         pclass = ctx->pclass;
         ptag = ctx->ptag;
         p += ctx->hdrlen;
     } else {
         i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
         if (ctx) {
             ctx->ret = i;
             ctx->plen = plen;
             ctx->pclass = pclass;
             ctx->ptag = ptag;
             ctx->hdrlen = p - q;
             ctx->valid = 1;
             /*
              * If definite length, and no error, length + header can't exceed
              * total amount of data available.
              */
             if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
                 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
                 asn1_tlc_clear(ctx);
                 return 0;
             }
         }
     }
 
     if (i & 0x80) {
         ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
         asn1_tlc_clear(ctx);
         return 0;
     }
     if (exptag >= 0) {
         if ((exptag != ptag) || (expclass != pclass)) {
             /*
              * If type is OPTIONAL, not an error: indicate missing type.
              */
             if (opt)
                 return -1;
             asn1_tlc_clear(ctx);
             ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
             return 0;
         }
         /*
          * We have a tag and class match: assume we are going to do something
          * with it
          */
         asn1_tlc_clear(ctx);
     }
 
     if (i & 1)
         plen = len - (p - q);
 
     if (inf)
         *inf = i & 1;
 
     if (cst)
         *cst = i & V_ASN1_CONSTRUCTED;
 
     if (olen)
         *olen = plen;
 
     if (oclass)
         *oclass = pclass;
 
     if (otag)
         *otag = ptag;
 
     *in = p;
     return 1;
 }
Index: vendor-crypto/openssl/dist/crypto/bio/b_print.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/bio/b_print.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bio/b_print.c	(revision 296273)
@@ -1,818 +1,863 @@
 /* crypto/bio/b_print.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 /* disable assert() unless BIO_DEBUG has been defined */
 #ifndef BIO_DEBUG
 # ifndef NDEBUG
 #  define NDEBUG
 # endif
 #endif
 
 /*
  * Stolen from tjh's ssl/ssl_trc.c stuff.
  */
 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include "cryptlib.h"
 #ifndef NO_SYS_TYPES_H
 # include 
 #endif
 #include          /* To get BN_LLONG properly defined */
 #include 
 
 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
 # ifndef HAVE_LONG_LONG
 #  define HAVE_LONG_LONG 1
 # endif
 #endif
 
 /***************************************************************************/
 
 /*
  * Copyright Patrick Powell 1995
  * This code is based on code written by Patrick Powell 
  * It may be used for any purpose as long as this notice remains intact
  * on all source code distributions.
  */
 
 /*-
  * This code contains numerious changes and enhancements which were
  * made by lots of contributors over the last years to Patrick Powell's
  * original code:
  *
  * o Patrick Powell       (1995)
  * o Brandon Long           (1996, for Mutt)
  * o Thomas Roessler         (1998, for Mutt)
  * o Michael Elkins             (1998, for Mutt)
  * o Andrew Tridgell         (1998, for Samba)
  * o Luke Mewburn            (1999, for LukemFTP)
  * o Ralf S. Engelschall  (1999, for Pth)
  * o ...                                       (for OpenSSL)
  */
 
 #ifdef HAVE_LONG_DOUBLE
 # define LDOUBLE long double
 #else
 # define LDOUBLE double
 #endif
 
 #ifdef HAVE_LONG_LONG
 # if defined(_WIN32) && !defined(__GNUC__)
 #  define LLONG __int64
 # else
 #  define LLONG long long
 # endif
 #else
 # define LLONG long
 #endif
 
-static void fmtstr(char **, char **, size_t *, size_t *,
-                   const char *, int, int, int);
-static void fmtint(char **, char **, size_t *, size_t *,
-                   LLONG, int, int, int, int);
-static void fmtfp(char **, char **, size_t *, size_t *,
-                  LDOUBLE, int, int, int);
-static void doapr_outch(char **, char **, size_t *, size_t *, int);
-static void _dopr(char **sbuffer, char **buffer,
-                  size_t *maxlen, size_t *retlen, int *truncated,
-                  const char *format, va_list args);
+static int fmtstr(char **, char **, size_t *, size_t *,
+                  const char *, int, int, int);
+static int fmtint(char **, char **, size_t *, size_t *,
+                  LLONG, int, int, int, int);
+static int fmtfp(char **, char **, size_t *, size_t *,
+                 LDOUBLE, int, int, int);
+static int doapr_outch(char **, char **, size_t *, size_t *, int);
+static int _dopr(char **sbuffer, char **buffer,
+                 size_t *maxlen, size_t *retlen, int *truncated,
+                 const char *format, va_list args);
 
 /* format read states */
 #define DP_S_DEFAULT    0
 #define DP_S_FLAGS      1
 #define DP_S_MIN        2
 #define DP_S_DOT        3
 #define DP_S_MAX        4
 #define DP_S_MOD        5
 #define DP_S_CONV       6
 #define DP_S_DONE       7
 
 /* format flags - Bits */
 #define DP_F_MINUS      (1 << 0)
 #define DP_F_PLUS       (1 << 1)
 #define DP_F_SPACE      (1 << 2)
 #define DP_F_NUM        (1 << 3)
 #define DP_F_ZERO       (1 << 4)
 #define DP_F_UP         (1 << 5)
 #define DP_F_UNSIGNED   (1 << 6)
 
 /* conversion flags */
 #define DP_C_SHORT      1
 #define DP_C_LONG       2
 #define DP_C_LDOUBLE    3
 #define DP_C_LLONG      4
 
 /* some handy macros */
 #define char_to_int(p) (p - '0')
 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
 
-static void
+static int
 _dopr(char **sbuffer,
       char **buffer,
       size_t *maxlen,
       size_t *retlen, int *truncated, const char *format, va_list args)
 {
     char ch;
     LLONG value;
     LDOUBLE fvalue;
     char *strvalue;
     int min;
     int max;
     int state;
     int flags;
     int cflags;
     size_t currlen;
 
     state = DP_S_DEFAULT;
     flags = currlen = cflags = min = 0;
     max = -1;
     ch = *format++;
 
     while (state != DP_S_DONE) {
         if (ch == '\0' || (buffer == NULL && currlen >= *maxlen))
             state = DP_S_DONE;
 
         switch (state) {
         case DP_S_DEFAULT:
             if (ch == '%')
                 state = DP_S_FLAGS;
             else
-                doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
+                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
+                    return 0;
             ch = *format++;
             break;
         case DP_S_FLAGS:
             switch (ch) {
             case '-':
                 flags |= DP_F_MINUS;
                 ch = *format++;
                 break;
             case '+':
                 flags |= DP_F_PLUS;
                 ch = *format++;
                 break;
             case ' ':
                 flags |= DP_F_SPACE;
                 ch = *format++;
                 break;
             case '#':
                 flags |= DP_F_NUM;
                 ch = *format++;
                 break;
             case '0':
                 flags |= DP_F_ZERO;
                 ch = *format++;
                 break;
             default:
                 state = DP_S_MIN;
                 break;
             }
             break;
         case DP_S_MIN:
             if (isdigit((unsigned char)ch)) {
                 min = 10 * min + char_to_int(ch);
                 ch = *format++;
             } else if (ch == '*') {
                 min = va_arg(args, int);
                 ch = *format++;
                 state = DP_S_DOT;
             } else
                 state = DP_S_DOT;
             break;
         case DP_S_DOT:
             if (ch == '.') {
                 state = DP_S_MAX;
                 ch = *format++;
             } else
                 state = DP_S_MOD;
             break;
         case DP_S_MAX:
             if (isdigit((unsigned char)ch)) {
                 if (max < 0)
                     max = 0;
                 max = 10 * max + char_to_int(ch);
                 ch = *format++;
             } else if (ch == '*') {
                 max = va_arg(args, int);
                 ch = *format++;
                 state = DP_S_MOD;
             } else
                 state = DP_S_MOD;
             break;
         case DP_S_MOD:
             switch (ch) {
             case 'h':
                 cflags = DP_C_SHORT;
                 ch = *format++;
                 break;
             case 'l':
                 if (*format == 'l') {
                     cflags = DP_C_LLONG;
                     format++;
                 } else
                     cflags = DP_C_LONG;
                 ch = *format++;
                 break;
             case 'q':
                 cflags = DP_C_LLONG;
                 ch = *format++;
                 break;
             case 'L':
                 cflags = DP_C_LDOUBLE;
                 ch = *format++;
                 break;
             default:
                 break;
             }
             state = DP_S_CONV;
             break;
         case DP_S_CONV:
             switch (ch) {
             case 'd':
             case 'i':
                 switch (cflags) {
                 case DP_C_SHORT:
                     value = (short int)va_arg(args, int);
                     break;
                 case DP_C_LONG:
                     value = va_arg(args, long int);
                     break;
                 case DP_C_LLONG:
                     value = va_arg(args, LLONG);
                     break;
                 default:
                     value = va_arg(args, int);
                     break;
                 }
-                fmtint(sbuffer, buffer, &currlen, maxlen,
-                       value, 10, min, max, flags);
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value, 10, min,
+                            max, flags))
+                    return 0;
                 break;
             case 'X':
                 flags |= DP_F_UP;
                 /* FALLTHROUGH */
             case 'x':
             case 'o':
             case 'u':
                 flags |= DP_F_UNSIGNED;
                 switch (cflags) {
                 case DP_C_SHORT:
                     value = (unsigned short int)va_arg(args, unsigned int);
                     break;
                 case DP_C_LONG:
                     value = (LLONG) va_arg(args, unsigned long int);
                     break;
                 case DP_C_LLONG:
                     value = va_arg(args, unsigned LLONG);
                     break;
                 default:
                     value = (LLONG) va_arg(args, unsigned int);
                     break;
                 }
-                fmtint(sbuffer, buffer, &currlen, maxlen, value,
-                       ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
-                       min, max, flags);
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen, value,
+                            ch == 'o' ? 8 : (ch == 'u' ? 10 : 16),
+                            min, max, flags))
+                    return 0;
                 break;
             case 'f':
                 if (cflags == DP_C_LDOUBLE)
                     fvalue = va_arg(args, LDOUBLE);
                 else
                     fvalue = va_arg(args, double);
-                fmtfp(sbuffer, buffer, &currlen, maxlen,
-                      fvalue, min, max, flags);
+                if (!fmtfp(sbuffer, buffer, &currlen, maxlen, fvalue, min, max,
+                           flags))
+                    return 0;
                 break;
             case 'E':
                 flags |= DP_F_UP;
             case 'e':
                 if (cflags == DP_C_LDOUBLE)
                     fvalue = va_arg(args, LDOUBLE);
                 else
                     fvalue = va_arg(args, double);
                 break;
             case 'G':
                 flags |= DP_F_UP;
             case 'g':
                 if (cflags == DP_C_LDOUBLE)
                     fvalue = va_arg(args, LDOUBLE);
                 else
                     fvalue = va_arg(args, double);
                 break;
             case 'c':
-                doapr_outch(sbuffer, buffer, &currlen, maxlen,
-                            va_arg(args, int));
+                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen,
+                            va_arg(args, int)))
+                    return 0;
                 break;
             case 's':
                 strvalue = va_arg(args, char *);
                 if (max < 0) {
                     if (buffer)
                         max = INT_MAX;
                     else
                         max = *maxlen;
                 }
-                fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
-                       flags, min, max);
+                if (!fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
+                            flags, min, max))
+                    return 0;
                 break;
             case 'p':
                 value = (long)va_arg(args, void *);
-                fmtint(sbuffer, buffer, &currlen, maxlen,
-                       value, 16, min, max, flags | DP_F_NUM);
+                if (!fmtint(sbuffer, buffer, &currlen, maxlen,
+                            value, 16, min, max, flags | DP_F_NUM))
+                    return 0;
                 break;
             case 'n':          /* XXX */
                 if (cflags == DP_C_SHORT) {
                     short int *num;
                     num = va_arg(args, short int *);
                     *num = currlen;
                 } else if (cflags == DP_C_LONG) { /* XXX */
                     long int *num;
                     num = va_arg(args, long int *);
                     *num = (long int)currlen;
                 } else if (cflags == DP_C_LLONG) { /* XXX */
                     LLONG *num;
                     num = va_arg(args, LLONG *);
                     *num = (LLONG) currlen;
                 } else {
                     int *num;
                     num = va_arg(args, int *);
                     *num = currlen;
                 }
                 break;
             case '%':
-                doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
+                if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, ch))
+                    return 0;
                 break;
             case 'w':
                 /* not supported yet, treat as next char */
                 ch = *format++;
                 break;
             default:
                 /* unknown, skip */
                 break;
             }
             ch = *format++;
             state = DP_S_DEFAULT;
             flags = cflags = min = 0;
             max = -1;
             break;
         case DP_S_DONE:
             break;
         default:
             break;
         }
     }
     *truncated = (currlen > *maxlen - 1);
     if (*truncated)
         currlen = *maxlen - 1;
-    doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0');
+    if(!doapr_outch(sbuffer, buffer, &currlen, maxlen, '\0'))
+        return 0;
     *retlen = currlen - 1;
-    return;
+    return 1;
 }
 
-static void
+static int
 fmtstr(char **sbuffer,
        char **buffer,
        size_t *currlen,
        size_t *maxlen, const char *value, int flags, int min, int max)
 {
-    int padlen, strln;
+    int padlen;
+    size_t strln;
     int cnt = 0;
 
     if (value == 0)
         value = "";
-    for (strln = 0; value[strln]; ++strln) ;
+
+    strln = strlen(value);
+    if (strln > INT_MAX)
+        strln = INT_MAX;
+
     padlen = min - strln;
-    if (padlen < 0)
+    if (min < 0 || padlen < 0)
         padlen = 0;
     if (flags & DP_F_MINUS)
         padlen = -padlen;
 
     while ((padlen > 0) && (cnt < max)) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         --padlen;
         ++cnt;
     }
     while (*value && (cnt < max)) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *value++))
+            return 0;
         ++cnt;
     }
     while ((padlen < 0) && (cnt < max)) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         ++padlen;
         ++cnt;
     }
+    return 1;
 }
 
-static void
+static int
 fmtint(char **sbuffer,
        char **buffer,
        size_t *currlen,
        size_t *maxlen, LLONG value, int base, int min, int max, int flags)
 {
     int signvalue = 0;
     const char *prefix = "";
     unsigned LLONG uvalue;
     char convert[DECIMAL_SIZE(value) + 3];
     int place = 0;
     int spadlen = 0;
     int zpadlen = 0;
     int caps = 0;
 
     if (max < 0)
         max = 0;
     uvalue = value;
     if (!(flags & DP_F_UNSIGNED)) {
         if (value < 0) {
             signvalue = '-';
             uvalue = -value;
         } else if (flags & DP_F_PLUS)
             signvalue = '+';
         else if (flags & DP_F_SPACE)
             signvalue = ' ';
     }
     if (flags & DP_F_NUM) {
         if (base == 8)
             prefix = "0";
         if (base == 16)
             prefix = "0x";
     }
     if (flags & DP_F_UP)
         caps = 1;
     do {
         convert[place++] = (caps ? "0123456789ABCDEF" : "0123456789abcdef")
             [uvalue % (unsigned)base];
         uvalue = (uvalue / (unsigned)base);
     } while (uvalue && (place < (int)sizeof(convert)));
     if (place == sizeof(convert))
         place--;
     convert[place] = 0;
 
     zpadlen = max - place;
     spadlen =
         min - OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
     if (zpadlen < 0)
         zpadlen = 0;
     if (spadlen < 0)
         spadlen = 0;
     if (flags & DP_F_ZERO) {
         zpadlen = OSSL_MAX(zpadlen, spadlen);
         spadlen = 0;
     }
     if (flags & DP_F_MINUS)
         spadlen = -spadlen;
 
     /* spaces */
     while (spadlen > 0) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         --spadlen;
     }
 
     /* sign */
     if (signvalue)
-        doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
+            return 0;
 
     /* prefix */
     while (*prefix) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
+        if(!doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix))
+            return 0;
         prefix++;
     }
 
     /* zeros */
     if (zpadlen > 0) {
         while (zpadlen > 0) {
-            doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
+            if(!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
+                return 0;
             --zpadlen;
         }
     }
     /* digits */
-    while (place > 0)
-        doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
+    while (place > 0) {
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]))
+            return 0;
+    }
 
     /* left justified spaces */
     while (spadlen < 0) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         ++spadlen;
     }
-    return;
+    return 1;
 }
 
 static LDOUBLE abs_val(LDOUBLE value)
 {
     LDOUBLE result = value;
     if (value < 0)
         result = -value;
     return result;
 }
 
 static LDOUBLE pow_10(int in_exp)
 {
     LDOUBLE result = 1;
     while (in_exp) {
         result *= 10;
         in_exp--;
     }
     return result;
 }
 
 static long roundv(LDOUBLE value)
 {
     long intpart;
     intpart = (long)value;
     value = value - intpart;
     if (value >= 0.5)
         intpart++;
     return intpart;
 }
 
-static void
+static int
 fmtfp(char **sbuffer,
       char **buffer,
       size_t *currlen,
       size_t *maxlen, LDOUBLE fvalue, int min, int max, int flags)
 {
     int signvalue = 0;
     LDOUBLE ufvalue;
     char iconvert[20];
     char fconvert[20];
     int iplace = 0;
     int fplace = 0;
     int padlen = 0;
     int zpadlen = 0;
     long intpart;
     long fracpart;
     long max10;
 
     if (max < 0)
         max = 6;
     ufvalue = abs_val(fvalue);
     if (fvalue < 0)
         signvalue = '-';
     else if (flags & DP_F_PLUS)
         signvalue = '+';
     else if (flags & DP_F_SPACE)
         signvalue = ' ';
 
     intpart = (long)ufvalue;
 
     /*
      * sorry, we only support 9 digits past the decimal because of our
      * conversion method
      */
     if (max > 9)
         max = 9;
 
     /*
      * we "cheat" by converting the fractional part to integer by multiplying
      * by a factor of 10
      */
     max10 = roundv(pow_10(max));
     fracpart = roundv(pow_10(max) * (ufvalue - intpart));
 
     if (fracpart >= max10) {
         intpart++;
         fracpart -= max10;
     }
 
     /* convert integer part */
     do {
         iconvert[iplace++] = "0123456789"[intpart % 10];
         intpart = (intpart / 10);
     } while (intpart && (iplace < (int)sizeof(iconvert)));
     if (iplace == sizeof iconvert)
         iplace--;
     iconvert[iplace] = 0;
 
     /* convert fractional part */
     do {
         fconvert[fplace++] = "0123456789"[fracpart % 10];
         fracpart = (fracpart / 10);
     } while (fplace < max);
     if (fplace == sizeof fconvert)
         fplace--;
     fconvert[fplace] = 0;
 
     /* -1 for decimal point, another -1 if we are printing a sign */
     padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
     zpadlen = max - fplace;
     if (zpadlen < 0)
         zpadlen = 0;
     if (padlen < 0)
         padlen = 0;
     if (flags & DP_F_MINUS)
         padlen = -padlen;
 
     if ((flags & DP_F_ZERO) && (padlen > 0)) {
         if (signvalue) {
-            doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
+            if (!doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
+                return 0;
             --padlen;
             signvalue = 0;
         }
         while (padlen > 0) {
-            doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
+            if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
+                return 0;
             --padlen;
         }
     }
     while (padlen > 0) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         --padlen;
     }
-    if (signvalue)
-        doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
+    if (signvalue && !doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue))
+        return 0;
 
-    while (iplace > 0)
-        doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
+    while (iplace > 0) {
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]))
+            return 0;
+    }
 
     /*
      * Decimal point. This should probably use locale to find the correct
      * char to print out.
      */
     if (max > 0 || (flags & DP_F_NUM)) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, '.');
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '.'))
+            return 0;
 
-        while (fplace > 0)
-            doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
+        while (fplace > 0) {
+            if(!doapr_outch(sbuffer, buffer, currlen, maxlen,
+                            fconvert[--fplace]))
+                return 0;
+        }
     }
     while (zpadlen > 0) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, '0');
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, '0'))
+            return 0;
         --zpadlen;
     }
 
     while (padlen < 0) {
-        doapr_outch(sbuffer, buffer, currlen, maxlen, ' ');
+        if (!doapr_outch(sbuffer, buffer, currlen, maxlen, ' '))
+            return 0;
         ++padlen;
     }
+    return 1;
 }
 
-static void
+#define BUFFER_INC  1024
+
+static int
 doapr_outch(char **sbuffer,
             char **buffer, size_t *currlen, size_t *maxlen, int c)
 {
     /* If we haven't at least one buffer, someone has doe a big booboo */
     assert(*sbuffer != NULL || buffer != NULL);
 
     /* |currlen| must always be <= |*maxlen| */
     assert(*currlen <= *maxlen);
 
     if (buffer && *currlen == *maxlen) {
-        *maxlen += 1024;
+        if (*maxlen > INT_MAX - BUFFER_INC)
+            return 0;
+
+        *maxlen += BUFFER_INC;
         if (*buffer == NULL) {
             *buffer = OPENSSL_malloc(*maxlen);
-            if (!*buffer) {
-                /* Panic! Can't really do anything sensible. Just return */
-                return;
-            }
+            if (*buffer == NULL)
+                return 0;
             if (*currlen > 0) {
                 assert(*sbuffer != NULL);
                 memcpy(*buffer, *sbuffer, *currlen);
             }
             *sbuffer = NULL;
         } else {
-            *buffer = OPENSSL_realloc(*buffer, *maxlen);
-            if (!*buffer) {
-                /* Panic! Can't really do anything sensible. Just return */
-                return;
-            }
+            char *tmpbuf;
+            tmpbuf = OPENSSL_realloc(*buffer, *maxlen);
+            if (tmpbuf == NULL)
+                return 0;
+            *buffer = tmpbuf;
         }
     }
 
     if (*currlen < *maxlen) {
         if (*sbuffer)
             (*sbuffer)[(*currlen)++] = (char)c;
         else
             (*buffer)[(*currlen)++] = (char)c;
     }
 
-    return;
+    return 1;
 }
 
 /***************************************************************************/
 
 int BIO_printf(BIO *bio, const char *format, ...)
 {
     va_list args;
     int ret;
 
     va_start(args, format);
 
     ret = BIO_vprintf(bio, format, args);
 
     va_end(args);
     return (ret);
 }
 
 int BIO_vprintf(BIO *bio, const char *format, va_list args)
 {
     int ret;
     size_t retlen;
     char hugebuf[1024 * 2];     /* Was previously 10k, which is unreasonable
                                  * in small-stack environments, like threads
                                  * or DOS programs. */
     char *hugebufp = hugebuf;
     size_t hugebufsize = sizeof(hugebuf);
     char *dynbuf = NULL;
     int ignored;
 
     dynbuf = NULL;
     CRYPTO_push_info("doapr()");
-    _dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format, args);
+    if (!_dopr(&hugebufp, &dynbuf, &hugebufsize, &retlen, &ignored, format,
+                args)) {
+        OPENSSL_free(dynbuf);
+        return -1;
+    }
     if (dynbuf) {
         ret = BIO_write(bio, dynbuf, (int)retlen);
         OPENSSL_free(dynbuf);
     } else {
         ret = BIO_write(bio, hugebuf, (int)retlen);
     }
     CRYPTO_pop_info();
     return (ret);
 }
 
 /*
  * As snprintf is not available everywhere, we provide our own
  * implementation. This function has nothing to do with BIOs, but it's
  * closely related to BIO_printf, and we need *some* name prefix ... (XXX the
  * function should be renamed, but to what?)
  */
 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
 {
     va_list args;
     int ret;
 
     va_start(args, format);
 
     ret = BIO_vsnprintf(buf, n, format, args);
 
     va_end(args);
     return (ret);
 }
 
 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
 {
     size_t retlen;
     int truncated;
 
-    _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
+    if(!_dopr(&buf, NULL, &n, &retlen, &truncated, format, args))
+        return -1;
 
     if (truncated)
         /*
          * In case of truncation, return -1 like traditional snprintf.
          * (Current drafts for ISO/IEC 9899 say snprintf should return the
          * number of characters that would have been written, had the buffer
          * been large enough.)
          */
         return -1;
     else
         return (retlen <= INT_MAX) ? (int)retlen : -1;
 }
Index: vendor-crypto/openssl/dist/crypto/bio/bio.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/bio/bio.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bio/bio.h	(revision 296273)
@@ -1,883 +1,883 @@
 /* crypto/bio/bio.h */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #ifndef HEADER_BIO_H
 # define HEADER_BIO_H
 
 # include 
 
 # ifndef OPENSSL_NO_FP_API
 #  include 
 # endif
 # include 
 
 # include 
 
 # ifndef OPENSSL_NO_SCTP
 #  ifndef OPENSSL_SYS_VMS
 #   include 
 #  else
 #   include 
 #  endif
 # endif
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 /* These are the 'types' of BIOs */
 # define BIO_TYPE_NONE           0
 # define BIO_TYPE_MEM            (1|0x0400)
 # define BIO_TYPE_FILE           (2|0x0400)
 
 # define BIO_TYPE_FD             (4|0x0400|0x0100)
 # define BIO_TYPE_SOCKET         (5|0x0400|0x0100)
 # define BIO_TYPE_NULL           (6|0x0400)
 # define BIO_TYPE_SSL            (7|0x0200)
 # define BIO_TYPE_MD             (8|0x0200)/* passive filter */
 # define BIO_TYPE_BUFFER         (9|0x0200)/* filter */
 # define BIO_TYPE_CIPHER         (10|0x0200)/* filter */
 # define BIO_TYPE_BASE64         (11|0x0200)/* filter */
 # define BIO_TYPE_CONNECT        (12|0x0400|0x0100)/* socket - connect */
 # define BIO_TYPE_ACCEPT         (13|0x0400|0x0100)/* socket for accept */
 # define BIO_TYPE_PROXY_CLIENT   (14|0x0200)/* client proxy BIO */
 # define BIO_TYPE_PROXY_SERVER   (15|0x0200)/* server proxy BIO */
 # define BIO_TYPE_NBIO_TEST      (16|0x0200)/* server proxy BIO */
 # define BIO_TYPE_NULL_FILTER    (17|0x0200)
 # define BIO_TYPE_BER            (18|0x0200)/* BER -> bin filter */
 # define BIO_TYPE_BIO            (19|0x0400)/* (half a) BIO pair */
 # define BIO_TYPE_LINEBUFFER     (20|0x0200)/* filter */
 # define BIO_TYPE_DGRAM          (21|0x0400|0x0100)
 # ifndef OPENSSL_NO_SCTP
 #  define BIO_TYPE_DGRAM_SCTP     (24|0x0400|0x0100)
 # endif
 # define BIO_TYPE_ASN1           (22|0x0200)/* filter */
 # define BIO_TYPE_COMP           (23|0x0200)/* filter */
 
 # define BIO_TYPE_DESCRIPTOR     0x0100/* socket, fd, connect or accept */
 # define BIO_TYPE_FILTER         0x0200
 # define BIO_TYPE_SOURCE_SINK    0x0400
 
 /*
  * BIO_FILENAME_READ|BIO_CLOSE to open or close on free.
  * BIO_set_fp(in,stdin,BIO_NOCLOSE);
  */
 # define BIO_NOCLOSE             0x00
 # define BIO_CLOSE               0x01
 
 /*
  * These are used in the following macros and are passed to BIO_ctrl()
  */
 # define BIO_CTRL_RESET          1/* opt - rewind/zero etc */
 # define BIO_CTRL_EOF            2/* opt - are we at the eof */
 # define BIO_CTRL_INFO           3/* opt - extra tit-bits */
 # define BIO_CTRL_SET            4/* man - set the 'IO' type */
 # define BIO_CTRL_GET            5/* man - get the 'IO' type */
 # define BIO_CTRL_PUSH           6/* opt - internal, used to signify change */
 # define BIO_CTRL_POP            7/* opt - internal, used to signify change */
 # define BIO_CTRL_GET_CLOSE      8/* man - set the 'close' on free */
 # define BIO_CTRL_SET_CLOSE      9/* man - set the 'close' on free */
 # define BIO_CTRL_PENDING        10/* opt - is their more data buffered */
 # define BIO_CTRL_FLUSH          11/* opt - 'flush' buffered output */
 # define BIO_CTRL_DUP            12/* man - extra stuff for 'duped' BIO */
 # define BIO_CTRL_WPENDING       13/* opt - number of bytes still to write */
 /* callback is int cb(BIO *bio,state,ret); */
 # define BIO_CTRL_SET_CALLBACK   14/* opt - set callback function */
 # define BIO_CTRL_GET_CALLBACK   15/* opt - set callback function */
 
 # define BIO_CTRL_SET_FILENAME   30/* BIO_s_file special */
 
 /* dgram BIO stuff */
 # define BIO_CTRL_DGRAM_CONNECT       31/* BIO dgram special */
 # define BIO_CTRL_DGRAM_SET_CONNECTED 32/* allow for an externally connected
                                          * socket to be passed in */
 # define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33/* setsockopt, essentially */
 # define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34/* getsockopt, essentially */
 # define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35/* setsockopt, essentially */
 # define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36/* getsockopt, essentially */
 
 # define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37/* flag whether the last */
 # define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38/* I/O operation tiemd out */
 
 /* #ifdef IP_MTU_DISCOVER */
 # define BIO_CTRL_DGRAM_MTU_DISCOVER       39/* set DF bit on egress packets */
 /* #endif */
 
 # define BIO_CTRL_DGRAM_QUERY_MTU          40/* as kernel for current MTU */
 # define BIO_CTRL_DGRAM_GET_FALLBACK_MTU   47
 # define BIO_CTRL_DGRAM_GET_MTU            41/* get cached value for MTU */
 # define BIO_CTRL_DGRAM_SET_MTU            42/* set cached value for MTU.
                                               * want to use this if asking
                                               * the kernel fails */
 
 # define BIO_CTRL_DGRAM_MTU_EXCEEDED       43/* check whether the MTU was
                                               * exceed in the previous write
                                               * operation */
 
 # define BIO_CTRL_DGRAM_GET_PEER           46
 # define BIO_CTRL_DGRAM_SET_PEER           44/* Destination for the data */
 
 # define BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT   45/* Next DTLS handshake timeout
                                               * to adjust socket timeouts */
 # define BIO_CTRL_DGRAM_SET_DONT_FRAG      48
 
 # define BIO_CTRL_DGRAM_GET_MTU_OVERHEAD   49
 
 # ifndef OPENSSL_NO_SCTP
 /* SCTP stuff */
 #  define BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE    50
 #  define BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY                51
 #  define BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY               52
 #  define BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD               53
 #  define BIO_CTRL_DGRAM_SCTP_GET_SNDINFO         60
 #  define BIO_CTRL_DGRAM_SCTP_SET_SNDINFO         61
 #  define BIO_CTRL_DGRAM_SCTP_GET_RCVINFO         62
 #  define BIO_CTRL_DGRAM_SCTP_SET_RCVINFO         63
 #  define BIO_CTRL_DGRAM_SCTP_GET_PRINFO                  64
 #  define BIO_CTRL_DGRAM_SCTP_SET_PRINFO                  65
 #  define BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN               70
 # endif
 
 /* modifiers */
 # define BIO_FP_READ             0x02
 # define BIO_FP_WRITE            0x04
 # define BIO_FP_APPEND           0x08
 # define BIO_FP_TEXT             0x10
 
 # define BIO_FLAGS_READ          0x01
 # define BIO_FLAGS_WRITE         0x02
 # define BIO_FLAGS_IO_SPECIAL    0x04
 # define BIO_FLAGS_RWS (BIO_FLAGS_READ|BIO_FLAGS_WRITE|BIO_FLAGS_IO_SPECIAL)
 # define BIO_FLAGS_SHOULD_RETRY  0x08
 # ifndef BIO_FLAGS_UPLINK
 /*
  * "UPLINK" flag denotes file descriptors provided by application. It
  * defaults to 0, as most platforms don't require UPLINK interface.
  */
 #  define BIO_FLAGS_UPLINK        0
 # endif
 
 /* Used in BIO_gethostbyname() */
 # define BIO_GHBN_CTRL_HITS              1
 # define BIO_GHBN_CTRL_MISSES            2
 # define BIO_GHBN_CTRL_CACHE_SIZE        3
 # define BIO_GHBN_CTRL_GET_ENTRY         4
 # define BIO_GHBN_CTRL_FLUSH             5
 
 /* Mostly used in the SSL BIO */
 /*-
  * Not used anymore
  * #define BIO_FLAGS_PROTOCOL_DELAYED_READ 0x10
  * #define BIO_FLAGS_PROTOCOL_DELAYED_WRITE 0x20
  * #define BIO_FLAGS_PROTOCOL_STARTUP   0x40
  */
 
 # define BIO_FLAGS_BASE64_NO_NL  0x100
 
 /*
  * This is used with memory BIOs: it means we shouldn't free up or change the
  * data in any way.
  */
 # define BIO_FLAGS_MEM_RDONLY    0x200
 
 typedef struct bio_st BIO;
 
 void BIO_set_flags(BIO *b, int flags);
 int BIO_test_flags(const BIO *b, int flags);
 void BIO_clear_flags(BIO *b, int flags);
 
 # define BIO_get_flags(b) BIO_test_flags(b, ~(0x0))
 # define BIO_set_retry_special(b) \
                 BIO_set_flags(b, (BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY))
 # define BIO_set_retry_read(b) \
                 BIO_set_flags(b, (BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY))
 # define BIO_set_retry_write(b) \
                 BIO_set_flags(b, (BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY))
 
 /* These are normally used internally in BIOs */
 # define BIO_clear_retry_flags(b) \
                 BIO_clear_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
 # define BIO_get_retry_flags(b) \
                 BIO_test_flags(b, (BIO_FLAGS_RWS|BIO_FLAGS_SHOULD_RETRY))
 
 /* These should be used by the application to tell why we should retry */
 # define BIO_should_read(a)              BIO_test_flags(a, BIO_FLAGS_READ)
 # define BIO_should_write(a)             BIO_test_flags(a, BIO_FLAGS_WRITE)
 # define BIO_should_io_special(a)        BIO_test_flags(a, BIO_FLAGS_IO_SPECIAL)
 # define BIO_retry_type(a)               BIO_test_flags(a, BIO_FLAGS_RWS)
 # define BIO_should_retry(a)             BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
 
 /*
  * The next three are used in conjunction with the BIO_should_io_special()
  * condition.  After this returns true, BIO *BIO_get_retry_BIO(BIO *bio, int
  * *reason); will walk the BIO stack and return the 'reason' for the special
  * and the offending BIO. Given a BIO, BIO_get_retry_reason(bio) will return
  * the code.
  */
 /*
  * Returned from the SSL bio when the certificate retrieval code had an error
  */
 # define BIO_RR_SSL_X509_LOOKUP          0x01
 /* Returned from the connect BIO when a connect would have blocked */
 # define BIO_RR_CONNECT                  0x02
 /* Returned from the accept BIO when an accept would have blocked */
 # define BIO_RR_ACCEPT                   0x03
 
 /* These are passed by the BIO callback */
 # define BIO_CB_FREE     0x01
 # define BIO_CB_READ     0x02
 # define BIO_CB_WRITE    0x03
 # define BIO_CB_PUTS     0x04
 # define BIO_CB_GETS     0x05
 # define BIO_CB_CTRL     0x06
 
 /*
  * The callback is called before and after the underling operation, The
  * BIO_CB_RETURN flag indicates if it is after the call
  */
 # define BIO_CB_RETURN   0x80
 # define BIO_CB_return(a) ((a)|BIO_CB_RETURN)
 # define BIO_cb_pre(a)   (!((a)&BIO_CB_RETURN))
 # define BIO_cb_post(a)  ((a)&BIO_CB_RETURN)
 
 long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
                                         int, long, long);
 void BIO_set_callback(BIO *b,
                       long (*callback) (struct bio_st *, int, const char *,
                                         int, long, long));
 char *BIO_get_callback_arg(const BIO *b);
 void BIO_set_callback_arg(BIO *b, char *arg);
 
 const char *BIO_method_name(const BIO *b);
 int BIO_method_type(const BIO *b);
 
 typedef void bio_info_cb (struct bio_st *, int, const char *, int, long,
                           long);
 
 typedef struct bio_method_st {
     int type;
     const char *name;
     int (*bwrite) (BIO *, const char *, int);
     int (*bread) (BIO *, char *, int);
     int (*bputs) (BIO *, const char *);
     int (*bgets) (BIO *, char *, int);
     long (*ctrl) (BIO *, int, long, void *);
     int (*create) (BIO *);
     int (*destroy) (BIO *);
     long (*callback_ctrl) (BIO *, int, bio_info_cb *);
 } BIO_METHOD;
 
 struct bio_st {
     BIO_METHOD *method;
     /* bio, mode, argp, argi, argl, ret */
     long (*callback) (struct bio_st *, int, const char *, int, long, long);
     char *cb_arg;               /* first argument for the callback */
     int init;
     int shutdown;
     int flags;                  /* extra storage */
     int retry_reason;
     int num;
     void *ptr;
     struct bio_st *next_bio;    /* used by filter BIOs */
     struct bio_st *prev_bio;    /* used by filter BIOs */
     int references;
     unsigned long num_read;
     unsigned long num_write;
     CRYPTO_EX_DATA ex_data;
 };
 
 DECLARE_STACK_OF(BIO)
 
 typedef struct bio_f_buffer_ctx_struct {
     /*-
      * Buffers are setup like this:
      *
      * <---------------------- size ----------------------->
      * +---------------------------------------------------+
      * | consumed | remaining          | free space        |
      * +---------------------------------------------------+
      * <-- off --><------- len ------->
      */
     /*- BIO *bio; *//*
      * this is now in the BIO struct
      */
     int ibuf_size;              /* how big is the input buffer */
     int obuf_size;              /* how big is the output buffer */
     char *ibuf;                 /* the char array */
     int ibuf_len;               /* how many bytes are in it */
     int ibuf_off;               /* write/read offset */
     char *obuf;                 /* the char array */
     int obuf_len;               /* how many bytes are in it */
     int obuf_off;               /* write/read offset */
 } BIO_F_BUFFER_CTX;
 
 /* Prefix and suffix callback in ASN1 BIO */
 typedef int asn1_ps_func (BIO *b, unsigned char **pbuf, int *plen,
                           void *parg);
 
 # ifndef OPENSSL_NO_SCTP
 /* SCTP parameter structs */
 struct bio_dgram_sctp_sndinfo {
     uint16_t snd_sid;
     uint16_t snd_flags;
     uint32_t snd_ppid;
     uint32_t snd_context;
 };
 
 struct bio_dgram_sctp_rcvinfo {
     uint16_t rcv_sid;
     uint16_t rcv_ssn;
     uint16_t rcv_flags;
     uint32_t rcv_ppid;
     uint32_t rcv_tsn;
     uint32_t rcv_cumtsn;
     uint32_t rcv_context;
 };
 
 struct bio_dgram_sctp_prinfo {
     uint16_t pr_policy;
     uint32_t pr_value;
 };
 # endif
 
 /* connect BIO stuff */
 # define BIO_CONN_S_BEFORE               1
 # define BIO_CONN_S_GET_IP               2
 # define BIO_CONN_S_GET_PORT             3
 # define BIO_CONN_S_CREATE_SOCKET        4
 # define BIO_CONN_S_CONNECT              5
 # define BIO_CONN_S_OK                   6
 # define BIO_CONN_S_BLOCKED_CONNECT      7
 # define BIO_CONN_S_NBIO                 8
 /*
  * #define BIO_CONN_get_param_hostname BIO_ctrl
  */
 
 # define BIO_C_SET_CONNECT                       100
 # define BIO_C_DO_STATE_MACHINE                  101
 # define BIO_C_SET_NBIO                          102
 # define BIO_C_SET_PROXY_PARAM                   103
 # define BIO_C_SET_FD                            104
 # define BIO_C_GET_FD                            105
 # define BIO_C_SET_FILE_PTR                      106
 # define BIO_C_GET_FILE_PTR                      107
 # define BIO_C_SET_FILENAME                      108
 # define BIO_C_SET_SSL                           109
 # define BIO_C_GET_SSL                           110
 # define BIO_C_SET_MD                            111
 # define BIO_C_GET_MD                            112
 # define BIO_C_GET_CIPHER_STATUS                 113
 # define BIO_C_SET_BUF_MEM                       114
 # define BIO_C_GET_BUF_MEM_PTR                   115
 # define BIO_C_GET_BUFF_NUM_LINES                116
 # define BIO_C_SET_BUFF_SIZE                     117
 # define BIO_C_SET_ACCEPT                        118
 # define BIO_C_SSL_MODE                          119
 # define BIO_C_GET_MD_CTX                        120
 # define BIO_C_GET_PROXY_PARAM                   121
 # define BIO_C_SET_BUFF_READ_DATA                122/* data to read first */
 # define BIO_C_GET_CONNECT                       123
 # define BIO_C_GET_ACCEPT                        124
 # define BIO_C_SET_SSL_RENEGOTIATE_BYTES         125
 # define BIO_C_GET_SSL_NUM_RENEGOTIATES          126
 # define BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT       127
 # define BIO_C_FILE_SEEK                         128
 # define BIO_C_GET_CIPHER_CTX                    129
 # define BIO_C_SET_BUF_MEM_EOF_RETURN            130/* return end of input
                                                      * value */
 # define BIO_C_SET_BIND_MODE                     131
 # define BIO_C_GET_BIND_MODE                     132
 # define BIO_C_FILE_TELL                         133
 # define BIO_C_GET_SOCKS                         134
 # define BIO_C_SET_SOCKS                         135
 
 # define BIO_C_SET_WRITE_BUF_SIZE                136/* for BIO_s_bio */
 # define BIO_C_GET_WRITE_BUF_SIZE                137
 # define BIO_C_MAKE_BIO_PAIR                     138
 # define BIO_C_DESTROY_BIO_PAIR                  139
 # define BIO_C_GET_WRITE_GUARANTEE               140
 # define BIO_C_GET_READ_REQUEST                  141
 # define BIO_C_SHUTDOWN_WR                       142
 # define BIO_C_NREAD0                            143
 # define BIO_C_NREAD                             144
 # define BIO_C_NWRITE0                           145
 # define BIO_C_NWRITE                            146
 # define BIO_C_RESET_READ_REQUEST                147
 # define BIO_C_SET_MD_CTX                        148
 
 # define BIO_C_SET_PREFIX                        149
 # define BIO_C_GET_PREFIX                        150
 # define BIO_C_SET_SUFFIX                        151
 # define BIO_C_GET_SUFFIX                        152
 
 # define BIO_C_SET_EX_ARG                        153
 # define BIO_C_GET_EX_ARG                        154
 
 # define BIO_set_app_data(s,arg)         BIO_set_ex_data(s,0,arg)
 # define BIO_get_app_data(s)             BIO_get_ex_data(s,0)
 
 /* BIO_s_connect() and BIO_s_socks4a_connect() */
 # define BIO_set_conn_hostname(b,name) BIO_ctrl(b,BIO_C_SET_CONNECT,0,(char *)name)
 # define BIO_set_conn_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,1,(char *)port)
 # define BIO_set_conn_ip(b,ip)     BIO_ctrl(b,BIO_C_SET_CONNECT,2,(char *)ip)
 # define BIO_set_conn_int_port(b,port) BIO_ctrl(b,BIO_C_SET_CONNECT,3,(char *)port)
 # define BIO_get_conn_hostname(b)  BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,0)
 # define BIO_get_conn_port(b)      BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,1)
 # define BIO_get_conn_ip(b)               BIO_ptr_ctrl(b,BIO_C_GET_CONNECT,2)
-# define BIO_get_conn_int_port(b) BIO_ctrl(b,BIO_C_GET_CONNECT,3,0,NULL)
+# define BIO_get_conn_int_port(b) BIO_ctrl(b,BIO_C_GET_CONNECT,3,NULL)
 
 # define BIO_set_nbio(b,n)       BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL)
 
 /* BIO_s_accept() */
 # define BIO_set_accept_port(b,name) BIO_ctrl(b,BIO_C_SET_ACCEPT,0,(char *)name)
 # define BIO_get_accept_port(b)  BIO_ptr_ctrl(b,BIO_C_GET_ACCEPT,0)
 /* #define BIO_set_nbio(b,n)    BIO_ctrl(b,BIO_C_SET_NBIO,(n),NULL) */
 # define BIO_set_nbio_accept(b,n) BIO_ctrl(b,BIO_C_SET_ACCEPT,1,(n)?(void *)"a":NULL)
 # define BIO_set_accept_bios(b,bio) BIO_ctrl(b,BIO_C_SET_ACCEPT,2,(char *)bio)
 
 # define BIO_BIND_NORMAL                 0
 # define BIO_BIND_REUSEADDR_IF_UNUSED    1
 # define BIO_BIND_REUSEADDR              2
 # define BIO_set_bind_mode(b,mode) BIO_ctrl(b,BIO_C_SET_BIND_MODE,mode,NULL)
 # define BIO_get_bind_mode(b,mode) BIO_ctrl(b,BIO_C_GET_BIND_MODE,0,NULL)
 
 /* BIO_s_accept() and BIO_s_connect() */
 # define BIO_do_connect(b)       BIO_do_handshake(b)
 # define BIO_do_accept(b)        BIO_do_handshake(b)
 # define BIO_do_handshake(b)     BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
 
 /* BIO_s_proxy_client() */
 # define BIO_set_url(b,url)      BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,0,(char *)(url))
 # define BIO_set_proxies(b,p)    BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,1,(char *)(p))
 /* BIO_set_nbio(b,n) */
 # define BIO_set_filter_bio(b,s) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,2,(char *)(s))
 /* BIO *BIO_get_filter_bio(BIO *bio); */
 # define BIO_set_proxy_cb(b,cb) BIO_callback_ctrl(b,BIO_C_SET_PROXY_PARAM,3,(void *(*cb)()))
 # define BIO_set_proxy_header(b,sk) BIO_ctrl(b,BIO_C_SET_PROXY_PARAM,4,(char *)sk)
 # define BIO_set_no_connect_return(b,bool) BIO_int_ctrl(b,BIO_C_SET_PROXY_PARAM,5,bool)
 
 # define BIO_get_proxy_header(b,skp) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,0,(char *)skp)
 # define BIO_get_proxies(b,pxy_p) BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,1,(char *)(pxy_p))
 # define BIO_get_url(b,url)      BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,2,(char *)(url))
 # define BIO_get_no_connect_return(b)    BIO_ctrl(b,BIO_C_GET_PROXY_PARAM,5,NULL)
 
 /* BIO_s_datagram(), BIO_s_fd(), BIO_s_socket(), BIO_s_accept() and BIO_s_connect() */
 # define BIO_set_fd(b,fd,c)      BIO_int_ctrl(b,BIO_C_SET_FD,c,fd)
 # define BIO_get_fd(b,c)         BIO_ctrl(b,BIO_C_GET_FD,0,(char *)c)
 
 /* BIO_s_file() */
 # define BIO_set_fp(b,fp,c)      BIO_ctrl(b,BIO_C_SET_FILE_PTR,c,(char *)fp)
 # define BIO_get_fp(b,fpp)       BIO_ctrl(b,BIO_C_GET_FILE_PTR,0,(char *)fpp)
 
 /* BIO_s_fd() and BIO_s_file() */
 # define BIO_seek(b,ofs) (int)BIO_ctrl(b,BIO_C_FILE_SEEK,ofs,NULL)
 # define BIO_tell(b)     (int)BIO_ctrl(b,BIO_C_FILE_TELL,0,NULL)
 
 /*
  * name is cast to lose const, but might be better to route through a
  * function so we can do it safely
  */
 # ifdef CONST_STRICT
 /*
  * If you are wondering why this isn't defined, its because CONST_STRICT is
  * purely a compile-time kludge to allow const to be checked.
  */
 int BIO_read_filename(BIO *b, const char *name);
 # else
 #  define BIO_read_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
                 BIO_CLOSE|BIO_FP_READ,(char *)name)
 # endif
 # define BIO_write_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
                 BIO_CLOSE|BIO_FP_WRITE,name)
 # define BIO_append_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
                 BIO_CLOSE|BIO_FP_APPEND,name)
 # define BIO_rw_filename(b,name) BIO_ctrl(b,BIO_C_SET_FILENAME, \
                 BIO_CLOSE|BIO_FP_READ|BIO_FP_WRITE,name)
 
 /*
  * WARNING WARNING, this ups the reference count on the read bio of the SSL
  * structure.  This is because the ssl read BIO is now pointed to by the
  * next_bio field in the bio.  So when you free the BIO, make sure you are
  * doing a BIO_free_all() to catch the underlying BIO.
  */
 # define BIO_set_ssl(b,ssl,c)    BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
 # define BIO_get_ssl(b,sslp)     BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
 # define BIO_set_ssl_mode(b,client)      BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
 # define BIO_set_ssl_renegotiate_bytes(b,num) \
         BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
 # define BIO_get_num_renegotiates(b) \
         BIO_ctrl(b,BIO_C_GET_SSL_NUM_RENEGOTIATES,0,NULL);
 # define BIO_set_ssl_renegotiate_timeout(b,seconds) \
         BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
 
 /* defined in evp.h */
 /* #define BIO_set_md(b,md)     BIO_ctrl(b,BIO_C_SET_MD,1,(char *)md) */
 
 # define BIO_get_mem_data(b,pp)  BIO_ctrl(b,BIO_CTRL_INFO,0,(char *)pp)
 # define BIO_set_mem_buf(b,bm,c) BIO_ctrl(b,BIO_C_SET_BUF_MEM,c,(char *)bm)
 # define BIO_get_mem_ptr(b,pp)   BIO_ctrl(b,BIO_C_GET_BUF_MEM_PTR,0,(char *)pp)
 # define BIO_set_mem_eof_return(b,v) \
                                 BIO_ctrl(b,BIO_C_SET_BUF_MEM_EOF_RETURN,v,NULL)
 
 /* For the BIO_f_buffer() type */
 # define BIO_get_buffer_num_lines(b)     BIO_ctrl(b,BIO_C_GET_BUFF_NUM_LINES,0,NULL)
 # define BIO_set_buffer_size(b,size)     BIO_ctrl(b,BIO_C_SET_BUFF_SIZE,size,NULL)
 # define BIO_set_read_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,0)
 # define BIO_set_write_buffer_size(b,size) BIO_int_ctrl(b,BIO_C_SET_BUFF_SIZE,size,1)
 # define BIO_set_buffer_read_data(b,buf,num) BIO_ctrl(b,BIO_C_SET_BUFF_READ_DATA,num,buf)
 
 /* Don't use the next one unless you know what you are doing :-) */
 # define BIO_dup_state(b,ret)    BIO_ctrl(b,BIO_CTRL_DUP,0,(char *)(ret))
 
 # define BIO_reset(b)            (int)BIO_ctrl(b,BIO_CTRL_RESET,0,NULL)
 # define BIO_eof(b)              (int)BIO_ctrl(b,BIO_CTRL_EOF,0,NULL)
 # define BIO_set_close(b,c)      (int)BIO_ctrl(b,BIO_CTRL_SET_CLOSE,(c),NULL)
 # define BIO_get_close(b)        (int)BIO_ctrl(b,BIO_CTRL_GET_CLOSE,0,NULL)
 # define BIO_pending(b)          (int)BIO_ctrl(b,BIO_CTRL_PENDING,0,NULL)
 # define BIO_wpending(b)         (int)BIO_ctrl(b,BIO_CTRL_WPENDING,0,NULL)
 /* ...pending macros have inappropriate return type */
 size_t BIO_ctrl_pending(BIO *b);
 size_t BIO_ctrl_wpending(BIO *b);
 # define BIO_flush(b)            (int)BIO_ctrl(b,BIO_CTRL_FLUSH,0,NULL)
 # define BIO_get_info_callback(b,cbp) (int)BIO_ctrl(b,BIO_CTRL_GET_CALLBACK,0, \
                                                    cbp)
 # define BIO_set_info_callback(b,cb) (int)BIO_callback_ctrl(b,BIO_CTRL_SET_CALLBACK,cb)
 
 /* For the BIO_f_buffer() type */
 # define BIO_buffer_get_num_lines(b) BIO_ctrl(b,BIO_CTRL_GET,0,NULL)
 
 /* For BIO_s_bio() */
 # define BIO_set_write_buf_size(b,size) (int)BIO_ctrl(b,BIO_C_SET_WRITE_BUF_SIZE,size,NULL)
 # define BIO_get_write_buf_size(b,size) (size_t)BIO_ctrl(b,BIO_C_GET_WRITE_BUF_SIZE,size,NULL)
 # define BIO_make_bio_pair(b1,b2)   (int)BIO_ctrl(b1,BIO_C_MAKE_BIO_PAIR,0,b2)
 # define BIO_destroy_bio_pair(b)    (int)BIO_ctrl(b,BIO_C_DESTROY_BIO_PAIR,0,NULL)
 # define BIO_shutdown_wr(b) (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL)
 /* macros with inappropriate type -- but ...pending macros use int too: */
 # define BIO_get_write_guarantee(b) (int)BIO_ctrl(b,BIO_C_GET_WRITE_GUARANTEE,0,NULL)
 # define BIO_get_read_request(b)    (int)BIO_ctrl(b,BIO_C_GET_READ_REQUEST,0,NULL)
 size_t BIO_ctrl_get_write_guarantee(BIO *b);
 size_t BIO_ctrl_get_read_request(BIO *b);
 int BIO_ctrl_reset_read_request(BIO *b);
 
 /* ctrl macros for dgram */
 # define BIO_ctrl_dgram_connect(b,peer)  \
                      (int)BIO_ctrl(b,BIO_CTRL_DGRAM_CONNECT,0, (char *)peer)
 # define BIO_ctrl_set_connected(b, state, peer) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_CONNECTED, state, (char *)peer)
 # define BIO_dgram_recv_timedout(b) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP, 0, NULL)
 # define BIO_dgram_send_timedout(b) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP, 0, NULL)
 # define BIO_dgram_get_peer(b,peer) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_GET_PEER, 0, (char *)peer)
 # define BIO_dgram_set_peer(b,peer) \
          (int)BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, (char *)peer)
 # define BIO_dgram_get_mtu_overhead(b) \
          (unsigned int)BIO_ctrl((b), BIO_CTRL_DGRAM_GET_MTU_OVERHEAD, 0, NULL)
 
 /* These two aren't currently implemented */
 /* int BIO_get_ex_num(BIO *bio); */
 /* void BIO_set_ex_free_func(BIO *bio,int idx,void (*cb)()); */
 int BIO_set_ex_data(BIO *bio, int idx, void *data);
 void *BIO_get_ex_data(BIO *bio, int idx);
 int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
                          CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
 unsigned long BIO_number_read(BIO *bio);
 unsigned long BIO_number_written(BIO *bio);
 
 /* For BIO_f_asn1() */
 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
                         asn1_ps_func *prefix_free);
 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
                         asn1_ps_func **pprefix_free);
 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
                         asn1_ps_func *suffix_free);
 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
                         asn1_ps_func **psuffix_free);
 
 # ifndef OPENSSL_NO_FP_API
 BIO_METHOD *BIO_s_file(void);
 BIO *BIO_new_file(const char *filename, const char *mode);
 BIO *BIO_new_fp(FILE *stream, int close_flag);
 #  define BIO_s_file_internal    BIO_s_file
 # endif
 BIO *BIO_new(BIO_METHOD *type);
 int BIO_set(BIO *a, BIO_METHOD *type);
 int BIO_free(BIO *a);
 void BIO_vfree(BIO *a);
 int BIO_read(BIO *b, void *data, int len);
 int BIO_gets(BIO *bp, char *buf, int size);
 int BIO_write(BIO *b, const void *data, int len);
 int BIO_puts(BIO *bp, const char *buf);
 int BIO_indent(BIO *b, int indent, int max);
 long BIO_ctrl(BIO *bp, int cmd, long larg, void *parg);
 long BIO_callback_ctrl(BIO *b, int cmd,
                        void (*fp) (struct bio_st *, int, const char *, int,
                                    long, long));
 char *BIO_ptr_ctrl(BIO *bp, int cmd, long larg);
 long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg);
 BIO *BIO_push(BIO *b, BIO *append);
 BIO *BIO_pop(BIO *b);
 void BIO_free_all(BIO *a);
 BIO *BIO_find_type(BIO *b, int bio_type);
 BIO *BIO_next(BIO *b);
 BIO *BIO_get_retry_BIO(BIO *bio, int *reason);
 int BIO_get_retry_reason(BIO *bio);
 BIO *BIO_dup_chain(BIO *in);
 
 int BIO_nread0(BIO *bio, char **buf);
 int BIO_nread(BIO *bio, char **buf, int num);
 int BIO_nwrite0(BIO *bio, char **buf);
 int BIO_nwrite(BIO *bio, char **buf, int num);
 
 long BIO_debug_callback(BIO *bio, int cmd, const char *argp, int argi,
                         long argl, long ret);
 
 BIO_METHOD *BIO_s_mem(void);
-BIO *BIO_new_mem_buf(void *buf, int len);
+BIO *BIO_new_mem_buf(const void *buf, int len);
 BIO_METHOD *BIO_s_socket(void);
 BIO_METHOD *BIO_s_connect(void);
 BIO_METHOD *BIO_s_accept(void);
 BIO_METHOD *BIO_s_fd(void);
 # ifndef OPENSSL_SYS_OS2
 BIO_METHOD *BIO_s_log(void);
 # endif
 BIO_METHOD *BIO_s_bio(void);
 BIO_METHOD *BIO_s_null(void);
 BIO_METHOD *BIO_f_null(void);
 BIO_METHOD *BIO_f_buffer(void);
 # ifdef OPENSSL_SYS_VMS
 BIO_METHOD *BIO_f_linebuffer(void);
 # endif
 BIO_METHOD *BIO_f_nbio_test(void);
 # ifndef OPENSSL_NO_DGRAM
 BIO_METHOD *BIO_s_datagram(void);
 #  ifndef OPENSSL_NO_SCTP
 BIO_METHOD *BIO_s_datagram_sctp(void);
 #  endif
 # endif
 
 /* BIO_METHOD *BIO_f_ber(void); */
 
 int BIO_sock_should_retry(int i);
 int BIO_sock_non_fatal_error(int error);
 int BIO_dgram_non_fatal_error(int error);
 
 int BIO_fd_should_retry(int i);
 int BIO_fd_non_fatal_error(int error);
 int BIO_dump_cb(int (*cb) (const void *data, size_t len, void *u),
                 void *u, const char *s, int len);
 int BIO_dump_indent_cb(int (*cb) (const void *data, size_t len, void *u),
                        void *u, const char *s, int len, int indent);
 int BIO_dump(BIO *b, const char *bytes, int len);
 int BIO_dump_indent(BIO *b, const char *bytes, int len, int indent);
 # ifndef OPENSSL_NO_FP_API
 int BIO_dump_fp(FILE *fp, const char *s, int len);
 int BIO_dump_indent_fp(FILE *fp, const char *s, int len, int indent);
 # endif
 int BIO_hex_string(BIO *out, int indent, int width, unsigned char *data,
                    int datalen);
 
 struct hostent *BIO_gethostbyname(const char *name);
 /*-
  * We might want a thread-safe interface too:
  * struct hostent *BIO_gethostbyname_r(const char *name,
  *     struct hostent *result, void *buffer, size_t buflen);
  * or something similar (caller allocates a struct hostent,
  * pointed to by "result", and additional buffer space for the various
  * substructures; if the buffer does not suffice, NULL is returned
  * and an appropriate error code is set).
  */
 int BIO_sock_error(int sock);
 int BIO_socket_ioctl(int fd, long type, void *arg);
 int BIO_socket_nbio(int fd, int mode);
 int BIO_get_port(const char *str, unsigned short *port_ptr);
 int BIO_get_host_ip(const char *str, unsigned char *ip);
 int BIO_get_accept_socket(char *host_port, int mode);
 int BIO_accept(int sock, char **ip_port);
 int BIO_sock_init(void);
 void BIO_sock_cleanup(void);
 int BIO_set_tcp_ndelay(int sock, int turn_on);
 
 BIO *BIO_new_socket(int sock, int close_flag);
 BIO *BIO_new_dgram(int fd, int close_flag);
 # ifndef OPENSSL_NO_SCTP
 BIO *BIO_new_dgram_sctp(int fd, int close_flag);
 int BIO_dgram_is_sctp(BIO *bio);
 int BIO_dgram_sctp_notification_cb(BIO *b,
                                    void (*handle_notifications) (BIO *bio,
                                                                  void
                                                                  *context,
                                                                  void *buf),
                                    void *context);
 int BIO_dgram_sctp_wait_for_dry(BIO *b);
 int BIO_dgram_sctp_msg_waiting(BIO *b);
 # endif
 BIO *BIO_new_fd(int fd, int close_flag);
 BIO *BIO_new_connect(const char *host_port);
 BIO *BIO_new_accept(const char *host_port);
 
 int BIO_new_bio_pair(BIO **bio1, size_t writebuf1,
                      BIO **bio2, size_t writebuf2);
 /*
  * If successful, returns 1 and in *bio1, *bio2 two BIO pair endpoints.
  * Otherwise returns 0 and sets *bio1 and *bio2 to NULL. Size 0 uses default
  * value.
  */
 
 void BIO_copy_next_retry(BIO *b);
 
 /*
  * long BIO_ghbn_ctrl(int cmd,int iarg,char *parg);
  */
 
 # ifdef __GNUC__
 #  define __bio_h__attr__ __attribute__
 # else
 #  define __bio_h__attr__(x)
 # endif
 int BIO_printf(BIO *bio, const char *format, ...)
 __bio_h__attr__((__format__(__printf__, 2, 3)));
 int BIO_vprintf(BIO *bio, const char *format, va_list args)
 __bio_h__attr__((__format__(__printf__, 2, 0)));
 int BIO_snprintf(char *buf, size_t n, const char *format, ...)
 __bio_h__attr__((__format__(__printf__, 3, 4)));
 int BIO_vsnprintf(char *buf, size_t n, const char *format, va_list args)
 __bio_h__attr__((__format__(__printf__, 3, 0)));
 # undef __bio_h__attr__
 
 /* BEGIN ERROR CODES */
 /*
  * The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
  */
 void ERR_load_BIO_strings(void);
 
 /* Error codes for the BIO functions. */
 
 /* Function codes. */
 # define BIO_F_ACPT_STATE                                 100
 # define BIO_F_BIO_ACCEPT                                 101
 # define BIO_F_BIO_BER_GET_HEADER                         102
 # define BIO_F_BIO_CALLBACK_CTRL                          131
 # define BIO_F_BIO_CTRL                                   103
 # define BIO_F_BIO_GETHOSTBYNAME                          120
 # define BIO_F_BIO_GETS                                   104
 # define BIO_F_BIO_GET_ACCEPT_SOCKET                      105
 # define BIO_F_BIO_GET_HOST_IP                            106
 # define BIO_F_BIO_GET_PORT                               107
 # define BIO_F_BIO_MAKE_PAIR                              121
 # define BIO_F_BIO_NEW                                    108
 # define BIO_F_BIO_NEW_FILE                               109
 # define BIO_F_BIO_NEW_MEM_BUF                            126
 # define BIO_F_BIO_NREAD                                  123
 # define BIO_F_BIO_NREAD0                                 124
 # define BIO_F_BIO_NWRITE                                 125
 # define BIO_F_BIO_NWRITE0                                122
 # define BIO_F_BIO_PUTS                                   110
 # define BIO_F_BIO_READ                                   111
 # define BIO_F_BIO_SOCK_INIT                              112
 # define BIO_F_BIO_WRITE                                  113
 # define BIO_F_BUFFER_CTRL                                114
 # define BIO_F_CONN_CTRL                                  127
 # define BIO_F_CONN_STATE                                 115
 # define BIO_F_DGRAM_SCTP_READ                            132
 # define BIO_F_DGRAM_SCTP_WRITE                           133
 # define BIO_F_FILE_CTRL                                  116
 # define BIO_F_FILE_READ                                  130
 # define BIO_F_LINEBUFFER_CTRL                            129
 # define BIO_F_MEM_READ                                   128
 # define BIO_F_MEM_WRITE                                  117
 # define BIO_F_SSL_NEW                                    118
 # define BIO_F_WSASTARTUP                                 119
 
 /* Reason codes. */
 # define BIO_R_ACCEPT_ERROR                               100
 # define BIO_R_BAD_FOPEN_MODE                             101
 # define BIO_R_BAD_HOSTNAME_LOOKUP                        102
 # define BIO_R_BROKEN_PIPE                                124
 # define BIO_R_CONNECT_ERROR                              103
 # define BIO_R_EOF_ON_MEMORY_BIO                          127
 # define BIO_R_ERROR_SETTING_NBIO                         104
 # define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPTED_SOCKET      105
 # define BIO_R_ERROR_SETTING_NBIO_ON_ACCEPT_SOCKET        106
 # define BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET          107
 # define BIO_R_INVALID_ARGUMENT                           125
 # define BIO_R_INVALID_IP_ADDRESS                         108
 # define BIO_R_IN_USE                                     123
 # define BIO_R_KEEPALIVE                                  109
 # define BIO_R_NBIO_CONNECT_ERROR                         110
 # define BIO_R_NO_ACCEPT_PORT_SPECIFIED                   111
 # define BIO_R_NO_HOSTNAME_SPECIFIED                      112
 # define BIO_R_NO_PORT_DEFINED                            113
 # define BIO_R_NO_PORT_SPECIFIED                          114
 # define BIO_R_NO_SUCH_FILE                               128
 # define BIO_R_NULL_PARAMETER                             115
 # define BIO_R_TAG_MISMATCH                               116
 # define BIO_R_UNABLE_TO_BIND_SOCKET                      117
 # define BIO_R_UNABLE_TO_CREATE_SOCKET                    118
 # define BIO_R_UNABLE_TO_LISTEN_SOCKET                    119
 # define BIO_R_UNINITIALIZED                              120
 # define BIO_R_UNSUPPORTED_METHOD                         121
 # define BIO_R_WRITE_TO_READ_ONLY_BIO                     126
 # define BIO_R_WSASTARTUP                                 122
 
 #ifdef  __cplusplus
 }
 #endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/bio/bss_mem.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/bio/bss_mem.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bio/bss_mem.c	(revision 296273)
@@ -1,311 +1,313 @@
 /* crypto/bio/bss_mem.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include 
 #include "cryptlib.h"
 #include 
 
 static int mem_write(BIO *h, const char *buf, int num);
 static int mem_read(BIO *h, char *buf, int size);
 static int mem_puts(BIO *h, const char *str);
 static int mem_gets(BIO *h, char *str, int size);
 static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
 static int mem_new(BIO *h);
 static int mem_free(BIO *data);
 static BIO_METHOD mem_method = {
     BIO_TYPE_MEM,
     "memory buffer",
     mem_write,
     mem_read,
     mem_puts,
     mem_gets,
     mem_ctrl,
     mem_new,
     mem_free,
     NULL,
 };
 
 /*
  * bio->num is used to hold the value to return on 'empty', if it is 0,
  * should_retry is not set
  */
 
 BIO_METHOD *BIO_s_mem(void)
 {
     return (&mem_method);
 }
 
-BIO *BIO_new_mem_buf(void *buf, int len)
+
+BIO *BIO_new_mem_buf(const void *buf, int len)
 {
     BIO *ret;
     BUF_MEM *b;
     size_t sz;
 
     if (!buf) {
         BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
         return NULL;
     }
     sz = (len < 0) ? strlen(buf) : (size_t)len;
     if (!(ret = BIO_new(BIO_s_mem())))
         return NULL;
     b = (BUF_MEM *)ret->ptr;
-    b->data = buf;
+    /* Cast away const and trust in the MEM_RDONLY flag. */
+    b->data = (void *)buf;
     b->length = sz;
     b->max = sz;
     ret->flags |= BIO_FLAGS_MEM_RDONLY;
     /* Since this is static data retrying wont help */
     ret->num = 0;
     return ret;
 }
 
 static int mem_new(BIO *bi)
 {
     BUF_MEM *b;
 
     if ((b = BUF_MEM_new()) == NULL)
         return (0);
     bi->shutdown = 1;
     bi->init = 1;
     bi->num = -1;
     bi->ptr = (char *)b;
     return (1);
 }
 
 static int mem_free(BIO *a)
 {
     if (a == NULL)
         return (0);
     if (a->shutdown) {
         if ((a->init) && (a->ptr != NULL)) {
             BUF_MEM *b;
             b = (BUF_MEM *)a->ptr;
             if (a->flags & BIO_FLAGS_MEM_RDONLY)
                 b->data = NULL;
             BUF_MEM_free(b);
             a->ptr = NULL;
         }
     }
     return (1);
 }
 
 static int mem_read(BIO *b, char *out, int outl)
 {
     int ret = -1;
     BUF_MEM *bm;
 
     bm = (BUF_MEM *)b->ptr;
     BIO_clear_retry_flags(b);
     ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
     if ((out != NULL) && (ret > 0)) {
         memcpy(out, bm->data, ret);
         bm->length -= ret;
         if (b->flags & BIO_FLAGS_MEM_RDONLY)
             bm->data += ret;
         else {
             memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
         }
     } else if (bm->length == 0) {
         ret = b->num;
         if (ret != 0)
             BIO_set_retry_read(b);
     }
     return (ret);
 }
 
 static int mem_write(BIO *b, const char *in, int inl)
 {
     int ret = -1;
     int blen;
     BUF_MEM *bm;
 
     bm = (BUF_MEM *)b->ptr;
     if (in == NULL) {
         BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
         goto end;
     }
 
     if (b->flags & BIO_FLAGS_MEM_RDONLY) {
         BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
         goto end;
     }
 
     BIO_clear_retry_flags(b);
     blen = bm->length;
     if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
         goto end;
     memcpy(&(bm->data[blen]), in, inl);
     ret = inl;
  end:
     return (ret);
 }
 
 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
 {
     long ret = 1;
     char **pptr;
 
     BUF_MEM *bm = (BUF_MEM *)b->ptr;
 
     switch (cmd) {
     case BIO_CTRL_RESET:
         if (bm->data != NULL) {
             /* For read only case reset to the start again */
             if (b->flags & BIO_FLAGS_MEM_RDONLY) {
                 bm->data -= bm->max - bm->length;
                 bm->length = bm->max;
             } else {
                 memset(bm->data, 0, bm->max);
                 bm->length = 0;
             }
         }
         break;
     case BIO_CTRL_EOF:
         ret = (long)(bm->length == 0);
         break;
     case BIO_C_SET_BUF_MEM_EOF_RETURN:
         b->num = (int)num;
         break;
     case BIO_CTRL_INFO:
         ret = (long)bm->length;
         if (ptr != NULL) {
             pptr = (char **)ptr;
             *pptr = (char *)&(bm->data[0]);
         }
         break;
     case BIO_C_SET_BUF_MEM:
         mem_free(b);
         b->shutdown = (int)num;
         b->ptr = ptr;
         break;
     case BIO_C_GET_BUF_MEM_PTR:
         if (ptr != NULL) {
             pptr = (char **)ptr;
             *pptr = (char *)bm;
         }
         break;
     case BIO_CTRL_GET_CLOSE:
         ret = (long)b->shutdown;
         break;
     case BIO_CTRL_SET_CLOSE:
         b->shutdown = (int)num;
         break;
 
     case BIO_CTRL_WPENDING:
         ret = 0L;
         break;
     case BIO_CTRL_PENDING:
         ret = (long)bm->length;
         break;
     case BIO_CTRL_DUP:
     case BIO_CTRL_FLUSH:
         ret = 1;
         break;
     case BIO_CTRL_PUSH:
     case BIO_CTRL_POP:
     default:
         ret = 0;
         break;
     }
     return (ret);
 }
 
 static int mem_gets(BIO *bp, char *buf, int size)
 {
     int i, j;
     int ret = -1;
     char *p;
     BUF_MEM *bm = (BUF_MEM *)bp->ptr;
 
     BIO_clear_retry_flags(bp);
     j = bm->length;
     if ((size - 1) < j)
         j = size - 1;
     if (j <= 0) {
         *buf = '\0';
         return 0;
     }
     p = bm->data;
     for (i = 0; i < j; i++) {
         if (p[i] == '\n') {
             i++;
             break;
         }
     }
 
     /*
      * i is now the max num of bytes to copy, either j or up to
      * and including the first newline
      */
 
     i = mem_read(bp, buf, i);
     if (i > 0)
         buf[i] = '\0';
     ret = i;
     return (ret);
 }
 
 static int mem_puts(BIO *bp, const char *str)
 {
     int n, ret;
 
     n = strlen(str);
     ret = mem_write(bp, str, n);
     /* memory semantics is that it will always work */
     return (ret);
 }
Index: vendor-crypto/openssl/dist/crypto/bn/Makefile
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/Makefile	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/Makefile	(revision 296273)
@@ -1,389 +1,389 @@
 #
 # OpenSSL/crypto/bn/Makefile
 #
 
 DIR=	bn
 TOP=	../..
 CC=	cc
 CPP=    $(CC) -E
 INCLUDES= -I.. -I$(TOP) -I../../include
 CFLAG=-g
 MAKEFILE=	Makefile
 AR=		ar r
 
 BN_ASM=		bn_asm.o
 
 CFLAGS= $(INCLUDES) $(CFLAG)
 ASFLAGS= $(INCLUDES) $(ASFLAG)
 AFLAGS= $(ASFLAGS)
 
 GENERAL=Makefile
 TEST=bntest.c exptest.c
 APPS=
 
 LIB=$(TOP)/libcrypto.a
 LIBSRC=	bn_add.c bn_div.c bn_exp.c bn_lib.c bn_ctx.c bn_mul.c bn_mod.c \
 	bn_print.c bn_rand.c bn_shift.c bn_word.c bn_blind.c \
 	bn_kron.c bn_sqrt.c bn_gcd.c bn_prime.c bn_err.c bn_sqr.c bn_asm.c \
 	bn_recp.c bn_mont.c bn_mpi.c bn_exp2.c bn_gf2m.c bn_nist.c \
 	bn_depr.c bn_const.c bn_x931p.c
 
 LIBOBJ=	bn_add.o bn_div.o bn_exp.o bn_lib.o bn_ctx.o bn_mul.o bn_mod.o \
 	bn_print.o bn_rand.o bn_shift.o bn_word.o bn_blind.o \
 	bn_kron.o bn_sqrt.o bn_gcd.o bn_prime.o bn_err.o bn_sqr.o $(BN_ASM) \
 	bn_recp.o bn_mont.o bn_mpi.o bn_exp2.o bn_gf2m.o bn_nist.o \
 	bn_depr.o bn_const.o bn_x931p.o
 
 SRC= $(LIBSRC)
 
 EXHEADER= bn.h
 HEADER=	bn_lcl.h bn_prime.h $(EXHEADER)
 
 ALL=    $(GENERAL) $(SRC) $(HEADER)
 
 top:
 	(cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
 
 all:	lib
 
 bn_prime.h: bn_prime.pl
 	$(PERL) bn_prime.pl >bn_prime.h
 
 divtest: divtest.c ../../libcrypto.a
 	cc -I../../include divtest.c -o divtest ../../libcrypto.a
 
 bnbug: bnbug.c ../../libcrypto.a top
 	cc -g -I../../include bnbug.c -o bnbug ../../libcrypto.a
 
 lib:	$(LIBOBJ)
 	$(AR) $(LIB) $(LIBOBJ)
 	$(RANLIB) $(LIB) || echo Never mind.
 	@touch lib
 
 bn-586.s:	asm/bn-586.pl ../perlasm/x86asm.pl
 	$(PERL) asm/bn-586.pl $(PERLASM_SCHEME) $(CFLAGS) $(PROCESSOR) > $@
 co-586.s:	asm/co-586.pl ../perlasm/x86asm.pl
 	$(PERL) asm/co-586.pl $(PERLASM_SCHEME) $(CFLAGS) $(PROCESSOR) > $@
 x86-mont.s:	asm/x86-mont.pl ../perlasm/x86asm.pl
 	$(PERL) asm/x86-mont.pl $(PERLASM_SCHEME) $(CFLAGS) $(PROCESSOR) > $@
 x86-gf2m.s:	asm/x86-gf2m.pl ../perlasm/x86asm.pl
 	$(PERL) asm/x86-gf2m.pl $(PERLASM_SCHEME) $(CFLAGS) $(PROCESSOR) > $@
 
 sparcv8.o:	asm/sparcv8.S
 	$(CC) $(CFLAGS) -c asm/sparcv8.S
 bn-sparcv9.o:	asm/sparcv8plus.S
 	$(CC) $(CFLAGS) -c -o $@ asm/sparcv8plus.S
 sparcv9a-mont.s:	asm/sparcv9a-mont.pl
 	$(PERL) asm/sparcv9a-mont.pl $(CFLAGS) > $@
 sparcv9-mont.s:		asm/sparcv9-mont.pl
 	$(PERL) asm/sparcv9-mont.pl $(CFLAGS) > $@
 vis3-mont.s:		asm/vis3-mont.pl
 	$(PERL) asm/vis3-mont.pl $(CFLAGS) > $@
 sparct4-mont.S:	asm/sparct4-mont.pl
 	$(PERL) asm/sparct4-mont.pl $(CFLAGS) > $@
 sparcv9-gf2m.S:	asm/sparcv9-gf2m.pl
 	$(PERL) asm/sparcv9-gf2m.pl $(CFLAGS) > $@
 
 bn-mips3.o:	asm/mips3.s
 	@if [ "$(CC)" = "gcc" ]; then \
 		ABI=`expr "$(CFLAGS)" : ".*-mabi=\([n3264]*\)"` && \
 		as -$$ABI -O -o $@ asm/mips3.s; \
 	else	$(CC) -c $(CFLAGS) -o $@ asm/mips3.s; fi
 
 bn-mips.s:	asm/mips.pl
 	$(PERL) asm/mips.pl $(PERLASM_SCHEME) $@
 mips-mont.s:	asm/mips-mont.pl
 	$(PERL)	asm/mips-mont.pl $(PERLASM_SCHEME) $@
 
 bn-s390x.o:	asm/s390x.S
 	$(CC) $(CFLAGS) -c -o $@ asm/s390x.S
 s390x-gf2m.s:	asm/s390x-gf2m.pl
 	$(PERL) asm/s390x-gf2m.pl $(PERLASM_SCHEME) $@
 
 x86_64-gcc.o:	asm/x86_64-gcc.c
 	$(CC) $(CFLAGS) -c -o $@ asm/x86_64-gcc.c
 x86_64-mont.s:	asm/x86_64-mont.pl
 	$(PERL) asm/x86_64-mont.pl $(PERLASM_SCHEME) > $@
 x86_64-mont5.s:	asm/x86_64-mont5.pl
 	$(PERL) asm/x86_64-mont5.pl $(PERLASM_SCHEME) > $@
 x86_64-gf2m.s:	asm/x86_64-gf2m.pl
 	$(PERL) asm/x86_64-gf2m.pl $(PERLASM_SCHEME) > $@
 rsaz-x86_64.s:	asm/rsaz-x86_64.pl
 	$(PERL) asm/rsaz-x86_64.pl $(PERLASM_SCHEME) > $@
 rsaz-avx2.s:	asm/rsaz-avx2.pl 
 	$(PERL) asm/rsaz-avx2.pl $(PERLASM_SCHEME) > $@
 
 bn-ia64.s:	asm/ia64.S
 	$(CC) $(CFLAGS) -E asm/ia64.S > $@
 ia64-mont.s:	asm/ia64-mont.pl
 	$(PERL) asm/ia64-mont.pl $@ $(CFLAGS)
 
 # GNU assembler fails to compile PA-RISC2 modules, insist on calling
 # vendor assembler...
 pa-risc2W.o: asm/pa-risc2W.s
 	/usr/ccs/bin/as -o pa-risc2W.o asm/pa-risc2W.s
 pa-risc2.o: asm/pa-risc2.s
 	/usr/ccs/bin/as -o pa-risc2.o asm/pa-risc2.s
 parisc-mont.s:	asm/parisc-mont.pl
 	$(PERL) asm/parisc-mont.pl $(PERLASM_SCHEME) $@
 
 # ppc - AIX, Linux, MacOS X...
 bn-ppc.s:	asm/ppc.pl;	$(PERL) asm/ppc.pl $(PERLASM_SCHEME) $@
 ppc-mont.s:	asm/ppc-mont.pl;$(PERL) asm/ppc-mont.pl $(PERLASM_SCHEME) $@
 ppc64-mont.s:	asm/ppc64-mont.pl;$(PERL) asm/ppc64-mont.pl $(PERLASM_SCHEME) $@
 
 alpha-mont.s:	asm/alpha-mont.pl
 	(preproc=$$$$.$@.S; trap "rm $$preproc" INT; \
 	$(PERL) asm/alpha-mont.pl > $$preproc && \
 	$(CC) -E -P $$preproc > $@ && rm $$preproc)
 
 # GNU make "catch all"
 %-mont.S:	asm/%-mont.pl;	$(PERL) $< $(PERLASM_SCHEME) $@
 %-gf2m.S:	asm/%-gf2m.pl;	$(PERL) $< $(PERLASM_SCHEME) $@
 
 armv4-mont.o:	armv4-mont.S
 armv4-gf2m.o:	armv4-gf2m.S
 
 files:
 	$(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
 
 links:
 	@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
 	@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
 	@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
 
 install:
 	@[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
 	@headerlist="$(EXHEADER)"; for i in $$headerlist ; \
 	do  \
 	(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
 	chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
 	done;
 
 exptest:
 	rm -f exptest
 	gcc -I../../include -g2 -ggdb -o exptest exptest.c ../../libcrypto.a
 
 div:
 	rm -f a.out
 	gcc -I.. -g div.c ../../libcrypto.a
 
 tags:
 	ctags $(SRC)
 
 tests:
 
 lint:
 	lint -DLINT $(INCLUDES) $(SRC)>fluff
 
 update: bn_prime.h depend
 
 depend:
 	@[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
 	$(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
 
 dclean:
 	$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
 	mv -f Makefile.new $(MAKEFILE)
 
 clean:
 	rm -f *.s *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
 
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
 bn_add.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_add.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_add.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_add.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_add.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_add.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_add.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_add.c bn_lcl.h
 bn_asm.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_asm.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_asm.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_asm.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_asm.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_asm.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_asm.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_asm.c bn_lcl.h
 bn_blind.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_blind.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_blind.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_blind.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_blind.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_blind.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_blind.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_blind.c bn_lcl.h
 bn_const.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
 bn_const.o: ../../include/openssl/opensslconf.h
 bn_const.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_const.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_const.o: ../../include/openssl/symhacks.h bn.h bn_const.c
 bn_ctx.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_ctx.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_ctx.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_ctx.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_ctx.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_ctx.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_ctx.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_ctx.c bn_lcl.h
 bn_depr.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_depr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_depr.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_depr.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_depr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_depr.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
 bn_depr.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
 bn_depr.o: ../cryptlib.h bn_depr.c bn_lcl.h
 bn_div.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_div.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_div.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_div.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_div.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_div.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_div.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_div.c bn_lcl.h
 bn_err.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
 bn_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
 bn_err.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h
 bn_err.o: ../../include/openssl/ossl_typ.h ../../include/openssl/safestack.h
 bn_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
 bn_err.o: bn_err.c
 bn_exp.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_exp.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_exp.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_exp.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_exp.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_exp.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
-bn_exp.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_exp.c bn_lcl.h
-bn_exp.o: rsaz_exp.h
+bn_exp.o: ../../include/openssl/symhacks.h ../constant_time_locl.h
+bn_exp.o: ../cryptlib.h bn_exp.c bn_lcl.h rsaz_exp.h
 bn_exp2.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_exp2.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_exp2.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_exp2.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_exp2.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_exp2.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_exp2.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_exp2.c bn_lcl.h
 bn_gcd.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_gcd.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_gcd.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_gcd.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_gcd.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_gcd.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_gcd.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_gcd.c bn_lcl.h
 bn_gf2m.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_gf2m.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_gf2m.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_gf2m.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_gf2m.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_gf2m.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_gf2m.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_gf2m.c bn_lcl.h
 bn_kron.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_kron.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_kron.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_kron.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_kron.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_kron.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_kron.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_kron.c bn_lcl.h
 bn_lib.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_lib.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_lib.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_lib.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_lib.c
 bn_mod.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_mod.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_mod.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_mod.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_mod.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_mod.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_mod.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_mod.c
 bn_mont.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_mont.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_mont.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_mont.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_mont.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_mont.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_mont.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_mont.c
 bn_mpi.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_mpi.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_mpi.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_mpi.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_mpi.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_mpi.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_mpi.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_mpi.c
 bn_mul.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_mul.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_mul.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_mul.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_mul.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_mul.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_mul.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_mul.c
 bn_nist.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_nist.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_nist.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_nist.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_nist.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_nist.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_nist.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_nist.c
 bn_prime.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_prime.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_prime.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_prime.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_prime.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_prime.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
 bn_prime.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
 bn_prime.o: ../cryptlib.h bn_lcl.h bn_prime.c bn_prime.h
 bn_print.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_print.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_print.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_print.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_print.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_print.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_print.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_print.c
 bn_rand.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_rand.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_rand.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_rand.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_rand.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_rand.o: ../../include/openssl/rand.h ../../include/openssl/safestack.h
 bn_rand.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
 bn_rand.o: ../cryptlib.h bn_lcl.h bn_rand.c
 bn_recp.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_recp.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_recp.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_recp.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_recp.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_recp.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_recp.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_recp.c
 bn_shift.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_shift.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_shift.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_shift.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_shift.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_shift.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_shift.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_shift.c
 bn_sqr.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_sqr.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_sqr.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_sqr.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_sqr.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_sqr.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_sqr.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_sqr.c
 bn_sqrt.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_sqrt.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_sqrt.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_sqrt.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_sqrt.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_sqrt.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_sqrt.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_sqrt.c
 bn_word.o: ../../e_os.h ../../include/openssl/bio.h ../../include/openssl/bn.h
 bn_word.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
 bn_word.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
 bn_word.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
 bn_word.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_word.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_word.o: ../../include/openssl/symhacks.h ../cryptlib.h bn_lcl.h bn_word.c
 bn_x931p.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
 bn_x931p.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
 bn_x931p.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
 bn_x931p.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
 bn_x931p.o: ../../include/openssl/symhacks.h bn_x931p.c
Index: vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-avx2.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-avx2.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-avx2.pl	(revision 296273)
@@ -1,1898 +1,1961 @@
 #!/usr/bin/env perl
 
 ##############################################################################
 #                                                                            #
 #  Copyright (c) 2012, Intel Corporation                                     #
 #                                                                            #
 #  All rights reserved.                                                      #
 #                                                                            #
 #  Redistribution and use in source and binary forms, with or without        #
 #  modification, are permitted provided that the following conditions are    #
 #  met:                                                                      #
 #                                                                            #
 #  *  Redistributions of source code must retain the above copyright         #
 #     notice, this list of conditions and the following disclaimer.          #
 #                                                                            #
 #  *  Redistributions in binary form must reproduce the above copyright      #
 #     notice, this list of conditions and the following disclaimer in the    #
 #     documentation and/or other materials provided with the                 #
 #     distribution.                                                          #
 #                                                                            #
 #  *  Neither the name of the Intel Corporation nor the names of its         #
 #     contributors may be used to endorse or promote products derived from   #
 #     this software without specific prior written permission.               #
 #                                                                            #
 #                                                                            #
 #  THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION ""AS IS"" AND ANY          #
 #  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE         #
 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        #
 #  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR            #
 #  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
 #  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       #
 #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        #
 #  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    #
 #  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      #
 #  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        #
 #  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              #
 #                                                                            #
 ##############################################################################
 # Developers and authors:                                                    #
 # Shay Gueron (1, 2), and Vlad Krasnov (1)                                   #
 # (1) Intel Corporation, Israel Development Center, Haifa, Israel            #
 # (2) University of Haifa, Israel                                            #
 ##############################################################################
 # Reference:                                                                 #
 # [1] S. Gueron, V. Krasnov: "Software Implementation of Modular             #
 #     Exponentiation,  Using Advanced Vector Instructions Architectures",    #
 #     F. Ozbudak and F. Rodriguez-Henriquez (Eds.): WAIFI 2012, LNCS 7369,   #
 #     pp. 119?135, 2012. Springer-Verlag Berlin Heidelberg 2012              #
 # [2] S. Gueron: "Efficient Software Implementations of Modular              #
 #     Exponentiation", Journal of Cryptographic Engineering 2:31-43 (2012).  #
 # [3] S. Gueron, V. Krasnov: "Speeding up Big-numbers Squaring",IEEE         #
 #     Proceedings of 9th International Conference on Information Technology: #
 #     New Generations (ITNG 2012), pp.821-823 (2012)                         #
 # [4] S. Gueron, V. Krasnov: "[PATCH] Efficient and side channel analysis    #
 #     resistant 1024-bit modular exponentiation, for optimizing RSA2048      #
 #     on AVX2 capable x86_64 platforms",                                     #
 #     http://rt.openssl.org/Ticket/Display.html?id=2850&user=guest&pass=guest#
 ##############################################################################
 #
 # +13% improvement over original submission by 
 #
 # rsa2048 sign/sec	OpenSSL 1.0.1	scalar(*)	this
 # 2.3GHz Haswell	621		765/+23%	1113/+79%
 # 2.3GHz Broadwell(**)	688		1200(***)/+74%	1120/+63%
 #
 # (*)	if system doesn't support AVX2, for reference purposes;
 # (**)	scaled to 2.3GHz to simplify comparison;
 # (***)	scalar AD*X code is faster than AVX2 and is preferred code
 #	path for Broadwell;
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.19) + ($1>=2.22);
 	$addx = ($1>=2.23);
 }
 
 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.09) + ($1>=2.10);
 	$addx = ($1>=2.10);
 }
 
 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$avx = ($1>=10) + ($1>=11);
 	$addx = ($1>=11);
 }
 
 if (!$avx && `$ENV{CC} -v 2>&1` =~ /(^clang version|based on LLVM) ([3-9])\.([0-9]+)/) {
 	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
 	$avx = ($ver>=3.0) + ($ver>=3.01);
 	$addx = ($ver>=3.03);
 }
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT = *OUT;
 
 if ($avx>1) {{{
 { # void AMS_WW(
 my $rp="%rdi";	# BN_ULONG *rp,
 my $ap="%rsi";	# const BN_ULONG *ap,
 my $np="%rdx";	# const BN_ULONG *np,
 my $n0="%ecx";	# const BN_ULONG n0,
 my $rep="%r8d";	# int repeat);
 
 # The registers that hold the accumulated redundant result
 # The AMM works on 1024 bit operands, and redundant word size is 29
 # Therefore: ceil(1024/29)/4 = 9
 my $ACC0="%ymm0";
 my $ACC1="%ymm1";
 my $ACC2="%ymm2";
 my $ACC3="%ymm3";
 my $ACC4="%ymm4";
 my $ACC5="%ymm5";
 my $ACC6="%ymm6";
 my $ACC7="%ymm7";
 my $ACC8="%ymm8";
 my $ACC9="%ymm9";
 # Registers that hold the broadcasted words of bp, currently used
 my $B1="%ymm10";
 my $B2="%ymm11";
 # Registers that hold the broadcasted words of Y, currently used
 my $Y1="%ymm12";
 my $Y2="%ymm13";
 # Helper registers
 my $TEMP1="%ymm14";
 my $AND_MASK="%ymm15";
 # alu registers that hold the first words of the ACC
 my $r0="%r9";
 my $r1="%r10";
 my $r2="%r11";
 my $r3="%r12";
 
 my $i="%r14d";			# loop counter
 my $tmp = "%r15";
 
 my $FrameSize=32*18+32*8;	# place for A^2 and 2*A
 
 my $aap=$r0;
 my $tp0="%rbx";
 my $tp1=$r3;
 my $tpa=$tmp;
 
 $np="%r13";			# reassigned argument
 
 $code.=<<___;
 .text
 
 .globl	rsaz_1024_sqr_avx2
 .type	rsaz_1024_sqr_avx2,\@function,5
 .align	64
 rsaz_1024_sqr_avx2:		# 702 cycles, 14% faster than rsaz_1024_mul_avx2
 	lea	(%rsp), %rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
 	lea	-0xa8(%rsp),%rsp
 	vmovaps	%xmm6,-0xd8(%rax)
 	vmovaps	%xmm7,-0xc8(%rax)
 	vmovaps	%xmm8,-0xb8(%rax)
 	vmovaps	%xmm9,-0xa8(%rax)
 	vmovaps	%xmm10,-0x98(%rax)
 	vmovaps	%xmm11,-0x88(%rax)
 	vmovaps	%xmm12,-0x78(%rax)
 	vmovaps	%xmm13,-0x68(%rax)
 	vmovaps	%xmm14,-0x58(%rax)
 	vmovaps	%xmm15,-0x48(%rax)
 .Lsqr_1024_body:
 ___
 $code.=<<___;
 	mov	%rax,%rbp
 	mov	%rdx, $np			# reassigned argument
 	sub	\$$FrameSize, %rsp
 	mov	$np, $tmp
 	sub	\$-128, $rp			# size optimization
 	sub	\$-128, $ap
 	sub	\$-128, $np
 
 	and	\$4095, $tmp			# see if $np crosses page
 	add	\$32*10, $tmp
 	shr	\$12, $tmp
 	vpxor	$ACC9,$ACC9,$ACC9
 	jz	.Lsqr_1024_no_n_copy
 
 	# unaligned 256-bit load that crosses page boundary can
 	# cause >2x performance degradation here, so if $np does
 	# cross page boundary, copy it to stack and make sure stack
 	# frame doesn't...
 	sub		\$32*10,%rsp
 	vmovdqu		32*0-128($np), $ACC0
 	and		\$-2048, %rsp
 	vmovdqu		32*1-128($np), $ACC1
 	vmovdqu		32*2-128($np), $ACC2
 	vmovdqu		32*3-128($np), $ACC3
 	vmovdqu		32*4-128($np), $ACC4
 	vmovdqu		32*5-128($np), $ACC5
 	vmovdqu		32*6-128($np), $ACC6
 	vmovdqu		32*7-128($np), $ACC7
 	vmovdqu		32*8-128($np), $ACC8
 	lea		$FrameSize+128(%rsp),$np
 	vmovdqu		$ACC0, 32*0-128($np)
 	vmovdqu		$ACC1, 32*1-128($np)
 	vmovdqu		$ACC2, 32*2-128($np)
 	vmovdqu		$ACC3, 32*3-128($np)
 	vmovdqu		$ACC4, 32*4-128($np)
 	vmovdqu		$ACC5, 32*5-128($np)
 	vmovdqu		$ACC6, 32*6-128($np)
 	vmovdqu		$ACC7, 32*7-128($np)
 	vmovdqu		$ACC8, 32*8-128($np)
 	vmovdqu		$ACC9, 32*9-128($np)	# $ACC9 is zero
 
 .Lsqr_1024_no_n_copy:
 	and		\$-1024, %rsp
 
 	vmovdqu		32*1-128($ap), $ACC1
 	vmovdqu		32*2-128($ap), $ACC2
 	vmovdqu		32*3-128($ap), $ACC3
 	vmovdqu		32*4-128($ap), $ACC4
 	vmovdqu		32*5-128($ap), $ACC5
 	vmovdqu		32*6-128($ap), $ACC6
 	vmovdqu		32*7-128($ap), $ACC7
 	vmovdqu		32*8-128($ap), $ACC8
 
 	lea	192(%rsp), $tp0			# 64+128=192
 	vpbroadcastq	.Land_mask(%rip), $AND_MASK
 	jmp	.LOOP_GRANDE_SQR_1024
 
 .align	32
 .LOOP_GRANDE_SQR_1024:
 	lea	32*18+128(%rsp), $aap		# size optimization
 	lea	448(%rsp), $tp1			# 64+128+256=448
 
 	# the squaring is performed as described in Variant B of
 	# "Speeding up Big-Number Squaring", so start by calculating
 	# the A*2=A+A vector
 	vpaddq		$ACC1, $ACC1, $ACC1
 	 vpbroadcastq	32*0-128($ap), $B1
 	vpaddq		$ACC2, $ACC2, $ACC2
 	vmovdqa		$ACC1, 32*0-128($aap)
 	vpaddq		$ACC3, $ACC3, $ACC3
 	vmovdqa		$ACC2, 32*1-128($aap)
 	vpaddq		$ACC4, $ACC4, $ACC4
 	vmovdqa		$ACC3, 32*2-128($aap)
 	vpaddq		$ACC5, $ACC5, $ACC5
 	vmovdqa		$ACC4, 32*3-128($aap)
 	vpaddq		$ACC6, $ACC6, $ACC6
 	vmovdqa		$ACC5, 32*4-128($aap)
 	vpaddq		$ACC7, $ACC7, $ACC7
 	vmovdqa		$ACC6, 32*5-128($aap)
 	vpaddq		$ACC8, $ACC8, $ACC8
 	vmovdqa		$ACC7, 32*6-128($aap)
 	vpxor		$ACC9, $ACC9, $ACC9
 	vmovdqa		$ACC8, 32*7-128($aap)
 
 	vpmuludq	32*0-128($ap), $B1, $ACC0
 	 vpbroadcastq	32*1-128($ap), $B2
 	 vmovdqu	$ACC9, 32*9-192($tp0)	# zero upper half
 	vpmuludq	$B1, $ACC1, $ACC1
 	 vmovdqu	$ACC9, 32*10-448($tp1)
 	vpmuludq	$B1, $ACC2, $ACC2
 	 vmovdqu	$ACC9, 32*11-448($tp1)
 	vpmuludq	$B1, $ACC3, $ACC3
 	 vmovdqu	$ACC9, 32*12-448($tp1)
 	vpmuludq	$B1, $ACC4, $ACC4
 	 vmovdqu	$ACC9, 32*13-448($tp1)
 	vpmuludq	$B1, $ACC5, $ACC5
 	 vmovdqu	$ACC9, 32*14-448($tp1)
 	vpmuludq	$B1, $ACC6, $ACC6
 	 vmovdqu	$ACC9, 32*15-448($tp1)
 	vpmuludq	$B1, $ACC7, $ACC7
 	 vmovdqu	$ACC9, 32*16-448($tp1)
 	vpmuludq	$B1, $ACC8, $ACC8
 	 vpbroadcastq	32*2-128($ap), $B1
 	 vmovdqu	$ACC9, 32*17-448($tp1)
 
 	mov	$ap, $tpa
 	mov 	\$4, $i
 	jmp	.Lsqr_entry_1024
 ___
 $TEMP0=$Y1;
 $TEMP2=$Y2;
 $code.=<<___;
 .align	32
 .LOOP_SQR_1024:
 	 vpbroadcastq	32*1-128($tpa), $B2
 	vpmuludq	32*0-128($ap), $B1, $ACC0
 	vpaddq		32*0-192($tp0), $ACC0, $ACC0
 	vpmuludq	32*0-128($aap), $B1, $ACC1
 	vpaddq		32*1-192($tp0), $ACC1, $ACC1
 	vpmuludq	32*1-128($aap), $B1, $ACC2
 	vpaddq		32*2-192($tp0), $ACC2, $ACC2
 	vpmuludq	32*2-128($aap), $B1, $ACC3
 	vpaddq		32*3-192($tp0), $ACC3, $ACC3
 	vpmuludq	32*3-128($aap), $B1, $ACC4
 	vpaddq		32*4-192($tp0), $ACC4, $ACC4
 	vpmuludq	32*4-128($aap), $B1, $ACC5
 	vpaddq		32*5-192($tp0), $ACC5, $ACC5
 	vpmuludq	32*5-128($aap), $B1, $ACC6
 	vpaddq		32*6-192($tp0), $ACC6, $ACC6
 	vpmuludq	32*6-128($aap), $B1, $ACC7
 	vpaddq		32*7-192($tp0), $ACC7, $ACC7
 	vpmuludq	32*7-128($aap), $B1, $ACC8
 	 vpbroadcastq	32*2-128($tpa), $B1
 	vpaddq		32*8-192($tp0), $ACC8, $ACC8
 .Lsqr_entry_1024:
 	vmovdqu		$ACC0, 32*0-192($tp0)
 	vmovdqu		$ACC1, 32*1-192($tp0)
 
 	vpmuludq	32*1-128($ap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC2, $ACC2
 	vpmuludq	32*1-128($aap), $B2, $TEMP1
 	vpaddq		$TEMP1, $ACC3, $ACC3
 	vpmuludq	32*2-128($aap), $B2, $TEMP2
 	vpaddq		$TEMP2, $ACC4, $ACC4
 	vpmuludq	32*3-128($aap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC5, $ACC5
 	vpmuludq	32*4-128($aap), $B2, $TEMP1
 	vpaddq		$TEMP1, $ACC6, $ACC6
 	vpmuludq	32*5-128($aap), $B2, $TEMP2
 	vpaddq		$TEMP2, $ACC7, $ACC7
 	vpmuludq	32*6-128($aap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC8, $ACC8
 	vpmuludq	32*7-128($aap), $B2, $ACC0
 	 vpbroadcastq	32*3-128($tpa), $B2
 	vpaddq		32*9-192($tp0), $ACC0, $ACC0
 
 	vmovdqu		$ACC2, 32*2-192($tp0)
 	vmovdqu		$ACC3, 32*3-192($tp0)
 
 	vpmuludq	32*2-128($ap), $B1, $TEMP2
 	vpaddq		$TEMP2, $ACC4, $ACC4
 	vpmuludq	32*2-128($aap), $B1, $TEMP0
 	vpaddq		$TEMP0, $ACC5, $ACC5
 	vpmuludq	32*3-128($aap), $B1, $TEMP1
 	vpaddq		$TEMP1, $ACC6, $ACC6
 	vpmuludq	32*4-128($aap), $B1, $TEMP2
 	vpaddq		$TEMP2, $ACC7, $ACC7
 	vpmuludq	32*5-128($aap), $B1, $TEMP0
 	vpaddq		$TEMP0, $ACC8, $ACC8
 	vpmuludq	32*6-128($aap), $B1, $TEMP1
 	vpaddq		$TEMP1, $ACC0, $ACC0
 	vpmuludq	32*7-128($aap), $B1, $ACC1
 	 vpbroadcastq	32*4-128($tpa), $B1
 	vpaddq		32*10-448($tp1), $ACC1, $ACC1
 
 	vmovdqu		$ACC4, 32*4-192($tp0)
 	vmovdqu		$ACC5, 32*5-192($tp0)
 
 	vpmuludq	32*3-128($ap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC6, $ACC6
 	vpmuludq	32*3-128($aap), $B2, $TEMP1
 	vpaddq		$TEMP1, $ACC7, $ACC7
 	vpmuludq	32*4-128($aap), $B2, $TEMP2
 	vpaddq		$TEMP2, $ACC8, $ACC8
 	vpmuludq	32*5-128($aap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC0, $ACC0
 	vpmuludq	32*6-128($aap), $B2, $TEMP1
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vpmuludq	32*7-128($aap), $B2, $ACC2
 	 vpbroadcastq	32*5-128($tpa), $B2
 	vpaddq		32*11-448($tp1), $ACC2, $ACC2	
 
 	vmovdqu		$ACC6, 32*6-192($tp0)
 	vmovdqu		$ACC7, 32*7-192($tp0)
 
 	vpmuludq	32*4-128($ap), $B1, $TEMP0
 	vpaddq		$TEMP0, $ACC8, $ACC8
 	vpmuludq	32*4-128($aap), $B1, $TEMP1
 	vpaddq		$TEMP1, $ACC0, $ACC0
 	vpmuludq	32*5-128($aap), $B1, $TEMP2
 	vpaddq		$TEMP2, $ACC1, $ACC1
 	vpmuludq	32*6-128($aap), $B1, $TEMP0
 	vpaddq		$TEMP0, $ACC2, $ACC2
 	vpmuludq	32*7-128($aap), $B1, $ACC3
 	 vpbroadcastq	32*6-128($tpa), $B1
 	vpaddq		32*12-448($tp1), $ACC3, $ACC3
 
 	vmovdqu		$ACC8, 32*8-192($tp0)
 	vmovdqu		$ACC0, 32*9-192($tp0)
 	lea		8($tp0), $tp0
 
 	vpmuludq	32*5-128($ap), $B2, $TEMP2
 	vpaddq		$TEMP2, $ACC1, $ACC1
 	vpmuludq	32*5-128($aap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC2, $ACC2
 	vpmuludq	32*6-128($aap), $B2, $TEMP1
 	vpaddq		$TEMP1, $ACC3, $ACC3
 	vpmuludq	32*7-128($aap), $B2, $ACC4
 	 vpbroadcastq	32*7-128($tpa), $B2
 	vpaddq		32*13-448($tp1), $ACC4, $ACC4
 
 	vmovdqu		$ACC1, 32*10-448($tp1)
 	vmovdqu		$ACC2, 32*11-448($tp1)
 
 	vpmuludq	32*6-128($ap), $B1, $TEMP0
 	vpaddq		$TEMP0, $ACC3, $ACC3
 	vpmuludq	32*6-128($aap), $B1, $TEMP1
 	 vpbroadcastq	32*8-128($tpa), $ACC0		# borrow $ACC0 for $B1
 	vpaddq		$TEMP1, $ACC4, $ACC4
 	vpmuludq	32*7-128($aap), $B1, $ACC5
 	 vpbroadcastq	32*0+8-128($tpa), $B1		# for next iteration
 	vpaddq		32*14-448($tp1), $ACC5, $ACC5
 
 	vmovdqu		$ACC3, 32*12-448($tp1)
 	vmovdqu		$ACC4, 32*13-448($tp1)
 	lea		8($tpa), $tpa
 
 	vpmuludq	32*7-128($ap), $B2, $TEMP0
 	vpaddq		$TEMP0, $ACC5, $ACC5
 	vpmuludq	32*7-128($aap), $B2, $ACC6
 	vpaddq		32*15-448($tp1), $ACC6, $ACC6
 
 	vpmuludq	32*8-128($ap), $ACC0, $ACC7
 	vmovdqu		$ACC5, 32*14-448($tp1)
 	vpaddq		32*16-448($tp1), $ACC7, $ACC7
 	vmovdqu		$ACC6, 32*15-448($tp1)
 	vmovdqu		$ACC7, 32*16-448($tp1)
 	lea		8($tp1), $tp1
 
 	dec	$i        
 	jnz	.LOOP_SQR_1024
 ___
 $ZERO = $ACC9;
 $TEMP0 = $B1;
 $TEMP2 = $B2;
 $TEMP3 = $Y1;
 $TEMP4 = $Y2;
 $code.=<<___;
-	#we need to fix indexes 32-39 to avoid overflow
+	# we need to fix indices 32-39 to avoid overflow
 	vmovdqu		32*8(%rsp), $ACC8		# 32*8-192($tp0),
 	vmovdqu		32*9(%rsp), $ACC1		# 32*9-192($tp0)
 	vmovdqu		32*10(%rsp), $ACC2		# 32*10-192($tp0)
 	lea		192(%rsp), $tp0			# 64+128=192
 
 	vpsrlq		\$29, $ACC8, $TEMP1
 	vpand		$AND_MASK, $ACC8, $ACC8
 	vpsrlq		\$29, $ACC1, $TEMP2
 	vpand		$AND_MASK, $ACC1, $ACC1
 
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpxor		$ZERO, $ZERO, $ZERO
 	vpermq		\$0x93, $TEMP2, $TEMP2
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC8, $ACC8
 	vpblendd	\$3, $TEMP2, $ZERO, $TEMP2
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vmovdqu		$ACC1, 32*9-192($tp0)
 	vmovdqu		$ACC2, 32*10-192($tp0)
 
 	mov	(%rsp), %rax
 	mov	8(%rsp), $r1
 	mov	16(%rsp), $r2
 	mov	24(%rsp), $r3
 	vmovdqu	32*1(%rsp), $ACC1
 	vmovdqu	32*2-192($tp0), $ACC2
 	vmovdqu	32*3-192($tp0), $ACC3
 	vmovdqu	32*4-192($tp0), $ACC4
 	vmovdqu	32*5-192($tp0), $ACC5
 	vmovdqu	32*6-192($tp0), $ACC6
 	vmovdqu	32*7-192($tp0), $ACC7
 
 	mov	%rax, $r0
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 	vmovd	%eax, $Y1
 
 	mov	%rax, %rdx
 	imulq	-128($np), %rax
 	 vpbroadcastq	$Y1, $Y1
 	add	%rax, $r0
 	mov	%rdx, %rax
 	imulq	8-128($np), %rax
 	shr	\$29, $r0
 	add	%rax, $r1
 	mov	%rdx, %rax
 	imulq	16-128($np), %rax
 	add	$r0, $r1
 	add	%rax, $r2
 	imulq	24-128($np), %rdx
 	add	%rdx, $r3
 
 	mov	$r1, %rax
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 
 	mov \$9, $i
 	jmp .LOOP_REDUCE_1024
 
 .align	32
 .LOOP_REDUCE_1024:
 	vmovd	%eax, $Y2
 	vpbroadcastq	$Y2, $Y2
 
 	vpmuludq	32*1-128($np), $Y1, $TEMP0
 	 mov	%rax, %rdx
 	 imulq	-128($np), %rax
 	vpaddq		$TEMP0, $ACC1, $ACC1
 	 add	%rax, $r1
 	vpmuludq	32*2-128($np), $Y1, $TEMP1
 	 mov	%rdx, %rax
 	 imulq	8-128($np), %rax
 	vpaddq		$TEMP1, $ACC2, $ACC2
 	vpmuludq	32*3-128($np), $Y1, $TEMP2
 	 .byte	0x67
 	 add	%rax, $r2
 	 .byte	0x67
 	 mov	%rdx, %rax
 	 imulq	16-128($np), %rax
 	 shr	\$29, $r1
 	vpaddq		$TEMP2, $ACC3, $ACC3
 	vpmuludq	32*4-128($np), $Y1, $TEMP0
 	 add	%rax, $r3
 	 add	$r1, $r2
 	vpaddq		$TEMP0, $ACC4, $ACC4
 	vpmuludq	32*5-128($np), $Y1, $TEMP1
 	 mov	$r2, %rax
 	 imull	$n0, %eax
 	vpaddq		$TEMP1, $ACC5, $ACC5
 	vpmuludq	32*6-128($np), $Y1, $TEMP2
 	 and	\$0x1fffffff, %eax
 	vpaddq		$TEMP2, $ACC6, $ACC6
 	vpmuludq	32*7-128($np), $Y1, $TEMP0
 	vpaddq		$TEMP0, $ACC7, $ACC7
 	vpmuludq	32*8-128($np), $Y1, $TEMP1
 	 vmovd	%eax, $Y1
 	 #vmovdqu	32*1-8-128($np), $TEMP2		# moved below
 	vpaddq		$TEMP1, $ACC8, $ACC8
 	 #vmovdqu	32*2-8-128($np), $TEMP0		# moved below
 	 vpbroadcastq	$Y1, $Y1
 
 	vpmuludq	32*1-8-128($np), $Y2, $TEMP2	# see above
 	vmovdqu		32*3-8-128($np), $TEMP1
 	 mov	%rax, %rdx
 	 imulq	-128($np), %rax
 	vpaddq		$TEMP2, $ACC1, $ACC1
 	vpmuludq	32*2-8-128($np), $Y2, $TEMP0	# see above
 	vmovdqu		32*4-8-128($np), $TEMP2
 	 add	%rax, $r2
 	 mov	%rdx, %rax
 	 imulq	8-128($np), %rax
 	vpaddq		$TEMP0, $ACC2, $ACC2
 	 add	$r3, %rax
 	 shr	\$29, $r2
 	vpmuludq	$Y2, $TEMP1, $TEMP1
 	vmovdqu		32*5-8-128($np), $TEMP0
 	 add	$r2, %rax
 	vpaddq		$TEMP1, $ACC3, $ACC3
 	vpmuludq	$Y2, $TEMP2, $TEMP2
 	vmovdqu		32*6-8-128($np), $TEMP1
 	 .byte	0x67
 	 mov	%rax, $r3
 	 imull	$n0, %eax
 	vpaddq		$TEMP2, $ACC4, $ACC4
 	vpmuludq	$Y2, $TEMP0, $TEMP0
 	.byte	0xc4,0x41,0x7e,0x6f,0x9d,0x58,0x00,0x00,0x00	# vmovdqu		32*7-8-128($np), $TEMP2
 	 and	\$0x1fffffff, %eax
 	vpaddq		$TEMP0, $ACC5, $ACC5
 	vpmuludq	$Y2, $TEMP1, $TEMP1
 	vmovdqu		32*8-8-128($np), $TEMP0
 	vpaddq		$TEMP1, $ACC6, $ACC6
 	vpmuludq	$Y2, $TEMP2, $TEMP2
 	vmovdqu		32*9-8-128($np), $ACC9
 	 vmovd	%eax, $ACC0			# borrow ACC0 for Y2
 	 imulq	-128($np), %rax
 	vpaddq		$TEMP2, $ACC7, $ACC7
 	vpmuludq	$Y2, $TEMP0, $TEMP0
 	 vmovdqu	32*1-16-128($np), $TEMP1
 	 vpbroadcastq	$ACC0, $ACC0
 	vpaddq		$TEMP0, $ACC8, $ACC8
 	vpmuludq	$Y2, $ACC9, $ACC9
 	 vmovdqu	32*2-16-128($np), $TEMP2
 	 add	%rax, $r3
 
 ___
 ($ACC0,$Y2)=($Y2,$ACC0);
 $code.=<<___;
 	 vmovdqu	32*1-24-128($np), $ACC0
 	vpmuludq	$Y1, $TEMP1, $TEMP1
 	vmovdqu		32*3-16-128($np), $TEMP0
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	 vpmuludq	$Y2, $ACC0, $ACC0
 	vpmuludq	$Y1, $TEMP2, $TEMP2
 	.byte	0xc4,0x41,0x7e,0x6f,0xb5,0xf0,0xff,0xff,0xff	# vmovdqu		32*4-16-128($np), $TEMP1
 	 vpaddq		$ACC1, $ACC0, $ACC0
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vpmuludq	$Y1, $TEMP0, $TEMP0
 	vmovdqu		32*5-16-128($np), $TEMP2
 	 .byte	0x67
 	 vmovq		$ACC0, %rax
 	 vmovdqu	$ACC0, (%rsp)		# transfer $r0-$r3
 	vpaddq		$TEMP0, $ACC3, $ACC3
 	vpmuludq	$Y1, $TEMP1, $TEMP1
 	vmovdqu		32*6-16-128($np), $TEMP0
 	vpaddq		$TEMP1, $ACC4, $ACC4
 	vpmuludq	$Y1, $TEMP2, $TEMP2
 	vmovdqu		32*7-16-128($np), $TEMP1
 	vpaddq		$TEMP2, $ACC5, $ACC5
 	vpmuludq	$Y1, $TEMP0, $TEMP0
 	vmovdqu		32*8-16-128($np), $TEMP2
 	vpaddq		$TEMP0, $ACC6, $ACC6
 	vpmuludq	$Y1, $TEMP1, $TEMP1
 	 shr	\$29, $r3
 	vmovdqu		32*9-16-128($np), $TEMP0
 	 add	$r3, %rax
 	vpaddq		$TEMP1, $ACC7, $ACC7
 	vpmuludq	$Y1, $TEMP2, $TEMP2
 	 #vmovdqu	32*2-24-128($np), $TEMP1	# moved below
 	 mov	%rax, $r0
 	 imull	$n0, %eax
 	vpaddq		$TEMP2, $ACC8, $ACC8
 	vpmuludq	$Y1, $TEMP0, $TEMP0
 	 and	\$0x1fffffff, %eax
 	 vmovd	%eax, $Y1
 	 vmovdqu	32*3-24-128($np), $TEMP2
 	.byte	0x67
 	vpaddq		$TEMP0, $ACC9, $ACC9
 	 vpbroadcastq	$Y1, $Y1
 
 	vpmuludq	32*2-24-128($np), $Y2, $TEMP1	# see above
 	vmovdqu		32*4-24-128($np), $TEMP0
 	 mov	%rax, %rdx
 	 imulq	-128($np), %rax
 	 mov	8(%rsp), $r1
 	vpaddq		$TEMP1, $ACC2, $ACC1
 	vpmuludq	$Y2, $TEMP2, $TEMP2
 	vmovdqu		32*5-24-128($np), $TEMP1
 	 add	%rax, $r0
 	 mov	%rdx, %rax
 	 imulq	8-128($np), %rax
 	 .byte	0x67
 	 shr	\$29, $r0
 	 mov	16(%rsp), $r2
 	vpaddq		$TEMP2, $ACC3, $ACC2
 	vpmuludq	$Y2, $TEMP0, $TEMP0
 	vmovdqu		32*6-24-128($np), $TEMP2
 	 add	%rax, $r1
 	 mov	%rdx, %rax
 	 imulq	16-128($np), %rax
 	vpaddq		$TEMP0, $ACC4, $ACC3
 	vpmuludq	$Y2, $TEMP1, $TEMP1
 	vmovdqu		32*7-24-128($np), $TEMP0
 	 imulq	24-128($np), %rdx		# future $r3
 	 add	%rax, $r2
 	 lea	($r0,$r1), %rax
 	vpaddq		$TEMP1, $ACC5, $ACC4
 	vpmuludq	$Y2, $TEMP2, $TEMP2
 	vmovdqu		32*8-24-128($np), $TEMP1
 	 mov	%rax, $r1
 	 imull	$n0, %eax
 	vpmuludq	$Y2, $TEMP0, $TEMP0
 	vpaddq		$TEMP2, $ACC6, $ACC5
 	vmovdqu		32*9-24-128($np), $TEMP2
 	 and	\$0x1fffffff, %eax
 	vpaddq		$TEMP0, $ACC7, $ACC6
 	vpmuludq	$Y2, $TEMP1, $TEMP1
 	 add	24(%rsp), %rdx
 	vpaddq		$TEMP1, $ACC8, $ACC7
 	vpmuludq	$Y2, $TEMP2, $TEMP2
 	vpaddq		$TEMP2, $ACC9, $ACC8
 	 vmovq	$r3, $ACC9
 	 mov	%rdx, $r3
 
 	dec	$i
 	jnz	.LOOP_REDUCE_1024
 ___
 ($ACC0,$Y2)=($Y2,$ACC0);
 $code.=<<___;
 	lea	448(%rsp), $tp1			# size optimization
 	vpaddq	$ACC9, $Y2, $ACC0
 	vpxor	$ZERO, $ZERO, $ZERO
 
 	vpaddq		32*9-192($tp0), $ACC0, $ACC0
 	vpaddq		32*10-448($tp1), $ACC1, $ACC1
 	vpaddq		32*11-448($tp1), $ACC2, $ACC2
 	vpaddq		32*12-448($tp1), $ACC3, $ACC3
 	vpaddq		32*13-448($tp1), $ACC4, $ACC4
 	vpaddq		32*14-448($tp1), $ACC5, $ACC5
 	vpaddq		32*15-448($tp1), $ACC6, $ACC6
 	vpaddq		32*16-448($tp1), $ACC7, $ACC7
 	vpaddq		32*17-448($tp1), $ACC8, $ACC8
 
 	vpsrlq		\$29, $ACC0, $TEMP1
 	vpand		$AND_MASK, $ACC0, $ACC0
 	vpsrlq		\$29, $ACC1, $TEMP2
 	vpand		$AND_MASK, $ACC1, $ACC1
 	vpsrlq		\$29, $ACC2, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC2, $ACC2
 	vpsrlq		\$29, $ACC3, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC3, $ACC3
 	vpermq		\$0x93, $TEMP3, $TEMP3
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP4, $TEMP4
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC0, $ACC0
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vpblendd	\$3, $TEMP4, $ZERO, $TEMP4
 	vpaddq		$TEMP3, $ACC3, $ACC3
 	vpaddq		$TEMP4, $ACC4, $ACC4
 
 	vpsrlq		\$29, $ACC0, $TEMP1
 	vpand		$AND_MASK, $ACC0, $ACC0
 	vpsrlq		\$29, $ACC1, $TEMP2
 	vpand		$AND_MASK, $ACC1, $ACC1
 	vpsrlq		\$29, $ACC2, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC2, $ACC2
 	vpsrlq		\$29, $ACC3, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC3, $ACC3
 	vpermq		\$0x93, $TEMP3, $TEMP3
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP4, $TEMP4
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC0, $ACC0
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vmovdqu		$ACC0, 32*0-128($rp)
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vmovdqu		$ACC1, 32*1-128($rp)
 	vpblendd	\$3, $TEMP4, $ZERO, $TEMP4
 	vpaddq		$TEMP3, $ACC3, $ACC3
 	vmovdqu		$ACC2, 32*2-128($rp)
 	vpaddq		$TEMP4, $ACC4, $ACC4
 	vmovdqu		$ACC3, 32*3-128($rp)
 ___
 $TEMP5=$ACC0;
 $code.=<<___;
 	vpsrlq		\$29, $ACC4, $TEMP1
 	vpand		$AND_MASK, $ACC4, $ACC4
 	vpsrlq		\$29, $ACC5, $TEMP2
 	vpand		$AND_MASK, $ACC5, $ACC5
 	vpsrlq		\$29, $ACC6, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC6, $ACC6
 	vpsrlq		\$29, $ACC7, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC7, $ACC7
 	vpsrlq		\$29, $ACC8, $TEMP5
 	vpermq		\$0x93, $TEMP3, $TEMP3
 	vpand		$AND_MASK, $ACC8, $ACC8
 	vpermq		\$0x93, $TEMP4, $TEMP4
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP5, $TEMP5
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC4, $ACC4
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC5, $ACC5
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC6, $ACC6
 	vpblendd	\$3, $TEMP4, $TEMP5, $TEMP4
 	vpaddq		$TEMP3, $ACC7, $ACC7
 	vpaddq		$TEMP4, $ACC8, $ACC8
      
 	vpsrlq		\$29, $ACC4, $TEMP1
 	vpand		$AND_MASK, $ACC4, $ACC4
 	vpsrlq		\$29, $ACC5, $TEMP2
 	vpand		$AND_MASK, $ACC5, $ACC5
 	vpsrlq		\$29, $ACC6, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC6, $ACC6
 	vpsrlq		\$29, $ACC7, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC7, $ACC7
 	vpsrlq		\$29, $ACC8, $TEMP5
 	vpermq		\$0x93, $TEMP3, $TEMP3
 	vpand		$AND_MASK, $ACC8, $ACC8
 	vpermq		\$0x93, $TEMP4, $TEMP4
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP5, $TEMP5
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC4, $ACC4
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC5, $ACC5
 	vmovdqu		$ACC4, 32*4-128($rp)
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC6, $ACC6
 	vmovdqu		$ACC5, 32*5-128($rp)
 	vpblendd	\$3, $TEMP4, $TEMP5, $TEMP4
 	vpaddq		$TEMP3, $ACC7, $ACC7
 	vmovdqu		$ACC6, 32*6-128($rp)
 	vpaddq		$TEMP4, $ACC8, $ACC8
 	vmovdqu		$ACC7, 32*7-128($rp)
 	vmovdqu		$ACC8, 32*8-128($rp)
 
 	mov	$rp, $ap
 	dec	$rep
 	jne	.LOOP_GRANDE_SQR_1024
 
 	vzeroall
 	mov	%rbp, %rax
 ___
 $code.=<<___ if ($win64);
 	movaps	-0xd8(%rax),%xmm6
 	movaps	-0xc8(%rax),%xmm7
 	movaps	-0xb8(%rax),%xmm8
 	movaps	-0xa8(%rax),%xmm9
 	movaps	-0x98(%rax),%xmm10
 	movaps	-0x88(%rax),%xmm11
 	movaps	-0x78(%rax),%xmm12
 	movaps	-0x68(%rax),%xmm13
 	movaps	-0x58(%rax),%xmm14
 	movaps	-0x48(%rax),%xmm15
 ___
 $code.=<<___;
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	lea	(%rax),%rsp		# restore %rsp
 .Lsqr_1024_epilogue:
 	ret
 .size	rsaz_1024_sqr_avx2,.-rsaz_1024_sqr_avx2
 ___
 }
 
 { # void AMM_WW(
 my $rp="%rdi";	# BN_ULONG *rp,
 my $ap="%rsi";	# const BN_ULONG *ap,
 my $bp="%rdx";	# const BN_ULONG *bp,
 my $np="%rcx";	# const BN_ULONG *np,
 my $n0="%r8d";	# unsigned int n0);
 
 # The registers that hold the accumulated redundant result
 # The AMM works on 1024 bit operands, and redundant word size is 29
 # Therefore: ceil(1024/29)/4 = 9
 my $ACC0="%ymm0";
 my $ACC1="%ymm1";
 my $ACC2="%ymm2";
 my $ACC3="%ymm3";
 my $ACC4="%ymm4";
 my $ACC5="%ymm5";
 my $ACC6="%ymm6";
 my $ACC7="%ymm7";
 my $ACC8="%ymm8";
 my $ACC9="%ymm9";
 
 # Registers that hold the broadcasted words of multiplier, currently used
 my $Bi="%ymm10";
 my $Yi="%ymm11";
 
 # Helper registers
 my $TEMP0=$ACC0;
 my $TEMP1="%ymm12";
 my $TEMP2="%ymm13";
 my $ZERO="%ymm14";
 my $AND_MASK="%ymm15";
 
 # alu registers that hold the first words of the ACC
 my $r0="%r9";
 my $r1="%r10";
 my $r2="%r11";
 my $r3="%r12";
 
 my $i="%r14d";
 my $tmp="%r15";
 
 $bp="%r13";	# reassigned argument
 
 $code.=<<___;
 .globl	rsaz_1024_mul_avx2
 .type	rsaz_1024_mul_avx2,\@function,5
 .align	64
 rsaz_1024_mul_avx2:
 	lea	(%rsp), %rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 ___
 $code.=<<___ if ($win64);
 	vzeroupper
 	lea	-0xa8(%rsp),%rsp
 	vmovaps	%xmm6,-0xd8(%rax)
 	vmovaps	%xmm7,-0xc8(%rax)
 	vmovaps	%xmm8,-0xb8(%rax)
 	vmovaps	%xmm9,-0xa8(%rax)
 	vmovaps	%xmm10,-0x98(%rax)
 	vmovaps	%xmm11,-0x88(%rax)
 	vmovaps	%xmm12,-0x78(%rax)
 	vmovaps	%xmm13,-0x68(%rax)
 	vmovaps	%xmm14,-0x58(%rax)
 	vmovaps	%xmm15,-0x48(%rax)
 .Lmul_1024_body:
 ___
 $code.=<<___;
 	mov	%rax,%rbp
 	vzeroall
 	mov	%rdx, $bp	# reassigned argument
 	sub	\$64,%rsp
 
 	# unaligned 256-bit load that crosses page boundary can
 	# cause severe performance degradation here, so if $ap does
 	# cross page boundary, swap it with $bp [meaning that caller
 	# is advised to lay down $ap and $bp next to each other, so
 	# that only one can cross page boundary].
 	.byte	0x67,0x67
 	mov	$ap, $tmp
 	and	\$4095, $tmp
 	add	\$32*10, $tmp
 	shr	\$12, $tmp
 	mov	$ap, $tmp
 	cmovnz	$bp, $ap
 	cmovnz	$tmp, $bp
 
 	mov	$np, $tmp
 	sub	\$-128,$ap	# size optimization
 	sub	\$-128,$np
 	sub	\$-128,$rp
 
 	and	\$4095, $tmp	# see if $np crosses page
 	add	\$32*10, $tmp
 	.byte	0x67,0x67
 	shr	\$12, $tmp
 	jz	.Lmul_1024_no_n_copy
 
 	# unaligned 256-bit load that crosses page boundary can
 	# cause severe performance degradation here, so if $np does
 	# cross page boundary, copy it to stack and make sure stack
 	# frame doesn't...
 	sub		\$32*10,%rsp
 	vmovdqu		32*0-128($np), $ACC0
 	and		\$-512, %rsp
 	vmovdqu		32*1-128($np), $ACC1
 	vmovdqu		32*2-128($np), $ACC2
 	vmovdqu		32*3-128($np), $ACC3
 	vmovdqu		32*4-128($np), $ACC4
 	vmovdqu		32*5-128($np), $ACC5
 	vmovdqu		32*6-128($np), $ACC6
 	vmovdqu		32*7-128($np), $ACC7
 	vmovdqu		32*8-128($np), $ACC8
 	lea		64+128(%rsp),$np
 	vmovdqu		$ACC0, 32*0-128($np)
 	vpxor		$ACC0, $ACC0, $ACC0
 	vmovdqu		$ACC1, 32*1-128($np)
 	vpxor		$ACC1, $ACC1, $ACC1
 	vmovdqu		$ACC2, 32*2-128($np)
 	vpxor		$ACC2, $ACC2, $ACC2
 	vmovdqu		$ACC3, 32*3-128($np)
 	vpxor		$ACC3, $ACC3, $ACC3
 	vmovdqu		$ACC4, 32*4-128($np)
 	vpxor		$ACC4, $ACC4, $ACC4
 	vmovdqu		$ACC5, 32*5-128($np)
 	vpxor		$ACC5, $ACC5, $ACC5
 	vmovdqu		$ACC6, 32*6-128($np)
 	vpxor		$ACC6, $ACC6, $ACC6
 	vmovdqu		$ACC7, 32*7-128($np)
 	vpxor		$ACC7, $ACC7, $ACC7
 	vmovdqu		$ACC8, 32*8-128($np)
 	vmovdqa		$ACC0, $ACC8
 	vmovdqu		$ACC9, 32*9-128($np)	# $ACC9 is zero after vzeroall
 .Lmul_1024_no_n_copy:
 	and	\$-64,%rsp
 
 	mov	($bp), %rbx
 	vpbroadcastq ($bp), $Bi
 	vmovdqu	$ACC0, (%rsp)			# clear top of stack
 	xor	$r0, $r0
 	.byte	0x67
 	xor	$r1, $r1
 	xor	$r2, $r2
 	xor	$r3, $r3
 
 	vmovdqu	.Land_mask(%rip), $AND_MASK
 	mov	\$9, $i
 	vmovdqu	$ACC9, 32*9-128($rp)		# $ACC9 is zero after vzeroall
 	jmp	.Loop_mul_1024
 
 .align	32
 .Loop_mul_1024:
 	 vpsrlq		\$29, $ACC3, $ACC9		# correct $ACC3(*)
 	mov	%rbx, %rax
 	imulq	-128($ap), %rax
 	add	$r0, %rax
 	mov	%rbx, $r1
 	imulq	8-128($ap), $r1
 	add	8(%rsp), $r1
 
 	mov	%rax, $r0
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 
 	 mov	%rbx, $r2
 	 imulq	16-128($ap), $r2
 	 add	16(%rsp), $r2
 
 	 mov	%rbx, $r3
 	 imulq	24-128($ap), $r3
 	 add	24(%rsp), $r3
 	vpmuludq	32*1-128($ap),$Bi,$TEMP0
 	 vmovd		%eax, $Yi
 	vpaddq		$TEMP0,$ACC1,$ACC1
 	vpmuludq	32*2-128($ap),$Bi,$TEMP1
 	 vpbroadcastq	$Yi, $Yi
 	vpaddq		$TEMP1,$ACC2,$ACC2
 	vpmuludq	32*3-128($ap),$Bi,$TEMP2
 	 vpand		$AND_MASK, $ACC3, $ACC3		# correct $ACC3
 	vpaddq		$TEMP2,$ACC3,$ACC3
 	vpmuludq	32*4-128($ap),$Bi,$TEMP0
 	vpaddq		$TEMP0,$ACC4,$ACC4
 	vpmuludq	32*5-128($ap),$Bi,$TEMP1
 	vpaddq		$TEMP1,$ACC5,$ACC5
 	vpmuludq	32*6-128($ap),$Bi,$TEMP2
 	vpaddq		$TEMP2,$ACC6,$ACC6
 	vpmuludq	32*7-128($ap),$Bi,$TEMP0
 	 vpermq		\$0x93, $ACC9, $ACC9		# correct $ACC3
 	vpaddq		$TEMP0,$ACC7,$ACC7
 	vpmuludq	32*8-128($ap),$Bi,$TEMP1
 	 vpbroadcastq	8($bp), $Bi
 	vpaddq		$TEMP1,$ACC8,$ACC8
 
 	mov	%rax,%rdx
 	imulq	-128($np),%rax
 	add	%rax,$r0
 	mov	%rdx,%rax
 	imulq	8-128($np),%rax
 	add	%rax,$r1
 	mov	%rdx,%rax
 	imulq	16-128($np),%rax
 	add	%rax,$r2
 	shr	\$29, $r0
 	imulq	24-128($np),%rdx
 	add	%rdx,$r3
 	add	$r0, $r1
 
 	vpmuludq	32*1-128($np),$Yi,$TEMP2
 	 vmovq		$Bi, %rbx
 	vpaddq		$TEMP2,$ACC1,$ACC1
 	vpmuludq	32*2-128($np),$Yi,$TEMP0
 	vpaddq		$TEMP0,$ACC2,$ACC2
 	vpmuludq	32*3-128($np),$Yi,$TEMP1
 	vpaddq		$TEMP1,$ACC3,$ACC3
 	vpmuludq	32*4-128($np),$Yi,$TEMP2
 	vpaddq		$TEMP2,$ACC4,$ACC4
 	vpmuludq	32*5-128($np),$Yi,$TEMP0
 	vpaddq		$TEMP0,$ACC5,$ACC5
 	vpmuludq	32*6-128($np),$Yi,$TEMP1
 	vpaddq		$TEMP1,$ACC6,$ACC6
 	vpmuludq	32*7-128($np),$Yi,$TEMP2
 	 vpblendd	\$3, $ZERO, $ACC9, $ACC9	# correct $ACC3
 	vpaddq		$TEMP2,$ACC7,$ACC7
 	vpmuludq	32*8-128($np),$Yi,$TEMP0
 	 vpaddq		$ACC9, $ACC3, $ACC3		# correct $ACC3
 	vpaddq		$TEMP0,$ACC8,$ACC8
 
 	mov	%rbx, %rax
 	imulq	-128($ap),%rax
 	add	%rax,$r1
 	 vmovdqu	-8+32*1-128($ap),$TEMP1
 	mov	%rbx, %rax
 	imulq	8-128($ap),%rax
 	add	%rax,$r2
 	 vmovdqu	-8+32*2-128($ap),$TEMP2
 
 	mov	$r1, %rax
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 
 	 imulq	16-128($ap),%rbx
 	 add	%rbx,$r3
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	 vmovd		%eax, $Yi
 	vmovdqu		-8+32*3-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC1,$ACC1
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	 vpbroadcastq	$Yi, $Yi
 	vmovdqu		-8+32*4-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC2,$ACC2
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-8+32*5-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC3,$ACC3
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vmovdqu		-8+32*6-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC4,$ACC4
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vmovdqu		-8+32*7-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC5,$ACC5
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-8+32*8-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC6,$ACC6
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vmovdqu		-8+32*9-128($ap),$ACC9
 	vpaddq		$TEMP1,$ACC7,$ACC7
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vpaddq		$TEMP2,$ACC8,$ACC8
 	vpmuludq	$Bi,$ACC9,$ACC9
 	 vpbroadcastq	16($bp), $Bi
 
 	mov	%rax,%rdx
 	imulq	-128($np),%rax
 	add	%rax,$r1
 	 vmovdqu	-8+32*1-128($np),$TEMP0
 	mov	%rdx,%rax
 	imulq	8-128($np),%rax
 	add	%rax,$r2
 	 vmovdqu	-8+32*2-128($np),$TEMP1
 	shr	\$29, $r1
 	imulq	16-128($np),%rdx
 	add	%rdx,$r3
 	add	$r1, $r2
 
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	 vmovq		$Bi, %rbx
 	vmovdqu		-8+32*3-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC1,$ACC1
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vmovdqu		-8+32*4-128($np),$TEMP0
 	vpaddq		$TEMP1,$ACC2,$ACC2
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-8+32*5-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC3,$ACC3
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-8+32*6-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC4,$ACC4
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vmovdqu		-8+32*7-128($np),$TEMP0
 	vpaddq		$TEMP1,$ACC5,$ACC5
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-8+32*8-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC6,$ACC6
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-8+32*9-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC7,$ACC7
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vpaddq		$TEMP1,$ACC8,$ACC8
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vpaddq		$TEMP2,$ACC9,$ACC9
 
 	 vmovdqu	-16+32*1-128($ap),$TEMP0
 	mov	%rbx,%rax
 	imulq	-128($ap),%rax
 	add	$r2,%rax
 
 	 vmovdqu	-16+32*2-128($ap),$TEMP1
 	mov	%rax,$r2
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 
 	 imulq	8-128($ap),%rbx
 	 add	%rbx,$r3
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	 vmovd		%eax, $Yi
 	vmovdqu		-16+32*3-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC1,$ACC1
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	 vpbroadcastq	$Yi, $Yi
 	vmovdqu		-16+32*4-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC2,$ACC2
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vmovdqu		-16+32*5-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC3,$ACC3
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-16+32*6-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC4,$ACC4
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vmovdqu		-16+32*7-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC5,$ACC5
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vmovdqu		-16+32*8-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC6,$ACC6
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-16+32*9-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC7,$ACC7
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vpaddq		$TEMP1,$ACC8,$ACC8
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	 vpbroadcastq	24($bp), $Bi
 	vpaddq		$TEMP2,$ACC9,$ACC9
 
 	 vmovdqu	-16+32*1-128($np),$TEMP0
 	mov	%rax,%rdx
 	imulq	-128($np),%rax
 	add	%rax,$r2
 	 vmovdqu	-16+32*2-128($np),$TEMP1
 	imulq	8-128($np),%rdx
 	add	%rdx,$r3
 	shr	\$29, $r2
 
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	 vmovq		$Bi, %rbx
 	vmovdqu		-16+32*3-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC1,$ACC1
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vmovdqu		-16+32*4-128($np),$TEMP0
 	vpaddq		$TEMP1,$ACC2,$ACC2
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-16+32*5-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC3,$ACC3
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-16+32*6-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC4,$ACC4
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vmovdqu		-16+32*7-128($np),$TEMP0
 	vpaddq		$TEMP1,$ACC5,$ACC5
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-16+32*8-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC6,$ACC6
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-16+32*9-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC7,$ACC7
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	 vmovdqu	-24+32*1-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC8,$ACC8
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	 vmovdqu	-24+32*2-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC9,$ACC9
 
 	add	$r2, $r3
 	imulq	-128($ap),%rbx
 	add	%rbx,$r3
 
 	mov	$r3, %rax
 	imull	$n0, %eax
 	and	\$0x1fffffff, %eax
 
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	 vmovd		%eax, $Yi
 	vmovdqu		-24+32*3-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC1,$ACC1
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	 vpbroadcastq	$Yi, $Yi
 	vmovdqu		-24+32*4-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC2,$ACC2
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vmovdqu		-24+32*5-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC3,$ACC3
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-24+32*6-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC4,$ACC4
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vmovdqu		-24+32*7-128($ap),$TEMP0
 	vpaddq		$TEMP1,$ACC5,$ACC5
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	vmovdqu		-24+32*8-128($ap),$TEMP1
 	vpaddq		$TEMP2,$ACC6,$ACC6
 	vpmuludq	$Bi,$TEMP0,$TEMP0
 	vmovdqu		-24+32*9-128($ap),$TEMP2
 	vpaddq		$TEMP0,$ACC7,$ACC7
 	vpmuludq	$Bi,$TEMP1,$TEMP1
 	vpaddq		$TEMP1,$ACC8,$ACC8
 	vpmuludq	$Bi,$TEMP2,$TEMP2
 	 vpbroadcastq	32($bp), $Bi
 	vpaddq		$TEMP2,$ACC9,$ACC9
 	 add		\$32, $bp			# $bp++
 
 	vmovdqu		-24+32*1-128($np),$TEMP0
 	imulq	-128($np),%rax
 	add	%rax,$r3
 	shr	\$29, $r3
 
 	vmovdqu		-24+32*2-128($np),$TEMP1
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	 vmovq		$Bi, %rbx
 	vmovdqu		-24+32*3-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC1,$ACC0		# $ACC0==$TEMP0
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	 vmovdqu	$ACC0, (%rsp)			# transfer $r0-$r3
 	vpaddq		$TEMP1,$ACC2,$ACC1
 	vmovdqu		-24+32*4-128($np),$TEMP0
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-24+32*5-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC3,$ACC2
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-24+32*6-128($np),$TEMP2
 	vpaddq		$TEMP0,$ACC4,$ACC3
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	vmovdqu		-24+32*7-128($np),$TEMP0
 	vpaddq		$TEMP1,$ACC5,$ACC4
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	vmovdqu		-24+32*8-128($np),$TEMP1
 	vpaddq		$TEMP2,$ACC6,$ACC5
 	vpmuludq	$Yi,$TEMP0,$TEMP0
 	vmovdqu		-24+32*9-128($np),$TEMP2
 	 mov	$r3, $r0
 	vpaddq		$TEMP0,$ACC7,$ACC6
 	vpmuludq	$Yi,$TEMP1,$TEMP1
 	 add	(%rsp), $r0
 	vpaddq		$TEMP1,$ACC8,$ACC7
 	vpmuludq	$Yi,$TEMP2,$TEMP2
 	 vmovq	$r3, $TEMP1
 	vpaddq		$TEMP2,$ACC9,$ACC8
 
 	dec	$i
 	jnz	.Loop_mul_1024
 ___
 
 # (*)	Original implementation was correcting ACC1-ACC3 for overflow
 #	after 7 loop runs, or after 28 iterations, or 56 additions.
 #	But as we underutilize resources, it's possible to correct in
 #	each iteration with marginal performance loss. But then, as
 #	we do it in each iteration, we can correct less digits, and
 #	avoid performance penalties completely. Also note that we
 #	correct only three digits out of four. This works because
 #	most significant digit is subjected to less additions.
 
 $TEMP0 = $ACC9;
 $TEMP3 = $Bi;
 $TEMP4 = $Yi;
 $code.=<<___;
 	vpermq		\$0, $AND_MASK, $AND_MASK
 	vpaddq		(%rsp), $TEMP1, $ACC0
 
 	vpsrlq		\$29, $ACC0, $TEMP1
 	vpand		$AND_MASK, $ACC0, $ACC0
 	vpsrlq		\$29, $ACC1, $TEMP2
 	vpand		$AND_MASK, $ACC1, $ACC1
 	vpsrlq		\$29, $ACC2, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC2, $ACC2
 	vpsrlq		\$29, $ACC3, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC3, $ACC3
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP3, $TEMP3
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpermq		\$0x93, $TEMP4, $TEMP4
 	vpaddq		$TEMP0, $ACC0, $ACC0
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vpblendd	\$3, $TEMP4, $ZERO, $TEMP4
 	vpaddq		$TEMP3, $ACC3, $ACC3
 	vpaddq		$TEMP4, $ACC4, $ACC4
 
 	vpsrlq		\$29, $ACC0, $TEMP1
 	vpand		$AND_MASK, $ACC0, $ACC0
 	vpsrlq		\$29, $ACC1, $TEMP2
 	vpand		$AND_MASK, $ACC1, $ACC1
 	vpsrlq		\$29, $ACC2, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC2, $ACC2
 	vpsrlq		\$29, $ACC3, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC3, $ACC3
 	vpermq		\$0x93, $TEMP3, $TEMP3
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP4, $TEMP4
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC0, $ACC0
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC1, $ACC1
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC2, $ACC2
 	vpblendd	\$3, $TEMP4, $ZERO, $TEMP4
 	vpaddq		$TEMP3, $ACC3, $ACC3
 	vpaddq		$TEMP4, $ACC4, $ACC4
 
 	vmovdqu		$ACC0, 0-128($rp)
 	vmovdqu		$ACC1, 32-128($rp)
 	vmovdqu		$ACC2, 64-128($rp)
 	vmovdqu		$ACC3, 96-128($rp)
 ___
 
 $TEMP5=$ACC0;
 $code.=<<___;
 	vpsrlq		\$29, $ACC4, $TEMP1
 	vpand		$AND_MASK, $ACC4, $ACC4
 	vpsrlq		\$29, $ACC5, $TEMP2
 	vpand		$AND_MASK, $ACC5, $ACC5
 	vpsrlq		\$29, $ACC6, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC6, $ACC6
 	vpsrlq		\$29, $ACC7, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC7, $ACC7
 	vpsrlq		\$29, $ACC8, $TEMP5
 	vpermq		\$0x93, $TEMP3, $TEMP3
 	vpand		$AND_MASK, $ACC8, $ACC8
 	vpermq		\$0x93, $TEMP4, $TEMP4
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP5, $TEMP5
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC4, $ACC4
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC5, $ACC5
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC6, $ACC6
 	vpblendd	\$3, $TEMP4, $TEMP5, $TEMP4
 	vpaddq		$TEMP3, $ACC7, $ACC7
 	vpaddq		$TEMP4, $ACC8, $ACC8
 
 	vpsrlq		\$29, $ACC4, $TEMP1
 	vpand		$AND_MASK, $ACC4, $ACC4
 	vpsrlq		\$29, $ACC5, $TEMP2
 	vpand		$AND_MASK, $ACC5, $ACC5
 	vpsrlq		\$29, $ACC6, $TEMP3
 	vpermq		\$0x93, $TEMP1, $TEMP1
 	vpand		$AND_MASK, $ACC6, $ACC6
 	vpsrlq		\$29, $ACC7, $TEMP4
 	vpermq		\$0x93, $TEMP2, $TEMP2
 	vpand		$AND_MASK, $ACC7, $ACC7
 	vpsrlq		\$29, $ACC8, $TEMP5
 	vpermq		\$0x93, $TEMP3, $TEMP3
 	vpand		$AND_MASK, $ACC8, $ACC8
 	vpermq		\$0x93, $TEMP4, $TEMP4
 
 	vpblendd	\$3, $ZERO, $TEMP1, $TEMP0
 	vpermq		\$0x93, $TEMP5, $TEMP5
 	vpblendd	\$3, $TEMP1, $TEMP2, $TEMP1
 	vpaddq		$TEMP0, $ACC4, $ACC4
 	vpblendd	\$3, $TEMP2, $TEMP3, $TEMP2
 	vpaddq		$TEMP1, $ACC5, $ACC5
 	vpblendd	\$3, $TEMP3, $TEMP4, $TEMP3
 	vpaddq		$TEMP2, $ACC6, $ACC6
 	vpblendd	\$3, $TEMP4, $TEMP5, $TEMP4
 	vpaddq		$TEMP3, $ACC7, $ACC7
 	vpaddq		$TEMP4, $ACC8, $ACC8
 
 	vmovdqu		$ACC4, 128-128($rp)
 	vmovdqu		$ACC5, 160-128($rp)    
 	vmovdqu		$ACC6, 192-128($rp)
 	vmovdqu		$ACC7, 224-128($rp)
 	vmovdqu		$ACC8, 256-128($rp)
 	vzeroupper
 
 	mov	%rbp, %rax
 ___
 $code.=<<___ if ($win64);
 	movaps	-0xd8(%rax),%xmm6
 	movaps	-0xc8(%rax),%xmm7
 	movaps	-0xb8(%rax),%xmm8
 	movaps	-0xa8(%rax),%xmm9
 	movaps	-0x98(%rax),%xmm10
 	movaps	-0x88(%rax),%xmm11
 	movaps	-0x78(%rax),%xmm12
 	movaps	-0x68(%rax),%xmm13
 	movaps	-0x58(%rax),%xmm14
 	movaps	-0x48(%rax),%xmm15
 ___
 $code.=<<___;
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	lea	(%rax),%rsp		# restore %rsp
 .Lmul_1024_epilogue:
 	ret
 .size	rsaz_1024_mul_avx2,.-rsaz_1024_mul_avx2
 ___
 }
 {
 my ($out,$inp) = $win64 ? ("%rcx","%rdx") : ("%rdi","%rsi");
 my @T = map("%r$_",(8..11));
 
 $code.=<<___;
 .globl	rsaz_1024_red2norm_avx2
 .type	rsaz_1024_red2norm_avx2,\@abi-omnipotent
 .align	32
 rsaz_1024_red2norm_avx2:
 	sub	\$-128,$inp	# size optimization
 	xor	%rax,%rax
 ___
 
 for ($j=0,$i=0; $i<16; $i++) {
     my $k=0;
     while (29*$j<64*($i+1)) {	# load data till boundary
 	$code.="	mov	`8*$j-128`($inp), @T[0]\n";
 	$j++; $k++; push(@T,shift(@T));
     }
     $l=$k;
     while ($k>1) {		# shift loaded data but last value
 	$code.="	shl	\$`29*($j-$k)`,@T[-$k]\n";
 	$k--;
     }
     $code.=<<___;		# shift last value
 	mov	@T[-1], @T[0]
 	shl	\$`29*($j-1)`, @T[-1]
 	shr	\$`-29*($j-1)`, @T[0]
 ___
     while ($l) {		# accumulate all values
 	$code.="	add	@T[-$l], %rax\n";
 	$l--;
     }
 	$code.=<<___;
 	adc	\$0, @T[0]	# consume eventual carry
 	mov	%rax, 8*$i($out)
 	mov	@T[0], %rax
 ___
     push(@T,shift(@T));
 }
 $code.=<<___;
 	ret
 .size	rsaz_1024_red2norm_avx2,.-rsaz_1024_red2norm_avx2
 
 .globl	rsaz_1024_norm2red_avx2
 .type	rsaz_1024_norm2red_avx2,\@abi-omnipotent
 .align	32
 rsaz_1024_norm2red_avx2:
 	sub	\$-128,$out	# size optimization
 	mov	($inp),@T[0]
 	mov	\$0x1fffffff,%eax
 ___
 for ($j=0,$i=0; $i<16; $i++) {
     $code.="	mov	`8*($i+1)`($inp),@T[1]\n"	if ($i<15);
     $code.="	xor	@T[1],@T[1]\n"			if ($i==15);
     my $k=1;
     while (29*($j+1)<64*($i+1)) {
     	$code.=<<___;
 	mov	@T[0],@T[-$k]
 	shr	\$`29*$j`,@T[-$k]
 	and	%rax,@T[-$k]				# &0x1fffffff
 	mov	@T[-$k],`8*$j-128`($out)
 ___
 	$j++; $k++;
     }
     $code.=<<___;
 	shrd	\$`29*$j`,@T[1],@T[0]
 	and	%rax,@T[0]
 	mov	@T[0],`8*$j-128`($out)
 ___
     $j++;
     push(@T,shift(@T));
 }
 $code.=<<___;
 	mov	@T[0],`8*$j-128`($out)			# zero
 	mov	@T[0],`8*($j+1)-128`($out)
 	mov	@T[0],`8*($j+2)-128`($out)
 	mov	@T[0],`8*($j+3)-128`($out)
 	ret
 .size	rsaz_1024_norm2red_avx2,.-rsaz_1024_norm2red_avx2
 ___
 }
 {
 my ($out,$inp,$power) = $win64 ? ("%rcx","%rdx","%r8d") : ("%rdi","%rsi","%edx");
 
 $code.=<<___;
 .globl	rsaz_1024_scatter5_avx2
 .type	rsaz_1024_scatter5_avx2,\@abi-omnipotent
 .align	32
 rsaz_1024_scatter5_avx2:
 	vzeroupper
 	vmovdqu	.Lscatter_permd(%rip),%ymm5
 	shl	\$4,$power
 	lea	($out,$power),$out
 	mov	\$9,%eax
 	jmp	.Loop_scatter_1024
 
 .align	32
 .Loop_scatter_1024:
 	vmovdqu		($inp),%ymm0
 	lea		32($inp),$inp
 	vpermd		%ymm0,%ymm5,%ymm0
 	vmovdqu		%xmm0,($out)
 	lea		16*32($out),$out
 	dec	%eax
 	jnz	.Loop_scatter_1024
 
 	vzeroupper
 	ret
 .size	rsaz_1024_scatter5_avx2,.-rsaz_1024_scatter5_avx2
 
 .globl	rsaz_1024_gather5_avx2
 .type	rsaz_1024_gather5_avx2,\@abi-omnipotent
 .align	32
 rsaz_1024_gather5_avx2:
+	vzeroupper
+	mov	%rsp,%r11
 ___
 $code.=<<___ if ($win64);
 	lea	-0x88(%rsp),%rax
-	vzeroupper
 .LSEH_begin_rsaz_1024_gather5:
 	# I can't trust assembler to use specific encoding:-(
-	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax),%rsp
-	.byte	0xc5,0xf8,0x29,0x70,0xe0	#vmovaps %xmm6,-0x20(%rax)
-	.byte	0xc5,0xf8,0x29,0x78,0xf0	#vmovaps %xmm7,-0x10(%rax)
-	.byte	0xc5,0x78,0x29,0x40,0x00	#vmovaps %xmm8,0(%rax)
-	.byte	0xc5,0x78,0x29,0x48,0x10	#vmovaps %xmm9,0x10(%rax)
-	.byte	0xc5,0x78,0x29,0x50,0x20	#vmovaps %xmm10,0x20(%rax)
-	.byte	0xc5,0x78,0x29,0x58,0x30	#vmovaps %xmm11,0x30(%rax)
-	.byte	0xc5,0x78,0x29,0x60,0x40	#vmovaps %xmm12,0x40(%rax)
-	.byte	0xc5,0x78,0x29,0x68,0x50	#vmovaps %xmm13,0x50(%rax)
-	.byte	0xc5,0x78,0x29,0x70,0x60	#vmovaps %xmm14,0x60(%rax)
-	.byte	0xc5,0x78,0x29,0x78,0x70	#vmovaps %xmm15,0x70(%rax)
+	.byte	0x48,0x8d,0x60,0xe0		# lea	-0x20(%rax),%rsp
+	.byte	0xc5,0xf8,0x29,0x70,0xe0	# vmovaps %xmm6,-0x20(%rax)
+	.byte	0xc5,0xf8,0x29,0x78,0xf0	# vmovaps %xmm7,-0x10(%rax)
+	.byte	0xc5,0x78,0x29,0x40,0x00	# vmovaps %xmm8,0(%rax)
+	.byte	0xc5,0x78,0x29,0x48,0x10	# vmovaps %xmm9,0x10(%rax)
+	.byte	0xc5,0x78,0x29,0x50,0x20	# vmovaps %xmm10,0x20(%rax)
+	.byte	0xc5,0x78,0x29,0x58,0x30	# vmovaps %xmm11,0x30(%rax)
+	.byte	0xc5,0x78,0x29,0x60,0x40	# vmovaps %xmm12,0x40(%rax)
+	.byte	0xc5,0x78,0x29,0x68,0x50	# vmovaps %xmm13,0x50(%rax)
+	.byte	0xc5,0x78,0x29,0x70,0x60	# vmovaps %xmm14,0x60(%rax)
+	.byte	0xc5,0x78,0x29,0x78,0x70	# vmovaps %xmm15,0x70(%rax)
 ___
 $code.=<<___;
-	lea	.Lgather_table(%rip),%r11
-	mov	$power,%eax
-	and	\$3,$power
-	shr	\$2,%eax			# cache line number
-	shl	\$4,$power			# offset within cache line
+	lea	-0x100(%rsp),%rsp
+	and	\$-32, %rsp
+	lea	.Linc(%rip), %r10
+	lea	-128(%rsp),%rax			# control u-op density
 
-	vmovdqu		-32(%r11),%ymm7		# .Lgather_permd
-	vpbroadcastb	8(%r11,%rax), %xmm8
-	vpbroadcastb	7(%r11,%rax), %xmm9
-	vpbroadcastb	6(%r11,%rax), %xmm10
-	vpbroadcastb	5(%r11,%rax), %xmm11
-	vpbroadcastb	4(%r11,%rax), %xmm12
-	vpbroadcastb	3(%r11,%rax), %xmm13
-	vpbroadcastb	2(%r11,%rax), %xmm14
-	vpbroadcastb	1(%r11,%rax), %xmm15
+	vmovd		$power, %xmm4
+	vmovdqa		(%r10),%ymm0
+	vmovdqa		32(%r10),%ymm1
+	vmovdqa		64(%r10),%ymm5
+	vpbroadcastd	%xmm4,%ymm4
 
-	lea	64($inp,$power),$inp
-	mov	\$64,%r11			# size optimization
-	mov	\$9,%eax
-	jmp	.Loop_gather_1024
+	vpaddd		%ymm5, %ymm0, %ymm2
+	vpcmpeqd	%ymm4, %ymm0, %ymm0
+	vpaddd		%ymm5, %ymm1, %ymm3
+	vpcmpeqd	%ymm4, %ymm1, %ymm1
+	vmovdqa		%ymm0, 32*0+128(%rax)
+	vpaddd		%ymm5, %ymm2, %ymm0
+	vpcmpeqd	%ymm4, %ymm2, %ymm2
+	vmovdqa		%ymm1, 32*1+128(%rax)
+	vpaddd		%ymm5, %ymm3, %ymm1
+	vpcmpeqd	%ymm4, %ymm3, %ymm3
+	vmovdqa		%ymm2, 32*2+128(%rax)
+	vpaddd		%ymm5, %ymm0, %ymm2
+	vpcmpeqd	%ymm4, %ymm0, %ymm0
+	vmovdqa		%ymm3, 32*3+128(%rax)
+	vpaddd		%ymm5, %ymm1, %ymm3
+	vpcmpeqd	%ymm4, %ymm1, %ymm1
+	vmovdqa		%ymm0, 32*4+128(%rax)
+	vpaddd		%ymm5, %ymm2, %ymm8
+	vpcmpeqd	%ymm4, %ymm2, %ymm2
+	vmovdqa		%ymm1, 32*5+128(%rax)
+	vpaddd		%ymm5, %ymm3, %ymm9
+	vpcmpeqd	%ymm4, %ymm3, %ymm3
+	vmovdqa		%ymm2, 32*6+128(%rax)
+	vpaddd		%ymm5, %ymm8, %ymm10
+	vpcmpeqd	%ymm4, %ymm8, %ymm8
+	vmovdqa		%ymm3, 32*7+128(%rax)
+	vpaddd		%ymm5, %ymm9, %ymm11
+	vpcmpeqd	%ymm4, %ymm9, %ymm9
+	vpaddd		%ymm5, %ymm10, %ymm12
+	vpcmpeqd	%ymm4, %ymm10, %ymm10
+	vpaddd		%ymm5, %ymm11, %ymm13
+	vpcmpeqd	%ymm4, %ymm11, %ymm11
+	vpaddd		%ymm5, %ymm12, %ymm14
+	vpcmpeqd	%ymm4, %ymm12, %ymm12
+	vpaddd		%ymm5, %ymm13, %ymm15
+	vpcmpeqd	%ymm4, %ymm13, %ymm13
+	vpcmpeqd	%ymm4, %ymm14, %ymm14
+	vpcmpeqd	%ymm4, %ymm15, %ymm15
 
-.align	32
+	vmovdqa	-32(%r10),%ymm7			# .Lgather_permd
+	lea	128($inp), $inp
+	mov	\$9,$power
+
 .Loop_gather_1024:
-	vpand		-64($inp),		%xmm8,%xmm0
-	vpand		($inp),			%xmm9,%xmm1
-	vpand		64($inp),		%xmm10,%xmm2
-	vpand		($inp,%r11,2),		%xmm11,%xmm3
-	 vpor					%xmm0,%xmm1,%xmm1
-	vpand		64($inp,%r11,2),	%xmm12,%xmm4
-	 vpor					%xmm2,%xmm3,%xmm3
-	vpand		($inp,%r11,4),		%xmm13,%xmm5
-	 vpor					%xmm1,%xmm3,%xmm3
-	vpand		64($inp,%r11,4),	%xmm14,%xmm6
-	 vpor					%xmm4,%xmm5,%xmm5
-	vpand		-128($inp,%r11,8),	%xmm15,%xmm2
-	lea		($inp,%r11,8),$inp
-	 vpor					%xmm3,%xmm5,%xmm5
-	 vpor					%xmm2,%xmm6,%xmm6
-	 vpor					%xmm5,%xmm6,%xmm6
-	vpermd		%ymm6,%ymm7,%ymm6
-	vmovdqu		%ymm6,($out)
+	vmovdqa		32*0-128($inp),	%ymm0
+	vmovdqa		32*1-128($inp),	%ymm1
+	vmovdqa		32*2-128($inp),	%ymm2
+	vmovdqa		32*3-128($inp),	%ymm3
+	vpand		32*0+128(%rax),	%ymm0,	%ymm0
+	vpand		32*1+128(%rax),	%ymm1,	%ymm1
+	vpand		32*2+128(%rax),	%ymm2,	%ymm2
+	vpor		%ymm0, %ymm1, %ymm4
+	vpand		32*3+128(%rax),	%ymm3,	%ymm3
+	vmovdqa		32*4-128($inp),	%ymm0
+	vmovdqa		32*5-128($inp),	%ymm1
+	vpor		%ymm2, %ymm3, %ymm5
+	vmovdqa		32*6-128($inp),	%ymm2
+	vmovdqa		32*7-128($inp),	%ymm3
+	vpand		32*4+128(%rax),	%ymm0,	%ymm0
+	vpand		32*5+128(%rax),	%ymm1,	%ymm1
+	vpand		32*6+128(%rax),	%ymm2,	%ymm2
+	vpor		%ymm0, %ymm4, %ymm4
+	vpand		32*7+128(%rax),	%ymm3,	%ymm3
+	vpand		32*8-128($inp),	%ymm8,	%ymm0
+	vpor		%ymm1, %ymm5, %ymm5
+	vpand		32*9-128($inp),	%ymm9,	%ymm1
+	vpor		%ymm2, %ymm4, %ymm4
+	vpand		32*10-128($inp),%ymm10,	%ymm2
+	vpor		%ymm3, %ymm5, %ymm5
+	vpand		32*11-128($inp),%ymm11,	%ymm3
+	vpor		%ymm0, %ymm4, %ymm4
+	vpand		32*12-128($inp),%ymm12,	%ymm0
+	vpor		%ymm1, %ymm5, %ymm5
+	vpand		32*13-128($inp),%ymm13,	%ymm1
+	vpor		%ymm2, %ymm4, %ymm4
+	vpand		32*14-128($inp),%ymm14,	%ymm2
+	vpor		%ymm3, %ymm5, %ymm5
+	vpand		32*15-128($inp),%ymm15,	%ymm3
+	lea		32*16($inp), $inp
+	vpor		%ymm0, %ymm4, %ymm4
+	vpor		%ymm1, %ymm5, %ymm5
+	vpor		%ymm2, %ymm4, %ymm4
+	vpor		%ymm3, %ymm5, %ymm5
+
+	vpor		%ymm5, %ymm4, %ymm4
+	vextracti128	\$1, %ymm4, %xmm5	# upper half is cleared
+	vpor		%xmm4, %xmm5, %xmm5
+	vpermd		%ymm5,%ymm7,%ymm5
+	vmovdqu		%ymm5,($out)
 	lea		32($out),$out
-	dec	%eax
+	dec	$power
 	jnz	.Loop_gather_1024
 
 	vpxor	%ymm0,%ymm0,%ymm0
 	vmovdqu	%ymm0,($out)
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
-	movaps	(%rsp),%xmm6
-	movaps	0x10(%rsp),%xmm7
-	movaps	0x20(%rsp),%xmm8
-	movaps	0x30(%rsp),%xmm9
-	movaps	0x40(%rsp),%xmm10
-	movaps	0x50(%rsp),%xmm11
-	movaps	0x60(%rsp),%xmm12
-	movaps	0x70(%rsp),%xmm13
-	movaps	0x80(%rsp),%xmm14
-	movaps	0x90(%rsp),%xmm15
-	lea	0xa8(%rsp),%rsp
+	movaps	-0xa8(%r11),%xmm6
+	movaps	-0x98(%r11),%xmm7
+	movaps	-0x88(%r11),%xmm8
+	movaps	-0x78(%r11),%xmm9
+	movaps	-0x68(%r11),%xmm10
+	movaps	-0x58(%r11),%xmm11
+	movaps	-0x48(%r11),%xmm12
+	movaps	-0x38(%r11),%xmm13
+	movaps	-0x28(%r11),%xmm14
+	movaps	-0x18(%r11),%xmm15
 .LSEH_end_rsaz_1024_gather5:
 ___
 $code.=<<___;
+	lea	(%r11),%rsp
 	ret
 .size	rsaz_1024_gather5_avx2,.-rsaz_1024_gather5_avx2
 ___
 }
 
 $code.=<<___;
 .extern	OPENSSL_ia32cap_P
 .globl	rsaz_avx2_eligible
 .type	rsaz_avx2_eligible,\@abi-omnipotent
 .align	32
 rsaz_avx2_eligible:
 	mov	OPENSSL_ia32cap_P+8(%rip),%eax
 ___
 $code.=<<___	if ($addx);
 	mov	\$`1<<8|1<<19`,%ecx
 	mov	\$0,%edx
 	and	%eax,%ecx
 	cmp	\$`1<<8|1<<19`,%ecx	# check for BMI2+AD*X
 	cmove	%edx,%eax
 ___
 $code.=<<___;
 	and	\$`1<<5`,%eax
 	shr	\$5,%eax
 	ret
 .size	rsaz_avx2_eligible,.-rsaz_avx2_eligible
 
 .align	64
 .Land_mask:
 	.quad	0x1fffffff,0x1fffffff,0x1fffffff,-1
 .Lscatter_permd:
 	.long	0,2,4,6,7,7,7,7
 .Lgather_permd:
 	.long	0,7,1,7,2,7,3,7
-.Lgather_table:
-	.byte	0,0,0,0,0,0,0,0, 0xff,0,0,0,0,0,0,0
+.Linc:
+	.long	0,0,0,0, 1,1,1,1
+	.long	2,2,2,2, 3,3,3,3
+	.long	4,4,4,4, 4,4,4,4
 .align	64
 ___
 
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___
 .extern	__imp_RtlVirtualUnwind
 .type	rsaz_se_handler,\@abi-omnipotent
 .align	16
 rsaz_se_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lcommon_seh_tail
 
 	mov	160($context),%rax	# pull context->Rbp
 
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	mov	%r15,240($context)
 	mov	%r14,232($context)
 	mov	%r13,224($context)
 	mov	%r12,216($context)
 	mov	%rbp,160($context)
 	mov	%rbx,144($context)
 
 	lea	-0xd8(%rax),%rsi	# %xmm save area
 	lea	512($context),%rdi	# & context.Xmm6
 	mov	\$20,%ecx		# 10*sizeof(%xmm0)/sizeof(%rax)
 	.long	0xa548f3fc		# cld; rep movsq
 
 .Lcommon_seh_tail:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$154,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
 .size	rsaz_se_handler,.-rsaz_se_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_rsaz_1024_sqr_avx2
 	.rva	.LSEH_end_rsaz_1024_sqr_avx2
 	.rva	.LSEH_info_rsaz_1024_sqr_avx2
 
 	.rva	.LSEH_begin_rsaz_1024_mul_avx2
 	.rva	.LSEH_end_rsaz_1024_mul_avx2
 	.rva	.LSEH_info_rsaz_1024_mul_avx2
 
 	.rva	.LSEH_begin_rsaz_1024_gather5
 	.rva	.LSEH_end_rsaz_1024_gather5
 	.rva	.LSEH_info_rsaz_1024_gather5
 .section	.xdata
 .align	8
 .LSEH_info_rsaz_1024_sqr_avx2:
 	.byte	9,0,0,0
 	.rva	rsaz_se_handler
 	.rva	.Lsqr_1024_body,.Lsqr_1024_epilogue
 .LSEH_info_rsaz_1024_mul_avx2:
 	.byte	9,0,0,0
 	.rva	rsaz_se_handler
 	.rva	.Lmul_1024_body,.Lmul_1024_epilogue
 .LSEH_info_rsaz_1024_gather5:
-	.byte	0x01,0x33,0x16,0x00
-	.byte	0x36,0xf8,0x09,0x00	#vmovaps 0x90(rsp),xmm15
-	.byte	0x31,0xe8,0x08,0x00	#vmovaps 0x80(rsp),xmm14
-	.byte	0x2c,0xd8,0x07,0x00	#vmovaps 0x70(rsp),xmm13
-	.byte	0x27,0xc8,0x06,0x00	#vmovaps 0x60(rsp),xmm12
-	.byte	0x22,0xb8,0x05,0x00	#vmovaps 0x50(rsp),xmm11
-	.byte	0x1d,0xa8,0x04,0x00	#vmovaps 0x40(rsp),xmm10
-	.byte	0x18,0x98,0x03,0x00	#vmovaps 0x30(rsp),xmm9
-	.byte	0x13,0x88,0x02,0x00	#vmovaps 0x20(rsp),xmm8
-	.byte	0x0e,0x78,0x01,0x00	#vmovaps 0x10(rsp),xmm7
-	.byte	0x09,0x68,0x00,0x00	#vmovaps 0x00(rsp),xmm6
-	.byte	0x04,0x01,0x15,0x00	#sub	rsp,0xa8
+	.byte	0x01,0x36,0x17,0x0b
+	.byte	0x36,0xf8,0x09,0x00	# vmovaps 0x90(rsp),xmm15
+	.byte	0x31,0xe8,0x08,0x00	# vmovaps 0x80(rsp),xmm14
+	.byte	0x2c,0xd8,0x07,0x00	# vmovaps 0x70(rsp),xmm13
+	.byte	0x27,0xc8,0x06,0x00	# vmovaps 0x60(rsp),xmm12
+	.byte	0x22,0xb8,0x05,0x00	# vmovaps 0x50(rsp),xmm11
+	.byte	0x1d,0xa8,0x04,0x00	# vmovaps 0x40(rsp),xmm10
+	.byte	0x18,0x98,0x03,0x00	# vmovaps 0x30(rsp),xmm9
+	.byte	0x13,0x88,0x02,0x00	# vmovaps 0x20(rsp),xmm8
+	.byte	0x0e,0x78,0x01,0x00	# vmovaps 0x10(rsp),xmm7
+	.byte	0x09,0x68,0x00,0x00	# vmovaps 0x00(rsp),xmm6
+	.byte	0x04,0x01,0x15,0x00	# sub	  rsp,0xa8
+	.byte	0x00,0xb3,0x00,0x00	# set_frame r11
 ___
 }
 
 foreach (split("\n",$code)) {
 	s/\`([^\`]*)\`/eval($1)/ge;
 
 	s/\b(sh[rl]d?\s+\$)(-?[0-9]+)/$1.$2%64/ge		or
 
 	s/\b(vmov[dq])\b(.+)%ymm([0-9]+)/$1$2%xmm$3/go		or
 	s/\b(vmovdqu)\b(.+)%x%ymm([0-9]+)/$1$2%xmm$3/go		or
 	s/\b(vpinsr[qd])\b(.+)%ymm([0-9]+)/$1$2%xmm$3/go	or
 	s/\b(vpextr[qd])\b(.+)%ymm([0-9]+)/$1$2%xmm$3/go	or
 	s/\b(vpbroadcast[qd]\s+)%ymm([0-9]+)/$1%xmm$2/go;
 	print $_,"\n";
 }
 
 }}} else {{{
 print <<___;	# assembler is too old
 .text
 
 .globl	rsaz_avx2_eligible
 .type	rsaz_avx2_eligible,\@abi-omnipotent
 rsaz_avx2_eligible:
 	xor	%eax,%eax
 	ret
 .size	rsaz_avx2_eligible,.-rsaz_avx2_eligible
 
 .globl	rsaz_1024_sqr_avx2
 .globl	rsaz_1024_mul_avx2
 .globl	rsaz_1024_norm2red_avx2
 .globl	rsaz_1024_red2norm_avx2
 .globl	rsaz_1024_scatter5_avx2
 .globl	rsaz_1024_gather5_avx2
 .type	rsaz_1024_sqr_avx2,\@abi-omnipotent
 rsaz_1024_sqr_avx2:
 rsaz_1024_mul_avx2:
 rsaz_1024_norm2red_avx2:
 rsaz_1024_red2norm_avx2:
 rsaz_1024_scatter5_avx2:
 rsaz_1024_gather5_avx2:
 	.byte	0x0f,0x0b	# ud2
 	ret
 .size	rsaz_1024_sqr_avx2,.-rsaz_1024_sqr_avx2
 ___
 }}}
 
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-x86_64.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-x86_64.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/asm/rsaz-x86_64.pl	(revision 296273)
@@ -1,2144 +1,2351 @@
 #!/usr/bin/env perl
 
 ##############################################################################
 #                                                                            #
 #  Copyright (c) 2012, Intel Corporation                                     #
 #                                                                            #
 #  All rights reserved.                                                      #
 #                                                                            #
 #  Redistribution and use in source and binary forms, with or without        #
 #  modification, are permitted provided that the following conditions are    #
 #  met:                                                                      #
 #                                                                            #
 #  *  Redistributions of source code must retain the above copyright         #
 #     notice, this list of conditions and the following disclaimer.          #
 #                                                                            #
 #  *  Redistributions in binary form must reproduce the above copyright      #
 #     notice, this list of conditions and the following disclaimer in the    #
 #     documentation and/or other materials provided with the                 #
 #     distribution.                                                          #
 #                                                                            #
 #  *  Neither the name of the Intel Corporation nor the names of its         #
 #     contributors may be used to endorse or promote products derived from   #
 #     this software without specific prior written permission.               #
 #                                                                            #
 #                                                                            #
 #  THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION ""AS IS"" AND ANY          #
 #  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE         #
 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        #
 #  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION OR            #
 #  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
 #  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       #
 #  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        #
 #  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    #
 #  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      #
 #  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        #
 #  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              #
 #                                                                            #
 ##############################################################################
 # Developers and authors:                                                    #
 # Shay Gueron (1, 2), and Vlad Krasnov (1)                                   #
 # (1) Intel Architecture Group, Microprocessor and Chipset Development,      #
 #     Israel Development Center, Haifa, Israel                               #
 # (2) University of Haifa                                                    #
 ##############################################################################
 # Reference:                                                                 #
 # [1] S. Gueron, "Efficient Software Implementations of Modular              #
 #     Exponentiation", http://eprint.iacr.org/2011/239                       #
 # [2] S. Gueron, V. Krasnov. "Speeding up Big-Numbers Squaring".             #
 #     IEEE Proceedings of 9th International Conference on Information        #
 #     Technology: New Generations (ITNG 2012), 821-823 (2012).               #
 # [3] S. Gueron, Efficient Software Implementations of Modular Exponentiation#
 #     Journal of Cryptographic Engineering 2:31-43 (2012).                   #
 # [4] S. Gueron, V. Krasnov: "[PATCH] Efficient and side channel analysis    #
 #     resistant 512-bit and 1024-bit modular exponentiation for optimizing   #
 #     RSA1024 and RSA2048 on x86_64 platforms",                              #
 #     http://rt.openssl.org/Ticket/Display.html?id=2582&user=guest&pass=guest#
 ##############################################################################
 
 # While original submission covers 512- and 1024-bit exponentiation,
 # this module is limited to 512-bit version only (and as such
 # accelerates RSA1024 sign). This is because improvement for longer
 # keys is not high enough to justify the effort, highest measured
 # was ~5% on Westmere. [This is relative to OpenSSL 1.0.2, upcoming
 # for the moment of this writing!] Nor does this module implement
 # "monolithic" complete exponentiation jumbo-subroutine, but adheres
 # to more modular mixture of C and assembly. And it's optimized even
 # for processors other than Intel Core family (see table below for
 # improvement coefficients).
 # 						
 #
 # RSA1024 sign/sec	this/original	|this/rsax(*)	this/fips(*)
 #			----------------+---------------------------
 # Opteron		+13%		|+5%		+20%
 # Bulldozer		-0%		|-1%		+10%
 # P4			+11%		|+7%		+8%
 # Westmere		+5%		|+14%		+17%
 # Sandy Bridge		+2%		|+12%		+29%
 # Ivy Bridge		+1%		|+11%		+35%
 # Haswell(**)		-0%		|+12%		+39%
 # Atom			+13%		|+11%		+4%
 # VIA Nano		+70%		|+9%		+25%
 #
 # (*)	rsax engine and fips numbers are presented for reference
 #	purposes;
 # (**)	MULX was attempted, but found to give only marginal improvement;
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.23);
 }
 
 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.10);
 }
 
 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$addx = ($1>=12);
 }
 
 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
 	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
 	$addx = ($ver>=3.03);
 }
 
 ($out, $inp, $mod) = ("%rdi", "%rsi", "%rbp");	# common internal API
 {
 my ($out,$inp,$mod,$n0,$times) = ("%rdi","%rsi","%rdx","%rcx","%r8d");
 
 $code.=<<___;
 .text
 
 .extern	OPENSSL_ia32cap_P
 
 .globl	rsaz_512_sqr
 .type	rsaz_512_sqr,\@function,5
 .align	32
 rsaz_512_sqr:				# 25-29% faster than rsaz_512_mul
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	subq	\$128+24, %rsp
 .Lsqr_body:
 	movq	$mod, %rbp		# common argument
 	movq	($inp), %rdx
 	movq	8($inp), %rax
 	movq	$n0, 128(%rsp)
 ___
 $code.=<<___ if ($addx);
 	movl	\$0x80100,%r11d
 	andl	OPENSSL_ia32cap_P+8(%rip),%r11d
 	cmpl	\$0x80100,%r11d		# check for MULX and ADO/CX
 	je	.Loop_sqrx
 ___
 $code.=<<___;
 	jmp	.Loop_sqr
 
 .align	32
 .Loop_sqr:
 	movl	$times,128+8(%rsp)
 #first iteration
 	movq	%rdx, %rbx
 	mulq	%rdx
 	movq	%rax, %r8
 	movq	16($inp), %rax
 	movq	%rdx, %r9
 
 	mulq	%rbx
 	addq	%rax, %r9
 	movq	24($inp), %rax
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
 	addq	%rax, %r10
 	movq	32($inp), %rax
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	mulq	%rbx
 	addq	%rax, %r11
 	movq	40($inp), %rax
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r12
 	movq	48($inp), %rax
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
 	addq	%rax, %r13
 	movq	56($inp), %rax
 	movq	%rdx, %r14
 	adcq	\$0, %r14
 
 	mulq	%rbx
 	addq	%rax, %r14
 	movq	%rbx, %rax
 	movq	%rdx, %r15
 	adcq	\$0, %r15
 
 	addq	%r8, %r8		#shlq	\$1, %r8
 	movq	%r9, %rcx
 	adcq	%r9, %r9		#shld	\$1, %r8, %r9
 
 	mulq	%rax
 	movq	%rax, (%rsp)
 	addq	%rdx, %r8
 	adcq	\$0, %r9
 
 	movq	%r8, 8(%rsp)
 	shrq	\$63, %rcx
 
 #second iteration
 	movq	8($inp), %r8
 	movq	16($inp), %rax
 	mulq	%r8
 	addq	%rax, %r10
 	movq	24($inp), %rax
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r8
 	addq	%rax, %r11
 	movq	32($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r11
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r8
 	addq	%rax, %r12
 	movq	40($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r12
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r8
 	addq	%rax, %r13
 	movq	48($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r13
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r8
 	addq	%rax, %r14
 	movq	56($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r14
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r8
 	addq	%rax, %r15
 	movq	%r8, %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r15
 	movq	%rdx, %r8
 	movq	%r10, %rdx
 	adcq	\$0, %r8
 
 	add	%rdx, %rdx
 	lea	(%rcx,%r10,2), %r10	#shld	\$1, %rcx, %r10
 	movq	%r11, %rbx
 	adcq	%r11, %r11		#shld	\$1, %r10, %r11
 
 	mulq	%rax
 	addq	%rax, %r9
 	adcq	%rdx, %r10
 	adcq	\$0, %r11
 
 	movq	%r9, 16(%rsp)
 	movq	%r10, 24(%rsp)
 	shrq	\$63, %rbx
 	
 #third iteration
 	movq	16($inp), %r9	
 	movq	24($inp), %rax
 	mulq	%r9
 	addq	%rax, %r12
 	movq	32($inp), %rax
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r9
 	addq	%rax, %r13
 	movq	40($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rcx, %r13
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r9
 	addq	%rax, %r14
 	movq	48($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rcx, %r14
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r9
 	 movq	%r12, %r10
 	 lea	(%rbx,%r12,2), %r12	#shld	\$1, %rbx, %r12
 	addq	%rax, %r15
 	movq	56($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rcx, %r15
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r9
 	 shrq	\$63, %r10
 	addq	%rax, %r8
 	movq	%r9, %rax
 	adcq	\$0, %rdx
 	addq	%rcx, %r8
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	movq	%r13, %rcx
 	leaq	(%r10,%r13,2), %r13	#shld	\$1, %r12, %r13
 
 	mulq	%rax
 	addq	%rax, %r11
 	adcq	%rdx, %r12
 	adcq	\$0, %r13
 
 	movq	%r11, 32(%rsp)
 	movq	%r12, 40(%rsp)
 	shrq	\$63, %rcx
 
 #fourth iteration
 	movq	24($inp), %r10
 	movq	32($inp), %rax
 	mulq	%r10
 	addq	%rax, %r14
 	movq	40($inp), %rax
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r10
 	addq	%rax, %r15
 	movq	48($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r15
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r10
 	 movq	%r14, %r12
 	 leaq	(%rcx,%r14,2), %r14	#shld	\$1, %rcx, %r14
 	addq	%rax, %r8
 	movq	56($inp), %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r8
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r10
 	 shrq	\$63, %r12
 	addq	%rax, %r9
 	movq	%r10, %rax
 	adcq	\$0, %rdx
 	addq	%rbx, %r9
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	movq	%r15, %rbx
 	leaq	(%r12,%r15,2),%r15	#shld	\$1, %r14, %r15
 
 	mulq	%rax
 	addq	%rax, %r13
 	adcq	%rdx, %r14
 	adcq	\$0, %r15
 
 	movq	%r13, 48(%rsp)
 	movq	%r14, 56(%rsp)
 	shrq	\$63, %rbx
 
 #fifth iteration
 	movq	32($inp), %r11
 	movq	40($inp), %rax
 	mulq	%r11
 	addq	%rax, %r8
 	movq	48($inp), %rax
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r11
 	addq	%rax, %r9
 	movq	56($inp), %rax
 	adcq	\$0, %rdx
 	 movq	%r8, %r12
 	 leaq	(%rbx,%r8,2), %r8	#shld	\$1, %rbx, %r8
 	addq	%rcx, %r9
 	movq	%rdx, %rcx
 	adcq	\$0, %rcx
 
 	mulq	%r11
 	 shrq	\$63, %r12
 	addq	%rax, %r10
 	movq	%r11, %rax
 	adcq	\$0, %rdx
 	addq	%rcx, %r10
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	movq	%r9, %rcx
 	leaq	(%r12,%r9,2), %r9	#shld	\$1, %r8, %r9
 
 	mulq	%rax
 	addq	%rax, %r15
 	adcq	%rdx, %r8
 	adcq	\$0, %r9
 
 	movq	%r15, 64(%rsp)
 	movq	%r8, 72(%rsp)
 	shrq	\$63, %rcx
 
 #sixth iteration
 	movq	40($inp), %r12
 	movq	48($inp), %rax
 	mulq	%r12
 	addq	%rax, %r10
 	movq	56($inp), %rax
 	movq	%rdx, %rbx
 	adcq	\$0, %rbx
 
 	mulq	%r12
 	addq	%rax, %r11
 	movq	%r12, %rax
 	 movq	%r10, %r15
 	 leaq	(%rcx,%r10,2), %r10	#shld	\$1, %rcx, %r10
 	adcq	\$0, %rdx
 	 shrq	\$63, %r15
 	addq	%rbx, %r11
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	movq	%r11, %rbx
 	leaq	(%r15,%r11,2), %r11	#shld	\$1, %r10, %r11
 
 	mulq	%rax
 	addq	%rax, %r9
 	adcq	%rdx, %r10
 	adcq	\$0, %r11
 
 	movq	%r9, 80(%rsp)
 	movq	%r10, 88(%rsp)
 
 #seventh iteration
 	movq	48($inp), %r13
 	movq	56($inp), %rax
 	mulq	%r13
 	addq	%rax, %r12
 	movq	%r13, %rax
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	xorq	%r14, %r14
 	shlq	\$1, %rbx
 	adcq	%r12, %r12		#shld	\$1, %rbx, %r12
 	adcq	%r13, %r13		#shld	\$1, %r12, %r13
 	adcq	%r14, %r14		#shld	\$1, %r13, %r14
 
 	mulq	%rax
 	addq	%rax, %r11
 	adcq	%rdx, %r12
 	adcq	\$0, %r13
 
 	movq	%r11, 96(%rsp)
 	movq	%r12, 104(%rsp)
 
 #eighth iteration
 	movq	56($inp), %rax
 	mulq	%rax
 	addq	%rax, %r13
 	adcq	\$0, %rdx
 
 	addq	%rdx, %r14
 
 	movq	%r13, 112(%rsp)
 	movq	%r14, 120(%rsp)
 
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reduce
 
 	addq	64(%rsp), %r8
 	adcq	72(%rsp), %r9
 	adcq	80(%rsp), %r10
 	adcq	88(%rsp), %r11
 	adcq	96(%rsp), %r12
 	adcq	104(%rsp), %r13
 	adcq	112(%rsp), %r14
 	adcq	120(%rsp), %r15
 	sbbq	%rcx, %rcx
 
 	call	__rsaz_512_subtract
 
 	movq	%r8, %rdx
 	movq	%r9, %rax
 	movl	128+8(%rsp), $times
 	movq	$out, $inp
 
 	decl	$times
 	jnz	.Loop_sqr
 ___
 if ($addx) {
 $code.=<<___;
 	jmp	.Lsqr_tail
 
 .align	32
 .Loop_sqrx:
 	movl	$times,128+8(%rsp)
 	movq	$out, %xmm0		# off-load
 	movq	%rbp, %xmm1		# off-load
 #first iteration	
 	mulx	%rax, %r8, %r9
 
 	mulx	16($inp), %rcx, %r10
 	xor	%rbp, %rbp		# cf=0, of=0
 
 	mulx	24($inp), %rax, %r11
 	adcx	%rcx, %r9
 
 	mulx	32($inp), %rcx, %r12
 	adcx	%rax, %r10
 
 	mulx	40($inp), %rax, %r13
 	adcx	%rcx, %r11
 
 	.byte	0xc4,0x62,0xf3,0xf6,0xb6,0x30,0x00,0x00,0x00	# mulx	48($inp), %rcx, %r14
 	adcx	%rax, %r12
 	adcx	%rcx, %r13
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xbe,0x38,0x00,0x00,0x00	# mulx	56($inp), %rax, %r15
 	adcx	%rax, %r14
 	adcx	%rbp, %r15		# %rbp is 0
 
 	mov	%r9, %rcx
 	shld	\$1, %r8, %r9
 	shl	\$1, %r8
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rdx, %r8
 	 mov	8($inp), %rdx
 	adcx	%rbp, %r9
 
 	mov	%rax, (%rsp)
 	mov	%r8, 8(%rsp)
 
 #second iteration	
 	mulx	16($inp), %rax, %rbx
 	adox	%rax, %r10
 	adcx	%rbx, %r11
 
 	.byte	0xc4,0x62,0xc3,0xf6,0x86,0x18,0x00,0x00,0x00	# mulx	24($inp), $out, %r8
 	adox	$out, %r11
 	adcx	%r8, %r12
 
 	mulx	32($inp), %rax, %rbx
 	adox	%rax, %r12
 	adcx	%rbx, %r13
 
 	mulx	40($inp), $out, %r8
 	adox	$out, %r13
 	adcx	%r8, %r14
 
 	.byte	0xc4,0xe2,0xfb,0xf6,0x9e,0x30,0x00,0x00,0x00	# mulx	48($inp), %rax, %rbx
 	adox	%rax, %r14
 	adcx	%rbx, %r15
 
 	.byte	0xc4,0x62,0xc3,0xf6,0x86,0x38,0x00,0x00,0x00	# mulx	56($inp), $out, %r8
 	adox	$out, %r15
 	adcx	%rbp, %r8
 	adox	%rbp, %r8
 
 	mov	%r11, %rbx
 	shld	\$1, %r10, %r11
 	shld	\$1, %rcx, %r10
 
 	xor	%ebp,%ebp
 	mulx	%rdx, %rax, %rcx
 	 mov	16($inp), %rdx
 	adcx	%rax, %r9
 	adcx	%rcx, %r10
 	adcx	%rbp, %r11
 
 	mov	%r9, 16(%rsp)
 	.byte	0x4c,0x89,0x94,0x24,0x18,0x00,0x00,0x00		# mov	%r10, 24(%rsp)
 	
 #third iteration	
 	.byte	0xc4,0x62,0xc3,0xf6,0x8e,0x18,0x00,0x00,0x00	# mulx	24($inp), $out, %r9
 	adox	$out, %r12
 	adcx	%r9, %r13
 
 	mulx	32($inp), %rax, %rcx
 	adox	%rax, %r13
 	adcx	%rcx, %r14
 
 	mulx	40($inp), $out, %r9
 	adox	$out, %r14
 	adcx	%r9, %r15
 
 	.byte	0xc4,0xe2,0xfb,0xf6,0x8e,0x30,0x00,0x00,0x00	# mulx	48($inp), %rax, %rcx
 	adox	%rax, %r15
 	adcx	%rcx, %r8
 
 	.byte	0xc4,0x62,0xc3,0xf6,0x8e,0x38,0x00,0x00,0x00	# mulx	56($inp), $out, %r9
 	adox	$out, %r8
 	adcx	%rbp, %r9
 	adox	%rbp, %r9
 
 	mov	%r13, %rcx
 	shld	\$1, %r12, %r13
 	shld	\$1, %rbx, %r12
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rax, %r11
 	adcx	%rdx, %r12
 	 mov	24($inp), %rdx
 	adcx	%rbp, %r13
 
 	mov	%r11, 32(%rsp)
 	.byte	0x4c,0x89,0xa4,0x24,0x28,0x00,0x00,0x00		# mov	%r12, 40(%rsp)
 	
 #fourth iteration	
 	.byte	0xc4,0xe2,0xfb,0xf6,0x9e,0x20,0x00,0x00,0x00	# mulx	32($inp), %rax, %rbx
 	adox	%rax, %r14
 	adcx	%rbx, %r15
 
 	mulx	40($inp), $out, %r10
 	adox	$out, %r15
 	adcx	%r10, %r8
 
 	mulx	48($inp), %rax, %rbx
 	adox	%rax, %r8
 	adcx	%rbx, %r9
 
 	mulx	56($inp), $out, %r10
 	adox	$out, %r9
 	adcx	%rbp, %r10
 	adox	%rbp, %r10
 
 	.byte	0x66
 	mov	%r15, %rbx
 	shld	\$1, %r14, %r15
 	shld	\$1, %rcx, %r14
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rax, %r13
 	adcx	%rdx, %r14
 	 mov	32($inp), %rdx
 	adcx	%rbp, %r15
 
 	mov	%r13, 48(%rsp)
 	mov	%r14, 56(%rsp)
 	
 #fifth iteration	
 	.byte	0xc4,0x62,0xc3,0xf6,0x9e,0x28,0x00,0x00,0x00	# mulx	40($inp), $out, %r11
 	adox	$out, %r8
 	adcx	%r11, %r9
 
 	mulx	48($inp), %rax, %rcx
 	adox	%rax, %r9
 	adcx	%rcx, %r10
 
 	mulx	56($inp), $out, %r11
 	adox	$out, %r10
 	adcx	%rbp, %r11
 	adox	%rbp, %r11
 
 	mov	%r9, %rcx
 	shld	\$1, %r8, %r9
 	shld	\$1, %rbx, %r8
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rax, %r15
 	adcx	%rdx, %r8
 	 mov	40($inp), %rdx
 	adcx	%rbp, %r9
 
 	mov	%r15, 64(%rsp)
 	mov	%r8, 72(%rsp)
 	
 #sixth iteration	
 	.byte	0xc4,0xe2,0xfb,0xf6,0x9e,0x30,0x00,0x00,0x00	# mulx	48($inp), %rax, %rbx
 	adox	%rax, %r10
 	adcx	%rbx, %r11
 
 	.byte	0xc4,0x62,0xc3,0xf6,0xa6,0x38,0x00,0x00,0x00	# mulx	56($inp), $out, %r12
 	adox	$out, %r11
 	adcx	%rbp, %r12
 	adox	%rbp, %r12
 
 	mov	%r11, %rbx
 	shld	\$1, %r10, %r11
 	shld	\$1, %rcx, %r10
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rax, %r9
 	adcx	%rdx, %r10
 	 mov	48($inp), %rdx
 	adcx	%rbp, %r11
 
 	mov	%r9, 80(%rsp)
 	mov	%r10, 88(%rsp)
 
 #seventh iteration
 	.byte	0xc4,0x62,0xfb,0xf6,0xae,0x38,0x00,0x00,0x00	# mulx	56($inp), %rax, %r13
 	adox	%rax, %r12
 	adox	%rbp, %r13
 
 	xor	%r14, %r14
 	shld	\$1, %r13, %r14
 	shld	\$1, %r12, %r13
 	shld	\$1, %rbx, %r12
 
 	xor	%ebp, %ebp
 	mulx	%rdx, %rax, %rdx
 	adcx	%rax, %r11
 	adcx	%rdx, %r12
 	 mov	56($inp), %rdx
 	adcx	%rbp, %r13
 
 	.byte	0x4c,0x89,0x9c,0x24,0x60,0x00,0x00,0x00		# mov	%r11, 96(%rsp)
 	.byte	0x4c,0x89,0xa4,0x24,0x68,0x00,0x00,0x00		# mov	%r12, 104(%rsp)
 
 #eighth iteration
 	mulx	%rdx, %rax, %rdx
 	adox	%rax, %r13
 	adox	%rbp, %rdx
 
 	.byte	0x66
 	add	%rdx, %r14
 
 	movq	%r13, 112(%rsp)
 	movq	%r14, 120(%rsp)
 	movq	%xmm0, $out
 	movq	%xmm1, %rbp
 
 	movq	128(%rsp), %rdx		# pull $n0
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reducex
 
 	addq	64(%rsp), %r8
 	adcq	72(%rsp), %r9
 	adcq	80(%rsp), %r10
 	adcq	88(%rsp), %r11
 	adcq	96(%rsp), %r12
 	adcq	104(%rsp), %r13
 	adcq	112(%rsp), %r14
 	adcq	120(%rsp), %r15
 	sbbq	%rcx, %rcx
 
 	call	__rsaz_512_subtract
 
 	movq	%r8, %rdx
 	movq	%r9, %rax
 	movl	128+8(%rsp), $times
 	movq	$out, $inp
 
 	decl	$times
 	jnz	.Loop_sqrx
 
 .Lsqr_tail:
 ___
 }
 $code.=<<___;
 
 	leaq	128+24+48(%rsp), %rax
 	movq	-48(%rax), %r15
 	movq	-40(%rax), %r14
 	movq	-32(%rax), %r13
 	movq	-24(%rax), %r12
 	movq	-16(%rax), %rbp
 	movq	-8(%rax), %rbx
 	leaq	(%rax), %rsp
 .Lsqr_epilogue:
 	ret
 .size	rsaz_512_sqr,.-rsaz_512_sqr
 ___
 }
 {
 my ($out,$ap,$bp,$mod,$n0) = ("%rdi","%rsi","%rdx","%rcx","%r8");
 $code.=<<___;
 .globl	rsaz_512_mul
 .type	rsaz_512_mul,\@function,5
 .align	32
 rsaz_512_mul:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	subq	\$128+24, %rsp
 .Lmul_body:
 	movq	$out, %xmm0		# off-load arguments
 	movq	$mod, %xmm1
 	movq	$n0, 128(%rsp)
 ___
 $code.=<<___ if ($addx);
 	movl	\$0x80100,%r11d
 	andl	OPENSSL_ia32cap_P+8(%rip),%r11d
 	cmpl	\$0x80100,%r11d		# check for MULX and ADO/CX
 	je	.Lmulx
 ___
 $code.=<<___;
 	movq	($bp), %rbx		# pass b[0]
 	movq	$bp, %rbp		# pass argument
 	call	__rsaz_512_mul
 
 	movq	%xmm0, $out
 	movq	%xmm1, %rbp
 
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reduce
 ___
 $code.=<<___ if ($addx);
 	jmp	.Lmul_tail
 
 .align	32
 .Lmulx:
 	movq	$bp, %rbp		# pass argument
 	movq	($bp), %rdx		# pass b[0]
 	call	__rsaz_512_mulx
 
 	movq	%xmm0, $out
 	movq	%xmm1, %rbp
 
 	movq	128(%rsp), %rdx		# pull $n0
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reducex
 .Lmul_tail:
 ___
 $code.=<<___;
 	addq	64(%rsp), %r8
 	adcq	72(%rsp), %r9
 	adcq	80(%rsp), %r10
 	adcq	88(%rsp), %r11
 	adcq	96(%rsp), %r12
 	adcq	104(%rsp), %r13
 	adcq	112(%rsp), %r14
 	adcq	120(%rsp), %r15
 	sbbq	%rcx, %rcx
 
 	call	__rsaz_512_subtract
 
 	leaq	128+24+48(%rsp), %rax
 	movq	-48(%rax), %r15
 	movq	-40(%rax), %r14
 	movq	-32(%rax), %r13
 	movq	-24(%rax), %r12
 	movq	-16(%rax), %rbp
 	movq	-8(%rax), %rbx
 	leaq	(%rax), %rsp
 .Lmul_epilogue:
 	ret
 .size	rsaz_512_mul,.-rsaz_512_mul
 ___
 }
 {
 my ($out,$ap,$bp,$mod,$n0,$pwr) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
 $code.=<<___;
 .globl	rsaz_512_mul_gather4
 .type	rsaz_512_mul_gather4,\@function,6
 .align	32
 rsaz_512_mul_gather4:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
-	mov	$pwr, $pwr
-	subq	\$128+24, %rsp
+	subq	\$`128+24+($win64?0xb0:0)`, %rsp
+___
+$code.=<<___	if ($win64);
+	movaps	%xmm6,0xa0(%rsp)
+	movaps	%xmm7,0xb0(%rsp)
+	movaps	%xmm8,0xc0(%rsp)
+	movaps	%xmm9,0xd0(%rsp)
+	movaps	%xmm10,0xe0(%rsp)
+	movaps	%xmm11,0xf0(%rsp)
+	movaps	%xmm12,0x100(%rsp)
+	movaps	%xmm13,0x110(%rsp)
+	movaps	%xmm14,0x120(%rsp)
+	movaps	%xmm15,0x130(%rsp)
+___
+$code.=<<___;
 .Lmul_gather4_body:
+	movd	$pwr,%xmm8
+	movdqa	.Linc+16(%rip),%xmm1	# 00000002000000020000000200000002
+	movdqa	.Linc(%rip),%xmm0	# 00000001000000010000000000000000
+
+	pshufd	\$0,%xmm8,%xmm8		# broadcast $power
+	movdqa	%xmm1,%xmm7
+	movdqa	%xmm1,%xmm2
 ___
+########################################################################
+# calculate mask by comparing 0..15 to $power
+#
+for($i=0;$i<4;$i++) {
+$code.=<<___;
+	paddd	%xmm`$i`,%xmm`$i+1`
+	pcmpeqd	%xmm8,%xmm`$i`
+	movdqa	%xmm7,%xmm`$i+3`
+___
+}
+for(;$i<7;$i++) {
+$code.=<<___;
+	paddd	%xmm`$i`,%xmm`$i+1`
+	pcmpeqd	%xmm8,%xmm`$i`
+___
+}
+$code.=<<___;
+	pcmpeqd	%xmm8,%xmm7
+
+	movdqa	16*0($bp),%xmm8
+	movdqa	16*1($bp),%xmm9
+	movdqa	16*2($bp),%xmm10
+	movdqa	16*3($bp),%xmm11
+	pand	%xmm0,%xmm8
+	movdqa	16*4($bp),%xmm12
+	pand	%xmm1,%xmm9
+	movdqa	16*5($bp),%xmm13
+	pand	%xmm2,%xmm10
+	movdqa	16*6($bp),%xmm14
+	pand	%xmm3,%xmm11
+	movdqa	16*7($bp),%xmm15
+	leaq	128($bp), %rbp
+	pand	%xmm4,%xmm12
+	pand	%xmm5,%xmm13
+	pand	%xmm6,%xmm14
+	pand	%xmm7,%xmm15
+	por	%xmm10,%xmm8
+	por	%xmm11,%xmm9
+	por	%xmm12,%xmm8
+	por	%xmm13,%xmm9
+	por	%xmm14,%xmm8
+	por	%xmm15,%xmm9
+
+	por	%xmm9,%xmm8
+	pshufd	\$0x4e,%xmm8,%xmm9
+	por	%xmm9,%xmm8
+___
 $code.=<<___ if ($addx);
 	movl	\$0x80100,%r11d
 	andl	OPENSSL_ia32cap_P+8(%rip),%r11d
 	cmpl	\$0x80100,%r11d		# check for MULX and ADO/CX
 	je	.Lmulx_gather
 ___
 $code.=<<___;
-	movl	64($bp,$pwr,4), %eax
-	movq	$out, %xmm0		# off-load arguments
-	movl	($bp,$pwr,4), %ebx
-	movq	$mod, %xmm1
-	movq	$n0, 128(%rsp)
+	movq	%xmm8,%rbx
 
-	shlq	\$32, %rax
-	or	%rax, %rbx
+	movq	$n0, 128(%rsp)		# off-load arguments
+	movq	$out, 128+8(%rsp)
+	movq	$mod, 128+16(%rsp)
+
 	movq	($ap), %rax
 	 movq	8($ap), %rcx
-	 leaq	128($bp,$pwr,4), %rbp
 	mulq	%rbx			# 0 iteration
 	movq	%rax, (%rsp)
 	movq	%rcx, %rax
 	movq	%rdx, %r8
 
 	mulq	%rbx
-	 movd	(%rbp), %xmm4
 	addq	%rax, %r8
 	movq	16($ap), %rax
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	mulq	%rbx
-	 movd	64(%rbp), %xmm5
 	addq	%rax, %r9
 	movq	24($ap), %rax
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
-	 pslldq	\$4, %xmm5
 	addq	%rax, %r10
 	movq	32($ap), %rax
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	mulq	%rbx
-	 por	%xmm5, %xmm4
 	addq	%rax, %r11
 	movq	40($ap), %rax
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r12
 	movq	48($ap), %rax
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
-	 leaq	128(%rbp), %rbp
 	addq	%rax, %r13
 	movq	56($ap), %rax
 	movq	%rdx, %r14
 	adcq	\$0, %r14
 	
 	mulq	%rbx
-	 movq	%xmm4, %rbx
 	addq	%rax, %r14
 	 movq	($ap), %rax
 	movq	%rdx, %r15
 	adcq	\$0, %r15
 
 	leaq	8(%rsp), %rdi
 	movl	\$7, %ecx
 	jmp	.Loop_mul_gather
 
 .align	32
 .Loop_mul_gather:
+	movdqa	16*0(%rbp),%xmm8
+	movdqa	16*1(%rbp),%xmm9
+	movdqa	16*2(%rbp),%xmm10
+	movdqa	16*3(%rbp),%xmm11
+	pand	%xmm0,%xmm8
+	movdqa	16*4(%rbp),%xmm12
+	pand	%xmm1,%xmm9
+	movdqa	16*5(%rbp),%xmm13
+	pand	%xmm2,%xmm10
+	movdqa	16*6(%rbp),%xmm14
+	pand	%xmm3,%xmm11
+	movdqa	16*7(%rbp),%xmm15
+	leaq	128(%rbp), %rbp
+	pand	%xmm4,%xmm12
+	pand	%xmm5,%xmm13
+	pand	%xmm6,%xmm14
+	pand	%xmm7,%xmm15
+	por	%xmm10,%xmm8
+	por	%xmm11,%xmm9
+	por	%xmm12,%xmm8
+	por	%xmm13,%xmm9
+	por	%xmm14,%xmm8
+	por	%xmm15,%xmm9
+
+	por	%xmm9,%xmm8
+	pshufd	\$0x4e,%xmm8,%xmm9
+	por	%xmm9,%xmm8
+	movq	%xmm8,%rbx
+
 	mulq	%rbx
 	addq	%rax, %r8
 	movq	8($ap), %rax
 	movq	%r8, (%rdi)
 	movq	%rdx, %r8
 	adcq	\$0, %r8
 
 	mulq	%rbx
-	 movd	(%rbp), %xmm4
 	addq	%rax, %r9
 	movq	16($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r9, %r8
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	mulq	%rbx
-	 movd	64(%rbp), %xmm5
 	addq	%rax, %r10
 	movq	24($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r10, %r9
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
-	 pslldq	\$4, %xmm5
 	addq	%rax, %r11
 	movq	32($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r11, %r10
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	mulq	%rbx
-	 por	%xmm5, %xmm4
 	addq	%rax, %r12
 	movq	40($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r12, %r11
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r13
 	movq	48($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r13, %r12
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
 	addq	%rax, %r14
 	movq	56($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r14, %r13
 	movq	%rdx, %r14
 	adcq	\$0, %r14
 
 	mulq	%rbx
-	 movq	%xmm4, %rbx
 	addq	%rax, %r15
 	 movq	($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r15, %r14
 	movq	%rdx, %r15	
 	adcq	\$0, %r15
 
-	leaq	128(%rbp), %rbp
 	leaq	8(%rdi), %rdi
 
 	decl	%ecx
 	jnz	.Loop_mul_gather
 
 	movq	%r8, (%rdi)
 	movq	%r9, 8(%rdi)
 	movq	%r10, 16(%rdi)
 	movq	%r11, 24(%rdi)
 	movq	%r12, 32(%rdi)
 	movq	%r13, 40(%rdi)
 	movq	%r14, 48(%rdi)
 	movq	%r15, 56(%rdi)
 
-	movq	%xmm0, $out
-	movq	%xmm1, %rbp
+	movq	128+8(%rsp), $out
+	movq	128+16(%rsp), %rbp
 
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reduce
 ___
 $code.=<<___ if ($addx);
 	jmp	.Lmul_gather_tail
 
 .align	32
 .Lmulx_gather:
-	mov	64($bp,$pwr,4), %eax
-	movq	$out, %xmm0		# off-load arguments
-	lea	128($bp,$pwr,4), %rbp
-	mov	($bp,$pwr,4), %edx
-	movq	$mod, %xmm1
-	mov	$n0, 128(%rsp)
+	movq	%xmm8,%rdx
 
-	shl	\$32, %rax
-	or	%rax, %rdx
+	mov	$n0, 128(%rsp)		# off-load arguments
+	mov	$out, 128+8(%rsp)
+	mov	$mod, 128+16(%rsp)
+
 	mulx	($ap), %rbx, %r8	# 0 iteration
 	mov	%rbx, (%rsp)
 	xor	%edi, %edi		# cf=0, of=0
 
 	mulx	8($ap), %rax, %r9
-	 movd	(%rbp), %xmm4
 
 	mulx	16($ap), %rbx, %r10
-	 movd	64(%rbp), %xmm5
 	adcx	%rax, %r8
 
 	mulx	24($ap), %rax, %r11
-	 pslldq	\$4, %xmm5
 	adcx	%rbx, %r9
 
 	mulx	32($ap), %rbx, %r12
-	 por	%xmm5, %xmm4
 	adcx	%rax, %r10
 
 	mulx	40($ap), %rax, %r13
 	adcx	%rbx, %r11
 
 	mulx	48($ap), %rbx, %r14
-	 lea	128(%rbp), %rbp
 	adcx	%rax, %r12
 	
 	mulx	56($ap), %rax, %r15
-	 movq	%xmm4, %rdx
 	adcx	%rbx, %r13
 	adcx	%rax, %r14
+	.byte	0x67
 	mov	%r8, %rbx
 	adcx	%rdi, %r15		# %rdi is 0
 
 	mov	\$-7, %rcx
 	jmp	.Loop_mulx_gather
 
 .align	32
 .Loop_mulx_gather:
-	mulx	($ap), %rax, %r8
+	movdqa	16*0(%rbp),%xmm8
+	movdqa	16*1(%rbp),%xmm9
+	movdqa	16*2(%rbp),%xmm10
+	movdqa	16*3(%rbp),%xmm11
+	pand	%xmm0,%xmm8
+	movdqa	16*4(%rbp),%xmm12
+	pand	%xmm1,%xmm9
+	movdqa	16*5(%rbp),%xmm13
+	pand	%xmm2,%xmm10
+	movdqa	16*6(%rbp),%xmm14
+	pand	%xmm3,%xmm11
+	movdqa	16*7(%rbp),%xmm15
+	leaq	128(%rbp), %rbp
+	pand	%xmm4,%xmm12
+	pand	%xmm5,%xmm13
+	pand	%xmm6,%xmm14
+	pand	%xmm7,%xmm15
+	por	%xmm10,%xmm8
+	por	%xmm11,%xmm9
+	por	%xmm12,%xmm8
+	por	%xmm13,%xmm9
+	por	%xmm14,%xmm8
+	por	%xmm15,%xmm9
+
+	por	%xmm9,%xmm8
+	pshufd	\$0x4e,%xmm8,%xmm9
+	por	%xmm9,%xmm8
+	movq	%xmm8,%rdx
+
+	.byte	0xc4,0x62,0xfb,0xf6,0x86,0x00,0x00,0x00,0x00	# mulx	($ap), %rax, %r8
 	adcx	%rax, %rbx
 	adox	%r9, %r8
 
 	mulx	8($ap), %rax, %r9
-	.byte	0x66,0x0f,0x6e,0xa5,0x00,0x00,0x00,0x00		# movd	(%rbp), %xmm4
 	adcx	%rax, %r8
 	adox	%r10, %r9
 
 	mulx	16($ap), %rax, %r10
-	 movd	64(%rbp), %xmm5
-	 lea	128(%rbp), %rbp
 	adcx	%rax, %r9
 	adox	%r11, %r10
 
 	.byte	0xc4,0x62,0xfb,0xf6,0x9e,0x18,0x00,0x00,0x00	# mulx	24($ap), %rax, %r11
-	 pslldq	\$4, %xmm5
-	 por	%xmm5, %xmm4
 	adcx	%rax, %r10
 	adox	%r12, %r11
 
 	mulx	32($ap), %rax, %r12
 	adcx	%rax, %r11
 	adox	%r13, %r12
 
 	mulx	40($ap), %rax, %r13
 	adcx	%rax, %r12
 	adox	%r14, %r13
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xb6,0x30,0x00,0x00,0x00	# mulx	48($ap), %rax, %r14
 	adcx	%rax, %r13
+	.byte	0x67
 	adox	%r15, %r14
 
 	mulx	56($ap), %rax, %r15
-	 movq	%xmm4, %rdx
 	 mov	%rbx, 64(%rsp,%rcx,8)
 	adcx	%rax, %r14
 	adox	%rdi, %r15
 	mov	%r8, %rbx
 	adcx	%rdi, %r15		# cf=0
 
 	inc	%rcx			# of=0
 	jnz	.Loop_mulx_gather
 
 	mov	%r8, 64(%rsp)
 	mov	%r9, 64+8(%rsp)
 	mov	%r10, 64+16(%rsp)
 	mov	%r11, 64+24(%rsp)
 	mov	%r12, 64+32(%rsp)
 	mov	%r13, 64+40(%rsp)
 	mov	%r14, 64+48(%rsp)
 	mov	%r15, 64+56(%rsp)
 
-	movq	%xmm0, $out
-	movq	%xmm1, %rbp
+	mov	128(%rsp), %rdx		# pull arguments
+	mov	128+8(%rsp), $out
+	mov	128+16(%rsp), %rbp
 
-	mov	128(%rsp), %rdx		# pull $n0
 	mov	(%rsp), %r8
 	mov	8(%rsp), %r9
 	mov	16(%rsp), %r10
 	mov	24(%rsp), %r11
 	mov	32(%rsp), %r12
 	mov	40(%rsp), %r13
 	mov	48(%rsp), %r14
 	mov	56(%rsp), %r15
 
 	call	__rsaz_512_reducex
 
 .Lmul_gather_tail:
 ___
 $code.=<<___;
 	addq	64(%rsp), %r8
 	adcq	72(%rsp), %r9
 	adcq	80(%rsp), %r10
 	adcq	88(%rsp), %r11
 	adcq	96(%rsp), %r12
 	adcq	104(%rsp), %r13
 	adcq	112(%rsp), %r14
 	adcq	120(%rsp), %r15
 	sbbq	%rcx, %rcx
 
 	call	__rsaz_512_subtract
 
 	leaq	128+24+48(%rsp), %rax
+___
+$code.=<<___	if ($win64);
+	movaps	0xa0-0xc8(%rax),%xmm6
+	movaps	0xb0-0xc8(%rax),%xmm7
+	movaps	0xc0-0xc8(%rax),%xmm8
+	movaps	0xd0-0xc8(%rax),%xmm9
+	movaps	0xe0-0xc8(%rax),%xmm10
+	movaps	0xf0-0xc8(%rax),%xmm11
+	movaps	0x100-0xc8(%rax),%xmm12
+	movaps	0x110-0xc8(%rax),%xmm13
+	movaps	0x120-0xc8(%rax),%xmm14
+	movaps	0x130-0xc8(%rax),%xmm15
+	lea	0xb0(%rax),%rax
+___
+$code.=<<___;
 	movq	-48(%rax), %r15
 	movq	-40(%rax), %r14
 	movq	-32(%rax), %r13
 	movq	-24(%rax), %r12
 	movq	-16(%rax), %rbp
 	movq	-8(%rax), %rbx
 	leaq	(%rax), %rsp
 .Lmul_gather4_epilogue:
 	ret
 .size	rsaz_512_mul_gather4,.-rsaz_512_mul_gather4
 ___
 }
 {
 my ($out,$ap,$mod,$n0,$tbl,$pwr) = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9d");
 $code.=<<___;
 .globl	rsaz_512_mul_scatter4
 .type	rsaz_512_mul_scatter4,\@function,6
 .align	32
 rsaz_512_mul_scatter4:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	mov	$pwr, $pwr
 	subq	\$128+24, %rsp
 .Lmul_scatter4_body:
-	leaq	($tbl,$pwr,4), $tbl
+	leaq	($tbl,$pwr,8), $tbl
 	movq	$out, %xmm0		# off-load arguments
 	movq	$mod, %xmm1
 	movq	$tbl, %xmm2
 	movq	$n0, 128(%rsp)
 
 	movq	$out, %rbp
 ___
 $code.=<<___ if ($addx);
 	movl	\$0x80100,%r11d
 	andl	OPENSSL_ia32cap_P+8(%rip),%r11d
 	cmpl	\$0x80100,%r11d		# check for MULX and ADO/CX
 	je	.Lmulx_scatter
 ___
 $code.=<<___;
 	movq	($out),%rbx		# pass b[0]
 	call	__rsaz_512_mul
 
 	movq	%xmm0, $out
 	movq	%xmm1, %rbp
 
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reduce
 ___
 $code.=<<___ if ($addx);
 	jmp	.Lmul_scatter_tail
 	
 .align	32
 .Lmulx_scatter:
 	movq	($out), %rdx		# pass b[0]
 	call	__rsaz_512_mulx
 
 	movq	%xmm0, $out
 	movq	%xmm1, %rbp
 
 	movq	128(%rsp), %rdx		# pull $n0
 	movq	(%rsp), %r8
 	movq	8(%rsp), %r9
 	movq	16(%rsp), %r10
 	movq	24(%rsp), %r11
 	movq	32(%rsp), %r12
 	movq	40(%rsp), %r13
 	movq	48(%rsp), %r14
 	movq	56(%rsp), %r15
 
 	call	__rsaz_512_reducex
 
 .Lmul_scatter_tail:
 ___
 $code.=<<___;
 	addq	64(%rsp), %r8
 	adcq	72(%rsp), %r9
 	adcq	80(%rsp), %r10
 	adcq	88(%rsp), %r11
 	adcq	96(%rsp), %r12
 	adcq	104(%rsp), %r13
 	adcq	112(%rsp), %r14
 	adcq	120(%rsp), %r15
 	movq	%xmm2, $inp
 	sbbq	%rcx, %rcx
 
 	call	__rsaz_512_subtract
 
-	movl	%r8d, 64*0($inp)	# scatter
-	shrq	\$32, %r8
-	movl	%r9d, 64*2($inp)
-	shrq	\$32, %r9
-	movl	%r10d, 64*4($inp)
-	shrq	\$32, %r10
-	movl	%r11d, 64*6($inp)
-	shrq	\$32, %r11
-	movl	%r12d, 64*8($inp)
-	shrq	\$32, %r12
-	movl	%r13d, 64*10($inp)
-	shrq	\$32, %r13
-	movl	%r14d, 64*12($inp)
-	shrq	\$32, %r14
-	movl	%r15d, 64*14($inp)
-	shrq	\$32, %r15
-	movl	%r8d, 64*1($inp)
-	movl	%r9d, 64*3($inp)
-	movl	%r10d, 64*5($inp)
-	movl	%r11d, 64*7($inp)
-	movl	%r12d, 64*9($inp)
-	movl	%r13d, 64*11($inp)
-	movl	%r14d, 64*13($inp)
-	movl	%r15d, 64*15($inp)
+	movq	%r8, 128*0($inp)	# scatter
+	movq	%r9, 128*1($inp)
+	movq	%r10, 128*2($inp)
+	movq	%r11, 128*3($inp)
+	movq	%r12, 128*4($inp)
+	movq	%r13, 128*5($inp)
+	movq	%r14, 128*6($inp)
+	movq	%r15, 128*7($inp)
 
 	leaq	128+24+48(%rsp), %rax
 	movq	-48(%rax), %r15
 	movq	-40(%rax), %r14
 	movq	-32(%rax), %r13
 	movq	-24(%rax), %r12
 	movq	-16(%rax), %rbp
 	movq	-8(%rax), %rbx
 	leaq	(%rax), %rsp
 .Lmul_scatter4_epilogue:
 	ret
 .size	rsaz_512_mul_scatter4,.-rsaz_512_mul_scatter4
 ___
 }
 {
 my ($out,$inp,$mod,$n0) = ("%rdi","%rsi","%rdx","%rcx");
 $code.=<<___;
 .globl	rsaz_512_mul_by_one
 .type	rsaz_512_mul_by_one,\@function,4
 .align	32
 rsaz_512_mul_by_one:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	subq	\$128+24, %rsp
 .Lmul_by_one_body:
 ___
 $code.=<<___ if ($addx);
 	movl	OPENSSL_ia32cap_P+8(%rip),%eax
 ___
 $code.=<<___;
 	movq	$mod, %rbp	# reassign argument
 	movq	$n0, 128(%rsp)
 
 	movq	($inp), %r8
 	pxor	%xmm0, %xmm0
 	movq	8($inp), %r9
 	movq	16($inp), %r10
 	movq	24($inp), %r11
 	movq	32($inp), %r12
 	movq	40($inp), %r13
 	movq	48($inp), %r14
 	movq	56($inp), %r15
 
 	movdqa	%xmm0, (%rsp)
 	movdqa	%xmm0, 16(%rsp)
 	movdqa	%xmm0, 32(%rsp)
 	movdqa	%xmm0, 48(%rsp)
 	movdqa	%xmm0, 64(%rsp)
 	movdqa	%xmm0, 80(%rsp)
 	movdqa	%xmm0, 96(%rsp)
 ___
 $code.=<<___ if ($addx);
 	andl	\$0x80100,%eax
 	cmpl	\$0x80100,%eax		# check for MULX and ADO/CX
 	je	.Lby_one_callx
 ___
 $code.=<<___;
 	call	__rsaz_512_reduce
 ___
 $code.=<<___ if ($addx);
 	jmp	.Lby_one_tail
 .align	32
 .Lby_one_callx:
 	movq	128(%rsp), %rdx		# pull $n0
 	call	__rsaz_512_reducex
 .Lby_one_tail:
 ___
 $code.=<<___;
 	movq	%r8, ($out)
 	movq	%r9, 8($out)
 	movq	%r10, 16($out)
 	movq	%r11, 24($out)
 	movq	%r12, 32($out)
 	movq	%r13, 40($out)
 	movq	%r14, 48($out)
 	movq	%r15, 56($out)
 
 	leaq	128+24+48(%rsp), %rax
 	movq	-48(%rax), %r15
 	movq	-40(%rax), %r14
 	movq	-32(%rax), %r13
 	movq	-24(%rax), %r12
 	movq	-16(%rax), %rbp
 	movq	-8(%rax), %rbx
 	leaq	(%rax), %rsp
 .Lmul_by_one_epilogue:
 	ret
 .size	rsaz_512_mul_by_one,.-rsaz_512_mul_by_one
 ___
 }
 {	# __rsaz_512_reduce
 	#
 	# input:	%r8-%r15, %rbp - mod, 128(%rsp) - n0
 	# output:	%r8-%r15
 	# clobbers:	everything except %rbp and %rdi
 $code.=<<___;
 .type	__rsaz_512_reduce,\@abi-omnipotent
 .align	32
 __rsaz_512_reduce:
 	movq	%r8, %rbx
 	imulq	128+8(%rsp), %rbx
 	movq	0(%rbp), %rax
 	movl	\$8, %ecx
 	jmp	.Lreduction_loop
 
 .align	32
 .Lreduction_loop:
 	mulq	%rbx
 	movq	8(%rbp), %rax
 	negq	%r8
 	movq	%rdx, %r8
 	adcq	\$0, %r8
 
 	mulq	%rbx
 	addq	%rax, %r9
 	movq	16(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r9, %r8
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	mulq	%rbx
 	addq	%rax, %r10
 	movq	24(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r10, %r9
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
 	addq	%rax, %r11
 	movq	32(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r11, %r10
 	 movq	128+8(%rsp), %rsi
 	#movq	%rdx, %r11
 	#adcq	\$0, %r11
 	adcq	\$0, %rdx
 	movq	%rdx, %r11
 
 	mulq	%rbx
 	addq	%rax, %r12
 	movq	40(%rbp), %rax
 	adcq	\$0, %rdx
 	 imulq	%r8, %rsi
 	addq	%r12, %r11
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r13
 	movq	48(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r13, %r12
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
 	addq	%rax, %r14
 	movq	56(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r14, %r13
 	movq	%rdx, %r14
 	adcq	\$0, %r14
 
 	mulq	%rbx
 	 movq	%rsi, %rbx
 	addq	%rax, %r15
 	 movq	0(%rbp), %rax
 	adcq	\$0, %rdx
 	addq	%r15, %r14
 	movq	%rdx, %r15
 	adcq	\$0, %r15
 
 	decl	%ecx
 	jne	.Lreduction_loop
 
 	ret
 .size	__rsaz_512_reduce,.-__rsaz_512_reduce
 ___
 }
 if ($addx) {
 	# __rsaz_512_reducex
 	#
 	# input:	%r8-%r15, %rbp - mod, 128(%rsp) - n0
 	# output:	%r8-%r15
 	# clobbers:	everything except %rbp and %rdi
 $code.=<<___;
 .type	__rsaz_512_reducex,\@abi-omnipotent
 .align	32
 __rsaz_512_reducex:
 	#movq	128+8(%rsp), %rdx		# pull $n0
 	imulq	%r8, %rdx
 	xorq	%rsi, %rsi			# cf=0,of=0
 	movl	\$8, %ecx
 	jmp	.Lreduction_loopx
 
 .align	32
 .Lreduction_loopx:
 	mov	%r8, %rbx
 	mulx	0(%rbp), %rax, %r8
 	adcx	%rbx, %rax
 	adox	%r9, %r8
 
 	mulx	8(%rbp), %rax, %r9
 	adcx	%rax, %r8
 	adox	%r10, %r9
 
 	mulx	16(%rbp), %rbx, %r10
 	adcx	%rbx, %r9
 	adox	%r11, %r10
 
 	mulx	24(%rbp), %rbx, %r11
 	adcx	%rbx, %r10
 	adox	%r12, %r11
 
 	.byte	0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	32(%rbp), %rbx, %r12
 	 mov	%rdx, %rax
 	 mov	%r8, %rdx
 	adcx	%rbx, %r11
 	adox	%r13, %r12
 
 	 mulx	128+8(%rsp), %rbx, %rdx
 	 mov	%rax, %rdx
 
 	mulx	40(%rbp), %rax, %r13
 	adcx	%rax, %r12
 	adox	%r14, %r13
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xb5,0x30,0x00,0x00,0x00	# mulx	48(%rbp), %rax, %r14
 	adcx	%rax, %r13
 	adox	%r15, %r14
 
 	mulx	56(%rbp), %rax, %r15
 	 mov	%rbx, %rdx
 	adcx	%rax, %r14
 	adox	%rsi, %r15			# %rsi is 0
 	adcx	%rsi, %r15			# cf=0
 
 	decl	%ecx				# of=0
 	jne	.Lreduction_loopx
 
 	ret
 .size	__rsaz_512_reducex,.-__rsaz_512_reducex
 ___
 }
 {	# __rsaz_512_subtract
 	# input: %r8-%r15, %rdi - $out, %rbp - $mod, %rcx - mask
 	# output:
 	# clobbers: everything but %rdi, %rsi and %rbp
 $code.=<<___;
 .type	__rsaz_512_subtract,\@abi-omnipotent
 .align	32
 __rsaz_512_subtract:
 	movq	%r8, ($out)
 	movq	%r9, 8($out)
 	movq	%r10, 16($out)
 	movq	%r11, 24($out)
 	movq	%r12, 32($out)
 	movq	%r13, 40($out)
 	movq	%r14, 48($out)
 	movq	%r15, 56($out)
 
 	movq	0($mod), %r8
 	movq	8($mod), %r9
 	negq	%r8
 	notq	%r9
 	andq	%rcx, %r8
 	movq	16($mod), %r10
 	andq	%rcx, %r9
 	notq	%r10
 	movq	24($mod), %r11
 	andq	%rcx, %r10
 	notq	%r11
 	movq	32($mod), %r12
 	andq	%rcx, %r11
 	notq	%r12
 	movq	40($mod), %r13
 	andq	%rcx, %r12
 	notq	%r13
 	movq	48($mod), %r14
 	andq	%rcx, %r13
 	notq	%r14
 	movq	56($mod), %r15
 	andq	%rcx, %r14
 	notq	%r15
 	andq	%rcx, %r15
 
 	addq	($out), %r8
 	adcq	8($out), %r9
 	adcq	16($out), %r10
 	adcq	24($out), %r11
 	adcq	32($out), %r12
 	adcq	40($out), %r13
 	adcq	48($out), %r14
 	adcq	56($out), %r15
 
 	movq	%r8, ($out)
 	movq	%r9, 8($out)
 	movq	%r10, 16($out)
 	movq	%r11, 24($out)
 	movq	%r12, 32($out)
 	movq	%r13, 40($out)
 	movq	%r14, 48($out)
 	movq	%r15, 56($out)
 
 	ret
 .size	__rsaz_512_subtract,.-__rsaz_512_subtract
 ___
 }
 {	# __rsaz_512_mul
 	#
 	# input: %rsi - ap, %rbp - bp
 	# ouput:
 	# clobbers: everything
 my ($ap,$bp) = ("%rsi","%rbp");
 $code.=<<___;
 .type	__rsaz_512_mul,\@abi-omnipotent
 .align	32
 __rsaz_512_mul:
 	leaq	8(%rsp), %rdi
 
 	movq	($ap), %rax
 	mulq	%rbx
 	movq	%rax, (%rdi)
 	movq	8($ap), %rax
 	movq	%rdx, %r8
 
 	mulq	%rbx
 	addq	%rax, %r8
 	movq	16($ap), %rax
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	mulq	%rbx
 	addq	%rax, %r9
 	movq	24($ap), %rax
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
 	addq	%rax, %r10
 	movq	32($ap), %rax
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	mulq	%rbx
 	addq	%rax, %r11
 	movq	40($ap), %rax
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r12
 	movq	48($ap), %rax
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
 	addq	%rax, %r13
 	movq	56($ap), %rax
 	movq	%rdx, %r14
 	adcq	\$0, %r14
 	
 	mulq	%rbx
 	addq	%rax, %r14
 	 movq	($ap), %rax
 	movq	%rdx, %r15
 	adcq	\$0, %r15
 
 	leaq	8($bp), $bp
 	leaq	8(%rdi), %rdi
 
 	movl	\$7, %ecx
 	jmp	.Loop_mul
 
 .align	32
 .Loop_mul:
 	movq	($bp), %rbx
 	mulq	%rbx
 	addq	%rax, %r8
 	movq	8($ap), %rax
 	movq	%r8, (%rdi)
 	movq	%rdx, %r8
 	adcq	\$0, %r8
 
 	mulq	%rbx
 	addq	%rax, %r9
 	movq	16($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r9, %r8
 	movq	%rdx, %r9
 	adcq	\$0, %r9
 
 	mulq	%rbx
 	addq	%rax, %r10
 	movq	24($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r10, %r9
 	movq	%rdx, %r10
 	adcq	\$0, %r10
 
 	mulq	%rbx
 	addq	%rax, %r11
 	movq	32($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r11, %r10
 	movq	%rdx, %r11
 	adcq	\$0, %r11
 
 	mulq	%rbx
 	addq	%rax, %r12
 	movq	40($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r12, %r11
 	movq	%rdx, %r12
 	adcq	\$0, %r12
 
 	mulq	%rbx
 	addq	%rax, %r13
 	movq	48($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r13, %r12
 	movq	%rdx, %r13
 	adcq	\$0, %r13
 
 	mulq	%rbx
 	addq	%rax, %r14
 	movq	56($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r14, %r13
 	movq	%rdx, %r14
 	 leaq	8($bp), $bp
 	adcq	\$0, %r14
 
 	mulq	%rbx
 	addq	%rax, %r15
 	 movq	($ap), %rax
 	adcq	\$0, %rdx
 	addq	%r15, %r14
 	movq	%rdx, %r15	
 	adcq	\$0, %r15
 
 	leaq	8(%rdi), %rdi
 
 	decl	%ecx
 	jnz	.Loop_mul
 
 	movq	%r8, (%rdi)
 	movq	%r9, 8(%rdi)
 	movq	%r10, 16(%rdi)
 	movq	%r11, 24(%rdi)
 	movq	%r12, 32(%rdi)
 	movq	%r13, 40(%rdi)
 	movq	%r14, 48(%rdi)
 	movq	%r15, 56(%rdi)
 
 	ret
 .size	__rsaz_512_mul,.-__rsaz_512_mul
 ___
 }
 if ($addx) {
 	# __rsaz_512_mulx
 	#
 	# input: %rsi - ap, %rbp - bp
 	# ouput:
 	# clobbers: everything
 my ($ap,$bp,$zero) = ("%rsi","%rbp","%rdi");
 $code.=<<___;
 .type	__rsaz_512_mulx,\@abi-omnipotent
 .align	32
 __rsaz_512_mulx:
 	mulx	($ap), %rbx, %r8	# initial %rdx preloaded by caller
 	mov	\$-6, %rcx
 
 	mulx	8($ap), %rax, %r9
 	movq	%rbx, 8(%rsp)
 
 	mulx	16($ap), %rbx, %r10
 	adc	%rax, %r8
 
 	mulx	24($ap), %rax, %r11
 	adc	%rbx, %r9
 
 	mulx	32($ap), %rbx, %r12
 	adc	%rax, %r10
 
 	mulx	40($ap), %rax, %r13
 	adc	%rbx, %r11
 
 	mulx	48($ap), %rbx, %r14
 	adc	%rax, %r12
 
 	mulx	56($ap), %rax, %r15
 	 mov	8($bp), %rdx
 	adc	%rbx, %r13
 	adc	%rax, %r14
 	adc	\$0, %r15
 
 	xor	$zero, $zero		# cf=0,of=0
 	jmp	.Loop_mulx
 
 .align	32
 .Loop_mulx:
 	movq	%r8, %rbx
 	mulx	($ap), %rax, %r8
 	adcx	%rax, %rbx
 	adox	%r9, %r8
 
 	mulx	8($ap), %rax, %r9
 	adcx	%rax, %r8
 	adox	%r10, %r9
 
 	mulx	16($ap), %rax, %r10
 	adcx	%rax, %r9
 	adox	%r11, %r10
 
 	mulx	24($ap), %rax, %r11
 	adcx	%rax, %r10
 	adox	%r12, %r11
 
 	.byte	0x3e,0xc4,0x62,0xfb,0xf6,0xa6,0x20,0x00,0x00,0x00	# mulx	32($ap), %rax, %r12
 	adcx	%rax, %r11
 	adox	%r13, %r12
 
 	mulx	40($ap), %rax, %r13
 	adcx	%rax, %r12
 	adox	%r14, %r13
 
 	mulx	48($ap), %rax, %r14
 	adcx	%rax, %r13
 	adox	%r15, %r14
 
 	mulx	56($ap), %rax, %r15
 	 movq	64($bp,%rcx,8), %rdx
 	 movq	%rbx, 8+64-8(%rsp,%rcx,8)
 	adcx	%rax, %r14
 	adox	$zero, %r15
 	adcx	$zero, %r15		# cf=0
 
 	inc	%rcx			# of=0
 	jnz	.Loop_mulx
 
 	movq	%r8, %rbx
 	mulx	($ap), %rax, %r8
 	adcx	%rax, %rbx
 	adox	%r9, %r8
 
 	.byte	0xc4,0x62,0xfb,0xf6,0x8e,0x08,0x00,0x00,0x00	# mulx	8($ap), %rax, %r9
 	adcx	%rax, %r8
 	adox	%r10, %r9
 
 	.byte	0xc4,0x62,0xfb,0xf6,0x96,0x10,0x00,0x00,0x00	# mulx	16($ap), %rax, %r10
 	adcx	%rax, %r9
 	adox	%r11, %r10
 
 	mulx	24($ap), %rax, %r11
 	adcx	%rax, %r10
 	adox	%r12, %r11
 
 	mulx	32($ap), %rax, %r12
 	adcx	%rax, %r11
 	adox	%r13, %r12
 
 	mulx	40($ap), %rax, %r13
 	adcx	%rax, %r12
 	adox	%r14, %r13
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xb6,0x30,0x00,0x00,0x00	# mulx	48($ap), %rax, %r14
 	adcx	%rax, %r13
 	adox	%r15, %r14
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xbe,0x38,0x00,0x00,0x00	# mulx	56($ap), %rax, %r15
 	adcx	%rax, %r14
 	adox	$zero, %r15
 	adcx	$zero, %r15
 
 	mov	%rbx, 8+64-8(%rsp)
 	mov	%r8, 8+64(%rsp)
 	mov	%r9, 8+64+8(%rsp)
 	mov	%r10, 8+64+16(%rsp)
 	mov	%r11, 8+64+24(%rsp)
 	mov	%r12, 8+64+32(%rsp)
 	mov	%r13, 8+64+40(%rsp)
 	mov	%r14, 8+64+48(%rsp)
 	mov	%r15, 8+64+56(%rsp)
 
 	ret
 .size	__rsaz_512_mulx,.-__rsaz_512_mulx
 ___
 }
 {
 my ($out,$inp,$power)= $win64 ? ("%rcx","%rdx","%r8d") : ("%rdi","%rsi","%edx");
 $code.=<<___;
 .globl	rsaz_512_scatter4
 .type	rsaz_512_scatter4,\@abi-omnipotent
 .align	16
 rsaz_512_scatter4:
-	leaq	($out,$power,4), $out
+	leaq	($out,$power,8), $out
 	movl	\$8, %r9d
 	jmp	.Loop_scatter
 .align	16
 .Loop_scatter:
 	movq	($inp), %rax
 	leaq	8($inp), $inp
-	movl	%eax, ($out)
-	shrq	\$32, %rax
-	movl	%eax, 64($out)
+	movq	%rax, ($out)
 	leaq	128($out), $out
 	decl	%r9d
 	jnz	.Loop_scatter
 	ret
 .size	rsaz_512_scatter4,.-rsaz_512_scatter4
 
 .globl	rsaz_512_gather4
 .type	rsaz_512_gather4,\@abi-omnipotent
 .align	16
 rsaz_512_gather4:
-	leaq	($inp,$power,4), $inp
+___
+$code.=<<___	if ($win64);
+.LSEH_begin_rsaz_512_gather4:
+	.byte	0x48,0x81,0xec,0xa8,0x00,0x00,0x00	# sub    $0xa8,%rsp
+	.byte	0x0f,0x29,0x34,0x24			# movaps %xmm6,(%rsp)
+	.byte	0x0f,0x29,0x7c,0x24,0x10		# movaps %xmm7,0x10(%rsp)
+	.byte	0x44,0x0f,0x29,0x44,0x24,0x20		# movaps %xmm8,0x20(%rsp)
+	.byte	0x44,0x0f,0x29,0x4c,0x24,0x30		# movaps %xmm9,0x30(%rsp)
+	.byte	0x44,0x0f,0x29,0x54,0x24,0x40		# movaps %xmm10,0x40(%rsp)
+	.byte	0x44,0x0f,0x29,0x5c,0x24,0x50		# movaps %xmm11,0x50(%rsp)
+	.byte	0x44,0x0f,0x29,0x64,0x24,0x60		# movaps %xmm12,0x60(%rsp)
+	.byte	0x44,0x0f,0x29,0x6c,0x24,0x70		# movaps %xmm13,0x70(%rsp)
+	.byte	0x44,0x0f,0x29,0xb4,0x24,0x80,0,0,0	# movaps %xmm14,0x80(%rsp)
+	.byte	0x44,0x0f,0x29,0xbc,0x24,0x90,0,0,0	# movaps %xmm15,0x90(%rsp)
+___
+$code.=<<___;
+	movd	$power,%xmm8
+	movdqa	.Linc+16(%rip),%xmm1	# 00000002000000020000000200000002
+	movdqa	.Linc(%rip),%xmm0	# 00000001000000010000000000000000
+
+	pshufd	\$0,%xmm8,%xmm8		# broadcast $power
+	movdqa	%xmm1,%xmm7
+	movdqa	%xmm1,%xmm2
+___
+########################################################################
+# calculate mask by comparing 0..15 to $power
+#
+for($i=0;$i<4;$i++) {
+$code.=<<___;
+	paddd	%xmm`$i`,%xmm`$i+1`
+	pcmpeqd	%xmm8,%xmm`$i`
+	movdqa	%xmm7,%xmm`$i+3`
+___
+}
+for(;$i<7;$i++) {
+$code.=<<___;
+	paddd	%xmm`$i`,%xmm`$i+1`
+	pcmpeqd	%xmm8,%xmm`$i`
+___
+}
+$code.=<<___;
+	pcmpeqd	%xmm8,%xmm7
 	movl	\$8, %r9d
 	jmp	.Loop_gather
 .align	16
 .Loop_gather:
-	movl	($inp), %eax
-	movl	64($inp), %r8d
+	movdqa	16*0($inp),%xmm8
+	movdqa	16*1($inp),%xmm9
+	movdqa	16*2($inp),%xmm10
+	movdqa	16*3($inp),%xmm11
+	pand	%xmm0,%xmm8
+	movdqa	16*4($inp),%xmm12
+	pand	%xmm1,%xmm9
+	movdqa	16*5($inp),%xmm13
+	pand	%xmm2,%xmm10
+	movdqa	16*6($inp),%xmm14
+	pand	%xmm3,%xmm11
+	movdqa	16*7($inp),%xmm15
 	leaq	128($inp), $inp
-	shlq	\$32, %r8
-	or	%r8, %rax
-	movq	%rax, ($out)
+	pand	%xmm4,%xmm12
+	pand	%xmm5,%xmm13
+	pand	%xmm6,%xmm14
+	pand	%xmm7,%xmm15
+	por	%xmm10,%xmm8
+	por	%xmm11,%xmm9
+	por	%xmm12,%xmm8
+	por	%xmm13,%xmm9
+	por	%xmm14,%xmm8
+	por	%xmm15,%xmm9
+
+	por	%xmm9,%xmm8
+	pshufd	\$0x4e,%xmm8,%xmm9
+	por	%xmm9,%xmm8
+	movq	%xmm8,($out)
 	leaq	8($out), $out
 	decl	%r9d
 	jnz	.Loop_gather
+___
+$code.=<<___	if ($win64);
+	movaps	0x00(%rsp),%xmm6
+	movaps	0x10(%rsp),%xmm7
+	movaps	0x20(%rsp),%xmm8
+	movaps	0x30(%rsp),%xmm9
+	movaps	0x40(%rsp),%xmm10
+	movaps	0x50(%rsp),%xmm11
+	movaps	0x60(%rsp),%xmm12
+	movaps	0x70(%rsp),%xmm13
+	movaps	0x80(%rsp),%xmm14
+	movaps	0x90(%rsp),%xmm15
+	add	\$0xa8,%rsp
+___
+$code.=<<___;
 	ret
+.LSEH_end_rsaz_512_gather4:
 .size	rsaz_512_gather4,.-rsaz_512_gather4
+
+.align	64
+.Linc:
+	.long	0,0, 1,1
+	.long	2,2, 2,2
 ___
 }
 
 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
 #		CONTEXT *context,DISPATCHER_CONTEXT *disp)
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___;
 .extern	__imp_RtlVirtualUnwind
 .type	se_handler,\@abi-omnipotent
 .align	16
 se_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# end of prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lcommon_seh_tail
 
 	lea	128+24+48(%rax),%rax
 
+	lea	.Lmul_gather4_epilogue(%rip),%rbx
+	cmp	%r10,%rbx
+	jne	.Lse_not_in_mul_gather4
+
+	lea	0xb0(%rax),%rax
+
+	lea	-48-0xa8(%rax),%rsi
+	lea	512($context),%rdi
+	mov	\$20,%ecx
+	.long	0xa548f3fc		# cld; rep movsq
+
+.Lse_not_in_mul_gather4:
 	mov	-8(%rax),%rbx
 	mov	-16(%rax),%rbp
 	mov	-24(%rax),%r12
 	mov	-32(%rax),%r13
 	mov	-40(%rax),%r14
 	mov	-48(%rax),%r15
 	mov	%rbx,144($context)	# restore context->Rbx
 	mov	%rbp,160($context)	# restore context->Rbp
 	mov	%r12,216($context)	# restore context->R12
 	mov	%r13,224($context)	# restore context->R13
 	mov	%r14,232($context)	# restore context->R14
 	mov	%r15,240($context)	# restore context->R15
 
 .Lcommon_seh_tail:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$154,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
-.size	sqr_handler,.-sqr_handler
+.size	se_handler,.-se_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_rsaz_512_sqr
 	.rva	.LSEH_end_rsaz_512_sqr
 	.rva	.LSEH_info_rsaz_512_sqr
 
 	.rva	.LSEH_begin_rsaz_512_mul
 	.rva	.LSEH_end_rsaz_512_mul
 	.rva	.LSEH_info_rsaz_512_mul
 
 	.rva	.LSEH_begin_rsaz_512_mul_gather4
 	.rva	.LSEH_end_rsaz_512_mul_gather4
 	.rva	.LSEH_info_rsaz_512_mul_gather4
 
 	.rva	.LSEH_begin_rsaz_512_mul_scatter4
 	.rva	.LSEH_end_rsaz_512_mul_scatter4
 	.rva	.LSEH_info_rsaz_512_mul_scatter4
 
 	.rva	.LSEH_begin_rsaz_512_mul_by_one
 	.rva	.LSEH_end_rsaz_512_mul_by_one
 	.rva	.LSEH_info_rsaz_512_mul_by_one
 
+	.rva	.LSEH_begin_rsaz_512_gather4
+	.rva	.LSEH_end_rsaz_512_gather4
+	.rva	.LSEH_info_rsaz_512_gather4
+
 .section	.xdata
 .align	8
 .LSEH_info_rsaz_512_sqr:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lsqr_body,.Lsqr_epilogue			# HandlerData[]
 .LSEH_info_rsaz_512_mul:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lmul_body,.Lmul_epilogue			# HandlerData[]
 .LSEH_info_rsaz_512_mul_gather4:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lmul_gather4_body,.Lmul_gather4_epilogue	# HandlerData[]
 .LSEH_info_rsaz_512_mul_scatter4:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lmul_scatter4_body,.Lmul_scatter4_epilogue	# HandlerData[]
 .LSEH_info_rsaz_512_mul_by_one:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lmul_by_one_body,.Lmul_by_one_epilogue		# HandlerData[]
+.LSEH_info_rsaz_512_gather4:
+	.byte	0x01,0x46,0x16,0x00
+	.byte	0x46,0xf8,0x09,0x00	# vmovaps 0x90(rsp),xmm15
+	.byte	0x3d,0xe8,0x08,0x00	# vmovaps 0x80(rsp),xmm14
+	.byte	0x34,0xd8,0x07,0x00	# vmovaps 0x70(rsp),xmm13
+	.byte	0x2e,0xc8,0x06,0x00	# vmovaps 0x60(rsp),xmm12
+	.byte	0x28,0xb8,0x05,0x00	# vmovaps 0x50(rsp),xmm11
+	.byte	0x22,0xa8,0x04,0x00	# vmovaps 0x40(rsp),xmm10
+	.byte	0x1c,0x98,0x03,0x00	# vmovaps 0x30(rsp),xmm9
+	.byte	0x16,0x88,0x02,0x00	# vmovaps 0x20(rsp),xmm8
+	.byte	0x10,0x78,0x01,0x00	# vmovaps 0x10(rsp),xmm7
+	.byte	0x0b,0x68,0x00,0x00	# vmovaps 0x00(rsp),xmm6
+	.byte	0x07,0x01,0x15,0x00	# sub     rsp,0xa8
 ___
 }
 
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
 print $code;
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont.pl	(revision 296273)
@@ -1,1407 +1,1444 @@
 #!/usr/bin/env perl
 
 # ====================================================================
 # Written by Andy Polyakov  for the OpenSSL
 # project. The module is, however, dual licensed under OpenSSL and
 # CRYPTOGAMS licenses depending on where you obtain it. For further
 # details see http://www.openssl.org/~appro/cryptogams/.
 # ====================================================================
 
 # October 2005.
 #
 # Montgomery multiplication routine for x86_64. While it gives modest
 # 9% improvement of rsa4096 sign on Opteron, rsa512 sign runs more
 # than twice, >2x, as fast. Most common rsa1024 sign is improved by
 # respectful 50%. It remains to be seen if loop unrolling and
 # dedicated squaring routine can provide further improvement...
 
 # July 2011.
 #
 # Add dedicated squaring procedure. Performance improvement varies
 # from platform to platform, but in average it's ~5%/15%/25%/33%
 # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
 
 # August 2011.
 #
 # Unroll and modulo-schedule inner loops in such manner that they
 # are "fallen through" for input lengths of 8, which is critical for
 # 1024-bit RSA *sign*. Average performance improvement in comparison
 # to *initial* version of this module from 2005 is ~0%/30%/40%/45%
 # for 512-/1024-/2048-/4096-bit RSA *sign* benchmarks respectively.
 
 # June 2013.
 #
 # Optimize reduction in squaring procedure and improve 1024+-bit RSA
 # sign performance by 10-16% on Intel Sandy Bridge and later
 # (virtually same on non-Intel processors).
 
 # August 2013.
 #
 # Add MULX/ADOX/ADCX code path.
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.23);
 }
 
 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.10);
 }
 
 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$addx = ($1>=12);
 }
 
 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
 	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
 	$addx = ($ver>=3.03);
 }
 
 # int bn_mul_mont(
 $rp="%rdi";	# BN_ULONG *rp,
 $ap="%rsi";	# const BN_ULONG *ap,
 $bp="%rdx";	# const BN_ULONG *bp,
 $np="%rcx";	# const BN_ULONG *np,
 $n0="%r8";	# const BN_ULONG *n0,
 $num="%r9";	# int num);
 $lo0="%r10";
 $hi0="%r11";
 $hi1="%r13";
 $i="%r14";
 $j="%r15";
 $m0="%rbx";
 $m1="%rbp";
 
 $code=<<___;
 .text
 
 .extern	OPENSSL_ia32cap_P
 
 .globl	bn_mul_mont
 .type	bn_mul_mont,\@function,6
 .align	16
 bn_mul_mont:
 	test	\$3,${num}d
 	jnz	.Lmul_enter
 	cmp	\$8,${num}d
 	jb	.Lmul_enter
 ___
 $code.=<<___ if ($addx);
 	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
 ___
 $code.=<<___;
 	cmp	$ap,$bp
 	jne	.Lmul4x_enter
 	test	\$7,${num}d
 	jz	.Lsqr8x_enter
 	jmp	.Lmul4x_enter
 
 .align	16
 .Lmul_enter:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	mov	${num}d,${num}d
 	lea	2($num),%r10
 	mov	%rsp,%r11
 	neg	%r10
 	lea	(%rsp,%r10,8),%rsp	# tp=alloca(8*(num+2))
 	and	\$-1024,%rsp		# minimize TLB usage
 
 	mov	%r11,8(%rsp,$num,8)	# tp[num+1]=%rsp
 .Lmul_body:
 	mov	$bp,%r12		# reassign $bp
 ___
 		$bp="%r12";
 $code.=<<___;
 	mov	($n0),$n0		# pull n0[0] value
 	mov	($bp),$m0		# m0=bp[0]
 	mov	($ap),%rax
 
 	xor	$i,$i			# i=0
 	xor	$j,$j			# j=0
 
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[0]
 	mov	%rax,$lo0
 	mov	($np),%rax
 
 	imulq	$lo0,$m1		# "tp[0]"*n0
 	mov	%rdx,$hi0
 
 	mulq	$m1			# np[0]*m1
 	add	%rax,$lo0		# discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$hi1
 
 	lea	1($j),$j		# j++
 	jmp	.L1st_enter
 
 .align	16
 .L1st:
 	add	%rax,$hi1
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
 	mov	$lo0,$hi0
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 
 .L1st_enter:
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$hi0
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	lea	1($j),$j		# j++
 	mov	%rdx,$lo0
 
 	mulq	$m1			# np[j]*m1
 	cmp	$num,$j
 	jne	.L1st
 
 	add	%rax,$hi1
 	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 	mov	$lo0,$hi0
 
 	xor	%rdx,%rdx
 	add	$hi0,$hi1
 	adc	\$0,%rdx
 	mov	$hi1,-8(%rsp,$num,8)
 	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
 
 	lea	1($i),$i		# i++
 	jmp	.Louter
 .align	16
 .Louter:
 	mov	($bp,$i,8),$m0		# m0=bp[i]
 	xor	$j,$j			# j=0
 	mov	$n0,$m1
 	mov	(%rsp),$lo0
 	mulq	$m0			# ap[0]*bp[i]
 	add	%rax,$lo0		# ap[0]*bp[i]+tp[0]
 	mov	($np),%rax
 	adc	\$0,%rdx
 
 	imulq	$lo0,$m1		# tp[0]*n0
 	mov	%rdx,$hi0
 
 	mulq	$m1			# np[0]*m1
 	add	%rax,$lo0		# discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	8(%rsp),$lo0		# tp[1]
 	mov	%rdx,$hi1
 
 	lea	1($j),$j		# j++
 	jmp	.Linner_enter
 
 .align	16
 .Linner:
 	add	%rax,$hi1
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
 	mov	(%rsp,$j,8),$lo0
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 
 .Linner_enter:
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$hi0
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	$hi0,$lo0		# ap[j]*bp[i]+tp[j]
 	mov	%rdx,$hi0
 	adc	\$0,$hi0
 	lea	1($j),$j		# j++
 
 	mulq	$m1			# np[j]*m1
 	cmp	$num,$j
 	jne	.Linner
 
 	add	%rax,$hi1
 	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
 	mov	(%rsp,$j,8),$lo0
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 
 	xor	%rdx,%rdx
 	add	$hi0,$hi1
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# pull upmost overflow bit
 	adc	\$0,%rdx
 	mov	$hi1,-8(%rsp,$num,8)
 	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
 
 	lea	1($i),$i		# i++
 	cmp	$num,$i
 	jb	.Louter
 
 	xor	$i,$i			# i=0 and clear CF!
 	mov	(%rsp),%rax		# tp[0]
 	lea	(%rsp),$ap		# borrow ap for tp
 	mov	$num,$j			# j=num
 	jmp	.Lsub
 .align	16
 .Lsub:	sbb	($np,$i,8),%rax
 	mov	%rax,($rp,$i,8)		# rp[i]=tp[i]-np[i]
 	mov	8($ap,$i,8),%rax	# tp[i+1]
 	lea	1($i),$i		# i++
 	dec	$j			# doesnn't affect CF!
 	jnz	.Lsub
 
 	sbb	\$0,%rax		# handle upmost overflow bit
 	xor	$i,$i
 	and	%rax,$ap
 	not	%rax
 	mov	$rp,$np
 	and	%rax,$np
 	mov	$num,$j			# j=num
 	or	$np,$ap			# ap=borrow?tp:rp
 .align	16
 .Lcopy:					# copy or in-place refresh
 	mov	($ap,$i,8),%rax
 	mov	$i,(%rsp,$i,8)		# zap temporary vector
 	mov	%rax,($rp,$i,8)		# rp[i]=tp[i]
 	lea	1($i),$i
 	sub	\$1,$j
 	jnz	.Lcopy
 
 	mov	8(%rsp,$num,8),%rsi	# restore %rsp
 	mov	\$1,%rax
 	mov	(%rsi),%r15
 	mov	8(%rsi),%r14
 	mov	16(%rsi),%r13
 	mov	24(%rsi),%r12
 	mov	32(%rsi),%rbp
 	mov	40(%rsi),%rbx
 	lea	48(%rsi),%rsp
 .Lmul_epilogue:
 	ret
 .size	bn_mul_mont,.-bn_mul_mont
 ___
 {{{
 my @A=("%r10","%r11");
 my @N=("%r13","%rdi");
 $code.=<<___;
 .type	bn_mul4x_mont,\@function,6
 .align	16
 bn_mul4x_mont:
 .Lmul4x_enter:
 ___
 $code.=<<___ if ($addx);
 	and	\$0x80100,%r11d
 	cmp	\$0x80100,%r11d
 	je	.Lmulx4x_enter
 ___
 $code.=<<___;
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	mov	${num}d,${num}d
 	lea	4($num),%r10
 	mov	%rsp,%r11
 	neg	%r10
 	lea	(%rsp,%r10,8),%rsp	# tp=alloca(8*(num+4))
 	and	\$-1024,%rsp		# minimize TLB usage
 
 	mov	%r11,8(%rsp,$num,8)	# tp[num+1]=%rsp
 .Lmul4x_body:
 	mov	$rp,16(%rsp,$num,8)	# tp[num+2]=$rp
 	mov	%rdx,%r12		# reassign $bp
 ___
 		$bp="%r12";
 $code.=<<___;
 	mov	($n0),$n0		# pull n0[0] value
 	mov	($bp),$m0		# m0=bp[0]
 	mov	($ap),%rax
 
 	xor	$i,$i			# i=0
 	xor	$j,$j			# j=0
 
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[0]
 	mov	%rax,$A[0]
 	mov	($np),%rax
 
 	imulq	$A[0],$m1		# "tp[0]"*n0
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[0]*m1
 	add	%rax,$A[0]		# discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$N[1]
 
 	mulq	$m0
 	add	%rax,$A[1]
 	mov	8($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1
 	add	%rax,$N[1]
 	mov	16($ap),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	lea	4($j),$j		# j++
 	adc	\$0,%rdx
 	mov	$N[1],(%rsp)
 	mov	%rdx,$N[0]
 	jmp	.L1st4x
 .align	16
 .L1st4x:
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
 	mov	-16($np,$j,8),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
 	mov	-8($np,$j,8),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[1],-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-8(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
 	mov	8($np,$j,8),%rax
 	adc	\$0,%rdx
 	lea	4($j),$j		# j++
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	-16($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[1],-32(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 	cmp	$num,$j
 	jb	.L1st4x
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
 	mov	-16($np,$j,8),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
 	mov	-8($np,$j,8),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[1],-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 
 	xor	$N[1],$N[1]
 	add	$A[0],$N[0]
 	adc	\$0,$N[1]
 	mov	$N[0],-8(%rsp,$j,8)
 	mov	$N[1],(%rsp,$j,8)	# store upmost overflow bit
 
 	lea	1($i),$i		# i++
 .align	4
 .Louter4x:
 	mov	($bp,$i,8),$m0		# m0=bp[i]
 	xor	$j,$j			# j=0
 	mov	(%rsp),$A[0]
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[i]
 	add	%rax,$A[0]		# ap[0]*bp[i]+tp[0]
 	mov	($np),%rax
 	adc	\$0,%rdx
 
 	imulq	$A[0],$m1		# tp[0]*n0
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[0]*m1
 	add	%rax,$A[0]		# "$N[0]", discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
 	mov	8($np),%rax
 	adc	\$0,%rdx
 	add	8(%rsp),$A[1]		# +tp[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	16($ap),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[i]+tp[j]
 	lea	4($j),$j		# j+=2
 	adc	\$0,%rdx
 	mov	$N[1],(%rsp)		# tp[j-1]
 	mov	%rdx,$N[0]
 	jmp	.Linner4x
 .align	16
 .Linner4x:
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
 	mov	-16($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	-16(%rsp,$j,8),$A[0]	# ap[j]*bp[i]+tp[j]
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
 	mov	-8($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	-8(%rsp,$j,8),$A[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	adc	\$0,%rdx
 	mov	$N[1],-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	(%rsp,$j,8),$A[0]	# ap[j]*bp[i]+tp[j]
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[0],-8(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
 	mov	8($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	8(%rsp,$j,8),$A[1]
 	adc	\$0,%rdx
 	lea	4($j),$j		# j++
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	-16($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	adc	\$0,%rdx
 	mov	$N[1],-32(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 	cmp	$num,$j
 	jb	.Linner4x
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
 	mov	-16($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	-16(%rsp,$j,8),$A[0]	# ap[j]*bp[i]+tp[j]
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
 	mov	-8($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	-8(%rsp,$j,8),$A[1]
 	adc	\$0,%rdx
 	lea	1($i),$i		# i++
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	adc	\$0,%rdx
 	mov	$N[1],-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$N[0]
 
 	xor	$N[1],$N[1]
 	add	$A[0],$N[0]
 	adc	\$0,$N[1]
 	add	(%rsp,$num,8),$N[0]	# pull upmost overflow bit
 	adc	\$0,$N[1]
 	mov	$N[0],-8(%rsp,$j,8)
 	mov	$N[1],(%rsp,$j,8)	# store upmost overflow bit
 
 	cmp	$num,$i
 	jb	.Louter4x
 ___
 {
 my @ri=("%rax","%rdx",$m0,$m1);
 $code.=<<___;
 	mov	16(%rsp,$num,8),$rp	# restore $rp
 	mov	0(%rsp),@ri[0]		# tp[0]
 	pxor	%xmm0,%xmm0
 	mov	8(%rsp),@ri[1]		# tp[1]
 	shr	\$2,$num		# num/=4
 	lea	(%rsp),$ap		# borrow ap for tp
 	xor	$i,$i			# i=0 and clear CF!
 
 	sub	0($np),@ri[0]
 	mov	16($ap),@ri[2]		# tp[2]
 	mov	24($ap),@ri[3]		# tp[3]
 	sbb	8($np),@ri[1]
 	lea	-1($num),$j		# j=num/4-1
 	jmp	.Lsub4x
 .align	16
 .Lsub4x:
 	mov	@ri[0],0($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	mov	@ri[1],8($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	sbb	16($np,$i,8),@ri[2]
 	mov	32($ap,$i,8),@ri[0]	# tp[i+1]
 	mov	40($ap,$i,8),@ri[1]
 	sbb	24($np,$i,8),@ri[3]
 	mov	@ri[2],16($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	mov	@ri[3],24($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	sbb	32($np,$i,8),@ri[0]
 	mov	48($ap,$i,8),@ri[2]
 	mov	56($ap,$i,8),@ri[3]
 	sbb	40($np,$i,8),@ri[1]
 	lea	4($i),$i		# i++
 	dec	$j			# doesnn't affect CF!
 	jnz	.Lsub4x
 
 	mov	@ri[0],0($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	mov	32($ap,$i,8),@ri[0]	# load overflow bit
 	sbb	16($np,$i,8),@ri[2]
 	mov	@ri[1],8($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	sbb	24($np,$i,8),@ri[3]
 	mov	@ri[2],16($rp,$i,8)	# rp[i]=tp[i]-np[i]
 
 	sbb	\$0,@ri[0]		# handle upmost overflow bit
 	mov	@ri[3],24($rp,$i,8)	# rp[i]=tp[i]-np[i]
 	xor	$i,$i			# i=0
 	and	@ri[0],$ap
 	not	@ri[0]
 	mov	$rp,$np
 	and	@ri[0],$np
 	lea	-1($num),$j
 	or	$np,$ap			# ap=borrow?tp:rp
 
 	movdqu	($ap),%xmm1
 	movdqa	%xmm0,(%rsp)
 	movdqu	%xmm1,($rp)
 	jmp	.Lcopy4x
 .align	16
 .Lcopy4x:					# copy or in-place refresh
 	movdqu	16($ap,$i),%xmm2
 	movdqu	32($ap,$i),%xmm1
 	movdqa	%xmm0,16(%rsp,$i)
 	movdqu	%xmm2,16($rp,$i)
 	movdqa	%xmm0,32(%rsp,$i)
 	movdqu	%xmm1,32($rp,$i)
 	lea	32($i),$i
 	dec	$j
 	jnz	.Lcopy4x
 
 	shl	\$2,$num
 	movdqu	16($ap,$i),%xmm2
 	movdqa	%xmm0,16(%rsp,$i)
 	movdqu	%xmm2,16($rp,$i)
 ___
 }
 $code.=<<___;
 	mov	8(%rsp,$num,8),%rsi	# restore %rsp
 	mov	\$1,%rax
 	mov	(%rsi),%r15
 	mov	8(%rsi),%r14
 	mov	16(%rsi),%r13
 	mov	24(%rsi),%r12
 	mov	32(%rsi),%rbp
 	mov	40(%rsi),%rbx
 	lea	48(%rsi),%rsp
 .Lmul4x_epilogue:
 	ret
 .size	bn_mul4x_mont,.-bn_mul4x_mont
 ___
 }}}
 {{{
 ######################################################################
 # void bn_sqr8x_mont(
 my $rptr="%rdi";	# const BN_ULONG *rptr,
 my $aptr="%rsi";	# const BN_ULONG *aptr,
 my $bptr="%rdx";	# not used
 my $nptr="%rcx";	# const BN_ULONG *nptr,
 my $n0  ="%r8";		# const BN_ULONG *n0);
 my $num ="%r9";		# int num, has to be divisible by 8
 
 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
 my @A0=("%r10","%r11");
 my @A1=("%r12","%r13");
 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
 
 $code.=<<___	if ($addx);
 .extern	bn_sqrx8x_internal		# see x86_64-mont5 module
 ___
 $code.=<<___;
 .extern	bn_sqr8x_internal		# see x86_64-mont5 module
 
 .type	bn_sqr8x_mont,\@function,6
 .align	32
 bn_sqr8x_mont:
 .Lsqr8x_enter:
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	mov	${num}d,%r10d
 	shl	\$3,${num}d		# convert $num to bytes
 	shl	\$3+2,%r10		# 4*$num
 	neg	$num
 
 	##############################################################
 	# ensure that stack frame doesn't alias with $aptr modulo
 	# 4096. this is done to allow memory disambiguation logic
 	# do its job.
 	#
-	lea	-64(%rsp,$num,4),%r11
+	lea	-64(%rsp,$num,2),%r11
 	mov	($n0),$n0		# *n0
 	sub	$aptr,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lsqr8x_sp_alt
 	sub	%r11,%rsp		# align with $aptr
-	lea	-64(%rsp,$num,4),%rsp	# alloca(frame+4*$num)
+	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
 	jmp	.Lsqr8x_sp_done
 
 .align	32
 .Lsqr8x_sp_alt:
-	lea	4096-64(,$num,4),%r10	# 4096-frame-4*$num
-	lea	-64(%rsp,$num,4),%rsp	# alloca(frame+4*$num)
+	lea	4096-64(,$num,2),%r10	# 4096-frame-2*$num
+	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lsqr8x_sp_done:
 	and	\$-64,%rsp
-	mov	$num,%r10	
+	mov	$num,%r10
 	neg	$num
 
-	lea	64(%rsp,$num,2),%r11	# copy of modulus
 	mov	$n0,  32(%rsp)
 	mov	%rax, 40(%rsp)		# save original %rsp
 .Lsqr8x_body:
 
-	mov	$num,$i
-	movq	%r11, %xmm2		# save pointer to modulus copy
-	shr	\$3+2,$i
-	mov	OPENSSL_ia32cap_P+8(%rip),%eax
-	jmp	.Lsqr8x_copy_n
-
-.align	32
-.Lsqr8x_copy_n:
-	movq	8*0($nptr),%xmm0
-	movq	8*1($nptr),%xmm1
-	movq	8*2($nptr),%xmm3
-	movq	8*3($nptr),%xmm4
-	lea	8*4($nptr),$nptr
-	movdqa	%xmm0,16*0(%r11)
-	movdqa	%xmm1,16*1(%r11)
-	movdqa	%xmm3,16*2(%r11)
-	movdqa	%xmm4,16*3(%r11)
-	lea	16*4(%r11),%r11
-	dec	$i
-	jnz	.Lsqr8x_copy_n
-
+	movq	$nptr, %xmm2		# save pointer to modulus
 	pxor	%xmm0,%xmm0
 	movq	$rptr,%xmm1		# save $rptr
 	movq	%r10, %xmm3		# -$num
 ___
 $code.=<<___ if ($addx);
+	mov	OPENSSL_ia32cap_P+8(%rip),%eax
 	and	\$0x80100,%eax
 	cmp	\$0x80100,%eax
 	jne	.Lsqr8x_nox
 
 	call	bn_sqrx8x_internal	# see x86_64-mont5 module
+					# %rax	top-most carry
+					# %rbp	nptr
+					# %rcx	-8*num
+					# %r8	end of tp[2*num]
+	lea	(%r8,%rcx),%rbx
+	mov	%rcx,$num
+	mov	%rcx,%rdx
+	movq	%xmm1,$rptr
+	sar	\$3+2,%rcx		# %cf=0
+	jmp	.Lsqr8x_sub
 
-	pxor	%xmm0,%xmm0
-	lea	48(%rsp),%rax
-	lea	64(%rsp,$num,2),%rdx
-	shr	\$3+2,$num
-	mov	40(%rsp),%rsi		# restore %rsp
-	jmp	.Lsqr8x_zero
-
 .align	32
 .Lsqr8x_nox:
 ___
 $code.=<<___;
 	call	bn_sqr8x_internal	# see x86_64-mont5 module
+					# %rax	top-most carry
+					# %rbp	nptr
+					# %r8	-8*num
+					# %rdi	end of tp[2*num]
+	lea	(%rdi,$num),%rbx
+	mov	$num,%rcx
+	mov	$num,%rdx
+	movq	%xmm1,$rptr
+	sar	\$3+2,%rcx		# %cf=0
+	jmp	.Lsqr8x_sub
 
+.align	32
+.Lsqr8x_sub:
+	mov	8*0(%rbx),%r12
+	mov	8*1(%rbx),%r13
+	mov	8*2(%rbx),%r14
+	mov	8*3(%rbx),%r15
+	lea	8*4(%rbx),%rbx
+	sbb	8*0(%rbp),%r12
+	sbb	8*1(%rbp),%r13
+	sbb	8*2(%rbp),%r14
+	sbb	8*3(%rbp),%r15
+	lea	8*4(%rbp),%rbp
+	mov	%r12,8*0($rptr)
+	mov	%r13,8*1($rptr)
+	mov	%r14,8*2($rptr)
+	mov	%r15,8*3($rptr)
+	lea	8*4($rptr),$rptr
+	inc	%rcx			# preserves %cf
+	jnz	.Lsqr8x_sub
+
+	sbb	\$0,%rax		# top-most carry
+	lea	(%rbx,$num),%rbx	# rewind
+	lea	($rptr,$num),$rptr	# rewind
+
+	movq	%rax,%xmm1
 	pxor	%xmm0,%xmm0
-	lea	48(%rsp),%rax
-	lea	64(%rsp,$num,2),%rdx
-	shr	\$3+2,$num
+	pshufd	\$0,%xmm1,%xmm1
 	mov	40(%rsp),%rsi		# restore %rsp
-	jmp	.Lsqr8x_zero
+	jmp	.Lsqr8x_cond_copy
 
 .align	32
-.Lsqr8x_zero:
-	movdqa	%xmm0,16*0(%rax)	# wipe t
-	movdqa	%xmm0,16*1(%rax)
-	movdqa	%xmm0,16*2(%rax)
-	movdqa	%xmm0,16*3(%rax)
-	lea	16*4(%rax),%rax
-	movdqa	%xmm0,16*0(%rdx)	# wipe n
-	movdqa	%xmm0,16*1(%rdx)
-	movdqa	%xmm0,16*2(%rdx)
-	movdqa	%xmm0,16*3(%rdx)
-	lea	16*4(%rdx),%rdx
-	dec	$num
-	jnz	.Lsqr8x_zero
+.Lsqr8x_cond_copy:
+	movdqa	16*0(%rbx),%xmm2
+	movdqa	16*1(%rbx),%xmm3
+	lea	16*2(%rbx),%rbx
+	movdqu	16*0($rptr),%xmm4
+	movdqu	16*1($rptr),%xmm5
+	lea	16*2($rptr),$rptr
+	movdqa	%xmm0,-16*2(%rbx)	# zero tp
+	movdqa	%xmm0,-16*1(%rbx)
+	movdqa	%xmm0,-16*2(%rbx,%rdx)
+	movdqa	%xmm0,-16*1(%rbx,%rdx)
+	pcmpeqd	%xmm1,%xmm0
+	pand	%xmm1,%xmm2
+	pand	%xmm1,%xmm3
+	pand	%xmm0,%xmm4
+	pand	%xmm0,%xmm5
+	pxor	%xmm0,%xmm0
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+	movdqu	%xmm4,-16*2($rptr)
+	movdqu	%xmm5,-16*1($rptr)
+	add	\$32,$num
+	jnz	.Lsqr8x_cond_copy
 
 	mov	\$1,%rax
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lsqr8x_epilogue:
 	ret
 .size	bn_sqr8x_mont,.-bn_sqr8x_mont
 ___
 }}}
 
 if ($addx) {{{
 my $bp="%rdx";	# original value
 
 $code.=<<___;
 .type	bn_mulx4x_mont,\@function,6
 .align	32
 bn_mulx4x_mont:
 .Lmulx4x_enter:
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 
 	shl	\$3,${num}d		# convert $num to bytes
 	.byte	0x67
 	xor	%r10,%r10
 	sub	$num,%r10		# -$num
 	mov	($n0),$n0		# *n0
 	lea	-72(%rsp,%r10),%rsp	# alloca(frame+$num+8)
 	lea	($bp,$num),%r10
 	and	\$-128,%rsp
 	##############################################################
 	# Stack layout
 	# +0	num
 	# +8	off-loaded &b[i]
 	# +16	end of b[num]
 	# +24	saved n0
 	# +32	saved rp
 	# +40	saved %rsp
 	# +48	inner counter
 	# +56
 	# +64	tmp[num+1]
 	#
 	mov	$num,0(%rsp)		# save $num
 	shr	\$5,$num
 	mov	%r10,16(%rsp)		# end of b[num]
 	sub	\$1,$num
 	mov	$n0, 24(%rsp)		# save *n0
 	mov	$rp, 32(%rsp)		# save $rp
 	mov	%rax,40(%rsp)		# save original %rsp
 	mov	$num,48(%rsp)		# inner counter
 	jmp	.Lmulx4x_body
 
 .align	32
 .Lmulx4x_body:
 ___
 my ($aptr, $bptr, $nptr, $tptr, $mi,  $bi,  $zero, $num)=
    ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
 my $rptr=$bptr;
 $code.=<<___;
 	lea	8($bp),$bptr
 	mov	($bp),%rdx		# b[0], $bp==%rdx actually
 	lea	64+32(%rsp),$tptr
 	mov	%rdx,$bi
 
 	mulx	0*8($aptr),$mi,%rax	# a[0]*b[0]
 	mulx	1*8($aptr),%r11,%r14	# a[1]*b[0]
 	add	%rax,%r11
 	mov	$bptr,8(%rsp)		# off-load &b[i]
 	mulx	2*8($aptr),%r12,%r13	# ...
 	adc	%r14,%r12
 	adc	\$0,%r13
 
 	mov	$mi,$bptr		# borrow $bptr
 	imulq	24(%rsp),$mi		# "t[0]"*n0
 	xor	$zero,$zero		# cf=0, of=0
 
 	mulx	3*8($aptr),%rax,%r14
 	 mov	$mi,%rdx
 	lea	4*8($aptr),$aptr
 	adcx	%rax,%r13
 	adcx	$zero,%r14		# cf=0
 
 	mulx	0*8($nptr),%rax,%r10
 	adcx	%rax,$bptr		# discarded
 	adox	%r11,%r10
 	mulx	1*8($nptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
 	.byte	0xc4,0x62,0xfb,0xf6,0xa1,0x10,0x00,0x00,0x00	# mulx	2*8($nptr),%rax,%r12
 	mov	48(%rsp),$bptr		# counter value
 	mov	%r10,-4*8($tptr)
 	adcx	%rax,%r11
 	adox	%r13,%r12
 	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r11,-3*8($tptr)
 	adcx	%rax,%r12
 	adox	$zero,%r15		# of=0
 	lea	4*8($nptr),$nptr
 	mov	%r12,-2*8($tptr)
 
 	jmp	.Lmulx4x_1st
 
 .align	32
 .Lmulx4x_1st:
 	adcx	$zero,%r15		# cf=0, modulo-scheduled
 	mulx	0*8($aptr),%r10,%rax	# a[4]*b[0]
 	adcx	%r14,%r10
 	mulx	1*8($aptr),%r11,%r14	# a[5]*b[0]
 	adcx	%rax,%r11
 	mulx	2*8($aptr),%r12,%rax	# ...
 	adcx	%r14,%r12
 	mulx	3*8($aptr),%r13,%r14
 	 .byte	0x67,0x67
 	 mov	$mi,%rdx
 	adcx	%rax,%r13
 	adcx	$zero,%r14		# cf=0
 	lea	4*8($aptr),$aptr
 	lea	4*8($tptr),$tptr
 
 	adox	%r15,%r10
 	mulx	0*8($nptr),%rax,%r15
 	adcx	%rax,%r10
 	adox	%r15,%r11
 	mulx	1*8($nptr),%rax,%r15
 	adcx	%rax,%r11
 	adox	%r15,%r12
 	mulx	2*8($nptr),%rax,%r15
 	mov	%r10,-5*8($tptr)
 	adcx	%rax,%r12
 	mov	%r11,-4*8($tptr)
 	adox	%r15,%r13
 	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r12,-3*8($tptr)
 	adcx	%rax,%r13
 	adox	$zero,%r15
 	lea	4*8($nptr),$nptr
 	mov	%r13,-2*8($tptr)
 
 	dec	$bptr			# of=0, pass cf
 	jnz	.Lmulx4x_1st
 
 	mov	0(%rsp),$num		# load num
 	mov	8(%rsp),$bptr		# re-load &b[i]
 	adc	$zero,%r15		# modulo-scheduled
 	add	%r15,%r14
 	sbb	%r15,%r15		# top-most carry
 	mov	%r14,-1*8($tptr)
 	jmp	.Lmulx4x_outer
 
 .align	32
 .Lmulx4x_outer:
 	mov	($bptr),%rdx		# b[i]
 	lea	8($bptr),$bptr		# b++
 	sub	$num,$aptr		# rewind $aptr
 	mov	%r15,($tptr)		# save top-most carry
 	lea	64+4*8(%rsp),$tptr
 	sub	$num,$nptr		# rewind $nptr
 
 	mulx	0*8($aptr),$mi,%r11	# a[0]*b[i]
 	xor	%ebp,%ebp		# xor	$zero,$zero	# cf=0, of=0
 	mov	%rdx,$bi
 	mulx	1*8($aptr),%r14,%r12	# a[1]*b[i]
 	adox	-4*8($tptr),$mi
 	adcx	%r14,%r11
 	mulx	2*8($aptr),%r15,%r13	# ...
 	adox	-3*8($tptr),%r11
 	adcx	%r15,%r12
 	adox	$zero,%r12
 	adcx	$zero,%r13
 
 	mov	$bptr,8(%rsp)		# off-load &b[i]
 	.byte	0x67
 	mov	$mi,%r15
 	imulq	24(%rsp),$mi		# "t[0]"*n0
 	xor	%ebp,%ebp		# xor	$zero,$zero	# cf=0, of=0
 
 	mulx	3*8($aptr),%rax,%r14
 	 mov	$mi,%rdx
 	adox	-2*8($tptr),%r12
 	adcx	%rax,%r13
 	adox	-1*8($tptr),%r13
 	adcx	$zero,%r14
 	lea	4*8($aptr),$aptr
 	adox	$zero,%r14
 
 	mulx	0*8($nptr),%rax,%r10
 	adcx	%rax,%r15		# discarded
 	adox	%r11,%r10
 	mulx	1*8($nptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
 	mulx	2*8($nptr),%rax,%r12
 	mov	%r10,-4*8($tptr)
 	adcx	%rax,%r11
 	adox	%r13,%r12
 	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r11,-3*8($tptr)
 	lea	4*8($nptr),$nptr
 	adcx	%rax,%r12
 	adox	$zero,%r15		# of=0
 	mov	48(%rsp),$bptr		# counter value
 	mov	%r12,-2*8($tptr)
 
 	jmp	.Lmulx4x_inner
 
 .align	32
 .Lmulx4x_inner:
 	mulx	0*8($aptr),%r10,%rax	# a[4]*b[i]
 	adcx	$zero,%r15		# cf=0, modulo-scheduled
 	adox	%r14,%r10
 	mulx	1*8($aptr),%r11,%r14	# a[5]*b[i]
 	adcx	0*8($tptr),%r10
 	adox	%rax,%r11
 	mulx	2*8($aptr),%r12,%rax	# ...
 	adcx	1*8($tptr),%r11
 	adox	%r14,%r12
 	mulx	3*8($aptr),%r13,%r14
 	 mov	$mi,%rdx
 	adcx	2*8($tptr),%r12
 	adox	%rax,%r13
 	adcx	3*8($tptr),%r13
 	adox	$zero,%r14		# of=0
 	lea	4*8($aptr),$aptr
 	lea	4*8($tptr),$tptr
 	adcx	$zero,%r14		# cf=0
 
 	adox	%r15,%r10
 	mulx	0*8($nptr),%rax,%r15
 	adcx	%rax,%r10
 	adox	%r15,%r11
 	mulx	1*8($nptr),%rax,%r15
 	adcx	%rax,%r11
 	adox	%r15,%r12
 	mulx	2*8($nptr),%rax,%r15
 	mov	%r10,-5*8($tptr)
 	adcx	%rax,%r12
 	adox	%r15,%r13
 	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r11,-4*8($tptr)
 	mov	%r12,-3*8($tptr)
 	adcx	%rax,%r13
 	adox	$zero,%r15
 	lea	4*8($nptr),$nptr
 	mov	%r13,-2*8($tptr)
 
 	dec	$bptr			# of=0, pass cf
 	jnz	.Lmulx4x_inner
 
 	mov	0(%rsp),$num		# load num
 	mov	8(%rsp),$bptr		# re-load &b[i]
 	adc	$zero,%r15		# modulo-scheduled
 	sub	0*8($tptr),$zero	# pull top-most carry
 	adc	%r15,%r14
-	mov	-8($nptr),$mi
 	sbb	%r15,%r15		# top-most carry
 	mov	%r14,-1*8($tptr)
 
 	cmp	16(%rsp),$bptr
 	jne	.Lmulx4x_outer
 
-	sub	%r14,$mi		# compare top-most words
-	sbb	$mi,$mi
-	or	$mi,%r15
-
-	neg	$num
-	xor	%rdx,%rdx
-	mov	32(%rsp),$rptr		# restore rp
 	lea	64(%rsp),$tptr
+	sub	$num,$nptr		# rewind $nptr
+	neg	%r15
+	mov	$num,%rdx
+	shr	\$3+2,$num		# %cf=0
+	mov	32(%rsp),$rptr		# restore rp
+	jmp	.Lmulx4x_sub
 
-	pxor	%xmm0,%xmm0
-	mov	0*8($nptr,$num),%r8
-	mov	1*8($nptr,$num),%r9
-	neg	%r8
-	jmp	.Lmulx4x_sub_entry
-
 .align	32
 .Lmulx4x_sub:
-	mov	0*8($nptr,$num),%r8
-	mov	1*8($nptr,$num),%r9
-	not	%r8
-.Lmulx4x_sub_entry:
-	mov	2*8($nptr,$num),%r10
-	not	%r9
-	and	%r15,%r8
-	mov	3*8($nptr,$num),%r11
-	not	%r10
-	and	%r15,%r9
-	not	%r11
-	and	%r15,%r10
-	and	%r15,%r11
+	mov	8*0($tptr),%r11
+	mov	8*1($tptr),%r12
+	mov	8*2($tptr),%r13
+	mov	8*3($tptr),%r14
+	lea	8*4($tptr),$tptr
+	sbb	8*0($nptr),%r11
+	sbb	8*1($nptr),%r12
+	sbb	8*2($nptr),%r13
+	sbb	8*3($nptr),%r14
+	lea	8*4($nptr),$nptr
+	mov	%r11,8*0($rptr)
+	mov	%r12,8*1($rptr)
+	mov	%r13,8*2($rptr)
+	mov	%r14,8*3($rptr)
+	lea	8*4($rptr),$rptr
+	dec	$num			# preserves %cf
+	jnz	.Lmulx4x_sub
 
-	neg	%rdx			# mov %rdx,%cf
-	adc	0*8($tptr),%r8
-	adc	1*8($tptr),%r9
-	movdqa	%xmm0,($tptr)
-	adc	2*8($tptr),%r10
-	adc	3*8($tptr),%r11
-	movdqa	%xmm0,16($tptr)
-	lea	4*8($tptr),$tptr
-	sbb	%rdx,%rdx		# mov %cf,%rdx
+	sbb	\$0,%r15		# top-most carry
+	lea	64(%rsp),$tptr
+	sub	%rdx,$rptr		# rewind
 
-	mov	%r8,0*8($rptr)
-	mov	%r9,1*8($rptr)
-	mov	%r10,2*8($rptr)
-	mov	%r11,3*8($rptr)
-	lea	4*8($rptr),$rptr
+	movq	%r15,%xmm1
+	pxor	%xmm0,%xmm0
+	pshufd	\$0,%xmm1,%xmm1
+	mov	40(%rsp),%rsi		# restore %rsp
+	jmp	.Lmulx4x_cond_copy
 
-	add	\$32,$num
-	jnz	.Lmulx4x_sub
+.align	32
+.Lmulx4x_cond_copy:
+	movdqa	16*0($tptr),%xmm2
+	movdqa	16*1($tptr),%xmm3
+	lea	16*2($tptr),$tptr
+	movdqu	16*0($rptr),%xmm4
+	movdqu	16*1($rptr),%xmm5
+	lea	16*2($rptr),$rptr
+	movdqa	%xmm0,-16*2($tptr)	# zero tp
+	movdqa	%xmm0,-16*1($tptr)
+	pcmpeqd	%xmm1,%xmm0
+	pand	%xmm1,%xmm2
+	pand	%xmm1,%xmm3
+	pand	%xmm0,%xmm4
+	pand	%xmm0,%xmm5
+	pxor	%xmm0,%xmm0
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+	movdqu	%xmm4,-16*2($rptr)
+	movdqu	%xmm5,-16*1($rptr)
+	sub	\$32,%rdx
+	jnz	.Lmulx4x_cond_copy
 
-	mov	40(%rsp),%rsi		# restore %rsp
+	mov	%rdx,($tptr)
+
 	mov	\$1,%rax
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lmulx4x_epilogue:
 	ret
 .size	bn_mulx4x_mont,.-bn_mulx4x_mont
 ___
 }}}
 $code.=<<___;
 .asciz	"Montgomery Multiplication for x86_64, CRYPTOGAMS by "
 .align	16
 ___
 
 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
 #		CONTEXT *context,DISPATCHER_CONTEXT *disp)
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___;
 .extern	__imp_RtlVirtualUnwind
 .type	mul_handler,\@abi-omnipotent
 .align	16
 mul_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# end of prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lcommon_seh_tail
 
 	mov	192($context),%r10	# pull $num
 	mov	8(%rax,%r10,8),%rax	# pull saved stack pointer
 	lea	48(%rax),%rax
 
 	mov	-8(%rax),%rbx
 	mov	-16(%rax),%rbp
 	mov	-24(%rax),%r12
 	mov	-32(%rax),%r13
 	mov	-40(%rax),%r14
 	mov	-48(%rax),%r15
 	mov	%rbx,144($context)	# restore context->Rbx
 	mov	%rbp,160($context)	# restore context->Rbp
 	mov	%r12,216($context)	# restore context->R12
 	mov	%r13,224($context)	# restore context->R13
 	mov	%r14,232($context)	# restore context->R14
 	mov	%r15,240($context)	# restore context->R15
 
 	jmp	.Lcommon_seh_tail
 .size	mul_handler,.-mul_handler
 
 .type	sqr_handler,\@abi-omnipotent
 .align	16
 sqr_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# end of prologue label
 	cmp	%r10,%rbx		# context->Rip<.Lsqr_body
 	jb	.Lcommon_seh_tail
 
 	mov	152($context),%rax	# pull context->Rsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=.Lsqr_epilogue
 	jae	.Lcommon_seh_tail
 
 	mov	40(%rax),%rax		# pull saved stack pointer
 
 	mov	-8(%rax),%rbx
 	mov	-16(%rax),%rbp
 	mov	-24(%rax),%r12
 	mov	-32(%rax),%r13
 	mov	-40(%rax),%r14
 	mov	-48(%rax),%r15
 	mov	%rbx,144($context)	# restore context->Rbx
 	mov	%rbp,160($context)	# restore context->Rbp
 	mov	%r12,216($context)	# restore context->R12
 	mov	%r13,224($context)	# restore context->R13
 	mov	%r14,232($context)	# restore context->R14
 	mov	%r15,240($context)	# restore context->R15
 
 .Lcommon_seh_tail:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$154,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
 .size	sqr_handler,.-sqr_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_bn_mul_mont
 	.rva	.LSEH_end_bn_mul_mont
 	.rva	.LSEH_info_bn_mul_mont
 
 	.rva	.LSEH_begin_bn_mul4x_mont
 	.rva	.LSEH_end_bn_mul4x_mont
 	.rva	.LSEH_info_bn_mul4x_mont
 
 	.rva	.LSEH_begin_bn_sqr8x_mont
 	.rva	.LSEH_end_bn_sqr8x_mont
 	.rva	.LSEH_info_bn_sqr8x_mont
 ___
 $code.=<<___ if ($addx);
 	.rva	.LSEH_begin_bn_mulx4x_mont
 	.rva	.LSEH_end_bn_mulx4x_mont
 	.rva	.LSEH_info_bn_mulx4x_mont
 ___
 $code.=<<___;
 .section	.xdata
 .align	8
 .LSEH_info_bn_mul_mont:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lmul_body,.Lmul_epilogue	# HandlerData[]
 .LSEH_info_bn_mul4x_mont:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lmul4x_body,.Lmul4x_epilogue	# HandlerData[]
 .LSEH_info_bn_sqr8x_mont:
 	.byte	9,0,0,0
 	.rva	sqr_handler
 	.rva	.Lsqr8x_body,.Lsqr8x_epilogue	# HandlerData[]
 ___
 $code.=<<___ if ($addx);
 .LSEH_info_bn_mulx4x_mont:
 	.byte	9,0,0,0
 	.rva	sqr_handler
 	.rva	.Lmulx4x_body,.Lmulx4x_epilogue	# HandlerData[]
 ___
 }
 
 print $code;
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont5.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont5.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/asm/x86_64-mont5.pl	(revision 296273)
@@ -1,3540 +1,3702 @@
 #!/usr/bin/env perl
 
 # ====================================================================
 # Written by Andy Polyakov  for the OpenSSL
 # project. The module is, however, dual licensed under OpenSSL and
 # CRYPTOGAMS licenses depending on where you obtain it. For further
 # details see http://www.openssl.org/~appro/cryptogams/.
 # ====================================================================
 
 # August 2011.
 #
 # Companion to x86_64-mont.pl that optimizes cache-timing attack
 # countermeasures. The subroutines are produced by replacing bp[i]
 # references in their x86_64-mont.pl counterparts with cache-neutral
 # references to powers table computed in BN_mod_exp_mont_consttime.
 # In addition subroutine that scatters elements of the powers table
 # is implemented, so that scatter-/gathering can be tuned without
 # bn_exp.c modifications.
 
 # August 2013.
 #
 # Add MULX/AD*X code paths and additional interfaces to optimize for
 # branch prediction unit. For input lengths that are multiples of 8
 # the np argument is not just modulus value, but one interleaved
 # with 0. This is to optimize post-condition...
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.23);
 }
 
 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$addx = ($1>=2.10);
 }
 
 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$addx = ($1>=12);
 }
 
 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
 	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
 	$addx = ($ver>=3.03);
 }
 
 # int bn_mul_mont_gather5(
 $rp="%rdi";	# BN_ULONG *rp,
 $ap="%rsi";	# const BN_ULONG *ap,
 $bp="%rdx";	# const BN_ULONG *bp,
 $np="%rcx";	# const BN_ULONG *np,
 $n0="%r8";	# const BN_ULONG *n0,
 $num="%r9";	# int num,
 		# int idx);	# 0 to 2^5-1, "index" in $bp holding
 				# pre-computed powers of a', interlaced
 				# in such manner that b[0] is $bp[idx],
 				# b[1] is [2^5+idx], etc.
 $lo0="%r10";
 $hi0="%r11";
 $hi1="%r13";
 $i="%r14";
 $j="%r15";
 $m0="%rbx";
 $m1="%rbp";
 
 $code=<<___;
 .text
 
 .extern	OPENSSL_ia32cap_P
 
 .globl	bn_mul_mont_gather5
 .type	bn_mul_mont_gather5,\@function,6
 .align	64
 bn_mul_mont_gather5:
 	test	\$7,${num}d
 	jnz	.Lmul_enter
 ___
 $code.=<<___ if ($addx);
 	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
 ___
 $code.=<<___;
 	jmp	.Lmul4x_enter
 
 .align	16
 .Lmul_enter:
 	mov	${num}d,${num}d
 	mov	%rsp,%rax
-	mov	`($win64?56:8)`(%rsp),%r10d	# load 7th argument
+	movd	`($win64?56:8)`(%rsp),%xmm5	# load 7th argument
+	lea	.Linc(%rip),%r10
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
+
 	lea	2($num),%r11
 	neg	%r11
-	lea	(%rsp,%r11,8),%rsp	# tp=alloca(8*(num+2))
+	lea	-264(%rsp,%r11,8),%rsp	# tp=alloca(8*(num+2)+256+8)
 	and	\$-1024,%rsp		# minimize TLB usage
 
 	mov	%rax,8(%rsp,$num,8)	# tp[num+1]=%rsp
 .Lmul_body:
-	mov	$bp,%r12		# reassign $bp
+	lea	128($bp),%r12		# reassign $bp (+size optimization)
 ___
 		$bp="%r12";
 		$STRIDE=2**5*8;		# 5 is "window size"
 		$N=$STRIDE/4;		# should match cache line size
 $code.=<<___;
-	mov	%r10,%r11
-	shr	\$`log($N/8)/log(2)`,%r10
-	and	\$`$N/8-1`,%r11
-	not	%r10
-	lea	.Lmagic_masks(%rip),%rax
-	and	\$`2**5/($N/8)-1`,%r10	# 5 is "window size"
-	lea	96($bp,%r11,8),$bp	# pointer within 1st cache line
-	movq	0(%rax,%r10,8),%xmm4	# set of masks denoting which
-	movq	8(%rax,%r10,8),%xmm5	# cache line contains element
-	movq	16(%rax,%r10,8),%xmm6	# denoted by 7th argument
-	movq	24(%rax,%r10,8),%xmm7
+	movdqa	0(%r10),%xmm0		# 00000001000000010000000000000000
+	movdqa	16(%r10),%xmm1		# 00000002000000020000000200000002
+	lea	24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
+	and	\$-16,%r10
 
-	movq	`0*$STRIDE/4-96`($bp),%xmm0
-	movq	`1*$STRIDE/4-96`($bp),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bp),%xmm2
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-96`($bp),%xmm3
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
+	pshufd	\$0,%xmm5,%xmm5		# broadcast index
+	movdqa	%xmm1,%xmm4
+	movdqa	%xmm1,%xmm2
+___
+########################################################################
+# calculate mask by comparing 0..31 to index and save result to stack
+#
+$code.=<<___;
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
+	.byte	0x67
+	movdqa	%xmm4,%xmm3
+___
+for($k=0;$k<$STRIDE/16-4;$k+=4) {
+$code.=<<___;
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
+	movdqa	%xmm0,`16*($k+0)+112`(%r10)
+	movdqa	%xmm4,%xmm0
+
+	paddd	%xmm2,%xmm3
+	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
+	movdqa	%xmm1,`16*($k+1)+112`(%r10)
+	movdqa	%xmm4,%xmm1
+
+	paddd	%xmm3,%xmm0
+	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
+	movdqa	%xmm2,`16*($k+2)+112`(%r10)
+	movdqa	%xmm4,%xmm2
+
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0
+	movdqa	%xmm3,`16*($k+3)+112`(%r10)
+	movdqa	%xmm4,%xmm3
+___
+}
+$code.=<<___;				# last iteration can be optimized
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1
+	movdqa	%xmm0,`16*($k+0)+112`(%r10)
+
+	paddd	%xmm2,%xmm3
+	.byte	0x67
+	pcmpeqd	%xmm5,%xmm2
+	movdqa	%xmm1,`16*($k+1)+112`(%r10)
+
+	pcmpeqd	%xmm5,%xmm3
+	movdqa	%xmm2,`16*($k+2)+112`(%r10)
+	pand	`16*($k+0)-128`($bp),%xmm0	# while it's still in register
+
+	pand	`16*($k+1)-128`($bp),%xmm1
+	pand	`16*($k+2)-128`($bp),%xmm2
+	movdqa	%xmm3,`16*($k+3)+112`(%r10)
+	pand	`16*($k+3)-128`($bp),%xmm3
 	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+for($k=0;$k<$STRIDE/16-4;$k+=4) {
+$code.=<<___;
+	movdqa	`16*($k+0)-128`($bp),%xmm4
+	movdqa	`16*($k+1)-128`($bp),%xmm5
+	movdqa	`16*($k+2)-128`($bp),%xmm2
+	pand	`16*($k+0)+112`(%r10),%xmm4
+	movdqa	`16*($k+3)-128`($bp),%xmm3
+	pand	`16*($k+1)+112`(%r10),%xmm5
+	por	%xmm4,%xmm0
+	pand	`16*($k+2)+112`(%r10),%xmm2
+	por	%xmm5,%xmm1
+	pand	`16*($k+3)+112`(%r10),%xmm3
+	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+}
+$code.=<<___;
+	por	%xmm1,%xmm0
+	pshufd	\$0x4e,%xmm0,%xmm1
+	por	%xmm1,%xmm0
 	lea	$STRIDE($bp),$bp
-	por	%xmm3,%xmm0
-
 	movq	%xmm0,$m0		# m0=bp[0]
 
 	mov	($n0),$n0		# pull n0[0] value
 	mov	($ap),%rax
 
 	xor	$i,$i			# i=0
 	xor	$j,$j			# j=0
 
-	movq	`0*$STRIDE/4-96`($bp),%xmm0
-	movq	`1*$STRIDE/4-96`($bp),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bp),%xmm2
-	pand	%xmm5,%xmm1
-
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[0]
 	mov	%rax,$lo0
 	mov	($np),%rax
 
-	movq	`3*$STRIDE/4-96`($bp),%xmm3
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
-
 	imulq	$lo0,$m1		# "tp[0]"*n0
 	mov	%rdx,$hi0
 
-	por	%xmm2,%xmm0
-	lea	$STRIDE($bp),$bp
-	por	%xmm3,%xmm0
-
 	mulq	$m1			# np[0]*m1
 	add	%rax,$lo0		# discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$hi1
 
 	lea	1($j),$j		# j++
 	jmp	.L1st_enter
 
 .align	16
 .L1st:
 	add	%rax,$hi1
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
 	mov	$lo0,$hi0
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 
 .L1st_enter:
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$hi0
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	lea	1($j),$j		# j++
 	mov	%rdx,$lo0
 
 	mulq	$m1			# np[j]*m1
 	cmp	$num,$j
-	jne	.L1st
+	jne	.L1st			# note that upon exit $j==$num, so
+					# they can be used interchangeably
 
-	movq	%xmm0,$m0		# bp[1]
-
 	add	%rax,$hi1
-	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
-	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
+	mov	$hi1,-16(%rsp,$num,8)	# tp[num-1]
 	mov	%rdx,$hi1
 	mov	$lo0,$hi0
 
 	xor	%rdx,%rdx
 	add	$hi0,$hi1
 	adc	\$0,%rdx
 	mov	$hi1,-8(%rsp,$num,8)
 	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
 
 	lea	1($i),$i		# i++
 	jmp	.Louter
 .align	16
 .Louter:
+	lea	24+128(%rsp,$num,8),%rdx	# where 256-byte mask is (+size optimization)
+	and	\$-16,%rdx
+	pxor	%xmm4,%xmm4
+	pxor	%xmm5,%xmm5
+___
+for($k=0;$k<$STRIDE/16;$k+=4) {
+$code.=<<___;
+	movdqa	`16*($k+0)-128`($bp),%xmm0
+	movdqa	`16*($k+1)-128`($bp),%xmm1
+	movdqa	`16*($k+2)-128`($bp),%xmm2
+	movdqa	`16*($k+3)-128`($bp),%xmm3
+	pand	`16*($k+0)-128`(%rdx),%xmm0
+	pand	`16*($k+1)-128`(%rdx),%xmm1
+	por	%xmm0,%xmm4
+	pand	`16*($k+2)-128`(%rdx),%xmm2
+	por	%xmm1,%xmm5
+	pand	`16*($k+3)-128`(%rdx),%xmm3
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+___
+}
+$code.=<<___;
+	por	%xmm5,%xmm4
+	pshufd	\$0x4e,%xmm4,%xmm0
+	por	%xmm4,%xmm0
+	lea	$STRIDE($bp),$bp
+
+	mov	($ap),%rax		# ap[0]
+	movq	%xmm0,$m0		# m0=bp[i]
+
 	xor	$j,$j			# j=0
 	mov	$n0,$m1
 	mov	(%rsp),$lo0
 
-	movq	`0*$STRIDE/4-96`($bp),%xmm0
-	movq	`1*$STRIDE/4-96`($bp),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bp),%xmm2
-	pand	%xmm5,%xmm1
-
 	mulq	$m0			# ap[0]*bp[i]
 	add	%rax,$lo0		# ap[0]*bp[i]+tp[0]
 	mov	($np),%rax
 	adc	\$0,%rdx
 
-	movq	`3*$STRIDE/4-96`($bp),%xmm3
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
-
 	imulq	$lo0,$m1		# tp[0]*n0
 	mov	%rdx,$hi0
 
-	por	%xmm2,%xmm0
-	lea	$STRIDE($bp),$bp
-	por	%xmm3,%xmm0
-
 	mulq	$m1			# np[0]*m1
 	add	%rax,$lo0		# discarded
 	mov	8($ap),%rax
 	adc	\$0,%rdx
 	mov	8(%rsp),$lo0		# tp[1]
 	mov	%rdx,$hi1
 
 	lea	1($j),$j		# j++
 	jmp	.Linner_enter
 
 .align	16
 .Linner:
 	add	%rax,$hi1
 	mov	($ap,$j,8),%rax
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
 	mov	(%rsp,$j,8),$lo0
 	adc	\$0,%rdx
 	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
 	mov	%rdx,$hi1
 
 .Linner_enter:
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$hi0
 	mov	($np,$j,8),%rax
 	adc	\$0,%rdx
 	add	$hi0,$lo0		# ap[j]*bp[i]+tp[j]
 	mov	%rdx,$hi0
 	adc	\$0,$hi0
 	lea	1($j),$j		# j++
 
 	mulq	$m1			# np[j]*m1
 	cmp	$num,$j
-	jne	.Linner
-
-	movq	%xmm0,$m0		# bp[i+1]
-
+	jne	.Linner			# note that upon exit $j==$num, so
+					# they can be used interchangeably
 	add	%rax,$hi1
-	mov	($ap),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
-	mov	(%rsp,$j,8),$lo0
+	mov	(%rsp,$num,8),$lo0
 	adc	\$0,%rdx
-	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
+	mov	$hi1,-16(%rsp,$num,8)	# tp[num-1]
 	mov	%rdx,$hi1
 
 	xor	%rdx,%rdx
 	add	$hi0,$hi1
 	adc	\$0,%rdx
 	add	$lo0,$hi1		# pull upmost overflow bit
 	adc	\$0,%rdx
 	mov	$hi1,-8(%rsp,$num,8)
 	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
 
 	lea	1($i),$i		# i++
 	cmp	$num,$i
 	jb	.Louter
 
 	xor	$i,$i			# i=0 and clear CF!
 	mov	(%rsp),%rax		# tp[0]
 	lea	(%rsp),$ap		# borrow ap for tp
 	mov	$num,$j			# j=num
 	jmp	.Lsub
 .align	16
 .Lsub:	sbb	($np,$i,8),%rax
 	mov	%rax,($rp,$i,8)		# rp[i]=tp[i]-np[i]
 	mov	8($ap,$i,8),%rax	# tp[i+1]
 	lea	1($i),$i		# i++
 	dec	$j			# doesnn't affect CF!
 	jnz	.Lsub
 
 	sbb	\$0,%rax		# handle upmost overflow bit
 	xor	$i,$i
 	and	%rax,$ap
 	not	%rax
 	mov	$rp,$np
 	and	%rax,$np
 	mov	$num,$j			# j=num
 	or	$np,$ap			# ap=borrow?tp:rp
 .align	16
 .Lcopy:					# copy or in-place refresh
 	mov	($ap,$i,8),%rax
 	mov	$i,(%rsp,$i,8)		# zap temporary vector
 	mov	%rax,($rp,$i,8)		# rp[i]=tp[i]
 	lea	1($i),$i
 	sub	\$1,$j
 	jnz	.Lcopy
 
 	mov	8(%rsp,$num,8),%rsi	# restore %rsp
 	mov	\$1,%rax
-___
-$code.=<<___ if ($win64);
-	movaps	-88(%rsi),%xmm6
-	movaps	-72(%rsi),%xmm7
-___
-$code.=<<___;
+
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lmul_epilogue:
 	ret
 .size	bn_mul_mont_gather5,.-bn_mul_mont_gather5
 ___
 {{{
 my @A=("%r10","%r11");
 my @N=("%r13","%rdi");
 $code.=<<___;
 .type	bn_mul4x_mont_gather5,\@function,6
 .align	32
 bn_mul4x_mont_gather5:
 .Lmul4x_enter:
 ___
 $code.=<<___ if ($addx);
-	and	\$0x80100,%r11d
-	cmp	\$0x80100,%r11d
+	and	\$0x80108,%r11d
+	cmp	\$0x80108,%r11d		# check for AD*X+BMI2+BMI1
 	je	.Lmulx4x_enter
 ___
 $code.=<<___;
 	.byte	0x67
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
+
 	.byte	0x67
-	mov	${num}d,%r10d
-	shl	\$3,${num}d
-	shl	\$3+2,%r10d		# 4*$num
+	shl	\$3,${num}d		# convert $num to bytes
+	lea	($num,$num,2),%r10	# 3*$num in bytes
 	neg	$num			# -$num
 
 	##############################################################
-	# ensure that stack frame doesn't alias with $aptr+4*$num
-	# modulo 4096, which covers ret[num], am[num] and n[2*num]
-	# (see bn_exp.c). this is done to allow memory disambiguation
-	# logic do its magic. [excessive frame is allocated in order
-	# to allow bn_from_mont8x to clear it.]
+	# Ensure that stack frame doesn't alias with $rptr+3*$num
+	# modulo 4096, which covers ret[num], am[num] and n[num]
+	# (see bn_exp.c). This is done to allow memory disambiguation
+	# logic do its magic. [Extra [num] is allocated in order
+	# to align with bn_power5's frame, which is cleansed after
+	# completing exponentiation. Extra 256 bytes is for power mask
+	# calculated from 7th argument, the index.]
 	#
-	lea	-64(%rsp,$num,2),%r11
-	sub	$ap,%r11
+	lea	-320(%rsp,$num,2),%r11
+	sub	$rp,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lmul4xsp_alt
-	sub	%r11,%rsp		# align with $ap
-	lea	-64(%rsp,$num,2),%rsp	# alloca(128+num*8)
+	sub	%r11,%rsp		# align with $rp
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*num*8+256)
 	jmp	.Lmul4xsp_done
 
 .align	32
 .Lmul4xsp_alt:
-	lea	4096-64(,$num,2),%r10
-	lea	-64(%rsp,$num,2),%rsp	# alloca(128+num*8)
+	lea	4096-320(,$num,2),%r10
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*num*8+256)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lmul4xsp_done:
 	and	\$-64,%rsp
 	neg	$num
 
 	mov	%rax,40(%rsp)
 .Lmul4x_body:
 
 	call	mul4x_internal
 
 	mov	40(%rsp),%rsi		# restore %rsp
 	mov	\$1,%rax
-___
-$code.=<<___ if ($win64);
-	movaps	-88(%rsi),%xmm6
-	movaps	-72(%rsi),%xmm7
-___
-$code.=<<___;
+
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lmul4x_epilogue:
 	ret
 .size	bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
 
 .type	mul4x_internal,\@abi-omnipotent
 .align	32
 mul4x_internal:
-	shl	\$5,$num
-	mov	`($win64?56:8)`(%rax),%r10d	# load 7th argument
-	lea	256(%rdx,$num),%r13
+	shl	\$5,$num		# $num was in bytes
+	movd	`($win64?56:8)`(%rax),%xmm5	# load 7th argument, index
+	lea	.Linc(%rip),%rax
+	lea	128(%rdx,$num),%r13	# end of powers table (+size optimization)
 	shr	\$5,$num		# restore $num
 ___
 		$bp="%r12";
 		$STRIDE=2**5*8;		# 5 is "window size"
 		$N=$STRIDE/4;		# should match cache line size
 		$tp=$i;
 $code.=<<___;
-	mov	%r10,%r11
-	shr	\$`log($N/8)/log(2)`,%r10
-	and	\$`$N/8-1`,%r11
-	not	%r10
-	lea	.Lmagic_masks(%rip),%rax
-	and	\$`2**5/($N/8)-1`,%r10	# 5 is "window size"
-	lea	96(%rdx,%r11,8),$bp	# pointer within 1st cache line
-	movq	0(%rax,%r10,8),%xmm4	# set of masks denoting which
-	movq	8(%rax,%r10,8),%xmm5	# cache line contains element
-	add	\$7,%r11
-	movq	16(%rax,%r10,8),%xmm6	# denoted by 7th argument
-	movq	24(%rax,%r10,8),%xmm7
-	and	\$7,%r11
+	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
+	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
+	lea	88-112(%rsp,$num),%r10	# place the mask after tp[num+1] (+ICache optimization)
+	lea	128(%rdx),$bp		# size optimization
 
-	movq	`0*$STRIDE/4-96`($bp),%xmm0
-	lea	$STRIDE($bp),$tp	# borrow $tp
-	movq	`1*$STRIDE/4-96`($bp),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bp),%xmm2
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-96`($bp),%xmm3
-	pand	%xmm6,%xmm2
+	pshufd	\$0,%xmm5,%xmm5		# broadcast index
+	movdqa	%xmm1,%xmm4
+	.byte	0x67,0x67
+	movdqa	%xmm1,%xmm2
+___
+########################################################################
+# calculate mask by comparing 0..31 to index and save result to stack
+#
+$code.=<<___;
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
 	.byte	0x67
-	por	%xmm1,%xmm0
-	movq	`0*$STRIDE/4-96`($tp),%xmm1
+	movdqa	%xmm4,%xmm3
+___
+for($i=0;$i<$STRIDE/16-4;$i+=4) {
+$code.=<<___;
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
+	movdqa	%xmm0,`16*($i+0)+112`(%r10)
+	movdqa	%xmm4,%xmm0
+
+	paddd	%xmm2,%xmm3
+	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
+	movdqa	%xmm1,`16*($i+1)+112`(%r10)
+	movdqa	%xmm4,%xmm1
+
+	paddd	%xmm3,%xmm0
+	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
+	movdqa	%xmm2,`16*($i+2)+112`(%r10)
+	movdqa	%xmm4,%xmm2
+
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0
+	movdqa	%xmm3,`16*($i+3)+112`(%r10)
+	movdqa	%xmm4,%xmm3
+___
+}
+$code.=<<___;				# last iteration can be optimized
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1
+	movdqa	%xmm0,`16*($i+0)+112`(%r10)
+
+	paddd	%xmm2,%xmm3
 	.byte	0x67
-	pand	%xmm7,%xmm3
-	.byte	0x67
-	por	%xmm2,%xmm0
-	movq	`1*$STRIDE/4-96`($tp),%xmm2
-	.byte	0x67
-	pand	%xmm4,%xmm1
-	.byte	0x67
-	por	%xmm3,%xmm0
-	movq	`2*$STRIDE/4-96`($tp),%xmm3
+	pcmpeqd	%xmm5,%xmm2
+	movdqa	%xmm1,`16*($i+1)+112`(%r10)
 
+	pcmpeqd	%xmm5,%xmm3
+	movdqa	%xmm2,`16*($i+2)+112`(%r10)
+	pand	`16*($i+0)-128`($bp),%xmm0	# while it's still in register
+
+	pand	`16*($i+1)-128`($bp),%xmm1
+	pand	`16*($i+2)-128`($bp),%xmm2
+	movdqa	%xmm3,`16*($i+3)+112`(%r10)
+	pand	`16*($i+3)-128`($bp),%xmm3
+	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+for($i=0;$i<$STRIDE/16-4;$i+=4) {
+$code.=<<___;
+	movdqa	`16*($i+0)-128`($bp),%xmm4
+	movdqa	`16*($i+1)-128`($bp),%xmm5
+	movdqa	`16*($i+2)-128`($bp),%xmm2
+	pand	`16*($i+0)+112`(%r10),%xmm4
+	movdqa	`16*($i+3)-128`($bp),%xmm3
+	pand	`16*($i+1)+112`(%r10),%xmm5
+	por	%xmm4,%xmm0
+	pand	`16*($i+2)+112`(%r10),%xmm2
+	por	%xmm5,%xmm1
+	pand	`16*($i+3)+112`(%r10),%xmm3
+	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+}
+$code.=<<___;
+	por	%xmm1,%xmm0
+	pshufd	\$0x4e,%xmm0,%xmm1
+	por	%xmm1,%xmm0
+	lea	$STRIDE($bp),$bp
 	movq	%xmm0,$m0		# m0=bp[0]
-	movq	`3*$STRIDE/4-96`($tp),%xmm0
+
 	mov	%r13,16+8(%rsp)		# save end of b[num]
 	mov	$rp, 56+8(%rsp)		# save $rp
 
 	mov	($n0),$n0		# pull n0[0] value
 	mov	($ap),%rax
 	lea	($ap,$num),$ap		# end of a[num]
 	neg	$num
 
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[0]
 	mov	%rax,$A[0]
 	mov	($np),%rax
 
-	pand	%xmm5,%xmm2
-	pand	%xmm6,%xmm3
-	por	%xmm2,%xmm1
-
 	imulq	$A[0],$m1		# "tp[0]"*n0
-	##############################################################
-	# $tp is chosen so that writing to top-most element of the
-	# vector occurs just "above" references to powers table,
-	# "above" modulo cache-line size, which effectively precludes
-	# possibility of memory disambiguation logic failure when
-	# accessing the table.
-	# 
-	lea	64+8(%rsp,%r11,8),$tp
+	lea	64+8(%rsp),$tp
 	mov	%rdx,$A[1]
 
-	pand	%xmm7,%xmm0
-	por	%xmm3,%xmm1
-	lea	2*$STRIDE($bp),$bp
-	por	%xmm1,%xmm0
-
 	mulq	$m1			# np[0]*m1
 	add	%rax,$A[0]		# discarded
 	mov	8($ap,$num),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$N[1]
 
 	mulq	$m0
 	add	%rax,$A[1]
-	mov	16*1($np),%rax		# interleaved with 0, therefore 16*n
+	mov	8*1($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1
 	add	%rax,$N[1]
 	mov	16($ap,$num),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	lea	4*8($num),$j		# j=4
-	lea	16*4($np),$np
+	lea	8*4($np),$np
 	adc	\$0,%rdx
 	mov	$N[1],($tp)
 	mov	%rdx,$N[0]
 	jmp	.L1st4x
 
 .align	32
 .L1st4x:
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
-	mov	-16*2($np),%rax
+	mov	-8*2($np),%rax
 	lea	32($tp),$tp
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
-	mov	-16*1($np),%rax
+	mov	-8*1($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[1],-16($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
-	mov	16*0($np),%rax
+	mov	8*0($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	8($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-8($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
-	mov	16*1($np),%rax
+	mov	8*1($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	16($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
-	lea	16*4($np),$np
+	lea	8*4($np),$np
 	adc	\$0,%rdx
 	mov	$N[1],($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
 	add	\$32,$j			# j+=4
 	jnz	.L1st4x
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[0]
-	mov	-16*2($np),%rax
+	mov	-8*2($np),%rax
 	lea	32($tp),$tp
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[0],-24($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[0]
 	add	%rax,$A[1]
-	mov	-16*1($np),%rax
+	mov	-8*1($np),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$num),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
 	adc	\$0,%rdx
 	mov	$N[1],-16($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
-	movq	%xmm0,$m0		# bp[1]
-	lea	($np,$num,2),$np	# rewind $np
+	lea	($np,$num),$np		# rewind $np
 
 	xor	$N[1],$N[1]
 	add	$A[0],$N[0]
 	adc	\$0,$N[1]
 	mov	$N[0],-8($tp)
 
 	jmp	.Louter4x
 
 .align	32
 .Louter4x:
+	lea	16+128($tp),%rdx	# where 256-byte mask is (+size optimization)
+	pxor	%xmm4,%xmm4
+	pxor	%xmm5,%xmm5
+___
+for($i=0;$i<$STRIDE/16;$i+=4) {
+$code.=<<___;
+	movdqa	`16*($i+0)-128`($bp),%xmm0
+	movdqa	`16*($i+1)-128`($bp),%xmm1
+	movdqa	`16*($i+2)-128`($bp),%xmm2
+	movdqa	`16*($i+3)-128`($bp),%xmm3
+	pand	`16*($i+0)-128`(%rdx),%xmm0
+	pand	`16*($i+1)-128`(%rdx),%xmm1
+	por	%xmm0,%xmm4
+	pand	`16*($i+2)-128`(%rdx),%xmm2
+	por	%xmm1,%xmm5
+	pand	`16*($i+3)-128`(%rdx),%xmm3
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+___
+}
+$code.=<<___;
+	por	%xmm5,%xmm4
+	pshufd	\$0x4e,%xmm4,%xmm0
+	por	%xmm4,%xmm0
+	lea	$STRIDE($bp),$bp
+	movq	%xmm0,$m0		# m0=bp[i]
+
 	mov	($tp,$num),$A[0]
 	mov	$n0,$m1
 	mulq	$m0			# ap[0]*bp[i]
 	add	%rax,$A[0]		# ap[0]*bp[i]+tp[0]
 	mov	($np),%rax
 	adc	\$0,%rdx
 
-	movq	`0*$STRIDE/4-96`($bp),%xmm0
-	movq	`1*$STRIDE/4-96`($bp),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bp),%xmm2
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-96`($bp),%xmm3
-
 	imulq	$A[0],$m1		# tp[0]*n0
-	.byte	0x67
 	mov	%rdx,$A[1]
 	mov	$N[1],($tp)		# store upmost overflow bit
 
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
-	por	%xmm2,%xmm0
 	lea	($tp,$num),$tp		# rewind $tp
-	lea	$STRIDE($bp),$bp
-	por	%xmm3,%xmm0
 
 	mulq	$m1			# np[0]*m1
 	add	%rax,$A[0]		# "$N[0]", discarded
 	mov	8($ap,$num),%rax
 	adc	\$0,%rdx
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
-	mov	16*1($np),%rax		# interleaved with 0, therefore 16*n
+	mov	8*1($np),%rax
 	adc	\$0,%rdx
 	add	8($tp),$A[1]		# +tp[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	16($ap,$num),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[i]+tp[j]
 	lea	4*8($num),$j		# j=4
-	lea	16*4($np),$np
+	lea	8*4($np),$np
 	adc	\$0,%rdx
 	mov	%rdx,$N[0]
 	jmp	.Linner4x
 
 .align	32
 .Linner4x:
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
-	mov	-16*2($np),%rax
+	mov	-8*2($np),%rax
 	adc	\$0,%rdx
 	add	16($tp),$A[0]		# ap[j]*bp[i]+tp[j]
 	lea	32($tp),$tp
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[1],-32($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
-	mov	-16*1($np),%rax
+	mov	-8*1($np),%rax
 	adc	\$0,%rdx
 	add	-8($tp),$A[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	adc	\$0,%rdx
 	mov	$N[0],-24($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
-	mov	16*0($np),%rax
+	mov	8*0($np),%rax
 	adc	\$0,%rdx
 	add	($tp),$A[0]		# ap[j]*bp[i]+tp[j]
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	8($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[1],-16($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
-	mov	16*1($np),%rax
+	mov	8*1($np),%rax
 	adc	\$0,%rdx
 	add	8($tp),$A[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	16($ap,$j),%rax
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
-	lea	16*4($np),$np
+	lea	8*4($np),$np
 	adc	\$0,%rdx
 	mov	$N[0],-8($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
 	add	\$32,$j			# j+=4
 	jnz	.Linner4x
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[0]
-	mov	-16*2($np),%rax
+	mov	-8*2($np),%rax
 	adc	\$0,%rdx
 	add	16($tp),$A[0]		# ap[j]*bp[i]+tp[j]
 	lea	32($tp),$tp
 	adc	\$0,%rdx
 	mov	%rdx,$A[1]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[0]
 	mov	-8($ap),%rax
 	adc	\$0,%rdx
 	add	$A[0],$N[0]
 	adc	\$0,%rdx
 	mov	$N[1],-32($tp)		# tp[j-1]
 	mov	%rdx,$N[1]
 
 	mulq	$m0			# ap[j]*bp[i]
 	add	%rax,$A[1]
 	mov	$m1,%rax
-	mov	-16*1($np),$m1
+	mov	-8*1($np),$m1
 	adc	\$0,%rdx
 	add	-8($tp),$A[1]
 	adc	\$0,%rdx
 	mov	%rdx,$A[0]
 
 	mulq	$m1			# np[j]*m1
 	add	%rax,$N[1]
 	mov	($ap,$num),%rax		# ap[0]
 	adc	\$0,%rdx
 	add	$A[1],$N[1]
 	adc	\$0,%rdx
 	mov	$N[0],-24($tp)		# tp[j-1]
 	mov	%rdx,$N[0]
 
-	movq	%xmm0,$m0		# bp[i+1]
 	mov	$N[1],-16($tp)		# tp[j-1]
-	lea	($np,$num,2),$np	# rewind $np
+	lea	($np,$num),$np		# rewind $np
 
 	xor	$N[1],$N[1]
 	add	$A[0],$N[0]
 	adc	\$0,$N[1]
 	add	($tp),$N[0]		# pull upmost overflow bit
 	adc	\$0,$N[1]		# upmost overflow bit
 	mov	$N[0],-8($tp)
 
 	cmp	16+8(%rsp),$bp
 	jb	.Louter4x
 ___
 if (1) {
 $code.=<<___;
+	xor	%rax,%rax
 	sub	$N[0],$m1		# compare top-most words
 	adc	$j,$j			# $j is zero
 	or	$j,$N[1]
-	xor	\$1,$N[1]
+	sub	$N[1],%rax		# %rax=-$N[1]
 	lea	($tp,$num),%rbx		# tptr in .sqr4x_sub
-	lea	($np,$N[1],8),%rbp	# nptr in .sqr4x_sub
+	mov	($np),%r12
+	lea	($np),%rbp		# nptr in .sqr4x_sub
 	mov	%r9,%rcx
-	sar	\$3+2,%rcx		# cf=0
+	sar	\$3+2,%rcx
 	mov	56+8(%rsp),%rdi		# rptr in .sqr4x_sub
-	jmp	.Lsqr4x_sub
+	dec	%r12			# so that after 'not' we get -n[0]
+	xor	%r10,%r10
+	mov	8*1(%rbp),%r13
+	mov	8*2(%rbp),%r14
+	mov	8*3(%rbp),%r15
+	jmp	.Lsqr4x_sub_entry
 ___
 } else {
 my @ri=("%rax",$bp,$m0,$m1);
 my $rp="%rdx";
 $code.=<<___
 	xor	\$1,$N[1]
 	lea	($tp,$num),$tp		# rewind $tp
 	sar	\$5,$num		# cf=0
 	lea	($np,$N[1],8),$np
 	mov	56+8(%rsp),$rp		# restore $rp
 	jmp	.Lsub4x
 
 .align	32
 .Lsub4x:
 	.byte	0x66
 	mov	8*0($tp),@ri[0]
 	mov	8*1($tp),@ri[1]
 	.byte	0x66
 	sbb	16*0($np),@ri[0]
 	mov	8*2($tp),@ri[2]
 	sbb	16*1($np),@ri[1]
 	mov	3*8($tp),@ri[3]
 	lea	4*8($tp),$tp
 	sbb	16*2($np),@ri[2]
 	mov	@ri[0],8*0($rp)
 	sbb	16*3($np),@ri[3]
 	lea	16*4($np),$np
 	mov	@ri[1],8*1($rp)
 	mov	@ri[2],8*2($rp)
 	mov	@ri[3],8*3($rp)
 	lea	8*4($rp),$rp
 
 	inc	$num
 	jnz	.Lsub4x
 
 	ret
 ___
 }
 $code.=<<___;
 .size	mul4x_internal,.-mul4x_internal
 ___
 }}}
 {{{
 ######################################################################
 # void bn_power5(
 my $rptr="%rdi";	# BN_ULONG *rptr,
 my $aptr="%rsi";	# const BN_ULONG *aptr,
 my $bptr="%rdx";	# const void *table,
 my $nptr="%rcx";	# const BN_ULONG *nptr,
 my $n0  ="%r8";		# const BN_ULONG *n0);
 my $num ="%r9";		# int num, has to be divisible by 8
 			# int pwr 
 
 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
 my @A0=("%r10","%r11");
 my @A1=("%r12","%r13");
 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
 
 $code.=<<___;
 .globl	bn_power5
 .type	bn_power5,\@function,6
 .align	32
 bn_power5:
 ___
 $code.=<<___ if ($addx);
 	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
-	and	\$0x80100,%r11d
-	cmp	\$0x80100,%r11d
+	and	\$0x80108,%r11d
+	cmp	\$0x80108,%r11d		# check for AD*X+BMI2+BMI1
 	je	.Lpowerx5_enter
 ___
 $code.=<<___;
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
-	mov	${num}d,%r10d
+
 	shl	\$3,${num}d		# convert $num to bytes
-	shl	\$3+2,%r10d		# 4*$num
+	lea	($num,$num,2),%r10d	# 3*$num
 	neg	$num
 	mov	($n0),$n0		# *n0
 
 	##############################################################
-	# ensure that stack frame doesn't alias with $aptr+4*$num
-	# modulo 4096, which covers ret[num], am[num] and n[2*num]
-	# (see bn_exp.c). this is done to allow memory disambiguation
-	# logic do its magic.
+	# Ensure that stack frame doesn't alias with $rptr+3*$num
+	# modulo 4096, which covers ret[num], am[num] and n[num]
+	# (see bn_exp.c). This is done to allow memory disambiguation
+	# logic do its magic. [Extra 256 bytes is for power mask
+	# calculated from 7th argument, the index.]
 	#
-	lea	-64(%rsp,$num,2),%r11
-	sub	$aptr,%r11
+	lea	-320(%rsp,$num,2),%r11
+	sub	$rptr,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lpwr_sp_alt
 	sub	%r11,%rsp		# align with $aptr
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*num*8+256)
 	jmp	.Lpwr_sp_done
 
 .align	32
 .Lpwr_sp_alt:
-	lea	4096-64(,$num,2),%r10	# 4096-frame-2*$num
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	4096-320(,$num,2),%r10
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*num*8+256)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lpwr_sp_done:
 	and	\$-64,%rsp
 	mov	$num,%r10	
 	neg	$num
 
 	##############################################################
 	# Stack layout
 	#
 	# +0	saved $num, used in reduction section
 	# +8	&t[2*$num], used in reduction section
 	# +32	saved *n0
 	# +40	saved %rsp
 	# +48	t[2*$num]
 	#
 	mov	$n0,  32(%rsp)
 	mov	%rax, 40(%rsp)		# save original %rsp
 .Lpower5_body:
-	movq	$rptr,%xmm1		# save $rptr
+	movq	$rptr,%xmm1		# save $rptr, used in sqr8x
 	movq	$nptr,%xmm2		# save $nptr
-	movq	%r10, %xmm3		# -$num
+	movq	%r10, %xmm3		# -$num, used in sqr8x
 	movq	$bptr,%xmm4
 
 	call	__bn_sqr8x_internal
+	call	__bn_post4x_internal
 	call	__bn_sqr8x_internal
+	call	__bn_post4x_internal
 	call	__bn_sqr8x_internal
+	call	__bn_post4x_internal
 	call	__bn_sqr8x_internal
+	call	__bn_post4x_internal
 	call	__bn_sqr8x_internal
+	call	__bn_post4x_internal
 
 	movq	%xmm2,$nptr
 	movq	%xmm4,$bptr
 	mov	$aptr,$rptr
 	mov	40(%rsp),%rax
 	lea	32(%rsp),$n0
 
 	call	mul4x_internal
 
 	mov	40(%rsp),%rsi		# restore %rsp
 	mov	\$1,%rax
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lpower5_epilogue:
 	ret
 .size	bn_power5,.-bn_power5
 
 .globl	bn_sqr8x_internal
 .hidden	bn_sqr8x_internal
 .type	bn_sqr8x_internal,\@abi-omnipotent
 .align	32
 bn_sqr8x_internal:
 __bn_sqr8x_internal:
 	##############################################################
 	# Squaring part:
 	#
 	# a) multiply-n-add everything but a[i]*a[i];
 	# b) shift result of a) by 1 to the left and accumulate
 	#    a[i]*a[i] products;
 	#
 	##############################################################
 	#                                                     a[1]a[0]
 	#                                                 a[2]a[0]
 	#                                             a[3]a[0]
 	#                                             a[2]a[1]
 	#                                         a[4]a[0]
 	#                                         a[3]a[1]
 	#                                     a[5]a[0]
 	#                                     a[4]a[1]
 	#                                     a[3]a[2]
 	#                                 a[6]a[0]
 	#                                 a[5]a[1]
 	#                                 a[4]a[2]
 	#                             a[7]a[0]
 	#                             a[6]a[1]
 	#                             a[5]a[2]
 	#                             a[4]a[3]
 	#                         a[7]a[1]
 	#                         a[6]a[2]
 	#                         a[5]a[3]
 	#                     a[7]a[2]
 	#                     a[6]a[3]
 	#                     a[5]a[4]
 	#                 a[7]a[3]
 	#                 a[6]a[4]
 	#             a[7]a[4]
 	#             a[6]a[5]
 	#         a[7]a[5]
 	#     a[7]a[6]
 	#                                                     a[1]a[0]
 	#                                                 a[2]a[0]
 	#                                             a[3]a[0]
 	#                                         a[4]a[0]
 	#                                     a[5]a[0]
 	#                                 a[6]a[0]
 	#                             a[7]a[0]
 	#                                             a[2]a[1]
 	#                                         a[3]a[1]
 	#                                     a[4]a[1]
 	#                                 a[5]a[1]
 	#                             a[6]a[1]
 	#                         a[7]a[1]
 	#                                     a[3]a[2]
 	#                                 a[4]a[2]
 	#                             a[5]a[2]
 	#                         a[6]a[2]
 	#                     a[7]a[2]
 	#                             a[4]a[3]
 	#                         a[5]a[3]
 	#                     a[6]a[3]
 	#                 a[7]a[3]
 	#                     a[5]a[4]
 	#                 a[6]a[4]
 	#             a[7]a[4]
 	#             a[6]a[5]
 	#         a[7]a[5]
 	#     a[7]a[6]
 	#                                                         a[0]a[0]
 	#                                                 a[1]a[1]
 	#                                         a[2]a[2]
 	#                                 a[3]a[3]
 	#                         a[4]a[4]
 	#                 a[5]a[5]
 	#         a[6]a[6]
 	# a[7]a[7]
 
 	lea	32(%r10),$i		# $i=-($num-32)
 	lea	($aptr,$num),$aptr	# end of a[] buffer, ($aptr,$i)=&ap[2]
 
 	mov	$num,$j			# $j=$num
 
 					# comments apply to $num==8 case
 	mov	-32($aptr,$i),$a0	# a[0]
 	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
 	mov	-24($aptr,$i),%rax	# a[1]
 	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
 	mov	-16($aptr,$i),$ai	# a[2]
 	mov	%rax,$a1
 
 	mul	$a0			# a[1]*a[0]
 	mov	%rax,$A0[0]		# a[1]*a[0]
 	 mov	$ai,%rax		# a[2]
 	mov	%rdx,$A0[1]
 	mov	$A0[0],-24($tptr,$i)	# t[1]
 
 	mul	$a0			# a[2]*a[0]
 	add	%rax,$A0[1]
 	 mov	$ai,%rax
 	adc	\$0,%rdx
 	mov	$A0[1],-16($tptr,$i)	# t[2]
 	mov	%rdx,$A0[0]
 
 
 	 mov	-8($aptr,$i),$ai	# a[3]
 	mul	$a1			# a[2]*a[1]
 	mov	%rax,$A1[0]		# a[2]*a[1]+t[3]
 	 mov	$ai,%rax
 	mov	%rdx,$A1[1]
 
 	 lea	($i),$j
 	mul	$a0			# a[3]*a[0]
 	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
 	 mov	$ai,%rax
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	add	$A1[0],$A0[0]
 	adc	\$0,$A0[1]
 	mov	$A0[0],-8($tptr,$j)	# t[3]
 	jmp	.Lsqr4x_1st
 
 .align	32
 .Lsqr4x_1st:
 	 mov	($aptr,$j),$ai		# a[4]
 	mul	$a1			# a[3]*a[1]
 	add	%rax,$A1[1]		# a[3]*a[1]+t[4]
 	 mov	$ai,%rax
 	mov	%rdx,$A1[0]
 	adc	\$0,$A1[0]
 
 	mul	$a0			# a[4]*a[0]
 	add	%rax,$A0[1]		# a[4]*a[0]+a[3]*a[1]+t[4]
 	 mov	$ai,%rax		# a[3]
 	 mov	8($aptr,$j),$ai		# a[5]
 	mov	%rdx,$A0[0]
 	adc	\$0,$A0[0]
 	add	$A1[1],$A0[1]
 	adc	\$0,$A0[0]
 
 
 	mul	$a1			# a[4]*a[3]
 	add	%rax,$A1[0]		# a[4]*a[3]+t[5]
 	 mov	$ai,%rax
 	 mov	$A0[1],($tptr,$j)	# t[4]
 	mov	%rdx,$A1[1]
 	adc	\$0,$A1[1]
 
 	mul	$a0			# a[5]*a[2]
 	add	%rax,$A0[0]		# a[5]*a[2]+a[4]*a[3]+t[5]
 	 mov	$ai,%rax
 	 mov	16($aptr,$j),$ai	# a[6]
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	add	$A1[0],$A0[0]
 	adc	\$0,$A0[1]
 
 	mul	$a1			# a[5]*a[3]
 	add	%rax,$A1[1]		# a[5]*a[3]+t[6]
 	 mov	$ai,%rax
 	 mov	$A0[0],8($tptr,$j)	# t[5]
 	mov	%rdx,$A1[0]
 	adc	\$0,$A1[0]
 
 	mul	$a0			# a[6]*a[2]
 	add	%rax,$A0[1]		# a[6]*a[2]+a[5]*a[3]+t[6]
 	 mov	$ai,%rax		# a[3]
 	 mov	24($aptr,$j),$ai	# a[7]
 	mov	%rdx,$A0[0]
 	adc	\$0,$A0[0]
 	add	$A1[1],$A0[1]
 	adc	\$0,$A0[0]
 
 
 	mul	$a1			# a[6]*a[5]
 	add	%rax,$A1[0]		# a[6]*a[5]+t[7]
 	 mov	$ai,%rax
 	 mov	$A0[1],16($tptr,$j)	# t[6]
 	mov	%rdx,$A1[1]
 	adc	\$0,$A1[1]
 	 lea	32($j),$j
 
 	mul	$a0			# a[7]*a[4]
 	add	%rax,$A0[0]		# a[7]*a[4]+a[6]*a[5]+t[6]
 	 mov	$ai,%rax
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	add	$A1[0],$A0[0]
 	adc	\$0,$A0[1]
 	mov	$A0[0],-8($tptr,$j)	# t[7]
 
 	cmp	\$0,$j
 	jne	.Lsqr4x_1st
 
 	mul	$a1			# a[7]*a[5]
 	add	%rax,$A1[1]
 	lea	16($i),$i
 	adc	\$0,%rdx
 	add	$A0[1],$A1[1]
 	adc	\$0,%rdx
 
 	mov	$A1[1],($tptr)		# t[8]
 	mov	%rdx,$A1[0]
 	mov	%rdx,8($tptr)		# t[9]
 	jmp	.Lsqr4x_outer
 
 .align	32
 .Lsqr4x_outer:				# comments apply to $num==6 case
 	mov	-32($aptr,$i),$a0	# a[0]
 	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
 	mov	-24($aptr,$i),%rax	# a[1]
 	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
 	mov	-16($aptr,$i),$ai	# a[2]
 	mov	%rax,$a1
 
 	mul	$a0			# a[1]*a[0]
 	mov	-24($tptr,$i),$A0[0]	# t[1]
 	add	%rax,$A0[0]		# a[1]*a[0]+t[1]
 	 mov	$ai,%rax		# a[2]
 	adc	\$0,%rdx
 	mov	$A0[0],-24($tptr,$i)	# t[1]
 	mov	%rdx,$A0[1]
 
 	mul	$a0			# a[2]*a[0]
 	add	%rax,$A0[1]
 	 mov	$ai,%rax
 	adc	\$0,%rdx
 	add	-16($tptr,$i),$A0[1]	# a[2]*a[0]+t[2]
 	mov	%rdx,$A0[0]
 	adc	\$0,$A0[0]
 	mov	$A0[1],-16($tptr,$i)	# t[2]
 
 	xor	$A1[0],$A1[0]
 
 	 mov	-8($aptr,$i),$ai	# a[3]
 	mul	$a1			# a[2]*a[1]
 	add	%rax,$A1[0]		# a[2]*a[1]+t[3]
 	 mov	$ai,%rax
 	adc	\$0,%rdx
 	add	-8($tptr,$i),$A1[0]
 	mov	%rdx,$A1[1]
 	adc	\$0,$A1[1]
 
 	mul	$a0			# a[3]*a[0]
 	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
 	 mov	$ai,%rax
 	adc	\$0,%rdx
 	add	$A1[0],$A0[0]
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	mov	$A0[0],-8($tptr,$i)	# t[3]
 
 	lea	($i),$j
 	jmp	.Lsqr4x_inner
 
 .align	32
 .Lsqr4x_inner:
 	 mov	($aptr,$j),$ai		# a[4]
 	mul	$a1			# a[3]*a[1]
 	add	%rax,$A1[1]		# a[3]*a[1]+t[4]
 	 mov	$ai,%rax
 	mov	%rdx,$A1[0]
 	adc	\$0,$A1[0]
 	add	($tptr,$j),$A1[1]
 	adc	\$0,$A1[0]
 
 	.byte	0x67
 	mul	$a0			# a[4]*a[0]
 	add	%rax,$A0[1]		# a[4]*a[0]+a[3]*a[1]+t[4]
 	 mov	$ai,%rax		# a[3]
 	 mov	8($aptr,$j),$ai		# a[5]
 	mov	%rdx,$A0[0]
 	adc	\$0,$A0[0]
 	add	$A1[1],$A0[1]
 	adc	\$0,$A0[0]
 
 	mul	$a1			# a[4]*a[3]
 	add	%rax,$A1[0]		# a[4]*a[3]+t[5]
 	mov	$A0[1],($tptr,$j)	# t[4]
 	 mov	$ai,%rax
 	mov	%rdx,$A1[1]
 	adc	\$0,$A1[1]
 	add	8($tptr,$j),$A1[0]
 	lea	16($j),$j		# j++
 	adc	\$0,$A1[1]
 
 	mul	$a0			# a[5]*a[2]
 	add	%rax,$A0[0]		# a[5]*a[2]+a[4]*a[3]+t[5]
 	 mov	$ai,%rax
 	adc	\$0,%rdx
 	add	$A1[0],$A0[0]
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	mov	$A0[0],-8($tptr,$j)	# t[5], "preloaded t[1]" below
 
 	cmp	\$0,$j
 	jne	.Lsqr4x_inner
 
 	.byte	0x67
 	mul	$a1			# a[5]*a[3]
 	add	%rax,$A1[1]
 	adc	\$0,%rdx
 	add	$A0[1],$A1[1]
 	adc	\$0,%rdx
 
 	mov	$A1[1],($tptr)		# t[6], "preloaded t[2]" below
 	mov	%rdx,$A1[0]
 	mov	%rdx,8($tptr)		# t[7], "preloaded t[3]" below
 
 	add	\$16,$i
 	jnz	.Lsqr4x_outer
 
 					# comments apply to $num==4 case
 	mov	-32($aptr),$a0		# a[0]
 	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
 	mov	-24($aptr),%rax		# a[1]
 	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
 	mov	-16($aptr),$ai		# a[2]
 	mov	%rax,$a1
 
 	mul	$a0			# a[1]*a[0]
 	add	%rax,$A0[0]		# a[1]*a[0]+t[1], preloaded t[1]
 	 mov	$ai,%rax		# a[2]
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 
 	mul	$a0			# a[2]*a[0]
 	add	%rax,$A0[1]
 	 mov	$ai,%rax
 	 mov	$A0[0],-24($tptr)	# t[1]
 	mov	%rdx,$A0[0]
 	adc	\$0,$A0[0]
 	add	$A1[1],$A0[1]		# a[2]*a[0]+t[2], preloaded t[2]
 	 mov	-8($aptr),$ai		# a[3]
 	adc	\$0,$A0[0]
 
 	mul	$a1			# a[2]*a[1]
 	add	%rax,$A1[0]		# a[2]*a[1]+t[3], preloaded t[3]
 	 mov	$ai,%rax
 	 mov	$A0[1],-16($tptr)	# t[2]
 	mov	%rdx,$A1[1]
 	adc	\$0,$A1[1]
 
 	mul	$a0			# a[3]*a[0]
 	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
 	 mov	$ai,%rax
 	mov	%rdx,$A0[1]
 	adc	\$0,$A0[1]
 	add	$A1[0],$A0[0]
 	adc	\$0,$A0[1]
 	mov	$A0[0],-8($tptr)	# t[3]
 
 	mul	$a1			# a[3]*a[1]
 	add	%rax,$A1[1]
 	 mov	-16($aptr),%rax		# a[2]
 	adc	\$0,%rdx
 	add	$A0[1],$A1[1]
 	adc	\$0,%rdx
 
 	mov	$A1[1],($tptr)		# t[4]
 	mov	%rdx,$A1[0]
 	mov	%rdx,8($tptr)		# t[5]
 
 	mul	$ai			# a[2]*a[3]
 ___
 {
 my ($shift,$carry)=($a0,$a1);
 my @S=(@A1,$ai,$n0);
 $code.=<<___;
 	 add	\$16,$i
 	 xor	$shift,$shift
 	 sub	$num,$i			# $i=16-$num
 	 xor	$carry,$carry
 
 	add	$A1[0],%rax		# t[5]
 	adc	\$0,%rdx
 	mov	%rax,8($tptr)		# t[5]
 	mov	%rdx,16($tptr)		# t[6]
 	mov	$carry,24($tptr)	# t[7]
 
 	 mov	-16($aptr,$i),%rax	# a[0]
 	lea	48+8(%rsp),$tptr
 	 xor	$A0[0],$A0[0]		# t[0]
 	 mov	8($tptr),$A0[1]		# t[1]
 
 	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[1]		# | t[2*i]>>63
 	 mov	16($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	24($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[0]
 	 mov	-8($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[0],($tptr)
 	adc	%rdx,$S[1]
 
 	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
 	 mov	$S[1],8($tptr)
 	 sbb	$carry,$carry		# mov cf,$carry
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[3]		# | t[2*i]>>63
 	 mov	32($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	40($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[2]
 	 mov	0($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[2],16($tptr)
 	adc	%rdx,$S[3]
 	lea	16($i),$i
 	mov	$S[3],24($tptr)
 	sbb	$carry,$carry		# mov cf,$carry
 	lea	64($tptr),$tptr
 	jmp	.Lsqr4x_shift_n_add
 
 .align	32
 .Lsqr4x_shift_n_add:
 	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[1]		# | t[2*i]>>63
 	 mov	-16($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	-8($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[0]
 	 mov	-8($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[0],-32($tptr)
 	adc	%rdx,$S[1]
 
 	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
 	 mov	$S[1],-24($tptr)
 	 sbb	$carry,$carry		# mov cf,$carry
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[3]		# | t[2*i]>>63
 	 mov	0($tptr),$A0[0]		# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	8($tptr),$A0[1]		# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[2]
 	 mov	0($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[2],-16($tptr)
 	adc	%rdx,$S[3]
 
 	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
 	 mov	$S[3],-8($tptr)
 	 sbb	$carry,$carry		# mov cf,$carry
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[1]		# | t[2*i]>>63
 	 mov	16($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	24($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[0]
 	 mov	8($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[0],0($tptr)
 	adc	%rdx,$S[1]
 
 	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
 	 mov	$S[1],8($tptr)
 	 sbb	$carry,$carry		# mov cf,$carry
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[3]		# | t[2*i]>>63
 	 mov	32($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	40($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[2]
 	 mov	16($aptr,$i),%rax	# a[i+1]	# prefetch
 	mov	$S[2],16($tptr)
 	adc	%rdx,$S[3]
 	mov	$S[3],24($tptr)
 	sbb	$carry,$carry		# mov cf,$carry
 	lea	64($tptr),$tptr
 	add	\$32,$i
 	jnz	.Lsqr4x_shift_n_add
 
 	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
 	.byte	0x67
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[1]		# | t[2*i]>>63
 	 mov	-16($tptr),$A0[0]	# t[2*i+2]	# prefetch
 	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	 mov	-8($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
 	adc	%rax,$S[0]
 	 mov	-8($aptr),%rax		# a[i+1]	# prefetch
 	mov	$S[0],-32($tptr)
 	adc	%rdx,$S[1]
 
 	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1|shift
 	 mov	$S[1],-24($tptr)
 	 sbb	$carry,$carry		# mov cf,$carry
 	shr	\$63,$A0[0]
 	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
 	shr	\$63,$A0[1]
 	or	$A0[0],$S[3]		# | t[2*i]>>63
 	mul	%rax			# a[i]*a[i]
 	neg	$carry			# mov $carry,cf
 	adc	%rax,$S[2]
 	adc	%rdx,$S[3]
 	mov	$S[2],-16($tptr)
 	mov	$S[3],-8($tptr)
 ___
 }
 ######################################################################
 # Montgomery reduction part, "word-by-word" algorithm.
 #
 # This new path is inspired by multiple submissions from Intel, by
 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
 # Vinodh Gopal...
 {
 my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
 
 $code.=<<___;
 	movq	%xmm2,$nptr
-sqr8x_reduction:
+__bn_sqr8x_reduction:
 	xor	%rax,%rax
-	lea	($nptr,$num,2),%rcx	# end of n[]
+	lea	($nptr,$num),%rcx	# end of n[]
 	lea	48+8(%rsp,$num,2),%rdx	# end of t[] buffer
 	mov	%rcx,0+8(%rsp)
 	lea	48+8(%rsp,$num),$tptr	# end of initial t[] window
 	mov	%rdx,8+8(%rsp)
 	neg	$num
 	jmp	.L8x_reduction_loop
 
 .align	32
 .L8x_reduction_loop:
 	lea	($tptr,$num),$tptr	# start of current t[] window
 	.byte	0x66
 	mov	8*0($tptr),$m0
 	mov	8*1($tptr),%r9
 	mov	8*2($tptr),%r10
 	mov	8*3($tptr),%r11
 	mov	8*4($tptr),%r12
 	mov	8*5($tptr),%r13
 	mov	8*6($tptr),%r14
 	mov	8*7($tptr),%r15
 	mov	%rax,(%rdx)		# store top-most carry bit
 	lea	8*8($tptr),$tptr
 
 	.byte	0x67
 	mov	$m0,%r8
 	imulq	32+8(%rsp),$m0		# n0*a[0]
-	mov	16*0($nptr),%rax	# n[0]
+	mov	8*0($nptr),%rax		# n[0]
 	mov	\$8,%ecx
 	jmp	.L8x_reduce
 
 .align	32
 .L8x_reduce:
 	mulq	$m0
-	 mov	16*1($nptr),%rax	# n[1]
+	 mov	8*1($nptr),%rax		# n[1]
 	neg	%r8
 	mov	%rdx,%r8
 	adc	\$0,%r8
 
 	mulq	$m0
 	add	%rax,%r9
-	 mov	16*2($nptr),%rax
+	 mov	8*2($nptr),%rax
 	adc	\$0,%rdx
 	add	%r9,%r8
 	 mov	$m0,48-8+8(%rsp,%rcx,8)	# put aside n0*a[i]
 	mov	%rdx,%r9
 	adc	\$0,%r9
 
 	mulq	$m0
 	add	%rax,%r10
-	 mov	16*3($nptr),%rax
+	 mov	8*3($nptr),%rax
 	adc	\$0,%rdx
 	add	%r10,%r9
 	 mov	32+8(%rsp),$carry	# pull n0, borrow $carry
 	mov	%rdx,%r10
 	adc	\$0,%r10
 
 	mulq	$m0
 	add	%rax,%r11
-	 mov	16*4($nptr),%rax
+	 mov	8*4($nptr),%rax
 	adc	\$0,%rdx
 	 imulq	%r8,$carry		# modulo-scheduled
 	add	%r11,%r10
 	mov	%rdx,%r11
 	adc	\$0,%r11
 
 	mulq	$m0
 	add	%rax,%r12
-	 mov	16*5($nptr),%rax
+	 mov	8*5($nptr),%rax
 	adc	\$0,%rdx
 	add	%r12,%r11
 	mov	%rdx,%r12
 	adc	\$0,%r12
 
 	mulq	$m0
 	add	%rax,%r13
-	 mov	16*6($nptr),%rax
+	 mov	8*6($nptr),%rax
 	adc	\$0,%rdx
 	add	%r13,%r12
 	mov	%rdx,%r13
 	adc	\$0,%r13
 
 	mulq	$m0
 	add	%rax,%r14
-	 mov	16*7($nptr),%rax
+	 mov	8*7($nptr),%rax
 	adc	\$0,%rdx
 	add	%r14,%r13
 	mov	%rdx,%r14
 	adc	\$0,%r14
 
 	mulq	$m0
 	 mov	$carry,$m0		# n0*a[i]
 	add	%rax,%r15
-	 mov	16*0($nptr),%rax	# n[0]
+	 mov	8*0($nptr),%rax		# n[0]
 	adc	\$0,%rdx
 	add	%r15,%r14
 	mov	%rdx,%r15
 	adc	\$0,%r15
 
 	dec	%ecx
 	jnz	.L8x_reduce
 
-	lea	16*8($nptr),$nptr
+	lea	8*8($nptr),$nptr
 	xor	%rax,%rax
 	mov	8+8(%rsp),%rdx		# pull end of t[]
 	cmp	0+8(%rsp),$nptr		# end of n[]?
 	jae	.L8x_no_tail
 
 	.byte	0x66
 	add	8*0($tptr),%r8
 	adc	8*1($tptr),%r9
 	adc	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	sbb	$carry,$carry		# top carry
 
 	mov	48+56+8(%rsp),$m0	# pull n0*a[0]
 	mov	\$8,%ecx
-	mov	16*0($nptr),%rax
+	mov	8*0($nptr),%rax
 	jmp	.L8x_tail
 
 .align	32
 .L8x_tail:
 	mulq	$m0
 	add	%rax,%r8
-	 mov	16*1($nptr),%rax
+	 mov	8*1($nptr),%rax
 	 mov	%r8,($tptr)		# save result
 	mov	%rdx,%r8
 	adc	\$0,%r8
 
 	mulq	$m0
 	add	%rax,%r9
-	 mov	16*2($nptr),%rax
+	 mov	8*2($nptr),%rax
 	adc	\$0,%rdx
 	add	%r9,%r8
 	 lea	8($tptr),$tptr		# $tptr++
 	mov	%rdx,%r9
 	adc	\$0,%r9
 
 	mulq	$m0
 	add	%rax,%r10
-	 mov	16*3($nptr),%rax
+	 mov	8*3($nptr),%rax
 	adc	\$0,%rdx
 	add	%r10,%r9
 	mov	%rdx,%r10
 	adc	\$0,%r10
 
 	mulq	$m0
 	add	%rax,%r11
-	 mov	16*4($nptr),%rax
+	 mov	8*4($nptr),%rax
 	adc	\$0,%rdx
 	add	%r11,%r10
 	mov	%rdx,%r11
 	adc	\$0,%r11
 
 	mulq	$m0
 	add	%rax,%r12
-	 mov	16*5($nptr),%rax
+	 mov	8*5($nptr),%rax
 	adc	\$0,%rdx
 	add	%r12,%r11
 	mov	%rdx,%r12
 	adc	\$0,%r12
 
 	mulq	$m0
 	add	%rax,%r13
-	 mov	16*6($nptr),%rax
+	 mov	8*6($nptr),%rax
 	adc	\$0,%rdx
 	add	%r13,%r12
 	mov	%rdx,%r13
 	adc	\$0,%r13
 
 	mulq	$m0
 	add	%rax,%r14
-	 mov	16*7($nptr),%rax
+	 mov	8*7($nptr),%rax
 	adc	\$0,%rdx
 	add	%r14,%r13
 	mov	%rdx,%r14
 	adc	\$0,%r14
 
 	mulq	$m0
 	 mov	48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
 	add	%rax,%r15
 	adc	\$0,%rdx
 	add	%r15,%r14
-	 mov	16*0($nptr),%rax	# pull n[0]
+	 mov	8*0($nptr),%rax		# pull n[0]
 	mov	%rdx,%r15
 	adc	\$0,%r15
 
 	dec	%ecx
 	jnz	.L8x_tail
 
-	lea	16*8($nptr),$nptr
+	lea	8*8($nptr),$nptr
 	mov	8+8(%rsp),%rdx		# pull end of t[]
 	cmp	0+8(%rsp),$nptr		# end of n[]?
 	jae	.L8x_tail_done		# break out of loop
 
 	 mov	48+56+8(%rsp),$m0	# pull n0*a[0]
 	neg	$carry
 	 mov	8*0($nptr),%rax		# pull n[0]
 	adc	8*0($tptr),%r8
 	adc	8*1($tptr),%r9
 	adc	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	sbb	$carry,$carry		# top carry
 
 	mov	\$8,%ecx
 	jmp	.L8x_tail
 
 .align	32
 .L8x_tail_done:
 	add	(%rdx),%r8		# can this overflow?
 	adc	\$0,%r9
 	adc	\$0,%r10
 	adc	\$0,%r11
 	adc	\$0,%r12
 	adc	\$0,%r13
 	adc	\$0,%r14
 	adc	\$0,%r15		# can't overflow, because we
 					# started with "overhung" part
 					# of multiplication
 	xor	%rax,%rax
 
 	neg	$carry
 .L8x_no_tail:
 	adc	8*0($tptr),%r8
 	adc	8*1($tptr),%r9
 	adc	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	adc	\$0,%rax		# top-most carry
-	 mov	-16($nptr),%rcx		# np[num-1]
+	 mov	-8($nptr),%rcx		# np[num-1]
 	 xor	$carry,$carry
 
 	movq	%xmm2,$nptr		# restore $nptr
 
 	mov	%r8,8*0($tptr)		# store top 512 bits
 	mov	%r9,8*1($tptr)
 	 movq	%xmm3,$num		# $num is %r9, can't be moved upwards
 	mov	%r10,8*2($tptr)
 	mov	%r11,8*3($tptr)
 	mov	%r12,8*4($tptr)
 	mov	%r13,8*5($tptr)
 	mov	%r14,8*6($tptr)
 	mov	%r15,8*7($tptr)
 	lea	8*8($tptr),$tptr
 
 	cmp	%rdx,$tptr		# end of t[]?
 	jb	.L8x_reduction_loop
+	ret
+.size	bn_sqr8x_internal,.-bn_sqr8x_internal
 ___
 }
 ##############################################################
 # Post-condition, 4x unrolled
 #
 {
 my ($tptr,$nptr)=("%rbx","%rbp");
 $code.=<<___;
-	#xor	%rsi,%rsi		# %rsi was $carry above
-	sub	%r15,%rcx		# compare top-most words
+.type	__bn_post4x_internal,\@abi-omnipotent
+.align	32
+__bn_post4x_internal:
+	mov	8*0($nptr),%r12
 	lea	(%rdi,$num),$tptr	# %rdi was $tptr above
-	adc	%rsi,%rsi
 	mov	$num,%rcx
-	or	%rsi,%rax
 	movq	%xmm1,$rptr		# restore $rptr
-	xor	\$1,%rax
+	neg	%rax
 	movq	%xmm1,$aptr		# prepare for back-to-back call
-	lea	($nptr,%rax,8),$nptr
-	sar	\$3+2,%rcx		# cf=0
-	jmp	.Lsqr4x_sub
+	sar	\$3+2,%rcx
+	dec	%r12			# so that after 'not' we get -n[0]
+	xor	%r10,%r10
+	mov	8*1($nptr),%r13
+	mov	8*2($nptr),%r14
+	mov	8*3($nptr),%r15
+	jmp	.Lsqr4x_sub_entry
 
-.align	32
+.align	16
 .Lsqr4x_sub:
-	.byte	0x66
-	mov	8*0($tptr),%r12
-	mov	8*1($tptr),%r13
-	sbb	16*0($nptr),%r12
-	mov	8*2($tptr),%r14
-	sbb	16*1($nptr),%r13
-	mov	8*3($tptr),%r15
-	lea	8*4($tptr),$tptr
-	sbb	16*2($nptr),%r14
+	mov	8*0($nptr),%r12
+	mov	8*1($nptr),%r13
+	mov	8*2($nptr),%r14
+	mov	8*3($nptr),%r15
+.Lsqr4x_sub_entry:
+	lea	8*4($nptr),$nptr
+	not	%r12
+	not	%r13
+	not	%r14
+	not	%r15
+	and	%rax,%r12
+	and	%rax,%r13
+	and	%rax,%r14
+	and	%rax,%r15
+
+	neg	%r10			# mov %r10,%cf
+	adc	8*0($tptr),%r12
+	adc	8*1($tptr),%r13
+	adc	8*2($tptr),%r14
+	adc	8*3($tptr),%r15
 	mov	%r12,8*0($rptr)
-	sbb	16*3($nptr),%r15
-	lea	16*4($nptr),$nptr
+	lea	8*4($tptr),$tptr
 	mov	%r13,8*1($rptr)
+	sbb	%r10,%r10		# mov %cf,%r10
 	mov	%r14,8*2($rptr)
 	mov	%r15,8*3($rptr)
 	lea	8*4($rptr),$rptr
 
 	inc	%rcx			# pass %cf
 	jnz	.Lsqr4x_sub
-___
-}
-$code.=<<___;
+
 	mov	$num,%r10		# prepare for back-to-back call
 	neg	$num			# restore $num	
 	ret
-.size	bn_sqr8x_internal,.-bn_sqr8x_internal
+.size	__bn_post4x_internal,.-__bn_post4x_internal
 ___
+}
 {
 $code.=<<___;
 .globl	bn_from_montgomery
 .type	bn_from_montgomery,\@abi-omnipotent
 .align	32
 bn_from_montgomery:
 	testl	\$7,`($win64?"48(%rsp)":"%r9d")`
 	jz	bn_from_mont8x
 	xor	%eax,%eax
 	ret
 .size	bn_from_montgomery,.-bn_from_montgomery
 
 .type	bn_from_mont8x,\@function,6
 .align	32
 bn_from_mont8x:
 	.byte	0x67
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
-	.byte	0x67
-	mov	${num}d,%r10d
+
 	shl	\$3,${num}d		# convert $num to bytes
-	shl	\$3+2,%r10d		# 4*$num
+	lea	($num,$num,2),%r10	# 3*$num in bytes
 	neg	$num
 	mov	($n0),$n0		# *n0
 
 	##############################################################
-	# ensure that stack frame doesn't alias with $aptr+4*$num
-	# modulo 4096, which covers ret[num], am[num] and n[2*num]
-	# (see bn_exp.c). this is done to allow memory disambiguation
-	# logic do its magic.
+	# Ensure that stack frame doesn't alias with $rptr+3*$num
+	# modulo 4096, which covers ret[num], am[num] and n[num]
+	# (see bn_exp.c). The stack is allocated to aligned with
+	# bn_power5's frame, and as bn_from_montgomery happens to be
+	# last operation, we use the opportunity to cleanse it.
 	#
-	lea	-64(%rsp,$num,2),%r11
-	sub	$aptr,%r11
+	lea	-320(%rsp,$num,2),%r11
+	sub	$rptr,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lfrom_sp_alt
 	sub	%r11,%rsp		# align with $aptr
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	jmp	.Lfrom_sp_done
 
 .align	32
 .Lfrom_sp_alt:
-	lea	4096-64(,$num,2),%r10	# 4096-frame-2*$num
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	4096-320(,$num,2),%r10
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lfrom_sp_done:
 	and	\$-64,%rsp
 	mov	$num,%r10	
 	neg	$num
 
 	##############################################################
 	# Stack layout
 	#
 	# +0	saved $num, used in reduction section
 	# +8	&t[2*$num], used in reduction section
 	# +32	saved *n0
 	# +40	saved %rsp
 	# +48	t[2*$num]
 	#
 	mov	$n0,  32(%rsp)
 	mov	%rax, 40(%rsp)		# save original %rsp
 .Lfrom_body:
 	mov	$num,%r11
 	lea	48(%rsp),%rax
 	pxor	%xmm0,%xmm0
 	jmp	.Lmul_by_1
 
 .align	32
 .Lmul_by_1:
 	movdqu	($aptr),%xmm1
 	movdqu	16($aptr),%xmm2
 	movdqu	32($aptr),%xmm3
 	movdqa	%xmm0,(%rax,$num)
 	movdqu	48($aptr),%xmm4
 	movdqa	%xmm0,16(%rax,$num)
 	.byte	0x48,0x8d,0xb6,0x40,0x00,0x00,0x00	# lea	64($aptr),$aptr
 	movdqa	%xmm1,(%rax)
 	movdqa	%xmm0,32(%rax,$num)
 	movdqa	%xmm2,16(%rax)
 	movdqa	%xmm0,48(%rax,$num)
 	movdqa	%xmm3,32(%rax)
 	movdqa	%xmm4,48(%rax)
 	lea	64(%rax),%rax
 	sub	\$64,%r11
 	jnz	.Lmul_by_1
 
 	movq	$rptr,%xmm1
 	movq	$nptr,%xmm2
 	.byte	0x67
 	mov	$nptr,%rbp
 	movq	%r10, %xmm3		# -num
 ___
 $code.=<<___ if ($addx);
 	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
-	and	\$0x80100,%r11d
-	cmp	\$0x80100,%r11d
+	and	\$0x80108,%r11d
+	cmp	\$0x80108,%r11d		# check for AD*X+BMI2+BMI1
 	jne	.Lfrom_mont_nox
 
 	lea	(%rax,$num),$rptr
-	call	sqrx8x_reduction
+	call	__bn_sqrx8x_reduction
+	call	__bn_postx4x_internal
 
 	pxor	%xmm0,%xmm0
 	lea	48(%rsp),%rax
 	mov	40(%rsp),%rsi		# restore %rsp
 	jmp	.Lfrom_mont_zero
 
 .align	32
 .Lfrom_mont_nox:
 ___
 $code.=<<___;
-	call	sqr8x_reduction
+	call	__bn_sqr8x_reduction
+	call	__bn_post4x_internal
 
 	pxor	%xmm0,%xmm0
 	lea	48(%rsp),%rax
 	mov	40(%rsp),%rsi		# restore %rsp
 	jmp	.Lfrom_mont_zero
 
 .align	32
 .Lfrom_mont_zero:
 	movdqa	%xmm0,16*0(%rax)
 	movdqa	%xmm0,16*1(%rax)
 	movdqa	%xmm0,16*2(%rax)
 	movdqa	%xmm0,16*3(%rax)
 	lea	16*4(%rax),%rax
 	sub	\$32,$num
 	jnz	.Lfrom_mont_zero
 
 	mov	\$1,%rax
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lfrom_epilogue:
 	ret
 .size	bn_from_mont8x,.-bn_from_mont8x
 ___
 }
 }}}
 
 if ($addx) {{{
 my $bp="%rdx";	# restore original value
 
 $code.=<<___;
 .type	bn_mulx4x_mont_gather5,\@function,6
 .align	32
 bn_mulx4x_mont_gather5:
 .Lmulx4x_enter:
-	.byte	0x67
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
-	.byte	0x67
-	mov	${num}d,%r10d
+
 	shl	\$3,${num}d		# convert $num to bytes
-	shl	\$3+2,%r10d		# 4*$num
+	lea	($num,$num,2),%r10	# 3*$num in bytes
 	neg	$num			# -$num
 	mov	($n0),$n0		# *n0
 
 	##############################################################
-	# ensure that stack frame doesn't alias with $aptr+4*$num
-	# modulo 4096, which covers a[num], ret[num] and n[2*num]
-	# (see bn_exp.c). this is done to allow memory disambiguation
-	# logic do its magic. [excessive frame is allocated in order
-	# to allow bn_from_mont8x to clear it.]
+	# Ensure that stack frame doesn't alias with $rptr+3*$num
+	# modulo 4096, which covers ret[num], am[num] and n[num]
+	# (see bn_exp.c). This is done to allow memory disambiguation
+	# logic do its magic. [Extra [num] is allocated in order
+	# to align with bn_power5's frame, which is cleansed after
+	# completing exponentiation. Extra 256 bytes is for power mask
+	# calculated from 7th argument, the index.]
 	#
-	lea	-64(%rsp,$num,2),%r11
-	sub	$ap,%r11
+	lea	-320(%rsp,$num,2),%r11
+	sub	$rp,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lmulx4xsp_alt
 	sub	%r11,%rsp		# align with $aptr
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+$num)
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	jmp	.Lmulx4xsp_done
 
-.align	32
 .Lmulx4xsp_alt:
-	lea	4096-64(,$num,2),%r10	# 4096-frame-$num
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+$num)
+	lea	4096-320(,$num,2),%r10
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lmulx4xsp_done:	
 	and	\$-64,%rsp		# ensure alignment
 	##############################################################
 	# Stack layout
 	# +0	-num
 	# +8	off-loaded &b[i]
 	# +16	end of b[num]
 	# +24	inner counter
 	# +32	saved n0
 	# +40	saved %rsp
 	# +48
 	# +56	saved rp
 	# +64	tmp[num+1]
 	#
 	mov	$n0, 32(%rsp)		# save *n0
 	mov	%rax,40(%rsp)		# save original %rsp
 .Lmulx4x_body:
 	call	mulx4x_internal
 
 	mov	40(%rsp),%rsi		# restore %rsp
 	mov	\$1,%rax
-___
-$code.=<<___ if ($win64);
-	movaps	-88(%rsi),%xmm6
-	movaps	-72(%rsi),%xmm7
-___
-$code.=<<___;
+
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lmulx4x_epilogue:
 	ret
 .size	bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
 
 .type	mulx4x_internal,\@abi-omnipotent
 .align	32
 mulx4x_internal:
-	.byte	0x4c,0x89,0x8c,0x24,0x08,0x00,0x00,0x00	# mov	$num,8(%rsp)		# save -$num
-	.byte	0x67
+	mov	$num,8(%rsp)		# save -$num (it was in bytes)
+	mov	$num,%r10
 	neg	$num			# restore $num
 	shl	\$5,$num
-	lea	256($bp,$num),%r13
+	neg	%r10			# restore $num
+	lea	128($bp,$num),%r13	# end of powers table (+size optimization)
 	shr	\$5+5,$num
-	mov	`($win64?56:8)`(%rax),%r10d	# load 7th argument
+	movd	`($win64?56:8)`(%rax),%xmm5	# load 7th argument
 	sub	\$1,$num
+	lea	.Linc(%rip),%rax
 	mov	%r13,16+8(%rsp)		# end of b[num]
 	mov	$num,24+8(%rsp)		# inner counter
 	mov	$rp, 56+8(%rsp)		# save $rp
 ___
 my ($aptr, $bptr, $nptr, $tptr, $mi,  $bi,  $zero, $num)=
    ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
 my $rptr=$bptr;
 my $STRIDE=2**5*8;		# 5 is "window size"
 my $N=$STRIDE/4;		# should match cache line size
 $code.=<<___;
-	mov	%r10,%r11
-	shr	\$`log($N/8)/log(2)`,%r10
-	and	\$`$N/8-1`,%r11
-	not	%r10
-	lea	.Lmagic_masks(%rip),%rax
-	and	\$`2**5/($N/8)-1`,%r10	# 5 is "window size"
-	lea	96($bp,%r11,8),$bptr	# pointer within 1st cache line
-	movq	0(%rax,%r10,8),%xmm4	# set of masks denoting which
-	movq	8(%rax,%r10,8),%xmm5	# cache line contains element
-	add	\$7,%r11
-	movq	16(%rax,%r10,8),%xmm6	# denoted by 7th argument
-	movq	24(%rax,%r10,8),%xmm7
-	and	\$7,%r11
+	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
+	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
+	lea	88-112(%rsp,%r10),%r10	# place the mask after tp[num+1] (+ICache optimizaton)
+	lea	128($bp),$bptr		# size optimization
 
-	movq	`0*$STRIDE/4-96`($bptr),%xmm0
-	lea	$STRIDE($bptr),$tptr	# borrow $tptr
-	movq	`1*$STRIDE/4-96`($bptr),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bptr),%xmm2
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-96`($bptr),%xmm3
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	movq	`0*$STRIDE/4-96`($tptr),%xmm1
-	pand	%xmm7,%xmm3
-	por	%xmm2,%xmm0
-	movq	`1*$STRIDE/4-96`($tptr),%xmm2
-	por	%xmm3,%xmm0
-	.byte	0x67,0x67
-	pand	%xmm4,%xmm1
-	movq	`2*$STRIDE/4-96`($tptr),%xmm3
+	pshufd	\$0,%xmm5,%xmm5		# broadcast index
+	movdqa	%xmm1,%xmm4
+	.byte	0x67
+	movdqa	%xmm1,%xmm2
+___
+########################################################################
+# calculate mask by comparing 0..31 to index and save result to stack
+#
+$code.=<<___;
+	.byte	0x67
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
+	movdqa	%xmm4,%xmm3
+___
+for($i=0;$i<$STRIDE/16-4;$i+=4) {
+$code.=<<___;
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
+	movdqa	%xmm0,`16*($i+0)+112`(%r10)
+	movdqa	%xmm4,%xmm0
 
+	paddd	%xmm2,%xmm3
+	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
+	movdqa	%xmm1,`16*($i+1)+112`(%r10)
+	movdqa	%xmm4,%xmm1
+
+	paddd	%xmm3,%xmm0
+	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
+	movdqa	%xmm2,`16*($i+2)+112`(%r10)
+	movdqa	%xmm4,%xmm2
+
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0
+	movdqa	%xmm3,`16*($i+3)+112`(%r10)
+	movdqa	%xmm4,%xmm3
+___
+}
+$code.=<<___;				# last iteration can be optimized
+	.byte	0x67
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1
+	movdqa	%xmm0,`16*($i+0)+112`(%r10)
+
+	paddd	%xmm2,%xmm3
+	pcmpeqd	%xmm5,%xmm2
+	movdqa	%xmm1,`16*($i+1)+112`(%r10)
+
+	pcmpeqd	%xmm5,%xmm3
+	movdqa	%xmm2,`16*($i+2)+112`(%r10)
+
+	pand	`16*($i+0)-128`($bptr),%xmm0	# while it's still in register
+	pand	`16*($i+1)-128`($bptr),%xmm1
+	pand	`16*($i+2)-128`($bptr),%xmm2
+	movdqa	%xmm3,`16*($i+3)+112`(%r10)
+	pand	`16*($i+3)-128`($bptr),%xmm3
+	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+for($i=0;$i<$STRIDE/16-4;$i+=4) {
+$code.=<<___;
+	movdqa	`16*($i+0)-128`($bptr),%xmm4
+	movdqa	`16*($i+1)-128`($bptr),%xmm5
+	movdqa	`16*($i+2)-128`($bptr),%xmm2
+	pand	`16*($i+0)+112`(%r10),%xmm4
+	movdqa	`16*($i+3)-128`($bptr),%xmm3
+	pand	`16*($i+1)+112`(%r10),%xmm5
+	por	%xmm4,%xmm0
+	pand	`16*($i+2)+112`(%r10),%xmm2
+	por	%xmm5,%xmm1
+	pand	`16*($i+3)+112`(%r10),%xmm3
+	por	%xmm2,%xmm0
+	por	%xmm3,%xmm1
+___
+}
+$code.=<<___;
+	pxor	%xmm1,%xmm0
+	pshufd	\$0x4e,%xmm0,%xmm1
+	por	%xmm1,%xmm0
+	lea	$STRIDE($bptr),$bptr
 	movq	%xmm0,%rdx		# bp[0]
-	movq	`3*$STRIDE/4-96`($tptr),%xmm0
-	lea	2*$STRIDE($bptr),$bptr	# next &b[i]
-	pand	%xmm5,%xmm2
-	.byte	0x67,0x67
-	pand	%xmm6,%xmm3
-	##############################################################
-	# $tptr is chosen so that writing to top-most element of the
-	# vector occurs just "above" references to powers table,
-	# "above" modulo cache-line size, which effectively precludes
-	# possibility of memory disambiguation logic failure when
-	# accessing the table.
-	# 
-	lea	64+8*4+8(%rsp,%r11,8),$tptr
+	lea	64+8*4+8(%rsp),$tptr
 
 	mov	%rdx,$bi
 	mulx	0*8($aptr),$mi,%rax	# a[0]*b[0]
 	mulx	1*8($aptr),%r11,%r12	# a[1]*b[0]
 	add	%rax,%r11
 	mulx	2*8($aptr),%rax,%r13	# ...
 	adc	%rax,%r12
 	adc	\$0,%r13
 	mulx	3*8($aptr),%rax,%r14
 
 	mov	$mi,%r15
 	imulq	32+8(%rsp),$mi		# "t[0]"*n0
 	xor	$zero,$zero		# cf=0, of=0
 	mov	$mi,%rdx
 
-	por	%xmm2,%xmm1
-	pand	%xmm7,%xmm0
-	por	%xmm3,%xmm1
 	mov	$bptr,8+8(%rsp)		# off-load &b[i]
-	por	%xmm1,%xmm0
 
-	.byte	0x48,0x8d,0xb6,0x20,0x00,0x00,0x00	# lea	4*8($aptr),$aptr
+	lea	4*8($aptr),$aptr
 	adcx	%rax,%r13
 	adcx	$zero,%r14		# cf=0
 
-	mulx	0*16($nptr),%rax,%r10
+	mulx	0*8($nptr),%rax,%r10
 	adcx	%rax,%r15		# discarded
 	adox	%r11,%r10
-	mulx	1*16($nptr),%rax,%r11
+	mulx	1*8($nptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
-	mulx	2*16($nptr),%rax,%r12
+	mulx	2*8($nptr),%rax,%r12
 	mov	24+8(%rsp),$bptr	# counter value
-	.byte	0x66
 	mov	%r10,-8*4($tptr)
 	adcx	%rax,%r11
 	adox	%r13,%r12
-	mulx	3*16($nptr),%rax,%r15
-	 .byte	0x67,0x67
+	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r11,-8*3($tptr)
 	adcx	%rax,%r12
 	adox	$zero,%r15		# of=0
-	.byte	0x48,0x8d,0x89,0x40,0x00,0x00,0x00	# lea	4*16($nptr),$nptr
+	lea	4*8($nptr),$nptr
 	mov	%r12,-8*2($tptr)
-	#jmp	.Lmulx4x_1st
+	jmp	.Lmulx4x_1st
 
 .align	32
 .Lmulx4x_1st:
 	adcx	$zero,%r15		# cf=0, modulo-scheduled
 	mulx	0*8($aptr),%r10,%rax	# a[4]*b[0]
 	adcx	%r14,%r10
 	mulx	1*8($aptr),%r11,%r14	# a[5]*b[0]
 	adcx	%rax,%r11
 	mulx	2*8($aptr),%r12,%rax	# ...
 	adcx	%r14,%r12
 	mulx	3*8($aptr),%r13,%r14
 	 .byte	0x67,0x67
 	 mov	$mi,%rdx
 	adcx	%rax,%r13
 	adcx	$zero,%r14		# cf=0
 	lea	4*8($aptr),$aptr
 	lea	4*8($tptr),$tptr
 
 	adox	%r15,%r10
-	mulx	0*16($nptr),%rax,%r15
+	mulx	0*8($nptr),%rax,%r15
 	adcx	%rax,%r10
 	adox	%r15,%r11
-	mulx	1*16($nptr),%rax,%r15
+	mulx	1*8($nptr),%rax,%r15
 	adcx	%rax,%r11
 	adox	%r15,%r12
-	mulx	2*16($nptr),%rax,%r15
+	mulx	2*8($nptr),%rax,%r15
 	mov	%r10,-5*8($tptr)
 	adcx	%rax,%r12
 	mov	%r11,-4*8($tptr)
 	adox	%r15,%r13
-	mulx	3*16($nptr),%rax,%r15
+	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
 	mov	%r12,-3*8($tptr)
 	adcx	%rax,%r13
 	adox	$zero,%r15
-	lea	4*16($nptr),$nptr
+	lea	4*8($nptr),$nptr
 	mov	%r13,-2*8($tptr)
 
 	dec	$bptr			# of=0, pass cf
 	jnz	.Lmulx4x_1st
 
 	mov	8(%rsp),$num		# load -num
-	movq	%xmm0,%rdx		# bp[1]
 	adc	$zero,%r15		# modulo-scheduled
 	lea	($aptr,$num),$aptr	# rewind $aptr
 	add	%r15,%r14
 	mov	8+8(%rsp),$bptr		# re-load &b[i]
 	adc	$zero,$zero		# top-most carry
 	mov	%r14,-1*8($tptr)
 	jmp	.Lmulx4x_outer
 
 .align	32
 .Lmulx4x_outer:
+	lea	16-256($tptr),%r10	# where 256-byte mask is (+density control)
+	pxor	%xmm4,%xmm4
+	.byte	0x67,0x67
+	pxor	%xmm5,%xmm5
+___
+for($i=0;$i<$STRIDE/16;$i+=4) {
+$code.=<<___;
+	movdqa	`16*($i+0)-128`($bptr),%xmm0
+	movdqa	`16*($i+1)-128`($bptr),%xmm1
+	movdqa	`16*($i+2)-128`($bptr),%xmm2
+	pand	`16*($i+0)+256`(%r10),%xmm0
+	movdqa	`16*($i+3)-128`($bptr),%xmm3
+	pand	`16*($i+1)+256`(%r10),%xmm1
+	por	%xmm0,%xmm4
+	pand	`16*($i+2)+256`(%r10),%xmm2
+	por	%xmm1,%xmm5
+	pand	`16*($i+3)+256`(%r10),%xmm3
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+___
+}
+$code.=<<___;
+	por	%xmm5,%xmm4
+	pshufd	\$0x4e,%xmm4,%xmm0
+	por	%xmm4,%xmm0
+	lea	$STRIDE($bptr),$bptr
+	movq	%xmm0,%rdx		# m0=bp[i]
+
 	mov	$zero,($tptr)		# save top-most carry
 	lea	4*8($tptr,$num),$tptr	# rewind $tptr
 	mulx	0*8($aptr),$mi,%r11	# a[0]*b[i]
 	xor	$zero,$zero		# cf=0, of=0
 	mov	%rdx,$bi
 	mulx	1*8($aptr),%r14,%r12	# a[1]*b[i]
 	adox	-4*8($tptr),$mi		# +t[0]
 	adcx	%r14,%r11
 	mulx	2*8($aptr),%r15,%r13	# ...
 	adox	-3*8($tptr),%r11
 	adcx	%r15,%r12
 	mulx	3*8($aptr),%rdx,%r14
 	adox	-2*8($tptr),%r12
 	adcx	%rdx,%r13
-	lea	($nptr,$num,2),$nptr	# rewind $nptr
+	lea	($nptr,$num),$nptr	# rewind $nptr
 	lea	4*8($aptr),$aptr
 	adox	-1*8($tptr),%r13
 	adcx	$zero,%r14
 	adox	$zero,%r14
 
-	.byte	0x67
 	mov	$mi,%r15
 	imulq	32+8(%rsp),$mi		# "t[0]"*n0
 
-	movq	`0*$STRIDE/4-96`($bptr),%xmm0
-	.byte	0x67,0x67
 	mov	$mi,%rdx
-	movq	`1*$STRIDE/4-96`($bptr),%xmm1
-	.byte	0x67
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-96`($bptr),%xmm2
-	.byte	0x67
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-96`($bptr),%xmm3
-	add	\$$STRIDE,$bptr		# next &b[i]
-	.byte	0x67
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
 	xor	$zero,$zero		# cf=0, of=0
 	mov	$bptr,8+8(%rsp)		# off-load &b[i]
 
-	mulx	0*16($nptr),%rax,%r10
+	mulx	0*8($nptr),%rax,%r10
 	adcx	%rax,%r15		# discarded
 	adox	%r11,%r10
-	mulx	1*16($nptr),%rax,%r11
+	mulx	1*8($nptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
-	mulx	2*16($nptr),%rax,%r12
+	mulx	2*8($nptr),%rax,%r12
 	adcx	%rax,%r11
 	adox	%r13,%r12
-	mulx	3*16($nptr),%rax,%r15
+	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
-	 por	%xmm2,%xmm0
 	mov	24+8(%rsp),$bptr	# counter value
 	mov	%r10,-8*4($tptr)
-	 por	%xmm3,%xmm0
 	adcx	%rax,%r12
 	mov	%r11,-8*3($tptr)
 	adox	$zero,%r15		# of=0
 	mov	%r12,-8*2($tptr)
-	lea	4*16($nptr),$nptr
+	lea	4*8($nptr),$nptr
 	jmp	.Lmulx4x_inner
 
 .align	32
 .Lmulx4x_inner:
 	mulx	0*8($aptr),%r10,%rax	# a[4]*b[i]
 	adcx	$zero,%r15		# cf=0, modulo-scheduled
 	adox	%r14,%r10
 	mulx	1*8($aptr),%r11,%r14	# a[5]*b[i]
 	adcx	0*8($tptr),%r10
 	adox	%rax,%r11
 	mulx	2*8($aptr),%r12,%rax	# ...
 	adcx	1*8($tptr),%r11
 	adox	%r14,%r12
 	mulx	3*8($aptr),%r13,%r14
 	 mov	$mi,%rdx
 	adcx	2*8($tptr),%r12
 	adox	%rax,%r13
 	adcx	3*8($tptr),%r13
 	adox	$zero,%r14		# of=0
 	lea	4*8($aptr),$aptr
 	lea	4*8($tptr),$tptr
 	adcx	$zero,%r14		# cf=0
 
 	adox	%r15,%r10
-	mulx	0*16($nptr),%rax,%r15
+	mulx	0*8($nptr),%rax,%r15
 	adcx	%rax,%r10
 	adox	%r15,%r11
-	mulx	1*16($nptr),%rax,%r15
+	mulx	1*8($nptr),%rax,%r15
 	adcx	%rax,%r11
 	adox	%r15,%r12
-	mulx	2*16($nptr),%rax,%r15
+	mulx	2*8($nptr),%rax,%r15
 	mov	%r10,-5*8($tptr)
 	adcx	%rax,%r12
 	adox	%r15,%r13
 	mov	%r11,-4*8($tptr)
-	mulx	3*16($nptr),%rax,%r15
+	mulx	3*8($nptr),%rax,%r15
 	 mov	$bi,%rdx
-	lea	4*16($nptr),$nptr
+	lea	4*8($nptr),$nptr
 	mov	%r12,-3*8($tptr)
 	adcx	%rax,%r13
 	adox	$zero,%r15
 	mov	%r13,-2*8($tptr)
 
 	dec	$bptr			# of=0, pass cf
 	jnz	.Lmulx4x_inner
 
 	mov	0+8(%rsp),$num		# load -num
-	movq	%xmm0,%rdx		# bp[i+1]
 	adc	$zero,%r15		# modulo-scheduled
 	sub	0*8($tptr),$bptr	# pull top-most carry to %cf
 	mov	8+8(%rsp),$bptr		# re-load &b[i]
 	mov	16+8(%rsp),%r10
 	adc	%r15,%r14
 	lea	($aptr,$num),$aptr	# rewind $aptr
 	adc	$zero,$zero		# top-most carry
 	mov	%r14,-1*8($tptr)
 
 	cmp	%r10,$bptr
 	jb	.Lmulx4x_outer
 
-	mov	-16($nptr),%r10
+	mov	-8($nptr),%r10
+	mov	$zero,%r8
+	mov	($nptr,$num),%r12
+	lea	($nptr,$num),%rbp	# rewind $nptr
+	mov	$num,%rcx
+	lea	($tptr,$num),%rdi	# rewind $tptr
+	xor	%eax,%eax
 	xor	%r15,%r15
 	sub	%r14,%r10		# compare top-most words
 	adc	%r15,%r15
-	or	%r15,$zero
-	xor	\$1,$zero
-	lea	($tptr,$num),%rdi	# rewind $tptr
-	lea	($nptr,$num,2),$nptr	# rewind $nptr
-	.byte	0x67,0x67
-	sar	\$3+2,$num		# cf=0
-	lea	($nptr,$zero,8),%rbp
+	or	%r15,%r8
+	sar	\$3+2,%rcx
+	sub	%r8,%rax		# %rax=-%r8
 	mov	56+8(%rsp),%rdx		# restore rp
-	mov	$num,%rcx
-	jmp	.Lsqrx4x_sub		# common post-condition
+	dec	%r12			# so that after 'not' we get -n[0]
+	mov	8*1(%rbp),%r13
+	xor	%r8,%r8
+	mov	8*2(%rbp),%r14
+	mov	8*3(%rbp),%r15
+	jmp	.Lsqrx4x_sub_entry	# common post-condition
 .size	mulx4x_internal,.-mulx4x_internal
 ___
 }{
 ######################################################################
 # void bn_power5(
 my $rptr="%rdi";	# BN_ULONG *rptr,
 my $aptr="%rsi";	# const BN_ULONG *aptr,
 my $bptr="%rdx";	# const void *table,
 my $nptr="%rcx";	# const BN_ULONG *nptr,
 my $n0  ="%r8";		# const BN_ULONG *n0);
 my $num ="%r9";		# int num, has to be divisible by 8
 			# int pwr);
 
 my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
 my @A0=("%r10","%r11");
 my @A1=("%r12","%r13");
 my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
 
 $code.=<<___;
 .type	bn_powerx5,\@function,6
 .align	32
 bn_powerx5:
 .Lpowerx5_enter:
-	.byte	0x67
 	mov	%rsp,%rax
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
-___
-$code.=<<___ if ($win64);
-	lea	-0x28(%rsp),%rsp
-	movaps	%xmm6,(%rsp)
-	movaps	%xmm7,0x10(%rsp)
-___
-$code.=<<___;
-	.byte	0x67
-	mov	${num}d,%r10d
+
 	shl	\$3,${num}d		# convert $num to bytes
-	shl	\$3+2,%r10d		# 4*$num
+	lea	($num,$num,2),%r10	# 3*$num in bytes
 	neg	$num
 	mov	($n0),$n0		# *n0
 
 	##############################################################
-	# ensure that stack frame doesn't alias with $aptr+4*$num
-	# modulo 4096, which covers ret[num], am[num] and n[2*num]
-	# (see bn_exp.c). this is done to allow memory disambiguation
-	# logic do its magic.
+	# Ensure that stack frame doesn't alias with $rptr+3*$num
+	# modulo 4096, which covers ret[num], am[num] and n[num]
+	# (see bn_exp.c). This is done to allow memory disambiguation
+	# logic do its magic. [Extra 256 bytes is for power mask
+	# calculated from 7th argument, the index.]
 	#
-	lea	-64(%rsp,$num,2),%r11
-	sub	$aptr,%r11
+	lea	-320(%rsp,$num,2),%r11
+	sub	$rptr,%r11
 	and	\$4095,%r11
 	cmp	%r11,%r10
 	jb	.Lpwrx_sp_alt
 	sub	%r11,%rsp		# align with $aptr
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	jmp	.Lpwrx_sp_done
 
 .align	32
 .Lpwrx_sp_alt:
-	lea	4096-64(,$num,2),%r10	# 4096-frame-2*$num
-	lea	-64(%rsp,$num,2),%rsp	# alloca(frame+2*$num)
+	lea	4096-320(,$num,2),%r10
+	lea	-320(%rsp,$num,2),%rsp	# alloca(frame+2*$num*8+256)
 	sub	%r10,%r11
 	mov	\$0,%r10
 	cmovc	%r10,%r11
 	sub	%r11,%rsp
 .Lpwrx_sp_done:
 	and	\$-64,%rsp
 	mov	$num,%r10	
 	neg	$num
 
 	##############################################################
 	# Stack layout
 	#
 	# +0	saved $num, used in reduction section
 	# +8	&t[2*$num], used in reduction section
 	# +16	intermediate carry bit
 	# +24	top-most carry bit, used in reduction section
 	# +32	saved *n0
 	# +40	saved %rsp
 	# +48	t[2*$num]
 	#
 	pxor	%xmm0,%xmm0
 	movq	$rptr,%xmm1		# save $rptr
 	movq	$nptr,%xmm2		# save $nptr
 	movq	%r10, %xmm3		# -$num
 	movq	$bptr,%xmm4
 	mov	$n0,  32(%rsp)
 	mov	%rax, 40(%rsp)		# save original %rsp
 .Lpowerx5_body:
 
 	call	__bn_sqrx8x_internal
+	call	__bn_postx4x_internal
 	call	__bn_sqrx8x_internal
+	call	__bn_postx4x_internal
 	call	__bn_sqrx8x_internal
+	call	__bn_postx4x_internal
 	call	__bn_sqrx8x_internal
+	call	__bn_postx4x_internal
 	call	__bn_sqrx8x_internal
+	call	__bn_postx4x_internal
 
 	mov	%r10,$num		# -num
 	mov	$aptr,$rptr
 	movq	%xmm2,$nptr
 	movq	%xmm4,$bptr
 	mov	40(%rsp),%rax
 
 	call	mulx4x_internal
 
 	mov	40(%rsp),%rsi		# restore %rsp
 	mov	\$1,%rax
-___
-$code.=<<___ if ($win64);
-	movaps	-88(%rsi),%xmm6
-	movaps	-72(%rsi),%xmm7
-___
-$code.=<<___;
+
 	mov	-48(%rsi),%r15
 	mov	-40(%rsi),%r14
 	mov	-32(%rsi),%r13
 	mov	-24(%rsi),%r12
 	mov	-16(%rsi),%rbp
 	mov	-8(%rsi),%rbx
 	lea	(%rsi),%rsp
 .Lpowerx5_epilogue:
 	ret
 .size	bn_powerx5,.-bn_powerx5
 
 .globl	bn_sqrx8x_internal
 .hidden	bn_sqrx8x_internal
 .type	bn_sqrx8x_internal,\@abi-omnipotent
 .align	32
 bn_sqrx8x_internal:
 __bn_sqrx8x_internal:
 	##################################################################
 	# Squaring part:
 	#
 	# a) multiply-n-add everything but a[i]*a[i];
 	# b) shift result of a) by 1 to the left and accumulate
 	#    a[i]*a[i] products;
 	#
 	##################################################################
 	# a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
 	#                                                     a[1]a[0]
 	#                                                 a[2]a[0]
 	#                                             a[3]a[0]
 	#                                             a[2]a[1]
 	#                                         a[3]a[1]
 	#                                     a[3]a[2]
 	#
 	#                                         a[4]a[0]
 	#                                     a[5]a[0]
 	#                                 a[6]a[0]
 	#                             a[7]a[0]
 	#                                     a[4]a[1]
 	#                                 a[5]a[1]
 	#                             a[6]a[1]
 	#                         a[7]a[1]
 	#                                 a[4]a[2]
 	#                             a[5]a[2]
 	#                         a[6]a[2]
 	#                     a[7]a[2]
 	#                             a[4]a[3]
 	#                         a[5]a[3]
 	#                     a[6]a[3]
 	#                 a[7]a[3]
 	#
 	#                     a[5]a[4]
 	#                 a[6]a[4]
 	#             a[7]a[4]
 	#             a[6]a[5]
 	#         a[7]a[5]
 	#     a[7]a[6]
 	# a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
 ___
 {
 my ($zero,$carry)=("%rbp","%rcx");
 my $aaptr=$zero;
 $code.=<<___;
 	lea	48+8(%rsp),$tptr
 	lea	($aptr,$num),$aaptr
 	mov	$num,0+8(%rsp)			# save $num
 	mov	$aaptr,8+8(%rsp)		# save end of $aptr
 	jmp	.Lsqr8x_zero_start
 
 .align	32
 .byte	0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
 .Lsqrx8x_zero:
 	.byte	0x3e
 	movdqa	%xmm0,0*8($tptr)
 	movdqa	%xmm0,2*8($tptr)
 	movdqa	%xmm0,4*8($tptr)
 	movdqa	%xmm0,6*8($tptr)
 .Lsqr8x_zero_start:			# aligned at 32
 	movdqa	%xmm0,8*8($tptr)
 	movdqa	%xmm0,10*8($tptr)
 	movdqa	%xmm0,12*8($tptr)
 	movdqa	%xmm0,14*8($tptr)
 	lea	16*8($tptr),$tptr
 	sub	\$64,$num
 	jnz	.Lsqrx8x_zero
 
 	mov	0*8($aptr),%rdx		# a[0], modulo-scheduled
 	#xor	%r9,%r9			# t[1], ex-$num, zero already
 	xor	%r10,%r10
 	xor	%r11,%r11
 	xor	%r12,%r12
 	xor	%r13,%r13
 	xor	%r14,%r14
 	xor	%r15,%r15
 	lea	48+8(%rsp),$tptr
 	xor	$zero,$zero		# cf=0, cf=0
 	jmp	.Lsqrx8x_outer_loop
 
 .align	32
 .Lsqrx8x_outer_loop:
 	mulx	1*8($aptr),%r8,%rax	# a[1]*a[0]
 	adcx	%r9,%r8			# a[1]*a[0]+=t[1]
 	adox	%rax,%r10
 	mulx	2*8($aptr),%r9,%rax	# a[2]*a[0]
 	adcx	%r10,%r9
 	adox	%rax,%r11
 	.byte	0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00	# mulx	3*8($aptr),%r10,%rax	# ...
 	adcx	%r11,%r10
 	adox	%rax,%r12
 	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00	# mulx	4*8($aptr),%r11,%rax
 	adcx	%r12,%r11
 	adox	%rax,%r13
 	mulx	5*8($aptr),%r12,%rax
 	adcx	%r13,%r12
 	adox	%rax,%r14
 	mulx	6*8($aptr),%r13,%rax
 	adcx	%r14,%r13
 	adox	%r15,%rax
 	mulx	7*8($aptr),%r14,%r15
 	 mov	1*8($aptr),%rdx		# a[1]
 	adcx	%rax,%r14
 	adox	$zero,%r15
 	adc	8*8($tptr),%r15
 	mov	%r8,1*8($tptr)		# t[1]
 	mov	%r9,2*8($tptr)		# t[2]
 	sbb	$carry,$carry		# mov %cf,$carry
 	xor	$zero,$zero		# cf=0, of=0
 
 
 	mulx	2*8($aptr),%r8,%rbx	# a[2]*a[1]
 	mulx	3*8($aptr),%r9,%rax	# a[3]*a[1]
 	adcx	%r10,%r8
 	adox	%rbx,%r9
 	mulx	4*8($aptr),%r10,%rbx	# ...
 	adcx	%r11,%r9
 	adox	%rax,%r10
 	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00	# mulx	5*8($aptr),%r11,%rax
 	adcx	%r12,%r10
 	adox	%rbx,%r11
 	.byte	0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00	# mulx	6*8($aptr),%r12,%rbx
 	adcx	%r13,%r11
 	adox	%r14,%r12
 	.byte	0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00	# mulx	7*8($aptr),%r13,%r14
 	 mov	2*8($aptr),%rdx		# a[2]
 	adcx	%rax,%r12
 	adox	%rbx,%r13
 	adcx	%r15,%r13
 	adox	$zero,%r14		# of=0
 	adcx	$zero,%r14		# cf=0
 
 	mov	%r8,3*8($tptr)		# t[3]
 	mov	%r9,4*8($tptr)		# t[4]
 
 	mulx	3*8($aptr),%r8,%rbx	# a[3]*a[2]
 	mulx	4*8($aptr),%r9,%rax	# a[4]*a[2]
 	adcx	%r10,%r8
 	adox	%rbx,%r9
 	mulx	5*8($aptr),%r10,%rbx	# ...
 	adcx	%r11,%r9
 	adox	%rax,%r10
 	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00	# mulx	6*8($aptr),%r11,%rax
 	adcx	%r12,%r10
 	adox	%r13,%r11
 	.byte	0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00	# mulx	7*8($aptr),%r12,%r13
 	.byte	0x3e
 	 mov	3*8($aptr),%rdx		# a[3]
 	adcx	%rbx,%r11
 	adox	%rax,%r12
 	adcx	%r14,%r12
 	mov	%r8,5*8($tptr)		# t[5]
 	mov	%r9,6*8($tptr)		# t[6]
 	 mulx	4*8($aptr),%r8,%rax	# a[4]*a[3]
 	adox	$zero,%r13		# of=0
 	adcx	$zero,%r13		# cf=0
 
 	mulx	5*8($aptr),%r9,%rbx	# a[5]*a[3]
 	adcx	%r10,%r8
 	adox	%rax,%r9
 	mulx	6*8($aptr),%r10,%rax	# ...
 	adcx	%r11,%r9
 	adox	%r12,%r10
 	mulx	7*8($aptr),%r11,%r12
 	 mov	4*8($aptr),%rdx		# a[4]
 	 mov	5*8($aptr),%r14		# a[5]
 	adcx	%rbx,%r10
 	adox	%rax,%r11
 	 mov	6*8($aptr),%r15		# a[6]
 	adcx	%r13,%r11
 	adox	$zero,%r12		# of=0
 	adcx	$zero,%r12		# cf=0
 
 	mov	%r8,7*8($tptr)		# t[7]
 	mov	%r9,8*8($tptr)		# t[8]
 
 	mulx	%r14,%r9,%rax		# a[5]*a[4]
 	 mov	7*8($aptr),%r8		# a[7]
 	adcx	%r10,%r9
 	mulx	%r15,%r10,%rbx		# a[6]*a[4]
 	adox	%rax,%r10
 	adcx	%r11,%r10
 	mulx	%r8,%r11,%rax		# a[7]*a[4]
 	 mov	%r14,%rdx		# a[5]
 	adox	%rbx,%r11
 	adcx	%r12,%r11
 	#adox	$zero,%rax		# of=0
 	adcx	$zero,%rax		# cf=0
 
 	mulx	%r15,%r14,%rbx		# a[6]*a[5]
 	mulx	%r8,%r12,%r13		# a[7]*a[5]
 	 mov	%r15,%rdx		# a[6]
 	 lea	8*8($aptr),$aptr
 	adcx	%r14,%r11
 	adox	%rbx,%r12
 	adcx	%rax,%r12
 	adox	$zero,%r13
 
 	.byte	0x67,0x67
 	mulx	%r8,%r8,%r14		# a[7]*a[6]
 	adcx	%r8,%r13
 	adcx	$zero,%r14
 
 	cmp	8+8(%rsp),$aptr
 	je	.Lsqrx8x_outer_break
 
 	neg	$carry			# mov $carry,%cf
 	mov	\$-8,%rcx
 	mov	$zero,%r15
 	mov	8*8($tptr),%r8
 	adcx	9*8($tptr),%r9		# +=t[9]
 	adcx	10*8($tptr),%r10	# ...
 	adcx	11*8($tptr),%r11
 	adc	12*8($tptr),%r12
 	adc	13*8($tptr),%r13
 	adc	14*8($tptr),%r14
 	adc	15*8($tptr),%r15
 	lea	($aptr),$aaptr
 	lea	2*64($tptr),$tptr
 	sbb	%rax,%rax		# mov %cf,$carry
 
 	mov	-64($aptr),%rdx		# a[0]
 	mov	%rax,16+8(%rsp)		# offload $carry
 	mov	$tptr,24+8(%rsp)
 
 	#lea	8*8($tptr),$tptr	# see 2*8*8($tptr) above
 	xor	%eax,%eax		# cf=0, of=0
 	jmp	.Lsqrx8x_loop
 
 .align	32
 .Lsqrx8x_loop:
 	mov	%r8,%rbx
 	mulx	0*8($aaptr),%rax,%r8	# a[8]*a[i]
 	adcx	%rax,%rbx		# +=t[8]
 	adox	%r9,%r8
 
 	mulx	1*8($aaptr),%rax,%r9	# ...
 	adcx	%rax,%r8
 	adox	%r10,%r9
 
 	mulx	2*8($aaptr),%rax,%r10
 	adcx	%rax,%r9
 	adox	%r11,%r10
 
 	mulx	3*8($aaptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	4*8($aaptr),%rax,%r12
 	adcx	%rax,%r11
 	adox	%r13,%r12
 
 	mulx	5*8($aaptr),%rax,%r13
 	adcx	%rax,%r12
 	adox	%r14,%r13
 
 	mulx	6*8($aaptr),%rax,%r14
 	 mov	%rbx,($tptr,%rcx,8)	# store t[8+i]
 	 mov	\$0,%ebx
 	adcx	%rax,%r13
 	adox	%r15,%r14
 
 	.byte	0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00	# mulx	7*8($aaptr),%rax,%r15
 	 mov	8($aptr,%rcx,8),%rdx	# a[i]
 	adcx	%rax,%r14
 	adox	%rbx,%r15		# %rbx is 0, of=0
 	adcx	%rbx,%r15		# cf=0
 
 	.byte	0x67
 	inc	%rcx			# of=0
 	jnz	.Lsqrx8x_loop
 
 	lea	8*8($aaptr),$aaptr
 	mov	\$-8,%rcx
 	cmp	8+8(%rsp),$aaptr	# done?
 	je	.Lsqrx8x_break
 
 	sub	16+8(%rsp),%rbx		# mov 16(%rsp),%cf
 	.byte	0x66
 	mov	-64($aptr),%rdx
 	adcx	0*8($tptr),%r8
 	adcx	1*8($tptr),%r9
 	adc	2*8($tptr),%r10
 	adc	3*8($tptr),%r11
 	adc	4*8($tptr),%r12
 	adc	5*8($tptr),%r13
 	adc	6*8($tptr),%r14
 	adc	7*8($tptr),%r15
 	lea	8*8($tptr),$tptr
 	.byte	0x67
 	sbb	%rax,%rax		# mov %cf,%rax
 	xor	%ebx,%ebx		# cf=0, of=0
 	mov	%rax,16+8(%rsp)		# offload carry
 	jmp	.Lsqrx8x_loop
 
 .align	32
 .Lsqrx8x_break:
 	sub	16+8(%rsp),%r8		# consume last carry
 	mov	24+8(%rsp),$carry	# initial $tptr, borrow $carry
 	mov	0*8($aptr),%rdx		# a[8], modulo-scheduled
 	xor	%ebp,%ebp		# xor	$zero,$zero
 	mov	%r8,0*8($tptr)
 	cmp	$carry,$tptr		# cf=0, of=0
 	je	.Lsqrx8x_outer_loop
 
 	mov	%r9,1*8($tptr)
 	 mov	1*8($carry),%r9
 	mov	%r10,2*8($tptr)
 	 mov	2*8($carry),%r10
 	mov	%r11,3*8($tptr)
 	 mov	3*8($carry),%r11
 	mov	%r12,4*8($tptr)
 	 mov	4*8($carry),%r12
 	mov	%r13,5*8($tptr)
 	 mov	5*8($carry),%r13
 	mov	%r14,6*8($tptr)
 	 mov	6*8($carry),%r14
 	mov	%r15,7*8($tptr)
 	 mov	7*8($carry),%r15
 	mov	$carry,$tptr
 	jmp	.Lsqrx8x_outer_loop
 
 .align	32
 .Lsqrx8x_outer_break:
 	mov	%r9,9*8($tptr)		# t[9]
 	 movq	%xmm3,%rcx		# -$num
 	mov	%r10,10*8($tptr)	# ...
 	mov	%r11,11*8($tptr)
 	mov	%r12,12*8($tptr)
 	mov	%r13,13*8($tptr)
 	mov	%r14,14*8($tptr)
 ___
 }{
 my $i="%rcx";
 $code.=<<___;
 	lea	48+8(%rsp),$tptr
 	mov	($aptr,$i),%rdx		# a[0]
 
 	mov	8($tptr),$A0[1]		# t[1]
 	xor	$A0[0],$A0[0]		# t[0], of=0, cf=0
 	mov	0+8(%rsp),$num		# restore $num
 	adox	$A0[1],$A0[1]
 	 mov	16($tptr),$A1[0]	# t[2]	# prefetch
 	 mov	24($tptr),$A1[1]	# t[3]	# prefetch
 	#jmp	.Lsqrx4x_shift_n_add	# happens to be aligned
 
 .align	32
 .Lsqrx4x_shift_n_add:
 	mulx	%rdx,%rax,%rbx
 	 adox	$A1[0],$A1[0]
 	adcx	$A0[0],%rax
 	 .byte	0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00	# mov	8($aptr,$i),%rdx	# a[i+1]	# prefetch
 	 .byte	0x4c,0x8b,0x97,0x20,0x00,0x00,0x00	# mov	32($tptr),$A0[0]	# t[2*i+4]	# prefetch
 	 adox	$A1[1],$A1[1]
 	adcx	$A0[1],%rbx
 	 mov	40($tptr),$A0[1]		# t[2*i+4+1]	# prefetch
 	mov	%rax,0($tptr)
 	mov	%rbx,8($tptr)
 
 	mulx	%rdx,%rax,%rbx
 	 adox	$A0[0],$A0[0]
 	adcx	$A1[0],%rax
 	 mov	16($aptr,$i),%rdx	# a[i+2]	# prefetch
 	 mov	48($tptr),$A1[0]	# t[2*i+6]	# prefetch
 	 adox	$A0[1],$A0[1]
 	adcx	$A1[1],%rbx
 	 mov	56($tptr),$A1[1]	# t[2*i+6+1]	# prefetch
 	mov	%rax,16($tptr)
 	mov	%rbx,24($tptr)
 
 	mulx	%rdx,%rax,%rbx
 	 adox	$A1[0],$A1[0]
 	adcx	$A0[0],%rax
 	 mov	24($aptr,$i),%rdx	# a[i+3]	# prefetch
 	 lea	32($i),$i
 	 mov	64($tptr),$A0[0]	# t[2*i+8]	# prefetch
 	 adox	$A1[1],$A1[1]
 	adcx	$A0[1],%rbx
 	 mov	72($tptr),$A0[1]	# t[2*i+8+1]	# prefetch
 	mov	%rax,32($tptr)
 	mov	%rbx,40($tptr)
 
 	mulx	%rdx,%rax,%rbx
 	 adox	$A0[0],$A0[0]
 	adcx	$A1[0],%rax
 	jrcxz	.Lsqrx4x_shift_n_add_break
 	 .byte	0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00	# mov	0($aptr,$i),%rdx	# a[i+4]	# prefetch
 	 adox	$A0[1],$A0[1]
 	adcx	$A1[1],%rbx
 	 mov	80($tptr),$A1[0]	# t[2*i+10]	# prefetch
 	 mov	88($tptr),$A1[1]	# t[2*i+10+1]	# prefetch
 	mov	%rax,48($tptr)
 	mov	%rbx,56($tptr)
 	lea	64($tptr),$tptr
 	nop
 	jmp	.Lsqrx4x_shift_n_add
 
 .align	32
 .Lsqrx4x_shift_n_add_break:
 	adcx	$A1[1],%rbx
 	mov	%rax,48($tptr)
 	mov	%rbx,56($tptr)
 	lea	64($tptr),$tptr		# end of t[] buffer
 ___
 }
 ######################################################################
 # Montgomery reduction part, "word-by-word" algorithm.
 #
 # This new path is inspired by multiple submissions from Intel, by
 # Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
 # Vinodh Gopal...
 {
 my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
 
 $code.=<<___;
 	movq	%xmm2,$nptr
-sqrx8x_reduction:
+__bn_sqrx8x_reduction:
 	xor	%eax,%eax		# initial top-most carry bit
 	mov	32+8(%rsp),%rbx		# n0
 	mov	48+8(%rsp),%rdx		# "%r8", 8*0($tptr)
-	lea	-128($nptr,$num,2),%rcx	# end of n[]
+	lea	-8*8($nptr,$num),%rcx	# end of n[]
 	#lea	48+8(%rsp,$num,2),$tptr	# end of t[] buffer
 	mov	%rcx, 0+8(%rsp)		# save end of n[]
 	mov	$tptr,8+8(%rsp)		# save end of t[]
 
 	lea	48+8(%rsp),$tptr		# initial t[] window
 	jmp	.Lsqrx8x_reduction_loop
 
 .align	32
 .Lsqrx8x_reduction_loop:
 	mov	8*1($tptr),%r9
 	mov	8*2($tptr),%r10
 	mov	8*3($tptr),%r11
 	mov	8*4($tptr),%r12
 	mov	%rdx,%r8
 	imulq	%rbx,%rdx		# n0*a[i]
 	mov	8*5($tptr),%r13
 	mov	8*6($tptr),%r14
 	mov	8*7($tptr),%r15
 	mov	%rax,24+8(%rsp)		# store top-most carry bit
 
 	lea	8*8($tptr),$tptr
 	xor	$carry,$carry		# cf=0,of=0
 	mov	\$-8,%rcx
 	jmp	.Lsqrx8x_reduce
 
 .align	32
 .Lsqrx8x_reduce:
 	mov	%r8, %rbx
-	mulx	16*0($nptr),%rax,%r8	# n[0]
+	mulx	8*0($nptr),%rax,%r8	# n[0]
 	adcx	%rbx,%rax		# discarded
 	adox	%r9,%r8
 
-	mulx	16*1($nptr),%rbx,%r9	# n[1]
+	mulx	8*1($nptr),%rbx,%r9	# n[1]
 	adcx	%rbx,%r8
 	adox	%r10,%r9
 
-	mulx	16*2($nptr),%rbx,%r10
+	mulx	8*2($nptr),%rbx,%r10
 	adcx	%rbx,%r9
 	adox	%r11,%r10
 
-	mulx	16*3($nptr),%rbx,%r11
+	mulx	8*3($nptr),%rbx,%r11
 	adcx	%rbx,%r10
 	adox	%r12,%r11
 
-	.byte	0xc4,0x62,0xe3,0xf6,0xa5,0x40,0x00,0x00,0x00	# mulx	16*4($nptr),%rbx,%r12
+	.byte	0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	8*4($nptr),%rbx,%r12
 	 mov	%rdx,%rax
 	 mov	%r8,%rdx
 	adcx	%rbx,%r11
 	adox	%r13,%r12
 
 	 mulx	32+8(%rsp),%rbx,%rdx	# %rdx discarded
 	 mov	%rax,%rdx
 	 mov	%rax,64+48+8(%rsp,%rcx,8)	# put aside n0*a[i]
 
-	mulx	16*5($nptr),%rax,%r13
+	mulx	8*5($nptr),%rax,%r13
 	adcx	%rax,%r12
 	adox	%r14,%r13
 
-	mulx	16*6($nptr),%rax,%r14
+	mulx	8*6($nptr),%rax,%r14
 	adcx	%rax,%r13
 	adox	%r15,%r14
 
-	mulx	16*7($nptr),%rax,%r15
+	mulx	8*7($nptr),%rax,%r15
 	 mov	%rbx,%rdx
 	adcx	%rax,%r14
 	adox	$carry,%r15		# $carry is 0
 	adcx	$carry,%r15		# cf=0
 
 	.byte	0x67,0x67,0x67
 	inc	%rcx			# of=0
 	jnz	.Lsqrx8x_reduce
 
 	mov	$carry,%rax		# xor	%rax,%rax
 	cmp	0+8(%rsp),$nptr		# end of n[]?
 	jae	.Lsqrx8x_no_tail
 
 	mov	48+8(%rsp),%rdx		# pull n0*a[0]
 	add	8*0($tptr),%r8
-	lea	16*8($nptr),$nptr
+	lea	8*8($nptr),$nptr
 	mov	\$-8,%rcx
 	adcx	8*1($tptr),%r9
 	adcx	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	lea	8*8($tptr),$tptr
 	sbb	%rax,%rax		# top carry
 
 	xor	$carry,$carry		# of=0, cf=0
 	mov	%rax,16+8(%rsp)
 	jmp	.Lsqrx8x_tail
 
 .align	32
 .Lsqrx8x_tail:
 	mov	%r8,%rbx
-	mulx	16*0($nptr),%rax,%r8
+	mulx	8*0($nptr),%rax,%r8
 	adcx	%rax,%rbx
 	adox	%r9,%r8
 
-	mulx	16*1($nptr),%rax,%r9
+	mulx	8*1($nptr),%rax,%r9
 	adcx	%rax,%r8
 	adox	%r10,%r9
 
-	mulx	16*2($nptr),%rax,%r10
+	mulx	8*2($nptr),%rax,%r10
 	adcx	%rax,%r9
 	adox	%r11,%r10
 
-	mulx	16*3($nptr),%rax,%r11
+	mulx	8*3($nptr),%rax,%r11
 	adcx	%rax,%r10
 	adox	%r12,%r11
 
-	.byte	0xc4,0x62,0xfb,0xf6,0xa5,0x40,0x00,0x00,0x00	# mulx	16*4($nptr),%rax,%r12
+	.byte	0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	8*4($nptr),%rax,%r12
 	adcx	%rax,%r11
 	adox	%r13,%r12
 
-	mulx	16*5($nptr),%rax,%r13
+	mulx	8*5($nptr),%rax,%r13
 	adcx	%rax,%r12
 	adox	%r14,%r13
 
-	mulx	16*6($nptr),%rax,%r14
+	mulx	8*6($nptr),%rax,%r14
 	adcx	%rax,%r13
 	adox	%r15,%r14
 
-	mulx	16*7($nptr),%rax,%r15
+	mulx	8*7($nptr),%rax,%r15
 	 mov	72+48+8(%rsp,%rcx,8),%rdx	# pull n0*a[i]
 	adcx	%rax,%r14
 	adox	$carry,%r15
 	 mov	%rbx,($tptr,%rcx,8)	# save result
 	 mov	%r8,%rbx
 	adcx	$carry,%r15		# cf=0
 
 	inc	%rcx			# of=0
 	jnz	.Lsqrx8x_tail
 
 	cmp	0+8(%rsp),$nptr		# end of n[]?
 	jae	.Lsqrx8x_tail_done	# break out of loop
 
 	sub	16+8(%rsp),$carry	# mov 16(%rsp),%cf
 	 mov	48+8(%rsp),%rdx		# pull n0*a[0]
-	 lea	16*8($nptr),$nptr
+	 lea	8*8($nptr),$nptr
 	adc	8*0($tptr),%r8
 	adc	8*1($tptr),%r9
 	adc	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	lea	8*8($tptr),$tptr
 	sbb	%rax,%rax
 	sub	\$8,%rcx		# mov	\$-8,%rcx
 
 	xor	$carry,$carry		# of=0, cf=0
 	mov	%rax,16+8(%rsp)
 	jmp	.Lsqrx8x_tail
 
 .align	32
 .Lsqrx8x_tail_done:
 	add	24+8(%rsp),%r8		# can this overflow?
 	adc	\$0,%r9
 	adc	\$0,%r10
 	adc	\$0,%r11
 	adc	\$0,%r12
 	adc	\$0,%r13
 	adc	\$0,%r14
 	adc	\$0,%r15		# can't overflow, because we
 					# started with "overhung" part
 					# of multiplication
 	mov	$carry,%rax		# xor	%rax,%rax
 
 	sub	16+8(%rsp),$carry	# mov 16(%rsp),%cf
 .Lsqrx8x_no_tail:			# %cf is 0 if jumped here
 	adc	8*0($tptr),%r8
 	 movq	%xmm3,%rcx
 	adc	8*1($tptr),%r9
-	 mov	16*7($nptr),$carry
+	 mov	8*7($nptr),$carry
 	 movq	%xmm2,$nptr		# restore $nptr
 	adc	8*2($tptr),%r10
 	adc	8*3($tptr),%r11
 	adc	8*4($tptr),%r12
 	adc	8*5($tptr),%r13
 	adc	8*6($tptr),%r14
 	adc	8*7($tptr),%r15
 	adc	%rax,%rax		# top-most carry
 
 	mov	32+8(%rsp),%rbx		# n0
 	mov	8*8($tptr,%rcx),%rdx	# modulo-scheduled "%r8"
 
 	mov	%r8,8*0($tptr)		# store top 512 bits
 	 lea	8*8($tptr),%r8		# borrow %r8
 	mov	%r9,8*1($tptr)
 	mov	%r10,8*2($tptr)
 	mov	%r11,8*3($tptr)
 	mov	%r12,8*4($tptr)
 	mov	%r13,8*5($tptr)
 	mov	%r14,8*6($tptr)
 	mov	%r15,8*7($tptr)
 
 	lea	8*8($tptr,%rcx),$tptr	# start of current t[] window
 	cmp	8+8(%rsp),%r8		# end of t[]?
 	jb	.Lsqrx8x_reduction_loop
+	ret
+.size	bn_sqrx8x_internal,.-bn_sqrx8x_internal
 ___
 }
 ##############################################################
 # Post-condition, 4x unrolled
 #
 {
 my ($rptr,$nptr)=("%rdx","%rbp");
-my @ri=map("%r$_",(10..13));
-my @ni=map("%r$_",(14..15));
 $code.=<<___;
-	xor	%ebx,%ebx
-	sub	%r15,%rsi		# compare top-most words
-	adc	%rbx,%rbx
+.align	32
+__bn_postx4x_internal:
+	mov	8*0($nptr),%r12
 	mov	%rcx,%r10		# -$num
-	or	%rbx,%rax
 	mov	%rcx,%r9		# -$num
-	xor	\$1,%rax
-	sar	\$3+2,%rcx		# cf=0
+	neg	%rax
+	sar	\$3+2,%rcx
 	#lea	48+8(%rsp,%r9),$tptr
-	lea	($nptr,%rax,8),$nptr
 	movq	%xmm1,$rptr		# restore $rptr
 	movq	%xmm1,$aptr		# prepare for back-to-back call
-	jmp	.Lsqrx4x_sub
+	dec	%r12			# so that after 'not' we get -n[0]
+	mov	8*1($nptr),%r13
+	xor	%r8,%r8
+	mov	8*2($nptr),%r14
+	mov	8*3($nptr),%r15
+	jmp	.Lsqrx4x_sub_entry
 
-.align	32
+.align	16
 .Lsqrx4x_sub:
-	.byte	0x66
-	mov	8*0($tptr),%r12
-	mov	8*1($tptr),%r13
-	sbb	16*0($nptr),%r12
-	mov	8*2($tptr),%r14
-	sbb	16*1($nptr),%r13
-	mov	8*3($tptr),%r15
-	lea	8*4($tptr),$tptr
-	sbb	16*2($nptr),%r14
+	mov	8*0($nptr),%r12
+	mov	8*1($nptr),%r13
+	mov	8*2($nptr),%r14
+	mov	8*3($nptr),%r15
+.Lsqrx4x_sub_entry:
+	andn	%rax,%r12,%r12
+	lea	8*4($nptr),$nptr
+	andn	%rax,%r13,%r13
+	andn	%rax,%r14,%r14
+	andn	%rax,%r15,%r15
+
+	neg	%r8			# mov %r8,%cf
+	adc	8*0($tptr),%r12
+	adc	8*1($tptr),%r13
+	adc	8*2($tptr),%r14
+	adc	8*3($tptr),%r15
 	mov	%r12,8*0($rptr)
-	sbb	16*3($nptr),%r15
-	lea	16*4($nptr),$nptr
+	lea	8*4($tptr),$tptr
 	mov	%r13,8*1($rptr)
+	sbb	%r8,%r8			# mov %cf,%r8
 	mov	%r14,8*2($rptr)
 	mov	%r15,8*3($rptr)
 	lea	8*4($rptr),$rptr
 
 	inc	%rcx
 	jnz	.Lsqrx4x_sub
-___
-}
-$code.=<<___;
+
 	neg	%r9			# restore $num
 
 	ret
-.size	bn_sqrx8x_internal,.-bn_sqrx8x_internal
+.size	__bn_postx4x_internal,.-__bn_postx4x_internal
 ___
+}
 }}}
 {
 my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
 				("%rdi","%esi","%rdx","%ecx");  # Unix order
 my $out=$inp;
 my $STRIDE=2**5*8;
 my $N=$STRIDE/4;
 
 $code.=<<___;
 .globl	bn_get_bits5
 .type	bn_get_bits5,\@abi-omnipotent
 .align	16
 bn_get_bits5:
 	lea	0($inp),%r10
 	lea	1($inp),%r11
 	mov	$num,%ecx
 	shr	\$4,$num
 	and	\$15,%ecx
 	lea	-8(%ecx),%eax
 	cmp	\$11,%ecx
 	cmova	%r11,%r10
 	cmova	%eax,%ecx
 	movzw	(%r10,$num,2),%eax
 	shrl	%cl,%eax
 	and	\$31,%eax
 	ret
 .size	bn_get_bits5,.-bn_get_bits5
 
 .globl	bn_scatter5
 .type	bn_scatter5,\@abi-omnipotent
 .align	16
 bn_scatter5:
 	cmp	\$0, $num
 	jz	.Lscatter_epilogue
 	lea	($tbl,$idx,8),$tbl
 .Lscatter:
 	mov	($inp),%rax
 	lea	8($inp),$inp
 	mov	%rax,($tbl)
 	lea	32*8($tbl),$tbl
 	sub	\$1,$num
 	jnz	.Lscatter
 .Lscatter_epilogue:
 	ret
 .size	bn_scatter5,.-bn_scatter5
 
 .globl	bn_gather5
 .type	bn_gather5,\@abi-omnipotent
-.align	16
+.align	32
 bn_gather5:
-___
-$code.=<<___ if ($win64);
-.LSEH_begin_bn_gather5:
+.LSEH_begin_bn_gather5:			# Win64 thing, but harmless in other cases
 	# I can't trust assembler to use specific encoding:-(
-	.byte	0x48,0x83,0xec,0x28		#sub	\$0x28,%rsp
-	.byte	0x0f,0x29,0x34,0x24		#movaps	%xmm6,(%rsp)
-	.byte	0x0f,0x29,0x7c,0x24,0x10	#movdqa	%xmm7,0x10(%rsp)
+	.byte	0x4c,0x8d,0x14,0x24			#lea    (%rsp),%r10
+	.byte	0x48,0x81,0xec,0x08,0x01,0x00,0x00	#sub	$0x108,%rsp
+	lea	.Linc(%rip),%rax
+	and	\$-16,%rsp		# shouldn't be formally required
+
+	movd	$idx,%xmm5
+	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
+	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
+	lea	128($tbl),%r11		# size optimization
+	lea	128(%rsp),%rax		# size optimization
+
+	pshufd	\$0,%xmm5,%xmm5		# broadcast $idx
+	movdqa	%xmm1,%xmm4
+	movdqa	%xmm1,%xmm2
 ___
+########################################################################
+# calculate mask by comparing 0..31 to $idx and save result to stack
+#
+for($i=0;$i<$STRIDE/16;$i+=4) {
 $code.=<<___;
-	mov	$idx,%r11d
-	shr	\$`log($N/8)/log(2)`,$idx
-	and	\$`$N/8-1`,%r11
-	not	$idx
-	lea	.Lmagic_masks(%rip),%rax
-	and	\$`2**5/($N/8)-1`,$idx	# 5 is "window size"
-	lea	128($tbl,%r11,8),$tbl	# pointer within 1st cache line
-	movq	0(%rax,$idx,8),%xmm4	# set of masks denoting which
-	movq	8(%rax,$idx,8),%xmm5	# cache line contains element
-	movq	16(%rax,$idx,8),%xmm6	# denoted by 7th argument
-	movq	24(%rax,$idx,8),%xmm7
+	paddd	%xmm0,%xmm1
+	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
+___
+$code.=<<___	if ($i);
+	movdqa	%xmm3,`16*($i-1)-128`(%rax)
+___
+$code.=<<___;
+	movdqa	%xmm4,%xmm3
+
+	paddd	%xmm1,%xmm2
+	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
+	movdqa	%xmm0,`16*($i+0)-128`(%rax)
+	movdqa	%xmm4,%xmm0
+
+	paddd	%xmm2,%xmm3
+	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
+	movdqa	%xmm1,`16*($i+1)-128`(%rax)
+	movdqa	%xmm4,%xmm1
+
+	paddd	%xmm3,%xmm0
+	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
+	movdqa	%xmm2,`16*($i+2)-128`(%rax)
+	movdqa	%xmm4,%xmm2
+___
+}
+$code.=<<___;
+	movdqa	%xmm3,`16*($i-1)-128`(%rax)
 	jmp	.Lgather
-.align	16
-.Lgather:
-	movq	`0*$STRIDE/4-128`($tbl),%xmm0
-	movq	`1*$STRIDE/4-128`($tbl),%xmm1
-	pand	%xmm4,%xmm0
-	movq	`2*$STRIDE/4-128`($tbl),%xmm2
-	pand	%xmm5,%xmm1
-	movq	`3*$STRIDE/4-128`($tbl),%xmm3
-	pand	%xmm6,%xmm2
-	por	%xmm1,%xmm0
-	pand	%xmm7,%xmm3
-	.byte	0x67,0x67
-	por	%xmm2,%xmm0
-	lea	$STRIDE($tbl),$tbl
-	por	%xmm3,%xmm0
 
+.align	32
+.Lgather:
+	pxor	%xmm4,%xmm4
+	pxor	%xmm5,%xmm5
+___
+for($i=0;$i<$STRIDE/16;$i+=4) {
+$code.=<<___;
+	movdqa	`16*($i+0)-128`(%r11),%xmm0
+	movdqa	`16*($i+1)-128`(%r11),%xmm1
+	movdqa	`16*($i+2)-128`(%r11),%xmm2
+	pand	`16*($i+0)-128`(%rax),%xmm0
+	movdqa	`16*($i+3)-128`(%r11),%xmm3
+	pand	`16*($i+1)-128`(%rax),%xmm1
+	por	%xmm0,%xmm4
+	pand	`16*($i+2)-128`(%rax),%xmm2
+	por	%xmm1,%xmm5
+	pand	`16*($i+3)-128`(%rax),%xmm3
+	por	%xmm2,%xmm4
+	por	%xmm3,%xmm5
+___
+}
+$code.=<<___;
+	por	%xmm5,%xmm4
+	lea	$STRIDE(%r11),%r11
+	pshufd	\$0x4e,%xmm4,%xmm0
+	por	%xmm4,%xmm0
 	movq	%xmm0,($out)		# m0=bp[0]
 	lea	8($out),$out
 	sub	\$1,$num
 	jnz	.Lgather
-___
-$code.=<<___ if ($win64);
-	movaps	(%rsp),%xmm6
-	movaps	0x10(%rsp),%xmm7
-	lea	0x28(%rsp),%rsp
-___
-$code.=<<___;
+
+	lea	(%r10),%rsp
 	ret
 .LSEH_end_bn_gather5:
 .size	bn_gather5,.-bn_gather5
 ___
 }
 $code.=<<___;
 .align	64
-.Lmagic_masks:
-	.long	0,0, 0,0, 0,0, -1,-1
-	.long	0,0, 0,0, 0,0,  0,0
+.Linc:
+	.long	0,0, 1,1
+	.long	2,2, 2,2
 .asciz	"Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by "
 ___
 
 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
 #		CONTEXT *context,DISPATCHER_CONTEXT *disp)
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___;
 .extern	__imp_RtlVirtualUnwind
 .type	mul_handler,\@abi-omnipotent
 .align	16
 mul_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# end of prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lcommon_seh_tail
 
 	lea	.Lmul_epilogue(%rip),%r10
 	cmp	%r10,%rbx
-	jb	.Lbody_40
+	ja	.Lbody_40
 
 	mov	192($context),%r10	# pull $num
 	mov	8(%rax,%r10,8),%rax	# pull saved stack pointer
+
 	jmp	.Lbody_proceed
 
 .Lbody_40:
 	mov	40(%rax),%rax		# pull saved stack pointer
 .Lbody_proceed:
-
-	movaps	-88(%rax),%xmm0
-	movaps	-72(%rax),%xmm1
-
 	mov	-8(%rax),%rbx
 	mov	-16(%rax),%rbp
 	mov	-24(%rax),%r12
 	mov	-32(%rax),%r13
 	mov	-40(%rax),%r14
 	mov	-48(%rax),%r15
 	mov	%rbx,144($context)	# restore context->Rbx
 	mov	%rbp,160($context)	# restore context->Rbp
 	mov	%r12,216($context)	# restore context->R12
 	mov	%r13,224($context)	# restore context->R13
 	mov	%r14,232($context)	# restore context->R14
 	mov	%r15,240($context)	# restore context->R15
-	movups	%xmm0,512($context)	# restore context->Xmm6
-	movups	%xmm1,528($context)	# restore context->Xmm7
 
 .Lcommon_seh_tail:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$154,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
 .size	mul_handler,.-mul_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_bn_mul_mont_gather5
 	.rva	.LSEH_end_bn_mul_mont_gather5
 	.rva	.LSEH_info_bn_mul_mont_gather5
 
 	.rva	.LSEH_begin_bn_mul4x_mont_gather5
 	.rva	.LSEH_end_bn_mul4x_mont_gather5
 	.rva	.LSEH_info_bn_mul4x_mont_gather5
 
 	.rva	.LSEH_begin_bn_power5
 	.rva	.LSEH_end_bn_power5
 	.rva	.LSEH_info_bn_power5
 
 	.rva	.LSEH_begin_bn_from_mont8x
 	.rva	.LSEH_end_bn_from_mont8x
 	.rva	.LSEH_info_bn_from_mont8x
 ___
 $code.=<<___ if ($addx);
 	.rva	.LSEH_begin_bn_mulx4x_mont_gather5
 	.rva	.LSEH_end_bn_mulx4x_mont_gather5
 	.rva	.LSEH_info_bn_mulx4x_mont_gather5
 
 	.rva	.LSEH_begin_bn_powerx5
 	.rva	.LSEH_end_bn_powerx5
 	.rva	.LSEH_info_bn_powerx5
 ___
 $code.=<<___;
 	.rva	.LSEH_begin_bn_gather5
 	.rva	.LSEH_end_bn_gather5
 	.rva	.LSEH_info_bn_gather5
 
 .section	.xdata
 .align	8
 .LSEH_info_bn_mul_mont_gather5:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lmul_body,.Lmul_epilogue		# HandlerData[]
 .align	8
 .LSEH_info_bn_mul4x_mont_gather5:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lmul4x_body,.Lmul4x_epilogue		# HandlerData[]
 .align	8
 .LSEH_info_bn_power5:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lpower5_body,.Lpower5_epilogue		# HandlerData[]
 .align	8
 .LSEH_info_bn_from_mont8x:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lfrom_body,.Lfrom_epilogue		# HandlerData[]
 ___
 $code.=<<___ if ($addx);
 .align	8
 .LSEH_info_bn_mulx4x_mont_gather5:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lmulx4x_body,.Lmulx4x_epilogue		# HandlerData[]
 .align	8
 .LSEH_info_bn_powerx5:
 	.byte	9,0,0,0
 	.rva	mul_handler
 	.rva	.Lpowerx5_body,.Lpowerx5_epilogue	# HandlerData[]
 ___
 $code.=<<___;
 .align	8
 .LSEH_info_bn_gather5:
-        .byte   0x01,0x0d,0x05,0x00
-        .byte   0x0d,0x78,0x01,0x00	#movaps	0x10(rsp),xmm7
-        .byte   0x08,0x68,0x00,0x00	#movaps	(rsp),xmm6
-        .byte   0x04,0x42,0x00,0x00	#sub	rsp,0x28
+	.byte	0x01,0x0b,0x03,0x0a
+	.byte	0x0b,0x01,0x21,0x00	# sub	rsp,0x108
+	.byte	0x04,0xa3,0x00,0x00	# lea	r10,(rsp)
 .align	8
 ___
 }
 
 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
 
 print $code;
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/bn/bn.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/bn.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/bn.h	(revision 296273)
@@ -1,939 +1,949 @@
 /* crypto/bn/bn.h */
 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 /* ====================================================================
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  *
  * Portions of the attached software ("Contribution") are developed by
  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  *
  * The Contribution is licensed pursuant to the Eric Young open source
  * license provided above.
  *
  * The binary polynomial arithmetic software is originally written by
  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  *
  */
 
 #ifndef HEADER_BN_H
 # define HEADER_BN_H
 
+# include 
 # include 
 # ifndef OPENSSL_NO_FP_API
 #  include             /* FILE */
 # endif
 # include 
 # include 
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 /*
  * These preprocessor symbols control various aspects of the bignum headers
  * and library code. They're not defined by any "normal" configuration, as
  * they are intended for development and testing purposes. NB: defining all
  * three can be useful for debugging application code as well as openssl
  * itself. BN_DEBUG - turn on various debugging alterations to the bignum
  * code BN_DEBUG_RAND - uses random poisoning of unused words to trip up
  * mismanagement of bignum internals. You must also define BN_DEBUG.
  */
 /* #define BN_DEBUG */
 /* #define BN_DEBUG_RAND */
 
 # ifndef OPENSSL_SMALL_FOOTPRINT
 #  define BN_MUL_COMBA
 #  define BN_SQR_COMBA
 #  define BN_RECURSION
 # endif
 
 /*
  * This next option uses the C libraries (2 word)/(1 word) function. If it is
  * not defined, I use my C version (which is slower). The reason for this
  * flag is that when the particular C compiler library routine is used, and
  * the library is linked with a different compiler, the library is missing.
  * This mostly happens when the library is built with gcc and then linked
  * using normal cc.  This would be a common occurrence because gcc normally
  * produces code that is 2 times faster than system compilers for the big
  * number stuff. For machines with only one compiler (or shared libraries),
  * this should be on.  Again this in only really a problem on machines using
  * "long long's", are 32bit, and are not using my assembler code.
  */
 # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || \
     defined(OPENSSL_SYS_WIN32) || defined(linux)
 #  ifndef BN_DIV2W
 #   define BN_DIV2W
 #  endif
 # endif
 
 /*
  * assuming long is 64bit - this is the DEC Alpha unsigned long long is only
  * 64 bits :-(, don't define BN_LLONG for the DEC Alpha
  */
 # ifdef SIXTY_FOUR_BIT_LONG
 #  define BN_ULLONG       unsigned long long
 #  define BN_ULONG        unsigned long
 #  define BN_LONG         long
 #  define BN_BITS         128
 #  define BN_BYTES        8
 #  define BN_BITS2        64
 #  define BN_BITS4        32
 #  define BN_MASK         (0xffffffffffffffffffffffffffffffffLL)
 #  define BN_MASK2        (0xffffffffffffffffL)
 #  define BN_MASK2l       (0xffffffffL)
 #  define BN_MASK2h       (0xffffffff00000000L)
 #  define BN_MASK2h1      (0xffffffff80000000L)
 #  define BN_TBIT         (0x8000000000000000L)
 #  define BN_DEC_CONV     (10000000000000000000UL)
 #  define BN_DEC_FMT1     "%lu"
 #  define BN_DEC_FMT2     "%019lu"
 #  define BN_DEC_NUM      19
 #  define BN_HEX_FMT1     "%lX"
 #  define BN_HEX_FMT2     "%016lX"
 # endif
 
 /*
  * This is where the long long data type is 64 bits, but long is 32. For
  * machines where there are 64bit registers, this is the mode to use. IRIX,
  * on R4000 and above should use this mode, along with the relevant assembler
  * code :-).  Do NOT define BN_LLONG.
  */
 # ifdef SIXTY_FOUR_BIT
 #  undef BN_LLONG
 #  undef BN_ULLONG
 #  define BN_ULONG        unsigned long long
 #  define BN_LONG         long long
 #  define BN_BITS         128
 #  define BN_BYTES        8
 #  define BN_BITS2        64
 #  define BN_BITS4        32
 #  define BN_MASK2        (0xffffffffffffffffLL)
 #  define BN_MASK2l       (0xffffffffL)
 #  define BN_MASK2h       (0xffffffff00000000LL)
 #  define BN_MASK2h1      (0xffffffff80000000LL)
 #  define BN_TBIT         (0x8000000000000000LL)
 #  define BN_DEC_CONV     (10000000000000000000ULL)
 #  define BN_DEC_FMT1     "%llu"
 #  define BN_DEC_FMT2     "%019llu"
 #  define BN_DEC_NUM      19
 #  define BN_HEX_FMT1     "%llX"
 #  define BN_HEX_FMT2     "%016llX"
 # endif
 
 # ifdef THIRTY_TWO_BIT
 #  ifdef BN_LLONG
 #   if defined(_WIN32) && !defined(__GNUC__)
 #    define BN_ULLONG     unsigned __int64
 #    define BN_MASK       (0xffffffffffffffffI64)
 #   else
 #    define BN_ULLONG     unsigned long long
 #    define BN_MASK       (0xffffffffffffffffLL)
 #   endif
 #  endif
 #  define BN_ULONG        unsigned int
 #  define BN_LONG         int
 #  define BN_BITS         64
 #  define BN_BYTES        4
 #  define BN_BITS2        32
 #  define BN_BITS4        16
 #  define BN_MASK2        (0xffffffffL)
 #  define BN_MASK2l       (0xffff)
 #  define BN_MASK2h1      (0xffff8000L)
 #  define BN_MASK2h       (0xffff0000L)
 #  define BN_TBIT         (0x80000000L)
 #  define BN_DEC_CONV     (1000000000L)
 #  define BN_DEC_FMT1     "%u"
 #  define BN_DEC_FMT2     "%09u"
 #  define BN_DEC_NUM      9
 #  define BN_HEX_FMT1     "%X"
 #  define BN_HEX_FMT2     "%08X"
 # endif
 
 # define BN_DEFAULT_BITS 1280
 
 # define BN_FLG_MALLOCED         0x01
 # define BN_FLG_STATIC_DATA      0x02
 
 /*
  * avoid leaking exponent information through timing,
  * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime,
  * BN_div() will call BN_div_no_branch,
  * BN_mod_inverse() will call BN_mod_inverse_no_branch.
  */
 # define BN_FLG_CONSTTIME        0x04
 
 # ifdef OPENSSL_NO_DEPRECATED
 /* deprecated name for the flag */
 #  define BN_FLG_EXP_CONSTTIME BN_FLG_CONSTTIME
 /*
  * avoid leaking exponent information through timings
  * (BN_mod_exp_mont() will call BN_mod_exp_mont_consttime)
  */
 # endif
 
 # ifndef OPENSSL_NO_DEPRECATED
 #  define BN_FLG_FREE             0x8000
                                        /* used for debuging */
 # endif
 # define BN_set_flags(b,n)       ((b)->flags|=(n))
 # define BN_get_flags(b,n)       ((b)->flags&(n))
 
 /*
  * get a clone of a BIGNUM with changed flags, for *temporary* use only (the
  * two BIGNUMs cannot not be used in parallel!)
  */
 # define BN_with_flags(dest,b,n)  ((dest)->d=(b)->d, \
                                   (dest)->top=(b)->top, \
                                   (dest)->dmax=(b)->dmax, \
                                   (dest)->neg=(b)->neg, \
                                   (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
                                                  |  ((b)->flags & ~BN_FLG_MALLOCED) \
                                                  |  BN_FLG_STATIC_DATA \
                                                  |  (n)))
 
 /* Already declared in ossl_typ.h */
 # if 0
 typedef struct bignum_st BIGNUM;
 /* Used for temp variables (declaration hidden in bn_lcl.h) */
 typedef struct bignum_ctx BN_CTX;
 typedef struct bn_blinding_st BN_BLINDING;
 typedef struct bn_mont_ctx_st BN_MONT_CTX;
 typedef struct bn_recp_ctx_st BN_RECP_CTX;
 typedef struct bn_gencb_st BN_GENCB;
 # endif
 
 struct bignum_st {
     BN_ULONG *d;                /* Pointer to an array of 'BN_BITS2' bit
                                  * chunks. */
     int top;                    /* Index of last used d +1. */
     /* The next are internal book keeping for bn_expand. */
     int dmax;                   /* Size of the d array. */
     int neg;                    /* one if the number is negative */
     int flags;
 };
 
 /* Used for montgomery multiplication */
 struct bn_mont_ctx_st {
     int ri;                     /* number of bits in R */
     BIGNUM RR;                  /* used to convert to montgomery form */
     BIGNUM N;                   /* The modulus */
     BIGNUM Ni;                  /* R*(1/R mod N) - N*Ni = 1 (Ni is only
                                  * stored for bignum algorithm) */
     BN_ULONG n0[2];             /* least significant word(s) of Ni; (type
                                  * changed with 0.9.9, was "BN_ULONG n0;"
                                  * before) */
     int flags;
 };
 
 /*
  * Used for reciprocal division/mod functions It cannot be shared between
  * threads
  */
 struct bn_recp_ctx_st {
     BIGNUM N;                   /* the divisor */
     BIGNUM Nr;                  /* the reciprocal */
     int num_bits;
     int shift;
     int flags;
 };
 
 /* Used for slow "generation" functions. */
 struct bn_gencb_st {
     unsigned int ver;           /* To handle binary (in)compatibility */
     void *arg;                  /* callback-specific data */
     union {
         /* if(ver==1) - handles old style callbacks */
         void (*cb_1) (int, int, void *);
         /* if(ver==2) - new callback style */
         int (*cb_2) (int, int, BN_GENCB *);
     } cb;
 };
 /* Wrapper function to make using BN_GENCB easier,  */
 int BN_GENCB_call(BN_GENCB *cb, int a, int b);
 /* Macro to populate a BN_GENCB structure with an "old"-style callback */
 # define BN_GENCB_set_old(gencb, callback, cb_arg) { \
                 BN_GENCB *tmp_gencb = (gencb); \
                 tmp_gencb->ver = 1; \
                 tmp_gencb->arg = (cb_arg); \
                 tmp_gencb->cb.cb_1 = (callback); }
 /* Macro to populate a BN_GENCB structure with a "new"-style callback */
 # define BN_GENCB_set(gencb, callback, cb_arg) { \
                 BN_GENCB *tmp_gencb = (gencb); \
                 tmp_gencb->ver = 2; \
                 tmp_gencb->arg = (cb_arg); \
                 tmp_gencb->cb.cb_2 = (callback); }
 
 # define BN_prime_checks 0      /* default: select number of iterations based
                                  * on the size of the number */
 
 /*
  * number of Miller-Rabin iterations for an error rate of less than 2^-80 for
  * random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook of
  * Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996];
  * original paper: Damgaard, Landrock, Pomerance: Average case error
  * estimates for the strong probable prime test. -- Math. Comp. 61 (1993)
  * 177-194)
  */
 # define BN_prime_checks_for_size(b) ((b) >= 1300 ?  2 : \
                                 (b) >=  850 ?  3 : \
                                 (b) >=  650 ?  4 : \
                                 (b) >=  550 ?  5 : \
                                 (b) >=  450 ?  6 : \
                                 (b) >=  400 ?  7 : \
                                 (b) >=  350 ?  8 : \
                                 (b) >=  300 ?  9 : \
                                 (b) >=  250 ? 12 : \
                                 (b) >=  200 ? 15 : \
                                 (b) >=  150 ? 18 : \
                                 /* b >= 100 */ 27)
 
 # define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
 
 /* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */
 # define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
                                 (((w) == 0) && ((a)->top == 0)))
 # define BN_is_zero(a)       ((a)->top == 0)
 # define BN_is_one(a)        (BN_abs_is_word((a),1) && !(a)->neg)
 # define BN_is_word(a,w)     (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
 # define BN_is_odd(a)        (((a)->top > 0) && ((a)->d[0] & 1))
 
 # define BN_one(a)       (BN_set_word((a),1))
 # define BN_zero_ex(a) \
         do { \
                 BIGNUM *_tmp_bn = (a); \
                 _tmp_bn->top = 0; \
                 _tmp_bn->neg = 0; \
         } while(0)
 # ifdef OPENSSL_NO_DEPRECATED
 #  define BN_zero(a)      BN_zero_ex(a)
 # else
 #  define BN_zero(a)      (BN_set_word((a),0))
 # endif
 
 const BIGNUM *BN_value_one(void);
 char *BN_options(void);
 BN_CTX *BN_CTX_new(void);
 # ifndef OPENSSL_NO_DEPRECATED
 void BN_CTX_init(BN_CTX *c);
 # endif
 void BN_CTX_free(BN_CTX *c);
 void BN_CTX_start(BN_CTX *ctx);
 BIGNUM *BN_CTX_get(BN_CTX *ctx);
 void BN_CTX_end(BN_CTX *ctx);
 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
 int BN_rand_range(BIGNUM *rnd, const BIGNUM *range);
 int BN_pseudo_rand_range(BIGNUM *rnd, const BIGNUM *range);
 int BN_num_bits(const BIGNUM *a);
 int BN_num_bits_word(BN_ULONG);
 BIGNUM *BN_new(void);
 void BN_init(BIGNUM *);
 void BN_clear_free(BIGNUM *a);
 BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
 void BN_swap(BIGNUM *a, BIGNUM *b);
 BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
 int BN_bn2bin(const BIGNUM *a, unsigned char *to);
 BIGNUM *BN_mpi2bn(const unsigned char *s, int len, BIGNUM *ret);
 int BN_bn2mpi(const BIGNUM *a, unsigned char *to);
 int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
 int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
 int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
 int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
 int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
 /** BN_set_negative sets sign of a BIGNUM
  * \param  b  pointer to the BIGNUM object
  * \param  n  0 if the BIGNUM b should be positive and a value != 0 otherwise
  */
 void BN_set_negative(BIGNUM *b, int n);
 /** BN_is_negative returns 1 if the BIGNUM is negative
  * \param  a  pointer to the BIGNUM object
  * \return 1 if a < 0 and 0 otherwise
  */
 # define BN_is_negative(a) ((a)->neg != 0)
 
 int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
            BN_CTX *ctx);
 # define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
 int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
                BN_CTX *ctx);
 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                      const BIGNUM *m);
 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
                BN_CTX *ctx);
 int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                      const BIGNUM *m);
 int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
                BN_CTX *ctx);
 int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
 int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
 int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
 int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
                   BN_CTX *ctx);
 int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
 
 BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
 BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
 int BN_mul_word(BIGNUM *a, BN_ULONG w);
 int BN_add_word(BIGNUM *a, BN_ULONG w);
 int BN_sub_word(BIGNUM *a, BN_ULONG w);
 int BN_set_word(BIGNUM *a, BN_ULONG w);
 BN_ULONG BN_get_word(const BIGNUM *a);
 
 int BN_cmp(const BIGNUM *a, const BIGNUM *b);
 void BN_free(BIGNUM *a);
 int BN_is_bit_set(const BIGNUM *a, int n);
 int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
 int BN_lshift1(BIGNUM *r, const BIGNUM *a);
 int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 
 int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                const BIGNUM *m, BN_CTX *ctx);
 int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
                               const BIGNUM *m, BN_CTX *ctx,
                               BN_MONT_CTX *in_mont);
 int BN_mod_exp_mont_word(BIGNUM *r, BN_ULONG a, const BIGNUM *p,
                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 int BN_mod_exp2_mont(BIGNUM *r, const BIGNUM *a1, const BIGNUM *p1,
                      const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
                      BN_CTX *ctx, BN_MONT_CTX *m_ctx);
 int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                       const BIGNUM *m, BN_CTX *ctx);
 
 int BN_mask_bits(BIGNUM *a, int n);
 # ifndef OPENSSL_NO_FP_API
 int BN_print_fp(FILE *fp, const BIGNUM *a);
 # endif
 # ifdef HEADER_BIO_H
 int BN_print(BIO *fp, const BIGNUM *a);
 # else
 int BN_print(void *fp, const BIGNUM *a);
 # endif
 int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
 int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
 int BN_rshift1(BIGNUM *r, const BIGNUM *a);
 void BN_clear(BIGNUM *a);
 BIGNUM *BN_dup(const BIGNUM *a);
 int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
 int BN_set_bit(BIGNUM *a, int n);
 int BN_clear_bit(BIGNUM *a, int n);
 char *BN_bn2hex(const BIGNUM *a);
 char *BN_bn2dec(const BIGNUM *a);
 int BN_hex2bn(BIGNUM **a, const char *str);
 int BN_dec2bn(BIGNUM **a, const char *str);
 int BN_asc2bn(BIGNUM **a, const char *str);
 int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
 int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx); /* returns
                                                                   * -2 for
                                                                   * error */
 BIGNUM *BN_mod_inverse(BIGNUM *ret,
                        const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
 BIGNUM *BN_mod_sqrt(BIGNUM *ret,
                     const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
 
 void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
 
 /* Deprecated versions */
 # ifndef OPENSSL_NO_DEPRECATED
 BIGNUM *BN_generate_prime(BIGNUM *ret, int bits, int safe,
                           const BIGNUM *add, const BIGNUM *rem,
                           void (*callback) (int, int, void *), void *cb_arg);
 int BN_is_prime(const BIGNUM *p, int nchecks,
                 void (*callback) (int, int, void *),
                 BN_CTX *ctx, void *cb_arg);
 int BN_is_prime_fasttest(const BIGNUM *p, int nchecks,
                          void (*callback) (int, int, void *), BN_CTX *ctx,
                          void *cb_arg, int do_trial_division);
 # endif                         /* !defined(OPENSSL_NO_DEPRECATED) */
 
 /* Newer versions */
 int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
                          const BIGNUM *rem, BN_GENCB *cb);
 int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
 int BN_is_prime_fasttest_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx,
                             int do_trial_division, BN_GENCB *cb);
 
 int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
 
 int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
                             const BIGNUM *Xp, const BIGNUM *Xp1,
                             const BIGNUM *Xp2, const BIGNUM *e, BN_CTX *ctx,
                             BN_GENCB *cb);
 int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1,
                               BIGNUM *Xp2, const BIGNUM *Xp, const BIGNUM *e,
                               BN_CTX *ctx, BN_GENCB *cb);
 
 BN_MONT_CTX *BN_MONT_CTX_new(void);
 void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
 int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                           BN_MONT_CTX *mont, BN_CTX *ctx);
 # define BN_to_montgomery(r,a,mont,ctx)  BN_mod_mul_montgomery(\
         (r),(a),&((mont)->RR),(mont),(ctx))
 int BN_from_montgomery(BIGNUM *r, const BIGNUM *a,
                        BN_MONT_CTX *mont, BN_CTX *ctx);
 void BN_MONT_CTX_free(BN_MONT_CTX *mont);
 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx);
 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
 BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, int lock,
                                     const BIGNUM *mod, BN_CTX *ctx);
 
 /* BN_BLINDING flags */
 # define BN_BLINDING_NO_UPDATE   0x00000001
 # define BN_BLINDING_NO_RECREATE 0x00000002
 
 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
 void BN_BLINDING_free(BN_BLINDING *b);
 int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
 int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
 int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
 int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
 int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b,
                           BN_CTX *);
 # ifndef OPENSSL_NO_DEPRECATED
 unsigned long BN_BLINDING_get_thread_id(const BN_BLINDING *);
 void BN_BLINDING_set_thread_id(BN_BLINDING *, unsigned long);
 # endif
 CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
 unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
 void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
 BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
                                       const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
                                       int (*bn_mod_exp) (BIGNUM *r,
                                                          const BIGNUM *a,
                                                          const BIGNUM *p,
                                                          const BIGNUM *m,
                                                          BN_CTX *ctx,
                                                          BN_MONT_CTX *m_ctx),
                                       BN_MONT_CTX *m_ctx);
 
 # ifndef OPENSSL_NO_DEPRECATED
 void BN_set_params(int mul, int high, int low, int mont);
 int BN_get_params(int which);   /* 0, mul, 1 high, 2 low, 3 mont */
 # endif
 
 void BN_RECP_CTX_init(BN_RECP_CTX *recp);
 BN_RECP_CTX *BN_RECP_CTX_new(void);
 void BN_RECP_CTX_free(BN_RECP_CTX *recp);
 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *rdiv, BN_CTX *ctx);
 int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
                           BN_RECP_CTX *recp, BN_CTX *ctx);
 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                     const BIGNUM *m, BN_CTX *ctx);
 int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
                 BN_RECP_CTX *recp, BN_CTX *ctx);
 
 # ifndef OPENSSL_NO_EC2M
 
 /*
  * Functions for arithmetic over binary polynomials represented by BIGNUMs.
  * The BIGNUM::neg property of BIGNUMs representing binary polynomials is
  * ignored. Note that input arguments are not const so that their bit arrays
  * can be expanded to the appropriate size if needed.
  */
 
 /*
  * r = a + b
  */
 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
 #  define BN_GF2m_sub(r, a, b) BN_GF2m_add(r, a, b)
 /*
  * r=a mod p
  */
 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p);
 /* r = (a * b) mod p */
 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                     const BIGNUM *p, BN_CTX *ctx);
 /* r = (a * a) mod p */
 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 /* r = (1 / b) mod p */
 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx);
 /* r = (a / b) mod p */
 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                     const BIGNUM *p, BN_CTX *ctx);
 /* r = (a ^ b) mod p */
 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                     const BIGNUM *p, BN_CTX *ctx);
 /* r = sqrt(a) mod p */
 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                      BN_CTX *ctx);
 /* r^2 + r = a mod p */
 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                            BN_CTX *ctx);
 #  define BN_GF2m_cmp(a, b) BN_ucmp((a), (b))
 /*-
  * Some functions allow for representation of the irreducible polynomials
  * as an unsigned int[], say p.  The irreducible f(t) is then of the form:
  *     t^p[0] + t^p[1] + ... + t^p[k]
  * where m = p[0] > p[1] > ... > p[k] = 0.
  */
 /* r = a mod p */
 int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]);
 /* r = (a * b) mod p */
 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                         const int p[], BN_CTX *ctx);
 /* r = (a * a) mod p */
 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
                         BN_CTX *ctx);
 /* r = (1 / b) mod p */
 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *b, const int p[],
                         BN_CTX *ctx);
 /* r = (a / b) mod p */
 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                         const int p[], BN_CTX *ctx);
 /* r = (a ^ b) mod p */
 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                         const int p[], BN_CTX *ctx);
 /* r = sqrt(a) mod p */
 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a,
                          const int p[], BN_CTX *ctx);
 /* r^2 + r = a mod p */
 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a,
                                const int p[], BN_CTX *ctx);
 int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max);
 int BN_GF2m_arr2poly(const int p[], BIGNUM *a);
 
 # endif
 
 /*
  * faster mod functions for the 'NIST primes' 0 <= a < p^2
  */
 int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 int BN_nist_mod_521(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
 
 const BIGNUM *BN_get0_nist_prime_192(void);
 const BIGNUM *BN_get0_nist_prime_224(void);
 const BIGNUM *BN_get0_nist_prime_256(void);
 const BIGNUM *BN_get0_nist_prime_384(void);
 const BIGNUM *BN_get0_nist_prime_521(void);
 
 /* library internal functions */
 
-# define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
-        (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
+# define bn_expand(a,bits) \
+    ( \
+        bits > (INT_MAX - BN_BITS2 + 1) ? \
+            NULL \
+        : \
+            (((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax) ? \
+                (a) \
+            : \
+                bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2) \
+    )
+
 # define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
 BIGNUM *bn_expand2(BIGNUM *a, int words);
 # ifndef OPENSSL_NO_DEPRECATED
 BIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */
 # endif
 
 /*-
  * Bignum consistency macros
  * There is one "API" macro, bn_fix_top(), for stripping leading zeroes from
  * bignum data after direct manipulations on the data. There is also an
  * "internal" macro, bn_check_top(), for verifying that there are no leading
  * zeroes. Unfortunately, some auditing is required due to the fact that
  * bn_fix_top() has become an overabused duct-tape because bignum data is
  * occasionally passed around in an inconsistent state. So the following
  * changes have been made to sort this out;
  * - bn_fix_top()s implementation has been moved to bn_correct_top()
  * - if BN_DEBUG isn't defined, bn_fix_top() maps to bn_correct_top(), and
  *   bn_check_top() is as before.
  * - if BN_DEBUG *is* defined;
  *   - bn_check_top() tries to pollute unused words even if the bignum 'top' is
  *     consistent. (ed: only if BN_DEBUG_RAND is defined)
  *   - bn_fix_top() maps to bn_check_top() rather than "fixing" anything.
  * The idea is to have debug builds flag up inconsistent bignums when they
  * occur. If that occurs in a bn_fix_top(), we examine the code in question; if
  * the use of bn_fix_top() was appropriate (ie. it follows directly after code
  * that manipulates the bignum) it is converted to bn_correct_top(), and if it
  * was not appropriate, we convert it permanently to bn_check_top() and track
  * down the cause of the bug. Eventually, no internal code should be using the
  * bn_fix_top() macro. External applications and libraries should try this with
  * their own code too, both in terms of building against the openssl headers
  * with BN_DEBUG defined *and* linking with a version of OpenSSL built with it
  * defined. This not only improves external code, it provides more test
  * coverage for openssl's own code.
  */
 
 # ifdef BN_DEBUG
 
 /* We only need assert() when debugging */
 #  include 
 
 #  ifdef BN_DEBUG_RAND
 /* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */
 #   ifndef RAND_pseudo_bytes
 int RAND_pseudo_bytes(unsigned char *buf, int num);
 #    define BN_DEBUG_TRIX
 #   endif
 #   define bn_pollute(a) \
         do { \
                 const BIGNUM *_bnum1 = (a); \
                 if(_bnum1->top < _bnum1->dmax) { \
                         unsigned char _tmp_char; \
                         /* We cast away const without the compiler knowing, any \
                          * *genuinely* constant variables that aren't mutable \
                          * wouldn't be constructed with top!=dmax. */ \
                         BN_ULONG *_not_const; \
                         memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
                         /* Debug only - safe to ignore error return */ \
                         RAND_pseudo_bytes(&_tmp_char, 1); \
                         memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
                                 (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
                 } \
         } while(0)
 #   ifdef BN_DEBUG_TRIX
 #    undef RAND_pseudo_bytes
 #   endif
 #  else
 #   define bn_pollute(a)
 #  endif
 #  define bn_check_top(a) \
         do { \
                 const BIGNUM *_bnum2 = (a); \
                 if (_bnum2 != NULL) { \
                         assert((_bnum2->top == 0) || \
                                 (_bnum2->d[_bnum2->top - 1] != 0)); \
                         bn_pollute(_bnum2); \
                 } \
         } while(0)
 
 #  define bn_fix_top(a)           bn_check_top(a)
 
 #  define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
 #  define bn_wcheck_size(bn, words) \
         do { \
                 const BIGNUM *_bnum2 = (bn); \
                 assert((words) <= (_bnum2)->dmax && (words) >= (_bnum2)->top); \
                 /* avoid unused variable warning with NDEBUG */ \
                 (void)(_bnum2); \
         } while(0)
 
 # else                          /* !BN_DEBUG */
 
 #  define bn_pollute(a)
 #  define bn_check_top(a)
 #  define bn_fix_top(a)           bn_correct_top(a)
 #  define bn_check_size(bn, bits)
 #  define bn_wcheck_size(bn, words)
 
 # endif
 
 # define bn_correct_top(a) \
         { \
         BN_ULONG *ftl; \
         int tmp_top = (a)->top; \
         if (tmp_top > 0) \
                 { \
                 for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
                         if (*(ftl--)) break; \
                 (a)->top = tmp_top; \
                 } \
         bn_pollute(a); \
         }
 
 BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num,
                           BN_ULONG w);
 BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
 void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
 BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
 BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
                       int num);
 BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
                       int num);
 
 /* Primes from RFC 2409 */
 BIGNUM *get_rfc2409_prime_768(BIGNUM *bn);
 BIGNUM *get_rfc2409_prime_1024(BIGNUM *bn);
 
 /* Primes from RFC 3526 */
 BIGNUM *get_rfc3526_prime_1536(BIGNUM *bn);
 BIGNUM *get_rfc3526_prime_2048(BIGNUM *bn);
 BIGNUM *get_rfc3526_prime_3072(BIGNUM *bn);
 BIGNUM *get_rfc3526_prime_4096(BIGNUM *bn);
 BIGNUM *get_rfc3526_prime_6144(BIGNUM *bn);
 BIGNUM *get_rfc3526_prime_8192(BIGNUM *bn);
 
 int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom);
 
 /* BEGIN ERROR CODES */
 /*
  * The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
  */
 void ERR_load_BN_strings(void);
 
 /* Error codes for the BN functions. */
 
 /* Function codes. */
 # define BN_F_BNRAND                                      127
 # define BN_F_BN_BLINDING_CONVERT_EX                      100
 # define BN_F_BN_BLINDING_CREATE_PARAM                    128
 # define BN_F_BN_BLINDING_INVERT_EX                       101
 # define BN_F_BN_BLINDING_NEW                             102
 # define BN_F_BN_BLINDING_UPDATE                          103
 # define BN_F_BN_BN2DEC                                   104
 # define BN_F_BN_BN2HEX                                   105
 # define BN_F_BN_CTX_GET                                  116
 # define BN_F_BN_CTX_NEW                                  106
 # define BN_F_BN_CTX_START                                129
 # define BN_F_BN_DIV                                      107
 # define BN_F_BN_DIV_NO_BRANCH                            138
 # define BN_F_BN_DIV_RECP                                 130
 # define BN_F_BN_EXP                                      123
 # define BN_F_BN_EXPAND2                                  108
 # define BN_F_BN_EXPAND_INTERNAL                          120
 # define BN_F_BN_GF2M_MOD                                 131
 # define BN_F_BN_GF2M_MOD_EXP                             132
 # define BN_F_BN_GF2M_MOD_MUL                             133
 # define BN_F_BN_GF2M_MOD_SOLVE_QUAD                      134
 # define BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR                  135
 # define BN_F_BN_GF2M_MOD_SQR                             136
 # define BN_F_BN_GF2M_MOD_SQRT                            137
 # define BN_F_BN_LSHIFT                                   145
 # define BN_F_BN_MOD_EXP2_MONT                            118
 # define BN_F_BN_MOD_EXP_MONT                             109
 # define BN_F_BN_MOD_EXP_MONT_CONSTTIME                   124
 # define BN_F_BN_MOD_EXP_MONT_WORD                        117
 # define BN_F_BN_MOD_EXP_RECP                             125
 # define BN_F_BN_MOD_EXP_SIMPLE                           126
 # define BN_F_BN_MOD_INVERSE                              110
 # define BN_F_BN_MOD_INVERSE_NO_BRANCH                    139
 # define BN_F_BN_MOD_LSHIFT_QUICK                         119
 # define BN_F_BN_MOD_MUL_RECIPROCAL                       111
 # define BN_F_BN_MOD_SQRT                                 121
 # define BN_F_BN_MPI2BN                                   112
 # define BN_F_BN_NEW                                      113
 # define BN_F_BN_RAND                                     114
 # define BN_F_BN_RAND_RANGE                               122
 # define BN_F_BN_RSHIFT                                   146
 # define BN_F_BN_USUB                                     115
 
 /* Reason codes. */
 # define BN_R_ARG2_LT_ARG3                                100
 # define BN_R_BAD_RECIPROCAL                              101
 # define BN_R_BIGNUM_TOO_LONG                             114
 # define BN_R_BITS_TOO_SMALL                              118
 # define BN_R_CALLED_WITH_EVEN_MODULUS                    102
 # define BN_R_DIV_BY_ZERO                                 103
 # define BN_R_ENCODING_ERROR                              104
 # define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA                105
 # define BN_R_INPUT_NOT_REDUCED                           110
 # define BN_R_INVALID_LENGTH                              106
 # define BN_R_INVALID_RANGE                               115
 # define BN_R_INVALID_SHIFT                               119
 # define BN_R_NOT_A_SQUARE                                111
 # define BN_R_NOT_INITIALIZED                             107
 # define BN_R_NO_INVERSE                                  108
 # define BN_R_NO_SOLUTION                                 116
 # define BN_R_P_IS_NOT_PRIME                              112
 # define BN_R_TOO_MANY_ITERATIONS                         113
 # define BN_R_TOO_MANY_TEMPORARY_VARIABLES                109
 
 #ifdef  __cplusplus
 }
 #endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/bn/bn_exp.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/bn_exp.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/bn_exp.c	(revision 296273)
@@ -1,1418 +1,1457 @@
 /* crypto/bn/bn_exp.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 #include "cryptlib.h"
+#include "constant_time_locl.h"
 #include "bn_lcl.h"
 
 #include 
 #ifdef _WIN32
 # include 
 # ifndef alloca
 #  define alloca _alloca
 # endif
 #elif defined(__GNUC__)
 # ifndef alloca
 #  define alloca(s) __builtin_alloca((s))
 # endif
 #elif defined(__sun)
 # include 
 #endif
 
 #include "rsaz_exp.h"
 
 #undef SPARC_T4_MONT
 #if defined(OPENSSL_BN_ASM_MONT) && (defined(__sparc__) || defined(__sparc))
 # include "sparc_arch.h"
 extern unsigned int OPENSSL_sparcv9cap_P[];
 # define SPARC_T4_MONT
 #endif
 
 /* maximum precomputation table size for *variable* sliding windows */
 #define TABLE_SIZE      32
 
 /* this one works - simple but works */
 int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
 {
     int i, bits, ret = 0;
     BIGNUM *v, *rr;
 
     if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
         /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
         BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
         return -1;
     }
 
     BN_CTX_start(ctx);
     if ((r == a) || (r == p))
         rr = BN_CTX_get(ctx);
     else
         rr = r;
     v = BN_CTX_get(ctx);
     if (rr == NULL || v == NULL)
         goto err;
 
     if (BN_copy(v, a) == NULL)
         goto err;
     bits = BN_num_bits(p);
 
     if (BN_is_odd(p)) {
         if (BN_copy(rr, a) == NULL)
             goto err;
     } else {
         if (!BN_one(rr))
             goto err;
     }
 
     for (i = 1; i < bits; i++) {
         if (!BN_sqr(v, v, ctx))
             goto err;
         if (BN_is_bit_set(p, i)) {
             if (!BN_mul(rr, rr, v, ctx))
                 goto err;
         }
     }
     if (r != rr)
         BN_copy(r, rr);
     ret = 1;
  err:
     BN_CTX_end(ctx);
     bn_check_top(r);
     return (ret);
 }
 
 int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
                BN_CTX *ctx)
 {
     int ret;
 
     bn_check_top(a);
     bn_check_top(p);
     bn_check_top(m);
 
     /*-
      * For even modulus  m = 2^k*m_odd,  it might make sense to compute
      * a^p mod m_odd  and  a^p mod 2^k  separately (with Montgomery
      * exponentiation for the odd part), using appropriate exponent
      * reductions, and combine the results using the CRT.
      *
      * For now, we use Montgomery only if the modulus is odd; otherwise,
      * exponentiation using the reciprocal-based quick remaindering
      * algorithm is used.
      *
      * (Timing obtained with expspeed.c [computations  a^p mod m
      * where  a, p, m  are of the same length: 256, 512, 1024, 2048,
      * 4096, 8192 bits], compared to the running time of the
      * standard algorithm:
      *
      *   BN_mod_exp_mont   33 .. 40 %  [AMD K6-2, Linux, debug configuration]
      *                     55 .. 77 %  [UltraSparc processor, but
      *                                  debug-solaris-sparcv8-gcc conf.]
      *
      *   BN_mod_exp_recp   50 .. 70 %  [AMD K6-2, Linux, debug configuration]
      *                     62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
      *
      * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont
      * at 2048 and more bits, but at 512 and 1024 bits, it was
      * slower even than the standard algorithm!
      *
      * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations]
      * should be obtained when the new Montgomery reduction code
      * has been integrated into OpenSSL.)
      */
 
 #define MONT_MUL_MOD
 #define MONT_EXP_WORD
 #define RECP_MUL_MOD
 
 #ifdef MONT_MUL_MOD
     /*
      * I have finally been able to take out this pre-condition of the top bit
      * being set.  It was caused by an error in BN_div with negatives.  There
      * was also another problem when for a^b%m a >= m.  eay 07-May-97
      */
     /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
 
     if (BN_is_odd(m)) {
 # ifdef MONT_EXP_WORD
         if (a->top == 1 && !a->neg
             && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {
             BN_ULONG A = a->d[0];
             ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);
         } else
 # endif
             ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);
     } else
 #endif
 #ifdef RECP_MUL_MOD
     {
         ret = BN_mod_exp_recp(r, a, p, m, ctx);
     }
 #else
     {
         ret = BN_mod_exp_simple(r, a, p, m, ctx);
     }
 #endif
 
     bn_check_top(r);
     return (ret);
 }
 
 int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                     const BIGNUM *m, BN_CTX *ctx)
 {
     int i, j, bits, ret = 0, wstart, wend, window, wvalue;
     int start = 1;
     BIGNUM *aa;
     /* Table of variables obtained from 'ctx' */
     BIGNUM *val[TABLE_SIZE];
     BN_RECP_CTX recp;
 
     if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
         /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
         BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
         return -1;
     }
 
     bits = BN_num_bits(p);
     if (bits == 0) {
         /* x**0 mod 1 is still zero. */
         if (BN_is_one(m)) {
             ret = 1;
             BN_zero(r);
         } else {
             ret = BN_one(r);
         }
         return ret;
     }
 
     BN_CTX_start(ctx);
     aa = BN_CTX_get(ctx);
     val[0] = BN_CTX_get(ctx);
     if (!aa || !val[0])
         goto err;
 
     BN_RECP_CTX_init(&recp);
     if (m->neg) {
         /* ignore sign of 'm' */
         if (!BN_copy(aa, m))
             goto err;
         aa->neg = 0;
         if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)
             goto err;
     } else {
         if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)
             goto err;
     }
 
     if (!BN_nnmod(val[0], a, m, ctx))
         goto err;               /* 1 */
     if (BN_is_zero(val[0])) {
         BN_zero(r);
         ret = 1;
         goto err;
     }
 
     window = BN_window_bits_for_exponent_size(bits);
     if (window > 1) {
         if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))
             goto err;           /* 2 */
         j = 1 << (window - 1);
         for (i = 1; i < j; i++) {
             if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
                 !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))
                 goto err;
         }
     }
 
     start = 1;                  /* This is used to avoid multiplication etc
                                  * when there is only the value '1' in the
                                  * buffer. */
     wvalue = 0;                 /* The 'value' of the window */
     wstart = bits - 1;          /* The top bit of the window */
     wend = 0;                   /* The bottom bit of the window */
 
     if (!BN_one(r))
         goto err;
 
     for (;;) {
         if (BN_is_bit_set(p, wstart) == 0) {
             if (!start)
                 if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
                     goto err;
             if (wstart == 0)
                 break;
             wstart--;
             continue;
         }
         /*
          * We now have wstart on a 'set' bit, we now need to work out how bit
          * a window to do.  To do this we need to scan forward until the last
          * set bit before the end of the window
          */
         j = wstart;
         wvalue = 1;
         wend = 0;
         for (i = 1; i < window; i++) {
             if (wstart - i < 0)
                 break;
             if (BN_is_bit_set(p, wstart - i)) {
                 wvalue <<= (i - wend);
                 wvalue |= 1;
                 wend = i;
             }
         }
 
         /* wend is the size of the current window */
         j = wend + 1;
         /* add the 'bytes above' */
         if (!start)
             for (i = 0; i < j; i++) {
                 if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
                     goto err;
             }
 
         /* wvalue will be an odd number < 2^window */
         if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))
             goto err;
 
         /* move the 'window' down further */
         wstart -= wend + 1;
         wvalue = 0;
         start = 0;
         if (wstart < 0)
             break;
     }
     ret = 1;
  err:
     BN_CTX_end(ctx);
     BN_RECP_CTX_free(&recp);
     bn_check_top(r);
     return (ret);
 }
 
 int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
                     const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
 {
     int i, j, bits, ret = 0, wstart, wend, window, wvalue;
     int start = 1;
     BIGNUM *d, *r;
     const BIGNUM *aa;
     /* Table of variables obtained from 'ctx' */
     BIGNUM *val[TABLE_SIZE];
     BN_MONT_CTX *mont = NULL;
 
     if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
         return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
     }
 
     bn_check_top(a);
     bn_check_top(p);
     bn_check_top(m);
 
     if (!BN_is_odd(m)) {
         BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);
         return (0);
     }
     bits = BN_num_bits(p);
     if (bits == 0) {
         /* x**0 mod 1 is still zero. */
         if (BN_is_one(m)) {
             ret = 1;
             BN_zero(rr);
         } else {
             ret = BN_one(rr);
         }
         return ret;
     }
 
     BN_CTX_start(ctx);
     d = BN_CTX_get(ctx);
     r = BN_CTX_get(ctx);
     val[0] = BN_CTX_get(ctx);
     if (!d || !r || !val[0])
         goto err;
 
     /*
      * If this is not done, things will break in the montgomery part
      */
 
     if (in_mont != NULL)
         mont = in_mont;
     else {
         if ((mont = BN_MONT_CTX_new()) == NULL)
             goto err;
         if (!BN_MONT_CTX_set(mont, m, ctx))
             goto err;
     }
 
     if (a->neg || BN_ucmp(a, m) >= 0) {
         if (!BN_nnmod(val[0], a, m, ctx))
             goto err;
         aa = val[0];
     } else
         aa = a;
     if (BN_is_zero(aa)) {
         BN_zero(rr);
         ret = 1;
         goto err;
     }
     if (!BN_to_montgomery(val[0], aa, mont, ctx))
         goto err;               /* 1 */
 
     window = BN_window_bits_for_exponent_size(bits);
     if (window > 1) {
         if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))
             goto err;           /* 2 */
         j = 1 << (window - 1);
         for (i = 1; i < j; i++) {
             if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
                 !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))
                 goto err;
         }
     }
 
     start = 1;                  /* This is used to avoid multiplication etc
                                  * when there is only the value '1' in the
                                  * buffer. */
     wvalue = 0;                 /* The 'value' of the window */
     wstart = bits - 1;          /* The top bit of the window */
     wend = 0;                   /* The bottom bit of the window */
 
 #if 1                           /* by Shay Gueron's suggestion */
     j = m->top;                 /* borrow j */
     if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
         if (bn_wexpand(r, j) == NULL)
             goto err;
         /* 2^(top*BN_BITS2) - m */
         r->d[0] = (0 - m->d[0]) & BN_MASK2;
         for (i = 1; i < j; i++)
             r->d[i] = (~m->d[i]) & BN_MASK2;
         r->top = j;
         /*
          * Upper words will be zero if the corresponding words of 'm' were
          * 0xfff[...], so decrement r->top accordingly.
          */
         bn_correct_top(r);
     } else
 #endif
     if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
         goto err;
     for (;;) {
         if (BN_is_bit_set(p, wstart) == 0) {
             if (!start) {
                 if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
                     goto err;
             }
             if (wstart == 0)
                 break;
             wstart--;
             continue;
         }
         /*
          * We now have wstart on a 'set' bit, we now need to work out how bit
          * a window to do.  To do this we need to scan forward until the last
          * set bit before the end of the window
          */
         j = wstart;
         wvalue = 1;
         wend = 0;
         for (i = 1; i < window; i++) {
             if (wstart - i < 0)
                 break;
             if (BN_is_bit_set(p, wstart - i)) {
                 wvalue <<= (i - wend);
                 wvalue |= 1;
                 wend = i;
             }
         }
 
         /* wend is the size of the current window */
         j = wend + 1;
         /* add the 'bytes above' */
         if (!start)
             for (i = 0; i < j; i++) {
                 if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
                     goto err;
             }
 
         /* wvalue will be an odd number < 2^window */
         if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))
             goto err;
 
         /* move the 'window' down further */
         wstart -= wend + 1;
         wvalue = 0;
         start = 0;
         if (wstart < 0)
             break;
     }
 #if defined(SPARC_T4_MONT)
     if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
         j = mont->N.top;        /* borrow j */
         val[0]->d[0] = 1;       /* borrow val[0] */
         for (i = 1; i < j; i++)
             val[0]->d[i] = 0;
         val[0]->top = j;
         if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))
             goto err;
     } else
 #endif
     if (!BN_from_montgomery(rr, r, mont, ctx))
         goto err;
     ret = 1;
  err:
     if ((in_mont == NULL) && (mont != NULL))
         BN_MONT_CTX_free(mont);
     BN_CTX_end(ctx);
     bn_check_top(rr);
     return (ret);
 }
 
 #if defined(SPARC_T4_MONT)
 static BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos)
 {
     BN_ULONG ret = 0;
     int wordpos;
 
     wordpos = bitpos / BN_BITS2;
     bitpos %= BN_BITS2;
     if (wordpos >= 0 && wordpos < a->top) {
         ret = a->d[wordpos] & BN_MASK2;
         if (bitpos) {
             ret >>= bitpos;
             if (++wordpos < a->top)
                 ret |= a->d[wordpos] << (BN_BITS2 - bitpos);
         }
     }
 
     return ret & BN_MASK2;
 }
 #endif
 
 /*
  * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
  * layout so that accessing any of these table values shows the same access
  * pattern as far as cache lines are concerned.  The following functions are
  * used to transfer a BIGNUM from/to that table.
  */
 
 static int MOD_EXP_CTIME_COPY_TO_PREBUF(const BIGNUM *b, int top,
                                         unsigned char *buf, int idx,
-                                        int width)
+                                        int window)
 {
-    size_t i, j;
+    int i, j;
+    int width = 1 << window;
+    BN_ULONG *table = (BN_ULONG *)buf;
 
     if (top > b->top)
         top = b->top;           /* this works because 'buf' is explicitly
                                  * zeroed */
-    for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) {
-        buf[j] = ((unsigned char *)b->d)[i];
+    for (i = 0, j = idx; i < top; i++, j += width) {
+        table[j] = b->d[i];
     }
 
     return 1;
 }
 
 static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,
                                           unsigned char *buf, int idx,
-                                          int width)
+                                          int window)
 {
-    size_t i, j;
+    int i, j;
+    int width = 1 << window;
+    volatile BN_ULONG *table = (volatile BN_ULONG *)buf;
 
     if (bn_wexpand(b, top) == NULL)
         return 0;
 
-    for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) {
-        ((unsigned char *)b->d)[i] = buf[j];
+    if (window <= 3) {
+        for (i = 0; i < top; i++, table += width) {
+            BN_ULONG acc = 0;
+
+            for (j = 0; j < width; j++) {
+                acc |= table[j] &
+                       ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
+            }
+
+            b->d[i] = acc;
+        }
+    } else {
+        int xstride = 1 << (window - 2);
+        BN_ULONG y0, y1, y2, y3;
+
+        i = idx >> (window - 2);        /* equivalent of idx / xstride */
+        idx &= xstride - 1;             /* equivalent of idx % xstride */
+
+        y0 = (BN_ULONG)0 - (constant_time_eq_int(i,0)&1);
+        y1 = (BN_ULONG)0 - (constant_time_eq_int(i,1)&1);
+        y2 = (BN_ULONG)0 - (constant_time_eq_int(i,2)&1);
+        y3 = (BN_ULONG)0 - (constant_time_eq_int(i,3)&1);
+
+        for (i = 0; i < top; i++, table += width) {
+            BN_ULONG acc = 0;
+
+            for (j = 0; j < xstride; j++) {
+                acc |= ( (table[j + 0 * xstride] & y0) |
+                         (table[j + 1 * xstride] & y1) |
+                         (table[j + 2 * xstride] & y2) |
+                         (table[j + 3 * xstride] & y3) )
+                       & ((BN_ULONG)0 - (constant_time_eq_int(j,idx)&1));
+            }
+
+            b->d[i] = acc;
+        }
     }
 
     b->top = top;
     bn_correct_top(b);
     return 1;
 }
 
 /*
  * Given a pointer value, compute the next address that is a cache line
  * multiple.
  */
 #define MOD_EXP_CTIME_ALIGN(x_) \
         ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((size_t)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
 
 /*
  * This variant of BN_mod_exp_mont() uses fixed windows and the special
  * precomputation memory layout to limit data-dependency to a minimum to
  * protect secret exponents (cf. the hyper-threading timing attacks pointed
  * out by Colin Percival,
  * http://www.daemonology.net/hyperthreading-considered-harmful/)
  */
 int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
                               const BIGNUM *m, BN_CTX *ctx,
                               BN_MONT_CTX *in_mont)
 {
     int i, bits, ret = 0, window, wvalue;
     int top;
     BN_MONT_CTX *mont = NULL;
 
     int numPowers;
     unsigned char *powerbufFree = NULL;
     int powerbufLen = 0;
     unsigned char *powerbuf = NULL;
     BIGNUM tmp, am;
 #if defined(SPARC_T4_MONT)
     unsigned int t4 = 0;
 #endif
 
     bn_check_top(a);
     bn_check_top(p);
     bn_check_top(m);
 
     if (!BN_is_odd(m)) {
         BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);
         return (0);
     }
 
     top = m->top;
 
     bits = BN_num_bits(p);
     if (bits == 0) {
         /* x**0 mod 1 is still zero. */
         if (BN_is_one(m)) {
             ret = 1;
             BN_zero(rr);
         } else {
             ret = BN_one(rr);
         }
         return ret;
     }
 
     BN_CTX_start(ctx);
 
     /*
      * Allocate a montgomery context if it was not supplied by the caller. If
      * this is not done, things will break in the montgomery part.
      */
     if (in_mont != NULL)
         mont = in_mont;
     else {
         if ((mont = BN_MONT_CTX_new()) == NULL)
             goto err;
         if (!BN_MONT_CTX_set(mont, m, ctx))
             goto err;
     }
 
 #ifdef RSAZ_ENABLED
     /*
      * If the size of the operands allow it, perform the optimized
      * RSAZ exponentiation. For further information see
      * crypto/bn/rsaz_exp.c and accompanying assembly modules.
      */
     if ((16 == a->top) && (16 == p->top) && (BN_num_bits(m) == 1024)
         && rsaz_avx2_eligible()) {
         if (NULL == bn_wexpand(rr, 16))
             goto err;
         RSAZ_1024_mod_exp_avx2(rr->d, a->d, p->d, m->d, mont->RR.d,
                                mont->n0[0]);
         rr->top = 16;
         rr->neg = 0;
         bn_correct_top(rr);
         ret = 1;
         goto err;
     } else if ((8 == a->top) && (8 == p->top) && (BN_num_bits(m) == 512)) {
         if (NULL == bn_wexpand(rr, 8))
             goto err;
         RSAZ_512_mod_exp(rr->d, a->d, p->d, m->d, mont->n0[0], mont->RR.d);
         rr->top = 8;
         rr->neg = 0;
         bn_correct_top(rr);
         ret = 1;
         goto err;
     }
 #endif
 
     /* Get the window size to use with size of p. */
     window = BN_window_bits_for_ctime_exponent_size(bits);
 #if defined(SPARC_T4_MONT)
     if (window >= 5 && (top & 15) == 0 && top <= 64 &&
         (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) ==
         (CFR_MONTMUL | CFR_MONTSQR) && (t4 = OPENSSL_sparcv9cap_P[0]))
         window = 5;
     else
 #endif
 #if defined(OPENSSL_BN_ASM_MONT5)
     if (window >= 5) {
         window = 5;             /* ~5% improvement for RSA2048 sign, and even
                                  * for RSA4096 */
-        if ((top & 7) == 0)
-            powerbufLen += 2 * top * sizeof(m->d[0]);
+        /* reserve space for mont->N.d[] copy */
+        powerbufLen += top * sizeof(mont->N.d[0]);
     }
 #endif
     (void)0;
 
     /*
      * Allocate a buffer large enough to hold all of the pre-computed powers
      * of am, am itself and tmp.
      */
     numPowers = 1 << window;
     powerbufLen += sizeof(m->d[0]) * (top * numPowers +
                                       ((2 * top) >
                                        numPowers ? (2 * top) : numPowers));
 #ifdef alloca
     if (powerbufLen < 3072)
         powerbufFree =
             alloca(powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
     else
 #endif
         if ((powerbufFree =
              (unsigned char *)OPENSSL_malloc(powerbufLen +
                                              MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))
             == NULL)
         goto err;
 
     powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
     memset(powerbuf, 0, powerbufLen);
 
 #ifdef alloca
     if (powerbufLen < 3072)
         powerbufFree = NULL;
 #endif
 
     /* lay down tmp and am right after powers table */
     tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
     am.d = tmp.d + top;
     tmp.top = am.top = 0;
     tmp.dmax = am.dmax = top;
     tmp.neg = am.neg = 0;
     tmp.flags = am.flags = BN_FLG_STATIC_DATA;
 
     /* prepare a^0 in Montgomery domain */
 #if 1                           /* by Shay Gueron's suggestion */
     if (m->d[top - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {
         /* 2^(top*BN_BITS2) - m */
         tmp.d[0] = (0 - m->d[0]) & BN_MASK2;
         for (i = 1; i < top; i++)
             tmp.d[i] = (~m->d[i]) & BN_MASK2;
         tmp.top = top;
     } else
 #endif
     if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))
         goto err;
 
     /* prepare a^1 in Montgomery domain */
     if (a->neg || BN_ucmp(a, m) >= 0) {
         if (!BN_mod(&am, a, m, ctx))
             goto err;
         if (!BN_to_montgomery(&am, &am, mont, ctx))
             goto err;
     } else if (!BN_to_montgomery(&am, a, mont, ctx))
         goto err;
 
 #if defined(SPARC_T4_MONT)
     if (t4) {
         typedef int (*bn_pwr5_mont_f) (BN_ULONG *tp, const BN_ULONG *np,
                                        const BN_ULONG *n0, const void *table,
                                        int power, int bits);
         int bn_pwr5_mont_t4_8(BN_ULONG *tp, const BN_ULONG *np,
                               const BN_ULONG *n0, const void *table,
                               int power, int bits);
         int bn_pwr5_mont_t4_16(BN_ULONG *tp, const BN_ULONG *np,
                                const BN_ULONG *n0, const void *table,
                                int power, int bits);
         int bn_pwr5_mont_t4_24(BN_ULONG *tp, const BN_ULONG *np,
                                const BN_ULONG *n0, const void *table,
                                int power, int bits);
         int bn_pwr5_mont_t4_32(BN_ULONG *tp, const BN_ULONG *np,
                                const BN_ULONG *n0, const void *table,
                                int power, int bits);
         static const bn_pwr5_mont_f pwr5_funcs[4] = {
             bn_pwr5_mont_t4_8, bn_pwr5_mont_t4_16,
             bn_pwr5_mont_t4_24, bn_pwr5_mont_t4_32
         };
         bn_pwr5_mont_f pwr5_worker = pwr5_funcs[top / 16 - 1];
 
         typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap,
                                       const void *bp, const BN_ULONG *np,
                                       const BN_ULONG *n0);
         int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, const void *bp,
                              const BN_ULONG *np, const BN_ULONG *n0);
         int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap,
                               const void *bp, const BN_ULONG *np,
                               const BN_ULONG *n0);
         int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap,
                               const void *bp, const BN_ULONG *np,
                               const BN_ULONG *n0);
         int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap,
                               const void *bp, const BN_ULONG *np,
                               const BN_ULONG *n0);
         static const bn_mul_mont_f mul_funcs[4] = {
             bn_mul_mont_t4_8, bn_mul_mont_t4_16,
             bn_mul_mont_t4_24, bn_mul_mont_t4_32
         };
         bn_mul_mont_f mul_worker = mul_funcs[top / 16 - 1];
 
         void bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap,
                               const void *bp, const BN_ULONG *np,
                               const BN_ULONG *n0, int num);
         void bn_mul_mont_t4(BN_ULONG *rp, const BN_ULONG *ap,
                             const void *bp, const BN_ULONG *np,
                             const BN_ULONG *n0, int num);
         void bn_mul_mont_gather5_t4(BN_ULONG *rp, const BN_ULONG *ap,
                                     const void *table, const BN_ULONG *np,
                                     const BN_ULONG *n0, int num, int power);
         void bn_flip_n_scatter5_t4(const BN_ULONG *inp, size_t num,
                                    void *table, size_t power);
         void bn_gather5_t4(BN_ULONG *out, size_t num,
                            void *table, size_t power);
         void bn_flip_t4(BN_ULONG *dst, BN_ULONG *src, size_t num);
 
         BN_ULONG *np = mont->N.d, *n0 = mont->n0;
         int stride = 5 * (6 - (top / 16 - 1)); /* multiple of 5, but less
                                                 * than 32 */
 
         /*
          * BN_to_montgomery can contaminate words above .top [in
          * BN_DEBUG[_DEBUG] build]...
          */
         for (i = am.top; i < top; i++)
             am.d[i] = 0;
         for (i = tmp.top; i < top; i++)
             tmp.d[i] = 0;
 
         bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 0);
         bn_flip_n_scatter5_t4(am.d, top, powerbuf, 1);
         if (!(*mul_worker) (tmp.d, am.d, am.d, np, n0) &&
             !(*mul_worker) (tmp.d, am.d, am.d, np, n0))
             bn_mul_mont_vis3(tmp.d, am.d, am.d, np, n0, top);
         bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, 2);
 
         for (i = 3; i < 32; i++) {
             /* Calculate a^i = a^(i-1) * a */
             if (!(*mul_worker) (tmp.d, tmp.d, am.d, np, n0) &&
                 !(*mul_worker) (tmp.d, tmp.d, am.d, np, n0))
                 bn_mul_mont_vis3(tmp.d, tmp.d, am.d, np, n0, top);
             bn_flip_n_scatter5_t4(tmp.d, top, powerbuf, i);
         }
 
         /* switch to 64-bit domain */
         np = alloca(top * sizeof(BN_ULONG));
         top /= 2;
         bn_flip_t4(np, mont->N.d, top);
 
         bits--;
         for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)
             wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
         bn_gather5_t4(tmp.d, top, powerbuf, wvalue);
 
         /*
          * Scan the exponent one window at a time starting from the most
          * significant bits.
          */
         while (bits >= 0) {
             if (bits < stride)
                 stride = bits + 1;
             bits -= stride;
             wvalue = bn_get_bits(p, bits + 1);
 
             if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
                 continue;
             /* retry once and fall back */
             if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
                 continue;
 
             bits += stride - 5;
             wvalue >>= stride - 5;
             wvalue &= 31;
             bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_mul_mont_t4(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_mul_mont_gather5_t4(tmp.d, tmp.d, powerbuf, np, n0, top,
                                    wvalue);
         }
 
         bn_flip_t4(tmp.d, tmp.d, top);
         top *= 2;
         /* back to 32-bit domain */
         tmp.top = top;
         bn_correct_top(&tmp);
         OPENSSL_cleanse(np, top * sizeof(BN_ULONG));
     } else
 #endif
 #if defined(OPENSSL_BN_ASM_MONT5)
     if (window == 5 && top > 1) {
         /*
          * This optimization uses ideas from http://eprint.iacr.org/2011/239,
          * specifically optimization of cache-timing attack countermeasures
          * and pre-computation optimization.
          */
 
         /*
          * Dedicated window==4 case improves 512-bit RSA sign by ~15%, but as
          * 512-bit RSA is hardly relevant, we omit it to spare size...
          */
         void bn_mul_mont_gather5(BN_ULONG *rp, const BN_ULONG *ap,
                                  const void *table, const BN_ULONG *np,
                                  const BN_ULONG *n0, int num, int power);
         void bn_scatter5(const BN_ULONG *inp, size_t num,
                          void *table, size_t power);
         void bn_gather5(BN_ULONG *out, size_t num, void *table, size_t power);
         void bn_power5(BN_ULONG *rp, const BN_ULONG *ap,
                        const void *table, const BN_ULONG *np,
                        const BN_ULONG *n0, int num, int power);
         int bn_get_bits5(const BN_ULONG *ap, int off);
         int bn_from_montgomery(BN_ULONG *rp, const BN_ULONG *ap,
                                const BN_ULONG *not_used, const BN_ULONG *np,
                                const BN_ULONG *n0, int num);
 
-        BN_ULONG *np = mont->N.d, *n0 = mont->n0, *np2;
+        BN_ULONG *n0 = mont->n0, *np;
 
         /*
          * BN_to_montgomery can contaminate words above .top [in
          * BN_DEBUG[_DEBUG] build]...
          */
         for (i = am.top; i < top; i++)
             am.d[i] = 0;
         for (i = tmp.top; i < top; i++)
             tmp.d[i] = 0;
 
-        if (top & 7)
-            np2 = np;
-        else
-            for (np2 = am.d + top, i = 0; i < top; i++)
-                np2[2 * i] = np[i];
+        /*
+         * copy mont->N.d[] to improve cache locality
+         */
+        for (np = am.d + top, i = 0; i < top; i++)
+            np[i] = mont->N.d[i];
 
         bn_scatter5(tmp.d, top, powerbuf, 0);
         bn_scatter5(am.d, am.top, powerbuf, 1);
         bn_mul_mont(tmp.d, am.d, am.d, np, n0, top);
         bn_scatter5(tmp.d, top, powerbuf, 2);
 
 # if 0
         for (i = 3; i < 32; i++) {
             /* Calculate a^i = a^(i-1) * a */
-            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);
+            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
             bn_scatter5(tmp.d, top, powerbuf, i);
         }
 # else
         /* same as above, but uses squaring for 1/2 of operations */
         for (i = 4; i < 32; i *= 2) {
             bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_scatter5(tmp.d, top, powerbuf, i);
         }
         for (i = 3; i < 8; i += 2) {
             int j;
-            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);
+            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
             bn_scatter5(tmp.d, top, powerbuf, i);
             for (j = 2 * i; j < 32; j *= 2) {
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_scatter5(tmp.d, top, powerbuf, j);
             }
         }
         for (; i < 16; i += 2) {
-            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);
+            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
             bn_scatter5(tmp.d, top, powerbuf, i);
             bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
             bn_scatter5(tmp.d, top, powerbuf, 2 * i);
         }
         for (; i < 32; i += 2) {
-            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np2, n0, top, i - 1);
+            bn_mul_mont_gather5(tmp.d, am.d, powerbuf, np, n0, top, i - 1);
             bn_scatter5(tmp.d, top, powerbuf, i);
         }
 # endif
         bits--;
         for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)
             wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
         bn_gather5(tmp.d, top, powerbuf, wvalue);
 
         /*
          * Scan the exponent one window at a time starting from the most
          * significant bits.
          */
         if (top & 7)
             while (bits >= 0) {
                 for (wvalue = 0, i = 0; i < 5; i++, bits--)
                     wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
 
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
                 bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,
                                     wvalue);
         } else {
             while (bits >= 0) {
                 wvalue = bn_get_bits5(p->d, bits - 4);
                 bits -= 5;
-                bn_power5(tmp.d, tmp.d, powerbuf, np2, n0, top, wvalue);
+                bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);
             }
         }
 
-        ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np2, n0, top);
+        ret = bn_from_montgomery(tmp.d, tmp.d, NULL, np, n0, top);
         tmp.top = top;
         bn_correct_top(&tmp);
         if (ret) {
             if (!BN_copy(rr, &tmp))
                 ret = 0;
             goto err;           /* non-zero ret means it's not error */
         }
     } else
 #endif
     {
-        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, numPowers))
+        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 0, window))
             goto err;
-        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, numPowers))
+        if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&am, top, powerbuf, 1, window))
             goto err;
 
         /*
          * If the window size is greater than 1, then calculate
          * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even
          * powers could instead be computed as (a^(i/2))^2 to use the slight
          * performance advantage of sqr over mul).
          */
         if (window > 1) {
             if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))
                 goto err;
-            if (!MOD_EXP_CTIME_COPY_TO_PREBUF
-                (&tmp, top, powerbuf, 2, numPowers))
+            if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,
+                                              window))
                 goto err;
             for (i = 3; i < numPowers; i++) {
                 /* Calculate a^i = a^(i-1) * a */
                 if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))
                     goto err;
-                if (!MOD_EXP_CTIME_COPY_TO_PREBUF
-                    (&tmp, top, powerbuf, i, numPowers))
+                if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,
+                                                  window))
                     goto err;
             }
         }
 
         bits--;
         for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)
             wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
-        if (!MOD_EXP_CTIME_COPY_FROM_PREBUF
-            (&tmp, top, powerbuf, wvalue, numPowers))
+        if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,
+                                            window))
             goto err;
 
         /*
          * Scan the exponent one window at a time starting from the most
          * significant bits.
          */
         while (bits >= 0) {
             wvalue = 0;         /* The 'value' of the window */
 
             /* Scan the window, squaring the result as we go */
             for (i = 0; i < window; i++, bits--) {
                 if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))
                     goto err;
                 wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
             }
 
             /*
              * Fetch the appropriate pre-computed value from the pre-buf
              */
-            if (!MOD_EXP_CTIME_COPY_FROM_PREBUF
-                (&am, top, powerbuf, wvalue, numPowers))
+            if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,
+                                                window))
                 goto err;
 
             /* Multiply the result into the intermediate result */
             if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))
                 goto err;
         }
     }
 
     /* Convert the final result from montgomery to standard format */
 #if defined(SPARC_T4_MONT)
     if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
         am.d[0] = 1;            /* borrow am */
         for (i = 1; i < top; i++)
             am.d[i] = 0;
         if (!BN_mod_mul_montgomery(rr, &tmp, &am, mont, ctx))
             goto err;
     } else
 #endif
     if (!BN_from_montgomery(rr, &tmp, mont, ctx))
         goto err;
     ret = 1;
  err:
     if ((in_mont == NULL) && (mont != NULL))
         BN_MONT_CTX_free(mont);
     if (powerbuf != NULL) {
         OPENSSL_cleanse(powerbuf, powerbufLen);
         if (powerbufFree)
             OPENSSL_free(powerbufFree);
     }
     BN_CTX_end(ctx);
     return (ret);
 }
 
 int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
                          const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
 {
     BN_MONT_CTX *mont = NULL;
     int b, bits, ret = 0;
     int r_is_one;
     BN_ULONG w, next_w;
     BIGNUM *d, *r, *t;
     BIGNUM *swap_tmp;
 #define BN_MOD_MUL_WORD(r, w, m) \
                 (BN_mul_word(r, (w)) && \
                 (/* BN_ucmp(r, (m)) < 0 ? 1 :*/  \
                         (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
     /*
      * BN_MOD_MUL_WORD is only used with 'w' large, so the BN_ucmp test is
      * probably more overhead than always using BN_mod (which uses BN_copy if
      * a similar test returns true).
      */
     /*
      * We can use BN_mod and do not need BN_nnmod because our accumulator is
      * never negative (the result of BN_mod does not depend on the sign of
      * the modulus).
      */
 #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
                 (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
 
     if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
         /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
         BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
         return -1;
     }
 
     bn_check_top(p);
     bn_check_top(m);
 
     if (!BN_is_odd(m)) {
         BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);
         return (0);
     }
     if (m->top == 1)
         a %= m->d[0];           /* make sure that 'a' is reduced */
 
     bits = BN_num_bits(p);
     if (bits == 0) {
         /* x**0 mod 1 is still zero. */
         if (BN_is_one(m)) {
             ret = 1;
             BN_zero(rr);
         } else {
             ret = BN_one(rr);
         }
         return ret;
     }
     if (a == 0) {
         BN_zero(rr);
         ret = 1;
         return ret;
     }
 
     BN_CTX_start(ctx);
     d = BN_CTX_get(ctx);
     r = BN_CTX_get(ctx);
     t = BN_CTX_get(ctx);
     if (d == NULL || r == NULL || t == NULL)
         goto err;
 
     if (in_mont != NULL)
         mont = in_mont;
     else {
         if ((mont = BN_MONT_CTX_new()) == NULL)
             goto err;
         if (!BN_MONT_CTX_set(mont, m, ctx))
             goto err;
     }
 
     r_is_one = 1;               /* except for Montgomery factor */
 
     /* bits-1 >= 0 */
 
     /* The result is accumulated in the product r*w. */
     w = a;                      /* bit 'bits-1' of 'p' is always set */
     for (b = bits - 2; b >= 0; b--) {
         /* First, square r*w. */
         next_w = w * w;
         if ((next_w / w) != w) { /* overflow */
             if (r_is_one) {
                 if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
                     goto err;
                 r_is_one = 0;
             } else {
                 if (!BN_MOD_MUL_WORD(r, w, m))
                     goto err;
             }
             next_w = 1;
         }
         w = next_w;
         if (!r_is_one) {
             if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
                 goto err;
         }
 
         /* Second, multiply r*w by 'a' if exponent bit is set. */
         if (BN_is_bit_set(p, b)) {
             next_w = w * a;
             if ((next_w / a) != w) { /* overflow */
                 if (r_is_one) {
                     if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
                         goto err;
                     r_is_one = 0;
                 } else {
                     if (!BN_MOD_MUL_WORD(r, w, m))
                         goto err;
                 }
                 next_w = a;
             }
             w = next_w;
         }
     }
 
     /* Finally, set r:=r*w. */
     if (w != 1) {
         if (r_is_one) {
             if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
                 goto err;
             r_is_one = 0;
         } else {
             if (!BN_MOD_MUL_WORD(r, w, m))
                 goto err;
         }
     }
 
     if (r_is_one) {             /* can happen only if a == 1 */
         if (!BN_one(rr))
             goto err;
     } else {
         if (!BN_from_montgomery(rr, r, mont, ctx))
             goto err;
     }
     ret = 1;
  err:
     if ((in_mont == NULL) && (mont != NULL))
         BN_MONT_CTX_free(mont);
     BN_CTX_end(ctx);
     bn_check_top(rr);
     return (ret);
 }
 
 /* The old fallback, simple version :-) */
 int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
                       const BIGNUM *m, BN_CTX *ctx)
 {
     int i, j, bits, ret = 0, wstart, wend, window, wvalue;
     int start = 1;
     BIGNUM *d;
     /* Table of variables obtained from 'ctx' */
     BIGNUM *val[TABLE_SIZE];
 
     if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
         /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
         BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
         return -1;
     }
 
     bits = BN_num_bits(p);
    if (bits == 0) {
         /* x**0 mod 1 is still zero. */
         if (BN_is_one(m)) {
             ret = 1;
             BN_zero(r);
         } else {
             ret = BN_one(r);
         }
         return ret;
     }
 
     BN_CTX_start(ctx);
     d = BN_CTX_get(ctx);
     val[0] = BN_CTX_get(ctx);
     if (!d || !val[0])
         goto err;
 
     if (!BN_nnmod(val[0], a, m, ctx))
         goto err;               /* 1 */
     if (BN_is_zero(val[0])) {
         BN_zero(r);
         ret = 1;
         goto err;
     }
 
     window = BN_window_bits_for_exponent_size(bits);
     if (window > 1) {
         if (!BN_mod_mul(d, val[0], val[0], m, ctx))
             goto err;           /* 2 */
         j = 1 << (window - 1);
         for (i = 1; i < j; i++) {
             if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
                 !BN_mod_mul(val[i], val[i - 1], d, m, ctx))
                 goto err;
         }
     }
 
     start = 1;                  /* This is used to avoid multiplication etc
                                  * when there is only the value '1' in the
                                  * buffer. */
     wvalue = 0;                 /* The 'value' of the window */
     wstart = bits - 1;          /* The top bit of the window */
     wend = 0;                   /* The bottom bit of the window */
 
     if (!BN_one(r))
         goto err;
 
     for (;;) {
         if (BN_is_bit_set(p, wstart) == 0) {
             if (!start)
                 if (!BN_mod_mul(r, r, r, m, ctx))
                     goto err;
             if (wstart == 0)
                 break;
             wstart--;
             continue;
         }
         /*
          * We now have wstart on a 'set' bit, we now need to work out how bit
          * a window to do.  To do this we need to scan forward until the last
          * set bit before the end of the window
          */
         j = wstart;
         wvalue = 1;
         wend = 0;
         for (i = 1; i < window; i++) {
             if (wstart - i < 0)
                 break;
             if (BN_is_bit_set(p, wstart - i)) {
                 wvalue <<= (i - wend);
                 wvalue |= 1;
                 wend = i;
             }
         }
 
         /* wend is the size of the current window */
         j = wend + 1;
         /* add the 'bytes above' */
         if (!start)
             for (i = 0; i < j; i++) {
                 if (!BN_mod_mul(r, r, r, m, ctx))
                     goto err;
             }
 
         /* wvalue will be an odd number < 2^window */
         if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))
             goto err;
 
         /* move the 'window' down further */
         wstart -= wend + 1;
         wvalue = 0;
         start = 0;
         if (wstart < 0)
             break;
     }
     ret = 1;
  err:
     BN_CTX_end(ctx);
     bn_check_top(r);
     return (ret);
 }
Index: vendor-crypto/openssl/dist/crypto/bn/bn_print.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/bn_print.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/bn_print.c	(revision 296273)
@@ -1,388 +1,397 @@
 /* crypto/bn/bn_print.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include 
+#include 
 #include "cryptlib.h"
 #include 
 #include "bn_lcl.h"
 
 static const char Hex[] = "0123456789ABCDEF";
 
 /* Must 'OPENSSL_free' the returned data */
 char *BN_bn2hex(const BIGNUM *a)
 {
     int i, j, v, z = 0;
     char *buf;
     char *p;
 
     if (a->neg && BN_is_zero(a)) {
         /* "-0" == 3 bytes including NULL terminator */
         buf = OPENSSL_malloc(3);
     } else {
         buf = OPENSSL_malloc(a->top * BN_BYTES * 2 + 2);
     }
     if (buf == NULL) {
         BNerr(BN_F_BN_BN2HEX, ERR_R_MALLOC_FAILURE);
         goto err;
     }
     p = buf;
     if (a->neg)
         *(p++) = '-';
     if (BN_is_zero(a))
         *(p++) = '0';
     for (i = a->top - 1; i >= 0; i--) {
         for (j = BN_BITS2 - 8; j >= 0; j -= 8) {
             /* strip leading zeros */
             v = ((int)(a->d[i] >> (long)j)) & 0xff;
             if (z || (v != 0)) {
                 *(p++) = Hex[v >> 4];
                 *(p++) = Hex[v & 0x0f];
                 z = 1;
             }
         }
     }
     *p = '\0';
  err:
     return (buf);
 }
 
 /* Must 'OPENSSL_free' the returned data */
 char *BN_bn2dec(const BIGNUM *a)
 {
     int i = 0, num, ok = 0;
     char *buf = NULL;
     char *p;
     BIGNUM *t = NULL;
     BN_ULONG *bn_data = NULL, *lp;
 
     /*-
      * get an upper bound for the length of the decimal integer
      * num <= (BN_num_bits(a) + 1) * log(2)
      *     <= 3 * BN_num_bits(a) * 0.1001 + log(2) + 1     (rounding error)
      *     <= BN_num_bits(a)/10 + BN_num_bits/1000 + 1 + 1
      */
     i = BN_num_bits(a) * 3;
     num = (i / 10 + i / 1000 + 1) + 1;
     bn_data =
         (BN_ULONG *)OPENSSL_malloc((num / BN_DEC_NUM + 1) * sizeof(BN_ULONG));
     buf = (char *)OPENSSL_malloc(num + 3);
     if ((buf == NULL) || (bn_data == NULL)) {
         BNerr(BN_F_BN_BN2DEC, ERR_R_MALLOC_FAILURE);
         goto err;
     }
     if ((t = BN_dup(a)) == NULL)
         goto err;
 
 #define BUF_REMAIN (num+3 - (size_t)(p - buf))
     p = buf;
     lp = bn_data;
     if (BN_is_zero(t)) {
         *(p++) = '0';
         *(p++) = '\0';
     } else {
         if (BN_is_negative(t))
             *p++ = '-';
 
         i = 0;
         while (!BN_is_zero(t)) {
             *lp = BN_div_word(t, BN_DEC_CONV);
             lp++;
         }
         lp--;
         /*
          * We now have a series of blocks, BN_DEC_NUM chars in length, where
          * the last one needs truncation. The blocks need to be reversed in
          * order.
          */
         BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT1, *lp);
         while (*p)
             p++;
         while (lp != bn_data) {
             lp--;
             BIO_snprintf(p, BUF_REMAIN, BN_DEC_FMT2, *lp);
             while (*p)
                 p++;
         }
     }
     ok = 1;
  err:
     if (bn_data != NULL)
         OPENSSL_free(bn_data);
     if (t != NULL)
         BN_free(t);
     if (!ok && buf) {
         OPENSSL_free(buf);
         buf = NULL;
     }
 
     return (buf);
 }
 
 int BN_hex2bn(BIGNUM **bn, const char *a)
 {
     BIGNUM *ret = NULL;
     BN_ULONG l = 0;
     int neg = 0, h, m, i, j, k, c;
     int num;
 
     if ((a == NULL) || (*a == '\0'))
         return (0);
 
     if (*a == '-') {
         neg = 1;
         a++;
     }
 
-    for (i = 0; isxdigit((unsigned char)a[i]); i++) ;
+    for (i = 0; i <= (INT_MAX/4) && isxdigit((unsigned char)a[i]); i++)
+        continue;
 
+    if (i > INT_MAX/4)
+        goto err;
+
     num = i + neg;
     if (bn == NULL)
         return (num);
 
     /* a is the start of the hex digits, and it is 'i' long */
     if (*bn == NULL) {
         if ((ret = BN_new()) == NULL)
             return (0);
     } else {
         ret = *bn;
         BN_zero(ret);
     }
 
-    /* i is the number of hex digests; */
+    /* i is the number of hex digits */
     if (bn_expand(ret, i * 4) == NULL)
         goto err;
 
     j = i;                      /* least significant 'hex' */
     m = 0;
     h = 0;
     while (j > 0) {
         m = ((BN_BYTES * 2) <= j) ? (BN_BYTES * 2) : j;
         l = 0;
         for (;;) {
             c = a[j - m];
             if ((c >= '0') && (c <= '9'))
                 k = c - '0';
             else if ((c >= 'a') && (c <= 'f'))
                 k = c - 'a' + 10;
             else if ((c >= 'A') && (c <= 'F'))
                 k = c - 'A' + 10;
             else
                 k = 0;          /* paranoia */
             l = (l << 4) | k;
 
             if (--m <= 0) {
                 ret->d[h++] = l;
                 break;
             }
         }
         j -= (BN_BYTES * 2);
     }
     ret->top = h;
     bn_correct_top(ret);
     ret->neg = neg;
 
     *bn = ret;
     bn_check_top(ret);
     return (num);
  err:
     if (*bn == NULL)
         BN_free(ret);
     return (0);
 }
 
 int BN_dec2bn(BIGNUM **bn, const char *a)
 {
     BIGNUM *ret = NULL;
     BN_ULONG l = 0;
     int neg = 0, i, j;
     int num;
 
     if ((a == NULL) || (*a == '\0'))
         return (0);
     if (*a == '-') {
         neg = 1;
         a++;
     }
 
-    for (i = 0; isdigit((unsigned char)a[i]); i++) ;
+    for (i = 0; i <= (INT_MAX/4) && isdigit((unsigned char)a[i]); i++)
+        continue;
 
+    if (i > INT_MAX/4)
+        goto err;
+
     num = i + neg;
     if (bn == NULL)
         return (num);
 
     /*
      * a is the start of the digits, and it is 'i' long. We chop it into
      * BN_DEC_NUM digits at a time
      */
     if (*bn == NULL) {
         if ((ret = BN_new()) == NULL)
             return (0);
     } else {
         ret = *bn;
         BN_zero(ret);
     }
 
-    /* i is the number of digests, a bit of an over expand; */
+    /* i is the number of digits, a bit of an over expand */
     if (bn_expand(ret, i * 4) == NULL)
         goto err;
 
     j = BN_DEC_NUM - (i % BN_DEC_NUM);
     if (j == BN_DEC_NUM)
         j = 0;
     l = 0;
     while (*a) {
         l *= 10;
         l += *a - '0';
         a++;
         if (++j == BN_DEC_NUM) {
             BN_mul_word(ret, BN_DEC_CONV);
             BN_add_word(ret, l);
             l = 0;
             j = 0;
         }
     }
     ret->neg = neg;
 
     bn_correct_top(ret);
     *bn = ret;
     bn_check_top(ret);
     return (num);
  err:
     if (*bn == NULL)
         BN_free(ret);
     return (0);
 }
 
 int BN_asc2bn(BIGNUM **bn, const char *a)
 {
     const char *p = a;
     if (*p == '-')
         p++;
 
     if (p[0] == '0' && (p[1] == 'X' || p[1] == 'x')) {
         if (!BN_hex2bn(bn, p + 2))
             return 0;
     } else {
         if (!BN_dec2bn(bn, p))
             return 0;
     }
     if (*a == '-')
         (*bn)->neg = 1;
     return 1;
 }
 
 #ifndef OPENSSL_NO_BIO
 # ifndef OPENSSL_NO_FP_API
 int BN_print_fp(FILE *fp, const BIGNUM *a)
 {
     BIO *b;
     int ret;
 
     if ((b = BIO_new(BIO_s_file())) == NULL)
         return (0);
     BIO_set_fp(b, fp, BIO_NOCLOSE);
     ret = BN_print(b, a);
     BIO_free(b);
     return (ret);
 }
 # endif
 
 int BN_print(BIO *bp, const BIGNUM *a)
 {
     int i, j, v, z = 0;
     int ret = 0;
 
     if ((a->neg) && (BIO_write(bp, "-", 1) != 1))
         goto end;
     if (BN_is_zero(a) && (BIO_write(bp, "0", 1) != 1))
         goto end;
     for (i = a->top - 1; i >= 0; i--) {
         for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
             /* strip leading zeros */
             v = ((int)(a->d[i] >> (long)j)) & 0x0f;
             if (z || (v != 0)) {
                 if (BIO_write(bp, &(Hex[v]), 1) != 1)
                     goto end;
                 z = 1;
             }
         }
     }
     ret = 1;
  end:
     return (ret);
 }
 #endif
 
 char *BN_options(void)
 {
     static int init = 0;
     static char data[16];
 
     if (!init) {
         init++;
 #ifdef BN_LLONG
         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
                      (int)sizeof(BN_ULLONG) * 8, (int)sizeof(BN_ULONG) * 8);
 #else
         BIO_snprintf(data, sizeof data, "bn(%d,%d)",
                      (int)sizeof(BN_ULONG) * 8, (int)sizeof(BN_ULONG) * 8);
 #endif
     }
     return (data);
 }
Index: vendor-crypto/openssl/dist/crypto/bn/bn_recp.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/bn/bn_recp.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/bn/bn_recp.c	(revision 296273)
@@ -1,251 +1,252 @@
 /* crypto/bn/bn_recp.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include "cryptlib.h"
 #include "bn_lcl.h"
 
 void BN_RECP_CTX_init(BN_RECP_CTX *recp)
 {
     BN_init(&(recp->N));
     BN_init(&(recp->Nr));
     recp->num_bits = 0;
+    recp->shift = 0;
     recp->flags = 0;
 }
 
 BN_RECP_CTX *BN_RECP_CTX_new(void)
 {
     BN_RECP_CTX *ret;
 
     if ((ret = (BN_RECP_CTX *)OPENSSL_malloc(sizeof(BN_RECP_CTX))) == NULL)
         return (NULL);
 
     BN_RECP_CTX_init(ret);
     ret->flags = BN_FLG_MALLOCED;
     return (ret);
 }
 
 void BN_RECP_CTX_free(BN_RECP_CTX *recp)
 {
     if (recp == NULL)
         return;
 
     BN_free(&(recp->N));
     BN_free(&(recp->Nr));
     if (recp->flags & BN_FLG_MALLOCED)
         OPENSSL_free(recp);
 }
 
 int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
 {
     if (!BN_copy(&(recp->N), d))
         return 0;
     BN_zero(&(recp->Nr));
     recp->num_bits = BN_num_bits(d);
     recp->shift = 0;
     return (1);
 }
 
 int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
                           BN_RECP_CTX *recp, BN_CTX *ctx)
 {
     int ret = 0;
     BIGNUM *a;
     const BIGNUM *ca;
 
     BN_CTX_start(ctx);
     if ((a = BN_CTX_get(ctx)) == NULL)
         goto err;
     if (y != NULL) {
         if (x == y) {
             if (!BN_sqr(a, x, ctx))
                 goto err;
         } else {
             if (!BN_mul(a, x, y, ctx))
                 goto err;
         }
         ca = a;
     } else
         ca = x;                 /* Just do the mod */
 
     ret = BN_div_recp(NULL, r, ca, recp, ctx);
  err:
     BN_CTX_end(ctx);
     bn_check_top(r);
     return (ret);
 }
 
 int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
                 BN_RECP_CTX *recp, BN_CTX *ctx)
 {
     int i, j, ret = 0;
     BIGNUM *a, *b, *d, *r;
 
     BN_CTX_start(ctx);
     a = BN_CTX_get(ctx);
     b = BN_CTX_get(ctx);
     if (dv != NULL)
         d = dv;
     else
         d = BN_CTX_get(ctx);
     if (rem != NULL)
         r = rem;
     else
         r = BN_CTX_get(ctx);
     if (a == NULL || b == NULL || d == NULL || r == NULL)
         goto err;
 
     if (BN_ucmp(m, &(recp->N)) < 0) {
         BN_zero(d);
         if (!BN_copy(r, m)) {
             BN_CTX_end(ctx);
             return 0;
         }
         BN_CTX_end(ctx);
         return (1);
     }
 
     /*
      * We want the remainder Given input of ABCDEF / ab we need multiply
      * ABCDEF by 3 digests of the reciprocal of ab
      */
 
     /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
     i = BN_num_bits(m);
     j = recp->num_bits << 1;
     if (j > i)
         i = j;
 
     /* Nr := round(2^i / N) */
     if (i != recp->shift)
         recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
     /* BN_reciprocal could have returned -1 for an error */
     if (recp->shift == -1)
         goto err;
 
     /*-
      * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
      *    = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
      *   <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
      *    = |m/N|
      */
     if (!BN_rshift(a, m, recp->num_bits))
         goto err;
     if (!BN_mul(b, a, &(recp->Nr), ctx))
         goto err;
     if (!BN_rshift(d, b, i - recp->num_bits))
         goto err;
     d->neg = 0;
 
     if (!BN_mul(b, &(recp->N), d, ctx))
         goto err;
     if (!BN_usub(r, m, b))
         goto err;
     r->neg = 0;
 
 #if 1
     j = 0;
     while (BN_ucmp(r, &(recp->N)) >= 0) {
         if (j++ > 2) {
             BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);
             goto err;
         }
         if (!BN_usub(r, r, &(recp->N)))
             goto err;
         if (!BN_add_word(d, 1))
             goto err;
     }
 #endif
 
     r->neg = BN_is_zero(r) ? 0 : m->neg;
     d->neg = m->neg ^ recp->N.neg;
     ret = 1;
  err:
     BN_CTX_end(ctx);
     bn_check_top(dv);
     bn_check_top(rem);
     return (ret);
 }
 
 /*
  * len is the expected size of the result We actually calculate with an extra
  * word of precision, so we can do faster division if the remainder is not
  * required.
  */
 /* r := 2^len / m */
 int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
 {
     int ret = -1;
     BIGNUM *t;
 
     BN_CTX_start(ctx);
     if ((t = BN_CTX_get(ctx)) == NULL)
         goto err;
 
     if (!BN_set_bit(t, len))
         goto err;
 
     if (!BN_div(r, NULL, t, m, ctx))
         goto err;
 
     ret = len;
  err:
     bn_check_top(r);
     BN_CTX_end(ctx);
     return (ret);
 }
Index: vendor-crypto/openssl/dist/crypto/cmac/cmac.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/cmac/cmac.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/cmac/cmac.c	(revision 296273)
@@ -1,298 +1,306 @@
 /* crypto/cmac/cmac.c */
 /*
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project.
  */
 /* ====================================================================
  * Copyright (c) 2010 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  */
 
 #include 
 #include 
 #include 
 #include "cryptlib.h"
 #include 
 
 #ifdef OPENSSL_FIPS
 # include 
 #endif
 
 struct CMAC_CTX_st {
     /* Cipher context to use */
     EVP_CIPHER_CTX cctx;
     /* Keys k1 and k2 */
     unsigned char k1[EVP_MAX_BLOCK_LENGTH];
     unsigned char k2[EVP_MAX_BLOCK_LENGTH];
     /* Temporary block */
     unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
     /* Last (possibly partial) block */
     unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
     /* Number of bytes in last block: -1 means context not initialised */
     int nlast_block;
 };
 
 /* Make temporary keys K1 and K2 */
 
 static void make_kn(unsigned char *k1, unsigned char *l, int bl)
 {
     int i;
     /* Shift block to left, including carry */
     for (i = 0; i < bl; i++) {
         k1[i] = l[i] << 1;
         if (i < bl - 1 && l[i + 1] & 0x80)
             k1[i] |= 1;
     }
     /* If MSB set fixup with R */
     if (l[0] & 0x80)
         k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
 }
 
 CMAC_CTX *CMAC_CTX_new(void)
 {
     CMAC_CTX *ctx;
     ctx = OPENSSL_malloc(sizeof(CMAC_CTX));
     if (!ctx)
         return NULL;
     EVP_CIPHER_CTX_init(&ctx->cctx);
     ctx->nlast_block = -1;
     return ctx;
 }
 
 void CMAC_CTX_cleanup(CMAC_CTX *ctx)
 {
 #ifdef OPENSSL_FIPS
     if (FIPS_mode() && !ctx->cctx.engine) {
         FIPS_cmac_ctx_cleanup(ctx);
         return;
     }
 #endif
     EVP_CIPHER_CTX_cleanup(&ctx->cctx);
     OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
     OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
     OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
     OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
     ctx->nlast_block = -1;
 }
 
 EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
 {
     return &ctx->cctx;
 }
 
 void CMAC_CTX_free(CMAC_CTX *ctx)
 {
     if (!ctx)
         return;
     CMAC_CTX_cleanup(ctx);
     OPENSSL_free(ctx);
 }
 
 int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
 {
     int bl;
     if (in->nlast_block == -1)
         return 0;
     if (!EVP_CIPHER_CTX_copy(&out->cctx, &in->cctx))
         return 0;
     bl = EVP_CIPHER_CTX_block_size(&in->cctx);
     memcpy(out->k1, in->k1, bl);
     memcpy(out->k2, in->k2, bl);
     memcpy(out->tbl, in->tbl, bl);
     memcpy(out->last_block, in->last_block, bl);
     out->nlast_block = in->nlast_block;
     return 1;
 }
 
 int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
               const EVP_CIPHER *cipher, ENGINE *impl)
 {
     static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH];
 #ifdef OPENSSL_FIPS
     if (FIPS_mode()) {
         /* If we have an ENGINE need to allow non FIPS */
         if ((impl || ctx->cctx.engine)
             && !(ctx->cctx.flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)) {
             EVPerr(EVP_F_CMAC_INIT, EVP_R_DISABLED_FOR_FIPS);
             return 0;
         }
+
+        /* Switch to FIPS cipher implementation if possible */
+        if (cipher != NULL) {
+            const EVP_CIPHER *fcipher;
+            fcipher = FIPS_get_cipherbynid(EVP_CIPHER_nid(cipher));
+            if (fcipher != NULL)
+                cipher = fcipher;
+        }
         /*
          * Other algorithm blocking will be done in FIPS_cmac_init, via
          * FIPS_cipherinit().
          */
         if (!impl && !ctx->cctx.engine)
             return FIPS_cmac_init(ctx, key, keylen, cipher, NULL);
     }
 #endif
     /* All zeros means restart */
     if (!key && !cipher && !impl && keylen == 0) {
         /* Not initialised */
         if (ctx->nlast_block == -1)
             return 0;
         if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
             return 0;
         memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(&ctx->cctx));
         ctx->nlast_block = 0;
         return 1;
     }
     /* Initialiase context */
     if (cipher && !EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
         return 0;
     /* Non-NULL key means initialisation complete */
     if (key) {
         int bl;
         if (!EVP_CIPHER_CTX_cipher(&ctx->cctx))
             return 0;
         if (!EVP_CIPHER_CTX_set_key_length(&ctx->cctx, keylen))
             return 0;
         if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, key, zero_iv))
             return 0;
         bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
         if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, bl))
             return 0;
         make_kn(ctx->k1, ctx->tbl, bl);
         make_kn(ctx->k2, ctx->k1, bl);
         OPENSSL_cleanse(ctx->tbl, bl);
         /* Reset context again ready for first data block */
         if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
             return 0;
         /* Zero tbl so resume works */
         memset(ctx->tbl, 0, bl);
         ctx->nlast_block = 0;
     }
     return 1;
 }
 
 int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
 {
     const unsigned char *data = in;
     size_t bl;
 #ifdef OPENSSL_FIPS
     if (FIPS_mode() && !ctx->cctx.engine)
         return FIPS_cmac_update(ctx, in, dlen);
 #endif
     if (ctx->nlast_block == -1)
         return 0;
     if (dlen == 0)
         return 1;
     bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
     /* Copy into partial block if we need to */
     if (ctx->nlast_block > 0) {
         size_t nleft;
         nleft = bl - ctx->nlast_block;
         if (dlen < nleft)
             nleft = dlen;
         memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
         dlen -= nleft;
         ctx->nlast_block += nleft;
         /* If no more to process return */
         if (dlen == 0)
             return 1;
         data += nleft;
         /* Else not final block so encrypt it */
         if (!EVP_Cipher(&ctx->cctx, ctx->tbl, ctx->last_block, bl))
             return 0;
     }
     /* Encrypt all but one of the complete blocks left */
     while (dlen > bl) {
         if (!EVP_Cipher(&ctx->cctx, ctx->tbl, data, bl))
             return 0;
         dlen -= bl;
         data += bl;
     }
     /* Copy any data left to last block buffer */
     memcpy(ctx->last_block, data, dlen);
     ctx->nlast_block = dlen;
     return 1;
 
 }
 
 int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
 {
     int i, bl, lb;
 #ifdef OPENSSL_FIPS
     if (FIPS_mode() && !ctx->cctx.engine)
         return FIPS_cmac_final(ctx, out, poutlen);
 #endif
     if (ctx->nlast_block == -1)
         return 0;
     bl = EVP_CIPHER_CTX_block_size(&ctx->cctx);
     *poutlen = (size_t)bl;
     if (!out)
         return 1;
     lb = ctx->nlast_block;
     /* Is last block complete? */
     if (lb == bl) {
         for (i = 0; i < bl; i++)
             out[i] = ctx->last_block[i] ^ ctx->k1[i];
     } else {
         ctx->last_block[lb] = 0x80;
         if (bl - lb > 1)
             memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
         for (i = 0; i < bl; i++)
             out[i] = ctx->last_block[i] ^ ctx->k2[i];
     }
     if (!EVP_Cipher(&ctx->cctx, out, out, bl)) {
         OPENSSL_cleanse(out, bl);
         return 0;
     }
     return 1;
 }
 
 int CMAC_resume(CMAC_CTX *ctx)
 {
     if (ctx->nlast_block == -1)
         return 0;
     /*
      * The buffer "tbl" containes the last fully encrypted block which is the
      * last IV (or all zeroes if no last encrypted block). The last block has
      * not been modified since CMAC_final(). So reinitliasing using the last
      * decrypted block will allow CMAC to continue after calling
      * CMAC_Final().
      */
     return EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, ctx->tbl);
 }
Index: vendor-crypto/openssl/dist/crypto/cryptlib.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/cryptlib.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/cryptlib.c	(revision 296273)
@@ -1,1030 +1,1030 @@
 /* crypto/cryptlib.c */
 /* ====================================================================
  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  * ECDH support in OpenSSL originally developed by
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  */
 
 #include "cryptlib.h"
 #include 
 
 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
 static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
 #endif
 
 DECLARE_STACK_OF(CRYPTO_dynlock)
 
 /* real #defines in crypto.h, keep these upto date */
 static const char *const lock_names[CRYPTO_NUM_LOCKS] = {
     "<>",
     "err",
     "ex_data",
     "x509",
     "x509_info",
     "x509_pkey",
     "x509_crl",
     "x509_req",
     "dsa",
     "rsa",
     "evp_pkey",
     "x509_store",
     "ssl_ctx",
     "ssl_cert",
     "ssl_session",
     "ssl_sess_cert",
     "ssl",
     "ssl_method",
     "rand",
     "rand2",
     "debug_malloc",
     "BIO",
     "gethostbyname",
     "getservbyname",
     "readdir",
     "RSA_blinding",
     "dh",
     "debug_malloc2",
     "dso",
     "dynlock",
     "engine",
     "ui",
     "ecdsa",
     "ec",
     "ecdh",
     "bn",
     "ec_pre_comp",
     "store",
     "comp",
     "fips",
     "fips2",
 #if CRYPTO_NUM_LOCKS != 41
 # error "Inconsistency between crypto.h and cryptlib.c"
 #endif
 };
 
 /*
  * This is for applications to allocate new type names in the non-dynamic
  * array of lock names.  These are numbered with positive numbers.
  */
 static STACK_OF(OPENSSL_STRING) *app_locks = NULL;
 
 /*
  * For applications that want a more dynamic way of handling threads, the
  * following stack is used.  These are externally numbered with negative
  * numbers.
  */
 static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
 
 static void (MS_FAR *locking_callback) (int mode, int type,
                                         const char *file, int line) = 0;
 static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
                                         int type, const char *file,
                                         int line) = 0;
 #ifndef OPENSSL_NO_DEPRECATED
 static unsigned long (MS_FAR *id_callback) (void) = 0;
 #endif
 static void (MS_FAR *threadid_callback) (CRYPTO_THREADID *) = 0;
 static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
  (const char *file, int line) = 0;
 static void (MS_FAR *dynlock_lock_callback) (int mode,
                                              struct CRYPTO_dynlock_value *l,
                                              const char *file, int line) = 0;
 static void (MS_FAR *dynlock_destroy_callback) (struct CRYPTO_dynlock_value
                                                 *l, const char *file,
                                                 int line) = 0;
 
 int CRYPTO_get_new_lockid(char *name)
 {
     char *str;
     int i;
 
 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
     /*
      * A hack to make Visual C++ 5.0 work correctly when linking as a DLL
      * using /MT. Without this, the application cannot use any floating point
      * printf's. It also seems to be needed for Visual C 1.5 (win16)
      */
     SSLeay_MSVC5_hack = (double)name[0] * (double)name[1];
 #endif
 
     if ((app_locks == NULL)
         && ((app_locks = sk_OPENSSL_STRING_new_null()) == NULL)) {
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
         return (0);
     }
     if ((str = BUF_strdup(name)) == NULL) {
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
         return (0);
     }
     i = sk_OPENSSL_STRING_push(app_locks, str);
     if (!i)
         OPENSSL_free(str);
     else
         i += CRYPTO_NUM_LOCKS;  /* gap of one :-) */
     return (i);
 }
 
 int CRYPTO_num_locks(void)
 {
     return CRYPTO_NUM_LOCKS;
 }
 
 int CRYPTO_get_new_dynlockid(void)
 {
     int i = 0;
     CRYPTO_dynlock *pointer = NULL;
 
     if (dynlock_create_callback == NULL) {
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,
                   CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
         return (0);
     }
     CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
     if ((dyn_locks == NULL)
         && ((dyn_locks = sk_CRYPTO_dynlock_new_null()) == NULL)) {
         CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
         return (0);
     }
     CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
 
     pointer = (CRYPTO_dynlock *) OPENSSL_malloc(sizeof(CRYPTO_dynlock));
     if (pointer == NULL) {
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
         return (0);
     }
     pointer->references = 1;
     pointer->data = dynlock_create_callback(__FILE__, __LINE__);
     if (pointer->data == NULL) {
         OPENSSL_free(pointer);
         CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
         return (0);
     }
 
     CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
     /* First, try to find an existing empty slot */
     i = sk_CRYPTO_dynlock_find(dyn_locks, NULL);
     /* If there was none, push, thereby creating a new one */
     if (i == -1)
         /*
          * Since sk_push() returns the number of items on the stack, not the
          * location of the pushed item, we need to transform the returned
          * number into a position, by decreasing it.
          */
         i = sk_CRYPTO_dynlock_push(dyn_locks, pointer) - 1;
     else
         /*
          * If we found a place with a NULL pointer, put our pointer in it.
          */
         (void)sk_CRYPTO_dynlock_set(dyn_locks, i, pointer);
     CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
 
     if (i == -1) {
         dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
         OPENSSL_free(pointer);
     } else
         i += 1;                 /* to avoid 0 */
     return -i;
 }
 
 void CRYPTO_destroy_dynlockid(int i)
 {
     CRYPTO_dynlock *pointer = NULL;
     if (i)
         i = -i - 1;
     if (dynlock_destroy_callback == NULL)
         return;
 
     CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
 
     if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks)) {
         CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
         return;
     }
     pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
     if (pointer != NULL) {
         --pointer->references;
 #ifdef REF_CHECK
         if (pointer->references < 0) {
             fprintf(stderr,
                     "CRYPTO_destroy_dynlockid, bad reference count\n");
             abort();
         } else
 #endif
         if (pointer->references <= 0) {
             (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
         } else
             pointer = NULL;
     }
     CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
 
     if (pointer) {
         dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
         OPENSSL_free(pointer);
     }
 }
 
 struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
 {
     CRYPTO_dynlock *pointer = NULL;
     if (i)
         i = -i - 1;
 
     CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
 
     if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
         pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
     if (pointer)
         pointer->references++;
 
     CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
 
     if (pointer)
         return pointer->data;
     return NULL;
 }
 
 struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
  (const char *file, int line) {
     return (dynlock_create_callback);
 }
 
 void (*CRYPTO_get_dynlock_lock_callback(void)) (int mode,
                                                 struct CRYPTO_dynlock_value
                                                 *l, const char *file,
                                                 int line) {
     return (dynlock_lock_callback);
 }
 
 void (*CRYPTO_get_dynlock_destroy_callback(void))
  (struct CRYPTO_dynlock_value *l, const char *file, int line) {
     return (dynlock_destroy_callback);
 }
 
 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
                                          (const char *file, int line))
 {
     dynlock_create_callback = func;
 }
 
 void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
                                                     struct
                                                     CRYPTO_dynlock_value *l,
                                                     const char *file,
                                                     int line))
 {
     dynlock_lock_callback = func;
 }
 
 void CRYPTO_set_dynlock_destroy_callback(void (*func)
                                           (struct CRYPTO_dynlock_value *l,
                                            const char *file, int line))
 {
     dynlock_destroy_callback = func;
 }
 
 void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
                                            const char *file, int line) {
     return (locking_callback);
 }
 
 int (*CRYPTO_get_add_lock_callback(void)) (int *num, int mount, int type,
                                            const char *file, int line) {
     return (add_lock_callback);
 }
 
 void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
                                                const char *file, int line))
 {
     /*
      * Calling this here ensures initialisation before any threads are
      * started.
      */
     OPENSSL_init();
     locking_callback = func;
 }
 
 void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
                                                const char *file, int line))
 {
     add_lock_callback = func;
 }
 
 /*
  * the memset() here and in set_pointer() seem overkill, but for the sake of
  * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause
  * two "equal" THREADID structs to not be memcmp()-identical.
  */
 void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
 {
     memset(id, 0, sizeof(*id));
     id->val = val;
 }
 
 static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
 
 void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
 {
     unsigned char *dest = (void *)&id->val;
     unsigned int accum = 0;
     unsigned char dnum = sizeof(id->val);
 
     memset(id, 0, sizeof(*id));
     id->ptr = ptr;
     if (sizeof(id->val) >= sizeof(id->ptr)) {
         /*
          * 'ptr' can be embedded in 'val' without loss of uniqueness
          */
         id->val = (unsigned long)id->ptr;
         return;
     }
     /*
      * hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a
      * linear function over the bytes in 'ptr', the co-efficients of which
      * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - the
      * starting prime for the sequence varies for each byte of 'val' (unique
      * polynomials unless pointers are >64-bit). For added spice, the totals
      * accumulate rather than restarting from zero, and the index of the
      * 'val' byte is added each time (position dependence). If I was a
      * black-belt, I'd scan big-endian pointers in reverse to give low-order
      * bits more play, but this isn't crypto and I'd prefer nobody mistake it
      * as such. Plus I'm lazy.
      */
     while (dnum--) {
         const unsigned char *src = (void *)&id->ptr;
         unsigned char snum = sizeof(id->ptr);
         while (snum--)
             accum += *(src++) * hash_coeffs[(snum + dnum) & 7];
         accum += dnum;
         *(dest++) = accum & 255;
     }
 }
 
 int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
 {
     if (threadid_callback)
         return 0;
     threadid_callback = func;
     return 1;
 }
 
 void (*CRYPTO_THREADID_get_callback(void)) (CRYPTO_THREADID *) {
     return threadid_callback;
 }
 
 void CRYPTO_THREADID_current(CRYPTO_THREADID *id)
 {
     if (threadid_callback) {
         threadid_callback(id);
         return;
     }
 #ifndef OPENSSL_NO_DEPRECATED
     /* If the deprecated callback was set, fall back to that */
     if (id_callback) {
         CRYPTO_THREADID_set_numeric(id, id_callback());
         return;
     }
 #endif
     /* Else pick a backup */
 #ifdef OPENSSL_SYS_WIN16
     CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentTask());
 #elif defined(OPENSSL_SYS_WIN32)
     CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId());
 #elif defined(OPENSSL_SYS_BEOS)
     CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL));
 #else
     /* For everything else, default to using the address of 'errno' */
     CRYPTO_THREADID_set_pointer(id, (void *)&errno);
 #endif
 }
 
 int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b)
 {
     return memcmp(a, b, sizeof(*a));
 }
 
 void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src)
 {
     memcpy(dest, src, sizeof(*src));
 }
 
 unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id)
 {
     return id->val;
 }
 
 #ifndef OPENSSL_NO_DEPRECATED
 unsigned long (*CRYPTO_get_id_callback(void)) (void) {
     return (id_callback);
 }
 
 void CRYPTO_set_id_callback(unsigned long (*func) (void))
 {
     id_callback = func;
 }
 
 unsigned long CRYPTO_thread_id(void)
 {
     unsigned long ret = 0;
 
     if (id_callback == NULL) {
 # ifdef OPENSSL_SYS_WIN16
         ret = (unsigned long)GetCurrentTask();
 # elif defined(OPENSSL_SYS_WIN32)
         ret = (unsigned long)GetCurrentThreadId();
 # elif defined(GETPID_IS_MEANINGLESS)
         ret = 1L;
 # elif defined(OPENSSL_SYS_BEOS)
         ret = (unsigned long)find_thread(NULL);
 # else
         ret = (unsigned long)getpid();
 # endif
     } else
         ret = id_callback();
     return (ret);
 }
 #endif
 
 void CRYPTO_lock(int mode, int type, const char *file, int line)
 {
 #ifdef LOCK_DEBUG
     {
         CRYPTO_THREADID id;
         char *rw_text, *operation_text;
 
         if (mode & CRYPTO_LOCK)
             operation_text = "lock  ";
         else if (mode & CRYPTO_UNLOCK)
             operation_text = "unlock";
         else
             operation_text = "ERROR ";
 
         if (mode & CRYPTO_READ)
             rw_text = "r";
         else if (mode & CRYPTO_WRITE)
             rw_text = "w";
         else
             rw_text = "ERROR";
 
         CRYPTO_THREADID_current(&id);
         fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\n",
                 CRYPTO_THREADID_hash(&id), rw_text, operation_text,
                 CRYPTO_get_lock_name(type), file, line);
     }
 #endif
     if (type < 0) {
         if (dynlock_lock_callback != NULL) {
             struct CRYPTO_dynlock_value *pointer
                 = CRYPTO_get_dynlock_value(type);
 
             OPENSSL_assert(pointer != NULL);
 
             dynlock_lock_callback(mode, pointer, file, line);
 
             CRYPTO_destroy_dynlockid(type);
         }
     } else if (locking_callback != NULL)
         locking_callback(mode, type, file, line);
 }
 
 int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
                     int line)
 {
     int ret = 0;
 
     if (add_lock_callback != NULL) {
 #ifdef LOCK_DEBUG
         int before = *pointer;
 #endif
 
         ret = add_lock_callback(pointer, amount, type, file, line);
 #ifdef LOCK_DEBUG
         {
             CRYPTO_THREADID id;
             CRYPTO_THREADID_current(&id);
             fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
                     CRYPTO_THREADID_hash(&id), before, amount, ret,
                     CRYPTO_get_lock_name(type), file, line);
         }
 #endif
     } else {
         CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);
 
         ret = *pointer + amount;
 #ifdef LOCK_DEBUG
         {
             CRYPTO_THREADID id;
             CRYPTO_THREADID_current(&id);
             fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
                     CRYPTO_THREADID_hash(&id),
                     *pointer, amount, ret,
                     CRYPTO_get_lock_name(type), file, line);
         }
 #endif
         *pointer = ret;
         CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);
     }
     return (ret);
 }
 
 const char *CRYPTO_get_lock_name(int type)
 {
     if (type < 0)
         return ("dynamic");
     else if (type < CRYPTO_NUM_LOCKS)
         return (lock_names[type]);
     else if (type - CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks))
         return ("ERROR");
     else
         return (sk_OPENSSL_STRING_value(app_locks, type - CRYPTO_NUM_LOCKS));
 }
 
 #if     defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
         defined(__INTEL__) || \
         defined(__x86_64) || defined(__x86_64__) || \
         defined(_M_AMD64) || defined(_M_X64)
 
 extern unsigned int OPENSSL_ia32cap_P[4];
 unsigned long *OPENSSL_ia32cap_loc(void)
 {
     if (sizeof(long) == 4)
         /*
          * If 32-bit application pulls address of OPENSSL_ia32cap_P[0]
          * clear second element to maintain the illusion that vector
          * is 32-bit.
          */
         OPENSSL_ia32cap_P[1] = 0;
 
     OPENSSL_ia32cap_P[2] = 0;
 
     return (unsigned long *)OPENSSL_ia32cap_P;
 }
 
 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
 #  define OPENSSL_CPUID_SETUP
 #  if defined(_WIN32)
 typedef unsigned __int64 IA32CAP;
 #  else
 typedef unsigned long long IA32CAP;
 #  endif
 void OPENSSL_cpuid_setup(void)
 {
     static int trigger = 0;
     IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
     IA32CAP vec;
     char *env;
 
     if (trigger)
         return;
 
     trigger = 1;
     if ((env = getenv("OPENSSL_ia32cap"))) {
         int off = (env[0] == '~') ? 1 : 0;
 #  if defined(_WIN32)
         if (!sscanf(env + off, "%I64i", &vec))
             vec = strtoul(env + off, NULL, 0);
 #  else
         if (!sscanf(env + off, "%lli", (long long *)&vec))
             vec = strtoul(env + off, NULL, 0);
 #  endif
         if (off)
             vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~vec;
         else if (env[0] == ':')
             vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
 
         OPENSSL_ia32cap_P[2] = 0;
         if ((env = strchr(env, ':'))) {
             unsigned int vecx;
             env++;
             off = (env[0] == '~') ? 1 : 0;
             vecx = strtoul(env + off, NULL, 0);
             if (off)
                 OPENSSL_ia32cap_P[2] &= ~vecx;
             else
                 OPENSSL_ia32cap_P[2] = vecx;
         }
     } else
         vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
 
     /*
      * |(1<<10) sets a reserved bit to signal that variable
      * was initialized already... This is to avoid interference
      * with cpuid snippets in ELF .init segment.
      */
     OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
     OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
 }
 # else
 unsigned int OPENSSL_ia32cap_P[4];
 # endif
 
 #else
 unsigned long *OPENSSL_ia32cap_loc(void)
 {
     return NULL;
 }
 #endif
 int OPENSSL_NONPIC_relocated = 0;
 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
 void OPENSSL_cpuid_setup(void)
 {
 }
 #endif
 
 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
 # ifdef __CYGWIN__
 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
 #  include 
 /*
  * this has side-effect of _WIN32 getting defined, which otherwise is
  * mutually exclusive with __CYGWIN__...
  */
 # endif
 
 /*
  * All we really need to do is remove the 'error' state when a thread
  * detaches
  */
 
 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 {
     switch (fdwReason) {
     case DLL_PROCESS_ATTACH:
         OPENSSL_cpuid_setup();
 # if defined(_WIN32_WINNT)
         {
             IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *) hinstDLL;
             IMAGE_NT_HEADERS *nt_headers;
 
             if (dos_header->e_magic == IMAGE_DOS_SIGNATURE) {
                 nt_headers = (IMAGE_NT_HEADERS *) ((char *)dos_header
                                                    + dos_header->e_lfanew);
                 if (nt_headers->Signature == IMAGE_NT_SIGNATURE &&
                     hinstDLL !=
                     (HINSTANCE) (nt_headers->OptionalHeader.ImageBase))
                     OPENSSL_NONPIC_relocated = 1;
             }
         }
 # endif
         break;
     case DLL_THREAD_ATTACH:
         break;
     case DLL_THREAD_DETACH:
         break;
     case DLL_PROCESS_DETACH:
         break;
     }
     return (TRUE);
 }
 #endif
 
 #if defined(_WIN32) && !defined(__CYGWIN__)
 # include 
 # include 
 # ifdef __WATCOMC__
 #  if defined(_UNICODE) || defined(__UNICODE__)
 #   define _vsntprintf _vsnwprintf
 #  else
 #   define _vsntprintf _vsnprintf
 #  endif
 # endif
 # ifdef _MSC_VER
 #  define alloca _alloca
 # endif
 
 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
 int OPENSSL_isservice(void)
 {
     HWINSTA h;
     DWORD len;
     WCHAR *name;
     static union {
         void *p;
         int (*f) (void);
     } _OPENSSL_isservice = {
         NULL
     };
 
     if (_OPENSSL_isservice.p == NULL) {
         HANDLE h = GetModuleHandle(NULL);
         if (h != NULL)
             _OPENSSL_isservice.p = GetProcAddress(h, "_OPENSSL_isservice");
         if (_OPENSSL_isservice.p == NULL)
             _OPENSSL_isservice.p = (void *)-1;
     }
 
     if (_OPENSSL_isservice.p != (void *)-1)
         return (*_OPENSSL_isservice.f) ();
 
     h = GetProcessWindowStation();
     if (h == NULL)
         return -1;
 
     if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
         GetLastError() != ERROR_INSUFFICIENT_BUFFER)
         return -1;
 
     if (len > 512)
         return -1;              /* paranoia */
     len++, len &= ~1;           /* paranoia */
     name = (WCHAR *)alloca(len + sizeof(WCHAR));
     if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
         return -1;
 
     len++, len &= ~1;           /* paranoia */
     name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
 #  if 1
     /*
      * This doesn't cover "interactive" services [working with real
      * WinSta0's] nor programs started non-interactively by Task Scheduler
      * [those are working with SAWinSta].
      */
     if (wcsstr(name, L"Service-0x"))
         return 1;
 #  else
     /* This covers all non-interactive programs such as services. */
     if (!wcsstr(name, L"WinSta0"))
         return 1;
 #  endif
     else
         return 0;
 }
 # else
 int OPENSSL_isservice(void)
 {
     return 0;
 }
 # endif
 
 void OPENSSL_showfatal(const char *fmta, ...)
 {
     va_list ap;
     TCHAR buf[256];
     const TCHAR *fmt;
 # ifdef STD_ERROR_HANDLE        /* what a dirty trick! */
     HANDLE h;
 
     if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
         GetFileType(h) != FILE_TYPE_UNKNOWN) {
         /* must be console application */
         int len;
         DWORD out;
 
         va_start(ap, fmta);
         len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
         WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
         va_end(ap);
         return;
     }
 # endif
 
     if (sizeof(TCHAR) == sizeof(char))
         fmt = (const TCHAR *)fmta;
     else
         do {
             int keepgoing;
             size_t len_0 = strlen(fmta) + 1, i;
             WCHAR *fmtw;
 
             fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
             if (fmtw == NULL) {
                 fmt = (const TCHAR *)L"no stack?";
                 break;
             }
 # ifndef OPENSSL_NO_MULTIBYTE
             if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
 # endif
                 for (i = 0; i < len_0; i++)
                     fmtw[i] = (WCHAR)fmta[i];
 
             for (i = 0; i < len_0; i++) {
                 if (fmtw[i] == L'%')
                     do {
                         keepgoing = 0;
                         switch (fmtw[i + 1]) {
                         case L'0':
                         case L'1':
                         case L'2':
                         case L'3':
                         case L'4':
                         case L'5':
                         case L'6':
                         case L'7':
                         case L'8':
                         case L'9':
                         case L'.':
                         case L'*':
                         case L'-':
                             i++;
                             keepgoing = 1;
                             break;
                         case L's':
                             fmtw[i + 1] = L'S';
                             break;
                         case L'S':
                             fmtw[i + 1] = L's';
                             break;
                         case L'c':
                             fmtw[i + 1] = L'C';
                             break;
                         case L'C':
                             fmtw[i + 1] = L'c';
                             break;
                         }
                     } while (keepgoing);
             }
             fmt = (const TCHAR *)fmtw;
         } while (0);
 
     va_start(ap, fmta);
     _vsntprintf(buf, sizeof(buf) / sizeof(TCHAR) - 1, fmt, ap);
     buf[sizeof(buf) / sizeof(TCHAR) - 1] = _T('\0');
     va_end(ap);
 
 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
     /* this -------------v--- guards NT-specific calls */
     if (check_winnt() && OPENSSL_isservice() > 0) {
         HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
 
         if (hEventLog != NULL) {
             const TCHAR *pmsg = buf;
 
             if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
                              1, 0, &pmsg, NULL)) {
 #if defined(DEBUG)
                 /*
                  * We are in a situation where we tried to report a critical
                  * error and this failed for some reason. As a last resort,
                  * in debug builds, send output to the debugger or any other
                  * tool like DebugView which can monitor the output.
                  */
                 OutputDebugString(pmsg);
 #endif
             }
 
             (void)DeregisterEventSource(hEventLog);
         }
     } else
 # endif
         MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
 }
 #else
 void OPENSSL_showfatal(const char *fmta, ...)
 {
     va_list ap;
 
     va_start(ap, fmta);
     vfprintf(stderr, fmta, ap);
     va_end(ap);
 }
 
 int OPENSSL_isservice(void)
 {
     return 0;
 }
 #endif
 
 void OpenSSLDie(const char *file, int line, const char *assertion)
 {
     OPENSSL_showfatal
         ("%s(%d): OpenSSL internal error, assertion failed: %s\n", file, line,
          assertion);
 #if !defined(_WIN32) || defined(__CYGWIN__)
     abort();
 #else
     /*
      * Win32 abort() customarily shows a dialog, but we just did that...
      */
 # if !defined(_WIN32_WCE)
     raise(SIGABRT);
 # endif
     _exit(3);
 #endif
 }
 
 void *OPENSSL_stderr(void)
 {
     return stderr;
 }
 
-int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
+int CRYPTO_memcmp(const volatile void *in_a, const volatile void *in_b, size_t len)
 {
     size_t i;
-    const unsigned char *a = in_a;
-    const unsigned char *b = in_b;
+    const volatile unsigned char *a = in_a;
+    const volatile unsigned char *b = in_b;
     unsigned char x = 0;
 
     for (i = 0; i < len; i++)
         x |= a[i] ^ b[i];
 
     return x;
 }
Index: vendor-crypto/openssl/dist/crypto/crypto.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/crypto.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/crypto.h	(revision 296273)
@@ -1,661 +1,661 @@
 /* crypto/crypto.h */
 /* ====================================================================
  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 /* ====================================================================
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  * ECDH support in OpenSSL originally developed by
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  */
 
 #ifndef HEADER_CRYPTO_H
 # define HEADER_CRYPTO_H
 
 # include 
 
 # include 
 
 # ifndef OPENSSL_NO_FP_API
 #  include 
 # endif
 
 # include 
 # include 
 # include 
 # include 
 
 # ifdef CHARSET_EBCDIC
 #  include 
 # endif
 
 /*
  * Resolve problems on some operating systems with symbol names that clash
  * one way or another
  */
 # include 
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 /* Backward compatibility to SSLeay */
 /*
  * This is more to be used to check the correct DLL is being used in the MS
  * world.
  */
 # define SSLEAY_VERSION_NUMBER   OPENSSL_VERSION_NUMBER
 # define SSLEAY_VERSION          0
 /* #define SSLEAY_OPTIONS       1 no longer supported */
 # define SSLEAY_CFLAGS           2
 # define SSLEAY_BUILT_ON         3
 # define SSLEAY_PLATFORM         4
 # define SSLEAY_DIR              5
 
 /* Already declared in ossl_typ.h */
 # if 0
 typedef struct crypto_ex_data_st CRYPTO_EX_DATA;
 /* Called when a new object is created */
 typedef int CRYPTO_EX_new (void *parent, void *ptr, CRYPTO_EX_DATA *ad,
                            int idx, long argl, void *argp);
 /* Called when an object is free()ed */
 typedef void CRYPTO_EX_free (void *parent, void *ptr, CRYPTO_EX_DATA *ad,
                              int idx, long argl, void *argp);
 /* Called when we need to dup an object */
 typedef int CRYPTO_EX_dup (CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
                            void *from_d, int idx, long argl, void *argp);
 # endif
 
 /* A generic structure to pass assorted data in a expandable way */
 typedef struct openssl_item_st {
     int code;
     void *value;                /* Not used for flag attributes */
     size_t value_size;          /* Max size of value for output, length for
                                  * input */
     size_t *value_length;       /* Returned length of value for output */
 } OPENSSL_ITEM;
 
 /*
  * When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
  * names in cryptlib.c
  */
 
 # define CRYPTO_LOCK_ERR                 1
 # define CRYPTO_LOCK_EX_DATA             2
 # define CRYPTO_LOCK_X509                3
 # define CRYPTO_LOCK_X509_INFO           4
 # define CRYPTO_LOCK_X509_PKEY           5
 # define CRYPTO_LOCK_X509_CRL            6
 # define CRYPTO_LOCK_X509_REQ            7
 # define CRYPTO_LOCK_DSA                 8
 # define CRYPTO_LOCK_RSA                 9
 # define CRYPTO_LOCK_EVP_PKEY            10
 # define CRYPTO_LOCK_X509_STORE          11
 # define CRYPTO_LOCK_SSL_CTX             12
 # define CRYPTO_LOCK_SSL_CERT            13
 # define CRYPTO_LOCK_SSL_SESSION         14
 # define CRYPTO_LOCK_SSL_SESS_CERT       15
 # define CRYPTO_LOCK_SSL                 16
 # define CRYPTO_LOCK_SSL_METHOD          17
 # define CRYPTO_LOCK_RAND                18
 # define CRYPTO_LOCK_RAND2               19
 # define CRYPTO_LOCK_MALLOC              20
 # define CRYPTO_LOCK_BIO                 21
 # define CRYPTO_LOCK_GETHOSTBYNAME       22
 # define CRYPTO_LOCK_GETSERVBYNAME       23
 # define CRYPTO_LOCK_READDIR             24
 # define CRYPTO_LOCK_RSA_BLINDING        25
 # define CRYPTO_LOCK_DH                  26
 # define CRYPTO_LOCK_MALLOC2             27
 # define CRYPTO_LOCK_DSO                 28
 # define CRYPTO_LOCK_DYNLOCK             29
 # define CRYPTO_LOCK_ENGINE              30
 # define CRYPTO_LOCK_UI                  31
 # define CRYPTO_LOCK_ECDSA               32
 # define CRYPTO_LOCK_EC                  33
 # define CRYPTO_LOCK_ECDH                34
 # define CRYPTO_LOCK_BN                  35
 # define CRYPTO_LOCK_EC_PRE_COMP         36
 # define CRYPTO_LOCK_STORE               37
 # define CRYPTO_LOCK_COMP                38
 # define CRYPTO_LOCK_FIPS                39
 # define CRYPTO_LOCK_FIPS2               40
 # define CRYPTO_NUM_LOCKS                41
 
 # define CRYPTO_LOCK             1
 # define CRYPTO_UNLOCK           2
 # define CRYPTO_READ             4
 # define CRYPTO_WRITE            8
 
 # ifndef OPENSSL_NO_LOCKING
 #  ifndef CRYPTO_w_lock
 #   define CRYPTO_w_lock(type)     \
         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
 #   define CRYPTO_w_unlock(type)   \
         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
 #   define CRYPTO_r_lock(type)     \
         CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
 #   define CRYPTO_r_unlock(type)   \
         CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
 #   define CRYPTO_add(addr,amount,type)    \
         CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
 #  endif
 # else
 #  define CRYPTO_w_lock(a)
 #  define CRYPTO_w_unlock(a)
 #  define CRYPTO_r_lock(a)
 #  define CRYPTO_r_unlock(a)
 #  define CRYPTO_add(a,b,c)       ((*(a))+=(b))
 # endif
 
 /*
  * Some applications as well as some parts of OpenSSL need to allocate and
  * deallocate locks in a dynamic fashion.  The following typedef makes this
  * possible in a type-safe manner.
  */
 /* struct CRYPTO_dynlock_value has to be defined by the application. */
 typedef struct {
     int references;
     struct CRYPTO_dynlock_value *data;
 } CRYPTO_dynlock;
 
 /*
  * The following can be used to detect memory leaks in the SSLeay library. It
  * used, it turns on malloc checking
  */
 
 # define CRYPTO_MEM_CHECK_OFF    0x0/* an enume */
 # define CRYPTO_MEM_CHECK_ON     0x1/* a bit */
 # define CRYPTO_MEM_CHECK_ENABLE 0x2/* a bit */
 # define CRYPTO_MEM_CHECK_DISABLE 0x3/* an enume */
 
 /*
  * The following are bit values to turn on or off options connected to the
  * malloc checking functionality
  */
 
 /* Adds time to the memory checking information */
 # define V_CRYPTO_MDEBUG_TIME    0x1/* a bit */
 /* Adds thread number to the memory checking information */
 # define V_CRYPTO_MDEBUG_THREAD  0x2/* a bit */
 
 # define V_CRYPTO_MDEBUG_ALL (V_CRYPTO_MDEBUG_TIME | V_CRYPTO_MDEBUG_THREAD)
 
 /* predec of the BIO type */
 typedef struct bio_st BIO_dummy;
 
 struct crypto_ex_data_st {
     STACK_OF(void) *sk;
     /* gcc is screwing up this data structure :-( */
     int dummy;
 };
 DECLARE_STACK_OF(void)
 
 /*
  * This stuff is basically class callback functions The current classes are
  * SSL_CTX, SSL, SSL_SESSION, and a few more
  */
 
 typedef struct crypto_ex_data_func_st {
     long argl;                  /* Arbitary long */
     void *argp;                 /* Arbitary void * */
     CRYPTO_EX_new *new_func;
     CRYPTO_EX_free *free_func;
     CRYPTO_EX_dup *dup_func;
 } CRYPTO_EX_DATA_FUNCS;
 
 DECLARE_STACK_OF(CRYPTO_EX_DATA_FUNCS)
 
 /*
  * Per class, we have a STACK of CRYPTO_EX_DATA_FUNCS for each CRYPTO_EX_DATA
  * entry.
  */
 
 # define CRYPTO_EX_INDEX_BIO             0
 # define CRYPTO_EX_INDEX_SSL             1
 # define CRYPTO_EX_INDEX_SSL_CTX         2
 # define CRYPTO_EX_INDEX_SSL_SESSION     3
 # define CRYPTO_EX_INDEX_X509_STORE      4
 # define CRYPTO_EX_INDEX_X509_STORE_CTX  5
 # define CRYPTO_EX_INDEX_RSA             6
 # define CRYPTO_EX_INDEX_DSA             7
 # define CRYPTO_EX_INDEX_DH              8
 # define CRYPTO_EX_INDEX_ENGINE          9
 # define CRYPTO_EX_INDEX_X509            10
 # define CRYPTO_EX_INDEX_UI              11
 # define CRYPTO_EX_INDEX_ECDSA           12
 # define CRYPTO_EX_INDEX_ECDH            13
 # define CRYPTO_EX_INDEX_COMP            14
 # define CRYPTO_EX_INDEX_STORE           15
 
 /*
  * Dynamically assigned indexes start from this value (don't use directly,
  * use via CRYPTO_ex_data_new_class).
  */
 # define CRYPTO_EX_INDEX_USER            100
 
 /*
  * This is the default callbacks, but we can have others as well: this is
  * needed in Win32 where the application malloc and the library malloc may
  * not be the same.
  */
 # define CRYPTO_malloc_init()    CRYPTO_set_mem_functions(\
         malloc, realloc, free)
 
 # if defined CRYPTO_MDEBUG_ALL || defined CRYPTO_MDEBUG_TIME || defined CRYPTO_MDEBUG_THREAD
 #  ifndef CRYPTO_MDEBUG         /* avoid duplicate #define */
 #   define CRYPTO_MDEBUG
 #  endif
 # endif
 
 /*
  * Set standard debugging functions (not done by default unless CRYPTO_MDEBUG
  * is defined)
  */
 # define CRYPTO_malloc_debug_init()      do {\
         CRYPTO_set_mem_debug_functions(\
                 CRYPTO_dbg_malloc,\
                 CRYPTO_dbg_realloc,\
                 CRYPTO_dbg_free,\
                 CRYPTO_dbg_set_options,\
                 CRYPTO_dbg_get_options);\
         } while(0)
 
 int CRYPTO_mem_ctrl(int mode);
 int CRYPTO_is_mem_check_on(void);
 
 /* for applications */
 # define MemCheck_start() CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON)
 # define MemCheck_stop() CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF)
 
 /* for library-internal use */
 # define MemCheck_on()   CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE)
 # define MemCheck_off()  CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE)
 # define is_MemCheck_on() CRYPTO_is_mem_check_on()
 
 # define OPENSSL_malloc(num)     CRYPTO_malloc((int)num,__FILE__,__LINE__)
 # define OPENSSL_strdup(str)     CRYPTO_strdup((str),__FILE__,__LINE__)
 # define OPENSSL_realloc(addr,num) \
         CRYPTO_realloc((char *)addr,(int)num,__FILE__,__LINE__)
 # define OPENSSL_realloc_clean(addr,old_num,num) \
         CRYPTO_realloc_clean(addr,old_num,num,__FILE__,__LINE__)
 # define OPENSSL_remalloc(addr,num) \
         CRYPTO_remalloc((char **)addr,(int)num,__FILE__,__LINE__)
 # define OPENSSL_freeFunc        CRYPTO_free
 # define OPENSSL_free(addr)      CRYPTO_free(addr)
 
 # define OPENSSL_malloc_locked(num) \
         CRYPTO_malloc_locked((int)num,__FILE__,__LINE__)
 # define OPENSSL_free_locked(addr) CRYPTO_free_locked(addr)
 
 const char *SSLeay_version(int type);
 unsigned long SSLeay(void);
 
 int OPENSSL_issetugid(void);
 
 /* An opaque type representing an implementation of "ex_data" support */
 typedef struct st_CRYPTO_EX_DATA_IMPL CRYPTO_EX_DATA_IMPL;
 /* Return an opaque pointer to the current "ex_data" implementation */
 const CRYPTO_EX_DATA_IMPL *CRYPTO_get_ex_data_implementation(void);
 /* Sets the "ex_data" implementation to be used (if it's not too late) */
 int CRYPTO_set_ex_data_implementation(const CRYPTO_EX_DATA_IMPL *i);
 /* Get a new "ex_data" class, and return the corresponding "class_index" */
 int CRYPTO_ex_data_new_class(void);
 /* Within a given class, get/register a new index */
 int CRYPTO_get_ex_new_index(int class_index, long argl, void *argp,
                             CRYPTO_EX_new *new_func, CRYPTO_EX_dup *dup_func,
                             CRYPTO_EX_free *free_func);
 /*
  * Initialise/duplicate/free CRYPTO_EX_DATA variables corresponding to a
  * given class (invokes whatever per-class callbacks are applicable)
  */
 int CRYPTO_new_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
 int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
                        CRYPTO_EX_DATA *from);
 void CRYPTO_free_ex_data(int class_index, void *obj, CRYPTO_EX_DATA *ad);
 /*
  * Get/set data in a CRYPTO_EX_DATA variable corresponding to a particular
  * index (relative to the class type involved)
  */
 int CRYPTO_set_ex_data(CRYPTO_EX_DATA *ad, int idx, void *val);
 void *CRYPTO_get_ex_data(const CRYPTO_EX_DATA *ad, int idx);
 /*
  * This function cleans up all "ex_data" state. It mustn't be called under
  * potential race-conditions.
  */
 void CRYPTO_cleanup_all_ex_data(void);
 
 int CRYPTO_get_new_lockid(char *name);
 
 int CRYPTO_num_locks(void);     /* return CRYPTO_NUM_LOCKS (shared libs!) */
 void CRYPTO_lock(int mode, int type, const char *file, int line);
 void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
                                                const char *file, int line));
 void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
                                            const char *file, int line);
 void CRYPTO_set_add_lock_callback(int (*func)
                                    (int *num, int mount, int type,
                                     const char *file, int line));
 int (*CRYPTO_get_add_lock_callback(void)) (int *num, int mount, int type,
                                            const char *file, int line);
 
 /* Don't use this structure directly. */
 typedef struct crypto_threadid_st {
     void *ptr;
     unsigned long val;
 } CRYPTO_THREADID;
 /* Only use CRYPTO_THREADID_set_[numeric|pointer]() within callbacks */
 void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val);
 void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr);
 int CRYPTO_THREADID_set_callback(void (*threadid_func) (CRYPTO_THREADID *));
 void (*CRYPTO_THREADID_get_callback(void)) (CRYPTO_THREADID *);
 void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
 int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b);
 void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src);
 unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id);
 # ifndef OPENSSL_NO_DEPRECATED
 void CRYPTO_set_id_callback(unsigned long (*func) (void));
 unsigned long (*CRYPTO_get_id_callback(void)) (void);
 unsigned long CRYPTO_thread_id(void);
 # endif
 
 const char *CRYPTO_get_lock_name(int type);
 int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
                     int line);
 
 int CRYPTO_get_new_dynlockid(void);
 void CRYPTO_destroy_dynlockid(int i);
 struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i);
 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value
                                         *(*dyn_create_function) (const char
                                                                  *file,
                                                                  int line));
 void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
                                        (int mode,
                                         struct CRYPTO_dynlock_value *l,
                                         const char *file, int line));
 void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
                                           (struct CRYPTO_dynlock_value *l,
                                            const char *file, int line));
 struct CRYPTO_dynlock_value
 *(*CRYPTO_get_dynlock_create_callback(void)) (const char *file, int line);
 void (*CRYPTO_get_dynlock_lock_callback(void)) (int mode,
                                                 struct CRYPTO_dynlock_value
                                                 *l, const char *file,
                                                 int line);
 void (*CRYPTO_get_dynlock_destroy_callback(void)) (struct CRYPTO_dynlock_value
                                                    *l, const char *file,
                                                    int line);
 
 /*
  * CRYPTO_set_mem_functions includes CRYPTO_set_locked_mem_functions -- call
  * the latter last if you need different functions
  */
 int CRYPTO_set_mem_functions(void *(*m) (size_t), void *(*r) (void *, size_t),
                              void (*f) (void *));
 int CRYPTO_set_locked_mem_functions(void *(*m) (size_t),
                                     void (*free_func) (void *));
 int CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int),
                                 void *(*r) (void *, size_t, const char *,
                                             int), void (*f) (void *));
 int CRYPTO_set_locked_mem_ex_functions(void *(*m) (size_t, const char *, int),
                                        void (*free_func) (void *));
 int CRYPTO_set_mem_debug_functions(void (*m)
                                     (void *, int, const char *, int, int),
                                    void (*r) (void *, void *, int,
                                               const char *, int, int),
                                    void (*f) (void *, int), void (*so) (long),
                                    long (*go) (void));
 void CRYPTO_get_mem_functions(void *(**m) (size_t),
                               void *(**r) (void *, size_t),
                               void (**f) (void *));
 void CRYPTO_get_locked_mem_functions(void *(**m) (size_t),
                                      void (**f) (void *));
 void CRYPTO_get_mem_ex_functions(void *(**m) (size_t, const char *, int),
                                  void *(**r) (void *, size_t, const char *,
                                               int), void (**f) (void *));
 void CRYPTO_get_locked_mem_ex_functions(void
                                         *(**m) (size_t, const char *, int),
                                         void (**f) (void *));
 void CRYPTO_get_mem_debug_functions(void (**m)
                                      (void *, int, const char *, int, int),
                                     void (**r) (void *, void *, int,
                                                 const char *, int, int),
                                     void (**f) (void *, int),
                                     void (**so) (long), long (**go) (void));
 
 void *CRYPTO_malloc_locked(int num, const char *file, int line);
 void CRYPTO_free_locked(void *ptr);
 void *CRYPTO_malloc(int num, const char *file, int line);
 char *CRYPTO_strdup(const char *str, const char *file, int line);
 void CRYPTO_free(void *ptr);
 void *CRYPTO_realloc(void *addr, int num, const char *file, int line);
 void *CRYPTO_realloc_clean(void *addr, int old_num, int num, const char *file,
                            int line);
 void *CRYPTO_remalloc(void *addr, int num, const char *file, int line);
 
 void OPENSSL_cleanse(void *ptr, size_t len);
 
 void CRYPTO_set_mem_debug_options(long bits);
 long CRYPTO_get_mem_debug_options(void);
 
 # define CRYPTO_push_info(info) \
         CRYPTO_push_info_(info, __FILE__, __LINE__);
 int CRYPTO_push_info_(const char *info, const char *file, int line);
 int CRYPTO_pop_info(void);
 int CRYPTO_remove_all_info(void);
 
 /*
  * Default debugging functions (enabled by CRYPTO_malloc_debug_init() macro;
  * used as default in CRYPTO_MDEBUG compilations):
  */
 /*-
  * The last argument has the following significance:
  *
  * 0:   called before the actual memory allocation has taken place
  * 1:   called after the actual memory allocation has taken place
  */
 void CRYPTO_dbg_malloc(void *addr, int num, const char *file, int line,
                        int before_p);
 void CRYPTO_dbg_realloc(void *addr1, void *addr2, int num, const char *file,
                         int line, int before_p);
 void CRYPTO_dbg_free(void *addr, int before_p);
 /*-
  * Tell the debugging code about options.  By default, the following values
  * apply:
  *
  * 0:                           Clear all options.
  * V_CRYPTO_MDEBUG_TIME (1):    Set the "Show Time" option.
  * V_CRYPTO_MDEBUG_THREAD (2):  Set the "Show Thread Number" option.
  * V_CRYPTO_MDEBUG_ALL (3):     1 + 2
  */
 void CRYPTO_dbg_set_options(long bits);
 long CRYPTO_dbg_get_options(void);
 
 # ifndef OPENSSL_NO_FP_API
 void CRYPTO_mem_leaks_fp(FILE *);
 # endif
 void CRYPTO_mem_leaks(struct bio_st *bio);
 /* unsigned long order, char *file, int line, int num_bytes, char *addr */
 typedef void *CRYPTO_MEM_LEAK_CB (unsigned long, const char *, int, int,
                                   void *);
 void CRYPTO_mem_leaks_cb(CRYPTO_MEM_LEAK_CB *cb);
 
 /* die if we have to */
 void OpenSSLDie(const char *file, int line, const char *assertion);
 # define OPENSSL_assert(e)       (void)((e) ? 0 : (OpenSSLDie(__FILE__, __LINE__, #e),1))
 
 unsigned long *OPENSSL_ia32cap_loc(void);
 # define OPENSSL_ia32cap (*(OPENSSL_ia32cap_loc()))
 int OPENSSL_isservice(void);
 
 int FIPS_mode(void);
 int FIPS_mode_set(int r);
 
 void OPENSSL_init(void);
 
 # define fips_md_init(alg) fips_md_init_ctx(alg, alg)
 
 # ifdef OPENSSL_FIPS
 #  define fips_md_init_ctx(alg, cx) \
         int alg##_Init(cx##_CTX *c) \
         { \
         if (FIPS_mode()) OpenSSLDie(__FILE__, __LINE__, \
                 "Low level API call to digest " #alg " forbidden in FIPS mode!"); \
         return private_##alg##_Init(c); \
         } \
         int private_##alg##_Init(cx##_CTX *c)
 
 #  define fips_cipher_abort(alg) \
         if (FIPS_mode()) OpenSSLDie(__FILE__, __LINE__, \
                 "Low level API call to cipher " #alg " forbidden in FIPS mode!")
 
 # else
 #  define fips_md_init_ctx(alg, cx) \
         int alg##_Init(cx##_CTX *c)
 #  define fips_cipher_abort(alg) while(0)
 # endif
 
 /*
  * CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal.
  * It takes an amount of time dependent on |len|, but independent of the
  * contents of |a| and |b|. Unlike memcmp, it cannot be used to put elements
  * into a defined order as the return value when a != b is undefined, other
  * than to be non-zero.
  */
-int CRYPTO_memcmp(const void *a, const void *b, size_t len);
+int CRYPTO_memcmp(const volatile void *a, const volatile void *b, size_t len);
 
 /* BEGIN ERROR CODES */
 /*
  * The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
  */
 void ERR_load_CRYPTO_strings(void);
 
 /* Error codes for the CRYPTO functions. */
 
 /* Function codes. */
 # define CRYPTO_F_CRYPTO_GET_EX_NEW_INDEX                 100
 # define CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID                103
 # define CRYPTO_F_CRYPTO_GET_NEW_LOCKID                   101
 # define CRYPTO_F_CRYPTO_SET_EX_DATA                      102
 # define CRYPTO_F_DEF_ADD_INDEX                           104
 # define CRYPTO_F_DEF_GET_CLASS                           105
 # define CRYPTO_F_FIPS_MODE_SET                           109
 # define CRYPTO_F_INT_DUP_EX_DATA                         106
 # define CRYPTO_F_INT_FREE_EX_DATA                        107
 # define CRYPTO_F_INT_NEW_EX_DATA                         108
 
 /* Reason codes. */
 # define CRYPTO_R_FIPS_MODE_NOT_SUPPORTED                 101
 # define CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK              100
 
 #ifdef  __cplusplus
 }
 #endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/dh/dh.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/dh/dh.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/dh/dh.h	(revision 296273)
@@ -1,393 +1,393 @@
 /* crypto/dh/dh.h */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #ifndef HEADER_DH_H
 # define HEADER_DH_H
 
 # include 
 
 # ifdef OPENSSL_NO_DH
 #  error DH is disabled.
 # endif
 
 # ifndef OPENSSL_NO_BIO
 #  include 
 # endif
 # include 
 # ifndef OPENSSL_NO_DEPRECATED
 #  include 
 # endif
 
 # ifndef OPENSSL_DH_MAX_MODULUS_BITS
 #  define OPENSSL_DH_MAX_MODULUS_BITS    10000
 # endif
 
 # define DH_FLAG_CACHE_MONT_P     0x01
 
 /*
  * new with 0.9.7h; the built-in DH
  * implementation now uses constant time
  * modular exponentiation for secret exponents
  * by default. This flag causes the
  * faster variable sliding window method to
  * be used for all exponents.
  */
 # define DH_FLAG_NO_EXP_CONSTTIME 0x02
 
 /*
  * If this flag is set the DH method is FIPS compliant and can be used in
  * FIPS mode. This is set in the validated module method. If an application
  * sets this flag in its own methods it is its reposibility to ensure the
  * result is compliant.
  */
 
 # define DH_FLAG_FIPS_METHOD                     0x0400
 
 /*
  * If this flag is set the operations normally disabled in FIPS mode are
  * permitted it is then the applications responsibility to ensure that the
  * usage is compliant.
  */
 
 # define DH_FLAG_NON_FIPS_ALLOW                  0x0400
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 /* Already defined in ossl_typ.h */
 /* typedef struct dh_st DH; */
 /* typedef struct dh_method DH_METHOD; */
 
 struct dh_method {
     const char *name;
     /* Methods here */
     int (*generate_key) (DH *dh);
     int (*compute_key) (unsigned char *key, const BIGNUM *pub_key, DH *dh);
     /* Can be null */
     int (*bn_mod_exp) (const DH *dh, BIGNUM *r, const BIGNUM *a,
                        const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
                        BN_MONT_CTX *m_ctx);
     int (*init) (DH *dh);
     int (*finish) (DH *dh);
     int flags;
     char *app_data;
     /* If this is non-NULL, it will be used to generate parameters */
     int (*generate_params) (DH *dh, int prime_len, int generator,
                             BN_GENCB *cb);
 };
 
 struct dh_st {
     /*
      * This first argument is used to pick up errors when a DH is passed
      * instead of a EVP_PKEY
      */
     int pad;
     int version;
     BIGNUM *p;
     BIGNUM *g;
     long length;                /* optional */
     BIGNUM *pub_key;            /* g^x % p */
     BIGNUM *priv_key;           /* x */
     int flags;
     BN_MONT_CTX *method_mont_p;
     /* Place holders if we want to do X9.42 DH */
     BIGNUM *q;
     BIGNUM *j;
     unsigned char *seed;
     int seedlen;
     BIGNUM *counter;
     int references;
     CRYPTO_EX_DATA ex_data;
     const DH_METHOD *meth;
     ENGINE *engine;
 };
 
 # define DH_GENERATOR_2          2
 /* #define DH_GENERATOR_3       3 */
 # define DH_GENERATOR_5          5
 
 /* DH_check error codes */
 # define DH_CHECK_P_NOT_PRIME            0x01
 # define DH_CHECK_P_NOT_SAFE_PRIME       0x02
 # define DH_UNABLE_TO_CHECK_GENERATOR    0x04
 # define DH_NOT_SUITABLE_GENERATOR       0x08
 # define DH_CHECK_Q_NOT_PRIME            0x10
 # define DH_CHECK_INVALID_Q_VALUE        0x20
 # define DH_CHECK_INVALID_J_VALUE        0x40
 
 /* DH_check_pub_key error codes */
 # define DH_CHECK_PUBKEY_TOO_SMALL       0x01
 # define DH_CHECK_PUBKEY_TOO_LARGE       0x02
-# define DH_CHECK_PUBKEY_INVALID         0x03
+# define DH_CHECK_PUBKEY_INVALID         0x04
 
 /*
  * primes p where (p-1)/2 is prime too are called "safe"; we define this for
  * backward compatibility:
  */
 # define DH_CHECK_P_NOT_STRONG_PRIME     DH_CHECK_P_NOT_SAFE_PRIME
 
 # define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
                 (char *(*)())d2i_DHparams,(fp),(unsigned char **)(x))
 # define i2d_DHparams_fp(fp,x) ASN1_i2d_fp(i2d_DHparams,(fp), \
                 (unsigned char *)(x))
 # define d2i_DHparams_bio(bp,x) ASN1_d2i_bio_of(DH,DH_new,d2i_DHparams,bp,x)
 # define i2d_DHparams_bio(bp,x) ASN1_i2d_bio_of_const(DH,i2d_DHparams,bp,x)
 
 DH *DHparams_dup(DH *);
 
 const DH_METHOD *DH_OpenSSL(void);
 
 void DH_set_default_method(const DH_METHOD *meth);
 const DH_METHOD *DH_get_default_method(void);
 int DH_set_method(DH *dh, const DH_METHOD *meth);
 DH *DH_new_method(ENGINE *engine);
 
 DH *DH_new(void);
 void DH_free(DH *dh);
 int DH_up_ref(DH *dh);
 int DH_size(const DH *dh);
 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
                         CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
 int DH_set_ex_data(DH *d, int idx, void *arg);
 void *DH_get_ex_data(DH *d, int idx);
 
 /* Deprecated version */
 # ifndef OPENSSL_NO_DEPRECATED
 DH *DH_generate_parameters(int prime_len, int generator,
                            void (*callback) (int, int, void *), void *cb_arg);
 # endif                         /* !defined(OPENSSL_NO_DEPRECATED) */
 
 /* New version */
 int DH_generate_parameters_ex(DH *dh, int prime_len, int generator,
                               BN_GENCB *cb);
 
 int DH_check(const DH *dh, int *codes);
 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *codes);
 int DH_generate_key(DH *dh);
 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);
 DH *d2i_DHparams(DH **a, const unsigned char **pp, long length);
 int i2d_DHparams(const DH *a, unsigned char **pp);
 DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length);
 int i2d_DHxparams(const DH *a, unsigned char **pp);
 # ifndef OPENSSL_NO_FP_API
 int DHparams_print_fp(FILE *fp, const DH *x);
 # endif
 # ifndef OPENSSL_NO_BIO
 int DHparams_print(BIO *bp, const DH *x);
 # else
 int DHparams_print(char *bp, const DH *x);
 # endif
 
 /* RFC 5114 parameters */
 DH *DH_get_1024_160(void);
 DH *DH_get_2048_224(void);
 DH *DH_get_2048_256(void);
 
 /* RFC2631 KDF */
 int DH_KDF_X9_42(unsigned char *out, size_t outlen,
                  const unsigned char *Z, size_t Zlen,
                  ASN1_OBJECT *key_oid,
                  const unsigned char *ukm, size_t ukmlen, const EVP_MD *md);
 
 # define EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, len, NULL)
 
 # define EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, len, NULL)
 
 # define EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, typ, NULL)
 
 # define EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, gen) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, gen, NULL)
 
 # define EVP_PKEY_CTX_set_dh_rfc5114(ctx, gen) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
 
 # define EVP_PKEY_CTX_set_dhx_rfc5114(ctx, gen) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, EVP_PKEY_OP_PARAMGEN, \
                         EVP_PKEY_CTRL_DH_RFC5114, gen, NULL)
 
 # define EVP_PKEY_CTX_set_dh_kdf_type(ctx, kdf) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_TYPE, kdf, NULL)
 
 # define EVP_PKEY_CTX_get_dh_kdf_type(ctx) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_TYPE, -2, NULL)
 
 # define EVP_PKEY_CTX_set0_dh_kdf_oid(ctx, oid) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_OID, 0, (void *)oid)
 
 # define EVP_PKEY_CTX_get0_dh_kdf_oid(ctx, poid) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_GET_DH_KDF_OID, 0, (void *)poid)
 
 # define EVP_PKEY_CTX_set_dh_kdf_md(ctx, md) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_MD, 0, (void *)md)
 
 # define EVP_PKEY_CTX_get_dh_kdf_md(ctx, pmd) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_GET_DH_KDF_MD, 0, (void *)pmd)
 
 # define EVP_PKEY_CTX_set_dh_kdf_outlen(ctx, len) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_OUTLEN, len, NULL)
 
 # define EVP_PKEY_CTX_get_dh_kdf_outlen(ctx, plen) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                         EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, 0, (void *)plen)
 
 # define EVP_PKEY_CTX_set0_dh_kdf_ukm(ctx, p, plen) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_DH_KDF_UKM, plen, (void *)p)
 
 # define EVP_PKEY_CTX_get0_dh_kdf_ukm(ctx, p) \
         EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DHX, \
                                 EVP_PKEY_OP_DERIVE, \
                                 EVP_PKEY_CTRL_GET_DH_KDF_UKM, 0, (void *)p)
 
 # define EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN     (EVP_PKEY_ALG_CTRL + 1)
 # define EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR     (EVP_PKEY_ALG_CTRL + 2)
 # define EVP_PKEY_CTRL_DH_RFC5114                (EVP_PKEY_ALG_CTRL + 3)
 # define EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN  (EVP_PKEY_ALG_CTRL + 4)
 # define EVP_PKEY_CTRL_DH_PARAMGEN_TYPE          (EVP_PKEY_ALG_CTRL + 5)
 # define EVP_PKEY_CTRL_DH_KDF_TYPE               (EVP_PKEY_ALG_CTRL + 6)
 # define EVP_PKEY_CTRL_DH_KDF_MD                 (EVP_PKEY_ALG_CTRL + 7)
 # define EVP_PKEY_CTRL_GET_DH_KDF_MD             (EVP_PKEY_ALG_CTRL + 8)
 # define EVP_PKEY_CTRL_DH_KDF_OUTLEN             (EVP_PKEY_ALG_CTRL + 9)
 # define EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN         (EVP_PKEY_ALG_CTRL + 10)
 # define EVP_PKEY_CTRL_DH_KDF_UKM                (EVP_PKEY_ALG_CTRL + 11)
 # define EVP_PKEY_CTRL_GET_DH_KDF_UKM            (EVP_PKEY_ALG_CTRL + 12)
 # define EVP_PKEY_CTRL_DH_KDF_OID                (EVP_PKEY_ALG_CTRL + 13)
 # define EVP_PKEY_CTRL_GET_DH_KDF_OID            (EVP_PKEY_ALG_CTRL + 14)
 
 /* KDF types */
 # define EVP_PKEY_DH_KDF_NONE                            1
 # define EVP_PKEY_DH_KDF_X9_42                           2
 
 /* BEGIN ERROR CODES */
 /*
  * The following lines are auto generated by the script mkerr.pl. Any changes
  * made after this point may be overwritten when the script is next run.
  */
 void ERR_load_DH_strings(void);
 
 /* Error codes for the DH functions. */
 
 /* Function codes. */
 # define DH_F_COMPUTE_KEY                                 102
 # define DH_F_DHPARAMS_PRINT_FP                           101
 # define DH_F_DH_BUILTIN_GENPARAMS                        106
 # define DH_F_DH_CMS_DECRYPT                              117
 # define DH_F_DH_CMS_SET_PEERKEY                          118
 # define DH_F_DH_CMS_SET_SHARED_INFO                      119
 # define DH_F_DH_COMPUTE_KEY                              114
 # define DH_F_DH_GENERATE_KEY                             115
 # define DH_F_DH_GENERATE_PARAMETERS_EX                   116
 # define DH_F_DH_NEW_METHOD                               105
 # define DH_F_DH_PARAM_DECODE                             107
 # define DH_F_DH_PRIV_DECODE                              110
 # define DH_F_DH_PRIV_ENCODE                              111
 # define DH_F_DH_PUB_DECODE                               108
 # define DH_F_DH_PUB_ENCODE                               109
 # define DH_F_DO_DH_PRINT                                 100
 # define DH_F_GENERATE_KEY                                103
 # define DH_F_GENERATE_PARAMETERS                         104
 # define DH_F_PKEY_DH_DERIVE                              112
 # define DH_F_PKEY_DH_KEYGEN                              113
 
 /* Reason codes. */
 # define DH_R_BAD_GENERATOR                               101
 # define DH_R_BN_DECODE_ERROR                             109
 # define DH_R_BN_ERROR                                    106
 # define DH_R_DECODE_ERROR                                104
 # define DH_R_INVALID_PUBKEY                              102
 # define DH_R_KDF_PARAMETER_ERROR                         112
 # define DH_R_KEYS_NOT_SET                                108
 # define DH_R_KEY_SIZE_TOO_SMALL                          110
 # define DH_R_MODULUS_TOO_LARGE                           103
 # define DH_R_NON_FIPS_METHOD                             111
 # define DH_R_NO_PARAMETERS_SET                           107
 # define DH_R_NO_PRIVATE_VALUE                            100
 # define DH_R_PARAMETER_ENCODING_ERROR                    105
 # define DH_R_PEER_KEY_ERROR                              113
 # define DH_R_SHARED_INFO_ERROR                           114
 
 #ifdef  __cplusplus
 }
 #endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/dh/dh_check.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/dh/dh_check.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/dh/dh_check.c	(revision 296273)
@@ -1,188 +1,187 @@
 /* crypto/dh/dh_check.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include "cryptlib.h"
 #include 
 #include 
 
 /*-
  * Check that p is a safe prime and
  * if g is 2, 3 or 5, check that it is a suitable generator
  * where
  * for 2, p mod 24 == 11
  * for 3, p mod 12 == 5
  * for 5, p mod 10 == 3 or 7
  * should hold.
  */
 
 int DH_check(const DH *dh, int *ret)
 {
     int ok = 0;
     BN_CTX *ctx = NULL;
     BN_ULONG l;
     BIGNUM *t1 = NULL, *t2 = NULL;
 
     *ret = 0;
     ctx = BN_CTX_new();
     if (ctx == NULL)
         goto err;
     BN_CTX_start(ctx);
     t1 = BN_CTX_get(ctx);
     if (t1 == NULL)
         goto err;
     t2 = BN_CTX_get(ctx);
     if (t2 == NULL)
         goto err;
 
     if (dh->q) {
         if (BN_cmp(dh->g, BN_value_one()) <= 0)
             *ret |= DH_NOT_SUITABLE_GENERATOR;
         else if (BN_cmp(dh->g, dh->p) >= 0)
             *ret |= DH_NOT_SUITABLE_GENERATOR;
         else {
             /* Check g^q == 1 mod p */
             if (!BN_mod_exp(t1, dh->g, dh->q, dh->p, ctx))
                 goto err;
             if (!BN_is_one(t1))
                 *ret |= DH_NOT_SUITABLE_GENERATOR;
         }
         if (!BN_is_prime_ex(dh->q, BN_prime_checks, ctx, NULL))
             *ret |= DH_CHECK_Q_NOT_PRIME;
         /* Check p == 1 mod q  i.e. q divides p - 1 */
         if (!BN_div(t1, t2, dh->p, dh->q, ctx))
             goto err;
         if (!BN_is_one(t2))
             *ret |= DH_CHECK_INVALID_Q_VALUE;
         if (dh->j && BN_cmp(dh->j, t1))
             *ret |= DH_CHECK_INVALID_J_VALUE;
 
     } else if (BN_is_word(dh->g, DH_GENERATOR_2)) {
         l = BN_mod_word(dh->p, 24);
         if (l != 11)
             *ret |= DH_NOT_SUITABLE_GENERATOR;
     }
 #if 0
     else if (BN_is_word(dh->g, DH_GENERATOR_3)) {
         l = BN_mod_word(dh->p, 12);
         if (l != 5)
             *ret |= DH_NOT_SUITABLE_GENERATOR;
     }
 #endif
     else if (BN_is_word(dh->g, DH_GENERATOR_5)) {
         l = BN_mod_word(dh->p, 10);
         if ((l != 3) && (l != 7))
             *ret |= DH_NOT_SUITABLE_GENERATOR;
     } else
         *ret |= DH_UNABLE_TO_CHECK_GENERATOR;
 
     if (!BN_is_prime_ex(dh->p, BN_prime_checks, ctx, NULL))
         *ret |= DH_CHECK_P_NOT_PRIME;
     else if (!dh->q) {
         if (!BN_rshift1(t1, dh->p))
             goto err;
         if (!BN_is_prime_ex(t1, BN_prime_checks, ctx, NULL))
             *ret |= DH_CHECK_P_NOT_SAFE_PRIME;
     }
     ok = 1;
  err:
     if (ctx != NULL) {
         BN_CTX_end(ctx);
         BN_CTX_free(ctx);
     }
     return (ok);
 }
 
 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret)
 {
     int ok = 0;
     BIGNUM *tmp = NULL;
     BN_CTX *ctx = NULL;
 
     *ret = 0;
     ctx = BN_CTX_new();
     if (ctx == NULL)
         goto err;
     BN_CTX_start(ctx);
     tmp = BN_CTX_get(ctx);
-    if (tmp == NULL)
+    if (tmp == NULL || !BN_set_word(tmp, 1))
         goto err;
-    BN_set_word(tmp, 1);
     if (BN_cmp(pub_key, tmp) <= 0)
         *ret |= DH_CHECK_PUBKEY_TOO_SMALL;
-    BN_copy(tmp, dh->p);
-    BN_sub_word(tmp, 1);
+    if (BN_copy(tmp, dh->p) == NULL || !BN_sub_word(tmp, 1))
+        goto err;
     if (BN_cmp(pub_key, tmp) >= 0)
         *ret |= DH_CHECK_PUBKEY_TOO_LARGE;
 
     if (dh->q != NULL) {
         /* Check pub_key^q == 1 mod p */
         if (!BN_mod_exp(tmp, pub_key, dh->q, dh->p, ctx))
             goto err;
         if (!BN_is_one(tmp))
             *ret |= DH_CHECK_PUBKEY_INVALID;
     }
 
     ok = 1;
  err:
     if (ctx != NULL) {
         BN_CTX_end(ctx);
         BN_CTX_free(ctx);
     }
     return (ok);
 }
Index: vendor-crypto/openssl/dist/crypto/dsa/dsa_ameth.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/dsa/dsa_ameth.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/dsa/dsa_ameth.c	(revision 296273)
@@ -1,678 +1,678 @@
 /*
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  * 2006.
  */
 /* ====================================================================
  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 #include 
 #include "cryptlib.h"
 #include 
 #include 
 #include 
 #include 
 #ifndef OPENSSL_NO_CMS
 # include 
 #endif
 #include "asn1_locl.h"
 
 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
 {
     const unsigned char *p, *pm;
     int pklen, pmlen;
     int ptype;
     void *pval;
     ASN1_STRING *pstr;
     X509_ALGOR *palg;
     ASN1_INTEGER *public_key = NULL;
 
     DSA *dsa = NULL;
 
     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
         return 0;
     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
 
     if (ptype == V_ASN1_SEQUENCE) {
         pstr = pval;
         pm = pstr->data;
         pmlen = pstr->length;
 
         if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) {
             DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
             goto err;
         }
 
     } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
         if (!(dsa = DSA_new())) {
             DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
             goto err;
         }
     } else {
         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
         goto err;
     }
 
     if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) {
         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
         goto err;
     }
 
     if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
         goto err;
     }
 
     ASN1_INTEGER_free(public_key);
     EVP_PKEY_assign_DSA(pkey, dsa);
     return 1;
 
  err:
     if (public_key)
         ASN1_INTEGER_free(public_key);
     if (dsa)
         DSA_free(dsa);
     return 0;
 
 }
 
 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
 {
     DSA *dsa;
     int ptype;
     unsigned char *penc = NULL;
     int penclen;
     ASN1_STRING *str = NULL;
 
     dsa = pkey->pkey.dsa;
     if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
         str = ASN1_STRING_new();
         if (!str) {
             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
             goto err;
         }
         str->length = i2d_DSAparams(dsa, &str->data);
         if (str->length <= 0) {
             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
             goto err;
         }
         ptype = V_ASN1_SEQUENCE;
     } else
         ptype = V_ASN1_UNDEF;
 
     dsa->write_params = 0;
 
     penclen = i2d_DSAPublicKey(dsa, &penc);
 
     if (penclen <= 0) {
         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
         goto err;
     }
 
     if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
                                ptype, str, penc, penclen))
         return 1;
 
  err:
     if (penc)
         OPENSSL_free(penc);
     if (str)
         ASN1_STRING_free(str);
 
     return 0;
 }
 
 /*
  * In PKCS#8 DSA: you just get a private key integer and parameters in the
  * AlgorithmIdentifier the pubkey must be recalculated.
  */
 
 static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
 {
     const unsigned char *p, *pm;
     int pklen, pmlen;
     int ptype;
     void *pval;
     ASN1_STRING *pstr;
     X509_ALGOR *palg;
     ASN1_INTEGER *privkey = NULL;
     BN_CTX *ctx = NULL;
 
     STACK_OF(ASN1_TYPE) *ndsa = NULL;
     DSA *dsa = NULL;
 
+    int ret = 0;
+
     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
         return 0;
     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
 
     /* Check for broken DSA PKCS#8, UGH! */
     if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
         ASN1_TYPE *t1, *t2;
         if (!(ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen)))
             goto decerr;
         if (sk_ASN1_TYPE_num(ndsa) != 2)
             goto decerr;
         /*-
          * Handle Two broken types:
          * SEQUENCE {parameters, priv_key}
          * SEQUENCE {pub_key, priv_key}
          */
 
         t1 = sk_ASN1_TYPE_value(ndsa, 0);
         t2 = sk_ASN1_TYPE_value(ndsa, 1);
         if (t1->type == V_ASN1_SEQUENCE) {
             p8->broken = PKCS8_EMBEDDED_PARAM;
             pval = t1->value.ptr;
         } else if (ptype == V_ASN1_SEQUENCE)
             p8->broken = PKCS8_NS_DB;
         else
             goto decerr;
 
         if (t2->type != V_ASN1_INTEGER)
             goto decerr;
 
         privkey = t2->value.integer;
     } else {
         const unsigned char *q = p;
         if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)))
             goto decerr;
         if (privkey->type == V_ASN1_NEG_INTEGER) {
             p8->broken = PKCS8_NEG_PRIVKEY;
             ASN1_STRING_clear_free(privkey);
             if (!(privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen)))
                 goto decerr;
         }
         if (ptype != V_ASN1_SEQUENCE)
             goto decerr;
     }
 
     pstr = pval;
     pm = pstr->data;
     pmlen = pstr->length;
     if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
         goto decerr;
     /* We have parameters now set private key */
     if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
         goto dsaerr;
     }
     /* Calculate public key */
     if (!(dsa->pub_key = BN_new())) {
         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
         goto dsaerr;
     }
     if (!(ctx = BN_CTX_new())) {
         DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
         goto dsaerr;
     }
 
     if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
         DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
         goto dsaerr;
     }
 
     EVP_PKEY_assign_DSA(pkey, dsa);
-    BN_CTX_free(ctx);
-    if (ndsa)
-        sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
-    else
-        ASN1_STRING_clear_free(privkey);
 
-    return 1;
+    ret = 1;
+    goto done;
 
  decerr:
-    DSAerr(DSA_F_DSA_PRIV_DECODE, EVP_R_DECODE_ERROR);
+    DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
  dsaerr:
+    DSA_free(dsa);
+ done:
     BN_CTX_free(ctx);
-    if (privkey)
+    if (ndsa)
+        sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
+    else
         ASN1_STRING_clear_free(privkey);
-    sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
-    DSA_free(dsa);
-    return 0;
+    return ret;
 }
 
 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
 {
     ASN1_STRING *params = NULL;
     ASN1_INTEGER *prkey = NULL;
     unsigned char *dp = NULL;
     int dplen;
 
     if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
         goto err;
     }
 
     params = ASN1_STRING_new();
 
     if (!params) {
         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
         goto err;
     }
 
     params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
     if (params->length <= 0) {
         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
         goto err;
     }
     params->type = V_ASN1_SEQUENCE;
 
     /* Get private key into integer */
     prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
 
     if (!prkey) {
         DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
         goto err;
     }
 
     dplen = i2d_ASN1_INTEGER(prkey, &dp);
 
     ASN1_STRING_clear_free(prkey);
     prkey = NULL;
 
     if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
                          V_ASN1_SEQUENCE, params, dp, dplen))
         goto err;
 
     return 1;
 
  err:
     if (dp != NULL)
         OPENSSL_free(dp);
     if (params != NULL)
         ASN1_STRING_free(params);
     if (prkey != NULL)
         ASN1_STRING_clear_free(prkey);
     return 0;
 }
 
 static int int_dsa_size(const EVP_PKEY *pkey)
 {
     return (DSA_size(pkey->pkey.dsa));
 }
 
 static int dsa_bits(const EVP_PKEY *pkey)
 {
     return BN_num_bits(pkey->pkey.dsa->p);
 }
 
 static int dsa_missing_parameters(const EVP_PKEY *pkey)
 {
     DSA *dsa;
     dsa = pkey->pkey.dsa;
     if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
         return 1;
     return 0;
 }
 
 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
 {
     BIGNUM *a;
 
     if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
         return 0;
     if (to->pkey.dsa->p != NULL)
         BN_free(to->pkey.dsa->p);
     to->pkey.dsa->p = a;
 
     if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
         return 0;
     if (to->pkey.dsa->q != NULL)
         BN_free(to->pkey.dsa->q);
     to->pkey.dsa->q = a;
 
     if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
         return 0;
     if (to->pkey.dsa->g != NULL)
         BN_free(to->pkey.dsa->g);
     to->pkey.dsa->g = a;
     return 1;
 }
 
 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
 {
     if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
         BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
         BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
         return 0;
     else
         return 1;
 }
 
 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
 {
     if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
         return 0;
     else
         return 1;
 }
 
 static void int_dsa_free(EVP_PKEY *pkey)
 {
     DSA_free(pkey->pkey.dsa);
 }
 
 static void update_buflen(const BIGNUM *b, size_t *pbuflen)
 {
     size_t i;
     if (!b)
         return;
     if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
         *pbuflen = i;
 }
 
 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
 {
     unsigned char *m = NULL;
     int ret = 0;
     size_t buf_len = 0;
     const char *ktype = NULL;
 
     const BIGNUM *priv_key, *pub_key;
 
     if (ptype == 2)
         priv_key = x->priv_key;
     else
         priv_key = NULL;
 
     if (ptype > 0)
         pub_key = x->pub_key;
     else
         pub_key = NULL;
 
     if (ptype == 2)
         ktype = "Private-Key";
     else if (ptype == 1)
         ktype = "Public-Key";
     else
         ktype = "DSA-Parameters";
 
     update_buflen(x->p, &buf_len);
     update_buflen(x->q, &buf_len);
     update_buflen(x->g, &buf_len);
     update_buflen(priv_key, &buf_len);
     update_buflen(pub_key, &buf_len);
 
     m = (unsigned char *)OPENSSL_malloc(buf_len + 10);
     if (m == NULL) {
         DSAerr(DSA_F_DO_DSA_PRINT, ERR_R_MALLOC_FAILURE);
         goto err;
     }
 
     if (priv_key) {
         if (!BIO_indent(bp, off, 128))
             goto err;
         if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
             <= 0)
             goto err;
     }
 
     if (!ASN1_bn_print(bp, "priv:", priv_key, m, off))
         goto err;
     if (!ASN1_bn_print(bp, "pub: ", pub_key, m, off))
         goto err;
     if (!ASN1_bn_print(bp, "P:   ", x->p, m, off))
         goto err;
     if (!ASN1_bn_print(bp, "Q:   ", x->q, m, off))
         goto err;
     if (!ASN1_bn_print(bp, "G:   ", x->g, m, off))
         goto err;
     ret = 1;
  err:
     if (m != NULL)
         OPENSSL_free(m);
     return (ret);
 }
 
 static int dsa_param_decode(EVP_PKEY *pkey,
                             const unsigned char **pder, int derlen)
 {
     DSA *dsa;
     if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) {
         DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
         return 0;
     }
     EVP_PKEY_assign_DSA(pkey, dsa);
     return 1;
 }
 
 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
 {
     return i2d_DSAparams(pkey->pkey.dsa, pder);
 }
 
 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
                            ASN1_PCTX *ctx)
 {
     return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
 }
 
 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
                          ASN1_PCTX *ctx)
 {
     return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
 }
 
 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
                           ASN1_PCTX *ctx)
 {
     return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
 }
 
 static int old_dsa_priv_decode(EVP_PKEY *pkey,
                                const unsigned char **pder, int derlen)
 {
     DSA *dsa;
     if (!(dsa = d2i_DSAPrivateKey(NULL, pder, derlen))) {
         DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
         return 0;
     }
     EVP_PKEY_assign_DSA(pkey, dsa);
     return 1;
 }
 
 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
 {
     return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
 }
 
 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
                          const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
 {
     DSA_SIG *dsa_sig;
     const unsigned char *p;
     if (!sig) {
         if (BIO_puts(bp, "\n") <= 0)
             return 0;
         else
             return 1;
     }
     p = sig->data;
     dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
     if (dsa_sig) {
         int rv = 0;
         size_t buf_len = 0;
         unsigned char *m = NULL;
         update_buflen(dsa_sig->r, &buf_len);
         update_buflen(dsa_sig->s, &buf_len);
         m = OPENSSL_malloc(buf_len + 10);
         if (m == NULL) {
             DSAerr(DSA_F_DSA_SIG_PRINT, ERR_R_MALLOC_FAILURE);
             goto err;
         }
 
         if (BIO_write(bp, "\n", 1) != 1)
             goto err;
 
         if (!ASN1_bn_print(bp, "r:   ", dsa_sig->r, m, indent))
             goto err;
         if (!ASN1_bn_print(bp, "s:   ", dsa_sig->s, m, indent))
             goto err;
         rv = 1;
  err:
         if (m)
             OPENSSL_free(m);
         DSA_SIG_free(dsa_sig);
         return rv;
     }
     return X509_signature_dump(bp, sig, indent);
 }
 
 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
 {
     switch (op) {
     case ASN1_PKEY_CTRL_PKCS7_SIGN:
         if (arg1 == 0) {
             int snid, hnid;
             X509_ALGOR *alg1, *alg2;
             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
             if (alg1 == NULL || alg1->algorithm == NULL)
                 return -1;
             hnid = OBJ_obj2nid(alg1->algorithm);
             if (hnid == NID_undef)
                 return -1;
             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
                 return -1;
             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
         }
         return 1;
 #ifndef OPENSSL_NO_CMS
     case ASN1_PKEY_CTRL_CMS_SIGN:
         if (arg1 == 0) {
             int snid, hnid;
             X509_ALGOR *alg1, *alg2;
             CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
             if (alg1 == NULL || alg1->algorithm == NULL)
                 return -1;
             hnid = OBJ_obj2nid(alg1->algorithm);
             if (hnid == NID_undef)
                 return -1;
             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
                 return -1;
             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
         }
         return 1;
 
     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
         *(int *)arg2 = CMS_RECIPINFO_NONE;
         return 1;
 #endif
 
     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
         *(int *)arg2 = NID_sha256;
         return 2;
 
     default:
         return -2;
 
     }
 
 }
 
 /* NB these are sorted in pkey_id order, lowest first */
 
 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
 
     {
      EVP_PKEY_DSA2,
      EVP_PKEY_DSA,
      ASN1_PKEY_ALIAS},
 
     {
      EVP_PKEY_DSA1,
      EVP_PKEY_DSA,
      ASN1_PKEY_ALIAS},
 
     {
      EVP_PKEY_DSA4,
      EVP_PKEY_DSA,
      ASN1_PKEY_ALIAS},
 
     {
      EVP_PKEY_DSA3,
      EVP_PKEY_DSA,
      ASN1_PKEY_ALIAS},
 
     {
      EVP_PKEY_DSA,
      EVP_PKEY_DSA,
      0,
 
      "DSA",
      "OpenSSL DSA method",
 
      dsa_pub_decode,
      dsa_pub_encode,
      dsa_pub_cmp,
      dsa_pub_print,
 
      dsa_priv_decode,
      dsa_priv_encode,
      dsa_priv_print,
 
      int_dsa_size,
      dsa_bits,
 
      dsa_param_decode,
      dsa_param_encode,
      dsa_missing_parameters,
      dsa_copy_parameters,
      dsa_cmp_parameters,
      dsa_param_print,
      dsa_sig_print,
 
      int_dsa_free,
      dsa_pkey_ctrl,
      old_dsa_priv_decode,
      old_dsa_priv_encode}
 };
Index: vendor-crypto/openssl/dist/crypto/dso/dso_lib.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/dso/dso_lib.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/dso/dso_lib.c	(revision 296273)
@@ -1,447 +1,448 @@
 /* dso_lib.c */
 /*
  * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  * 2000.
  */
 /* ====================================================================
  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 #include 
 #include 
 #include "cryptlib.h"
 #include 
 
 static DSO_METHOD *default_DSO_meth = NULL;
 
 DSO *DSO_new(void)
 {
     return (DSO_new_method(NULL));
 }
 
 void DSO_set_default_method(DSO_METHOD *meth)
 {
     default_DSO_meth = meth;
 }
 
 DSO_METHOD *DSO_get_default_method(void)
 {
     return (default_DSO_meth);
 }
 
 DSO_METHOD *DSO_get_method(DSO *dso)
 {
     return (dso->meth);
 }
 
 DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
 {
     DSO_METHOD *mtmp;
     mtmp = dso->meth;
     dso->meth = meth;
     return (mtmp);
 }
 
 DSO *DSO_new_method(DSO_METHOD *meth)
 {
     DSO *ret;
 
     if (default_DSO_meth == NULL)
         /*
          * We default to DSO_METH_openssl() which in turn defaults to
          * stealing the "best available" method. Will fallback to
          * DSO_METH_null() in the worst case.
          */
         default_DSO_meth = DSO_METHOD_openssl();
     ret = (DSO *)OPENSSL_malloc(sizeof(DSO));
     if (ret == NULL) {
         DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
         return (NULL);
     }
     memset(ret, 0, sizeof(DSO));
     ret->meth_data = sk_void_new_null();
     if (ret->meth_data == NULL) {
         /* sk_new doesn't generate any errors so we do */
         DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
         OPENSSL_free(ret);
         return (NULL);
     }
     if (meth == NULL)
         ret->meth = default_DSO_meth;
     else
         ret->meth = meth;
     ret->references = 1;
     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
+        sk_void_free(ret->meth_data);
         OPENSSL_free(ret);
         ret = NULL;
     }
     return (ret);
 }
 
 int DSO_free(DSO *dso)
 {
     int i;
 
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_FREE, ERR_R_PASSED_NULL_PARAMETER);
         return (0);
     }
 
     i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
 #ifdef REF_PRINT
     REF_PRINT("DSO", dso);
 #endif
     if (i > 0)
         return (1);
 #ifdef REF_CHECK
     if (i < 0) {
         fprintf(stderr, "DSO_free, bad reference count\n");
         abort();
     }
 #endif
 
     if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
         DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
         return (0);
     }
 
     if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
         DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
         return (0);
     }
 
     sk_void_free(dso->meth_data);
     if (dso->filename != NULL)
         OPENSSL_free(dso->filename);
     if (dso->loaded_filename != NULL)
         OPENSSL_free(dso->loaded_filename);
 
     OPENSSL_free(dso);
     return (1);
 }
 
 int DSO_flags(DSO *dso)
 {
     return ((dso == NULL) ? 0 : dso->flags);
 }
 
 int DSO_up_ref(DSO *dso)
 {
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
         return (0);
     }
 
     CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
     return (1);
 }
 
 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
 {
     DSO *ret;
     int allocated = 0;
 
     if (dso == NULL) {
         ret = DSO_new_method(meth);
         if (ret == NULL) {
             DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
             goto err;
         }
         allocated = 1;
         /* Pass the provided flags to the new DSO object */
         if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
             DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
             goto err;
         }
     } else
         ret = dso;
     /* Don't load if we're currently already loaded */
     if (ret->filename != NULL) {
         DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
         goto err;
     }
     /*
      * filename can only be NULL if we were passed a dso that already has one
      * set.
      */
     if (filename != NULL)
         if (!DSO_set_filename(ret, filename)) {
             DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
             goto err;
         }
     filename = ret->filename;
     if (filename == NULL) {
         DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
         goto err;
     }
     if (ret->meth->dso_load == NULL) {
         DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
         goto err;
     }
     if (!ret->meth->dso_load(ret)) {
         DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
         goto err;
     }
     /* Load succeeded */
     return (ret);
  err:
     if (allocated)
         DSO_free(ret);
     return (NULL);
 }
 
 void *DSO_bind_var(DSO *dso, const char *symname)
 {
     void *ret = NULL;
 
     if ((dso == NULL) || (symname == NULL)) {
         DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     if (dso->meth->dso_bind_var == NULL) {
         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
         return (NULL);
     }
     if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
         return (NULL);
     }
     /* Success */
     return (ret);
 }
 
 DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
 {
     DSO_FUNC_TYPE ret = NULL;
 
     if ((dso == NULL) || (symname == NULL)) {
         DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     if (dso->meth->dso_bind_func == NULL) {
         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
         return (NULL);
     }
     if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
         return (NULL);
     }
     /* Success */
     return (ret);
 }
 
 /*
  * I don't really like these *_ctrl functions very much to be perfectly
  * honest. For one thing, I think I have to return a negative value for any
  * error because possible DSO_ctrl() commands may return values such as
  * "size"s that can legitimately be zero (making the standard
  * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
  * times. I'd prefer "output" values to be passed by reference and the return
  * value as success/failure like usual ... but we conform when we must... :-)
  */
 long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
 {
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
         return (-1);
     }
     /*
      * We should intercept certain generic commands and only pass control to
      * the method-specific ctrl() function if it's something we don't handle.
      */
     switch (cmd) {
     case DSO_CTRL_GET_FLAGS:
         return dso->flags;
     case DSO_CTRL_SET_FLAGS:
         dso->flags = (int)larg;
         return (0);
     case DSO_CTRL_OR_FLAGS:
         dso->flags |= (int)larg;
         return (0);
     default:
         break;
     }
     if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
         DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
         return (-1);
     }
     return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
 }
 
 int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
                            DSO_NAME_CONVERTER_FUNC *oldcb)
 {
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
         return (0);
     }
     if (oldcb)
         *oldcb = dso->name_converter;
     dso->name_converter = cb;
     return (1);
 }
 
 const char *DSO_get_filename(DSO *dso)
 {
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     return (dso->filename);
 }
 
 int DSO_set_filename(DSO *dso, const char *filename)
 {
     char *copied;
 
     if ((dso == NULL) || (filename == NULL)) {
         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
         return (0);
     }
     if (dso->loaded_filename) {
         DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
         return (0);
     }
     /* We'll duplicate filename */
     copied = OPENSSL_malloc(strlen(filename) + 1);
     if (copied == NULL) {
         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
         return (0);
     }
     BUF_strlcpy(copied, filename, strlen(filename) + 1);
     if (dso->filename)
         OPENSSL_free(dso->filename);
     dso->filename = copied;
     return (1);
 }
 
 char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
 {
     char *result = NULL;
 
     if (dso == NULL || filespec1 == NULL) {
         DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
         if (dso->merger != NULL)
             result = dso->merger(dso, filespec1, filespec2);
         else if (dso->meth->dso_merger != NULL)
             result = dso->meth->dso_merger(dso, filespec1, filespec2);
     }
     return (result);
 }
 
 char *DSO_convert_filename(DSO *dso, const char *filename)
 {
     char *result = NULL;
 
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     if (filename == NULL)
         filename = dso->filename;
     if (filename == NULL) {
         DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
         return (NULL);
     }
     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
         if (dso->name_converter != NULL)
             result = dso->name_converter(dso, filename);
         else if (dso->meth->dso_name_converter != NULL)
             result = dso->meth->dso_name_converter(dso, filename);
     }
     if (result == NULL) {
         result = OPENSSL_malloc(strlen(filename) + 1);
         if (result == NULL) {
             DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
             return (NULL);
         }
         BUF_strlcpy(result, filename, strlen(filename) + 1);
     }
     return (result);
 }
 
 const char *DSO_get_loaded_filename(DSO *dso)
 {
     if (dso == NULL) {
         DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
         return (NULL);
     }
     return (dso->loaded_filename);
 }
 
 int DSO_pathbyaddr(void *addr, char *path, int sz)
 {
     DSO_METHOD *meth = default_DSO_meth;
     if (meth == NULL)
         meth = DSO_METHOD_openssl();
     if (meth->pathbyaddr == NULL) {
         DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
         return -1;
     }
     return (*meth->pathbyaddr) (addr, path, sz);
 }
 
 void *DSO_global_lookup(const char *name)
 {
     DSO_METHOD *meth = default_DSO_meth;
     if (meth == NULL)
         meth = DSO_METHOD_openssl();
     if (meth->globallookup == NULL) {
         DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
         return NULL;
     }
     return (*meth->globallookup) (name);
 }
Index: vendor-crypto/openssl/dist/crypto/ec/asm/ecp_nistz256-x86_64.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/ec/asm/ecp_nistz256-x86_64.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/ec/asm/ecp_nistz256-x86_64.pl	(revision 296273)
@@ -1,2997 +1,3006 @@
 #!/usr/bin/env perl
 
 ##############################################################################
 #                                                                            #
 # Copyright 2014 Intel Corporation                                           #
 #                                                                            #
 # Licensed under the Apache License, Version 2.0 (the "License");            #
 # you may not use this file except in compliance with the License.           #
 # You may obtain a copy of the License at                                    #
 #                                                                            #
 #    http://www.apache.org/licenses/LICENSE-2.0                              #
 #                                                                            #
 # Unless required by applicable law or agreed to in writing, software        #
 # distributed under the License is distributed on an "AS IS" BASIS,          #
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
 # See the License for the specific language governing permissions and        #
 # limitations under the License.                                             #
 #                                                                            #
 ##############################################################################
 #                                                                            #
 #  Developers and authors:                                                   #
 #  Shay Gueron (1, 2), and Vlad Krasnov (1)                                  #
 #  (1) Intel Corporation, Israel Development Center                          #
 #  (2) University of Haifa                                                   #
 #  Reference:                                                                #
 #  S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with#
 #                           256 Bit Primes"                                  #
 #                                                                            #
 ##############################################################################
 
 # Further optimization by :
 #
 #		this/original	with/without -DECP_NISTZ256_ASM(*)
 # Opteron	+12-49%		+110-150%
 # Bulldozer	+14-45%		+175-210%
 # P4		+18-46%		n/a :-(
 # Westmere	+12-34%		+80-87%
 # Sandy Bridge	+9-35%		+110-120%
 # Ivy Bridge	+9-35%		+110-125%
 # Haswell	+8-37%		+140-160%
 # Broadwell	+18-58%		+145-210%
 # Atom		+15-50%		+130-180%
 # VIA Nano	+43-160%	+300-480%
 #
 # (*)	"without -DECP_NISTZ256_ASM" refers to build with
 #	"enable-ec_nistp_64_gcc_128";
 #
 # Ranges denote minimum and maximum improvement coefficients depending
 # on benchmark. Lower coefficients are for ECDSA sign, relatively fastest
 # server-side operation. Keep in mind that +100% means 2x improvement.
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.19) + ($1>=2.22);
 	$addx = ($1>=2.23);
 }
 
 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.09) + ($1>=2.10);
 	$addx = ($1>=2.10);
 }
 
 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$avx = ($1>=10) + ($1>=11);
 	$addx = ($1>=12);
 }
 
 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
 	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
 	$avx = ($ver>=3.0) + ($ver>=3.01);
 	$addx = ($ver>=3.03);
 }
 
 $code.=<<___;
 .text
 .extern	OPENSSL_ia32cap_P
 
 # The polynomial
 .align 64
 .Lpoly:
 .quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001
 
 # 2^512 mod P precomputed for NIST P256 polynomial
 .LRR:
 .quad 0x0000000000000003, 0xfffffffbffffffff, 0xfffffffffffffffe, 0x00000004fffffffd
 
 .LOne:
 .long 1,1,1,1,1,1,1,1
 .LTwo:
 .long 2,2,2,2,2,2,2,2
 .LThree:
 .long 3,3,3,3,3,3,3,3
 .LONE_mont:
 .quad 0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0x00000000fffffffe
 ___
 
 {
 ################################################################################
 # void ecp_nistz256_mul_by_2(uint64_t res[4], uint64_t a[4]);
 
 my ($a0,$a1,$a2,$a3)=map("%r$_",(8..11));
 my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rdx","%rcx","%r12","%r13");
 my ($r_ptr,$a_ptr,$b_ptr)=("%rdi","%rsi","%rdx");
 
 $code.=<<___;
 
 .globl	ecp_nistz256_mul_by_2
 .type	ecp_nistz256_mul_by_2,\@function,2
 .align	64
 ecp_nistz256_mul_by_2:
 	push	%r12
 	push	%r13
 
 	mov	8*0($a_ptr), $a0
 	mov	8*1($a_ptr), $a1
 	add	$a0, $a0		# a0:a3+a0:a3
 	mov	8*2($a_ptr), $a2
 	adc	$a1, $a1
 	mov	8*3($a_ptr), $a3
 	lea	.Lpoly(%rip), $a_ptr
 	 mov	$a0, $t0
 	adc	$a2, $a2
 	adc	$a3, $a3
 	 mov	$a1, $t1
 	sbb	$t4, $t4
 
 	sub	8*0($a_ptr), $a0
 	 mov	$a2, $t2
 	sbb	8*1($a_ptr), $a1
 	sbb	8*2($a_ptr), $a2
 	 mov	$a3, $t3
 	sbb	8*3($a_ptr), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop	%r13
 	pop	%r12
 	ret
 .size	ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
 
 ################################################################################
 # void ecp_nistz256_div_by_2(uint64_t res[4], uint64_t a[4]);
 .globl	ecp_nistz256_div_by_2
 .type	ecp_nistz256_div_by_2,\@function,2
 .align	32
 ecp_nistz256_div_by_2:
 	push	%r12
 	push	%r13
 
 	mov	8*0($a_ptr), $a0
 	mov	8*1($a_ptr), $a1
 	mov	8*2($a_ptr), $a2
 	 mov	$a0, $t0
 	mov	8*3($a_ptr), $a3
 	lea	.Lpoly(%rip), $a_ptr
 
 	 mov	$a1, $t1
 	xor	$t4, $t4
 	add	8*0($a_ptr), $a0
 	 mov	$a2, $t2
 	adc	8*1($a_ptr), $a1
 	adc	8*2($a_ptr), $a2
 	 mov	$a3, $t3
 	adc	8*3($a_ptr), $a3
 	adc	\$0, $t4
 	xor	$a_ptr, $a_ptr		# borrow $a_ptr
 	test	\$1, $t0
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	cmovz	$t2, $a2
 	cmovz	$t3, $a3
 	cmovz	$a_ptr, $t4
 
 	mov	$a1, $t0		# a0:a3>>1
 	shr	\$1, $a0
 	shl	\$63, $t0
 	mov	$a2, $t1
 	shr	\$1, $a1
 	or	$t0, $a0
 	shl	\$63, $t1
 	mov	$a3, $t2
 	shr	\$1, $a2
 	or	$t1, $a1
 	shl	\$63, $t2
 	shr	\$1, $a3
 	shl	\$63, $t4
 	or	$t2, $a2
 	or	$t4, $a3
 
 	mov	$a0, 8*0($r_ptr)
 	mov	$a1, 8*1($r_ptr)
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop	%r13
 	pop	%r12
 	ret
 .size	ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
 
 ################################################################################
 # void ecp_nistz256_mul_by_3(uint64_t res[4], uint64_t a[4]);
 .globl	ecp_nistz256_mul_by_3
 .type	ecp_nistz256_mul_by_3,\@function,2
 .align	32
 ecp_nistz256_mul_by_3:
 	push	%r12
 	push	%r13
 
 	mov	8*0($a_ptr), $a0
 	xor	$t4, $t4
 	mov	8*1($a_ptr), $a1
 	add	$a0, $a0		# a0:a3+a0:a3
 	mov	8*2($a_ptr), $a2
 	adc	$a1, $a1
 	mov	8*3($a_ptr), $a3
 	 mov	$a0, $t0
 	adc	$a2, $a2
 	adc	$a3, $a3
 	 mov	$a1, $t1
 	adc	\$0, $t4
 
 	sub	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	.Lpoly+8*1(%rip), $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	.Lpoly+8*3(%rip), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	cmovz	$t2, $a2
 	cmovz	$t3, $a3
 
 	xor	$t4, $t4
 	add	8*0($a_ptr), $a0	# a0:a3+=a_ptr[0:3]
 	adc	8*1($a_ptr), $a1
 	 mov	$a0, $t0
 	adc	8*2($a_ptr), $a2
 	adc	8*3($a_ptr), $a3
 	 mov	$a1, $t1
 	adc	\$0, $t4
 
 	sub	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	.Lpoly+8*1(%rip), $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	.Lpoly+8*3(%rip), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop %r13
 	pop %r12
 	ret
 .size	ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
 
 ################################################################################
 # void ecp_nistz256_add(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
 .globl	ecp_nistz256_add
 .type	ecp_nistz256_add,\@function,3
 .align	32
 ecp_nistz256_add:
 	push	%r12
 	push	%r13
 
 	mov	8*0($a_ptr), $a0
 	xor	$t4, $t4
 	mov	8*1($a_ptr), $a1
 	mov	8*2($a_ptr), $a2
 	mov	8*3($a_ptr), $a3
 	lea	.Lpoly(%rip), $a_ptr
 
 	add	8*0($b_ptr), $a0
 	adc	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	adc	8*2($b_ptr), $a2
 	adc	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	adc	\$0, $t4
 
 	sub	8*0($a_ptr), $a0
 	 mov	$a2, $t2
 	sbb	8*1($a_ptr), $a1
 	sbb	8*2($a_ptr), $a2
 	 mov	$a3, $t3
 	sbb	8*3($a_ptr), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop %r13
 	pop %r12
 	ret
 .size	ecp_nistz256_add,.-ecp_nistz256_add
 
 ################################################################################
 # void ecp_nistz256_sub(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
 .globl	ecp_nistz256_sub
 .type	ecp_nistz256_sub,\@function,3
 .align	32
 ecp_nistz256_sub:
 	push	%r12
 	push	%r13
 
 	mov	8*0($a_ptr), $a0
 	xor	$t4, $t4
 	mov	8*1($a_ptr), $a1
 	mov	8*2($a_ptr), $a2
 	mov	8*3($a_ptr), $a3
 	lea	.Lpoly(%rip), $a_ptr
 
 	sub	8*0($b_ptr), $a0
 	sbb	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	sbb	8*2($b_ptr), $a2
 	sbb	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	sbb	\$0, $t4
 
 	add	8*0($a_ptr), $a0
 	 mov	$a2, $t2
 	adc	8*1($a_ptr), $a1
 	adc	8*2($a_ptr), $a2
 	 mov	$a3, $t3
 	adc	8*3($a_ptr), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop %r13
 	pop %r12
 	ret
 .size	ecp_nistz256_sub,.-ecp_nistz256_sub
 
 ################################################################################
 # void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]);
 .globl	ecp_nistz256_neg
 .type	ecp_nistz256_neg,\@function,2
 .align	32
 ecp_nistz256_neg:
 	push	%r12
 	push	%r13
 
 	xor	$a0, $a0
 	xor	$a1, $a1
 	xor	$a2, $a2
 	xor	$a3, $a3
 	xor	$t4, $t4
 
 	sub	8*0($a_ptr), $a0
 	sbb	8*1($a_ptr), $a1
 	sbb	8*2($a_ptr), $a2
 	 mov	$a0, $t0
 	sbb	8*3($a_ptr), $a3
 	lea	.Lpoly(%rip), $a_ptr
 	 mov	$a1, $t1
 	sbb	\$0, $t4
 
 	add	8*0($a_ptr), $a0
 	 mov	$a2, $t2
 	adc	8*1($a_ptr), $a1
 	adc	8*2($a_ptr), $a2
 	 mov	$a3, $t3
 	adc	8*3($a_ptr), $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	pop %r13
 	pop %r12
 	ret
 .size	ecp_nistz256_neg,.-ecp_nistz256_neg
 ___
 }
 {
 my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
 my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
 my ($t0,$t1,$t2,$t3,$t4)=("%rcx","%rbp","%rbx","%rdx","%rax");
 my ($poly1,$poly3)=($acc6,$acc7);
 
 $code.=<<___;
 ################################################################################
 # void ecp_nistz256_to_mont(
 #   uint64_t res[4],
 #   uint64_t in[4]);
 .globl	ecp_nistz256_to_mont
 .type	ecp_nistz256_to_mont,\@function,2
 .align	32
 ecp_nistz256_to_mont:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 ___
 $code.=<<___;
 	lea	.LRR(%rip), $b_org
 	jmp	.Lmul_mont
 .size	ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
 
 ################################################################################
 # void ecp_nistz256_mul_mont(
 #   uint64_t res[4],
 #   uint64_t a[4],
 #   uint64_t b[4]);
 
 .globl	ecp_nistz256_mul_mont
 .type	ecp_nistz256_mul_mont,\@function,3
 .align	32
 ecp_nistz256_mul_mont:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 ___
 $code.=<<___;
 .Lmul_mont:
 	push	%rbp
 	push	%rbx
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 ___
 $code.=<<___	if ($addx);
 	cmp	\$0x80100, %ecx
 	je	.Lmul_montx
 ___
 $code.=<<___;
 	mov	$b_org, $b_ptr
 	mov	8*0($b_org), %rax
 	mov	8*0($a_ptr), $acc1
 	mov	8*1($a_ptr), $acc2
 	mov	8*2($a_ptr), $acc3
 	mov	8*3($a_ptr), $acc4
 
 	call	__ecp_nistz256_mul_montq
 ___
 $code.=<<___	if ($addx);
 	jmp	.Lmul_mont_done
 
 .align	32
 .Lmul_montx:
 	mov	$b_org, $b_ptr
 	mov	8*0($b_org), %rdx
 	mov	8*0($a_ptr), $acc1
 	mov	8*1($a_ptr), $acc2
 	mov	8*2($a_ptr), $acc3
 	mov	8*3($a_ptr), $acc4
 	lea	-128($a_ptr), $a_ptr	# control u-op density
 
 	call	__ecp_nistz256_mul_montx
 ___
 $code.=<<___;
 .Lmul_mont_done:
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbx
 	pop	%rbp
 	ret
 .size	ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
 
 .type	__ecp_nistz256_mul_montq,\@abi-omnipotent
 .align	32
 __ecp_nistz256_mul_montq:
 	########################################################################
 	# Multiply a by b[0]
 	mov	%rax, $t1
 	mulq	$acc1
 	mov	.Lpoly+8*1(%rip),$poly1
 	mov	%rax, $acc0
 	mov	$t1, %rax
 	mov	%rdx, $acc1
 
 	mulq	$acc2
 	mov	.Lpoly+8*3(%rip),$poly3
 	add	%rax, $acc1
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $acc2
 
 	mulq	$acc3
 	add	%rax, $acc2
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $acc3
 
 	mulq	$acc4
 	add	%rax, $acc3
 	 mov	$acc0, %rax
 	adc	\$0, %rdx
 	xor	$acc5, $acc5
 	mov	%rdx, $acc4
 
 	########################################################################
 	# First reduction step
 	# Basically now we want to multiply acc[0] by p256,
 	# and add the result to the acc.
 	# Due to the special form of p256 we do some optimizations
 	#
 	# acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0]
 	# then we add acc[0] and get acc[0] x 2^96
 
 	mov	$acc0, $t1
 	shl	\$32, $acc0
 	mulq	$poly3
 	shr	\$32, $t1
 	add	$acc0, $acc1		# +=acc[0]<<96
 	adc	$t1, $acc2
 	adc	%rax, $acc3
 	 mov	8*1($b_ptr), %rax
 	adc	%rdx, $acc4
 	adc	\$0, $acc5
 	xor	$acc0, $acc0
 
 	########################################################################
 	# Multiply by b[1]
 	mov	%rax, $t1
 	mulq	8*0($a_ptr)
 	add	%rax, $acc1
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*1($a_ptr)
 	add	$t0, $acc2
 	adc	\$0, %rdx
 	add	%rax, $acc2
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*2($a_ptr)
 	add	$t0, $acc3
 	adc	\$0, %rdx
 	add	%rax, $acc3
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*3($a_ptr)
 	add	$t0, $acc4
 	adc	\$0, %rdx
 	add	%rax, $acc4
 	 mov	$acc1, %rax
 	adc	%rdx, $acc5
 	adc	\$0, $acc0
 
 	########################################################################
 	# Second reduction step	
 	mov	$acc1, $t1
 	shl	\$32, $acc1
 	mulq	$poly3
 	shr	\$32, $t1
 	add	$acc1, $acc2
 	adc	$t1, $acc3
 	adc	%rax, $acc4
 	 mov	8*2($b_ptr), %rax
 	adc	%rdx, $acc5
 	adc	\$0, $acc0
 	xor	$acc1, $acc1
 
 	########################################################################
 	# Multiply by b[2]
 	mov	%rax, $t1
 	mulq	8*0($a_ptr)
 	add	%rax, $acc2
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*1($a_ptr)
 	add	$t0, $acc3
 	adc	\$0, %rdx
 	add	%rax, $acc3
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*2($a_ptr)
 	add	$t0, $acc4
 	adc	\$0, %rdx
 	add	%rax, $acc4
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*3($a_ptr)
 	add	$t0, $acc5
 	adc	\$0, %rdx
 	add	%rax, $acc5
 	 mov	$acc2, %rax
 	adc	%rdx, $acc0
 	adc	\$0, $acc1
 
 	########################################################################
 	# Third reduction step	
 	mov	$acc2, $t1
 	shl	\$32, $acc2
 	mulq	$poly3
 	shr	\$32, $t1
 	add	$acc2, $acc3
 	adc	$t1, $acc4
 	adc	%rax, $acc5
 	 mov	8*3($b_ptr), %rax
 	adc	%rdx, $acc0
 	adc	\$0, $acc1
 	xor	$acc2, $acc2
 
 	########################################################################
 	# Multiply by b[3]
 	mov	%rax, $t1
 	mulq	8*0($a_ptr)
 	add	%rax, $acc3
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*1($a_ptr)
 	add	$t0, $acc4
 	adc	\$0, %rdx
 	add	%rax, $acc4
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*2($a_ptr)
 	add	$t0, $acc5
 	adc	\$0, %rdx
 	add	%rax, $acc5
 	mov	$t1, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	8*3($a_ptr)
 	add	$t0, $acc0
 	adc	\$0, %rdx
 	add	%rax, $acc0
 	 mov	$acc3, %rax
 	adc	%rdx, $acc1
 	adc	\$0, $acc2
 
 	########################################################################
 	# Final reduction step	
 	mov	$acc3, $t1
 	shl	\$32, $acc3
 	mulq	$poly3
 	shr	\$32, $t1
 	add	$acc3, $acc4
 	adc	$t1, $acc5
 	 mov	$acc4, $t0
 	adc	%rax, $acc0
 	adc	%rdx, $acc1
 	 mov	$acc5, $t1
 	adc	\$0, $acc2
 
 	########################################################################	
 	# Branch-less conditional subtraction of P
 	sub	\$-1, $acc4		# .Lpoly[0]
 	 mov	$acc0, $t2
 	sbb	$poly1, $acc5		# .Lpoly[1]
 	sbb	\$0, $acc0		# .Lpoly[2]
 	 mov	$acc1, $t3
 	sbb	$poly3, $acc1		# .Lpoly[3]
 	sbb	\$0, $acc2
 
 	cmovc	$t0, $acc4
 	cmovc	$t1, $acc5
 	mov	$acc4, 8*0($r_ptr)
 	cmovc	$t2, $acc0
 	mov	$acc5, 8*1($r_ptr)
 	cmovc	$t3, $acc1
 	mov	$acc0, 8*2($r_ptr)
 	mov	$acc1, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_mul_montq,.-__ecp_nistz256_mul_montq
 
 ################################################################################
 # void ecp_nistz256_sqr_mont(
 #   uint64_t res[4],
 #   uint64_t a[4]);
 
 # we optimize the square according to S.Gueron and V.Krasnov,
 # "Speeding up Big-Number Squaring"
 .globl	ecp_nistz256_sqr_mont
 .type	ecp_nistz256_sqr_mont,\@function,2
 .align	32
 ecp_nistz256_sqr_mont:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 ___
 $code.=<<___;
 	push	%rbp
 	push	%rbx
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 ___
 $code.=<<___	if ($addx);
 	cmp	\$0x80100, %ecx
 	je	.Lsqr_montx
 ___
 $code.=<<___;
 	mov	8*0($a_ptr), %rax
 	mov	8*1($a_ptr), $acc6
 	mov	8*2($a_ptr), $acc7
 	mov	8*3($a_ptr), $acc0
 
 	call	__ecp_nistz256_sqr_montq
 ___
 $code.=<<___	if ($addx);
 	jmp	.Lsqr_mont_done
 
 .align	32
 .Lsqr_montx:
 	mov	8*0($a_ptr), %rdx
 	mov	8*1($a_ptr), $acc6
 	mov	8*2($a_ptr), $acc7
 	mov	8*3($a_ptr), $acc0
 	lea	-128($a_ptr), $a_ptr	# control u-op density
 
 	call	__ecp_nistz256_sqr_montx
 ___
 $code.=<<___;
 .Lsqr_mont_done:
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbx
 	pop	%rbp
 	ret
 .size	ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
 
 .type	__ecp_nistz256_sqr_montq,\@abi-omnipotent
 .align	32
 __ecp_nistz256_sqr_montq:
 	mov	%rax, $acc5
 	mulq	$acc6			# a[1]*a[0]
 	mov	%rax, $acc1
 	mov	$acc7, %rax
 	mov	%rdx, $acc2
 
 	mulq	$acc5			# a[0]*a[2]
 	add	%rax, $acc2
 	mov	$acc0, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $acc3
 
 	mulq	$acc5			# a[0]*a[3]
 	add	%rax, $acc3
 	 mov	$acc7, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $acc4
 
 	#################################
 	mulq	$acc6			# a[1]*a[2]
 	add	%rax, $acc3
 	mov	$acc0, %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t1
 
 	mulq	$acc6			# a[1]*a[3]
 	add	%rax, $acc4
 	 mov	$acc0, %rax
 	adc	\$0, %rdx
 	add	$t1, $acc4
 	mov	%rdx, $acc5
 	adc	\$0, $acc5
 
 	#################################
 	mulq	$acc7			# a[2]*a[3]
 	xor	$acc7, $acc7
 	add	%rax, $acc5
 	 mov	8*0($a_ptr), %rax
 	mov	%rdx, $acc6
 	adc	\$0, $acc6
 
 	add	$acc1, $acc1		# acc1:6<<1
 	adc	$acc2, $acc2
 	adc	$acc3, $acc3
 	adc	$acc4, $acc4
 	adc	$acc5, $acc5
 	adc	$acc6, $acc6
 	adc	\$0, $acc7
 
 	mulq	%rax
 	mov	%rax, $acc0
 	mov	8*1($a_ptr), %rax
 	mov	%rdx, $t0
 
 	mulq	%rax
 	add	$t0, $acc1
 	adc	%rax, $acc2
 	mov	8*2($a_ptr), %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	%rax
 	add	$t0, $acc3
 	adc	%rax, $acc4
 	mov	8*3($a_ptr), %rax
 	adc	\$0, %rdx
 	mov	%rdx, $t0
 
 	mulq	%rax
 	add	$t0, $acc5
 	adc	%rax, $acc6
 	 mov	$acc0, %rax
 	adc	%rdx, $acc7
 
 	mov	.Lpoly+8*1(%rip), $a_ptr
 	mov	.Lpoly+8*3(%rip), $t1
 
 	##########################################
 	# Now the reduction
 	# First iteration
 	mov	$acc0, $t0
 	shl	\$32, $acc0
 	mulq	$t1
 	shr	\$32, $t0
 	add	$acc0, $acc1		# +=acc[0]<<96
 	adc	$t0, $acc2
 	adc	%rax, $acc3
 	 mov	$acc1, %rax
 	adc	\$0, %rdx
 
 	##########################################
 	# Second iteration
 	mov	$acc1, $t0
 	shl	\$32, $acc1
 	mov	%rdx, $acc0
 	mulq	$t1
 	shr	\$32, $t0
 	add	$acc1, $acc2
 	adc	$t0, $acc3
 	adc	%rax, $acc0
 	 mov	$acc2, %rax
 	adc	\$0, %rdx
 
 	##########################################
 	# Third iteration
 	mov	$acc2, $t0
 	shl	\$32, $acc2
 	mov	%rdx, $acc1
 	mulq	$t1
 	shr	\$32, $t0
 	add	$acc2, $acc3
 	adc	$t0, $acc0
 	adc	%rax, $acc1
 	 mov	$acc3, %rax
 	adc	\$0, %rdx
 
 	###########################################
 	# Last iteration
 	mov	$acc3, $t0
 	shl	\$32, $acc3
 	mov	%rdx, $acc2
 	mulq	$t1
 	shr	\$32, $t0
 	add	$acc3, $acc0
 	adc	$t0, $acc1
 	adc	%rax, $acc2
 	adc	\$0, %rdx
 	xor	$acc3, $acc3
 
 	############################################
 	# Add the rest of the acc
 	add	$acc0, $acc4
 	adc	$acc1, $acc5
 	 mov	$acc4, $acc0
 	adc	$acc2, $acc6
 	adc	%rdx, $acc7
 	 mov	$acc5, $acc1
 	adc	\$0, $acc3
 
 	sub	\$-1, $acc4		# .Lpoly[0]
 	 mov	$acc6, $acc2
 	sbb	$a_ptr, $acc5		# .Lpoly[1]
 	sbb	\$0, $acc6		# .Lpoly[2]
 	 mov	$acc7, $t0
 	sbb	$t1, $acc7		# .Lpoly[3]
 	sbb	\$0, $acc3
 
 	cmovc	$acc0, $acc4
 	cmovc	$acc1, $acc5
 	mov	$acc4, 8*0($r_ptr)
 	cmovc	$acc2, $acc6
 	mov	$acc5, 8*1($r_ptr)
 	cmovc	$t0, $acc7
 	mov	$acc6, 8*2($r_ptr)
 	mov	$acc7, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_sqr_montq,.-__ecp_nistz256_sqr_montq
 ___
 
 if ($addx) {
 $code.=<<___;
 .type	__ecp_nistz256_mul_montx,\@abi-omnipotent
 .align	32
 __ecp_nistz256_mul_montx:
 	########################################################################
 	# Multiply by b[0]
 	mulx	$acc1, $acc0, $acc1
 	mulx	$acc2, $t0, $acc2
 	mov	\$32, $poly1
 	xor	$acc5, $acc5		# cf=0
 	mulx	$acc3, $t1, $acc3
 	mov	.Lpoly+8*3(%rip), $poly3
 	adc	$t0, $acc1
 	mulx	$acc4, $t0, $acc4
 	 mov	$acc0, %rdx
 	adc	$t1, $acc2
 	 shlx	$poly1,$acc0,$t1
 	adc	$t0, $acc3
 	 shrx	$poly1,$acc0,$t0
 	adc	\$0, $acc4
 
 	########################################################################
 	# First reduction step
 	add	$t1, $acc1
 	adc	$t0, $acc2
 
 	mulx	$poly3, $t0, $t1
 	 mov	8*1($b_ptr), %rdx
 	adc	$t0, $acc3
 	adc	$t1, $acc4
 	adc	\$0, $acc5
 	xor	$acc0, $acc0		# $acc0=0,cf=0,of=0
 
 	########################################################################
 	# Multiply by b[1]
 	mulx	8*0+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc1
 	adox	$t1, $acc2
 
 	mulx	8*1+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc2
 	adox	$t1, $acc3
 
 	mulx	8*2+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc3
 	adox	$t1, $acc4
 
 	mulx	8*3+128($a_ptr), $t0, $t1
 	 mov	$acc1, %rdx
 	adcx	$t0, $acc4
 	 shlx	$poly1, $acc1, $t0
 	adox	$t1, $acc5
 	 shrx	$poly1, $acc1, $t1
 
 	adcx	$acc0, $acc5
 	adox	$acc0, $acc0
 	adc	\$0, $acc0
 
 	########################################################################
 	# Second reduction step
 	add	$t0, $acc2
 	adc	$t1, $acc3
 
 	mulx	$poly3, $t0, $t1
 	 mov	8*2($b_ptr), %rdx
 	adc	$t0, $acc4
 	adc	$t1, $acc5
 	adc	\$0, $acc0
 	xor	$acc1 ,$acc1		# $acc1=0,cf=0,of=0
 
 	########################################################################
 	# Multiply by b[2]
 	mulx	8*0+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc2
 	adox	$t1, $acc3
 
 	mulx	8*1+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc3
 	adox	$t1, $acc4
 
 	mulx	8*2+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc4
 	adox	$t1, $acc5
 
 	mulx	8*3+128($a_ptr), $t0, $t1
 	 mov	$acc2, %rdx
 	adcx	$t0, $acc5
 	 shlx	$poly1, $acc2, $t0
 	adox	$t1, $acc0
 	 shrx	$poly1, $acc2, $t1
 
 	adcx	$acc1, $acc0
 	adox	$acc1, $acc1
 	adc	\$0, $acc1
 
 	########################################################################
 	# Third reduction step
 	add	$t0, $acc3
 	adc	$t1, $acc4
 
 	mulx	$poly3, $t0, $t1
 	 mov	8*3($b_ptr), %rdx
 	adc	$t0, $acc5
 	adc	$t1, $acc0
 	adc	\$0, $acc1
 	xor	$acc2, $acc2		# $acc2=0,cf=0,of=0
 
 	########################################################################
 	# Multiply by b[3]
 	mulx	8*0+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc3
 	adox	$t1, $acc4
 
 	mulx	8*1+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc4
 	adox	$t1, $acc5
 
 	mulx	8*2+128($a_ptr), $t0, $t1
 	adcx	$t0, $acc5
 	adox	$t1, $acc0
 
 	mulx	8*3+128($a_ptr), $t0, $t1
 	 mov	$acc3, %rdx
 	adcx	$t0, $acc0
 	 shlx	$poly1, $acc3, $t0
 	adox	$t1, $acc1
 	 shrx	$poly1, $acc3, $t1
 
 	adcx	$acc2, $acc1
 	adox	$acc2, $acc2
 	adc	\$0, $acc2
 
 	########################################################################
 	# Fourth reduction step
 	add	$t0, $acc4
 	adc	$t1, $acc5
 
 	mulx	$poly3, $t0, $t1
 	 mov	$acc4, $t2
 	mov	.Lpoly+8*1(%rip), $poly1
 	adc	$t0, $acc0
 	 mov	$acc5, $t3
 	adc	$t1, $acc1
 	adc	\$0, $acc2
 
 	########################################################################
 	# Branch-less conditional subtraction of P
 	xor	%eax, %eax
 	 mov	$acc0, $t0
 	sbb	\$-1, $acc4		# .Lpoly[0]
 	sbb	$poly1, $acc5		# .Lpoly[1]
 	sbb	\$0, $acc0		# .Lpoly[2]
 	 mov	$acc1, $t1
 	sbb	$poly3, $acc1		# .Lpoly[3]
 	sbb	\$0, $acc2
 
 	cmovc	$t2, $acc4
 	cmovc	$t3, $acc5
 	mov	$acc4, 8*0($r_ptr)
 	cmovc	$t0, $acc0
 	mov	$acc5, 8*1($r_ptr)
 	cmovc	$t1, $acc1
 	mov	$acc0, 8*2($r_ptr)
 	mov	$acc1, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_mul_montx,.-__ecp_nistz256_mul_montx
 
 .type	__ecp_nistz256_sqr_montx,\@abi-omnipotent
 .align	32
 __ecp_nistz256_sqr_montx:
 	mulx	$acc6, $acc1, $acc2	# a[0]*a[1]
 	mulx	$acc7, $t0, $acc3	# a[0]*a[2]
 	xor	%eax, %eax
 	adc	$t0, $acc2
 	mulx	$acc0, $t1, $acc4	# a[0]*a[3]
 	 mov	$acc6, %rdx
 	adc	$t1, $acc3
 	adc	\$0, $acc4
 	xor	$acc5, $acc5		# $acc5=0,cf=0,of=0
 
 	#################################
 	mulx	$acc7, $t0, $t1		# a[1]*a[2]
 	adcx	$t0, $acc3
 	adox	$t1, $acc4
 
 	mulx	$acc0, $t0, $t1		# a[1]*a[3]
 	 mov	$acc7, %rdx
 	adcx	$t0, $acc4
 	adox	$t1, $acc5
 	adc	\$0, $acc5
 
 	#################################
 	mulx	$acc0, $t0, $acc6	# a[2]*a[3]
 	 mov	8*0+128($a_ptr), %rdx
 	xor	$acc7, $acc7		# $acc7=0,cf=0,of=0
 	 adcx	$acc1, $acc1		# acc1:6<<1
 	adox	$t0, $acc5
 	 adcx	$acc2, $acc2
 	adox	$acc7, $acc6		# of=0
 
 	mulx	%rdx, $acc0, $t1
 	mov	8*1+128($a_ptr), %rdx
 	 adcx	$acc3, $acc3
 	adox	$t1, $acc1
 	 adcx	$acc4, $acc4
 	mulx	%rdx, $t0, $t4
 	mov	8*2+128($a_ptr), %rdx
 	 adcx	$acc5, $acc5
 	adox	$t0, $acc2
 	 adcx	$acc6, $acc6
 	.byte	0x67
 	mulx	%rdx, $t0, $t1
 	mov	8*3+128($a_ptr), %rdx
 	adox	$t4, $acc3
 	 adcx	$acc7, $acc7
 	adox	$t0, $acc4
 	 mov	\$32, $a_ptr
 	adox	$t1, $acc5
 	.byte	0x67,0x67
 	mulx	%rdx, $t0, $t4
 	 mov	$acc0, %rdx
 	adox	$t0, $acc6
 	 shlx	$a_ptr, $acc0, $t0
 	adox	$t4, $acc7
 	 shrx	$a_ptr, $acc0, $t4
 	 mov	.Lpoly+8*3(%rip), $t1
 
 	# reduction step 1
 	add	$t0, $acc1
 	adc	$t4, $acc2
 
 	mulx	$t1, $t0, $acc0
 	 mov	$acc1, %rdx
 	adc	$t0, $acc3
 	 shlx	$a_ptr, $acc1, $t0
 	adc	\$0, $acc0
 	 shrx	$a_ptr, $acc1, $t4
 
 	# reduction step 2
 	add	$t0, $acc2
 	adc	$t4, $acc3
 
 	mulx	$t1, $t0, $acc1
 	 mov	$acc2, %rdx
 	adc	$t0, $acc0
 	 shlx	$a_ptr, $acc2, $t0
 	adc	\$0, $acc1
 	 shrx	$a_ptr, $acc2, $t4
 
 	# reduction step 3
 	add	$t0, $acc3
 	adc	$t4, $acc0
 
 	mulx	$t1, $t0, $acc2
 	 mov	$acc3, %rdx
 	adc	$t0, $acc1
 	 shlx	$a_ptr, $acc3, $t0
 	adc	\$0, $acc2
 	 shrx	$a_ptr, $acc3, $t4
 
 	# reduction step 4
 	add	$t0, $acc0
 	adc	$t4, $acc1
 
 	mulx	$t1, $t0, $acc3
 	adc	$t0, $acc2
 	adc	\$0, $acc3
 
 	xor	$t3, $t3		# cf=0
 	adc	$acc0, $acc4		# accumulate upper half
 	 mov	.Lpoly+8*1(%rip), $a_ptr
 	adc	$acc1, $acc5
 	 mov	$acc4, $acc0
 	adc	$acc2, $acc6
 	adc	$acc3, $acc7
 	 mov	$acc5, $acc1
 	adc	\$0, $t3
 
 	xor	%eax, %eax		# cf=0
 	sbb	\$-1, $acc4		# .Lpoly[0]
 	 mov	$acc6, $acc2
 	sbb	$a_ptr, $acc5		# .Lpoly[1]
 	sbb	\$0, $acc6		# .Lpoly[2]
 	 mov	$acc7, $acc3
 	sbb	$t1, $acc7		# .Lpoly[3]
 	sbb	\$0, $t3
 
 	cmovc	$acc0, $acc4
 	cmovc	$acc1, $acc5
 	mov	$acc4, 8*0($r_ptr)
 	cmovc	$acc2, $acc6
 	mov	$acc5, 8*1($r_ptr)
 	cmovc	$acc3, $acc7
 	mov	$acc6, 8*2($r_ptr)
 	mov	$acc7, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_sqr_montx,.-__ecp_nistz256_sqr_montx
 ___
 }
 }
 {
 my ($r_ptr,$in_ptr)=("%rdi","%rsi");
 my ($acc0,$acc1,$acc2,$acc3)=map("%r$_",(8..11));
 my ($t0,$t1,$t2)=("%rcx","%r12","%r13");
 
 $code.=<<___;
 ################################################################################
 # void ecp_nistz256_from_mont(
 #   uint64_t res[4],
 #   uint64_t in[4]);
 # This one performs Montgomery multiplication by 1, so we only need the reduction
 
 .globl	ecp_nistz256_from_mont
 .type	ecp_nistz256_from_mont,\@function,2
 .align	32
 ecp_nistz256_from_mont:
 	push	%r12
 	push	%r13
 
 	mov	8*0($in_ptr), %rax
 	mov	.Lpoly+8*3(%rip), $t2
 	mov	8*1($in_ptr), $acc1
 	mov	8*2($in_ptr), $acc2
 	mov	8*3($in_ptr), $acc3
 	mov	%rax, $acc0
 	mov	.Lpoly+8*1(%rip), $t1
 
 	#########################################
 	# First iteration
 	mov	%rax, $t0
 	shl	\$32, $acc0
 	mulq	$t2
 	shr	\$32, $t0
 	add	$acc0, $acc1
 	adc	$t0, $acc2
 	adc	%rax, $acc3
 	 mov	$acc1, %rax
 	adc	\$0, %rdx
 
 	#########################################
 	# Second iteration
 	mov	$acc1, $t0
 	shl	\$32, $acc1
 	mov	%rdx, $acc0
 	mulq	$t2
 	shr	\$32, $t0
 	add	$acc1, $acc2
 	adc	$t0, $acc3
 	adc	%rax, $acc0
 	 mov	$acc2, %rax
 	adc	\$0, %rdx
 
 	##########################################
 	# Third iteration
 	mov	$acc2, $t0
 	shl	\$32, $acc2
 	mov	%rdx, $acc1
 	mulq	$t2
 	shr	\$32, $t0
 	add	$acc2, $acc3
 	adc	$t0, $acc0
 	adc	%rax, $acc1
 	 mov	$acc3, %rax
 	adc	\$0, %rdx
 
 	###########################################
 	# Last iteration
 	mov	$acc3, $t0
 	shl	\$32, $acc3
 	mov	%rdx, $acc2
 	mulq	$t2
 	shr	\$32, $t0
 	add	$acc3, $acc0
 	adc	$t0, $acc1
 	 mov	$acc0, $t0
 	adc	%rax, $acc2
 	 mov	$acc1, $in_ptr
 	adc	\$0, %rdx
 
 	###########################################
 	# Branch-less conditional subtraction
 	sub	\$-1, $acc0
 	 mov	$acc2, %rax
 	sbb	$t1, $acc1
 	sbb	\$0, $acc2
 	 mov	%rdx, $acc3
 	sbb	$t2, %rdx
 	sbb	$t2, $t2
 
 	cmovnz	$t0, $acc0
 	cmovnz	$in_ptr, $acc1
 	mov	$acc0, 8*0($r_ptr)
 	cmovnz	%rax, $acc2
 	mov	$acc1, 8*1($r_ptr)
 	cmovz	%rdx, $acc3
 	mov	$acc2, 8*2($r_ptr)
 	mov	$acc3, 8*3($r_ptr)
 
 	pop	%r13
 	pop	%r12
 	ret
 .size	ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
 ___
 }
 {
 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
 my ($ONE,$INDEX,$Ra,$Rb,$Rc,$Rd,$Re,$Rf)=map("%xmm$_",(0..7));
 my ($M0,$T0a,$T0b,$T0c,$T0d,$T0e,$T0f,$TMP0)=map("%xmm$_",(8..15));
 my ($M1,$T2a,$T2b,$TMP2,$M2,$T2a,$T2b,$TMP2)=map("%xmm$_",(8..15));
 
 $code.=<<___;
 ################################################################################
 # void ecp_nistz256_select_w5(uint64_t *val, uint64_t *in_t, int index);
 .globl	ecp_nistz256_select_w5
 .type	ecp_nistz256_select_w5,\@abi-omnipotent
 .align	32
 ecp_nistz256_select_w5:
 ___
 $code.=<<___	if ($avx>1);
 	mov	OPENSSL_ia32cap_P+8(%rip), %eax
 	test	\$`1<<5`, %eax
 	jnz	.Lavx2_select_w5
 ___
 $code.=<<___	if ($win64);
 	lea	-0x88(%rsp), %rax
 .LSEH_begin_ecp_nistz256_select_w5:
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax), %rsp
 	.byte	0x0f,0x29,0x70,0xe0		#movaps	%xmm6, -0x20(%rax)
 	.byte	0x0f,0x29,0x78,0xf0		#movaps	%xmm7, -0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x00		#movaps	%xmm8, 0(%rax)
 	.byte	0x44,0x0f,0x29,0x48,0x10	#movaps	%xmm9, 0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x50,0x20	#movaps	%xmm10, 0x20(%rax)
 	.byte	0x44,0x0f,0x29,0x58,0x30	#movaps	%xmm11, 0x30(%rax)
 	.byte	0x44,0x0f,0x29,0x60,0x40	#movaps	%xmm12, 0x40(%rax)
 	.byte	0x44,0x0f,0x29,0x68,0x50	#movaps	%xmm13, 0x50(%rax)
 	.byte	0x44,0x0f,0x29,0x70,0x60	#movaps	%xmm14, 0x60(%rax)
 	.byte	0x44,0x0f,0x29,0x78,0x70	#movaps	%xmm15, 0x70(%rax)
 ___
 $code.=<<___;
 	movdqa	.LOne(%rip), $ONE
 	movd	$index, $INDEX
 
 	pxor	$Ra, $Ra
 	pxor	$Rb, $Rb
 	pxor	$Rc, $Rc
 	pxor	$Rd, $Rd
 	pxor	$Re, $Re
 	pxor	$Rf, $Rf
 
 	movdqa	$ONE, $M0
 	pshufd	\$0, $INDEX, $INDEX
 
 	mov	\$16, %rax
 .Lselect_loop_sse_w5:
 
 	movdqa	$M0, $TMP0
 	paddd	$ONE, $M0
 	pcmpeqd $INDEX, $TMP0
 
 	movdqa	16*0($in_t), $T0a
 	movdqa	16*1($in_t), $T0b
 	movdqa	16*2($in_t), $T0c
 	movdqa	16*3($in_t), $T0d
 	movdqa	16*4($in_t), $T0e
 	movdqa	16*5($in_t), $T0f
 	lea 16*6($in_t), $in_t
 
 	pand	$TMP0, $T0a
 	pand	$TMP0, $T0b
 	por	$T0a, $Ra
 	pand	$TMP0, $T0c
 	por	$T0b, $Rb
 	pand	$TMP0, $T0d
 	por	$T0c, $Rc
 	pand	$TMP0, $T0e
 	por	$T0d, $Rd
 	pand	$TMP0, $T0f
 	por	$T0e, $Re
 	por	$T0f, $Rf
 
 	dec	%rax
 	jnz	.Lselect_loop_sse_w5
 
 	movdqu	$Ra, 16*0($val)
 	movdqu	$Rb, 16*1($val)
 	movdqu	$Rc, 16*2($val)
 	movdqu	$Rd, 16*3($val)
 	movdqu	$Re, 16*4($val)
 	movdqu	$Rf, 16*5($val)
 ___
 $code.=<<___	if ($win64);
 	movaps	(%rsp), %xmm6
 	movaps	0x10(%rsp), %xmm7
 	movaps	0x20(%rsp), %xmm8
 	movaps	0x30(%rsp), %xmm9
 	movaps	0x40(%rsp), %xmm10
 	movaps	0x50(%rsp), %xmm11
 	movaps	0x60(%rsp), %xmm12
 	movaps	0x70(%rsp), %xmm13
 	movaps	0x80(%rsp), %xmm14
 	movaps	0x90(%rsp), %xmm15
 	lea	0xa8(%rsp), %rsp
 .LSEH_end_ecp_nistz256_select_w5:
 ___
 $code.=<<___;
 	ret
 .size	ecp_nistz256_select_w5,.-ecp_nistz256_select_w5
 
 ################################################################################
 # void ecp_nistz256_select_w7(uint64_t *val, uint64_t *in_t, int index);
 .globl	ecp_nistz256_select_w7
 .type	ecp_nistz256_select_w7,\@abi-omnipotent
 .align	32
 ecp_nistz256_select_w7:
 ___
 $code.=<<___	if ($avx>1);
 	mov	OPENSSL_ia32cap_P+8(%rip), %eax
 	test	\$`1<<5`, %eax
 	jnz	.Lavx2_select_w7
 ___
 $code.=<<___	if ($win64);
 	lea	-0x88(%rsp), %rax
 .LSEH_begin_ecp_nistz256_select_w7:
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax), %rsp
 	.byte	0x0f,0x29,0x70,0xe0		#movaps	%xmm6, -0x20(%rax)
 	.byte	0x0f,0x29,0x78,0xf0		#movaps	%xmm7, -0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x00		#movaps	%xmm8, 0(%rax)
 	.byte	0x44,0x0f,0x29,0x48,0x10	#movaps	%xmm9, 0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x50,0x20	#movaps	%xmm10, 0x20(%rax)
 	.byte	0x44,0x0f,0x29,0x58,0x30	#movaps	%xmm11, 0x30(%rax)
 	.byte	0x44,0x0f,0x29,0x60,0x40	#movaps	%xmm12, 0x40(%rax)
 	.byte	0x44,0x0f,0x29,0x68,0x50	#movaps	%xmm13, 0x50(%rax)
 	.byte	0x44,0x0f,0x29,0x70,0x60	#movaps	%xmm14, 0x60(%rax)
 	.byte	0x44,0x0f,0x29,0x78,0x70	#movaps	%xmm15, 0x70(%rax)
 ___
 $code.=<<___;
 	movdqa	.LOne(%rip), $M0
 	movd	$index, $INDEX
 
 	pxor	$Ra, $Ra
 	pxor	$Rb, $Rb
 	pxor	$Rc, $Rc
 	pxor	$Rd, $Rd
 
 	movdqa	$M0, $ONE
 	pshufd	\$0, $INDEX, $INDEX
 	mov	\$64, %rax
 
 .Lselect_loop_sse_w7:
 	movdqa	$M0, $TMP0
 	paddd	$ONE, $M0
 	movdqa	16*0($in_t), $T0a
 	movdqa	16*1($in_t), $T0b
 	pcmpeqd	$INDEX, $TMP0
 	movdqa	16*2($in_t), $T0c
 	movdqa	16*3($in_t), $T0d
 	lea	16*4($in_t), $in_t
 
 	pand	$TMP0, $T0a
 	pand	$TMP0, $T0b
 	por	$T0a, $Ra
 	pand	$TMP0, $T0c
 	por	$T0b, $Rb
 	pand	$TMP0, $T0d
 	por	$T0c, $Rc
 	prefetcht0	255($in_t)
 	por	$T0d, $Rd
 
 	dec	%rax
 	jnz	.Lselect_loop_sse_w7
 
 	movdqu	$Ra, 16*0($val)
 	movdqu	$Rb, 16*1($val)
 	movdqu	$Rc, 16*2($val)
 	movdqu	$Rd, 16*3($val)
 ___
 $code.=<<___	if ($win64);
 	movaps	(%rsp), %xmm6
 	movaps	0x10(%rsp), %xmm7
 	movaps	0x20(%rsp), %xmm8
 	movaps	0x30(%rsp), %xmm9
 	movaps	0x40(%rsp), %xmm10
 	movaps	0x50(%rsp), %xmm11
 	movaps	0x60(%rsp), %xmm12
 	movaps	0x70(%rsp), %xmm13
 	movaps	0x80(%rsp), %xmm14
 	movaps	0x90(%rsp), %xmm15
 	lea	0xa8(%rsp), %rsp
 .LSEH_end_ecp_nistz256_select_w7:
 ___
 $code.=<<___;
 	ret
 .size	ecp_nistz256_select_w7,.-ecp_nistz256_select_w7
 ___
 }
 if ($avx>1) {
 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
 my ($TWO,$INDEX,$Ra,$Rb,$Rc)=map("%ymm$_",(0..4));
 my ($M0,$T0a,$T0b,$T0c,$TMP0)=map("%ymm$_",(5..9));
 my ($M1,$T1a,$T1b,$T1c,$TMP1)=map("%ymm$_",(10..14));
 
 $code.=<<___;
 ################################################################################
 # void ecp_nistz256_avx2_select_w5(uint64_t *val, uint64_t *in_t, int index);
 .type	ecp_nistz256_avx2_select_w5,\@abi-omnipotent
 .align	32
 ecp_nistz256_avx2_select_w5:
 .Lavx2_select_w5:
 	vzeroupper
 ___
 $code.=<<___	if ($win64);
 	lea	-0x88(%rsp), %rax
 .LSEH_begin_ecp_nistz256_avx2_select_w5:
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax), %rsp
 	.byte	0xc5,0xf8,0x29,0x70,0xe0	#vmovaps %xmm6, -0x20(%rax)
 	.byte	0xc5,0xf8,0x29,0x78,0xf0	#vmovaps %xmm7, -0x10(%rax)
 	.byte	0xc5,0x78,0x29,0x40,0x00	#vmovaps %xmm8, 8(%rax)
 	.byte	0xc5,0x78,0x29,0x48,0x10	#vmovaps %xmm9, 0x10(%rax)
 	.byte	0xc5,0x78,0x29,0x50,0x20	#vmovaps %xmm10, 0x20(%rax)
 	.byte	0xc5,0x78,0x29,0x58,0x30	#vmovaps %xmm11, 0x30(%rax)
 	.byte	0xc5,0x78,0x29,0x60,0x40	#vmovaps %xmm12, 0x40(%rax)
 	.byte	0xc5,0x78,0x29,0x68,0x50	#vmovaps %xmm13, 0x50(%rax)
 	.byte	0xc5,0x78,0x29,0x70,0x60	#vmovaps %xmm14, 0x60(%rax)
 	.byte	0xc5,0x78,0x29,0x78,0x70	#vmovaps %xmm15, 0x70(%rax)
 ___
 $code.=<<___;
 	vmovdqa	.LTwo(%rip), $TWO
 
 	vpxor	$Ra, $Ra, $Ra
 	vpxor	$Rb, $Rb, $Rb
 	vpxor	$Rc, $Rc, $Rc
 
 	vmovdqa .LOne(%rip), $M0
 	vmovdqa .LTwo(%rip), $M1
 
 	vmovd	$index, %xmm1
 	vpermd	$INDEX, $Ra, $INDEX
 
 	mov	\$8, %rax
 .Lselect_loop_avx2_w5:
 
 	vmovdqa	32*0($in_t), $T0a
 	vmovdqa	32*1($in_t), $T0b
 	vmovdqa	32*2($in_t), $T0c
 
 	vmovdqa	32*3($in_t), $T1a
 	vmovdqa	32*4($in_t), $T1b
 	vmovdqa	32*5($in_t), $T1c
 
 	vpcmpeqd	$INDEX, $M0, $TMP0
 	vpcmpeqd	$INDEX, $M1, $TMP1
 
 	vpaddd	$TWO, $M0, $M0
 	vpaddd	$TWO, $M1, $M1
 	lea	32*6($in_t), $in_t
 
 	vpand	$TMP0, $T0a, $T0a
 	vpand	$TMP0, $T0b, $T0b
 	vpand	$TMP0, $T0c, $T0c
 	vpand	$TMP1, $T1a, $T1a
 	vpand	$TMP1, $T1b, $T1b
 	vpand	$TMP1, $T1c, $T1c
 
 	vpxor	$T0a, $Ra, $Ra
 	vpxor	$T0b, $Rb, $Rb
 	vpxor	$T0c, $Rc, $Rc
 	vpxor	$T1a, $Ra, $Ra
 	vpxor	$T1b, $Rb, $Rb
 	vpxor	$T1c, $Rc, $Rc
 
 	dec %rax
 	jnz .Lselect_loop_avx2_w5
 
 	vmovdqu $Ra, 32*0($val)
 	vmovdqu $Rb, 32*1($val)
 	vmovdqu $Rc, 32*2($val)
 	vzeroupper
 ___
 $code.=<<___	if ($win64);
 	movaps	(%rsp), %xmm6
 	movaps	0x10(%rsp), %xmm7
 	movaps	0x20(%rsp), %xmm8
 	movaps	0x30(%rsp), %xmm9
 	movaps	0x40(%rsp), %xmm10
 	movaps	0x50(%rsp), %xmm11
 	movaps	0x60(%rsp), %xmm12
 	movaps	0x70(%rsp), %xmm13
 	movaps	0x80(%rsp), %xmm14
 	movaps	0x90(%rsp), %xmm15
 	lea	0xa8(%rsp), %rsp
 .LSEH_end_ecp_nistz256_avx2_select_w5:
 ___
 $code.=<<___;
 	ret
 .size	ecp_nistz256_avx2_select_w5,.-ecp_nistz256_avx2_select_w5
 ___
 }
 if ($avx>1) {
 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
 my ($THREE,$INDEX,$Ra,$Rb)=map("%ymm$_",(0..3));
 my ($M0,$T0a,$T0b,$TMP0)=map("%ymm$_",(4..7));
 my ($M1,$T1a,$T1b,$TMP1)=map("%ymm$_",(8..11));
 my ($M2,$T2a,$T2b,$TMP2)=map("%ymm$_",(12..15));
 
 $code.=<<___;
 
 ################################################################################
 # void ecp_nistz256_avx2_select_w7(uint64_t *val, uint64_t *in_t, int index);
 .globl	ecp_nistz256_avx2_select_w7
 .type	ecp_nistz256_avx2_select_w7,\@abi-omnipotent
 .align	32
 ecp_nistz256_avx2_select_w7:
 .Lavx2_select_w7:
 	vzeroupper
 ___
 $code.=<<___	if ($win64);
 	lea	-0x88(%rsp), %rax
 .LSEH_begin_ecp_nistz256_avx2_select_w7:
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax), %rsp
 	.byte	0xc5,0xf8,0x29,0x70,0xe0	#vmovaps %xmm6, -0x20(%rax)
 	.byte	0xc5,0xf8,0x29,0x78,0xf0	#vmovaps %xmm7, -0x10(%rax)
 	.byte	0xc5,0x78,0x29,0x40,0x00	#vmovaps %xmm8, 8(%rax)
 	.byte	0xc5,0x78,0x29,0x48,0x10	#vmovaps %xmm9, 0x10(%rax)
 	.byte	0xc5,0x78,0x29,0x50,0x20	#vmovaps %xmm10, 0x20(%rax)
 	.byte	0xc5,0x78,0x29,0x58,0x30	#vmovaps %xmm11, 0x30(%rax)
 	.byte	0xc5,0x78,0x29,0x60,0x40	#vmovaps %xmm12, 0x40(%rax)
 	.byte	0xc5,0x78,0x29,0x68,0x50	#vmovaps %xmm13, 0x50(%rax)
 	.byte	0xc5,0x78,0x29,0x70,0x60	#vmovaps %xmm14, 0x60(%rax)
 	.byte	0xc5,0x78,0x29,0x78,0x70	#vmovaps %xmm15, 0x70(%rax)
 ___
 $code.=<<___;
 	vmovdqa	.LThree(%rip), $THREE
 
 	vpxor	$Ra, $Ra, $Ra
 	vpxor	$Rb, $Rb, $Rb
 
 	vmovdqa .LOne(%rip), $M0
 	vmovdqa .LTwo(%rip), $M1
 	vmovdqa .LThree(%rip), $M2
 
 	vmovd	$index, %xmm1
 	vpermd	$INDEX, $Ra, $INDEX
 	# Skip index = 0, because it is implicitly the point at infinity
 
 	mov	\$21, %rax
 .Lselect_loop_avx2_w7:
 
 	vmovdqa	32*0($in_t), $T0a
 	vmovdqa	32*1($in_t), $T0b
 
 	vmovdqa	32*2($in_t), $T1a
 	vmovdqa	32*3($in_t), $T1b
 
 	vmovdqa	32*4($in_t), $T2a
 	vmovdqa	32*5($in_t), $T2b
 
 	vpcmpeqd	$INDEX, $M0, $TMP0
 	vpcmpeqd	$INDEX, $M1, $TMP1
 	vpcmpeqd	$INDEX, $M2, $TMP2
 
 	vpaddd	$THREE, $M0, $M0
 	vpaddd	$THREE, $M1, $M1
 	vpaddd	$THREE, $M2, $M2
 	lea	32*6($in_t), $in_t
 
 	vpand	$TMP0, $T0a, $T0a
 	vpand	$TMP0, $T0b, $T0b
 	vpand	$TMP1, $T1a, $T1a
 	vpand	$TMP1, $T1b, $T1b
 	vpand	$TMP2, $T2a, $T2a
 	vpand	$TMP2, $T2b, $T2b
 
 	vpxor	$T0a, $Ra, $Ra
 	vpxor	$T0b, $Rb, $Rb
 	vpxor	$T1a, $Ra, $Ra
 	vpxor	$T1b, $Rb, $Rb
 	vpxor	$T2a, $Ra, $Ra
 	vpxor	$T2b, $Rb, $Rb
 
 	dec %rax
 	jnz .Lselect_loop_avx2_w7
 
 
 	vmovdqa	32*0($in_t), $T0a
 	vmovdqa	32*1($in_t), $T0b
 
 	vpcmpeqd	$INDEX, $M0, $TMP0
 
 	vpand	$TMP0, $T0a, $T0a
 	vpand	$TMP0, $T0b, $T0b
 
 	vpxor	$T0a, $Ra, $Ra
 	vpxor	$T0b, $Rb, $Rb
 
 	vmovdqu $Ra, 32*0($val)
 	vmovdqu $Rb, 32*1($val)
 	vzeroupper
 ___
 $code.=<<___	if ($win64);
 	movaps	(%rsp), %xmm6
 	movaps	0x10(%rsp), %xmm7
 	movaps	0x20(%rsp), %xmm8
 	movaps	0x30(%rsp), %xmm9
 	movaps	0x40(%rsp), %xmm10
 	movaps	0x50(%rsp), %xmm11
 	movaps	0x60(%rsp), %xmm12
 	movaps	0x70(%rsp), %xmm13
 	movaps	0x80(%rsp), %xmm14
 	movaps	0x90(%rsp), %xmm15
 	lea	0xa8(%rsp), %rsp
 .LSEH_end_ecp_nistz256_avx2_select_w7:
 ___
 $code.=<<___;
 	ret
 .size	ecp_nistz256_avx2_select_w7,.-ecp_nistz256_avx2_select_w7
 ___
 } else {
 $code.=<<___;
 .globl	ecp_nistz256_avx2_select_w7
 .type	ecp_nistz256_avx2_select_w7,\@function,3
 .align	32
 ecp_nistz256_avx2_select_w7:
 	.byte	0x0f,0x0b	# ud2
 	ret
 .size	ecp_nistz256_avx2_select_w7,.-ecp_nistz256_avx2_select_w7
 ___
 }
 {{{
 ########################################################################
 # This block implements higher level point_double, point_add and
 # point_add_affine. The key to performance in this case is to allow
 # out-of-order execution logic to overlap computations from next step
 # with tail processing from current step. By using tailored calling
 # sequence we minimize inter-step overhead to give processor better
 # shot at overlapping operations...
 #
 # You will notice that input data is copied to stack. Trouble is that
 # there are no registers to spare for holding original pointers and
 # reloading them, pointers, would create undesired dependencies on
 # effective addresses calculation paths. In other words it's too done
 # to favour out-of-order execution logic.
 #						
 
 my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
 my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
 my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rbp","%rcx",$acc4,$acc4);
 my ($poly1,$poly3)=($acc6,$acc7);
 
 sub load_for_mul () {
 my ($a,$b,$src0) = @_;
 my $bias = $src0 eq "%rax" ? 0 : -128;
 
 "	mov	$b, $src0
 	lea	$b, $b_ptr
 	mov	8*0+$a, $acc1
 	mov	8*1+$a, $acc2
 	lea	$bias+$a, $a_ptr
 	mov	8*2+$a, $acc3
 	mov	8*3+$a, $acc4"
 }
 
 sub load_for_sqr () {
 my ($a,$src0) = @_;
 my $bias = $src0 eq "%rax" ? 0 : -128;
 
 "	mov	8*0+$a, $src0
 	mov	8*1+$a, $acc6
 	lea	$bias+$a, $a_ptr
 	mov	8*2+$a, $acc7
 	mov	8*3+$a, $acc0"
 }
 
 									{
 ########################################################################
 # operate in 4-5-0-1 "name space" that matches multiplication output
 #
 my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
 
 $code.=<<___;
 .type	__ecp_nistz256_add_toq,\@abi-omnipotent
 .align	32
 __ecp_nistz256_add_toq:
 	add	8*0($b_ptr), $a0
 	adc	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	adc	8*2($b_ptr), $a2
 	adc	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	sbb	$t4, $t4
 
 	sub	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	$poly1, $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	$poly3, $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_add_toq,.-__ecp_nistz256_add_toq
 
 .type	__ecp_nistz256_sub_fromq,\@abi-omnipotent
 .align	32
 __ecp_nistz256_sub_fromq:
 	sub	8*0($b_ptr), $a0
 	sbb	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	sbb	8*2($b_ptr), $a2
 	sbb	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	sbb	$t4, $t4
 
 	add	\$-1, $a0
 	 mov	$a2, $t2
 	adc	$poly1, $a1
 	adc	\$0, $a2
 	 mov	$a3, $t3
 	adc	$poly3, $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_sub_fromq,.-__ecp_nistz256_sub_fromq
 
 .type	__ecp_nistz256_subq,\@abi-omnipotent
 .align	32
 __ecp_nistz256_subq:
 	sub	$a0, $t0
 	sbb	$a1, $t1
 	 mov	$t0, $a0
 	sbb	$a2, $t2
 	sbb	$a3, $t3
 	 mov	$t1, $a1
 	sbb	$t4, $t4
 
 	add	\$-1, $t0
 	 mov	$t2, $a2
 	adc	$poly1, $t1
 	adc	\$0, $t2
 	 mov	$t3, $a3
 	adc	$poly3, $t3
 	test	$t4, $t4
 
 	cmovnz	$t0, $a0
 	cmovnz	$t1, $a1
 	cmovnz	$t2, $a2
 	cmovnz	$t3, $a3
 
 	ret
 .size	__ecp_nistz256_subq,.-__ecp_nistz256_subq
 
 .type	__ecp_nistz256_mul_by_2q,\@abi-omnipotent
 .align	32
 __ecp_nistz256_mul_by_2q:
 	add	$a0, $a0		# a0:a3+a0:a3
 	adc	$a1, $a1
 	 mov	$a0, $t0
 	adc	$a2, $a2
 	adc	$a3, $a3
 	 mov	$a1, $t1
 	sbb	$t4, $t4
 
 	sub	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	$poly1, $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	$poly3, $a3
 	test	$t4, $t4
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovz	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovz	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_mul_by_2q,.-__ecp_nistz256_mul_by_2q
 ___
 									}
 sub gen_double () {
     my $x = shift;
     my ($src0,$sfx,$bias);
     my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
 
     if ($x ne "x") {
 	$src0 = "%rax";
 	$sfx  = "";
 	$bias = 0;
 
 $code.=<<___;
 .globl	ecp_nistz256_point_double
 .type	ecp_nistz256_point_double,\@function,2
 .align	32
 ecp_nistz256_point_double:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 	cmp	\$0x80100, %ecx
 	je	.Lpoint_doublex
 ___
     } else {
 	$src0 = "%rdx";
 	$sfx  = "x";
 	$bias = 128;
 
 $code.=<<___;
 .type	ecp_nistz256_point_doublex,\@function,2
 .align	32
 ecp_nistz256_point_doublex:
 .Lpoint_doublex:
 ___
     }
 $code.=<<___;
 	push	%rbp
 	push	%rbx
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	sub	\$32*5+8, %rsp
 
+.Lpoint_double_shortcut$x:
 	movdqu	0x00($a_ptr), %xmm0		# copy	*(P256_POINT *)$a_ptr.x
 	mov	$a_ptr, $b_ptr			# backup copy
 	movdqu	0x10($a_ptr), %xmm1
 	 mov	0x20+8*0($a_ptr), $acc4		# load in_y in "5-4-0-1" order
 	 mov	0x20+8*1($a_ptr), $acc5
 	 mov	0x20+8*2($a_ptr), $acc0
 	 mov	0x20+8*3($a_ptr), $acc1
 	 mov	.Lpoly+8*1(%rip), $poly1
 	 mov	.Lpoly+8*3(%rip), $poly3
 	movdqa	%xmm0, $in_x(%rsp)
 	movdqa	%xmm1, $in_x+0x10(%rsp)
 	lea	0x20($r_ptr), $acc2
 	lea	0x40($r_ptr), $acc3
 	movq	$r_ptr, %xmm0
 	movq	$acc2, %xmm1
 	movq	$acc3, %xmm2
 
 	lea	$S(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_by_2$x	# p256_mul_by_2(S, in_y);
 
 	mov	0x40+8*0($a_ptr), $src0
 	mov	0x40+8*1($a_ptr), $acc6
 	mov	0x40+8*2($a_ptr), $acc7
 	mov	0x40+8*3($a_ptr), $acc0
 	lea	0x40-$bias($a_ptr), $a_ptr
 	lea	$Zsqr(%rsp), $r_ptr
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Zsqr, in_z);
 
 	`&load_for_sqr("$S(%rsp)", "$src0")`
 	lea	$S(%rsp), $r_ptr
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(S, S);
 
 	mov	0x20($b_ptr), $src0		# $b_ptr is still valid
 	mov	0x40+8*0($b_ptr), $acc1
 	mov	0x40+8*1($b_ptr), $acc2
 	mov	0x40+8*2($b_ptr), $acc3
 	mov	0x40+8*3($b_ptr), $acc4
 	lea	0x40-$bias($b_ptr), $a_ptr
 	lea	0x20($b_ptr), $b_ptr
 	movq	%xmm2, $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(res_z, in_z, in_y);
 	call	__ecp_nistz256_mul_by_2$x	# p256_mul_by_2(res_z, res_z);
 
 	mov	$in_x+8*0(%rsp), $acc4		# "5-4-0-1" order
 	mov	$in_x+8*1(%rsp), $acc5
 	lea	$Zsqr(%rsp), $b_ptr
 	mov	$in_x+8*2(%rsp), $acc0
 	mov	$in_x+8*3(%rsp), $acc1
 	lea	$M(%rsp), $r_ptr
 	call	__ecp_nistz256_add_to$x		# p256_add(M, in_x, Zsqr);
 
 	mov	$in_x+8*0(%rsp), $acc4		# "5-4-0-1" order
 	mov	$in_x+8*1(%rsp), $acc5
 	lea	$Zsqr(%rsp), $b_ptr
 	mov	$in_x+8*2(%rsp), $acc0
 	mov	$in_x+8*3(%rsp), $acc1
 	lea	$Zsqr(%rsp), $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(Zsqr, in_x, Zsqr);
 
 	`&load_for_sqr("$S(%rsp)", "$src0")`
 	movq	%xmm1, $r_ptr
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(res_y, S);
 ___
 {	
 ######## ecp_nistz256_div_by_2(res_y, res_y); ##########################
 # operate in 4-5-6-7 "name space" that matches squaring output
 #
 my ($poly1,$poly3)=($a_ptr,$t1);
 my ($a0,$a1,$a2,$a3,$t3,$t4,$t1)=($acc4,$acc5,$acc6,$acc7,$acc0,$acc1,$acc2);
 
 $code.=<<___;
 	xor	$t4, $t4
 	mov	$a0, $t0
 	add	\$-1, $a0
 	mov	$a1, $t1
 	adc	$poly1, $a1
 	mov	$a2, $t2
 	adc	\$0, $a2
 	mov	$a3, $t3
 	adc	$poly3, $a3
 	adc	\$0, $t4
 	xor	$a_ptr, $a_ptr		# borrow $a_ptr
 	test	\$1, $t0
 
 	cmovz	$t0, $a0
 	cmovz	$t1, $a1
 	cmovz	$t2, $a2
 	cmovz	$t3, $a3
 	cmovz	$a_ptr, $t4
 
 	mov	$a1, $t0		# a0:a3>>1
 	shr	\$1, $a0
 	shl	\$63, $t0
 	mov	$a2, $t1
 	shr	\$1, $a1
 	or	$t0, $a0
 	shl	\$63, $t1
 	mov	$a3, $t2
 	shr	\$1, $a2
 	or	$t1, $a1
 	shl	\$63, $t2
 	mov	$a0, 8*0($r_ptr)
 	shr	\$1, $a3
 	mov	$a1, 8*1($r_ptr)
 	shl	\$63, $t4
 	or	$t2, $a2
 	or	$t4, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 ___
 }
 $code.=<<___;
 	`&load_for_mul("$M(%rsp)", "$Zsqr(%rsp)", "$src0")`
 	lea	$M(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(M, M, Zsqr);
 
 	lea	$tmp0(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_by_2$x
 
 	lea	$M(%rsp), $b_ptr
 	lea	$M(%rsp), $r_ptr
 	call	__ecp_nistz256_add_to$x		# p256_mul_by_3(M, M);
 
 	`&load_for_mul("$S(%rsp)", "$in_x(%rsp)", "$src0")`
 	lea	$S(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S, S, in_x);
 
 	lea	$tmp0(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_by_2$x	# p256_mul_by_2(tmp0, S);
 
 	`&load_for_sqr("$M(%rsp)", "$src0")`
 	movq	%xmm0, $r_ptr
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(res_x, M);
 
 	lea	$tmp0(%rsp), $b_ptr
 	mov	$acc6, $acc0			# harmonize sqr output and sub input
 	mov	$acc7, $acc1
 	mov	$a_ptr, $poly1
 	mov	$t1, $poly3
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_x, res_x, tmp0);
 
 	mov	$S+8*0(%rsp), $t0
 	mov	$S+8*1(%rsp), $t1
 	mov	$S+8*2(%rsp), $t2
 	mov	$S+8*3(%rsp), $acc2		# "4-5-0-1" order
 	lea	$S(%rsp), $r_ptr
 	call	__ecp_nistz256_sub$x		# p256_sub(S, S, res_x);
 
 	mov	$M(%rsp), $src0
 	lea	$M(%rsp), $b_ptr
 	mov	$acc4, $acc6			# harmonize sub output and mul input
 	xor	%ecx, %ecx
 	mov	$acc4, $S+8*0(%rsp)		# have to save:-(	
 	mov	$acc5, $acc2
 	mov	$acc5, $S+8*1(%rsp)
 	cmovz	$acc0, $acc3
 	mov	$acc0, $S+8*2(%rsp)
 	lea	$S-$bias(%rsp), $a_ptr
 	cmovz	$acc1, $acc4
 	mov	$acc1, $S+8*3(%rsp)
 	mov	$acc6, $acc1
 	lea	$S(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S, S, M);
 
 	movq	%xmm1, $b_ptr
 	movq	%xmm1, $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_y, S, res_y);
 
 	add	\$32*5+8, %rsp
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbx
 	pop	%rbp
 	ret
 .size	ecp_nistz256_point_double$sfx,.-ecp_nistz256_point_double$sfx
 ___
 }
 &gen_double("q");
 
 sub gen_add () {
     my $x = shift;
     my ($src0,$sfx,$bias);
     my ($H,$Hsqr,$R,$Rsqr,$Hcub,
 	$U1,$U2,$S1,$S2,
 	$res_x,$res_y,$res_z,
 	$in1_x,$in1_y,$in1_z,
 	$in2_x,$in2_y,$in2_z)=map(32*$_,(0..17));
     my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
 
     if ($x ne "x") {
 	$src0 = "%rax";
 	$sfx  = "";
 	$bias = 0;
 
 $code.=<<___;
 .globl	ecp_nistz256_point_add
 .type	ecp_nistz256_point_add,\@function,3
 .align	32
 ecp_nistz256_point_add:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 	cmp	\$0x80100, %ecx
 	je	.Lpoint_addx
 ___
     } else {
 	$src0 = "%rdx";
 	$sfx  = "x";
 	$bias = 128;
 
 $code.=<<___;
 .type	ecp_nistz256_point_addx,\@function,3
 .align	32
 ecp_nistz256_point_addx:
 .Lpoint_addx:
 ___
     }
 $code.=<<___;
 	push	%rbp
 	push	%rbx
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	sub	\$32*18+8, %rsp
 
 	movdqu	0x00($a_ptr), %xmm0		# copy	*(P256_POINT *)$a_ptr
 	movdqu	0x10($a_ptr), %xmm1
 	movdqu	0x20($a_ptr), %xmm2
 	movdqu	0x30($a_ptr), %xmm3
 	movdqu	0x40($a_ptr), %xmm4
 	movdqu	0x50($a_ptr), %xmm5
 	mov	$a_ptr, $b_ptr			# reassign
 	mov	$b_org, $a_ptr			# reassign
 	movdqa	%xmm0, $in1_x(%rsp)
 	movdqa	%xmm1, $in1_x+0x10(%rsp)
 	por	%xmm0, %xmm1
 	movdqa	%xmm2, $in1_y(%rsp)
 	movdqa	%xmm3, $in1_y+0x10(%rsp)
 	por	%xmm2, %xmm3
 	movdqa	%xmm4, $in1_z(%rsp)
 	movdqa	%xmm5, $in1_z+0x10(%rsp)
 	por	%xmm1, %xmm3
 
 	movdqu	0x00($a_ptr), %xmm0		# copy	*(P256_POINT *)$b_ptr
 	 pshufd	\$0xb1, %xmm3, %xmm5
 	movdqu	0x10($a_ptr), %xmm1
 	movdqu	0x20($a_ptr), %xmm2
 	 por	%xmm3, %xmm5
 	movdqu	0x30($a_ptr), %xmm3
 	 mov	0x40+8*0($a_ptr), $src0		# load original in2_z
 	 mov	0x40+8*1($a_ptr), $acc6
 	 mov	0x40+8*2($a_ptr), $acc7
 	 mov	0x40+8*3($a_ptr), $acc0
 	movdqa	%xmm0, $in2_x(%rsp)
 	 pshufd	\$0x1e, %xmm5, %xmm4
 	movdqa	%xmm1, $in2_x+0x10(%rsp)
 	por	%xmm0, %xmm1
 	 movq	$r_ptr, %xmm0			# save $r_ptr
 	movdqa	%xmm2, $in2_y(%rsp)
 	movdqa	%xmm3, $in2_y+0x10(%rsp)
 	por	%xmm2, %xmm3
 	 por	%xmm4, %xmm5
 	 pxor	%xmm4, %xmm4
 	por	%xmm1, %xmm3
 
 	lea	0x40-$bias($a_ptr), $a_ptr	# $a_ptr is still valid
 	 mov	$src0, $in2_z+8*0(%rsp)		# make in2_z copy
 	 mov	$acc6, $in2_z+8*1(%rsp)
 	 mov	$acc7, $in2_z+8*2(%rsp)
 	 mov	$acc0, $in2_z+8*3(%rsp)
 	lea	$Z2sqr(%rsp), $r_ptr		# Z2^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Z2sqr, in2_z);
 
 	pcmpeqd	%xmm4, %xmm5
 	pshufd	\$0xb1, %xmm3, %xmm4
 	por	%xmm3, %xmm4
 	pshufd	\$0, %xmm5, %xmm5		# in1infty
 	pshufd	\$0x1e, %xmm4, %xmm3
 	por	%xmm3, %xmm4
 	pxor	%xmm3, %xmm3
 	pcmpeqd	%xmm3, %xmm4
 	pshufd	\$0, %xmm4, %xmm4		# in2infty
 	 mov	0x40+8*0($b_ptr), $src0		# load original in1_z
 	 mov	0x40+8*1($b_ptr), $acc6
 	 mov	0x40+8*2($b_ptr), $acc7
 	 mov	0x40+8*3($b_ptr), $acc0
+	movq	$b_ptr, %xmm1
 
 	lea	0x40-$bias($b_ptr), $a_ptr
 	lea	$Z1sqr(%rsp), $r_ptr		# Z1^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Z1sqr, in1_z);
 
 	`&load_for_mul("$Z2sqr(%rsp)", "$in2_z(%rsp)", "$src0")`
 	lea	$S1(%rsp), $r_ptr		# S1 = Z2^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S1, Z2sqr, in2_z);
 
 	`&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr		# S2 = Z1^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, Z1sqr, in1_z);
 
 	`&load_for_mul("$S1(%rsp)", "$in1_y(%rsp)", "$src0")`
 	lea	$S1(%rsp), $r_ptr		# S1 = Y1*Z2^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S1, S1, in1_y);
 
 	`&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr		# S2 = Y2*Z1^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, S2, in2_y);
 
 	lea	$S1(%rsp), $b_ptr
 	lea	$R(%rsp), $r_ptr		# R = S2 - S1
 	call	__ecp_nistz256_sub_from$x	# p256_sub(R, S2, S1);
 
 	or	$acc5, $acc4			# see if result is zero
 	movdqa	%xmm4, %xmm2
 	or	$acc0, $acc4
 	or	$acc1, $acc4
 	por	%xmm5, %xmm2			# in1infty || in2infty
 	movq	$acc4, %xmm3
 
 	`&load_for_mul("$Z2sqr(%rsp)", "$in1_x(%rsp)", "$src0")`
 	lea	$U1(%rsp), $r_ptr		# U1 = X1*Z2^2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(U1, in1_x, Z2sqr);
 
 	`&load_for_mul("$Z1sqr(%rsp)", "$in2_x(%rsp)", "$src0")`
 	lea	$U2(%rsp), $r_ptr		# U2 = X2*Z1^2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(U2, in2_x, Z1sqr);
 
 	lea	$U1(%rsp), $b_ptr
 	lea	$H(%rsp), $r_ptr		# H = U2 - U1
 	call	__ecp_nistz256_sub_from$x	# p256_sub(H, U2, U1);
 
 	or	$acc5, $acc4			# see if result is zero
 	or	$acc0, $acc4
 	or	$acc1, $acc4
 
 	.byte	0x3e				# predict taken
 	jnz	.Ladd_proceed$x			# is_equal(U1,U2)?
 	movq	%xmm2, $acc0
 	movq	%xmm3, $acc1
 	test	$acc0, $acc0
 	jnz	.Ladd_proceed$x			# (in1infty || in2infty)?
 	test	$acc1, $acc1
-	jz	.Ladd_proceed$x			# is_equal(S1,S2)?
+	jz	.Ladd_double$x			# is_equal(S1,S2)?
 
 	movq	%xmm0, $r_ptr			# restore $r_ptr
 	pxor	%xmm0, %xmm0
 	movdqu	%xmm0, 0x00($r_ptr)
 	movdqu	%xmm0, 0x10($r_ptr)
 	movdqu	%xmm0, 0x20($r_ptr)
 	movdqu	%xmm0, 0x30($r_ptr)
 	movdqu	%xmm0, 0x40($r_ptr)
 	movdqu	%xmm0, 0x50($r_ptr)
 	jmp	.Ladd_done$x
+
+.align	32
+.Ladd_double$x:
+	movq	%xmm1, $a_ptr			# restore $a_ptr
+	movq	%xmm0, $r_ptr			# restore $r_ptr
+	add	\$`32*(18-5)`, %rsp		# difference in frame sizes
+	jmp	.Lpoint_double_shortcut$x
 
 .align	32
 .Ladd_proceed$x:
 	`&load_for_sqr("$R(%rsp)", "$src0")`
 	lea	$Rsqr(%rsp), $r_ptr		# R^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Rsqr, R);
 
 	`&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
 	lea	$res_z(%rsp), $r_ptr		# Z3 = H*Z1*Z2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(res_z, H, in1_z);
 
 	`&load_for_sqr("$H(%rsp)", "$src0")`
 	lea	$Hsqr(%rsp), $r_ptr		# H^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Hsqr, H);
 
 	`&load_for_mul("$res_z(%rsp)", "$in2_z(%rsp)", "$src0")`
 	lea	$res_z(%rsp), $r_ptr		# Z3 = H*Z1*Z2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(res_z, res_z, in2_z);
 
 	`&load_for_mul("$Hsqr(%rsp)", "$H(%rsp)", "$src0")`
 	lea	$Hcub(%rsp), $r_ptr		# H^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(Hcub, Hsqr, H);
 
 	`&load_for_mul("$Hsqr(%rsp)", "$U1(%rsp)", "$src0")`
 	lea	$U2(%rsp), $r_ptr		# U1*H^2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(U2, U1, Hsqr);
 ___
 {
 #######################################################################
 # operate in 4-5-0-1 "name space" that matches multiplication output
 #
 my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
 my ($poly1, $poly3)=($acc6,$acc7);
 
 $code.=<<___;
 	#lea	$U2(%rsp), $a_ptr
 	#lea	$Hsqr(%rsp), $r_ptr	# 2*U1*H^2
 	#call	__ecp_nistz256_mul_by_2	# ecp_nistz256_mul_by_2(Hsqr, U2);
 
 	add	$acc0, $acc0		# a0:a3+a0:a3
 	lea	$Rsqr(%rsp), $a_ptr
 	adc	$acc1, $acc1
 	 mov	$acc0, $t0
 	adc	$acc2, $acc2
 	adc	$acc3, $acc3
 	 mov	$acc1, $t1
 	sbb	$t4, $t4
 
 	sub	\$-1, $acc0
 	 mov	$acc2, $t2
 	sbb	$poly1, $acc1
 	sbb	\$0, $acc2
 	 mov	$acc3, $t3
 	sbb	$poly3, $acc3
 	test	$t4, $t4
 
 	cmovz	$t0, $acc0
 	mov	8*0($a_ptr), $t0
 	cmovz	$t1, $acc1
 	mov	8*1($a_ptr), $t1
 	cmovz	$t2, $acc2
 	mov	8*2($a_ptr), $t2
 	cmovz	$t3, $acc3
 	mov	8*3($a_ptr), $t3
 
 	call	__ecp_nistz256_sub$x		# p256_sub(res_x, Rsqr, Hsqr);
 
 	lea	$Hcub(%rsp), $b_ptr
 	lea	$res_x(%rsp), $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_x, res_x, Hcub);
 
 	mov	$U2+8*0(%rsp), $t0
 	mov	$U2+8*1(%rsp), $t1
 	mov	$U2+8*2(%rsp), $t2
 	mov	$U2+8*3(%rsp), $t3
 	lea	$res_y(%rsp), $r_ptr
 
 	call	__ecp_nistz256_sub$x		# p256_sub(res_y, U2, res_x);
 
 	mov	$acc0, 8*0($r_ptr)		# save the result, as
 	mov	$acc1, 8*1($r_ptr)		# __ecp_nistz256_sub doesn't
 	mov	$acc2, 8*2($r_ptr)
 	mov	$acc3, 8*3($r_ptr)
 ___
 }
 $code.=<<___;
 	`&load_for_mul("$S1(%rsp)", "$Hcub(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, S1, Hcub);
 
 	`&load_for_mul("$R(%rsp)", "$res_y(%rsp)", "$src0")`
 	lea	$res_y(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(res_y, R, res_y);
 
 	lea	$S2(%rsp), $b_ptr
 	lea	$res_y(%rsp), $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_y, res_y, S2);
 
 	movq	%xmm0, $r_ptr		# restore $r_ptr
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_z, in2_z, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_z(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_z+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	$in2_z(%rsp), %xmm2
 	pand	$in2_z+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_z, in1_z, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_z(%rsp), %xmm2
 	pand	$in1_z+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x40($r_ptr)
 	movdqu	%xmm3, 0x50($r_ptr)
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_x, in2_x, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_x(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_x+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	$in2_x(%rsp), %xmm2
 	pand	$in2_x+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_x, in1_x, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_x(%rsp), %xmm2
 	pand	$in1_x+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x00($r_ptr)
 	movdqu	%xmm3, 0x10($r_ptr)
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_y, in2_y, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_y(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_y+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	$in2_y(%rsp), %xmm2
 	pand	$in2_y+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_y, in1_y, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_y(%rsp), %xmm2
 	pand	$in1_y+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x20($r_ptr)
 	movdqu	%xmm3, 0x30($r_ptr)
 
 .Ladd_done$x:
 	add	\$32*18+8, %rsp
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbx
 	pop	%rbp
 	ret
 .size	ecp_nistz256_point_add$sfx,.-ecp_nistz256_point_add$sfx
 ___
 }
 &gen_add("q");
 
 sub gen_add_affine () {
     my $x = shift;
     my ($src0,$sfx,$bias);
     my ($U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr,
 	$res_x,$res_y,$res_z,
 	$in1_x,$in1_y,$in1_z,
 	$in2_x,$in2_y)=map(32*$_,(0..14));
     my $Z1sqr = $S2;
 
     if ($x ne "x") {
 	$src0 = "%rax";
 	$sfx  = "";
 	$bias = 0;
 
 $code.=<<___;
 .globl	ecp_nistz256_point_add_affine
 .type	ecp_nistz256_point_add_affine,\@function,3
 .align	32
 ecp_nistz256_point_add_affine:
 ___
 $code.=<<___	if ($addx);
 	mov	\$0x80100, %ecx
 	and	OPENSSL_ia32cap_P+8(%rip), %ecx
 	cmp	\$0x80100, %ecx
 	je	.Lpoint_add_affinex
 ___
     } else {
 	$src0 = "%rdx";
 	$sfx  = "x";
 	$bias = 128;
 
 $code.=<<___;
 .type	ecp_nistz256_point_add_affinex,\@function,3
 .align	32
 ecp_nistz256_point_add_affinex:
 .Lpoint_add_affinex:
 ___
     }
 $code.=<<___;
 	push	%rbp
 	push	%rbx
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	sub	\$32*15+8, %rsp
 
 	movdqu	0x00($a_ptr), %xmm0	# copy	*(P256_POINT *)$a_ptr
 	mov	$b_org, $b_ptr		# reassign
 	movdqu	0x10($a_ptr), %xmm1
 	movdqu	0x20($a_ptr), %xmm2
 	movdqu	0x30($a_ptr), %xmm3
 	movdqu	0x40($a_ptr), %xmm4
 	movdqu	0x50($a_ptr), %xmm5
 	 mov	0x40+8*0($a_ptr), $src0	# load original in1_z
 	 mov	0x40+8*1($a_ptr), $acc6
 	 mov	0x40+8*2($a_ptr), $acc7
 	 mov	0x40+8*3($a_ptr), $acc0
 	movdqa	%xmm0, $in1_x(%rsp)
 	movdqa	%xmm1, $in1_x+0x10(%rsp)
 	por	%xmm0, %xmm1
 	movdqa	%xmm2, $in1_y(%rsp)
 	movdqa	%xmm3, $in1_y+0x10(%rsp)
 	por	%xmm2, %xmm3
 	movdqa	%xmm4, $in1_z(%rsp)
 	movdqa	%xmm5, $in1_z+0x10(%rsp)
 	por	%xmm1, %xmm3
 
 	movdqu	0x00($b_ptr), %xmm0	# copy	*(P256_POINT_AFFINE *)$b_ptr
 	 pshufd	\$0xb1, %xmm3, %xmm5
 	movdqu	0x10($b_ptr), %xmm1
 	movdqu	0x20($b_ptr), %xmm2
 	 por	%xmm3, %xmm5
 	movdqu	0x30($b_ptr), %xmm3
 	movdqa	%xmm0, $in2_x(%rsp)
 	 pshufd	\$0x1e, %xmm5, %xmm4
 	movdqa	%xmm1, $in2_x+0x10(%rsp)
 	por	%xmm0, %xmm1
 	 movq	$r_ptr, %xmm0		# save $r_ptr
 	movdqa	%xmm2, $in2_y(%rsp)
 	movdqa	%xmm3, $in2_y+0x10(%rsp)
 	por	%xmm2, %xmm3
 	 por	%xmm4, %xmm5
 	 pxor	%xmm4, %xmm4
 	por	%xmm1, %xmm3
 
 	lea	0x40-$bias($a_ptr), $a_ptr	# $a_ptr is still valid
 	lea	$Z1sqr(%rsp), $r_ptr		# Z1^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Z1sqr, in1_z);
 
 	pcmpeqd	%xmm4, %xmm5
 	pshufd	\$0xb1, %xmm3, %xmm4
 	 mov	0x00($b_ptr), $src0		# $b_ptr is still valid
 	 #lea	0x00($b_ptr), $b_ptr
 	 mov	$acc4, $acc1			# harmonize sqr output and mul input
 	por	%xmm3, %xmm4
 	pshufd	\$0, %xmm5, %xmm5		# in1infty
 	pshufd	\$0x1e, %xmm4, %xmm3
 	 mov	$acc5, $acc2
 	por	%xmm3, %xmm4
 	pxor	%xmm3, %xmm3
 	 mov	$acc6, $acc3
 	pcmpeqd	%xmm3, %xmm4
 	pshufd	\$0, %xmm4, %xmm4		# in2infty
 
 	lea	$Z1sqr-$bias(%rsp), $a_ptr
 	mov	$acc7, $acc4
 	lea	$U2(%rsp), $r_ptr		# U2 = X2*Z1^2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(U2, Z1sqr, in2_x);
 
 	lea	$in1_x(%rsp), $b_ptr
 	lea	$H(%rsp), $r_ptr		# H = U2 - U1
 	call	__ecp_nistz256_sub_from$x	# p256_sub(H, U2, in1_x);
 
 	`&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr		# S2 = Z1^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, Z1sqr, in1_z);
 
 	`&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
 	lea	$res_z(%rsp), $r_ptr		# Z3 = H*Z1*Z2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(res_z, H, in1_z);
 
 	`&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr		# S2 = Y2*Z1^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, S2, in2_y);
 
 	lea	$in1_y(%rsp), $b_ptr
 	lea	$R(%rsp), $r_ptr		# R = S2 - S1
 	call	__ecp_nistz256_sub_from$x	# p256_sub(R, S2, in1_y);
 
 	`&load_for_sqr("$H(%rsp)", "$src0")`
 	lea	$Hsqr(%rsp), $r_ptr		# H^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Hsqr, H);
 
 	`&load_for_sqr("$R(%rsp)", "$src0")`
 	lea	$Rsqr(%rsp), $r_ptr		# R^2
 	call	__ecp_nistz256_sqr_mont$x	# p256_sqr_mont(Rsqr, R);
 
 	`&load_for_mul("$H(%rsp)", "$Hsqr(%rsp)", "$src0")`
 	lea	$Hcub(%rsp), $r_ptr		# H^3
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(Hcub, Hsqr, H);
 
 	`&load_for_mul("$Hsqr(%rsp)", "$in1_x(%rsp)", "$src0")`
 	lea	$U2(%rsp), $r_ptr		# U1*H^2
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(U2, in1_x, Hsqr);
 ___
 {
 #######################################################################
 # operate in 4-5-0-1 "name space" that matches multiplication output
 #
 my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
 my ($poly1, $poly3)=($acc6,$acc7);
 
 $code.=<<___;
 	#lea	$U2(%rsp), $a_ptr
 	#lea	$Hsqr(%rsp), $r_ptr	# 2*U1*H^2
 	#call	__ecp_nistz256_mul_by_2	# ecp_nistz256_mul_by_2(Hsqr, U2);
 
 	add	$acc0, $acc0		# a0:a3+a0:a3
 	lea	$Rsqr(%rsp), $a_ptr
 	adc	$acc1, $acc1
 	 mov	$acc0, $t0
 	adc	$acc2, $acc2
 	adc	$acc3, $acc3
 	 mov	$acc1, $t1
 	sbb	$t4, $t4
 
 	sub	\$-1, $acc0
 	 mov	$acc2, $t2
 	sbb	$poly1, $acc1
 	sbb	\$0, $acc2
 	 mov	$acc3, $t3
 	sbb	$poly3, $acc3
 	test	$t4, $t4
 
 	cmovz	$t0, $acc0
 	mov	8*0($a_ptr), $t0
 	cmovz	$t1, $acc1
 	mov	8*1($a_ptr), $t1
 	cmovz	$t2, $acc2
 	mov	8*2($a_ptr), $t2
 	cmovz	$t3, $acc3
 	mov	8*3($a_ptr), $t3
 
 	call	__ecp_nistz256_sub$x		# p256_sub(res_x, Rsqr, Hsqr);
 
 	lea	$Hcub(%rsp), $b_ptr
 	lea	$res_x(%rsp), $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_x, res_x, Hcub);
 
 	mov	$U2+8*0(%rsp), $t0
 	mov	$U2+8*1(%rsp), $t1
 	mov	$U2+8*2(%rsp), $t2
 	mov	$U2+8*3(%rsp), $t3
 	lea	$H(%rsp), $r_ptr
 
 	call	__ecp_nistz256_sub$x		# p256_sub(H, U2, res_x);
 
 	mov	$acc0, 8*0($r_ptr)		# save the result, as
 	mov	$acc1, 8*1($r_ptr)		# __ecp_nistz256_sub doesn't
 	mov	$acc2, 8*2($r_ptr)
 	mov	$acc3, 8*3($r_ptr)
 ___
 }
 $code.=<<___;
 	`&load_for_mul("$Hcub(%rsp)", "$in1_y(%rsp)", "$src0")`
 	lea	$S2(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(S2, Hcub, in1_y);
 
 	`&load_for_mul("$H(%rsp)", "$R(%rsp)", "$src0")`
 	lea	$H(%rsp), $r_ptr
 	call	__ecp_nistz256_mul_mont$x	# p256_mul_mont(H, H, R);
 
 	lea	$S2(%rsp), $b_ptr
 	lea	$res_y(%rsp), $r_ptr
 	call	__ecp_nistz256_sub_from$x	# p256_sub(res_y, H, S2);
 
 	movq	%xmm0, $r_ptr		# restore $r_ptr
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_z, ONE, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_z(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_z+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	.LONE_mont(%rip), %xmm2
 	pand	.LONE_mont+0x10(%rip), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_z, in1_z, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_z(%rsp), %xmm2
 	pand	$in1_z+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x40($r_ptr)
 	movdqu	%xmm3, 0x50($r_ptr)
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_x, in2_x, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_x(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_x+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	$in2_x(%rsp), %xmm2
 	pand	$in2_x+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_x, in1_x, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_x(%rsp), %xmm2
 	pand	$in1_x+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x00($r_ptr)
 	movdqu	%xmm3, 0x10($r_ptr)
 
 	movdqa	%xmm5, %xmm0		# copy_conditional(res_y, in2_y, in1infty);
 	movdqa	%xmm5, %xmm1
 	pandn	$res_y(%rsp), %xmm0
 	movdqa	%xmm5, %xmm2
 	pandn	$res_y+0x10(%rsp), %xmm1
 	movdqa	%xmm5, %xmm3
 	pand	$in2_y(%rsp), %xmm2
 	pand	$in2_y+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 
 	movdqa	%xmm4, %xmm0		# copy_conditional(res_y, in1_y, in2infty);
 	movdqa	%xmm4, %xmm1
 	pandn	%xmm2, %xmm0
 	movdqa	%xmm4, %xmm2
 	pandn	%xmm3, %xmm1
 	movdqa	%xmm4, %xmm3
 	pand	$in1_y(%rsp), %xmm2
 	pand	$in1_y+0x10(%rsp), %xmm3
 	por	%xmm0, %xmm2
 	por	%xmm1, %xmm3
 	movdqu	%xmm2, 0x20($r_ptr)
 	movdqu	%xmm3, 0x30($r_ptr)
 
 	add	\$32*15+8, %rsp
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbx
 	pop	%rbp
 	ret
 .size	ecp_nistz256_point_add_affine$sfx,.-ecp_nistz256_point_add_affine$sfx
 ___
 }
 &gen_add_affine("q");
 
 ########################################################################
 # AD*X magic
 #
 if ($addx) {								{
 ########################################################################
 # operate in 4-5-0-1 "name space" that matches multiplication output
 #
 my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
 
 $code.=<<___;
 .type	__ecp_nistz256_add_tox,\@abi-omnipotent
 .align	32
 __ecp_nistz256_add_tox:
 	xor	$t4, $t4
 	adc	8*0($b_ptr), $a0
 	adc	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	adc	8*2($b_ptr), $a2
 	adc	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	adc	\$0, $t4
 
 	xor	$t3, $t3
 	sbb	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	$poly1, $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	$poly3, $a3
 
 	bt	\$0, $t4
 	cmovnc	$t0, $a0
 	cmovnc	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovnc	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovnc	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_add_tox,.-__ecp_nistz256_add_tox
 
 .type	__ecp_nistz256_sub_fromx,\@abi-omnipotent
 .align	32
 __ecp_nistz256_sub_fromx:
 	xor	$t4, $t4
 	sbb	8*0($b_ptr), $a0
 	sbb	8*1($b_ptr), $a1
 	 mov	$a0, $t0
 	sbb	8*2($b_ptr), $a2
 	sbb	8*3($b_ptr), $a3
 	 mov	$a1, $t1
 	sbb	\$0, $t4
 
 	xor	$t3, $t3
 	adc	\$-1, $a0
 	 mov	$a2, $t2
 	adc	$poly1, $a1
 	adc	\$0, $a2
 	 mov	$a3, $t3
 	adc	$poly3, $a3
 
 	bt	\$0, $t4
 	cmovnc	$t0, $a0
 	cmovnc	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovnc	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovnc	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_sub_fromx,.-__ecp_nistz256_sub_fromx
 
 .type	__ecp_nistz256_subx,\@abi-omnipotent
 .align	32
 __ecp_nistz256_subx:
 	xor	$t4, $t4
 	sbb	$a0, $t0
 	sbb	$a1, $t1
 	 mov	$t0, $a0
 	sbb	$a2, $t2
 	sbb	$a3, $t3
 	 mov	$t1, $a1
 	sbb	\$0, $t4
 
 	xor	$a3 ,$a3
 	adc	\$-1, $t0
 	 mov	$t2, $a2
 	adc	$poly1, $t1
 	adc	\$0, $t2
 	 mov	$t3, $a3
 	adc	$poly3, $t3
 
 	bt	\$0, $t4
 	cmovc	$t0, $a0
 	cmovc	$t1, $a1
 	cmovc	$t2, $a2
 	cmovc	$t3, $a3
 
 	ret
 .size	__ecp_nistz256_subx,.-__ecp_nistz256_subx
 
 .type	__ecp_nistz256_mul_by_2x,\@abi-omnipotent
 .align	32
 __ecp_nistz256_mul_by_2x:
 	xor	$t4, $t4
 	adc	$a0, $a0		# a0:a3+a0:a3
 	adc	$a1, $a1
 	 mov	$a0, $t0
 	adc	$a2, $a2
 	adc	$a3, $a3
 	 mov	$a1, $t1
 	adc	\$0, $t4
 
 	xor	$t3, $t3
 	sbb	\$-1, $a0
 	 mov	$a2, $t2
 	sbb	$poly1, $a1
 	sbb	\$0, $a2
 	 mov	$a3, $t3
 	sbb	$poly3, $a3
 
 	bt	\$0, $t4
 	cmovnc	$t0, $a0
 	cmovnc	$t1, $a1
 	mov	$a0, 8*0($r_ptr)
 	cmovnc	$t2, $a2
 	mov	$a1, 8*1($r_ptr)
 	cmovnc	$t3, $a3
 	mov	$a2, 8*2($r_ptr)
 	mov	$a3, 8*3($r_ptr)
 
 	ret
 .size	__ecp_nistz256_mul_by_2x,.-__ecp_nistz256_mul_by_2x
 ___
 									}
 &gen_double("x");
 &gen_add("x");
 &gen_add_affine("x");
 }
 }}}
 
 $code =~ s/\`([^\`]*)\`/eval $1/gem;
 print $code;
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/ec/ecp_nistp224.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/ec/ecp_nistp224.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/ec/ecp_nistp224.c	(revision 296273)
@@ -1,1769 +1,1769 @@
 /* crypto/ec/ecp_nistp224.c */
 /*
  * Written by Emilia Kasper (Google) for the OpenSSL project.
  */
 /* Copyright 2011 Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  *
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 /*
  * A 64-bit implementation of the NIST P-224 elliptic curve point multiplication
  *
  * Inspired by Daniel J. Bernstein's public domain nistp224 implementation
  * and Adam Langley's public domain 64-bit C implementation of curve25519
  */
 
 #include 
 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
 
 # ifndef OPENSSL_SYS_VMS
 #  include 
 # else
 #  include 
 # endif
 
 # include 
 # include 
 # include "ec_lcl.h"
 
 # if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
   /* even with gcc, the typedef won't work for 32-bit platforms */
 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
                                  * platforms */
 # else
 #  error "Need GCC 3.1 or later to define type uint128_t"
 # endif
 
 typedef uint8_t u8;
 typedef uint64_t u64;
 typedef int64_t s64;
 
 /******************************************************************************/
 /*-
  * INTERNAL REPRESENTATION OF FIELD ELEMENTS
  *
  * Field elements are represented as a_0 + 2^56*a_1 + 2^112*a_2 + 2^168*a_3
  * using 64-bit coefficients called 'limbs',
  * and sometimes (for multiplication results) as
  * b_0 + 2^56*b_1 + 2^112*b_2 + 2^168*b_3 + 2^224*b_4 + 2^280*b_5 + 2^336*b_6
  * using 128-bit coefficients called 'widelimbs'.
  * A 4-limb representation is an 'felem';
  * a 7-widelimb representation is a 'widefelem'.
  * Even within felems, bits of adjacent limbs overlap, and we don't always
  * reduce the representations: we ensure that inputs to each felem
  * multiplication satisfy a_i < 2^60, so outputs satisfy b_i < 4*2^60*2^60,
  * and fit into a 128-bit word without overflow. The coefficients are then
  * again partially reduced to obtain an felem satisfying a_i < 2^57.
  * We only reduce to the unique minimal representation at the end of the
  * computation.
  */
 
 typedef uint64_t limb;
 typedef uint128_t widelimb;
 
 typedef limb felem[4];
 typedef widelimb widefelem[7];
 
 /*
  * Field element represented as a byte arrary. 28*8 = 224 bits is also the
  * group order size for the elliptic curve, and we also use this type for
  * scalars for point multiplication.
  */
 typedef u8 felem_bytearray[28];
 
 static const felem_bytearray nistp224_curve_params[5] = {
     {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* p */
      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
     {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* a */
      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
      0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE},
     {0xB4, 0x05, 0x0A, 0x85, 0x0C, 0x04, 0xB3, 0xAB, 0xF5, 0x41, /* b */
      0x32, 0x56, 0x50, 0x44, 0xB0, 0xB7, 0xD7, 0xBF, 0xD8, 0xBA,
      0x27, 0x0B, 0x39, 0x43, 0x23, 0x55, 0xFF, 0xB4},
     {0xB7, 0x0E, 0x0C, 0xBD, 0x6B, 0xB4, 0xBF, 0x7F, 0x32, 0x13, /* x */
      0x90, 0xB9, 0x4A, 0x03, 0xC1, 0xD3, 0x56, 0xC2, 0x11, 0x22,
      0x34, 0x32, 0x80, 0xD6, 0x11, 0x5C, 0x1D, 0x21},
     {0xbd, 0x37, 0x63, 0x88, 0xb5, 0xf7, 0x23, 0xfb, 0x4c, 0x22, /* y */
      0xdf, 0xe6, 0xcd, 0x43, 0x75, 0xa0, 0x5a, 0x07, 0x47, 0x64,
      0x44, 0xd5, 0x81, 0x99, 0x85, 0x00, 0x7e, 0x34}
 };
 
 /*-
  * Precomputed multiples of the standard generator
  * Points are given in coordinates (X, Y, Z) where Z normally is 1
  * (0 for the point at infinity).
  * For each field element, slice a_0 is word 0, etc.
  *
  * The table has 2 * 16 elements, starting with the following:
  * index | bits    | point
  * ------+---------+------------------------------
  *     0 | 0 0 0 0 | 0G
  *     1 | 0 0 0 1 | 1G
  *     2 | 0 0 1 0 | 2^56G
  *     3 | 0 0 1 1 | (2^56 + 1)G
  *     4 | 0 1 0 0 | 2^112G
  *     5 | 0 1 0 1 | (2^112 + 1)G
  *     6 | 0 1 1 0 | (2^112 + 2^56)G
  *     7 | 0 1 1 1 | (2^112 + 2^56 + 1)G
  *     8 | 1 0 0 0 | 2^168G
  *     9 | 1 0 0 1 | (2^168 + 1)G
  *    10 | 1 0 1 0 | (2^168 + 2^56)G
  *    11 | 1 0 1 1 | (2^168 + 2^56 + 1)G
  *    12 | 1 1 0 0 | (2^168 + 2^112)G
  *    13 | 1 1 0 1 | (2^168 + 2^112 + 1)G
  *    14 | 1 1 1 0 | (2^168 + 2^112 + 2^56)G
  *    15 | 1 1 1 1 | (2^168 + 2^112 + 2^56 + 1)G
  * followed by a copy of this with each element multiplied by 2^28.
  *
  * The reason for this is so that we can clock bits into four different
  * locations when doing simple scalar multiplies against the base point,
  * and then another four locations using the second 16 elements.
  */
 static const felem gmul[2][16][3] = { {{{0, 0, 0, 0},
                                         {0, 0, 0, 0},
                                         {0, 0, 0, 0}},
                                        {{0x3280d6115c1d21, 0xc1d356c2112234,
                                          0x7f321390b94a03, 0xb70e0cbd6bb4bf},
                                         {0xd5819985007e34, 0x75a05a07476444,
                                          0xfb4c22dfe6cd43, 0xbd376388b5f723},
                                         {1, 0, 0, 0}},
                                        {{0xfd9675666ebbe9, 0xbca7664d40ce5e,
                                          0x2242df8d8a2a43, 0x1f49bbb0f99bc5},
                                         {0x29e0b892dc9c43, 0xece8608436e662,
                                          0xdc858f185310d0, 0x9812dd4eb8d321},
                                         {1, 0, 0, 0}},
                                        {{0x6d3e678d5d8eb8, 0x559eed1cb362f1,
                                          0x16e9a3bbce8a3f, 0xeedcccd8c2a748},
                                         {0xf19f90ed50266d, 0xabf2b4bf65f9df,
                                          0x313865468fafec, 0x5cb379ba910a17},
                                         {1, 0, 0, 0}},
                                        {{0x0641966cab26e3, 0x91fb2991fab0a0,
                                          0xefec27a4e13a0b, 0x0499aa8a5f8ebe},
                                         {0x7510407766af5d, 0x84d929610d5450,
                                          0x81d77aae82f706, 0x6916f6d4338c5b},
                                         {1, 0, 0, 0}},
                                        {{0xea95ac3b1f15c6, 0x086000905e82d4,
                                          0xdd323ae4d1c8b1, 0x932b56be7685a3},
                                         {0x9ef93dea25dbbf, 0x41665960f390f0,
                                          0xfdec76dbe2a8a7, 0x523e80f019062a},
                                         {1, 0, 0, 0}},
                                        {{0x822fdd26732c73, 0xa01c83531b5d0f,
                                          0x363f37347c1ba4, 0xc391b45c84725c},
                                         {0xbbd5e1b2d6ad24, 0xddfbcde19dfaec,
                                          0xc393da7e222a7f, 0x1efb7890ede244},
                                         {1, 0, 0, 0}},
                                        {{0x4c9e90ca217da1, 0xd11beca79159bb,
                                          0xff8d33c2c98b7c, 0x2610b39409f849},
                                         {0x44d1352ac64da0, 0xcdbb7b2c46b4fb,
                                          0x966c079b753c89, 0xfe67e4e820b112},
                                         {1, 0, 0, 0}},
                                        {{0xe28cae2df5312d, 0xc71b61d16f5c6e,
                                          0x79b7619a3e7c4c, 0x05c73240899b47},
                                         {0x9f7f6382c73e3a, 0x18615165c56bda,
                                          0x641fab2116fd56, 0x72855882b08394},
                                         {1, 0, 0, 0}},
                                        {{0x0469182f161c09, 0x74a98ca8d00fb5,
                                          0xb89da93489a3e0, 0x41c98768fb0c1d},
                                         {0xe5ea05fb32da81, 0x3dce9ffbca6855,
                                          0x1cfe2d3fbf59e6, 0x0e5e03408738a7},
                                         {1, 0, 0, 0}},
                                        {{0xdab22b2333e87f, 0x4430137a5dd2f6,
                                          0xe03ab9f738beb8, 0xcb0c5d0dc34f24},
                                         {0x764a7df0c8fda5, 0x185ba5c3fa2044,
                                          0x9281d688bcbe50, 0xc40331df893881},
                                         {1, 0, 0, 0}},
                                        {{0xb89530796f0f60, 0xade92bd26909a3,
                                          0x1a0c83fb4884da, 0x1765bf22a5a984},
                                         {0x772a9ee75db09e, 0x23bc6c67cec16f,
                                          0x4c1edba8b14e2f, 0xe2a215d9611369},
                                         {1, 0, 0, 0}},
                                        {{0x571e509fb5efb3, 0xade88696410552,
                                          0xc8ae85fada74fe, 0x6c7e4be83bbde3},
                                         {0xff9f51160f4652, 0xb47ce2495a6539,
                                          0xa2946c53b582f4, 0x286d2db3ee9a60},
                                         {1, 0, 0, 0}},
                                        {{0x40bbd5081a44af, 0x0995183b13926c,
                                          0xbcefba6f47f6d0, 0x215619e9cc0057},
                                         {0x8bc94d3b0df45e, 0xf11c54a3694f6f,
                                          0x8631b93cdfe8b5, 0xe7e3f4b0982db9},
                                         {1, 0, 0, 0}},
                                        {{0xb17048ab3e1c7b, 0xac38f36ff8a1d8,
                                          0x1c29819435d2c6, 0xc813132f4c07e9},
                                         {0x2891425503b11f, 0x08781030579fea,
                                          0xf5426ba5cc9674, 0x1e28ebf18562bc},
                                         {1, 0, 0, 0}},
                                        {{0x9f31997cc864eb, 0x06cd91d28b5e4c,
                                          0xff17036691a973, 0xf1aef351497c58},
                                         {0xdd1f2d600564ff, 0xdead073b1402db,
                                          0x74a684435bd693, 0xeea7471f962558},
                                         {1, 0, 0, 0}}},
 {{{0, 0, 0, 0},
   {0, 0, 0, 0},
   {0, 0, 0, 0}},
  {{0x9665266dddf554, 0x9613d78b60ef2d, 0xce27a34cdba417, 0xd35ab74d6afc31},
   {0x85ccdd22deb15e, 0x2137e5783a6aab, 0xa141cffd8c93c6, 0x355a1830e90f2d},
   {1, 0, 0, 0}},
  {{0x1a494eadaade65, 0xd6da4da77fe53c, 0xe7992996abec86, 0x65c3553c6090e3},
   {0xfa610b1fb09346, 0xf1c6540b8a4aaf, 0xc51a13ccd3cbab, 0x02995b1b18c28a},
   {1, 0, 0, 0}},
  {{0x7874568e7295ef, 0x86b419fbe38d04, 0xdc0690a7550d9a, 0xd3966a44beac33},
   {0x2b7280ec29132f, 0xbeaa3b6a032df3, 0xdc7dd88ae41200, 0xd25e2513e3a100},
   {1, 0, 0, 0}},
  {{0x924857eb2efafd, 0xac2bce41223190, 0x8edaa1445553fc, 0x825800fd3562d5},
   {0x8d79148ea96621, 0x23a01c3dd9ed8d, 0xaf8b219f9416b5, 0xd8db0cc277daea},
   {1, 0, 0, 0}},
  {{0x76a9c3b1a700f0, 0xe9acd29bc7e691, 0x69212d1a6b0327, 0x6322e97fe154be},
   {0x469fc5465d62aa, 0x8d41ed18883b05, 0x1f8eae66c52b88, 0xe4fcbe9325be51},
   {1, 0, 0, 0}},
  {{0x825fdf583cac16, 0x020b857c7b023a, 0x683c17744b0165, 0x14ffd0a2daf2f1},
   {0x323b36184218f9, 0x4944ec4e3b47d4, 0xc15b3080841acf, 0x0bced4b01a28bb},
   {1, 0, 0, 0}},
  {{0x92ac22230df5c4, 0x52f33b4063eda8, 0xcb3f19870c0c93, 0x40064f2ba65233},
   {0xfe16f0924f8992, 0x012da25af5b517, 0x1a57bb24f723a6, 0x06f8bc76760def},
   {1, 0, 0, 0}},
  {{0x4a7084f7817cb9, 0xbcab0738ee9a78, 0x3ec11e11d9c326, 0xdc0fe90e0f1aae},
   {0xcf639ea5f98390, 0x5c350aa22ffb74, 0x9afae98a4047b7, 0x956ec2d617fc45},
   {1, 0, 0, 0}},
  {{0x4306d648c1be6a, 0x9247cd8bc9a462, 0xf5595e377d2f2e, 0xbd1c3caff1a52e},
   {0x045e14472409d0, 0x29f3e17078f773, 0x745a602b2d4f7d, 0x191837685cdfbb},
   {1, 0, 0, 0}},
  {{0x5b6ee254a8cb79, 0x4953433f5e7026, 0xe21faeb1d1def4, 0xc4c225785c09de},
   {0x307ce7bba1e518, 0x31b125b1036db8, 0x47e91868839e8f, 0xc765866e33b9f3},
   {1, 0, 0, 0}},
  {{0x3bfece24f96906, 0x4794da641e5093, 0xde5df64f95db26, 0x297ecd89714b05},
   {0x701bd3ebb2c3aa, 0x7073b4f53cb1d5, 0x13c5665658af16, 0x9895089d66fe58},
   {1, 0, 0, 0}},
  {{0x0fef05f78c4790, 0x2d773633b05d2e, 0x94229c3a951c94, 0xbbbd70df4911bb},
   {0xb2c6963d2c1168, 0x105f47a72b0d73, 0x9fdf6111614080, 0x7b7e94b39e67b0},
   {1, 0, 0, 0}},
  {{0xad1a7d6efbe2b3, 0xf012482c0da69d, 0x6b3bdf12438345, 0x40d7558d7aa4d9},
   {0x8a09fffb5c6d3d, 0x9a356e5d9ffd38, 0x5973f15f4f9b1c, 0xdcd5f59f63c3ea},
   {1, 0, 0, 0}},
  {{0xacf39f4c5ca7ab, 0x4c8071cc5fd737, 0xc64e3602cd1184, 0x0acd4644c9abba},
   {0x6c011a36d8bf6e, 0xfecd87ba24e32a, 0x19f6f56574fad8, 0x050b204ced9405},
   {1, 0, 0, 0}},
  {{0xed4f1cae7d9a96, 0x5ceef7ad94c40a, 0x778e4a3bf3ef9b, 0x7405783dc3b55e},
   {0x32477c61b6e8c6, 0xb46a97570f018b, 0x91176d0a7e95d1, 0x3df90fbc4c7d0e},
   {1, 0, 0, 0}}}
 };
 
 /* Precomputation for the group generator. */
 typedef struct {
     felem g_pre_comp[2][16][3];
     int references;
 } NISTP224_PRE_COMP;
 
 const EC_METHOD *EC_GFp_nistp224_method(void)
 {
     static const EC_METHOD ret = {
         EC_FLAGS_DEFAULT_OCT,
         NID_X9_62_prime_field,
         ec_GFp_nistp224_group_init,
         ec_GFp_simple_group_finish,
         ec_GFp_simple_group_clear_finish,
         ec_GFp_nist_group_copy,
         ec_GFp_nistp224_group_set_curve,
         ec_GFp_simple_group_get_curve,
         ec_GFp_simple_group_get_degree,
         ec_GFp_simple_group_check_discriminant,
         ec_GFp_simple_point_init,
         ec_GFp_simple_point_finish,
         ec_GFp_simple_point_clear_finish,
         ec_GFp_simple_point_copy,
         ec_GFp_simple_point_set_to_infinity,
         ec_GFp_simple_set_Jprojective_coordinates_GFp,
         ec_GFp_simple_get_Jprojective_coordinates_GFp,
         ec_GFp_simple_point_set_affine_coordinates,
         ec_GFp_nistp224_point_get_affine_coordinates,
         0 /* point_set_compressed_coordinates */ ,
         0 /* point2oct */ ,
         0 /* oct2point */ ,
         ec_GFp_simple_add,
         ec_GFp_simple_dbl,
         ec_GFp_simple_invert,
         ec_GFp_simple_is_at_infinity,
         ec_GFp_simple_is_on_curve,
         ec_GFp_simple_cmp,
         ec_GFp_simple_make_affine,
         ec_GFp_simple_points_make_affine,
         ec_GFp_nistp224_points_mul,
         ec_GFp_nistp224_precompute_mult,
         ec_GFp_nistp224_have_precompute_mult,
         ec_GFp_nist_field_mul,
         ec_GFp_nist_field_sqr,
         0 /* field_div */ ,
         0 /* field_encode */ ,
         0 /* field_decode */ ,
         0                       /* field_set_to_one */
     };
 
     return &ret;
 }
 
 /*
  * Helper functions to convert field elements to/from internal representation
  */
 static void bin28_to_felem(felem out, const u8 in[28])
 {
     out[0] = *((const uint64_t *)(in)) & 0x00ffffffffffffff;
     out[1] = (*((const uint64_t *)(in + 7))) & 0x00ffffffffffffff;
     out[2] = (*((const uint64_t *)(in + 14))) & 0x00ffffffffffffff;
     out[3] = (*((const uint64_t *)(in+20))) >> 8;
 }
 
 static void felem_to_bin28(u8 out[28], const felem in)
 {
     unsigned i;
     for (i = 0; i < 7; ++i) {
         out[i] = in[0] >> (8 * i);
         out[i + 7] = in[1] >> (8 * i);
         out[i + 14] = in[2] >> (8 * i);
         out[i + 21] = in[3] >> (8 * i);
     }
 }
 
 /* To preserve endianness when using BN_bn2bin and BN_bin2bn */
 static void flip_endian(u8 *out, const u8 *in, unsigned len)
 {
     unsigned i;
     for (i = 0; i < len; ++i)
         out[i] = in[len - 1 - i];
 }
 
 /* From OpenSSL BIGNUM to internal representation */
 static int BN_to_felem(felem out, const BIGNUM *bn)
 {
     felem_bytearray b_in;
     felem_bytearray b_out;
     unsigned num_bytes;
 
     /* BN_bn2bin eats leading zeroes */
     memset(b_out, 0, sizeof b_out);
     num_bytes = BN_num_bytes(bn);
     if (num_bytes > sizeof b_out) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     if (BN_is_negative(bn)) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     num_bytes = BN_bn2bin(bn, b_in);
     flip_endian(b_out, b_in, num_bytes);
     bin28_to_felem(out, b_out);
     return 1;
 }
 
 /* From internal representation to OpenSSL BIGNUM */
 static BIGNUM *felem_to_BN(BIGNUM *out, const felem in)
 {
     felem_bytearray b_in, b_out;
     felem_to_bin28(b_in, in);
     flip_endian(b_out, b_in, sizeof b_out);
     return BN_bin2bn(b_out, sizeof b_out, out);
 }
 
 /******************************************************************************/
 /*-
  *                              FIELD OPERATIONS
  *
  * Field operations, using the internal representation of field elements.
  * NB! These operations are specific to our point multiplication and cannot be
  * expected to be correct in general - e.g., multiplication with a large scalar
  * will cause an overflow.
  *
  */
 
 static void felem_one(felem out)
 {
     out[0] = 1;
     out[1] = 0;
     out[2] = 0;
     out[3] = 0;
 }
 
 static void felem_assign(felem out, const felem in)
 {
     out[0] = in[0];
     out[1] = in[1];
     out[2] = in[2];
     out[3] = in[3];
 }
 
 /* Sum two field elements: out += in */
 static void felem_sum(felem out, const felem in)
 {
     out[0] += in[0];
     out[1] += in[1];
     out[2] += in[2];
     out[3] += in[3];
 }
 
 /* Get negative value: out = -in */
 /* Assumes in[i] < 2^57 */
 static void felem_neg(felem out, const felem in)
 {
     static const limb two58p2 = (((limb) 1) << 58) + (((limb) 1) << 2);
     static const limb two58m2 = (((limb) 1) << 58) - (((limb) 1) << 2);
     static const limb two58m42m2 = (((limb) 1) << 58) -
         (((limb) 1) << 42) - (((limb) 1) << 2);
 
     /* Set to 0 mod 2^224-2^96+1 to ensure out > in */
     out[0] = two58p2 - in[0];
     out[1] = two58m42m2 - in[1];
     out[2] = two58m2 - in[2];
     out[3] = two58m2 - in[3];
 }
 
 /* Subtract field elements: out -= in */
 /* Assumes in[i] < 2^57 */
 static void felem_diff(felem out, const felem in)
 {
     static const limb two58p2 = (((limb) 1) << 58) + (((limb) 1) << 2);
     static const limb two58m2 = (((limb) 1) << 58) - (((limb) 1) << 2);
     static const limb two58m42m2 = (((limb) 1) << 58) -
         (((limb) 1) << 42) - (((limb) 1) << 2);
 
     /* Add 0 mod 2^224-2^96+1 to ensure out > in */
     out[0] += two58p2;
     out[1] += two58m42m2;
     out[2] += two58m2;
     out[3] += two58m2;
 
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
 }
 
 /* Subtract in unreduced 128-bit mode: out -= in */
 /* Assumes in[i] < 2^119 */
 static void widefelem_diff(widefelem out, const widefelem in)
 {
     static const widelimb two120 = ((widelimb) 1) << 120;
     static const widelimb two120m64 = (((widelimb) 1) << 120) -
         (((widelimb) 1) << 64);
     static const widelimb two120m104m64 = (((widelimb) 1) << 120) -
         (((widelimb) 1) << 104) - (((widelimb) 1) << 64);
 
     /* Add 0 mod 2^224-2^96+1 to ensure out > in */
     out[0] += two120;
     out[1] += two120m64;
     out[2] += two120m64;
     out[3] += two120;
     out[4] += two120m104m64;
     out[5] += two120m64;
     out[6] += two120m64;
 
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
     out[4] -= in[4];
     out[5] -= in[5];
     out[6] -= in[6];
 }
 
 /* Subtract in mixed mode: out128 -= in64 */
 /* in[i] < 2^63 */
 static void felem_diff_128_64(widefelem out, const felem in)
 {
     static const widelimb two64p8 = (((widelimb) 1) << 64) +
         (((widelimb) 1) << 8);
     static const widelimb two64m8 = (((widelimb) 1) << 64) -
         (((widelimb) 1) << 8);
     static const widelimb two64m48m8 = (((widelimb) 1) << 64) -
         (((widelimb) 1) << 48) - (((widelimb) 1) << 8);
 
     /* Add 0 mod 2^224-2^96+1 to ensure out > in */
     out[0] += two64p8;
     out[1] += two64m48m8;
     out[2] += two64m8;
     out[3] += two64m8;
 
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
 }
 
 /*
  * Multiply a field element by a scalar: out = out * scalar The scalars we
  * actually use are small, so results fit without overflow
  */
 static void felem_scalar(felem out, const limb scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
 }
 
 /*
  * Multiply an unreduced field element by a scalar: out = out * scalar The
  * scalars we actually use are small, so results fit without overflow
  */
 static void widefelem_scalar(widefelem out, const widelimb scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
     out[4] *= scalar;
     out[5] *= scalar;
     out[6] *= scalar;
 }
 
 /* Square a field element: out = in^2 */
 static void felem_square(widefelem out, const felem in)
 {
     limb tmp0, tmp1, tmp2;
     tmp0 = 2 * in[0];
     tmp1 = 2 * in[1];
     tmp2 = 2 * in[2];
     out[0] = ((widelimb) in[0]) * in[0];
     out[1] = ((widelimb) in[0]) * tmp1;
     out[2] = ((widelimb) in[0]) * tmp2 + ((widelimb) in[1]) * in[1];
     out[3] = ((widelimb) in[3]) * tmp0 + ((widelimb) in[1]) * tmp2;
     out[4] = ((widelimb) in[3]) * tmp1 + ((widelimb) in[2]) * in[2];
     out[5] = ((widelimb) in[3]) * tmp2;
     out[6] = ((widelimb) in[3]) * in[3];
 }
 
 /* Multiply two field elements: out = in1 * in2 */
 static void felem_mul(widefelem out, const felem in1, const felem in2)
 {
     out[0] = ((widelimb) in1[0]) * in2[0];
     out[1] = ((widelimb) in1[0]) * in2[1] + ((widelimb) in1[1]) * in2[0];
     out[2] = ((widelimb) in1[0]) * in2[2] + ((widelimb) in1[1]) * in2[1] +
         ((widelimb) in1[2]) * in2[0];
     out[3] = ((widelimb) in1[0]) * in2[3] + ((widelimb) in1[1]) * in2[2] +
         ((widelimb) in1[2]) * in2[1] + ((widelimb) in1[3]) * in2[0];
     out[4] = ((widelimb) in1[1]) * in2[3] + ((widelimb) in1[2]) * in2[2] +
         ((widelimb) in1[3]) * in2[1];
     out[5] = ((widelimb) in1[2]) * in2[3] + ((widelimb) in1[3]) * in2[2];
     out[6] = ((widelimb) in1[3]) * in2[3];
 }
 
 /*-
  * Reduce seven 128-bit coefficients to four 64-bit coefficients.
  * Requires in[i] < 2^126,
  * ensures out[0] < 2^56, out[1] < 2^56, out[2] < 2^56, out[3] <= 2^56 + 2^16 */
 static void felem_reduce(felem out, const widefelem in)
 {
     static const widelimb two127p15 = (((widelimb) 1) << 127) +
         (((widelimb) 1) << 15);
     static const widelimb two127m71 = (((widelimb) 1) << 127) -
         (((widelimb) 1) << 71);
     static const widelimb two127m71m55 = (((widelimb) 1) << 127) -
         (((widelimb) 1) << 71) - (((widelimb) 1) << 55);
     widelimb output[5];
 
     /* Add 0 mod 2^224-2^96+1 to ensure all differences are positive */
     output[0] = in[0] + two127p15;
     output[1] = in[1] + two127m71m55;
     output[2] = in[2] + two127m71;
     output[3] = in[3];
     output[4] = in[4];
 
     /* Eliminate in[4], in[5], in[6] */
     output[4] += in[6] >> 16;
     output[3] += (in[6] & 0xffff) << 40;
     output[2] -= in[6];
 
     output[3] += in[5] >> 16;
     output[2] += (in[5] & 0xffff) << 40;
     output[1] -= in[5];
 
     output[2] += output[4] >> 16;
     output[1] += (output[4] & 0xffff) << 40;
     output[0] -= output[4];
 
     /* Carry 2 -> 3 -> 4 */
     output[3] += output[2] >> 56;
     output[2] &= 0x00ffffffffffffff;
 
     output[4] = output[3] >> 56;
     output[3] &= 0x00ffffffffffffff;
 
     /* Now output[2] < 2^56, output[3] < 2^56, output[4] < 2^72 */
 
     /* Eliminate output[4] */
     output[2] += output[4] >> 16;
     /* output[2] < 2^56 + 2^56 = 2^57 */
     output[1] += (output[4] & 0xffff) << 40;
     output[0] -= output[4];
 
     /* Carry 0 -> 1 -> 2 -> 3 */
     output[1] += output[0] >> 56;
     out[0] = output[0] & 0x00ffffffffffffff;
 
     output[2] += output[1] >> 56;
     /* output[2] < 2^57 + 2^72 */
     out[1] = output[1] & 0x00ffffffffffffff;
     output[3] += output[2] >> 56;
     /* output[3] <= 2^56 + 2^16 */
     out[2] = output[2] & 0x00ffffffffffffff;
 
     /*-
      * out[0] < 2^56, out[1] < 2^56, out[2] < 2^56,
      * out[3] <= 2^56 + 2^16 (due to final carry),
      * so out < 2*p
      */
     out[3] = output[3];
 }
 
 static void felem_square_reduce(felem out, const felem in)
 {
     widefelem tmp;
     felem_square(tmp, in);
     felem_reduce(out, tmp);
 }
 
 static void felem_mul_reduce(felem out, const felem in1, const felem in2)
 {
     widefelem tmp;
     felem_mul(tmp, in1, in2);
     felem_reduce(out, tmp);
 }
 
 /*
  * Reduce to unique minimal representation. Requires 0 <= in < 2*p (always
  * call felem_reduce first)
  */
 static void felem_contract(felem out, const felem in)
 {
     static const int64_t two56 = ((limb) 1) << 56;
     /* 0 <= in < 2*p, p = 2^224 - 2^96 + 1 */
     /* if in > p , reduce in = in - 2^224 + 2^96 - 1 */
     int64_t tmp[4], a;
     tmp[0] = in[0];
     tmp[1] = in[1];
     tmp[2] = in[2];
     tmp[3] = in[3];
     /* Case 1: a = 1 iff in >= 2^224 */
     a = (in[3] >> 56);
     tmp[0] -= a;
     tmp[1] += a << 40;
     tmp[3] &= 0x00ffffffffffffff;
     /*
      * Case 2: a = 0 iff p <= in < 2^224, i.e., the high 128 bits are all 1
      * and the lower part is non-zero
      */
     a = ((in[3] & in[2] & (in[1] | 0x000000ffffffffff)) + 1) |
         (((int64_t) (in[0] + (in[1] & 0x000000ffffffffff)) - 1) >> 63);
     a &= 0x00ffffffffffffff;
     /* turn a into an all-one mask (if a = 0) or an all-zero mask */
     a = (a - 1) >> 63;
     /* subtract 2^224 - 2^96 + 1 if a is all-one */
     tmp[3] &= a ^ 0xffffffffffffffff;
     tmp[2] &= a ^ 0xffffffffffffffff;
     tmp[1] &= (a ^ 0xffffffffffffffff) | 0x000000ffffffffff;
     tmp[0] -= 1 & a;
 
     /*
      * eliminate negative coefficients: if tmp[0] is negative, tmp[1] must be
      * non-zero, so we only need one step
      */
     a = tmp[0] >> 63;
     tmp[0] += two56 & a;
     tmp[1] -= 1 & a;
 
     /* carry 1 -> 2 -> 3 */
     tmp[2] += tmp[1] >> 56;
     tmp[1] &= 0x00ffffffffffffff;
 
     tmp[3] += tmp[2] >> 56;
     tmp[2] &= 0x00ffffffffffffff;
 
     /* Now 0 <= out < p */
     out[0] = tmp[0];
     out[1] = tmp[1];
     out[2] = tmp[2];
     out[3] = tmp[3];
 }
 
 /*
  * Zero-check: returns 1 if input is 0, and 0 otherwise. We know that field
  * elements are reduced to in < 2^225, so we only need to check three cases:
  * 0, 2^224 - 2^96 + 1, and 2^225 - 2^97 + 2
  */
 static limb felem_is_zero(const felem in)
 {
     limb zero, two224m96p1, two225m97p2;
 
     zero = in[0] | in[1] | in[2] | in[3];
     zero = (((int64_t) (zero) - 1) >> 63) & 1;
     two224m96p1 = (in[0] ^ 1) | (in[1] ^ 0x00ffff0000000000)
         | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x00ffffffffffffff);
     two224m96p1 = (((int64_t) (two224m96p1) - 1) >> 63) & 1;
     two225m97p2 = (in[0] ^ 2) | (in[1] ^ 0x00fffe0000000000)
         | (in[2] ^ 0x00ffffffffffffff) | (in[3] ^ 0x01ffffffffffffff);
     two225m97p2 = (((int64_t) (two225m97p2) - 1) >> 63) & 1;
     return (zero | two224m96p1 | two225m97p2);
 }
 
 static limb felem_is_zero_int(const felem in)
 {
     return (int)(felem_is_zero(in) & ((limb) 1));
 }
 
 /* Invert a field element */
 /* Computation chain copied from djb's code */
 static void felem_inv(felem out, const felem in)
 {
     felem ftmp, ftmp2, ftmp3, ftmp4;
     widefelem tmp;
     unsigned i;
 
     felem_square(tmp, in);
     felem_reduce(ftmp, tmp);    /* 2 */
     felem_mul(tmp, in, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^2 - 1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^3 - 2 */
     felem_mul(tmp, in, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^3 - 1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp2, tmp);   /* 2^4 - 2 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^5 - 4 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^6 - 8 */
     felem_mul(tmp, ftmp2, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^6 - 1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp2, tmp);   /* 2^7 - 2 */
     for (i = 0; i < 5; ++i) {   /* 2^12 - 2^6 */
         felem_square(tmp, ftmp2);
         felem_reduce(ftmp2, tmp);
     }
     felem_mul(tmp, ftmp2, ftmp);
     felem_reduce(ftmp2, tmp);   /* 2^12 - 1 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^13 - 2 */
     for (i = 0; i < 11; ++i) {  /* 2^24 - 2^12 */
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp);
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^24 - 1 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^25 - 2 */
     for (i = 0; i < 23; ++i) {  /* 2^48 - 2^24 */
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp);
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^48 - 1 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp4, tmp);   /* 2^49 - 2 */
     for (i = 0; i < 47; ++i) {  /* 2^96 - 2^48 */
         felem_square(tmp, ftmp4);
         felem_reduce(ftmp4, tmp);
     }
     felem_mul(tmp, ftmp3, ftmp4);
     felem_reduce(ftmp3, tmp);   /* 2^96 - 1 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp4, tmp);   /* 2^97 - 2 */
     for (i = 0; i < 23; ++i) {  /* 2^120 - 2^24 */
         felem_square(tmp, ftmp4);
         felem_reduce(ftmp4, tmp);
     }
     felem_mul(tmp, ftmp2, ftmp4);
     felem_reduce(ftmp2, tmp);   /* 2^120 - 1 */
     for (i = 0; i < 6; ++i) {   /* 2^126 - 2^6 */
         felem_square(tmp, ftmp2);
         felem_reduce(ftmp2, tmp);
     }
     felem_mul(tmp, ftmp2, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^126 - 1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^127 - 2 */
     felem_mul(tmp, ftmp, in);
     felem_reduce(ftmp, tmp);    /* 2^127 - 1 */
     for (i = 0; i < 97; ++i) {  /* 2^224 - 2^97 */
         felem_square(tmp, ftmp);
         felem_reduce(ftmp, tmp);
     }
     felem_mul(tmp, ftmp, ftmp3);
     felem_reduce(out, tmp);     /* 2^224 - 2^96 - 1 */
 }
 
 /*
  * Copy in constant time: if icopy == 1, copy in to out, if icopy == 0, copy
  * out to itself.
  */
 static void copy_conditional(felem out, const felem in, limb icopy)
 {
     unsigned i;
     /*
      * icopy is a (64-bit) 0 or 1, so copy is either all-zero or all-one
      */
     const limb copy = -icopy;
     for (i = 0; i < 4; ++i) {
         const limb tmp = copy & (in[i] ^ out[i]);
         out[i] ^= tmp;
     }
 }
 
 /******************************************************************************/
 /*-
  *                       ELLIPTIC CURVE POINT OPERATIONS
  *
  * Points are represented in Jacobian projective coordinates:
  * (X, Y, Z) corresponds to the affine point (X/Z^2, Y/Z^3),
  * or to the point at infinity if Z == 0.
  *
  */
 
 /*-
  * Double an elliptic curve point:
  * (X', Y', Z') = 2 * (X, Y, Z), where
  * X' = (3 * (X - Z^2) * (X + Z^2))^2 - 8 * X * Y^2
  * Y' = 3 * (X - Z^2) * (X + Z^2) * (4 * X * Y^2 - X') - 8 * Y^2
  * Z' = (Y + Z)^2 - Y^2 - Z^2 = 2 * Y * Z
  * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed,
  * while x_out == y_in is not (maybe this works, but it's not tested).
  */
 static void
 point_double(felem x_out, felem y_out, felem z_out,
              const felem x_in, const felem y_in, const felem z_in)
 {
     widefelem tmp, tmp2;
     felem delta, gamma, beta, alpha, ftmp, ftmp2;
 
     felem_assign(ftmp, x_in);
     felem_assign(ftmp2, x_in);
 
     /* delta = z^2 */
     felem_square(tmp, z_in);
     felem_reduce(delta, tmp);
 
     /* gamma = y^2 */
     felem_square(tmp, y_in);
     felem_reduce(gamma, tmp);
 
     /* beta = x*gamma */
     felem_mul(tmp, x_in, gamma);
     felem_reduce(beta, tmp);
 
     /* alpha = 3*(x-delta)*(x+delta) */
     felem_diff(ftmp, delta);
     /* ftmp[i] < 2^57 + 2^58 + 2 < 2^59 */
     felem_sum(ftmp2, delta);
     /* ftmp2[i] < 2^57 + 2^57 = 2^58 */
     felem_scalar(ftmp2, 3);
     /* ftmp2[i] < 3 * 2^58 < 2^60 */
     felem_mul(tmp, ftmp, ftmp2);
     /* tmp[i] < 2^60 * 2^59 * 4 = 2^121 */
     felem_reduce(alpha, tmp);
 
     /* x' = alpha^2 - 8*beta */
     felem_square(tmp, alpha);
     /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
     felem_assign(ftmp, beta);
     felem_scalar(ftmp, 8);
     /* ftmp[i] < 8 * 2^57 = 2^60 */
     felem_diff_128_64(tmp, ftmp);
     /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
     felem_reduce(x_out, tmp);
 
     /* z' = (y + z)^2 - gamma - delta */
     felem_sum(delta, gamma);
     /* delta[i] < 2^57 + 2^57 = 2^58 */
     felem_assign(ftmp, y_in);
     felem_sum(ftmp, z_in);
     /* ftmp[i] < 2^57 + 2^57 = 2^58 */
     felem_square(tmp, ftmp);
     /* tmp[i] < 4 * 2^58 * 2^58 = 2^118 */
     felem_diff_128_64(tmp, delta);
     /* tmp[i] < 2^118 + 2^64 + 8 < 2^119 */
     felem_reduce(z_out, tmp);
 
     /* y' = alpha*(4*beta - x') - 8*gamma^2 */
     felem_scalar(beta, 4);
     /* beta[i] < 4 * 2^57 = 2^59 */
     felem_diff(beta, x_out);
     /* beta[i] < 2^59 + 2^58 + 2 < 2^60 */
     felem_mul(tmp, alpha, beta);
     /* tmp[i] < 4 * 2^57 * 2^60 = 2^119 */
     felem_square(tmp2, gamma);
     /* tmp2[i] < 4 * 2^57 * 2^57 = 2^116 */
     widefelem_scalar(tmp2, 8);
     /* tmp2[i] < 8 * 2^116 = 2^119 */
     widefelem_diff(tmp, tmp2);
     /* tmp[i] < 2^119 + 2^120 < 2^121 */
     felem_reduce(y_out, tmp);
 }
 
 /*-
  * Add two elliptic curve points:
  * (X_1, Y_1, Z_1) + (X_2, Y_2, Z_2) = (X_3, Y_3, Z_3), where
  * X_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1)^2 - (Z_1^2 * X_2 - Z_2^2 * X_1)^3 -
  * 2 * Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2
  * Y_3 = (Z_1^3 * Y_2 - Z_2^3 * Y_1) * (Z_2^2 * X_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^2 - X_3) -
  *        Z_2^3 * Y_1 * (Z_1^2 * X_2 - Z_2^2 * X_1)^3
  * Z_3 = (Z_1^2 * X_2 - Z_2^2 * X_1) * (Z_1 * Z_2)
  *
  * This runs faster if 'mixed' is set, which requires Z_2 = 1 or Z_2 = 0.
  */
 
 /*
  * This function is not entirely constant-time: it includes a branch for
  * checking whether the two input points are equal, (while not equal to the
  * point at infinity). This case never happens during single point
  * multiplication, so there is no timing leak for ECDH or ECDSA signing.
  */
 static void point_add(felem x3, felem y3, felem z3,
                       const felem x1, const felem y1, const felem z1,
                       const int mixed, const felem x2, const felem y2,
                       const felem z2)
 {
     felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, x_out, y_out, z_out;
     widefelem tmp, tmp2;
     limb z1_is_zero, z2_is_zero, x_equal, y_equal;
 
     if (!mixed) {
         /* ftmp2 = z2^2 */
         felem_square(tmp, z2);
         felem_reduce(ftmp2, tmp);
 
         /* ftmp4 = z2^3 */
         felem_mul(tmp, ftmp2, z2);
         felem_reduce(ftmp4, tmp);
 
         /* ftmp4 = z2^3*y1 */
         felem_mul(tmp2, ftmp4, y1);
         felem_reduce(ftmp4, tmp2);
 
         /* ftmp2 = z2^2*x1 */
         felem_mul(tmp2, ftmp2, x1);
         felem_reduce(ftmp2, tmp2);
     } else {
         /*
          * We'll assume z2 = 1 (special case z2 = 0 is handled later)
          */
 
         /* ftmp4 = z2^3*y1 */
         felem_assign(ftmp4, y1);
 
         /* ftmp2 = z2^2*x1 */
         felem_assign(ftmp2, x1);
     }
 
     /* ftmp = z1^2 */
     felem_square(tmp, z1);
     felem_reduce(ftmp, tmp);
 
     /* ftmp3 = z1^3 */
     felem_mul(tmp, ftmp, z1);
     felem_reduce(ftmp3, tmp);
 
     /* tmp = z1^3*y2 */
     felem_mul(tmp, ftmp3, y2);
     /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
 
     /* ftmp3 = z1^3*y2 - z2^3*y1 */
     felem_diff_128_64(tmp, ftmp4);
     /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
     felem_reduce(ftmp3, tmp);
 
     /* tmp = z1^2*x2 */
     felem_mul(tmp, ftmp, x2);
     /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
 
     /* ftmp = z1^2*x2 - z2^2*x1 */
     felem_diff_128_64(tmp, ftmp2);
     /* tmp[i] < 2^116 + 2^64 + 8 < 2^117 */
     felem_reduce(ftmp, tmp);
 
     /*
      * the formulae are incorrect if the points are equal so we check for
      * this and do doubling if this happens
      */
     x_equal = felem_is_zero(ftmp);
     y_equal = felem_is_zero(ftmp3);
     z1_is_zero = felem_is_zero(z1);
     z2_is_zero = felem_is_zero(z2);
     /* In affine coordinates, (X_1, Y_1) == (X_2, Y_2) */
     if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
         point_double(x3, y3, z3, x1, y1, z1);
         return;
     }
 
     /* ftmp5 = z1*z2 */
     if (!mixed) {
         felem_mul(tmp, z1, z2);
         felem_reduce(ftmp5, tmp);
     } else {
         /* special case z2 = 0 is handled later */
         felem_assign(ftmp5, z1);
     }
 
     /* z_out = (z1^2*x2 - z2^2*x1)*(z1*z2) */
     felem_mul(tmp, ftmp, ftmp5);
     felem_reduce(z_out, tmp);
 
     /* ftmp = (z1^2*x2 - z2^2*x1)^2 */
     felem_assign(ftmp5, ftmp);
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);
 
     /* ftmp5 = (z1^2*x2 - z2^2*x1)^3 */
     felem_mul(tmp, ftmp, ftmp5);
     felem_reduce(ftmp5, tmp);
 
     /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
     felem_mul(tmp, ftmp2, ftmp);
     felem_reduce(ftmp2, tmp);
 
     /* tmp = z2^3*y1*(z1^2*x2 - z2^2*x1)^3 */
     felem_mul(tmp, ftmp4, ftmp5);
     /* tmp[i] < 4 * 2^57 * 2^57 = 2^116 */
 
     /* tmp2 = (z1^3*y2 - z2^3*y1)^2 */
     felem_square(tmp2, ftmp3);
     /* tmp2[i] < 4 * 2^57 * 2^57 < 2^116 */
 
     /* tmp2 = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 */
     felem_diff_128_64(tmp2, ftmp5);
     /* tmp2[i] < 2^116 + 2^64 + 8 < 2^117 */
 
     /* ftmp5 = 2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2 */
     felem_assign(ftmp5, ftmp2);
     felem_scalar(ftmp5, 2);
     /* ftmp5[i] < 2 * 2^57 = 2^58 */
 
     /*-
      * x_out = (z1^3*y2 - z2^3*y1)^2 - (z1^2*x2 - z2^2*x1)^3 -
      *  2*z2^2*x1*(z1^2*x2 - z2^2*x1)^2
      */
     felem_diff_128_64(tmp2, ftmp5);
     /* tmp2[i] < 2^117 + 2^64 + 8 < 2^118 */
     felem_reduce(x_out, tmp2);
 
     /* ftmp2 = z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out */
     felem_diff(ftmp2, x_out);
     /* ftmp2[i] < 2^57 + 2^58 + 2 < 2^59 */
 
     /*
      * tmp2 = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out)
      */
     felem_mul(tmp2, ftmp3, ftmp2);
     /* tmp2[i] < 4 * 2^57 * 2^59 = 2^118 */
 
     /*-
      * y_out = (z1^3*y2 - z2^3*y1)*(z2^2*x1*(z1^2*x2 - z2^2*x1)^2 - x_out) -
      *  z2^3*y1*(z1^2*x2 - z2^2*x1)^3
      */
     widefelem_diff(tmp2, tmp);
     /* tmp2[i] < 2^118 + 2^120 < 2^121 */
     felem_reduce(y_out, tmp2);
 
     /*
      * the result (x_out, y_out, z_out) is incorrect if one of the inputs is
      * the point at infinity, so we need to check for this separately
      */
 
     /*
      * if point 1 is at infinity, copy point 2 to output, and vice versa
      */
     copy_conditional(x_out, x2, z1_is_zero);
     copy_conditional(x_out, x1, z2_is_zero);
     copy_conditional(y_out, y2, z1_is_zero);
     copy_conditional(y_out, y1, z2_is_zero);
     copy_conditional(z_out, z2, z1_is_zero);
     copy_conditional(z_out, z1, z2_is_zero);
     felem_assign(x3, x_out);
     felem_assign(y3, y_out);
     felem_assign(z3, z_out);
 }
 
 /*
  * select_point selects the |idx|th point from a precomputation table and
  * copies it to out.
  * The pre_comp array argument should be size of |size| argument
  */
 static void select_point(const u64 idx, unsigned int size,
                          const felem pre_comp[][3], felem out[3])
 {
     unsigned i, j;
     limb *outlimbs = &out[0][0];
     memset(outlimbs, 0, 3 * sizeof(felem));
 
     for (i = 0; i < size; i++) {
         const limb *inlimbs = &pre_comp[i][0][0];
         u64 mask = i ^ idx;
         mask |= mask >> 4;
         mask |= mask >> 2;
         mask |= mask >> 1;
         mask &= 1;
         mask--;
         for (j = 0; j < 4 * 3; j++)
             outlimbs[j] |= inlimbs[j] & mask;
     }
 }
 
 /* get_bit returns the |i|th bit in |in| */
 static char get_bit(const felem_bytearray in, unsigned i)
 {
     if (i >= 224)
         return 0;
     return (in[i >> 3] >> (i & 7)) & 1;
 }
 
 /*
  * Interleaved point multiplication using precomputed point multiples: The
  * small point multiples 0*P, 1*P, ..., 16*P are in pre_comp[], the scalars
  * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  * generator, using certain (large) precomputed multiples in g_pre_comp.
  * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  */
 static void batch_mul(felem x_out, felem y_out, felem z_out,
                       const felem_bytearray scalars[],
                       const unsigned num_points, const u8 *g_scalar,
                       const int mixed, const felem pre_comp[][17][3],
                       const felem g_pre_comp[2][16][3])
 {
     int i, skip;
     unsigned num;
     unsigned gen_mul = (g_scalar != NULL);
     felem nq[3], tmp[4];
     u64 bits;
     u8 sign, digit;
 
     /* set nq to the point at infinity */
     memset(nq, 0, 3 * sizeof(felem));
 
     /*
      * Loop over all scalars msb-to-lsb, interleaving additions of multiples
      * of the generator (two in each of the last 28 rounds) and additions of
      * other points multiples (every 5th round).
      */
     skip = 1;                   /* save two point operations in the first
                                  * round */
     for (i = (num_points ? 220 : 27); i >= 0; --i) {
         /* double */
         if (!skip)
             point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
 
         /* add multiples of the generator */
         if (gen_mul && (i <= 27)) {
             /* first, look 28 bits upwards */
             bits = get_bit(g_scalar, i + 196) << 3;
             bits |= get_bit(g_scalar, i + 140) << 2;
             bits |= get_bit(g_scalar, i + 84) << 1;
             bits |= get_bit(g_scalar, i + 28);
             /* select the point to add, in constant time */
             select_point(bits, 16, g_pre_comp[1], tmp);
 
             if (!skip) {
                 /* value 1 below is argument for "mixed" */
                 point_add(nq[0], nq[1], nq[2],
                           nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
             } else {
                 memcpy(nq, tmp, 3 * sizeof(felem));
                 skip = 0;
             }
 
             /* second, look at the current position */
             bits = get_bit(g_scalar, i + 168) << 3;
             bits |= get_bit(g_scalar, i + 112) << 2;
             bits |= get_bit(g_scalar, i + 56) << 1;
             bits |= get_bit(g_scalar, i);
             /* select the point to add, in constant time */
             select_point(bits, 16, g_pre_comp[0], tmp);
             point_add(nq[0], nq[1], nq[2],
                       nq[0], nq[1], nq[2],
                       1 /* mixed */ , tmp[0], tmp[1], tmp[2]);
         }
 
         /* do other additions every 5 doublings */
         if (num_points && (i % 5 == 0)) {
             /* loop over all scalars */
             for (num = 0; num < num_points; ++num) {
                 bits = get_bit(scalars[num], i + 4) << 5;
                 bits |= get_bit(scalars[num], i + 3) << 4;
                 bits |= get_bit(scalars[num], i + 2) << 3;
                 bits |= get_bit(scalars[num], i + 1) << 2;
                 bits |= get_bit(scalars[num], i) << 1;
                 bits |= get_bit(scalars[num], i - 1);
                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
 
                 /* select the point to add or subtract */
                 select_point(digit, 17, pre_comp[num], tmp);
                 felem_neg(tmp[3], tmp[1]); /* (X, -Y, Z) is the negative
                                             * point */
                 copy_conditional(tmp[1], tmp[3], sign);
 
                 if (!skip) {
                     point_add(nq[0], nq[1], nq[2],
                               nq[0], nq[1], nq[2],
                               mixed, tmp[0], tmp[1], tmp[2]);
                 } else {
                     memcpy(nq, tmp, 3 * sizeof(felem));
                     skip = 0;
                 }
             }
         }
     }
     felem_assign(x_out, nq[0]);
     felem_assign(y_out, nq[1]);
     felem_assign(z_out, nq[2]);
 }
 
 /******************************************************************************/
 /*
  * FUNCTIONS TO MANAGE PRECOMPUTATION
  */
 
 static NISTP224_PRE_COMP *nistp224_pre_comp_new()
 {
     NISTP224_PRE_COMP *ret = NULL;
     ret = (NISTP224_PRE_COMP *) OPENSSL_malloc(sizeof *ret);
     if (!ret) {
         ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
         return ret;
     }
     memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
     ret->references = 1;
     return ret;
 }
 
 static void *nistp224_pre_comp_dup(void *src_)
 {
     NISTP224_PRE_COMP *src = src_;
 
     /* no need to actually copy, these objects never change! */
     CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
 
     return src_;
 }
 
 static void nistp224_pre_comp_free(void *pre_)
 {
     int i;
     NISTP224_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_free(pre);
 }
 
 static void nistp224_pre_comp_clear_free(void *pre_)
 {
     int i;
     NISTP224_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_cleanse(pre, sizeof *pre);
     OPENSSL_free(pre);
 }
 
 /******************************************************************************/
 /*
  * OPENSSL EC_METHOD FUNCTIONS
  */
 
 int ec_GFp_nistp224_group_init(EC_GROUP *group)
 {
     int ret;
     ret = ec_GFp_simple_group_init(group);
     group->a_is_minus3 = 1;
     return ret;
 }
 
 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
                                     const BIGNUM *a, const BIGNUM *b,
                                     BN_CTX *ctx)
 {
     int ret = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *curve_p, *curve_a, *curve_b;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
         ((curve_a = BN_CTX_get(ctx)) == NULL) ||
         ((curve_b = BN_CTX_get(ctx)) == NULL))
         goto err;
     BN_bin2bn(nistp224_curve_params[0], sizeof(felem_bytearray), curve_p);
     BN_bin2bn(nistp224_curve_params[1], sizeof(felem_bytearray), curve_a);
     BN_bin2bn(nistp224_curve_params[2], sizeof(felem_bytearray), curve_b);
     if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
         ECerr(EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE,
               EC_R_WRONG_CURVE_PARAMETERS);
         goto err;
     }
     group->field_mod_func = BN_nist_mod_224;
     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  err:
     BN_CTX_end(ctx);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     return ret;
 }
 
 /*
  * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  * (X/Z^2, Y/Z^3)
  */
 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
                                                  const EC_POINT *point,
                                                  BIGNUM *x, BIGNUM *y,
                                                  BN_CTX *ctx)
 {
     felem z1, z2, x_in, y_in, x_out, y_out;
     widefelem tmp;
 
     if (EC_POINT_is_at_infinity(group, point)) {
         ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
               EC_R_POINT_AT_INFINITY);
         return 0;
     }
     if ((!BN_to_felem(x_in, &point->X)) || (!BN_to_felem(y_in, &point->Y)) ||
         (!BN_to_felem(z1, &point->Z)))
         return 0;
     felem_inv(z2, z1);
     felem_square(tmp, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, x_in, z1);
     felem_reduce(x_in, tmp);
     felem_contract(x_out, x_in);
     if (x != NULL) {
         if (!felem_to_BN(x, x_out)) {
             ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     felem_mul(tmp, z1, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, y_in, z1);
     felem_reduce(y_in, tmp);
     felem_contract(y_out, y_in);
     if (y != NULL) {
         if (!felem_to_BN(y, y_out)) {
             ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     return 1;
 }
 
 static void make_points_affine(size_t num, felem points[ /* num */ ][3],
                                felem tmp_felems[ /* num+1 */ ])
 {
     /*
      * Runs in constant time, unless an input is the point at infinity (which
      * normally shouldn't happen).
      */
     ec_GFp_nistp_points_make_affine_internal(num,
                                              points,
                                              sizeof(felem),
                                              tmp_felems,
                                              (void (*)(void *))felem_one,
                                              (int (*)(const void *))
                                              felem_is_zero_int,
                                              (void (*)(void *, const void *))
                                              felem_assign,
                                              (void (*)(void *, const void *))
                                              felem_square_reduce, (void (*)
                                                                    (void *,
                                                                     const void
                                                                     *,
                                                                     const void
                                                                     *))
                                              felem_mul_reduce,
                                              (void (*)(void *, const void *))
                                              felem_inv,
                                              (void (*)(void *, const void *))
                                              felem_contract);
 }
 
 /*
  * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  * values Result is stored in r (r can equal one of the inputs).
  */
 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
                                const BIGNUM *scalar, size_t num,
                                const EC_POINT *points[],
                                const BIGNUM *scalars[], BN_CTX *ctx)
 {
     int ret = 0;
     int j;
     unsigned i;
     int mixed = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y, *z, *tmp_scalar;
     felem_bytearray g_secret;
     felem_bytearray *secrets = NULL;
     felem(*pre_comp)[17][3] = NULL;
     felem *tmp_felems = NULL;
     felem_bytearray tmp;
     unsigned num_bytes;
     int have_pre_comp = 0;
     size_t num_points = num;
     felem x_in, y_in, z_in, x_out, y_out, z_out;
     NISTP224_PRE_COMP *pre = NULL;
     const felem(*g_pre_comp)[16][3] = NULL;
     EC_POINT *generator = NULL;
     const EC_POINT *p = NULL;
     const BIGNUM *p_scalar = NULL;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) ||
         ((y = BN_CTX_get(ctx)) == NULL) ||
         ((z = BN_CTX_get(ctx)) == NULL) ||
         ((tmp_scalar = BN_CTX_get(ctx)) == NULL))
         goto err;
 
     if (scalar != NULL) {
         pre = EC_EX_DATA_get_data(group->extra_data,
                                   nistp224_pre_comp_dup,
                                   nistp224_pre_comp_free,
                                   nistp224_pre_comp_clear_free);
         if (pre)
             /* we have precomputation, try to use it */
             g_pre_comp = (const felem(*)[16][3])pre->g_pre_comp;
         else
             /* try to use the standard precomputation */
             g_pre_comp = &gmul[0];
         generator = EC_POINT_new(group);
         if (generator == NULL)
             goto err;
         /* get the generator from precomputation */
         if (!felem_to_BN(x, g_pre_comp[0][1][0]) ||
             !felem_to_BN(y, g_pre_comp[0][1][1]) ||
             !felem_to_BN(z, g_pre_comp[0][1][2])) {
             ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
             goto err;
         }
         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
                                                       generator, x, y, z,
                                                       ctx))
             goto err;
         if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
             /* precomputation matches generator */
             have_pre_comp = 1;
         else
             /*
              * we don't have valid precomputation: treat the generator as a
              * random point
              */
             num_points = num_points + 1;
     }
 
     if (num_points > 0) {
         if (num_points >= 3) {
             /*
              * unless we precompute multiples for just one or two points,
              * converting those into affine form is time well spent
              */
             mixed = 1;
         }
         secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
         pre_comp = OPENSSL_malloc(num_points * 17 * 3 * sizeof(felem));
         if (mixed)
             tmp_felems =
                 OPENSSL_malloc((num_points * 17 + 1) * sizeof(felem));
         if ((secrets == NULL) || (pre_comp == NULL)
             || (mixed && (tmp_felems == NULL))) {
             ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_MALLOC_FAILURE);
             goto err;
         }
 
         /*
          * we treat NULL scalars as 0, and NULL points as points at infinity,
          * i.e., they contribute nothing to the linear combination
          */
         memset(secrets, 0, num_points * sizeof(felem_bytearray));
         memset(pre_comp, 0, num_points * 17 * 3 * sizeof(felem));
         for (i = 0; i < num_points; ++i) {
             if (i == num)
                 /* the generator */
             {
                 p = EC_GROUP_get0_generator(group);
                 p_scalar = scalar;
             } else
                 /* the i^th point */
             {
                 p = points[i];
                 p_scalar = scalars[i];
             }
             if ((p_scalar != NULL) && (p != NULL)) {
                 /* reduce scalar to 0 <= scalar < 2^224 */
                 if ((BN_num_bits(p_scalar) > 224)
                     || (BN_is_negative(p_scalar))) {
                     /*
                      * this is an unusual input, and we don't guarantee
                      * constant-timeness
                      */
                     if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
                         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
                         goto err;
                     }
                     num_bytes = BN_bn2bin(tmp_scalar, tmp);
                 } else
                     num_bytes = BN_bn2bin(p_scalar, tmp);
                 flip_endian(secrets[i], tmp, num_bytes);
                 /* precompute multiples */
                 if ((!BN_to_felem(x_out, &p->X)) ||
                     (!BN_to_felem(y_out, &p->Y)) ||
                     (!BN_to_felem(z_out, &p->Z)))
                     goto err;
                 felem_assign(pre_comp[i][1][0], x_out);
                 felem_assign(pre_comp[i][1][1], y_out);
                 felem_assign(pre_comp[i][1][2], z_out);
                 for (j = 2; j <= 16; ++j) {
                     if (j & 1) {
                         point_add(pre_comp[i][j][0], pre_comp[i][j][1],
                                   pre_comp[i][j][2], pre_comp[i][1][0],
                                   pre_comp[i][1][1], pre_comp[i][1][2], 0,
                                   pre_comp[i][j - 1][0],
                                   pre_comp[i][j - 1][1],
                                   pre_comp[i][j - 1][2]);
                     } else {
                         point_double(pre_comp[i][j][0], pre_comp[i][j][1],
                                      pre_comp[i][j][2], pre_comp[i][j / 2][0],
                                      pre_comp[i][j / 2][1],
                                      pre_comp[i][j / 2][2]);
                     }
                 }
             }
         }
         if (mixed)
             make_points_affine(num_points * 17, pre_comp[0], tmp_felems);
     }
 
     /* the scalar for the generator */
     if ((scalar != NULL) && (have_pre_comp)) {
         memset(g_secret, 0, sizeof g_secret);
         /* reduce scalar to 0 <= scalar < 2^224 */
         if ((BN_num_bits(scalar) > 224) || (BN_is_negative(scalar))) {
             /*
              * this is an unusual input, and we don't guarantee
              * constant-timeness
              */
             if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
                 ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
                 goto err;
             }
             num_bytes = BN_bn2bin(tmp_scalar, tmp);
         } else
             num_bytes = BN_bn2bin(scalar, tmp);
         flip_endian(g_secret, tmp, num_bytes);
         /* do the multiplication with generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   g_secret,
                   mixed, (const felem(*)[17][3])pre_comp, g_pre_comp);
     } else
         /* do the multiplication without generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   NULL, mixed, (const felem(*)[17][3])pre_comp, NULL);
     /* reduce the output to its unique minimal representation */
     felem_contract(x_in, x_out);
     felem_contract(y_in, y_out);
     felem_contract(z_in, z_out);
     if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
         (!felem_to_BN(z, z_in))) {
         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
         goto err;
     }
     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
 
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (secrets != NULL)
         OPENSSL_free(secrets);
     if (pre_comp != NULL)
         OPENSSL_free(pre_comp);
     if (tmp_felems != NULL)
         OPENSSL_free(tmp_felems);
     return ret;
 }
 
 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
 {
     int ret = 0;
     NISTP224_PRE_COMP *pre = NULL;
     int i, j;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y;
     EC_POINT *generator = NULL;
     felem tmp_felems[32];
 
     /* throw away old precomputation */
     EC_EX_DATA_free_data(&group->extra_data, nistp224_pre_comp_dup,
                          nistp224_pre_comp_free,
                          nistp224_pre_comp_clear_free);
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) || ((y = BN_CTX_get(ctx)) == NULL))
         goto err;
     /* get the generator */
     if (group->generator == NULL)
         goto err;
     generator = EC_POINT_new(group);
     if (generator == NULL)
         goto err;
     BN_bin2bn(nistp224_curve_params[3], sizeof(felem_bytearray), x);
     BN_bin2bn(nistp224_curve_params[4], sizeof(felem_bytearray), y);
     if (!EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx))
         goto err;
     if ((pre = nistp224_pre_comp_new()) == NULL)
         goto err;
     /*
      * if the generator is the standard one, use built-in precomputation
      */
     if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
         memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
-        ret = 1;
-        goto err;
+        goto done;
     }
     if ((!BN_to_felem(pre->g_pre_comp[0][1][0], &group->generator->X)) ||
         (!BN_to_felem(pre->g_pre_comp[0][1][1], &group->generator->Y)) ||
         (!BN_to_felem(pre->g_pre_comp[0][1][2], &group->generator->Z)))
         goto err;
     /*
      * compute 2^56*G, 2^112*G, 2^168*G for the first table, 2^28*G, 2^84*G,
      * 2^140*G, 2^196*G for the second one
      */
     for (i = 1; i <= 8; i <<= 1) {
         point_double(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
                      pre->g_pre_comp[1][i][2], pre->g_pre_comp[0][i][0],
                      pre->g_pre_comp[0][i][1], pre->g_pre_comp[0][i][2]);
         for (j = 0; j < 27; ++j) {
             point_double(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
                          pre->g_pre_comp[1][i][2], pre->g_pre_comp[1][i][0],
                          pre->g_pre_comp[1][i][1], pre->g_pre_comp[1][i][2]);
         }
         if (i == 8)
             break;
         point_double(pre->g_pre_comp[0][2 * i][0],
                      pre->g_pre_comp[0][2 * i][1],
                      pre->g_pre_comp[0][2 * i][2], pre->g_pre_comp[1][i][0],
                      pre->g_pre_comp[1][i][1], pre->g_pre_comp[1][i][2]);
         for (j = 0; j < 27; ++j) {
             point_double(pre->g_pre_comp[0][2 * i][0],
                          pre->g_pre_comp[0][2 * i][1],
                          pre->g_pre_comp[0][2 * i][2],
                          pre->g_pre_comp[0][2 * i][0],
                          pre->g_pre_comp[0][2 * i][1],
                          pre->g_pre_comp[0][2 * i][2]);
         }
     }
     for (i = 0; i < 2; i++) {
         /* g_pre_comp[i][0] is the point at infinity */
         memset(pre->g_pre_comp[i][0], 0, sizeof(pre->g_pre_comp[i][0]));
         /* the remaining multiples */
         /* 2^56*G + 2^112*G resp. 2^84*G + 2^140*G */
         point_add(pre->g_pre_comp[i][6][0], pre->g_pre_comp[i][6][1],
                   pre->g_pre_comp[i][6][2], pre->g_pre_comp[i][4][0],
                   pre->g_pre_comp[i][4][1], pre->g_pre_comp[i][4][2],
                   0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                   pre->g_pre_comp[i][2][2]);
         /* 2^56*G + 2^168*G resp. 2^84*G + 2^196*G */
         point_add(pre->g_pre_comp[i][10][0], pre->g_pre_comp[i][10][1],
                   pre->g_pre_comp[i][10][2], pre->g_pre_comp[i][8][0],
                   pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
                   0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                   pre->g_pre_comp[i][2][2]);
         /* 2^112*G + 2^168*G resp. 2^140*G + 2^196*G */
         point_add(pre->g_pre_comp[i][12][0], pre->g_pre_comp[i][12][1],
                   pre->g_pre_comp[i][12][2], pre->g_pre_comp[i][8][0],
                   pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
                   0, pre->g_pre_comp[i][4][0], pre->g_pre_comp[i][4][1],
                   pre->g_pre_comp[i][4][2]);
         /*
          * 2^56*G + 2^112*G + 2^168*G resp. 2^84*G + 2^140*G + 2^196*G
          */
         point_add(pre->g_pre_comp[i][14][0], pre->g_pre_comp[i][14][1],
                   pre->g_pre_comp[i][14][2], pre->g_pre_comp[i][12][0],
                   pre->g_pre_comp[i][12][1], pre->g_pre_comp[i][12][2],
                   0, pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                   pre->g_pre_comp[i][2][2]);
         for (j = 1; j < 8; ++j) {
             /* odd multiples: add G resp. 2^28*G */
             point_add(pre->g_pre_comp[i][2 * j + 1][0],
                       pre->g_pre_comp[i][2 * j + 1][1],
                       pre->g_pre_comp[i][2 * j + 1][2],
                       pre->g_pre_comp[i][2 * j][0],
                       pre->g_pre_comp[i][2 * j][1],
                       pre->g_pre_comp[i][2 * j][2], 0,
                       pre->g_pre_comp[i][1][0], pre->g_pre_comp[i][1][1],
                       pre->g_pre_comp[i][1][2]);
         }
     }
     make_points_affine(31, &(pre->g_pre_comp[0][1]), tmp_felems);
 
+ done:
     if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp224_pre_comp_dup,
                              nistp224_pre_comp_free,
                              nistp224_pre_comp_clear_free))
         goto err;
     ret = 1;
     pre = NULL;
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (pre)
         nistp224_pre_comp_free(pre);
     return ret;
 }
 
 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group)
 {
     if (EC_EX_DATA_get_data(group->extra_data, nistp224_pre_comp_dup,
                             nistp224_pre_comp_free,
                             nistp224_pre_comp_clear_free)
         != NULL)
         return 1;
     else
         return 0;
 }
 
 #else
 static void *dummy = &dummy;
 #endif
Index: vendor-crypto/openssl/dist/crypto/ec/ecp_nistp256.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/ec/ecp_nistp256.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/ec/ecp_nistp256.c	(revision 296273)
@@ -1,2369 +1,2369 @@
 /* crypto/ec/ecp_nistp256.c */
 /*
  * Written by Adam Langley (Google) for the OpenSSL project
  */
 /* Copyright 2011 Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  *
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 /*
  * A 64-bit implementation of the NIST P-256 elliptic curve point multiplication
  *
  * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
  * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
  * work which got its smarts from Daniel J. Bernstein's work on the same.
  */
 
 #include 
 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
 
 # ifndef OPENSSL_SYS_VMS
 #  include 
 # else
 #  include 
 # endif
 
 # include 
 # include 
 # include "ec_lcl.h"
 
 # if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
   /* even with gcc, the typedef won't work for 32-bit platforms */
 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
                                  * platforms */
 typedef __int128_t int128_t;
 # else
 #  error "Need GCC 3.1 or later to define type uint128_t"
 # endif
 
 typedef uint8_t u8;
 typedef uint32_t u32;
 typedef uint64_t u64;
 typedef int64_t s64;
 
 /*
  * The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
  * can serialise an element of this field into 32 bytes. We call this an
  * felem_bytearray.
  */
 
 typedef u8 felem_bytearray[32];
 
 /*
  * These are the parameters of P256, taken from FIPS 186-3, page 86. These
  * values are big-endian.
  */
 static const felem_bytearray nistp256_curve_params[5] = {
     {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* p */
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
     {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, /* a = -3 */
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc}, /* b */
     {0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
      0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
      0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
      0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b},
     {0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, /* x */
      0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
      0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
      0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96},
     {0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, /* y */
      0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
      0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
      0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5}
 };
 
 /*-
  * The representation of field elements.
  * ------------------------------------
  *
  * We represent field elements with either four 128-bit values, eight 128-bit
  * values, or four 64-bit values. The field element represented is:
  *   v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192  (mod p)
  * or:
  *   v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512  (mod p)
  *
  * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
  * apart, but are 128-bits wide, the most significant bits of each limb overlap
  * with the least significant bits of the next.
  *
  * A field element with four limbs is an 'felem'. One with eight limbs is a
  * 'longfelem'
  *
  * A field element with four, 64-bit values is called a 'smallfelem'. Small
  * values are used as intermediate values before multiplication.
  */
 
 # define NLIMBS 4
 
 typedef uint128_t limb;
 typedef limb felem[NLIMBS];
 typedef limb longfelem[NLIMBS * 2];
 typedef u64 smallfelem[NLIMBS];
 
 /* This is the value of the prime as four 64-bit words, little-endian. */
 static const u64 kPrime[4] =
     { 0xfffffffffffffffful, 0xffffffff, 0, 0xffffffff00000001ul };
 static const u64 bottom63bits = 0x7ffffffffffffffful;
 
 /*
  * bin32_to_felem takes a little-endian byte array and converts it into felem
  * form. This assumes that the CPU is little-endian.
  */
 static void bin32_to_felem(felem out, const u8 in[32])
 {
     out[0] = *((u64 *)&in[0]);
     out[1] = *((u64 *)&in[8]);
     out[2] = *((u64 *)&in[16]);
     out[3] = *((u64 *)&in[24]);
 }
 
 /*
  * smallfelem_to_bin32 takes a smallfelem and serialises into a little
  * endian, 32 byte array. This assumes that the CPU is little-endian.
  */
 static void smallfelem_to_bin32(u8 out[32], const smallfelem in)
 {
     *((u64 *)&out[0]) = in[0];
     *((u64 *)&out[8]) = in[1];
     *((u64 *)&out[16]) = in[2];
     *((u64 *)&out[24]) = in[3];
 }
 
 /* To preserve endianness when using BN_bn2bin and BN_bin2bn */
 static void flip_endian(u8 *out, const u8 *in, unsigned len)
 {
     unsigned i;
     for (i = 0; i < len; ++i)
         out[i] = in[len - 1 - i];
 }
 
 /* BN_to_felem converts an OpenSSL BIGNUM into an felem */
 static int BN_to_felem(felem out, const BIGNUM *bn)
 {
     felem_bytearray b_in;
     felem_bytearray b_out;
     unsigned num_bytes;
 
     /* BN_bn2bin eats leading zeroes */
     memset(b_out, 0, sizeof b_out);
     num_bytes = BN_num_bytes(bn);
     if (num_bytes > sizeof b_out) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     if (BN_is_negative(bn)) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     num_bytes = BN_bn2bin(bn, b_in);
     flip_endian(b_out, b_in, num_bytes);
     bin32_to_felem(out, b_out);
     return 1;
 }
 
 /* felem_to_BN converts an felem into an OpenSSL BIGNUM */
 static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in)
 {
     felem_bytearray b_in, b_out;
     smallfelem_to_bin32(b_in, in);
     flip_endian(b_out, b_in, sizeof b_out);
     return BN_bin2bn(b_out, sizeof b_out, out);
 }
 
 /*-
  * Field operations
  * ----------------
  */
 
 static void smallfelem_one(smallfelem out)
 {
     out[0] = 1;
     out[1] = 0;
     out[2] = 0;
     out[3] = 0;
 }
 
 static void smallfelem_assign(smallfelem out, const smallfelem in)
 {
     out[0] = in[0];
     out[1] = in[1];
     out[2] = in[2];
     out[3] = in[3];
 }
 
 static void felem_assign(felem out, const felem in)
 {
     out[0] = in[0];
     out[1] = in[1];
     out[2] = in[2];
     out[3] = in[3];
 }
 
 /* felem_sum sets out = out + in. */
 static void felem_sum(felem out, const felem in)
 {
     out[0] += in[0];
     out[1] += in[1];
     out[2] += in[2];
     out[3] += in[3];
 }
 
 /* felem_small_sum sets out = out + in. */
 static void felem_small_sum(felem out, const smallfelem in)
 {
     out[0] += in[0];
     out[1] += in[1];
     out[2] += in[2];
     out[3] += in[3];
 }
 
 /* felem_scalar sets out = out * scalar */
 static void felem_scalar(felem out, const u64 scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
 }
 
 /* longfelem_scalar sets out = out * scalar */
 static void longfelem_scalar(longfelem out, const u64 scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
     out[4] *= scalar;
     out[5] *= scalar;
     out[6] *= scalar;
     out[7] *= scalar;
 }
 
 # define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
 # define two105 (((limb)1) << 105)
 # define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
 
 /* zero105 is 0 mod p */
 static const felem zero105 =
     { two105m41m9, two105, two105m41p9, two105m41p9 };
 
 /*-
  * smallfelem_neg sets |out| to |-small|
  * On exit:
  *   out[i] < out[i] + 2^105
  */
 static void smallfelem_neg(felem out, const smallfelem small)
 {
     /* In order to prevent underflow, we subtract from 0 mod p. */
     out[0] = zero105[0] - small[0];
     out[1] = zero105[1] - small[1];
     out[2] = zero105[2] - small[2];
     out[3] = zero105[3] - small[3];
 }
 
 /*-
  * felem_diff subtracts |in| from |out|
  * On entry:
  *   in[i] < 2^104
  * On exit:
  *   out[i] < out[i] + 2^105
  */
 static void felem_diff(felem out, const felem in)
 {
     /*
      * In order to prevent underflow, we add 0 mod p before subtracting.
      */
     out[0] += zero105[0];
     out[1] += zero105[1];
     out[2] += zero105[2];
     out[3] += zero105[3];
 
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
 }
 
 # define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
 # define two107 (((limb)1) << 107)
 # define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
 
 /* zero107 is 0 mod p */
 static const felem zero107 =
     { two107m43m11, two107, two107m43p11, two107m43p11 };
 
 /*-
  * An alternative felem_diff for larger inputs |in|
  * felem_diff_zero107 subtracts |in| from |out|
  * On entry:
  *   in[i] < 2^106
  * On exit:
  *   out[i] < out[i] + 2^107
  */
 static void felem_diff_zero107(felem out, const felem in)
 {
     /*
      * In order to prevent underflow, we add 0 mod p before subtracting.
      */
     out[0] += zero107[0];
     out[1] += zero107[1];
     out[2] += zero107[2];
     out[3] += zero107[3];
 
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
 }
 
 /*-
  * longfelem_diff subtracts |in| from |out|
  * On entry:
  *   in[i] < 7*2^67
  * On exit:
  *   out[i] < out[i] + 2^70 + 2^40
  */
 static void longfelem_diff(longfelem out, const longfelem in)
 {
     static const limb two70m8p6 =
         (((limb) 1) << 70) - (((limb) 1) << 8) + (((limb) 1) << 6);
     static const limb two70p40 = (((limb) 1) << 70) + (((limb) 1) << 40);
     static const limb two70 = (((limb) 1) << 70);
     static const limb two70m40m38p6 =
         (((limb) 1) << 70) - (((limb) 1) << 40) - (((limb) 1) << 38) +
         (((limb) 1) << 6);
     static const limb two70m6 = (((limb) 1) << 70) - (((limb) 1) << 6);
 
     /* add 0 mod p to avoid underflow */
     out[0] += two70m8p6;
     out[1] += two70p40;
     out[2] += two70;
     out[3] += two70m40m38p6;
     out[4] += two70m6;
     out[5] += two70m6;
     out[6] += two70m6;
     out[7] += two70m6;
 
     /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
     out[0] -= in[0];
     out[1] -= in[1];
     out[2] -= in[2];
     out[3] -= in[3];
     out[4] -= in[4];
     out[5] -= in[5];
     out[6] -= in[6];
     out[7] -= in[7];
 }
 
 # define two64m0 (((limb)1) << 64) - 1
 # define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
 # define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
 # define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
 
 /* zero110 is 0 mod p */
 static const felem zero110 = { two64m0, two110p32m0, two64m46, two64m32 };
 
 /*-
  * felem_shrink converts an felem into a smallfelem. The result isn't quite
  * minimal as the value may be greater than p.
  *
  * On entry:
  *   in[i] < 2^109
  * On exit:
  *   out[i] < 2^64
  */
 static void felem_shrink(smallfelem out, const felem in)
 {
     felem tmp;
     u64 a, b, mask;
     s64 high, low;
     static const u64 kPrime3Test = 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
 
     /* Carry 2->3 */
     tmp[3] = zero110[3] + in[3] + ((u64)(in[2] >> 64));
     /* tmp[3] < 2^110 */
 
     tmp[2] = zero110[2] + (u64)in[2];
     tmp[0] = zero110[0] + in[0];
     tmp[1] = zero110[1] + in[1];
     /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
 
     /*
      * We perform two partial reductions where we eliminate the high-word of
      * tmp[3]. We don't update the other words till the end.
      */
     a = tmp[3] >> 64;           /* a < 2^46 */
     tmp[3] = (u64)tmp[3];
     tmp[3] -= a;
     tmp[3] += ((limb) a) << 32;
     /* tmp[3] < 2^79 */
 
     b = a;
     a = tmp[3] >> 64;           /* a < 2^15 */
     b += a;                     /* b < 2^46 + 2^15 < 2^47 */
     tmp[3] = (u64)tmp[3];
     tmp[3] -= a;
     tmp[3] += ((limb) a) << 32;
     /* tmp[3] < 2^64 + 2^47 */
 
     /*
      * This adjusts the other two words to complete the two partial
      * reductions.
      */
     tmp[0] += b;
     tmp[1] -= (((limb) b) << 32);
 
     /*
      * In order to make space in tmp[3] for the carry from 2 -> 3, we
      * conditionally subtract kPrime if tmp[3] is large enough.
      */
     high = tmp[3] >> 64;
     /* As tmp[3] < 2^65, high is either 1 or 0 */
     high <<= 63;
     high >>= 63;
     /*-
      * high is:
      *   all ones   if the high word of tmp[3] is 1
      *   all zeros  if the high word of tmp[3] if 0 */
     low = tmp[3];
     mask = low >> 63;
     /*-
      * mask is:
      *   all ones   if the MSB of low is 1
      *   all zeros  if the MSB of low if 0 */
     low &= bottom63bits;
     low -= kPrime3Test;
     /* if low was greater than kPrime3Test then the MSB is zero */
     low = ~low;
     low >>= 63;
     /*-
      * low is:
      *   all ones   if low was > kPrime3Test
      *   all zeros  if low was <= kPrime3Test */
     mask = (mask & low) | high;
     tmp[0] -= mask & kPrime[0];
     tmp[1] -= mask & kPrime[1];
     /* kPrime[2] is zero, so omitted */
     tmp[3] -= mask & kPrime[3];
     /* tmp[3] < 2**64 - 2**32 + 1 */
 
     tmp[1] += ((u64)(tmp[0] >> 64));
     tmp[0] = (u64)tmp[0];
     tmp[2] += ((u64)(tmp[1] >> 64));
     tmp[1] = (u64)tmp[1];
     tmp[3] += ((u64)(tmp[2] >> 64));
     tmp[2] = (u64)tmp[2];
     /* tmp[i] < 2^64 */
 
     out[0] = tmp[0];
     out[1] = tmp[1];
     out[2] = tmp[2];
     out[3] = tmp[3];
 }
 
 /* smallfelem_expand converts a smallfelem to an felem */
 static void smallfelem_expand(felem out, const smallfelem in)
 {
     out[0] = in[0];
     out[1] = in[1];
     out[2] = in[2];
     out[3] = in[3];
 }
 
 /*-
  * smallfelem_square sets |out| = |small|^2
  * On entry:
  *   small[i] < 2^64
  * On exit:
  *   out[i] < 7 * 2^64 < 2^67
  */
 static void smallfelem_square(longfelem out, const smallfelem small)
 {
     limb a;
     u64 high, low;
 
     a = ((uint128_t) small[0]) * small[0];
     low = a;
     high = a >> 64;
     out[0] = low;
     out[1] = high;
 
     a = ((uint128_t) small[0]) * small[1];
     low = a;
     high = a >> 64;
     out[1] += low;
     out[1] += low;
     out[2] = high;
 
     a = ((uint128_t) small[0]) * small[2];
     low = a;
     high = a >> 64;
     out[2] += low;
     out[2] *= 2;
     out[3] = high;
 
     a = ((uint128_t) small[0]) * small[3];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[4] = high;
 
     a = ((uint128_t) small[1]) * small[2];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[3] *= 2;
     out[4] += high;
 
     a = ((uint128_t) small[1]) * small[1];
     low = a;
     high = a >> 64;
     out[2] += low;
     out[3] += high;
 
     a = ((uint128_t) small[1]) * small[3];
     low = a;
     high = a >> 64;
     out[4] += low;
     out[4] *= 2;
     out[5] = high;
 
     a = ((uint128_t) small[2]) * small[3];
     low = a;
     high = a >> 64;
     out[5] += low;
     out[5] *= 2;
     out[6] = high;
     out[6] += high;
 
     a = ((uint128_t) small[2]) * small[2];
     low = a;
     high = a >> 64;
     out[4] += low;
     out[5] += high;
 
     a = ((uint128_t) small[3]) * small[3];
     low = a;
     high = a >> 64;
     out[6] += low;
     out[7] = high;
 }
 
 /*-
  * felem_square sets |out| = |in|^2
  * On entry:
  *   in[i] < 2^109
  * On exit:
  *   out[i] < 7 * 2^64 < 2^67
  */
 static void felem_square(longfelem out, const felem in)
 {
     u64 small[4];
     felem_shrink(small, in);
     smallfelem_square(out, small);
 }
 
 /*-
  * smallfelem_mul sets |out| = |small1| * |small2|
  * On entry:
  *   small1[i] < 2^64
  *   small2[i] < 2^64
  * On exit:
  *   out[i] < 7 * 2^64 < 2^67
  */
 static void smallfelem_mul(longfelem out, const smallfelem small1,
                            const smallfelem small2)
 {
     limb a;
     u64 high, low;
 
     a = ((uint128_t) small1[0]) * small2[0];
     low = a;
     high = a >> 64;
     out[0] = low;
     out[1] = high;
 
     a = ((uint128_t) small1[0]) * small2[1];
     low = a;
     high = a >> 64;
     out[1] += low;
     out[2] = high;
 
     a = ((uint128_t) small1[1]) * small2[0];
     low = a;
     high = a >> 64;
     out[1] += low;
     out[2] += high;
 
     a = ((uint128_t) small1[0]) * small2[2];
     low = a;
     high = a >> 64;
     out[2] += low;
     out[3] = high;
 
     a = ((uint128_t) small1[1]) * small2[1];
     low = a;
     high = a >> 64;
     out[2] += low;
     out[3] += high;
 
     a = ((uint128_t) small1[2]) * small2[0];
     low = a;
     high = a >> 64;
     out[2] += low;
     out[3] += high;
 
     a = ((uint128_t) small1[0]) * small2[3];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[4] = high;
 
     a = ((uint128_t) small1[1]) * small2[2];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[4] += high;
 
     a = ((uint128_t) small1[2]) * small2[1];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[4] += high;
 
     a = ((uint128_t) small1[3]) * small2[0];
     low = a;
     high = a >> 64;
     out[3] += low;
     out[4] += high;
 
     a = ((uint128_t) small1[1]) * small2[3];
     low = a;
     high = a >> 64;
     out[4] += low;
     out[5] = high;
 
     a = ((uint128_t) small1[2]) * small2[2];
     low = a;
     high = a >> 64;
     out[4] += low;
     out[5] += high;
 
     a = ((uint128_t) small1[3]) * small2[1];
     low = a;
     high = a >> 64;
     out[4] += low;
     out[5] += high;
 
     a = ((uint128_t) small1[2]) * small2[3];
     low = a;
     high = a >> 64;
     out[5] += low;
     out[6] = high;
 
     a = ((uint128_t) small1[3]) * small2[2];
     low = a;
     high = a >> 64;
     out[5] += low;
     out[6] += high;
 
     a = ((uint128_t) small1[3]) * small2[3];
     low = a;
     high = a >> 64;
     out[6] += low;
     out[7] = high;
 }
 
 /*-
  * felem_mul sets |out| = |in1| * |in2|
  * On entry:
  *   in1[i] < 2^109
  *   in2[i] < 2^109
  * On exit:
  *   out[i] < 7 * 2^64 < 2^67
  */
 static void felem_mul(longfelem out, const felem in1, const felem in2)
 {
     smallfelem small1, small2;
     felem_shrink(small1, in1);
     felem_shrink(small2, in2);
     smallfelem_mul(out, small1, small2);
 }
 
 /*-
  * felem_small_mul sets |out| = |small1| * |in2|
  * On entry:
  *   small1[i] < 2^64
  *   in2[i] < 2^109
  * On exit:
  *   out[i] < 7 * 2^64 < 2^67
  */
 static void felem_small_mul(longfelem out, const smallfelem small1,
                             const felem in2)
 {
     smallfelem small2;
     felem_shrink(small2, in2);
     smallfelem_mul(out, small1, small2);
 }
 
 # define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
 # define two100 (((limb)1) << 100)
 # define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
 /* zero100 is 0 mod p */
 static const felem zero100 =
     { two100m36m4, two100, two100m36p4, two100m36p4 };
 
 /*-
  * Internal function for the different flavours of felem_reduce.
  * felem_reduce_ reduces the higher coefficients in[4]-in[7].
  * On entry:
  *   out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
  *   out[1] >= in[7] + 2^32*in[4]
  *   out[2] >= in[5] + 2^32*in[5]
  *   out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
  * On exit:
  *   out[0] <= out[0] + in[4] + 2^32*in[5]
  *   out[1] <= out[1] + in[5] + 2^33*in[6]
  *   out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
  *   out[3] <= out[3] + 2^32*in[4] + 3*in[7]
  */
 static void felem_reduce_(felem out, const longfelem in)
 {
     int128_t c;
     /* combine common terms from below */
     c = in[4] + (in[5] << 32);
     out[0] += c;
     out[3] -= c;
 
     c = in[5] - in[7];
     out[1] += c;
     out[2] -= c;
 
     /* the remaining terms */
     /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
     out[1] -= (in[4] << 32);
     out[3] += (in[4] << 32);
 
     /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
     out[2] -= (in[5] << 32);
 
     /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
     out[0] -= in[6];
     out[0] -= (in[6] << 32);
     out[1] += (in[6] << 33);
     out[2] += (in[6] * 2);
     out[3] -= (in[6] << 32);
 
     /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
     out[0] -= in[7];
     out[0] -= (in[7] << 32);
     out[2] += (in[7] << 33);
     out[3] += (in[7] * 3);
 }
 
 /*-
  * felem_reduce converts a longfelem into an felem.
  * To be called directly after felem_square or felem_mul.
  * On entry:
  *   in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
  *   in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
  * On exit:
  *   out[i] < 2^101
  */
 static void felem_reduce(felem out, const longfelem in)
 {
     out[0] = zero100[0] + in[0];
     out[1] = zero100[1] + in[1];
     out[2] = zero100[2] + in[2];
     out[3] = zero100[3] + in[3];
 
     felem_reduce_(out, in);
 
     /*-
      * out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
      * out[1] > 2^100 - 2^64 - 7*2^96 > 0
      * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
      * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
      *
      * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
      * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
      * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
      * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101
      */
 }
 
 /*-
  * felem_reduce_zero105 converts a larger longfelem into an felem.
  * On entry:
  *   in[0] < 2^71
  * On exit:
  *   out[i] < 2^106
  */
 static void felem_reduce_zero105(felem out, const longfelem in)
 {
     out[0] = zero105[0] + in[0];
     out[1] = zero105[1] + in[1];
     out[2] = zero105[2] + in[2];
     out[3] = zero105[3] + in[3];
 
     felem_reduce_(out, in);
 
     /*-
      * out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
      * out[1] > 2^105 - 2^71 - 2^103 > 0
      * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
      * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
      *
      * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
      * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
      * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
      * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106
      */
 }
 
 /*
  * subtract_u64 sets *result = *result - v and *carry to one if the
  * subtraction underflowed.
  */
 static void subtract_u64(u64 *result, u64 *carry, u64 v)
 {
     uint128_t r = *result;
     r -= v;
     *carry = (r >> 64) & 1;
     *result = (u64)r;
 }
 
 /*
  * felem_contract converts |in| to its unique, minimal representation. On
  * entry: in[i] < 2^109
  */
 static void felem_contract(smallfelem out, const felem in)
 {
     unsigned i;
     u64 all_equal_so_far = 0, result = 0, carry;
 
     felem_shrink(out, in);
     /* small is minimal except that the value might be > p */
 
     all_equal_so_far--;
     /*
      * We are doing a constant time test if out >= kPrime. We need to compare
      * each u64, from most-significant to least significant. For each one, if
      * all words so far have been equal (m is all ones) then a non-equal
      * result is the answer. Otherwise we continue.
      */
     for (i = 3; i < 4; i--) {
         u64 equal;
         uint128_t a = ((uint128_t) kPrime[i]) - out[i];
         /*
          * if out[i] > kPrime[i] then a will underflow and the high 64-bits
          * will all be set.
          */
         result |= all_equal_so_far & ((u64)(a >> 64));
 
         /*
          * if kPrime[i] == out[i] then |equal| will be all zeros and the
          * decrement will make it all ones.
          */
         equal = kPrime[i] ^ out[i];
         equal--;
         equal &= equal << 32;
         equal &= equal << 16;
         equal &= equal << 8;
         equal &= equal << 4;
         equal &= equal << 2;
         equal &= equal << 1;
         equal = ((s64) equal) >> 63;
 
         all_equal_so_far &= equal;
     }
 
     /*
      * if all_equal_so_far is still all ones then the two values are equal
      * and so out >= kPrime is true.
      */
     result |= all_equal_so_far;
 
     /* if out >= kPrime then we subtract kPrime. */
     subtract_u64(&out[0], &carry, result & kPrime[0]);
     subtract_u64(&out[1], &carry, carry);
     subtract_u64(&out[2], &carry, carry);
     subtract_u64(&out[3], &carry, carry);
 
     subtract_u64(&out[1], &carry, result & kPrime[1]);
     subtract_u64(&out[2], &carry, carry);
     subtract_u64(&out[3], &carry, carry);
 
     subtract_u64(&out[2], &carry, result & kPrime[2]);
     subtract_u64(&out[3], &carry, carry);
 
     subtract_u64(&out[3], &carry, result & kPrime[3]);
 }
 
 static void smallfelem_square_contract(smallfelem out, const smallfelem in)
 {
     longfelem longtmp;
     felem tmp;
 
     smallfelem_square(longtmp, in);
     felem_reduce(tmp, longtmp);
     felem_contract(out, tmp);
 }
 
 static void smallfelem_mul_contract(smallfelem out, const smallfelem in1,
                                     const smallfelem in2)
 {
     longfelem longtmp;
     felem tmp;
 
     smallfelem_mul(longtmp, in1, in2);
     felem_reduce(tmp, longtmp);
     felem_contract(out, tmp);
 }
 
 /*-
  * felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  * otherwise.
  * On entry:
  *   small[i] < 2^64
  */
 static limb smallfelem_is_zero(const smallfelem small)
 {
     limb result;
     u64 is_p;
 
     u64 is_zero = small[0] | small[1] | small[2] | small[3];
     is_zero--;
     is_zero &= is_zero << 32;
     is_zero &= is_zero << 16;
     is_zero &= is_zero << 8;
     is_zero &= is_zero << 4;
     is_zero &= is_zero << 2;
     is_zero &= is_zero << 1;
     is_zero = ((s64) is_zero) >> 63;
 
     is_p = (small[0] ^ kPrime[0]) |
         (small[1] ^ kPrime[1]) |
         (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
     is_p--;
     is_p &= is_p << 32;
     is_p &= is_p << 16;
     is_p &= is_p << 8;
     is_p &= is_p << 4;
     is_p &= is_p << 2;
     is_p &= is_p << 1;
     is_p = ((s64) is_p) >> 63;
 
     is_zero |= is_p;
 
     result = is_zero;
     result |= ((limb) is_zero) << 64;
     return result;
 }
 
 static int smallfelem_is_zero_int(const smallfelem small)
 {
     return (int)(smallfelem_is_zero(small) & ((limb) 1));
 }
 
 /*-
  * felem_inv calculates |out| = |in|^{-1}
  *
  * Based on Fermat's Little Theorem:
  *   a^p = a (mod p)
  *   a^{p-1} = 1 (mod p)
  *   a^{p-2} = a^{-1} (mod p)
  */
 static void felem_inv(felem out, const felem in)
 {
     felem ftmp, ftmp2;
     /* each e_I will hold |in|^{2^I - 1} */
     felem e2, e4, e8, e16, e32, e64;
     longfelem tmp;
     unsigned i;
 
     felem_square(tmp, in);
     felem_reduce(ftmp, tmp);    /* 2^1 */
     felem_mul(tmp, in, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^2 - 2^0 */
     felem_assign(e2, ftmp);
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^3 - 2^1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^4 - 2^2 */
     felem_mul(tmp, ftmp, e2);
     felem_reduce(ftmp, tmp);    /* 2^4 - 2^0 */
     felem_assign(e4, ftmp);
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^5 - 2^1 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^6 - 2^2 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^7 - 2^3 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^8 - 2^4 */
     felem_mul(tmp, ftmp, e4);
     felem_reduce(ftmp, tmp);    /* 2^8 - 2^0 */
     felem_assign(e8, ftmp);
     for (i = 0; i < 8; i++) {
         felem_square(tmp, ftmp);
         felem_reduce(ftmp, tmp);
     }                           /* 2^16 - 2^8 */
     felem_mul(tmp, ftmp, e8);
     felem_reduce(ftmp, tmp);    /* 2^16 - 2^0 */
     felem_assign(e16, ftmp);
     for (i = 0; i < 16; i++) {
         felem_square(tmp, ftmp);
         felem_reduce(ftmp, tmp);
     }                           /* 2^32 - 2^16 */
     felem_mul(tmp, ftmp, e16);
     felem_reduce(ftmp, tmp);    /* 2^32 - 2^0 */
     felem_assign(e32, ftmp);
     for (i = 0; i < 32; i++) {
         felem_square(tmp, ftmp);
         felem_reduce(ftmp, tmp);
     }                           /* 2^64 - 2^32 */
     felem_assign(e64, ftmp);
     felem_mul(tmp, ftmp, in);
     felem_reduce(ftmp, tmp);    /* 2^64 - 2^32 + 2^0 */
     for (i = 0; i < 192; i++) {
         felem_square(tmp, ftmp);
         felem_reduce(ftmp, tmp);
     }                           /* 2^256 - 2^224 + 2^192 */
 
     felem_mul(tmp, e64, e32);
     felem_reduce(ftmp2, tmp);   /* 2^64 - 2^0 */
     for (i = 0; i < 16; i++) {
         felem_square(tmp, ftmp2);
         felem_reduce(ftmp2, tmp);
     }                           /* 2^80 - 2^16 */
     felem_mul(tmp, ftmp2, e16);
     felem_reduce(ftmp2, tmp);   /* 2^80 - 2^0 */
     for (i = 0; i < 8; i++) {
         felem_square(tmp, ftmp2);
         felem_reduce(ftmp2, tmp);
     }                           /* 2^88 - 2^8 */
     felem_mul(tmp, ftmp2, e8);
     felem_reduce(ftmp2, tmp);   /* 2^88 - 2^0 */
     for (i = 0; i < 4; i++) {
         felem_square(tmp, ftmp2);
         felem_reduce(ftmp2, tmp);
     }                           /* 2^92 - 2^4 */
     felem_mul(tmp, ftmp2, e4);
     felem_reduce(ftmp2, tmp);   /* 2^92 - 2^0 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^93 - 2^1 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^94 - 2^2 */
     felem_mul(tmp, ftmp2, e2);
     felem_reduce(ftmp2, tmp);   /* 2^94 - 2^0 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^95 - 2^1 */
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp2, tmp);   /* 2^96 - 2^2 */
     felem_mul(tmp, ftmp2, in);
     felem_reduce(ftmp2, tmp);   /* 2^96 - 3 */
 
     felem_mul(tmp, ftmp2, ftmp);
     felem_reduce(out, tmp);     /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
 }
 
 static void smallfelem_inv_contract(smallfelem out, const smallfelem in)
 {
     felem tmp;
 
     smallfelem_expand(tmp, in);
     felem_inv(tmp, tmp);
     felem_contract(out, tmp);
 }
 
 /*-
  * Group operations
  * ----------------
  *
  * Building on top of the field operations we have the operations on the
  * elliptic curve group itself. Points on the curve are represented in Jacobian
  * coordinates
  */
 
 /*-
  * point_double calculates 2*(x_in, y_in, z_in)
  *
  * The method is taken from:
  *   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  *
  * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  * while x_out == y_in is not (maybe this works, but it's not tested).
  */
 static void
 point_double(felem x_out, felem y_out, felem z_out,
              const felem x_in, const felem y_in, const felem z_in)
 {
     longfelem tmp, tmp2;
     felem delta, gamma, beta, alpha, ftmp, ftmp2;
     smallfelem small1, small2;
 
     felem_assign(ftmp, x_in);
     /* ftmp[i] < 2^106 */
     felem_assign(ftmp2, x_in);
     /* ftmp2[i] < 2^106 */
 
     /* delta = z^2 */
     felem_square(tmp, z_in);
     felem_reduce(delta, tmp);
     /* delta[i] < 2^101 */
 
     /* gamma = y^2 */
     felem_square(tmp, y_in);
     felem_reduce(gamma, tmp);
     /* gamma[i] < 2^101 */
     felem_shrink(small1, gamma);
 
     /* beta = x*gamma */
     felem_small_mul(tmp, small1, x_in);
     felem_reduce(beta, tmp);
     /* beta[i] < 2^101 */
 
     /* alpha = 3*(x-delta)*(x+delta) */
     felem_diff(ftmp, delta);
     /* ftmp[i] < 2^105 + 2^106 < 2^107 */
     felem_sum(ftmp2, delta);
     /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
     felem_scalar(ftmp2, 3);
     /* ftmp2[i] < 3 * 2^107 < 2^109 */
     felem_mul(tmp, ftmp, ftmp2);
     felem_reduce(alpha, tmp);
     /* alpha[i] < 2^101 */
     felem_shrink(small2, alpha);
 
     /* x' = alpha^2 - 8*beta */
     smallfelem_square(tmp, small2);
     felem_reduce(x_out, tmp);
     felem_assign(ftmp, beta);
     felem_scalar(ftmp, 8);
     /* ftmp[i] < 8 * 2^101 = 2^104 */
     felem_diff(x_out, ftmp);
     /* x_out[i] < 2^105 + 2^101 < 2^106 */
 
     /* z' = (y + z)^2 - gamma - delta */
     felem_sum(delta, gamma);
     /* delta[i] < 2^101 + 2^101 = 2^102 */
     felem_assign(ftmp, y_in);
     felem_sum(ftmp, z_in);
     /* ftmp[i] < 2^106 + 2^106 = 2^107 */
     felem_square(tmp, ftmp);
     felem_reduce(z_out, tmp);
     felem_diff(z_out, delta);
     /* z_out[i] < 2^105 + 2^101 < 2^106 */
 
     /* y' = alpha*(4*beta - x') - 8*gamma^2 */
     felem_scalar(beta, 4);
     /* beta[i] < 4 * 2^101 = 2^103 */
     felem_diff_zero107(beta, x_out);
     /* beta[i] < 2^107 + 2^103 < 2^108 */
     felem_small_mul(tmp, small2, beta);
     /* tmp[i] < 7 * 2^64 < 2^67 */
     smallfelem_square(tmp2, small1);
     /* tmp2[i] < 7 * 2^64 */
     longfelem_scalar(tmp2, 8);
     /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
     longfelem_diff(tmp, tmp2);
     /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
     felem_reduce_zero105(y_out, tmp);
     /* y_out[i] < 2^106 */
 }
 
 /*
  * point_double_small is the same as point_double, except that it operates on
  * smallfelems
  */
 static void
 point_double_small(smallfelem x_out, smallfelem y_out, smallfelem z_out,
                    const smallfelem x_in, const smallfelem y_in,
                    const smallfelem z_in)
 {
     felem felem_x_out, felem_y_out, felem_z_out;
     felem felem_x_in, felem_y_in, felem_z_in;
 
     smallfelem_expand(felem_x_in, x_in);
     smallfelem_expand(felem_y_in, y_in);
     smallfelem_expand(felem_z_in, z_in);
     point_double(felem_x_out, felem_y_out, felem_z_out,
                  felem_x_in, felem_y_in, felem_z_in);
     felem_shrink(x_out, felem_x_out);
     felem_shrink(y_out, felem_y_out);
     felem_shrink(z_out, felem_z_out);
 }
 
 /* copy_conditional copies in to out iff mask is all ones. */
 static void copy_conditional(felem out, const felem in, limb mask)
 {
     unsigned i;
     for (i = 0; i < NLIMBS; ++i) {
         const limb tmp = mask & (in[i] ^ out[i]);
         out[i] ^= tmp;
     }
 }
 
 /* copy_small_conditional copies in to out iff mask is all ones. */
 static void copy_small_conditional(felem out, const smallfelem in, limb mask)
 {
     unsigned i;
     const u64 mask64 = mask;
     for (i = 0; i < NLIMBS; ++i) {
         out[i] = ((limb) (in[i] & mask64)) | (out[i] & ~mask);
     }
 }
 
 /*-
  * point_add calcuates (x1, y1, z1) + (x2, y2, z2)
  *
  * The method is taken from:
  *   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  *
  * This function includes a branch for checking whether the two input points
  * are equal, (while not equal to the point at infinity). This case never
  * happens during single point multiplication, so there is no timing leak for
  * ECDH or ECDSA signing.
  */
 static void point_add(felem x3, felem y3, felem z3,
                       const felem x1, const felem y1, const felem z1,
                       const int mixed, const smallfelem x2,
                       const smallfelem y2, const smallfelem z2)
 {
     felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
     longfelem tmp, tmp2;
     smallfelem small1, small2, small3, small4, small5;
     limb x_equal, y_equal, z1_is_zero, z2_is_zero;
 
     felem_shrink(small3, z1);
 
     z1_is_zero = smallfelem_is_zero(small3);
     z2_is_zero = smallfelem_is_zero(z2);
 
     /* ftmp = z1z1 = z1**2 */
     smallfelem_square(tmp, small3);
     felem_reduce(ftmp, tmp);
     /* ftmp[i] < 2^101 */
     felem_shrink(small1, ftmp);
 
     if (!mixed) {
         /* ftmp2 = z2z2 = z2**2 */
         smallfelem_square(tmp, z2);
         felem_reduce(ftmp2, tmp);
         /* ftmp2[i] < 2^101 */
         felem_shrink(small2, ftmp2);
 
         felem_shrink(small5, x1);
 
         /* u1 = ftmp3 = x1*z2z2 */
         smallfelem_mul(tmp, small5, small2);
         felem_reduce(ftmp3, tmp);
         /* ftmp3[i] < 2^101 */
 
         /* ftmp5 = z1 + z2 */
         felem_assign(ftmp5, z1);
         felem_small_sum(ftmp5, z2);
         /* ftmp5[i] < 2^107 */
 
         /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
         felem_square(tmp, ftmp5);
         felem_reduce(ftmp5, tmp);
         /* ftmp2 = z2z2 + z1z1 */
         felem_sum(ftmp2, ftmp);
         /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
         felem_diff(ftmp5, ftmp2);
         /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
 
         /* ftmp2 = z2 * z2z2 */
         smallfelem_mul(tmp, small2, z2);
         felem_reduce(ftmp2, tmp);
 
         /* s1 = ftmp2 = y1 * z2**3 */
         felem_mul(tmp, y1, ftmp2);
         felem_reduce(ftmp6, tmp);
         /* ftmp6[i] < 2^101 */
     } else {
         /*
          * We'll assume z2 = 1 (special case z2 = 0 is handled later)
          */
 
         /* u1 = ftmp3 = x1*z2z2 */
         felem_assign(ftmp3, x1);
         /* ftmp3[i] < 2^106 */
 
         /* ftmp5 = 2z1z2 */
         felem_assign(ftmp5, z1);
         felem_scalar(ftmp5, 2);
         /* ftmp5[i] < 2*2^106 = 2^107 */
 
         /* s1 = ftmp2 = y1 * z2**3 */
         felem_assign(ftmp6, y1);
         /* ftmp6[i] < 2^106 */
     }
 
     /* u2 = x2*z1z1 */
     smallfelem_mul(tmp, x2, small1);
     felem_reduce(ftmp4, tmp);
 
     /* h = ftmp4 = u2 - u1 */
     felem_diff_zero107(ftmp4, ftmp3);
     /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
     felem_shrink(small4, ftmp4);
 
     x_equal = smallfelem_is_zero(small4);
 
     /* z_out = ftmp5 * h */
     felem_small_mul(tmp, small4, ftmp5);
     felem_reduce(z_out, tmp);
     /* z_out[i] < 2^101 */
 
     /* ftmp = z1 * z1z1 */
     smallfelem_mul(tmp, small1, small3);
     felem_reduce(ftmp, tmp);
 
     /* s2 = tmp = y2 * z1**3 */
     felem_small_mul(tmp, y2, ftmp);
     felem_reduce(ftmp5, tmp);
 
     /* r = ftmp5 = (s2 - s1)*2 */
     felem_diff_zero107(ftmp5, ftmp6);
     /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
     felem_scalar(ftmp5, 2);
     /* ftmp5[i] < 2^109 */
     felem_shrink(small1, ftmp5);
     y_equal = smallfelem_is_zero(small1);
 
     if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
         point_double(x3, y3, z3, x1, y1, z1);
         return;
     }
 
     /* I = ftmp = (2h)**2 */
     felem_assign(ftmp, ftmp4);
     felem_scalar(ftmp, 2);
     /* ftmp[i] < 2*2^108 = 2^109 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);
 
     /* J = ftmp2 = h * I */
     felem_mul(tmp, ftmp4, ftmp);
     felem_reduce(ftmp2, tmp);
 
     /* V = ftmp4 = U1 * I */
     felem_mul(tmp, ftmp3, ftmp);
     felem_reduce(ftmp4, tmp);
 
     /* x_out = r**2 - J - 2V */
     smallfelem_square(tmp, small1);
     felem_reduce(x_out, tmp);
     felem_assign(ftmp3, ftmp4);
     felem_scalar(ftmp4, 2);
     felem_sum(ftmp4, ftmp2);
     /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
     felem_diff(x_out, ftmp4);
     /* x_out[i] < 2^105 + 2^101 */
 
     /* y_out = r(V-x_out) - 2 * s1 * J */
     felem_diff_zero107(ftmp3, x_out);
     /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
     felem_small_mul(tmp, small1, ftmp3);
     felem_mul(tmp2, ftmp6, ftmp2);
     longfelem_scalar(tmp2, 2);
     /* tmp2[i] < 2*2^67 = 2^68 */
     longfelem_diff(tmp, tmp2);
     /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
     felem_reduce_zero105(y_out, tmp);
     /* y_out[i] < 2^106 */
 
     copy_small_conditional(x_out, x2, z1_is_zero);
     copy_conditional(x_out, x1, z2_is_zero);
     copy_small_conditional(y_out, y2, z1_is_zero);
     copy_conditional(y_out, y1, z2_is_zero);
     copy_small_conditional(z_out, z2, z1_is_zero);
     copy_conditional(z_out, z1, z2_is_zero);
     felem_assign(x3, x_out);
     felem_assign(y3, y_out);
     felem_assign(z3, z_out);
 }
 
 /*
  * point_add_small is the same as point_add, except that it operates on
  * smallfelems
  */
 static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
                             smallfelem x1, smallfelem y1, smallfelem z1,
                             smallfelem x2, smallfelem y2, smallfelem z2)
 {
     felem felem_x3, felem_y3, felem_z3;
     felem felem_x1, felem_y1, felem_z1;
     smallfelem_expand(felem_x1, x1);
     smallfelem_expand(felem_y1, y1);
     smallfelem_expand(felem_z1, z1);
     point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0,
               x2, y2, z2);
     felem_shrink(x3, felem_x3);
     felem_shrink(y3, felem_y3);
     felem_shrink(z3, felem_z3);
 }
 
 /*-
  * Base point pre computation
  * --------------------------
  *
  * Two different sorts of precomputed tables are used in the following code.
  * Each contain various points on the curve, where each point is three field
  * elements (x, y, z).
  *
  * For the base point table, z is usually 1 (0 for the point at infinity).
  * This table has 2 * 16 elements, starting with the following:
  * index | bits    | point
  * ------+---------+------------------------------
  *     0 | 0 0 0 0 | 0G
  *     1 | 0 0 0 1 | 1G
  *     2 | 0 0 1 0 | 2^64G
  *     3 | 0 0 1 1 | (2^64 + 1)G
  *     4 | 0 1 0 0 | 2^128G
  *     5 | 0 1 0 1 | (2^128 + 1)G
  *     6 | 0 1 1 0 | (2^128 + 2^64)G
  *     7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
  *     8 | 1 0 0 0 | 2^192G
  *     9 | 1 0 0 1 | (2^192 + 1)G
  *    10 | 1 0 1 0 | (2^192 + 2^64)G
  *    11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
  *    12 | 1 1 0 0 | (2^192 + 2^128)G
  *    13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
  *    14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
  *    15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
  * followed by a copy of this with each element multiplied by 2^32.
  *
  * The reason for this is so that we can clock bits into four different
  * locations when doing simple scalar multiplies against the base point,
  * and then another four locations using the second 16 elements.
  *
  * Tables for other points have table[i] = iG for i in 0 .. 16. */
 
 /* gmul is the table of precomputed base points */
 static const smallfelem gmul[2][16][3] = {
     {{{0, 0, 0, 0},
       {0, 0, 0, 0},
       {0, 0, 0, 0}},
      {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
        0x6b17d1f2e12c4247},
       {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
        0x4fe342e2fe1a7f9b},
       {1, 0, 0, 0}},
      {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
        0x0fa822bc2811aaa5},
       {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
        0xbff44ae8f5dba80d},
       {1, 0, 0, 0}},
      {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
        0x300a4bbc89d6726f},
       {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
        0x72aac7e0d09b4644},
       {1, 0, 0, 0}},
      {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
        0x447d739beedb5e67},
       {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
        0x2d4825ab834131ee},
       {1, 0, 0, 0}},
      {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
        0xef9519328a9c72ff},
       {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
        0x611e9fc37dbb2c9b},
       {1, 0, 0, 0}},
      {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
        0x550663797b51f5d8},
       {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
        0x157164848aecb851},
       {1, 0, 0, 0}},
      {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
        0xeb5d7745b21141ea},
       {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
        0xeafd72ebdbecc17b},
       {1, 0, 0, 0}},
      {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
        0xa6d39677a7849276},
       {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
        0x674f84749b0b8816},
       {1, 0, 0, 0}},
      {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
        0x4e769e7672c9ddad},
       {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
        0x42b99082de830663},
       {1, 0, 0, 0}},
      {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
        0x78878ef61c6ce04d},
       {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
        0xb6cb3f5d7b72c321},
       {1, 0, 0, 0}},
      {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
        0x0c88bc4d716b1287},
       {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
        0xdd5ddea3f3901dc6},
       {1, 0, 0, 0}},
      {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
        0x68f344af6b317466},
       {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
        0x31b9c405f8540a20},
       {1, 0, 0, 0}},
      {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
        0x4052bf4b6f461db9},
       {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
        0xfecf4d5190b0fc61},
       {1, 0, 0, 0}},
      {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
        0x1eddbae2c802e41a},
       {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
        0x43104d86560ebcfc},
       {1, 0, 0, 0}},
      {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
        0xb48e26b484f7a21c},
       {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
        0xfac015404d4d3dab},
       {1, 0, 0, 0}}},
     {{{0, 0, 0, 0},
       {0, 0, 0, 0},
       {0, 0, 0, 0}},
      {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
        0x7fe36b40af22af89},
       {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
        0xe697d45825b63624},
       {1, 0, 0, 0}},
      {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
        0x4a5b506612a677a6},
       {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
        0xeb13461ceac089f1},
       {1, 0, 0, 0}},
      {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
        0x0781b8291c6a220a},
       {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
        0x690cde8df0151593},
       {1, 0, 0, 0}},
      {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
        0x8a535f566ec73617},
       {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
        0x0455c08468b08bd7},
       {1, 0, 0, 0}},
      {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
        0x06bada7ab77f8276},
       {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
        0x5b476dfd0e6cb18a},
       {1, 0, 0, 0}},
      {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
        0x3e29864e8a2ec908},
       {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
        0x239b90ea3dc31e7e},
       {1, 0, 0, 0}},
      {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
        0x820f4dd949f72ff7},
       {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
        0x140406ec783a05ec},
       {1, 0, 0, 0}},
      {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
        0x68f6b8542783dfee},
       {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
        0xcbe1feba92e40ce6},
       {1, 0, 0, 0}},
      {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
        0xd0b2f94d2f420109},
       {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
        0x971459828b0719e5},
       {1, 0, 0, 0}},
      {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
        0x961610004a866aba},
       {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
        0x7acb9fadcee75e44},
       {1, 0, 0, 0}},
      {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
        0x24eb9acca333bf5b},
       {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
        0x69f891c5acd079cc},
       {1, 0, 0, 0}},
      {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
        0xe51f547c5972a107},
       {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
        0x1c309a2b25bb1387},
       {1, 0, 0, 0}},
      {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
        0x20b87b8aa2c4e503},
       {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
        0xf5c6fa49919776be},
       {1, 0, 0, 0}},
      {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
        0x1ed7d1b9332010b9},
       {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
        0x3a2b03f03217257a},
       {1, 0, 0, 0}},
      {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
        0x15fee545c78dd9f6},
       {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
        0x4ab5b6b2b8753f81},
       {1, 0, 0, 0}}}
 };
 
 /*
  * select_point selects the |idx|th point from a precomputation table and
  * copies it to out.
  */
 static void select_point(const u64 idx, unsigned int size,
                          const smallfelem pre_comp[16][3], smallfelem out[3])
 {
     unsigned i, j;
     u64 *outlimbs = &out[0][0];
     memset(outlimbs, 0, 3 * sizeof(smallfelem));
 
     for (i = 0; i < size; i++) {
         const u64 *inlimbs = (u64 *)&pre_comp[i][0][0];
         u64 mask = i ^ idx;
         mask |= mask >> 4;
         mask |= mask >> 2;
         mask |= mask >> 1;
         mask &= 1;
         mask--;
         for (j = 0; j < NLIMBS * 3; j++)
             outlimbs[j] |= inlimbs[j] & mask;
     }
 }
 
 /* get_bit returns the |i|th bit in |in| */
 static char get_bit(const felem_bytearray in, int i)
 {
     if ((i < 0) || (i >= 256))
         return 0;
     return (in[i >> 3] >> (i & 7)) & 1;
 }
 
 /*
  * Interleaved point multiplication using precomputed point multiples: The
  * small point multiples 0*P, 1*P, ..., 17*P are in pre_comp[], the scalars
  * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  * generator, using certain (large) precomputed multiples in g_pre_comp.
  * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  */
 static void batch_mul(felem x_out, felem y_out, felem z_out,
                       const felem_bytearray scalars[],
                       const unsigned num_points, const u8 *g_scalar,
                       const int mixed, const smallfelem pre_comp[][17][3],
                       const smallfelem g_pre_comp[2][16][3])
 {
     int i, skip;
     unsigned num, gen_mul = (g_scalar != NULL);
     felem nq[3], ftmp;
     smallfelem tmp[3];
     u64 bits;
     u8 sign, digit;
 
     /* set nq to the point at infinity */
     memset(nq, 0, 3 * sizeof(felem));
 
     /*
      * Loop over all scalars msb-to-lsb, interleaving additions of multiples
      * of the generator (two in each of the last 32 rounds) and additions of
      * other points multiples (every 5th round).
      */
     skip = 1;                   /* save two point operations in the first
                                  * round */
     for (i = (num_points ? 255 : 31); i >= 0; --i) {
         /* double */
         if (!skip)
             point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
 
         /* add multiples of the generator */
         if (gen_mul && (i <= 31)) {
             /* first, look 32 bits upwards */
             bits = get_bit(g_scalar, i + 224) << 3;
             bits |= get_bit(g_scalar, i + 160) << 2;
             bits |= get_bit(g_scalar, i + 96) << 1;
             bits |= get_bit(g_scalar, i + 32);
             /* select the point to add, in constant time */
             select_point(bits, 16, g_pre_comp[1], tmp);
 
             if (!skip) {
                 /* Arg 1 below is for "mixed" */
                 point_add(nq[0], nq[1], nq[2],
                           nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
             } else {
                 smallfelem_expand(nq[0], tmp[0]);
                 smallfelem_expand(nq[1], tmp[1]);
                 smallfelem_expand(nq[2], tmp[2]);
                 skip = 0;
             }
 
             /* second, look at the current position */
             bits = get_bit(g_scalar, i + 192) << 3;
             bits |= get_bit(g_scalar, i + 128) << 2;
             bits |= get_bit(g_scalar, i + 64) << 1;
             bits |= get_bit(g_scalar, i);
             /* select the point to add, in constant time */
             select_point(bits, 16, g_pre_comp[0], tmp);
             /* Arg 1 below is for "mixed" */
             point_add(nq[0], nq[1], nq[2],
                       nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
         }
 
         /* do other additions every 5 doublings */
         if (num_points && (i % 5 == 0)) {
             /* loop over all scalars */
             for (num = 0; num < num_points; ++num) {
                 bits = get_bit(scalars[num], i + 4) << 5;
                 bits |= get_bit(scalars[num], i + 3) << 4;
                 bits |= get_bit(scalars[num], i + 2) << 3;
                 bits |= get_bit(scalars[num], i + 1) << 2;
                 bits |= get_bit(scalars[num], i) << 1;
                 bits |= get_bit(scalars[num], i - 1);
                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
 
                 /*
                  * select the point to add or subtract, in constant time
                  */
                 select_point(digit, 17, pre_comp[num], tmp);
                 smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
                                                * point */
                 copy_small_conditional(ftmp, tmp[1], (((limb) sign) - 1));
                 felem_contract(tmp[1], ftmp);
 
                 if (!skip) {
                     point_add(nq[0], nq[1], nq[2],
                               nq[0], nq[1], nq[2],
                               mixed, tmp[0], tmp[1], tmp[2]);
                 } else {
                     smallfelem_expand(nq[0], tmp[0]);
                     smallfelem_expand(nq[1], tmp[1]);
                     smallfelem_expand(nq[2], tmp[2]);
                     skip = 0;
                 }
             }
         }
     }
     felem_assign(x_out, nq[0]);
     felem_assign(y_out, nq[1]);
     felem_assign(z_out, nq[2]);
 }
 
 /* Precomputation for the group generator. */
 typedef struct {
     smallfelem g_pre_comp[2][16][3];
     int references;
 } NISTP256_PRE_COMP;
 
 const EC_METHOD *EC_GFp_nistp256_method(void)
 {
     static const EC_METHOD ret = {
         EC_FLAGS_DEFAULT_OCT,
         NID_X9_62_prime_field,
         ec_GFp_nistp256_group_init,
         ec_GFp_simple_group_finish,
         ec_GFp_simple_group_clear_finish,
         ec_GFp_nist_group_copy,
         ec_GFp_nistp256_group_set_curve,
         ec_GFp_simple_group_get_curve,
         ec_GFp_simple_group_get_degree,
         ec_GFp_simple_group_check_discriminant,
         ec_GFp_simple_point_init,
         ec_GFp_simple_point_finish,
         ec_GFp_simple_point_clear_finish,
         ec_GFp_simple_point_copy,
         ec_GFp_simple_point_set_to_infinity,
         ec_GFp_simple_set_Jprojective_coordinates_GFp,
         ec_GFp_simple_get_Jprojective_coordinates_GFp,
         ec_GFp_simple_point_set_affine_coordinates,
         ec_GFp_nistp256_point_get_affine_coordinates,
         0 /* point_set_compressed_coordinates */ ,
         0 /* point2oct */ ,
         0 /* oct2point */ ,
         ec_GFp_simple_add,
         ec_GFp_simple_dbl,
         ec_GFp_simple_invert,
         ec_GFp_simple_is_at_infinity,
         ec_GFp_simple_is_on_curve,
         ec_GFp_simple_cmp,
         ec_GFp_simple_make_affine,
         ec_GFp_simple_points_make_affine,
         ec_GFp_nistp256_points_mul,
         ec_GFp_nistp256_precompute_mult,
         ec_GFp_nistp256_have_precompute_mult,
         ec_GFp_nist_field_mul,
         ec_GFp_nist_field_sqr,
         0 /* field_div */ ,
         0 /* field_encode */ ,
         0 /* field_decode */ ,
         0                       /* field_set_to_one */
     };
 
     return &ret;
 }
 
 /******************************************************************************/
 /*
  * FUNCTIONS TO MANAGE PRECOMPUTATION
  */
 
 static NISTP256_PRE_COMP *nistp256_pre_comp_new()
 {
     NISTP256_PRE_COMP *ret = NULL;
     ret = (NISTP256_PRE_COMP *) OPENSSL_malloc(sizeof *ret);
     if (!ret) {
         ECerr(EC_F_NISTP256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
         return ret;
     }
     memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
     ret->references = 1;
     return ret;
 }
 
 static void *nistp256_pre_comp_dup(void *src_)
 {
     NISTP256_PRE_COMP *src = src_;
 
     /* no need to actually copy, these objects never change! */
     CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
 
     return src_;
 }
 
 static void nistp256_pre_comp_free(void *pre_)
 {
     int i;
     NISTP256_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_free(pre);
 }
 
 static void nistp256_pre_comp_clear_free(void *pre_)
 {
     int i;
     NISTP256_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_cleanse(pre, sizeof *pre);
     OPENSSL_free(pre);
 }
 
 /******************************************************************************/
 /*
  * OPENSSL EC_METHOD FUNCTIONS
  */
 
 int ec_GFp_nistp256_group_init(EC_GROUP *group)
 {
     int ret;
     ret = ec_GFp_simple_group_init(group);
     group->a_is_minus3 = 1;
     return ret;
 }
 
 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
                                     const BIGNUM *a, const BIGNUM *b,
                                     BN_CTX *ctx)
 {
     int ret = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *curve_p, *curve_a, *curve_b;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
         ((curve_a = BN_CTX_get(ctx)) == NULL) ||
         ((curve_b = BN_CTX_get(ctx)) == NULL))
         goto err;
     BN_bin2bn(nistp256_curve_params[0], sizeof(felem_bytearray), curve_p);
     BN_bin2bn(nistp256_curve_params[1], sizeof(felem_bytearray), curve_a);
     BN_bin2bn(nistp256_curve_params[2], sizeof(felem_bytearray), curve_b);
     if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
         ECerr(EC_F_EC_GFP_NISTP256_GROUP_SET_CURVE,
               EC_R_WRONG_CURVE_PARAMETERS);
         goto err;
     }
     group->field_mod_func = BN_nist_mod_256;
     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  err:
     BN_CTX_end(ctx);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     return ret;
 }
 
 /*
  * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  * (X/Z^2, Y/Z^3)
  */
 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
                                                  const EC_POINT *point,
                                                  BIGNUM *x, BIGNUM *y,
                                                  BN_CTX *ctx)
 {
     felem z1, z2, x_in, y_in;
     smallfelem x_out, y_out;
     longfelem tmp;
 
     if (EC_POINT_is_at_infinity(group, point)) {
         ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
               EC_R_POINT_AT_INFINITY);
         return 0;
     }
     if ((!BN_to_felem(x_in, &point->X)) || (!BN_to_felem(y_in, &point->Y)) ||
         (!BN_to_felem(z1, &point->Z)))
         return 0;
     felem_inv(z2, z1);
     felem_square(tmp, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, x_in, z1);
     felem_reduce(x_in, tmp);
     felem_contract(x_out, x_in);
     if (x != NULL) {
         if (!smallfelem_to_BN(x, x_out)) {
             ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     felem_mul(tmp, z1, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, y_in, z1);
     felem_reduce(y_in, tmp);
     felem_contract(y_out, y_in);
     if (y != NULL) {
         if (!smallfelem_to_BN(y, y_out)) {
             ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     return 1;
 }
 
 /* points below is of size |num|, and tmp_smallfelems is of size |num+1| */
 static void make_points_affine(size_t num, smallfelem points[][3],
                                smallfelem tmp_smallfelems[])
 {
     /*
      * Runs in constant time, unless an input is the point at infinity (which
      * normally shouldn't happen).
      */
     ec_GFp_nistp_points_make_affine_internal(num,
                                              points,
                                              sizeof(smallfelem),
                                              tmp_smallfelems,
                                              (void (*)(void *))smallfelem_one,
                                              (int (*)(const void *))
                                              smallfelem_is_zero_int,
                                              (void (*)(void *, const void *))
                                              smallfelem_assign,
                                              (void (*)(void *, const void *))
                                              smallfelem_square_contract,
                                              (void (*)
                                               (void *, const void *,
                                                const void *))
                                              smallfelem_mul_contract,
                                              (void (*)(void *, const void *))
                                              smallfelem_inv_contract,
                                              /* nothing to contract */
                                              (void (*)(void *, const void *))
                                              smallfelem_assign);
 }
 
 /*
  * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  * values Result is stored in r (r can equal one of the inputs).
  */
 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
                                const BIGNUM *scalar, size_t num,
                                const EC_POINT *points[],
                                const BIGNUM *scalars[], BN_CTX *ctx)
 {
     int ret = 0;
     int j;
     int mixed = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y, *z, *tmp_scalar;
     felem_bytearray g_secret;
     felem_bytearray *secrets = NULL;
     smallfelem(*pre_comp)[17][3] = NULL;
     smallfelem *tmp_smallfelems = NULL;
     felem_bytearray tmp;
     unsigned i, num_bytes;
     int have_pre_comp = 0;
     size_t num_points = num;
     smallfelem x_in, y_in, z_in;
     felem x_out, y_out, z_out;
     NISTP256_PRE_COMP *pre = NULL;
     const smallfelem(*g_pre_comp)[16][3] = NULL;
     EC_POINT *generator = NULL;
     const EC_POINT *p = NULL;
     const BIGNUM *p_scalar = NULL;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) ||
         ((y = BN_CTX_get(ctx)) == NULL) ||
         ((z = BN_CTX_get(ctx)) == NULL) ||
         ((tmp_scalar = BN_CTX_get(ctx)) == NULL))
         goto err;
 
     if (scalar != NULL) {
         pre = EC_EX_DATA_get_data(group->extra_data,
                                   nistp256_pre_comp_dup,
                                   nistp256_pre_comp_free,
                                   nistp256_pre_comp_clear_free);
         if (pre)
             /* we have precomputation, try to use it */
             g_pre_comp = (const smallfelem(*)[16][3])pre->g_pre_comp;
         else
             /* try to use the standard precomputation */
             g_pre_comp = &gmul[0];
         generator = EC_POINT_new(group);
         if (generator == NULL)
             goto err;
         /* get the generator from precomputation */
         if (!smallfelem_to_BN(x, g_pre_comp[0][1][0]) ||
             !smallfelem_to_BN(y, g_pre_comp[0][1][1]) ||
             !smallfelem_to_BN(z, g_pre_comp[0][1][2])) {
             ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
             goto err;
         }
         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
                                                       generator, x, y, z,
                                                       ctx))
             goto err;
         if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
             /* precomputation matches generator */
             have_pre_comp = 1;
         else
             /*
              * we don't have valid precomputation: treat the generator as a
              * random point
              */
             num_points++;
     }
     if (num_points > 0) {
         if (num_points >= 3) {
             /*
              * unless we precompute multiples for just one or two points,
              * converting those into affine form is time well spent
              */
             mixed = 1;
         }
         secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
         pre_comp = OPENSSL_malloc(num_points * 17 * 3 * sizeof(smallfelem));
         if (mixed)
             tmp_smallfelems =
                 OPENSSL_malloc((num_points * 17 + 1) * sizeof(smallfelem));
         if ((secrets == NULL) || (pre_comp == NULL)
             || (mixed && (tmp_smallfelems == NULL))) {
             ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
             goto err;
         }
 
         /*
          * we treat NULL scalars as 0, and NULL points as points at infinity,
          * i.e., they contribute nothing to the linear combination
          */
         memset(secrets, 0, num_points * sizeof(felem_bytearray));
         memset(pre_comp, 0, num_points * 17 * 3 * sizeof(smallfelem));
         for (i = 0; i < num_points; ++i) {
             if (i == num)
                 /*
                  * we didn't have a valid precomputation, so we pick the
                  * generator
                  */
             {
                 p = EC_GROUP_get0_generator(group);
                 p_scalar = scalar;
             } else
                 /* the i^th point */
             {
                 p = points[i];
                 p_scalar = scalars[i];
             }
             if ((p_scalar != NULL) && (p != NULL)) {
                 /* reduce scalar to 0 <= scalar < 2^256 */
                 if ((BN_num_bits(p_scalar) > 256)
                     || (BN_is_negative(p_scalar))) {
                     /*
                      * this is an unusual input, and we don't guarantee
                      * constant-timeness
                      */
                     if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
                         ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
                         goto err;
                     }
                     num_bytes = BN_bn2bin(tmp_scalar, tmp);
                 } else
                     num_bytes = BN_bn2bin(p_scalar, tmp);
                 flip_endian(secrets[i], tmp, num_bytes);
                 /* precompute multiples */
                 if ((!BN_to_felem(x_out, &p->X)) ||
                     (!BN_to_felem(y_out, &p->Y)) ||
                     (!BN_to_felem(z_out, &p->Z)))
                     goto err;
                 felem_shrink(pre_comp[i][1][0], x_out);
                 felem_shrink(pre_comp[i][1][1], y_out);
                 felem_shrink(pre_comp[i][1][2], z_out);
                 for (j = 2; j <= 16; ++j) {
                     if (j & 1) {
                         point_add_small(pre_comp[i][j][0], pre_comp[i][j][1],
                                         pre_comp[i][j][2], pre_comp[i][1][0],
                                         pre_comp[i][1][1], pre_comp[i][1][2],
                                         pre_comp[i][j - 1][0],
                                         pre_comp[i][j - 1][1],
                                         pre_comp[i][j - 1][2]);
                     } else {
                         point_double_small(pre_comp[i][j][0],
                                            pre_comp[i][j][1],
                                            pre_comp[i][j][2],
                                            pre_comp[i][j / 2][0],
                                            pre_comp[i][j / 2][1],
                                            pre_comp[i][j / 2][2]);
                     }
                 }
             }
         }
         if (mixed)
             make_points_affine(num_points * 17, pre_comp[0], tmp_smallfelems);
     }
 
     /* the scalar for the generator */
     if ((scalar != NULL) && (have_pre_comp)) {
         memset(g_secret, 0, sizeof(g_secret));
         /* reduce scalar to 0 <= scalar < 2^256 */
         if ((BN_num_bits(scalar) > 256) || (BN_is_negative(scalar))) {
             /*
              * this is an unusual input, and we don't guarantee
              * constant-timeness
              */
             if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
                 ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
                 goto err;
             }
             num_bytes = BN_bn2bin(tmp_scalar, tmp);
         } else
             num_bytes = BN_bn2bin(scalar, tmp);
         flip_endian(g_secret, tmp, num_bytes);
         /* do the multiplication with generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   g_secret,
                   mixed, (const smallfelem(*)[17][3])pre_comp, g_pre_comp);
     } else
         /* do the multiplication without generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   NULL, mixed, (const smallfelem(*)[17][3])pre_comp, NULL);
     /* reduce the output to its unique minimal representation */
     felem_contract(x_in, x_out);
     felem_contract(y_in, y_out);
     felem_contract(z_in, z_out);
     if ((!smallfelem_to_BN(x, x_in)) || (!smallfelem_to_BN(y, y_in)) ||
         (!smallfelem_to_BN(z, z_in))) {
         ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
         goto err;
     }
     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
 
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (secrets != NULL)
         OPENSSL_free(secrets);
     if (pre_comp != NULL)
         OPENSSL_free(pre_comp);
     if (tmp_smallfelems != NULL)
         OPENSSL_free(tmp_smallfelems);
     return ret;
 }
 
 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
 {
     int ret = 0;
     NISTP256_PRE_COMP *pre = NULL;
     int i, j;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y;
     EC_POINT *generator = NULL;
     smallfelem tmp_smallfelems[32];
     felem x_tmp, y_tmp, z_tmp;
 
     /* throw away old precomputation */
     EC_EX_DATA_free_data(&group->extra_data, nistp256_pre_comp_dup,
                          nistp256_pre_comp_free,
                          nistp256_pre_comp_clear_free);
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) || ((y = BN_CTX_get(ctx)) == NULL))
         goto err;
     /* get the generator */
     if (group->generator == NULL)
         goto err;
     generator = EC_POINT_new(group);
     if (generator == NULL)
         goto err;
     BN_bin2bn(nistp256_curve_params[3], sizeof(felem_bytearray), x);
     BN_bin2bn(nistp256_curve_params[4], sizeof(felem_bytearray), y);
     if (!EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx))
         goto err;
     if ((pre = nistp256_pre_comp_new()) == NULL)
         goto err;
     /*
      * if the generator is the standard one, use built-in precomputation
      */
     if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
         memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
-        ret = 1;
-        goto err;
+        goto done;
     }
     if ((!BN_to_felem(x_tmp, &group->generator->X)) ||
         (!BN_to_felem(y_tmp, &group->generator->Y)) ||
         (!BN_to_felem(z_tmp, &group->generator->Z)))
         goto err;
     felem_shrink(pre->g_pre_comp[0][1][0], x_tmp);
     felem_shrink(pre->g_pre_comp[0][1][1], y_tmp);
     felem_shrink(pre->g_pre_comp[0][1][2], z_tmp);
     /*
      * compute 2^64*G, 2^128*G, 2^192*G for the first table, 2^32*G, 2^96*G,
      * 2^160*G, 2^224*G for the second one
      */
     for (i = 1; i <= 8; i <<= 1) {
         point_double_small(pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
                            pre->g_pre_comp[1][i][2], pre->g_pre_comp[0][i][0],
                            pre->g_pre_comp[0][i][1],
                            pre->g_pre_comp[0][i][2]);
         for (j = 0; j < 31; ++j) {
             point_double_small(pre->g_pre_comp[1][i][0],
                                pre->g_pre_comp[1][i][1],
                                pre->g_pre_comp[1][i][2],
                                pre->g_pre_comp[1][i][0],
                                pre->g_pre_comp[1][i][1],
                                pre->g_pre_comp[1][i][2]);
         }
         if (i == 8)
             break;
         point_double_small(pre->g_pre_comp[0][2 * i][0],
                            pre->g_pre_comp[0][2 * i][1],
                            pre->g_pre_comp[0][2 * i][2],
                            pre->g_pre_comp[1][i][0], pre->g_pre_comp[1][i][1],
                            pre->g_pre_comp[1][i][2]);
         for (j = 0; j < 31; ++j) {
             point_double_small(pre->g_pre_comp[0][2 * i][0],
                                pre->g_pre_comp[0][2 * i][1],
                                pre->g_pre_comp[0][2 * i][2],
                                pre->g_pre_comp[0][2 * i][0],
                                pre->g_pre_comp[0][2 * i][1],
                                pre->g_pre_comp[0][2 * i][2]);
         }
     }
     for (i = 0; i < 2; i++) {
         /* g_pre_comp[i][0] is the point at infinity */
         memset(pre->g_pre_comp[i][0], 0, sizeof(pre->g_pre_comp[i][0]));
         /* the remaining multiples */
         /* 2^64*G + 2^128*G resp. 2^96*G + 2^160*G */
         point_add_small(pre->g_pre_comp[i][6][0], pre->g_pre_comp[i][6][1],
                         pre->g_pre_comp[i][6][2], pre->g_pre_comp[i][4][0],
                         pre->g_pre_comp[i][4][1], pre->g_pre_comp[i][4][2],
                         pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                         pre->g_pre_comp[i][2][2]);
         /* 2^64*G + 2^192*G resp. 2^96*G + 2^224*G */
         point_add_small(pre->g_pre_comp[i][10][0], pre->g_pre_comp[i][10][1],
                         pre->g_pre_comp[i][10][2], pre->g_pre_comp[i][8][0],
                         pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
                         pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                         pre->g_pre_comp[i][2][2]);
         /* 2^128*G + 2^192*G resp. 2^160*G + 2^224*G */
         point_add_small(pre->g_pre_comp[i][12][0], pre->g_pre_comp[i][12][1],
                         pre->g_pre_comp[i][12][2], pre->g_pre_comp[i][8][0],
                         pre->g_pre_comp[i][8][1], pre->g_pre_comp[i][8][2],
                         pre->g_pre_comp[i][4][0], pre->g_pre_comp[i][4][1],
                         pre->g_pre_comp[i][4][2]);
         /*
          * 2^64*G + 2^128*G + 2^192*G resp. 2^96*G + 2^160*G + 2^224*G
          */
         point_add_small(pre->g_pre_comp[i][14][0], pre->g_pre_comp[i][14][1],
                         pre->g_pre_comp[i][14][2], pre->g_pre_comp[i][12][0],
                         pre->g_pre_comp[i][12][1], pre->g_pre_comp[i][12][2],
                         pre->g_pre_comp[i][2][0], pre->g_pre_comp[i][2][1],
                         pre->g_pre_comp[i][2][2]);
         for (j = 1; j < 8; ++j) {
             /* odd multiples: add G resp. 2^32*G */
             point_add_small(pre->g_pre_comp[i][2 * j + 1][0],
                             pre->g_pre_comp[i][2 * j + 1][1],
                             pre->g_pre_comp[i][2 * j + 1][2],
                             pre->g_pre_comp[i][2 * j][0],
                             pre->g_pre_comp[i][2 * j][1],
                             pre->g_pre_comp[i][2 * j][2],
                             pre->g_pre_comp[i][1][0],
                             pre->g_pre_comp[i][1][1],
                             pre->g_pre_comp[i][1][2]);
         }
     }
     make_points_affine(31, &(pre->g_pre_comp[0][1]), tmp_smallfelems);
 
+ done:
     if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp256_pre_comp_dup,
                              nistp256_pre_comp_free,
                              nistp256_pre_comp_clear_free))
         goto err;
     ret = 1;
     pre = NULL;
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (pre)
         nistp256_pre_comp_free(pre);
     return ret;
 }
 
 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group)
 {
     if (EC_EX_DATA_get_data(group->extra_data, nistp256_pre_comp_dup,
                             nistp256_pre_comp_free,
                             nistp256_pre_comp_clear_free)
         != NULL)
         return 1;
     else
         return 0;
 }
 #else
 static void *dummy = &dummy;
 #endif
Index: vendor-crypto/openssl/dist/crypto/ec/ecp_nistp521.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/ec/ecp_nistp521.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/ec/ecp_nistp521.c	(revision 296273)
@@ -1,2148 +1,2148 @@
 /* crypto/ec/ecp_nistp521.c */
 /*
  * Written by Adam Langley (Google) for the OpenSSL project
  */
 /* Copyright 2011 Google Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  *
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 /*
  * A 64-bit implementation of the NIST P-521 elliptic curve point multiplication
  *
  * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
  * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
  * work which got its smarts from Daniel J. Bernstein's work on the same.
  */
 
 #include 
 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
 
 # ifndef OPENSSL_SYS_VMS
 #  include 
 # else
 #  include 
 # endif
 
 # include 
 # include 
 # include "ec_lcl.h"
 
 # if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
   /* even with gcc, the typedef won't work for 32-bit platforms */
 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
                                  * platforms */
 # else
 #  error "Need GCC 3.1 or later to define type uint128_t"
 # endif
 
 typedef uint8_t u8;
 typedef uint64_t u64;
 typedef int64_t s64;
 
 /*
  * The underlying field. P521 operates over GF(2^521-1). We can serialise an
  * element of this field into 66 bytes where the most significant byte
  * contains only a single bit. We call this an felem_bytearray.
  */
 
 typedef u8 felem_bytearray[66];
 
 /*
  * These are the parameters of P521, taken from FIPS 186-3, section D.1.2.5.
  * These values are big-endian.
  */
 static const felem_bytearray nistp521_curve_params[5] = {
     {0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* p */
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff},
     {0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* a = -3 */
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xfc},
     {0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, /* b */
      0x9a, 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85,
      0x40, 0xee, 0xa2, 0xda, 0x72, 0x5b, 0x99, 0xb3,
      0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1,
      0x09, 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e,
      0x93, 0x7b, 0x16, 0x52, 0xc0, 0xbd, 0x3b, 0xb1,
      0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c,
      0x34, 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50,
      0x3f, 0x00},
     {0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, /* x */
      0xe9, 0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95,
      0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
      0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d,
      0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7,
      0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
      0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a,
      0x42, 0x9b, 0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5,
      0xbd, 0x66},
     {0x01, 0x18, 0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, /* y */
      0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d,
      0x1b, 0xd9, 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b,
      0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e,
      0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4,
      0x26, 0x40, 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad,
      0x07, 0x61, 0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72,
      0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1,
      0x66, 0x50}
 };
 
 /*-
  * The representation of field elements.
  * ------------------------------------
  *
  * We represent field elements with nine values. These values are either 64 or
  * 128 bits and the field element represented is:
  *   v[0]*2^0 + v[1]*2^58 + v[2]*2^116 + ... + v[8]*2^464  (mod p)
  * Each of the nine values is called a 'limb'. Since the limbs are spaced only
  * 58 bits apart, but are greater than 58 bits in length, the most significant
  * bits of each limb overlap with the least significant bits of the next.
  *
  * A field element with 64-bit limbs is an 'felem'. One with 128-bit limbs is a
  * 'largefelem' */
 
 # define NLIMBS 9
 
 typedef uint64_t limb;
 typedef limb felem[NLIMBS];
 typedef uint128_t largefelem[NLIMBS];
 
 static const limb bottom57bits = 0x1ffffffffffffff;
 static const limb bottom58bits = 0x3ffffffffffffff;
 
 /*
  * bin66_to_felem takes a little-endian byte array and converts it into felem
  * form. This assumes that the CPU is little-endian.
  */
 static void bin66_to_felem(felem out, const u8 in[66])
 {
     out[0] = (*((limb *) & in[0])) & bottom58bits;
     out[1] = (*((limb *) & in[7]) >> 2) & bottom58bits;
     out[2] = (*((limb *) & in[14]) >> 4) & bottom58bits;
     out[3] = (*((limb *) & in[21]) >> 6) & bottom58bits;
     out[4] = (*((limb *) & in[29])) & bottom58bits;
     out[5] = (*((limb *) & in[36]) >> 2) & bottom58bits;
     out[6] = (*((limb *) & in[43]) >> 4) & bottom58bits;
     out[7] = (*((limb *) & in[50]) >> 6) & bottom58bits;
     out[8] = (*((limb *) & in[58])) & bottom57bits;
 }
 
 /*
  * felem_to_bin66 takes an felem and serialises into a little endian, 66 byte
  * array. This assumes that the CPU is little-endian.
  */
 static void felem_to_bin66(u8 out[66], const felem in)
 {
     memset(out, 0, 66);
     (*((limb *) & out[0])) = in[0];
     (*((limb *) & out[7])) |= in[1] << 2;
     (*((limb *) & out[14])) |= in[2] << 4;
     (*((limb *) & out[21])) |= in[3] << 6;
     (*((limb *) & out[29])) = in[4];
     (*((limb *) & out[36])) |= in[5] << 2;
     (*((limb *) & out[43])) |= in[6] << 4;
     (*((limb *) & out[50])) |= in[7] << 6;
     (*((limb *) & out[58])) = in[8];
 }
 
 /* To preserve endianness when using BN_bn2bin and BN_bin2bn */
 static void flip_endian(u8 *out, const u8 *in, unsigned len)
 {
     unsigned i;
     for (i = 0; i < len; ++i)
         out[i] = in[len - 1 - i];
 }
 
 /* BN_to_felem converts an OpenSSL BIGNUM into an felem */
 static int BN_to_felem(felem out, const BIGNUM *bn)
 {
     felem_bytearray b_in;
     felem_bytearray b_out;
     unsigned num_bytes;
 
     /* BN_bn2bin eats leading zeroes */
     memset(b_out, 0, sizeof b_out);
     num_bytes = BN_num_bytes(bn);
     if (num_bytes > sizeof b_out) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     if (BN_is_negative(bn)) {
         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
         return 0;
     }
     num_bytes = BN_bn2bin(bn, b_in);
     flip_endian(b_out, b_in, num_bytes);
     bin66_to_felem(out, b_out);
     return 1;
 }
 
 /* felem_to_BN converts an felem into an OpenSSL BIGNUM */
 static BIGNUM *felem_to_BN(BIGNUM *out, const felem in)
 {
     felem_bytearray b_in, b_out;
     felem_to_bin66(b_in, in);
     flip_endian(b_out, b_in, sizeof b_out);
     return BN_bin2bn(b_out, sizeof b_out, out);
 }
 
 /*-
  * Field operations
  * ----------------
  */
 
 static void felem_one(felem out)
 {
     out[0] = 1;
     out[1] = 0;
     out[2] = 0;
     out[3] = 0;
     out[4] = 0;
     out[5] = 0;
     out[6] = 0;
     out[7] = 0;
     out[8] = 0;
 }
 
 static void felem_assign(felem out, const felem in)
 {
     out[0] = in[0];
     out[1] = in[1];
     out[2] = in[2];
     out[3] = in[3];
     out[4] = in[4];
     out[5] = in[5];
     out[6] = in[6];
     out[7] = in[7];
     out[8] = in[8];
 }
 
 /* felem_sum64 sets out = out + in. */
 static void felem_sum64(felem out, const felem in)
 {
     out[0] += in[0];
     out[1] += in[1];
     out[2] += in[2];
     out[3] += in[3];
     out[4] += in[4];
     out[5] += in[5];
     out[6] += in[6];
     out[7] += in[7];
     out[8] += in[8];
 }
 
 /* felem_scalar sets out = in * scalar */
 static void felem_scalar(felem out, const felem in, limb scalar)
 {
     out[0] = in[0] * scalar;
     out[1] = in[1] * scalar;
     out[2] = in[2] * scalar;
     out[3] = in[3] * scalar;
     out[4] = in[4] * scalar;
     out[5] = in[5] * scalar;
     out[6] = in[6] * scalar;
     out[7] = in[7] * scalar;
     out[8] = in[8] * scalar;
 }
 
 /* felem_scalar64 sets out = out * scalar */
 static void felem_scalar64(felem out, limb scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
     out[4] *= scalar;
     out[5] *= scalar;
     out[6] *= scalar;
     out[7] *= scalar;
     out[8] *= scalar;
 }
 
 /* felem_scalar128 sets out = out * scalar */
 static void felem_scalar128(largefelem out, limb scalar)
 {
     out[0] *= scalar;
     out[1] *= scalar;
     out[2] *= scalar;
     out[3] *= scalar;
     out[4] *= scalar;
     out[5] *= scalar;
     out[6] *= scalar;
     out[7] *= scalar;
     out[8] *= scalar;
 }
 
 /*-
  * felem_neg sets |out| to |-in|
  * On entry:
  *   in[i] < 2^59 + 2^14
  * On exit:
  *   out[i] < 2^62
  */
 static void felem_neg(felem out, const felem in)
 {
     /* In order to prevent underflow, we subtract from 0 mod p. */
     static const limb two62m3 = (((limb) 1) << 62) - (((limb) 1) << 5);
     static const limb two62m2 = (((limb) 1) << 62) - (((limb) 1) << 4);
 
     out[0] = two62m3 - in[0];
     out[1] = two62m2 - in[1];
     out[2] = two62m2 - in[2];
     out[3] = two62m2 - in[3];
     out[4] = two62m2 - in[4];
     out[5] = two62m2 - in[5];
     out[6] = two62m2 - in[6];
     out[7] = two62m2 - in[7];
     out[8] = two62m2 - in[8];
 }
 
 /*-
  * felem_diff64 subtracts |in| from |out|
  * On entry:
  *   in[i] < 2^59 + 2^14
  * On exit:
  *   out[i] < out[i] + 2^62
  */
 static void felem_diff64(felem out, const felem in)
 {
     /*
      * In order to prevent underflow, we add 0 mod p before subtracting.
      */
     static const limb two62m3 = (((limb) 1) << 62) - (((limb) 1) << 5);
     static const limb two62m2 = (((limb) 1) << 62) - (((limb) 1) << 4);
 
     out[0] += two62m3 - in[0];
     out[1] += two62m2 - in[1];
     out[2] += two62m2 - in[2];
     out[3] += two62m2 - in[3];
     out[4] += two62m2 - in[4];
     out[5] += two62m2 - in[5];
     out[6] += two62m2 - in[6];
     out[7] += two62m2 - in[7];
     out[8] += two62m2 - in[8];
 }
 
 /*-
  * felem_diff_128_64 subtracts |in| from |out|
  * On entry:
  *   in[i] < 2^62 + 2^17
  * On exit:
  *   out[i] < out[i] + 2^63
  */
 static void felem_diff_128_64(largefelem out, const felem in)
 {
     /*
      * In order to prevent underflow, we add 0 mod p before subtracting.
      */
     static const limb two63m6 = (((limb) 1) << 62) - (((limb) 1) << 5);
     static const limb two63m5 = (((limb) 1) << 62) - (((limb) 1) << 4);
 
     out[0] += two63m6 - in[0];
     out[1] += two63m5 - in[1];
     out[2] += two63m5 - in[2];
     out[3] += two63m5 - in[3];
     out[4] += two63m5 - in[4];
     out[5] += two63m5 - in[5];
     out[6] += two63m5 - in[6];
     out[7] += two63m5 - in[7];
     out[8] += two63m5 - in[8];
 }
 
 /*-
  * felem_diff_128_64 subtracts |in| from |out|
  * On entry:
  *   in[i] < 2^126
  * On exit:
  *   out[i] < out[i] + 2^127 - 2^69
  */
 static void felem_diff128(largefelem out, const largefelem in)
 {
     /*
      * In order to prevent underflow, we add 0 mod p before subtracting.
      */
     static const uint128_t two127m70 =
         (((uint128_t) 1) << 127) - (((uint128_t) 1) << 70);
     static const uint128_t two127m69 =
         (((uint128_t) 1) << 127) - (((uint128_t) 1) << 69);
 
     out[0] += (two127m70 - in[0]);
     out[1] += (two127m69 - in[1]);
     out[2] += (two127m69 - in[2]);
     out[3] += (two127m69 - in[3]);
     out[4] += (two127m69 - in[4]);
     out[5] += (two127m69 - in[5]);
     out[6] += (two127m69 - in[6]);
     out[7] += (two127m69 - in[7]);
     out[8] += (two127m69 - in[8]);
 }
 
 /*-
  * felem_square sets |out| = |in|^2
  * On entry:
  *   in[i] < 2^62
  * On exit:
  *   out[i] < 17 * max(in[i]) * max(in[i])
  */
 static void felem_square(largefelem out, const felem in)
 {
     felem inx2, inx4;
     felem_scalar(inx2, in, 2);
     felem_scalar(inx4, in, 4);
 
     /*-
      * We have many cases were we want to do
      *   in[x] * in[y] +
      *   in[y] * in[x]
      * This is obviously just
      *   2 * in[x] * in[y]
      * However, rather than do the doubling on the 128 bit result, we
      * double one of the inputs to the multiplication by reading from
      * |inx2|
      */
 
     out[0] = ((uint128_t) in[0]) * in[0];
     out[1] = ((uint128_t) in[0]) * inx2[1];
     out[2] = ((uint128_t) in[0]) * inx2[2] + ((uint128_t) in[1]) * in[1];
     out[3] = ((uint128_t) in[0]) * inx2[3] + ((uint128_t) in[1]) * inx2[2];
     out[4] = ((uint128_t) in[0]) * inx2[4] +
         ((uint128_t) in[1]) * inx2[3] + ((uint128_t) in[2]) * in[2];
     out[5] = ((uint128_t) in[0]) * inx2[5] +
         ((uint128_t) in[1]) * inx2[4] + ((uint128_t) in[2]) * inx2[3];
     out[6] = ((uint128_t) in[0]) * inx2[6] +
         ((uint128_t) in[1]) * inx2[5] +
         ((uint128_t) in[2]) * inx2[4] + ((uint128_t) in[3]) * in[3];
     out[7] = ((uint128_t) in[0]) * inx2[7] +
         ((uint128_t) in[1]) * inx2[6] +
         ((uint128_t) in[2]) * inx2[5] + ((uint128_t) in[3]) * inx2[4];
     out[8] = ((uint128_t) in[0]) * inx2[8] +
         ((uint128_t) in[1]) * inx2[7] +
         ((uint128_t) in[2]) * inx2[6] +
         ((uint128_t) in[3]) * inx2[5] + ((uint128_t) in[4]) * in[4];
 
     /*
      * The remaining limbs fall above 2^521, with the first falling at 2^522.
      * They correspond to locations one bit up from the limbs produced above
      * so we would have to multiply by two to align them. Again, rather than
      * operate on the 128-bit result, we double one of the inputs to the
      * multiplication. If we want to double for both this reason, and the
      * reason above, then we end up multiplying by four.
      */
 
     /* 9 */
     out[0] += ((uint128_t) in[1]) * inx4[8] +
         ((uint128_t) in[2]) * inx4[7] +
         ((uint128_t) in[3]) * inx4[6] + ((uint128_t) in[4]) * inx4[5];
 
     /* 10 */
     out[1] += ((uint128_t) in[2]) * inx4[8] +
         ((uint128_t) in[3]) * inx4[7] +
         ((uint128_t) in[4]) * inx4[6] + ((uint128_t) in[5]) * inx2[5];
 
     /* 11 */
     out[2] += ((uint128_t) in[3]) * inx4[8] +
         ((uint128_t) in[4]) * inx4[7] + ((uint128_t) in[5]) * inx4[6];
 
     /* 12 */
     out[3] += ((uint128_t) in[4]) * inx4[8] +
         ((uint128_t) in[5]) * inx4[7] + ((uint128_t) in[6]) * inx2[6];
 
     /* 13 */
     out[4] += ((uint128_t) in[5]) * inx4[8] + ((uint128_t) in[6]) * inx4[7];
 
     /* 14 */
     out[5] += ((uint128_t) in[6]) * inx4[8] + ((uint128_t) in[7]) * inx2[7];
 
     /* 15 */
     out[6] += ((uint128_t) in[7]) * inx4[8];
 
     /* 16 */
     out[7] += ((uint128_t) in[8]) * inx2[8];
 }
 
 /*-
  * felem_mul sets |out| = |in1| * |in2|
  * On entry:
  *   in1[i] < 2^64
  *   in2[i] < 2^63
  * On exit:
  *   out[i] < 17 * max(in1[i]) * max(in2[i])
  */
 static void felem_mul(largefelem out, const felem in1, const felem in2)
 {
     felem in2x2;
     felem_scalar(in2x2, in2, 2);
 
     out[0] = ((uint128_t) in1[0]) * in2[0];
 
     out[1] = ((uint128_t) in1[0]) * in2[1] + ((uint128_t) in1[1]) * in2[0];
 
     out[2] = ((uint128_t) in1[0]) * in2[2] +
         ((uint128_t) in1[1]) * in2[1] + ((uint128_t) in1[2]) * in2[0];
 
     out[3] = ((uint128_t) in1[0]) * in2[3] +
         ((uint128_t) in1[1]) * in2[2] +
         ((uint128_t) in1[2]) * in2[1] + ((uint128_t) in1[3]) * in2[0];
 
     out[4] = ((uint128_t) in1[0]) * in2[4] +
         ((uint128_t) in1[1]) * in2[3] +
         ((uint128_t) in1[2]) * in2[2] +
         ((uint128_t) in1[3]) * in2[1] + ((uint128_t) in1[4]) * in2[0];
 
     out[5] = ((uint128_t) in1[0]) * in2[5] +
         ((uint128_t) in1[1]) * in2[4] +
         ((uint128_t) in1[2]) * in2[3] +
         ((uint128_t) in1[3]) * in2[2] +
         ((uint128_t) in1[4]) * in2[1] + ((uint128_t) in1[5]) * in2[0];
 
     out[6] = ((uint128_t) in1[0]) * in2[6] +
         ((uint128_t) in1[1]) * in2[5] +
         ((uint128_t) in1[2]) * in2[4] +
         ((uint128_t) in1[3]) * in2[3] +
         ((uint128_t) in1[4]) * in2[2] +
         ((uint128_t) in1[5]) * in2[1] + ((uint128_t) in1[6]) * in2[0];
 
     out[7] = ((uint128_t) in1[0]) * in2[7] +
         ((uint128_t) in1[1]) * in2[6] +
         ((uint128_t) in1[2]) * in2[5] +
         ((uint128_t) in1[3]) * in2[4] +
         ((uint128_t) in1[4]) * in2[3] +
         ((uint128_t) in1[5]) * in2[2] +
         ((uint128_t) in1[6]) * in2[1] + ((uint128_t) in1[7]) * in2[0];
 
     out[8] = ((uint128_t) in1[0]) * in2[8] +
         ((uint128_t) in1[1]) * in2[7] +
         ((uint128_t) in1[2]) * in2[6] +
         ((uint128_t) in1[3]) * in2[5] +
         ((uint128_t) in1[4]) * in2[4] +
         ((uint128_t) in1[5]) * in2[3] +
         ((uint128_t) in1[6]) * in2[2] +
         ((uint128_t) in1[7]) * in2[1] + ((uint128_t) in1[8]) * in2[0];
 
     /* See comment in felem_square about the use of in2x2 here */
 
     out[0] += ((uint128_t) in1[1]) * in2x2[8] +
         ((uint128_t) in1[2]) * in2x2[7] +
         ((uint128_t) in1[3]) * in2x2[6] +
         ((uint128_t) in1[4]) * in2x2[5] +
         ((uint128_t) in1[5]) * in2x2[4] +
         ((uint128_t) in1[6]) * in2x2[3] +
         ((uint128_t) in1[7]) * in2x2[2] + ((uint128_t) in1[8]) * in2x2[1];
 
     out[1] += ((uint128_t) in1[2]) * in2x2[8] +
         ((uint128_t) in1[3]) * in2x2[7] +
         ((uint128_t) in1[4]) * in2x2[6] +
         ((uint128_t) in1[5]) * in2x2[5] +
         ((uint128_t) in1[6]) * in2x2[4] +
         ((uint128_t) in1[7]) * in2x2[3] + ((uint128_t) in1[8]) * in2x2[2];
 
     out[2] += ((uint128_t) in1[3]) * in2x2[8] +
         ((uint128_t) in1[4]) * in2x2[7] +
         ((uint128_t) in1[5]) * in2x2[6] +
         ((uint128_t) in1[6]) * in2x2[5] +
         ((uint128_t) in1[7]) * in2x2[4] + ((uint128_t) in1[8]) * in2x2[3];
 
     out[3] += ((uint128_t) in1[4]) * in2x2[8] +
         ((uint128_t) in1[5]) * in2x2[7] +
         ((uint128_t) in1[6]) * in2x2[6] +
         ((uint128_t) in1[7]) * in2x2[5] + ((uint128_t) in1[8]) * in2x2[4];
 
     out[4] += ((uint128_t) in1[5]) * in2x2[8] +
         ((uint128_t) in1[6]) * in2x2[7] +
         ((uint128_t) in1[7]) * in2x2[6] + ((uint128_t) in1[8]) * in2x2[5];
 
     out[5] += ((uint128_t) in1[6]) * in2x2[8] +
         ((uint128_t) in1[7]) * in2x2[7] + ((uint128_t) in1[8]) * in2x2[6];
 
     out[6] += ((uint128_t) in1[7]) * in2x2[8] +
         ((uint128_t) in1[8]) * in2x2[7];
 
     out[7] += ((uint128_t) in1[8]) * in2x2[8];
 }
 
 static const limb bottom52bits = 0xfffffffffffff;
 
 /*-
  * felem_reduce converts a largefelem to an felem.
  * On entry:
  *   in[i] < 2^128
  * On exit:
  *   out[i] < 2^59 + 2^14
  */
 static void felem_reduce(felem out, const largefelem in)
 {
     u64 overflow1, overflow2;
 
     out[0] = ((limb) in[0]) & bottom58bits;
     out[1] = ((limb) in[1]) & bottom58bits;
     out[2] = ((limb) in[2]) & bottom58bits;
     out[3] = ((limb) in[3]) & bottom58bits;
     out[4] = ((limb) in[4]) & bottom58bits;
     out[5] = ((limb) in[5]) & bottom58bits;
     out[6] = ((limb) in[6]) & bottom58bits;
     out[7] = ((limb) in[7]) & bottom58bits;
     out[8] = ((limb) in[8]) & bottom58bits;
 
     /* out[i] < 2^58 */
 
     out[1] += ((limb) in[0]) >> 58;
     out[1] += (((limb) (in[0] >> 64)) & bottom52bits) << 6;
     /*-
      * out[1] < 2^58 + 2^6 + 2^58
      *        = 2^59 + 2^6
      */
     out[2] += ((limb) (in[0] >> 64)) >> 52;
 
     out[2] += ((limb) in[1]) >> 58;
     out[2] += (((limb) (in[1] >> 64)) & bottom52bits) << 6;
     out[3] += ((limb) (in[1] >> 64)) >> 52;
 
     out[3] += ((limb) in[2]) >> 58;
     out[3] += (((limb) (in[2] >> 64)) & bottom52bits) << 6;
     out[4] += ((limb) (in[2] >> 64)) >> 52;
 
     out[4] += ((limb) in[3]) >> 58;
     out[4] += (((limb) (in[3] >> 64)) & bottom52bits) << 6;
     out[5] += ((limb) (in[3] >> 64)) >> 52;
 
     out[5] += ((limb) in[4]) >> 58;
     out[5] += (((limb) (in[4] >> 64)) & bottom52bits) << 6;
     out[6] += ((limb) (in[4] >> 64)) >> 52;
 
     out[6] += ((limb) in[5]) >> 58;
     out[6] += (((limb) (in[5] >> 64)) & bottom52bits) << 6;
     out[7] += ((limb) (in[5] >> 64)) >> 52;
 
     out[7] += ((limb) in[6]) >> 58;
     out[7] += (((limb) (in[6] >> 64)) & bottom52bits) << 6;
     out[8] += ((limb) (in[6] >> 64)) >> 52;
 
     out[8] += ((limb) in[7]) >> 58;
     out[8] += (((limb) (in[7] >> 64)) & bottom52bits) << 6;
     /*-
      * out[x > 1] < 2^58 + 2^6 + 2^58 + 2^12
      *            < 2^59 + 2^13
      */
     overflow1 = ((limb) (in[7] >> 64)) >> 52;
 
     overflow1 += ((limb) in[8]) >> 58;
     overflow1 += (((limb) (in[8] >> 64)) & bottom52bits) << 6;
     overflow2 = ((limb) (in[8] >> 64)) >> 52;
 
     overflow1 <<= 1;            /* overflow1 < 2^13 + 2^7 + 2^59 */
     overflow2 <<= 1;            /* overflow2 < 2^13 */
 
     out[0] += overflow1;        /* out[0] < 2^60 */
     out[1] += overflow2;        /* out[1] < 2^59 + 2^6 + 2^13 */
 
     out[1] += out[0] >> 58;
     out[0] &= bottom58bits;
     /*-
      * out[0] < 2^58
      * out[1] < 2^59 + 2^6 + 2^13 + 2^2
      *        < 2^59 + 2^14
      */
 }
 
 static void felem_square_reduce(felem out, const felem in)
 {
     largefelem tmp;
     felem_square(tmp, in);
     felem_reduce(out, tmp);
 }
 
 static void felem_mul_reduce(felem out, const felem in1, const felem in2)
 {
     largefelem tmp;
     felem_mul(tmp, in1, in2);
     felem_reduce(out, tmp);
 }
 
 /*-
  * felem_inv calculates |out| = |in|^{-1}
  *
  * Based on Fermat's Little Theorem:
  *   a^p = a (mod p)
  *   a^{p-1} = 1 (mod p)
  *   a^{p-2} = a^{-1} (mod p)
  */
 static void felem_inv(felem out, const felem in)
 {
     felem ftmp, ftmp2, ftmp3, ftmp4;
     largefelem tmp;
     unsigned i;
 
     felem_square(tmp, in);
     felem_reduce(ftmp, tmp);    /* 2^1 */
     felem_mul(tmp, in, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^2 - 2^0 */
     felem_assign(ftmp2, ftmp);
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^3 - 2^1 */
     felem_mul(tmp, in, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^3 - 2^0 */
     felem_square(tmp, ftmp);
     felem_reduce(ftmp, tmp);    /* 2^4 - 2^1 */
 
     felem_square(tmp, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^3 - 2^1 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp3, tmp);   /* 2^4 - 2^2 */
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^4 - 2^0 */
 
     felem_assign(ftmp2, ftmp3);
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp3, tmp);   /* 2^5 - 2^1 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp3, tmp);   /* 2^6 - 2^2 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp3, tmp);   /* 2^7 - 2^3 */
     felem_square(tmp, ftmp3);
     felem_reduce(ftmp3, tmp);   /* 2^8 - 2^4 */
     felem_assign(ftmp4, ftmp3);
     felem_mul(tmp, ftmp3, ftmp);
     felem_reduce(ftmp4, tmp);   /* 2^8 - 2^1 */
     felem_square(tmp, ftmp4);
     felem_reduce(ftmp4, tmp);   /* 2^9 - 2^2 */
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^8 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 8; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^16 - 2^8 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^16 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 16; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^32 - 2^16 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^32 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 32; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^64 - 2^32 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^64 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 64; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^128 - 2^64 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^128 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 128; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^256 - 2^128 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^256 - 2^0 */
     felem_assign(ftmp2, ftmp3);
 
     for (i = 0; i < 256; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^512 - 2^256 */
     }
     felem_mul(tmp, ftmp3, ftmp2);
     felem_reduce(ftmp3, tmp);   /* 2^512 - 2^0 */
 
     for (i = 0; i < 9; i++) {
         felem_square(tmp, ftmp3);
         felem_reduce(ftmp3, tmp); /* 2^521 - 2^9 */
     }
     felem_mul(tmp, ftmp3, ftmp4);
     felem_reduce(ftmp3, tmp);   /* 2^512 - 2^2 */
     felem_mul(tmp, ftmp3, in);
     felem_reduce(out, tmp);     /* 2^512 - 3 */
 }
 
 /* This is 2^521-1, expressed as an felem */
 static const felem kPrime = {
     0x03ffffffffffffff, 0x03ffffffffffffff, 0x03ffffffffffffff,
     0x03ffffffffffffff, 0x03ffffffffffffff, 0x03ffffffffffffff,
     0x03ffffffffffffff, 0x03ffffffffffffff, 0x01ffffffffffffff
 };
 
 /*-
  * felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
  * otherwise.
  * On entry:
  *   in[i] < 2^59 + 2^14
  */
 static limb felem_is_zero(const felem in)
 {
     felem ftmp;
     limb is_zero, is_p;
     felem_assign(ftmp, in);
 
     ftmp[0] += ftmp[8] >> 57;
     ftmp[8] &= bottom57bits;
     /* ftmp[8] < 2^57 */
     ftmp[1] += ftmp[0] >> 58;
     ftmp[0] &= bottom58bits;
     ftmp[2] += ftmp[1] >> 58;
     ftmp[1] &= bottom58bits;
     ftmp[3] += ftmp[2] >> 58;
     ftmp[2] &= bottom58bits;
     ftmp[4] += ftmp[3] >> 58;
     ftmp[3] &= bottom58bits;
     ftmp[5] += ftmp[4] >> 58;
     ftmp[4] &= bottom58bits;
     ftmp[6] += ftmp[5] >> 58;
     ftmp[5] &= bottom58bits;
     ftmp[7] += ftmp[6] >> 58;
     ftmp[6] &= bottom58bits;
     ftmp[8] += ftmp[7] >> 58;
     ftmp[7] &= bottom58bits;
     /* ftmp[8] < 2^57 + 4 */
 
     /*
      * The ninth limb of 2*(2^521-1) is 0x03ffffffffffffff, which is greater
      * than our bound for ftmp[8]. Therefore we only have to check if the
      * zero is zero or 2^521-1.
      */
 
     is_zero = 0;
     is_zero |= ftmp[0];
     is_zero |= ftmp[1];
     is_zero |= ftmp[2];
     is_zero |= ftmp[3];
     is_zero |= ftmp[4];
     is_zero |= ftmp[5];
     is_zero |= ftmp[6];
     is_zero |= ftmp[7];
     is_zero |= ftmp[8];
 
     is_zero--;
     /*
      * We know that ftmp[i] < 2^63, therefore the only way that the top bit
      * can be set is if is_zero was 0 before the decrement.
      */
     is_zero = ((s64) is_zero) >> 63;
 
     is_p = ftmp[0] ^ kPrime[0];
     is_p |= ftmp[1] ^ kPrime[1];
     is_p |= ftmp[2] ^ kPrime[2];
     is_p |= ftmp[3] ^ kPrime[3];
     is_p |= ftmp[4] ^ kPrime[4];
     is_p |= ftmp[5] ^ kPrime[5];
     is_p |= ftmp[6] ^ kPrime[6];
     is_p |= ftmp[7] ^ kPrime[7];
     is_p |= ftmp[8] ^ kPrime[8];
 
     is_p--;
     is_p = ((s64) is_p) >> 63;
 
     is_zero |= is_p;
     return is_zero;
 }
 
 static int felem_is_zero_int(const felem in)
 {
     return (int)(felem_is_zero(in) & ((limb) 1));
 }
 
 /*-
  * felem_contract converts |in| to its unique, minimal representation.
  * On entry:
  *   in[i] < 2^59 + 2^14
  */
 static void felem_contract(felem out, const felem in)
 {
     limb is_p, is_greater, sign;
     static const limb two58 = ((limb) 1) << 58;
 
     felem_assign(out, in);
 
     out[0] += out[8] >> 57;
     out[8] &= bottom57bits;
     /* out[8] < 2^57 */
     out[1] += out[0] >> 58;
     out[0] &= bottom58bits;
     out[2] += out[1] >> 58;
     out[1] &= bottom58bits;
     out[3] += out[2] >> 58;
     out[2] &= bottom58bits;
     out[4] += out[3] >> 58;
     out[3] &= bottom58bits;
     out[5] += out[4] >> 58;
     out[4] &= bottom58bits;
     out[6] += out[5] >> 58;
     out[5] &= bottom58bits;
     out[7] += out[6] >> 58;
     out[6] &= bottom58bits;
     out[8] += out[7] >> 58;
     out[7] &= bottom58bits;
     /* out[8] < 2^57 + 4 */
 
     /*
      * If the value is greater than 2^521-1 then we have to subtract 2^521-1
      * out. See the comments in felem_is_zero regarding why we don't test for
      * other multiples of the prime.
      */
 
     /*
      * First, if |out| is equal to 2^521-1, we subtract it out to get zero.
      */
 
     is_p = out[0] ^ kPrime[0];
     is_p |= out[1] ^ kPrime[1];
     is_p |= out[2] ^ kPrime[2];
     is_p |= out[3] ^ kPrime[3];
     is_p |= out[4] ^ kPrime[4];
     is_p |= out[5] ^ kPrime[5];
     is_p |= out[6] ^ kPrime[6];
     is_p |= out[7] ^ kPrime[7];
     is_p |= out[8] ^ kPrime[8];
 
     is_p--;
     is_p &= is_p << 32;
     is_p &= is_p << 16;
     is_p &= is_p << 8;
     is_p &= is_p << 4;
     is_p &= is_p << 2;
     is_p &= is_p << 1;
     is_p = ((s64) is_p) >> 63;
     is_p = ~is_p;
 
     /* is_p is 0 iff |out| == 2^521-1 and all ones otherwise */
 
     out[0] &= is_p;
     out[1] &= is_p;
     out[2] &= is_p;
     out[3] &= is_p;
     out[4] &= is_p;
     out[5] &= is_p;
     out[6] &= is_p;
     out[7] &= is_p;
     out[8] &= is_p;
 
     /*
      * In order to test that |out| >= 2^521-1 we need only test if out[8] >>
      * 57 is greater than zero as (2^521-1) + x >= 2^522
      */
     is_greater = out[8] >> 57;
     is_greater |= is_greater << 32;
     is_greater |= is_greater << 16;
     is_greater |= is_greater << 8;
     is_greater |= is_greater << 4;
     is_greater |= is_greater << 2;
     is_greater |= is_greater << 1;
     is_greater = ((s64) is_greater) >> 63;
 
     out[0] -= kPrime[0] & is_greater;
     out[1] -= kPrime[1] & is_greater;
     out[2] -= kPrime[2] & is_greater;
     out[3] -= kPrime[3] & is_greater;
     out[4] -= kPrime[4] & is_greater;
     out[5] -= kPrime[5] & is_greater;
     out[6] -= kPrime[6] & is_greater;
     out[7] -= kPrime[7] & is_greater;
     out[8] -= kPrime[8] & is_greater;
 
     /* Eliminate negative coefficients */
     sign = -(out[0] >> 63);
     out[0] += (two58 & sign);
     out[1] -= (1 & sign);
     sign = -(out[1] >> 63);
     out[1] += (two58 & sign);
     out[2] -= (1 & sign);
     sign = -(out[2] >> 63);
     out[2] += (two58 & sign);
     out[3] -= (1 & sign);
     sign = -(out[3] >> 63);
     out[3] += (two58 & sign);
     out[4] -= (1 & sign);
     sign = -(out[4] >> 63);
     out[4] += (two58 & sign);
     out[5] -= (1 & sign);
     sign = -(out[0] >> 63);
     out[5] += (two58 & sign);
     out[6] -= (1 & sign);
     sign = -(out[6] >> 63);
     out[6] += (two58 & sign);
     out[7] -= (1 & sign);
     sign = -(out[7] >> 63);
     out[7] += (two58 & sign);
     out[8] -= (1 & sign);
     sign = -(out[5] >> 63);
     out[5] += (two58 & sign);
     out[6] -= (1 & sign);
     sign = -(out[6] >> 63);
     out[6] += (two58 & sign);
     out[7] -= (1 & sign);
     sign = -(out[7] >> 63);
     out[7] += (two58 & sign);
     out[8] -= (1 & sign);
 }
 
 /*-
  * Group operations
  * ----------------
  *
  * Building on top of the field operations we have the operations on the
  * elliptic curve group itself. Points on the curve are represented in Jacobian
  * coordinates */
 
 /*-
  * point_double calcuates 2*(x_in, y_in, z_in)
  *
  * The method is taken from:
  *   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
  *
  * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
  * while x_out == y_in is not (maybe this works, but it's not tested). */
 static void
 point_double(felem x_out, felem y_out, felem z_out,
              const felem x_in, const felem y_in, const felem z_in)
 {
     largefelem tmp, tmp2;
     felem delta, gamma, beta, alpha, ftmp, ftmp2;
 
     felem_assign(ftmp, x_in);
     felem_assign(ftmp2, x_in);
 
     /* delta = z^2 */
     felem_square(tmp, z_in);
     felem_reduce(delta, tmp);   /* delta[i] < 2^59 + 2^14 */
 
     /* gamma = y^2 */
     felem_square(tmp, y_in);
     felem_reduce(gamma, tmp);   /* gamma[i] < 2^59 + 2^14 */
 
     /* beta = x*gamma */
     felem_mul(tmp, x_in, gamma);
     felem_reduce(beta, tmp);    /* beta[i] < 2^59 + 2^14 */
 
     /* alpha = 3*(x-delta)*(x+delta) */
     felem_diff64(ftmp, delta);
     /* ftmp[i] < 2^61 */
     felem_sum64(ftmp2, delta);
     /* ftmp2[i] < 2^60 + 2^15 */
     felem_scalar64(ftmp2, 3);
     /* ftmp2[i] < 3*2^60 + 3*2^15 */
     felem_mul(tmp, ftmp, ftmp2);
     /*-
      * tmp[i] < 17(3*2^121 + 3*2^76)
      *        = 61*2^121 + 61*2^76
      *        < 64*2^121 + 64*2^76
      *        = 2^127 + 2^82
      *        < 2^128
      */
     felem_reduce(alpha, tmp);
 
     /* x' = alpha^2 - 8*beta */
     felem_square(tmp, alpha);
     /*
      * tmp[i] < 17*2^120 < 2^125
      */
     felem_assign(ftmp, beta);
     felem_scalar64(ftmp, 8);
     /* ftmp[i] < 2^62 + 2^17 */
     felem_diff_128_64(tmp, ftmp);
     /* tmp[i] < 2^125 + 2^63 + 2^62 + 2^17 */
     felem_reduce(x_out, tmp);
 
     /* z' = (y + z)^2 - gamma - delta */
     felem_sum64(delta, gamma);
     /* delta[i] < 2^60 + 2^15 */
     felem_assign(ftmp, y_in);
     felem_sum64(ftmp, z_in);
     /* ftmp[i] < 2^60 + 2^15 */
     felem_square(tmp, ftmp);
     /*
      * tmp[i] < 17(2^122) < 2^127
      */
     felem_diff_128_64(tmp, delta);
     /* tmp[i] < 2^127 + 2^63 */
     felem_reduce(z_out, tmp);
 
     /* y' = alpha*(4*beta - x') - 8*gamma^2 */
     felem_scalar64(beta, 4);
     /* beta[i] < 2^61 + 2^16 */
     felem_diff64(beta, x_out);
     /* beta[i] < 2^61 + 2^60 + 2^16 */
     felem_mul(tmp, alpha, beta);
     /*-
      * tmp[i] < 17*((2^59 + 2^14)(2^61 + 2^60 + 2^16))
      *        = 17*(2^120 + 2^75 + 2^119 + 2^74 + 2^75 + 2^30)
      *        = 17*(2^120 + 2^119 + 2^76 + 2^74 + 2^30)
      *        < 2^128
      */
     felem_square(tmp2, gamma);
     /*-
      * tmp2[i] < 17*(2^59 + 2^14)^2
      *         = 17*(2^118 + 2^74 + 2^28)
      */
     felem_scalar128(tmp2, 8);
     /*-
      * tmp2[i] < 8*17*(2^118 + 2^74 + 2^28)
      *         = 2^125 + 2^121 + 2^81 + 2^77 + 2^35 + 2^31
      *         < 2^126
      */
     felem_diff128(tmp, tmp2);
     /*-
      * tmp[i] < 2^127 - 2^69 + 17(2^120 + 2^119 + 2^76 + 2^74 + 2^30)
      *        = 2^127 + 2^124 + 2^122 + 2^120 + 2^118 + 2^80 + 2^78 + 2^76 +
      *          2^74 + 2^69 + 2^34 + 2^30
      *        < 2^128
      */
     felem_reduce(y_out, tmp);
 }
 
 /* copy_conditional copies in to out iff mask is all ones. */
 static void copy_conditional(felem out, const felem in, limb mask)
 {
     unsigned i;
     for (i = 0; i < NLIMBS; ++i) {
         const limb tmp = mask & (in[i] ^ out[i]);
         out[i] ^= tmp;
     }
 }
 
 /*-
  * point_add calcuates (x1, y1, z1) + (x2, y2, z2)
  *
  * The method is taken from
  *   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
  * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
  *
  * This function includes a branch for checking whether the two input points
  * are equal (while not equal to the point at infinity). This case never
  * happens during single point multiplication, so there is no timing leak for
  * ECDH or ECDSA signing. */
 static void point_add(felem x3, felem y3, felem z3,
                       const felem x1, const felem y1, const felem z1,
                       const int mixed, const felem x2, const felem y2,
                       const felem z2)
 {
     felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
     largefelem tmp, tmp2;
     limb x_equal, y_equal, z1_is_zero, z2_is_zero;
 
     z1_is_zero = felem_is_zero(z1);
     z2_is_zero = felem_is_zero(z2);
 
     /* ftmp = z1z1 = z1**2 */
     felem_square(tmp, z1);
     felem_reduce(ftmp, tmp);
 
     if (!mixed) {
         /* ftmp2 = z2z2 = z2**2 */
         felem_square(tmp, z2);
         felem_reduce(ftmp2, tmp);
 
         /* u1 = ftmp3 = x1*z2z2 */
         felem_mul(tmp, x1, ftmp2);
         felem_reduce(ftmp3, tmp);
 
         /* ftmp5 = z1 + z2 */
         felem_assign(ftmp5, z1);
         felem_sum64(ftmp5, z2);
         /* ftmp5[i] < 2^61 */
 
         /* ftmp5 = (z1 + z2)**2 - z1z1 - z2z2 = 2*z1z2 */
         felem_square(tmp, ftmp5);
         /* tmp[i] < 17*2^122 */
         felem_diff_128_64(tmp, ftmp);
         /* tmp[i] < 17*2^122 + 2^63 */
         felem_diff_128_64(tmp, ftmp2);
         /* tmp[i] < 17*2^122 + 2^64 */
         felem_reduce(ftmp5, tmp);
 
         /* ftmp2 = z2 * z2z2 */
         felem_mul(tmp, ftmp2, z2);
         felem_reduce(ftmp2, tmp);
 
         /* s1 = ftmp6 = y1 * z2**3 */
         felem_mul(tmp, y1, ftmp2);
         felem_reduce(ftmp6, tmp);
     } else {
         /*
          * We'll assume z2 = 1 (special case z2 = 0 is handled later)
          */
 
         /* u1 = ftmp3 = x1*z2z2 */
         felem_assign(ftmp3, x1);
 
         /* ftmp5 = 2*z1z2 */
         felem_scalar(ftmp5, z1, 2);
 
         /* s1 = ftmp6 = y1 * z2**3 */
         felem_assign(ftmp6, y1);
     }
 
     /* u2 = x2*z1z1 */
     felem_mul(tmp, x2, ftmp);
     /* tmp[i] < 17*2^120 */
 
     /* h = ftmp4 = u2 - u1 */
     felem_diff_128_64(tmp, ftmp3);
     /* tmp[i] < 17*2^120 + 2^63 */
     felem_reduce(ftmp4, tmp);
 
     x_equal = felem_is_zero(ftmp4);
 
     /* z_out = ftmp5 * h */
     felem_mul(tmp, ftmp5, ftmp4);
     felem_reduce(z_out, tmp);
 
     /* ftmp = z1 * z1z1 */
     felem_mul(tmp, ftmp, z1);
     felem_reduce(ftmp, tmp);
 
     /* s2 = tmp = y2 * z1**3 */
     felem_mul(tmp, y2, ftmp);
     /* tmp[i] < 17*2^120 */
 
     /* r = ftmp5 = (s2 - s1)*2 */
     felem_diff_128_64(tmp, ftmp6);
     /* tmp[i] < 17*2^120 + 2^63 */
     felem_reduce(ftmp5, tmp);
     y_equal = felem_is_zero(ftmp5);
     felem_scalar64(ftmp5, 2);
     /* ftmp5[i] < 2^61 */
 
     if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
         point_double(x3, y3, z3, x1, y1, z1);
         return;
     }
 
     /* I = ftmp = (2h)**2 */
     felem_assign(ftmp, ftmp4);
     felem_scalar64(ftmp, 2);
     /* ftmp[i] < 2^61 */
     felem_square(tmp, ftmp);
     /* tmp[i] < 17*2^122 */
     felem_reduce(ftmp, tmp);
 
     /* J = ftmp2 = h * I */
     felem_mul(tmp, ftmp4, ftmp);
     felem_reduce(ftmp2, tmp);
 
     /* V = ftmp4 = U1 * I */
     felem_mul(tmp, ftmp3, ftmp);
     felem_reduce(ftmp4, tmp);
 
     /* x_out = r**2 - J - 2V */
     felem_square(tmp, ftmp5);
     /* tmp[i] < 17*2^122 */
     felem_diff_128_64(tmp, ftmp2);
     /* tmp[i] < 17*2^122 + 2^63 */
     felem_assign(ftmp3, ftmp4);
     felem_scalar64(ftmp4, 2);
     /* ftmp4[i] < 2^61 */
     felem_diff_128_64(tmp, ftmp4);
     /* tmp[i] < 17*2^122 + 2^64 */
     felem_reduce(x_out, tmp);
 
     /* y_out = r(V-x_out) - 2 * s1 * J */
     felem_diff64(ftmp3, x_out);
     /*
      * ftmp3[i] < 2^60 + 2^60 = 2^61
      */
     felem_mul(tmp, ftmp5, ftmp3);
     /* tmp[i] < 17*2^122 */
     felem_mul(tmp2, ftmp6, ftmp2);
     /* tmp2[i] < 17*2^120 */
     felem_scalar128(tmp2, 2);
     /* tmp2[i] < 17*2^121 */
     felem_diff128(tmp, tmp2);
         /*-
          * tmp[i] < 2^127 - 2^69 + 17*2^122
          *        = 2^126 - 2^122 - 2^6 - 2^2 - 1
          *        < 2^127
          */
     felem_reduce(y_out, tmp);
 
     copy_conditional(x_out, x2, z1_is_zero);
     copy_conditional(x_out, x1, z2_is_zero);
     copy_conditional(y_out, y2, z1_is_zero);
     copy_conditional(y_out, y1, z2_is_zero);
     copy_conditional(z_out, z2, z1_is_zero);
     copy_conditional(z_out, z1, z2_is_zero);
     felem_assign(x3, x_out);
     felem_assign(y3, y_out);
     felem_assign(z3, z_out);
 }
 
 /*-
  * Base point pre computation
  * --------------------------
  *
  * Two different sorts of precomputed tables are used in the following code.
  * Each contain various points on the curve, where each point is three field
  * elements (x, y, z).
  *
  * For the base point table, z is usually 1 (0 for the point at infinity).
  * This table has 16 elements:
  * index | bits    | point
  * ------+---------+------------------------------
  *     0 | 0 0 0 0 | 0G
  *     1 | 0 0 0 1 | 1G
  *     2 | 0 0 1 0 | 2^130G
  *     3 | 0 0 1 1 | (2^130 + 1)G
  *     4 | 0 1 0 0 | 2^260G
  *     5 | 0 1 0 1 | (2^260 + 1)G
  *     6 | 0 1 1 0 | (2^260 + 2^130)G
  *     7 | 0 1 1 1 | (2^260 + 2^130 + 1)G
  *     8 | 1 0 0 0 | 2^390G
  *     9 | 1 0 0 1 | (2^390 + 1)G
  *    10 | 1 0 1 0 | (2^390 + 2^130)G
  *    11 | 1 0 1 1 | (2^390 + 2^130 + 1)G
  *    12 | 1 1 0 0 | (2^390 + 2^260)G
  *    13 | 1 1 0 1 | (2^390 + 2^260 + 1)G
  *    14 | 1 1 1 0 | (2^390 + 2^260 + 2^130)G
  *    15 | 1 1 1 1 | (2^390 + 2^260 + 2^130 + 1)G
  *
  * The reason for this is so that we can clock bits into four different
  * locations when doing simple scalar multiplies against the base point.
  *
  * Tables for other points have table[i] = iG for i in 0 .. 16. */
 
 /* gmul is the table of precomputed base points */
 static const felem gmul[16][3] = { {{0, 0, 0, 0, 0, 0, 0, 0, 0},
                                     {0, 0, 0, 0, 0, 0, 0, 0, 0},
                                     {0, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x017e7e31c2e5bd66, 0x022cf0615a90a6fe, 0x00127a2ffa8de334,
   0x01dfbf9d64a3f877, 0x006b4d3dbaa14b5e, 0x014fed487e0a2bd8,
   0x015b4429c6481390, 0x03a73678fb2d988e, 0x00c6858e06b70404},
  {0x00be94769fd16650, 0x031c21a89cb09022, 0x039013fad0761353,
   0x02657bd099031542, 0x03273e662c97ee72, 0x01e6d11a05ebef45,
   0x03d1bd998f544495, 0x03001172297ed0b1, 0x011839296a789a3b},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x0373faacbc875bae, 0x00f325023721c671, 0x00f666fd3dbde5ad,
   0x01a6932363f88ea7, 0x01fc6d9e13f9c47b, 0x03bcbffc2bbf734e,
   0x013ee3c3647f3a92, 0x029409fefe75d07d, 0x00ef9199963d85e5},
  {0x011173743ad5b178, 0x02499c7c21bf7d46, 0x035beaeabb8b1a58,
   0x00f989c4752ea0a3, 0x0101e1de48a9c1a3, 0x01a20076be28ba6c,
   0x02f8052e5eb2de95, 0x01bfe8f82dea117c, 0x0160074d3c36ddb7},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x012f3fc373393b3b, 0x03d3d6172f1419fa, 0x02adc943c0b86873,
   0x00d475584177952b, 0x012a4d1673750ee2, 0x00512517a0f13b0c,
   0x02b184671a7b1734, 0x0315b84236f1a50a, 0x00a4afc472edbdb9},
  {0x00152a7077f385c4, 0x03044007d8d1c2ee, 0x0065829d61d52b52,
   0x00494ff6b6631d0d, 0x00a11d94d5f06bcf, 0x02d2f89474d9282e,
   0x0241c5727c06eeb9, 0x0386928710fbdb9d, 0x01f883f727b0dfbe},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x019b0c3c9185544d, 0x006243a37c9d97db, 0x02ee3cbe030a2ad2,
   0x00cfdd946bb51e0d, 0x0271c00932606b91, 0x03f817d1ec68c561,
   0x03f37009806a369c, 0x03c1f30baf184fd5, 0x01091022d6d2f065},
  {0x0292c583514c45ed, 0x0316fca51f9a286c, 0x00300af507c1489a,
   0x0295f69008298cf1, 0x02c0ed8274943d7b, 0x016509b9b47a431e,
   0x02bc9de9634868ce, 0x005b34929bffcb09, 0x000c1a0121681524},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x0286abc0292fb9f2, 0x02665eee9805b3f7, 0x01ed7455f17f26d6,
   0x0346355b83175d13, 0x006284944cd0a097, 0x0191895bcdec5e51,
   0x02e288370afda7d9, 0x03b22312bfefa67a, 0x01d104d3fc0613fe},
  {0x0092421a12f7e47f, 0x0077a83fa373c501, 0x03bd25c5f696bd0d,
   0x035c41e4d5459761, 0x01ca0d1742b24f53, 0x00aaab27863a509c,
   0x018b6de47df73917, 0x025c0b771705cd01, 0x01fd51d566d760a7},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x01dd92ff6b0d1dbd, 0x039c5e2e8f8afa69, 0x0261ed13242c3b27,
   0x0382c6e67026e6a0, 0x01d60b10be2089f9, 0x03c15f3dce86723f,
   0x03c764a32d2a062d, 0x017307eac0fad056, 0x018207c0b96c5256},
  {0x0196a16d60e13154, 0x03e6ce74c0267030, 0x00ddbf2b4e52a5aa,
   0x012738241bbf31c8, 0x00ebe8dc04685a28, 0x024c2ad6d380d4a2,
   0x035ee062a6e62d0e, 0x0029ed74af7d3a0f, 0x00eef32aec142ebd},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x00c31ec398993b39, 0x03a9f45bcda68253, 0x00ac733c24c70890,
   0x00872b111401ff01, 0x01d178c23195eafb, 0x03bca2c816b87f74,
   0x0261a9af46fbad7a, 0x0324b2a8dd3d28f9, 0x00918121d8f24e23},
  {0x032bc8c1ca983cd7, 0x00d869dfb08fc8c6, 0x01693cb61fce1516,
   0x012a5ea68f4e88a8, 0x010869cab88d7ae3, 0x009081ad277ceee1,
   0x033a77166d064cdc, 0x03955235a1fb3a95, 0x01251a4a9b25b65e},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x00148a3a1b27f40b, 0x0123186df1b31fdc, 0x00026e7beaad34ce,
   0x01db446ac1d3dbba, 0x0299c1a33437eaec, 0x024540610183cbb7,
   0x0173bb0e9ce92e46, 0x02b937e43921214b, 0x01ab0436a9bf01b5},
  {0x0383381640d46948, 0x008dacbf0e7f330f, 0x03602122bcc3f318,
   0x01ee596b200620d6, 0x03bd0585fda430b3, 0x014aed77fd123a83,
   0x005ace749e52f742, 0x0390fe041da2b842, 0x0189a8ceb3299242},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x012a19d6b3282473, 0x00c0915918b423ce, 0x023a954eb94405ae,
   0x00529f692be26158, 0x0289fa1b6fa4b2aa, 0x0198ae4ceea346ef,
   0x0047d8cdfbdedd49, 0x00cc8c8953f0f6b8, 0x001424abbff49203},
  {0x0256732a1115a03a, 0x0351bc38665c6733, 0x03f7b950fb4a6447,
   0x000afffa94c22155, 0x025763d0a4dab540, 0x000511e92d4fc283,
   0x030a7e9eda0ee96c, 0x004c3cd93a28bf0a, 0x017edb3a8719217f},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x011de5675a88e673, 0x031d7d0f5e567fbe, 0x0016b2062c970ae5,
   0x03f4a2be49d90aa7, 0x03cef0bd13822866, 0x03f0923dcf774a6c,
   0x0284bebc4f322f72, 0x016ab2645302bb2c, 0x01793f95dace0e2a},
  {0x010646e13527a28f, 0x01ca1babd59dc5e7, 0x01afedfd9a5595df,
   0x01f15785212ea6b1, 0x0324e5d64f6ae3f4, 0x02d680f526d00645,
   0x0127920fadf627a7, 0x03b383f75df4f684, 0x0089e0057e783b0a},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x00f334b9eb3c26c6, 0x0298fdaa98568dce, 0x01c2d24843a82292,
   0x020bcb24fa1b0711, 0x02cbdb3d2b1875e6, 0x0014907598f89422,
   0x03abe3aa43b26664, 0x02cbf47f720bc168, 0x0133b5e73014b79b},
  {0x034aab5dab05779d, 0x00cdc5d71fee9abb, 0x0399f16bd4bd9d30,
   0x03582fa592d82647, 0x02be1cdfb775b0e9, 0x0034f7cea32e94cb,
   0x0335a7f08f56f286, 0x03b707e9565d1c8b, 0x0015c946ea5b614f},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x024676f6cff72255, 0x00d14625cac96378, 0x00532b6008bc3767,
   0x01fc16721b985322, 0x023355ea1b091668, 0x029de7afdc0317c3,
   0x02fc8a7ca2da037c, 0x02de1217d74a6f30, 0x013f7173175b73bf},
  {0x0344913f441490b5, 0x0200f9e272b61eca, 0x0258a246b1dd55d2,
   0x03753db9ea496f36, 0x025e02937a09c5ef, 0x030cbd3d14012692,
   0x01793a67e70dc72a, 0x03ec1d37048a662e, 0x006550f700c32a8d},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x00d3f48a347eba27, 0x008e636649b61bd8, 0x00d3b93716778fb3,
   0x004d1915757bd209, 0x019d5311a3da44e0, 0x016d1afcbbe6aade,
   0x0241bf5f73265616, 0x0384672e5d50d39b, 0x005009fee522b684},
  {0x029b4fab064435fe, 0x018868ee095bbb07, 0x01ea3d6936cc92b8,
   0x000608b00f78a2f3, 0x02db911073d1c20f, 0x018205938470100a,
   0x01f1e4964cbe6ff2, 0x021a19a29eed4663, 0x01414485f42afa81},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x01612b3a17f63e34, 0x03813992885428e6, 0x022b3c215b5a9608,
   0x029b4057e19f2fcb, 0x0384059a587af7e6, 0x02d6400ace6fe610,
   0x029354d896e8e331, 0x00c047ee6dfba65e, 0x0037720542e9d49d},
  {0x02ce9eed7c5e9278, 0x0374ed703e79643b, 0x01316c54c4072006,
   0x005aaa09054b2ee8, 0x002824000c840d57, 0x03d4eba24771ed86,
   0x0189c50aabc3bdae, 0x0338c01541e15510, 0x00466d56e38eed42},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}},
 {{0x007efd8330ad8bd6, 0x02465ed48047710b, 0x0034c6606b215e0c,
   0x016ae30c53cbf839, 0x01fa17bd37161216, 0x018ead4e61ce8ab9,
   0x005482ed5f5dee46, 0x037543755bba1d7f, 0x005e5ac7e70a9d0f},
  {0x0117e1bb2fdcb2a2, 0x03deea36249f40c4, 0x028d09b4a6246cb7,
   0x03524b8855bcf756, 0x023d7d109d5ceb58, 0x0178e43e3223ef9c,
   0x0154536a0c6e966a, 0x037964d1286ee9fe, 0x0199bcd90e125055},
  {1, 0, 0, 0, 0, 0, 0, 0, 0}}
 };
 
 /*
  * select_point selects the |idx|th point from a precomputation table and
  * copies it to out.
  */
  /* pre_comp below is of the size provided in |size| */
 static void select_point(const limb idx, unsigned int size,
                          const felem pre_comp[][3], felem out[3])
 {
     unsigned i, j;
     limb *outlimbs = &out[0][0];
     memset(outlimbs, 0, 3 * sizeof(felem));
 
     for (i = 0; i < size; i++) {
         const limb *inlimbs = &pre_comp[i][0][0];
         limb mask = i ^ idx;
         mask |= mask >> 4;
         mask |= mask >> 2;
         mask |= mask >> 1;
         mask &= 1;
         mask--;
         for (j = 0; j < NLIMBS * 3; j++)
             outlimbs[j] |= inlimbs[j] & mask;
     }
 }
 
 /* get_bit returns the |i|th bit in |in| */
 static char get_bit(const felem_bytearray in, int i)
 {
     if (i < 0)
         return 0;
     return (in[i >> 3] >> (i & 7)) & 1;
 }
 
 /*
  * Interleaved point multiplication using precomputed point multiples: The
  * small point multiples 0*P, 1*P, ..., 16*P are in pre_comp[], the scalars
  * in scalars[]. If g_scalar is non-NULL, we also add this multiple of the
  * generator, using certain (large) precomputed multiples in g_pre_comp.
  * Output point (X, Y, Z) is stored in x_out, y_out, z_out
  */
 static void batch_mul(felem x_out, felem y_out, felem z_out,
                       const felem_bytearray scalars[],
                       const unsigned num_points, const u8 *g_scalar,
                       const int mixed, const felem pre_comp[][17][3],
                       const felem g_pre_comp[16][3])
 {
     int i, skip;
     unsigned num, gen_mul = (g_scalar != NULL);
     felem nq[3], tmp[4];
     limb bits;
     u8 sign, digit;
 
     /* set nq to the point at infinity */
     memset(nq, 0, 3 * sizeof(felem));
 
     /*
      * Loop over all scalars msb-to-lsb, interleaving additions of multiples
      * of the generator (last quarter of rounds) and additions of other
      * points multiples (every 5th round).
      */
     skip = 1;                   /* save two point operations in the first
                                  * round */
     for (i = (num_points ? 520 : 130); i >= 0; --i) {
         /* double */
         if (!skip)
             point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
 
         /* add multiples of the generator */
         if (gen_mul && (i <= 130)) {
             bits = get_bit(g_scalar, i + 390) << 3;
             if (i < 130) {
                 bits |= get_bit(g_scalar, i + 260) << 2;
                 bits |= get_bit(g_scalar, i + 130) << 1;
                 bits |= get_bit(g_scalar, i);
             }
             /* select the point to add, in constant time */
             select_point(bits, 16, g_pre_comp, tmp);
             if (!skip) {
                 /* The 1 argument below is for "mixed" */
                 point_add(nq[0], nq[1], nq[2],
                           nq[0], nq[1], nq[2], 1, tmp[0], tmp[1], tmp[2]);
             } else {
                 memcpy(nq, tmp, 3 * sizeof(felem));
                 skip = 0;
             }
         }
 
         /* do other additions every 5 doublings */
         if (num_points && (i % 5 == 0)) {
             /* loop over all scalars */
             for (num = 0; num < num_points; ++num) {
                 bits = get_bit(scalars[num], i + 4) << 5;
                 bits |= get_bit(scalars[num], i + 3) << 4;
                 bits |= get_bit(scalars[num], i + 2) << 3;
                 bits |= get_bit(scalars[num], i + 1) << 2;
                 bits |= get_bit(scalars[num], i) << 1;
                 bits |= get_bit(scalars[num], i - 1);
                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
 
                 /*
                  * select the point to add or subtract, in constant time
                  */
                 select_point(digit, 17, pre_comp[num], tmp);
                 felem_neg(tmp[3], tmp[1]); /* (X, -Y, Z) is the negative
                                             * point */
                 copy_conditional(tmp[1], tmp[3], (-(limb) sign));
 
                 if (!skip) {
                     point_add(nq[0], nq[1], nq[2],
                               nq[0], nq[1], nq[2],
                               mixed, tmp[0], tmp[1], tmp[2]);
                 } else {
                     memcpy(nq, tmp, 3 * sizeof(felem));
                     skip = 0;
                 }
             }
         }
     }
     felem_assign(x_out, nq[0]);
     felem_assign(y_out, nq[1]);
     felem_assign(z_out, nq[2]);
 }
 
 /* Precomputation for the group generator. */
 typedef struct {
     felem g_pre_comp[16][3];
     int references;
 } NISTP521_PRE_COMP;
 
 const EC_METHOD *EC_GFp_nistp521_method(void)
 {
     static const EC_METHOD ret = {
         EC_FLAGS_DEFAULT_OCT,
         NID_X9_62_prime_field,
         ec_GFp_nistp521_group_init,
         ec_GFp_simple_group_finish,
         ec_GFp_simple_group_clear_finish,
         ec_GFp_nist_group_copy,
         ec_GFp_nistp521_group_set_curve,
         ec_GFp_simple_group_get_curve,
         ec_GFp_simple_group_get_degree,
         ec_GFp_simple_group_check_discriminant,
         ec_GFp_simple_point_init,
         ec_GFp_simple_point_finish,
         ec_GFp_simple_point_clear_finish,
         ec_GFp_simple_point_copy,
         ec_GFp_simple_point_set_to_infinity,
         ec_GFp_simple_set_Jprojective_coordinates_GFp,
         ec_GFp_simple_get_Jprojective_coordinates_GFp,
         ec_GFp_simple_point_set_affine_coordinates,
         ec_GFp_nistp521_point_get_affine_coordinates,
         0 /* point_set_compressed_coordinates */ ,
         0 /* point2oct */ ,
         0 /* oct2point */ ,
         ec_GFp_simple_add,
         ec_GFp_simple_dbl,
         ec_GFp_simple_invert,
         ec_GFp_simple_is_at_infinity,
         ec_GFp_simple_is_on_curve,
         ec_GFp_simple_cmp,
         ec_GFp_simple_make_affine,
         ec_GFp_simple_points_make_affine,
         ec_GFp_nistp521_points_mul,
         ec_GFp_nistp521_precompute_mult,
         ec_GFp_nistp521_have_precompute_mult,
         ec_GFp_nist_field_mul,
         ec_GFp_nist_field_sqr,
         0 /* field_div */ ,
         0 /* field_encode */ ,
         0 /* field_decode */ ,
         0                       /* field_set_to_one */
     };
 
     return &ret;
 }
 
 /******************************************************************************/
 /*
  * FUNCTIONS TO MANAGE PRECOMPUTATION
  */
 
 static NISTP521_PRE_COMP *nistp521_pre_comp_new()
 {
     NISTP521_PRE_COMP *ret = NULL;
     ret = (NISTP521_PRE_COMP *) OPENSSL_malloc(sizeof(NISTP521_PRE_COMP));
     if (!ret) {
         ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
         return ret;
     }
     memset(ret->g_pre_comp, 0, sizeof(ret->g_pre_comp));
     ret->references = 1;
     return ret;
 }
 
 static void *nistp521_pre_comp_dup(void *src_)
 {
     NISTP521_PRE_COMP *src = src_;
 
     /* no need to actually copy, these objects never change! */
     CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
 
     return src_;
 }
 
 static void nistp521_pre_comp_free(void *pre_)
 {
     int i;
     NISTP521_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_free(pre);
 }
 
 static void nistp521_pre_comp_clear_free(void *pre_)
 {
     int i;
     NISTP521_PRE_COMP *pre = pre_;
 
     if (!pre)
         return;
 
     i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
     if (i > 0)
         return;
 
     OPENSSL_cleanse(pre, sizeof(*pre));
     OPENSSL_free(pre);
 }
 
 /******************************************************************************/
 /*
  * OPENSSL EC_METHOD FUNCTIONS
  */
 
 int ec_GFp_nistp521_group_init(EC_GROUP *group)
 {
     int ret;
     ret = ec_GFp_simple_group_init(group);
     group->a_is_minus3 = 1;
     return ret;
 }
 
 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
                                     const BIGNUM *a, const BIGNUM *b,
                                     BN_CTX *ctx)
 {
     int ret = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *curve_p, *curve_a, *curve_b;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((curve_p = BN_CTX_get(ctx)) == NULL) ||
         ((curve_a = BN_CTX_get(ctx)) == NULL) ||
         ((curve_b = BN_CTX_get(ctx)) == NULL))
         goto err;
     BN_bin2bn(nistp521_curve_params[0], sizeof(felem_bytearray), curve_p);
     BN_bin2bn(nistp521_curve_params[1], sizeof(felem_bytearray), curve_a);
     BN_bin2bn(nistp521_curve_params[2], sizeof(felem_bytearray), curve_b);
     if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
         ECerr(EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE,
               EC_R_WRONG_CURVE_PARAMETERS);
         goto err;
     }
     group->field_mod_func = BN_nist_mod_521;
     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
  err:
     BN_CTX_end(ctx);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     return ret;
 }
 
 /*
  * Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
  * (X/Z^2, Y/Z^3)
  */
 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
                                                  const EC_POINT *point,
                                                  BIGNUM *x, BIGNUM *y,
                                                  BN_CTX *ctx)
 {
     felem z1, z2, x_in, y_in, x_out, y_out;
     largefelem tmp;
 
     if (EC_POINT_is_at_infinity(group, point)) {
         ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
               EC_R_POINT_AT_INFINITY);
         return 0;
     }
     if ((!BN_to_felem(x_in, &point->X)) || (!BN_to_felem(y_in, &point->Y)) ||
         (!BN_to_felem(z1, &point->Z)))
         return 0;
     felem_inv(z2, z1);
     felem_square(tmp, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, x_in, z1);
     felem_reduce(x_in, tmp);
     felem_contract(x_out, x_in);
     if (x != NULL) {
         if (!felem_to_BN(x, x_out)) {
             ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     felem_mul(tmp, z1, z2);
     felem_reduce(z1, tmp);
     felem_mul(tmp, y_in, z1);
     felem_reduce(y_in, tmp);
     felem_contract(y_out, y_in);
     if (y != NULL) {
         if (!felem_to_BN(y, y_out)) {
             ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
                   ERR_R_BN_LIB);
             return 0;
         }
     }
     return 1;
 }
 
 /* points below is of size |num|, and tmp_felems is of size |num+1/ */
 static void make_points_affine(size_t num, felem points[][3],
                                felem tmp_felems[])
 {
     /*
      * Runs in constant time, unless an input is the point at infinity (which
      * normally shouldn't happen).
      */
     ec_GFp_nistp_points_make_affine_internal(num,
                                              points,
                                              sizeof(felem),
                                              tmp_felems,
                                              (void (*)(void *))felem_one,
                                              (int (*)(const void *))
                                              felem_is_zero_int,
                                              (void (*)(void *, const void *))
                                              felem_assign,
                                              (void (*)(void *, const void *))
                                              felem_square_reduce, (void (*)
                                                                    (void *,
                                                                     const void
                                                                     *,
                                                                     const void
                                                                     *))
                                              felem_mul_reduce,
                                              (void (*)(void *, const void *))
                                              felem_inv,
                                              (void (*)(void *, const void *))
                                              felem_contract);
 }
 
 /*
  * Computes scalar*generator + \sum scalars[i]*points[i], ignoring NULL
  * values Result is stored in r (r can equal one of the inputs).
  */
 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
                                const BIGNUM *scalar, size_t num,
                                const EC_POINT *points[],
                                const BIGNUM *scalars[], BN_CTX *ctx)
 {
     int ret = 0;
     int j;
     int mixed = 0;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y, *z, *tmp_scalar;
     felem_bytearray g_secret;
     felem_bytearray *secrets = NULL;
     felem(*pre_comp)[17][3] = NULL;
     felem *tmp_felems = NULL;
     felem_bytearray tmp;
     unsigned i, num_bytes;
     int have_pre_comp = 0;
     size_t num_points = num;
     felem x_in, y_in, z_in, x_out, y_out, z_out;
     NISTP521_PRE_COMP *pre = NULL;
     felem(*g_pre_comp)[3] = NULL;
     EC_POINT *generator = NULL;
     const EC_POINT *p = NULL;
     const BIGNUM *p_scalar = NULL;
 
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) ||
         ((y = BN_CTX_get(ctx)) == NULL) ||
         ((z = BN_CTX_get(ctx)) == NULL) ||
         ((tmp_scalar = BN_CTX_get(ctx)) == NULL))
         goto err;
 
     if (scalar != NULL) {
         pre = EC_EX_DATA_get_data(group->extra_data,
                                   nistp521_pre_comp_dup,
                                   nistp521_pre_comp_free,
                                   nistp521_pre_comp_clear_free);
         if (pre)
             /* we have precomputation, try to use it */
             g_pre_comp = &pre->g_pre_comp[0];
         else
             /* try to use the standard precomputation */
             g_pre_comp = (felem(*)[3]) gmul;
         generator = EC_POINT_new(group);
         if (generator == NULL)
             goto err;
         /* get the generator from precomputation */
         if (!felem_to_BN(x, g_pre_comp[1][0]) ||
             !felem_to_BN(y, g_pre_comp[1][1]) ||
             !felem_to_BN(z, g_pre_comp[1][2])) {
             ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
             goto err;
         }
         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
                                                       generator, x, y, z,
                                                       ctx))
             goto err;
         if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
             /* precomputation matches generator */
             have_pre_comp = 1;
         else
             /*
              * we don't have valid precomputation: treat the generator as a
              * random point
              */
             num_points++;
     }
 
     if (num_points > 0) {
         if (num_points >= 2) {
             /*
              * unless we precompute multiples for just one point, converting
              * those into affine form is time well spent
              */
             mixed = 1;
         }
         secrets = OPENSSL_malloc(num_points * sizeof(felem_bytearray));
         pre_comp = OPENSSL_malloc(num_points * 17 * 3 * sizeof(felem));
         if (mixed)
             tmp_felems =
                 OPENSSL_malloc((num_points * 17 + 1) * sizeof(felem));
         if ((secrets == NULL) || (pre_comp == NULL)
             || (mixed && (tmp_felems == NULL))) {
             ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_MALLOC_FAILURE);
             goto err;
         }
 
         /*
          * we treat NULL scalars as 0, and NULL points as points at infinity,
          * i.e., they contribute nothing to the linear combination
          */
         memset(secrets, 0, num_points * sizeof(felem_bytearray));
         memset(pre_comp, 0, num_points * 17 * 3 * sizeof(felem));
         for (i = 0; i < num_points; ++i) {
             if (i == num)
                 /*
                  * we didn't have a valid precomputation, so we pick the
                  * generator
                  */
             {
                 p = EC_GROUP_get0_generator(group);
                 p_scalar = scalar;
             } else
                 /* the i^th point */
             {
                 p = points[i];
                 p_scalar = scalars[i];
             }
             if ((p_scalar != NULL) && (p != NULL)) {
                 /* reduce scalar to 0 <= scalar < 2^521 */
                 if ((BN_num_bits(p_scalar) > 521)
                     || (BN_is_negative(p_scalar))) {
                     /*
                      * this is an unusual input, and we don't guarantee
                      * constant-timeness
                      */
                     if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
                         ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
                         goto err;
                     }
                     num_bytes = BN_bn2bin(tmp_scalar, tmp);
                 } else
                     num_bytes = BN_bn2bin(p_scalar, tmp);
                 flip_endian(secrets[i], tmp, num_bytes);
                 /* precompute multiples */
                 if ((!BN_to_felem(x_out, &p->X)) ||
                     (!BN_to_felem(y_out, &p->Y)) ||
                     (!BN_to_felem(z_out, &p->Z)))
                     goto err;
                 memcpy(pre_comp[i][1][0], x_out, sizeof(felem));
                 memcpy(pre_comp[i][1][1], y_out, sizeof(felem));
                 memcpy(pre_comp[i][1][2], z_out, sizeof(felem));
                 for (j = 2; j <= 16; ++j) {
                     if (j & 1) {
                         point_add(pre_comp[i][j][0], pre_comp[i][j][1],
                                   pre_comp[i][j][2], pre_comp[i][1][0],
                                   pre_comp[i][1][1], pre_comp[i][1][2], 0,
                                   pre_comp[i][j - 1][0],
                                   pre_comp[i][j - 1][1],
                                   pre_comp[i][j - 1][2]);
                     } else {
                         point_double(pre_comp[i][j][0], pre_comp[i][j][1],
                                      pre_comp[i][j][2], pre_comp[i][j / 2][0],
                                      pre_comp[i][j / 2][1],
                                      pre_comp[i][j / 2][2]);
                     }
                 }
             }
         }
         if (mixed)
             make_points_affine(num_points * 17, pre_comp[0], tmp_felems);
     }
 
     /* the scalar for the generator */
     if ((scalar != NULL) && (have_pre_comp)) {
         memset(g_secret, 0, sizeof(g_secret));
         /* reduce scalar to 0 <= scalar < 2^521 */
         if ((BN_num_bits(scalar) > 521) || (BN_is_negative(scalar))) {
             /*
              * this is an unusual input, and we don't guarantee
              * constant-timeness
              */
             if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
                 ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
                 goto err;
             }
             num_bytes = BN_bn2bin(tmp_scalar, tmp);
         } else
             num_bytes = BN_bn2bin(scalar, tmp);
         flip_endian(g_secret, tmp, num_bytes);
         /* do the multiplication with generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   g_secret,
                   mixed, (const felem(*)[17][3])pre_comp,
                   (const felem(*)[3])g_pre_comp);
     } else
         /* do the multiplication without generator precomputation */
         batch_mul(x_out, y_out, z_out,
                   (const felem_bytearray(*))secrets, num_points,
                   NULL, mixed, (const felem(*)[17][3])pre_comp, NULL);
     /* reduce the output to its unique minimal representation */
     felem_contract(x_in, x_out);
     felem_contract(y_in, y_out);
     felem_contract(z_in, z_out);
     if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
         (!felem_to_BN(z, z_in))) {
         ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
         goto err;
     }
     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
 
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (secrets != NULL)
         OPENSSL_free(secrets);
     if (pre_comp != NULL)
         OPENSSL_free(pre_comp);
     if (tmp_felems != NULL)
         OPENSSL_free(tmp_felems);
     return ret;
 }
 
 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
 {
     int ret = 0;
     NISTP521_PRE_COMP *pre = NULL;
     int i, j;
     BN_CTX *new_ctx = NULL;
     BIGNUM *x, *y;
     EC_POINT *generator = NULL;
     felem tmp_felems[16];
 
     /* throw away old precomputation */
     EC_EX_DATA_free_data(&group->extra_data, nistp521_pre_comp_dup,
                          nistp521_pre_comp_free,
                          nistp521_pre_comp_clear_free);
     if (ctx == NULL)
         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
             return 0;
     BN_CTX_start(ctx);
     if (((x = BN_CTX_get(ctx)) == NULL) || ((y = BN_CTX_get(ctx)) == NULL))
         goto err;
     /* get the generator */
     if (group->generator == NULL)
         goto err;
     generator = EC_POINT_new(group);
     if (generator == NULL)
         goto err;
     BN_bin2bn(nistp521_curve_params[3], sizeof(felem_bytearray), x);
     BN_bin2bn(nistp521_curve_params[4], sizeof(felem_bytearray), y);
     if (!EC_POINT_set_affine_coordinates_GFp(group, generator, x, y, ctx))
         goto err;
     if ((pre = nistp521_pre_comp_new()) == NULL)
         goto err;
     /*
      * if the generator is the standard one, use built-in precomputation
      */
     if (0 == EC_POINT_cmp(group, generator, group->generator, ctx)) {
         memcpy(pre->g_pre_comp, gmul, sizeof(pre->g_pre_comp));
-        ret = 1;
-        goto err;
+        goto done;
     }
     if ((!BN_to_felem(pre->g_pre_comp[1][0], &group->generator->X)) ||
         (!BN_to_felem(pre->g_pre_comp[1][1], &group->generator->Y)) ||
         (!BN_to_felem(pre->g_pre_comp[1][2], &group->generator->Z)))
         goto err;
     /* compute 2^130*G, 2^260*G, 2^390*G */
     for (i = 1; i <= 4; i <<= 1) {
         point_double(pre->g_pre_comp[2 * i][0], pre->g_pre_comp[2 * i][1],
                      pre->g_pre_comp[2 * i][2], pre->g_pre_comp[i][0],
                      pre->g_pre_comp[i][1], pre->g_pre_comp[i][2]);
         for (j = 0; j < 129; ++j) {
             point_double(pre->g_pre_comp[2 * i][0],
                          pre->g_pre_comp[2 * i][1],
                          pre->g_pre_comp[2 * i][2],
                          pre->g_pre_comp[2 * i][0],
                          pre->g_pre_comp[2 * i][1],
                          pre->g_pre_comp[2 * i][2]);
         }
     }
     /* g_pre_comp[0] is the point at infinity */
     memset(pre->g_pre_comp[0], 0, sizeof(pre->g_pre_comp[0]));
     /* the remaining multiples */
     /* 2^130*G + 2^260*G */
     point_add(pre->g_pre_comp[6][0], pre->g_pre_comp[6][1],
               pre->g_pre_comp[6][2], pre->g_pre_comp[4][0],
               pre->g_pre_comp[4][1], pre->g_pre_comp[4][2],
               0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
               pre->g_pre_comp[2][2]);
     /* 2^130*G + 2^390*G */
     point_add(pre->g_pre_comp[10][0], pre->g_pre_comp[10][1],
               pre->g_pre_comp[10][2], pre->g_pre_comp[8][0],
               pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
               0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
               pre->g_pre_comp[2][2]);
     /* 2^260*G + 2^390*G */
     point_add(pre->g_pre_comp[12][0], pre->g_pre_comp[12][1],
               pre->g_pre_comp[12][2], pre->g_pre_comp[8][0],
               pre->g_pre_comp[8][1], pre->g_pre_comp[8][2],
               0, pre->g_pre_comp[4][0], pre->g_pre_comp[4][1],
               pre->g_pre_comp[4][2]);
     /* 2^130*G + 2^260*G + 2^390*G */
     point_add(pre->g_pre_comp[14][0], pre->g_pre_comp[14][1],
               pre->g_pre_comp[14][2], pre->g_pre_comp[12][0],
               pre->g_pre_comp[12][1], pre->g_pre_comp[12][2],
               0, pre->g_pre_comp[2][0], pre->g_pre_comp[2][1],
               pre->g_pre_comp[2][2]);
     for (i = 1; i < 8; ++i) {
         /* odd multiples: add G */
         point_add(pre->g_pre_comp[2 * i + 1][0],
                   pre->g_pre_comp[2 * i + 1][1],
                   pre->g_pre_comp[2 * i + 1][2], pre->g_pre_comp[2 * i][0],
                   pre->g_pre_comp[2 * i][1], pre->g_pre_comp[2 * i][2], 0,
                   pre->g_pre_comp[1][0], pre->g_pre_comp[1][1],
                   pre->g_pre_comp[1][2]);
     }
     make_points_affine(15, &(pre->g_pre_comp[1]), tmp_felems);
 
+ done:
     if (!EC_EX_DATA_set_data(&group->extra_data, pre, nistp521_pre_comp_dup,
                              nistp521_pre_comp_free,
                              nistp521_pre_comp_clear_free))
         goto err;
     ret = 1;
     pre = NULL;
  err:
     BN_CTX_end(ctx);
     if (generator != NULL)
         EC_POINT_free(generator);
     if (new_ctx != NULL)
         BN_CTX_free(new_ctx);
     if (pre)
         nistp521_pre_comp_free(pre);
     return ret;
 }
 
 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group)
 {
     if (EC_EX_DATA_get_data(group->extra_data, nistp521_pre_comp_dup,
                             nistp521_pre_comp_free,
                             nistp521_pre_comp_clear_free)
         != NULL)
         return 1;
     else
         return 0;
 }
 
 #else
 static void *dummy = &dummy;
 #endif
Index: vendor-crypto/openssl/dist/crypto/ec/ectest.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/ec/ectest.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/ec/ectest.c	(revision 296273)
@@ -1,1861 +1,1870 @@
 /* crypto/ec/ectest.c */
 /*
  * Originally written by Bodo Moeller for the OpenSSL project.
  */
 /* ====================================================================
  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 /* ====================================================================
  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  *
  * Portions of the attached software ("Contribution") are developed by
  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  *
  * The Contribution is licensed pursuant to the OpenSSL open source
  * license provided above.
  *
  * The elliptic curve binary polynomial software is originally written by
  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  *
  */
 
 #include 
 #include 
 #ifdef FLAT_INC
 # include "e_os.h"
 #else
 # include "../e_os.h"
 #endif
 #include 
 #include 
 
 #ifdef OPENSSL_NO_EC
 int main(int argc, char *argv[])
 {
     puts("Elliptic curves are disabled.");
     return 0;
 }
 #else
 
 # include 
 # ifndef OPENSSL_NO_ENGINE
 #  include 
 # endif
 # include 
 # include 
 # include 
 # include 
 # include 
 # include 
 
 # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
 /* suppress "too big too optimize" warning */
 #  pragma warning(disable:4959)
 # endif
 
 # define ABORT do { \
         fflush(stdout); \
         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
         ERR_print_errors_fp(stderr); \
         EXIT(1); \
 } while (0)
 
 # define TIMING_BASE_PT 0
 # define TIMING_RAND_PT 1
 # define TIMING_SIMUL 2
 
 # if 0
 static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
 {
     clock_t clck;
     int i, j;
     BIGNUM *s;
     BIGNUM *r[10], *r0[10];
     EC_POINT *P;
 
     s = BN_new();
     if (s == NULL)
         ABORT;
 
     fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
     if (!EC_GROUP_get_order(group, s, ctx))
         ABORT;
     fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
     fflush(stdout);
 
     P = EC_POINT_new(group);
     if (P == NULL)
         ABORT;
     EC_POINT_copy(P, EC_GROUP_get0_generator(group));
 
     for (i = 0; i < 10; i++) {
         if ((r[i] = BN_new()) == NULL)
             ABORT;
         if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0))
             ABORT;
         if (type != TIMING_BASE_PT) {
             if ((r0[i] = BN_new()) == NULL)
                 ABORT;
             if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0))
                 ABORT;
         }
     }
 
     clck = clock();
     for (i = 0; i < 10; i++) {
         for (j = 0; j < 10; j++) {
             if (!EC_POINT_mul
                 (group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
                  (type != TIMING_BASE_PT) ? P : NULL,
                  (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx))
                 ABORT;
         }
     }
     clck = clock() - clck;
 
     fprintf(stdout, "\n");
 
 #  ifdef CLOCKS_PER_SEC
     /*
      * "To determine the time in seconds, the value returned by the clock
      * function should be divided by the value of the macro CLOCKS_PER_SEC."
      * -- ISO/IEC 9899
      */
 #   define UNIT "s"
 #  else
     /*
      * "`CLOCKS_PER_SEC' undeclared (first use this function)" -- cc on
      * NeXTstep/OpenStep
      */
 #   define UNIT "units"
 #   define CLOCKS_PER_SEC 1
 #  endif
 
     if (type == TIMING_BASE_PT) {
         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
                 "base point multiplications", (double)clck / CLOCKS_PER_SEC);
     } else if (type == TIMING_RAND_PT) {
         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
                 "random point multiplications",
                 (double)clck / CLOCKS_PER_SEC);
     } else if (type == TIMING_SIMUL) {
         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
                 "s*P+t*Q operations", (double)clck / CLOCKS_PER_SEC);
     }
     fprintf(stdout, "average: %.4f " UNIT "\n",
             (double)clck / (CLOCKS_PER_SEC * i * j));
 
     EC_POINT_free(P);
     BN_free(s);
     for (i = 0; i < 10; i++) {
         BN_free(r[i]);
         if (type != TIMING_BASE_PT)
             BN_free(r0[i]);
     }
 }
 # endif
 
 /* test multiplication with group order, long and negative scalars */
 static void group_order_tests(EC_GROUP *group)
 {
     BIGNUM *n1, *n2, *order;
     EC_POINT *P = EC_POINT_new(group);
     EC_POINT *Q = EC_POINT_new(group);
     BN_CTX *ctx = BN_CTX_new();
     int i;
 
     n1 = BN_new();
     n2 = BN_new();
     order = BN_new();
     fprintf(stdout, "verify group order ...");
     fflush(stdout);
     if (!EC_GROUP_get_order(group, order, ctx))
         ABORT;
     if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, Q))
         ABORT;
     fprintf(stdout, ".");
     fflush(stdout);
     if (!EC_GROUP_precompute_mult(group, ctx))
         ABORT;
     if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, Q))
         ABORT;
     fprintf(stdout, " ok\n");
     fprintf(stdout, "long/negative scalar tests ");
     for (i = 1; i <= 2; i++) {
         const BIGNUM *scalars[6];
         const EC_POINT *points[6];
 
         fprintf(stdout, i == 1 ?
                 "allowing precomputation ... " :
                 "without precomputation ... ");
         if (!BN_set_word(n1, i))
             ABORT;
         /*
          * If i == 1, P will be the predefined generator for which
          * EC_GROUP_precompute_mult has set up precomputation.
          */
         if (!EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
             ABORT;
 
         if (!BN_one(n1))
             ABORT;
         /* n1 = 1 - order */
         if (!BN_sub(n1, n1, order))
             ABORT;
         if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, Q, P, ctx))
             ABORT;
 
         /* n2 = 1 + order */
         if (!BN_add(n2, order, BN_value_one()))
             ABORT;
         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, Q, P, ctx))
             ABORT;
 
         /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
         if (!BN_mul(n2, n1, n2, ctx))
             ABORT;
         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, Q, P, ctx))
             ABORT;
 
         /* n2 = order^2 - 1 */
         BN_set_negative(n2, 0);
         if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
             ABORT;
         /* Add P to verify the result. */
         if (!EC_POINT_add(group, Q, Q, P, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, Q))
             ABORT;
 
         /* Exercise EC_POINTs_mul, including corner cases. */
         if (EC_POINT_is_at_infinity(group, P))
             ABORT;
         scalars[0] = n1;
         points[0] = Q;          /* => infinity */
         scalars[1] = n2;
         points[1] = P;          /* => -P */
         scalars[2] = n1;
         points[2] = Q;          /* => infinity */
         scalars[3] = n2;
         points[3] = Q;          /* => infinity */
         scalars[4] = n1;
         points[4] = P;          /* => P */
         scalars[5] = n2;
         points[5] = Q;          /* => infinity */
         if (!EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, P))
             ABORT;
     }
     fprintf(stdout, "ok\n");
 
     EC_POINT_free(P);
     EC_POINT_free(Q);
     BN_free(n1);
     BN_free(n2);
     BN_free(order);
     BN_CTX_free(ctx);
 }
 
 static void prime_field_tests(void)
 {
     BN_CTX *ctx = NULL;
     BIGNUM *p, *a, *b;
     EC_GROUP *group;
     EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 =
         NULL, *P_384 = NULL, *P_521 = NULL;
     EC_POINT *P, *Q, *R;
     BIGNUM *x, *y, *z;
     unsigned char buf[100];
     size_t i, len;
     int k;
 
 # if 1                          /* optional */
     ctx = BN_CTX_new();
     if (!ctx)
         ABORT;
 # endif
 
     p = BN_new();
     a = BN_new();
     b = BN_new();
     if (!p || !a || !b)
         ABORT;
 
     if (!BN_hex2bn(&p, "17"))
         ABORT;
     if (!BN_hex2bn(&a, "1"))
         ABORT;
     if (!BN_hex2bn(&b, "1"))
         ABORT;
 
     group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use
                                                  * EC_GROUP_new_curve_GFp so
                                                  * that the library gets to
                                                  * choose the EC_METHOD */
     if (!group)
         ABORT;
 
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     {
         EC_GROUP *tmp;
         tmp = EC_GROUP_new(EC_GROUP_method_of(group));
         if (!tmp)
             ABORT;
         if (!EC_GROUP_copy(tmp, group))
             ABORT;
         EC_GROUP_free(group);
         group = tmp;
     }
 
     if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     fprintf(stdout,
             "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
     BN_print_fp(stdout, p);
     fprintf(stdout, ")\n     a = 0x");
     BN_print_fp(stdout, a);
     fprintf(stdout, "\n     b = 0x");
     BN_print_fp(stdout, b);
     fprintf(stdout, "\n");
 
     P = EC_POINT_new(group);
     Q = EC_POINT_new(group);
     R = EC_POINT_new(group);
     if (!P || !Q || !R)
         ABORT;
 
     if (!EC_POINT_set_to_infinity(group, P))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
     buf[0] = 0;
     if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
         ABORT;
 
     if (!EC_POINT_add(group, P, P, Q, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
     x = BN_new();
     y = BN_new();
     z = BN_new();
     if (!x || !y || !z)
         ABORT;
 
     if (!BN_hex2bn(&x, "D"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
         if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx))
             ABORT;
         fprintf(stderr, "Point is not on curve: x = 0x");
         BN_print_fp(stderr, x);
         fprintf(stderr, ", y = 0x");
         BN_print_fp(stderr, y);
         fprintf(stderr, "\n");
         ABORT;
     }
 
     fprintf(stdout, "A cyclic subgroup:\n");
     k = 100;
     do {
         if (k-- == 0)
             ABORT;
 
         if (EC_POINT_is_at_infinity(group, P))
             fprintf(stdout, "     point at infinity\n");
         else {
             if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
                 ABORT;
 
             fprintf(stdout, "     x = 0x");
             BN_print_fp(stdout, x);
             fprintf(stdout, ", y = 0x");
             BN_print_fp(stdout, y);
             fprintf(stdout, "\n");
         }
 
         if (!EC_POINT_copy(R, P))
             ABORT;
         if (!EC_POINT_add(group, P, P, Q, ctx))
             ABORT;
 
 # if 0                          /* optional */
         {
             EC_POINT *points[3];
 
             points[0] = R;
             points[1] = Q;
             points[2] = P;
             if (!EC_POINTs_make_affine(group, 2, points, ctx))
                 ABORT;
         }
 # endif
 
     }
     while (!EC_POINT_is_at_infinity(group, P));
 
     if (!EC_POINT_add(group, P, Q, R, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
                            sizeof buf, ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "Generator as octet string, compressed form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
                            sizeof buf, ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
                            ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 
     if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))
         ABORT;
     fprintf(stdout,
             "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, ", Y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, ", Z = 0x");
     BN_print_fp(stdout, z);
     fprintf(stdout, "\n");
 
     if (!EC_POINT_invert(group, P, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, R, ctx))
         ABORT;
 
     /*
      * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
      * 2000) -- not a NIST curve, but commonly used
      */
 
     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
         ABORT;
     if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))
         ABORT;
     if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))
         ABORT;
     if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 160)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_160, group))
         ABORT;
 
     /* Curve P-192 (FIPS PUB 186-2, App. 6) */
 
     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
         ABORT;
     if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 192)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_192, group))
         ABORT;
 
     /* Curve P-224 (FIPS PUB 186-2, App. 6) */
 
     if (!BN_hex2bn
         (&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn
         (&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
         ABORT;
     if (!BN_hex2bn
         (&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn
         (&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn
         (&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn
         (&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 224)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_224, group))
         ABORT;
 
     /* Curve P-256 (FIPS PUB 186-2, App. 6) */
 
     if (!BN_hex2bn
         (&p,
          "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn
         (&a,
          "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))
         ABORT;
     if (!BN_hex2bn
         (&b,
          "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn
         (&x,
          "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
                    "84F3B9CAC2FC632551"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn
         (&z,
          "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 256)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_256, group))
         ABORT;
 
     /* Curve P-384 (FIPS PUB 186-2, App. 6) */
 
     if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"))
         ABORT;
     if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
                    "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
                    "9859F741E082542A385502F25DBF55296C3A545E3872760AB7"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
                    "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 384)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_384, group))
         ABORT;
 
     /* Curve P-521 (FIPS PUB 186-2, App. 6) */
 
     if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
         ABORT;
     if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
                    "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
                    "DF883D2C34F1EF451FD46B503F00"))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
         ABORT;
 
     if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
                    "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
                    "3C1856A429BF97E7E31C2E5BD66"))
         ABORT;
     if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
                    "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
                    "C9B8899C47AEBB6FB71E91386409"))
         ABORT;
     if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
         ABORT;
 
     if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
         ABORT;
     fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
     BN_print_fp(stdout, x);
     fprintf(stdout, "\n     y = 0x");
     BN_print_fp(stdout, y);
     fprintf(stdout, "\n");
     /* G_y value taken from the standard: */
     if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
                    "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
                    "7086A272C24088BE94769FD16650"))
         ABORT;
     if (0 != BN_cmp(y, z))
         ABORT;
 
     fprintf(stdout, "verify degree ...");
     if (EC_GROUP_get_degree(group) != 521)
         ABORT;
     fprintf(stdout, " ok\n");
 
     group_order_tests(group);
 
     if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group))))
         ABORT;
     if (!EC_GROUP_copy(P_521, group))
         ABORT;
 
     /* more tests using the last curve */
 
     if (!EC_POINT_copy(Q, P))
         ABORT;
     if (EC_POINT_is_at_infinity(group, Q))
         ABORT;
     if (!EC_POINT_dbl(group, P, P, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!EC_POINT_invert(group, Q, ctx))
         ABORT;                  /* P = -2Q */
 
     if (!EC_POINT_add(group, R, P, Q, ctx))
         ABORT;
     if (!EC_POINT_add(group, R, R, Q, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, R))
         ABORT;                  /* R = P + 2Q */
 
     {
         const EC_POINT *points[4];
         const BIGNUM *scalars[4];
         BIGNUM scalar3;
 
         if (EC_POINT_is_at_infinity(group, Q))
             ABORT;
         points[0] = Q;
         points[1] = Q;
         points[2] = Q;
         points[3] = Q;
 
         if (!EC_GROUP_get_order(group, z, ctx))
             ABORT;
         if (!BN_add(y, z, BN_value_one()))
             ABORT;
         if (BN_is_odd(y))
             ABORT;
         if (!BN_rshift1(y, y))
             ABORT;
         scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
         scalars[1] = y;
 
         fprintf(stdout, "combined multiplication ...");
         fflush(stdout);
 
         /* z is still the group order */
         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
             ABORT;
         if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, P, R, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, R, Q, ctx))
             ABORT;
 
         fprintf(stdout, ".");
         fflush(stdout);
 
         if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
             ABORT;
         if (!BN_add(z, z, y))
             ABORT;
         BN_set_negative(z, 1);
         scalars[0] = y;
         scalars[1] = z;         /* z = -(order + y) */
 
         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, P))
             ABORT;
 
         fprintf(stdout, ".");
         fflush(stdout);
 
         if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
             ABORT;
         if (!BN_add(z, x, y))
             ABORT;
         BN_set_negative(z, 1);
         scalars[0] = x;
         scalars[1] = y;
         scalars[2] = z;         /* z = -(x+y) */
 
         BN_init(&scalar3);
         BN_zero(&scalar3);
         scalars[3] = &scalar3;
 
         if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, P))
             ABORT;
 
         fprintf(stdout, " ok\n\n");
 
         BN_free(&scalar3);
     }
 
 # if 0
     timings(P_160, TIMING_BASE_PT, ctx);
     timings(P_160, TIMING_RAND_PT, ctx);
     timings(P_160, TIMING_SIMUL, ctx);
     timings(P_192, TIMING_BASE_PT, ctx);
     timings(P_192, TIMING_RAND_PT, ctx);
     timings(P_192, TIMING_SIMUL, ctx);
     timings(P_224, TIMING_BASE_PT, ctx);
     timings(P_224, TIMING_RAND_PT, ctx);
     timings(P_224, TIMING_SIMUL, ctx);
     timings(P_256, TIMING_BASE_PT, ctx);
     timings(P_256, TIMING_RAND_PT, ctx);
     timings(P_256, TIMING_SIMUL, ctx);
     timings(P_384, TIMING_BASE_PT, ctx);
     timings(P_384, TIMING_RAND_PT, ctx);
     timings(P_384, TIMING_SIMUL, ctx);
     timings(P_521, TIMING_BASE_PT, ctx);
     timings(P_521, TIMING_RAND_PT, ctx);
     timings(P_521, TIMING_SIMUL, ctx);
 # endif
 
     if (ctx)
         BN_CTX_free(ctx);
     BN_free(p);
     BN_free(a);
     BN_free(b);
     EC_GROUP_free(group);
     EC_POINT_free(P);
     EC_POINT_free(Q);
     EC_POINT_free(R);
     BN_free(x);
     BN_free(y);
     BN_free(z);
 
     if (P_160)
         EC_GROUP_free(P_160);
     if (P_192)
         EC_GROUP_free(P_192);
     if (P_224)
         EC_GROUP_free(P_224);
     if (P_256)
         EC_GROUP_free(P_256);
     if (P_384)
         EC_GROUP_free(P_384);
     if (P_521)
         EC_GROUP_free(P_521);
 
 }
 
 /* Change test based on whether binary point compression is enabled or not. */
 # ifdef OPENSSL_EC_BIN_PT_COMP
 #  define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
         if (!BN_hex2bn(&x, _x)) ABORT; \
         if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
         if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
         if (!BN_hex2bn(&z, _order)) ABORT; \
         if (!BN_hex2bn(&cof, _cof)) ABORT; \
         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
         if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
         BN_print_fp(stdout, x); \
         fprintf(stdout, "\n     y = 0x"); \
         BN_print_fp(stdout, y); \
         fprintf(stdout, "\n"); \
         /* G_y value taken from the standard: */ \
         if (!BN_hex2bn(&z, _y)) ABORT; \
         if (0 != BN_cmp(y, z)) ABORT;
 # else
 #  define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
         if (!BN_hex2bn(&x, _x)) ABORT; \
         if (!BN_hex2bn(&y, _y)) ABORT; \
         if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
         if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
         if (!BN_hex2bn(&z, _order)) ABORT; \
         if (!BN_hex2bn(&cof, _cof)) ABORT; \
         if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
         fprintf(stdout, "\n%s -- Generator:\n     x = 0x", _name); \
         BN_print_fp(stdout, x); \
         fprintf(stdout, "\n     y = 0x"); \
         BN_print_fp(stdout, y); \
         fprintf(stdout, "\n");
 # endif
 
 # define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
         if (!BN_hex2bn(&p, _p)) ABORT; \
         if (!BN_hex2bn(&a, _a)) ABORT; \
         if (!BN_hex2bn(&b, _b)) ABORT; \
         if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
         CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
         fprintf(stdout, "verify degree ..."); \
         if (EC_GROUP_get_degree(group) != _degree) ABORT; \
         fprintf(stdout, " ok\n"); \
         group_order_tests(group); \
         if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
         if (!EC_GROUP_copy(_variable, group)) ABORT; \
 
 # ifndef OPENSSL_NO_EC2M
 
 static void char2_field_tests(void)
 {
     BN_CTX *ctx = NULL;
     BIGNUM *p, *a, *b;
     EC_GROUP *group;
     EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 =
         NULL, *C2_K571 = NULL;
     EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 =
         NULL, *C2_B571 = NULL;
     EC_POINT *P, *Q, *R;
     BIGNUM *x, *y, *z, *cof;
     unsigned char buf[100];
     size_t i, len;
     int k;
 
 #  if 1                         /* optional */
     ctx = BN_CTX_new();
     if (!ctx)
         ABORT;
 #  endif
 
     p = BN_new();
     a = BN_new();
     b = BN_new();
     if (!p || !a || !b)
         ABORT;
 
     if (!BN_hex2bn(&p, "13"))
         ABORT;
     if (!BN_hex2bn(&a, "3"))
         ABORT;
     if (!BN_hex2bn(&b, "1"))
         ABORT;
 
     group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
                                                     * EC_GROUP_new_curve_GF2m
                                                     * so that the library gets
                                                     * to choose the EC_METHOD */
     if (!group)
         ABORT;
     if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
         ABORT;
 
     {
         EC_GROUP *tmp;
         tmp = EC_GROUP_new(EC_GROUP_method_of(group));
         if (!tmp)
             ABORT;
         if (!EC_GROUP_copy(tmp, group))
             ABORT;
         EC_GROUP_free(group);
         group = tmp;
     }
 
     if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx))
         ABORT;
 
     fprintf(stdout,
             "Curve defined by Weierstrass equation\n     y^2 + x*y = x^3 + a*x^2 + b  (mod 0x");
     BN_print_fp(stdout, p);
     fprintf(stdout, ")\n     a = 0x");
     BN_print_fp(stdout, a);
     fprintf(stdout, "\n     b = 0x");
     BN_print_fp(stdout, b);
     fprintf(stdout, "\n(0x... means binary polynomial)\n");
 
     P = EC_POINT_new(group);
     Q = EC_POINT_new(group);
     R = EC_POINT_new(group);
     if (!P || !Q || !R)
         ABORT;
 
     if (!EC_POINT_set_to_infinity(group, P))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
     buf[0] = 0;
     if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
         ABORT;
 
     if (!EC_POINT_add(group, P, P, Q, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
     x = BN_new();
     y = BN_new();
     z = BN_new();
     cof = BN_new();
     if (!x || !y || !z || !cof)
         ABORT;
 
     if (!BN_hex2bn(&x, "6"))
         ABORT;
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
     if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx))
         ABORT;
 #  else
     if (!BN_hex2bn(&y, "8"))
         ABORT;
     if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx))
         ABORT;
 #  endif
     if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
         if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx))
             ABORT;
 #  endif
         fprintf(stderr, "Point is not on curve: x = 0x");
         BN_print_fp(stderr, x);
         fprintf(stderr, ", y = 0x");
         BN_print_fp(stderr, y);
         fprintf(stderr, "\n");
         ABORT;
     }
 
     fprintf(stdout, "A cyclic subgroup:\n");
     k = 100;
     do {
         if (k-- == 0)
             ABORT;
 
         if (EC_POINT_is_at_infinity(group, P))
             fprintf(stdout, "     point at infinity\n");
         else {
             if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx))
                 ABORT;
 
             fprintf(stdout, "     x = 0x");
             BN_print_fp(stdout, x);
             fprintf(stdout, ", y = 0x");
             BN_print_fp(stdout, y);
             fprintf(stdout, "\n");
         }
 
         if (!EC_POINT_copy(R, P))
             ABORT;
         if (!EC_POINT_add(group, P, P, Q, ctx))
             ABORT;
     }
     while (!EC_POINT_is_at_infinity(group, P));
 
     if (!EC_POINT_add(group, P, Q, R, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, P))
         ABORT;
 
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
                            sizeof buf, ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "Generator as octet string, compressed form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 #  endif
 
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
                            sizeof buf, ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
                            ctx);
     if (len == 0)
         ABORT;
     if (!EC_POINT_oct2point(group, P, buf, len, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, Q, ctx))
         ABORT;
     fprintf(stdout, "\nGenerator as octet string, hybrid form:\n     ");
     for (i = 0; i < len; i++)
         fprintf(stdout, "%02X", buf[i]);
 #  endif
 
     fprintf(stdout, "\n");
 
     if (!EC_POINT_invert(group, P, ctx))
         ABORT;
     if (0 != EC_POINT_cmp(group, P, R, ctx))
         ABORT;
 
     /* Curve K-163 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve K-163",
          "0800000000000000000000000000000000000000C9",
          "1",
          "1",
          "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
          "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
          1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163, C2_K163);
 
     /* Curve B-163 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve B-163",
          "0800000000000000000000000000000000000000C9",
          "1",
          "020A601907B8C953CA1481EB10512F78744A3205FD",
          "03F0EBA16286A2D57EA0991168D4994637E8343E36",
          "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
          1, "040000000000000000000292FE77E70C12A4234C33", "2", 163, C2_B163);
 
     /* Curve K-233 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve K-233",
          "020000000000000000000000000000000000000004000000000000000001",
          "0",
          "1",
          "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
          "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
          0,
          "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
          "4", 233, C2_K233);
 
     /* Curve B-233 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve B-233",
          "020000000000000000000000000000000000000004000000000000000001",
          "000000000000000000000000000000000000000000000000000000000001",
          "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
          "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
          "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
          1,
          "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
          "2", 233, C2_B233);
 
     /* Curve K-283 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve K-283",
          "0800000000000000000000000000000000000000000000000000000000000000000010A1",
          "0",
          "1",
          "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
          "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
          0,
          "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
          "4", 283, C2_K283);
 
     /* Curve B-283 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve B-283",
          "0800000000000000000000000000000000000000000000000000000000000000000010A1",
          "000000000000000000000000000000000000000000000000000000000000000000000001",
          "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
          "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
          "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
          1,
          "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
          "2", 283, C2_B283);
 
     /* Curve K-409 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve K-409",
          "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
          "0",
          "1",
          "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
          "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
          1,
          "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
          "4", 409, C2_K409);
 
     /* Curve B-409 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve B-409",
          "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
          "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
          "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
          "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
          "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
          1,
          "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
          "2", 409, C2_B409);
 
     /* Curve K-571 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve K-571",
          "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
          "0",
          "1",
          "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
          "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
          0,
          "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
          "4", 571, C2_K571);
 
     /* Curve B-571 (FIPS PUB 186-2, App. 6) */
     CHAR2_CURVE_TEST
         ("NIST curve B-571",
          "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
          "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
          "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
          "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
          "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
          1,
          "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
          "2", 571, C2_B571);
 
     /* more tests using the last curve */
 
     if (!EC_POINT_copy(Q, P))
         ABORT;
     if (EC_POINT_is_at_infinity(group, Q))
         ABORT;
     if (!EC_POINT_dbl(group, P, P, ctx))
         ABORT;
     if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
         ABORT;
     if (!EC_POINT_invert(group, Q, ctx))
         ABORT;                  /* P = -2Q */
 
     if (!EC_POINT_add(group, R, P, Q, ctx))
         ABORT;
     if (!EC_POINT_add(group, R, R, Q, ctx))
         ABORT;
     if (!EC_POINT_is_at_infinity(group, R))
         ABORT;                  /* R = P + 2Q */
 
     {
         const EC_POINT *points[3];
         const BIGNUM *scalars[3];
 
         if (EC_POINT_is_at_infinity(group, Q))
             ABORT;
         points[0] = Q;
         points[1] = Q;
         points[2] = Q;
 
         if (!BN_add(y, z, BN_value_one()))
             ABORT;
         if (BN_is_odd(y))
             ABORT;
         if (!BN_rshift1(y, y))
             ABORT;
         scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
         scalars[1] = y;
 
         fprintf(stdout, "combined multiplication ...");
         fflush(stdout);
 
         /* z is still the group order */
         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
             ABORT;
         if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, P, R, ctx))
             ABORT;
         if (0 != EC_POINT_cmp(group, R, Q, ctx))
             ABORT;
 
         fprintf(stdout, ".");
         fflush(stdout);
 
         if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
             ABORT;
         if (!BN_add(z, z, y))
             ABORT;
         BN_set_negative(z, 1);
         scalars[0] = y;
         scalars[1] = z;         /* z = -(order + y) */
 
         if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, P))
             ABORT;
 
         fprintf(stdout, ".");
         fflush(stdout);
 
         if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
             ABORT;
         if (!BN_add(z, x, y))
             ABORT;
         BN_set_negative(z, 1);
         scalars[0] = x;
         scalars[1] = y;
         scalars[2] = z;         /* z = -(x+y) */
 
         if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
             ABORT;
         if (!EC_POINT_is_at_infinity(group, P))
             ABORT;
 
         fprintf(stdout, " ok\n\n");
     }
 
 #  if 0
     timings(C2_K163, TIMING_BASE_PT, ctx);
     timings(C2_K163, TIMING_RAND_PT, ctx);
     timings(C2_K163, TIMING_SIMUL, ctx);
     timings(C2_B163, TIMING_BASE_PT, ctx);
     timings(C2_B163, TIMING_RAND_PT, ctx);
     timings(C2_B163, TIMING_SIMUL, ctx);
     timings(C2_K233, TIMING_BASE_PT, ctx);
     timings(C2_K233, TIMING_RAND_PT, ctx);
     timings(C2_K233, TIMING_SIMUL, ctx);
     timings(C2_B233, TIMING_BASE_PT, ctx);
     timings(C2_B233, TIMING_RAND_PT, ctx);
     timings(C2_B233, TIMING_SIMUL, ctx);
     timings(C2_K283, TIMING_BASE_PT, ctx);
     timings(C2_K283, TIMING_RAND_PT, ctx);
     timings(C2_K283, TIMING_SIMUL, ctx);
     timings(C2_B283, TIMING_BASE_PT, ctx);
     timings(C2_B283, TIMING_RAND_PT, ctx);
     timings(C2_B283, TIMING_SIMUL, ctx);
     timings(C2_K409, TIMING_BASE_PT, ctx);
     timings(C2_K409, TIMING_RAND_PT, ctx);
     timings(C2_K409, TIMING_SIMUL, ctx);
     timings(C2_B409, TIMING_BASE_PT, ctx);
     timings(C2_B409, TIMING_RAND_PT, ctx);
     timings(C2_B409, TIMING_SIMUL, ctx);
     timings(C2_K571, TIMING_BASE_PT, ctx);
     timings(C2_K571, TIMING_RAND_PT, ctx);
     timings(C2_K571, TIMING_SIMUL, ctx);
     timings(C2_B571, TIMING_BASE_PT, ctx);
     timings(C2_B571, TIMING_RAND_PT, ctx);
     timings(C2_B571, TIMING_SIMUL, ctx);
 #  endif
 
     if (ctx)
         BN_CTX_free(ctx);
     BN_free(p);
     BN_free(a);
     BN_free(b);
     EC_GROUP_free(group);
     EC_POINT_free(P);
     EC_POINT_free(Q);
     EC_POINT_free(R);
     BN_free(x);
     BN_free(y);
     BN_free(z);
     BN_free(cof);
 
     if (C2_K163)
         EC_GROUP_free(C2_K163);
     if (C2_B163)
         EC_GROUP_free(C2_B163);
     if (C2_K233)
         EC_GROUP_free(C2_K233);
     if (C2_B233)
         EC_GROUP_free(C2_B233);
     if (C2_K283)
         EC_GROUP_free(C2_K283);
     if (C2_B283)
         EC_GROUP_free(C2_B283);
     if (C2_K409)
         EC_GROUP_free(C2_K409);
     if (C2_B409)
         EC_GROUP_free(C2_B409);
     if (C2_K571)
         EC_GROUP_free(C2_K571);
     if (C2_B571)
         EC_GROUP_free(C2_B571);
 
 }
 # endif
 
 static void internal_curve_test(void)
 {
     EC_builtin_curve *curves = NULL;
     size_t crv_len = 0, n = 0;
     int ok = 1;
 
     crv_len = EC_get_builtin_curves(NULL, 0);
 
     curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
 
     if (curves == NULL)
         return;
 
     if (!EC_get_builtin_curves(curves, crv_len)) {
         OPENSSL_free(curves);
         return;
     }
 
     fprintf(stdout, "testing internal curves: ");
 
     for (n = 0; n < crv_len; n++) {
         EC_GROUP *group = NULL;
         int nid = curves[n].nid;
         if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
             ok = 0;
             fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
                     " curve %s\n", OBJ_nid2sn(nid));
             /* try next curve */
             continue;
         }
         if (!EC_GROUP_check(group, NULL)) {
             ok = 0;
             fprintf(stdout, "\nEC_GROUP_check() failed with"
                     " curve %s\n", OBJ_nid2sn(nid));
             EC_GROUP_free(group);
             /* try the next curve */
             continue;
         }
         fprintf(stdout, ".");
         fflush(stdout);
         EC_GROUP_free(group);
     }
     if (ok)
         fprintf(stdout, " ok\n\n");
     else {
         fprintf(stdout, " failed\n\n");
         ABORT;
     }
     OPENSSL_free(curves);
     return;
 }
 
 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
 /*
  * nistp_test_params contains magic numbers for testing our optimized
  * implementations of several NIST curves with characteristic > 3.
  */
 struct nistp_test_params {
     const EC_METHOD *(*meth) ();
     int degree;
     /*
      * Qx, Qy and D are taken from
      * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
      * Otherwise, values are standard curve parameters from FIPS 180-3
      */
     const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
 };
 
 static const struct nistp_test_params nistp_tests_params[] = {
     {
      /* P-224 */
      EC_GFp_nistp224_method,
      224,
      /* p */
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
      /* a */
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
      /* b */
      "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
      /* Qx */
      "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
      /* Qy */
      "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
      /* Gx */
      "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
      /* Gy */
      "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
      /* order */
      "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
      /* d */
      "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
      },
     {
      /* P-256 */
      EC_GFp_nistp256_method,
      256,
      /* p */
      "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
      /* a */
      "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
      /* b */
      "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
      /* Qx */
      "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
      /* Qy */
      "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
      /* Gx */
      "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
      /* Gy */
      "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
      /* order */
      "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
      /* d */
      "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
      },
     {
      /* P-521 */
      EC_GFp_nistp521_method,
      521,
      /* p */
      "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
      /* a */
      "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
      /* b */
      "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
      /* Qx */
      "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
      /* Qy */
      "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
      /* Gx */
      "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
      /* Gy */
      "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
      /* order */
      "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
      /* d */
      "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
      },
 };
 
 static void nistp_single_test(const struct nistp_test_params *test)
 {
     BN_CTX *ctx;
     BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
     EC_GROUP *NISTP;
     EC_POINT *G, *P, *Q, *Q_CHECK;
 
     fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n",
             test->degree);
     ctx = BN_CTX_new();
     p = BN_new();
     a = BN_new();
     b = BN_new();
     x = BN_new();
     y = BN_new();
     m = BN_new();
     n = BN_new();
     order = BN_new();
 
     NISTP = EC_GROUP_new(test->meth());
     if (!NISTP)
         ABORT;
     if (!BN_hex2bn(&p, test->p))
         ABORT;
     if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
         ABORT;
     if (!BN_hex2bn(&a, test->a))
         ABORT;
     if (!BN_hex2bn(&b, test->b))
         ABORT;
     if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx))
         ABORT;
     G = EC_POINT_new(NISTP);
     P = EC_POINT_new(NISTP);
     Q = EC_POINT_new(NISTP);
     Q_CHECK = EC_POINT_new(NISTP);
     if (!BN_hex2bn(&x, test->Qx))
         ABORT;
     if (!BN_hex2bn(&y, test->Qy))
         ABORT;
     if (!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx))
         ABORT;
     if (!BN_hex2bn(&x, test->Gx))
         ABORT;
     if (!BN_hex2bn(&y, test->Gy))
         ABORT;
     if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx))
         ABORT;
     if (!BN_hex2bn(&order, test->order))
         ABORT;
     if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
         ABORT;
 
     fprintf(stdout, "verify degree ... ");
     if (EC_GROUP_get_degree(NISTP) != test->degree)
         ABORT;
     fprintf(stdout, "ok\n");
 
     fprintf(stdout, "NIST test vectors ... ");
     if (!BN_hex2bn(&n, test->d))
         ABORT;
     /* fixed point multiplication */
     EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
     /* random point multiplication */
     EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
 
     /* set generator to P = 2*G, where G is the standard generator */
     if (!EC_POINT_dbl(NISTP, P, G, ctx))
         ABORT;
     if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
         ABORT;
     /* set the scalar to m=n/2, where n is the NIST test scalar */
     if (!BN_rshift(m, n, 1))
         ABORT;
 
     /* test the non-standard generator */
     /* fixed point multiplication */
     EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
     /* random point multiplication */
     EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
 
+    /*
+     * We have not performed precomputation so have_precompute mult should be
+     * false
+     */
+    if (EC_GROUP_have_precompute_mult(NISTP))
+        ABORT;
+
     /* now repeat all tests with precomputation */
     if (!EC_GROUP_precompute_mult(NISTP, ctx))
+        ABORT;
+    if (!EC_GROUP_have_precompute_mult(NISTP))
         ABORT;
 
     /* fixed point multiplication */
     EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
     /* random point multiplication */
     EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
 
     /* reset generator */
     if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
         ABORT;
     /* fixed point multiplication */
     EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
     /* random point multiplication */
     EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
     if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
         ABORT;
 
     fprintf(stdout, "ok\n");
     group_order_tests(NISTP);
 #  if 0
     timings(NISTP, TIMING_BASE_PT, ctx);
     timings(NISTP, TIMING_RAND_PT, ctx);
 #  endif
     EC_GROUP_free(NISTP);
     EC_POINT_free(G);
     EC_POINT_free(P);
     EC_POINT_free(Q);
     EC_POINT_free(Q_CHECK);
     BN_free(n);
     BN_free(m);
     BN_free(p);
     BN_free(a);
     BN_free(b);
     BN_free(x);
     BN_free(y);
     BN_free(order);
     BN_CTX_free(ctx);
 }
 
 static void nistp_tests()
 {
     unsigned i;
 
     for (i = 0;
          i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params);
          i++) {
         nistp_single_test(&nistp_tests_params[i]);
     }
 }
 # endif
 
 static const char rnd_seed[] =
     "string to make the random number generator think it has entropy";
 
 int main(int argc, char *argv[])
 {
 
     /* enable memory leak checking unless explicitly disabled */
     if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL)
           && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) {
         CRYPTO_malloc_debug_init();
         CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
     } else {
         /* OPENSSL_DEBUG_MEMORY=off */
         CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
     }
     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
     ERR_load_crypto_strings();
 
     RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
 
     prime_field_tests();
     puts("");
 # ifndef OPENSSL_NO_EC2M
     char2_field_tests();
 # endif
 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
     nistp_tests();
 # endif
     /* test the internal curves */
     internal_curve_test();
 
 # ifndef OPENSSL_NO_ENGINE
     ENGINE_cleanup();
 # endif
     CRYPTO_cleanup_all_ex_data();
     ERR_free_strings();
     ERR_remove_thread_state(NULL);
     CRYPTO_mem_leaks_fp(stderr);
 
     return 0;
 }
 #endif
Index: vendor-crypto/openssl/dist/crypto/engine/eng_dyn.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/engine/eng_dyn.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/engine/eng_dyn.c	(revision 296273)
@@ -1,568 +1,570 @@
 /* crypto/engine/eng_dyn.c */
 /*
  * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  * 2001.
  */
 /* ====================================================================
  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 #include "eng_int.h"
 #include 
 
 /*
  * Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE
  * loader should implement the hook-up functions with the following
  * prototypes.
  */
 
 /* Our ENGINE handlers */
 static int dynamic_init(ENGINE *e);
 static int dynamic_finish(ENGINE *e);
 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p,
                         void (*f) (void));
 /* Predeclare our context type */
 typedef struct st_dynamic_data_ctx dynamic_data_ctx;
 /* The implementation for the important control command */
 static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx);
 
 #define DYNAMIC_CMD_SO_PATH             ENGINE_CMD_BASE
 #define DYNAMIC_CMD_NO_VCHECK           (ENGINE_CMD_BASE + 1)
 #define DYNAMIC_CMD_ID                  (ENGINE_CMD_BASE + 2)
 #define DYNAMIC_CMD_LIST_ADD            (ENGINE_CMD_BASE + 3)
 #define DYNAMIC_CMD_DIR_LOAD            (ENGINE_CMD_BASE + 4)
 #define DYNAMIC_CMD_DIR_ADD             (ENGINE_CMD_BASE + 5)
 #define DYNAMIC_CMD_LOAD                (ENGINE_CMD_BASE + 6)
 
 /* The constants used when creating the ENGINE */
 static const char *engine_dynamic_id = "dynamic";
 static const char *engine_dynamic_name = "Dynamic engine loading support";
 static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
     {DYNAMIC_CMD_SO_PATH,
      "SO_PATH",
      "Specifies the path to the new ENGINE shared library",
      ENGINE_CMD_FLAG_STRING},
     {DYNAMIC_CMD_NO_VCHECK,
      "NO_VCHECK",
      "Specifies to continue even if version checking fails (boolean)",
      ENGINE_CMD_FLAG_NUMERIC},
     {DYNAMIC_CMD_ID,
      "ID",
      "Specifies an ENGINE id name for loading",
      ENGINE_CMD_FLAG_STRING},
     {DYNAMIC_CMD_LIST_ADD,
      "LIST_ADD",
      "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
      ENGINE_CMD_FLAG_NUMERIC},
     {DYNAMIC_CMD_DIR_LOAD,
      "DIR_LOAD",
      "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)",
      ENGINE_CMD_FLAG_NUMERIC},
     {DYNAMIC_CMD_DIR_ADD,
      "DIR_ADD",
      "Adds a directory from which ENGINEs can be loaded",
      ENGINE_CMD_FLAG_STRING},
     {DYNAMIC_CMD_LOAD,
      "LOAD",
      "Load up the ENGINE specified by other settings",
      ENGINE_CMD_FLAG_NO_INPUT},
     {0, NULL, NULL, 0}
 };
 
 /*
  * Loading code stores state inside the ENGINE structure via the "ex_data"
  * element. We load all our state into a single structure and use that as a
  * single context in the "ex_data" stack.
  */
 struct st_dynamic_data_ctx {
     /* The DSO object we load that supplies the ENGINE code */
     DSO *dynamic_dso;
     /*
      * The function pointer to the version checking shared library function
      */
     dynamic_v_check_fn v_check;
     /*
      * The function pointer to the engine-binding shared library function
      */
     dynamic_bind_engine bind_engine;
     /* The default name/path for loading the shared library */
     const char *DYNAMIC_LIBNAME;
     /* Whether to continue loading on a version check failure */
     int no_vcheck;
     /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
     const char *engine_id;
     /*
      * If non-zero, a successfully loaded ENGINE should be added to the
      * internal ENGINE list. If 2, the add must succeed or the entire load
      * should fail.
      */
     int list_add_value;
     /* The symbol name for the version checking function */
     const char *DYNAMIC_F1;
     /* The symbol name for the "initialise ENGINE structure" function */
     const char *DYNAMIC_F2;
     /*
      * Whether to never use 'dirs', use 'dirs' as a fallback, or only use
      * 'dirs' for loading. Default is to use 'dirs' as a fallback.
      */
     int dir_load;
     /* A stack of directories from which ENGINEs could be loaded */
     STACK_OF(OPENSSL_STRING) *dirs;
 };
 
 /*
  * This is the "ex_data" index we obtain and reserve for use with our context
  * structure.
  */
 static int dynamic_ex_data_idx = -1;
 
 static void int_free_str(char *s)
 {
     OPENSSL_free(s);
 }
 
 /*
  * Because our ex_data element may or may not get allocated depending on
  * whether a "first-use" occurs before the ENGINE is freed, we have a memory
  * leak problem to solve. We can't declare a "new" handler for the ex_data as
  * we don't want a dynamic_data_ctx in *all* ENGINE structures of all types
  * (this is a bug in the design of CRYPTO_EX_DATA). As such, we just declare
  * a "free" handler and that will get called if an ENGINE is being destroyed
  * and there was an ex_data element corresponding to our context type.
  */
 static void dynamic_data_ctx_free_func(void *parent, void *ptr,
                                        CRYPTO_EX_DATA *ad, int idx, long argl,
                                        void *argp)
 {
     if (ptr) {
         dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
         if (ctx->dynamic_dso)
             DSO_free(ctx->dynamic_dso);
         if (ctx->DYNAMIC_LIBNAME)
             OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME);
         if (ctx->engine_id)
             OPENSSL_free((void *)ctx->engine_id);
         if (ctx->dirs)
             sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str);
         OPENSSL_free(ctx);
     }
 }
 
 /*
  * Construct the per-ENGINE context. We create it blindly and then use a lock
  * to check for a race - if so, all but one of the threads "racing" will have
  * wasted their time. The alternative involves creating everything inside the
  * lock which is far worse.
  */
 static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
 {
     dynamic_data_ctx *c;
     c = OPENSSL_malloc(sizeof(dynamic_data_ctx));
     if (!c) {
         ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
         return 0;
     }
     memset(c, 0, sizeof(dynamic_data_ctx));
     c->dynamic_dso = NULL;
     c->v_check = NULL;
     c->bind_engine = NULL;
     c->DYNAMIC_LIBNAME = NULL;
     c->no_vcheck = 0;
     c->engine_id = NULL;
     c->list_add_value = 0;
     c->DYNAMIC_F1 = "v_check";
     c->DYNAMIC_F2 = "bind_engine";
     c->dir_load = 1;
     c->dirs = sk_OPENSSL_STRING_new_null();
     if (!c->dirs) {
         ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
         OPENSSL_free(c);
         return 0;
     }
     CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
     if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
                                                        dynamic_ex_data_idx))
         == NULL) {
         /* Good, we're the first */
         ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
         *ctx = c;
         c = NULL;
     }
     CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
     /*
      * If we lost the race to set the context, c is non-NULL and *ctx is the
      * context of the thread that won.
      */
-    if (c)
+    if (c) {
+        sk_OPENSSL_STRING_free(c->dirs);
         OPENSSL_free(c);
+    }
     return 1;
 }
 
 /*
  * This function retrieves the context structure from an ENGINE's "ex_data",
  * or if it doesn't exist yet, sets it up.
  */
 static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
 {
     dynamic_data_ctx *ctx;
     if (dynamic_ex_data_idx < 0) {
         /*
          * Create and register the ENGINE ex_data, and associate our "free"
          * function with it to ensure any allocated contexts get freed when
          * an ENGINE goes underground.
          */
         int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
                                               dynamic_data_ctx_free_func);
         if (new_idx == -1) {
             ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
             return NULL;
         }
         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
         /* Avoid a race by checking again inside this lock */
         if (dynamic_ex_data_idx < 0) {
             /* Good, someone didn't beat us to it */
             dynamic_ex_data_idx = new_idx;
             new_idx = -1;
         }
         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
         /*
          * In theory we could "give back" the index here if (new_idx>-1), but
          * it's not possible and wouldn't gain us much if it were.
          */
     }
     ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
     /* Check if the context needs to be created */
     if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
         /* "set_data" will set errors if necessary */
         return NULL;
     return ctx;
 }
 
 static ENGINE *engine_dynamic(void)
 {
     ENGINE *ret = ENGINE_new();
     if (!ret)
         return NULL;
     if (!ENGINE_set_id(ret, engine_dynamic_id) ||
         !ENGINE_set_name(ret, engine_dynamic_name) ||
         !ENGINE_set_init_function(ret, dynamic_init) ||
         !ENGINE_set_finish_function(ret, dynamic_finish) ||
         !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
         !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
         !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
         ENGINE_free(ret);
         return NULL;
     }
     return ret;
 }
 
 void ENGINE_load_dynamic(void)
 {
     ENGINE *toadd = engine_dynamic();
     if (!toadd)
         return;
     ENGINE_add(toadd);
     /*
      * If the "add" worked, it gets a structural reference. So either way, we
      * release our just-created reference.
      */
     ENGINE_free(toadd);
     /*
      * If the "add" didn't work, it was probably a conflict because it was
      * already added (eg. someone calling ENGINE_load_blah then calling
      * ENGINE_load_builtin_engines() perhaps).
      */
     ERR_clear_error();
 }
 
 static int dynamic_init(ENGINE *e)
 {
     /*
      * We always return failure - the "dyanamic" engine itself can't be used
      * for anything.
      */
     return 0;
 }
 
 static int dynamic_finish(ENGINE *e)
 {
     /*
      * This should never be called on account of "dynamic_init" always
      * failing.
      */
     return 0;
 }
 
 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
 {
     dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
     int initialised;
 
     if (!ctx) {
         ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
         return 0;
     }
     initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
     /* All our control commands require the ENGINE to be uninitialised */
     if (initialised) {
         ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
         return 0;
     }
     switch (cmd) {
     case DYNAMIC_CMD_SO_PATH:
         /* a NULL 'p' or a string of zero-length is the same thing */
         if (p && (strlen((const char *)p) < 1))
             p = NULL;
         if (ctx->DYNAMIC_LIBNAME)
             OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME);
         if (p)
             ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
         else
             ctx->DYNAMIC_LIBNAME = NULL;
         return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
     case DYNAMIC_CMD_NO_VCHECK:
         ctx->no_vcheck = ((i == 0) ? 0 : 1);
         return 1;
     case DYNAMIC_CMD_ID:
         /* a NULL 'p' or a string of zero-length is the same thing */
         if (p && (strlen((const char *)p) < 1))
             p = NULL;
         if (ctx->engine_id)
             OPENSSL_free((void *)ctx->engine_id);
         if (p)
             ctx->engine_id = BUF_strdup(p);
         else
             ctx->engine_id = NULL;
         return (ctx->engine_id ? 1 : 0);
     case DYNAMIC_CMD_LIST_ADD:
         if ((i < 0) || (i > 2)) {
             ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
             return 0;
         }
         ctx->list_add_value = (int)i;
         return 1;
     case DYNAMIC_CMD_LOAD:
         return dynamic_load(e, ctx);
     case DYNAMIC_CMD_DIR_LOAD:
         if ((i < 0) || (i > 2)) {
             ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
             return 0;
         }
         ctx->dir_load = (int)i;
         return 1;
     case DYNAMIC_CMD_DIR_ADD:
         /* a NULL 'p' or a string of zero-length is the same thing */
         if (!p || (strlen((const char *)p) < 1)) {
             ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
             return 0;
         }
         {
             char *tmp_str = BUF_strdup(p);
             if (!tmp_str) {
                 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
                 return 0;
             }
             sk_OPENSSL_STRING_insert(ctx->dirs, tmp_str, -1);
         }
         return 1;
     default:
         break;
     }
     ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
     return 0;
 }
 
 static int int_load(dynamic_data_ctx *ctx)
 {
     int num, loop;
     /* Unless told not to, try a direct load */
     if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
                                           ctx->DYNAMIC_LIBNAME, NULL,
                                           0)) != NULL)
         return 1;
     /* If we're not allowed to use 'dirs' or we have none, fail */
     if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1)
         return 0;
     for (loop = 0; loop < num; loop++) {
         const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop);
         char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
         if (!merge)
             return 0;
         if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
             /* Found what we're looking for */
             OPENSSL_free(merge);
             return 1;
         }
         OPENSSL_free(merge);
     }
     return 0;
 }
 
 static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
 {
     ENGINE cpy;
     dynamic_fns fns;
 
     if (!ctx->dynamic_dso)
         ctx->dynamic_dso = DSO_new();
     if (!ctx->DYNAMIC_LIBNAME) {
         if (!ctx->engine_id)
             return 0;
         ctx->DYNAMIC_LIBNAME =
             DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
     }
     if (!int_load(ctx)) {
         ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
         DSO_free(ctx->dynamic_dso);
         ctx->dynamic_dso = NULL;
         return 0;
     }
     /* We have to find a bind function otherwise it'll always end badly */
     if (!
         (ctx->bind_engine =
          (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
                                              ctx->DYNAMIC_F2))) {
         ctx->bind_engine = NULL;
         DSO_free(ctx->dynamic_dso);
         ctx->dynamic_dso = NULL;
         ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
         return 0;
     }
     /* Do we perform version checking? */
     if (!ctx->no_vcheck) {
         unsigned long vcheck_res = 0;
         /*
          * Now we try to find a version checking function and decide how to
          * cope with failure if/when it fails.
          */
         ctx->v_check =
             (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
                                                ctx->DYNAMIC_F1);
         if (ctx->v_check)
             vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
         /*
          * We fail if the version checker veto'd the load *or* if it is
          * deferring to us (by returning its version) and we think it is too
          * old.
          */
         if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
             /* Fail */
             ctx->bind_engine = NULL;
             ctx->v_check = NULL;
             DSO_free(ctx->dynamic_dso);
             ctx->dynamic_dso = NULL;
             ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
                       ENGINE_R_VERSION_INCOMPATIBILITY);
             return 0;
         }
     }
     /*
      * First binary copy the ENGINE structure so that we can roll back if the
      * hand-over fails
      */
     memcpy(&cpy, e, sizeof(ENGINE));
     /*
      * Provide the ERR, "ex_data", memory, and locking callbacks so the
      * loaded library uses our state rather than its own. FIXME: As noted in
      * engine.h, much of this would be simplified if each area of code
      * provided its own "summary" structure of all related callbacks. It
      * would also increase opaqueness.
      */
     fns.static_state = ENGINE_get_static_state();
     fns.err_fns = ERR_get_implementation();
     fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
     CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
                              &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb);
     fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
     fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
     fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
     fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
     fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
     /*
      * Now that we've loaded the dynamic engine, make sure no "dynamic"
      * ENGINE elements will show through.
      */
     engine_set_all_null(e);
 
     /* Try to bind the ENGINE onto our own ENGINE structure */
     if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
         ctx->bind_engine = NULL;
         ctx->v_check = NULL;
         DSO_free(ctx->dynamic_dso);
         ctx->dynamic_dso = NULL;
         ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
         /* Copy the original ENGINE structure back */
         memcpy(e, &cpy, sizeof(ENGINE));
         return 0;
     }
     /* Do we try to add this ENGINE to the internal list too? */
     if (ctx->list_add_value > 0) {
         if (!ENGINE_add(e)) {
             /* Do we tolerate this or fail? */
             if (ctx->list_add_value > 1) {
                 /*
                  * Fail - NB: By this time, it's too late to rollback, and
                  * trying to do so allows the bind_engine() code to have
                  * created leaks. We just have to fail where we are, after
                  * the ENGINE has changed.
                  */
                 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
                           ENGINE_R_CONFLICTING_ENGINE_ID);
                 return 0;
             }
             /* Tolerate */
             ERR_clear_error();
         }
     }
     return 1;
 }
Index: vendor-crypto/openssl/dist/crypto/evp/e_des.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/evp/e_des.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/evp/e_des.c	(revision 296273)
@@ -1,268 +1,269 @@
 /* crypto/evp/e_des.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include "cryptlib.h"
 #ifndef OPENSSL_NO_DES
 # include 
 # include 
 # include "evp_locl.h"
 # include 
 # include 
 
 typedef struct {
     union {
         double align;
         DES_key_schedule ks;
     } ks;
     union {
-        void (*cbc) (const void *, void *, size_t, const void *, void *);
+        void (*cbc) (const void *, void *, size_t,
+                     const DES_key_schedule *, unsigned char *);
     } stream;
 } EVP_DES_KEY;
 
 # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
-/* ---------^^^ this is not a typo, just a way to detect that
+/* ----------^^^ this is not a typo, just a way to detect that
  * assembler support was in general requested... */
 #  include "sparc_arch.h"
 
 extern unsigned int OPENSSL_sparcv9cap_P[];
 
 #  define SPARC_DES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_DES)
 
 void des_t4_key_expand(const void *key, DES_key_schedule *ks);
 void des_t4_cbc_encrypt(const void *inp, void *out, size_t len,
-                        DES_key_schedule *ks, unsigned char iv[8]);
+                        const DES_key_schedule *ks, unsigned char iv[8]);
 void des_t4_cbc_decrypt(const void *inp, void *out, size_t len,
-                        DES_key_schedule *ks, unsigned char iv[8]);
+                        const DES_key_schedule *ks, unsigned char iv[8]);
 # endif
 
 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                         const unsigned char *iv, int enc);
 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
 
 /*
  * Because of various casts and different names can't use
  * IMPLEMENT_BLOCK_CIPHER
  */
 
 static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                           const unsigned char *in, size_t inl)
 {
     BLOCK_CIPHER_ecb_loop()
         DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
                         ctx->cipher_data, ctx->encrypt);
     return 1;
 }
 
 static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                           const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
                           (DES_cblock *)ctx->iv, &ctx->num);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data,
                           (DES_cblock *)ctx->iv, &ctx->num);
     return 1;
 }
 
 static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                           const unsigned char *in, size_t inl)
 {
     EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
 
-    if (dat->stream.cbc) {
+    if (dat->stream.cbc != NULL) {
         (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv);
         return 1;
     }
     while (inl >= EVP_MAXCHUNK) {
         DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
                          (DES_cblock *)ctx->iv, ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data,
                          (DES_cblock *)ctx->iv, ctx->encrypt);
     return 1;
 }
 
 static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                             const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data,
                           (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data,
                           (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
     return 1;
 }
 
 /*
  * Although we have a CFB-r implementation for DES, it doesn't pack the right
  * way, so wrap it here
  */
 static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                            const unsigned char *in, size_t inl)
 {
     size_t n, chunk = EVP_MAXCHUNK / 8;
     unsigned char c[1], d[1];
 
     if (inl < chunk)
         chunk = inl;
 
     while (inl && inl >= chunk) {
         for (n = 0; n < chunk * 8; ++n) {
             c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
             DES_cfb_encrypt(c, d, 1, 1, ctx->cipher_data,
                             (DES_cblock *)ctx->iv, ctx->encrypt);
             out[n / 8] =
                 (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) |
                 ((d[0] & 0x80) >> (unsigned int)(n % 8));
         }
         inl -= chunk;
         in += chunk;
         out += chunk;
         if (inl < chunk)
             chunk = inl;
     }
 
     return 1;
 }
 
 static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                            const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, ctx->cipher_data,
                         (DES_cblock *)ctx->iv, ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_cfb_encrypt(in, out, 8, (long)inl, ctx->cipher_data,
                         (DES_cblock *)ctx->iv, ctx->encrypt);
     return 1;
 }
 
 BLOCK_CIPHER_defs(des, EVP_DES_KEY, NID_des, 8, 8, 8, 64,
                   EVP_CIPH_RAND_KEY, des_init_key, NULL,
                   EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
 
     BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 1,
                      EVP_CIPH_RAND_KEY, des_init_key, NULL,
                      EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
 
     BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 8,
                      EVP_CIPH_RAND_KEY, des_init_key, NULL,
                      EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
 
 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                         const unsigned char *iv, int enc)
 {
     DES_cblock *deskey = (DES_cblock *)key;
     EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
 
     dat->stream.cbc = NULL;
 # if defined(SPARC_DES_CAPABLE)
     if (SPARC_DES_CAPABLE) {
         int mode = ctx->cipher->flags & EVP_CIPH_MODE;
 
         if (mode == EVP_CIPH_CBC_MODE) {
             des_t4_key_expand(key, &dat->ks.ks);
             dat->stream.cbc = enc ? des_t4_cbc_encrypt : des_t4_cbc_decrypt;
             return 1;
         }
     }
 # endif
 # ifdef EVP_CHECK_DES_KEY
     if (DES_set_key_checked(deskey, dat->ks.ks) != 0)
         return 0;
 # else
     DES_set_key_unchecked(deskey, ctx->cipher_data);
 # endif
     return 1;
 }
 
 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
 {
 
     switch (type) {
     case EVP_CTRL_RAND_KEY:
         if (RAND_bytes(ptr, 8) <= 0)
             return 0;
         DES_set_odd_parity((DES_cblock *)ptr);
         return 1;
 
     default:
         return -1;
     }
 }
 
 #endif
Index: vendor-crypto/openssl/dist/crypto/evp/e_des3.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/evp/e_des3.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/evp/e_des3.c	(revision 296273)
@@ -1,494 +1,495 @@
 /* crypto/evp/e_des3.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include "cryptlib.h"
 #ifndef OPENSSL_NO_DES
 # include 
 # include 
 # include "evp_locl.h"
 # include 
 # include 
 
 /* Block use of implementations in FIPS mode */
 # undef EVP_CIPH_FLAG_FIPS
 # define EVP_CIPH_FLAG_FIPS      0
 
 typedef struct {
     union {
         double align;
         DES_key_schedule ks[3];
     } ks;
     union {
-        void (*cbc) (const void *, void *, size_t, const void *, void *);
+        void (*cbc) (const void *, void *, size_t,
+                     const DES_key_schedule *, unsigned char *);
     } stream;
 } DES_EDE_KEY;
 # define ks1 ks.ks[0]
 # define ks2 ks.ks[1]
 # define ks3 ks.ks[2]
 
 # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
 /* ---------^^^ this is not a typo, just a way to detect that
  * assembler support was in general requested... */
 #  include "sparc_arch.h"
 
 extern unsigned int OPENSSL_sparcv9cap_P[];
 
 #  define SPARC_DES_CAPABLE       (OPENSSL_sparcv9cap_P[1] & CFR_DES)
 
 void des_t4_key_expand(const void *key, DES_key_schedule *ks);
 void des_t4_ede3_cbc_encrypt(const void *inp, void *out, size_t len,
-                             DES_key_schedule *ks, unsigned char iv[8]);
+                             const DES_key_schedule ks[3], unsigned char iv[8]);
 void des_t4_ede3_cbc_decrypt(const void *inp, void *out, size_t len,
-                             DES_key_schedule *ks, unsigned char iv[8]);
+                             const DES_key_schedule ks[3], unsigned char iv[8]);
 # endif
 
 static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                             const unsigned char *iv, int enc);
 
 static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                              const unsigned char *iv, int enc);
 
 static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
 
 # define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data)
 
 /*
  * Because of various casts and different args can't use
  * IMPLEMENT_BLOCK_CIPHER
  */
 
 static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                               const unsigned char *in, size_t inl)
 {
     BLOCK_CIPHER_ecb_loop()
         DES_ecb3_encrypt((const_DES_cblock *)(in + i),
                          (DES_cblock *)(out + i),
                          &data(ctx)->ks1, &data(ctx)->ks2,
                          &data(ctx)->ks3, ctx->encrypt);
     return 1;
 }
 
 static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                               const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
                                &data(ctx)->ks1, &data(ctx)->ks2,
                                &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                                &ctx->num);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ede3_ofb64_encrypt(in, out, (long)inl,
                                &data(ctx)->ks1, &data(ctx)->ks2,
                                &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                                &ctx->num);
 
     return 1;
 }
 
 static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                               const unsigned char *in, size_t inl)
 {
     DES_EDE_KEY *dat = data(ctx);
 
 # ifdef KSSL_DEBUG
     {
         int i;
         fprintf(stderr, "des_ede_cbc_cipher(ctx=%p, buflen=%d)\n", ctx,
                 ctx->buf_len);
         fprintf(stderr, "\t iv= ");
         for (i = 0; i < 8; i++)
             fprintf(stderr, "%02X", ctx->iv[i]);
         fprintf(stderr, "\n");
     }
 # endif                         /* KSSL_DEBUG */
     if (dat->stream.cbc) {
-        (*dat->stream.cbc) (in, out, inl, &dat->ks, ctx->iv);
+        (*dat->stream.cbc) (in, out, inl, dat->ks.ks, ctx->iv);
         return 1;
     }
 
     while (inl >= EVP_MAXCHUNK) {
         DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK,
                              &dat->ks1, &dat->ks2, &dat->ks3,
                              (DES_cblock *)ctx->iv, ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ede3_cbc_encrypt(in, out, (long)inl,
                              &dat->ks1, &dat->ks2, &dat->ks3,
                              (DES_cblock *)ctx->iv, ctx->encrypt);
     return 1;
 }
 
 static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                 const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
                                &data(ctx)->ks1, &data(ctx)->ks2,
                                &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                                &ctx->num, ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ede3_cfb64_encrypt(in, out, (long)inl,
                                &data(ctx)->ks1, &data(ctx)->ks2,
                                &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                                &ctx->num, ctx->encrypt);
     return 1;
 }
 
 /*
  * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
  * right way, so wrap it here
  */
 static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                 const unsigned char *in, size_t inl)
 {
     size_t n;
     unsigned char c[1], d[1];
 
     for (n = 0; n < inl; ++n) {
         c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
         DES_ede3_cfb_encrypt(c, d, 1, 1,
                              &data(ctx)->ks1, &data(ctx)->ks2,
                              &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                              ctx->encrypt);
         out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
             | ((d[0] & 0x80) >> (unsigned int)(n % 8));
     }
 
     return 1;
 }
 
 static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                 const unsigned char *in, size_t inl)
 {
     while (inl >= EVP_MAXCHUNK) {
         DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK,
                              &data(ctx)->ks1, &data(ctx)->ks2,
                              &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                              ctx->encrypt);
         inl -= EVP_MAXCHUNK;
         in += EVP_MAXCHUNK;
         out += EVP_MAXCHUNK;
     }
     if (inl)
         DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
                              &data(ctx)->ks1, &data(ctx)->ks2,
                              &data(ctx)->ks3, (DES_cblock *)ctx->iv,
                              ctx->encrypt);
     return 1;
 }
 
 BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64,
                   EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1,
                   des_ede_init_key, NULL, NULL, NULL, des3_ctrl)
 # define des_ede3_cfb64_cipher des_ede_cfb64_cipher
 # define des_ede3_ofb_cipher des_ede_ofb_cipher
 # define des_ede3_cbc_cipher des_ede_cbc_cipher
 # define des_ede3_ecb_cipher des_ede_ecb_cipher
     BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64,
                   EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_FIPS |
                   EVP_CIPH_FLAG_DEFAULT_ASN1, des_ede3_init_key, NULL, NULL, NULL,
                   des3_ctrl)
 
     BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 1,
                      EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_FIPS |
                      EVP_CIPH_FLAG_DEFAULT_ASN1, des_ede3_init_key, NULL, NULL,
                      NULL, des3_ctrl)
 
     BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 8,
                      EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_FIPS |
                      EVP_CIPH_FLAG_DEFAULT_ASN1, des_ede3_init_key, NULL, NULL,
                      NULL, des3_ctrl)
 
 static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                             const unsigned char *iv, int enc)
 {
     DES_cblock *deskey = (DES_cblock *)key;
     DES_EDE_KEY *dat = data(ctx);
 
     dat->stream.cbc = NULL;
 # if defined(SPARC_DES_CAPABLE)
     if (SPARC_DES_CAPABLE) {
         int mode = ctx->cipher->flags & EVP_CIPH_MODE;
 
         if (mode == EVP_CIPH_CBC_MODE) {
             des_t4_key_expand(&deskey[0], &dat->ks1);
             des_t4_key_expand(&deskey[1], &dat->ks2);
             memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1));
             dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt :
                 des_t4_ede3_cbc_decrypt;
             return 1;
         }
     }
 # endif
 # ifdef EVP_CHECK_DES_KEY
     if (DES_set_key_checked(&deskey[0], &dat->ks1)
         || DES_set_key_checked(&deskey[1], &dat->ks2))
         return 0;
 # else
     DES_set_key_unchecked(&deskey[0], &dat->ks1);
     DES_set_key_unchecked(&deskey[1], &dat->ks2);
 # endif
     memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1));
     return 1;
 }
 
 static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                              const unsigned char *iv, int enc)
 {
     DES_cblock *deskey = (DES_cblock *)key;
     DES_EDE_KEY *dat = data(ctx);
 
 # ifdef KSSL_DEBUG
     {
         int i;
         fprintf(stderr, "des_ede3_init_key(ctx=%p)\n", ctx);
         fprintf(stderr, "\tKEY= ");
         for (i = 0; i < 24; i++)
             fprintf(stderr, "%02X", key[i]);
         fprintf(stderr, "\n");
         if (iv) {
             fprintf(stderr, "\t IV= ");
             for (i = 0; i < 8; i++)
                 fprintf(stderr, "%02X", iv[i]);
             fprintf(stderr, "\n");
         }
     }
 # endif                         /* KSSL_DEBUG */
 
     dat->stream.cbc = NULL;
 # if defined(SPARC_DES_CAPABLE)
     if (SPARC_DES_CAPABLE) {
         int mode = ctx->cipher->flags & EVP_CIPH_MODE;
 
         if (mode == EVP_CIPH_CBC_MODE) {
             des_t4_key_expand(&deskey[0], &dat->ks1);
             des_t4_key_expand(&deskey[1], &dat->ks2);
             des_t4_key_expand(&deskey[2], &dat->ks3);
             dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt :
                 des_t4_ede3_cbc_decrypt;
             return 1;
         }
     }
 # endif
 # ifdef EVP_CHECK_DES_KEY
     if (DES_set_key_checked(&deskey[0], &dat->ks1)
         || DES_set_key_checked(&deskey[1], &dat->ks2)
         || DES_set_key_checked(&deskey[2], &dat->ks3))
         return 0;
 # else
     DES_set_key_unchecked(&deskey[0], &dat->ks1);
     DES_set_key_unchecked(&deskey[1], &dat->ks2);
     DES_set_key_unchecked(&deskey[2], &dat->ks3);
 # endif
     return 1;
 }
 
 static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
 {
 
     DES_cblock *deskey = ptr;
 
     switch (type) {
     case EVP_CTRL_RAND_KEY:
         if (RAND_bytes(ptr, c->key_len) <= 0)
             return 0;
         DES_set_odd_parity(deskey);
         if (c->key_len >= 16)
             DES_set_odd_parity(deskey + 1);
         if (c->key_len >= 24)
             DES_set_odd_parity(deskey + 2);
         return 1;
 
     default:
         return -1;
     }
 }
 
 const EVP_CIPHER *EVP_des_ede(void)
 {
     return &des_ede_ecb;
 }
 
 const EVP_CIPHER *EVP_des_ede3(void)
 {
     return &des_ede3_ecb;
 }
 
 # ifndef OPENSSL_NO_SHA
 
 #  include 
 
 static const unsigned char wrap_iv[8] =
     { 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05 };
 
 static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
                            const unsigned char *in, size_t inl)
 {
     unsigned char icv[8], iv[8], sha1tmp[SHA_DIGEST_LENGTH];
     int rv = -1;
     if (inl < 24)
         return -1;
-    if (!out)
+    if (out == NULL)
         return inl - 16;
     memcpy(ctx->iv, wrap_iv, 8);
     /* Decrypt first block which will end up as icv */
     des_ede_cbc_cipher(ctx, icv, in, 8);
     /* Decrypt central blocks */
     /*
      * If decrypting in place move whole output along a block so the next
      * des_ede_cbc_cipher is in place.
      */
     if (out == in) {
         memmove(out, out + 8, inl - 8);
         in -= 8;
     }
     des_ede_cbc_cipher(ctx, out, in + 8, inl - 16);
     /* Decrypt final block which will be IV */
     des_ede_cbc_cipher(ctx, iv, in + inl - 8, 8);
     /* Reverse order of everything */
     BUF_reverse(icv, NULL, 8);
     BUF_reverse(out, NULL, inl - 16);
     BUF_reverse(ctx->iv, iv, 8);
     /* Decrypt again using new IV */
     des_ede_cbc_cipher(ctx, out, out, inl - 16);
     des_ede_cbc_cipher(ctx, icv, icv, 8);
     /* Work out SHA1 hash of first portion */
     SHA1(out, inl - 16, sha1tmp);
 
     if (!CRYPTO_memcmp(sha1tmp, icv, 8))
         rv = inl - 16;
     OPENSSL_cleanse(icv, 8);
     OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
     OPENSSL_cleanse(iv, 8);
     OPENSSL_cleanse(ctx->iv, 8);
     if (rv == -1)
         OPENSSL_cleanse(out, inl - 16);
 
     return rv;
 }
 
 static int des_ede3_wrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
                          const unsigned char *in, size_t inl)
 {
     unsigned char sha1tmp[SHA_DIGEST_LENGTH];
-    if (!out)
+    if (out == NULL)
         return inl + 16;
     /* Copy input to output buffer + 8 so we have space for IV */
     memmove(out + 8, in, inl);
     /* Work out ICV */
     SHA1(in, inl, sha1tmp);
     memcpy(out + inl + 8, sha1tmp, 8);
     OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
     /* Generate random IV */
     if (RAND_bytes(ctx->iv, 8) <= 0)
         return -1;
     memcpy(out, ctx->iv, 8);
     /* Encrypt everything after IV in place */
     des_ede_cbc_cipher(ctx, out + 8, out + 8, inl + 8);
     BUF_reverse(out, NULL, inl + 16);
     memcpy(ctx->iv, wrap_iv, 8);
     des_ede_cbc_cipher(ctx, out, out, inl + 16);
     return inl + 16;
 }
 
 static int des_ede3_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                 const unsigned char *in, size_t inl)
 {
     /*
      * Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK
      * is more than will ever be needed. Also input length must be a multiple
      * of 8 bits.
      */
     if (inl >= EVP_MAXCHUNK || inl % 8)
         return -1;
     if (ctx->encrypt)
         return des_ede3_wrap(ctx, out, in, inl);
     else
         return des_ede3_unwrap(ctx, out, in, inl);
 }
 
 static const EVP_CIPHER des3_wrap = {
     NID_id_smime_alg_CMS3DESwrap,
     8, 24, 0,
     EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
         | EVP_CIPH_FLAG_DEFAULT_ASN1,
     des_ede3_init_key, des_ede3_wrap_cipher,
     NULL,
     sizeof(DES_EDE_KEY),
     NULL, NULL, NULL, NULL
 };
 
 const EVP_CIPHER *EVP_des_ede3_wrap(void)
 {
     return &des3_wrap;
 }
 
 # endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/modes/asm/aesni-gcm-x86_64.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/modes/asm/aesni-gcm-x86_64.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/modes/asm/aesni-gcm-x86_64.pl	(revision 296273)
@@ -1,1057 +1,1057 @@
 #!/usr/bin/env perl
 #
 # ====================================================================
 # Written by Andy Polyakov  for the OpenSSL
 # project. The module is, however, dual licensed under OpenSSL and
 # CRYPTOGAMS licenses depending on where you obtain it. For further
 # details see http://www.openssl.org/~appro/cryptogams/.
 # ====================================================================
 #
 #
 # AES-NI-CTR+GHASH stitch.
 #
 # February 2013
 #
 # OpenSSL GCM implementation is organized in such way that its
 # performance is rather close to the sum of its streamed components,
 # in the context parallelized AES-NI CTR and modulo-scheduled
 # PCLMULQDQ-enabled GHASH. Unfortunately, as no stitch implementation
 # was observed to perform significantly better than the sum of the
 # components on contemporary CPUs, the effort was deemed impossible to
 # justify. This module is based on combination of Intel submissions,
 # [1] and [2], with MOVBE twist suggested by Ilya Albrekht and Max
 # Locktyukhin of Intel Corp. who verified that it reduces shuffles
 # pressure with notable relative improvement, achieving 1.0 cycle per
 # byte processed with 128-bit key on Haswell processor, and 0.74 -
 # on Broadwell. [Mentioned results are raw profiled measurements for
 # favourable packet size, one divisible by 96. Applications using the
 # EVP interface will observe a few percent worse performance.]
 #
 # [1] http://rt.openssl.org/Ticket/Display.html?id=2900&user=guest&pass=guest
 # [2] http://www.intel.com/content/dam/www/public/us/en/documents/software-support/enabling-high-performance-gcm.pdf
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
-	$avx = ($1>=2.19) + ($1>=2.22);
+	$avx = ($1>=2.20) + ($1>=2.22);
 }
 
 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.09) + ($1>=2.10);
 }
 
 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$avx = ($1>=10) + ($1>=11);
 }
 
 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/) {
 	$avx = ($2>=3.0) + ($2>3.0);
 }
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 if ($avx>1) {{{
 
 ($inp,$out,$len,$key,$ivp,$Xip)=("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
 
 ($Ii,$T1,$T2,$Hkey,
  $Z0,$Z1,$Z2,$Z3,$Xi) = map("%xmm$_",(0..8));
 
 ($inout0,$inout1,$inout2,$inout3,$inout4,$inout5,$rndkey) = map("%xmm$_",(9..15));
 
 ($counter,$rounds,$ret,$const,$in0,$end0)=("%ebx","%ebp","%r10","%r11","%r14","%r15");
 
 $code=<<___;
 .text
 
 .type	_aesni_ctr32_ghash_6x,\@abi-omnipotent
 .align	32
 _aesni_ctr32_ghash_6x:
 	vmovdqu		0x20($const),$T2	# borrow $T2, .Lone_msb
 	sub		\$6,$len
 	vpxor		$Z0,$Z0,$Z0		# $Z0   = 0
 	vmovdqu		0x00-0x80($key),$rndkey
 	vpaddb		$T2,$T1,$inout1
 	vpaddb		$T2,$inout1,$inout2
 	vpaddb		$T2,$inout2,$inout3
 	vpaddb		$T2,$inout3,$inout4
 	vpaddb		$T2,$inout4,$inout5
 	vpxor		$rndkey,$T1,$inout0
 	vmovdqu		$Z0,16+8(%rsp)		# "$Z3" = 0
 	jmp		.Loop6x
 
 .align	32
 .Loop6x:
 	add		\$`6<<24`,$counter
 	jc		.Lhandle_ctr32		# discard $inout[1-5]?
 	vmovdqu		0x00-0x20($Xip),$Hkey	# $Hkey^1
 	  vpaddb	$T2,$inout5,$T1		# next counter value
 	  vpxor		$rndkey,$inout1,$inout1
 	  vpxor		$rndkey,$inout2,$inout2
 
 .Lresume_ctr32:
 	vmovdqu		$T1,($ivp)		# save next counter value
 	vpclmulqdq	\$0x10,$Hkey,$Z3,$Z1
 	  vpxor		$rndkey,$inout3,$inout3
 	  vmovups	0x10-0x80($key),$T2	# borrow $T2 for $rndkey
 	vpclmulqdq	\$0x01,$Hkey,$Z3,$Z2
 	xor		%r12,%r12
 	cmp		$in0,$end0
 
 	  vaesenc	$T2,$inout0,$inout0
 	vmovdqu		0x30+8(%rsp),$Ii	# I[4]
 	  vpxor		$rndkey,$inout4,$inout4
 	vpclmulqdq	\$0x00,$Hkey,$Z3,$T1
 	  vaesenc	$T2,$inout1,$inout1
 	  vpxor		$rndkey,$inout5,$inout5
 	setnc		%r12b
 	vpclmulqdq	\$0x11,$Hkey,$Z3,$Z3
 	  vaesenc	$T2,$inout2,$inout2
 	vmovdqu		0x10-0x20($Xip),$Hkey	# $Hkey^2
 	neg		%r12
 	  vaesenc	$T2,$inout3,$inout3
 	 vpxor		$Z1,$Z2,$Z2
 	vpclmulqdq	\$0x00,$Hkey,$Ii,$Z1
 	 vpxor		$Z0,$Xi,$Xi		# modulo-scheduled
 	  vaesenc	$T2,$inout4,$inout4
 	 vpxor		$Z1,$T1,$Z0
 	and		\$0x60,%r12
 	  vmovups	0x20-0x80($key),$rndkey
 	vpclmulqdq	\$0x10,$Hkey,$Ii,$T1
 	  vaesenc	$T2,$inout5,$inout5
 
 	vpclmulqdq	\$0x01,$Hkey,$Ii,$T2
 	lea		($in0,%r12),$in0
 	  vaesenc	$rndkey,$inout0,$inout0
 	 vpxor		16+8(%rsp),$Xi,$Xi	# modulo-scheduled [vpxor $Z3,$Xi,$Xi]
 	vpclmulqdq	\$0x11,$Hkey,$Ii,$Hkey
 	 vmovdqu	0x40+8(%rsp),$Ii	# I[3]
 	  vaesenc	$rndkey,$inout1,$inout1
 	movbe		0x58($in0),%r13
 	  vaesenc	$rndkey,$inout2,$inout2
 	movbe		0x50($in0),%r12
 	  vaesenc	$rndkey,$inout3,$inout3
 	mov		%r13,0x20+8(%rsp)
 	  vaesenc	$rndkey,$inout4,$inout4
 	mov		%r12,0x28+8(%rsp)
 	vmovdqu		0x30-0x20($Xip),$Z1	# borrow $Z1 for $Hkey^3
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vmovups	0x30-0x80($key),$rndkey
 	 vpxor		$T1,$Z2,$Z2
 	vpclmulqdq	\$0x00,$Z1,$Ii,$T1
 	  vaesenc	$rndkey,$inout0,$inout0
 	 vpxor		$T2,$Z2,$Z2
 	vpclmulqdq	\$0x10,$Z1,$Ii,$T2
 	  vaesenc	$rndkey,$inout1,$inout1
 	 vpxor		$Hkey,$Z3,$Z3
 	vpclmulqdq	\$0x01,$Z1,$Ii,$Hkey
 	  vaesenc	$rndkey,$inout2,$inout2
 	vpclmulqdq	\$0x11,$Z1,$Ii,$Z1
 	 vmovdqu	0x50+8(%rsp),$Ii	# I[2]
 	  vaesenc	$rndkey,$inout3,$inout3
 	  vaesenc	$rndkey,$inout4,$inout4
 	 vpxor		$T1,$Z0,$Z0
 	vmovdqu		0x40-0x20($Xip),$T1	# borrow $T1 for $Hkey^4
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vmovups	0x40-0x80($key),$rndkey
 	 vpxor		$T2,$Z2,$Z2
 	vpclmulqdq	\$0x00,$T1,$Ii,$T2
 	  vaesenc	$rndkey,$inout0,$inout0
 	 vpxor		$Hkey,$Z2,$Z2
 	vpclmulqdq	\$0x10,$T1,$Ii,$Hkey
 	  vaesenc	$rndkey,$inout1,$inout1
 	movbe		0x48($in0),%r13
 	 vpxor		$Z1,$Z3,$Z3
 	vpclmulqdq	\$0x01,$T1,$Ii,$Z1
 	  vaesenc	$rndkey,$inout2,$inout2
 	movbe		0x40($in0),%r12
 	vpclmulqdq	\$0x11,$T1,$Ii,$T1
 	 vmovdqu	0x60+8(%rsp),$Ii	# I[1]
 	  vaesenc	$rndkey,$inout3,$inout3
 	mov		%r13,0x30+8(%rsp)
 	  vaesenc	$rndkey,$inout4,$inout4
 	mov		%r12,0x38+8(%rsp)
 	 vpxor		$T2,$Z0,$Z0
 	vmovdqu		0x60-0x20($Xip),$T2	# borrow $T2 for $Hkey^5
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vmovups	0x50-0x80($key),$rndkey
 	 vpxor		$Hkey,$Z2,$Z2
 	vpclmulqdq	\$0x00,$T2,$Ii,$Hkey
 	  vaesenc	$rndkey,$inout0,$inout0
 	 vpxor		$Z1,$Z2,$Z2
 	vpclmulqdq	\$0x10,$T2,$Ii,$Z1
 	  vaesenc	$rndkey,$inout1,$inout1
 	movbe		0x38($in0),%r13
 	 vpxor		$T1,$Z3,$Z3
 	vpclmulqdq	\$0x01,$T2,$Ii,$T1
 	 vpxor		0x70+8(%rsp),$Xi,$Xi	# accumulate I[0]
 	  vaesenc	$rndkey,$inout2,$inout2
 	movbe		0x30($in0),%r12
 	vpclmulqdq	\$0x11,$T2,$Ii,$T2
 	  vaesenc	$rndkey,$inout3,$inout3
 	mov		%r13,0x40+8(%rsp)
 	  vaesenc	$rndkey,$inout4,$inout4
 	mov		%r12,0x48+8(%rsp)
 	 vpxor		$Hkey,$Z0,$Z0
 	 vmovdqu	0x70-0x20($Xip),$Hkey	# $Hkey^6
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vmovups	0x60-0x80($key),$rndkey
 	 vpxor		$Z1,$Z2,$Z2
 	vpclmulqdq	\$0x10,$Hkey,$Xi,$Z1
 	  vaesenc	$rndkey,$inout0,$inout0
 	 vpxor		$T1,$Z2,$Z2
 	vpclmulqdq	\$0x01,$Hkey,$Xi,$T1
 	  vaesenc	$rndkey,$inout1,$inout1
 	movbe		0x28($in0),%r13
 	 vpxor		$T2,$Z3,$Z3
 	vpclmulqdq	\$0x00,$Hkey,$Xi,$T2
 	  vaesenc	$rndkey,$inout2,$inout2
 	movbe		0x20($in0),%r12
 	vpclmulqdq	\$0x11,$Hkey,$Xi,$Xi
 	  vaesenc	$rndkey,$inout3,$inout3
 	mov		%r13,0x50+8(%rsp)
 	  vaesenc	$rndkey,$inout4,$inout4
 	mov		%r12,0x58+8(%rsp)
 	vpxor		$Z1,$Z2,$Z2
 	  vaesenc	$rndkey,$inout5,$inout5
 	vpxor		$T1,$Z2,$Z2
 
 	  vmovups	0x70-0x80($key),$rndkey
 	vpslldq		\$8,$Z2,$Z1
 	vpxor		$T2,$Z0,$Z0
 	vmovdqu		0x10($const),$Hkey	# .Lpoly
 
 	  vaesenc	$rndkey,$inout0,$inout0
 	vpxor		$Xi,$Z3,$Z3
 	  vaesenc	$rndkey,$inout1,$inout1
 	vpxor		$Z1,$Z0,$Z0
 	movbe		0x18($in0),%r13
 	  vaesenc	$rndkey,$inout2,$inout2
 	movbe		0x10($in0),%r12
 	vpalignr	\$8,$Z0,$Z0,$Ii		# 1st phase
 	vpclmulqdq	\$0x10,$Hkey,$Z0,$Z0
 	mov		%r13,0x60+8(%rsp)
 	  vaesenc	$rndkey,$inout3,$inout3
 	mov		%r12,0x68+8(%rsp)
 	  vaesenc	$rndkey,$inout4,$inout4
 	  vmovups	0x80-0x80($key),$T1	# borrow $T1 for $rndkey
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vaesenc	$T1,$inout0,$inout0
 	  vmovups	0x90-0x80($key),$rndkey
 	  vaesenc	$T1,$inout1,$inout1
 	vpsrldq		\$8,$Z2,$Z2
 	  vaesenc	$T1,$inout2,$inout2
 	vpxor		$Z2,$Z3,$Z3
 	  vaesenc	$T1,$inout3,$inout3
 	vpxor		$Ii,$Z0,$Z0
 	movbe		0x08($in0),%r13
 	  vaesenc	$T1,$inout4,$inout4
 	movbe		0x00($in0),%r12
 	  vaesenc	$T1,$inout5,$inout5
 	  vmovups	0xa0-0x80($key),$T1
 	  cmp		\$11,$rounds
 	  jb		.Lenc_tail		# 128-bit key
 
 	  vaesenc	$rndkey,$inout0,$inout0
 	  vaesenc	$rndkey,$inout1,$inout1
 	  vaesenc	$rndkey,$inout2,$inout2
 	  vaesenc	$rndkey,$inout3,$inout3
 	  vaesenc	$rndkey,$inout4,$inout4
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vaesenc	$T1,$inout0,$inout0
 	  vaesenc	$T1,$inout1,$inout1
 	  vaesenc	$T1,$inout2,$inout2
 	  vaesenc	$T1,$inout3,$inout3
 	  vaesenc	$T1,$inout4,$inout4
 	  vmovups	0xb0-0x80($key),$rndkey
 	  vaesenc	$T1,$inout5,$inout5
 	  vmovups	0xc0-0x80($key),$T1
 	  je		.Lenc_tail		# 192-bit key
 
 	  vaesenc	$rndkey,$inout0,$inout0
 	  vaesenc	$rndkey,$inout1,$inout1
 	  vaesenc	$rndkey,$inout2,$inout2
 	  vaesenc	$rndkey,$inout3,$inout3
 	  vaesenc	$rndkey,$inout4,$inout4
 	  vaesenc	$rndkey,$inout5,$inout5
 
 	  vaesenc	$T1,$inout0,$inout0
 	  vaesenc	$T1,$inout1,$inout1
 	  vaesenc	$T1,$inout2,$inout2
 	  vaesenc	$T1,$inout3,$inout3
 	  vaesenc	$T1,$inout4,$inout4
 	  vmovups	0xd0-0x80($key),$rndkey
 	  vaesenc	$T1,$inout5,$inout5
 	  vmovups	0xe0-0x80($key),$T1
 	  jmp		.Lenc_tail		# 256-bit key
 
 .align	32
 .Lhandle_ctr32:
 	vmovdqu		($const),$Ii		# borrow $Ii for .Lbswap_mask
 	  vpshufb	$Ii,$T1,$Z2		# byte-swap counter
 	  vmovdqu	0x30($const),$Z1	# borrow $Z1, .Ltwo_lsb
 	  vpaddd	0x40($const),$Z2,$inout1	# .Lone_lsb
 	  vpaddd	$Z1,$Z2,$inout2
 	vmovdqu		0x00-0x20($Xip),$Hkey	# $Hkey^1
 	  vpaddd	$Z1,$inout1,$inout3
 	  vpshufb	$Ii,$inout1,$inout1
 	  vpaddd	$Z1,$inout2,$inout4
 	  vpshufb	$Ii,$inout2,$inout2
 	  vpxor		$rndkey,$inout1,$inout1
 	  vpaddd	$Z1,$inout3,$inout5
 	  vpshufb	$Ii,$inout3,$inout3
 	  vpxor		$rndkey,$inout2,$inout2
 	  vpaddd	$Z1,$inout4,$T1		# byte-swapped next counter value
 	  vpshufb	$Ii,$inout4,$inout4
 	  vpshufb	$Ii,$inout5,$inout5
 	  vpshufb	$Ii,$T1,$T1		# next counter value
 	jmp		.Lresume_ctr32
 
 .align	32
 .Lenc_tail:
 	  vaesenc	$rndkey,$inout0,$inout0
 	vmovdqu		$Z3,16+8(%rsp)		# postpone vpxor $Z3,$Xi,$Xi
 	vpalignr	\$8,$Z0,$Z0,$Xi		# 2nd phase
 	  vaesenc	$rndkey,$inout1,$inout1
 	vpclmulqdq	\$0x10,$Hkey,$Z0,$Z0
 	  vpxor		0x00($inp),$T1,$T2
 	  vaesenc	$rndkey,$inout2,$inout2
 	  vpxor		0x10($inp),$T1,$Ii
 	  vaesenc	$rndkey,$inout3,$inout3
 	  vpxor		0x20($inp),$T1,$Z1
 	  vaesenc	$rndkey,$inout4,$inout4
 	  vpxor		0x30($inp),$T1,$Z2
 	  vaesenc	$rndkey,$inout5,$inout5
 	  vpxor		0x40($inp),$T1,$Z3
 	  vpxor		0x50($inp),$T1,$Hkey
 	  vmovdqu	($ivp),$T1		# load next counter value
 
 	  vaesenclast	$T2,$inout0,$inout0
 	  vmovdqu	0x20($const),$T2	# borrow $T2, .Lone_msb
 	  vaesenclast	$Ii,$inout1,$inout1
 	 vpaddb		$T2,$T1,$Ii
 	mov		%r13,0x70+8(%rsp)
 	lea		0x60($inp),$inp
 	  vaesenclast	$Z1,$inout2,$inout2
 	 vpaddb		$T2,$Ii,$Z1
 	mov		%r12,0x78+8(%rsp)
 	lea		0x60($out),$out
 	  vmovdqu	0x00-0x80($key),$rndkey
 	  vaesenclast	$Z2,$inout3,$inout3
 	 vpaddb		$T2,$Z1,$Z2
 	  vaesenclast	$Z3, $inout4,$inout4
 	 vpaddb		$T2,$Z2,$Z3
 	  vaesenclast	$Hkey,$inout5,$inout5
 	 vpaddb		$T2,$Z3,$Hkey
 
 	add		\$0x60,$ret
 	sub		\$0x6,$len
 	jc		.L6x_done
 
 	  vmovups	$inout0,-0x60($out)	# save output
 	 vpxor		$rndkey,$T1,$inout0
 	  vmovups	$inout1,-0x50($out)
 	 vmovdqa	$Ii,$inout1		# 0 latency
 	  vmovups	$inout2,-0x40($out)
 	 vmovdqa	$Z1,$inout2		# 0 latency
 	  vmovups	$inout3,-0x30($out)
 	 vmovdqa	$Z2,$inout3		# 0 latency
 	  vmovups	$inout4,-0x20($out)
 	 vmovdqa	$Z3,$inout4		# 0 latency
 	  vmovups	$inout5,-0x10($out)
 	 vmovdqa	$Hkey,$inout5		# 0 latency
 	vmovdqu		0x20+8(%rsp),$Z3	# I[5]
 	jmp		.Loop6x
 
 .L6x_done:
 	vpxor		16+8(%rsp),$Xi,$Xi	# modulo-scheduled
 	vpxor		$Z0,$Xi,$Xi		# modulo-scheduled
 
 	ret
 .size	_aesni_ctr32_ghash_6x,.-_aesni_ctr32_ghash_6x
 ___
 ######################################################################
 #
 # size_t aesni_gcm_[en|de]crypt(const void *inp, void *out, size_t len,
 #		const AES_KEY *key, unsigned char iv[16],
 #		struct { u128 Xi,H,Htbl[9]; } *Xip);
 $code.=<<___;
 .globl	aesni_gcm_decrypt
 .type	aesni_gcm_decrypt,\@function,6
 .align	32
 aesni_gcm_decrypt:
 	xor	$ret,$ret
 	cmp	\$0x60,$len			# minimal accepted length
 	jb	.Lgcm_dec_abort
 
 	lea	(%rsp),%rax			# save stack pointer
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 ___
 $code.=<<___ if ($win64);
 	lea	-0xa8(%rsp),%rsp
 	movaps	%xmm6,-0xd8(%rax)
 	movaps	%xmm7,-0xc8(%rax)
 	movaps	%xmm8,-0xb8(%rax)
 	movaps	%xmm9,-0xa8(%rax)
 	movaps	%xmm10,-0x98(%rax)
 	movaps	%xmm11,-0x88(%rax)
 	movaps	%xmm12,-0x78(%rax)
 	movaps	%xmm13,-0x68(%rax)
 	movaps	%xmm14,-0x58(%rax)
 	movaps	%xmm15,-0x48(%rax)
 .Lgcm_dec_body:
 ___
 $code.=<<___;
 	vzeroupper
 
 	vmovdqu		($ivp),$T1		# input counter value
 	add		\$-128,%rsp
 	mov		12($ivp),$counter
 	lea		.Lbswap_mask(%rip),$const
 	lea		-0x80($key),$in0	# borrow $in0
 	mov		\$0xf80,$end0		# borrow $end0
 	vmovdqu		($Xip),$Xi		# load Xi
 	and		\$-128,%rsp		# ensure stack alignment
 	vmovdqu		($const),$Ii		# borrow $Ii for .Lbswap_mask
 	lea		0x80($key),$key		# size optimization
 	lea		0x20+0x20($Xip),$Xip	# size optimization
 	mov		0xf0-0x80($key),$rounds
 	vpshufb		$Ii,$Xi,$Xi
 
 	and		$end0,$in0
 	and		%rsp,$end0
 	sub		$in0,$end0
 	jc		.Ldec_no_key_aliasing
 	cmp		\$768,$end0
 	jnc		.Ldec_no_key_aliasing
 	sub		$end0,%rsp		# avoid aliasing with key
 .Ldec_no_key_aliasing:
 
 	vmovdqu		0x50($inp),$Z3		# I[5]
 	lea		($inp),$in0
 	vmovdqu		0x40($inp),$Z0
 	lea		-0xc0($inp,$len),$end0
 	vmovdqu		0x30($inp),$Z1
 	shr		\$4,$len
 	xor		$ret,$ret
 	vmovdqu		0x20($inp),$Z2
 	 vpshufb	$Ii,$Z3,$Z3		# passed to _aesni_ctr32_ghash_6x
 	vmovdqu		0x10($inp),$T2
 	 vpshufb	$Ii,$Z0,$Z0
 	vmovdqu		($inp),$Hkey
 	 vpshufb	$Ii,$Z1,$Z1
 	vmovdqu		$Z0,0x30(%rsp)
 	 vpshufb	$Ii,$Z2,$Z2
 	vmovdqu		$Z1,0x40(%rsp)
 	 vpshufb	$Ii,$T2,$T2
 	vmovdqu		$Z2,0x50(%rsp)
 	 vpshufb	$Ii,$Hkey,$Hkey
 	vmovdqu		$T2,0x60(%rsp)
 	vmovdqu		$Hkey,0x70(%rsp)
 
 	call		_aesni_ctr32_ghash_6x
 
 	vmovups		$inout0,-0x60($out)	# save output
 	vmovups		$inout1,-0x50($out)
 	vmovups		$inout2,-0x40($out)
 	vmovups		$inout3,-0x30($out)
 	vmovups		$inout4,-0x20($out)
 	vmovups		$inout5,-0x10($out)
 
 	vpshufb		($const),$Xi,$Xi	# .Lbswap_mask
 	vmovdqu		$Xi,-0x40($Xip)		# output Xi
 
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
 	movaps	-0xd8(%rax),%xmm6
-	movaps	-0xd8(%rax),%xmm7
+	movaps	-0xc8(%rax),%xmm7
 	movaps	-0xb8(%rax),%xmm8
 	movaps	-0xa8(%rax),%xmm9
 	movaps	-0x98(%rax),%xmm10
 	movaps	-0x88(%rax),%xmm11
 	movaps	-0x78(%rax),%xmm12
 	movaps	-0x68(%rax),%xmm13
 	movaps	-0x58(%rax),%xmm14
 	movaps	-0x48(%rax),%xmm15
 ___
 $code.=<<___;
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	lea	(%rax),%rsp		# restore %rsp
 .Lgcm_dec_abort:
 	mov	$ret,%rax		# return value
 	ret
 .size	aesni_gcm_decrypt,.-aesni_gcm_decrypt
 ___
 
 $code.=<<___;
 .type	_aesni_ctr32_6x,\@abi-omnipotent
 .align	32
 _aesni_ctr32_6x:
 	vmovdqu		0x00-0x80($key),$Z0	# borrow $Z0 for $rndkey
 	vmovdqu		0x20($const),$T2	# borrow $T2, .Lone_msb
 	lea		-1($rounds),%r13
 	vmovups		0x10-0x80($key),$rndkey
 	lea		0x20-0x80($key),%r12
 	vpxor		$Z0,$T1,$inout0
 	add		\$`6<<24`,$counter
 	jc		.Lhandle_ctr32_2
 	vpaddb		$T2,$T1,$inout1
 	vpaddb		$T2,$inout1,$inout2
 	vpxor		$Z0,$inout1,$inout1
 	vpaddb		$T2,$inout2,$inout3
 	vpxor		$Z0,$inout2,$inout2
 	vpaddb		$T2,$inout3,$inout4
 	vpxor		$Z0,$inout3,$inout3
 	vpaddb		$T2,$inout4,$inout5
 	vpxor		$Z0,$inout4,$inout4
 	vpaddb		$T2,$inout5,$T1
 	vpxor		$Z0,$inout5,$inout5
 	jmp		.Loop_ctr32
 
 .align	16
 .Loop_ctr32:
 	vaesenc		$rndkey,$inout0,$inout0
 	vaesenc		$rndkey,$inout1,$inout1
 	vaesenc		$rndkey,$inout2,$inout2
 	vaesenc		$rndkey,$inout3,$inout3
 	vaesenc		$rndkey,$inout4,$inout4
 	vaesenc		$rndkey,$inout5,$inout5
 	vmovups		(%r12),$rndkey
 	lea		0x10(%r12),%r12
 	dec		%r13d
 	jnz		.Loop_ctr32
 
 	vmovdqu		(%r12),$Hkey		# last round key
 	vaesenc		$rndkey,$inout0,$inout0
 	vpxor		0x00($inp),$Hkey,$Z0
 	vaesenc		$rndkey,$inout1,$inout1
 	vpxor		0x10($inp),$Hkey,$Z1
 	vaesenc		$rndkey,$inout2,$inout2
 	vpxor		0x20($inp),$Hkey,$Z2
 	vaesenc		$rndkey,$inout3,$inout3
 	vpxor		0x30($inp),$Hkey,$Xi
 	vaesenc		$rndkey,$inout4,$inout4
 	vpxor		0x40($inp),$Hkey,$T2
 	vaesenc		$rndkey,$inout5,$inout5
 	vpxor		0x50($inp),$Hkey,$Hkey
 	lea		0x60($inp),$inp
 
 	vaesenclast	$Z0,$inout0,$inout0
 	vaesenclast	$Z1,$inout1,$inout1
 	vaesenclast	$Z2,$inout2,$inout2
 	vaesenclast	$Xi,$inout3,$inout3
 	vaesenclast	$T2,$inout4,$inout4
 	vaesenclast	$Hkey,$inout5,$inout5
 	vmovups		$inout0,0x00($out)
 	vmovups		$inout1,0x10($out)
 	vmovups		$inout2,0x20($out)
 	vmovups		$inout3,0x30($out)
 	vmovups		$inout4,0x40($out)
 	vmovups		$inout5,0x50($out)
 	lea		0x60($out),$out
 
 	ret
 .align	32
 .Lhandle_ctr32_2:
 	vpshufb		$Ii,$T1,$Z2		# byte-swap counter
 	vmovdqu		0x30($const),$Z1	# borrow $Z1, .Ltwo_lsb
 	vpaddd		0x40($const),$Z2,$inout1	# .Lone_lsb
 	vpaddd		$Z1,$Z2,$inout2
 	vpaddd		$Z1,$inout1,$inout3
 	vpshufb		$Ii,$inout1,$inout1
 	vpaddd		$Z1,$inout2,$inout4
 	vpshufb		$Ii,$inout2,$inout2
 	vpxor		$Z0,$inout1,$inout1
 	vpaddd		$Z1,$inout3,$inout5
 	vpshufb		$Ii,$inout3,$inout3
 	vpxor		$Z0,$inout2,$inout2
 	vpaddd		$Z1,$inout4,$T1		# byte-swapped next counter value
 	vpshufb		$Ii,$inout4,$inout4
 	vpxor		$Z0,$inout3,$inout3
 	vpshufb		$Ii,$inout5,$inout5
 	vpxor		$Z0,$inout4,$inout4
 	vpshufb		$Ii,$T1,$T1		# next counter value
 	vpxor		$Z0,$inout5,$inout5
 	jmp	.Loop_ctr32
 .size	_aesni_ctr32_6x,.-_aesni_ctr32_6x
 
 .globl	aesni_gcm_encrypt
 .type	aesni_gcm_encrypt,\@function,6
 .align	32
 aesni_gcm_encrypt:
 	xor	$ret,$ret
 	cmp	\$0x60*3,$len			# minimal accepted length
 	jb	.Lgcm_enc_abort
 
 	lea	(%rsp),%rax			# save stack pointer
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 ___
 $code.=<<___ if ($win64);
 	lea	-0xa8(%rsp),%rsp
 	movaps	%xmm6,-0xd8(%rax)
 	movaps	%xmm7,-0xc8(%rax)
 	movaps	%xmm8,-0xb8(%rax)
 	movaps	%xmm9,-0xa8(%rax)
 	movaps	%xmm10,-0x98(%rax)
 	movaps	%xmm11,-0x88(%rax)
 	movaps	%xmm12,-0x78(%rax)
 	movaps	%xmm13,-0x68(%rax)
 	movaps	%xmm14,-0x58(%rax)
 	movaps	%xmm15,-0x48(%rax)
 .Lgcm_enc_body:
 ___
 $code.=<<___;
 	vzeroupper
 
 	vmovdqu		($ivp),$T1		# input counter value
 	add		\$-128,%rsp
 	mov		12($ivp),$counter
 	lea		.Lbswap_mask(%rip),$const
 	lea		-0x80($key),$in0	# borrow $in0
 	mov		\$0xf80,$end0		# borrow $end0
 	lea		0x80($key),$key		# size optimization
 	vmovdqu		($const),$Ii		# borrow $Ii for .Lbswap_mask
 	and		\$-128,%rsp		# ensure stack alignment
 	mov		0xf0-0x80($key),$rounds
 
 	and		$end0,$in0
 	and		%rsp,$end0
 	sub		$in0,$end0
 	jc		.Lenc_no_key_aliasing
 	cmp		\$768,$end0
 	jnc		.Lenc_no_key_aliasing
 	sub		$end0,%rsp		# avoid aliasing with key
 .Lenc_no_key_aliasing:
 
 	lea		($out),$in0
 	lea		-0xc0($out,$len),$end0
 	shr		\$4,$len
 
 	call		_aesni_ctr32_6x
 	vpshufb		$Ii,$inout0,$Xi		# save bswapped output on stack
 	vpshufb		$Ii,$inout1,$T2
 	vmovdqu		$Xi,0x70(%rsp)
 	vpshufb		$Ii,$inout2,$Z0
 	vmovdqu		$T2,0x60(%rsp)
 	vpshufb		$Ii,$inout3,$Z1
 	vmovdqu		$Z0,0x50(%rsp)
 	vpshufb		$Ii,$inout4,$Z2
 	vmovdqu		$Z1,0x40(%rsp)
 	vpshufb		$Ii,$inout5,$Z3		# passed to _aesni_ctr32_ghash_6x
 	vmovdqu		$Z2,0x30(%rsp)
 
 	call		_aesni_ctr32_6x
 
 	vmovdqu		($Xip),$Xi		# load Xi
 	lea		0x20+0x20($Xip),$Xip	# size optimization
 	sub		\$12,$len
 	mov		\$0x60*2,$ret
 	vpshufb		$Ii,$Xi,$Xi
 
 	call		_aesni_ctr32_ghash_6x
 	vmovdqu		0x20(%rsp),$Z3		# I[5]
 	 vmovdqu	($const),$Ii		# borrow $Ii for .Lbswap_mask
 	vmovdqu		0x00-0x20($Xip),$Hkey	# $Hkey^1
 	vpunpckhqdq	$Z3,$Z3,$T1
 	vmovdqu		0x20-0x20($Xip),$rndkey	# borrow $rndkey for $HK
 	 vmovups	$inout0,-0x60($out)	# save output
 	 vpshufb	$Ii,$inout0,$inout0	# but keep bswapped copy
 	vpxor		$Z3,$T1,$T1
 	 vmovups	$inout1,-0x50($out)
 	 vpshufb	$Ii,$inout1,$inout1
 	 vmovups	$inout2,-0x40($out)
 	 vpshufb	$Ii,$inout2,$inout2
 	 vmovups	$inout3,-0x30($out)
 	 vpshufb	$Ii,$inout3,$inout3
 	 vmovups	$inout4,-0x20($out)
 	 vpshufb	$Ii,$inout4,$inout4
 	 vmovups	$inout5,-0x10($out)
 	 vpshufb	$Ii,$inout5,$inout5
 	 vmovdqu	$inout0,0x10(%rsp)	# free $inout0
 ___
 { my ($HK,$T3)=($rndkey,$inout0);
 
 $code.=<<___;
 	 vmovdqu	0x30(%rsp),$Z2		# I[4]
 	 vmovdqu	0x10-0x20($Xip),$Ii	# borrow $Ii for $Hkey^2
 	 vpunpckhqdq	$Z2,$Z2,$T2
 	vpclmulqdq	\$0x00,$Hkey,$Z3,$Z1
 	 vpxor		$Z2,$T2,$T2
 	vpclmulqdq	\$0x11,$Hkey,$Z3,$Z3
 	vpclmulqdq	\$0x00,$HK,$T1,$T1
 
 	 vmovdqu	0x40(%rsp),$T3		# I[3]
 	vpclmulqdq	\$0x00,$Ii,$Z2,$Z0
 	 vmovdqu	0x30-0x20($Xip),$Hkey	# $Hkey^3
 	vpxor		$Z1,$Z0,$Z0
 	 vpunpckhqdq	$T3,$T3,$Z1
 	vpclmulqdq	\$0x11,$Ii,$Z2,$Z2
 	 vpxor		$T3,$Z1,$Z1
 	vpxor		$Z3,$Z2,$Z2
 	vpclmulqdq	\$0x10,$HK,$T2,$T2
 	 vmovdqu	0x50-0x20($Xip),$HK
 	vpxor		$T1,$T2,$T2
 
 	 vmovdqu	0x50(%rsp),$T1		# I[2]
 	vpclmulqdq	\$0x00,$Hkey,$T3,$Z3
 	 vmovdqu	0x40-0x20($Xip),$Ii	# borrow $Ii for $Hkey^4
 	vpxor		$Z0,$Z3,$Z3
 	 vpunpckhqdq	$T1,$T1,$Z0
 	vpclmulqdq	\$0x11,$Hkey,$T3,$T3
 	 vpxor		$T1,$Z0,$Z0
 	vpxor		$Z2,$T3,$T3
 	vpclmulqdq	\$0x00,$HK,$Z1,$Z1
 	vpxor		$T2,$Z1,$Z1
 
 	 vmovdqu	0x60(%rsp),$T2		# I[1]
 	vpclmulqdq	\$0x00,$Ii,$T1,$Z2
 	 vmovdqu	0x60-0x20($Xip),$Hkey	# $Hkey^5
 	vpxor		$Z3,$Z2,$Z2
 	 vpunpckhqdq	$T2,$T2,$Z3
 	vpclmulqdq	\$0x11,$Ii,$T1,$T1
 	 vpxor		$T2,$Z3,$Z3
 	vpxor		$T3,$T1,$T1
 	vpclmulqdq	\$0x10,$HK,$Z0,$Z0
 	 vmovdqu	0x80-0x20($Xip),$HK
 	vpxor		$Z1,$Z0,$Z0
 
 	 vpxor		0x70(%rsp),$Xi,$Xi	# accumulate I[0]
 	vpclmulqdq	\$0x00,$Hkey,$T2,$Z1
 	 vmovdqu	0x70-0x20($Xip),$Ii	# borrow $Ii for $Hkey^6
 	 vpunpckhqdq	$Xi,$Xi,$T3
 	vpxor		$Z2,$Z1,$Z1
 	vpclmulqdq	\$0x11,$Hkey,$T2,$T2
 	 vpxor		$Xi,$T3,$T3
 	vpxor		$T1,$T2,$T2
 	vpclmulqdq	\$0x00,$HK,$Z3,$Z3
 	vpxor		$Z0,$Z3,$Z0
 
 	vpclmulqdq	\$0x00,$Ii,$Xi,$Z2
 	 vmovdqu	0x00-0x20($Xip),$Hkey	# $Hkey^1
 	 vpunpckhqdq	$inout5,$inout5,$T1
 	vpclmulqdq	\$0x11,$Ii,$Xi,$Xi
 	 vpxor		$inout5,$T1,$T1
 	vpxor		$Z1,$Z2,$Z1
 	vpclmulqdq	\$0x10,$HK,$T3,$T3
 	 vmovdqu	0x20-0x20($Xip),$HK
 	vpxor		$T2,$Xi,$Z3
 	vpxor		$Z0,$T3,$Z2
 
 	 vmovdqu	0x10-0x20($Xip),$Ii	# borrow $Ii for $Hkey^2
 	  vpxor		$Z1,$Z3,$T3		# aggregated Karatsuba post-processing
 	vpclmulqdq	\$0x00,$Hkey,$inout5,$Z0
 	  vpxor		$T3,$Z2,$Z2
 	 vpunpckhqdq	$inout4,$inout4,$T2
 	vpclmulqdq	\$0x11,$Hkey,$inout5,$inout5
 	 vpxor		$inout4,$T2,$T2
 	  vpslldq	\$8,$Z2,$T3
 	vpclmulqdq	\$0x00,$HK,$T1,$T1
 	  vpxor		$T3,$Z1,$Xi
 	  vpsrldq	\$8,$Z2,$Z2
 	  vpxor		$Z2,$Z3,$Z3
 
 	vpclmulqdq	\$0x00,$Ii,$inout4,$Z1
 	 vmovdqu	0x30-0x20($Xip),$Hkey	# $Hkey^3
 	vpxor		$Z0,$Z1,$Z1
 	 vpunpckhqdq	$inout3,$inout3,$T3
 	vpclmulqdq	\$0x11,$Ii,$inout4,$inout4
 	 vpxor		$inout3,$T3,$T3
 	vpxor		$inout5,$inout4,$inout4
 	  vpalignr	\$8,$Xi,$Xi,$inout5	# 1st phase
 	vpclmulqdq	\$0x10,$HK,$T2,$T2
 	 vmovdqu	0x50-0x20($Xip),$HK
 	vpxor		$T1,$T2,$T2
 
 	vpclmulqdq	\$0x00,$Hkey,$inout3,$Z0
 	 vmovdqu	0x40-0x20($Xip),$Ii	# borrow $Ii for $Hkey^4
 	vpxor		$Z1,$Z0,$Z0
 	 vpunpckhqdq	$inout2,$inout2,$T1
 	vpclmulqdq	\$0x11,$Hkey,$inout3,$inout3
 	 vpxor		$inout2,$T1,$T1
 	vpxor		$inout4,$inout3,$inout3
 	  vxorps	0x10(%rsp),$Z3,$Z3	# accumulate $inout0
 	vpclmulqdq	\$0x00,$HK,$T3,$T3
 	vpxor		$T2,$T3,$T3
 
 	  vpclmulqdq	\$0x10,0x10($const),$Xi,$Xi
 	  vxorps	$inout5,$Xi,$Xi
 
 	vpclmulqdq	\$0x00,$Ii,$inout2,$Z1
 	 vmovdqu	0x60-0x20($Xip),$Hkey	# $Hkey^5
 	vpxor		$Z0,$Z1,$Z1
 	 vpunpckhqdq	$inout1,$inout1,$T2
 	vpclmulqdq	\$0x11,$Ii,$inout2,$inout2
 	 vpxor		$inout1,$T2,$T2
 	  vpalignr	\$8,$Xi,$Xi,$inout5	# 2nd phase
 	vpxor		$inout3,$inout2,$inout2
 	vpclmulqdq	\$0x10,$HK,$T1,$T1
 	 vmovdqu	0x80-0x20($Xip),$HK
 	vpxor		$T3,$T1,$T1
 
 	  vxorps	$Z3,$inout5,$inout5
 	  vpclmulqdq	\$0x10,0x10($const),$Xi,$Xi
 	  vxorps	$inout5,$Xi,$Xi
 
 	vpclmulqdq	\$0x00,$Hkey,$inout1,$Z0
 	 vmovdqu	0x70-0x20($Xip),$Ii	# borrow $Ii for $Hkey^6
 	vpxor		$Z1,$Z0,$Z0
 	 vpunpckhqdq	$Xi,$Xi,$T3
 	vpclmulqdq	\$0x11,$Hkey,$inout1,$inout1
 	 vpxor		$Xi,$T3,$T3
 	vpxor		$inout2,$inout1,$inout1
 	vpclmulqdq	\$0x00,$HK,$T2,$T2
 	vpxor		$T1,$T2,$T2
 
 	vpclmulqdq	\$0x00,$Ii,$Xi,$Z1
 	vpclmulqdq	\$0x11,$Ii,$Xi,$Z3
 	vpxor		$Z0,$Z1,$Z1
 	vpclmulqdq	\$0x10,$HK,$T3,$Z2
 	vpxor		$inout1,$Z3,$Z3
 	vpxor		$T2,$Z2,$Z2
 
 	vpxor		$Z1,$Z3,$Z0		# aggregated Karatsuba post-processing
 	vpxor		$Z0,$Z2,$Z2
 	vpslldq		\$8,$Z2,$T1
 	vmovdqu		0x10($const),$Hkey	# .Lpoly
 	vpsrldq		\$8,$Z2,$Z2
 	vpxor		$T1,$Z1,$Xi
 	vpxor		$Z2,$Z3,$Z3
 
 	vpalignr	\$8,$Xi,$Xi,$T2		# 1st phase
 	vpclmulqdq	\$0x10,$Hkey,$Xi,$Xi
 	vpxor		$T2,$Xi,$Xi
 
 	vpalignr	\$8,$Xi,$Xi,$T2		# 2nd phase
 	vpclmulqdq	\$0x10,$Hkey,$Xi,$Xi
 	vpxor		$Z3,$T2,$T2
 	vpxor		$T2,$Xi,$Xi
 ___
 }
 $code.=<<___;
 	vpshufb		($const),$Xi,$Xi	# .Lbswap_mask
 	vmovdqu		$Xi,-0x40($Xip)		# output Xi
 
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
 	movaps	-0xd8(%rax),%xmm6
 	movaps	-0xc8(%rax),%xmm7
 	movaps	-0xb8(%rax),%xmm8
 	movaps	-0xa8(%rax),%xmm9
 	movaps	-0x98(%rax),%xmm10
 	movaps	-0x88(%rax),%xmm11
 	movaps	-0x78(%rax),%xmm12
 	movaps	-0x68(%rax),%xmm13
 	movaps	-0x58(%rax),%xmm14
 	movaps	-0x48(%rax),%xmm15
 ___
 $code.=<<___;
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	lea	(%rax),%rsp		# restore %rsp
 .Lgcm_enc_abort:
 	mov	$ret,%rax		# return value
 	ret
 .size	aesni_gcm_encrypt,.-aesni_gcm_encrypt
 ___
 
 $code.=<<___;
 .align	64
 .Lbswap_mask:
 	.byte	15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
 .Lpoly:
 	.byte	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
 .Lone_msb:
 	.byte	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
 .Ltwo_lsb:
 	.byte	2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 .Lone_lsb:
 	.byte	1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
 .asciz	"AES-NI GCM module for x86_64, CRYPTOGAMS by "
 .align	64
 ___
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___
 .extern	__imp_RtlVirtualUnwind
 .type	gcm_se_handler,\@abi-omnipotent
 .align	16
 gcm_se_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lcommon_seh_tail
 
 	mov	120($context),%rax	# pull context->Rax
 
 	mov	-48(%rax),%r15
 	mov	-40(%rax),%r14
 	mov	-32(%rax),%r13
 	mov	-24(%rax),%r12
 	mov	-16(%rax),%rbp
 	mov	-8(%rax),%rbx
 	mov	%r15,240($context)
 	mov	%r14,232($context)
 	mov	%r13,224($context)
 	mov	%r12,216($context)
 	mov	%rbp,160($context)
 	mov	%rbx,144($context)
 
 	lea	-0xd8(%rax),%rsi	# %xmm save area
 	lea	512($context),%rdi	# & context.Xmm6
 	mov	\$20,%ecx		# 10*sizeof(%xmm0)/sizeof(%rax)
 	.long	0xa548f3fc		# cld; rep movsq
 
 .Lcommon_seh_tail:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$154,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
 .size	gcm_se_handler,.-gcm_se_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_aesni_gcm_decrypt
 	.rva	.LSEH_end_aesni_gcm_decrypt
 	.rva	.LSEH_gcm_dec_info
 
 	.rva	.LSEH_begin_aesni_gcm_encrypt
 	.rva	.LSEH_end_aesni_gcm_encrypt
 	.rva	.LSEH_gcm_enc_info
 .section	.xdata
 .align	8
 .LSEH_gcm_dec_info:
 	.byte	9,0,0,0
 	.rva	gcm_se_handler
 	.rva	.Lgcm_dec_body,.Lgcm_dec_abort
 .LSEH_gcm_enc_info:
 	.byte	9,0,0,0
 	.rva	gcm_se_handler
 	.rva	.Lgcm_enc_body,.Lgcm_enc_abort
 ___
 }
 }}} else {{{
 $code=<<___;	# assembler is too old
 .text
 
 .globl	aesni_gcm_encrypt
 .type	aesni_gcm_encrypt,\@abi-omnipotent
 aesni_gcm_encrypt:
 	xor	%eax,%eax
 	ret
 .size	aesni_gcm_encrypt,.-aesni_gcm_encrypt
 
 .globl	aesni_gcm_decrypt
 .type	aesni_gcm_decrypt,\@abi-omnipotent
 aesni_gcm_decrypt:
 	xor	%eax,%eax
 	ret
 .size	aesni_gcm_decrypt,.-aesni_gcm_decrypt
 ___
 }}}
 
 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
 
 print $code;
 
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/modes/asm/ghash-x86_64.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/modes/asm/ghash-x86_64.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/modes/asm/ghash-x86_64.pl	(revision 296273)
@@ -1,1753 +1,1753 @@
 #!/usr/bin/env perl
 #
 # ====================================================================
 # Written by Andy Polyakov  for the OpenSSL
 # project. The module is, however, dual licensed under OpenSSL and
 # CRYPTOGAMS licenses depending on where you obtain it. For further
 # details see http://www.openssl.org/~appro/cryptogams/.
 # ====================================================================
 #
 # March, June 2010
 #
 # The module implements "4-bit" GCM GHASH function and underlying
 # single multiplication operation in GF(2^128). "4-bit" means that
 # it uses 256 bytes per-key table [+128 bytes shared table]. GHASH
 # function features so called "528B" variant utilizing additional
 # 256+16 bytes of per-key storage [+512 bytes shared table].
 # Performance results are for this streamed GHASH subroutine and are
 # expressed in cycles per processed byte, less is better:
 #
 #		gcc 3.4.x(*)	assembler
 #
 # P4		28.6		14.0		+100%
 # Opteron	19.3		7.7		+150%
 # Core2		17.8		8.1(**)		+120%
 # Atom		31.6		16.8		+88%
 # VIA Nano	21.8		10.1		+115%
 #
 # (*)	comparison is not completely fair, because C results are
 #	for vanilla "256B" implementation, while assembler results
 #	are for "528B";-)
 # (**)	it's mystery [to me] why Core2 result is not same as for
 #	Opteron;
 
 # May 2010
 #
 # Add PCLMULQDQ version performing at 2.02 cycles per processed byte.
 # See ghash-x86.pl for background information and details about coding
 # techniques.
 #
 # Special thanks to David Woodhouse  for
 # providing access to a Westmere-based system on behalf of Intel
 # Open Source Technology Centre.
 
 # December 2012
 #
 # Overhaul: aggregate Karatsuba post-processing, improve ILP in
 # reduction_alg9, increase reduction aggregate factor to 4x. As for
 # the latter. ghash-x86.pl discusses that it makes lesser sense to
 # increase aggregate factor. Then why increase here? Critical path
 # consists of 3 independent pclmulqdq instructions, Karatsuba post-
 # processing and reduction. "On top" of this we lay down aggregated
 # multiplication operations, triplets of independent pclmulqdq's. As
 # issue rate for pclmulqdq is limited, it makes lesser sense to
 # aggregate more multiplications than it takes to perform remaining
 # non-multiplication operations. 2x is near-optimal coefficient for
 # contemporary Intel CPUs (therefore modest improvement coefficient),
 # but not for Bulldozer. Latter is because logical SIMD operations
 # are twice as slow in comparison to Intel, so that critical path is
 # longer. A CPU with higher pclmulqdq issue rate would also benefit
 # from higher aggregate factor...
 #
 # Westmere	1.78(+13%)
 # Sandy Bridge	1.80(+8%)
 # Ivy Bridge	1.80(+7%)
 # Haswell	0.55(+93%) (if system doesn't support AVX)
 # Broadwell	0.45(+110%)(if system doesn't support AVX)
 # Bulldozer	1.49(+27%)
 # Silvermont	2.88(+13%)
 
 # March 2013
 #
 # ... 8x aggregate factor AVX code path is using reduction algorithm
 # suggested by Shay Gueron[1]. Even though contemporary AVX-capable
 # CPUs such as Sandy and Ivy Bridge can execute it, the code performs
 # sub-optimally in comparison to above mentioned version. But thanks
 # to Ilya Albrekht and Max Locktyukhin of Intel Corp. we knew that
 # it performs in 0.41 cycles per byte on Haswell processor, and in
 # 0.29 on Broadwell.
 #
 # [1] http://rt.openssl.org/Ticket/Display.html?id=2900&user=guest&pass=guest
 
 $flavour = shift;
 $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
 
 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
 die "can't locate x86_64-xlate.pl";
 
 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
 		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
-	$avx = ($1>=2.19) + ($1>=2.22);
+	$avx = ($1>=2.20) + ($1>=2.22);
 }
 
 if (!$avx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
 	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
 	$avx = ($1>=2.09) + ($1>=2.10);
 }
 
 if (!$avx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
 	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
 	$avx = ($1>=10) + ($1>=11);
 }
 
 if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9]\.[0-9]+)/) {
 	$avx = ($2>=3.0) + ($2>3.0);
 }
 
 open OUT,"| \"$^X\" $xlate $flavour $output";
 *STDOUT=*OUT;
 
 $do4xaggr=1;
 
 # common register layout
 $nlo="%rax";
 $nhi="%rbx";
 $Zlo="%r8";
 $Zhi="%r9";
 $tmp="%r10";
 $rem_4bit = "%r11";
 
 $Xi="%rdi";
 $Htbl="%rsi";
 
 # per-function register layout
 $cnt="%rcx";
 $rem="%rdx";
 
 sub LB() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/	or
 			$r =~ s/%[er]([sd]i)/%\1l/	or
 			$r =~ s/%[er](bp)/%\1l/		or
 			$r =~ s/%(r[0-9]+)[d]?/%\1b/;   $r; }
 
 sub AUTOLOAD()		# thunk [simplified] 32-bit style perlasm
 { my $opcode = $AUTOLOAD; $opcode =~ s/.*:://;
   my $arg = pop;
     $arg = "\$$arg" if ($arg*1 eq $arg);
     $code .= "\t$opcode\t".join(',',$arg,reverse @_)."\n";
 }
 
 { my $N;
   sub loop() {
   my $inp = shift;
 
 	$N++;
 $code.=<<___;
 	xor	$nlo,$nlo
 	xor	$nhi,$nhi
 	mov	`&LB("$Zlo")`,`&LB("$nlo")`
 	mov	`&LB("$Zlo")`,`&LB("$nhi")`
 	shl	\$4,`&LB("$nlo")`
 	mov	\$14,$cnt
 	mov	8($Htbl,$nlo),$Zlo
 	mov	($Htbl,$nlo),$Zhi
 	and	\$0xf0,`&LB("$nhi")`
 	mov	$Zlo,$rem
 	jmp	.Loop$N
 
 .align	16
 .Loop$N:
 	shr	\$4,$Zlo
 	and	\$0xf,$rem
 	mov	$Zhi,$tmp
 	mov	($inp,$cnt),`&LB("$nlo")`
 	shr	\$4,$Zhi
 	xor	8($Htbl,$nhi),$Zlo
 	shl	\$60,$tmp
 	xor	($Htbl,$nhi),$Zhi
 	mov	`&LB("$nlo")`,`&LB("$nhi")`
 	xor	($rem_4bit,$rem,8),$Zhi
 	mov	$Zlo,$rem
 	shl	\$4,`&LB("$nlo")`
 	xor	$tmp,$Zlo
 	dec	$cnt
 	js	.Lbreak$N
 
 	shr	\$4,$Zlo
 	and	\$0xf,$rem
 	mov	$Zhi,$tmp
 	shr	\$4,$Zhi
 	xor	8($Htbl,$nlo),$Zlo
 	shl	\$60,$tmp
 	xor	($Htbl,$nlo),$Zhi
 	and	\$0xf0,`&LB("$nhi")`
 	xor	($rem_4bit,$rem,8),$Zhi
 	mov	$Zlo,$rem
 	xor	$tmp,$Zlo
 	jmp	.Loop$N
 
 .align	16
 .Lbreak$N:
 	shr	\$4,$Zlo
 	and	\$0xf,$rem
 	mov	$Zhi,$tmp
 	shr	\$4,$Zhi
 	xor	8($Htbl,$nlo),$Zlo
 	shl	\$60,$tmp
 	xor	($Htbl,$nlo),$Zhi
 	and	\$0xf0,`&LB("$nhi")`
 	xor	($rem_4bit,$rem,8),$Zhi
 	mov	$Zlo,$rem
 	xor	$tmp,$Zlo
 
 	shr	\$4,$Zlo
 	and	\$0xf,$rem
 	mov	$Zhi,$tmp
 	shr	\$4,$Zhi
 	xor	8($Htbl,$nhi),$Zlo
 	shl	\$60,$tmp
 	xor	($Htbl,$nhi),$Zhi
 	xor	$tmp,$Zlo
 	xor	($rem_4bit,$rem,8),$Zhi
 
 	bswap	$Zlo
 	bswap	$Zhi
 ___
 }}
 
 $code=<<___;
 .text
 .extern	OPENSSL_ia32cap_P
 
 .globl	gcm_gmult_4bit
 .type	gcm_gmult_4bit,\@function,2
 .align	16
 gcm_gmult_4bit:
 	push	%rbx
 	push	%rbp		# %rbp and %r12 are pushed exclusively in
 	push	%r12		# order to reuse Win64 exception handler...
 .Lgmult_prologue:
 
 	movzb	15($Xi),$Zlo
 	lea	.Lrem_4bit(%rip),$rem_4bit
 ___
 	&loop	($Xi);
 $code.=<<___;
 	mov	$Zlo,8($Xi)
 	mov	$Zhi,($Xi)
 
 	mov	16(%rsp),%rbx
 	lea	24(%rsp),%rsp
 .Lgmult_epilogue:
 	ret
 .size	gcm_gmult_4bit,.-gcm_gmult_4bit
 ___
 
 # per-function register layout
 $inp="%rdx";
 $len="%rcx";
 $rem_8bit=$rem_4bit;
 
 $code.=<<___;
 .globl	gcm_ghash_4bit
 .type	gcm_ghash_4bit,\@function,4
 .align	16
 gcm_ghash_4bit:
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	sub	\$280,%rsp
 .Lghash_prologue:
 	mov	$inp,%r14		# reassign couple of args
 	mov	$len,%r15
 ___
 { my $inp="%r14";
   my $dat="%edx";
   my $len="%r15";
   my @nhi=("%ebx","%ecx");
   my @rem=("%r12","%r13");
   my $Hshr4="%rbp";
 
 	&sub	($Htbl,-128);		# size optimization
 	&lea	($Hshr4,"16+128(%rsp)");
 	{ my @lo =($nlo,$nhi);
           my @hi =($Zlo,$Zhi);
 
 	  &xor	($dat,$dat);
 	  for ($i=0,$j=-2;$i<18;$i++,$j++) {
 	    &mov	("$j(%rsp)",&LB($dat))		if ($i>1);
 	    &or		($lo[0],$tmp)			if ($i>1);
 	    &mov	(&LB($dat),&LB($lo[1]))		if ($i>0 && $i<17);
 	    &shr	($lo[1],4)			if ($i>0 && $i<17);
 	    &mov	($tmp,$hi[1])			if ($i>0 && $i<17);
 	    &shr	($hi[1],4)			if ($i>0 && $i<17);
 	    &mov	("8*$j($Hshr4)",$hi[0])		if ($i>1);
 	    &mov	($hi[0],"16*$i+0-128($Htbl)")	if ($i<16);
 	    &shl	(&LB($dat),4)			if ($i>0 && $i<17);
 	    &mov	("8*$j-128($Hshr4)",$lo[0])	if ($i>1);
 	    &mov	($lo[0],"16*$i+8-128($Htbl)")	if ($i<16);
 	    &shl	($tmp,60)			if ($i>0 && $i<17);
 
 	    push	(@lo,shift(@lo));
 	    push	(@hi,shift(@hi));
 	  }
 	}
 	&add	($Htbl,-128);
 	&mov	($Zlo,"8($Xi)");
 	&mov	($Zhi,"0($Xi)");
 	&add	($len,$inp);		# pointer to the end of data
 	&lea	($rem_8bit,".Lrem_8bit(%rip)");
 	&jmp	(".Louter_loop");
 
 $code.=".align	16\n.Louter_loop:\n";
 	&xor	($Zhi,"($inp)");
 	&mov	("%rdx","8($inp)");
 	&lea	($inp,"16($inp)");
 	&xor	("%rdx",$Zlo);
 	&mov	("($Xi)",$Zhi);
 	&mov	("8($Xi)","%rdx");
 	&shr	("%rdx",32);
 
 	&xor	($nlo,$nlo);
 	&rol	($dat,8);
 	&mov	(&LB($nlo),&LB($dat));
 	&movz	($nhi[0],&LB($dat));
 	&shl	(&LB($nlo),4);
 	&shr	($nhi[0],4);
 
 	for ($j=11,$i=0;$i<15;$i++) {
 	    &rol	($dat,8);
 	    &xor	($Zlo,"8($Htbl,$nlo)")			if ($i>0);
 	    &xor	($Zhi,"($Htbl,$nlo)")			if ($i>0);
 	    &mov	($Zlo,"8($Htbl,$nlo)")			if ($i==0);
 	    &mov	($Zhi,"($Htbl,$nlo)")			if ($i==0);
 
 	    &mov	(&LB($nlo),&LB($dat));
 	    &xor	($Zlo,$tmp)				if ($i>0);
 	    &movzw	($rem[1],"($rem_8bit,$rem[1],2)")	if ($i>0);
 
 	    &movz	($nhi[1],&LB($dat));
 	    &shl	(&LB($nlo),4);
 	    &movzb	($rem[0],"(%rsp,$nhi[0])");
 
 	    &shr	($nhi[1],4)				if ($i<14);
 	    &and	($nhi[1],0xf0)				if ($i==14);
 	    &shl	($rem[1],48)				if ($i>0);
 	    &xor	($rem[0],$Zlo);
 
 	    &mov	($tmp,$Zhi);
 	    &xor	($Zhi,$rem[1])				if ($i>0);
 	    &shr	($Zlo,8);
 
 	    &movz	($rem[0],&LB($rem[0]));
 	    &mov	($dat,"$j($Xi)")			if (--$j%4==0);
 	    &shr	($Zhi,8);
 
 	    &xor	($Zlo,"-128($Hshr4,$nhi[0],8)");
 	    &shl	($tmp,56);
 	    &xor	($Zhi,"($Hshr4,$nhi[0],8)");
 
 	    unshift	(@nhi,pop(@nhi));		# "rotate" registers
 	    unshift	(@rem,pop(@rem));
 	}
 	&movzw	($rem[1],"($rem_8bit,$rem[1],2)");
 	&xor	($Zlo,"8($Htbl,$nlo)");
 	&xor	($Zhi,"($Htbl,$nlo)");
 
 	&shl	($rem[1],48);
 	&xor	($Zlo,$tmp);
 
 	&xor	($Zhi,$rem[1]);
 	&movz	($rem[0],&LB($Zlo));
 	&shr	($Zlo,4);
 
 	&mov	($tmp,$Zhi);
 	&shl	(&LB($rem[0]),4);
 	&shr	($Zhi,4);
 
 	&xor	($Zlo,"8($Htbl,$nhi[0])");
 	&movzw	($rem[0],"($rem_8bit,$rem[0],2)");
 	&shl	($tmp,60);
 
 	&xor	($Zhi,"($Htbl,$nhi[0])");
 	&xor	($Zlo,$tmp);
 	&shl	($rem[0],48);
 
 	&bswap	($Zlo);
 	&xor	($Zhi,$rem[0]);
 
 	&bswap	($Zhi);
 	&cmp	($inp,$len);
 	&jb	(".Louter_loop");
 }
 $code.=<<___;
 	mov	$Zlo,8($Xi)
 	mov	$Zhi,($Xi)
 
 	lea	280(%rsp),%rsi
 	mov	0(%rsi),%r15
 	mov	8(%rsi),%r14
 	mov	16(%rsi),%r13
 	mov	24(%rsi),%r12
 	mov	32(%rsi),%rbp
 	mov	40(%rsi),%rbx
 	lea	48(%rsi),%rsp
 .Lghash_epilogue:
 	ret
 .size	gcm_ghash_4bit,.-gcm_ghash_4bit
 ___
 
 ######################################################################
 # PCLMULQDQ version.
 
 @_4args=$win64?	("%rcx","%rdx","%r8", "%r9") :	# Win64 order
 		("%rdi","%rsi","%rdx","%rcx");	# Unix order
 
 ($Xi,$Xhi)=("%xmm0","%xmm1");	$Hkey="%xmm2";
 ($T1,$T2,$T3)=("%xmm3","%xmm4","%xmm5");
 
 sub clmul64x64_T2 {	# minimal register pressure
 my ($Xhi,$Xi,$Hkey,$HK)=@_;
 
 if (!defined($HK)) {	$HK = $T2;
 $code.=<<___;
 	movdqa		$Xi,$Xhi		#
 	pshufd		\$0b01001110,$Xi,$T1
 	pshufd		\$0b01001110,$Hkey,$T2
 	pxor		$Xi,$T1			#
 	pxor		$Hkey,$T2
 ___
 } else {
 $code.=<<___;
 	movdqa		$Xi,$Xhi		#
 	pshufd		\$0b01001110,$Xi,$T1
 	pxor		$Xi,$T1			#
 ___
 }
 $code.=<<___;
 	pclmulqdq	\$0x00,$Hkey,$Xi	#######
 	pclmulqdq	\$0x11,$Hkey,$Xhi	#######
 	pclmulqdq	\$0x00,$HK,$T1		#######
 	pxor		$Xi,$T1			#
 	pxor		$Xhi,$T1		#
 
 	movdqa		$T1,$T2			#
 	psrldq		\$8,$T1
 	pslldq		\$8,$T2			#
 	pxor		$T1,$Xhi
 	pxor		$T2,$Xi			#
 ___
 }
 
 sub reduction_alg9 {	# 17/11 times faster than Intel version
 my ($Xhi,$Xi) = @_;
 
 $code.=<<___;
 	# 1st phase
 	movdqa		$Xi,$T2			#
 	movdqa		$Xi,$T1
 	psllq		\$5,$Xi
 	pxor		$Xi,$T1			#
 	psllq		\$1,$Xi
 	pxor		$T1,$Xi			#
 	psllq		\$57,$Xi		#
 	movdqa		$Xi,$T1			#
 	pslldq		\$8,$Xi
 	psrldq		\$8,$T1			#	
 	pxor		$T2,$Xi
 	pxor		$T1,$Xhi		#
 
 	# 2nd phase
 	movdqa		$Xi,$T2
 	psrlq		\$1,$Xi
 	pxor		$T2,$Xhi		#
 	pxor		$Xi,$T2
 	psrlq		\$5,$Xi
 	pxor		$T2,$Xi			#
 	psrlq		\$1,$Xi			#
 	pxor		$Xhi,$Xi		#
 ___
 }
 
 { my ($Htbl,$Xip)=@_4args;
   my $HK="%xmm6";
 
 $code.=<<___;
 .globl	gcm_init_clmul
 .type	gcm_init_clmul,\@abi-omnipotent
 .align	16
 gcm_init_clmul:
 .L_init_clmul:
 ___
 $code.=<<___ if ($win64);
 .LSEH_begin_gcm_init_clmul:
 	# I can't trust assembler to use specific encoding:-(
 	.byte	0x48,0x83,0xec,0x18		#sub	$0x18,%rsp
 	.byte	0x0f,0x29,0x34,0x24		#movaps	%xmm6,(%rsp)
 ___
 $code.=<<___;
 	movdqu		($Xip),$Hkey
 	pshufd		\$0b01001110,$Hkey,$Hkey	# dword swap
 
 	# <<1 twist
 	pshufd		\$0b11111111,$Hkey,$T2	# broadcast uppermost dword
 	movdqa		$Hkey,$T1
 	psllq		\$1,$Hkey
 	pxor		$T3,$T3			#
 	psrlq		\$63,$T1
 	pcmpgtd		$T2,$T3			# broadcast carry bit
 	pslldq		\$8,$T1
 	por		$T1,$Hkey		# H<<=1
 
 	# magic reduction
 	pand		.L0x1c2_polynomial(%rip),$T3
 	pxor		$T3,$Hkey		# if(carry) H^=0x1c2_polynomial
 
 	# calculate H^2
 	pshufd		\$0b01001110,$Hkey,$HK
 	movdqa		$Hkey,$Xi
 	pxor		$Hkey,$HK
 ___
 	&clmul64x64_T2	($Xhi,$Xi,$Hkey,$HK);
 	&reduction_alg9	($Xhi,$Xi);
 $code.=<<___;
 	pshufd		\$0b01001110,$Hkey,$T1
 	pshufd		\$0b01001110,$Xi,$T2
 	pxor		$Hkey,$T1		# Karatsuba pre-processing
 	movdqu		$Hkey,0x00($Htbl)	# save H
 	pxor		$Xi,$T2			# Karatsuba pre-processing
 	movdqu		$Xi,0x10($Htbl)		# save H^2
 	palignr		\$8,$T1,$T2		# low part is H.lo^H.hi...
 	movdqu		$T2,0x20($Htbl)		# save Karatsuba "salt"
 ___
 if ($do4xaggr) {
 	&clmul64x64_T2	($Xhi,$Xi,$Hkey,$HK);	# H^3
 	&reduction_alg9	($Xhi,$Xi);
 $code.=<<___;
 	movdqa		$Xi,$T3
 ___
 	&clmul64x64_T2	($Xhi,$Xi,$Hkey,$HK);	# H^4
 	&reduction_alg9	($Xhi,$Xi);
 $code.=<<___;
 	pshufd		\$0b01001110,$T3,$T1
 	pshufd		\$0b01001110,$Xi,$T2
 	pxor		$T3,$T1			# Karatsuba pre-processing
 	movdqu		$T3,0x30($Htbl)		# save H^3
 	pxor		$Xi,$T2			# Karatsuba pre-processing
 	movdqu		$Xi,0x40($Htbl)		# save H^4
 	palignr		\$8,$T1,$T2		# low part is H^3.lo^H^3.hi...
 	movdqu		$T2,0x50($Htbl)		# save Karatsuba "salt"
 ___
 }
 $code.=<<___ if ($win64);
 	movaps	(%rsp),%xmm6
 	lea	0x18(%rsp),%rsp
 .LSEH_end_gcm_init_clmul:
 ___
 $code.=<<___;
 	ret
 .size	gcm_init_clmul,.-gcm_init_clmul
 ___
 }
 
 { my ($Xip,$Htbl)=@_4args;
 
 $code.=<<___;
 .globl	gcm_gmult_clmul
 .type	gcm_gmult_clmul,\@abi-omnipotent
 .align	16
 gcm_gmult_clmul:
 .L_gmult_clmul:
 	movdqu		($Xip),$Xi
 	movdqa		.Lbswap_mask(%rip),$T3
 	movdqu		($Htbl),$Hkey
 	movdqu		0x20($Htbl),$T2
 	pshufb		$T3,$Xi
 ___
 	&clmul64x64_T2	($Xhi,$Xi,$Hkey,$T2);
 $code.=<<___ if (0 || (&reduction_alg9($Xhi,$Xi)&&0));
 	# experimental alternative. special thing about is that there
 	# no dependency between the two multiplications... 
 	mov		\$`0xE1<<1`,%eax
 	mov		\$0xA040608020C0E000,%r10	# ((7..0)·0xE0)&0xff
 	mov		\$0x07,%r11d
 	movq		%rax,$T1
 	movq		%r10,$T2
 	movq		%r11,$T3		# borrow $T3
 	pand		$Xi,$T3
 	pshufb		$T3,$T2			# ($Xi&7)·0xE0
 	movq		%rax,$T3
 	pclmulqdq	\$0x00,$Xi,$T1		# ·(0xE1<<1)
 	pxor		$Xi,$T2
 	pslldq		\$15,$T2
 	paddd		$T2,$T2			# <<(64+56+1)
 	pxor		$T2,$Xi
 	pclmulqdq	\$0x01,$T3,$Xi
 	movdqa		.Lbswap_mask(%rip),$T3	# reload $T3
 	psrldq		\$1,$T1
 	pxor		$T1,$Xhi
 	pslldq		\$7,$Xi
 	pxor		$Xhi,$Xi
 ___
 $code.=<<___;
 	pshufb		$T3,$Xi
 	movdqu		$Xi,($Xip)
 	ret
 .size	gcm_gmult_clmul,.-gcm_gmult_clmul
 ___
 }
 
 { my ($Xip,$Htbl,$inp,$len)=@_4args;
   my ($Xln,$Xmn,$Xhn,$Hkey2,$HK) = map("%xmm$_",(3..7));
   my ($T1,$T2,$T3)=map("%xmm$_",(8..10));
 
 $code.=<<___;
 .globl	gcm_ghash_clmul
 .type	gcm_ghash_clmul,\@abi-omnipotent
 .align	32
 gcm_ghash_clmul:
 .L_ghash_clmul:
 ___
 $code.=<<___ if ($win64);
 	lea	-0x88(%rsp),%rax
 .LSEH_begin_gcm_ghash_clmul:
 	# I can't trust assembler to use specific encoding:-(
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax),%rsp
 	.byte	0x0f,0x29,0x70,0xe0		#movaps	%xmm6,-0x20(%rax)
 	.byte	0x0f,0x29,0x78,0xf0		#movaps	%xmm7,-0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x00		#movaps	%xmm8,0(%rax)
 	.byte	0x44,0x0f,0x29,0x48,0x10	#movaps	%xmm9,0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x50,0x20	#movaps	%xmm10,0x20(%rax)
 	.byte	0x44,0x0f,0x29,0x58,0x30	#movaps	%xmm11,0x30(%rax)
 	.byte	0x44,0x0f,0x29,0x60,0x40	#movaps	%xmm12,0x40(%rax)
 	.byte	0x44,0x0f,0x29,0x68,0x50	#movaps	%xmm13,0x50(%rax)
 	.byte	0x44,0x0f,0x29,0x70,0x60	#movaps	%xmm14,0x60(%rax)
 	.byte	0x44,0x0f,0x29,0x78,0x70	#movaps	%xmm15,0x70(%rax)
 ___
 $code.=<<___;
 	movdqa		.Lbswap_mask(%rip),$T3
 
 	movdqu		($Xip),$Xi
 	movdqu		($Htbl),$Hkey
 	movdqu		0x20($Htbl),$HK
 	pshufb		$T3,$Xi
 
 	sub		\$0x10,$len
 	jz		.Lodd_tail
 
 	movdqu		0x10($Htbl),$Hkey2
 ___
 if ($do4xaggr) {
 my ($Xl,$Xm,$Xh,$Hkey3,$Hkey4)=map("%xmm$_",(11..15));
 
 $code.=<<___;
 	mov		OPENSSL_ia32cap_P+4(%rip),%eax
 	cmp		\$0x30,$len
 	jb		.Lskip4x
 
 	and		\$`1<<26|1<<22`,%eax	# isolate MOVBE+XSAVE
 	cmp		\$`1<<22`,%eax		# check for MOVBE without XSAVE
 	je		.Lskip4x
 
 	sub		\$0x30,$len
 	mov		\$0xA040608020C0E000,%rax	# ((7..0)·0xE0)&0xff
 	movdqu		0x30($Htbl),$Hkey3
 	movdqu		0x40($Htbl),$Hkey4
 
 	#######
 	# Xi+4 =[(H*Ii+3) + (H^2*Ii+2) + (H^3*Ii+1) + H^4*(Ii+Xi)] mod P
 	#
 	movdqu		0x30($inp),$Xln
 	 movdqu		0x20($inp),$Xl
 	pshufb		$T3,$Xln
 	 pshufb		$T3,$Xl
 	movdqa		$Xln,$Xhn
 	pshufd		\$0b01001110,$Xln,$Xmn
 	pxor		$Xln,$Xmn
 	pclmulqdq	\$0x00,$Hkey,$Xln
 	pclmulqdq	\$0x11,$Hkey,$Xhn
 	pclmulqdq	\$0x00,$HK,$Xmn
 
 	movdqa		$Xl,$Xh
 	pshufd		\$0b01001110,$Xl,$Xm
 	pxor		$Xl,$Xm
 	pclmulqdq	\$0x00,$Hkey2,$Xl
 	pclmulqdq	\$0x11,$Hkey2,$Xh
 	pclmulqdq	\$0x10,$HK,$Xm
 	xorps		$Xl,$Xln
 	xorps		$Xh,$Xhn
 	movups		0x50($Htbl),$HK
 	xorps		$Xm,$Xmn
 
 	movdqu		0x10($inp),$Xl
 	 movdqu		0($inp),$T1
 	pshufb		$T3,$Xl
 	 pshufb		$T3,$T1
 	movdqa		$Xl,$Xh
 	pshufd		\$0b01001110,$Xl,$Xm
 	 pxor		$T1,$Xi
 	pxor		$Xl,$Xm
 	pclmulqdq	\$0x00,$Hkey3,$Xl
 	 movdqa		$Xi,$Xhi
 	 pshufd		\$0b01001110,$Xi,$T1
 	 pxor		$Xi,$T1
 	pclmulqdq	\$0x11,$Hkey3,$Xh
 	pclmulqdq	\$0x00,$HK,$Xm
 	xorps		$Xl,$Xln
 	xorps		$Xh,$Xhn
 
 	lea	0x40($inp),$inp
 	sub	\$0x40,$len
 	jc	.Ltail4x
 
 	jmp	.Lmod4_loop
 .align	32
 .Lmod4_loop:
 	pclmulqdq	\$0x00,$Hkey4,$Xi
 	xorps		$Xm,$Xmn
 	 movdqu		0x30($inp),$Xl
 	 pshufb		$T3,$Xl
 	pclmulqdq	\$0x11,$Hkey4,$Xhi
 	xorps		$Xln,$Xi
 	 movdqu		0x20($inp),$Xln
 	 movdqa		$Xl,$Xh
 	pclmulqdq	\$0x10,$HK,$T1
 	 pshufd		\$0b01001110,$Xl,$Xm
 	xorps		$Xhn,$Xhi
 	 pxor		$Xl,$Xm
 	 pshufb		$T3,$Xln
 	movups		0x20($Htbl),$HK
 	xorps		$Xmn,$T1
 	 pclmulqdq	\$0x00,$Hkey,$Xl
 	 pshufd		\$0b01001110,$Xln,$Xmn
 
 	pxor		$Xi,$T1			# aggregated Karatsuba post-processing
 	 movdqa		$Xln,$Xhn
 	pxor		$Xhi,$T1		#
 	 pxor		$Xln,$Xmn
 	movdqa		$T1,$T2			#
 	 pclmulqdq	\$0x11,$Hkey,$Xh
 	pslldq		\$8,$T1
 	psrldq		\$8,$T2			#
 	pxor		$T1,$Xi
 	movdqa		.L7_mask(%rip),$T1
 	pxor		$T2,$Xhi		#
 	movq		%rax,$T2
 
 	pand		$Xi,$T1			# 1st phase
 	pshufb		$T1,$T2			#
 	pxor		$Xi,$T2			#
 	 pclmulqdq	\$0x00,$HK,$Xm
 	psllq		\$57,$T2		#
 	movdqa		$T2,$T1			#
 	pslldq		\$8,$T2
 	 pclmulqdq	\$0x00,$Hkey2,$Xln
 	psrldq		\$8,$T1			#	
 	pxor		$T2,$Xi
 	pxor		$T1,$Xhi		#
 	movdqu		0($inp),$T1
 
 	movdqa		$Xi,$T2			# 2nd phase
 	psrlq		\$1,$Xi
 	 pclmulqdq	\$0x11,$Hkey2,$Xhn
 	 xorps		$Xl,$Xln
 	 movdqu		0x10($inp),$Xl
 	 pshufb		$T3,$Xl
 	 pclmulqdq	\$0x10,$HK,$Xmn
 	 xorps		$Xh,$Xhn
 	 movups		0x50($Htbl),$HK
 	pshufb		$T3,$T1
 	pxor		$T2,$Xhi		#
 	pxor		$Xi,$T2
 	psrlq		\$5,$Xi
 
 	 movdqa		$Xl,$Xh
 	 pxor		$Xm,$Xmn
 	 pshufd		\$0b01001110,$Xl,$Xm
 	pxor		$T2,$Xi			#
 	pxor		$T1,$Xhi
 	 pxor		$Xl,$Xm
 	 pclmulqdq	\$0x00,$Hkey3,$Xl
 	psrlq		\$1,$Xi			#
 	pxor		$Xhi,$Xi		#
 	movdqa		$Xi,$Xhi
 	 pclmulqdq	\$0x11,$Hkey3,$Xh
 	 xorps		$Xl,$Xln
 	pshufd		\$0b01001110,$Xi,$T1
 	pxor		$Xi,$T1
 
 	 pclmulqdq	\$0x00,$HK,$Xm
 	 xorps		$Xh,$Xhn
 
 	lea	0x40($inp),$inp
 	sub	\$0x40,$len
 	jnc	.Lmod4_loop
 
 .Ltail4x:
 	pclmulqdq	\$0x00,$Hkey4,$Xi
 	pclmulqdq	\$0x11,$Hkey4,$Xhi
 	pclmulqdq	\$0x10,$HK,$T1
 	xorps		$Xm,$Xmn
 	xorps		$Xln,$Xi
 	xorps		$Xhn,$Xhi
 	pxor		$Xi,$Xhi		# aggregated Karatsuba post-processing
 	pxor		$Xmn,$T1
 
 	pxor		$Xhi,$T1		#
 	pxor		$Xi,$Xhi
 
 	movdqa		$T1,$T2			#
 	psrldq		\$8,$T1
 	pslldq		\$8,$T2			#
 	pxor		$T1,$Xhi
 	pxor		$T2,$Xi			#
 ___
 	&reduction_alg9($Xhi,$Xi);
 $code.=<<___;
 	add	\$0x40,$len
 	jz	.Ldone
 	movdqu	0x20($Htbl),$HK
 	sub	\$0x10,$len
 	jz	.Lodd_tail
 .Lskip4x:
 ___
 }
 $code.=<<___;
 	#######
 	# Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
 	#	[(H*Ii+1) + (H*Xi+1)] mod P =
 	#	[(H*Ii+1) + H^2*(Ii+Xi)] mod P
 	#
 	movdqu		($inp),$T1		# Ii
 	movdqu		16($inp),$Xln		# Ii+1
 	pshufb		$T3,$T1
 	pshufb		$T3,$Xln
 	pxor		$T1,$Xi			# Ii+Xi
 
 	movdqa		$Xln,$Xhn
 	pshufd		\$0b01001110,$Xln,$Xmn
 	pxor		$Xln,$Xmn
 	pclmulqdq	\$0x00,$Hkey,$Xln
 	pclmulqdq	\$0x11,$Hkey,$Xhn
 	pclmulqdq	\$0x00,$HK,$Xmn
 
 	lea		32($inp),$inp		# i+=2
 	nop
 	sub		\$0x20,$len
 	jbe		.Leven_tail
 	nop
 	jmp		.Lmod_loop
 
 .align	32
 .Lmod_loop:
 	movdqa		$Xi,$Xhi
 	movdqa		$Xmn,$T1
 	pshufd		\$0b01001110,$Xi,$Xmn	#
 	pxor		$Xi,$Xmn		#
 
 	pclmulqdq	\$0x00,$Hkey2,$Xi
 	pclmulqdq	\$0x11,$Hkey2,$Xhi
 	pclmulqdq	\$0x10,$HK,$Xmn
 
 	pxor		$Xln,$Xi		# (H*Ii+1) + H^2*(Ii+Xi)
 	pxor		$Xhn,$Xhi
 	  movdqu	($inp),$T2		# Ii
 	pxor		$Xi,$T1			# aggregated Karatsuba post-processing
 	  pshufb	$T3,$T2
 	  movdqu	16($inp),$Xln		# Ii+1
 
 	pxor		$Xhi,$T1
 	  pxor		$T2,$Xhi		# "Ii+Xi", consume early
 	pxor		$T1,$Xmn
 	 pshufb		$T3,$Xln
 	movdqa		$Xmn,$T1		#
 	psrldq		\$8,$T1
 	pslldq		\$8,$Xmn		#
 	pxor		$T1,$Xhi
 	pxor		$Xmn,$Xi		#
 
 	movdqa		$Xln,$Xhn		#
 
 	  movdqa	$Xi,$T2			# 1st phase
 	  movdqa	$Xi,$T1
 	  psllq		\$5,$Xi
 	  pxor		$Xi,$T1			#
 	pclmulqdq	\$0x00,$Hkey,$Xln	#######
 	  psllq		\$1,$Xi
 	  pxor		$T1,$Xi			#
 	  psllq		\$57,$Xi		#
 	  movdqa	$Xi,$T1			#
 	  pslldq	\$8,$Xi
 	  psrldq	\$8,$T1			#	
 	  pxor		$T2,$Xi
 	pshufd		\$0b01001110,$Xhn,$Xmn
 	  pxor		$T1,$Xhi		#
 	pxor		$Xhn,$Xmn		#
 
 	  movdqa	$Xi,$T2			# 2nd phase
 	  psrlq		\$1,$Xi
 	pclmulqdq	\$0x11,$Hkey,$Xhn	#######
 	  pxor		$T2,$Xhi		#
 	  pxor		$Xi,$T2
 	  psrlq		\$5,$Xi
 	  pxor		$T2,$Xi			#
 	lea		32($inp),$inp
 	  psrlq		\$1,$Xi			#
 	pclmulqdq	\$0x00,$HK,$Xmn		#######
 	  pxor		$Xhi,$Xi		#
 
 	sub		\$0x20,$len
 	ja		.Lmod_loop
 
 .Leven_tail:
 	 movdqa		$Xi,$Xhi
 	 movdqa		$Xmn,$T1
 	 pshufd		\$0b01001110,$Xi,$Xmn	#
 	 pxor		$Xi,$Xmn		#
 
 	pclmulqdq	\$0x00,$Hkey2,$Xi
 	pclmulqdq	\$0x11,$Hkey2,$Xhi
 	pclmulqdq	\$0x10,$HK,$Xmn
 
 	pxor		$Xln,$Xi		# (H*Ii+1) + H^2*(Ii+Xi)
 	pxor		$Xhn,$Xhi
 	pxor		$Xi,$T1
 	pxor		$Xhi,$T1
 	pxor		$T1,$Xmn
 	movdqa		$Xmn,$T1		#
 	psrldq		\$8,$T1
 	pslldq		\$8,$Xmn		#
 	pxor		$T1,$Xhi
 	pxor		$Xmn,$Xi		#
 ___
 	&reduction_alg9	($Xhi,$Xi);
 $code.=<<___;
 	test		$len,$len
 	jnz		.Ldone
 
 .Lodd_tail:
 	movdqu		($inp),$T1		# Ii
 	pshufb		$T3,$T1
 	pxor		$T1,$Xi			# Ii+Xi
 ___
 	&clmul64x64_T2	($Xhi,$Xi,$Hkey,$HK);	# H*(Ii+Xi)
 	&reduction_alg9	($Xhi,$Xi);
 $code.=<<___;
 .Ldone:
 	pshufb		$T3,$Xi
 	movdqu		$Xi,($Xip)
 ___
 $code.=<<___ if ($win64);
 	movaps	(%rsp),%xmm6
 	movaps	0x10(%rsp),%xmm7
 	movaps	0x20(%rsp),%xmm8
 	movaps	0x30(%rsp),%xmm9
 	movaps	0x40(%rsp),%xmm10
 	movaps	0x50(%rsp),%xmm11
 	movaps	0x60(%rsp),%xmm12
 	movaps	0x70(%rsp),%xmm13
 	movaps	0x80(%rsp),%xmm14
 	movaps	0x90(%rsp),%xmm15
 	lea	0xa8(%rsp),%rsp
 .LSEH_end_gcm_ghash_clmul:
 ___
 $code.=<<___;
 	ret
 .size	gcm_ghash_clmul,.-gcm_ghash_clmul
 ___
 }
 
 $code.=<<___;
 .globl	gcm_init_avx
 .type	gcm_init_avx,\@abi-omnipotent
 .align	32
 gcm_init_avx:
 ___
 if ($avx) {
 my ($Htbl,$Xip)=@_4args;
 my $HK="%xmm6";
 
 $code.=<<___ if ($win64);
 .LSEH_begin_gcm_init_avx:
 	# I can't trust assembler to use specific encoding:-(
 	.byte	0x48,0x83,0xec,0x18		#sub	$0x18,%rsp
 	.byte	0x0f,0x29,0x34,0x24		#movaps	%xmm6,(%rsp)
 ___
 $code.=<<___;
 	vzeroupper
 
 	vmovdqu		($Xip),$Hkey
 	vpshufd		\$0b01001110,$Hkey,$Hkey	# dword swap
 
 	# <<1 twist
 	vpshufd		\$0b11111111,$Hkey,$T2	# broadcast uppermost dword
 	vpsrlq		\$63,$Hkey,$T1
 	vpsllq		\$1,$Hkey,$Hkey
 	vpxor		$T3,$T3,$T3		#
 	vpcmpgtd	$T2,$T3,$T3		# broadcast carry bit
 	vpslldq		\$8,$T1,$T1
 	vpor		$T1,$Hkey,$Hkey		# H<<=1
 
 	# magic reduction
 	vpand		.L0x1c2_polynomial(%rip),$T3,$T3
 	vpxor		$T3,$Hkey,$Hkey		# if(carry) H^=0x1c2_polynomial
 
 	vpunpckhqdq	$Hkey,$Hkey,$HK
 	vmovdqa		$Hkey,$Xi
 	vpxor		$Hkey,$HK,$HK
 	mov		\$4,%r10		# up to H^8
 	jmp		.Linit_start_avx
 ___
 
 sub clmul64x64_avx {
 my ($Xhi,$Xi,$Hkey,$HK)=@_;
 
 if (!defined($HK)) {	$HK = $T2;
 $code.=<<___;
 	vpunpckhqdq	$Xi,$Xi,$T1
 	vpunpckhqdq	$Hkey,$Hkey,$T2
 	vpxor		$Xi,$T1,$T1		#
 	vpxor		$Hkey,$T2,$T2
 ___
 } else {
 $code.=<<___;
 	vpunpckhqdq	$Xi,$Xi,$T1
 	vpxor		$Xi,$T1,$T1		#
 ___
 }
 $code.=<<___;
 	vpclmulqdq	\$0x11,$Hkey,$Xi,$Xhi	#######
 	vpclmulqdq	\$0x00,$Hkey,$Xi,$Xi	#######
 	vpclmulqdq	\$0x00,$HK,$T1,$T1	#######
 	vpxor		$Xi,$Xhi,$T2		#
 	vpxor		$T2,$T1,$T1		#
 
 	vpslldq		\$8,$T1,$T2		#
 	vpsrldq		\$8,$T1,$T1
 	vpxor		$T2,$Xi,$Xi		#
 	vpxor		$T1,$Xhi,$Xhi
 ___
 }
 
 sub reduction_avx {
 my ($Xhi,$Xi) = @_;
 
 $code.=<<___;
 	vpsllq		\$57,$Xi,$T1		# 1st phase
 	vpsllq		\$62,$Xi,$T2
 	vpxor		$T1,$T2,$T2		#
 	vpsllq		\$63,$Xi,$T1
 	vpxor		$T1,$T2,$T2		#
 	vpslldq		\$8,$T2,$T1		#
 	vpsrldq		\$8,$T2,$T2
 	vpxor		$T1,$Xi,$Xi		#
 	vpxor		$T2,$Xhi,$Xhi
 
 	vpsrlq		\$1,$Xi,$T2		# 2nd phase
 	vpxor		$Xi,$Xhi,$Xhi
 	vpxor		$T2,$Xi,$Xi		#
 	vpsrlq		\$5,$T2,$T2
 	vpxor		$T2,$Xi,$Xi		#
 	vpsrlq		\$1,$Xi,$Xi		#
 	vpxor		$Xhi,$Xi,$Xi		#
 ___
 }
 
 $code.=<<___;
 .align	32
 .Linit_loop_avx:
 	vpalignr	\$8,$T1,$T2,$T3		# low part is H.lo^H.hi...
 	vmovdqu		$T3,-0x10($Htbl)	# save Karatsuba "salt"
 ___
 	&clmul64x64_avx	($Xhi,$Xi,$Hkey,$HK);	# calculate H^3,5,7
 	&reduction_avx	($Xhi,$Xi);
 $code.=<<___;
 .Linit_start_avx:
 	vmovdqa		$Xi,$T3
 ___
 	&clmul64x64_avx	($Xhi,$Xi,$Hkey,$HK);	# calculate H^2,4,6,8
 	&reduction_avx	($Xhi,$Xi);
 $code.=<<___;
 	vpshufd		\$0b01001110,$T3,$T1
 	vpshufd		\$0b01001110,$Xi,$T2
 	vpxor		$T3,$T1,$T1		# Karatsuba pre-processing
 	vmovdqu		$T3,0x00($Htbl)		# save H^1,3,5,7
 	vpxor		$Xi,$T2,$T2		# Karatsuba pre-processing
 	vmovdqu		$Xi,0x10($Htbl)		# save H^2,4,6,8
 	lea		0x30($Htbl),$Htbl
 	sub		\$1,%r10
 	jnz		.Linit_loop_avx
 
 	vpalignr	\$8,$T2,$T1,$T3		# last "salt" is flipped
 	vmovdqu		$T3,-0x10($Htbl)
 
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
 	movaps	(%rsp),%xmm6
 	lea	0x18(%rsp),%rsp
 .LSEH_end_gcm_init_avx:
 ___
 $code.=<<___;
 	ret
 .size	gcm_init_avx,.-gcm_init_avx
 ___
 } else {
 $code.=<<___;
 	jmp	.L_init_clmul
 .size	gcm_init_avx,.-gcm_init_avx
 ___
 }
 
 $code.=<<___;
 .globl	gcm_gmult_avx
 .type	gcm_gmult_avx,\@abi-omnipotent
 .align	32
 gcm_gmult_avx:
 	jmp	.L_gmult_clmul
 .size	gcm_gmult_avx,.-gcm_gmult_avx
 ___
 
 $code.=<<___;
 .globl	gcm_ghash_avx
 .type	gcm_ghash_avx,\@abi-omnipotent
 .align	32
 gcm_ghash_avx:
 ___
 if ($avx) {
 my ($Xip,$Htbl,$inp,$len)=@_4args;
 my ($Xlo,$Xhi,$Xmi,
     $Zlo,$Zhi,$Zmi,
     $Hkey,$HK,$T1,$T2,
     $Xi,$Xo,$Tred,$bswap,$Ii,$Ij) = map("%xmm$_",(0..15));
 
 $code.=<<___ if ($win64);
 	lea	-0x88(%rsp),%rax
 .LSEH_begin_gcm_ghash_avx:
 	# I can't trust assembler to use specific encoding:-(
 	.byte	0x48,0x8d,0x60,0xe0		#lea	-0x20(%rax),%rsp
 	.byte	0x0f,0x29,0x70,0xe0		#movaps	%xmm6,-0x20(%rax)
 	.byte	0x0f,0x29,0x78,0xf0		#movaps	%xmm7,-0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x00		#movaps	%xmm8,0(%rax)
 	.byte	0x44,0x0f,0x29,0x48,0x10	#movaps	%xmm9,0x10(%rax)
 	.byte	0x44,0x0f,0x29,0x50,0x20	#movaps	%xmm10,0x20(%rax)
 	.byte	0x44,0x0f,0x29,0x58,0x30	#movaps	%xmm11,0x30(%rax)
 	.byte	0x44,0x0f,0x29,0x60,0x40	#movaps	%xmm12,0x40(%rax)
 	.byte	0x44,0x0f,0x29,0x68,0x50	#movaps	%xmm13,0x50(%rax)
 	.byte	0x44,0x0f,0x29,0x70,0x60	#movaps	%xmm14,0x60(%rax)
 	.byte	0x44,0x0f,0x29,0x78,0x70	#movaps	%xmm15,0x70(%rax)
 ___
 $code.=<<___;
 	vzeroupper
 
 	vmovdqu		($Xip),$Xi		# load $Xi
 	lea		.L0x1c2_polynomial(%rip),%r10
 	lea		0x40($Htbl),$Htbl	# size optimization
 	vmovdqu		.Lbswap_mask(%rip),$bswap
 	vpshufb		$bswap,$Xi,$Xi
 	cmp		\$0x80,$len
 	jb		.Lshort_avx
 	sub		\$0x80,$len
 
 	vmovdqu		0x70($inp),$Ii		# I[7]
 	vmovdqu		0x00-0x40($Htbl),$Hkey	# $Hkey^1
 	vpshufb		$bswap,$Ii,$Ii
 	vmovdqu		0x20-0x40($Htbl),$HK
 
 	vpunpckhqdq	$Ii,$Ii,$T2
 	 vmovdqu	0x60($inp),$Ij		# I[6]
 	vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	vpxor		$Ii,$T2,$T2
 	 vpshufb	$bswap,$Ij,$Ij
 	vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	 vmovdqu	0x10-0x40($Htbl),$Hkey	# $Hkey^2
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	 vmovdqu	0x50($inp),$Ii		# I[5]
 	vpclmulqdq	\$0x00,$HK,$T2,$Xmi
 	 vpxor		$Ij,$T1,$T1
 
 	 vpshufb	$bswap,$Ii,$Ii
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	 vmovdqu	0x30-0x40($Htbl),$Hkey	# $Hkey^3
 	 vpxor		$Ii,$T2,$T2
 	 vmovdqu	0x40($inp),$Ij		# I[4]
 	vpclmulqdq	\$0x10,$HK,$T1,$Zmi
 	 vmovdqu	0x50-0x40($Htbl),$HK
 
 	 vpshufb	$bswap,$Ij,$Ij
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	vpxor		$Xhi,$Zhi,$Zhi
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	 vmovdqu	0x40-0x40($Htbl),$Hkey	# $Hkey^4
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T2,$Xmi
 	 vpxor		$Ij,$T1,$T1
 
 	 vmovdqu	0x30($inp),$Ii		# I[3]
 	vpxor		$Zlo,$Xlo,$Xlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	vpxor		$Zhi,$Xhi,$Xhi
 	 vpshufb	$bswap,$Ii,$Ii
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	 vmovdqu	0x60-0x40($Htbl),$Hkey	# $Hkey^5
 	vpxor		$Zmi,$Xmi,$Xmi
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	vpclmulqdq	\$0x10,$HK,$T1,$Zmi
 	 vmovdqu	0x80-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 
 	 vmovdqu	0x20($inp),$Ij		# I[2]
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	vpxor		$Xhi,$Zhi,$Zhi
 	 vpshufb	$bswap,$Ij,$Ij
 	vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	 vmovdqu	0x70-0x40($Htbl),$Hkey	# $Hkey^6
 	vpxor		$Xmi,$Zmi,$Zmi
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	vpclmulqdq	\$0x00,$HK,$T2,$Xmi
 	 vpxor		$Ij,$T1,$T1
 
 	 vmovdqu	0x10($inp),$Ii		# I[1]
 	vpxor		$Zlo,$Xlo,$Xlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	vpxor		$Zhi,$Xhi,$Xhi
 	 vpshufb	$bswap,$Ii,$Ii
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	 vmovdqu	0x90-0x40($Htbl),$Hkey	# $Hkey^7
 	vpxor		$Zmi,$Xmi,$Xmi
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	vpclmulqdq	\$0x10,$HK,$T1,$Zmi
 	 vmovdqu	0xb0-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 
 	 vmovdqu	($inp),$Ij		# I[0]
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	vpxor		$Xhi,$Zhi,$Zhi
 	 vpshufb	$bswap,$Ij,$Ij
 	vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	 vmovdqu	0xa0-0x40($Htbl),$Hkey	# $Hkey^8
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x10,$HK,$T2,$Xmi
 
 	lea		0x80($inp),$inp
 	cmp		\$0x80,$len
 	jb		.Ltail_avx
 
 	vpxor		$Xi,$Ij,$Ij		# accumulate $Xi
 	sub		\$0x80,$len
 	jmp		.Loop8x_avx
 
 .align	32
 .Loop8x_avx:
 	vpunpckhqdq	$Ij,$Ij,$T1
 	 vmovdqu	0x70($inp),$Ii		# I[7]
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpxor		$Ij,$T1,$T1
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xi
 	 vpshufb	$bswap,$Ii,$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xo
 	 vmovdqu	0x00-0x40($Htbl),$Hkey	# $Hkey^1
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Tred
 	 vmovdqu	0x20-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 
 	  vmovdqu	0x60($inp),$Ij		# I[6]
 	 vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	vpxor		$Zlo,$Xi,$Xi		# collect result
 	  vpshufb	$bswap,$Ij,$Ij
 	 vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	vxorps		$Zhi,$Xo,$Xo
 	  vmovdqu	0x10-0x40($Htbl),$Hkey	# $Hkey^2
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	 vpclmulqdq	\$0x00,$HK,  $T2,$Xmi
 	vpxor		$Zmi,$Tred,$Tred
 	 vxorps		$Ij,$T1,$T1
 
 	  vmovdqu	0x50($inp),$Ii		# I[5]
 	vpxor		$Xi,$Tred,$Tred		# aggregated Karatsuba post-processing
 	 vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	vpxor		$Xo,$Tred,$Tred
 	vpslldq		\$8,$Tred,$T2
 	 vpxor		$Xlo,$Zlo,$Zlo
 	 vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	vpsrldq		\$8,$Tred,$Tred
 	vpxor		$T2, $Xi, $Xi
 	  vmovdqu	0x30-0x40($Htbl),$Hkey	# $Hkey^3
 	  vpshufb	$bswap,$Ii,$Ii
 	vxorps		$Tred,$Xo, $Xo
 	 vpxor		$Xhi,$Zhi,$Zhi
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	 vpclmulqdq	\$0x10,$HK,  $T1,$Zmi
 	  vmovdqu	0x50-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 	 vpxor		$Xmi,$Zmi,$Zmi
 
 	  vmovdqu	0x40($inp),$Ij		# I[4]
 	vpalignr	\$8,$Xi,$Xi,$Tred	# 1st phase
 	 vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	  vpshufb	$bswap,$Ij,$Ij
 	 vpxor		$Zlo,$Xlo,$Xlo
 	 vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	  vmovdqu	0x40-0x40($Htbl),$Hkey	# $Hkey^4
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	 vpxor		$Zhi,$Xhi,$Xhi
 	 vpclmulqdq	\$0x00,$HK,  $T2,$Xmi
 	 vxorps		$Ij,$T1,$T1
 	 vpxor		$Zmi,$Xmi,$Xmi
 
 	  vmovdqu	0x30($inp),$Ii		# I[3]
 	vpclmulqdq	\$0x10,(%r10),$Xi,$Xi
 	 vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	  vpshufb	$bswap,$Ii,$Ii
 	 vpxor		$Xlo,$Zlo,$Zlo
 	 vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	  vmovdqu	0x60-0x40($Htbl),$Hkey	# $Hkey^5
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	 vpxor		$Xhi,$Zhi,$Zhi
 	 vpclmulqdq	\$0x10,$HK,  $T1,$Zmi
 	  vmovdqu	0x80-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 	 vpxor		$Xmi,$Zmi,$Zmi
 
 	  vmovdqu	0x20($inp),$Ij		# I[2]
 	 vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	  vpshufb	$bswap,$Ij,$Ij
 	 vpxor		$Zlo,$Xlo,$Xlo
 	 vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	  vmovdqu	0x70-0x40($Htbl),$Hkey	# $Hkey^6
 	 vpunpckhqdq	$Ij,$Ij,$T1
 	 vpxor		$Zhi,$Xhi,$Xhi
 	 vpclmulqdq	\$0x00,$HK,  $T2,$Xmi
 	 vpxor		$Ij,$T1,$T1
 	 vpxor		$Zmi,$Xmi,$Xmi
 	vxorps		$Tred,$Xi,$Xi
 
 	  vmovdqu	0x10($inp),$Ii		# I[1]
 	vpalignr	\$8,$Xi,$Xi,$Tred	# 2nd phase
 	 vpclmulqdq	\$0x00,$Hkey,$Ij,$Zlo
 	  vpshufb	$bswap,$Ii,$Ii
 	 vpxor		$Xlo,$Zlo,$Zlo
 	 vpclmulqdq	\$0x11,$Hkey,$Ij,$Zhi
 	  vmovdqu	0x90-0x40($Htbl),$Hkey	# $Hkey^7
 	vpclmulqdq	\$0x10,(%r10),$Xi,$Xi
 	vxorps		$Xo,$Tred,$Tred
 	 vpunpckhqdq	$Ii,$Ii,$T2
 	 vpxor		$Xhi,$Zhi,$Zhi
 	 vpclmulqdq	\$0x10,$HK,  $T1,$Zmi
 	  vmovdqu	0xb0-0x40($Htbl),$HK
 	 vpxor		$Ii,$T2,$T2
 	 vpxor		$Xmi,$Zmi,$Zmi
 
 	  vmovdqu	($inp),$Ij		# I[0]
 	 vpclmulqdq	\$0x00,$Hkey,$Ii,$Xlo
 	  vpshufb	$bswap,$Ij,$Ij
 	 vpclmulqdq	\$0x11,$Hkey,$Ii,$Xhi
 	  vmovdqu	0xa0-0x40($Htbl),$Hkey	# $Hkey^8
 	vpxor		$Tred,$Ij,$Ij
 	 vpclmulqdq	\$0x10,$HK,  $T2,$Xmi
 	vpxor		$Xi,$Ij,$Ij		# accumulate $Xi
 
 	lea		0x80($inp),$inp
 	sub		\$0x80,$len
 	jnc		.Loop8x_avx
 
 	add		\$0x80,$len
 	jmp		.Ltail_no_xor_avx
 
 .align	32
 .Lshort_avx:
 	vmovdqu		-0x10($inp,$len),$Ii	# very last word
 	lea		($inp,$len),$inp
 	vmovdqu		0x00-0x40($Htbl),$Hkey	# $Hkey^1
 	vmovdqu		0x20-0x40($Htbl),$HK
 	vpshufb		$bswap,$Ii,$Ij
 
 	vmovdqa		$Xlo,$Zlo		# subtle way to zero $Zlo,
 	vmovdqa		$Xhi,$Zhi		# $Zhi and
 	vmovdqa		$Xmi,$Zmi		# $Zmi
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x20($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x10-0x40($Htbl),$Hkey	# $Hkey^2
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vpsrldq		\$8,$HK,$HK
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x30($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x30-0x40($Htbl),$Hkey	# $Hkey^3
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vmovdqu		0x50-0x40($Htbl),$HK
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x40($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x40-0x40($Htbl),$Hkey	# $Hkey^4
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vpsrldq		\$8,$HK,$HK
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x50($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x60-0x40($Htbl),$Hkey	# $Hkey^5
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vmovdqu		0x80-0x40($Htbl),$HK
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x60($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x70-0x40($Htbl),$Hkey	# $Hkey^6
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vpsrldq		\$8,$HK,$HK
 	sub		\$0x10,$len
 	jz		.Ltail_avx
 
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	 vmovdqu	-0x70($inp),$Ii
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vmovdqu		0x90-0x40($Htbl),$Hkey	# $Hkey^7
 	 vpshufb	$bswap,$Ii,$Ij
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 	vmovq		0xb8-0x40($Htbl),$HK
 	sub		\$0x10,$len
 	jmp		.Ltail_avx
 
 .align	32
 .Ltail_avx:
 	vpxor		$Xi,$Ij,$Ij		# accumulate $Xi
 .Ltail_no_xor_avx:
 	vpunpckhqdq	$Ij,$Ij,$T1
 	vpxor		$Xlo,$Zlo,$Zlo
 	vpclmulqdq	\$0x00,$Hkey,$Ij,$Xlo
 	vpxor		$Ij,$T1,$T1
 	vpxor		$Xhi,$Zhi,$Zhi
 	vpclmulqdq	\$0x11,$Hkey,$Ij,$Xhi
 	vpxor		$Xmi,$Zmi,$Zmi
 	vpclmulqdq	\$0x00,$HK,$T1,$Xmi
 
 	vmovdqu		(%r10),$Tred
 
 	vpxor		$Xlo,$Zlo,$Xi
 	vpxor		$Xhi,$Zhi,$Xo
 	vpxor		$Xmi,$Zmi,$Zmi
 
 	vpxor		$Xi, $Zmi,$Zmi		# aggregated Karatsuba post-processing
 	vpxor		$Xo, $Zmi,$Zmi
 	vpslldq		\$8, $Zmi,$T2
 	vpsrldq		\$8, $Zmi,$Zmi
 	vpxor		$T2, $Xi, $Xi
 	vpxor		$Zmi,$Xo, $Xo
 
 	vpclmulqdq	\$0x10,$Tred,$Xi,$T2	# 1st phase
 	vpalignr	\$8,$Xi,$Xi,$Xi
 	vpxor		$T2,$Xi,$Xi
 
 	vpclmulqdq	\$0x10,$Tred,$Xi,$T2	# 2nd phase
 	vpalignr	\$8,$Xi,$Xi,$Xi
 	vpxor		$Xo,$Xi,$Xi
 	vpxor		$T2,$Xi,$Xi
 
 	cmp		\$0,$len
 	jne		.Lshort_avx
 
 	vpshufb		$bswap,$Xi,$Xi
 	vmovdqu		$Xi,($Xip)
 	vzeroupper
 ___
 $code.=<<___ if ($win64);
 	movaps	(%rsp),%xmm6
 	movaps	0x10(%rsp),%xmm7
 	movaps	0x20(%rsp),%xmm8
 	movaps	0x30(%rsp),%xmm9
 	movaps	0x40(%rsp),%xmm10
 	movaps	0x50(%rsp),%xmm11
 	movaps	0x60(%rsp),%xmm12
 	movaps	0x70(%rsp),%xmm13
 	movaps	0x80(%rsp),%xmm14
 	movaps	0x90(%rsp),%xmm15
 	lea	0xa8(%rsp),%rsp
 .LSEH_end_gcm_ghash_avx:
 ___
 $code.=<<___;
 	ret
 .size	gcm_ghash_avx,.-gcm_ghash_avx
 ___
 } else {
 $code.=<<___;
 	jmp	.L_ghash_clmul
 .size	gcm_ghash_avx,.-gcm_ghash_avx
 ___
 }
 
 $code.=<<___;
 .align	64
 .Lbswap_mask:
 	.byte	15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
 .L0x1c2_polynomial:
 	.byte	1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2
 .L7_mask:
 	.long	7,0,7,0
 .L7_mask_poly:
 	.long	7,0,`0xE1<<1`,0
 .align	64
 .type	.Lrem_4bit,\@object
 .Lrem_4bit:
 	.long	0,`0x0000<<16`,0,`0x1C20<<16`,0,`0x3840<<16`,0,`0x2460<<16`
 	.long	0,`0x7080<<16`,0,`0x6CA0<<16`,0,`0x48C0<<16`,0,`0x54E0<<16`
 	.long	0,`0xE100<<16`,0,`0xFD20<<16`,0,`0xD940<<16`,0,`0xC560<<16`
 	.long	0,`0x9180<<16`,0,`0x8DA0<<16`,0,`0xA9C0<<16`,0,`0xB5E0<<16`
 .type	.Lrem_8bit,\@object
 .Lrem_8bit:
 	.value	0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E
 	.value	0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E
 	.value	0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E
 	.value	0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E
 	.value	0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E
 	.value	0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E
 	.value	0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E
 	.value	0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E
 	.value	0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE
 	.value	0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE
 	.value	0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE
 	.value	0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE
 	.value	0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E
 	.value	0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E
 	.value	0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE
 	.value	0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE
 	.value	0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E
 	.value	0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E
 	.value	0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E
 	.value	0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E
 	.value	0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E
 	.value	0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E
 	.value	0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E
 	.value	0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E
 	.value	0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE
 	.value	0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE
 	.value	0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE
 	.value	0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE
 	.value	0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E
 	.value	0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E
 	.value	0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE
 	.value	0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE
 
 .asciz	"GHASH for x86_64, CRYPTOGAMS by "
 .align	64
 ___
 
 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
 #		CONTEXT *context,DISPATCHER_CONTEXT *disp)
 if ($win64) {
 $rec="%rcx";
 $frame="%rdx";
 $context="%r8";
 $disp="%r9";
 
 $code.=<<___;
 .extern	__imp_RtlVirtualUnwind
 .type	se_handler,\@abi-omnipotent
 .align	16
 se_handler:
 	push	%rsi
 	push	%rdi
 	push	%rbx
 	push	%rbp
 	push	%r12
 	push	%r13
 	push	%r14
 	push	%r15
 	pushfq
 	sub	\$64,%rsp
 
 	mov	120($context),%rax	# pull context->Rax
 	mov	248($context),%rbx	# pull context->Rip
 
 	mov	8($disp),%rsi		# disp->ImageBase
 	mov	56($disp),%r11		# disp->HandlerData
 
 	mov	0(%r11),%r10d		# HandlerData[0]
 	lea	(%rsi,%r10),%r10	# prologue label
 	cmp	%r10,%rbx		# context->RipRsp
 
 	mov	4(%r11),%r10d		# HandlerData[1]
 	lea	(%rsi,%r10),%r10	# epilogue label
 	cmp	%r10,%rbx		# context->Rip>=epilogue label
 	jae	.Lin_prologue
 
 	lea	24(%rax),%rax		# adjust "rsp"
 
 	mov	-8(%rax),%rbx
 	mov	-16(%rax),%rbp
 	mov	-24(%rax),%r12
 	mov	%rbx,144($context)	# restore context->Rbx
 	mov	%rbp,160($context)	# restore context->Rbp
 	mov	%r12,216($context)	# restore context->R12
 
 .Lin_prologue:
 	mov	8(%rax),%rdi
 	mov	16(%rax),%rsi
 	mov	%rax,152($context)	# restore context->Rsp
 	mov	%rsi,168($context)	# restore context->Rsi
 	mov	%rdi,176($context)	# restore context->Rdi
 
 	mov	40($disp),%rdi		# disp->ContextRecord
 	mov	$context,%rsi		# context
 	mov	\$`1232/8`,%ecx		# sizeof(CONTEXT)
 	.long	0xa548f3fc		# cld; rep movsq
 
 	mov	$disp,%rsi
 	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
 	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
 	mov	0(%rsi),%r8		# arg3, disp->ControlPc
 	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
 	mov	40(%rsi),%r10		# disp->ContextRecord
 	lea	56(%rsi),%r11		# &disp->HandlerData
 	lea	24(%rsi),%r12		# &disp->EstablisherFrame
 	mov	%r10,32(%rsp)		# arg5
 	mov	%r11,40(%rsp)		# arg6
 	mov	%r12,48(%rsp)		# arg7
 	mov	%rcx,56(%rsp)		# arg8, (NULL)
 	call	*__imp_RtlVirtualUnwind(%rip)
 
 	mov	\$1,%eax		# ExceptionContinueSearch
 	add	\$64,%rsp
 	popfq
 	pop	%r15
 	pop	%r14
 	pop	%r13
 	pop	%r12
 	pop	%rbp
 	pop	%rbx
 	pop	%rdi
 	pop	%rsi
 	ret
 .size	se_handler,.-se_handler
 
 .section	.pdata
 .align	4
 	.rva	.LSEH_begin_gcm_gmult_4bit
 	.rva	.LSEH_end_gcm_gmult_4bit
 	.rva	.LSEH_info_gcm_gmult_4bit
 
 	.rva	.LSEH_begin_gcm_ghash_4bit
 	.rva	.LSEH_end_gcm_ghash_4bit
 	.rva	.LSEH_info_gcm_ghash_4bit
 
 	.rva	.LSEH_begin_gcm_init_clmul
 	.rva	.LSEH_end_gcm_init_clmul
 	.rva	.LSEH_info_gcm_init_clmul
 
 	.rva	.LSEH_begin_gcm_ghash_clmul
 	.rva	.LSEH_end_gcm_ghash_clmul
 	.rva	.LSEH_info_gcm_ghash_clmul
 ___
 $code.=<<___	if ($avx);
 	.rva	.LSEH_begin_gcm_init_avx
 	.rva	.LSEH_end_gcm_init_avx
 	.rva	.LSEH_info_gcm_init_clmul
 
 	.rva	.LSEH_begin_gcm_ghash_avx
 	.rva	.LSEH_end_gcm_ghash_avx
 	.rva	.LSEH_info_gcm_ghash_clmul
 ___
 $code.=<<___;
 .section	.xdata
 .align	8
 .LSEH_info_gcm_gmult_4bit:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lgmult_prologue,.Lgmult_epilogue	# HandlerData
 .LSEH_info_gcm_ghash_4bit:
 	.byte	9,0,0,0
 	.rva	se_handler
 	.rva	.Lghash_prologue,.Lghash_epilogue	# HandlerData
 .LSEH_info_gcm_init_clmul:
 	.byte	0x01,0x08,0x03,0x00
 	.byte	0x08,0x68,0x00,0x00	#movaps	0x00(rsp),xmm6
 	.byte	0x04,0x22,0x00,0x00	#sub	rsp,0x18
 .LSEH_info_gcm_ghash_clmul:
 	.byte	0x01,0x33,0x16,0x00
 	.byte	0x33,0xf8,0x09,0x00	#movaps 0x90(rsp),xmm15
 	.byte	0x2e,0xe8,0x08,0x00	#movaps 0x80(rsp),xmm14
 	.byte	0x29,0xd8,0x07,0x00	#movaps 0x70(rsp),xmm13
 	.byte	0x24,0xc8,0x06,0x00	#movaps 0x60(rsp),xmm12
 	.byte	0x1f,0xb8,0x05,0x00	#movaps 0x50(rsp),xmm11
 	.byte	0x1a,0xa8,0x04,0x00	#movaps 0x40(rsp),xmm10
 	.byte	0x15,0x98,0x03,0x00	#movaps 0x30(rsp),xmm9
 	.byte	0x10,0x88,0x02,0x00	#movaps 0x20(rsp),xmm8
 	.byte	0x0c,0x78,0x01,0x00	#movaps 0x10(rsp),xmm7
 	.byte	0x08,0x68,0x00,0x00	#movaps 0x00(rsp),xmm6
 	.byte	0x04,0x01,0x15,0x00	#sub	rsp,0xa8
 ___
 }
 
 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
 
 print $code;
 
 close STDOUT;
Index: vendor-crypto/openssl/dist/crypto/modes/ctr128.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/modes/ctr128.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/modes/ctr128.c	(revision 296273)
@@ -1,270 +1,263 @@
 /* ====================================================================
  * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    openssl-core@openssl.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  */
 
 #include 
 #include "modes_lcl.h"
 #include 
 
 #ifndef MODES_DEBUG
 # ifndef NDEBUG
 #  define NDEBUG
 # endif
 #endif
 #include 
 
 /*
  * NOTE: the IV/counter CTR mode is big-endian.  The code itself is
  * endian-neutral.
  */
 
 /* increment counter (128-bit int) by 1 */
 static void ctr128_inc(unsigned char *counter)
 {
-    u32 n = 16;
-    u8 c;
+    u32 n = 16, c = 1;
 
     do {
         --n;
-        c = counter[n];
-        ++c;
-        counter[n] = c;
-        if (c)
-            return;
+        c += counter[n];
+        counter[n] = (u8)c;
+        c >>= 8;
     } while (n);
 }
 
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
 static void ctr128_inc_aligned(unsigned char *counter)
 {
-    size_t *data, c, n;
+    size_t *data, c, d, n;
     const union {
         long one;
         char little;
     } is_endian = {
         1
     };
 
-    if (is_endian.little) {
+    if (is_endian.little || ((size_t)counter % sizeof(size_t)) != 0) {
         ctr128_inc(counter);
         return;
     }
 
     data = (size_t *)counter;
+    c = 1;
     n = 16 / sizeof(size_t);
     do {
         --n;
-        c = data[n];
-        ++c;
-        data[n] = c;
-        if (c)
-            return;
+        d = data[n] += c;
+        /* did addition carry? */
+        c = ((d - c) ^ d) >> (sizeof(size_t) * 8 - 1);
     } while (n);
 }
 #endif
 
 /*
  * The input encrypted as though 128bit counter mode is being used.  The
  * extra state information to record how much of the 128bit block we have
  * used is contained in *num, and the encrypted counter is kept in
  * ecount_buf.  Both *num and ecount_buf must be initialised with zeros
  * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
  * that the counter is in the x lower bits of the IV (ivec), and that the
  * application has full control over overflow and the rest of the IV.  This
  * implementation takes NO responsability for checking that the counter
  * doesn't overflow into the rest of the IV when incremented.
  */
 void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
                            size_t len, const void *key,
                            unsigned char ivec[16],
                            unsigned char ecount_buf[16], unsigned int *num,
                            block128_f block)
 {
     unsigned int n;
     size_t l = 0;
 
     assert(in && out && key && ecount_buf && num);
     assert(*num < 16);
 
     n = *num;
 
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
     if (16 % sizeof(size_t) == 0) { /* always true actually */
         do {
             while (n && len) {
                 *(out++) = *(in++) ^ ecount_buf[n];
                 --len;
                 n = (n + 1) % 16;
             }
 
 # if defined(STRICT_ALIGNMENT)
-            if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) !=
-                0)
+            if (((size_t)in | (size_t)out | (size_t)ecount_buf)
+                % sizeof(size_t) != 0)
                 break;
 # endif
             while (len >= 16) {
                 (*block) (ivec, ecount_buf, key);
                 ctr128_inc_aligned(ivec);
-                for (; n < 16; n += sizeof(size_t))
+                for (n = 0; n < 16; n += sizeof(size_t))
                     *(size_t *)(out + n) =
                         *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n);
                 len -= 16;
                 out += 16;
                 in += 16;
                 n = 0;
             }
             if (len) {
                 (*block) (ivec, ecount_buf, key);
                 ctr128_inc_aligned(ivec);
                 while (len--) {
                     out[n] = in[n] ^ ecount_buf[n];
                     ++n;
                 }
             }
             *num = n;
             return;
         } while (0);
     }
     /* the rest would be commonly eliminated by x86* compiler */
 #endif
     while (l < len) {
         if (n == 0) {
             (*block) (ivec, ecount_buf, key);
             ctr128_inc(ivec);
         }
         out[l] = in[l] ^ ecount_buf[n];
         ++l;
         n = (n + 1) % 16;
     }
 
     *num = n;
 }
 
 /* increment upper 96 bits of 128-bit counter by 1 */
 static void ctr96_inc(unsigned char *counter)
 {
-    u32 n = 12;
-    u8 c;
+    u32 n = 12, c = 1;
 
     do {
         --n;
-        c = counter[n];
-        ++c;
-        counter[n] = c;
-        if (c)
-            return;
+        c += counter[n];
+        counter[n] = (u8)c;
+        c >>= 8;
     } while (n);
 }
 
 void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
                                  size_t len, const void *key,
                                  unsigned char ivec[16],
                                  unsigned char ecount_buf[16],
                                  unsigned int *num, ctr128_f func)
 {
     unsigned int n, ctr32;
 
     assert(in && out && key && ecount_buf && num);
     assert(*num < 16);
 
     n = *num;
 
     while (n && len) {
         *(out++) = *(in++) ^ ecount_buf[n];
         --len;
         n = (n + 1) % 16;
     }
 
     ctr32 = GETU32(ivec + 12);
     while (len >= 16) {
         size_t blocks = len / 16;
         /*
          * 1<<28 is just a not-so-small yet not-so-large number...
          * Below condition is practically never met, but it has to
          * be checked for code correctness.
          */
         if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
             blocks = (1U << 28);
         /*
          * As (*func) operates on 32-bit counter, caller
          * has to handle overflow. 'if' below detects the
          * overflow, which is then handled by limiting the
          * amount of blocks to the exact overflow point...
          */
         ctr32 += (u32)blocks;
         if (ctr32 < blocks) {
             blocks -= ctr32;
             ctr32 = 0;
         }
         (*func) (in, out, blocks, key, ivec);
         /* (*ctr) does not update ivec, caller does: */
         PUTU32(ivec + 12, ctr32);
         /* ... overflow was detected, propogate carry. */
         if (ctr32 == 0)
             ctr96_inc(ivec);
         blocks *= 16;
         len -= blocks;
         out += blocks;
         in += blocks;
     }
     if (len) {
         memset(ecount_buf, 0, 16);
         (*func) (ecount_buf, ecount_buf, 1, key, ivec);
         ++ctr32;
         PUTU32(ivec + 12, ctr32);
         if (ctr32 == 0)
             ctr96_inc(ivec);
         while (len--) {
             out[n] = in[n] ^ ecount_buf[n];
             ++n;
         }
     }
 
     *num = n;
 }
Index: vendor-crypto/openssl/dist/crypto/opensslconf.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/opensslconf.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/opensslconf.h	(revision 296273)
@@ -1,253 +1,265 @@
 /* opensslconf.h */
 /* WARNING: Generated automatically from opensslconf.h.in by Configure. */
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 /* OpenSSL was configured with the following options: */
 #ifndef OPENSSL_DOING_MAKEDEPEND
 
 
 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
 # define OPENSSL_NO_EC_NISTP_64_GCC_128
 #endif
 #ifndef OPENSSL_NO_GMP
 # define OPENSSL_NO_GMP
 #endif
 #ifndef OPENSSL_NO_JPAKE
 # define OPENSSL_NO_JPAKE
 #endif
 #ifndef OPENSSL_NO_KRB5
 # define OPENSSL_NO_KRB5
 #endif
 #ifndef OPENSSL_NO_LIBUNBOUND
 # define OPENSSL_NO_LIBUNBOUND
 #endif
 #ifndef OPENSSL_NO_MD2
 # define OPENSSL_NO_MD2
 #endif
 #ifndef OPENSSL_NO_RC5
 # define OPENSSL_NO_RC5
 #endif
 #ifndef OPENSSL_NO_RFC3779
 # define OPENSSL_NO_RFC3779
 #endif
 #ifndef OPENSSL_NO_SCTP
 # define OPENSSL_NO_SCTP
 #endif
 #ifndef OPENSSL_NO_SSL_TRACE
 # define OPENSSL_NO_SSL_TRACE
 #endif
+#ifndef OPENSSL_NO_SSL2
+# define OPENSSL_NO_SSL2
+#endif
 #ifndef OPENSSL_NO_STORE
 # define OPENSSL_NO_STORE
 #endif
 #ifndef OPENSSL_NO_UNIT_TEST
 # define OPENSSL_NO_UNIT_TEST
 #endif
+#ifndef OPENSSL_NO_WEAK_SSL_CIPHERS
+# define OPENSSL_NO_WEAK_SSL_CIPHERS
+#endif
 
 #endif /* OPENSSL_DOING_MAKEDEPEND */
 
 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
 # define OPENSSL_NO_DYNAMIC_ENGINE
 #endif
 
 /* The OPENSSL_NO_* macros are also defined as NO_* if the application
    asks for it.  This is a transient feature that is provided for those
    who haven't had the time to do the appropriate changes in their
    applications.  */
 #ifdef OPENSSL_ALGORITHM_DEFINES
 # if defined(OPENSSL_NO_EC_NISTP_64_GCC_128) && !defined(NO_EC_NISTP_64_GCC_128)
 #  define NO_EC_NISTP_64_GCC_128
 # endif
 # if defined(OPENSSL_NO_GMP) && !defined(NO_GMP)
 #  define NO_GMP
 # endif
 # if defined(OPENSSL_NO_JPAKE) && !defined(NO_JPAKE)
 #  define NO_JPAKE
 # endif
 # if defined(OPENSSL_NO_KRB5) && !defined(NO_KRB5)
 #  define NO_KRB5
 # endif
 # if defined(OPENSSL_NO_LIBUNBOUND) && !defined(NO_LIBUNBOUND)
 #  define NO_LIBUNBOUND
 # endif
 # if defined(OPENSSL_NO_MD2) && !defined(NO_MD2)
 #  define NO_MD2
 # endif
 # if defined(OPENSSL_NO_RC5) && !defined(NO_RC5)
 #  define NO_RC5
 # endif
 # if defined(OPENSSL_NO_RFC3779) && !defined(NO_RFC3779)
 #  define NO_RFC3779
 # endif
 # if defined(OPENSSL_NO_SCTP) && !defined(NO_SCTP)
 #  define NO_SCTP
 # endif
 # if defined(OPENSSL_NO_SSL_TRACE) && !defined(NO_SSL_TRACE)
 #  define NO_SSL_TRACE
 # endif
+# if defined(OPENSSL_NO_SSL2) && !defined(NO_SSL2)
+#  define NO_SSL2
+# endif
 # if defined(OPENSSL_NO_STORE) && !defined(NO_STORE)
 #  define NO_STORE
 # endif
 # if defined(OPENSSL_NO_UNIT_TEST) && !defined(NO_UNIT_TEST)
 #  define NO_UNIT_TEST
+# endif
+# if defined(OPENSSL_NO_WEAK_SSL_CIPHERS) && !defined(NO_WEAK_SSL_CIPHERS)
+#  define NO_WEAK_SSL_CIPHERS
 # endif
 #endif
 
 /* crypto/opensslconf.h.in */
 
 /* Generate 80386 code? */
 #undef I386_ONLY
 
 #if !(defined(VMS) || defined(__VMS)) /* VMS uses logical names instead */
 #if defined(HEADER_CRYPTLIB_H) && !defined(OPENSSLDIR)
 #define ENGINESDIR "/usr/local/ssl/lib/engines"
 #define OPENSSLDIR "/usr/local/ssl"
 #endif
 #endif
 
 #undef OPENSSL_UNISTD
 #define OPENSSL_UNISTD 
 
 #undef OPENSSL_EXPORT_VAR_AS_FUNCTION
 
 #if defined(HEADER_IDEA_H) && !defined(IDEA_INT)
 #define IDEA_INT unsigned int
 #endif
 
 #if defined(HEADER_MD2_H) && !defined(MD2_INT)
 #define MD2_INT unsigned int
 #endif
 
 #if defined(HEADER_RC2_H) && !defined(RC2_INT)
 /* I need to put in a mod for the alpha - eay */
 #define RC2_INT unsigned int
 #endif
 
 #if defined(HEADER_RC4_H)
 #if !defined(RC4_INT)
 /* using int types make the structure larger but make the code faster
  * on most boxes I have tested - up to %20 faster. */
 /*
  * I don't know what does "most" mean, but declaring "int" is a must on:
  * - Intel P6 because partial register stalls are very expensive;
  * - elder Alpha because it lacks byte load/store instructions;
  */
 #define RC4_INT unsigned int
 #endif
 #if !defined(RC4_CHUNK)
 /*
  * This enables code handling data aligned at natural CPU word
  * boundary. See crypto/rc4/rc4_enc.c for further details.
  */
 #undef RC4_CHUNK
 #endif
 #endif
 
 #if (defined(HEADER_NEW_DES_H) || defined(HEADER_DES_H)) && !defined(DES_LONG)
 /* If this is set to 'unsigned int' on a DEC Alpha, this gives about a
  * %20 speed up (longs are 8 bytes, int's are 4). */
 #ifndef DES_LONG
 #define DES_LONG unsigned long
 #endif
 #endif
 
 #if defined(HEADER_BN_H) && !defined(CONFIG_HEADER_BN_H)
 #define CONFIG_HEADER_BN_H
 #undef BN_LLONG
 
 /* Should we define BN_DIV2W here? */
 
 /* Only one for the following should be defined */
 #undef SIXTY_FOUR_BIT_LONG
 #undef SIXTY_FOUR_BIT
 #define THIRTY_TWO_BIT
 #endif
 
 #if defined(HEADER_RC4_LOCL_H) && !defined(CONFIG_HEADER_RC4_LOCL_H)
 #define CONFIG_HEADER_RC4_LOCL_H
 /* if this is defined data[i] is used instead of *data, this is a %20
  * speedup on x86 */
 #undef RC4_INDEX
 #endif
 
 #if defined(HEADER_BF_LOCL_H) && !defined(CONFIG_HEADER_BF_LOCL_H)
 #define CONFIG_HEADER_BF_LOCL_H
 #undef BF_PTR
 #endif /* HEADER_BF_LOCL_H */
 
 #if defined(HEADER_DES_LOCL_H) && !defined(CONFIG_HEADER_DES_LOCL_H)
 #define CONFIG_HEADER_DES_LOCL_H
 #ifndef DES_DEFAULT_OPTIONS
 /* the following is tweaked from a config script, that is why it is a
  * protected undef/define */
 #ifndef DES_PTR
 #undef DES_PTR
 #endif
 
 /* This helps C compiler generate the correct code for multiple functional
  * units.  It reduces register dependancies at the expense of 2 more
  * registers */
 #ifndef DES_RISC1
 #undef DES_RISC1
 #endif
 
 #ifndef DES_RISC2
 #undef DES_RISC2
 #endif
 
 #if defined(DES_RISC1) && defined(DES_RISC2)
 #error YOU SHOULD NOT HAVE BOTH DES_RISC1 AND DES_RISC2 DEFINED!!!!!
 #endif
 
 /* Unroll the inner loop, this sometimes helps, sometimes hinders.
  * Very mucy CPU dependant */
 #ifndef DES_UNROLL
 #undef DES_UNROLL
 #endif
 
 /* These default values were supplied by
  * Peter Gutman 
  * They are only used if nothing else has been defined */
 #if !defined(DES_PTR) && !defined(DES_RISC1) && !defined(DES_RISC2) && !defined(DES_UNROLL)
 /* Special defines which change the way the code is built depending on the
    CPU and OS.  For SGI machines you can use _MIPS_SZLONG (32 or 64) to find
    even newer MIPS CPU's, but at the moment one size fits all for
    optimization options.  Older Sparc's work better with only UNROLL, but
    there's no way to tell at compile time what it is you're running on */
  
 #if defined( __sun ) || defined ( sun )		/* Newer Sparc's */
 #  define DES_PTR
 #  define DES_RISC1
 #  define DES_UNROLL
 #elif defined( __ultrix )	/* Older MIPS */
 #  define DES_PTR
 #  define DES_RISC2
 #  define DES_UNROLL
 #elif defined( __osf1__ )	/* Alpha */
 #  define DES_PTR
 #  define DES_RISC2
 #elif defined ( _AIX )		/* RS6000 */
   /* Unknown */
 #elif defined( __hpux )		/* HP-PA */
   /* Unknown */
 #elif defined( __aux )		/* 68K */
   /* Unknown */
 #elif defined( __dgux )		/* 88K (but P6 in latest boxes) */
 #  define DES_UNROLL
 #elif defined( __sgi )		/* Newer MIPS */
 #  define DES_PTR
 #  define DES_RISC2
 #  define DES_UNROLL
 #elif defined(i386) || defined(__i386__)	/* x86 boxes, should be gcc */
 #  define DES_PTR
 #  define DES_RISC1
 #  define DES_UNROLL
 #endif /* Systems-specific speed defines */
 #endif
 
 #endif /* DES_DEFAULT_OPTIONS */
 #endif /* HEADER_DES_LOCL_H */
 #ifdef  __cplusplus
 }
 #endif
Index: vendor-crypto/openssl/dist/crypto/opensslv.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/opensslv.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/opensslv.h	(revision 296273)
@@ -1,97 +1,97 @@
 #ifndef HEADER_OPENSSLV_H
 # define HEADER_OPENSSLV_H
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 /*-
  * Numeric release version identifier:
  * MNNFFPPS: major minor fix patch status
  * The status nibble has one of the values 0 for development, 1 to e for betas
  * 1 to 14, and f for release.  The patch level is exactly that.
  * For example:
  * 0.9.3-dev      0x00903000
  * 0.9.3-beta1    0x00903001
  * 0.9.3-beta2-dev 0x00903002
  * 0.9.3-beta2    0x00903002 (same as ...beta2-dev)
  * 0.9.3          0x0090300f
  * 0.9.3a         0x0090301f
  * 0.9.4          0x0090400f
  * 1.2.3z         0x102031af
  *
  * For continuity reasons (because 0.9.5 is already out, and is coded
  * 0x00905100), between 0.9.5 and 0.9.6 the coding of the patch level
  * part is slightly different, by setting the highest bit.  This means
  * that 0.9.5a looks like this: 0x0090581f.  At 0.9.6, we can start
  * with 0x0090600S...
  *
  * (Prior to 0.9.3-dev a different scheme was used: 0.9.2b is 0x0922.)
  * (Prior to 0.9.5a beta1, a different scheme was used: MMNNFFRBB for
  *  major minor fix final patch/beta)
  */
-# define OPENSSL_VERSION_NUMBER  0x1000206fL
+# define OPENSSL_VERSION_NUMBER  0x1000207fL
 # ifdef OPENSSL_FIPS
-#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2f-fips  28 Jan 2016"
+#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2g-fips  1 Mar 2016"
 # else
-#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2f  28 Jan 2016"
+#  define OPENSSL_VERSION_TEXT    "OpenSSL 1.0.2g  1 Mar 2016"
 # endif
 # define OPENSSL_VERSION_PTEXT   " part of " OPENSSL_VERSION_TEXT
 
 /*-
  * The macros below are to be used for shared library (.so, .dll, ...)
  * versioning.  That kind of versioning works a bit differently between
  * operating systems.  The most usual scheme is to set a major and a minor
  * number, and have the runtime loader check that the major number is equal
  * to what it was at application link time, while the minor number has to
  * be greater or equal to what it was at application link time.  With this
  * scheme, the version number is usually part of the file name, like this:
  *
  *      libcrypto.so.0.9
  *
  * Some unixen also make a softlink with the major verson number only:
  *
  *      libcrypto.so.0
  *
  * On Tru64 and IRIX 6.x it works a little bit differently.  There, the
  * shared library version is stored in the file, and is actually a series
  * of versions, separated by colons.  The rightmost version present in the
  * library when linking an application is stored in the application to be
  * matched at run time.  When the application is run, a check is done to
  * see if the library version stored in the application matches any of the
  * versions in the version string of the library itself.
  * This version string can be constructed in any way, depending on what
  * kind of matching is desired.  However, to implement the same scheme as
  * the one used in the other unixen, all compatible versions, from lowest
  * to highest, should be part of the string.  Consecutive builds would
  * give the following versions strings:
  *
  *      3.0
  *      3.0:3.1
  *      3.0:3.1:3.2
  *      4.0
  *      4.0:4.1
  *
  * Notice how version 4 is completely incompatible with version, and
  * therefore give the breach you can see.
  *
  * There may be other schemes as well that I haven't yet discovered.
  *
  * So, here's the way it works here: first of all, the library version
  * number doesn't need at all to match the overall OpenSSL version.
  * However, it's nice and more understandable if it actually does.
  * The current library version is stored in the macro SHLIB_VERSION_NUMBER,
  * which is just a piece of text in the format "M.m.e" (Major, minor, edit).
  * For the sake of Tru64, IRIX, and any other OS that behaves in similar ways,
  * we need to keep a history of version numbers, which is done in the
  * macro SHLIB_VERSION_HISTORY.  The numbers are separated by colons and
  * should only keep the versions that are binary compatible with the current.
  */
 # define SHLIB_VERSION_HISTORY ""
 # define SHLIB_VERSION_NUMBER "1.0.0"
 
 
 #ifdef  __cplusplus
 }
 #endif
 #endif                          /* HEADER_OPENSSLV_H */
Index: vendor-crypto/openssl/dist/crypto/perlasm/x86_64-xlate.pl
===================================================================
--- vendor-crypto/openssl/dist/crypto/perlasm/x86_64-xlate.pl	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/perlasm/x86_64-xlate.pl	(revision 296273)
@@ -1,1148 +1,1151 @@
 #!/usr/bin/env perl
 
 # Ascetic x86_64 AT&T to MASM/NASM assembler translator by .
 #
 # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
 # format is way easier to parse. Because it's simpler to "gear" from
 # Unix ABI to Windows one [see cross-reference "card" at the end of
 # file]. Because Linux targets were available first...
 #
 # In addition the script also "distills" code suitable for GNU
 # assembler, so that it can be compiled with more rigid assemblers,
 # such as Solaris /usr/ccs/bin/as.
 #
 # This translator is not designed to convert *arbitrary* assembler
 # code from AT&T format to MASM one. It's designed to convert just
 # enough to provide for dual-ABI OpenSSL modules development...
 # There *are* limitations and you might have to modify your assembler
 # code or this script to achieve the desired result...
 #
 # Currently recognized limitations:
 #
 # - can't use multiple ops per line;
 #
 # Dual-ABI styling rules.
 #
 # 1. Adhere to Unix register and stack layout [see cross-reference
 #    ABI "card" at the end for explanation].
 # 2. Forget about "red zone," stick to more traditional blended
 #    stack frame allocation. If volatile storage is actually required
 #    that is. If not, just leave the stack as is.
 # 3. Functions tagged with ".type name,@function" get crafted with
 #    unified Win64 prologue and epilogue automatically. If you want
 #    to take care of ABI differences yourself, tag functions as
 #    ".type name,@abi-omnipotent" instead.
 # 4. To optimize the Win64 prologue you can specify number of input
 #    arguments as ".type name,@function,N." Keep in mind that if N is
 #    larger than 6, then you *have to* write "abi-omnipotent" code,
 #    because >6 cases can't be addressed with unified prologue.
 # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
 #    (sorry about latter).
 # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
 #    required to identify the spots, where to inject Win64 epilogue!
 #    But on the pros, it's then prefixed with rep automatically:-)
 # 7. Stick to explicit ip-relative addressing. If you have to use
 #    GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
 #    Both are recognized and translated to proper Win64 addressing
 #    modes. To support legacy code a synthetic directive, .picmeup,
 #    is implemented. It puts address of the *next* instruction into
 #    target register, e.g.:
 #
 #		.picmeup	%rax
 #		lea		.Label-.(%rax),%rax
 #
 # 8. In order to provide for structured exception handling unified
 #    Win64 prologue copies %rsp value to %rax. For further details
 #    see SEH paragraph at the end.
 # 9. .init segment is allowed to contain calls to functions only.
 # a. If function accepts more than 4 arguments *and* >4th argument
 #    is declared as non 64-bit value, do clear its upper part.
 
 my $flavour = shift;
 my $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
 open STDOUT,">$output" || die "can't open $output: $!"
 	if (defined($output));
 
 my $gas=1;	$gas=0 if ($output =~ /\.asm$/);
 my $elf=1;	$elf=0 if (!$gas);
 my $win64=0;
 my $prefix="";
 my $decor=".L";
 
 my $masmref=8 + 50727*2**-32;	# 8.00.50727 shipped with VS2005
 my $masm=0;
 my $PTR=" PTR";
 
 my $nasmref=2.03;
 my $nasm=0;
 
 if    ($flavour eq "mingw64")	{ $gas=1; $elf=0; $win64=1;
 				  $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
 				  chomp($prefix);
 				}
 elsif ($flavour eq "macosx")	{ $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
 elsif ($flavour eq "masm")	{ $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
 elsif ($flavour eq "nasm")	{ $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
 elsif (!$gas)
 {   if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
     {	$nasm = $1 + $2*0.01; $PTR="";  }
     elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
     {	$masm = $1 + $2*2**-16 + $4*2**-32;   }
     die "no assembler found on %PATH" if (!($nasm || $masm));
     $win64=1;
     $elf=0;
     $decor="\$L\$";
 }
 
 my $current_segment;
 my $current_function;
 my %globals;
 
 { package opcode;	# pick up opcodes
     sub re {
 	my	$self = shift;	# single instance in enough...
 	local	*line = shift;
 	undef	$ret;
 
 	if ($line =~ /^([a-z][a-z0-9]*)/i) {
 	    $self->{op} = $1;
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 
 	    undef $self->{sz};
 	    if ($self->{op} =~ /^(movz)x?([bw]).*/) {	# movz is pain...
 		$self->{op} = $1;
 		$self->{sz} = $2;
 	    } elsif ($self->{op} =~ /call|jmp/) {
 		$self->{sz} = "";
 	    } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
 		$self->{sz} = "";
 	    } elsif ($self->{op} =~ /^v/) { # VEX
 		$self->{sz} = "";
 	    } elsif ($self->{op} =~ /mov[dq]/ && $line =~ /%xmm/) {
 		$self->{sz} = "";
 	    } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
 		$self->{op} = $1;
 		$self->{sz} = $2;
 	    }
 	}
 	$ret;
     }
     sub size {
 	my $self = shift;
 	my $sz   = shift;
 	$self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
 	$self->{sz};
     }
     sub out {
 	my $self = shift;
 	if ($gas) {
 	    if ($self->{op} eq "movz") {	# movz is pain...
 		sprintf "%s%s%s",$self->{op},$self->{sz},shift;
 	    } elsif ($self->{op} =~ /^set/) { 
 		"$self->{op}";
 	    } elsif ($self->{op} eq "ret") {
 		my $epilogue = "";
 		if ($win64 && $current_function->{abi} eq "svr4") {
 		    $epilogue = "movq	8(%rsp),%rdi\n\t" .
 				"movq	16(%rsp),%rsi\n\t";
 		}
 	    	$epilogue . ".byte	0xf3,0xc3";
 	    } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
 		".p2align\t3\n\t.quad";
 	    } else {
 		"$self->{op}$self->{sz}";
 	    }
 	} else {
 	    $self->{op} =~ s/^movz/movzx/;
 	    if ($self->{op} eq "ret") {
 		$self->{op} = "";
 		if ($win64 && $current_function->{abi} eq "svr4") {
 		    $self->{op} = "mov	rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t".
 				  "mov	rsi,QWORD${PTR}[16+rsp]\n\t";
 	    	}
 		$self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
 	    } elsif ($self->{op} =~ /^(pop|push)f/) {
 		$self->{op} .= $self->{sz};
 	    } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
 		$self->{op} = "\tDQ";
 	    } 
 	    $self->{op};
 	}
     }
     sub mnemonic {
 	my $self=shift;
 	my $op=shift;
 	$self->{op}=$op if (defined($op));
 	$self->{op};
     }
 }
 { package const;	# pick up constants, which start with $
     sub re {
 	my	$self = shift;	# single instance in enough...
 	local	*line = shift;
 	undef	$ret;
 
 	if ($line =~ /^\$([^,]+)/) {
 	    $self->{value} = $1;
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 	}
 	$ret;
     }
     sub out {
     	my $self = shift;
 
 	if ($gas) {
 	    # Solaris /usr/ccs/bin/as can't handle multiplications
 	    # in $self->{value}
-	    $self->{value} =~ s/(?{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
+	    my $value = $self->{value};
+	    $value =~ s/(?{value} = $value;
+	    }
 	    sprintf "\$%s",$self->{value};
 	} else {
 	    $self->{value} =~ s/(0b[0-1]+)/oct($1)/eig;
 	    $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
 	    sprintf "%s",$self->{value};
 	}
     }
 }
 { package ea;		# pick up effective addresses: expr(%reg,%reg,scale)
     sub re {
 	my	$self = shift;	# single instance in enough...
 	local	*line = shift;
 	undef	$ret;
 
 	# optional * ---vvv--- appears in indirect jmp/call
 	if ($line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) {
 	    $self->{asterisk} = $1;
 	    $self->{label} = $2;
 	    ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
 	    $self->{scale} = 1 if (!defined($self->{scale}));
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 
 	    if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
 		die if (opcode->mnemonic() ne "mov");
 		opcode->mnemonic("lea");
 	    }
 	    $self->{base}  =~ s/^%//;
 	    $self->{index} =~ s/^%// if (defined($self->{index}));
 	}
 	$ret;
     }
     sub size {}
     sub out {
     	my $self = shift;
 	my $sz = shift;
 
 	$self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
 	$self->{label} =~ s/\.L/$decor/g;
 
 	# Silently convert all EAs to 64-bit. This is required for
 	# elder GNU assembler and results in more compact code,
 	# *but* most importantly AES module depends on this feature!
 	$self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
 	$self->{base}  =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
 
 	# Solaris /usr/ccs/bin/as can't handle multiplications
 	# in $self->{label}, new gas requires sign extension...
 	use integer;
 	$self->{label} =~ s/(?{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
 	$self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
 
 	if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
 	    $self->{base} =~ /(rbp|r13)/) {
 		$self->{base} = $self->{index}; $self->{index} = $1;
 	}
 
 	if ($gas) {
 	    $self->{label} =~ s/^___imp_/__imp__/   if ($flavour eq "mingw64");
 
 	    if (defined($self->{index})) {
 		sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk},
 					$self->{label},
 					$self->{base}?"%$self->{base}":"",
 					$self->{index},$self->{scale};
 	    } else {
 		sprintf "%s%s(%%%s)",	$self->{asterisk},$self->{label},$self->{base};
 	    }
 	} else {
 	    %szmap = (	b=>"BYTE$PTR",  w=>"WORD$PTR",
 			l=>"DWORD$PTR", d=>"DWORD$PTR",
 	    		q=>"QWORD$PTR", o=>"OWORD$PTR",
 			x=>"XMMWORD$PTR", y=>"YMMWORD$PTR", z=>"ZMMWORD$PTR" );
 
 	    $self->{label} =~ s/\./\$/g;
 	    $self->{label} =~ s/(?{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
 
 	    ($self->{asterisk})					&& ($sz="q") ||
 	    (opcode->mnemonic() =~ /^v?mov([qd])$/)		&& ($sz=$1)  ||
 	    (opcode->mnemonic() =~ /^v?pinsr([qdwb])$/)		&& ($sz=$1)  ||
 	    (opcode->mnemonic() =~ /^vpbroadcast([qdwb])$/)	&& ($sz=$1)  ||
 	    (opcode->mnemonic() =~ /^vinsert[fi]128$/)		&& ($sz="x");
 
 	    if (defined($self->{index})) {
 		sprintf "%s[%s%s*%d%s]",$szmap{$sz},
 					$self->{label}?"$self->{label}+":"",
 					$self->{index},$self->{scale},
 					$self->{base}?"+$self->{base}":"";
 	    } elsif ($self->{base} eq "rip") {
 		sprintf "%s[%s]",$szmap{$sz},$self->{label};
 	    } else {
 		sprintf "%s[%s%s]",$szmap{$sz},
 					$self->{label}?"$self->{label}+":"",
 					$self->{base};
 	    }
 	}
     }
 }
 { package register;	# pick up registers, which start with %.
     sub re {
 	my	$class = shift;	# muliple instances...
 	my	$self = {};
 	local	*line = shift;
 	undef	$ret;
 
 	# optional * ---vvv--- appears in indirect jmp/call
 	if ($line =~ /^(\*?)%(\w+)/) {
 	    bless $self,$class;
 	    $self->{asterisk} = $1;
 	    $self->{value} = $2;
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 	}
 	$ret;
     }
     sub size {
 	my	$self = shift;
 	undef	$ret;
 
 	if    ($self->{value} =~ /^r[\d]+b$/i)	{ $ret="b"; }
 	elsif ($self->{value} =~ /^r[\d]+w$/i)	{ $ret="w"; }
 	elsif ($self->{value} =~ /^r[\d]+d$/i)	{ $ret="l"; }
 	elsif ($self->{value} =~ /^r[\w]+$/i)	{ $ret="q"; }
 	elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
 	elsif ($self->{value} =~ /^[\w]{2}l$/i)	{ $ret="b"; }
 	elsif ($self->{value} =~ /^[\w]{2}$/i)	{ $ret="w"; }
 	elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
 
 	$ret;
     }
     sub out {
     	my $self = shift;
 	if ($gas)	{ sprintf "%s%%%s",$self->{asterisk},$self->{value}; }
 	else		{ $self->{value}; }
     }
 }
 { package label;	# pick up labels, which end with :
     sub re {
 	my	$self = shift;	# single instance is enough...
 	local	*line = shift;
 	undef	$ret;
 
 	if ($line =~ /(^[\.\w]+)\:/) {
 	    $self->{value} = $1;
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 
 	    $self->{value} =~ s/^\.L/$decor/;
 	}
 	$ret;
     }
     sub out {
 	my $self = shift;
 
 	if ($gas) {
 	    my $func = ($globals{$self->{value}} or $self->{value}) . ":";
 	    if ($win64	&&
 			$current_function->{name} eq $self->{value} &&
 			$current_function->{abi} eq "svr4") {
 		$func .= "\n";
 		$func .= "	movq	%rdi,8(%rsp)\n";
 		$func .= "	movq	%rsi,16(%rsp)\n";
 		$func .= "	movq	%rsp,%rax\n";
 		$func .= "${decor}SEH_begin_$current_function->{name}:\n";
 		my $narg = $current_function->{narg};
 		$narg=6 if (!defined($narg));
 		$func .= "	movq	%rcx,%rdi\n" if ($narg>0);
 		$func .= "	movq	%rdx,%rsi\n" if ($narg>1);
 		$func .= "	movq	%r8,%rdx\n"  if ($narg>2);
 		$func .= "	movq	%r9,%rcx\n"  if ($narg>3);
 		$func .= "	movq	40(%rsp),%r8\n" if ($narg>4);
 		$func .= "	movq	48(%rsp),%r9\n" if ($narg>5);
 	    }
 	    $func;
 	} elsif ($self->{value} ne "$current_function->{name}") {
 	    $self->{value} .= ":" if ($masm && $ret!~m/^\$/);
 	    $self->{value} . ":";
 	} elsif ($win64 && $current_function->{abi} eq "svr4") {
 	    my $func =	"$current_function->{name}" .
 			($nasm ? ":" : "\tPROC $current_function->{scope}") .
 			"\n";
 	    $func .= "	mov	QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n";
 	    $func .= "	mov	QWORD${PTR}[16+rsp],rsi\n";
 	    $func .= "	mov	rax,rsp\n";
 	    $func .= "${decor}SEH_begin_$current_function->{name}:";
 	    $func .= ":" if ($masm);
 	    $func .= "\n";
 	    my $narg = $current_function->{narg};
 	    $narg=6 if (!defined($narg));
 	    $func .= "	mov	rdi,rcx\n" if ($narg>0);
 	    $func .= "	mov	rsi,rdx\n" if ($narg>1);
 	    $func .= "	mov	rdx,r8\n"  if ($narg>2);
 	    $func .= "	mov	rcx,r9\n"  if ($narg>3);
 	    $func .= "	mov	r8,QWORD${PTR}[40+rsp]\n" if ($narg>4);
 	    $func .= "	mov	r9,QWORD${PTR}[48+rsp]\n" if ($narg>5);
 	    $func .= "\n";
 	} else {
 	   "$current_function->{name}".
 			($nasm ? ":" : "\tPROC $current_function->{scope}");
 	}
     }
 }
 { package expr;		# pick up expressioins
     sub re {
 	my	$self = shift;	# single instance is enough...
 	local	*line = shift;
 	undef	$ret;
 
 	if ($line =~ /(^[^,]+)/) {
 	    $self->{value} = $1;
 	    $ret = $self;
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 
 	    $self->{value} =~ s/\@PLT// if (!$elf);
 	    $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
 	    $self->{value} =~ s/\.L/$decor/g;
 	}
 	$ret;
     }
     sub out {
 	my $self = shift;
 	if ($nasm && opcode->mnemonic()=~m/^j(?![re]cxz)/) {
 	    "NEAR ".$self->{value};
 	} else {
 	    $self->{value};
 	}
     }
 }
 { package directive;	# pick up directives, which start with .
     sub re {
 	my	$self = shift;	# single instance is enough...
 	local	*line = shift;
 	undef	$ret;
 	my	$dir;
 	my	%opcode =	# lea 2f-1f(%rip),%dst; 1: nop; 2:
 		(	"%rax"=>0x01058d48,	"%rcx"=>0x010d8d48,
 			"%rdx"=>0x01158d48,	"%rbx"=>0x011d8d48,
 			"%rsp"=>0x01258d48,	"%rbp"=>0x012d8d48,
 			"%rsi"=>0x01358d48,	"%rdi"=>0x013d8d48,
 			"%r8" =>0x01058d4c,	"%r9" =>0x010d8d4c,
 			"%r10"=>0x01158d4c,	"%r11"=>0x011d8d4c,
 			"%r12"=>0x01258d4c,	"%r13"=>0x012d8d4c,
 			"%r14"=>0x01358d4c,	"%r15"=>0x013d8d4c	);
 
 	if ($line =~ /^\s*(\.\w+)/) {
 	    $dir = $1;
 	    $ret = $self;
 	    undef $self->{value};
 	    $line = substr($line,@+[0]); $line =~ s/^\s+//;
 
 	    SWITCH: for ($dir) {
 		/\.picmeup/ && do { if ($line =~ /(%r[\w]+)/i) {
 			    		$dir="\t.long";
 					$line=sprintf "0x%x,0x90000000",$opcode{$1};
 				    }
 				    last;
 				  };
 		/\.global|\.globl|\.extern/
 			    && do { $globals{$line} = $prefix . $line;
 				    $line = $globals{$line} if ($prefix);
 				    last;
 				  };
 		/\.type/    && do { ($sym,$type,$narg) = split(',',$line);
 				    if ($type eq "\@function") {
 					undef $current_function;
 					$current_function->{name} = $sym;
 					$current_function->{abi}  = "svr4";
 					$current_function->{narg} = $narg;
 					$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
 				    } elsif ($type eq "\@abi-omnipotent") {
 					undef $current_function;
 					$current_function->{name} = $sym;
 					$current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
 				    }
 				    $line =~ s/\@abi\-omnipotent/\@function/;
 				    $line =~ s/\@function.*/\@function/;
 				    last;
 				  };
 		/\.asciz/   && do { if ($line =~ /^"(.*)"$/) {
 					$dir  = ".byte";
 					$line = join(",",unpack("C*",$1),0);
 				    }
 				    last;
 				  };
 		/\.rva|\.long|\.quad/
 			    && do { $line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
 				    $line =~ s/\.L/$decor/g;
 				    last;
 				  };
 	    }
 
 	    if ($gas) {
 		$self->{value} = $dir . "\t" . $line;
 
 		if ($dir =~ /\.extern/) {
 		    $self->{value} = ""; # swallow extern
 		} elsif (!$elf && $dir =~ /\.type/) {
 		    $self->{value} = "";
 		    $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
 				(defined($globals{$1})?".scl 2;":".scl 3;") .
 				"\t.type 32;\t.endef"
 				if ($win64 && $line =~ /([^,]+),\@function/);
 		} elsif (!$elf && $dir =~ /\.size/) {
 		    $self->{value} = "";
 		    if (defined($current_function)) {
 			$self->{value} .= "${decor}SEH_end_$current_function->{name}:"
 				if ($win64 && $current_function->{abi} eq "svr4");
 			undef $current_function;
 		    }
 		} elsif (!$elf && $dir =~ /\.align/) {
 		    $self->{value} = ".p2align\t" . (log($line)/log(2));
 		} elsif ($dir eq ".section") {
 		    $current_segment=$line;
 		    if (!$elf && $current_segment eq ".init") {
 			if	($flavour eq "macosx")	{ $self->{value} = ".mod_init_func"; }
 			elsif	($flavour eq "mingw64")	{ $self->{value} = ".section\t.ctors"; }
 		    }
 		} elsif ($dir =~ /\.(text|data)/) {
 		    $current_segment=".$1";
 		} elsif ($dir =~ /\.hidden/) {
 		    if    ($flavour eq "macosx")  { $self->{value} = ".private_extern\t$prefix$line"; }
 		    elsif ($flavour eq "mingw64") { $self->{value} = ""; }
 		} elsif ($dir =~ /\.comm/) {
 		    $self->{value} = "$dir\t$prefix$line";
 		    $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
 		}
 		$line = "";
 		return $self;
 	    }
 
 	    # non-gas case or nasm/masm
 	    SWITCH: for ($dir) {
 		/\.text/    && do { my $v=undef;
 				    if ($nasm) {
 					$v="section	.text code align=64\n";
 				    } else {
 					$v="$current_segment\tENDS\n" if ($current_segment);
 					$current_segment = ".text\$";
 					$v.="$current_segment\tSEGMENT ";
 					$v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
 					$v.=" 'CODE'";
 				    }
 				    $self->{value} = $v;
 				    last;
 				  };
 		/\.data/    && do { my $v=undef;
 				    if ($nasm) {
 					$v="section	.data data align=8\n";
 				    } else {
 					$v="$current_segment\tENDS\n" if ($current_segment);
 					$current_segment = "_DATA";
 					$v.="$current_segment\tSEGMENT";
 				    }
 				    $self->{value} = $v;
 				    last;
 				  };
 		/\.section/ && do { my $v=undef;
 				    $line =~ s/([^,]*).*/$1/;
 				    $line = ".CRT\$XCU" if ($line eq ".init");
 				    if ($nasm) {
 					$v="section	$line";
 					if ($line=~/\.([px])data/) {
 					    $v.=" rdata align=";
 					    $v.=$1 eq "p"? 4 : 8;
 					} elsif ($line=~/\.CRT\$/i) {
 					    $v.=" rdata align=8";
 					}
 				    } else {
 					$v="$current_segment\tENDS\n" if ($current_segment);
 					$v.="$line\tSEGMENT";
 					if ($line=~/\.([px])data/) {
 					    $v.=" READONLY";
 					    $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
 					} elsif ($line=~/\.CRT\$/i) {
 					    $v.=" READONLY ";
 					    $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
 					}
 				    }
 				    $current_segment = $line;
 				    $self->{value} = $v;
 				    last;
 				  };
 		/\.extern/  && do { $self->{value}  = "EXTERN\t".$line;
 				    $self->{value} .= ":NEAR" if ($masm);
 				    last;
 				  };
 		/\.globl|.global/
 			    && do { $self->{value}  = $masm?"PUBLIC":"global";
 				    $self->{value} .= "\t".$line;
 				    last;
 				  };
 		/\.size/    && do { if (defined($current_function)) {
 					undef $self->{value};
 					if ($current_function->{abi} eq "svr4") {
 					    $self->{value}="${decor}SEH_end_$current_function->{name}:";
 					    $self->{value}.=":\n" if($masm);
 					}
 					$self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
 					undef $current_function;
 				    }
 				    last;
 				  };
 		/\.align/   && do { $self->{value} = "ALIGN\t".$line; last; };
 		/\.(value|long|rva|quad)/
 			    && do { my $sz  = substr($1,0,1);
 				    my @arr = split(/,\s*/,$line);
 				    my $last = pop(@arr);
 				    my $conv = sub  {	my $var=shift;
 							$var=~s/^(0b[0-1]+)/oct($1)/eig;
 							$var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
 							if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
 							{ $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
 							$var;
 						    };  
 
 				    $sz =~ tr/bvlrq/BWDDQ/;
 				    $self->{value} = "\tD$sz\t";
 				    for (@arr) { $self->{value} .= &$conv($_).","; }
 				    $self->{value} .= &$conv($last);
 				    last;
 				  };
 		/\.byte/    && do { my @str=split(/,\s*/,$line);
 				    map(s/(0b[0-1]+)/oct($1)/eig,@str);
 				    map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);	
 				    while ($#str>15) {
 					$self->{value}.="DB\t"
 						.join(",",@str[0..15])."\n";
 					foreach (0..15) { shift @str; }
 				    }
 				    $self->{value}.="DB\t"
 						.join(",",@str) if (@str);
 				    last;
 				  };
 		/\.comm/    && do { my @str=split(/,\s*/,$line);
 				    my $v=undef;
 				    if ($nasm) {
 					$v.="common	$prefix@str[0] @str[1]";
 				    } else {
 					$v="$current_segment\tENDS\n" if ($current_segment);
 					$current_segment = "_DATA";
 					$v.="$current_segment\tSEGMENT\n";
 					$v.="COMM	@str[0]:DWORD:".@str[1]/4;
 				    }
 				    $self->{value} = $v;
 				    last;
 				  };
 	    }
 	    $line = "";
 	}
 
 	$ret;
     }
     sub out {
 	my $self = shift;
 	$self->{value};
     }
 }
 
 sub rex {
  local *opcode=shift;
  my ($dst,$src,$rex)=@_;
 
    $rex|=0x04 if($dst>=8);
    $rex|=0x01 if($src>=8);
    push @opcode,($rex|0x40) if ($rex);
 }
 
 # older gas and ml64 don't handle SSE>2 instructions
 my %regrm = (	"%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
 		"%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7	);
 
 my $movq = sub {	# elderly gas can't handle inter-register movq
   my $arg = shift;
   my @opcode=(0x66);
     if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
 	my ($src,$dst)=($1,$2);
 	if ($dst !~ /[0-9]+/)	{ $dst = $regrm{"%e$dst"}; }
 	rex(\@opcode,$src,$dst,0x8);
 	push @opcode,0x0f,0x7e;
 	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
 	@opcode;
     } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
 	my ($src,$dst)=($2,$1);
 	if ($dst !~ /[0-9]+/)	{ $dst = $regrm{"%e$dst"}; }
 	rex(\@opcode,$src,$dst,0x8);
 	push @opcode,0x0f,0x6e;
 	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
 	@opcode;
     } else {
 	();
     }
 };
 
 my $pextrd = sub {
     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
       my @opcode=(0x66);
 	$imm=$1;
 	$src=$2;
 	$dst=$3;
 	if ($dst =~ /%r([0-9]+)d/)	{ $dst = $1; }
 	elsif ($dst =~ /%e/)		{ $dst = $regrm{$dst}; }
 	rex(\@opcode,$src,$dst);
 	push @opcode,0x0f,0x3a,0x16;
 	push @opcode,0xc0|(($src&7)<<3)|($dst&7);	# ModR/M
 	push @opcode,$imm;
 	@opcode;
     } else {
 	();
     }
 };
 
 my $pinsrd = sub {
     if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x66);
 	$imm=$1;
 	$src=$2;
 	$dst=$3;
 	if ($src =~ /%r([0-9]+)/)	{ $src = $1; }
 	elsif ($src =~ /%e/)		{ $src = $regrm{$src}; }
 	rex(\@opcode,$dst,$src);
 	push @opcode,0x0f,0x3a,0x22;
 	push @opcode,0xc0|(($dst&7)<<3)|($src&7);	# ModR/M
 	push @opcode,$imm;
 	@opcode;
     } else {
 	();
     }
 };
 
 my $pshufb = sub {
     if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x66);
 	rex(\@opcode,$2,$1);
 	push @opcode,0x0f,0x38,0x00;
 	push @opcode,0xc0|($1&7)|(($2&7)<<3);		# ModR/M
 	@opcode;
     } else {
 	();
     }
 };
 
 my $palignr = sub {
     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x66);
 	rex(\@opcode,$3,$2);
 	push @opcode,0x0f,0x3a,0x0f;
 	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
 	push @opcode,$1;
 	@opcode;
     } else {
 	();
     }
 };
 
 my $pclmulqdq = sub {
     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x66);
 	rex(\@opcode,$3,$2);
 	push @opcode,0x0f,0x3a,0x44;
 	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
 	my $c=$1;
 	push @opcode,$c=~/^0/?oct($c):$c;
 	@opcode;
     } else {
 	();
     }
 };
 
 my $rdrand = sub {
     if (shift =~ /%[er](\w+)/) {
       my @opcode=();
       my $dst=$1;
 	if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
 	rex(\@opcode,0,$1,8);
 	push @opcode,0x0f,0xc7,0xf0|($dst&7);
 	@opcode;
     } else {
 	();
     }
 };
 
 my $rdseed = sub {
     if (shift =~ /%[er](\w+)/) {
       my @opcode=();
       my $dst=$1;
 	if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
 	rex(\@opcode,0,$1,8);
 	push @opcode,0x0f,0xc7,0xf8|($dst&7);
 	@opcode;
     } else {
 	();
     }
 };
 
 sub rxb {
  local *opcode=shift;
  my ($dst,$src1,$src2,$rxb)=@_;
 
    $rxb|=0x7<<5;
    $rxb&=~(0x04<<5) if($dst>=8);
    $rxb&=~(0x01<<5) if($src1>=8);
    $rxb&=~(0x02<<5) if($src2>=8);
    push @opcode,$rxb;
 }
 
 my $vprotd = sub {
     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x8f);
 	rxb(\@opcode,$3,$2,-1,0x08);
 	push @opcode,0x78,0xc2;
 	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
 	my $c=$1;
 	push @opcode,$c=~/^0/?oct($c):$c;
 	@opcode;
     } else {
 	();
     }
 };
 
 my $vprotq = sub {
     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
       my @opcode=(0x8f);
 	rxb(\@opcode,$3,$2,-1,0x08);
 	push @opcode,0x78,0xc3;
 	push @opcode,0xc0|($2&7)|(($3&7)<<3);		# ModR/M
 	my $c=$1;
 	push @opcode,$c=~/^0/?oct($c):$c;
 	@opcode;
     } else {
 	();
     }
 };
 
 if ($nasm) {
     print <<___;
 default	rel
 %define XMMWORD
 %define YMMWORD
 %define ZMMWORD
 ___
 } elsif ($masm) {
     print <<___;
 OPTION	DOTNAME
 ___
 }
 while($line=<>) {
 
     chomp($line);
 
     $line =~ s|[#!].*$||;	# get rid of asm-style comments...
     $line =~ s|/\*.*\*/||;	# ... and C-style comments...
     $line =~ s|^\s+||;		# ... and skip white spaces in beginning
     $line =~ s|\s+$||;		# ... and at the end
 
     undef $label;
     undef $opcode;
     undef @args;
 
     if ($label=label->re(\$line))	{ print $label->out(); }
 
     if (directive->re(\$line)) {
 	printf "%s",directive->out();
     } elsif ($opcode=opcode->re(\$line)) {
 	my $asm = eval("\$".$opcode->mnemonic());
 	undef @bytes;
 	
 	if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) {
 	    print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
 	    next;
 	}
 
 	ARGUMENT: while (1) {
 	my $arg;
 
 	if ($arg=register->re(\$line))	{ opcode->size($arg->size()); }
 	elsif ($arg=const->re(\$line))	{ }
 	elsif ($arg=ea->re(\$line))	{ }
 	elsif ($arg=expr->re(\$line))	{ }
 	else				{ last ARGUMENT; }
 
 	push @args,$arg;
 
 	last ARGUMENT if ($line !~ /^,/);
 
 	$line =~ s/^,\s*//;
 	} # ARGUMENT:
 
 	if ($#args>=0) {
 	    my $insn;
 	    my $sz=opcode->size();
 
 	    if ($gas) {
 		$insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
 		@args = map($_->out($sz),@args);
 		printf "\t%s\t%s",$insn,join(",",@args);
 	    } else {
 		$insn = $opcode->out();
 		foreach (@args) {
 		    my $arg = $_->out();
 		    # $insn.=$sz compensates for movq, pinsrw, ...
 		    if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
 		    if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
 		    if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
 		    if ($arg =~ /^mm[0-9]+$/)  { $insn.=$sz; $sz="q" if(!$sz); last; }
 		}
 		@args = reverse(@args);
 		undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
 		printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
 	    }
 	} else {
 	    printf "\t%s",$opcode->out();
 	}
     }
 
     print $line,"\n";
 }
 
 print "\n$current_segment\tENDS\n"	if ($current_segment && $masm);
 print "END\n"				if ($masm);
 
 close STDOUT;
 
 #################################################
 # Cross-reference x86_64 ABI "card"
 #
 # 		Unix		Win64
 # %rax		*		*
 # %rbx		-		-
 # %rcx		#4		#1
 # %rdx		#3		#2
 # %rsi		#2		-
 # %rdi		#1		-
 # %rbp		-		-
 # %rsp		-		-
 # %r8		#5		#3
 # %r9		#6		#4
 # %r10		*		*
 # %r11		*		*
 # %r12		-		-
 # %r13		-		-
 # %r14		-		-
 # %r15		-		-
 # 
 # (*)	volatile register
 # (-)	preserved by callee
 # (#)	Nth argument, volatile
 #
 # In Unix terms top of stack is argument transfer area for arguments
 # which could not be accomodated in registers. Or in other words 7th
 # [integer] argument resides at 8(%rsp) upon function entry point.
 # 128 bytes above %rsp constitute a "red zone" which is not touched
 # by signal handlers and can be used as temporal storage without
 # allocating a frame.
 #
 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
 # which belongs to/can be overwritten by callee. N is the number of
 # arguments passed to callee, *but* not less than 4! This means that
 # upon function entry point 5th argument resides at 40(%rsp), as well
 # as that 32 bytes from 8(%rsp) can always be used as temporal
 # storage [without allocating a frame]. One can actually argue that
 # one can assume a "red zone" above stack pointer under Win64 as well.
 # Point is that at apparently no occasion Windows kernel would alter
 # the area above user stack pointer in true asynchronous manner...
 #
 # All the above means that if assembler programmer adheres to Unix
 # register and stack layout, but disregards the "red zone" existense,
 # it's possible to use following prologue and epilogue to "gear" from
 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
 #
 # omnipotent_function:
 # ifdef WIN64
 #	movq	%rdi,8(%rsp)
 #	movq	%rsi,16(%rsp)
 #	movq	%rcx,%rdi	; if 1st argument is actually present
 #	movq	%rdx,%rsi	; if 2nd argument is actually ...
 #	movq	%r8,%rdx	; if 3rd argument is ...
 #	movq	%r9,%rcx	; if 4th argument ...
 #	movq	40(%rsp),%r8	; if 5th ...
 #	movq	48(%rsp),%r9	; if 6th ...
 # endif
 #	...
 # ifdef WIN64
 #	movq	8(%rsp),%rdi
 #	movq	16(%rsp),%rsi
 # endif
 #	ret
 #
 #################################################
 # Win64 SEH, Structured Exception Handling.
 #
 # Unlike on Unix systems(*) lack of Win64 stack unwinding information
 # has undesired side-effect at run-time: if an exception is raised in
 # assembler subroutine such as those in question (basically we're
 # referring to segmentation violations caused by malformed input
 # parameters), the application is briskly terminated without invoking
 # any exception handlers, most notably without generating memory dump
 # or any user notification whatsoever. This poses a problem. It's
 # possible to address it by registering custom language-specific
 # handler that would restore processor context to the state at
 # subroutine entry point and return "exception is not handled, keep
 # unwinding" code. Writing such handler can be a challenge... But it's
 # doable, though requires certain coding convention. Consider following
 # snippet:
 #
 # .type	function,@function
 # function:
 #	movq	%rsp,%rax	# copy rsp to volatile register
 #	pushq	%r15		# save non-volatile registers
 #	pushq	%rbx
 #	pushq	%rbp
 #	movq	%rsp,%r11
 #	subq	%rdi,%r11	# prepare [variable] stack frame
 #	andq	$-64,%r11
 #	movq	%rax,0(%r11)	# check for exceptions
 #	movq	%r11,%rsp	# allocate [variable] stack frame
 #	movq	%rax,0(%rsp)	# save original rsp value
 # magic_point:
 #	...
 #	movq	0(%rsp),%rcx	# pull original rsp value
 #	movq	-24(%rcx),%rbp	# restore non-volatile registers
 #	movq	-16(%rcx),%rbx
 #	movq	-8(%rcx),%r15
 #	movq	%rcx,%rsp	# restore original rsp
 #	ret
 # .size function,.-function
 #
 # The key is that up to magic_point copy of original rsp value remains
 # in chosen volatile register and no non-volatile register, except for
 # rsp, is modified. While past magic_point rsp remains constant till
 # the very end of the function. In this case custom language-specific
 # exception handler would look like this:
 #
 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
 #		CONTEXT *context,DISPATCHER_CONTEXT *disp)
 # {	ULONG64 *rsp = (ULONG64 *)context->Rax;
 #	if (context->Rip >= magic_point)
 #	{   rsp = ((ULONG64 **)context->Rsp)[0];
 #	    context->Rbp = rsp[-3];
 #	    context->Rbx = rsp[-2];
 #	    context->R15 = rsp[-1];
 #	}
 #	context->Rsp = (ULONG64)rsp;
 #	context->Rdi = rsp[1];
 #	context->Rsi = rsp[2];
 #
 #	memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
 #	RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
 #		dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
 #		&disp->HandlerData,&disp->EstablisherFrame,NULL);
 #	return ExceptionContinueSearch;
 # }
 #
 # It's appropriate to implement this handler in assembler, directly in
 # function's module. In order to do that one has to know members'
 # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
 # values. Here they are:
 #
 #	CONTEXT.Rax				120
 #	CONTEXT.Rcx				128
 #	CONTEXT.Rdx				136
 #	CONTEXT.Rbx				144
 #	CONTEXT.Rsp				152
 #	CONTEXT.Rbp				160
 #	CONTEXT.Rsi				168
 #	CONTEXT.Rdi				176
 #	CONTEXT.R8				184
 #	CONTEXT.R9				192
 #	CONTEXT.R10				200
 #	CONTEXT.R11				208
 #	CONTEXT.R12				216
 #	CONTEXT.R13				224
 #	CONTEXT.R14				232
 #	CONTEXT.R15				240
 #	CONTEXT.Rip				248
 #	CONTEXT.Xmm6				512
 #	sizeof(CONTEXT)				1232
 #	DISPATCHER_CONTEXT.ControlPc		0
 #	DISPATCHER_CONTEXT.ImageBase		8
 #	DISPATCHER_CONTEXT.FunctionEntry	16
 #	DISPATCHER_CONTEXT.EstablisherFrame	24
 #	DISPATCHER_CONTEXT.TargetIp		32
 #	DISPATCHER_CONTEXT.ContextRecord	40
 #	DISPATCHER_CONTEXT.LanguageHandler	48
 #	DISPATCHER_CONTEXT.HandlerData		56
 #	UNW_FLAG_NHANDLER			0
 #	ExceptionContinueSearch			1
 #
 # In order to tie the handler to the function one has to compose
 # couple of structures: one for .xdata segment and one for .pdata.
 #
 # UNWIND_INFO structure for .xdata segment would be
 #
 # function_unwind_info:
 #	.byte	9,0,0,0
 #	.rva	handler
 #
 # This structure designates exception handler for a function with
 # zero-length prologue, no stack frame or frame register.
 #
 # To facilitate composing of .pdata structures, auto-generated "gear"
 # prologue copies rsp value to rax and denotes next instruction with
 # .LSEH_begin_{function_name} label. This essentially defines the SEH
 # styling rule mentioned in the beginning. Position of this label is
 # chosen in such manner that possible exceptions raised in the "gear"
 # prologue would be accounted to caller and unwound from latter's frame.
 # End of function is marked with respective .LSEH_end_{function_name}
 # label. To summarize, .pdata segment would contain
 #
 #	.rva	.LSEH_begin_function
 #	.rva	.LSEH_end_function
 #	.rva	function_unwind_info
 #
 # Reference to functon_unwind_info from .xdata segment is the anchor.
 # In case you wonder why references are 32-bit .rvas and not 64-bit
 # .quads. References put into these two segments are required to be
 # *relative* to the base address of the current binary module, a.k.a.
 # image base. No Win64 module, be it .exe or .dll, can be larger than
 # 2GB and thus such relative references can be and are accommodated in
 # 32 bits.
 #
 # Having reviewed the example function code, one can argue that "movq
 # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
 # rax would contain an undefined value. If this "offends" you, use
 # another register and refrain from modifying rax till magic_point is
 # reached, i.e. as if it was a non-volatile register. If more registers
 # are required prior [variable] frame setup is completed, note that
 # nobody says that you can have only one "magic point." You can
 # "liberate" non-volatile registers by denoting last stack off-load
 # instruction and reflecting it in finer grade unwind logic in handler.
 # After all, isn't it why it's called *language-specific* handler...
 #
 # Attentive reader can notice that exceptions would be mishandled in
 # auto-generated "gear" epilogue. Well, exception effectively can't
 # occur there, because if memory area used by it was subject to
 # segmentation violation, then it would be raised upon call to the
 # function (and as already mentioned be accounted to caller, which is
 # not a problem). If you're still not comfortable, then define tail
 # "magic point" just prior ret instruction and have handler treat it...
 #
 # (*)	Note that we're talking about run-time, not debug-time. Lack of
 #	unwind information makes debugging hard on both Windows and
 #	Unix. "Unlike" referes to the fact that on Unix signal handler
 #	will always be invoked, core dumped and appropriate exit code
 #	returned to parent (for user notification).
Index: vendor-crypto/openssl/dist/crypto/pkcs7/pk7_smime.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/pkcs7/pk7_smime.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/pkcs7/pk7_smime.c	(revision 296273)
@@ -1,573 +1,590 @@
 /* pk7_smime.c */
 /*
  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project.
  */
 /* ====================================================================
  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 
 /* Simple PKCS#7 processing functions */
 
 #include 
 #include "cryptlib.h"
 #include 
 #include 
 
 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
 
 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
                   BIO *data, int flags)
 {
     PKCS7 *p7;
     int i;
 
     if (!(p7 = PKCS7_new())) {
         PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
 
     if (!PKCS7_set_type(p7, NID_pkcs7_signed))
         goto err;
 
     if (!PKCS7_content_new(p7, NID_pkcs7_data))
         goto err;
 
     if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
         PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
         goto err;
     }
 
     if (!(flags & PKCS7_NOCERTS)) {
         for (i = 0; i < sk_X509_num(certs); i++) {
             if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
                 goto err;
         }
     }
 
     if (flags & PKCS7_DETACHED)
         PKCS7_set_detached(p7, 1);
 
     if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
         return p7;
 
     if (PKCS7_final(p7, data, flags))
         return p7;
 
  err:
     PKCS7_free(p7);
     return NULL;
 }
 
 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
 {
     BIO *p7bio;
     int ret = 0;
     if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
         PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE);
         return 0;
     }
 
     SMIME_crlf_copy(data, p7bio, flags);
 
     (void)BIO_flush(p7bio);
 
     if (!PKCS7_dataFinal(p7, p7bio)) {
         PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN);
         goto err;
     }
 
     ret = 1;
 
  err:
     BIO_free_all(p7bio);
 
     return ret;
 
 }
 
 /* Check to see if a cipher exists and if so add S/MIME capabilities */
 
 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
 {
     if (EVP_get_cipherbynid(nid))
         return PKCS7_simple_smimecap(sk, nid, arg);
     return 1;
 }
 
 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
 {
     if (EVP_get_digestbynid(nid))
         return PKCS7_simple_smimecap(sk, nid, arg);
     return 1;
 }
 
 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
                                          EVP_PKEY *pkey, const EVP_MD *md,
                                          int flags)
 {
     PKCS7_SIGNER_INFO *si = NULL;
     STACK_OF(X509_ALGOR) *smcap = NULL;
     if (!X509_check_private_key(signcert, pkey)) {
         PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
         return NULL;
     }
 
     if (!(si = PKCS7_add_signature(p7, signcert, pkey, md))) {
         PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
                  PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
         return NULL;
     }
 
     if (!(flags & PKCS7_NOCERTS)) {
         if (!PKCS7_add_certificate(p7, signcert))
             goto err;
     }
 
     if (!(flags & PKCS7_NOATTR)) {
         if (!PKCS7_add_attrib_content_type(si, NULL))
             goto err;
         /* Add SMIMECapabilities */
         if (!(flags & PKCS7_NOSMIMECAP)) {
             if (!(smcap = sk_X509_ALGOR_new_null())) {
                 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
                 goto err;
             }
             if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
                 || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
                 || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
                 || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
                 || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
                 || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
                 || !add_cipher_smcap(smcap, NID_des_cbc, -1)
                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
                 || !PKCS7_add_attrib_smimecap(si, smcap))
                 goto err;
             sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
             smcap = NULL;
         }
         if (flags & PKCS7_REUSE_DIGEST) {
             if (!pkcs7_copy_existing_digest(p7, si))
                 goto err;
             if (!(flags & PKCS7_PARTIAL) && !PKCS7_SIGNER_INFO_sign(si))
                 goto err;
         }
     }
     return si;
  err:
     if (smcap)
         sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
     return NULL;
 }
 
 /*
  * Search for a digest matching SignerInfo digest type and if found copy
  * across.
  */
 
 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
 {
     int i;
     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
     PKCS7_SIGNER_INFO *sitmp;
     ASN1_OCTET_STRING *osdig = NULL;
     sinfos = PKCS7_get_signer_info(p7);
     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
         sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
         if (si == sitmp)
             break;
         if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
             continue;
         if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
             osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
             break;
         }
 
     }
 
     if (osdig)
         return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
 
     PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
              PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
     return 0;
 }
 
 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
                  BIO *indata, BIO *out, int flags)
 {
     STACK_OF(X509) *signers;
     X509 *signer;
     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
     PKCS7_SIGNER_INFO *si;
     X509_STORE_CTX cert_ctx;
     char buf[4096];
     int i, j = 0, k, ret = 0;
     BIO *p7bio = NULL;
     BIO *tmpin = NULL, *tmpout = NULL;
 
     if (!p7) {
         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER);
         return 0;
     }
 
     if (!PKCS7_type_is_signed(p7)) {
         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE);
         return 0;
     }
 
     /* Check for no data and no content: no data to verify signature */
     if (PKCS7_get_detached(p7) && !indata) {
         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT);
         return 0;
     }
+#if 0
+    /*
+     * NB: this test commented out because some versions of Netscape
+     * illegally include zero length content when signing data. Also
+     * Microsoft Authenticode includes a SpcIndirectDataContent data
+     * structure which describes the content to be protected by the
+     * signature, rather than directly embedding that content. So
+     * Authenticode implementations are also expected to use
+     * PKCS7_verify() with explicit external data, on non-detached
+     * PKCS#7 signatures.
+     *
+     * In OpenSSL 1.1 a new flag PKCS7_NO_DUAL_CONTENT has been
+     * introduced to disable this sanity check. For the 1.0.2 branch
+     * this change is not acceptable, so the check remains completely
+     * commented out (as it has been for a long time).
+     */
 
     /* Check for data and content: two sets of data */
     if (!PKCS7_get_detached(p7) && indata) {
         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT);
         return 0;
     }
+#endif
 
     sinfos = PKCS7_get_signer_info(p7);
 
     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
         PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA);
         return 0;
     }
 
     signers = PKCS7_get0_signers(p7, certs, flags);
     if (!signers)
         return 0;
 
     /* Now verify the certificates */
 
     if (!(flags & PKCS7_NOVERIFY))
         for (k = 0; k < sk_X509_num(signers); k++) {
             signer = sk_X509_value(signers, k);
             if (!(flags & PKCS7_NOCHAIN)) {
                 if (!X509_STORE_CTX_init(&cert_ctx, store, signer,
                                          p7->d.sign->cert)) {
                     PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
                     goto err;
                 }
                 X509_STORE_CTX_set_default(&cert_ctx, "smime_sign");
             } else if (!X509_STORE_CTX_init(&cert_ctx, store, signer, NULL)) {
                 PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
                 goto err;
             }
             if (!(flags & PKCS7_NOCRL))
                 X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
             i = X509_verify_cert(&cert_ctx);
             if (i <= 0)
                 j = X509_STORE_CTX_get_error(&cert_ctx);
             X509_STORE_CTX_cleanup(&cert_ctx);
             if (i <= 0) {
                 PKCS7err(PKCS7_F_PKCS7_VERIFY,
                          PKCS7_R_CERTIFICATE_VERIFY_ERROR);
                 ERR_add_error_data(2, "Verify error:",
                                    X509_verify_cert_error_string(j));
                 goto err;
             }
             /* Check for revocation status here */
         }
 
     /*
      * Performance optimization: if the content is a memory BIO then store
      * its contents in a temporary read only memory BIO. This avoids
      * potentially large numbers of slow copies of data which will occur when
      * reading from a read write memory BIO when signatures are calculated.
      */
 
     if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
         char *ptr;
         long len;
         len = BIO_get_mem_data(indata, &ptr);
         tmpin = BIO_new_mem_buf(ptr, len);
         if (tmpin == NULL) {
             PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
             goto err;
         }
     } else
         tmpin = indata;
 
     if (!(p7bio = PKCS7_dataInit(p7, tmpin)))
         goto err;
 
     if (flags & PKCS7_TEXT) {
         if (!(tmpout = BIO_new(BIO_s_mem()))) {
             PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
             goto err;
         }
         BIO_set_mem_eof_return(tmpout, 0);
     } else
         tmpout = out;
 
     /* We now have to 'read' from p7bio to calculate digests etc. */
     for (;;) {
         i = BIO_read(p7bio, buf, sizeof(buf));
         if (i <= 0)
             break;
         if (tmpout)
             BIO_write(tmpout, buf, i);
     }
 
     if (flags & PKCS7_TEXT) {
         if (!SMIME_text(tmpout, out)) {
             PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR);
             BIO_free(tmpout);
             goto err;
         }
         BIO_free(tmpout);
     }
 
     /* Now Verify All Signatures */
     if (!(flags & PKCS7_NOSIGS))
         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
             signer = sk_X509_value(signers, i);
             j = PKCS7_signatureVerify(p7bio, p7, si, signer);
             if (j <= 0) {
                 PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE);
                 goto err;
             }
         }
 
     ret = 1;
 
  err:
     if (tmpin == indata) {
         if (indata)
             BIO_pop(p7bio);
     }
     BIO_free_all(p7bio);
     sk_X509_free(signers);
     return ret;
 }
 
 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
                                    int flags)
 {
     STACK_OF(X509) *signers;
     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
     PKCS7_SIGNER_INFO *si;
     PKCS7_ISSUER_AND_SERIAL *ias;
     X509 *signer;
     int i;
 
     if (!p7) {
         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
         return NULL;
     }
 
     if (!PKCS7_type_is_signed(p7)) {
         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
         return NULL;
     }
 
     /* Collect all the signers together */
 
     sinfos = PKCS7_get_signer_info(p7);
 
     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
         return 0;
     }
 
     if (!(signers = sk_X509_new_null())) {
         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
 
     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
         si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
         ias = si->issuer_and_serial;
         signer = NULL;
         /* If any certificates passed they take priority */
         if (certs)
             signer = X509_find_by_issuer_and_serial(certs,
                                                     ias->issuer, ias->serial);
         if (!signer && !(flags & PKCS7_NOINTERN)
             && p7->d.sign->cert)
             signer =
                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
                                                ias->issuer, ias->serial);
         if (!signer) {
             PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
                      PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
             sk_X509_free(signers);
             return 0;
         }
 
         if (!sk_X509_push(signers, signer)) {
             sk_X509_free(signers);
             return NULL;
         }
     }
     return signers;
 }
 
 /* Build a complete PKCS#7 enveloped data */
 
 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
                      int flags)
 {
     PKCS7 *p7;
     BIO *p7bio = NULL;
     int i;
     X509 *x509;
     if (!(p7 = PKCS7_new())) {
         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
 
     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
         goto err;
     if (!PKCS7_set_cipher(p7, cipher)) {
         PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER);
         goto err;
     }
 
     for (i = 0; i < sk_X509_num(certs); i++) {
         x509 = sk_X509_value(certs, i);
         if (!PKCS7_add_recipient(p7, x509)) {
             PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT);
             goto err;
         }
     }
 
     if (flags & PKCS7_STREAM)
         return p7;
 
     if (PKCS7_final(p7, in, flags))
         return p7;
 
  err:
 
     BIO_free_all(p7bio);
     PKCS7_free(p7);
     return NULL;
 
 }
 
 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
 {
     BIO *tmpmem;
     int ret, i;
     char buf[4096];
 
     if (!p7) {
         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
         return 0;
     }
 
     if (!PKCS7_type_is_enveloped(p7)) {
         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
         return 0;
     }
 
     if (cert && !X509_check_private_key(cert, pkey)) {
         PKCS7err(PKCS7_F_PKCS7_DECRYPT,
                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
         return 0;
     }
 
     if (!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) {
         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
         return 0;
     }
 
     if (flags & PKCS7_TEXT) {
         BIO *tmpbuf, *bread;
         /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
         if (!(tmpbuf = BIO_new(BIO_f_buffer()))) {
             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
             BIO_free_all(tmpmem);
             return 0;
         }
         if (!(bread = BIO_push(tmpbuf, tmpmem))) {
             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
             BIO_free_all(tmpbuf);
             BIO_free_all(tmpmem);
             return 0;
         }
         ret = SMIME_text(bread, data);
         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
             if (!BIO_get_cipher_status(tmpmem))
                 ret = 0;
         }
         BIO_free_all(bread);
         return ret;
     } else {
         for (;;) {
             i = BIO_read(tmpmem, buf, sizeof(buf));
             if (i <= 0) {
                 ret = 1;
                 if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
                     if (!BIO_get_cipher_status(tmpmem))
                         ret = 0;
                 }
 
                 break;
             }
             if (BIO_write(data, buf, i) != i) {
                 ret = 0;
                 break;
             }
         }
         BIO_free_all(tmpmem);
         return ret;
     }
 }
Index: vendor-crypto/openssl/dist/crypto/rsa/rsa_sign.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/rsa/rsa_sign.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/rsa/rsa_sign.c	(revision 296273)
@@ -1,301 +1,301 @@
 /* crypto/rsa/rsa_sign.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include "cryptlib.h"
 #include 
 #include 
 #include 
 #include 
 #include "rsa_locl.h"
 
 /* Size of an SSL signature: MD5+SHA1 */
 #define SSL_SIG_LENGTH  36
 
 int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
              unsigned char *sigret, unsigned int *siglen, RSA *rsa)
 {
     X509_SIG sig;
     ASN1_TYPE parameter;
     int i, j, ret = 1;
     unsigned char *p, *tmps = NULL;
     const unsigned char *s = NULL;
     X509_ALGOR algor;
     ASN1_OCTET_STRING digest;
 #ifdef OPENSSL_FIPS
     if (FIPS_mode() && !(rsa->meth->flags & RSA_FLAG_FIPS_METHOD)
         && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) {
         RSAerr(RSA_F_RSA_SIGN, RSA_R_NON_FIPS_RSA_METHOD);
         return 0;
     }
 #endif
-    if (rsa->meth->rsa_sign) {
+    if ((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign) {
         return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
     }
     /* Special case: SSL signature, just check the length */
     if (type == NID_md5_sha1) {
         if (m_len != SSL_SIG_LENGTH) {
             RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);
             return (0);
         }
         i = SSL_SIG_LENGTH;
         s = m;
     } else {
         sig.algor = &algor;
         sig.algor->algorithm = OBJ_nid2obj(type);
         if (sig.algor->algorithm == NULL) {
             RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE);
             return (0);
         }
         if (sig.algor->algorithm->length == 0) {
             RSAerr(RSA_F_RSA_SIGN,
                    RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
             return (0);
         }
         parameter.type = V_ASN1_NULL;
         parameter.value.ptr = NULL;
         sig.algor->parameter = ¶meter;
 
         sig.digest = &digest;
         sig.digest->data = (unsigned char *)m; /* TMP UGLY CAST */
         sig.digest->length = m_len;
 
         i = i2d_X509_SIG(&sig, NULL);
     }
     j = RSA_size(rsa);
     if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
         RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
         return (0);
     }
     if (type != NID_md5_sha1) {
         tmps = (unsigned char *)OPENSSL_malloc((unsigned int)j + 1);
         if (tmps == NULL) {
             RSAerr(RSA_F_RSA_SIGN, ERR_R_MALLOC_FAILURE);
             return (0);
         }
         p = tmps;
         i2d_X509_SIG(&sig, &p);
         s = tmps;
     }
     i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
     if (i <= 0)
         ret = 0;
     else
         *siglen = i;
 
     if (type != NID_md5_sha1) {
         OPENSSL_cleanse(tmps, (unsigned int)j + 1);
         OPENSSL_free(tmps);
     }
     return (ret);
 }
 
 /*
  * Check DigestInfo structure does not contain extraneous data by reencoding
  * using DER and checking encoding against original.
  */
 static int rsa_check_digestinfo(X509_SIG *sig, const unsigned char *dinfo,
                                 int dinfolen)
 {
     unsigned char *der = NULL;
     int derlen;
     int ret = 0;
     derlen = i2d_X509_SIG(sig, &der);
     if (derlen <= 0)
         return 0;
     if (derlen == dinfolen && !memcmp(dinfo, der, derlen))
         ret = 1;
     OPENSSL_cleanse(der, derlen);
     OPENSSL_free(der);
     return ret;
 }
 
 int int_rsa_verify(int dtype, const unsigned char *m,
                    unsigned int m_len,
                    unsigned char *rm, size_t *prm_len,
                    const unsigned char *sigbuf, size_t siglen, RSA *rsa)
 {
     int i, ret = 0, sigtype;
     unsigned char *s;
     X509_SIG *sig = NULL;
 
 #ifdef OPENSSL_FIPS
     if (FIPS_mode() && !(rsa->meth->flags & RSA_FLAG_FIPS_METHOD)
         && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) {
         RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_NON_FIPS_RSA_METHOD);
         return 0;
     }
 #endif
 
     if (siglen != (unsigned int)RSA_size(rsa)) {
         RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH);
         return (0);
     }
 
     if ((dtype == NID_md5_sha1) && rm) {
         i = RSA_public_decrypt((int)siglen,
                                sigbuf, rm, rsa, RSA_PKCS1_PADDING);
         if (i <= 0)
             return 0;
         *prm_len = i;
         return 1;
     }
 
     s = (unsigned char *)OPENSSL_malloc((unsigned int)siglen);
     if (s == NULL) {
         RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
         goto err;
     }
     if ((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH)) {
         RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
         goto err;
     }
     i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
 
     if (i <= 0)
         goto err;
     /*
      * Oddball MDC2 case: signature can be OCTET STRING. check for correct
      * tag and length octets.
      */
     if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10) {
         if (rm) {
             memcpy(rm, s + 2, 16);
             *prm_len = 16;
             ret = 1;
         } else if (memcmp(m, s + 2, 16)) {
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
         } else {
             ret = 1;
         }
     } else if (dtype == NID_md5_sha1) {
         /* Special case: SSL signature */
         if ((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
         else
             ret = 1;
     } else {
         const unsigned char *p = s;
         sig = d2i_X509_SIG(NULL, &p, (long)i);
 
         if (sig == NULL)
             goto err;
 
         /* Excess data can be used to create forgeries */
         if (p != s + i || !rsa_check_digestinfo(sig, s, i)) {
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
             goto err;
         }
 
         /*
          * Parameters to the signature algorithm can also be used to create
          * forgeries
          */
         if (sig->algor->parameter
             && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) {
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
             goto err;
         }
 
         sigtype = OBJ_obj2nid(sig->algor->algorithm);
 
 #ifdef RSA_DEBUG
         /* put a backward compatibility flag in EAY */
         fprintf(stderr, "in(%s) expect(%s)\n", OBJ_nid2ln(sigtype),
                 OBJ_nid2ln(dtype));
 #endif
         if (sigtype != dtype) {
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_ALGORITHM_MISMATCH);
             goto err;
         }
         if (rm) {
             const EVP_MD *md;
             md = EVP_get_digestbynid(dtype);
             if (md && (EVP_MD_size(md) != sig->digest->length))
                 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
             else {
                 memcpy(rm, sig->digest->data, sig->digest->length);
                 *prm_len = sig->digest->length;
                 ret = 1;
             }
         } else if (((unsigned int)sig->digest->length != m_len) ||
                    (memcmp(m, sig->digest->data, m_len) != 0)) {
             RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
         } else
             ret = 1;
     }
  err:
     if (sig != NULL)
         X509_SIG_free(sig);
     if (s != NULL) {
         OPENSSL_cleanse(s, (unsigned int)siglen);
         OPENSSL_free(s);
     }
     return (ret);
 }
 
 int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
                const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
 {
 
-    if (rsa->meth->rsa_verify) {
+    if ((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) {
         return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);
     }
 
     return int_rsa_verify(dtype, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
 }
Index: vendor-crypto/openssl/dist/crypto/srp/srp.h
===================================================================
--- vendor-crypto/openssl/dist/crypto/srp/srp.h	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/srp/srp.h	(revision 296273)
@@ -1,169 +1,179 @@
 /* crypto/srp/srp.h */
 /*
  * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
  * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
  * EdelKey project and contributed to the OpenSSL project 2004.
  */
 /* ====================================================================
  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 #ifndef __SRP_H__
 # define __SRP_H__
 
 # ifndef OPENSSL_NO_SRP
 
 #  include 
 #  include 
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
 
 #  include 
 #  include 
 #  include 
 
 typedef struct SRP_gN_cache_st {
     char *b64_bn;
     BIGNUM *bn;
 } SRP_gN_cache;
 
 
 DECLARE_STACK_OF(SRP_gN_cache)
 
 typedef struct SRP_user_pwd_st {
+    /* Owned by us. */
     char *id;
     BIGNUM *s;
     BIGNUM *v;
+    /* Not owned by us. */
     const BIGNUM *g;
     const BIGNUM *N;
+    /* Owned by us. */
     char *info;
 } SRP_user_pwd;
 
 DECLARE_STACK_OF(SRP_user_pwd)
 
+void SRP_user_pwd_free(SRP_user_pwd *user_pwd);
+
 typedef struct SRP_VBASE_st {
     STACK_OF(SRP_user_pwd) *users_pwd;
     STACK_OF(SRP_gN_cache) *gN_cache;
 /* to simulate a user */
     char *seed_key;
     BIGNUM *default_g;
     BIGNUM *default_N;
 } SRP_VBASE;
 
 /*
  * Structure interne pour retenir les couples N et g
  */
 typedef struct SRP_gN_st {
     char *id;
     BIGNUM *g;
     BIGNUM *N;
 } SRP_gN;
 
 DECLARE_STACK_OF(SRP_gN)
 
 SRP_VBASE *SRP_VBASE_new(char *seed_key);
 int SRP_VBASE_free(SRP_VBASE *vb);
 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file);
+
+/* This method ignores the configured seed and fails for an unknown user. */
 SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username);
+/* NOTE: unlike in SRP_VBASE_get_by_user, caller owns the returned pointer.*/
+SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username);
+
 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
                           char **verifier, const char *N, const char *g);
 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
                            BIGNUM **verifier, BIGNUM *N, BIGNUM *g);
 
 #  define SRP_NO_ERROR 0
 #  define SRP_ERR_VBASE_INCOMPLETE_FILE 1
 #  define SRP_ERR_VBASE_BN_LIB 2
 #  define SRP_ERR_OPEN_FILE 3
 #  define SRP_ERR_MEMORY 4
 
 #  define DB_srptype      0
 #  define DB_srpverifier  1
 #  define DB_srpsalt      2
 #  define DB_srpid        3
 #  define DB_srpgN        4
 #  define DB_srpinfo      5
 #  undef  DB_NUMBER
 #  define DB_NUMBER       6
 
 #  define DB_SRP_INDEX    'I'
 #  define DB_SRP_VALID    'V'
 #  define DB_SRP_REVOKED  'R'
 #  define DB_SRP_MODIF    'v'
 
 /* see srp.c */
 char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N);
 SRP_gN *SRP_get_default_gN(const char *id);
 
 /* server side .... */
 BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
                             BIGNUM *N);
 BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v);
 int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N);
 BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N);
 
 /* client side .... */
 BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass);
 BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g);
 BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
                             BIGNUM *a, BIGNUM *u);
 int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N);
 
 #  define SRP_MINIMAL_N 1024
 
 #ifdef  __cplusplus
 }
 #endif
 
 # endif
 #endif
Index: vendor-crypto/openssl/dist/crypto/srp/srp_vfy.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/srp/srp_vfy.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/srp/srp_vfy.c	(revision 296273)
@@ -1,658 +1,705 @@
 /* crypto/srp/srp_vfy.c */
 /*
  * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
  * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
  * EdelKey project and contributed to the OpenSSL project 2004.
  */
 /* ====================================================================
  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. All advertising materials mentioning features or use of this
  *    software must display the following acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  *
  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  *    endorse or promote products derived from this software without
  *    prior written permission. For written permission, please contact
  *    licensing@OpenSSL.org.
  *
  * 5. Products derived from this software may not be called "OpenSSL"
  *    nor may "OpenSSL" appear in their names without prior written
  *    permission of the OpenSSL Project.
  *
  * 6. Redistributions of any form whatsoever must retain the following
  *    acknowledgment:
  *    "This product includes software developed by the OpenSSL Project
  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  *
  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  * ====================================================================
  *
  * This product includes cryptographic software written by Eric Young
  * (eay@cryptsoft.com).  This product includes software written by Tim
  * Hudson (tjh@cryptsoft.com).
  *
  */
 #ifndef OPENSSL_NO_SRP
 # include "cryptlib.h"
 # include "srp_lcl.h"
 # include 
 # include 
 # include 
 # include 
 # include 
 
 # define SRP_RANDOM_SALT_LEN 20
 # define MAX_LEN 2500
 
 static char b64table[] =
     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
 
 /*
  * the following two conversion routines have been inspired by code from
  * Stanford
  */
 
 /*
  * Convert a base64 string into raw byte array representation.
  */
 static int t_fromb64(unsigned char *a, const char *src)
 {
     char *loc;
     int i, j;
     int size;
 
     while (*src && (*src == ' ' || *src == '\t' || *src == '\n'))
         ++src;
     size = strlen(src);
     i = 0;
     while (i < size) {
         loc = strchr(b64table, src[i]);
         if (loc == (char *)0)
             break;
         else
             a[i] = loc - b64table;
         ++i;
     }
     /* if nothing valid to process we have a zero length response */
     if (i == 0)
         return 0;
     size = i;
     i = size - 1;
     j = size;
     while (1) {
         a[j] = a[i];
         if (--i < 0)
             break;
         a[j] |= (a[i] & 3) << 6;
         --j;
         a[j] = (unsigned char)((a[i] & 0x3c) >> 2);
         if (--i < 0)
             break;
         a[j] |= (a[i] & 0xf) << 4;
         --j;
         a[j] = (unsigned char)((a[i] & 0x30) >> 4);
         if (--i < 0)
             break;
         a[j] |= (a[i] << 2);
 
         a[--j] = 0;
         if (--i < 0)
             break;
     }
     while (a[j] == 0 && j <= size)
         ++j;
     i = 0;
     while (j <= size)
         a[i++] = a[j++];
     return i;
 }
 
 /*
  * Convert a raw byte string into a null-terminated base64 ASCII string.
  */
 static char *t_tob64(char *dst, const unsigned char *src, int size)
 {
     int c, pos = size % 3;
     unsigned char b0 = 0, b1 = 0, b2 = 0, notleading = 0;
     char *olddst = dst;
 
     switch (pos) {
     case 1:
         b2 = src[0];
         break;
     case 2:
         b1 = src[0];
         b2 = src[1];
         break;
     }
 
     while (1) {
         c = (b0 & 0xfc) >> 2;
         if (notleading || c != 0) {
             *dst++ = b64table[c];
             notleading = 1;
         }
         c = ((b0 & 3) << 4) | ((b1 & 0xf0) >> 4);
         if (notleading || c != 0) {
             *dst++ = b64table[c];
             notleading = 1;
         }
         c = ((b1 & 0xf) << 2) | ((b2 & 0xc0) >> 6);
         if (notleading || c != 0) {
             *dst++ = b64table[c];
             notleading = 1;
         }
         c = b2 & 0x3f;
         if (notleading || c != 0) {
             *dst++ = b64table[c];
             notleading = 1;
         }
         if (pos >= size)
             break;
         else {
             b0 = src[pos++];
             b1 = src[pos++];
             b2 = src[pos++];
         }
     }
 
     *dst++ = '\0';
     return olddst;
 }
 
-static void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
+void SRP_user_pwd_free(SRP_user_pwd *user_pwd)
 {
     if (user_pwd == NULL)
         return;
     BN_free(user_pwd->s);
     BN_clear_free(user_pwd->v);
     OPENSSL_free(user_pwd->id);
     OPENSSL_free(user_pwd->info);
     OPENSSL_free(user_pwd);
 }
 
 static SRP_user_pwd *SRP_user_pwd_new()
 {
     SRP_user_pwd *ret = OPENSSL_malloc(sizeof(SRP_user_pwd));
     if (ret == NULL)
         return NULL;
     ret->N = NULL;
     ret->g = NULL;
     ret->s = NULL;
     ret->v = NULL;
     ret->id = NULL;
     ret->info = NULL;
     return ret;
 }
 
 static void SRP_user_pwd_set_gN(SRP_user_pwd *vinfo, const BIGNUM *g,
                                 const BIGNUM *N)
 {
     vinfo->N = N;
     vinfo->g = g;
 }
 
 static int SRP_user_pwd_set_ids(SRP_user_pwd *vinfo, const char *id,
                                 const char *info)
 {
     if (id != NULL && NULL == (vinfo->id = BUF_strdup(id)))
         return 0;
     return (info == NULL || NULL != (vinfo->info = BUF_strdup(info)));
 }
 
 static int SRP_user_pwd_set_sv(SRP_user_pwd *vinfo, const char *s,
                                const char *v)
 {
     unsigned char tmp[MAX_LEN];
     int len;
 
     if (strlen(s) > MAX_LEN || strlen(v) > MAX_LEN)
         return 0;
     len = t_fromb64(tmp, v);
     if (NULL == (vinfo->v = BN_bin2bn(tmp, len, NULL)))
         return 0;
     len = t_fromb64(tmp, s);
     return ((vinfo->s = BN_bin2bn(tmp, len, NULL)) != NULL);
 }
 
 static int SRP_user_pwd_set_sv_BN(SRP_user_pwd *vinfo, BIGNUM *s, BIGNUM *v)
 {
     vinfo->v = v;
     vinfo->s = s;
     return (vinfo->s != NULL && vinfo->v != NULL);
 }
 
+static SRP_user_pwd *srp_user_pwd_dup(SRP_user_pwd *src)
+{
+    SRP_user_pwd *ret;
+
+    if (src == NULL)
+        return NULL;
+    if ((ret = SRP_user_pwd_new()) == NULL)
+        return NULL;
+
+    SRP_user_pwd_set_gN(ret, src->g, src->N);
+    if (!SRP_user_pwd_set_ids(ret, src->id, src->info)
+        || !SRP_user_pwd_set_sv_BN(ret, BN_dup(src->s), BN_dup(src->v))) {
+            SRP_user_pwd_free(ret);
+            return NULL;
+    }
+    return ret;
+}
+
 SRP_VBASE *SRP_VBASE_new(char *seed_key)
 {
     SRP_VBASE *vb = (SRP_VBASE *)OPENSSL_malloc(sizeof(SRP_VBASE));
 
     if (vb == NULL)
         return NULL;
     if (!(vb->users_pwd = sk_SRP_user_pwd_new_null()) ||
         !(vb->gN_cache = sk_SRP_gN_cache_new_null())) {
         OPENSSL_free(vb);
         return NULL;
     }
     vb->default_g = NULL;
     vb->default_N = NULL;
     vb->seed_key = NULL;
     if ((seed_key != NULL) && (vb->seed_key = BUF_strdup(seed_key)) == NULL) {
         sk_SRP_user_pwd_free(vb->users_pwd);
         sk_SRP_gN_cache_free(vb->gN_cache);
         OPENSSL_free(vb);
         return NULL;
     }
     return vb;
 }
 
 int SRP_VBASE_free(SRP_VBASE *vb)
 {
     sk_SRP_user_pwd_pop_free(vb->users_pwd, SRP_user_pwd_free);
     sk_SRP_gN_cache_free(vb->gN_cache);
     OPENSSL_free(vb->seed_key);
     OPENSSL_free(vb);
     return 0;
 }
 
 static SRP_gN_cache *SRP_gN_new_init(const char *ch)
 {
     unsigned char tmp[MAX_LEN];
     int len;
 
     SRP_gN_cache *newgN =
         (SRP_gN_cache *)OPENSSL_malloc(sizeof(SRP_gN_cache));
     if (newgN == NULL)
         return NULL;
 
     if ((newgN->b64_bn = BUF_strdup(ch)) == NULL)
         goto err;
 
     len = t_fromb64(tmp, ch);
     if ((newgN->bn = BN_bin2bn(tmp, len, NULL)))
         return newgN;
 
     OPENSSL_free(newgN->b64_bn);
  err:
     OPENSSL_free(newgN);
     return NULL;
 }
 
 static void SRP_gN_free(SRP_gN_cache *gN_cache)
 {
     if (gN_cache == NULL)
         return;
     OPENSSL_free(gN_cache->b64_bn);
     BN_free(gN_cache->bn);
     OPENSSL_free(gN_cache);
 }
 
 static SRP_gN *SRP_get_gN_by_id(const char *id, STACK_OF(SRP_gN) *gN_tab)
 {
     int i;
 
     SRP_gN *gN;
     if (gN_tab != NULL)
         for (i = 0; i < sk_SRP_gN_num(gN_tab); i++) {
             gN = sk_SRP_gN_value(gN_tab, i);
             if (gN && (id == NULL || strcmp(gN->id, id) == 0))
                 return gN;
         }
 
     return SRP_get_default_gN(id);
 }
 
 static BIGNUM *SRP_gN_place_bn(STACK_OF(SRP_gN_cache) *gN_cache, char *ch)
 {
     int i;
     if (gN_cache == NULL)
         return NULL;
 
     /* search if we have already one... */
     for (i = 0; i < sk_SRP_gN_cache_num(gN_cache); i++) {
         SRP_gN_cache *cache = sk_SRP_gN_cache_value(gN_cache, i);
         if (strcmp(cache->b64_bn, ch) == 0)
             return cache->bn;
     }
     {                           /* it is the first time that we find it */
         SRP_gN_cache *newgN = SRP_gN_new_init(ch);
         if (newgN) {
             if (sk_SRP_gN_cache_insert(gN_cache, newgN, 0) > 0)
                 return newgN->bn;
             SRP_gN_free(newgN);
         }
     }
     return NULL;
 }
 
 /*
  * this function parses verifier file. Format is:
  * string(index):base64(N):base64(g):0
  * string(username):base64(v):base64(salt):int(index)
  */
 
 int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file)
 {
     int error_code;
     STACK_OF(SRP_gN) *SRP_gN_tab = sk_SRP_gN_new_null();
     char *last_index = NULL;
     int i;
     char **pp;
 
     SRP_gN *gN = NULL;
     SRP_user_pwd *user_pwd = NULL;
 
     TXT_DB *tmpdb = NULL;
     BIO *in = BIO_new(BIO_s_file());
 
     error_code = SRP_ERR_OPEN_FILE;
 
     if (in == NULL || BIO_read_filename(in, verifier_file) <= 0)
         goto err;
 
     error_code = SRP_ERR_VBASE_INCOMPLETE_FILE;
 
     if ((tmpdb = TXT_DB_read(in, DB_NUMBER)) == NULL)
         goto err;
 
     error_code = SRP_ERR_MEMORY;
 
     if (vb->seed_key) {
         last_index = SRP_get_default_gN(NULL)->id;
     }
     for (i = 0; i < sk_OPENSSL_PSTRING_num(tmpdb->data); i++) {
         pp = sk_OPENSSL_PSTRING_value(tmpdb->data, i);
         if (pp[DB_srptype][0] == DB_SRP_INDEX) {
             /*
              * we add this couple in the internal Stack
              */
 
             if ((gN = (SRP_gN *) OPENSSL_malloc(sizeof(SRP_gN))) == NULL)
                 goto err;
 
             if (!(gN->id = BUF_strdup(pp[DB_srpid]))
                 || !(gN->N =
                      SRP_gN_place_bn(vb->gN_cache, pp[DB_srpverifier]))
                 || !(gN->g = SRP_gN_place_bn(vb->gN_cache, pp[DB_srpsalt]))
                 || sk_SRP_gN_insert(SRP_gN_tab, gN, 0) == 0)
                 goto err;
 
             gN = NULL;
 
             if (vb->seed_key != NULL) {
                 last_index = pp[DB_srpid];
             }
         } else if (pp[DB_srptype][0] == DB_SRP_VALID) {
             /* it is a user .... */
             SRP_gN *lgN;
             if ((lgN = SRP_get_gN_by_id(pp[DB_srpgN], SRP_gN_tab)) != NULL) {
                 error_code = SRP_ERR_MEMORY;
                 if ((user_pwd = SRP_user_pwd_new()) == NULL)
                     goto err;
 
                 SRP_user_pwd_set_gN(user_pwd, lgN->g, lgN->N);
                 if (!SRP_user_pwd_set_ids
                     (user_pwd, pp[DB_srpid], pp[DB_srpinfo]))
                     goto err;
 
                 error_code = SRP_ERR_VBASE_BN_LIB;
                 if (!SRP_user_pwd_set_sv
                     (user_pwd, pp[DB_srpsalt], pp[DB_srpverifier]))
                     goto err;
 
                 if (sk_SRP_user_pwd_insert(vb->users_pwd, user_pwd, 0) == 0)
                     goto err;
                 user_pwd = NULL; /* abandon responsability */
             }
         }
     }
 
     if (last_index != NULL) {
         /* this means that we want to simulate a default user */
 
         if (((gN = SRP_get_gN_by_id(last_index, SRP_gN_tab)) == NULL)) {
             error_code = SRP_ERR_VBASE_BN_LIB;
             goto err;
         }
         vb->default_g = gN->g;
         vb->default_N = gN->N;
         gN = NULL;
     }
     error_code = SRP_NO_ERROR;
 
  err:
     /*
      * there may be still some leaks to fix, if this fails, the application
      * terminates most likely
      */
 
     if (gN != NULL) {
         OPENSSL_free(gN->id);
         OPENSSL_free(gN);
     }
 
     SRP_user_pwd_free(user_pwd);
 
     if (tmpdb)
         TXT_DB_free(tmpdb);
     if (in)
         BIO_free_all(in);
 
     sk_SRP_gN_free(SRP_gN_tab);
 
     return error_code;
 
 }
 
-SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
+static SRP_user_pwd *find_user(SRP_VBASE *vb, char *username)
 {
     int i;
     SRP_user_pwd *user;
-    unsigned char digv[SHA_DIGEST_LENGTH];
-    unsigned char digs[SHA_DIGEST_LENGTH];
-    EVP_MD_CTX ctxt;
 
     if (vb == NULL)
         return NULL;
+
     for (i = 0; i < sk_SRP_user_pwd_num(vb->users_pwd); i++) {
         user = sk_SRP_user_pwd_value(vb->users_pwd, i);
         if (strcmp(user->id, username) == 0)
             return user;
     }
+
+    return NULL;
+}
+
+/*
+ * This method ignores the configured seed and fails for an unknown user.
+ * Ownership of the returned pointer is not released to the caller.
+ * In other words, caller must not free the result.
+ */
+SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username)
+{
+    return find_user(vb, username);
+}
+
+/*
+ * Ownership of the returned pointer is released to the caller.
+ * In other words, caller must free the result once done.
+ */
+SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username)
+{
+    SRP_user_pwd *user;
+    unsigned char digv[SHA_DIGEST_LENGTH];
+    unsigned char digs[SHA_DIGEST_LENGTH];
+    EVP_MD_CTX ctxt;
+
+    if (vb == NULL)
+        return NULL;
+
+    if ((user = find_user(vb, username)) != NULL)
+        return srp_user_pwd_dup(user);
+
     if ((vb->seed_key == NULL) ||
         (vb->default_g == NULL) || (vb->default_N == NULL))
         return NULL;
 
 /* if the user is unknown we set parameters as well if we have a seed_key */
 
     if ((user = SRP_user_pwd_new()) == NULL)
         return NULL;
 
     SRP_user_pwd_set_gN(user, vb->default_g, vb->default_N);
 
     if (!SRP_user_pwd_set_ids(user, username, NULL))
         goto err;
 
     if (RAND_pseudo_bytes(digv, SHA_DIGEST_LENGTH) < 0)
         goto err;
     EVP_MD_CTX_init(&ctxt);
     EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
     EVP_DigestUpdate(&ctxt, vb->seed_key, strlen(vb->seed_key));
     EVP_DigestUpdate(&ctxt, username, strlen(username));
     EVP_DigestFinal_ex(&ctxt, digs, NULL);
     EVP_MD_CTX_cleanup(&ctxt);
     if (SRP_user_pwd_set_sv_BN
         (user, BN_bin2bn(digs, SHA_DIGEST_LENGTH, NULL),
          BN_bin2bn(digv, SHA_DIGEST_LENGTH, NULL)))
         return user;
 
  err:SRP_user_pwd_free(user);
     return NULL;
 }
 
 /*
  * create a verifier (*salt,*verifier,g and N are in base64)
  */
 char *SRP_create_verifier(const char *user, const char *pass, char **salt,
                           char **verifier, const char *N, const char *g)
 {
     int len;
     char *result = NULL, *vf = NULL;
     BIGNUM *N_bn = NULL, *g_bn = NULL, *s = NULL, *v = NULL;
     unsigned char tmp[MAX_LEN];
     unsigned char tmp2[MAX_LEN];
     char *defgNid = NULL;
     int vfsize = 0;
 
     if ((user == NULL) ||
         (pass == NULL) || (salt == NULL) || (verifier == NULL))
         goto err;
 
     if (N) {
         if (!(len = t_fromb64(tmp, N)))
             goto err;
         N_bn = BN_bin2bn(tmp, len, NULL);
         if (!(len = t_fromb64(tmp, g)))
             goto err;
         g_bn = BN_bin2bn(tmp, len, NULL);
         defgNid = "*";
     } else {
         SRP_gN *gN = SRP_get_gN_by_id(g, NULL);
         if (gN == NULL)
             goto err;
         N_bn = gN->N;
         g_bn = gN->g;
         defgNid = gN->id;
     }
 
     if (*salt == NULL) {
         if (RAND_pseudo_bytes(tmp2, SRP_RANDOM_SALT_LEN) < 0)
             goto err;
 
         s = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
     } else {
         if (!(len = t_fromb64(tmp2, *salt)))
             goto err;
         s = BN_bin2bn(tmp2, len, NULL);
     }
 
     if (!SRP_create_verifier_BN(user, pass, &s, &v, N_bn, g_bn))
         goto err;
 
     BN_bn2bin(v, tmp);
     vfsize = BN_num_bytes(v) * 2;
     if (((vf = OPENSSL_malloc(vfsize)) == NULL))
         goto err;
     t_tob64(vf, tmp, BN_num_bytes(v));
 
     if (*salt == NULL) {
         char *tmp_salt;
 
         if ((tmp_salt = OPENSSL_malloc(SRP_RANDOM_SALT_LEN * 2)) == NULL) {
             goto err;
         }
         t_tob64(tmp_salt, tmp2, SRP_RANDOM_SALT_LEN);
         *salt = tmp_salt;
     }
 
     *verifier = vf;
     vf = NULL;
     result = defgNid;
 
  err:
     if (N) {
         BN_free(N_bn);
         BN_free(g_bn);
     }
     OPENSSL_cleanse(vf, vfsize);
     OPENSSL_free(vf);
     BN_clear_free(s);
     BN_clear_free(v);
     return result;
 }
 
 /*
  * create a verifier (*salt,*verifier,g and N are BIGNUMs). If *salt != NULL
  * then the provided salt will be used. On successful exit *verifier will point
  * to a newly allocated BIGNUM containing the verifier and (if a salt was not
  * provided) *salt will be populated with a newly allocated BIGNUM containing a
  * random salt.
  * The caller is responsible for freeing the allocated *salt and *verifier
  * BIGNUMS.
  */
 int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
                            BIGNUM **verifier, BIGNUM *N, BIGNUM *g)
 {
     int result = 0;
     BIGNUM *x = NULL;
     BN_CTX *bn_ctx = BN_CTX_new();
     unsigned char tmp2[MAX_LEN];
     BIGNUM *salttmp = NULL;
 
     if ((user == NULL) ||
         (pass == NULL) ||
         (salt == NULL) ||
         (verifier == NULL) || (N == NULL) || (g == NULL) || (bn_ctx == NULL))
         goto err;
 
     srp_bn_print(N);
     srp_bn_print(g);
 
     if (*salt == NULL) {
         if (RAND_pseudo_bytes(tmp2, SRP_RANDOM_SALT_LEN) < 0)
             goto err;
 
         salttmp = BN_bin2bn(tmp2, SRP_RANDOM_SALT_LEN, NULL);
     } else {
         salttmp = *salt;
     }
 
     x = SRP_Calc_x(salttmp, user, pass);
 
     *verifier = BN_new();
     if (*verifier == NULL)
         goto err;
 
     if (!BN_mod_exp(*verifier, g, x, N, bn_ctx)) {
         BN_clear_free(*verifier);
         goto err;
     }
 
     srp_bn_print(*verifier);
 
     result = 1;
     *salt = salttmp;
 
  err:
     if (*salt != salttmp)
         BN_clear_free(salttmp);
     BN_clear_free(x);
     BN_CTX_free(bn_ctx);
     return result;
 }
 
 #endif
Index: vendor-crypto/openssl/dist/crypto/stack/stack.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/stack/stack.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/stack/stack.c	(revision 296273)
@@ -1,384 +1,384 @@
 /* crypto/stack/stack.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 /*-
  * Code for stacks
  * Author - Eric Young v 1.0
  * 1.2 eay 12-Mar-97 -  Modified sk_find so that it _DOES_ return the
  *                      lowest index for the searched item.
  *
  * 1.1 eay - Take from netdb and added to SSLeay
  *
  * 1.0 eay - First version 29/07/92
  */
 #include 
 #include "cryptlib.h"
 #include 
 #include 
 
 #undef MIN_NODES
 #define MIN_NODES       4
 
 const char STACK_version[] = "Stack" OPENSSL_VERSION_PTEXT;
 
 #include 
 
 int (*sk_set_cmp_func(_STACK *sk, int (*c) (const void *, const void *)))
  (const void *, const void *) {
     int (*old) (const void *, const void *) = sk->comp;
 
     if (sk->comp != c)
         sk->sorted = 0;
     sk->comp = c;
 
     return old;
 }
 
 _STACK *sk_dup(_STACK *sk)
 {
     _STACK *ret;
     char **s;
 
     if ((ret = sk_new(sk->comp)) == NULL)
         goto err;
     s = (char **)OPENSSL_realloc((char *)ret->data,
                                  (unsigned int)sizeof(char *) *
                                  sk->num_alloc);
     if (s == NULL)
         goto err;
     ret->data = s;
 
     ret->num = sk->num;
     memcpy(ret->data, sk->data, sizeof(char *) * sk->num);
     ret->sorted = sk->sorted;
     ret->num_alloc = sk->num_alloc;
     ret->comp = sk->comp;
     return (ret);
  err:
     if (ret)
         sk_free(ret);
     return (NULL);
 }
 
 _STACK *sk_deep_copy(_STACK *sk, void *(*copy_func) (void *),
                      void (*free_func) (void *))
 {
     _STACK *ret;
     int i;
 
     if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
         return ret;
     ret->comp = sk->comp;
     ret->sorted = sk->sorted;
     ret->num = sk->num;
     ret->num_alloc = sk->num > MIN_NODES ? sk->num : MIN_NODES;
     ret->data = OPENSSL_malloc(sizeof(char *) * ret->num_alloc);
     if (ret->data == NULL) {
         OPENSSL_free(ret);
         return NULL;
     }
     for (i = 0; i < ret->num_alloc; i++)
         ret->data[i] = NULL;
 
     for (i = 0; i < ret->num; ++i) {
         if (sk->data[i] == NULL)
             continue;
         if ((ret->data[i] = copy_func(sk->data[i])) == NULL) {
             while (--i >= 0)
                 if (ret->data[i] != NULL)
                     free_func(ret->data[i]);
             sk_free(ret);
             return NULL;
         }
     }
     return ret;
 }
 
 _STACK *sk_new_null(void)
 {
     return sk_new((int (*)(const void *, const void *))0);
 }
 
 _STACK *sk_new(int (*c) (const void *, const void *))
 {
     _STACK *ret;
     int i;
 
     if ((ret = OPENSSL_malloc(sizeof(_STACK))) == NULL)
         goto err;
     if ((ret->data = OPENSSL_malloc(sizeof(char *) * MIN_NODES)) == NULL)
         goto err;
     for (i = 0; i < MIN_NODES; i++)
         ret->data[i] = NULL;
     ret->comp = c;
     ret->num_alloc = MIN_NODES;
     ret->num = 0;
     ret->sorted = 0;
     return (ret);
  err:
     if (ret)
         OPENSSL_free(ret);
     return (NULL);
 }
 
 int sk_insert(_STACK *st, void *data, int loc)
 {
     char **s;
 
     if (st == NULL)
         return 0;
     if (st->num_alloc <= st->num + 1) {
         s = OPENSSL_realloc((char *)st->data,
                             (unsigned int)sizeof(char *) * st->num_alloc * 2);
         if (s == NULL)
             return (0);
         st->data = s;
         st->num_alloc *= 2;
     }
     if ((loc >= (int)st->num) || (loc < 0))
         st->data[st->num] = data;
     else {
         int i;
         char **f, **t;
 
         f = st->data;
         t = &(st->data[1]);
         for (i = st->num; i >= loc; i--)
             t[i] = f[i];
 
 #ifdef undef                    /* no memmove on sunos :-( */
         memmove(&(st->data[loc + 1]),
                 &(st->data[loc]), sizeof(char *) * (st->num - loc));
 #endif
         st->data[loc] = data;
     }
     st->num++;
     st->sorted = 0;
     return (st->num);
 }
 
 void *sk_delete_ptr(_STACK *st, void *p)
 {
     int i;
 
     for (i = 0; i < st->num; i++)
         if (st->data[i] == p)
             return (sk_delete(st, i));
     return (NULL);
 }
 
 void *sk_delete(_STACK *st, int loc)
 {
     char *ret;
     int i, j;
 
     if (!st || (loc < 0) || (loc >= st->num))
         return NULL;
 
     ret = st->data[loc];
     if (loc != st->num - 1) {
         j = st->num - 1;
         for (i = loc; i < j; i++)
             st->data[i] = st->data[i + 1];
         /*
          * In theory memcpy is not safe for this memcpy( &(st->data[loc]),
          * &(st->data[loc+1]), sizeof(char *)*(st->num-loc-1));
          */
     }
     st->num--;
     return (ret);
 }
 
 static int internal_find(_STACK *st, void *data, int ret_val_options)
 {
     const void *const *r;
     int i;
 
     if (st == NULL)
         return -1;
 
     if (st->comp == NULL) {
         for (i = 0; i < st->num; i++)
             if (st->data[i] == data)
                 return (i);
         return (-1);
     }
     sk_sort(st);
     if (data == NULL)
         return (-1);
     r = OBJ_bsearch_ex_(&data, st->data, st->num, sizeof(void *), st->comp,
                         ret_val_options);
     if (r == NULL)
         return (-1);
     return (int)((char **)r - st->data);
 }
 
 int sk_find(_STACK *st, void *data)
 {
     return internal_find(st, data, OBJ_BSEARCH_FIRST_VALUE_ON_MATCH);
 }
 
 int sk_find_ex(_STACK *st, void *data)
 {
     return internal_find(st, data, OBJ_BSEARCH_VALUE_ON_NOMATCH);
 }
 
 int sk_push(_STACK *st, void *data)
 {
     return (sk_insert(st, data, st->num));
 }
 
 int sk_unshift(_STACK *st, void *data)
 {
     return (sk_insert(st, data, 0));
 }
 
 void *sk_shift(_STACK *st)
 {
     if (st == NULL)
         return (NULL);
     if (st->num <= 0)
         return (NULL);
     return (sk_delete(st, 0));
 }
 
 void *sk_pop(_STACK *st)
 {
     if (st == NULL)
         return (NULL);
     if (st->num <= 0)
         return (NULL);
     return (sk_delete(st, st->num - 1));
 }
 
 void sk_zero(_STACK *st)
 {
     if (st == NULL)
         return;
     if (st->num <= 0)
         return;
     memset((char *)st->data, 0, sizeof(*st->data) * st->num);
     st->num = 0;
 }
 
 void sk_pop_free(_STACK *st, void (*func) (void *))
 {
     int i;
 
     if (st == NULL)
         return;
     for (i = 0; i < st->num; i++)
         if (st->data[i] != NULL)
             func(st->data[i]);
     sk_free(st);
 }
 
 void sk_free(_STACK *st)
 {
     if (st == NULL)
         return;
     if (st->data != NULL)
         OPENSSL_free(st->data);
     OPENSSL_free(st);
 }
 
 int sk_num(const _STACK *st)
 {
     if (st == NULL)
         return -1;
     return st->num;
 }
 
 void *sk_value(const _STACK *st, int i)
 {
     if (!st || (i < 0) || (i >= st->num))
         return NULL;
     return st->data[i];
 }
 
 void *sk_set(_STACK *st, int i, void *value)
 {
     if (!st || (i < 0) || (i >= st->num))
         return NULL;
     return (st->data[i] = value);
 }
 
 void sk_sort(_STACK *st)
 {
-    if (st && !st->sorted) {
+    if (st && !st->sorted && st->comp != NULL) {
         int (*comp_func) (const void *, const void *);
 
         /*
          * same comment as in sk_find ... previously st->comp was declared as
          * a (void*,void*) callback type, but this made the population of the
          * callback pointer illogical - our callbacks compare type** with
          * type**, so we leave the casting until absolutely necessary (ie.
          * "now").
          */
         comp_func = (int (*)(const void *, const void *))(st->comp);
         qsort(st->data, st->num, sizeof(char *), comp_func);
         st->sorted = 1;
     }
 }
 
 int sk_is_sorted(const _STACK *st)
 {
     if (!st)
         return 1;
     return st->sorted;
 }
Index: vendor-crypto/openssl/dist/crypto/x509/x509_vfy.c
===================================================================
--- vendor-crypto/openssl/dist/crypto/x509/x509_vfy.c	(revision 296272)
+++ vendor-crypto/openssl/dist/crypto/x509/x509_vfy.c	(revision 296273)
@@ -1,2487 +1,2497 @@
 /* crypto/x509/x509_vfy.c */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  * by Eric Young (eay@cryptsoft.com).
  * The implementation was written so as to conform with Netscapes SSL.
  *
  * This library is free for commercial and non-commercial use as long as
  * the following conditions are aheared to.  The following conditions
  * apply to all code found in this distribution, be it the RC4, RSA,
  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
  * included with this distribution is covered by the same copyright terms
  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  *
  * Copyright remains Eric Young's, and as such any Copyright notices in
  * the code are not to be removed.
  * If this package is used in a product, Eric Young should be given attribution
  * as the author of the parts of the library used.
  * This can be in the form of a textual message at program startup or
  * in documentation (online or textual) provided with the package.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the copyright
  *    notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
  *    "This product includes cryptographic software written by
  *     Eric Young (eay@cryptsoft.com)"
  *    The word 'cryptographic' can be left out if the rouines from the library
  *    being used are not cryptographic related :-).
  * 4. If you include any Windows specific code (or a derivative thereof) from
  *    the apps directory (application code) you must include an acknowledgement:
  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  *
  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
  * The licence and distribution terms for any publically available version or
  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
 
 #include 
 #include 
 #include 
 
 #include "cryptlib.h"
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 #include "vpm_int.h"
 
 /* CRL score values */
 
 /* No unhandled critical extensions */
 
 #define CRL_SCORE_NOCRITICAL    0x100
 
 /* certificate is within CRL scope */
 
 #define CRL_SCORE_SCOPE         0x080
 
 /* CRL times valid */
 
 #define CRL_SCORE_TIME          0x040
 
 /* Issuer name matches certificate */
 
 #define CRL_SCORE_ISSUER_NAME   0x020
 
 /* If this score or above CRL is probably valid */
 
 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
 
 /* CRL issuer is certificate issuer */
 
 #define CRL_SCORE_ISSUER_CERT   0x018
 
 /* CRL issuer is on certificate path */
 
 #define CRL_SCORE_SAME_PATH     0x008
 
 /* CRL issuer matches CRL AKID */
 
 #define CRL_SCORE_AKID          0x004
 
 /* Have a delta CRL with valid times */
 
 #define CRL_SCORE_TIME_DELTA    0x002
 
 static int null_callback(int ok, X509_STORE_CTX *e);
 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
 static int check_chain_extensions(X509_STORE_CTX *ctx);
 static int check_name_constraints(X509_STORE_CTX *ctx);
 static int check_id(X509_STORE_CTX *ctx);
 static int check_trust(X509_STORE_CTX *ctx);
 static int check_revocation(X509_STORE_CTX *ctx);
 static int check_cert(X509_STORE_CTX *ctx);
 static int check_policy(X509_STORE_CTX *ctx);
 
 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
                          unsigned int *preasons, X509_CRL *crl, X509 *x);
 static int get_crl_delta(X509_STORE_CTX *ctx,
                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
                          int *pcrl_score, X509_CRL *base,
                          STACK_OF(X509_CRL) *crls);
 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
                            int *pcrl_score);
 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
                            unsigned int *preasons);
 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
 static int check_crl_chain(X509_STORE_CTX *ctx,
                            STACK_OF(X509) *cert_path,
                            STACK_OF(X509) *crl_path);
 
 static int internal_verify(X509_STORE_CTX *ctx);
 const char X509_version[] = "X.509" OPENSSL_VERSION_PTEXT;
 
 static int null_callback(int ok, X509_STORE_CTX *e)
 {
     return ok;
 }
 
 #if 0
 static int x509_subject_cmp(X509 **a, X509 **b)
 {
     return X509_subject_name_cmp(*a, *b);
 }
 #endif
 /* Return 1 is a certificate is self signed */
 static int cert_self_signed(X509 *x)
 {
     X509_check_purpose(x, -1, 0);
     if (x->ex_flags & EXFLAG_SS)
         return 1;
     else
         return 0;
 }
 
 /* Given a certificate try and find an exact match in the store */
 
 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
 {
     STACK_OF(X509) *certs;
     X509 *xtmp = NULL;
     int i;
     /* Lookup all certs with matching subject name */
     certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
     if (certs == NULL)
         return NULL;
     /* Look for exact match */
     for (i = 0; i < sk_X509_num(certs); i++) {
         xtmp = sk_X509_value(certs, i);
         if (!X509_cmp(xtmp, x))
             break;
     }
     if (i < sk_X509_num(certs))
         CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
     else
         xtmp = NULL;
     sk_X509_pop_free(certs, X509_free);
     return xtmp;
 }
 
 int X509_verify_cert(X509_STORE_CTX *ctx)
 {
     X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
     int bad_chain = 0;
     X509_VERIFY_PARAM *param = ctx->param;
     int depth, i, ok = 0;
     int num, j, retry;
     int (*cb) (int xok, X509_STORE_CTX *xctx);
     STACK_OF(X509) *sktmp = NULL;
+    int trust = X509_TRUST_UNTRUSTED;
+    int err;
+
     if (ctx->cert == NULL) {
         X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
         return -1;
     }
     if (ctx->chain != NULL) {
         /*
          * This X509_STORE_CTX has already been used to verify a cert. We
          * cannot do another one.
          */
         X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
         return -1;
     }
 
     cb = ctx->verify_cb;
 
     /*
      * first we make sure the chain we are going to build is present and that
      * the first entry is in place
      */
     if (((ctx->chain = sk_X509_new_null()) == NULL) ||
         (!sk_X509_push(ctx->chain, ctx->cert))) {
         X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
-        goto end;
+        ok = -1;
+        goto err;
     }
     CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
     ctx->last_untrusted = 1;
 
     /* We use a temporary STACK so we can chop and hack at it */
     if (ctx->untrusted != NULL
         && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
         X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
-        goto end;
+        ok = -1;
+        goto err;
     }
 
     num = sk_X509_num(ctx->chain);
     x = sk_X509_value(ctx->chain, num - 1);
     depth = param->depth;
 
     for (;;) {
         /* If we have enough, we break */
         if (depth < num)
             break;              /* FIXME: If this happens, we should take
                                  * note of it and, if appropriate, use the
                                  * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
                                  * later. */
 
         /* If we are self signed, we break */
         if (cert_self_signed(x))
             break;
         /*
          * If asked see if we can find issuer in trusted store first
          */
         if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
             ok = ctx->get_issuer(&xtmp, ctx, x);
             if (ok < 0)
-                goto end;
+                goto err;
             /*
              * If successful for now free up cert so it will be picked up
              * again later.
              */
             if (ok > 0) {
                 X509_free(xtmp);
                 break;
             }
         }
 
         /* If we were passed a cert chain, use it first */
         if (ctx->untrusted != NULL) {
             xtmp = find_issuer(ctx, sktmp, x);
             if (xtmp != NULL) {
                 if (!sk_X509_push(ctx->chain, xtmp)) {
                     X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
-                    goto end;
+                    ok = -1;
+                    goto err;
                 }
                 CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
                 (void)sk_X509_delete_ptr(sktmp, xtmp);
                 ctx->last_untrusted++;
                 x = xtmp;
                 num++;
                 /*
                  * reparse the full chain for the next one
                  */
                 continue;
             }
         }
         break;
     }
 
     /* Remember how many untrusted certs we have */
     j = num;
     /*
      * at this point, chain should contain a list of untrusted certificates.
      * We now need to add at least one trusted one, if possible, otherwise we
      * complain.
      */
 
     do {
         /*
          * Examine last certificate in chain and see if it is self signed.
          */
         i = sk_X509_num(ctx->chain);
         x = sk_X509_value(ctx->chain, i - 1);
         if (cert_self_signed(x)) {
             /* we have a self signed certificate */
             if (sk_X509_num(ctx->chain) == 1) {
                 /*
                  * We have a single self signed certificate: see if we can
                  * find it in the store. We must have an exact match to avoid
                  * possible impersonation.
                  */
                 ok = ctx->get_issuer(&xtmp, ctx, x);
                 if ((ok <= 0) || X509_cmp(x, xtmp)) {
                     ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
                     ctx->current_cert = x;
                     ctx->error_depth = i - 1;
                     if (ok == 1)
                         X509_free(xtmp);
                     bad_chain = 1;
                     ok = cb(0, ctx);
                     if (!ok)
-                        goto end;
+                        goto err;
                 } else {
                     /*
                      * We have a match: replace certificate with store
                      * version so we get any trust settings.
                      */
                     X509_free(x);
                     x = xtmp;
                     (void)sk_X509_set(ctx->chain, i - 1, x);
                     ctx->last_untrusted = 0;
                 }
             } else {
                 /*
                  * extract and save self signed certificate for later use
                  */
                 chain_ss = sk_X509_pop(ctx->chain);
                 ctx->last_untrusted--;
                 num--;
                 j--;
                 x = sk_X509_value(ctx->chain, num - 1);
             }
         }
         /* We now lookup certs from the certificate store */
         for (;;) {
             /* If we have enough, we break */
             if (depth < num)
                 break;
             /* If we are self signed, we break */
             if (cert_self_signed(x))
                 break;
             ok = ctx->get_issuer(&xtmp, ctx, x);
 
             if (ok < 0)
-                goto end;
+                goto err;
             if (ok == 0)
                 break;
             x = xtmp;
             if (!sk_X509_push(ctx->chain, x)) {
                 X509_free(xtmp);
                 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
-                ok = 0;
-                goto end;
+                ok = -1;
+                goto err;
             }
             num++;
         }
 
         /* we now have our chain, lets check it... */
-        i = check_trust(ctx);
+        if ((trust = check_trust(ctx)) == X509_TRUST_REJECTED) {
+            /* Callback already issued */
+            ok = 0;
+            goto err;
+        }
 
-        /* If explicitly rejected error */
-        if (i == X509_TRUST_REJECTED)
-            goto end;
         /*
          * If it's not explicitly trusted then check if there is an alternative
          * chain that could be used. We only do this if we haven't already
          * checked via TRUSTED_FIRST and the user hasn't switched off alternate
          * chain checking
          */
         retry = 0;
-        if (i != X509_TRUST_TRUSTED
+        if (trust != X509_TRUST_TRUSTED
             && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
             && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
             while (j-- > 1) {
                 xtmp2 = sk_X509_value(ctx->chain, j - 1);
                 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
                 if (ok < 0)
-                    goto end;
+                    goto err;
                 /* Check if we found an alternate chain */
                 if (ok > 0) {
                     /*
                      * Free up the found cert we'll add it again later
                      */
                     X509_free(xtmp);
 
                     /*
                      * Dump all the certs above this point - we've found an
                      * alternate chain
                      */
                     while (num > j) {
                         xtmp = sk_X509_pop(ctx->chain);
                         X509_free(xtmp);
                         num--;
                     }
                     ctx->last_untrusted = sk_X509_num(ctx->chain);
                     retry = 1;
                     break;
                 }
             }
         }
     } while (retry);
 
     /*
      * If not explicitly trusted then indicate error unless it's a single
      * self signed certificate in which case we've indicated an error already
      * and set bad_chain == 1
      */
-    if (i != X509_TRUST_TRUSTED && !bad_chain) {
+    if (trust != X509_TRUST_TRUSTED && !bad_chain) {
         if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
             if (ctx->last_untrusted >= num)
                 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
             else
                 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
             ctx->current_cert = x;
         } else {
 
             sk_X509_push(ctx->chain, chain_ss);
             num++;
             ctx->last_untrusted = num;
             ctx->current_cert = chain_ss;
             ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
             chain_ss = NULL;
         }
 
         ctx->error_depth = num - 1;
         bad_chain = 1;
         ok = cb(0, ctx);
         if (!ok)
-            goto end;
+            goto err;
     }
 
     /* We have the chain complete: now we need to check its purpose */
     ok = check_chain_extensions(ctx);
 
     if (!ok)
-        goto end;
+        goto err;
 
     /* Check name constraints */
 
     ok = check_name_constraints(ctx);
 
     if (!ok)
-        goto end;
+        goto err;
 
     ok = check_id(ctx);
 
     if (!ok)
-        goto end;
+        goto err;
 
     /* We may as well copy down any DSA parameters that are required */
     X509_get_pubkey_parameters(NULL, ctx->chain);
 
     /*
      * Check revocation status: we do this after copying parameters because
      * they may be needed for CRL signature verification.
      */
 
     ok = ctx->check_revocation(ctx);
     if (!ok)
-        goto end;
+        goto err;
 
-    i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
-                                ctx->param->flags);
-    if (i != X509_V_OK) {
-        ctx->error = i;
+    err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
+                                  ctx->param->flags);
+    if (err != X509_V_OK) {
+        ctx->error = err;
         ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
         ok = cb(0, ctx);
         if (!ok)
-            goto end;
+            goto err;
     }
 
     /* At this point, we have a chain and need to verify it */
     if (ctx->verify != NULL)
         ok = ctx->verify(ctx);
     else
         ok = internal_verify(ctx);
     if (!ok)
-        goto end;
+        goto err;
 
 #ifndef OPENSSL_NO_RFC3779
     /* RFC 3779 path validation, now that CRL check has been done */
     ok = v3_asid_validate_path(ctx);
     if (!ok)
-        goto end;
+        goto err;
     ok = v3_addr_validate_path(ctx);
     if (!ok)
-        goto end;
+        goto err;
 #endif
 
     /* If we get this far evaluate policies */
     if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
         ok = ctx->check_policy(ctx);
     if (!ok)
-        goto end;
+        goto err;
     if (0) {
- end:
+ err:
+        /* Ensure we return an error */
+        if (ok > 0)
+            ok = 0;
         X509_get_pubkey_parameters(NULL, ctx->chain);
     }
     if (sktmp != NULL)
         sk_X509_free(sktmp);
     if (chain_ss != NULL)
         X509_free(chain_ss);
     return ok;
 }
 
 /*
  * Given a STACK_OF(X509) find the issuer of cert (if any)
  */
 
 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
 {
     int i;
     X509 *issuer;
     for (i = 0; i < sk_X509_num(sk); i++) {
         issuer = sk_X509_value(sk, i);
         if (ctx->check_issued(ctx, x, issuer))
             return issuer;
     }
     return NULL;
 }
 
 /* Given a possible certificate and issuer check them */
 
 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
 {
     int ret;
     ret = X509_check_issued(issuer, x);
     if (ret == X509_V_OK)
         return 1;
     /* If we haven't asked for issuer errors don't set ctx */
     if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
         return 0;
 
     ctx->error = ret;
     ctx->current_cert = x;
     ctx->current_issuer = issuer;
     return ctx->verify_cb(0, ctx);
 }
 
 /* Alternative lookup method: look from a STACK stored in other_ctx */
 
 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
 {
     *issuer = find_issuer(ctx, ctx->other_ctx, x);
     if (*issuer) {
         CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
         return 1;
     } else
         return 0;
 }
 
 /*
  * Check a certificate chains extensions for consistency with the supplied
  * purpose
  */
 
 static int check_chain_extensions(X509_STORE_CTX *ctx)
 {
 #ifdef OPENSSL_NO_CHAIN_VERIFY
     return 1;
 #else
     int i, ok = 0, must_be_ca, plen = 0;
     X509 *x;
     int (*cb) (int xok, X509_STORE_CTX *xctx);
     int proxy_path_length = 0;
     int purpose;
     int allow_proxy_certs;
     cb = ctx->verify_cb;
 
     /*-
      *  must_be_ca can have 1 of 3 values:
      * -1: we accept both CA and non-CA certificates, to allow direct
      *     use of self-signed certificates (which are marked as CA).
      * 0:  we only accept non-CA certificates.  This is currently not
      *     used, but the possibility is present for future extensions.
      * 1:  we only accept CA certificates.  This is currently used for
      *     all certificates in the chain except the leaf certificate.
      */
     must_be_ca = -1;
 
     /* CRL path validation */
     if (ctx->parent) {
         allow_proxy_certs = 0;
         purpose = X509_PURPOSE_CRL_SIGN;
     } else {
         allow_proxy_certs =
             ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
         /*
          * A hack to keep people who don't want to modify their software
          * happy
          */
         if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
             allow_proxy_certs = 1;
         purpose = ctx->param->purpose;
     }
 
     /* Check all untrusted certificates */
     for (i = 0; i < ctx->last_untrusted; i++) {
         int ret;
         x = sk_X509_value(ctx->chain, i);
         if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
             && (x->ex_flags & EXFLAG_CRITICAL)) {
             ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
             ctx->error_depth = i;
             ctx->current_cert = x;
             ok = cb(0, ctx);
             if (!ok)
                 goto end;
         }
         if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
             ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
             ctx->error_depth = i;
             ctx->current_cert = x;
             ok = cb(0, ctx);
             if (!ok)
                 goto end;
         }
         ret = X509_check_ca(x);
         switch (must_be_ca) {
         case -1:
             if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                 && (ret != 1) && (ret != 0)) {
                 ret = 0;
                 ctx->error = X509_V_ERR_INVALID_CA;
             } else
                 ret = 1;
             break;
         case 0:
             if (ret != 0) {
                 ret = 0;
                 ctx->error = X509_V_ERR_INVALID_NON_CA;
             } else
                 ret = 1;
             break;
         default:
             if ((ret == 0)
                 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                     && (ret != 1))) {
                 ret = 0;
                 ctx->error = X509_V_ERR_INVALID_CA;
             } else
                 ret = 1;
             break;
         }
         if (ret == 0) {
             ctx->error_depth = i;
             ctx->current_cert = x;
             ok = cb(0, ctx);
             if (!ok)
                 goto end;
         }
         if (ctx->param->purpose > 0) {
             ret = X509_check_purpose(x, purpose, must_be_ca > 0);
             if ((ret == 0)
                 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                     && (ret != 1))) {
                 ctx->error = X509_V_ERR_INVALID_PURPOSE;
                 ctx->error_depth = i;
                 ctx->current_cert = x;
                 ok = cb(0, ctx);
                 if (!ok)
                     goto end;
             }
         }
         /* Check pathlen if not self issued */
         if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
             && (x->ex_pathlen != -1)
             && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
             ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
             ctx->error_depth = i;
             ctx->current_cert = x;
             ok = cb(0, ctx);
             if (!ok)
                 goto end;
         }
         /* Increment path length if not self issued */
         if (!(x->ex_flags & EXFLAG_SI))
             plen++;
         /*
          * If this certificate is a proxy certificate, the next certificate
          * must be another proxy certificate or a EE certificate.  If not,
          * the next certificate must be a CA certificate.
          */
         if (x->ex_flags & EXFLAG_PROXY) {
             if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
                 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
                 ctx->error_depth = i;
                 ctx->current_cert = x;
                 ok = cb(0, ctx);
                 if (!ok)
                     goto end;
             }
             proxy_path_length++;
             must_be_ca = 0;
         } else
             must_be_ca = 1;
     }
     ok = 1;
  end:
     return ok;
 #endif
 }
 
 static int check_name_constraints(X509_STORE_CTX *ctx)
 {
     X509 *x;
     int i, j, rv;
     /* Check name constraints for all certificates */
     for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
         x = sk_X509_value(ctx->chain, i);
         /* Ignore self issued certs unless last in chain */
         if (i && (x->ex_flags & EXFLAG_SI))
             continue;
         /*
          * Check against constraints for all certificates higher in chain
          * including trust anchor. Trust anchor not strictly speaking needed
          * but if it includes constraints it is to be assumed it expects them
          * to be obeyed.
          */
         for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
             NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
             if (nc) {
                 rv = NAME_CONSTRAINTS_check(x, nc);
                 if (rv != X509_V_OK) {
                     ctx->error = rv;
                     ctx->error_depth = i;
                     ctx->current_cert = x;
                     if (!ctx->verify_cb(0, ctx))
                         return 0;
                 }
             }
         }
     }
     return 1;
 }
 
 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
 {
     ctx->error = errcode;
     ctx->current_cert = ctx->cert;
     ctx->error_depth = 0;
     return ctx->verify_cb(0, ctx);
 }
 
 static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
 {
     int i;
     int n = sk_OPENSSL_STRING_num(id->hosts);
     char *name;
 
     if (id->peername != NULL) {
         OPENSSL_free(id->peername);
         id->peername = NULL;
     }
     for (i = 0; i < n; ++i) {
         name = sk_OPENSSL_STRING_value(id->hosts, i);
         if (X509_check_host(x, name, 0, id->hostflags, &id->peername) > 0)
             return 1;
     }
     return n == 0;
 }
 
 static int check_id(X509_STORE_CTX *ctx)
 {
     X509_VERIFY_PARAM *vpm = ctx->param;
     X509_VERIFY_PARAM_ID *id = vpm->id;
     X509 *x = ctx->cert;
     if (id->hosts && check_hosts(x, id) <= 0) {
         if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
             return 0;
     }
     if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
         if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
             return 0;
     }
     if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
         if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
             return 0;
     }
     return 1;
 }
 
 static int check_trust(X509_STORE_CTX *ctx)
 {
     int i, ok;
     X509 *x = NULL;
     int (*cb) (int xok, X509_STORE_CTX *xctx);
     cb = ctx->verify_cb;
     /* Check all trusted certificates in chain */
     for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
         x = sk_X509_value(ctx->chain, i);
         ok = X509_check_trust(x, ctx->param->trust, 0);
         /* If explicitly trusted return trusted */
         if (ok == X509_TRUST_TRUSTED)
             return X509_TRUST_TRUSTED;
         /*
          * If explicitly rejected notify callback and reject if not
          * overridden.
          */
         if (ok == X509_TRUST_REJECTED) {
             ctx->error_depth = i;
             ctx->current_cert = x;
             ctx->error = X509_V_ERR_CERT_REJECTED;
             ok = cb(0, ctx);
             if (!ok)
                 return X509_TRUST_REJECTED;
         }
     }
     /*
      * If we accept partial chains and have at least one trusted certificate
      * return success.
      */
     if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
         X509 *mx;
         if (ctx->last_untrusted < sk_X509_num(ctx->chain))
             return X509_TRUST_TRUSTED;
         x = sk_X509_value(ctx->chain, 0);
         mx = lookup_cert_match(ctx, x);
         if (mx) {
             (void)sk_X509_set(ctx->chain, 0, mx);
             X509_free(x);
             ctx->last_untrusted = 0;
             return X509_TRUST_TRUSTED;
         }
     }
 
     /*
      * If no trusted certs in chain at all return untrusted and allow
      * standard (no issuer cert) etc errors to be indicated.
      */
     return X509_TRUST_UNTRUSTED;
 }
 
 static int check_revocation(X509_STORE_CTX *ctx)
 {
     int i, last, ok;
     if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
         return 1;
     if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
         last = sk_X509_num(ctx->chain) - 1;
     else {
         /* If checking CRL paths this isn't the EE certificate */
         if (ctx->parent)
             return 1;
         last = 0;
     }
     for (i = 0; i <= last; i++) {
         ctx->error_depth = i;
         ok = check_cert(ctx);
         if (!ok)
             return ok;
     }
     return 1;
 }
 
 static int check_cert(X509_STORE_CTX *ctx)
 {
     X509_CRL *crl = NULL, *dcrl = NULL;
     X509 *x;
     int ok, cnum;
     unsigned int last_reasons;
     cnum = ctx->error_depth;
     x = sk_X509_value(ctx->chain, cnum);
     ctx->current_cert = x;
     ctx->current_issuer = NULL;
     ctx->current_crl_score = 0;
     ctx->current_reasons = 0;
     while (ctx->current_reasons != CRLDP_ALL_REASONS) {
         last_reasons = ctx->current_reasons;
         /* Try to retrieve relevant CRL */
         if (ctx->get_crl)
             ok = ctx->get_crl(ctx, &crl, x);
         else
             ok = get_crl_delta(ctx, &crl, &dcrl, x);
         /*
          * If error looking up CRL, nothing we can do except notify callback
          */
         if (!ok) {
             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
             ok = ctx->verify_cb(0, ctx);
             goto err;
         }
         ctx->current_crl = crl;
         ok = ctx->check_crl(ctx, crl);
         if (!ok)
             goto err;
 
         if (dcrl) {
             ok = ctx->check_crl(ctx, dcrl);
             if (!ok)
                 goto err;
             ok = ctx->cert_crl(ctx, dcrl, x);
             if (!ok)
                 goto err;
         } else
             ok = 1;
 
         /* Don't look in full CRL if delta reason is removefromCRL */
         if (ok != 2) {
             ok = ctx->cert_crl(ctx, crl, x);
             if (!ok)
                 goto err;
         }
 
         X509_CRL_free(crl);
         X509_CRL_free(dcrl);
         crl = NULL;
         dcrl = NULL;
         /*
          * If reasons not updated we wont get anywhere by another iteration,
          * so exit loop.
          */
         if (last_reasons == ctx->current_reasons) {
             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
             ok = ctx->verify_cb(0, ctx);
             goto err;
         }
     }
  err:
     X509_CRL_free(crl);
     X509_CRL_free(dcrl);
 
     ctx->current_crl = NULL;
     return ok;
 
 }
 
 /* Check CRL times against values in X509_STORE_CTX */
 
 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
 {
     time_t *ptime;
     int i;
     if (notify)
         ctx->current_crl = crl;
     if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
         ptime = &ctx->param->check_time;
     else
         ptime = NULL;
 
     i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
     if (i == 0) {
         if (!notify)
             return 0;
         ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     if (i > 0) {
         if (!notify)
             return 0;
         ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     if (X509_CRL_get_nextUpdate(crl)) {
         i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
 
         if (i == 0) {
             if (!notify)
                 return 0;
             ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
             if (!ctx->verify_cb(0, ctx))
                 return 0;
         }
         /* Ignore expiry of base CRL is delta is valid */
         if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
             if (!notify)
                 return 0;
             ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
             if (!ctx->verify_cb(0, ctx))
                 return 0;
         }
     }
 
     if (notify)
         ctx->current_crl = NULL;
 
     return 1;
 }
 
 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
                       X509 **pissuer, int *pscore, unsigned int *preasons,
                       STACK_OF(X509_CRL) *crls)
 {
     int i, crl_score, best_score = *pscore;
     unsigned int reasons, best_reasons = 0;
     X509 *x = ctx->current_cert;
     X509_CRL *crl, *best_crl = NULL;
     X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
 
     for (i = 0; i < sk_X509_CRL_num(crls); i++) {
         crl = sk_X509_CRL_value(crls, i);
         reasons = *preasons;
         crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
 
         if (crl_score > best_score) {
             best_crl = crl;
             best_crl_issuer = crl_issuer;
             best_score = crl_score;
             best_reasons = reasons;
         }
     }
 
     if (best_crl) {
         if (*pcrl)
             X509_CRL_free(*pcrl);
         *pcrl = best_crl;
         *pissuer = best_crl_issuer;
         *pscore = best_score;
         *preasons = best_reasons;
         CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
         if (*pdcrl) {
             X509_CRL_free(*pdcrl);
             *pdcrl = NULL;
         }
         get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
     }
 
     if (best_score >= CRL_SCORE_VALID)
         return 1;
 
     return 0;
 }
 
 /*
  * Compare two CRL extensions for delta checking purposes. They should be
  * both present or both absent. If both present all fields must be identical.
  */
 
 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
 {
     ASN1_OCTET_STRING *exta, *extb;
     int i;
     i = X509_CRL_get_ext_by_NID(a, nid, -1);
     if (i >= 0) {
         /* Can't have multiple occurrences */
         if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
             return 0;
         exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
     } else
         exta = NULL;
 
     i = X509_CRL_get_ext_by_NID(b, nid, -1);
 
     if (i >= 0) {
 
         if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
             return 0;
         extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
     } else
         extb = NULL;
 
     if (!exta && !extb)
         return 1;
 
     if (!exta || !extb)
         return 0;
 
     if (ASN1_OCTET_STRING_cmp(exta, extb))
         return 0;
 
     return 1;
 }
 
 /* See if a base and delta are compatible */
 
 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
 {
     /* Delta CRL must be a delta */
     if (!delta->base_crl_number)
         return 0;
     /* Base must have a CRL number */
     if (!base->crl_number)
         return 0;
     /* Issuer names must match */
     if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
         return 0;
     /* AKID and IDP must match */
     if (!crl_extension_match(delta, base, NID_authority_key_identifier))
         return 0;
     if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
         return 0;
     /* Delta CRL base number must not exceed Full CRL number. */
     if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
         return 0;
     /* Delta CRL number must exceed full CRL number */
     if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
         return 1;
     return 0;
 }
 
 /*
  * For a given base CRL find a delta... maybe extend to delta scoring or
  * retrieve a chain of deltas...
  */
 
 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
                          X509_CRL *base, STACK_OF(X509_CRL) *crls)
 {
     X509_CRL *delta;
     int i;
     if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
         return;
     if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
         return;
     for (i = 0; i < sk_X509_CRL_num(crls); i++) {
         delta = sk_X509_CRL_value(crls, i);
         if (check_delta_base(delta, base)) {
             if (check_crl_time(ctx, delta, 0))
                 *pscore |= CRL_SCORE_TIME_DELTA;
             CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
             *dcrl = delta;
             return;
         }
     }
     *dcrl = NULL;
 }
 
 /*
  * For a given CRL return how suitable it is for the supplied certificate
  * 'x'. The return value is a mask of several criteria. If the issuer is not
  * the certificate issuer this is returned in *pissuer. The reasons mask is
  * also used to determine if the CRL is suitable: if no new reasons the CRL
  * is rejected, otherwise reasons is updated.
  */
 
 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
                          unsigned int *preasons, X509_CRL *crl, X509 *x)
 {
 
     int crl_score = 0;
     unsigned int tmp_reasons = *preasons, crl_reasons;
 
     /* First see if we can reject CRL straight away */
 
     /* Invalid IDP cannot be processed */
     if (crl->idp_flags & IDP_INVALID)
         return 0;
     /* Reason codes or indirect CRLs need extended CRL support */
     if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
         if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
             return 0;
     } else if (crl->idp_flags & IDP_REASONS) {
         /* If no new reasons reject */
         if (!(crl->idp_reasons & ~tmp_reasons))
             return 0;
     }
     /* Don't process deltas at this stage */
     else if (crl->base_crl_number)
         return 0;
     /* If issuer name doesn't match certificate need indirect CRL */
     if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
         if (!(crl->idp_flags & IDP_INDIRECT))
             return 0;
     } else
         crl_score |= CRL_SCORE_ISSUER_NAME;
 
     if (!(crl->flags & EXFLAG_CRITICAL))
         crl_score |= CRL_SCORE_NOCRITICAL;
 
     /* Check expiry */
     if (check_crl_time(ctx, crl, 0))
         crl_score |= CRL_SCORE_TIME;
 
     /* Check authority key ID and locate certificate issuer */
     crl_akid_check(ctx, crl, pissuer, &crl_score);
 
     /* If we can't locate certificate issuer at this point forget it */
 
     if (!(crl_score & CRL_SCORE_AKID))
         return 0;
 
     /* Check cert for matching CRL distribution points */
 
     if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
         /* If no new reasons reject */
         if (!(crl_reasons & ~tmp_reasons))
             return 0;
         tmp_reasons |= crl_reasons;
         crl_score |= CRL_SCORE_SCOPE;
     }
 
     *preasons = tmp_reasons;
 
     return crl_score;
 
 }
 
 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
                            X509 **pissuer, int *pcrl_score)
 {
     X509 *crl_issuer = NULL;
     X509_NAME *cnm = X509_CRL_get_issuer(crl);
     int cidx = ctx->error_depth;
     int i;
 
     if (cidx != sk_X509_num(ctx->chain) - 1)
         cidx++;
 
     crl_issuer = sk_X509_value(ctx->chain, cidx);
 
     if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
         if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
             *pissuer = crl_issuer;
             return;
         }
     }
 
     for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
         crl_issuer = sk_X509_value(ctx->chain, cidx);
         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
             continue;
         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
             *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
             *pissuer = crl_issuer;
             return;
         }
     }
 
     /* Anything else needs extended CRL support */
 
     if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
         return;
 
     /*
      * Otherwise the CRL issuer is not on the path. Look for it in the set of
      * untrusted certificates.
      */
     for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
         crl_issuer = sk_X509_value(ctx->untrusted, i);
         if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
             continue;
         if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
             *pissuer = crl_issuer;
             *pcrl_score |= CRL_SCORE_AKID;
             return;
         }
     }
 }
 
 /*
  * Check the path of a CRL issuer certificate. This creates a new
  * X509_STORE_CTX and populates it with most of the parameters from the
  * parent. This could be optimised somewhat since a lot of path checking will
  * be duplicated by the parent, but this will rarely be used in practice.
  */
 
 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
 {
     X509_STORE_CTX crl_ctx;
     int ret;
     /* Don't allow recursive CRL path validation */
     if (ctx->parent)
         return 0;
     if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
         return -1;
 
     crl_ctx.crls = ctx->crls;
     /* Copy verify params across */
     X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
 
     crl_ctx.parent = ctx;
     crl_ctx.verify_cb = ctx->verify_cb;
 
     /* Verify CRL issuer */
     ret = X509_verify_cert(&crl_ctx);
 
     if (ret <= 0)
         goto err;
 
     /* Check chain is acceptable */
 
     ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  err:
     X509_STORE_CTX_cleanup(&crl_ctx);
     return ret;
 }
 
 /*
  * RFC3280 says nothing about the relationship between CRL path and
  * certificate path, which could lead to situations where a certificate could
  * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  * strict and states that the two paths must end in the same trust anchor,
  * though some discussions remain... until this is resolved we use the
  * RFC5280 version
  */
 
 static int check_crl_chain(X509_STORE_CTX *ctx,
                            STACK_OF(X509) *cert_path,
                            STACK_OF(X509) *crl_path)
 {
     X509 *cert_ta, *crl_ta;
     cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
     crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
     if (!X509_cmp(cert_ta, crl_ta))
         return 1;
     return 0;
 }
 
 /*-
  * Check for match between two dist point names: three separate cases.
  * 1. Both are relative names and compare X509_NAME types.
  * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
  * 3. Both are full names and compare two GENERAL_NAMES.
  * 4. One is NULL: automatic match.
  */
 
 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
 {
     X509_NAME *nm = NULL;
     GENERAL_NAMES *gens = NULL;
     GENERAL_NAME *gena, *genb;
     int i, j;
     if (!a || !b)
         return 1;
     if (a->type == 1) {
         if (!a->dpname)
             return 0;
         /* Case 1: two X509_NAME */
         if (b->type == 1) {
             if (!b->dpname)
                 return 0;
             if (!X509_NAME_cmp(a->dpname, b->dpname))
                 return 1;
             else
                 return 0;
         }
         /* Case 2: set name and GENERAL_NAMES appropriately */
         nm = a->dpname;
         gens = b->name.fullname;
     } else if (b->type == 1) {
         if (!b->dpname)
             return 0;
         /* Case 2: set name and GENERAL_NAMES appropriately */
         gens = a->name.fullname;
         nm = b->dpname;
     }
 
     /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
     if (nm) {
         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
             gena = sk_GENERAL_NAME_value(gens, i);
             if (gena->type != GEN_DIRNAME)
                 continue;
             if (!X509_NAME_cmp(nm, gena->d.directoryName))
                 return 1;
         }
         return 0;
     }
 
     /* Else case 3: two GENERAL_NAMES */
 
     for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
         gena = sk_GENERAL_NAME_value(a->name.fullname, i);
         for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
             genb = sk_GENERAL_NAME_value(b->name.fullname, j);
             if (!GENERAL_NAME_cmp(gena, genb))
                 return 1;
         }
     }
 
     return 0;
 
 }
 
 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
 {
     int i;
     X509_NAME *nm = X509_CRL_get_issuer(crl);
     /* If no CRLissuer return is successful iff don't need a match */
     if (!dp->CRLissuer)
         return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
     for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
         GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
         if (gen->type != GEN_DIRNAME)
             continue;
         if (!X509_NAME_cmp(gen->d.directoryName, nm))
             return 1;
     }
     return 0;
 }
 
 /* Check CRLDP and IDP */
 
 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
                            unsigned int *preasons)
 {
     int i;
     if (crl->idp_flags & IDP_ONLYATTR)
         return 0;
     if (x->ex_flags & EXFLAG_CA) {
         if (crl->idp_flags & IDP_ONLYUSER)
             return 0;
     } else {
         if (crl->idp_flags & IDP_ONLYCA)
             return 0;
     }
     *preasons = crl->idp_reasons;
     for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
         DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
         if (crldp_check_crlissuer(dp, crl, crl_score)) {
             if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
                 *preasons &= dp->dp_reasons;
                 return 1;
             }
         }
     }
     if ((!crl->idp || !crl->idp->distpoint)
         && (crl_score & CRL_SCORE_ISSUER_NAME))
         return 1;
     return 0;
 }
 
 /*
  * Retrieve CRL corresponding to current certificate. If deltas enabled try
  * to find a delta CRL too
  */
 
 static int get_crl_delta(X509_STORE_CTX *ctx,
                          X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
 {
     int ok;
     X509 *issuer = NULL;
     int crl_score = 0;
     unsigned int reasons;
     X509_CRL *crl = NULL, *dcrl = NULL;
     STACK_OF(X509_CRL) *skcrl;
     X509_NAME *nm = X509_get_issuer_name(x);
     reasons = ctx->current_reasons;
     ok = get_crl_sk(ctx, &crl, &dcrl,
                     &issuer, &crl_score, &reasons, ctx->crls);
 
     if (ok)
         goto done;
 
     /* Lookup CRLs from store */
 
     skcrl = ctx->lookup_crls(ctx, nm);
 
     /* If no CRLs found and a near match from get_crl_sk use that */
     if (!skcrl && crl)
         goto done;
 
     get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
 
     sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
 
  done:
 
     /* If we got any kind of CRL use it and return success */
     if (crl) {
         ctx->current_issuer = issuer;
         ctx->current_crl_score = crl_score;
         ctx->current_reasons = reasons;
         *pcrl = crl;
         *pdcrl = dcrl;
         return 1;
     }
 
     return 0;
 }
 
 /* Check CRL validity */
 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
 {
     X509 *issuer = NULL;
     EVP_PKEY *ikey = NULL;
     int ok = 0, chnum, cnum;
     cnum = ctx->error_depth;
     chnum = sk_X509_num(ctx->chain) - 1;
     /* if we have an alternative CRL issuer cert use that */
     if (ctx->current_issuer)
         issuer = ctx->current_issuer;
 
     /*
      * Else find CRL issuer: if not last certificate then issuer is next
      * certificate in chain.
      */
     else if (cnum < chnum)
         issuer = sk_X509_value(ctx->chain, cnum + 1);
     else {
         issuer = sk_X509_value(ctx->chain, chnum);
         /* If not self signed, can't check signature */
         if (!ctx->check_issued(ctx, issuer, issuer)) {
             ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
             ok = ctx->verify_cb(0, ctx);
             if (!ok)
                 goto err;
         }
     }
 
     if (issuer) {
         /*
          * Skip most tests for deltas because they have already been done
          */
         if (!crl->base_crl_number) {
             /* Check for cRLSign bit if keyUsage present */
             if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
                 !(issuer->ex_kusage & KU_CRL_SIGN)) {
                 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
                 ok = ctx->verify_cb(0, ctx);
                 if (!ok)
                     goto err;
             }
 
             if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
                 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
                 ok = ctx->verify_cb(0, ctx);
                 if (!ok)
                     goto err;
             }
 
             if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
                 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
                     ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
                     ok = ctx->verify_cb(0, ctx);
                     if (!ok)
                         goto err;
                 }
             }
 
             if (crl->idp_flags & IDP_INVALID) {
                 ctx->error = X509_V_ERR_INVALID_EXTENSION;
                 ok = ctx->verify_cb(0, ctx);
                 if (!ok)
                     goto err;
             }
 
         }
 
         if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
             ok = check_crl_time(ctx, crl, 1);
             if (!ok)
                 goto err;
         }
 
         /* Attempt to get issuer certificate public key */
         ikey = X509_get_pubkey(issuer);
 
         if (!ikey) {
             ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
             ok = ctx->verify_cb(0, ctx);
             if (!ok)
                 goto err;
         } else {
             int rv;
             rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
             if (rv != X509_V_OK) {
                 ctx->error = rv;
                 ok = ctx->verify_cb(0, ctx);
                 if (!ok)
                     goto err;
             }
             /* Verify CRL signature */
             if (X509_CRL_verify(crl, ikey) <= 0) {
                 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
                 ok = ctx->verify_cb(0, ctx);
                 if (!ok)
                     goto err;
             }
         }
     }
 
     ok = 1;
 
  err:
     EVP_PKEY_free(ikey);
     return ok;
 }
 
 /* Check certificate against CRL */
 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
 {
     int ok;
     X509_REVOKED *rev;
     /*
      * The rules changed for this... previously if a CRL contained unhandled
      * critical extensions it could still be used to indicate a certificate
      * was revoked. This has since been changed since critical extension can
      * change the meaning of CRL entries.
      */
     if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
         && (crl->flags & EXFLAG_CRITICAL)) {
         ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
         ok = ctx->verify_cb(0, ctx);
         if (!ok)
             return 0;
     }
     /*
      * Look for serial number of certificate in CRL If found make sure reason
      * is not removeFromCRL.
      */
     if (X509_CRL_get0_by_cert(crl, &rev, x)) {
         if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
             return 2;
         ctx->error = X509_V_ERR_CERT_REVOKED;
         ok = ctx->verify_cb(0, ctx);
         if (!ok)
             return 0;
     }
 
     return 1;
 }
 
 static int check_policy(X509_STORE_CTX *ctx)
 {
     int ret;
     if (ctx->parent)
         return 1;
     ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
                             ctx->param->policies, ctx->param->flags);
     if (ret == 0) {
         X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
         return 0;
     }
     /* Invalid or inconsistent extensions */
     if (ret == -1) {
         /*
          * Locate certificates with bad extensions and notify callback.
          */
         X509 *x;
         int i;
         for (i = 1; i < sk_X509_num(ctx->chain); i++) {
             x = sk_X509_value(ctx->chain, i);
             if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
                 continue;
             ctx->current_cert = x;
             ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
             if (!ctx->verify_cb(0, ctx))
                 return 0;
         }
         return 1;
     }
     if (ret == -2) {
         ctx->current_cert = NULL;
         ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
         return ctx->verify_cb(0, ctx);
     }
 
     if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
         ctx->current_cert = NULL;
         ctx->error = X509_V_OK;
         if (!ctx->verify_cb(2, ctx))
             return 0;
     }
 
     return 1;
 }
 
 static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
 {
     time_t *ptime;
     int i;
 
     if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
         ptime = &ctx->param->check_time;
     else
         ptime = NULL;
 
     i = X509_cmp_time(X509_get_notBefore(x), ptime);
     if (i == 0) {
         ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
         ctx->current_cert = x;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     if (i > 0) {
         ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
         ctx->current_cert = x;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     i = X509_cmp_time(X509_get_notAfter(x), ptime);
     if (i == 0) {
         ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
         ctx->current_cert = x;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     if (i < 0) {
         ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
         ctx->current_cert = x;
         if (!ctx->verify_cb(0, ctx))
             return 0;
     }
 
     return 1;
 }
 
 static int internal_verify(X509_STORE_CTX *ctx)
 {
     int ok = 0, n;
     X509 *xs, *xi;
     EVP_PKEY *pkey = NULL;
     int (*cb) (int xok, X509_STORE_CTX *xctx);
 
     cb = ctx->verify_cb;
 
     n = sk_X509_num(ctx->chain);
     ctx->error_depth = n - 1;
     n--;
     xi = sk_X509_value(ctx->chain, n);
 
     if (ctx->check_issued(ctx, xi, xi))
         xs = xi;
     else {
         if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
             xs = xi;
             goto check_cert;
         }
         if (n <= 0) {
             ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
             ctx->current_cert = xi;
             ok = cb(0, ctx);
             goto end;
         } else {
             n--;
             ctx->error_depth = n;
             xs = sk_X509_value(ctx->chain, n);
         }
     }
 
 /*      ctx->error=0;  not needed */
     while (n >= 0) {
         ctx->error_depth = n;
 
         /*
          * Skip signature check for self signed certificates unless
          * explicitly asked for. It doesn't add any security and just wastes
          * time.
          */
         if (!xs->valid
             && (xs != xi
                 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
             if ((pkey = X509_get_pubkey(xi)) == NULL) {
                 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
                 ctx->current_cert = xi;
                 ok = (*cb) (0, ctx);
                 if (!ok)
                     goto end;
             } else if (X509_verify(xs, pkey) <= 0) {
                 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
                 ctx->current_cert = xs;
                 ok = (*cb) (0, ctx);
                 if (!ok) {
                     EVP_PKEY_free(pkey);
                     goto end;
                 }
             }
             EVP_PKEY_free(pkey);
             pkey = NULL;
         }
 
         xs->valid = 1;
 
  check_cert:
         ok = check_cert_time(ctx, xs);
         if (!ok)
             goto end;
 
         /* The last error (if any) is still in the error value */
         ctx->current_issuer = xi;
         ctx->current_cert = xs;
         ok = (*cb) (1, ctx);
         if (!ok)
             goto end;
 
         n--;
         if (n >= 0) {
             xi = xs;
             xs = sk_X509_value(ctx->chain, n);
         }
     }
     ok = 1;
  end:
     return ok;
 }
 
 int X509_cmp_current_time(const ASN1_TIME *ctm)
 {
     return X509_cmp_time(ctm, NULL);
 }
 
 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
 {
     char *str;
     ASN1_TIME atm;
     long offset;
     char buff1[24], buff2[24], *p;
     int i, j, remaining;
 
     p = buff1;
     remaining = ctm->length;
     str = (char *)ctm->data;
     /*
      * Note that the following (historical) code allows much more slack in the
      * time format than RFC5280. In RFC5280, the representation is fixed:
      * UTCTime: YYMMDDHHMMSSZ
      * GeneralizedTime: YYYYMMDDHHMMSSZ
      */
     if (ctm->type == V_ASN1_UTCTIME) {
         /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
         int min_length = sizeof("YYMMDDHHMMZ") - 1;
         int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
         if (remaining < min_length || remaining > max_length)
             return 0;
         memcpy(p, str, 10);
         p += 10;
         str += 10;
         remaining -= 10;
     } else {
         /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
         int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
         int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
         if (remaining < min_length || remaining > max_length)
             return 0;
         memcpy(p, str, 12);
         p += 12;
         str += 12;
         remaining -= 12;
     }
 
     if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
         *(p++) = '0';
         *(p++) = '0';
     } else {
         /* SS (seconds) */
         if (remaining < 2)
             return 0;
         *(p++) = *(str++);
         *(p++) = *(str++);
         remaining -= 2;
         /*
          * Skip any (up to three) fractional seconds...
          * TODO(emilia): in RFC5280, fractional seconds are forbidden.
          * Can we just kill them altogether?
          */
         if (remaining && *str == '.') {
             str++;
             remaining--;
             for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
                 if (*str < '0' || *str > '9')
                     break;
             }
         }
 
     }
     *(p++) = 'Z';
     *(p++) = '\0';
 
     /* We now need either a terminating 'Z' or an offset. */
     if (!remaining)
         return 0;
     if (*str == 'Z') {
         if (remaining != 1)
             return 0;
         offset = 0;
     } else {
         /* (+-)HHMM */
         if ((*str != '+') && (*str != '-'))
             return 0;
         /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
         if (remaining != 5)
             return 0;
         if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
             str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
             return 0;
         offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
         offset += (str[3] - '0') * 10 + (str[4] - '0');
         if (*str == '-')
             offset = -offset;
     }
     atm.type = ctm->type;
     atm.flags = 0;
     atm.length = sizeof(buff2);
     atm.data = (unsigned char *)buff2;
 
     if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
         return 0;
 
     if (ctm->type == V_ASN1_UTCTIME) {
         i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
         if (i < 50)
             i += 100;           /* cf. RFC 2459 */
         j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
         if (j < 50)
             j += 100;
 
         if (i < j)
             return -1;
         if (i > j)
             return 1;
     }
     i = strcmp(buff1, buff2);
     if (i == 0)                 /* wait a second then return younger :-) */
         return -1;
     else
         return i;
 }
 
 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
 {
     return X509_time_adj(s, adj, NULL);
 }
 
 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
 {
     return X509_time_adj_ex(s, 0, offset_sec, in_tm);
 }
 
 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
                             int offset_day, long offset_sec, time_t *in_tm)
 {
     time_t t;
 
     if (in_tm)
         t = *in_tm;
     else
         time(&t);
 
     if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
         if (s->type == V_ASN1_UTCTIME)
             return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
         if (s->type == V_ASN1_GENERALIZEDTIME)
             return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
     }
     return ASN1_TIME_adj(s, t, offset_day, offset_sec);
 }
 
 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
 {
     EVP_PKEY *ktmp = NULL, *ktmp2;
     int i, j;
 
     if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
         return 1;
 
     for (i = 0; i < sk_X509_num(chain); i++) {
         ktmp = X509_get_pubkey(sk_X509_value(chain, i));
         if (ktmp == NULL) {
             X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
                     X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
             return 0;
         }
         if (!EVP_PKEY_missing_parameters(ktmp))
             break;
         else {
             EVP_PKEY_free(ktmp);
             ktmp = NULL;
         }
     }
     if (ktmp == NULL) {
         X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
                 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
         return 0;
     }
 
     /* first, populate the other certs */
     for (j = i - 1; j >= 0; j--) {
         ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
         EVP_PKEY_copy_parameters(ktmp2, ktmp);
         EVP_PKEY_free(ktmp2);
     }
 
     if (pkey != NULL)
         EVP_PKEY_copy_parameters(pkey, ktmp);
     EVP_PKEY_free(ktmp);
     return 1;
 }
 
 /* Make a delta CRL as the diff between two full CRLs */
 
 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
                         EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
 {
     X509_CRL *crl = NULL;
     int i;
     STACK_OF(X509_REVOKED) *revs = NULL;
     /* CRLs can't be delta already */
     if (base->base_crl_number || newer->base_crl_number) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
         return NULL;
     }
     /* Base and new CRL must have a CRL number */
     if (!base->crl_number || !newer->crl_number) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
         return NULL;
     }
     /* Issuer names must match */
     if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
         return NULL;
     }
     /* AKID and IDP must match */
     if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
         return NULL;
     }
     if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
         return NULL;
     }
     /* Newer CRL number must exceed full CRL number */
     if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
         return NULL;
     }
     /* CRLs must verify */
     if (skey && (X509_CRL_verify(base, skey) <= 0 ||
                  X509_CRL_verify(newer, skey) <= 0)) {
         X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
         return NULL;
     }
     /* Create new CRL */
     crl = X509_CRL_new();
     if (!crl || !X509_CRL_set_version(crl, 1))
         goto memerr;
     /* Set issuer name */
     if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
         goto memerr;
 
     if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
         goto memerr;
     if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
         goto memerr;
 
     /* Set base CRL number: must be critical */
 
     if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
         goto memerr;
 
     /*
      * Copy extensions across from newest CRL to delta: this will set CRL
      * number to correct value too.
      */
 
     for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
         X509_EXTENSION *ext;
         ext = X509_CRL_get_ext(newer, i);
         if (!X509_CRL_add_ext(crl, ext, -1))
             goto memerr;
     }
 
     /* Go through revoked entries, copying as needed */
 
     revs = X509_CRL_get_REVOKED(newer);
 
     for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
         X509_REVOKED *rvn, *rvtmp;
         rvn = sk_X509_REVOKED_value(revs, i);
         /*
          * Add only if not also in base. TODO: need something cleverer here
          * for some more complex CRLs covering multiple CAs.
          */
         if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
             rvtmp = X509_REVOKED_dup(rvn);
             if (!rvtmp)
                 goto memerr;
             if (!X509_CRL_add0_revoked(crl, rvtmp)) {
                 X509_REVOKED_free(rvtmp);
                 goto memerr;
             }
         }
     }
     /* TODO: optionally prune deleted entries */
 
     if (skey && md && !X509_CRL_sign(crl, skey, md))
         goto memerr;
 
     return crl;
 
  memerr:
     X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
     if (crl)
         X509_CRL_free(crl);
     return NULL;
 }
 
 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
                                     CRYPTO_EX_new *new_func,
                                     CRYPTO_EX_dup *dup_func,
                                     CRYPTO_EX_free *free_func)
 {
     /*
      * This function is (usually) called only once, by
      * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
      */
     return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
                                    new_func, dup_func, free_func);
 }
 
 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
 {
     return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
 }
 
 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
 {
     return CRYPTO_get_ex_data(&ctx->ex_data, idx);
 }
 
 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
 {
     return ctx->error;
 }
 
 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
 {
     ctx->error = err;
 }
 
 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
 {
     return ctx->error_depth;
 }
 
 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
 {
     return ctx->current_cert;
 }
 
 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
 {
     return ctx->chain;
 }
 
 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
 {
     if (!ctx->chain)
         return NULL;
     return X509_chain_up_ref(ctx->chain);
 }
 
 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
 {
     return ctx->current_issuer;
 }
 
 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
 {
     return ctx->current_crl;
 }
 
 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
 {
     return ctx->parent;
 }
 
 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
 {
     ctx->cert = x;
 }
 
 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
 {
     ctx->untrusted = sk;
 }
 
 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
 {
     ctx->crls = sk;
 }
 
 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
 {
     return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
 }
 
 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
 {
     return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
 }
 
 /*
  * This function is used to set the X509_STORE_CTX purpose and trust values.
  * This is intended to be used when another structure has its own trust and
  * purpose values which (if set) will be inherited by the ctx. If they aren't
  * set then we will usually have a default purpose in mind which should then
  * be used to set the trust value. An example of this is SSL use: an SSL
  * structure will have its own purpose and trust settings which the
  * application can set: if they aren't set then we use the default of SSL
  * client/server.
  */
 
 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
                                    int purpose, int trust)
 {
     int idx;
     /* If purpose not set use default */
     if (!purpose)
         purpose = def_purpose;
     /* If we have a purpose then check it is valid */
     if (purpose) {
         X509_PURPOSE *ptmp;
         idx = X509_PURPOSE_get_by_id(purpose);
         if (idx == -1) {
             X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                     X509_R_UNKNOWN_PURPOSE_ID);
             return 0;
         }
         ptmp = X509_PURPOSE_get0(idx);
         if (ptmp->trust == X509_TRUST_DEFAULT) {
             idx = X509_PURPOSE_get_by_id(def_purpose);
             if (idx == -1) {
                 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                         X509_R_UNKNOWN_PURPOSE_ID);
                 return 0;
             }
             ptmp = X509_PURPOSE_get0(idx);
         }
         /* If trust not set then get from purpose default */
         if (!trust)
             trust = ptmp->trust;
     }
     if (trust) {
         idx = X509_TRUST_get_by_id(trust);
         if (idx == -1) {
             X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
                     X509_R_UNKNOWN_TRUST_ID);
             return 0;
         }
     }
 
     if (purpose && !ctx->param->purpose)
         ctx->param->purpose = purpose;
     if (trust && !ctx->param->trust)
         ctx->param->trust = trust;
     return 1;
 }
 
 X509_STORE_CTX *X509_STORE_CTX_new(void)
 {
     X509_STORE_CTX *ctx;
     ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
     if (!ctx) {
         X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
         return NULL;
     }
     memset(ctx, 0, sizeof(X509_STORE_CTX));
     return ctx;
 }
 
 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
 {
     if (!ctx)
         return;
     X509_STORE_CTX_cleanup(ctx);
     OPENSSL_free(ctx);
 }
 
 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
                         STACK_OF(X509) *chain)
 {
     int ret = 1;
     ctx->ctx = store;
     ctx->current_method = 0;
     ctx->cert = x509;
     ctx->untrusted = chain;
     ctx->crls = NULL;
     ctx->last_untrusted = 0;
     ctx->other_ctx = NULL;
     ctx->valid = 0;
     ctx->chain = NULL;
     ctx->error = 0;
     ctx->explicit_policy = 0;
     ctx->error_depth = 0;
     ctx->current_cert = NULL;
     ctx->current_issuer = NULL;
     ctx->current_crl = NULL;
     ctx->current_crl_score = 0;
     ctx->current_reasons = 0;
     ctx->tree = NULL;
     ctx->parent = NULL;
     /* Zero ex_data to make sure we're cleanup-safe */
     memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
 
     ctx->param = X509_VERIFY_PARAM_new();
     if (!ctx->param) {
         X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
         return 0;
     }
 
     /*
      * Inherit callbacks and flags from X509_STORE if not set use defaults.
      */
     if (store)
         ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
     else
         ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
 
     if (store) {
         ctx->verify_cb = store->verify_cb;
         /* Seems to always be 0 in OpenSSL, else must be idempotent */
         ctx->cleanup = store->cleanup;
     } else
         ctx->cleanup = 0;
 
     if (ret)
         ret = X509_VERIFY_PARAM_inherit(ctx->param,
                                         X509_VERIFY_PARAM_lookup("default"));
 
     if (ret == 0) {
         X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
         goto err;
     }
 
     if (store && store->check_issued)
         ctx->check_issued = store->check_issued;
     else
         ctx->check_issued = check_issued;
 
     if (store && store->get_issuer)
         ctx->get_issuer = store->get_issuer;
     else
         ctx->get_issuer = X509_STORE_CTX_get1_issuer;
 
     if (store && store->verify_cb)
         ctx->verify_cb = store->verify_cb;
     else
         ctx->verify_cb = null_callback;
 
     if (store && store->verify)
         ctx->verify = store->verify;
     else
         ctx->verify = internal_verify;
 
     if (store && store->check_revocation)
         ctx->check_revocation = store->check_revocation;
     else
         ctx->check_revocation = check_revocation;
 
     if (store && store->get_crl)
         ctx->get_crl = store->get_crl;
     else
         ctx->get_crl = NULL;
 
     if (store && store->check_crl)
         ctx->check_crl = store->check_crl;
     else
         ctx->check_crl = check_crl;
 
     if (store && store->cert_crl)
         ctx->cert_crl = store->cert_crl;
     else
         ctx->cert_crl = cert_crl;
 
     if (store && store->lookup_certs)
         ctx->lookup_certs = store->lookup_certs;
     else
         ctx->lookup_certs = X509_STORE_get1_certs;
 
     if (store && store->lookup_crls)
         ctx->lookup_crls = store->lookup_crls;
     else
         ctx->lookup_crls = X509_STORE_get1_crls;
 
     ctx->check_policy = check_policy;
 
     if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
                            &ctx->ex_data))
         return 1;
     X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
 
  err:
     /*
      * On error clean up allocated storage, if the store context was not
      * allocated with X509_STORE_CTX_new() this is our last chance to do so.
      */
     X509_STORE_CTX_cleanup(ctx);
     return 0;
 }
 
 /*
  * Set alternative lookup method: just a STACK of trusted certificates. This
  * avoids X509_STORE nastiness where it isn't needed.
  */
 
 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
 {
     ctx->other_ctx = sk;
     ctx->get_issuer = get_issuer_sk;
 }
 
 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
 {
     /*
      * We need to be idempotent because, unfortunately, free() also calls
      * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
      * calls cleanup() for the same object twice!  Thus we must zero the
      * pointers below after they're freed!
      */
     /* Seems to always be 0 in OpenSSL, do this at most once. */
     if (ctx->cleanup != NULL) {
         ctx->cleanup(ctx);
         ctx->cleanup = NULL;
     }
     if (ctx->param != NULL) {
         if (ctx->parent == NULL)
             X509_VERIFY_PARAM_free(ctx->param);
         ctx->param = NULL;
     }
     if (ctx->tree != NULL) {
         X509_policy_tree_free(ctx->tree);
         ctx->tree = NULL;
     }
     if (ctx->chain != NULL) {
         sk_X509_pop_free(ctx->chain, X509_free);
         ctx->chain = NULL;
     }
     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
     memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
 }
 
 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
 {
     X509_VERIFY_PARAM_set_depth(ctx->param, depth);
 }
 
 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
 {
     X509_VERIFY_PARAM_set_flags(ctx->param, flags);
 }
 
 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
                              time_t t)
 {
     X509_VERIFY_PARAM_set_time(ctx->param, t);
 }
 
 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
                                   int (*verify_cb) (int, X509_STORE_CTX *))
 {
     ctx->verify_cb = verify_cb;
 }
 
 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
 {
     return ctx->tree;
 }
 
 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
 {
     return ctx->explicit_policy;
 }
 
 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
 {
     const X509_VERIFY_PARAM *param;
     param = X509_VERIFY_PARAM_lookup(name);
     if (!param)
         return 0;
     return X509_VERIFY_PARAM_inherit(ctx->param, param);
 }
 
 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
 {
     return ctx->param;
 }
 
 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
 {
     if (ctx->param)
         X509_VERIFY_PARAM_free(ctx->param);
     ctx->param = param;
 }
 
 IMPLEMENT_STACK_OF(X509)
 
 IMPLEMENT_ASN1_SET_OF(X509)
 
 IMPLEMENT_STACK_OF(X509_NAME)
 
 IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
 
 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)
Index: vendor-crypto/openssl/dist/doc/apps/ciphers.pod
===================================================================
--- vendor-crypto/openssl/dist/doc/apps/ciphers.pod	(revision 296272)
+++ vendor-crypto/openssl/dist/doc/apps/ciphers.pod	(revision 296273)
@@ -1,636 +1,645 @@
 =pod
 
 =head1 NAME
 
 ciphers - SSL cipher display and cipher list tool.
 
 =head1 SYNOPSIS
 
 B B
 [B<-v>]
 [B<-V>]
 [B<-ssl2>]
 [B<-ssl3>]
 [B<-tls1>]
 [B]
 
 =head1 DESCRIPTION
 
 The B command converts textual OpenSSL cipher lists into ordered
 SSL cipher preference lists. It can be used as a test tool to determine
 the appropriate cipherlist.
 
 =head1 COMMAND OPTIONS
 
 =over 4
 
 =item B<-v>
 
 Verbose option. List ciphers with a complete description of
 protocol version (SSLv2 or SSLv3; the latter includes TLS), key exchange,
 authentication, encryption and mac algorithms used along with any key size
 restrictions and whether the algorithm is classed as an "export" cipher.
 Note that without the B<-v> option, ciphers may seem to appear twice
 in a cipher list; this is when similar ciphers are available for
 SSL v2 and for SSL v3/TLS v1.
 
 =item B<-V>
 
 Like B<-v>, but include cipher suite codes in output (hex format).
 
-=item B<-ssl3>
+=item B<-ssl3>, B<-tls1>
 
-only include SSL v3 ciphers.
+This lists ciphers compatible with any of SSLv3, TLSv1, TLSv1.1 or TLSv1.2.
 
 =item B<-ssl2>
 
-only include SSL v2 ciphers.
+Only include SSLv2 ciphers.
 
-=item B<-tls1>
-
-only include TLS v1 ciphers.
-
 =item B<-h>, B<-?>
 
-print a brief usage message.
+Print a brief usage message.
 
 =item B
 
-a cipher list to convert to a cipher preference list. If it is not included
+A cipher list to convert to a cipher preference list. If it is not included
 then the default cipher list will be used. The format is described below.
 
 =back
 
 =head1 CIPHER LIST FORMAT
 
 The cipher list consists of one or more I separated by colons.
 Commas or spaces are also acceptable separators but colons are normally used.
 
 The actual cipher string can take several different forms.
 
 It can consist of a single cipher suite such as B.
 
 It can represent a list of cipher suites containing a certain algorithm, or
 cipher suites of a certain type. For example B represents all ciphers
 suites using the digest algorithm SHA1 and B represents all SSL v3
 algorithms.
 
 Lists of cipher suites can be combined in a single cipher string using the
 B<+> character. This is used as a logical B operation. For example
 B represents all cipher suites containing the SHA1 B the DES
 algorithms.
 
 Each cipher string can be optionally preceded by the characters B,
 B<-> or B<+>.
 
 If B is used then the ciphers are permanently deleted from the list.
 The ciphers deleted can never reappear in the list even if they are
 explicitly stated.
 
 If B<-> is used then the ciphers are deleted from the list, but some or
 all of the ciphers can be added again by later options.
 
 If B<+> is used then the ciphers are moved to the end of the list. This
 option doesn't add any new ciphers it just moves matching existing ones.
 
 If none of these characters is present then the string is just interpreted
 as a list of ciphers to be appended to the current preference list. If the
 list includes any ciphers already present they will be ignored: that is they
 will not moved to the end of the list.
 
 Additionally the cipher string B<@STRENGTH> can be used at any point to sort
 the current cipher list in order of encryption algorithm key length.
 
 =head1 CIPHER STRINGS
 
 The following is a list of all permitted cipher strings and their meanings.
 
 =over 4
 
 =item B
 
-the default cipher list. This is determined at compile time and
-is normally B. This must be the firstcipher string
-specified.
+The default cipher list.
+This is determined at compile time and is normally
+B.
+When used, this must be the first cipherstring specified.
 
 =item B
 
 the ciphers included in B, but not enabled by default. Currently
 this is B and B. Note that this rule does not cover B,
 which is not included by B (use B if necessary).
 
 =item B
 
 all cipher suites except the B ciphers which must be explicitly enabled;
 as of OpenSSL, the B cipher suites are reasonably ordered by default
 
 =item B
 
 the cipher suites not enabled by B, currently being B.
 
 =item B
 
 "high" encryption cipher suites. This currently means those with key lengths larger
 than 128 bits, and some cipher suites with 128-bit keys.
 
 =item B
 
 "medium" encryption cipher suites, currently some of those using 128 bit encryption.
 
 =item B
 
-"low" encryption cipher suites, currently those using 64 or 56 bit encryption algorithms
-but excluding export cipher suites.
+Low strength encryption cipher suites, currently those using 64 or 56 bit
+encryption algorithms but excluding export cipher suites.
+As of OpenSSL 1.0.2g, these are disabled in default builds.
 
 =item B, B
 
-export encryption algorithms. Including 40 and 56 bits algorithms.
+Export strength encryption algorithms. Including 40 and 56 bits algorithms.
+As of OpenSSL 1.0.2g, these are disabled in default builds.
 
 =item B
 
-40 bit export encryption algorithms
+40-bit export encryption algorithms
+As of OpenSSL 1.0.2g, these are disabled in default builds.
 
 =item B
 
-56 bit export encryption algorithms. In OpenSSL 0.9.8c and later the set of
+56-bit export encryption algorithms. In OpenSSL 0.9.8c and later the set of
 56 bit export ciphers is empty unless OpenSSL has been explicitly configured
 with support for experimental ciphers.
+As of OpenSSL 1.0.2g, these are disabled in default builds.
 
 =item B, B
 
-the "NULL" ciphers that is those offering no encryption. Because these offer no
-encryption at all and are a security risk they are disabled unless explicitly
-included.
+The "NULL" ciphers that is those offering no encryption. Because these offer no
+encryption at all and are a security risk they are not enabled via either the
+B or B cipher strings.
+Be careful when building cipherlists out of lower-level primitives such as
+B or B as these do overlap with the B ciphers.
+When in doubt, include B in your cipherlist.
 
 =item B
 
-the cipher suites offering no authentication. This is currently the anonymous
+The cipher suites offering no authentication. This is currently the anonymous
 DH algorithms and anonymous ECDH algorithms. These cipher suites are vulnerable
 to a "man in the middle" attack and so their use is normally discouraged.
+These are excluded from the B ciphers, but included in the B
+ciphers.
+Be careful when building cipherlists out of lower-level primitives such as
+B or B as these do overlap with the B ciphers.
+When in doubt, include B in your cipherlist.
 
 =item B, B
 
 cipher suites using RSA key exchange.
 
 =item B, B, B
 
 cipher suites using DH key agreement and DH certificates signed by CAs with RSA
 and DSS keys or either respectively.
 
 =item B, B
 
 cipher suites using ephemeral DH key agreement, including anonymous cipher
 suites.
 
 =item B, B
 
 cipher suites using authenticated ephemeral DH key agreement.
 
 =item B
 
 anonymous DH cipher suites, note that this does not include anonymous Elliptic
 Curve DH (ECDH) cipher suites.
 
 =item B
 
 cipher suites using DH, including anonymous DH, ephemeral DH and fixed DH.
 
 =item B, B, B
 
 cipher suites using fixed ECDH key agreement signed by CAs with RSA and ECDSA
 keys or either respectively.
 
 =item B, B
 
 cipher suites using ephemeral ECDH key agreement, including anonymous
 cipher suites.
 
 =item B, B
 
 cipher suites using authenticated ephemeral ECDH key agreement.
 
 =item B
 
 anonymous Elliptic Curve Diffie Hellman cipher suites.
 
 =item B
 
 cipher suites using ECDH key exchange, including anonymous, ephemeral and
 fixed ECDH.
 
 =item B
 
 cipher suites using RSA authentication, i.e. the certificates carry RSA keys.
 
 =item B, B
 
 cipher suites using DSS authentication, i.e. the certificates carry DSS keys.
 
 =item B
 
 cipher suites effectively using DH authentication, i.e. the certificates carry
 DH keys.
 
 =item B
 
 cipher suites effectively using ECDH authentication, i.e. the certificates
 carry ECDH keys.
 
 =item B, B
 
 cipher suites using ECDSA authentication, i.e. the certificates carry ECDSA
 keys.
 
 =item B, B, B, B
 
 ciphers suites using FORTEZZA key exchange, authentication, encryption or all
 FORTEZZA algorithms. Not implemented.
 
 =item B, B, B, B
 
 TLS v1.2, TLS v1.0, SSL v3.0 or SSL v2.0 cipher suites respectively. Note:
 there are no ciphersuites specific to TLS v1.1.
 
 =item B, B, B
 
 cipher suites using 128 bit AES, 256 bit AES or either 128 or 256 bit AES.
 
 =item B
 
 AES in Galois Counter Mode (GCM): these ciphersuites are only supported
 in TLS v1.2.
 
 =item B, B, B
 
 cipher suites using 128 bit CAMELLIA, 256 bit CAMELLIA or either 128 or 256 bit
 CAMELLIA.
 
 =item B<3DES>
 
 cipher suites using triple DES.
 
 =item B
 
 cipher suites using DES (not triple DES).
 
 =item B
 
 cipher suites using RC4.
 
 =item B
 
 cipher suites using RC2.
 
 =item B
 
 cipher suites using IDEA.
 
 =item B
 
 cipher suites using SEED.
 
 =item B
 
 cipher suites using MD5.
 
 =item B, B
 
 cipher suites using SHA1.
 
 =item B, B
 
 ciphersuites using SHA256 or SHA384.
 
 =item B 
 
 cipher suites using GOST R 34.10 (either 2001 or 94) for authenticaction
 (needs an engine supporting GOST algorithms). 
 
 =item B
 
 cipher suites using GOST R 34.10-2001 authentication.
 
 =item B
 
 cipher suites using GOST R 34.10-94 authentication (note that R 34.10-94
 standard has been expired so use GOST R 34.10-2001)
 
 =item B
 
 cipher suites, using VKO 34.10 key exchange, specified in the RFC 4357.
 
 =item B
 
 cipher suites, using HMAC based on GOST R 34.11-94.
 
 =item B
 
 cipher suites using GOST 28147-89 MAC B HMAC.
 
 =item B
 
 cipher suites using pre-shared keys (PSK).
 
 =item B, B, B
 
 enables suite B mode operation using 128 (permitting 192 bit mode by peer)
 128 bit (not permitting 192 bit by peer) or 192 bit level of security
 respectively. If used these cipherstrings should appear first in the cipher
 list and anything after them is ignored. Setting Suite B mode has additional
 consequences required to comply with RFC6460. In particular the supported
 signature algorithms is reduced to support only ECDSA and SHA256 or SHA384,
 only the elliptic curves P-256 and P-384 can be used and only the two suite B
 compliant ciphersuites (ECDHE-ECDSA-AES128-GCM-SHA256 and
 ECDHE-ECDSA-AES256-GCM-SHA384) are permissible.
 
 =back
 
 =head1 CIPHER SUITE NAMES
 
 The following lists give the SSL or TLS cipher suites names from the
 relevant specification and their OpenSSL equivalents. It should be noted,
 that several cipher suite names do not include the authentication used,
 e.g. DES-CBC3-SHA. In these cases, RSA authentication is used.
 
 =head2 SSL v3.0 cipher suites.
 
  SSL_RSA_WITH_NULL_MD5                   NULL-MD5
  SSL_RSA_WITH_NULL_SHA                   NULL-SHA
  SSL_RSA_EXPORT_WITH_RC4_40_MD5          EXP-RC4-MD5
  SSL_RSA_WITH_RC4_128_MD5                RC4-MD5
  SSL_RSA_WITH_RC4_128_SHA                RC4-SHA
  SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5      EXP-RC2-CBC-MD5
  SSL_RSA_WITH_IDEA_CBC_SHA               IDEA-CBC-SHA
  SSL_RSA_EXPORT_WITH_DES40_CBC_SHA       EXP-DES-CBC-SHA
  SSL_RSA_WITH_DES_CBC_SHA                DES-CBC-SHA
  SSL_RSA_WITH_3DES_EDE_CBC_SHA           DES-CBC3-SHA
 
  SSL_DH_DSS_WITH_DES_CBC_SHA             DH-DSS-DES-CBC-SHA
  SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA        DH-DSS-DES-CBC3-SHA
  SSL_DH_RSA_WITH_DES_CBC_SHA             DH-RSA-DES-CBC-SHA
  SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA        DH-RSA-DES-CBC3-SHA
  SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA   EXP-EDH-DSS-DES-CBC-SHA
  SSL_DHE_DSS_WITH_DES_CBC_SHA            EDH-DSS-CBC-SHA
  SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA       EDH-DSS-DES-CBC3-SHA
  SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA   EXP-EDH-RSA-DES-CBC-SHA
  SSL_DHE_RSA_WITH_DES_CBC_SHA            EDH-RSA-DES-CBC-SHA
  SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA       EDH-RSA-DES-CBC3-SHA
 
  SSL_DH_anon_EXPORT_WITH_RC4_40_MD5      EXP-ADH-RC4-MD5
  SSL_DH_anon_WITH_RC4_128_MD5            ADH-RC4-MD5
  SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA   EXP-ADH-DES-CBC-SHA
  SSL_DH_anon_WITH_DES_CBC_SHA            ADH-DES-CBC-SHA
  SSL_DH_anon_WITH_3DES_EDE_CBC_SHA       ADH-DES-CBC3-SHA
 
  SSL_FORTEZZA_KEA_WITH_NULL_SHA          Not implemented.
  SSL_FORTEZZA_KEA_WITH_FORTEZZA_CBC_SHA  Not implemented.
  SSL_FORTEZZA_KEA_WITH_RC4_128_SHA       Not implemented.
 
 =head2 TLS v1.0 cipher suites.
 
  TLS_RSA_WITH_NULL_MD5                   NULL-MD5
  TLS_RSA_WITH_NULL_SHA                   NULL-SHA
  TLS_RSA_EXPORT_WITH_RC4_40_MD5          EXP-RC4-MD5
  TLS_RSA_WITH_RC4_128_MD5                RC4-MD5
  TLS_RSA_WITH_RC4_128_SHA                RC4-SHA
  TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5      EXP-RC2-CBC-MD5
  TLS_RSA_WITH_IDEA_CBC_SHA               IDEA-CBC-SHA
  TLS_RSA_EXPORT_WITH_DES40_CBC_SHA       EXP-DES-CBC-SHA
  TLS_RSA_WITH_DES_CBC_SHA                DES-CBC-SHA
  TLS_RSA_WITH_3DES_EDE_CBC_SHA           DES-CBC3-SHA
 
  TLS_DH_DSS_EXPORT_WITH_DES40_CBC_SHA    Not implemented.
  TLS_DH_DSS_WITH_DES_CBC_SHA             Not implemented.
  TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA        Not implemented.
  TLS_DH_RSA_EXPORT_WITH_DES40_CBC_SHA    Not implemented.
  TLS_DH_RSA_WITH_DES_CBC_SHA             Not implemented.
  TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA        Not implemented.
  TLS_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA   EXP-EDH-DSS-DES-CBC-SHA
  TLS_DHE_DSS_WITH_DES_CBC_SHA            EDH-DSS-CBC-SHA
  TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA       EDH-DSS-DES-CBC3-SHA
  TLS_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA   EXP-EDH-RSA-DES-CBC-SHA
  TLS_DHE_RSA_WITH_DES_CBC_SHA            EDH-RSA-DES-CBC-SHA
  TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA       EDH-RSA-DES-CBC3-SHA
 
  TLS_DH_anon_EXPORT_WITH_RC4_40_MD5      EXP-ADH-RC4-MD5
  TLS_DH_anon_WITH_RC4_128_MD5            ADH-RC4-MD5
  TLS_DH_anon_EXPORT_WITH_DES40_CBC_SHA   EXP-ADH-DES-CBC-SHA
  TLS_DH_anon_WITH_DES_CBC_SHA            ADH-DES-CBC-SHA
  TLS_DH_anon_WITH_3DES_EDE_CBC_SHA       ADH-DES-CBC3-SHA
 
 =head2 AES ciphersuites from RFC3268, extending TLS v1.0
 
  TLS_RSA_WITH_AES_128_CBC_SHA            AES128-SHA
  TLS_RSA_WITH_AES_256_CBC_SHA            AES256-SHA
 
  TLS_DH_DSS_WITH_AES_128_CBC_SHA         DH-DSS-AES128-SHA
  TLS_DH_DSS_WITH_AES_256_CBC_SHA         DH-DSS-AES256-SHA
  TLS_DH_RSA_WITH_AES_128_CBC_SHA         DH-RSA-AES128-SHA
  TLS_DH_RSA_WITH_AES_256_CBC_SHA         DH-RSA-AES256-SHA
 
  TLS_DHE_DSS_WITH_AES_128_CBC_SHA        DHE-DSS-AES128-SHA
  TLS_DHE_DSS_WITH_AES_256_CBC_SHA        DHE-DSS-AES256-SHA
  TLS_DHE_RSA_WITH_AES_128_CBC_SHA        DHE-RSA-AES128-SHA
  TLS_DHE_RSA_WITH_AES_256_CBC_SHA        DHE-RSA-AES256-SHA
 
  TLS_DH_anon_WITH_AES_128_CBC_SHA        ADH-AES128-SHA
  TLS_DH_anon_WITH_AES_256_CBC_SHA        ADH-AES256-SHA
 
 =head2 Camellia ciphersuites from RFC4132, extending TLS v1.0
 
  TLS_RSA_WITH_CAMELLIA_128_CBC_SHA      CAMELLIA128-SHA
  TLS_RSA_WITH_CAMELLIA_256_CBC_SHA      CAMELLIA256-SHA
 
  TLS_DH_DSS_WITH_CAMELLIA_128_CBC_SHA   DH-DSS-CAMELLIA128-SHA
  TLS_DH_DSS_WITH_CAMELLIA_256_CBC_SHA   DH-DSS-CAMELLIA256-SHA
  TLS_DH_RSA_WITH_CAMELLIA_128_CBC_SHA   DH-RSA-CAMELLIA128-SHA
  TLS_DH_RSA_WITH_CAMELLIA_256_CBC_SHA   DH-RSA-CAMELLIA256-SHA
 
  TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA  DHE-DSS-CAMELLIA128-SHA
  TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA  DHE-DSS-CAMELLIA256-SHA
  TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA  DHE-RSA-CAMELLIA128-SHA
  TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA  DHE-RSA-CAMELLIA256-SHA
 
  TLS_DH_anon_WITH_CAMELLIA_128_CBC_SHA  ADH-CAMELLIA128-SHA
  TLS_DH_anon_WITH_CAMELLIA_256_CBC_SHA  ADH-CAMELLIA256-SHA
 
 =head2 SEED ciphersuites from RFC4162, extending TLS v1.0
 
  TLS_RSA_WITH_SEED_CBC_SHA              SEED-SHA
 
  TLS_DH_DSS_WITH_SEED_CBC_SHA           DH-DSS-SEED-SHA
  TLS_DH_RSA_WITH_SEED_CBC_SHA           DH-RSA-SEED-SHA
 
  TLS_DHE_DSS_WITH_SEED_CBC_SHA          DHE-DSS-SEED-SHA
  TLS_DHE_RSA_WITH_SEED_CBC_SHA          DHE-RSA-SEED-SHA
 
  TLS_DH_anon_WITH_SEED_CBC_SHA          ADH-SEED-SHA
 
 =head2 GOST ciphersuites from draft-chudov-cryptopro-cptls, extending TLS v1.0
 
 Note: these ciphers require an engine which including GOST cryptographic
 algorithms, such as the B engine, included in the OpenSSL distribution.
 
  TLS_GOSTR341094_WITH_28147_CNT_IMIT GOST94-GOST89-GOST89
  TLS_GOSTR341001_WITH_28147_CNT_IMIT GOST2001-GOST89-GOST89
  TLS_GOSTR341094_WITH_NULL_GOSTR3411 GOST94-NULL-GOST94
  TLS_GOSTR341001_WITH_NULL_GOSTR3411 GOST2001-NULL-GOST94
 
 =head2 Additional Export 1024 and other cipher suites
 
 Note: these ciphers can also be used in SSL v3.
 
  TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA     EXP1024-DES-CBC-SHA
  TLS_RSA_EXPORT1024_WITH_RC4_56_SHA      EXP1024-RC4-SHA
  TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA EXP1024-DHE-DSS-DES-CBC-SHA
  TLS_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA  EXP1024-DHE-DSS-RC4-SHA
  TLS_DHE_DSS_WITH_RC4_128_SHA            DHE-DSS-RC4-SHA
 
 =head2 Elliptic curve cipher suites.
 
  TLS_ECDH_RSA_WITH_NULL_SHA              ECDH-RSA-NULL-SHA
  TLS_ECDH_RSA_WITH_RC4_128_SHA           ECDH-RSA-RC4-SHA
  TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA      ECDH-RSA-DES-CBC3-SHA
  TLS_ECDH_RSA_WITH_AES_128_CBC_SHA       ECDH-RSA-AES128-SHA
  TLS_ECDH_RSA_WITH_AES_256_CBC_SHA       ECDH-RSA-AES256-SHA
 
  TLS_ECDH_ECDSA_WITH_NULL_SHA            ECDH-ECDSA-NULL-SHA
  TLS_ECDH_ECDSA_WITH_RC4_128_SHA         ECDH-ECDSA-RC4-SHA
  TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA    ECDH-ECDSA-DES-CBC3-SHA
  TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA     ECDH-ECDSA-AES128-SHA
  TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA     ECDH-ECDSA-AES256-SHA
 
  TLS_ECDHE_RSA_WITH_NULL_SHA             ECDHE-RSA-NULL-SHA
  TLS_ECDHE_RSA_WITH_RC4_128_SHA          ECDHE-RSA-RC4-SHA
  TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA     ECDHE-RSA-DES-CBC3-SHA
  TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA      ECDHE-RSA-AES128-SHA
  TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA      ECDHE-RSA-AES256-SHA
 
  TLS_ECDHE_ECDSA_WITH_NULL_SHA           ECDHE-ECDSA-NULL-SHA
  TLS_ECDHE_ECDSA_WITH_RC4_128_SHA        ECDHE-ECDSA-RC4-SHA
  TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA   ECDHE-ECDSA-DES-CBC3-SHA
  TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA    ECDHE-ECDSA-AES128-SHA
  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA    ECDHE-ECDSA-AES256-SHA
 
  TLS_ECDH_anon_WITH_NULL_SHA             AECDH-NULL-SHA
  TLS_ECDH_anon_WITH_RC4_128_SHA          AECDH-RC4-SHA
  TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA     AECDH-DES-CBC3-SHA
  TLS_ECDH_anon_WITH_AES_128_CBC_SHA      AECDH-AES128-SHA
  TLS_ECDH_anon_WITH_AES_256_CBC_SHA      AECDH-AES256-SHA
 
 =head2 TLS v1.2 cipher suites
 
  TLS_RSA_WITH_NULL_SHA256                  NULL-SHA256
 
  TLS_RSA_WITH_AES_128_CBC_SHA256           AES128-SHA256
  TLS_RSA_WITH_AES_256_CBC_SHA256           AES256-SHA256
  TLS_RSA_WITH_AES_128_GCM_SHA256           AES128-GCM-SHA256
  TLS_RSA_WITH_AES_256_GCM_SHA384           AES256-GCM-SHA384
 
  TLS_DH_RSA_WITH_AES_128_CBC_SHA256        DH-RSA-AES128-SHA256
  TLS_DH_RSA_WITH_AES_256_CBC_SHA256        DH-RSA-AES256-SHA256
  TLS_DH_RSA_WITH_AES_128_GCM_SHA256        DH-RSA-AES128-GCM-SHA256
  TLS_DH_RSA_WITH_AES_256_GCM_SHA384        DH-RSA-AES256-GCM-SHA384
 
  TLS_DH_DSS_WITH_AES_128_CBC_SHA256        DH-DSS-AES128-SHA256
  TLS_DH_DSS_WITH_AES_256_CBC_SHA256        DH-DSS-AES256-SHA256
  TLS_DH_DSS_WITH_AES_128_GCM_SHA256        DH-DSS-AES128-GCM-SHA256
  TLS_DH_DSS_WITH_AES_256_GCM_SHA384        DH-DSS-AES256-GCM-SHA384
 
  TLS_DHE_RSA_WITH_AES_128_CBC_SHA256       DHE-RSA-AES128-SHA256
  TLS_DHE_RSA_WITH_AES_256_CBC_SHA256       DHE-RSA-AES256-SHA256
  TLS_DHE_RSA_WITH_AES_128_GCM_SHA256       DHE-RSA-AES128-GCM-SHA256
  TLS_DHE_RSA_WITH_AES_256_GCM_SHA384       DHE-RSA-AES256-GCM-SHA384
 
  TLS_DHE_DSS_WITH_AES_128_CBC_SHA256       DHE-DSS-AES128-SHA256
  TLS_DHE_DSS_WITH_AES_256_CBC_SHA256       DHE-DSS-AES256-SHA256
  TLS_DHE_DSS_WITH_AES_128_GCM_SHA256       DHE-DSS-AES128-GCM-SHA256
  TLS_DHE_DSS_WITH_AES_256_GCM_SHA384       DHE-DSS-AES256-GCM-SHA384
 
  TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256      ECDH-RSA-AES128-SHA256
  TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384      ECDH-RSA-AES256-SHA384
  TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256      ECDH-RSA-AES128-GCM-SHA256
  TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384      ECDH-RSA-AES256-GCM-SHA384
 
  TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256    ECDH-ECDSA-AES128-SHA256
  TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384    ECDH-ECDSA-AES256-SHA384
  TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256    ECDH-ECDSA-AES128-GCM-SHA256
  TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384    ECDH-ECDSA-AES256-GCM-SHA384
 
  TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256     ECDHE-RSA-AES128-SHA256
  TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384     ECDHE-RSA-AES256-SHA384
  TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256     ECDHE-RSA-AES128-GCM-SHA256
  TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384     ECDHE-RSA-AES256-GCM-SHA384
 
  TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256   ECDHE-ECDSA-AES128-SHA256
  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384   ECDHE-ECDSA-AES256-SHA384
  TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256   ECDHE-ECDSA-AES128-GCM-SHA256
  TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384   ECDHE-ECDSA-AES256-GCM-SHA384
 
  TLS_DH_anon_WITH_AES_128_CBC_SHA256       ADH-AES128-SHA256
  TLS_DH_anon_WITH_AES_256_CBC_SHA256       ADH-AES256-SHA256
  TLS_DH_anon_WITH_AES_128_GCM_SHA256       ADH-AES128-GCM-SHA256
  TLS_DH_anon_WITH_AES_256_GCM_SHA384       ADH-AES256-GCM-SHA384
 
 =head2 Pre shared keying (PSK) cipheruites
 
  TLS_PSK_WITH_RC4_128_SHA                  PSK-RC4-SHA
  TLS_PSK_WITH_3DES_EDE_CBC_SHA             PSK-3DES-EDE-CBC-SHA
  TLS_PSK_WITH_AES_128_CBC_SHA              PSK-AES128-CBC-SHA
  TLS_PSK_WITH_AES_256_CBC_SHA              PSK-AES256-CBC-SHA
 
 =head2 Deprecated SSL v2.0 cipher suites.
 
  SSL_CK_RC4_128_WITH_MD5                 RC4-MD5
- SSL_CK_RC4_128_EXPORT40_WITH_MD5        EXP-RC4-MD5
- SSL_CK_RC2_128_CBC_WITH_MD5             RC2-MD5
- SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5    EXP-RC2-MD5
+ SSL_CK_RC4_128_EXPORT40_WITH_MD5        Not implemented.
+ SSL_CK_RC2_128_CBC_WITH_MD5             RC2-CBC-MD5
+ SSL_CK_RC2_128_CBC_EXPORT40_WITH_MD5    Not implemented.
  SSL_CK_IDEA_128_CBC_WITH_MD5            IDEA-CBC-MD5
- SSL_CK_DES_64_CBC_WITH_MD5              DES-CBC-MD5
+ SSL_CK_DES_64_CBC_WITH_MD5              Not implemented.
  SSL_CK_DES_192_EDE3_CBC_WITH_MD5        DES-CBC3-MD5
 
 =head1 NOTES
 
 Some compiled versions of OpenSSL may not include all the ciphers
 listed here because some ciphers were excluded at compile time.
 
 =head1 EXAMPLES
 
 Verbose listing of all OpenSSL ciphers including NULL ciphers:
 
  openssl ciphers -v 'ALL:eNULL'
 
 Include all ciphers except NULL and anonymous DH then sort by
 strength:
 
  openssl ciphers -v 'ALL:!ADH:@STRENGTH'
 
 Include all ciphers except ones with no encryption (eNULL) or no
 authentication (aNULL):
 
  openssl ciphers -v 'ALL:!aNULL'
 
 Include only 3DES ciphers and then place RSA ciphers last:
 
  openssl ciphers -v '3DES:+RSA'
 
 Include all RC4 ciphers but leave out those without authentication:
 
  openssl ciphers -v 'RC4:!COMPLEMENTOFDEFAULT'
 
 Include all chiphers with RSA authentication but leave out ciphers without
 encryption.
 
  openssl ciphers -v 'RSA:!COMPLEMENTOFALL'
 
 =head1 SEE ALSO
 
 L, L, L
 
 =head1 HISTORY
 
 The B and B selection options
 for cipherlist strings were added in OpenSSL 0.9.7.
 The B<-V> option for the B command was added in OpenSSL 1.0.0.
 
 =cut
Index: vendor-crypto/openssl/dist/doc/apps/pkeyutl.pod
===================================================================
--- vendor-crypto/openssl/dist/doc/apps/pkeyutl.pod	(revision 296272)
+++ vendor-crypto/openssl/dist/doc/apps/pkeyutl.pod	(revision 296273)
@@ -1,222 +1,235 @@
 =pod
 
 =head1 NAME
 
 pkeyutl - public key algorithm utility
 
 =head1 SYNOPSIS
 
 B B
 [B<-in file>]
 [B<-out file>]
 [B<-sigfile file>]
 [B<-inkey file>]
 [B<-keyform PEM|DER>]
 [B<-passin arg>]
 [B<-peerkey file>]
 [B<-peerform PEM|DER>]
 [B<-pubin>]
 [B<-certin>]
 [B<-rev>]
 [B<-sign>]
 [B<-verify>]
 [B<-verifyrecover>]
 [B<-encrypt>]
 [B<-decrypt>]
 [B<-derive>]
 [B<-pkeyopt opt:value>]
 [B<-hexdump>]
 [B<-asn1parse>]
 [B<-engine id>]
 
 =head1 DESCRIPTION
 
 The B command can be used to perform public key operations using
 any supported algorithm.
 
 =head1 COMMAND OPTIONS
 
 =over 4
 
 =item B<-in filename>
 
 This specifies the input filename to read data from or standard input
 if this option is not specified.
 
 =item B<-out filename>
 
 specifies the output filename to write to or standard output by
 default.
 
 =item B<-inkey file>
 
 the input key file, by default it should be a private key.
 
 =item B<-keyform PEM|DER>
 
 the key format PEM, DER or ENGINE.
 
 =item B<-passin arg>
 
 the input key password source. For more information about the format of B
 see the B section in L.
 
 
 =item B<-peerkey file>
 
 the peer key file, used by key derivation (agreement) operations.
 
 =item B<-peerform PEM|DER>
 
 the peer key format PEM, DER or ENGINE.
 
 =item B<-engine id>
 
 specifying an engine (by its unique B string) will cause B
 to attempt to obtain a functional reference to the specified engine,
 thus initialising it if needed. The engine will then be set as the default
 for all available algorithms.
 
 
 =item B<-pubin>
 
 the input file is a public key. 
 
 =item B<-certin>
 
 the input is a certificate containing a public key. 
 
 =item B<-rev>
 
 reverse the order of the input buffer. This is useful for some libraries
 (such as CryptoAPI) which represent the buffer in little endian format.
 
 =item B<-sign>
 
 sign the input data and output the signed result. This requires
 a private key.
 
 =item B<-verify>
 
 verify the input data against the signature file and indicate if the
 verification succeeded or failed.
 
 =item B<-verifyrecover>
 
 verify the input data and output the recovered data.
 
 =item B<-encrypt>
 
 encrypt the input data using a public key.
 
 =item B<-decrypt>
 
 decrypt the input data using a private key.
 
 =item B<-derive>
 
 derive a shared secret using the peer key.
 
 =item B<-hexdump>
 
 hex dump the output data.
 
 =item B<-asn1parse>
 
 asn1parse the output data, this is useful when combined with the
 B<-verifyrecover> option when an ASN1 structure is signed.
 
 =back
 
 =head1 NOTES
 
 The operations and options supported vary according to the key algorithm
 and its implementation. The OpenSSL operations and options are indicated below.
 
 Unless otherwise mentioned all algorithms support the B option
 which specifies the digest in use for sign, verify and verifyrecover operations.
 The value B should represent a digest name as used in the
 EVP_get_digestbyname() function for example B.
+This value is used only for sanity-checking the lengths of data passed in to
+the B and for creating the structures that make up the signature
+(e.g. B in RSASSA PKCS#1 v1.5 signatures).
+In case of RSA, ECDSA and DSA signatures, this utility
+will not perform hashing on input data but rather use the data directly as
+input of signature algorithm. Depending on key type, signature type and mode
+of padding, the maximum acceptable lengths of input data differ. In general,
+with RSA the signed data can't be longer than the key modulus, in case of ECDSA
+and DSA the data shouldn't be longer than field size, otherwise it will be
+silently truncated to field size.
+
+In other words, if the value of digest is B the input should be 20 bytes
+long binary encoding of SHA-1 hash function output.
 
 =head1 RSA ALGORITHM
 
 The RSA algorithm supports encrypt, decrypt, sign, verify and verifyrecover
 operations in general. Some padding modes only support some of these 
 operations however.
 
 =over 4
 
 =item -B
 
 This sets the RSA padding mode. Acceptable values for B are B for
 PKCS#1 padding, B for SSLv23 padding, B for no padding, B
 for B mode, B for X9.31 mode and B for PSS.
 
 In PKCS#1 padding if the message digest is not set then the supplied data is 
 signed or verified directly instead of using a B structure. If a
 digest is set then the a B structure is used and its the length
 must correspond to the digest type.
 
 For B mode only encryption and decryption is supported.
 
 For B if the digest type is set it is used to format the block data
 otherwise the first byte is used to specify the X9.31 digest ID. Sign,
 verify and verifyrecover are can be performed in this mode.
 
 For B mode only sign and verify are supported and the digest type must be
 specified.
 
 =item B
 
 For B mode only this option specifies the salt length. Two special values
 are supported: -1 sets the salt length to the digest length. When signing -2
 sets the salt length to the maximum permissible value. When verifying -2 causes
 the salt length to be automatically determined based on the B block
 structure.
 
 =back
 
 =head1 DSA ALGORITHM
 
 The DSA algorithm supports signing and verification operations only. Currently
 there are no additional options other than B. Only the SHA1
 digest can be used and this digest is assumed by default.
 
 =head1 DH ALGORITHM
 
 The DH algorithm only supports the derivation operation and no additional
 options.
 
 =head1 EC ALGORITHM
 
 The EC algorithm supports sign, verify and derive operations. The sign and
 verify operations use ECDSA and derive uses ECDH. Currently there are no
 additional options other than B. Only the SHA1 digest can be used and
 this digest is assumed by default.
 
 =head1 EXAMPLES
 
 Sign some data using a private key:
 
  openssl pkeyutl -sign -in file -inkey key.pem -out sig
 
 Recover the signed data (e.g. if an RSA key is used):
 
  openssl pkeyutl -verifyrecover -in sig -inkey key.pem
 
 Verify the signature (e.g. a DSA key):
 
  openssl pkeyutl -verify -in file -sigfile sig -inkey key.pem
 
 Sign data using a message digest value (this is currently only valid for RSA):
 
  openssl pkeyutl -sign -in file -inkey key.pem -out sig -pkeyopt digest:sha256
 
 Derive a shared secret value:
 
  openssl pkeyutl -derive -inkey key.pem -peerkey pubkey.pem -out secret
 
 =head1 SEE ALSO
 
 L, L, L
 L, L, L
Index: vendor-crypto/openssl/dist/doc/apps/req.pod
===================================================================
--- vendor-crypto/openssl/dist/doc/apps/req.pod	(revision 296272)
+++ vendor-crypto/openssl/dist/doc/apps/req.pod	(revision 296273)
@@ -1,677 +1,680 @@
 
 =pod
 
 =head1 NAME
 
 req - PKCS#10 certificate request and certificate generating utility.
 
 =head1 SYNOPSIS
 
 B B
 [B<-inform PEM|DER>]
 [B<-outform PEM|DER>]
 [B<-in filename>]
 [B<-passin arg>]
 [B<-out filename>]
 [B<-passout arg>]
 [B<-text>]
 [B<-pubkey>]
 [B<-noout>]
 [B<-verify>]
 [B<-modulus>]
 [B<-new>]
 [B<-rand file(s)>]
 [B<-newkey rsa:bits>]
 [B<-newkey alg:file>]
 [B<-nodes>]
 [B<-key filename>]
 [B<-keyform PEM|DER>]
 [B<-keyout filename>]
 [B<-keygen_engine id>]
 [B<-[digest]>]
 [B<-config filename>]
 [B<-multivalue-rdn>]
 [B<-x509>]
 [B<-days n>]
 [B<-set_serial n>]
 [B<-asn1-kludge>]
 [B<-no-asn1-kludge>]
 [B<-newhdr>]
 [B<-extensions section>]
 [B<-reqexts section>]
 [B<-utf8>]
 [B<-nameopt>]
 [B<-reqopt>]
 [B<-subject>]
 [B<-subj arg>]
 [B<-batch>]
 [B<-verbose>]
 [B<-engine id>]
 
 =head1 DESCRIPTION
 
 The B command primarily creates and processes certificate requests
 in PKCS#10 format. It can additionally create self signed certificates
 for use as root CAs for example.
 
 =head1 COMMAND OPTIONS
 
 =over 4
 
 =item B<-inform DER|PEM>
 
 This specifies the input format. The B option uses an ASN1 DER encoded
 form compatible with the PKCS#10. The B form is the default format: it
 consists of the B format base64 encoded with additional header and
 footer lines.
 
 =item B<-outform DER|PEM>
 
 This specifies the output format, the options have the same meaning as the 
 B<-inform> option.
 
 =item B<-in filename>
 
 This specifies the input filename to read a request from or standard input
 if this option is not specified. A request is only read if the creation
 options (B<-new> and B<-newkey>) are not specified.
 
 =item B<-passin arg>
 
 the input file password source. For more information about the format of B
 see the B section in L.
 
 =item B<-out filename>
 
 This specifies the output filename to write to or standard output by
 default.
 
 =item B<-passout arg>
 
 the output file password source. For more information about the format of B
 see the B section in L.
 
 =item B<-text>
 
 prints out the certificate request in text form.
 
 =item B<-subject>
 
 prints out the request subject (or certificate subject if B<-x509> is
 specified)
 
 =item B<-pubkey>
 
 outputs the public key.
 
 =item B<-noout>
 
 this option prevents output of the encoded version of the request.
 
 =item B<-modulus>
 
 this option prints out the value of the modulus of the public key
 contained in the request.
 
 =item B<-verify>
 
 verifies the signature on the request.
 
 =item B<-new>
 
 this option generates a new certificate request. It will prompt
 the user for the relevant field values. The actual fields
 prompted for and their maximum and minimum sizes are specified
 in the configuration file and any requested extensions.
 
 If the B<-key> option is not used it will generate a new RSA private
 key using information specified in the configuration file.
 
 =item B<-subj arg>
 
 Replaces subject field of input request with specified data and outputs
 modified request. The arg must be formatted as
 I,
 characters may be escaped by \ (backslash), no spaces are skipped.
 
 =item B<-rand file(s)>
 
 a file or files containing random data used to seed the random number
 generator, or an EGD socket (see L).
 Multiple files can be specified separated by a OS-dependent character.
 The separator is B<;> for MS-Windows, B<,> for OpenVMS, and B<:> for
 all others.
 
 =item B<-newkey arg>
 
 this option creates a new certificate request and a new private
 key. The argument takes one of several forms. B, where
 B is the number of bits, generates an RSA key B
 in size. If B is omitted, i.e. B<-newkey rsa> specified,
 the default key size, specified in the configuration file is used.
 
 All other algorithms support the B<-newkey alg:file> form, where file may be
 an algorithm parameter file, created by the B command
 or and X.509 certificate for a key with approriate algorithm.
 
 B generates a key using the parameter file or certificate B,
 the algorithm is determined by the parameters. B use algorithm
 B and parameter file B: the two algorithms must match or an
 error occurs. B just uses algorithm B, and parameters,
 if neccessary should be specified via B<-pkeyopt> parameter.
 
 B generates a DSA key using the parameters
 in the file B. B generates EC key (usable both with
 ECDSA or ECDH algorithms), B generates GOST R
 34.10-2001 key (requires B engine configured in the configuration
 file). If just B is specified a parameter set should be
 specified by B<-pkeyopt paramset:X>
 
 
 =item B<-pkeyopt opt:value>
 
 set the public key algorithm option B to B. The precise set of
 options supported depends on the public key algorithm used and its
 implementation. See B in the B manual page
 for more details.
 
 =item B<-key filename>
 
 This specifies the file to read the private key from. It also
 accepts PKCS#8 format private keys for PEM format files.
 
 =item B<-keyform PEM|DER>
 
 the format of the private key file specified in the B<-key>
 argument. PEM is the default.
 
 =item B<-keyout filename>
 
 this gives the filename to write the newly created private key to.
 If this option is not specified then the filename present in the
 configuration file is used.
 
 =item B<-nodes>
 
 if this option is specified then if a private key is created it
 will not be encrypted.
 
 =item B<-[digest]>
 
 this specifies the message digest to sign the request with (such as
 B<-md5>, B<-sha1>). This overrides the digest algorithm specified in
 the configuration file.
 
 Some public key algorithms may override this choice. For instance, DSA
 signatures always use SHA1, GOST R 34.10 signatures always use
 GOST R 34.11-94 (B<-md_gost94>).
 
 =item B<-config filename>
 
 this allows an alternative configuration file to be specified,
 this overrides the compile time filename or any specified in
 the B environment variable.
 
 =item B<-subj arg>
 
 sets subject name for new request or supersedes the subject name
 when processing a request.
 The arg must be formatted as I,
 characters may be escaped by \ (backslash), no spaces are skipped.
 
 =item B<-multivalue-rdn>
 
 this option causes the -subj argument to be interpreted with full
 support for multivalued RDNs. Example:
 
 I
 
 If -multi-rdn is not used then the UID value is I<123456+CN=John Doe>.
 
 =item B<-x509>
 
 this option outputs a self signed certificate instead of a certificate
 request. This is typically used to generate a test certificate or
 a self signed root CA. The extensions added to the certificate
 (if any) are specified in the configuration file. Unless specified
 using the B option, a large random number will be used for
 the serial number.
 
 =item B<-days n>
 
 when the B<-x509> option is being used this specifies the number of
 days to certify the certificate for. The default is 30 days.
 
 =item B<-set_serial n>
 
 serial number to use when outputting a self signed certificate. This
 may be specified as a decimal value or a hex value if preceded by B<0x>.
 It is possible to use negative serial numbers but this is not recommended.
 
 =item B<-extensions section>
 
 =item B<-reqexts section>
 
 these options specify alternative sections to include certificate
 extensions (if the B<-x509> option is present) or certificate
 request extensions. This allows several different sections to
 be used in the same configuration file to specify requests for
 a variety of purposes.
 
 =item B<-utf8>
 
 this option causes field values to be interpreted as UTF8 strings, by 
 default they are interpreted as ASCII. This means that the field
 values, whether prompted from a terminal or obtained from a
 configuration file, must be valid UTF8 strings.
 
 =item B<-nameopt option>
 
 option which determines how the subject or issuer names are displayed. The
 B