Files
swift-crypto/Tests/_CryptoExtrasTests/TestRSABlindSigningAPI.swift
T
Si Beaumont fdfdc11707 extras: Add APIs for RSA Blind Signatures (#232)
* 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
2024-06-26 13:20:43 +01:00

60 lines
2.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
import XCTest
import Crypto
import _CryptoExtras // NOTE: No @testable import, because we want to test the public API.
final class TestRSABlindSigningAPI: XCTestCase {
func testEndToEnd() throws {
let allNamedRFC9474Variants: [_RSA.BlindSigning.Parameters] = [
.RSABSSA_SHA384_PSSZERO_Deterministic,
.RSABSSA_SHA384_PSSZERO_Randomized,
.RSABSSA_SHA384_PSS_Deterministic,
.RSABSSA_SHA384_PSS_Randomized,
]
let keySizes: [_RSA.Signing.KeySize] = [
.bits2048,
.bits3072,
.bits4096,
]
for parameters in allNamedRFC9474Variants {
for keySize in keySizes {
// [Issuer] Create key-pair (other initializers are available).
let privateKey = try _RSA.BlindSigning.PrivateKey(keySize: keySize, parameters: parameters)
// [Client] Create public key (other initializers are available).
let publicKey = privateKey.publicKey
// [Client] Have a message they wish to use.
let message = Data("This is some input data".utf8)
// [Client] Prepare the message.
let preparedMessage = publicKey.prepare(message)
// [Client] Blind the message to send to the server and get its blinding inverse.
let blindingResult = try publicKey.blind(preparedMessage)
// [Issuer] Blind sign, construting the blinded message from the bytes received from the client.
let blindSignature = try privateKey.blindSignature(for: blindingResult.blindedMessage)
// [Client] Finalize using the blind inverse to unblind the signature.
let unblindedSignature = try publicKey.finalize(blindSignature, for: preparedMessage, blindingInverse: blindingResult.inverse)
// [Verifier] Verify the unblinded signature.
XCTAssert(publicKey.isValidSignature(unblindedSignature, for: preparedMessage))
}
}
}
}