mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 18:48:13 -07:00
changing {} style to match majority of previous style
This commit is contained in:
parent
da6cdf014b
commit
961d929f4d
320 changed files with 5502 additions and 10485 deletions
|
@ -393,8 +393,7 @@ static uint32_t RCON[10];
|
|||
|
||||
static int aes_init_done = 0;
|
||||
|
||||
static void aes_gen_tables(void)
|
||||
{
|
||||
static void aes_gen_tables(void) {
|
||||
int i, x, y, z;
|
||||
int pow[256];
|
||||
int log[256];
|
||||
|
@ -507,13 +506,11 @@ static void aes_gen_tables(void)
|
|||
|
||||
#endif /* MBEDTLS_AES_FEWER_TABLES */
|
||||
|
||||
void mbedtls_aes_init(mbedtls_aes_context *ctx)
|
||||
{
|
||||
void mbedtls_aes_init(mbedtls_aes_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_aes_context));
|
||||
}
|
||||
|
||||
void mbedtls_aes_free(mbedtls_aes_context *ctx)
|
||||
{
|
||||
void mbedtls_aes_free(mbedtls_aes_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -521,14 +518,12 @@ void mbedtls_aes_free(mbedtls_aes_context *ctx)
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx)
|
||||
{
|
||||
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx) {
|
||||
mbedtls_aes_init(&ctx->crypt);
|
||||
mbedtls_aes_init(&ctx->tweak);
|
||||
}
|
||||
|
||||
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
|
||||
{
|
||||
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx) {
|
||||
mbedtls_aes_free(&ctx->crypt);
|
||||
mbedtls_aes_free(&ctx->tweak);
|
||||
}
|
||||
|
@ -539,8 +534,7 @@ void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
|
|||
*/
|
||||
#if !defined(MBEDTLS_AES_SETKEY_ENC_ALT)
|
||||
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
unsigned int i;
|
||||
uint32_t *RK;
|
||||
|
||||
|
@ -653,8 +647,7 @@ int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
|||
*/
|
||||
#if !defined(MBEDTLS_AES_SETKEY_DEC_ALT)
|
||||
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
int i, j, ret;
|
||||
mbedtls_aes_context cty;
|
||||
uint32_t *RK;
|
||||
|
@ -719,8 +712,7 @@ static int mbedtls_aes_xts_decode_keys(const unsigned char *key,
|
|||
const unsigned char **key1,
|
||||
unsigned int *key1bits,
|
||||
const unsigned char **key2,
|
||||
unsigned int *key2bits)
|
||||
{
|
||||
unsigned int *key2bits) {
|
||||
const unsigned int half_keybits = keybits / 2;
|
||||
const unsigned int half_keybytes = half_keybits / 8;
|
||||
|
||||
|
@ -743,8 +735,7 @@ static int mbedtls_aes_xts_decode_keys(const unsigned char *key,
|
|||
|
||||
int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
int ret;
|
||||
const unsigned char *key1, *key2;
|
||||
unsigned int key1bits, key2bits;
|
||||
|
@ -765,8 +756,7 @@ int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
|
|||
|
||||
int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
int ret;
|
||||
const unsigned char *key1, *key2;
|
||||
unsigned int key1bits, key2bits;
|
||||
|
@ -840,8 +830,7 @@ int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
|
|||
#if !defined(MBEDTLS_AES_ENCRYPT_ALT)
|
||||
int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
int i;
|
||||
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
|
||||
|
||||
|
@ -899,8 +888,7 @@ int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
mbedtls_internal_aes_encrypt(ctx, input, output);
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
@ -911,8 +899,7 @@ void mbedtls_aes_encrypt(mbedtls_aes_context *ctx,
|
|||
#if !defined(MBEDTLS_AES_DECRYPT_ALT)
|
||||
int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
int i;
|
||||
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
|
||||
|
||||
|
@ -970,8 +957,7 @@ int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
mbedtls_internal_aes_decrypt(ctx, input, output);
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
|
@ -982,8 +968,7 @@ void mbedtls_aes_decrypt(mbedtls_aes_context *ctx,
|
|||
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
|
||||
if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES))
|
||||
return (mbedtls_aesni_crypt_ecb(ctx, mode, input, output));
|
||||
|
@ -1015,8 +1000,7 @@ int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int i;
|
||||
unsigned char temp[16];
|
||||
|
||||
|
@ -1108,8 +1092,7 @@ typedef unsigned char mbedtls_be128[16];
|
|||
* endian machines.
|
||||
*/
|
||||
static void mbedtls_gf128mul_x_ble(unsigned char r[16],
|
||||
const unsigned char x[16])
|
||||
{
|
||||
const unsigned char x[16]) {
|
||||
uint64_t a, b, ra, rb;
|
||||
|
||||
GET_UINT64_LE(a, x, 0);
|
||||
|
@ -1130,8 +1113,7 @@ int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
|
|||
size_t length,
|
||||
const unsigned char data_unit[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret;
|
||||
size_t blocks = length / 16;
|
||||
size_t leftover = length % 16;
|
||||
|
@ -1231,8 +1213,7 @@ int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
|
|||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
|
||||
|
@ -1271,8 +1252,7 @@ int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
unsigned char c;
|
||||
unsigned char ov[17];
|
||||
|
||||
|
@ -1304,8 +1284,7 @@ int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
|
|||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret = 0;
|
||||
size_t n = *iv_off;
|
||||
|
||||
|
@ -1337,8 +1316,7 @@ int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
|
|||
unsigned char nonce_counter[16],
|
||||
unsigned char stream_block[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
|
||||
|
@ -1757,8 +1735,7 @@ static const unsigned char aes_test_xts_data_unit[][16] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_aes_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_aes_self_test(int verbose) {
|
||||
int ret = 0, i, j, u, mode;
|
||||
unsigned int keybits;
|
||||
unsigned char key[32];
|
||||
|
|
|
@ -50,13 +50,11 @@
|
|||
|
||||
#if !defined(MBEDTLS_ARC4_ALT)
|
||||
|
||||
void mbedtls_arc4_init(mbedtls_arc4_context *ctx)
|
||||
{
|
||||
void mbedtls_arc4_init(mbedtls_arc4_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_arc4_context));
|
||||
}
|
||||
|
||||
void mbedtls_arc4_free(mbedtls_arc4_context *ctx)
|
||||
{
|
||||
void mbedtls_arc4_free(mbedtls_arc4_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -67,8 +65,7 @@ void mbedtls_arc4_free(mbedtls_arc4_context *ctx)
|
|||
* ARC4 key schedule
|
||||
*/
|
||||
void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key,
|
||||
unsigned int keylen)
|
||||
{
|
||||
unsigned int keylen) {
|
||||
int i, j, a;
|
||||
unsigned int k;
|
||||
unsigned char *m;
|
||||
|
@ -96,8 +93,7 @@ void mbedtls_arc4_setup(mbedtls_arc4_context *ctx, const unsigned char *key,
|
|||
* ARC4 cipher function
|
||||
*/
|
||||
int mbedtls_arc4_crypt(mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int x, y, a, b;
|
||||
size_t i;
|
||||
unsigned char *m;
|
||||
|
@ -154,8 +150,7 @@ static const unsigned char arc4_test_ct[3][8] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_arc4_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_arc4_self_test(int verbose) {
|
||||
int i, ret = 0;
|
||||
unsigned char ibuf[8];
|
||||
unsigned char obuf[8];
|
||||
|
|
|
@ -51,8 +51,7 @@
|
|||
*/
|
||||
int mbedtls_asn1_get_len(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
size_t *len)
|
||||
{
|
||||
size_t *len) {
|
||||
if ((end - *p) < 1)
|
||||
return (MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
||||
|
||||
|
@ -107,8 +106,7 @@ int mbedtls_asn1_get_len(unsigned char **p,
|
|||
|
||||
int mbedtls_asn1_get_tag(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
size_t *len, int tag)
|
||||
{
|
||||
size_t *len, int tag) {
|
||||
if ((end - *p) < 1)
|
||||
return (MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
||||
|
||||
|
@ -122,8 +120,7 @@ int mbedtls_asn1_get_tag(unsigned char **p,
|
|||
|
||||
int mbedtls_asn1_get_bool(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *val)
|
||||
{
|
||||
int *val) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -141,8 +138,7 @@ int mbedtls_asn1_get_bool(unsigned char **p,
|
|||
|
||||
int mbedtls_asn1_get_int(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *val)
|
||||
{
|
||||
int *val) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -165,8 +161,7 @@ int mbedtls_asn1_get_int(unsigned char **p,
|
|||
#if defined(MBEDTLS_BIGNUM_C)
|
||||
int mbedtls_asn1_get_mpi(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_mpi *X)
|
||||
{
|
||||
mbedtls_mpi *X) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -182,8 +177,7 @@ int mbedtls_asn1_get_mpi(unsigned char **p,
|
|||
#endif /* MBEDTLS_BIGNUM_C */
|
||||
|
||||
int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_asn1_bitstring *bs)
|
||||
{
|
||||
mbedtls_asn1_bitstring *bs) {
|
||||
int ret;
|
||||
|
||||
/* Certificate type is a single byte bitstring */
|
||||
|
@ -215,8 +209,7 @@ int mbedtls_asn1_get_bitstring(unsigned char **p, const unsigned char *end,
|
|||
* Get a bit string without unused bits
|
||||
*/
|
||||
int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end,
|
||||
size_t *len)
|
||||
{
|
||||
size_t *len) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_tag(p, end, len, MBEDTLS_ASN1_BIT_STRING)) != 0)
|
||||
|
@ -236,8 +229,7 @@ int mbedtls_asn1_get_bitstring_null(unsigned char **p, const unsigned char *end,
|
|||
int mbedtls_asn1_get_sequence_of(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_asn1_sequence *cur,
|
||||
int tag)
|
||||
{
|
||||
int tag) {
|
||||
int ret;
|
||||
size_t len;
|
||||
mbedtls_asn1_buf *buf;
|
||||
|
@ -283,8 +275,7 @@ int mbedtls_asn1_get_sequence_of(unsigned char **p,
|
|||
|
||||
int mbedtls_asn1_get_alg(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params)
|
||||
{
|
||||
mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -326,8 +317,7 @@ int mbedtls_asn1_get_alg(unsigned char **p,
|
|||
|
||||
int mbedtls_asn1_get_alg_null(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_asn1_buf *alg)
|
||||
{
|
||||
mbedtls_asn1_buf *alg) {
|
||||
int ret;
|
||||
mbedtls_asn1_buf params;
|
||||
|
||||
|
@ -342,8 +332,7 @@ int mbedtls_asn1_get_alg_null(unsigned char **p,
|
|||
return (0);
|
||||
}
|
||||
|
||||
void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur)
|
||||
{
|
||||
void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur) {
|
||||
if (cur == NULL)
|
||||
return;
|
||||
|
||||
|
@ -353,8 +342,7 @@ void mbedtls_asn1_free_named_data(mbedtls_asn1_named_data *cur)
|
|||
mbedtls_platform_zeroize(cur, sizeof(mbedtls_asn1_named_data));
|
||||
}
|
||||
|
||||
void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head)
|
||||
{
|
||||
void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head) {
|
||||
mbedtls_asn1_named_data *cur;
|
||||
|
||||
while ((cur = *head) != NULL) {
|
||||
|
@ -365,11 +353,10 @@ void mbedtls_asn1_free_named_data_list(mbedtls_asn1_named_data **head)
|
|||
}
|
||||
|
||||
mbedtls_asn1_named_data *mbedtls_asn1_find_named_data(mbedtls_asn1_named_data *list,
|
||||
const char *oid, size_t len)
|
||||
{
|
||||
const char *oid, size_t len) {
|
||||
while (list != NULL) {
|
||||
if (list->oid.len == len &&
|
||||
memcmp(list->oid.p, oid, len) == 0) {
|
||||
memcmp(list->oid.p, oid, len) == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,8 +41,7 @@
|
|||
#define mbedtls_free free
|
||||
#endif
|
||||
|
||||
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
|
||||
{
|
||||
int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len) {
|
||||
if (len < 0x80) {
|
||||
if (*p - start < 1)
|
||||
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
|
||||
|
@ -101,8 +100,7 @@ int mbedtls_asn1_write_len(unsigned char **p, unsigned char *start, size_t len)
|
|||
#endif
|
||||
}
|
||||
|
||||
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag)
|
||||
{
|
||||
int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned char tag) {
|
||||
if (*p - start < 1)
|
||||
return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
|
||||
|
||||
|
@ -112,8 +110,7 @@ int mbedtls_asn1_write_tag(unsigned char **p, unsigned char *start, unsigned cha
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
|
||||
const unsigned char *buf, size_t size)
|
||||
{
|
||||
const unsigned char *buf, size_t size) {
|
||||
size_t len = 0;
|
||||
|
||||
if (*p < start || (size_t)(*p - start) < size)
|
||||
|
@ -127,8 +124,7 @@ int mbedtls_asn1_write_raw_buffer(unsigned char **p, unsigned char *start,
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C)
|
||||
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X)
|
||||
{
|
||||
int mbedtls_asn1_write_mpi(unsigned char **p, unsigned char *start, const mbedtls_mpi *X) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -163,8 +159,7 @@ cleanup:
|
|||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C */
|
||||
|
||||
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
|
||||
{
|
||||
int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -177,8 +172,7 @@ int mbedtls_asn1_write_null(unsigned char **p, unsigned char *start)
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
|
||||
const char *oid, size_t oid_len)
|
||||
{
|
||||
const char *oid, size_t oid_len) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -192,8 +186,7 @@ int mbedtls_asn1_write_oid(unsigned char **p, unsigned char *start,
|
|||
|
||||
int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *start,
|
||||
const char *oid, size_t oid_len,
|
||||
size_t par_len)
|
||||
{
|
||||
size_t par_len) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -211,8 +204,7 @@ int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, unsigned char *st
|
|||
return ((int) len);
|
||||
}
|
||||
|
||||
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean)
|
||||
{
|
||||
int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -228,8 +220,7 @@ int mbedtls_asn1_write_bool(unsigned char **p, unsigned char *start, int boolean
|
|||
return ((int) len);
|
||||
}
|
||||
|
||||
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
|
||||
{
|
||||
int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -254,8 +245,7 @@ int mbedtls_asn1_write_int(unsigned char **p, unsigned char *start, int val)
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
|
||||
const char *text, size_t text_len)
|
||||
{
|
||||
const char *text, size_t text_len) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -269,8 +259,7 @@ int mbedtls_asn1_write_printable_string(unsigned char **p, unsigned char *start,
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
|
||||
const char *text, size_t text_len)
|
||||
{
|
||||
const char *text, size_t text_len) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -284,8 +273,7 @@ int mbedtls_asn1_write_ia5_string(unsigned char **p, unsigned char *start,
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
|
||||
const unsigned char *buf, size_t bits)
|
||||
{
|
||||
const unsigned char *buf, size_t bits) {
|
||||
int ret;
|
||||
size_t len = 0, size;
|
||||
|
||||
|
@ -311,8 +299,7 @@ int mbedtls_asn1_write_bitstring(unsigned char **p, unsigned char *start,
|
|||
}
|
||||
|
||||
int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
|
||||
const unsigned char *buf, size_t size)
|
||||
{
|
||||
const unsigned char *buf, size_t size) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -327,8 +314,7 @@ int mbedtls_asn1_write_octet_string(unsigned char **p, unsigned char *start,
|
|||
mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(mbedtls_asn1_named_data **head,
|
||||
const char *oid, size_t oid_len,
|
||||
const unsigned char *val,
|
||||
size_t val_len)
|
||||
{
|
||||
size_t val_len) {
|
||||
mbedtls_asn1_named_data *cur;
|
||||
|
||||
if ((cur = mbedtls_asn1_find_named_data(*head, oid, oid_len)) == NULL) {
|
||||
|
|
|
@ -75,8 +75,7 @@ static const unsigned char base64_dec_map[128] = {
|
|||
* Encode a buffer into base64 format
|
||||
*/
|
||||
int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
|
||||
const unsigned char *src, size_t slen)
|
||||
{
|
||||
const unsigned char *src, size_t slen) {
|
||||
size_t i, n;
|
||||
int C1, C2, C3;
|
||||
unsigned char *p;
|
||||
|
@ -137,8 +136,7 @@ int mbedtls_base64_encode(unsigned char *dst, size_t dlen, size_t *olen,
|
|||
* Decode a base64-formatted buffer
|
||||
*/
|
||||
int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
|
||||
const unsigned char *src, size_t slen)
|
||||
{
|
||||
const unsigned char *src, size_t slen) {
|
||||
size_t i, n;
|
||||
uint32_t j, x;
|
||||
unsigned char *p;
|
||||
|
@ -157,7 +155,7 @@ int mbedtls_base64_decode(unsigned char *dst, size_t dlen, size_t *olen,
|
|||
break;
|
||||
|
||||
if ((slen - i) >= 2 &&
|
||||
src[i] == '\r' && src[i + 1] == '\n')
|
||||
src[i] == '\r' && src[i + 1] == '\n')
|
||||
continue;
|
||||
|
||||
if (src[i] == '\n')
|
||||
|
@ -236,8 +234,7 @@ static const unsigned char base64_test_enc[] =
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_base64_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_base64_self_test(int verbose) {
|
||||
size_t len;
|
||||
const unsigned char *src;
|
||||
unsigned char buffer[128];
|
||||
|
@ -248,7 +245,7 @@ int mbedtls_base64_self_test(int verbose)
|
|||
src = base64_test_dec;
|
||||
|
||||
if (mbedtls_base64_encode(buffer, sizeof(buffer), &len, src, 64) != 0 ||
|
||||
memcmp(base64_test_enc, buffer, 88) != 0) {
|
||||
memcmp(base64_test_enc, buffer, 88) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
@ -261,7 +258,7 @@ int mbedtls_base64_self_test(int verbose)
|
|||
src = base64_test_enc;
|
||||
|
||||
if (mbedtls_base64_decode(buffer, sizeof(buffer), &len, src, 88) != 0 ||
|
||||
memcmp(base64_test_dec, buffer, 64) != 0) {
|
||||
memcmp(base64_test_dec, buffer, 64) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
|
|
@ -75,16 +75,14 @@
|
|||
#define CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) )
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n)
|
||||
{
|
||||
static void mbedtls_mpi_zeroize(mbedtls_mpi_uint *v, size_t n) {
|
||||
mbedtls_platform_zeroize(v, ciL * n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize one MPI
|
||||
*/
|
||||
void mbedtls_mpi_init(mbedtls_mpi *X)
|
||||
{
|
||||
void mbedtls_mpi_init(mbedtls_mpi *X) {
|
||||
if (X == NULL)
|
||||
return;
|
||||
|
||||
|
@ -96,8 +94,7 @@ void mbedtls_mpi_init(mbedtls_mpi *X)
|
|||
/*
|
||||
* Unallocate one MPI
|
||||
*/
|
||||
void mbedtls_mpi_free(mbedtls_mpi *X)
|
||||
{
|
||||
void mbedtls_mpi_free(mbedtls_mpi *X) {
|
||||
if (X == NULL)
|
||||
return;
|
||||
|
||||
|
@ -114,8 +111,7 @@ void mbedtls_mpi_free(mbedtls_mpi *X)
|
|||
/*
|
||||
* Enlarge to the specified number of limbs
|
||||
*/
|
||||
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
|
||||
{
|
||||
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs) {
|
||||
mbedtls_mpi_uint *p;
|
||||
|
||||
if (nblimbs > MBEDTLS_MPI_MAX_LIMBS)
|
||||
|
@ -142,8 +138,7 @@ int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
|
|||
* Resize down as much as possible,
|
||||
* while keeping at least the specified number of limbs
|
||||
*/
|
||||
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
|
||||
{
|
||||
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs) {
|
||||
mbedtls_mpi_uint *p;
|
||||
size_t i;
|
||||
|
||||
|
@ -177,8 +172,7 @@ int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
|
|||
/*
|
||||
* Copy the contents of Y into X
|
||||
*/
|
||||
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
|
||||
{
|
||||
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y) {
|
||||
int ret = 0;
|
||||
size_t i;
|
||||
|
||||
|
@ -213,8 +207,7 @@ cleanup:
|
|||
/*
|
||||
* Swap the contents of X and Y
|
||||
*/
|
||||
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
|
||||
{
|
||||
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y) {
|
||||
mbedtls_mpi T;
|
||||
|
||||
memcpy(&T, X, sizeof(mbedtls_mpi));
|
||||
|
@ -227,8 +220,7 @@ void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
|
|||
* about whether the assignment was made or not.
|
||||
* (Leaking information about the respective sizes of X and Y is ok however.)
|
||||
*/
|
||||
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
|
||||
{
|
||||
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign) {
|
||||
int ret = 0;
|
||||
size_t i;
|
||||
|
||||
|
@ -255,8 +247,7 @@ cleanup:
|
|||
* Here it is not ok to simply swap the pointers, which whould lead to
|
||||
* different memory access patterns when X and Y are used afterwards.
|
||||
*/
|
||||
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap)
|
||||
{
|
||||
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap) {
|
||||
int ret, s;
|
||||
size_t i;
|
||||
mbedtls_mpi_uint tmp;
|
||||
|
@ -288,8 +279,7 @@ cleanup:
|
|||
/*
|
||||
* Set value from integer
|
||||
*/
|
||||
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
|
||||
{
|
||||
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z) {
|
||||
int ret;
|
||||
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(X, 1));
|
||||
|
@ -306,8 +296,7 @@ cleanup:
|
|||
/*
|
||||
* Get a specific bit
|
||||
*/
|
||||
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
|
||||
{
|
||||
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos) {
|
||||
if (X->n * biL <= pos)
|
||||
return (0);
|
||||
|
||||
|
@ -317,8 +306,7 @@ int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
|
|||
/*
|
||||
* Set a bit to a specific value of 0 or 1
|
||||
*/
|
||||
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
|
||||
{
|
||||
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val) {
|
||||
int ret = 0;
|
||||
size_t off = pos / biL;
|
||||
size_t idx = pos % biL;
|
||||
|
@ -344,8 +332,7 @@ cleanup:
|
|||
/*
|
||||
* Return the number of less significant zero-bits
|
||||
*/
|
||||
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
|
||||
{
|
||||
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X) {
|
||||
size_t i, j, count = 0;
|
||||
|
||||
for (i = 0; i < X->n; i++)
|
||||
|
@ -359,8 +346,7 @@ size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
|
|||
/*
|
||||
* Count leading zero bits in a given integer
|
||||
*/
|
||||
static size_t mbedtls_clz(const mbedtls_mpi_uint x)
|
||||
{
|
||||
static size_t mbedtls_clz(const mbedtls_mpi_uint x) {
|
||||
size_t j;
|
||||
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
||||
|
||||
|
@ -376,8 +362,7 @@ static size_t mbedtls_clz(const mbedtls_mpi_uint x)
|
|||
/*
|
||||
* Return the number of bits
|
||||
*/
|
||||
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
|
||||
{
|
||||
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X) {
|
||||
size_t i, j;
|
||||
|
||||
if (X->n == 0)
|
||||
|
@ -395,16 +380,14 @@ size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
|
|||
/*
|
||||
* Return the total size in bytes
|
||||
*/
|
||||
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
|
||||
{
|
||||
size_t mbedtls_mpi_size(const mbedtls_mpi *X) {
|
||||
return ((mbedtls_mpi_bitlen(X) + 7) >> 3);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert an ASCII character to digit value
|
||||
*/
|
||||
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
|
||||
{
|
||||
static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c) {
|
||||
*d = 255;
|
||||
|
||||
if (c >= 0x30 && c <= 0x39) *d = c - 0x30;
|
||||
|
@ -420,8 +403,7 @@ static int mpi_get_digit(mbedtls_mpi_uint *d, int radix, char c)
|
|||
/*
|
||||
* Import from an ASCII string
|
||||
*/
|
||||
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
|
||||
{
|
||||
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s) {
|
||||
int ret;
|
||||
size_t i, j, slen, n;
|
||||
mbedtls_mpi_uint d;
|
||||
|
@ -482,8 +464,7 @@ cleanup:
|
|||
/*
|
||||
* Helper to write the digits high-order first
|
||||
*/
|
||||
static int mpi_write_hlp(mbedtls_mpi *X, int radix, char **p)
|
||||
{
|
||||
static int mpi_write_hlp(mbedtls_mpi *X, int radix, char **p) {
|
||||
int ret;
|
||||
mbedtls_mpi_uint r;
|
||||
|
||||
|
@ -510,8 +491,7 @@ cleanup:
|
|||
* Export into an ASCII string
|
||||
*/
|
||||
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
|
||||
char *buf, size_t buflen, size_t *olen)
|
||||
{
|
||||
char *buf, size_t buflen, size_t *olen) {
|
||||
int ret = 0;
|
||||
size_t n;
|
||||
char *p;
|
||||
|
@ -580,8 +560,7 @@ cleanup:
|
|||
/*
|
||||
* Read X from an opened file
|
||||
*/
|
||||
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
|
||||
{
|
||||
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin) {
|
||||
mbedtls_mpi_uint d;
|
||||
size_t slen;
|
||||
char *p;
|
||||
|
@ -613,8 +592,7 @@ int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
|
|||
/*
|
||||
* Write X into an opened file (or stdout if fout == NULL)
|
||||
*/
|
||||
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
|
||||
{
|
||||
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout) {
|
||||
int ret;
|
||||
size_t n, slen, plen;
|
||||
/*
|
||||
|
@ -636,7 +614,7 @@ int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE
|
|||
|
||||
if (fout != NULL) {
|
||||
if (fwrite(p, 1, plen, fout) != plen ||
|
||||
fwrite(s, 1, slen, fout) != slen)
|
||||
fwrite(s, 1, slen, fout) != slen)
|
||||
return (MBEDTLS_ERR_MPI_FILE_IO_ERROR);
|
||||
} else
|
||||
mbedtls_printf("%s%s", p, s);
|
||||
|
@ -650,8 +628,7 @@ cleanup:
|
|||
/*
|
||||
* Import X from unsigned binary data, big endian
|
||||
*/
|
||||
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
|
||||
{
|
||||
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen) {
|
||||
int ret;
|
||||
size_t i, j;
|
||||
size_t const limbs = CHARS_TO_LIMBS(buflen);
|
||||
|
@ -676,8 +653,7 @@ cleanup:
|
|||
/*
|
||||
* Export X into unsigned binary data, big endian
|
||||
*/
|
||||
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
|
||||
{
|
||||
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen) {
|
||||
size_t i, j, n;
|
||||
|
||||
n = mbedtls_mpi_size(X);
|
||||
|
@ -696,8 +672,7 @@ int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t bu
|
|||
/*
|
||||
* Left-shift: X <<= count
|
||||
*/
|
||||
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
|
||||
{
|
||||
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count) {
|
||||
int ret;
|
||||
size_t i, v0, t1;
|
||||
mbedtls_mpi_uint r0 = 0, r1;
|
||||
|
@ -743,8 +718,7 @@ cleanup:
|
|||
/*
|
||||
* Right-shift: X >>= count
|
||||
*/
|
||||
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
|
||||
{
|
||||
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count) {
|
||||
size_t i, v0, v1;
|
||||
mbedtls_mpi_uint r0 = 0, r1;
|
||||
|
||||
|
@ -783,8 +757,7 @@ int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
|
|||
/*
|
||||
* Compare unsigned values
|
||||
*/
|
||||
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
||||
{
|
||||
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y) {
|
||||
size_t i, j;
|
||||
|
||||
for (i = X->n; i > 0; i--)
|
||||
|
@ -812,8 +785,7 @@ int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
|||
/*
|
||||
* Compare signed values
|
||||
*/
|
||||
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
||||
{
|
||||
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y) {
|
||||
size_t i, j;
|
||||
|
||||
for (i = X->n; i > 0; i--)
|
||||
|
@ -844,8 +816,7 @@ int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
|
|||
/*
|
||||
* Compare signed values
|
||||
*/
|
||||
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
|
||||
{
|
||||
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z) {
|
||||
mbedtls_mpi Y;
|
||||
mbedtls_mpi_uint p[1];
|
||||
|
||||
|
@ -860,8 +831,7 @@ int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
|
|||
/*
|
||||
* Unsigned addition: X = |A| + |B| (HAC 14.7)
|
||||
*/
|
||||
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret;
|
||||
size_t i, j;
|
||||
mbedtls_mpi_uint *o, *p, c, tmp;
|
||||
|
@ -921,8 +891,7 @@ cleanup:
|
|||
/*
|
||||
* Helper for mbedtls_mpi subtraction
|
||||
*/
|
||||
static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d)
|
||||
{
|
||||
static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d) {
|
||||
size_t i;
|
||||
mbedtls_mpi_uint c, z;
|
||||
|
||||
|
@ -944,8 +913,7 @@ static void mpi_sub_hlp(size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d)
|
|||
/*
|
||||
* Unsigned subtraction: X = |A| - |B| (HAC 14.9)
|
||||
*/
|
||||
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
mbedtls_mpi TB;
|
||||
int ret;
|
||||
size_t n;
|
||||
|
@ -986,8 +954,7 @@ cleanup:
|
|||
/*
|
||||
* Signed addition: X = A + B
|
||||
*/
|
||||
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret, s = A->s;
|
||||
|
||||
if (A->s * B->s < 0) {
|
||||
|
@ -1011,8 +978,7 @@ cleanup:
|
|||
/*
|
||||
* Signed subtraction: X = A - B
|
||||
*/
|
||||
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret, s = A->s;
|
||||
|
||||
if (A->s * B->s > 0) {
|
||||
|
@ -1036,8 +1002,7 @@ cleanup:
|
|||
/*
|
||||
* Signed addition: X = A + b
|
||||
*/
|
||||
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
||||
{
|
||||
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
|
||||
mbedtls_mpi _B;
|
||||
mbedtls_mpi_uint p[1];
|
||||
|
||||
|
@ -1052,8 +1017,7 @@ int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b
|
|||
/*
|
||||
* Signed subtraction: X = A - b
|
||||
*/
|
||||
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
||||
{
|
||||
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
|
||||
mbedtls_mpi _B;
|
||||
mbedtls_mpi_uint p[1];
|
||||
|
||||
|
@ -1076,8 +1040,7 @@ static
|
|||
*/
|
||||
__attribute__((noinline))
|
||||
#endif
|
||||
void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b)
|
||||
{
|
||||
void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b) {
|
||||
mbedtls_mpi_uint c = 0, t = 0;
|
||||
|
||||
#if defined(MULADDC_HUIT)
|
||||
|
@ -1136,8 +1099,7 @@ void mpi_mul_hlp(size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi
|
|||
/*
|
||||
* Baseline multiplication: X = A * B (HAC 14.12)
|
||||
*/
|
||||
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret;
|
||||
size_t i, j;
|
||||
mbedtls_mpi TA, TB;
|
||||
|
@ -1175,8 +1137,7 @@ cleanup:
|
|||
/*
|
||||
* Baseline multiplication: X = A * b
|
||||
*/
|
||||
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
|
||||
{
|
||||
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b) {
|
||||
mbedtls_mpi _B;
|
||||
mbedtls_mpi_uint p[1];
|
||||
|
||||
|
@ -1193,8 +1154,7 @@ int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b
|
|||
* mbedtls_mpi_uint divisor, d
|
||||
*/
|
||||
static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
|
||||
mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r)
|
||||
{
|
||||
mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r) {
|
||||
#if defined(MBEDTLS_HAVE_UDBL)
|
||||
mbedtls_t_udbl dividend, quotient;
|
||||
#else
|
||||
|
@ -1284,8 +1244,7 @@ static mbedtls_mpi_uint mbedtls_int_div_int(mbedtls_mpi_uint u1,
|
|||
/*
|
||||
* Division by mbedtls_mpi: A = Q * B + R (HAC 14.20)
|
||||
*/
|
||||
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret;
|
||||
size_t i, n, t, k;
|
||||
mbedtls_mpi X, Y, Z, T1, T2;
|
||||
|
@ -1394,8 +1353,7 @@ cleanup:
|
|||
/*
|
||||
* Division by int: A = Q * b + R
|
||||
*/
|
||||
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
||||
{
|
||||
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
|
||||
mbedtls_mpi _B;
|
||||
mbedtls_mpi_uint p[1];
|
||||
|
||||
|
@ -1410,8 +1368,7 @@ int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mb
|
|||
/*
|
||||
* Modulo: R = A mod B
|
||||
*/
|
||||
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret;
|
||||
|
||||
if (mbedtls_mpi_cmp_int(B, 0) < 0)
|
||||
|
@ -1433,8 +1390,7 @@ cleanup:
|
|||
/*
|
||||
* Modulo: r = A mod b
|
||||
*/
|
||||
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
|
||||
{
|
||||
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b) {
|
||||
size_t i;
|
||||
mbedtls_mpi_uint x, y, z;
|
||||
|
||||
|
@ -1487,8 +1443,7 @@ int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_s
|
|||
/*
|
||||
* Fast Montgomery initialization (thanks to Tom St Denis)
|
||||
*/
|
||||
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
|
||||
{
|
||||
static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N) {
|
||||
mbedtls_mpi_uint x, m0 = N->p[0];
|
||||
unsigned int i;
|
||||
|
||||
|
@ -1505,8 +1460,7 @@ static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
|
|||
* Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
|
||||
*/
|
||||
static int mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
|
||||
const mbedtls_mpi *T)
|
||||
{
|
||||
const mbedtls_mpi *T) {
|
||||
size_t i, n, m;
|
||||
mbedtls_mpi_uint u0, u1, *d;
|
||||
|
||||
|
@ -1547,8 +1501,7 @@ static int mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *
|
|||
/*
|
||||
* Montgomery reduction: A = A * R^-1 mod N
|
||||
*/
|
||||
static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T)
|
||||
{
|
||||
static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T) {
|
||||
mbedtls_mpi_uint z = 1;
|
||||
mbedtls_mpi U;
|
||||
|
||||
|
@ -1561,8 +1514,7 @@ static int mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm
|
|||
/*
|
||||
* Sliding-window exponentiation: X = A^E mod N (HAC 14.85)
|
||||
*/
|
||||
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
|
||||
{
|
||||
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR) {
|
||||
int ret;
|
||||
size_t wbits, wsize, one = 1;
|
||||
size_t i, j, nblimbs;
|
||||
|
@ -1761,8 +1713,7 @@ cleanup:
|
|||
/*
|
||||
* Greatest common divisor: G = gcd(A, B) (HAC 14.54)
|
||||
*/
|
||||
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
|
||||
{
|
||||
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B) {
|
||||
int ret;
|
||||
size_t lz, lzt;
|
||||
mbedtls_mpi TG, TA, TB;
|
||||
|
@ -1819,8 +1770,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
|
||||
|
@ -1838,8 +1788,7 @@ cleanup:
|
|||
/*
|
||||
* Modular inverse: X = A^-1 mod N (HAC 14.61 / 14.64)
|
||||
*/
|
||||
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
|
||||
{
|
||||
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N) {
|
||||
int ret;
|
||||
mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
|
||||
|
||||
|
@ -1967,8 +1916,7 @@ static const int small_prime[] = {
|
|||
* MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
|
||||
* other negative: error
|
||||
*/
|
||||
static int mpi_check_small_factors(const mbedtls_mpi *X)
|
||||
{
|
||||
static int mpi_check_small_factors(const mbedtls_mpi *X) {
|
||||
int ret = 0;
|
||||
size_t i;
|
||||
mbedtls_mpi_uint r;
|
||||
|
@ -1995,8 +1943,7 @@ cleanup:
|
|||
*/
|
||||
static int mpi_miller_rabin(const mbedtls_mpi *X,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret, count;
|
||||
size_t i, j, k, n, s;
|
||||
mbedtls_mpi W, R, T, A, RR;
|
||||
|
@ -2059,7 +2006,7 @@ static int mpi_miller_rabin(const mbedtls_mpi *X,
|
|||
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&A, &A, &R, X, &RR));
|
||||
|
||||
if (mbedtls_mpi_cmp_mpi(&A, &W) == 0 ||
|
||||
mbedtls_mpi_cmp_int(&A, 1) == 0)
|
||||
mbedtls_mpi_cmp_int(&A, 1) == 0)
|
||||
continue;
|
||||
|
||||
j = 1;
|
||||
|
@ -2080,7 +2027,7 @@ static int mpi_miller_rabin(const mbedtls_mpi *X,
|
|||
* not prime if A != |X| - 1 or A == 1
|
||||
*/
|
||||
if (mbedtls_mpi_cmp_mpi(&A, &W) != 0 ||
|
||||
mbedtls_mpi_cmp_int(&A, 1) == 0) {
|
||||
mbedtls_mpi_cmp_int(&A, 1) == 0) {
|
||||
ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
|
||||
break;
|
||||
}
|
||||
|
@ -2101,8 +2048,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_mpi XX;
|
||||
|
||||
|
@ -2111,7 +2057,7 @@ int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
|
|||
XX.p = X->p;
|
||||
|
||||
if (mbedtls_mpi_cmp_int(&XX, 0) == 0 ||
|
||||
mbedtls_mpi_cmp_int(&XX, 1) == 0)
|
||||
mbedtls_mpi_cmp_int(&XX, 1) == 0)
|
||||
return (MBEDTLS_ERR_MPI_NOT_ACCEPTABLE);
|
||||
|
||||
if (mbedtls_mpi_cmp_int(&XX, 2) == 0)
|
||||
|
@ -2136,8 +2082,7 @@ int mbedtls_mpi_is_prime(const mbedtls_mpi *X,
|
|||
*/
|
||||
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
#ifdef MBEDTLS_HAVE_INT64
|
||||
// ceil(2^63.5)
|
||||
#define CEIL_MAXUINT_DIV_SQRT2 0xb504f333f9de6485ULL
|
||||
|
@ -2196,9 +2141,9 @@ int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag,
|
|||
* before doing Miller-Rabin on any of them
|
||||
*/
|
||||
if ((ret = mpi_check_small_factors(X)) == 0 &&
|
||||
(ret = mpi_check_small_factors(&Y)) == 0 &&
|
||||
(ret = mpi_miller_rabin(X, f_rng, p_rng)) == 0 &&
|
||||
(ret = mpi_miller_rabin(&Y, f_rng, p_rng)) == 0)
|
||||
(ret = mpi_check_small_factors(&Y)) == 0 &&
|
||||
(ret = mpi_miller_rabin(X, f_rng, p_rng)) == 0 &&
|
||||
(ret = mpi_miller_rabin(&Y, f_rng, p_rng)) == 0)
|
||||
goto cleanup;
|
||||
|
||||
if (ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE)
|
||||
|
@ -2237,8 +2182,7 @@ static const int gcd_pairs[GCD_PAIR_COUNT][3] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_mpi_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_mpi_self_test(int verbose) {
|
||||
int ret, i;
|
||||
mbedtls_mpi A, E, N, X, Y, U, V;
|
||||
|
||||
|
@ -2306,7 +2250,7 @@ int mbedtls_mpi_self_test(int verbose)
|
|||
mbedtls_printf(" MPI test #2 (div_mpi): ");
|
||||
|
||||
if (mbedtls_mpi_cmp_mpi(&X, &U) != 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
|
||||
mbedtls_mpi_cmp_mpi(&Y, &V) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
|
|
@ -76,8 +76,7 @@ static const uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2] = {
|
|||
/* declarations of data at the end of this file */
|
||||
static const uint32_t S[4][256];
|
||||
|
||||
static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x)
|
||||
{
|
||||
static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x) {
|
||||
unsigned short a, b, c, d;
|
||||
uint32_t y;
|
||||
|
||||
|
@ -95,8 +94,7 @@ static uint32_t F(mbedtls_blowfish_context *ctx, uint32_t x)
|
|||
return (y);
|
||||
}
|
||||
|
||||
static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr)
|
||||
{
|
||||
static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
|
||||
uint32_t Xl, Xr, temp;
|
||||
short i;
|
||||
|
||||
|
@ -123,8 +121,7 @@ static void blowfish_enc(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
|
|||
*xr = Xr;
|
||||
}
|
||||
|
||||
static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr)
|
||||
{
|
||||
static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr) {
|
||||
uint32_t Xl, Xr, temp;
|
||||
short i;
|
||||
|
||||
|
@ -151,13 +148,11 @@ static void blowfish_dec(mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *
|
|||
*xr = Xr;
|
||||
}
|
||||
|
||||
void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx)
|
||||
{
|
||||
void mbedtls_blowfish_init(mbedtls_blowfish_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_blowfish_context));
|
||||
}
|
||||
|
||||
void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx)
|
||||
{
|
||||
void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -168,13 +163,12 @@ void mbedtls_blowfish_free(mbedtls_blowfish_context *ctx)
|
|||
* Blowfish key schedule
|
||||
*/
|
||||
int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
unsigned int i, j, k;
|
||||
uint32_t data, datal, datar;
|
||||
|
||||
if (keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS || keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS ||
|
||||
(keybits % 8)) {
|
||||
(keybits % 8)) {
|
||||
return (MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH);
|
||||
}
|
||||
|
||||
|
@ -221,8 +215,7 @@ int mbedtls_blowfish_setkey(mbedtls_blowfish_context *ctx, const unsigned char *
|
|||
int mbedtls_blowfish_crypt_ecb(mbedtls_blowfish_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE])
|
||||
{
|
||||
unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE]) {
|
||||
uint32_t X0, X1;
|
||||
|
||||
GET_UINT32_BE(X0, input, 0);
|
||||
|
@ -249,8 +242,7 @@ int mbedtls_blowfish_crypt_cbc(mbedtls_blowfish_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int i;
|
||||
unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE];
|
||||
|
||||
|
@ -299,8 +291,7 @@ int mbedtls_blowfish_crypt_cfb64(mbedtls_blowfish_context *ctx,
|
|||
size_t *iv_off,
|
||||
unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
|
||||
|
@ -342,8 +333,7 @@ int mbedtls_blowfish_crypt_ctr(mbedtls_blowfish_context *ctx,
|
|||
unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
|
||||
|
|
|
@ -304,8 +304,7 @@ static const signed char transposes[2][20] = {
|
|||
}
|
||||
|
||||
static void camellia_feistel(const uint32_t x[2], const uint32_t k[2],
|
||||
uint32_t z[2])
|
||||
{
|
||||
uint32_t z[2]) {
|
||||
uint32_t I0, I1;
|
||||
I0 = x[0] ^ k[0];
|
||||
I1 = x[1] ^ k[1];
|
||||
|
@ -328,13 +327,11 @@ static void camellia_feistel(const uint32_t x[2], const uint32_t k[2],
|
|||
z[1] ^= I0;
|
||||
}
|
||||
|
||||
void mbedtls_camellia_init(mbedtls_camellia_context *ctx)
|
||||
{
|
||||
void mbedtls_camellia_init(mbedtls_camellia_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_camellia_context));
|
||||
}
|
||||
|
||||
void mbedtls_camellia_free(mbedtls_camellia_context *ctx)
|
||||
{
|
||||
void mbedtls_camellia_free(mbedtls_camellia_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -345,8 +342,7 @@ void mbedtls_camellia_free(mbedtls_camellia_context *ctx)
|
|||
* Camellia key schedule (encryption)
|
||||
*/
|
||||
int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
int idx;
|
||||
size_t i;
|
||||
uint32_t *RK;
|
||||
|
@ -456,8 +452,7 @@ int mbedtls_camellia_setkey_enc(mbedtls_camellia_context *ctx, const unsigned ch
|
|||
* Camellia key schedule (decryption)
|
||||
*/
|
||||
int mbedtls_camellia_setkey_dec(mbedtls_camellia_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
unsigned int keybits) {
|
||||
int idx, ret;
|
||||
size_t i;
|
||||
mbedtls_camellia_context cty;
|
||||
|
@ -505,8 +500,7 @@ exit:
|
|||
int mbedtls_camellia_crypt_ecb(mbedtls_camellia_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
int NR;
|
||||
uint32_t *RK, X[4];
|
||||
|
||||
|
@ -570,8 +564,7 @@ int mbedtls_camellia_crypt_cbc(mbedtls_camellia_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int i;
|
||||
unsigned char temp[16];
|
||||
|
||||
|
@ -620,8 +613,7 @@ int mbedtls_camellia_crypt_cfb128(mbedtls_camellia_context *ctx,
|
|||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c;
|
||||
size_t n = *iv_off;
|
||||
|
||||
|
@ -663,8 +655,7 @@ int mbedtls_camellia_crypt_ctr(mbedtls_camellia_context *ctx,
|
|||
unsigned char nonce_counter[16],
|
||||
unsigned char stream_block[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int c, i;
|
||||
size_t n = *nc_off;
|
||||
|
||||
|
@ -964,8 +955,7 @@ static const int camellia_test_ctr_len[3] =
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_camellia_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_camellia_self_test(int verbose) {
|
||||
int i, j, u, v;
|
||||
unsigned char key[32];
|
||||
unsigned char buf[64];
|
||||
|
|
|
@ -73,8 +73,7 @@
|
|||
* a non-zero value.
|
||||
* This is currently only used by GCM and ChaCha20+Poly1305.
|
||||
*/
|
||||
static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t len)
|
||||
{
|
||||
static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t len) {
|
||||
const unsigned char *p1 = (const unsigned char *) v1;
|
||||
const unsigned char *p2 = (const unsigned char *) v2;
|
||||
size_t i;
|
||||
|
@ -89,8 +88,7 @@ static int mbedtls_constant_time_memcmp(const void *v1, const void *v2, size_t l
|
|||
|
||||
static int supported_init = 0;
|
||||
|
||||
const int *mbedtls_cipher_list(void)
|
||||
{
|
||||
const int *mbedtls_cipher_list(void) {
|
||||
const mbedtls_cipher_definition_t *def;
|
||||
int *type;
|
||||
|
||||
|
@ -109,8 +107,7 @@ const int *mbedtls_cipher_list(void)
|
|||
return (mbedtls_cipher_supported);
|
||||
}
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
|
||||
{
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type) {
|
||||
const mbedtls_cipher_definition_t *def;
|
||||
|
||||
for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
|
||||
|
@ -120,8 +117,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type(const mbedtls_cipher_
|
|||
return (NULL);
|
||||
}
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name)
|
||||
{
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_name) {
|
||||
const mbedtls_cipher_definition_t *def;
|
||||
|
||||
if (NULL == cipher_name)
|
||||
|
@ -136,26 +132,23 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string(const char *cipher_
|
|||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id,
|
||||
int key_bitlen,
|
||||
const mbedtls_cipher_mode_t mode)
|
||||
{
|
||||
const mbedtls_cipher_mode_t mode) {
|
||||
const mbedtls_cipher_definition_t *def;
|
||||
|
||||
for (def = mbedtls_cipher_definitions; def->info != NULL; def++)
|
||||
if (def->info->base->cipher == cipher_id &&
|
||||
def->info->key_bitlen == (unsigned) key_bitlen &&
|
||||
def->info->mode == mode)
|
||||
def->info->key_bitlen == (unsigned) key_bitlen &&
|
||||
def->info->mode == mode)
|
||||
return (def->info);
|
||||
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_cipher_context_t));
|
||||
}
|
||||
|
||||
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -173,8 +166,7 @@ void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
|
|||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t));
|
||||
}
|
||||
|
||||
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
|
||||
{
|
||||
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info) {
|
||||
if (NULL == cipher_info || NULL == ctx)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -200,13 +192,12 @@ int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_inf
|
|||
}
|
||||
|
||||
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key,
|
||||
int key_bitlen, const mbedtls_operation_t operation)
|
||||
{
|
||||
int key_bitlen, const mbedtls_operation_t operation) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 &&
|
||||
(int) ctx->cipher_info->key_bitlen != key_bitlen) {
|
||||
(int) ctx->cipher_info->key_bitlen != key_bitlen) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -217,9 +208,9 @@ int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *ke
|
|||
* For OFB, CFB and CTR mode always use the encryption key schedule
|
||||
*/
|
||||
if (MBEDTLS_ENCRYPT == operation ||
|
||||
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
|
||||
MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode) {
|
||||
return ctx->cipher_info->base->setkey_enc_func(ctx->cipher_ctx, key,
|
||||
ctx->key_bitlen);
|
||||
}
|
||||
|
@ -232,8 +223,7 @@ int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *ke
|
|||
}
|
||||
|
||||
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *iv, size_t iv_len)
|
||||
{
|
||||
const unsigned char *iv, size_t iv_len) {
|
||||
size_t actual_iv_size;
|
||||
|
||||
if (NULL == ctx || NULL == ctx->cipher_info || NULL == iv)
|
||||
|
@ -269,8 +259,7 @@ int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx,
|
|||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -281,8 +270,7 @@ int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
|
|||
|
||||
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
||||
int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *ad, size_t ad_len)
|
||||
{
|
||||
const unsigned char *ad, size_t ad_len) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -318,8 +306,7 @@ int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx,
|
|||
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
|
||||
|
||||
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input,
|
||||
size_t ilen, unsigned char *output, size_t *olen)
|
||||
{
|
||||
size_t ilen, unsigned char *output, size_t *olen) {
|
||||
int ret;
|
||||
size_t block_size = 0;
|
||||
|
||||
|
@ -365,7 +352,7 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
|
|||
}
|
||||
|
||||
if (input == output &&
|
||||
(ctx->unprocessed_len != 0 || ilen % block_size)) {
|
||||
(ctx->unprocessed_len != 0 || ilen % block_size)) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -377,11 +364,11 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
|
|||
* If there is not enough data for a full block, cache it.
|
||||
*/
|
||||
if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding &&
|
||||
ilen <= block_size - ctx->unprocessed_len) ||
|
||||
(ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
|
||||
ilen < block_size - ctx->unprocessed_len) ||
|
||||
(ctx->operation == MBEDTLS_ENCRYPT &&
|
||||
ilen < block_size - ctx->unprocessed_len)) {
|
||||
ilen <= block_size - ctx->unprocessed_len) ||
|
||||
(ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding &&
|
||||
ilen < block_size - ctx->unprocessed_len) ||
|
||||
(ctx->operation == MBEDTLS_ENCRYPT &&
|
||||
ilen < block_size - ctx->unprocessed_len)) {
|
||||
memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input,
|
||||
ilen);
|
||||
|
||||
|
@ -426,8 +413,8 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
|
|||
*/
|
||||
copy_len = ilen % block_size;
|
||||
if (copy_len == 0 &&
|
||||
ctx->operation == MBEDTLS_DECRYPT &&
|
||||
NULL != ctx->add_padding) {
|
||||
ctx->operation == MBEDTLS_DECRYPT &&
|
||||
NULL != ctx->add_padding) {
|
||||
copy_len = block_size;
|
||||
}
|
||||
|
||||
|
@ -536,8 +523,7 @@ int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *in
|
|||
* PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
|
||||
*/
|
||||
static void add_pkcs_padding(unsigned char *output, size_t output_len,
|
||||
size_t data_len)
|
||||
{
|
||||
size_t data_len) {
|
||||
size_t padding_len = output_len - data_len;
|
||||
unsigned char i;
|
||||
|
||||
|
@ -546,8 +532,7 @@ static void add_pkcs_padding(unsigned char *output, size_t output_len,
|
|||
}
|
||||
|
||||
static int get_pkcs_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t *data_len) {
|
||||
size_t i, pad_idx;
|
||||
unsigned char padding_len, bad = 0;
|
||||
|
||||
|
@ -576,8 +561,7 @@ static int get_pkcs_padding(unsigned char *input, size_t input_len,
|
|||
* One and zeros padding: fill with 80 00 ... 00
|
||||
*/
|
||||
static void add_one_and_zeros_padding(unsigned char *output,
|
||||
size_t output_len, size_t data_len)
|
||||
{
|
||||
size_t output_len, size_t data_len) {
|
||||
size_t padding_len = output_len - data_len;
|
||||
unsigned char i = 0;
|
||||
|
||||
|
@ -587,8 +571,7 @@ static void add_one_and_zeros_padding(unsigned char *output,
|
|||
}
|
||||
|
||||
static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t *data_len) {
|
||||
size_t i;
|
||||
unsigned char done = 0, prev_done, bad;
|
||||
|
||||
|
@ -614,8 +597,7 @@ static int get_one_and_zeros_padding(unsigned char *input, size_t input_len,
|
|||
* Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
|
||||
*/
|
||||
static void add_zeros_and_len_padding(unsigned char *output,
|
||||
size_t output_len, size_t data_len)
|
||||
{
|
||||
size_t output_len, size_t data_len) {
|
||||
size_t padding_len = output_len - data_len;
|
||||
unsigned char i = 0;
|
||||
|
||||
|
@ -625,8 +607,7 @@ static void add_zeros_and_len_padding(unsigned char *output,
|
|||
}
|
||||
|
||||
static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t *data_len) {
|
||||
size_t i, pad_idx;
|
||||
unsigned char padding_len, bad = 0;
|
||||
|
||||
|
@ -654,8 +635,7 @@ static int get_zeros_and_len_padding(unsigned char *input, size_t input_len,
|
|||
* Zero padding: fill with 00 ... 00
|
||||
*/
|
||||
static void add_zeros_padding(unsigned char *output,
|
||||
size_t output_len, size_t data_len)
|
||||
{
|
||||
size_t output_len, size_t data_len) {
|
||||
size_t i;
|
||||
|
||||
for (i = data_len; i < output_len; i++)
|
||||
|
@ -663,8 +643,7 @@ static void add_zeros_padding(unsigned char *output,
|
|||
}
|
||||
|
||||
static int get_zeros_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t *data_len) {
|
||||
size_t i;
|
||||
unsigned char done = 0, prev_done;
|
||||
|
||||
|
@ -689,8 +668,7 @@ static int get_zeros_padding(unsigned char *input, size_t input_len,
|
|||
* but a trivial get_padding function
|
||||
*/
|
||||
static int get_no_padding(unsigned char *input, size_t input_len,
|
||||
size_t *data_len)
|
||||
{
|
||||
size_t *data_len) {
|
||||
if (NULL == input || NULL == data_len)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -701,24 +679,23 @@ static int get_no_padding(unsigned char *input, size_t input_len,
|
|||
#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
||||
|
||||
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *output, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t *olen) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info || NULL == olen)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
*olen = 0;
|
||||
|
||||
if (MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
|
||||
MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_XTS == ctx->cipher_info->mode ||
|
||||
MBEDTLS_MODE_STREAM == ctx->cipher_info->mode) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((MBEDTLS_CIPHER_CHACHA20 == ctx->cipher_info->type) ||
|
||||
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
|
||||
(MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type)) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -779,10 +756,9 @@ int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx,
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
||||
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode)
|
||||
{
|
||||
int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode) {
|
||||
if (NULL == ctx ||
|
||||
MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
|
||||
MBEDTLS_MODE_CBC != ctx->cipher_info->mode) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -826,8 +802,7 @@ int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, mbedtls_ciphe
|
|||
|
||||
#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
|
||||
int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *tag, size_t tag_len)
|
||||
{
|
||||
unsigned char *tag, size_t tag_len) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info || NULL == tag)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -854,13 +829,12 @@ int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx,
|
|||
}
|
||||
|
||||
int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *tag, size_t tag_len)
|
||||
{
|
||||
const unsigned char *tag, size_t tag_len) {
|
||||
unsigned char check_tag[16];
|
||||
int ret;
|
||||
|
||||
if (NULL == ctx || NULL == ctx->cipher_info ||
|
||||
MBEDTLS_DECRYPT != ctx->operation) {
|
||||
MBEDTLS_DECRYPT != ctx->operation) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -912,8 +886,7 @@ int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx,
|
|||
int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *iv, size_t iv_len,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t *olen) {
|
||||
int ret;
|
||||
size_t finish_olen;
|
||||
|
||||
|
@ -943,8 +916,7 @@ int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
|
|||
const unsigned char *ad, size_t ad_len,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen,
|
||||
unsigned char *tag, size_t tag_len)
|
||||
{
|
||||
unsigned char *tag, size_t tag_len) {
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
|
||||
*olen = ilen;
|
||||
|
@ -965,7 +937,7 @@ int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t *ctx,
|
|||
if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ctx->cipher_info->type) {
|
||||
/* ChachaPoly has fixed length nonce and MAC (tag) */
|
||||
if ((iv_len != ctx->cipher_info->iv_size) ||
|
||||
(tag_len != 16U)) {
|
||||
(tag_len != 16U)) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -986,8 +958,7 @@ int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
|
|||
const unsigned char *ad, size_t ad_len,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen,
|
||||
const unsigned char *tag, size_t tag_len)
|
||||
{
|
||||
const unsigned char *tag, size_t tag_len) {
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
if (MBEDTLS_MODE_GCM == ctx->cipher_info->mode) {
|
||||
int ret;
|
||||
|
@ -1024,7 +995,7 @@ int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t *ctx,
|
|||
|
||||
/* ChachaPoly has fixed length nonce and MAC (tag) */
|
||||
if ((iv_len != ctx->cipher_info->iv_size) ||
|
||||
(tag_len != 16U)) {
|
||||
(tag_len != 16U)) {
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
|
|
@ -413,8 +413,7 @@ int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_inf
|
|||
* \return The size of the blocks of the cipher.
|
||||
* \return 0 if \p ctx has not been initialized.
|
||||
*/
|
||||
static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return 0;
|
||||
|
||||
|
@ -430,8 +429,7 @@ static inline unsigned int mbedtls_cipher_get_block_size(const mbedtls_cipher_co
|
|||
* \return The mode of operation.
|
||||
* \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
|
||||
*/
|
||||
static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return MBEDTLS_MODE_NONE;
|
||||
|
||||
|
@ -448,8 +446,7 @@ static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(const mbedtls
|
|||
* \return \c 0 for ciphers not using an IV or a nonce.
|
||||
* \return The actual size if an IV has been set.
|
||||
*/
|
||||
static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return 0;
|
||||
|
||||
|
@ -467,8 +464,7 @@ static inline int mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t *ctx
|
|||
* \return The type of the cipher.
|
||||
* \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
|
||||
*/
|
||||
static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return MBEDTLS_CIPHER_NONE;
|
||||
|
||||
|
@ -484,8 +480,7 @@ static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(const mbedtls_cipher
|
|||
* \return The name of the cipher.
|
||||
* \return NULL if \p ctx has not been not initialized.
|
||||
*/
|
||||
static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return 0;
|
||||
|
||||
|
@ -501,8 +496,7 @@ static inline const char *mbedtls_cipher_get_name(const mbedtls_cipher_context_t
|
|||
* \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
|
||||
* initialized.
|
||||
*/
|
||||
static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return MBEDTLS_KEY_LENGTH_NONE;
|
||||
|
||||
|
@ -517,8 +511,7 @@ static inline int mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t *
|
|||
* \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
|
||||
* \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
|
||||
*/
|
||||
static inline mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
static inline mbedtls_operation_t mbedtls_cipher_get_operation(const mbedtls_cipher_context_t *ctx) {
|
||||
if (NULL == ctx || NULL == ctx->cipher_info)
|
||||
return MBEDTLS_OPERATION_NONE;
|
||||
|
||||
|
|
|
@ -89,8 +89,7 @@
|
|||
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
/* shared by all GCM ciphers */
|
||||
static void *gcm_ctx_alloc(void)
|
||||
{
|
||||
static void *gcm_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_gcm_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -99,8 +98,7 @@ static void *gcm_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void gcm_ctx_free(void *ctx)
|
||||
{
|
||||
static void gcm_ctx_free(void *ctx) {
|
||||
mbedtls_gcm_free(ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -108,8 +106,7 @@ static void gcm_ctx_free(void *ctx)
|
|||
|
||||
#if defined(MBEDTLS_CCM_C)
|
||||
/* shared by all CCM ciphers */
|
||||
static void *ccm_ctx_alloc(void)
|
||||
{
|
||||
static void *ccm_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ccm_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -118,8 +115,7 @@ static void *ccm_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void ccm_ctx_free(void *ctx)
|
||||
{
|
||||
static void ccm_ctx_free(void *ctx) {
|
||||
mbedtls_ccm_free(ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -128,15 +124,13 @@ static void ccm_ctx_free(void *ctx)
|
|||
#if defined(MBEDTLS_AES_C)
|
||||
|
||||
static int aes_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aes_crypt_ecb((mbedtls_aes_context *) ctx, operation, input, output);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aes_crypt_cbc((mbedtls_aes_context *) ctx, operation, length, iv, input,
|
||||
output);
|
||||
}
|
||||
|
@ -145,8 +139,7 @@ static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t l
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, size_t *iv_off, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aes_crypt_cfb128((mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
|
||||
input, output);
|
||||
}
|
||||
|
@ -154,8 +147,7 @@ static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
||||
static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aes_crypt_ofb((mbedtls_aes_context *) ctx, length, iv_off,
|
||||
iv, input, output);
|
||||
}
|
||||
|
@ -164,8 +156,7 @@ static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
static int aes_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
|
||||
unsigned char *nonce_counter, unsigned char *stream_block,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aes_crypt_ctr((mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
|
||||
stream_block, input, output);
|
||||
}
|
||||
|
@ -176,8 +167,7 @@ static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
size_t length,
|
||||
const unsigned char data_unit[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
mbedtls_aes_xts_context *xts_ctx = ctx;
|
||||
int mode;
|
||||
|
||||
|
@ -198,19 +188,16 @@ static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
|
||||
static int aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_aes_setkey_dec((mbedtls_aes_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static int aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_aes_setkey_enc((mbedtls_aes_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static void *aes_ctx_alloc(void)
|
||||
{
|
||||
static void *aes_ctx_alloc(void) {
|
||||
mbedtls_aes_context *aes = mbedtls_calloc(1, sizeof(mbedtls_aes_context));
|
||||
|
||||
if (aes == NULL)
|
||||
|
@ -221,8 +208,7 @@ static void *aes_ctx_alloc(void)
|
|||
return (aes);
|
||||
}
|
||||
|
||||
static void aes_ctx_free(void *ctx)
|
||||
{
|
||||
static void aes_ctx_free(void *ctx) {
|
||||
mbedtls_aes_free((mbedtls_aes_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -429,21 +415,18 @@ static const mbedtls_cipher_info_t aes_256_ctr_info = {
|
|||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
static int xts_aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
mbedtls_aes_xts_context *xts_ctx = ctx;
|
||||
return (mbedtls_aes_xts_setkey_enc(xts_ctx, key, key_bitlen));
|
||||
}
|
||||
|
||||
static int xts_aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
mbedtls_aes_xts_context *xts_ctx = ctx;
|
||||
return (mbedtls_aes_xts_setkey_dec(xts_ctx, key, key_bitlen));
|
||||
}
|
||||
|
||||
static void *xts_aes_ctx_alloc(void)
|
||||
{
|
||||
static void *xts_aes_ctx_alloc(void) {
|
||||
mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc(1, sizeof(*xts_ctx));
|
||||
|
||||
if (xts_ctx != NULL)
|
||||
|
@ -452,8 +435,7 @@ static void *xts_aes_ctx_alloc(void)
|
|||
return (xts_ctx);
|
||||
}
|
||||
|
||||
static void xts_aes_ctx_free(void *ctx)
|
||||
{
|
||||
static void xts_aes_ctx_free(void *ctx) {
|
||||
mbedtls_aes_xts_context *xts_ctx = ctx;
|
||||
|
||||
if (xts_ctx == NULL)
|
||||
|
@ -515,8 +497,7 @@ static const mbedtls_cipher_info_t aes_256_xts_info = {
|
|||
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -584,8 +565,7 @@ static const mbedtls_cipher_info_t aes_256_gcm_info = {
|
|||
|
||||
#if defined(MBEDTLS_CCM_C)
|
||||
static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -656,8 +636,7 @@ static const mbedtls_cipher_info_t aes_256_ccm_info = {
|
|||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
|
||||
static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_camellia_crypt_ecb((mbedtls_camellia_context *) ctx, operation, input,
|
||||
output);
|
||||
}
|
||||
|
@ -665,8 +644,7 @@ static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_camellia_crypt_cbc((mbedtls_camellia_context *) ctx, operation, length, iv,
|
||||
input, output);
|
||||
}
|
||||
|
@ -675,8 +653,7 @@ static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, size_t *iv_off, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_camellia_crypt_cfb128((mbedtls_camellia_context *) ctx, operation, length,
|
||||
iv_off, iv, input, output);
|
||||
}
|
||||
|
@ -685,27 +662,23 @@ static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
static int camellia_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
|
||||
unsigned char *nonce_counter, unsigned char *stream_block,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_camellia_crypt_ctr((mbedtls_camellia_context *) ctx, length, nc_off,
|
||||
nonce_counter, stream_block, input, output);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
static int camellia_setkey_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_camellia_setkey_dec((mbedtls_camellia_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static int camellia_setkey_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_camellia_setkey_enc((mbedtls_camellia_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static void *camellia_ctx_alloc(void)
|
||||
{
|
||||
static void *camellia_ctx_alloc(void) {
|
||||
mbedtls_camellia_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_camellia_context));
|
||||
|
||||
|
@ -717,8 +690,7 @@ static void *camellia_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void camellia_ctx_free(void *ctx)
|
||||
{
|
||||
static void camellia_ctx_free(void *ctx) {
|
||||
mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -890,8 +862,7 @@ static const mbedtls_cipher_info_t camellia_256_ctr_info = {
|
|||
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
static int gcm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -959,8 +930,7 @@ static const mbedtls_cipher_info_t camellia_256_gcm_info = {
|
|||
|
||||
#if defined(MBEDTLS_CCM_C)
|
||||
static int ccm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -1031,8 +1001,7 @@ static const mbedtls_cipher_info_t camellia_256_ccm_info = {
|
|||
#if defined(MBEDTLS_ARIA_C)
|
||||
|
||||
static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
(void) operation;
|
||||
return mbedtls_aria_crypt_ecb((mbedtls_aria_context *) ctx, input,
|
||||
output);
|
||||
|
@ -1041,8 +1010,7 @@ static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aria_crypt_cbc((mbedtls_aria_context *) ctx, operation, length, iv,
|
||||
input, output);
|
||||
}
|
||||
|
@ -1051,8 +1019,7 @@ static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, size_t *iv_off, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aria_crypt_cfb128((mbedtls_aria_context *) ctx, operation, length,
|
||||
iv_off, iv, input, output);
|
||||
}
|
||||
|
@ -1061,27 +1028,23 @@ static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
static int aria_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
|
||||
unsigned char *nonce_counter, unsigned char *stream_block,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_aria_crypt_ctr((mbedtls_aria_context *) ctx, length, nc_off,
|
||||
nonce_counter, stream_block, input, output);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
static int aria_setkey_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_aria_setkey_dec((mbedtls_aria_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static int aria_setkey_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_aria_setkey_enc((mbedtls_aria_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static void *aria_ctx_alloc(void)
|
||||
{
|
||||
static void *aria_ctx_alloc(void) {
|
||||
mbedtls_aria_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_aria_context));
|
||||
|
||||
|
@ -1093,8 +1056,7 @@ static void *aria_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void aria_ctx_free(void *ctx)
|
||||
{
|
||||
static void aria_ctx_free(void *ctx) {
|
||||
mbedtls_aria_free((mbedtls_aria_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -1266,8 +1228,7 @@ static const mbedtls_cipher_info_t aria_256_ctr_info = {
|
|||
|
||||
#if defined(MBEDTLS_GCM_C)
|
||||
static int gcm_aria_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -1335,8 +1296,7 @@ static const mbedtls_cipher_info_t aria_256_gcm_info = {
|
|||
|
||||
#if defined(MBEDTLS_CCM_C)
|
||||
static int ccm_aria_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
|
||||
key, key_bitlen);
|
||||
}
|
||||
|
@ -1407,23 +1367,20 @@ static const mbedtls_cipher_info_t aria_256_ccm_info = {
|
|||
#if defined(MBEDTLS_DES_C)
|
||||
|
||||
static int des_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
((void) operation);
|
||||
return mbedtls_des_crypt_ecb((mbedtls_des_context *) ctx, input, output);
|
||||
}
|
||||
|
||||
static int des3_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
((void) operation);
|
||||
return mbedtls_des3_crypt_ecb((mbedtls_des3_context *) ctx, input, output);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_des_crypt_cbc((mbedtls_des_context *) ctx, operation, length, iv, input,
|
||||
output);
|
||||
}
|
||||
|
@ -1431,63 +1388,55 @@ static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t l
|
|||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int des3_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
unsigned char *iv, const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_des3_crypt_cbc((mbedtls_des3_context *) ctx, operation, length, iv, input,
|
||||
output);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
||||
|
||||
static int des_setkey_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des_setkey_dec((mbedtls_des_context *) ctx, key);
|
||||
}
|
||||
|
||||
static int des_setkey_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des_setkey_enc((mbedtls_des_context *) ctx, key);
|
||||
}
|
||||
|
||||
static int des3_set2key_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des3_set2key_dec((mbedtls_des3_context *) ctx, key);
|
||||
}
|
||||
|
||||
static int des3_set2key_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des3_set2key_enc((mbedtls_des3_context *) ctx, key);
|
||||
}
|
||||
|
||||
static int des3_set3key_dec_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des3_set3key_dec((mbedtls_des3_context *) ctx, key);
|
||||
}
|
||||
|
||||
static int des3_set3key_enc_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) key_bitlen);
|
||||
|
||||
return mbedtls_des3_set3key_enc((mbedtls_des3_context *) ctx, key);
|
||||
}
|
||||
|
||||
static void *des_ctx_alloc(void)
|
||||
{
|
||||
static void *des_ctx_alloc(void) {
|
||||
mbedtls_des_context *des = mbedtls_calloc(1, sizeof(mbedtls_des_context));
|
||||
|
||||
if (des == NULL)
|
||||
|
@ -1498,14 +1447,12 @@ static void *des_ctx_alloc(void)
|
|||
return (des);
|
||||
}
|
||||
|
||||
static void des_ctx_free(void *ctx)
|
||||
{
|
||||
static void des_ctx_free(void *ctx) {
|
||||
mbedtls_des_free((mbedtls_des_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void *des3_ctx_alloc(void)
|
||||
{
|
||||
static void *des3_ctx_alloc(void) {
|
||||
mbedtls_des3_context *des3;
|
||||
des3 = mbedtls_calloc(1, sizeof(mbedtls_des3_context));
|
||||
|
||||
|
@ -1517,8 +1464,7 @@ static void *des3_ctx_alloc(void)
|
|||
return (des3);
|
||||
}
|
||||
|
||||
static void des3_ctx_free(void *ctx)
|
||||
{
|
||||
static void des3_ctx_free(void *ctx) {
|
||||
mbedtls_des3_free((mbedtls_des3_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -1679,8 +1625,7 @@ static const mbedtls_cipher_info_t des_ede3_cbc_info = {
|
|||
#if defined(MBEDTLS_BLOWFISH_C)
|
||||
|
||||
static int blowfish_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_blowfish_crypt_ecb((mbedtls_blowfish_context *) ctx, operation, input,
|
||||
output);
|
||||
}
|
||||
|
@ -1688,8 +1633,7 @@ static int blowfish_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
static int blowfish_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, unsigned char *iv, const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return mbedtls_blowfish_crypt_cbc((mbedtls_blowfish_context *) ctx, operation, length, iv,
|
||||
input, output);
|
||||
}
|
||||
|
@ -1698,8 +1642,7 @@ static int blowfish_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
static int blowfish_crypt_cfb64_wrap(void *ctx, mbedtls_operation_t operation,
|
||||
size_t length, size_t *iv_off, unsigned char *iv,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_blowfish_crypt_cfb64((mbedtls_blowfish_context *) ctx, operation, length,
|
||||
iv_off, iv, input, output);
|
||||
}
|
||||
|
@ -1708,21 +1651,18 @@ static int blowfish_crypt_cfb64_wrap(void *ctx, mbedtls_operation_t operation,
|
|||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
static int blowfish_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
|
||||
unsigned char *nonce_counter, unsigned char *stream_block,
|
||||
const unsigned char *input, unsigned char *output)
|
||||
{
|
||||
const unsigned char *input, unsigned char *output) {
|
||||
return mbedtls_blowfish_crypt_ctr((mbedtls_blowfish_context *) ctx, length, nc_off,
|
||||
nonce_counter, stream_block, input, output);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
static int blowfish_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
return mbedtls_blowfish_setkey((mbedtls_blowfish_context *) ctx, key, key_bitlen);
|
||||
}
|
||||
|
||||
static void *blowfish_ctx_alloc(void)
|
||||
{
|
||||
static void *blowfish_ctx_alloc(void) {
|
||||
mbedtls_blowfish_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_blowfish_context));
|
||||
|
||||
|
@ -1734,8 +1674,7 @@ static void *blowfish_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void blowfish_ctx_free(void *ctx)
|
||||
{
|
||||
static void blowfish_ctx_free(void *ctx) {
|
||||
mbedtls_blowfish_free((mbedtls_blowfish_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -1821,14 +1760,12 @@ static const mbedtls_cipher_info_t blowfish_ctr_info = {
|
|||
#if defined(MBEDTLS_ARC4_C)
|
||||
static int arc4_crypt_stream_wrap(void *ctx, size_t length,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return (mbedtls_arc4_crypt((mbedtls_arc4_context *) ctx, length, input, output));
|
||||
}
|
||||
|
||||
static int arc4_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
/* we get key_bitlen in bits, arc4 expects it in bytes */
|
||||
if (key_bitlen % 8 != 0)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
@ -1837,8 +1774,7 @@ static int arc4_setkey_wrap(void *ctx, const unsigned char *key,
|
|||
return (0);
|
||||
}
|
||||
|
||||
static void *arc4_ctx_alloc(void)
|
||||
{
|
||||
static void *arc4_ctx_alloc(void) {
|
||||
mbedtls_arc4_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_arc4_context));
|
||||
|
||||
|
@ -1850,8 +1786,7 @@ static void *arc4_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void arc4_ctx_free(void *ctx)
|
||||
{
|
||||
static void arc4_ctx_free(void *ctx) {
|
||||
mbedtls_arc4_free((mbedtls_arc4_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -1898,8 +1833,7 @@ static const mbedtls_cipher_info_t arc4_128_info = {
|
|||
#if defined(MBEDTLS_CHACHA20_C)
|
||||
|
||||
static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
if (key_bitlen != 256U)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -1911,8 +1845,7 @@ static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
|
|||
|
||||
static int chacha20_stream_wrap(void *ctx, size_t length,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret;
|
||||
|
||||
ret = mbedtls_chacha20_update(ctx, length, input, output);
|
||||
|
@ -1922,8 +1855,7 @@ static int chacha20_stream_wrap(void *ctx, size_t length,
|
|||
return (ret);
|
||||
}
|
||||
|
||||
static void *chacha20_ctx_alloc(void)
|
||||
{
|
||||
static void *chacha20_ctx_alloc(void) {
|
||||
mbedtls_chacha20_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_chacha20_context));
|
||||
|
||||
|
@ -1935,8 +1867,7 @@ static void *chacha20_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void chacha20_ctx_free(void *ctx)
|
||||
{
|
||||
static void chacha20_ctx_free(void *ctx) {
|
||||
mbedtls_chacha20_free((mbedtls_chacha20_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -1983,8 +1914,7 @@ static const mbedtls_cipher_info_t chacha20_info = {
|
|||
|
||||
static int chachapoly_setkey_wrap(void *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
if (key_bitlen != 256U)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -1994,8 +1924,7 @@ static int chachapoly_setkey_wrap(void *ctx,
|
|||
return (0);
|
||||
}
|
||||
|
||||
static void *chachapoly_ctx_alloc(void)
|
||||
{
|
||||
static void *chachapoly_ctx_alloc(void) {
|
||||
mbedtls_chachapoly_context *ctx;
|
||||
ctx = mbedtls_calloc(1, sizeof(mbedtls_chachapoly_context));
|
||||
|
||||
|
@ -2007,8 +1936,7 @@ static void *chachapoly_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void chachapoly_ctx_free(void *ctx)
|
||||
{
|
||||
static void chachapoly_ctx_free(void *ctx) {
|
||||
mbedtls_chachapoly_free((mbedtls_chachapoly_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -2054,16 +1982,14 @@ static const mbedtls_cipher_info_t chachapoly_info = {
|
|||
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
|
||||
static int null_crypt_stream(void *ctx, size_t length,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
((void) ctx);
|
||||
memmove(output, input, length);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int null_setkey(void *ctx, const unsigned char *key,
|
||||
unsigned int key_bitlen)
|
||||
{
|
||||
unsigned int key_bitlen) {
|
||||
((void) ctx);
|
||||
((void) key);
|
||||
((void) key_bitlen);
|
||||
|
@ -2071,13 +1997,11 @@ static int null_setkey(void *ctx, const unsigned char *key,
|
|||
return (0);
|
||||
}
|
||||
|
||||
static void *null_ctx_alloc(void)
|
||||
{
|
||||
static void *null_ctx_alloc(void) {
|
||||
return ((void *) 1);
|
||||
}
|
||||
|
||||
static void null_ctx_free(void *ctx)
|
||||
{
|
||||
static void null_ctx_free(void *ctx) {
|
||||
((void) ctx);
|
||||
}
|
||||
|
||||
|
|
|
@ -84,8 +84,7 @@
|
|||
*/
|
||||
static int cmac_multiply_by_u(unsigned char *output,
|
||||
const unsigned char *input,
|
||||
size_t blocksize)
|
||||
{
|
||||
size_t blocksize) {
|
||||
const unsigned char R_128 = 0x87;
|
||||
const unsigned char R_64 = 0x1B;
|
||||
unsigned char R_n, mask;
|
||||
|
@ -130,8 +129,7 @@ static int cmac_multiply_by_u(unsigned char *output,
|
|||
* - as specified by RFC 4493, section 2.3 Subkey Generation Algorithm
|
||||
*/
|
||||
static int cmac_generate_subkeys(mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *K1, unsigned char *K2)
|
||||
{
|
||||
unsigned char *K1, unsigned char *K2) {
|
||||
int ret;
|
||||
unsigned char L[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
||||
size_t olen, block_size;
|
||||
|
@ -163,8 +161,7 @@ exit:
|
|||
#if !defined(MBEDTLS_CMAC_ALT)
|
||||
static void cmac_xor_block(unsigned char *output, const unsigned char *input1,
|
||||
const unsigned char *input2,
|
||||
const size_t block_size)
|
||||
{
|
||||
const size_t block_size) {
|
||||
size_t idx;
|
||||
|
||||
for (idx = 0; idx < block_size; idx++)
|
||||
|
@ -180,8 +177,7 @@ static void cmac_xor_block(unsigned char *output, const unsigned char *input1,
|
|||
static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
|
||||
size_t padded_block_len,
|
||||
const unsigned char *last_block,
|
||||
size_t last_block_len)
|
||||
{
|
||||
size_t last_block_len) {
|
||||
size_t j;
|
||||
|
||||
for (j = 0; j < padded_block_len; j++) {
|
||||
|
@ -195,8 +191,7 @@ static void cmac_pad(unsigned char padded_block[MBEDTLS_CIPHER_BLKSIZE_MAX],
|
|||
}
|
||||
|
||||
int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *key, size_t keybits)
|
||||
{
|
||||
const unsigned char *key, size_t keybits) {
|
||||
mbedtls_cipher_type_t type;
|
||||
mbedtls_cmac_context_t *cmac_ctx;
|
||||
int retval;
|
||||
|
@ -234,15 +229,14 @@ int mbedtls_cipher_cmac_starts(mbedtls_cipher_context_t *ctx,
|
|||
}
|
||||
|
||||
int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *input, size_t ilen)
|
||||
{
|
||||
const unsigned char *input, size_t ilen) {
|
||||
mbedtls_cmac_context_t *cmac_ctx;
|
||||
unsigned char *state;
|
||||
int ret = 0;
|
||||
size_t n, j, olen, block_size;
|
||||
|
||||
if (ctx == NULL || ctx->cipher_info == NULL || input == NULL ||
|
||||
ctx->cmac_ctx == NULL)
|
||||
ctx->cmac_ctx == NULL)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
cmac_ctx = ctx->cmac_ctx;
|
||||
|
@ -252,7 +246,7 @@ int mbedtls_cipher_cmac_update(mbedtls_cipher_context_t *ctx,
|
|||
/* Is there data still to process from the last call, that's greater in
|
||||
* size than a block? */
|
||||
if (cmac_ctx->unprocessed_len > 0 &&
|
||||
ilen > block_size - cmac_ctx->unprocessed_len) {
|
||||
ilen > block_size - cmac_ctx->unprocessed_len) {
|
||||
memcpy(&cmac_ctx->unprocessed_block[cmac_ctx->unprocessed_len],
|
||||
input,
|
||||
block_size - cmac_ctx->unprocessed_len);
|
||||
|
@ -298,8 +292,7 @@ exit:
|
|||
}
|
||||
|
||||
int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
mbedtls_cmac_context_t *cmac_ctx;
|
||||
unsigned char *state, *last_block;
|
||||
unsigned char K1[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
||||
|
@ -309,7 +302,7 @@ int mbedtls_cipher_cmac_finish(mbedtls_cipher_context_t *ctx,
|
|||
size_t olen, block_size;
|
||||
|
||||
if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL ||
|
||||
output == NULL)
|
||||
output == NULL)
|
||||
return (MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
|
||||
|
||||
cmac_ctx = ctx->cmac_ctx;
|
||||
|
@ -354,8 +347,7 @@ exit:
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
|
||||
{
|
||||
int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx) {
|
||||
mbedtls_cmac_context_t *cmac_ctx;
|
||||
|
||||
if (ctx == NULL || ctx->cipher_info == NULL || ctx->cmac_ctx == NULL)
|
||||
|
@ -376,8 +368,7 @@ int mbedtls_cipher_cmac_reset(mbedtls_cipher_context_t *ctx)
|
|||
int mbedtls_cipher_cmac(const mbedtls_cipher_info_t *cipher_info,
|
||||
const unsigned char *key, size_t keylen,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
mbedtls_cipher_context_t ctx;
|
||||
int ret;
|
||||
|
||||
|
@ -411,8 +402,7 @@ exit:
|
|||
*/
|
||||
int mbedtls_aes_cmac_prf_128(const unsigned char *key, size_t key_length,
|
||||
const unsigned char *input, size_t in_len,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret;
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
unsigned char zero_key[MBEDTLS_AES_BLOCK_SIZE];
|
||||
|
@ -747,8 +737,7 @@ static int cmac_test_subkeys(int verbose,
|
|||
const unsigned char *subkeys,
|
||||
mbedtls_cipher_type_t cipher_type,
|
||||
int block_size,
|
||||
int num_tests)
|
||||
{
|
||||
int num_tests) {
|
||||
int i, ret = 0;
|
||||
mbedtls_cipher_context_t ctx;
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
|
@ -791,7 +780,7 @@ static int cmac_test_subkeys(int verbose,
|
|||
}
|
||||
|
||||
if ((ret = memcmp(K1, subkeys, block_size)) != 0 ||
|
||||
(ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) {
|
||||
(ret = memcmp(K2, &subkeys[block_size], block_size)) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
@ -823,8 +812,7 @@ static int cmac_test_wth_cipher(int verbose,
|
|||
const unsigned char *expected_result,
|
||||
mbedtls_cipher_type_t cipher_type,
|
||||
int block_size,
|
||||
int num_tests)
|
||||
{
|
||||
int num_tests) {
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
int i, ret = 0;
|
||||
unsigned char output[MBEDTLS_CIPHER_BLKSIZE_MAX];
|
||||
|
@ -863,8 +851,7 @@ exit:
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
static int test_aes128_cmac_prf(int verbose)
|
||||
{
|
||||
static int test_aes128_cmac_prf(int verbose) {
|
||||
int i;
|
||||
int ret;
|
||||
unsigned char output[MBEDTLS_AES_BLOCK_SIZE];
|
||||
|
@ -873,7 +860,7 @@ static int test_aes128_cmac_prf(int verbose)
|
|||
mbedtls_printf(" AES CMAC 128 PRF #%u: ", i);
|
||||
ret = mbedtls_aes_cmac_prf_128(PRFK, PRFKlen[i], PRFM, 20, output);
|
||||
if (ret != 0 ||
|
||||
memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) {
|
||||
memcmp(output, PRFT[i], MBEDTLS_AES_BLOCK_SIZE) != 0) {
|
||||
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
@ -887,8 +874,7 @@ static int test_aes128_cmac_prf(int verbose)
|
|||
}
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
|
||||
int mbedtls_cmac_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_cmac_self_test(int verbose) {
|
||||
int ret;
|
||||
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
|
|
|
@ -55,8 +55,7 @@
|
|||
/*
|
||||
* CTR_DRBG context initialization
|
||||
*/
|
||||
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
|
||||
{
|
||||
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_ctr_drbg_context));
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
|
@ -74,8 +73,7 @@ int mbedtls_ctr_drbg_seed_entropy_len(
|
|||
void *p_entropy,
|
||||
const unsigned char *custom,
|
||||
size_t len,
|
||||
size_t entropy_len)
|
||||
{
|
||||
size_t entropy_len) {
|
||||
int ret;
|
||||
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
|
||||
|
||||
|
@ -106,14 +104,12 @@ int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
|
|||
int (*f_entropy)(void *, unsigned char *, size_t),
|
||||
void *p_entropy,
|
||||
const unsigned char *custom,
|
||||
size_t len)
|
||||
{
|
||||
size_t len) {
|
||||
return (mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy, custom, len,
|
||||
MBEDTLS_CTR_DRBG_ENTROPY_LEN));
|
||||
}
|
||||
|
||||
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
|
||||
{
|
||||
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -124,24 +120,20 @@ void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
|
|||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ctr_drbg_context));
|
||||
}
|
||||
|
||||
void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance)
|
||||
{
|
||||
void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance) {
|
||||
ctx->prediction_resistance = resistance;
|
||||
}
|
||||
|
||||
void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, size_t len)
|
||||
{
|
||||
void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, size_t len) {
|
||||
ctx->entropy_len = len;
|
||||
}
|
||||
|
||||
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval)
|
||||
{
|
||||
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval) {
|
||||
ctx->reseed_interval = interval;
|
||||
}
|
||||
|
||||
static int block_cipher_df(unsigned char *output,
|
||||
const unsigned char *data, size_t data_len)
|
||||
{
|
||||
const unsigned char *data, size_t data_len) {
|
||||
unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
|
||||
unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
|
||||
unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
|
||||
|
@ -249,8 +241,7 @@ exit:
|
|||
}
|
||||
|
||||
static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx,
|
||||
const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN])
|
||||
{
|
||||
const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN]) {
|
||||
unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
|
||||
unsigned char *p = tmp;
|
||||
int i, j;
|
||||
|
@ -291,8 +282,7 @@ static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx,
|
|||
}
|
||||
|
||||
void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
|
||||
const unsigned char *additional, size_t add_len)
|
||||
{
|
||||
const unsigned char *additional, size_t add_len) {
|
||||
unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
|
||||
|
||||
if (add_len > 0) {
|
||||
|
@ -307,14 +297,13 @@ void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
|
|||
}
|
||||
|
||||
int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
|
||||
const unsigned char *additional, size_t len)
|
||||
{
|
||||
const unsigned char *additional, size_t len) {
|
||||
unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
|
||||
size_t seedlen = 0;
|
||||
int ret;
|
||||
|
||||
if (ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
|
||||
len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len)
|
||||
len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len)
|
||||
return (MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
|
||||
|
||||
memset(seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT);
|
||||
|
@ -357,8 +346,7 @@ int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
|
|||
|
||||
int mbedtls_ctr_drbg_random_with_add(void *p_rng,
|
||||
unsigned char *output, size_t output_len,
|
||||
const unsigned char *additional, size_t add_len)
|
||||
{
|
||||
const unsigned char *additional, size_t add_len) {
|
||||
int ret = 0;
|
||||
mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
|
||||
unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
|
||||
|
@ -376,7 +364,7 @@ int mbedtls_ctr_drbg_random_with_add(void *p_rng,
|
|||
memset(add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN);
|
||||
|
||||
if (ctx->reseed_counter > ctx->reseed_interval ||
|
||||
ctx->prediction_resistance) {
|
||||
ctx->prediction_resistance) {
|
||||
if ((ret = mbedtls_ctr_drbg_reseed(ctx, additional, add_len)) != 0) {
|
||||
return (ret);
|
||||
}
|
||||
|
@ -426,8 +414,7 @@ int mbedtls_ctr_drbg_random_with_add(void *p_rng,
|
|||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
|
||||
{
|
||||
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len) {
|
||||
int ret;
|
||||
mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
|
||||
|
||||
|
@ -447,8 +434,7 @@ int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_le
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
|
||||
{
|
||||
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path) {
|
||||
int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
|
||||
FILE *f;
|
||||
unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
|
||||
|
@ -471,8 +457,7 @@ exit:
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
|
||||
{
|
||||
int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path) {
|
||||
int ret = 0;
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
@ -556,8 +541,7 @@ static const unsigned char result_nopr[16] = {
|
|||
|
||||
static size_t test_offset;
|
||||
static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf,
|
||||
size_t len)
|
||||
{
|
||||
size_t len) {
|
||||
const unsigned char *p = data;
|
||||
memcpy(buf, p + test_offset, len);
|
||||
test_offset += len;
|
||||
|
@ -574,8 +558,7 @@ static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf,
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_ctr_drbg_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_ctr_drbg_self_test(int verbose) {
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[16];
|
||||
|
||||
|
|
|
@ -294,26 +294,22 @@ static const uint32_t RHs[16] = {
|
|||
|
||||
#define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
|
||||
|
||||
void mbedtls_des_init(mbedtls_des_context *ctx)
|
||||
{
|
||||
void mbedtls_des_init(mbedtls_des_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_des_context));
|
||||
}
|
||||
|
||||
void mbedtls_des_free(mbedtls_des_context *ctx)
|
||||
{
|
||||
void mbedtls_des_free(mbedtls_des_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_des_context));
|
||||
}
|
||||
|
||||
void mbedtls_des3_init(mbedtls_des3_context *ctx)
|
||||
{
|
||||
void mbedtls_des3_init(mbedtls_des3_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_des3_context));
|
||||
}
|
||||
|
||||
void mbedtls_des3_free(mbedtls_des3_context *ctx)
|
||||
{
|
||||
void mbedtls_des3_free(mbedtls_des3_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -332,8 +328,7 @@ static const unsigned char odd_parity_table[128] = { 1, 2, 4, 7, 8,
|
|||
254
|
||||
};
|
||||
|
||||
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MBEDTLS_DES_KEY_SIZE; i++)
|
||||
|
@ -343,8 +338,7 @@ void mbedtls_des_key_set_parity(unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
|||
/*
|
||||
* Check the given key's parity, returns 1 on failure, 0 on SUCCESS
|
||||
*/
|
||||
int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
int mbedtls_des_key_check_key_parity(const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MBEDTLS_DES_KEY_SIZE; i++)
|
||||
|
@ -397,8 +391,7 @@ static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE]
|
|||
{ 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
|
||||
};
|
||||
|
||||
int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
int i;
|
||||
|
||||
for (i = 0; i < WEAK_KEY_COUNT; i++)
|
||||
|
@ -409,8 +402,7 @@ int mbedtls_des_key_check_weak(const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
|||
}
|
||||
|
||||
#if !defined(MBEDTLS_DES_SETKEY_ALT)
|
||||
void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
int i;
|
||||
uint32_t X, Y, T;
|
||||
|
||||
|
@ -482,8 +474,7 @@ void mbedtls_des_setkey(uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY
|
|||
/*
|
||||
* DES key schedule (56-bit, encryption)
|
||||
*/
|
||||
int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
mbedtls_des_setkey(ctx->sk, key);
|
||||
|
||||
return (0);
|
||||
|
@ -492,8 +483,7 @@ int mbedtls_des_setkey_enc(mbedtls_des_context *ctx, const unsigned char key[MBE
|
|||
/*
|
||||
* DES key schedule (56-bit, decryption)
|
||||
*/
|
||||
int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE])
|
||||
{
|
||||
int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE]) {
|
||||
int i;
|
||||
|
||||
mbedtls_des_setkey(ctx->sk, key);
|
||||
|
@ -508,8 +498,7 @@ int mbedtls_des_setkey_dec(mbedtls_des_context *ctx, const unsigned char key[MBE
|
|||
|
||||
static void des3_set2key(uint32_t esk[96],
|
||||
uint32_t dsk[96],
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
|
||||
{
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
|
||||
int i;
|
||||
|
||||
mbedtls_des_setkey(esk, key);
|
||||
|
@ -534,8 +523,7 @@ static void des3_set2key(uint32_t esk[96],
|
|||
* Triple-DES key schedule (112-bit, encryption)
|
||||
*/
|
||||
int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
|
||||
{
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
|
||||
uint32_t sk[96];
|
||||
|
||||
des3_set2key(ctx->sk, sk, key);
|
||||
|
@ -548,8 +536,7 @@ int mbedtls_des3_set2key_enc(mbedtls_des3_context *ctx,
|
|||
* Triple-DES key schedule (112-bit, decryption)
|
||||
*/
|
||||
int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2])
|
||||
{
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2]) {
|
||||
uint32_t sk[96];
|
||||
|
||||
des3_set2key(sk, ctx->sk, key);
|
||||
|
@ -560,8 +547,7 @@ int mbedtls_des3_set2key_dec(mbedtls_des3_context *ctx,
|
|||
|
||||
static void des3_set3key(uint32_t esk[96],
|
||||
uint32_t dsk[96],
|
||||
const unsigned char key[24])
|
||||
{
|
||||
const unsigned char key[24]) {
|
||||
int i;
|
||||
|
||||
mbedtls_des_setkey(esk, key);
|
||||
|
@ -584,8 +570,7 @@ static void des3_set3key(uint32_t esk[96],
|
|||
* Triple-DES key schedule (168-bit, encryption)
|
||||
*/
|
||||
int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3])
|
||||
{
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]) {
|
||||
uint32_t sk[96];
|
||||
|
||||
des3_set3key(ctx->sk, sk, key);
|
||||
|
@ -598,8 +583,7 @@ int mbedtls_des3_set3key_enc(mbedtls_des3_context *ctx,
|
|||
* Triple-DES key schedule (168-bit, decryption)
|
||||
*/
|
||||
int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3])
|
||||
{
|
||||
const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3]) {
|
||||
uint32_t sk[96];
|
||||
|
||||
des3_set3key(sk, ctx->sk, key);
|
||||
|
@ -614,8 +598,7 @@ int mbedtls_des3_set3key_dec(mbedtls_des3_context *ctx,
|
|||
#if !defined(MBEDTLS_DES_CRYPT_ECB_ALT)
|
||||
int mbedtls_des_crypt_ecb(mbedtls_des_context *ctx,
|
||||
const unsigned char input[8],
|
||||
unsigned char output[8])
|
||||
{
|
||||
unsigned char output[8]) {
|
||||
int i;
|
||||
uint32_t X, Y, T, *SK;
|
||||
|
||||
|
@ -649,8 +632,7 @@ int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[8],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int i;
|
||||
unsigned char temp[8];
|
||||
|
||||
|
@ -695,8 +677,7 @@ int mbedtls_des_crypt_cbc(mbedtls_des_context *ctx,
|
|||
#if !defined(MBEDTLS_DES3_CRYPT_ECB_ALT)
|
||||
int mbedtls_des3_crypt_ecb(mbedtls_des3_context *ctx,
|
||||
const unsigned char input[8],
|
||||
unsigned char output[8])
|
||||
{
|
||||
unsigned char output[8]) {
|
||||
int i;
|
||||
uint32_t X, Y, T, *SK;
|
||||
|
||||
|
@ -740,8 +721,7 @@ int mbedtls_des3_crypt_cbc(mbedtls_des3_context *ctx,
|
|||
size_t length,
|
||||
unsigned char iv[8],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int i;
|
||||
unsigned char temp[8];
|
||||
|
||||
|
@ -831,8 +811,7 @@ static const unsigned char des3_test_cbc_enc[3][8] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_des_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_des_self_test(int verbose) {
|
||||
int i, j, u, v, ret = 0;
|
||||
mbedtls_des_context ctx;
|
||||
mbedtls_des3_context ctx3;
|
||||
|
@ -895,9 +874,9 @@ int mbedtls_des_self_test(int verbose)
|
|||
}
|
||||
|
||||
if ((v == MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_ecb_dec[u], 8) != 0) ||
|
||||
(v != MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_ecb_enc[u], 8) != 0)) {
|
||||
memcmp(buf, des3_test_ecb_dec[u], 8) != 0) ||
|
||||
(v != MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_ecb_enc[u], 8) != 0)) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
@ -983,9 +962,9 @@ int mbedtls_des_self_test(int verbose)
|
|||
}
|
||||
|
||||
if ((v == MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_cbc_dec[u], 8) != 0) ||
|
||||
(v != MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_cbc_enc[u], 8) != 0)) {
|
||||
memcmp(buf, des3_test_cbc_dec[u], 8) != 0) ||
|
||||
(v != MBEDTLS_DES_DECRYPT &&
|
||||
memcmp(buf, des3_test_cbc_enc[u], 8) != 0)) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
|
|
@ -49,8 +49,7 @@
|
|||
* SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
|
||||
*/
|
||||
static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
|
||||
const unsigned char *buf, size_t blen)
|
||||
{
|
||||
const unsigned char *buf, size_t blen) {
|
||||
int ret;
|
||||
size_t n_size = (grp->nbits + 7) / 8;
|
||||
size_t use_size = blen > n_size ? n_size : blen;
|
||||
|
@ -74,8 +73,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret, key_tries, sign_tries, blind_tries;
|
||||
mbedtls_ecp_point R;
|
||||
mbedtls_mpi k, e, t;
|
||||
|
@ -164,8 +162,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecdsa_sign_det(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
||||
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
||||
mbedtls_md_type_t md_alg)
|
||||
{
|
||||
mbedtls_md_type_t md_alg) {
|
||||
int ret;
|
||||
mbedtls_hmac_drbg_context rng_ctx;
|
||||
unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
|
||||
|
@ -203,8 +200,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
|
||||
const unsigned char *buf, size_t blen,
|
||||
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
|
||||
{
|
||||
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) {
|
||||
int ret;
|
||||
mbedtls_mpi e, s_inv, u1, u2;
|
||||
mbedtls_ecp_point R;
|
||||
|
@ -223,7 +219,7 @@ int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
|
|||
* Step 1: make sure r and s are in range 1..n-1
|
||||
*/
|
||||
if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
|
||||
mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
|
||||
mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
|
||||
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -291,8 +287,7 @@ cleanup:
|
|||
* Convert a signature (given by context) to ASN.1
|
||||
*/
|
||||
static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
|
||||
unsigned char *sig, size_t *slen)
|
||||
{
|
||||
unsigned char *sig, size_t *slen) {
|
||||
int ret;
|
||||
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
|
||||
unsigned char *p = buf + sizeof(buf);
|
||||
|
@ -318,8 +313,7 @@ int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx, mbedtls_md_type_t
|
|||
const unsigned char *hash, size_t hlen,
|
||||
unsigned char *sig, size_t *slen,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_mpi r, s;
|
||||
|
||||
|
@ -353,8 +347,7 @@ cleanup:
|
|||
int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx,
|
||||
const unsigned char *hash, size_t hlen,
|
||||
unsigned char *sig, size_t *slen,
|
||||
mbedtls_md_type_t md_alg)
|
||||
{
|
||||
mbedtls_md_type_t md_alg) {
|
||||
return (mbedtls_ecdsa_write_signature(ctx, md_alg, hash, hlen, sig, slen,
|
||||
NULL, NULL));
|
||||
}
|
||||
|
@ -365,8 +358,7 @@ int mbedtls_ecdsa_write_signature_det(mbedtls_ecdsa_context *ctx,
|
|||
*/
|
||||
int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
|
||||
const unsigned char *hash, size_t hlen,
|
||||
const unsigned char *sig, size_t slen)
|
||||
{
|
||||
const unsigned char *sig, size_t slen) {
|
||||
int ret;
|
||||
unsigned char *p = (unsigned char *) sig;
|
||||
const unsigned char *end = sig + slen;
|
||||
|
@ -389,7 +381,7 @@ int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
|
|||
}
|
||||
|
||||
if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
|
||||
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -416,8 +408,7 @@ cleanup:
|
|||
* Generate key pair
|
||||
*/
|
||||
int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
return (mbedtls_ecp_group_load(&ctx->grp, gid) ||
|
||||
mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng));
|
||||
}
|
||||
|
@ -426,13 +417,12 @@ int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
|
|||
/*
|
||||
* Set context from an mbedtls_ecp_keypair
|
||||
*/
|
||||
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
|
||||
{
|
||||
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
|
||||
(ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
|
||||
(ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
|
||||
(ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
|
||||
(ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
|
||||
mbedtls_ecdsa_free(ctx);
|
||||
}
|
||||
|
||||
|
@ -442,16 +432,14 @@ int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_key
|
|||
/*
|
||||
* Initialize context
|
||||
*/
|
||||
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
|
||||
{
|
||||
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx) {
|
||||
mbedtls_ecp_keypair_init(ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
* Free context
|
||||
*/
|
||||
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
|
||||
{
|
||||
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx) {
|
||||
mbedtls_ecp_keypair_free(ctx);
|
||||
}
|
||||
|
||||
|
|
|
@ -169,16 +169,14 @@ static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
|
|||
/*
|
||||
* List of supported curves and associated info
|
||||
*/
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
|
||||
{
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void) {
|
||||
return (ecp_supported_curves);
|
||||
}
|
||||
|
||||
/*
|
||||
* List of supported curves, group ID only
|
||||
*/
|
||||
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
|
||||
{
|
||||
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void) {
|
||||
static int init_done = 0;
|
||||
|
||||
if (! init_done) {
|
||||
|
@ -186,8 +184,8 @@ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
|
|||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
for (curve_info = mbedtls_ecp_curve_list();
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
ecp_supported_grp_id[i++] = curve_info->grp_id;
|
||||
}
|
||||
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
|
||||
|
@ -201,13 +199,12 @@ const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
|
|||
/*
|
||||
* Get the curve info for the internal identifier
|
||||
*/
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
|
||||
{
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id) {
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
for (curve_info = mbedtls_ecp_curve_list();
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
if (curve_info->grp_id == grp_id)
|
||||
return (curve_info);
|
||||
}
|
||||
|
@ -218,13 +215,12 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_gro
|
|||
/*
|
||||
* Get the curve info from the TLS identifier
|
||||
*/
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
|
||||
{
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id) {
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
for (curve_info = mbedtls_ecp_curve_list();
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
if (curve_info->tls_id == tls_id)
|
||||
return (curve_info);
|
||||
}
|
||||
|
@ -235,13 +231,12 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id
|
|||
/*
|
||||
* Get the curve info from the name
|
||||
*/
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
|
||||
{
|
||||
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name) {
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
for (curve_info = mbedtls_ecp_curve_list();
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
||||
curve_info++) {
|
||||
if (strcmp(curve_info->name, name) == 0)
|
||||
return (curve_info);
|
||||
}
|
||||
|
@ -252,8 +247,7 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
|
|||
/*
|
||||
* Get the type of a curve
|
||||
*/
|
||||
static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp)
|
||||
{
|
||||
static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp) {
|
||||
if (grp->G.X.p == NULL)
|
||||
return (ECP_TYPE_NONE);
|
||||
|
||||
|
@ -266,8 +260,7 @@ static inline ecp_curve_type ecp_get_type(const mbedtls_ecp_group *grp)
|
|||
/*
|
||||
* Initialize (the components of) a point
|
||||
*/
|
||||
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
|
||||
{
|
||||
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt) {
|
||||
if (pt == NULL)
|
||||
return;
|
||||
|
||||
|
@ -279,8 +272,7 @@ void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
|
|||
/*
|
||||
* Initialize (the components of) a group
|
||||
*/
|
||||
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
|
||||
{
|
||||
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp) {
|
||||
if (grp == NULL)
|
||||
return;
|
||||
|
||||
|
@ -290,8 +282,7 @@ void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
|
|||
/*
|
||||
* Initialize (the components of) a key pair
|
||||
*/
|
||||
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
|
||||
{
|
||||
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key) {
|
||||
if (key == NULL)
|
||||
return;
|
||||
|
||||
|
@ -303,8 +294,7 @@ void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
|
|||
/*
|
||||
* Unallocate (the components of) a point
|
||||
*/
|
||||
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
|
||||
{
|
||||
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt) {
|
||||
if (pt == NULL)
|
||||
return;
|
||||
|
||||
|
@ -316,8 +306,7 @@ void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
|
|||
/*
|
||||
* Unallocate (the components of) a group
|
||||
*/
|
||||
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
|
||||
{
|
||||
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp) {
|
||||
size_t i;
|
||||
|
||||
if (grp == NULL)
|
||||
|
@ -343,8 +332,7 @@ void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
|
|||
/*
|
||||
* Unallocate (the components of) a key pair
|
||||
*/
|
||||
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
|
||||
{
|
||||
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key) {
|
||||
if (key == NULL)
|
||||
return;
|
||||
|
||||
|
@ -356,8 +344,7 @@ void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
|
|||
/*
|
||||
* Copy the contents of a point
|
||||
*/
|
||||
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
|
||||
{
|
||||
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) {
|
||||
int ret;
|
||||
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
|
||||
|
@ -371,16 +358,14 @@ cleanup:
|
|||
/*
|
||||
* Copy the contents of a group object
|
||||
*/
|
||||
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
|
||||
{
|
||||
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src) {
|
||||
return mbedtls_ecp_group_load(dst, src->id);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set point to zero
|
||||
*/
|
||||
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
|
||||
{
|
||||
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt) {
|
||||
int ret;
|
||||
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
|
||||
|
@ -394,8 +379,7 @@ cleanup:
|
|||
/*
|
||||
* Tell if a point is zero
|
||||
*/
|
||||
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
|
||||
{
|
||||
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt) {
|
||||
return (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0);
|
||||
}
|
||||
|
||||
|
@ -403,11 +387,10 @@ int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
|
|||
* Compare two points lazyly
|
||||
*/
|
||||
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
|
||||
const mbedtls_ecp_point *Q)
|
||||
{
|
||||
const mbedtls_ecp_point *Q) {
|
||||
if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
|
||||
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -418,8 +401,7 @@ int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
|
|||
* Import a non-zero point from ASCII strings
|
||||
*/
|
||||
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
|
||||
const char *x, const char *y)
|
||||
{
|
||||
const char *x, const char *y) {
|
||||
int ret;
|
||||
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
|
||||
|
@ -435,13 +417,12 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
|
||||
int format, size_t *olen,
|
||||
unsigned char *buf, size_t buflen)
|
||||
{
|
||||
unsigned char *buf, size_t buflen) {
|
||||
int ret = 0;
|
||||
size_t plen;
|
||||
|
||||
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
|
||||
format != MBEDTLS_ECP_PF_COMPRESSED)
|
||||
format != MBEDTLS_ECP_PF_COMPRESSED)
|
||||
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
|
||||
|
||||
/*
|
||||
|
@ -486,8 +467,7 @@ cleanup:
|
|||
* Import a point from unsigned binary data (SEC1 2.3.4)
|
||||
*/
|
||||
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
||||
const unsigned char *buf, size_t ilen)
|
||||
{
|
||||
const unsigned char *buf, size_t ilen) {
|
||||
int ret;
|
||||
size_t plen;
|
||||
|
||||
|
@ -524,8 +504,7 @@ cleanup:
|
|||
* } ECPoint;
|
||||
*/
|
||||
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
||||
const unsigned char **buf, size_t buf_len)
|
||||
{
|
||||
const unsigned char **buf, size_t buf_len) {
|
||||
unsigned char data_len;
|
||||
const unsigned char *buf_start;
|
||||
|
||||
|
@ -556,8 +535,7 @@ int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, mbedtls_ecp_point *
|
|||
*/
|
||||
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
|
||||
int format, size_t *olen,
|
||||
unsigned char *buf, size_t blen)
|
||||
{
|
||||
unsigned char *buf, size_t blen) {
|
||||
int ret;
|
||||
|
||||
/*
|
||||
|
@ -582,8 +560,7 @@ int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_
|
|||
/*
|
||||
* Set a group from an ECParameters record (RFC 4492)
|
||||
*/
|
||||
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len)
|
||||
{
|
||||
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf, size_t len) {
|
||||
uint16_t tls_id;
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
|
@ -616,8 +593,7 @@ int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, const unsigned char **buf
|
|||
* Write the ECParameters record corresponding to a group (RFC 4492)
|
||||
*/
|
||||
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
|
||||
unsigned char *buf, size_t blen)
|
||||
{
|
||||
unsigned char *buf, size_t blen) {
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
|
||||
if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL)
|
||||
|
@ -650,8 +626,7 @@ int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
|
|||
*
|
||||
* This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
|
||||
*/
|
||||
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
|
||||
{
|
||||
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp) {
|
||||
int ret;
|
||||
|
||||
if (grp->modp == NULL)
|
||||
|
@ -659,7 +634,7 @@ static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
|
|||
|
||||
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
||||
if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
|
||||
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
|
||||
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
|
||||
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -729,8 +704,7 @@ cleanup:
|
|||
* Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
|
||||
* Cost: 1N := 1I + 3M + 1S
|
||||
*/
|
||||
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
|
||||
{
|
||||
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt) {
|
||||
int ret;
|
||||
mbedtls_mpi Zi, ZZi;
|
||||
|
||||
|
@ -787,8 +761,7 @@ cleanup:
|
|||
* Cost: 1N(t) := 1I + (6t - 3)M + 1S
|
||||
*/
|
||||
static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
|
||||
mbedtls_ecp_point *T[], size_t t_len)
|
||||
{
|
||||
mbedtls_ecp_point *T[], size_t t_len) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_mpi *c, u, Zi, ZZi;
|
||||
|
@ -881,8 +854,7 @@ cleanup:
|
|||
*/
|
||||
static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
|
||||
mbedtls_ecp_point *Q,
|
||||
unsigned char inv)
|
||||
{
|
||||
unsigned char inv) {
|
||||
int ret;
|
||||
unsigned char nonzero;
|
||||
mbedtls_mpi mQY;
|
||||
|
@ -915,8 +887,7 @@ cleanup:
|
|||
* 3M + 6S + 1a otherwise
|
||||
*/
|
||||
static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_ecp_point *P)
|
||||
{
|
||||
const mbedtls_ecp_point *P) {
|
||||
int ret;
|
||||
mbedtls_mpi M, S, T, U;
|
||||
|
||||
|
@ -1039,8 +1010,7 @@ cleanup:
|
|||
* Cost: 1A := 8M + 3S
|
||||
*/
|
||||
static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
|
||||
{
|
||||
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) {
|
||||
int ret;
|
||||
mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
|
||||
|
||||
|
@ -1151,8 +1121,7 @@ cleanup:
|
|||
* This countermeasure was first suggested in [2].
|
||||
*/
|
||||
static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_mpi l, ll;
|
||||
size_t p_size;
|
||||
|
@ -1236,8 +1205,7 @@ cleanup:
|
|||
* (the result will be incorrect if these assumptions are not satisfied)
|
||||
*/
|
||||
static void ecp_comb_fixed(unsigned char x[], size_t d,
|
||||
unsigned char w, const mbedtls_mpi *m)
|
||||
{
|
||||
unsigned char w, const mbedtls_mpi *m) {
|
||||
size_t i, j;
|
||||
unsigned char c, cc, adjust;
|
||||
|
||||
|
@ -1276,8 +1244,7 @@ static void ecp_comb_fixed(unsigned char x[], size_t d,
|
|||
*/
|
||||
static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
|
||||
mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
|
||||
unsigned char w, size_t d)
|
||||
{
|
||||
unsigned char w, size_t d) {
|
||||
int ret;
|
||||
unsigned char i, k;
|
||||
size_t j;
|
||||
|
@ -1326,8 +1293,7 @@ cleanup:
|
|||
*/
|
||||
static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_ecp_point T[], unsigned char t_len,
|
||||
unsigned char i)
|
||||
{
|
||||
unsigned char i) {
|
||||
int ret;
|
||||
unsigned char ii, j;
|
||||
|
||||
|
@ -1357,8 +1323,7 @@ static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||
const mbedtls_ecp_point T[], unsigned char t_len,
|
||||
const unsigned char x[], size_t d,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_ecp_point Txi;
|
||||
size_t i;
|
||||
|
@ -1392,8 +1357,7 @@ cleanup:
|
|||
static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
unsigned char w, m_is_odd, p_eq_g, pre_len, i;
|
||||
size_t d;
|
||||
|
@ -1521,8 +1485,7 @@ cleanup:
|
|||
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
|
||||
* Cost: 1M + 1I
|
||||
*/
|
||||
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
|
||||
{
|
||||
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P) {
|
||||
int ret;
|
||||
|
||||
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
|
||||
|
@ -1549,8 +1512,7 @@ cleanup:
|
|||
* Cost: 2M
|
||||
*/
|
||||
static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_mpi l;
|
||||
size_t p_size;
|
||||
|
@ -1605,8 +1567,7 @@ cleanup:
|
|||
static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
|
||||
mbedtls_ecp_point *R, mbedtls_ecp_point *S,
|
||||
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
|
||||
const mbedtls_mpi *d)
|
||||
{
|
||||
const mbedtls_mpi *d) {
|
||||
int ret;
|
||||
mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
|
||||
|
||||
|
@ -1684,8 +1645,7 @@ cleanup:
|
|||
static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
size_t i;
|
||||
unsigned char b;
|
||||
|
@ -1745,8 +1705,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
char is_grp_capable = 0;
|
||||
|
@ -1757,7 +1716,7 @@ int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|||
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
|
||||
|
||||
if ((ret = mbedtls_ecp_check_privkey(grp, m)) != 0 ||
|
||||
(ret = mbedtls_ecp_check_pubkey(grp, P)) != 0)
|
||||
(ret = mbedtls_ecp_check_pubkey(grp, P)) != 0)
|
||||
return (ret);
|
||||
|
||||
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
|
@ -1792,16 +1751,15 @@ cleanup:
|
|||
* Check that an affine point is valid as a public key,
|
||||
* short weierstrass curves (SEC1 3.2.3.1)
|
||||
*/
|
||||
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
|
||||
{
|
||||
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
|
||||
int ret;
|
||||
mbedtls_mpi YY, RHS;
|
||||
|
||||
/* pt coordinates must be normalized for our checks */
|
||||
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
|
||||
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0)
|
||||
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0)
|
||||
return (MBEDTLS_ERR_ECP_INVALID_KEY);
|
||||
|
||||
mbedtls_mpi_init(&YY);
|
||||
|
@ -1849,8 +1807,7 @@ cleanup:
|
|||
static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
|
||||
mbedtls_ecp_point *R,
|
||||
const mbedtls_mpi *m,
|
||||
const mbedtls_ecp_point *P)
|
||||
{
|
||||
const mbedtls_ecp_point *P) {
|
||||
int ret;
|
||||
|
||||
if (mbedtls_mpi_cmp_int(m, 1) == 0) {
|
||||
|
@ -1873,8 +1830,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
||||
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
||||
const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
|
||||
{
|
||||
const mbedtls_mpi *n, const mbedtls_ecp_point *Q) {
|
||||
int ret;
|
||||
mbedtls_ecp_point mP;
|
||||
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
||||
|
@ -1916,8 +1872,7 @@ cleanup:
|
|||
/*
|
||||
* Check validity of a public key for Montgomery curves with x-only schemes
|
||||
*/
|
||||
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
|
||||
{
|
||||
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
|
||||
/* [Curve25519 p. 5] Just check X is the correct number of bytes */
|
||||
/* Allow any public value, if it's too big then we'll just reduce it mod p
|
||||
* (RFC 7748 sec. 5 para. 3). */
|
||||
|
@ -1931,8 +1886,7 @@ static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_p
|
|||
/*
|
||||
* Check that a point is valid as a public key
|
||||
*/
|
||||
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
|
||||
{
|
||||
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) {
|
||||
/* Must use affine coordinates */
|
||||
if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0)
|
||||
return (MBEDTLS_ERR_ECP_INVALID_KEY);
|
||||
|
@ -1951,14 +1905,13 @@ int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, const mbedtls_ecp_poi
|
|||
/*
|
||||
* Check that an mbedtls_mpi is valid as a private key
|
||||
*/
|
||||
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d)
|
||||
{
|
||||
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d) {
|
||||
#if defined(ECP_MONTGOMERY)
|
||||
if (ecp_get_type(grp) == ECP_TYPE_MONTGOMERY) {
|
||||
/* see RFC 7748 sec. 5 para. 5 */
|
||||
if (mbedtls_mpi_get_bit(d, 0) != 0 ||
|
||||
mbedtls_mpi_get_bit(d, 1) != 0 ||
|
||||
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) /* mbedtls_mpi_bitlen is one-based! */
|
||||
mbedtls_mpi_get_bit(d, 1) != 0 ||
|
||||
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) /* mbedtls_mpi_bitlen is one-based! */
|
||||
return (MBEDTLS_ERR_ECP_INVALID_KEY);
|
||||
|
||||
/* see [Curve25519] page 5 */
|
||||
|
@ -1972,7 +1925,7 @@ int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, const mbedtls_mpi *d
|
|||
if (ecp_get_type(grp) == ECP_TYPE_SHORT_WEIERSTRASS) {
|
||||
/* see SEC1 3.2 */
|
||||
if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
|
||||
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0)
|
||||
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0)
|
||||
return (MBEDTLS_ERR_ECP_INVALID_KEY);
|
||||
else
|
||||
return (0);
|
||||
|
@ -1989,8 +1942,7 @@ int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
|
|||
const mbedtls_ecp_point *G,
|
||||
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret;
|
||||
size_t n_size = (grp->nbits + 7) / 8;
|
||||
|
||||
|
@ -2065,8 +2017,7 @@ cleanup:
|
|||
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
return (mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng));
|
||||
}
|
||||
|
||||
|
@ -2074,8 +2025,7 @@ int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
|
|||
* Generate a keypair, prettier wrapper
|
||||
*/
|
||||
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0)
|
||||
|
@ -2087,17 +2037,16 @@ int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
|
|||
/*
|
||||
* Check a public-private key pair
|
||||
*/
|
||||
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv)
|
||||
{
|
||||
int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv) {
|
||||
int ret;
|
||||
mbedtls_ecp_point Q;
|
||||
mbedtls_ecp_group grp;
|
||||
|
||||
if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
|
||||
pub->grp.id != prv->grp.id ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
|
||||
pub->grp.id != prv->grp.id ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
|
||||
return (MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -2111,8 +2060,8 @@ int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp
|
|||
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, NULL, NULL));
|
||||
|
||||
if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
|
||||
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
|
||||
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
|
||||
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
|
||||
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
|
||||
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -2129,8 +2078,7 @@ cleanup:
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_ecp_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_ecp_self_test(int verbose) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_ecp_group grp;
|
||||
|
@ -2184,8 +2132,8 @@ int mbedtls_ecp_self_test(int verbose)
|
|||
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &R, &m, &grp.G, NULL, NULL));
|
||||
|
||||
if (add_count != add_c_prev ||
|
||||
dbl_count != dbl_c_prev ||
|
||||
mul_count != mul_c_prev) {
|
||||
dbl_count != dbl_c_prev ||
|
||||
mul_count != mul_c_prev) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed (%u)\n", (unsigned int) i);
|
||||
|
||||
|
@ -2219,8 +2167,8 @@ int mbedtls_ecp_self_test(int verbose)
|
|||
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &R, &m, &P, NULL, NULL));
|
||||
|
||||
if (add_count != add_c_prev ||
|
||||
dbl_count != dbl_c_prev ||
|
||||
mul_count != mul_c_prev) {
|
||||
dbl_count != dbl_c_prev ||
|
||||
mul_count != mul_c_prev) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed (%u)\n", (unsigned int) i);
|
||||
|
||||
|
|
|
@ -555,8 +555,7 @@ static const mbedtls_mpi_uint brainpoolP512r1_n[] = {
|
|||
* Create an MPI from embedded constants
|
||||
* (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
|
||||
*/
|
||||
static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len)
|
||||
{
|
||||
static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len) {
|
||||
X->s = 1;
|
||||
X->n = len / sizeof(mbedtls_mpi_uint);
|
||||
X->p = (mbedtls_mpi_uint *) p;
|
||||
|
@ -565,8 +564,7 @@ static inline void ecp_mpi_load(mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_
|
|||
/*
|
||||
* Set an MPI to static value 1
|
||||
*/
|
||||
static inline void ecp_mpi_set1(mbedtls_mpi *X)
|
||||
{
|
||||
static inline void ecp_mpi_set1(mbedtls_mpi *X) {
|
||||
static mbedtls_mpi_uint one[] = { 1 };
|
||||
X->s = 1;
|
||||
X->n = 1;
|
||||
|
@ -582,8 +580,7 @@ static int ecp_group_load(mbedtls_ecp_group *grp,
|
|||
const mbedtls_mpi_uint *b, size_t blen,
|
||||
const mbedtls_mpi_uint *gx, size_t gxlen,
|
||||
const mbedtls_mpi_uint *gy, size_t gylen,
|
||||
const mbedtls_mpi_uint *n, size_t nlen)
|
||||
{
|
||||
const mbedtls_mpi_uint *n, size_t nlen) {
|
||||
ecp_mpi_load(&grp->P, p, plen);
|
||||
if (a != NULL)
|
||||
ecp_mpi_load(&grp->A, a, alen);
|
||||
|
@ -662,8 +659,7 @@ static int ecp_mod_p256k1(mbedtls_mpi *);
|
|||
/*
|
||||
* Specialized function for creating the Curve25519 group
|
||||
*/
|
||||
static int ecp_use_curve25519(mbedtls_ecp_group *grp)
|
||||
{
|
||||
static int ecp_use_curve25519(mbedtls_ecp_group *grp) {
|
||||
int ret;
|
||||
|
||||
/* Actually ( A + 2 ) / 4 */
|
||||
|
@ -701,8 +697,7 @@ cleanup:
|
|||
/*
|
||||
* Specialized function for creating the Curve448 group
|
||||
*/
|
||||
static int ecp_use_curve448(mbedtls_ecp_group *grp)
|
||||
{
|
||||
static int ecp_use_curve448(mbedtls_ecp_group *grp) {
|
||||
mbedtls_mpi Ns;
|
||||
int ret;
|
||||
|
||||
|
@ -746,8 +741,7 @@ cleanup:
|
|||
/*
|
||||
* Set a group using well-known domain parameters
|
||||
*/
|
||||
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
|
||||
{
|
||||
int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id) {
|
||||
mbedtls_ecp_group_free(grp);
|
||||
|
||||
grp->id = id;
|
||||
|
@ -859,8 +853,7 @@ int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id)
|
|||
*/
|
||||
|
||||
/* Add 64-bit chunks (dst += src) and update carry */
|
||||
static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry)
|
||||
{
|
||||
static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry) {
|
||||
unsigned char i;
|
||||
mbedtls_mpi_uint c = 0;
|
||||
for (i = 0; i < 8 / sizeof(mbedtls_mpi_uint); i++, dst++, src++) {
|
||||
|
@ -873,8 +866,7 @@ static inline void add64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_m
|
|||
}
|
||||
|
||||
/* Add carry to a 64-bit chunk and update carry */
|
||||
static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry)
|
||||
{
|
||||
static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry) {
|
||||
unsigned char i;
|
||||
for (i = 0; i < 8 / sizeof(mbedtls_mpi_uint); i++, dst++) {
|
||||
*dst += *carry;
|
||||
|
@ -891,8 +883,7 @@ static inline void carry64(mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry)
|
|||
/*
|
||||
* Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
|
||||
*/
|
||||
static int ecp_mod_p192(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p192(mbedtls_mpi *N) {
|
||||
int ret;
|
||||
mbedtls_mpi_uint c = 0;
|
||||
mbedtls_mpi_uint *p, *end;
|
||||
|
@ -970,14 +961,12 @@ cleanup:
|
|||
/*
|
||||
* Helpers for addition and subtraction of chunks, with signed carry.
|
||||
*/
|
||||
static inline void add32(uint32_t *dst, uint32_t src, signed char *carry)
|
||||
{
|
||||
static inline void add32(uint32_t *dst, uint32_t src, signed char *carry) {
|
||||
*dst += src;
|
||||
*carry += (*dst < src);
|
||||
}
|
||||
|
||||
static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry)
|
||||
{
|
||||
static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry) {
|
||||
*carry -= (*dst < src);
|
||||
*dst -= src;
|
||||
}
|
||||
|
@ -1023,8 +1012,7 @@ static inline void sub32(uint32_t *dst, uint32_t src, signed char *carry)
|
|||
* If the result is negative, we get it in the form
|
||||
* c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
|
||||
*/
|
||||
static inline int fix_negative(mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits)
|
||||
{
|
||||
static inline int fix_negative(mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits) {
|
||||
int ret;
|
||||
|
||||
/* C = - c * 2^(bits + 32) */
|
||||
|
@ -1050,8 +1038,7 @@ cleanup:
|
|||
/*
|
||||
* Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
|
||||
*/
|
||||
static int ecp_mod_p224(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p224(mbedtls_mpi *N) {
|
||||
INIT(224);
|
||||
|
||||
SUB(7);
|
||||
|
@ -1088,8 +1075,7 @@ cleanup:
|
|||
/*
|
||||
* Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
|
||||
*/
|
||||
static int ecp_mod_p256(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p256(mbedtls_mpi *N) {
|
||||
INIT(256);
|
||||
|
||||
ADD(8);
|
||||
|
@ -1172,8 +1158,7 @@ cleanup:
|
|||
/*
|
||||
* Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
|
||||
*/
|
||||
static int ecp_mod_p384(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p384(mbedtls_mpi *N) {
|
||||
INIT(384);
|
||||
|
||||
ADD(12);
|
||||
|
@ -1299,8 +1284,7 @@ cleanup:
|
|||
* Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
|
||||
* Write N as A1 + 2^521 A0, return A0 + A1
|
||||
*/
|
||||
static int ecp_mod_p521(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p521(mbedtls_mpi *N) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_mpi M;
|
||||
|
@ -1348,8 +1332,7 @@ cleanup:
|
|||
* Fast quasi-reduction modulo p255 = 2^255 - 19
|
||||
* Write N as A0 + 2^255 A1, return A0 + 19 * A1
|
||||
*/
|
||||
static int ecp_mod_p255(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p255(mbedtls_mpi *N) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_mpi M;
|
||||
|
@ -1405,8 +1388,7 @@ cleanup:
|
|||
* but for 64-bit targets it should use half the number of operations if we do
|
||||
* the reduction with 224-bit limbs, since mpi_add_mpi will then use 64-bit adds.
|
||||
*/
|
||||
static int ecp_mod_p448(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p448(mbedtls_mpi *N) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_mpi M, Q;
|
||||
|
@ -1467,8 +1449,7 @@ cleanup:
|
|||
#define P_KOBLITZ_MAX ( 256 / 8 / sizeof( mbedtls_mpi_uint ) ) // Max limbs in P
|
||||
#define P_KOBLITZ_R ( 8 / sizeof( mbedtls_mpi_uint ) ) // Limbs in R
|
||||
static inline int ecp_mod_koblitz(mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs,
|
||||
size_t adjust, size_t shift, mbedtls_mpi_uint mask)
|
||||
{
|
||||
size_t adjust, size_t shift, mbedtls_mpi_uint mask) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_mpi M, R;
|
||||
|
@ -1540,8 +1521,7 @@ cleanup:
|
|||
* Fast quasi-reduction modulo p192k1 = 2^192 - R,
|
||||
* with R = 2^32 + 2^12 + 2^8 + 2^7 + 2^6 + 2^3 + 1 = 0x0100001119
|
||||
*/
|
||||
static int ecp_mod_p192k1(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p192k1(mbedtls_mpi *N) {
|
||||
static mbedtls_mpi_uint Rp[] = {
|
||||
BYTES_TO_T_UINT_8(0xC9, 0x11, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
@ -1555,8 +1535,7 @@ static int ecp_mod_p192k1(mbedtls_mpi *N)
|
|||
* Fast quasi-reduction modulo p224k1 = 2^224 - R,
|
||||
* with R = 2^32 + 2^12 + 2^11 + 2^9 + 2^7 + 2^4 + 2 + 1 = 0x0100001A93
|
||||
*/
|
||||
static int ecp_mod_p224k1(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p224k1(mbedtls_mpi *N) {
|
||||
static mbedtls_mpi_uint Rp[] = {
|
||||
BYTES_TO_T_UINT_8(0x93, 0x1A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
@ -1575,8 +1554,7 @@ static int ecp_mod_p224k1(mbedtls_mpi *N)
|
|||
* Fast quasi-reduction modulo p256k1 = 2^256 - R,
|
||||
* with R = 2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1 = 0x01000003D1
|
||||
*/
|
||||
static int ecp_mod_p256k1(mbedtls_mpi *N)
|
||||
{
|
||||
static int ecp_mod_p256k1(mbedtls_mpi *N) {
|
||||
static mbedtls_mpi_uint Rp[] = {
|
||||
BYTES_TO_T_UINT_8(0xD1, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00)
|
||||
};
|
||||
|
|
|
@ -64,8 +64,7 @@
|
|||
|
||||
#define ENTROPY_MAX_LOOP 256 /**< Maximum amount to loop before error */
|
||||
|
||||
void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
|
||||
{
|
||||
void mbedtls_entropy_init(mbedtls_entropy_context *ctx) {
|
||||
ctx->source_count = 0;
|
||||
memset(ctx->source, 0, sizeof(ctx->source));
|
||||
|
||||
|
@ -121,8 +120,7 @@ void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
|
|||
#endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */
|
||||
}
|
||||
|
||||
void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
|
||||
{
|
||||
void mbedtls_entropy_free(mbedtls_entropy_context *ctx) {
|
||||
#if defined(MBEDTLS_HAVEGE_C)
|
||||
mbedtls_havege_free(&ctx->havege_data);
|
||||
#endif
|
||||
|
@ -144,8 +142,7 @@ void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
|
|||
|
||||
int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
|
||||
mbedtls_entropy_f_source_ptr f_source, void *p_source,
|
||||
size_t threshold, int strong)
|
||||
{
|
||||
size_t threshold, int strong) {
|
||||
int idx, ret = 0;
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
|
@ -179,8 +176,7 @@ exit:
|
|||
* Entropy accumulator update
|
||||
*/
|
||||
static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
|
||||
const unsigned char *data, size_t len)
|
||||
{
|
||||
const unsigned char *data, size_t len) {
|
||||
unsigned char header[2];
|
||||
unsigned char tmp[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
size_t use_len = len;
|
||||
|
@ -209,7 +205,7 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
|
|||
*/
|
||||
#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
|
||||
if (ctx->accumulator_started == 0 &&
|
||||
(ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0)
|
||||
(ret = mbedtls_sha512_starts_ret(&ctx->accumulator, 0)) != 0)
|
||||
goto cleanup;
|
||||
else
|
||||
ctx->accumulator_started = 1;
|
||||
|
@ -218,7 +214,7 @@ static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
|
|||
ret = mbedtls_sha512_update_ret(&ctx->accumulator, p, use_len);
|
||||
#else
|
||||
if (ctx->accumulator_started == 0 &&
|
||||
(ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0)
|
||||
(ret = mbedtls_sha256_starts_ret(&ctx->accumulator, 0)) != 0)
|
||||
goto cleanup;
|
||||
else
|
||||
ctx->accumulator_started = 1;
|
||||
|
@ -234,8 +230,7 @@ cleanup:
|
|||
}
|
||||
|
||||
int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
|
||||
const unsigned char *data, size_t len)
|
||||
{
|
||||
const unsigned char *data, size_t len) {
|
||||
int ret;
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
|
@ -256,8 +251,7 @@ int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
|
|||
/*
|
||||
* Run through the different sources to add entropy to our accumulator
|
||||
*/
|
||||
static int entropy_gather_internal(mbedtls_entropy_context *ctx)
|
||||
{
|
||||
static int entropy_gather_internal(mbedtls_entropy_context *ctx) {
|
||||
int ret, i, have_one_strong = 0;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_MAX_GATHER];
|
||||
size_t olen;
|
||||
|
@ -301,8 +295,7 @@ cleanup:
|
|||
/*
|
||||
* Thread-safe wrapper for entropy_gather_internal()
|
||||
*/
|
||||
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
|
||||
{
|
||||
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx) {
|
||||
int ret;
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
|
@ -320,8 +313,7 @@ int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
|
||||
{
|
||||
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len) {
|
||||
int ret, count = 0, i, done;
|
||||
mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
@ -433,8 +425,7 @@ exit:
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
||||
int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
|
||||
{
|
||||
int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx) {
|
||||
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
||||
|
@ -454,8 +445,7 @@ int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
|
|||
#endif /* MBEDTLS_ENTROPY_NV_SEED */
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
|
||||
{
|
||||
int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path) {
|
||||
int ret = MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR;
|
||||
FILE *f;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
@ -480,8 +470,7 @@ exit:
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
|
||||
{
|
||||
int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path) {
|
||||
int ret = 0;
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
@ -519,8 +508,7 @@ int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *p
|
|||
* Dummy source function
|
||||
*/
|
||||
static int entropy_dummy_source(void *data, unsigned char *output,
|
||||
size_t len, size_t *olen)
|
||||
{
|
||||
size_t len, size_t *olen) {
|
||||
((void) data);
|
||||
|
||||
memset(output, 0x2a, len);
|
||||
|
@ -532,8 +520,7 @@ static int entropy_dummy_source(void *data, unsigned char *output,
|
|||
|
||||
#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
|
||||
|
||||
static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t buf_len) {
|
||||
int ret = 0;
|
||||
size_t entropy_len = 0;
|
||||
size_t olen = 0;
|
||||
|
@ -557,8 +544,7 @@ static int mbedtls_entropy_source_self_test_gather(unsigned char *buf, size_t bu
|
|||
|
||||
|
||||
static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
|
||||
size_t buf_len)
|
||||
{
|
||||
size_t buf_len) {
|
||||
unsigned char set = 0xFF;
|
||||
unsigned char unset = 0x00;
|
||||
size_t i;
|
||||
|
@ -582,8 +568,7 @@ static int mbedtls_entropy_source_self_test_check_bits(const unsigned char *buf,
|
|||
* are not equal.
|
||||
* - The error code returned by the entropy source is not an error.
|
||||
*/
|
||||
int mbedtls_entropy_source_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_entropy_source_self_test(int verbose) {
|
||||
int ret = 0;
|
||||
unsigned char buf0[2 * sizeof(unsigned long long int)];
|
||||
unsigned char buf1[2 * sizeof(unsigned long long int)];
|
||||
|
@ -629,8 +614,7 @@ cleanup:
|
|||
* test that the functions don't cause errors and write the correct
|
||||
* amount of data to buffers.
|
||||
*/
|
||||
int mbedtls_entropy_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_entropy_self_test(int verbose) {
|
||||
int ret = 1;
|
||||
#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
|
||||
mbedtls_entropy_context ctx;
|
||||
|
|
|
@ -66,8 +66,7 @@
|
|||
#include <wincrypt.h>
|
||||
|
||||
int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
|
||||
size_t *olen)
|
||||
{
|
||||
size_t *olen) {
|
||||
HCRYPTPROV provider;
|
||||
((void) data);
|
||||
*olen = 0;
|
||||
|
@ -100,8 +99,7 @@ int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
|
|||
#if defined(SYS_getrandom)
|
||||
#define HAVE_GETRANDOM
|
||||
|
||||
static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
|
||||
{
|
||||
static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) {
|
||||
/* MemSan cannot understand that the syscall writes to the buffer */
|
||||
#if defined(__has_feature)
|
||||
#if __has_feature(memory_sanitizer)
|
||||
|
@ -114,8 +112,7 @@ static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
|
|||
|
||||
#include <sys/utsname.h>
|
||||
/* Check if version is at least 3.17.0 */
|
||||
static int check_version_3_17_plus(void)
|
||||
{
|
||||
static int check_version_3_17_plus(void) {
|
||||
int minor;
|
||||
struct utsname un;
|
||||
const char *ver;
|
||||
|
@ -155,8 +152,7 @@ static int has_getrandom = -1;
|
|||
#include <stdio.h>
|
||||
|
||||
int mbedtls_platform_entropy_poll(void *data,
|
||||
unsigned char *output, size_t len, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t len, size_t *olen) {
|
||||
FILE *file;
|
||||
size_t read_len;
|
||||
((void) data);
|
||||
|
@ -198,8 +194,7 @@ int mbedtls_platform_entropy_poll(void *data,
|
|||
|
||||
#if defined(MBEDTLS_TEST_NULL_ENTROPY)
|
||||
int mbedtls_null_entropy_poll(void *data,
|
||||
unsigned char *output, size_t len, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t len, size_t *olen) {
|
||||
((void) data);
|
||||
((void) output);
|
||||
*olen = 0;
|
||||
|
@ -215,8 +210,7 @@ int mbedtls_null_entropy_poll(void *data,
|
|||
|
||||
#if defined(MBEDTLS_TIMING_C)
|
||||
int mbedtls_hardclock_poll(void *data,
|
||||
unsigned char *output, size_t len, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t len, size_t *olen) {
|
||||
unsigned long timer = mbedtls_timing_hardclock();
|
||||
((void) data);
|
||||
*olen = 0;
|
||||
|
@ -233,8 +227,7 @@ int mbedtls_hardclock_poll(void *data,
|
|||
|
||||
#if defined(MBEDTLS_HAVEGE_C)
|
||||
int mbedtls_havege_poll(void *data,
|
||||
unsigned char *output, size_t len, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t len, size_t *olen) {
|
||||
mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
|
||||
*olen = 0;
|
||||
|
||||
|
@ -249,8 +242,7 @@ int mbedtls_havege_poll(void *data,
|
|||
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED)
|
||||
int mbedtls_nv_seed_poll(void *data,
|
||||
unsigned char *output, size_t len, size_t *olen)
|
||||
{
|
||||
unsigned char *output, size_t len, size_t *olen) {
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
|
||||
((void) data);
|
||||
|
|
|
@ -208,8 +208,7 @@
|
|||
#endif
|
||||
|
||||
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen)
|
||||
{
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen) {
|
||||
size_t len;
|
||||
int use_ret;
|
||||
|
||||
|
@ -881,8 +880,7 @@ void mbedtls_strerror(int ret, char *buf, size_t buflen)
|
|||
/*
|
||||
* Provide an non-function in case MBEDTLS_ERROR_C is not defined
|
||||
*/
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen)
|
||||
{
|
||||
void mbedtls_strerror(int ret, char *buf, size_t buflen) {
|
||||
((void) ret);
|
||||
|
||||
if (buflen > 0)
|
||||
|
|
|
@ -89,13 +89,11 @@ static const int supported_digests[] = {
|
|||
MBEDTLS_MD_NONE
|
||||
};
|
||||
|
||||
const int *mbedtls_md_list(void)
|
||||
{
|
||||
const int *mbedtls_md_list(void) {
|
||||
return (supported_digests);
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
||||
{
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name) {
|
||||
if (NULL == md_name)
|
||||
return (NULL);
|
||||
|
||||
|
@ -135,8 +133,7 @@ const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name)
|
|||
return (NULL);
|
||||
}
|
||||
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
||||
{
|
||||
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type) {
|
||||
switch (md_type) {
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
case MBEDTLS_MD_MD2:
|
||||
|
@ -175,13 +172,11 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
|
|||
}
|
||||
}
|
||||
|
||||
void mbedtls_md_init(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
void mbedtls_md_init(mbedtls_md_context_t *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_md_context_t));
|
||||
}
|
||||
|
||||
void mbedtls_md_free(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
void mbedtls_md_free(mbedtls_md_context_t *ctx) {
|
||||
if (ctx == NULL || ctx->md_info == NULL)
|
||||
return;
|
||||
|
||||
|
@ -198,11 +193,10 @@ void mbedtls_md_free(mbedtls_md_context_t *ctx)
|
|||
}
|
||||
|
||||
int mbedtls_md_clone(mbedtls_md_context_t *dst,
|
||||
const mbedtls_md_context_t *src)
|
||||
{
|
||||
const mbedtls_md_context_t *src) {
|
||||
if (dst == NULL || dst->md_info == NULL ||
|
||||
src == NULL || src->md_info == NULL ||
|
||||
dst->md_info != src->md_info) {
|
||||
src == NULL || src->md_info == NULL ||
|
||||
dst->md_info != src->md_info) {
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -212,14 +206,12 @@ int mbedtls_md_clone(mbedtls_md_context_t *dst,
|
|||
}
|
||||
|
||||
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info) {
|
||||
return mbedtls_md_setup(ctx, md_info, 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
|
||||
{
|
||||
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac) {
|
||||
if (md_info == NULL || ctx == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -239,24 +231,21 @@ int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info
|
|||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
int mbedtls_md_starts(mbedtls_md_context_t *ctx) {
|
||||
if (ctx == NULL || ctx->md_info == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
return (ctx->md_info->starts_func(ctx->md_ctx));
|
||||
}
|
||||
|
||||
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
|
||||
{
|
||||
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen) {
|
||||
if (ctx == NULL || ctx->md_info == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
return (ctx->md_info->update_func(ctx->md_ctx, input, ilen));
|
||||
}
|
||||
|
||||
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
|
||||
{
|
||||
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output) {
|
||||
if (ctx == NULL || ctx->md_info == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -264,8 +253,7 @@ int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
|
|||
}
|
||||
|
||||
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
if (md_info == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -273,8 +261,7 @@ int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, siz
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
|
||||
{
|
||||
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output) {
|
||||
int ret;
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
@ -313,8 +300,7 @@ cleanup:
|
|||
}
|
||||
#endif /* MBEDTLS_FS_IO */
|
||||
|
||||
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
|
||||
{
|
||||
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen) {
|
||||
int ret;
|
||||
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char *ipad, *opad;
|
||||
|
@ -358,16 +344,14 @@ cleanup:
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
|
||||
{
|
||||
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen) {
|
||||
if (ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
return (ctx->md_info->update_func(ctx->md_ctx, input, ilen));
|
||||
}
|
||||
|
||||
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
|
||||
{
|
||||
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output) {
|
||||
int ret;
|
||||
unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char *opad;
|
||||
|
@ -390,8 +374,7 @@ int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
|
|||
return (ctx->md_info->finish_func(ctx->md_ctx, output));
|
||||
}
|
||||
|
||||
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
|
||||
{
|
||||
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx) {
|
||||
int ret;
|
||||
unsigned char *ipad;
|
||||
|
||||
|
@ -409,8 +392,7 @@ int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
|
|||
int mbedtls_md_hmac(const mbedtls_md_info_t *md_info,
|
||||
const unsigned char *key, size_t keylen,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
mbedtls_md_context_t ctx;
|
||||
int ret;
|
||||
|
||||
|
@ -435,32 +417,28 @@ cleanup:
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data)
|
||||
{
|
||||
int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data) {
|
||||
if (ctx == NULL || ctx->md_info == NULL)
|
||||
return (MBEDTLS_ERR_MD_BAD_INPUT_DATA);
|
||||
|
||||
return (ctx->md_info->process_func(ctx->md_ctx, data));
|
||||
}
|
||||
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info) {
|
||||
if (md_info == NULL)
|
||||
return (0);
|
||||
|
||||
return md_info->size;
|
||||
}
|
||||
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info) {
|
||||
if (md_info == NULL)
|
||||
return (MBEDTLS_MD_NONE);
|
||||
|
||||
return md_info->type;
|
||||
}
|
||||
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
|
||||
{
|
||||
const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info) {
|
||||
if (md_info == NULL)
|
||||
return (NULL);
|
||||
|
||||
|
|
|
@ -73,13 +73,11 @@
|
|||
}
|
||||
#endif
|
||||
|
||||
void mbedtls_md5_init(mbedtls_md5_context *ctx)
|
||||
{
|
||||
void mbedtls_md5_init(mbedtls_md5_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_md5_context));
|
||||
}
|
||||
|
||||
void mbedtls_md5_free(mbedtls_md5_context *ctx)
|
||||
{
|
||||
void mbedtls_md5_free(mbedtls_md5_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -87,16 +85,14 @@ void mbedtls_md5_free(mbedtls_md5_context *ctx)
|
|||
}
|
||||
|
||||
void mbedtls_md5_clone(mbedtls_md5_context *dst,
|
||||
const mbedtls_md5_context *src)
|
||||
{
|
||||
const mbedtls_md5_context *src) {
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 context setup
|
||||
*/
|
||||
int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx)
|
||||
{
|
||||
int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx) {
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
|
@ -109,16 +105,14 @@ int mbedtls_md5_starts_ret(mbedtls_md5_context *ctx)
|
|||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_md5_starts(mbedtls_md5_context *ctx)
|
||||
{
|
||||
void mbedtls_md5_starts(mbedtls_md5_context *ctx) {
|
||||
mbedtls_md5_starts_ret(ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_MD5_PROCESS_ALT)
|
||||
int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
uint32_t X[16], A, B, C, D;
|
||||
|
||||
GET_UINT32_LE(X[ 0], data, 0);
|
||||
|
@ -244,8 +238,7 @@ int mbedtls_internal_md5_process(mbedtls_md5_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_md5_process(mbedtls_md5_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
mbedtls_internal_md5_process(ctx, data);
|
||||
}
|
||||
#endif
|
||||
|
@ -256,8 +249,7 @@ void mbedtls_md5_process(mbedtls_md5_context *ctx,
|
|||
*/
|
||||
int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
int ret;
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
@ -302,8 +294,7 @@ int mbedtls_md5_update_ret(mbedtls_md5_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_md5_update(mbedtls_md5_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
mbedtls_md5_update_ret(ctx, input, ilen);
|
||||
}
|
||||
#endif
|
||||
|
@ -312,8 +303,7 @@ void mbedtls_md5_update(mbedtls_md5_context *ctx,
|
|||
* MD5 final digest
|
||||
*/
|
||||
int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
int ret;
|
||||
uint32_t used;
|
||||
uint32_t high, low;
|
||||
|
@ -364,8 +354,7 @@ int mbedtls_md5_finish_ret(mbedtls_md5_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_md5_finish(mbedtls_md5_context *ctx,
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
mbedtls_md5_finish_ret(ctx, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -377,8 +366,7 @@ void mbedtls_md5_finish(mbedtls_md5_context *ctx,
|
|||
*/
|
||||
int mbedtls_md5_ret(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
int ret;
|
||||
mbedtls_md5_context ctx;
|
||||
|
||||
|
@ -402,8 +390,7 @@ exit:
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_md5(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[16])
|
||||
{
|
||||
unsigned char output[16]) {
|
||||
mbedtls_md5_ret(input, ilen, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -463,8 +450,7 @@ static const unsigned char md5_test_sum[7][16] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_md5_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_md5_self_test(int verbose) {
|
||||
int i, ret = 0;
|
||||
unsigned char md5sum[16];
|
||||
|
||||
|
|
|
@ -73,24 +73,20 @@
|
|||
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
|
||||
static int md2_starts_wrap(void *ctx)
|
||||
{
|
||||
static int md2_starts_wrap(void *ctx) {
|
||||
return (mbedtls_md2_starts_ret((mbedtls_md2_context *) ctx));
|
||||
}
|
||||
|
||||
static int md2_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_md2_update_ret((mbedtls_md2_context *) ctx, input, ilen));
|
||||
}
|
||||
|
||||
static int md2_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int md2_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_md2_finish_ret((mbedtls_md2_context *) ctx, output));
|
||||
}
|
||||
|
||||
static void *md2_ctx_alloc(void)
|
||||
{
|
||||
static void *md2_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md2_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -99,20 +95,17 @@ static void *md2_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void md2_ctx_free(void *ctx)
|
||||
{
|
||||
static void md2_ctx_free(void *ctx) {
|
||||
mbedtls_md2_free((mbedtls_md2_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void md2_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void md2_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_md2_clone((mbedtls_md2_context *) dst,
|
||||
(const mbedtls_md2_context *) src);
|
||||
}
|
||||
|
||||
static int md2_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int md2_process_wrap(void *ctx, const unsigned char *data) {
|
||||
((void) data);
|
||||
|
||||
return (mbedtls_internal_md2_process((mbedtls_md2_context *) ctx));
|
||||
|
@ -137,24 +130,20 @@ const mbedtls_md_info_t mbedtls_md2_info = {
|
|||
|
||||
#if defined(MBEDTLS_MD4_C)
|
||||
|
||||
static int md4_starts_wrap(void *ctx)
|
||||
{
|
||||
static int md4_starts_wrap(void *ctx) {
|
||||
return (mbedtls_md4_starts_ret((mbedtls_md4_context *) ctx));
|
||||
}
|
||||
|
||||
static int md4_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_md4_update_ret((mbedtls_md4_context *) ctx, input, ilen));
|
||||
}
|
||||
|
||||
static int md4_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int md4_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_md4_finish_ret((mbedtls_md4_context *) ctx, output));
|
||||
}
|
||||
|
||||
static void *md4_ctx_alloc(void)
|
||||
{
|
||||
static void *md4_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md4_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -163,20 +152,17 @@ static void *md4_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void md4_ctx_free(void *ctx)
|
||||
{
|
||||
static void md4_ctx_free(void *ctx) {
|
||||
mbedtls_md4_free((mbedtls_md4_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void md4_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void md4_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_md4_clone((mbedtls_md4_context *) dst,
|
||||
(const mbedtls_md4_context *) src);
|
||||
}
|
||||
|
||||
static int md4_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int md4_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_md4_process((mbedtls_md4_context *) ctx, data));
|
||||
}
|
||||
|
||||
|
@ -199,24 +185,20 @@ const mbedtls_md_info_t mbedtls_md4_info = {
|
|||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
|
||||
static int md5_starts_wrap(void *ctx)
|
||||
{
|
||||
static int md5_starts_wrap(void *ctx) {
|
||||
return (mbedtls_md5_starts_ret((mbedtls_md5_context *) ctx));
|
||||
}
|
||||
|
||||
static int md5_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_md5_update_ret((mbedtls_md5_context *) ctx, input, ilen));
|
||||
}
|
||||
|
||||
static int md5_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int md5_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_md5_finish_ret((mbedtls_md5_context *) ctx, output));
|
||||
}
|
||||
|
||||
static void *md5_ctx_alloc(void)
|
||||
{
|
||||
static void *md5_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_md5_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -225,20 +207,17 @@ static void *md5_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void md5_ctx_free(void *ctx)
|
||||
{
|
||||
static void md5_ctx_free(void *ctx) {
|
||||
mbedtls_md5_free((mbedtls_md5_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void md5_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void md5_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_md5_clone((mbedtls_md5_context *) dst,
|
||||
(const mbedtls_md5_context *) src);
|
||||
}
|
||||
|
||||
static int md5_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int md5_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_md5_process((mbedtls_md5_context *) ctx, data));
|
||||
}
|
||||
|
||||
|
@ -261,26 +240,22 @@ const mbedtls_md_info_t mbedtls_md5_info = {
|
|||
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
|
||||
static int ripemd160_starts_wrap(void *ctx)
|
||||
{
|
||||
static int ripemd160_starts_wrap(void *ctx) {
|
||||
return (mbedtls_ripemd160_starts_ret((mbedtls_ripemd160_context *) ctx));
|
||||
}
|
||||
|
||||
static int ripemd160_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_ripemd160_update_ret((mbedtls_ripemd160_context *) ctx,
|
||||
input, ilen));
|
||||
}
|
||||
|
||||
static int ripemd160_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int ripemd160_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_ripemd160_finish_ret((mbedtls_ripemd160_context *) ctx,
|
||||
output));
|
||||
}
|
||||
|
||||
static void *ripemd160_ctx_alloc(void)
|
||||
{
|
||||
static void *ripemd160_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ripemd160_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -289,20 +264,17 @@ static void *ripemd160_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void ripemd160_ctx_free(void *ctx)
|
||||
{
|
||||
static void ripemd160_ctx_free(void *ctx) {
|
||||
mbedtls_ripemd160_free((mbedtls_ripemd160_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void ripemd160_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void ripemd160_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_ripemd160_clone((mbedtls_ripemd160_context *) dst,
|
||||
(const mbedtls_ripemd160_context *) src);
|
||||
}
|
||||
|
||||
static int ripemd160_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int ripemd160_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_ripemd160_process(
|
||||
(mbedtls_ripemd160_context *) ctx, data));
|
||||
}
|
||||
|
@ -326,25 +298,21 @@ const mbedtls_md_info_t mbedtls_ripemd160_info = {
|
|||
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
|
||||
static int sha1_starts_wrap(void *ctx)
|
||||
{
|
||||
static int sha1_starts_wrap(void *ctx) {
|
||||
return (mbedtls_sha1_starts_ret((mbedtls_sha1_context *) ctx));
|
||||
}
|
||||
|
||||
static int sha1_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_sha1_update_ret((mbedtls_sha1_context *) ctx,
|
||||
input, ilen));
|
||||
}
|
||||
|
||||
static int sha1_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int sha1_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_sha1_finish_ret((mbedtls_sha1_context *) ctx, output));
|
||||
}
|
||||
|
||||
static void *sha1_ctx_alloc(void)
|
||||
{
|
||||
static void *sha1_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha1_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -353,20 +321,17 @@ static void *sha1_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void sha1_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void sha1_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_sha1_clone((mbedtls_sha1_context *) dst,
|
||||
(const mbedtls_sha1_context *) src);
|
||||
}
|
||||
|
||||
static void sha1_ctx_free(void *ctx)
|
||||
{
|
||||
static void sha1_ctx_free(void *ctx) {
|
||||
mbedtls_sha1_free((mbedtls_sha1_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static int sha1_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int sha1_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_sha1_process((mbedtls_sha1_context *) ctx,
|
||||
data));
|
||||
}
|
||||
|
@ -393,32 +358,27 @@ const mbedtls_md_info_t mbedtls_sha1_info = {
|
|||
*/
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
|
||||
static int sha224_starts_wrap(void *ctx)
|
||||
{
|
||||
static int sha224_starts_wrap(void *ctx) {
|
||||
return (mbedtls_sha256_starts_ret((mbedtls_sha256_context *) ctx, 1));
|
||||
}
|
||||
|
||||
static int sha224_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_sha256_update_ret((mbedtls_sha256_context *) ctx,
|
||||
input, ilen));
|
||||
}
|
||||
|
||||
static int sha224_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int sha224_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_sha256_finish_ret((mbedtls_sha256_context *) ctx,
|
||||
output));
|
||||
}
|
||||
|
||||
static int sha224_wrap(const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return (mbedtls_sha256_ret(input, ilen, output, 1));
|
||||
}
|
||||
|
||||
static void *sha224_ctx_alloc(void)
|
||||
{
|
||||
static void *sha224_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha256_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -427,20 +387,17 @@ static void *sha224_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void sha224_ctx_free(void *ctx)
|
||||
{
|
||||
static void sha224_ctx_free(void *ctx) {
|
||||
mbedtls_sha256_free((mbedtls_sha256_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void sha224_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void sha224_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_sha256_clone((mbedtls_sha256_context *) dst,
|
||||
(const mbedtls_sha256_context *) src);
|
||||
}
|
||||
|
||||
static int sha224_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int sha224_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_sha256_process((mbedtls_sha256_context *) ctx,
|
||||
data));
|
||||
}
|
||||
|
@ -460,14 +417,12 @@ const mbedtls_md_info_t mbedtls_sha224_info = {
|
|||
sha224_process_wrap,
|
||||
};
|
||||
|
||||
static int sha256_starts_wrap(void *ctx)
|
||||
{
|
||||
static int sha256_starts_wrap(void *ctx) {
|
||||
return (mbedtls_sha256_starts_ret((mbedtls_sha256_context *) ctx, 0));
|
||||
}
|
||||
|
||||
static int sha256_wrap(const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return (mbedtls_sha256_ret(input, ilen, output, 0));
|
||||
}
|
||||
|
||||
|
@ -490,32 +445,27 @@ const mbedtls_md_info_t mbedtls_sha256_info = {
|
|||
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
|
||||
static int sha384_starts_wrap(void *ctx)
|
||||
{
|
||||
static int sha384_starts_wrap(void *ctx) {
|
||||
return (mbedtls_sha512_starts_ret((mbedtls_sha512_context *) ctx, 1));
|
||||
}
|
||||
|
||||
static int sha384_update_wrap(void *ctx, const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
return (mbedtls_sha512_update_ret((mbedtls_sha512_context *) ctx,
|
||||
input, ilen));
|
||||
}
|
||||
|
||||
static int sha384_finish_wrap(void *ctx, unsigned char *output)
|
||||
{
|
||||
static int sha384_finish_wrap(void *ctx, unsigned char *output) {
|
||||
return (mbedtls_sha512_finish_ret((mbedtls_sha512_context *) ctx,
|
||||
output));
|
||||
}
|
||||
|
||||
static int sha384_wrap(const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return (mbedtls_sha512_ret(input, ilen, output, 1));
|
||||
}
|
||||
|
||||
static void *sha384_ctx_alloc(void)
|
||||
{
|
||||
static void *sha384_ctx_alloc(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_sha512_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -524,20 +474,17 @@ static void *sha384_ctx_alloc(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void sha384_ctx_free(void *ctx)
|
||||
{
|
||||
static void sha384_ctx_free(void *ctx) {
|
||||
mbedtls_sha512_free((mbedtls_sha512_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void sha384_clone_wrap(void *dst, const void *src)
|
||||
{
|
||||
static void sha384_clone_wrap(void *dst, const void *src) {
|
||||
mbedtls_sha512_clone((mbedtls_sha512_context *) dst,
|
||||
(const mbedtls_sha512_context *) src);
|
||||
}
|
||||
|
||||
static int sha384_process_wrap(void *ctx, const unsigned char *data)
|
||||
{
|
||||
static int sha384_process_wrap(void *ctx, const unsigned char *data) {
|
||||
return (mbedtls_internal_sha512_process((mbedtls_sha512_context *) ctx,
|
||||
data));
|
||||
}
|
||||
|
@ -557,14 +504,12 @@ const mbedtls_md_info_t mbedtls_sha384_info = {
|
|||
sha384_process_wrap,
|
||||
};
|
||||
|
||||
static int sha512_starts_wrap(void *ctx)
|
||||
{
|
||||
static int sha512_starts_wrap(void *ctx) {
|
||||
return (mbedtls_sha512_starts_ret((mbedtls_sha512_context *) ctx, 0));
|
||||
}
|
||||
|
||||
static int sha512_wrap(const unsigned char *input, size_t ilen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
return (mbedtls_sha512_ret(input, ilen, output, 0));
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@
|
|||
if( p == NULL || oid == NULL ) return( NULL ); \
|
||||
while( cur->asn1 != NULL ) { \
|
||||
if( cur->asn1_len == oid->len && \
|
||||
memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
|
||||
memcmp( cur->asn1, oid->p, oid->len ) == 0 ) { \
|
||||
return( p ); \
|
||||
} \
|
||||
p++; \
|
||||
|
@ -705,8 +705,7 @@ FN_OID_GET_ATTR2(mbedtls_oid_get_pkcs12_pbe_alg, oid_pkcs12_pbe_alg_t, pkcs12_pb
|
|||
|
||||
/* Return the x.y.z.... style numeric string for the given OID */
|
||||
int mbedtls_oid_get_numeric_string(char *buf, size_t size,
|
||||
const mbedtls_asn1_buf *oid)
|
||||
{
|
||||
const mbedtls_asn1_buf *oid) {
|
||||
int ret;
|
||||
size_t i, n;
|
||||
unsigned int value;
|
||||
|
|
|
@ -48,8 +48,7 @@
|
|||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PEM_PARSE_C)
|
||||
void mbedtls_pem_init(mbedtls_pem_context *ctx)
|
||||
{
|
||||
void mbedtls_pem_init(mbedtls_pem_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_pem_context));
|
||||
}
|
||||
|
||||
|
@ -59,8 +58,7 @@ void mbedtls_pem_init(mbedtls_pem_context *ctx)
|
|||
* Read a 16-byte hex string and convert it to binary
|
||||
*/
|
||||
static int pem_get_iv(const unsigned char *s, unsigned char *iv,
|
||||
size_t iv_len)
|
||||
{
|
||||
size_t iv_len) {
|
||||
size_t i, j, k;
|
||||
|
||||
memset(iv, 0, iv_len);
|
||||
|
@ -82,8 +80,7 @@ static int pem_get_iv(const unsigned char *s, unsigned char *iv,
|
|||
|
||||
static int pem_pbkdf1(unsigned char *key, size_t keylen,
|
||||
unsigned char *iv,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
mbedtls_md5_context md5_ctx;
|
||||
unsigned char md5sum[16];
|
||||
size_t use_len;
|
||||
|
@ -143,8 +140,7 @@ exit:
|
|||
*/
|
||||
static int pem_des_decrypt(unsigned char des_iv[8],
|
||||
unsigned char *buf, size_t buflen,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
mbedtls_des_context des_ctx;
|
||||
unsigned char des_key[8];
|
||||
int ret;
|
||||
|
@ -171,8 +167,7 @@ exit:
|
|||
*/
|
||||
static int pem_des3_decrypt(unsigned char des3_iv[8],
|
||||
unsigned char *buf, size_t buflen,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
mbedtls_des3_context des3_ctx;
|
||||
unsigned char des3_key[24];
|
||||
int ret;
|
||||
|
@ -201,8 +196,7 @@ exit:
|
|||
*/
|
||||
static int pem_aes_decrypt(unsigned char aes_iv[16], unsigned int keylen,
|
||||
unsigned char *buf, size_t buflen,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
mbedtls_aes_context aes_ctx;
|
||||
unsigned char aes_key[32];
|
||||
int ret;
|
||||
|
@ -230,8 +224,7 @@ exit:
|
|||
|
||||
int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const char *footer,
|
||||
const unsigned char *data, const unsigned char *pwd,
|
||||
size_t pwdlen, size_t *use_len)
|
||||
{
|
||||
size_t pwdlen, size_t *use_len) {
|
||||
int ret, enc;
|
||||
size_t len;
|
||||
unsigned char *buf;
|
||||
|
@ -412,8 +405,7 @@ int mbedtls_pem_read_buffer(mbedtls_pem_context *ctx, const char *header, const
|
|||
return (0);
|
||||
}
|
||||
|
||||
void mbedtls_pem_free(mbedtls_pem_context *ctx)
|
||||
{
|
||||
void mbedtls_pem_free(mbedtls_pem_context *ctx) {
|
||||
if (ctx->buf != NULL)
|
||||
mbedtls_platform_zeroize(ctx->buf, ctx->buflen);
|
||||
mbedtls_free(ctx->buf);
|
||||
|
@ -426,8 +418,7 @@ void mbedtls_pem_free(mbedtls_pem_context *ctx)
|
|||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
int mbedtls_pem_write_buffer(const char *header, const char *footer,
|
||||
const unsigned char *der_data, size_t der_len,
|
||||
unsigned char *buf, size_t buf_len, size_t *olen)
|
||||
{
|
||||
unsigned char *buf, size_t buf_len, size_t *olen) {
|
||||
int ret;
|
||||
unsigned char *encode_buf = NULL, *c, *p = buf;
|
||||
size_t len = 0, use_len, add_len = 0;
|
||||
|
@ -441,7 +432,7 @@ int mbedtls_pem_write_buffer(const char *header, const char *footer,
|
|||
}
|
||||
|
||||
if (use_len != 0 &&
|
||||
((encode_buf = mbedtls_calloc(1, use_len)) == NULL))
|
||||
((encode_buf = mbedtls_calloc(1, use_len)) == NULL))
|
||||
return (MBEDTLS_ERR_PEM_ALLOC_FAILED);
|
||||
|
||||
if ((ret = mbedtls_base64_encode(encode_buf, use_len, &use_len, der_data,
|
||||
|
|
|
@ -49,8 +49,7 @@
|
|||
/*
|
||||
* Initialise a mbedtls_pk_context
|
||||
*/
|
||||
void mbedtls_pk_init(mbedtls_pk_context *ctx)
|
||||
{
|
||||
void mbedtls_pk_init(mbedtls_pk_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -61,8 +60,7 @@ void mbedtls_pk_init(mbedtls_pk_context *ctx)
|
|||
/*
|
||||
* Free (the components of) a mbedtls_pk_context
|
||||
*/
|
||||
void mbedtls_pk_free(mbedtls_pk_context *ctx)
|
||||
{
|
||||
void mbedtls_pk_free(mbedtls_pk_context *ctx) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return;
|
||||
|
||||
|
@ -74,8 +72,7 @@ void mbedtls_pk_free(mbedtls_pk_context *ctx)
|
|||
/*
|
||||
* Get pk_info structure from type
|
||||
*/
|
||||
const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
|
||||
{
|
||||
const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type) {
|
||||
switch (pk_type) {
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
case MBEDTLS_PK_RSA:
|
||||
|
@ -100,8 +97,7 @@ const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
|
|||
/*
|
||||
* Initialise context
|
||||
*/
|
||||
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
|
||||
{
|
||||
int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info) {
|
||||
if (ctx == NULL || info == NULL || ctx->pk_info != NULL)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -120,8 +116,7 @@ int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
|
|||
int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
|
||||
mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
|
||||
mbedtls_pk_rsa_alt_sign_func sign_func,
|
||||
mbedtls_pk_rsa_alt_key_len_func key_len_func)
|
||||
{
|
||||
mbedtls_pk_rsa_alt_key_len_func key_len_func) {
|
||||
mbedtls_rsa_alt_context *rsa_alt;
|
||||
const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
|
||||
|
||||
|
@ -147,8 +142,7 @@ int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
|
|||
/*
|
||||
* Tell if a PK can do the operations of the given type
|
||||
*/
|
||||
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
|
||||
{
|
||||
int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type) {
|
||||
/* null or NONE context can't do anything */
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (0);
|
||||
|
@ -159,8 +153,7 @@ int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
|
|||
/*
|
||||
* Helper for mbedtls_pk_sign and mbedtls_pk_verify
|
||||
*/
|
||||
static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
|
||||
{
|
||||
static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len) {
|
||||
const mbedtls_md_info_t *md_info;
|
||||
|
||||
if (*hash_len != 0)
|
||||
|
@ -178,10 +171,9 @@ static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
|
|||
*/
|
||||
int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const unsigned char *sig, size_t sig_len)
|
||||
{
|
||||
const unsigned char *sig, size_t sig_len) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL ||
|
||||
pk_hashlen_helper(md_alg, &hash_len) != 0)
|
||||
pk_hashlen_helper(md_alg, &hash_len) != 0)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
if (ctx->pk_info->verify_func == NULL)
|
||||
|
@ -197,8 +189,7 @@ int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
|
|||
int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
|
||||
mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const unsigned char *sig, size_t sig_len)
|
||||
{
|
||||
const unsigned char *sig, size_t sig_len) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -254,10 +245,9 @@ int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
|
|||
int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t *sig_len,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL ||
|
||||
pk_hashlen_helper(md_alg, &hash_len) != 0)
|
||||
pk_hashlen_helper(md_alg, &hash_len) != 0)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
if (ctx->pk_info->sign_func == NULL)
|
||||
|
@ -273,8 +263,7 @@ int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
|
|||
int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen, size_t osize,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -291,8 +280,7 @@ int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
|
|||
int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen, size_t osize,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -306,11 +294,10 @@ int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
|
|||
/*
|
||||
* Check public-private key pair
|
||||
*/
|
||||
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv)
|
||||
{
|
||||
int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_context *prv) {
|
||||
if (pub == NULL || pub->pk_info == NULL ||
|
||||
prv == NULL || prv->pk_info == NULL ||
|
||||
prv->pk_info->check_pair_func == NULL) {
|
||||
prv == NULL || prv->pk_info == NULL ||
|
||||
prv->pk_info->check_pair_func == NULL) {
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -328,8 +315,7 @@ int mbedtls_pk_check_pair(const mbedtls_pk_context *pub, const mbedtls_pk_contex
|
|||
/*
|
||||
* Get key size in bits
|
||||
*/
|
||||
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
|
||||
{
|
||||
size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (0);
|
||||
|
||||
|
@ -339,8 +325,7 @@ size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
|
|||
/*
|
||||
* Export debug information
|
||||
*/
|
||||
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
|
||||
{
|
||||
int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -354,8 +339,7 @@ int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items
|
|||
/*
|
||||
* Access the PK type name
|
||||
*/
|
||||
const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
|
||||
{
|
||||
const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return ("invalid PK");
|
||||
|
||||
|
@ -365,8 +349,7 @@ const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
|
|||
/*
|
||||
* Access the PK type
|
||||
*/
|
||||
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
|
||||
{
|
||||
mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx) {
|
||||
if (ctx == NULL || ctx->pk_info == NULL)
|
||||
return (MBEDTLS_PK_NONE);
|
||||
|
||||
|
|
|
@ -136,8 +136,7 @@ typedef struct mbedtls_pk_context {
|
|||
* \warning You must make sure the PK context actually holds an RSA context
|
||||
* before using this function!
|
||||
*/
|
||||
static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
|
||||
{
|
||||
static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk) {
|
||||
return ((mbedtls_rsa_context *)(pk).pk_ctx);
|
||||
}
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
|
@ -149,8 +148,7 @@ static inline mbedtls_rsa_context *mbedtls_pk_rsa(const mbedtls_pk_context pk)
|
|||
* \warning You must make sure the PK context actually holds an EC context
|
||||
* before using this function!
|
||||
*/
|
||||
static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk)
|
||||
{
|
||||
static inline mbedtls_ecp_keypair *mbedtls_pk_ec(const mbedtls_pk_context pk) {
|
||||
return ((mbedtls_ecp_keypair *)(pk).pk_ctx);
|
||||
}
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
|
@ -240,8 +238,7 @@ size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx);
|
|||
*
|
||||
* \return Key length in bytes, or 0 on error
|
||||
*/
|
||||
static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx)
|
||||
{
|
||||
static inline size_t mbedtls_pk_get_len(const mbedtls_pk_context *ctx) {
|
||||
return ((mbedtls_pk_get_bitlen(ctx) + 7) / 8);
|
||||
}
|
||||
|
||||
|
|
|
@ -59,22 +59,19 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
static int rsa_can_do(mbedtls_pk_type_t type)
|
||||
{
|
||||
static int rsa_can_do(mbedtls_pk_type_t type) {
|
||||
return (type == MBEDTLS_PK_RSA ||
|
||||
type == MBEDTLS_PK_RSASSA_PSS);
|
||||
}
|
||||
|
||||
static size_t rsa_get_bitlen(const void *ctx)
|
||||
{
|
||||
static size_t rsa_get_bitlen(const void *ctx) {
|
||||
const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
|
||||
return (8 * mbedtls_rsa_get_len(rsa));
|
||||
}
|
||||
|
||||
static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const unsigned char *sig, size_t sig_len)
|
||||
{
|
||||
const unsigned char *sig, size_t sig_len) {
|
||||
int ret;
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
|
||||
size_t rsa_len = mbedtls_rsa_get_len(rsa);
|
||||
|
@ -106,8 +103,7 @@ static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t *sig_len,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
|
||||
|
||||
#if SIZE_MAX > UINT_MAX
|
||||
|
@ -124,8 +120,7 @@ static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
static int rsa_decrypt_wrap(void *ctx,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen, size_t osize,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
|
||||
|
||||
if (ilen != mbedtls_rsa_get_len(rsa))
|
||||
|
@ -138,8 +133,7 @@ static int rsa_decrypt_wrap(void *ctx,
|
|||
static int rsa_encrypt_wrap(void *ctx,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen, size_t osize,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
|
||||
*olen = mbedtls_rsa_get_len(rsa);
|
||||
|
||||
|
@ -150,14 +144,12 @@ static int rsa_encrypt_wrap(void *ctx,
|
|||
ilen, input, output));
|
||||
}
|
||||
|
||||
static int rsa_check_pair_wrap(const void *pub, const void *prv)
|
||||
{
|
||||
static int rsa_check_pair_wrap(const void *pub, const void *prv) {
|
||||
return (mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
|
||||
(const mbedtls_rsa_context *) prv));
|
||||
}
|
||||
|
||||
static void *rsa_alloc_wrap(void)
|
||||
{
|
||||
static void *rsa_alloc_wrap(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -166,14 +158,12 @@ static void *rsa_alloc_wrap(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void rsa_free_wrap(void *ctx)
|
||||
{
|
||||
static void rsa_free_wrap(void *ctx) {
|
||||
mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
|
||||
{
|
||||
static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items) {
|
||||
items->type = MBEDTLS_PK_DEBUG_MPI;
|
||||
items->name = "rsa.N";
|
||||
items->value = &(((mbedtls_rsa_context *) ctx)->N);
|
||||
|
@ -205,15 +195,13 @@ const mbedtls_pk_info_t mbedtls_rsa_info = {
|
|||
/*
|
||||
* Generic EC key
|
||||
*/
|
||||
static int eckey_can_do(mbedtls_pk_type_t type)
|
||||
{
|
||||
static int eckey_can_do(mbedtls_pk_type_t type) {
|
||||
return (type == MBEDTLS_PK_ECKEY ||
|
||||
type == MBEDTLS_PK_ECKEY_DH ||
|
||||
type == MBEDTLS_PK_ECDSA);
|
||||
}
|
||||
|
||||
static size_t eckey_get_bitlen(const void *ctx)
|
||||
{
|
||||
static size_t eckey_get_bitlen(const void *ctx) {
|
||||
return (((mbedtls_ecp_keypair *) ctx)->grp.pbits);
|
||||
}
|
||||
|
||||
|
@ -230,8 +218,7 @@ static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
|
||||
static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const unsigned char *sig, size_t sig_len)
|
||||
{
|
||||
const unsigned char *sig, size_t sig_len) {
|
||||
int ret;
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
|
||||
|
@ -248,8 +235,7 @@ static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t *sig_len,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret;
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
|
||||
|
@ -266,14 +252,12 @@ static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
|
||||
#endif /* MBEDTLS_ECDSA_C */
|
||||
|
||||
static int eckey_check_pair(const void *pub, const void *prv)
|
||||
{
|
||||
static int eckey_check_pair(const void *pub, const void *prv) {
|
||||
return (mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
|
||||
(const mbedtls_ecp_keypair *) prv));
|
||||
}
|
||||
|
||||
static void *eckey_alloc_wrap(void)
|
||||
{
|
||||
static void *eckey_alloc_wrap(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -282,14 +266,12 @@ static void *eckey_alloc_wrap(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void eckey_free_wrap(void *ctx)
|
||||
{
|
||||
static void eckey_free_wrap(void *ctx) {
|
||||
mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
||||
static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
|
||||
{
|
||||
static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items) {
|
||||
items->type = MBEDTLS_PK_DEBUG_ECP;
|
||||
items->name = "eckey.Q";
|
||||
items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
|
||||
|
@ -318,8 +300,7 @@ const mbedtls_pk_info_t mbedtls_eckey_info = {
|
|||
/*
|
||||
* EC key restricted to ECDH
|
||||
*/
|
||||
static int eckeydh_can_do(mbedtls_pk_type_t type)
|
||||
{
|
||||
static int eckeydh_can_do(mbedtls_pk_type_t type) {
|
||||
return (type == MBEDTLS_PK_ECKEY ||
|
||||
type == MBEDTLS_PK_ECKEY_DH);
|
||||
}
|
||||
|
@ -341,15 +322,13 @@ const mbedtls_pk_info_t mbedtls_eckeydh_info = {
|
|||
#endif /* MBEDTLS_ECP_C */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C)
|
||||
static int ecdsa_can_do(mbedtls_pk_type_t type)
|
||||
{
|
||||
static int ecdsa_can_do(mbedtls_pk_type_t type) {
|
||||
return (type == MBEDTLS_PK_ECDSA);
|
||||
}
|
||||
|
||||
static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
const unsigned char *sig, size_t sig_len)
|
||||
{
|
||||
const unsigned char *sig, size_t sig_len) {
|
||||
int ret;
|
||||
((void) md_alg);
|
||||
|
||||
|
@ -365,14 +344,12 @@ static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t *sig_len,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
return (mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
|
||||
md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng));
|
||||
}
|
||||
|
||||
static void *ecdsa_alloc_wrap(void)
|
||||
{
|
||||
static void *ecdsa_alloc_wrap(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -381,8 +358,7 @@ static void *ecdsa_alloc_wrap(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void ecdsa_free_wrap(void *ctx)
|
||||
{
|
||||
static void ecdsa_free_wrap(void *ctx) {
|
||||
mbedtls_ecdsa_free((mbedtls_ecdsa_context *) ctx);
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
@ -408,13 +384,11 @@ const mbedtls_pk_info_t mbedtls_ecdsa_info = {
|
|||
* Support for alternative RSA-private implementations
|
||||
*/
|
||||
|
||||
static int rsa_alt_can_do(mbedtls_pk_type_t type)
|
||||
{
|
||||
static int rsa_alt_can_do(mbedtls_pk_type_t type) {
|
||||
return (type == MBEDTLS_PK_RSA);
|
||||
}
|
||||
|
||||
static size_t rsa_alt_get_bitlen(const void *ctx)
|
||||
{
|
||||
static size_t rsa_alt_get_bitlen(const void *ctx) {
|
||||
const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
|
||||
|
||||
return (8 * rsa_alt->key_len_func(rsa_alt->key));
|
||||
|
@ -423,8 +397,7 @@ static size_t rsa_alt_get_bitlen(const void *ctx)
|
|||
static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
||||
const unsigned char *hash, size_t hash_len,
|
||||
unsigned char *sig, size_t *sig_len,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
|
||||
|
||||
#if SIZE_MAX > UINT_MAX
|
||||
|
@ -441,8 +414,7 @@ static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
|
|||
static int rsa_alt_decrypt_wrap(void *ctx,
|
||||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen, size_t osize,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
|
||||
|
||||
((void) f_rng);
|
||||
|
@ -456,8 +428,7 @@ static int rsa_alt_decrypt_wrap(void *ctx,
|
|||
}
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
static int rsa_alt_check_pair(const void *pub, const void *prv)
|
||||
{
|
||||
static int rsa_alt_check_pair(const void *pub, const void *prv) {
|
||||
unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
|
||||
unsigned char hash[32];
|
||||
size_t sig_len = 0;
|
||||
|
@ -483,8 +454,7 @@ static int rsa_alt_check_pair(const void *pub, const void *prv)
|
|||
}
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
|
||||
static void *rsa_alt_alloc_wrap(void)
|
||||
{
|
||||
static void *rsa_alt_alloc_wrap(void) {
|
||||
void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
|
||||
|
||||
if (ctx != NULL)
|
||||
|
@ -493,8 +463,7 @@ static void *rsa_alt_alloc_wrap(void)
|
|||
return (ctx);
|
||||
}
|
||||
|
||||
static void rsa_alt_free_wrap(void *ctx)
|
||||
{
|
||||
static void rsa_alt_free_wrap(void *ctx) {
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
|
||||
mbedtls_free(ctx);
|
||||
}
|
||||
|
|
|
@ -51,8 +51,7 @@
|
|||
#endif
|
||||
|
||||
static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations)
|
||||
{
|
||||
mbedtls_asn1_buf *salt, int *iterations) {
|
||||
int ret;
|
||||
unsigned char **p = ¶ms->p;
|
||||
const unsigned char *end = params->p + params->len;
|
||||
|
@ -89,8 +88,7 @@ static int pkcs12_parse_pbe_params(mbedtls_asn1_buf *params,
|
|||
static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_type_t md_type,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
unsigned char *key, size_t keylen,
|
||||
unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
unsigned char *iv, size_t ivlen) {
|
||||
int ret, iterations = 0;
|
||||
mbedtls_asn1_buf salt;
|
||||
size_t i;
|
||||
|
@ -131,8 +129,7 @@ static int pkcs12_pbe_derive_key_iv(mbedtls_asn1_buf *pbe_params, mbedtls_md_typ
|
|||
int mbedtls_pkcs12_pbe_sha1_rc4_128(mbedtls_asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t len,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
#if !defined(MBEDTLS_ARC4_C)
|
||||
((void) pbe_params);
|
||||
((void) mode);
|
||||
|
@ -172,8 +169,7 @@ int mbedtls_pkcs12_pbe(mbedtls_asn1_buf *pbe_params, int mode,
|
|||
mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t len,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret, keylen = 0;
|
||||
unsigned char key[32];
|
||||
unsigned char iv[16];
|
||||
|
@ -224,8 +220,7 @@ exit:
|
|||
}
|
||||
|
||||
static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
|
||||
const unsigned char *filler, size_t fill_len)
|
||||
{
|
||||
const unsigned char *filler, size_t fill_len) {
|
||||
unsigned char *p = data;
|
||||
size_t use_len;
|
||||
|
||||
|
@ -240,8 +235,7 @@ static void pkcs12_fill_buffer(unsigned char *data, size_t data_len,
|
|||
int mbedtls_pkcs12_derivation(unsigned char *data, size_t datalen,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *salt, size_t saltlen,
|
||||
mbedtls_md_type_t md_type, int id, int iterations)
|
||||
{
|
||||
mbedtls_md_type_t md_type, int id, int iterations) {
|
||||
int ret;
|
||||
unsigned int j;
|
||||
|
||||
|
|
|
@ -60,8 +60,7 @@
|
|||
int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t datalen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
((void) pbe_params);
|
||||
((void) mode);
|
||||
((void) pwd);
|
||||
|
@ -74,8 +73,7 @@ int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
|
|||
#else
|
||||
static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations,
|
||||
int *keylen, mbedtls_md_type_t *md_type)
|
||||
{
|
||||
int *keylen, mbedtls_md_type_t *md_type) {
|
||||
int ret;
|
||||
mbedtls_asn1_buf prf_alg_oid;
|
||||
unsigned char *p = params->p;
|
||||
|
@ -129,8 +127,7 @@ static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
|
|||
int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t datalen,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret, iterations = 0, keylen = 0;
|
||||
unsigned char *p, *end;
|
||||
mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
|
||||
|
@ -194,7 +191,7 @@ int mbedtls_pkcs5_pbes2(const mbedtls_asn1_buf *pbe_params, int mode,
|
|||
keylen = cipher_info->key_bitlen / 8;
|
||||
|
||||
if (enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
|
||||
enc_scheme_params.len != cipher_info->iv_size) {
|
||||
enc_scheme_params.len != cipher_info->iv_size) {
|
||||
return (MBEDTLS_ERR_PKCS5_INVALID_FORMAT);
|
||||
}
|
||||
|
||||
|
@ -232,8 +229,7 @@ exit:
|
|||
int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, const unsigned char *password,
|
||||
size_t plen, const unsigned char *salt, size_t slen,
|
||||
unsigned int iteration_count,
|
||||
uint32_t key_length, unsigned char *output)
|
||||
{
|
||||
uint32_t key_length, unsigned char *output) {
|
||||
int ret, j;
|
||||
unsigned int i;
|
||||
unsigned char md1[MBEDTLS_MD_MAX_SIZE];
|
||||
|
@ -303,8 +299,7 @@ int mbedtls_pkcs5_pbkdf2_hmac(mbedtls_md_context_t *ctx, const unsigned char *pa
|
|||
#if defined(MBEDTLS_SELF_TEST)
|
||||
|
||||
#if !defined(MBEDTLS_SHA1_C)
|
||||
int mbedtls_pkcs5_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_pkcs5_self_test(int verbose) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf(" PBKDF2 (SHA1): skipped\n\n");
|
||||
|
||||
|
@ -370,8 +365,7 @@ static const unsigned char result_key[MAX_TESTS][32] = {
|
|||
},
|
||||
};
|
||||
|
||||
int mbedtls_pkcs5_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_pkcs5_self_test(int verbose) {
|
||||
mbedtls_md_context_t sha1_ctx;
|
||||
const mbedtls_md_info_t *info_sha1;
|
||||
int ret, i;
|
||||
|
@ -397,7 +391,7 @@ int mbedtls_pkcs5_self_test(int verbose)
|
|||
ret = mbedtls_pkcs5_pbkdf2_hmac(&sha1_ctx, password[i], plen[i], salt[i],
|
||||
slen[i], it_cnt[i], key_len[i], key);
|
||||
if (ret != 0 ||
|
||||
memcmp(result_key[i], key, key_len[i]) != 0) {
|
||||
memcmp(result_key[i], key, key_len[i]) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
|
|
@ -71,8 +71,7 @@
|
|||
* A terminating null byte is always appended. It is included in the announced
|
||||
* length only if the data looks like it is PEM encoded.
|
||||
*/
|
||||
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
|
||||
{
|
||||
int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n) {
|
||||
FILE *f;
|
||||
long size;
|
||||
|
||||
|
@ -89,7 +88,7 @@ int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
|
|||
*n = (size_t) size;
|
||||
|
||||
if (*n + 1 == 0 ||
|
||||
(*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
|
||||
(*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
|
||||
fclose(f);
|
||||
return (MBEDTLS_ERR_PK_ALLOC_FAILED);
|
||||
}
|
||||
|
@ -117,8 +116,7 @@ int mbedtls_pk_load_file(const char *path, unsigned char **buf, size_t *n)
|
|||
* Load and parse a private key
|
||||
*/
|
||||
int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
|
||||
const char *path, const char *pwd)
|
||||
{
|
||||
const char *path, const char *pwd) {
|
||||
int ret;
|
||||
size_t n;
|
||||
unsigned char *buf;
|
||||
|
@ -141,8 +139,7 @@ int mbedtls_pk_parse_keyfile(mbedtls_pk_context *ctx,
|
|||
/*
|
||||
* Load and parse a public key
|
||||
*/
|
||||
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
|
||||
{
|
||||
int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path) {
|
||||
int ret;
|
||||
size_t n;
|
||||
unsigned char *buf;
|
||||
|
@ -169,8 +166,7 @@ int mbedtls_pk_parse_public_keyfile(mbedtls_pk_context *ctx, const char *path)
|
|||
* }
|
||||
*/
|
||||
static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_asn1_buf *params)
|
||||
{
|
||||
mbedtls_asn1_buf *params) {
|
||||
int ret;
|
||||
|
||||
if (end - *p < 1)
|
||||
|
@ -181,7 +177,7 @@ static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
|
|||
params->tag = **p;
|
||||
if (params->tag != MBEDTLS_ASN1_OID
|
||||
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
|
||||
&& params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
|
||||
&& params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)
|
||||
#endif
|
||||
) {
|
||||
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
||||
|
@ -222,8 +218,7 @@ static int pk_get_ecparams(unsigned char **p, const unsigned char *end,
|
|||
*
|
||||
* We only support prime-field as field type, and ignore hash and cofactor.
|
||||
*/
|
||||
static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
|
||||
{
|
||||
static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) {
|
||||
int ret;
|
||||
unsigned char *p = params->p;
|
||||
const unsigned char *const end = params->p + params->len;
|
||||
|
@ -262,7 +257,7 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
|
|||
return (ret);
|
||||
|
||||
if (len != MBEDTLS_OID_SIZE(MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD) ||
|
||||
memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
|
||||
memcmp(p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len) != 0) {
|
||||
return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
|
@ -298,14 +293,14 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
|
|||
* containing an integer in the case of a prime field
|
||||
*/
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
|
||||
(ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
|
||||
(ret = mbedtls_mpi_read_binary(&grp->A, p, len)) != 0) {
|
||||
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
|
||||
}
|
||||
|
||||
p += len;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING)) != 0 ||
|
||||
(ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
|
||||
(ret = mbedtls_mpi_read_binary(&grp->B, p, len)) != 0) {
|
||||
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
|
||||
}
|
||||
|
||||
|
@ -332,11 +327,11 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
|
|||
* reading only the X coordinate and the parity bit of Y.
|
||||
*/
|
||||
if (ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
|
||||
(p[0] != 0x02 && p[0] != 0x03) ||
|
||||
len != mbedtls_mpi_size(&grp->P) + 1 ||
|
||||
mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
|
||||
mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
|
||||
mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
|
||||
(p[0] != 0x02 && p[0] != 0x03) ||
|
||||
len != mbedtls_mpi_size(&grp->P) + 1 ||
|
||||
mbedtls_mpi_read_binary(&grp->G.X, p + 1, len - 1) != 0 ||
|
||||
mbedtls_mpi_lset(&grp->G.Y, p[0] - 2) != 0 ||
|
||||
mbedtls_mpi_lset(&grp->G.Z, 1) != 0) {
|
||||
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT);
|
||||
}
|
||||
}
|
||||
|
@ -362,8 +357,7 @@ static int pk_group_from_specified(const mbedtls_asn1_buf *params, mbedtls_ecp_g
|
|||
* Find the group id associated with an (almost filled) group as generated by
|
||||
* pk_group_from_specified(), or return an error if unknown.
|
||||
*/
|
||||
static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id)
|
||||
{
|
||||
static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id) {
|
||||
int ret = 0;
|
||||
mbedtls_ecp_group ref;
|
||||
const mbedtls_ecp_group_id *id;
|
||||
|
@ -377,14 +371,14 @@ static int pk_group_id_from_group(const mbedtls_ecp_group *grp, mbedtls_ecp_grou
|
|||
|
||||
/* Compare to the group we were given, starting with easy tests */
|
||||
if (grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
|
||||
/* For Y we may only know the parity bit, so compare only that */
|
||||
mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
|
||||
mbedtls_mpi_cmp_mpi(&grp->P, &ref.P) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->A, &ref.A) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->B, &ref.B) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->N, &ref.N) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->G.X, &ref.G.X) == 0 &&
|
||||
mbedtls_mpi_cmp_mpi(&grp->G.Z, &ref.G.Z) == 0 &&
|
||||
/* For Y we may only know the parity bit, so compare only that */
|
||||
mbedtls_mpi_get_bit(&grp->G.Y, 0) == mbedtls_mpi_get_bit(&ref.G.Y, 0)) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -405,8 +399,7 @@ cleanup:
|
|||
* Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
|
||||
*/
|
||||
static int pk_group_id_from_specified(const mbedtls_asn1_buf *params,
|
||||
mbedtls_ecp_group_id *grp_id)
|
||||
{
|
||||
mbedtls_ecp_group_id *grp_id) {
|
||||
int ret;
|
||||
mbedtls_ecp_group grp;
|
||||
|
||||
|
@ -432,8 +425,7 @@ cleanup:
|
|||
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
|
||||
* -- implicitCurve NULL
|
||||
*/
|
||||
static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp)
|
||||
{
|
||||
static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp) {
|
||||
int ret;
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
|
||||
|
@ -469,8 +461,7 @@ static int pk_use_ecparams(const mbedtls_asn1_buf *params, mbedtls_ecp_group *gr
|
|||
* return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
|
||||
*/
|
||||
static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_ecp_keypair *key)
|
||||
{
|
||||
mbedtls_ecp_keypair *key) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_ecp_point_read_binary(&key->grp, &key->Q,
|
||||
|
@ -496,8 +487,7 @@ static int pk_get_ecpubkey(unsigned char **p, const unsigned char *end,
|
|||
*/
|
||||
static int pk_get_rsapubkey(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_rsa_context *rsa)
|
||||
{
|
||||
mbedtls_rsa_context *rsa) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -530,7 +520,7 @@ static int pk_get_rsapubkey(unsigned char **p,
|
|||
*p += len;
|
||||
|
||||
if (mbedtls_rsa_complete(rsa) != 0 ||
|
||||
mbedtls_rsa_check_pubkey(rsa) != 0) {
|
||||
mbedtls_rsa_check_pubkey(rsa) != 0) {
|
||||
return (MBEDTLS_ERR_PK_INVALID_PUBKEY);
|
||||
}
|
||||
|
||||
|
@ -550,8 +540,7 @@ static int pk_get_rsapubkey(unsigned char **p,
|
|||
*/
|
||||
static int pk_get_pk_alg(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params)
|
||||
{
|
||||
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params) {
|
||||
int ret;
|
||||
mbedtls_asn1_buf alg_oid;
|
||||
|
||||
|
@ -567,8 +556,8 @@ static int pk_get_pk_alg(unsigned char **p,
|
|||
* No parameters with RSA (only for EC)
|
||||
*/
|
||||
if (*pk_alg == MBEDTLS_PK_RSA &&
|
||||
((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
|
||||
params->len != 0)) {
|
||||
((params->tag != MBEDTLS_ASN1_NULL && params->tag != 0) ||
|
||||
params->len != 0)) {
|
||||
return (MBEDTLS_ERR_PK_INVALID_ALG);
|
||||
}
|
||||
|
||||
|
@ -581,8 +570,7 @@ static int pk_get_pk_alg(unsigned char **p,
|
|||
* subjectPublicKey BIT STRING }
|
||||
*/
|
||||
int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
mbedtls_pk_context *pk) {
|
||||
int ret;
|
||||
size_t len;
|
||||
mbedtls_asn1_buf alg_params;
|
||||
|
@ -642,8 +630,7 @@ int mbedtls_pk_parse_subpubkey(unsigned char **p, const unsigned char *end,
|
|||
*/
|
||||
static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
size_t keylen) {
|
||||
int ret, version;
|
||||
size_t len;
|
||||
unsigned char *p, *end;
|
||||
|
@ -688,40 +675,40 @@ static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
|
|||
/* Import N */
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0 ||
|
||||
(ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
(ret = mbedtls_rsa_import_raw(rsa, p, len, NULL, 0, NULL, 0,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
goto cleanup;
|
||||
p += len;
|
||||
|
||||
/* Import E */
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0 ||
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
|
||||
NULL, 0, p, len)) != 0)
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
|
||||
NULL, 0, p, len)) != 0)
|
||||
goto cleanup;
|
||||
p += len;
|
||||
|
||||
/* Import D */
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0 ||
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
|
||||
p, len, NULL, 0)) != 0)
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, NULL, 0,
|
||||
p, len, NULL, 0)) != 0)
|
||||
goto cleanup;
|
||||
p += len;
|
||||
|
||||
/* Import P */
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0 ||
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, p, len, NULL, 0,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, p, len, NULL, 0,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
goto cleanup;
|
||||
p += len;
|
||||
|
||||
/* Import Q */
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_INTEGER)) != 0 ||
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, p, len,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
(ret = mbedtls_rsa_import_raw(rsa, NULL, 0, NULL, 0, p, len,
|
||||
NULL, 0, NULL, 0)) != 0)
|
||||
goto cleanup;
|
||||
p += len;
|
||||
|
||||
|
@ -731,8 +718,8 @@ static int pk_parse_key_pkcs1_der(mbedtls_rsa_context *rsa,
|
|||
|
||||
/* Check optional parameters */
|
||||
if ((ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0)
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_get_mpi(&p, end, &T)) != 0)
|
||||
goto cleanup;
|
||||
|
||||
if (p != end) {
|
||||
|
@ -764,8 +751,7 @@ cleanup:
|
|||
*/
|
||||
static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
size_t keylen) {
|
||||
int ret;
|
||||
int version, pubkey_done;
|
||||
size_t len;
|
||||
|
@ -815,7 +801,7 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
|
|||
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0)) == 0) {
|
||||
if ((ret = pk_get_ecparams(&p, p + len, ¶ms)) != 0 ||
|
||||
(ret = pk_use_ecparams(¶ms, &eck->grp)) != 0) {
|
||||
(ret = pk_use_ecparams(¶ms, &eck->grp)) != 0) {
|
||||
mbedtls_ecp_keypair_free(eck);
|
||||
return (ret);
|
||||
}
|
||||
|
@ -858,8 +844,8 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
|
|||
}
|
||||
|
||||
if (! pubkey_done &&
|
||||
(ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
|
||||
NULL, NULL)) != 0) {
|
||||
(ret = mbedtls_ecp_mul(&eck->grp, &eck->Q, &eck->d, &eck->grp.G,
|
||||
NULL, NULL)) != 0) {
|
||||
mbedtls_ecp_keypair_free(eck);
|
||||
return (MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret);
|
||||
}
|
||||
|
@ -889,8 +875,7 @@ static int pk_parse_key_sec1_der(mbedtls_ecp_keypair *eck,
|
|||
static int pk_parse_key_pkcs8_unencrypted_der(
|
||||
mbedtls_pk_context *pk,
|
||||
const unsigned char *key,
|
||||
size_t keylen)
|
||||
{
|
||||
size_t keylen) {
|
||||
int ret, version;
|
||||
size_t len;
|
||||
mbedtls_asn1_buf params;
|
||||
|
@ -955,7 +940,7 @@ static int pk_parse_key_pkcs8_unencrypted_der(
|
|||
#if defined(MBEDTLS_ECP_C)
|
||||
if (pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH) {
|
||||
if ((ret = pk_use_ecparams(¶ms, &mbedtls_pk_ec(*pk)->grp)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) {
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk), p, len)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
return (ret);
|
||||
}
|
||||
|
@ -979,8 +964,7 @@ static int pk_parse_key_pkcs8_unencrypted_der(
|
|||
static int pk_parse_key_pkcs8_encrypted_der(
|
||||
mbedtls_pk_context *pk,
|
||||
unsigned char *key, size_t keylen,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
int ret, decrypted = 0;
|
||||
size_t len;
|
||||
unsigned char *buf;
|
||||
|
@ -1088,8 +1072,7 @@ static int pk_parse_key_pkcs8_encrypted_der(
|
|||
*/
|
||||
int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
||||
const unsigned char *key, size_t keylen,
|
||||
const unsigned char *pwd, size_t pwdlen)
|
||||
{
|
||||
const unsigned char *pwd, size_t pwdlen) {
|
||||
int ret;
|
||||
const mbedtls_pk_info_t *pk_info;
|
||||
|
||||
|
@ -1112,8 +1095,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
if (ret == 0) {
|
||||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
|
||||
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
|
||||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
|
||||
pem.buf, pem.buflen)) != 0) {
|
||||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
|
||||
pem.buf, pem.buflen)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
}
|
||||
|
||||
|
@ -1140,8 +1123,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
|
||||
|
||||
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
pem.buf, pem.buflen)) != 0) {
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
pem.buf, pem.buflen)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
}
|
||||
|
||||
|
@ -1245,8 +1228,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
|
||||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_RSA);
|
||||
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
|
||||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
|
||||
key, keylen)) != 0) {
|
||||
(ret = pk_parse_key_pkcs1_der(mbedtls_pk_rsa(*pk),
|
||||
key, keylen)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
} else {
|
||||
return (0);
|
||||
|
@ -1258,8 +1241,8 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
|
||||
pk_info = mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY);
|
||||
if ((ret = mbedtls_pk_setup(pk, pk_info)) != 0 ||
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
key, keylen)) != 0) {
|
||||
(ret = pk_parse_key_sec1_der(mbedtls_pk_ec(*pk),
|
||||
key, keylen)) != 0) {
|
||||
mbedtls_pk_free(pk);
|
||||
} else {
|
||||
return (0);
|
||||
|
@ -1274,8 +1257,7 @@ int mbedtls_pk_parse_key(mbedtls_pk_context *pk,
|
|||
* Parse a public key
|
||||
*/
|
||||
int mbedtls_pk_parse_public_key(mbedtls_pk_context *ctx,
|
||||
const unsigned char *key, size_t keylen)
|
||||
{
|
||||
const unsigned char *key, size_t keylen) {
|
||||
int ret;
|
||||
unsigned char *p;
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
|
|
|
@ -64,8 +64,7 @@
|
|||
* }
|
||||
*/
|
||||
static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
|
||||
mbedtls_rsa_context *rsa)
|
||||
{
|
||||
mbedtls_rsa_context *rsa) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
mbedtls_mpi T;
|
||||
|
@ -74,13 +73,13 @@ static int pk_write_rsa_pubkey(unsigned char **p, unsigned char *start,
|
|||
|
||||
/* Export E */
|
||||
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL, NULL, NULL, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export N */
|
||||
if ((ret = mbedtls_rsa_export(rsa, &T, NULL, NULL, NULL, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(p, start, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
|
@ -103,8 +102,7 @@ end_of_export:
|
|||
* EC public key is an EC point
|
||||
*/
|
||||
static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
|
||||
mbedtls_ecp_keypair *ec)
|
||||
{
|
||||
mbedtls_ecp_keypair *ec) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
|
||||
|
@ -130,8 +128,7 @@ static int pk_write_ec_pubkey(unsigned char **p, unsigned char *start,
|
|||
* }
|
||||
*/
|
||||
static int pk_write_ec_param(unsigned char **p, unsigned char *start,
|
||||
mbedtls_ecp_keypair *ec)
|
||||
{
|
||||
mbedtls_ecp_keypair *ec) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
const char *oid;
|
||||
|
@ -147,8 +144,7 @@ static int pk_write_ec_param(unsigned char **p, unsigned char *start,
|
|||
#endif /* MBEDTLS_ECP_C */
|
||||
|
||||
int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
|
||||
const mbedtls_pk_context *key)
|
||||
{
|
||||
const mbedtls_pk_context *key) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -167,8 +163,7 @@ int mbedtls_pk_write_pubkey(unsigned char **p, unsigned char *start,
|
|||
return ((int) len);
|
||||
}
|
||||
|
||||
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size)
|
||||
{
|
||||
int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
|
||||
int ret;
|
||||
unsigned char *c;
|
||||
size_t len = 0, par_len = 0, oid_len;
|
||||
|
@ -213,8 +208,7 @@ int mbedtls_pk_write_pubkey_der(mbedtls_pk_context *key, unsigned char *buf, siz
|
|||
return ((int) len);
|
||||
}
|
||||
|
||||
int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size)
|
||||
{
|
||||
int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
|
||||
int ret;
|
||||
unsigned char *c = buf + size;
|
||||
size_t len = 0;
|
||||
|
@ -232,54 +226,54 @@ int mbedtls_pk_write_key_der(mbedtls_pk_context *key, unsigned char *buf, size_t
|
|||
|
||||
/* Export QP */
|
||||
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, NULL, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export DQ */
|
||||
if ((ret = mbedtls_rsa_export_crt(rsa, NULL, &T, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export DP */
|
||||
if ((ret = mbedtls_rsa_export_crt(rsa, &T, NULL, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export Q */
|
||||
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
|
||||
&T, NULL, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export P */
|
||||
if ((ret = mbedtls_rsa_export(rsa, NULL, &T,
|
||||
NULL, NULL, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export D */
|
||||
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
|
||||
NULL, &T, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export E */
|
||||
if ((ret = mbedtls_rsa_export(rsa, NULL, NULL,
|
||||
NULL, NULL, &T)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
/* Export N */
|
||||
if ((ret = mbedtls_rsa_export(rsa, &T, NULL,
|
||||
NULL, NULL, NULL)) != 0 ||
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
(ret = mbedtls_asn1_write_mpi(&c, buf, &T)) < 0)
|
||||
goto end_of_export;
|
||||
len += ret;
|
||||
|
||||
|
@ -446,8 +440,7 @@ end_of_export:
|
|||
#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
|
||||
RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
|
||||
|
||||
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size)
|
||||
{
|
||||
int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
|
||||
int ret;
|
||||
unsigned char output_buf[PUB_DER_MAX_BYTES];
|
||||
size_t olen = 0;
|
||||
|
@ -466,8 +459,7 @@ int mbedtls_pk_write_pubkey_pem(mbedtls_pk_context *key, unsigned char *buf, siz
|
|||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size)
|
||||
{
|
||||
int mbedtls_pk_write_key_pem(mbedtls_pk_context *key, unsigned char *buf, size_t size) {
|
||||
int ret;
|
||||
unsigned char output_buf[PRV_DER_MAX_BYTES];
|
||||
const char *begin, *end;
|
||||
|
|
|
@ -34,8 +34,7 @@
|
|||
|
||||
#if defined(MBEDTLS_PLATFORM_MEMORY)
|
||||
#if !defined(MBEDTLS_PLATFORM_STD_CALLOC)
|
||||
static void *platform_calloc_uninit(size_t n, size_t size)
|
||||
{
|
||||
static void *platform_calloc_uninit(size_t n, size_t size) {
|
||||
((void) n);
|
||||
((void) size);
|
||||
return (NULL);
|
||||
|
@ -45,8 +44,7 @@ static void *platform_calloc_uninit(size_t n, size_t size)
|
|||
#endif /* !MBEDTLS_PLATFORM_STD_CALLOC */
|
||||
|
||||
#if !defined(MBEDTLS_PLATFORM_STD_FREE)
|
||||
static void platform_free_uninit(void *ptr)
|
||||
{
|
||||
static void platform_free_uninit(void *ptr) {
|
||||
((void) ptr);
|
||||
}
|
||||
|
||||
|
@ -56,19 +54,16 @@ static void platform_free_uninit(void *ptr)
|
|||
static void *(*mbedtls_calloc_func)(size_t, size_t) = MBEDTLS_PLATFORM_STD_CALLOC;
|
||||
static void (*mbedtls_free_func)(void *) = MBEDTLS_PLATFORM_STD_FREE;
|
||||
|
||||
void *mbedtls_calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
void *mbedtls_calloc(size_t nmemb, size_t size) {
|
||||
return (*mbedtls_calloc_func)(nmemb, size);
|
||||
}
|
||||
|
||||
void mbedtls_free(void *ptr)
|
||||
{
|
||||
void mbedtls_free(void *ptr) {
|
||||
(*mbedtls_free_func)(ptr);
|
||||
}
|
||||
|
||||
int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
|
||||
void (*free_func)(void *))
|
||||
{
|
||||
void (*free_func)(void *)) {
|
||||
mbedtls_calloc_func = calloc_func;
|
||||
mbedtls_free_func = free_func;
|
||||
return (0);
|
||||
|
@ -77,8 +72,7 @@ int mbedtls_platform_set_calloc_free(void *(*calloc_func)(size_t, size_t),
|
|||
|
||||
#if defined(_WIN32)
|
||||
#include <stdarg.h>
|
||||
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
|
||||
{
|
||||
int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...) {
|
||||
int ret;
|
||||
va_list argp;
|
||||
|
||||
|
@ -108,8 +102,7 @@ int mbedtls_platform_win32_snprintf(char *s, size_t n, const char *fmt, ...)
|
|||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_snprintf_uninit(char *s, size_t n,
|
||||
const char *format, ...)
|
||||
{
|
||||
const char *format, ...) {
|
||||
((void) s);
|
||||
((void) n);
|
||||
((void) format);
|
||||
|
@ -125,8 +118,7 @@ int (*mbedtls_snprintf)(char *s, size_t n,
|
|||
|
||||
int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
|
||||
const char *format,
|
||||
...))
|
||||
{
|
||||
...)) {
|
||||
mbedtls_snprintf = snprintf_func;
|
||||
return (0);
|
||||
}
|
||||
|
@ -137,8 +129,7 @@ int mbedtls_platform_set_snprintf(int (*snprintf_func)(char *s, size_t n,
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_printf_uninit(const char *format, ...)
|
||||
{
|
||||
static int platform_printf_uninit(const char *format, ...) {
|
||||
((void) format);
|
||||
return (0);
|
||||
}
|
||||
|
@ -148,8 +139,7 @@ static int platform_printf_uninit(const char *format, ...)
|
|||
|
||||
int (*mbedtls_printf)(const char *, ...) = MBEDTLS_PLATFORM_STD_PRINTF;
|
||||
|
||||
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
|
||||
{
|
||||
int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...)) {
|
||||
mbedtls_printf = printf_func;
|
||||
return (0);
|
||||
}
|
||||
|
@ -160,8 +150,7 @@ int mbedtls_platform_set_printf(int (*printf_func)(const char *, ...))
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
|
||||
{
|
||||
static int platform_fprintf_uninit(FILE *stream, const char *format, ...) {
|
||||
((void) stream);
|
||||
((void) format);
|
||||
return (0);
|
||||
|
@ -173,8 +162,7 @@ static int platform_fprintf_uninit(FILE *stream, const char *format, ...)
|
|||
int (*mbedtls_fprintf)(FILE *, const char *, ...) =
|
||||
MBEDTLS_PLATFORM_STD_FPRINTF;
|
||||
|
||||
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
|
||||
{
|
||||
int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...)) {
|
||||
mbedtls_fprintf = fprintf_func;
|
||||
return (0);
|
||||
}
|
||||
|
@ -185,8 +173,7 @@ int mbedtls_platform_set_fprintf(int (*fprintf_func)(FILE *, const char *, ...))
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static void platform_exit_uninit(int status)
|
||||
{
|
||||
static void platform_exit_uninit(int status) {
|
||||
((void) status);
|
||||
}
|
||||
|
||||
|
@ -195,8 +182,7 @@ static void platform_exit_uninit(int status)
|
|||
|
||||
void (*mbedtls_exit)(int status) = MBEDTLS_PLATFORM_STD_EXIT;
|
||||
|
||||
int mbedtls_platform_set_exit(void (*exit_func)(int status))
|
||||
{
|
||||
int mbedtls_platform_set_exit(void (*exit_func)(int status)) {
|
||||
mbedtls_exit = exit_func;
|
||||
return (0);
|
||||
}
|
||||
|
@ -209,8 +195,7 @@ int mbedtls_platform_set_exit(void (*exit_func)(int status))
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
|
||||
{
|
||||
static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer) {
|
||||
((void) timer);
|
||||
return (0);
|
||||
}
|
||||
|
@ -220,8 +205,7 @@ static mbedtls_time_t platform_time_uninit(mbedtls_time_t *timer)
|
|||
|
||||
mbedtls_time_t (*mbedtls_time)(mbedtls_time_t *timer) = MBEDTLS_PLATFORM_STD_TIME;
|
||||
|
||||
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer))
|
||||
{
|
||||
int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer)) {
|
||||
mbedtls_time = time_func;
|
||||
return (0);
|
||||
}
|
||||
|
@ -234,8 +218,7 @@ int mbedtls_platform_set_time(mbedtls_time_t (*time_func)(mbedtls_time_t *timer)
|
|||
/* Default implementations for the platform independent seed functions use
|
||||
* standard libc file functions to read from and write to a pre-defined filename
|
||||
*/
|
||||
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len) {
|
||||
FILE *file;
|
||||
size_t n;
|
||||
|
||||
|
@ -252,8 +235,7 @@ int mbedtls_platform_std_nv_seed_read(unsigned char *buf, size_t buf_len)
|
|||
return ((int)n);
|
||||
}
|
||||
|
||||
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len) {
|
||||
FILE *file;
|
||||
size_t n;
|
||||
|
||||
|
@ -275,8 +257,7 @@ int mbedtls_platform_std_nv_seed_write(unsigned char *buf, size_t buf_len)
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len) {
|
||||
((void) buf);
|
||||
((void) buf_len);
|
||||
return (-1);
|
||||
|
@ -289,8 +270,7 @@ static int platform_nv_seed_read_uninit(unsigned char *buf, size_t buf_len)
|
|||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len)
|
||||
{
|
||||
static int platform_nv_seed_write_uninit(unsigned char *buf, size_t buf_len) {
|
||||
((void) buf);
|
||||
((void) buf_len);
|
||||
return (-1);
|
||||
|
@ -306,8 +286,7 @@ int (*mbedtls_nv_seed_write)(unsigned char *buf, size_t buf_len) =
|
|||
|
||||
int mbedtls_platform_set_nv_seed(
|
||||
int (*nv_seed_read_func)(unsigned char *buf, size_t buf_len),
|
||||
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len))
|
||||
{
|
||||
int (*nv_seed_write_func)(unsigned char *buf, size_t buf_len)) {
|
||||
mbedtls_nv_seed_read = nv_seed_read_func;
|
||||
mbedtls_nv_seed_write = nv_seed_write_func;
|
||||
return (0);
|
||||
|
@ -319,8 +298,7 @@ int mbedtls_platform_set_nv_seed(
|
|||
/*
|
||||
* Placeholder platform setup that does nothing by default
|
||||
*/
|
||||
int mbedtls_platform_setup(mbedtls_platform_context *ctx)
|
||||
{
|
||||
int mbedtls_platform_setup(mbedtls_platform_context *ctx) {
|
||||
(void)ctx;
|
||||
|
||||
return (0);
|
||||
|
@ -329,8 +307,7 @@ int mbedtls_platform_setup(mbedtls_platform_context *ctx)
|
|||
/*
|
||||
* Placeholder platform teardown that does nothing by default
|
||||
*/
|
||||
void mbedtls_platform_teardown(mbedtls_platform_context *ctx)
|
||||
{
|
||||
void mbedtls_platform_teardown(mbedtls_platform_context *ctx) {
|
||||
(void)ctx;
|
||||
}
|
||||
#endif /* MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT */
|
||||
|
|
|
@ -62,8 +62,7 @@
|
|||
*/
|
||||
static void *(* const volatile memset_func)(void *, int, size_t) = memset;
|
||||
|
||||
void mbedtls_platform_zeroize(void *buf, size_t len)
|
||||
{
|
||||
void mbedtls_platform_zeroize(void *buf, size_t len) {
|
||||
memset_func(buf, 0, len);
|
||||
}
|
||||
#endif /* MBEDTLS_PLATFORM_ZEROIZE_ALT */
|
||||
|
|
|
@ -75,8 +75,7 @@
|
|||
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
/* constant-time buffer comparison */
|
||||
static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n)
|
||||
{
|
||||
static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n) {
|
||||
size_t i;
|
||||
const unsigned char *A = (const unsigned char *) a;
|
||||
const unsigned char *B = (const unsigned char *) b;
|
||||
|
@ -92,15 +91,14 @@ static inline int mbedtls_safer_memcmp(const void *a, const void *b, size_t n)
|
|||
int mbedtls_rsa_import(mbedtls_rsa_context *ctx,
|
||||
const mbedtls_mpi *N,
|
||||
const mbedtls_mpi *P, const mbedtls_mpi *Q,
|
||||
const mbedtls_mpi *D, const mbedtls_mpi *E)
|
||||
{
|
||||
const mbedtls_mpi *D, const mbedtls_mpi *E) {
|
||||
int ret;
|
||||
|
||||
if ((N != NULL && (ret = mbedtls_mpi_copy(&ctx->N, N)) != 0) ||
|
||||
(P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
|
||||
(Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
|
||||
(D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
|
||||
(E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
|
||||
(P != NULL && (ret = mbedtls_mpi_copy(&ctx->P, P)) != 0) ||
|
||||
(Q != NULL && (ret = mbedtls_mpi_copy(&ctx->Q, Q)) != 0) ||
|
||||
(D != NULL && (ret = mbedtls_mpi_copy(&ctx->D, D)) != 0) ||
|
||||
(E != NULL && (ret = mbedtls_mpi_copy(&ctx->E, E)) != 0)) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret);
|
||||
}
|
||||
|
||||
|
@ -115,8 +113,7 @@ int mbedtls_rsa_import_raw(mbedtls_rsa_context *ctx,
|
|||
unsigned char const *P, size_t P_len,
|
||||
unsigned char const *Q, size_t Q_len,
|
||||
unsigned char const *D, size_t D_len,
|
||||
unsigned char const *E, size_t E_len)
|
||||
{
|
||||
unsigned char const *E, size_t E_len) {
|
||||
int ret = 0;
|
||||
|
||||
if (N != NULL) {
|
||||
|
@ -150,8 +147,7 @@ cleanup:
|
|||
* It does *not* make guarantees for consistency of the parameters.
|
||||
*/
|
||||
static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
||||
int blinding_needed)
|
||||
{
|
||||
int blinding_needed) {
|
||||
#if !defined(MBEDTLS_RSA_NO_CRT)
|
||||
/* blinding_needed is only used for NO_CRT to decide whether
|
||||
* P,Q need to be present or not. */
|
||||
|
@ -159,7 +155,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
#endif
|
||||
|
||||
if (ctx->len != mbedtls_mpi_size(&ctx->N) ||
|
||||
ctx->len > MBEDTLS_MPI_MAX_SIZE) {
|
||||
ctx->len > MBEDTLS_MPI_MAX_SIZE) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -170,7 +166,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
/* Modular exponentiation wrt. N is always used for
|
||||
* RSA public key operations. */
|
||||
if (mbedtls_mpi_cmp_int(&ctx->N, 0) <= 0 ||
|
||||
mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
|
||||
mbedtls_mpi_get_bit(&ctx->N, 0) == 0) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -179,10 +175,10 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
* used for private key operations and if CRT
|
||||
* is used. */
|
||||
if (is_priv &&
|
||||
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
|
||||
mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
|
||||
mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
|
||||
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
|
||||
mbedtls_mpi_get_bit(&ctx->P, 0) == 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0 ||
|
||||
mbedtls_mpi_get_bit(&ctx->Q, 0) == 0)) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
#endif /* !MBEDTLS_RSA_NO_CRT */
|
||||
|
@ -202,8 +198,8 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
#else
|
||||
if (is_priv &&
|
||||
(mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
|
||||
(mbedtls_mpi_cmp_int(&ctx->DP, 0) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->DQ, 0) <= 0)) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
#endif /* MBEDTLS_RSA_NO_CRT */
|
||||
|
@ -213,8 +209,8 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
* done as part of 1. */
|
||||
#if defined(MBEDTLS_RSA_NO_CRT)
|
||||
if (is_priv && blinding_needed &&
|
||||
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
|
||||
(mbedtls_mpi_cmp_int(&ctx->P, 0) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(&ctx->Q, 0) <= 0)) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
#endif
|
||||
|
@ -223,7 +219,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
* but check for QP >= 1 nonetheless. */
|
||||
#if !defined(MBEDTLS_RSA_NO_CRT)
|
||||
if (is_priv &&
|
||||
mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
|
||||
mbedtls_mpi_cmp_int(&ctx->QP, 0) <= 0) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
}
|
||||
#endif
|
||||
|
@ -231,8 +227,7 @@ static int rsa_check_context(mbedtls_rsa_context const *ctx, int is_priv,
|
|||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx)
|
||||
{
|
||||
int mbedtls_rsa_complete(mbedtls_rsa_context *ctx) {
|
||||
int ret = 0;
|
||||
|
||||
const int have_N = (mbedtls_mpi_cmp_int(&ctx->N, 0) != 0);
|
||||
|
@ -320,8 +315,7 @@ int mbedtls_rsa_export_raw(const mbedtls_rsa_context *ctx,
|
|||
unsigned char *P, size_t P_len,
|
||||
unsigned char *Q, size_t Q_len,
|
||||
unsigned char *D, size_t D_len,
|
||||
unsigned char *E, size_t E_len)
|
||||
{
|
||||
unsigned char *E, size_t E_len) {
|
||||
int ret = 0;
|
||||
|
||||
/* Check if key is private or public */
|
||||
|
@ -362,8 +356,7 @@ cleanup:
|
|||
|
||||
int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
|
||||
mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q,
|
||||
mbedtls_mpi *D, mbedtls_mpi *E)
|
||||
{
|
||||
mbedtls_mpi *D, mbedtls_mpi *E) {
|
||||
int ret;
|
||||
|
||||
/* Check if key is private or public */
|
||||
|
@ -385,10 +378,10 @@ int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
|
|||
/* Export all requested core parameters. */
|
||||
|
||||
if ((N != NULL && (ret = mbedtls_mpi_copy(N, &ctx->N)) != 0) ||
|
||||
(P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
|
||||
(Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
|
||||
(D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
|
||||
(E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
|
||||
(P != NULL && (ret = mbedtls_mpi_copy(P, &ctx->P)) != 0) ||
|
||||
(Q != NULL && (ret = mbedtls_mpi_copy(Q, &ctx->Q)) != 0) ||
|
||||
(D != NULL && (ret = mbedtls_mpi_copy(D, &ctx->D)) != 0) ||
|
||||
(E != NULL && (ret = mbedtls_mpi_copy(E, &ctx->E)) != 0)) {
|
||||
return (ret);
|
||||
}
|
||||
|
||||
|
@ -402,8 +395,7 @@ int mbedtls_rsa_export(const mbedtls_rsa_context *ctx,
|
|||
* can be used in this case.
|
||||
*/
|
||||
int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
|
||||
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP)
|
||||
{
|
||||
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP) {
|
||||
int ret;
|
||||
|
||||
/* Check if key is private or public */
|
||||
|
@ -420,8 +412,8 @@ int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
|
|||
#if !defined(MBEDTLS_RSA_NO_CRT)
|
||||
/* Export all requested blinding parameters. */
|
||||
if ((DP != NULL && (ret = mbedtls_mpi_copy(DP, &ctx->DP)) != 0) ||
|
||||
(DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
|
||||
(QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
|
||||
(DQ != NULL && (ret = mbedtls_mpi_copy(DQ, &ctx->DQ)) != 0) ||
|
||||
(QP != NULL && (ret = mbedtls_mpi_copy(QP, &ctx->QP)) != 0)) {
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret);
|
||||
}
|
||||
#else
|
||||
|
@ -439,8 +431,7 @@ int mbedtls_rsa_export_crt(const mbedtls_rsa_context *ctx,
|
|||
*/
|
||||
void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
|
||||
int padding,
|
||||
int hash_id)
|
||||
{
|
||||
int hash_id) {
|
||||
memset(ctx, 0, sizeof(mbedtls_rsa_context));
|
||||
|
||||
mbedtls_rsa_set_padding(ctx, padding, hash_id);
|
||||
|
@ -453,8 +444,7 @@ void mbedtls_rsa_init(mbedtls_rsa_context *ctx,
|
|||
/*
|
||||
* Set padding for an existing RSA context
|
||||
*/
|
||||
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
|
||||
{
|
||||
void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id) {
|
||||
ctx->padding = padding;
|
||||
ctx->hash_id = hash_id;
|
||||
}
|
||||
|
@ -463,8 +453,7 @@ void mbedtls_rsa_set_padding(mbedtls_rsa_context *ctx, int padding, int hash_id)
|
|||
* Get length in bytes of RSA modulus
|
||||
*/
|
||||
|
||||
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
|
||||
{
|
||||
size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx) {
|
||||
return (ctx->len);
|
||||
}
|
||||
|
||||
|
@ -480,8 +469,7 @@ size_t mbedtls_rsa_get_len(const mbedtls_rsa_context *ctx)
|
|||
int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng,
|
||||
unsigned int nbits, int exponent)
|
||||
{
|
||||
unsigned int nbits, int exponent) {
|
||||
int ret;
|
||||
mbedtls_mpi H, G, L;
|
||||
|
||||
|
@ -580,8 +568,7 @@ cleanup:
|
|||
/*
|
||||
* Check a public RSA key
|
||||
*/
|
||||
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
|
||||
{
|
||||
int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx) {
|
||||
if (rsa_check_context(ctx, 0 /* public */, 0 /* no blinding */) != 0)
|
||||
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
|
||||
|
||||
|
@ -590,8 +577,8 @@ int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
|
|||
}
|
||||
|
||||
if (mbedtls_mpi_get_bit(&ctx->E, 0) == 0 ||
|
||||
mbedtls_mpi_bitlen(&ctx->E) < 2 ||
|
||||
mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
|
||||
mbedtls_mpi_bitlen(&ctx->E) < 2 ||
|
||||
mbedtls_mpi_cmp_mpi(&ctx->E, &ctx->N) >= 0) {
|
||||
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
|
||||
}
|
||||
|
||||
|
@ -601,10 +588,9 @@ int mbedtls_rsa_check_pubkey(const mbedtls_rsa_context *ctx)
|
|||
/*
|
||||
* Check for the consistency of all fields in an RSA private key context
|
||||
*/
|
||||
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
|
||||
{
|
||||
int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx) {
|
||||
if (mbedtls_rsa_check_pubkey(ctx) != 0 ||
|
||||
rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
|
||||
rsa_check_context(ctx, 1 /* private */, 1 /* blinding */) != 0) {
|
||||
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
|
||||
}
|
||||
|
||||
|
@ -627,15 +613,14 @@ int mbedtls_rsa_check_privkey(const mbedtls_rsa_context *ctx)
|
|||
* Check if contexts holding a public and private key match
|
||||
*/
|
||||
int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
|
||||
const mbedtls_rsa_context *prv)
|
||||
{
|
||||
const mbedtls_rsa_context *prv) {
|
||||
if (mbedtls_rsa_check_pubkey(pub) != 0 ||
|
||||
mbedtls_rsa_check_privkey(prv) != 0) {
|
||||
mbedtls_rsa_check_privkey(prv) != 0) {
|
||||
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
|
||||
}
|
||||
|
||||
if (mbedtls_mpi_cmp_mpi(&pub->N, &prv->N) != 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
|
||||
mbedtls_mpi_cmp_mpi(&pub->E, &prv->E) != 0) {
|
||||
return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
|
||||
}
|
||||
|
||||
|
@ -647,8 +632,7 @@ int mbedtls_rsa_check_pub_priv(const mbedtls_rsa_context *pub,
|
|||
*/
|
||||
int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret;
|
||||
size_t olen;
|
||||
mbedtls_mpi T;
|
||||
|
@ -695,8 +679,7 @@ cleanup:
|
|||
* Berlin Heidelberg, 1996. p. 104-113.
|
||||
*/
|
||||
static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
||||
{
|
||||
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) {
|
||||
int ret, count = 0;
|
||||
|
||||
if (ctx->Vf.p != NULL) {
|
||||
|
@ -755,8 +738,7 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,
|
|||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
int ret;
|
||||
size_t olen;
|
||||
|
||||
|
@ -980,8 +962,7 @@ cleanup:
|
|||
* \param md_ctx message digest context to use
|
||||
*/
|
||||
static int mgf_mask(unsigned char *dst, size_t dlen, unsigned char *src,
|
||||
size_t slen, mbedtls_md_context_t *md_ctx)
|
||||
{
|
||||
size_t slen, mbedtls_md_context_t *md_ctx) {
|
||||
unsigned char mask[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char counter[4];
|
||||
unsigned char *p;
|
||||
|
@ -1037,8 +1018,7 @@ int mbedtls_rsa_rsaes_oaep_encrypt(mbedtls_rsa_context *ctx,
|
|||
const unsigned char *label, size_t label_len,
|
||||
size_t ilen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
size_t olen;
|
||||
int ret;
|
||||
unsigned char *p = output;
|
||||
|
@ -1116,8 +1096,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt(mbedtls_rsa_context *ctx,
|
|||
void *p_rng,
|
||||
int mode, size_t ilen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
size_t nb_pad, olen;
|
||||
int ret;
|
||||
unsigned char *p = output;
|
||||
|
@ -1178,8 +1157,7 @@ int mbedtls_rsa_pkcs1_encrypt(mbedtls_rsa_context *ctx,
|
|||
void *p_rng,
|
||||
int mode, size_t ilen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
unsigned char *output) {
|
||||
switch (ctx->padding) {
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
case MBEDTLS_RSA_PKCS_V15:
|
||||
|
@ -1210,8 +1188,7 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
|
|||
size_t *olen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output,
|
||||
size_t output_max_len)
|
||||
{
|
||||
size_t output_max_len) {
|
||||
int ret;
|
||||
size_t ilen, i, pad_len;
|
||||
unsigned char *p, bad, pad_done;
|
||||
|
@ -1264,9 +1241,9 @@ int mbedtls_rsa_rsaes_oaep_decrypt(mbedtls_rsa_context *ctx,
|
|||
/* seed: Apply seedMask to maskedSeed */
|
||||
if ((ret = mgf_mask(buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
|
||||
&md_ctx)) != 0 ||
|
||||
/* DB: Apply dbMask to maskedDB */
|
||||
(ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
|
||||
&md_ctx)) != 0) {
|
||||
/* DB: Apply dbMask to maskedDB */
|
||||
(ret = mgf_mask(buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
|
||||
&md_ctx)) != 0) {
|
||||
mbedtls_md_free(&md_ctx);
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -1341,8 +1318,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt(mbedtls_rsa_context *ctx,
|
|||
int mode, size_t *olen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output,
|
||||
size_t output_max_len)
|
||||
{
|
||||
size_t output_max_len) {
|
||||
int ret;
|
||||
size_t ilen, pad_count = 0, i;
|
||||
unsigned char *p, bad, pad_done = 0;
|
||||
|
@ -1430,8 +1406,7 @@ int mbedtls_rsa_pkcs1_decrypt(mbedtls_rsa_context *ctx,
|
|||
int mode, size_t *olen,
|
||||
const unsigned char *input,
|
||||
unsigned char *output,
|
||||
size_t output_max_len)
|
||||
{
|
||||
size_t output_max_len) {
|
||||
switch (ctx->padding) {
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
case MBEDTLS_RSA_PKCS_V15:
|
||||
|
@ -1462,8 +1437,7 @@ int mbedtls_rsa_rsassa_pss_sign(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
unsigned char *sig)
|
||||
{
|
||||
unsigned char *sig) {
|
||||
size_t olen;
|
||||
unsigned char *p = sig;
|
||||
unsigned char salt[MBEDTLS_MD_MAX_SIZE];
|
||||
|
@ -1585,8 +1559,7 @@ static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
|
|||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
size_t dst_len,
|
||||
unsigned char *dst)
|
||||
{
|
||||
unsigned char *dst) {
|
||||
size_t oid_size = 0;
|
||||
size_t nb_pad = dst_len;
|
||||
unsigned char *p = dst;
|
||||
|
@ -1606,8 +1579,8 @@ static int rsa_rsassa_pkcs1_v15_encode(mbedtls_md_type_t md_alg,
|
|||
/* Double-check that 8 + hashlen + oid_size can be used as a
|
||||
* 1-byte ASN.1 length encoding and that there's no overflow. */
|
||||
if (8 + hashlen + oid_size >= 0x80 ||
|
||||
10 + hashlen < hashlen ||
|
||||
10 + hashlen + oid_size < 10 + hashlen)
|
||||
10 + hashlen < hashlen ||
|
||||
10 + hashlen + oid_size < 10 + hashlen)
|
||||
return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
|
||||
|
||||
/*
|
||||
|
@ -1698,8 +1671,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
unsigned char *sig)
|
||||
{
|
||||
unsigned char *sig) {
|
||||
int ret;
|
||||
unsigned char *sig_try = NULL, *verif = NULL;
|
||||
|
||||
|
@ -1767,8 +1739,7 @@ int mbedtls_rsa_pkcs1_sign(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
unsigned char *sig)
|
||||
{
|
||||
unsigned char *sig) {
|
||||
switch (ctx->padding) {
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
case MBEDTLS_RSA_PKCS_V15:
|
||||
|
@ -1800,8 +1771,7 @@ int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
|
|||
const unsigned char *hash,
|
||||
mbedtls_md_type_t mgf1_hash_id,
|
||||
int expected_salt_len,
|
||||
const unsigned char *sig)
|
||||
{
|
||||
const unsigned char *sig) {
|
||||
int ret;
|
||||
size_t siglen;
|
||||
unsigned char *p;
|
||||
|
@ -1890,7 +1860,7 @@ int mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_rsa_context *ctx,
|
|||
observed_salt_len = hash_start - p;
|
||||
|
||||
if (expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
|
||||
observed_salt_len != (size_t) expected_salt_len) {
|
||||
observed_salt_len != (size_t) expected_salt_len) {
|
||||
ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
|
||||
goto exit;
|
||||
}
|
||||
|
@ -1935,8 +1905,7 @@ int mbedtls_rsa_rsassa_pss_verify(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
const unsigned char *sig)
|
||||
{
|
||||
const unsigned char *sig) {
|
||||
mbedtls_md_type_t mgf1_hash_id = (ctx->hash_id != MBEDTLS_MD_NONE)
|
||||
? (mbedtls_md_type_t) ctx->hash_id
|
||||
: md_alg;
|
||||
|
@ -1960,8 +1929,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
const unsigned char *sig)
|
||||
{
|
||||
const unsigned char *sig) {
|
||||
int ret = 0;
|
||||
const size_t sig_len = ctx->len;
|
||||
unsigned char *encoded = NULL, *encoded_expected = NULL;
|
||||
|
@ -1974,7 +1942,7 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify(mbedtls_rsa_context *ctx,
|
|||
*/
|
||||
|
||||
if ((encoded = mbedtls_calloc(1, sig_len)) == NULL ||
|
||||
(encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
|
||||
(encoded_expected = mbedtls_calloc(1, sig_len)) == NULL) {
|
||||
ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -2029,8 +1997,7 @@ int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
|
|||
mbedtls_md_type_t md_alg,
|
||||
unsigned int hashlen,
|
||||
const unsigned char *hash,
|
||||
const unsigned char *sig)
|
||||
{
|
||||
const unsigned char *sig) {
|
||||
switch (ctx->padding) {
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
case MBEDTLS_RSA_PKCS_V15:
|
||||
|
@ -2052,8 +2019,7 @@ int mbedtls_rsa_pkcs1_verify(mbedtls_rsa_context *ctx,
|
|||
/*
|
||||
* Copy the components of an RSA key
|
||||
*/
|
||||
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src)
|
||||
{
|
||||
int mbedtls_rsa_copy(mbedtls_rsa_context *dst, const mbedtls_rsa_context *src) {
|
||||
int ret;
|
||||
|
||||
dst->ver = src->ver;
|
||||
|
@ -2092,8 +2058,7 @@ cleanup:
|
|||
/*
|
||||
* Free the components of an RSA key
|
||||
*/
|
||||
void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
|
||||
{
|
||||
void mbedtls_rsa_free(mbedtls_rsa_context *ctx) {
|
||||
mbedtls_mpi_free(&ctx->Vi);
|
||||
mbedtls_mpi_free(&ctx->Vf);
|
||||
mbedtls_mpi_free(&ctx->RN);
|
||||
|
@ -2162,8 +2127,7 @@ void mbedtls_rsa_free(mbedtls_rsa_context *ctx)
|
|||
"\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
|
||||
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
static int myrand(void *rng_state, unsigned char *output, size_t len)
|
||||
{
|
||||
static int myrand(void *rng_state, unsigned char *output, size_t len) {
|
||||
#if !defined(__OpenBSD__)
|
||||
size_t i;
|
||||
|
||||
|
@ -2186,8 +2150,7 @@ static int myrand(void *rng_state, unsigned char *output, size_t len)
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_rsa_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_rsa_self_test(int verbose) {
|
||||
int ret = 0;
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
size_t len;
|
||||
|
@ -2221,7 +2184,7 @@ int mbedtls_rsa_self_test(int verbose)
|
|||
mbedtls_printf(" RSA key validation: ");
|
||||
|
||||
if (mbedtls_rsa_check_pubkey(&rsa) != 0 ||
|
||||
mbedtls_rsa_check_privkey(&rsa) != 0) {
|
||||
mbedtls_rsa_check_privkey(&rsa) != 0) {
|
||||
if (verbose != 0)
|
||||
mbedtls_printf("failed\n");
|
||||
|
||||
|
|
|
@ -69,8 +69,7 @@
|
|||
*/
|
||||
int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
|
||||
mbedtls_mpi const *E, mbedtls_mpi const *D,
|
||||
mbedtls_mpi *P, mbedtls_mpi *Q)
|
||||
{
|
||||
mbedtls_mpi *P, mbedtls_mpi *Q) {
|
||||
int ret = 0;
|
||||
|
||||
uint16_t attempt; /* Number of current attempt */
|
||||
|
@ -97,10 +96,10 @@ int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
|
|||
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
||||
|
||||
if (mbedtls_mpi_cmp_int(N, 0) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(D, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
||||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
||||
mbedtls_mpi_cmp_int(D, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
||||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
||||
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -156,7 +155,7 @@ int mbedtls_rsa_deduce_primes(mbedtls_mpi const *N,
|
|||
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd(P, &K, N));
|
||||
|
||||
if (mbedtls_mpi_cmp_int(P, 1) == 1 &&
|
||||
mbedtls_mpi_cmp_mpi(P, N) == -1) {
|
||||
mbedtls_mpi_cmp_mpi(P, N) == -1) {
|
||||
/*
|
||||
* Have found a nontrivial divisor P of N.
|
||||
* Set Q := N / P.
|
||||
|
@ -199,8 +198,7 @@ cleanup:
|
|||
int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
|
||||
mbedtls_mpi const *Q,
|
||||
mbedtls_mpi const *E,
|
||||
mbedtls_mpi *D)
|
||||
{
|
||||
mbedtls_mpi *D) {
|
||||
int ret = 0;
|
||||
mbedtls_mpi K, L;
|
||||
|
||||
|
@ -208,8 +206,8 @@ int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
|
|||
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
||||
|
||||
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(E, 0) == 0) {
|
||||
mbedtls_mpi_cmp_int(Q, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(E, 0) == 0) {
|
||||
return (MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
||||
}
|
||||
|
||||
|
@ -243,8 +241,7 @@ cleanup:
|
|||
*/
|
||||
int mbedtls_rsa_validate_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
|
||||
const mbedtls_mpi *D, const mbedtls_mpi *DP,
|
||||
const mbedtls_mpi *DQ, const mbedtls_mpi *QP)
|
||||
{
|
||||
const mbedtls_mpi *DQ, const mbedtls_mpi *QP) {
|
||||
int ret = 0;
|
||||
|
||||
mbedtls_mpi K, L;
|
||||
|
@ -305,8 +302,8 @@ cleanup:
|
|||
|
||||
/* Wrap MPI error codes by RSA check failure error code */
|
||||
if (ret != 0 &&
|
||||
ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
|
||||
ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
|
||||
ret != MBEDTLS_ERR_RSA_KEY_CHECK_FAILED &&
|
||||
ret != MBEDTLS_ERR_RSA_BAD_INPUT_DATA) {
|
||||
ret += MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
}
|
||||
|
||||
|
@ -323,8 +320,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|||
const mbedtls_mpi *Q, const mbedtls_mpi *D,
|
||||
const mbedtls_mpi *E,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
void *p_rng) {
|
||||
int ret = 0;
|
||||
mbedtls_mpi K, L;
|
||||
|
||||
|
@ -337,13 +333,13 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|||
|
||||
#if defined(MBEDTLS_GENPRIME)
|
||||
if (f_rng != NULL && P != NULL &&
|
||||
(ret = mbedtls_mpi_is_prime(P, f_rng, p_rng)) != 0) {
|
||||
(ret = mbedtls_mpi_is_prime(P, f_rng, p_rng)) != 0) {
|
||||
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (f_rng != NULL && Q != NULL &&
|
||||
(ret = mbedtls_mpi_is_prime(Q, f_rng, p_rng)) != 0) {
|
||||
(ret = mbedtls_mpi_is_prime(Q, f_rng, p_rng)) != 0) {
|
||||
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -359,7 +355,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|||
if (P != NULL && Q != NULL && N != NULL) {
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&K, P, Q));
|
||||
if (mbedtls_mpi_cmp_int(N, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(&K, N) != 0) {
|
||||
mbedtls_mpi_cmp_mpi(&K, N) != 0) {
|
||||
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -371,9 +367,9 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|||
|
||||
if (N != NULL && D != NULL && E != NULL) {
|
||||
if (mbedtls_mpi_cmp_int(D, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
||||
mbedtls_mpi_cmp_int(E, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(D, N) >= 0 ||
|
||||
mbedtls_mpi_cmp_mpi(E, N) >= 0) {
|
||||
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -385,7 +381,7 @@ int mbedtls_rsa_validate_params(const mbedtls_mpi *N, const mbedtls_mpi *P,
|
|||
|
||||
if (P != NULL && Q != NULL && D != NULL && E != NULL) {
|
||||
if (mbedtls_mpi_cmp_int(P, 1) <= 0 ||
|
||||
mbedtls_mpi_cmp_int(Q, 1) <= 0) {
|
||||
mbedtls_mpi_cmp_int(Q, 1) <= 0) {
|
||||
ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
|
||||
goto cleanup;
|
||||
}
|
||||
|
@ -426,8 +422,7 @@ cleanup:
|
|||
|
||||
int mbedtls_rsa_deduce_crt(const mbedtls_mpi *P, const mbedtls_mpi *Q,
|
||||
const mbedtls_mpi *D, mbedtls_mpi *DP,
|
||||
mbedtls_mpi *DQ, mbedtls_mpi *QP)
|
||||
{
|
||||
mbedtls_mpi *DQ, mbedtls_mpi *QP) {
|
||||
int ret = 0;
|
||||
mbedtls_mpi K;
|
||||
mbedtls_mpi_init(&K);
|
||||
|
|
|
@ -73,13 +73,11 @@
|
|||
}
|
||||
#endif
|
||||
|
||||
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
|
||||
{
|
||||
void mbedtls_sha1_init(mbedtls_sha1_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_sha1_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
||||
{
|
||||
void mbedtls_sha1_free(mbedtls_sha1_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -87,16 +85,14 @@ void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
|
|||
}
|
||||
|
||||
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
|
||||
const mbedtls_sha1_context *src)
|
||||
{
|
||||
const mbedtls_sha1_context *src) {
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-1 context setup
|
||||
*/
|
||||
int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
|
||||
{
|
||||
int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx) {
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
|
@ -110,16 +106,14 @@ int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
|
|||
}
|
||||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
|
||||
{
|
||||
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx) {
|
||||
mbedtls_sha1_starts_ret(ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_SHA1_PROCESS_ALT)
|
||||
int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
uint32_t temp, W[16], A, B, C, D, E;
|
||||
|
||||
GET_UINT32_BE(W[ 0], data, 0);
|
||||
|
@ -278,8 +272,7 @@ int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
mbedtls_internal_sha1_process(ctx, data);
|
||||
}
|
||||
#endif
|
||||
|
@ -290,8 +283,7 @@ void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
|
|||
*/
|
||||
int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
int ret;
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
@ -336,8 +328,7 @@ int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
mbedtls_sha1_update_ret(ctx, input, ilen);
|
||||
}
|
||||
#endif
|
||||
|
@ -346,8 +337,7 @@ void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
|
|||
* SHA-1 final digest
|
||||
*/
|
||||
int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
|
||||
unsigned char output[20])
|
||||
{
|
||||
unsigned char output[20]) {
|
||||
int ret;
|
||||
uint32_t used;
|
||||
uint32_t high, low;
|
||||
|
@ -399,8 +389,7 @@ int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
|
||||
unsigned char output[20])
|
||||
{
|
||||
unsigned char output[20]) {
|
||||
mbedtls_sha1_finish_ret(ctx, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -412,8 +401,7 @@ void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
|
|||
*/
|
||||
int mbedtls_sha1_ret(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[20])
|
||||
{
|
||||
unsigned char output[20]) {
|
||||
int ret;
|
||||
mbedtls_sha1_context ctx;
|
||||
|
||||
|
@ -437,8 +425,7 @@ exit:
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha1(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[20])
|
||||
{
|
||||
unsigned char output[20]) {
|
||||
mbedtls_sha1_ret(input, ilen, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -475,8 +462,7 @@ static const unsigned char sha1_test_sum[3][20] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_sha1_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_sha1_self_test(int verbose) {
|
||||
int i, j, buflen, ret = 0;
|
||||
unsigned char buf[1024];
|
||||
unsigned char sha1sum[20];
|
||||
|
|
|
@ -76,13 +76,11 @@
|
|||
} while( 0 )
|
||||
#endif
|
||||
|
||||
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
void mbedtls_sha256_init(mbedtls_sha256_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_sha256_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
void mbedtls_sha256_free(mbedtls_sha256_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -90,16 +88,14 @@ void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
|||
}
|
||||
|
||||
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
|
||||
const mbedtls_sha256_context *src)
|
||||
{
|
||||
const mbedtls_sha256_context *src) {
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 context setup
|
||||
*/
|
||||
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
|
||||
{
|
||||
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224) {
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
|
@ -132,8 +128,7 @@ int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
|
||||
int is224)
|
||||
{
|
||||
int is224) {
|
||||
mbedtls_sha256_starts_ret(ctx, is224);
|
||||
}
|
||||
#endif
|
||||
|
@ -184,8 +179,7 @@ static const uint32_t K[] = {
|
|||
}
|
||||
|
||||
int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
uint32_t temp1, temp2, W[64];
|
||||
uint32_t A[8];
|
||||
unsigned int i;
|
||||
|
@ -247,8 +241,7 @@ int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
|
||||
const unsigned char data[64])
|
||||
{
|
||||
const unsigned char data[64]) {
|
||||
mbedtls_internal_sha256_process(ctx, data);
|
||||
}
|
||||
#endif
|
||||
|
@ -259,8 +252,7 @@ void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
|
|||
*/
|
||||
int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
int ret;
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
@ -305,8 +297,7 @@ int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
mbedtls_sha256_update_ret(ctx, input, ilen);
|
||||
}
|
||||
#endif
|
||||
|
@ -315,8 +306,7 @@ void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
|
|||
* SHA-256 final digest
|
||||
*/
|
||||
int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
|
||||
unsigned char output[32])
|
||||
{
|
||||
unsigned char output[32]) {
|
||||
int ret;
|
||||
uint32_t used;
|
||||
uint32_t high, low;
|
||||
|
@ -373,8 +363,7 @@ int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
|
||||
unsigned char output[32])
|
||||
{
|
||||
unsigned char output[32]) {
|
||||
mbedtls_sha256_finish_ret(ctx, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -387,8 +376,7 @@ void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
|
|||
int mbedtls_sha256_ret(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[32],
|
||||
int is224)
|
||||
{
|
||||
int is224) {
|
||||
int ret;
|
||||
mbedtls_sha256_context ctx;
|
||||
|
||||
|
@ -413,8 +401,7 @@ exit:
|
|||
void mbedtls_sha256(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[32],
|
||||
int is224)
|
||||
{
|
||||
int is224) {
|
||||
mbedtls_sha256_ret(input, ilen, output, is224);
|
||||
}
|
||||
#endif
|
||||
|
@ -482,8 +469,7 @@ static const unsigned char sha256_test_sum[6][32] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_sha256_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_sha256_self_test(int verbose) {
|
||||
int i, j, k, buflen, ret = 0;
|
||||
unsigned char *buf;
|
||||
unsigned char sha256sum[32];
|
||||
|
|
|
@ -90,13 +90,11 @@
|
|||
}
|
||||
#endif /* PUT_UINT64_BE */
|
||||
|
||||
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
|
||||
{
|
||||
void mbedtls_sha512_init(mbedtls_sha512_context *ctx) {
|
||||
memset(ctx, 0, sizeof(mbedtls_sha512_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
|
||||
{
|
||||
void mbedtls_sha512_free(mbedtls_sha512_context *ctx) {
|
||||
if (ctx == NULL)
|
||||
return;
|
||||
|
||||
|
@ -104,16 +102,14 @@ void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
|
|||
}
|
||||
|
||||
void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
|
||||
const mbedtls_sha512_context *src)
|
||||
{
|
||||
const mbedtls_sha512_context *src) {
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-512 context setup
|
||||
*/
|
||||
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
|
||||
{
|
||||
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384) {
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
|
@ -146,8 +142,7 @@ int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_starts(mbedtls_sha512_context *ctx,
|
||||
int is384)
|
||||
{
|
||||
int is384) {
|
||||
mbedtls_sha512_starts_ret(ctx, is384);
|
||||
}
|
||||
#endif
|
||||
|
@ -201,8 +196,7 @@ static const uint64_t K[80] = {
|
|||
};
|
||||
|
||||
int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128])
|
||||
{
|
||||
const unsigned char data[128]) {
|
||||
int i;
|
||||
uint64_t temp1, temp2, W[80];
|
||||
uint64_t A, B, C, D, E, F, G, H;
|
||||
|
@ -278,8 +272,7 @@ int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
|
||||
const unsigned char data[128])
|
||||
{
|
||||
const unsigned char data[128]) {
|
||||
mbedtls_internal_sha512_process(ctx, data);
|
||||
}
|
||||
#endif
|
||||
|
@ -290,8 +283,7 @@ void mbedtls_sha512_process(mbedtls_sha512_context *ctx,
|
|||
*/
|
||||
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
int ret;
|
||||
size_t fill;
|
||||
unsigned int left;
|
||||
|
@ -335,8 +327,7 @@ int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx,
|
|||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
size_t ilen) {
|
||||
mbedtls_sha512_update_ret(ctx, input, ilen);
|
||||
}
|
||||
#endif
|
||||
|
@ -345,8 +336,7 @@ void mbedtls_sha512_update(mbedtls_sha512_context *ctx,
|
|||
* SHA-512 final digest
|
||||
*/
|
||||
int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64])
|
||||
{
|
||||
unsigned char output[64]) {
|
||||
int ret;
|
||||
unsigned used;
|
||||
uint64_t high, low;
|
||||
|
@ -404,8 +394,7 @@ int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx,
|
|||
|
||||
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
||||
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
|
||||
unsigned char output[64])
|
||||
{
|
||||
unsigned char output[64]) {
|
||||
mbedtls_sha512_finish_ret(ctx, output);
|
||||
}
|
||||
#endif
|
||||
|
@ -418,8 +407,7 @@ void mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
|
|||
int mbedtls_sha512_ret(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384)
|
||||
{
|
||||
int is384) {
|
||||
int ret;
|
||||
mbedtls_sha512_context ctx;
|
||||
|
||||
|
@ -444,8 +432,7 @@ exit:
|
|||
void mbedtls_sha512(const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[64],
|
||||
int is384)
|
||||
{
|
||||
int is384) {
|
||||
mbedtls_sha512_ret(input, ilen, output, is384);
|
||||
}
|
||||
#endif
|
||||
|
@ -535,8 +522,7 @@ static const unsigned char sha512_test_sum[6][64] = {
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_sha512_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_sha512_self_test(int verbose) {
|
||||
int i, j, k, buflen, ret = 0;
|
||||
unsigned char *buf;
|
||||
unsigned char sha512sum[64];
|
||||
|
|
|
@ -32,16 +32,14 @@
|
|||
#include "mbedtls/threading.h"
|
||||
|
||||
#if defined(MBEDTLS_THREADING_PTHREAD)
|
||||
static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static void threading_mutex_init_pthread(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL)
|
||||
return;
|
||||
|
||||
mutex->is_valid = pthread_mutex_init(&mutex->mutex, NULL) == 0;
|
||||
}
|
||||
|
||||
static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || !mutex->is_valid)
|
||||
return;
|
||||
|
||||
|
@ -49,8 +47,7 @@ static void threading_mutex_free_pthread(mbedtls_threading_mutex_t *mutex)
|
|||
mutex->is_valid = 0;
|
||||
}
|
||||
|
||||
static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || ! mutex->is_valid)
|
||||
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -60,8 +57,7 @@ static int threading_mutex_lock_pthread(mbedtls_threading_mutex_t *mutex)
|
|||
return (0);
|
||||
}
|
||||
|
||||
static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static int threading_mutex_unlock_pthread(mbedtls_threading_mutex_t *mutex) {
|
||||
if (mutex == NULL || ! mutex->is_valid)
|
||||
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
|
||||
|
||||
|
@ -84,13 +80,11 @@ int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_unloc
|
|||
#endif /* MBEDTLS_THREADING_PTHREAD */
|
||||
|
||||
#if defined(MBEDTLS_THREADING_ALT)
|
||||
static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static int threading_mutex_fail(mbedtls_threading_mutex_t *mutex) {
|
||||
((void) mutex);
|
||||
return (MBEDTLS_ERR_THREADING_BAD_INPUT_DATA);
|
||||
}
|
||||
static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex)
|
||||
{
|
||||
static void threading_mutex_dummy(mbedtls_threading_mutex_t *mutex) {
|
||||
((void) mutex);
|
||||
return;
|
||||
}
|
||||
|
@ -106,8 +100,7 @@ int (*mbedtls_mutex_unlock)(mbedtls_threading_mutex_t *) = threading_mutex_fail;
|
|||
void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
|
||||
void (*mutex_free)(mbedtls_threading_mutex_t *),
|
||||
int (*mutex_lock)(mbedtls_threading_mutex_t *),
|
||||
int (*mutex_unlock)(mbedtls_threading_mutex_t *))
|
||||
{
|
||||
int (*mutex_unlock)(mbedtls_threading_mutex_t *)) {
|
||||
mbedtls_mutex_init = mutex_init;
|
||||
mbedtls_mutex_free = mutex_free;
|
||||
mbedtls_mutex_lock = mutex_lock;
|
||||
|
@ -121,8 +114,7 @@ void mbedtls_threading_set_alt(void (*mutex_init)(mbedtls_threading_mutex_t *),
|
|||
/*
|
||||
* Free global mutexes
|
||||
*/
|
||||
void mbedtls_threading_free_alt(void)
|
||||
{
|
||||
void mbedtls_threading_free_alt(void) {
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
mbedtls_mutex_free(&mbedtls_threading_readdir_mutex);
|
||||
#endif
|
||||
|
|
|
@ -78,8 +78,7 @@ struct _hr_time {
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long tsc;
|
||||
__asm rdtsc
|
||||
__asm mov [tsc], eax
|
||||
|
@ -95,8 +94,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long lo, hi;
|
||||
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
|
||||
return (lo);
|
||||
|
@ -109,8 +107,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long lo, hi;
|
||||
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
|
||||
return (lo | (hi << 32));
|
||||
|
@ -123,8 +120,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long tbl, tbu0, tbu1;
|
||||
|
||||
do {
|
||||
|
@ -146,8 +142,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
#else
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long tick;
|
||||
asm volatile("rdpr %%tick, %0;" : "=&r"(tick));
|
||||
return (tick);
|
||||
|
@ -161,8 +156,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long tick;
|
||||
asm volatile(".byte 0x83, 0x41, 0x00, 0x00");
|
||||
asm volatile("mov %%g1, %0" : "=r"(tick));
|
||||
|
@ -176,8 +170,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long cc;
|
||||
asm volatile("rpcc %0" : "=r"(cc));
|
||||
return (cc & 0xFFFFFFFF);
|
||||
|
@ -190,8 +183,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
unsigned long itc;
|
||||
asm volatile("mov %0 = ar.itc" : "=r"(itc));
|
||||
return (itc);
|
||||
|
@ -204,8 +196,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
|
||||
#define HAVE_HARDCLOCK
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
LARGE_INTEGER offset;
|
||||
|
||||
QueryPerformanceCounter(&offset);
|
||||
|
@ -221,8 +212,7 @@ unsigned long mbedtls_timing_hardclock(void)
|
|||
static int hardclock_init = 0;
|
||||
static struct timeval tv_init;
|
||||
|
||||
unsigned long mbedtls_timing_hardclock(void)
|
||||
{
|
||||
unsigned long mbedtls_timing_hardclock(void) {
|
||||
struct timeval tv_cur;
|
||||
|
||||
if (hardclock_init == 0) {
|
||||
|
@ -248,8 +238,7 @@ volatile int mbedtls_timing_alarmed = 0;
|
|||
|
||||
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
||||
|
||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
||||
{
|
||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) {
|
||||
struct _hr_time *t = (struct _hr_time *) val;
|
||||
|
||||
if (reset) {
|
||||
|
@ -269,16 +258,14 @@ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int r
|
|||
/* It's OK to use a global because alarm() is supposed to be global anyway */
|
||||
static DWORD alarmMs;
|
||||
|
||||
static DWORD WINAPI TimerProc(LPVOID TimerContext)
|
||||
{
|
||||
static DWORD WINAPI TimerProc(LPVOID TimerContext) {
|
||||
((void) TimerContext);
|
||||
Sleep(alarmMs);
|
||||
mbedtls_timing_alarmed = 1;
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
void mbedtls_set_alarm(int seconds)
|
||||
{
|
||||
void mbedtls_set_alarm(int seconds) {
|
||||
DWORD ThreadId;
|
||||
|
||||
if (seconds == 0) {
|
||||
|
@ -295,8 +282,7 @@ void mbedtls_set_alarm(int seconds)
|
|||
|
||||
#else /* _WIN32 && !EFIX64 && !EFI32 */
|
||||
|
||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset)
|
||||
{
|
||||
unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int reset) {
|
||||
struct _hr_time *t = (struct _hr_time *) val;
|
||||
|
||||
if (reset) {
|
||||
|
@ -312,14 +298,12 @@ unsigned long mbedtls_timing_get_timer(struct mbedtls_timing_hr_time *val, int r
|
|||
}
|
||||
}
|
||||
|
||||
static void sighandler(int signum)
|
||||
{
|
||||
static void sighandler(int signum) {
|
||||
mbedtls_timing_alarmed = 1;
|
||||
signal(signum, sighandler);
|
||||
}
|
||||
|
||||
void mbedtls_set_alarm(int seconds)
|
||||
{
|
||||
void mbedtls_set_alarm(int seconds) {
|
||||
mbedtls_timing_alarmed = 0;
|
||||
signal(SIGALRM, sighandler);
|
||||
alarm(seconds);
|
||||
|
@ -335,8 +319,7 @@ void mbedtls_set_alarm(int seconds)
|
|||
/*
|
||||
* Set delays to watch
|
||||
*/
|
||||
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
|
||||
{
|
||||
void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms) {
|
||||
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
||||
|
||||
ctx->int_ms = int_ms;
|
||||
|
@ -349,8 +332,7 @@ void mbedtls_timing_set_delay(void *data, uint32_t int_ms, uint32_t fin_ms)
|
|||
/*
|
||||
* Get number of delays expired
|
||||
*/
|
||||
int mbedtls_timing_get_delay(void *data)
|
||||
{
|
||||
int mbedtls_timing_get_delay(void *data) {
|
||||
mbedtls_timing_delay_context *ctx = (mbedtls_timing_delay_context *) data;
|
||||
unsigned long elapsed_ms;
|
||||
|
||||
|
@ -376,8 +358,7 @@ int mbedtls_timing_get_delay(void *data)
|
|||
* Busy-waits for the given number of milliseconds.
|
||||
* Used for testing mbedtls_timing_hardclock.
|
||||
*/
|
||||
static void busy_msleep(unsigned long msec)
|
||||
{
|
||||
static void busy_msleep(unsigned long msec) {
|
||||
struct mbedtls_timing_hr_time hires;
|
||||
unsigned long i = 0; /* for busy-waiting */
|
||||
volatile unsigned long j; /* to prevent optimisation */
|
||||
|
@ -413,8 +394,7 @@ static void busy_msleep(unsigned long msec)
|
|||
* Warning: this is work in progress, some tests may not be reliable enough
|
||||
* yet! False positives may happen.
|
||||
*/
|
||||
int mbedtls_timing_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_timing_self_test(int verbose) {
|
||||
unsigned long cycles = 0, ratio = 0;
|
||||
unsigned long millisecs = 0, secs = 0;
|
||||
int hardfail = 0;
|
||||
|
@ -509,7 +489,7 @@ hard_test:
|
|||
|
||||
/* Allow variation up to 20% */
|
||||
if (cycles / millisecs < ratio - ratio / 5 ||
|
||||
cycles / millisecs > ratio + ratio / 5) {
|
||||
cycles / millisecs > ratio + ratio / 5) {
|
||||
hardfail++;
|
||||
goto hard_test;
|
||||
}
|
||||
|
|
|
@ -79,8 +79,7 @@
|
|||
* CertificateSerialNumber ::= INTEGER
|
||||
*/
|
||||
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_buf *serial)
|
||||
{
|
||||
mbedtls_x509_buf *serial) {
|
||||
int ret;
|
||||
|
||||
if ((end - *p) < 1)
|
||||
|
@ -88,7 +87,7 @@ int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
|
|||
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
||||
|
||||
if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
|
||||
**p != MBEDTLS_ASN1_INTEGER)
|
||||
**p != MBEDTLS_ASN1_INTEGER)
|
||||
return (MBEDTLS_ERR_X509_INVALID_SERIAL +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
||||
|
||||
|
@ -110,8 +109,7 @@ int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
|
|||
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
||||
*/
|
||||
int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_buf *alg)
|
||||
{
|
||||
mbedtls_x509_buf *alg) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0)
|
||||
|
@ -124,8 +122,7 @@ int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
|
|||
* Parse an algorithm identifier with (optional) paramaters
|
||||
*/
|
||||
int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
|
||||
{
|
||||
mbedtls_x509_buf *alg, mbedtls_x509_buf *params) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0)
|
||||
|
@ -144,8 +141,7 @@ int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
|
|||
*
|
||||
* For HashAlgorithm, parameters MUST be NULL or absent.
|
||||
*/
|
||||
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
|
||||
{
|
||||
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg) {
|
||||
int ret;
|
||||
unsigned char *p;
|
||||
const unsigned char *end;
|
||||
|
@ -205,8 +201,7 @@ static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_
|
|||
*/
|
||||
int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
|
||||
mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
|
||||
int *salt_len)
|
||||
{
|
||||
int *salt_len) {
|
||||
int ret;
|
||||
unsigned char *p;
|
||||
const unsigned char *end, *end2;
|
||||
|
@ -340,8 +335,7 @@ int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
|
|||
*/
|
||||
static int x509_get_attr_type_value(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_name *cur)
|
||||
{
|
||||
mbedtls_x509_name *cur) {
|
||||
int ret;
|
||||
size_t len;
|
||||
mbedtls_x509_buf *oid;
|
||||
|
@ -369,9 +363,9 @@ static int x509_get_attr_type_value(unsigned char **p,
|
|||
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
||||
|
||||
if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
|
||||
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
|
||||
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
|
||||
**p != MBEDTLS_ASN1_BIT_STRING)
|
||||
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
|
||||
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
|
||||
**p != MBEDTLS_ASN1_BIT_STRING)
|
||||
return (MBEDTLS_ERR_X509_INVALID_NAME +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
||||
|
||||
|
@ -413,8 +407,7 @@ static int x509_get_attr_type_value(unsigned char **p,
|
|||
* this list, eg mbedtls_x509_dn_gets().
|
||||
*/
|
||||
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_name *cur)
|
||||
{
|
||||
mbedtls_x509_name *cur) {
|
||||
int ret;
|
||||
size_t set_len;
|
||||
const unsigned char *end_set;
|
||||
|
@ -463,8 +456,7 @@ int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
|
|||
}
|
||||
}
|
||||
|
||||
static int x509_parse_int(unsigned char **p, size_t n, int *res)
|
||||
{
|
||||
static int x509_parse_int(unsigned char **p, size_t n, int *res) {
|
||||
*res = 0;
|
||||
|
||||
for (; n > 0; --n) {
|
||||
|
@ -478,8 +470,7 @@ static int x509_parse_int(unsigned char **p, size_t n, int *res)
|
|||
return (0);
|
||||
}
|
||||
|
||||
static int x509_date_is_valid(const mbedtls_x509_time *t)
|
||||
{
|
||||
static int x509_date_is_valid(const mbedtls_x509_time *t) {
|
||||
int ret = MBEDTLS_ERR_X509_INVALID_DATE;
|
||||
int month_len;
|
||||
|
||||
|
@ -506,7 +497,7 @@ static int x509_date_is_valid(const mbedtls_x509_time *t)
|
|||
break;
|
||||
case 2:
|
||||
if ((!(t->year % 4) && t->year % 100) ||
|
||||
!(t->year % 400))
|
||||
!(t->year % 400))
|
||||
month_len = 29;
|
||||
else
|
||||
month_len = 28;
|
||||
|
@ -524,8 +515,7 @@ static int x509_date_is_valid(const mbedtls_x509_time *t)
|
|||
* field.
|
||||
*/
|
||||
static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
|
||||
mbedtls_x509_time *tm)
|
||||
{
|
||||
mbedtls_x509_time *tm) {
|
||||
int ret;
|
||||
|
||||
/*
|
||||
|
@ -585,8 +575,7 @@ static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
|
|||
* generalTime GeneralizedTime }
|
||||
*/
|
||||
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_time *tm)
|
||||
{
|
||||
mbedtls_x509_time *tm) {
|
||||
int ret;
|
||||
size_t len, year_len;
|
||||
unsigned char tag;
|
||||
|
@ -614,8 +603,7 @@ int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
|
|||
return x509_parse_time(p, len, year_len, tm);
|
||||
}
|
||||
|
||||
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
|
||||
{
|
||||
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig) {
|
||||
int ret;
|
||||
size_t len;
|
||||
int tag_type;
|
||||
|
@ -643,8 +631,7 @@ int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x5
|
|||
*/
|
||||
int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
|
||||
mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
|
||||
void **sig_opts)
|
||||
{
|
||||
void **sig_opts) {
|
||||
int ret;
|
||||
|
||||
if (*sig_opts != NULL)
|
||||
|
@ -676,7 +663,7 @@ int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509
|
|||
{
|
||||
/* Make sure parameters are absent or NULL */
|
||||
if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
|
||||
sig_params->len != 0)
|
||||
sig_params->len != 0)
|
||||
return (MBEDTLS_ERR_X509_INVALID_ALG);
|
||||
}
|
||||
|
||||
|
@ -688,8 +675,7 @@ int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509
|
|||
* be either manually updated or extensions should be parsed!)
|
||||
*/
|
||||
int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
||||
mbedtls_x509_buf *ext, int tag)
|
||||
{
|
||||
mbedtls_x509_buf *ext, int tag) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -728,8 +714,7 @@ int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
|||
* Store the name in printable form into buf; no more
|
||||
* than size characters will be written
|
||||
*/
|
||||
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
||||
{
|
||||
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn) {
|
||||
int ret;
|
||||
size_t i, n;
|
||||
unsigned char c, merge = 0;
|
||||
|
@ -786,8 +771,7 @@ int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
|||
* Store the serial in printable form into buf; no more
|
||||
* than size characters will be written
|
||||
*/
|
||||
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
|
||||
{
|
||||
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial) {
|
||||
int ret;
|
||||
size_t i, n, nr;
|
||||
char *p;
|
||||
|
@ -820,8 +804,7 @@ int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *ser
|
|||
*/
|
||||
int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
|
||||
mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
|
||||
const void *sig_opts)
|
||||
{
|
||||
const void *sig_opts) {
|
||||
int ret;
|
||||
char *p = buf;
|
||||
size_t n = size;
|
||||
|
@ -862,8 +845,7 @@ int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *si
|
|||
/*
|
||||
* Helper for writing "RSA key size", "EC key size", etc
|
||||
*/
|
||||
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
|
||||
{
|
||||
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name) {
|
||||
char *p = buf;
|
||||
size_t n = buf_size;
|
||||
int ret;
|
||||
|
@ -879,8 +861,7 @@ int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
|
|||
* Set the time structure to the current time.
|
||||
* Return 0 on success, non-zero on failure.
|
||||
*/
|
||||
static int x509_get_current_time(mbedtls_x509_time *now)
|
||||
{
|
||||
static int x509_get_current_time(mbedtls_x509_time *now) {
|
||||
struct tm *lt, tm_buf;
|
||||
mbedtls_time_t tt;
|
||||
int ret = 0;
|
||||
|
@ -909,46 +890,44 @@ static int x509_get_current_time(mbedtls_x509_time *now)
|
|||
/*
|
||||
* Return 0 if before <= after, 1 otherwise
|
||||
*/
|
||||
static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
|
||||
{
|
||||
static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after) {
|
||||
if (before->year > after->year)
|
||||
return (1);
|
||||
|
||||
if (before->year == after->year &&
|
||||
before->mon > after->mon)
|
||||
before->mon > after->mon)
|
||||
return (1);
|
||||
|
||||
if (before->year == after->year &&
|
||||
before->mon == after->mon &&
|
||||
before->day > after->day)
|
||||
before->mon == after->mon &&
|
||||
before->day > after->day)
|
||||
return (1);
|
||||
|
||||
if (before->year == after->year &&
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour > after->hour)
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour > after->hour)
|
||||
return (1);
|
||||
|
||||
if (before->year == after->year &&
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour == after->hour &&
|
||||
before->min > after->min)
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour == after->hour &&
|
||||
before->min > after->min)
|
||||
return (1);
|
||||
|
||||
if (before->year == after->year &&
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour == after->hour &&
|
||||
before->min == after->min &&
|
||||
before->sec > after->sec)
|
||||
before->mon == after->mon &&
|
||||
before->day == after->day &&
|
||||
before->hour == after->hour &&
|
||||
before->min == after->min &&
|
||||
before->sec > after->sec)
|
||||
return (1);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
||||
{
|
||||
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) {
|
||||
mbedtls_x509_time now;
|
||||
|
||||
if (x509_get_current_time(&now) != 0)
|
||||
|
@ -957,8 +936,7 @@ int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
|||
return (x509_check_time(&now, to));
|
||||
}
|
||||
|
||||
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
||||
{
|
||||
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) {
|
||||
mbedtls_x509_time now;
|
||||
|
||||
if (x509_get_current_time(&now) != 0)
|
||||
|
@ -969,14 +947,12 @@ int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
|||
|
||||
#else /* MBEDTLS_HAVE_TIME_DATE */
|
||||
|
||||
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
||||
{
|
||||
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to) {
|
||||
((void) to);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
||||
{
|
||||
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from) {
|
||||
((void) from);
|
||||
return (0);
|
||||
}
|
||||
|
@ -990,8 +966,7 @@ int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
|||
/*
|
||||
* Checkup routine
|
||||
*/
|
||||
int mbedtls_x509_self_test(int verbose)
|
||||
{
|
||||
int mbedtls_x509_self_test(int verbose) {
|
||||
#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
|
||||
int ret;
|
||||
uint32_t flags;
|
||||
|
|
|
@ -74,8 +74,7 @@
|
|||
*/
|
||||
static int x509_crl_get_version(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *ver)
|
||||
{
|
||||
int *ver) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
|
||||
|
@ -99,8 +98,7 @@ static int x509_crl_get_version(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_crl_ext(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_buf *ext)
|
||||
{
|
||||
mbedtls_x509_buf *ext) {
|
||||
int ret;
|
||||
|
||||
/*
|
||||
|
@ -142,7 +140,7 @@ static int x509_get_crl_ext(unsigned char **p,
|
|||
/* Get optional critical */
|
||||
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data,
|
||||
&is_critical)) != 0 &&
|
||||
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
|
||||
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
|
||||
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
|
||||
}
|
||||
|
||||
|
@ -175,8 +173,7 @@ static int x509_get_crl_ext(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_crl_entry_ext(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_buf *ext)
|
||||
{
|
||||
mbedtls_x509_buf *ext) {
|
||||
int ret;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -226,8 +223,7 @@ static int x509_get_crl_entry_ext(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_entries(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_crl_entry *entry)
|
||||
{
|
||||
mbedtls_x509_crl_entry *entry) {
|
||||
int ret;
|
||||
size_t entry_len;
|
||||
mbedtls_x509_crl_entry *cur_entry = entry;
|
||||
|
@ -287,8 +283,7 @@ static int x509_get_entries(unsigned char **p,
|
|||
* Parse one CRLs in DER format and append it to the chained list
|
||||
*/
|
||||
int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
||||
const unsigned char *buf, size_t buflen)
|
||||
{
|
||||
const unsigned char *buf, size_t buflen) {
|
||||
int ret;
|
||||
size_t len;
|
||||
unsigned char *p = NULL, *end = NULL;
|
||||
|
@ -379,7 +374,7 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
|||
* signature AlgorithmIdentifier
|
||||
*/
|
||||
if ((ret = x509_crl_get_version(&p, end, &crl->version)) != 0 ||
|
||||
(ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
|
||||
(ret = mbedtls_x509_get_alg(&p, end, &crl->sig_oid, &sig_params1)) != 0) {
|
||||
mbedtls_x509_crl_free(crl);
|
||||
return (ret);
|
||||
}
|
||||
|
@ -428,8 +423,8 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
|||
if ((ret = mbedtls_x509_get_time(&p, end, &crl->next_update)) != 0) {
|
||||
if (ret != (MBEDTLS_ERR_X509_INVALID_DATE +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) &&
|
||||
ret != (MBEDTLS_ERR_X509_INVALID_DATE +
|
||||
MBEDTLS_ERR_ASN1_OUT_OF_DATA)) {
|
||||
ret != (MBEDTLS_ERR_X509_INVALID_DATE +
|
||||
MBEDTLS_ERR_ASN1_OUT_OF_DATA)) {
|
||||
mbedtls_x509_crl_free(crl);
|
||||
return (ret);
|
||||
}
|
||||
|
@ -479,10 +474,10 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
|||
}
|
||||
|
||||
if (crl->sig_oid.len != sig_oid2.len ||
|
||||
memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
|
||||
sig_params1.len != sig_params2.len ||
|
||||
(sig_params1.len != 0 &&
|
||||
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
|
||||
memcmp(crl->sig_oid.p, sig_oid2.p, crl->sig_oid.len) != 0 ||
|
||||
sig_params1.len != sig_params2.len ||
|
||||
(sig_params1.len != 0 &&
|
||||
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
|
||||
mbedtls_x509_crl_free(crl);
|
||||
return (MBEDTLS_ERR_X509_SIG_MISMATCH);
|
||||
}
|
||||
|
@ -504,8 +499,7 @@ int mbedtls_x509_crl_parse_der(mbedtls_x509_crl *chain,
|
|||
/*
|
||||
* Parse one or more CRLs and add them to the chained list
|
||||
*/
|
||||
int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen)
|
||||
{
|
||||
int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen) {
|
||||
#if defined(MBEDTLS_PEM_PARSE_C)
|
||||
int ret;
|
||||
size_t use_len;
|
||||
|
@ -564,8 +558,7 @@ int mbedtls_x509_crl_parse(mbedtls_x509_crl *chain, const unsigned char *buf, si
|
|||
/*
|
||||
* Load one or more CRLs and add them to the chained list
|
||||
*/
|
||||
int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
|
||||
{
|
||||
int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path) {
|
||||
int ret;
|
||||
size_t n;
|
||||
unsigned char *buf;
|
||||
|
@ -591,8 +584,7 @@ int mbedtls_x509_crl_parse_file(mbedtls_x509_crl *chain, const char *path)
|
|||
* Return an informational string about the CRL.
|
||||
*/
|
||||
int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
|
||||
const mbedtls_x509_crl *crl)
|
||||
{
|
||||
const mbedtls_x509_crl *crl) {
|
||||
int ret;
|
||||
size_t n;
|
||||
char *p;
|
||||
|
@ -664,16 +656,14 @@ int mbedtls_x509_crl_info(char *buf, size_t size, const char *prefix,
|
|||
/*
|
||||
* Initialize a CRL chain
|
||||
*/
|
||||
void mbedtls_x509_crl_init(mbedtls_x509_crl *crl)
|
||||
{
|
||||
void mbedtls_x509_crl_init(mbedtls_x509_crl *crl) {
|
||||
memset(crl, 0, sizeof(mbedtls_x509_crl));
|
||||
}
|
||||
|
||||
/*
|
||||
* Unallocate all CRL data
|
||||
*/
|
||||
void mbedtls_x509_crl_free(mbedtls_x509_crl *crl)
|
||||
{
|
||||
void mbedtls_x509_crl_free(mbedtls_x509_crl *crl) {
|
||||
mbedtls_x509_crl *crl_cur = crl;
|
||||
mbedtls_x509_crl *crl_prv;
|
||||
mbedtls_x509_name *name_cur;
|
||||
|
|
|
@ -160,8 +160,7 @@ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = {
|
|||
* Return 0 if md_alg is acceptable for this profile, -1 otherwise
|
||||
*/
|
||||
static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
|
||||
mbedtls_md_type_t md_alg)
|
||||
{
|
||||
mbedtls_md_type_t md_alg) {
|
||||
if (md_alg == MBEDTLS_MD_NONE)
|
||||
return (-1);
|
||||
|
||||
|
@ -176,8 +175,7 @@ static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
|
|||
* Return 0 if pk_alg is acceptable for this profile, -1 otherwise
|
||||
*/
|
||||
static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
|
||||
mbedtls_pk_type_t pk_alg)
|
||||
{
|
||||
mbedtls_pk_type_t pk_alg) {
|
||||
if (pk_alg == MBEDTLS_PK_NONE)
|
||||
return (-1);
|
||||
|
||||
|
@ -192,8 +190,7 @@ static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
|
|||
* Return 0 if pk is acceptable for this profile, -1 otherwise
|
||||
*/
|
||||
static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
|
||||
const mbedtls_pk_context *pk)
|
||||
{
|
||||
const mbedtls_pk_context *pk) {
|
||||
const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
|
@ -207,8 +204,8 @@ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
|
|||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if (pk_alg == MBEDTLS_PK_ECDSA ||
|
||||
pk_alg == MBEDTLS_PK_ECKEY ||
|
||||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
|
||||
pk_alg == MBEDTLS_PK_ECKEY ||
|
||||
pk_alg == MBEDTLS_PK_ECKEY_DH) {
|
||||
const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
|
||||
|
||||
if (gid == MBEDTLS_ECP_DP_NONE)
|
||||
|
@ -229,8 +226,7 @@ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
|
|||
*/
|
||||
static int x509_get_version(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *ver)
|
||||
{
|
||||
int *ver) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -264,8 +260,7 @@ static int x509_get_version(unsigned char **p,
|
|||
static int x509_get_dates(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_time *from,
|
||||
mbedtls_x509_time *to)
|
||||
{
|
||||
mbedtls_x509_time *to) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -293,8 +288,7 @@ static int x509_get_dates(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_uid(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_buf *uid, int n)
|
||||
{
|
||||
mbedtls_x509_buf *uid, int n) {
|
||||
int ret;
|
||||
|
||||
if (*p == end)
|
||||
|
@ -319,8 +313,7 @@ static int x509_get_uid(unsigned char **p,
|
|||
static int x509_get_basic_constraints(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
int *ca_istrue,
|
||||
int *max_pathlen)
|
||||
{
|
||||
int *max_pathlen) {
|
||||
int ret;
|
||||
size_t len;
|
||||
|
||||
|
@ -367,8 +360,7 @@ static int x509_get_basic_constraints(unsigned char **p,
|
|||
|
||||
static int x509_get_ns_cert_type(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
unsigned char *ns_cert_type)
|
||||
{
|
||||
unsigned char *ns_cert_type) {
|
||||
int ret;
|
||||
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
||||
|
||||
|
@ -386,8 +378,7 @@ static int x509_get_ns_cert_type(unsigned char **p,
|
|||
|
||||
static int x509_get_key_usage(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
unsigned int *key_usage)
|
||||
{
|
||||
unsigned int *key_usage) {
|
||||
int ret;
|
||||
size_t i;
|
||||
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
||||
|
@ -415,8 +406,7 @@ static int x509_get_key_usage(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_ext_key_usage(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_sequence *ext_key_usage)
|
||||
{
|
||||
mbedtls_x509_sequence *ext_key_usage) {
|
||||
int ret;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0)
|
||||
|
@ -458,8 +448,7 @@ static int x509_get_ext_key_usage(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_subject_alt_name(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_sequence *subject_alt_name)
|
||||
{
|
||||
mbedtls_x509_sequence *subject_alt_name) {
|
||||
int ret;
|
||||
size_t len, tag_len;
|
||||
mbedtls_asn1_buf *buf;
|
||||
|
@ -486,7 +475,7 @@ static int x509_get_subject_alt_name(unsigned char **p,
|
|||
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
|
||||
|
||||
if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
|
||||
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
||||
}
|
||||
|
@ -534,8 +523,7 @@ static int x509_get_subject_alt_name(unsigned char **p,
|
|||
*/
|
||||
static int x509_get_crt_ext(unsigned char **p,
|
||||
const unsigned char *end,
|
||||
mbedtls_x509_crt *crt)
|
||||
{
|
||||
mbedtls_x509_crt *crt) {
|
||||
int ret;
|
||||
size_t len;
|
||||
unsigned char *end_ext_data, *end_ext_octet;
|
||||
|
@ -575,7 +563,7 @@ static int x509_get_crt_ext(unsigned char **p,
|
|||
|
||||
/* Get optional critical */
|
||||
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
|
||||
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
|
||||
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
|
||||
return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
|
||||
|
||||
/* Data should be octet string type */
|
||||
|
@ -666,8 +654,7 @@ static int x509_get_crt_ext(unsigned char **p,
|
|||
* Parse and fill a single X.509 certificate in DER format
|
||||
*/
|
||||
static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *buf,
|
||||
size_t buflen)
|
||||
{
|
||||
size_t buflen) {
|
||||
int ret;
|
||||
size_t len;
|
||||
unsigned char *p, *end, *crt_end;
|
||||
|
@ -741,9 +728,9 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
|
|||
* signature AlgorithmIdentifier
|
||||
*/
|
||||
if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
|
||||
(ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
|
||||
(ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
|
||||
&sig_params1)) != 0) {
|
||||
(ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
|
||||
(ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
|
||||
&sig_params1)) != 0) {
|
||||
mbedtls_x509_crt_free(crt);
|
||||
return (ret);
|
||||
}
|
||||
|
@ -874,10 +861,10 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
|
|||
}
|
||||
|
||||
if (crt->sig_oid.len != sig_oid2.len ||
|
||||
memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
|
||||
sig_params1.len != sig_params2.len ||
|
||||
(sig_params1.len != 0 &&
|
||||
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
|
||||
memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
|
||||
sig_params1.len != sig_params2.len ||
|
||||
(sig_params1.len != 0 &&
|
||||
memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
|
||||
mbedtls_x509_crt_free(crt);
|
||||
return (MBEDTLS_ERR_X509_SIG_MISMATCH);
|
||||
}
|
||||
|
@ -901,8 +888,7 @@ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt, const unsigned char *b
|
|||
* chained list
|
||||
*/
|
||||
int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, const unsigned char *buf,
|
||||
size_t buflen)
|
||||
{
|
||||
size_t buflen) {
|
||||
int ret;
|
||||
mbedtls_x509_crt *crt = chain, *prev = NULL;
|
||||
|
||||
|
@ -948,8 +934,7 @@ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain, const unsigned char *buf
|
|||
* Parse one or more PEM certificates from a buffer and add them to the chained
|
||||
* list
|
||||
*/
|
||||
int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen)
|
||||
{
|
||||
int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen) {
|
||||
#if defined(MBEDTLS_PEM_PARSE_C)
|
||||
int success = 0, first_error = 0, total_failed = 0;
|
||||
int buf_format = MBEDTLS_X509_FORMAT_DER;
|
||||
|
@ -967,7 +952,7 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, si
|
|||
*/
|
||||
#if defined(MBEDTLS_PEM_PARSE_C)
|
||||
if (buflen != 0 && buf[buflen - 1] == '\0' &&
|
||||
strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
|
||||
strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
|
||||
buf_format = MBEDTLS_X509_FORMAT_PEM;
|
||||
}
|
||||
|
||||
|
@ -1053,8 +1038,7 @@ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain, const unsigned char *buf, si
|
|||
/*
|
||||
* Load one or more certificates and add them to the chained list
|
||||
*/
|
||||
int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
|
||||
{
|
||||
int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path) {
|
||||
int ret;
|
||||
size_t n;
|
||||
unsigned char *buf;
|
||||
|
@ -1070,8 +1054,7 @@ int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
|
|||
return (ret);
|
||||
}
|
||||
|
||||
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
|
||||
{
|
||||
int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) {
|
||||
int ret = 0;
|
||||
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
||||
int w_ret;
|
||||
|
@ -1187,8 +1170,7 @@ cleanup:
|
|||
#endif /* MBEDTLS_FS_IO */
|
||||
|
||||
static int x509_info_subject_alt_name(char **buf, size_t *size,
|
||||
const mbedtls_x509_sequence *subject_alt_name)
|
||||
{
|
||||
const mbedtls_x509_sequence *subject_alt_name) {
|
||||
size_t i;
|
||||
size_t n = *size;
|
||||
char *p = *buf;
|
||||
|
@ -1234,8 +1216,7 @@ static int x509_info_subject_alt_name(char **buf, size_t *size,
|
|||
PRINT_ITEM( name );
|
||||
|
||||
static int x509_info_cert_type(char **buf, size_t *size,
|
||||
unsigned char ns_cert_type)
|
||||
{
|
||||
unsigned char ns_cert_type) {
|
||||
int ret;
|
||||
size_t n = *size;
|
||||
char *p = *buf;
|
||||
|
@ -1261,8 +1242,7 @@ static int x509_info_cert_type(char **buf, size_t *size,
|
|||
PRINT_ITEM( name );
|
||||
|
||||
static int x509_info_key_usage(char **buf, size_t *size,
|
||||
unsigned int key_usage)
|
||||
{
|
||||
unsigned int key_usage) {
|
||||
int ret;
|
||||
size_t n = *size;
|
||||
char *p = *buf;
|
||||
|
@ -1285,8 +1265,7 @@ static int x509_info_key_usage(char **buf, size_t *size,
|
|||
}
|
||||
|
||||
static int x509_info_ext_key_usage(char **buf, size_t *size,
|
||||
const mbedtls_x509_sequence *extended_key_usage)
|
||||
{
|
||||
const mbedtls_x509_sequence *extended_key_usage) {
|
||||
int ret;
|
||||
const char *desc;
|
||||
size_t n = *size;
|
||||
|
@ -1318,8 +1297,7 @@ static int x509_info_ext_key_usage(char **buf, size_t *size,
|
|||
#define BEFORE_COLON 18
|
||||
#define BC "18"
|
||||
int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
|
||||
const mbedtls_x509_crt *crt)
|
||||
{
|
||||
const mbedtls_x509_crt *crt) {
|
||||
int ret;
|
||||
size_t n;
|
||||
char *p;
|
||||
|
@ -1471,8 +1449,7 @@ static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
|
|||
};
|
||||
|
||||
int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
|
||||
uint32_t flags)
|
||||
{
|
||||
uint32_t flags) {
|
||||
int ret;
|
||||
const struct x509_crt_verify_string *cur;
|
||||
char *p = buf;
|
||||
|
@ -1498,8 +1475,7 @@ int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
|
|||
|
||||
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
||||
int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
|
||||
unsigned int usage)
|
||||
{
|
||||
unsigned int usage) {
|
||||
unsigned int usage_must, usage_may;
|
||||
unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
|
||||
| MBEDTLS_X509_KU_DECIPHER_ONLY;
|
||||
|
@ -1524,8 +1500,7 @@ int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
|
|||
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
|
||||
int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
|
||||
const char *usage_oid,
|
||||
size_t usage_len)
|
||||
{
|
||||
size_t usage_len) {
|
||||
const mbedtls_x509_sequence *cur;
|
||||
|
||||
/* Extension is not mandatory, absent means no restriction */
|
||||
|
@ -1539,7 +1514,7 @@ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
|
|||
const mbedtls_x509_buf *cur_oid = &cur->buf;
|
||||
|
||||
if (cur_oid->len == usage_len &&
|
||||
memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
|
||||
memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -1555,13 +1530,12 @@ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
|
|||
/*
|
||||
* Return 1 if the certificate is revoked, or 0 otherwise.
|
||||
*/
|
||||
int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
|
||||
{
|
||||
int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl) {
|
||||
const mbedtls_x509_crl_entry *cur = &crl->entry;
|
||||
|
||||
while (cur != NULL && cur->serial.len != 0) {
|
||||
if (crt->serial.len == cur->serial.len &&
|
||||
memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
|
||||
memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
|
||||
if (mbedtls_x509_time_is_past(&cur->revocation_date))
|
||||
return (1);
|
||||
}
|
||||
|
@ -1578,8 +1552,7 @@ int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_
|
|||
*/
|
||||
static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
||||
mbedtls_x509_crl *crl_list,
|
||||
const mbedtls_x509_crt_profile *profile)
|
||||
{
|
||||
const mbedtls_x509_crt_profile *profile) {
|
||||
int flags = 0;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
const mbedtls_md_info_t *md_info;
|
||||
|
@ -1589,9 +1562,9 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
|||
|
||||
while (crl_list != NULL) {
|
||||
if (crl_list->version == 0 ||
|
||||
crl_list->issuer_raw.len != ca->subject_raw.len ||
|
||||
memcmp(crl_list->issuer_raw.p, ca->subject_raw.p,
|
||||
crl_list->issuer_raw.len) != 0) {
|
||||
crl_list->issuer_raw.len != ca->subject_raw.len ||
|
||||
memcmp(crl_list->issuer_raw.p, ca->subject_raw.p,
|
||||
crl_list->issuer_raw.len) != 0) {
|
||||
crl_list = crl_list->next;
|
||||
continue;
|
||||
}
|
||||
|
@ -1659,8 +1632,7 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
|||
/*
|
||||
* Like memcmp, but case-insensitive and always returns -1 if different
|
||||
*/
|
||||
static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
|
||||
{
|
||||
static int x509_memcasecmp(const void *s1, const void *s2, size_t len) {
|
||||
size_t i;
|
||||
unsigned char diff;
|
||||
const unsigned char *n1 = s1, *n2 = s2;
|
||||
|
@ -1672,8 +1644,8 @@ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
|
|||
continue;
|
||||
|
||||
if (diff == 32 &&
|
||||
((n1[i] >= 'a' && n1[i] <= 'z') ||
|
||||
(n1[i] >= 'A' && n1[i] <= 'Z'))) {
|
||||
((n1[i] >= 'a' && n1[i] <= 'z') ||
|
||||
(n1[i] >= 'A' && n1[i] <= 'Z'))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1686,8 +1658,7 @@ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
|
|||
/*
|
||||
* Return 0 if name matches wildcard, -1 otherwise
|
||||
*/
|
||||
static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
|
||||
{
|
||||
static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name) {
|
||||
size_t i;
|
||||
size_t cn_idx = 0, cn_len = strlen(cn);
|
||||
|
||||
|
@ -1706,7 +1677,7 @@ static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
|
|||
return (-1);
|
||||
|
||||
if (cn_len - cn_idx == name->len - 1 &&
|
||||
x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
|
||||
x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -1719,18 +1690,17 @@ static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
|
|||
*
|
||||
* Return 0 if equal, -1 otherwise.
|
||||
*/
|
||||
static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
|
||||
{
|
||||
static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b) {
|
||||
if (a->tag == b->tag &&
|
||||
a->len == b->len &&
|
||||
memcmp(a->p, b->p, b->len) == 0) {
|
||||
a->len == b->len &&
|
||||
memcmp(a->p, b->p, b->len) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
|
||||
(b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
|
||||
a->len == b->len &&
|
||||
x509_memcasecmp(a->p, b->p, b->len) == 0) {
|
||||
(b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
|
||||
a->len == b->len &&
|
||||
x509_memcasecmp(a->p, b->p, b->len) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -1747,8 +1717,7 @@ static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
|
|||
*
|
||||
* Return 0 if equal, -1 otherwise.
|
||||
*/
|
||||
static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
|
||||
{
|
||||
static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b) {
|
||||
/* Avoid recursion, it might not be optimised by the compiler */
|
||||
while (a != NULL || b != NULL) {
|
||||
if (a == NULL || b == NULL)
|
||||
|
@ -1756,8 +1725,8 @@ static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
|
|||
|
||||
/* type */
|
||||
if (a->oid.tag != b->oid.tag ||
|
||||
a->oid.len != b->oid.len ||
|
||||
memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
|
||||
a->oid.len != b->oid.len ||
|
||||
memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
|
@ -1781,8 +1750,7 @@ static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
|
|||
* Check the signature of a certificate by its parent
|
||||
*/
|
||||
static int x509_crt_check_signature(const mbedtls_x509_crt *child,
|
||||
mbedtls_x509_crt *parent)
|
||||
{
|
||||
mbedtls_x509_crt *parent) {
|
||||
const mbedtls_md_info_t *md_info;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
|
||||
|
@ -1809,8 +1777,7 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
|
|||
*/
|
||||
static int x509_crt_check_parent(const mbedtls_x509_crt *child,
|
||||
const mbedtls_x509_crt *parent,
|
||||
int top)
|
||||
{
|
||||
int top) {
|
||||
int need_ca_bit;
|
||||
|
||||
/* Parent must be the issuer */
|
||||
|
@ -1829,7 +1796,7 @@ static int x509_crt_check_parent(const mbedtls_x509_crt *child,
|
|||
|
||||
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
||||
if (need_ca_bit &&
|
||||
mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
|
||||
mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
|
||||
return (-1);
|
||||
}
|
||||
#endif
|
||||
|
@ -1880,8 +1847,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
|
|||
mbedtls_x509_crt *candidates,
|
||||
int top,
|
||||
size_t path_cnt,
|
||||
size_t self_cnt)
|
||||
{
|
||||
size_t self_cnt) {
|
||||
mbedtls_x509_crt *parent, *badtime_parent = NULL;
|
||||
|
||||
for (parent = candidates; parent != NULL; parent = parent->next) {
|
||||
|
@ -1891,7 +1857,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
|
|||
|
||||
/* +1 because stored max_pathlen is 1 higher that the actual value */
|
||||
if (parent->max_pathlen > 0 &&
|
||||
(size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
|
||||
(size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1902,7 +1868,7 @@ static mbedtls_x509_crt *x509_crt_find_parent_in(mbedtls_x509_crt *child,
|
|||
|
||||
/* optional time check */
|
||||
if (mbedtls_x509_time_is_past(&parent->valid_to) ||
|
||||
mbedtls_x509_time_is_future(&parent->valid_from)) {
|
||||
mbedtls_x509_time_is_future(&parent->valid_from)) {
|
||||
if (badtime_parent == NULL)
|
||||
badtime_parent = parent;
|
||||
|
||||
|
@ -1941,8 +1907,7 @@ static mbedtls_x509_crt *x509_crt_find_parent(mbedtls_x509_crt *child,
|
|||
mbedtls_x509_crt *trust_ca,
|
||||
int *parent_is_trusted,
|
||||
size_t path_cnt,
|
||||
size_t self_cnt)
|
||||
{
|
||||
size_t self_cnt) {
|
||||
mbedtls_x509_crt *parent;
|
||||
|
||||
/* Look for a parent in trusted CAs */
|
||||
|
@ -1965,8 +1930,7 @@ static mbedtls_x509_crt *x509_crt_find_parent(mbedtls_x509_crt *child,
|
|||
*/
|
||||
static int x509_crt_check_ee_locally_trusted(
|
||||
mbedtls_x509_crt *crt,
|
||||
mbedtls_x509_crt *trust_ca)
|
||||
{
|
||||
mbedtls_x509_crt *trust_ca) {
|
||||
mbedtls_x509_crt *cur;
|
||||
|
||||
/* must be self-issued */
|
||||
|
@ -1976,7 +1940,7 @@ static int x509_crt_check_ee_locally_trusted(
|
|||
/* look for an exact match with trusted cert */
|
||||
for (cur = trust_ca; cur != NULL; cur = cur->next) {
|
||||
if (crt->raw.len == cur->raw.len &&
|
||||
memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
|
||||
memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
@ -2018,8 +1982,7 @@ static int x509_crt_verify_chain(
|
|||
mbedtls_x509_crl *ca_crl,
|
||||
const mbedtls_x509_crt_profile *profile,
|
||||
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
|
||||
size_t *chain_len)
|
||||
{
|
||||
size_t *chain_len) {
|
||||
uint32_t *flags;
|
||||
mbedtls_x509_crt *child;
|
||||
mbedtls_x509_crt *parent;
|
||||
|
@ -2056,7 +2019,7 @@ static int x509_crt_verify_chain(
|
|||
|
||||
/* Special case: EE certs that are locally trusted */
|
||||
if (*chain_len == 1 &&
|
||||
x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
|
||||
x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -2074,14 +2037,14 @@ static int x509_crt_verify_chain(
|
|||
* These can occur with some strategies for key rollover, see [SIRO],
|
||||
* and should be excluded from max_pathlen checks. */
|
||||
if (*chain_len != 1 &&
|
||||
x509_name_cmp(&child->issuer, &child->subject) == 0) {
|
||||
x509_name_cmp(&child->issuer, &child->subject) == 0) {
|
||||
self_cnt++;
|
||||
}
|
||||
|
||||
/* path_cnt is 0 for the first intermediate CA,
|
||||
* and if parent is trusted it's not an intermediate CA */
|
||||
if (! parent_is_trusted &&
|
||||
*chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
|
||||
*chain_len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
|
||||
/* return immediately to avoid overflow the chain array */
|
||||
return (MBEDTLS_ERR_X509_FATAL_ERROR);
|
||||
}
|
||||
|
@ -2112,11 +2075,10 @@ static int x509_crt_verify_chain(
|
|||
* Check for CN match
|
||||
*/
|
||||
static int x509_crt_check_cn(const mbedtls_x509_buf *name,
|
||||
const char *cn, size_t cn_len)
|
||||
{
|
||||
const char *cn, size_t cn_len) {
|
||||
/* try exact match */
|
||||
if (name->len == cn_len &&
|
||||
x509_memcasecmp(cn, name->p, cn_len) == 0) {
|
||||
x509_memcasecmp(cn, name->p, cn_len) == 0) {
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -2133,8 +2095,7 @@ static int x509_crt_check_cn(const mbedtls_x509_buf *name,
|
|||
*/
|
||||
static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
|
||||
const char *cn,
|
||||
uint32_t *flags)
|
||||
{
|
||||
uint32_t *flags) {
|
||||
const mbedtls_x509_name *name;
|
||||
const mbedtls_x509_sequence *cur;
|
||||
size_t cn_len = strlen(cn);
|
||||
|
@ -2150,7 +2111,7 @@ static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
|
|||
} else {
|
||||
for (name = &crt->subject; name != NULL; name = name->next) {
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
|
||||
x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
|
||||
x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -2168,8 +2129,7 @@ static int x509_crt_merge_flags_with_cb(
|
|||
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE],
|
||||
size_t chain_len,
|
||||
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
||||
void *p_vrfy)
|
||||
{
|
||||
void *p_vrfy) {
|
||||
int ret;
|
||||
size_t i;
|
||||
uint32_t cur_flags;
|
||||
|
@ -2195,8 +2155,7 @@ int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
|
|||
mbedtls_x509_crl *ca_crl,
|
||||
const char *cn, uint32_t *flags,
|
||||
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
||||
void *p_vrfy)
|
||||
{
|
||||
void *p_vrfy) {
|
||||
return (mbedtls_x509_crt_verify_with_profile(crt, trust_ca, ca_crl,
|
||||
&mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy));
|
||||
}
|
||||
|
@ -2217,8 +2176,7 @@ int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
|
|||
const mbedtls_x509_crt_profile *profile,
|
||||
const char *cn, uint32_t *flags,
|
||||
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
||||
void *p_vrfy)
|
||||
{
|
||||
void *p_vrfy) {
|
||||
int ret;
|
||||
mbedtls_pk_type_t pk_type;
|
||||
x509_crt_verify_chain_item ver_chain[X509_MAX_VERIFY_CHAIN_SIZE];
|
||||
|
@ -2278,16 +2236,14 @@ exit:
|
|||
/*
|
||||
* Initialize a certificate chain
|
||||
*/
|
||||
void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
|
||||
{
|
||||
void mbedtls_x509_crt_init(mbedtls_x509_crt *crt) {
|
||||
memset(crt, 0, sizeof(mbedtls_x509_crt));
|
||||
}
|
||||
|
||||
/*
|
||||
* Unallocate all certificate data
|
||||
*/
|
||||
void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
|
||||
{
|
||||
void mbedtls_x509_crt_free(mbedtls_x509_crt *crt) {
|
||||
mbedtls_x509_crt *cert_cur = crt;
|
||||
mbedtls_x509_crt *cert_prv;
|
||||
mbedtls_x509_name *name_cur;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue