RNCryptorEngine.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // RNCryptorEngine
  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 "RNCryptorEngine.h"
  27. @interface RNCryptorEngine ()
  28. @property (nonatomic, readonly) CCCryptorRef cryptor;
  29. @property (nonatomic, readonly) NSMutableData *buffer;
  30. @end
  31. @implementation RNCryptorEngine
  32. @synthesize cryptor = __cryptor;
  33. @synthesize buffer = __buffer;
  34. - (RNCryptorEngine *)initWithOperation:(CCOperation)operation settings:(RNCryptorSettings)settings key:(NSData *)key IV:(NSData *)IV error:(NSError **)error
  35. {
  36. self = [super init];
  37. if (self) {
  38. CCCryptorStatus
  39. cryptorStatus = CCCryptorCreate(operation,
  40. settings.algorithm,
  41. settings.options,
  42. key.bytes,
  43. key.length,
  44. IV.bytes,
  45. &__cryptor);
  46. if (cryptorStatus != kCCSuccess || __cryptor == NULL) {
  47. if (error) {
  48. *error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
  49. }
  50. self = nil;
  51. return nil;
  52. }
  53. __buffer = [NSMutableData data];
  54. }
  55. return self;
  56. }
  57. - (void)dealloc
  58. {
  59. if (__cryptor) {
  60. CCCryptorRelease(__cryptor);
  61. }
  62. }
  63. - (NSData *)addData:(NSData *)data error:(NSError **)error
  64. {
  65. NSMutableData *buffer = self.buffer;
  66. [buffer setLength:CCCryptorGetOutputLength(self.cryptor, [data length], true)]; // We'll reuse the buffer in -finish
  67. size_t dataOutMoved;
  68. CCCryptorStatus
  69. cryptorStatus = CCCryptorUpdate(self.cryptor, // cryptor
  70. data.bytes, // dataIn
  71. data.length, // dataInLength (verified > 0 above)
  72. buffer.mutableBytes, // dataOut
  73. buffer.length, // dataOutAvailable
  74. &dataOutMoved); // dataOutMoved
  75. if (cryptorStatus != kCCSuccess) {
  76. if (error) {
  77. *error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
  78. }
  79. return nil;
  80. }
  81. return [buffer subdataWithRange:NSMakeRange(0, dataOutMoved)];
  82. }
  83. - (NSData *)finishWithError:(NSError **)error
  84. {
  85. NSMutableData *buffer = self.buffer;
  86. size_t dataOutMoved;
  87. CCCryptorStatus
  88. cryptorStatus = CCCryptorFinal(self.cryptor, // cryptor
  89. buffer.mutableBytes, // dataOut
  90. buffer.length, // dataOutAvailable
  91. &dataOutMoved); // dataOutMoved
  92. if (cryptorStatus != kCCSuccess) {
  93. if (error) {
  94. *error = [NSError errorWithDomain:kRNCryptorErrorDomain code:cryptorStatus userInfo:nil];
  95. }
  96. return nil;
  97. }
  98. return [buffer subdataWithRange:NSMakeRange(0, dataOutMoved)];
  99. }
  100. @end