Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is based upon the <strong>SecKeyWrapper</strong> class from Apple's <a href="http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Introduction/Intro.html" rel="nofollow"><strong>'CryptoExercise' example</strong></a>. I just ran into the same error message when using the doCipher method.</p> <p>'<strong>-4301</strong>' is in this case the value of the ccStatus variable after the call of <strong>CCCryptoFinal</strong>, which returns a value of type <strong>CCryptorStatus</strong>. It is defined in CommonCryptor.h as follows:</p> <pre><code>enum { kCCSuccess = 0, kCCParamError = -4300, kCCBufferTooSmall = -4301, kCCMemoryFailure = -4302, kCCAlignmentError = -4303, kCCDecodeError = -4304, kCCUnimplemented = -4305 }; </code></pre> <p>So somehow the buffer created in the doCipher method is too small, which is odd, because it's size is determined via <strong>CCCryptorGetOutputLength</strong>, just as the documentation of <strong>CCCryptorFinal</strong> suggests:</p> <pre><code>@result kCCBufferTooSmall indicates insufficent space in the dataOut buffer. The caller can use CCCryptorGetOutputLength() to determine the required output buffer size in this case. The operation can be retried; no state is lost when this is returned. </code></pre> <p>I've got the hunch that the SecKeyWrapper class from the Apple <strong>example</strong> is not bug free. I'll see if I can solve the problem or find another way to do AES encryption on iPhone. There's an explanation and example code in Rob Napier's and Mugunth Kumar's book <a href="http://iosptl.com/" rel="nofollow"><strong>'iOS 5 programming - Pushing the limits'</strong></a> that I'll try out. Also, the authors recommend two WWDC Sessions, available at <a href="http://developer.apple.com" rel="nofollow">developer.apple.com</a>.</p> <ul> <li>WWDC 2010, "Session 204: Creating Secure Applications"</li> <li>WWDC 2011, "Session 208: Securing iOS Applications"</li> </ul> <p><strong>EDIT:</strong></p> <p>I just found the error. It's within the <strong>doCipher</strong> method. Simply substitute the following lines:</p> <pre><code>// We don't want to toss padding on if we don't need to if(encryptOrDecrypt == kCCEncrypt) { if(*pkcs7 != kCCOptionECBMode) { if((plainTextBufferSize % kCCBlockSizeAES128) == 0) { *pkcs7 = 0x0000; } else { *pkcs7 = kCCOptionPKCS7Padding; } } } else if(encryptOrDecrypt != kCCDecrypt) { LOGGING_FACILITY1( 0, @"Invalid CCOperation parameter [%d] for cipher context.", *pkcs7 ); } </code></pre> <p>with the following lines:</p> <pre><code>// check for valid context parameter if (encryptOrDecrypt != kCCEncrypt &amp;&amp; encryptOrDecrypt != kCCDecrypt) { LOGGING_FACILITY1( 0, @"Invalid CCOperation parameter [%d] for cipher context.", encryptOrDecrypt ); } </code></pre> <p>As Ortwin pointed out below, you furthermore have to replace the third parameter in the subsequent CCCryptorCreate call to be always kCCOptionPKCS7Padding:</p> <pre><code>// Create and Initialize the crypto reference. ccStatus = CCCryptorCreate( encryptOrDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, (const void *)[theSymmetricKey bytes], kCCKeySizeAES128, (const void *)iv, &amp;thisEncipher ); </code></pre>
    singulars
    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.
    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