Hi ppommarel,
Thank you for your questions and interest in wolfSSL. Sorry for the delay.
I have coded up an example here that will perform the same steps as openssl sign op (Not sure if you are doing signatures or otherwise) but this example shows the flow and how to initialize the RNG for calling wc_PrivateKeyDecode.
Let us know if you continue to have any issues and for much faster response time please shoot an email to support@wolfssl.com anytime.
/* To compile:
*
* gcc -Wall main.c -lwolfssl
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/coding.h>
#include <wolfssl/wolfcrypt/sha256.h>
static void err_sys(const char* msg, int ret)
{
if (ret) {
printf("ERROR: %s, ret = %d\n", msg, ret);
} else {
printf("ERROR: %s\n", msg);
}
exit(EXIT_FAILURE);
}
int main(void)
{
int ret;
int derKeySz;
byte derKey[4096];
FILE* file;
RNG rng;
RsaKey privKey;
word32 idx = 0;
Sha256 sha;
byte hash[SHA256_DIGEST_SIZE];
byte sig[1024];
int inSz;
byte in[1024];
int encodedSz;
byte encoded[1024];
byte base64[1024];
word32 base64Len = sizeof(base64);
file = fopen("./input.txt", "rb");
if (!file)
err_sys("failed to open input.txt", 0);
inSz = fread(in, 1, sizeof(in), file);
fclose(file);
/* init RNG */
ret = wc_InitRng(&rng);
if (ret != 0)
err_sys("wc_InitRng failed", ret);
/* import DER-encoded private key into RsaKey structure */
file = fopen("./key.der", "rb");
if (!file)
err_sys("can't open key file", 0);
derKeySz = fread(derKey, 1, sizeof(derKey), file);
fclose(file);
wc_InitRsaKey(&privKey, 0);
ret = wc_RsaPrivateKeyDecode(derKey, &idx, &privKey, derKeySz);
if (ret != 0)
err_sys("wc_RsaPrivateKeyDecode failed", ret);
/* hash data */
wc_InitSha256(&sha);
wc_Sha256Update(&sha, in, inSz);
wc_Sha256Final(&sha, hash);
/* write hash to "hash-wolfssl" file */
file = fopen("./hash-wolfssl", "wb");
if (!file)
err_sys("failed to open hash-wolfssl file", 0);
ret = (int)fwrite(hash, 1, SHA256_DIGEST_SIZE, file);
fclose(file);
/* Encode the signature before signing */
encodedSz = wc_EncodeSignature(encoded, hash, SHA256_DIGEST_SIZE, SHA256h);
if (ret < 0)
err_sys("failed to encode signature", ret);
/* sign hash */
ret = wc_RsaSSL_Sign(encoded, encodedSz, sig, sizeof(sig), &privKey,
&rng);
if (ret < 0)
err_sys("wc_RsaSSL_Sign failed", ret);
ret = Base64_Encode(sig, ret, base64, &base64Len);
if (ret != 0)
printf("ERROR %d\n", ret);
/* write signature to "signature-wolfssl" file */
file = fopen("./signature-wolfssl", "wb");
if (!file)
err_sys("failed to open signature-wolfssl file", 0);
ret = (int)fwrite(base64, 1, base64Len, file);
fclose(file);
return 0;
}
Warm Regards,
Kaleb
Post's attachmentsfiles-for-example.zip 2.4 kb, file has never been downloaded.
You don't have the permssions to download the attachments of this post.