int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen, RsaKey* key) { int plainLen; byte* tmp; byte* pad = 0; tmp = (byte*)XMALLOC(inLen, key->heap, DYNAMIC_TYPE_RSA); if (tmp == NULL) { return MEMORY_E; } XMEMCPY(tmp, in, inLen); if ( (plainLen = wc_RsaSSL_VerifyInline(tmp, inLen, &pad, key) ) < 0) { XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); return plainLen; } if (plainLen > (int)outLen) plainLen = BAD_FUNC_ARG; else XMEMCPY(out, pad, plainLen); ForceZero(tmp, inLen); XFREE(tmp, key->heap, DYNAMIC_TYPE_RSA); return plainLen; }
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; }