AEADChaCha20Poly1305.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // AEADChaCha20Poly1305.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2022 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. //
  8. // In no event will the authors be held liable for any damages arising from the use of this software.
  9. //
  10. // 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:
  11. //
  12. // - 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.
  13. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. // - This notice may not be removed or altered from any source or binary distribution.
  15. //
  16. //
  17. // https://tools.ietf.org/html/rfc7539#section-2.8.1
  18. /// AEAD_CHACHA20_POLY1305
  19. public final class AEADChaCha20Poly1305: AEAD {
  20. public static let kLen = 32 // key length
  21. public static var ivRange = Range<Int>(12...12)
  22. /// Authenticated encryption
  23. public static func encrypt(_ plainText: Array<UInt8>, key: Array<UInt8>, iv: Array<UInt8>, authenticationHeader: Array<UInt8>) throws -> (cipherText: Array<UInt8>, authenticationTag: Array<UInt8>) {
  24. let cipher = try ChaCha20(key: key, iv: iv)
  25. return try self.encrypt(cipher: cipher, plainText, key: key, iv: iv, authenticationHeader: authenticationHeader)
  26. }
  27. public static func encrypt(cipher: Cipher, _ plainText: Array<UInt8>, key: Array<UInt8>, iv: Array<UInt8>, authenticationHeader: Array<UInt8>) throws -> (cipherText: Array<UInt8>, authenticationTag: Array<UInt8>) {
  28. var polykey = Array<UInt8>(repeating: 0, count: kLen)
  29. var toEncrypt = polykey
  30. polykey = try cipher.encrypt(polykey)
  31. toEncrypt += polykey
  32. toEncrypt += plainText
  33. let fullCipherText = try cipher.encrypt(toEncrypt)
  34. let cipherText = Array(fullCipherText.dropFirst(64))
  35. let tag = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader)
  36. return (cipherText, tag)
  37. }
  38. /// Authenticated decryption
  39. 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) {
  40. let cipher = try ChaCha20(key: key, iv: iv)
  41. return try self.decrypt(cipher: cipher, cipherText: cipherText, key: key, iv: iv, authenticationHeader: authenticationHeader, authenticationTag: authenticationTag)
  42. }
  43. static func decrypt(cipher: Cipher, cipherText: Array<UInt8>, key: Array<UInt8>, iv: Array<UInt8>, authenticationHeader: Array<UInt8>, authenticationTag: Array<UInt8>) throws -> (plainText: Array<UInt8>, success: Bool) {
  44. let polykey = try cipher.encrypt(Array<UInt8>(repeating: 0, count: self.kLen))
  45. let mac = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader)
  46. guard mac == authenticationTag else {
  47. return (cipherText, false)
  48. }
  49. var toDecrypt = Array<UInt8>(reserveCapacity: cipherText.count + 64)
  50. toDecrypt += polykey
  51. toDecrypt += polykey
  52. toDecrypt += cipherText
  53. let fullPlainText = try cipher.decrypt(toDecrypt)
  54. let plainText = Array(fullPlainText.dropFirst(64))
  55. return (plainText, true)
  56. }
  57. }