mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ffca28be3c | ||
|
|
35703579f6 | ||
|
|
a24950b8aa | ||
|
|
e8f2ddd49e | ||
|
|
55201a8f83 |
No files matched your search
+23
-21
@@ -1,4 +1,4 @@
|
||||
// swift-tools-version:5.8
|
||||
// swift-tools-version:5.9
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftCrypto open source project
|
||||
@@ -57,6 +57,18 @@ if development {
|
||||
]
|
||||
}
|
||||
|
||||
// This doesn't work when cross-compiling: the privacy manifest will be included in the Bundle and
|
||||
// Foundation will be linked. This is, however, strictly better than unconditionally adding the
|
||||
// resource.
|
||||
#if canImport(Darwin)
|
||||
let privacyManifestExclude: [String] = []
|
||||
let privacyManifestResource: [PackageDescription.Resource] = [.copy("PrivacyInfo.xcprivacy")]
|
||||
#else
|
||||
// Exclude on other platforms to avoid build warnings.
|
||||
let privacyManifestExclude: [String] = ["PrivacyInfo.xcprivacy"]
|
||||
let privacyManifestResource: [PackageDescription.Resource] = []
|
||||
#endif
|
||||
|
||||
let package = Package(
|
||||
name: "swift-crypto",
|
||||
platforms: [
|
||||
@@ -76,7 +88,7 @@ let package = Package(
|
||||
targets: [
|
||||
.target(
|
||||
name: "CCryptoBoringSSL",
|
||||
exclude: [
|
||||
exclude: privacyManifestExclude + [
|
||||
"hash.txt",
|
||||
"include/boringssl_prefix_symbols_nasm.inc",
|
||||
"CMakeLists.txt",
|
||||
@@ -88,9 +100,7 @@ let package = Package(
|
||||
"crypto/bio/socket_helper.c",
|
||||
"crypto/bio/socket.c"
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
resources: privacyManifestResource,
|
||||
cSettings: [
|
||||
// These defines come from BoringSSL's build system
|
||||
.define("_HAS_EXCEPTIONS", to: "0", .when(platforms: [Platform.windows])),
|
||||
@@ -107,26 +117,22 @@ let package = Package(
|
||||
.target(
|
||||
name: "CCryptoBoringSSLShims",
|
||||
dependencies: ["CCryptoBoringSSL"],
|
||||
exclude: [
|
||||
exclude: privacyManifestExclude + [
|
||||
"CMakeLists.txt"
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
]
|
||||
resources: privacyManifestResource
|
||||
),
|
||||
.target(
|
||||
name: "Crypto",
|
||||
dependencies: dependencies,
|
||||
exclude: [
|
||||
exclude: privacyManifestExclude + [
|
||||
"CMakeLists.txt",
|
||||
"AEADs/Nonces.swift.gyb",
|
||||
"Digests/Digests.swift.gyb",
|
||||
"Key Agreement/ECDH.swift.gyb",
|
||||
"Signatures/ECDSA.swift.gyb",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
resources: privacyManifestResource,
|
||||
swiftSettings: swiftSettings + [.define("MODULE_IS_CRYPTO")]
|
||||
),
|
||||
.target(
|
||||
@@ -137,12 +143,10 @@ let package = Package(
|
||||
"CryptoBoringWrapper",
|
||||
"Crypto"
|
||||
],
|
||||
exclude: [
|
||||
exclude: privacyManifestExclude + [
|
||||
"CMakeLists.txt",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
resources: privacyManifestResource,
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
.target(
|
||||
@@ -151,12 +155,10 @@ let package = Package(
|
||||
"CCryptoBoringSSL",
|
||||
"CCryptoBoringSSLShims"
|
||||
],
|
||||
exclude: [
|
||||
exclude: privacyManifestExclude + [
|
||||
"CMakeLists.txt",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
]
|
||||
resources: privacyManifestResource
|
||||
),
|
||||
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
.testTarget(
|
||||
|
||||
@@ -117,7 +117,8 @@ Swift Crypto | Minimum Swift Version
|
||||
`2.2.0 ..< 2.4.2` | 5.5
|
||||
`2.4.2 ..< 3.1.0` | 5.6
|
||||
`3.1.0 ..< 3.3.0` | 5.7
|
||||
`3.3.0 ...` | 5.8
|
||||
`3.3.0 ..< 3.8.0` | 5.8
|
||||
`3.8.0 ...` | 5.9
|
||||
|
||||
### Compatibility
|
||||
|
||||
|
||||
@@ -33,8 +33,10 @@ public enum CryptoKitError: Error {
|
||||
case invalidParameter
|
||||
}
|
||||
|
||||
extension CryptoKitError: Equatable, Hashable {}
|
||||
|
||||
/// Errors from decoding ASN.1 content.
|
||||
public enum CryptoKitASN1Error: Error {
|
||||
public enum CryptoKitASN1Error: Equatable, Error, Hashable {
|
||||
/// The ASN.1 tag for this field is invalid or unsupported.
|
||||
case invalidFieldIdentifier
|
||||
|
||||
|
||||
@@ -405,6 +405,13 @@ extension ArbitraryPrecisionInteger: SignedNumeric {
|
||||
// MARK: - Other arithmetic operations
|
||||
|
||||
extension ArbitraryPrecisionInteger {
|
||||
@usableFromInline
|
||||
var trailingZeroBitCount: Int32 {
|
||||
self.withUnsafeBignumPointer {
|
||||
CCryptoBoringSSL_BN_count_low_zero_bits($0)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
static func gcd(_ a: ArbitraryPrecisionInteger, _ b: ArbitraryPrecisionInteger) throws -> ArbitraryPrecisionInteger {
|
||||
var result = ArbitraryPrecisionInteger()
|
||||
@@ -443,6 +450,46 @@ extension ArbitraryPrecisionInteger {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
static func >> (lhs: ArbitraryPrecisionInteger, rhs: Int32) -> ArbitraryPrecisionInteger {
|
||||
var result = ArbitraryPrecisionInteger()
|
||||
|
||||
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
|
||||
lhs.withUnsafeBignumPointer { lhsPtr in
|
||||
CCryptoBoringSSL_BN_rshift(resultPtr, lhsPtr, rhs)
|
||||
}
|
||||
}
|
||||
|
||||
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
static func / (lhs: ArbitraryPrecisionInteger, rhs: ArbitraryPrecisionInteger) -> ArbitraryPrecisionInteger {
|
||||
var result = ArbitraryPrecisionInteger()
|
||||
|
||||
let rc = result.withUnsafeMutableBignumPointer { resultPtr in
|
||||
lhs.withUnsafeBignumPointer { lhsPtr in
|
||||
rhs.withUnsafeBignumPointer { rhsPtr in
|
||||
ArbitraryPrecisionInteger.withUnsafeBN_CTX { bnCtx in
|
||||
CCryptoBoringSSL_BN_div(resultPtr, nil, lhsPtr, rhsPtr, bnCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
precondition(rc == 1, "Unable to allocate memory for new ArbitraryPrecisionInteger")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
var isEven: Bool {
|
||||
self.withUnsafeBignumPointer {
|
||||
CCryptoBoringSSL_BN_is_odd($0) == 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Serializing
|
||||
|
||||
@@ -16,11 +16,7 @@ import Crypto
|
||||
@_implementationOnly import CCryptoBoringSSL
|
||||
@_implementationOnly import CCryptoBoringSSLShims
|
||||
@_implementationOnly import CryptoBoringWrapper
|
||||
#if canImport(Darwin) || swift(>=5.9.1)
|
||||
import Foundation
|
||||
#else
|
||||
@preconcurrency import Foundation
|
||||
#endif
|
||||
|
||||
/// Types associated with the AES GCM SIV algorithm
|
||||
extension AES.GCM {
|
||||
|
||||
@@ -16,11 +16,7 @@
|
||||
@_implementationOnly import CCryptoBoringSSLShims
|
||||
import Crypto
|
||||
@_implementationOnly import CryptoBoringWrapper
|
||||
#if canImport(Darwin) || swift(>=5.9.1)
|
||||
import Foundation
|
||||
#else
|
||||
@preconcurrency import Foundation
|
||||
#endif
|
||||
|
||||
typealias ChaCha20CTRImpl = OpenSSLChaCha20CTRImpl
|
||||
|
||||
|
||||
@@ -26,6 +26,16 @@ extension _RSA.BlindSigning {
|
||||
public struct PublicKey<H: HashFunction>: Sendable {
|
||||
public typealias Parameters = _RSA.BlindSigning.Parameters<H>
|
||||
|
||||
public struct Primitives: Sendable, Hashable {
|
||||
public var modulus: Data
|
||||
public var publicExponent: Data
|
||||
|
||||
public init(modulus: Data, publicExponent: Data) {
|
||||
self.modulus = modulus
|
||||
self.publicExponent = publicExponent
|
||||
}
|
||||
}
|
||||
|
||||
private var backing: BackingPublicKey
|
||||
private let parameters: Parameters
|
||||
|
||||
@@ -113,6 +123,11 @@ extension _RSA.BlindSigning {
|
||||
self.backing = backing
|
||||
self.parameters = parameters
|
||||
}
|
||||
|
||||
public func getKeyPrimitives() throws -> Primitives {
|
||||
let (n, e) = try self.backing.getKeyPrimitives()
|
||||
return Primitives(modulus: n, publicExponent: e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +242,34 @@ extension _RSA.BlindSigning {
|
||||
public var publicKey: _RSA.BlindSigning.PublicKey<H> {
|
||||
_RSA.BlindSigning.PublicKey(self.backing.publicKey, self.parameters)
|
||||
}
|
||||
|
||||
/// Construct a private key with the specified parameters.
|
||||
///
|
||||
/// The use of this API is strongly discouraged for performance reasons,
|
||||
/// as it requires the factorization of the modulus, which is resource-intensive.
|
||||
/// It is recommended to use the other initializers to construct a private key,
|
||||
/// unless you have only the modulus, public exponent, and private exponent
|
||||
/// to construct the key.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - n: modulus of the key
|
||||
/// - e: public exponent of the key
|
||||
/// - d: private exponent of the key
|
||||
/// - parameters: parameters used in the blind signing protocol
|
||||
public static func _createFromNumbers(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes, parameters: Parameters) throws -> Self {
|
||||
let (p, q) = try _RSA.extractPrimeFactors(
|
||||
n: try ArbitraryPrecisionInteger(bytes: n),
|
||||
e: try ArbitraryPrecisionInteger(bytes: e),
|
||||
d: try ArbitraryPrecisionInteger(bytes: d)
|
||||
)
|
||||
|
||||
return try Self.init(
|
||||
n: n, e: e, d: d,
|
||||
p: try Data(bytesOf: p, paddedToSize: p.byteCount),
|
||||
q: try Data(bytesOf: q, paddedToSize: q.byteCount),
|
||||
parameters: parameters
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,12 +31,8 @@ fileprivate typealias BackingPrivateKey = BoringSSLRSAPrivateKey
|
||||
///
|
||||
/// When rolling out new cryptosystems, users should avoid RSA and use ECDSA or edDSA instead. RSA
|
||||
/// support is provided for interoperability with legacy systems.
|
||||
#if swift(>=5.8)
|
||||
@_documentation(visibility: public)
|
||||
public enum _RSA { }
|
||||
#else
|
||||
public enum _RSA { }
|
||||
#endif
|
||||
|
||||
extension _RSA {
|
||||
public enum Signing { }
|
||||
@@ -45,6 +41,16 @@ extension _RSA {
|
||||
|
||||
extension _RSA.Signing {
|
||||
public struct PublicKey: Sendable {
|
||||
public struct Primitives: Sendable, Hashable {
|
||||
public var modulus: Data
|
||||
public var publicExponent: Data
|
||||
|
||||
public init(modulus: Data, publicExponent: Data) {
|
||||
self.modulus = modulus
|
||||
self.publicExponent = publicExponent
|
||||
}
|
||||
}
|
||||
|
||||
private var backing: BackingPublicKey
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
@@ -132,6 +138,11 @@ extension _RSA.Signing {
|
||||
fileprivate init(_ backing: BackingPublicKey) {
|
||||
self.backing = backing
|
||||
}
|
||||
|
||||
public func getKeyPrimitives() throws -> Primitives {
|
||||
let (n, e) = try self.backing.getKeyPrimitives()
|
||||
return Primitives(modulus: n, publicExponent: e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,6 +253,32 @@ extension _RSA.Signing {
|
||||
public var publicKey: _RSA.Signing.PublicKey {
|
||||
_RSA.Signing.PublicKey(self.backing.publicKey)
|
||||
}
|
||||
|
||||
/// Construct a private key with the specified parameters.
|
||||
///
|
||||
/// The use of this API is strongly discouraged for performance reasons,
|
||||
/// as it requires the factorization of the modulus, which is resource-intensive.
|
||||
/// It is recommended to use the other initializers to construct a private key,
|
||||
/// unless you have only the modulus, public exponent, and private exponent
|
||||
/// to construct the key.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - n: modulus of the key
|
||||
/// - e: public exponent of the key
|
||||
/// - d: private exponent of the key
|
||||
public static func _createFromNumbers(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes) throws -> Self {
|
||||
let (p, q) = try _RSA.extractPrimeFactors(
|
||||
n: try ArbitraryPrecisionInteger(bytes: n),
|
||||
e: try ArbitraryPrecisionInteger(bytes: e),
|
||||
d: try ArbitraryPrecisionInteger(bytes: d)
|
||||
)
|
||||
|
||||
return try Self.init(
|
||||
n: n, e: e, d: d,
|
||||
p: try Data(bytesOf: p, paddedToSize: p.byteCount),
|
||||
q: try Data(bytesOf: q, paddedToSize: q.byteCount)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -426,6 +463,16 @@ extension _RSA.Signing {
|
||||
extension _RSA.Encryption {
|
||||
/// Identical to ``_RSA/Signing/PublicKey``.
|
||||
public struct PublicKey {
|
||||
public struct Primitives: Sendable, Hashable {
|
||||
public var modulus: Data
|
||||
public var publicExponent: Data
|
||||
|
||||
public init(modulus: Data, publicExponent: Data) {
|
||||
self.modulus = modulus
|
||||
self.publicExponent = publicExponent
|
||||
}
|
||||
}
|
||||
|
||||
private var backing: BackingPublicKey
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
@@ -483,6 +530,11 @@ extension _RSA.Encryption {
|
||||
public var pemRepresentation: String { self.backing.pemRepresentation }
|
||||
public var keySizeInBits: Int { self.backing.keySizeInBits }
|
||||
fileprivate init(_ backing: BackingPublicKey) { self.backing = backing }
|
||||
|
||||
public func getKeyPrimitives() throws -> Primitives {
|
||||
let (n, e) = try self.backing.getKeyPrimitives()
|
||||
return Primitives(modulus: n, publicExponent: e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Identical to ``_RSA/Signing/PrivateKey``.
|
||||
@@ -563,6 +615,32 @@ extension _RSA.Encryption {
|
||||
public var pkcs8PEMRepresentation: String { self.backing.pkcs8PEMRepresentation }
|
||||
public var keySizeInBits: Int { self.backing.keySizeInBits }
|
||||
public var publicKey: _RSA.Encryption.PublicKey { .init(self.backing.publicKey) }
|
||||
|
||||
/// Construct a private key with the specified parameters.
|
||||
///
|
||||
/// The use of this API is strongly discouraged for performance reasons,
|
||||
/// as it requires the factorization of the modulus, which is resource-intensive.
|
||||
/// It is recommended to use the other initializers to construct a private key,
|
||||
/// unless you have only the modulus, public exponent, and private exponent
|
||||
/// to construct the key.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - n: modulus of the key
|
||||
/// - e: public exponent of the key
|
||||
/// - d: private exponent of the key
|
||||
public static func _createFromNumbers(n: some ContiguousBytes, e: some ContiguousBytes, d: some ContiguousBytes) throws -> Self {
|
||||
let (p, q) = try _RSA.extractPrimeFactors(
|
||||
n: try ArbitraryPrecisionInteger(bytes: n),
|
||||
e: try ArbitraryPrecisionInteger(bytes: e),
|
||||
d: try ArbitraryPrecisionInteger(bytes: d)
|
||||
)
|
||||
|
||||
return try Self.init(
|
||||
n: n, e: e, d: d,
|
||||
p: try Data(bytesOf: p, paddedToSize: p.byteCount),
|
||||
q: try Data(bytesOf: q, paddedToSize: q.byteCount)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,3 +717,60 @@ extension _RSA {
|
||||
|
||||
static let SPKIPublicKeyType = "PUBLIC KEY"
|
||||
}
|
||||
|
||||
extension _RSA {
|
||||
static func extractPrimeFactors(
|
||||
n: ArbitraryPrecisionInteger,
|
||||
e: ArbitraryPrecisionInteger,
|
||||
d: ArbitraryPrecisionInteger
|
||||
) throws -> (p: ArbitraryPrecisionInteger, q: ArbitraryPrecisionInteger) {
|
||||
// This is based on the proof of fact 1 in https://www.ams.org/notices/199902/boneh.pdf
|
||||
let k = (d * e) - 1
|
||||
let t = k.trailingZeroBitCount
|
||||
let r = k >> t
|
||||
|
||||
guard k.isEven else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
var y: ArbitraryPrecisionInteger = 0
|
||||
var i = 1
|
||||
|
||||
let context = try FiniteFieldArithmeticContext(fieldSize: n)
|
||||
|
||||
while i <= 100 {
|
||||
let g = try ArbitraryPrecisionInteger.random(inclusiveMin: 2, exclusiveMax: n)
|
||||
y = try context.pow(g, r)
|
||||
|
||||
guard y != 1, y != n - 1 else {
|
||||
continue
|
||||
}
|
||||
|
||||
var j = 1
|
||||
var x: ArbitraryPrecisionInteger
|
||||
|
||||
while j <= t &- 1 {
|
||||
x = try context.pow(y, 2)
|
||||
|
||||
guard x != 1, x != n - 1 else {
|
||||
break
|
||||
}
|
||||
|
||||
y = x
|
||||
j &+= 1
|
||||
}
|
||||
|
||||
x = try context.pow(y, 2)
|
||||
if x == 1 {
|
||||
let p = try ArbitraryPrecisionInteger.gcd(y - 1, n)
|
||||
let q = n / p
|
||||
|
||||
return (p, q)
|
||||
}
|
||||
|
||||
i &+= 1
|
||||
}
|
||||
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
@@ -60,6 +60,10 @@ internal struct BoringSSLRSAPublicKey: Sendable {
|
||||
fileprivate init(_ backing: Backing) {
|
||||
self.backing = backing
|
||||
}
|
||||
|
||||
func getKeyPrimitives() throws -> (n: Data, e: Data) {
|
||||
try self.backing.getKeyPrimitives()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -465,6 +469,22 @@ extension BoringSSLRSAPublicKey {
|
||||
deinit {
|
||||
CCryptoBoringSSL_EVP_PKEY_free(self.pointer)
|
||||
}
|
||||
|
||||
fileprivate func getKeyPrimitives() -> (n: Data, e: Data) {
|
||||
let key = CCryptoBoringSSL_EVP_PKEY_get0_RSA(self.pointer)
|
||||
|
||||
func getPrimitive(_ getPointer: (OpaquePointer?) -> UnsafePointer<BIGNUM>?) -> Data {
|
||||
let ptr = getPointer(key)
|
||||
let size = Int(CCryptoBoringSSL_BN_num_bytes(ptr))
|
||||
var data = Data(count: size)
|
||||
data.withUnsafeMutableBytes { dataPtr in
|
||||
_ = CCryptoBoringSSL_BN_bn2bin(ptr, dataPtr.baseAddress)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
return (getPrimitive(CCryptoBoringSSL_RSA_get0_n), getPrimitive(CCryptoBoringSSL_RSA_get0_e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,10 @@ internal struct SecurityRSAPublicKey: @unchecked Sendable {
|
||||
fileprivate init(_ backing: SecKey) {
|
||||
self.backing = backing
|
||||
}
|
||||
|
||||
func getKeyPrimitives() throws -> (n: Data, e: Data) {
|
||||
try BoringSSLRSAPublicKey(derRepresentation: self.derRepresentation).getKeyPrimitives()
|
||||
}
|
||||
}
|
||||
|
||||
// unchecked sendable until `SecKey` gets sendable annotations
|
||||
|
||||
@@ -202,4 +202,37 @@ final class TestRSABlindSigning: XCTestCase {
|
||||
parameters: .RSABSSA_SHA384_PSS_Randomized
|
||||
)
|
||||
}
|
||||
|
||||
func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws {
|
||||
let data = Array("hello, world!".utf8)
|
||||
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let key = try _RSA.BlindSigning.PrivateKey._createFromNumbers(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d),
|
||||
parameters: testVector.parameters
|
||||
)
|
||||
|
||||
let preparedMessage = key.publicKey.prepare(data)
|
||||
let blindedMessage = try key.publicKey.blind(preparedMessage)
|
||||
let blindSignature = try key.blindSignature(for: blindedMessage.blindedMessage)
|
||||
let signature = try key.publicKey.finalize(blindSignature, for: preparedMessage, blindingInverse: blindedMessage.inverse)
|
||||
XCTAssert(key.publicKey.isValidSignature(signature, for: preparedMessage))
|
||||
}
|
||||
}
|
||||
|
||||
func testGetKeyPrimitives() throws {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let n = try Data(hexString: testVector.n)
|
||||
let e = try Data(hexString: testVector.e)
|
||||
|
||||
let primitives = try _RSA.BlindSigning.PublicKey(
|
||||
n: n, e: e,
|
||||
parameters: testVector.parameters
|
||||
).getKeyPrimitives()
|
||||
XCTAssertEqual(primitives.modulus, n)
|
||||
XCTAssertEqual(primitives.publicExponent, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,6 +108,34 @@ final class TestRSAEncryption: XCTestCase {
|
||||
e: bytesValues.randomElement()!
|
||||
)
|
||||
}
|
||||
|
||||
func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws {
|
||||
let data = Array("hello, world!".utf8)
|
||||
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let key = try _RSA.Encryption.PrivateKey._createFromNumbers(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d)
|
||||
)
|
||||
|
||||
let encrypted = try key.publicKey.encrypt(data, padding: .PKCS1_OAEP_SHA256)
|
||||
let decrypted = try key.decrypt(encrypted, padding: .PKCS1_OAEP_SHA256)
|
||||
|
||||
XCTAssertEqual(Data(data), decrypted)
|
||||
}
|
||||
}
|
||||
|
||||
func testGetKeyPrimitives() throws {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let n = try Data(hexString: testVector.n)
|
||||
let e = try Data(hexString: testVector.e)
|
||||
|
||||
let primitives = try _RSA.Encryption.PublicKey(n: n, e: e).getKeyPrimitives()
|
||||
XCTAssertEqual(primitives.modulus, n)
|
||||
XCTAssertEqual(primitives.publicExponent, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RSAEncryptionOAEPTestGroup: Codable {
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
import Foundation
|
||||
import XCTest
|
||||
@testable import Crypto
|
||||
import _CryptoExtras
|
||||
import Crypto
|
||||
@testable import _CryptoExtras
|
||||
|
||||
final class TestRSASigning: XCTestCase {
|
||||
func test_wycheproofPKCS1Vectors() throws {
|
||||
@@ -73,6 +73,15 @@ final class TestRSASigning: XCTestCase {
|
||||
testFunction: self.testPSSGroup)
|
||||
}
|
||||
|
||||
func test_wycheproofPrimitives() throws {
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_oaep_2048_sha1_mgf1sha1_test",
|
||||
testFunction: self.testPrimeFactors)
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_oaep_2048_sha256_mgf1sha256_test",
|
||||
testFunction: self.testPrimeFactors)
|
||||
}
|
||||
|
||||
func testPKCS1Signing() throws {
|
||||
try self.testPKCS1Signing(_RSA.Signing.PrivateKey(keySize: .bits2048))
|
||||
try self.testPKCS1Signing(_RSA.Signing.PrivateKey(keySize: .bits3072))
|
||||
@@ -708,6 +717,34 @@ final class TestRSASigning: XCTestCase {
|
||||
)
|
||||
}
|
||||
|
||||
func testConstructAndUseKeyFromRSANumbersWhileRecoveringPrimes() throws {
|
||||
let data = Array("hello, world!".utf8)
|
||||
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let key = try _RSA.Signing.PrivateKey._createFromNumbers(
|
||||
n: Data(hexString: testVector.n),
|
||||
e: Data(hexString: testVector.e),
|
||||
d: Data(hexString: testVector.d)
|
||||
)
|
||||
|
||||
let signature = try key.signature(for: data)
|
||||
let roundTripped = _RSA.Signing.RSASignature(rawRepresentation: signature.rawRepresentation)
|
||||
XCTAssertEqual(signature.rawRepresentation, roundTripped.rawRepresentation)
|
||||
XCTAssertTrue(key.publicKey.isValidSignature(roundTripped, for: data))
|
||||
}
|
||||
}
|
||||
|
||||
func testGetKeyPrimitives() throws {
|
||||
for testVector in RFC9474TestVector.allValues {
|
||||
let n = try Data(hexString: testVector.n)
|
||||
let e = try Data(hexString: testVector.e)
|
||||
|
||||
let primitives = try _RSA.Signing.PublicKey(n: n, e: e).getKeyPrimitives()
|
||||
XCTAssertEqual(primitives.modulus, n)
|
||||
XCTAssertEqual(primitives.publicExponent, e)
|
||||
}
|
||||
}
|
||||
|
||||
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
|
||||
let derKey: _RSA.Signing.PublicKey
|
||||
let pemKey: _RSA.Signing.PublicKey
|
||||
@@ -790,6 +827,15 @@ final class TestRSASigning: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private func testPrimeFactors(_ group: RSAPrimitivesTestGroup) throws {
|
||||
let n = try ArbitraryPrecisionInteger(hexString: group.n)
|
||||
let e = try ArbitraryPrecisionInteger(hexString: group.e)
|
||||
let d = try ArbitraryPrecisionInteger(hexString: group.d)
|
||||
|
||||
let (p, q) = try _RSA.extractPrimeFactors(n: n, e: e, d: d)
|
||||
XCTAssertEqual(p * q, n, "The product of p and q should equal n; got \(p) * \(q) != \(n)")
|
||||
}
|
||||
|
||||
private func pemForDERBytes(discriminator: String, derBytes: Data) -> String {
|
||||
let lineLength = 64
|
||||
var encoded = derBytes.base64EncodedString()[...]
|
||||
@@ -871,3 +917,27 @@ struct RSATest: Codable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RSAPrimitivesTestGroup: Codable {
|
||||
let n: String
|
||||
let e: String
|
||||
let d: String
|
||||
let privateKeyJwk: PrivateKeyJWK?
|
||||
|
||||
struct PrivateKeyJWK: Codable {
|
||||
let kty: String
|
||||
let n: String
|
||||
let e: String
|
||||
let d: String
|
||||
let p: String
|
||||
let q: String
|
||||
let dp: String
|
||||
let dq: String
|
||||
let qi: String
|
||||
}
|
||||
}
|
||||
|
||||
struct RSAPrimitivesTestVectors: Codable {
|
||||
let testGroups: [RSAPrimitivesTestGroup]
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
version: "3"
|
||||
|
||||
services:
|
||||
|
||||
runtime-setup:
|
||||
image: swift-crypto:22.04-5.8
|
||||
build:
|
||||
args:
|
||||
ubuntu_version: "jammy"
|
||||
swift_version: "5.8"
|
||||
|
||||
test:
|
||||
image: swift-crypto:22.04-5.8
|
||||
environment: []
|
||||
#- SANITIZER_ARG=--sanitize=thread
|
||||
|
||||
cmake:
|
||||
image: swift-crypto:22.04-5.8
|
||||
|
||||
shell:
|
||||
image: swift-crypto:22.04-5.8
|
||||
Reference in new issue
Block a user