mirror of https://github.com/wolfSSL/wolfssl.git
adds wc_SrpInit() with unit tests.
parent
6d7b5bd2f8
commit
dfa956d227
|
@ -11,6 +11,7 @@ tests_unit_test_SOURCES = \
|
||||||
tests/api.c \
|
tests/api.c \
|
||||||
tests/suites.c \
|
tests/suites.c \
|
||||||
tests/hash.c \
|
tests/hash.c \
|
||||||
|
tests/srp.c \
|
||||||
examples/client/client.c \
|
examples/client/client.c \
|
||||||
examples/server/server.c
|
examples/server/server.c
|
||||||
tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS)
|
tests_unit_test_CFLAGS = -DNO_MAIN_DRIVER $(AM_CFLAGS)
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
/* srp.c SRP unit tests
|
||||||
|
*
|
||||||
|
* Copyright (C) 2006-2015 wolfSSL Inc.
|
||||||
|
*
|
||||||
|
* This file is part of wolfSSL. (formerly known as CyaSSL)
|
||||||
|
*
|
||||||
|
* wolfSSL 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.
|
||||||
|
*
|
||||||
|
* wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include <config.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <wolfssl/wolfcrypt/settings.h>
|
||||||
|
|
||||||
|
#include <tests/unit.h>
|
||||||
|
#include <wolfssl/wolfcrypt/srp.h>
|
||||||
|
|
||||||
|
#ifdef WOLFCRYPT_HAVE_SRP
|
||||||
|
|
||||||
|
static void test_SrpInit(void)
|
||||||
|
{
|
||||||
|
byte N[] = {
|
||||||
|
0x00, 0xc0, 0x37, 0xc3, 0x75, 0x88, 0xb4, 0x32, 0x98, 0x87, 0xe6, 0x1c,
|
||||||
|
0x2d, 0xa3, 0x32, 0x4b, 0x1b, 0xa4, 0xb8, 0x1a, 0x63, 0xf9, 0x74, 0x8f,
|
||||||
|
0xed, 0x2d, 0x8a, 0x41, 0x0c, 0x2f, 0xc2, 0x1b, 0x12, 0x32, 0xf0, 0xd3,
|
||||||
|
0xbf, 0xa0, 0x24, 0x27, 0x6c, 0xfd, 0x88, 0x44, 0x81, 0x97, 0xaa, 0xe4,
|
||||||
|
0x86, 0xa6, 0x3b, 0xfc, 0xa7, 0xb8, 0xbf, 0x77, 0x54, 0xdf, 0xb3, 0x27,
|
||||||
|
0xc7, 0x20, 0x1f, 0x6f, 0xd1, 0x7f, 0xd7, 0xfd, 0x74, 0x15, 0x8b, 0xd3,
|
||||||
|
0x1c, 0xe7, 0x72, 0xc9, 0xf5, 0xf8, 0xab, 0x58, 0x45, 0x48, 0xa9, 0x9a,
|
||||||
|
0x75, 0x9b, 0x5a, 0x2c, 0x05, 0x32, 0x16, 0x2b, 0x7b, 0x62, 0x18, 0xe8,
|
||||||
|
0xf1, 0x42, 0xbc, 0xe2, 0xc3, 0x0d, 0x77, 0x84, 0x68, 0x9a, 0x48, 0x3e,
|
||||||
|
0x09, 0x5e, 0x70, 0x16, 0x18, 0x43, 0x79, 0x13, 0xa8, 0xc3, 0x9c, 0x3d,
|
||||||
|
0xd0, 0xd4, 0xca, 0x3c, 0x50, 0x0b, 0x88, 0x5f, 0xe3
|
||||||
|
};
|
||||||
|
|
||||||
|
byte g[] = {
|
||||||
|
0x02
|
||||||
|
};
|
||||||
|
|
||||||
|
Srp srp;
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(NULL, SRP_TYPE_SHA, SRP_CLIENT_SIDE, N, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(&srp, 255, SRP_CLIENT_SIDE, N, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(&srp, SRP_TYPE_SHA, 255, N, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(&srp, SRP_TYPE_SHA, 255, N, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(&srp, SRP_TYPE_SHA, SRP_CLIENT_SIDE, NULL, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(BAD_FUNC_ARG,
|
||||||
|
wc_SrpInit(&srp, SRP_TYPE_SHA, SRP_CLIENT_SIDE, N, sizeof(N),
|
||||||
|
NULL, sizeof(g)));
|
||||||
|
|
||||||
|
AssertIntEQ(0,
|
||||||
|
wc_SrpInit(&srp, SRP_TYPE_SHA, SRP_CLIENT_SIDE, N, sizeof(N),
|
||||||
|
g, sizeof(g)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void SrpTest(void)
|
||||||
|
{
|
||||||
|
#ifdef WOLFCRYPT_HAVE_SRP
|
||||||
|
test_SrpInit();
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -77,6 +77,8 @@ int unit_test(int argc, char** argv)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
SrpTest();
|
||||||
|
|
||||||
#ifdef HAVE_CAVIUM
|
#ifdef HAVE_CAVIUM
|
||||||
CspShutdown(CAVIUM_DEV_ID);
|
CspShutdown(CAVIUM_DEV_ID);
|
||||||
#endif
|
#endif
|
||||||
|
@ -176,4 +178,3 @@ void FreeTcpReady(tcp_ready* ready)
|
||||||
(void)ready;
|
(void)ready;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -76,9 +76,9 @@
|
||||||
|
|
||||||
|
|
||||||
void ApiTest(void);
|
void ApiTest(void);
|
||||||
int SuiteTest(void);
|
int SuiteTest(void);
|
||||||
int HashTest(void);
|
int HashTest(void);
|
||||||
|
void SrpTest(void);
|
||||||
|
|
||||||
|
|
||||||
#endif /* CyaSSL_UNIT_H */
|
#endif /* CyaSSL_UNIT_H */
|
||||||
|
|
||||||
|
|
|
@ -876,4 +876,3 @@ int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
|
||||||
|
|
||||||
#endif /* HAVE_FIPS */
|
#endif /* HAVE_FIPS */
|
||||||
#endif /* NO_HMAC */
|
#endif /* NO_HMAC */
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,131 @@
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CONFIG_H
|
||||||
|
#include <config.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <wolfssl/wolfcrypt/settings.h>
|
||||||
|
|
||||||
#ifdef WOLFCRYPT_HAVE_SRP
|
#ifdef WOLFCRYPT_HAVE_SRP
|
||||||
|
|
||||||
#include <wolfssl/wolfcrypt/srp.h>
|
#include <wolfssl/wolfcrypt/srp.h>
|
||||||
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||||
|
|
||||||
|
static int SrpHashInit(Srp* srp)
|
||||||
|
{
|
||||||
|
switch (srp->type) {
|
||||||
|
#ifndef NO_SHA
|
||||||
|
case SRP_TYPE_SHA:
|
||||||
|
return wc_InitSha(&srp->hash.sha);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NO_SHA256
|
||||||
|
case SRP_TYPE_SHA256:
|
||||||
|
return wc_InitSha256(&srp->hash.sha256);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA384
|
||||||
|
case SRP_TYPE_SHA384:
|
||||||
|
return wc_InitSha384(&srp->hash.sha384);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA512
|
||||||
|
case SRP_TYPE_SHA512:
|
||||||
|
return wc_InitSha512(&srp->hash.sha512);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
default: return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int SrpHashUpdate(Srp* srp, byte* data, word32 size)
|
||||||
|
{
|
||||||
|
switch (srp->type) {
|
||||||
|
#ifndef NO_SHA
|
||||||
|
case SRP_TYPE_SHA:
|
||||||
|
return wc_ShaUpdate(&srp->hash.sha, data, size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NO_SHA256
|
||||||
|
case SRP_TYPE_SHA256:
|
||||||
|
return wc_Sha256Update(&srp->hash.sha256, data, size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA384
|
||||||
|
case SRP_TYPE_SHA384:
|
||||||
|
return wc_Sha384Update(&srp->hash.sha384, data, size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA512
|
||||||
|
case SRP_TYPE_SHA512:
|
||||||
|
return wc_Sha512Update(&srp->hash.sha512, data, size);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
default: return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int SrpHashFinal(Srp* srp, byte* digest)
|
||||||
|
{
|
||||||
|
switch (srp->type) {
|
||||||
|
#ifndef NO_SHA
|
||||||
|
case SRP_TYPE_SHA:
|
||||||
|
return wc_ShaFinal(&srp->hash.sha, digest);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NO_SHA256
|
||||||
|
case SRP_TYPE_SHA256: return
|
||||||
|
wc_Sha256Final(&srp->hash.sha256, digest);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA384
|
||||||
|
case SRP_TYPE_SHA384: return
|
||||||
|
wc_Sha384Final(&srp->hash.sha384, digest);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SHA512
|
||||||
|
case SRP_TYPE_SHA512: return
|
||||||
|
wc_Sha512Final(&srp->hash.sha512, digest);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
default: return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int wc_SrpInit(Srp* srp, byte type, byte side, byte* N, word32 nSz,
|
||||||
|
byte* g, word32 gSz)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if (!srp || !N || !g)
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
if (side != SRP_CLIENT_SIDE && side != SRP_SERVER_SIDE)
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
srp->side = side;
|
||||||
|
srp->type = type; /* a valid type is checked inside SrpHashXXX functions. */
|
||||||
|
|
||||||
|
if (mp_init_multi(&srp->N, &srp->g, &srp->s, 0, 0, 0) != MP_OKAY)
|
||||||
|
return MP_INIT_E;
|
||||||
|
|
||||||
|
if (mp_read_unsigned_bin(&srp->N, N, nSz) != MP_OKAY)
|
||||||
|
ret = MP_READ_E;
|
||||||
|
|
||||||
|
if (ret == 0 && mp_read_unsigned_bin(&srp->g, g, gSz) != MP_OKAY)
|
||||||
|
ret = MP_READ_E;
|
||||||
|
|
||||||
|
if (ret == 0) ret = SrpHashInit(srp);
|
||||||
|
if (ret == 0) ret = SrpHashUpdate(srp, N, nSz);
|
||||||
|
if (ret == 0) ret = SrpHashUpdate(srp, g, gSz);
|
||||||
|
if (ret == 0) ret = SrpHashFinal(srp, srp->k);
|
||||||
|
|
||||||
|
if (ret != 0) {
|
||||||
|
mp_clear(&srp->N); mp_clear(&srp->g); mp_clear(&srp->s);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* WOLFCRYPT_HAVE_SRP */
|
#endif /* WOLFCRYPT_HAVE_SRP */
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef WOLFCRYPT_HAVE_SRP
|
||||||
|
|
||||||
#ifndef WOLFCRYPT_SRP_H
|
#ifndef WOLFCRYPT_SRP_H
|
||||||
#define WOLFCRYPT_SRP_H
|
#define WOLFCRYPT_SRP_H
|
||||||
|
|
||||||
|
@ -28,20 +30,24 @@
|
||||||
#include <wolfssl/wolfcrypt/sha512.h>
|
#include <wolfssl/wolfcrypt/sha512.h>
|
||||||
#include <wolfssl/wolfcrypt/integer.h>
|
#include <wolfssl/wolfcrypt/integer.h>
|
||||||
|
|
||||||
#ifdef WOLFCRYPT_HAVE_SRP
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
#ifdef NO_SHA
|
SRP_CLIENT_SIDE = 0,
|
||||||
SRP_SHA = 1,
|
SRP_SERVER_SIDE = 1,
|
||||||
|
#ifndef NO_SHA
|
||||||
|
SRP_TYPE_SHA = 1,
|
||||||
#endif
|
#endif
|
||||||
#ifdef NO_SHA256
|
#ifndef NO_SHA256
|
||||||
SRP_SHA256 = 2,
|
SRP_TYPE_SHA256 = 2,
|
||||||
#endif
|
#endif
|
||||||
#ifndef WOLFSSL_SHA384
|
#ifdef WOLFSSL_SHA384
|
||||||
SRP_SHA384 = 3,
|
SRP_TYPE_SHA384 = 3,
|
||||||
#endif
|
#endif
|
||||||
#ifndef WOLFSSL_SHA512
|
#ifdef WOLFSSL_SHA512
|
||||||
SRP_SHA512 = 4,
|
SRP_TYPE_SHA512 = 4,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Select the largest available hash for the buffer size. */
|
/* Select the largest available hash for the buffer size. */
|
||||||
|
@ -58,6 +64,21 @@ enum {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
#ifndef NO_SHA
|
||||||
|
Sha sha;
|
||||||
|
#endif
|
||||||
|
#ifndef NO_SHA256
|
||||||
|
Sha256 sha256;
|
||||||
|
#endif
|
||||||
|
#ifdef WOLFSSL_SHA384
|
||||||
|
Sha384 sha384;
|
||||||
|
#endif
|
||||||
|
#ifdef WOLFSSL_SHA512
|
||||||
|
Sha512 sha512;
|
||||||
|
#endif
|
||||||
|
} SrpHash;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
mp_int a; /**< Private ephemeral value. Random. */
|
mp_int a; /**< Private ephemeral value. Random. */
|
||||||
mp_int A; /**< Public ephemeral value. pow(g, a, N) */
|
mp_int A; /**< Public ephemeral value. pow(g, a, N) */
|
||||||
|
@ -90,9 +111,15 @@ typedef struct {
|
||||||
SrpClient client;
|
SrpClient client;
|
||||||
SrpServer server;
|
SrpServer server;
|
||||||
} specific;
|
} specific;
|
||||||
|
SrpHash hash; /**< Hash object. */
|
||||||
} Srp;
|
} Srp;
|
||||||
|
|
||||||
int SrpInit(Srp* srp, byte* N, word32 nSz, byte* g, word32 gSz);
|
WOLFSSL_API int wc_SrpInit(Srp* srp, byte type, byte side, byte* N, word32 nSz,
|
||||||
|
byte* g, word32 gSz);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} /* extern "C" */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* WOLFCRYPT_SRP_H */
|
||||||
#endif /* WOLFCRYPT_HAVE_SRP */
|
#endif /* WOLFCRYPT_HAVE_SRP */
|
||||||
#endif /* WOLF_CRYPT_SRP_H */
|
|
||||||
|
|
Loading…
Reference in New Issue