-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AEADXChaCha20Poly1305.swift
83 lines (77 loc) · 3.46 KB
/
AEADXChaCha20Poly1305.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
//
// CryptoSwift
//
// Copyright (C) Marcin Krzyżanowski <marcin@krzyzanowskim.com>
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
//
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
// - This notice may not be removed or altered from any source or binary distribution.
//
import Foundation
/// This class implements the XChaCha20-Poly1305 Authenticated Encryption with
/// Associated Data (AEAD_XCHACHA20_POLY1305) construction, providing both encryption and authentication.
///
/// For more information about the XChaCha20-Poly1305 algorithm, refer to the IETF draft: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha
public final class AEADXChaCha20Poly1305: AEAD {
/// The key length (in bytes) required for the XChaCha20 cipher (32 bytes).
public static let kLen = 32 // key length
/// The valid range of initialization vector lengths for the XChaCha20 cipher (12 bytes).
public static var ivRange = Range<Int>(12...12)
/// Encrypts the given plaintext using the XChaCha20 cipher and generates an authentication
/// tag using the Poly1305 MAC.
///
/// - Parameters:
/// - plainText: The plaintext to be encrypted.
/// - key: The encryption key.
/// - iv: The initialization vector.
/// - authenticationHeader: The authentication header.
/// - Returns: A tuple containing the ciphertext and authentication tag.
/// - Throws: An error if encryption fails.
public static func encrypt(
_ plainText: Array<UInt8>,
key: Array<UInt8>,
iv: Array<UInt8>,
authenticationHeader: Array<UInt8>
) throws -> (cipherText: Array<UInt8>, authenticationTag: Array<UInt8>) {
try AEADChaCha20Poly1305.encrypt(
cipher: XChaCha20(key: key, iv: iv),
plainText,
key: key,
iv: iv,
authenticationHeader: authenticationHeader
)
}
/// Decrypts the given ciphertext using the XChaCha20 cipher and verifies the authentication
/// tag using the Poly1305 MAC.
///
/// - Parameters:
/// - cipherText: The ciphertext to be decrypted.
/// - key: The decryption key.
/// - iv: The initialization vector.
/// - authenticationHeader: The authentication header.
/// - authenticationTag: The authentication tag.
/// - Returns: A tuple containing the decrypted plaintext and a boolean value indicating
/// the success of the decryption and authentication process.
/// - Throws: An error if decryption fails.
public static func decrypt(
_ cipherText: Array<UInt8>,
key: Array<UInt8>,
iv: Array<UInt8>,
authenticationHeader: Array<UInt8>,
authenticationTag: Array<UInt8>
) throws -> (plainText: Array<UInt8>, success: Bool) {
try AEADChaCha20Poly1305.decrypt(
cipher: XChaCha20(key: key, iv: iv),
cipherText: cipherText,
key: key,
iv: iv,
authenticationHeader: authenticationHeader,
authenticationTag: authenticationTag
)
}
}