Browse Source

AES and ChaCha20 block size as static property instead of function.

Marcin Krzyżanowski 10 years ago
parent
commit
8052106625
2 changed files with 15 additions and 17 deletions
  1. 9 11
      CryptoSwift/AES.swift
  2. 6 6
      CryptoSwift/ChaCha20.swift

+ 9 - 11
CryptoSwift/AES.swift

@@ -26,6 +26,8 @@ private enum AESVariant:Int {
 
 public class AES {
     public let blockMode:CipherBlockMode
+    static let blockSize:Int = 16 // 128 /8
+    
     private let variant:AESVariant
     private let key:[UInt8]
     private let iv:[UInt8]?
@@ -113,7 +115,7 @@ public class AES {
             return nil
         }
         
-        if (blockMode.needIV && iv.count != AES.blockSizeBytes()) {
+        if (blockMode.needIV && iv.count != AES.blockSize) {
             assert(false, "Block size and Initialization Vector must be the same length!")
             return nil
         }
@@ -121,14 +123,10 @@ public class AES {
     
     convenience public init?(key:[UInt8], blockMode:CipherBlockMode = .CBC) {
         // default IV is all 0x00...
-        let defaultIV = [UInt8](count: AES.blockSizeBytes(), repeatedValue: 0)
+        let defaultIV = [UInt8](count: AES.blockSize, repeatedValue: 0)
         self.init(key: key, iv: defaultIV, blockMode: blockMode)
     }
     
-    public class func blockSizeBytes() -> Int {
-        return 128 / 8 // 16 bytes
-    }
-
     /**
     Encrypt message. If padding is necessary, then PKCS7 padding is addedd and need to be removed after decryption.
     
@@ -141,14 +139,14 @@ public class AES {
         var finalBytes = bytes;
 
         if let padding = padding {
-            finalBytes = padding.add(bytes, blockSize: AES.blockSizeBytes())
-        } else if (bytes.count % AES.blockSizeBytes() != 0) {
+            finalBytes = padding.add(bytes, blockSize: AES.blockSize)
+        } else if (bytes.count % AES.blockSize != 0) {
             // 128 bit block exceeded, need padding
             assert(false, "AES 128-bit block exceeded!");
             return nil
         }
         
-        let blocks = finalBytes.chunks(AES.blockSizeBytes())
+        let blocks = finalBytes.chunks(AES.blockSize)
         return blockMode.encryptBlocks(blocks, iv: self.iv, cipherOperation: encryptBlock)
     }
     
@@ -187,13 +185,13 @@ public class AES {
     }
     
     public func decrypt(bytes:[UInt8], padding:Padding? = PKCS7()) -> [UInt8]? {
-        if (bytes.count % AES.blockSizeBytes() != 0) {
+        if (bytes.count % AES.blockSize != 0) {
             // 128 bit block exceeded
             assert(false,"AES 128-bit block exceeded!")
             return nil
         }
         
-        let blocks = bytes.chunks(AES.blockSizeBytes())
+        let blocks = bytes.chunks(AES.blockSize)
         let out:[UInt8]?
         if (blockMode == .CFB) {
             // CFB uses encryptBlock to decrypt

+ 6 - 6
CryptoSwift/ChaCha20.swift

@@ -10,7 +10,7 @@ import Foundation
 
 public class ChaCha20 {
     
-    private let blockSizeBytes = 512 / 8
+    static let blockSize = 64 // 512 / 8
     private let stateSize = 16
     private var context:Context?
     
@@ -137,18 +137,18 @@ public class ChaCha20 {
                         ctx.input[13] = ctx.input[13] &+ 1
                         /* stopping at 2^70 bytes per nonce is user's responsibility */
                     }
-                    if (bytes <= blockSizeBytes) {
+                    if (bytes <= ChaCha20.blockSize) {
                         for (var i = 0; i < bytes; i++) {
                             c[i + cPos] = message[i + mPos] ^ output[i]
                         }
                         return c
                     }
-                    for (var i = 0; i < blockSizeBytes; i++) {
+                    for (var i = 0; i < ChaCha20.blockSize; i++) {
                         c[i + cPos] = message[i + mPos] ^ output[i]
                     }
-                    bytes -= blockSizeBytes
-                    cPos += blockSizeBytes
-                    mPos += blockSizeBytes
+                    bytes -= ChaCha20.blockSize
+                    cPos += ChaCha20.blockSize
+                    mPos += ChaCha20.blockSize
                 }
             }
         }