mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
* Add basic RSA encrypt/decrypt API. * Add test vectors and tests for RSA encryption API * Fix build failures * Fix soundness issue * Remove support for RSA encryption using the unsafe PKCS1 v1.5 padding * Fix deprecation warning about the "pointer" version of Data.withUnsafeMutableBytes() by nudging the compiler to infer the "buffer" version. * Fix deprecation warning about String.init(bytesNoCopy:length:freeWhenDone:) by using String.init(unsafeInitializedCapacity:initializingWith:) instead. * Make _RSA.Encryption.[Public|Private]Key their own types * Remove _RSA.Encryption.RSA[Encrypted|Decrypted]Data and just use Data directly. Don't use intermediate Array in BoringSSL implementations. * Document the message size limits on RSA encrypt/decrypt operations * Mark `_RSA` visible to documentation so the new docs can actually be seen (underscored name is assumed private by default) * Fix generic type parameter shadowing warning (new warning in Swift 5.9) * Fix pre-5.8 build * Un-correct switch case indentation to make it wrong
108 lines
3.3 KiB
Swift
108 lines
3.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) YEARS 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 Foundation
|
|
import XCTest
|
|
import Crypto
|
|
import _CryptoExtras
|
|
|
|
final class TestRSAEncryption: XCTestCase {
|
|
|
|
func test_wycheproofOAEPVectors() throws {
|
|
try wycheproofTest(
|
|
jsonName: "rsa_oaep_2048_sha1_mgf1sha1_test",
|
|
testFunction: self.testOAEPGroup)
|
|
try wycheproofTest(
|
|
jsonName: "rsa_oaep_misc_test",
|
|
testFunction: self.testOAEPGroup)
|
|
}
|
|
|
|
private func testOAEPGroup(_ group: RSAEncryptionOAEPTestGroup) throws {
|
|
let derPrivKey = try _RSA.Encryption.PrivateKey(derRepresentation: group.privateKeyDerBytes)
|
|
let pemPrivKey = try _RSA.Encryption.PrivateKey(pemRepresentation: group.privateKeyPem)
|
|
|
|
XCTAssertEqual(derPrivKey.derRepresentation, pemPrivKey.derRepresentation)
|
|
XCTAssertEqual(derPrivKey.pemRepresentation, pemPrivKey.pemRepresentation)
|
|
|
|
let derPubKey = derPrivKey.publicKey
|
|
|
|
guard group.sha == "SHA-1", group.mgfSha == "SHA-1" else {
|
|
// We currently only support SHA-1 OAEP, which is very legacy but oh well.
|
|
return
|
|
}
|
|
|
|
for test in group.tests {
|
|
guard test.label?.isEmpty ?? true else {
|
|
// We currently have no support for OAEP labels.
|
|
continue
|
|
}
|
|
let valid: Bool
|
|
|
|
do {
|
|
let decryptResult = try derPrivKey.decrypt(test.ciphertextBytes, padding: .PKCS1_OAEP)
|
|
let encryptResult = try derPubKey.encrypt(test.messageBytes, padding: .PKCS1_OAEP)
|
|
let decryptResult2 = try derPrivKey.decrypt(encryptResult, padding: .PKCS1_OAEP)
|
|
|
|
valid = (test.messageBytes == decryptResult && decryptResult2 == decryptResult)
|
|
} catch {
|
|
valid = false
|
|
}
|
|
|
|
XCTAssertEqual(valid, test.expectedValidity, "test number \(test.tcId) failed, expected \(test.result) but got \(valid)")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct RSAEncryptionOAEPTestGroup: Codable {
|
|
var privateKeyPem: String
|
|
var privateKeyPkcs8: String
|
|
var sha: String
|
|
var tests: [RSAEncryptionTest]
|
|
var mgfSha: String
|
|
|
|
var privateKeyDerBytes: Data {
|
|
return try! Data(hexString: self.privateKeyPkcs8)
|
|
}
|
|
}
|
|
|
|
struct RSAEncryptionTest: Codable {
|
|
var tcId: Int
|
|
var comment: String
|
|
var msg: String
|
|
var ct: String
|
|
var result: String
|
|
var flags: [String]
|
|
var label: String?
|
|
|
|
var messageBytes: Data {
|
|
return try! Data(hexString: self.msg)
|
|
}
|
|
|
|
var ciphertextBytes: Data {
|
|
return try! Data(hexString: self.ct)
|
|
}
|
|
|
|
var expectedValidity: Bool {
|
|
switch self.result {
|
|
case "valid":
|
|
return true
|
|
case "invalid":
|
|
return false
|
|
case "acceptable":
|
|
return true
|
|
default:
|
|
fatalError("Unexpected validity")
|
|
}
|
|
}
|
|
}
|