mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60f13f60c4 | ||
|
|
b71d0c1c9b | ||
|
|
ac03330c12 | ||
|
|
940a631a68 | ||
|
|
dcf0815f8f | ||
|
|
33a20e650c | ||
|
|
65f8c60ab3 | ||
|
|
1863cc9dcf | ||
|
|
6d9c36b4be | ||
|
|
a56d70a286 | ||
|
|
3ea0bdb8fd | ||
|
|
d1047c1c9a | ||
|
|
401a7a8ba7 | ||
|
|
944428373d | ||
|
|
5a1b404183 | ||
|
|
3c4009af05 | ||
|
|
22a06540cc | ||
|
|
7d7419ebba | ||
|
|
f2125b625f | ||
|
|
959c1fe152 | ||
|
|
36d5ec4458 | ||
|
|
6be84be0bd | ||
|
|
25a68d63b7 | ||
|
|
5d8b14d227 | ||
|
|
da0fe44138 | ||
|
|
80a6e3edc1 | ||
|
|
a1b800581e | ||
|
|
935c74be2f | ||
|
|
75ec60b8b4 | ||
|
|
d330d21edf | ||
|
|
9cc89f0170 | ||
|
|
92a04c10fc |
No files matched your search
+1
-53
@@ -1,55 +1,3 @@
|
||||
# Code of Conduct
|
||||
To be a truly great community, swift-crypto needs to welcome developers from all walks of life,
|
||||
with different backgrounds, and with a wide range of experience. A diverse and friendly
|
||||
community will have more great ideas, more unique perspectives, and produce more great
|
||||
code. We will work diligently to make the swift-crypto community welcoming to everyone.
|
||||
|
||||
To give clarity of what is expected of our members, swift-crypto has adopted the code of conduct
|
||||
defined by [contributor-covenant.org](https://www.contributor-covenant.org). This document is used across many open source
|
||||
communities, and we think it articulates our values well. The full text is copied below:
|
||||
|
||||
### Contributor Code of Conduct v1.3
|
||||
As contributors and maintainers of this project, and in the interest of fostering an open and
|
||||
welcoming community, we pledge to respect all people who contribute through reporting
|
||||
issues, posting feature requests, updating documentation, submitting pull requests or patches,
|
||||
and other activities.
|
||||
|
||||
We are committed to making participation in this project a harassment-free experience for
|
||||
everyone, regardless of level of experience, gender, gender identity and expression, sexual
|
||||
orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or
|
||||
nationality.
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
- The use of sexualized language or imagery
|
||||
- Personal attacks
|
||||
- Trolling or insulting/derogatory comments
|
||||
- Public or private harassment
|
||||
- Publishing other’s private information, such as physical or electronic addresses, without explicit permission
|
||||
- Other unethical or unprofessional conduct
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments,
|
||||
commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of
|
||||
Conduct, or to ban temporarily or permanently any contributor for other behaviors that they
|
||||
deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
By adopting this Code of Conduct, project maintainers commit themselves to fairly and
|
||||
consistently applying these principles to every aspect of managing this project. Project
|
||||
maintainers who do not follow or enforce the Code of Conduct may be permanently removed
|
||||
from the project team.
|
||||
|
||||
This code of conduct applies both within project spaces and in public spaces when an
|
||||
individual is representing the project or its community.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by
|
||||
contacting a project maintainer at [swift-server-conduct@group.apple.com](mailto:swift-server-conduct@group.apple.com). All complaints will be reviewed and
|
||||
investigated and will result in a response that is deemed necessary and appropriate to the
|
||||
circumstances. Maintainers are obligated to maintain confidentiality with regard to the reporter
|
||||
of an incident.
|
||||
|
||||
*This policy is adapted from the Contributor Code of Conduct [version 1.3.0](https://contributor-covenant.org/version/1/3/0/).*
|
||||
|
||||
### Reporting
|
||||
A working group of community members is committed to promptly addressing any [reported issues](mailto:swift-server-conduct@group.apple.com).
|
||||
Working group members are volunteers appointed by the project lead, with a
|
||||
preference for individuals with varied backgrounds and perspectives. Membership is expected
|
||||
to change regularly, and may grow or shrink.
|
||||
The code of conduct for this project can be found at https://swift.org/code-of-conduct.
|
||||
+32
-7
@@ -1,9 +1,9 @@
|
||||
// swift-tools-version:5.5
|
||||
// swift-tools-version:5.6
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
//
|
||||
// Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors
|
||||
// Copyright (c) 2019-2023 Apple Inc. and the SwiftCrypto project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
@@ -20,7 +20,7 @@
|
||||
// Sources/CCryptoBoringSSL directory. The source repository is at
|
||||
// https://boringssl.googlesource.com/boringssl.
|
||||
//
|
||||
// BoringSSL Commit: 0faffc7a30eeb195248ea43056f4848e2a9b1c6d
|
||||
// BoringSSL Commit: 7ae2b910c13017b63f1a8bd6c8decfce692869b0
|
||||
|
||||
import PackageDescription
|
||||
|
||||
@@ -40,13 +40,15 @@ if development {
|
||||
"CryptoBoringWrapper"
|
||||
]
|
||||
} else {
|
||||
swiftSettings = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
]
|
||||
let platforms: [Platform] = [
|
||||
Platform.linux,
|
||||
Platform.android,
|
||||
Platform.windows,
|
||||
Platform.wasi,
|
||||
]
|
||||
swiftSettings = [
|
||||
.define("CRYPTO_IN_SWIFTPM"),
|
||||
.define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API", .when(platforms: platforms)),
|
||||
]
|
||||
dependencies = [
|
||||
.target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)),
|
||||
@@ -78,6 +80,13 @@ let package = Package(
|
||||
"hash.txt",
|
||||
"include/boringssl_prefix_symbols_nasm.inc",
|
||||
"CMakeLists.txt",
|
||||
/*
|
||||
* These files are excluded to support WASI libc which doesn't provide <netdb.h>.
|
||||
* This is safe for all platforms as we do not rely on networking features.
|
||||
*/
|
||||
"crypto/bio/connect.c",
|
||||
"crypto/bio/socket_helper.c",
|
||||
"crypto/bio/socket.c"
|
||||
],
|
||||
cSettings: [
|
||||
/*
|
||||
@@ -86,6 +95,11 @@ let package = Package(
|
||||
* only. Unconditionally define it instead.
|
||||
*/
|
||||
.define("WIN32_LEAN_AND_MEAN"),
|
||||
/*
|
||||
* These defines are required on Wasm/WASI, to disable use of pthread.
|
||||
*/
|
||||
.define("OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED", .when(platforms: [Platform.wasi])),
|
||||
.define("OPENSSL_NO_ASM", .when(platforms: [Platform.wasi])),
|
||||
]
|
||||
),
|
||||
.target(
|
||||
@@ -107,7 +121,18 @@ let package = Package(
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "CryptoBoringWrapper", "Crypto"]),
|
||||
.target(
|
||||
name: "_CryptoExtras",
|
||||
dependencies: [
|
||||
"CCryptoBoringSSL",
|
||||
"CCryptoBoringSSLShims",
|
||||
"CryptoBoringWrapper",
|
||||
"Crypto"
|
||||
],
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
]
|
||||
),
|
||||
.target(
|
||||
name: "CryptoBoringWrapper",
|
||||
dependencies: [
|
||||
|
||||
@@ -108,13 +108,14 @@ If you believe you have identified a vulnerability in Swift Crypto, please [repo
|
||||
|
||||
### Swift versions
|
||||
|
||||
The most recent versions of Swift Crypto support Swift 5.5 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below:
|
||||
The most recent versions of Swift Crypto support Swift 5.6 and newer. The minimum Swift version supported by Swift Crypto releases are detailed below:
|
||||
|
||||
Swift Crypto | Minimum Swift Version
|
||||
------------------|----------------------
|
||||
`2.0.0 ..< 2.1.0` | 5.2
|
||||
`2.1.0 ..< 2.2.0` | 5.4
|
||||
`2.2.0 ...` | 5.5
|
||||
`2.2.0 ..< 2.4.2` | 5.5
|
||||
`2.4.2 ...` | 5.6
|
||||
|
||||
### Compatibility
|
||||
|
||||
|
||||
+77
-75
@@ -23,7 +23,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"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"
|
||||
@@ -45,6 +44,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/bio/bio.c"
|
||||
"crypto/bio/bio_mem.c"
|
||||
"crypto/bio/connect.c"
|
||||
"crypto/bio/errno.c"
|
||||
"crypto/bio/fd.c"
|
||||
"crypto/bio/file.c"
|
||||
"crypto/bio/hexdump.c"
|
||||
@@ -75,17 +75,18 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/cipher_extra/tls_cbc.c"
|
||||
"crypto/conf/conf.c"
|
||||
"crypto/cpu_aarch64_apple.c"
|
||||
"crypto/cpu_aarch64_freebsd.c"
|
||||
"crypto/cpu_aarch64_fuchsia.c"
|
||||
"crypto/cpu_aarch64_linux.c"
|
||||
"crypto/cpu_aarch64_openbsd.c"
|
||||
"crypto/cpu_aarch64_sysreg.c"
|
||||
"crypto/cpu_aarch64_win.c"
|
||||
"crypto/cpu_arm.c"
|
||||
"crypto/cpu_arm_freebsd.c"
|
||||
"crypto/cpu_arm_linux.c"
|
||||
"crypto/cpu_intel.c"
|
||||
"crypto/cpu_ppc64le.c"
|
||||
"crypto/crypto.c"
|
||||
"crypto/curve25519/curve25519.c"
|
||||
"crypto/curve25519/curve25519_64_adx.c"
|
||||
"crypto/curve25519/spake25519.c"
|
||||
"crypto/des/des.c"
|
||||
"crypto/dh_extra/dh_asn1.c"
|
||||
@@ -170,6 +171,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/ecdh/ecdh.c"
|
||||
"crypto/fipsmodule/ecdsa/ecdsa.c"
|
||||
"crypto/fipsmodule/fips_shared_support.c"
|
||||
"crypto/fipsmodule/hkdf/hkdf.c"
|
||||
"crypto/fipsmodule/hmac/hmac.c"
|
||||
"crypto/fipsmodule/md4/md4.c"
|
||||
"crypto/fipsmodule/md5/md5.c"
|
||||
@@ -191,14 +193,14 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/fipsmodule/self_check/fips.c"
|
||||
"crypto/fipsmodule/self_check/self_check.c"
|
||||
"crypto/fipsmodule/service_indicator/service_indicator.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/kyber/keccak.c"
|
||||
"crypto/kyber/kyber.c"
|
||||
"crypto/lhash/lhash.c"
|
||||
"crypto/mem.c"
|
||||
"crypto/obj/obj.c"
|
||||
@@ -222,14 +224,16 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/pool/pool.c"
|
||||
"crypto/rand_extra/deterministic.c"
|
||||
"crypto/rand_extra/forkunsafe.c"
|
||||
"crypto/rand_extra/fuchsia.c"
|
||||
"crypto/rand_extra/getentropy.c"
|
||||
"crypto/rand_extra/ios.c"
|
||||
"crypto/rand_extra/passive.c"
|
||||
"crypto/rand_extra/rand_extra.c"
|
||||
"crypto/rand_extra/trusty.c"
|
||||
"crypto/rand_extra/windows.c"
|
||||
"crypto/rc4/rc4.c"
|
||||
"crypto/refcount_c11.c"
|
||||
"crypto/refcount_lock.c"
|
||||
"crypto/refcount.c"
|
||||
"crypto/rsa_extra/rsa_asn1.c"
|
||||
"crypto/rsa_extra/rsa_crypt.c"
|
||||
"crypto/rsa_extra/rsa_print.c"
|
||||
"crypto/siphash/siphash.c"
|
||||
"crypto/stack/stack.c"
|
||||
@@ -249,6 +253,7 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"crypto/x509/by_file.c"
|
||||
"crypto/x509/i2d_pr.c"
|
||||
"crypto/x509/name_print.c"
|
||||
"crypto/x509/policy.c"
|
||||
"crypto/x509/rsa_pss.c"
|
||||
"crypto/x509/t_crl.c"
|
||||
"crypto/x509/t_req.c"
|
||||
@@ -288,11 +293,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"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_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"
|
||||
@@ -310,8 +310,6 @@ add_library(CCryptoBoringSSL STATIC
|
||||
"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"
|
||||
@@ -321,72 +319,76 @@ add_library(CCryptoBoringSSL STATIC
|
||||
|
||||
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)
|
||||
crypto/chacha/chacha-x86_64-mac.mac.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64-mac.mac.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont-mac.mac.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5-mac.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)
|
||||
crypto/chacha/chacha-x86_64-linux.linux.x86_64.S
|
||||
crypto/cipher_extra/aes128gcmsiv-x86_64-linux.linux.x86_64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-gcm-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/aesni-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-ssse3-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/ghash-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/md5-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/p256-x86_64-asm-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/p256_beeu-x86_64-asm-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/rdrand-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/rsaz-avx2-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha1-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha256-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/sha512-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/vpaes-x86_64-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont-linux.linux.x86_64.S
|
||||
crypto/fipsmodule/x86_64-mont5-linux.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/cipher_extra/chacha20_poly1305_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/p256-armv8-asm.ios.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.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)
|
||||
crypto/chacha/chacha-armv8-ios.ios.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/bn-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8-ios.ios.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8-ios.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/cipher_extra/chacha20_poly1305_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/p256-armv8-asm.linux.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm.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)
|
||||
crypto/chacha/chacha-armv8-linux.linux.aarch64.S
|
||||
crypto/cipher_extra/chacha20_poly1305_armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/aesv8-gcm-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/armv8-mont-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/bn-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/ghash-neon-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/ghashv8-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/p256-armv8-asm-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/p256_beeu-armv8-asm-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha1-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha256-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/sha512-armv8-linux.linux.aarch64.S
|
||||
crypto/fipsmodule/vpaes-armv8-linux.linux.aarch64.S)
|
||||
endif()
|
||||
|
||||
target_include_directories(CCryptoBoringSSL PUBLIC
|
||||
|
||||
@@ -66,7 +66,8 @@
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d, int len) {
|
||||
int ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, const unsigned char *d,
|
||||
ossl_ssize_t len) {
|
||||
return ASN1_STRING_set(x, d, len);
|
||||
}
|
||||
|
||||
@@ -183,7 +184,6 @@ ASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
|
||||
if (len > 0) {
|
||||
s = OPENSSL_memdup(p, len);
|
||||
if (s == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
p += len;
|
||||
@@ -235,7 +235,6 @@ int ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value) {
|
||||
c = (unsigned char *)OPENSSL_realloc(a->data, w + 1);
|
||||
}
|
||||
if (c == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
if (w + 1 - a->length > 0) {
|
||||
|
||||
@@ -56,65 +56,40 @@
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **pp) {
|
||||
int r;
|
||||
unsigned char *p, *allocated = NULL;
|
||||
#include "../bytestring/internal.h"
|
||||
|
||||
r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN);
|
||||
if (pp == NULL) {
|
||||
return r;
|
||||
|
||||
int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 3) || //
|
||||
!CBB_add_asn1_bool(&cbb, a != ASN1_BOOLEAN_FALSE)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(r)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
|
||||
*p = a ? 0xff : 0x00;
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + 1;
|
||||
return r;
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *a, const unsigned char **pp,
|
||||
long length) {
|
||||
const unsigned char *p = *pp;
|
||||
long len;
|
||||
int inf, tag, xclass;
|
||||
inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
return -1;
|
||||
ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, const unsigned char **inp,
|
||||
long len) {
|
||||
if (len < 0) {
|
||||
return ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
return -1;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
int val;
|
||||
if (!CBS_get_asn1_bool(&cbs, &val)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
return ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_BOOLEAN || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_A_BOOLEAN);
|
||||
return -1;
|
||||
ASN1_BOOLEAN ret = val ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE;
|
||||
if (out != NULL) {
|
||||
*out = ret;
|
||||
}
|
||||
|
||||
if (len != 1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
|
||||
return -1;
|
||||
}
|
||||
ASN1_BOOLEAN ret = (ASN1_BOOLEAN) * (p++);
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
*inp = CBS_data(&cbs);
|
||||
return ret;
|
||||
}
|
||||
@@ -75,7 +75,6 @@ void *ASN1_item_dup(const ASN1_ITEM *it, void *x) {
|
||||
|
||||
i = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
p = b;
|
||||
|
||||
@@ -58,7 +58,9 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -81,34 +83,32 @@ int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d) {
|
||||
}
|
||||
|
||||
int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) {
|
||||
ASN1_GENERALIZEDTIME t;
|
||||
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_GENERALIZEDTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s, (unsigned char *)str, t.length)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_GENERALIZEDTIME;
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
size_t len = strlen(str);
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, (const uint8_t *)str, len);
|
||||
if (!CBS_parse_generalized_time(&cbs, /*out_tm=*/NULL,
|
||||
/*allow_timezone_offset=*/0)) {
|
||||
return 0;
|
||||
}
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set(s, str, len)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_GENERALIZEDTIME;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
|
||||
time_t t) {
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
|
||||
int64_t posix_time) {
|
||||
return ASN1_GENERALIZEDTIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
|
||||
time_t t, int offset_day,
|
||||
int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -124,9 +124,12 @@ ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
|
||||
}
|
||||
|
||||
char buf[16];
|
||||
BIO_snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
|
||||
data.tm_year + 1900, data.tm_mon + 1, data.tm_mday, data.tm_hour,
|
||||
data.tm_min, data.tm_sec);
|
||||
int ret = snprintf(buf, sizeof(buf), "%04d%02d%02d%02d%02d%02dZ",
|
||||
data.tm_year + 1900, data.tm_mon + 1, data.tm_mday,
|
||||
data.tm_hour, data.tm_min, data.tm_sec);
|
||||
if (ret != (int)(sizeof(buf) - 1)) {
|
||||
abort(); // |snprintf| should neither truncate nor write fewer bytes.
|
||||
}
|
||||
|
||||
int free_s = 0;
|
||||
if (s == NULL) {
|
||||
|
||||
@@ -76,7 +76,6 @@ int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, void *x) {
|
||||
unsigned char *b = NULL;
|
||||
int n = ASN1_item_i2d(x, &b, it);
|
||||
if (b == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -122,14 +122,17 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
// |ASN1_INTEGER|s should be represented minimally, but it is possible to
|
||||
// construct invalid ones. Skip leading zeros so this does not produce an
|
||||
// invalid encoding or break invariants.
|
||||
int start = 0;
|
||||
while (start < in->length && in->data[start] == 0) {
|
||||
start++;
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in->data, in->length);
|
||||
while (CBS_len(&cbs) > 0 && CBS_data(&cbs)[0] == 0) {
|
||||
CBS_skip(&cbs, 1);
|
||||
}
|
||||
|
||||
int is_negative = (in->type & V_ASN1_NEG) != 0;
|
||||
int pad;
|
||||
if (start >= in->length) {
|
||||
size_t pad;
|
||||
CBS copy = cbs;
|
||||
uint8_t msb;
|
||||
if (!CBS_get_u8(©, &msb)) {
|
||||
// Zero is represented as a single byte.
|
||||
is_negative = 0;
|
||||
pad = 1;
|
||||
@@ -138,20 +141,19 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
// through 0x00...01 and need an extra byte to be negative.
|
||||
// 0x01...00 through 0x80...00 have a two's complement of 0xfe...ff
|
||||
// through 0x80...00 and can be negated as-is.
|
||||
pad = in->data[start] > 0x80 ||
|
||||
(in->data[start] == 0x80 &&
|
||||
!is_all_zeros(in->data + start + 1, in->length - start - 1));
|
||||
pad = msb > 0x80 ||
|
||||
(msb == 0x80 && !is_all_zeros(CBS_data(©), CBS_len(©)));
|
||||
} else {
|
||||
// If the high bit is set, the signed representation needs an extra
|
||||
// byte to be positive.
|
||||
pad = (in->data[start] & 0x80) != 0;
|
||||
pad = (msb & 0x80) != 0;
|
||||
}
|
||||
|
||||
if (in->length - start > INT_MAX - pad) {
|
||||
if (CBS_len(&cbs) > INT_MAX - pad) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
int len = pad + in->length - start;
|
||||
int len = (int)(pad + CBS_len(&cbs));
|
||||
assert(len > 0);
|
||||
if (outp == NULL) {
|
||||
return len;
|
||||
@@ -160,7 +162,7 @@ int i2c_ASN1_INTEGER(const ASN1_INTEGER *in, unsigned char **outp) {
|
||||
if (pad) {
|
||||
(*outp)[0] = 0;
|
||||
}
|
||||
OPENSSL_memcpy(*outp + pad, in->data + start, in->length - start);
|
||||
OPENSSL_memcpy(*outp + pad, CBS_data(&cbs), CBS_len(&cbs));
|
||||
if (is_negative) {
|
||||
negate_twos_complement(*outp, len);
|
||||
assert((*outp)[0] >= 0x80);
|
||||
|
||||
@@ -73,23 +73,19 @@
|
||||
// horrible: it has to be :-( The 'ncopy' form checks minimum and maximum
|
||||
// size limits too.
|
||||
|
||||
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
int inform, unsigned long mask) {
|
||||
return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
|
||||
int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in,
|
||||
ossl_ssize_t len, int inform, unsigned long mask) {
|
||||
return ASN1_mbstring_ncopy(out, in, len, inform, mask, /*minsize=*/0,
|
||||
/*maxsize=*/0);
|
||||
}
|
||||
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_BMPSTRING)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UNIVERSALSTRING)
|
||||
OPENSSL_DECLARE_ERROR_REASON(ASN1, INVALID_UTF8STRING)
|
||||
|
||||
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
int inform, unsigned long mask, long minsize,
|
||||
long maxsize) {
|
||||
int str_type;
|
||||
char free_out;
|
||||
ASN1_STRING *dest;
|
||||
size_t nchar = 0;
|
||||
char strbuf[32];
|
||||
int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in,
|
||||
ossl_ssize_t len, int inform, unsigned long mask,
|
||||
ossl_ssize_t minsize, ossl_ssize_t maxsize) {
|
||||
if (len == -1) {
|
||||
len = strlen((const char *)in);
|
||||
}
|
||||
@@ -128,7 +124,7 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
// Check |minsize| and |maxsize| and work out the minimal type, if any.
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, len);
|
||||
size_t utf8_len = 0;
|
||||
size_t utf8_len = 0, nchar = 0;
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
uint32_t c;
|
||||
if (!decode_func(&cbs, &c)) {
|
||||
@@ -167,23 +163,21 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
|
||||
nchar++;
|
||||
utf8_len += cbb_get_utf8_len(c);
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
ERR_add_error_dataf("maxsize=%zu", (size_t)maxsize);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (minsize > 0 && nchar < (size_t)minsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_SHORT);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
|
||||
ERR_add_error_data(2, "minsize=", strbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (maxsize > 0 && nchar > (size_t)maxsize) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
|
||||
ERR_add_error_data(2, "maxsize=", strbuf);
|
||||
ERR_add_error_dataf("minsize=%zu", (size_t)minsize);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Now work out output format and string type
|
||||
int str_type;
|
||||
int (*encode_func)(CBB *, uint32_t) = cbb_add_latin1;
|
||||
size_t size_estimate = nchar;
|
||||
int outform = MBSTRING_ASC;
|
||||
@@ -216,37 +210,31 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
if (!out) {
|
||||
return str_type;
|
||||
}
|
||||
|
||||
int free_dest = 0;
|
||||
ASN1_STRING *dest;
|
||||
if (*out) {
|
||||
free_out = 0;
|
||||
dest = *out;
|
||||
if (dest->data) {
|
||||
dest->length = 0;
|
||||
OPENSSL_free(dest->data);
|
||||
dest->data = NULL;
|
||||
}
|
||||
dest->type = str_type;
|
||||
} else {
|
||||
free_out = 1;
|
||||
free_dest = 1;
|
||||
dest = ASN1_STRING_type_new(str_type);
|
||||
if (!dest) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
*out = dest;
|
||||
}
|
||||
|
||||
// If both the same type just copy across
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
return str_type;
|
||||
}
|
||||
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
// If both the same type just copy across
|
||||
if (inform == outform) {
|
||||
if (!ASN1_STRING_set(dest, in, len)) {
|
||||
goto err;
|
||||
}
|
||||
dest->type = str_type;
|
||||
*out = dest;
|
||||
return str_type;
|
||||
}
|
||||
if (!CBB_init(&cbb, size_estimate + 1)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
CBS_init(&cbs, in, len);
|
||||
@@ -267,12 +255,13 @@ int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
|
||||
OPENSSL_free(data);
|
||||
goto err;
|
||||
}
|
||||
dest->length = (int)(data_len - 1);
|
||||
dest->data = data;
|
||||
dest->type = str_type;
|
||||
ASN1_STRING_set0(dest, data, (int)data_len - 1);
|
||||
*out = dest;
|
||||
return str_type;
|
||||
|
||||
err:
|
||||
if (free_out) {
|
||||
if (free_dest) {
|
||||
ASN1_STRING_free(dest);
|
||||
}
|
||||
CBB_cleanup(&cbb);
|
||||
@@ -283,10 +272,7 @@ int asn1_is_printable(uint32_t value) {
|
||||
if (value > 0x7f) {
|
||||
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') || //
|
||||
return OPENSSL_isalnum(value) || //
|
||||
value == ' ' || value == '\'' || value == '(' || value == ')' ||
|
||||
value == '+' || value == ',' || value == '-' || value == '.' ||
|
||||
value == '/' || value == ':' || value == '=' || value == '?';
|
||||
|
||||
@@ -59,47 +59,36 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
|
||||
#include "../bytestring/internal.h"
|
||||
#include "../internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp) {
|
||||
if (a == NULL) {
|
||||
int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, unsigned char **outp) {
|
||||
if (in == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a->length == 0) {
|
||||
if (in->length <= 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
|
||||
if (pp == NULL || objsize == -1) {
|
||||
return objsize;
|
||||
CBB cbb, child;
|
||||
if (!CBB_init(&cbb, (size_t)in->length + 2) ||
|
||||
!CBB_add_asn1(&cbb, &child, CBS_ASN1_OBJECT) ||
|
||||
!CBB_add_bytes(&child, in->data, in->length)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char *p, *allocated = NULL;
|
||||
if (*pp == NULL) {
|
||||
if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
p = *pp;
|
||||
}
|
||||
|
||||
ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
|
||||
OPENSSL_memcpy(p, a->data, a->length);
|
||||
|
||||
// If a new buffer was allocated, just return it back.
|
||||
// If not, return the incremented buffer pointer.
|
||||
*pp = allocated != NULL ? allocated : p + a->length;
|
||||
return objsize;
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
|
||||
@@ -107,8 +96,12 @@ int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
|
||||
}
|
||||
|
||||
static int write_str(BIO *bp, const char *str) {
|
||||
int len = strlen(str);
|
||||
return BIO_write(bp, str, len) == len ? len : -1;
|
||||
size_t len = strlen(str);
|
||||
if (len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return -1;
|
||||
}
|
||||
return BIO_write(bp, str, (int)len) == (int)len ? (int)len : -1;
|
||||
}
|
||||
|
||||
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
|
||||
@@ -137,106 +130,55 @@ int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
long length) {
|
||||
long len;
|
||||
int tag, xclass;
|
||||
const unsigned char *p = *pp;
|
||||
int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
||||
if (inf & 0x80) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
|
||||
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
|
||||
long len) {
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (inf & V_ASN1_CONSTRUCTED) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
|
||||
CBS cbs, child;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
if (!CBS_get_asn1(&cbs, &child, CBS_ASN1_OBJECT)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPECTING_AN_OBJECT);
|
||||
return NULL;
|
||||
}
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(a, &p, len);
|
||||
if (ret) {
|
||||
*pp = p;
|
||||
const uint8_t *contents = CBS_data(&child);
|
||||
ASN1_OBJECT *ret = c2i_ASN1_OBJECT(out, &contents, CBS_len(&child));
|
||||
if (ret != NULL) {
|
||||
// |c2i_ASN1_OBJECT| should have consumed the entire input.
|
||||
assert(CBS_data(&cbs) == contents);
|
||||
*inp = CBS_data(&cbs);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
|
||||
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
|
||||
long len) {
|
||||
ASN1_OBJECT *ret = NULL;
|
||||
const unsigned char *p;
|
||||
unsigned char *data;
|
||||
int i, length;
|
||||
|
||||
// Sanity check OID encoding. Need at least one content octet. MSB must
|
||||
// be clear in the last octet. can't have leading 0x80 in subidentifiers,
|
||||
// see: X.690 8.19.2
|
||||
if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
|
||||
p[len - 1] & 0x80) {
|
||||
if (len < 0) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
// Now 0 < len <= INT_MAX, so the cast is safe.
|
||||
length = (int)len;
|
||||
for (i = 0; i < length; i++, p++) {
|
||||
if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
if (!CBS_is_valid_asn1_oid(&cbs)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((a == NULL) || ((*a) == NULL) ||
|
||||
!((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
|
||||
if ((ret = ASN1_OBJECT_new()) == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
} else {
|
||||
ret = (*a);
|
||||
ASN1_OBJECT *ret = ASN1_OBJECT_create(NID_undef, *inp, (size_t)len,
|
||||
/*sn=*/NULL, /*ln=*/NULL);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p = *pp;
|
||||
// detach data from object
|
||||
data = (unsigned char *)ret->data;
|
||||
ret->data = NULL;
|
||||
// once detached we can change it
|
||||
if ((data == NULL) || (ret->length < length)) {
|
||||
ret->length = 0;
|
||||
OPENSSL_free(data);
|
||||
data = (unsigned char *)OPENSSL_malloc(length);
|
||||
if (data == NULL) {
|
||||
i = ERR_R_MALLOC_FAILURE;
|
||||
goto err;
|
||||
}
|
||||
ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
if (out != NULL) {
|
||||
ASN1_OBJECT_free(*out);
|
||||
*out = ret;
|
||||
}
|
||||
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;
|
||||
p += length;
|
||||
|
||||
if (a != NULL) {
|
||||
(*a) = ret;
|
||||
}
|
||||
*pp = p;
|
||||
*inp += len; // All bytes were consumed.
|
||||
return ret;
|
||||
err:
|
||||
OPENSSL_PUT_ERROR(ASN1, i);
|
||||
if ((ret != NULL) && ((a == NULL) || (*a != ret))) {
|
||||
ASN1_OBJECT_free(ret);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void) {
|
||||
@@ -244,7 +186,6 @@ ASN1_OBJECT *ASN1_OBJECT_new(void) {
|
||||
|
||||
ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
@@ -275,16 +216,20 @@ void ASN1_OBJECT_free(ASN1_OBJECT *a) {
|
||||
}
|
||||
}
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
|
||||
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, size_t len,
|
||||
const char *sn, const char *ln) {
|
||||
ASN1_OBJECT o;
|
||||
if (len > INT_MAX) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ASN1_OBJECT o;
|
||||
o.sn = sn;
|
||||
o.ln = ln;
|
||||
o.data = data;
|
||||
o.nid = nid;
|
||||
o.length = len;
|
||||
o.length = (int)len;
|
||||
o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
|
||||
ASN1_OBJECT_FLAG_DYNAMIC_DATA;
|
||||
return (OBJ_dup(&o));
|
||||
return OBJ_dup(&o);
|
||||
}
|
||||
@@ -59,6 +59,7 @@
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -88,18 +89,18 @@ static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
|
||||
char buf[16]; // Large enough for "\\W01234567".
|
||||
unsigned char u8 = (unsigned char)c;
|
||||
if (c > 0xffff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
|
||||
snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
|
||||
} else if (c > 0xff) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
|
||||
snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) {
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
snprintf(buf, sizeof(buf), "\\%02X", c);
|
||||
} else if (flags & ASN1_STRFLGS_ESC_2253) {
|
||||
// See RFC 2253, sections 2.4 and 4.
|
||||
if (c == '\\' || c == '"') {
|
||||
// Quotes and backslashes are always escaped, quoted or not.
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' ||
|
||||
(is_first && (c == ' ' || c == '#')) ||
|
||||
(is_last && (c == ' '))) {
|
||||
@@ -110,18 +111,19 @@ static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
|
||||
}
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
} else if ((flags & ESC_FLAGS) && c == '\\') {
|
||||
// If any escape flags are set, also escape backslashes.
|
||||
BIO_snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
snprintf(buf, sizeof(buf), "\\%c", (int)c);
|
||||
} else {
|
||||
return maybe_write(out, &u8, 1) ? 1 : -1;
|
||||
}
|
||||
|
||||
int len = strlen(buf);
|
||||
static_assert(sizeof(buf) < INT_MAX, "len may not fit in int");
|
||||
int len = (int)strlen(buf);
|
||||
return maybe_write(out, buf, len) ? len : -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@
|
||||
DEFINE_LHASH_OF(ASN1_STRING_TABLE)
|
||||
|
||||
static LHASH_OF(ASN1_STRING_TABLE) *string_tables = NULL;
|
||||
static struct CRYPTO_STATIC_MUTEX string_tables_lock = CRYPTO_STATIC_MUTEX_INIT;
|
||||
static CRYPTO_MUTEX string_tables_lock = CRYPTO_MUTEX_INIT;
|
||||
|
||||
void ASN1_STRING_set_default_mask(unsigned long mask) {}
|
||||
|
||||
@@ -87,7 +87,7 @@ static const ASN1_STRING_TABLE *asn1_string_table_get(int nid);
|
||||
// a corresponding OID. For example certificates and certificate requests.
|
||||
|
||||
ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
|
||||
int len, int inform, int nid) {
|
||||
ossl_ssize_t len, int inform, int nid) {
|
||||
ASN1_STRING *str = NULL;
|
||||
int ret;
|
||||
if (!out) {
|
||||
@@ -176,11 +176,11 @@ static const ASN1_STRING_TABLE *asn1_string_table_get(int nid) {
|
||||
return tbl;
|
||||
}
|
||||
|
||||
CRYPTO_STATIC_MUTEX_lock_read(&string_tables_lock);
|
||||
CRYPTO_MUTEX_lock_read(&string_tables_lock);
|
||||
if (string_tables != NULL) {
|
||||
tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key);
|
||||
}
|
||||
CRYPTO_STATIC_MUTEX_unlock_read(&string_tables_lock);
|
||||
CRYPTO_MUTEX_unlock_read(&string_tables_lock);
|
||||
// Note returning |tbl| without the lock is only safe because
|
||||
// |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we
|
||||
// wish to support that, this function must copy the result under a lock.
|
||||
@@ -196,7 +196,7 @@ int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&string_tables_lock);
|
||||
CRYPTO_MUTEX_lock_write(&string_tables_lock);
|
||||
|
||||
if (string_tables == NULL) {
|
||||
string_tables = lh_ASN1_STRING_TABLE_new(table_hash, table_cmp);
|
||||
@@ -232,7 +232,7 @@ int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&string_tables_lock);
|
||||
CRYPTO_MUTEX_unlock_write(&string_tables_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,11 +55,13 @@
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <CCryptoBoringSSL_asn1t.h>
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
@@ -73,29 +75,35 @@ IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
|
||||
|
||||
IMPLEMENT_ASN1_FUNCTIONS_const(ASN1_TIME)
|
||||
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) {
|
||||
return ASN1_TIME_adj(s, t, 0, 0);
|
||||
ASN1_TIME *ASN1_TIME_set_posix(ASN1_TIME *s, int64_t posix_time) {
|
||||
return ASN1_TIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm *ts;
|
||||
struct tm data;
|
||||
ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t time) {
|
||||
return ASN1_TIME_adj(s, time, 0, 0);
|
||||
}
|
||||
|
||||
ts = OPENSSL_gmtime(&t, &data);
|
||||
if (ts == NULL) {
|
||||
static int fits_in_utc_time(const struct tm *tm) {
|
||||
return 50 <= tm->tm_year && tm->tm_year < 150;
|
||||
}
|
||||
|
||||
ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm tm;
|
||||
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &tm)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ERROR_GETTING_TIME);
|
||||
return NULL;
|
||||
}
|
||||
if (offset_day || offset_sec) {
|
||||
if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) {
|
||||
if (!OPENSSL_gmtime_adj(&tm, offset_day, offset_sec)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if ((ts->tm_year >= 50) && (ts->tm_year < 150)) {
|
||||
return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
|
||||
if (fits_in_utc_time(&tm)) {
|
||||
return ASN1_UTCTIME_adj(s, posix_time, offset_day, offset_sec);
|
||||
}
|
||||
return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
|
||||
return ASN1_GENERALIZEDTIME_adj(s, posix_time, offset_day, offset_sec);
|
||||
}
|
||||
|
||||
int ASN1_TIME_check(const ASN1_TIME *t) {
|
||||
@@ -163,36 +171,43 @@ err:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) {
|
||||
ASN1_TIME t;
|
||||
return ASN1_UTCTIME_set_string(s, str) ||
|
||||
ASN1_GENERALIZEDTIME_set_string(s, str);
|
||||
}
|
||||
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
t.flags = 0;
|
||||
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
|
||||
if (!ASN1_TIME_check(&t)) {
|
||||
t.type = V_ASN1_GENERALIZEDTIME;
|
||||
if (!ASN1_TIME_check(&t)) {
|
||||
return 0;
|
||||
int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str) {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, (const uint8_t*)str, strlen(str));
|
||||
int type;
|
||||
struct tm tm;
|
||||
if (CBS_parse_utc_time(&cbs, /*out_tm=*/NULL,
|
||||
/*allow_timezone_offset=*/0)) {
|
||||
type = V_ASN1_UTCTIME;
|
||||
} else if (CBS_parse_generalized_time(&cbs, &tm,
|
||||
/*allow_timezone_offset=*/0)) {
|
||||
type = V_ASN1_GENERALIZEDTIME;
|
||||
if (fits_in_utc_time(&tm)) {
|
||||
type = V_ASN1_UTCTIME;
|
||||
CBS_skip(&cbs, 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t)) {
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set(s, CBS_data(&cbs), CBS_len(&cbs))) {
|
||||
return 0;
|
||||
}
|
||||
s->type = type;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t,
|
||||
int allow_timezone_offset) {
|
||||
if (t == NULL) {
|
||||
time_t now_t;
|
||||
time(&now_t);
|
||||
if (OPENSSL_gmtime(&now_t, tm)) {
|
||||
if (OPENSSL_posix_to_tm(time(NULL), tm)) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -65,31 +65,65 @@
|
||||
|
||||
|
||||
int ASN1_TYPE_get(const ASN1_TYPE *a) {
|
||||
if (a->type == V_ASN1_BOOLEAN || a->type == V_ASN1_NULL ||
|
||||
a->value.ptr != NULL) {
|
||||
return a->type;
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
case V_ASN1_BOOLEAN:
|
||||
return a->type;
|
||||
case V_ASN1_OBJECT:
|
||||
return a->value.object != NULL ? a->type : 0;
|
||||
default:
|
||||
return a->value.asn1_string != NULL ? a->type : 0;
|
||||
}
|
||||
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;
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
return NULL;
|
||||
case V_ASN1_BOOLEAN:
|
||||
return a->value.boolean ? (void *)0xff : NULL;
|
||||
case V_ASN1_OBJECT:
|
||||
return a->value.object;
|
||||
default:
|
||||
return a->value.asn1_string;
|
||||
}
|
||||
if (a->type == V_ASN1_NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void asn1_type_cleanup(ASN1_TYPE *a) {
|
||||
switch (a->type) {
|
||||
case V_ASN1_NULL:
|
||||
a->value.ptr = NULL;
|
||||
break;
|
||||
case V_ASN1_BOOLEAN:
|
||||
a->value.boolean = ASN1_BOOLEAN_NONE;
|
||||
break;
|
||||
case V_ASN1_OBJECT:
|
||||
ASN1_OBJECT_free(a->value.object);
|
||||
a->value.object = NULL;
|
||||
break;
|
||||
default:
|
||||
ASN1_STRING_free(a->value.asn1_string);
|
||||
a->value.asn1_string = NULL;
|
||||
break;
|
||||
}
|
||||
return a->value.ptr;
|
||||
}
|
||||
|
||||
void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value) {
|
||||
ASN1_TYPE **tmp_a = &a;
|
||||
ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
|
||||
asn1_type_cleanup(a);
|
||||
a->type = type;
|
||||
if (type == V_ASN1_BOOLEAN) {
|
||||
a->value.boolean = value ? 0xff : 0;
|
||||
} else {
|
||||
a->value.ptr = value;
|
||||
switch (type) {
|
||||
case V_ASN1_NULL:
|
||||
a->value.ptr = NULL;
|
||||
break;
|
||||
case V_ASN1_BOOLEAN:
|
||||
a->value.boolean = value ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE;
|
||||
break;
|
||||
case V_ASN1_OBJECT:
|
||||
a->value.object = value;
|
||||
break;
|
||||
default:
|
||||
a->value.asn1_string = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,9 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -82,32 +84,33 @@ int ASN1_UTCTIME_check(const ASN1_UTCTIME *d) {
|
||||
}
|
||||
|
||||
int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) {
|
||||
ASN1_UTCTIME t;
|
||||
|
||||
t.type = V_ASN1_UTCTIME;
|
||||
t.length = strlen(str);
|
||||
t.data = (unsigned char *)str;
|
||||
if (ASN1_UTCTIME_check(&t)) {
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set((ASN1_STRING *)s, (unsigned char *)str, t.length)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
// Although elsewhere we allow timezone offsets with UTCTime, to be compatible
|
||||
// with some existing misissued certificates, this function is used to
|
||||
// construct new certificates and can be stricter.
|
||||
size_t len = strlen(str);
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, (const uint8_t *)str, len);
|
||||
if (!CBS_parse_utc_time(&cbs, /*out_tm=*/NULL,
|
||||
/*allow_timezone_offset=*/0)) {
|
||||
return 0;
|
||||
}
|
||||
if (s != NULL) {
|
||||
if (!ASN1_STRING_set(s, str, len)) {
|
||||
return 0;
|
||||
}
|
||||
s->type = V_ASN1_UTCTIME;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) {
|
||||
return ASN1_UTCTIME_adj(s, t, 0, 0);
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, int64_t posix_time) {
|
||||
return ASN1_UTCTIME_adj(s, posix_time, 0, 0);
|
||||
}
|
||||
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day,
|
||||
ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, int64_t posix_time, int offset_day,
|
||||
long offset_sec) {
|
||||
struct tm data;
|
||||
if (!OPENSSL_gmtime(&t, &data)) {
|
||||
if (!OPENSSL_posix_to_tm(posix_time, &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -122,9 +125,12 @@ ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t, int offset_day,
|
||||
}
|
||||
|
||||
char buf[14];
|
||||
BIO_snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
|
||||
data.tm_year % 100, data.tm_mon + 1, data.tm_mday, data.tm_hour,
|
||||
data.tm_min, data.tm_sec);
|
||||
int ret = snprintf(buf, sizeof(buf), "%02d%02d%02d%02d%02d%02dZ",
|
||||
data.tm_year % 100, data.tm_mon + 1, data.tm_mday,
|
||||
data.tm_hour, data.tm_min, data.tm_sec);
|
||||
if (ret != (int)(sizeof(buf) - 1)) {
|
||||
abort(); // |snprintf| should neither truncate nor write fewer bytes.
|
||||
}
|
||||
|
||||
int free_s = 0;
|
||||
if (s == NULL) {
|
||||
@@ -153,7 +159,7 @@ int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (!OPENSSL_gmtime(&t, &ttm)) {
|
||||
if (!OPENSSL_posix_to_tm(t, &ttm)) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
|
||||
@@ -111,20 +111,10 @@ int ASN1_get_object(const unsigned char **inp, long *out_len, int *out_tag,
|
||||
return 0x80;
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/354): This should use |CBS_get_asn1| to
|
||||
// reject non-minimal lengths, which are only allowed in BER. However,
|
||||
// Android sometimes needs allow a non-minimal length in certificate
|
||||
// signature fields (see b/18228011). Make this only apply to that field,
|
||||
// while requiring DER elsewhere. Better yet, it should be limited to an
|
||||
// preprocessing step in that part of Android.
|
||||
unsigned tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBS_ASN1_TAG tag;
|
||||
CBS cbs, body;
|
||||
CBS_init(&cbs, *inp, (size_t)in_len);
|
||||
if (!CBS_get_any_ber_asn1_element(&cbs, &body, &tag, &header_len,
|
||||
/*out_ber_found=*/NULL, &indefinite) ||
|
||||
indefinite || !CBS_skip(&body, header_len) ||
|
||||
if (!CBS_get_any_asn1(&cbs, &body, &tag) ||
|
||||
// Bound the length to comfortably fit in an int. Lengths in this
|
||||
// module often switch between int and long without overflow checks.
|
||||
CBS_len(&body) > INT_MAX / 2) {
|
||||
@@ -271,19 +261,27 @@ ASN1_STRING *ASN1_STRING_dup(const ASN1_STRING *str) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) {
|
||||
unsigned char *c;
|
||||
int ASN1_STRING_set(ASN1_STRING *str, const void *_data, ossl_ssize_t len_s) {
|
||||
const char *data = _data;
|
||||
|
||||
if (len < 0) {
|
||||
size_t len;
|
||||
if (len_s < 0) {
|
||||
if (data == NULL) {
|
||||
return 0;
|
||||
} else {
|
||||
len = strlen(data);
|
||||
}
|
||||
len = strlen(data);
|
||||
} else {
|
||||
len = (size_t)len_s;
|
||||
}
|
||||
if ((str->length <= len) || (str->data == NULL)) {
|
||||
c = str->data;
|
||||
|
||||
// |ASN1_STRING| cannot represent strings that exceed |int|, and we must
|
||||
// reserve space for a trailing NUL below.
|
||||
if (len > INT_MAX || len + 1 < len) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (str->length <= (int)len || str->data == NULL) {
|
||||
unsigned char *c = str->data;
|
||||
if (c == NULL) {
|
||||
str->data = OPENSSL_malloc(len + 1);
|
||||
} else {
|
||||
@@ -291,15 +289,17 @@ int ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len) {
|
||||
}
|
||||
|
||||
if (str->data == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
str->data = c;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
str->length = len;
|
||||
str->length = (int)len;
|
||||
if (data != NULL) {
|
||||
OPENSSL_memcpy(str->data, data, len);
|
||||
// an allowance for strings :-)
|
||||
// Historically, OpenSSL would NUL-terminate most (but not all)
|
||||
// |ASN1_STRING|s, in case anyone accidentally passed |str->data| into a
|
||||
// function expecting a C string. We retain this behavior for compatibility,
|
||||
// but code must not rely on this. See CVE-2021-3712.
|
||||
str->data[len] = '\0';
|
||||
}
|
||||
return 1;
|
||||
@@ -320,7 +320,6 @@ ASN1_STRING *ASN1_STRING_type_new(int type) {
|
||||
|
||||
ret = (ASN1_STRING *)OPENSSL_malloc(sizeof(ASN1_STRING));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
ret->length = 0;
|
||||
|
||||
@@ -72,7 +72,6 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **out) {
|
||||
if (out == NULL || *out == NULL) {
|
||||
ret = ASN1_STRING_new();
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_free(new_data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#define OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#ifndef OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
#define OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
|
||||
#include <time.h>
|
||||
|
||||
@@ -71,16 +71,6 @@ extern "C" {
|
||||
|
||||
// Wrapper functions for time functions.
|
||||
|
||||
// OPENSSL_posix_to_tm converts a int64_t POSIX time value in |time| whuch must
|
||||
// be in the range of year 0000 to 9999 to a broken out time value in |tm|. It
|
||||
// returns one on success and zero on error.
|
||||
OPENSSL_EXPORT int OPENSSL_posix_to_tm(int64_t time, struct tm *out_tm);
|
||||
|
||||
// OPENSSL_tm_to_posix converts a time value between the years 0 and 9999 in
|
||||
// |tm| to a POSIX time value in |out|. One is returned on success, zero is
|
||||
// returned on failure. It is a failure if the tm contains out of range values.
|
||||
OPENSSL_EXPORT int OPENSSL_tm_to_posix(const struct tm *tm, int64_t *out);
|
||||
|
||||
// OPENSSL_gmtime converts a time_t value in |time| which must be in the range
|
||||
// of year 0000 to 9999 to a broken out time value in |tm|. On success |tm| is
|
||||
// returned. On failure NULL is returned.
|
||||
@@ -132,21 +122,16 @@ struct asn1_object_st {
|
||||
|
||||
ASN1_OBJECT *ASN1_OBJECT_new(void);
|
||||
|
||||
// ASN1_ENCODING structure: this is used to save the received
|
||||
// encoding of an ASN1 type. This is useful to get round
|
||||
// problems with invalid encodings which can break signatures.
|
||||
// ASN1_ENCODING is used to save the received encoding of an ASN.1 type. This
|
||||
// avoids problems with invalid encodings that break signatures.
|
||||
typedef struct ASN1_ENCODING_st {
|
||||
unsigned char *enc; // DER encoding
|
||||
long len; // Length of encoding
|
||||
int modified; // set to 1 if 'enc' is invalid
|
||||
// alias_only is zero if |enc| owns the buffer that it points to
|
||||
// (although |enc| may still be NULL). If one, |enc| points into a
|
||||
// buffer that is owned elsewhere.
|
||||
unsigned alias_only : 1;
|
||||
// alias_only_on_next_parse is one iff the next parsing operation
|
||||
// should avoid taking a copy of the input and rather set
|
||||
// |alias_only|.
|
||||
unsigned alias_only_on_next_parse : 1;
|
||||
// enc is the saved DER encoding. Its ownership is determined by |buf|.
|
||||
uint8_t *enc;
|
||||
// len is the length of |enc|. If zero, there is no saved encoding.
|
||||
size_t len;
|
||||
// buf, if non-NULL, is the |CRYPTO_BUFFER| that |enc| points into. If NULL,
|
||||
// |enc| must be released with |OPENSSL_free|.
|
||||
CRYPTO_BUFFER *buf;
|
||||
} ASN1_ENCODING;
|
||||
|
||||
OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
|
||||
@@ -154,16 +139,22 @@ OPENSSL_EXPORT int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d,
|
||||
OPENSSL_EXPORT int asn1_generalizedtime_to_tm(struct tm *tm,
|
||||
const ASN1_GENERALIZEDTIME *d);
|
||||
|
||||
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine);
|
||||
|
||||
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);
|
||||
|
||||
// ASN1_item_ex_d2i parses |len| bytes from |*in| as a structure of type |it|
|
||||
// and writes the result to |*pval|. If |tag| is non-negative, |it| is
|
||||
// implicitly tagged with the tag specified by |tag| and |aclass|. If |opt| is
|
||||
// non-zero, the value is optional. If |buf| is non-NULL, |*in| must point into
|
||||
// |buf|.
|
||||
//
|
||||
// This function returns one and advances |*in| if an object was successfully
|
||||
// parsed, -1 if an optional value was successfully skipped, and zero on error.
|
||||
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);
|
||||
CRYPTO_BUFFER *buf);
|
||||
|
||||
// ASN1_item_ex_i2d encodes |*pval| as a value of type |it| to |out| under the
|
||||
// i2d output convention. It returns a non-zero length on success and -1 on
|
||||
@@ -205,14 +196,24 @@ 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_enc_save saves |inlen| bytes from |in| as |*pval|'s saved encoding. It
|
||||
// returns one on success and zero on error. If |buf| is non-NULL, |in| must
|
||||
// point into |buf|.
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t inlen,
|
||||
const ASN1_ITEM *it, CRYPTO_BUFFER *buf);
|
||||
|
||||
// asn1_encoding_clear clears the cached encoding in |enc|.
|
||||
void asn1_encoding_clear(ASN1_ENCODING *enc);
|
||||
|
||||
// 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_type_cleanup releases memory associated with |a|'s value, without
|
||||
// freeing |a| itself.
|
||||
void asn1_type_cleanup(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);
|
||||
@@ -238,9 +239,31 @@ typedef struct {
|
||||
OPENSSL_EXPORT void asn1_get_string_table_for_testing(
|
||||
const ASN1_STRING_TABLE **out_ptr, size_t *out_len);
|
||||
|
||||
typedef ASN1_VALUE *ASN1_new_func(void);
|
||||
typedef void ASN1_free_func(ASN1_VALUE *a);
|
||||
typedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in,
|
||||
long length);
|
||||
typedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in);
|
||||
|
||||
typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it, int opt, ASN1_TLC *ctx);
|
||||
|
||||
typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_ITEM *it);
|
||||
typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
|
||||
|
||||
typedef struct ASN1_EXTERN_FUNCS_st {
|
||||
ASN1_ex_new_func *asn1_ex_new;
|
||||
ASN1_ex_free_func *asn1_ex_free;
|
||||
ASN1_ex_free_func *asn1_ex_clear;
|
||||
ASN1_ex_d2i *asn1_ex_d2i;
|
||||
ASN1_ex_i2d *asn1_ex_i2d;
|
||||
} ASN1_EXTERN_FUNCS;
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
#endif // OPENSSL_HEADER_ASN1_ASN1_LOCL_H
|
||||
#endif // OPENSSL_HEADER_ASN1_INTERNAL_H
|
||||
@@ -15,6 +15,8 @@
|
||||
// Time conversion to/from POSIX time_t and struct tm, with no support
|
||||
// for time zones other than UTC
|
||||
|
||||
#include <CCryptoBoringSSL_time.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
|
||||
@@ -59,7 +59,9 @@
|
||||
#include <CCryptoBoringSSL_bytestring.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_pool.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -79,18 +81,18 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
|
||||
|
||||
static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth);
|
||||
CRYPTO_BUFFER *buf, int depth);
|
||||
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
CRYPTO_BUFFER *buf, int depth);
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
|
||||
int utype, const ASN1_ITEM *it);
|
||||
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag,
|
||||
int aclass, char opt);
|
||||
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag, int aclass,
|
||||
char opt, int depth);
|
||||
char opt, CRYPTO_BUFFER *buf, int depth);
|
||||
|
||||
// Table to convert tags to bit values, used for MSTRING type
|
||||
static const unsigned long tag2bit[31] = {
|
||||
@@ -134,6 +136,23 @@ unsigned long ASN1_tag2bit(int tag) {
|
||||
return tag2bit[tag];
|
||||
}
|
||||
|
||||
static int is_supported_universal_type(int tag, int aclass) {
|
||||
if (aclass != V_ASN1_UNIVERSAL) {
|
||||
return 0;
|
||||
}
|
||||
return tag == V_ASN1_OBJECT || tag == V_ASN1_NULL || tag == V_ASN1_BOOLEAN ||
|
||||
tag == V_ASN1_BIT_STRING || tag == V_ASN1_INTEGER ||
|
||||
tag == V_ASN1_ENUMERATED || tag == V_ASN1_OCTET_STRING ||
|
||||
tag == V_ASN1_NUMERICSTRING || tag == V_ASN1_PRINTABLESTRING ||
|
||||
tag == V_ASN1_T61STRING || tag == V_ASN1_VIDEOTEXSTRING ||
|
||||
tag == V_ASN1_IA5STRING || tag == V_ASN1_UTCTIME ||
|
||||
tag == V_ASN1_GENERALIZEDTIME || tag == V_ASN1_GRAPHICSTRING ||
|
||||
tag == V_ASN1_VISIBLESTRING || tag == V_ASN1_GENERALSTRING ||
|
||||
tag == V_ASN1_UNIVERSALSTRING || tag == V_ASN1_BMPSTRING ||
|
||||
tag == V_ASN1_UTF8STRING || tag == V_ASN1_SET ||
|
||||
tag == V_ASN1_SEQUENCE;
|
||||
}
|
||||
|
||||
// Macro to initialize and invalidate the cache
|
||||
|
||||
// Decode an ASN1 item, this currently behaves just like a standard 'd2i'
|
||||
@@ -143,25 +162,40 @@ unsigned long ASN1_tag2bit(int tag) {
|
||||
|
||||
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
const ASN1_ITEM *it) {
|
||||
ASN1_VALUE *ptmpval = NULL;
|
||||
if (!pval) {
|
||||
pval = &ptmpval;
|
||||
ASN1_VALUE *ret = NULL;
|
||||
if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0,
|
||||
/*buf=*/NULL, /*depth=*/0) <= 0) {
|
||||
// Clean up, in case the caller left a partial object.
|
||||
//
|
||||
// TODO(davidben): I don't think it can leave one, but the codepaths below
|
||||
// are a bit inconsistent. Revisit this when rewriting this function.
|
||||
ASN1_item_ex_free(&ret, it);
|
||||
}
|
||||
|
||||
if (asn1_item_ex_d2i(pval, in, len, it, -1, 0, 0, 0) > 0) {
|
||||
return *pval;
|
||||
// If the caller supplied an output pointer, free the old one and replace it
|
||||
// with |ret|. This differs from OpenSSL slightly in that we don't support
|
||||
// object reuse. We run this on both success and failure. On failure, even
|
||||
// with object reuse, OpenSSL destroys the previous object.
|
||||
if (pval != NULL) {
|
||||
ASN1_item_ex_free(pval, it);
|
||||
*pval = ret;
|
||||
}
|
||||
return NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
|
||||
// tag mismatch return -1 to handle OPTIONAL
|
||||
//
|
||||
// TODO(davidben): Historically, all functions in this file had to account for
|
||||
// |*pval| containing an arbitrary existing value. This is no longer the case
|
||||
// because |ASN1_item_d2i| now always starts from NULL. As part of rewriting
|
||||
// this function, take the simplified assumptions into account. Though we must
|
||||
// still account for the internal calls to |ASN1_item_ex_new|.
|
||||
|
||||
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
long len, const ASN1_ITEM *it, int tag, int aclass,
|
||||
char opt, int depth) {
|
||||
char opt, CRYPTO_BUFFER *buf, int depth) {
|
||||
const ASN1_TEMPLATE *tt, *errtt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
const unsigned char *p = NULL, *q;
|
||||
unsigned char oclass;
|
||||
char cst, isopt;
|
||||
@@ -169,12 +203,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
int otag;
|
||||
int ret = 0;
|
||||
ASN1_VALUE **pchptr;
|
||||
int combine = aclass & ASN1_TFLG_COMBINE;
|
||||
aclass &= ~ASN1_TFLG_COMBINE;
|
||||
if (!pval) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (buf != NULL) {
|
||||
assert(CRYPTO_BUFFER_data(buf) <= *in &&
|
||||
*in + len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
|
||||
}
|
||||
|
||||
// Bound |len| to comfortably fit in an int. Lengths in this module often
|
||||
// switch between int and long without overflow checks.
|
||||
if (len > INT_MAX / 2) {
|
||||
@@ -197,7 +234,8 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
return asn1_template_ex_d2i(pval, in, len, it->templates, opt, depth);
|
||||
return asn1_template_ex_d2i(pval, in, len, it->templates, opt, buf,
|
||||
depth);
|
||||
}
|
||||
return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt);
|
||||
break;
|
||||
@@ -238,10 +276,15 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
}
|
||||
return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0);
|
||||
|
||||
case ASN1_ITYPE_EXTERN:
|
||||
// Use new style d2i
|
||||
ef = it->funcs;
|
||||
return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, NULL);
|
||||
case ASN1_ITYPE_EXTERN: {
|
||||
// We don't support implicit tagging with external types.
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
goto err;
|
||||
}
|
||||
const ASN1_EXTERN_FUNCS *ef = it->funcs;
|
||||
return ef->asn1_ex_d2i(pval, in, len, it, opt, NULL);
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_CHOICE: {
|
||||
// It never makes sense for CHOICE types to have implicit tagging, so if
|
||||
@@ -275,7 +318,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
|
||||
pchptr = asn1_get_field_ptr(pval, tt);
|
||||
// We mark field as OPTIONAL so its absence can be recognised.
|
||||
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, depth);
|
||||
ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, buf, depth);
|
||||
// If field not present, try the next one
|
||||
if (ret == -1) {
|
||||
continue;
|
||||
@@ -377,11 +420,11 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
if (i == (it->tcount - 1)) {
|
||||
isopt = 0;
|
||||
} else {
|
||||
isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
|
||||
isopt = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
}
|
||||
// attempt to read in field, allowing each to be OPTIONAL
|
||||
|
||||
ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, depth);
|
||||
ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, buf, depth);
|
||||
if (!ret) {
|
||||
errtt = seqtt;
|
||||
goto err;
|
||||
@@ -420,7 +463,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
}
|
||||
}
|
||||
// Save encoding
|
||||
if (!asn1_enc_save(pval, *in, p - *in, it)) {
|
||||
if (!asn1_enc_save(pval, *in, p - *in, it, buf)) {
|
||||
goto auxerr;
|
||||
}
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) {
|
||||
@@ -436,9 +479,7 @@ static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
err:
|
||||
if (combine == 0) {
|
||||
ASN1_item_ex_free(pval, it);
|
||||
}
|
||||
ASN1_item_ex_free(pval, it);
|
||||
if (errtt) {
|
||||
ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname);
|
||||
} else {
|
||||
@@ -449,8 +490,9 @@ err:
|
||||
|
||||
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) {
|
||||
return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, 0);
|
||||
CRYPTO_BUFFER *buf) {
|
||||
return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, buf,
|
||||
/*depth=*/0);
|
||||
}
|
||||
|
||||
// Templates are handled with two separate functions. One handles any
|
||||
@@ -458,15 +500,15 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
|
||||
|
||||
static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long inlen, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth) {
|
||||
int flags, aclass;
|
||||
CRYPTO_BUFFER *buf, int depth) {
|
||||
int aclass;
|
||||
int ret;
|
||||
long len;
|
||||
const unsigned char *p, *q;
|
||||
if (!val) {
|
||||
return 0;
|
||||
}
|
||||
flags = tt->flags;
|
||||
uint32_t flags = tt->flags;
|
||||
aclass = flags & ASN1_TFLG_TAG_CLASS;
|
||||
|
||||
p = *in;
|
||||
@@ -490,7 +532,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
return 0;
|
||||
}
|
||||
// We've found the field so it can't be OPTIONAL now
|
||||
ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, depth);
|
||||
ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
@@ -503,7 +545,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, depth);
|
||||
return asn1_template_noexp_d2i(val, in, inlen, tt, opt, buf, depth);
|
||||
}
|
||||
|
||||
*in = p;
|
||||
@@ -516,14 +558,14 @@ err:
|
||||
|
||||
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
long len, const ASN1_TEMPLATE *tt, char opt,
|
||||
int depth) {
|
||||
int flags, aclass;
|
||||
CRYPTO_BUFFER *buf, int depth) {
|
||||
int aclass;
|
||||
int ret;
|
||||
const unsigned char *p;
|
||||
if (!val) {
|
||||
return 0;
|
||||
}
|
||||
flags = tt->flags;
|
||||
uint32_t flags = tt->flags;
|
||||
aclass = flags & ASN1_TFLG_TAG_CLASS;
|
||||
|
||||
p = *in;
|
||||
@@ -565,7 +607,6 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
}
|
||||
|
||||
if (!*val) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -574,22 +615,21 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
ASN1_VALUE *skfield;
|
||||
const unsigned char *q = p;
|
||||
skfield = NULL;
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), -1, 0,
|
||||
0, depth)) {
|
||||
if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
|
||||
/*tag=*/-1, /*aclass=*/0, /*opt=*/0, buf, depth)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
}
|
||||
len -= p - q;
|
||||
if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
|
||||
ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item));
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
} else if (flags & ASN1_TFLG_IMPTAG) {
|
||||
// IMPLICIT tagging
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag,
|
||||
aclass, opt, depth);
|
||||
aclass, opt, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
@@ -598,8 +638,8 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
|
||||
}
|
||||
} else {
|
||||
// Nothing special
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), -1,
|
||||
tt->flags & ASN1_TFLG_COMBINE, opt, depth);
|
||||
ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1,
|
||||
/*aclass=*/0, opt, buf, depth);
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
goto err;
|
||||
@@ -654,7 +694,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
|
||||
return 0;
|
||||
}
|
||||
if (oclass != V_ASN1_UNIVERSAL) {
|
||||
if (!is_supported_universal_type(utype, oclass)) {
|
||||
utype = V_ASN1_OTHER;
|
||||
}
|
||||
}
|
||||
@@ -709,7 +749,7 @@ err:
|
||||
|
||||
// Translate ASN1 content octets into a structure
|
||||
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
|
||||
int utype, const ASN1_ITEM *it) {
|
||||
ASN1_VALUE **opval = NULL;
|
||||
ASN1_STRING *stmp;
|
||||
@@ -798,6 +838,12 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
case V_ASN1_OTHER:
|
||||
case V_ASN1_SET:
|
||||
case V_ASN1_SEQUENCE:
|
||||
// TODO(crbug.com/boringssl/412): This default case should be removed, now
|
||||
// that we've resolved https://crbug.com/boringssl/561. However, it is still
|
||||
// needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE|
|
||||
// broadly doesn't tolerate unrecognized universal tags, but except for
|
||||
// eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the
|
||||
// X509Test.NameAttributeValues test.
|
||||
default: {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, cont, (size_t)len);
|
||||
@@ -847,7 +893,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
if (!*pval) {
|
||||
stmp = ASN1_STRING_type_new(utype);
|
||||
if (!stmp) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
*pval = (ASN1_VALUE *)stmp;
|
||||
@@ -856,7 +901,6 @@ static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
|
||||
stmp->type = utype;
|
||||
}
|
||||
if (!ASN1_STRING_set(stmp, cont, len)) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ASN1_STRING_free(stmp);
|
||||
*pval = NULL;
|
||||
goto err;
|
||||
|
||||
@@ -78,7 +78,8 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *out_omit,
|
||||
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
int skcontlen, const ASN1_ITEM *item, int do_sort);
|
||||
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
const ASN1_TEMPLATE *tt, int tag, int aclass);
|
||||
const ASN1_TEMPLATE *tt, int tag, int aclass,
|
||||
int optional);
|
||||
|
||||
// Top level i2d equivalents
|
||||
|
||||
@@ -91,12 +92,12 @@ int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) {
|
||||
}
|
||||
buf = OPENSSL_malloc(len);
|
||||
if (!buf) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
p = buf;
|
||||
int len2 = ASN1_item_ex_i2d(&val, &p, it, /*tag=*/-1, /*aclass=*/0);
|
||||
if (len2 <= 0) {
|
||||
OPENSSL_free(buf);
|
||||
return len2;
|
||||
}
|
||||
assert(len == len2);
|
||||
@@ -144,11 +145,13 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
switch (it->itype) {
|
||||
case ASN1_ITYPE_PRIMITIVE:
|
||||
if (it->templates) {
|
||||
// This is an |ASN1_ITEM_TEMPLATE|.
|
||||
if (it->templates->flags & ASN1_TFLG_OPTIONAL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass);
|
||||
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass,
|
||||
optional);
|
||||
}
|
||||
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass, optional);
|
||||
|
||||
@@ -179,13 +182,17 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
ASN1_VALUE **pchval = asn1_get_field_ptr(pval, chtt);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0);
|
||||
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0, /*optional=*/0);
|
||||
}
|
||||
|
||||
case ASN1_ITYPE_EXTERN: {
|
||||
// If new style i2d it does all the work
|
||||
// We don't support implicit tagging with external types.
|
||||
if (tag != -1) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
|
||||
return -1;
|
||||
}
|
||||
const ASN1_EXTERN_FUNCS *ef = it->funcs;
|
||||
int ret = ef->asn1_ex_i2d(pval, out, it, tag, aclass);
|
||||
int ret = ef->asn1_ex_i2d(pval, out, it);
|
||||
if (ret == 0) {
|
||||
// |asn1_ex_i2d| should never return zero. We have already checked
|
||||
// for optional values generically, and |ASN1_ITYPE_EXTERN| fields
|
||||
@@ -223,7 +230,8 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0);
|
||||
tmplen =
|
||||
asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0, /*optional=*/0);
|
||||
if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -244,7 +252,8 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
return -1;
|
||||
}
|
||||
pseqval = asn1_get_field_ptr(pval, seqtt);
|
||||
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0) < 0) {
|
||||
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0, /*optional=*/0) <
|
||||
0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -259,13 +268,13 @@ int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out,
|
||||
|
||||
// asn1_template_ex_i2d behaves like |asn1_item_ex_i2d_opt| but uses an
|
||||
// |ASN1_TEMPLATE| instead of an |ASN1_ITEM|. An |ASN1_TEMPLATE| wraps an
|
||||
// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc. Instead of
|
||||
// taking an |optional| parameter, it uses the |ASN1_TFLG_OPTIONAL| flag.
|
||||
// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc.
|
||||
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;
|
||||
const ASN1_TEMPLATE *tt, int tag, int iclass,
|
||||
int optional) {
|
||||
int i, ret, ttag, tclass;
|
||||
size_t j;
|
||||
flags = tt->flags;
|
||||
uint32_t flags = tt->flags;
|
||||
|
||||
// Historically, |iclass| was repurposed to pass additional flags into the
|
||||
// encoding process.
|
||||
@@ -294,7 +303,12 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
|
||||
tclass = 0;
|
||||
}
|
||||
|
||||
const int optional = (flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
// The template may itself by marked as optional, or this may be the template
|
||||
// of an |ASN1_ITEM_TEMPLATE| type which was contained inside an outer
|
||||
// optional template. (They cannot both be true because the
|
||||
// |ASN1_ITEM_TEMPLATE| codepath rejects optional templates.)
|
||||
assert(!optional || (flags & ASN1_TFLG_OPTIONAL) == 0);
|
||||
optional = optional || (flags & ASN1_TFLG_OPTIONAL) != 0;
|
||||
|
||||
// At this point 'ttag' contains the outer tag to use, and 'tclass' is the
|
||||
// class.
|
||||
@@ -447,7 +461,6 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
|
||||
unsigned char *const buf = OPENSSL_malloc(skcontlen);
|
||||
DER_ENC *encoded = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) * sizeof(*encoded));
|
||||
if (encoded == NULL || buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -631,7 +644,7 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit,
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
tbool = (ASN1_BOOLEAN *)pval;
|
||||
if (*tbool == -1) {
|
||||
if (*tbool == ASN1_BOOLEAN_NONE) {
|
||||
*out_omit = 1;
|
||||
return 0;
|
||||
}
|
||||
@@ -678,12 +691,19 @@ static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit,
|
||||
case V_ASN1_UTF8STRING:
|
||||
case V_ASN1_SEQUENCE:
|
||||
case V_ASN1_SET:
|
||||
// This is not a valid |ASN1_ITEM| type, but it appears in |ASN1_TYPE|.
|
||||
case V_ASN1_OTHER:
|
||||
// TODO(crbug.com/boringssl/412): This default case should be removed, now
|
||||
// that we've resolved https://crbug.com/boringssl/561. However, it is still
|
||||
// needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE|
|
||||
// broadly doesn't tolerate unrecognized universal tags, but except for
|
||||
// eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the
|
||||
// X509Test.NameAttributeValues test.
|
||||
default:
|
||||
// All based on ASN1_STRING and handled the same
|
||||
strtmp = (ASN1_STRING *)*pval;
|
||||
cont = strtmp->data;
|
||||
len = strtmp->length;
|
||||
|
||||
break;
|
||||
}
|
||||
if (cout && len) {
|
||||
|
||||
@@ -66,15 +66,10 @@
|
||||
// Free up an ASN1 structure
|
||||
|
||||
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(&val, it, 0);
|
||||
ASN1_item_ex_free(&val, it);
|
||||
}
|
||||
|
||||
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
asn1_item_combine_free(pval, it, 0);
|
||||
}
|
||||
|
||||
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;
|
||||
int i;
|
||||
@@ -117,10 +112,8 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -160,57 +153,33 @@ void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
if (asn1_cb) {
|
||||
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL);
|
||||
}
|
||||
if (!combine) {
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
}
|
||||
OPENSSL_free(*pval);
|
||||
*pval = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
size_t i;
|
||||
if (tt->flags & ASN1_TFLG_SK_MASK) {
|
||||
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
|
||||
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp;
|
||||
vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 0);
|
||||
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
|
||||
ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);
|
||||
ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
|
||||
}
|
||||
sk_ASN1_VALUE_free(sk);
|
||||
*pval = NULL;
|
||||
} else {
|
||||
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
|
||||
tt->flags & ASN1_TFLG_COMBINE);
|
||||
ASN1_item_ex_free(pval, ASN1_ITEM_ptr(tt->item));
|
||||
}
|
||||
}
|
||||
|
||||
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
int utype;
|
||||
// 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 (utype != V_ASN1_BOOLEAN && !*pval) {
|
||||
return;
|
||||
}
|
||||
} else if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
if (!*pval) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
utype = it->utype;
|
||||
if ((utype != V_ASN1_BOOLEAN) && !*pval) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
int utype = it->itype == ASN1_ITYPE_MSTRING ? -1 : it->utype;
|
||||
switch (utype) {
|
||||
case V_ASN1_OBJECT:
|
||||
ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
|
||||
@@ -218,9 +187,9 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
if (it) {
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
*(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
|
||||
} else {
|
||||
*(ASN1_BOOLEAN *)pval = -1;
|
||||
*(ASN1_BOOLEAN *)pval = ASN1_BOOLEAN_NONE;
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -228,8 +197,10 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
break;
|
||||
|
||||
case V_ASN1_ANY:
|
||||
ASN1_primitive_free(pval, NULL);
|
||||
OPENSSL_free(*pval);
|
||||
if (*pval != NULL) {
|
||||
asn1_type_cleanup((ASN1_TYPE *)*pval);
|
||||
OPENSSL_free(*pval);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -67,8 +67,6 @@
|
||||
#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);
|
||||
@@ -86,11 +84,6 @@ ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) {
|
||||
// Allocate an ASN1 structure
|
||||
|
||||
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
return asn1_item_ex_combine_new(pval, it, 0);
|
||||
}
|
||||
|
||||
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
int combine) {
|
||||
const ASN1_TEMPLATE *tt = NULL;
|
||||
const ASN1_EXTERN_FUNCS *ef;
|
||||
ASN1_VALUE **pseqval;
|
||||
@@ -134,13 +127,11 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_set_choice_selector(pval, -1, it);
|
||||
if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
|
||||
goto auxerr2;
|
||||
@@ -160,15 +151,13 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!combine) {
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
*pval = OPENSSL_malloc(it->size);
|
||||
if (!*pval) {
|
||||
goto memerr;
|
||||
}
|
||||
OPENSSL_memset(*pval, 0, it->size);
|
||||
asn1_refcount_set_one(pval, it);
|
||||
asn1_enc_init(pval, it);
|
||||
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
|
||||
pseqval = asn1_get_field_ptr(pval, tt);
|
||||
if (!ASN1_template_new(pseqval, tt)) {
|
||||
@@ -184,13 +173,12 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
|
||||
return 1;
|
||||
|
||||
memerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
ASN1_item_ex_free(pval, it);
|
||||
memerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
|
||||
auxerr2:
|
||||
asn1_item_combine_free(pval, it, combine);
|
||||
ASN1_item_ex_free(pval, it);
|
||||
auxerr:
|
||||
OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
|
||||
return 0;
|
||||
@@ -246,7 +234,6 @@ static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
STACK_OF(ASN1_VALUE) *skval;
|
||||
skval = sk_ASN1_VALUE_new_null();
|
||||
if (!skval) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
ret = 0;
|
||||
goto done;
|
||||
}
|
||||
@@ -255,7 +242,7 @@ static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
goto done;
|
||||
}
|
||||
// Otherwise pass it back to the item routine
|
||||
ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
|
||||
ret = ASN1_item_ex_new(pval, it);
|
||||
done:
|
||||
return ret;
|
||||
}
|
||||
@@ -273,9 +260,6 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
// all the old functions.
|
||||
|
||||
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
ASN1_TYPE *typ;
|
||||
int utype;
|
||||
|
||||
if (!it) {
|
||||
return 0;
|
||||
}
|
||||
@@ -284,6 +268,7 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
// |ASN1_PRIMITIVE_FUNCS| table of calbacks.
|
||||
assert(it->funcs == NULL);
|
||||
|
||||
int utype;
|
||||
if (it->itype == ASN1_ITYPE_MSTRING) {
|
||||
utype = -1;
|
||||
} else {
|
||||
@@ -295,15 +280,15 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
return 1;
|
||||
|
||||
case V_ASN1_BOOLEAN:
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
*(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
|
||||
return 1;
|
||||
|
||||
case V_ASN1_NULL:
|
||||
*pval = (ASN1_VALUE *)1;
|
||||
return 1;
|
||||
|
||||
case V_ASN1_ANY:
|
||||
typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
|
||||
case V_ASN1_ANY: {
|
||||
ASN1_TYPE *typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
|
||||
if (!typ) {
|
||||
return 0;
|
||||
}
|
||||
@@ -311,6 +296,7 @@ static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
typ->type = -1;
|
||||
*pval = (ASN1_VALUE *)typ;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
*pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
|
||||
@@ -333,7 +319,7 @@ static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
utype = it->utype;
|
||||
}
|
||||
if (utype == V_ASN1_BOOLEAN) {
|
||||
*(ASN1_BOOLEAN *)pval = it->size;
|
||||
*(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
|
||||
} else {
|
||||
*pval = NULL;
|
||||
}
|
||||
|
||||
@@ -107,9 +107,9 @@ IMPLEMENT_ASN1_FUNCTIONS_const_fname(ASN1_STRING, DIRECTORYSTRING,
|
||||
DIRECTORYSTRING)
|
||||
|
||||
// Three separate BOOLEAN type: normal, DEFAULT TRUE and DEFAULT FALSE
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, -1)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, 1)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_BOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_NONE)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_TBOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_TRUE)
|
||||
IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, ASN1_BOOLEAN_FALSE)
|
||||
|
||||
ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
|
||||
ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_obj.h>
|
||||
#include <CCryptoBoringSSL_pool.h>
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
@@ -131,68 +132,62 @@ static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
}
|
||||
|
||||
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
|
||||
if (enc) {
|
||||
enc->enc = NULL;
|
||||
enc->len = 0;
|
||||
enc->alias_only = 0;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
enc->modified = 1;
|
||||
enc->buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
|
||||
if (enc) {
|
||||
if (!enc->alias_only) {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
enc->enc = NULL;
|
||||
enc->len = 0;
|
||||
enc->alias_only = 0;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
enc->modified = 1;
|
||||
asn1_encoding_clear(enc);
|
||||
}
|
||||
}
|
||||
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
|
||||
const ASN1_ITEM *it) {
|
||||
int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t in_len,
|
||||
const ASN1_ITEM *it, CRYPTO_BUFFER *buf) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
if (!enc) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!enc->alias_only) {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
|
||||
enc->alias_only = enc->alias_only_on_next_parse;
|
||||
enc->alias_only_on_next_parse = 0;
|
||||
|
||||
if (enc->alias_only) {
|
||||
asn1_encoding_clear(enc);
|
||||
if (buf != NULL) {
|
||||
assert(CRYPTO_BUFFER_data(buf) <= in &&
|
||||
in + in_len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
|
||||
CRYPTO_BUFFER_up_ref(buf);
|
||||
enc->buf = buf;
|
||||
enc->enc = (uint8_t *)in;
|
||||
} else {
|
||||
enc->enc = OPENSSL_malloc(inlen);
|
||||
enc->enc = OPENSSL_memdup(in, in_len);
|
||||
if (!enc->enc) {
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(enc->enc, in, inlen);
|
||||
}
|
||||
|
||||
enc->len = inlen;
|
||||
enc->modified = 0;
|
||||
|
||||
enc->len = in_len;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void asn1_encoding_clear(ASN1_ENCODING *enc) {
|
||||
if (enc->buf != NULL) {
|
||||
CRYPTO_BUFFER_free(enc->buf);
|
||||
} else {
|
||||
OPENSSL_free(enc->enc);
|
||||
}
|
||||
enc->enc = NULL;
|
||||
enc->len = 0;
|
||||
enc->buf = NULL;
|
||||
}
|
||||
|
||||
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
const ASN1_ITEM *it) {
|
||||
ASN1_ENCODING *enc;
|
||||
enc = asn1_get_enc_ptr(pval, it);
|
||||
if (!enc || enc->modified) {
|
||||
ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
|
||||
if (!enc || enc->len == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (out) {
|
||||
@@ -207,11 +202,7 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
|
||||
|
||||
// Given an ASN1_TEMPLATE get a pointer to a field
|
||||
ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
|
||||
ASN1_VALUE **pvaltmp;
|
||||
if (tt->flags & ASN1_TFLG_COMBINE) {
|
||||
return pval;
|
||||
}
|
||||
pvaltmp = offset2ptr(*pval, tt->offset);
|
||||
ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
|
||||
// NOTE for BOOLEAN types the field is just a plain int so we can't return
|
||||
// int **, so settle for (int *).
|
||||
return pvaltmp;
|
||||
|
||||
@@ -72,7 +72,6 @@
|
||||
BIO *BIO_new(const BIO_METHOD *method) {
|
||||
BIO *ret = OPENSSL_malloc(sizeof(BIO));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -202,7 +201,7 @@ int BIO_puts(BIO *bio, const char *in) {
|
||||
}
|
||||
|
||||
int BIO_flush(BIO *bio) {
|
||||
return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
|
||||
}
|
||||
|
||||
long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
|
||||
@@ -235,11 +234,11 @@ long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
|
||||
}
|
||||
|
||||
int BIO_reset(BIO *bio) {
|
||||
return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
|
||||
}
|
||||
|
||||
int BIO_eof(BIO *bio) {
|
||||
return BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
|
||||
}
|
||||
|
||||
void BIO_set_flags(BIO *bio, int flags) {
|
||||
@@ -339,7 +338,7 @@ size_t BIO_wpending(const BIO *bio) {
|
||||
}
|
||||
|
||||
int BIO_set_close(BIO *bio, int close_flag) {
|
||||
return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
|
||||
}
|
||||
|
||||
OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
|
||||
@@ -424,7 +423,7 @@ int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
|
||||
}
|
||||
|
||||
static int print_bio(const char *str, size_t len, void *bio) {
|
||||
return BIO_write((BIO *)bio, str, len);
|
||||
return BIO_write_all((BIO *)bio, str, len);
|
||||
}
|
||||
|
||||
void ERR_print_errors(BIO *bio) {
|
||||
@@ -463,9 +462,11 @@ static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
|
||||
OPENSSL_free(*out);
|
||||
return 0;
|
||||
}
|
||||
const size_t todo = len - done;
|
||||
assert(todo < INT_MAX);
|
||||
const int n = BIO_read(bio, *out + done, todo);
|
||||
size_t todo = len - done;
|
||||
if (todo > INT_MAX) {
|
||||
todo = INT_MAX;
|
||||
}
|
||||
const int n = BIO_read(bio, *out + done, (int)todo);
|
||||
if (n == 0) {
|
||||
*out_len = done;
|
||||
return 1;
|
||||
@@ -609,7 +610,6 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
|
||||
|
||||
*out = OPENSSL_malloc(len);
|
||||
if (*out == NULL) {
|
||||
OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_memcpy(*out, header, header_len);
|
||||
@@ -628,14 +628,14 @@ void BIO_set_retry_special(BIO *bio) {
|
||||
|
||||
int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
|
||||
|
||||
static struct CRYPTO_STATIC_MUTEX g_index_lock = CRYPTO_STATIC_MUTEX_INIT;
|
||||
static CRYPTO_MUTEX g_index_lock = CRYPTO_MUTEX_INIT;
|
||||
static int g_index = BIO_TYPE_START;
|
||||
|
||||
int BIO_get_new_index(void) {
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&g_index_lock);
|
||||
CRYPTO_MUTEX_lock_write(&g_index_lock);
|
||||
// If |g_index| exceeds 255, it will collide with the flags bits.
|
||||
int ret = g_index > 255 ? -1 : g_index++;
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&g_index_lock);
|
||||
CRYPTO_MUTEX_unlock_write(&g_index_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
BIO *BIO_new_mem_buf(const void *buf, int len) {
|
||||
BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
|
||||
BIO *ret;
|
||||
BUF_MEM *b;
|
||||
const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
|
||||
@@ -130,13 +130,15 @@ static int mem_free(BIO *bio) {
|
||||
}
|
||||
|
||||
static int mem_read(BIO *bio, char *out, int outl) {
|
||||
int ret;
|
||||
BUF_MEM *b = (BUF_MEM*) bio->ptr;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
ret = outl;
|
||||
if (b->length < INT_MAX && ret > (int)b->length) {
|
||||
ret = b->length;
|
||||
if (outl <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BUF_MEM *b = bio->ptr;
|
||||
int ret = outl;
|
||||
if ((size_t)ret > b->length) {
|
||||
ret = (int)b->length;
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
@@ -157,70 +159,53 @@ static int mem_read(BIO *bio, char *out, int outl) {
|
||||
}
|
||||
|
||||
static int mem_write(BIO *bio, const char *in, int inl) {
|
||||
int ret = -1;
|
||||
int blen;
|
||||
BUF_MEM *b;
|
||||
|
||||
b = (BUF_MEM *)bio->ptr;
|
||||
BIO_clear_retry_flags(bio);
|
||||
if (inl <= 0) {
|
||||
return 0; // Successfully write zero bytes.
|
||||
}
|
||||
|
||||
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
|
||||
OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
|
||||
goto err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
blen = b->length;
|
||||
if (INT_MAX - blen < inl) {
|
||||
goto err;
|
||||
BUF_MEM *b = bio->ptr;
|
||||
if (!BUF_MEM_append(b, in, inl)) {
|
||||
return -1;
|
||||
}
|
||||
if (BUF_MEM_grow_clean(b, blen + inl) != ((size_t) blen) + inl) {
|
||||
goto err;
|
||||
}
|
||||
OPENSSL_memcpy(&b->data[blen], in, inl);
|
||||
ret = inl;
|
||||
|
||||
err:
|
||||
return ret;
|
||||
return inl;
|
||||
}
|
||||
|
||||
static int mem_gets(BIO *bio, char *buf, int size) {
|
||||
int i, j;
|
||||
char *p;
|
||||
BUF_MEM *b = (BUF_MEM *)bio->ptr;
|
||||
|
||||
BIO_clear_retry_flags(bio);
|
||||
j = b->length;
|
||||
if (size - 1 < j) {
|
||||
j = size - 1;
|
||||
}
|
||||
if (j <= 0) {
|
||||
if (size > 0) {
|
||||
*buf = 0;
|
||||
}
|
||||
if (size <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
p = b->data;
|
||||
for (i = 0; i < j; i++) {
|
||||
if (p[i] == '\n') {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
// The buffer size includes space for the trailing NUL, so we can read at most
|
||||
// one fewer byte.
|
||||
BUF_MEM *b = bio->ptr;
|
||||
int ret = size - 1;
|
||||
if ((size_t)ret > b->length) {
|
||||
ret = (int)b->length;
|
||||
}
|
||||
|
||||
// i is now the max num of bytes to copy, either j or up to and including the
|
||||
// first newline
|
||||
|
||||
i = mem_read(bio, buf, i);
|
||||
if (i > 0) {
|
||||
buf[i] = '\0';
|
||||
// Stop at the first newline.
|
||||
const char *newline = OPENSSL_memchr(b->data, '\n', ret);
|
||||
if (newline != NULL) {
|
||||
ret = (int)(newline - b->data + 1);
|
||||
}
|
||||
return i;
|
||||
|
||||
ret = mem_read(bio, buf, ret);
|
||||
if (ret >= 0) {
|
||||
buf[ret] = '\0';
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
|
||||
long ret = 1;
|
||||
char **pptr;
|
||||
|
||||
BUF_MEM *b = (BUF_MEM *)bio->ptr;
|
||||
|
||||
@@ -246,8 +231,8 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
|
||||
case BIO_CTRL_INFO:
|
||||
ret = (long)b->length;
|
||||
if (ptr != NULL) {
|
||||
pptr = (char **)ptr;
|
||||
*pptr = (char *)&b->data[0];
|
||||
char **pptr = ptr;
|
||||
*pptr = b->data;
|
||||
}
|
||||
break;
|
||||
case BIO_C_SET_BUF_MEM:
|
||||
@@ -257,8 +242,8 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
|
||||
break;
|
||||
case BIO_C_GET_BUF_MEM_PTR:
|
||||
if (ptr != NULL) {
|
||||
pptr = (char **)ptr;
|
||||
*pptr = (char *)b;
|
||||
BUF_MEM **pptr = ptr;
|
||||
*pptr = b;
|
||||
}
|
||||
break;
|
||||
case BIO_CTRL_GET_CLOSE:
|
||||
@@ -308,17 +293,17 @@ int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
|
||||
}
|
||||
|
||||
long BIO_get_mem_data(BIO *bio, char **contents) {
|
||||
return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
|
||||
return BIO_ctrl(bio, BIO_CTRL_INFO, 0, contents);
|
||||
}
|
||||
|
||||
int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
|
||||
return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
|
||||
return (int)BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, out);
|
||||
}
|
||||
|
||||
int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, b);
|
||||
}
|
||||
|
||||
int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
|
||||
}
|
||||
@@ -56,7 +56,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
#if !defined(OPENSSL_TRUSTY)
|
||||
#if !defined(OPENSSL_NO_SOCK)
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
@@ -233,7 +233,7 @@ static int conn_state(BIO *bio, BIO_CONNECT *c) {
|
||||
BIO_clear_retry_flags(bio);
|
||||
ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
|
||||
if (ret < 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
|
||||
c->state = BIO_CONN_S_BLOCKED_CONNECT;
|
||||
bio->retry_reason = BIO_RR_CONNECT;
|
||||
@@ -252,7 +252,7 @@ static int conn_state(BIO *bio, BIO_CONNECT *c) {
|
||||
case BIO_CONN_S_BLOCKED_CONNECT:
|
||||
i = bio_sock_error(bio->num);
|
||||
if (i) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
|
||||
c->state = BIO_CONN_S_BLOCKED_CONNECT;
|
||||
bio->retry_reason = BIO_RR_CONNECT;
|
||||
@@ -363,10 +363,10 @@ static int conn_read(BIO *bio, char *out, int out_len) {
|
||||
}
|
||||
|
||||
bio_clear_socket_error();
|
||||
ret = recv(bio->num, out, out_len, 0);
|
||||
ret = (int)recv(bio->num, out, out_len, 0);
|
||||
BIO_clear_retry_flags(bio);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_retry_read(bio);
|
||||
}
|
||||
}
|
||||
@@ -387,10 +387,10 @@ static int conn_write(BIO *bio, const char *in, int in_len) {
|
||||
}
|
||||
|
||||
bio_clear_socket_error();
|
||||
ret = send(bio->num, in, in_len, 0);
|
||||
ret = (int)send(bio->num, in, in_len, 0);
|
||||
BIO_clear_retry_flags(bio);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_retry_write(bio);
|
||||
}
|
||||
}
|
||||
@@ -523,25 +523,25 @@ static const BIO_METHOD methods_connectp = {
|
||||
const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
|
||||
|
||||
int BIO_set_conn_hostname(BIO *bio, const char *name) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
|
||||
}
|
||||
|
||||
int BIO_set_conn_port(BIO *bio, const char *port_str) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
|
||||
}
|
||||
|
||||
int BIO_set_conn_int_port(BIO *bio, const int *port) {
|
||||
char buf[DECIMAL_SIZE(int) + 1];
|
||||
BIO_snprintf(buf, sizeof(buf), "%d", *port);
|
||||
snprintf(buf, sizeof(buf), "%d", *port);
|
||||
return BIO_set_conn_port(bio, buf);
|
||||
}
|
||||
|
||||
int BIO_set_nbio(BIO *bio, int on) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
|
||||
}
|
||||
|
||||
int BIO_do_connect(BIO *bio) {
|
||||
return BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL);
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
#endif // OPENSSL_NO_SOCK
|
||||
+28
-18
@@ -54,29 +54,39 @@
|
||||
* copied and put under another distribution licence
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <CCryptoBoringSSL_asn1.h>
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
int ASN1_PRINTABLE_type(const unsigned char *s, int len) {
|
||||
if (len < 0) {
|
||||
len = strlen((const char *)s);
|
||||
int bio_errno_should_retry(int return_value) {
|
||||
if (return_value != -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
return
|
||||
#ifdef EWOULDBLOCK
|
||||
errno == EWOULDBLOCK ||
|
||||
#endif
|
||||
#ifdef ENOTCONN
|
||||
errno == ENOTCONN ||
|
||||
#endif
|
||||
#ifdef EINTR
|
||||
errno == EINTR ||
|
||||
#endif
|
||||
#ifdef EAGAIN
|
||||
errno == EAGAIN ||
|
||||
#endif
|
||||
#ifdef EPROTO
|
||||
errno == EPROTO ||
|
||||
#endif
|
||||
#ifdef EINPROGRESS
|
||||
errno == EINPROGRESS ||
|
||||
#endif
|
||||
#ifdef EALREADY
|
||||
errno == EALREADY ||
|
||||
#endif
|
||||
0;
|
||||
}
|
||||
@@ -56,7 +56,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
#if !defined(OPENSSL_TRUSTY)
|
||||
#if !defined(OPENSSL_NO_POSIX_IO)
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
@@ -65,9 +65,6 @@
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <io.h>
|
||||
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
#include <windows.h>
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
#endif
|
||||
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
@@ -77,59 +74,18 @@ OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
static int bio_fd_non_fatal_error(int err) {
|
||||
if (
|
||||
#ifdef EWOULDBLOCK
|
||||
err == EWOULDBLOCK ||
|
||||
#endif
|
||||
#ifdef WSAEWOULDBLOCK
|
||||
err == WSAEWOULDBLOCK ||
|
||||
#endif
|
||||
#ifdef ENOTCONN
|
||||
err == ENOTCONN ||
|
||||
#endif
|
||||
#ifdef EINTR
|
||||
err == EINTR ||
|
||||
#endif
|
||||
#ifdef EAGAIN
|
||||
err == EAGAIN ||
|
||||
#endif
|
||||
#ifdef EPROTO
|
||||
err == EPROTO ||
|
||||
#endif
|
||||
#ifdef EINPROGRESS
|
||||
err == EINPROGRESS ||
|
||||
#endif
|
||||
#ifdef EALREADY
|
||||
err == EALREADY ||
|
||||
#endif
|
||||
0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
#define BORINGSSL_ERRNO (int)GetLastError()
|
||||
#define BORINGSSL_CLOSE _close
|
||||
#define BORINGSSL_LSEEK _lseek
|
||||
#define BORINGSSL_READ _read
|
||||
#define BORINGSSL_WRITE _write
|
||||
#else
|
||||
#define BORINGSSL_ERRNO errno
|
||||
#define BORINGSSL_CLOSE close
|
||||
#define BORINGSSL_LSEEK lseek
|
||||
#define BORINGSSL_READ read
|
||||
#define BORINGSSL_WRITE write
|
||||
#endif
|
||||
|
||||
int bio_fd_should_retry(int i) {
|
||||
if (i == -1) {
|
||||
return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
BIO *BIO_new_fd(int fd, int close_flag) {
|
||||
BIO *ret = BIO_new(BIO_s_fd());
|
||||
if (ret == NULL) {
|
||||
@@ -161,7 +117,7 @@ static int fd_read(BIO *b, char *out, int outl) {
|
||||
ret = (int)BORINGSSL_READ(b->num, out, outl);
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_errno_should_retry(ret)) {
|
||||
BIO_set_retry_read(b);
|
||||
}
|
||||
}
|
||||
@@ -173,7 +129,7 @@ static int fd_write(BIO *b, const char *in, int inl) {
|
||||
int ret = (int)BORINGSSL_WRITE(b->num, in, inl);
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_errno_should_retry(ret)) {
|
||||
BIO_set_retry_write(b);
|
||||
}
|
||||
}
|
||||
@@ -241,20 +197,24 @@ static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
}
|
||||
|
||||
static int fd_gets(BIO *bp, char *buf, int size) {
|
||||
char *ptr = buf;
|
||||
char *end = buf + size - 1;
|
||||
|
||||
if (size <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
|
||||
char *ptr = buf;
|
||||
char *end = buf + size - 1;
|
||||
while (ptr < end && fd_read(bp, ptr, 1) > 0) {
|
||||
char c = ptr[0];
|
||||
ptr++;
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ptr[0] = '\0';
|
||||
|
||||
return ptr - buf;
|
||||
// The output length is bounded by |size|.
|
||||
return (int)(ptr - buf);
|
||||
}
|
||||
|
||||
static const BIO_METHOD methods_fdp = {
|
||||
@@ -264,12 +224,12 @@ static const BIO_METHOD methods_fdp = {
|
||||
|
||||
const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
|
||||
|
||||
#endif // OPENSSL_NO_POSIX_IO
|
||||
|
||||
int BIO_set_fd(BIO *bio, int fd, int close_flag) {
|
||||
return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
|
||||
return (int)BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
|
||||
}
|
||||
|
||||
int BIO_get_fd(BIO *bio, int *out_fd) {
|
||||
return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
|
||||
return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
@@ -73,8 +73,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
#if !defined(OPENSSL_TRUSTY)
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -89,11 +87,20 @@
|
||||
#define BIO_FP_WRITE 0x04
|
||||
#define BIO_FP_APPEND 0x08
|
||||
|
||||
#if !defined(OPENSSL_NO_FILESYSTEM)
|
||||
#define fopen_if_available fopen
|
||||
#else
|
||||
static FILE *fopen_if_available(const char *path, const char *mode) {
|
||||
errno = ENOENT;
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
BIO *BIO_new_file(const char *filename, const char *mode) {
|
||||
BIO *ret;
|
||||
FILE *file;
|
||||
|
||||
file = fopen(filename, mode);
|
||||
file = fopen_if_available(filename, mode);
|
||||
if (file == NULL) {
|
||||
OPENSSL_PUT_SYSTEM_ERROR();
|
||||
|
||||
@@ -157,13 +164,11 @@ static int file_read(BIO *b, char *out, int outl) {
|
||||
}
|
||||
|
||||
static int file_write(BIO *b, const char *in, int inl) {
|
||||
int ret = 0;
|
||||
|
||||
if (!b->init) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = fwrite(in, inl, 1, (FILE *)b->ptr);
|
||||
int ret = (int)fwrite(in, inl, 1, (FILE *)b->ptr);
|
||||
if (ret > 0) {
|
||||
ret = inl;
|
||||
}
|
||||
@@ -174,7 +179,6 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
long ret = 1;
|
||||
FILE *fp = (FILE *)b->ptr;
|
||||
FILE **fpp;
|
||||
char p[4];
|
||||
|
||||
switch (cmd) {
|
||||
case BIO_CTRL_RESET:
|
||||
@@ -199,27 +203,28 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
case BIO_C_SET_FILENAME:
|
||||
file_free(b);
|
||||
b->shutdown = (int)num & BIO_CLOSE;
|
||||
const char *mode;
|
||||
if (num & BIO_FP_APPEND) {
|
||||
if (num & BIO_FP_READ) {
|
||||
OPENSSL_strlcpy(p, "a+", sizeof(p));
|
||||
mode = "a+";
|
||||
} else {
|
||||
OPENSSL_strlcpy(p, "a", sizeof(p));
|
||||
mode = "a";
|
||||
}
|
||||
} else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) {
|
||||
OPENSSL_strlcpy(p, "r+", sizeof(p));
|
||||
mode = "r+";
|
||||
} else if (num & BIO_FP_WRITE) {
|
||||
OPENSSL_strlcpy(p, "w", sizeof(p));
|
||||
mode = "w";
|
||||
} else if (num & BIO_FP_READ) {
|
||||
OPENSSL_strlcpy(p, "r", sizeof(p));
|
||||
mode = "r";
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE);
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
fp = fopen(ptr, p);
|
||||
fp = fopen_if_available(ptr, mode);
|
||||
if (fp == NULL) {
|
||||
OPENSSL_PUT_SYSTEM_ERROR();
|
||||
ERR_add_error_data(5, "fopen('", ptr, "','", p, "')");
|
||||
ERR_add_error_data(5, "fopen('", ptr, "','", mode, "')");
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB);
|
||||
ret = 0;
|
||||
break;
|
||||
@@ -253,20 +258,18 @@ static long file_ctrl(BIO *b, int cmd, long num, void *ptr) {
|
||||
}
|
||||
|
||||
static int file_gets(BIO *bp, char *buf, int size) {
|
||||
int ret = 0;
|
||||
|
||||
if (size == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!fgets(buf, size, (FILE *)bp->ptr)) {
|
||||
buf[0] = 0;
|
||||
goto err;
|
||||
// TODO(davidben): This doesn't distinguish error and EOF. This should check
|
||||
// |ferror| as in |file_read|.
|
||||
return 0;
|
||||
}
|
||||
ret = strlen(buf);
|
||||
|
||||
err:
|
||||
return ret;
|
||||
return (int)strlen(buf);
|
||||
}
|
||||
|
||||
static const BIO_METHOD methods_filep = {
|
||||
@@ -281,31 +284,32 @@ const BIO_METHOD *BIO_s_file(void) { return &methods_filep; }
|
||||
|
||||
|
||||
int BIO_get_fp(BIO *bio, FILE **out_file) {
|
||||
return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file);
|
||||
return (int)BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char *)out_file);
|
||||
}
|
||||
|
||||
int BIO_set_fp(BIO *bio, FILE *file, int close_flag) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *)file);
|
||||
}
|
||||
|
||||
int BIO_read_filename(BIO *bio, const char *filename) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
|
||||
(char *)filename);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ,
|
||||
(char *)filename);
|
||||
}
|
||||
|
||||
int BIO_write_filename(BIO *bio, const char *filename) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
|
||||
(char *)filename);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE,
|
||||
(char *)filename);
|
||||
}
|
||||
|
||||
int BIO_append_filename(BIO *bio, const char *filename) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
|
||||
(char *)filename);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND,
|
||||
(char *)filename);
|
||||
}
|
||||
|
||||
int BIO_rw_filename(BIO *bio, const char *filename) {
|
||||
return BIO_ctrl(bio, BIO_C_SET_FILENAME,
|
||||
BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SET_FILENAME,
|
||||
BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE,
|
||||
(char *)filename);
|
||||
}
|
||||
|
||||
long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
|
||||
@@ -313,5 +317,3 @@ long BIO_tell(BIO *bio) { return BIO_ctrl(bio, BIO_C_FILE_TELL, 0, NULL); }
|
||||
long BIO_seek(BIO *bio, long offset) {
|
||||
return BIO_ctrl(bio, BIO_C_FILE_SEEK, offset, NULL);
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
@@ -59,6 +59,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_SOCK)
|
||||
#if !defined(OPENSSL_WINDOWS)
|
||||
#if defined(OPENSSL_PNACL)
|
||||
// newlib uses u_short in socket.h without defining it.
|
||||
@@ -72,13 +73,16 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
typedef int socklen_t;
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_SOCK
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// BIO_ip_and_port_to_socket_and_addr creates a socket and fills in |*out_addr|
|
||||
#if !defined(OPENSSL_NO_SOCK)
|
||||
|
||||
// bio_ip_and_port_to_socket_and_addr creates a socket and fills in |*out_addr|
|
||||
// and |*out_addr_length| with the correct values for connecting to |hostname|
|
||||
// on |port_str|. It returns one on success or zero on error.
|
||||
int bio_ip_and_port_to_socket_and_addr(int *out_sock,
|
||||
@@ -87,21 +91,27 @@ int bio_ip_and_port_to_socket_and_addr(int *out_sock,
|
||||
const char *hostname,
|
||||
const char *port_str);
|
||||
|
||||
// BIO_socket_nbio sets whether |sock| is non-blocking. It returns one on
|
||||
// bio_socket_nbio sets whether |sock| is non-blocking. It returns one on
|
||||
// success and zero otherwise.
|
||||
int bio_socket_nbio(int sock, int on);
|
||||
|
||||
// BIO_clear_socket_error clears the last system socket error.
|
||||
// bio_clear_socket_error clears the last system socket error.
|
||||
//
|
||||
// TODO(fork): remove all callers of this.
|
||||
void bio_clear_socket_error(void);
|
||||
|
||||
// BIO_sock_error returns the last socket error on |sock|.
|
||||
// bio_sock_error returns the last socket error on |sock|.
|
||||
int bio_sock_error(int sock);
|
||||
|
||||
// BIO_fd_should_retry returns non-zero if |return_value| indicates an error
|
||||
// bio_socket_should_retry returns non-zero if |return_value| indicates an error
|
||||
// and the last socket error indicates that it's non-fatal.
|
||||
int bio_socket_should_retry(int return_value);
|
||||
|
||||
#endif // !OPENSSL_NO_SOCK
|
||||
|
||||
// bio_errno_should_retry returns non-zero if |return_value| indicates an error
|
||||
// and |errno| indicates that it's non-fatal.
|
||||
int bio_fd_should_retry(int return_value);
|
||||
int bio_errno_should_retry(int return_value);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -221,7 +221,8 @@ static int bio_read(BIO *bio, char *buf, int size_) {
|
||||
rest -= chunk;
|
||||
} while (rest);
|
||||
|
||||
return size;
|
||||
// |size| is bounded by the buffer size, which fits in |int|.
|
||||
return (int)size;
|
||||
}
|
||||
|
||||
static int bio_write(BIO *bio, const char *buf, int num_) {
|
||||
@@ -293,7 +294,8 @@ static int bio_write(BIO *bio, const char *buf, int num_) {
|
||||
buf += chunk;
|
||||
} while (rest);
|
||||
|
||||
return num;
|
||||
// |num| is bounded by the buffer size, which fits in |int|.
|
||||
return (int)num;
|
||||
}
|
||||
|
||||
static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
@@ -317,7 +319,6 @@ static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
}
|
||||
b1->buf = OPENSSL_malloc(b1->size);
|
||||
if (b1->buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
b1->len = 0;
|
||||
@@ -330,7 +331,6 @@ static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
|
||||
}
|
||||
b2->buf = OPENSSL_malloc(b2->size);
|
||||
if (b2->buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
b2->len = 0;
|
||||
@@ -479,5 +479,5 @@ size_t BIO_ctrl_get_write_guarantee(BIO *bio) {
|
||||
}
|
||||
|
||||
int BIO_shutdown_wr(BIO *bio) {
|
||||
return BIO_ctrl(bio, BIO_C_SHUTDOWN_WR, 0, NULL);
|
||||
return (int)BIO_ctrl(bio, BIO_C_SHUTDOWN_WR, 0, NULL);
|
||||
}
|
||||
@@ -83,7 +83,6 @@ int BIO_printf(BIO *bio, const char *format, ...) {
|
||||
out = OPENSSL_malloc(requested_len + 1);
|
||||
out_malloced = 1;
|
||||
if (out == NULL) {
|
||||
OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
|
||||
return -1;
|
||||
}
|
||||
va_start(args, format);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
/* crypto/bio/bss_sock.c */
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
@@ -57,7 +56,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
|
||||
#if !defined(OPENSSL_TRUSTY)
|
||||
#if !defined(OPENSSL_NO_SOCK)
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
@@ -105,7 +104,7 @@ static int sock_read(BIO *b, char *out, int outl) {
|
||||
#endif
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_retry_read(b);
|
||||
}
|
||||
}
|
||||
@@ -121,7 +120,7 @@ static int sock_write(BIO *b, const char *in, int inl) {
|
||||
#endif
|
||||
BIO_clear_retry_flags(b);
|
||||
if (ret <= 0) {
|
||||
if (bio_fd_should_retry(ret)) {
|
||||
if (bio_socket_should_retry(ret)) {
|
||||
BIO_set_retry_write(b);
|
||||
}
|
||||
}
|
||||
@@ -187,4 +186,4 @@ BIO *BIO_new_socket(int fd, int close_flag) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
#endif // OPENSSL_NO_SOCK
|
||||
@@ -20,7 +20,7 @@
|
||||
#include <CCryptoBoringSSL_bio.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#if !defined(OPENSSL_TRUSTY)
|
||||
#if !defined(OPENSSL_NO_SOCK)
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
@@ -121,4 +121,13 @@ int bio_sock_error(int sock) {
|
||||
return error;
|
||||
}
|
||||
|
||||
#endif // OPENSSL_TRUSTY
|
||||
int bio_socket_should_retry(int return_value) {
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
return return_value == -1 && WSAGetLastError() == WSAEWOULDBLOCK;
|
||||
#else
|
||||
// On POSIX platforms, sockets and fds are the same.
|
||||
return bio_errno_should_retry(return_value);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // OPENSSL_NO_SOCK
|
||||
@@ -55,10 +55,13 @@ static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x,
|
||||
v[b] = CRYPTO_rotr_u64(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) {
|
||||
static uint64_t blake2b_load(const uint8_t block[BLAKE2B_CBLOCK], size_t i) {
|
||||
return CRYPTO_load_u64_le(block + 8 * i);
|
||||
}
|
||||
|
||||
static void blake2b_transform(BLAKE2B_CTX *b2b,
|
||||
const uint8_t block[BLAKE2B_CBLOCK],
|
||||
size_t num_bytes, int is_final_block) {
|
||||
// https://tools.ietf.org/html/rfc7693#section-3.2
|
||||
uint64_t v[16];
|
||||
static_assert(sizeof(v) == sizeof(b2b->h) + sizeof(kIV), "");
|
||||
@@ -78,14 +81,22 @@ static void blake2b_transform(
|
||||
|
||||
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]]);
|
||||
blake2b_mix(v, 0, 4, 8, 12, blake2b_load(block, s[0]),
|
||||
blake2b_load(block, s[1]));
|
||||
blake2b_mix(v, 1, 5, 9, 13, blake2b_load(block, s[2]),
|
||||
blake2b_load(block, s[3]));
|
||||
blake2b_mix(v, 2, 6, 10, 14, blake2b_load(block, s[4]),
|
||||
blake2b_load(block, s[5]));
|
||||
blake2b_mix(v, 3, 7, 11, 15, blake2b_load(block, s[6]),
|
||||
blake2b_load(block, s[7]));
|
||||
blake2b_mix(v, 0, 5, 10, 15, blake2b_load(block, s[8]),
|
||||
blake2b_load(block, s[9]));
|
||||
blake2b_mix(v, 1, 6, 11, 12, blake2b_load(block, s[10]),
|
||||
blake2b_load(block, s[11]));
|
||||
blake2b_mix(v, 2, 7, 8, 13, blake2b_load(block, s[12]),
|
||||
blake2b_load(block, s[13]));
|
||||
blake2b_mix(v, 3, 4, 9, 14, blake2b_load(block, s[14]),
|
||||
blake2b_load(block, s[15]));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(b2b->h); i++) {
|
||||
@@ -105,13 +116,17 @@ void BLAKE2B256_Init(BLAKE2B_CTX *b2b) {
|
||||
}
|
||||
|
||||
void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) {
|
||||
const uint8_t *data = (const uint8_t *)in_data;
|
||||
if (len == 0) {
|
||||
// Work around a C language bug. See https://crbug.com/1019588.
|
||||
return;
|
||||
}
|
||||
|
||||
size_t todo = sizeof(b2b->block.bytes) - b2b->block_used;
|
||||
const uint8_t *data = in_data;
|
||||
size_t todo = sizeof(b2b->block) - b2b->block_used;
|
||||
if (todo > len) {
|
||||
todo = len;
|
||||
}
|
||||
OPENSSL_memcpy(&b2b->block.bytes[b2b->block_used], data, todo);
|
||||
OPENSSL_memcpy(&b2b->block[b2b->block_used], data, todo);
|
||||
b2b->block_used += todo;
|
||||
data += todo;
|
||||
len -= todo;
|
||||
@@ -122,26 +137,24 @@ void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) {
|
||||
|
||||
// More input remains therefore we must have filled |b2b->block|.
|
||||
assert(b2b->block_used == BLAKE2B_CBLOCK);
|
||||
blake2b_transform(b2b, b2b->block.words, BLAKE2B_CBLOCK,
|
||||
blake2b_transform(b2b, b2b->block, 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);
|
||||
blake2b_transform(b2b, data, BLAKE2B_CBLOCK, /*is_final_block=*/0);
|
||||
data += BLAKE2B_CBLOCK;
|
||||
len -= BLAKE2B_CBLOCK;
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(b2b->block.bytes, data, len);
|
||||
OPENSSL_memcpy(b2b->block, 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,
|
||||
OPENSSL_memset(&b2b->block[b2b->block_used], 0,
|
||||
sizeof(b2b->block) - b2b->block_used);
|
||||
blake2b_transform(b2b, b2b->block, b2b->block_used,
|
||||
/*is_final_block=*/1);
|
||||
static_assert(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), "");
|
||||
memcpy(out, b2b->h, BLAKE2B256_DIGEST_LENGTH);
|
||||
|
||||
@@ -81,7 +81,6 @@ char *BN_bn2hex(const BIGNUM *bn) {
|
||||
char *buf = OPENSSL_malloc(1 /* leading '-' */ + 1 /* zero is non-empty */ +
|
||||
width * BN_BYTES * 2 + 1 /* trailing NUL */);
|
||||
if (buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -133,18 +132,9 @@ static int decode_hex(BIGNUM *bn, const char *in, int in_len) {
|
||||
BN_ULONG word = 0;
|
||||
int j;
|
||||
for (j = todo; j > 0; j--) {
|
||||
char c = in[in_len - j];
|
||||
|
||||
BN_ULONG hex;
|
||||
if (c >= '0' && c <= '9') {
|
||||
hex = c - '0';
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
hex = c - 'a' + 10;
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
hex = c - 'A' + 10;
|
||||
} else {
|
||||
hex = 0;
|
||||
// This shouldn't happen. The caller checks |isxdigit|.
|
||||
uint8_t hex = 0;
|
||||
if (!OPENSSL_fromxdigit(&hex, in[in_len - j])) {
|
||||
// This shouldn't happen. The caller checks |OPENSSL_isxdigit|.
|
||||
assert(0);
|
||||
}
|
||||
word = (word << 4) | hex;
|
||||
@@ -240,7 +230,7 @@ err:
|
||||
}
|
||||
|
||||
int BN_hex2bn(BIGNUM **outp, const char *in) {
|
||||
return bn_x2bn(outp, in, decode_hex, isxdigit);
|
||||
return bn_x2bn(outp, in, decode_hex, OPENSSL_isxdigit);
|
||||
}
|
||||
|
||||
char *BN_bn2dec(const BIGNUM *a) {
|
||||
@@ -250,12 +240,12 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 16) ||
|
||||
!CBB_add_u8(&cbb, 0 /* trailing NUL */)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (BN_is_zero(a)) {
|
||||
if (!CBB_add_u8(&cbb, '0')) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
} else {
|
||||
copy = BN_dup(a);
|
||||
@@ -272,7 +262,7 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
const int add_leading_zeros = !BN_is_zero(copy);
|
||||
for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) {
|
||||
if (!CBB_add_u8(&cbb, '0' + word % 10)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
word /= 10;
|
||||
}
|
||||
@@ -282,13 +272,13 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
|
||||
if (BN_is_negative(a) &&
|
||||
!CBB_add_u8(&cbb, '-')) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
if (!CBB_finish(&cbb, &data, &len)) {
|
||||
goto cbb_err;
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Reverse the buffer.
|
||||
@@ -301,8 +291,6 @@ char *BN_bn2dec(const BIGNUM *a) {
|
||||
BN_free(copy);
|
||||
return (char *)data;
|
||||
|
||||
cbb_err:
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
err:
|
||||
BN_free(copy);
|
||||
CBB_cleanup(&cbb);
|
||||
@@ -310,7 +298,7 @@ err:
|
||||
}
|
||||
|
||||
int BN_dec2bn(BIGNUM **outp, const char *in) {
|
||||
return bn_x2bn(outp, in, decode_dec, isdigit);
|
||||
return bn_x2bn(outp, in, decode_dec, OPENSSL_isdigit);
|
||||
}
|
||||
|
||||
int BN_asc2bn(BIGNUM **outp, const char *in) {
|
||||
@@ -436,7 +424,6 @@ BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) {
|
||||
if (out == NULL) {
|
||||
out = BN_new();
|
||||
if (out == NULL) {
|
||||
OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
out_is_alloced = 1;
|
||||
|
||||
@@ -69,7 +69,6 @@ BUF_MEM *BUF_MEM_new(void) {
|
||||
|
||||
ret = OPENSSL_malloc(sizeof(BUF_MEM));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -93,21 +92,18 @@ int BUF_MEM_reserve(BUF_MEM *buf, size_t cap) {
|
||||
|
||||
size_t n = cap + 3;
|
||||
if (n < cap) {
|
||||
// overflow
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
n = n / 3;
|
||||
size_t alloc_size = n * 4;
|
||||
if (alloc_size / 4 != n) {
|
||||
// overflow
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_OVERFLOW);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *new_buf = OPENSSL_realloc(buf->data, alloc_size);
|
||||
if (new_buf == NULL) {
|
||||
OPENSSL_PUT_ERROR(BUF, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,11 +24,11 @@
|
||||
// kMaxDepth is a just a sanity limit. The code should be such that the length
|
||||
// of the input being processes always decreases. None the less, a very large
|
||||
// input could otherwise cause the stack to overflow.
|
||||
static const unsigned kMaxDepth = 2048;
|
||||
static const uint32_t kMaxDepth = 2048;
|
||||
|
||||
// is_string_type returns one if |tag| is a string type and zero otherwise. It
|
||||
// ignores the constructed bit.
|
||||
static int is_string_type(unsigned tag) {
|
||||
static int is_string_type(CBS_ASN1_TAG tag) {
|
||||
// While BER supports constructed BIT STRINGS, OpenSSL misparses them. To
|
||||
// avoid acting on an ambiguous input, we do not support constructed BIT
|
||||
// STRINGS. See https://github.com/openssl/openssl/issues/12810.
|
||||
@@ -55,7 +55,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, int *ber_found, unsigned depth) {
|
||||
static int cbs_find_ber(const CBS *orig_in, int *ber_found, uint32_t depth) {
|
||||
CBS in;
|
||||
|
||||
if (depth > kMaxDepth) {
|
||||
@@ -67,7 +67,7 @@ static int cbs_find_ber(const CBS *orig_in, int *ber_found, unsigned depth) {
|
||||
|
||||
while (CBS_len(&in) > 0) {
|
||||
CBS contents;
|
||||
unsigned tag;
|
||||
CBS_ASN1_TAG tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
if (!CBS_get_any_ber_asn1_element(&in, &contents, &tag, &header_len,
|
||||
@@ -110,8 +110,8 @@ static int cbs_get_eoc(CBS *cbs) {
|
||||
// constructed string. If |looking_for_eoc| is set then any EOC elements found
|
||||
// will cause the function to return after consuming it. It returns one on
|
||||
// success and zero on error.
|
||||
static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
char looking_for_eoc, unsigned depth) {
|
||||
static int cbs_convert_ber(CBS *in, CBB *out, CBS_ASN1_TAG string_tag,
|
||||
int looking_for_eoc, uint32_t depth) {
|
||||
assert(!(string_tag & CBS_ASN1_CONSTRUCTED));
|
||||
|
||||
if (depth > kMaxDepth) {
|
||||
@@ -124,7 +124,7 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
}
|
||||
|
||||
CBS contents;
|
||||
unsigned tag, child_string_tag = string_tag;
|
||||
CBS_ASN1_TAG tag, child_string_tag = string_tag;
|
||||
size_t header_len;
|
||||
int indefinite;
|
||||
CBB *out_contents, out_contents_storage;
|
||||
@@ -142,7 +142,7 @@ static int cbs_convert_ber(CBS *in, CBB *out, unsigned string_tag,
|
||||
}
|
||||
out_contents = out;
|
||||
} else {
|
||||
unsigned out_tag = tag;
|
||||
CBS_ASN1_TAG out_tag = tag;
|
||||
if ((tag & CBS_ASN1_CONSTRUCTED) && is_string_type(tag)) {
|
||||
// If a constructed string, clear the constructed bit and inform
|
||||
// children to concatenate bodies.
|
||||
@@ -221,7 +221,8 @@ int CBS_asn1_ber_to_der(CBS *in, CBS *out, uint8_t **out_storage) {
|
||||
}
|
||||
|
||||
int CBS_get_asn1_implicit_string(CBS *in, CBS *out, uint8_t **out_storage,
|
||||
unsigned outer_tag, unsigned inner_tag) {
|
||||
CBS_ASN1_TAG outer_tag,
|
||||
CBS_ASN1_TAG inner_tag) {
|
||||
assert(!(outer_tag & CBS_ASN1_CONSTRUCTED));
|
||||
assert(!(inner_tag & CBS_ASN1_CONSTRUCTED));
|
||||
assert(is_string_type(inner_tag));
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
@@ -77,11 +78,13 @@ static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out,
|
||||
size_t newlen = base->len + len;
|
||||
if (newlen < base->len) {
|
||||
// Overflow
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (newlen > base->cap) {
|
||||
if (!base->can_resize) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -121,6 +124,7 @@ static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
|
||||
|
||||
int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
|
||||
if (cbb->is_child) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -191,6 +195,7 @@ int CBB_flush(CBB *cbb) {
|
||||
assert (child->pending_len_len == 1);
|
||||
|
||||
if (len > 0xfffffffe) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
// Too large.
|
||||
goto err;
|
||||
} else if (len > 0xffffff) {
|
||||
@@ -229,6 +234,7 @@ int CBB_flush(CBB *cbb) {
|
||||
len >>= 8;
|
||||
}
|
||||
if (len != 0) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -333,14 +339,14 @@ static int add_base128_integer(CBB *cbb, uint64_t v) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBB_add_asn1(CBB *cbb, CBB *out_contents, unsigned tag) {
|
||||
int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) {
|
||||
if (!CBB_flush(cbb)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Split the tag into leading bits and tag number.
|
||||
uint8_t tag_bits = (tag >> CBS_ASN1_TAG_SHIFT) & 0xe0;
|
||||
unsigned tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
|
||||
CBS_ASN1_TAG tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK;
|
||||
if (tag_number >= 0x1f) {
|
||||
// Set all the bits in the tag number to signal high tag number form.
|
||||
if (!CBB_add_u8(cbb, tag_bits | 0x1f) ||
|
||||
@@ -470,7 +476,7 @@ int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
|
||||
return CBB_add_asn1_uint64_with_tag(cbb, value, CBS_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, unsigned tag) {
|
||||
int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, CBS_ASN1_TAG tag) {
|
||||
CBB child;
|
||||
if (!CBB_add_asn1(cbb, &child, tag)) {
|
||||
return 0;
|
||||
@@ -508,7 +514,7 @@ int CBB_add_asn1_int64(CBB *cbb, int64_t value) {
|
||||
return CBB_add_asn1_int64_with_tag(cbb, value, CBS_ASN1_INTEGER);
|
||||
}
|
||||
|
||||
int CBB_add_asn1_int64_with_tag(CBB *cbb, int64_t value, unsigned tag) {
|
||||
int CBB_add_asn1_int64_with_tag(CBB *cbb, int64_t value, CBS_ASN1_TAG tag) {
|
||||
if (value >= 0) {
|
||||
return CBB_add_asn1_uint64_with_tag(cbb, (uint64_t)value, tag);
|
||||
}
|
||||
@@ -560,30 +566,15 @@ int CBB_add_asn1_bool(CBB *cbb, int value) {
|
||||
// component and the dot, so |cbs| may be passed into the function again for the
|
||||
// next value.
|
||||
static int parse_dotted_decimal(CBS *cbs, uint64_t *out) {
|
||||
*out = 0;
|
||||
int seen_digit = 0;
|
||||
for (;;) {
|
||||
// Valid terminators for a component are the end of the string or a
|
||||
// non-terminal dot. If the string ends with a dot, this is not a valid OID
|
||||
// string.
|
||||
uint8_t u;
|
||||
if (!CBS_get_u8(cbs, &u) ||
|
||||
(u == '.' && CBS_len(cbs) > 0)) {
|
||||
break;
|
||||
}
|
||||
if (u < '0' || u > '9' ||
|
||||
// Forbid stray leading zeros.
|
||||
(seen_digit && *out == 0) ||
|
||||
// Check for overflow.
|
||||
*out > UINT64_MAX / 10 ||
|
||||
*out * 10 > UINT64_MAX - (u - '0')) {
|
||||
return 0;
|
||||
}
|
||||
*out = *out * 10 + (u - '0');
|
||||
seen_digit = 1;
|
||||
if (!CBS_get_u64_decimal(cbs, out)) {
|
||||
return 0;
|
||||
}
|
||||
// The empty string is not a legal OID component.
|
||||
return seen_digit;
|
||||
|
||||
// The integer must have either ended at the end of the string, or a
|
||||
// non-terminal dot, which should be consumed. If the string ends with a dot,
|
||||
// this is not a valid OID string.
|
||||
uint8_t dot;
|
||||
return !CBS_get_u8(cbs, &dot) || (dot == '.' && CBS_len(cbs) > 0);
|
||||
}
|
||||
|
||||
int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) {
|
||||
@@ -649,6 +640,7 @@ int CBB_flush_asn1_set_of(CBB *cbb) {
|
||||
CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb));
|
||||
while (CBS_len(&cbs) != 0) {
|
||||
if (!CBS_get_any_asn1_element(&cbs, NULL, NULL, NULL)) {
|
||||
OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
num_children++;
|
||||
|
||||
@@ -136,7 +136,7 @@ int CBS_get_u24(CBS *cbs, uint32_t *out) {
|
||||
if (!cbs_get_u(cbs, &v, 3)) {
|
||||
return 0;
|
||||
}
|
||||
*out = v;
|
||||
*out = (uint32_t)v;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ int CBS_get_u32(CBS *cbs, uint32_t *out) {
|
||||
if (!cbs_get_u(cbs, &v, 4)) {
|
||||
return 0;
|
||||
}
|
||||
*out = v;
|
||||
*out = (uint32_t)v;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -227,6 +227,30 @@ int CBS_get_until_first(CBS *cbs, CBS *out, uint8_t c) {
|
||||
return CBS_get_bytes(cbs, out, split - CBS_data(cbs));
|
||||
}
|
||||
|
||||
int CBS_get_u64_decimal(CBS *cbs, uint64_t *out) {
|
||||
uint64_t v = 0;
|
||||
int seen_digit = 0;
|
||||
while (CBS_len(cbs) != 0) {
|
||||
uint8_t c = CBS_data(cbs)[0];
|
||||
if (!OPENSSL_isdigit(c)) {
|
||||
break;
|
||||
}
|
||||
CBS_skip(cbs, 1);
|
||||
if (// Forbid stray leading zeros.
|
||||
(v == 0 && seen_digit) ||
|
||||
// Check for overflow.
|
||||
v > UINT64_MAX / 10 || //
|
||||
v * 10 > UINT64_MAX - (c - '0')) {
|
||||
return 0;
|
||||
}
|
||||
v = v * 10 + (c - '0');
|
||||
seen_digit = 1;
|
||||
}
|
||||
|
||||
*out = v;
|
||||
return seen_digit;
|
||||
}
|
||||
|
||||
// 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,7 +278,7 @@ static int parse_base128_integer(CBS *cbs, uint64_t *out) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
static int parse_asn1_tag(CBS *cbs, CBS_ASN1_TAG *out) {
|
||||
uint8_t tag_byte;
|
||||
if (!CBS_get_u8(cbs, &tag_byte)) {
|
||||
return 0;
|
||||
@@ -266,8 +290,8 @@ 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.
|
||||
unsigned tag = ((unsigned)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
|
||||
unsigned tag_number = tag_byte & 0x1f;
|
||||
CBS_ASN1_TAG tag = ((CBS_ASN1_TAG)tag_byte & 0xe0) << CBS_ASN1_TAG_SHIFT;
|
||||
CBS_ASN1_TAG tag_number = tag_byte & 0x1f;
|
||||
if (tag_number == 0x1f) {
|
||||
uint64_t v;
|
||||
if (!parse_base128_integer(cbs, &v) ||
|
||||
@@ -277,7 +301,7 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
v < 0x1f) {
|
||||
return 0;
|
||||
}
|
||||
tag_number = (unsigned)v;
|
||||
tag_number = (CBS_ASN1_TAG)v;
|
||||
}
|
||||
|
||||
tag |= tag_number;
|
||||
@@ -293,7 +317,7 @@ static int parse_asn1_tag(CBS *cbs, unsigned *out) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
|
||||
size_t *out_header_len, int *out_ber_found,
|
||||
int *out_indefinite, int ber_ok) {
|
||||
CBS header = *cbs;
|
||||
@@ -310,7 +334,7 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
assert(out_indefinite == NULL);
|
||||
}
|
||||
|
||||
unsigned tag;
|
||||
CBS_ASN1_TAG tag;
|
||||
if (!parse_asn1_tag(&header, &tag)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -394,7 +418,7 @@ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
return CBS_get_bytes(cbs, out, len);
|
||||
}
|
||||
|
||||
int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
|
||||
int CBS_get_any_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag) {
|
||||
size_t header_len;
|
||||
if (!CBS_get_any_asn1_element(cbs, out, out_tag, &header_len)) {
|
||||
return 0;
|
||||
@@ -408,13 +432,13 @@ int CBS_get_any_asn1(CBS *cbs, CBS *out, unsigned *out_tag) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
int CBS_get_any_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
|
||||
size_t *out_header_len) {
|
||||
return cbs_get_any_asn1_element(cbs, out, out_tag, out_header_len, NULL, NULL,
|
||||
/*ber_ok=*/0);
|
||||
}
|
||||
|
||||
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG *out_tag,
|
||||
size_t *out_header_len, int *out_ber_found,
|
||||
int *out_indefinite) {
|
||||
int ber_found_temp;
|
||||
@@ -424,10 +448,10 @@ int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
|
||||
/*ber_ok=*/1);
|
||||
}
|
||||
|
||||
static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
|
||||
static int cbs_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value,
|
||||
int skip_header) {
|
||||
size_t header_len;
|
||||
unsigned tag;
|
||||
CBS_ASN1_TAG tag;
|
||||
CBS throwaway;
|
||||
|
||||
if (out == NULL) {
|
||||
@@ -447,21 +471,17 @@ static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
|
||||
int CBS_get_asn1(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
|
||||
return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
|
||||
}
|
||||
|
||||
int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
|
||||
int CBS_get_asn1_element(CBS *cbs, CBS *out, CBS_ASN1_TAG tag_value) {
|
||||
return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
|
||||
}
|
||||
|
||||
int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
|
||||
if (CBS_len(cbs) < 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CBS_peek_asn1_tag(const CBS *cbs, CBS_ASN1_TAG tag_value) {
|
||||
CBS copy = *cbs;
|
||||
unsigned actual_tag;
|
||||
CBS_ASN1_TAG actual_tag;
|
||||
return parse_asn1_tag(©, &actual_tag) && tag_value == actual_tag;
|
||||
}
|
||||
|
||||
@@ -524,7 +544,7 @@ int CBS_get_asn1_bool(CBS *cbs, int *out) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
|
||||
int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, CBS_ASN1_TAG tag) {
|
||||
int present = 0;
|
||||
|
||||
if (CBS_peek_asn1_tag(cbs, tag)) {
|
||||
@@ -542,7 +562,7 @@ int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
|
||||
}
|
||||
|
||||
int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
|
||||
unsigned tag) {
|
||||
CBS_ASN1_TAG tag) {
|
||||
CBS child;
|
||||
int present;
|
||||
if (!CBS_get_optional_asn1(cbs, &child, &present, tag)) {
|
||||
@@ -563,7 +583,7 @@ int CBS_get_optional_asn1_octet_string(CBS *cbs, CBS *out, int *out_present,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
|
||||
int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, CBS_ASN1_TAG tag,
|
||||
uint64_t default_value) {
|
||||
CBS child;
|
||||
int present;
|
||||
@@ -581,7 +601,7 @@ int CBS_get_optional_asn1_uint64(CBS *cbs, uint64_t *out, unsigned tag,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CBS_get_optional_asn1_bool(CBS *cbs, int *out, unsigned tag,
|
||||
int CBS_get_optional_asn1_bool(CBS *cbs, int *out, CBS_ASN1_TAG tag,
|
||||
int default_value) {
|
||||
CBS child, child2;
|
||||
int present;
|
||||
@@ -674,10 +694,33 @@ int CBS_is_unsigned_asn1_integer(const CBS *cbs) {
|
||||
|
||||
static int add_decimal(CBB *out, uint64_t v) {
|
||||
char buf[DECIMAL_SIZE(uint64_t) + 1];
|
||||
BIO_snprintf(buf, sizeof(buf), "%" PRIu64, v);
|
||||
snprintf(buf, sizeof(buf), "%" PRIu64, v);
|
||||
return CBB_add_bytes(out, (const uint8_t *)buf, strlen(buf));
|
||||
}
|
||||
|
||||
int CBS_is_valid_asn1_oid(const CBS *cbs) {
|
||||
if (CBS_len(cbs) == 0) {
|
||||
return 0; // OID encodings cannot be empty.
|
||||
}
|
||||
|
||||
CBS copy = *cbs;
|
||||
uint8_t v, prev = 0;
|
||||
while (CBS_get_u8(©, &v)) {
|
||||
// OID encodings are a sequence of minimally-encoded base-128 integers (see
|
||||
// |parse_base128_integer|). If |prev|'s MSB was clear, it was the last byte
|
||||
// of an integer (or |v| is the first byte). |v| is then the first byte of
|
||||
// the next integer. If first byte of an integer is 0x80, it is not
|
||||
// minimally-encoded.
|
||||
if ((prev & 0x80) == 0 && v == 0x80) {
|
||||
return 0;
|
||||
}
|
||||
prev = v;
|
||||
}
|
||||
|
||||
// The last byte should must end an integer encoding.
|
||||
return (prev & 0x80) == 0;
|
||||
}
|
||||
|
||||
char *CBS_asn1_oid_to_text(const CBS *cbs) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 32)) {
|
||||
@@ -729,13 +772,13 @@ static int cbs_get_two_digits(CBS *cbs, int *out) {
|
||||
if (!CBS_get_u8(cbs, &first_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!isdigit(first_digit)) {
|
||||
if (!OPENSSL_isdigit(first_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!CBS_get_u8(cbs, &second_digit)) {
|
||||
return 0;
|
||||
}
|
||||
if (!isdigit(second_digit)) {
|
||||
if (!OPENSSL_isdigit(second_digit)) {
|
||||
return 0;
|
||||
}
|
||||
*out = (first_digit - '0') * 10 + (second_digit - '0');
|
||||
|
||||
@@ -54,8 +54,8 @@ OPENSSL_EXPORT int CBS_asn1_ber_to_der(CBS *in, CBS *out,
|
||||
// It returns one on success and zero otherwise.
|
||||
OPENSSL_EXPORT int CBS_get_asn1_implicit_string(CBS *in, CBS *out,
|
||||
uint8_t **out_storage,
|
||||
unsigned outer_tag,
|
||||
unsigned inner_tag);
|
||||
CBS_ASN1_TAG outer_tag,
|
||||
CBS_ASN1_TAG inner_tag);
|
||||
|
||||
// CBB_finish_i2d calls |CBB_finish| on |cbb| which must have been initialized
|
||||
// with |CBB_init|. If |outp| is not NULL then the result is written to |*outp|
|
||||
|
||||
+3
-11
@@ -3,17 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) && defined(__APPLE__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
@ Silence ARMv8 deprecated IT instruction warnings. This file is used by both
|
||||
@@ -1497,7 +1489,7 @@ OPENSSL_armcap_P:
|
||||
.indirect_symbol _OPENSSL_armcap_P
|
||||
.long 0
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_ARM) && defined(__APPLE__)
|
||||
#endif // defined(__arm__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+3
-14
@@ -3,18 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(__arm__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) && defined(__ELF__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
@ Silence ARMv8 deprecated IT instruction warnings. This file is used by both
|
||||
@@ -1490,9 +1481,7 @@ ChaCha20_neon:
|
||||
.size ChaCha20_neon,.-ChaCha20_neon
|
||||
.comm OPENSSL_armcap_P,4,4
|
||||
#endif
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_ARM) && defined(__ELF__)
|
||||
#endif // defined(__arm__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+4
-12
@@ -3,17 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__APPLE__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
@@ -38,7 +30,7 @@ Lone:
|
||||
_ChaCha20_ctr32:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
cbz x2,Labort
|
||||
#if __has_feature(hwaddress_sanitizer) && __clang_major__ >= 10
|
||||
#if defined(OPENSSL_HWASAN) && __clang_major__ >= 10
|
||||
adrp x5,:pg_hi21_nc:_OPENSSL_armcap_P
|
||||
#else
|
||||
adrp x5,_OPENSSL_armcap_P@PAGE
|
||||
@@ -1991,7 +1983,7 @@ Ldone_512_neon:
|
||||
AARCH64_VALIDATE_LINK_REGISTER
|
||||
ret
|
||||
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_AARCH64) && defined(__APPLE__)
|
||||
#endif // defined(__aarch64__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+4
-15
@@ -3,18 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(__aarch64__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__ELF__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
@@ -39,7 +30,7 @@
|
||||
ChaCha20_ctr32:
|
||||
AARCH64_VALID_CALL_TARGET
|
||||
cbz x2,.Labort
|
||||
#if __has_feature(hwaddress_sanitizer) && __clang_major__ >= 10
|
||||
#if defined(OPENSSL_HWASAN) && __clang_major__ >= 10
|
||||
adrp x5,:pg_hi21_nc:OPENSSL_armcap_P
|
||||
#else
|
||||
adrp x5,OPENSSL_armcap_P
|
||||
@@ -1992,9 +1983,7 @@ ChaCha20_512_neon:
|
||||
AARCH64_VALIDATE_LINK_REGISTER
|
||||
ret
|
||||
.size ChaCha20_512_neon,.-ChaCha20_512_neon
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_AARCH64) && defined(__ELF__)
|
||||
#endif // defined(__aarch64__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+4
-6
@@ -3,10 +3,9 @@
|
||||
// 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
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__ELF__)
|
||||
.text
|
||||
.globl ChaCha20_ctr32
|
||||
.hidden ChaCha20_ctr32
|
||||
@@ -973,8 +972,7 @@ ChaCha20_ssse3:
|
||||
.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
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
#endif // !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__ELF__)
|
||||
#endif // defined(__i386__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
@@ -0,0 +1,987 @@
|
||||
#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.
|
||||
|
||||
%ifdef BORINGSSL_PREFIX
|
||||
%include "boringssl_prefix_symbols_nasm.inc"
|
||||
%endif
|
||||
%ifidn __OUTPUT_FORMAT__, win32
|
||||
%ifidn __OUTPUT_FORMAT__,obj
|
||||
section code use32 class=code align=64
|
||||
%elifidn __OUTPUT_FORMAT__,win32
|
||||
$@feat.00 equ 1
|
||||
section .text code align=64
|
||||
%else
|
||||
section .text code
|
||||
%endif
|
||||
global _ChaCha20_ctr32
|
||||
align 16
|
||||
_ChaCha20_ctr32:
|
||||
L$_ChaCha20_ctr32_begin:
|
||||
push ebp
|
||||
push ebx
|
||||
push esi
|
||||
push edi
|
||||
xor eax,eax
|
||||
cmp eax,DWORD [28+esp]
|
||||
je NEAR L$000no_data
|
||||
call L$pic_point
|
||||
L$pic_point:
|
||||
pop eax
|
||||
lea ebp,[_OPENSSL_ia32cap_P]
|
||||
test DWORD [ebp],16777216
|
||||
jz NEAR L$001x86
|
||||
test DWORD [4+ebp],512
|
||||
jz NEAR L$001x86
|
||||
jmp NEAR L$ssse3_shortcut
|
||||
L$001x86:
|
||||
mov esi,DWORD [32+esp]
|
||||
mov edi,DWORD [36+esp]
|
||||
sub esp,132
|
||||
mov eax,DWORD [esi]
|
||||
mov ebx,DWORD [4+esi]
|
||||
mov ecx,DWORD [8+esi]
|
||||
mov edx,DWORD [12+esi]
|
||||
mov DWORD [80+esp],eax
|
||||
mov DWORD [84+esp],ebx
|
||||
mov DWORD [88+esp],ecx
|
||||
mov DWORD [92+esp],edx
|
||||
mov eax,DWORD [16+esi]
|
||||
mov ebx,DWORD [20+esi]
|
||||
mov ecx,DWORD [24+esi]
|
||||
mov edx,DWORD [28+esi]
|
||||
mov DWORD [96+esp],eax
|
||||
mov DWORD [100+esp],ebx
|
||||
mov DWORD [104+esp],ecx
|
||||
mov DWORD [108+esp],edx
|
||||
mov eax,DWORD [edi]
|
||||
mov ebx,DWORD [4+edi]
|
||||
mov ecx,DWORD [8+edi]
|
||||
mov edx,DWORD [12+edi]
|
||||
sub eax,1
|
||||
mov DWORD [112+esp],eax
|
||||
mov DWORD [116+esp],ebx
|
||||
mov DWORD [120+esp],ecx
|
||||
mov DWORD [124+esp],edx
|
||||
jmp NEAR L$002entry
|
||||
align 16
|
||||
L$003outer_loop:
|
||||
mov DWORD [156+esp],ebx
|
||||
mov DWORD [152+esp],eax
|
||||
mov DWORD [160+esp],ecx
|
||||
L$002entry:
|
||||
mov eax,1634760805
|
||||
mov DWORD [4+esp],857760878
|
||||
mov DWORD [8+esp],2036477234
|
||||
mov DWORD [12+esp],1797285236
|
||||
mov ebx,DWORD [84+esp]
|
||||
mov ebp,DWORD [88+esp]
|
||||
mov ecx,DWORD [104+esp]
|
||||
mov esi,DWORD [108+esp]
|
||||
mov edx,DWORD [116+esp]
|
||||
mov edi,DWORD [120+esp]
|
||||
mov DWORD [20+esp],ebx
|
||||
mov DWORD [24+esp],ebp
|
||||
mov DWORD [40+esp],ecx
|
||||
mov DWORD [44+esp],esi
|
||||
mov DWORD [52+esp],edx
|
||||
mov DWORD [56+esp],edi
|
||||
mov ebx,DWORD [92+esp]
|
||||
mov edi,DWORD [124+esp]
|
||||
mov edx,DWORD [112+esp]
|
||||
mov ebp,DWORD [80+esp]
|
||||
mov ecx,DWORD [96+esp]
|
||||
mov esi,DWORD [100+esp]
|
||||
add edx,1
|
||||
mov DWORD [28+esp],ebx
|
||||
mov DWORD [60+esp],edi
|
||||
mov DWORD [112+esp],edx
|
||||
mov ebx,10
|
||||
jmp NEAR L$004loop
|
||||
align 16
|
||||
L$004loop:
|
||||
add eax,ebp
|
||||
mov DWORD [128+esp],ebx
|
||||
mov ebx,ebp
|
||||
xor edx,eax
|
||||
rol edx,16
|
||||
add ecx,edx
|
||||
xor ebx,ecx
|
||||
mov edi,DWORD [52+esp]
|
||||
rol ebx,12
|
||||
mov ebp,DWORD [20+esp]
|
||||
add eax,ebx
|
||||
xor edx,eax
|
||||
mov DWORD [esp],eax
|
||||
rol edx,8
|
||||
mov eax,DWORD [4+esp]
|
||||
add ecx,edx
|
||||
mov DWORD [48+esp],edx
|
||||
xor ebx,ecx
|
||||
add eax,ebp
|
||||
rol ebx,7
|
||||
xor edi,eax
|
||||
mov DWORD [32+esp],ecx
|
||||
rol edi,16
|
||||
mov DWORD [16+esp],ebx
|
||||
add esi,edi
|
||||
mov ecx,DWORD [40+esp]
|
||||
xor ebp,esi
|
||||
mov edx,DWORD [56+esp]
|
||||
rol ebp,12
|
||||
mov ebx,DWORD [24+esp]
|
||||
add eax,ebp
|
||||
xor edi,eax
|
||||
mov DWORD [4+esp],eax
|
||||
rol edi,8
|
||||
mov eax,DWORD [8+esp]
|
||||
add esi,edi
|
||||
mov DWORD [52+esp],edi
|
||||
xor ebp,esi
|
||||
add eax,ebx
|
||||
rol ebp,7
|
||||
xor edx,eax
|
||||
mov DWORD [36+esp],esi
|
||||
rol edx,16
|
||||
mov DWORD [20+esp],ebp
|
||||
add ecx,edx
|
||||
mov esi,DWORD [44+esp]
|
||||
xor ebx,ecx
|
||||
mov edi,DWORD [60+esp]
|
||||
rol ebx,12
|
||||
mov ebp,DWORD [28+esp]
|
||||
add eax,ebx
|
||||
xor edx,eax
|
||||
mov DWORD [8+esp],eax
|
||||
rol edx,8
|
||||
mov eax,DWORD [12+esp]
|
||||
add ecx,edx
|
||||
mov DWORD [56+esp],edx
|
||||
xor ebx,ecx
|
||||
add eax,ebp
|
||||
rol ebx,7
|
||||
xor edi,eax
|
||||
rol edi,16
|
||||
mov DWORD [24+esp],ebx
|
||||
add esi,edi
|
||||
xor ebp,esi
|
||||
rol ebp,12
|
||||
mov ebx,DWORD [20+esp]
|
||||
add eax,ebp
|
||||
xor edi,eax
|
||||
mov DWORD [12+esp],eax
|
||||
rol edi,8
|
||||
mov eax,DWORD [esp]
|
||||
add esi,edi
|
||||
mov edx,edi
|
||||
xor ebp,esi
|
||||
add eax,ebx
|
||||
rol ebp,7
|
||||
xor edx,eax
|
||||
rol edx,16
|
||||
mov DWORD [28+esp],ebp
|
||||
add ecx,edx
|
||||
xor ebx,ecx
|
||||
mov edi,DWORD [48+esp]
|
||||
rol ebx,12
|
||||
mov ebp,DWORD [24+esp]
|
||||
add eax,ebx
|
||||
xor edx,eax
|
||||
mov DWORD [esp],eax
|
||||
rol edx,8
|
||||
mov eax,DWORD [4+esp]
|
||||
add ecx,edx
|
||||
mov DWORD [60+esp],edx
|
||||
xor ebx,ecx
|
||||
add eax,ebp
|
||||
rol ebx,7
|
||||
xor edi,eax
|
||||
mov DWORD [40+esp],ecx
|
||||
rol edi,16
|
||||
mov DWORD [20+esp],ebx
|
||||
add esi,edi
|
||||
mov ecx,DWORD [32+esp]
|
||||
xor ebp,esi
|
||||
mov edx,DWORD [52+esp]
|
||||
rol ebp,12
|
||||
mov ebx,DWORD [28+esp]
|
||||
add eax,ebp
|
||||
xor edi,eax
|
||||
mov DWORD [4+esp],eax
|
||||
rol edi,8
|
||||
mov eax,DWORD [8+esp]
|
||||
add esi,edi
|
||||
mov DWORD [48+esp],edi
|
||||
xor ebp,esi
|
||||
add eax,ebx
|
||||
rol ebp,7
|
||||
xor edx,eax
|
||||
mov DWORD [44+esp],esi
|
||||
rol edx,16
|
||||
mov DWORD [24+esp],ebp
|
||||
add ecx,edx
|
||||
mov esi,DWORD [36+esp]
|
||||
xor ebx,ecx
|
||||
mov edi,DWORD [56+esp]
|
||||
rol ebx,12
|
||||
mov ebp,DWORD [16+esp]
|
||||
add eax,ebx
|
||||
xor edx,eax
|
||||
mov DWORD [8+esp],eax
|
||||
rol edx,8
|
||||
mov eax,DWORD [12+esp]
|
||||
add ecx,edx
|
||||
mov DWORD [52+esp],edx
|
||||
xor ebx,ecx
|
||||
add eax,ebp
|
||||
rol ebx,7
|
||||
xor edi,eax
|
||||
rol edi,16
|
||||
mov DWORD [28+esp],ebx
|
||||
add esi,edi
|
||||
xor ebp,esi
|
||||
mov edx,DWORD [48+esp]
|
||||
rol ebp,12
|
||||
mov ebx,DWORD [128+esp]
|
||||
add eax,ebp
|
||||
xor edi,eax
|
||||
mov DWORD [12+esp],eax
|
||||
rol edi,8
|
||||
mov eax,DWORD [esp]
|
||||
add esi,edi
|
||||
mov DWORD [56+esp],edi
|
||||
xor ebp,esi
|
||||
rol ebp,7
|
||||
dec ebx
|
||||
jnz NEAR L$004loop
|
||||
mov ebx,DWORD [160+esp]
|
||||
add eax,1634760805
|
||||
add ebp,DWORD [80+esp]
|
||||
add ecx,DWORD [96+esp]
|
||||
add esi,DWORD [100+esp]
|
||||
cmp ebx,64
|
||||
jb NEAR L$005tail
|
||||
mov ebx,DWORD [156+esp]
|
||||
add edx,DWORD [112+esp]
|
||||
add edi,DWORD [120+esp]
|
||||
xor eax,DWORD [ebx]
|
||||
xor ebp,DWORD [16+ebx]
|
||||
mov DWORD [esp],eax
|
||||
mov eax,DWORD [152+esp]
|
||||
xor ecx,DWORD [32+ebx]
|
||||
xor esi,DWORD [36+ebx]
|
||||
xor edx,DWORD [48+ebx]
|
||||
xor edi,DWORD [56+ebx]
|
||||
mov DWORD [16+eax],ebp
|
||||
mov DWORD [32+eax],ecx
|
||||
mov DWORD [36+eax],esi
|
||||
mov DWORD [48+eax],edx
|
||||
mov DWORD [56+eax],edi
|
||||
mov ebp,DWORD [4+esp]
|
||||
mov ecx,DWORD [8+esp]
|
||||
mov esi,DWORD [12+esp]
|
||||
mov edx,DWORD [20+esp]
|
||||
mov edi,DWORD [24+esp]
|
||||
add ebp,857760878
|
||||
add ecx,2036477234
|
||||
add esi,1797285236
|
||||
add edx,DWORD [84+esp]
|
||||
add edi,DWORD [88+esp]
|
||||
xor ebp,DWORD [4+ebx]
|
||||
xor ecx,DWORD [8+ebx]
|
||||
xor esi,DWORD [12+ebx]
|
||||
xor edx,DWORD [20+ebx]
|
||||
xor edi,DWORD [24+ebx]
|
||||
mov DWORD [4+eax],ebp
|
||||
mov DWORD [8+eax],ecx
|
||||
mov DWORD [12+eax],esi
|
||||
mov DWORD [20+eax],edx
|
||||
mov DWORD [24+eax],edi
|
||||
mov ebp,DWORD [28+esp]
|
||||
mov ecx,DWORD [40+esp]
|
||||
mov esi,DWORD [44+esp]
|
||||
mov edx,DWORD [52+esp]
|
||||
mov edi,DWORD [60+esp]
|
||||
add ebp,DWORD [92+esp]
|
||||
add ecx,DWORD [104+esp]
|
||||
add esi,DWORD [108+esp]
|
||||
add edx,DWORD [116+esp]
|
||||
add edi,DWORD [124+esp]
|
||||
xor ebp,DWORD [28+ebx]
|
||||
xor ecx,DWORD [40+ebx]
|
||||
xor esi,DWORD [44+ebx]
|
||||
xor edx,DWORD [52+ebx]
|
||||
xor edi,DWORD [60+ebx]
|
||||
lea ebx,[64+ebx]
|
||||
mov DWORD [28+eax],ebp
|
||||
mov ebp,DWORD [esp]
|
||||
mov DWORD [40+eax],ecx
|
||||
mov ecx,DWORD [160+esp]
|
||||
mov DWORD [44+eax],esi
|
||||
mov DWORD [52+eax],edx
|
||||
mov DWORD [60+eax],edi
|
||||
mov DWORD [eax],ebp
|
||||
lea eax,[64+eax]
|
||||
sub ecx,64
|
||||
jnz NEAR L$003outer_loop
|
||||
jmp NEAR L$006done
|
||||
L$005tail:
|
||||
add edx,DWORD [112+esp]
|
||||
add edi,DWORD [120+esp]
|
||||
mov DWORD [esp],eax
|
||||
mov DWORD [16+esp],ebp
|
||||
mov DWORD [32+esp],ecx
|
||||
mov DWORD [36+esp],esi
|
||||
mov DWORD [48+esp],edx
|
||||
mov DWORD [56+esp],edi
|
||||
mov ebp,DWORD [4+esp]
|
||||
mov ecx,DWORD [8+esp]
|
||||
mov esi,DWORD [12+esp]
|
||||
mov edx,DWORD [20+esp]
|
||||
mov edi,DWORD [24+esp]
|
||||
add ebp,857760878
|
||||
add ecx,2036477234
|
||||
add esi,1797285236
|
||||
add edx,DWORD [84+esp]
|
||||
add edi,DWORD [88+esp]
|
||||
mov DWORD [4+esp],ebp
|
||||
mov DWORD [8+esp],ecx
|
||||
mov DWORD [12+esp],esi
|
||||
mov DWORD [20+esp],edx
|
||||
mov DWORD [24+esp],edi
|
||||
mov ebp,DWORD [28+esp]
|
||||
mov ecx,DWORD [40+esp]
|
||||
mov esi,DWORD [44+esp]
|
||||
mov edx,DWORD [52+esp]
|
||||
mov edi,DWORD [60+esp]
|
||||
add ebp,DWORD [92+esp]
|
||||
add ecx,DWORD [104+esp]
|
||||
add esi,DWORD [108+esp]
|
||||
add edx,DWORD [116+esp]
|
||||
add edi,DWORD [124+esp]
|
||||
mov DWORD [28+esp],ebp
|
||||
mov ebp,DWORD [156+esp]
|
||||
mov DWORD [40+esp],ecx
|
||||
mov ecx,DWORD [152+esp]
|
||||
mov DWORD [44+esp],esi
|
||||
xor esi,esi
|
||||
mov DWORD [52+esp],edx
|
||||
mov DWORD [60+esp],edi
|
||||
xor eax,eax
|
||||
xor edx,edx
|
||||
L$007tail_loop:
|
||||
mov al,BYTE [ebp*1+esi]
|
||||
mov dl,BYTE [esi*1+esp]
|
||||
lea esi,[1+esi]
|
||||
xor al,dl
|
||||
mov BYTE [esi*1+ecx-1],al
|
||||
dec ebx
|
||||
jnz NEAR L$007tail_loop
|
||||
L$006done:
|
||||
add esp,132
|
||||
L$000no_data:
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebx
|
||||
pop ebp
|
||||
ret
|
||||
global _ChaCha20_ssse3
|
||||
align 16
|
||||
_ChaCha20_ssse3:
|
||||
L$_ChaCha20_ssse3_begin:
|
||||
push ebp
|
||||
push ebx
|
||||
push esi
|
||||
push edi
|
||||
L$ssse3_shortcut:
|
||||
mov edi,DWORD [20+esp]
|
||||
mov esi,DWORD [24+esp]
|
||||
mov ecx,DWORD [28+esp]
|
||||
mov edx,DWORD [32+esp]
|
||||
mov ebx,DWORD [36+esp]
|
||||
mov ebp,esp
|
||||
sub esp,524
|
||||
and esp,-64
|
||||
mov DWORD [512+esp],ebp
|
||||
lea eax,[(L$ssse3_data-L$pic_point)+eax]
|
||||
movdqu xmm3,[ebx]
|
||||
cmp ecx,256
|
||||
jb NEAR L$0081x
|
||||
mov DWORD [516+esp],edx
|
||||
mov DWORD [520+esp],ebx
|
||||
sub ecx,256
|
||||
lea ebp,[384+esp]
|
||||
movdqu xmm7,[edx]
|
||||
pshufd xmm0,xmm3,0
|
||||
pshufd xmm1,xmm3,85
|
||||
pshufd xmm2,xmm3,170
|
||||
pshufd xmm3,xmm3,255
|
||||
paddd xmm0,[48+eax]
|
||||
pshufd xmm4,xmm7,0
|
||||
pshufd xmm5,xmm7,85
|
||||
psubd xmm0,[64+eax]
|
||||
pshufd xmm6,xmm7,170
|
||||
pshufd xmm7,xmm7,255
|
||||
movdqa [64+ebp],xmm0
|
||||
movdqa [80+ebp],xmm1
|
||||
movdqa [96+ebp],xmm2
|
||||
movdqa [112+ebp],xmm3
|
||||
movdqu xmm3,[16+edx]
|
||||
movdqa [ebp-64],xmm4
|
||||
movdqa [ebp-48],xmm5
|
||||
movdqa [ebp-32],xmm6
|
||||
movdqa [ebp-16],xmm7
|
||||
movdqa xmm7,[32+eax]
|
||||
lea ebx,[128+esp]
|
||||
pshufd xmm0,xmm3,0
|
||||
pshufd xmm1,xmm3,85
|
||||
pshufd xmm2,xmm3,170
|
||||
pshufd xmm3,xmm3,255
|
||||
pshufd xmm4,xmm7,0
|
||||
pshufd xmm5,xmm7,85
|
||||
pshufd xmm6,xmm7,170
|
||||
pshufd xmm7,xmm7,255
|
||||
movdqa [ebp],xmm0
|
||||
movdqa [16+ebp],xmm1
|
||||
movdqa [32+ebp],xmm2
|
||||
movdqa [48+ebp],xmm3
|
||||
movdqa [ebp-128],xmm4
|
||||
movdqa [ebp-112],xmm5
|
||||
movdqa [ebp-96],xmm6
|
||||
movdqa [ebp-80],xmm7
|
||||
lea esi,[128+esi]
|
||||
lea edi,[128+edi]
|
||||
jmp NEAR L$009outer_loop
|
||||
align 16
|
||||
L$009outer_loop:
|
||||
movdqa xmm1,[ebp-112]
|
||||
movdqa xmm2,[ebp-96]
|
||||
movdqa xmm3,[ebp-80]
|
||||
movdqa xmm5,[ebp-48]
|
||||
movdqa xmm6,[ebp-32]
|
||||
movdqa xmm7,[ebp-16]
|
||||
movdqa [ebx-112],xmm1
|
||||
movdqa [ebx-96],xmm2
|
||||
movdqa [ebx-80],xmm3
|
||||
movdqa [ebx-48],xmm5
|
||||
movdqa [ebx-32],xmm6
|
||||
movdqa [ebx-16],xmm7
|
||||
movdqa xmm2,[32+ebp]
|
||||
movdqa xmm3,[48+ebp]
|
||||
movdqa xmm4,[64+ebp]
|
||||
movdqa xmm5,[80+ebp]
|
||||
movdqa xmm6,[96+ebp]
|
||||
movdqa xmm7,[112+ebp]
|
||||
paddd xmm4,[64+eax]
|
||||
movdqa [32+ebx],xmm2
|
||||
movdqa [48+ebx],xmm3
|
||||
movdqa [64+ebx],xmm4
|
||||
movdqa [80+ebx],xmm5
|
||||
movdqa [96+ebx],xmm6
|
||||
movdqa [112+ebx],xmm7
|
||||
movdqa [64+ebp],xmm4
|
||||
movdqa xmm0,[ebp-128]
|
||||
movdqa xmm6,xmm4
|
||||
movdqa xmm3,[ebp-64]
|
||||
movdqa xmm4,[ebp]
|
||||
movdqa xmm5,[16+ebp]
|
||||
mov edx,10
|
||||
nop
|
||||
align 16
|
||||
L$010loop:
|
||||
paddd xmm0,xmm3
|
||||
movdqa xmm2,xmm3
|
||||
pxor xmm6,xmm0
|
||||
pshufb xmm6,[eax]
|
||||
paddd xmm4,xmm6
|
||||
pxor xmm2,xmm4
|
||||
movdqa xmm3,[ebx-48]
|
||||
movdqa xmm1,xmm2
|
||||
pslld xmm2,12
|
||||
psrld xmm1,20
|
||||
por xmm2,xmm1
|
||||
movdqa xmm1,[ebx-112]
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm7,[80+ebx]
|
||||
pxor xmm6,xmm0
|
||||
movdqa [ebx-128],xmm0
|
||||
pshufb xmm6,[16+eax]
|
||||
paddd xmm4,xmm6
|
||||
movdqa [64+ebx],xmm6
|
||||
pxor xmm2,xmm4
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm0,xmm2
|
||||
pslld xmm2,7
|
||||
psrld xmm0,25
|
||||
pxor xmm7,xmm1
|
||||
por xmm2,xmm0
|
||||
movdqa [ebx],xmm4
|
||||
pshufb xmm7,[eax]
|
||||
movdqa [ebx-64],xmm2
|
||||
paddd xmm5,xmm7
|
||||
movdqa xmm4,[32+ebx]
|
||||
pxor xmm3,xmm5
|
||||
movdqa xmm2,[ebx-32]
|
||||
movdqa xmm0,xmm3
|
||||
pslld xmm3,12
|
||||
psrld xmm0,20
|
||||
por xmm3,xmm0
|
||||
movdqa xmm0,[ebx-96]
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm6,[96+ebx]
|
||||
pxor xmm7,xmm1
|
||||
movdqa [ebx-112],xmm1
|
||||
pshufb xmm7,[16+eax]
|
||||
paddd xmm5,xmm7
|
||||
movdqa [80+ebx],xmm7
|
||||
pxor xmm3,xmm5
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm1,xmm3
|
||||
pslld xmm3,7
|
||||
psrld xmm1,25
|
||||
pxor xmm6,xmm0
|
||||
por xmm3,xmm1
|
||||
movdqa [16+ebx],xmm5
|
||||
pshufb xmm6,[eax]
|
||||
movdqa [ebx-48],xmm3
|
||||
paddd xmm4,xmm6
|
||||
movdqa xmm5,[48+ebx]
|
||||
pxor xmm2,xmm4
|
||||
movdqa xmm3,[ebx-16]
|
||||
movdqa xmm1,xmm2
|
||||
pslld xmm2,12
|
||||
psrld xmm1,20
|
||||
por xmm2,xmm1
|
||||
movdqa xmm1,[ebx-80]
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm7,[112+ebx]
|
||||
pxor xmm6,xmm0
|
||||
movdqa [ebx-96],xmm0
|
||||
pshufb xmm6,[16+eax]
|
||||
paddd xmm4,xmm6
|
||||
movdqa [96+ebx],xmm6
|
||||
pxor xmm2,xmm4
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm0,xmm2
|
||||
pslld xmm2,7
|
||||
psrld xmm0,25
|
||||
pxor xmm7,xmm1
|
||||
por xmm2,xmm0
|
||||
pshufb xmm7,[eax]
|
||||
movdqa [ebx-32],xmm2
|
||||
paddd xmm5,xmm7
|
||||
pxor xmm3,xmm5
|
||||
movdqa xmm2,[ebx-48]
|
||||
movdqa xmm0,xmm3
|
||||
pslld xmm3,12
|
||||
psrld xmm0,20
|
||||
por xmm3,xmm0
|
||||
movdqa xmm0,[ebx-128]
|
||||
paddd xmm1,xmm3
|
||||
pxor xmm7,xmm1
|
||||
movdqa [ebx-80],xmm1
|
||||
pshufb xmm7,[16+eax]
|
||||
paddd xmm5,xmm7
|
||||
movdqa xmm6,xmm7
|
||||
pxor xmm3,xmm5
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm1,xmm3
|
||||
pslld xmm3,7
|
||||
psrld xmm1,25
|
||||
pxor xmm6,xmm0
|
||||
por xmm3,xmm1
|
||||
pshufb xmm6,[eax]
|
||||
movdqa [ebx-16],xmm3
|
||||
paddd xmm4,xmm6
|
||||
pxor xmm2,xmm4
|
||||
movdqa xmm3,[ebx-32]
|
||||
movdqa xmm1,xmm2
|
||||
pslld xmm2,12
|
||||
psrld xmm1,20
|
||||
por xmm2,xmm1
|
||||
movdqa xmm1,[ebx-112]
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm7,[64+ebx]
|
||||
pxor xmm6,xmm0
|
||||
movdqa [ebx-128],xmm0
|
||||
pshufb xmm6,[16+eax]
|
||||
paddd xmm4,xmm6
|
||||
movdqa [112+ebx],xmm6
|
||||
pxor xmm2,xmm4
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm0,xmm2
|
||||
pslld xmm2,7
|
||||
psrld xmm0,25
|
||||
pxor xmm7,xmm1
|
||||
por xmm2,xmm0
|
||||
movdqa [32+ebx],xmm4
|
||||
pshufb xmm7,[eax]
|
||||
movdqa [ebx-48],xmm2
|
||||
paddd xmm5,xmm7
|
||||
movdqa xmm4,[ebx]
|
||||
pxor xmm3,xmm5
|
||||
movdqa xmm2,[ebx-16]
|
||||
movdqa xmm0,xmm3
|
||||
pslld xmm3,12
|
||||
psrld xmm0,20
|
||||
por xmm3,xmm0
|
||||
movdqa xmm0,[ebx-96]
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm6,[80+ebx]
|
||||
pxor xmm7,xmm1
|
||||
movdqa [ebx-112],xmm1
|
||||
pshufb xmm7,[16+eax]
|
||||
paddd xmm5,xmm7
|
||||
movdqa [64+ebx],xmm7
|
||||
pxor xmm3,xmm5
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm1,xmm3
|
||||
pslld xmm3,7
|
||||
psrld xmm1,25
|
||||
pxor xmm6,xmm0
|
||||
por xmm3,xmm1
|
||||
movdqa [48+ebx],xmm5
|
||||
pshufb xmm6,[eax]
|
||||
movdqa [ebx-32],xmm3
|
||||
paddd xmm4,xmm6
|
||||
movdqa xmm5,[16+ebx]
|
||||
pxor xmm2,xmm4
|
||||
movdqa xmm3,[ebx-64]
|
||||
movdqa xmm1,xmm2
|
||||
pslld xmm2,12
|
||||
psrld xmm1,20
|
||||
por xmm2,xmm1
|
||||
movdqa xmm1,[ebx-80]
|
||||
paddd xmm0,xmm2
|
||||
movdqa xmm7,[96+ebx]
|
||||
pxor xmm6,xmm0
|
||||
movdqa [ebx-96],xmm0
|
||||
pshufb xmm6,[16+eax]
|
||||
paddd xmm4,xmm6
|
||||
movdqa [80+ebx],xmm6
|
||||
pxor xmm2,xmm4
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm0,xmm2
|
||||
pslld xmm2,7
|
||||
psrld xmm0,25
|
||||
pxor xmm7,xmm1
|
||||
por xmm2,xmm0
|
||||
pshufb xmm7,[eax]
|
||||
movdqa [ebx-16],xmm2
|
||||
paddd xmm5,xmm7
|
||||
pxor xmm3,xmm5
|
||||
movdqa xmm0,xmm3
|
||||
pslld xmm3,12
|
||||
psrld xmm0,20
|
||||
por xmm3,xmm0
|
||||
movdqa xmm0,[ebx-128]
|
||||
paddd xmm1,xmm3
|
||||
movdqa xmm6,[64+ebx]
|
||||
pxor xmm7,xmm1
|
||||
movdqa [ebx-80],xmm1
|
||||
pshufb xmm7,[16+eax]
|
||||
paddd xmm5,xmm7
|
||||
movdqa [96+ebx],xmm7
|
||||
pxor xmm3,xmm5
|
||||
movdqa xmm1,xmm3
|
||||
pslld xmm3,7
|
||||
psrld xmm1,25
|
||||
por xmm3,xmm1
|
||||
dec edx
|
||||
jnz NEAR L$010loop
|
||||
movdqa [ebx-64],xmm3
|
||||
movdqa [ebx],xmm4
|
||||
movdqa [16+ebx],xmm5
|
||||
movdqa [64+ebx],xmm6
|
||||
movdqa [96+ebx],xmm7
|
||||
movdqa xmm1,[ebx-112]
|
||||
movdqa xmm2,[ebx-96]
|
||||
movdqa xmm3,[ebx-80]
|
||||
paddd xmm0,[ebp-128]
|
||||
paddd xmm1,[ebp-112]
|
||||
paddd xmm2,[ebp-96]
|
||||
paddd xmm3,[ebp-80]
|
||||
movdqa xmm6,xmm0
|
||||
punpckldq xmm0,xmm1
|
||||
movdqa xmm7,xmm2
|
||||
punpckldq xmm2,xmm3
|
||||
punpckhdq xmm6,xmm1
|
||||
punpckhdq xmm7,xmm3
|
||||
movdqa xmm1,xmm0
|
||||
punpcklqdq xmm0,xmm2
|
||||
movdqa xmm3,xmm6
|
||||
punpcklqdq xmm6,xmm7
|
||||
punpckhqdq xmm1,xmm2
|
||||
punpckhqdq xmm3,xmm7
|
||||
movdqu xmm4,[esi-128]
|
||||
movdqu xmm5,[esi-64]
|
||||
movdqu xmm2,[esi]
|
||||
movdqu xmm7,[64+esi]
|
||||
lea esi,[16+esi]
|
||||
pxor xmm4,xmm0
|
||||
movdqa xmm0,[ebx-64]
|
||||
pxor xmm5,xmm1
|
||||
movdqa xmm1,[ebx-48]
|
||||
pxor xmm6,xmm2
|
||||
movdqa xmm2,[ebx-32]
|
||||
pxor xmm7,xmm3
|
||||
movdqa xmm3,[ebx-16]
|
||||
movdqu [edi-128],xmm4
|
||||
movdqu [edi-64],xmm5
|
||||
movdqu [edi],xmm6
|
||||
movdqu [64+edi],xmm7
|
||||
lea edi,[16+edi]
|
||||
paddd xmm0,[ebp-64]
|
||||
paddd xmm1,[ebp-48]
|
||||
paddd xmm2,[ebp-32]
|
||||
paddd xmm3,[ebp-16]
|
||||
movdqa xmm6,xmm0
|
||||
punpckldq xmm0,xmm1
|
||||
movdqa xmm7,xmm2
|
||||
punpckldq xmm2,xmm3
|
||||
punpckhdq xmm6,xmm1
|
||||
punpckhdq xmm7,xmm3
|
||||
movdqa xmm1,xmm0
|
||||
punpcklqdq xmm0,xmm2
|
||||
movdqa xmm3,xmm6
|
||||
punpcklqdq xmm6,xmm7
|
||||
punpckhqdq xmm1,xmm2
|
||||
punpckhqdq xmm3,xmm7
|
||||
movdqu xmm4,[esi-128]
|
||||
movdqu xmm5,[esi-64]
|
||||
movdqu xmm2,[esi]
|
||||
movdqu xmm7,[64+esi]
|
||||
lea esi,[16+esi]
|
||||
pxor xmm4,xmm0
|
||||
movdqa xmm0,[ebx]
|
||||
pxor xmm5,xmm1
|
||||
movdqa xmm1,[16+ebx]
|
||||
pxor xmm6,xmm2
|
||||
movdqa xmm2,[32+ebx]
|
||||
pxor xmm7,xmm3
|
||||
movdqa xmm3,[48+ebx]
|
||||
movdqu [edi-128],xmm4
|
||||
movdqu [edi-64],xmm5
|
||||
movdqu [edi],xmm6
|
||||
movdqu [64+edi],xmm7
|
||||
lea edi,[16+edi]
|
||||
paddd xmm0,[ebp]
|
||||
paddd xmm1,[16+ebp]
|
||||
paddd xmm2,[32+ebp]
|
||||
paddd xmm3,[48+ebp]
|
||||
movdqa xmm6,xmm0
|
||||
punpckldq xmm0,xmm1
|
||||
movdqa xmm7,xmm2
|
||||
punpckldq xmm2,xmm3
|
||||
punpckhdq xmm6,xmm1
|
||||
punpckhdq xmm7,xmm3
|
||||
movdqa xmm1,xmm0
|
||||
punpcklqdq xmm0,xmm2
|
||||
movdqa xmm3,xmm6
|
||||
punpcklqdq xmm6,xmm7
|
||||
punpckhqdq xmm1,xmm2
|
||||
punpckhqdq xmm3,xmm7
|
||||
movdqu xmm4,[esi-128]
|
||||
movdqu xmm5,[esi-64]
|
||||
movdqu xmm2,[esi]
|
||||
movdqu xmm7,[64+esi]
|
||||
lea esi,[16+esi]
|
||||
pxor xmm4,xmm0
|
||||
movdqa xmm0,[64+ebx]
|
||||
pxor xmm5,xmm1
|
||||
movdqa xmm1,[80+ebx]
|
||||
pxor xmm6,xmm2
|
||||
movdqa xmm2,[96+ebx]
|
||||
pxor xmm7,xmm3
|
||||
movdqa xmm3,[112+ebx]
|
||||
movdqu [edi-128],xmm4
|
||||
movdqu [edi-64],xmm5
|
||||
movdqu [edi],xmm6
|
||||
movdqu [64+edi],xmm7
|
||||
lea edi,[16+edi]
|
||||
paddd xmm0,[64+ebp]
|
||||
paddd xmm1,[80+ebp]
|
||||
paddd xmm2,[96+ebp]
|
||||
paddd xmm3,[112+ebp]
|
||||
movdqa xmm6,xmm0
|
||||
punpckldq xmm0,xmm1
|
||||
movdqa xmm7,xmm2
|
||||
punpckldq xmm2,xmm3
|
||||
punpckhdq xmm6,xmm1
|
||||
punpckhdq xmm7,xmm3
|
||||
movdqa xmm1,xmm0
|
||||
punpcklqdq xmm0,xmm2
|
||||
movdqa xmm3,xmm6
|
||||
punpcklqdq xmm6,xmm7
|
||||
punpckhqdq xmm1,xmm2
|
||||
punpckhqdq xmm3,xmm7
|
||||
movdqu xmm4,[esi-128]
|
||||
movdqu xmm5,[esi-64]
|
||||
movdqu xmm2,[esi]
|
||||
movdqu xmm7,[64+esi]
|
||||
lea esi,[208+esi]
|
||||
pxor xmm4,xmm0
|
||||
pxor xmm5,xmm1
|
||||
pxor xmm6,xmm2
|
||||
pxor xmm7,xmm3
|
||||
movdqu [edi-128],xmm4
|
||||
movdqu [edi-64],xmm5
|
||||
movdqu [edi],xmm6
|
||||
movdqu [64+edi],xmm7
|
||||
lea edi,[208+edi]
|
||||
sub ecx,256
|
||||
jnc NEAR L$009outer_loop
|
||||
add ecx,256
|
||||
jz NEAR L$011done
|
||||
mov ebx,DWORD [520+esp]
|
||||
lea esi,[esi-128]
|
||||
mov edx,DWORD [516+esp]
|
||||
lea edi,[edi-128]
|
||||
movd xmm2,DWORD [64+ebp]
|
||||
movdqu xmm3,[ebx]
|
||||
paddd xmm2,[96+eax]
|
||||
pand xmm3,[112+eax]
|
||||
por xmm3,xmm2
|
||||
L$0081x:
|
||||
movdqa xmm0,[32+eax]
|
||||
movdqu xmm1,[edx]
|
||||
movdqu xmm2,[16+edx]
|
||||
movdqa xmm6,[eax]
|
||||
movdqa xmm7,[16+eax]
|
||||
mov DWORD [48+esp],ebp
|
||||
movdqa [esp],xmm0
|
||||
movdqa [16+esp],xmm1
|
||||
movdqa [32+esp],xmm2
|
||||
movdqa [48+esp],xmm3
|
||||
mov edx,10
|
||||
jmp NEAR L$012loop1x
|
||||
align 16
|
||||
L$013outer1x:
|
||||
movdqa xmm3,[80+eax]
|
||||
movdqa xmm0,[esp]
|
||||
movdqa xmm1,[16+esp]
|
||||
movdqa xmm2,[32+esp]
|
||||
paddd xmm3,[48+esp]
|
||||
mov edx,10
|
||||
movdqa [48+esp],xmm3
|
||||
jmp NEAR L$012loop1x
|
||||
align 16
|
||||
L$012loop1x:
|
||||
paddd xmm0,xmm1
|
||||
pxor xmm3,xmm0
|
||||
db 102,15,56,0,222
|
||||
paddd xmm2,xmm3
|
||||
pxor xmm1,xmm2
|
||||
movdqa xmm4,xmm1
|
||||
psrld xmm1,20
|
||||
pslld xmm4,12
|
||||
por xmm1,xmm4
|
||||
paddd xmm0,xmm1
|
||||
pxor xmm3,xmm0
|
||||
db 102,15,56,0,223
|
||||
paddd xmm2,xmm3
|
||||
pxor xmm1,xmm2
|
||||
movdqa xmm4,xmm1
|
||||
psrld xmm1,25
|
||||
pslld xmm4,7
|
||||
por xmm1,xmm4
|
||||
pshufd xmm2,xmm2,78
|
||||
pshufd xmm1,xmm1,57
|
||||
pshufd xmm3,xmm3,147
|
||||
nop
|
||||
paddd xmm0,xmm1
|
||||
pxor xmm3,xmm0
|
||||
db 102,15,56,0,222
|
||||
paddd xmm2,xmm3
|
||||
pxor xmm1,xmm2
|
||||
movdqa xmm4,xmm1
|
||||
psrld xmm1,20
|
||||
pslld xmm4,12
|
||||
por xmm1,xmm4
|
||||
paddd xmm0,xmm1
|
||||
pxor xmm3,xmm0
|
||||
db 102,15,56,0,223
|
||||
paddd xmm2,xmm3
|
||||
pxor xmm1,xmm2
|
||||
movdqa xmm4,xmm1
|
||||
psrld xmm1,25
|
||||
pslld xmm4,7
|
||||
por xmm1,xmm4
|
||||
pshufd xmm2,xmm2,78
|
||||
pshufd xmm1,xmm1,147
|
||||
pshufd xmm3,xmm3,57
|
||||
dec edx
|
||||
jnz NEAR L$012loop1x
|
||||
paddd xmm0,[esp]
|
||||
paddd xmm1,[16+esp]
|
||||
paddd xmm2,[32+esp]
|
||||
paddd xmm3,[48+esp]
|
||||
cmp ecx,64
|
||||
jb NEAR L$014tail
|
||||
movdqu xmm4,[esi]
|
||||
movdqu xmm5,[16+esi]
|
||||
pxor xmm0,xmm4
|
||||
movdqu xmm4,[32+esi]
|
||||
pxor xmm1,xmm5
|
||||
movdqu xmm5,[48+esi]
|
||||
pxor xmm2,xmm4
|
||||
pxor xmm3,xmm5
|
||||
lea esi,[64+esi]
|
||||
movdqu [edi],xmm0
|
||||
movdqu [16+edi],xmm1
|
||||
movdqu [32+edi],xmm2
|
||||
movdqu [48+edi],xmm3
|
||||
lea edi,[64+edi]
|
||||
sub ecx,64
|
||||
jnz NEAR L$013outer1x
|
||||
jmp NEAR L$011done
|
||||
L$014tail:
|
||||
movdqa [esp],xmm0
|
||||
movdqa [16+esp],xmm1
|
||||
movdqa [32+esp],xmm2
|
||||
movdqa [48+esp],xmm3
|
||||
xor eax,eax
|
||||
xor edx,edx
|
||||
xor ebp,ebp
|
||||
L$015tail_loop:
|
||||
mov al,BYTE [ebp*1+esp]
|
||||
mov dl,BYTE [ebp*1+esi]
|
||||
lea ebp,[1+ebp]
|
||||
xor al,dl
|
||||
mov BYTE [ebp*1+edi-1],al
|
||||
dec ecx
|
||||
jnz NEAR L$015tail_loop
|
||||
L$011done:
|
||||
mov esp,DWORD [512+esp]
|
||||
pop edi
|
||||
pop esi
|
||||
pop ebx
|
||||
pop ebp
|
||||
ret
|
||||
align 64
|
||||
L$ssse3_data:
|
||||
db 2,3,0,1,6,7,4,5,10,11,8,9,14,15,12,13
|
||||
db 3,0,1,2,7,4,5,6,11,8,9,10,15,12,13,14
|
||||
dd 1634760805,857760878,2036477234,1797285236
|
||||
dd 0,1,2,3
|
||||
dd 4,4,4,4
|
||||
dd 1,0,0,0
|
||||
dd 4,0,0,0
|
||||
dd 0,-1,-1,-1
|
||||
align 64
|
||||
db 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54
|
||||
db 44,32,67,82,89,80,84,79,71,65,77,83,32,98,121,32
|
||||
db 60,97,112,112,114,111,64,111,112,101,110,115,115,108,46,111
|
||||
db 114,103,62,0
|
||||
segment .bss
|
||||
common _OPENSSL_ia32cap_P 16
|
||||
%else
|
||||
; Work around https://bugzilla.nasm.us/show_bug.cgi?id=3392738
|
||||
ret
|
||||
%endif
|
||||
#endif // defined(__i386__) && defined(_WIN32)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
|
||||
@@ -1,979 +0,0 @@
|
||||
#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
|
||||
|
||||
+5
-10
@@ -3,21 +3,15 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__)
|
||||
.text
|
||||
|
||||
.extern OPENSSL_ia32cap_P
|
||||
.hidden OPENSSL_ia32cap_P
|
||||
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lzero:
|
||||
.long 0,0,0,0
|
||||
@@ -47,12 +41,14 @@
|
||||
.Lsixteen:
|
||||
.long 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
|
||||
.byte 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54,95,54,52,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
|
||||
.text
|
||||
.globl ChaCha20_ctr32
|
||||
.hidden ChaCha20_ctr32
|
||||
.type ChaCha20_ctr32,@function
|
||||
.align 64
|
||||
ChaCha20_ctr32:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
cmpq $0,%rdx
|
||||
je .Lno_data
|
||||
movq OPENSSL_ia32cap_P+4(%rip),%r10
|
||||
@@ -1632,7 +1628,6 @@ ChaCha20_8x:
|
||||
.cfi_endproc
|
||||
.size ChaCha20_8x,.-ChaCha20_8x
|
||||
#endif
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
#endif // defined(__x86_64__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+5
-9
@@ -3,20 +3,14 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__)
|
||||
.text
|
||||
|
||||
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$zero:
|
||||
.long 0,0,0,0
|
||||
@@ -46,12 +40,14 @@ L$incz:
|
||||
L$sixteen:
|
||||
.long 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
|
||||
.byte 67,104,97,67,104,97,50,48,32,102,111,114,32,120,56,54,95,54,52,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
|
||||
.text
|
||||
.globl _ChaCha20_ctr32
|
||||
.private_extern _ChaCha20_ctr32
|
||||
|
||||
.p2align 6
|
||||
_ChaCha20_ctr32:
|
||||
|
||||
_CET_ENDBR
|
||||
cmpq $0,%rdx
|
||||
je L$no_data
|
||||
movq _OPENSSL_ia32cap_P+4(%rip),%r10
|
||||
@@ -91,7 +91,25 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
|
||||
}
|
||||
#endif
|
||||
|
||||
ChaCha20_ctr32(out, in, in_len, key_ptr, counter_nonce);
|
||||
while (in_len > 0) {
|
||||
// The assembly functions do not have defined overflow behavior. While
|
||||
// overflow is almost always a bug in the caller, we prefer our functions to
|
||||
// behave the same across platforms, so divide into multiple calls to avoid
|
||||
// this case.
|
||||
uint64_t todo = 64 * ((UINT64_C(1) << 32) - counter_nonce[0]);
|
||||
if (todo > in_len) {
|
||||
todo = in_len;
|
||||
}
|
||||
|
||||
ChaCha20_ctr32(out, in, (size_t)todo, key_ptr, counter_nonce);
|
||||
in += todo;
|
||||
out += todo;
|
||||
in_len -= todo;
|
||||
|
||||
// We're either done and will next break out of the loop, or we stopped at
|
||||
// the wraparound point and the counter should continue at zero.
|
||||
counter_nonce[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@@ -32,7 +32,14 @@ void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32],
|
||||
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
|
||||
#define CHACHA20_ASM
|
||||
|
||||
// ChaCha20_ctr32 is defined in asm/chacha-*.pl.
|
||||
// ChaCha20_ctr32 encrypts |in_len| bytes from |in| and writes the result to
|
||||
// |out|. If |in| and |out| alias, they must be equal.
|
||||
//
|
||||
// |counter[0]| is the initial 32-bit block counter, and the remainder is the
|
||||
// 96-bit nonce. If the counter overflows, the output is undefined. The function
|
||||
// will produce output, but the output may vary by machine and may not be
|
||||
// self-consistent. (On some architectures, the assembly implements a mix of
|
||||
// 64-bit and 32-bit counters.)
|
||||
void ChaCha20_ctr32(uint8_t *out, const uint8_t *in, size_t in_len,
|
||||
const uint32_t key[8], const uint32_t counter[4]);
|
||||
#endif
|
||||
|
||||
+21
-11
@@ -3,17 +3,10 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.data
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__)
|
||||
.section .rodata
|
||||
|
||||
.align 16
|
||||
one:
|
||||
@@ -80,6 +73,7 @@ GFMUL:
|
||||
.align 16
|
||||
aesgcmsiv_htable_init:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rsi),%xmm0
|
||||
vmovdqa %xmm0,%xmm1
|
||||
vmovdqa %xmm0,(%rdi)
|
||||
@@ -106,6 +100,7 @@ aesgcmsiv_htable_init:
|
||||
.align 16
|
||||
aesgcmsiv_htable6_init:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rsi),%xmm0
|
||||
vmovdqa %xmm0,%xmm1
|
||||
vmovdqa %xmm0,(%rdi)
|
||||
@@ -128,6 +123,7 @@ aesgcmsiv_htable6_init:
|
||||
.align 16
|
||||
aesgcmsiv_htable_polyval:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %rdx,%rdx
|
||||
jnz .Lhtable_polyval_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -345,6 +341,7 @@ aesgcmsiv_htable_polyval:
|
||||
.align 16
|
||||
aesgcmsiv_polyval_horner:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %rcx,%rcx
|
||||
jnz .Lpolyval_horner_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -378,6 +375,7 @@ aesgcmsiv_polyval_horner:
|
||||
.align 16
|
||||
aes128gcmsiv_aes_ks:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqu (%rdi),%xmm1
|
||||
vmovdqa %xmm1,(%rsi)
|
||||
|
||||
@@ -434,6 +432,7 @@ aes128gcmsiv_aes_ks:
|
||||
.align 16
|
||||
aes256gcmsiv_aes_ks:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqu (%rdi),%xmm1
|
||||
vmovdqu 16(%rdi),%xmm3
|
||||
vmovdqa %xmm1,(%rsi)
|
||||
@@ -481,6 +480,7 @@ aes256gcmsiv_aes_ks:
|
||||
.align 16
|
||||
aes128gcmsiv_aes_ks_enc_x1:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rcx),%xmm1
|
||||
vmovdqa 0(%rdi),%xmm4
|
||||
|
||||
@@ -623,6 +623,7 @@ aes128gcmsiv_aes_ks_enc_x1:
|
||||
.align 16
|
||||
aes128gcmsiv_kdf:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
|
||||
|
||||
|
||||
@@ -716,6 +717,7 @@ aes128gcmsiv_kdf:
|
||||
.align 16
|
||||
aes128gcmsiv_enc_msg_x4:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz .L128_enc_msg_x4_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -895,6 +897,7 @@ aes128gcmsiv_enc_msg_x4:
|
||||
.align 16
|
||||
aes128gcmsiv_enc_msg_x8:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz .L128_enc_msg_x8_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -1156,6 +1159,7 @@ aes128gcmsiv_enc_msg_x8:
|
||||
.align 16
|
||||
aes128gcmsiv_dec:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq $~15,%r9
|
||||
jnz .L128_dec_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -1648,6 +1652,7 @@ aes128gcmsiv_dec:
|
||||
.align 16
|
||||
aes128gcmsiv_ecb_enc_block:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rdi),%xmm1
|
||||
|
||||
vpxor (%rdx),%xmm1,%xmm1
|
||||
@@ -1673,6 +1678,7 @@ aes128gcmsiv_ecb_enc_block:
|
||||
.align 16
|
||||
aes256gcmsiv_aes_ks_enc_x1:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa con1(%rip),%xmm0
|
||||
vmovdqa mask(%rip),%xmm15
|
||||
vmovdqa (%rdi),%xmm8
|
||||
@@ -1856,6 +1862,7 @@ aes256gcmsiv_aes_ks_enc_x1:
|
||||
.align 16
|
||||
aes256gcmsiv_ecb_enc_block:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rdi),%xmm1
|
||||
vpxor (%rdx),%xmm1,%xmm1
|
||||
vaesenc 16(%rdx),%xmm1,%xmm1
|
||||
@@ -1882,6 +1889,7 @@ aes256gcmsiv_ecb_enc_block:
|
||||
.align 16
|
||||
aes256gcmsiv_enc_msg_x4:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz .L256_enc_msg_x4_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2083,6 +2091,7 @@ aes256gcmsiv_enc_msg_x4:
|
||||
.align 16
|
||||
aes256gcmsiv_enc_msg_x8:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz .L256_enc_msg_x8_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2372,6 +2381,7 @@ aes256gcmsiv_enc_msg_x8:
|
||||
.align 16
|
||||
aes256gcmsiv_dec:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
testq $~15,%r9
|
||||
jnz .L256_dec_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2932,6 +2942,7 @@ aes256gcmsiv_dec:
|
||||
.align 16
|
||||
aes256gcmsiv_kdf:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
|
||||
|
||||
|
||||
@@ -3078,7 +3089,6 @@ aes256gcmsiv_kdf:
|
||||
.cfi_endproc
|
||||
.size aes256gcmsiv_kdf, .-aes256gcmsiv_kdf
|
||||
#endif
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
#endif // defined(__x86_64__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+21
-10
@@ -3,17 +3,10 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
.data
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__)
|
||||
.section __DATA,__const
|
||||
|
||||
.p2align 4
|
||||
one:
|
||||
@@ -80,6 +73,7 @@ GFMUL:
|
||||
.p2align 4
|
||||
_aesgcmsiv_htable_init:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rsi),%xmm0
|
||||
vmovdqa %xmm0,%xmm1
|
||||
vmovdqa %xmm0,(%rdi)
|
||||
@@ -106,6 +100,7 @@ _aesgcmsiv_htable_init:
|
||||
.p2align 4
|
||||
_aesgcmsiv_htable6_init:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rsi),%xmm0
|
||||
vmovdqa %xmm0,%xmm1
|
||||
vmovdqa %xmm0,(%rdi)
|
||||
@@ -128,6 +123,7 @@ _aesgcmsiv_htable6_init:
|
||||
.p2align 4
|
||||
_aesgcmsiv_htable_polyval:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %rdx,%rdx
|
||||
jnz L$htable_polyval_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -345,6 +341,7 @@ L$htable_polyval_out:
|
||||
.p2align 4
|
||||
_aesgcmsiv_polyval_horner:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %rcx,%rcx
|
||||
jnz L$polyval_horner_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -378,6 +375,7 @@ L$polyval_horner_loop:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_aes_ks:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqu (%rdi),%xmm1
|
||||
vmovdqa %xmm1,(%rsi)
|
||||
|
||||
@@ -434,6 +432,7 @@ L$ks128_loop:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_aes_ks:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqu (%rdi),%xmm1
|
||||
vmovdqu 16(%rdi),%xmm3
|
||||
vmovdqa %xmm1,(%rsi)
|
||||
@@ -481,6 +480,7 @@ L$ks256_loop:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_aes_ks_enc_x1:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rcx),%xmm1
|
||||
vmovdqa 0(%rdi),%xmm4
|
||||
|
||||
@@ -623,6 +623,7 @@ _aes128gcmsiv_aes_ks_enc_x1:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_kdf:
|
||||
|
||||
_CET_ENDBR
|
||||
|
||||
|
||||
|
||||
@@ -716,6 +717,7 @@ _aes128gcmsiv_kdf:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_enc_msg_x4:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz L$128_enc_msg_x4_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -891,6 +893,7 @@ L$128_enc_msg_x4_out:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_enc_msg_x8:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz L$128_enc_msg_x8_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -1146,6 +1149,7 @@ L$128_enc_msg_x8_out:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_dec:
|
||||
|
||||
_CET_ENDBR
|
||||
testq $~15,%r9
|
||||
jnz L$128_dec_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -1638,6 +1642,7 @@ L$128_dec_out:
|
||||
.p2align 4
|
||||
_aes128gcmsiv_ecb_enc_block:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rdi),%xmm1
|
||||
|
||||
vpxor (%rdx),%xmm1,%xmm1
|
||||
@@ -1663,6 +1668,7 @@ _aes128gcmsiv_ecb_enc_block:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_aes_ks_enc_x1:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa con1(%rip),%xmm0
|
||||
vmovdqa mask(%rip),%xmm15
|
||||
vmovdqa (%rdi),%xmm8
|
||||
@@ -1846,6 +1852,7 @@ _aes256gcmsiv_aes_ks_enc_x1:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_ecb_enc_block:
|
||||
|
||||
_CET_ENDBR
|
||||
vmovdqa (%rdi),%xmm1
|
||||
vpxor (%rdx),%xmm1,%xmm1
|
||||
vaesenc 16(%rdx),%xmm1,%xmm1
|
||||
@@ -1872,6 +1879,7 @@ _aes256gcmsiv_ecb_enc_block:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_enc_msg_x4:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz L$256_enc_msg_x4_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2073,6 +2081,7 @@ L$256_enc_msg_x4_out:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_enc_msg_x8:
|
||||
|
||||
_CET_ENDBR
|
||||
testq %r8,%r8
|
||||
jnz L$256_enc_msg_x8_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2362,6 +2371,7 @@ L$256_enc_msg_x8_out:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_dec:
|
||||
|
||||
_CET_ENDBR
|
||||
testq $~15,%r9
|
||||
jnz L$256_dec_start
|
||||
.byte 0xf3,0xc3
|
||||
@@ -2922,6 +2932,7 @@ L$256_dec_out:
|
||||
.p2align 4
|
||||
_aes256gcmsiv_kdf:
|
||||
|
||||
_CET_ENDBR
|
||||
|
||||
|
||||
|
||||
+3
-11
@@ -3,17 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__APPLE__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
.section __TEXT,__const
|
||||
|
||||
@@ -3016,7 +3008,7 @@ Lopen_128_hash_64:
|
||||
b Lopen_128_hash_64
|
||||
.cfi_endproc
|
||||
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_AARCH64) && defined(__APPLE__)
|
||||
#endif // defined(__aarch64__) && defined(__APPLE__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+3
-14
@@ -3,18 +3,9 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if !defined(__has_feature)
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM)
|
||||
#if defined(__aarch64__)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_AARCH64) && defined(__ELF__)
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
.section .rodata
|
||||
|
||||
@@ -3017,9 +3008,7 @@ chacha20_poly1305_open:
|
||||
b .Lopen_128_hash_64
|
||||
.cfi_endproc
|
||||
.size chacha20_poly1305_open,.-chacha20_poly1305_open
|
||||
#endif
|
||||
#endif // !OPENSSL_NO_ASM
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif // !OPENSSL_NO_ASM && defined(OPENSSL_AARCH64) && defined(__ELF__)
|
||||
#endif // defined(__aarch64__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+6
-10
@@ -3,22 +3,16 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__ELF__)
|
||||
.text
|
||||
.extern OPENSSL_ia32cap_P
|
||||
.hidden OPENSSL_ia32cap_P
|
||||
|
||||
chacha20_poly1305_constants:
|
||||
|
||||
.section .rodata
|
||||
.align 64
|
||||
.Lchacha20_consts:
|
||||
.byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'
|
||||
@@ -56,6 +50,7 @@ chacha20_poly1305_constants:
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
|
||||
.text
|
||||
|
||||
.type poly_hash_ad_internal,@function
|
||||
.align 64
|
||||
@@ -230,6 +225,7 @@ poly_hash_ad_internal:
|
||||
.align 64
|
||||
chacha20_poly1305_open:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
pushq %rbp
|
||||
.cfi_adjust_cfa_offset 8
|
||||
.cfi_offset %rbp,-16
|
||||
@@ -2115,6 +2111,7 @@ chacha20_poly1305_open:
|
||||
.align 64
|
||||
chacha20_poly1305_seal:
|
||||
.cfi_startproc
|
||||
_CET_ENDBR
|
||||
pushq %rbp
|
||||
.cfi_adjust_cfa_offset 8
|
||||
.cfi_offset %rbp,-16
|
||||
@@ -8921,7 +8918,6 @@ chacha20_poly1305_seal_avx2:
|
||||
.cfi_endproc
|
||||
.size chacha20_poly1305_seal_avx2, .-chacha20_poly1305_seal_avx2
|
||||
#endif
|
||||
.section .note.GNU-stack,"",@progbits
|
||||
#endif // defined(__x86_64__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
+6
-9
@@ -3,21 +3,15 @@
|
||||
// This file is generated from a similarly-named Perl script in the BoringSSL
|
||||
// source tree. Do not edit by hand.
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if defined(__x86_64__) && !defined(OPENSSL_NO_ASM)
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__APPLE__)
|
||||
.text
|
||||
|
||||
|
||||
chacha20_poly1305_constants:
|
||||
|
||||
.section __DATA,__const
|
||||
.p2align 6
|
||||
L$chacha20_consts:
|
||||
.byte 'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'
|
||||
@@ -55,6 +49,7 @@ L$and_masks:
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00
|
||||
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
|
||||
.text
|
||||
|
||||
|
||||
.p2align 6
|
||||
@@ -229,6 +224,7 @@ L$hash_ad_done:
|
||||
.p2align 6
|
||||
_chacha20_poly1305_open:
|
||||
|
||||
_CET_ENDBR
|
||||
pushq %rbp
|
||||
|
||||
pushq %rbx
|
||||
@@ -2100,6 +2096,7 @@ L$open_sse_128_xor_hash:
|
||||
.p2align 6
|
||||
_chacha20_poly1305_seal:
|
||||
|
||||
_CET_ENDBR
|
||||
pushq %rbp
|
||||
|
||||
pushq %rbx
|
||||
@@ -400,6 +400,14 @@ 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) {
|
||||
@@ -442,13 +450,6 @@ static int aead_tls_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aead_null_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) {
|
||||
return aead_tls_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
|
||||
EVP_sha1(), 1 /* implicit iv */);
|
||||
}
|
||||
|
||||
static const EVP_AEAD aead_aes_128_cbc_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH + 16, // key len (SHA1 + AES128)
|
||||
16, // nonce len (IV)
|
||||
@@ -483,6 +484,23 @@ 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)
|
||||
@@ -551,23 +569,6 @@ static const EVP_AEAD aead_des_ede3_cbc_sha1_tls_implicit_iv = {
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
static const EVP_AEAD aead_null_sha1_tls = {
|
||||
SHA_DIGEST_LENGTH, // key len
|
||||
0, // nonce len
|
||||
SHA_DIGEST_LENGTH, // overhead (SHA1)
|
||||
SHA_DIGEST_LENGTH, // max tag length
|
||||
0, // seal_scatter_supports_extra_in
|
||||
|
||||
NULL, // init
|
||||
aead_null_sha1_tls_init,
|
||||
aead_tls_cleanup,
|
||||
aead_tls_open,
|
||||
aead_tls_seal_scatter,
|
||||
NULL, // open_gather
|
||||
NULL, // get_iv
|
||||
aead_tls_tag_len,
|
||||
};
|
||||
|
||||
const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void) {
|
||||
return &aead_aes_128_cbc_sha1_tls;
|
||||
}
|
||||
@@ -576,6 +577,10 @@ 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;
|
||||
}
|
||||
@@ -591,5 +596,3 @@ const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void) {
|
||||
const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void) {
|
||||
return &aead_des_ede3_cbc_sha1_tls_implicit_iv;
|
||||
}
|
||||
|
||||
const EVP_AEAD *EVP_aead_null_sha1_tls(void) { return &aead_null_sha1_tls; }
|
||||
@@ -109,6 +109,14 @@ 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_sha256_final_with_secret_suffix acts like
|
||||
// |EVP_sha1_final_with_secret_suffix|, but for SHA-256.
|
||||
//
|
||||
// This function is exported for unit tests.
|
||||
OPENSSL_EXPORT int EVP_sha256_final_with_secret_suffix(
|
||||
SHA256_CTX *ctx, uint8_t out[SHA256_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.
|
||||
//
|
||||
|
||||
@@ -267,24 +267,115 @@ int EVP_sha1_final_with_secret_suffix(SHA_CTX *ctx,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_tls_cbc_record_digest_supported(const EVP_MD *md) {
|
||||
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_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length) {
|
||||
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);
|
||||
*md_out_size = 0;
|
||||
return 0;
|
||||
int EVP_sha256_final_with_secret_suffix(SHA256_CTX *ctx,
|
||||
uint8_t out[SHA256_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;
|
||||
}
|
||||
|
||||
// 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 + SHA256_CBLOCK - 1) >> 6;
|
||||
size_t last_block = num_blocks - 1;
|
||||
size_t max_blocks = (ctx->num + max_len + 1 + 8 + SHA256_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[SHA256_CBLOCK] = {0};
|
||||
uint32_t result[8] = {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 = SHA256_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 < SHA256_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 += SHA256_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[SHA256_CBLOCK - 4 + j] |= is_last_block & length_bytes[j];
|
||||
}
|
||||
|
||||
// Process the block and save the hash state if it is the final value.
|
||||
SHA256_Transform(ctx, block);
|
||||
for (size_t j = 0; j < 8; j++) {
|
||||
result[j] |= is_last_block & ctx->h[j];
|
||||
}
|
||||
}
|
||||
|
||||
// Write the output.
|
||||
for (size_t i = 0; i < 8; 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:
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int tls_cbc_digest_record_sha1(uint8_t *md_out, size_t *md_out_size,
|
||||
const uint8_t header[13],
|
||||
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) {
|
||||
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.
|
||||
@@ -336,3 +427,88 @@ int EVP_tls_cbc_digest_record(const EVP_MD *md, uint8_t *md_out,
|
||||
*md_out_size = SHA_DIGEST_LENGTH;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tls_cbc_digest_record_sha256(uint8_t *md_out, size_t *md_out_size,
|
||||
const uint8_t header[13],
|
||||
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) {
|
||||
if (mac_secret_length > SHA256_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;
|
||||
}
|
||||
|
||||
// Compute the initial HMAC block.
|
||||
uint8_t hmac_pad[SHA256_CBLOCK];
|
||||
OPENSSL_memset(hmac_pad, 0, sizeof(hmac_pad));
|
||||
OPENSSL_memcpy(hmac_pad, mac_secret, mac_secret_length);
|
||||
for (size_t i = 0; i < SHA256_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x36;
|
||||
}
|
||||
|
||||
SHA256_CTX ctx;
|
||||
SHA256_Init(&ctx);
|
||||
SHA256_Update(&ctx, hmac_pad, SHA256_CBLOCK);
|
||||
SHA256_Update(&ctx, header, 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 > SHA256_DIGEST_LENGTH + 256) {
|
||||
min_data_size =
|
||||
data_plus_mac_plus_padding_size - SHA256_DIGEST_LENGTH - 256;
|
||||
}
|
||||
|
||||
// Hash the public minimum length directly. This reduces the number of blocks
|
||||
// that must be computed in constant-time.
|
||||
SHA256_Update(&ctx, data, min_data_size);
|
||||
|
||||
// Hash the remaining data without leaking |data_size|.
|
||||
uint8_t mac_out[SHA256_DIGEST_LENGTH];
|
||||
if (!EVP_sha256_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.
|
||||
SHA256_Init(&ctx);
|
||||
for (size_t i = 0; i < SHA256_CBLOCK; i++) {
|
||||
hmac_pad[i] ^= 0x6a;
|
||||
}
|
||||
|
||||
SHA256_Update(&ctx, hmac_pad, SHA256_CBLOCK);
|
||||
SHA256_Update(&ctx, mac_out, SHA256_DIGEST_LENGTH);
|
||||
SHA256_Final(md_out, &ctx);
|
||||
*md_out_size = SHA256_DIGEST_LENGTH;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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_size,
|
||||
size_t data_plus_mac_plus_padding_size,
|
||||
const uint8_t *mac_secret,
|
||||
unsigned mac_secret_length) {
|
||||
switch (EVP_MD_type(md)) {
|
||||
case NID_sha1:
|
||||
return tls_cbc_digest_record_sha1(
|
||||
md_out, md_out_size, header, data, data_size,
|
||||
data_plus_mac_plus_padding_size, mac_secret, mac_secret_length);
|
||||
|
||||
case NID_sha256:
|
||||
return tls_cbc_digest_record_sha256(
|
||||
md_out, md_out_size, header, data, data_size,
|
||||
data_plus_mac_plus_padding_size, mac_secret, mac_secret_length);
|
||||
|
||||
default:
|
||||
// EVP_tls_cbc_record_digest_supported should have been called first to
|
||||
// check that the hash function is supported.
|
||||
assert(0);
|
||||
*md_out_size = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -68,21 +68,10 @@
|
||||
#include "conf_def.h"
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
#include "../lhash/internal.h"
|
||||
|
||||
|
||||
DEFINE_LHASH_OF(CONF_VALUE)
|
||||
|
||||
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) {
|
||||
const uint32_t section_hash = v->section ? OPENSSL_strhash(v->section) : 0;
|
||||
const uint32_t name_hash = v->name ? OPENSSL_strhash(v->name) : 0;
|
||||
@@ -132,7 +121,6 @@ CONF *NCONF_new(void *method) {
|
||||
CONF_VALUE *CONF_VALUE_new(void) {
|
||||
CONF_VALUE *v = OPENSSL_malloc(sizeof(CONF_VALUE));
|
||||
if (!v) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(v, 0, sizeof(CONF_VALUE));
|
||||
@@ -140,24 +128,23 @@ CONF_VALUE *CONF_VALUE_new(void) {
|
||||
}
|
||||
|
||||
static void value_free_contents(CONF_VALUE *value) {
|
||||
if (value->section) {
|
||||
OPENSSL_free(value->section);
|
||||
}
|
||||
OPENSSL_free(value->section);
|
||||
if (value->name) {
|
||||
OPENSSL_free(value->name);
|
||||
if (value->value) {
|
||||
OPENSSL_free(value->value);
|
||||
}
|
||||
OPENSSL_free(value->value);
|
||||
} else {
|
||||
if (value->value) {
|
||||
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
|
||||
}
|
||||
// TODO(davidben): When |value->name| is NULL, |CONF_VALUE| is actually an
|
||||
// entirely different structure. This is fragile and confusing. Make a
|
||||
// proper |CONF_SECTION| type that doesn't require this.
|
||||
sk_CONF_VALUE_free((STACK_OF(CONF_VALUE) *)value->value);
|
||||
}
|
||||
}
|
||||
|
||||
static void value_free(CONF_VALUE *value) {
|
||||
value_free_contents(value);
|
||||
OPENSSL_free(value);
|
||||
if (value != NULL) {
|
||||
value_free_contents(value);
|
||||
OPENSSL_free(value);
|
||||
}
|
||||
}
|
||||
|
||||
static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); }
|
||||
@@ -193,28 +180,21 @@ static CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
|
||||
if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
|
||||
goto err;
|
||||
}
|
||||
if (old_value) {
|
||||
value_free(old_value);
|
||||
}
|
||||
value_free(old_value);
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
if (!ok) {
|
||||
if (sk != NULL) {
|
||||
sk_CONF_VALUE_free(sk);
|
||||
}
|
||||
if (v != NULL) {
|
||||
OPENSSL_free(v);
|
||||
}
|
||||
sk_CONF_VALUE_free(sk);
|
||||
OPENSSL_free(v);
|
||||
v = NULL;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static int str_copy(CONF *conf, char *section, char **pto, char *from) {
|
||||
int q, r, rr = 0, to = 0, len = 0;
|
||||
char *s, *e, *rp, *rrp, *np, *cp, v;
|
||||
const char *p;
|
||||
int q, to = 0, len = 0;
|
||||
char v;
|
||||
BUF_MEM *buf;
|
||||
|
||||
buf = BUF_MEM_new();
|
||||
@@ -243,22 +223,6 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
|
||||
if (*from == q) {
|
||||
from++;
|
||||
}
|
||||
} else if (IS_DQUOTE(conf, *from)) {
|
||||
q = *from;
|
||||
from++;
|
||||
while (!IS_EOF(conf, *from)) {
|
||||
if (*from == q) {
|
||||
if (*(from + 1) == q) {
|
||||
from++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
buf->data[to++] = *(from++);
|
||||
}
|
||||
if (*from == q) {
|
||||
from++;
|
||||
}
|
||||
} else if (IS_ESC(conf, *from)) {
|
||||
from++;
|
||||
v = *(from++);
|
||||
@@ -277,103 +241,23 @@ static int str_copy(CONF *conf, char *section, char **pto, char *from) {
|
||||
} else if (IS_EOF(conf, *from)) {
|
||||
break;
|
||||
} else if (*from == '$') {
|
||||
// try to expand it
|
||||
rrp = NULL;
|
||||
s = &(from[1]);
|
||||
if (*s == '{') {
|
||||
q = '}';
|
||||
} else if (*s == '(') {
|
||||
q = ')';
|
||||
} else {
|
||||
q = 0;
|
||||
}
|
||||
|
||||
if (q) {
|
||||
s++;
|
||||
}
|
||||
cp = section;
|
||||
e = np = s;
|
||||
while (IS_ALPHA_NUMERIC(conf, *e)) {
|
||||
e++;
|
||||
}
|
||||
if (e[0] == ':' && e[1] == ':') {
|
||||
cp = np;
|
||||
rrp = e;
|
||||
rr = *e;
|
||||
*rrp = '\0';
|
||||
e += 2;
|
||||
np = e;
|
||||
while (IS_ALPHA_NUMERIC(conf, *e)) {
|
||||
e++;
|
||||
}
|
||||
}
|
||||
r = *e;
|
||||
*e = '\0';
|
||||
rp = e;
|
||||
if (q) {
|
||||
if (r != q) {
|
||||
OPENSSL_PUT_ERROR(CONF, CONF_R_NO_CLOSE_BRACE);
|
||||
goto err;
|
||||
}
|
||||
e++;
|
||||
}
|
||||
// So at this point we have
|
||||
// np which is the start of the name string which is
|
||||
// '\0' terminated.
|
||||
// cp which is the start of the section string which is
|
||||
// '\0' terminated.
|
||||
// e is the 'next point after'.
|
||||
// r and rr are the chars replaced by the '\0'
|
||||
// rp and rrp is where 'r' and 'rr' came from.
|
||||
p = NCONF_get_string(conf, cp, np);
|
||||
if (rrp != NULL) {
|
||||
*rrp = rr;
|
||||
}
|
||||
*rp = r;
|
||||
if (p == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_HAS_NO_VALUE);
|
||||
goto err;
|
||||
}
|
||||
size_t newsize = strlen(p) + buf->length - (e - from);
|
||||
if (newsize > MAX_CONF_VALUE_LENGTH) {
|
||||
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
|
||||
goto err;
|
||||
}
|
||||
if (!BUF_MEM_grow_clean(buf, newsize)) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
while (*p) {
|
||||
buf->data[to++] = *(p++);
|
||||
}
|
||||
|
||||
/* Since we change the pointer 'from', we also have
|
||||
to change the perceived length of the string it
|
||||
points at. /RL */
|
||||
len -= e - from;
|
||||
from = e;
|
||||
|
||||
/* In case there were no braces or parenthesis around
|
||||
the variable reference, we have to put back the
|
||||
character that was replaced with a '\0'. /RL */
|
||||
*rp = r;
|
||||
// Historically, $foo would expand to a previously-parsed value. This
|
||||
// feature has been removed as it was unused and is a DoS vector.
|
||||
OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED);
|
||||
goto err;
|
||||
} else {
|
||||
buf->data[to++] = *(from++);
|
||||
}
|
||||
}
|
||||
|
||||
buf->data[to] = '\0';
|
||||
if (*pto != NULL) {
|
||||
OPENSSL_free(*pto);
|
||||
}
|
||||
OPENSSL_free(*pto);
|
||||
*pto = buf->data;
|
||||
OPENSSL_free(buf);
|
||||
return 1;
|
||||
|
||||
err:
|
||||
if (buf != NULL) {
|
||||
BUF_MEM_free(buf);
|
||||
}
|
||||
BUF_MEM_free(buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -385,8 +269,9 @@ static CONF_VALUE *get_section(const CONF *conf, const char *section) {
|
||||
return lh_CONF_VALUE_retrieve(conf->data, &template);
|
||||
}
|
||||
|
||||
STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) {
|
||||
CONF_VALUE *section_value = get_section(conf, section);
|
||||
const STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,
|
||||
const char *section) {
|
||||
const CONF_VALUE *section_value = get_section(conf, section);
|
||||
if (section_value == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -473,33 +358,8 @@ static char *scan_quote(CONF *conf, char *p) {
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static char *scan_dquote(CONF *conf, char *p) {
|
||||
int q = *p;
|
||||
|
||||
p++;
|
||||
while (!(IS_EOF(conf, *p))) {
|
||||
if (*p == q) {
|
||||
if (*(p + 1) == q) {
|
||||
p++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (*p == q) {
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static void clear_comments(CONF *conf, char *p) {
|
||||
for (;;) {
|
||||
if (IS_FCOMMENT(conf, *p)) {
|
||||
*p = '\0';
|
||||
return;
|
||||
}
|
||||
if (!IS_WS(conf, *p)) {
|
||||
break;
|
||||
}
|
||||
@@ -511,10 +371,6 @@ static void clear_comments(CONF *conf, char *p) {
|
||||
*p = '\0';
|
||||
return;
|
||||
}
|
||||
if (IS_DQUOTE(conf, *p)) {
|
||||
p = scan_dquote(conf, p);
|
||||
continue;
|
||||
}
|
||||
if (IS_QUOTE(conf, *p)) {
|
||||
p = scan_quote(conf, p);
|
||||
continue;
|
||||
@@ -531,7 +387,7 @@ static void clear_comments(CONF *conf, char *p) {
|
||||
}
|
||||
}
|
||||
|
||||
static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
int NCONF_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
static const size_t CONFBUFSIZE = 512;
|
||||
int bufnum = 0, i, ii;
|
||||
BUF_MEM *buff = NULL;
|
||||
@@ -551,7 +407,6 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
|
||||
section = OPENSSL_strdup(kDefaultSectionName);
|
||||
if (section == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
@@ -686,7 +541,6 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
}
|
||||
v->name = OPENSSL_strdup(pname);
|
||||
if (v->name == NULL) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
if (!str_copy(conf, psection, &(v->value), start)) {
|
||||
@@ -705,43 +559,28 @@ static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
|
||||
tv = sv;
|
||||
}
|
||||
if (add_string(conf, tv, v) == 0) {
|
||||
OPENSSL_PUT_ERROR(CONF, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
v = NULL;
|
||||
}
|
||||
}
|
||||
if (buff != NULL) {
|
||||
BUF_MEM_free(buff);
|
||||
}
|
||||
if (section != NULL) {
|
||||
OPENSSL_free(section);
|
||||
}
|
||||
BUF_MEM_free(buff);
|
||||
OPENSSL_free(section);
|
||||
return 1;
|
||||
|
||||
err:
|
||||
if (buff != NULL) {
|
||||
BUF_MEM_free(buff);
|
||||
}
|
||||
if (section != NULL) {
|
||||
OPENSSL_free(section);
|
||||
}
|
||||
BUF_MEM_free(buff);
|
||||
OPENSSL_free(section);
|
||||
if (out_error_line != NULL) {
|
||||
*out_error_line = eline;
|
||||
}
|
||||
BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
|
||||
snprintf(btmp, sizeof btmp, "%ld", eline);
|
||||
ERR_add_error_data(2, "line ", btmp);
|
||||
|
||||
if (v != NULL) {
|
||||
if (v->name != NULL) {
|
||||
OPENSSL_free(v->name);
|
||||
}
|
||||
if (v->value != NULL) {
|
||||
OPENSSL_free(v->value);
|
||||
}
|
||||
if (v != NULL) {
|
||||
OPENSSL_free(v);
|
||||
}
|
||||
OPENSSL_free(v->name);
|
||||
OPENSSL_free(v->value);
|
||||
OPENSSL_free(v);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -755,16 +594,12 @@ int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = def_load_bio(conf, in, out_error_line);
|
||||
ret = NCONF_load_bio(conf, in, out_error_line);
|
||||
BIO_free(in);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line) {
|
||||
return def_load_bio(conf, bio, out_error_line);
|
||||
}
|
||||
|
||||
int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
int (*list_cb)(const char *elem, size_t len, void *usr),
|
||||
void *arg) {
|
||||
@@ -779,7 +614,7 @@ int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
lstart = list;
|
||||
for (;;) {
|
||||
if (remove_whitespace) {
|
||||
while (*lstart && isspace((unsigned char)*lstart)) {
|
||||
while (*lstart && OPENSSL_isspace((unsigned char)*lstart)) {
|
||||
lstart++;
|
||||
}
|
||||
}
|
||||
@@ -793,7 +628,7 @@ int CONF_parse_list(const char *list, char sep, int remove_whitespace,
|
||||
tmpend = lstart + strlen(lstart) - 1;
|
||||
}
|
||||
if (remove_whitespace) {
|
||||
while (isspace((unsigned char)*tmpend)) {
|
||||
while (OPENSSL_isspace((unsigned char)*tmpend)) {
|
||||
tmpend--;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
/* crypto/conf/conf_def.h */
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
@@ -56,8 +55,9 @@
|
||||
* [including the GNU Public Licence.]
|
||||
*/
|
||||
|
||||
/* THIS FILE WAS AUTOMAGICALLY GENERATED!
|
||||
Please modify and use keysets.pl to regenerate it. */
|
||||
// This file was historically generated by keysets.pl in OpenSSL.
|
||||
//
|
||||
// TODO(davidben): Replace it with something more readable.
|
||||
|
||||
#define CONF_NUMBER 1
|
||||
#define CONF_UPPER 2
|
||||
@@ -67,9 +67,7 @@
|
||||
#define CONF_WS 16
|
||||
#define CONF_ESC 32
|
||||
#define CONF_QUOTE 64
|
||||
#define CONF_DQUOTE 1024
|
||||
#define CONF_COMMENT 128
|
||||
#define CONF_FCOMMENT 2048
|
||||
#define CONF_EOF 8
|
||||
#define CONF_HIGHBIT 4096
|
||||
#define CONF_ALPHA (CONF_UPPER|CONF_LOWER)
|
||||
@@ -79,7 +77,6 @@
|
||||
|
||||
#define KEYTYPES(c) CONF_type_default
|
||||
#define IS_COMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_COMMENT)
|
||||
#define IS_FCOMMENT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_FCOMMENT)
|
||||
#define IS_EOF(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_EOF)
|
||||
#define IS_ESC(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_ESC)
|
||||
#define IS_NUMBER(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_NUMBER)
|
||||
@@ -88,8 +85,6 @@
|
||||
#define IS_ALPHA_NUMERIC_PUNCT(c,a) \
|
||||
(KEYTYPES(c)[(a)&0xff]&CONF_ALPHA_NUMERIC_PUNCT)
|
||||
#define IS_QUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_QUOTE)
|
||||
#define IS_DQUOTE(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_DQUOTE)
|
||||
#define IS_HIGHBIT(c,a) (KEYTYPES(c)[(a)&0xff]&CONF_HIGHBIT)
|
||||
|
||||
static const unsigned short CONF_type_default[256]={
|
||||
0x0008,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
|
||||
|
||||
@@ -17,11 +17,19 @@
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#include "../lhash/internal.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
DEFINE_LHASH_OF(CONF_VALUE)
|
||||
|
||||
struct conf_st {
|
||||
LHASH_OF(CONF_VALUE) *data;
|
||||
};
|
||||
|
||||
// CONF_VALUE_new returns a freshly allocated and zeroed |CONF_VALUE|.
|
||||
CONF_VALUE *CONF_VALUE_new(void);
|
||||
|
||||
|
||||
@@ -23,8 +23,6 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
static int has_hw_feature(const char *name) {
|
||||
int value;
|
||||
size_t len = sizeof(value);
|
||||
@@ -65,6 +63,9 @@ void OPENSSL_cpuid_setup(void) {
|
||||
// available in macOS 12. For compatibility with macOS 11, we also support
|
||||
// the old names. The old names don't have values for features like FEAT_AES,
|
||||
// so instead we detect them statically above.
|
||||
//
|
||||
// If querying new sysctls, update the Chromium sandbox definition. See
|
||||
// https://crrev.com/c/4415225.
|
||||
if (has_hw_feature("hw.optional.arm.FEAT_SHA512") ||
|
||||
has_hw_feature("hw.optional.armv8_2_sha512")) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
uint32_t hwcap;
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
unsigned long hwcap = getauxval(AT_HWCAP);
|
||||
|
||||
|
||||
+23
-24
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2022, Google Inc.
|
||||
/* Copyright (c) 2022, Robert Nagy <robert.nagy@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@@ -12,51 +12,50 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include "internal.h"
|
||||
#include <CCryptoBoringSSL_cpu.h>
|
||||
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_FREEBSD) && \
|
||||
#if defined(OPENSSL_AARCH64) && defined(OPENSSL_OPENBSD) && \
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
|
||||
#include <sys/sysctl.h>
|
||||
#include <machine/cpu.h>
|
||||
#include <machine/armreg.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
#include "internal.h"
|
||||
|
||||
// ID_AA64ISAR0_*_VAL are defined starting FreeBSD 13.0. When FreeBSD
|
||||
// 12.x is out of support, these compatibility macros can be removed.
|
||||
|
||||
#ifndef ID_AA64ISAR0_AES_VAL
|
||||
#define ID_AA64ISAR0_AES_VAL ID_AA64ISAR0_AES
|
||||
#endif
|
||||
#ifndef ID_AA64ISAR0_SHA1_VAL
|
||||
#define ID_AA64ISAR0_SHA1_VAL ID_AA64ISAR0_SHA1
|
||||
#endif
|
||||
#ifndef ID_AA64ISAR0_SHA2_VAL
|
||||
#define ID_AA64ISAR0_SHA2_VAL ID_AA64ISAR0_SHA2
|
||||
#endif
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
uint64_t id_aa64isar0 = READ_SPECIALREG(id_aa64isar0_el1);
|
||||
int isar0_mib[] = { CTL_MACHDEP, CPU_ID_AA64ISAR0 };
|
||||
uint64_t cpu_id = 0;
|
||||
size_t len = sizeof(cpu_id);
|
||||
|
||||
if (sysctl(isar0_mib, 2, &cpu_id, &len, NULL, 0) < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
if (ID_AA64ISAR0_AES_VAL(id_aa64isar0) >= ID_AA64ISAR0_AES_BASE) {
|
||||
if (ID_AA64ISAR0_AES(cpu_id) >= ID_AA64ISAR0_AES_BASE) {
|
||||
OPENSSL_armcap_P |= ARMV8_AES;
|
||||
}
|
||||
if (ID_AA64ISAR0_AES_VAL(id_aa64isar0) >= ID_AA64ISAR0_AES_PMULL) {
|
||||
|
||||
if (ID_AA64ISAR0_AES(cpu_id) >= ID_AA64ISAR0_AES_PMULL) {
|
||||
OPENSSL_armcap_P |= ARMV8_PMULL;
|
||||
}
|
||||
if (ID_AA64ISAR0_SHA1_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA1_BASE) {
|
||||
|
||||
if (ID_AA64ISAR0_SHA1(cpu_id) >= ID_AA64ISAR0_SHA1_BASE) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA1;
|
||||
}
|
||||
if (ID_AA64ISAR0_SHA2_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA2_BASE) {
|
||||
|
||||
if (ID_AA64ISAR0_SHA2(cpu_id) >= ID_AA64ISAR0_SHA2_BASE) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA256;
|
||||
}
|
||||
if (ID_AA64ISAR0_SHA2_VAL(id_aa64isar0) >= ID_AA64ISAR0_SHA2_512) {
|
||||
|
||||
if (ID_AA64ISAR0_SHA2(cpu_id) >= ID_AA64ISAR0_SHA2_512) {
|
||||
OPENSSL_armcap_P |= ARMV8_SHA512;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_FREEBSD && !OPENSSL_STATIC_ARMCAP
|
||||
#endif // OPENSSL_AARCH64 && OPENSSL_OPENBSD && !OPENSSL_STATIC_ARMCAP
|
||||
@@ -0,0 +1,93 @@
|
||||
/* Copyright (c) 2023, 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 "internal.h"
|
||||
|
||||
// While Arm system registers are normally not available to userspace, FreeBSD
|
||||
// expects userspace to simply read them. It traps the reads and fills in CPU
|
||||
// capabilities.
|
||||
#if defined(OPENSSL_AARCH64) && !defined(OPENSSL_STATIC_ARMCAP) && \
|
||||
(defined(ANDROID_BAREMETAL) || defined(OPENSSL_FREEBSD))
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
#define ID_AA64PFR0_EL1_ADVSIMD 5
|
||||
|
||||
#define ID_AA64ISAR0_EL1_AES 1
|
||||
#define ID_AA64ISAR0_EL1_SHA1 2
|
||||
#define ID_AA64ISAR0_EL1_SHA2 3
|
||||
|
||||
#define NBITS_ID_FIELD 4
|
||||
|
||||
#define READ_SYSREG(name) \
|
||||
({ \
|
||||
uint64_t _r; \
|
||||
__asm__("mrs %0, " name : "=r"(_r)); \
|
||||
_r; \
|
||||
})
|
||||
|
||||
static unsigned get_id_field(uint64_t reg, unsigned field) {
|
||||
return (reg >> (field * NBITS_ID_FIELD)) & ((1 << NBITS_ID_FIELD) - 1);
|
||||
}
|
||||
|
||||
static int get_signed_id_field(uint64_t reg, unsigned field) {
|
||||
unsigned value = get_id_field(reg, field);
|
||||
if (value & (1 << (NBITS_ID_FIELD - 1))) {
|
||||
return (int)(value | (UINT64_MAX << NBITS_ID_FIELD));
|
||||
} else {
|
||||
return (int)value;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t read_armcap(void) {
|
||||
uint32_t armcap = ARMV7_NEON;
|
||||
|
||||
uint64_t id_aa64pfr0_el1 = READ_SYSREG("id_aa64pfr0_el1");
|
||||
|
||||
if (get_signed_id_field(id_aa64pfr0_el1, ID_AA64PFR0_EL1_ADVSIMD) < 0) {
|
||||
// If AdvSIMD ("NEON") is missing, don't report other features either.
|
||||
// This matches OpenSSL.
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t id_aa64isar0_el1 = READ_SYSREG("id_aa64isar0_el1");
|
||||
|
||||
unsigned aes = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_AES);
|
||||
if (aes > 0) {
|
||||
armcap |= ARMV8_AES;
|
||||
}
|
||||
if (aes > 1) {
|
||||
armcap |= ARMV8_PMULL;
|
||||
}
|
||||
|
||||
unsigned sha1 = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_SHA1);
|
||||
if (sha1 > 0) {
|
||||
armcap |= ARMV8_SHA1;
|
||||
}
|
||||
|
||||
unsigned sha2 = get_id_field(id_aa64isar0_el1, ID_AA64ISAR0_EL1_SHA2);
|
||||
if (sha2 > 0) {
|
||||
armcap |= ARMV8_SHA256;
|
||||
}
|
||||
if (sha2 > 1) {
|
||||
armcap |= ARMV8_SHA512;
|
||||
}
|
||||
|
||||
return armcap;
|
||||
}
|
||||
|
||||
void OPENSSL_cpuid_setup(void) { OPENSSL_armcap_P |= read_armcap(); }
|
||||
|
||||
#endif // OPENSSL_AARCH64 && !OPENSSL_STATIC_ARMCAP &&
|
||||
// (ANDROID_BAREMETAL || OPENSSL_FREEBSD)
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.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;
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
int CRYPTO_is_NEON_capable_at_runtime(void) {
|
||||
return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
#include <CCryptoBoringSSL_mem.h>
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
unsigned long hwcap = 0, hwcap2 = 0;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
!defined(OPENSSL_STATIC_ARMCAP)
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/auxv.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -26,13 +27,6 @@
|
||||
|
||||
#include "cpu_arm_linux.h"
|
||||
|
||||
#define AT_HWCAP 16
|
||||
#define AT_HWCAP2 26
|
||||
|
||||
// |getauxval| is not available on Android until API level 20. Link it as a weak
|
||||
// symbol and use other methods as fallback.
|
||||
unsigned long getauxval(unsigned long type) __attribute__((weak));
|
||||
|
||||
static int open_eintr(const char *path, int flags) {
|
||||
int ret;
|
||||
do {
|
||||
@@ -49,21 +43,6 @@ static ssize_t read_eintr(int fd, void *out, size_t len) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
|
||||
// file, it returns zero.
|
||||
static int read_full(int fd, void *out, size_t len) {
|
||||
char *outp = out;
|
||||
while (len > 0) {
|
||||
ssize_t ret = read_eintr(fd, outp, len);
|
||||
if (ret <= 0) {
|
||||
return 0;
|
||||
}
|
||||
outp += ret;
|
||||
len -= ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// read_file opens |path| and reads until end-of-file. On success, it returns
|
||||
// one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
|
||||
// contents. Otherwise, it returns zero.
|
||||
@@ -116,41 +95,12 @@ err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
// getauxval_proc behaves like |getauxval| but reads from /proc/self/auxv.
|
||||
static unsigned long getauxval_proc(unsigned long type) {
|
||||
int fd = open_eintr("/proc/self/auxv", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct {
|
||||
unsigned long tag;
|
||||
unsigned long value;
|
||||
} entry;
|
||||
|
||||
for (;;) {
|
||||
if (!read_full(fd, &entry, sizeof(entry)) ||
|
||||
(entry.tag == 0 && entry.value == 0)) {
|
||||
break;
|
||||
}
|
||||
if (entry.tag == type) {
|
||||
close(fd);
|
||||
return entry.value;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern uint32_t OPENSSL_armcap_P;
|
||||
|
||||
static int g_has_broken_neon, g_needs_hwcap2_workaround;
|
||||
static int g_needs_hwcap2_workaround;
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
// We ignore the return value of |read_file| and proceed with an empty
|
||||
// /proc/cpuinfo on error. If |getauxval| works, we will still detect
|
||||
// capabilities. There may be a false positive due to
|
||||
// |crypto_cpuinfo_has_broken_neon|, but this is now rare.
|
||||
// capabilities.
|
||||
char *cpuinfo_data = NULL;
|
||||
size_t cpuinfo_len = 0;
|
||||
read_file(&cpuinfo_data, &cpuinfo_len, "/proc/cpuinfo");
|
||||
@@ -158,37 +108,8 @@ void OPENSSL_cpuid_setup(void) {
|
||||
cpuinfo.data = cpuinfo_data;
|
||||
cpuinfo.len = cpuinfo_len;
|
||||
|
||||
// |getauxval| is not available on Android until API level 20. If it is
|
||||
// unavailable, read from /proc/self/auxv as a fallback. This is unreadable
|
||||
// on some versions of Android, so further fall back to /proc/cpuinfo.
|
||||
//
|
||||
// See
|
||||
// https://android.googlesource.com/platform/ndk/+/882ac8f3392858991a0e1af33b4b7387ec856bd2
|
||||
// and b/13679666 (Google-internal) for details.
|
||||
unsigned long hwcap = 0;
|
||||
if (getauxval != NULL) {
|
||||
hwcap = getauxval(AT_HWCAP);
|
||||
}
|
||||
if (hwcap == 0) {
|
||||
hwcap = getauxval_proc(AT_HWCAP);
|
||||
}
|
||||
if (hwcap == 0) {
|
||||
hwcap = crypto_get_arm_hwcap_from_cpuinfo(&cpuinfo);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Matching OpenSSL, only report other features if NEON is present.
|
||||
unsigned long hwcap = getauxval(AT_HWCAP);
|
||||
if (hwcap & HWCAP_NEON) {
|
||||
OPENSSL_armcap_P |= ARMV7_NEON;
|
||||
|
||||
@@ -197,10 +118,7 @@ void OPENSSL_cpuid_setup(void) {
|
||||
// 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);
|
||||
}
|
||||
unsigned long hwcap2 = getauxval(AT_HWCAP2);
|
||||
if (hwcap2 == 0) {
|
||||
hwcap2 = crypto_get_arm_hwcap2_from_cpuinfo(&cpuinfo);
|
||||
g_needs_hwcap2_workaround = hwcap2 != 0;
|
||||
@@ -223,7 +141,7 @@ void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_free(cpuinfo_data);
|
||||
}
|
||||
|
||||
int CRYPTO_has_broken_NEON(void) { return g_has_broken_neon; }
|
||||
int CRYPTO_has_broken_NEON(void) { return 0; }
|
||||
|
||||
int CRYPTO_needs_hwcap2_workaround(void) { return g_needs_hwcap2_workaround; }
|
||||
|
||||
|
||||
@@ -118,13 +118,6 @@ static int extract_cpuinfo_field(STRING_PIECE *out, const STRING_PIECE *in,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cpuinfo_field_equals(const STRING_PIECE *cpuinfo, const char *field,
|
||||
const char *value) {
|
||||
STRING_PIECE extracted;
|
||||
return extract_cpuinfo_field(&extracted, cpuinfo, field) &&
|
||||
STRING_PIECE_equals(&extracted, value);
|
||||
}
|
||||
|
||||
// has_list_item treats |list| as a space-separated list of items and returns
|
||||
// one if |item| is contained in |list| and zero otherwise.
|
||||
static int has_list_item(const STRING_PIECE *list, const char *item) {
|
||||
@@ -137,27 +130,6 @@ static int has_list_item(const STRING_PIECE *list, const char *item) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// crypto_get_arm_hwcap_from_cpuinfo returns an equivalent ARM |AT_HWCAP| value
|
||||
// from |cpuinfo|.
|
||||
static unsigned long crypto_get_arm_hwcap_from_cpuinfo(
|
||||
const STRING_PIECE *cpuinfo) {
|
||||
if (cpuinfo_field_equals(cpuinfo, "CPU architecture", "8")) {
|
||||
// This is a 32-bit ARM binary running on a 64-bit kernel. NEON is always
|
||||
// available on ARMv8. Linux omits required features, so reading the
|
||||
// "Features" line does not work. (For simplicity, use strict equality. We
|
||||
// assume everything running on future ARM architectures will have a
|
||||
// working |getauxval|.)
|
||||
return HWCAP_NEON;
|
||||
}
|
||||
|
||||
STRING_PIECE features;
|
||||
if (extract_cpuinfo_field(&features, cpuinfo, "Features") &&
|
||||
has_list_item(&features, "neon")) {
|
||||
return HWCAP_NEON;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// crypto_get_arm_hwcap2_from_cpuinfo returns an equivalent ARM |AT_HWCAP2|
|
||||
// value from |cpuinfo|.
|
||||
static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
|
||||
@@ -183,16 +155,6 @@ static unsigned long crypto_get_arm_hwcap2_from_cpuinfo(
|
||||
return ret;
|
||||
}
|
||||
|
||||
// crypto_cpuinfo_has_broken_neon returns one if |cpuinfo| matches a CPU known
|
||||
// to have broken NEON unit and zero otherwise. See https://crbug.com/341598.
|
||||
static int crypto_cpuinfo_has_broken_neon(const STRING_PIECE *cpuinfo) {
|
||||
return cpuinfo_field_equals(cpuinfo, "CPU implementer", "0x51") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU architecture", "7") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU variant", "0x1") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU part", "0x04d") &&
|
||||
cpuinfo_field_equals(cpuinfo, "CPU revision", "0");
|
||||
}
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
|
||||
@@ -14,18 +14,21 @@
|
||||
|
||||
#include <CCryptoBoringSSL_crypto.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "fipsmodule/rand/fork_detect.h"
|
||||
#include "fipsmodule/rand/internal.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static_assert(sizeof(ossl_ssize_t) == sizeof(size_t),
|
||||
"ossl_ssize_t should be the same size as size_t");
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_STATIC_ARMCAP) && \
|
||||
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
|
||||
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64) || \
|
||||
defined(OPENSSL_PPC64LE))
|
||||
// x86, x86_64, the ARMs and ppc64le need to record the result of a
|
||||
// cpuid/getauxval call for the asm to work correctly, unless compiled without
|
||||
// asm code.
|
||||
(defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
|
||||
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
|
||||
// x86, x86_64, and the ARMs need to record the result of a cpuid/getauxval call
|
||||
// for the asm to work correctly, unless compiled without asm code.
|
||||
#define NEED_CPUID
|
||||
|
||||
#else
|
||||
@@ -36,8 +39,7 @@
|
||||
#define BORINGSSL_NO_STATIC_INITIALIZER
|
||||
#endif
|
||||
|
||||
#endif // !NO_ASM && !STATIC_ARMCAP &&
|
||||
// (X86 || X86_64 || ARM || AARCH64 || PPC64LE)
|
||||
#endif // !NO_ASM && !STATIC_ARMCAP && (X86 || X86_64 || ARM || AARCH64)
|
||||
|
||||
|
||||
// Our assembly does not use the GOT to reference symbols, which means
|
||||
@@ -76,10 +78,6 @@ HIDDEN uint8_t BORINGSSL_function_hit[7] = {0};
|
||||
// This value must be explicitly initialized to zero. See similar comment above.
|
||||
HIDDEN uint32_t OPENSSL_ia32cap_P[4] = {0};
|
||||
|
||||
#elif defined(OPENSSL_PPC64LE)
|
||||
|
||||
HIDDEN unsigned long OPENSSL_ppc64le_hwcap2 = 0;
|
||||
|
||||
#elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
|
||||
|
||||
#include <CCryptoBoringSSL_arm_arch.h>
|
||||
|
||||
@@ -19,17 +19,9 @@
|
||||
* domain licensed but the standard ISC license is included above to keep
|
||||
* licensing simple. */
|
||||
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM)
|
||||
#define OPENSSL_NO_ASM
|
||||
#endif
|
||||
#endif
|
||||
#include <CCryptoBoringSSL_asm_base.h>
|
||||
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(__arm__) && !defined(__APPLE__)
|
||||
|
||||
#if defined(BORINGSSL_PREFIX)
|
||||
#include <CCryptoBoringSSL_boringssl_prefix_symbols_asm.h>
|
||||
#endif
|
||||
#if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_ARM) && defined(__ELF__)
|
||||
|
||||
.fpu neon
|
||||
.text
|
||||
@@ -2131,11 +2123,7 @@ mov sp,r12
|
||||
vpop {q4,q5,q6,q7}
|
||||
bx lr
|
||||
|
||||
#endif /* !OPENSSL_NO_ASM && __arm__ && !__APPLE__ */
|
||||
|
||||
#if defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
#endif
|
||||
#endif /* !OPENSSL_NO_ASM && OPENSSL_ARM && __ELF__ */
|
||||
#endif // defined(__arm__) && defined(__linux__)
|
||||
#if defined(__linux__) && defined(__ELF__)
|
||||
.section .note.GNU-stack,"",%progbits
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
//
|
||||
// The field functions are shared by Ed25519 and X25519 where possible.
|
||||
|
||||
#include <CCryptoBoringSSL_curve25519.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -31,19 +29,16 @@
|
||||
#include "internal.h"
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
// Various pre-computed constants.
|
||||
#include "./curve25519_tables.h"
|
||||
|
||||
#if defined(OPENSSL_NO_ASM)
|
||||
#define FIAT_25519_NO_ASM
|
||||
#endif
|
||||
|
||||
#if defined(BORINGSSL_CURVE25519_64BIT)
|
||||
#if defined(BORINGSSL_HAS_UINT128)
|
||||
#include "../../third_party/fiat/curve25519_64.h"
|
||||
#elif defined(OPENSSL_64_BIT)
|
||||
#include "../../third_party/fiat/curve25519_64_msvc.h"
|
||||
#else
|
||||
#include "../../third_party/fiat/curve25519_32.h"
|
||||
#endif // BORINGSSL_CURVE25519_64BIT
|
||||
#endif
|
||||
|
||||
|
||||
// Low-level intrinsic operations
|
||||
@@ -68,7 +63,7 @@ static uint64_t load_4(const uint8_t *in) {
|
||||
|
||||
// Field operations.
|
||||
|
||||
#if defined(BORINGSSL_CURVE25519_64BIT)
|
||||
#if defined(OPENSSL_64_BIT)
|
||||
|
||||
typedef uint64_t fe_limb_t;
|
||||
#define FE_NUM_LIMBS 5
|
||||
@@ -148,7 +143,7 @@ typedef uint32_t fe_limb_t;
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif // BORINGSSL_CURVE25519_64BIT
|
||||
#endif // OPENSSL_64_BIT
|
||||
|
||||
static_assert(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
|
||||
"fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
|
||||
@@ -317,11 +312,6 @@ static void fe_copy_lt(fe_loose *h, const fe *f) {
|
||||
static_assert(sizeof(fe_loose) == sizeof(fe), "fe and fe_loose mismatch");
|
||||
OPENSSL_memmove(h, f, sizeof(fe));
|
||||
}
|
||||
#if !defined(OPENSSL_SMALL)
|
||||
static void fe_copy_ll(fe_loose *h, const fe_loose *f) {
|
||||
OPENSSL_memmove(h, f, sizeof(fe_loose));
|
||||
}
|
||||
#endif // !defined(OPENSSL_SMALL)
|
||||
|
||||
static void fe_loose_invert(fe *out, const fe_loose *z) {
|
||||
fe t0;
|
||||
@@ -700,16 +690,6 @@ void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
|
||||
fe_add(&r->T, &trZ, &trT);
|
||||
}
|
||||
|
||||
static uint8_t equal(signed char b, signed char c) {
|
||||
uint8_t ub = b;
|
||||
uint8_t uc = c;
|
||||
uint8_t x = ub ^ uc; // 0: yes; 1..255: no
|
||||
uint32_t y = x; // 0: yes; 1..255: no
|
||||
y -= 1; // 4294967295: yes; 0..254: no
|
||||
y >>= 31; // 1: yes; 0: no
|
||||
return y;
|
||||
}
|
||||
|
||||
static void cmov(ge_precomp *t, const ge_precomp *u, uint8_t b) {
|
||||
fe_cmov(&t->yplusx, &u->yplusx, b);
|
||||
fe_cmov(&t->yminusx, &u->yminusx, b);
|
||||
@@ -756,7 +736,7 @@ void x25519_ge_scalarmult_small_precomp(
|
||||
ge_precomp_0(&e);
|
||||
|
||||
for (j = 1; j < 16; j++) {
|
||||
cmov(&e, &multiples[j-1], equal(index, j));
|
||||
cmov(&e, &multiples[j-1], 1&constant_time_eq_w(index, j));
|
||||
}
|
||||
|
||||
ge_cached cached;
|
||||
@@ -778,35 +758,36 @@ void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]) {
|
||||
|
||||
#else
|
||||
|
||||
static uint8_t negative(signed char b) {
|
||||
uint32_t x = b;
|
||||
x >>= 31; // 1: yes; 0: no
|
||||
return x;
|
||||
}
|
||||
static void table_select(ge_precomp *t, const int pos, const signed char b) {
|
||||
uint8_t bnegative = constant_time_msb_w(b);
|
||||
uint8_t babs = b - ((bnegative & b) << 1);
|
||||
|
||||
uint8_t t_bytes[3][32] = {
|
||||
{constant_time_is_zero_w(b) & 1}, {constant_time_is_zero_w(b) & 1}, {0}};
|
||||
#if defined(__clang__) // materialize for vectorization, 6% speedup
|
||||
__asm__("" : "+m" (t_bytes) : /*no inputs*/);
|
||||
#endif
|
||||
static_assert(sizeof(t_bytes) == sizeof(k25519Precomp[pos][0]), "");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
constant_time_conditional_memxor(t_bytes, k25519Precomp[pos][i],
|
||||
sizeof(t_bytes),
|
||||
constant_time_eq_w(babs, 1 + i));
|
||||
}
|
||||
|
||||
fe yplusx, yminusx, xy2d;
|
||||
fe_frombytes_strict(&yplusx, t_bytes[0]);
|
||||
fe_frombytes_strict(&yminusx, t_bytes[1]);
|
||||
fe_frombytes_strict(&xy2d, t_bytes[2]);
|
||||
|
||||
fe_copy_lt(&t->yplusx, &yplusx);
|
||||
fe_copy_lt(&t->yminusx, &yminusx);
|
||||
fe_copy_lt(&t->xy2d, &xy2d);
|
||||
|
||||
static void table_select(ge_precomp *t, int pos, signed char b) {
|
||||
ge_precomp minust;
|
||||
uint8_t bnegative = negative(b);
|
||||
uint8_t babs = b - ((uint8_t)((-bnegative) & b) << 1);
|
||||
|
||||
ge_precomp_0(t);
|
||||
cmov(t, &k25519Precomp[pos][0], equal(babs, 1));
|
||||
cmov(t, &k25519Precomp[pos][1], equal(babs, 2));
|
||||
cmov(t, &k25519Precomp[pos][2], equal(babs, 3));
|
||||
cmov(t, &k25519Precomp[pos][3], equal(babs, 4));
|
||||
cmov(t, &k25519Precomp[pos][4], equal(babs, 5));
|
||||
cmov(t, &k25519Precomp[pos][5], equal(babs, 6));
|
||||
cmov(t, &k25519Precomp[pos][6], equal(babs, 7));
|
||||
cmov(t, &k25519Precomp[pos][7], equal(babs, 8));
|
||||
fe_copy_ll(&minust.yplusx, &t->yminusx);
|
||||
fe_copy_ll(&minust.yminusx, &t->yplusx);
|
||||
|
||||
// NOTE: the input table is canonical, but types don't encode it
|
||||
fe tmp;
|
||||
fe_carry(&tmp, &t->xy2d);
|
||||
fe_neg(&minust.xy2d, &tmp);
|
||||
|
||||
cmov(t, &minust, bnegative);
|
||||
fe_copy_lt(&minust.yplusx, &yminusx);
|
||||
fe_copy_lt(&minust.yminusx, &yplusx);
|
||||
fe_neg(&minust.xy2d, &xy2d);
|
||||
cmov(t, &minust, bnegative>>7);
|
||||
}
|
||||
|
||||
// h = a * B
|
||||
@@ -816,6 +797,18 @@ 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[32]) {
|
||||
#if defined(BORINGSSL_FE25519_ADX)
|
||||
if (CRYPTO_is_BMI1_capable() && CRYPTO_is_BMI2_capable() &&
|
||||
CRYPTO_is_ADX_capable()) {
|
||||
uint8_t t[4][32];
|
||||
x25519_ge_scalarmult_base_adx(t, a);
|
||||
fiat_25519_from_bytes(h->X.v, t[0]);
|
||||
fiat_25519_from_bytes(h->Y.v, t[1]);
|
||||
fiat_25519_from_bytes(h->Z.v, t[2]);
|
||||
fiat_25519_from_bytes(h->T.v, t[3]);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
signed char e[64];
|
||||
signed char carry;
|
||||
ge_p1p1 r;
|
||||
@@ -918,7 +911,7 @@ void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A) {
|
||||
ge_cached selected;
|
||||
ge_cached_0(&selected);
|
||||
for (j = 0; j < 16; j++) {
|
||||
cmov_cached(&selected, &Ai[j], equal(j, index));
|
||||
cmov_cached(&selected, &Ai[j], 1&constant_time_eq_w(index, j));
|
||||
}
|
||||
|
||||
x25519_ge_add(&t, &u, &selected);
|
||||
@@ -2085,6 +2078,12 @@ static void x25519_scalar_mult(uint8_t out[32], const uint8_t scalar[32],
|
||||
x25519_NEON(out, scalar, point);
|
||||
return;
|
||||
}
|
||||
#elif defined(BORINGSSL_FE25519_ADX)
|
||||
if (CRYPTO_is_BMI1_capable() && CRYPTO_is_BMI2_capable() &&
|
||||
CRYPTO_is_ADX_capable()) {
|
||||
x25519_scalar_mult_adx(out, scalar, point);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
x25519_scalar_mult_generic(out, scalar, point);
|
||||
@@ -2118,7 +2117,8 @@ int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
|
||||
static const uint8_t kZeros[32] = {0};
|
||||
x25519_scalar_mult(out_shared_key, private_key, peer_public_value);
|
||||
// The all-zero output results when the input is a point of small order.
|
||||
return CRYPTO_memcmp(kZeros, out_shared_key, 32) != 0;
|
||||
return constant_time_declassify_int(
|
||||
CRYPTO_memcmp(kZeros, out_shared_key, 32)) != 0;
|
||||
}
|
||||
|
||||
void X25519_public_from_private(uint8_t out_public_value[32],
|
||||
@@ -2149,4 +2149,5 @@ void X25519_public_from_private(uint8_t out_public_value[32],
|
||||
fe_loose_invert(&zminusy_inv, &zminusy);
|
||||
fe_mul_tlt(&zminusy_inv, &zplusy, &zminusy_inv);
|
||||
fe_tobytes(out_public_value, &zminusy_inv);
|
||||
CONSTTIME_DECLASSIFY(out_public_value, 32);
|
||||
}
|
||||
+3
-23
@@ -1,4 +1,4 @@
|
||||
/* Copyright (c) 2016, Google Inc.
|
||||
/* Copyright (c) 2023, 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
|
||||
@@ -12,27 +12,7 @@
|
||||
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
|
||||
#if defined(OPENSSL_PPC64LE)
|
||||
|
||||
#include <sys/auxv.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
#if !defined(PPC_FEATURE2_HAS_VCRYPTO)
|
||||
// PPC_FEATURE2_HAS_VCRYPTO was taken from section 4.1.2.3 of the “OpenPOWER
|
||||
// ABI for Linux Supplement”.
|
||||
#define PPC_FEATURE2_HAS_VCRYPTO 0x02000000
|
||||
#if defined(BORINGSSL_FE25519_ADX)
|
||||
#include "../../third_party/fiat/curve25519_64_adx.h"
|
||||
#endif
|
||||
|
||||
void OPENSSL_cpuid_setup(void) {
|
||||
OPENSSL_ppc64le_hwcap2 = getauxval(AT_HWCAP2);
|
||||
}
|
||||
|
||||
int CRYPTO_is_PPC64LE_vcrypto_capable(void) {
|
||||
return (OPENSSL_ppc64le_hwcap2 & PPC_FEATURE2_HAS_VCRYPTO) != 0;
|
||||
}
|
||||
|
||||
#endif // OPENSSL_PPC64LE
|
||||
File diff suppressed because it is too large.
Load diff
@@ -15,14 +15,13 @@
|
||||
#ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H
|
||||
#define OPENSSL_HEADER_CURVE25519_INTERNAL_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <CCryptoBoringSSL_base.h>
|
||||
#include <CCryptoBoringSSL_curve25519.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE)
|
||||
#define BORINGSSL_X25519_NEON
|
||||
@@ -32,11 +31,28 @@ void x25519_NEON(uint8_t out[32], const uint8_t scalar[32],
|
||||
const uint8_t point[32]);
|
||||
#endif
|
||||
|
||||
#if defined(BORINGSSL_HAS_UINT128)
|
||||
#define BORINGSSL_CURVE25519_64BIT
|
||||
#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_SMALL) && \
|
||||
defined(__GNUC__) && defined(__x86_64__)
|
||||
#define BORINGSSL_FE25519_ADX
|
||||
|
||||
// fiat_curve25519_adx_mul is defined in
|
||||
// third_party/fiat/asm/fiat_curve25519_adx_mul.S
|
||||
void __attribute__((sysv_abi))
|
||||
fiat_curve25519_adx_mul(uint64_t out[4], const uint64_t in1[4],
|
||||
const uint64_t in2[4]);
|
||||
|
||||
// fiat_curve25519_adx_square is defined in
|
||||
// third_party/fiat/asm/fiat_curve25519_adx_square.S
|
||||
void __attribute__((sysv_abi))
|
||||
fiat_curve25519_adx_square(uint64_t out[4], const uint64_t in[4]);
|
||||
|
||||
// x25519_scalar_mult_adx is defined in third_party/fiat/curve25519_64_adx.h
|
||||
void x25519_scalar_mult_adx(uint8_t out[32], const uint8_t scalar[32],
|
||||
const uint8_t point[32]);
|
||||
void x25519_ge_scalarmult_base_adx(uint8_t h[4][32], const uint8_t a[32]);
|
||||
#endif
|
||||
|
||||
#if defined(BORINGSSL_CURVE25519_64BIT)
|
||||
#if defined(OPENSSL_64_BIT)
|
||||
// fe means field element. Here the field is \Z/(2^255-19). An element t,
|
||||
// entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153
|
||||
// t[3]+2^204 t[4].
|
||||
@@ -139,6 +155,8 @@ struct spake2_ctx_st {
|
||||
};
|
||||
|
||||
|
||||
extern const uint8_t k25519Precomp[32][8][3][32];
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} // extern C
|
||||
#endif
|
||||
|
||||
@@ -63,231 +63,231 @@
|
||||
|
||||
static const uint32_t des_skb[8][64] = {
|
||||
{ // for C bits (numbered as per FIPS 46) 1 2 3 4 5 6
|
||||
0x00000000L, 0x00000010L, 0x20000000L, 0x20000010L, 0x00010000L,
|
||||
0x00010010L, 0x20010000L, 0x20010010L, 0x00000800L, 0x00000810L,
|
||||
0x20000800L, 0x20000810L, 0x00010800L, 0x00010810L, 0x20010800L,
|
||||
0x20010810L, 0x00000020L, 0x00000030L, 0x20000020L, 0x20000030L,
|
||||
0x00010020L, 0x00010030L, 0x20010020L, 0x20010030L, 0x00000820L,
|
||||
0x00000830L, 0x20000820L, 0x20000830L, 0x00010820L, 0x00010830L,
|
||||
0x20010820L, 0x20010830L, 0x00080000L, 0x00080010L, 0x20080000L,
|
||||
0x20080010L, 0x00090000L, 0x00090010L, 0x20090000L, 0x20090010L,
|
||||
0x00080800L, 0x00080810L, 0x20080800L, 0x20080810L, 0x00090800L,
|
||||
0x00090810L, 0x20090800L, 0x20090810L, 0x00080020L, 0x00080030L,
|
||||
0x20080020L, 0x20080030L, 0x00090020L, 0x00090030L, 0x20090020L,
|
||||
0x20090030L, 0x00080820L, 0x00080830L, 0x20080820L, 0x20080830L,
|
||||
0x00090820L, 0x00090830L, 0x20090820L, 0x20090830L, },
|
||||
0x00000000, 0x00000010, 0x20000000, 0x20000010, 0x00010000,
|
||||
0x00010010, 0x20010000, 0x20010010, 0x00000800, 0x00000810,
|
||||
0x20000800, 0x20000810, 0x00010800, 0x00010810, 0x20010800,
|
||||
0x20010810, 0x00000020, 0x00000030, 0x20000020, 0x20000030,
|
||||
0x00010020, 0x00010030, 0x20010020, 0x20010030, 0x00000820,
|
||||
0x00000830, 0x20000820, 0x20000830, 0x00010820, 0x00010830,
|
||||
0x20010820, 0x20010830, 0x00080000, 0x00080010, 0x20080000,
|
||||
0x20080010, 0x00090000, 0x00090010, 0x20090000, 0x20090010,
|
||||
0x00080800, 0x00080810, 0x20080800, 0x20080810, 0x00090800,
|
||||
0x00090810, 0x20090800, 0x20090810, 0x00080020, 0x00080030,
|
||||
0x20080020, 0x20080030, 0x00090020, 0x00090030, 0x20090020,
|
||||
0x20090030, 0x00080820, 0x00080830, 0x20080820, 0x20080830,
|
||||
0x00090820, 0x00090830, 0x20090820, 0x20090830, },
|
||||
{ // for C bits (numbered as per FIPS 46) 7 8 10 11 12 13
|
||||
0x00000000L, 0x02000000L, 0x00002000L, 0x02002000L, 0x00200000L,
|
||||
0x02200000L, 0x00202000L, 0x02202000L, 0x00000004L, 0x02000004L,
|
||||
0x00002004L, 0x02002004L, 0x00200004L, 0x02200004L, 0x00202004L,
|
||||
0x02202004L, 0x00000400L, 0x02000400L, 0x00002400L, 0x02002400L,
|
||||
0x00200400L, 0x02200400L, 0x00202400L, 0x02202400L, 0x00000404L,
|
||||
0x02000404L, 0x00002404L, 0x02002404L, 0x00200404L, 0x02200404L,
|
||||
0x00202404L, 0x02202404L, 0x10000000L, 0x12000000L, 0x10002000L,
|
||||
0x12002000L, 0x10200000L, 0x12200000L, 0x10202000L, 0x12202000L,
|
||||
0x10000004L, 0x12000004L, 0x10002004L, 0x12002004L, 0x10200004L,
|
||||
0x12200004L, 0x10202004L, 0x12202004L, 0x10000400L, 0x12000400L,
|
||||
0x10002400L, 0x12002400L, 0x10200400L, 0x12200400L, 0x10202400L,
|
||||
0x12202400L, 0x10000404L, 0x12000404L, 0x10002404L, 0x12002404L,
|
||||
0x10200404L, 0x12200404L, 0x10202404L, 0x12202404L, },
|
||||
0x00000000, 0x02000000, 0x00002000, 0x02002000, 0x00200000,
|
||||
0x02200000, 0x00202000, 0x02202000, 0x00000004, 0x02000004,
|
||||
0x00002004, 0x02002004, 0x00200004, 0x02200004, 0x00202004,
|
||||
0x02202004, 0x00000400, 0x02000400, 0x00002400, 0x02002400,
|
||||
0x00200400, 0x02200400, 0x00202400, 0x02202400, 0x00000404,
|
||||
0x02000404, 0x00002404, 0x02002404, 0x00200404, 0x02200404,
|
||||
0x00202404, 0x02202404, 0x10000000, 0x12000000, 0x10002000,
|
||||
0x12002000, 0x10200000, 0x12200000, 0x10202000, 0x12202000,
|
||||
0x10000004, 0x12000004, 0x10002004, 0x12002004, 0x10200004,
|
||||
0x12200004, 0x10202004, 0x12202004, 0x10000400, 0x12000400,
|
||||
0x10002400, 0x12002400, 0x10200400, 0x12200400, 0x10202400,
|
||||
0x12202400, 0x10000404, 0x12000404, 0x10002404, 0x12002404,
|
||||
0x10200404, 0x12200404, 0x10202404, 0x12202404, },
|
||||
{ // for C bits (numbered as per FIPS 46) 14 15 16 17 19 20
|
||||
0x00000000L, 0x00000001L, 0x00040000L, 0x00040001L, 0x01000000L,
|
||||
0x01000001L, 0x01040000L, 0x01040001L, 0x00000002L, 0x00000003L,
|
||||
0x00040002L, 0x00040003L, 0x01000002L, 0x01000003L, 0x01040002L,
|
||||
0x01040003L, 0x00000200L, 0x00000201L, 0x00040200L, 0x00040201L,
|
||||
0x01000200L, 0x01000201L, 0x01040200L, 0x01040201L, 0x00000202L,
|
||||
0x00000203L, 0x00040202L, 0x00040203L, 0x01000202L, 0x01000203L,
|
||||
0x01040202L, 0x01040203L, 0x08000000L, 0x08000001L, 0x08040000L,
|
||||
0x08040001L, 0x09000000L, 0x09000001L, 0x09040000L, 0x09040001L,
|
||||
0x08000002L, 0x08000003L, 0x08040002L, 0x08040003L, 0x09000002L,
|
||||
0x09000003L, 0x09040002L, 0x09040003L, 0x08000200L, 0x08000201L,
|
||||
0x08040200L, 0x08040201L, 0x09000200L, 0x09000201L, 0x09040200L,
|
||||
0x09040201L, 0x08000202L, 0x08000203L, 0x08040202L, 0x08040203L,
|
||||
0x09000202L, 0x09000203L, 0x09040202L, 0x09040203L, },
|
||||
0x00000000, 0x00000001, 0x00040000, 0x00040001, 0x01000000,
|
||||
0x01000001, 0x01040000, 0x01040001, 0x00000002, 0x00000003,
|
||||
0x00040002, 0x00040003, 0x01000002, 0x01000003, 0x01040002,
|
||||
0x01040003, 0x00000200, 0x00000201, 0x00040200, 0x00040201,
|
||||
0x01000200, 0x01000201, 0x01040200, 0x01040201, 0x00000202,
|
||||
0x00000203, 0x00040202, 0x00040203, 0x01000202, 0x01000203,
|
||||
0x01040202, 0x01040203, 0x08000000, 0x08000001, 0x08040000,
|
||||
0x08040001, 0x09000000, 0x09000001, 0x09040000, 0x09040001,
|
||||
0x08000002, 0x08000003, 0x08040002, 0x08040003, 0x09000002,
|
||||
0x09000003, 0x09040002, 0x09040003, 0x08000200, 0x08000201,
|
||||
0x08040200, 0x08040201, 0x09000200, 0x09000201, 0x09040200,
|
||||
0x09040201, 0x08000202, 0x08000203, 0x08040202, 0x08040203,
|
||||
0x09000202, 0x09000203, 0x09040202, 0x09040203, },
|
||||
{ // for C bits (numbered as per FIPS 46) 21 23 24 26 27 28
|
||||
0x00000000L, 0x00100000L, 0x00000100L, 0x00100100L, 0x00000008L,
|
||||
0x00100008L, 0x00000108L, 0x00100108L, 0x00001000L, 0x00101000L,
|
||||
0x00001100L, 0x00101100L, 0x00001008L, 0x00101008L, 0x00001108L,
|
||||
0x00101108L, 0x04000000L, 0x04100000L, 0x04000100L, 0x04100100L,
|
||||
0x04000008L, 0x04100008L, 0x04000108L, 0x04100108L, 0x04001000L,
|
||||
0x04101000L, 0x04001100L, 0x04101100L, 0x04001008L, 0x04101008L,
|
||||
0x04001108L, 0x04101108L, 0x00020000L, 0x00120000L, 0x00020100L,
|
||||
0x00120100L, 0x00020008L, 0x00120008L, 0x00020108L, 0x00120108L,
|
||||
0x00021000L, 0x00121000L, 0x00021100L, 0x00121100L, 0x00021008L,
|
||||
0x00121008L, 0x00021108L, 0x00121108L, 0x04020000L, 0x04120000L,
|
||||
0x04020100L, 0x04120100L, 0x04020008L, 0x04120008L, 0x04020108L,
|
||||
0x04120108L, 0x04021000L, 0x04121000L, 0x04021100L, 0x04121100L,
|
||||
0x04021008L, 0x04121008L, 0x04021108L, 0x04121108L, },
|
||||
0x00000000, 0x00100000, 0x00000100, 0x00100100, 0x00000008,
|
||||
0x00100008, 0x00000108, 0x00100108, 0x00001000, 0x00101000,
|
||||
0x00001100, 0x00101100, 0x00001008, 0x00101008, 0x00001108,
|
||||
0x00101108, 0x04000000, 0x04100000, 0x04000100, 0x04100100,
|
||||
0x04000008, 0x04100008, 0x04000108, 0x04100108, 0x04001000,
|
||||
0x04101000, 0x04001100, 0x04101100, 0x04001008, 0x04101008,
|
||||
0x04001108, 0x04101108, 0x00020000, 0x00120000, 0x00020100,
|
||||
0x00120100, 0x00020008, 0x00120008, 0x00020108, 0x00120108,
|
||||
0x00021000, 0x00121000, 0x00021100, 0x00121100, 0x00021008,
|
||||
0x00121008, 0x00021108, 0x00121108, 0x04020000, 0x04120000,
|
||||
0x04020100, 0x04120100, 0x04020008, 0x04120008, 0x04020108,
|
||||
0x04120108, 0x04021000, 0x04121000, 0x04021100, 0x04121100,
|
||||
0x04021008, 0x04121008, 0x04021108, 0x04121108, },
|
||||
{ // for D bits (numbered as per FIPS 46) 1 2 3 4 5 6
|
||||
0x00000000L, 0x10000000L, 0x00010000L, 0x10010000L, 0x00000004L,
|
||||
0x10000004L, 0x00010004L, 0x10010004L, 0x20000000L, 0x30000000L,
|
||||
0x20010000L, 0x30010000L, 0x20000004L, 0x30000004L, 0x20010004L,
|
||||
0x30010004L, 0x00100000L, 0x10100000L, 0x00110000L, 0x10110000L,
|
||||
0x00100004L, 0x10100004L, 0x00110004L, 0x10110004L, 0x20100000L,
|
||||
0x30100000L, 0x20110000L, 0x30110000L, 0x20100004L, 0x30100004L,
|
||||
0x20110004L, 0x30110004L, 0x00001000L, 0x10001000L, 0x00011000L,
|
||||
0x10011000L, 0x00001004L, 0x10001004L, 0x00011004L, 0x10011004L,
|
||||
0x20001000L, 0x30001000L, 0x20011000L, 0x30011000L, 0x20001004L,
|
||||
0x30001004L, 0x20011004L, 0x30011004L, 0x00101000L, 0x10101000L,
|
||||
0x00111000L, 0x10111000L, 0x00101004L, 0x10101004L, 0x00111004L,
|
||||
0x10111004L, 0x20101000L, 0x30101000L, 0x20111000L, 0x30111000L,
|
||||
0x20101004L, 0x30101004L, 0x20111004L, 0x30111004L, },
|
||||
0x00000000, 0x10000000, 0x00010000, 0x10010000, 0x00000004,
|
||||
0x10000004, 0x00010004, 0x10010004, 0x20000000, 0x30000000,
|
||||
0x20010000, 0x30010000, 0x20000004, 0x30000004, 0x20010004,
|
||||
0x30010004, 0x00100000, 0x10100000, 0x00110000, 0x10110000,
|
||||
0x00100004, 0x10100004, 0x00110004, 0x10110004, 0x20100000,
|
||||
0x30100000, 0x20110000, 0x30110000, 0x20100004, 0x30100004,
|
||||
0x20110004, 0x30110004, 0x00001000, 0x10001000, 0x00011000,
|
||||
0x10011000, 0x00001004, 0x10001004, 0x00011004, 0x10011004,
|
||||
0x20001000, 0x30001000, 0x20011000, 0x30011000, 0x20001004,
|
||||
0x30001004, 0x20011004, 0x30011004, 0x00101000, 0x10101000,
|
||||
0x00111000, 0x10111000, 0x00101004, 0x10101004, 0x00111004,
|
||||
0x10111004, 0x20101000, 0x30101000, 0x20111000, 0x30111000,
|
||||
0x20101004, 0x30101004, 0x20111004, 0x30111004, },
|
||||
{ // for D bits (numbered as per FIPS 46) 8 9 11 12 13 14
|
||||
0x00000000L, 0x08000000L, 0x00000008L, 0x08000008L, 0x00000400L,
|
||||
0x08000400L, 0x00000408L, 0x08000408L, 0x00020000L, 0x08020000L,
|
||||
0x00020008L, 0x08020008L, 0x00020400L, 0x08020400L, 0x00020408L,
|
||||
0x08020408L, 0x00000001L, 0x08000001L, 0x00000009L, 0x08000009L,
|
||||
0x00000401L, 0x08000401L, 0x00000409L, 0x08000409L, 0x00020001L,
|
||||
0x08020001L, 0x00020009L, 0x08020009L, 0x00020401L, 0x08020401L,
|
||||
0x00020409L, 0x08020409L, 0x02000000L, 0x0A000000L, 0x02000008L,
|
||||
0x0A000008L, 0x02000400L, 0x0A000400L, 0x02000408L, 0x0A000408L,
|
||||
0x02020000L, 0x0A020000L, 0x02020008L, 0x0A020008L, 0x02020400L,
|
||||
0x0A020400L, 0x02020408L, 0x0A020408L, 0x02000001L, 0x0A000001L,
|
||||
0x02000009L, 0x0A000009L, 0x02000401L, 0x0A000401L, 0x02000409L,
|
||||
0x0A000409L, 0x02020001L, 0x0A020001L, 0x02020009L, 0x0A020009L,
|
||||
0x02020401L, 0x0A020401L, 0x02020409L, 0x0A020409L, },
|
||||
0x00000000, 0x08000000, 0x00000008, 0x08000008, 0x00000400,
|
||||
0x08000400, 0x00000408, 0x08000408, 0x00020000, 0x08020000,
|
||||
0x00020008, 0x08020008, 0x00020400, 0x08020400, 0x00020408,
|
||||
0x08020408, 0x00000001, 0x08000001, 0x00000009, 0x08000009,
|
||||
0x00000401, 0x08000401, 0x00000409, 0x08000409, 0x00020001,
|
||||
0x08020001, 0x00020009, 0x08020009, 0x00020401, 0x08020401,
|
||||
0x00020409, 0x08020409, 0x02000000, 0x0A000000, 0x02000008,
|
||||
0x0A000008, 0x02000400, 0x0A000400, 0x02000408, 0x0A000408,
|
||||
0x02020000, 0x0A020000, 0x02020008, 0x0A020008, 0x02020400,
|
||||
0x0A020400, 0x02020408, 0x0A020408, 0x02000001, 0x0A000001,
|
||||
0x02000009, 0x0A000009, 0x02000401, 0x0A000401, 0x02000409,
|
||||
0x0A000409, 0x02020001, 0x0A020001, 0x02020009, 0x0A020009,
|
||||
0x02020401, 0x0A020401, 0x02020409, 0x0A020409, },
|
||||
{ // for D bits (numbered as per FIPS 46) 16 17 18 19 20 21
|
||||
0x00000000L, 0x00000100L, 0x00080000L, 0x00080100L, 0x01000000L,
|
||||
0x01000100L, 0x01080000L, 0x01080100L, 0x00000010L, 0x00000110L,
|
||||
0x00080010L, 0x00080110L, 0x01000010L, 0x01000110L, 0x01080010L,
|
||||
0x01080110L, 0x00200000L, 0x00200100L, 0x00280000L, 0x00280100L,
|
||||
0x01200000L, 0x01200100L, 0x01280000L, 0x01280100L, 0x00200010L,
|
||||
0x00200110L, 0x00280010L, 0x00280110L, 0x01200010L, 0x01200110L,
|
||||
0x01280010L, 0x01280110L, 0x00000200L, 0x00000300L, 0x00080200L,
|
||||
0x00080300L, 0x01000200L, 0x01000300L, 0x01080200L, 0x01080300L,
|
||||
0x00000210L, 0x00000310L, 0x00080210L, 0x00080310L, 0x01000210L,
|
||||
0x01000310L, 0x01080210L, 0x01080310L, 0x00200200L, 0x00200300L,
|
||||
0x00280200L, 0x00280300L, 0x01200200L, 0x01200300L, 0x01280200L,
|
||||
0x01280300L, 0x00200210L, 0x00200310L, 0x00280210L, 0x00280310L,
|
||||
0x01200210L, 0x01200310L, 0x01280210L, 0x01280310L, },
|
||||
0x00000000, 0x00000100, 0x00080000, 0x00080100, 0x01000000,
|
||||
0x01000100, 0x01080000, 0x01080100, 0x00000010, 0x00000110,
|
||||
0x00080010, 0x00080110, 0x01000010, 0x01000110, 0x01080010,
|
||||
0x01080110, 0x00200000, 0x00200100, 0x00280000, 0x00280100,
|
||||
0x01200000, 0x01200100, 0x01280000, 0x01280100, 0x00200010,
|
||||
0x00200110, 0x00280010, 0x00280110, 0x01200010, 0x01200110,
|
||||
0x01280010, 0x01280110, 0x00000200, 0x00000300, 0x00080200,
|
||||
0x00080300, 0x01000200, 0x01000300, 0x01080200, 0x01080300,
|
||||
0x00000210, 0x00000310, 0x00080210, 0x00080310, 0x01000210,
|
||||
0x01000310, 0x01080210, 0x01080310, 0x00200200, 0x00200300,
|
||||
0x00280200, 0x00280300, 0x01200200, 0x01200300, 0x01280200,
|
||||
0x01280300, 0x00200210, 0x00200310, 0x00280210, 0x00280310,
|
||||
0x01200210, 0x01200310, 0x01280210, 0x01280310, },
|
||||
{ // for D bits (numbered as per FIPS 46) 22 23 24 25 27 28
|
||||
0x00000000L, 0x04000000L, 0x00040000L, 0x04040000L, 0x00000002L,
|
||||
0x04000002L, 0x00040002L, 0x04040002L, 0x00002000L, 0x04002000L,
|
||||
0x00042000L, 0x04042000L, 0x00002002L, 0x04002002L, 0x00042002L,
|
||||
0x04042002L, 0x00000020L, 0x04000020L, 0x00040020L, 0x04040020L,
|
||||
0x00000022L, 0x04000022L, 0x00040022L, 0x04040022L, 0x00002020L,
|
||||
0x04002020L, 0x00042020L, 0x04042020L, 0x00002022L, 0x04002022L,
|
||||
0x00042022L, 0x04042022L, 0x00000800L, 0x04000800L, 0x00040800L,
|
||||
0x04040800L, 0x00000802L, 0x04000802L, 0x00040802L, 0x04040802L,
|
||||
0x00002800L, 0x04002800L, 0x00042800L, 0x04042800L, 0x00002802L,
|
||||
0x04002802L, 0x00042802L, 0x04042802L, 0x00000820L, 0x04000820L,
|
||||
0x00040820L, 0x04040820L, 0x00000822L, 0x04000822L, 0x00040822L,
|
||||
0x04040822L, 0x00002820L, 0x04002820L, 0x00042820L, 0x04042820L,
|
||||
0x00002822L, 0x04002822L, 0x00042822L, 0x04042822L, }};
|
||||
0x00000000, 0x04000000, 0x00040000, 0x04040000, 0x00000002,
|
||||
0x04000002, 0x00040002, 0x04040002, 0x00002000, 0x04002000,
|
||||
0x00042000, 0x04042000, 0x00002002, 0x04002002, 0x00042002,
|
||||
0x04042002, 0x00000020, 0x04000020, 0x00040020, 0x04040020,
|
||||
0x00000022, 0x04000022, 0x00040022, 0x04040022, 0x00002020,
|
||||
0x04002020, 0x00042020, 0x04042020, 0x00002022, 0x04002022,
|
||||
0x00042022, 0x04042022, 0x00000800, 0x04000800, 0x00040800,
|
||||
0x04040800, 0x00000802, 0x04000802, 0x00040802, 0x04040802,
|
||||
0x00002800, 0x04002800, 0x00042800, 0x04042800, 0x00002802,
|
||||
0x04002802, 0x00042802, 0x04042802, 0x00000820, 0x04000820,
|
||||
0x00040820, 0x04040820, 0x00000822, 0x04000822, 0x00040822,
|
||||
0x04040822, 0x00002820, 0x04002820, 0x00042820, 0x04042820,
|
||||
0x00002822, 0x04002822, 0x00042822, 0x04042822, }};
|
||||
|
||||
static const uint32_t DES_SPtrans[8][64] = {
|
||||
{ // nibble 0
|
||||
0x02080800L, 0x00080000L, 0x02000002L, 0x02080802L, 0x02000000L,
|
||||
0x00080802L, 0x00080002L, 0x02000002L, 0x00080802L, 0x02080800L,
|
||||
0x02080000L, 0x00000802L, 0x02000802L, 0x02000000L, 0x00000000L,
|
||||
0x00080002L, 0x00080000L, 0x00000002L, 0x02000800L, 0x00080800L,
|
||||
0x02080802L, 0x02080000L, 0x00000802L, 0x02000800L, 0x00000002L,
|
||||
0x00000800L, 0x00080800L, 0x02080002L, 0x00000800L, 0x02000802L,
|
||||
0x02080002L, 0x00000000L, 0x00000000L, 0x02080802L, 0x02000800L,
|
||||
0x00080002L, 0x02080800L, 0x00080000L, 0x00000802L, 0x02000800L,
|
||||
0x02080002L, 0x00000800L, 0x00080800L, 0x02000002L, 0x00080802L,
|
||||
0x00000002L, 0x02000002L, 0x02080000L, 0x02080802L, 0x00080800L,
|
||||
0x02080000L, 0x02000802L, 0x02000000L, 0x00000802L, 0x00080002L,
|
||||
0x00000000L, 0x00080000L, 0x02000000L, 0x02000802L, 0x02080800L,
|
||||
0x00000002L, 0x02080002L, 0x00000800L, 0x00080802L, },
|
||||
0x02080800, 0x00080000, 0x02000002, 0x02080802, 0x02000000,
|
||||
0x00080802, 0x00080002, 0x02000002, 0x00080802, 0x02080800,
|
||||
0x02080000, 0x00000802, 0x02000802, 0x02000000, 0x00000000,
|
||||
0x00080002, 0x00080000, 0x00000002, 0x02000800, 0x00080800,
|
||||
0x02080802, 0x02080000, 0x00000802, 0x02000800, 0x00000002,
|
||||
0x00000800, 0x00080800, 0x02080002, 0x00000800, 0x02000802,
|
||||
0x02080002, 0x00000000, 0x00000000, 0x02080802, 0x02000800,
|
||||
0x00080002, 0x02080800, 0x00080000, 0x00000802, 0x02000800,
|
||||
0x02080002, 0x00000800, 0x00080800, 0x02000002, 0x00080802,
|
||||
0x00000002, 0x02000002, 0x02080000, 0x02080802, 0x00080800,
|
||||
0x02080000, 0x02000802, 0x02000000, 0x00000802, 0x00080002,
|
||||
0x00000000, 0x00080000, 0x02000000, 0x02000802, 0x02080800,
|
||||
0x00000002, 0x02080002, 0x00000800, 0x00080802, },
|
||||
{ // nibble 1
|
||||
0x40108010L, 0x00000000L, 0x00108000L, 0x40100000L, 0x40000010L,
|
||||
0x00008010L, 0x40008000L, 0x00108000L, 0x00008000L, 0x40100010L,
|
||||
0x00000010L, 0x40008000L, 0x00100010L, 0x40108000L, 0x40100000L,
|
||||
0x00000010L, 0x00100000L, 0x40008010L, 0x40100010L, 0x00008000L,
|
||||
0x00108010L, 0x40000000L, 0x00000000L, 0x00100010L, 0x40008010L,
|
||||
0x00108010L, 0x40108000L, 0x40000010L, 0x40000000L, 0x00100000L,
|
||||
0x00008010L, 0x40108010L, 0x00100010L, 0x40108000L, 0x40008000L,
|
||||
0x00108010L, 0x40108010L, 0x00100010L, 0x40000010L, 0x00000000L,
|
||||
0x40000000L, 0x00008010L, 0x00100000L, 0x40100010L, 0x00008000L,
|
||||
0x40000000L, 0x00108010L, 0x40008010L, 0x40108000L, 0x00008000L,
|
||||
0x00000000L, 0x40000010L, 0x00000010L, 0x40108010L, 0x00108000L,
|
||||
0x40100000L, 0x40100010L, 0x00100000L, 0x00008010L, 0x40008000L,
|
||||
0x40008010L, 0x00000010L, 0x40100000L, 0x00108000L, },
|
||||
0x40108010, 0x00000000, 0x00108000, 0x40100000, 0x40000010,
|
||||
0x00008010, 0x40008000, 0x00108000, 0x00008000, 0x40100010,
|
||||
0x00000010, 0x40008000, 0x00100010, 0x40108000, 0x40100000,
|
||||
0x00000010, 0x00100000, 0x40008010, 0x40100010, 0x00008000,
|
||||
0x00108010, 0x40000000, 0x00000000, 0x00100010, 0x40008010,
|
||||
0x00108010, 0x40108000, 0x40000010, 0x40000000, 0x00100000,
|
||||
0x00008010, 0x40108010, 0x00100010, 0x40108000, 0x40008000,
|
||||
0x00108010, 0x40108010, 0x00100010, 0x40000010, 0x00000000,
|
||||
0x40000000, 0x00008010, 0x00100000, 0x40100010, 0x00008000,
|
||||
0x40000000, 0x00108010, 0x40008010, 0x40108000, 0x00008000,
|
||||
0x00000000, 0x40000010, 0x00000010, 0x40108010, 0x00108000,
|
||||
0x40100000, 0x40100010, 0x00100000, 0x00008010, 0x40008000,
|
||||
0x40008010, 0x00000010, 0x40100000, 0x00108000, },
|
||||
{ // nibble 2
|
||||
0x04000001L, 0x04040100L, 0x00000100L, 0x04000101L, 0x00040001L,
|
||||
0x04000000L, 0x04000101L, 0x00040100L, 0x04000100L, 0x00040000L,
|
||||
0x04040000L, 0x00000001L, 0x04040101L, 0x00000101L, 0x00000001L,
|
||||
0x04040001L, 0x00000000L, 0x00040001L, 0x04040100L, 0x00000100L,
|
||||
0x00000101L, 0x04040101L, 0x00040000L, 0x04000001L, 0x04040001L,
|
||||
0x04000100L, 0x00040101L, 0x04040000L, 0x00040100L, 0x00000000L,
|
||||
0x04000000L, 0x00040101L, 0x04040100L, 0x00000100L, 0x00000001L,
|
||||
0x00040000L, 0x00000101L, 0x00040001L, 0x04040000L, 0x04000101L,
|
||||
0x00000000L, 0x04040100L, 0x00040100L, 0x04040001L, 0x00040001L,
|
||||
0x04000000L, 0x04040101L, 0x00000001L, 0x00040101L, 0x04000001L,
|
||||
0x04000000L, 0x04040101L, 0x00040000L, 0x04000100L, 0x04000101L,
|
||||
0x00040100L, 0x04000100L, 0x00000000L, 0x04040001L, 0x00000101L,
|
||||
0x04000001L, 0x00040101L, 0x00000100L, 0x04040000L, },
|
||||
0x04000001, 0x04040100, 0x00000100, 0x04000101, 0x00040001,
|
||||
0x04000000, 0x04000101, 0x00040100, 0x04000100, 0x00040000,
|
||||
0x04040000, 0x00000001, 0x04040101, 0x00000101, 0x00000001,
|
||||
0x04040001, 0x00000000, 0x00040001, 0x04040100, 0x00000100,
|
||||
0x00000101, 0x04040101, 0x00040000, 0x04000001, 0x04040001,
|
||||
0x04000100, 0x00040101, 0x04040000, 0x00040100, 0x00000000,
|
||||
0x04000000, 0x00040101, 0x04040100, 0x00000100, 0x00000001,
|
||||
0x00040000, 0x00000101, 0x00040001, 0x04040000, 0x04000101,
|
||||
0x00000000, 0x04040100, 0x00040100, 0x04040001, 0x00040001,
|
||||
0x04000000, 0x04040101, 0x00000001, 0x00040101, 0x04000001,
|
||||
0x04000000, 0x04040101, 0x00040000, 0x04000100, 0x04000101,
|
||||
0x00040100, 0x04000100, 0x00000000, 0x04040001, 0x00000101,
|
||||
0x04000001, 0x00040101, 0x00000100, 0x04040000, },
|
||||
{ // nibble 3
|
||||
0x00401008L, 0x10001000L, 0x00000008L, 0x10401008L, 0x00000000L,
|
||||
0x10400000L, 0x10001008L, 0x00400008L, 0x10401000L, 0x10000008L,
|
||||
0x10000000L, 0x00001008L, 0x10000008L, 0x00401008L, 0x00400000L,
|
||||
0x10000000L, 0x10400008L, 0x00401000L, 0x00001000L, 0x00000008L,
|
||||
0x00401000L, 0x10001008L, 0x10400000L, 0x00001000L, 0x00001008L,
|
||||
0x00000000L, 0x00400008L, 0x10401000L, 0x10001000L, 0x10400008L,
|
||||
0x10401008L, 0x00400000L, 0x10400008L, 0x00001008L, 0x00400000L,
|
||||
0x10000008L, 0x00401000L, 0x10001000L, 0x00000008L, 0x10400000L,
|
||||
0x10001008L, 0x00000000L, 0x00001000L, 0x00400008L, 0x00000000L,
|
||||
0x10400008L, 0x10401000L, 0x00001000L, 0x10000000L, 0x10401008L,
|
||||
0x00401008L, 0x00400000L, 0x10401008L, 0x00000008L, 0x10001000L,
|
||||
0x00401008L, 0x00400008L, 0x00401000L, 0x10400000L, 0x10001008L,
|
||||
0x00001008L, 0x10000000L, 0x10000008L, 0x10401000L, },
|
||||
0x00401008, 0x10001000, 0x00000008, 0x10401008, 0x00000000,
|
||||
0x10400000, 0x10001008, 0x00400008, 0x10401000, 0x10000008,
|
||||
0x10000000, 0x00001008, 0x10000008, 0x00401008, 0x00400000,
|
||||
0x10000000, 0x10400008, 0x00401000, 0x00001000, 0x00000008,
|
||||
0x00401000, 0x10001008, 0x10400000, 0x00001000, 0x00001008,
|
||||
0x00000000, 0x00400008, 0x10401000, 0x10001000, 0x10400008,
|
||||
0x10401008, 0x00400000, 0x10400008, 0x00001008, 0x00400000,
|
||||
0x10000008, 0x00401000, 0x10001000, 0x00000008, 0x10400000,
|
||||
0x10001008, 0x00000000, 0x00001000, 0x00400008, 0x00000000,
|
||||
0x10400008, 0x10401000, 0x00001000, 0x10000000, 0x10401008,
|
||||
0x00401008, 0x00400000, 0x10401008, 0x00000008, 0x10001000,
|
||||
0x00401008, 0x00400008, 0x00401000, 0x10400000, 0x10001008,
|
||||
0x00001008, 0x10000000, 0x10000008, 0x10401000, },
|
||||
{ // nibble 4
|
||||
0x08000000L, 0x00010000L, 0x00000400L, 0x08010420L, 0x08010020L,
|
||||
0x08000400L, 0x00010420L, 0x08010000L, 0x00010000L, 0x00000020L,
|
||||
0x08000020L, 0x00010400L, 0x08000420L, 0x08010020L, 0x08010400L,
|
||||
0x00000000L, 0x00010400L, 0x08000000L, 0x00010020L, 0x00000420L,
|
||||
0x08000400L, 0x00010420L, 0x00000000L, 0x08000020L, 0x00000020L,
|
||||
0x08000420L, 0x08010420L, 0x00010020L, 0x08010000L, 0x00000400L,
|
||||
0x00000420L, 0x08010400L, 0x08010400L, 0x08000420L, 0x00010020L,
|
||||
0x08010000L, 0x00010000L, 0x00000020L, 0x08000020L, 0x08000400L,
|
||||
0x08000000L, 0x00010400L, 0x08010420L, 0x00000000L, 0x00010420L,
|
||||
0x08000000L, 0x00000400L, 0x00010020L, 0x08000420L, 0x00000400L,
|
||||
0x00000000L, 0x08010420L, 0x08010020L, 0x08010400L, 0x00000420L,
|
||||
0x00010000L, 0x00010400L, 0x08010020L, 0x08000400L, 0x00000420L,
|
||||
0x00000020L, 0x00010420L, 0x08010000L, 0x08000020L, },
|
||||
0x08000000, 0x00010000, 0x00000400, 0x08010420, 0x08010020,
|
||||
0x08000400, 0x00010420, 0x08010000, 0x00010000, 0x00000020,
|
||||
0x08000020, 0x00010400, 0x08000420, 0x08010020, 0x08010400,
|
||||
0x00000000, 0x00010400, 0x08000000, 0x00010020, 0x00000420,
|
||||
0x08000400, 0x00010420, 0x00000000, 0x08000020, 0x00000020,
|
||||
0x08000420, 0x08010420, 0x00010020, 0x08010000, 0x00000400,
|
||||
0x00000420, 0x08010400, 0x08010400, 0x08000420, 0x00010020,
|
||||
0x08010000, 0x00010000, 0x00000020, 0x08000020, 0x08000400,
|
||||
0x08000000, 0x00010400, 0x08010420, 0x00000000, 0x00010420,
|
||||
0x08000000, 0x00000400, 0x00010020, 0x08000420, 0x00000400,
|
||||
0x00000000, 0x08010420, 0x08010020, 0x08010400, 0x00000420,
|
||||
0x00010000, 0x00010400, 0x08010020, 0x08000400, 0x00000420,
|
||||
0x00000020, 0x00010420, 0x08010000, 0x08000020, },
|
||||
{ // nibble 5
|
||||
0x80000040L, 0x00200040L, 0x00000000L, 0x80202000L, 0x00200040L,
|
||||
0x00002000L, 0x80002040L, 0x00200000L, 0x00002040L, 0x80202040L,
|
||||
0x00202000L, 0x80000000L, 0x80002000L, 0x80000040L, 0x80200000L,
|
||||
0x00202040L, 0x00200000L, 0x80002040L, 0x80200040L, 0x00000000L,
|
||||
0x00002000L, 0x00000040L, 0x80202000L, 0x80200040L, 0x80202040L,
|
||||
0x80200000L, 0x80000000L, 0x00002040L, 0x00000040L, 0x00202000L,
|
||||
0x00202040L, 0x80002000L, 0x00002040L, 0x80000000L, 0x80002000L,
|
||||
0x00202040L, 0x80202000L, 0x00200040L, 0x00000000L, 0x80002000L,
|
||||
0x80000000L, 0x00002000L, 0x80200040L, 0x00200000L, 0x00200040L,
|
||||
0x80202040L, 0x00202000L, 0x00000040L, 0x80202040L, 0x00202000L,
|
||||
0x00200000L, 0x80002040L, 0x80000040L, 0x80200000L, 0x00202040L,
|
||||
0x00000000L, 0x00002000L, 0x80000040L, 0x80002040L, 0x80202000L,
|
||||
0x80200000L, 0x00002040L, 0x00000040L, 0x80200040L, },
|
||||
0x80000040, 0x00200040, 0x00000000, 0x80202000, 0x00200040,
|
||||
0x00002000, 0x80002040, 0x00200000, 0x00002040, 0x80202040,
|
||||
0x00202000, 0x80000000, 0x80002000, 0x80000040, 0x80200000,
|
||||
0x00202040, 0x00200000, 0x80002040, 0x80200040, 0x00000000,
|
||||
0x00002000, 0x00000040, 0x80202000, 0x80200040, 0x80202040,
|
||||
0x80200000, 0x80000000, 0x00002040, 0x00000040, 0x00202000,
|
||||
0x00202040, 0x80002000, 0x00002040, 0x80000000, 0x80002000,
|
||||
0x00202040, 0x80202000, 0x00200040, 0x00000000, 0x80002000,
|
||||
0x80000000, 0x00002000, 0x80200040, 0x00200000, 0x00200040,
|
||||
0x80202040, 0x00202000, 0x00000040, 0x80202040, 0x00202000,
|
||||
0x00200000, 0x80002040, 0x80000040, 0x80200000, 0x00202040,
|
||||
0x00000000, 0x00002000, 0x80000040, 0x80002040, 0x80202000,
|
||||
0x80200000, 0x00002040, 0x00000040, 0x80200040, },
|
||||
{ // nibble 6
|
||||
0x00004000L, 0x00000200L, 0x01000200L, 0x01000004L, 0x01004204L,
|
||||
0x00004004L, 0x00004200L, 0x00000000L, 0x01000000L, 0x01000204L,
|
||||
0x00000204L, 0x01004000L, 0x00000004L, 0x01004200L, 0x01004000L,
|
||||
0x00000204L, 0x01000204L, 0x00004000L, 0x00004004L, 0x01004204L,
|
||||
0x00000000L, 0x01000200L, 0x01000004L, 0x00004200L, 0x01004004L,
|
||||
0x00004204L, 0x01004200L, 0x00000004L, 0x00004204L, 0x01004004L,
|
||||
0x00000200L, 0x01000000L, 0x00004204L, 0x01004000L, 0x01004004L,
|
||||
0x00000204L, 0x00004000L, 0x00000200L, 0x01000000L, 0x01004004L,
|
||||
0x01000204L, 0x00004204L, 0x00004200L, 0x00000000L, 0x00000200L,
|
||||
0x01000004L, 0x00000004L, 0x01000200L, 0x00000000L, 0x01000204L,
|
||||
0x01000200L, 0x00004200L, 0x00000204L, 0x00004000L, 0x01004204L,
|
||||
0x01000000L, 0x01004200L, 0x00000004L, 0x00004004L, 0x01004204L,
|
||||
0x01000004L, 0x01004200L, 0x01004000L, 0x00004004L, },
|
||||
0x00004000, 0x00000200, 0x01000200, 0x01000004, 0x01004204,
|
||||
0x00004004, 0x00004200, 0x00000000, 0x01000000, 0x01000204,
|
||||
0x00000204, 0x01004000, 0x00000004, 0x01004200, 0x01004000,
|
||||
0x00000204, 0x01000204, 0x00004000, 0x00004004, 0x01004204,
|
||||
0x00000000, 0x01000200, 0x01000004, 0x00004200, 0x01004004,
|
||||
0x00004204, 0x01004200, 0x00000004, 0x00004204, 0x01004004,
|
||||
0x00000200, 0x01000000, 0x00004204, 0x01004000, 0x01004004,
|
||||
0x00000204, 0x00004000, 0x00000200, 0x01000000, 0x01004004,
|
||||
0x01000204, 0x00004204, 0x00004200, 0x00000000, 0x00000200,
|
||||
0x01000004, 0x00000004, 0x01000200, 0x00000000, 0x01000204,
|
||||
0x01000200, 0x00004200, 0x00000204, 0x00004000, 0x01004204,
|
||||
0x01000000, 0x01004200, 0x00000004, 0x00004004, 0x01004204,
|
||||
0x01000004, 0x01004200, 0x01004000, 0x00004004, },
|
||||
{ // nibble 7
|
||||
0x20800080L, 0x20820000L, 0x00020080L, 0x00000000L, 0x20020000L,
|
||||
0x00800080L, 0x20800000L, 0x20820080L, 0x00000080L, 0x20000000L,
|
||||
0x00820000L, 0x00020080L, 0x00820080L, 0x20020080L, 0x20000080L,
|
||||
0x20800000L, 0x00020000L, 0x00820080L, 0x00800080L, 0x20020000L,
|
||||
0x20820080L, 0x20000080L, 0x00000000L, 0x00820000L, 0x20000000L,
|
||||
0x00800000L, 0x20020080L, 0x20800080L, 0x00800000L, 0x00020000L,
|
||||
0x20820000L, 0x00000080L, 0x00800000L, 0x00020000L, 0x20000080L,
|
||||
0x20820080L, 0x00020080L, 0x20000000L, 0x00000000L, 0x00820000L,
|
||||
0x20800080L, 0x20020080L, 0x20020000L, 0x00800080L, 0x20820000L,
|
||||
0x00000080L, 0x00800080L, 0x20020000L, 0x20820080L, 0x00800000L,
|
||||
0x20800000L, 0x20000080L, 0x00820000L, 0x00020080L, 0x20020080L,
|
||||
0x20800000L, 0x00000080L, 0x20820000L, 0x00820080L, 0x00000000L,
|
||||
0x20000000L, 0x20800080L, 0x00020000L, 0x00820080L, }};
|
||||
0x20800080, 0x20820000, 0x00020080, 0x00000000, 0x20020000,
|
||||
0x00800080, 0x20800000, 0x20820080, 0x00000080, 0x20000000,
|
||||
0x00820000, 0x00020080, 0x00820080, 0x20020080, 0x20000080,
|
||||
0x20800000, 0x00020000, 0x00820080, 0x00800080, 0x20020000,
|
||||
0x20820080, 0x20000080, 0x00000000, 0x00820000, 0x20000000,
|
||||
0x00800000, 0x20020080, 0x20800080, 0x00800000, 0x00020000,
|
||||
0x20820000, 0x00000080, 0x00800000, 0x00020000, 0x20000080,
|
||||
0x20820080, 0x00020080, 0x20000000, 0x00000000, 0x00820000,
|
||||
0x20800080, 0x20020080, 0x20020000, 0x00800080, 0x20820000,
|
||||
0x00000080, 0x00800080, 0x20020000, 0x20820080, 0x00800000,
|
||||
0x20800000, 0x20000080, 0x00820000, 0x00020080, 0x20020080,
|
||||
0x20800000, 0x00000080, 0x20820000, 0x00820080, 0x00000000,
|
||||
0x20000000, 0x20800080, 0x00020000, 0x00820080, }};
|
||||
|
||||
#define HPERM_OP(a, t, n, m) \
|
||||
((t) = ((((a) << (16 - (n))) ^ (a)) & (m)), \
|
||||
@@ -308,43 +308,43 @@ void DES_set_key(const DES_cblock *key, DES_key_schedule *schedule) {
|
||||
// do PC1 in 47 simple operations :-)
|
||||
// Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
|
||||
// for the inspiration. :-)
|
||||
PERM_OP(d, c, t, 4, 0x0f0f0f0fL);
|
||||
HPERM_OP(c, t, -2, 0xcccc0000L);
|
||||
HPERM_OP(d, t, -2, 0xcccc0000L);
|
||||
PERM_OP(d, c, t, 1, 0x55555555L);
|
||||
PERM_OP(c, d, t, 8, 0x00ff00ffL);
|
||||
PERM_OP(d, c, t, 1, 0x55555555L);
|
||||
d = (((d & 0x000000ffL) << 16L) | (d & 0x0000ff00L) |
|
||||
((d & 0x00ff0000L) >> 16L) | ((c & 0xf0000000L) >> 4L));
|
||||
c &= 0x0fffffffL;
|
||||
PERM_OP(d, c, t, 4, 0x0f0f0f0f);
|
||||
HPERM_OP(c, t, -2, 0xcccc0000);
|
||||
HPERM_OP(d, t, -2, 0xcccc0000);
|
||||
PERM_OP(d, c, t, 1, 0x55555555);
|
||||
PERM_OP(c, d, t, 8, 0x00ff00ff);
|
||||
PERM_OP(d, c, t, 1, 0x55555555);
|
||||
d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) |
|
||||
((d & 0x00ff0000) >> 16) | ((c & 0xf0000000) >> 4));
|
||||
c &= 0x0fffffff;
|
||||
|
||||
for (i = 0; i < ITERATIONS; i++) {
|
||||
if (shifts2[i]) {
|
||||
c = ((c >> 2L) | (c << 26L));
|
||||
d = ((d >> 2L) | (d << 26L));
|
||||
c = ((c >> 2) | (c << 26));
|
||||
d = ((d >> 2) | (d << 26));
|
||||
} else {
|
||||
c = ((c >> 1L) | (c << 27L));
|
||||
d = ((d >> 1L) | (d << 27L));
|
||||
c = ((c >> 1) | (c << 27));
|
||||
d = ((d >> 1) | (d << 27));
|
||||
}
|
||||
c &= 0x0fffffffL;
|
||||
d &= 0x0fffffffL;
|
||||
c &= 0x0fffffff;
|
||||
d &= 0x0fffffff;
|
||||
// could be a few less shifts but I am to lazy at this
|
||||
// point in time to investigate
|
||||
s = des_skb[0][(c) & 0x3f] |
|
||||
des_skb[1][((c >> 6L) & 0x03) | ((c >> 7L) & 0x3c)] |
|
||||
des_skb[2][((c >> 13L) & 0x0f) | ((c >> 14L) & 0x30)] |
|
||||
des_skb[3][((c >> 20L) & 0x01) | ((c >> 21L) & 0x06) |
|
||||
((c >> 22L) & 0x38)];
|
||||
des_skb[1][((c >> 6) & 0x03) | ((c >> 7) & 0x3c)] |
|
||||
des_skb[2][((c >> 13) & 0x0f) | ((c >> 14) & 0x30)] |
|
||||
des_skb[3][((c >> 20) & 0x01) | ((c >> 21) & 0x06) |
|
||||
((c >> 22) & 0x38)];
|
||||
t = des_skb[4][(d) & 0x3f] |
|
||||
des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)] |
|
||||
des_skb[6][(d >> 15L) & 0x3f] |
|
||||
des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)];
|
||||
des_skb[5][((d >> 7) & 0x03) | ((d >> 8) & 0x3c)] |
|
||||
des_skb[6][(d >> 15) & 0x3f] |
|
||||
des_skb[7][((d >> 21) & 0x0f) | ((d >> 22) & 0x30)];
|
||||
|
||||
// table contained 0213 4657
|
||||
t2 = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL;
|
||||
t2 = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff;
|
||||
schedule->subkeys[i][0] = CRYPTO_rotr_u32(t2, 30);
|
||||
|
||||
t2 = ((s >> 16L) | (t & 0xffff0000L));
|
||||
t2 = ((s >> 16) | (t & 0xffff0000));
|
||||
schedule->subkeys[i][1] = CRYPTO_rotr_u32(t2, 26);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +200,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
CBB algorithm, oid, null;
|
||||
if (!CBB_add_asn1(cbb, &algorithm, CBS_ASN1_SEQUENCE) ||
|
||||
!CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -209,7 +208,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMDOIDs); i++) {
|
||||
if (nid == kMDOIDs[i].nid) {
|
||||
if (!CBB_add_bytes(&oid, kMDOIDs[i].oid, kMDOIDs[i].oid_len)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
found = 1;
|
||||
@@ -224,7 +222,6 @@ int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md) {
|
||||
|
||||
if (!CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
|
||||
!CBB_flush(cbb)) {
|
||||
OPENSSL_PUT_ERROR(DIGEST, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,6 @@ static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
|
||||
DSA *DSA_new(void) {
|
||||
DSA *dsa = OPENSSL_malloc(sizeof(DSA));
|
||||
if (dsa == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -203,6 +202,10 @@ int DSA_set0_pqg(DSA *dsa, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
|
||||
dsa->g = g;
|
||||
}
|
||||
|
||||
BN_MONT_CTX_free(dsa->method_mont_p);
|
||||
dsa->method_mont_p = NULL;
|
||||
BN_MONT_CTX_free(dsa->method_mont_q);
|
||||
dsa->method_mont_q = NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -589,7 +592,12 @@ static int mod_mul_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
|
||||
}
|
||||
|
||||
DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
|
||||
if (!dsa_check_parameters(dsa)) {
|
||||
if (!dsa_check_key(dsa)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (dsa->priv_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -610,6 +618,14 @@ DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, const DSA *dsa) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
// Cap iterations so that invalid parameters do not infinite loop. This does
|
||||
// not impact valid parameters because the probability of requiring even one
|
||||
// retry is negligible, let alone 32. Unfortunately, DSA was mis-specified, so
|
||||
// invalid parameters are reachable from most callers handling untrusted
|
||||
// private keys. (The |dsa_check_key| call above is not sufficient. Checking
|
||||
// whether arbitrary paremeters form a valid DSA group is expensive.)
|
||||
static const int kMaxIterations = 32;
|
||||
int iters = 0;
|
||||
redo:
|
||||
if (!dsa_sign_setup(dsa, ctx, &kinv, &r)) {
|
||||
goto err;
|
||||
@@ -649,8 +665,14 @@ redo:
|
||||
// Redo if r or s is zero as required by FIPS 186-3: this is
|
||||
// very unlikely.
|
||||
if (BN_is_zero(r) || BN_is_zero(s)) {
|
||||
iters++;
|
||||
if (iters > kMaxIterations) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_TOO_MANY_ITERATIONS);
|
||||
goto err;
|
||||
}
|
||||
goto redo;
|
||||
}
|
||||
|
||||
ret = DSA_SIG_new();
|
||||
if (ret == NULL) {
|
||||
goto err;
|
||||
@@ -684,7 +706,12 @@ int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
|
||||
int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
|
||||
size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
|
||||
*out_valid = 0;
|
||||
if (!dsa_check_parameters(dsa)) {
|
||||
if (!dsa_check_key(dsa)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dsa->pub_key == NULL) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -843,6 +870,10 @@ static size_t der_len_len(size_t len) {
|
||||
}
|
||||
|
||||
int DSA_size(const DSA *dsa) {
|
||||
if (dsa->q == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t order_len = BN_num_bytes(dsa->q);
|
||||
// Compute the maximum length of an |order_len| byte integer. Defensively
|
||||
// assume that the leading 0x00 is included.
|
||||
@@ -865,11 +896,6 @@ int DSA_size(const DSA *dsa) {
|
||||
|
||||
static int dsa_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
|
||||
BIGNUM **out_r) {
|
||||
if (!dsa->p || !dsa->q || !dsa->g) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
BIGNUM k;
|
||||
BN_init(&k);
|
||||
|
||||
@@ -70,15 +70,25 @@
|
||||
// This function is in dsa_asn1.c rather than dsa.c because it is reachable from
|
||||
// |EVP_PKEY| parsers. This makes it easier for the static linker to drop most
|
||||
// of the DSA implementation.
|
||||
int dsa_check_parameters(const DSA *dsa) {
|
||||
int dsa_check_key(const DSA *dsa) {
|
||||
if (!dsa->p || !dsa->q || !dsa->g) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_MISSING_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Reject invalid parameters. In particular, signing will infinite loop if |g|
|
||||
// is zero.
|
||||
if (BN_is_zero(dsa->p) || BN_is_zero(dsa->q) || BN_is_zero(dsa->g)) {
|
||||
// Fully checking for invalid DSA groups is expensive, so security and
|
||||
// correctness of the signature scheme depend on how |dsa| was computed. I.e.
|
||||
// we leave "assurance of domain parameter validity" from FIPS 186-4 to the
|
||||
// caller. However, we check bounds on all values to avoid DoS vectors even
|
||||
// when domain parameters are invalid. In particular, signing will infinite
|
||||
// loop if |g| is zero.
|
||||
if (BN_is_negative(dsa->p) || BN_is_negative(dsa->q) || BN_is_zero(dsa->p) ||
|
||||
BN_is_zero(dsa->q) || !BN_is_odd(dsa->p) || !BN_is_odd(dsa->q) ||
|
||||
// |q| must be a prime divisor of |p - 1|, which implies |q < p|.
|
||||
BN_cmp(dsa->q, dsa->p) >= 0 ||
|
||||
// |g| is in the multiplicative group of |p|.
|
||||
BN_is_negative(dsa->g) || BN_is_zero(dsa->g) ||
|
||||
BN_cmp(dsa->g, dsa->p) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
@@ -97,6 +107,25 @@ int dsa_check_parameters(const DSA *dsa) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dsa->pub_key != NULL) {
|
||||
// The public key is also in the multiplicative group of |p|.
|
||||
if (BN_is_negative(dsa->pub_key) || BN_is_zero(dsa->pub_key) ||
|
||||
BN_cmp(dsa->pub_key, dsa->p) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (dsa->priv_key != NULL) {
|
||||
// The private key is a non-zero element of the scalar field, determined by
|
||||
// |q|.
|
||||
if (BN_is_negative(dsa->priv_key) || BN_is_zero(dsa->priv_key) ||
|
||||
BN_cmp(dsa->priv_key, dsa->q) >= 0) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_INVALID_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -162,7 +191,7 @@ DSA *DSA_parse_public_key(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
@@ -200,7 +229,7 @@ DSA *DSA_parse_parameters(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
return ret;
|
||||
@@ -251,9 +280,10 @@ DSA *DSA_parse_private_key(CBS *cbs) {
|
||||
OPENSSL_PUT_ERROR(DSA, DSA_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
if (!dsa_check_parameters(ret)) {
|
||||
if (!dsa_check_key(ret)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
err:
|
||||
|
||||
@@ -17,14 +17,34 @@
|
||||
|
||||
#include <CCryptoBoringSSL_dsa.h>
|
||||
|
||||
#include <CCryptoBoringSSL_thread.h>
|
||||
|
||||
#include "../internal.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
// dsa_check_parameters checks that |dsa|'s group is within DoS bounds. It
|
||||
// returns one on success and zero on error.
|
||||
int dsa_check_parameters(const DSA *dsa);
|
||||
struct dsa_st {
|
||||
BIGNUM *p;
|
||||
BIGNUM *q;
|
||||
BIGNUM *g;
|
||||
|
||||
BIGNUM *pub_key;
|
||||
BIGNUM *priv_key;
|
||||
|
||||
// Normally used to cache montgomery values
|
||||
CRYPTO_MUTEX method_mont_lock;
|
||||
BN_MONT_CTX *method_mont_p;
|
||||
BN_MONT_CTX *method_mont_q;
|
||||
CRYPTO_refcount_t references;
|
||||
CRYPTO_EX_DATA ex_data;
|
||||
};
|
||||
|
||||
// dsa_check_key performs cheap self-checks on |dsa|, and ensures it is within
|
||||
// DoS bounds. It returns one on success and zero on error.
|
||||
int dsa_check_key(const DSA *dsa);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -67,11 +67,21 @@
|
||||
#include "../internal.h"
|
||||
|
||||
|
||||
static const unsigned kParametersTag =
|
||||
static const CBS_ASN1_TAG kParametersTag =
|
||||
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 0;
|
||||
static const unsigned kPublicKeyTag =
|
||||
static const CBS_ASN1_TAG kPublicKeyTag =
|
||||
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1;
|
||||
|
||||
// TODO(https://crbug.com/boringssl/497): Allow parsers to specify a list of
|
||||
// acceptable groups, so parsers don't have to pull in all four.
|
||||
typedef const EC_GROUP *(*ec_group_func)(void);
|
||||
static const ec_group_func kAllGroups[] = {
|
||||
&EC_group_p224,
|
||||
&EC_group_p256,
|
||||
&EC_group_p384,
|
||||
&EC_group_p521,
|
||||
};
|
||||
|
||||
EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
|
||||
CBS ec_private_key, private_key;
|
||||
uint64_t version;
|
||||
@@ -84,7 +94,6 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
|
||||
}
|
||||
|
||||
// Parse the optional parameters field.
|
||||
EC_GROUP *inner_group = NULL;
|
||||
EC_KEY *ret = NULL;
|
||||
BIGNUM *priv_key = NULL;
|
||||
if (CBS_peek_asn1_tag(&ec_private_key, kParametersTag)) {
|
||||
@@ -97,7 +106,7 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
|
||||
goto err;
|
||||
}
|
||||
inner_group = EC_KEY_parse_parameters(&child);
|
||||
const EC_GROUP *inner_group = EC_KEY_parse_parameters(&child);
|
||||
if (inner_group == NULL) {
|
||||
goto err;
|
||||
}
|
||||
@@ -179,13 +188,11 @@ EC_KEY *EC_KEY_parse_private_key(CBS *cbs, const EC_GROUP *group) {
|
||||
}
|
||||
|
||||
BN_free(priv_key);
|
||||
EC_GROUP_free(inner_group);
|
||||
return ret;
|
||||
|
||||
err:
|
||||
EC_KEY_free(ret);
|
||||
BN_free(priv_key);
|
||||
EC_GROUP_free(inner_group);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -244,9 +251,12 @@ int EC_KEY_marshal_private_key(CBB *cbb, const EC_KEY *key,
|
||||
// kPrimeFieldOID is the encoding of 1.2.840.10045.1.1.
|
||||
static const uint8_t kPrimeField[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01};
|
||||
|
||||
static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
|
||||
CBS *out_b, CBS *out_base_x,
|
||||
CBS *out_base_y, CBS *out_order) {
|
||||
struct explicit_prime_curve {
|
||||
CBS prime, a, b, base_x, base_y, order;
|
||||
};
|
||||
|
||||
static int parse_explicit_prime_curve(CBS *in,
|
||||
struct explicit_prime_curve *out) {
|
||||
// See RFC 3279, section 2.3.5. Note that RFC 3279 calls this structure an
|
||||
// ECParameters while RFC 5480 calls it a SpecifiedECDomain.
|
||||
CBS params, field_id, field_type, curve, base, cofactor;
|
||||
@@ -260,18 +270,18 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
|
||||
CBS_len(&field_type) != sizeof(kPrimeField) ||
|
||||
OPENSSL_memcmp(CBS_data(&field_type), kPrimeField, sizeof(kPrimeField)) !=
|
||||
0 ||
|
||||
!CBS_get_asn1(&field_id, out_prime, CBS_ASN1_INTEGER) ||
|
||||
!CBS_is_unsigned_asn1_integer(out_prime) ||
|
||||
!CBS_get_asn1(&field_id, &out->prime, CBS_ASN1_INTEGER) ||
|
||||
!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) ||
|
||||
!CBS_get_asn1(&curve, out_b, CBS_ASN1_OCTETSTRING) ||
|
||||
!CBS_get_asn1(&curve, &out->a, CBS_ASN1_OCTETSTRING) ||
|
||||
!CBS_get_asn1(&curve, &out->b, CBS_ASN1_OCTETSTRING) ||
|
||||
// |curve| has an optional BIT STRING seed which we ignore.
|
||||
!CBS_get_optional_asn1(&curve, NULL, NULL, CBS_ASN1_BITSTRING) ||
|
||||
CBS_len(&curve) != 0 ||
|
||||
!CBS_get_asn1(¶ms, &base, CBS_ASN1_OCTETSTRING) ||
|
||||
!CBS_get_asn1(¶ms, out_order, CBS_ASN1_INTEGER) ||
|
||||
!CBS_is_unsigned_asn1_integer(out_order) ||
|
||||
!CBS_get_asn1(¶ms, &out->order, CBS_ASN1_INTEGER) ||
|
||||
!CBS_is_unsigned_asn1_integer(&out->order) ||
|
||||
!CBS_get_optional_asn1(¶ms, &cofactor, &has_cofactor,
|
||||
CBS_ASN1_INTEGER) ||
|
||||
CBS_len(¶ms) != 0) {
|
||||
@@ -300,25 +310,33 @@ static int parse_explicit_prime_curve(CBS *in, CBS *out_prime, CBS *out_a,
|
||||
return 0;
|
||||
}
|
||||
size_t field_len = CBS_len(&base) / 2;
|
||||
CBS_init(out_base_x, CBS_data(&base), field_len);
|
||||
CBS_init(out_base_y, CBS_data(&base) + field_len, field_len);
|
||||
CBS_init(&out->base_x, CBS_data(&base), field_len);
|
||||
CBS_init(&out->base_y, CBS_data(&base) + field_len, field_len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// integers_equal returns one if |a| and |b| are equal, up to leading zeros, and
|
||||
// integers_equal returns one if |bytes| is a big-endian encoding of |bn|, and
|
||||
// zero otherwise.
|
||||
static int integers_equal(const CBS *a, const uint8_t *b, size_t b_len) {
|
||||
// Remove leading zeros from |a| and |b|.
|
||||
CBS a_copy = *a;
|
||||
while (CBS_len(&a_copy) > 0 && CBS_data(&a_copy)[0] == 0) {
|
||||
CBS_skip(&a_copy, 1);
|
||||
static int integers_equal(const CBS *bytes, const BIGNUM *bn) {
|
||||
// Although, in SEC 1, Field-Element-to-Octet-String has a fixed width,
|
||||
// OpenSSL mis-encodes the |a| and |b|, so we tolerate any number of leading
|
||||
// zeros. (This matters for P-521 whose |b| has a leading 0.)
|
||||
CBS copy = *bytes;
|
||||
while (CBS_len(©) > 0 && CBS_data(©)[0] == 0) {
|
||||
CBS_skip(©, 1);
|
||||
}
|
||||
while (b_len > 0 && b[0] == 0) {
|
||||
b++;
|
||||
b_len--;
|
||||
|
||||
if (CBS_len(©) > EC_MAX_BYTES) {
|
||||
return 0;
|
||||
}
|
||||
return CBS_mem_equal(&a_copy, b, b_len);
|
||||
uint8_t buf[EC_MAX_BYTES];
|
||||
if (!BN_bn2bin_padded(buf, CBS_len(©), bn)) {
|
||||
ERR_clear_error();
|
||||
return 0;
|
||||
}
|
||||
|
||||
return CBS_mem_equal(©, buf, CBS_len(©));
|
||||
}
|
||||
|
||||
EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
|
||||
@@ -329,13 +347,10 @@ EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
|
||||
}
|
||||
|
||||
// Look for a matching curve.
|
||||
const struct built_in_curves *const curves = OPENSSL_built_in_curves();
|
||||
for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
|
||||
const struct built_in_curve *curve = &curves->curves[i];
|
||||
if (CBS_len(&named_curve) == curve->oid_len &&
|
||||
OPENSSL_memcmp(CBS_data(&named_curve), curve->oid, curve->oid_len) ==
|
||||
0) {
|
||||
return EC_GROUP_new_by_curve_name(curve->nid);
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kAllGroups); i++) {
|
||||
const EC_GROUP *group = kAllGroups[i]();
|
||||
if (CBS_mem_equal(&named_curve, group->oid, group->oid_len)) {
|
||||
return (EC_GROUP *)group;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,25 +359,15 @@ EC_GROUP *EC_KEY_parse_curve_name(CBS *cbs) {
|
||||
}
|
||||
|
||||
int EC_KEY_marshal_curve_name(CBB *cbb, const EC_GROUP *group) {
|
||||
int nid = EC_GROUP_get_curve_name(group);
|
||||
if (nid == NID_undef) {
|
||||
if (group->oid_len == 0) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct built_in_curves *const curves = OPENSSL_built_in_curves();
|
||||
for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
|
||||
const struct built_in_curve *curve = &curves->curves[i];
|
||||
if (curve->nid == nid) {
|
||||
CBB child;
|
||||
return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) &&
|
||||
CBB_add_bytes(&child, curve->oid, curve->oid_len) &&
|
||||
CBB_flush(cbb);
|
||||
}
|
||||
}
|
||||
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
|
||||
return 0;
|
||||
CBB child;
|
||||
return CBB_add_asn1(cbb, &child, CBS_ASN1_OBJECT) &&
|
||||
CBB_add_bytes(&child, group->oid, group->oid_len) && //
|
||||
CBB_flush(cbb);
|
||||
}
|
||||
|
||||
EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
|
||||
@@ -374,34 +379,56 @@ EC_GROUP *EC_KEY_parse_parameters(CBS *cbs) {
|
||||
// of named curves.
|
||||
//
|
||||
// TODO(davidben): Remove support for this.
|
||||
CBS prime, a, b, base_x, base_y, order;
|
||||
if (!parse_explicit_prime_curve(cbs, &prime, &a, &b, &base_x, &base_y,
|
||||
&order)) {
|
||||
struct explicit_prime_curve curve;
|
||||
if (!parse_explicit_prime_curve(cbs, &curve)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Look for a matching prime curve.
|
||||
const struct built_in_curves *const curves = OPENSSL_built_in_curves();
|
||||
for (size_t i = 0; i < OPENSSL_NUM_BUILT_IN_CURVES; i++) {
|
||||
const struct built_in_curve *curve = &curves->curves[i];
|
||||
const unsigned param_len = curve->param_len;
|
||||
// |curve->params| is ordered p, a, b, x, y, order, each component
|
||||
// zero-padded up to the field length. Although SEC 1 states that the
|
||||
// Field-Element-to-Octet-String conversion also pads, OpenSSL mis-encodes
|
||||
// |a| and |b|, so this comparison must allow omitting leading zeros. (This
|
||||
// is relevant for P-521 whose |b| has a leading 0.)
|
||||
if (integers_equal(&prime, curve->params, param_len) &&
|
||||
integers_equal(&a, curve->params + param_len, param_len) &&
|
||||
integers_equal(&b, curve->params + param_len * 2, param_len) &&
|
||||
integers_equal(&base_x, curve->params + param_len * 3, param_len) &&
|
||||
integers_equal(&base_y, curve->params + param_len * 4, param_len) &&
|
||||
integers_equal(&order, curve->params + param_len * 5, param_len)) {
|
||||
return EC_GROUP_new_by_curve_name(curve->nid);
|
||||
}
|
||||
const EC_GROUP *ret = NULL;
|
||||
BIGNUM *p = BN_new(), *a = BN_new(), *b = BN_new(), *x = BN_new(),
|
||||
*y = BN_new();
|
||||
if (p == NULL || a == NULL || b == NULL || x == NULL || y == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
|
||||
return NULL;
|
||||
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kAllGroups); i++) {
|
||||
const EC_GROUP *group = kAllGroups[i]();
|
||||
if (!integers_equal(&curve.order, EC_GROUP_get0_order(group))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The order alone uniquely identifies the group, but we check the other
|
||||
// parameters to avoid misinterpreting the group.
|
||||
if (!EC_GROUP_get_curve_GFp(group, p, a, b, NULL)) {
|
||||
goto err;
|
||||
}
|
||||
if (!integers_equal(&curve.prime, p) || !integers_equal(&curve.a, a) ||
|
||||
!integers_equal(&curve.b, b)) {
|
||||
break;
|
||||
}
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(
|
||||
group, EC_GROUP_get0_generator(group), x, y, NULL)) {
|
||||
goto err;
|
||||
}
|
||||
if (!integers_equal(&curve.base_x, x) ||
|
||||
!integers_equal(&curve.base_y, y)) {
|
||||
break;
|
||||
}
|
||||
ret = group;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_UNKNOWN_GROUP);
|
||||
}
|
||||
|
||||
err:
|
||||
BN_free(p);
|
||||
BN_free(a);
|
||||
BN_free(b);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
return (EC_GROUP *)ret;
|
||||
}
|
||||
|
||||
int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point,
|
||||
@@ -458,18 +485,16 @@ EC_KEY *d2i_ECParameters(EC_KEY **out_key, const uint8_t **inp, long len) {
|
||||
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
|
||||
const EC_GROUP *group = EC_KEY_parse_parameters(&cbs);
|
||||
if (group == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EC_KEY *ret = EC_KEY_new();
|
||||
if (ret == NULL || !EC_KEY_set_group(ret, group)) {
|
||||
EC_GROUP_free(group);
|
||||
EC_KEY_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
EC_GROUP_free(group);
|
||||
|
||||
if (out_key != NULL) {
|
||||
EC_KEY_free(*out_key);
|
||||
@@ -504,7 +529,6 @@ EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
|
||||
ret = *keyp;
|
||||
if (ret->pub_key == NULL &&
|
||||
(ret->pub_key = EC_POINT_new(ret->group)) == NULL) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
if (!EC_POINT_oct2point(ret->group, ret->pub_key, *inp, len, NULL)) {
|
||||
@@ -518,42 +542,31 @@ EC_KEY *o2i_ECPublicKey(EC_KEY **keyp, const uint8_t **inp, long len) {
|
||||
}
|
||||
|
||||
int i2o_ECPublicKey(const EC_KEY *key, uint8_t **outp) {
|
||||
size_t buf_len = 0;
|
||||
int new_buffer = 0;
|
||||
|
||||
if (key == NULL) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf_len = EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, NULL,
|
||||
0, NULL);
|
||||
|
||||
if (outp == NULL || buf_len == 0) {
|
||||
// out == NULL => just return the length of the octet string
|
||||
return buf_len;
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 0) || //
|
||||
!EC_POINT_point2cbb(&cbb, key->group, key->pub_key, key->conv_form,
|
||||
NULL)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (*outp == NULL) {
|
||||
*outp = OPENSSL_malloc(buf_len);
|
||||
if (*outp == NULL) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
new_buffer = 1;
|
||||
}
|
||||
if (!EC_POINT_point2oct(key->group, key->pub_key, key->conv_form, *outp,
|
||||
buf_len, NULL)) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_EC_LIB);
|
||||
if (new_buffer) {
|
||||
OPENSSL_free(*outp);
|
||||
*outp = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!new_buffer) {
|
||||
*outp += buf_len;
|
||||
}
|
||||
return buf_len;
|
||||
int ret = CBB_finish_i2d(&cbb, outp);
|
||||
// Historically, this function used the wrong return value on error.
|
||||
return ret > 0 ? ret : 0;
|
||||
}
|
||||
|
||||
size_t EC_get_builtin_curves(EC_builtin_curve *out_curves,
|
||||
size_t max_num_curves) {
|
||||
if (max_num_curves > OPENSSL_ARRAY_SIZE(kAllGroups)) {
|
||||
max_num_curves = OPENSSL_ARRAY_SIZE(kAllGroups);
|
||||
}
|
||||
for (size_t i = 0; i < max_num_curves; i++) {
|
||||
const EC_GROUP *group = kAllGroups[i]();
|
||||
out_curves[i].nid = group->curve_name;
|
||||
out_curves[i].comment = group->comment;
|
||||
}
|
||||
return OPENSSL_ARRAY_SIZE(kAllGroups);
|
||||
}
|
||||
@@ -55,7 +55,8 @@ EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret,
|
||||
}
|
||||
|
||||
uint8_t derived[EC_KEY_DERIVE_EXTRA_BYTES + EC_MAX_BYTES];
|
||||
size_t derived_len = BN_num_bytes(&group->order) + EC_KEY_DERIVE_EXTRA_BYTES;
|
||||
size_t derived_len =
|
||||
BN_num_bytes(EC_GROUP_get0_order(group)) + EC_KEY_DERIVE_EXTRA_BYTES;
|
||||
assert(derived_len <= sizeof(derived));
|
||||
if (!HKDF(derived, derived_len, EVP_sha256(), secret, secret_len,
|
||||
/*salt=*/NULL, /*salt_len=*/0, (const uint8_t *)info,
|
||||
@@ -74,10 +75,10 @@ EC_KEY *EC_KEY_derive_from_secret(const EC_GROUP *group, const uint8_t *secret,
|
||||
// enough. 2^(num_bytes(order)) < 2^8 * order, so:
|
||||
//
|
||||
// priv < 2^8 * order * 2^128 < order * order < order * R
|
||||
!BN_from_montgomery(priv, priv, group->order_mont, ctx) ||
|
||||
!BN_from_montgomery(priv, priv, &group->order, ctx) ||
|
||||
// Multiply by R^2 and do another Montgomery reduction to compute
|
||||
// priv * R^-1 * R^2 * R^-1 = priv mod order.
|
||||
!BN_to_montgomery(priv, priv, group->order_mont, ctx) ||
|
||||
!BN_to_montgomery(priv, priv, &group->order, ctx) ||
|
||||
!EC_POINT_mul(group, pub, priv, NULL, NULL, ctx) ||
|
||||
!EC_KEY_set_group(key, group) || !EC_KEY_set_public_key(key, pub) ||
|
||||
!EC_KEY_set_private_key(key, priv)) {
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
|
||||
// This file implements hash-to-curve, as described in
|
||||
// draft-irtf-cfrg-hash-to-curve-07.
|
||||
// draft-irtf-cfrg-hash-to-curve-16.
|
||||
//
|
||||
// This hash-to-curve implementation is written generically with the
|
||||
// expectation that we will eventually wish to support other curves. If it
|
||||
@@ -48,11 +48,17 @@
|
||||
// templates to make specializing more convenient.
|
||||
|
||||
// expand_message_xmd implements the operation described in section 5.3.1 of
|
||||
// draft-irtf-cfrg-hash-to-curve-07. It returns one on success and zero on
|
||||
// allocation failure or if |out_len| was too large.
|
||||
// draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
static int expand_message_xmd(const EVP_MD *md, uint8_t *out, size_t out_len,
|
||||
const uint8_t *msg, size_t msg_len,
|
||||
const uint8_t *dst, size_t dst_len) {
|
||||
// See https://github.com/cfrg/draft-irtf-cfrg-hash-to-curve/issues/352
|
||||
if (dst_len == 0) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
const size_t block_size = EVP_MD_block_size(md);
|
||||
const size_t md_size = EVP_MD_size(md);
|
||||
@@ -132,7 +138,7 @@ err:
|
||||
|
||||
// num_bytes_to_derive determines the number of bytes to derive when hashing to
|
||||
// a number modulo |modulus|. See the hash_to_field operation defined in
|
||||
// section 5.2 of draft-irtf-cfrg-hash-to-curve-07.
|
||||
// section 5.2 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
static int num_bytes_to_derive(size_t *out, const BIGNUM *modulus, unsigned k) {
|
||||
size_t bits = BN_num_bits(modulus);
|
||||
size_t L = (bits + k + 7) / 8;
|
||||
@@ -165,7 +171,7 @@ static void big_endian_to_words(BN_ULONG *out, size_t num_words,
|
||||
}
|
||||
|
||||
// hash_to_field implements the operation described in section 5.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-07, with count = 2. |k| is the security
|
||||
// of draft-irtf-cfrg-hash-to-curve-16, with count = 2. |k| is the security
|
||||
// factor.
|
||||
static int hash_to_field2(const EC_GROUP *group, const EVP_MD *md,
|
||||
EC_FELEM *out1, EC_FELEM *out2, const uint8_t *dst,
|
||||
@@ -173,12 +179,12 @@ static int hash_to_field2(const EC_GROUP *group, const EVP_MD *md,
|
||||
size_t msg_len) {
|
||||
size_t L;
|
||||
uint8_t buf[4 * EC_MAX_BYTES];
|
||||
if (!num_bytes_to_derive(&L, &group->field, k) ||
|
||||
if (!num_bytes_to_derive(&L, &group->field.N, k) ||
|
||||
!expand_message_xmd(md, buf, 2 * L, msg, msg_len, dst, dst_len)) {
|
||||
return 0;
|
||||
}
|
||||
BN_ULONG words[2 * EC_MAX_WORDS];
|
||||
size_t num_words = 2 * group->field.width;
|
||||
size_t num_words = 2 * group->field.N.width;
|
||||
big_endian_to_words(words, num_words, buf, L);
|
||||
group->meth->felem_reduce(group, out1, words, num_words);
|
||||
big_endian_to_words(words, num_words, buf + L, L);
|
||||
@@ -191,15 +197,16 @@ static int hash_to_field2(const EC_GROUP *group, const EVP_MD *md,
|
||||
static int hash_to_scalar(const EC_GROUP *group, const EVP_MD *md,
|
||||
EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
unsigned k, const uint8_t *msg, size_t msg_len) {
|
||||
const BIGNUM *order = EC_GROUP_get0_order(group);
|
||||
size_t L;
|
||||
uint8_t buf[EC_MAX_BYTES * 2];
|
||||
if (!num_bytes_to_derive(&L, &group->order, k) ||
|
||||
if (!num_bytes_to_derive(&L, order, k) ||
|
||||
!expand_message_xmd(md, buf, L, msg, msg_len, dst, dst_len)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
BN_ULONG words[2 * EC_MAX_WORDS];
|
||||
size_t num_words = 2 * group->order.width;
|
||||
size_t num_words = 2 * order->width;
|
||||
big_endian_to_words(words, num_words, buf, L);
|
||||
ec_scalar_reduce(group, out, words, num_words);
|
||||
return 1;
|
||||
@@ -214,95 +221,131 @@ static inline void mul_A(const EC_GROUP *group, EC_FELEM *out,
|
||||
ec_felem_sub(group, out, in, &tmp); // out = -3*in
|
||||
}
|
||||
|
||||
static inline void mul_minus_A(const EC_GROUP *group, EC_FELEM *out,
|
||||
const EC_FELEM *in) {
|
||||
assert(group->a_is_minus3);
|
||||
EC_FELEM tmp;
|
||||
ec_felem_add(group, &tmp, in, in); // tmp = 2*in
|
||||
ec_felem_add(group, out, &tmp, in); // out = 3*in
|
||||
}
|
||||
|
||||
// sgn0_le implements the operation described in section 4.1.2 of
|
||||
// draft-irtf-cfrg-hash-to-curve-07.
|
||||
static BN_ULONG sgn0_le(const EC_GROUP *group, const EC_FELEM *a) {
|
||||
// sgn0 implements the operation described in section 4.1.2 of
|
||||
// draft-irtf-cfrg-hash-to-curve-16.
|
||||
static BN_ULONG sgn0(const EC_GROUP *group, const EC_FELEM *a) {
|
||||
uint8_t buf[EC_MAX_BYTES];
|
||||
size_t len;
|
||||
ec_felem_to_bytes(group, buf, &len, a);
|
||||
return buf[len - 1] & 1;
|
||||
}
|
||||
|
||||
// map_to_curve_simple_swu implements the operation described in section 6.6.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-07, using the optimization in appendix
|
||||
// D.2.1. It returns one on success and zero on error.
|
||||
static int map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_RAW_POINT *out,
|
||||
const EC_FELEM *u) {
|
||||
OPENSSL_UNUSED static int is_3mod4(const EC_GROUP *group) {
|
||||
return group->field.N.width > 0 && (group->field.N.d[0] & 3) == 3;
|
||||
}
|
||||
|
||||
// sqrt_ratio_3mod4 implements the operation described in appendix F.2.1.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-16.
|
||||
static BN_ULONG sqrt_ratio_3mod4(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_FELEM *out_y,
|
||||
const EC_FELEM *u, const EC_FELEM *v) {
|
||||
assert(is_3mod4(group));
|
||||
|
||||
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
|
||||
const EC_FELEM *b) = group->meth->felem_mul;
|
||||
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
|
||||
group->meth->felem_sqr;
|
||||
|
||||
EC_FELEM tv1, tv2, tv3, y1, y2;
|
||||
felem_sqr(group, &tv1, v); // 1. tv1 = v^2
|
||||
felem_mul(group, &tv2, u, v); // 2. tv2 = u * v
|
||||
felem_mul(group, &tv1, &tv1, &tv2); // 3. tv1 = tv1 * tv2
|
||||
group->meth->felem_exp(group, &y1, &tv1, c1, num_c1); // 4. y1 = tv1^c1
|
||||
felem_mul(group, &y1, &y1, &tv2); // 5. y1 = y1 * tv2
|
||||
felem_mul(group, &y2, &y1, c2); // 6. y2 = y1 * c2
|
||||
felem_sqr(group, &tv3, &y1); // 7. tv3 = y1^2
|
||||
felem_mul(group, &tv3, &tv3, v); // 8. tv3 = tv3 * v
|
||||
|
||||
// 9. isQR = tv3 == u
|
||||
// 10. y = CMOV(y2, y1, isQR)
|
||||
// 11. return (isQR, y)
|
||||
//
|
||||
// Note the specification's CMOV function and our |ec_felem_select| have the
|
||||
// opposite argument order.
|
||||
ec_felem_sub(group, &tv1, &tv3, u);
|
||||
const BN_ULONG isQR = ~ec_felem_non_zero_mask(group, &tv1);
|
||||
ec_felem_select(group, out_y, isQR, &y1, &y2);
|
||||
return isQR;
|
||||
}
|
||||
|
||||
// map_to_curve_simple_swu implements the operation described in section 6.6.2
|
||||
// of draft-irtf-cfrg-hash-to-curve-16, using the straight-line implementation
|
||||
// in appendix F.2.
|
||||
static void map_to_curve_simple_swu(const EC_GROUP *group, const EC_FELEM *Z,
|
||||
const BN_ULONG *c1, size_t num_c1,
|
||||
const EC_FELEM *c2, EC_JACOBIAN *out,
|
||||
const EC_FELEM *u) {
|
||||
// This function requires the prime be 3 mod 4, and that A = -3.
|
||||
if (group->field.width == 0 || (group->field.d[0] & 3) != 3 ||
|
||||
!group->a_is_minus3) {
|
||||
OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
assert(is_3mod4(group));
|
||||
assert(group->a_is_minus3);
|
||||
|
||||
EC_FELEM tv1, tv2, tv3, tv4, xd, x1n, x2n, tmp, gxd, gx1, y1, y2;
|
||||
felem_sqr(group, &tv1, u); // tv1 = u^2
|
||||
felem_mul(group, &tv3, Z, &tv1); // tv3 = Z * tv1
|
||||
felem_sqr(group, &tv2, &tv3); // tv2 = tv3^2
|
||||
ec_felem_add(group, &xd, &tv2, &tv3); // xd = tv2 + tv3
|
||||
ec_felem_add(group, &x1n, &xd, &group->one); // x1n = xd + 1
|
||||
felem_mul(group, &x1n, &x1n, &group->b); // x1n = x1n * B
|
||||
mul_minus_A(group, &xd, &xd); // xd = -A * xd
|
||||
BN_ULONG e1 = ec_felem_non_zero_mask(group, &xd); // e1 = xd == 0 [flipped]
|
||||
mul_A(group, &tmp, Z);
|
||||
ec_felem_select(group, &xd, e1, &xd, &tmp); // xd = CMOV(xd, Z * A, e1)
|
||||
felem_sqr(group, &tv2, &xd); // tv2 = xd^2
|
||||
felem_mul(group, &gxd, &tv2, &xd); // gxd = tv2 * xd = xd^3
|
||||
mul_A(group, &tv2, &tv2); // tv2 = A * tv2
|
||||
felem_sqr(group, &gx1, &x1n); // gx1 = x1n^2
|
||||
ec_felem_add(group, &gx1, &gx1, &tv2); // gx1 = gx1 + tv2
|
||||
felem_mul(group, &gx1, &gx1, &x1n); // gx1 = gx1 * x1n
|
||||
felem_mul(group, &tv2, &group->b, &gxd); // tv2 = B * gxd
|
||||
ec_felem_add(group, &gx1, &gx1, &tv2); // gx1 = gx1 + tv2
|
||||
felem_sqr(group, &tv4, &gxd); // tv4 = gxd^2
|
||||
felem_mul(group, &tv2, &gx1, &gxd); // tv2 = gx1 * gxd
|
||||
felem_mul(group, &tv4, &tv4, &tv2); // tv4 = tv4 * tv2
|
||||
group->meth->felem_exp(group, &y1, &tv4, c1, num_c1); // y1 = tv4^c1
|
||||
felem_mul(group, &y1, &y1, &tv2); // y1 = y1 * tv2
|
||||
felem_mul(group, &x2n, &tv3, &x1n); // x2n = tv3 * x1n
|
||||
felem_mul(group, &y2, &y1, c2); // y2 = y1 * c2
|
||||
felem_mul(group, &y2, &y2, &tv1); // y2 = y2 * tv1
|
||||
felem_mul(group, &y2, &y2, u); // y2 = y2 * u
|
||||
felem_sqr(group, &tv2, &y1); // tv2 = y1^2
|
||||
felem_mul(group, &tv2, &tv2, &gxd); // tv2 = tv2 * gxd
|
||||
ec_felem_sub(group, &tv3, &tv2, &gx1);
|
||||
BN_ULONG e2 =
|
||||
ec_felem_non_zero_mask(group, &tv3); // e2 = tv2 == gx1 [flipped]
|
||||
ec_felem_select(group, &x1n, e2, &x2n, &x1n); // xn = CMOV(x2n, x1n, e2)
|
||||
ec_felem_select(group, &y1, e2, &y2, &y1); // y = CMOV(y2, y1, e2)
|
||||
BN_ULONG sgn0_u = sgn0_le(group, u);
|
||||
BN_ULONG sgn0_y = sgn0_le(group, &y1);
|
||||
BN_ULONG e3 = sgn0_u ^ sgn0_y;
|
||||
e3 = ((BN_ULONG)0) - e3; // e3 = sgn0(u) == sgn0(y) [flipped]
|
||||
ec_felem_neg(group, &y2, &y1);
|
||||
ec_felem_select(group, &y1, e3, &y2, &y1); // y = CMOV(-y, y, e3)
|
||||
void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
|
||||
const EC_FELEM *b) = group->meth->felem_mul;
|
||||
void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
|
||||
group->meth->felem_sqr;
|
||||
|
||||
// Appendix D.1 describes how to convert (x1n, xd, y1, 1) to Jacobian
|
||||
// coordinates. Note yd = 1. Also note that gxd computed above is xd^3.
|
||||
felem_mul(group, &out->X, &x1n, &xd); // X = xn * xd
|
||||
felem_mul(group, &out->Y, &y1, &gxd); // Y = yn * gxd = yn * xd^3
|
||||
out->Z = xd; // Z = xd
|
||||
return 1;
|
||||
EC_FELEM tv1, tv2, tv3, tv4, tv5, tv6, x, y, y1;
|
||||
felem_sqr(group, &tv1, u); // 1. tv1 = u^2
|
||||
felem_mul(group, &tv1, Z, &tv1); // 2. tv1 = Z * tv1
|
||||
felem_sqr(group, &tv2, &tv1); // 3. tv2 = tv1^2
|
||||
ec_felem_add(group, &tv2, &tv2, &tv1); // 4. tv2 = tv2 + tv1
|
||||
ec_felem_add(group, &tv3, &tv2, ec_felem_one(group)); // 5. tv3 = tv2 + 1
|
||||
felem_mul(group, &tv3, &group->b, &tv3); // 6. tv3 = B * tv3
|
||||
|
||||
// 7. tv4 = CMOV(Z, -tv2, tv2 != 0)
|
||||
const BN_ULONG tv2_non_zero = ec_felem_non_zero_mask(group, &tv2);
|
||||
ec_felem_neg(group, &tv4, &tv2);
|
||||
ec_felem_select(group, &tv4, tv2_non_zero, &tv4, Z);
|
||||
|
||||
mul_A(group, &tv4, &tv4); // 8. tv4 = A * tv4
|
||||
felem_sqr(group, &tv2, &tv3); // 9. tv2 = tv3^2
|
||||
felem_sqr(group, &tv6, &tv4); // 10. tv6 = tv4^2
|
||||
mul_A(group, &tv5, &tv6); // 11. tv5 = A * tv6
|
||||
ec_felem_add(group, &tv2, &tv2, &tv5); // 12. tv2 = tv2 + tv5
|
||||
felem_mul(group, &tv2, &tv2, &tv3); // 13. tv2 = tv2 * tv3
|
||||
felem_mul(group, &tv6, &tv6, &tv4); // 14. tv6 = tv6 * tv4
|
||||
felem_mul(group, &tv5, &group->b, &tv6); // 15. tv5 = B * tv6
|
||||
ec_felem_add(group, &tv2, &tv2, &tv5); // 16. tv2 = tv2 + tv5
|
||||
felem_mul(group, &x, &tv1, &tv3); // 17. x = tv1 * tv3
|
||||
|
||||
// 18. (is_gx1_square, y1) = sqrt_ratio(tv2, tv6)
|
||||
const BN_ULONG is_gx1_square =
|
||||
sqrt_ratio_3mod4(group, Z, c1, num_c1, c2, &y1, &tv2, &tv6);
|
||||
|
||||
felem_mul(group, &y, &tv1, u); // 19. y = tv1 * u
|
||||
felem_mul(group, &y, &y, &y1); // 20. y = y * y1
|
||||
|
||||
// 21. x = CMOV(x, tv3, is_gx1_square)
|
||||
ec_felem_select(group, &x, is_gx1_square, &tv3, &x);
|
||||
// 22. y = CMOV(y, y1, is_gx1_square)
|
||||
ec_felem_select(group, &y, is_gx1_square, &y1, &y);
|
||||
|
||||
// 23. e1 = sgn0(u) == sgn0(y)
|
||||
BN_ULONG sgn0_u = sgn0(group, u);
|
||||
BN_ULONG sgn0_y = sgn0(group, &y);
|
||||
BN_ULONG not_e1 = sgn0_u ^ sgn0_y;
|
||||
not_e1 = ((BN_ULONG)0) - not_e1;
|
||||
|
||||
// 24. y = CMOV(-y, y, e1)
|
||||
ec_felem_neg(group, &tv1, &y);
|
||||
ec_felem_select(group, &y, not_e1, &tv1, &y);
|
||||
|
||||
// 25. x = x / tv4
|
||||
//
|
||||
// Our output is in projective coordinates, so rather than inverting |tv4|
|
||||
// now, represent (x / tv4, y) as (x * tv4, y * tv4^3, tv4). This is much more
|
||||
// efficient if the caller will do further computation on the output. (If the
|
||||
// caller will immediately convert to affine coordinates, it is slightly less
|
||||
// efficient, but only by a few field multiplications.)
|
||||
felem_mul(group, &out->X, &x, &tv4);
|
||||
felem_mul(group, &out->Y, &y, &tv6);
|
||||
out->Z = tv4;
|
||||
}
|
||||
|
||||
static int hash_to_curve(const EC_GROUP *group, const EVP_MD *md,
|
||||
const EC_FELEM *Z, const EC_FELEM *c2, unsigned k,
|
||||
EC_RAW_POINT *out, const uint8_t *dst, size_t dst_len,
|
||||
EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
EC_FELEM u0, u1;
|
||||
if (!hash_to_field2(group, md, &u0, &u1, dst, dst_len, k, msg, msg_len)) {
|
||||
@@ -311,17 +354,15 @@ static int hash_to_curve(const EC_GROUP *group, const EVP_MD *md,
|
||||
|
||||
// Compute |c1| = (p - 3) / 4.
|
||||
BN_ULONG c1[EC_MAX_WORDS];
|
||||
size_t num_c1 = group->field.width;
|
||||
if (!bn_copy_words(c1, num_c1, &group->field)) {
|
||||
size_t num_c1 = group->field.N.width;
|
||||
if (!bn_copy_words(c1, num_c1, &group->field.N)) {
|
||||
return 0;
|
||||
}
|
||||
bn_rshift_words(c1, c1, /*shift=*/2, /*num=*/num_c1);
|
||||
|
||||
EC_RAW_POINT Q0, Q1;
|
||||
if (!map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, &u0) ||
|
||||
!map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, &u1)) {
|
||||
return 0;
|
||||
}
|
||||
EC_JACOBIAN Q0, Q1;
|
||||
map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q0, &u0);
|
||||
map_to_curve_simple_swu(group, Z, c1, num_c1, c2, &Q1, &u1);
|
||||
|
||||
group->meth->add(group, out, &Q0, &Q1); // R = Q0 + Q1
|
||||
// All our curves have cofactor one, so |clear_cofactor| is a no-op.
|
||||
@@ -330,13 +371,116 @@ static int hash_to_curve(const EC_GROUP *group, const EVP_MD *md,
|
||||
|
||||
static int felem_from_u8(const EC_GROUP *group, EC_FELEM *out, uint8_t a) {
|
||||
uint8_t bytes[EC_MAX_BYTES] = {0};
|
||||
size_t len = BN_num_bytes(&group->field);
|
||||
size_t len = BN_num_bytes(&group->field.N);
|
||||
bytes[len - 1] = a;
|
||||
return ec_felem_from_bytes(group, out, bytes, len);
|
||||
}
|
||||
|
||||
// kP256Sqrt10 is sqrt(10) in P-256's field. It was computed as follows in
|
||||
// python3:
|
||||
//
|
||||
// p = 2**256 - 2**224 + 2**192 + 2**96 - 1
|
||||
// c2 = pow(10, (p+1)//4, p)
|
||||
// assert pow(c2, 2, p) == 10
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(256//8, 'big'))
|
||||
static const uint8_t kP256Sqrt10[] = {
|
||||
0xda, 0x53, 0x8e, 0x3b, 0xe1, 0xd8, 0x9b, 0x99, 0xc9, 0x78, 0xfc,
|
||||
0x67, 0x51, 0x80, 0xaa, 0xb2, 0x7b, 0x8d, 0x1f, 0xf8, 0x4c, 0x55,
|
||||
0xd5, 0xb6, 0x2c, 0xcd, 0x34, 0x27, 0xe4, 0x33, 0xc4, 0x7f};
|
||||
|
||||
// kP384Sqrt12 is sqrt(12) in P-384's field. It was computed as follows in
|
||||
// python3:
|
||||
//
|
||||
// p = 2**384 - 2**128 - 2**96 + 2**32 - 1
|
||||
// c2 = pow(12, (p+1)//4, p)
|
||||
// assert pow(c2, 2, p) == 12
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(384//8, 'big'))
|
||||
static const uint8_t kP384Sqrt12[] = {
|
||||
0x2a, 0xcc, 0xb4, 0xa6, 0x56, 0xb0, 0x24, 0x9c, 0x71, 0xf0, 0x50, 0x0e,
|
||||
0x83, 0xda, 0x2f, 0xdd, 0x7f, 0x98, 0xe3, 0x83, 0xd6, 0x8b, 0x53, 0x87,
|
||||
0x1f, 0x87, 0x2f, 0xcb, 0x9c, 0xcb, 0x80, 0xc5, 0x3c, 0x0d, 0xe1, 0xf8,
|
||||
0xa8, 0x0f, 0x7e, 0x19, 0x14, 0xe2, 0xec, 0x69, 0xf5, 0xa6, 0x26, 0xb3};
|
||||
|
||||
int ec_hash_to_curve_p256_xmd_sha256_sswu(const EC_GROUP *group,
|
||||
EC_JACOBIAN *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg,
|
||||
size_t msg_len) {
|
||||
// See section 8.3 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
if (EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Z = -10, c2 = sqrt(10)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 10) ||
|
||||
!ec_felem_from_bytes(group, &c2, kP256Sqrt10, sizeof(kP256Sqrt10))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
return hash_to_curve(group, EVP_sha256(), &Z, &c2, /*k=*/128, out, dst,
|
||||
dst_len, msg, msg_len);
|
||||
}
|
||||
|
||||
int EC_hash_to_curve_p256_xmd_sha256_sswu(const EC_GROUP *group, EC_POINT *out,
|
||||
const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_cmp(group, out->group, NULL) != 0) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
|
||||
return 0;
|
||||
}
|
||||
return ec_hash_to_curve_p256_xmd_sha256_sswu(group, &out->raw, dst, dst_len,
|
||||
msg, msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_curve_p384_xmd_sha384_sswu(const EC_GROUP *group,
|
||||
EC_JACOBIAN *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg,
|
||||
size_t msg_len) {
|
||||
// See section 8.3 of draft-irtf-cfrg-hash-to-curve-16.
|
||||
if (EC_GROUP_get_curve_name(group) != NID_secp384r1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Z = -12, c2 = sqrt(12)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 12) ||
|
||||
!ec_felem_from_bytes(group, &c2, kP384Sqrt12, sizeof(kP384Sqrt12))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
return hash_to_curve(group, EVP_sha384(), &Z, &c2, /*k=*/192, out, dst,
|
||||
dst_len, msg, msg_len);
|
||||
}
|
||||
|
||||
int EC_hash_to_curve_p384_xmd_sha384_sswu(const EC_GROUP *group, EC_POINT *out,
|
||||
const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_cmp(group, out->group, NULL) != 0) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
|
||||
return 0;
|
||||
}
|
||||
return ec_hash_to_curve_p384_xmd_sha384_sswu(group, &out->raw, dst, dst_len,
|
||||
msg, msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_scalar_p384_xmd_sha384(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len) {
|
||||
if (EC_GROUP_get_curve_name(group) != NID_secp384r1) {
|
||||
OPENSSL_PUT_ERROR(EC, EC_R_GROUP_MISMATCH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hash_to_scalar(group, EVP_sha384(), out, dst, dst_len, /*k=*/192, msg,
|
||||
msg_len);
|
||||
}
|
||||
|
||||
int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len) {
|
||||
// See section 8.3 of draft-irtf-cfrg-hash-to-curve-07.
|
||||
if (EC_GROUP_get_curve_name(group) != NID_secp384r1) {
|
||||
@@ -344,25 +488,10 @@ int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
return 0;
|
||||
}
|
||||
|
||||
// kSqrt1728 was computed as follows in python3:
|
||||
//
|
||||
// p = 2**384 - 2**128 - 2**96 + 2**32 - 1
|
||||
// z3 = 12**3
|
||||
// c2 = pow(z3, (p+1)//4, p)
|
||||
// assert z3 == pow(c2, 2, p)
|
||||
// ", ".join("0x%02x" % b for b in c2.to_bytes(384//8, 'big')
|
||||
|
||||
static const uint8_t kSqrt1728[] = {
|
||||
0x01, 0x98, 0x77, 0xcc, 0x10, 0x41, 0xb7, 0x55, 0x57, 0x43, 0xc0, 0xae,
|
||||
0x2e, 0x3a, 0x3e, 0x61, 0xfb, 0x2a, 0xaa, 0x2e, 0x0e, 0x87, 0xea, 0x55,
|
||||
0x7a, 0x56, 0x3d, 0x8b, 0x59, 0x8a, 0x09, 0x40, 0xd0, 0xa6, 0x97, 0xa9,
|
||||
0xe0, 0xb9, 0xe9, 0x2c, 0xfa, 0xa3, 0x14, 0xf5, 0x83, 0xc9, 0xd0, 0x66
|
||||
};
|
||||
|
||||
// Z = -12, c2 = sqrt(1728)
|
||||
// Z = -12, c2 = sqrt(12)
|
||||
EC_FELEM Z, c2;
|
||||
if (!felem_from_u8(group, &Z, 12) ||
|
||||
!ec_felem_from_bytes(group, &c2, kSqrt1728, sizeof(kSqrt1728))) {
|
||||
!ec_felem_from_bytes(group, &c2, kP384Sqrt12, sizeof(kP384Sqrt12))) {
|
||||
return 0;
|
||||
}
|
||||
ec_felem_neg(group, &Z, &Z);
|
||||
|
||||
@@ -26,24 +26,48 @@ extern "C" {
|
||||
|
||||
// Hash-to-curve.
|
||||
//
|
||||
// The following functions implement primitives from
|
||||
// draft-irtf-cfrg-hash-to-curve. The |dst| parameter in each function is the
|
||||
// domain separation tag and must be unique for each protocol and between the
|
||||
// |hash_to_curve| and |hash_to_scalar| variants. See section 3.1 of the spec
|
||||
// for additional guidance on this parameter.
|
||||
// Internal |EC_JACOBIAN| versions of the corresponding public APIs.
|
||||
|
||||
// ec_hash_to_curve_p256_xmd_sha256_sswu hashes |msg| to a point on |group| and
|
||||
// writes the result to |out|, implementing the P256_XMD:SHA-256_SSWU_RO_ suite
|
||||
// from draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p256_xmd_sha256_sswu(
|
||||
const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_curve_p384_xmd_sha384_sswu hashes |msg| to a point on |group| and
|
||||
// writes the result to |out|, implementing the P384_XMD:SHA-384_SSWU_RO_ suite
|
||||
// from draft-irtf-cfrg-hash-to-curve-16. It returns one on success and zero on
|
||||
// error.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha384_sswu(
|
||||
const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_scalar_p384_xmd_sha384 hashes |msg| to a scalar on |group|
|
||||
// and writes the result to |out|, using the hash_to_field operation from the
|
||||
// P384_XMD:SHA-384_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-16, but
|
||||
// generating a value modulo the group order rather than a field element.
|
||||
OPENSSL_EXPORT int ec_hash_to_scalar_p384_xmd_sha384(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_curve_p384_xmd_sha512_sswu_draft07 hashes |msg| to a point on
|
||||
// |group| and writes the result to |out|, implementing the
|
||||
// P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07. It
|
||||
// returns one on success and zero on error.
|
||||
//
|
||||
// TODO(https://crbug.com/1414562): Migrate this to the final version.
|
||||
OPENSSL_EXPORT int ec_hash_to_curve_p384_xmd_sha512_sswu_draft07(
|
||||
const EC_GROUP *group, EC_RAW_POINT *out, const uint8_t *dst,
|
||||
size_t dst_len, const uint8_t *msg, size_t msg_len);
|
||||
const EC_GROUP *group, EC_JACOBIAN *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
// ec_hash_to_scalar_p384_xmd_sha512_draft07 hashes |msg| to a scalar on |group|
|
||||
// and writes the result to |out|, using the hash_to_field operation from the
|
||||
// P384_XMD:SHA-512_SSWU_RO_ suite from draft-irtf-cfrg-hash-to-curve-07, but
|
||||
// generating a value modulo the group order rather than a field element.
|
||||
//
|
||||
// TODO(https://crbug.com/1414562): Migrate this to the final version.
|
||||
OPENSSL_EXPORT int ec_hash_to_scalar_p384_xmd_sha512_draft07(
|
||||
const EC_GROUP *group, EC_SCALAR *out, const uint8_t *dst, size_t dst_len,
|
||||
const uint8_t *msg, size_t msg_len);
|
||||
|
||||
@@ -92,7 +92,7 @@ int ECDH_compute_key(void *out, size_t out_len, const EC_POINT *pub_key,
|
||||
return -1;
|
||||
}
|
||||
|
||||
EC_RAW_POINT shared_point;
|
||||
EC_JACOBIAN shared_point;
|
||||
uint8_t buf[EC_MAX_BYTES];
|
||||
size_t buf_len;
|
||||
if (!ec_point_mul_scalar(group, &shared_point, &pub_key->raw, priv) ||
|
||||
|
||||
@@ -106,11 +106,15 @@
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
// Ensure we can't call OPENSSL_malloc circularly.
|
||||
#define _BORINGSSL_PROHIBIT_OPENSSL_MALLOC
|
||||
#include <CCryptoBoringSSL_err.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
@@ -129,8 +133,8 @@ OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
struct err_error_st {
|
||||
// file contains the filename where the error occurred.
|
||||
const char *file;
|
||||
// data contains a NUL-terminated string with optional data. It must be freed
|
||||
// with |OPENSSL_free|.
|
||||
// data contains a NUL-terminated string with optional data. It is allocated
|
||||
// with system |malloc| and must be freed with |free| (not |OPENSSL_free|)
|
||||
char *data;
|
||||
// packed contains the error library and reason, as packed by ERR_PACK.
|
||||
uint32_t packed;
|
||||
@@ -142,13 +146,13 @@ struct err_error_st {
|
||||
|
||||
// ERR_STATE contains the per-thread, error queue.
|
||||
typedef struct err_state_st {
|
||||
// errors contains the ERR_NUM_ERRORS most recent errors, organised as a ring
|
||||
// buffer.
|
||||
// errors contains up to ERR_NUM_ERRORS - 1 most recent errors, organised as a
|
||||
// ring buffer.
|
||||
struct err_error_st errors[ERR_NUM_ERRORS];
|
||||
// top contains the index one past the most recent error. If |top| equals
|
||||
// |bottom| then the queue is empty.
|
||||
// top contains the index of the most recent error. If |top| equals |bottom|
|
||||
// then the queue is empty.
|
||||
unsigned top;
|
||||
// bottom contains the index of the last error in the queue.
|
||||
// bottom contains the index before the least recent error in the queue.
|
||||
unsigned bottom;
|
||||
|
||||
// to_free, if not NULL, contains a pointer owned by this structure that was
|
||||
@@ -162,7 +166,7 @@ extern const char kOpenSSLReasonStringData[];
|
||||
|
||||
// err_clear clears the given queued error.
|
||||
static void err_clear(struct err_error_st *error) {
|
||||
OPENSSL_free(error->data);
|
||||
free(error->data);
|
||||
OPENSSL_memset(error, 0, sizeof(struct err_error_st));
|
||||
}
|
||||
|
||||
@@ -170,19 +174,25 @@ static void err_copy(struct err_error_st *dst, const struct err_error_st *src) {
|
||||
err_clear(dst);
|
||||
dst->file = src->file;
|
||||
if (src->data != NULL) {
|
||||
dst->data = OPENSSL_strdup(src->data);
|
||||
// Disable deprecated functions on msvc so it doesn't complain about strdup.
|
||||
OPENSSL_MSVC_PRAGMA(warning(push))
|
||||
OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
|
||||
// We can't use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
|
||||
// which can affect the error stack.
|
||||
dst->data = strdup(src->data);
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
}
|
||||
dst->packed = src->packed;
|
||||
dst->line = src->line;
|
||||
}
|
||||
|
||||
|
||||
// global_next_library contains the next custom library value to return.
|
||||
static int global_next_library = ERR_NUM_LIBS;
|
||||
|
||||
// global_next_library_mutex protects |global_next_library| from concurrent
|
||||
// updates.
|
||||
static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
|
||||
CRYPTO_STATIC_MUTEX_INIT;
|
||||
static CRYPTO_MUTEX global_next_library_mutex = CRYPTO_MUTEX_INIT;
|
||||
|
||||
static void err_state_free(void *statep) {
|
||||
ERR_STATE *state = statep;
|
||||
@@ -194,15 +204,15 @@ static void err_state_free(void *statep) {
|
||||
for (unsigned i = 0; i < ERR_NUM_ERRORS; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->to_free);
|
||||
OPENSSL_free(state);
|
||||
free(state->to_free);
|
||||
free(state);
|
||||
}
|
||||
|
||||
// err_get_state gets the ERR_STATE object for the current thread.
|
||||
static ERR_STATE *err_get_state(void) {
|
||||
ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
|
||||
if (state == NULL) {
|
||||
state = OPENSSL_malloc(sizeof(ERR_STATE));
|
||||
state = malloc(sizeof(ERR_STATE));
|
||||
if (state == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -258,7 +268,10 @@ static uint32_t get_error_values(int inc, int top, const char **file, int *line,
|
||||
} else {
|
||||
*data = error->data;
|
||||
if (flags != NULL) {
|
||||
*flags = ERR_FLAG_STRING;
|
||||
// Without |ERR_FLAG_MALLOCED|, rust-openssl assumes the string has a
|
||||
// static lifetime. In both cases, we retain ownership of the string,
|
||||
// and the caller is not expected to free it.
|
||||
*flags = ERR_FLAG_STRING | ERR_FLAG_MALLOCED;
|
||||
}
|
||||
// If this error is being removed, take ownership of data from
|
||||
// the error. The semantics are such that the caller doesn't
|
||||
@@ -267,7 +280,7 @@ static uint32_t get_error_values(int inc, int top, const char **file, int *line,
|
||||
// error queue.
|
||||
if (inc) {
|
||||
if (error->data != NULL) {
|
||||
OPENSSL_free(state->to_free);
|
||||
free(state->to_free);
|
||||
state->to_free = error->data;
|
||||
}
|
||||
error->data = NULL;
|
||||
@@ -335,7 +348,7 @@ void ERR_clear_error(void) {
|
||||
for (i = 0; i < ERR_NUM_ERRORS; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->to_free);
|
||||
free(state->to_free);
|
||||
state->to_free = NULL;
|
||||
|
||||
state->top = state->bottom = 0;
|
||||
@@ -353,9 +366,9 @@ void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
|
||||
int ERR_get_next_error_library(void) {
|
||||
int ret;
|
||||
|
||||
CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
|
||||
CRYPTO_MUTEX_lock_write(&global_next_library_mutex);
|
||||
ret = global_next_library++;
|
||||
CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex);
|
||||
CRYPTO_MUTEX_unlock_write(&global_next_library_mutex);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -539,22 +552,21 @@ char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
|
||||
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];
|
||||
char lib_buf[32], reason_buf[32];
|
||||
if (lib_str == NULL) {
|
||||
BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
|
||||
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);
|
||||
if (reason_str == NULL) {
|
||||
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
|
||||
int ret = snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
|
||||
packed_error, lib_str, reason_str);
|
||||
if (ret >= 0 && (size_t)ret >= len) {
|
||||
// The output was truncated; make sure we always have 5 colon-separated
|
||||
// fields, i.e. 4 colons.
|
||||
static const unsigned num_colons = 4;
|
||||
unsigned i;
|
||||
@@ -604,8 +616,8 @@ void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
|
||||
}
|
||||
|
||||
ERR_error_string_n(packed_error, buf, sizeof(buf));
|
||||
BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
|
||||
file, line, (flags & ERR_FLAG_STRING) ? data : "");
|
||||
snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf, file,
|
||||
line, (flags & ERR_FLAG_STRING) ? data : "");
|
||||
if (callback(buf2, strlen(buf2), ctx) <= 0) {
|
||||
break;
|
||||
}
|
||||
@@ -629,13 +641,13 @@ static void err_set_error_data(char *data) {
|
||||
struct err_error_st *error;
|
||||
|
||||
if (state == NULL || state->top == state->bottom) {
|
||||
OPENSSL_free(data);
|
||||
free(data);
|
||||
return;
|
||||
}
|
||||
|
||||
error = &state->errors[state->top];
|
||||
|
||||
OPENSSL_free(error->data);
|
||||
free(error->data);
|
||||
error->data = data;
|
||||
}
|
||||
|
||||
@@ -672,48 +684,42 @@ void ERR_put_error(int library, int unused, int reason, const char *file,
|
||||
// concatenates them and sets the result as the data on the most recent
|
||||
// error.
|
||||
static void err_add_error_vdata(unsigned num, va_list args) {
|
||||
size_t alloced, new_len, len = 0, substr_len;
|
||||
char *buf;
|
||||
size_t total_size = 0;
|
||||
const char *substr;
|
||||
unsigned i;
|
||||
char *buf;
|
||||
|
||||
alloced = 80;
|
||||
buf = OPENSSL_malloc(alloced + 1);
|
||||
if (buf == NULL) {
|
||||
va_list args_copy;
|
||||
va_copy(args_copy, args);
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
substr = va_arg(args_copy, const char *);
|
||||
if (substr == NULL) {
|
||||
continue;
|
||||
}
|
||||
size_t substr_len = strlen(substr);
|
||||
if (SIZE_MAX - total_size < substr_len) {
|
||||
return; // Would overflow.
|
||||
}
|
||||
total_size += substr_len;
|
||||
}
|
||||
va_end(args_copy);
|
||||
if (total_size == SIZE_MAX) {
|
||||
return; // Would overflow.
|
||||
}
|
||||
total_size += 1; // NUL terminator.
|
||||
if ((buf = malloc(total_size)) == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
buf[0] = '\0';
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
substr = va_arg(args, const char *);
|
||||
if (substr == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
substr_len = strlen(substr);
|
||||
new_len = len + substr_len;
|
||||
if (new_len > alloced) {
|
||||
char *new_buf;
|
||||
|
||||
if (alloced + 20 + 1 < alloced) {
|
||||
// overflow.
|
||||
OPENSSL_free(buf);
|
||||
return;
|
||||
}
|
||||
|
||||
alloced = new_len + 20;
|
||||
new_buf = OPENSSL_realloc(buf, alloced + 1);
|
||||
if (new_buf == NULL) {
|
||||
OPENSSL_free(buf);
|
||||
return;
|
||||
}
|
||||
buf = new_buf;
|
||||
if (OPENSSL_strlcat(buf, substr, total_size) >= total_size) {
|
||||
assert(0); // should not be possible.
|
||||
}
|
||||
|
||||
OPENSSL_memcpy(buf + len, substr, substr_len);
|
||||
len = new_len;
|
||||
}
|
||||
|
||||
buf[len] = 0;
|
||||
va_end(args);
|
||||
err_set_error_data(buf);
|
||||
}
|
||||
|
||||
@@ -725,21 +731,13 @@ void ERR_add_error_data(unsigned count, ...) {
|
||||
}
|
||||
|
||||
void ERR_add_error_dataf(const char *format, ...) {
|
||||
char *buf = NULL;
|
||||
va_list ap;
|
||||
char *buf;
|
||||
static const unsigned buf_len = 256;
|
||||
|
||||
// A fixed-size buffer is used because va_copy (which would be needed in
|
||||
// order to call vsnprintf twice and measure the buffer) wasn't defined until
|
||||
// C99.
|
||||
buf = OPENSSL_malloc(buf_len + 1);
|
||||
if (buf == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
va_start(ap, format);
|
||||
BIO_vsnprintf(buf, buf_len, format, ap);
|
||||
buf[buf_len] = 0;
|
||||
if (OPENSSL_vasprintf_internal(&buf, format, ap, /*system_malloc=*/1) == -1) {
|
||||
return;
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
err_set_error_data(buf);
|
||||
@@ -751,13 +749,20 @@ void ERR_set_error_data(char *data, int flags) {
|
||||
assert(0);
|
||||
return;
|
||||
}
|
||||
// Disable deprecated functions on msvc so it doesn't complain about strdup.
|
||||
OPENSSL_MSVC_PRAGMA(warning(push))
|
||||
OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
|
||||
// We can not use OPENSSL_strdup because we don't want to call OPENSSL_malloc,
|
||||
// which can affect the error stack.
|
||||
char *copy = strdup(data);
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
if (copy != NULL) {
|
||||
err_set_error_data(copy);
|
||||
}
|
||||
if (flags & ERR_FLAG_MALLOCED) {
|
||||
err_set_error_data(data);
|
||||
} else {
|
||||
char *copy = OPENSSL_strdup(data);
|
||||
if (copy != NULL) {
|
||||
err_set_error_data(copy);
|
||||
}
|
||||
// We can not take ownership of |data| directly because it is allocated with
|
||||
// |OPENSSL_malloc| and we will free it with system |free| later.
|
||||
OPENSSL_free(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -819,8 +824,8 @@ void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
|
||||
for (size_t i = 0; i < state->num_errors; i++) {
|
||||
err_clear(&state->errors[i]);
|
||||
}
|
||||
OPENSSL_free(state->errors);
|
||||
OPENSSL_free(state);
|
||||
free(state->errors);
|
||||
free(state);
|
||||
}
|
||||
|
||||
ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
@@ -829,7 +834,7 @@ ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ERR_SAVE_STATE *ret = OPENSSL_malloc(sizeof(ERR_SAVE_STATE));
|
||||
ERR_SAVE_STATE *ret = malloc(sizeof(ERR_SAVE_STATE));
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -839,9 +844,9 @@ ERR_SAVE_STATE *ERR_save_state(void) {
|
||||
? state->top - state->bottom
|
||||
: ERR_NUM_ERRORS + state->top - state->bottom;
|
||||
assert(num_errors < ERR_NUM_ERRORS);
|
||||
ret->errors = OPENSSL_malloc(num_errors * sizeof(struct err_error_st));
|
||||
ret->errors = malloc(num_errors * sizeof(struct err_error_st));
|
||||
if (ret->errors == NULL) {
|
||||
OPENSSL_free(ret);
|
||||
free(ret);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
|
||||
@@ -860,6 +865,10 @@ void ERR_restore_state(const ERR_SAVE_STATE *state) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state->num_errors >= ERR_NUM_ERRORS) {
|
||||
abort();
|
||||
}
|
||||
|
||||
ERR_STATE *const dst = err_get_state();
|
||||
if (dst == NULL) {
|
||||
return;
|
||||
@@ -868,6 +877,6 @@ void ERR_restore_state(const ERR_SAVE_STATE *state) {
|
||||
for (size_t i = 0; i < state->num_errors; i++) {
|
||||
err_copy(&dst->errors[i], &state->errors[i]);
|
||||
}
|
||||
dst->top = state->num_errors - 1;
|
||||
dst->top = (unsigned)(state->num_errors - 1);
|
||||
dst->bottom = ERR_NUM_ERRORS - 1;
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -85,7 +85,6 @@ EVP_PKEY *EVP_PKEY_new(void) {
|
||||
|
||||
ret = OPENSSL_malloc(sizeof(EVP_PKEY));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ EVP_PKEY *EVP_PKEY_new(void) {
|
||||
static void free_it(EVP_PKEY *pkey) {
|
||||
if (pkey->ameth && pkey->ameth->pkey_free) {
|
||||
pkey->ameth->pkey_free(pkey);
|
||||
pkey->pkey.ptr = NULL;
|
||||
pkey->pkey = NULL;
|
||||
pkey->type = EVP_PKEY_NONE;
|
||||
}
|
||||
}
|
||||
@@ -167,10 +166,21 @@ int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Once set, parameters may not change.
|
||||
if (!EVP_PKEY_missing_parameters(to)) {
|
||||
if (EVP_PKEY_cmp_parameters(to, from) == 1) {
|
||||
return 1;
|
||||
}
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_PARAMETERS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (from->ameth && from->ameth->param_copy) {
|
||||
return from->ameth->param_copy(to, from);
|
||||
}
|
||||
|
||||
// TODO(https://crbug.com/boringssl/536): If the algorithm takes no
|
||||
// parameters, copying them should vacuously succeed.
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -219,6 +229,13 @@ static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
|
||||
}
|
||||
}
|
||||
|
||||
static void evp_pkey_set_method(EVP_PKEY *pkey,
|
||||
const EVP_PKEY_ASN1_METHOD *method) {
|
||||
free_it(pkey);
|
||||
pkey->ameth = method;
|
||||
pkey->type = pkey->ameth->pkey_id;
|
||||
}
|
||||
|
||||
int EVP_PKEY_type(int nid) {
|
||||
const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid);
|
||||
if (meth == NULL) {
|
||||
@@ -236,7 +253,9 @@ int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
|
||||
}
|
||||
|
||||
int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
|
||||
return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
|
||||
evp_pkey_set_method(pkey, &rsa_asn1_meth);
|
||||
pkey->pkey = key;
|
||||
return key != NULL;
|
||||
}
|
||||
|
||||
RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
|
||||
@@ -244,7 +263,7 @@ RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.rsa;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
|
||||
@@ -264,7 +283,9 @@ int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
|
||||
}
|
||||
|
||||
int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
|
||||
return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
|
||||
evp_pkey_set_method(pkey, &dsa_asn1_meth);
|
||||
pkey->pkey = key;
|
||||
return key != NULL;
|
||||
}
|
||||
|
||||
DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
|
||||
@@ -272,7 +293,7 @@ DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.dsa;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
DSA *EVP_PKEY_get1_DSA(const EVP_PKEY *pkey) {
|
||||
@@ -292,7 +313,9 @@ int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
|
||||
}
|
||||
|
||||
int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
|
||||
return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
|
||||
evp_pkey_set_method(pkey, &ec_asn1_meth);
|
||||
pkey->pkey = key;
|
||||
return key != NULL;
|
||||
}
|
||||
|
||||
EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
|
||||
@@ -300,7 +323,7 @@ EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
|
||||
return NULL;
|
||||
}
|
||||
return pkey->pkey.ec;
|
||||
return pkey->pkey;
|
||||
}
|
||||
|
||||
EC_KEY *EVP_PKEY_get1_EC_KEY(const EVP_PKEY *pkey) {
|
||||
@@ -315,21 +338,32 @@ DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) { return NULL; }
|
||||
DH *EVP_PKEY_get1_DH(const EVP_PKEY *pkey) { return NULL; }
|
||||
|
||||
int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
|
||||
if (!EVP_PKEY_set_type(pkey, type)) {
|
||||
return 0;
|
||||
// This function can only be used to assign RSA, DSA, and EC keys. Other key
|
||||
// types have internal representations which are not exposed through the
|
||||
// public API.
|
||||
switch (type) {
|
||||
case EVP_PKEY_RSA:
|
||||
return EVP_PKEY_assign_RSA(pkey, key);
|
||||
case EVP_PKEY_DSA:
|
||||
return EVP_PKEY_assign_DSA(pkey, key);
|
||||
case EVP_PKEY_EC:
|
||||
return EVP_PKEY_assign_EC_KEY(pkey, key);
|
||||
}
|
||||
pkey->pkey.ptr = key;
|
||||
return key != NULL;
|
||||
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
|
||||
ERR_add_error_dataf("algorithm %d", type);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
|
||||
if (pkey && pkey->pkey.ptr) {
|
||||
if (pkey && pkey->pkey) {
|
||||
// This isn't strictly necessary, but historically |EVP_PKEY_set_type| would
|
||||
// clear |pkey| even if |evp_pkey_asn1_find| failed, so we preserve that
|
||||
// behavior.
|
||||
free_it(pkey);
|
||||
}
|
||||
|
||||
ameth = evp_pkey_asn1_find(type);
|
||||
const EVP_PKEY_ASN1_METHOD *ameth = evp_pkey_asn1_find(type);
|
||||
if (ameth == NULL) {
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
|
||||
ERR_add_error_dataf("algorithm %d", type);
|
||||
@@ -337,8 +371,7 @@ int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
|
||||
}
|
||||
|
||||
if (pkey) {
|
||||
pkey->ameth = ameth;
|
||||
pkey->type = pkey->ameth->pkey_id;
|
||||
evp_pkey_set_method(pkey, ameth);
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -419,6 +452,8 @@ int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
|
||||
if (a->ameth && a->ameth->param_cmp) {
|
||||
return a->ameth->param_cmp(a, b);
|
||||
}
|
||||
// TODO(https://crbug.com/boringssl/536): If the algorithm doesn't use
|
||||
// parameters, they should compare as vacuously equal.
|
||||
return -2;
|
||||
}
|
||||
|
||||
@@ -435,7 +470,7 @@ int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **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
|
||||
// rather than reading |pkey->pkey| directly. This avoids problems if our
|
||||
// internal representation does not match the type the caller expects from
|
||||
// OpenSSL.
|
||||
return NULL;
|
||||
|
||||
@@ -336,11 +336,11 @@ EVP_PKEY *d2i_AutoPrivateKey(EVP_PKEY **out, const uint8_t **inp, long len) {
|
||||
int i2d_PublicKey(const EVP_PKEY *key, uint8_t **outp) {
|
||||
switch (key->type) {
|
||||
case EVP_PKEY_RSA:
|
||||
return i2d_RSAPublicKey(key->pkey.rsa, outp);
|
||||
return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(key), outp);
|
||||
case EVP_PKEY_DSA:
|
||||
return i2d_DSAPublicKey(key->pkey.dsa, outp);
|
||||
return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(key), outp);
|
||||
case EVP_PKEY_EC:
|
||||
return i2o_ECPublicKey(key->pkey.ec, outp);
|
||||
return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(key), outp);
|
||||
default:
|
||||
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
|
||||
return -1;
|
||||
|
||||
@@ -88,7 +88,6 @@ static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e,
|
||||
const EVP_PKEY_METHOD *pmeth) {
|
||||
EVP_PKEY_CTX *ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
|
||||
if (!ret) {
|
||||
OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
OPENSSL_memset(ret, 0, sizeof(EVP_PKEY_CTX));
|
||||
|
||||
@@ -66,6 +66,9 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
|
||||
typedef struct evp_pkey_method_st EVP_PKEY_METHOD;
|
||||
|
||||
struct evp_pkey_asn1_method_st {
|
||||
int pkey_id;
|
||||
uint8_t oid[9];
|
||||
@@ -128,6 +131,20 @@ struct evp_pkey_asn1_method_st {
|
||||
void (*pkey_free)(EVP_PKEY *pkey);
|
||||
} /* EVP_PKEY_ASN1_METHOD */;
|
||||
|
||||
struct evp_pkey_st {
|
||||
CRYPTO_refcount_t references;
|
||||
|
||||
// type contains one of the EVP_PKEY_* values or NID_undef and determines
|
||||
// the type of |pkey|.
|
||||
int type;
|
||||
|
||||
// pkey contains a pointer to a structure dependent on |type|.
|
||||
void *pkey;
|
||||
|
||||
// ameth contains a pointer to a method table that contains many ASN.1
|
||||
// methods for the key type.
|
||||
const EVP_PKEY_ASN1_METHOD *ameth;
|
||||
} /* EVP_PKEY */;
|
||||
|
||||
#define EVP_PKEY_OP_UNDEFINED 0
|
||||
#define EVP_PKEY_OP_KEYGEN (1 << 2)
|
||||
|
||||
Loaded 100 of 589 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user