mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/pk.h"
markrad 0:cdf462088d13 3 #include "mbedtls/pem.h"
markrad 0:cdf462088d13 4 #include "mbedtls/oid.h"
markrad 0:cdf462088d13 5 /* END_HEADER */
markrad 0:cdf462088d13 6
markrad 0:cdf462088d13 7 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 8 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
markrad 0:cdf462088d13 9 * END_DEPENDENCIES
markrad 0:cdf462088d13 10 */
markrad 0:cdf462088d13 11
markrad 0:cdf462088d13 12 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
markrad 0:cdf462088d13 13 void pk_parse_keyfile_rsa( char *key_file, char *password, int result )
markrad 0:cdf462088d13 14 {
markrad 0:cdf462088d13 15 mbedtls_pk_context ctx;
markrad 0:cdf462088d13 16 int res;
markrad 0:cdf462088d13 17 char *pwd = password;
markrad 0:cdf462088d13 18
markrad 0:cdf462088d13 19 mbedtls_pk_init( &ctx );
markrad 0:cdf462088d13 20
markrad 0:cdf462088d13 21 if( strcmp( pwd, "NULL" ) == 0 )
markrad 0:cdf462088d13 22 pwd = NULL;
markrad 0:cdf462088d13 23
markrad 0:cdf462088d13 24 res = mbedtls_pk_parse_keyfile( &ctx, key_file, pwd );
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 TEST_ASSERT( res == result );
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 if( res == 0 )
markrad 0:cdf462088d13 29 {
markrad 0:cdf462088d13 30 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 31 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
markrad 0:cdf462088d13 32 rsa = mbedtls_pk_rsa( ctx );
markrad 0:cdf462088d13 33 TEST_ASSERT( mbedtls_rsa_check_privkey( rsa ) == 0 );
markrad 0:cdf462088d13 34 }
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 exit:
markrad 0:cdf462088d13 37 mbedtls_pk_free( &ctx );
markrad 0:cdf462088d13 38 }
markrad 0:cdf462088d13 39 /* END_CASE */
markrad 0:cdf462088d13 40
markrad 0:cdf462088d13 41 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
markrad 0:cdf462088d13 42 void pk_parse_public_keyfile_rsa( char *key_file, int result )
markrad 0:cdf462088d13 43 {
markrad 0:cdf462088d13 44 mbedtls_pk_context ctx;
markrad 0:cdf462088d13 45 int res;
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 mbedtls_pk_init( &ctx );
markrad 0:cdf462088d13 48
markrad 0:cdf462088d13 49 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
markrad 0:cdf462088d13 50
markrad 0:cdf462088d13 51 TEST_ASSERT( res == result );
markrad 0:cdf462088d13 52
markrad 0:cdf462088d13 53 if( res == 0 )
markrad 0:cdf462088d13 54 {
markrad 0:cdf462088d13 55 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 56 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_RSA ) );
markrad 0:cdf462088d13 57 rsa = mbedtls_pk_rsa( ctx );
markrad 0:cdf462088d13 58 TEST_ASSERT( mbedtls_rsa_check_pubkey( rsa ) == 0 );
markrad 0:cdf462088d13 59 }
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 exit:
markrad 0:cdf462088d13 62 mbedtls_pk_free( &ctx );
markrad 0:cdf462088d13 63 }
markrad 0:cdf462088d13 64 /* END_CASE */
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
markrad 0:cdf462088d13 67 void pk_parse_public_keyfile_ec( char *key_file, int result )
markrad 0:cdf462088d13 68 {
markrad 0:cdf462088d13 69 mbedtls_pk_context ctx;
markrad 0:cdf462088d13 70 int res;
markrad 0:cdf462088d13 71
markrad 0:cdf462088d13 72 mbedtls_pk_init( &ctx );
markrad 0:cdf462088d13 73
markrad 0:cdf462088d13 74 res = mbedtls_pk_parse_public_keyfile( &ctx, key_file );
markrad 0:cdf462088d13 75
markrad 0:cdf462088d13 76 TEST_ASSERT( res == result );
markrad 0:cdf462088d13 77
markrad 0:cdf462088d13 78 if( res == 0 )
markrad 0:cdf462088d13 79 {
markrad 0:cdf462088d13 80 mbedtls_ecp_keypair *eckey;
markrad 0:cdf462088d13 81 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
markrad 0:cdf462088d13 82 eckey = mbedtls_pk_ec( ctx );
markrad 0:cdf462088d13 83 TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 );
markrad 0:cdf462088d13 84 }
markrad 0:cdf462088d13 85
markrad 0:cdf462088d13 86 exit:
markrad 0:cdf462088d13 87 mbedtls_pk_free( &ctx );
markrad 0:cdf462088d13 88 }
markrad 0:cdf462088d13 89 /* END_CASE */
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
markrad 0:cdf462088d13 92 void pk_parse_keyfile_ec( char *key_file, char *password, int result )
markrad 0:cdf462088d13 93 {
markrad 0:cdf462088d13 94 mbedtls_pk_context ctx;
markrad 0:cdf462088d13 95 int res;
markrad 0:cdf462088d13 96
markrad 0:cdf462088d13 97 mbedtls_pk_init( &ctx );
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 res = mbedtls_pk_parse_keyfile( &ctx, key_file, password );
markrad 0:cdf462088d13 100
markrad 0:cdf462088d13 101 TEST_ASSERT( res == result );
markrad 0:cdf462088d13 102
markrad 0:cdf462088d13 103 if( res == 0 )
markrad 0:cdf462088d13 104 {
markrad 0:cdf462088d13 105 mbedtls_ecp_keypair *eckey;
markrad 0:cdf462088d13 106 TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) );
markrad 0:cdf462088d13 107 eckey = mbedtls_pk_ec( ctx );
markrad 0:cdf462088d13 108 TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 );
markrad 0:cdf462088d13 109 }
markrad 0:cdf462088d13 110
markrad 0:cdf462088d13 111 exit:
markrad 0:cdf462088d13 112 mbedtls_pk_free( &ctx );
markrad 0:cdf462088d13 113 }
markrad 0:cdf462088d13 114 /* END_CASE */
markrad 0:cdf462088d13 115
markrad 0:cdf462088d13 116 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
markrad 0:cdf462088d13 117 void pk_parse_key_rsa( char *key_data, char *result_str, int result )
markrad 0:cdf462088d13 118 {
markrad 0:cdf462088d13 119 mbedtls_pk_context pk;
markrad 0:cdf462088d13 120 unsigned char buf[2000];
markrad 0:cdf462088d13 121 unsigned char output[2000];
markrad 0:cdf462088d13 122 int data_len;
markrad 0:cdf462088d13 123 ((void) result_str);
markrad 0:cdf462088d13 124
markrad 0:cdf462088d13 125 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 126
markrad 0:cdf462088d13 127 memset( buf, 0, 2000 );
markrad 0:cdf462088d13 128 memset( output, 0, 2000 );
markrad 0:cdf462088d13 129
markrad 0:cdf462088d13 130 data_len = unhexify( buf, key_data );
markrad 0:cdf462088d13 131
markrad 0:cdf462088d13 132 TEST_ASSERT( mbedtls_pk_parse_key( &pk, buf, data_len, NULL, 0 ) == ( result ) );
markrad 0:cdf462088d13 133 if( ( result ) == 0 )
markrad 0:cdf462088d13 134 {
markrad 0:cdf462088d13 135 TEST_ASSERT( 1 );
markrad 0:cdf462088d13 136 }
markrad 0:cdf462088d13 137
markrad 0:cdf462088d13 138 exit:
markrad 0:cdf462088d13 139 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 140 }
markrad 0:cdf462088d13 141 /* END_CASE */