Browse Source

Implemented GiantUInt addition

Nathan Fallet 4 years ago
parent
commit
adf1545494

+ 12 - 0
CryptoSwift.xcodeproj/project.pbxproj

@@ -15,6 +15,9 @@
 		35F3E51C23BF9A6700A024A1 /* OCB.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35F3E51B23BF9A6700A024A1 /* OCB.swift */; };
 		42012783267A6F1C00F82506 /* ISO10126Padding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 42012782267A6F1C00F82506 /* ISO10126Padding.swift */; };
 		674A736F1BF5D85B00866C5B /* RabbitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 674A736E1BF5D85B00866C5B /* RabbitTests.swift */; };
+		6A072FF726CAB3F900F4E94F /* RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A072FF626CAB3F900F4E94F /* RSA.swift */; };
+		6ADC6EEF26CBAC0C00AFBF4E /* GiantUInt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */; };
+		6ADC6EF226CBAFC600AFBF4E /* GiantUIntTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */; };
 		750509991F6BEF2A00394A1B /* PKCS7.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750509981F6BEF2A00394A1B /* PKCS7.swift */; };
 		750CC3EB1DC0CACE0096BE6E /* BlowfishTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */; };
 		75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */; };
@@ -207,6 +210,9 @@
 		35F3E51D23BF9AD300A024A1 /* AESOCBTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AESOCBTests.swift; sourceTree = "<group>"; };
 		42012782267A6F1C00F82506 /* ISO10126Padding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ISO10126Padding.swift; sourceTree = "<group>"; };
 		674A736E1BF5D85B00866C5B /* RabbitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RabbitTests.swift; sourceTree = "<group>"; };
+		6A072FF626CAB3F900F4E94F /* RSA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSA.swift; sourceTree = "<group>"; };
+		6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUInt.swift; sourceTree = "<group>"; };
+		6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiantUIntTests.swift; sourceTree = "<group>"; };
 		750509981F6BEF2A00394A1B /* PKCS7.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PKCS7.swift; sourceTree = "<group>"; };
 		750CC3EA1DC0CACE0096BE6E /* BlowfishTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BlowfishTests.swift; sourceTree = "<group>"; };
 		75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Poly1305Tests.swift; sourceTree = "<group>"; };
@@ -435,6 +441,7 @@
 				E3FD2D511D6B813C00A9F35F /* Error+Extension.swift */,
 				754BE46719693E190098E6F3 /* DigestTests.swift */,
 				7576F6EB20726319006688F8 /* DigestTestsPerf.swift */,
+				6ADC6EF026CBAF9F00AFBF4E /* GiantUIntTests.swift */,
 				75100F8E19B0BC890005C5F5 /* Poly1305Tests.swift */,
 				758A94271A65C59200E46135 /* HMACTests.swift */,
 				0EE73E72204D599C00110E11 /* CMACTests.swift */,
@@ -555,12 +562,14 @@
 				75EC52741EE8B6CA0048EB3B /* UInt32+Extension.swift */,
 				75EC52751EE8B6CA0048EB3B /* UInt64+Extension.swift */,
 				75D7AF37208BFB1600D22BEB /* UInt128.swift */,
+				6ADC6EEE26CBAC0C00AFBF4E /* GiantUInt.swift */,
 				75EC52771EE8B6CA0048EB3B /* Updatable.swift */,
 				75EC52781EE8B6CA0048EB3B /* Utils.swift */,
 				75EC52791EE8B6CA0048EB3B /* ZeroPadding.swift */,
 				754310432050111A003FB1DF /* CompactMap.swift */,
 				75B3ED76210F9DF7005D4ADA /* BlockDecryptor.swift */,
 				75B3ED78210FA016005D4ADA /* BlockEncryptor.swift */,
+				6A072FF626CAB3F900F4E94F /* RSA.swift */,
 				753674062175D012003E32A6 /* StreamDecryptor.swift */,
 				756A64C52111083B00BE8805 /* StreamEncryptor.swift */,
 			);
@@ -864,6 +873,7 @@
 				75EC529B1EE8B8200048EB3B /* Rabbit+Foundation.swift in Sources */,
 				756A64C62111083B00BE8805 /* StreamEncryptor.swift in Sources */,
 				75EC52A61EE8B8390048EB3B /* PBKDF1.swift in Sources */,
+				6ADC6EEF26CBAC0C00AFBF4E /* GiantUInt.swift in Sources */,
 				75EC52B41EE8B83D0048EB3B /* UInt32+Extension.swift in Sources */,
 				75EC52911EE8B81A0048EB3B /* Cryptors.swift in Sources */,
 				75EC52881EE8B8170048EB3B /* ECB.swift in Sources */,
@@ -896,6 +906,7 @@
 				75EC52A81EE8B8390048EB3B /* PKCS5.swift in Sources */,
 				1467460F2017BB3600DF04ED /* AEAD.swift in Sources */,
 				35F3E51C23BF9A6700A024A1 /* OCB.swift in Sources */,
+				6A072FF726CAB3F900F4E94F /* RSA.swift in Sources */,
 				75EC528A1EE8B8170048EB3B /* PCBC.swift in Sources */,
 				75EC528D1EE8B81A0048EB3B /* ChaCha20.swift in Sources */,
 				75EC52851EE8B8170048EB3B /* CBC.swift in Sources */,
@@ -924,6 +935,7 @@
 				758A94291A65C67400E46135 /* HMACTests.swift in Sources */,
 				75100F8F19B0BC890005C5F5 /* Poly1305Tests.swift in Sources */,
 				E6200E171FB9B68C00258382 /* HKDFTests.swift in Sources */,
+				6ADC6EF226CBAFC600AFBF4E /* GiantUIntTests.swift in Sources */,
 				754BE46819693E190098E6F3 /* DigestTests.swift in Sources */,
 				E3FD2D531D6B81CE00A9F35F /* Error+Extension.swift in Sources */,
 				757DA2591A4ED4D7002BA3EF /* ChaCha20Tests.swift in Sources */,

+ 5 - 0
Sources/CryptoSwift/Array+Extension.swift

@@ -24,6 +24,11 @@ extension Array {
   var slice: ArraySlice<Element> {
     self[self.startIndex ..< self.endIndex]
   }
+  
+  @inlinable
+  subscript (safe index: Index) -> Element? {
+    return indices.contains(index) ? self[index] : nil
+  }
 }
 
 extension Array where Element == UInt8 {

+ 53 - 0
Sources/CryptoSwift/GiantUInt.swift

@@ -0,0 +1,53 @@
+//
+//  CryptoSwift
+//
+//  Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
+//  This software is provided 'as-is', without any express or implied warranty.
+//
+//  In no event will the authors be held liable for any damages arising from the use of this software.
+//
+//  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:
+//
+//  - 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.
+//  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
+//  - This notice may not be removed or altered from any source or binary distribution.
+//
+
+struct GiantUInt: Equatable {
+  
+  let bytes: Array<UInt8>
+  
+  init(_ raw: Array<UInt8>) {
+    self.bytes = raw
+  }
+    
+  // Equatable
+  
+  static func == (lhs: GiantUInt, rhs: GiantUInt) -> Bool {
+    lhs.bytes == rhs.bytes
+  }
+  
+  // Operations
+  
+  static func + (rhs: GiantUInt, lhs: GiantUInt) -> GiantUInt {
+    var newBytes = [UInt8]()
+    
+    var r: UInt8 = 0
+    
+    for i in 0 ..< max(rhs.bytes.count, lhs.bytes.count) {
+      let res1 = (rhs.bytes[safe: i] ?? 0).addingReportingOverflow(lhs.bytes[safe: i] ?? 0)
+      let res2 = res1.partialValue.addingReportingOverflow(r)
+      newBytes.append(res2.partialValue)
+      r = (res1.overflow ? 1 : 0) + (res2.overflow ? 1 : 0)
+    }
+    
+    if r != 0 {
+      newBytes.append(r)
+    }
+    
+    return GiantUInt(newBytes)
+  }
+  
+  
+  
+}

+ 64 - 0
Sources/CryptoSwift/RSA.swift

@@ -0,0 +1,64 @@
+//
+//  CryptoSwift
+//
+//  Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
+//  This software is provided 'as-is', without any express or implied warranty.
+//
+//  In no event will the authors be held liable for any damages arising from the use of this software.
+//
+//  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:
+//
+//  - 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.
+//  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
+//  - This notice may not be removed or altered from any source or binary distribution.
+//
+
+public final class RSA {
+  
+  public enum Error: Swift.Error {
+    /// Invalid key
+    case invalidKey
+  }
+  
+  let publicKey: Key?
+  let privateKey: Key?
+  
+  public var keySize: Int = 0
+  
+  public init(publicKey: Array<UInt8>?, privateKey: Array<UInt8>?) throws {
+    if let publicKey = publicKey {
+      self.publicKey = Key(bytes: publicKey)
+      self.keySize = self.publicKey!.count
+    } else {
+      self.publicKey = nil
+    }
+    if let privateKey = privateKey {
+      self.privateKey = Key(bytes: privateKey)
+      self.keySize = self.privateKey!.count
+    } else {
+      self.privateKey = nil
+    }
+    if keySize == 0 {
+      throw RSA.Error.invalidKey
+    }
+  }
+  
+}
+
+// MARK: Cipher
+
+extension RSA: Cipher {
+  
+  @inlinable
+  public func encrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
+    // TODO
+    return []
+  }
+
+  @inlinable
+  public func decrypt(_ bytes: ArraySlice<UInt8>) throws -> Array<UInt8> {
+    // TODO
+    return []
+  }
+  
+}

+ 43 - 0
Tests/CryptoSwiftTests/GiantUIntTests.swift

@@ -0,0 +1,43 @@
+//
+// CryptoSwift
+//
+//  Copyright (C) 2014-2021 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
+//  This software is provided 'as-is', without any express or implied warranty.
+//
+//  In no event will the authors be held liable for any damages arising from the use of this software.
+//
+//  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:
+//
+//  - 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.
+//  - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
+//  - This notice may not be removed or altered from any source or binary distribution.
+//
+
+import Foundation
+import XCTest
+@testable import CryptoSwift
+
+final class GiantUIntTests: XCTestCase {
+  
+  func testAddition() {
+    let a = GiantUInt([1]) + GiantUInt([1])
+    XCTAssertEqual(a, GiantUInt([2]), "simple addition failed")
+    
+    let b = GiantUInt([200]) + GiantUInt([200])
+    XCTAssertEqual(b, GiantUInt([144, 1]), "addition with retenue failed")
+    
+    let c = GiantUInt([200, 200]) + GiantUInt([200, 200])
+    XCTAssertEqual(c, GiantUInt([144, 145, 1]), "addition with double retenue failed")
+  }
+  
+}
+
+extension GiantUIntTests {
+  static func allTests() -> [(String, (GiantUIntTests) -> () -> Void)] {
+    let tests = [
+      ("testAddition", testAddition)
+    ]
+
+    return tests
+  }
+}