Files
mars-matrixssl/crypto/test/rsaTest.c
Janne Johansson d0a51a7e43 MatrixSSL 4.0.0
2018-09-13 12:17:26 +03:00

219 lines
9.9 KiB
C

/**
* @file rsaTest.c
* @version $Format:%h%d$
*
* RSA test.
*/
#include <stdio.h>
#include <stdlib.h>
#include "crypto/cryptoApi.h"
#ifdef USE_RSA
#define BUFFER_SIZE 256
int sign_verify_test1(psRsaKey_t *privKey,
psRsaKey_t *pubKey,
unsigned char (*buffer_p)[BUFFER_SIZE],
psSize_t *buffer_size_result_p)
{
/* SHA-256("abc") */
psPool_t *pool = NULL;
unsigned char in[SHA256_HASHLEN] =
{
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40,
0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17,
0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
};
unsigned char tmp[BUFFER_SIZE];
unsigned char *buffer = *buffer_p;
*buffer_size_result_p = BUFFER_SIZE;
if (psRsaEncryptPriv(pool, privKey, in, sizeof(in), buffer, BUFFER_SIZE,
NULL) < 0)
{
printf("Operation failed: Rsa Sign.\n");
return 1;
}
if (psRsaDecryptPub(pool, pubKey, buffer, BUFFER_SIZE, tmp,
SHA256_HASHLEN, NULL) < 0)
{
printf("Operation failed: Rsa VerifyRecover.\n");
return 2;
}
if (memcmp(tmp, in, 32) != 0)
{
printf("RSA sign-verify mismatch.\n");
}
return 0;
}
const static unsigned char RSA2048KEY[] =
{
0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
0xad, 0x6a, 0x33, 0xee, 0xa2, 0x73, 0x6d, 0x68, 0xba, 0xbd, 0x2d, 0xb1,
0xb5, 0x54, 0xf0, 0x04, 0x14, 0xff, 0xbd, 0xd7, 0x5b, 0x4c, 0x69, 0xe7,
0xf4, 0x66, 0x7f,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, 0x9d, 0x7b,
0x5c, 0x78, 0x7f, 0xcb, 0xce, 0x32, 0x57, 0xf7, 0x6a, 0xd2, 0x32, 0x0f,
0x4f, 0x8c, 0xbe, 0xc1, 0xd5, 0xa2, 0xd3, 0xba, 0xe4, 0x2d, 0x14, 0x9e,
0x02, 0x11, 0xce, 0x52, 0x7b, 0xbf, 0x33, 0x42, 0xb4, 0xff, 0xbc, 0x8a,
0x74, 0xcf, 0x47
};
int main(void)
{
psRsaKey_t privkey;
unsigned char buffer[BUFFER_SIZE];
psSize_t sz = 0;
psPool_t *misc = NULL;
int res;
if (psCryptoOpen(PSCRYPTO_CONFIG) < PS_SUCCESS)
{
printf("Failed to initialize library: psCryptoOpen failed\n");
return 4;
}
#if defined(USE_RSA) && defined(USE_PRIVATE_KEY_PARSING)
if (psRsaParsePkcs1PrivKey(misc, (unsigned char *) RSA2048KEY,
(psSize_t) sizeof(RSA2048KEY),
&privkey) < 0)
{
printf("FAILED OPERATION: ParsePriv\n");
return 5;
}
#else
printf("FAILED OPERATION: RSA keys not configured");
#endif
/* private key also includes public key. */
res = sign_verify_test1(&privkey, &privkey, &buffer, &sz);
if (res != 0)
{
printf("Sign-verify test failed: %d!\n", res);
return res;
}
printf("Result: encoded bytes=%d (max=%d)\n",
(int) sz, (int) BUFFER_SIZE);
{
psSize_t i;
/* Result will be something like
XXXXXX [repeats 512 times], where all nibbles appear random.
*/
for(i = 0; i < sz; i++)
{
printf("%02x", buffer[i]);
}
printf("\n");
}
psRsaClearKey(&privkey);
psCryptoClose();
return 0;
}
#else
int main(void)
{
printf("Skipped RSA test: USE_RSA is not enabled.\n");
return 0;
}
#endif /* USE_RSA */