Skip to content

Commit 1e8f18c

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
Update mbedTLS to 2.16.3-cesanta4
Changes necessary for Apple HomeKit * Enabled Curve25519 * Cherry-picked `mbedtls_ecp_read_key()` from 2.18 CL: mbedTLS 2.16.3-cesanta4: Enable Curve25519, add mbedtls_ecp_read_key() PUBLISHED_FROM=75464cbd459d50f0739fd6c53a8219599ccc2dec
1 parent 2416c72 commit 1e8f18c

File tree

10 files changed

+137
-10
lines changed

10 files changed

+137
-10
lines changed

mbedtls/include/mbedtls/bignum.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,24 @@ int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
495495
size_t buflen );
496496

497497
/**
498-
* \brief Export an MPI into unsigned big endian binary data
499-
* of fixed size.
498+
* \brief Import X from unsigned binary data, little endian
499+
*
500+
* \param X The destination MPI. This must point to an initialized MPI.
501+
* \param buf The input buffer. This must be a readable buffer of length
502+
* \p buflen Bytes.
503+
* \param buflen The length of the input buffer \p p in Bytes.
504+
*
505+
* \return \c 0 if successful.
506+
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
507+
* \return Another negative error code on different kinds of failure.
508+
*/
509+
int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
510+
const unsigned char *buf, size_t buflen );
511+
512+
/**
513+
* \brief Export X into unsigned binary data, big endian.
514+
* Always fills the whole buffer, which will start with zeros
515+
* if the number is smaller.
500516
*
501517
* \param X The source MPI. This must point to an initialized MPI.
502518
* \param buf The output buffer. This must be a writable buffer of length

mbedtls/include/mbedtls/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@
762762
//#define MBEDTLS_ECP_DP_BP256R1_ENABLED
763763
//#define MBEDTLS_ECP_DP_BP384R1_ENABLED
764764
//#define MBEDTLS_ECP_DP_BP512R1_ENABLED
765-
//#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
765+
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
766766
//#define MBEDTLS_ECP_DP_CURVE448_ENABLED
767767

768768
/**

mbedtls/include/mbedtls/ecp.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,22 @@ int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
10941094
int (*f_rng)(void *, unsigned char *, size_t),
10951095
void *p_rng );
10961096

1097+
/**
1098+
* \brief This function reads an ECP key.
1099+
*
1100+
* \param grp_id The ECP group identifier.
1101+
* \param key The destination key.
1102+
* \param buf The the buffer containing the binary representation of the
1103+
* key. (Big endian integer for Weierstrass curves, byte
1104+
* string for Montgomery curves.)
1105+
* \param buflen The length of the buffer in bytes.
1106+
*
1107+
* \return \c 0 on success.
1108+
* \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1109+
* on failure.
1110+
*/
1111+
int mbedtls_ecp_read_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1112+
const unsigned char *buf, size_t buflen );
10971113
/**
10981114
* \brief This function checks that the keypair objects
10991115
* \p pub and \p prv have the same group and the

mbedtls/include/mbedtls/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
* Major version | Minor version | Patch version
4949
*/
5050
#define MBEDTLS_VERSION_NUMBER 0x02100300
51-
#define MBEDTLS_VERSION_STRING "2.16.3-cesanta3"
52-
#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.3-cesanta3"
51+
#define MBEDTLS_VERSION_STRING "2.16.3-cesanta4"
52+
#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.16.3-cesanta4"
5353

5454
#if defined(MBEDTLS_VERSION_C)
5555

mbedtls/tests/suites/test_suite_ecdh.function

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ static int load_private_key( int grp_id, data_t *private_key,
2222
rnd_pseudo_info *rnd_info )
2323
{
2424
int ok = 0;
25-
TEST_ASSERT( mbedtls_ecp_group_load( &ecp->grp, grp_id ) == 0 );
26-
TEST_ASSERT( mbedtls_mpi_read_binary( &ecp->d,
27-
private_key->x,
28-
private_key->len ) == 0 );
25+
TEST_ASSERT( mbedtls_ecp_read_key( grp_id, ecp,
26+
private_key->x,
27+
private_key->len ) == 0 );
2928
TEST_ASSERT( mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) == 0 );
3029
/* Calculate the public key from the private key. */
3130
TEST_ASSERT( mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d,

mbedtls/tests/suites/test_suite_ecp.data

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,58 @@ ECP gen keypair wrapper
244244
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
245245
mbedtls_ecp_gen_key:MBEDTLS_ECP_DP_SECP192R1
246246

247+
ECP read key #1 (short weierstrass, too small)
248+
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
249+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_SECP192R1:"00":MBEDTLS_ERR_ECP_INVALID_KEY
250+
251+
ECP read key #2 (short weierstrass, smallest)
252+
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
253+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_SECP192R1:"01":0
254+
255+
ECP read key #3 (short weierstrass, biggest)
256+
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
257+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_SECP192R1:"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830":0
258+
259+
ECP read key #4 (short weierstrass, too big)
260+
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
261+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_SECP192R1:"FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831":MBEDTLS_ERR_ECP_INVALID_KEY
262+
263+
ECP read key #5 (montgomery, too big)
264+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
265+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"000000000000000000000000000000000000000000000000000000000000000C":0
266+
267+
ECP read key #6 (montgomery, not big enough)
268+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
269+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3":0
270+
271+
ECP read key #7 (montgomery, msb OK)
272+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
273+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"0000000000000000000000000000000000000000000000000000000000000004":0
274+
275+
ECP read key #8 (montgomery, bit 0 set)
276+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
277+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"1000000000000000000000000000000000000000000000000000000000000000":0
278+
279+
ECP read key #9 (montgomery, bit 1 set)
280+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
281+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"2000000000000000000000000000000000000000000000000000000000000004":0
282+
283+
ECP read key #10 (montgomery, bit 2 set)
284+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
285+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"4000000000000000000000000000000000000000000000000000000000000004":0
286+
287+
ECP read key #11 (montgomery, OK)
288+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
289+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7":0
290+
291+
ECP read key #12 (montgomery, too long)
292+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
293+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"00000000000000000000000000000000000000000000000000000000000000000C":MBEDTLS_ERR_ECP_INVALID_KEY
294+
295+
ECP read key #13 (montgomery, not long enough)
296+
depends_on:MBEDTLS_ECP_DP_CURVE25519_ENABLED
297+
mbedtls_ecp_read_key:MBEDTLS_ECP_DP_CURVE25519:"0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3":MBEDTLS_ERR_ECP_INVALID_KEY
298+
247299
ECP mod p192 small (more than 192 bits, less limbs than 2 * 192 bits)
248300
depends_on:MBEDTLS_ECP_DP_SECP192R1_ENABLED
249301
ecp_fast_mod:MBEDTLS_ECP_DP_SECP192R1:"0100000000000103010000000000010201000000000001010100000000000100"

mbedtls/tests/suites/test_suite_ecp.function

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,28 @@ exit:
10081008
}
10091009
/* END_CASE */
10101010

1011+
/* BEGIN_CASE */
1012+
void mbedtls_ecp_read_key( int grp_id, data_t* in_key, int expected )
1013+
{
1014+
int ret = 0;
1015+
mbedtls_ecp_keypair key;
1016+
1017+
mbedtls_ecp_keypair_init( &key );
1018+
1019+
ret = mbedtls_ecp_read_key( grp_id, &key, in_key->x, in_key->len );
1020+
TEST_ASSERT( ret == expected );
1021+
1022+
if( expected == 0 )
1023+
{
1024+
ret = mbedtls_ecp_check_privkey( &key.grp, &key.d );
1025+
TEST_ASSERT( ret == 0 );
1026+
}
1027+
1028+
exit:
1029+
mbedtls_ecp_keypair_free( &key );
1030+
}
1031+
/* END_CASE */
1032+
10111033
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
10121034
void ecp_selftest( )
10131035
{

mbedtls/tests/suites/test_suite_mpi.data

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ mpi_read_write_string:16:"-1":16:"":3:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
6161
Base test mbedtls_mpi_read_binary #1
6262
mbedtls_mpi_read_binary:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924"
6363

64+
Base test mbedtls_mpi_read_binary_le #1
65+
mbedtls_mpi_read_binary_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"219946662473865722255717126709915431768051735954189829340600976826409773245337023925691629251672268961177825243440202069039100741562168093042339401187848509859789949044607421190014088260008793380554914226244485299326152319899746569"
66+
6467
Base test mbedtls_mpi_write_binary #1
6568
mbedtls_mpi_write_binary:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":200:0
6669

mbedtls/tests/suites/test_suite_mpi.function

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ exit:
331331
}
332332
/* END_CASE */
333333

334+
/* BEGIN_CASE */
335+
void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
336+
{
337+
mbedtls_mpi X;
338+
unsigned char str[1000];
339+
size_t len;
340+
341+
mbedtls_mpi_init( &X );
342+
343+
344+
TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
345+
TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 );
346+
TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
347+
348+
exit:
349+
mbedtls_mpi_free( &X );
350+
}
351+
/* END_CASE */
352+
334353
/* BEGIN_CASE */
335354
void mbedtls_mpi_write_binary( int radix_X, char * input_X,
336355
data_t * input_A, int output_size,

mos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
author: mongoose-os
22
description: Implements SPI API on Mongoose OS
33
type: lib
4-
version: 2.16.3-cesanta3
4+
version: 2.16.3-cesanta4
55

66
sources:
77
- src

0 commit comments

Comments
 (0)