jdk/src/share/native/sun/security/pkcs11/wrapper/p11_digest.c
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 14414 f338be3ef659
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14414
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * Redistribution and use in  source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * modification, are permitted  provided that the following conditions are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * 1. Redistributions of  source code must retain the above copyright notice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *    this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * 2. Redistributions in  binary form must reproduce the above copyright notice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *    this list of conditions and the following disclaimer in the documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *    and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 3. The end-user documentation included with the redistribution, if any, must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *    include the following acknowledgment:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *    "This product includes software developed by IAIK of Graz University of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 *     Technology."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 *    Alternately, this acknowledgment may appear in the software itself, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 *    and wherever such third-party acknowledgments normally appear.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * 4. The names "Graz University of Technology" and "IAIK of Graz University of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *    Technology" must not be used to endorse or promote products derived from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *    this software without prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * 5. Products derived from this software may not be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *    written permission of Graz University of Technology.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *  POSSIBILITY  OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "pkcs11wrapper.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <assert.h>
14414
f338be3ef659 8001579: Cleanup warnings in security native code
jzavgren
parents: 14342
diff changeset
    54
#include "jlong.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include "sun_security_pkcs11_wrapper_PKCS11.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#ifdef P11_ENABLE_C_DIGESTINIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Method:    C_DigestInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Signature: (JLsun/security/pkcs11/wrapper/CK_MECHANISM;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @param   jobject jMechanism          CK_MECHANISM_PTR pMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    if (ckpFunctions == NULL) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    79
    if ((*env)->ExceptionCheck(env)) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    if (ckMechanism.pParameter != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
    87
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#ifdef P11_ENABLE_C_DIGEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Method:    C_Digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Signature: (J[BII[BII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @param   jbyteArray jData            CK_BYTE_PTR pData
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *                                      CK_ULONG ulDataLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @return  jbyteArray jDigest          CK_BYTE_PTR pDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *                                      CK_ULONG_PTR pulDigestLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestSingle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jobject jMechanism, jbyteArray jIn, jint jInOfs, jint jInLen, jbyteArray jDigest, jint jDigestOfs, jint jDigestLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    CK_BYTE_PTR bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    CK_BYTE DIGESTBUF[MAX_DIGEST_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    CK_ULONG ckDigestLength = min(MAX_DIGEST_LEN, jDigestLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    CK_MECHANISM ckMechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if (ckpFunctions == NULL) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    jMechanismToCKMechanism(env, jMechanism, &ckMechanism);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   119
    if ((*env)->ExceptionCheck(env)) { return 0; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    rv = (*ckpFunctions->C_DigestInit)(ckSessionHandle, &ckMechanism);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    if (ckMechanism.pParameter != NULL_PTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        free(ckMechanism.pParameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   127
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return 0; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        /* always use single part op, even for large data */
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   133
        bufP = (CK_BYTE_PTR) malloc((size_t)jInLen);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   134
        if (bufP == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   135
            throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   136
            return 0;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   137
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    (*env)->GetByteArrayRegion(env, jIn, jInOfs, jInLen, (jbyte *)bufP);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   141
    if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   142
        if (bufP != BUF) { free(bufP); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   146
    rv = (*ckpFunctions->C_Digest)(ckSessionHandle, bufP, jInLen, DIGESTBUF, &ckDigestLength);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   147
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   148
        (*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)DIGESTBUF);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   149
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   151
    if (bufP != BUF) { free(bufP); }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   152
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    return ckDigestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
#ifdef P11_ENABLE_C_DIGESTUPDATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Method:    C_DigestUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Signature: (J[B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @param   jbyteArray jData            CK_BYTE_PTR pData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *                                      CK_ULONG ulDataLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong directIn, jbyteArray jIn, jint jInOfs, jint jInLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    CK_BYTE_PTR bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    CK_BYTE BUF[MAX_STACK_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    jsize bufLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if (ckpFunctions == NULL) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    if (directIn != 0) {
14414
f338be3ef659 8001579: Cleanup warnings in security native code
jzavgren
parents: 14342
diff changeset
   182
        rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, (CK_BYTE_PTR)jlong_to_ptr(directIn), jInLen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ckAssertReturnValueOK(env, rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    if (jInLen <= MAX_STACK_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        bufLen = MAX_STACK_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        bufLen = min(MAX_HEAP_BUFFER_LEN, jInLen);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   192
        bufP = (CK_BYTE_PTR) malloc((size_t)bufLen);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   193
        if (bufP == NULL) {
10798
413b731e1818 7103549: Remove dependencies on libjava and libjvm from security libraries
chegar
parents: 5506
diff changeset
   194
            throwOutOfMemoryError(env, 0);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   195
            return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   196
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    while (jInLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        jsize chunkLen = min(bufLen, jInLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        (*env)->GetByteArrayRegion(env, jIn, jInOfs, chunkLen, (jbyte *)bufP);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   202
        if ((*env)->ExceptionCheck(env)) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   203
            if (bufP != BUF) { free(bufP); }
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   204
            return;
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   205
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        rv = (*ckpFunctions->C_DigestUpdate)(ckSessionHandle, bufP, chunkLen);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   207
        if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   208
            if (bufP != BUF) { free(bufP); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        jInOfs += chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        jInLen -= chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
#ifdef P11_ENABLE_C_DIGESTKEY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * Method:    C_DigestKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * Signature: (JJ)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * @param   jlong jKeyHandle            CK_OBJECT_HANDLE hKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jlong jKeyHandle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    CK_ULONG ckKeyHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    if (ckpFunctions == NULL) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    ckKeyHandle = jLongToCKULong(jKeyHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    rv = (*ckpFunctions->C_DigestKey)(ckSessionHandle, ckKeyHandle);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   244
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
#ifdef P11_ENABLE_C_DIGESTFINAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Method:    C_DigestFinal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * Signature: (J[BII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * @return  jbyteArray jDigest          CK_BYTE_PTR pDigest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *                                      CK_ULONG_PTR pulDigestLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
JNIEXPORT jint JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1DigestFinal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
  (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jDigest, jint jDigestOfs, jint jDigestLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    CK_BYTE BUF[MAX_DIGEST_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    CK_ULONG ckDigestLength = min(MAX_DIGEST_LEN, jDigestLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    if (ckpFunctions == NULL) { return 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    rv = (*ckpFunctions->C_DigestFinal)(ckSessionHandle, BUF, &ckDigestLength);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   272
    if (ckAssertReturnValueOK(env, rv) == CK_ASSERT_OK) {
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   273
        (*env)->SetByteArrayRegion(env, jDigest, jDigestOfs, ckDigestLength, (jbyte *)BUF);
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   274
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    return ckDigestLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
#ifdef P11_ENABLE_C_SEEDRANDOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * Method:    C_SeedRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * Signature: (J[B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 * @param   jbyteArray jSeed            CK_BYTE_PTR pSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *                                      CK_ULONG ulSeedLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1SeedRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jSeed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    CK_BYTE_PTR ckpSeed = NULL_PTR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    CK_ULONG ckSeedLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    if (ckpFunctions == NULL) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    jByteArrayToCKByteArray(env, jSeed, &ckpSeed, &ckSeedLength);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   302
    if ((*env)->ExceptionCheck(env)) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    rv = (*ckpFunctions->C_SeedRandom)(ckSessionHandle, ckpSeed, ckSeedLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    free(ckpSeed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   308
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
#ifdef P11_ENABLE_C_GENERATERANDOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * Class:     sun_security_pkcs11_wrapper_PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * Method:    C_GenerateRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * Signature: (J[B)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * Parametermapping:                    *PKCS11*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * @param   jlong jSessionHandle        CK_SESSION_HANDLE hSession
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * @param   jbyteArray jRandomData      CK_BYTE_PTR pRandomData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *                                      CK_ULONG ulRandomDataLen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
JNIEXPORT void JNICALL Java_sun_security_pkcs11_wrapper_PKCS11_C_1GenerateRandom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    (JNIEnv *env, jobject obj, jlong jSessionHandle, jbyteArray jRandomData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    CK_SESSION_HANDLE ckSessionHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    jbyte *jRandomBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    jlong jRandomBufferLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    CK_RV rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    CK_FUNCTION_LIST_PTR ckpFunctions = getFunctionList(env, obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (ckpFunctions == NULL) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    ckSessionHandle = jLongToCKULong(jSessionHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    jRandomBufferLength = (*env)->GetArrayLength(env, jRandomData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    jRandomBuffer = (*env)->GetByteArrayElements(env, jRandomData, NULL);
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   337
    if (jRandomBuffer == NULL) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    rv = (*ckpFunctions->C_GenerateRandom)(ckSessionHandle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                         (CK_BYTE_PTR) jRandomBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                         jLongToCKULong(jRandomBufferLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /* copy back generated bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    (*env)->ReleaseByteArrayElements(env, jRandomData, jRandomBuffer, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
2180
9994f4f08a59 6812738: SSL stress test with GF leads to 32 bit max process size in less than 5 minutes with PCKS11 provider
valeriep
parents: 2
diff changeset
   346
    if (ckAssertReturnValueOK(env, rv) != CK_ASSERT_OK) { return; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
#endif