Marcin Krzyzanowski 7 ani în urmă
părinte
comite
949e3a7343
2 a modificat fișierele cu 27 adăugiri și 13 ștergeri
  1. 20 6
      Sources/CryptoSwift/BlockMode/CCM.swift
  2. 7 7
      Tests/Tests/AESTests.swift

+ 20 - 6
Sources/CryptoSwift/BlockMode/CCM.swift

@@ -23,6 +23,7 @@ import Glibc
 #endif
 
 
+/// Counter with Cipher Block Chaining-Message Authentication Code
 public struct CCM: StreamMode {
     public enum Error: Swift.Error {
         /// Invalid IV
@@ -42,17 +43,30 @@ public struct CCM: StreamMode {
     /// For decryption, this is a known Tag to validate against.
     public var authenticationTag: Array<UInt8>?
 
-    // encrypt
-    public init(nonce: Array<UInt8>, tagLength: Int, messageLength: Int, additionalAuthenticatedData: Array<UInt8>? = nil) {
-        self.nonce = nonce
+    /// Initialize CCM
+    ///
+    /// - Parameters:
+    ///   - iv: Initialization vector. Nonce. Valid length between 7 and 13 bytes.
+    ///   - tagLength: Authentication tag length, in bytes. Value of {4, 6, 8, 10, 12, 14, 16}.
+    ///   - messageLength: Plaintext message length (excluding tag if attached). Length have to be provided in advance.
+    ///   - additionalAuthenticatedData: Additional authenticated data.
+    public init(iv: Array<UInt8>, tagLength: Int, messageLength: Int, additionalAuthenticatedData: Array<UInt8>? = nil) {
+        self.nonce = iv
         self.tagLength = tagLength
         self.additionalAuthenticatedData = additionalAuthenticatedData
         self.messageLength = messageLength // - tagLength
     }
 
-    // decrypt
-    public init(nonce: Array<UInt8>, tagLength: Int, messageLength: Int, authenticationTag: Array<UInt8>, additionalAuthenticatedData: Array<UInt8>? = nil) {
-        self.init(nonce: nonce, tagLength: tagLength, messageLength: messageLength, additionalAuthenticatedData: additionalAuthenticatedData)
+    /// Initialize CCM
+    ///
+    /// - Parameters:
+    ///   - iv: Initialization vector. Nonce. Valid length between 7 and 13 bytes.
+    ///   - tagLength: Authentication tag length, in bytes. Value of {4, 6, 8, 10, 12, 14, 16}.
+    ///   - messageLength: Plaintext message length (excluding tag if attached). Length have to be provided in advance.
+    ///   - authenticationTag: Authentication Tag value if not concatenated to ciphertext.
+    ///   - additionalAuthenticatedData: Additional authenticated data.
+    public init(iv: Array<UInt8>, tagLength: Int, messageLength: Int, authenticationTag: Array<UInt8>, additionalAuthenticatedData: Array<UInt8>? = nil) {
+        self.init(iv: iv, tagLength: tagLength, messageLength: messageLength, additionalAuthenticatedData: additionalAuthenticatedData)
         self.authenticationTag = authenticationTag
     }
 

+ 7 - 7
Tests/Tests/AESTests.swift

@@ -584,7 +584,7 @@ extension AESTests {
         let plaintext: Array<UInt8> = [0x20, 0x21, 0x22, 0x23]
         let expected: Array<UInt8> =  [0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 4, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 4, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
     }
@@ -596,7 +596,7 @@ extension AESTests {
         let ciphertext: Array<UInt8> = [0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d]
         let expected: Array<UInt8> = [0x20, 0x21, 0x22, 0x23]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 4, messageLength: ciphertext.count - 4, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 4, messageLength: ciphertext.count - 4, additionalAuthenticatedData: aad), padding: .noPadding)
         let decrypted = try! aes.decrypt(ciphertext)
         XCTAssertEqual(decrypted, expected, "decryption failed")
     }
@@ -608,7 +608,7 @@ extension AESTests {
         let plaintext: Array<UInt8> = [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f]
         let expected: Array<UInt8>  = [0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 6, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 6, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
     }
@@ -620,7 +620,7 @@ extension AESTests {
         let ciphertext: Array<UInt8> = [0xd2, 0xa1, 0xf0, 0xe0, 0x51, 0xea, 0x5f, 0x62, 0x08, 0x1a, 0x77, 0x92, 0x07, 0x3d, 0x59, 0x3d, 0x1f, 0xc6, 0x4f, 0xbf, 0xac, 0xcd]
         let expected: Array<UInt8>   = [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 6, messageLength: ciphertext.count - 6, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 6, messageLength: ciphertext.count - 6, additionalAuthenticatedData: aad), padding: .noPadding)
         let plaintext = try! aes.decrypt(ciphertext)
         XCTAssertEqual(plaintext, expected, "encryption failed")
     }
@@ -632,7 +632,7 @@ extension AESTests {
         let plaintext: Array<UInt8> = [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]
         let expected: Array<UInt8>  = [0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 8, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 8, messageLength: plaintext.count, additionalAuthenticatedData: aad), padding: .noPadding)
         let encrypted = try! aes.encrypt(plaintext)
         XCTAssertEqual(encrypted, expected, "encryption failed")
     }
@@ -644,7 +644,7 @@ extension AESTests {
         let ciphertext: Array<UInt8> = [0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51]
         let expected: Array<UInt8> =   [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 8, messageLength: ciphertext.count - 8, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 8, messageLength: ciphertext.count - 8, additionalAuthenticatedData: aad), padding: .noPadding)
         let plaintext = try! aes.decrypt(ciphertext)
         XCTAssertEqual(plaintext, expected, "encryption failed")
     }
@@ -656,7 +656,7 @@ extension AESTests {
         let ciphertext: Array<UInt8> = [0xe3, 0xb2, 0x01, 0xa9, 0xf5, 0xb7, 0x1a, 0x7a, 0x9b, 0x1c, 0xea, 0xec, 0xcd, 0x97, 0xe7, 0x0b, 0x61, 0x76, 0xaa, 0xd9, 0xa4, 0x42, 0x8a, 0xa5, 0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51]
         let expected: Array<UInt8> =   [0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37]
 
-        let aes = try! AES(key: key, blockMode: CCM(nonce: nonce, tagLength: 8, messageLength: ciphertext.count - 8, additionalAuthenticatedData: aad), padding: .noPadding)
+        let aes = try! AES(key: key, blockMode: CCM(iv: nonce, tagLength: 8, messageLength: ciphertext.count - 8, additionalAuthenticatedData: aad), padding: .noPadding)
         var decryptor = try! aes.makeDecryptor()
 
         var plaintext = [UInt8]()