Vedroid36
1/18/2019 - 3:17 PM

1 - deriveDecryptionKey.m

(void)deriveDecryptionKey: (NSString *)salt password:(NSString *)password volumeName:(NSString *)volumeName {
    
    /**
     * This was an attempt to use RNCryptor to derive the key.
     */
    /*
    NSData *saltdata = [salt dataUsingEncoding:NSUTF8StringEncoding];
    NSData *mykey2 = [RNDecryptor keyForPassword:password salt:saltdata settings:kRNCryptorAES256Settings.keySettings];
    */
    
    // Step 1
    NSData *binsalt = [self dataFromHexString:salt];
    
    // Step 2
    NSString *base64Encoded = [binsalt base64EncodedStringWithOptions:0];
    
    NSString *base64_alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    NSString *blfish_alphabet = @"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
    NSString *bfsalt = [base64Encoded substringToIndex:22];
    
    for(int i = 0; i < [base64_alphabet length]; ++i) {
        NSString *from = [base64_alphabet substringWithRange:NSMakeRange(i, 1)];
        NSString *to = [blfish_alphabet substringWithRange:NSMakeRange(i, 1)];
        bfsalt = [bfsalt stringByReplacingOccurrencesOfString:from withString:to];
    }
    
    // Step 3 + 4
    NSString *bfhash = [self _bCryptPassword:password withSaltData:binsalt];
    
    // Step 5
    NSString *encrkey = [self createSHA512: bfhash];
    uint8_t key[64];
    CCKeyDerivationPBKDF(kCCPBKDF2, [bfhash dataUsingEncoding:NSUTF8StringEncoding].bytes, [bfhash dataUsingEncoding:NSUTF8StringEncoding].length, binsalt.bytes, binsalt.length, kCCPRFHmacAlgSHA512, 1, key, 64);
    
    [SKYSyncEncryptionHelper setDecryptionKeyForVolume:volumeName decryptionKey:encrkey];

    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // Step 6
}