This library implements Galois/Counter Mode for Swift. It has no dependencies other than Apple's CommonCrypto (CCCrypt
). You can include SwiftGCM in your project by simply dragging and dropping SwiftGCM.swift
into your project source files.
- Support for AES-128, AES-192 and AES-256.
- Support for Additional Authenticated Data.
- Support for arbitrary nonce sizes (96 bits recommended).
- Support for 128, 120, 112, 104, 96, 64 and 32 bit authentication tag sizes (128 bits recommended).
To use SwiftGCM, simply drag SwiftGCM.swift
into your project source files.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler. It is in early development, but Alamofire does support its use on supported platforms.
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/denisoliveira/SwiftGCM.git", .branch("master"))
]
let key: Data = ...
let nonce: Data = ...
let plaintext: Data = ...
let aad: Data = ...
let tagSize = 16
let gcmEnc: SwiftGCM = try SwiftGCM(key: key, nonce: nonce, tagSize:tagSize)
let ciphertext: Data = try gcmEnc.encrypt(auth: aad, plaintext: plaintext)
let gcmDec: SwiftGCM = try SwiftGCM(key: key, nonce: nonce, tagSize:tagSize)
let result: Data = try gcmDec.decrypt(auth: aad, ciphertext: ciphertext)
Once an instance of SwiftGCM
has been used to encrypt or decrypt, it cannot be used again, as per the example above. Note that auth
(the AAD) can be omitted by passing nil
.
Warning: Reusing a key/nonce pair can lead to trivial exposure of plaintext. If you are not versed in cryptography, ensure you view the production ready examples below before implementing this code in your project.
For examples on how to work with encryption in production, consult the example code in this repository.
This library has passed no security audits or anything similar, it is merely an implementation of GCM mode as per The Galois/Counter Mode of Operation by David A. McGrew and John Viega. It may contain bugs or errors in implementation. Your contribution is appreciated!
SwiftGCM is licensed under the MIT License. If you use SwiftGCM in your code, please attribute back to this repository.