JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_RSA_doDec (JNIEnv* jenv, jobject jcl, jobject in, jlong inSz, jobject out, jlong outSz, jobject keyDer, jlong keySz) { int ret; RsaKey myKey; unsigned int idx; /* check in and key sz */ if ((inSz < 0) || (keySz < 0) || (outSz < 0)) { return -1; } /* get pointers to our buffers */ unsigned char* inBuf = (*jenv)->GetDirectBufferAddress(jenv, in); if (inBuf == NULL) { printf("problem getting in buffer address\n"); return -1; } unsigned char* outBuf = (*jenv)->GetDirectBufferAddress(jenv, out); if (outBuf == NULL) { printf("problem getting out buffer address\n"); return -1; } unsigned char* keyBuf = (*jenv)->GetDirectBufferAddress(jenv, keyDer); if (keyBuf == NULL) { printf("problem getting key buffer address\n"); return -1; } wc_InitRsaKey(&myKey, NULL); idx = 0; ret = wc_RsaPrivateKeyDecode(keyBuf, &idx, &myKey, (unsigned int)keySz); if (ret == 0) { ret = wc_RsaPrivateDecrypt(inBuf, (unsigned int)inSz, outBuf, (unsigned int)outSz, &myKey); if (ret < 0) { printf("wc_RsaPrivateDecrypt failed, ret = %d\n", ret); return ret; } } else { printf("wc_RsaPrivateKeyDecode failed, ret = %d\n", ret); } wc_FreeRsaKey(&myKey); return ret; }
int rsa_test(void) { byte* tmp = NULL; size_t bytes; RsaKey key; WC_RNG rng; word32 idx = 0; int ret; byte in[] = "Everyone gets Friday off."; word32 inLen = (word32)XSTRLEN((char*)in); byte out[256]; byte plain[256]; byte* outPtr = NULL; tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (tmp == NULL) { ret = MEMORY_E; goto exit; } XMEMCPY(tmp, privkey_der_2048, sizeof(privkey_der_2048)); bytes = sizeof(privkey_der_2048); ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID); if (ret < 0) { goto exit; } ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes); if (ret < 0) { goto exit; } printf("Key Size: %d\n", wc_RsaEncryptSize(&key)); ret = wc_InitRng(&rng); if (ret < 0) { goto exit; } #ifdef WC_RSA_BLINDING ret = wc_RsaSetRNG(&key, &rng); if (ret < 0) { goto exit; } #endif ret = wc_RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng); printf("wc_RsaPublicEncrypt: %d\n", ret); if (ret < 0) { goto exit; } idx = ret; /* save off encrypted length */ ret = wc_RsaPrivateDecrypt(out, idx, plain, sizeof(plain), &key); printf("wc_RsaPrivateDecrypt: %d\n", ret); printf("\n%d", ret); if (ret < 0) { goto exit; } if (XMEMCMP(plain, in, ret)) { printf("Compare failed!\n"); goto exit; } ret = wc_RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng); printf("wc_RsaSSL_Sign: %d\n", ret); if (ret < 0) { goto exit; } idx = ret; XMEMSET(plain, 0, sizeof(plain)); ret = wc_RsaSSL_VerifyInline(out, idx, &outPtr, &key); printf("wc_RsaSSL_Verify: %d\n", ret); if (ret < 0) { goto exit; } if (XMEMCMP(in, outPtr, ret)) { printf("Compare failed!\n"); goto exit; } ret = 0; /* success */ exit: wc_FreeRsaKey(&key); XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); wc_FreeRng(&rng); return ret; }