mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e15b6234fe |
No files matched your search
@@ -152,7 +152,7 @@ extension BoringSSLAEAD.AEADContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Lowest level seal operation that calls into BoringSSL directly and
|
/// Lowest level seal operation that calls into BoringSSL directly and
|
||||||
/// operates in-place on already-allocated memory.
|
/// operates on already-allocated memory.
|
||||||
#if compiler(<6.3)
|
#if compiler(<6.3)
|
||||||
@_lifetime(tag: copy tag)
|
@_lifetime(tag: copy tag)
|
||||||
#endif
|
#endif
|
||||||
@@ -162,99 +162,43 @@ extension BoringSSLAEAD.AEADContext {
|
|||||||
authenticatedData: RawSpan,
|
authenticatedData: RawSpan,
|
||||||
tag: inout OutputRawSpan
|
tag: inout OutputRawSpan
|
||||||
) throws {
|
) throws {
|
||||||
// BoringSSL writes up to (EVP_AEAD_max_overhead + extra_in_len) bytes. We use zero extra_in_len.
|
let tagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead)
|
||||||
let maxTagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead)
|
precondition(tag.freeCapacity >= tagByteCount)
|
||||||
precondition(tag.freeCapacity >= maxTagByteCount)
|
var actualTagSize = tagByteCount
|
||||||
|
|
||||||
try withUnsafeMutablePointer(to: &self.context) { contextPointer in
|
let rc = withUnsafeMutablePointer(to: &self.context) { contextPointer in
|
||||||
try message.withUnsafeMutableBytes { messageBuffer in
|
message.withUnsafeMutableBytes { messageBuffer in
|
||||||
try tag.withUnsafeMutableBytes { tagBuffer, tagInitializedCount in
|
tag.withUnsafeMutableBytes { tagBuffer, tagInitializedCount in
|
||||||
// BoringSSL returns the size of the tag it wrote.
|
defer {
|
||||||
var actualTagByteCount = maxTagByteCount
|
tagInitializedCount += actualTagSize
|
||||||
defer { tagInitializedCount += actualTagByteCount }
|
}
|
||||||
// BoringSSL guarantees to fill out with in_len bytes, even on error.
|
|
||||||
try authenticatedData.withUnsafeBytes { authenticatedDataBuffer in
|
return authenticatedData.withUnsafeBytes { authenticatedDataBuffer in
|
||||||
try nonce.withUnsafeBytes { nonceBuffer in
|
nonce.withUnsafeBytes { nonceBuffer in
|
||||||
guard
|
CCryptoBoringSSL_EVP_AEAD_CTX_seal_scatter(
|
||||||
CCryptoBoringSSL_EVP_AEAD_CTX_seal_scatter(
|
contextPointer,
|
||||||
contextPointer,
|
messageBuffer.baseAddress,
|
||||||
messageBuffer.baseAddress,
|
tagBuffer.baseAddress! + tagInitializedCount,
|
||||||
tagBuffer.baseAddress! + tagInitializedCount,
|
&actualTagSize,
|
||||||
&actualTagByteCount,
|
tagBuffer.count - tagInitializedCount,
|
||||||
tagBuffer.count - tagInitializedCount,
|
nonceBuffer.baseAddress,
|
||||||
nonceBuffer.baseAddress,
|
nonceBuffer.count,
|
||||||
nonceBuffer.count,
|
messageBuffer.baseAddress,
|
||||||
messageBuffer.baseAddress,
|
messageBuffer.count,
|
||||||
messageBuffer.count,
|
nil,
|
||||||
nil,
|
0,
|
||||||
0,
|
authenticatedDataBuffer.baseAddress,
|
||||||
authenticatedDataBuffer.baseAddress,
|
authenticatedDataBuffer.count
|
||||||
authenticatedDataBuffer.count
|
)
|
||||||
) == 1
|
|
||||||
else {
|
|
||||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Lowest level seal operation that calls into BoringSSL directly and
|
guard rc == 1 else {
|
||||||
/// operates on already-allocated memory to input and output buffers.
|
throw CryptoBoringWrapperError.internalBoringSSLError()
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(tag: copy tag)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
public func seal(
|
|
||||||
message: RawSpan,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: RawSpan,
|
|
||||||
authenticatedData: RawSpan,
|
|
||||||
tag: inout OutputRawSpan,
|
|
||||||
) throws {
|
|
||||||
// BoringSSL writes up to (EVP_AEAD_max_overhead + extra_in_len) bytes. We use zero extra_in_len.
|
|
||||||
let maxTagByteCount = CCryptoBoringSSL_EVP_AEAD_max_overhead(self.context.aead)
|
|
||||||
precondition(tag.freeCapacity >= maxTagByteCount)
|
|
||||||
precondition(output.freeCapacity >= message.byteCount)
|
|
||||||
|
|
||||||
try withUnsafeMutablePointer(to: &self.context) { contextPointer in
|
|
||||||
try message.withUnsafeBytes { messageBuffer in
|
|
||||||
try tag.withUnsafeMutableBytes { tagBuffer, tagInitializedCount in
|
|
||||||
// BoringSSL returns the size of the tag it wrote.
|
|
||||||
var actualTagByteCount = maxTagByteCount
|
|
||||||
|
|
||||||
try output.withUnsafeMutableBytes { outputBuffer, outputInitializedCount in
|
|
||||||
try authenticatedData.withUnsafeBytes { authenticatedDataBuffer in
|
|
||||||
try nonce.withUnsafeBytes { nonceBuffer in
|
|
||||||
guard
|
|
||||||
CCryptoBoringSSL_EVP_AEAD_CTX_seal_scatter(
|
|
||||||
contextPointer,
|
|
||||||
outputBuffer.baseAddress?.advanced(by: outputInitializedCount),
|
|
||||||
tagBuffer.baseAddress! + tagInitializedCount,
|
|
||||||
&actualTagByteCount,
|
|
||||||
tagBuffer.count - tagInitializedCount,
|
|
||||||
nonceBuffer.baseAddress,
|
|
||||||
nonceBuffer.count,
|
|
||||||
messageBuffer.baseAddress,
|
|
||||||
messageBuffer.count,
|
|
||||||
nil,
|
|
||||||
0,
|
|
||||||
authenticatedDataBuffer.baseAddress,
|
|
||||||
authenticatedDataBuffer.count
|
|
||||||
) == 1
|
|
||||||
else {
|
|
||||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// BoringSSL writes zeros on error; count only on success so a throw leaves output unchanged.
|
|
||||||
outputInitializedCount += messageBuffer.count
|
|
||||||
}
|
|
||||||
tagInitializedCount += actualTagByteCount
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,52 +340,6 @@ extension BoringSSLAEAD.AEADContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lowest level open operation that calls into BoringSSL directly and
|
|
||||||
/// operates on already-allocated memory to input and output buffers.
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
public func open(
|
|
||||||
message: RawSpan,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: RawSpan,
|
|
||||||
tag: RawSpan,
|
|
||||||
authenticatedData: RawSpan
|
|
||||||
) throws {
|
|
||||||
precondition(output.freeCapacity >= message.byteCount)
|
|
||||||
|
|
||||||
try withUnsafePointer(to: &self.context) { contextPointer in
|
|
||||||
try message.withUnsafeBytes { messageBuffer in
|
|
||||||
try output.withUnsafeMutableBytes { outputBuffer, outputInitializedCount in
|
|
||||||
try nonce.withUnsafeBytes { nonceBuffer in
|
|
||||||
try tag.withUnsafeBytes { tagBuffer in
|
|
||||||
try authenticatedData.withUnsafeBytes { authenticatedDataBuffer in
|
|
||||||
guard
|
|
||||||
CCryptoBoringSSL_EVP_AEAD_CTX_open_gather(
|
|
||||||
contextPointer,
|
|
||||||
outputBuffer.baseAddress?.advanced(by: outputInitializedCount),
|
|
||||||
nonceBuffer.baseAddress,
|
|
||||||
nonceBuffer.count,
|
|
||||||
messageBuffer.baseAddress,
|
|
||||||
messageBuffer.count,
|
|
||||||
tagBuffer.baseAddress,
|
|
||||||
tagBuffer.count,
|
|
||||||
authenticatedDataBuffer.baseAddress,
|
|
||||||
authenticatedDataBuffer.count
|
|
||||||
) == 1
|
|
||||||
else {
|
|
||||||
throw CryptoBoringWrapperError.internalBoringSSLError()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// BoringSSL writes zeros on error; count only on success so a throw leaves output unchanged.
|
|
||||||
outputInitializedCount += messageBuffer.count
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The unsafe base call: not inlinable so that it can touch private variables.
|
/// The unsafe base call: not inlinable so that it can touch private variables.
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
func _openContiguous(
|
func _openContiguous(
|
||||||
|
|||||||
@@ -1,85 +0,0 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
|
||||||
//
|
|
||||||
// This source file is part of the SwiftCrypto open source project
|
|
||||||
//
|
|
||||||
// Copyright (c) 2026 Apple Inc. and the SwiftCrypto project authors
|
|
||||||
// Licensed under Apache License v2.0
|
|
||||||
//
|
|
||||||
// See LICENSE.txt for license information
|
|
||||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
import Crypto
|
|
||||||
|
|
||||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
||||||
extension AES.GCM {
|
|
||||||
/// Secures the given plaintext message with encryption and an authentication tag.
|
|
||||||
///
|
|
||||||
/// - Parameters:
|
|
||||||
/// - message: The plaintext data to seal.
|
|
||||||
/// - key: A cryptographic key used to seal the message.
|
|
||||||
/// - nonce: The nonce the sealing process requires.
|
|
||||||
/// - authenticatedData: Additional data to be authenticated, if provided.
|
|
||||||
/// - tag: Receives the 16-byte authentication tag. Needs at least 16 bytes of free capacity.
|
|
||||||
/// - output: Receives the encrypted message. Needs at least `message.byteCount` bytes of free capacity.
|
|
||||||
///
|
|
||||||
/// The call traps if `tag` or `output` lacks the free capacity. If the call throws, `tag` and
|
|
||||||
/// `output` are unchanged.
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(tag: copy tag)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
public static func _seal(
|
|
||||||
_ message: RawSpan,
|
|
||||||
using key: SymmetricKey,
|
|
||||||
nonce: Nonce,
|
|
||||||
authenticating authenticatedData: RawSpan? = nil,
|
|
||||||
tag: inout OutputRawSpan,
|
|
||||||
output: inout OutputRawSpan
|
|
||||||
) throws(CryptoKitMetaError) {
|
|
||||||
try OpenSSLAESGCMImpl.seal(
|
|
||||||
key: key,
|
|
||||||
message: message,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonce,
|
|
||||||
authenticatedData: authenticatedData,
|
|
||||||
tag: &tag
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Decrypts the message and verifies its authenticity.
|
|
||||||
///
|
|
||||||
/// - Parameters:
|
|
||||||
/// - message: The ciphertext to decrypt.
|
|
||||||
/// - key: The cryptographic key that was used to seal the message.
|
|
||||||
/// - nonce: The nonce used to encrypt the message.
|
|
||||||
/// - authenticatedData: Additional data that was authenticated, if provided.
|
|
||||||
/// - tag: The 16-byte authentication tag.
|
|
||||||
/// - output: Receives the decrypted message. Needs at least `message.byteCount` bytes of free capacity.
|
|
||||||
///
|
|
||||||
/// The call traps if `output` lacks the free capacity. The call throws an error if decryption or
|
|
||||||
/// authentication fail, and leaves `output` unchanged.
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
public static func _open(
|
|
||||||
_ message: RawSpan,
|
|
||||||
using key: SymmetricKey,
|
|
||||||
nonce: Nonce,
|
|
||||||
authenticating authenticatedData: RawSpan? = nil,
|
|
||||||
tag: RawSpan,
|
|
||||||
output: inout OutputRawSpan
|
|
||||||
) throws(CryptoKitMetaError) {
|
|
||||||
try OpenSSLAESGCMImpl.open(
|
|
||||||
key: key,
|
|
||||||
message: message,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonce,
|
|
||||||
authenticatedData: authenticatedData,
|
|
||||||
tag: tag
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
|
||||||
//
|
|
||||||
// This source file is part of the SwiftCrypto open source project
|
|
||||||
//
|
|
||||||
// Copyright (c) 2026 Apple Inc. and the SwiftCrypto project authors
|
|
||||||
// Licensed under Apache License v2.0
|
|
||||||
//
|
|
||||||
// See LICENSE.txt for license information
|
|
||||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
import Crypto
|
|
||||||
import CryptoBoringWrapper
|
|
||||||
|
|
||||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
||||||
extension BoringSSLAEAD {
|
|
||||||
/// Seal a given message with caller owned buffers.
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(tag: copy tag)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
func seal(
|
|
||||||
message: RawSpan,
|
|
||||||
key: SymmetricKey,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: RawSpan,
|
|
||||||
authenticatedData: RawSpan,
|
|
||||||
tag: inout OutputRawSpan
|
|
||||||
) throws {
|
|
||||||
do {
|
|
||||||
let context = try AEADContext(cipher: self, key: key)
|
|
||||||
return try context.seal(
|
|
||||||
message: message,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonce,
|
|
||||||
authenticatedData: authenticatedData,
|
|
||||||
tag: &tag
|
|
||||||
)
|
|
||||||
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
|
|
||||||
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Open a given message with caller owned buffers.
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
func open(
|
|
||||||
message: RawSpan,
|
|
||||||
key: SymmetricKey,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: RawSpan,
|
|
||||||
tag: RawSpan,
|
|
||||||
authenticatedData: RawSpan
|
|
||||||
) throws {
|
|
||||||
do {
|
|
||||||
let context = try AEADContext(cipher: self, key: key)
|
|
||||||
return try context.open(
|
|
||||||
message: message,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonce,
|
|
||||||
tag: tag,
|
|
||||||
authenticatedData: authenticatedData
|
|
||||||
)
|
|
||||||
} catch CryptoBoringWrapperError.underlyingCoreCryptoError(let errorCode) {
|
|
||||||
throw CryptoKitError.underlyingCoreCryptoError(error: errorCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
||||||
enum OpenSSLAESGCMImpl {
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(tag: copy tag)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
static func seal(
|
|
||||||
key: SymmetricKey,
|
|
||||||
message: RawSpan,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: AES.GCM.Nonce,
|
|
||||||
authenticatedData: RawSpan?,
|
|
||||||
tag: inout OutputRawSpan
|
|
||||||
) throws {
|
|
||||||
let aead = try Self._backingAEAD(key: key)
|
|
||||||
let authenticatedData = if let authenticatedData { authenticatedData } else { RawSpan() }
|
|
||||||
try nonce.withUnsafeBytes { nonceBuffer in
|
|
||||||
try aead.seal(
|
|
||||||
message: message,
|
|
||||||
key: key,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonceBuffer.bytes,
|
|
||||||
authenticatedData: authenticatedData,
|
|
||||||
tag: &tag
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if compiler(<6.3)
|
|
||||||
@_lifetime(output: copy output)
|
|
||||||
#endif
|
|
||||||
static func open(
|
|
||||||
key: SymmetricKey,
|
|
||||||
message: RawSpan,
|
|
||||||
output: inout OutputRawSpan,
|
|
||||||
nonce: AES.GCM.Nonce,
|
|
||||||
authenticatedData: RawSpan?,
|
|
||||||
tag: RawSpan
|
|
||||||
) throws {
|
|
||||||
let aead = try Self._backingAEAD(key: key)
|
|
||||||
let authenticatedData = if let authenticatedData { authenticatedData } else { RawSpan() }
|
|
||||||
try nonce.withUnsafeBytes { nonceBuffer in
|
|
||||||
try aead.open(
|
|
||||||
message: message,
|
|
||||||
key: key,
|
|
||||||
output: &output,
|
|
||||||
nonce: nonceBuffer.bytes,
|
|
||||||
tag: tag,
|
|
||||||
authenticatedData: authenticatedData
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func _backingAEAD(key: SymmetricKey) throws -> BoringSSLAEAD {
|
|
||||||
switch key.bitCount {
|
|
||||||
case 128:
|
|
||||||
return .aes128gcm
|
|
||||||
case 192:
|
|
||||||
return .aes192gcm
|
|
||||||
case 256:
|
|
||||||
return .aes256gcm
|
|
||||||
default:
|
|
||||||
throw CryptoKitError.incorrectKeySize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,14 +16,12 @@ add_library(CryptoExtras
|
|||||||
"AES/AES_CBC.swift"
|
"AES/AES_CBC.swift"
|
||||||
"AES/AES_CFB.swift"
|
"AES/AES_CFB.swift"
|
||||||
"AES/AES_CTR.swift"
|
"AES/AES_CTR.swift"
|
||||||
"AES/AES_GCM+Extensions.swift"
|
|
||||||
"AES/AES_GCM_SIV.swift"
|
"AES/AES_GCM_SIV.swift"
|
||||||
"AES/Block Function.swift"
|
"AES/Block Function.swift"
|
||||||
"AES/BoringSSL/AES_CBC_boring.swift"
|
"AES/BoringSSL/AES_CBC_boring.swift"
|
||||||
"AES/BoringSSL/AES_CFB_boring.swift"
|
"AES/BoringSSL/AES_CFB_boring.swift"
|
||||||
"AES/BoringSSL/AES_CTR_boring.swift"
|
"AES/BoringSSL/AES_CTR_boring.swift"
|
||||||
"AES/BoringSSL/AES_GCM_SIV_boring.swift"
|
"AES/BoringSSL/AES_GCM_SIV_boring.swift"
|
||||||
"AES/BoringSSL/AES_GCM_boring.swift"
|
|
||||||
"AES/CMAC.swift"
|
"AES/CMAC.swift"
|
||||||
"ARC/ARC+API.swift"
|
"ARC/ARC+API.swift"
|
||||||
"ARC/ARC.swift"
|
"ARC/ARC.swift"
|
||||||
|
|||||||
@@ -56,9 +56,6 @@ extension Curve25519.Signing.PrivateKey {
|
|||||||
public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 {
|
public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 {
|
||||||
let bytes = Array(derRepresentation)
|
let bytes = Array(derRepresentation)
|
||||||
let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes)
|
let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes)
|
||||||
guard key.algorithm == .ed25519 else {
|
|
||||||
throw CryptoKitASN1Error.invalidASN1Object
|
|
||||||
}
|
|
||||||
self = try .init(rawRepresentation: key.privateKey.bytes)
|
self = try .init(rawRepresentation: key.privateKey.bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,9 +138,6 @@ extension Curve25519.KeyAgreement.PrivateKey {
|
|||||||
public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 {
|
public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 {
|
||||||
let bytes = Array(derRepresentation)
|
let bytes = Array(derRepresentation)
|
||||||
let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes)
|
let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes)
|
||||||
guard key.algorithm == .x25519 else {
|
|
||||||
throw CryptoKitASN1Error.invalidASN1Object
|
|
||||||
}
|
|
||||||
self = try .init(rawRepresentation: key.privateKey.bytes)
|
self = try .init(rawRepresentation: key.privateKey.bytes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,16 +89,13 @@ internal struct BoringSSLScrypt {
|
|||||||
if let maxMemory {
|
if let maxMemory {
|
||||||
memoryLimit = maxMemory
|
memoryLimit = maxMemory
|
||||||
} else {
|
} else {
|
||||||
// BoringSSL needs 128 * blockSize * (rounds + parallelism + 1) bytes; one page is added as slack.
|
// This computes the maximum amount of memory that will be used by the scrypt algorithm with an additional memory page to spare. This value will be used by the BoringSSL as the memory limit for the algorithm.
|
||||||
|
// An additional memory page is added to the computed value (using POSIX specification) to ensure that the memory limit is not too tight.
|
||||||
let (blockBytes, blockBytesOverflowed) = 128.multipliedReportingOverflow(by: blockSize)
|
let (blockBytes, blockBytesOverflowed) = 128.multipliedReportingOverflow(by: blockSize)
|
||||||
let (scratchBlockCount, scratchBlockCountOverflowed) = rounds.addingReportingOverflow(parallelism)
|
let (laneBytes, laneBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: rounds)
|
||||||
let (blockCount, blockCountOverflowed) = scratchBlockCount.addingReportingOverflow(1)
|
let (stateBytes, stateBytesOverflowed) = laneBytes.multipliedReportingOverflow(by: parallelism)
|
||||||
let (stateBytes, stateBytesOverflowed) = blockBytes.multipliedReportingOverflow(by: blockCount)
|
|
||||||
let (computedLimit, computedLimitOverflowed) = stateBytes.addingReportingOverflow(getPageSize())
|
let (computedLimit, computedLimitOverflowed) = stateBytes.addingReportingOverflow(getPageSize())
|
||||||
guard
|
guard !blockBytesOverflowed, !laneBytesOverflowed, !stateBytesOverflowed, !computedLimitOverflowed else {
|
||||||
!blockBytesOverflowed, !scratchBlockCountOverflowed, !blockCountOverflowed, !stateBytesOverflowed,
|
|
||||||
!computedLimitOverflowed
|
|
||||||
else {
|
|
||||||
throw CryptoKitError.incorrectParameterSize
|
throw CryptoKitError.incorrectParameterSize
|
||||||
}
|
}
|
||||||
memoryLimit = computedLimit
|
memoryLimit = computedLimit
|
||||||
|
|||||||
@@ -1,107 +0,0 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
|
||||||
//
|
|
||||||
// This source file is part of the SwiftCrypto open source project
|
|
||||||
//
|
|
||||||
// Copyright (c) 2026 Apple Inc. and the SwiftCrypto project authors
|
|
||||||
// Licensed under Apache License v2.0
|
|
||||||
//
|
|
||||||
// See LICENSE.txt for license information
|
|
||||||
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
//
|
|
||||||
//===----------------------------------------------------------------------===//
|
|
||||||
|
|
||||||
import Crypto
|
|
||||||
import CryptoExtras
|
|
||||||
import XCTest
|
|
||||||
|
|
||||||
final class AESGCMExtensionsTests: XCTestCase {
|
|
||||||
func testSealOverOutputSpanAppendsCiphertext() throws {
|
|
||||||
guard #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) else {
|
|
||||||
throw XCTSkip("Array.span requires macOS 26.0+, iOS 26.0+, watchOS 26.0+, tvOS 26.0+, visionOS 26.0+")
|
|
||||||
}
|
|
||||||
let key = SymmetricKey(size: .bits256)
|
|
||||||
let nonce = AES.GCM.Nonce()
|
|
||||||
let plaintext = Array("Some Super Secret Message".utf8)
|
|
||||||
let authenticatedData = Array("Some Authenticated Data".utf8)
|
|
||||||
|
|
||||||
var ciphertext = [UInt8](repeating: 0xFF, count: 1 + plaintext.count)
|
|
||||||
var tag = [UInt8](repeating: 0, count: 16)
|
|
||||||
try ciphertext.withUnsafeMutableBytes { ciphertextBuffer in
|
|
||||||
try tag.withUnsafeMutableBytes { tagBuffer in
|
|
||||||
var ciphertextOutput = OutputRawSpan(buffer: ciphertextBuffer, initializedCount: 1)
|
|
||||||
var tagOutput = OutputRawSpan(buffer: tagBuffer, initializedCount: 0)
|
|
||||||
try AES.GCM._seal(
|
|
||||||
plaintext.span.bytes,
|
|
||||||
using: key,
|
|
||||||
nonce: nonce,
|
|
||||||
authenticating: authenticatedData.span.bytes,
|
|
||||||
tag: &tagOutput,
|
|
||||||
output: &ciphertextOutput
|
|
||||||
)
|
|
||||||
XCTAssertEqual(ciphertextOutput.byteCount, ciphertextBuffer.count)
|
|
||||||
XCTAssertEqual(tagOutput.byteCount, tagBuffer.count)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let sealedBox = try AES.GCM.seal(plaintext, using: key, nonce: nonce, authenticating: authenticatedData)
|
|
||||||
XCTAssertEqual(ciphertext, [0xFF] + Array(sealedBox.ciphertext))
|
|
||||||
XCTAssertEqual(tag, Array(sealedBox.tag))
|
|
||||||
}
|
|
||||||
|
|
||||||
func testOpenOverOutputSpanAppendsPlaintext() throws {
|
|
||||||
guard #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) else {
|
|
||||||
throw XCTSkip("Array.span requires macOS 26.0+, iOS 26.0+, watchOS 26.0+, tvOS 26.0+, visionOS 26.0+")
|
|
||||||
}
|
|
||||||
let key = SymmetricKey(size: .bits256)
|
|
||||||
let nonce = AES.GCM.Nonce()
|
|
||||||
let plaintext = Array("Some Super Secret Message".utf8)
|
|
||||||
let authenticatedData = Array("Some Authenticated Data".utf8)
|
|
||||||
let sealedBox = try AES.GCM.seal(plaintext, using: key, nonce: nonce, authenticating: authenticatedData)
|
|
||||||
let ciphertext = Array(sealedBox.ciphertext)
|
|
||||||
let tag = Array(sealedBox.tag)
|
|
||||||
|
|
||||||
var decrypted = [UInt8](repeating: 0xFF, count: 1 + ciphertext.count)
|
|
||||||
try decrypted.withUnsafeMutableBytes { decryptedBuffer in
|
|
||||||
var output = OutputRawSpan(buffer: decryptedBuffer, initializedCount: 1)
|
|
||||||
try AES.GCM._open(
|
|
||||||
ciphertext.span.bytes,
|
|
||||||
using: key,
|
|
||||||
nonce: nonce,
|
|
||||||
authenticating: authenticatedData.span.bytes,
|
|
||||||
tag: tag.span.bytes,
|
|
||||||
output: &output
|
|
||||||
)
|
|
||||||
XCTAssertEqual(output.byteCount, decryptedBuffer.count)
|
|
||||||
}
|
|
||||||
XCTAssertEqual(decrypted, [0xFF] + plaintext)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testOpenOverOutputSpanWithModifiedTagThrows() throws {
|
|
||||||
guard #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) else {
|
|
||||||
throw XCTSkip("Array.span requires macOS 26.0+, iOS 26.0+, watchOS 26.0+, tvOS 26.0+, visionOS 26.0+")
|
|
||||||
}
|
|
||||||
let key = SymmetricKey(size: .bits256)
|
|
||||||
let nonce = AES.GCM.Nonce()
|
|
||||||
let sealedBox = try AES.GCM.seal(Array("Some Super Secret Message".utf8), using: key, nonce: nonce)
|
|
||||||
let ciphertext = Array(sealedBox.ciphertext)
|
|
||||||
var tag = Array(sealedBox.tag)
|
|
||||||
tag[0] ^= 1
|
|
||||||
|
|
||||||
var decrypted = [UInt8](repeating: 0, count: ciphertext.count)
|
|
||||||
try decrypted.withUnsafeMutableBytes { decryptedBuffer in
|
|
||||||
var output = OutputRawSpan(buffer: decryptedBuffer, initializedCount: 0)
|
|
||||||
XCTAssertThrowsError(
|
|
||||||
try AES.GCM._open(
|
|
||||||
ciphertext.span.bytes,
|
|
||||||
using: key,
|
|
||||||
nonce: nonce,
|
|
||||||
tag: tag.span.bytes,
|
|
||||||
output: &output
|
|
||||||
)
|
|
||||||
)
|
|
||||||
XCTAssertEqual(output.byteCount, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -89,14 +89,4 @@ final class Curve25519DERTests: XCTestCase {
|
|||||||
let key = try Curve25519.KeyAgreement.PrivateKey(derRepresentation: derBytes)
|
let key = try Curve25519.KeyAgreement.PrivateKey(derRepresentation: derBytes)
|
||||||
XCTAssertEqual(key.rawRepresentation.count, 32)
|
XCTAssertEqual(key.rawRepresentation.count, 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPrivateKeyWithMismatchedAlgorithmThrows() throws {
|
|
||||||
let signingKey = Curve25519.Signing.PrivateKey()
|
|
||||||
let keyAgreementKey = Curve25519.KeyAgreement.PrivateKey()
|
|
||||||
|
|
||||||
XCTAssertThrowsError(try Curve25519.Signing.PrivateKey(derRepresentation: keyAgreementKey.derRepresentation))
|
|
||||||
XCTAssertThrowsError(try Curve25519.Signing.PrivateKey(pemRepresentation: keyAgreementKey.pemRepresentation))
|
|
||||||
XCTAssertThrowsError(try Curve25519.KeyAgreement.PrivateKey(derRepresentation: signingKey.derRepresentation))
|
|
||||||
XCTAssertThrowsError(try Curve25519.KeyAgreement.PrivateKey(pemRepresentation: signingKey.pemRepresentation))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -125,20 +125,6 @@ class ScryptTests: XCTestCase {
|
|||||||
XCTAssertEqual(bounded, unbounded)
|
XCTAssertEqual(bounded, unbounded)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDeriveKeyWithDefaultMaxMemoryAndBlockSizeAboveRoundsSucceeds() throws {
|
|
||||||
// Needs 128 * 1024 * (2 + 1 + 1) bytes, more than 128 * blockSize * rounds * parallelism.
|
|
||||||
XCTAssertNoThrow(
|
|
||||||
try KDF.Scrypt.deriveKey(
|
|
||||||
from: [UInt8](),
|
|
||||||
salt: [UInt8](),
|
|
||||||
outputByteCount: 32,
|
|
||||||
rounds: 2,
|
|
||||||
blockSize: 1024,
|
|
||||||
parallelism: 1
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testDeriveKeyWithOutOfRangeParametersThrows() throws {
|
func testDeriveKeyWithOutOfRangeParametersThrows() throws {
|
||||||
let password = Array("password".utf8)
|
let password = Array("password".utf8)
|
||||||
let salt = Array("salt".utf8)
|
let salt = Array("salt".utf8)
|
||||||
@@ -149,9 +135,8 @@ class ScryptTests: XCTestCase {
|
|||||||
(1024, -1, 1, 32),
|
(1024, -1, 1, 32),
|
||||||
(1024, 8, -1, 32),
|
(1024, 8, -1, 32),
|
||||||
(1024, 8, 1, -1),
|
(1024, 8, 1, -1),
|
||||||
// 128 * blockSize * (rounds + parallelism + 1) overflows Int.
|
// 128 * blockSize * rounds * parallelism overflows Int.
|
||||||
(1 << 32, 1 << 30, 1, 32),
|
(1 << 32, 1 << 30, 1, 32),
|
||||||
(1024, 8, .max, 32),
|
|
||||||
] {
|
] {
|
||||||
XCTAssertThrowsError(
|
XCTAssertThrowsError(
|
||||||
try KDF.Scrypt.deriveKey(
|
try KDF.Scrypt.deriveKey(
|
||||||
|
|||||||
Reference in new issue
Block a user