Skip to content

Instantly share code, notes, and snippets.

@0xc010d
Created August 13, 2013 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xc010d/6221405 to your computer and use it in GitHub Desktop.
Save 0xc010d/6221405 to your computer and use it in GitHub Desktop.
static __attribute__((always_inline)) CCCryptorStatus AES128Run(CCOperation operation, NSData *inData, NSData *key, NSData *__autoreleasing *outData) {
CCCryptorStatus status = kCCParamError;
if (outData != NULL) {
CCCryptorRef cryptor = NULL;
//correct key length
NSMutableData *correctedKey = [key mutableCopy];
if ([key length] <= kCCKeySizeAES128) {
[correctedKey setLength:kCCKeySizeAES128];
}
else if ([key length] <= kCCKeySizeAES192) {
[correctedKey setLength:kCCKeySizeAES192];
}
else {
[correctedKey setLength:kCCKeySizeAES256];
}
#if !__has_feature(objc_arc)
[correctedKey autorelease];
#endif
status = CCCryptorCreate(operation, kCCAlgorithmAES128, kCCOptionPKCS7Padding, [correctedKey bytes], [correctedKey length], NULL, &cryptor);
if (status == kCCSuccess) {
size_t length = CCCryptorGetOutputLength(cryptor, [inData length], true);
NSMutableData *result = [NSMutableData dataWithLength:length];
size_t updateLength;
status = CCCryptorUpdate(cryptor, [inData bytes], [inData length], [result mutableBytes], [result length], &updateLength);
if (status == kCCSuccess) {
char *finalDataPointer = (char *)[result mutableBytes] + updateLength;
size_t remainingLength = [result length] - updateLength;
size_t finalLength;
status = CCCryptorFinal(cryptor, finalDataPointer, remainingLength, &finalLength);
[result setLength:updateLength + finalLength];
}
*outData = status == kCCSuccess ? [NSData dataWithData:result] : nil;
}
CCCryptorRelease(cryptor);
}
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment