move from polarssl to mbedtls (#708)

* update polarssl to mbedtls
* fix a warning in armsrc/iso15693
* added random generator and ecdsa test
* added signature check to test
* move crypto lib to client directory
This commit is contained in:
Oleg Moiseenko 2018-11-19 10:02:38 +02:00 committed by pwpiwi
commit 700d868794
112 changed files with 54624 additions and 8485 deletions

View file

@ -24,13 +24,14 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rsa.h"
#include "sha1.h"
#include "mbedtls/rsa.h"
#include "mbedtls/sha1.h"
struct crypto_hash_polarssl {
struct crypto_hash ch;
sha1_context ctx;
mbedtls_sha1_context ctx;
};
static void crypto_hash_polarssl_close(struct crypto_hash *_ch)
@ -44,7 +45,7 @@ static void crypto_hash_polarssl_write(struct crypto_hash *_ch, const unsigned c
{
struct crypto_hash_polarssl *ch = (struct crypto_hash_polarssl *)_ch;
sha1_update(&(ch->ctx), buf, len);
mbedtls_sha1_update(&(ch->ctx), buf, len);
}
static unsigned char *crypto_hash_polarssl_read(struct crypto_hash *_ch)
@ -52,7 +53,7 @@ static unsigned char *crypto_hash_polarssl_read(struct crypto_hash *_ch)
struct crypto_hash_polarssl *ch = (struct crypto_hash_polarssl *)_ch;
static unsigned char sha1sum[20];
sha1_finish(&(ch->ctx), sha1sum);
mbedtls_sha1_finish(&(ch->ctx), sha1sum);
return sha1sum;
}
@ -71,7 +72,7 @@ static struct crypto_hash *crypto_hash_polarssl_open(enum crypto_algo_hash hash)
struct crypto_hash_polarssl *ch = malloc(sizeof(*ch));
sha1_starts(&(ch->ctx));
mbedtls_sha1_starts(&(ch->ctx));
ch->ch.write = crypto_hash_polarssl_write;
ch->ch.read = crypto_hash_polarssl_read;
@ -83,7 +84,7 @@ static struct crypto_hash *crypto_hash_polarssl_open(enum crypto_algo_hash hash)
struct crypto_pk_polarssl {
struct crypto_pk cp;
rsa_context ctx;
mbedtls_rsa_context ctx;
};
static struct crypto_pk *crypto_pk_polarssl_open_rsa(va_list vl)
@ -96,13 +97,13 @@ static struct crypto_pk *crypto_pk_polarssl_open_rsa(va_list vl)
char *exp = va_arg(vl, char *); // E
int explen = va_arg(vl, size_t);
rsa_init(&cp->ctx, RSA_PKCS_V15, 0);
mbedtls_rsa_init(&cp->ctx, MBEDTLS_RSA_PKCS_V15, 0);
cp->ctx.len = modlen; // size(N) in bytes
mpi_read_binary(&cp->ctx.N, (const unsigned char *)mod, modlen);
mpi_read_binary(&cp->ctx.E, (const unsigned char *)exp, explen);
mbedtls_mpi_read_binary(&cp->ctx.N, (const unsigned char *)mod, modlen);
mbedtls_mpi_read_binary(&cp->ctx.E, (const unsigned char *)exp, explen);
int res = rsa_check_pubkey(&cp->ctx);
int res = mbedtls_rsa_check_pubkey(&cp->ctx);
if(res != 0) {
fprintf(stderr, "PolarSSL public key error res=%x exp=%d mod=%d.\n", res * -1, explen, modlen);
free(cp);
@ -134,20 +135,20 @@ static struct crypto_pk *crypto_pk_polarssl_open_priv_rsa(va_list vl)
// char *inv = va_arg(vl, char *);
// int invlen = va_arg(vl, size_t);
rsa_init(&cp->ctx, RSA_PKCS_V15, 0);
mbedtls_rsa_init(&cp->ctx, MBEDTLS_RSA_PKCS_V15, 0);
cp->ctx.len = modlen; // size(N) in bytes
mpi_read_binary(&cp->ctx.N, (const unsigned char *)mod, modlen);
mpi_read_binary(&cp->ctx.E, (const unsigned char *)exp, explen);
mbedtls_mpi_read_binary(&cp->ctx.N, (const unsigned char *)mod, modlen);
mbedtls_mpi_read_binary(&cp->ctx.E, (const unsigned char *)exp, explen);
mpi_read_binary(&cp->ctx.D, (const unsigned char *)d, dlen);
mpi_read_binary(&cp->ctx.P, (const unsigned char *)p, plen);
mpi_read_binary(&cp->ctx.Q, (const unsigned char *)q, qlen);
mpi_read_binary(&cp->ctx.DP, (const unsigned char *)dp, dplen);
mpi_read_binary(&cp->ctx.DQ, (const unsigned char *)dq, dqlen);
mpi_inv_mod(&cp->ctx.QP, &cp->ctx.Q, &cp->ctx.P);
mbedtls_mpi_read_binary(&cp->ctx.D, (const unsigned char *)d, dlen);
mbedtls_mpi_read_binary(&cp->ctx.P, (const unsigned char *)p, plen);
mbedtls_mpi_read_binary(&cp->ctx.Q, (const unsigned char *)q, qlen);
mbedtls_mpi_read_binary(&cp->ctx.DP, (const unsigned char *)dp, dplen);
mbedtls_mpi_read_binary(&cp->ctx.DQ, (const unsigned char *)dq, dqlen);
mbedtls_mpi_inv_mod(&cp->ctx.QP, &cp->ctx.Q, &cp->ctx.P);
int res = rsa_check_privkey(&cp->ctx);
int res = mbedtls_rsa_check_privkey(&cp->ctx);
if(res != 0) {
fprintf(stderr, "PolarSSL private key error res=%x exp=%d mod=%d.\n", res * -1, explen, modlen);
free(cp);
@ -182,7 +183,7 @@ static struct crypto_pk *crypto_pk_polarssl_genkey_rsa(va_list vl)
if (transient) {
}
int res = rsa_gen_key(&cp->ctx, &myrand, NULL, nbits, exp);
int res = mbedtls_rsa_gen_key(&cp->ctx, &myrand, NULL, nbits, exp);
if (res) {
fprintf(stderr, "PolarSSL private key generation error res=%x exp=%d nbits=%d.\n", res * -1, exp, nbits);
free(cp);
@ -196,7 +197,7 @@ static void crypto_pk_polarssl_close(struct crypto_pk *_cp)
{
struct crypto_pk_polarssl *cp = (struct crypto_pk_polarssl *)_cp;
rsa_free(&cp->ctx);
mbedtls_rsa_free(&cp->ctx);
free(cp);
}
@ -207,7 +208,7 @@ static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk *_cp, co
unsigned char *result;
*clen = 0;
size_t keylen = mpi_size(&cp->ctx.N);
size_t keylen = mbedtls_mpi_size(&cp->ctx.N);
result = malloc(keylen);
if (!result) {
@ -215,7 +216,7 @@ static unsigned char *crypto_pk_polarssl_encrypt(const struct crypto_pk *_cp, co
return NULL;
}
res = rsa_public(&cp->ctx, buf, result);
res = mbedtls_rsa_public(&cp->ctx, buf, result);
if(res) {
printf("RSA encrypt failed. Error: %x data len: %zd key len: %zd\n", res * -1, len, keylen);
free(result);
@ -234,7 +235,7 @@ static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk *_cp, co
unsigned char *result;
*clen = 0;
size_t keylen = mpi_size(&cp->ctx.N);
size_t keylen = mbedtls_mpi_size(&cp->ctx.N);
result = malloc(keylen);
if (!result) {
@ -242,7 +243,7 @@ static unsigned char *crypto_pk_polarssl_decrypt(const struct crypto_pk *_cp, co
return NULL;
}
res = rsa_private(&cp->ctx, buf, result); // CHECK???
res = mbedtls_rsa_private(&cp->ctx, NULL, NULL, buf, result); // CHECK???
if(res) {
printf("RSA decrypt failed. Error: %x data len: %zd key len: %zd\n", res * -1, len, keylen);
free(result);
@ -269,17 +270,17 @@ static unsigned char *crypto_pk_polarssl_get_parameter(const struct crypto_pk *_
switch(param){
// mod
case 0:
*plen = mpi_size(&cp->ctx.N);
*plen = mbedtls_mpi_size(&cp->ctx.N);
result = malloc(*plen);
memset(result, 0x00, *plen);
mpi_write_binary(&cp->ctx.N, result, *plen);
mbedtls_mpi_write_binary(&cp->ctx.N, result, *plen);
break;
// exp
case 1:
*plen = mpi_size(&cp->ctx.E);
*plen = mbedtls_mpi_size(&cp->ctx.E);
result = malloc(*plen);
memset(result, 0x00, *plen);
mpi_write_binary(&cp->ctx.E, result, *plen);
mbedtls_mpi_write_binary(&cp->ctx.E, result, *plen);
break;
default:
printf("Error get parameter. Param=%d", param);

View file

@ -12,38 +12,70 @@
#include "util.h"
#include "ui.h"
#include "bignum.h"
#include "aes.h"
#include "aes_cmac128.h"
#include "des.h"
#include "rsa.h"
#include "sha1.h"
#include "mbedtls/bignum.h"
#include "mbedtls/aes.h"
#include "mbedtls/cmac.h"
#include "mbedtls/des.h"
#include "mbedtls/ecp.h"
#include "mbedtls/rsa.h"
#include "mbedtls/sha1.h"
#include "mbedtls/md5.h"
#include "mbedtls/x509.h"
#include "mbedtls/base64.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/timing.h"
#include "crypto_test.h"
#include "sda_test.h"
#include "dda_test.h"
#include "cda_test.h"
#include "crypto/libpcrypto.h"
int ExecuteCryptoTests(bool verbose) {
int res;
bool TestFail = false;
res = mpi_self_test(verbose);
res = mbedtls_mpi_self_test(verbose);
if (res) TestFail = true;
res = aes_self_test(verbose);
res = mbedtls_aes_self_test(verbose);
if (res) TestFail = true;
res = aes_cmac_self_test(verbose);
res = mbedtls_des_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_sha1_self_test(verbose);
if (res) TestFail = true;
res = des_self_test(verbose);
res = mbedtls_md5_self_test(verbose);
if (res) TestFail = true;
res = sha1_self_test(verbose);
res = mbedtls_rsa_self_test(verbose);
if (res) TestFail = true;
res = rsa_self_test(verbose);
res = mbedtls_entropy_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_timing_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_ctr_drbg_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_base64_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_cmac_self_test(verbose);
if (res) TestFail = true;
res = ecdsa_nist_test(verbose);
if (res) TestFail = true;
res = mbedtls_ecp_self_test(verbose);
if (res) TestFail = true;
res = mbedtls_x509_self_test(verbose);
if (res) TestFail = true;
res = exec_sda_test(verbose);