mirror of
https://github.com/apple/swift-crypto
synced 2026-11-22 04:30:39 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc1c29221f | ||
|
|
2adec617e6 | ||
|
|
89876ab72c |
No files matched your search
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -88,6 +88,9 @@ let package = Package(
|
||||
"crypto/bio/socket_helper.c",
|
||||
"crypto/bio/socket.c"
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
cSettings: [
|
||||
// These defines come from BoringSSL's build system
|
||||
.define("_HAS_EXCEPTIONS", to: "0", .when(platforms: [Platform.windows])),
|
||||
@@ -106,6 +109,9 @@ let package = Package(
|
||||
dependencies: ["CCryptoBoringSSL"],
|
||||
exclude: [
|
||||
"CMakeLists.txt"
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
]
|
||||
),
|
||||
.target(
|
||||
@@ -118,6 +124,9 @@ let package = Package(
|
||||
"Key Agreement/ECDH.swift.gyb",
|
||||
"Signatures/ECDSA.swift.gyb",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
.target(
|
||||
@@ -131,6 +140,9 @@ let package = Package(
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
],
|
||||
swiftSettings: swiftSettings
|
||||
),
|
||||
.target(
|
||||
@@ -141,6 +153,9 @@ let package = Package(
|
||||
],
|
||||
exclude: [
|
||||
"CMakeLists.txt",
|
||||
],
|
||||
resources: [
|
||||
.copy("PrivacyInfo.xcprivacy"),
|
||||
]
|
||||
),
|
||||
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
+1
@@ -0,0 +1 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -0,0 +1 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -15,69 +15,30 @@
|
||||
import Crypto
|
||||
import Foundation
|
||||
|
||||
@usableFromInline
|
||||
typealias AESCTRImpl = OpenSSLAESCTRImpl
|
||||
|
||||
extension AES {
|
||||
|
||||
public enum _CTR {
|
||||
private static func encryptInPlace(
|
||||
_ plaintext: UnsafeMutableRawBufferPointer,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) throws {
|
||||
precondition(MemoryLayout<AES._CTR.Nonce>.size == 16)
|
||||
|
||||
guard [128, 192, 256].contains(key.bitCount) else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
|
||||
var nonce = nonce
|
||||
|
||||
for offset in stride(from: 0, to: plaintext.count, by: MemoryLayout<AES._CTR.Nonce>.size) {
|
||||
var nonceCopy = nonce
|
||||
|
||||
try nonceCopy.withUnsafeMutableBytes { noncePtr in
|
||||
var noncePtr = noncePtr
|
||||
try AES.permute(&noncePtr, key: key)
|
||||
let remainingPlaintextBytes = plaintext.count &- offset
|
||||
|
||||
for i in 0..<min(remainingPlaintextBytes, MemoryLayout<AES._CTR.Nonce>.size) {
|
||||
plaintext[offset &+ i] ^= noncePtr[i]
|
||||
}
|
||||
}
|
||||
|
||||
nonce.incrementCounter()
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func encrypt<Plaintext: DataProtocol>(
|
||||
_ plaintext: Plaintext,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) throws -> Data {
|
||||
var flattenedPlaintext = Data(plaintext)
|
||||
try flattenedPlaintext.withUnsafeMutableBytes {
|
||||
try Self.encryptInPlace($0, using: key, nonce: nonce)
|
||||
}
|
||||
return flattenedPlaintext
|
||||
}
|
||||
|
||||
private static func decryptInPlace(
|
||||
_ ciphertext: UnsafeMutableRawBufferPointer,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) throws {
|
||||
// Surprise, CTR mode is symmetric in encryption/decryption!
|
||||
try Self.encryptInPlace(ciphertext, using: key, nonce: nonce)
|
||||
let bytes: ContiguousBytes = plaintext.regions.count == 1 ? plaintext.regions.first! : Array(plaintext)
|
||||
return try AESCTRImpl.encrypt(bytes, using: key, nonce: nonce)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public static func decrypt<Ciphertext: DataProtocol>(
|
||||
_ ciphertext: Ciphertext,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) throws -> Data {
|
||||
var flattenedCiphertext = Data(ciphertext)
|
||||
try flattenedCiphertext.withUnsafeMutableBytes {
|
||||
try Self.decryptInPlace($0, using: key, nonce: nonce)
|
||||
}
|
||||
return flattenedCiphertext
|
||||
// Surprise, CTR mode is symmetric in encryption/decryption!
|
||||
try Self.encrypt(ciphertext, using: key, nonce: nonce)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,21 +75,6 @@ extension AES._CTR {
|
||||
}
|
||||
}
|
||||
|
||||
mutating func incrementCounter() {
|
||||
var (newValue, overflow) = UInt32(bigEndian: self.nonceBytes.2).addingReportingOverflow(1)
|
||||
self.nonceBytes.2 = newValue.bigEndian
|
||||
|
||||
if overflow {
|
||||
(newValue, overflow) = UInt32(bigEndian: self.nonceBytes.1).addingReportingOverflow(1)
|
||||
self.nonceBytes.1 = newValue.bigEndian
|
||||
}
|
||||
|
||||
if overflow {
|
||||
// If this overflows that's fine: we'll have overflowed everything and gone back to 0.
|
||||
self.nonceBytes.0 = (UInt64(bigEndian: self.nonceBytes.0) &+ 1).bigEndian
|
||||
}
|
||||
}
|
||||
|
||||
mutating func withUnsafeMutableBytes<ReturnType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ReturnType) rethrows -> ReturnType {
|
||||
return try Swift.withUnsafeMutableBytes(of: &self.nonceBytes, body)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@_implementationOnly import CCryptoBoringSSL
|
||||
import Crypto
|
||||
import Foundation
|
||||
|
||||
@usableFromInline
|
||||
enum OpenSSLAESCTRImpl {
|
||||
@inlinable
|
||||
static func encrypt<Plaintext: ContiguousBytes>(
|
||||
_ plaintext: Plaintext,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) throws -> Data {
|
||||
guard [128, 192, 256].contains(key.bitCount) else {
|
||||
throw CryptoKitError.incorrectKeySize
|
||||
}
|
||||
return plaintext.withUnsafeBytes { plaintextBufferPtr in
|
||||
Self._encrypt(plaintextBufferPtr, using: key, nonce: nonce)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
static func _encrypt(
|
||||
_ plaintextBufferPtr: UnsafeRawBufferPointer,
|
||||
using key: SymmetricKey,
|
||||
nonce: AES._CTR.Nonce
|
||||
) -> Data {
|
||||
var ciphertext = Data(repeating: 0, count: plaintextBufferPtr.count)
|
||||
ciphertext.withUnsafeMutableBytes { ciphertextBufferPtr in
|
||||
var nonce = nonce
|
||||
var ecountBytes = (Int64.zero, Int64.zero)
|
||||
var num = UInt32.zero
|
||||
key.withUnsafeBytes { keyBufferPtr in
|
||||
nonce.withUnsafeMutableBytes { nonceBufferPtr in
|
||||
withUnsafeMutableBytes(of: &ecountBytes) { ecountBufferPtr in
|
||||
var key = AES_KEY()
|
||||
precondition(CCryptoBoringSSL_AES_set_encrypt_key(keyBufferPtr.baseAddress, UInt32(keyBufferPtr.count * 8), &key) == 0)
|
||||
CCryptoBoringSSL_AES_ctr128_encrypt(
|
||||
plaintextBufferPtr.baseAddress,
|
||||
ciphertextBufferPtr.baseAddress,
|
||||
plaintextBufferPtr.count,
|
||||
&key,
|
||||
nonceBufferPtr.baseAddress,
|
||||
ecountBufferPtr.baseAddress,
|
||||
&num
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ciphertext
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
../../.PrivacyInfo.xcprivacy
|
||||
@@ -49,23 +49,50 @@ extension _RSA.Signing {
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init(pemRepresentation: String) throws {
|
||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 2048 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafePEMRepresentation pemRepresentation: String) throws {
|
||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 1024 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 2048 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init<Bytes: DataProtocol>(unsafeDERRepresentation derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 1024 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
@@ -103,11 +130,24 @@ extension _RSA.Signing {
|
||||
|
||||
/// Construct an RSA private key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init(pemRepresentation: String) throws {
|
||||
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 2048 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafePEMRepresentation pemRepresentation: String) throws {
|
||||
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 1024 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
@@ -115,11 +155,24 @@ extension _RSA.Signing {
|
||||
|
||||
/// Construct an RSA private key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 2048 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init<Bytes: DataProtocol>(unsafeDERRepresentation derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
||||
|
||||
guard self.keySizeInBits >= 1024 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
@@ -127,9 +180,21 @@ extension _RSA.Signing {
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 1024 bits. Callers that want to enforce minimum
|
||||
/// This constructor will refuse to generate keys smaller than 2048 bits. Callers that want to enforce minimum
|
||||
/// key size requirements should validate `keySize` before use.
|
||||
public init(keySize: _RSA.Signing.KeySize) throws {
|
||||
guard keySize.bitCount >= 2048 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
self.backing = try BackingPrivateKey(keySize: keySize)
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 1024 bits. Callers that want to enforce minimum
|
||||
/// key size requirements should validate `unsafekeySize` before use.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafeKeySize keySize: _RSA.Signing.KeySize) throws {
|
||||
guard keySize.bitCount >= 1024 else {
|
||||
throw CryptoKitError.incorrectParameterSize
|
||||
}
|
||||
@@ -335,18 +400,38 @@ extension _RSA.Encryption {
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init(pemRepresentation: String) throws {
|
||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
guard self.keySizeInBits >= 2048, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafePEMRepresentation pemRepresentation: String) throws {
|
||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
||||
guard self.keySizeInBits >= 2048, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
||||
guard self.keySizeInBits >= 2048, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init<Bytes: DataProtocol>(unsafeDERRepresentation derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
@@ -365,27 +450,57 @@ extension _RSA.Encryption {
|
||||
|
||||
/// Construct an RSA private key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init(pemRepresentation: String) throws {
|
||||
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
||||
guard self.keySizeInBits >= 2048, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a PEM representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafePEMRepresentation pemRepresentation: String) throws {
|
||||
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA private key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// This constructor supports key sizes of 2048 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
||||
guard self.keySizeInBits >= 2048, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Construct an RSA public key from a DER representation.
|
||||
///
|
||||
/// This constructor supports key sizes of 1024 bits or more. Users should validate that key sizes are appropriate
|
||||
/// for their use-case.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init<Bytes: DataProtocol>(unsafeDERRepresentation derRepresentation: Bytes) throws {
|
||||
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 1024 bits. Callers that want to enforce minimum
|
||||
/// This constructor will refuse to generate keys smaller than 2048 bits. Callers that want to enforce minimum
|
||||
/// key size requirements should validate `keySize` before use.
|
||||
public init(keySize: _RSA.Signing.KeySize) throws {
|
||||
guard keySize.bitCount >= 2048 else { throw CryptoKitError.incorrectParameterSize }
|
||||
self.backing = try BackingPrivateKey(keySize: keySize)
|
||||
}
|
||||
|
||||
/// Randomly generate a new RSA private key of a given size.
|
||||
///
|
||||
/// This constructor will refuse to generate keys smaller than 1024 bits. Callers that want to enforce minimum
|
||||
/// key size requirements should validate `keySize` before use.
|
||||
/// - Warning: Key sizes less than 2048 are not recommended and should only be used for compatibility reasons.
|
||||
public init(unsafeKeySize keySize: _RSA.Signing.KeySize) throws {
|
||||
guard keySize.bitCount >= 1024 else { throw CryptoKitError.incorrectParameterSize }
|
||||
self.backing = try BackingPrivateKey(keySize: keySize)
|
||||
}
|
||||
|
||||
@@ -156,65 +156,4 @@ final class AESCTRTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testOverflowAllNonceBytesWorks() throws {
|
||||
var nonce = try AES._CTR.Nonce(nonceBytes: repeatElement(0xFF, count: 16))
|
||||
nonce.withUnsafeMutableBytes { nonceBytes in
|
||||
XCTAssertTrue(nonceBytes.allSatisfy { $0 == 0xFF })
|
||||
}
|
||||
|
||||
nonce.incrementCounter()
|
||||
|
||||
nonce.withUnsafeMutableBytes { nonceBytes in
|
||||
XCTAssertTrue(nonceBytes.allSatisfy { $0 == 0x00 })
|
||||
}
|
||||
}
|
||||
|
||||
func testOverflowCounterCorrectlyStoresInBigEndian() throws {
|
||||
let nonceBytes: [UInt8] = [
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x01, 0x02, 0x03, 0x04,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
]
|
||||
var nonce = try AES._CTR.Nonce(nonceBytes: nonceBytes)
|
||||
|
||||
nonce.incrementCounter()
|
||||
|
||||
let newNonceBytes = nonce.withUnsafeMutableBytes {
|
||||
Array($0)
|
||||
}
|
||||
|
||||
let expectedNonceBytes: [UInt8] = [
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x01, 0x02, 0x03, 0x05,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
]
|
||||
XCTAssertEqual(expectedNonceBytes, newNonceBytes)
|
||||
}
|
||||
|
||||
func testOverflowCounterAndNextUInt32CorrectlyStoresInBigEndian() throws {
|
||||
let nonceBytes: [UInt8] = [
|
||||
0x01, 0x02, 0x03, 0x04,
|
||||
0x05, 0x06, 0x07, 0x08,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
]
|
||||
var nonce = try AES._CTR.Nonce(nonceBytes: nonceBytes)
|
||||
|
||||
nonce.incrementCounter()
|
||||
|
||||
let newNonceBytes = nonce.withUnsafeMutableBytes {
|
||||
Array($0)
|
||||
}
|
||||
|
||||
let expectedNonceBytes: [UInt8] = [
|
||||
0x01, 0x02, 0x03, 0x04,
|
||||
0x05, 0x06, 0x07, 0x09,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
]
|
||||
XCTAssertEqual(expectedNonceBytes, newNonceBytes)
|
||||
}
|
||||
}
|
||||
@@ -19,20 +19,28 @@ import _CryptoExtras
|
||||
final class TestRSAEncryption: XCTestCase {
|
||||
|
||||
func test_wycheproofOAEPVectors() throws {
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_oaep_misc_test",
|
||||
testFunction: self.testOAEPGroup)
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_oaep_2048_sha1_mgf1sha1_test",
|
||||
testFunction: self.testOAEPGroup)
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_oaep_2048_sha256_mgf1sha256_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)
|
||||
let derPrivKey: _RSA.Encryption.PrivateKey
|
||||
let pemPrivKey: _RSA.Encryption.PrivateKey
|
||||
|
||||
if group.keysize < 2048 {
|
||||
derPrivKey = try _RSA.Encryption.PrivateKey(unsafeDERRepresentation: group.privateKeyDerBytes)
|
||||
pemPrivKey = try _RSA.Encryption.PrivateKey(unsafePEMRepresentation: group.privateKeyPem)
|
||||
} else {
|
||||
derPrivKey = try _RSA.Encryption.PrivateKey(derRepresentation: group.privateKeyDerBytes)
|
||||
pemPrivKey = try _RSA.Encryption.PrivateKey(pemRepresentation: group.privateKeyPem)
|
||||
}
|
||||
|
||||
XCTAssertEqual(derPrivKey.derRepresentation, pemPrivKey.derRepresentation)
|
||||
XCTAssertEqual(derPrivKey.pemRepresentation, pemPrivKey.pemRepresentation)
|
||||
@@ -77,6 +85,7 @@ struct RSAEncryptionOAEPTestGroup: Codable {
|
||||
var sha: String
|
||||
var tests: [RSAEncryptionTest]
|
||||
var mgfSha: String
|
||||
var keysize: Int
|
||||
|
||||
var privateKeyDerBytes: Data {
|
||||
return try! Data(hexString: self.privateKeyPkcs8)
|
||||
|
||||
@@ -21,7 +21,7 @@ final class TestRSASigning: XCTestCase {
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_signature_test",
|
||||
testFunction: self.testPKCS1Group)
|
||||
|
||||
|
||||
try wycheproofTest(
|
||||
jsonName: "rsa_signature_2048_sha256_test",
|
||||
testFunction: self.testPKCS1Group)
|
||||
@@ -286,13 +286,15 @@ final class TestRSASigning: XCTestCase {
|
||||
(_RSA.Signing.PrivateKey(keySize: .bits2048), 2048),
|
||||
(_RSA.Signing.PrivateKey(keySize: .bits3072), 3072),
|
||||
(_RSA.Signing.PrivateKey(keySize: .bits4096), 4096),
|
||||
(_RSA.Signing.PrivateKey(keySize: .init(bitCount: 1024)), 1024),
|
||||
(_RSA.Signing.PrivateKey(unsafeKeySize: .init(bitCount: 1024)), 1024),
|
||||
]
|
||||
|
||||
|
||||
for (key, size) in keysAndSizes {
|
||||
XCTAssertEqual(size, key.keySizeInBits)
|
||||
XCTAssertEqual(size, key.publicKey.keySizeInBits)
|
||||
}
|
||||
|
||||
try XCTAssertThrowsError((_RSA.Signing.PrivateKey(keySize: .init(bitCount: 1024)), 1024))
|
||||
}
|
||||
|
||||
func testRejectSmallKeys() throws {
|
||||
@@ -676,8 +678,16 @@ final class TestRSASigning: XCTestCase {
|
||||
}
|
||||
|
||||
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
|
||||
let derKey = try _RSA.Signing.PublicKey(derRepresentation: group.keyDerBytes)
|
||||
let pemKey = try _RSA.Signing.PublicKey(pemRepresentation: group.keyPem)
|
||||
let derKey: _RSA.Signing.PublicKey
|
||||
let pemKey: _RSA.Signing.PublicKey
|
||||
|
||||
if group.keysize < 2048 {
|
||||
derKey = try _RSA.Signing.PublicKey(unsafeDERRepresentation: group.keyDerBytes)
|
||||
pemKey = try _RSA.Signing.PublicKey(unsafePEMRepresentation: group.keyPem)
|
||||
} else {
|
||||
derKey = try _RSA.Signing.PublicKey(derRepresentation: group.keyDerBytes)
|
||||
pemKey = try _RSA.Signing.PublicKey(pemRepresentation: group.keyPem)
|
||||
}
|
||||
|
||||
XCTAssertEqual(derKey.derRepresentation, pemKey.derRepresentation)
|
||||
XCTAssertEqual(derKey.pemRepresentation, pemKey.pemRepresentation)
|
||||
@@ -777,6 +787,7 @@ struct RSAPKCS1TestGroup: Codable {
|
||||
var keyPem: String
|
||||
var sha: String
|
||||
var tests: [RSATest]
|
||||
var keysize: Int
|
||||
|
||||
var keyDerBytes: Data {
|
||||
return try! Data(hexString: self.keyDer)
|
||||
|
||||
Reference in new issue
Block a user