RNCryptor.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // RNCryptor.h
  3. //
  4. // Copyright (c) 2012 Rob Napier
  5. //
  6. // This code is licensed under the MIT License:
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a
  9. // copy of this software and associated documentation files (the "Software"),
  10. // to deal in the Software without restriction, including without limitation
  11. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. // and/or sell copies of the Software, and to permit persons to whom the
  13. // Software is furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. // DEALINGS IN THE SOFTWARE.
  25. //
  26. #import <Foundation/Foundation.h>
  27. #import <Security/Security.h>
  28. // NOTE: No CommonCrypto types may be used in this file. Swift can't handle them.
  29. extern NSString *const kRNCryptorErrorDomain;
  30. extern const uint8_t kRNCryptorFileVersion;
  31. typedef struct _RNCryptorKeyDerivationSettings
  32. {
  33. size_t keySize;
  34. size_t saltSize;
  35. /* CCPBKDFAlgorithm */ uint32_t PBKDFAlgorithm;
  36. /* CCPseudoRandomAlgorithm */ uint32_t PRF;
  37. uint rounds;
  38. BOOL hasV2Password; // See Issue #77. V2 incorrectly handled multi-byte characters.
  39. } RNCryptorKeyDerivationSettings;
  40. typedef struct _RNCryptorSettings
  41. {
  42. /* CCAlgorithm */ uint32_t algorithm;
  43. size_t blockSize;
  44. size_t IVSize;
  45. /* CCOptions */ uint32_t options;
  46. /* CCHmacAlgorithm */ uint32_t HMACAlgorithm;
  47. size_t HMACLength;
  48. RNCryptorKeyDerivationSettings keySettings;
  49. RNCryptorKeyDerivationSettings HMACKeySettings;
  50. } RNCryptorSettings;
  51. extern const RNCryptorSettings kRNCryptorAES256Settings;
  52. enum _RNCryptorOptions
  53. {
  54. kRNCryptorOptionHasPassword = 1 << 0,
  55. };
  56. typedef uint8_t RNCryptorOptions;
  57. enum
  58. {
  59. kRNCryptorHMACMismatch = 1,
  60. kRNCryptorUnknownHeader = 2,
  61. };
  62. @class RNCryptor;
  63. typedef void (^RNCryptorHandler)(RNCryptor *cryptor, NSData *data);
  64. ///** Encryptor/Decryptor for iOS
  65. //
  66. // Provides an easy-to-use, Objective-C interface to the AES functionality of CommonCrypto. Simplifies correct handling of
  67. // password stretching (PBKDF2), salting, and IV. For more information on these terms, see "Properly encrypting with AES
  68. // with CommonCrypto," and iOS 5 Programming Pushing the Limits, Chapter 11. Also includes automatic HMAC handling to integrity-check messages.
  69. //
  70. // RNCryptor is abstract. Use RNEncryptor to encrypt or RNDecryptor to decrypt
  71. // */
  72. //
  73. @interface RNCryptor : NSObject
  74. @property (nonatomic, readonly, strong) NSError *error;
  75. @property (nonatomic, readonly, getter=isFinished) BOOL finished;
  76. @property (nonatomic, readonly, copy) RNCryptorHandler handler;
  77. @property (nonatomic, readwrite) dispatch_queue_t responseQueue;
  78. - (void)addData:(NSData *)data;
  79. - (void)finish;
  80. /** Generate key given a password and salt using a PBKDF
  81. *
  82. * @param password Password to use for PBKDF
  83. * @param salt Salt for password
  84. * @param keySettings Settings for the derivation (RNCryptorKeyDerivationSettings)
  85. * @returns Key
  86. * @throws if settings are illegal
  87. */
  88. + (NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt settings:(RNCryptorKeyDerivationSettings)keySettings;
  89. /** Generate random data
  90. *
  91. * @param length Length of data to generate
  92. * @returns random data
  93. */
  94. + (NSData *)randomDataOfLength:(size_t)length;
  95. @end