-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
pal_swiftbindings.swift
195 lines (165 loc) · 6.24 KB
/
pal_swiftbindings.swift
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
import CryptoKit
import Foundation
protocol NonceProtocol {
init<D>(data: D) throws where D : DataProtocol
}
protocol SealedBoxProtocol {
associatedtype Nonce : NonceProtocol
var ciphertext: Data { get }
var tag: Data { get }
init<C, T>(
nonce: Nonce,
ciphertext: C,
tag: T
) throws where C : DataProtocol, T : DataProtocol
}
@available(iOS 13, tvOS 13, *)
protocol AEADSymmetricAlgorithm {
associatedtype SealedBox : SealedBoxProtocol
static func seal<Plaintext>(_ plaintext: Plaintext, using key: SymmetricKey, nonce: SealedBox.Nonce?) throws -> SealedBox where Plaintext: DataProtocol
static func seal<Plaintext, AuthenticatedData>(_ plaintext: Plaintext, using key: SymmetricKey, nonce: SealedBox.Nonce?, authenticating additionalData: AuthenticatedData) throws -> SealedBox where Plaintext: DataProtocol, AuthenticatedData: DataProtocol
static func open<AuthenticatedData>(_ sealedBox: SealedBox, using key: SymmetricKey, authenticating additionalData: AuthenticatedData) throws -> Data where AuthenticatedData: DataProtocol
static func open(_ sealedBox: SealedBox, using key: SymmetricKey) throws -> Data
}
@available(iOS 13, tvOS 13, *)
extension AES.GCM.Nonce: NonceProtocol {}
@available(iOS 13, tvOS 13, *)
extension AES.GCM.SealedBox: SealedBoxProtocol {
typealias Nonce = AES.GCM.Nonce
}
@available(iOS 13, tvOS 13, *)
extension AES.GCM: AEADSymmetricAlgorithm {}
@available(iOS 13, tvOS 13, *)
extension ChaChaPoly.Nonce: NonceProtocol {}
@available(iOS 13, tvOS 13, *)
extension ChaChaPoly.SealedBox: SealedBoxProtocol {
typealias Nonce = ChaChaPoly.Nonce
}
@available(iOS 13, tvOS 13, *)
extension ChaChaPoly: AEADSymmetricAlgorithm {}
@available(iOS 13, tvOS 13, *)
func encrypt<Algorithm>(
_ algorithm: Algorithm.Type,
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeMutableBufferPointer<UInt8>,
tag: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>) throws where Algorithm: AEADSymmetricAlgorithm {
let symmetricKey = SymmetricKey(data: key)
let nonce = try Algorithm.SealedBox.Nonce(data: nonceData)
let result = try Algorithm.seal(plaintext, using: symmetricKey, nonce: nonce, authenticating: aad)
// Copy results out of the SealedBox as the Data objects returned here are sometimes slices,
// which don't have a correct implementation of copyBytes.
// See https://github.com/apple/swift-foundation/issues/638 for more information.
let resultCiphertext = Data(result.ciphertext)
let resultTag = Data(result.tag)
_ = resultCiphertext.copyBytes(to: cipherText)
_ = resultTag.copyBytes(to: tag)
}
@available(iOS 13, tvOS 13, *)
func decrypt<Algorithm>(
_ algorithm: Algorithm.Type,
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeBufferPointer<UInt8>,
tag: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>) throws where Algorithm: AEADSymmetricAlgorithm {
let symmetricKey = SymmetricKey(data: key)
let nonce = try Algorithm.SealedBox.Nonce(data: nonceData)
let sealedBox = try Algorithm.SealedBox(nonce: nonce, ciphertext: cipherText, tag: tag)
let result = try Algorithm.open(sealedBox, using: symmetricKey, authenticating: aad)
_ = result.copyBytes(to: plaintext)
}
@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Encrypt")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_ChaCha20Poly1305Encrypt(
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeMutableBufferPointer<UInt8>,
tag: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>
) throws {
return try encrypt(
ChaChaPoly.self,
key: key,
nonceData: nonceData,
plaintext: plaintext,
cipherText: cipherText,
tag: tag,
aad: aad)
}
@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Decrypt")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_ChaCha20Poly1305Decrypt(
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeBufferPointer<UInt8>,
tag: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>
) throws {
return try decrypt(
ChaChaPoly.self,
key: key,
nonceData: nonceData,
cipherText: cipherText,
tag: tag,
plaintext: plaintext,
aad: aad);
}
@_silgen_name("AppleCryptoNative_AesGcmEncrypt")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_AesGcmEncrypt(
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeMutableBufferPointer<UInt8>,
tag: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>
) throws {
return try encrypt(
AES.GCM.self,
key: key,
nonceData: nonceData,
plaintext: plaintext,
cipherText: cipherText,
tag: tag,
aad: aad)
}
@_silgen_name("AppleCryptoNative_AesGcmDecrypt")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_AesGcmDecrypt(
key: UnsafeBufferPointer<UInt8>,
nonceData: UnsafeBufferPointer<UInt8>,
cipherText: UnsafeBufferPointer<UInt8>,
tag: UnsafeBufferPointer<UInt8>,
plaintext: UnsafeMutableBufferPointer<UInt8>,
aad: UnsafeBufferPointer<UInt8>
) throws {
return try decrypt(
AES.GCM.self,
key: key,
nonceData: nonceData,
cipherText: cipherText,
tag: tag,
plaintext: plaintext,
aad: aad);
}
@_silgen_name("AppleCryptoNative_IsAuthenticationFailure")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_IsAuthenticationFailure(error: Error) -> Bool {
if let error = error as? CryptoKitError {
switch error {
case .authenticationFailure:
return true
default:
return false
}
}
return false
}