mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
127d3745c3 | ||
|
|
d89b2ef9ed | ||
|
|
684952cafa | ||
|
|
8273610176 | ||
|
|
53da7b3706 | ||
|
|
22fd389195 | ||
|
|
01c3fdfb85 | ||
|
|
eefb07cc59 | ||
|
|
158e2b140c | ||
|
|
8b7b68753f | ||
|
|
6c245a7f7f | ||
|
|
3bea268b22 | ||
|
|
0141f53dd5 | ||
|
|
b985ba6482 | ||
|
|
296d3308b4 | ||
|
|
8ff3d9dc83 | ||
|
|
5c066af98c | ||
|
|
26d3f525eb | ||
|
|
03581fe6ef | ||
|
|
f9e47ba809 | ||
|
|
42f5ffecd4 | ||
|
|
47640ada33 | ||
|
|
73dc51e2b2 | ||
|
|
f8034c8319 | ||
|
|
85d00fea15 | ||
|
|
8f4bfa5bc1 | ||
|
|
0effb70b52 | ||
|
|
6eee6ab1d9 | ||
|
|
8b1daa33f6 | ||
|
|
ef6f94da02 |
No files matched your search
@@ -8,7 +8,7 @@ about: If something isn't working as expected
|
||||
|
||||
### New Issue Checklist
|
||||
|
||||
- [ ] I'm using the latest version of Swift Crypto (master branch)
|
||||
- [ ] I'm using the latest version of Swift Crypto (`main` branch)
|
||||
- [ ] I read the [Contribution Guidelines](https://github.com/apple/swift-crypto/blob/master/CONTRIBUTING.md)
|
||||
- [ ] I searched for [existing GitHub issues](https://github.com/apple/swift-crypto/issues)
|
||||
|
||||
|
||||
+5
-1
@@ -1,5 +1,6 @@
|
||||
.DS_Store
|
||||
/.build
|
||||
/out
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
Package.pins
|
||||
@@ -10,4 +11,7 @@ Package.resolved
|
||||
.podspecs
|
||||
DerivedData
|
||||
.swiftpm
|
||||
**/gyb.pyc
|
||||
**/gyb.pyc
|
||||
|
||||
.*.sw[nop]
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# file options
|
||||
|
||||
--swiftversion 5.0
|
||||
--swiftversion 5.2
|
||||
--exclude .build
|
||||
|
||||
# format options
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftCrypto open source project
|
||||
##
|
||||
## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.md for the list of SwiftCrypto project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
cmake_minimum_required(VERSION 3.15.1)
|
||||
|
||||
project(SwiftCrypto
|
||||
LANGUAGES ASM C Swift)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
||||
|
||||
include(SwiftSupport)
|
||||
|
||||
if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
else()
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
endif()
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
|
||||
|
||||
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
|
||||
|
||||
if(BUILD_SHARED_LIBS)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
|
||||
endif()
|
||||
|
||||
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
find_package(dispatch CONFIG)
|
||||
find_package(Foundation CONFIG)
|
||||
endif()
|
||||
|
||||
add_subdirectory(Sources)
|
||||
add_subdirectory(cmake/modules)
|
||||
+16
-3
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.1
|
||||
// swift-tools-version:5.2
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -20,7 +20,7 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: 67818bea6690a230e2f42e8a588e0f54949bbbf1
|
||||
// BoringSSL Commit: 25773430c07075a368416c3646fa4b07daf4968a
|
||||
|
||||
import PackageDescription
|
||||
|
||||
@@ -40,17 +40,30 @@ let package = Package(
|
||||
],
|
||||
products: [
|
||||
.library(name: "Crypto", targets: ["Crypto"]),
|
||||
.library(name: "_CryptoExtras", targets: ["_CryptoExtras"]),
|
||||
/* This target is used only for symbol mangling. It's added and removed automatically because it emits build warnings. MANGLE_START
|
||||
.library(name: "CCryptoBoringSSL", type: .static, targets: ["CCryptoBoringSSL"]),
|
||||
MANGLE_END */
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.target(name: "CCryptoBoringSSL"),
|
||||
.target(
|
||||
name: "CCryptoBoringSSL",
|
||||
cSettings: [
|
||||
/*
|
||||
* This define is required on Windows, but because we need older
|
||||
* versions of SPM, we cannot conditionally define this on Windows
|
||||
* only. Unconditionally define it instead.
|
||||
*/
|
||||
.define("WIN32_LEAN_AND_MEAN"),
|
||||
]
|
||||
),
|
||||
.target(name: "CCryptoBoringSSLShims", dependencies: ["CCryptoBoringSSL"]),
|
||||
.target(name: "Crypto", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims"], swiftSettings: swiftSettings),
|
||||
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "Crypto"]),
|
||||
.target(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
.testTarget(name: "CryptoTests", dependencies: ["Crypto"], swiftSettings: swiftSettings),
|
||||
.testTarget(name: "_CryptoExtrasTests", dependencies: ["_CryptoExtras"]),
|
||||
],
|
||||
cxxLanguageStandard: .cxx11
|
||||
)
|
||||
@@ -96,11 +96,11 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo
|
||||
|
||||
### Swift versions
|
||||
|
||||
Swift Crypto supports Swift 5.1 and later.
|
||||
Swift Crypto supports Swift 5.2 and later. Swift Crypto supported Swift 5.1 until version 1.2.
|
||||
|
||||
### Compatibility
|
||||
|
||||
Swift Crypto follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200). Our public API is the same as that of CryptoKit (except where we lack an implementation entirely), as well as everything in the Server and Staging namespaces. We do not maintain a stable ABI, as Swift Crypto is a source-only distribution.
|
||||
Swift Crypto follows [SemVer 2.0.0](https://semver.org/#semantic-versioning-200). Our public API is the same as that of CryptoKit (except where we lack an implementation entirely), as well as everything in the Server and Staging namespaces. Any symbol beginning with an underscore, and any product beginning with an underscore, is not subject to semantic versioning: these APIs may change without warning. We do not maintain a stable ABI, as Swift Crypto is a source-only distribution.
|
||||
|
||||
What this means for you is that you should depend on Swift Crypto with a version range that covers everything from the minimum Swift Crypto version you require up to the next major version.
|
||||
In SwiftPM that can be easily done specifying for example `from: "1.0.0"` meaning that you support Swift Crypto in every version starting from 1.0.0 up to (excluding) 2.0.0.
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
# Security
|
||||
|
||||
This document specifies the security process for Swift Crypto.
|
||||
|
||||
## Disclosures
|
||||
|
||||
To report a known or suspected vulnerability in Swift Crypto, [please report that vulnerability to Apple through the usual channel](https://support.apple.com/en-us/HT201220).
|
||||
|
||||
**Do not file a public issue.**
|
||||
|
||||
Fixes to Swift Crypto will be released simultaneously with any changes that need to be made in CryptoKit, to avoid the risk of having only partially fixed projects.
|
||||
|
||||
+393
@@ -0,0 +1,393 @@
|
||||
##===----------------------------------------------------------------------===##
|
||||
##
|
||||
## This source file is part of the SwiftCrypto open source project
|
||||
##
|
||||
## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
|
||||
## Licensed under Apache License v2.0
|
||||
##
|
||||
## See LICENSE.txt for license information
|
||||
## See CONTRIBUTORS.md for the list of SwiftCrypto project authors
|
||||
##
|
||||
## SPDX-License-Identifier: Apache-2.0
|
||||
##
|
||||
##===----------------------------------------------------------------------===##
|
||||
|
||||
add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/asn1/a_bitstr.c"
|
||||
"crypto/asn1/a_bool.c"
|
||||
"crypto/asn1/a_d2i_fp.c"
|
||||
"crypto/asn1/a_dup.c"
|
||||
"crypto/asn1/a_enum.c"
|
||||
"crypto/asn1/a_gentm.c"
|
||||
"crypto/asn1/a_i2d_fp.c"
|
||||
"crypto/asn1/a_int.c"
|
||||
"crypto/asn1/a_mbstr.c"
|
||||
"crypto/asn1/a_object.c"
|
||||
"crypto/asn1/a_octet.c"
|
||||
"crypto/asn1/a_print.c"
|
||||
"crypto/asn1/a_strex.c"
|
||||
"crypto/asn1/a_strnid.c"
|
||||
"crypto/asn1/a_time.c"
|
||||
"crypto/asn1/a_type.c"
|
||||
"crypto/asn1/a_utctm.c"
|
||||
"crypto/asn1/a_utf8.c"
|
||||
"crypto/asn1/asn1_lib.c"
|
||||
"crypto/asn1/asn1_par.c"
|
||||
"crypto/asn1/asn_pack.c"
|
||||
"crypto/asn1/f_enum.c"
|
||||
"crypto/asn1/f_int.c"
|
||||
"crypto/asn1/f_string.c"
|
||||
"crypto/asn1/tasn_dec.c"
|
||||
"crypto/asn1/tasn_enc.c"
|
||||
"crypto/asn1/tasn_fre.c"
|
||||
"crypto/asn1/tasn_new.c"
|
||||
"crypto/asn1/tasn_typ.c"
|
||||
"crypto/asn1/tasn_utl.c"
|
||||
"crypto/asn1/time_support.c"
|
||||
"crypto/base64/base64.c"
|
||||
"crypto/bio/bio.c"
|
||||
"crypto/bio/bio_mem.c"
|
||||
"crypto/bio/connect.c"
|
||||
"crypto/bio/fd.c"
|
||||
"crypto/bio/file.c"
|
||||
"crypto/bio/hexdump.c"
|
||||
"crypto/bio/pair.c"
|
||||
"crypto/bio/printf.c"
|
||||
"crypto/bio/socket.c"
|
||||
"crypto/bio/socket_helper.c"
|
||||
"crypto/blake2/blake2.c"
|
||||
"crypto/bn_extra/bn_asn1.c"
|
||||
"crypto/bn_extra/convert.c"
|
||||
"crypto/buf/buf.c"
|
||||
"crypto/bytestring/asn1_compat.c"
|
||||
"crypto/bytestring/ber.c"
|
||||
"crypto/bytestring/cbb.c"
|
||||
"crypto/bytestring/cbs.c"
|
||||
"crypto/bytestring/unicode.c"
|
||||
"crypto/chacha/chacha.c"
|
||||
"crypto/cipher_extra/cipher_extra.c"
|
||||
"crypto/cipher_extra/derive_key.c"
|
||||
"crypto/cipher_extra/e_aesccm.c"
|
||||
"crypto/cipher_extra/e_aesctrhmac.c"
|
||||
"crypto/cipher_extra/e_aesgcmsiv.c"
|
||||
"crypto/cipher_extra/e_chacha20poly1305.c"
|
||||
"crypto/cipher_extra/e_null.c"
|
||||
"crypto/cipher_extra/e_rc2.c"
|
||||
"crypto/cipher_extra/e_rc4.c"
|
||||
"crypto/cipher_extra/e_tls.c"
|
||||
"crypto/cipher_extra/tls_cbc.c"
|
||||
"crypto/cmac/cmac.c"
|
||||
"crypto/conf/conf.c"
|
||||
"crypto/cpu-aarch64-fuchsia.c"
|
||||
"crypto/cpu-aarch64-linux.c"
|
||||
"crypto/cpu-aarch64-win.c"
|
||||
"crypto/cpu-arm-linux.c"
|
||||
"crypto/cpu-arm.c"
|
||||
"crypto/cpu-intel.c"
|
||||
"crypto/cpu-ppc64le.c"
|
||||
"crypto/crypto.c"
|
||||
"crypto/curve25519/curve25519.c"
|
||||
"crypto/curve25519/spake25519.c"
|
||||
"crypto/dh_extra/dh_asn1.c"
|
||||
"crypto/dh_extra/params.c"
|
||||
"crypto/digest_extra/digest_extra.c"
|
||||
"crypto/dsa/dsa.c"
|
||||
"crypto/dsa/dsa_asn1.c"
|
||||
"crypto/ec_extra/ec_asn1.c"
|
||||
"crypto/ec_extra/ec_derive.c"
|
||||
"crypto/ec_extra/hash_to_curve.c"
|
||||
"crypto/ecdh_extra/ecdh_extra.c"
|
||||
"crypto/ecdsa_extra/ecdsa_asn1.c"
|
||||
"crypto/engine/engine.c"
|
||||
"crypto/err/err.c"
|
||||
"crypto/err/err_data.c"
|
||||
"crypto/evp/digestsign.c"
|
||||
"crypto/evp/evp.c"
|
||||
"crypto/evp/evp_asn1.c"
|
||||
"crypto/evp/evp_ctx.c"
|
||||
"crypto/evp/p_dsa_asn1.c"
|
||||
"crypto/evp/p_ec.c"
|
||||
"crypto/evp/p_ec_asn1.c"
|
||||
"crypto/evp/p_ed25519.c"
|
||||
"crypto/evp/p_ed25519_asn1.c"
|
||||
"crypto/evp/p_rsa.c"
|
||||
"crypto/evp/p_rsa_asn1.c"
|
||||
"crypto/evp/p_x25519.c"
|
||||
"crypto/evp/p_x25519_asn1.c"
|
||||
"crypto/evp/pbkdf.c"
|
||||
"crypto/evp/print.c"
|
||||
"crypto/evp/scrypt.c"
|
||||
"crypto/evp/sign.c"
|
||||
"crypto/ex_data.c"
|
||||
"crypto/fipsmodule/aes/aes.c"
|
||||
"crypto/fipsmodule/aes/aes_nohw.c"
|
||||
"crypto/fipsmodule/aes/key_wrap.c"
|
||||
"crypto/fipsmodule/aes/mode_wrappers.c"
|
||||
"crypto/fipsmodule/bn/add.c"
|
||||
"crypto/fipsmodule/bn/asm/x86_64-gcc.c"
|
||||
"crypto/fipsmodule/bn/bn.c"
|
||||
"crypto/fipsmodule/bn/bytes.c"
|
||||
"crypto/fipsmodule/bn/cmp.c"
|
||||
"crypto/fipsmodule/bn/ctx.c"
|
||||
"crypto/fipsmodule/bn/div.c"
|
||||
"crypto/fipsmodule/bn/div_extra.c"
|
||||
"crypto/fipsmodule/bn/exponentiation.c"
|
||||
"crypto/fipsmodule/bn/gcd.c"
|
||||
"crypto/fipsmodule/bn/gcd_extra.c"
|
||||
"crypto/fipsmodule/bn/generic.c"
|
||||
"crypto/fipsmodule/bn/jacobi.c"
|
||||
"crypto/fipsmodule/bn/montgomery.c"
|
||||
"crypto/fipsmodule/bn/montgomery_inv.c"
|
||||
"crypto/fipsmodule/bn/mul.c"
|
||||
"crypto/fipsmodule/bn/prime.c"
|
||||
"crypto/fipsmodule/bn/random.c"
|
||||
"crypto/fipsmodule/bn/rsaz_exp.c"
|
||||
"crypto/fipsmodule/bn/shift.c"
|
||||
"crypto/fipsmodule/bn/sqrt.c"
|
||||
"crypto/fipsmodule/cipher/aead.c"
|
||||
"crypto/fipsmodule/cipher/cipher.c"
|
||||
"crypto/fipsmodule/cipher/e_aes.c"
|
||||
"crypto/fipsmodule/cipher/e_des.c"
|
||||
"crypto/fipsmodule/des/des.c"
|
||||
"crypto/fipsmodule/dh/check.c"
|
||||
"crypto/fipsmodule/dh/dh.c"
|
||||
"crypto/fipsmodule/digest/digest.c"
|
||||
"crypto/fipsmodule/digest/digests.c"
|
||||
"crypto/fipsmodule/ec/ec.c"
|
||||
"crypto/fipsmodule/ec/ec_key.c"
|
||||
"crypto/fipsmodule/ec/ec_montgomery.c"
|
||||
"crypto/fipsmodule/ec/felem.c"
|
||||
"crypto/fipsmodule/ec/oct.c"
|
||||
"crypto/fipsmodule/ec/p224-64.c"
|
||||
"crypto/fipsmodule/ec/p256-x86_64.c"
|
||||
"crypto/fipsmodule/ec/p256.c"
|
||||
"crypto/fipsmodule/ec/scalar.c"
|
||||
"crypto/fipsmodule/ec/simple.c"
|
||||
"crypto/fipsmodule/ec/simple_mul.c"
|
||||
"crypto/fipsmodule/ec/util.c"
|
||||
"crypto/fipsmodule/ec/wnaf.c"
|
||||
"crypto/fipsmodule/ecdh/ecdh.c"
|
||||
"crypto/fipsmodule/ecdsa/ecdsa.c"
|
||||
"crypto/fipsmodule/fips_shared_support.c"
|
||||
"crypto/fipsmodule/hmac/hmac.c"
|
||||
"crypto/fipsmodule/md4/md4.c"
|
||||
"crypto/fipsmodule/md5/md5.c"
|
||||
"crypto/fipsmodule/modes/cbc.c"
|
||||
"crypto/fipsmodule/modes/cfb.c"
|
||||
"crypto/fipsmodule/modes/ctr.c"
|
||||
"crypto/fipsmodule/modes/gcm.c"
|
||||
"crypto/fipsmodule/modes/gcm_nohw.c"
|
||||
"crypto/fipsmodule/modes/ofb.c"
|
||||
"crypto/fipsmodule/modes/polyval.c"
|
||||
"crypto/fipsmodule/rand/ctrdrbg.c"
|
||||
"crypto/fipsmodule/rand/fork_detect.c"
|
||||
"crypto/fipsmodule/rand/rand.c"
|
||||
"crypto/fipsmodule/rand/urandom.c"
|
||||
"crypto/fipsmodule/rsa/blinding.c"
|
||||
"crypto/fipsmodule/rsa/padding.c"
|
||||
"crypto/fipsmodule/rsa/rsa.c"
|
||||
"crypto/fipsmodule/rsa/rsa_impl.c"
|
||||
"crypto/fipsmodule/self_check/fips.c"
|
||||
"crypto/fipsmodule/self_check/self_check.c"
|
||||
"crypto/fipsmodule/sha/sha1-altivec.c"
|
||||
"crypto/fipsmodule/sha/sha1.c"
|
||||
"crypto/fipsmodule/sha/sha256.c"
|
||||
"crypto/fipsmodule/sha/sha512.c"
|
||||
"crypto/fipsmodule/tls/kdf.c"
|
||||
"crypto/hkdf/hkdf.c"
|
||||
"crypto/hpke/hpke.c"
|
||||
"crypto/hrss/hrss.c"
|
||||
"crypto/lhash/lhash.c"
|
||||
"crypto/mem.c"
|
||||
"crypto/obj/obj.c"
|
||||
"crypto/obj/obj_xref.c"
|
||||
"crypto/pem/pem_all.c"
|
||||
"crypto/pem/pem_info.c"
|
||||
"crypto/pem/pem_lib.c"
|
||||
"crypto/pem/pem_oth.c"
|
||||
"crypto/pem/pem_pk8.c"
|
||||
"crypto/pem/pem_pkey.c"
|
||||
"crypto/pem/pem_x509.c"
|
||||
"crypto/pem/pem_xaux.c"
|
||||
"crypto/pkcs7/pkcs7.c"
|
||||
"crypto/pkcs7/pkcs7_x509.c"
|
||||
"crypto/pkcs8/p5_pbev2.c"
|
||||
"crypto/pkcs8/pkcs8.c"
|
||||
"crypto/pkcs8/pkcs8_x509.c"
|
||||
"crypto/poly1305/poly1305.c"
|
||||
"crypto/poly1305/poly1305_arm.c"
|
||||
"crypto/poly1305/poly1305_vec.c"
|
||||
"crypto/pool/pool.c"
|
||||
"crypto/rand_extra/deterministic.c"
|
||||
"crypto/rand_extra/forkunsafe.c"
|
||||
"crypto/rand_extra/fuchsia.c"
|
||||
"crypto/rand_extra/passive.c"
|
||||
"crypto/rand_extra/rand_extra.c"
|
||||
"crypto/rand_extra/windows.c"
|
||||
"crypto/rc4/rc4.c"
|
||||
"crypto/refcount_c11.c"
|
||||
"crypto/refcount_lock.c"
|
||||
"crypto/rsa_extra/rsa_asn1.c"
|
||||
"crypto/rsa_extra/rsa_print.c"
|
||||
"crypto/siphash/siphash.c"
|
||||
"crypto/stack/stack.c"
|
||||
"crypto/thread.c"
|
||||
"crypto/thread_none.c"
|
||||
"crypto/thread_pthread.c"
|
||||
"crypto/thread_win.c"
|
||||
"crypto/trust_token/pmbtoken.c"
|
||||
"crypto/trust_token/trust_token.c"
|
||||
"crypto/trust_token/voprf.c"
|
||||
"crypto/x509/a_digest.c"
|
||||
"crypto/x509/a_sign.c"
|
||||
"crypto/x509/a_verify.c"
|
||||
"crypto/x509/algorithm.c"
|
||||
"crypto/x509/asn1_gen.c"
|
||||
"crypto/x509/by_dir.c"
|
||||
"crypto/x509/by_file.c"
|
||||
"crypto/x509/i2d_pr.c"
|
||||
"crypto/x509/name_print.c"
|
||||
"crypto/x509/rsa_pss.c"
|
||||
"crypto/x509/t_crl.c"
|
||||
"crypto/x509/t_req.c"
|
||||
"crypto/x509/t_x509.c"
|
||||
"crypto/x509/t_x509a.c"
|
||||
"crypto/x509/x509.c"
|
||||
"crypto/x509/x509_att.c"
|
||||
"crypto/x509/x509_cmp.c"
|
||||
"crypto/x509/x509_d2.c"
|
||||
"crypto/x509/x509_def.c"
|
||||
"crypto/x509/x509_ext.c"
|
||||
"crypto/x509/x509_lu.c"
|
||||
"crypto/x509/x509_obj.c"
|
||||
"crypto/x509/x509_req.c"
|
||||
"crypto/x509/x509_set.c"
|
||||
"crypto/x509/x509_trs.c"
|
||||
"crypto/x509/x509_txt.c"
|
||||
"crypto/x509/x509_v3.c"
|
||||
"crypto/x509/x509_vfy.c"
|
||||
"crypto/x509/x509_vpm.c"
|
||||
"crypto/x509/x509cset.c"
|
||||
"crypto/x509/x509name.c"
|
||||
"crypto/x509/x509rset.c"
|
||||
"crypto/x509/x509spki.c"
|
||||
"crypto/x509/x_algor.c"
|
||||
"crypto/x509/x_all.c"
|
||||
"crypto/x509/x_attrib.c"
|
||||
"crypto/x509/x_crl.c"
|
||||
"crypto/x509/x_exten.c"
|
||||
"crypto/x509/x_info.c"
|
||||
"crypto/x509/x_name.c"
|
||||
"crypto/x509/x_pkey.c"
|
||||
"crypto/x509/x_pubkey.c"
|
||||
"crypto/x509/x_req.c"
|
||||
"crypto/x509/x_sig.c"
|
||||
"crypto/x509/x_spki.c"
|
||||
"crypto/x509/x_val.c"
|
||||
"crypto/x509/x_x509.c"
|
||||
"crypto/x509/x_x509a.c"
|
||||
"crypto/x509v3/pcy_cache.c"
|
||||
"crypto/x509v3/pcy_data.c"
|
||||
"crypto/x509v3/pcy_lib.c"
|
||||
"crypto/x509v3/pcy_map.c"
|
||||
"crypto/x509v3/pcy_node.c"
|
||||
"crypto/x509v3/pcy_tree.c"
|
||||
"crypto/x509v3/v3_akey.c"
|
||||
"crypto/x509v3/v3_akeya.c"
|
||||
"crypto/x509v3/v3_alt.c"
|
||||
"crypto/x509v3/v3_bcons.c"
|
||||
"crypto/x509v3/v3_bitst.c"
|
||||
"crypto/x509v3/v3_conf.c"
|
||||
"crypto/x509v3/v3_cpols.c"
|
||||
"crypto/x509v3/v3_crld.c"
|
||||
"crypto/x509v3/v3_enum.c"
|
||||
"crypto/x509v3/v3_extku.c"
|
||||
"crypto/x509v3/v3_genn.c"
|
||||
"crypto/x509v3/v3_ia5.c"
|
||||
"crypto/x509v3/v3_info.c"
|
||||
"crypto/x509v3/v3_int.c"
|
||||
"crypto/x509v3/v3_lib.c"
|
||||
"crypto/x509v3/v3_ncons.c"
|
||||
"crypto/x509v3/v3_ocsp.c"
|
||||
"crypto/x509v3/v3_pci.c"
|
||||
"crypto/x509v3/v3_pcia.c"
|
||||
"crypto/x509v3/v3_pcons.c"
|
||||
"crypto/x509v3/v3_pmaps.c"
|
||||
"crypto/x509v3/v3_prn.c"
|
||||
"crypto/x509v3/v3_purp.c"
|
||||
"crypto/x509v3/v3_skey.c"
|
||||
"crypto/x509v3/v3_utl.c")
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.mac.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5.linux.x86_64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.ios.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.ios.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.ios.aarch64.S)
|
||||
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64")
|
||||
target_sources(CCryptoBoringSSL PRIVATE
|
||||
crypto/chacha/chacha-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont.linux.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armx64.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8.linux.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8.linux.aarch64.S)
|
||||
endif()
|
||||
|
||||
target_include_directories(CCryptoBoringSSL PUBLIC
|
||||
include)
|
||||
|
||||
target_compile_definitions(CCryptoBoringSSL PRIVATE
|
||||
$<$<PLATFORM_ID:Windows>:WIN32_LEAN_AND_MEAN>)
|
||||
set_target_properties(CCryptoBoringSSL PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_Swift_MODULE_DIRECTORY}")
|
||||
|
||||
set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSL)
|
||||
@@ -65,64 +65,69 @@
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len)
|
||||
{
|
||||
return M_ASN1_BIT_STRING_set(x, d, len);
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
|
||||
static int asn1_bit_string_length(const ASN1_BIT_STRING *str,
|
||||
uint8_t *out_padding_bits) {
|
||||
int len = str->length;
|
||||
if (str->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
// If the string is already empty, it cannot have padding bits.
|
||||
*out_padding_bits = len == 0 ? 0 : str->flags & 0x07;
|
||||
return len;
|
||||
}
|
||||
|
||||
// TODO(davidben): If we move this logic to |ASN1_BIT_STRING_set_bit|, can
|
||||
// we remove this representation?
|
||||
while (len > 0 && str->data[len - 1] == 0) {
|
||||
len--;
|
||||
}
|
||||
uint8_t padding_bits = 0;
|
||||
if (len > 0) {
|
||||
uint8_t last = str->data[len - 1];
|
||||
assert(last != 0);
|
||||
for (; padding_bits < 7; padding_bits++) {
|
||||
if (last & (1 << padding_bits)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
*out_padding_bits = padding_bits;
|
||||
return len;
|
||||
}
|
||||
|
||||
int ASN1_BIT_STRING_num_bytes(const ASN1_BIT_STRING *str, size_t *out) {
|
||||
uint8_t padding_bits;
|
||||
int len = asn1_bit_string_length(str, &padding_bits);
|
||||
if (padding_bits != 0) {
|
||||
return 0;
|
||||
}
|
||||
*out = len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i2c_ASN1_BIT_STRING(const ASN1_BIT_STRING *a, unsigned char **pp)
|
||||
{
|
||||
int ret, j, bits, len;
|
||||
unsigned char *p, *d;
|
||||
if (a == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
|
||||
len = a->length;
|
||||
uint8_t bits;
|
||||
int len = asn1_bit_string_length(a, &bits);
|
||||
int ret = 1 + len;
|
||||
if (pp == NULL) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t *p = *pp;
|
||||
*(p++) = bits;
|
||||
OPENSSL_memcpy(p, a->data, len);
|
||||
if (len > 0) {
|
||||
if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
|
||||
bits = (int)a->flags & 0x07;
|
||||
} else {
|
||||
for (; len > 0; len--) {
|
||||
if (a->data[len - 1])
|
||||
break;
|
||||
}
|
||||
j = a->data[len - 1];
|
||||
if (j & 0x01)
|
||||
bits = 0;
|
||||
else if (j & 0x02)
|
||||
bits = 1;
|
||||
else if (j & 0x04)
|
||||
bits = 2;
|
||||
else if (j & 0x08)
|
||||
bits = 3;
|
||||
else if (j & 0x10)
|
||||
bits = 4;
|
||||
else if (j & 0x20)
|
||||
bits = 5;
|
||||
else if (j & 0x40)
|
||||
bits = 6;
|
||||
else if (j & 0x80)
|
||||
bits = 7;
|
||||
else
|
||||
bits = 0; /* should not happen */
|
||||
}
|
||||
} else
|
||||
bits = 0;
|
||||
|
||||
ret = 1 + len;
|
||||
if (pp == NULL)
|
||||
return (ret);
|
||||
|
||||
p = *pp;
|
||||
|
||||
*(p++) = (unsigned char)bits;
|
||||
d = a->data;
|
||||
OPENSSL_memcpy(p, d, len);
|
||||
p[len - 1] &= (0xff << bits);
|
||||
}
|
||||
p += len;
|
||||
if (len > 0)
|
||||
p[-1] &= (0xff << bits);
|
||||
*pp = p;
|
||||
return (ret);
|
||||
}
|
||||
@@ -146,7 +151,7 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL)) {
|
||||
if ((ret = M_ASN1_BIT_STRING_new()) == NULL)
|
||||
if ((ret = ASN1_BIT_STRING_new()) == NULL)
|
||||
return (NULL);
|
||||
} else
|
||||
ret = (*a);
|
||||
@@ -188,7 +193,7 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
return (ret);
|
||||
err:
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret)))
|
||||
M_ASN1_BIT_STRING_free(ret);
|
||||
ASN1_BIT_STRING_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -251,7 +256,7 @@ int ASN1_BIT_STRING_get_bit(const ASN1_BIT_STRING *a, int n)
|
||||
* 'len' is the length of 'flags'.
|
||||
*/
|
||||
int ASN1_BIT_STRING_check(const ASN1_BIT_STRING *a,
|
||||
unsigned char *flags, int flags_len)
|
||||
const unsigned char *flags, int flags_len)
|
||||
{
|
||||
int i, ok;
|
||||
/* Check if there is one bit set at all. */
|
||||
|
||||
@@ -78,7 +78,7 @@ int i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = (unsigned char)a;
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
/*
|
||||
* If a new buffer was allocated, just return it back.
|
||||
|
||||
@@ -78,7 +78,6 @@ void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
||||
{
|
||||
BIO *b = BIO_new_fp(in, BIO_NOCLOSE);
|
||||
@@ -90,4 +89,3 @@ void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
||||
BIO_free(b);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
@@ -153,7 +153,7 @@ ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
|
||||
int len, j;
|
||||
|
||||
if (ai == NULL)
|
||||
ret = M_ASN1_ENUMERATED_new();
|
||||
ret = ASN1_ENUMERATED_new();
|
||||
else
|
||||
ret = ai;
|
||||
if (ret == NULL) {
|
||||
@@ -179,7 +179,7 @@ ASN1_ENUMERATED *BN_to_ASN1_ENUMERATED(const BIGNUM *bn, ASN1_ENUMERATED *ai)
|
||||
return (ret);
|
||||
err:
|
||||
if (ret != ai)
|
||||
M_ASN1_ENUMERATED_free(ret);
|
||||
ASN1_ENUMERATED_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
|
||||
{
|
||||
@@ -237,6 +237,11 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ts->tm_year < 0 - 1900 || ts->tm_year > 9999 - 1900) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TIME_VALUE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
p = (char *)tmps->data;
|
||||
if ((p == NULL) || ((size_t)tmps->length < len)) {
|
||||
p = OPENSSL_malloc(len);
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
|
||||
ASN1_INTEGER *ASN1_INTEGER_dup(const ASN1_INTEGER *x)
|
||||
{
|
||||
return M_ASN1_INTEGER_dup(x);
|
||||
return ASN1_STRING_dup(x);
|
||||
}
|
||||
|
||||
int ASN1_INTEGER_cmp(const ASN1_INTEGER *x, const ASN1_INTEGER *y)
|
||||
@@ -206,7 +206,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL)) {
|
||||
if ((ret = M_ASN1_INTEGER_new()) == NULL)
|
||||
if ((ret = ASN1_INTEGER_new()) == NULL)
|
||||
return (NULL);
|
||||
ret->type = V_ASN1_INTEGER;
|
||||
} else
|
||||
@@ -282,7 +282,7 @@ ASN1_INTEGER *c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp,
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret)))
|
||||
M_ASN1_INTEGER_free(ret);
|
||||
ASN1_INTEGER_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@@ -374,7 +374,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
|
||||
int len, j;
|
||||
|
||||
if (ai == NULL)
|
||||
ret = M_ASN1_INTEGER_new();
|
||||
ret = ASN1_INTEGER_new();
|
||||
else
|
||||
ret = ai;
|
||||
if (ret == NULL) {
|
||||
@@ -404,7 +404,7 @@ ASN1_INTEGER *BN_to_ASN1_INTEGER(const BIGNUM *bn, ASN1_INTEGER *ai)
|
||||
return (ret);
|
||||
err:
|
||||
if (ret != ai)
|
||||
M_ASN1_INTEGER_free(ret);
|
||||
ASN1_INTEGER_free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
||||
@@ -63,11 +63,9 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
#include "../bytestring/internal.h"
|
||||
|
||||
static int is_printable(uint32_t value);
|
||||
|
||||
/*
|
||||
* These functions take a string in UTF8, ASCII or multibyte form and a mask
|
||||
* of permissible ASN1 string types. It then works out the minimal type
|
||||
@@ -153,7 +151,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
}
|
||||
|
||||
/* Update which output formats are still possible. */
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !is_printable(c)) {
|
||||
if ((mask & B_ASN1_PRINTABLESTRING) && !asn1_is_printable(c)) {
|
||||
mask &= ~B_ASN1_PRINTABLESTRING;
|
||||
}
|
||||
if ((mask & B_ASN1_IA5STRING) && (c > 127)) {
|
||||
@@ -208,11 +206,14 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
encode_func = cbb_add_utf32_be;
|
||||
size_estimate = 4 * nchar;
|
||||
outform = MBSTRING_UNIV;
|
||||
} else {
|
||||
} else if (mask & B_ASN1_UTF8STRING) {
|
||||
str_type = V_ASN1_UTF8STRING;
|
||||
outform = MBSTRING_UTF8;
|
||||
encode_func = cbb_add_utf8;
|
||||
size_estimate = utf8_len;
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_CHARACTERS);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!out)
|
||||
@@ -282,24 +283,16 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Return 1 if the character is permitted in a PrintableString */
|
||||
static int is_printable(uint32_t value)
|
||||
int asn1_is_printable(uint32_t value)
|
||||
{
|
||||
int ch;
|
||||
if (value > 0x7f)
|
||||
if (value > 0x7f) {
|
||||
return 0;
|
||||
ch = (int)value;
|
||||
/*
|
||||
* Note: we can't use 'isalnum' because certain accented characters may
|
||||
* count as alphanumeric in some environments.
|
||||
*/
|
||||
if ((ch >= 'a') && (ch <= 'z'))
|
||||
return 1;
|
||||
if ((ch >= 'A') && (ch <= 'Z'))
|
||||
return 1;
|
||||
if ((ch >= '0') && (ch <= '9'))
|
||||
return 1;
|
||||
if ((ch == ' ') || strchr("'()+,-./:=?", ch))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
/* Note we cannot use |isalnum| because it is locale-dependent. */
|
||||
return ('a' <= value && value <= 'z') || //
|
||||
('A' <= value && value <= 'Z') || //
|
||||
('0' <= value && value <= '9') || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
}
|
||||
@@ -64,6 +64,7 @@
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
|
||||
@@ -180,16 +181,13 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* only the ASN1_OBJECTs from the 'table' will have values for ->sn or
|
||||
* ->ln
|
||||
*/
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL)
|
||||
return (NULL);
|
||||
} else
|
||||
} else {
|
||||
ret = (*a);
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
/* detach data from object */
|
||||
@@ -208,12 +206,17 @@ ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
}
|
||||
OPENSSL_memcpy(data, p, length);
|
||||
/* If there are dynamic strings, free them here, and clear the flag */
|
||||
if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
|
||||
OPENSSL_free((char *)ret->sn);
|
||||
OPENSSL_free((char *)ret->ln);
|
||||
ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
|
||||
}
|
||||
/* reattach data to object, after which it remains const */
|
||||
ret->data = data;
|
||||
ret->length = length;
|
||||
ret->sn = NULL;
|
||||
ret->ln = NULL;
|
||||
/* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
|
||||
p += length;
|
||||
|
||||
if (a != NULL)
|
||||
@@ -250,19 +253,12 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a)
|
||||
if (a == NULL)
|
||||
return;
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
|
||||
#ifndef CONST_STRICT /* disable purely for compile-time strict
|
||||
* const checking. Doing this on a "real"
|
||||
* compile will cause memory leaks */
|
||||
if (a->sn != NULL)
|
||||
OPENSSL_free((void *)a->sn);
|
||||
if (a->ln != NULL)
|
||||
OPENSSL_free((void *)a->ln);
|
||||
#endif
|
||||
OPENSSL_free((void *)a->sn);
|
||||
OPENSSL_free((void *)a->ln);
|
||||
a->sn = a->ln = NULL;
|
||||
}
|
||||
if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
|
||||
if (a->data != NULL)
|
||||
OPENSSL_free((void *)a->data);
|
||||
OPENSSL_free((void *)a->data);
|
||||
a->data = NULL;
|
||||
a->length = 0;
|
||||
}
|
||||
@@ -270,7 +266,7 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a)
|
||||
OPENSSL_free(a);
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
|
||||
const char *sn, const char *ln)
|
||||
{
|
||||
ASN1_OBJECT o;
|
||||
|
||||
@@ -61,17 +61,17 @@
|
||||
|
||||
ASN1_OCTET_STRING *ASN1_OCTET_STRING_dup(const ASN1_OCTET_STRING *x)
|
||||
{
|
||||
return M_ASN1_OCTET_STRING_dup(x);
|
||||
return ASN1_STRING_dup(x);
|
||||
}
|
||||
|
||||
int ASN1_OCTET_STRING_cmp(const ASN1_OCTET_STRING *a,
|
||||
const ASN1_OCTET_STRING *b)
|
||||
{
|
||||
return M_ASN1_OCTET_STRING_cmp(a, b);
|
||||
return ASN1_STRING_cmp(a, b);
|
||||
}
|
||||
|
||||
int ASN1_OCTET_STRING_set(ASN1_OCTET_STRING *x, const unsigned char *d,
|
||||
int len)
|
||||
{
|
||||
return M_ASN1_OCTET_STRING_set(x, d, len);
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
@@ -56,38 +56,28 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_PRINTABLE_type(const unsigned char *s, int len)
|
||||
{
|
||||
int c;
|
||||
int ia5 = 0;
|
||||
int t61 = 0;
|
||||
|
||||
if (len <= 0)
|
||||
len = -1;
|
||||
if (s == NULL)
|
||||
return (V_ASN1_PRINTABLESTRING);
|
||||
|
||||
while ((*s) && (len-- != 0)) {
|
||||
c = *(s++);
|
||||
if (!(((c >= 'a') && (c <= 'z')) ||
|
||||
((c >= 'A') && (c <= 'Z')) ||
|
||||
(c == ' ') ||
|
||||
((c >= '0') && (c <= '9')) ||
|
||||
(c == ' ') || (c == '\'') ||
|
||||
(c == '(') || (c == ')') ||
|
||||
(c == '+') || (c == ',') ||
|
||||
(c == '-') || (c == '.') ||
|
||||
(c == '/') || (c == ':') || (c == '=') || (c == '?')))
|
||||
ia5 = 1;
|
||||
if (c & 0x80)
|
||||
t61 = 1;
|
||||
if (len < 0) {
|
||||
len = strlen((const char *)s);
|
||||
}
|
||||
if (t61)
|
||||
return (V_ASN1_T61STRING);
|
||||
if (ia5)
|
||||
return (V_ASN1_IA5STRING);
|
||||
return (V_ASN1_PRINTABLESTRING);
|
||||
|
||||
int printable = 1;
|
||||
for (int i = 0; i < len; i++) {
|
||||
unsigned char c = s[i];
|
||||
if (c & 0x80) {
|
||||
/* No need to continue iterating. */
|
||||
return V_ASN1_T61STRING;
|
||||
}
|
||||
if (!asn1_is_printable(c)) {
|
||||
printable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return printable ? V_ASN1_PRINTABLESTRING : V_ASN1_IA5STRING;
|
||||
}
|
||||
+269
-272
@@ -54,23 +54,27 @@
|
||||
* copied and put under another distribution licence
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_x509.h>
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "charmap.h"
|
||||
#include "../asn1/asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
|
||||
* printing routines handling multibyte characters, RFC2253 and a host of
|
||||
* other options.
|
||||
*/
|
||||
|
||||
// These flags must be distinct from |ESC_FLAGS| and fit in a byte.
|
||||
|
||||
// Character is a valid PrintableString character
|
||||
#define CHARTYPE_PRINTABLESTRING 0x10
|
||||
// Character needs escaping if it is the first character
|
||||
#define CHARTYPE_FIRST_ESC_2253 0x20
|
||||
// Character needs escaping if it is the last character
|
||||
#define CHARTYPE_LAST_ESC_2253 0x40
|
||||
|
||||
#define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
|
||||
|
||||
@@ -79,26 +83,12 @@
|
||||
ASN1_STRFLGS_ESC_CTRL | \
|
||||
ASN1_STRFLGS_ESC_MSB)
|
||||
|
||||
static int send_bio_chars(void *arg, const void *buf, int len)
|
||||
static int maybe_write(BIO *out, const void *buf, int len)
|
||||
{
|
||||
if (!arg)
|
||||
return 1;
|
||||
if (BIO_write(arg, buf, len) != len)
|
||||
return 0;
|
||||
return 1;
|
||||
/* If |out| is NULL, ignore the output but report the length. */
|
||||
return out == NULL || BIO_write(out, buf, len) == len;
|
||||
}
|
||||
|
||||
static int send_fp_chars(void *arg, const void *buf, int len)
|
||||
{
|
||||
if (!arg)
|
||||
return 1;
|
||||
if (fwrite(buf, 1, len, arg) != (unsigned int)len)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
typedef int char_io (void *arg, const void *buf, int len);
|
||||
|
||||
/*
|
||||
* This function handles display of strings, one character at a time. It is
|
||||
* passed an unsigned long for each character because it could come from 2 or
|
||||
@@ -108,20 +98,20 @@ typedef int char_io (void *arg, const void *buf, int len);
|
||||
#define HEX_SIZE(type) (sizeof(type)*2)
|
||||
|
||||
static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
char_io *io_ch, void *arg)
|
||||
BIO *out)
|
||||
{
|
||||
unsigned char chflgs, chtmp;
|
||||
char tmphex[HEX_SIZE(uint32_t) + 3];
|
||||
|
||||
if (c > 0xffff) {
|
||||
BIO_snprintf(tmphex, sizeof tmphex, "\\W%08" PRIX32, c);
|
||||
if (!io_ch(arg, tmphex, 10))
|
||||
if (!maybe_write(out, tmphex, 10))
|
||||
return -1;
|
||||
return 10;
|
||||
}
|
||||
if (c > 0xff) {
|
||||
BIO_snprintf(tmphex, sizeof tmphex, "\\U%04" PRIX32, c);
|
||||
if (!io_ch(arg, tmphex, 6))
|
||||
if (!maybe_write(out, tmphex, 6))
|
||||
return -1;
|
||||
return 6;
|
||||
}
|
||||
@@ -135,19 +125,19 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
|
||||
if (do_quotes)
|
||||
*do_quotes = 1;
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
if (!io_ch(arg, "\\", 1))
|
||||
if (!maybe_write(out, "\\", 1))
|
||||
return -1;
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
if (chflgs & (ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB)) {
|
||||
BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
|
||||
if (!io_ch(arg, tmphex, 3))
|
||||
if (!maybe_write(out, tmphex, 3))
|
||||
return -1;
|
||||
return 3;
|
||||
}
|
||||
@@ -156,11 +146,11 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
* character itself: backslash.
|
||||
*/
|
||||
if (chtmp == '\\' && flags & ESC_FLAGS) {
|
||||
if (!io_ch(arg, "\\\\", 2))
|
||||
if (!maybe_write(out, "\\\\", 2))
|
||||
return -1;
|
||||
return 2;
|
||||
}
|
||||
if (!io_ch(arg, &chtmp, 1))
|
||||
if (!maybe_write(out, &chtmp, 1))
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
@@ -175,8 +165,7 @@ static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
|
||||
*/
|
||||
|
||||
static int do_buf(unsigned char *buf, int buflen,
|
||||
int type, unsigned char flags, char *quotes, char_io *io_ch,
|
||||
void *arg)
|
||||
int type, unsigned char flags, char *quotes, BIO *out)
|
||||
{
|
||||
int i, outlen, len, charwidth;
|
||||
unsigned char orflags, *p, *q;
|
||||
@@ -208,6 +197,8 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
orflags = CHARTYPE_FIRST_ESC_2253;
|
||||
else
|
||||
orflags = 0;
|
||||
/* TODO(davidben): Replace this with |cbs_get_ucs2_be|, etc., to check
|
||||
* for invalid codepoints. */
|
||||
switch (charwidth) {
|
||||
case 4:
|
||||
c = ((uint32_t)*p++) << 24;
|
||||
@@ -248,17 +239,14 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
* otherwise each character will be > 0x7f and so the
|
||||
* character will never be escaped on first and last.
|
||||
*/
|
||||
len =
|
||||
do_esc_char(utfbuf[i], (unsigned char)(flags | orflags),
|
||||
quotes, io_ch, arg);
|
||||
len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags),
|
||||
quotes, out);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
}
|
||||
} else {
|
||||
len =
|
||||
do_esc_char(c, (unsigned char)(flags | orflags), quotes,
|
||||
io_ch, arg);
|
||||
len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, out);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
@@ -269,19 +257,18 @@ static int do_buf(unsigned char *buf, int buflen,
|
||||
|
||||
/* This function hex dumps a buffer of characters */
|
||||
|
||||
static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
|
||||
int buflen)
|
||||
static int do_hex_dump(BIO *out, unsigned char *buf, int buflen)
|
||||
{
|
||||
static const char hexdig[] = "0123456789ABCDEF";
|
||||
unsigned char *p, *q;
|
||||
char hextmp[2];
|
||||
if (arg) {
|
||||
if (out) {
|
||||
p = buf;
|
||||
q = buf + buflen;
|
||||
while (p != q) {
|
||||
hextmp[0] = hexdig[*p >> 4];
|
||||
hextmp[1] = hexdig[*p & 0xf];
|
||||
if (!io_ch(arg, hextmp, 2))
|
||||
if (!maybe_write(out, hextmp, 2))
|
||||
return -1;
|
||||
p++;
|
||||
}
|
||||
@@ -292,41 +279,55 @@ static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
|
||||
/*
|
||||
* "dump" a string. This is done when the type is unknown, or the flags
|
||||
* request it. We can either dump the content octets or the entire DER
|
||||
* encoding. This uses the RFC2253 #01234 format.
|
||||
* encoding. This uses the RFC 2253 #01234 format.
|
||||
*/
|
||||
|
||||
static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
|
||||
const ASN1_STRING *str)
|
||||
static int do_dump(unsigned long lflags, BIO *out, const ASN1_STRING *str)
|
||||
{
|
||||
/*
|
||||
* Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
|
||||
* readily obtained
|
||||
*/
|
||||
ASN1_TYPE t;
|
||||
unsigned char *der_buf, *p;
|
||||
int outlen, der_len;
|
||||
|
||||
if (!io_ch(arg, "#", 1))
|
||||
if (!maybe_write(out, "#", 1)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If we don't dump DER encoding just dump content octets */
|
||||
if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
|
||||
outlen = do_hex_dump(io_ch, arg, str->data, str->length);
|
||||
if (outlen < 0)
|
||||
int outlen = do_hex_dump(out, str->data, str->length);
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
|
||||
* to readily obtained.
|
||||
*/
|
||||
ASN1_TYPE t;
|
||||
t.type = str->type;
|
||||
t.value.ptr = (char *)str;
|
||||
der_len = i2d_ASN1_TYPE(&t, NULL);
|
||||
der_buf = OPENSSL_malloc(der_len);
|
||||
if (!der_buf)
|
||||
/* Negative INTEGER and ENUMERATED values are the only case where
|
||||
* |ASN1_STRING| and |ASN1_TYPE| types do not match.
|
||||
*
|
||||
* TODO(davidben): There are also some type fields which, in |ASN1_TYPE|, do
|
||||
* not correspond to |ASN1_STRING|. It is unclear whether those are allowed
|
||||
* in |ASN1_STRING| at all, or what the space of allowed types is.
|
||||
* |ASN1_item_ex_d2i| will never produce such a value so, for now, we say
|
||||
* this is an invalid input. But this corner of the library in general
|
||||
* should be more robust. */
|
||||
if (t.type == V_ASN1_NEG_INTEGER) {
|
||||
t.type = V_ASN1_INTEGER;
|
||||
} else if (t.type == V_ASN1_NEG_ENUMERATED) {
|
||||
t.type = V_ASN1_ENUMERATED;
|
||||
}
|
||||
t.value.asn1_string = (ASN1_STRING *)str;
|
||||
unsigned char *der_buf = NULL;
|
||||
int der_len = i2d_ASN1_TYPE(&t, &der_buf);
|
||||
if (der_len < 0) {
|
||||
return -1;
|
||||
p = der_buf;
|
||||
i2d_ASN1_TYPE(&t, &p);
|
||||
outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
|
||||
}
|
||||
int outlen = do_hex_dump(out, der_buf, der_len);
|
||||
OPENSSL_free(der_buf);
|
||||
if (outlen < 0)
|
||||
if (outlen < 0) {
|
||||
return -1;
|
||||
}
|
||||
return outlen + 1;
|
||||
}
|
||||
|
||||
@@ -353,8 +354,7 @@ static const signed char tag2nbyte[] = {
|
||||
* an error occurred.
|
||||
*/
|
||||
|
||||
static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
const ASN1_STRING *str)
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long lflags)
|
||||
{
|
||||
int outlen, len;
|
||||
int type;
|
||||
@@ -372,7 +372,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
const char *tagname;
|
||||
tagname = ASN1_tag2str(type);
|
||||
outlen += strlen(tagname);
|
||||
if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
|
||||
if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1))
|
||||
return -1;
|
||||
outlen++;
|
||||
}
|
||||
@@ -396,7 +396,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
}
|
||||
|
||||
if (type == -1) {
|
||||
len = do_dump(lflags, io_ch, arg, str);
|
||||
len = do_dump(lflags, out, str);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
@@ -415,219 +415,41 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
|
||||
type |= BUF_TYPE_CONVUTF8;
|
||||
}
|
||||
|
||||
len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
|
||||
len = do_buf(str->data, str->length, type, flags, "es, NULL);
|
||||
if (len < 0)
|
||||
return -1;
|
||||
outlen += len;
|
||||
if (quotes)
|
||||
outlen += 2;
|
||||
if (!arg)
|
||||
if (!out)
|
||||
return outlen;
|
||||
if (quotes && !io_ch(arg, "\"", 1))
|
||||
if (quotes && !maybe_write(out, "\"", 1))
|
||||
return -1;
|
||||
if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
|
||||
if (do_buf(str->data, str->length, type, flags, NULL, out) < 0)
|
||||
return -1;
|
||||
if (quotes && !io_ch(arg, "\"", 1))
|
||||
if (quotes && !maybe_write(out, "\"", 1))
|
||||
return -1;
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/* Used for line indenting: print 'indent' spaces */
|
||||
|
||||
static int do_indent(char_io *io_ch, void *arg, int indent)
|
||||
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
|
||||
unsigned long flags)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < indent; i++)
|
||||
if (!io_ch(arg, " ", 1))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define FN_WIDTH_LN 25
|
||||
#define FN_WIDTH_SN 10
|
||||
|
||||
static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
|
||||
int indent, unsigned long flags)
|
||||
{
|
||||
int i, prev = -1, orflags, cnt;
|
||||
int fn_opt, fn_nid;
|
||||
ASN1_OBJECT *fn;
|
||||
ASN1_STRING *val;
|
||||
X509_NAME_ENTRY *ent;
|
||||
char objtmp[80];
|
||||
const char *objbuf;
|
||||
int outlen, len;
|
||||
const char *sep_dn, *sep_mv, *sep_eq;
|
||||
int sep_dn_len, sep_mv_len, sep_eq_len;
|
||||
if (indent < 0)
|
||||
indent = 0;
|
||||
outlen = indent;
|
||||
if (!do_indent(io_ch, arg, indent))
|
||||
return -1;
|
||||
switch (flags & XN_FLAG_SEP_MASK) {
|
||||
case XN_FLAG_SEP_MULTILINE:
|
||||
sep_dn = "\n";
|
||||
sep_dn_len = 1;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_COMMA_PLUS:
|
||||
sep_dn = ",";
|
||||
sep_dn_len = 1;
|
||||
sep_mv = "+";
|
||||
sep_mv_len = 1;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_CPLUS_SPC:
|
||||
sep_dn = ", ";
|
||||
sep_dn_len = 2;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
case XN_FLAG_SEP_SPLUS_SPC:
|
||||
sep_dn = "; ";
|
||||
sep_dn_len = 2;
|
||||
sep_mv = " + ";
|
||||
sep_mv_len = 3;
|
||||
indent = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (flags & XN_FLAG_SPC_EQ) {
|
||||
sep_eq = " = ";
|
||||
sep_eq_len = 3;
|
||||
} else {
|
||||
sep_eq = "=";
|
||||
sep_eq_len = 1;
|
||||
}
|
||||
|
||||
fn_opt = flags & XN_FLAG_FN_MASK;
|
||||
|
||||
cnt = X509_NAME_entry_count(n);
|
||||
for (i = 0; i < cnt; i++) {
|
||||
if (flags & XN_FLAG_DN_REV)
|
||||
ent = X509_NAME_get_entry(n, cnt - i - 1);
|
||||
else
|
||||
ent = X509_NAME_get_entry(n, i);
|
||||
if (prev != -1) {
|
||||
if (prev == ent->set) {
|
||||
if (!io_ch(arg, sep_mv, sep_mv_len))
|
||||
return -1;
|
||||
outlen += sep_mv_len;
|
||||
} else {
|
||||
if (!io_ch(arg, sep_dn, sep_dn_len))
|
||||
return -1;
|
||||
outlen += sep_dn_len;
|
||||
if (!do_indent(io_ch, arg, indent))
|
||||
return -1;
|
||||
outlen += indent;
|
||||
}
|
||||
}
|
||||
prev = ent->set;
|
||||
fn = X509_NAME_ENTRY_get_object(ent);
|
||||
val = X509_NAME_ENTRY_get_data(ent);
|
||||
fn_nid = OBJ_obj2nid(fn);
|
||||
if (fn_opt != XN_FLAG_FN_NONE) {
|
||||
int objlen, fld_len;
|
||||
if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
|
||||
OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
|
||||
fld_len = 0; /* XXX: what should this be? */
|
||||
objbuf = objtmp;
|
||||
} else {
|
||||
if (fn_opt == XN_FLAG_FN_SN) {
|
||||
fld_len = FN_WIDTH_SN;
|
||||
objbuf = OBJ_nid2sn(fn_nid);
|
||||
} else if (fn_opt == XN_FLAG_FN_LN) {
|
||||
fld_len = FN_WIDTH_LN;
|
||||
objbuf = OBJ_nid2ln(fn_nid);
|
||||
} else {
|
||||
fld_len = 0; /* XXX: what should this be? */
|
||||
objbuf = "";
|
||||
}
|
||||
}
|
||||
objlen = strlen(objbuf);
|
||||
if (!io_ch(arg, objbuf, objlen))
|
||||
return -1;
|
||||
if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
|
||||
if (!do_indent(io_ch, arg, fld_len - objlen))
|
||||
return -1;
|
||||
outlen += fld_len - objlen;
|
||||
}
|
||||
if (!io_ch(arg, sep_eq, sep_eq_len))
|
||||
return -1;
|
||||
outlen += objlen + sep_eq_len;
|
||||
}
|
||||
/*
|
||||
* If the field name is unknown then fix up the DER dump flag. We
|
||||
* might want to limit this further so it will DER dump on anything
|
||||
* other than a few 'standard' fields.
|
||||
*/
|
||||
if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
|
||||
orflags = ASN1_STRFLGS_DUMP_ALL;
|
||||
else
|
||||
orflags = 0;
|
||||
|
||||
len = do_print_ex(io_ch, arg, flags | orflags, val);
|
||||
if (len < 0)
|
||||
BIO *bio = NULL;
|
||||
if (fp != NULL) {
|
||||
/* If |fp| is NULL, this function returns the number of bytes without
|
||||
* writing. */
|
||||
bio = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (bio == NULL) {
|
||||
return -1;
|
||||
outlen += len;
|
||||
}
|
||||
}
|
||||
return outlen;
|
||||
int ret = ASN1_STRING_print_ex(bio, str, flags);
|
||||
BIO_free(bio);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Wrappers round the main functions */
|
||||
|
||||
int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
|
||||
unsigned long flags)
|
||||
{
|
||||
if (flags == XN_FLAG_COMPAT)
|
||||
return X509_NAME_print(out, nm, indent);
|
||||
return do_name_ex(send_bio_chars, out, nm, indent, flags);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
|
||||
unsigned long flags)
|
||||
{
|
||||
if (flags == XN_FLAG_COMPAT) {
|
||||
BIO *btmp;
|
||||
int ret;
|
||||
btmp = BIO_new_fp(fp, BIO_NOCLOSE);
|
||||
if (!btmp)
|
||||
return -1;
|
||||
ret = X509_NAME_print(btmp, nm, indent);
|
||||
BIO_free(btmp);
|
||||
return ret;
|
||||
}
|
||||
return do_name_ex(send_fp_chars, fp, nm, indent, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
|
||||
{
|
||||
return do_print_ex(send_bio_chars, out, flags, str);
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_FP_API
|
||||
int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
|
||||
{
|
||||
return do_print_ex(send_fp_chars, fp, flags, str);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Utility function: convert any string type to UTF8, returns number of bytes
|
||||
* in output string or a negative error code
|
||||
*/
|
||||
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
|
||||
int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
|
||||
{
|
||||
ASN1_STRING stmp, *str = &stmp;
|
||||
int mbflag, type, ret;
|
||||
@@ -643,11 +465,186 @@ int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
|
||||
stmp.data = NULL;
|
||||
stmp.length = 0;
|
||||
stmp.flags = 0;
|
||||
ret =
|
||||
ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
|
||||
B_ASN1_UTF8STRING);
|
||||
ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
|
||||
B_ASN1_UTF8STRING);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
*out = stmp.data;
|
||||
return stmp.length;
|
||||
}
|
||||
|
||||
int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
|
||||
{
|
||||
int i, n;
|
||||
char buf[80];
|
||||
const char *p;
|
||||
|
||||
if (v == NULL)
|
||||
return (0);
|
||||
n = 0;
|
||||
p = (const char *)v->data;
|
||||
for (i = 0; i < v->length; i++) {
|
||||
if ((p[i] > '~') || ((p[i] < ' ') &&
|
||||
(p[i] != '\n') && (p[i] != '\r')))
|
||||
buf[n] = '.';
|
||||
else
|
||||
buf[n] = p[i];
|
||||
n++;
|
||||
if (n >= 80) {
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
n = 0;
|
||||
}
|
||||
}
|
||||
if (n > 0)
|
||||
if (BIO_write(bp, buf, n) <= 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
|
||||
{
|
||||
if (tm->type == V_ASN1_UTCTIME)
|
||||
return ASN1_UTCTIME_print(bp, tm);
|
||||
if (tm->type == V_ASN1_GENERALIZEDTIME)
|
||||
return ASN1_GENERALIZEDTIME_print(bp, tm);
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static const char *const mon[12] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
|
||||
{
|
||||
char *v;
|
||||
int gmt = 0;
|
||||
int i;
|
||||
int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
|
||||
char *f = NULL;
|
||||
int f_len = 0;
|
||||
|
||||
i = tm->length;
|
||||
v = (char *)tm->data;
|
||||
|
||||
if (i < 12)
|
||||
goto err;
|
||||
if (v[i - 1] == 'Z')
|
||||
gmt = 1;
|
||||
for (i = 0; i < 12; i++)
|
||||
if ((v[i] > '9') || (v[i] < '0'))
|
||||
goto err;
|
||||
y = (v[0] - '0') * 1000 + (v[1] - '0') * 100 + (v[2] - '0') * 10 + (v[3] -
|
||||
'0');
|
||||
M = (v[4] - '0') * 10 + (v[5] - '0');
|
||||
if ((M > 12) || (M < 1))
|
||||
goto err;
|
||||
d = (v[6] - '0') * 10 + (v[7] - '0');
|
||||
h = (v[8] - '0') * 10 + (v[9] - '0');
|
||||
m = (v[10] - '0') * 10 + (v[11] - '0');
|
||||
if (tm->length >= 14 &&
|
||||
(v[12] >= '0') && (v[12] <= '9') &&
|
||||
(v[13] >= '0') && (v[13] <= '9')) {
|
||||
s = (v[12] - '0') * 10 + (v[13] - '0');
|
||||
/* Check for fractions of seconds. */
|
||||
if (tm->length >= 15 && v[14] == '.') {
|
||||
int l = tm->length;
|
||||
f = &v[14]; /* The decimal point. */
|
||||
f_len = 1;
|
||||
while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
|
||||
++f_len;
|
||||
}
|
||||
}
|
||||
|
||||
if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
|
||||
mon[M - 1], d, h, m, s, f_len, f, y,
|
||||
(gmt) ? " GMT" : "") <= 0)
|
||||
return (0);
|
||||
else
|
||||
return (1);
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return (0);
|
||||
}
|
||||
|
||||
// consume_two_digits is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, has two leading digits, updates |*out| with
|
||||
// their value, updates |v| and |len|, and returns one. Otherwise, returns
|
||||
// zero.
|
||||
static int consume_two_digits(int* out, const char **v, int *len) {
|
||||
if (*len < 2|| !isdigit((*v)[0]) || !isdigit((*v)[1])) {
|
||||
return 0;
|
||||
}
|
||||
*out = ((*v)[0] - '0') * 10 + ((*v)[1] - '0');
|
||||
*len -= 2;
|
||||
*v += 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// consume_zulu_timezone is a helper function for ASN1_UTCTIME_print. If |*v|,
|
||||
// assumed to be |*len| bytes long, starts with "Z" then it updates |*v| and
|
||||
// |*len| and returns one. Otherwise returns zero.
|
||||
static int consume_zulu_timezone(const char **v, int *len) {
|
||||
if (*len == 0 || (*v)[0] != 'Z') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*len -= 1;
|
||||
*v += 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
|
||||
const char *v = (const char *)tm->data;
|
||||
int len = tm->length;
|
||||
int Y = 0, M = 0, D = 0, h = 0, m = 0, s = 0;
|
||||
|
||||
// YYMMDDhhmm are required to be present.
|
||||
if (!consume_two_digits(&Y, &v, &len) ||
|
||||
!consume_two_digits(&M, &v, &len) ||
|
||||
!consume_two_digits(&D, &v, &len) ||
|
||||
!consume_two_digits(&h, &v, &len) ||
|
||||
!consume_two_digits(&m, &v, &len)) {
|
||||
goto err;
|
||||
}
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires seconds
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
consume_two_digits(&s, &v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, specifies this
|
||||
// interpretation of the year.
|
||||
if (Y < 50) {
|
||||
Y += 2000;
|
||||
} else {
|
||||
Y += 1900;
|
||||
}
|
||||
if (M > 12 || M == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (D > 31 || D == 0) {
|
||||
goto err;
|
||||
}
|
||||
if (h > 23 || m > 59 || s > 60) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, requires the "Z"
|
||||
// to be present, but historically this code has forgiven its absence.
|
||||
const int is_gmt = consume_zulu_timezone(&v, &len);
|
||||
|
||||
// https://tools.ietf.org/html/rfc5280, section 4.1.2.5.1, does not permit
|
||||
// the specification of timezones using the +hhmm / -hhmm syntax, which is
|
||||
// the only other thing that might legitimately be found at the end.
|
||||
if (len) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", mon[M - 1], D, h, m, s, Y,
|
||||
is_gmt ? " GMT" : "") > 0;
|
||||
|
||||
err:
|
||||
BIO_write(bp, "Bad time value", 14);
|
||||
return 0;
|
||||
}
|
||||
@@ -69,53 +69,17 @@ DEFINE_STACK_OF(ASN1_STRING_TABLE)
|
||||
static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
|
||||
static void st_free(ASN1_STRING_TABLE *tbl);
|
||||
|
||||
/*
|
||||
* This is the global mask for the mbstring functions: this is use to mask
|
||||
* out certain types (such as BMPString and UTF8String) because certain
|
||||
* software (e.g. Netscape) has problems with them.
|
||||
*/
|
||||
|
||||
static unsigned long global_mask = B_ASN1_UTF8STRING;
|
||||
|
||||
void ASN1_STRING_set_default_mask(unsigned long mask)
|
||||
{
|
||||
global_mask = mask;
|
||||
}
|
||||
|
||||
unsigned long ASN1_STRING_get_default_mask(void)
|
||||
{
|
||||
return global_mask;
|
||||
return B_ASN1_UTF8STRING;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function sets the default to various "flavours" of configuration.
|
||||
* based on an ASCII string. Currently this is: MASK:XXXX : a numerical mask
|
||||
* value. nobmp : Don't use BMPStrings (just Printable, T61). pkix : PKIX
|
||||
* recommendation in RFC2459. utf8only : only use UTF8Strings (RFC2459
|
||||
* recommendation for 2004). default: the default value, Printable, T61, BMP.
|
||||
*/
|
||||
|
||||
int ASN1_STRING_set_default_mask_asc(const char *p)
|
||||
{
|
||||
unsigned long mask;
|
||||
char *end;
|
||||
if (!strncmp(p, "MASK:", 5)) {
|
||||
if (!p[5])
|
||||
return 0;
|
||||
mask = strtoul(p + 5, &end, 0);
|
||||
if (*end)
|
||||
return 0;
|
||||
} else if (!strcmp(p, "nombstr"))
|
||||
mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
|
||||
else if (!strcmp(p, "pkix"))
|
||||
mask = ~((unsigned long)B_ASN1_T61STRING);
|
||||
else if (!strcmp(p, "utf8only"))
|
||||
mask = B_ASN1_UTF8STRING;
|
||||
else if (!strcmp(p, "default"))
|
||||
mask = 0xFFFFFFFFL;
|
||||
else
|
||||
return 0;
|
||||
ASN1_STRING_set_default_mask(mask);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -139,13 +103,12 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
|
||||
if (tbl) {
|
||||
mask = tbl->mask;
|
||||
if (!(tbl->flags & STABLE_NO_MASK))
|
||||
mask &= global_mask;
|
||||
mask &= B_ASN1_UTF8STRING;
|
||||
ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
|
||||
tbl->minsize, tbl->maxsize);
|
||||
} else
|
||||
ret =
|
||||
ASN1_mbstring_copy(out, in, inlen, inform,
|
||||
DIRSTRING_TYPE & global_mask);
|
||||
} else {
|
||||
ret = ASN1_mbstring_copy(out, in, inlen, inform, B_ASN1_UTF8STRING);
|
||||
}
|
||||
if (ret <= 0)
|
||||
return NULL;
|
||||
return *out;
|
||||
@@ -155,7 +118,7 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
|
||||
* Now the tables and helper functions for the string table:
|
||||
*/
|
||||
|
||||
/* size limits: this stuff is taken straight from RFC3280 */
|
||||
/* size limits: this stuff is taken straight from RFC 3280 */
|
||||
|
||||
#define ub_name 32768
|
||||
#define ub_common_name 64
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* This is an implementation of the ASN1 Time structure which is: Time ::=
|
||||
@@ -200,7 +200,7 @@ static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ASN1_TIME_diff(int *pday, int *psec,
|
||||
int ASN1_TIME_diff(int *out_days, int *out_seconds,
|
||||
const ASN1_TIME *from, const ASN1_TIME *to)
|
||||
{
|
||||
struct tm tm_from, tm_to;
|
||||
@@ -208,5 +208,5 @@ int ASN1_TIME_diff(int *pday, int *psec,
|
||||
return 0;
|
||||
if (!asn1_time_to_tm(&tm_to, to))
|
||||
return 0;
|
||||
return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
|
||||
return OPENSSL_gmtime_diff(out_days, out_seconds, &tm_from, &tm_to);
|
||||
}
|
||||
@@ -61,20 +61,33 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a)
|
||||
{
|
||||
if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
|
||||
return (a->type);
|
||||
else
|
||||
return (0);
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a)
|
||||
{
|
||||
if (a->type == V_ASN1_BOOLEAN) {
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return a->value.ptr;
|
||||
}
|
||||
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
|
||||
{
|
||||
if (a->value.ptr != NULL) {
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
}
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN)
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
@@ -142,8 +155,7 @@ int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
|
||||
case V_ASN1_UTF8STRING:
|
||||
case V_ASN1_OTHER:
|
||||
default:
|
||||
result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
|
||||
(ASN1_STRING *)b->value.ptr);
|
||||
result = ASN1_STRING_cmp(a->value.asn1_string, b->value.asn1_string);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
||||
@@ -197,7 +197,7 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
|
||||
|
||||
if (s == NULL) {
|
||||
free_s = 1;
|
||||
s = M_ASN1_UTCTIME_new();
|
||||
s = ASN1_UTCTIME_new();
|
||||
}
|
||||
if (s == NULL)
|
||||
goto err;
|
||||
@@ -234,7 +234,7 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
|
||||
return (s);
|
||||
err:
|
||||
if (free_s && s)
|
||||
M_ASN1_UTCTIME_free(s);
|
||||
ASN1_UTCTIME_free(s);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -262,42 +262,3 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
|
||||
{
|
||||
struct tm tm;
|
||||
int offset;
|
||||
|
||||
OPENSSL_memset(&tm, '\0', sizeof tm);
|
||||
|
||||
# define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
|
||||
tm.tm_year = g2(s->data);
|
||||
if (tm.tm_year < 50)
|
||||
tm.tm_year += 100;
|
||||
tm.tm_mon = g2(s->data + 2) - 1;
|
||||
tm.tm_mday = g2(s->data + 4);
|
||||
tm.tm_hour = g2(s->data + 6);
|
||||
tm.tm_min = g2(s->data + 8);
|
||||
tm.tm_sec = g2(s->data + 10);
|
||||
if (s->data[12] == 'Z')
|
||||
offset = 0;
|
||||
else {
|
||||
offset = g2(s->data + 13) * 60 + g2(s->data + 15);
|
||||
if (s->data[12] == '-')
|
||||
offset = -offset;
|
||||
}
|
||||
# undef g2
|
||||
|
||||
return mktime(&tm) - offset * 60; /* FIXME: mktime assumes the current
|
||||
* timezone instead of UTC, and unless
|
||||
* we rewrite OpenSSL in Lisp we cannot
|
||||
* locally change the timezone without
|
||||
* possibly interfering with other
|
||||
* parts of the program. timegm, which
|
||||
* uses UTC, is non-standard. Also
|
||||
* time_t is inappropriate for general
|
||||
* UTC times because it may a 32 bit
|
||||
* type. */
|
||||
}
|
||||
#endif
|
||||
@@ -59,7 +59,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* UTF8 utilities */
|
||||
|
||||
|
||||
@@ -251,6 +251,8 @@ void ASN1_put_object(unsigned char **pp, int constructed, int length, int tag,
|
||||
|
||||
int ASN1_put_eoc(unsigned char **pp)
|
||||
{
|
||||
/* This function is no longer used in the library, but some external code
|
||||
* uses it. */
|
||||
unsigned char *p = *pp;
|
||||
*p++ = 0;
|
||||
*p++ = 0;
|
||||
@@ -311,9 +313,9 @@ int ASN1_STRING_copy(ASN1_STRING *dst, const ASN1_STRING *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return 0;
|
||||
dst->type = str->type;
|
||||
if (!ASN1_STRING_set(dst, str->data, str->length))
|
||||
return 0;
|
||||
dst->type = str->type;
|
||||
dst->flags = str->flags;
|
||||
return 1;
|
||||
}
|
||||
@@ -368,8 +370,7 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
|
||||
|
||||
void ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
|
||||
{
|
||||
if (str->data)
|
||||
OPENSSL_free(str->data);
|
||||
OPENSSL_free(str->data);
|
||||
str->data = data;
|
||||
str->length = len;
|
||||
}
|
||||
@@ -395,13 +396,12 @@ ASN1_STRING *ASN1_STRING_type_new(int type)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void ASN1_STRING_free(ASN1_STRING *a)
|
||||
void ASN1_STRING_free(ASN1_STRING *str)
|
||||
{
|
||||
if (a == NULL)
|
||||
if (str == NULL)
|
||||
return;
|
||||
if (a->data && !(a->flags & ASN1_STRING_FLAG_NDEF))
|
||||
OPENSSL_free(a->data);
|
||||
OPENSSL_free(a);
|
||||
OPENSSL_free(str->data);
|
||||
OPENSSL_free(str);
|
||||
}
|
||||
|
||||
int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
||||
@@ -419,22 +419,22 @@ int ASN1_STRING_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ASN1_STRING_length(const ASN1_STRING *x)
|
||||
int ASN1_STRING_length(const ASN1_STRING *str)
|
||||
{
|
||||
return M_ASN1_STRING_length(x);
|
||||
return str->length;
|
||||
}
|
||||
|
||||
int ASN1_STRING_type(const ASN1_STRING *x)
|
||||
int ASN1_STRING_type(const ASN1_STRING *str)
|
||||
{
|
||||
return M_ASN1_STRING_type(x);
|
||||
return str->type;
|
||||
}
|
||||
|
||||
unsigned char *ASN1_STRING_data(ASN1_STRING *x)
|
||||
unsigned char *ASN1_STRING_data(ASN1_STRING *str)
|
||||
{
|
||||
return M_ASN1_STRING_data(x);
|
||||
return str->data;
|
||||
}
|
||||
|
||||
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
|
||||
const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *str)
|
||||
{
|
||||
return x->data;
|
||||
return str->data;
|
||||
}
|
||||
@@ -72,7 +72,7 @@ const char *ASN1_tag2str(int tag)
|
||||
};
|
||||
|
||||
if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
|
||||
tag &= ~0x100;
|
||||
tag &= ~V_ASN1_NEG;
|
||||
|
||||
if (tag < 0 || tag > 30)
|
||||
return "(unknown)";
|
||||
|
||||
File renamed without changes.
+59
-1
@@ -1,4 +1,3 @@
|
||||
/* asn1t.h */
|
||||
/*
|
||||
* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
|
||||
* 2006.
|
||||
@@ -87,6 +86,26 @@ int OPENSSL_gmtime_diff(int *out_days, int *out_secs, const struct tm *from,
|
||||
|
||||
/* Internal ASN1 structures and functions: not for application use */
|
||||
|
||||
/* These are used internally in the ASN1_OBJECT to keep track of
|
||||
* whether the names and data need to be free()ed */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC 0x01 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS 0x04 /* internal use */
|
||||
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA 0x08 /* internal use */
|
||||
|
||||
/* An asn1_object_st (aka |ASN1_OBJECT|) represents an ASN.1 OBJECT IDENTIFIER.
|
||||
* Note: Mutating an |ASN1_OBJECT| is only permitted when initializing it. The
|
||||
* library maintains a table of static |ASN1_OBJECT|s, which may be referenced
|
||||
* by non-const |ASN1_OBJECT| pointers. Code which receives an |ASN1_OBJECT|
|
||||
* pointer externally must assume it is immutable, even if the pointer is not
|
||||
* const. */
|
||||
struct asn1_object_st {
|
||||
const char *sn, *ln;
|
||||
int nid;
|
||||
int length;
|
||||
const unsigned char *data; /* data remains const after init */
|
||||
int flags; /* Should we free this one */
|
||||
};
|
||||
|
||||
int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d);
|
||||
int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d);
|
||||
|
||||
@@ -96,6 +115,45 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int UTF8_getc(const unsigned char *str, int len, uint32_t *val);
|
||||
int UTF8_putc(unsigned char *str, int len, uint32_t value);
|
||||
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
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);
|
||||
|
||||
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass);
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
|
||||
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
|
||||
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
|
||||
int nullerr);
|
||||
|
||||
void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it);
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it);
|
||||
|
||||
/* asn1_type_value_as_pointer returns |a|'s value in pointer form. This is
|
||||
* usually the value object but, for BOOLEAN values, is 0 or 0xff cast to
|
||||
* a pointer. */
|
||||
const void *asn1_type_value_as_pointer(const ASN1_TYPE *a);
|
||||
|
||||
/* asn1_is_printable returns one if |value| is a valid Unicode codepoint for an
|
||||
* ASN.1 PrintableString, and zero otherwise. */
|
||||
int asn1_is_printable(uint32_t value);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern C */
|
||||
@@ -65,6 +65,7 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
/*
|
||||
* Constructed types with a recursive definition (such as can be found in PKCS7)
|
||||
@@ -95,6 +96,8 @@ 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 depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
int utype, char *free_cont, const ASN1_ITEM *it);
|
||||
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it,
|
||||
@@ -166,19 +169,16 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
char opt, ASN1_TLC *ctx, int depth)
|
||||
{
|
||||
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;
|
||||
unsigned char oclass;
|
||||
char seq_eoc, seq_nolen, cst, isopt;
|
||||
long tmplen;
|
||||
int i;
|
||||
int otag;
|
||||
int ret = 0;
|
||||
ASN1_VALUE **pchptr, *ptmpval;
|
||||
ASN1_VALUE **pchptr;
|
||||
int combine = aclass & ASN1_TFLG_COMBINE;
|
||||
aclass &= ~ASN1_TFLG_COMBINE;
|
||||
if (!pval)
|
||||
@@ -223,6 +223,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
/*
|
||||
* It never makes sense for multi-strings to have implicit tagging, so
|
||||
* if tag != -1, then this looks like an error in the template.
|
||||
*/
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
p = *in;
|
||||
/* Just read in tag and class */
|
||||
ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
|
||||
@@ -255,67 +264,16 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
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) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_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) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_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;
|
||||
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
/*
|
||||
* It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
* tag != -1, then this looks like an error in the template.
|
||||
*/
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
|
||||
goto auxerr;
|
||||
|
||||
@@ -370,10 +328,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
*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) {
|
||||
@@ -388,13 +344,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
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;
|
||||
seq_nolen = seq_eoc;
|
||||
if (!cst) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
|
||||
goto err;
|
||||
@@ -649,7 +600,7 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
|
||||
} else if (ret == -1)
|
||||
return -1;
|
||||
if (!*val)
|
||||
*val = (ASN1_VALUE *)sk_new_null();
|
||||
*val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
|
||||
else {
|
||||
/*
|
||||
* We've got a valid STACK: free up any items present
|
||||
@@ -860,19 +811,19 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
|
||||
|
||||
/* 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)
|
||||
static 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);
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of callbacks. */
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
/* If ANY type clear type and set pointer to internal value */
|
||||
if (it->utype == V_ASN1_ANY) {
|
||||
if (!*pval) {
|
||||
@@ -1055,15 +1006,13 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
|
||||
* 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
|
||||
#define ASN1_MAX_STRING_NEST 5
|
||||
|
||||
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
|
||||
char inf, int tag, int aclass, int depth)
|
||||
|
||||
@@ -63,10 +63,13 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass);
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
|
||||
const ASN1_ITEM *it);
|
||||
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
int skcontlen, const ASN1_ITEM *item,
|
||||
int do_sort, int iclass);
|
||||
@@ -76,16 +79,9 @@ static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
|
||||
const ASN1_ITEM *it, int flags);
|
||||
|
||||
/*
|
||||
* Top level i2d equivalents: the 'ndef' variant instructs the encoder to use
|
||||
* indefinite length constructed encoding, where appropriate
|
||||
* Top level i2d equivalents
|
||||
*/
|
||||
|
||||
int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
|
||||
const ASN1_ITEM *it)
|
||||
{
|
||||
return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
|
||||
}
|
||||
|
||||
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
|
||||
{
|
||||
return asn1_item_flags_i2d(val, out, it, 0);
|
||||
@@ -128,9 +124,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it, int tag, int aclass)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
unsigned char *p = NULL;
|
||||
int i, seqcontlen, seqlen, ndef = 1;
|
||||
const ASN1_COMPAT_FUNCS *cf;
|
||||
int i, seqcontlen, seqlen;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb = 0;
|
||||
@@ -151,9 +145,25 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_MSTRING:
|
||||
/*
|
||||
* It never makes sense for multi-strings to have implicit tagging, so
|
||||
* if tag != -1, then this looks like an error in the template.
|
||||
*/
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
|
||||
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
/*
|
||||
* It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
* tag != -1, then this looks like an error in the template.
|
||||
*/
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
|
||||
return 0;
|
||||
i = asn1_get_choice_selector(pval, it);
|
||||
@@ -174,26 +184,6 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
ef = it->funcs;
|
||||
return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
||||
|
||||
case ASN1_ITYPE_COMPAT:
|
||||
/* old style hackery... */
|
||||
cf = it->funcs;
|
||||
if (out)
|
||||
p = *out;
|
||||
i = cf->asn1_i2d(*pval, out);
|
||||
/*
|
||||
* Fixup for IMPLICIT tag: note this messes up for tags > 30, but so
|
||||
* did the old code. Tags > 30 are very rare anyway.
|
||||
*/
|
||||
if (out && (tag != -1))
|
||||
*p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
|
||||
return i;
|
||||
|
||||
case ASN1_ITYPE_NDEF_SEQUENCE:
|
||||
/* Use indefinite length constructed if requested */
|
||||
if (aclass & ASN1_TFLG_NDEF)
|
||||
ndef = 2;
|
||||
OPENSSL_FALLTHROUGH;
|
||||
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
i = asn1_enc_restore(&seqcontlen, out, pval, it);
|
||||
/* An error occurred */
|
||||
@@ -228,11 +218,11 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
seqcontlen += tmplen;
|
||||
}
|
||||
|
||||
seqlen = ASN1_object_size(ndef, seqcontlen, tag);
|
||||
seqlen = ASN1_object_size(/*constructed=*/1, seqcontlen, tag);
|
||||
if (!out || seqlen == -1)
|
||||
return seqlen;
|
||||
/* Output SEQUENCE header */
|
||||
ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
|
||||
ASN1_put_object(out, /*constructed=*/1, seqcontlen, tag, aclass);
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
const ASN1_TEMPLATE *seqtt;
|
||||
ASN1_VALUE **pseqval;
|
||||
@@ -243,8 +233,6 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
/* FIXME: check for errors in enhanced version */
|
||||
asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
|
||||
}
|
||||
if (ndef == 2)
|
||||
ASN1_put_eoc(out);
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
|
||||
return 0;
|
||||
return seqlen;
|
||||
@@ -259,7 +247,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int iclass)
|
||||
{
|
||||
int i, ret, flags, ttag, tclass, ndef;
|
||||
int i, ret, flags, ttag, tclass;
|
||||
size_t j;
|
||||
flags = tt->flags;
|
||||
/*
|
||||
@@ -295,12 +283,6 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
* class and iclass is any flags passed to this function.
|
||||
*/
|
||||
|
||||
/* if template and arguments require ndef, use it */
|
||||
if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
|
||||
ndef = 2;
|
||||
else
|
||||
ndef = 1;
|
||||
|
||||
if (flags & ASN1_TFLG_SK_MASK) {
|
||||
/* SET OF, SEQUENCE OF */
|
||||
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
||||
@@ -313,11 +295,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
|
||||
if (flags & ASN1_TFLG_SET_OF) {
|
||||
isset = 1;
|
||||
/* 2 means we reorder */
|
||||
if (flags & ASN1_TFLG_SEQUENCE_OF)
|
||||
isset = 2;
|
||||
} else
|
||||
/* Historically, types with both bits set were mutated when
|
||||
* serialized to apply the sort. We no longer support this. */
|
||||
assert((flags & ASN1_TFLG_SEQUENCE_OF) == 0);
|
||||
} else {
|
||||
isset = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Work out inner tag value: if EXPLICIT or no tagging use underlying
|
||||
@@ -345,12 +328,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
skcontlen += tmplen;
|
||||
}
|
||||
sklen = ASN1_object_size(ndef, skcontlen, sktag);
|
||||
sklen = ASN1_object_size(/*constructed=*/1, skcontlen, sktag);
|
||||
if (sklen == -1)
|
||||
return -1;
|
||||
/* If EXPLICIT need length of surrounding tag */
|
||||
if (flags & ASN1_TFLG_EXPTAG)
|
||||
ret = ASN1_object_size(ndef, sklen, ttag);
|
||||
ret = ASN1_object_size(/*constructed=*/1, sklen, ttag);
|
||||
else
|
||||
ret = sklen;
|
||||
|
||||
@@ -360,18 +343,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
/* Now encode this lot... */
|
||||
/* EXPLICIT tag */
|
||||
if (flags & ASN1_TFLG_EXPTAG)
|
||||
ASN1_put_object(out, ndef, sklen, ttag, tclass);
|
||||
ASN1_put_object(out, /*constructed=*/1, sklen, ttag, tclass);
|
||||
/* SET or SEQUENCE and IMPLICIT tag */
|
||||
ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
|
||||
ASN1_put_object(out, /*constructed=*/1, skcontlen, sktag, skaclass);
|
||||
/* And the stuff itself */
|
||||
asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
|
||||
isset, iclass);
|
||||
if (ndef == 2) {
|
||||
ASN1_put_eoc(out);
|
||||
if (flags & ASN1_TFLG_EXPTAG)
|
||||
ASN1_put_eoc(out);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -382,13 +359,11 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
if (!i)
|
||||
return 0;
|
||||
/* Find length of EXPLICIT tag */
|
||||
ret = ASN1_object_size(ndef, i, ttag);
|
||||
ret = ASN1_object_size(/*constructed=*/1, i, ttag);
|
||||
if (out && ret != -1) {
|
||||
/* Output tag and item */
|
||||
ASN1_put_object(out, ndef, i, ttag, tclass);
|
||||
ASN1_put_object(out, /*constructed=*/1, i, ttag, tclass);
|
||||
ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass);
|
||||
if (ndef == 2)
|
||||
ASN1_put_eoc(out);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -404,7 +379,6 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
typedef struct {
|
||||
unsigned char *data;
|
||||
int length;
|
||||
ASN1_VALUE *field;
|
||||
} DER_ENC;
|
||||
|
||||
static int der_cmp(const void *a, const void *b)
|
||||
@@ -459,7 +433,6 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
skitem = sk_ASN1_VALUE_value(sk, i);
|
||||
tder->data = p;
|
||||
tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
|
||||
tder->field = skitem;
|
||||
}
|
||||
|
||||
/* Now sort them */
|
||||
@@ -471,11 +444,6 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
p += tder->length;
|
||||
}
|
||||
*out = p;
|
||||
/* If do_sort is 2 then reorder the STACK */
|
||||
if (do_sort == 2) {
|
||||
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
|
||||
(void)sk_ASN1_VALUE_set(sk, i, tder->field);
|
||||
}
|
||||
OPENSSL_free(derlst);
|
||||
OPENSSL_free(tmpdat);
|
||||
return 1;
|
||||
@@ -487,7 +455,6 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
int len;
|
||||
int utype;
|
||||
int usetag;
|
||||
int ndef = 0;
|
||||
|
||||
utype = it->utype;
|
||||
|
||||
@@ -513,12 +480,6 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
if (len == -1)
|
||||
return 0;
|
||||
|
||||
/* -2 return is special meaning use ndef */
|
||||
if (len == -2) {
|
||||
ndef = 2;
|
||||
len = 0;
|
||||
}
|
||||
|
||||
/* If not implicitly tagged get tag from underlying type */
|
||||
if (tag == -1)
|
||||
tag = utype;
|
||||
@@ -526,23 +487,20 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
|
||||
/* Output tag+length followed by content octets */
|
||||
if (out) {
|
||||
if (usetag)
|
||||
ASN1_put_object(out, ndef, len, tag, aclass);
|
||||
ASN1_put_object(out, /*constructed=*/0, len, tag, aclass);
|
||||
asn1_ex_i2c(pval, *out, &utype, it);
|
||||
if (ndef)
|
||||
ASN1_put_eoc(out);
|
||||
else
|
||||
*out += len;
|
||||
*out += len;
|
||||
}
|
||||
|
||||
if (usetag)
|
||||
return ASN1_object_size(ndef, len, tag);
|
||||
return ASN1_object_size(/*constructed=*/0, len, tag);
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Produce content octets from a structure */
|
||||
|
||||
int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
const ASN1_ITEM *it)
|
||||
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_BOOLEAN *tbool = NULL;
|
||||
ASN1_STRING *strtmp;
|
||||
@@ -551,10 +509,10 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
const unsigned char *cont;
|
||||
unsigned char c;
|
||||
int len;
|
||||
const ASN1_PRIMITIVE_FUNCS *pf;
|
||||
pf = it->funcs;
|
||||
if (pf && pf->prim_i2c)
|
||||
return pf->prim_i2c(pval, cout, putype, it);
|
||||
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of callbacks. */
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
/* Should type be omitted? */
|
||||
if ((it->itype != ASN1_ITYPE_PRIMITIVE)
|
||||
@@ -567,6 +525,20 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
/* If MSTRING type set the underlying type */
|
||||
strtmp = (ASN1_STRING *)*pval;
|
||||
utype = strtmp->type;
|
||||
/* Negative INTEGER and ENUMERATED values use |ASN1_STRING| type values
|
||||
* that do not match their corresponding utype values. INTEGERs cannot
|
||||
* participate in MSTRING types, but ENUMERATEDs can.
|
||||
*
|
||||
* TODO(davidben): Is this a bug? Although arguably one of the MSTRING
|
||||
* types should contain more values, rather than less. See
|
||||
* https://crbug.com/boringssl/412. But it is not possible to fit all
|
||||
* possible ANY values into an |ASN1_STRING|, so matching the spec here
|
||||
* is somewhat hopeless. */
|
||||
if (utype == V_ASN1_NEG_INTEGER) {
|
||||
utype = V_ASN1_INTEGER;
|
||||
} else if (utype == V_ASN1_NEG_ENUMERATED) {
|
||||
utype = V_ASN1_ENUMERATED;
|
||||
}
|
||||
*putype = utype;
|
||||
} else if (it->utype == V_ASN1_ANY) {
|
||||
/* If ANY set type and pointer to value */
|
||||
@@ -605,7 +577,7 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
if (!*tbool && !it->size)
|
||||
return -1;
|
||||
}
|
||||
c = (unsigned char)*tbool;
|
||||
c = *tbool ? 0xff : 0x00;
|
||||
cont = &c;
|
||||
len = 1;
|
||||
break;
|
||||
@@ -642,16 +614,6 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
|
||||
default:
|
||||
/* All based on ASN1_STRING and handled the same */
|
||||
strtmp = (ASN1_STRING *)*pval;
|
||||
/* Special handling for NDEF */
|
||||
if ((it->size == ASN1_TFLG_NDEF)
|
||||
&& (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
|
||||
if (cout) {
|
||||
strtmp->data = cout;
|
||||
strtmp->length = 0;
|
||||
}
|
||||
/* Special return code */
|
||||
return -2;
|
||||
}
|
||||
cont = strtmp->data;
|
||||
len = strtmp->length;
|
||||
|
||||
|
||||
@@ -56,10 +56,12 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
/* Free up an ASN1 structure */
|
||||
|
||||
@@ -77,7 +79,6 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL, *seqtt;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_COMPAT_FUNCS *cf;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb;
|
||||
int i;
|
||||
@@ -124,19 +125,12 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_COMPAT:
|
||||
cf = it->funcs;
|
||||
if (cf && cf->asn1_free)
|
||||
cf->asn1_free(*pval);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
ef = it->funcs;
|
||||
if (ef && ef->asn1_ex_free)
|
||||
ef->asn1_ex_free(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_NDEF_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
if (!asn1_refcount_dec_and_test_zero(pval, it))
|
||||
return;
|
||||
@@ -190,20 +184,15 @@ void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
int utype;
|
||||
if (it) {
|
||||
const ASN1_PRIMITIVE_FUNCS *pf;
|
||||
pf = it->funcs;
|
||||
if (pf && pf->prim_free) {
|
||||
pf->prim_free(pval, it);
|
||||
return;
|
||||
}
|
||||
}
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
/* Special case: if 'it' is NULL free contents of ASN1_TYPE */
|
||||
if (!it) {
|
||||
ASN1_TYPE *typ = (ASN1_TYPE *)*pval;
|
||||
utype = typ->type;
|
||||
pval = &typ->value.asn1_value;
|
||||
if (!*pval)
|
||||
if (utype != V_ASN1_BOOLEAN && !*pval)
|
||||
return;
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
|
||||
@@ -63,14 +63,16 @@
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine);
|
||||
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
|
||||
@@ -92,7 +94,6 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine)
|
||||
{
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_COMPAT_FUNCS *cf;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
ASN1_aux_cb *asn1_cb;
|
||||
@@ -103,11 +104,6 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
else
|
||||
asn1_cb = 0;
|
||||
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_push_info(it->sname);
|
||||
#endif
|
||||
|
||||
switch (it->itype) {
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
@@ -118,15 +114,6 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_COMPAT:
|
||||
cf = it->funcs;
|
||||
if (cf && cf->asn1_new) {
|
||||
*pval = cf->asn1_new();
|
||||
if (!*pval)
|
||||
goto memerr;
|
||||
}
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates) {
|
||||
if (!ASN1_template_new(pval, it->templates))
|
||||
@@ -146,10 +133,6 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (!i)
|
||||
goto auxerr;
|
||||
if (i == 2) {
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -164,17 +147,12 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
goto auxerr2;
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_NDEF_SEQUENCE:
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
if (asn1_cb) {
|
||||
i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
|
||||
if (!i)
|
||||
goto auxerr;
|
||||
if (i == 2) {
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -195,30 +173,18 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
goto auxerr2;
|
||||
break;
|
||||
}
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return 1;
|
||||
|
||||
memerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
memerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
auxerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -248,16 +214,14 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
asn1_primitive_clear(pval, it);
|
||||
break;
|
||||
|
||||
case ASN1_ITYPE_COMPAT:
|
||||
case ASN1_ITYPE_CHOICE:
|
||||
case ASN1_ITYPE_SEQUENCE:
|
||||
case ASN1_ITYPE_NDEF_SEQUENCE:
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
{
|
||||
const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
|
||||
int ret;
|
||||
@@ -271,10 +235,6 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
*pval = NULL;
|
||||
return 1;
|
||||
}
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (tt->field_name)
|
||||
CRYPTO_push_info(tt->field_name);
|
||||
#endif
|
||||
/* If SET OF or SEQUENCE OF, its a STACK */
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *skval;
|
||||
@@ -291,10 +251,6 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
/* Otherwise pass it back to the item routine */
|
||||
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
|
||||
done:
|
||||
#ifdef CRYPTO_MDEBUG
|
||||
if (it->sname)
|
||||
CRYPTO_pop_info();
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -312,20 +268,17 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
|
||||
* all the old functions.
|
||||
*/
|
||||
|
||||
int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
ASN1_TYPE *typ;
|
||||
ASN1_STRING *str;
|
||||
int utype;
|
||||
|
||||
if (!it)
|
||||
return 0;
|
||||
|
||||
if (it->funcs) {
|
||||
const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
|
||||
if (pf->prim_new)
|
||||
return pf->prim_new(pval, it);
|
||||
}
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
if (it->itype == ASN1_ITYPE_MSTRING)
|
||||
utype = -1;
|
||||
@@ -354,10 +307,7 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
break;
|
||||
|
||||
default:
|
||||
str = ASN1_STRING_type_new(utype);
|
||||
if (it->itype == ASN1_ITYPE_MSTRING && str)
|
||||
str->flags |= ASN1_STRING_FLAG_MSTRING;
|
||||
*pval = (ASN1_VALUE *)str;
|
||||
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
|
||||
break;
|
||||
}
|
||||
if (*pval)
|
||||
@@ -368,14 +318,9 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
|
||||
{
|
||||
int utype;
|
||||
if (it && it->funcs) {
|
||||
const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
|
||||
if (pf->prim_clear)
|
||||
pf->prim_clear(pval, it);
|
||||
else
|
||||
*pval = NULL;
|
||||
return;
|
||||
}
|
||||
/* Historically, |it->funcs| for primitive types contained an
|
||||
* |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
|
||||
assert(it == NULL || it->funcs == NULL);
|
||||
if (!it || (it->itype == ASN1_ITYPE_MSTRING))
|
||||
utype = -1;
|
||||
else
|
||||
|
||||
@@ -117,8 +117,6 @@ IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
|
||||
|
||||
/* Special, OCTET STRING with indefinite length constructed support */
|
||||
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF)
|
||||
|
||||
ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
|
||||
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
|
||||
ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
|
||||
|
||||
@@ -66,6 +66,7 @@
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
/* Utility functions for manipulating fields and offsets */
|
||||
@@ -91,8 +92,7 @@ int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
|
||||
|
||||
static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it) {
|
||||
if (it->itype != ASN1_ITYPE_SEQUENCE &&
|
||||
it->itype != ASN1_ITYPE_NDEF_SEQUENCE) {
|
||||
if (it->itype != ASN1_ITYPE_SEQUENCE) {
|
||||
return NULL;
|
||||
}
|
||||
const ASN1_AUX *aux = it->funcs;
|
||||
|
||||
@@ -59,7 +59,7 @@
|
||||
#define _POSIX_C_SOURCE 201410L /* for gmtime_r */
|
||||
#endif
|
||||
|
||||
#include "asn1_locl.h"
|
||||
#include "internal.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
@@ -262,6 +262,8 @@ int BIO_should_io_special(const BIO *bio) {
|
||||
|
||||
int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
|
||||
|
||||
void BIO_set_retry_reason(BIO *bio, int reason) { bio->retry_reason = reason; }
|
||||
|
||||
void BIO_clear_flags(BIO *bio, int flags) {
|
||||
bio->flags &= ~flags;
|
||||
}
|
||||
|
||||
@@ -116,17 +116,11 @@ static int mem_new(BIO *bio) {
|
||||
}
|
||||
|
||||
static int mem_free(BIO *bio) {
|
||||
BUF_MEM *b;
|
||||
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
b = (BUF_MEM *)bio->ptr;
|
||||
BUF_MEM *b = (BUF_MEM *)bio->ptr;
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
b->data = NULL;
|
||||
}
|
||||
|
||||
@@ -320,7 +320,7 @@ static int conn_new(BIO *bio) {
|
||||
bio->init = 0;
|
||||
bio->num = -1;
|
||||
bio->flags = 0;
|
||||
bio->ptr = (char *)BIO_CONNECT_new();
|
||||
bio->ptr = BIO_CONNECT_new();
|
||||
return bio->ptr != NULL;
|
||||
}
|
||||
|
||||
@@ -340,10 +340,6 @@ static void conn_close_socket(BIO *bio) {
|
||||
}
|
||||
|
||||
static int conn_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
conn_close_socket(bio);
|
||||
}
|
||||
|
||||
@@ -146,10 +146,6 @@ static int fd_new(BIO *bio) {
|
||||
}
|
||||
|
||||
static int fd_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
if (bio->init) {
|
||||
BORINGSSL_CLOSE(bio->num);
|
||||
|
||||
@@ -126,13 +126,7 @@ BIO *BIO_new_fp(FILE *stream, int close_flag) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int file_new(BIO *bio) { return 1; }
|
||||
|
||||
static int file_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!bio->shutdown) {
|
||||
return 1;
|
||||
}
|
||||
@@ -279,7 +273,7 @@ static const BIO_METHOD methods_filep = {
|
||||
BIO_TYPE_FILE, "FILE pointer",
|
||||
file_write, file_read,
|
||||
NULL /* puts */, file_gets,
|
||||
file_ctrl, file_new,
|
||||
file_ctrl, NULL /* create */,
|
||||
file_free, NULL /* callback_ctrl */,
|
||||
};
|
||||
|
||||
|
||||
@@ -127,12 +127,7 @@ static void bio_destroy_pair(BIO *bio) {
|
||||
}
|
||||
|
||||
static int bio_free(BIO *bio) {
|
||||
struct bio_bio_st *b;
|
||||
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
b = bio->ptr;
|
||||
struct bio_bio_st *b = bio->ptr;
|
||||
|
||||
assert(b != NULL);
|
||||
|
||||
|
||||
@@ -81,19 +81,7 @@ static int closesocket(int sock) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static int sock_new(BIO *bio) {
|
||||
bio->init = 0;
|
||||
bio->num = 0;
|
||||
bio->ptr = NULL;
|
||||
bio->flags = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sock_free(BIO *bio) {
|
||||
if (bio == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bio->shutdown) {
|
||||
if (bio->init) {
|
||||
closesocket(bio->num);
|
||||
@@ -105,17 +93,15 @@ static int sock_free(BIO *bio) {
|
||||
}
|
||||
|
||||
static int sock_read(BIO *b, char *out, int outl) {
|
||||
int ret = 0;
|
||||
|
||||
if (out == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bio_clear_socket_error();
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
ret = recv(b->num, out, outl, 0);
|
||||
int ret = recv(b->num, out, outl, 0);
|
||||
#else
|
||||
ret = read(b->num, out, outl);
|
||||
int ret = read(b->num, out, outl);
|
||||
#endif
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
@@ -186,7 +172,7 @@ static const BIO_METHOD methods_sockp = {
|
||||
BIO_TYPE_SOCKET, "socket",
|
||||
sock_write, sock_read,
|
||||
NULL /* puts */, NULL /* gets, */,
|
||||
sock_ctrl, sock_new,
|
||||
sock_ctrl, NULL /* create */,
|
||||
sock_free, NULL /* callback_ctrl */,
|
||||
};
|
||||
|
||||
|
||||
@@ -55,7 +55,11 @@ int bio_ip_and_port_to_socket_and_addr(int *out_sock,
|
||||
ret = getaddrinfo(hostname, port_str, &hint, &result);
|
||||
if (ret != 0) {
|
||||
OPENSSL_PUT_ERROR(SYS, 0);
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
ERR_add_error_data(1, gai_strerrorA(ret));
|
||||
#else
|
||||
ERR_add_error_data(1, gai_strerror(ret));
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/* Copyright (c) 2021, Google Inc.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_blake2.h>
|
||||
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
// https://tools.ietf.org/html/rfc7693#section-2.6
|
||||
static const uint64_t kIV[8] = {
|
||||
UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
|
||||
UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
|
||||
UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
|
||||
UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179),
|
||||
};
|
||||
|
||||
// https://tools.ietf.org/html/rfc7693#section-2.7
|
||||
static const uint8_t kSigma[10 * 16] = {
|
||||
// clang-format off
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
|
||||
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
|
||||
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
|
||||
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
|
||||
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
|
||||
12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
|
||||
13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
|
||||
6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
|
||||
10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
|
||||
// clang-format on
|
||||
};
|
||||
|
||||
#define RIGHT_ROTATE(v, n) (((v) >> (n)) | ((v) << (64 - (n))))
|
||||
|
||||
// https://tools.ietf.org/html/rfc7693#section-3.1
|
||||
static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x,
|
||||
uint64_t y) {
|
||||
v[a] = v[a] + v[b] + x;
|
||||
v[d] = RIGHT_ROTATE(v[d] ^ v[a], 32);
|
||||
v[c] = v[c] + v[d];
|
||||
v[b] = RIGHT_ROTATE(v[b] ^ v[c], 24);
|
||||
v[a] = v[a] + v[b] + y;
|
||||
v[d] = RIGHT_ROTATE(v[d] ^ v[a], 16);
|
||||
v[c] = v[c] + v[d];
|
||||
v[b] = RIGHT_ROTATE(v[b] ^ v[c], 63);
|
||||
}
|
||||
|
||||
static void blake2b_transform(
|
||||
BLAKE2B_CTX *b2b,
|
||||
const uint64_t block_words[BLAKE2B_CBLOCK / sizeof(uint64_t)],
|
||||
size_t num_bytes, int is_final_block) {
|
||||
// https://tools.ietf.org/html/rfc7693#section-3.2
|
||||
uint64_t v[16];
|
||||
OPENSSL_STATIC_ASSERT(sizeof(v) == sizeof(b2b->h) + sizeof(kIV), "");
|
||||
OPENSSL_memcpy(v, b2b->h, sizeof(b2b->h));
|
||||
OPENSSL_memcpy(&v[8], kIV, sizeof(kIV));
|
||||
|
||||
b2b->t_low += num_bytes;
|
||||
if (b2b->t_low < num_bytes) {
|
||||
b2b->t_high++;
|
||||
}
|
||||
v[12] ^= b2b->t_low;
|
||||
v[13] ^= b2b->t_high;
|
||||
|
||||
if (is_final_block) {
|
||||
v[14] = ~v[14];
|
||||
}
|
||||
|
||||
for (int round = 0; round < 12; round++) {
|
||||
const uint8_t *const s = &kSigma[16 * (round % 10)];
|
||||
blake2b_mix(v, 0, 4, 8, 12, block_words[s[0]], block_words[s[1]]);
|
||||
blake2b_mix(v, 1, 5, 9, 13, block_words[s[2]], block_words[s[3]]);
|
||||
blake2b_mix(v, 2, 6, 10, 14, block_words[s[4]], block_words[s[5]]);
|
||||
blake2b_mix(v, 3, 7, 11, 15, block_words[s[6]], block_words[s[7]]);
|
||||
blake2b_mix(v, 0, 5, 10, 15, block_words[s[8]], block_words[s[9]]);
|
||||
blake2b_mix(v, 1, 6, 11, 12, block_words[s[10]], block_words[s[11]]);
|
||||
blake2b_mix(v, 2, 7, 8, 13, block_words[s[12]], block_words[s[13]]);
|
||||
blake2b_mix(v, 3, 4, 9, 14, block_words[s[14]], block_words[s[15]]);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(b2b->h); i++) {
|
||||
b2b->h[i] ^= v[i];
|
||||
b2b->h[i] ^= v[i + 8];
|
||||
}
|
||||
}
|
||||
|
||||
void BLAKE2B256_Init(BLAKE2B_CTX *b2b) {
|
||||
OPENSSL_memset(b2b, 0, sizeof(BLAKE2B_CTX));
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(kIV) == sizeof(b2b->h), "");
|
||||
OPENSSL_memcpy(&b2b->h, kIV, sizeof(kIV));
|
||||
|
||||
// https://tools.ietf.org/html/rfc7693#section-2.5
|
||||
b2b->h[0] ^= 0x01010000 | BLAKE2B256_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) {
|
||||
const uint8_t *data = (const uint8_t *)in_data;
|
||||
|
||||
size_t todo = sizeof(b2b->block.bytes) - b2b->block_used;
|
||||
if (todo > len) {
|
||||
todo = len;
|
||||
}
|
||||
OPENSSL_memcpy(&b2b->block.bytes[b2b->block_used], data, todo);
|
||||
b2b->block_used += todo;
|
||||
data += todo;
|
||||
len -= todo;
|
||||
|
||||
if (!len) {
|
||||
return;
|
||||
}
|
||||
|
||||
// More input remains therefore we must have filled |b2b->block|.
|
||||
assert(b2b->block_used == BLAKE2B_CBLOCK);
|
||||
blake2b_transform(b2b, b2b->block.words, BLAKE2B_CBLOCK,
|
||||
/*is_final_block=*/0);
|
||||
b2b->block_used = 0;
|
||||
|
||||
while (len > BLAKE2B_CBLOCK) {
|
||||
uint64_t block_words[BLAKE2B_CBLOCK / sizeof(uint64_t)];
|
||||
OPENSSL_memcpy(block_words, data, sizeof(block_words));
|
||||
blake2b_transform(b2b, block_words, BLAKE2B_CBLOCK, /*is_final_block=*/0);
|
||||
data += BLAKE2B_CBLOCK;
|
||||
len -= BLAKE2B_CBLOCK;
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(b2b->block.bytes, data, len);
|
||||
b2b->block_used = len;
|
||||
}
|
||||
|
||||
void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b) {
|
||||
OPENSSL_memset(&b2b->block.bytes[b2b->block_used], 0,
|
||||
sizeof(b2b->block.bytes) - b2b->block_used);
|
||||
blake2b_transform(b2b, b2b->block.words, b2b->block_used,
|
||||
/*is_final_block=*/1);
|
||||
OPENSSL_STATIC_ASSERT(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), "");
|
||||
memcpy(out, b2b->h, BLAKE2B256_DIGEST_LENGTH);
|
||||
}
|
||||
|
||||
void BLAKE2B256(const uint8_t *data, size_t len,
|
||||
uint8_t out[BLAKE2B256_DIGEST_LENGTH]) {
|
||||
BLAKE2B_CTX ctx;
|
||||
BLAKE2B256_Init(&ctx);
|
||||
BLAKE2B256_Update(&ctx, data, len);
|
||||
BLAKE2B256_Final(out, &ctx);
|
||||
}
|
||||
@@ -20,25 +20,18 @@
|
||||
|
||||
int BN_parse_asn1_unsigned(CBS *cbs, BIGNUM *ret) {
|
||||
CBS child;
|
||||
int is_negative;
|
||||
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_INTEGER) ||
|
||||
CBS_len(&child) == 0) {
|
||||
!CBS_is_valid_asn1_integer(&child, &is_negative)) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (CBS_data(&child)[0] & 0x80) {
|
||||
if (is_negative) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// INTEGERs must be minimal.
|
||||
if (CBS_data(&child)[0] == 0x00 &&
|
||||
CBS_len(&child) > 1 &&
|
||||
!(CBS_data(&child)[1] & 0x80)) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return BN_bin2bn(CBS_data(&child), CBS_len(&child), ret) != NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ static int is_string_type(unsigned tag) {
|
||||
// depending on whether an indefinite length element or constructed string was
|
||||
// found. The value of |orig_in| is not changed. It returns one on success (i.e.
|
||||
// |*ber_found| was set) and zero on error.
|
||||
static int cbs_find_ber(const CBS *orig_in, char *ber_found, unsigned depth) {
|
||||
static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
|
||||
CBS in;
|
||||
|
||||
if (depth > kMaxDepth) {
|
||||
@@ -68,14 +68,11 @@ static int cbs_find_ber(const CBS *orig_in, char *ber_found, unsigned depth) {
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
|
||||
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len)) {
|
||||
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
|
||||
ber_found)) {
|
||||
return 0;
|
||||
}
|
||||
if (CBS_len(&contents) == header_len &&
|
||||
header_len > 0 &&
|
||||
CBS_data(&contents)[header_len-1] == 0x80) {
|
||||
// Found an indefinite-length element.
|
||||
*ber_found = 1;
|
||||
if (*ber_found) {
|
||||
return 1;
|
||||
}
|
||||
if (tag & CBS_ASN1_CONSTRUCTED) {
|
||||
@@ -120,9 +117,11 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
CBS contents;
|
||||
unsigned tag, child_string_tag = string_tag;
|
||||
size_t header_len;
|
||||
int ber_found;
|
||||
CBB *out_contents, out_contents_storage;
|
||||
|
||||
if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len)) {
|
||||
if (!CBS_get_any_ber_asn1_element(in, &contents, &tag, &header_len,
|
||||
&ber_found)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -194,7 +193,7 @@ int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) {
|
||||
|
||||
// First, do a quick walk to find any indefinite-length elements. Most of the
|
||||
// time we hope that there aren't any and thus we can quickly return.
|
||||
char conversion_needed;
|
||||
int conversion_needed;
|
||||
if (!cbs_find_ber(in, &conversion_needed, 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -404,6 +404,15 @@ int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBB_add_zeros(CBB *cbb, size_t len) {
|
||||
uint8_t *out;
|
||||
if (!CBB_add_space(cbb, &out, len)) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memset(out, 0, len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
|
||||
if (!CBB_flush(cbb) ||
|
||||
!cbb_buffer_add(cbb->base, out_data, len)) {
|
||||
|
||||
@@ -216,6 +216,14 @@ int CBS_get_u24_length_prefixed(CBS *cbs, CBS *out) {
|
||||
return cbs_get_length_prefixed(cbs, out, 3);
|
||||
}
|
||||
|
||||
int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
|
||||
const uint8_t *split = OPENSSL_memchr(CBS_data(cbs), c, CBS_len(cbs));
|
||||
if (split == NULL) {
|
||||
return 0;
|
||||
}
|
||||
return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
|
||||
}
|
||||
|
||||
// parse_base128_integer reads a big-endian base-128 integer from |cbs| and sets
|
||||
// |*out| to the result. This is the encoding used in DER for both high tag
|
||||
// number form and OID components.
|
||||
@@ -254,8 +262,7 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
//
|
||||
// If the number portion is 31 (0x1f, the largest value that fits in the
|
||||
// allotted bits), then the tag is more than one byte long and the
|
||||
// continuation bytes contain the tag number. This parser only supports tag
|
||||
// numbers less than 31 (and thus single-byte tags).
|
||||
// continuation bytes contain the tag number.
|
||||
unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
|
||||
unsigned tag_number = tag_byte & 0x1f;
|
||||
if (tag_number == 0x1f) {
|
||||
@@ -263,7 +270,7 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
if (!parse_base128_integer(cbs, &v) ||
|
||||
// Check the tag number is within our supported bounds.
|
||||
v > CBS_ASN1_TAG_NUMBER_MASK ||
|
||||
// Small tag numbers should have used low tag number form.
|
||||
// Small tag numbers should have used low tag number form, even in BER.
|
||||
v < 0x1f) {
|
||||
return 0;
|
||||
}
|
||||
@@ -277,13 +284,17 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
}
|
||||
|
||||
static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len, int ber_ok) {
|
||||
size_t *out_header_len, int *out_ber_found,
|
||||
int ber_ok) {
|
||||
CBS header = *cbs;
|
||||
CBS throwaway;
|
||||
|
||||
if (out == NULL) {
|
||||
out = &throwaway;
|
||||
}
|
||||
if (ber_ok) {
|
||||
*out_ber_found = 0;
|
||||
}
|
||||
|
||||
unsigned tag;
|
||||
if (!parse_asn1_tag(&header, &tag)) {
|
||||
@@ -321,27 +332,38 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
if (out_header_len != NULL) {
|
||||
*out_header_len = header_len;
|
||||
}
|
||||
*out_ber_found = 1;
|
||||
return CBS_get_bytes(cbs, out, header_len);
|
||||
}
|
||||
|
||||
// ITU-T X.690 clause 8.1.3.5.c specifies that the value 0xff shall not be
|
||||
// used as the first byte of the length. If this parser encounters that
|
||||
// value, num_bytes will be parsed as 127, which will fail the check below.
|
||||
// value, num_bytes will be parsed as 127, which will fail this check.
|
||||
if (num_bytes == 0 || num_bytes > 4) {
|
||||
return 0;
|
||||
}
|
||||
if (!cbs_get_u(&header, &len64, num_bytes)) {
|
||||
return 0;
|
||||
}
|
||||
// ITU-T X.690 section 10.1 (DER length forms) requires encoding the length
|
||||
// with the minimum number of octets.
|
||||
// ITU-T X.690 section 10.1 (DER length forms) requires encoding the
|
||||
// length with the minimum number of octets. BER could, technically, have
|
||||
// 125 superfluous zero bytes. We do not attempt to handle that and still
|
||||
// require that the length fit in a |uint32_t| for BER.
|
||||
if (len64 < 128) {
|
||||
// Length should have used short-form encoding.
|
||||
return 0;
|
||||
if (ber_ok) {
|
||||
*out_ber_found = 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if ((len64 >> ((num_bytes-1)*8)) == 0) {
|
||||
if ((len64 >> ((num_bytes - 1) * 8)) == 0) {
|
||||
// Length should have been at least one byte shorter.
|
||||
return 0;
|
||||
if (ber_ok) {
|
||||
*out_ber_found = 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
len = len64;
|
||||
if (len + header_len + num_bytes < len) {
|
||||
@@ -374,13 +396,15 @@ int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
|
||||
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len) {
|
||||
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
|
||||
0 /* DER only */);
|
||||
NULL, 0 /* DER only */);
|
||||
}
|
||||
|
||||
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
size_t *out_header_len) {
|
||||
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len,
|
||||
1 /* BER allowed */);
|
||||
size_t *out_header_len, int *out_ber_found) {
|
||||
int ber_found_temp;
|
||||
return cbs_get_any_asn1_element(
|
||||
cbs, out, out_tag, out_header_len,
|
||||
out_ber_found ? out_ber_found : &ber_found_temp, 1 /* BER allowed */);
|
||||
}
|
||||
|
||||
static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
|
||||
@@ -426,29 +450,14 @@ int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
|
||||
|
||||
int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
|
||||
CBS bytes;
|
||||
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
|
||||
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
|
||||
!CBS_is_unsigned_asn1_integer(&bytes)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*out = 0;
|
||||
const uint8_t *data = CBS_data(&bytes);
|
||||
size_t len = CBS_len(&bytes);
|
||||
|
||||
if (len == 0) {
|
||||
// An INTEGER is encoded with at least one octet.
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((data[0] & 0x80) != 0) {
|
||||
// Negative number.
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
|
||||
// Extra leading zeros.
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if ((*out >> 56) != 0) {
|
||||
// Too large to represent as a uint64_t.
|
||||
@@ -462,31 +471,21 @@ int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
|
||||
}
|
||||
|
||||
int CBS_get_asn1_int64(CBS *cbs, int64_t *out) {
|
||||
int is_negative;
|
||||
CBS bytes;
|
||||
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
|
||||
if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER) ||
|
||||
!CBS_is_valid_asn1_integer(&bytes, &is_negative)) {
|
||||
return 0;
|
||||
}
|
||||
const uint8_t *data = CBS_data(&bytes);
|
||||
const size_t len = CBS_len(&bytes);
|
||||
|
||||
if (len == 0 || len > sizeof(int64_t)) {
|
||||
// An INTEGER is encoded with at least one octet.
|
||||
if (len > sizeof(int64_t)) {
|
||||
return 0;
|
||||
}
|
||||
if (len > 1) {
|
||||
if (data[0] == 0 && (data[1] & 0x80) == 0) {
|
||||
return 0; // Extra leading zeros.
|
||||
}
|
||||
if (data[0] == 0xff && (data[1] & 0x80) != 0) {
|
||||
return 0; // Extra leading 0xff.
|
||||
}
|
||||
}
|
||||
|
||||
union {
|
||||
int64_t i;
|
||||
uint8_t bytes[sizeof(int64_t)];
|
||||
} u;
|
||||
const int is_negative = (data[0] & 0x80);
|
||||
memset(u.bytes, is_negative ? 0xff : 0, sizeof(u.bytes)); // Sign-extend.
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
u.bytes[i] = data[len - i - 1];
|
||||
@@ -635,6 +634,30 @@ int CBS_asn1_bitstring_has_bit(const CBS *cbs, unsigned bit) {
|
||||
(CBS_data(cbs)[byte_num] & (1 << bit_num)) != 0;
|
||||
}
|
||||
|
||||
int CBS_is_valid_asn1_integer(const CBS *cbs, int *out_is_negative) {
|
||||
CBS copy = *cbs;
|
||||
uint8_t first_byte, second_byte;
|
||||
if (!CBS_get_u8(©, &first_byte)) {
|
||||
return 0; // INTEGERs may not be empty.
|
||||
}
|
||||
if (out_is_negative != NULL) {
|
||||
*out_is_negative = (first_byte & 0x80) != 0;
|
||||
}
|
||||
if (!CBS_get_u8(©, &second_byte)) {
|
||||
return 1; // One byte INTEGERs are always minimal.
|
||||
}
|
||||
if ((first_byte == 0x00 && (second_byte & 0x80) == 0) ||
|
||||
(first_byte == 0xff && (second_byte & 0x80) != 0)) {
|
||||
return 0; // The value is minimal iff the first 9 bits are not all equal.
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
|
||||
int is_negative;
|
||||
return CBS_is_valid_asn1_integer(cbs, &is_negative) && !is_negative;
|
||||
}
|
||||
|
||||
static int add_decimal(CBB *out, uint64_t v) {
|
||||
char buf[DECIMAL_SIZE(uint64_t) + 1];
|
||||
BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
.private_extern _OPENSSL_armcap_P
|
||||
|
||||
.section __TEXT,__const
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
.hidden OPENSSL_armcap_P
|
||||
|
||||
.section .rodata
|
||||
|
||||
|
||||
@@ -0,0 +1,979 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.text
|
||||
.globl _ChaCha20_ctr32
|
||||
.hidden _ChaCha20_ctr32
|
||||
.def _ChaCha20_ctr32; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_ChaCha20_ctr32:
|
||||
.L_ChaCha20_ctr32_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
xorl %eax,%eax
|
||||
cmpl 28(%esp),%eax
|
||||
je .L000no_data
|
||||
call .Lpic_point
|
||||
.Lpic_point:
|
||||
popl %eax
|
||||
leal _OPENSSL_ia32cap_P,%ebp
|
||||
testl $16777216,(%ebp)
|
||||
jz .L001x86
|
||||
testl $512,4(%ebp)
|
||||
jz .L001x86
|
||||
jmp .Lssse3_shortcut
|
||||
.L001x86:
|
||||
movl 32(%esp),%esi
|
||||
movl 36(%esp),%edi
|
||||
subl $132,%esp
|
||||
movl (%esi),%eax
|
||||
movl 4(%esi),%ebx
|
||||
movl 8(%esi),%ecx
|
||||
movl 12(%esi),%edx
|
||||
movl %eax,80(%esp)
|
||||
movl %ebx,84(%esp)
|
||||
movl %ecx,88(%esp)
|
||||
movl %edx,92(%esp)
|
||||
movl 16(%esi),%eax
|
||||
movl 20(%esi),%ebx
|
||||
movl 24(%esi),%ecx
|
||||
movl 28(%esi),%edx
|
||||
movl %eax,96(%esp)
|
||||
movl %ebx,100(%esp)
|
||||
movl %ecx,104(%esp)
|
||||
movl %edx,108(%esp)
|
||||
movl (%edi),%eax
|
||||
movl 4(%edi),%ebx
|
||||
movl 8(%edi),%ecx
|
||||
movl 12(%edi),%edx
|
||||
subl $1,%eax
|
||||
movl %eax,112(%esp)
|
||||
movl %ebx,116(%esp)
|
||||
movl %ecx,120(%esp)
|
||||
movl %edx,124(%esp)
|
||||
jmp .L002entry
|
||||
.align 16
|
||||
.L003outer_loop:
|
||||
movl %ebx,156(%esp)
|
||||
movl %eax,152(%esp)
|
||||
movl %ecx,160(%esp)
|
||||
.L002entry:
|
||||
movl $1634760805,%eax
|
||||
movl $857760878,4(%esp)
|
||||
movl $2036477234,8(%esp)
|
||||
movl $1797285236,12(%esp)
|
||||
movl 84(%esp),%ebx
|
||||
movl 88(%esp),%ebp
|
||||
movl 104(%esp),%ecx
|
||||
movl 108(%esp),%esi
|
||||
movl 116(%esp),%edx
|
||||
movl 120(%esp),%edi
|
||||
movl %ebx,20(%esp)
|
||||
movl %ebp,24(%esp)
|
||||
movl %ecx,40(%esp)
|
||||
movl %esi,44(%esp)
|
||||
movl %edx,52(%esp)
|
||||
movl %edi,56(%esp)
|
||||
movl 92(%esp),%ebx
|
||||
movl 124(%esp),%edi
|
||||
movl 112(%esp),%edx
|
||||
movl 80(%esp),%ebp
|
||||
movl 96(%esp),%ecx
|
||||
movl 100(%esp),%esi
|
||||
addl $1,%edx
|
||||
movl %ebx,28(%esp)
|
||||
movl %edi,60(%esp)
|
||||
movl %edx,112(%esp)
|
||||
movl $10,%ebx
|
||||
jmp .L004loop
|
||||
.align 16
|
||||
.L004loop:
|
||||
addl %ebp,%eax
|
||||
movl %ebx,128(%esp)
|
||||
movl %ebp,%ebx
|
||||
xorl %eax,%edx
|
||||
roll $16,%edx
|
||||
addl %edx,%ecx
|
||||
xorl %ecx,%ebx
|
||||
movl 52(%esp),%edi
|
||||
roll $12,%ebx
|
||||
movl 20(%esp),%ebp
|
||||
addl %ebx,%eax
|
||||
xorl %eax,%edx
|
||||
movl %eax,(%esp)
|
||||
roll $8,%edx
|
||||
movl 4(%esp),%eax
|
||||
addl %edx,%ecx
|
||||
movl %edx,48(%esp)
|
||||
xorl %ecx,%ebx
|
||||
addl %ebp,%eax
|
||||
roll $7,%ebx
|
||||
xorl %eax,%edi
|
||||
movl %ecx,32(%esp)
|
||||
roll $16,%edi
|
||||
movl %ebx,16(%esp)
|
||||
addl %edi,%esi
|
||||
movl 40(%esp),%ecx
|
||||
xorl %esi,%ebp
|
||||
movl 56(%esp),%edx
|
||||
roll $12,%ebp
|
||||
movl 24(%esp),%ebx
|
||||
addl %ebp,%eax
|
||||
xorl %eax,%edi
|
||||
movl %eax,4(%esp)
|
||||
roll $8,%edi
|
||||
movl 8(%esp),%eax
|
||||
addl %edi,%esi
|
||||
movl %edi,52(%esp)
|
||||
xorl %esi,%ebp
|
||||
addl %ebx,%eax
|
||||
roll $7,%ebp
|
||||
xorl %eax,%edx
|
||||
movl %esi,36(%esp)
|
||||
roll $16,%edx
|
||||
movl %ebp,20(%esp)
|
||||
addl %edx,%ecx
|
||||
movl 44(%esp),%esi
|
||||
xorl %ecx,%ebx
|
||||
movl 60(%esp),%edi
|
||||
roll $12,%ebx
|
||||
movl 28(%esp),%ebp
|
||||
addl %ebx,%eax
|
||||
xorl %eax,%edx
|
||||
movl %eax,8(%esp)
|
||||
roll $8,%edx
|
||||
movl 12(%esp),%eax
|
||||
addl %edx,%ecx
|
||||
movl %edx,56(%esp)
|
||||
xorl %ecx,%ebx
|
||||
addl %ebp,%eax
|
||||
roll $7,%ebx
|
||||
xorl %eax,%edi
|
||||
roll $16,%edi
|
||||
movl %ebx,24(%esp)
|
||||
addl %edi,%esi
|
||||
xorl %esi,%ebp
|
||||
roll $12,%ebp
|
||||
movl 20(%esp),%ebx
|
||||
addl %ebp,%eax
|
||||
xorl %eax,%edi
|
||||
movl %eax,12(%esp)
|
||||
roll $8,%edi
|
||||
movl (%esp),%eax
|
||||
addl %edi,%esi
|
||||
movl %edi,%edx
|
||||
xorl %esi,%ebp
|
||||
addl %ebx,%eax
|
||||
roll $7,%ebp
|
||||
xorl %eax,%edx
|
||||
roll $16,%edx
|
||||
movl %ebp,28(%esp)
|
||||
addl %edx,%ecx
|
||||
xorl %ecx,%ebx
|
||||
movl 48(%esp),%edi
|
||||
roll $12,%ebx
|
||||
movl 24(%esp),%ebp
|
||||
addl %ebx,%eax
|
||||
xorl %eax,%edx
|
||||
movl %eax,(%esp)
|
||||
roll $8,%edx
|
||||
movl 4(%esp),%eax
|
||||
addl %edx,%ecx
|
||||
movl %edx,60(%esp)
|
||||
xorl %ecx,%ebx
|
||||
addl %ebp,%eax
|
||||
roll $7,%ebx
|
||||
xorl %eax,%edi
|
||||
movl %ecx,40(%esp)
|
||||
roll $16,%edi
|
||||
movl %ebx,20(%esp)
|
||||
addl %edi,%esi
|
||||
movl 32(%esp),%ecx
|
||||
xorl %esi,%ebp
|
||||
movl 52(%esp),%edx
|
||||
roll $12,%ebp
|
||||
movl 28(%esp),%ebx
|
||||
addl %ebp,%eax
|
||||
xorl %eax,%edi
|
||||
movl %eax,4(%esp)
|
||||
roll $8,%edi
|
||||
movl 8(%esp),%eax
|
||||
addl %edi,%esi
|
||||
movl %edi,48(%esp)
|
||||
xorl %esi,%ebp
|
||||
addl %ebx,%eax
|
||||
roll $7,%ebp
|
||||
xorl %eax,%edx
|
||||
movl %esi,44(%esp)
|
||||
roll $16,%edx
|
||||
movl %ebp,24(%esp)
|
||||
addl %edx,%ecx
|
||||
movl 36(%esp),%esi
|
||||
xorl %ecx,%ebx
|
||||
movl 56(%esp),%edi
|
||||
roll $12,%ebx
|
||||
movl 16(%esp),%ebp
|
||||
addl %ebx,%eax
|
||||
xorl %eax,%edx
|
||||
movl %eax,8(%esp)
|
||||
roll $8,%edx
|
||||
movl 12(%esp),%eax
|
||||
addl %edx,%ecx
|
||||
movl %edx,52(%esp)
|
||||
xorl %ecx,%ebx
|
||||
addl %ebp,%eax
|
||||
roll $7,%ebx
|
||||
xorl %eax,%edi
|
||||
roll $16,%edi
|
||||
movl %ebx,28(%esp)
|
||||
addl %edi,%esi
|
||||
xorl %esi,%ebp
|
||||
movl 48(%esp),%edx
|
||||
roll $12,%ebp
|
||||
movl 128(%esp),%ebx
|
||||
addl %ebp,%eax
|
||||
xorl %eax,%edi
|
||||
movl %eax,12(%esp)
|
||||
roll $8,%edi
|
||||
movl (%esp),%eax
|
||||
addl %edi,%esi
|
||||
movl %edi,56(%esp)
|
||||
xorl %esi,%ebp
|
||||
roll $7,%ebp
|
||||
decl %ebx
|
||||
jnz .L004loop
|
||||
movl 160(%esp),%ebx
|
||||
addl $1634760805,%eax
|
||||
addl 80(%esp),%ebp
|
||||
addl 96(%esp),%ecx
|
||||
addl 100(%esp),%esi
|
||||
cmpl $64,%ebx
|
||||
jb .L005tail
|
||||
movl 156(%esp),%ebx
|
||||
addl 112(%esp),%edx
|
||||
addl 120(%esp),%edi
|
||||
xorl (%ebx),%eax
|
||||
xorl 16(%ebx),%ebp
|
||||
movl %eax,(%esp)
|
||||
movl 152(%esp),%eax
|
||||
xorl 32(%ebx),%ecx
|
||||
xorl 36(%ebx),%esi
|
||||
xorl 48(%ebx),%edx
|
||||
xorl 56(%ebx),%edi
|
||||
movl %ebp,16(%eax)
|
||||
movl %ecx,32(%eax)
|
||||
movl %esi,36(%eax)
|
||||
movl %edx,48(%eax)
|
||||
movl %edi,56(%eax)
|
||||
movl 4(%esp),%ebp
|
||||
movl 8(%esp),%ecx
|
||||
movl 12(%esp),%esi
|
||||
movl 20(%esp),%edx
|
||||
movl 24(%esp),%edi
|
||||
addl $857760878,%ebp
|
||||
addl $2036477234,%ecx
|
||||
addl $1797285236,%esi
|
||||
addl 84(%esp),%edx
|
||||
addl 88(%esp),%edi
|
||||
xorl 4(%ebx),%ebp
|
||||
xorl 8(%ebx),%ecx
|
||||
xorl 12(%ebx),%esi
|
||||
xorl 20(%ebx),%edx
|
||||
xorl 24(%ebx),%edi
|
||||
movl %ebp,4(%eax)
|
||||
movl %ecx,8(%eax)
|
||||
movl %esi,12(%eax)
|
||||
movl %edx,20(%eax)
|
||||
movl %edi,24(%eax)
|
||||
movl 28(%esp),%ebp
|
||||
movl 40(%esp),%ecx
|
||||
movl 44(%esp),%esi
|
||||
movl 52(%esp),%edx
|
||||
movl 60(%esp),%edi
|
||||
addl 92(%esp),%ebp
|
||||
addl 104(%esp),%ecx
|
||||
addl 108(%esp),%esi
|
||||
addl 116(%esp),%edx
|
||||
addl 124(%esp),%edi
|
||||
xorl 28(%ebx),%ebp
|
||||
xorl 40(%ebx),%ecx
|
||||
xorl 44(%ebx),%esi
|
||||
xorl 52(%ebx),%edx
|
||||
xorl 60(%ebx),%edi
|
||||
leal 64(%ebx),%ebx
|
||||
movl %ebp,28(%eax)
|
||||
movl (%esp),%ebp
|
||||
movl %ecx,40(%eax)
|
||||
movl 160(%esp),%ecx
|
||||
movl %esi,44(%eax)
|
||||
movl %edx,52(%eax)
|
||||
movl %edi,60(%eax)
|
||||
movl %ebp,(%eax)
|
||||
leal 64(%eax),%eax
|
||||
subl $64,%ecx
|
||||
jnz .L003outer_loop
|
||||
jmp .L006done
|
||||
.L005tail:
|
||||
addl 112(%esp),%edx
|
||||
addl 120(%esp),%edi
|
||||
movl %eax,(%esp)
|
||||
movl %ebp,16(%esp)
|
||||
movl %ecx,32(%esp)
|
||||
movl %esi,36(%esp)
|
||||
movl %edx,48(%esp)
|
||||
movl %edi,56(%esp)
|
||||
movl 4(%esp),%ebp
|
||||
movl 8(%esp),%ecx
|
||||
movl 12(%esp),%esi
|
||||
movl 20(%esp),%edx
|
||||
movl 24(%esp),%edi
|
||||
addl $857760878,%ebp
|
||||
addl $2036477234,%ecx
|
||||
addl $1797285236,%esi
|
||||
addl 84(%esp),%edx
|
||||
addl 88(%esp),%edi
|
||||
movl %ebp,4(%esp)
|
||||
movl %ecx,8(%esp)
|
||||
movl %esi,12(%esp)
|
||||
movl %edx,20(%esp)
|
||||
movl %edi,24(%esp)
|
||||
movl 28(%esp),%ebp
|
||||
movl 40(%esp),%ecx
|
||||
movl 44(%esp),%esi
|
||||
movl 52(%esp),%edx
|
||||
movl 60(%esp),%edi
|
||||
addl 92(%esp),%ebp
|
||||
addl 104(%esp),%ecx
|
||||
addl 108(%esp),%esi
|
||||
addl 116(%esp),%edx
|
||||
addl 124(%esp),%edi
|
||||
movl %ebp,28(%esp)
|
||||
movl 156(%esp),%ebp
|
||||
movl %ecx,40(%esp)
|
||||
movl 152(%esp),%ecx
|
||||
movl %esi,44(%esp)
|
||||
xorl %esi,%esi
|
||||
movl %edx,52(%esp)
|
||||
movl %edi,60(%esp)
|
||||
xorl %eax,%eax
|
||||
xorl %edx,%edx
|
||||
.L007tail_loop:
|
||||
movb (%esi,%ebp,1),%al
|
||||
movb (%esp,%esi,1),%dl
|
||||
leal 1(%esi),%esi
|
||||
xorb %dl,%al
|
||||
movb %al,-1(%ecx,%esi,1)
|
||||
decl %ebx
|
||||
jnz .L007tail_loop
|
||||
.L006done:
|
||||
addl $132,%esp
|
||||
.L000no_data:
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _ChaCha20_ssse3
|
||||
.hidden _ChaCha20_ssse3
|
||||
.def _ChaCha20_ssse3; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_ChaCha20_ssse3:
|
||||
.L_ChaCha20_ssse3_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
.Lssse3_shortcut:
|
||||
movl 20(%esp),%edi
|
||||
movl 24(%esp),%esi
|
||||
movl 28(%esp),%ecx
|
||||
movl 32(%esp),%edx
|
||||
movl 36(%esp),%ebx
|
||||
movl %esp,%ebp
|
||||
subl $524,%esp
|
||||
andl $-64,%esp
|
||||
movl %ebp,512(%esp)
|
||||
leal .Lssse3_data-.Lpic_point(%eax),%eax
|
||||
movdqu (%ebx),%xmm3
|
||||
cmpl $256,%ecx
|
||||
jb .L0081x
|
||||
movl %edx,516(%esp)
|
||||
movl %ebx,520(%esp)
|
||||
subl $256,%ecx
|
||||
leal 384(%esp),%ebp
|
||||
movdqu (%edx),%xmm7
|
||||
pshufd $0,%xmm3,%xmm0
|
||||
pshufd $85,%xmm3,%xmm1
|
||||
pshufd $170,%xmm3,%xmm2
|
||||
pshufd $255,%xmm3,%xmm3
|
||||
paddd 48(%eax),%xmm0
|
||||
pshufd $0,%xmm7,%xmm4
|
||||
pshufd $85,%xmm7,%xmm5
|
||||
psubd 64(%eax),%xmm0
|
||||
pshufd $170,%xmm7,%xmm6
|
||||
pshufd $255,%xmm7,%xmm7
|
||||
movdqa %xmm0,64(%ebp)
|
||||
movdqa %xmm1,80(%ebp)
|
||||
movdqa %xmm2,96(%ebp)
|
||||
movdqa %xmm3,112(%ebp)
|
||||
movdqu 16(%edx),%xmm3
|
||||
movdqa %xmm4,-64(%ebp)
|
||||
movdqa %xmm5,-48(%ebp)
|
||||
movdqa %xmm6,-32(%ebp)
|
||||
movdqa %xmm7,-16(%ebp)
|
||||
movdqa 32(%eax),%xmm7
|
||||
leal 128(%esp),%ebx
|
||||
pshufd $0,%xmm3,%xmm0
|
||||
pshufd $85,%xmm3,%xmm1
|
||||
pshufd $170,%xmm3,%xmm2
|
||||
pshufd $255,%xmm3,%xmm3
|
||||
pshufd $0,%xmm7,%xmm4
|
||||
pshufd $85,%xmm7,%xmm5
|
||||
pshufd $170,%xmm7,%xmm6
|
||||
pshufd $255,%xmm7,%xmm7
|
||||
movdqa %xmm0,(%ebp)
|
||||
movdqa %xmm1,16(%ebp)
|
||||
movdqa %xmm2,32(%ebp)
|
||||
movdqa %xmm3,48(%ebp)
|
||||
movdqa %xmm4,-128(%ebp)
|
||||
movdqa %xmm5,-112(%ebp)
|
||||
movdqa %xmm6,-96(%ebp)
|
||||
movdqa %xmm7,-80(%ebp)
|
||||
leal 128(%esi),%esi
|
||||
leal 128(%edi),%edi
|
||||
jmp .L009outer_loop
|
||||
.align 16
|
||||
.L009outer_loop:
|
||||
movdqa -112(%ebp),%xmm1
|
||||
movdqa -96(%ebp),%xmm2
|
||||
movdqa -80(%ebp),%xmm3
|
||||
movdqa -48(%ebp),%xmm5
|
||||
movdqa -32(%ebp),%xmm6
|
||||
movdqa -16(%ebp),%xmm7
|
||||
movdqa %xmm1,-112(%ebx)
|
||||
movdqa %xmm2,-96(%ebx)
|
||||
movdqa %xmm3,-80(%ebx)
|
||||
movdqa %xmm5,-48(%ebx)
|
||||
movdqa %xmm6,-32(%ebx)
|
||||
movdqa %xmm7,-16(%ebx)
|
||||
movdqa 32(%ebp),%xmm2
|
||||
movdqa 48(%ebp),%xmm3
|
||||
movdqa 64(%ebp),%xmm4
|
||||
movdqa 80(%ebp),%xmm5
|
||||
movdqa 96(%ebp),%xmm6
|
||||
movdqa 112(%ebp),%xmm7
|
||||
paddd 64(%eax),%xmm4
|
||||
movdqa %xmm2,32(%ebx)
|
||||
movdqa %xmm3,48(%ebx)
|
||||
movdqa %xmm4,64(%ebx)
|
||||
movdqa %xmm5,80(%ebx)
|
||||
movdqa %xmm6,96(%ebx)
|
||||
movdqa %xmm7,112(%ebx)
|
||||
movdqa %xmm4,64(%ebp)
|
||||
movdqa -128(%ebp),%xmm0
|
||||
movdqa %xmm4,%xmm6
|
||||
movdqa -64(%ebp),%xmm3
|
||||
movdqa (%ebp),%xmm4
|
||||
movdqa 16(%ebp),%xmm5
|
||||
movl $10,%edx
|
||||
nop
|
||||
.align 16
|
||||
.L010loop:
|
||||
paddd %xmm3,%xmm0
|
||||
movdqa %xmm3,%xmm2
|
||||
pxor %xmm0,%xmm6
|
||||
pshufb (%eax),%xmm6
|
||||
paddd %xmm6,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
movdqa -48(%ebx),%xmm3
|
||||
movdqa %xmm2,%xmm1
|
||||
pslld $12,%xmm2
|
||||
psrld $20,%xmm1
|
||||
por %xmm1,%xmm2
|
||||
movdqa -112(%ebx),%xmm1
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa 80(%ebx),%xmm7
|
||||
pxor %xmm0,%xmm6
|
||||
movdqa %xmm0,-128(%ebx)
|
||||
pshufb 16(%eax),%xmm6
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa %xmm6,64(%ebx)
|
||||
pxor %xmm4,%xmm2
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa %xmm2,%xmm0
|
||||
pslld $7,%xmm2
|
||||
psrld $25,%xmm0
|
||||
pxor %xmm1,%xmm7
|
||||
por %xmm0,%xmm2
|
||||
movdqa %xmm4,(%ebx)
|
||||
pshufb (%eax),%xmm7
|
||||
movdqa %xmm2,-64(%ebx)
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa 32(%ebx),%xmm4
|
||||
pxor %xmm5,%xmm3
|
||||
movdqa -32(%ebx),%xmm2
|
||||
movdqa %xmm3,%xmm0
|
||||
pslld $12,%xmm3
|
||||
psrld $20,%xmm0
|
||||
por %xmm0,%xmm3
|
||||
movdqa -96(%ebx),%xmm0
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa 96(%ebx),%xmm6
|
||||
pxor %xmm1,%xmm7
|
||||
movdqa %xmm1,-112(%ebx)
|
||||
pshufb 16(%eax),%xmm7
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa %xmm7,80(%ebx)
|
||||
pxor %xmm5,%xmm3
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa %xmm3,%xmm1
|
||||
pslld $7,%xmm3
|
||||
psrld $25,%xmm1
|
||||
pxor %xmm0,%xmm6
|
||||
por %xmm1,%xmm3
|
||||
movdqa %xmm5,16(%ebx)
|
||||
pshufb (%eax),%xmm6
|
||||
movdqa %xmm3,-48(%ebx)
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa 48(%ebx),%xmm5
|
||||
pxor %xmm4,%xmm2
|
||||
movdqa -16(%ebx),%xmm3
|
||||
movdqa %xmm2,%xmm1
|
||||
pslld $12,%xmm2
|
||||
psrld $20,%xmm1
|
||||
por %xmm1,%xmm2
|
||||
movdqa -80(%ebx),%xmm1
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa 112(%ebx),%xmm7
|
||||
pxor %xmm0,%xmm6
|
||||
movdqa %xmm0,-96(%ebx)
|
||||
pshufb 16(%eax),%xmm6
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa %xmm6,96(%ebx)
|
||||
pxor %xmm4,%xmm2
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa %xmm2,%xmm0
|
||||
pslld $7,%xmm2
|
||||
psrld $25,%xmm0
|
||||
pxor %xmm1,%xmm7
|
||||
por %xmm0,%xmm2
|
||||
pshufb (%eax),%xmm7
|
||||
movdqa %xmm2,-32(%ebx)
|
||||
paddd %xmm7,%xmm5
|
||||
pxor %xmm5,%xmm3
|
||||
movdqa -48(%ebx),%xmm2
|
||||
movdqa %xmm3,%xmm0
|
||||
pslld $12,%xmm3
|
||||
psrld $20,%xmm0
|
||||
por %xmm0,%xmm3
|
||||
movdqa -128(%ebx),%xmm0
|
||||
paddd %xmm3,%xmm1
|
||||
pxor %xmm1,%xmm7
|
||||
movdqa %xmm1,-80(%ebx)
|
||||
pshufb 16(%eax),%xmm7
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa %xmm7,%xmm6
|
||||
pxor %xmm5,%xmm3
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa %xmm3,%xmm1
|
||||
pslld $7,%xmm3
|
||||
psrld $25,%xmm1
|
||||
pxor %xmm0,%xmm6
|
||||
por %xmm1,%xmm3
|
||||
pshufb (%eax),%xmm6
|
||||
movdqa %xmm3,-16(%ebx)
|
||||
paddd %xmm6,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
movdqa -32(%ebx),%xmm3
|
||||
movdqa %xmm2,%xmm1
|
||||
pslld $12,%xmm2
|
||||
psrld $20,%xmm1
|
||||
por %xmm1,%xmm2
|
||||
movdqa -112(%ebx),%xmm1
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa 64(%ebx),%xmm7
|
||||
pxor %xmm0,%xmm6
|
||||
movdqa %xmm0,-128(%ebx)
|
||||
pshufb 16(%eax),%xmm6
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa %xmm6,112(%ebx)
|
||||
pxor %xmm4,%xmm2
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa %xmm2,%xmm0
|
||||
pslld $7,%xmm2
|
||||
psrld $25,%xmm0
|
||||
pxor %xmm1,%xmm7
|
||||
por %xmm0,%xmm2
|
||||
movdqa %xmm4,32(%ebx)
|
||||
pshufb (%eax),%xmm7
|
||||
movdqa %xmm2,-48(%ebx)
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa (%ebx),%xmm4
|
||||
pxor %xmm5,%xmm3
|
||||
movdqa -16(%ebx),%xmm2
|
||||
movdqa %xmm3,%xmm0
|
||||
pslld $12,%xmm3
|
||||
psrld $20,%xmm0
|
||||
por %xmm0,%xmm3
|
||||
movdqa -96(%ebx),%xmm0
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa 80(%ebx),%xmm6
|
||||
pxor %xmm1,%xmm7
|
||||
movdqa %xmm1,-112(%ebx)
|
||||
pshufb 16(%eax),%xmm7
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa %xmm7,64(%ebx)
|
||||
pxor %xmm5,%xmm3
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa %xmm3,%xmm1
|
||||
pslld $7,%xmm3
|
||||
psrld $25,%xmm1
|
||||
pxor %xmm0,%xmm6
|
||||
por %xmm1,%xmm3
|
||||
movdqa %xmm5,48(%ebx)
|
||||
pshufb (%eax),%xmm6
|
||||
movdqa %xmm3,-32(%ebx)
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa 16(%ebx),%xmm5
|
||||
pxor %xmm4,%xmm2
|
||||
movdqa -64(%ebx),%xmm3
|
||||
movdqa %xmm2,%xmm1
|
||||
pslld $12,%xmm2
|
||||
psrld $20,%xmm1
|
||||
por %xmm1,%xmm2
|
||||
movdqa -80(%ebx),%xmm1
|
||||
paddd %xmm2,%xmm0
|
||||
movdqa 96(%ebx),%xmm7
|
||||
pxor %xmm0,%xmm6
|
||||
movdqa %xmm0,-96(%ebx)
|
||||
pshufb 16(%eax),%xmm6
|
||||
paddd %xmm6,%xmm4
|
||||
movdqa %xmm6,80(%ebx)
|
||||
pxor %xmm4,%xmm2
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa %xmm2,%xmm0
|
||||
pslld $7,%xmm2
|
||||
psrld $25,%xmm0
|
||||
pxor %xmm1,%xmm7
|
||||
por %xmm0,%xmm2
|
||||
pshufb (%eax),%xmm7
|
||||
movdqa %xmm2,-16(%ebx)
|
||||
paddd %xmm7,%xmm5
|
||||
pxor %xmm5,%xmm3
|
||||
movdqa %xmm3,%xmm0
|
||||
pslld $12,%xmm3
|
||||
psrld $20,%xmm0
|
||||
por %xmm0,%xmm3
|
||||
movdqa -128(%ebx),%xmm0
|
||||
paddd %xmm3,%xmm1
|
||||
movdqa 64(%ebx),%xmm6
|
||||
pxor %xmm1,%xmm7
|
||||
movdqa %xmm1,-80(%ebx)
|
||||
pshufb 16(%eax),%xmm7
|
||||
paddd %xmm7,%xmm5
|
||||
movdqa %xmm7,96(%ebx)
|
||||
pxor %xmm5,%xmm3
|
||||
movdqa %xmm3,%xmm1
|
||||
pslld $7,%xmm3
|
||||
psrld $25,%xmm1
|
||||
por %xmm1,%xmm3
|
||||
decl %edx
|
||||
jnz .L010loop
|
||||
movdqa %xmm3,-64(%ebx)
|
||||
movdqa %xmm4,(%ebx)
|
||||
movdqa %xmm5,16(%ebx)
|
||||
movdqa %xmm6,64(%ebx)
|
||||
movdqa %xmm7,96(%ebx)
|
||||
movdqa -112(%ebx),%xmm1
|
||||
movdqa -96(%ebx),%xmm2
|
||||
movdqa -80(%ebx),%xmm3
|
||||
paddd -128(%ebp),%xmm0
|
||||
paddd -112(%ebp),%xmm1
|
||||
paddd -96(%ebp),%xmm2
|
||||
paddd -80(%ebp),%xmm3
|
||||
movdqa %xmm0,%xmm6
|
||||
punpckldq %xmm1,%xmm0
|
||||
movdqa %xmm2,%xmm7
|
||||
punpckldq %xmm3,%xmm2
|
||||
punpckhdq %xmm1,%xmm6
|
||||
punpckhdq %xmm3,%xmm7
|
||||
movdqa %xmm0,%xmm1
|
||||
punpcklqdq %xmm2,%xmm0
|
||||
movdqa %xmm6,%xmm3
|
||||
punpcklqdq %xmm7,%xmm6
|
||||
punpckhqdq %xmm2,%xmm1
|
||||
punpckhqdq %xmm7,%xmm3
|
||||
movdqu -128(%esi),%xmm4
|
||||
movdqu -64(%esi),%xmm5
|
||||
movdqu (%esi),%xmm2
|
||||
movdqu 64(%esi),%xmm7
|
||||
leal 16(%esi),%esi
|
||||
pxor %xmm0,%xmm4
|
||||
movdqa -64(%ebx),%xmm0
|
||||
pxor %xmm1,%xmm5
|
||||
movdqa -48(%ebx),%xmm1
|
||||
pxor %xmm2,%xmm6
|
||||
movdqa -32(%ebx),%xmm2
|
||||
pxor %xmm3,%xmm7
|
||||
movdqa -16(%ebx),%xmm3
|
||||
movdqu %xmm4,-128(%edi)
|
||||
movdqu %xmm5,-64(%edi)
|
||||
movdqu %xmm6,(%edi)
|
||||
movdqu %xmm7,64(%edi)
|
||||
leal 16(%edi),%edi
|
||||
paddd -64(%ebp),%xmm0
|
||||
paddd -48(%ebp),%xmm1
|
||||
paddd -32(%ebp),%xmm2
|
||||
paddd -16(%ebp),%xmm3
|
||||
movdqa %xmm0,%xmm6
|
||||
punpckldq %xmm1,%xmm0
|
||||
movdqa %xmm2,%xmm7
|
||||
punpckldq %xmm3,%xmm2
|
||||
punpckhdq %xmm1,%xmm6
|
||||
punpckhdq %xmm3,%xmm7
|
||||
movdqa %xmm0,%xmm1
|
||||
punpcklqdq %xmm2,%xmm0
|
||||
movdqa %xmm6,%xmm3
|
||||
punpcklqdq %xmm7,%xmm6
|
||||
punpckhqdq %xmm2,%xmm1
|
||||
punpckhqdq %xmm7,%xmm3
|
||||
movdqu -128(%esi),%xmm4
|
||||
movdqu -64(%esi),%xmm5
|
||||
movdqu (%esi),%xmm2
|
||||
movdqu 64(%esi),%xmm7
|
||||
leal 16(%esi),%esi
|
||||
pxor %xmm0,%xmm4
|
||||
movdqa (%ebx),%xmm0
|
||||
pxor %xmm1,%xmm5
|
||||
movdqa 16(%ebx),%xmm1
|
||||
pxor %xmm2,%xmm6
|
||||
movdqa 32(%ebx),%xmm2
|
||||
pxor %xmm3,%xmm7
|
||||
movdqa 48(%ebx),%xmm3
|
||||
movdqu %xmm4,-128(%edi)
|
||||
movdqu %xmm5,-64(%edi)
|
||||
movdqu %xmm6,(%edi)
|
||||
movdqu %xmm7,64(%edi)
|
||||
leal 16(%edi),%edi
|
||||
paddd (%ebp),%xmm0
|
||||
paddd 16(%ebp),%xmm1
|
||||
paddd 32(%ebp),%xmm2
|
||||
paddd 48(%ebp),%xmm3
|
||||
movdqa %xmm0,%xmm6
|
||||
punpckldq %xmm1,%xmm0
|
||||
movdqa %xmm2,%xmm7
|
||||
punpckldq %xmm3,%xmm2
|
||||
punpckhdq %xmm1,%xmm6
|
||||
punpckhdq %xmm3,%xmm7
|
||||
movdqa %xmm0,%xmm1
|
||||
punpcklqdq %xmm2,%xmm0
|
||||
movdqa %xmm6,%xmm3
|
||||
punpcklqdq %xmm7,%xmm6
|
||||
punpckhqdq %xmm2,%xmm1
|
||||
punpckhqdq %xmm7,%xmm3
|
||||
movdqu -128(%esi),%xmm4
|
||||
movdqu -64(%esi),%xmm5
|
||||
movdqu (%esi),%xmm2
|
||||
movdqu 64(%esi),%xmm7
|
||||
leal 16(%esi),%esi
|
||||
pxor %xmm0,%xmm4
|
||||
movdqa 64(%ebx),%xmm0
|
||||
pxor %xmm1,%xmm5
|
||||
movdqa 80(%ebx),%xmm1
|
||||
pxor %xmm2,%xmm6
|
||||
movdqa 96(%ebx),%xmm2
|
||||
pxor %xmm3,%xmm7
|
||||
movdqa 112(%ebx),%xmm3
|
||||
movdqu %xmm4,-128(%edi)
|
||||
movdqu %xmm5,-64(%edi)
|
||||
movdqu %xmm6,(%edi)
|
||||
movdqu %xmm7,64(%edi)
|
||||
leal 16(%edi),%edi
|
||||
paddd 64(%ebp),%xmm0
|
||||
paddd 80(%ebp),%xmm1
|
||||
paddd 96(%ebp),%xmm2
|
||||
paddd 112(%ebp),%xmm3
|
||||
movdqa %xmm0,%xmm6
|
||||
punpckldq %xmm1,%xmm0
|
||||
movdqa %xmm2,%xmm7
|
||||
punpckldq %xmm3,%xmm2
|
||||
punpckhdq %xmm1,%xmm6
|
||||
punpckhdq %xmm3,%xmm7
|
||||
movdqa %xmm0,%xmm1
|
||||
punpcklqdq %xmm2,%xmm0
|
||||
movdqa %xmm6,%xmm3
|
||||
punpcklqdq %xmm7,%xmm6
|
||||
punpckhqdq %xmm2,%xmm1
|
||||
punpckhqdq %xmm7,%xmm3
|
||||
movdqu -128(%esi),%xmm4
|
||||
movdqu -64(%esi),%xmm5
|
||||
movdqu (%esi),%xmm2
|
||||
movdqu 64(%esi),%xmm7
|
||||
leal 208(%esi),%esi
|
||||
pxor %xmm0,%xmm4
|
||||
pxor %xmm1,%xmm5
|
||||
pxor %xmm2,%xmm6
|
||||
pxor %xmm3,%xmm7
|
||||
movdqu %xmm4,-128(%edi)
|
||||
movdqu %xmm5,-64(%edi)
|
||||
movdqu %xmm6,(%edi)
|
||||
movdqu %xmm7,64(%edi)
|
||||
leal 208(%edi),%edi
|
||||
subl $256,%ecx
|
||||
jnc .L009outer_loop
|
||||
addl $256,%ecx
|
||||
jz .L011done
|
||||
movl 520(%esp),%ebx
|
||||
leal -128(%esi),%esi
|
||||
movl 516(%esp),%edx
|
||||
leal -128(%edi),%edi
|
||||
movd 64(%ebp),%xmm2
|
||||
movdqu (%ebx),%xmm3
|
||||
paddd 96(%eax),%xmm2
|
||||
pand 112(%eax),%xmm3
|
||||
por %xmm2,%xmm3
|
||||
.L0081x:
|
||||
movdqa 32(%eax),%xmm0
|
||||
movdqu (%edx),%xmm1
|
||||
movdqu 16(%edx),%xmm2
|
||||
movdqa (%eax),%xmm6
|
||||
movdqa 16(%eax),%xmm7
|
||||
movl %ebp,48(%esp)
|
||||
movdqa %xmm0,(%esp)
|
||||
movdqa %xmm1,16(%esp)
|
||||
movdqa %xmm2,32(%esp)
|
||||
movdqa %xmm3,48(%esp)
|
||||
movl $10,%edx
|
||||
jmp .L012loop1x
|
||||
.align 16
|
||||
.L013outer1x:
|
||||
movdqa 80(%eax),%xmm3
|
||||
movdqa (%esp),%xmm0
|
||||
movdqa 16(%esp),%xmm1
|
||||
movdqa 32(%esp),%xmm2
|
||||
paddd 48(%esp),%xmm3
|
||||
movl $10,%edx
|
||||
movdqa %xmm3,48(%esp)
|
||||
jmp .L012loop1x
|
||||
.align 16
|
||||
.L012loop1x:
|
||||
paddd %xmm1,%xmm0
|
||||
pxor %xmm0,%xmm3
|
||||
.byte 102,15,56,0,222
|
||||
paddd %xmm3,%xmm2
|
||||
pxor %xmm2,%xmm1
|
||||
movdqa %xmm1,%xmm4
|
||||
psrld $20,%xmm1
|
||||
pslld $12,%xmm4
|
||||
por %xmm4,%xmm1
|
||||
paddd %xmm1,%xmm0
|
||||
pxor %xmm0,%xmm3
|
||||
.byte 102,15,56,0,223
|
||||
paddd %xmm3,%xmm2
|
||||
pxor %xmm2,%xmm1
|
||||
movdqa %xmm1,%xmm4
|
||||
psrld $25,%xmm1
|
||||
pslld $7,%xmm4
|
||||
por %xmm4,%xmm1
|
||||
pshufd $78,%xmm2,%xmm2
|
||||
pshufd $57,%xmm1,%xmm1
|
||||
pshufd $147,%xmm3,%xmm3
|
||||
nop
|
||||
paddd %xmm1,%xmm0
|
||||
pxor %xmm0,%xmm3
|
||||
.byte 102,15,56,0,222
|
||||
paddd %xmm3,%xmm2
|
||||
pxor %xmm2,%xmm1
|
||||
movdqa %xmm1,%xmm4
|
||||
psrld $20,%xmm1
|
||||
pslld $12,%xmm4
|
||||
por %xmm4,%xmm1
|
||||
paddd %xmm1,%xmm0
|
||||
pxor %xmm0,%xmm3
|
||||
.byte 102,15,56,0,223
|
||||
paddd %xmm3,%xmm2
|
||||
pxor %xmm2,%xmm1
|
||||
movdqa %xmm1,%xmm4
|
||||
psrld $25,%xmm1
|
||||
pslld $7,%xmm4
|
||||
por %xmm4,%xmm1
|
||||
pshufd $78,%xmm2,%xmm2
|
||||
pshufd $147,%xmm1,%xmm1
|
||||
pshufd $57,%xmm3,%xmm3
|
||||
decl %edx
|
||||
jnz .L012loop1x
|
||||
paddd (%esp),%xmm0
|
||||
paddd 16(%esp),%xmm1
|
||||
paddd 32(%esp),%xmm2
|
||||
paddd 48(%esp),%xmm3
|
||||
cmpl $64,%ecx
|
||||
jb .L014tail
|
||||
movdqu (%esi),%xmm4
|
||||
movdqu 16(%esi),%xmm5
|
||||
pxor %xmm4,%xmm0
|
||||
movdqu 32(%esi),%xmm4
|
||||
pxor %xmm5,%xmm1
|
||||
movdqu 48(%esi),%xmm5
|
||||
pxor %xmm4,%xmm2
|
||||
pxor %xmm5,%xmm3
|
||||
leal 64(%esi),%esi
|
||||
movdqu %xmm0,(%edi)
|
||||
movdqu %xmm1,16(%edi)
|
||||
movdqu %xmm2,32(%edi)
|
||||
movdqu %xmm3,48(%edi)
|
||||
leal 64(%edi),%edi
|
||||
subl $64,%ecx
|
||||
jnz .L013outer1x
|
||||
jmp .L011done
|
||||
.L014tail:
|
||||
movdqa %xmm0,(%esp)
|
||||
movdqa %xmm1,16(%esp)
|
||||
movdqa %xmm2,32(%esp)
|
||||
movdqa %xmm3,48(%esp)
|
||||
xorl %eax,%eax
|
||||
xorl %edx,%edx
|
||||
xorl %ebp,%ebp
|
||||
.L015tail_loop:
|
||||
movb (%esp,%ebp,1),%al
|
||||
movb (%esi,%ebp,1),%dl
|
||||
leal 1(%ebp),%ebp
|
||||
xorb %dl,%al
|
||||
movb %al,-1(%edi,%ebp,1)
|
||||
decl %ecx
|
||||
jnz .L015tail_loop
|
||||
.L011done:
|
||||
movl 512(%esp),%esp
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.align 64
|
||||
.Lssse3_data:
|
||||
.byte 2,3,0,1,6,7,4,5,10,11,8,9,14,15,12,13
|
||||
.byte 3,0,1,2,7,4,5,6,11,8,9,10,15,12,13,14
|
||||
.long 1634760805,857760878,2036477234,1797285236
|
||||
.long 0,1,2,3
|
||||
.long 4,4,4,4
|
||||
.long 1,0,0,0
|
||||
.long 4,0,0,0
|
||||
.long 0,-1,-1,-1
|
||||
.align 64
|
||||
.byte 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54
|
||||
.byte 44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32
|
||||
.byte 60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111
|
||||
.byte 114,103,62,0
|
||||
#endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
+1915
-1980
File diff suppressed because it is too large.
Load diff
+1870
-1977
File diff suppressed because it is too large.
Load diff
@@ -89,6 +89,10 @@ const EVP_CIPHER *EVP_get_cipherbynid(int nid) {
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_get_cipherbyname(const char *name) {
|
||||
if (name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (OPENSSL_strcasecmp(name, "rc4") == 0) {
|
||||
return EVP_rc4();
|
||||
} else if (OPENSSL_strcasecmp(name, "des-cbc") == 0) {
|
||||
|
||||
@@ -18,18 +18,15 @@
|
||||
|
||||
#include <CCryptoBoringSSL_chacha.h>
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_poly1305.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../chacha/internal.h"
|
||||
#include "../fipsmodule/cipher/internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../chacha/internal.h"
|
||||
|
||||
|
||||
#define POLY1305_TAG_LEN 16
|
||||
|
||||
struct aead_chacha20_poly1305_ctx {
|
||||
uint8_t key[32];
|
||||
@@ -44,78 +41,6 @@ OPENSSL_STATIC_ASSERT(alignof(union evp_aead_ctx_st_state) >=
|
||||
"AEAD state has insufficient alignment");
|
||||
#endif
|
||||
|
||||
// For convenience (the x86_64 calling convention allows only six parameters in
|
||||
// registers), the final parameter for the assembly functions is both an input
|
||||
// and output parameter.
|
||||
union open_data {
|
||||
struct {
|
||||
alignas(16) uint8_t key[32];
|
||||
uint32_t counter;
|
||||
uint8_t nonce[12];
|
||||
} in;
|
||||
struct {
|
||||
uint8_t tag[POLY1305_TAG_LEN];
|
||||
} out;
|
||||
};
|
||||
|
||||
union seal_data {
|
||||
struct {
|
||||
alignas(16) uint8_t key[32];
|
||||
uint32_t counter;
|
||||
uint8_t nonce[12];
|
||||
const uint8_t *extra_ciphertext;
|
||||
size_t extra_ciphertext_len;
|
||||
} in;
|
||||
struct {
|
||||
uint8_t tag[POLY1305_TAG_LEN];
|
||||
} out;
|
||||
};
|
||||
|
||||
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
|
||||
!defined(OPENSSL_WINDOWS)
|
||||
static int asm_capable(void) {
|
||||
const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
|
||||
return sse41_capable;
|
||||
}
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(union open_data) == 48, "wrong open_data size");
|
||||
OPENSSL_STATIC_ASSERT(sizeof(union seal_data) == 48 + 8 + 8,
|
||||
"wrong seal_data size");
|
||||
|
||||
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
|
||||
// |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
|
||||
// Additional input parameters are passed in |aead_data->in|. On exit, it will
|
||||
// write calculated tag value to |aead_data->out.tag|, which the caller must
|
||||
// check.
|
||||
extern void chacha20_poly1305_open(uint8_t *out_plaintext,
|
||||
const uint8_t *ciphertext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len, union open_data *aead_data);
|
||||
|
||||
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
|
||||
// |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
|
||||
// Additional input parameters are passed in |aead_data->in|. The calculated tag
|
||||
// value is over the computed ciphertext concatenated with |extra_ciphertext|
|
||||
// and written to |aead_data->out.tag|.
|
||||
extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
||||
const uint8_t *plaintext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len, union seal_data *aead_data);
|
||||
#else
|
||||
static int asm_capable(void) { return 0; }
|
||||
|
||||
|
||||
static void chacha20_poly1305_open(uint8_t *out_plaintext,
|
||||
const uint8_t *ciphertext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len, union open_data *aead_data) {}
|
||||
|
||||
static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
||||
const uint8_t *plaintext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len, union seal_data *aead_data) {}
|
||||
#endif
|
||||
|
||||
static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
||||
size_t key_len, size_t tag_len) {
|
||||
struct aead_chacha20_poly1305_ctx *c20_ctx =
|
||||
@@ -238,8 +163,8 @@ static int chacha20_poly1305_seal_scatter(
|
||||
}
|
||||
}
|
||||
|
||||
union seal_data data;
|
||||
if (asm_capable()) {
|
||||
union chacha20_poly1305_seal_data data;
|
||||
if (chacha20_poly1305_asm_capable()) {
|
||||
OPENSSL_memcpy(data.in.key, key, 32);
|
||||
data.in.counter = 0;
|
||||
OPENSSL_memcpy(data.in.nonce, nonce, 12);
|
||||
@@ -321,8 +246,8 @@ static int chacha20_poly1305_open_gather(
|
||||
return 0;
|
||||
}
|
||||
|
||||
union open_data data;
|
||||
if (asm_capable()) {
|
||||
union chacha20_poly1305_open_data data;
|
||||
if (chacha20_poly1305_asm_capable()) {
|
||||
OPENSSL_memcpy(data.in.key, key, 32);
|
||||
data.in.counter = 0;
|
||||
OPENSSL_memcpy(data.in.nonce, nonce, 12);
|
||||
|
||||
@@ -343,7 +343,7 @@ static int aead_tls_open(const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len,
|
||||
if (EVP_CIPHER_CTX_mode(&tls_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE &&
|
||||
EVP_tls_cbc_record_digest_supported(tls_ctx->hmac_ctx.md)) {
|
||||
if (!EVP_tls_cbc_digest_record(tls_ctx->hmac_ctx.md, mac, &mac_len,
|
||||
ad_fixed, out, data_plus_mac_len, total,
|
||||
ad_fixed, out, data_len, total,
|
||||
tls_ctx->mac_key, tls_ctx->mac_key_len)) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
||||
return 0;
|
||||
@@ -406,14 +406,6 @@ static int aead_aes_128_cbc_sha1_tls_implicit_iv_init(
|
||||
EVP_sha1(), 1);
|
||||
}
|
||||
|
||||
static int aead_aes_128_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
|
||||
EVP_sha256(), 0);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
||||
size_t key_len, size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
@@ -428,22 +420,6 @@ static int aead_aes_256_cbc_sha1_tls_implicit_iv_init(
|
||||
EVP_sha1(), 1);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha256_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
||||
EVP_sha256(), 0);
|
||||
}
|
||||
|
||||
static int aead_aes_256_cbc_sha384_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
enum evp_aead_direction_t dir) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
|
||||
EVP_sha384(), 0);
|
||||
}
|
||||
|
||||
static int aead_des_ede3_cbc_sha1_tls_init(EVP_AEAD_CTX *ctx,
|
||||
const uint8_t *key, size_t key_len,
|
||||
size_t tag_len,
|
||||
@@ -513,23 +489,6 @@ static const EVP_AEAD aead_aes_128_cbc_sha1_tls_implicit_iv = {
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_128_cbc_sha256_tls = {
|
||||
SHA256_DIGEST_LENGTH + 16, // key len (SHA256 + AES128)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
|
||||
SHA256_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_128_cbc_sha256_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH + 32, // key len (SHA1 + AES256)
|
||||
16, // nonce len (IV)
|
||||
@@ -564,40 +523,6 @@ static const EVP_AEAD aead_aes_256_cbc_sha1_tls_implicit_iv = {
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha256_tls = {
|
||||
SHA256_DIGEST_LENGTH + 32, // key len (SHA256 + AES256)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA256_DIGEST_LENGTH, // overhead (padding + SHA256)
|
||||
SHA256_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_256_cbc_sha256_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_aes_256_cbc_sha384_tls = {
|
||||
SHA384_DIGEST_LENGTH + 32, // key len (SHA384 + AES256)
|
||||
16, // nonce len (IV)
|
||||
16 + SHA384_DIGEST_LENGTH, // overhead (padding + SHA384)
|
||||
SHA384_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_aes_256_cbc_sha384_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_des_ede3_cbc_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH + 24, // key len (SHA1 + 3DES)
|
||||
8, // nonce len (IV)
|
||||
@@ -657,10 +582,6 @@ const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void) {
|
||||
return &aead_aes_128_cbc_sha1_tls_implicit_iv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void) {
|
||||
return &aead_aes_128_cbc_sha256_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void) {
|
||||
return &aead_aes_256_cbc_sha1_tls;
|
||||
}
|
||||
@@ -669,14 +590,6 @@ const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void) {
|
||||
return &aead_aes_256_cbc_sha1_tls_implicit_iv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void) {
|
||||
return &aead_aes_256_cbc_sha256_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void) {
|
||||
return &aead_aes_256_cbc_sha384_tls;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
|
||||
return &aead_des_ede3_cbc_sha1_tls;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,11 @@
|
||||
#ifndef OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
|
||||
#define OPENSSL_HEADER_CIPHER_EXTRA_INTERNAL_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
@@ -95,6 +99,17 @@ void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
|
||||
// which EVP_tls_cbc_digest_record supports.
|
||||
int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
|
||||
// EVP_sha1_final_with_secret_suffix computes the result of hashing |len| bytes
|
||||
// from |in| to |ctx| and writes the resulting hash to |out|. |len| is treated
|
||||
// as secret and must be at most |max_len|, which is treated as public. |in|
|
||||
// must point to a buffer of at least |max_len| bytes. It returns one on success
|
||||
// and zero if inputs are too long.
|
||||
//
|
||||
// This function is exported for unit tests.
|
||||
OPENSSL_EXPORT int EVP_sha1_final_with_secret_suffix(
|
||||
SHA_CTX *ctx, uint8_t out[SHA_DIGEST_LENGTH], const uint8_t *in, size_t len,
|
||||
size_t max_len);
|
||||
|
||||
// EVP_tls_cbc_digest_record computes the MAC of a decrypted, padded TLS
|
||||
// record.
|
||||
//
|
||||
@@ -104,8 +119,8 @@ int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
// md_out_size: the number of output bytes is written here.
|
||||
// header: the 13-byte, TLS record header.
|
||||
// data: the record data itself
|
||||
// data_plus_mac_size: the secret, reported length of the data and MAC
|
||||
// once the padding has been removed.
|
||||
// data_size: the secret, reported length of the data once the padding and MAC
|
||||
// have been removed.
|
||||
// data_plus_mac_plus_padding_size: the public length of the whole
|
||||
// record, including padding.
|
||||
//
|
||||
@@ -115,11 +130,94 @@ int EVP_tls_cbc_record_digest_supported(const EVP_MD *md);
|
||||
// padding too. )
|
||||
int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
size_t *md_out_size, const uint8_t header[13],
|
||||
const uint8_t *data, size_t data_plus_mac_size,
|
||||
const uint8_t *data, size_t data_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length);
|
||||
|
||||
#define POLY1305_TAG_LEN 16
|
||||
|
||||
// For convenience (the x86_64 calling convention allows only six parameters in
|
||||
// registers), the final parameter for the assembly functions is both an input
|
||||
// and output parameter.
|
||||
union chacha20_poly1305_open_data {
|
||||
struct {
|
||||
alignas(16) uint8_t key[32];
|
||||
uint32_t counter;
|
||||
uint8_t nonce[12];
|
||||
} in;
|
||||
struct {
|
||||
uint8_t tag[POLY1305_TAG_LEN];
|
||||
} out;
|
||||
};
|
||||
|
||||
union chacha20_poly1305_seal_data {
|
||||
struct {
|
||||
alignas(16) uint8_t key[32];
|
||||
uint32_t counter;
|
||||
uint8_t nonce[12];
|
||||
const uint8_t *extra_ciphertext;
|
||||
size_t extra_ciphertext_len;
|
||||
} in;
|
||||
struct {
|
||||
uint8_t tag[POLY1305_TAG_LEN];
|
||||
} out;
|
||||
};
|
||||
|
||||
#if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM)
|
||||
|
||||
OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_open_data) == 48,
|
||||
"wrong chacha20_poly1305_open_data size");
|
||||
OPENSSL_STATIC_ASSERT(sizeof(union chacha20_poly1305_seal_data) == 48 + 8 + 8,
|
||||
"wrong chacha20_poly1305_seal_data size");
|
||||
|
||||
OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) {
|
||||
const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
|
||||
return sse41_capable;
|
||||
}
|
||||
|
||||
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
|
||||
// |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
|
||||
// Additional input parameters are passed in |aead_data->in|. On exit, it will
|
||||
// write calculated tag value to |aead_data->out.tag|, which the caller must
|
||||
// check.
|
||||
extern void chacha20_poly1305_open(uint8_t *out_plaintext,
|
||||
const uint8_t *ciphertext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len,
|
||||
union chacha20_poly1305_open_data *data);
|
||||
|
||||
// chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
|
||||
// |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
|
||||
// Additional input parameters are passed in |aead_data->in|. The calculated tag
|
||||
// value is over the computed ciphertext concatenated with |extra_ciphertext|
|
||||
// and written to |aead_data->out.tag|.
|
||||
extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
||||
const uint8_t *plaintext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len,
|
||||
union chacha20_poly1305_seal_data *data);
|
||||
#else
|
||||
|
||||
OPENSSL_INLINE int chacha20_poly1305_asm_capable(void) { return 0; }
|
||||
|
||||
OPENSSL_INLINE void chacha20_poly1305_open(uint8_t *out_plaintext,
|
||||
const uint8_t *ciphertext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len,
|
||||
union chacha20_poly1305_open_data *data) {
|
||||
abort();
|
||||
}
|
||||
|
||||
OPENSSL_INLINE void chacha20_poly1305_seal(uint8_t *out_ciphertext,
|
||||
const uint8_t *plaintext,
|
||||
size_t plaintext_len, const uint8_t *ad,
|
||||
size_t ad_len,
|
||||
union chacha20_poly1305_seal_data *data) {
|
||||
abort();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
|
||||
@@ -62,15 +62,6 @@
|
||||
#include "../fipsmodule/cipher/internal.h"
|
||||
|
||||
|
||||
// MAX_HASH_BIT_COUNT_BYTES is the maximum number of bytes in the hash's length
|
||||
// field. (SHA-384/512 have 128-bit length.)
|
||||
#define MAX_HASH_BIT_COUNT_BYTES 16
|
||||
|
||||
// MAX_HASH_BLOCK_SIZE is the maximum hash block size that we'll support.
|
||||
// Currently SHA-384/512 has a 128-byte block size and that's the largest
|
||||
// supported by TLS.)
|
||||
#define MAX_HASH_BLOCK_SIZE 128
|
||||
|
||||
int EVP_tls_cbc_remove_padding(crypto_word_t *out_padding_ok, size_t *out_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
size_t block_size, size_t mac_size) {
|
||||
@@ -183,134 +174,110 @@ void EVP_tls_cbc_copy_mac(uint8_t *out, size_t md_size, const uint8_t *in,
|
||||
OPENSSL_memcpy(out, rotated_mac, md_size);
|
||||
}
|
||||
|
||||
// u32toBE serialises an unsigned, 32-bit number (n) as four bytes at (p) in
|
||||
// big-endian order. The value of p is advanced by four.
|
||||
#define u32toBE(n, p) \
|
||||
do { \
|
||||
*((p)++) = (uint8_t)((n) >> 24); \
|
||||
*((p)++) = (uint8_t)((n) >> 16); \
|
||||
*((p)++) = (uint8_t)((n) >> 8); \
|
||||
*((p)++) = (uint8_t)((n)); \
|
||||
} while (0)
|
||||
|
||||
// u64toBE serialises an unsigned, 64-bit number (n) as eight bytes at (p) in
|
||||
// big-endian order. The value of p is advanced by eight.
|
||||
#define u64toBE(n, p) \
|
||||
do { \
|
||||
*((p)++) = (uint8_t)((n) >> 56); \
|
||||
*((p)++) = (uint8_t)((n) >> 48); \
|
||||
*((p)++) = (uint8_t)((n) >> 40); \
|
||||
*((p)++) = (uint8_t)((n) >> 32); \
|
||||
*((p)++) = (uint8_t)((n) >> 24); \
|
||||
*((p)++) = (uint8_t)((n) >> 16); \
|
||||
*((p)++) = (uint8_t)((n) >> 8); \
|
||||
*((p)++) = (uint8_t)((n)); \
|
||||
} while (0)
|
||||
|
||||
typedef union {
|
||||
SHA_CTX sha1;
|
||||
SHA256_CTX sha256;
|
||||
SHA512_CTX sha512;
|
||||
} HASH_CTX;
|
||||
|
||||
static void tls1_sha1_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA1_Transform(&ctx->sha1, block);
|
||||
}
|
||||
|
||||
static void tls1_sha256_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA256_Transform(&ctx->sha256, block);
|
||||
}
|
||||
|
||||
static void tls1_sha512_transform(HASH_CTX *ctx, const uint8_t *block) {
|
||||
SHA512_Transform(&ctx->sha512, block);
|
||||
}
|
||||
|
||||
// These functions serialize the state of a hash and thus perform the standard
|
||||
// "final" operation without adding the padding and length that such a function
|
||||
// typically does.
|
||||
static void tls1_sha1_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA_CTX *sha1 = &ctx->sha1;
|
||||
u32toBE(sha1->h[0], md_out);
|
||||
u32toBE(sha1->h[1], md_out);
|
||||
u32toBE(sha1->h[2], md_out);
|
||||
u32toBE(sha1->h[3], md_out);
|
||||
u32toBE(sha1->h[4], md_out);
|
||||
}
|
||||
|
||||
static void tls1_sha256_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA256_CTX *sha256 = &ctx->sha256;
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
u32toBE(sha256->h[i], md_out);
|
||||
int EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx,
|
||||
uint8_t out[SHA_DIGEST_LENGTH],
|
||||
const uint8_t *in, size_t len,
|
||||
size_t max_len) {
|
||||
// Bound the input length so |total_bits| below fits in four bytes. This is
|
||||
// redundant with TLS record size limits. This also ensures |input_idx| below
|
||||
// does not overflow.
|
||||
size_t max_len_bits = max_len << 3;
|
||||
if (ctx->Nh != 0 ||
|
||||
(max_len_bits >> 3) != max_len || // Overflow
|
||||
ctx->Nl + max_len_bits < max_len_bits ||
|
||||
ctx->Nl + max_len_bits > UINT32_MAX) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void tls1_sha512_final_raw(HASH_CTX *ctx, uint8_t *md_out) {
|
||||
SHA512_CTX *sha512 = &ctx->sha512;
|
||||
for (unsigned i = 0; i < 8; i++) {
|
||||
u64toBE(sha512->h[i], md_out);
|
||||
// We need to hash the following into |ctx|:
|
||||
//
|
||||
// - ctx->data[:ctx->num]
|
||||
// - in[:len]
|
||||
// - A 0x80 byte
|
||||
// - However many zero bytes are needed to pad up to a block.
|
||||
// - Eight bytes of length.
|
||||
size_t num_blocks = (ctx->num + len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
|
||||
size_t last_block = num_blocks - 1;
|
||||
size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA_CBLOCK - 1) >> 6;
|
||||
|
||||
// The bounds above imply |total_bits| fits in four bytes.
|
||||
size_t total_bits = ctx->Nl + (len << 3);
|
||||
uint8_t length_bytes[4];
|
||||
length_bytes[0] = (uint8_t)(total_bits >> 24);
|
||||
length_bytes[1] = (uint8_t)(total_bits >> 16);
|
||||
length_bytes[2] = (uint8_t)(total_bits >> 8);
|
||||
length_bytes[3] = (uint8_t)total_bits;
|
||||
|
||||
// We now construct and process each expected block in constant-time.
|
||||
uint8_t block[SHA_CBLOCK] = {0};
|
||||
uint32_t result[5] = {0};
|
||||
// input_idx is the index into |in| corresponding to the current block.
|
||||
// However, we allow this index to overflow beyond |max_len|, to simplify the
|
||||
// 0x80 byte.
|
||||
size_t input_idx = 0;
|
||||
for (size_t i = 0; i < max_blocks; i++) {
|
||||
// Fill |block| with data from the partial block in |ctx| and |in|. We copy
|
||||
// as if we were hashing up to |max_len| and then zero the excess later.
|
||||
size_t block_start = 0;
|
||||
if (i == 0) {
|
||||
OPENSSL_memcpy(block, ctx->data, ctx->num);
|
||||
block_start = ctx->num;
|
||||
}
|
||||
if (input_idx < max_len) {
|
||||
size_t to_copy = SHA_CBLOCK - block_start;
|
||||
if (to_copy > max_len - input_idx) {
|
||||
to_copy = max_len - input_idx;
|
||||
}
|
||||
OPENSSL_memcpy(block + block_start, in + input_idx, to_copy);
|
||||
}
|
||||
|
||||
// Zero any bytes beyond |len| and add the 0x80 byte.
|
||||
for (size_t j = block_start; j < SHA_CBLOCK; j++) {
|
||||
// input[idx] corresponds to block[j].
|
||||
size_t idx = input_idx + j - block_start;
|
||||
// The barriers on |len| are not strictly necessary. However, without
|
||||
// them, GCC compiles this code by incorporating |len| into the loop
|
||||
// counter and subtracting it out later. This is still constant-time, but
|
||||
// it frustrates attempts to validate this.
|
||||
uint8_t is_in_bounds = constant_time_lt_8(idx, value_barrier_w(len));
|
||||
uint8_t is_padding_byte = constant_time_eq_8(idx, value_barrier_w(len));
|
||||
block[j] &= is_in_bounds;
|
||||
block[j] |= 0x80 & is_padding_byte;
|
||||
}
|
||||
|
||||
input_idx += SHA_CBLOCK - block_start;
|
||||
|
||||
// Fill in the length if this is the last block.
|
||||
crypto_word_t is_last_block = constant_time_eq_w(i, last_block);
|
||||
for (size_t j = 0; j < 4; j++) {
|
||||
block[SHA_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
|
||||
}
|
||||
|
||||
// Process the block and save the hash state if it is the final value.
|
||||
SHA1_Transform(ctx, block);
|
||||
for (size_t j = 0; j < 5; j++) {
|
||||
result[j] |= is_last_block & ctx->h[j];
|
||||
}
|
||||
}
|
||||
|
||||
// Write the output.
|
||||
for (size_t i = 0; i < 5; i++) {
|
||||
CRYPTO_store_u32_be(out + 4 * i, result[i]);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
|
||||
switch (EVP_MD_type(md)) {
|
||||
case NID_sha1:
|
||||
case NID_sha256:
|
||||
case NID_sha384:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
return EVP_MD_type(md) == NID_sha1;
|
||||
}
|
||||
|
||||
int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
size_t *md_out_size, const uint8_t header[13],
|
||||
const uint8_t *data, size_t data_plus_mac_size,
|
||||
const uint8_t *data, size_t data_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length) {
|
||||
HASH_CTX md_state;
|
||||
void (*md_final_raw)(HASH_CTX *ctx, uint8_t *md_out);
|
||||
void (*md_transform)(HASH_CTX *ctx, const uint8_t *block);
|
||||
unsigned md_size, md_block_size = 64, md_block_shift = 6;
|
||||
// md_length_size is the number of bytes in the length field that terminates
|
||||
// the hash.
|
||||
unsigned md_length_size = 8;
|
||||
|
||||
// Bound the acceptable input so we can forget about many possible overflows
|
||||
// later in this function. This is redundant with the record size limits in
|
||||
// TLS.
|
||||
if (data_plus_mac_plus_padding_size >= 1024 * 1024) {
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (EVP_MD_type(md)) {
|
||||
case NID_sha1:
|
||||
SHA1_Init(&md_state.sha1);
|
||||
md_final_raw = tls1_sha1_final_raw;
|
||||
md_transform = tls1_sha1_transform;
|
||||
md_size = SHA_DIGEST_LENGTH;
|
||||
break;
|
||||
|
||||
case NID_sha256:
|
||||
SHA256_Init(&md_state.sha256);
|
||||
md_final_raw = tls1_sha256_final_raw;
|
||||
md_transform = tls1_sha256_transform;
|
||||
md_size = SHA256_DIGEST_LENGTH;
|
||||
break;
|
||||
|
||||
case NID_sha384:
|
||||
SHA384_Init(&md_state.sha512);
|
||||
md_final_raw = tls1_sha512_final_raw;
|
||||
md_transform = tls1_sha512_transform;
|
||||
md_size = SHA384_DIGEST_LENGTH;
|
||||
md_block_size = 128;
|
||||
md_block_shift = 7;
|
||||
md_length_size = 16;
|
||||
break;
|
||||
|
||||
default:
|
||||
if (EVP_MD_type(md) != NID_sha1) {
|
||||
// EVP_tls_cbc_record_digest_supported should have been called first to
|
||||
// check that the hash function is supported.
|
||||
assert(0);
|
||||
@@ -318,175 +285,54 @@ int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(md_length_size <= MAX_HASH_BIT_COUNT_BYTES);
|
||||
assert(md_block_size <= MAX_HASH_BLOCK_SIZE);
|
||||
assert(md_block_size == (1u << md_block_shift));
|
||||
assert(md_size <= EVP_MAX_MD_SIZE);
|
||||
|
||||
static const size_t kHeaderLength = 13;
|
||||
|
||||
// kVarianceBlocks is the number of blocks of the hash that we have to
|
||||
// calculate in constant time because they could be altered by the
|
||||
// padding value.
|
||||
//
|
||||
// TLSv1 has MACs up to 48 bytes long (SHA-384) and the padding is not
|
||||
// required to be minimal. Therefore we say that the final |kVarianceBlocks|
|
||||
// blocks can vary based on the padding and on the hash used. This value
|
||||
// must be derived from public information.
|
||||
const size_t kVarianceBlocks =
|
||||
( 255 + 1 + // maximum padding bytes + padding length
|
||||
md_size + // length of hash's output
|
||||
md_block_size - 1 // ceiling
|
||||
) / md_block_size
|
||||
+ 1; // the 0x80 marker and the encoded message length could or not
|
||||
// require an extra block; since the exact value depends on the
|
||||
// message length; thus, one extra block is always added to run
|
||||
// in constant time.
|
||||
|
||||
// From now on we're dealing with the MAC, which conceptually has 13
|
||||
// bytes of `header' before the start of the data.
|
||||
size_t len = data_plus_mac_plus_padding_size + kHeaderLength;
|
||||
// max_mac_bytes contains the maximum bytes of bytes in the MAC, including
|
||||
// |header|, assuming that there's no padding.
|
||||
size_t max_mac_bytes = len - md_size - 1;
|
||||
// num_blocks is the maximum number of hash blocks.
|
||||
size_t num_blocks =
|
||||
(max_mac_bytes + 1 + md_length_size + md_block_size - 1) / md_block_size;
|
||||
// In order to calculate the MAC in constant time we have to handle
|
||||
// the final blocks specially because the padding value could cause the
|
||||
// end to appear somewhere in the final |kVarianceBlocks| blocks and we
|
||||
// can't leak where. However, |num_starting_blocks| worth of data can
|
||||
// be hashed right away because no padding value can affect whether
|
||||
// they are plaintext.
|
||||
size_t num_starting_blocks = 0;
|
||||
// k is the starting byte offset into the conceptual header||data where
|
||||
// we start processing.
|
||||
size_t k = 0;
|
||||
// mac_end_offset is the index just past the end of the data to be MACed.
|
||||
size_t mac_end_offset = data_plus_mac_size + kHeaderLength - md_size;
|
||||
// c is the index of the 0x80 byte in the final hash block that contains
|
||||
// application data.
|
||||
size_t c = mac_end_offset & (md_block_size - 1);
|
||||
// index_a is the hash block number that contains the 0x80 terminating value.
|
||||
size_t index_a = mac_end_offset >> md_block_shift;
|
||||
// index_b is the hash block number that contains the 64-bit hash length, in
|
||||
// bits.
|
||||
size_t index_b = (mac_end_offset + md_length_size) >> md_block_shift;
|
||||
|
||||
if (num_blocks > kVarianceBlocks) {
|
||||
num_starting_blocks = num_blocks - kVarianceBlocks;
|
||||
k = md_block_size * num_starting_blocks;
|
||||
if (mac_secret_length > SHA_CBLOCK) {
|
||||
// HMAC pads small keys with zeros and hashes large keys down. This function
|
||||
// should never reach the large key case.
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// bits is the hash-length in bits. It includes the additional hash
|
||||
// block for the masked HMAC key.
|
||||
size_t bits = 8 * mac_end_offset; // at most 18 bits to represent
|
||||
|
||||
// Compute the initial HMAC block.
|
||||
bits += 8 * md_block_size;
|
||||
// hmac_pad is the masked HMAC key.
|
||||
uint8_t hmac_pad[MAX_HASH_BLOCK_SIZE];
|
||||
OPENSSL_memset(hmac_pad, 0, md_block_size);
|
||||
assert(mac_secret_length <= sizeof(hmac_pad));
|
||||
uint8_t hmac_pad[SHA_CBLOCK];
|
||||
OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
|
||||
OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
|
||||
for (size_t i = 0; i < md_block_size; i++) {
|
||||
for (size_t i = 0; i < SHA_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x36;
|
||||
}
|
||||
|
||||
md_transform(&md_state, hmac_pad);
|
||||
SHA_CTX ctx;
|
||||
SHA1_Init(&ctx);
|
||||
SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
|
||||
SHA1_Update(&ctx, header, 13);
|
||||
|
||||
// The length check means |bits| fits in four bytes.
|
||||
uint8_t length_bytes[MAX_HASH_BIT_COUNT_BYTES];
|
||||
OPENSSL_memset(length_bytes, 0, md_length_size - 4);
|
||||
length_bytes[md_length_size - 4] = (uint8_t)(bits >> 24);
|
||||
length_bytes[md_length_size - 3] = (uint8_t)(bits >> 16);
|
||||
length_bytes[md_length_size - 2] = (uint8_t)(bits >> 8);
|
||||
length_bytes[md_length_size - 1] = (uint8_t)bits;
|
||||
|
||||
if (k > 0) {
|
||||
// k is a multiple of md_block_size.
|
||||
uint8_t first_block[MAX_HASH_BLOCK_SIZE];
|
||||
OPENSSL_memcpy(first_block, header, 13);
|
||||
OPENSSL_memcpy(first_block + 13, data, md_block_size - 13);
|
||||
md_transform(&md_state, first_block);
|
||||
for (size_t i = 1; i < k / md_block_size; i++) {
|
||||
md_transform(&md_state, data + md_block_size * i - 13);
|
||||
}
|
||||
// There are at most 256 bytes of padding, so we can compute the public
|
||||
// minimum length for |data_size|.
|
||||
size_t min_data_size = 0;
|
||||
if (data_plus_mac_plus_padding_size > SHA_DIGEST_LENGTH + 256) {
|
||||
min_data_size = data_plus_mac_plus_padding_size - SHA_DIGEST_LENGTH - 256;
|
||||
}
|
||||
|
||||
uint8_t mac_out[EVP_MAX_MD_SIZE];
|
||||
OPENSSL_memset(mac_out, 0, sizeof(mac_out));
|
||||
// Hash the public minimum length directly. This reduces the number of blocks
|
||||
// that must be computed in constant-time.
|
||||
SHA1_Update(&ctx, data, min_data_size);
|
||||
|
||||
// We now process the final hash blocks. For each block, we construct
|
||||
// it in constant time. If the |i==index_a| then we'll include the 0x80
|
||||
// bytes and zero pad etc. For each block we selectively copy it, in
|
||||
// constant time, to |mac_out|.
|
||||
for (size_t i = num_starting_blocks;
|
||||
i <= num_starting_blocks + kVarianceBlocks; i++) {
|
||||
uint8_t block[MAX_HASH_BLOCK_SIZE];
|
||||
uint8_t is_block_a = constant_time_eq_8(i, index_a);
|
||||
uint8_t is_block_b = constant_time_eq_8(i, index_b);
|
||||
for (size_t j = 0; j < md_block_size; j++) {
|
||||
uint8_t b = 0;
|
||||
if (k < kHeaderLength) {
|
||||
b = header[k];
|
||||
} else if (k < data_plus_mac_plus_padding_size + kHeaderLength) {
|
||||
b = data[k - kHeaderLength];
|
||||
}
|
||||
k++;
|
||||
|
||||
uint8_t is_past_c = is_block_a & constant_time_ge_8(j, c);
|
||||
uint8_t is_past_cp1 = is_block_a & constant_time_ge_8(j, c + 1);
|
||||
// If this is the block containing the end of the
|
||||
// application data, and we are at the offset for the
|
||||
// 0x80 value, then overwrite b with 0x80.
|
||||
b = constant_time_select_8(is_past_c, 0x80, b);
|
||||
// If this the the block containing the end of the
|
||||
// application data and we're past the 0x80 value then
|
||||
// just write zero.
|
||||
b = b & ~is_past_cp1;
|
||||
// If this is index_b (the final block), but not
|
||||
// index_a (the end of the data), then the 64-bit
|
||||
// length didn't fit into index_a and we're having to
|
||||
// add an extra block of zeros.
|
||||
b &= ~is_block_b | is_block_a;
|
||||
|
||||
// The final bytes of one of the blocks contains the
|
||||
// length.
|
||||
if (j >= md_block_size - md_length_size) {
|
||||
// If this is index_b, write a length byte.
|
||||
b = constant_time_select_8(
|
||||
is_block_b, length_bytes[j - (md_block_size - md_length_size)], b);
|
||||
}
|
||||
block[j] = b;
|
||||
}
|
||||
|
||||
md_transform(&md_state, block);
|
||||
md_final_raw(&md_state, block);
|
||||
// If this is index_b, copy the hash value to |mac_out|.
|
||||
for (size_t j = 0; j < md_size; j++) {
|
||||
mac_out[j] |= block[j] & is_block_b;
|
||||
}
|
||||
}
|
||||
|
||||
EVP_MD_CTX md_ctx;
|
||||
EVP_MD_CTX_init(&md_ctx);
|
||||
if (!EVP_DigestInit_ex(&md_ctx, md, NULL /* engine */)) {
|
||||
EVP_MD_CTX_cleanup(&md_ctx);
|
||||
// Hash the remaining data without leaking |data_size|.
|
||||
uint8_t mac_out[SHA_DIGEST_LENGTH];
|
||||
if (!EVP_sha1_final_with_secret_suffix(
|
||||
&ctx, mac_out, data + min_data_size, data_size - min_data_size,
|
||||
data_plus_mac_plus_padding_size - min_data_size)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Complete the HMAC in the standard manner.
|
||||
for (size_t i = 0; i < md_block_size; i++) {
|
||||
SHA1_Init(&ctx);
|
||||
for (size_t i = 0; i < SHA_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x6a;
|
||||
}
|
||||
|
||||
EVP_DigestUpdate(&md_ctx, hmac_pad, md_block_size);
|
||||
EVP_DigestUpdate(&md_ctx, mac_out, md_size);
|
||||
unsigned md_out_size_u;
|
||||
EVP_DigestFinal(&md_ctx, md_out, &md_out_size_u);
|
||||
*md_out_size = md_out_size_u;
|
||||
EVP_MD_CTX_cleanup(&md_ctx);
|
||||
|
||||
SHA1_Update(&ctx, hmac_pad, SHA_CBLOCK);
|
||||
SHA1_Update(&ctx, mac_out, SHA_DIGEST_LENGTH);
|
||||
SHA1_Final(md_out, &ctx);
|
||||
*md_out_size = SHA_DIGEST_LENGTH;
|
||||
return 1;
|
||||
}
|
||||
@@ -68,6 +68,7 @@
|
||||
#include "conf_def.h"
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../lhash/internal.h"
|
||||
|
||||
|
||||
DEFINE_LHASH_OF(CONF_VALUE)
|
||||
@@ -76,12 +77,16 @@ struct conf_st {
|
||||
LHASH_OF(CONF_VALUE) *data;
|
||||
};
|
||||
|
||||
static const char kDefaultSectionName[] = "default";
|
||||
|
||||
// The maximum length we can grow a value to after variable expansion. 64k
|
||||
// should be more than enough for all reasonable uses.
|
||||
#define MAX_CONF_VALUE_LENGTH 65536
|
||||
|
||||
static uint32_t conf_value_hash(const CONF_VALUE *v) {
|
||||
return (lh_strhash(v->section) << 2) ^ lh_strhash(v->name);
|
||||
const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
|
||||
const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
|
||||
return (section_hash << 2) ^ name_hash;
|
||||
}
|
||||
|
||||
static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
|
||||
@@ -155,12 +160,14 @@ static void value_free(CONF_VALUE *value) {
|
||||
OPENSSL_free(value);
|
||||
}
|
||||
|
||||
static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
|
||||
|
||||
void NCONF_free(CONF *conf) {
|
||||
if (conf == NULL || conf->data == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
lh_CONF_VALUE_doall(conf->data, value_free);
|
||||
lh_CONF_VALUE_doall_arg(conf->data, value_free_arg, NULL);
|
||||
lh_CONF_VALUE_free(conf->data);
|
||||
OPENSSL_free(conf);
|
||||
}
|
||||
@@ -390,6 +397,10 @@ const char *NCONF_get_string(const CONF *conf, const char *section,
|
||||
const char *name) {
|
||||
CONF_VALUE template, *value;
|
||||
|
||||
if (section == NULL) {
|
||||
section = kDefaultSectionName;
|
||||
}
|
||||
|
||||
OPENSSL_memset(&template, 0, sizeof(template));
|
||||
template.section = (char *) section;
|
||||
template.name = (char *) name;
|
||||
@@ -538,7 +549,7 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
section = OPENSSL_strdup("default");
|
||||
section = OPENSSL_strdup(kDefaultSectionName);
|
||||
if (section == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/* Copyright (c) 2018, Google Inc.
|
||||
* Copyright (c) 2020, Arm Ltd.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_WINDOWS) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
// We do not need to check for the presence of NEON, as Armv8-A always has it
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) {
|
||||
// These are all covered by one call in Windows
|
||||
OPENSSL_armcap_P |= ARMV8_AES;
|
||||
OPENSSL_armcap_P |= ARMV8_PMULL;
|
||||
OPENSSL_armcap_P |= ARMV8_SHA1;
|
||||
OPENSSL_armcap_P |= ARMV8_SHA256;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -175,7 +175,13 @@ void OPENSSL_cpuid_setup(void) {
|
||||
hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
|
||||
}
|
||||
|
||||
// Clear NEON support if known broken.
|
||||
// Clear NEON support if known broken. Note, if NEON is available statically,
|
||||
// the non-NEON code is dropped and this workaround is a no-op.
|
||||
//
|
||||
// TODO(davidben): The Android NDK now builds with NEON statically available
|
||||
// by default. Cronet still has some consumers that support NEON-less devices
|
||||
// (b/150371744). Get metrics on whether they still see this CPU and, if not,
|
||||
// remove this check entirely.
|
||||
g_has_broken_neon = crypto_cpuinfo_has_broken_neon(&cpuinfo);
|
||||
if (g_has_broken_neon) {
|
||||
hwcap &= ~HWCAP_NEON;
|
||||
@@ -186,7 +192,10 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
// Some ARMv8 Android devices don't expose AT_HWCAP2. Fall back to
|
||||
// /proc/cpuinfo. See https://crbug.com/596156.
|
||||
// /proc/cpuinfo. See https://crbug.com/boringssl/46. As of February 2021,
|
||||
// this is now rare (see Chrome's Net.NeedsHWCAP2Workaround metric), but AES
|
||||
// and PMULL extensions are very useful, so we still carry the workaround
|
||||
// for now.
|
||||
unsigned long hwcap2 = 0;
|
||||
if (getauxval != NULL) {
|
||||
hwcap2 = getauxval(AT_HWCAP2);
|
||||
|
||||
@@ -22,15 +22,15 @@
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
char CRYPTO_is_NEON_capable_at_runtime(void) {
|
||||
int CRYPTO_is_NEON_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
|
||||
}
|
||||
|
||||
int CRYPTO_is_ARMv8_AES_capable(void) {
|
||||
int CRYPTO_is_ARMv8_AES_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV8_AES) != 0;
|
||||
}
|
||||
|
||||
int CRYPTO_is_ARMv8_PMULL_capable(void) {
|
||||
int CRYPTO_is_ARMv8_PMULL_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV8_PMULL) != 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -820,7 +820,7 @@ static void table_select(ge_precomp *t, int pos, signed char b) {
|
||||
//
|
||||
// Preconditions:
|
||||
// a[31] <= 127
|
||||
void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t *a) {
|
||||
void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) {
|
||||
signed char e[64];
|
||||
signed char carry;
|
||||
ge_p1p1 r;
|
||||
|
||||
@@ -106,7 +106,7 @@ typedef struct {
|
||||
} ge_cached;
|
||||
|
||||
void x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h);
|
||||
int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t *s);
|
||||
int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]);
|
||||
void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
|
||||
void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
|
||||
void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
|
||||
|
||||
File renamed without changes.
+179
@@ -53,6 +53,8 @@
|
||||
#include <CCryptoBoringSSL_dh.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
#include "../fipsmodule/bn/internal.h"
|
||||
|
||||
@@ -91,3 +93,180 @@ BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *ret) {
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator,
|
||||
BN_GENCB *cb) {
|
||||
// We generate DH parameters as follows
|
||||
// find a prime q which is prime_bits/2 bits long.
|
||||
// p=(2*q)+1 or (p-1)/2 = q
|
||||
// For this case, g is a generator if
|
||||
// g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
|
||||
// Since the factors of p-1 are q and 2, we just need to check
|
||||
// g^2 mod p != 1 and g^q mod p != 1.
|
||||
//
|
||||
// Having said all that,
|
||||
// there is another special case method for the generators 2, 3 and 5.
|
||||
// for 2, p mod 24 == 11
|
||||
// for 3, p mod 12 == 5 <<<<< does not work for safe primes.
|
||||
// for 5, p mod 10 == 3 or 7
|
||||
//
|
||||
// Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
|
||||
// special generators and for answering some of my questions.
|
||||
//
|
||||
// I've implemented the second simple method :-).
|
||||
// Since DH should be using a safe prime (both p and q are prime),
|
||||
// this generator function can take a very very long time to run.
|
||||
|
||||
// Actually there is no reason to insist that 'generator' be a generator.
|
||||
// It's just as OK (and in some sense better) to use a generator of the
|
||||
// order-q subgroup.
|
||||
|
||||
BIGNUM *t1, *t2;
|
||||
int g, ok = 0;
|
||||
BN_CTX *ctx = NULL;
|
||||
|
||||
ctx = BN_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
goto err;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
t1 = BN_CTX_get(ctx);
|
||||
t2 = BN_CTX_get(ctx);
|
||||
if (t1 == NULL || t2 == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Make sure |dh| has the necessary elements
|
||||
if (dh->p == NULL) {
|
||||
dh->p = BN_new();
|
||||
if (dh->p == NULL) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
if (dh->g == NULL) {
|
||||
dh->g = BN_new();
|
||||
if (dh->g == NULL) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (generator <= 1) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
|
||||
goto err;
|
||||
}
|
||||
if (generator == DH_GENERATOR_2) {
|
||||
if (!BN_set_word(t1, 24)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 11)) {
|
||||
goto err;
|
||||
}
|
||||
g = 2;
|
||||
} else if (generator == DH_GENERATOR_5) {
|
||||
if (!BN_set_word(t1, 10)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 3)) {
|
||||
goto err;
|
||||
}
|
||||
// BN_set_word(t3,7); just have to miss
|
||||
// out on these ones :-(
|
||||
g = 5;
|
||||
} else {
|
||||
// in the general case, don't worry if 'generator' is a
|
||||
// generator or not: since we are using safe primes,
|
||||
// it will generate either an order-q or an order-2q group,
|
||||
// which both is OK
|
||||
if (!BN_set_word(t1, 2)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 1)) {
|
||||
goto err;
|
||||
}
|
||||
g = generator;
|
||||
}
|
||||
|
||||
if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_GENCB_call(cb, 3, 0)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(dh->g, g)) {
|
||||
goto err;
|
||||
}
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
|
||||
}
|
||||
|
||||
if (ctx != NULL) {
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
|
||||
BIGNUM *a = NULL;
|
||||
|
||||
if (src) {
|
||||
a = BN_dup(src);
|
||||
if (!a) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
BN_free(*dst);
|
||||
*dst = a;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
|
||||
if (is_x942 == -1) {
|
||||
is_x942 = !!from->q;
|
||||
}
|
||||
if (!int_dh_bn_cpy(&to->p, from->p) ||
|
||||
!int_dh_bn_cpy(&to->g, from->g)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!is_x942) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!int_dh_bn_cpy(&to->q, from->q) ||
|
||||
!int_dh_bn_cpy(&to->j, from->j)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_free(to->seed);
|
||||
to->seed = NULL;
|
||||
to->seedlen = 0;
|
||||
|
||||
if (from->seed) {
|
||||
to->seed = OPENSSL_memdup(from->seed, from->seedlen);
|
||||
if (!to->seed) {
|
||||
return 0;
|
||||
}
|
||||
to->seedlen = from->seedlen;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
DH *DHparams_dup(const DH *dh) {
|
||||
DH *ret = DH_new();
|
||||
if (!ret) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!int_dh_param_copy(ret, dh, -1)) {
|
||||
DH_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -58,11 +58,14 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_blake2.h>
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_nid.h>
|
||||
|
||||
#include "../asn1/internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../fipsmodule/digest/internal.h"
|
||||
|
||||
|
||||
struct nid_to_digest {
|
||||
@@ -80,6 +83,7 @@ static const struct nid_to_digest nid_to_digest_mapping[] = {
|
||||
{NID_sha256, EVP_sha256, SN_sha256, LN_sha256},
|
||||
{NID_sha384, EVP_sha384, SN_sha384, LN_sha384},
|
||||
{NID_sha512, EVP_sha512, SN_sha512, LN_sha512},
|
||||
{NID_sha512_256, EVP_sha512_256, SN_sha512_256, LN_sha512_256},
|
||||
{NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1},
|
||||
// As a remnant of signing |EVP_MD|s, OpenSSL returned the corresponding
|
||||
// hash function when given a signature OID. To avoid unintended lax parsing
|
||||
@@ -150,13 +154,14 @@ static const EVP_MD *cbs_to_md(const CBS *cbs) {
|
||||
}
|
||||
|
||||
const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj) {
|
||||
// Handle objects with no corresponding OID.
|
||||
// Handle objects with no corresponding OID. Note we don't use |OBJ_obj2nid|
|
||||
// here to avoid pulling in the OID table.
|
||||
if (obj->nid != NID_undef) {
|
||||
return EVP_get_digestbynid(obj->nid);
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, obj->data, obj->length);
|
||||
CBS_init(&cbs, OBJ_get0_data(obj), OBJ_length(obj));
|
||||
return cbs_to_md(&cbs);
|
||||
}
|
||||
|
||||
@@ -238,3 +243,26 @@ const EVP_MD *EVP_get_digestbyname(const char *name) {
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void blake2b256_init(EVP_MD_CTX *ctx) { BLAKE2B256_Init(ctx->md_data); }
|
||||
|
||||
static void blake2b256_update(EVP_MD_CTX *ctx, const void *data, size_t len) {
|
||||
BLAKE2B256_Update(ctx->md_data, data, len);
|
||||
}
|
||||
|
||||
static void blake2b256_final(EVP_MD_CTX *ctx, uint8_t *md) {
|
||||
BLAKE2B256_Final(md, ctx->md_data);
|
||||
}
|
||||
|
||||
static const EVP_MD evp_md_blake2b256 = {
|
||||
NID_undef,
|
||||
BLAKE2B256_DIGEST_LENGTH,
|
||||
0,
|
||||
blake2b256_init,
|
||||
blake2b256_update,
|
||||
blake2b256_final,
|
||||
BLAKE2B_CBLOCK,
|
||||
sizeof(BLAKE2B_CTX),
|
||||
};
|
||||
|
||||
const EVP_MD *EVP_blake2b256(void) { return &evp_md_blake2b256; }
|
||||
@@ -241,21 +241,6 @@ int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
// is_unsigned_integer returns one if |cbs| is a valid unsigned DER INTEGER and
|
||||
// zero otherwise.
|
||||
static int is_unsigned_integer(const CBS *cbs) {
|
||||
if (CBS_len(cbs) == 0) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t byte = CBS_data(cbs)[0];
|
||||
if ((byte & 0x80) ||
|
||||
(byte == 0 && CBS_len(cbs) > 1 && (CBS_data(cbs)[1] & 0x80) == 0)) {
|
||||
// Negative or not minimally-encoded.
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// kPrimeFieldOID is the encoding of 1.2.840.10045.1.1.
|
||||
static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01};
|
||||
|
||||
@@ -276,7 +261,7 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
|
||||
OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) !=
|
||||
0 ||
|
||||
!CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) ||
|
||||
!is_unsigned_integer(out_prime) ||
|
||||
!CBS_is_unsigned_asn1_integer(out_prime) ||
|
||||
CBS_len(&field_id) != 0 ||
|
||||
!CBS_get_asn1(¶ms, &curve, CBS_ASN1_SEQUENCE) ||
|
||||
!CBS_get_asn1(&curve, out_a, CBS_ASN1_OCTETSTRING) ||
|
||||
@@ -286,7 +271,7 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
|
||||
CBS_len(&curve) != 0 ||
|
||||
!CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) ||
|
||||
!CBS_get_asn1(¶ms, out_order, CBS_ASN1_INTEGER) ||
|
||||
!is_unsigned_integer(out_order) ||
|
||||
!CBS_is_unsigned_asn1_integer(out_order) ||
|
||||
!CBS_get_optional_asn1(¶ms, &cofactor, &has_cofactor,
|
||||
CBS_ASN1_INTEGER) ||
|
||||
CBS_len(¶ms) != 0) {
|
||||
|
||||
@@ -368,84 +368,6 @@ void ERR_clear_system_error(void) {
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
char *ERR_error_string(uint32_t packed_error, char *ret) {
|
||||
static char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
|
||||
if (ret == NULL) {
|
||||
// TODO(fork): remove this.
|
||||
ret = buf;
|
||||
}
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
// This is aimed to help catch callers who don't provide
|
||||
// |ERR_ERROR_STRING_BUF_LEN| bytes of space.
|
||||
OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
|
||||
#endif
|
||||
|
||||
return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
|
||||
}
|
||||
|
||||
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
|
||||
char lib_buf[64], reason_buf[64];
|
||||
const char *lib_str, *reason_str;
|
||||
unsigned lib, reason;
|
||||
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lib = ERR_GET_LIB(packed_error);
|
||||
reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
lib_str = ERR_lib_error_string(packed_error);
|
||||
reason_str = ERR_reason_error_string(packed_error);
|
||||
|
||||
if (lib_str == NULL) {
|
||||
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
|
||||
lib_str = lib_buf;
|
||||
}
|
||||
|
||||
if (reason_str == NULL) {
|
||||
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
|
||||
reason_str = reason_buf;
|
||||
}
|
||||
|
||||
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
|
||||
packed_error, lib_str, reason_str);
|
||||
|
||||
if (strlen(buf) == len - 1) {
|
||||
// output may be truncated; make sure we always have 5 colon-separated
|
||||
// fields, i.e. 4 colons.
|
||||
static const unsigned num_colons = 4;
|
||||
unsigned i;
|
||||
char *s = buf;
|
||||
|
||||
if (len <= num_colons) {
|
||||
// In this situation it's not possible to ensure that the correct number
|
||||
// of colons are included in the output.
|
||||
return buf;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_colons; i++) {
|
||||
char *colon = strchr(s, ':');
|
||||
char *last_pos = &buf[len - 1] - num_colons + i;
|
||||
|
||||
if (colon == NULL || colon > last_pos) {
|
||||
// set colon |i| at last possible position (buf[len-1] is the
|
||||
// terminating 0). If we're setting this colon, then all whole of the
|
||||
// rest of the string must be colons in order to have the correct
|
||||
// number.
|
||||
OPENSSL_memset(last_pos, ':', num_colons - i);
|
||||
break;
|
||||
}
|
||||
|
||||
s = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
// err_string_cmp is a compare function for searching error values with
|
||||
// |bsearch| in |err_string_lookup|.
|
||||
static int err_string_cmp(const void *a, const void *b) {
|
||||
@@ -530,7 +452,7 @@ static const char *const kLibraryNames[ERR_NUM_LIBS] = {
|
||||
"User defined functions", // ERR_LIB_USER
|
||||
};
|
||||
|
||||
const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
static const char *err_lib_error_string(uint32_t packed_error) {
|
||||
const uint32_t lib = ERR_GET_LIB(packed_error);
|
||||
|
||||
if (lib >= ERR_NUM_LIBS) {
|
||||
@@ -539,11 +461,16 @@ const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
return kLibraryNames[lib];
|
||||
}
|
||||
|
||||
const char *ERR_lib_error_string(uint32_t packed_error) {
|
||||
const char *ret = err_lib_error_string(packed_error);
|
||||
return ret == NULL ? "unknown library" : ret;
|
||||
}
|
||||
|
||||
const char *ERR_func_error_string(uint32_t packed_error) {
|
||||
return "OPENSSL_internal";
|
||||
}
|
||||
|
||||
const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
static const char *err_reason_error_string(uint32_t packed_error) {
|
||||
const uint32_t lib = ERR_GET_LIB(packed_error);
|
||||
const uint32_t reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
@@ -579,6 +506,86 @@ const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
|
||||
}
|
||||
|
||||
const char *ERR_reason_error_string(uint32_t packed_error) {
|
||||
const char *ret = err_reason_error_string(packed_error);
|
||||
return ret == NULL ? "unknown error" : ret;
|
||||
}
|
||||
|
||||
char *ERR_error_string(uint32_t packed_error, char *ret) {
|
||||
static char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
|
||||
if (ret == NULL) {
|
||||
// TODO(fork): remove this.
|
||||
ret = buf;
|
||||
}
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
// This is aimed to help catch callers who don't provide
|
||||
// |ERR_ERROR_STRING_BUF_LEN| bytes of space.
|
||||
OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
|
||||
#endif
|
||||
|
||||
return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
|
||||
}
|
||||
|
||||
char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned lib = ERR_GET_LIB(packed_error);
|
||||
unsigned reason = ERR_GET_REASON(packed_error);
|
||||
|
||||
const char *lib_str = err_lib_error_string(packed_error);
|
||||
const char *reason_str = err_reason_error_string(packed_error);
|
||||
|
||||
char lib_buf[64], reason_buf[64];
|
||||
if (lib_str == NULL) {
|
||||
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
|
||||
lib_str = lib_buf;
|
||||
}
|
||||
|
||||
if (reason_str == NULL) {
|
||||
BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
|
||||
reason_str = reason_buf;
|
||||
}
|
||||
|
||||
BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
|
||||
packed_error, lib_str, reason_str);
|
||||
|
||||
if (strlen(buf) == len - 1) {
|
||||
// output may be truncated; make sure we always have 5 colon-separated
|
||||
// fields, i.e. 4 colons.
|
||||
static const unsigned num_colons = 4;
|
||||
unsigned i;
|
||||
char *s = buf;
|
||||
|
||||
if (len <= num_colons) {
|
||||
// In this situation it's not possible to ensure that the correct number
|
||||
// of colons are included in the output.
|
||||
return buf;
|
||||
}
|
||||
|
||||
for (i = 0; i < num_colons; i++) {
|
||||
char *colon = strchr(s, ':');
|
||||
char *last_pos = &buf[len - 1] - num_colons + i;
|
||||
|
||||
if (colon == NULL || colon > last_pos) {
|
||||
// set colon |i| at last possible position (buf[len-1] is the
|
||||
// terminating 0). If we're setting this colon, then all whole of the
|
||||
// rest of the string must be colons in order to have the correct
|
||||
// number.
|
||||
OPENSSL_memset(last_pos, ':', num_colons - i);
|
||||
break;
|
||||
}
|
||||
|
||||
s = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
|
||||
char buf[ERR_ERROR_STRING_BUF_LEN];
|
||||
char buf2[1024];
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -429,6 +429,15 @@ int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
|
||||
0, (void *)out_md);
|
||||
}
|
||||
|
||||
void *EVP_PKEY_get0(const EVP_PKEY *pkey) {
|
||||
// Node references, but never calls this function, so for now we return NULL.
|
||||
// If other projects require complete support, call |EVP_PKEY_get0_RSA|, etc.,
|
||||
// rather than reading |pkey->pkey.ptr| directly. This avoids problems if our
|
||||
// internal representation does not match the type the caller expects from
|
||||
// OpenSSL.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void OpenSSL_add_all_algorithms(void) {}
|
||||
|
||||
void OPENSSL_add_all_algorithms_conf(void) {}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -250,6 +250,7 @@ Ldec_key_abort:
|
||||
#endif
|
||||
.align 5
|
||||
_aes_hw_encrypt:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
ldr r3,[r2,#240]
|
||||
vld1.32 {q0},[r2]!
|
||||
vld1.8 {q2},[r0]
|
||||
@@ -282,6 +283,7 @@ Loop_enc:
|
||||
#endif
|
||||
.align 5
|
||||
_aes_hw_decrypt:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
ldr r3,[r2,#240]
|
||||
vld1.32 {q0},[r2]!
|
||||
vld1.8 {q2},[r0]
|
||||
@@ -630,20 +632,34 @@ _aes_hw_ctr32_encrypt_blocks:
|
||||
add r7,r3,#32
|
||||
mov r6,r5
|
||||
movlo r12,#0
|
||||
|
||||
@ ARM Cortex-A57 and Cortex-A72 cores running in 32-bit mode are
|
||||
@ affected by silicon errata #1742098 [0] and #1655431 [1],
|
||||
@ respectively, where the second instruction of an aese/aesmc
|
||||
@ instruction pair may execute twice if an interrupt is taken right
|
||||
@ after the first instruction consumes an input register of which a
|
||||
@ single 32-bit lane has been updated the last time it was modified.
|
||||
@
|
||||
@ This function uses a counter in one 32-bit lane. The
|
||||
@ could write to q1 and q10 directly, but that trips this bugs.
|
||||
@ We write to q6 and copy to the final register as a workaround.
|
||||
@
|
||||
@ [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
@ [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
rev r8, r8
|
||||
#endif
|
||||
vorr q1,q0,q0
|
||||
add r10, r8, #1
|
||||
vorr q10,q0,q0
|
||||
add r8, r8, #2
|
||||
vorr q6,q0,q0
|
||||
rev r10, r10
|
||||
vmov.32 d3[1],r10
|
||||
vmov.32 d13[1],r10
|
||||
add r8, r8, #2
|
||||
vorr q1,q6,q6
|
||||
bls Lctr32_tail
|
||||
rev r12, r8
|
||||
vmov.32 d13[1],r12
|
||||
sub r2,r2,#3 @ bias
|
||||
vmov.32 d21[1],r12
|
||||
vorr q10,q6,q6
|
||||
b Loop3x_ctr32
|
||||
|
||||
.align 4
|
||||
@@ -670,11 +686,11 @@ Loop3x_ctr32:
|
||||
.byte 0x20,0x23,0xb0,0xf3 @ aese q1,q8
|
||||
.byte 0x82,0xa3,0xb0,0xf3 @ aesmc q5,q1
|
||||
vld1.8 {q2},[r0]!
|
||||
vorr q0,q6,q6
|
||||
add r9,r8,#1
|
||||
.byte 0x20,0x43,0xf0,0xf3 @ aese q10,q8
|
||||
.byte 0xa4,0x43,0xf0,0xf3 @ aesmc q10,q10
|
||||
vld1.8 {q3},[r0]!
|
||||
vorr q1,q6,q6
|
||||
rev r9,r9
|
||||
.byte 0x22,0x83,0xb0,0xf3 @ aese q4,q9
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x22,0xa3,0xb0,0xf3 @ aese q5,q9
|
||||
@@ -683,8 +699,6 @@ Loop3x_ctr32:
|
||||
mov r7,r3
|
||||
.byte 0x22,0x43,0xf0,0xf3 @ aese q10,q9
|
||||
.byte 0xa4,0x23,0xf0,0xf3 @ aesmc q9,q10
|
||||
vorr q10,q6,q6
|
||||
add r9,r8,#1
|
||||
.byte 0x28,0x83,0xb0,0xf3 @ aese q4,q12
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x28,0xa3,0xb0,0xf3 @ aese q5,q12
|
||||
@@ -699,21 +713,26 @@ Loop3x_ctr32:
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x2a,0xa3,0xb0,0xf3 @ aese q5,q13
|
||||
.byte 0x8a,0xa3,0xb0,0xf3 @ aesmc q5,q5
|
||||
@ Note the logic to update q0, q1, and q1 is written to work
|
||||
@ around a bug in ARM Cortex-A57 and Cortex-A72 cores running in
|
||||
@ 32-bit mode. See the comment above.
|
||||
veor q11,q11,q7
|
||||
rev r9,r9
|
||||
vmov.32 d13[1], r9
|
||||
.byte 0x2a,0x23,0xf0,0xf3 @ aese q9,q13
|
||||
.byte 0xa2,0x23,0xf0,0xf3 @ aesmc q9,q9
|
||||
vmov.32 d1[1], r9
|
||||
vorr q0,q6,q6
|
||||
rev r10,r10
|
||||
.byte 0x2c,0x83,0xb0,0xf3 @ aese q4,q14
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
vmov.32 d13[1], r10
|
||||
rev r12,r8
|
||||
.byte 0x2c,0xa3,0xb0,0xf3 @ aese q5,q14
|
||||
.byte 0x8a,0xa3,0xb0,0xf3 @ aesmc q5,q5
|
||||
vmov.32 d3[1], r10
|
||||
rev r12,r8
|
||||
vorr q1,q6,q6
|
||||
vmov.32 d13[1], r12
|
||||
.byte 0x2c,0x23,0xf0,0xf3 @ aese q9,q14
|
||||
.byte 0xa2,0x23,0xf0,0xf3 @ aesmc q9,q9
|
||||
vmov.32 d21[1], r12
|
||||
vorr q10,q6,q6
|
||||
subs r2,r2,#3
|
||||
.byte 0x2e,0x83,0xb0,0xf3 @ aese q4,q15
|
||||
.byte 0x2e,0xa3,0xb0,0xf3 @ aese q5,q15
|
||||
|
||||
@@ -245,6 +245,7 @@ aes_hw_set_decrypt_key:
|
||||
.type aes_hw_encrypt,%function
|
||||
.align 5
|
||||
aes_hw_encrypt:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
ldr r3,[r2,#240]
|
||||
vld1.32 {q0},[r2]!
|
||||
vld1.8 {q2},[r0]
|
||||
@@ -275,6 +276,7 @@ aes_hw_encrypt:
|
||||
.type aes_hw_decrypt,%function
|
||||
.align 5
|
||||
aes_hw_decrypt:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
ldr r3,[r2,#240]
|
||||
vld1.32 {q0},[r2]!
|
||||
vld1.8 {q2},[r0]
|
||||
@@ -619,20 +621,34 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
add r7,r3,#32
|
||||
mov r6,r5
|
||||
movlo r12,#0
|
||||
|
||||
@ ARM Cortex-A57 and Cortex-A72 cores running in 32-bit mode are
|
||||
@ affected by silicon errata #1742098 [0] and #1655431 [1],
|
||||
@ respectively, where the second instruction of an aese/aesmc
|
||||
@ instruction pair may execute twice if an interrupt is taken right
|
||||
@ after the first instruction consumes an input register of which a
|
||||
@ single 32-bit lane has been updated the last time it was modified.
|
||||
@
|
||||
@ This function uses a counter in one 32-bit lane. The
|
||||
@ could write to q1 and q10 directly, but that trips this bugs.
|
||||
@ We write to q6 and copy to the final register as a workaround.
|
||||
@
|
||||
@ [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
@ [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
rev r8, r8
|
||||
#endif
|
||||
vorr q1,q0,q0
|
||||
add r10, r8, #1
|
||||
vorr q10,q0,q0
|
||||
add r8, r8, #2
|
||||
vorr q6,q0,q0
|
||||
rev r10, r10
|
||||
vmov.32 d3[1],r10
|
||||
vmov.32 d13[1],r10
|
||||
add r8, r8, #2
|
||||
vorr q1,q6,q6
|
||||
bls .Lctr32_tail
|
||||
rev r12, r8
|
||||
vmov.32 d13[1],r12
|
||||
sub r2,r2,#3 @ bias
|
||||
vmov.32 d21[1],r12
|
||||
vorr q10,q6,q6
|
||||
b .Loop3x_ctr32
|
||||
|
||||
.align 4
|
||||
@@ -659,11 +675,11 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
.byte 0x20,0x23,0xb0,0xf3 @ aese q1,q8
|
||||
.byte 0x82,0xa3,0xb0,0xf3 @ aesmc q5,q1
|
||||
vld1.8 {q2},[r0]!
|
||||
vorr q0,q6,q6
|
||||
add r9,r8,#1
|
||||
.byte 0x20,0x43,0xf0,0xf3 @ aese q10,q8
|
||||
.byte 0xa4,0x43,0xf0,0xf3 @ aesmc q10,q10
|
||||
vld1.8 {q3},[r0]!
|
||||
vorr q1,q6,q6
|
||||
rev r9,r9
|
||||
.byte 0x22,0x83,0xb0,0xf3 @ aese q4,q9
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x22,0xa3,0xb0,0xf3 @ aese q5,q9
|
||||
@@ -672,8 +688,6 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
mov r7,r3
|
||||
.byte 0x22,0x43,0xf0,0xf3 @ aese q10,q9
|
||||
.byte 0xa4,0x23,0xf0,0xf3 @ aesmc q9,q10
|
||||
vorr q10,q6,q6
|
||||
add r9,r8,#1
|
||||
.byte 0x28,0x83,0xb0,0xf3 @ aese q4,q12
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x28,0xa3,0xb0,0xf3 @ aese q5,q12
|
||||
@@ -688,21 +702,26 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
.byte 0x2a,0xa3,0xb0,0xf3 @ aese q5,q13
|
||||
.byte 0x8a,0xa3,0xb0,0xf3 @ aesmc q5,q5
|
||||
@ Note the logic to update q0, q1, and q1 is written to work
|
||||
@ around a bug in ARM Cortex-A57 and Cortex-A72 cores running in
|
||||
@ 32-bit mode. See the comment above.
|
||||
veor q11,q11,q7
|
||||
rev r9,r9
|
||||
vmov.32 d13[1], r9
|
||||
.byte 0x2a,0x23,0xf0,0xf3 @ aese q9,q13
|
||||
.byte 0xa2,0x23,0xf0,0xf3 @ aesmc q9,q9
|
||||
vmov.32 d1[1], r9
|
||||
vorr q0,q6,q6
|
||||
rev r10,r10
|
||||
.byte 0x2c,0x83,0xb0,0xf3 @ aese q4,q14
|
||||
.byte 0x88,0x83,0xb0,0xf3 @ aesmc q4,q4
|
||||
vmov.32 d13[1], r10
|
||||
rev r12,r8
|
||||
.byte 0x2c,0xa3,0xb0,0xf3 @ aese q5,q14
|
||||
.byte 0x8a,0xa3,0xb0,0xf3 @ aesmc q5,q5
|
||||
vmov.32 d3[1], r10
|
||||
rev r12,r8
|
||||
vorr q1,q6,q6
|
||||
vmov.32 d13[1], r12
|
||||
.byte 0x2c,0x23,0xf0,0xf3 @ aese q9,q14
|
||||
.byte 0xa2,0x23,0xf0,0xf3 @ aesmc q9,q9
|
||||
vmov.32 d21[1], r12
|
||||
vorr q10,q6,q6
|
||||
subs r2,r2,#3
|
||||
.byte 0x2e,0x83,0xb0,0xf3 @ aese q4,q15
|
||||
.byte 0x2e,0xa3,0xb0,0xf3 @ aese q5,q15
|
||||
|
||||
@@ -622,20 +622,34 @@ _aes_hw_ctr32_encrypt_blocks:
|
||||
add x7,x3,#32
|
||||
mov w6,w5
|
||||
csel x12,xzr,x12,lo
|
||||
|
||||
// ARM Cortex-A57 and Cortex-A72 cores running in 32-bit mode are
|
||||
// affected by silicon errata #1742098 [0] and #1655431 [1],
|
||||
// respectively, where the second instruction of an aese/aesmc
|
||||
// instruction pair may execute twice if an interrupt is taken right
|
||||
// after the first instruction consumes an input register of which a
|
||||
// single 32-bit lane has been updated the last time it was modified.
|
||||
//
|
||||
// This function uses a counter in one 32-bit lane. The vmov lines
|
||||
// could write to v1.16b and v18.16b directly, but that trips this bugs.
|
||||
// We write to v6.16b and copy to the final register as a workaround.
|
||||
//
|
||||
// [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
// [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
rev w8, w8
|
||||
#endif
|
||||
orr v1.16b,v0.16b,v0.16b
|
||||
add w10, w8, #1
|
||||
orr v18.16b,v0.16b,v0.16b
|
||||
add w8, w8, #2
|
||||
orr v6.16b,v0.16b,v0.16b
|
||||
rev w10, w10
|
||||
mov v1.s[3],w10
|
||||
mov v6.s[3],w10
|
||||
add w8, w8, #2
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
b.ls Lctr32_tail
|
||||
rev w12, w8
|
||||
mov v6.s[3],w12
|
||||
sub x2,x2,#3 // bias
|
||||
mov v18.s[3],w12
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
b Loop3x_ctr32
|
||||
|
||||
.align 4
|
||||
@@ -662,11 +676,11 @@ Loop3x_ctr32:
|
||||
aese v1.16b,v16.16b
|
||||
aesmc v5.16b,v1.16b
|
||||
ld1 {v2.16b},[x0],#16
|
||||
orr v0.16b,v6.16b,v6.16b
|
||||
add w9,w8,#1
|
||||
aese v18.16b,v16.16b
|
||||
aesmc v18.16b,v18.16b
|
||||
ld1 {v3.16b},[x0],#16
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
rev w9,w9
|
||||
aese v4.16b,v17.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v17.16b
|
||||
@@ -675,8 +689,6 @@ Loop3x_ctr32:
|
||||
mov x7,x3
|
||||
aese v18.16b,v17.16b
|
||||
aesmc v17.16b,v18.16b
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
add w9,w8,#1
|
||||
aese v4.16b,v20.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v20.16b
|
||||
@@ -691,21 +703,26 @@ Loop3x_ctr32:
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v21.16b
|
||||
aesmc v5.16b,v5.16b
|
||||
// Note the logic to update v0.16b, v1.16b, and v1.16b is written to work
|
||||
// around a bug in ARM Cortex-A57 and Cortex-A72 cores running in
|
||||
// 32-bit mode. See the comment above.
|
||||
eor v19.16b,v19.16b,v7.16b
|
||||
rev w9,w9
|
||||
mov v6.s[3], w9
|
||||
aese v17.16b,v21.16b
|
||||
aesmc v17.16b,v17.16b
|
||||
mov v0.s[3], w9
|
||||
orr v0.16b,v6.16b,v6.16b
|
||||
rev w10,w10
|
||||
aese v4.16b,v22.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
mov v6.s[3], w10
|
||||
rev w12,w8
|
||||
aese v5.16b,v22.16b
|
||||
aesmc v5.16b,v5.16b
|
||||
mov v1.s[3], w10
|
||||
rev w12,w8
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
mov v6.s[3], w12
|
||||
aese v17.16b,v22.16b
|
||||
aesmc v17.16b,v17.16b
|
||||
mov v18.s[3], w12
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
subs x2,x2,#3
|
||||
aese v4.16b,v23.16b
|
||||
aese v5.16b,v23.16b
|
||||
|
||||
@@ -623,20 +623,34 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
add x7,x3,#32
|
||||
mov w6,w5
|
||||
csel x12,xzr,x12,lo
|
||||
|
||||
// ARM Cortex-A57 and Cortex-A72 cores running in 32-bit mode are
|
||||
// affected by silicon errata #1742098 [0] and #1655431 [1],
|
||||
// respectively, where the second instruction of an aese/aesmc
|
||||
// instruction pair may execute twice if an interrupt is taken right
|
||||
// after the first instruction consumes an input register of which a
|
||||
// single 32-bit lane has been updated the last time it was modified.
|
||||
//
|
||||
// This function uses a counter in one 32-bit lane. The vmov lines
|
||||
// could write to v1.16b and v18.16b directly, but that trips this bugs.
|
||||
// We write to v6.16b and copy to the final register as a workaround.
|
||||
//
|
||||
// [0] ARM-EPM-049219 v23 Cortex-A57 MPCore Software Developers Errata Notice
|
||||
// [1] ARM-EPM-012079 v11.0 Cortex-A72 MPCore Software Developers Errata Notice
|
||||
#ifndef __ARMEB__
|
||||
rev w8, w8
|
||||
#endif
|
||||
orr v1.16b,v0.16b,v0.16b
|
||||
add w10, w8, #1
|
||||
orr v18.16b,v0.16b,v0.16b
|
||||
add w8, w8, #2
|
||||
orr v6.16b,v0.16b,v0.16b
|
||||
rev w10, w10
|
||||
mov v1.s[3],w10
|
||||
mov v6.s[3],w10
|
||||
add w8, w8, #2
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
b.ls .Lctr32_tail
|
||||
rev w12, w8
|
||||
mov v6.s[3],w12
|
||||
sub x2,x2,#3 // bias
|
||||
mov v18.s[3],w12
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
b .Loop3x_ctr32
|
||||
|
||||
.align 4
|
||||
@@ -663,11 +677,11 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
aese v1.16b,v16.16b
|
||||
aesmc v5.16b,v1.16b
|
||||
ld1 {v2.16b},[x0],#16
|
||||
orr v0.16b,v6.16b,v6.16b
|
||||
add w9,w8,#1
|
||||
aese v18.16b,v16.16b
|
||||
aesmc v18.16b,v18.16b
|
||||
ld1 {v3.16b},[x0],#16
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
rev w9,w9
|
||||
aese v4.16b,v17.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v17.16b
|
||||
@@ -676,8 +690,6 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
mov x7,x3
|
||||
aese v18.16b,v17.16b
|
||||
aesmc v17.16b,v18.16b
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
add w9,w8,#1
|
||||
aese v4.16b,v20.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v20.16b
|
||||
@@ -692,21 +704,26 @@ aes_hw_ctr32_encrypt_blocks:
|
||||
aesmc v4.16b,v4.16b
|
||||
aese v5.16b,v21.16b
|
||||
aesmc v5.16b,v5.16b
|
||||
// Note the logic to update v0.16b, v1.16b, and v1.16b is written to work
|
||||
// around a bug in ARM Cortex-A57 and Cortex-A72 cores running in
|
||||
// 32-bit mode. See the comment above.
|
||||
eor v19.16b,v19.16b,v7.16b
|
||||
rev w9,w9
|
||||
mov v6.s[3], w9
|
||||
aese v17.16b,v21.16b
|
||||
aesmc v17.16b,v17.16b
|
||||
mov v0.s[3], w9
|
||||
orr v0.16b,v6.16b,v6.16b
|
||||
rev w10,w10
|
||||
aese v4.16b,v22.16b
|
||||
aesmc v4.16b,v4.16b
|
||||
mov v6.s[3], w10
|
||||
rev w12,w8
|
||||
aese v5.16b,v22.16b
|
||||
aesmc v5.16b,v5.16b
|
||||
mov v1.s[3], w10
|
||||
rev w12,w8
|
||||
orr v1.16b,v6.16b,v6.16b
|
||||
mov v6.s[3], w12
|
||||
aese v17.16b,v22.16b
|
||||
aesmc v17.16b,v17.16b
|
||||
mov v18.s[3], w12
|
||||
orr v18.16b,v6.16b,v6.16b
|
||||
subs x2,x2,#3
|
||||
aese v4.16b,v23.16b
|
||||
aese v5.16b,v23.16b
|
||||
|
||||
@@ -0,0 +1,852 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.text
|
||||
.globl _bn_mul_add_words
|
||||
.hidden _bn_mul_add_words
|
||||
.def _bn_mul_add_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_mul_add_words:
|
||||
.L_bn_mul_add_words_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
|
||||
xorl %esi,%esi
|
||||
movl 20(%esp),%edi
|
||||
movl 28(%esp),%ecx
|
||||
movl 24(%esp),%ebx
|
||||
andl $4294967288,%ecx
|
||||
movl 32(%esp),%ebp
|
||||
pushl %ecx
|
||||
jz .L000maw_finish
|
||||
.align 16
|
||||
.L001maw_loop:
|
||||
# Round 0
|
||||
movl (%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl (%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 4
|
||||
movl 4(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 4(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,4(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 8
|
||||
movl 8(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 8(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,8(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 12
|
||||
movl 12(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 12(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,12(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 16
|
||||
movl 16(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 16(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,16(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 20
|
||||
movl 20(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 20(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,20(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 24
|
||||
movl 24(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 24(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,24(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 28
|
||||
movl 28(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 28(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,28(%edi)
|
||||
movl %edx,%esi
|
||||
|
||||
subl $8,%ecx
|
||||
leal 32(%ebx),%ebx
|
||||
leal 32(%edi),%edi
|
||||
jnz .L001maw_loop
|
||||
.L000maw_finish:
|
||||
movl 32(%esp),%ecx
|
||||
andl $7,%ecx
|
||||
jnz .L002maw_finish2
|
||||
jmp .L003maw_end
|
||||
.L002maw_finish2:
|
||||
# Tail Round 0
|
||||
movl (%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl (%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 1
|
||||
movl 4(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 4(%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,4(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 2
|
||||
movl 8(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 8(%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,8(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 3
|
||||
movl 12(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 12(%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,12(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 4
|
||||
movl 16(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 16(%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,16(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 5
|
||||
movl 20(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 20(%edi),%eax
|
||||
adcl $0,%edx
|
||||
decl %ecx
|
||||
movl %eax,20(%edi)
|
||||
movl %edx,%esi
|
||||
jz .L003maw_end
|
||||
# Tail Round 6
|
||||
movl 24(%ebx),%eax
|
||||
mull %ebp
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
addl 24(%edi),%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,24(%edi)
|
||||
movl %edx,%esi
|
||||
.L003maw_end:
|
||||
movl %esi,%eax
|
||||
popl %ecx
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _bn_mul_words
|
||||
.hidden _bn_mul_words
|
||||
.def _bn_mul_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_mul_words:
|
||||
.L_bn_mul_words_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
|
||||
xorl %esi,%esi
|
||||
movl 20(%esp),%edi
|
||||
movl 24(%esp),%ebx
|
||||
movl 28(%esp),%ebp
|
||||
movl 32(%esp),%ecx
|
||||
andl $4294967288,%ebp
|
||||
jz .L004mw_finish
|
||||
.L005mw_loop:
|
||||
# Round 0
|
||||
movl (%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 4
|
||||
movl 4(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,4(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 8
|
||||
movl 8(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,8(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 12
|
||||
movl 12(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,12(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 16
|
||||
movl 16(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,16(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 20
|
||||
movl 20(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,20(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 24
|
||||
movl 24(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,24(%edi)
|
||||
movl %edx,%esi
|
||||
# Round 28
|
||||
movl 28(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,28(%edi)
|
||||
movl %edx,%esi
|
||||
|
||||
addl $32,%ebx
|
||||
addl $32,%edi
|
||||
subl $8,%ebp
|
||||
jz .L004mw_finish
|
||||
jmp .L005mw_loop
|
||||
.L004mw_finish:
|
||||
movl 28(%esp),%ebp
|
||||
andl $7,%ebp
|
||||
jnz .L006mw_finish2
|
||||
jmp .L007mw_end
|
||||
.L006mw_finish2:
|
||||
# Tail Round 0
|
||||
movl (%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 1
|
||||
movl 4(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,4(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 2
|
||||
movl 8(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,8(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 3
|
||||
movl 12(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,12(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 4
|
||||
movl 16(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,16(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 5
|
||||
movl 20(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,20(%edi)
|
||||
movl %edx,%esi
|
||||
decl %ebp
|
||||
jz .L007mw_end
|
||||
# Tail Round 6
|
||||
movl 24(%ebx),%eax
|
||||
mull %ecx
|
||||
addl %esi,%eax
|
||||
adcl $0,%edx
|
||||
movl %eax,24(%edi)
|
||||
movl %edx,%esi
|
||||
.L007mw_end:
|
||||
movl %esi,%eax
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _bn_sqr_words
|
||||
.hidden _bn_sqr_words
|
||||
.def _bn_sqr_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_sqr_words:
|
||||
.L_bn_sqr_words_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
|
||||
movl 20(%esp),%esi
|
||||
movl 24(%esp),%edi
|
||||
movl 28(%esp),%ebx
|
||||
andl $4294967288,%ebx
|
||||
jz .L008sw_finish
|
||||
.L009sw_loop:
|
||||
# Round 0
|
||||
movl (%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,(%esi)
|
||||
movl %edx,4(%esi)
|
||||
# Round 4
|
||||
movl 4(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,8(%esi)
|
||||
movl %edx,12(%esi)
|
||||
# Round 8
|
||||
movl 8(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,16(%esi)
|
||||
movl %edx,20(%esi)
|
||||
# Round 12
|
||||
movl 12(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,24(%esi)
|
||||
movl %edx,28(%esi)
|
||||
# Round 16
|
||||
movl 16(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,32(%esi)
|
||||
movl %edx,36(%esi)
|
||||
# Round 20
|
||||
movl 20(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,40(%esi)
|
||||
movl %edx,44(%esi)
|
||||
# Round 24
|
||||
movl 24(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,48(%esi)
|
||||
movl %edx,52(%esi)
|
||||
# Round 28
|
||||
movl 28(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,56(%esi)
|
||||
movl %edx,60(%esi)
|
||||
|
||||
addl $32,%edi
|
||||
addl $64,%esi
|
||||
subl $8,%ebx
|
||||
jnz .L009sw_loop
|
||||
.L008sw_finish:
|
||||
movl 28(%esp),%ebx
|
||||
andl $7,%ebx
|
||||
jz .L010sw_end
|
||||
# Tail Round 0
|
||||
movl (%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,(%esi)
|
||||
decl %ebx
|
||||
movl %edx,4(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 1
|
||||
movl 4(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,8(%esi)
|
||||
decl %ebx
|
||||
movl %edx,12(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 2
|
||||
movl 8(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,16(%esi)
|
||||
decl %ebx
|
||||
movl %edx,20(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 3
|
||||
movl 12(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,24(%esi)
|
||||
decl %ebx
|
||||
movl %edx,28(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 4
|
||||
movl 16(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,32(%esi)
|
||||
decl %ebx
|
||||
movl %edx,36(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 5
|
||||
movl 20(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,40(%esi)
|
||||
decl %ebx
|
||||
movl %edx,44(%esi)
|
||||
jz .L010sw_end
|
||||
# Tail Round 6
|
||||
movl 24(%edi),%eax
|
||||
mull %eax
|
||||
movl %eax,48(%esi)
|
||||
movl %edx,52(%esi)
|
||||
.L010sw_end:
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _bn_div_words
|
||||
.hidden _bn_div_words
|
||||
.def _bn_div_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_div_words:
|
||||
.L_bn_div_words_begin:
|
||||
movl 4(%esp),%edx
|
||||
movl 8(%esp),%eax
|
||||
movl 12(%esp),%ecx
|
||||
divl %ecx
|
||||
ret
|
||||
.globl _bn_add_words
|
||||
.hidden _bn_add_words
|
||||
.def _bn_add_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_add_words:
|
||||
.L_bn_add_words_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
|
||||
movl 20(%esp),%ebx
|
||||
movl 24(%esp),%esi
|
||||
movl 28(%esp),%edi
|
||||
movl 32(%esp),%ebp
|
||||
xorl %eax,%eax
|
||||
andl $4294967288,%ebp
|
||||
jz .L011aw_finish
|
||||
.L012aw_loop:
|
||||
# Round 0
|
||||
movl (%esi),%ecx
|
||||
movl (%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,(%ebx)
|
||||
# Round 1
|
||||
movl 4(%esi),%ecx
|
||||
movl 4(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,4(%ebx)
|
||||
# Round 2
|
||||
movl 8(%esi),%ecx
|
||||
movl 8(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,8(%ebx)
|
||||
# Round 3
|
||||
movl 12(%esi),%ecx
|
||||
movl 12(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,12(%ebx)
|
||||
# Round 4
|
||||
movl 16(%esi),%ecx
|
||||
movl 16(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,16(%ebx)
|
||||
# Round 5
|
||||
movl 20(%esi),%ecx
|
||||
movl 20(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,20(%ebx)
|
||||
# Round 6
|
||||
movl 24(%esi),%ecx
|
||||
movl 24(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,24(%ebx)
|
||||
# Round 7
|
||||
movl 28(%esi),%ecx
|
||||
movl 28(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,28(%ebx)
|
||||
|
||||
addl $32,%esi
|
||||
addl $32,%edi
|
||||
addl $32,%ebx
|
||||
subl $8,%ebp
|
||||
jnz .L012aw_loop
|
||||
.L011aw_finish:
|
||||
movl 32(%esp),%ebp
|
||||
andl $7,%ebp
|
||||
jz .L013aw_end
|
||||
# Tail Round 0
|
||||
movl (%esi),%ecx
|
||||
movl (%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 1
|
||||
movl 4(%esi),%ecx
|
||||
movl 4(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,4(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 2
|
||||
movl 8(%esi),%ecx
|
||||
movl 8(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,8(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 3
|
||||
movl 12(%esi),%ecx
|
||||
movl 12(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,12(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 4
|
||||
movl 16(%esi),%ecx
|
||||
movl 16(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,16(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 5
|
||||
movl 20(%esi),%ecx
|
||||
movl 20(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,20(%ebx)
|
||||
jz .L013aw_end
|
||||
# Tail Round 6
|
||||
movl 24(%esi),%ecx
|
||||
movl 24(%edi),%edx
|
||||
addl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
addl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,24(%ebx)
|
||||
.L013aw_end:
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _bn_sub_words
|
||||
.hidden _bn_sub_words
|
||||
.def _bn_sub_words; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_bn_sub_words:
|
||||
.L_bn_sub_words_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
|
||||
movl 20(%esp),%ebx
|
||||
movl 24(%esp),%esi
|
||||
movl 28(%esp),%edi
|
||||
movl 32(%esp),%ebp
|
||||
xorl %eax,%eax
|
||||
andl $4294967288,%ebp
|
||||
jz .L014aw_finish
|
||||
.L015aw_loop:
|
||||
# Round 0
|
||||
movl (%esi),%ecx
|
||||
movl (%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,(%ebx)
|
||||
# Round 1
|
||||
movl 4(%esi),%ecx
|
||||
movl 4(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,4(%ebx)
|
||||
# Round 2
|
||||
movl 8(%esi),%ecx
|
||||
movl 8(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,8(%ebx)
|
||||
# Round 3
|
||||
movl 12(%esi),%ecx
|
||||
movl 12(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,12(%ebx)
|
||||
# Round 4
|
||||
movl 16(%esi),%ecx
|
||||
movl 16(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,16(%ebx)
|
||||
# Round 5
|
||||
movl 20(%esi),%ecx
|
||||
movl 20(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,20(%ebx)
|
||||
# Round 6
|
||||
movl 24(%esi),%ecx
|
||||
movl 24(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,24(%ebx)
|
||||
# Round 7
|
||||
movl 28(%esi),%ecx
|
||||
movl 28(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,28(%ebx)
|
||||
|
||||
addl $32,%esi
|
||||
addl $32,%edi
|
||||
addl $32,%ebx
|
||||
subl $8,%ebp
|
||||
jnz .L015aw_loop
|
||||
.L014aw_finish:
|
||||
movl 32(%esp),%ebp
|
||||
andl $7,%ebp
|
||||
jz .L016aw_end
|
||||
# Tail Round 0
|
||||
movl (%esi),%ecx
|
||||
movl (%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 1
|
||||
movl 4(%esi),%ecx
|
||||
movl 4(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,4(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 2
|
||||
movl 8(%esi),%ecx
|
||||
movl 8(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,8(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 3
|
||||
movl 12(%esi),%ecx
|
||||
movl 12(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,12(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 4
|
||||
movl 16(%esi),%ecx
|
||||
movl 16(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,16(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 5
|
||||
movl 20(%esi),%ecx
|
||||
movl 20(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
decl %ebp
|
||||
movl %ecx,20(%ebx)
|
||||
jz .L016aw_end
|
||||
# Tail Round 6
|
||||
movl 24(%esi),%ecx
|
||||
movl 24(%edi),%edx
|
||||
subl %eax,%ecx
|
||||
movl $0,%eax
|
||||
adcl %eax,%eax
|
||||
subl %edx,%ecx
|
||||
adcl $0,%eax
|
||||
movl %ecx,24(%ebx)
|
||||
.L016aw_end:
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
#endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
@@ -101,26 +101,7 @@ void BN_free(BIGNUM *bn) {
|
||||
}
|
||||
|
||||
void BN_clear_free(BIGNUM *bn) {
|
||||
char should_free;
|
||||
|
||||
if (bn == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bn->d != NULL) {
|
||||
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
|
||||
OPENSSL_free(bn->d);
|
||||
} else {
|
||||
OPENSSL_cleanse(bn->d, bn->dmax * sizeof(bn->d[0]));
|
||||
}
|
||||
}
|
||||
|
||||
should_free = (bn->flags & BN_FLG_MALLOCED) != 0;
|
||||
if (should_free) {
|
||||
OPENSSL_free(bn);
|
||||
} else {
|
||||
OPENSSL_cleanse(bn, sizeof(BIGNUM));
|
||||
}
|
||||
BN_free(bn);
|
||||
}
|
||||
|
||||
BIGNUM *BN_dup(const BIGNUM *src) {
|
||||
@@ -302,6 +283,18 @@ int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num) {
|
||||
if ((bn->flags & BN_FLG_STATIC_DATA) == 0) {
|
||||
OPENSSL_free(bn->d);
|
||||
}
|
||||
bn->d = (BN_ULONG *)words;
|
||||
|
||||
bn->width = num;
|
||||
bn->dmax = num;
|
||||
bn->neg = 0;
|
||||
bn->flags |= BN_FLG_STATIC_DATA;
|
||||
}
|
||||
|
||||
int bn_fits_in_words(const BIGNUM *bn, size_t num) {
|
||||
// All words beyond |num| must be zero.
|
||||
BN_ULONG mask = 0;
|
||||
|
||||
@@ -64,10 +64,10 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
#if !defined(BN_CAN_DIVIDE_ULLONG) && !defined(BN_CAN_USE_INLINE_ASM)
|
||||
// bn_div_words divides a double-width |h|,|l| by |d| and returns the result,
|
||||
// which must fit in a |BN_ULONG|.
|
||||
static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
|
||||
OPENSSL_UNUSED static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l,
|
||||
BN_ULONG d) {
|
||||
BN_ULONG dh, dl, q, ret = 0, th, tl, t;
|
||||
int i, count = 2;
|
||||
|
||||
@@ -135,7 +135,6 @@ static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
|
||||
ret |= q;
|
||||
return ret;
|
||||
}
|
||||
#endif // !defined(BN_CAN_DIVIDE_ULLONG) && !defined(BN_CAN_USE_INLINE_ASM)
|
||||
|
||||
static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
|
||||
BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
|
||||
@@ -286,8 +285,10 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
|
||||
// pointer to the 'top' of snum
|
||||
wnump = &(snum->d[num_n - 1]);
|
||||
|
||||
// Setup to 'res'
|
||||
res->neg = (numerator->neg ^ divisor->neg);
|
||||
// Setup |res|. |numerator| and |res| may alias, so we save |numerator->neg|
|
||||
// for later.
|
||||
const int numerator_neg = numerator->neg;
|
||||
res->neg = (numerator_neg ^ divisor->neg);
|
||||
if (!bn_wexpand(res, loop + 1)) {
|
||||
goto err;
|
||||
}
|
||||
@@ -380,14 +381,11 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
|
||||
bn_set_minimal_width(snum);
|
||||
|
||||
if (rem != NULL) {
|
||||
// Keep a copy of the neg flag in numerator because if |rem| == |numerator|
|
||||
// |BN_rshift| will overwrite it.
|
||||
int neg = numerator->neg;
|
||||
if (!BN_rshift(rem, snum, norm_shift)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_is_zero(rem)) {
|
||||
rem->neg = neg;
|
||||
rem->neg = numerator_neg;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,7 +456,7 @@ void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
|
||||
|
||||
int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
const BIGNUM *numerator, const BIGNUM *divisor,
|
||||
BN_CTX *ctx) {
|
||||
unsigned divisor_min_bits, BN_CTX *ctx) {
|
||||
if (BN_is_negative(numerator) || BN_is_negative(divisor)) {
|
||||
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
|
||||
return 0;
|
||||
@@ -498,8 +496,26 @@ int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
r->neg = 0;
|
||||
|
||||
// Incorporate |numerator| into |r|, one bit at a time, reducing after each
|
||||
// step. At the start of each loop iteration, |r| < |divisor|
|
||||
for (int i = numerator->width - 1; i >= 0; i--) {
|
||||
// step. We maintain the invariant that |0 <= r < divisor| and
|
||||
// |q * divisor + r = n| where |n| is the portion of |numerator| incorporated
|
||||
// so far.
|
||||
//
|
||||
// First, we short-circuit the loop: if we know |divisor| has at least
|
||||
// |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated
|
||||
// without reductions. This significantly speeds up |RSA_check_key|. For
|
||||
// simplicity, we round down to a whole number of words.
|
||||
assert(divisor_min_bits <= BN_num_bits(divisor));
|
||||
int initial_words = 0;
|
||||
if (divisor_min_bits > 0) {
|
||||
initial_words = (divisor_min_bits - 1) / BN_BITS2;
|
||||
if (initial_words > numerator->width) {
|
||||
initial_words = numerator->width;
|
||||
}
|
||||
OPENSSL_memcpy(r->d, numerator->d + numerator->width - initial_words,
|
||||
initial_words * sizeof(BN_ULONG));
|
||||
}
|
||||
|
||||
for (int i = numerator->width - initial_words - 1; i >= 0; i--) {
|
||||
for (int bit = BN_BITS2 - 1; bit >= 0; bit--) {
|
||||
// Incorporate the next bit of the numerator, by computing
|
||||
// r = 2*r or 2*r + 1. Note the result fits in one more word. We store the
|
||||
|
||||
@@ -157,10 +157,11 @@ int bn_lcm_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) {
|
||||
BN_CTX_start(ctx);
|
||||
unsigned shift;
|
||||
BIGNUM *gcd = BN_CTX_get(ctx);
|
||||
int ret = gcd != NULL &&
|
||||
int ret = gcd != NULL && //
|
||||
bn_mul_consttime(r, a, b, ctx) &&
|
||||
bn_gcd_consttime(gcd, &shift, a, b, ctx) &&
|
||||
bn_div_consttime(r, NULL, r, gcd, ctx) &&
|
||||
// |gcd| has a secret bit width.
|
||||
bn_div_consttime(r, NULL, r, gcd, /*divisor_min_bits=*/0, ctx) &&
|
||||
bn_rshift_secret_shift(r, r, shift, ctx);
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
|
||||
@@ -123,7 +123,7 @@
|
||||
#ifndef OPENSSL_HEADER_BN_INTERNAL_H
|
||||
#define OPENSSL_HEADER_BN_INTERNAL_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
|
||||
#if defined(OPENSSL_X86_64) && defined(_MSC_VER)
|
||||
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
@@ -241,6 +241,14 @@ void bn_select_words(BN_ULONG *r, BN_ULONG mask, const BN_ULONG *a,
|
||||
// least significant word first.
|
||||
int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
|
||||
|
||||
// bn_set_static_words acts like |bn_set_words|, but doesn't copy the data. A
|
||||
// flag is set on |bn| so that |BN_free| won't attempt to free the data.
|
||||
//
|
||||
// The |STATIC_BIGNUM| macro is probably a better solution for this outside of
|
||||
// the FIPS module. Inside of the FIPS module that macro generates rel.ro data,
|
||||
// which doesn't work with FIPS requirements.
|
||||
void bn_set_static_words(BIGNUM *bn, const BN_ULONG *words, size_t num);
|
||||
|
||||
// bn_fits_in_words returns one if |bn| may be represented in |num| words, plus
|
||||
// a sign bit, and zero otherwise.
|
||||
int bn_fits_in_words(const BIGNUM *bn, size_t num);
|
||||
@@ -289,7 +297,7 @@ void bn_mul_comba4(BN_ULONG r[8], const BN_ULONG a[4], const BN_ULONG b[4]);
|
||||
void bn_mul_comba8(BN_ULONG r[16], const BN_ULONG a[8], const BN_ULONG b[8]);
|
||||
|
||||
// bn_sqr_comba8 sets |r| to |a|^2.
|
||||
void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[4]);
|
||||
void bn_sqr_comba8(BN_ULONG r[16], const BN_ULONG a[8]);
|
||||
|
||||
// bn_sqr_comba4 sets |r| to |a|^2.
|
||||
void bn_sqr_comba4(BN_ULONG r[8], const BN_ULONG a[4]);
|
||||
@@ -544,12 +552,15 @@ int bn_sqr_consttime(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx);
|
||||
// bn_div_consttime behaves like |BN_div|, but it rejects negative inputs and
|
||||
// treats both inputs, including their magnitudes, as secret. It is, as a
|
||||
// result, much slower than |BN_div| and should only be used for rare operations
|
||||
// where Montgomery reduction is not available.
|
||||
// where Montgomery reduction is not available. |divisor_min_bits| is a
|
||||
// public lower bound for |BN_num_bits(divisor)|. When |divisor|'s bit width is
|
||||
// public, this can speed up the operation.
|
||||
//
|
||||
// Note that |quotient->width| will be set pessimally to |numerator->width|.
|
||||
OPENSSL_EXPORT int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder,
|
||||
const BIGNUM *numerator,
|
||||
const BIGNUM *divisor, BN_CTX *ctx);
|
||||
const BIGNUM *divisor,
|
||||
unsigned divisor_min_bits, BN_CTX *ctx);
|
||||
|
||||
// bn_is_relatively_prime checks whether GCD(|x|, |y|) is one. On success, it
|
||||
// returns one and sets |*out_relatively_prime| to one if the GCD was one and
|
||||
|
||||
@@ -115,10 +115,6 @@
|
||||
#include "../../internal.h"
|
||||
|
||||
|
||||
// The quick sieve algorithm approach to weeding out primes is Philip
|
||||
// Zimmermann's, as implemented in PGP. I have had a read of his comments and
|
||||
// implemented my own version.
|
||||
|
||||
// kPrimes contains the first 1024 primes.
|
||||
static const uint16_t kPrimes[] = {
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
|
||||
|
||||
@@ -57,6 +57,7 @@
|
||||
#include <CCryptoBoringSSL_cipher.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
@@ -224,7 +225,6 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
|
||||
ctx->buf_len = 0;
|
||||
ctx->final_used = 0;
|
||||
ctx->block_mask = ctx->cipher->block_size - 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -238,16 +238,31 @@ int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
|
||||
return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
|
||||
}
|
||||
|
||||
// block_remainder returns the number of bytes to remove from |len| to get a
|
||||
// multiple of |ctx|'s block size.
|
||||
static int block_remainder(const EVP_CIPHER_CTX *ctx, int len) {
|
||||
// |block_size| must be a power of two.
|
||||
assert(ctx->cipher->block_size != 0);
|
||||
assert((ctx->cipher->block_size & (ctx->cipher->block_size - 1)) == 0);
|
||||
return len & (ctx->cipher->block_size - 1);
|
||||
}
|
||||
|
||||
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
int i, j, bl;
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
int bl = ctx->cipher->block_size;
|
||||
if (bl > 1 && in_len > INT_MAX - bl) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
i = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
if (i < 0) {
|
||||
int ret = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
if (ret < 0) {
|
||||
return 0;
|
||||
} else {
|
||||
*out_len = i;
|
||||
*out_len = ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -257,7 +272,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
return in_len == 0;
|
||||
}
|
||||
|
||||
if (ctx->buf_len == 0 && (in_len & ctx->block_mask) == 0) {
|
||||
if (ctx->buf_len == 0 && block_remainder(ctx, in_len) == 0) {
|
||||
if (ctx->cipher->cipher(ctx, out, in, in_len)) {
|
||||
*out_len = in_len;
|
||||
return 1;
|
||||
@@ -267,8 +282,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
}
|
||||
}
|
||||
|
||||
i = ctx->buf_len;
|
||||
bl = ctx->cipher->block_size;
|
||||
int i = ctx->buf_len;
|
||||
assert(bl <= (int)sizeof(ctx->buf));
|
||||
if (i != 0) {
|
||||
if (bl - i > in_len) {
|
||||
@@ -277,7 +291,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
*out_len = 0;
|
||||
return 1;
|
||||
} else {
|
||||
j = bl - i;
|
||||
int j = bl - i;
|
||||
OPENSSL_memcpy(&ctx->buf[i], in, j);
|
||||
if (!ctx->cipher->cipher(ctx, out, ctx->buf, bl)) {
|
||||
return 0;
|
||||
@@ -291,7 +305,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
*out_len = 0;
|
||||
}
|
||||
|
||||
i = in_len & ctx->block_mask;
|
||||
i = block_remainder(ctx, in_len);
|
||||
in_len -= i;
|
||||
if (in_len > 0) {
|
||||
if (!ctx->cipher->cipher(ctx, out, in, in_len)) {
|
||||
@@ -353,8 +367,13 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len) {
|
||||
|
||||
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
const uint8_t *in, int in_len) {
|
||||
int fix_len;
|
||||
unsigned int b;
|
||||
// Ciphers that use blocks may write up to |bl| extra bytes. Ensure the output
|
||||
// does not overflow |*out_len|.
|
||||
unsigned int b = ctx->cipher->block_size;
|
||||
if (b > 1 && in_len > INT_MAX - (int)b) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
|
||||
int r = ctx->cipher->cipher(ctx, out, in, in_len);
|
||||
@@ -376,15 +395,12 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, uint8_t *out, int *out_len,
|
||||
return EVP_EncryptUpdate(ctx, out, out_len, in, in_len);
|
||||
}
|
||||
|
||||
b = ctx->cipher->block_size;
|
||||
assert(b <= sizeof(ctx->final));
|
||||
|
||||
int fix_len = 0;
|
||||
if (ctx->final_used) {
|
||||
OPENSSL_memcpy(out, ctx->final, b);
|
||||
out += b;
|
||||
fix_len = 1;
|
||||
} else {
|
||||
fix_len = 0;
|
||||
}
|
||||
|
||||
if (!EVP_EncryptUpdate(ctx, out, out_len, in, in_len)) {
|
||||
|
||||
@@ -68,6 +68,8 @@
|
||||
OPENSSL_MSVC_PRAGMA(warning(push))
|
||||
OPENSSL_MSVC_PRAGMA(warning(disable: 4702)) // Unreachable code.
|
||||
|
||||
#define AES_GCM_NONCE_LENGTH 12
|
||||
|
||||
#if defined(BSAES)
|
||||
static void vpaes_ctr32_encrypt_blocks_with_bsaes(const uint8_t *in,
|
||||
uint8_t *out, size_t blocks,
|
||||
@@ -139,10 +141,22 @@ typedef struct {
|
||||
|
||||
static int aes_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
int ret, mode;
|
||||
int ret;
|
||||
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;
|
||||
const int mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
|
||||
|
||||
if (mode == EVP_CIPH_CTR_MODE) {
|
||||
switch (ctx->key_len) {
|
||||
case 16:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_128_ctr);
|
||||
break;
|
||||
|
||||
case 32:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_256_ctr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mode = ctx->cipher->flags & EVP_CIPH_MODE_MASK;
|
||||
if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) && !enc) {
|
||||
if (hwaes_capable()) {
|
||||
ret = aes_hw_set_decrypt_key(key, ctx->key_len * 8, &dat->ks.ks);
|
||||
@@ -351,6 +365,17 @@ static int aes_gcm_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
if (!iv && !key) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
switch (ctx->key_len) {
|
||||
case 16:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_128_gcm);
|
||||
break;
|
||||
|
||||
case 32:
|
||||
boringssl_fips_inc_counter(fips_counter_evp_aes_256_gcm);
|
||||
break;
|
||||
}
|
||||
|
||||
if (key) {
|
||||
OPENSSL_memset(&gctx->gcm, 0, sizeof(gctx->gcm));
|
||||
gctx->ctr = aes_ctr_set_key(&gctx->ks.ks, &gctx->gcm.gcm_key, NULL, key,
|
||||
@@ -630,7 +655,7 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_128_gcm_generic) {
|
||||
out->nid = NID_aes_128_gcm;
|
||||
out->block_size = 1;
|
||||
out->key_len = 16;
|
||||
out->iv_len = 12;
|
||||
out->iv_len = AES_GCM_NONCE_LENGTH;
|
||||
out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
|
||||
out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
|
||||
EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
|
||||
@@ -698,7 +723,7 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_192_gcm_generic) {
|
||||
out->nid = NID_aes_192_gcm;
|
||||
out->block_size = 1;
|
||||
out->key_len = 24;
|
||||
out->iv_len = 12;
|
||||
out->iv_len = AES_GCM_NONCE_LENGTH;
|
||||
out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
|
||||
out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
|
||||
EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
|
||||
@@ -766,7 +791,7 @@ DEFINE_LOCAL_DATA(EVP_CIPHER, aes_256_gcm_generic) {
|
||||
out->nid = NID_aes_256_gcm;
|
||||
out->block_size = 1;
|
||||
out->key_len = 32;
|
||||
out->iv_len = 12;
|
||||
out->iv_len = AES_GCM_NONCE_LENGTH;
|
||||
out->ctx_size = sizeof(EVP_AES_GCM_CTX) + EVP_AES_GCM_CTX_PADDING;
|
||||
out->flags = EVP_CIPH_GCM_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_CUSTOM_COPY |
|
||||
EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT |
|
||||
@@ -931,21 +956,19 @@ static int aead_aes_gcm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
||||
|
||||
static void aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx) {}
|
||||
|
||||
static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
uint8_t *out_tag, size_t *out_tag_len,
|
||||
size_t max_out_tag_len,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *extra_in,
|
||||
size_t extra_in_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
|
||||
|
||||
if (extra_in_len + ctx->tag_len < ctx->tag_len) {
|
||||
static int aead_aes_gcm_seal_scatter_impl(
|
||||
const struct aead_aes_gcm_ctx *gcm_ctx,
|
||||
uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *extra_in, size_t extra_in_len,
|
||||
const uint8_t *ad, size_t ad_len,
|
||||
size_t tag_len) {
|
||||
if (extra_in_len + tag_len < tag_len) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
|
||||
return 0;
|
||||
}
|
||||
if (max_out_tag_len < extra_in_len + ctx->tag_len) {
|
||||
if (max_out_tag_len < extra_in_len + tag_len) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
@@ -989,18 +1012,35 @@ static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
}
|
||||
}
|
||||
|
||||
CRYPTO_gcm128_tag(&gcm, out_tag + extra_in_len, ctx->tag_len);
|
||||
*out_tag_len = ctx->tag_len + extra_in_len;
|
||||
CRYPTO_gcm128_tag(&gcm, out_tag + extra_in_len, tag_len);
|
||||
*out_tag_len = tag_len + extra_in_len;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *in_tag, size_t in_tag_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *) &ctx->state;
|
||||
static int aead_aes_gcm_seal_scatter(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
uint8_t *out_tag, size_t *out_tag_len,
|
||||
size_t max_out_tag_len,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *extra_in,
|
||||
size_t extra_in_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
const struct aead_aes_gcm_ctx *gcm_ctx =
|
||||
(const struct aead_aes_gcm_ctx *)&ctx->state;
|
||||
return aead_aes_gcm_seal_scatter_impl(
|
||||
gcm_ctx, out, out_tag, out_tag_len, max_out_tag_len, nonce, nonce_len, in,
|
||||
in_len, extra_in, extra_in_len, ad, ad_len, ctx->tag_len);
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_open_gather_impl(const struct aead_aes_gcm_ctx *gcm_ctx,
|
||||
uint8_t *out,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *in_tag,
|
||||
size_t in_tag_len,
|
||||
const uint8_t *ad, size_t ad_len,
|
||||
size_t tag_len) {
|
||||
uint8_t tag[EVP_AEAD_AES_GCM_TAG_LEN];
|
||||
|
||||
if (nonce_len == 0) {
|
||||
@@ -1008,7 +1048,7 @@ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (in_tag_len != ctx->tag_len) {
|
||||
if (in_tag_len != tag_len) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
||||
return 0;
|
||||
}
|
||||
@@ -1035,8 +1075,8 @@ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
}
|
||||
}
|
||||
|
||||
CRYPTO_gcm128_tag(&gcm, tag, ctx->tag_len);
|
||||
if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) {
|
||||
CRYPTO_gcm128_tag(&gcm, tag, tag_len);
|
||||
if (CRYPTO_memcmp(tag, in_tag, tag_len) != 0) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
||||
return 0;
|
||||
}
|
||||
@@ -1044,11 +1084,22 @@ static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
const uint8_t *nonce, size_t nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *in_tag, size_t in_tag_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
struct aead_aes_gcm_ctx *gcm_ctx = (struct aead_aes_gcm_ctx *)&ctx->state;
|
||||
return aead_aes_gcm_open_gather_impl(gcm_ctx, out, nonce, nonce_len, in,
|
||||
in_len, in_tag, in_tag_len, ad, ad_len,
|
||||
ctx->tag_len);
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 16;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1063,7 +1114,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_192_gcm) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 24;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1078,7 +1129,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 32;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1089,6 +1140,116 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm) {
|
||||
out->open_gather = aead_aes_gcm_open_gather;
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_init_randnonce(EVP_AEAD_CTX *ctx, const uint8_t *key,
|
||||
size_t key_len,
|
||||
size_t requested_tag_len) {
|
||||
if (requested_tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH) {
|
||||
if (requested_tag_len < AES_GCM_NONCE_LENGTH) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
requested_tag_len -= AES_GCM_NONCE_LENGTH;
|
||||
}
|
||||
|
||||
if (!aead_aes_gcm_init(ctx, key, key_len, requested_tag_len)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx->tag_len += AES_GCM_NONCE_LENGTH;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_seal_scatter_randnonce(
|
||||
const EVP_AEAD_CTX *ctx,
|
||||
uint8_t *out, uint8_t *out_tag, size_t *out_tag_len, size_t max_out_tag_len,
|
||||
const uint8_t *external_nonce, size_t external_nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *extra_in, size_t extra_in_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
if (external_nonce_len != 0) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t nonce[AES_GCM_NONCE_LENGTH];
|
||||
if (max_out_tag_len < sizeof(nonce)) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
RAND_bytes(nonce, sizeof(nonce));
|
||||
const struct aead_aes_gcm_ctx *gcm_ctx =
|
||||
(const struct aead_aes_gcm_ctx *)&ctx->state;
|
||||
if (!aead_aes_gcm_seal_scatter_impl(gcm_ctx, out, out_tag, out_tag_len,
|
||||
max_out_tag_len - AES_GCM_NONCE_LENGTH,
|
||||
nonce, sizeof(nonce), in, in_len,
|
||||
extra_in, extra_in_len, ad, ad_len,
|
||||
ctx->tag_len - AES_GCM_NONCE_LENGTH)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
assert(*out_tag_len + sizeof(nonce) <= max_out_tag_len);
|
||||
memcpy(out_tag + *out_tag_len, nonce, sizeof(nonce));
|
||||
*out_tag_len += sizeof(nonce);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aead_aes_gcm_open_gather_randnonce(
|
||||
const EVP_AEAD_CTX *ctx, uint8_t *out,
|
||||
const uint8_t *external_nonce, size_t external_nonce_len,
|
||||
const uint8_t *in, size_t in_len,
|
||||
const uint8_t *in_tag, size_t in_tag_len,
|
||||
const uint8_t *ad, size_t ad_len) {
|
||||
if (external_nonce_len != 0) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (in_tag_len < AES_GCM_NONCE_LENGTH) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
|
||||
return 0;
|
||||
}
|
||||
const uint8_t *nonce = in_tag + in_tag_len - AES_GCM_NONCE_LENGTH;
|
||||
|
||||
const struct aead_aes_gcm_ctx *gcm_ctx =
|
||||
(const struct aead_aes_gcm_ctx *)&ctx->state;
|
||||
return aead_aes_gcm_open_gather_impl(
|
||||
gcm_ctx, out, nonce, AES_GCM_NONCE_LENGTH, in, in_len, in_tag,
|
||||
in_tag_len - AES_GCM_NONCE_LENGTH, ad, ad_len,
|
||||
ctx->tag_len - AES_GCM_NONCE_LENGTH);
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_randnonce) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 16;
|
||||
out->nonce_len = 0;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
|
||||
out->init = aead_aes_gcm_init_randnonce;
|
||||
out->cleanup = aead_aes_gcm_cleanup;
|
||||
out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
|
||||
out->open_gather = aead_aes_gcm_open_gather_randnonce;
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_randnonce) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 32;
|
||||
out->nonce_len = 0;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN + AES_GCM_NONCE_LENGTH;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
|
||||
out->init = aead_aes_gcm_init_randnonce;
|
||||
out->cleanup = aead_aes_gcm_cleanup;
|
||||
out->seal_scatter = aead_aes_gcm_seal_scatter_randnonce;
|
||||
out->open_gather = aead_aes_gcm_open_gather_randnonce;
|
||||
}
|
||||
|
||||
struct aead_aes_gcm_tls12_ctx {
|
||||
struct aead_aes_gcm_ctx gcm_ctx;
|
||||
uint64_t min_next_nonce;
|
||||
@@ -1128,7 +1289,7 @@ static int aead_aes_gcm_tls12_seal_scatter(
|
||||
struct aead_aes_gcm_tls12_ctx *gcm_ctx =
|
||||
(struct aead_aes_gcm_tls12_ctx *) &ctx->state;
|
||||
|
||||
if (nonce_len != 12) {
|
||||
if (nonce_len != AES_GCM_NONCE_LENGTH) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
||||
return 0;
|
||||
}
|
||||
@@ -1155,7 +1316,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls12) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 16;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1170,7 +1331,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls12) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 32;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1223,7 +1384,7 @@ static int aead_aes_gcm_tls13_seal_scatter(
|
||||
struct aead_aes_gcm_tls13_ctx *gcm_ctx =
|
||||
(struct aead_aes_gcm_tls13_ctx *) &ctx->state;
|
||||
|
||||
if (nonce_len != 12) {
|
||||
if (nonce_len != AES_GCM_NONCE_LENGTH) {
|
||||
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
|
||||
return 0;
|
||||
}
|
||||
@@ -1261,7 +1422,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_128_gcm_tls13) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 16;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
@@ -1276,7 +1437,7 @@ DEFINE_METHOD_FUNCTION(EVP_AEAD, EVP_aead_aes_256_gcm_tls13) {
|
||||
memset(out, 0, sizeof(EVP_AEAD));
|
||||
|
||||
out->key_len = 32;
|
||||
out->nonce_len = 12;
|
||||
out->nonce_len = AES_GCM_NONCE_LENGTH;
|
||||
out->overhead = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->max_tag_len = EVP_AEAD_AES_GCM_TAG_LEN;
|
||||
out->seal_scatter_supports_extra_in = 1;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File renamed without changes.
+146
-223
@@ -60,17 +60,16 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bn.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_ex_data.h>
|
||||
#include <CCryptoBoringSSL_digest.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
#include "../../internal.h"
|
||||
#include "../bn/internal.h"
|
||||
|
||||
|
||||
#define OPENSSL_DH_MAX_MODULUS_BITS 10000
|
||||
|
||||
static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
|
||||
|
||||
DH *DH_new(void) {
|
||||
DH *dh = OPENSSL_malloc(sizeof(DH));
|
||||
if (dh == NULL) {
|
||||
@@ -83,7 +82,6 @@ DH *DH_new(void) {
|
||||
CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
|
||||
|
||||
dh->references = 1;
|
||||
CRYPTO_new_ex_data(&dh->ex_data);
|
||||
|
||||
return dh;
|
||||
}
|
||||
@@ -97,8 +95,6 @@ void DH_free(DH *dh) {
|
||||
return;
|
||||
}
|
||||
|
||||
CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
|
||||
|
||||
BN_MONT_CTX_free(dh->method_mont_p);
|
||||
BN_clear_free(dh->p);
|
||||
BN_clear_free(dh->g);
|
||||
@@ -189,120 +185,6 @@ int DH_set_length(DH *dh, unsigned priv_length) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
|
||||
// We generate DH parameters as follows
|
||||
// find a prime q which is prime_bits/2 bits long.
|
||||
// p=(2*q)+1 or (p-1)/2 = q
|
||||
// For this case, g is a generator if
|
||||
// g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
|
||||
// Since the factors of p-1 are q and 2, we just need to check
|
||||
// g^2 mod p != 1 and g^q mod p != 1.
|
||||
//
|
||||
// Having said all that,
|
||||
// there is another special case method for the generators 2, 3 and 5.
|
||||
// for 2, p mod 24 == 11
|
||||
// for 3, p mod 12 == 5 <<<<< does not work for safe primes.
|
||||
// for 5, p mod 10 == 3 or 7
|
||||
//
|
||||
// Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
|
||||
// special generators and for answering some of my questions.
|
||||
//
|
||||
// I've implemented the second simple method :-).
|
||||
// Since DH should be using a safe prime (both p and q are prime),
|
||||
// this generator function can take a very very long time to run.
|
||||
|
||||
// Actually there is no reason to insist that 'generator' be a generator.
|
||||
// It's just as OK (and in some sense better) to use a generator of the
|
||||
// order-q subgroup.
|
||||
|
||||
BIGNUM *t1, *t2;
|
||||
int g, ok = 0;
|
||||
BN_CTX *ctx = NULL;
|
||||
|
||||
ctx = BN_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
goto err;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
t1 = BN_CTX_get(ctx);
|
||||
t2 = BN_CTX_get(ctx);
|
||||
if (t1 == NULL || t2 == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Make sure |dh| has the necessary elements
|
||||
if (dh->p == NULL) {
|
||||
dh->p = BN_new();
|
||||
if (dh->p == NULL) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
if (dh->g == NULL) {
|
||||
dh->g = BN_new();
|
||||
if (dh->g == NULL) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
if (generator <= 1) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
|
||||
goto err;
|
||||
}
|
||||
if (generator == DH_GENERATOR_2) {
|
||||
if (!BN_set_word(t1, 24)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 11)) {
|
||||
goto err;
|
||||
}
|
||||
g = 2;
|
||||
} else if (generator == DH_GENERATOR_5) {
|
||||
if (!BN_set_word(t1, 10)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 3)) {
|
||||
goto err;
|
||||
}
|
||||
// BN_set_word(t3,7); just have to miss
|
||||
// out on these ones :-(
|
||||
g = 5;
|
||||
} else {
|
||||
// in the general case, don't worry if 'generator' is a
|
||||
// generator or not: since we are using safe primes,
|
||||
// it will generate either an order-q or an order-2q group,
|
||||
// which both is OK
|
||||
if (!BN_set_word(t1, 2)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(t2, 1)) {
|
||||
goto err;
|
||||
}
|
||||
g = generator;
|
||||
}
|
||||
|
||||
if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_GENCB_call(cb, 3, 0)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_set_word(dh->g, g)) {
|
||||
goto err;
|
||||
}
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
|
||||
}
|
||||
|
||||
if (ctx != NULL) {
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
int DH_generate_key(DH *dh) {
|
||||
int ok = 0;
|
||||
int generate_new_key = 0;
|
||||
@@ -390,56 +272,127 @@ err:
|
||||
return ok;
|
||||
}
|
||||
|
||||
int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
|
||||
BN_CTX *ctx = NULL;
|
||||
BIGNUM *shared_key;
|
||||
int ret = -1;
|
||||
int check_result;
|
||||
|
||||
static int dh_compute_key(DH *dh, BIGNUM *out_shared_key,
|
||||
const BIGNUM *peers_key, BN_CTX *ctx) {
|
||||
if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ctx = BN_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
goto err;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
shared_key = BN_CTX_get(ctx);
|
||||
if (shared_key == NULL) {
|
||||
goto err;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dh->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
|
||||
goto err;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
|
||||
int check_result;
|
||||
if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
BN_CTX_start(ctx);
|
||||
BIGNUM *p_minus_1 = BN_CTX_get(ctx);
|
||||
|
||||
if (!p_minus_1 ||
|
||||
!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
|
||||
dh->p, ctx)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
|
||||
ctx, dh->method_mont_p)) {
|
||||
if (!BN_mod_exp_mont_consttime(out_shared_key, peers_key, dh->priv_key, dh->p,
|
||||
ctx, dh->method_mont_p) ||
|
||||
!BN_copy(p_minus_1, dh->p) ||
|
||||
!BN_sub_word(p_minus_1, 1)) {
|
||||
OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = BN_bn2bin(shared_key, out);
|
||||
|
||||
err:
|
||||
if (ctx != NULL) {
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
// This performs the check required by SP 800-56Ar3 section 5.7.1.1 step two.
|
||||
if (BN_cmp_word(out_shared_key, 1) <= 0 ||
|
||||
BN_cmp(out_shared_key, p_minus_1) == 0) {
|
||||
OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
BN_CTX_end(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
|
||||
BN_CTX *ctx = BN_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
return -1;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
|
||||
int dh_size = DH_size(dh);
|
||||
int ret = -1;
|
||||
BIGNUM *shared_key = BN_CTX_get(ctx);
|
||||
if (shared_key &&
|
||||
dh_compute_key(dh, shared_key, peers_key, ctx) &&
|
||||
BN_bn2bin_padded(out, dh_size, shared_key)) {
|
||||
ret = dh_size;
|
||||
}
|
||||
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
|
||||
BN_CTX *ctx = BN_CTX_new();
|
||||
if (ctx == NULL) {
|
||||
return -1;
|
||||
}
|
||||
BN_CTX_start(ctx);
|
||||
|
||||
int ret = -1;
|
||||
BIGNUM *shared_key = BN_CTX_get(ctx);
|
||||
if (shared_key && dh_compute_key(dh, shared_key, peers_key, ctx)) {
|
||||
ret = BN_bn2bin(shared_key, out);
|
||||
}
|
||||
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DH_compute_key_hashed(DH *dh, uint8_t *out, size_t *out_len,
|
||||
size_t max_out_len, const BIGNUM *peers_key,
|
||||
const EVP_MD *digest) {
|
||||
*out_len = (size_t)-1;
|
||||
|
||||
const size_t digest_len = EVP_MD_size(digest);
|
||||
if (digest_len > max_out_len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
const size_t dh_len = DH_size(dh);
|
||||
uint8_t *shared_bytes = OPENSSL_malloc(dh_len);
|
||||
unsigned out_len_unsigned;
|
||||
if (!shared_bytes ||
|
||||
// SP 800-56A is ambiguous about whether the output should be padded prior
|
||||
// to revision three. But revision three, section C.1, awkwardly specifies
|
||||
// padding to the length of p.
|
||||
//
|
||||
// Also, padded output avoids side-channels, so is always strongly
|
||||
// advisable.
|
||||
DH_compute_key_padded(shared_bytes, peers_key, dh) != (int)dh_len ||
|
||||
!EVP_Digest(shared_bytes, dh_len, out, &out_len_unsigned, digest, NULL) ||
|
||||
out_len_unsigned != digest_len) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
*out_len = digest_len;
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(shared_bytes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -452,82 +405,52 @@ int DH_up_ref(DH *dh) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
|
||||
BIGNUM *a = NULL;
|
||||
DH *DH_get_rfc7919_2048(void) {
|
||||
// This is the prime from https://tools.ietf.org/html/rfc7919#appendix-A.1,
|
||||
// which is specifically approved for FIPS in appendix D of SP 800-56Ar3.
|
||||
static const BN_ULONG kFFDHE2048Data[] = {
|
||||
TOBN(0xffffffff, 0xffffffff), TOBN(0x886b4238, 0x61285c97),
|
||||
TOBN(0xc6f34a26, 0xc1b2effa), TOBN(0xc58ef183, 0x7d1683b2),
|
||||
TOBN(0x3bb5fcbc, 0x2ec22005), TOBN(0xc3fe3b1b, 0x4c6fad73),
|
||||
TOBN(0x8e4f1232, 0xeef28183), TOBN(0x9172fe9c, 0xe98583ff),
|
||||
TOBN(0xc03404cd, 0x28342f61), TOBN(0x9e02fce1, 0xcdf7e2ec),
|
||||
TOBN(0x0b07a7c8, 0xee0a6d70), TOBN(0xae56ede7, 0x6372bb19),
|
||||
TOBN(0x1d4f42a3, 0xde394df4), TOBN(0xb96adab7, 0x60d7f468),
|
||||
TOBN(0xd108a94b, 0xb2c8e3fb), TOBN(0xbc0ab182, 0xb324fb61),
|
||||
TOBN(0x30acca4f, 0x483a797a), TOBN(0x1df158a1, 0x36ade735),
|
||||
TOBN(0xe2a689da, 0xf3efe872), TOBN(0x984f0c70, 0xe0e68b77),
|
||||
TOBN(0xb557135e, 0x7f57c935), TOBN(0x85636555, 0x3ded1af3),
|
||||
TOBN(0x2433f51f, 0x5f066ed0), TOBN(0xd3df1ed5, 0xd5fd6561),
|
||||
TOBN(0xf681b202, 0xaec4617a), TOBN(0x7d2fe363, 0x630c75d8),
|
||||
TOBN(0xcc939dce, 0x249b3ef9), TOBN(0xa9e13641, 0x146433fb),
|
||||
TOBN(0xd8b9c583, 0xce2d3695), TOBN(0xafdc5620, 0x273d3cf1),
|
||||
TOBN(0xadf85458, 0xa2bb4a9a), TOBN(0xffffffff, 0xffffffff),
|
||||
};
|
||||
|
||||
if (src) {
|
||||
a = BN_dup(src);
|
||||
if (!a) {
|
||||
return 0;
|
||||
}
|
||||
BIGNUM *const ffdhe2048_p = BN_new();
|
||||
BIGNUM *const ffdhe2048_q = BN_new();
|
||||
BIGNUM *const ffdhe2048_g = BN_new();
|
||||
DH *const dh = DH_new();
|
||||
|
||||
if (!ffdhe2048_p || !ffdhe2048_q || !ffdhe2048_g || !dh) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
BN_free(*dst);
|
||||
*dst = a;
|
||||
return 1;
|
||||
}
|
||||
bn_set_static_words(ffdhe2048_p, kFFDHE2048Data,
|
||||
OPENSSL_ARRAY_SIZE(kFFDHE2048Data));
|
||||
|
||||
static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
|
||||
if (is_x942 == -1) {
|
||||
is_x942 = !!from->q;
|
||||
}
|
||||
if (!int_dh_bn_cpy(&to->p, from->p) ||
|
||||
!int_dh_bn_cpy(&to->g, from->g)) {
|
||||
return 0;
|
||||
if (!BN_rshift1(ffdhe2048_q, ffdhe2048_p) ||
|
||||
!BN_set_word(ffdhe2048_g, 2) ||
|
||||
!DH_set0_pqg(dh, ffdhe2048_p, ffdhe2048_q, ffdhe2048_g)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!is_x942) {
|
||||
return 1;
|
||||
}
|
||||
return dh;
|
||||
|
||||
if (!int_dh_bn_cpy(&to->q, from->q) ||
|
||||
!int_dh_bn_cpy(&to->j, from->j)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
OPENSSL_free(to->seed);
|
||||
to->seed = NULL;
|
||||
to->seedlen = 0;
|
||||
|
||||
if (from->seed) {
|
||||
to->seed = OPENSSL_memdup(from->seed, from->seedlen);
|
||||
if (!to->seed) {
|
||||
return 0;
|
||||
}
|
||||
to->seedlen = from->seedlen;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
DH *DHparams_dup(const DH *dh) {
|
||||
DH *ret = DH_new();
|
||||
if (!ret) {
|
||||
err:
|
||||
BN_free(ffdhe2048_p);
|
||||
BN_free(ffdhe2048_q);
|
||||
BN_free(ffdhe2048_g);
|
||||
DH_free(dh);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!int_dh_param_copy(ret, dh, -1)) {
|
||||
DH_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
|
||||
CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
|
||||
int index;
|
||||
if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
|
||||
free_func)) {
|
||||
return -1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
int DH_set_ex_data(DH *d, int idx, void *arg) {
|
||||
return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
|
||||
}
|
||||
|
||||
void *DH_get_ex_data(DH *d, int idx) {
|
||||
return CRYPTO_get_ex_data(&d->ex_data, idx);
|
||||
}
|
||||
@@ -68,6 +68,8 @@
|
||||
|
||||
int EVP_MD_type(const EVP_MD *md) { return md->type; }
|
||||
|
||||
int EVP_MD_nid(const EVP_MD *md) { return EVP_MD_type(md); }
|
||||
|
||||
uint32_t EVP_MD_flags(const EVP_MD *md) { return md->flags; }
|
||||
|
||||
size_t EVP_MD_size(const EVP_MD *md) { return md->md_size; }
|
||||
@@ -177,6 +179,13 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in) {
|
||||
EVP_MD_CTX_cleanup(out);
|
||||
// While not guaranteed, |EVP_MD_CTX| is currently safe to move with |memcpy|.
|
||||
OPENSSL_memcpy(out, in, sizeof(EVP_MD_CTX));
|
||||
EVP_MD_CTX_init(in);
|
||||
}
|
||||
|
||||
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) {
|
||||
EVP_MD_CTX_init(out);
|
||||
return EVP_MD_CTX_copy_ex(out, in);
|
||||
|
||||
@@ -247,13 +247,21 @@ static void sha512_256_init(EVP_MD_CTX *ctx) {
|
||||
CHECK(SHA512_256_Init(ctx->md_data));
|
||||
}
|
||||
|
||||
static void sha512_256_update(EVP_MD_CTX *ctx, const void *data, size_t count) {
|
||||
CHECK(SHA512_256_Update(ctx->md_data, data, count));
|
||||
}
|
||||
|
||||
static void sha512_256_final(EVP_MD_CTX *ctx, uint8_t *md) {
|
||||
CHECK(SHA512_256_Final(md, ctx->md_data));
|
||||
}
|
||||
|
||||
DEFINE_METHOD_FUNCTION(EVP_MD, EVP_sha512_256) {
|
||||
out->type = NID_sha512_256;
|
||||
out->md_size = SHA512_256_DIGEST_LENGTH;
|
||||
out->flags = 0;
|
||||
out->init = sha512_256_init;
|
||||
out->update = sha512_update;
|
||||
out->final = sha512_final;
|
||||
out->update = sha512_256_update;
|
||||
out->final = sha512_256_final;
|
||||
out->block_size = 128;
|
||||
out->ctx_size = sizeof(SHA512_CTX);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,9 @@
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ==================================================================== */
|
||||
|
||||
#ifndef OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
#define OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#include <assert.h>
|
||||
@@ -59,22 +62,15 @@ extern "C" {
|
||||
|
||||
// This is a generic 32-bit "collector" for message digest algorithms. It
|
||||
// collects input character stream into chunks of 32-bit values and invokes the
|
||||
// block function that performs the actual hash calculations. To make use of
|
||||
// this mechanism, the following macros must be defined before including
|
||||
// md32_common.h.
|
||||
// block function that performs the actual hash calculations.
|
||||
//
|
||||
// One of |DATA_ORDER_IS_BIG_ENDIAN| or |DATA_ORDER_IS_LITTLE_ENDIAN| must be
|
||||
// defined to specify the byte order of the input stream.
|
||||
//
|
||||
// |HASH_CBLOCK| must be defined as the integer block size, in bytes.
|
||||
//
|
||||
// |HASH_CTX| must be defined as the name of the context structure, which must
|
||||
// have at least the following members:
|
||||
// To make use of this mechanism, the hash context should be defined with the
|
||||
// following parameters.
|
||||
//
|
||||
// typedef struct <name>_state_st {
|
||||
// uint32_t h[<chaining length> / sizeof(uint32_t)];
|
||||
// uint32_t Nl, Nh;
|
||||
// uint8_t data[HASH_CBLOCK];
|
||||
// uint8_t data[<block size>];
|
||||
// unsigned num;
|
||||
// ...
|
||||
// } <NAME>_CTX;
|
||||
@@ -83,186 +79,117 @@ extern "C" {
|
||||
// any truncation (e.g. 64 for SHA-224 and SHA-256, 128 for SHA-384 and
|
||||
// SHA-512).
|
||||
//
|
||||
// |HASH_UPDATE| must be defined as the name of the "Update" function to
|
||||
// generate.
|
||||
//
|
||||
// |HASH_TRANSFORM| must be defined as the the name of the "Transform"
|
||||
// function to generate.
|
||||
//
|
||||
// |HASH_FINAL| must be defined as the name of "Final" function to generate.
|
||||
//
|
||||
// |HASH_BLOCK_DATA_ORDER| must be defined as the name of the "Block" function.
|
||||
// That function must be implemented manually. It must be capable of operating
|
||||
// on *unaligned* input data in its original (data) byte order. It must have
|
||||
// this signature:
|
||||
//
|
||||
// void HASH_BLOCK_DATA_ORDER(uint32_t *state, const uint8_t *data,
|
||||
// size_t num);
|
||||
//
|
||||
// It must update the hash state |state| with |num| blocks of data from |data|,
|
||||
// where each block is |HASH_CBLOCK| bytes; i.e. |data| points to a array of
|
||||
// |HASH_CBLOCK * num| bytes. |state| points to the |h| member of a |HASH_CTX|,
|
||||
// and so will have |<chaining length> / sizeof(uint32_t)| elements.
|
||||
//
|
||||
// |HASH_MAKE_STRING(c, s)| must be defined as a block statement that converts
|
||||
// the hash state |c->h| into the output byte order, storing the result in |s|.
|
||||
// |h| is the hash state and is updated by a function of type
|
||||
// |crypto_md32_block_func|. |data| is the partial unprocessed block and has
|
||||
// |num| bytes. |Nl| and |Nh| maintain the number of bits processed so far.
|
||||
|
||||
#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
#error "DATA_ORDER must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_CBLOCK
|
||||
#error "HASH_CBLOCK must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_CTX
|
||||
#error "HASH_CTX must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_UPDATE
|
||||
#error "HASH_UPDATE must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_TRANSFORM
|
||||
#error "HASH_TRANSFORM must be defined!"
|
||||
#endif
|
||||
#ifndef HASH_FINAL
|
||||
#error "HASH_FINAL must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_BLOCK_DATA_ORDER
|
||||
#error "HASH_BLOCK_DATA_ORDER must be defined!"
|
||||
#endif
|
||||
|
||||
#ifndef HASH_MAKE_STRING
|
||||
#error "HASH_MAKE_STRING must be defined!"
|
||||
#endif
|
||||
|
||||
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
|
||||
|
||||
#define HOST_c2l(c, l) \
|
||||
do { \
|
||||
(l) = (((uint32_t)(*((c)++))) << 24); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 16); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 8); \
|
||||
(l) |= (((uint32_t)(*((c)++)))); \
|
||||
} while (0)
|
||||
|
||||
#define HOST_l2c(l, c) \
|
||||
do { \
|
||||
*((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l)) & 0xff); \
|
||||
} while (0)
|
||||
|
||||
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
|
||||
#define HOST_c2l(c, l) \
|
||||
do { \
|
||||
(l) = (((uint32_t)(*((c)++)))); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 8); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 16); \
|
||||
(l) |= (((uint32_t)(*((c)++))) << 24); \
|
||||
} while (0)
|
||||
|
||||
#define HOST_l2c(l, c) \
|
||||
do { \
|
||||
*((c)++) = (uint8_t)(((l)) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
|
||||
*((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
|
||||
} while (0)
|
||||
|
||||
#endif // DATA_ORDER
|
||||
|
||||
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
|
||||
const uint8_t *data = data_;
|
||||
// A crypto_md32_block_func should incorporate |num_blocks| of input from |data|
|
||||
// into |state|. It is assumed the caller has sized |state| and |data| for the
|
||||
// hash function.
|
||||
typedef void (*crypto_md32_block_func)(uint32_t *state, const uint8_t *data,
|
||||
size_t num_blocks);
|
||||
|
||||
// crypto_md32_update adds |len| bytes from |in| to the digest. |data| must be a
|
||||
// buffer of length |block_size| with the first |*num| bytes containing a
|
||||
// partial block. This function combines the partial block with |in| and
|
||||
// incorporates any complete blocks into the digest state |h|. It then updates
|
||||
// |data| and |*num| with the new partial block and updates |*Nh| and |*Nl| with
|
||||
// the data consumed.
|
||||
static inline void crypto_md32_update(crypto_md32_block_func block_func,
|
||||
uint32_t *h, uint8_t *data,
|
||||
size_t block_size, unsigned *num,
|
||||
uint32_t *Nh, uint32_t *Nl,
|
||||
const uint8_t *in, size_t len) {
|
||||
if (len == 0) {
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t l = c->Nl + (((uint32_t)len) << 3);
|
||||
if (l < c->Nl) {
|
||||
uint32_t l = *Nl + (((uint32_t)len) << 3);
|
||||
if (l < *Nl) {
|
||||
// Handle carries.
|
||||
c->Nh++;
|
||||
(*Nh)++;
|
||||
}
|
||||
c->Nh += (uint32_t)(len >> 29);
|
||||
c->Nl = l;
|
||||
*Nh += (uint32_t)(len >> 29);
|
||||
*Nl = l;
|
||||
|
||||
size_t n = c->num;
|
||||
size_t n = *num;
|
||||
if (n != 0) {
|
||||
if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
|
||||
OPENSSL_memcpy(c->data + n, data, HASH_CBLOCK - n);
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
n = HASH_CBLOCK - n;
|
||||
data += n;
|
||||
if (len >= block_size || len + n >= block_size) {
|
||||
OPENSSL_memcpy(data + n, in, block_size - n);
|
||||
block_func(h, data, 1);
|
||||
n = block_size - n;
|
||||
in += n;
|
||||
len -= n;
|
||||
c->num = 0;
|
||||
// Keep |c->data| zeroed when unused.
|
||||
OPENSSL_memset(c->data, 0, HASH_CBLOCK);
|
||||
*num = 0;
|
||||
// Keep |data| zeroed when unused.
|
||||
OPENSSL_memset(data, 0, block_size);
|
||||
} else {
|
||||
OPENSSL_memcpy(c->data + n, data, len);
|
||||
c->num += (unsigned)len;
|
||||
return 1;
|
||||
OPENSSL_memcpy(data + n, in, len);
|
||||
*num += (unsigned)len;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
n = len / HASH_CBLOCK;
|
||||
n = len / block_size;
|
||||
if (n > 0) {
|
||||
HASH_BLOCK_DATA_ORDER(c->h, data, n);
|
||||
n *= HASH_CBLOCK;
|
||||
data += n;
|
||||
block_func(h, in, n);
|
||||
n *= block_size;
|
||||
in += n;
|
||||
len -= n;
|
||||
}
|
||||
|
||||
if (len != 0) {
|
||||
c->num = (unsigned)len;
|
||||
OPENSSL_memcpy(c->data, data, len);
|
||||
*num = (unsigned)len;
|
||||
OPENSSL_memcpy(data, in, len);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void HASH_TRANSFORM(HASH_CTX *c, const uint8_t data[HASH_CBLOCK]) {
|
||||
HASH_BLOCK_DATA_ORDER(c->h, data, 1);
|
||||
}
|
||||
|
||||
|
||||
int HASH_FINAL(uint8_t out[HASH_DIGEST_LENGTH], HASH_CTX *c) {
|
||||
// |c->data| always has room for at least one byte. A full block would have
|
||||
// crypto_md32_final incorporates the partial block and trailing length into the
|
||||
// digest state |h|. The trailing length is encoded in little-endian if
|
||||
// |is_big_endian| is zero and big-endian otherwise. |data| must be a buffer of
|
||||
// length |block_size| with the first |*num| bytes containing a partial block.
|
||||
// |Nh| and |Nl| contain the total number of bits processed. On return, this
|
||||
// function clears the partial block in |data| and
|
||||
// |*num|.
|
||||
//
|
||||
// This function does not serialize |h| into a final digest. This is the
|
||||
// responsibility of the caller.
|
||||
static inline void crypto_md32_final(crypto_md32_block_func block_func,
|
||||
uint32_t *h, uint8_t *data,
|
||||
size_t block_size, unsigned *num,
|
||||
uint32_t Nh, uint32_t Nl,
|
||||
int is_big_endian) {
|
||||
// |data| always has room for at least one byte. A full block would have
|
||||
// been consumed.
|
||||
size_t n = c->num;
|
||||
assert(n < HASH_CBLOCK);
|
||||
c->data[n] = 0x80;
|
||||
size_t n = *num;
|
||||
assert(n < block_size);
|
||||
data[n] = 0x80;
|
||||
n++;
|
||||
|
||||
// Fill the block with zeros if there isn't room for a 64-bit length.
|
||||
if (n > (HASH_CBLOCK - 8)) {
|
||||
OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - n);
|
||||
if (n > block_size - 8) {
|
||||
OPENSSL_memset(data + n, 0, block_size - n);
|
||||
n = 0;
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
block_func(h, data, 1);
|
||||
}
|
||||
OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
|
||||
OPENSSL_memset(data + n, 0, block_size - 8 - n);
|
||||
|
||||
// Append a 64-bit length to the block and process it.
|
||||
uint8_t *p = c->data + HASH_CBLOCK - 8;
|
||||
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
|
||||
HOST_l2c(c->Nh, p);
|
||||
HOST_l2c(c->Nl, p);
|
||||
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
|
||||
HOST_l2c(c->Nl, p);
|
||||
HOST_l2c(c->Nh, p);
|
||||
#endif
|
||||
assert(p == c->data + HASH_CBLOCK);
|
||||
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
|
||||
c->num = 0;
|
||||
OPENSSL_memset(c->data, 0, HASH_CBLOCK);
|
||||
|
||||
HASH_MAKE_STRING(c, out);
|
||||
return 1;
|
||||
if (is_big_endian) {
|
||||
CRYPTO_store_u32_be(data + block_size - 8, Nh);
|
||||
CRYPTO_store_u32_be(data + block_size - 4, Nl);
|
||||
} else {
|
||||
CRYPTO_store_u32_le(data + block_size - 8, Nl);
|
||||
CRYPTO_store_u32_le(data + block_size - 4, Nh);
|
||||
}
|
||||
block_func(h, data, 1);
|
||||
*num = 0;
|
||||
OPENSSL_memset(data, 0, block_size);
|
||||
}
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_DIGEST_MD32_COMMON_H
|
||||
@@ -797,6 +797,12 @@ int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
|
||||
const EC_POINT *point, BIGNUM *x, BIGNUM *y,
|
||||
BN_CTX *ctx) {
|
||||
return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx);
|
||||
}
|
||||
|
||||
void ec_affine_to_jacobian(const EC_GROUP *group, EC_RAW_POINT *out,
|
||||
const EC_AFFINE *p) {
|
||||
out->X = p->X;
|
||||
@@ -879,6 +885,12 @@ int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
|
||||
const BIGNUM *x, const BIGNUM *y,
|
||||
BN_CTX *ctx) {
|
||||
return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx);
|
||||
}
|
||||
|
||||
int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
|
||||
const EC_POINT *b, BN_CTX *ctx) {
|
||||
if (EC_GROUP_cmp(group, r->group, NULL) != 0 ||
|
||||
@@ -1220,6 +1232,10 @@ void ec_set_to_safe_point(const EC_GROUP *group, EC_RAW_POINT *out) {
|
||||
|
||||
void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) {}
|
||||
|
||||
int EC_GROUP_get_asn1_flag(const EC_GROUP *group) {
|
||||
return OPENSSL_EC_NAMED_CURVE;
|
||||
}
|
||||
|
||||
const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) {
|
||||
// This function exists purely to give callers a way to call
|
||||
// |EC_METHOD_get_field_type|. cryptography.io crashes if |EC_GROUP_method_of|
|
||||
|
||||
@@ -171,7 +171,6 @@ void EC_KEY_free(EC_KEY *r) {
|
||||
EC_GROUP_free(r->group);
|
||||
EC_POINT_free(r->pub_key);
|
||||
ec_wrapped_scalar_free(r->priv_key);
|
||||
BN_free(r->fixed_k);
|
||||
|
||||
CRYPTO_free_ex_data(g_ec_ex_data_class_bss_get(), r, &r->ex_data);
|
||||
|
||||
@@ -440,7 +439,15 @@ int EC_KEY_generate_key(EC_KEY *key) {
|
||||
}
|
||||
|
||||
int EC_KEY_generate_key_fips(EC_KEY *eckey) {
|
||||
return EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey);
|
||||
if (EC_KEY_generate_key(eckey) && EC_KEY_check_fips(eckey)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
EC_POINT_free(eckey->pub_key);
|
||||
ec_wrapped_scalar_free(eckey->priv_key);
|
||||
eckey->pub_key = NULL;
|
||||
eckey->priv_key = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EC_KEY_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
|
||||
|
||||
@@ -729,10 +729,6 @@ struct ec_key_st {
|
||||
EC_POINT *pub_key;
|
||||
EC_WRAPPED_SCALAR *priv_key;
|
||||
|
||||
// fixed_k may contain a specific value of 'k', to be used in ECDSA signing.
|
||||
// This is only for the FIPS power-on tests.
|
||||
BIGNUM *fixed_k;
|
||||
|
||||
unsigned int enc_flag;
|
||||
point_conversion_form_t conv_form;
|
||||
|
||||
|
||||
@@ -61,9 +61,10 @@
|
||||
#include <CCryptoBoringSSL_sha.h>
|
||||
#include <CCryptoBoringSSL_type_check.h>
|
||||
|
||||
#include "../../internal.h"
|
||||
#include "../bn/internal.h"
|
||||
#include "../ec/internal.h"
|
||||
#include "../../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
// digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
|
||||
@@ -198,68 +199,104 @@ int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecdsa_sign_setup(const EC_KEY *eckey, EC_SCALAR *out_kinv_mont,
|
||||
EC_SCALAR *out_r, const uint8_t *digest,
|
||||
size_t digest_len, const EC_SCALAR *priv_key) {
|
||||
static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry,
|
||||
const EC_SCALAR *priv_key, const EC_SCALAR *k,
|
||||
const uint8_t *digest, size_t digest_len) {
|
||||
*out_retry = 0;
|
||||
|
||||
// Check that the size of the group order is FIPS compliant (FIPS 186-4
|
||||
// B.5.2).
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
if (BN_num_bits(order) < 160) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER);
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
EC_SCALAR k;
|
||||
// Compute r, the x-coordinate of k * generator.
|
||||
EC_RAW_POINT tmp_point;
|
||||
do {
|
||||
// Include the private key and message digest in the k generation.
|
||||
if (eckey->fixed_k != NULL) {
|
||||
if (!ec_bignum_to_scalar(group, &k, eckey->fixed_k)) {
|
||||
goto err;
|
||||
}
|
||||
if (ec_scalar_is_zero(group, &k)) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
// Pass a SHA512 hash of the private key and digest as additional data
|
||||
// into the RBG. This is a hardening measure against entropy failure.
|
||||
OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
|
||||
"additional_data is too large for SHA-512");
|
||||
SHA512_CTX sha;
|
||||
uint8_t additional_data[SHA512_DIGEST_LENGTH];
|
||||
SHA512_Init(&sha);
|
||||
SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
|
||||
SHA512_Update(&sha, digest, digest_len);
|
||||
SHA512_Final(additional_data, &sha);
|
||||
if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
EC_SCALAR r;
|
||||
if (!ec_point_mul_scalar_base(group, &tmp_point, k) ||
|
||||
!ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute k^-1 in the Montgomery domain. This is |ec_scalar_to_montgomery|
|
||||
// followed by |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery|
|
||||
// followed by |ec_scalar_from_montgomery| is equivalent and slightly more
|
||||
// efficient. Note k is non-zero, so the inverse must exist.
|
||||
ec_scalar_inv0_montgomery(group, out_kinv_mont, &k);
|
||||
ec_scalar_from_montgomery(group, out_kinv_mont, out_kinv_mont);
|
||||
if (ec_scalar_is_zero(group, &r)) {
|
||||
*out_retry = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute r, the x-coordinate of generator * k.
|
||||
if (!ec_point_mul_scalar_base(group, &tmp_point, &k) ||
|
||||
!ec_get_x_coordinate_as_scalar(group, out_r, &tmp_point)) {
|
||||
goto err;
|
||||
}
|
||||
} while (ec_scalar_is_zero(group, out_r));
|
||||
// s = priv_key * r. Note if only one parameter is in the Montgomery domain,
|
||||
// |ec_scalar_mod_mul_montgomery| will compute the answer in the normal
|
||||
// domain.
|
||||
EC_SCALAR s;
|
||||
ec_scalar_to_montgomery(group, &s, &r);
|
||||
ec_scalar_mul_montgomery(group, &s, priv_key, &s);
|
||||
|
||||
ret = 1;
|
||||
// s = m + priv_key * r.
|
||||
EC_SCALAR tmp;
|
||||
digest_to_scalar(group, &tmp, digest, digest_len);
|
||||
ec_scalar_add(group, &s, &s, &tmp);
|
||||
|
||||
err:
|
||||
OPENSSL_cleanse(&k, sizeof(k));
|
||||
// s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery
|
||||
// domain. This is |ec_scalar_to_montgomery| followed by
|
||||
// |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by
|
||||
// |ec_scalar_from_montgomery| is equivalent and slightly more efficient.
|
||||
// Then, as above, only one parameter is in the Montgomery domain, so the
|
||||
// result is in the normal domain. Finally, note k is non-zero (or computing r
|
||||
// would fail), so the inverse must exist.
|
||||
ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2
|
||||
ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R
|
||||
ec_scalar_mul_montgomery(group, &s, &s, &tmp);
|
||||
if (ec_scalar_is_zero(group, &s)) {
|
||||
*out_retry = 1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ret = ECDSA_SIG_new();
|
||||
if (ret == NULL || //
|
||||
!bn_set_words(ret->r, r.words, order->width) ||
|
||||
!bn_set_words(ret->s, s.words, order->width)) {
|
||||
ECDSA_SIG_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest,
|
||||
size_t digest_len,
|
||||
const EC_KEY *eckey,
|
||||
const uint8_t *nonce,
|
||||
size_t nonce_len) {
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EC_GROUP *group = EC_KEY_get0_group(eckey);
|
||||
if (group == NULL || eckey->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
|
||||
|
||||
EC_SCALAR k;
|
||||
if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) {
|
||||
return NULL;
|
||||
}
|
||||
int retry_ignored;
|
||||
return ecdsa_sign_impl(group, &retry_ignored, priv_key, &k, digest,
|
||||
digest_len);
|
||||
}
|
||||
|
||||
// This function is only exported for testing and is not called in production
|
||||
// code.
|
||||
ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing(
|
||||
const uint8_t *digest, size_t digest_len, const EC_KEY *eckey,
|
||||
const uint8_t *nonce, size_t nonce_len) {
|
||||
return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey,
|
||||
nonce, nonce_len);
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
|
||||
const EC_KEY *eckey) {
|
||||
if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
|
||||
@@ -275,54 +312,28 @@ ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
|
||||
|
||||
int ok = 0;
|
||||
ECDSA_SIG *ret = ECDSA_SIG_new();
|
||||
EC_SCALAR kinv_mont, r_mont, s, m, tmp;
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
// Pass a SHA512 hash of the private key and digest as additional data
|
||||
// into the RBG. This is a hardening measure against entropy failure.
|
||||
OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
|
||||
"additional_data is too large for SHA-512");
|
||||
SHA512_CTX sha;
|
||||
uint8_t additional_data[SHA512_DIGEST_LENGTH];
|
||||
SHA512_Init(&sha);
|
||||
SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
|
||||
SHA512_Update(&sha, digest, digest_len);
|
||||
SHA512_Final(additional_data, &sha);
|
||||
|
||||
digest_to_scalar(group, &m, digest, digest_len);
|
||||
for (;;) {
|
||||
if (!ecdsa_sign_setup(eckey, &kinv_mont, &r_mont, digest, digest_len,
|
||||
priv_key) ||
|
||||
!bn_set_words(ret->r, r_mont.words, order->width)) {
|
||||
goto err;
|
||||
EC_SCALAR k;
|
||||
if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Compute priv_key * r (mod order). Note if only one parameter is in the
|
||||
// Montgomery domain, |ec_scalar_mod_mul_montgomery| will compute the answer
|
||||
// in the normal domain.
|
||||
ec_scalar_to_montgomery(group, &r_mont, &r_mont);
|
||||
ec_scalar_mul_montgomery(group, &s, priv_key, &r_mont);
|
||||
|
||||
// Compute tmp = m + priv_key * r.
|
||||
ec_scalar_add(group, &tmp, &m, &s);
|
||||
|
||||
// Finally, multiply s by k^-1. That was retained in Montgomery form, so the
|
||||
// same technique as the previous multiplication works.
|
||||
ec_scalar_mul_montgomery(group, &s, &tmp, &kinv_mont);
|
||||
if (!bn_set_words(ret->s, s.words, order->width)) {
|
||||
goto err;
|
||||
}
|
||||
if (!BN_is_zero(ret->s)) {
|
||||
// s != 0 => we have a valid signature
|
||||
break;
|
||||
int retry;
|
||||
ECDSA_SIG *sig =
|
||||
ecdsa_sign_impl(group, &retry, priv_key, &k, digest, digest_len);
|
||||
if (sig != NULL || !retry) {
|
||||
return sig;
|
||||
}
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
ECDSA_SIG_free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
OPENSSL_cleanse(&kinv_mont, sizeof(kinv_mont));
|
||||
OPENSSL_cleanse(&r_mont, sizeof(r_mont));
|
||||
OPENSSL_cleanse(&s, sizeof(s));
|
||||
OPENSSL_cleanse(&tmp, sizeof(tmp));
|
||||
OPENSSL_cleanse(&m, sizeof(m));
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2021, Google Inc.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
#define OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// ecdsa_sign_with_nonce_for_known_answer_test behaves like |ECDSA_do_sign| but
|
||||
// takes a fixed nonce. This function is used as part of known-answer tests in
|
||||
// the FIPS module.
|
||||
ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest,
|
||||
size_t digest_len,
|
||||
const EC_KEY *eckey,
|
||||
const uint8_t *nonce,
|
||||
size_t nonce_len);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_CRYPTO_FIPSMODULE_ECDSA_INTERNAL_H
|
||||
@@ -0,0 +1,298 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.text
|
||||
.globl _gcm_gmult_ssse3
|
||||
.hidden _gcm_gmult_ssse3
|
||||
.def _gcm_gmult_ssse3; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_gcm_gmult_ssse3:
|
||||
.L_gcm_gmult_ssse3_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
movl 20(%esp),%edi
|
||||
movl 24(%esp),%esi
|
||||
movdqu (%edi),%xmm0
|
||||
call .L000pic_point
|
||||
.L000pic_point:
|
||||
popl %eax
|
||||
movdqa .Lreverse_bytes-.L000pic_point(%eax),%xmm7
|
||||
movdqa .Llow4_mask-.L000pic_point(%eax),%xmm2
|
||||
.byte 102,15,56,0,199
|
||||
movdqa %xmm2,%xmm1
|
||||
pandn %xmm0,%xmm1
|
||||
psrld $4,%xmm1
|
||||
pand %xmm2,%xmm0
|
||||
pxor %xmm2,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movl $5,%eax
|
||||
.L001loop_row_1:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L001loop_row_1
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movl $5,%eax
|
||||
.L002loop_row_2:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L002loop_row_2
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movl $6,%eax
|
||||
.L003loop_row_3:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L003loop_row_3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
.byte 102,15,56,0,215
|
||||
movdqu %xmm2,(%edi)
|
||||
pxor %xmm0,%xmm0
|
||||
pxor %xmm1,%xmm1
|
||||
pxor %xmm2,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
pxor %xmm4,%xmm4
|
||||
pxor %xmm5,%xmm5
|
||||
pxor %xmm6,%xmm6
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.globl _gcm_ghash_ssse3
|
||||
.hidden _gcm_ghash_ssse3
|
||||
.def _gcm_ghash_ssse3; .scl 2; .type 32; .endef
|
||||
.align 16
|
||||
_gcm_ghash_ssse3:
|
||||
.L_gcm_ghash_ssse3_begin:
|
||||
pushl %ebp
|
||||
pushl %ebx
|
||||
pushl %esi
|
||||
pushl %edi
|
||||
movl 20(%esp),%edi
|
||||
movl 24(%esp),%esi
|
||||
movl 28(%esp),%edx
|
||||
movl 32(%esp),%ecx
|
||||
movdqu (%edi),%xmm0
|
||||
call .L004pic_point
|
||||
.L004pic_point:
|
||||
popl %ebx
|
||||
movdqa .Lreverse_bytes-.L004pic_point(%ebx),%xmm7
|
||||
andl $-16,%ecx
|
||||
.byte 102,15,56,0,199
|
||||
pxor %xmm3,%xmm3
|
||||
.L005loop_ghash:
|
||||
movdqa .Llow4_mask-.L004pic_point(%ebx),%xmm2
|
||||
movdqu (%edx),%xmm1
|
||||
.byte 102,15,56,0,207
|
||||
pxor %xmm1,%xmm0
|
||||
movdqa %xmm2,%xmm1
|
||||
pandn %xmm0,%xmm1
|
||||
psrld $4,%xmm1
|
||||
pand %xmm2,%xmm0
|
||||
pxor %xmm2,%xmm2
|
||||
movl $5,%eax
|
||||
.L006loop_row_4:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L006loop_row_4
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movl $5,%eax
|
||||
.L007loop_row_5:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L007loop_row_5
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movl $6,%eax
|
||||
.L008loop_row_6:
|
||||
movdqa (%esi),%xmm4
|
||||
leal 16(%esi),%esi
|
||||
movdqa %xmm2,%xmm6
|
||||
.byte 102,15,58,15,243,1
|
||||
movdqa %xmm6,%xmm3
|
||||
psrldq $1,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
.byte 102,15,56,0,224
|
||||
.byte 102,15,56,0,233
|
||||
pxor %xmm5,%xmm2
|
||||
movdqa %xmm4,%xmm5
|
||||
psllq $60,%xmm5
|
||||
movdqa %xmm5,%xmm6
|
||||
pslldq $8,%xmm6
|
||||
pxor %xmm6,%xmm3
|
||||
psrldq $8,%xmm5
|
||||
pxor %xmm5,%xmm2
|
||||
psrlq $4,%xmm4
|
||||
pxor %xmm4,%xmm2
|
||||
subl $1,%eax
|
||||
jnz .L008loop_row_6
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $1,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
psrlq $5,%xmm3
|
||||
pxor %xmm3,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
movdqa %xmm2,%xmm0
|
||||
leal -256(%esi),%esi
|
||||
leal 16(%edx),%edx
|
||||
subl $16,%ecx
|
||||
jnz .L005loop_ghash
|
||||
.byte 102,15,56,0,199
|
||||
movdqu %xmm0,(%edi)
|
||||
pxor %xmm0,%xmm0
|
||||
pxor %xmm1,%xmm1
|
||||
pxor %xmm2,%xmm2
|
||||
pxor %xmm3,%xmm3
|
||||
pxor %xmm4,%xmm4
|
||||
pxor %xmm5,%xmm5
|
||||
pxor %xmm6,%xmm6
|
||||
popl %edi
|
||||
popl %esi
|
||||
popl %ebx
|
||||
popl %ebp
|
||||
ret
|
||||
.align 16
|
||||
.Lreverse_bytes:
|
||||
.byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0
|
||||
.align 16
|
||||
.Llow4_mask:
|
||||
.long 252645135,252645135,252645135,252645135
|
||||
#endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#define BORINGSSL_PREFIX CCryptoBoringSSL
|
||||
#if defined(__i386__) && defined(_WIN32)
|
||||
# This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
# source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__i386__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.text
|
||||
.byte 71,72,65,83,72,32,102,111,114,32,120,56,54,44,32,67
|
||||
.byte 82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112
|
||||
.byte 112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62
|
||||
.byte 0
|
||||
#endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#if __ARM_MAX_ARCH__>=7
|
||||
.text
|
||||
|
||||
.code 32
|
||||
@@ -27,6 +28,7 @@
|
||||
#endif
|
||||
.align 4
|
||||
_gcm_init_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vld1.64 {q9},[r1] @ load input H
|
||||
vmov.i8 q11,#0xe1
|
||||
vshl.i64 q11,q11,#57 @ 0xc2.0
|
||||
@@ -69,8 +71,7 @@ _gcm_init_v8:
|
||||
vext.8 q9,q14,q14,#8 @ Karatsuba pre-processing
|
||||
veor q9,q9,q14
|
||||
vext.8 q13,q8,q9,#8 @ pack Karatsuba pre-processed
|
||||
vst1.64 {q13,q14},[r0] @ store Htable[1..2]
|
||||
|
||||
vst1.64 {q13,q14},[r0]! @ store Htable[1..2]
|
||||
bx lr
|
||||
|
||||
.globl _gcm_gmult_v8
|
||||
@@ -80,6 +81,7 @@ _gcm_init_v8:
|
||||
#endif
|
||||
.align 4
|
||||
_gcm_gmult_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vld1.64 {q9},[r0] @ load Xi
|
||||
vmov.i8 q11,#0xe1
|
||||
vld1.64 {q12,q13},[r1] @ load twisted H, ...
|
||||
@@ -124,6 +126,7 @@ _gcm_gmult_v8:
|
||||
#endif
|
||||
.align 4
|
||||
_gcm_ghash_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vstmdb sp!,{d8,d9,d10,d11,d12,d13,d14,d15} @ 32-bit ABI says so
|
||||
vld1.64 {q0},[r0] @ load [rotated] Xi
|
||||
@ "[rotated]" means that
|
||||
@@ -255,6 +258,7 @@ Ldone_v8:
|
||||
.byte 71,72,65,83,72,32,102,111,114,32,65,82,77,118,56,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.align 2
|
||||
.align 2
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
#endif // defined(__arm__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#if __ARM_MAX_ARCH__>=7
|
||||
.text
|
||||
.fpu neon
|
||||
.code 32
|
||||
@@ -26,6 +27,7 @@
|
||||
.type gcm_init_v8,%function
|
||||
.align 4
|
||||
gcm_init_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vld1.64 {q9},[r1] @ load input H
|
||||
vmov.i8 q11,#0xe1
|
||||
vshl.i64 q11,q11,#57 @ 0xc2.0
|
||||
@@ -68,8 +70,7 @@ gcm_init_v8:
|
||||
vext.8 q9,q14,q14,#8 @ Karatsuba pre-processing
|
||||
veor q9,q9,q14
|
||||
vext.8 q13,q8,q9,#8 @ pack Karatsuba pre-processed
|
||||
vst1.64 {q13,q14},[r0] @ store Htable[1..2]
|
||||
|
||||
vst1.64 {q13,q14},[r0]! @ store Htable[1..2]
|
||||
bx lr
|
||||
.size gcm_init_v8,.-gcm_init_v8
|
||||
.globl gcm_gmult_v8
|
||||
@@ -77,6 +78,7 @@ gcm_init_v8:
|
||||
.type gcm_gmult_v8,%function
|
||||
.align 4
|
||||
gcm_gmult_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vld1.64 {q9},[r0] @ load Xi
|
||||
vmov.i8 q11,#0xe1
|
||||
vld1.64 {q12,q13},[r1] @ load twisted H, ...
|
||||
@@ -119,6 +121,7 @@ gcm_gmult_v8:
|
||||
.type gcm_ghash_v8,%function
|
||||
.align 4
|
||||
gcm_ghash_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
vstmdb sp!,{d8,d9,d10,d11,d12,d13,d14,d15} @ 32-bit ABI says so
|
||||
vld1.64 {q0},[r0] @ load [rotated] Xi
|
||||
@ "[rotated]" means that
|
||||
@@ -251,6 +254,7 @@ gcm_ghash_v8:
|
||||
.align 2
|
||||
.align 2
|
||||
#endif
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif // defined(__arm__) && defined(__linux__)
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#if __ARM_MAX_ARCH__>=7
|
||||
.text
|
||||
|
||||
.globl _gcm_init_v8
|
||||
@@ -66,8 +67,48 @@ _gcm_init_v8:
|
||||
ext v17.16b,v22.16b,v22.16b,#8 //Karatsuba pre-processing
|
||||
eor v17.16b,v17.16b,v22.16b
|
||||
ext v21.16b,v16.16b,v17.16b,#8 //pack Karatsuba pre-processed
|
||||
st1 {v21.2d,v22.2d},[x0] //store Htable[1..2]
|
||||
st1 {v21.2d,v22.2d},[x0],#32 //store Htable[1..2]
|
||||
//calculate H^3 and H^4
|
||||
pmull v0.1q,v20.1d, v22.1d
|
||||
pmull v5.1q,v22.1d,v22.1d
|
||||
pmull2 v2.1q,v20.2d, v22.2d
|
||||
pmull2 v7.1q,v22.2d,v22.2d
|
||||
pmull v1.1q,v16.1d,v17.1d
|
||||
pmull v6.1q,v17.1d,v17.1d
|
||||
|
||||
ext v16.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
ext v17.16b,v5.16b,v7.16b,#8
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
eor v1.16b,v1.16b,v16.16b
|
||||
eor v4.16b,v5.16b,v7.16b
|
||||
eor v6.16b,v6.16b,v17.16b
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase
|
||||
eor v6.16b,v6.16b,v4.16b
|
||||
pmull v4.1q,v5.1d,v19.1d
|
||||
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v7.d[0],v6.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
ins v6.d[1],v5.d[0]
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
eor v5.16b,v6.16b,v4.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase
|
||||
ext v4.16b,v5.16b,v5.16b,#8
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
pmull v5.1q,v5.1d,v19.1d
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
eor v4.16b,v4.16b,v7.16b
|
||||
eor v20.16b, v0.16b,v18.16b //H^3
|
||||
eor v22.16b,v5.16b,v4.16b //H^4
|
||||
|
||||
ext v16.16b,v20.16b, v20.16b,#8 //Karatsuba pre-processing
|
||||
ext v17.16b,v22.16b,v22.16b,#8
|
||||
eor v16.16b,v16.16b,v20.16b
|
||||
eor v17.16b,v17.16b,v22.16b
|
||||
ext v21.16b,v16.16b,v17.16b,#8 //pack Karatsuba pre-processed
|
||||
st1 {v20.2d,v21.2d,v22.2d},[x0] //store Htable[3..5]
|
||||
ret
|
||||
|
||||
.globl _gcm_gmult_v8
|
||||
@@ -119,6 +160,8 @@ _gcm_gmult_v8:
|
||||
.align 4
|
||||
_gcm_ghash_v8:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
cmp x3,#64
|
||||
b.hs Lgcm_ghash_v8_4x
|
||||
ld1 {v0.2d},[x0] //load [rotated] Xi
|
||||
//"[rotated]" means that
|
||||
//loaded value would have
|
||||
@@ -245,9 +288,290 @@ Ldone_v8:
|
||||
|
||||
ret
|
||||
|
||||
|
||||
.align 4
|
||||
gcm_ghash_v8_4x:
|
||||
Lgcm_ghash_v8_4x:
|
||||
ld1 {v0.2d},[x0] //load [rotated] Xi
|
||||
ld1 {v20.2d,v21.2d,v22.2d},[x1],#48 //load twisted H, ..., H^2
|
||||
movi v19.16b,#0xe1
|
||||
ld1 {v26.2d,v27.2d,v28.2d},[x1] //load twisted H^3, ..., H^4
|
||||
shl v19.2d,v19.2d,#57 //compose 0xc2.0 constant
|
||||
|
||||
ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64
|
||||
#ifndef __ARMEB__
|
||||
rev64 v0.16b,v0.16b
|
||||
rev64 v5.16b,v5.16b
|
||||
rev64 v6.16b,v6.16b
|
||||
rev64 v7.16b,v7.16b
|
||||
rev64 v4.16b,v4.16b
|
||||
#endif
|
||||
ext v25.16b,v7.16b,v7.16b,#8
|
||||
ext v24.16b,v6.16b,v6.16b,#8
|
||||
ext v23.16b,v5.16b,v5.16b,#8
|
||||
|
||||
pmull v29.1q,v20.1d,v25.1d //H·Ii+3
|
||||
eor v7.16b,v7.16b,v25.16b
|
||||
pmull2 v31.1q,v20.2d,v25.2d
|
||||
pmull v30.1q,v21.1d,v7.1d
|
||||
|
||||
pmull v16.1q,v22.1d,v24.1d //H^2·Ii+2
|
||||
eor v6.16b,v6.16b,v24.16b
|
||||
pmull2 v24.1q,v22.2d,v24.2d
|
||||
pmull2 v6.1q,v21.2d,v6.2d
|
||||
|
||||
eor v29.16b,v29.16b,v16.16b
|
||||
eor v31.16b,v31.16b,v24.16b
|
||||
eor v30.16b,v30.16b,v6.16b
|
||||
|
||||
pmull v7.1q,v26.1d,v23.1d //H^3·Ii+1
|
||||
eor v5.16b,v5.16b,v23.16b
|
||||
pmull2 v23.1q,v26.2d,v23.2d
|
||||
pmull v5.1q,v27.1d,v5.1d
|
||||
|
||||
eor v29.16b,v29.16b,v7.16b
|
||||
eor v31.16b,v31.16b,v23.16b
|
||||
eor v30.16b,v30.16b,v5.16b
|
||||
|
||||
subs x3,x3,#128
|
||||
b.lo Ltail4x
|
||||
|
||||
b Loop4x
|
||||
|
||||
.align 4
|
||||
Loop4x:
|
||||
eor v16.16b,v4.16b,v0.16b
|
||||
ld1 {v4.2d,v5.2d,v6.2d,v7.2d},[x2],#64
|
||||
ext v3.16b,v16.16b,v16.16b,#8
|
||||
#ifndef __ARMEB__
|
||||
rev64 v5.16b,v5.16b
|
||||
rev64 v6.16b,v6.16b
|
||||
rev64 v7.16b,v7.16b
|
||||
rev64 v4.16b,v4.16b
|
||||
#endif
|
||||
|
||||
pmull v0.1q,v28.1d,v3.1d //H^4·(Xi+Ii)
|
||||
eor v16.16b,v16.16b,v3.16b
|
||||
pmull2 v2.1q,v28.2d,v3.2d
|
||||
ext v25.16b,v7.16b,v7.16b,#8
|
||||
pmull2 v1.1q,v27.2d,v16.2d
|
||||
|
||||
eor v0.16b,v0.16b,v29.16b
|
||||
eor v2.16b,v2.16b,v31.16b
|
||||
ext v24.16b,v6.16b,v6.16b,#8
|
||||
eor v1.16b,v1.16b,v30.16b
|
||||
ext v23.16b,v5.16b,v5.16b,#8
|
||||
|
||||
ext v17.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
pmull v29.1q,v20.1d,v25.1d //H·Ii+3
|
||||
eor v7.16b,v7.16b,v25.16b
|
||||
eor v1.16b,v1.16b,v17.16b
|
||||
pmull2 v31.1q,v20.2d,v25.2d
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
pmull v30.1q,v21.1d,v7.1d
|
||||
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase of reduction
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
pmull v16.1q,v22.1d,v24.1d //H^2·Ii+2
|
||||
eor v6.16b,v6.16b,v24.16b
|
||||
pmull2 v24.1q,v22.2d,v24.2d
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
pmull2 v6.1q,v21.2d,v6.2d
|
||||
|
||||
eor v29.16b,v29.16b,v16.16b
|
||||
eor v31.16b,v31.16b,v24.16b
|
||||
eor v30.16b,v30.16b,v6.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase of reduction
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
pmull v7.1q,v26.1d,v23.1d //H^3·Ii+1
|
||||
eor v5.16b,v5.16b,v23.16b
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
pmull2 v23.1q,v26.2d,v23.2d
|
||||
pmull v5.1q,v27.1d,v5.1d
|
||||
|
||||
eor v0.16b,v0.16b,v18.16b
|
||||
eor v29.16b,v29.16b,v7.16b
|
||||
eor v31.16b,v31.16b,v23.16b
|
||||
ext v0.16b,v0.16b,v0.16b,#8
|
||||
eor v30.16b,v30.16b,v5.16b
|
||||
|
||||
subs x3,x3,#64
|
||||
b.hs Loop4x
|
||||
|
||||
Ltail4x:
|
||||
eor v16.16b,v4.16b,v0.16b
|
||||
ext v3.16b,v16.16b,v16.16b,#8
|
||||
|
||||
pmull v0.1q,v28.1d,v3.1d //H^4·(Xi+Ii)
|
||||
eor v16.16b,v16.16b,v3.16b
|
||||
pmull2 v2.1q,v28.2d,v3.2d
|
||||
pmull2 v1.1q,v27.2d,v16.2d
|
||||
|
||||
eor v0.16b,v0.16b,v29.16b
|
||||
eor v2.16b,v2.16b,v31.16b
|
||||
eor v1.16b,v1.16b,v30.16b
|
||||
|
||||
adds x3,x3,#64
|
||||
b.eq Ldone4x
|
||||
|
||||
cmp x3,#32
|
||||
b.lo Lone
|
||||
b.eq Ltwo
|
||||
Lthree:
|
||||
ext v17.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
eor v1.16b,v1.16b,v17.16b
|
||||
ld1 {v4.2d,v5.2d,v6.2d},[x2]
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
#ifndef __ARMEB__
|
||||
rev64 v5.16b,v5.16b
|
||||
rev64 v6.16b,v6.16b
|
||||
rev64 v4.16b,v4.16b
|
||||
#endif
|
||||
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase of reduction
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
ext v24.16b,v6.16b,v6.16b,#8
|
||||
ext v23.16b,v5.16b,v5.16b,#8
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
|
||||
pmull v29.1q,v20.1d,v24.1d //H·Ii+2
|
||||
eor v6.16b,v6.16b,v24.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase of reduction
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
pmull2 v31.1q,v20.2d,v24.2d
|
||||
pmull v30.1q,v21.1d,v6.1d
|
||||
eor v0.16b,v0.16b,v18.16b
|
||||
pmull v7.1q,v22.1d,v23.1d //H^2·Ii+1
|
||||
eor v5.16b,v5.16b,v23.16b
|
||||
ext v0.16b,v0.16b,v0.16b,#8
|
||||
|
||||
pmull2 v23.1q,v22.2d,v23.2d
|
||||
eor v16.16b,v4.16b,v0.16b
|
||||
pmull2 v5.1q,v21.2d,v5.2d
|
||||
ext v3.16b,v16.16b,v16.16b,#8
|
||||
|
||||
eor v29.16b,v29.16b,v7.16b
|
||||
eor v31.16b,v31.16b,v23.16b
|
||||
eor v30.16b,v30.16b,v5.16b
|
||||
|
||||
pmull v0.1q,v26.1d,v3.1d //H^3·(Xi+Ii)
|
||||
eor v16.16b,v16.16b,v3.16b
|
||||
pmull2 v2.1q,v26.2d,v3.2d
|
||||
pmull v1.1q,v27.1d,v16.1d
|
||||
|
||||
eor v0.16b,v0.16b,v29.16b
|
||||
eor v2.16b,v2.16b,v31.16b
|
||||
eor v1.16b,v1.16b,v30.16b
|
||||
b Ldone4x
|
||||
|
||||
.align 4
|
||||
Ltwo:
|
||||
ext v17.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
eor v1.16b,v1.16b,v17.16b
|
||||
ld1 {v4.2d,v5.2d},[x2]
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
#ifndef __ARMEB__
|
||||
rev64 v5.16b,v5.16b
|
||||
rev64 v4.16b,v4.16b
|
||||
#endif
|
||||
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase of reduction
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
ext v23.16b,v5.16b,v5.16b,#8
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase of reduction
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
eor v0.16b,v0.16b,v18.16b
|
||||
ext v0.16b,v0.16b,v0.16b,#8
|
||||
|
||||
pmull v29.1q,v20.1d,v23.1d //H·Ii+1
|
||||
eor v5.16b,v5.16b,v23.16b
|
||||
|
||||
eor v16.16b,v4.16b,v0.16b
|
||||
ext v3.16b,v16.16b,v16.16b,#8
|
||||
|
||||
pmull2 v31.1q,v20.2d,v23.2d
|
||||
pmull v30.1q,v21.1d,v5.1d
|
||||
|
||||
pmull v0.1q,v22.1d,v3.1d //H^2·(Xi+Ii)
|
||||
eor v16.16b,v16.16b,v3.16b
|
||||
pmull2 v2.1q,v22.2d,v3.2d
|
||||
pmull2 v1.1q,v21.2d,v16.2d
|
||||
|
||||
eor v0.16b,v0.16b,v29.16b
|
||||
eor v2.16b,v2.16b,v31.16b
|
||||
eor v1.16b,v1.16b,v30.16b
|
||||
b Ldone4x
|
||||
|
||||
.align 4
|
||||
Lone:
|
||||
ext v17.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
eor v1.16b,v1.16b,v17.16b
|
||||
ld1 {v4.2d},[x2]
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
#ifndef __ARMEB__
|
||||
rev64 v4.16b,v4.16b
|
||||
#endif
|
||||
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase of reduction
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase of reduction
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
eor v0.16b,v0.16b,v18.16b
|
||||
ext v0.16b,v0.16b,v0.16b,#8
|
||||
|
||||
eor v16.16b,v4.16b,v0.16b
|
||||
ext v3.16b,v16.16b,v16.16b,#8
|
||||
|
||||
pmull v0.1q,v20.1d,v3.1d
|
||||
eor v16.16b,v16.16b,v3.16b
|
||||
pmull2 v2.1q,v20.2d,v3.2d
|
||||
pmull v1.1q,v21.1d,v16.1d
|
||||
|
||||
Ldone4x:
|
||||
ext v17.16b,v0.16b,v2.16b,#8 //Karatsuba post-processing
|
||||
eor v18.16b,v0.16b,v2.16b
|
||||
eor v1.16b,v1.16b,v17.16b
|
||||
eor v1.16b,v1.16b,v18.16b
|
||||
|
||||
pmull v18.1q,v0.1d,v19.1d //1st phase of reduction
|
||||
ins v2.d[0],v1.d[1]
|
||||
ins v1.d[1],v0.d[0]
|
||||
eor v0.16b,v1.16b,v18.16b
|
||||
|
||||
ext v18.16b,v0.16b,v0.16b,#8 //2nd phase of reduction
|
||||
pmull v0.1q,v0.1d,v19.1d
|
||||
eor v18.16b,v18.16b,v2.16b
|
||||
eor v0.16b,v0.16b,v18.16b
|
||||
ext v0.16b,v0.16b,v0.16b,#8
|
||||
|
||||
#ifndef __ARMEB__
|
||||
rev64 v0.16b,v0.16b
|
||||
#endif
|
||||
st1 {v0.2d},[x0] //write out Xi
|
||||
|
||||
ret
|
||||
|
||||
.byte 71,72,65,83,72,32,102,111,114,32,65,82,77,118,56,44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32,60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111,114,103,62,0
|
||||
.align 2
|
||||
.align 2
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
#endif // defined(__aarch64__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
|
||||
Loaded 100 of 349 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user