mirror of https://github.com/wolfSSL/wolfssh.git
Module Static Global
1. Move ConvertHexToBin() and Base16_Decode() into test.h. 2. Add a description for the options in test.h. 3. Add a static to the hexDecode const array for Base16_Decode(). 4. Add the check for options.h/user_settings.h to api and unit tests.pull/471/head
parent
f88c3d6d3a
commit
c580b2c3ba
169
tests/api.c
169
tests/api.c
|
@ -22,22 +22,28 @@
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_USER_SETTINGS
|
||||||
|
#include <wolfssl/wolfcrypt/settings.h>
|
||||||
|
#else
|
||||||
|
#include <wolfssl/options.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <wolfssh/ssh.h>
|
#include <wolfssh/ssh.h>
|
||||||
#include <wolfssh/internal.h>
|
#include <wolfssh/internal.h>
|
||||||
#ifdef WOLFSSH_SCP
|
#ifdef WOLFSSH_SCP
|
||||||
#include <wolfssh/wolfscp.h>
|
#include <wolfssh/wolfscp.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef WOLFSSH_SFTP
|
#ifdef WOLFSSH_SFTP
|
||||||
#define WOLFSSH_TEST_LOCKING
|
#define WOLFSSH_TEST_LOCKING
|
||||||
#define WOLFSSH_TEST_THREADING
|
#define WOLFSSH_TEST_THREADING
|
||||||
|
|
||||||
#define WOLFSSH_TEST_SERVER
|
#define WOLFSSH_TEST_SERVER
|
||||||
#define WOLFSSH_TEST_ECHOSERVER
|
#define WOLFSSH_TEST_ECHOSERVER
|
||||||
#include <wolfssh/test.h>
|
|
||||||
|
|
||||||
#include "examples/echoserver/echoserver.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
#define WOLFSSH_TEST_HEX2BIN
|
||||||
|
#include <wolfssh/test.h>
|
||||||
#include "tests/api.h"
|
#include "tests/api.h"
|
||||||
|
|
||||||
/* for echoserver test cases */
|
/* for echoserver test cases */
|
||||||
|
@ -95,161 +101,10 @@ char* myoptarg = NULL;
|
||||||
#define AssertStrLE(x, y) AssertStr(x, y, <=, >)
|
#define AssertStrLE(x, y) AssertStr(x, y, <=, >)
|
||||||
|
|
||||||
|
|
||||||
/* Utility functions */
|
|
||||||
|
|
||||||
#define BAD 0xFF
|
|
||||||
|
|
||||||
const byte hexDecode[] =
|
|
||||||
{
|
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
10, 11, 12, 13, 14, 15, /* upper case A-F */
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, /* G - ` */
|
|
||||||
10, 11, 12, 13, 14, 15 /* lower case a-f */
|
|
||||||
}; /* A starts at 0x41 not 0x3A */
|
|
||||||
|
|
||||||
|
|
||||||
static int Base16_Decode(const byte* in, word32 inLen,
|
|
||||||
byte* out, word32* outLen)
|
|
||||||
{
|
|
||||||
word32 inIdx = 0;
|
|
||||||
word32 outIdx = 0;
|
|
||||||
|
|
||||||
if (inLen == 1 && *outLen && in) {
|
|
||||||
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
|
|
||||||
|
|
||||||
/* sanity check */
|
|
||||||
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
b = hexDecode[b];
|
|
||||||
|
|
||||||
if (b == BAD)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
out[outIdx++] = b;
|
|
||||||
|
|
||||||
*outLen = outIdx;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inLen % 2)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (*outLen < (inLen / 2))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
while (inLen) {
|
|
||||||
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
|
|
||||||
byte b2 = in[inIdx++] - 0x30;
|
|
||||||
|
|
||||||
/* sanity checks */
|
|
||||||
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
b = hexDecode[b];
|
|
||||||
b2 = hexDecode[b2];
|
|
||||||
|
|
||||||
if (b == BAD || b2 == BAD)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
out[outIdx++] = (byte)((b << 4) | b2);
|
|
||||||
inLen -= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
*outLen = outIdx;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
|
|
||||||
{
|
|
||||||
if (b1 != NULL) free(b1);
|
|
||||||
if (b2 != NULL) free(b2);
|
|
||||||
if (b3 != NULL) free(b3);
|
|
||||||
if (b4 != NULL) free(b4);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* convert hex string to binary, store size, 0 success (free mem on failure) */
|
|
||||||
static int ConvertHexToBin(const char* h1, byte** b1, word32* b1Sz,
|
|
||||||
const char* h2, byte** b2, word32* b2Sz,
|
|
||||||
const char* h3, byte** b3, word32* b3Sz,
|
|
||||||
const char* h4, byte** b4, word32* b4Sz)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* b1 */
|
|
||||||
if (h1 && b1 && b1Sz) {
|
|
||||||
*b1Sz = (word32)strlen(h1) / 2;
|
|
||||||
*b1 = (byte*)malloc(*b1Sz);
|
|
||||||
if (*b1 == NULL)
|
|
||||||
return -1;
|
|
||||||
ret = Base16_Decode((const byte*)h1, (word32)strlen(h1),
|
|
||||||
*b1, b1Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(*b1, NULL, NULL, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b2 */
|
|
||||||
if (h2 && b2 && b2Sz) {
|
|
||||||
*b2Sz = (word32)strlen(h2) / 2;
|
|
||||||
*b2 = (byte*)malloc(*b2Sz);
|
|
||||||
if (*b2 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, NULL, NULL, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h2, (word32)strlen(h2),
|
|
||||||
*b2, b2Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, *b2, NULL, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b3 */
|
|
||||||
if (h3 && b3 && b3Sz) {
|
|
||||||
*b3Sz = (word32)strlen(h3) / 2;
|
|
||||||
*b3 = (byte*)malloc(*b3Sz);
|
|
||||||
if (*b3 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, NULL, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h3, (word32)strlen(h3),
|
|
||||||
*b3, b3Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, *b3, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b4 */
|
|
||||||
if (h4 && b4 && b4Sz) {
|
|
||||||
*b4Sz = (word32)strlen(h4) / 2;
|
|
||||||
*b4 = (byte*)malloc(*b4Sz);
|
|
||||||
if (*b4 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, NULL);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h4, (word32)strlen(h4),
|
|
||||||
*b4, b4Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, *b4);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
|
#if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT)
|
||||||
|
|
||||||
|
#include "examples/echoserver/echoserver.h"
|
||||||
|
|
||||||
byte userPassword[256];
|
byte userPassword[256];
|
||||||
static int sftpUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
|
static int sftpUserAuth(byte authType, WS_UserAuthData* authData, void* ctx)
|
||||||
{
|
{
|
||||||
|
|
180
tests/unit.c
180
tests/unit.c
|
@ -22,184 +22,22 @@
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_USER_SETTINGS
|
||||||
|
#include <wolfssl/wolfcrypt/settings.h>
|
||||||
|
#else
|
||||||
|
#include <wolfssl/options.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <wolfssh/ssh.h>
|
#include <wolfssh/ssh.h>
|
||||||
#include <wolfssh/keygen.h>
|
#include <wolfssh/keygen.h>
|
||||||
#include <wolfssh/internal.h>
|
#include <wolfssh/internal.h>
|
||||||
|
|
||||||
|
#define WOLFSSH_TEST_HEX2BIN
|
||||||
|
#include <wolfssh/test.h>
|
||||||
#include "tests/unit.h"
|
#include "tests/unit.h"
|
||||||
|
|
||||||
|
|
||||||
/* Utility functions */
|
|
||||||
|
|
||||||
#define BAD 0xFF
|
|
||||||
|
|
||||||
const byte hexDecode[] =
|
|
||||||
{
|
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
10, 11, 12, 13, 14, 15, /* upper case A-F */
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
|
||||||
BAD, BAD, /* G - ` */
|
|
||||||
10, 11, 12, 13, 14, 15 /* lower case a-f */
|
|
||||||
}; /* A starts at 0x41 not 0x3A */
|
|
||||||
|
|
||||||
|
|
||||||
static int Base16_Decode(const byte* in, word32 inLen,
|
|
||||||
byte* out, word32* outLen)
|
|
||||||
{
|
|
||||||
word32 inIdx = 0;
|
|
||||||
word32 outIdx = 0;
|
|
||||||
|
|
||||||
if (inLen == 1 && *outLen && in) {
|
|
||||||
byte b = in[inIdx] - 0x30; /* 0 starts at 0x30 */
|
|
||||||
|
|
||||||
/* sanity check */
|
|
||||||
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
b = hexDecode[b];
|
|
||||||
|
|
||||||
if (b == BAD)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
out[outIdx++] = b;
|
|
||||||
|
|
||||||
*outLen = outIdx;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inLen % 2)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (*outLen < (inLen / 2))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
while (inLen) {
|
|
||||||
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
|
|
||||||
byte b2 = in[inIdx++] - 0x30;
|
|
||||||
|
|
||||||
/* sanity checks */
|
|
||||||
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
b = hexDecode[b];
|
|
||||||
b2 = hexDecode[b2];
|
|
||||||
|
|
||||||
if (b == BAD || b2 == BAD)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
out[outIdx++] = (byte)((b << 4) | b2);
|
|
||||||
inLen -= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
*outLen = outIdx;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
|
|
||||||
{
|
|
||||||
if (b1 != NULL) free(b1);
|
|
||||||
if (b2 != NULL) free(b2);
|
|
||||||
if (b3 != NULL) free(b3);
|
|
||||||
if (b4 != NULL) free(b4);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* convert hex string to binary, store size, 0 success (free mem on failure) */
|
|
||||||
static int ConvertHexToBin(const char* h1, byte** b1, word32* b1Sz,
|
|
||||||
const char* h2, byte** b2, word32* b2Sz,
|
|
||||||
const char* h3, byte** b3, word32* b3Sz,
|
|
||||||
const char* h4, byte** b4, word32* b4Sz)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* b1 */
|
|
||||||
if (h1 && b1 && b1Sz) {
|
|
||||||
*b1Sz = (word32)strlen(h1) / 2;
|
|
||||||
*b1 = (byte*)malloc(*b1Sz);
|
|
||||||
if (*b1 == NULL)
|
|
||||||
return -1;
|
|
||||||
ret = Base16_Decode((const byte*)h1, (word32)strlen(h1),
|
|
||||||
*b1, b1Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(*b1, NULL, NULL, NULL);
|
|
||||||
*b1 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b2 */
|
|
||||||
if (h2 && b2 && b2Sz) {
|
|
||||||
*b2Sz = (word32)strlen(h2) / 2;
|
|
||||||
*b2 = (byte*)malloc(*b2Sz);
|
|
||||||
if (*b2 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, NULL, NULL, NULL);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h2, (word32)strlen(h2),
|
|
||||||
*b2, b2Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, *b2, NULL, NULL);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
*b2 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b3 */
|
|
||||||
if (h3 && b3 && b3Sz) {
|
|
||||||
*b3Sz = (word32)strlen(h3) / 2;
|
|
||||||
*b3 = (byte*)malloc(*b3Sz);
|
|
||||||
if (*b3 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, NULL, NULL);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
if (b2) *b2 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h3, (word32)strlen(h3),
|
|
||||||
*b3, b3Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, *b3, NULL);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
if (b2) *b2 = NULL;
|
|
||||||
*b3 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* b4 */
|
|
||||||
if (h4 && b4 && b4Sz) {
|
|
||||||
*b4Sz = (word32)strlen(h4) / 2;
|
|
||||||
*b4 = (byte*)malloc(*b4Sz);
|
|
||||||
if (*b4 == NULL) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, NULL);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
if (b2) *b2 = NULL;
|
|
||||||
if (b3) *b3 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
ret = Base16_Decode((const byte*)h4, (word32)strlen(h4),
|
|
||||||
*b4, b4Sz);
|
|
||||||
if (ret != 0) {
|
|
||||||
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, *b4);
|
|
||||||
if (b1) *b1 = NULL;
|
|
||||||
if (b2) *b2 = NULL;
|
|
||||||
if (b3) *b3 = NULL;
|
|
||||||
*b4 = NULL;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Key Derivation Function (KDF) Unit Test */
|
/* Key Derivation Function (KDF) Unit Test */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
185
wolfssh/test.h
185
wolfssh/test.h
|
@ -18,6 +18,18 @@
|
||||||
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
* along with wolfSSH. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file contains some utility code shared between the wolfSSH test
|
||||||
|
* tools and examples. This is divided into a few sets of functions that
|
||||||
|
* may be enabled with flags included before including this file:
|
||||||
|
*
|
||||||
|
* WOLFSSH_TEST_CLIENT: Client utility functions
|
||||||
|
* WOLFSSH_TEST_SERVER: Server utility functions
|
||||||
|
* WOLFSSH_TEST_LOCKING: Mutex wrappers
|
||||||
|
* WOLFSSH_TEST_THREADING: Threading wrappers
|
||||||
|
* WOLFSSH_TEST_HEX2BIN: Hex2Bin conversion
|
||||||
|
* TEST_IPV6: IPv6 addressing options
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef _WOLFSSH_TEST_H_
|
#ifndef _WOLFSSH_TEST_H_
|
||||||
#define _WOLFSSH_TEST_H_
|
#define _WOLFSSH_TEST_H_
|
||||||
|
@ -942,4 +954,177 @@ static INLINE void build_addr_ipv6(struct sockaddr_in6* addr, const char* peer,
|
||||||
}
|
}
|
||||||
#endif /* TEST_IPV6 */
|
#endif /* TEST_IPV6 */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef WOLFSSH_TEST_HEX2BIN
|
||||||
|
|
||||||
|
#define BAD 0xFF
|
||||||
|
|
||||||
|
static const byte hexDecode[] =
|
||||||
|
{
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
||||||
|
10, 11, 12, 13, 14, 15, /* upper case A-F */
|
||||||
|
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
||||||
|
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
||||||
|
BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
|
||||||
|
BAD, BAD, /* G - ` */
|
||||||
|
10, 11, 12, 13, 14, 15 /* lower case a-f */
|
||||||
|
}; /* A starts at 0x41 not 0x3A */
|
||||||
|
|
||||||
|
|
||||||
|
static int Base16_Decode(const byte* in, word32 inLen,
|
||||||
|
byte* out, word32* outLen)
|
||||||
|
{
|
||||||
|
word32 inIdx = 0;
|
||||||
|
word32 outIdx = 0;
|
||||||
|
|
||||||
|
if (inLen == 1 && *outLen && in) {
|
||||||
|
byte b = in[inIdx] - 0x30; /* 0 starts at 0x30 */
|
||||||
|
|
||||||
|
/* sanity check */
|
||||||
|
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
b = hexDecode[b];
|
||||||
|
|
||||||
|
if (b == BAD)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
out[outIdx++] = b;
|
||||||
|
|
||||||
|
*outLen = outIdx;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inLen % 2)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (*outLen < (inLen / 2))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
while (inLen) {
|
||||||
|
byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
|
||||||
|
byte b2 = in[inIdx++] - 0x30;
|
||||||
|
|
||||||
|
/* sanity checks */
|
||||||
|
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
||||||
|
return -1;
|
||||||
|
if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
b = hexDecode[b];
|
||||||
|
b2 = hexDecode[b2];
|
||||||
|
|
||||||
|
if (b == BAD || b2 == BAD)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
out[outIdx++] = (byte)((b << 4) | b2);
|
||||||
|
inLen -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
*outLen = outIdx;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void FreeBins(byte* b1, byte* b2, byte* b3, byte* b4)
|
||||||
|
{
|
||||||
|
if (b1 != NULL) free(b1);
|
||||||
|
if (b2 != NULL) free(b2);
|
||||||
|
if (b3 != NULL) free(b3);
|
||||||
|
if (b4 != NULL) free(b4);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* convert hex string to binary, store size, 0 success (free mem on failure) */
|
||||||
|
static int ConvertHexToBin(const char* h1, byte** b1, word32* b1Sz,
|
||||||
|
const char* h2, byte** b2, word32* b2Sz,
|
||||||
|
const char* h3, byte** b3, word32* b3Sz,
|
||||||
|
const char* h4, byte** b4, word32* b4Sz)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* b1 */
|
||||||
|
if (h1 && b1 && b1Sz) {
|
||||||
|
*b1Sz = (word32)strlen(h1) / 2;
|
||||||
|
*b1 = (byte*)malloc(*b1Sz);
|
||||||
|
if (*b1 == NULL)
|
||||||
|
return -1;
|
||||||
|
ret = Base16_Decode((const byte*)h1, (word32)strlen(h1),
|
||||||
|
*b1, b1Sz);
|
||||||
|
if (ret != 0) {
|
||||||
|
FreeBins(*b1, NULL, NULL, NULL);
|
||||||
|
*b1 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* b2 */
|
||||||
|
if (h2 && b2 && b2Sz) {
|
||||||
|
*b2Sz = (word32)strlen(h2) / 2;
|
||||||
|
*b2 = (byte*)malloc(*b2Sz);
|
||||||
|
if (*b2 == NULL) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, NULL, NULL, NULL);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ret = Base16_Decode((const byte*)h2, (word32)strlen(h2),
|
||||||
|
*b2, b2Sz);
|
||||||
|
if (ret != 0) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, *b2, NULL, NULL);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
*b2 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* b3 */
|
||||||
|
if (h3 && b3 && b3Sz) {
|
||||||
|
*b3Sz = (word32)strlen(h3) / 2;
|
||||||
|
*b3 = (byte*)malloc(*b3Sz);
|
||||||
|
if (*b3 == NULL) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, NULL, NULL);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
if (b2) *b2 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ret = Base16_Decode((const byte*)h3, (word32)strlen(h3),
|
||||||
|
*b3, b3Sz);
|
||||||
|
if (ret != 0) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, *b3, NULL);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
if (b2) *b2 = NULL;
|
||||||
|
*b3 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* b4 */
|
||||||
|
if (h4 && b4 && b4Sz) {
|
||||||
|
*b4Sz = (word32)strlen(h4) / 2;
|
||||||
|
*b4 = (byte*)malloc(*b4Sz);
|
||||||
|
if (*b4 == NULL) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, NULL);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
if (b2) *b2 = NULL;
|
||||||
|
if (b3) *b3 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ret = Base16_Decode((const byte*)h4, (word32)strlen(h4),
|
||||||
|
*b4, b4Sz);
|
||||||
|
if (ret != 0) {
|
||||||
|
FreeBins(b1 ? *b1 : NULL, b2 ? *b2 : NULL, b3 ? *b3 : NULL, *b4);
|
||||||
|
if (b1) *b1 = NULL;
|
||||||
|
if (b2) *b2 = NULL;
|
||||||
|
if (b3) *b3 = NULL;
|
||||||
|
*b4 = NULL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* WOLFSSH_TEST_HEX2BIN */
|
||||||
|
|
||||||
#endif /* _WOLFSSH_TEST_H_ */
|
#endif /* _WOLFSSH_TEST_H_ */
|
||||||
|
|
Loading…
Reference in New Issue