|
| 1 | +// |
| 2 | +// CryptorTests.swift |
| 3 | +// CryptomatorCryptoLibTests |
| 4 | +// |
| 5 | +// Created by Sebastian Stenzel on 27.04.20. |
| 6 | +// Copyright © 2020 Skymatic GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import XCTest |
| 10 | +@testable import CryptomatorCryptoLib |
| 11 | + |
| 12 | +class GcmCryptorTests: CryptorTests { |
| 13 | + override class var defaultTestSuite: XCTestSuite { |
| 14 | + return XCTestSuite(forTestCaseClass: GcmCryptorTests.self) |
| 15 | + } |
| 16 | + |
| 17 | + override func setUpWithError() throws { |
| 18 | + let aesKey = [UInt8](repeating: 0x55, count: 32) |
| 19 | + let macKey = [UInt8](repeating: 0x77, count: 32) |
| 20 | + let masterkey = Masterkey.createFromRaw(aesMasterKey: aesKey, macMasterKey: macKey) |
| 21 | + let cryptoSupport = CryptoSupportMock() |
| 22 | + let contentCryptor = GcmContentCryptor() |
| 23 | + |
| 24 | + try super.setUpWithError(masterkey: masterkey, cryptoSupport: cryptoSupport, contentCryptor: contentCryptor) |
| 25 | + } |
| 26 | + |
| 27 | + func testCreateHeader() throws { |
| 28 | + let header = try cryptor.createHeader() |
| 29 | + XCTAssertEqual([UInt8](repeating: 0xF0, count: 12), header.nonce) |
| 30 | + XCTAssertEqual([UInt8](repeating: 0xF0, count: 32), header.contentKey) |
| 31 | + } |
| 32 | + |
| 33 | + func testEncryptHeader() throws { |
| 34 | + let header = try cryptor.createHeader() |
| 35 | + let encrypted = try cryptor.encryptHeader(header) |
| 36 | + |
| 37 | + // echo -n "///////////w8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8A==" | base64 --decode \ |
| 38 | + // | openssl enc -aes-256-gcm -K 5555555555555555555555555555555555555555555555555555555555555555 -iv F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0 -a |
| 39 | + let expected: [UInt8] = [ |
| 40 | + // nonce |
| 41 | + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, |
| 42 | + 0xF0, 0xF0, 0xF0, 0xF0, |
| 43 | + // ciphertext |
| 44 | + 0x1C, 0x87, 0x19, 0xF0, 0x31, 0x22, 0x86, 0x8F, |
| 45 | + 0xDB, 0x9D, 0x97, 0x03, 0xA0, 0x86, 0x08, 0xD5, |
| 46 | + 0x88, 0x58, 0x96, 0xC2, 0xE6, 0x60, 0x4B, 0xB9, |
| 47 | + 0xEA, 0x64, 0x31, 0xD4, 0xA0, 0x5D, 0x47, 0x6F, |
| 48 | + 0xE4, 0x1F, 0x32, 0x31, 0xF2, 0xC0, 0x61, 0x1F, |
| 49 | + // tag |
| 50 | + 0x6D, 0x42, 0x98, 0x82, 0x43, 0xF2, 0x1F, 0x43, |
| 51 | + 0xF6, 0x44, 0xFD, 0x6D, 0xF7, 0xA9, 0x3F, 0x0B |
| 52 | + ] |
| 53 | + XCTAssertEqual(expected, encrypted) |
| 54 | + } |
| 55 | + |
| 56 | + func testDecryptHeader() throws { |
| 57 | + let ciphertext: [UInt8] = [ |
| 58 | + // nonce |
| 59 | + 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, |
| 60 | + 0xF0, 0xF0, 0xF0, 0xF0, |
| 61 | + // ciphertext |
| 62 | + 0x1C, 0x87, 0x19, 0xF0, 0x31, 0x22, 0x86, 0x8F, |
| 63 | + 0xDB, 0x9D, 0x97, 0x03, 0xA0, 0x86, 0x08, 0xD5, |
| 64 | + 0x88, 0x58, 0x96, 0xC2, 0xE6, 0x60, 0x4B, 0xB9, |
| 65 | + 0xEA, 0x64, 0x31, 0xD4, 0xA0, 0x5D, 0x47, 0x6F, |
| 66 | + 0xE4, 0x1F, 0x32, 0x31, 0xF2, 0xC0, 0x61, 0x1F, |
| 67 | + // tag |
| 68 | + 0x6D, 0x42, 0x98, 0x82, 0x43, 0xF2, 0x1F, 0x43, |
| 69 | + 0xF6, 0x44, 0xFD, 0x6D, 0xF7, 0xA9, 0x3F, 0x0B |
| 70 | + ] |
| 71 | + let decrypted = try cryptor.decryptHeader(ciphertext) |
| 72 | + XCTAssertEqual([UInt8](repeating: 0xF0, count: 12), decrypted.nonce) |
| 73 | + XCTAssertEqual([UInt8](repeating: 0xF0, count: 32), decrypted.contentKey) |
| 74 | + } |
| 75 | +} |
0 commit comments