Skip to content

An implementation of Galois/Counter Mode for Swift 4.0.

License

Notifications You must be signed in to change notification settings

luke-park/SwiftGCM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SwiftGCM

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.

Features

  • 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).

Installing

To use SwiftGCM, simply drag SwiftGCM.swift into your project source files. You will also need to include Security.framework in your code and add a bridging header to import CommonCrypto:

#import <CommonCrypto/CommonCrypto.h>

Basic Example

let key: Data = ...
let nonce: Data = ...
let plaintext: Data = ...
let aad: Data = ...

let gcmEnc: SwiftGCM = try SwiftGCM(key: key, nonce: nonce)
let ciphertext: Data = try gcmEnc.encrypt(auth: aad, plaintext: plaintext)

let gcmDec: SwiftGCM = try SwiftGCM(key: key, nonce: nonce)
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.

Production Ready Examples

For examples on how to work with encryption in production, consult the example code in this repository.

Warning

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!

License

SwiftGCM is licensed under the MIT License. If you use SwiftGCM in your code, please attribute back to this repository.

Releases

No releases published

Packages

No packages published

Languages