#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)
{
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)
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;
}
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;
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)
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);
return NULL;
}
// 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);
-
- 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.N, (const unsigned char *)mod, modlen);
+ mbedtls_mpi_read_binary(&cp->ctx.E, (const unsigned char *)exp, explen);
+
+ 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);
return NULL;
}
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);
return NULL;
}
{
struct crypto_pk_polarssl *cp = (struct crypto_pk_polarssl *)_cp;
- rsa_free(&cp->ctx);
+ mbedtls_rsa_free(&cp->ctx);
free(cp);
}
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) {
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);
return NULL;
}
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) {
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);
return NULL;
}
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);