src/jdk.crypto.ucrypto/solaris/native/libj2ucrypto/nativeCrypto.c
author igerasim
Sun, 18 Feb 2018 17:06:20 -0800
changeset 48911 edaa989b4e67
parent 47216 71c04702a3d5
child 49440 396ea30afbd5
permissions -rw-r--r--
8058965: Remove IPv6 support from TwoStacksPlainSocketImpl [win] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    26
#include <stdlib.h>
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
#include <string.h>
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    28
#include <strings.h>
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    29
#include <jni.h>
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 32499
diff changeset
    30
#include "jni_util.h"
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
#include "nativeCrypto.h"
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
#include "nativeFunc.h"
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
 * Dumps out byte array in hex with and name and length info
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
 */
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    37
void printError(char* header, int mech, int rv) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    38
  if (mech != -1) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    39
    printf("%s, mech = %d, rv = 0x%0x\n", header, mech, rv);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    40
  } else {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    41
    printf("%s, rv = 0x%0x\n", header, rv);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    42
  }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    43
  if (*ftab->ucryptoStrerror != NULL) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    44
    char * reason = (*ftab->ucryptoStrerror)(rv);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    45
    printf("\tcause = %s\n", reason);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    46
    free(reason);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    47
  }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    48
}
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    49
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    50
/*
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    51
 * Dumps out byte array in hex with and name and length info
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    52
 */
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
void printBytes(char* header, unsigned char* bytes, int len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
  int i;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
  printf("%s", header);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    57
  printf("len=%d {", len);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
  for (i = 0; i < len; i++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
    if (i > 0) printf(":");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
    printf("%02X", bytes[i]);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
  printf("}\n");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    63
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    64
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    66
 * Throws java.lang.OutOfMemoryError
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    67
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    68
void throwOutOfMemoryError(JNIEnv *env, const char *msg)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    69
{
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    70
  jclass jExClass = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    71
  if (jExClass != 0) /* Otherwise an exception has already been thrown */ {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    72
    (*env)->ThrowNew(env, jExClass, msg);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    73
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
  /* free the local ref */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    75
  (*env)->DeleteLocalRef(env, jExClass);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    76
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    77
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    78
/*
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    79
 * De-allocates all memory associated with crypto_ctx_t
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    80
 */
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    81
void freeContext(crypto_ctx_t *context) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    82
  if (ftab->ucryptoFreeContext != NULL) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    83
    (*ftab->ucryptoFreeContext)(context);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    84
  }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    85
  free(context);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    86
}
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
    87
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 32499
diff changeset
    88
JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *vm, void *reserved) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    89
    return JNI_VERSION_1_4;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    90
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    91
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    92
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    93
 * Class:     com_oracle_security_ucrypto_UcryptoProvider
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    94
 * Method:    loadLibraries
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    95
 * Signature: ()[Z
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    96
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
JNIEXPORT jbooleanArray JNICALL Java_com_oracle_security_ucrypto_UcryptoProvider_loadLibraries
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    98
(JNIEnv *env, jclass jcls) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    99
  jbooleanArray jResult;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
  jboolean *result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
  jResult = (*env)->NewBooleanArray(env, 2);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
  if (jResult != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
    result = loadNative();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   105
    (*env)->SetBooleanArrayRegion(env, jResult, 0, 2, result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
    free(result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   107
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   108
  return jResult;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   109
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   110
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   111
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
 * Class:     com_oracle_security_ucrypto_UcryptoProvider
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
 * Method:    getMechList
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
 * Signature: ()Ljava/lang/String;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   116
JNIEXPORT jstring JNICALL Java_com_oracle_security_ucrypto_UcryptoProvider_getMechList
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   117
(JNIEnv *env, jclass jcls) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   118
  jstring jResult;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   119
  char* result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   120
  int length;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   122
  jResult = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   123
  if (ftab->ucryptoVersion != NULL && ftab->ucryptoGetMechList != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   124
      length = (*ftab->ucryptoGetMechList)(NULL);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   125
      if (J2UC_DEBUG) printf("mech list length: %d\n", length);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   126
      result = malloc(length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   127
      if (result == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   128
        throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   129
        return NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   130
      }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   131
      length = (*ftab->ucryptoGetMechList)(result);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   132
      if (J2UC_DEBUG) printf("mech list: %s\n", result);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   133
      jResult = (*env)->NewStringUTF(env, result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   134
      free(result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   135
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   136
      // version 0 on Solaris 10
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   137
      result = "CRYPTO_AES_ECB,CRYPTO_AES_CBC,CRYPTO_AES_CFB128,";
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   138
      jResult = (*env)->NewStringUTF(env, result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   139
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   140
  return jResult;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   141
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   142
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   143
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   144
 * Utility function for throwing a UcryptoException when rv is not CRYPTO_OK(0)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   145
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   146
void throwUCExceptionUsingRV(JNIEnv *env, int rv) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   147
  jclass jExClass;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   148
  jmethodID jConstructor;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   149
  jthrowable jException;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   150
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   151
  if ((*env)->ExceptionCheck(env)) return;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   152
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   153
  jExClass = (*env)->FindClass(env, "com/oracle/security/ucrypto/UcryptoException");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   154
  /* if jExClass is NULL, an exception has already been thrown */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   155
  if (jExClass != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   156
    jConstructor = (*env)->GetMethodID(env, jExClass, "<init>", "(I)V");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   157
    if (jConstructor != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   158
      jException = (jthrowable) (*env)->NewObject(env, jExClass, jConstructor, rv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   159
      if (jException != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   160
        (*env)->Throw(env, jException);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   161
      }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   162
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   163
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   164
  /* free the local ref */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   165
  (*env)->DeleteLocalRef(env, jExClass);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   166
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   167
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   168
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   169
 * Utility function for duplicating a byte array from jbyteArray
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   170
 * If anything went wrong, no memory will be allocated.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   171
 * NOTE: caller is responsible for freeing the allocated memory
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   172
 * once this method returned successfully.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   173
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   174
jbyte* getBytes(JNIEnv *env, jbyteArray bytes, int offset, int len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   175
  jbyte* result = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   176
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   177
  if (!(*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   178
    result = (jbyte*) calloc(len, sizeof(char));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   179
    if (result == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   180
      throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   181
      return NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   182
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   183
    (*env)->GetByteArrayRegion(env, bytes, offset, len, result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   184
    if ((*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   185
        // free allocated memory if error occurred
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   186
        free(result);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   187
        return NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   188
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   189
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   190
  return result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   191
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   192
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   193
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   194
int
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   195
CipherInit(crypto_ctx_t *context, int encrypt, ucrypto_mech_t mech,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   196
           unsigned char *jKey, int jKeyLen, unsigned char *jIv, int jIvLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   197
           int tagLen, unsigned char *jAad, int jAadLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   198
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   199
{
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   200
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   201
  void *iv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   202
  size_t ivLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   203
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   204
  if (J2UC_DEBUG) printf("CipherInit: mech %i, key %i(%i), iv %i(%i) tagLen %i, aad %i(%i)\n",
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   205
                    mech, jKey, jKeyLen, jIv, jIvLen, tagLen, jAad, jAadLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   206
  if (mech == CRYPTO_AES_CTR) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   207
    ivLen = sizeof(CK_AES_CTR_PARAMS);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   208
    iv = (CK_AES_CTR_PARAMS*) malloc(ivLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   209
    if (iv == NULL) return -1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   210
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   211
    ((CK_AES_CTR_PARAMS*)iv)->ulCounterBits = 32;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   212
    memcpy(((CK_AES_CTR_PARAMS*)iv)->cb, jIv, 16);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   213
  } else if (mech == CRYPTO_AES_GCM) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   214
    ivLen = sizeof(CK_AES_GCM_PARAMS);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   215
    iv = (CK_AES_GCM_PARAMS*) malloc(ivLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   216
    if (iv == NULL) return -1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   217
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   218
    ((CK_AES_GCM_PARAMS*)iv)->pIv = (uchar_t *)jIv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   219
    ((CK_AES_GCM_PARAMS*)iv)->ulIvLen = (ulong_t)jIvLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   220
    ((CK_AES_GCM_PARAMS*)iv)->ulIvBits = 96;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   221
    ((CK_AES_GCM_PARAMS*)iv)->pAAD = (uchar_t *)jAad;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   222
    ((CK_AES_GCM_PARAMS*)iv)->ulAADLen = (ulong_t)jAadLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   223
    ((CK_AES_GCM_PARAMS*)iv)->ulTagBits = (ulong_t)tagLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   224
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   225
    // normal bytes
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   226
    iv = jIv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   227
    ivLen = jIvLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   228
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   229
  if (encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   230
    rv = (*ftab->ucryptoEncryptInit)(context, mech, jKey, (size_t)jKeyLen, iv, ivLen);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   231
    if (rv != 0 && J2UC_DEBUG) printError("ucryptoEncryptInit", mech, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   232
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   233
    rv =(*ftab->ucryptoDecryptInit)(context, mech, jKey, (size_t)jKeyLen, iv, ivLen);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   234
    if (rv != 0 && J2UC_DEBUG) printError("ucryptoDecryptInit", mech, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   235
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   236
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   237
  if (iv != jIv) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   238
    if (mech == CRYPTO_AES_CTR) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   239
      free((CK_AES_CTR_PARAMS*)iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   240
    } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   241
      free((CK_AES_GCM_PARAMS*)iv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   242
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   243
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   244
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   245
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   246
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   247
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   248
int
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   249
CipherUpdate(crypto_ctx_t *context, int encrypt, unsigned char *bufIn, int inOfs,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   250
             int inLen, unsigned char *bufOut, int outOfs, int *outLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   251
{
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   252
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   253
  size_t outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   254
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   255
  outLength = (size_t) *outLen;
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   256
  if (J2UC_DEBUG) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   257
    printf("CipherUpdate: Inofs %i, InLen %i, OutOfs %i, OutLen %i\n", inOfs, inLen, outOfs, *outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   258
    printBytes("BufIn=", (unsigned char*)(bufIn+inOfs), inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   259
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   260
  if (encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   261
    rv = (*ftab->ucryptoEncryptUpdate)(context, (unsigned char*)(bufIn+inOfs), (size_t)inLen, (unsigned char*)(bufOut+outOfs), &outLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   262
    if (rv) {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   263
      if (J2UC_DEBUG) printError("ucryptoEncryptUpdate", -1, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   264
    } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   265
      *outLen = (int)outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   266
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   267
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   268
    rv = (*ftab->ucryptoDecryptUpdate)(context, (unsigned char*)(bufIn+inOfs), (size_t)inLen, (unsigned char*)(bufOut+outOfs), &outLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   269
    if (rv) {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   270
      if (J2UC_DEBUG) printError("ucryptoDecryptUpdate", -1, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   271
    } else {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   272
      if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   273
      *outLen = (int)outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   274
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   275
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   276
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   277
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   278
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   279
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   280
int
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   281
CipherFinal(crypto_ctx_t *context, int encrypt, unsigned char *bufOut, int outOfs, int *outLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   282
{
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   283
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   284
  size_t outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   285
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   286
  outLength = (size_t)*outLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   287
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   288
  if (J2UC_DEBUG) printf("CipherFinal: OutOfs %i, outLen %i\n", outOfs, *outLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   289
  if (encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   290
    rv = (*ftab->ucryptoEncryptFinal)(context, (unsigned char*)(bufOut+outOfs), &outLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   291
    if (rv) {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   292
      if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   293
    } else {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   294
      if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   295
      *outLen = (int)outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   296
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   297
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   298
    rv = (*ftab->ucryptoDecryptFinal)(context, (unsigned char*)(bufOut+outOfs), &outLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   299
    if (rv) {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   300
      if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   301
    } else {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   302
      if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   303
      *outLen = (int)outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   304
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   305
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   306
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   307
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   308
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   309
////////////////////////////////////////////////////////
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   310
// SPECIAL ENTRIES FOR JVM JNI-BYPASSING OPTIMIZATION
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   311
////////////////////////////////////////////////////////
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   312
jlong JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeInit(jint mech) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   313
  crypto_ctx_t *context = NULL;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   314
  int rv;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   315
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   316
  context = malloc(sizeof(crypto_ctx_t));
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   317
  if (context != NULL) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   318
    rv = (*ftab->ucryptoDigestInit)(context, (ucrypto_mech_t) mech, NULL, 0);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   319
    if (rv) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   320
      freeContext(context);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   321
      if (J2UC_DEBUG) printError("ucryptoDigestInit", mech, rv);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   322
      return 0L;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   323
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   324
  }
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   325
  return (jlong) context;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   326
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   327
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   328
jint JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   329
  (jint mech, jlong pContext, int notUsed, unsigned char* in, jint ofs, jint len) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   330
  crypto_ctx_t *context;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   331
  jint rv = 0;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   332
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   333
  context = (crypto_ctx_t *) pContext;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   334
  rv = (*ftab->ucryptoDigestUpdate)(context, (const unsigned char*)(in + ofs),
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   335
                                    (size_t) len);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   336
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   337
  if (rv) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   338
    freeContext(context);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   339
    if (J2UC_DEBUG) printError("ucryptoDigestUpdate", mech, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   340
  }
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   341
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   342
  return -rv; // use negative value to indicate error
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   343
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   344
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   345
jint JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   346
  (jint mech, jlong pContext, int notUsed, unsigned char* out, jint ofs, jint digestLen) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   347
  crypto_ctx_t *context;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   348
  jint rv = 0;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   349
  size_t digest_len = digestLen;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   350
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   351
  context = (crypto_ctx_t *) pContext;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   352
  rv = (*ftab->ucryptoDigestFinal)(context, (unsigned char*)(out + ofs),
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   353
                                   &digest_len);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   354
  if (rv) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   355
    freeContext(context);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   356
    if (J2UC_DEBUG) printError("ucryptoDigestFinal", mech, rv);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   357
  }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   358
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   359
  return -rv; // use negative value to indicate error
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   360
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   361
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   362
void JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   363
  (jint mech, jlong pContext) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   364
  crypto_ctx_t *context;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   365
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   366
  context = (crypto_ctx_t *) pContext;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   367
  freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   368
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   369
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   370
// AES
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   371
jlong JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   372
  (jint mech, jboolean encrypt, int keyLen, unsigned char* bufKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   373
   int ivLen, unsigned char* bufIv, jint tagLen, int aadLen, unsigned char* bufAad) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   374
  crypto_ctx_t *context = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   375
  int rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   376
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   377
  context = malloc(sizeof(crypto_ctx_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   378
  if (context != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   379
    rv = CipherInit(context, encrypt, (ucrypto_mech_t) mech, bufKey, keyLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   380
                    bufIv, ivLen, tagLen, bufAad, aadLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   381
    if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   382
      freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   383
      return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   384
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   385
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   386
  return (jlong)context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   387
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   388
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   389
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   390
 * Class:     com_oracle_security_ucrypto_NativeCipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   391
 * Method:    nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   392
 * Signature: (JZ[BII[BI)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   393
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   394
jint JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   395
  (jlong pContext, jboolean encrypt, int notUsed, jbyte* bufIn, jint inOfs, jint inLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   396
   int outCapacity, jbyte* bufOut, jint outOfs) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   397
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   398
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   399
  int outLen = outCapacity - outOfs; // recalculate the real out length
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   400
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   401
  context = (crypto_ctx_t *) pContext;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   402
  rv = CipherUpdate(context, encrypt, (unsigned char*)bufIn, inOfs, inLen, (unsigned char*)bufOut, outOfs, &outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   403
  if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   404
    freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   405
    return -rv; // use negative value to indicate error!
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   406
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   407
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   408
  return outLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   409
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   410
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   411
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   412
 * Class:     com_oracle_security_ucrypto_NativeCipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   413
 * Method:    nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   414
 * Signature: (JZ[BI)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   415
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   416
jint JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeFinal
32499
0a653603f68f 8135099: 9-dev solaris builds failed on 2015-09-04
valeriep
parents: 32475
diff changeset
   417
  (jlong pContext, jboolean encrypt, int outLen, jbyte* out, jint outOfs) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   418
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   419
  int rv = 0;
32499
0a653603f68f 8135099: 9-dev solaris builds failed on 2015-09-04
valeriep
parents: 32475
diff changeset
   420
  unsigned char* bufOut = (unsigned char*) out;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   421
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   422
  context = (crypto_ctx_t *) pContext;
32475
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   423
  // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   424
  if (bufOut == NULL) {
32499
0a653603f68f 8135099: 9-dev solaris builds failed on 2015-09-04
valeriep
parents: 32475
diff changeset
   425
    bufOut = (unsigned char*)(&outLen);
32475
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   426
    outLen = 0;
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   427
  }
32499
0a653603f68f 8135099: 9-dev solaris builds failed on 2015-09-04
valeriep
parents: 32475
diff changeset
   428
  rv = CipherFinal(context, encrypt, bufOut, outOfs, &outLen);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   429
  freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   430
  if (rv) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   431
     return -rv; // use negative value to indicate error!
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   432
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   433
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   434
  return outLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   435
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   436
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   437
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   438
 * Class:     com_oracle_security_ucrypto_NativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   439
 * Method:    nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   440
 * Signature: (I)J
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   441
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   442
JNIEXPORT jlong JNICALL Java_com_oracle_security_ucrypto_NativeDigest_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   443
  (JNIEnv *env, jclass jcls, jint mech) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   444
  jlong result = JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeInit(mech);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   445
  if (result == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   446
     throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   447
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   448
  return result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   449
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   450
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   451
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   452
 * Class:     com_oracle_security_ucrypto_NativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   453
 * Method:    nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   454
 * Signature: (IJ[BII)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   455
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   456
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeDigest_nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   457
  (JNIEnv *env, jclass jcls, jint mech, jlong pContext, jbyteArray jIn, jint jOfs, jint jLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   458
  unsigned char *bufIn;
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   459
  jint rv = 0;
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   460
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   461
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   462
  bufIn = (unsigned char *) getBytes(env, jIn, jOfs, jLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   463
  if (!(*env)->ExceptionCheck(env)) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   464
    rv = JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeUpdate(mech, pContext, jLen, bufIn, 0, jLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   465
    free(bufIn);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   466
  }
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   467
  return rv;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   468
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   469
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   470
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   471
 * Class:     com_oracle_security_ucrypto_NativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   472
 * Method:    nativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   473
 * Signature: (IJ[BII)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   474
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   475
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeDigest_nativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   476
  (JNIEnv *env, jclass jcls, jint mech, jlong pContext, jbyteArray jOut, jint jOutOfs, jint digestLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   477
  unsigned char *bufOut;
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   478
  jint rv = 0;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   479
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   480
  bufOut = (unsigned char *) malloc(digestLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   481
  if (bufOut == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   482
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   483
    return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   484
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   485
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   486
  rv = JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeDigest(mech, pContext, digestLen, bufOut, 0, digestLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   487
  if (rv == 0) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   488
      (*env)->SetByteArrayRegion(env, jOut, jOutOfs, digestLen, (jbyte *) bufOut);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   489
  }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   490
  free(bufOut);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   491
  return rv;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   492
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   493
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   494
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   495
 * Class:     com_oracle_security_ucrypto_NativeDigest
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   496
 * Method:    nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   497
 * Signature: (IJ)V
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   498
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   499
JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeDigest_nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   500
  (JNIEnv *env, jclass jcls, jint mech, jlong pContext) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   501
  JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeFree(mech, pContext);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   502
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   503
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   504
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   505
 * Class:     com_oracle_security_ucrypto_NativeCipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   506
 * Method:    nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   507
 * Signature: (IZ[B[BI[B)J
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   508
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   509
JNIEXPORT jlong JNICALL Java_com_oracle_security_ucrypto_NativeCipher_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   510
(JNIEnv *env, jclass jcls, jint mech, jboolean encrypt, jbyteArray jKey,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   511
 jbyteArray jIv, jint tagLen, jbyteArray jAad) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   512
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   513
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   514
  unsigned char *bufKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   515
  unsigned char *bufIv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   516
  unsigned char *bufAad;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   517
  int keyLen, ivLen, aadLen, rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   518
  jlong result = 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   519
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   520
  bufKey = bufIv = bufAad = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   521
  keyLen = ivLen = aadLen = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   522
  context = malloc(sizeof(crypto_ctx_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   523
  if (context == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   524
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   525
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   526
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   527
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   528
  // jKey MUST NOT BE NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   529
  keyLen = (*env)->GetArrayLength(env, jKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   530
  bufKey = (unsigned char *) (*env)->GetByteArrayElements(env, jKey, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   531
  if (bufKey == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   532
    goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   533
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   534
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   535
  if (jIv != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   536
    ivLen = (*env)->GetArrayLength(env, jIv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   537
    bufIv = (unsigned char *) (*env)->GetByteArrayElements(env, jIv, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   538
    if (bufIv == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   539
      goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   540
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   541
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   542
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   543
  if (jAad != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   544
    aadLen = (*env)->GetArrayLength(env, jAad);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   545
    bufAad = (unsigned char *) (*env)->GetByteArrayElements(env, jAad, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   546
    if (bufAad == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   547
      goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   548
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   549
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   550
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   551
  rv = CipherInit(context, encrypt, mech, bufKey, keyLen, bufIv, ivLen, tagLen, bufAad, aadLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   552
  if (rv != 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   553
    throwUCExceptionUsingRV(env, rv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   554
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   555
     result = (jlong) context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   556
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   557
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   558
cleanup:
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   559
  if ((result == 0L) && (context != NULL)) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   560
    freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   561
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   562
  if (bufKey != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   563
    (*env)->ReleaseByteArrayElements(env, jKey, (jbyte *)bufKey, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   564
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   565
  if (bufIv != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   566
    (*env)->ReleaseByteArrayElements(env, jIv, (jbyte *)bufIv, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   567
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   568
  if (bufAad != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   569
    (*env)->ReleaseByteArrayElements(env, jAad, (jbyte *)bufAad, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   570
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   571
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   572
  return result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   573
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   574
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   575
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   576
 * Class:     com_oracle_security_ucrypto_NativeCipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   577
 * Method:    nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   578
 * Signature: (JZ[BII[BI)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   579
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   580
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeCipher_nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   581
  (JNIEnv *env, jclass jcls, jlong contextID, jboolean encrypt,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   582
    jbyteArray jIn, jint inOfs, jint inLen, jbyteArray jOut, jint outOfs) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   583
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   584
  unsigned char *bufIn;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   585
  unsigned char *bufOut;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   586
  int outLen, rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   587
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   588
  context = (crypto_ctx_t *) contextID;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   589
  bufIn = (unsigned char *) getBytes(env, jIn, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   590
  if ((*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   591
    return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   592
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   593
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   594
  outLen = (*env)->GetArrayLength(env, jOut) - outOfs;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   595
  bufOut = calloc(outLen, sizeof(char));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   596
  if (bufOut == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   597
    free(bufIn);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   598
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   599
    return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   600
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   601
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   602
  rv = CipherUpdate(context, encrypt, bufIn, 0, inLen, bufOut, 0, &outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   603
  if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   604
    freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   605
    free(bufIn);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   606
    free(bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   607
    return -rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   608
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   609
    (*env)->SetByteArrayRegion(env, jOut, outOfs, outLen, (jbyte *)bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   610
    free(bufIn);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   611
    free(bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   612
    return outLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   613
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   614
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   615
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   616
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   617
 * Class:     com_oracle_security_ucrypto_NativeCipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   618
 * Method:    nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   619
 * Signature: (JZ[BI)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   620
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   621
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeCipher_nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   622
  (JNIEnv *env, jclass jCls, jlong contextID, jboolean encrypt,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   623
   jbyteArray out, jint outOfs) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   624
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   625
  unsigned char *bufIn;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   626
  unsigned char *bufOut;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   627
  int outLen, rv = 0;
37804
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   628
  jint rc;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   629
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   630
  context = (crypto_ctx_t *) contextID;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   631
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   632
  // out is null when nativeFinal() is called solely for resource clean up
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   633
  if (out == NULL) {
32475
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   634
    // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
d4e63a5e73c8 8130875: Ucrypto library leaks memory when null output buffer is specified
valeriep
parents: 32472
diff changeset
   635
    bufOut = (unsigned char *)(&outLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   636
    outLen = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   637
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   638
    outLen = (*env)->GetArrayLength(env, out) - outOfs;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   639
    bufOut = calloc(outLen, sizeof(char));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   640
    if (bufOut == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   641
      throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   642
      return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   643
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   644
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   645
  rv = CipherFinal(context, encrypt, bufOut, 0, &outLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   646
  if (rv) {
37804
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   647
    rc = -rv;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   648
  } else {
37804
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   649
    if (outLen > 0) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   650
      (*env)->SetByteArrayRegion(env, out, outOfs, outLen, (jbyte *)bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   651
    }
37804
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   652
    rc = outLen;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   653
  }
37804
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   654
  free(context);
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   655
  if (bufOut != (unsigned char *)(&outLen)) {
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   656
    free(bufOut);
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   657
  }
3d485fe01521 8155211: Ucrypto Library leaks native memory
stuefe
parents: 33653
diff changeset
   658
  return rc;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   659
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   660
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   661
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   662
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   663
 * Class:     com_oracle_security_ucrypto_NativeKey
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   664
 * Method:    nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   665
 * Signature: (JI)V
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   666
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   667
void JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   668
  (jlong id, jint numOfComponents) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   669
  crypto_object_attribute_t* pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   670
  int i;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   671
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   672
  pKey = (crypto_object_attribute_t*) id;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   673
  for (i = 0; i < numOfComponents; i++) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   674
    free(pKey[i].oa_value);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   675
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   676
  free(pKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   677
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   678
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   679
JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeKey_nativeFree
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   680
  (JNIEnv *env, jclass jCls, jlong id, jint numOfComponents) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   681
  JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree(id, numOfComponents);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   682
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   683
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   684
/*
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   685
 * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivate
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   686
 * Method:    nativeInit
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   687
 * Signature: ([B[B)J
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   688
 */
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   689
jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivate_nativeInit
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   690
(int modLen, jbyte* jMod, int privLen, jbyte* jPriv) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   691
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   692
  unsigned char *mod, *priv;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   693
  crypto_object_attribute_t* pKey = NULL;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   694
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   695
  pKey = calloc(2, sizeof(crypto_object_attribute_t));
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   696
  if (pKey == NULL) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   697
    return 0L;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   698
  }
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   699
  mod = priv = NULL;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   700
  mod = malloc(modLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   701
  priv = malloc(privLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   702
  if (mod == NULL || priv == NULL) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   703
    free(pKey);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   704
    free(mod);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   705
    free(priv);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   706
    return 0L;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   707
  } else {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   708
    memcpy(mod, jMod, modLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   709
    memcpy(priv, jPriv, privLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   710
  }
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   711
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   712
  // NOTE: numOfComponents should be 2
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   713
  pKey[0].oa_type = SUN_CKA_MODULUS;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   714
  pKey[0].oa_value = (char*) mod;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   715
  pKey[0].oa_value_len = (size_t) modLen;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   716
  pKey[1].oa_type = SUN_CKA_PRIVATE_EXPONENT;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   717
  pKey[1].oa_value = (char*) priv;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   718
  pKey[1].oa_value_len = (size_t) privLen;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   719
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   720
  return (jlong) pKey;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   721
}
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   722
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   723
JNIEXPORT jlong JNICALL
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   724
Java_com_oracle_security_ucrypto_NativeKey_00024RSAPrivate_nativeInit
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   725
  (JNIEnv *env, jclass jCls, jbyteArray jMod, jbyteArray jPriv) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   726
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   727
  int modLen, privLen;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   728
  jbyte *bufMod, *bufPriv;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   729
  crypto_object_attribute_t* pKey = NULL;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   730
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   731
  bufMod = bufPriv = NULL;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   732
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   733
  modLen = (*env)->GetArrayLength(env, jMod);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   734
  bufMod = getBytes(env, jMod, 0, modLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   735
  if ((*env)->ExceptionCheck(env)) goto cleanup;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   736
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   737
  privLen = (*env)->GetArrayLength(env, jPriv);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   738
  bufPriv = getBytes(env, jPriv, 0, privLen);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   739
  if ((*env)->ExceptionCheck(env)) goto cleanup;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   740
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   741
  // proceed if no error; otherwise free allocated memory
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   742
  pKey = calloc(2, sizeof(crypto_object_attribute_t));
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   743
  if (pKey == NULL) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   744
    throwOutOfMemoryError(env, NULL);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   745
    goto cleanup;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   746
  }
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   747
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   748
  // NOTE: numOfComponents should be 2
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   749
  pKey[0].oa_type = SUN_CKA_MODULUS;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   750
  pKey[0].oa_value = (char*) bufMod;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   751
  pKey[0].oa_value_len = (size_t) modLen;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   752
  pKey[1].oa_type = SUN_CKA_PRIVATE_EXPONENT;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   753
  pKey[1].oa_value = (char*) bufPriv;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   754
  pKey[1].oa_value_len = (size_t) privLen;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   755
  return (jlong) pKey;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   756
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   757
cleanup:
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   758
  free(bufMod);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   759
  free(bufPriv);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   760
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   761
  return 0L;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   762
}
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   763
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 27182
diff changeset
   764
/*
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   765
 * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivateCrt
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   766
 * Method:    nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   767
 * Signature: ([B[B[B[B[B[B[B[B)J
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   768
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   769
jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivateCrt_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   770
(int modLen, jbyte* jMod, int pubLen, jbyte* jPub, int privLen, jbyte* jPriv,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   771
 int pLen, jbyte* jP, int qLen, jbyte* jQ, int expPLen, jbyte* jExpP,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   772
 int expQLen, jbyte* jExpQ, int crtCoeffLen, jbyte* jCrtCoeff) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   773
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   774
  unsigned char *mod, *pub, *priv, *p, *q, *expP, *expQ, *crtCoeff;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   775
  crypto_object_attribute_t* pKey = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   776
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   777
  pKey = calloc(8, sizeof(crypto_object_attribute_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   778
  if (pKey == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   779
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   780
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   781
  mod = pub = priv = p = q = expP = expQ = crtCoeff = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   782
  mod = malloc(modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   783
  pub = malloc(pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   784
  priv = malloc(privLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   785
  p = malloc(pLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   786
  q = malloc(qLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   787
  expP = malloc(expPLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   788
  expQ = malloc(expQLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   789
  crtCoeff = malloc(crtCoeffLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   790
  if (mod == NULL || pub == NULL || priv == NULL || p == NULL ||
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   791
      q == NULL || expP == NULL || expQ == NULL || crtCoeff == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   792
    free(pKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   793
    free(mod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   794
    free(pub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   795
    free(priv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   796
    free(p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   797
    free(q);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   798
    free(expP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   799
    free(expQ);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   800
    free(crtCoeff);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   801
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   802
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   803
    memcpy(mod, jMod, modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   804
    memcpy(pub, jPub, pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   805
    memcpy(priv, jPriv, privLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   806
    memcpy(p, jP, pLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   807
    memcpy(q, jQ, qLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   808
    memcpy(expP, jExpP, expPLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   809
    memcpy(expQ, jExpQ, expQLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   810
    memcpy(crtCoeff, jCrtCoeff, crtCoeffLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   811
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   812
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   813
  // NOTE: numOfComponents should be 8
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   814
  pKey[0].oa_type = SUN_CKA_MODULUS;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   815
  pKey[0].oa_value = (char*) mod;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   816
  pKey[0].oa_value_len = (size_t) modLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   817
  pKey[1].oa_type = SUN_CKA_PUBLIC_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   818
  pKey[1].oa_value = (char*) pub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   819
  pKey[1].oa_value_len = (size_t) pubLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   820
  pKey[2].oa_type = SUN_CKA_PRIVATE_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   821
  pKey[2].oa_value = (char*) priv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   822
  pKey[2].oa_value_len = (size_t) privLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   823
  pKey[3].oa_type = SUN_CKA_PRIME_1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   824
  pKey[3].oa_value = (char*) p;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   825
  pKey[3].oa_value_len = (size_t) pLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   826
  pKey[4].oa_type = SUN_CKA_PRIME_2;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   827
  pKey[4].oa_value = (char*) q;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   828
  pKey[4].oa_value_len = (size_t) qLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   829
  pKey[5].oa_type = SUN_CKA_EXPONENT_1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   830
  pKey[5].oa_value = (char*) expP;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   831
  pKey[5].oa_value_len = (size_t) expPLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   832
  pKey[6].oa_type = SUN_CKA_EXPONENT_2;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   833
  pKey[6].oa_value = (char*) expQ;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   834
  pKey[6].oa_value_len = (size_t) expQLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   835
  pKey[7].oa_type = SUN_CKA_COEFFICIENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   836
  pKey[7].oa_value = (char*) crtCoeff;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   837
  pKey[7].oa_value_len = (size_t) crtCoeffLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   838
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   839
  return (jlong) pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   840
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   841
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   842
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   843
JNIEXPORT jlong JNICALL
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   844
Java_com_oracle_security_ucrypto_NativeKey_00024RSAPrivateCrt_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   845
  (JNIEnv *env, jclass jCls, jbyteArray jMod, jbyteArray jPub, jbyteArray jPriv,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   846
   jbyteArray jP, jbyteArray jQ, jbyteArray jExpP, jbyteArray jExpQ,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   847
   jbyteArray jCrtCoeff) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   848
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   849
  int modLen, pubLen, privLen, pLen, qLen, expPLen, expQLen, crtCoeffLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   850
  jbyte *bufMod, *bufPub, *bufPriv, *bufP, *bufQ, *bufExpP, *bufExpQ, *bufCrtCoeff;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   851
  crypto_object_attribute_t* pKey = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   852
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   853
  bufMod = bufPub = bufPriv = bufP = bufQ = bufExpP = bufExpQ = bufCrtCoeff = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   854
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   855
  modLen = (*env)->GetArrayLength(env, jMod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   856
  bufMod = getBytes(env, jMod, 0, modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   857
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   858
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   859
  pubLen = (*env)->GetArrayLength(env, jPub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   860
  bufPub = getBytes(env, jPub, 0, pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   861
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   862
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   863
  privLen = (*env)->GetArrayLength(env, jPriv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   864
  bufPriv = getBytes(env, jPriv, 0, privLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   865
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   866
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   867
  pLen = (*env)->GetArrayLength(env, jP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   868
  bufP = getBytes(env, jP, 0, pLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   869
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   870
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   871
  qLen = (*env)->GetArrayLength(env, jQ);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   872
  bufQ = getBytes(env, jQ, 0, qLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   873
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   874
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   875
  expPLen = (*env)->GetArrayLength(env, jExpP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   876
  bufExpP = getBytes(env, jExpP, 0, expPLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   877
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   878
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   879
  expQLen = (*env)->GetArrayLength(env, jExpQ);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   880
  bufExpQ = getBytes(env, jExpQ, 0, expQLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   881
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   882
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   883
  crtCoeffLen = (*env)->GetArrayLength(env, jCrtCoeff);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   884
  bufCrtCoeff = getBytes(env, jCrtCoeff, 0, crtCoeffLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   885
  if ((*env)->ExceptionCheck(env)) goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   886
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   887
  // proceed if no error; otherwise free allocated memory
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   888
  pKey = calloc(8, sizeof(crypto_object_attribute_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   889
  if (pKey == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   890
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   891
    goto cleanup;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   892
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   893
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   894
  // NOTE: numOfComponents should be 8
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   895
  pKey[0].oa_type = SUN_CKA_MODULUS;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   896
  pKey[0].oa_value = (char*) bufMod;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   897
  pKey[0].oa_value_len = (size_t) modLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   898
  pKey[1].oa_type = SUN_CKA_PUBLIC_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   899
  pKey[1].oa_value = (char*) bufPub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   900
  pKey[1].oa_value_len = (size_t) pubLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   901
  pKey[2].oa_type = SUN_CKA_PRIVATE_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   902
  pKey[2].oa_value = (char*) bufPriv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   903
  pKey[2].oa_value_len = (size_t) privLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   904
  pKey[3].oa_type = SUN_CKA_PRIME_1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   905
  pKey[3].oa_value = (char*) bufP;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   906
  pKey[3].oa_value_len = (size_t) pLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   907
  pKey[4].oa_type = SUN_CKA_PRIME_2;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   908
  pKey[4].oa_value = (char*) bufQ;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   909
  pKey[4].oa_value_len = (size_t) qLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   910
  pKey[5].oa_type = SUN_CKA_EXPONENT_1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   911
  pKey[5].oa_value = (char*) bufExpP;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   912
  pKey[5].oa_value_len = (size_t) expPLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   913
  pKey[6].oa_type = SUN_CKA_EXPONENT_2;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   914
  pKey[6].oa_value = (char*) bufExpQ;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   915
  pKey[6].oa_value_len = (size_t) expQLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   916
  pKey[7].oa_type = SUN_CKA_COEFFICIENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   917
  pKey[7].oa_value = (char*) bufCrtCoeff;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   918
  pKey[7].oa_value_len = (size_t) crtCoeffLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   919
  return (jlong) pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   920
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   921
cleanup:
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   922
  free(bufMod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   923
  free(bufPub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   924
  free(bufPriv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   925
  free(bufP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   926
  free(bufQ);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   927
  free(bufExpP);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   928
  free(bufExpQ);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   929
  free(bufCrtCoeff);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   930
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   931
  return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   932
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   933
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   934
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   935
 * Class:     com_oracle_security_ucrypto_NativeKey_RSAPublic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   936
 * Method:    nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   937
 * Signature: ([B[B)J
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   938
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   939
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   940
jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPublic_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   941
(int modLen, jbyte* jMod, int pubLen, jbyte* jPub) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   942
  unsigned char *mod, *pub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   943
  crypto_object_attribute_t* pKey = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   944
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   945
  pKey = calloc(2, sizeof(crypto_object_attribute_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   946
  if (pKey == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   947
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   948
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   949
  mod = pub = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   950
  mod = malloc(modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   951
  pub = malloc(pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   952
  if (mod == NULL || pub == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   953
    free(pKey);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   954
    free(mod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   955
    free(pub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   956
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   957
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   958
    memcpy(mod, jMod, modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   959
    memcpy(pub, jPub, pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   960
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   961
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
   962
  if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   963
    printf("RSAPublicKey.nativeInit: keyValue=%ld, keyLen=2\n", pKey);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   964
    printBytes("\tmod: ", (unsigned char*) mod, modLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
   965
    printBytes("\tpubExp: ", (unsigned char*) pub, pubLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   966
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   967
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   968
  pKey[0].oa_type = SUN_CKA_MODULUS;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   969
  pKey[0].oa_value = (char*) mod;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   970
  pKey[0].oa_value_len = (size_t) modLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   971
  pKey[1].oa_type = SUN_CKA_PUBLIC_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   972
  pKey[1].oa_value = (char*) pub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   973
  pKey[1].oa_value_len = (size_t) pubLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   974
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   975
  return (jlong) pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   976
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   977
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   978
JNIEXPORT jlong JNICALL
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   979
Java_com_oracle_security_ucrypto_NativeKey_00024RSAPublic_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   980
(JNIEnv *env, jclass jCls, jbyteArray jMod, jbyteArray jPub) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   981
  int modLen, pubLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   982
  jbyte *bufMod, *bufPub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   983
  crypto_object_attribute_t* pKey = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   984
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   985
  bufMod = bufPub = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   986
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   987
  modLen = (*env)->GetArrayLength(env, jMod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   988
  bufMod = getBytes(env, jMod, 0, modLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   989
  if ((*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   990
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   991
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   992
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   993
  pubLen = (*env)->GetArrayLength(env, jPub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   994
  bufPub = getBytes(env, jPub, 0, pubLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   995
  if ((*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   996
    free(bufMod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   997
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   998
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   999
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1000
  // proceed if no error; otherwise free allocated memory
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1001
  pKey = calloc(2, sizeof(crypto_object_attribute_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1002
  if (pKey != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1003
    // NOTE: numOfComponents should be 2
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1004
    pKey[0].oa_type = SUN_CKA_MODULUS;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1005
    pKey[0].oa_value = (char*) bufMod;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1006
    pKey[0].oa_value_len = (size_t) modLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1007
    pKey[1].oa_type = SUN_CKA_PUBLIC_EXPONENT;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1008
    pKey[1].oa_value = (char*) bufPub;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1009
    pKey[1].oa_value_len = (size_t) pubLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1010
    return (jlong) pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1011
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1012
    free(bufMod);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1013
    free(bufPub);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1014
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1015
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1016
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1017
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1018
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1019
////////////////////////
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1020
// NativeRSASignature
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1021
////////////////////////
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1022
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1023
int
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1024
SignatureInit(crypto_ctx_t *context, jint mechVal, jboolean sign,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1025
              uchar_t *pKey, size_t keyLength) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1026
  ucrypto_mech_t mech;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1027
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1028
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1029
  mech = (ucrypto_mech_t) mechVal;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1030
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1031
  if (sign) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1032
    rv = (*ftab->ucryptoSignInit)(context, mech, pKey, keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1033
                                  NULL, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1034
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1035
    rv = (*ftab->ucryptoVerifyInit)(context, mech, pKey, keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1036
                                    NULL, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1037
  }
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1038
  if (J2UC_DEBUG) {
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1039
    printf("SignatureInit: context=%ld, mech=%d, sign=%d, keyValue=%ld, keyLength=%d\n",
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1040
           context, mech, sign, pKey, keyLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1041
    printError("SignatureInit", mech, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1042
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1043
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1044
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1045
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1046
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1047
 * Class:     com_oracle_security_ucrypto_NativeRSASignature
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1048
 * Method:    nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1049
 * Signature: (IZJI[B)J
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1050
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1051
jlong JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1052
(jint mech, jboolean sign, jlong jKey, jint keyLength) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1053
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1054
  int rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1055
  uchar_t *pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1056
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1057
  context = malloc(sizeof(crypto_ctx_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1058
  if (context != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1059
    pKey = (uchar_t *) jKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1060
    rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1061
    if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1062
      freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1063
      return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1064
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1065
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1066
  return (jlong)context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1067
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1068
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1069
JNIEXPORT jlong JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeInit
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1070
(JNIEnv *env, jclass jCls, jint mech, jboolean sign, jlong jKey, jint keyLength) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1071
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1072
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1073
  uchar_t *pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1074
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1075
  context = malloc(sizeof(crypto_ctx_t));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1076
  if (context == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1077
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1078
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1079
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1080
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1081
  pKey = (uchar_t *) jKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1082
  rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1083
  if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1084
    freeContext(context);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1085
    throwUCExceptionUsingRV(env, rv);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1086
    return 0L;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1087
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1088
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1089
  return (jlong)context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1090
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1091
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1092
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1093
 * Class:     com_oracle_security_ucrypto_NativeRSASignature
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1094
 * Method:    nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1095
 * Signature: (JZ[BII)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1096
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1097
jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1098
(jlong pCtxt, jboolean sign, int notUsed, jbyte* jIn, jint jInOfs, jint jInLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1099
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1100
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1101
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1102
  context = (crypto_ctx_t *) pCtxt;
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1103
  if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1104
    printf("NativeRSASignature.nativeUpdate: context=%ld, sign=%d, jIn=%ld, jInOfs=%d, jInLen=%d\n",
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1105
           context, sign, jIn, jInOfs, jInLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1106
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1107
  if (sign) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1108
    rv = (*ftab->ucryptoSignUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1109
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1110
    rv = (*ftab->ucryptoVerifyUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1111
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1112
  if (rv) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1113
    freeContext(context);
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1114
    if (J2UC_DEBUG) printError("NativeRSASignature.nativeUpdate", -1, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1115
    return -rv; // use negative value to indicate error!
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1116
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1117
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1118
  return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1119
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1120
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1121
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1122
(JNIEnv *env, jclass jCls, jlong pCtxt, jboolean sign, jbyteArray jIn, jint inOfs, jint inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1123
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1124
  jbyte* bufIn;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1125
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1126
  bufIn = getBytes(env, jIn, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1127
  if ((*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1128
    return -1; // use negative value to indicate error!
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1129
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1130
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1131
  if (J2UC_DEBUG) printBytes("Update w/ data: ", (unsigned char*)bufIn, (size_t) inLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1132
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1133
  rv = JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1134
    (pCtxt, sign, inLen, bufIn, 0, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1135
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1136
  free(bufIn);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1137
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1138
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1139
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1140
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1141
 * Class:     com_oracle_security_ucrypto_NativeRSASignature
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1142
 * Method:    nativeUpdate
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1143
 * Signature: (JZJI)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1144
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1145
jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1146
(jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1147
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1148
  return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1149
    (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1150
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1151
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1152
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1153
(JNIEnv *env, jclass jCls, jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1154
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1155
  return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1156
    (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1157
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1158
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1159
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1160
 * Class:     com_oracle_security_ucrypto_NativeRSASignature
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1161
 * Method:    nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1162
 * Signature: (JZ[BII)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1163
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1164
jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1165
(jlong pCtxt, jboolean sign, int notUsed, jbyte* bufSig, jint sigOfs, jint jSigLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1166
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1167
  crypto_ctx_t *context;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1168
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1169
  size_t sigLength = (size_t) jSigLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1170
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1171
  context = (crypto_ctx_t *) pCtxt;
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1172
  if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1173
      printf("NativeRSASignature.nativeFinal: context=%ld, sign=%d, bufSig=%ld, sigOfs=%d, sigLen=%d\n",
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1174
             context, sign, bufSig, sigOfs, jSigLen);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1175
      printBytes("Before: SigBytes ", (unsigned char*) (bufSig + sigOfs), jSigLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1176
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1177
  if (sign) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1178
    rv = (*ftab->ucryptoSignFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1179
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1180
    rv = (*ftab->ucryptoVerifyFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1181
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1182
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1183
  freeContext(context);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1184
  if (rv) {
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1185
    if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1186
      printError("NativeRSASignature.nativeFinal", -1, rv);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1187
      if (sigLength != jSigLen) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1188
        printf("NativeRSASignature.nativeFinal out sig len=%d\n", sigLength);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1189
      }
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1190
      if (sign) {
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1191
        printBytes("After: SigBytes ", (unsigned char*) (bufSig + sigOfs), jSigLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1192
      }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1193
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1194
    return -rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1195
  } else return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1196
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1197
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1198
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1199
(JNIEnv *env, jclass jCls, jlong pCtxt, jboolean sign, jbyteArray jSig, jint jSigOfs, jint jSigLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1200
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1201
  jbyte* bufSig = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1202
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1203
  if (jSigLen != 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1204
    bufSig = calloc(jSigLen, sizeof(char));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1205
    if (bufSig == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1206
      throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1207
      return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1208
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1209
    if (!sign) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1210
      // need to copy over the to-be-verified signature bytes
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1211
      (*env)->GetByteArrayRegion(env, jSig, jSigOfs, jSigLen, (jbyte *)bufSig);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1212
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1213
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1214
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1215
  if (!(*env)->ExceptionCheck(env)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1216
    // Frees context + converts rv to negative if error occurred
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1217
    rv = JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeFinal
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1218
      (pCtxt, sign, jSigLen, bufSig, 0, jSigLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1219
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1220
    if (rv == 0 && sign) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1221
      // need to copy the generated signature bytes to the java bytearray
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1222
      (*env)->SetByteArrayRegion(env, jSig, jSigOfs, jSigLen, (jbyte *)bufSig);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1223
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1224
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1225
    // set rv to negative to indicate error
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1226
    rv = -1;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1227
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1228
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1229
  free(bufSig);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1230
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1231
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1232
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1233
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1234
/*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1235
 * Class:     com_oracle_security_ucrypto_NativeRSACipher
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1236
 * Method:    nativeAtomic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1237
 * Signature: (IZJI[BI[BII)I
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1238
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1239
jint JavaCritical_com_oracle_security_ucrypto_NativeRSACipher_nativeAtomic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1240
  (jint mech, jboolean encrypt, jlong keyValue, jint keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1241
   int notUsed1, jbyte* bufIn, jint jInLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1242
   int notUsed2, jbyte* bufOut, jint jOutOfs, jint jOutLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1243
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1244
  uchar_t *pKey;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1245
  crypto_object_attribute_t* pKey2;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1246
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1247
  size_t outLength = (size_t) jOutLen;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1248
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1249
  pKey = (uchar_t *) keyValue;
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1250
  if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1251
    printf("NativeRSACipher.nativeAtomic: mech=%d, encrypt=%d, pKey=%ld, keyLength=%d\n",
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1252
           mech, encrypt, pKey, keyLength);
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1253
    printBytes("Before: in  = ", (unsigned char*) bufIn, jInLen);
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1254
    printBytes("Before: out = ", (unsigned char*) (bufOut + jOutOfs), jOutLen);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1255
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1256
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1257
  if (encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1258
    rv = (*ftab->ucryptoEncrypt)((ucrypto_mech_t)mech, pKey, (size_t)keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1259
      NULL, 0, (uchar_t *)bufIn, (size_t)jInLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1260
      (uchar_t *)(bufOut + jOutOfs), &outLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1261
  } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1262
    rv = (*ftab->ucryptoDecrypt)((ucrypto_mech_t)mech, pKey, (size_t)keyLength,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1263
      NULL, 0, (uchar_t *)bufIn, (size_t)jInLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1264
      (uchar_t *)(bufOut + jOutOfs), &outLength);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1265
  }
42457
cd246ab491f9 8079898: Resolve disabled warnings for libj2ucrypto
valeriep
parents: 38878
diff changeset
  1266
  if (J2UC_DEBUG) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1267
    printError("NativeRSACipher.nativeAtomic", mech, rv);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1268
    if (outLength != jOutLen) {
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1269
      printf("NativeRSACipher.nativeAtomic out len=%d\n", outLength);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1270
    }
37909
38b1efe33344 8000415: Add support for SHA-3
valeriep
parents: 37804
diff changeset
  1271
    printBytes("After: ", (unsigned char*) (bufOut + jOutOfs), outLength);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1272
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1273
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1274
  if (rv) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1275
    return -rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1276
  } else return outLength;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1277
}
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1278
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1279
JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSACipher_nativeAtomic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1280
  (JNIEnv *env, jclass jCls, jint mech, jboolean encrypt,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1281
   jlong keyValue, jint keyLength, jbyteArray jIn, jint jInLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1282
   jbyteArray jOut, jint jOutOfs, jint jOutLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1283
  int rv = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1284
  jbyte *bufIn = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1285
  jbyte *bufOut = NULL;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1286
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1287
  if (jInLen != 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1288
    bufIn = (*env)->GetByteArrayElements(env, jIn, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1289
    if (bufIn == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1290
      return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1291
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1292
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1293
  bufOut = calloc(jOutLen, sizeof(jbyte));
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1294
  if (bufOut == NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1295
    (*env)->ReleaseByteArrayElements(env, jIn, bufIn, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1296
    throwOutOfMemoryError(env, NULL);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1297
    return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1298
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1299
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1300
  // rv: output length or error code (if negative)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1301
  rv = JavaCritical_com_oracle_security_ucrypto_NativeRSACipher_nativeAtomic
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1302
    (mech, encrypt, keyValue, keyLength, jInLen, bufIn, jInLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1303
     jOutLen, bufOut, 0, jOutLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1304
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1305
  if (rv > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1306
    (*env)->SetByteArrayRegion(env, jOut, jOutOfs, rv, (jbyte *)bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1307
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1308
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1309
  if (bufIn != NULL) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1310
    (*env)->ReleaseByteArrayElements(env, jIn, bufIn, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1311
  }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1312
  free(bufOut);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1313
  return rv;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
  1314
}