omadrm/drmengine/utils/src/drmaescrypto.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  AES encryption utility for drm engine use
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 
       
    23 #include <symmetric.h>
       
    24 #include <rijndael.h>
       
    25 
       
    26 #include <drmtypes.h>
       
    27 
       
    28 #include "drmaescrypto.h"
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 // -----------------------------------------------------------------------------
       
    32 // DrmAesCrypto::DrmAesEncryptL
       
    33 // Encrypt data using a given key and insert initial vector on beginning
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C HBufC8* DrmAesCrypto::DrmAesEncryptL(
       
    37     const TDesC8& aKey,
       
    38     const TDesC8& aIV,
       
    39     const TBool aAddPadding,
       
    40     const TDesC8& aData )
       
    41     {
       
    42 
       
    43     HBufC8* retBuf( NULL );
       
    44 
       
    45     CModeCBCEncryptor* cbc( NULL );
       
    46     TInt lastBlockStart( 0 );
       
    47     TPtr8 data( NULL, 0, 0 );
       
    48 
       
    49     if( aIV.Length() != KDCFKeySize || aKey.Length() != KDCFKeySize )
       
    50         {
       
    51         User::Leave( KErrArgument );
       
    52         }
       
    53 
       
    54     cbc = CModeCBCEncryptor::NewL( CAESEncryptor::NewLC( aKey ), aIV );
       
    55     CleanupStack::Pop(); // CAESEncryptor, owned by cbc.
       
    56     CleanupStack::PushL( cbc );
       
    57 
       
    58     retBuf = HBufC8::NewLC( aData.Size() + aIV.Size() + KDCFKeySize );
       
    59     data.Set( retBuf->Des() );
       
    60     data.Copy( aIV );
       
    61     data.Append( aData );
       
    62 
       
    63     lastBlockStart = data.Length() - ( data.Length() % KDCFKeySize );
       
    64     // Loop through the data, excluding aIV
       
    65     for ( TInt i = KDCFKeySize; i  < lastBlockStart; i+= KDCFKeySize )
       
    66         {
       
    67         data.Set( retBuf->Des().MidTPtr( i, KDCFKeySize ) );
       
    68 
       
    69         cbc->Transform( data );
       
    70         }
       
    71 
       
    72     if ( aAddPadding )
       
    73         {
       
    74         TInt dataLength = retBuf->Length();
       
    75         TUint8 padding( static_cast< TUint8 >
       
    76             ( lastBlockStart + KDCFKeySize - dataLength ) );
       
    77 
       
    78         __ASSERT_DEBUG( lastBlockStart + KDCFKeySize - dataLength <= KDCFKeySize,
       
    79             User::Invariant() );
       
    80 
       
    81         retBuf->Des().SetLength( lastBlockStart + KDCFKeySize );
       
    82 
       
    83         for ( TInt i = dataLength; i < retBuf->Length(); ++i )
       
    84             {
       
    85             retBuf->Des()[ i ] = padding;
       
    86             }
       
    87 
       
    88         data.Set( retBuf->Des().MidTPtr( lastBlockStart, KDCFKeySize ) );
       
    89         cbc->Transform( data );
       
    90         }
       
    91 
       
    92     CleanupStack::Pop( retBuf );
       
    93     CleanupStack::PopAndDestroy( cbc );
       
    94     return retBuf;
       
    95 
       
    96     }
       
    97 
       
    98 //  End of File