Files
Gwynne Raskind c661debfc2 Update BoringSSL to 0226f30467f540a3f62ef48d453f93927da199b6 (#406)
Fix multiple issues in the BoringSSL vendoring script and update
BoringSSL to 0226f30467f540a3f62ef48d453f93927da199b6

### Checklist
- [x] I've run tests to see all new and existing tests pass
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [x] I've updated the documentation if necessary

### Motivation:

The `vendor-boringssl.sh` script is currently broken in a number of
ways, both in general and relative to the latest BoringSSL version:

- `--enable-test-discovery` is still in use
- The Linux Swift SDKs being generated are out of date (5.10-jammy)
- The script does not prefix exported C++ symbols
- Invoking the script without a `BORINGSSL_REVISION` results in an
unconditional error
- The `PATTERNS` list is out of date for the latest BoringSSL
- The latest BoringSSL contains a bug which fails to apply `extern "C"`
to two exported symbols, allowing for collisions.

In addition, the latest BoringSSL has made `BN_MONT_CTX` opaque,
necessitating that it be referenced with `OpaquePointer` rather than
`UnsafePointer<>`.

### Modifications:

The following changes are included:

- `--enable-test-discovery` has been removed
- `vendor-boringssl.sh` and `generate-linux-sdks.sh` have been updated
to use Swift 6.1.2 SDKs built for Ubuntu Noble
- The `mangle_cpp_structures` function from `swift-nio-ssl`'s version
of `vendor-boringssl.sh` has been copied over (thanks @Lukasa!)
- `BORINGSSL_REVISION` is now allowed to be an empty string
- The `PATTERNS` array has been updated
- A patch has been added which applies `extern "C"` to the functions
which should have it
- `s/UnsafePointer<BN_MONT_CTX>/OpaquePointer/g`
- Updated `CMakeLists.txt` per `update-cmake-lists.sh`
- Update BoringSSL to
`0226f30467f540a3f62ef48d453f93927da199b6`

### Result:

Vendoring the latest BoringSSL will work again.
The latest BoringSSL is vendored.
2025-09-15 15:21:20 +01:00

88 lines
2.5 KiB
C++

// Copyright 2024 The BoringSSL Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <CCryptoBoringSSL_sha2.h>
#include <CCryptoBoringSSL_mem.h>
#include "../fipsmodule/bcm_interface.h"
int SHA224_Init(SHA256_CTX *sha) {
BCM_sha224_init(sha);
return 1;
}
int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len) {
BCM_sha224_update(sha, data, len);
return 1;
}
int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], SHA256_CTX *sha) {
BCM_sha224_final(out, sha);
return 1;
}
uint8_t *SHA224(const uint8_t *data, size_t len,
uint8_t out[SHA224_DIGEST_LENGTH]) {
SHA256_CTX ctx;
BCM_sha224_init(&ctx);
BCM_sha224_update(&ctx, data, len);
BCM_sha224_final(out, &ctx);
OPENSSL_cleanse(&ctx, sizeof(ctx));
return out;
}
int SHA256_Init(SHA256_CTX *sha) {
BCM_sha256_init(sha);
return 1;
}
int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len) {
BCM_sha256_update(sha, data, len);
return 1;
}
int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], SHA256_CTX *sha) {
// TODO(bbe): This overflow check one of the few places a low-level hash
// 'final' function can fail. SHA-512 does not have a corresponding check.
// The BCM function is infallible and will abort if this is done incorrectly.
// we should verify nothing crashes with this removed and eliminate the 0
// return.
if (sha->md_len > SHA256_DIGEST_LENGTH) {
return 0;
}
BCM_sha256_final(out, sha);
return 1;
}
uint8_t *SHA256(const uint8_t *data, size_t len,
uint8_t out[SHA256_DIGEST_LENGTH]) {
SHA256_CTX ctx;
BCM_sha256_init(&ctx);
BCM_sha256_update(&ctx, data, len);
BCM_sha256_final(out, &ctx);
OPENSSL_cleanse(&ctx, sizeof(ctx));
return out;
}
void SHA256_Transform(SHA256_CTX *sha, const uint8_t block[SHA256_CBLOCK]) {
BCM_sha256_transform(sha, block);
}
void SHA256_TransformBlocks(uint32_t state[8], const uint8_t *data,
size_t num_blocks) {
BCM_sha256_transform_blocks(state, data, num_blocks);
}