javaextensions/satsa/crypto/src/cipherimpljni.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19: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:  JNI Layer corresponding to CipherImpl.java
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "com_nokia_mj_impl_crypto_CipherImpl.h"
       
    20 #include "stscipher.h"
       
    21 #include "stscipherfactory.h"
       
    22 #include "stsconstants.h"
       
    23 
       
    24 using namespace java::satsa;
       
    25 
       
    26 
       
    27 
       
    28 // JNI function update.
       
    29 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_crypto_CipherImpl__1update
       
    30 (JNIEnv* aJni,
       
    31  jclass,
       
    32  jint aHandle,
       
    33  jbyteArray aInput,
       
    34  jint aInputOffset,
       
    35  jint aInputLength,
       
    36  jbyteArray aOutput,
       
    37  jint aOutputOffset,
       
    38  jboolean aDoFinal)
       
    39 {
       
    40 
       
    41     // return value that has to be passed to java
       
    42     jint retVal = 0;
       
    43 
       
    44     // Get the cipher object from aHandle
       
    45     STSCipher* cipher = reinterpret_cast< STSCipher* >(aHandle);
       
    46 
       
    47     if (aDoFinal == JNI_TRUE)
       
    48     {
       
    49         // Finalize flag is set, call the native cipher's finalize function.
       
    50         retVal = cipher->DoFinal(aJni,
       
    51                                  aInput,
       
    52                                  aInputOffset,
       
    53                                  aInputLength,
       
    54                                  aOutput,
       
    55                                  aOutputOffset);
       
    56     }
       
    57     else
       
    58     {
       
    59         // Finalize flag is not set, call the native cipher's Update function.
       
    60         retVal = cipher->Update(aJni,
       
    61                                 aInput,
       
    62                                 aInputOffset,
       
    63                                 aInputLength,
       
    64                                 aOutput,
       
    65                                 aOutputOffset);
       
    66     } // end if
       
    67 
       
    68     // return to Java Side
       
    69     return retVal;
       
    70 
       
    71 }// end update
       
    72 
       
    73 
       
    74 
       
    75 // JNI function getIV.
       
    76 JNIEXPORT jbyteArray JNICALL Java_com_nokia_mj_impl_crypto_CipherImpl__1getIV
       
    77 (JNIEnv* aJni,
       
    78  jclass,
       
    79  jint aHandle)
       
    80 {
       
    81     // iv to be returned to java
       
    82     jbyteArray ivToJava = NULL;
       
    83     const unsigned char* iv = NULL;
       
    84     int iv_length = 0;
       
    85 
       
    86     // Get the cipher object from aHandle
       
    87     STSCipher* cipher = reinterpret_cast< STSCipher* >(aHandle);
       
    88 
       
    89     // retrieve the iv from native cipher object
       
    90     iv = cipher->IV(&iv_length);
       
    91 
       
    92     if (iv)
       
    93     {
       
    94         // cipher had an iv, create Java byte array for it
       
    95         ivToJava = aJni->NewByteArray(iv_length);
       
    96         if (ivToJava)
       
    97         {
       
    98             // copy iv to Java byte array
       
    99             aJni->SetByteArrayRegion(ivToJava,0,iv_length,(signed char*)iv);
       
   100         }
       
   101     }
       
   102     else
       
   103     {
       
   104         // iv is not applicable
       
   105         ivToJava = NULL;
       
   106     }
       
   107 
       
   108     // return iv to java
       
   109     return ivToJava;
       
   110 } //  end getIV
       
   111 
       
   112 
       
   113 
       
   114 // JNI function init.
       
   115 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_crypto_CipherImpl__1init
       
   116 (JNIEnv* aJni,
       
   117  jclass,
       
   118  jint aHandle,
       
   119  jint aOpMode,
       
   120  jstring aKeyAlgorithm,
       
   121  jstring aKeyFormat,
       
   122  jbyteArray aKeyEncoded,
       
   123  jbyteArray aParams)
       
   124 {
       
   125     // return value
       
   126     jint retVal = 0;
       
   127 
       
   128     // Get the cipher object from aHandle
       
   129     STSCipher* cipher = reinterpret_cast< STSCipher* >(aHandle);
       
   130 
       
   131     // aOpMode can only be valid value because checked in Java side
       
   132     STSCipher::TCipherMode mode = (STSCipher::TCipherMode)aOpMode;
       
   133 
       
   134     if (aParams)
       
   135     {
       
   136         // Params was given as a parameter in Java side
       
   137         retVal = cipher->Init(aJni, mode, aKeyAlgorithm,aKeyFormat,aKeyEncoded, aParams);
       
   138     }
       
   139     else
       
   140     {
       
   141         // There is no params for the cipher.
       
   142         retVal = cipher->Init(aJni, mode, aKeyAlgorithm,aKeyFormat,aKeyEncoded);
       
   143     } // end if
       
   144 
       
   145     // retrun to Java side.
       
   146     return retVal;
       
   147 }//end init
       
   148 
       
   149 
       
   150 
       
   151 // JNI function create.
       
   152 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_crypto_CipherImpl__1create
       
   153 (JNIEnv* aJni,
       
   154  jclass,
       
   155  jstring aTransformation)
       
   156 {
       
   157 
       
   158     // The cipher handle to be returned to java
       
   159     jint handle = KSTSErrNoMemory;
       
   160     int errCode = 0;
       
   161 
       
   162     // The cipher object that gets created.
       
   163     STSCipher* aCipher = NULL;
       
   164 
       
   165     // Create cipher object which uses the given algorithm
       
   166     aCipher = STSCipherFactory::CreateCipher(aJni, aTransformation, &errCode);
       
   167 
       
   168     if (aCipher == NULL)
       
   169     {
       
   170         // CreateCipher failed.
       
   171         // Set error code to handle which is returned to Java
       
   172         handle = errCode;
       
   173     }
       
   174     else
       
   175     {
       
   176         // return handle to new cipher object or error code
       
   177         handle = reinterpret_cast<jint>(aCipher);
       
   178     }
       
   179     return handle;
       
   180 
       
   181 }//end create
       
   182 
       
   183 
       
   184 
       
   185 // JNI function dispose.
       
   186 JNIEXPORT void
       
   187 JNICALL Java_com_nokia_mj_impl_crypto_CipherImpl__1dispose
       
   188 (JNIEnv* /*aJni*/,
       
   189  jclass,
       
   190  jint aHandle)
       
   191 {
       
   192 
       
   193     //Get the cipher object from aHandle
       
   194     STSCipher* cipher = reinterpret_cast< STSCipher* >(aHandle);
       
   195 
       
   196     if (NULL != cipher)
       
   197     {
       
   198         // Cleanup the native handle.
       
   199         delete cipher;
       
   200         cipher = 0;
       
   201     }
       
   202 
       
   203 }//end dispose
       
   204 
       
   205 
       
   206 //end of file
       
   207 
       
   208