2
0

AEADChaCha20Poly1305.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // ChaCha20Poly1305.swift
  3. // CryptoSwift
  4. //
  5. // Copyright (C) 2014-2017 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. var polykey = Array<UInt8>(repeating: 0, count: kLen)
  26. var toEncrypt = polykey
  27. polykey = try cipher.encrypt(polykey)
  28. toEncrypt += polykey
  29. toEncrypt += plainText
  30. let fullCipherText = try cipher.encrypt(toEncrypt)
  31. let cipherText = Array(fullCipherText.dropFirst(64))
  32. let tag = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader)
  33. return (cipherText, tag)
  34. }
  35. /// Authenticated decryption
  36. 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) {
  37. let chacha = try ChaCha20(key: key, iv: iv)
  38. let polykey = try chacha.encrypt(Array<UInt8>(repeating: 0, count: self.kLen))
  39. let mac = try calculateAuthenticationTag(authenticator: Poly1305(key: polykey), cipherText: cipherText, authenticationHeader: authenticationHeader)
  40. guard mac == authenticationTag else {
  41. return (cipherText, false)
  42. }
  43. var toDecrypt = Array<UInt8>(reserveCapacity: cipherText.count + 64)
  44. toDecrypt += polykey
  45. toDecrypt += polykey
  46. toDecrypt += cipherText
  47. let fullPlainText = try chacha.decrypt(toDecrypt)
  48. let plainText = Array(fullPlainText.dropFirst(64))
  49. return (plainText, true)
  50. }
  51. }