javaextensions/satsa/crypto/src/stscipher.h
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Provides the functionality of a cryptographic cipher for
       
    15  *                encryption and decryption.
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef STSCIPHER_H
       
    21 #define STSCIPHER_H
       
    22 
       
    23 //  INCLUDES
       
    24 #include <stdlib.h>
       
    25 #include <jni.h>
       
    26 #include <openssl/evp.h>
       
    27 #include "ststransformation.h"
       
    28 
       
    29 namespace java
       
    30 {
       
    31 namespace satsa
       
    32 {
       
    33 // CLASS DECLARATION
       
    34 
       
    35 /**
       
    36  *  STSCipher provides the functionality of a cryptographic cipher for
       
    37  *  encryption and decryption.
       
    38  */
       
    39 class STSCipher
       
    40 {
       
    41 public:
       
    42     /**
       
    43      * Used to initialize cipher to encrypt or decrypt mode.
       
    44      */
       
    45     enum TCipherMode
       
    46     {
       
    47         EEncryptMode = 1, EDecryptMode = 2
       
    48     };
       
    49 
       
    50     virtual ~STSCipher();
       
    51 
       
    52 
       
    53     /**
       
    54      * Initializes this chipher. Creates needed transformation objects and
       
    55      * checks key validity.
       
    56      *
       
    57      * @param aMode EEncryptMode or EDecryptMode
       
    58      * @param aKey Key material used in the operations.
       
    59      */
       
    60     virtual jint Init(JNIEnv* aJni, const TCipherMode aMode,
       
    61                       const jstring aKeyAlgorithm, const jstring aKeyFormat,
       
    62                       const jbyteArray aKeyEncoded);
       
    63 
       
    64     /**
       
    65      * Initializes this cipher. Creates needed transformation objects and
       
    66      * checks key validity.
       
    67      *
       
    68      * @param aMode EEncryptMode or EDecryptMode
       
    69      * @param aKey Key material used in the operations
       
    70      * @param aParams Parameters for the algorithm
       
    71      */
       
    72     virtual jint Init(JNIEnv* aJni, const TCipherMode aMode,
       
    73                       const jstring aKeyAlgorithm, const jstring aKeyFormat,
       
    74                       const jbyteArray aKeyEncoded, const jbyteArray aParams);
       
    75 
       
    76     /**
       
    77      * Encrypts or decrypts data in a single-part operation,
       
    78      * or finishes a multiple-part operation.
       
    79      *
       
    80      * @param aInput Input buffer for the chipher.
       
    81      * @param aOutput Output buffer for the chipher.
       
    82      */
       
    83     virtual jint DoFinal(JNIEnv* aJni, jbyteArray aInput, jint aInputOffset,
       
    84                          jint aInputLength, jbyteArray aOutput, jint aOutputOffset) = 0;
       
    85 
       
    86     /**
       
    87      * Continues a multiple-part encryption or decryption operation.
       
    88      *
       
    89      * @param aInput Input buffer for the chipher.
       
    90      * @param aOutput Output buffer for the chipher.
       
    91      */
       
    92     virtual jint Update(JNIEnv* aJni, jbyteArray aInput, jint aInputOffset,
       
    93                         jint aInputLength, jbyteArray aOutput, jint aOutputOffset) = 0;
       
    94 
       
    95     /**
       
    96      * Return initialization vector or empty descriptor if IV is not set.
       
    97      *
       
    98      * @return Initialization vector.
       
    99      */
       
   100     virtual const unsigned char* IV(int* length) const;
       
   101 
       
   102     /**
       
   103      * Sets transformation to the cipher. Ownership is transferred.
       
   104      *
       
   105      * @param aTransformation Transformation used in encryption
       
   106      *        and decryption. Ownership is transferred.
       
   107      */
       
   108     void SetTransformation(STSTransformation* aTransformation);
       
   109 
       
   110 protected:
       
   111     // Pure virtual function which will be implemented by the individual
       
   112     // cipher classes.
       
   113     virtual jint DoInit(JNIEnv* aJni, const TCipherMode aMode,
       
   114                         const jstring aKeyAlgorithm, const jstring aKeyFormat,
       
   115                         const jbyteArray aKeyEncoded, const jbyteArray aParams) = 0;
       
   116 
       
   117     STSCipher();
       
   118 
       
   119 protected:
       
   120 
       
   121     // Transformation used in encryption and decryption.
       
   122     STSTransformation* mTransformation;
       
   123 
       
   124     // initialization vector
       
   125     unsigned char* mIV;
       
   126     unsigned int miv_length;
       
   127 
       
   128     // Key for the cipher
       
   129     unsigned char* mKey;
       
   130     unsigned int mKey_length;
       
   131 
       
   132     // Mode set in Init method.
       
   133     TCipherMode mMode;
       
   134 
       
   135 };
       
   136 } // namespace satsa
       
   137 } // namespace java
       
   138 #endif // STSCIPHER_H
       
   139 // End of File