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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//===----------------------------------------------------------------------===//
//
// 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.
// MARK: - AES.GCM + Nonce
extension AES.GCM {
/// 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: AES.GCM.defaultNonceByteCount)
data.withUnsafeMutableBytes {
assert($0.count == AES.GCM.defaultNonceByteCount)
$0.initializeWithRandomBytes(count: AES.GCM.defaultNonceByteCount)
}
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 < AES.GCM.defaultNonceByteCount {
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()
})
}
}
}
// MARK: - ChaChaPoly + Nonce
extension ChaChaPoly {
/// 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: ChaChaPoly.nonceByteCount)
data.withUnsafeMutableBytes {
assert($0.count == ChaChaPoly.nonceByteCount)
$0.initializeWithRandomBytes(count: ChaChaPoly.nonceByteCount)
}
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 != ChaChaPoly.nonceByteCount {
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()
})
}
}
}
#endif // Linux or !SwiftPM
|