mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-08-19 17:48:33 -04:00
Add FreeRTOS-Plus directory.
This commit is contained in:
parent
7bd5f21ad5
commit
f508a5f653
6798 changed files with 134949 additions and 19 deletions
783
FreeRTOS-Plus/CyaSSL/tests/api.c
Normal file
783
FreeRTOS-Plus/CyaSSL/tests/api.c
Normal file
|
@ -0,0 +1,783 @@
|
|||
/* api.c API unit tests
|
||||
*
|
||||
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
||||
*
|
||||
* This file is part of CyaSSL.
|
||||
*
|
||||
* CyaSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* CyaSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <cyassl/ssl.h>
|
||||
#define NO_MAIN_DRIVER
|
||||
#include <cyassl/test.h>
|
||||
#include "unit.h"
|
||||
|
||||
#define TEST_FAIL (-1)
|
||||
#define TEST_SUCCESS (0)
|
||||
|
||||
static int test_CyaSSL_Init(void);
|
||||
static int test_CyaSSL_Cleanup(void);
|
||||
static int test_CyaSSL_Method_Allocators(void);
|
||||
static int test_CyaSSL_CTX_new(CYASSL_METHOD *method);
|
||||
#ifndef NO_FILESYSTEM
|
||||
static int test_CyaSSL_CTX_use_certificate_file(void);
|
||||
static int test_CyaSSL_CTX_use_PrivateKey_file(void);
|
||||
static int test_CyaSSL_CTX_load_verify_locations(void);
|
||||
static int test_server_CyaSSL_new(void);
|
||||
static int test_client_CyaSSL_new(void);
|
||||
static int test_CyaSSL_read_write(void);
|
||||
#endif
|
||||
|
||||
/* test function helpers */
|
||||
static int test_method(CYASSL_METHOD *method, const char *name);
|
||||
static int test_method2(CYASSL_METHOD *method, const char *name);
|
||||
#ifndef NO_FILESYSTEM
|
||||
static int test_ucf(CYASSL_CTX *ctx, const char* file, int type,
|
||||
int cond, const char* name);
|
||||
static int test_upkf(CYASSL_CTX *ctx, const char* file, int type,
|
||||
int cond, const char* name);
|
||||
static int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path,
|
||||
int cond, const char* name);
|
||||
|
||||
THREAD_RETURN CYASSL_THREAD test_server_nofail(void*);
|
||||
void test_client_nofail(void*);
|
||||
void wait_tcp_ready(func_args*);
|
||||
#endif
|
||||
|
||||
static const char* bogusFile = "/dev/null";
|
||||
static const char* testingFmt = " %s:";
|
||||
static const char* resultFmt = " %s\n";
|
||||
static const char* passed = "passed";
|
||||
static const char* failed = "failed";
|
||||
|
||||
/* List of methods found in echoserver.c that I'm skipping for the moment:
|
||||
* - CyaSSL_CTX_set_session_cache_mode()
|
||||
*/
|
||||
|
||||
int ApiTest(void)
|
||||
{
|
||||
printf(" Begin API Tests\n");
|
||||
test_CyaSSL_Init();
|
||||
test_CyaSSL_Method_Allocators();
|
||||
test_CyaSSL_CTX_new(CyaSSLv23_server_method());
|
||||
#ifndef NO_FILESYSTEM
|
||||
test_CyaSSL_CTX_use_certificate_file();
|
||||
test_CyaSSL_CTX_use_PrivateKey_file();
|
||||
test_CyaSSL_CTX_load_verify_locations();
|
||||
test_server_CyaSSL_new();
|
||||
test_client_CyaSSL_new();
|
||||
test_CyaSSL_read_write();
|
||||
#endif
|
||||
test_CyaSSL_Cleanup();
|
||||
printf(" End API Tests\n");
|
||||
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_Init(void)
|
||||
{
|
||||
int result;
|
||||
|
||||
printf(testingFmt, "CyaSSL_Init()");
|
||||
result = CyaSSL_Init();
|
||||
printf(resultFmt, result ? failed : passed);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int test_CyaSSL_Cleanup(void)
|
||||
{
|
||||
int result;
|
||||
|
||||
printf(testingFmt, "CyaSSL_Cleanup()");
|
||||
result = CyaSSL_Cleanup();
|
||||
printf(resultFmt, result ? failed : passed);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int test_method(CYASSL_METHOD *method, const char *name)
|
||||
{
|
||||
printf(testingFmt, name);
|
||||
if (method == NULL)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
XFREE(method, 0, DYNAMIC_TYPE_METHOD);
|
||||
printf(resultFmt, passed);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_method2(CYASSL_METHOD *method, const char *name)
|
||||
{
|
||||
printf(testingFmt, name);
|
||||
if (method != NULL)
|
||||
{
|
||||
XFREE(method, 0, DYNAMIC_TYPE_METHOD);
|
||||
printf(resultFmt, failed);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
printf(resultFmt, passed);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_Method_Allocators(void)
|
||||
{
|
||||
test_method(CyaSSLv3_server_method(), "CyaSSLv3_server_method()");
|
||||
test_method(CyaSSLv3_client_method(), "CyaSSLv3_client_method()");
|
||||
test_method(CyaTLSv1_server_method(), "CyaTLSv1_server_method()");
|
||||
test_method(CyaTLSv1_client_method(), "CyaTLSv1_client_method()");
|
||||
test_method(CyaTLSv1_1_server_method(), "CyaTLSv1_1_server_method()");
|
||||
test_method(CyaTLSv1_1_client_method(), "CyaTLSv1_1_client_method()");
|
||||
test_method(CyaTLSv1_2_server_method(), "CyaTLSv1_2_server_method()");
|
||||
test_method(CyaTLSv1_2_client_method(), "CyaTLSv1_2_client_method()");
|
||||
test_method(CyaSSLv23_client_method(), "CyaSSLv23_client_method()");
|
||||
|
||||
#ifdef CYASSL_DTLS
|
||||
test_method(CyaDTLSv1_server_method(), "CyaDTLSv1_server_method()");
|
||||
test_method(CyaDTLSv1_client_method(), "CyaDTLSv1_client_method()");
|
||||
#endif /* CYASSL_DTLS */
|
||||
|
||||
#ifdef OPENSSL_EXTRA
|
||||
test_method2(CyaSSLv2_server_method(), "CyaSSLv2_server_method()");
|
||||
test_method2(CyaSSLv2_client_method(), "CyaSSLv2_client_method()");
|
||||
#endif /* OPENSSL_EXTRA */
|
||||
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_CTX_new(CYASSL_METHOD *method)
|
||||
{
|
||||
if (method != NULL)
|
||||
{
|
||||
CYASSL_CTX *ctx;
|
||||
|
||||
printf(testingFmt, "CyaSSL_CTX_new(NULL)");
|
||||
ctx = CyaSSL_CTX_new(NULL);
|
||||
if (ctx != NULL)
|
||||
{
|
||||
CyaSSL_CTX_free(ctx);
|
||||
printf(resultFmt, failed);
|
||||
}
|
||||
else
|
||||
printf(resultFmt, passed);
|
||||
|
||||
printf(testingFmt, "CyaSSL_CTX_new(method)");
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
XFREE(method, 0, DYNAMIC_TYPE_METHOD);
|
||||
/* free the method data. if this was successful, freeing
|
||||
the CTX frees the method. */
|
||||
}
|
||||
else
|
||||
{
|
||||
CyaSSL_CTX_free(ctx);
|
||||
printf(resultFmt, passed);
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("test_CyaSSL_CTX_new() called without method\n");
|
||||
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
#ifndef NO_FILESYSTEM
|
||||
/* Helper for testing CyaSSL_CTX_use_certificate_file() */
|
||||
int test_ucf(CYASSL_CTX *ctx, const char* file, int type, int cond,
|
||||
const char* name)
|
||||
{
|
||||
int result;
|
||||
|
||||
printf(testingFmt, name);
|
||||
result = CyaSSL_CTX_use_certificate_file(ctx, file, type);
|
||||
if (result != cond)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
printf(resultFmt, passed);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_CTX_use_certificate_file(void)
|
||||
{
|
||||
CYASSL_METHOD *method;
|
||||
CYASSL_CTX *ctx;
|
||||
|
||||
method = CyaSSLv23_server_method();
|
||||
if (method == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_use_certificate_file() cannot create method\n");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_use_certificate_file() cannot create context\n");
|
||||
XFREE(method, 0, DYNAMIC_TYPE_METHOD);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
/* setting all parameters to garbage. this should succeed with
|
||||
failure */
|
||||
/* Then set the parameters to legit values but set each item to
|
||||
bogus and call again. Finish with a successful success. */
|
||||
|
||||
test_ucf(NULL, NULL, 9999, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_certificate_file(NULL, NULL, 9999)");
|
||||
/* test_ucf(NULL, svrCert, SSL_FILETYPE_PEM, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_certificate_file(NULL, svrCert, SSL_FILETYPE_PEM)");*/
|
||||
test_ucf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_certificate_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
|
||||
test_ucf(ctx, svrCert, 9999, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_certificate_file(ctx, svrCert, 9999)");
|
||||
test_ucf(ctx, svrCert, SSL_FILETYPE_PEM, SSL_SUCCESS,
|
||||
"CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)");
|
||||
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
/* Helper for testing CyaSSL_CTX_use_PrivateKey_file() */
|
||||
int test_upkf(CYASSL_CTX *ctx, const char* file, int type, int cond,
|
||||
const char* name)
|
||||
{
|
||||
int result;
|
||||
|
||||
printf(testingFmt, name);
|
||||
result = CyaSSL_CTX_use_PrivateKey_file(ctx, file, type);
|
||||
if (result != cond)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
printf(resultFmt, passed);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_CTX_use_PrivateKey_file(void)
|
||||
{
|
||||
CYASSL_METHOD *method;
|
||||
CYASSL_CTX *ctx;
|
||||
|
||||
method = CyaSSLv23_server_method();
|
||||
if (method == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create method\n");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_use_PrivateKey_file() cannot create context\n");
|
||||
XFREE(method, 0, DYNAMIC_TYPE_METHOD);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
test_upkf(NULL, NULL, 9999, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_PrivateKey_file(NULL, NULL, 9999)");
|
||||
/* test_upkf(NULL, svrKey, SSL_FILETYPE_PEM, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_PrivateKey_file(NULL, svrKey, SSL_FILETYPE_PEM)");*/
|
||||
test_upkf(ctx, bogusFile, SSL_FILETYPE_PEM, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_PrivateKey_file(ctx, bogusFile, SSL_FILETYPE_PEM)");
|
||||
test_upkf(ctx, svrKey, 9999, SSL_FAILURE,
|
||||
"CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, 9999)");
|
||||
test_upkf(ctx, svrKey, SSL_FILETYPE_PEM, SSL_SUCCESS,
|
||||
"CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)");
|
||||
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
/* Helper for testing CyaSSL_CTX_load_verify_locations() */
|
||||
int test_lvl(CYASSL_CTX *ctx, const char* file, const char* path, int cond,
|
||||
const char* name)
|
||||
{
|
||||
int result;
|
||||
|
||||
printf(testingFmt, name);
|
||||
/*
|
||||
* CyaSSL_CTX_load_verify_locations() returns SSL_SUCCESS (1) for
|
||||
* success, SSL_FAILURE (0) for a non-specific failure, or a specific
|
||||
* failure code (<0). Need to normalize the return code to 1 or 0.
|
||||
*/
|
||||
result = CyaSSL_CTX_load_verify_locations(ctx, file, path) >= SSL_SUCCESS;
|
||||
if (result != cond)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
printf(resultFmt, passed);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_CyaSSL_CTX_load_verify_locations(void)
|
||||
{
|
||||
CYASSL_METHOD *method;
|
||||
CYASSL_CTX *ctx;
|
||||
|
||||
method = CyaSSLv23_client_method();
|
||||
if (method == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_load_verify_locations() cannot create method\n");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf("test_CyaSSL_CTX_load_verify_locations() cannot create context\n");
|
||||
free(method);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
test_lvl(NULL, NULL, NULL, SSL_FAILURE,
|
||||
"CyaSSL_CTX_load_verify_locations(NULL, NULL, NULL)");
|
||||
test_lvl(ctx, NULL, NULL, SSL_FAILURE,
|
||||
"CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
|
||||
test_lvl(NULL, caCert, NULL, SSL_FAILURE,
|
||||
"CyaSSL_CTX_load_verify_locations(ctx, NULL, NULL)");
|
||||
test_lvl(ctx, caCert, bogusFile, SSL_FAILURE,
|
||||
"CyaSSL_CTX_load_verify_locations(ctx, caCert, bogusFile)");
|
||||
/* Add a test for the certs directory path loading. */
|
||||
/* There is a leak here. If you load a second cert, the first one
|
||||
is lost. */
|
||||
test_lvl(ctx, caCert, 0, SSL_SUCCESS,
|
||||
"CyaSSL_CTX_load_verify_locations(ctx, caCert, 0)");
|
||||
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_server_CyaSSL_new(void)
|
||||
{
|
||||
int result;
|
||||
CYASSL_CTX *ctx;
|
||||
CYASSL_CTX *ctx_nocert;
|
||||
CYASSL *ssl;
|
||||
|
||||
ctx = CyaSSL_CTX_new(CyaSSLv23_server_method());
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf("test_server_CyaSSL_new() cannot create context\n");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
result = CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM);
|
||||
if (result == SSL_FAILURE)
|
||||
{
|
||||
printf("test_server_CyaSSL_new() cannot obtain certificate\n");
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
result = CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM);
|
||||
if (result == SSL_FAILURE)
|
||||
{
|
||||
printf("test_server_CyaSSL_new() cannot obtain key\n");
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_server_method());
|
||||
if (ctx_nocert == NULL)
|
||||
{
|
||||
printf("test_server_CyaSSL_new() cannot create bogus context\n");
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(NULL) server");
|
||||
ssl = CyaSSL_new(NULL);
|
||||
if (ssl != NULL)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
else
|
||||
printf(resultFmt, passed);
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(ctx_nocert) server");
|
||||
ssl = CyaSSL_new(ctx_nocert);
|
||||
if (ssl != NULL)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
else
|
||||
printf(resultFmt, passed);
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(ctx) server");
|
||||
ssl = CyaSSL_new(ctx);
|
||||
if (ssl == NULL)
|
||||
printf(resultFmt, failed);
|
||||
else
|
||||
{
|
||||
printf(resultFmt, passed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
|
||||
CyaSSL_CTX_free(ctx_nocert);
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
int test_client_CyaSSL_new(void)
|
||||
{
|
||||
int result;
|
||||
CYASSL_CTX *ctx;
|
||||
CYASSL_CTX *ctx_nocert;
|
||||
CYASSL *ssl;
|
||||
|
||||
ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
|
||||
if (ctx == NULL)
|
||||
{
|
||||
printf("test_client_CyaSSL_new() cannot create context\n");
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
result = CyaSSL_CTX_load_verify_locations(ctx, caCert, 0);
|
||||
if (result == SSL_FAILURE)
|
||||
{
|
||||
printf("test_client_CyaSSL_new() cannot obtain certificate\n");
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
ctx_nocert = CyaSSL_CTX_new(CyaSSLv23_client_method());
|
||||
if (ctx_nocert == NULL)
|
||||
{
|
||||
printf("test_client_CyaSSL_new() cannot create bogus context\n");
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_FAIL;
|
||||
}
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(NULL) client");
|
||||
ssl = CyaSSL_new(NULL);
|
||||
if (ssl != NULL)
|
||||
{
|
||||
printf(resultFmt, failed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
else
|
||||
printf(resultFmt, passed);
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(ctx_nocert) client");
|
||||
ssl = CyaSSL_new(ctx_nocert);
|
||||
if (ssl == NULL)
|
||||
printf(resultFmt, failed);
|
||||
else
|
||||
{
|
||||
printf(resultFmt, passed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
|
||||
printf(testingFmt, "CyaSSL_new(ctx) client");
|
||||
ssl = CyaSSL_new(ctx);
|
||||
if (ssl == NULL)
|
||||
printf(resultFmt, failed);
|
||||
else
|
||||
{
|
||||
printf(resultFmt, passed);
|
||||
CyaSSL_free(ssl);
|
||||
}
|
||||
|
||||
CyaSSL_CTX_free(ctx_nocert);
|
||||
CyaSSL_CTX_free(ctx);
|
||||
return TEST_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static int test_CyaSSL_read_write(void)
|
||||
{
|
||||
/* The unit testing for read and write shall happen simutaneously, since
|
||||
* one can't do anything with one without the other. (Except for a failure
|
||||
* test case.) This function will call all the others that will set up,
|
||||
* execute, and report their test findings.
|
||||
*
|
||||
* Set up the success case first. This function will become the template
|
||||
* for the other tests. This should eventually be renamed
|
||||
*
|
||||
* The success case isn't interesting, how can this fail?
|
||||
* - Do not give the client context a CA certificate. The connect should
|
||||
* fail. Do not need server for this?
|
||||
* - Using NULL for the ssl object on server. Do not need client for this.
|
||||
* - Using NULL for the ssl object on client. Do not need server for this.
|
||||
* - Good ssl objects for client and server. Client write() without server
|
||||
* read().
|
||||
* - Good ssl objects for client and server. Server write() without client
|
||||
* read().
|
||||
* - Forgetting the password callback?
|
||||
*/
|
||||
int test_result = TEST_SUCCESS;
|
||||
tcp_ready ready;
|
||||
func_args client_args;
|
||||
func_args server_args;
|
||||
THREAD_TYPE serverThread;
|
||||
|
||||
StartTCP();
|
||||
|
||||
InitTcpReady(&ready);
|
||||
server_args.signal = &ready;
|
||||
start_thread(test_server_nofail, &server_args, &serverThread);
|
||||
wait_tcp_ready(&server_args);
|
||||
test_client_nofail(&client_args);
|
||||
join_thread(serverThread);
|
||||
|
||||
if (client_args.return_code != TEST_SUCCESS)
|
||||
{
|
||||
printf(resultFmt, "client failure");
|
||||
test_result = TEST_FAIL;
|
||||
}
|
||||
if (server_args.return_code != TEST_SUCCESS)
|
||||
{
|
||||
printf(resultFmt, "server failure");
|
||||
test_result = TEST_FAIL;
|
||||
}
|
||||
|
||||
FreeTcpReady(&ready);
|
||||
|
||||
return test_result;
|
||||
};
|
||||
|
||||
|
||||
THREAD_RETURN CYASSL_THREAD test_server_nofail(void* args)
|
||||
{
|
||||
SOCKET_T sockfd = 0;
|
||||
int clientfd = 0;
|
||||
|
||||
CYASSL_METHOD* method = 0;
|
||||
CYASSL_CTX* ctx = 0;
|
||||
CYASSL* ssl = 0;
|
||||
|
||||
char msg[] = "I hear you fa shizzle!";
|
||||
char input[1024];
|
||||
int idx;
|
||||
|
||||
((func_args*)args)->return_code = TEST_FAIL;
|
||||
method = CyaSSLv23_server_method();
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
|
||||
CyaSSL_CTX_set_verify(ctx,
|
||||
SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
|
||||
|
||||
#ifdef OPENSSL_EXTRA
|
||||
CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
|
||||
#endif
|
||||
|
||||
if (CyaSSL_CTX_load_verify_locations(ctx, cliCert, 0) != SSL_SUCCESS)
|
||||
{
|
||||
/*err_sys("can't load ca file, Please run from CyaSSL home dir");*/
|
||||
return 0;
|
||||
}
|
||||
if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
|
||||
!= SSL_SUCCESS)
|
||||
{
|
||||
/*err_sys("can't load server cert chain file, "
|
||||
"Please run from CyaSSL home dir");*/
|
||||
return 0;
|
||||
}
|
||||
if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
|
||||
!= SSL_SUCCESS)
|
||||
{
|
||||
/*err_sys("can't load server key file, "
|
||||
"Please run from CyaSSL home dir");*/
|
||||
return 0;
|
||||
}
|
||||
ssl = CyaSSL_new(ctx);
|
||||
tcp_accept(&sockfd, &clientfd, (func_args*)args);
|
||||
#ifndef CYASSL_DTLS
|
||||
CloseSocket(sockfd);
|
||||
#endif
|
||||
|
||||
CyaSSL_set_fd(ssl, clientfd);
|
||||
|
||||
#ifdef NO_PSK
|
||||
#if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
|
||||
CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
|
||||
#else
|
||||
SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
|
||||
#endif
|
||||
#endif
|
||||
if (CyaSSL_accept(ssl) != SSL_SUCCESS)
|
||||
{
|
||||
int err = CyaSSL_get_error(ssl, 0);
|
||||
char buffer[80];
|
||||
printf("error = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
|
||||
/*err_sys("SSL_accept failed");*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
idx = CyaSSL_read(ssl, input, sizeof(input));
|
||||
if (idx > 0) {
|
||||
input[idx] = 0;
|
||||
printf("Client message: %s\n", input);
|
||||
}
|
||||
|
||||
if (CyaSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
|
||||
{
|
||||
/*err_sys("SSL_write failed");*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
CyaSSL_shutdown(ssl);
|
||||
CyaSSL_free(ssl);
|
||||
CyaSSL_CTX_free(ctx);
|
||||
|
||||
CloseSocket(clientfd);
|
||||
((func_args*)args)->return_code = TEST_SUCCESS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void test_client_nofail(void* args)
|
||||
{
|
||||
SOCKET_T sockfd = 0;
|
||||
|
||||
CYASSL_METHOD* method = 0;
|
||||
CYASSL_CTX* ctx = 0;
|
||||
CYASSL* ssl = 0;
|
||||
|
||||
char msg[64] = "hello cyassl!";
|
||||
char reply[1024];
|
||||
int input;
|
||||
int msgSz = strlen(msg);
|
||||
|
||||
int argc = ((func_args*)args)->argc;
|
||||
char** argv = ((func_args*)args)->argv;
|
||||
|
||||
((func_args*)args)->return_code = TEST_FAIL;
|
||||
method = CyaSSLv23_client_method();
|
||||
ctx = CyaSSL_CTX_new(method);
|
||||
|
||||
#ifdef OPENSSL_EXTRA
|
||||
CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
|
||||
#endif
|
||||
|
||||
if (CyaSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
|
||||
{
|
||||
/* err_sys("can't load ca file, Please run from CyaSSL home dir");*/
|
||||
return;
|
||||
}
|
||||
if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
|
||||
!= SSL_SUCCESS)
|
||||
{
|
||||
/*err_sys("can't load client cert file, "
|
||||
"Please run from CyaSSL home dir");*/
|
||||
return;
|
||||
}
|
||||
if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
|
||||
!= SSL_SUCCESS)
|
||||
{
|
||||
/*err_sys("can't load client key file, "
|
||||
"Please run from CyaSSL home dir");*/
|
||||
return;
|
||||
}
|
||||
|
||||
tcp_connect(&sockfd, yasslIP, yasslPort);
|
||||
|
||||
ssl = CyaSSL_new(ctx);
|
||||
CyaSSL_set_fd(ssl, sockfd);
|
||||
if (CyaSSL_connect(ssl) != SSL_SUCCESS)
|
||||
{
|
||||
int err = CyaSSL_get_error(ssl, 0);
|
||||
char buffer[80];
|
||||
printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
|
||||
/*printf("SSL_connect failed");*/
|
||||
return;
|
||||
}
|
||||
|
||||
if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
|
||||
{
|
||||
/*err_sys("SSL_write failed");*/
|
||||
return;
|
||||
}
|
||||
|
||||
input = CyaSSL_read(ssl, reply, sizeof(reply));
|
||||
if (input > 0)
|
||||
{
|
||||
reply[input] = 0;
|
||||
printf("Server response: %s\n", reply);
|
||||
}
|
||||
|
||||
((func_args*)args)->return_code = TEST_SUCCESS;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void wait_tcp_ready(func_args* args)
|
||||
{
|
||||
#ifdef _POSIX_THREADS
|
||||
pthread_mutex_lock(&args->signal->mutex);
|
||||
|
||||
if (!args->signal->ready)
|
||||
pthread_cond_wait(&args->signal->cond, &args->signal->mutex);
|
||||
args->signal->ready = 0; /* reset */
|
||||
|
||||
pthread_mutex_unlock(&args->signal->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void start_thread(THREAD_FUNC fun, func_args* args, THREAD_TYPE* thread)
|
||||
{
|
||||
#ifdef _POSIX_THREADS
|
||||
pthread_create(thread, 0, fun, args);
|
||||
return;
|
||||
#else
|
||||
*thread = (THREAD_TYPE)_beginthreadex(0, 0, fun, args, 0, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void join_thread(THREAD_TYPE thread)
|
||||
{
|
||||
#ifdef _POSIX_THREADS
|
||||
pthread_join(thread, 0);
|
||||
#else
|
||||
int res = WaitForSingleObject(thread, INFINITE);
|
||||
assert(res == WAIT_OBJECT_0);
|
||||
res = CloseHandle(thread);
|
||||
assert(res);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void InitTcpReady(tcp_ready* ready)
|
||||
{
|
||||
ready->ready = 0;
|
||||
#ifdef _POSIX_THREADS
|
||||
pthread_mutex_init(&ready->mutex, 0);
|
||||
pthread_cond_init(&ready->cond, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void FreeTcpReady(tcp_ready* ready)
|
||||
{
|
||||
#ifdef _POSIX_THREADS
|
||||
pthread_mutex_destroy(&ready->mutex);
|
||||
pthread_cond_destroy(&ready->cond);
|
||||
#endif
|
||||
}
|
||||
#endif /* NO_FILESYSTEM */
|
||||
|
||||
|
555
FreeRTOS-Plus/CyaSSL/tests/hash.c
Normal file
555
FreeRTOS-Plus/CyaSSL/tests/hash.c
Normal file
|
@ -0,0 +1,555 @@
|
|||
/* hash.c has unit tests
|
||||
*
|
||||
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
||||
*
|
||||
* This file is part of CyaSSL.
|
||||
*
|
||||
* CyaSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* CyaSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <cyassl/ctaocrypt/md4.h>
|
||||
#include <cyassl/ctaocrypt/md5.h>
|
||||
#include <cyassl/ctaocrypt/sha.h>
|
||||
#include <cyassl/ctaocrypt/sha256.h>
|
||||
#include <cyassl/ctaocrypt/sha512.h>
|
||||
#include <cyassl/ctaocrypt/ripemd.h>
|
||||
#include <cyassl/ctaocrypt/hmac.h>
|
||||
|
||||
#include "unit.h"
|
||||
|
||||
typedef struct testVector {
|
||||
char* input;
|
||||
char* output;
|
||||
size_t inLen;
|
||||
size_t outLen;
|
||||
} testVector;
|
||||
|
||||
int md4_test(void);
|
||||
int md5_test(void);
|
||||
int sha_test(void);
|
||||
int sha256_test(void);
|
||||
int sha512_test(void);
|
||||
int sha384_test(void);
|
||||
int ripemd_test(void);
|
||||
int hmac_test(void);
|
||||
|
||||
int HashTest(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
#ifndef NO_MD4
|
||||
if ( (ret = md4_test()) ) {
|
||||
printf( " MD4 test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " MD4 test passed!\n");
|
||||
#endif
|
||||
|
||||
if ( (ret = md5_test()) ) {
|
||||
printf( " MD5 test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " MD5 test passed!\n");
|
||||
|
||||
if ( (ret = sha_test()) ) {
|
||||
printf( " SHA test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " SHA test passed!\n");
|
||||
|
||||
#ifndef NO_SHA256
|
||||
if ( (ret = sha256_test()) ) {
|
||||
printf( " SHA-256 test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " SHA-256 test passed!\n");
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_SHA512
|
||||
if ( (ret = sha512_test()) ) {
|
||||
printf( " SHA-512 test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " SHA-512 test passed!\n");
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_SHA384
|
||||
if ( (ret = sha384_test()) ) {
|
||||
printf( " SHA-384 test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " SHA-384 test passed!\n");
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_RIPEMD
|
||||
if ( (ret = ripemd_test()) ) {
|
||||
printf( " RIPEMD test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " RIPEMD test passed!\n");
|
||||
#endif
|
||||
|
||||
#ifndef NO_HMAC
|
||||
if ( (ret = hmac_test()) ) {
|
||||
printf( " HMAC test failed!\n");
|
||||
return ret;
|
||||
} else
|
||||
printf( " HMAC test passed!\n");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef NO_MD4
|
||||
|
||||
int md4_test(void)
|
||||
{
|
||||
Md4 md4;
|
||||
byte hash[MD4_DIGEST_SIZE];
|
||||
|
||||
testVector a, b, c, d, e, f, g;
|
||||
testVector test_md4[7];
|
||||
int times = sizeof(test_md4) / sizeof(testVector), i;
|
||||
|
||||
a.input = "";
|
||||
a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
|
||||
"\xc0";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "a";
|
||||
b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
|
||||
"\x24";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
c.input = "abc";
|
||||
c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
|
||||
"\x9d";
|
||||
c.inLen = strlen(c.input);
|
||||
c.outLen = strlen(c.output);
|
||||
|
||||
d.input = "message digest";
|
||||
d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
|
||||
"\x4b";
|
||||
d.inLen = strlen(d.input);
|
||||
d.outLen = strlen(d.output);
|
||||
|
||||
e.input = "abcdefghijklmnopqrstuvwxyz";
|
||||
e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
|
||||
"\xa9";
|
||||
e.inLen = strlen(e.input);
|
||||
e.outLen = strlen(e.output);
|
||||
|
||||
f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
|
||||
"6789";
|
||||
f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
|
||||
"\xe4";
|
||||
f.inLen = strlen(f.input);
|
||||
f.outLen = strlen(f.output);
|
||||
|
||||
g.input = "1234567890123456789012345678901234567890123456789012345678"
|
||||
"9012345678901234567890";
|
||||
g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
|
||||
"\x36";
|
||||
g.inLen = strlen(g.input);
|
||||
g.outLen = strlen(g.output);
|
||||
|
||||
test_md4[0] = a;
|
||||
test_md4[1] = b;
|
||||
test_md4[2] = c;
|
||||
test_md4[3] = d;
|
||||
test_md4[4] = e;
|
||||
test_md4[5] = f;
|
||||
test_md4[6] = g;
|
||||
|
||||
InitMd4(&md4);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
|
||||
Md4Final(&md4, hash);
|
||||
|
||||
if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
|
||||
return -205 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* NO_MD4 */
|
||||
|
||||
int md5_test(void)
|
||||
{
|
||||
Md5 md5;
|
||||
byte hash[MD5_DIGEST_SIZE];
|
||||
|
||||
testVector a, b, c, d, e;
|
||||
testVector test_md5[5];
|
||||
int times = sizeof(test_md5) / sizeof(testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
|
||||
"\x72";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "message digest";
|
||||
b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
|
||||
"\xd0";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
c.input = "abcdefghijklmnopqrstuvwxyz";
|
||||
c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
|
||||
"\x3b";
|
||||
c.inLen = strlen(c.input);
|
||||
c.outLen = strlen(c.output);
|
||||
|
||||
d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
|
||||
"6789";
|
||||
d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
|
||||
"\x9f";
|
||||
d.inLen = strlen(d.input);
|
||||
d.outLen = strlen(d.output);
|
||||
|
||||
e.input = "1234567890123456789012345678901234567890123456789012345678"
|
||||
"9012345678901234567890";
|
||||
e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
|
||||
"\x7a";
|
||||
e.inLen = strlen(e.input);
|
||||
e.outLen = strlen(e.output);
|
||||
|
||||
test_md5[0] = a;
|
||||
test_md5[1] = b;
|
||||
test_md5[2] = c;
|
||||
test_md5[3] = d;
|
||||
test_md5[4] = e;
|
||||
|
||||
InitMd5(&md5);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
|
||||
Md5Final(&md5, hash);
|
||||
|
||||
if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
|
||||
return -5 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sha_test(void)
|
||||
{
|
||||
Sha sha;
|
||||
byte hash[SHA_DIGEST_SIZE];
|
||||
|
||||
testVector a, b, c, d;
|
||||
testVector test_sha[4];
|
||||
int times = sizeof(test_sha) / sizeof(struct testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
|
||||
"\x6C\x9C\xD0\xD8\x9D";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
|
||||
"\xE5\xE5\x46\x70\xF1";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
"aaaaaa";
|
||||
c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
|
||||
"\x2A\x25\xEC\x64\x4D";
|
||||
c.inLen = strlen(c.input);
|
||||
c.outLen = strlen(c.output);
|
||||
|
||||
d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
"aaaaaaaaaa";
|
||||
d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
|
||||
"\x53\x99\x5E\x26\xA0";
|
||||
d.inLen = strlen(d.input);
|
||||
d.outLen = strlen(d.output);
|
||||
|
||||
test_sha[0] = a;
|
||||
test_sha[1] = b;
|
||||
test_sha[2] = c;
|
||||
test_sha[3] = d;
|
||||
|
||||
InitSha(&sha);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
|
||||
ShaFinal(&sha, hash);
|
||||
|
||||
if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
|
||||
return -10 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef NO_SHA256
|
||||
int sha256_test(void)
|
||||
{
|
||||
Sha256 sha;
|
||||
byte hash[SHA256_DIGEST_SIZE];
|
||||
|
||||
testVector a, b;
|
||||
testVector test_sha[2];
|
||||
int times = sizeof(test_sha) / sizeof(struct testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
|
||||
"\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
|
||||
"\x15\xAD";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
|
||||
"\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
|
||||
"\x06\xC1";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
test_sha[0] = a;
|
||||
test_sha[1] = b;
|
||||
|
||||
InitSha256(&sha);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
|
||||
Sha256Final(&sha, hash);
|
||||
|
||||
if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
|
||||
return -10 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_SHA512
|
||||
int sha512_test(void)
|
||||
{
|
||||
Sha512 sha;
|
||||
byte hash[SHA512_DIGEST_SIZE];
|
||||
|
||||
testVector a, b;
|
||||
testVector test_sha[2];
|
||||
int times = sizeof(test_sha) / sizeof(struct testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
|
||||
"\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
|
||||
"\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
|
||||
"\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
|
||||
"\xa5\x4c\xa4\x9f";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
|
||||
"jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
|
||||
b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
|
||||
"\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
|
||||
"\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
|
||||
"\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
|
||||
"\x87\x4b\xe9\x09";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
test_sha[0] = a;
|
||||
test_sha[1] = b;
|
||||
|
||||
InitSha512(&sha);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
|
||||
Sha512Final(&sha, hash);
|
||||
|
||||
if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
|
||||
return -10 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_SHA384
|
||||
int sha384_test()
|
||||
{
|
||||
Sha384 sha;
|
||||
byte hash[SHA384_DIGEST_SIZE];
|
||||
|
||||
testVector a, b;
|
||||
testVector test_sha[2];
|
||||
int times = sizeof(test_sha) / sizeof(struct testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
|
||||
"\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
|
||||
"\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
|
||||
"\xc8\x25\xa7";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
|
||||
"jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
|
||||
b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
|
||||
"\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
|
||||
"\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
|
||||
"\x74\x60\x39";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
test_sha[0] = a;
|
||||
test_sha[1] = b;
|
||||
|
||||
InitSha384(&sha);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
|
||||
Sha384Final(&sha, hash);
|
||||
|
||||
if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
|
||||
return -10 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CYASSL_RIPEMD
|
||||
int ripemd_test(void)
|
||||
{
|
||||
RipeMd ripemd;
|
||||
byte hash[RIPEMD_DIGEST_SIZE];
|
||||
|
||||
testVector a, b, c, d;
|
||||
testVector test_ripemd[4];
|
||||
int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
|
||||
|
||||
a.input = "abc";
|
||||
a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
|
||||
"\xb0\x87\xf1\x5a\x0b\xfc";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "message digest";
|
||||
b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
|
||||
"\x5f\xfa\x21\x59\x5f\x36";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
|
||||
"\xf4\x9a\xda\x62\xeb\x2b";
|
||||
c.inLen = strlen(c.input);
|
||||
c.outLen = strlen(c.output);
|
||||
|
||||
d.input = "12345678901234567890123456789012345678901234567890123456"
|
||||
"789012345678901234567890";
|
||||
d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
|
||||
"\x82\xbf\x63\x32\x6b\xfb";
|
||||
d.inLen = strlen(d.input);
|
||||
d.outLen = strlen(d.output);
|
||||
|
||||
test_ripemd[0] = a;
|
||||
test_ripemd[1] = b;
|
||||
test_ripemd[2] = c;
|
||||
test_ripemd[3] = d;
|
||||
|
||||
InitRipeMd(&ripemd);
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
|
||||
(word32)test_ripemd[i].inLen);
|
||||
RipeMdFinal(&ripemd, hash);
|
||||
|
||||
if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
|
||||
return -10 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* CYASSL_RIPEMD */
|
||||
|
||||
#ifndef NO_HMAC
|
||||
int hmac_test(void)
|
||||
{
|
||||
Hmac hmac;
|
||||
byte hash[MD5_DIGEST_SIZE];
|
||||
|
||||
const char* keys[]=
|
||||
{
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
|
||||
"Jefe",
|
||||
"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
|
||||
};
|
||||
|
||||
testVector a, b, c;
|
||||
testVector test_hmac[3];
|
||||
|
||||
int times = sizeof(test_hmac) / sizeof(testVector), i;
|
||||
|
||||
a.input = "Hi There";
|
||||
a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
|
||||
"\x9d";
|
||||
a.inLen = strlen(a.input);
|
||||
a.outLen = strlen(a.output);
|
||||
|
||||
b.input = "what do ya want for nothing?";
|
||||
b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
|
||||
"\x38";
|
||||
b.inLen = strlen(b.input);
|
||||
b.outLen = strlen(b.output);
|
||||
|
||||
c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
|
||||
"\xDD\xDD\xDD\xDD\xDD\xDD";
|
||||
c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
|
||||
"\xf6";
|
||||
c.inLen = strlen(c.input);
|
||||
c.outLen = strlen(c.output);
|
||||
|
||||
test_hmac[0] = a;
|
||||
test_hmac[1] = b;
|
||||
test_hmac[2] = c;
|
||||
|
||||
for (i = 0; i < times; ++i) {
|
||||
HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
|
||||
HmacUpdate(&hmac, (byte*)test_hmac[i].input,
|
||||
(word32)test_hmac[i].inLen);
|
||||
HmacFinal(&hmac, hash);
|
||||
|
||||
if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
|
||||
return -20 - i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
16
FreeRTOS-Plus/CyaSSL/tests/include.am
Normal file
16
FreeRTOS-Plus/CyaSSL/tests/include.am
Normal file
|
@ -0,0 +1,16 @@
|
|||
# vim:ft=automake
|
||||
# included from Top Level Makefile.am
|
||||
# All paths should be given relative to the root
|
||||
|
||||
|
||||
check_PROGRAMS += tests/unit
|
||||
noinst_PROGRAMS += tests/unit
|
||||
tests_unit_SOURCES = \
|
||||
tests/unit.c \
|
||||
tests/api.c \
|
||||
tests/suites.c \
|
||||
tests/hash.c
|
||||
tests_unit_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
|
||||
tests_unit_LDADD = src/libcyassl.la $(PTHREAD_LIBS)
|
||||
tests_unit_DEPENDENCIES = src/libcyassl.la
|
||||
EXTRA_DIST+=tests/unit.h
|
1
FreeRTOS-Plus/CyaSSL/tests/suites.c
Normal file
1
FreeRTOS-Plus/CyaSSL/tests/suites.c
Normal file
|
@ -0,0 +1 @@
|
|||
/* suites.c cipher suite unit tests */
|
16
FreeRTOS-Plus/CyaSSL/tests/unit.c
Normal file
16
FreeRTOS-Plus/CyaSSL/tests/unit.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/* unit.c unit tests driver */
|
||||
#include <stdio.h>
|
||||
#include "unit.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
printf("hello unit tests\n");
|
||||
|
||||
if (ApiTest() != 0)
|
||||
printf("api test failed\n");
|
||||
|
||||
if (HashTest() != 0)
|
||||
printf("hash test failed\n");
|
||||
|
||||
return 0;
|
||||
}
|
6
FreeRTOS-Plus/CyaSSL/tests/unit.h
Normal file
6
FreeRTOS-Plus/CyaSSL/tests/unit.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
/* unit.h unit tests driver */
|
||||
|
||||
int ApiTest(void);
|
||||
int SuiteTest(void);
|
||||
int HashTest(void);
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue