Page MenuHomePhabricator

D4384.id13908.diff
No OneTemporary

D4384.id13908.diff

diff --git a/native/ios/Comm/TemporalMessageStorage/EncryptedFileUtils.mm b/native/ios/Comm/TemporalMessageStorage/EncryptedFileUtils.mm
--- a/native/ios/Comm/TemporalMessageStorage/EncryptedFileUtils.mm
+++ b/native/ios/Comm/TemporalMessageStorage/EncryptedFileUtils.mm
@@ -10,3 +10,65 @@
+ (NSData *)_encryptData:(NSString *)data error:(NSError **)error;
+ (NSString *)_decryptBinary:(NSData *)binary error:(NSError **)error;
@end
+
+@implementation EncryptedFileUtils
++ (NSData *)_encryptData:(NSString *)data error:(NSError **)error {
+ return [EncryptedFileUtils
+ _runCryptor:[data dataUsingEncoding:NSUTF8StringEncoding]
+ operation:kCCEncrypt
+ error:error];
+}
+
++ (NSString *)_decryptBinary:(NSData *)binary error:(NSError **)error {
+ return
+ [[NSString alloc] initWithData:[EncryptedFileUtils _runCryptor:binary
+ operation:kCCDecrypt
+ error:error]
+ encoding:NSUTF8StringEncoding];
+}
+
++ (NSData *)_runCryptor:(NSData *)binary
+ operation:(CCOperation)operation
+ error:(NSError **)err {
+ NSData *key = [[[CommSecureStoreIOSWrapper sharedInstance]
+ get:@"comm.encryptionKey"] dataUsingEncoding:NSUTF8StringEncoding];
+ if (!key) {
+ *err = [NSError
+ errorWithDomain:@"app.comm"
+ code:NSCoderValueNotFoundError
+ userInfo:@{
+ NSLocalizedDescriptionKey : @"Encryption key not created yet"
+ }];
+ return nil;
+ }
+ NSMutableData *resultBinary =
+ [NSMutableData dataWithLength:binary.length + kCCBlockSizeBlowfish];
+
+ size_t processedBytes = 0;
+ CCCryptorStatus ccStatus = CCCrypt(
+ operation,
+ kCCAlgorithmRC4,
+ kCCOptionPKCS7Padding,
+ key.bytes,
+ key.length,
+ nil,
+ binary.bytes,
+ binary.length,
+ resultBinary.mutableBytes,
+ resultBinary.length,
+ &processedBytes);
+
+ resultBinary.length = processedBytes;
+ if (ccStatus != kCCSuccess) {
+ *err = [NSError
+ errorWithDomain:@"app.comm"
+ code:ccStatus
+ userInfo:@{
+ NSLocalizedDescriptionKey : @"Cryptographic operation failed"
+ }];
+ return nil;
+ }
+ return resultBinary;
+}
+
+@end
\ No newline at end of file

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 6, 2:38 AM (22 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2427771
Default Alt Text
D4384.id13908.diff (2 KB)

Event Timeline