ObjC.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // ObjC.m
  3. //
  4. // Copyright © 2015 Rob Napier. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a
  7. // copy of this software and associated documentation files (the "Software"),
  8. // to deal in the Software without restriction, including without limitation
  9. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. // and/or sell copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. // DEALINGS IN THE SOFTWARE.
  23. //
  24. #import <Foundation/Foundation.h>
  25. #import <XCTest/XCTest.h>
  26. @import RNCryptor;
  27. #import <CommonCrypto/CommonCryptor.h>
  28. @interface ObjC : XCTestCase
  29. @end
  30. @implementation ObjC
  31. NSData *randomDataOfLength(NSInteger length) {
  32. NSMutableData *data = [[NSMutableData alloc] initWithLength:length];
  33. int result = SecRandomCopyBytes(kSecRandomDefault, length, data.mutableBytes);
  34. NSCAssert(result == errSecSuccess, @"SecRandomCopyBytes must succeed");
  35. return data;
  36. }
  37. - (void)testOneShotKey {
  38. NSData *encryptionKey = randomDataOfLength(kCCKeySizeAES256);
  39. NSData *hmacKey = randomDataOfLength(kCCKeySizeAES256);
  40. NSData *data = randomDataOfLength(1024);
  41. NSData *ciphertext = [[[RNEncryptorV3 alloc] initWithEncryptionKey:encryptionKey hmacKey:hmacKey] encryptData:data];
  42. XCTAssertNotNil(ciphertext);
  43. NSError *error = nil;
  44. NSData *plaintext = [[[RNDecryptorV3 alloc] initWithEncryptionKey:encryptionKey hmacKey:hmacKey] decryptData:ciphertext error:&error];
  45. XCTAssertNil(error);
  46. XCTAssertEqualObjects(plaintext, data);
  47. }
  48. - (void)testOneShotPassword {
  49. NSString *password = @"PASSWORD";
  50. NSData *data = randomDataOfLength(1024);
  51. NSData *ciphertext = [[[RNEncryptor alloc] initWithPassword:password] encryptData:data];
  52. XCTAssertNotNil(ciphertext);
  53. NSError *error = nil;
  54. NSData *plaintext = [[[RNDecryptor alloc] initWithPassword:password] decryptData:ciphertext error:&error];
  55. XCTAssertNil(error);
  56. XCTAssertEqualObjects(plaintext, data);
  57. }
  58. - (void)testOneShotPasswordV3 {
  59. NSString *password = @"PASSWORD";
  60. NSData *data = randomDataOfLength(1024);
  61. NSData *ciphertext = [[[RNEncryptorV3 alloc] initWithPassword:password] encryptData:data];
  62. XCTAssertNotNil(ciphertext);
  63. NSError *error = nil;
  64. NSData *plaintext = [[[RNDecryptorV3 alloc] initWithPassword:password] decryptData:ciphertext error:&error];
  65. XCTAssertNil(error);
  66. XCTAssertEqualObjects(plaintext, data);
  67. }
  68. - (void)testUpdatesPassword {
  69. NSString *password = @"PASSWORD";
  70. NSData *data = randomDataOfLength(1024);
  71. RNEncryptor *cryptor = [[RNEncryptor alloc] initWithPassword:password];
  72. XCTAssertNotNil(cryptor);
  73. NSMutableData *ciphertext = [NSMutableData new];
  74. [ciphertext appendData:[cryptor updateWithData:data]];
  75. [ciphertext appendData:[cryptor finalData]];
  76. XCTAssertGreaterThan(ciphertext.length, data.length);
  77. NSError *error = nil;
  78. RNDecryptor *decryptor = [[RNDecryptor alloc] initWithPassword:password];
  79. XCTAssertNotNil(decryptor);
  80. NSMutableData *plaintext = [NSMutableData new];
  81. [plaintext appendData:[decryptor updateWithData:ciphertext error:&error]];
  82. XCTAssertNil(error);
  83. [plaintext appendData:[decryptor finalDataAndReturnError:&error]];
  84. XCTAssertNil(error);
  85. XCTAssertEqualObjects(plaintext, data);
  86. }
  87. - (void)testBadFormat {
  88. NSData *data = [[NSMutableData alloc] initWithLength:1024];
  89. NSString *password = @"PASSWORD";
  90. NSError *error = nil;
  91. NSData *plaintext = [RNCryptor decryptData:data password:password error:&error];
  92. XCTAssertNil(plaintext);
  93. XCTAssertEqual(error.code, RNCryptorErrorUnknownHeader);
  94. }
  95. - (void)testClass {
  96. NSString *password = @"PASSWORD";
  97. NSData *data = randomDataOfLength(1024);
  98. NSData *ciphertext = [RNCryptor encryptData:data password:password];
  99. XCTAssertNotNil(ciphertext);
  100. NSError *error = nil;
  101. NSData *plaintext = [RNCryptor decryptData:ciphertext password:password error:&error];
  102. XCTAssertNil(error);
  103. XCTAssertEqualObjects(plaintext, data);
  104. }
  105. @end