mirror of
https://github.com/apple/swift-crypto
synced 2026-11-30 05:18:46 +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_helper.c",
|
||||||
"crypto/bio/socket.c"
|
"crypto/bio/socket.c"
|
||||||
],
|
],
|
||||||
|
resources: [
|
||||||
|
.copy("PrivacyInfo.xcprivacy"),
|
||||||
|
],
|
||||||
cSettings: [
|
cSettings: [
|
||||||
// These defines come from BoringSSL's build system
|
// These defines come from BoringSSL's build system
|
||||||
.define("_HAS_EXCEPTIONS", to: "0", .when(platforms: [Platform.windows])),
|
.define("_HAS_EXCEPTIONS", to: "0", .when(platforms: [Platform.windows])),
|
||||||
@@ -106,6 +109,9 @@ let package = Package(
|
|||||||
dependencies: ["CCryptoBoringSSL"],
|
dependencies: ["CCryptoBoringSSL"],
|
||||||
exclude: [
|
exclude: [
|
||||||
"CMakeLists.txt"
|
"CMakeLists.txt"
|
||||||
|
],
|
||||||
|
resources: [
|
||||||
|
.copy("PrivacyInfo.xcprivacy"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@@ -118,6 +124,9 @@ let package = Package(
|
|||||||
"Key Agreement/ECDH.swift.gyb",
|
"Key Agreement/ECDH.swift.gyb",
|
||||||
"Signatures/ECDSA.swift.gyb",
|
"Signatures/ECDSA.swift.gyb",
|
||||||
],
|
],
|
||||||
|
resources: [
|
||||||
|
.copy("PrivacyInfo.xcprivacy"),
|
||||||
|
],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@@ -131,6 +140,9 @@ let package = Package(
|
|||||||
exclude: [
|
exclude: [
|
||||||
"CMakeLists.txt",
|
"CMakeLists.txt",
|
||||||
],
|
],
|
||||||
|
resources: [
|
||||||
|
.copy("PrivacyInfo.xcprivacy"),
|
||||||
|
],
|
||||||
swiftSettings: swiftSettings
|
swiftSettings: swiftSettings
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
@@ -141,6 +153,9 @@ let package = Package(
|
|||||||
],
|
],
|
||||||
exclude: [
|
exclude: [
|
||||||
"CMakeLists.txt",
|
"CMakeLists.txt",
|
||||||
|
],
|
||||||
|
resources: [
|
||||||
|
.copy("PrivacyInfo.xcprivacy"),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.executableTarget(name: "crypto-shasum", dependencies: ["Crypto"]),
|
.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 Crypto
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
@usableFromInline
|
||||||
|
typealias AESCTRImpl = OpenSSLAESCTRImpl
|
||||||
|
|
||||||
extension AES {
|
extension AES {
|
||||||
|
|
||||||
public enum _CTR {
|
public enum _CTR {
|
||||||
private static func encryptInPlace(
|
@inlinable
|
||||||
_ 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static func encrypt<Plaintext: DataProtocol>(
|
public static func encrypt<Plaintext: DataProtocol>(
|
||||||
_ plaintext: Plaintext,
|
_ plaintext: Plaintext,
|
||||||
using key: SymmetricKey,
|
using key: SymmetricKey,
|
||||||
nonce: AES._CTR.Nonce
|
nonce: AES._CTR.Nonce
|
||||||
) throws -> Data {
|
) throws -> Data {
|
||||||
var flattenedPlaintext = Data(plaintext)
|
let bytes: ContiguousBytes = plaintext.regions.count == 1 ? plaintext.regions.first! : Array(plaintext)
|
||||||
try flattenedPlaintext.withUnsafeMutableBytes {
|
return try AESCTRImpl.encrypt(bytes, using: key, nonce: nonce)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
public static func decrypt<Ciphertext: DataProtocol>(
|
public static func decrypt<Ciphertext: DataProtocol>(
|
||||||
_ ciphertext: Ciphertext,
|
_ ciphertext: Ciphertext,
|
||||||
using key: SymmetricKey,
|
using key: SymmetricKey,
|
||||||
nonce: AES._CTR.Nonce
|
nonce: AES._CTR.Nonce
|
||||||
) throws -> Data {
|
) throws -> Data {
|
||||||
var flattenedCiphertext = Data(ciphertext)
|
// Surprise, CTR mode is symmetric in encryption/decryption!
|
||||||
try flattenedCiphertext.withUnsafeMutableBytes {
|
try Self.encrypt(ciphertext, using: key, nonce: nonce)
|
||||||
try Self.decryptInPlace($0, using: key, nonce: nonce)
|
|
||||||
}
|
|
||||||
return flattenedCiphertext
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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 {
|
mutating func withUnsafeMutableBytes<ReturnType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ReturnType) rethrows -> ReturnType {
|
||||||
return try Swift.withUnsafeMutableBytes(of: &self.nonceBytes, body)
|
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.
|
/// 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.
|
/// for their use-case.
|
||||||
public init(pemRepresentation: String) throws {
|
public init(pemRepresentation: String) throws {
|
||||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
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 {
|
guard self.keySizeInBits >= 1024 else {
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct an RSA public key from a DER representation.
|
/// 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.
|
/// for their use-case.
|
||||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||||
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
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 {
|
guard self.keySizeInBits >= 1024 else {
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
@@ -103,11 +130,24 @@ extension _RSA.Signing {
|
|||||||
|
|
||||||
/// Construct an RSA private key from a PEM representation.
|
/// 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.
|
/// for their use-case.
|
||||||
public init(pemRepresentation: String) throws {
|
public init(pemRepresentation: String) throws {
|
||||||
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
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 {
|
guard self.keySizeInBits >= 1024 else {
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
@@ -115,11 +155,24 @@ extension _RSA.Signing {
|
|||||||
|
|
||||||
/// Construct an RSA private key from a DER representation.
|
/// 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.
|
/// for their use-case.
|
||||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
||||||
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
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 {
|
guard self.keySizeInBits >= 1024 else {
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
@@ -127,9 +180,21 @@ extension _RSA.Signing {
|
|||||||
|
|
||||||
/// Randomly generate a new RSA private key of a given size.
|
/// 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.
|
/// key size requirements should validate `keySize` before use.
|
||||||
public init(keySize: _RSA.Signing.KeySize) throws {
|
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 {
|
guard keySize.bitCount >= 1024 else {
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
@@ -335,18 +400,38 @@ extension _RSA.Encryption {
|
|||||||
|
|
||||||
/// Construct an RSA public key from a PEM representation.
|
/// 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.
|
/// for their use-case.
|
||||||
public init(pemRepresentation: String) throws {
|
public init(pemRepresentation: String) throws {
|
||||||
self.backing = try BackingPublicKey(pemRepresentation: pemRepresentation)
|
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.
|
/// 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.
|
/// for their use-case.
|
||||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
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)
|
self.backing = try BackingPublicKey(derRepresentation: derRepresentation)
|
||||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
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.
|
/// 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.
|
/// for their use-case.
|
||||||
public init(pemRepresentation: String) throws {
|
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)
|
self.backing = try BackingPrivateKey(pemRepresentation: pemRepresentation)
|
||||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct an RSA private key from a DER representation.
|
/// 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.
|
/// for their use-case.
|
||||||
public init<Bytes: DataProtocol>(derRepresentation: Bytes) throws {
|
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)
|
self.backing = try BackingPrivateKey(derRepresentation: derRepresentation)
|
||||||
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
guard self.keySizeInBits >= 1024, self.keySizeInBits % 8 == 0 else { throw CryptoKitError.incorrectParameterSize }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Randomly generate a new RSA private key of a given size.
|
/// 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.
|
/// key size requirements should validate `keySize` before use.
|
||||||
public init(keySize: _RSA.Signing.KeySize) throws {
|
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 }
|
guard keySize.bitCount >= 1024 else { throw CryptoKitError.incorrectParameterSize }
|
||||||
self.backing = try BackingPrivateKey(keySize: keySize)
|
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 {
|
final class TestRSAEncryption: XCTestCase {
|
||||||
|
|
||||||
func test_wycheproofOAEPVectors() throws {
|
func test_wycheproofOAEPVectors() throws {
|
||||||
|
try wycheproofTest(
|
||||||
|
jsonName: "rsa_oaep_misc_test",
|
||||||
|
testFunction: self.testOAEPGroup)
|
||||||
try wycheproofTest(
|
try wycheproofTest(
|
||||||
jsonName: "rsa_oaep_2048_sha1_mgf1sha1_test",
|
jsonName: "rsa_oaep_2048_sha1_mgf1sha1_test",
|
||||||
testFunction: self.testOAEPGroup)
|
testFunction: self.testOAEPGroup)
|
||||||
try wycheproofTest(
|
try wycheproofTest(
|
||||||
jsonName: "rsa_oaep_2048_sha256_mgf1sha256_test",
|
jsonName: "rsa_oaep_2048_sha256_mgf1sha256_test",
|
||||||
testFunction: self.testOAEPGroup)
|
testFunction: self.testOAEPGroup)
|
||||||
try wycheproofTest(
|
|
||||||
jsonName: "rsa_oaep_misc_test",
|
|
||||||
testFunction: self.testOAEPGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func testOAEPGroup(_ group: RSAEncryptionOAEPTestGroup) throws {
|
private func testOAEPGroup(_ group: RSAEncryptionOAEPTestGroup) throws {
|
||||||
let derPrivKey = try _RSA.Encryption.PrivateKey(derRepresentation: group.privateKeyDerBytes)
|
let derPrivKey: _RSA.Encryption.PrivateKey
|
||||||
let pemPrivKey = try _RSA.Encryption.PrivateKey(pemRepresentation: group.privateKeyPem)
|
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.derRepresentation, pemPrivKey.derRepresentation)
|
||||||
XCTAssertEqual(derPrivKey.pemRepresentation, pemPrivKey.pemRepresentation)
|
XCTAssertEqual(derPrivKey.pemRepresentation, pemPrivKey.pemRepresentation)
|
||||||
@@ -77,6 +85,7 @@ struct RSAEncryptionOAEPTestGroup: Codable {
|
|||||||
var sha: String
|
var sha: String
|
||||||
var tests: [RSAEncryptionTest]
|
var tests: [RSAEncryptionTest]
|
||||||
var mgfSha: String
|
var mgfSha: String
|
||||||
|
var keysize: Int
|
||||||
|
|
||||||
var privateKeyDerBytes: Data {
|
var privateKeyDerBytes: Data {
|
||||||
return try! Data(hexString: self.privateKeyPkcs8)
|
return try! Data(hexString: self.privateKeyPkcs8)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ final class TestRSASigning: XCTestCase {
|
|||||||
try wycheproofTest(
|
try wycheproofTest(
|
||||||
jsonName: "rsa_signature_test",
|
jsonName: "rsa_signature_test",
|
||||||
testFunction: self.testPKCS1Group)
|
testFunction: self.testPKCS1Group)
|
||||||
|
|
||||||
try wycheproofTest(
|
try wycheproofTest(
|
||||||
jsonName: "rsa_signature_2048_sha256_test",
|
jsonName: "rsa_signature_2048_sha256_test",
|
||||||
testFunction: self.testPKCS1Group)
|
testFunction: self.testPKCS1Group)
|
||||||
@@ -286,13 +286,15 @@ final class TestRSASigning: XCTestCase {
|
|||||||
(_RSA.Signing.PrivateKey(keySize: .bits2048), 2048),
|
(_RSA.Signing.PrivateKey(keySize: .bits2048), 2048),
|
||||||
(_RSA.Signing.PrivateKey(keySize: .bits3072), 3072),
|
(_RSA.Signing.PrivateKey(keySize: .bits3072), 3072),
|
||||||
(_RSA.Signing.PrivateKey(keySize: .bits4096), 4096),
|
(_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 {
|
for (key, size) in keysAndSizes {
|
||||||
XCTAssertEqual(size, key.keySizeInBits)
|
XCTAssertEqual(size, key.keySizeInBits)
|
||||||
XCTAssertEqual(size, key.publicKey.keySizeInBits)
|
XCTAssertEqual(size, key.publicKey.keySizeInBits)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try XCTAssertThrowsError((_RSA.Signing.PrivateKey(keySize: .init(bitCount: 1024)), 1024))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRejectSmallKeys() throws {
|
func testRejectSmallKeys() throws {
|
||||||
@@ -676,8 +678,16 @@ final class TestRSASigning: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
|
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
|
||||||
let derKey = try _RSA.Signing.PublicKey(derRepresentation: group.keyDerBytes)
|
let derKey: _RSA.Signing.PublicKey
|
||||||
let pemKey = try _RSA.Signing.PublicKey(pemRepresentation: group.keyPem)
|
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.derRepresentation, pemKey.derRepresentation)
|
||||||
XCTAssertEqual(derKey.pemRepresentation, pemKey.pemRepresentation)
|
XCTAssertEqual(derKey.pemRepresentation, pemKey.pemRepresentation)
|
||||||
@@ -777,6 +787,7 @@ struct RSAPKCS1TestGroup: Codable {
|
|||||||
var keyPem: String
|
var keyPem: String
|
||||||
var sha: String
|
var sha: String
|
||||||
var tests: [RSATest]
|
var tests: [RSATest]
|
||||||
|
var keysize: Int
|
||||||
|
|
||||||
var keyDerBytes: Data {
|
var keyDerBytes: Data {
|
||||||
return try! Data(hexString: self.keyDer)
|
return try! Data(hexString: self.keyDer)
|
||||||
|
|||||||
Reference in new issue
Block a user