diff options
Diffstat (limited to 'src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp')
-rw-r--r-- | src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp | 78 |
1 files changed, 35 insertions, 43 deletions
diff --git a/src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp b/src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp index 9ab357126..3cc37f5ce 100644 --- a/src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp +++ b/src/filters/parser/MP4Splitter/AP4/Source/Crypto/Ap4KeyWrap.cpp @@ -28,7 +28,7 @@ /* Portions of this code are based on the code of LibTomCrypt - that was released into public domain by Tom St Denis. + that was released into public domain by Tom St Denis. */ /*---------------------------------------------------------------------- @@ -46,59 +46,56 @@ | AP4_AesKeyWrap +---------------------------------------------------------------------*/ AP4_Result -AP4_AesKeyWrap(const AP4_UI08* kek, - const AP4_UI08* cleartext_key, +AP4_AesKeyWrap(const AP4_UI08* kek, + const AP4_UI08* cleartext_key, AP4_Size cleartext_key_size, AP4_DataBuffer& wrapped_key) { // check parameters - if(cleartext_key_size % 8) - { + if (cleartext_key_size % 8) { // not a multiple of 64 bits return AP4_ERROR_INVALID_PARAMETERS; } - + // the output size is (n+1)*64 bits // where n is the number of 64-bit blocks // of the cleartext key - unsigned int n = cleartext_key_size / 8; - wrapped_key.SetDataSize((n + 1) * 8); - + unsigned int n = cleartext_key_size/8; + wrapped_key.SetDataSize((n+1)*8); + // Step 1. Initialize variables. // Set A = IV, an initial value (0xA6) // For i = 1 to n // R[i] = P[i] AP4_UI08* a = (AP4_UI08*)wrapped_key.UseData(); AP4_SetMemory(a, 0xA6, 8); - AP4_UI08* r = a + 8; + AP4_UI08* r = a+8; AP4_CopyMemory(r, cleartext_key, cleartext_key_size); - + // Step 2. Calculate intermediate values. // For j = 0 to 5 // For i=1 to n // B = AES(K, A | R[i]) // A = MSB(64, B) ^ t where t = (n*j)+i - // R[i] = LSB(64, B) + // R[i] = LSB(64, B) AP4_AesBlockCipher block_cipher(kek, AP4_BlockCipher::ENCRYPT); - for(unsigned int j = 0; j <= 5; j++) - { - r = a + 8; - for(unsigned int i = 1; i <= n; i++) - { + for (unsigned int j=0; j <= 5; j++) { + r = a + 8; + for (unsigned int i=1; i<=n; i++) { AP4_UI08 workspace[16]; AP4_UI08 b[16]; AP4_CopyMemory(workspace, a, 8); AP4_CopyMemory(&workspace[8], r, 8); block_cipher.ProcessBlock(workspace, b); AP4_CopyMemory(a, b, 8); - a[7] ^= n * j + i; + a[7] ^= n*j+i; AP4_CopyMemory(r, &b[8], 8); r += 8; - } - } - + } + } + // Step 3. Output the results. - // (Nothing to do here since we've worked in-place + // (Nothing to do here since we've worked in-place return AP4_SUCCESS; } @@ -112,25 +109,24 @@ AP4_AesKeyUnwrap(const AP4_UI08* kek, AP4_DataBuffer& cleartext_key) { // check parameters - if((wrapped_key_size % 8) || (wrapped_key_size < 24)) - { + if ((wrapped_key_size % 8) || (wrapped_key_size < 24)) { // not a multiple of 64 bits or too small return AP4_ERROR_INVALID_PARAMETERS; } - + // setup the output buffer - unsigned int n = (wrapped_key_size / 8) - 1; - cleartext_key.SetDataSize(n * 8); - + unsigned int n = (wrapped_key_size/8)-1; + cleartext_key.SetDataSize(n*8); + // Step 1. Initialize variables. // Set A = C[0] // For i = 1 to n // R[i] = C[i] - AP4_UI08 a[8]; + AP4_UI08 a[8]; AP4_CopyMemory(a, wrapped_key, 8); AP4_UI08* r = (AP4_UI08*)cleartext_key.UseData(); - AP4_CopyMemory(r, wrapped_key + 8, 8 * n); - + AP4_CopyMemory(r, wrapped_key+8, 8*n); + // Step 2. Compute intermediate values. // For j = 5 to 0 // For i = n to 1 @@ -138,15 +134,13 @@ AP4_AesKeyUnwrap(const AP4_UI08* kek, // A = MSB(64, B) // R[i] = LSB(64, B) AP4_AesBlockCipher block_cipher(kek, AP4_BlockCipher::DECRYPT); - for(int j = 5; j >= 0; j--) - { - r = (AP4_UI08*)cleartext_key.UseData() + (n - 1) * 8; - for(int i = n; i >= 1; i--) - { + for (int j=5; j>=0; j--) { + r = (AP4_UI08*)cleartext_key.UseData()+(n-1)*8; + for (int i=n; i>=1; i--) { AP4_UI08 workspace[16]; AP4_UI08 b[16]; AP4_CopyMemory(workspace, a, 8); - workspace[7] ^= (n * j) + i; + workspace[7] ^= (n*j)+i; AP4_CopyMemory(&workspace[8], r, 8); block_cipher.ProcessBlock(workspace, b); AP4_CopyMemory(a, b, 8); @@ -154,7 +148,7 @@ AP4_AesKeyUnwrap(const AP4_UI08* kek, r -= 8; } } - + // Step 3. Output results. // If A is an appropriate initial value (see 2.2.3), // Then @@ -162,14 +156,12 @@ AP4_AesKeyUnwrap(const AP4_UI08* kek, // P[i] = R[i] // Else // Return an error - for(unsigned int i = 0; i < 8; i++) - { - if(a[i] != 0xA6) - { + for (unsigned int i=0; i<8; i++) { + if (a[i] != 0xA6) { cleartext_key.SetDataSize(0); return AP4_ERROR_INVALID_FORMAT; } - } + } return AP4_SUCCESS; } |