Пример #1
0
JNIEXPORT jlong JNICALL Java_com_intel_diceros_crypto_engines_AESOpensslEngine_initWorkingKey(
    JNIEnv *env, jobject object, jbyteArray key, jboolean forEncryption,
    jint mode, jint padding, jbyteArray IV, jlong cipherContext) {
  CipherContext* cipherCtx = (CipherContext*) cipherContext;
  cipherCtx = preInitContext(env, cipherCtx, mode, key, IV);

  (*env)->GetByteArrayRegion(env, key, 0, cipherCtx->keyLength, cipherCtx->key);
  (*env)->GetByteArrayRegion(env, IV, 0, cipherCtx->ivLength, cipherCtx->iv);

  cryptInit cryptInitFunc = getCryptInitFunc(forEncryption);
  EVP_CIPHER* cipher = getCipher(mode, cipherCtx->keyLength);
  if (cipher != NULL) {
    cryptInitFunc(cipherCtx->opensslCtx, cipher, NULL,
        (unsigned char *) cipherCtx->key, (unsigned char *) cipherCtx->iv);
  } else {
    THROW(env, "java/lang/IllegalArgumentException", "unsupportted mode or key size");
  }

  if (padding == PADDING_NOPADDING) {
    EVP_CIPHER_CTX_set_padding(cipherCtx->opensslCtx, 0);
  } else if (padding == PADDING_PKCS5PADDING) {
    EVP_CIPHER_CTX_set_padding(cipherCtx->opensslCtx, 1);
  }

  return (long) cipherCtx;
}
Пример #2
0
JNIEXPORT jlong JNICALL Java_com_intel_diceros_crypto_engines_AESOpensslEngine_initWorkingKey(
		JNIEnv *env, jobject object, jbyteArray key, jboolean forEncryption,
		jint mode, jint padding, jbyteArray IV, jlong aesContext) {
	AESContext* aesCtx = (AESContext*) aesContext;
	aesCtx = preInitContext(env, aesCtx, mode, key, IV);

	(*env)->GetByteArrayRegion(env, key, 0, aesCtx->keyLength, aesCtx->nativeKey);
	(*env)->GetByteArrayRegion(env, IV, 0, aesCtx->ivLength, aesCtx->nativeIv);

	cryptInit cryptInitFunc = getCryptInitFunc(forEncryption);

	if (mode == MODE_CTR) {
		if (aesCtx->keyLength == 32) {
			cryptInitFunc(aesCtx->context, EVP_aes_256_ctr(), NULL,
					(unsigned char *) aesCtx->nativeKey, (unsigned char *) aesCtx->nativeIv);
		} else {
			cryptInitFunc(aesCtx->context, EVP_aes_128_ctr(), NULL,
					(unsigned char *) aesCtx->nativeKey, (unsigned char *) aesCtx->nativeIv);
		}
	} else if (mode == MODE_CBC) {
		if (aesCtx->keyLength == 32) {
			cryptInitFunc(aesCtx->context, EVP_aes_256_cbc(), NULL,
					(unsigned char *) aesCtx->nativeKey, (unsigned char *) aesCtx->nativeIv);
		} else {
			cryptInitFunc(aesCtx->context, EVP_aes_128_cbc(), NULL,
					(unsigned char *) aesCtx->nativeKey, (unsigned char *) aesCtx->nativeIv);
		}
	}

	if (padding == PADDING_NOPADDING) {
		EVP_CIPHER_CTX_set_padding(aesCtx->context, 0);
	} else if (padding == PADDING_PKCS5PADDING) {
		EVP_CIPHER_CTX_set_padding(aesCtx->context, 1);
	}

	return (long) aesCtx;
}