javaextensions/satsa/crypto/src/messagedigestimpljni.cpp
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:  JNI Layer corresponding to MessageDigestImpl.java
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "javajniutils.h"
       
    20 #include "com_nokia_mj_impl_security_MessageDigestImpl.h"
       
    21 #include <stdio.h>
       
    22 #include <openssl/evp.h>
       
    23 #include "stsconstants.h"
       
    24 #include <hash.h>
       
    25 #include "logger.h"
       
    26 
       
    27 using namespace java::satsa;
       
    28 
       
    29 
       
    30 // JNI function.
       
    31 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_MessageDigestImpl__1reset
       
    32 (JNIEnv*, jclass, jint aHandle)
       
    33 {
       
    34     const EVP_MD* md = NULL;
       
    35     int retVal;
       
    36 
       
    37     EVP_MD_CTX* mdctx = reinterpret_cast< EVP_MD_CTX* >(aHandle);
       
    38 
       
    39     md = EVP_MD_CTX_md(mdctx);
       
    40     if (md == NULL)
       
    41     {
       
    42         return KSTSErrNoMemory;
       
    43     }
       
    44     EVP_MD_CTX_init(mdctx);
       
    45     retVal = EVP_DigestInit_ex(mdctx, md, NULL);
       
    46     if (retVal == 0)
       
    47     {
       
    48         // Set error code to handle variable which is returned to Java
       
    49         return KSTSErrNoMemory;
       
    50     }
       
    51 
       
    52     return 0;
       
    53 }
       
    54 
       
    55 
       
    56 // JNI function.
       
    57 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_MessageDigestImpl__1digest
       
    58 (JNIEnv* aJni, jclass, jint aHandle,
       
    59  jbyteArray aBuf, jint aOffset, jint aLength)
       
    60 {
       
    61     EVP_MD_CTX* mdctx = reinterpret_cast< EVP_MD_CTX* >(aHandle);
       
    62     const EVP_MD* md = NULL;
       
    63     unsigned int retVal;
       
    64     unsigned int err;
       
    65     unsigned char* buf = new unsigned char[EVP_MAX_MD_SIZE];
       
    66     err = EVP_DigestFinal_ex(mdctx, buf, &retVal);
       
    67     if (err == 1)
       
    68     {
       
    69         //digest successful
       
    70         if (aLength < retVal)
       
    71         {
       
    72             return KSTSErrNoMemory;
       
    73         }
       
    74         else
       
    75         {
       
    76             // set the output buffer with hashed data
       
    77             aJni->SetByteArrayRegion(aBuf,aOffset,retVal,(signed char*)buf);
       
    78 
       
    79             // reset the digest as per the spec
       
    80             md = EVP_MD_CTX_md(mdctx);
       
    81             if (md == NULL)
       
    82             {
       
    83                 return KSTSErrNoMemory;
       
    84             }
       
    85             EVP_MD_CTX_init(mdctx);
       
    86             err = EVP_DigestInit_ex(mdctx, md, NULL);
       
    87             if (err == 0)
       
    88             {
       
    89                 // Set error code to handle variable which is returned to Java
       
    90                 return KSTSErrNoMemory;
       
    91             }
       
    92             else
       
    93             {
       
    94                 return retVal;
       
    95             }
       
    96         }
       
    97     }
       
    98     else
       
    99     {
       
   100         return KSTSErrNoMemory;
       
   101     }
       
   102 }
       
   103 
       
   104 
       
   105 // JNI function.
       
   106 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_MessageDigestImpl__1update
       
   107 (JNIEnv* aJni, jclass, jint aHandle,
       
   108  jbyteArray aBuf, jint aOffset, jint aLength)
       
   109 {
       
   110     int err;
       
   111     EVP_MD_CTX* mdctx = reinterpret_cast< EVP_MD_CTX* >(aHandle);
       
   112     unsigned char* buf = new unsigned char[aLength];
       
   113     aJni->GetByteArrayRegion(aBuf,aOffset,aLength,(signed char *)buf);
       
   114     err = EVP_DigestUpdate(mdctx, buf, aLength);
       
   115     if (0 == err)
       
   116     {
       
   117         err = KSTSErrNoMemory;
       
   118     }
       
   119     return err;
       
   120 }
       
   121 
       
   122 
       
   123 // JNI function.
       
   124 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_security_MessageDigestImpl__1create
       
   125 (JNIEnv* aJni, jclass,
       
   126  jstring aAlgorithm)
       
   127 {
       
   128     int retVal;
       
   129     EVP_MD_CTX *mdctx = new EVP_MD_CTX;
       
   130     const EVP_MD *md;
       
   131 
       
   132     OpenSSL_add_all_digests();
       
   133 
       
   134     // Set the transformation string contents to a wstring.
       
   135     std::wstring algo;
       
   136     try
       
   137     {
       
   138         algo = java::util::JniUtils::jstringToWstring(aJni, aAlgorithm);
       
   139     }
       
   140     catch (...)
       
   141     {
       
   142         // do nothing
       
   143     }
       
   144     TInt handle = KSTSErrNoMemory;
       
   145 
       
   146     // Create digest object according to aAlgorithm
       
   147     if (algo == L"SHA-1")
       
   148     {
       
   149         md = EVP_sha1();
       
   150     }
       
   151     else if (algo == L"MD2")
       
   152     {
       
   153         md = EVP_md2();
       
   154     }
       
   155     else if (algo == L"MD5")
       
   156     {
       
   157         md = EVP_md5();
       
   158     }
       
   159     else
       
   160     {
       
   161         // Algorithm was not found
       
   162         handle = KSTSErrDigest;
       
   163         return handle;
       
   164     }
       
   165 
       
   166     EVP_MD_CTX_init(mdctx);
       
   167     retVal = EVP_DigestInit_ex(mdctx, md, NULL);
       
   168     if (0 == retVal)
       
   169     {
       
   170         // Set error code to handle variable which is returned to Java
       
   171         handle = KSTSErrNoMemory;
       
   172         return handle;
       
   173     }
       
   174 
       
   175     // return handle to new MessageDigest object or error code
       
   176     handle = reinterpret_cast<jint>(mdctx);
       
   177     return handle;
       
   178 }
       
   179 
       
   180 
       
   181 // JNI function dispose.
       
   182 JNIEXPORT void
       
   183 JNICALL Java_com_nokia_mj_impl_security_MessageDigestImpl__1dispose
       
   184 (JNIEnv* /*aJni*/,
       
   185  jclass,
       
   186  jint aHandle)
       
   187 {
       
   188 
       
   189     //Get the digest object from aHandle
       
   190     EVP_MD_CTX* mdctx = reinterpret_cast< EVP_MD_CTX* >(aHandle);
       
   191 
       
   192     if (mdctx != NULL)
       
   193     {
       
   194         // Cleanup the native handle.
       
   195         delete mdctx;
       
   196         mdctx = 0;
       
   197     }
       
   198 
       
   199 }//end dispose
       
   200 
       
   201 //end of file
       
   202 
       
   203 
       
   204