Skip to content

Commit 9a36b87

Browse files
Andrzej KurekPatater
authored andcommitted
Pass platform context to tests
Add platform context as a parameter for tests to enable the usage of it in cryptographic calls
1 parent 7c78056 commit 9a36b87

File tree

8 files changed

+32
-14
lines changed

8 files changed

+32
-14
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/HelloHttpsClient.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ const char *HelloHttpsClient::HTTP_HELLO_STR = "Hello world!";
7070
const char *HelloHttpsClient::HTTP_OK_STR = "200 OK";
7171

7272
HelloHttpsClient::HelloHttpsClient(const char *in_server_name,
73-
const uint16_t in_server_port) :
73+
const uint16_t in_server_port,
74+
mbedtls_platform_context* in_platform_ctx) :
7475
socket(),
7576
server_name(in_server_name),
76-
server_port(in_server_port)
77+
server_port(in_server_port),
78+
platform_ctx(in_platform_ctx)
7779
{
7880
mbedtls_entropy_init(&entropy);
7981
mbedtls_ctr_drbg_init(&ctr_drbg);

tls-client/HelloHttpsClient.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "TCPSocket.h"
2626

2727
#include "mbedtls/config.h"
28+
#include "mbedtls/platform.h"
2829
#include "mbedtls/ssl.h"
2930
#include "mbedtls/entropy.h"
3031
#include "mbedtls/ctr_drbg.h"
@@ -60,7 +61,8 @@ class HelloHttpsClient
6061
* The server port
6162
*/
6263
HelloHttpsClient(const char *in_server_name,
63-
const uint16_t in_server_port);
64+
const uint16_t in_server_port,
65+
mbedtls_platform_context* in_platform_ctx);
6466

6567
/**
6668
* Free any allocated resources
@@ -223,6 +225,8 @@ class HelloHttpsClient
223225
* The TLS configuration in use
224226
*/
225227
mbedtls_ssl_config ssl_conf;
228+
229+
mbedtls_platform_context* platform_ctx;
226230
};
227231

228232
#endif /* _HELLOHTTPSCLIENT_H_ */

tls-client/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ int main()
7171
#endif /* MBEDTLS_MAJOR_VERSION */
7272

7373
/* Allocate a HTTPS client */
74-
client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_PORT);
74+
client = new (std::nothrow) HelloHttpsClient(SERVER_NAME, SERVER_PORT,
75+
&platform_ctx);
7576
if (client == NULL) {
7677
mbedtls_printf("Failed to allocate HelloHttpsClient object\n"
7778
"\nFAIL\n");

0 commit comments

Comments
 (0)