Files
3405691582andCory Benfield 041199614e OpenBSD support. (#349)
OpenBSD support.

### 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 (_not necessary_)

#### If you've made changes to `gyb` files
- [X] I've run `.script/generate_boilerplate_files_with_gyb` and
included updated generated files in a commit of this pull request (_not
applicable_)

### Motivation:

swift-crypto is a required dependency for swift-package-manager, and
must build and run on OpenBSD.

### Modifications:

    OpenBSD support for cmake/swiftpm.
    
* Add to patterns for CMakeLists. Also provide a helpful error so future
porters have a slightly better idea of what might need to be updated.
    
* Proactively reverse sense in some system name checks so they read as
"if not Darwin". This is likely safer to do here than elsewhere since
there are likely not going to be other platforms for which swift-crypto
      already exists.
    
    * Add to Package.swift

    ManagedBuffer.capacity is unavailable on OpenBSD.
    
    ManagedBuffer.capacity depends on malloc introspection (e.g.,
`malloc_size`), which is not available on this platform -- and is thus
    marked as such.
    
    Here, SecureBytes uses a ManagedBuffer in Backing. We have capacity
    tracked in the BackingHeader, and most of the references to the
ManagedBuffer.capacity property can be replaced with references to the
capacity in the header. However, since capacity in the header is marked
as internal which conflicts with some of the @inline functions, so we
    must change those to @usableFromInline for the platform as well.

    Add conditional for RandomBytes.

    Required for tests to build on OpenBSD.

### Result:

swift-crypto builds and runs on OpenBSD.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-04-17 14:46:07 +01:00

59 lines
2.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension UnsafeMutableRawBufferPointer {
@inlinable
package func initializeWithRandomBytes(count: Int) {
guard count > 0 else {
return
}
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) || os(FreeBSD) || os(OpenBSD)
var rng = SystemRandomNumberGenerator()
precondition(count <= self.count)
// We store bytes 64-bits at a time until we can't anymore.
var targetPtr = self
while targetPtr.count > 8 {
targetPtr.storeBytes(of: rng.next(), as: UInt64.self)
targetPtr = UnsafeMutableRawBufferPointer(rebasing: targetPtr[8...])
}
// Now we're down to having to store things an integer at a time. We do this by shifting and
// masking.
var remainingWord: UInt64 = rng.next()
while targetPtr.count > 0 {
targetPtr.storeBytes(of: UInt8(remainingWord & 0xFF), as: UInt8.self)
remainingWord >>= 8
targetPtr = UnsafeMutableRawBufferPointer(rebasing: targetPtr[1...])
}
#else
fatalError("No secure random number generator on this platform.")
#endif
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SystemRandomNumberGenerator {
@inlinable
package static func randomBytes(count: Int) -> [UInt8] {
Array(unsafeUninitializedCapacity: count) { buffer, initializedCount in
UnsafeMutableRawBufferPointer(start: buffer.baseAddress, count: buffer.count)
.initializeWithRandomBytes(count: count)
initializedCount = count
}
}
}