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