mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
* API for RSA blind signing and verification from RFC 9474 * Add blind signing backend for Security framework * Hide PSSZERO variants from public API for now * Update CMakeLists.txt * Verify signature produced within SecurityRSAPrivateKey.blindSignature(_:) * Update API: bake params into key, use wrappers for intermidiate values * Throw specific error when message too large for key modulus * Drop Security framework backend and support all variants * Factor out withNewBignumContext helper * Align parameter names and labels in internal functions with public API * Fix typo in doc comments: public -> private * Implement client operations for RSA Blind Signatures * Remove the BlindedMessage type * Move prepare function to public key method * Throw (internal) errors defined in the RFC * Fix test function names for Linux test discovery * Factor saltLength into a computed property on Parameters * Improve allocations for temporary bytes * Fixup documentation typos * Parameters are Sendable, regardless of H type parameter * PublicKey and PrivateKey are also Sendable, regardless of H * Wrap internal errors in most appropriate CryptoKitError * Factor code differently * Tweak API to return BlindingResult nominal type instead of tuple * Prefer withUnsafeTemporaryAllocation(of:capacity:_:) to Array.init(unsafeUnitializedCapacity:_:) * Move unsafe temporary allocations into BlindSigningHelpers methods * Replace hand-rolled BN ops with ArbitraryPrecisionInteger and FFAC * Fixup placement of paren in guards with closures * Replace closure invocation let binding with looped var * Use ERR_peek_last_error, not ERR_get_error
56 lines
2.0 KiB
Swift
56 lines
2.0 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.md for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
extension UnsafeMutableRawBufferPointer {
|
|
@inlinable
|
|
func initializeWithRandomBytes(count: Int) {
|
|
guard count > 0 else {
|
|
return
|
|
}
|
|
|
|
#if canImport(Darwin) || os(Linux) || os(Android) || os(Windows)
|
|
var rng = SystemRandomNumberGenerator()
|
|
#else
|
|
fatalError("No secure random number generator on this platform.")
|
|
#endif
|
|
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...])
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SystemRandomNumberGenerator {
|
|
@inlinable
|
|
static func randomBytes(count: Int) -> [UInt8] {
|
|
Array(unsafeUninitializedCapacity: count) { buffer, initializedCount in
|
|
UnsafeMutableRawBufferPointer(start: buffer.baseAddress, count: buffer.count).initializeWithRandomBytes(count: count)
|
|
initializedCount = count
|
|
}
|
|
}
|
|
}
|