0
This is the code I am using for the encryption but it generate an error 

"CCKeyDerivationPBKDF is unavailable" in AESKeyForPassword method though it is declare before implementation. How to Resolve it.

     #ifndef _CC_PBKDF_H_
#define _CC_PBKDF_H_

#include <sys/types.h>
#include <sys/param.h>

#include <string.h>
#include <limits.h>
#include <stdlib.h>

#include <Availability.h>

#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>


#ifdef __cplusplus
extern "C" {
#endif

    enum {
        kCCPBKDF2 = 2,
    };


    typedef uint32_t CCPBKDFAlgorithm;


    enum {
        kCCPRFHmacAlgSHA1 = 1, 
        kCCPRFHmacAlgSHA224 = 2,
        kCCPRFHmacAlgSHA256 = 3,
        kCCPRFHmacAlgSHA384 = 4,
        kCCPRFHmacAlgSHA512 = 5,
    };


    typedef uint32_t CCPseudoRandomAlgorithm;

    /*

     @function  CCKeyDerivationPBKDF
     @abstract  Derive a key from a text password/passphrase

     @param algorithm       Currently only PBKDF2 is available via kCCPBKDF2
     @param password        The text password used as input to the derivation 
     function.  The actual octets present in this string 
     will be used with no additional processing.  It's 
     extremely important that the same encoding and 
     normalization be used each time this routine is 
     called if the same key is  expected to be derived.
     @param passwordLen     The length of the text password in bytes.
     @param salt            The salt byte values used as input to the derivation 
     function.
     @param saltLen         The length of the salt in bytes.
     @param prf             The Pseudo Random Algorithm to use for the derivation 
     iterations.
     @param rounds          The number of rounds of the Pseudo Random Algorithm 
     to use.
     @param derivedKey      The resulting derived key produced by the function.  
     The space for this must be provided by the caller.
     @param derivedKeyLen   The expected length of the derived key in bytes.

     @discussion The following values are used to designate the PRF:

     * kCCPRFHmacAlgSHA1
     * kCCPRFHmacAlgSHA224
     * kCCPRFHmacAlgSHA256
     * kCCPRFHmacAlgSHA384
     * kCCPRFHmacAlgSHA512

     @result     kCCParamError can result from bad values for the password, salt, 
     and unwrapped key pointers as well as a bad value for the prf function.

     */

    int CCKeyDerivationPBKDF( CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
                             const uint8_t *salt, size_t saltLen,
                             CCPseudoRandomAlgorithm prf, uint rounds, 
                             uint8_t *derivedKey, size_t derivedKeyLen)
    __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

    /*
     * All lengths are in bytes - not bits.
     */

    /*

     @function  CCCalibratePBKDF
     @abstract  Determine the number of PRF rounds to use for a specific delay on 
     the current platform.  
     @param algorithm       Currently only PBKDF2 is available via kCCPBKDF2
     @param passwordLen     The length of the text password in bytes.
     @param saltLen         The length of the salt in bytes.
     @param prf             The Pseudo Random Algorithm to use for the derivation 
     iterations.
     @param derivedKeyLen   The expected length of the derived key in bytes.
     @param msec            The targetted duration we want to achieve for a key 
     derivation with these parameters.

     @result the number of iterations to use for the desired processing time.

     */

    uint CCCalibratePBKDF(CCPBKDFAlgorithm algorithm, size_t passwordLen, size_t saltLen,
                          CCPseudoRandomAlgorithm prf, size_t derivedKeyLen, uint32_t msec)
    __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

#ifdef __cplusplus
}
#endif

#endif  /* _CC_PBKDF_H_ */







#import "AESEncryption.h"
#import <CommonCrypto/CommonCryptor.h>
//#import <CommonCrypto/CommonKeyDerivation.h>
//#import <CommonKeyDerivation.h>

@implementation AESEncryption




NSString * const
kRNCryptManagerErrorDomain = @"net.robnapier.RNCryptManager";

const CCAlgorithm kAlgorithm = kCCAlgorithmAES128;
const NSUInteger kAlgorithmKeySize = kCCKeySizeAES128;
const NSUInteger kAlgorithmBlockSize = kCCBlockSizeAES128;
const NSUInteger kAlgorithmIVSize = kCCBlockSizeAES128;
const NSUInteger kPBKDFSaltSize = 8;
const NSUInteger kPBKDFRounds = 1000;//0;  // ~80ms on an iPhone 4

// ===================

+ (NSData *)encryptedDataForData:(NSData *)data
                        password:(NSString *)password
                              iv:(NSData **)iv
                            salt:(NSData **)salt
                           error:(NSError **)error {
    NSAssert(iv, @"IV must not be NULL");
    NSAssert(salt, @"salt must not be NULL");

    *iv = [self randomDataOfLength:kAlgorithmIVSize];
    *salt = [self randomDataOfLength:kPBKDFSaltSize];

    NSData *key = [self AESKeyForPassword:password salt:*salt];

    size_t outLength;
    NSMutableData *
    cipherData = [NSMutableData dataWithLength:data.length +
                  kAlgorithmBlockSize];

    CCCryptorStatus
    result = CCCrypt(kCCEncrypt, // operation
                     kAlgorithm, // Algorithm
                     kCCOptionPKCS7Padding, // options
                     key.bytes, // key
                     key.length, // keylength
                     (*iv).bytes,// iv
                     data.bytes, // dataIn
                     data.length, // dataInLength,
                     cipherData.mutableBytes, // dataOut
                     cipherData.length, // dataOutAvailable
                     &outLength); // dataOutMoved

    if (result == kCCSuccess) {
        cipherData.length = outLength;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:kRNCryptManagerErrorDomain
                                         code:result
                                     userInfo:nil];
        }
        return nil;
    }

    return cipherData;
}

// ===================

+ (NSData *)randomDataOfLength:(size_t)length {
    NSMutableData *data = [NSMutableData dataWithLength:length];

    int result = SecRandomCopyBytes(kSecRandomDefault, length,data.mutableBytes);
    NSLog(@"%d",result);
    NSAssert1(result == 0, @"Unable to generate random bytes: %d", errno);
    //NSAssert( @"Unable to generate random bytes: %d", errno);
    return data;
}




// ===================

// Replace this with a 10,000 hash calls if you don't have CCKeyDerivationPBKDF
+ (NSData *)AESKeyForPassword:(NSString *)password 
                         salt:(NSData *)salt {
    NSMutableData *
    derivedKey = [NSMutableData dataWithLength:kAlgorithmKeySize];

    int result = CCKeyDerivationPBKDF(kCCPBKDF2,            // algorithm
                                  password.UTF8String,  // password
                                  password.length,  // passwordLength
                                  salt.bytes,           // salt
                                  salt.length,          // saltLen
                                  kCCPRFHmacAlgSHA1,    // PRF
                                  kPBKDFRounds,         // rounds
                                  derivedKey.mutableBytes, // derivedKey
                                  derivedKey.length); // derivedKeyLen
    NSLog(@"%d",result);
    // Do not log password here
    NSAssert1(result == kCCSuccess,@"Unable to create AES key for password: %d", result);
    //NSAssert(@"Unable to create AES key for password: %d", result);
    return derivedKey;
}
@end

The code placed above implementation is of CommonCrypto/CommonKeyDerivation.h which was not found be me xcode and hence I put code directly at the top.

Soniya
  • 600
  • 7
  • 34
  • Including the content of the `.h` header file is not a surrogate for having the library actually available. Also, are you sure you can simply mix C++ and Objective-C in the same source code file? – Paŭlo Ebermann Oct 22 '11 at 13:37
  • I am new in iphone and suddenly I need to use encryption in my project.Same project is made in android and .Net and they use same approach so I have to use this anyhow.Xcode unable to recognize CommonCrypto/CommonKeyDerivation.h so I directly paste its contents from apple opensource. And still it give error of "CCKeyDerivationPBKDF is unavailable" in AESKeyForPassword method – Soniya Oct 24 '11 at 05:26

2 Answers2

1

Try to comment out this lines:

__OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_NA);

I think they limit the method to a particular Operating System and this is exactly what you don't need. But I cannot guarantee if further issues may appear. I'm trying to achieve the same.

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
0

You have merely declared 2 prototypes for CCKeyDerivationPBKDF and CCCalibratePBKDF. Either put the full code for the functions at this place or declare them as extern and have them in a seperate module or library.

ott--
  • 5,642
  • 4
  • 24
  • 27
  • It's really getting frustrating to me. I hv to implement it anyhow.Initially CommonCrypto/CommonKeyDerivation.h ws there bt it ws showing error of not available i paste code directly still show error I mentioned in my ques. now i add CommonCrypto/CommonKeyDerivation.c file still not able to rectify it. I am using http://robnapier.net/blog/aes-commoncrypto-564 and have Xcode 3.2.6 and not able to use CommonCrypto from apple open source – Soniya Oct 24 '11 at 10:02