Skip to content

Commit ca6c3d4

Browse files
author
Andrzej Kurek
committed
Pass platform context to tests
Add platform context as a parameter for tests to enable the usage of it in cryptographic calls
1 parent 513283d commit ca6c3d4

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

authcrypt/authcrypt.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#include "mbedtls/debug.h"
2929
#endif
3030

31-
#include "mbedtls/platform.h"
32-
3331
#include <string.h>
3432

3533
const unsigned char Authcrypt::secret_key[16] = {
@@ -41,8 +39,9 @@ const char Authcrypt::message[] = "Some things are better left unread";
4139

4240
const char Authcrypt::metadata[] = "eg sequence number, routing info";
4341

44-
Authcrypt::Authcrypt()
42+
Authcrypt::Authcrypt(mbedtls_platform_context* platform_ctx)
4543
{
44+
_platform_ctx = platform_ctx;
4645
memset(ciphertext, 0, sizeof(ciphertext));
4746
memset(decrypted, 0, sizeof(decrypted));
4847

authcrypt/authcrypt.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mbedtls/cipher.h"
2424
#include "mbedtls/entropy.h"
2525
#include "mbedtls/ctr_drbg.h"
26+
#include "mbedtls/platform.h"
2627

2728
/**
2829
* This class implements the logic to demonstrate authenticated encryption using
@@ -34,7 +35,7 @@ class Authcrypt
3435
/**
3536
* Construct an Authcrypt instance
3637
*/
37-
Authcrypt();
38+
Authcrypt(mbedtls_platform_context* platform_ctx);
3839

3940
/**
4041
* Free any allocated resources
@@ -103,6 +104,11 @@ class Authcrypt
103104
* The block cipher configuration
104105
*/
105106
mbedtls_cipher_context_t cipher;
107+
108+
/**
109+
* The platform context
110+
*/
111+
mbedtls_platform_context* _platform_ctx;
106112
};
107113

108114
#endif /* _AUTHCRYPT_H_ */

authcrypt/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int main() {
3030
}
3131

3232
int exit_code = MBEDTLS_EXIT_SUCCESS;
33-
Authcrypt *authcrypt = new Authcrypt();
33+
Authcrypt *authcrypt = new Authcrypt(&platform_ctx);
3434

3535
if (authcrypt->run() != 0) {
3636
exit_code = MBEDTLS_EXIT_FAILURE;

benchmark/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ typedef struct {
300300
rsa, dhm, ecdsa, ecdh;
301301
} todo_list;
302302

303-
static int benchmark( int argc, char *argv[] )
303+
static int benchmark( int argc, char *argv[], mbedtls_platform_context* ctx )
304304
{
305305
int i;
306306
unsigned char tmp[200];
@@ -309,7 +309,9 @@ static int benchmark( int argc, char *argv[] )
309309
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
310310
unsigned char malloc_buf[HEAP_SIZE] = { 0 };
311311
#endif
312-
312+
// The call below is used to avoid the "unused parameter" warning.
313+
// The context itself can be used by cryptographic calls which require it.
314+
(void)ctx;
313315
if( argc <= 1 )
314316
{
315317
memset( &todo, 1, sizeof( todo ) );
@@ -966,7 +968,7 @@ int main(void) {
966968
return -1;
967969
}
968970

969-
int ret = benchmark(0, NULL);
971+
int ret = benchmark(0, NULL, &platform_ctx);
970972
if (ret != 0) {
971973
mbedtls_printf("Benchmark failed with error %d\r\n", ret);
972974
}

hashing/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ static const char hello_str[] = "Hello, world!";
4747
static const unsigned char *hello_buffer = (const unsigned char *) hello_str;
4848
static const size_t hello_len = strlen(hello_str);
4949

50-
static int example(void)
50+
static int example(mbedtls_platform_context* ctx)
5151
{
52+
// The call below is used to avoid the "unused parameter" warning.
53+
// The context itself can be used by cryptographic calls which require it.
54+
(void)ctx;
55+
5256
mbedtls_printf("\r\n\r\n");
5357

5458
/*
@@ -157,7 +161,7 @@ int main() {
157161
return -1;
158162
}
159163

160-
int ret = example();
164+
int ret = example(&platform_ctx);
161165
if (ret != 0) {
162166
mbedtls_printf("Example failed with error %d\r\n", ret);
163167
}

tls-client/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ class HelloHTTPS {
102102
* @param[in] domain The domain name to fetch from
103103
* @param[in] port The port of the HTTPS server
104104
*/
105-
HelloHTTPS(const char * domain, const uint16_t port, NetworkInterface *net_iface) :
105+
HelloHTTPS(const char * domain, const uint16_t port, NetworkInterface *net_iface,
106+
mbedtls_platform_context* platform_ctx) :
106107
_domain(domain), _port(port)
107108
{
108109

110+
_platform_ctx = platform_ctx;
109111
_gothello = false;
110112
_got200 = false;
111113
_bpos = 0;
@@ -406,6 +408,7 @@ class HelloHTTPS {
406408
mbedtls_x509_crt _cacert;
407409
mbedtls_ssl_context _ssl;
408410
mbedtls_ssl_config _ssl_conf;
411+
mbedtls_platform_context* _platform_ctx;
409412
};
410413

411414
/**
@@ -440,7 +443,7 @@ int main() {
440443
return 1;
441444
}
442445

443-
HelloHTTPS *hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, network);
446+
HelloHTTPS *hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, network, &platform_ctx);
444447
hello->startTest(HTTPS_PATH);
445448
delete hello;
446449

0 commit comments

Comments
 (0)