1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019-2020 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
//
//===----------------------------------------------------------------------===//
#if CRYPTO_IN_SWIFTPM && !CRYPTO_IN_SWIFTPM_FORCE_BUILD_API
@_exported import CryptoKit
#else
import Foundation
// MARK: - Generated file, do NOT edit
// any edits of this file WILL be overwritten and thus discarded
// see section `gyb` in `README` for details.
%{
ciphers = [{"name": "AES.GCM", "recommendedNonceSize": "AES.GCM.defaultNonceByteCount", "nonceValidation": "< AES.GCM.defaultNonceByteCount"},{"name": "ChaChaPoly", "recommendedNonceSize": "ChaChaPoly.nonceByteCount", "nonceValidation": "!= ChaChaPoly.nonceByteCount"}]
}%
% for cipher in ciphers:
%{
name = cipher["name"]
nonceSize = cipher["recommendedNonceSize"]
nonceValidation = cipher["nonceValidation"]
}%
// MARK: - ${name} + Nonce
extension ${name} {
/// A value used once during a cryptographic operation and then discarded.
///
/// Don’t reuse the same nonce for multiple calls to encryption APIs. It’s critical
/// that nonces are unique per call to encryption APIs in order to protect the
/// integrity of the encryption.
public struct Nonce: ContiguousBytes, Sequence {
let bytes: Data
/// Creates a new random nonce.
///
/// The default nonce is a 12-byte random nonce.
public init() {
var data = Data(repeating: 0, count: ${nonceSize})
data.withUnsafeMutableBytes {
assert($0.count == ${nonceSize})
$0.initializeWithRandomBytes(count: ${nonceSize})
}
self.bytes = data
}
/// Creates a nonce from the given data.
///
/// Unless your use case calls for a nonce with a specific value, use the
/// ``init()`` method to instead create a random nonce.
///
/// - Parameters:
/// - data: A 12-byte data representation of the nonce. The initializer throws an
/// error if the data has a length other than 12 bytes.
public init<D: DataProtocol>(data: D) throws {
if data.count ${nonceValidation} {
throw CryptoKitError.incorrectParameterSize
}
self.bytes = Data(data)
}
/// Calls the given closure with a pointer to the underlying bytes of the array’s
/// contiguous storage.
///
/// - Parameters:
/// - body: A closure with an `UnsafeRawBufferPointer` parameter that points to the
/// contiguous storage for the array. The system creates the storage if it doesn’t
/// exist. If body has a return value, that value is also used as the return value
/// for the ``withUnsafeBytes(_:)`` method. The argument is valid only for
/// the duration of the closure’s execution.
///
/// - Returns: The return value, if any, of the body closure parameter.
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.bytes.withUnsafeBytes(body)
}
/// Returns an iterator over the elements of the nonce.
public func makeIterator() -> Array<UInt8>.Iterator {
self.withUnsafeBytes({ (buffPtr) in
return Array(buffPtr).makeIterator()
})
}
}
}
% end
#endif // Linux or !SwiftPM
|