Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange SecKeyEncrypt behaviour
    primarykey
    data
    text
    <p>I'm trying to implement RSA encryption with PKCS1 padding using SecKeyEncrypt function.</p> <p>The code is following:</p> <pre><code>NSData *encryptText(NSString *text, SecKeyRef publicKey) { NSCParameterAssert(text.length &gt; 0); NSCParameterAssert(publicKey != NULL); NSData *dataToEncrypt = [text dataUsingEncoding:NSUTF8StringEncoding]; const uint8_t *bytesToEncrypt = dataToEncrypt.bytes; size_t cipherBufferSize = SecKeyGetBlockSize(publicKey); NSCAssert(cipherBufferSize &gt; 11, @"block size is too small: %zd", cipherBufferSize); const size_t inputBlockSize = cipherBufferSize - 11; // since we'll use PKCS1 padding uint8_t *cipherBuffer = (uint8_t *) malloc(sizeof(uint8_t) * cipherBufferSize); NSMutableData *accumulator = [[NSMutableData alloc] init]; @try { for (size_t block = 0; block * inputBlockSize &lt; dataToEncrypt.length; block++) { size_t blockOffset = block * inputBlockSize; const uint8_t *chunkToEncrypt = (bytesToEncrypt + block * inputBlockSize); const size_t remainingSize = dataToEncrypt.length - blockOffset; const size_t subsize = remainingSize &lt; inputBlockSize ? remainingSize : inputBlockSize; size_t actualOutputSize = cipherBufferSize; OSStatus status = SecKeyEncrypt(publicKey, kSecPaddingPKCS1, chunkToEncrypt, subsize, cipherBuffer, &amp;actualOutputSize); if (status != noErr) { NSLog(@"Cannot encrypt data, last SecKeyEncrypt status: %ld", status); return nil; } [accumulator appendBytes:cipherBuffer length:actualOutputSize]; } return [accumulator copy]; } @finally { free(cipherBuffer); } } </code></pre> <p>It works perfectly on iOS 6, but fails on iOS 5, SecKeyEncrypt returns <code>-50</code> (<code>errSecParam</code>). It would work on iOS 5 if I change 11 to 12 in <code>inputBlockSize = cipherBufferSize - 11</code>. Apple doc says that input chunk length should be less or equal <code>SecKeyGetBlockSize() - 11</code> if PKCS1 padding used. But on iOS 5 it definitely requires shorter input.</p> <p>My key block size is 64, so input chunk max length is 53, according to docs. On iOS 5 only 52 or less would work.</p> <p>What's wrong with this code? Or it's iOS 5 Security.framework bug?</p> <p><strong>UPD:</strong> problem reproduces only with 512-bit key. Tried with generated 1024-bit key, code works on iOS 5 with <code>11</code></p> <p>Related Apple doc: <a href="http://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/Reference/reference.html#//apple_ref/c/func/SecKeyEncrypt" rel="noreferrer">http://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/Reference/reference.html#//apple_ref/c/func/SecKeyEncrypt</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload