< prev index next >

src/jdk.crypto.ucrypto/solaris/native/libj2ucrypto/nativeCrypto.c

Print this page


   1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 292       if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
 293     } else {
 294       if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
 295       *outLen = (int)outLength;
 296     }
 297   } else {
 298     rv = (*ftab->ucryptoDecryptFinal)(context, (unsigned char*)(bufOut+outOfs), &outLength);
 299     if (rv) {
 300       if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
 301     } else {
 302       if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
 303       *outLen = (int)outLength;
 304     }
 305   }
 306   return rv;
 307 }
 308 
 309 ////////////////////////////////////////////////////////
 310 // SPECIAL ENTRIES FOR JVM JNI-BYPASSING OPTIMIZATION
 311 ////////////////////////////////////////////////////////
 312 jlong JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeInit(jint mech) {

 313   crypto_ctx_t *context = NULL;
 314   int rv;
 315 
 316   context = malloc(sizeof(crypto_ctx_t));
 317   if (context != NULL) {
 318     rv = (*ftab->ucryptoDigestInit)(context, (ucrypto_mech_t) mech, NULL, 0);
 319     if (rv) {
 320       freeContext(context);
 321       if (J2UC_DEBUG) printError("ucryptoDigestInit", mech, rv);
 322       return 0L;
 323     }
 324   }
 325   return (jlong) context;
 326 }
 327 
 328 jint JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeUpdate

 329   (jint mech, jlong pContext, int notUsed, unsigned char* in, jint ofs, jint len) {
 330   crypto_ctx_t *context;
 331   jint rv = 0;
 332 
 333   context = (crypto_ctx_t *) pContext;
 334   rv = (*ftab->ucryptoDigestUpdate)(context, (const unsigned char*)(in + ofs),
 335                                     (size_t) len);
 336 
 337   if (rv) {
 338     freeContext(context);
 339     if (J2UC_DEBUG) printError("ucryptoDigestUpdate", mech, rv);
 340   }
 341 
 342   return -rv; // use negative value to indicate error
 343 }
 344 
 345 jint JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeDigest

 346   (jint mech, jlong pContext, int notUsed, unsigned char* out, jint ofs, jint digestLen) {
 347   crypto_ctx_t *context;
 348   jint rv = 0;
 349   size_t digest_len = digestLen;
 350 
 351   context = (crypto_ctx_t *) pContext;
 352   rv = (*ftab->ucryptoDigestFinal)(context, (unsigned char*)(out + ofs),
 353                                    &digest_len);
 354   if (rv) {
 355     freeContext(context);
 356     if (J2UC_DEBUG) printError("ucryptoDigestFinal", mech, rv);
 357   }
 358 
 359   return -rv; // use negative value to indicate error
 360 }
 361 
 362 void JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeFree

 363   (jint mech, jlong pContext) {
 364   crypto_ctx_t *context;
 365 
 366   context = (crypto_ctx_t *) pContext;
 367   freeContext(context);
 368 }
 369 
 370 // AES
 371 jlong JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeInit

 372   (jint mech, jboolean encrypt, int keyLen, unsigned char* bufKey,
 373    int ivLen, unsigned char* bufIv, jint tagLen, int aadLen, unsigned char* bufAad) {
 374   crypto_ctx_t *context = NULL;
 375   int rv;
 376 
 377   context = malloc(sizeof(crypto_ctx_t));
 378   if (context != NULL) {
 379     rv = CipherInit(context, encrypt, (ucrypto_mech_t) mech, bufKey, keyLen,
 380                     bufIv, ivLen, tagLen, bufAad, aadLen);
 381     if (rv) {
 382       freeContext(context);
 383       return 0L;
 384     }
 385   }
 386   return (jlong)context;
 387 }
 388 
 389 /*
 390  * Class:     com_oracle_security_ucrypto_NativeCipher
 391  * Method:    nativeUpdate
 392  * Signature: (JZ[BII[BI)I
 393  */
 394 jint JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeUpdate

 395   (jlong pContext, jboolean encrypt, int notUsed, jbyte* bufIn, jint inOfs, jint inLen,
 396    int outCapacity, jbyte* bufOut, jint outOfs) {
 397   crypto_ctx_t *context;
 398   int rv = 0;
 399   int outLen = outCapacity - outOfs; // recalculate the real out length
 400 
 401   context = (crypto_ctx_t *) pContext;
 402   rv = CipherUpdate(context, encrypt, (unsigned char*)bufIn, inOfs, inLen, (unsigned char*)bufOut, outOfs, &outLen);
 403   if (rv) {
 404     freeContext(context);
 405     return -rv; // use negative value to indicate error!
 406   }
 407 
 408   return outLen;
 409 }
 410 
 411 /*
 412  * Class:     com_oracle_security_ucrypto_NativeCipher
 413  * Method:    nativeFinal
 414  * Signature: (JZ[BI)I
 415  */
 416 jint JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeFinal

 417   (jlong pContext, jboolean encrypt, int outLen, jbyte* out, jint outOfs) {
 418   crypto_ctx_t *context;
 419   int rv = 0;
 420   unsigned char* bufOut = (unsigned char*) out;
 421 
 422   context = (crypto_ctx_t *) pContext;
 423   // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
 424   if (bufOut == NULL) {
 425     bufOut = (unsigned char*)(&outLen);
 426     outLen = 0;
 427   }
 428   rv = CipherFinal(context, encrypt, bufOut, outOfs, &outLen);
 429   freeContext(context);
 430   if (rv) {
 431      return -rv; // use negative value to indicate error!
 432   }
 433 
 434   return outLen;
 435 }
 436 


 647     rc = -rv;
 648   } else {
 649     if (outLen > 0) {
 650       (*env)->SetByteArrayRegion(env, out, outOfs, outLen, (jbyte *)bufOut);
 651     }
 652     rc = outLen;
 653   }
 654   free(context);
 655   if (bufOut != (unsigned char *)(&outLen)) {
 656     free(bufOut);
 657   }
 658   return rc;
 659 }
 660 
 661 
 662 /*
 663  * Class:     com_oracle_security_ucrypto_NativeKey
 664  * Method:    nativeFree
 665  * Signature: (JI)V
 666  */
 667 void JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree

 668   (jlong id, jint numOfComponents) {
 669   crypto_object_attribute_t* pKey;
 670   int i;
 671 
 672   pKey = (crypto_object_attribute_t*) id;
 673   for (i = 0; i < numOfComponents; i++) {
 674     free(pKey[i].oa_value);
 675   }
 676   free(pKey);
 677 }
 678 
 679 JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeKey_nativeFree
 680   (JNIEnv *env, jclass jCls, jlong id, jint numOfComponents) {
 681   JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree(id, numOfComponents);
 682 }
 683 
 684 /*
 685  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivate
 686  * Method:    nativeInit
 687  * Signature: ([B[B)J
 688  */
 689 jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivate_nativeInit

 690 (int modLen, jbyte* jMod, int privLen, jbyte* jPriv) {
 691 
 692   unsigned char *mod, *priv;
 693   crypto_object_attribute_t* pKey = NULL;
 694 
 695   pKey = calloc(2, sizeof(crypto_object_attribute_t));
 696   if (pKey == NULL) {
 697     return 0L;
 698   }
 699   mod = priv = NULL;
 700   mod = malloc(modLen);
 701   priv = malloc(privLen);
 702   if (mod == NULL || priv == NULL) {
 703     free(pKey);
 704     free(mod);
 705     free(priv);
 706     return 0L;
 707   } else {
 708     memcpy(mod, jMod, modLen);
 709     memcpy(priv, jPriv, privLen);


 749   pKey[0].oa_type = SUN_CKA_MODULUS;
 750   pKey[0].oa_value = (char*) bufMod;
 751   pKey[0].oa_value_len = (size_t) modLen;
 752   pKey[1].oa_type = SUN_CKA_PRIVATE_EXPONENT;
 753   pKey[1].oa_value = (char*) bufPriv;
 754   pKey[1].oa_value_len = (size_t) privLen;
 755   return (jlong) pKey;
 756 
 757 cleanup:
 758   free(bufMod);
 759   free(bufPriv);
 760 
 761   return 0L;
 762 }
 763 
 764 /*
 765  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivateCrt
 766  * Method:    nativeInit
 767  * Signature: ([B[B[B[B[B[B[B[B)J
 768  */
 769 jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivateCrt_nativeInit

 770 (int modLen, jbyte* jMod, int pubLen, jbyte* jPub, int privLen, jbyte* jPriv,
 771  int pLen, jbyte* jP, int qLen, jbyte* jQ, int expPLen, jbyte* jExpP,
 772  int expQLen, jbyte* jExpQ, int crtCoeffLen, jbyte* jCrtCoeff) {
 773 
 774   unsigned char *mod, *pub, *priv, *p, *q, *expP, *expQ, *crtCoeff;
 775   crypto_object_attribute_t* pKey = NULL;
 776 
 777   pKey = calloc(8, sizeof(crypto_object_attribute_t));
 778   if (pKey == NULL) {
 779     return 0L;
 780   }
 781   mod = pub = priv = p = q = expP = expQ = crtCoeff = NULL;
 782   mod = malloc(modLen);
 783   pub = malloc(pubLen);
 784   priv = malloc(privLen);
 785   p = malloc(pLen);
 786   q = malloc(qLen);
 787   expP = malloc(expPLen);
 788   expQ = malloc(expQLen);
 789   crtCoeff = malloc(crtCoeffLen);


 920 
 921 cleanup:
 922   free(bufMod);
 923   free(bufPub);
 924   free(bufPriv);
 925   free(bufP);
 926   free(bufQ);
 927   free(bufExpP);
 928   free(bufExpQ);
 929   free(bufCrtCoeff);
 930 
 931   return 0L;
 932 }
 933 
 934 /*
 935  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPublic
 936  * Method:    nativeInit
 937  * Signature: ([B[B)J
 938  */
 939 
 940 jlong JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPublic_nativeInit

 941 (int modLen, jbyte* jMod, int pubLen, jbyte* jPub) {
 942   unsigned char *mod, *pub;
 943   crypto_object_attribute_t* pKey = NULL;
 944 
 945   pKey = calloc(2, sizeof(crypto_object_attribute_t));
 946   if (pKey == NULL) {
 947     return 0L;
 948   }
 949   mod = pub = NULL;
 950   mod = malloc(modLen);
 951   pub = malloc(pubLen);
 952   if (mod == NULL || pub == NULL) {
 953     free(pKey);
 954     free(mod);
 955     free(pub);
 956     return 0L;
 957   } else {
 958     memcpy(mod, jMod, modLen);
 959     memcpy(pub, jPub, pubLen);
 960   }


1031   if (sign) {
1032     rv = (*ftab->ucryptoSignInit)(context, mech, pKey, keyLength,
1033                                   NULL, 0);
1034   } else {
1035     rv = (*ftab->ucryptoVerifyInit)(context, mech, pKey, keyLength,
1036                                     NULL, 0);
1037   }
1038   if (J2UC_DEBUG) {
1039     printf("SignatureInit: context=%ld, mech=%d, sign=%d, keyValue=%ld, keyLength=%d\n",
1040            context, mech, sign, pKey, keyLength);
1041     printError("SignatureInit", mech, rv);
1042   }
1043   return rv;
1044 }
1045 
1046 /*
1047  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1048  * Method:    nativeInit
1049  * Signature: (IZJI[B)J
1050  */
1051 jlong JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeInit

1052 (jint mech, jboolean sign, jlong jKey, jint keyLength) {
1053   crypto_ctx_t *context;
1054   int rv;
1055   uchar_t *pKey;
1056 
1057   context = malloc(sizeof(crypto_ctx_t));
1058   if (context != NULL) {
1059     pKey = (uchar_t *) jKey;
1060     rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
1061     if (rv) {
1062       freeContext(context);
1063       return 0L;
1064     }
1065   }
1066   return (jlong)context;
1067 }
1068 
1069 JNIEXPORT jlong JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeInit
1070 (JNIEnv *env, jclass jCls, jint mech, jboolean sign, jlong jKey, jint keyLength) {
1071   crypto_ctx_t *context;


1077     throwOutOfMemoryError(env, NULL);
1078     return 0L;
1079   }
1080 
1081   pKey = (uchar_t *) jKey;
1082   rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
1083   if (rv) {
1084     freeContext(context);
1085     throwUCExceptionUsingRV(env, rv);
1086     return 0L;
1087   }
1088 
1089   return (jlong)context;
1090 }
1091 
1092 /*
1093  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1094  * Method:    nativeUpdate
1095  * Signature: (JZ[BII)I
1096  */
1097 jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII

1098 (jlong pCtxt, jboolean sign, int notUsed, jbyte* jIn, jint jInOfs, jint jInLen) {
1099   crypto_ctx_t *context;
1100   int rv = 0;
1101 
1102   context = (crypto_ctx_t *) pCtxt;
1103   if (J2UC_DEBUG) {
1104     printf("NativeRSASignature.nativeUpdate: context=%ld, sign=%d, jIn=%ld, jInOfs=%d, jInLen=%d\n",
1105            context, sign, jIn, jInOfs, jInLen);
1106   }
1107   if (sign) {
1108     rv = (*ftab->ucryptoSignUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
1109   } else {
1110     rv = (*ftab->ucryptoVerifyUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
1111   }
1112   if (rv) {
1113     freeContext(context);
1114     if (J2UC_DEBUG) printError("NativeRSASignature.nativeUpdate", -1, rv);
1115     return -rv; // use negative value to indicate error!
1116   }
1117 


1125 
1126   bufIn = getBytes(env, jIn, inOfs, inLen);
1127   if ((*env)->ExceptionCheck(env)) {
1128     return -1; // use negative value to indicate error!
1129   }
1130 
1131   if (J2UC_DEBUG) printBytes("Update w/ data: ", (unsigned char*)bufIn, (size_t) inLen);
1132 
1133   rv = JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1134     (pCtxt, sign, inLen, bufIn, 0, inLen);
1135 
1136   free(bufIn);
1137   return rv;
1138 }
1139 
1140 /*
1141  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1142  * Method:    nativeUpdate
1143  * Signature: (JZJI)I
1144  */
1145 jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI

1146 (jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
1147 
1148   return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1149     (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
1150 }
1151 
1152 JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI
1153 (JNIEnv *env, jclass jCls, jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
1154 
1155   return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1156     (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
1157 }
1158 
1159 /*
1160  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1161  * Method:    nativeFinal
1162  * Signature: (JZ[BII)I
1163  */
1164 jint JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeFinal

1165 (jlong pCtxt, jboolean sign, int notUsed, jbyte* bufSig, jint sigOfs, jint jSigLen) {
1166 
1167   crypto_ctx_t *context;
1168   int rv = 0;
1169   size_t sigLength = (size_t) jSigLen;
1170 
1171   context = (crypto_ctx_t *) pCtxt;
1172   if (J2UC_DEBUG) {
1173       printf("NativeRSASignature.nativeFinal: context=%ld, sign=%d, bufSig=%ld, sigOfs=%d, sigLen=%d\n",
1174              context, sign, bufSig, sigOfs, jSigLen);
1175       printBytes("Before: SigBytes ", (unsigned char*) (bufSig + sigOfs), jSigLen);
1176   }
1177   if (sign) {
1178     rv = (*ftab->ucryptoSignFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
1179   } else {
1180     rv = (*ftab->ucryptoVerifyFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
1181   }
1182 
1183   freeContext(context);
1184   if (rv) {


1219 
1220     if (rv == 0 && sign) {
1221       // need to copy the generated signature bytes to the java bytearray
1222       (*env)->SetByteArrayRegion(env, jSig, jSigOfs, jSigLen, (jbyte *)bufSig);
1223     }
1224   } else {
1225     // set rv to negative to indicate error
1226     rv = -1;
1227   }
1228 
1229   free(bufSig);
1230 
1231   return rv;
1232 }
1233 
1234 /*
1235  * Class:     com_oracle_security_ucrypto_NativeRSACipher
1236  * Method:    nativeAtomic
1237  * Signature: (IZJI[BI[BII)I
1238  */
1239 jint JavaCritical_com_oracle_security_ucrypto_NativeRSACipher_nativeAtomic

1240   (jint mech, jboolean encrypt, jlong keyValue, jint keyLength,
1241    int notUsed1, jbyte* bufIn, jint jInLen,
1242    int notUsed2, jbyte* bufOut, jint jOutOfs, jint jOutLen) {
1243 
1244   uchar_t *pKey;
1245   crypto_object_attribute_t* pKey2;
1246   int rv = 0;
1247   size_t outLength = (size_t) jOutLen;
1248 
1249   pKey = (uchar_t *) keyValue;
1250   if (J2UC_DEBUG) {
1251     printf("NativeRSACipher.nativeAtomic: mech=%d, encrypt=%d, pKey=%ld, keyLength=%d\n",
1252            mech, encrypt, pKey, keyLength);
1253     printBytes("Before: in  = ", (unsigned char*) bufIn, jInLen);
1254     printBytes("Before: out = ", (unsigned char*) (bufOut + jOutOfs), jOutLen);
1255   }
1256 
1257   if (encrypt) {
1258     rv = (*ftab->ucryptoEncrypt)((ucrypto_mech_t)mech, pKey, (size_t)keyLength,
1259       NULL, 0, (uchar_t *)bufIn, (size_t)jInLen,


   1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 292       if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
 293     } else {
 294       if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
 295       *outLen = (int)outLength;
 296     }
 297   } else {
 298     rv = (*ftab->ucryptoDecryptFinal)(context, (unsigned char*)(bufOut+outOfs), &outLength);
 299     if (rv) {
 300       if (J2UC_DEBUG) printError("ucryptoDecryptFinal", -1, rv);
 301     } else {
 302       if (J2UC_DEBUG) printBytes("BufOut=", (unsigned char*)(bufOut+outOfs), outLength);
 303       *outLen = (int)outLength;
 304     }
 305   }
 306   return rv;
 307 }
 308 
 309 ////////////////////////////////////////////////////////
 310 // SPECIAL ENTRIES FOR JVM JNI-BYPASSING OPTIMIZATION
 311 ////////////////////////////////////////////////////////
 312 JNIEXPORT jlong JNICALL
 313 JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeInit(jint mech) {
 314   crypto_ctx_t *context = NULL;
 315   int rv;
 316 
 317   context = malloc(sizeof(crypto_ctx_t));
 318   if (context != NULL) {
 319     rv = (*ftab->ucryptoDigestInit)(context, (ucrypto_mech_t) mech, NULL, 0);
 320     if (rv) {
 321       freeContext(context);
 322       if (J2UC_DEBUG) printError("ucryptoDigestInit", mech, rv);
 323       return 0L;
 324     }
 325   }
 326   return (jlong) context;
 327 }
 328 
 329 JNIEXPORT jint JNICALL
 330 JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeUpdate
 331   (jint mech, jlong pContext, int notUsed, unsigned char* in, jint ofs, jint len) {
 332   crypto_ctx_t *context;
 333   jint rv = 0;
 334 
 335   context = (crypto_ctx_t *) pContext;
 336   rv = (*ftab->ucryptoDigestUpdate)(context, (const unsigned char*)(in + ofs),
 337                                     (size_t) len);
 338 
 339   if (rv) {
 340     freeContext(context);
 341     if (J2UC_DEBUG) printError("ucryptoDigestUpdate", mech, rv);
 342   }
 343 
 344   return -rv; // use negative value to indicate error
 345 }
 346 
 347 JNIEXPORT jint JNICALL
 348 JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeDigest
 349   (jint mech, jlong pContext, int notUsed, unsigned char* out, jint ofs, jint digestLen) {
 350   crypto_ctx_t *context;
 351   jint rv = 0;
 352   size_t digest_len = digestLen;
 353 
 354   context = (crypto_ctx_t *) pContext;
 355   rv = (*ftab->ucryptoDigestFinal)(context, (unsigned char*)(out + ofs),
 356                                    &digest_len);
 357   if (rv) {
 358     freeContext(context);
 359     if (J2UC_DEBUG) printError("ucryptoDigestFinal", mech, rv);
 360   }
 361 
 362   return -rv; // use negative value to indicate error
 363 }
 364 
 365 JNIEXPORT void JNICALL
 366 JavaCritical_com_oracle_security_ucrypto_NativeDigest_nativeFree
 367   (jint mech, jlong pContext) {
 368   crypto_ctx_t *context;
 369 
 370   context = (crypto_ctx_t *) pContext;
 371   freeContext(context);
 372 }
 373 
 374 // AES
 375 JNIEXPORT jlong JNICALL
 376 JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeInit
 377   (jint mech, jboolean encrypt, int keyLen, unsigned char* bufKey,
 378    int ivLen, unsigned char* bufIv, jint tagLen, int aadLen, unsigned char* bufAad) {
 379   crypto_ctx_t *context = NULL;
 380   int rv;
 381 
 382   context = malloc(sizeof(crypto_ctx_t));
 383   if (context != NULL) {
 384     rv = CipherInit(context, encrypt, (ucrypto_mech_t) mech, bufKey, keyLen,
 385                     bufIv, ivLen, tagLen, bufAad, aadLen);
 386     if (rv) {
 387       freeContext(context);
 388       return 0L;
 389     }
 390   }
 391   return (jlong)context;
 392 }
 393 
 394 /*
 395  * Class:     com_oracle_security_ucrypto_NativeCipher
 396  * Method:    nativeUpdate
 397  * Signature: (JZ[BII[BI)I
 398  */
 399 JNIEXPORT jint JNICALL
 400 JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeUpdate
 401   (jlong pContext, jboolean encrypt, int notUsed, jbyte* bufIn, jint inOfs, jint inLen,
 402    int outCapacity, jbyte* bufOut, jint outOfs) {
 403   crypto_ctx_t *context;
 404   int rv = 0;
 405   int outLen = outCapacity - outOfs; // recalculate the real out length
 406 
 407   context = (crypto_ctx_t *) pContext;
 408   rv = CipherUpdate(context, encrypt, (unsigned char*)bufIn, inOfs, inLen, (unsigned char*)bufOut, outOfs, &outLen);
 409   if (rv) {
 410     freeContext(context);
 411     return -rv; // use negative value to indicate error!
 412   }
 413 
 414   return outLen;
 415 }
 416 
 417 /*
 418  * Class:     com_oracle_security_ucrypto_NativeCipher
 419  * Method:    nativeFinal
 420  * Signature: (JZ[BI)I
 421  */
 422 JNIEXPORT jint JNICALL
 423 JavaCritical_com_oracle_security_ucrypto_NativeCipher_nativeFinal
 424   (jlong pContext, jboolean encrypt, int outLen, jbyte* out, jint outOfs) {
 425   crypto_ctx_t *context;
 426   int rv = 0;
 427   unsigned char* bufOut = (unsigned char*) out;
 428 
 429   context = (crypto_ctx_t *) pContext;
 430   // Avoid null output buffer to workaround Solaris bug21481818 (fixed in S12)
 431   if (bufOut == NULL) {
 432     bufOut = (unsigned char*)(&outLen);
 433     outLen = 0;
 434   }
 435   rv = CipherFinal(context, encrypt, bufOut, outOfs, &outLen);
 436   freeContext(context);
 437   if (rv) {
 438      return -rv; // use negative value to indicate error!
 439   }
 440 
 441   return outLen;
 442 }
 443 


 654     rc = -rv;
 655   } else {
 656     if (outLen > 0) {
 657       (*env)->SetByteArrayRegion(env, out, outOfs, outLen, (jbyte *)bufOut);
 658     }
 659     rc = outLen;
 660   }
 661   free(context);
 662   if (bufOut != (unsigned char *)(&outLen)) {
 663     free(bufOut);
 664   }
 665   return rc;
 666 }
 667 
 668 
 669 /*
 670  * Class:     com_oracle_security_ucrypto_NativeKey
 671  * Method:    nativeFree
 672  * Signature: (JI)V
 673  */
 674 JNIEXPORT void JNICALL
 675 JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree
 676   (jlong id, jint numOfComponents) {
 677   crypto_object_attribute_t* pKey;
 678   int i;
 679 
 680   pKey = (crypto_object_attribute_t*) id;
 681   for (i = 0; i < numOfComponents; i++) {
 682     free(pKey[i].oa_value);
 683   }
 684   free(pKey);
 685 }
 686 
 687 JNIEXPORT void JNICALL Java_com_oracle_security_ucrypto_NativeKey_nativeFree
 688   (JNIEnv *env, jclass jCls, jlong id, jint numOfComponents) {
 689   JavaCritical_com_oracle_security_ucrypto_NativeKey_nativeFree(id, numOfComponents);
 690 }
 691 
 692 /*
 693  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivate
 694  * Method:    nativeInit
 695  * Signature: ([B[B)J
 696  */
 697 JNIEXPORT jlong JNICALL
 698 JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivate_nativeInit
 699 (int modLen, jbyte* jMod, int privLen, jbyte* jPriv) {
 700 
 701   unsigned char *mod, *priv;
 702   crypto_object_attribute_t* pKey = NULL;
 703 
 704   pKey = calloc(2, sizeof(crypto_object_attribute_t));
 705   if (pKey == NULL) {
 706     return 0L;
 707   }
 708   mod = priv = NULL;
 709   mod = malloc(modLen);
 710   priv = malloc(privLen);
 711   if (mod == NULL || priv == NULL) {
 712     free(pKey);
 713     free(mod);
 714     free(priv);
 715     return 0L;
 716   } else {
 717     memcpy(mod, jMod, modLen);
 718     memcpy(priv, jPriv, privLen);


 758   pKey[0].oa_type = SUN_CKA_MODULUS;
 759   pKey[0].oa_value = (char*) bufMod;
 760   pKey[0].oa_value_len = (size_t) modLen;
 761   pKey[1].oa_type = SUN_CKA_PRIVATE_EXPONENT;
 762   pKey[1].oa_value = (char*) bufPriv;
 763   pKey[1].oa_value_len = (size_t) privLen;
 764   return (jlong) pKey;
 765 
 766 cleanup:
 767   free(bufMod);
 768   free(bufPriv);
 769 
 770   return 0L;
 771 }
 772 
 773 /*
 774  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPrivateCrt
 775  * Method:    nativeInit
 776  * Signature: ([B[B[B[B[B[B[B[B)J
 777  */
 778 JNIEXPORT jlong JNICALL
 779 JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPrivateCrt_nativeInit
 780 (int modLen, jbyte* jMod, int pubLen, jbyte* jPub, int privLen, jbyte* jPriv,
 781  int pLen, jbyte* jP, int qLen, jbyte* jQ, int expPLen, jbyte* jExpP,
 782  int expQLen, jbyte* jExpQ, int crtCoeffLen, jbyte* jCrtCoeff) {
 783 
 784   unsigned char *mod, *pub, *priv, *p, *q, *expP, *expQ, *crtCoeff;
 785   crypto_object_attribute_t* pKey = NULL;
 786 
 787   pKey = calloc(8, sizeof(crypto_object_attribute_t));
 788   if (pKey == NULL) {
 789     return 0L;
 790   }
 791   mod = pub = priv = p = q = expP = expQ = crtCoeff = NULL;
 792   mod = malloc(modLen);
 793   pub = malloc(pubLen);
 794   priv = malloc(privLen);
 795   p = malloc(pLen);
 796   q = malloc(qLen);
 797   expP = malloc(expPLen);
 798   expQ = malloc(expQLen);
 799   crtCoeff = malloc(crtCoeffLen);


 930 
 931 cleanup:
 932   free(bufMod);
 933   free(bufPub);
 934   free(bufPriv);
 935   free(bufP);
 936   free(bufQ);
 937   free(bufExpP);
 938   free(bufExpQ);
 939   free(bufCrtCoeff);
 940 
 941   return 0L;
 942 }
 943 
 944 /*
 945  * Class:     com_oracle_security_ucrypto_NativeKey_RSAPublic
 946  * Method:    nativeInit
 947  * Signature: ([B[B)J
 948  */
 949 
 950 JNIEXPORT jlong JNICALL
 951 JavaCritical_com_oracle_security_ucrypto_NativeKey_00024RSAPublic_nativeInit
 952 (int modLen, jbyte* jMod, int pubLen, jbyte* jPub) {
 953   unsigned char *mod, *pub;
 954   crypto_object_attribute_t* pKey = NULL;
 955 
 956   pKey = calloc(2, sizeof(crypto_object_attribute_t));
 957   if (pKey == NULL) {
 958     return 0L;
 959   }
 960   mod = pub = NULL;
 961   mod = malloc(modLen);
 962   pub = malloc(pubLen);
 963   if (mod == NULL || pub == NULL) {
 964     free(pKey);
 965     free(mod);
 966     free(pub);
 967     return 0L;
 968   } else {
 969     memcpy(mod, jMod, modLen);
 970     memcpy(pub, jPub, pubLen);
 971   }


1042   if (sign) {
1043     rv = (*ftab->ucryptoSignInit)(context, mech, pKey, keyLength,
1044                                   NULL, 0);
1045   } else {
1046     rv = (*ftab->ucryptoVerifyInit)(context, mech, pKey, keyLength,
1047                                     NULL, 0);
1048   }
1049   if (J2UC_DEBUG) {
1050     printf("SignatureInit: context=%ld, mech=%d, sign=%d, keyValue=%ld, keyLength=%d\n",
1051            context, mech, sign, pKey, keyLength);
1052     printError("SignatureInit", mech, rv);
1053   }
1054   return rv;
1055 }
1056 
1057 /*
1058  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1059  * Method:    nativeInit
1060  * Signature: (IZJI[B)J
1061  */
1062 JNIEXPORT jlong JNICALL
1063 JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeInit
1064 (jint mech, jboolean sign, jlong jKey, jint keyLength) {
1065   crypto_ctx_t *context;
1066   int rv;
1067   uchar_t *pKey;
1068 
1069   context = malloc(sizeof(crypto_ctx_t));
1070   if (context != NULL) {
1071     pKey = (uchar_t *) jKey;
1072     rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
1073     if (rv) {
1074       freeContext(context);
1075       return 0L;
1076     }
1077   }
1078   return (jlong)context;
1079 }
1080 
1081 JNIEXPORT jlong JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeInit
1082 (JNIEnv *env, jclass jCls, jint mech, jboolean sign, jlong jKey, jint keyLength) {
1083   crypto_ctx_t *context;


1089     throwOutOfMemoryError(env, NULL);
1090     return 0L;
1091   }
1092 
1093   pKey = (uchar_t *) jKey;
1094   rv = SignatureInit(context, mech, sign, pKey, (size_t)keyLength);
1095   if (rv) {
1096     freeContext(context);
1097     throwUCExceptionUsingRV(env, rv);
1098     return 0L;
1099   }
1100 
1101   return (jlong)context;
1102 }
1103 
1104 /*
1105  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1106  * Method:    nativeUpdate
1107  * Signature: (JZ[BII)I
1108  */
1109 JNIEXPORT jint JNICALL
1110 JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1111 (jlong pCtxt, jboolean sign, int notUsed, jbyte* jIn, jint jInOfs, jint jInLen) {
1112   crypto_ctx_t *context;
1113   int rv = 0;
1114 
1115   context = (crypto_ctx_t *) pCtxt;
1116   if (J2UC_DEBUG) {
1117     printf("NativeRSASignature.nativeUpdate: context=%ld, sign=%d, jIn=%ld, jInOfs=%d, jInLen=%d\n",
1118            context, sign, jIn, jInOfs, jInLen);
1119   }
1120   if (sign) {
1121     rv = (*ftab->ucryptoSignUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
1122   } else {
1123     rv = (*ftab->ucryptoVerifyUpdate)(context, (uchar_t *) (jIn + jInOfs), (size_t) jInLen);
1124   }
1125   if (rv) {
1126     freeContext(context);
1127     if (J2UC_DEBUG) printError("NativeRSASignature.nativeUpdate", -1, rv);
1128     return -rv; // use negative value to indicate error!
1129   }
1130 


1138 
1139   bufIn = getBytes(env, jIn, inOfs, inLen);
1140   if ((*env)->ExceptionCheck(env)) {
1141     return -1; // use negative value to indicate error!
1142   }
1143 
1144   if (J2UC_DEBUG) printBytes("Update w/ data: ", (unsigned char*)bufIn, (size_t) inLen);
1145 
1146   rv = JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1147     (pCtxt, sign, inLen, bufIn, 0, inLen);
1148 
1149   free(bufIn);
1150   return rv;
1151 }
1152 
1153 /*
1154  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1155  * Method:    nativeUpdate
1156  * Signature: (JZJI)I
1157  */
1158 JNIEXPORT jint JNICALL
1159 JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI
1160 (jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
1161 
1162   return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1163     (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
1164 }
1165 
1166 JNIEXPORT jint JNICALL Java_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZJI
1167 (JNIEnv *env, jclass jCls, jlong pCtxt, jboolean sign, jlong inAddr, jint inLen) {
1168 
1169   return JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeUpdate__JZ_3BII
1170     (pCtxt, sign, inLen, (jbyte*)inAddr, 0, inLen);
1171 }
1172 
1173 /*
1174  * Class:     com_oracle_security_ucrypto_NativeRSASignature
1175  * Method:    nativeFinal
1176  * Signature: (JZ[BII)I
1177  */
1178 JNIEXPORT jint JNICALL
1179 JavaCritical_com_oracle_security_ucrypto_NativeRSASignature_nativeFinal
1180 (jlong pCtxt, jboolean sign, int notUsed, jbyte* bufSig, jint sigOfs, jint jSigLen) {
1181 
1182   crypto_ctx_t *context;
1183   int rv = 0;
1184   size_t sigLength = (size_t) jSigLen;
1185 
1186   context = (crypto_ctx_t *) pCtxt;
1187   if (J2UC_DEBUG) {
1188       printf("NativeRSASignature.nativeFinal: context=%ld, sign=%d, bufSig=%ld, sigOfs=%d, sigLen=%d\n",
1189              context, sign, bufSig, sigOfs, jSigLen);
1190       printBytes("Before: SigBytes ", (unsigned char*) (bufSig + sigOfs), jSigLen);
1191   }
1192   if (sign) {
1193     rv = (*ftab->ucryptoSignFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
1194   } else {
1195     rv = (*ftab->ucryptoVerifyFinal)(context, (uchar_t *) (bufSig + sigOfs), &sigLength);
1196   }
1197 
1198   freeContext(context);
1199   if (rv) {


1234 
1235     if (rv == 0 && sign) {
1236       // need to copy the generated signature bytes to the java bytearray
1237       (*env)->SetByteArrayRegion(env, jSig, jSigOfs, jSigLen, (jbyte *)bufSig);
1238     }
1239   } else {
1240     // set rv to negative to indicate error
1241     rv = -1;
1242   }
1243 
1244   free(bufSig);
1245 
1246   return rv;
1247 }
1248 
1249 /*
1250  * Class:     com_oracle_security_ucrypto_NativeRSACipher
1251  * Method:    nativeAtomic
1252  * Signature: (IZJI[BI[BII)I
1253  */
1254 JNIEXPORT jint JNICALL
1255 JavaCritical_com_oracle_security_ucrypto_NativeRSACipher_nativeAtomic
1256   (jint mech, jboolean encrypt, jlong keyValue, jint keyLength,
1257    int notUsed1, jbyte* bufIn, jint jInLen,
1258    int notUsed2, jbyte* bufOut, jint jOutOfs, jint jOutLen) {
1259 
1260   uchar_t *pKey;
1261   crypto_object_attribute_t* pKey2;
1262   int rv = 0;
1263   size_t outLength = (size_t) jOutLen;
1264 
1265   pKey = (uchar_t *) keyValue;
1266   if (J2UC_DEBUG) {
1267     printf("NativeRSACipher.nativeAtomic: mech=%d, encrypt=%d, pKey=%ld, keyLength=%d\n",
1268            mech, encrypt, pKey, keyLength);
1269     printBytes("Before: in  = ", (unsigned char*) bufIn, jInLen);
1270     printBytes("Before: out = ", (unsigned char*) (bufOut + jOutOfs), jOutLen);
1271   }
1272 
1273   if (encrypt) {
1274     rv = (*ftab->ucryptoEncrypt)((ucrypto_mech_t)mech, pKey, (size_t)keyLength,
1275       NULL, 0, (uchar_t *)bufIn, (size_t)jInLen,


< prev index next >