diff --git a/.gitignore b/.gitignore index c323a97e..44c0f0c4 100644 --- a/.gitignore +++ b/.gitignore @@ -175,6 +175,8 @@ pk/ecc/*.x963 pk/ED25519/gen_key_files pk/ED25519/sign_and_verify pk/ecdh_generate_secret/ecdh_gen_secret +pk/rsa-kg/rsa-kg-sv +pk/dh-pg/dh-pg-ka wolfCLU/tests/somejunk/*somejunk*.txt diff --git a/pk/dh-pg/Makefile b/pk/dh-pg/Makefile new file mode 100644 index 00000000..4a9dfcb4 --- /dev/null +++ b/pk/dh-pg/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-Wall +LIBS= -lwolfssl -lm + +all: dh-pg-ka + +dh-pg-ka.o: dh-pg-ka.c dh-params.h + $(CC) -c -o $@ $< $(CFLAGS) + +dh-pg-ka: dh-pg-ka.o + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +.PHONY: clean + +clean: + rm -f *.o dh-pg-ka diff --git a/pk/dh-pg/README b/pk/dh-pg/README new file mode 100644 index 00000000..78ac813d --- /dev/null +++ b/pk/dh-pg/README @@ -0,0 +1,23 @@ +How to use dh-pg-ka.c + +1) a. Compile wolfSSL with ./configure --enable-keygen, run + 'make', and then install by typing 'sudo make install'. + NOTE: optionally compile with: + C_EXTRA_FLAGS="-DHAVE_FFDHE_2048 -DHAVE_FFDHE_3072 -DHAVE_FFDHE_4096" + b. In the pk/dh-pg directory run the Makefile by typing 'make'. +2) run the executable, for help run with -help. Basic command is as follows: + + ./dh-pg-ka + + Parameter sizes supported, in bits: 2048, 3072, 4096. + + With option -ffdhe, use the pre-defined standardized FFDHE parameters. + NOTE: no parameters will be generated. + With option -load, use the parameters in: dh-param.h + NOTE: on error, the parameters will be displayed so that they can be + copied into dh-params.h replacing the existing values. + Note: 4096-bit parameters cannot be generated - not supported by wolfSSL. + +4) Running 'make clean' will delete the executable and object files. + + diff --git a/pk/dh-pg/dh-params.h b/pk/dh-pg/dh-params.h new file mode 100644 index 00000000..92dbbe34 --- /dev/null +++ b/pk/dh-pg/dh-params.h @@ -0,0 +1,96 @@ +/* dh-params.h + * + * Copyright (C) 2006-2019 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 + */ + +static unsigned char dh_p[256] = { + 0xca, 0x6f, 0xef, 0x16, 0xb0, 0x23, 0x49, 0x4a, + 0x80, 0x73, 0x3e, 0xea, 0xbe, 0x4a, 0x32, 0x4d, + 0x84, 0x89, 0x76, 0xd6, 0xba, 0xaa, 0x9e, 0x14, + 0x0a, 0xe2, 0x04, 0xd9, 0xdf, 0x82, 0x71, 0xe3, + 0xcb, 0xdf, 0x3d, 0x8e, 0x0f, 0x2f, 0xc7, 0xe4, + 0x8f, 0x5f, 0x9b, 0x9d, 0xa8, 0x53, 0x8d, 0x4b, + 0x2f, 0x07, 0xe2, 0x74, 0x9e, 0x69, 0xff, 0x76, + 0xfb, 0x84, 0x04, 0xa3, 0x7a, 0x08, 0x45, 0x01, + 0x66, 0x68, 0xd6, 0x70, 0x41, 0x9b, 0x5d, 0x74, + 0xa8, 0x13, 0x17, 0x6b, 0x28, 0x6e, 0x88, 0x04, + 0xfe, 0x60, 0xc3, 0xb2, 0xf4, 0x79, 0x6a, 0xba, + 0xcf, 0x55, 0xab, 0x36, 0xd6, 0xa7, 0xb6, 0x92, + 0x7e, 0x2a, 0x02, 0x60, 0x55, 0xd5, 0xf5, 0xf2, + 0x8c, 0x68, 0xd0, 0xf2, 0x0a, 0xe0, 0xa4, 0xfd, + 0xca, 0x52, 0xb2, 0xc7, 0x10, 0x51, 0x8e, 0x74, + 0x80, 0xea, 0xc1, 0xd5, 0xc5, 0xf8, 0x7c, 0xf9, + 0x89, 0x1e, 0xf8, 0x3c, 0xcd, 0xd6, 0xaf, 0x46, + 0x56, 0x0c, 0xf7, 0xee, 0x0f, 0x09, 0xa1, 0x01, + 0x24, 0x35, 0x39, 0x06, 0x87, 0x90, 0xd3, 0x6c, + 0x69, 0x77, 0x1a, 0xe0, 0x35, 0x28, 0xdf, 0x46, + 0xc1, 0xda, 0xe6, 0x2c, 0x4b, 0xff, 0xf5, 0x05, + 0x1b, 0x58, 0x2a, 0x41, 0x77, 0xd1, 0xa9, 0x8c, + 0x4a, 0x37, 0x90, 0x67, 0xbb, 0xeb, 0x0f, 0x3e, + 0x4c, 0x42, 0xff, 0xce, 0xf8, 0xad, 0x49, 0x2f, + 0x6c, 0x2e, 0xcd, 0x7f, 0xd5, 0x07, 0x29, 0xe9, + 0xf3, 0xa3, 0x3c, 0xeb, 0x54, 0x75, 0x71, 0x20, + 0xb1, 0xea, 0x8d, 0x61, 0x9b, 0x53, 0x2b, 0xa6, + 0xf0, 0x08, 0xab, 0xca, 0x32, 0xa0, 0x8a, 0xd0, + 0x24, 0x59, 0xb5, 0x23, 0xd8, 0x42, 0x95, 0xf4, + 0xf6, 0xfc, 0x50, 0xb1, 0x55, 0x01, 0xef, 0x5d, + 0x0d, 0xa8, 0xfe, 0xcb, 0xee, 0x97, 0xcf, 0xc8, + 0x9f, 0x36, 0xa1, 0x6c, 0xbb, 0x17, 0x4f, 0x9d, +}; +static unsigned char dh_g[256] = { + 0xa0, 0x60, 0xb7, 0x80, 0x4a, 0x40, 0x0f, 0x64, + 0x4a, 0xd3, 0xf7, 0xb4, 0x14, 0xd3, 0x04, 0x1f, + 0x6c, 0x45, 0x9a, 0xd6, 0x17, 0x57, 0x35, 0xce, + 0x13, 0xfe, 0x02, 0x9c, 0xd2, 0x8f, 0xb6, 0x5f, + 0x72, 0x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, 0x04, 0xc7, + 0x48, 0x9f, 0xa2, 0xa5, 0xf8, 0xaa, 0xc1, 0x13, + 0x8f, 0xab, 0x10, 0x72, 0x43, 0x2f, 0x58, 0x45, + 0xbf, 0x43, 0xef, 0xb4, 0x85, 0x77, 0x62, 0x4f, + 0x28, 0xc8, 0x2d, 0xfd, 0x47, 0x9e, 0x9a, 0x33, + 0x84, 0xe0, 0x36, 0x95, 0xf1, 0x56, 0x59, 0x12, +}; +static unsigned char dh_q[32] = { + 0xde, 0x31, 0xf0, 0xd0, 0x3e, 0x4a, 0x2e, 0xea, + 0xe4, 0x92, 0x2b, 0x7b, 0xf9, 0x9f, 0x02, 0xbd, + 0xde, 0x06, 0x1f, 0xe6, 0xab, 0x0c, 0xee, 0x34, + 0x00, 0x0c, 0x82, 0x81, 0x0b, 0xde, 0x03, 0x27, +}; + diff --git a/pk/dh-pg/dh-pg-ka.c b/pk/dh-pg/dh-pg-ka.c new file mode 100644 index 00000000..07026273 --- /dev/null +++ b/pk/dh-pg/dh-pg-ka.c @@ -0,0 +1,441 @@ +/* dh-pg-ka.c + * + * Copyright (C) 2006-2019 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 + */ + + +#include +#include +#include + +#include "dh-params.h" + +#define MAX_DH_BITS 4096 +#define MAX_DH_Q_SIZE 256 +#define DEF_DH_SIZE 2048 +#define DEF_KA_CHECKS 512 +#define DEF_PARAMS_GEN 8 + +int load_dh_params(DhKey* key1, DhKey* key2, WC_RNG* rng) +{ + int ret; + + ret = wc_DhSetCheckKey(key1, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g), + dh_q, sizeof(dh_q), 0, rng); + if (ret == 0) { + ret = wc_DhSetCheckKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g), + NULL, 0, 0, rng); + } + + return ret; +} + +/* Print the buffer as bytes */ +void print_data(char *name, unsigned char *data, int len) +{ + int i; + printf("static unsigned char %s[%d] = {\n", name, len); + for (i = 0; i < len; i++) { + if ((i & 7) == 0) { + printf(" "); + } + printf("0x%02x, ", data[i]); + if ((i & 7) == 7) { + printf("\n"); + } + } + if ((i & 7) != 0) { + printf("\n"); + } + printf("};\n"); +} + +/* Print the DH parameters */ +void print_dh(DhKey *key) +{ + int ret; + static unsigned char p[MAX_DH_BITS/8]; + static unsigned char g[MAX_DH_BITS/8]; + static unsigned char q[MAX_DH_Q_SIZE/8]; + word32 p_len, g_len, q_len; + + /* Export the DH parameters */ + p_len = sizeof(p); + g_len = sizeof(g); + q_len = sizeof(q); + ret = wc_DhExportParamsRaw(key, p, &p_len, q, &q_len, g, &g_len); + if (ret != 0) { + fprintf(stderr, "Failed to export parameters\n"); + return; + } + + /* Print out parameters. */ + printf("\n"); + print_data("dh_p", p, p_len); + print_data("dh_g", g, g_len); + print_data("dh_q", q, q_len); +} + +/* Show usage information */ +void usage() +{ + fprintf(stderr, "dh-pg-ka :\n"); + fprintf(stderr, " -load Load the parameters\n"); + fprintf(stderr, " -bits Size in bits of RSA keys\n"); + fprintf(stderr, " -num-gen Number of params to generate\n"); + fprintf(stderr, " -checks Number of key exchanges to do\n"); + fprintf(stderr, " -ffdhe Used pre-defined FFDHE params\n"); + fprintf(stderr, "\n"); +} + +int main(int argc, char *argv[]) +{ + int ec = 0; + int ret; + static DhKey key1, key2; + const DhParams *dhparams; + WC_RNG rng; + static unsigned char p[MAX_DH_BITS/8]; + static unsigned char g[MAX_DH_BITS/8]; + static unsigned char q[MAX_DH_Q_SIZE/8]; + word32 p_len, g_len, q_len; + static unsigned char priv1[MAX_DH_BITS/8]; + static unsigned char priv2[MAX_DH_BITS/8]; + word32 priv1_len, priv2_len; + static unsigned char pub1[MAX_DH_BITS/8]; + static unsigned char pub2[MAX_DH_BITS/8]; + word32 pub1_len, pub2_len; + static unsigned char secret1[MAX_DH_BITS/8]; + static unsigned char secret2[MAX_DH_BITS/8]; + word32 secret1_len, secret2_len; + int i, cnt; + int bits = DEF_DH_SIZE; + int numParams = DEF_PARAMS_GEN; + int checks = DEF_KA_CHECKS; + int gen_params = 1; + int load_params = 0; + + /* Skip the program name */ + --argc; + ++argv; + + /* Process the command line arguments */ + while (argc > 0) { + /* Load the parameters from dh-params.h */ + if (XSTRNCMP(*argv, "-load", 7) == 0) { + load_params = 1; + gen_params = 0; + } + /* Number of bits in DH parameters to generate */ + else if (XSTRNCMP(*argv, "-bits", 6) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing bits value\n"); + usage(); + return 1; + } + bits = atoi(*argv); + } + /* Number of DH parameters to generate */ + else if (XSTRNCMP(*argv, "-num-gen", 9) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing loops value\n"); + usage(); + return 1; + } + numParams = atoi(*argv); + } + /* Number of key agreement checks to perform */ + else if (XSTRNCMP(*argv, "-checks", 7) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing check count value\n"); + usage(); + return 1; + } + checks = atoi(*argv); + } + /* Use the pre-defined FFDHE parameters */ + else if (XSTRNCMP(*argv, "-ffdhe", 7) == 0) { + gen_params = 0; + } + /* Display usage information */ + else if (XSTRNCMP(*argv, "-help", 6) == 0) { + usage(); + return 0; + } + else { + fprintf(stderr, "Unrecognized option: %s\n", *argv); + usage(); + return 1; + } + + --argc; + ++argv; + } + + /* Check bits are valid */ + if (bits != 1024 && bits != 2048 && bits != 3072 && bits != 4096) { + fprintf(stderr, "Bits out of range (1024, 2048, 3072 or 4086): %d\n", + bits); + usage(); + return 1; + } +#ifdef WOLFSSL_SP_MATH + if (0) { + } +#ifndef WOLFSSL_SP_NO_2048 + else if (bits == 2048) { + } +#endif +#ifndef WOLFSSL_SP_NO_3072 + else if (bits == 3072) { + } +#endif +#ifdef WOLFSSL_SP_4096 + else if (bits == 4096) { + } +#endif + else { + fprintf(stderr, "Bit size not supported with SP_MATH: %d\n", bits); + fprintf(stderr, " wolfSSL compiled to support, in bits:"); +#ifndef WOLFSSL_SP_NO_2048 + fprintf(stderr, " 2048"); +#endif +#ifndef WOLFSSL_SP_NO_3072 + fprintf(stderr, " 3072"); +#endif +#ifdef WOLFSSL_SP_4096 + fprintf(stderr, " 4096"); +#endif + fprintf(stderr, "\n"); + return 1; + } +#endif + if (bits > MAX_DH_BITS) { + fprintf(stderr, "Program isn't supporting bit sizes greater than %d\n", + MAX_DH_BITS); + return 1; + } + /* Only dping one set of parameters unless generating */ + if (!gen_params) { + numParams = 1; + } + + if (gen_params) { + /* Check valid bits for DH parameter generation */ + if (bits == 4096) { + fprintf(stderr, "4096-bit parameter generation not supported\n"); + return 1; + } + } + else if (load_params) { + /* Nothing to do */ + } +#ifdef HAVE_FFDHE_2048 + else if (bits == 2048) { + /* Set FFDHE-2048 */ + dhparams = wc_Dh_ffdhe2048_Get(); + } +#endif +#ifdef HAVE_FFDHE_3072 + else if (bits == 3072) { + /* Set FFDHE-3072 */ + dhparams = wc_Dh_ffdhe3072_Get(); + } +#endif +#ifdef HAVE_FFDHE_4096 + else if (bits == 4096) { + /* Set FFDHE-4096 */ + dhparams = wc_Dh_ffdhe4096_Get(); + } +#endif + else { + /* Set FFDHE size not supported */ + fprintf(stderr, "Unsupported FFDHE parameters: %d\n", bits); + return 1; + } + + /* Display the options to use */ + if (gen_params) { + printf("Bits: %d\n", bits); + printf("#Params: %d\n", numParams); + } + else if (!load_params) { + printf("FFHDE: %d\n", bits); + } + else { + printf("Load parameters\n"); + } + printf("Checks: %d\n", checks); + + /* Initialise a random number generator for generation */ + ret = wc_InitRng(&rng); + if (ret != 0) { + fprintf(stderr, "Failed to initialize random\n"); + return 1; + } + + /* Initialise DH key */ + ret = wc_InitDhKey(&key1); + if (ret != 0) { + wc_FreeRng(&rng); + fprintf(stderr, "Failed to initialize DH key\n"); + return 1; + } + /* Initialise DH key for key agreement */ + ret = wc_InitDhKey(&key2); + if (ret != 0) { + wc_FreeDhKey(&key1); + wc_FreeRng(&rng); + fprintf(stderr, "Failed to initialize DH key\n"); + return 1; + } + + /* Perform operations for specified number of parameters */ + for (cnt = 0; cnt < numParams; cnt++) { + if (gen_params) { + fprintf(stderr, "%d: ", cnt + 1); + + /* Generate a set of DH parameters */ + ret = wc_DhGenerateParams(&rng, bits, &key1); + if (ret != 0) { + fprintf(stderr, "Failed to generate DH params\n"); + fprintf(stderr, "%d\n", ret); + ec = 1; + break; + } + + /* Export DH parameters */ + p_len = sizeof(p); + q_len = sizeof(q); + g_len = sizeof(g); + ret = wc_DhExportParamsRaw(&key1, p, &p_len, q, &q_len, g, &g_len); + if (ret != 0) { + fprintf(stderr, "Failed to export DH params\n"); + ec = 1; + break; + } + + /* Set only p and g and check key */ + ret = wc_DhSetCheckKey(&key2, p, p_len, g, g_len, NULL, 0, 0, &rng); + if (ret != 0) { + fprintf(stderr, "Failed to set/check DH params\n"); + ec = 1; + break; + } + } + else if (!load_params) { + /* Set p and g - trust key */ + ret = wc_DhSetCheckKey(&key2, dhparams->p, dhparams->p_len, + dhparams->g, dhparams->g_len, NULL, 0, 1, + &rng); + if (ret != 0) { + fprintf(stderr, "Failed to set/check DH params\n"); + ec = 1; + break; + } + } + else { + /* Set p and g - trust key */ + ret = load_dh_params(&key1, &key2, &rng); + if (ret != 0) { + fprintf(stderr, "Failed to load DH params\n"); + ec = 1; + break; + } + } + + /* Perform a number of key agreements with parameters */ + for (i = 0; i < checks; i++) { + /* Generate first peer's key */ + priv1_len = sizeof(priv1); + pub1_len = sizeof(pub1); + ret = wc_DhGenerateKeyPair(&key2, &rng, priv1, &priv1_len, pub1, + &pub1_len); + if (ret != 0) { + fprintf(stderr, "Failed to generate key pair\n"); + print_dh(&key1); + ec = 1; + break; + } + + /* Generate second peer's key */ + priv2_len = sizeof(priv2); + pub2_len = sizeof(pub2); + ret = wc_DhGenerateKeyPair(&key2, &rng, priv2, &priv2_len, pub2, + &pub2_len); + if (ret != 0) { + fprintf(stderr, "Failed to generate key pair\n"); + print_dh(&key1); + ec = 1; + break; + } + + /* Calculate first peer's secret */ + secret1_len = sizeof(secret1); + ret = wc_DhAgree(&key2, secret1, &secret1_len, priv1, priv1_len, + pub2, pub2_len); + if (ret != 0) { + fprintf(stderr, "Failed to calculate secret\n"); + print_dh(&key1); + ec = 1; + break; + } + + /* Calculate second peer's secret */ + secret2_len = sizeof(secret2); + ret = wc_DhAgree(&key2, secret2, &secret2_len, priv2, priv2_len, + pub1, pub1_len); + if (ret != 0) { + fprintf(stderr, "Failed to calculate secret\n"); + print_dh(&key1); + ec = 1; + break; + } + + /* Secret's should be the same */ + if ((secret1_len != secret2_len) || (XMEMCMP(secret1, secret2, + secret1_len) != 0)) { + fprintf(stderr, "Secrets different\n"); + print_dh(&key1); + ec = 1; + break; + } + + fprintf(stderr, "."); + } + + /* Error during key generation or key agreement */ + if (ec) { + break; + } + + fprintf(stderr, "\n"); + } + + /* Free allocated items */ + wc_FreeDhKey(&key1); + wc_FreeDhKey(&key1); + wc_FreeRng(&rng); + + return 0; +} + diff --git a/pk/rsa-kg/Makefile b/pk/rsa-kg/Makefile new file mode 100644 index 00000000..5e0a9874 --- /dev/null +++ b/pk/rsa-kg/Makefile @@ -0,0 +1,16 @@ +CC=gcc +CFLAGS=-Wall +LIBS= -lwolfssl -lm + +all: rsa-kg-sv + +rsa-kg-sv.o: rsa-kg-sv.c rsa-key.h + $(CC) -c -o $@ $< $(CFLAGS) + +rsa-kg-sv: rsa-kg-sv.o + $(CC) -o $@ $^ $(CFLAGS) $(LIBS) + +.PHONY: clean + +clean: + rm -f *.o rsa-kg-sv diff --git a/pk/rsa-kg/README b/pk/rsa-kg/README new file mode 100644 index 00000000..19dbe6ea --- /dev/null +++ b/pk/rsa-kg/README @@ -0,0 +1,18 @@ +How to use rsa-kg-sv.c + +1) a. Compile wolfSSL with ./configure --enable-keygen, run + 'make', and then install by typing 'sudo make install'. + b. In the pk/rsa-kg directory run the Makefile by typing 'make'. +2) run the executable, for help run with -help. Basic command is as follows: + + ./rsa-kg-sv + + Key sizes supported, in bits: 1024-4096. + + With option -load-key, use the key in: rsa-key.h + NOTE: on error, the key and digest will be displayed so that they can be + copied into rsa-key.h replacing the existing values. + +4) Running 'make clean' will delete the executable and object files. + + diff --git a/pk/rsa-kg/rsa-key.h b/pk/rsa-kg/rsa-key.h new file mode 100644 index 00000000..cad7cfcd --- /dev/null +++ b/pk/rsa-kg/rsa-key.h @@ -0,0 +1,184 @@ +/* rsa-key.h + * + * Copyright (C) 2006-2019 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 + */ + +static unsigned char rsa_priv_key[1191] = { + 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, + 0x82, 0x01, 0x01, 0x00, 0xc8, 0xb0, 0xb8, 0x68, + 0x56, 0x15, 0xea, 0x1c, 0x38, 0x55, 0xb1, 0x4f, + 0xc4, 0x72, 0x13, 0xc3, 0x75, 0xb8, 0x4e, 0xf4, + 0x11, 0x3e, 0xcd, 0x89, 0xbf, 0x79, 0x1b, 0x5d, + 0x3c, 0xdd, 0x09, 0x8d, 0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x14, 0x1a, 0xa2, 0x17, 0x13, 0xd8, 0x99, 0xae, + 0x02, 0x3f, 0x30, 0x22, 0x29, 0xb9, 0x01, 0xe7, + 0xfd, 0xf8, 0x08, 0x1c, 0x43, 0xd2, 0xc4, 0x74, + 0x48, 0xcb, 0x9d, 0x66, 0x4a, 0x04, 0xb9, 0x45, + 0xd5, 0x5c, 0x74, 0xc2, 0x34, 0xf3, 0xc6, 0xc6, + 0xc3, 0x76, 0xd4, 0x8f, 0x02, 0x03, 0x01, 0x00, + 0x01, 0x02, 0x82, 0x01, 0x00, 0x1b, 0x90, 0x10, + 0x80, 0xf2, 0x93, 0x1c, 0xed, 0x96, 0x46, 0x72, + 0xa2, 0x92, 0xc7, 0x86, 0x1e, 0x59, 0xfc, 0x75, + 0x0a, 0x2b, 0xea, 0x2a, 0x5e, 0xc8, 0xf2, 0x25, + 0x9c, 0xcc, 0x69, 0xf6, 0x7b, 0x57, 0xc7, 0x06, + 0x1f, 0x13, 0xbb, 0x60, 0xdf, 0x80, 0x44, 0x63, + 0x15, 0xb6, 0x90, 0xb4, 0x2a, 0xa6, 0x95, 0x48, + 0xd8, 0xf8, 0x95, 0xb2, 0xe0, 0x60, 0xd7, 0xf6, + 0xe6, 0xc7, 0xca, 0xb1, 0x44, 0x09, 0x0d, 0xf0, + 0x47, 0x02, 0x2e, 0xae, 0x4a, 0x35, 0xb7, 0xe4, + 0x19, 0xef, 0xd7, 0xfb, 0x41, 0x67, 0x81, 0x5b, + 0xad, 0x43, 0x49, 0xfc, 0x18, 0xca, 0x54, 0xb9, + 0x55, 0x6c, 0xa8, 0x43, 0xbc, 0xef, 0x8c, 0x84, + 0xdb, 0xd9, 0x2f, 0xdc, 0xe7, 0x6b, 0xc9, 0x08, + 0xcb, 0x2e, 0x42, 0x0f, 0xa7, 0xf4, 0x68, 0xa6, + 0x41, 0xfd, 0x64, 0x49, 0x4e, 0x17, 0x2e, 0x2f, + 0xa2, 0x9d, 0xbc, 0x24, 0x14, 0xae, 0xf3, 0x70, + 0xd7, 0x34, 0x4b, 0xaa, 0x9a, 0xb9, 0xe8, 0x60, + 0x93, 0x77, 0xab, 0x11, 0xfa, 0xaf, 0x22, 0x00, + 0x5f, 0xff, 0x2f, 0x18, 0x2f, 0x53, 0xef, 0x51, + 0x8c, 0x9e, 0xbb, 0x76, 0x72, 0xae, 0xe7, 0x95, + 0x8a, 0x90, 0xfe, 0x56, 0xdc, 0xd8, 0x30, 0xb9, + 0xd3, 0x1c, 0x2f, 0x8f, 0xf5, 0x71, 0x93, 0xd5, + 0x91, 0xe4, 0x10, 0x8c, 0x8c, 0xcb, 0x9e, 0x51, + 0x8a, 0xdb, 0x4e, 0x15, 0xc1, 0xd5, 0x52, 0x73, + 0xb3, 0x60, 0x13, 0x47, 0xfb, 0x6d, 0x26, 0x3a, + 0xba, 0xa3, 0x55, 0x59, 0xae, 0x27, 0x24, 0x36, + 0x47, 0xed, 0x34, 0x48, 0x72, 0x13, 0xa1, 0x66, + 0x6b, 0x81, 0x8b, 0x09, 0x8d, 0x56, 0x76, 0xbf, + 0x92, 0x62, 0x97, 0xa6, 0x22, 0x9a, 0x09, 0x44, + 0xca, 0x04, 0x4d, 0x82, 0x90, 0x07, 0x6b, 0xb0, + 0x0d, 0xdb, 0xf1, 0x0f, 0xe4, 0xef, 0x18, 0x15, + 0x40, 0x9f, 0x83, 0x7f, 0xf1, 0x02, 0x81, 0x81, + 0x00, 0xf3, 0xb4, 0xa5, 0x0a, 0x8d, 0x3f, 0x6b, + 0x35, 0x22, 0x88, 0x82, 0xc1, 0x86, 0x0b, 0x26, + 0x3b, 0xec, 0x93, 0x36, 0x6c, 0x46, 0x5c, 0x80, + 0x49, 0xc7, 0xa4, 0xf3, 0x62, 0xbf, 0xdd, 0x75, + 0xbb, 0x00, 0x52, 0x62, 0xa9, 0x20, 0xbb, 0x7a, + 0xa5, 0xff, 0x40, 0x84, 0x7e, 0x95, 0x08, 0xb9, + 0x13, 0x74, 0x87, 0x76, 0xb6, 0x08, 0x65, 0xbd, + 0xb6, 0xcc, 0xa1, 0xe7, 0xb8, 0xec, 0x0a, 0x24, + 0xed, 0x9b, 0xac, 0x00, 0xdf, 0x41, 0x83, 0x85, + 0x7a, 0x31, 0x40, 0x5e, 0xe6, 0x4d, 0xdc, 0xf8, + 0xa5, 0x47, 0x83, 0xe1, 0x9b, 0x43, 0x87, 0x40, + 0xe9, 0xd1, 0x77, 0x11, 0x7f, 0xe3, 0x86, 0xc1, + 0x75, 0x8a, 0xda, 0x8c, 0x11, 0x32, 0x0a, 0xdf, + 0xdb, 0x59, 0xcb, 0xd1, 0x4c, 0x6f, 0xd2, 0x7f, + 0x49, 0x37, 0x2e, 0x96, 0x38, 0x67, 0x65, 0x53, + 0x12, 0x06, 0x35, 0xbe, 0x41, 0xa7, 0xac, 0x91, + 0x71, 0x02, 0x81, 0x81, 0x00, 0xd2, 0xd0, 0x8d, + 0x05, 0x58, 0x76, 0xb2, 0x31, 0xee, 0x3d, 0x78, + 0x0b, 0xdc, 0x23, 0x5f, 0x48, 0x37, 0x66, 0xb2, + 0x12, 0xd7, 0xf4, 0x0a, 0x0c, 0x94, 0xf3, 0xb5, + 0x4d, 0x65, 0x7f, 0xdd, 0xc9, 0x83, 0x98, 0x56, + 0xff, 0x49, 0xf7, 0xa4, 0x8a, 0xef, 0x68, 0x04, + 0xbe, 0xe9, 0x52, 0x48, 0x7a, 0x3a, 0xbd, 0xda, + 0x91, 0xb5, 0x92, 0xc9, 0x30, 0x83, 0xab, 0xc9, + 0x60, 0x29, 0xbc, 0x0d, 0xb7, 0x99, 0x48, 0x57, + 0x7d, 0xca, 0x26, 0x9c, 0xca, 0x41, 0x7e, 0x67, + 0x67, 0x8d, 0x26, 0xf5, 0x6c, 0x89, 0xa8, 0x99, + 0xcf, 0x7c, 0x83, 0xc3, 0xf9, 0x31, 0xc4, 0x52, + 0xb9, 0xf8, 0x2d, 0x02, 0x02, 0xdc, 0x45, 0xbc, + 0x7a, 0xdf, 0x6a, 0x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 0x5e, 0xd4, + 0xf4, 0x95, 0xc5, 0x73, 0xda, 0x5a, 0x9e, 0xe7, + 0x56, 0x49, 0xa8, 0x36, 0x1a, 0xe5, 0x86, 0x66, + 0xc8, 0x93, 0x95, 0x3e, 0x4e, 0xf2, 0x6b, 0x04, + 0x36, 0x24, 0x3e, 0x32, 0x6a, 0x26, 0x36, 0x2b, + 0x52, 0x1d, 0x49, 0x6b, 0x58, 0xf9, 0xe4, 0x43, + 0x19, 0xb5, 0xb6, 0x36, 0x3c, 0xfc, 0x96, 0x2e, + 0x8b, 0xfd, 0x9b, 0x7b, 0xf6, 0x6b, 0x7e, 0xfb, + 0x76, 0x4d, 0x00, 0xaa, 0xd4, 0x64, 0x59, 0xce, + 0x04, 0x80, 0xbc, 0x49, 0x81, 0x16, 0x9c, 0x0b, + 0x50, 0x57, 0x0c, 0x45, 0xc7, 0x7e, 0x2e, 0xee, + 0xe8, 0xbc, 0xb1, 0xa0, 0x1d, 0x28, 0xf3, 0x47, + 0x40, 0x66, 0x90, 0x01, 0xa7, 0xdf, 0x16, 0x59, + 0xfe, 0xa3, 0xa3, 0x21, 0xf1, 0x1d, 0x33, 0xa5, + 0x34, 0x6e, 0x6f, 0x02, 0x81, 0x81, 0x00, 0xd6, + 0x34, 0x8e, 0xd4, 0xcd, 0xf5, 0x5e, 0x54, 0x15, + 0xa4, 0x57, 0x6c, 0xe9, 0x2e, 0x0e, 0x88, 0xda, + 0xcc, 0x19, 0xfc, 0x89, 0xb4, 0x36, 0x17, 0x1b, + 0xd8, 0x72, 0x17, 0x1e, 0xa2, 0x22, 0xff, 0xfc, + 0x56, 0xb9, 0xc6, 0x39, 0x56, 0x52, 0xae, 0xd5, + 0xdd, 0x3a, 0x1f, 0x9e, 0xa2, 0xf0, 0x90, 0x67, + 0xd1, 0x3f, 0x97, 0x82, 0xb6, 0x7d, 0x33, 0x73, + 0xf8, 0xf0, 0x06, 0xc2, 0xe5, 0x98, 0x0c, 0x2a, + 0xc9, 0x34, 0x16, 0x13, 0x03, 0x6e, 0x3c, 0xa4, + 0x84, 0x52, 0x2a, 0x52, 0x27, 0x63, 0x80, 0x81, + 0x8f, 0xaf, 0x57, 0x9e, 0x00, 0xaf, 0xbb, 0x39, + 0x56, 0x55, 0xf6, 0xb2, 0x2c, 0x77, 0x5d, 0xd1, + 0x0c, 0xe6, 0x4a, 0x5f, 0x8d, 0xfb, 0x01, 0xcb, + 0xca, 0x4f, 0xf9, 0x51, 0xbf, 0x76, 0xea, 0xc3, + 0xba, 0x3d, 0x0b, 0x40, 0xc2, 0xc6, 0xed, 0x61, + 0x13, 0xa2, 0xff, 0x7c, 0x7f, 0xeb, 0x99, +}; +static unsigned char digest[64] = { + 0x81, 0x9e, 0x38, 0x5a, 0x31, 0xc9, 0xda, 0xfb, + 0x69, 0xc4, 0xd3, 0x68, 0xdc, 0x2e, 0x81, 0x66, + 0x56, 0x3d, 0x93, 0x18, 0xd3, 0x04, 0xc2, 0x86, + 0x54, 0x9b, 0x82, 0x9e, 0x48, 0x7f, 0xe3, 0x74, + 0xea, 0xc3, 0x49, 0x40, 0x9c, 0x23, 0x47, 0x3d, + 0x12, 0xfb, 0x82, 0x7c, 0x5a, 0x00, 0x99, 0x08, + 0x50, 0xce, 0xda, 0xc3, 0x40, 0x18, 0x61, 0x17, + 0x31, 0x3f, 0x3d, 0xe8, 0x9c, 0x45, 0x2d, 0x1e, +}; +static int digestSz = 64; + diff --git a/pk/rsa-kg/rsa-kg-sv.c b/pk/rsa-kg/rsa-kg-sv.c new file mode 100644 index 00000000..48fe994d --- /dev/null +++ b/pk/rsa-kg/rsa-kg-sv.c @@ -0,0 +1,370 @@ +/* rsa-kg-sv.c + * + * Copyright (C) 2006-2019 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 + */ + + +#include +#include +#include +#include + +#include "rsa-key.h" + +#define MIN_RSA_BITS 1024 +#define MAX_RSA_BITS 4096 +#define DEF_RSA_BITS 2048 +#define DEF_KEYS_GEN 128 +#define DEF_SV_CHECKS 512 + +/* Load the RSA private key */ +int load_rsa_key(RsaKey *key) +{ + word32 idx = 0; + return wc_RsaPrivateKeyDecode(rsa_priv_key, &idx, key, + sizeof(rsa_priv_key)); +} + +/* Print the buffer as bytes */ +void print_data(char *name, unsigned char *data, int len) +{ + int i; + printf("static unsigned char %s[%d] = {\n", name, len); + for (i = 0; i < len; i++) { + if ((i & 7) == 0) { + printf(" "); + } + printf("0x%02x, ", data[i]); + if ((i & 7) == 7) { + printf("\n"); + } + } + if ((i & 7) != 0) { + printf("\n"); + } + printf("};\n"); +} + +/* Print the components of an RSA key */ +void print_rsa(RsaKey *key) +{ + printf("\n"); + unsigned char buf[2400]; + int len; + + len = wc_RsaKeyToDer(key, buf, sizeof(buf)); + if (len < 0) { + fprintf(stderr, "Failed to encode RSA key\n"); + } + + print_data("rsa_priv_key", buf, len); +} + +/* Shows usage information */ +void usage() +{ + fprintf(stderr, "rsa_kg_sv :\n"); + fprintf(stderr, " -load-key Load RSA key from rsa-key.h\n"); + fprintf(stderr, " -bits Size in bits of RSA keys generated\n"); + fprintf(stderr, " Range: 1024-4096\n"); + fprintf(stderr, " -num-keys Number of keys to generate\n"); + fprintf(stderr, " -checks Number of signs/verifies to do\n"); + fprintf(stderr, " -hash-size Number of bytes in hash\n"); + fprintf(stderr, " Range: 1-64. Default: 64\n"); + fprintf(stderr, "\n"); +} + +int main(int argc, char *argv[]) +{ + int ec = 0; + int ret; + RsaKey key; + WC_RNG rng; + unsigned char hash[SHA512_DIGEST_SIZE]; + int hashSz = SHA512_DIGEST_SIZE; + int hashAlg = WC_HASH_TYPE_SHA512; + unsigned char sig[MAX_RSA_BITS/8]; + int sig_len; + unsigned char *out = NULL; + int i, cnt; + int bits = DEF_RSA_BITS; + int numKeys = DEF_KEYS_GEN; + int checks = DEF_SV_CHECKS; + int load_key = 0; + + /* Skip program name */ + --argc; + ++argv; + /* Process command line arguments */ + while (argc > 0) { + /* Load the key rather than generate */ + if (XSTRNCMP(*argv, "-load-key", 10) == 0) { + load_key = 1; + } + /* Number of bits in RSA key to generate */ + else if (XSTRNCMP(*argv, "-bits", 6) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing bits value\n"); + usage(); + return 1; + } + bits = atoi(*argv); + } + /* Number of keys to generate */ + else if (XSTRNCMP(*argv, "-num-keys", 10) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing number of keys value\n"); + usage(); + return 1; + } + numKeys = atoi(*argv); + } + /* Number of sign/verify checks to perform */ + else if (XSTRNCMP(*argv, "-checks", 7) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing check count value\n"); + usage(); + return 1; + } + checks = atoi(*argv); + } + /* Size of hash to use */ + else if (XSTRNCMP(*argv, "-hash-size", 11) == 0) { + ++argv; + if (--argc == 0) { + fprintf(stderr, "Missing hash size value\n"); + usage(); + return 1; + } + hashSz = atoi(*argv); + } + else if (XSTRNCMP(*argv, "-help", 6) == 0) { + usage(); + return 0; + } + else { + fprintf(stderr, "Unrecognized option: %s\n", *argv); + usage(); + return 1; + } + + --argc; + ++argv; + } + + /* Only use one key when loading */ + if (load_key) { + numKeys = 1; + } + + /* Check bit count if generating keys */ + if (!load_key && (bits < MIN_RSA_BITS || bits > MAX_RSA_BITS)) { + fprintf(stderr, "Bits out of range (%d-%d): %d\n", MIN_RSA_BITS, + MAX_RSA_BITS, bits); + usage(); + return 1; + } +#ifdef WOLFSSL_SP_MATH + if (0) { + } +#ifndef WOLFSSL_SP_NO_2048 + else if (bits == 2048) { + } +#endif +#ifndef WOLFSSL_SP_NO_3072 + else if (bits == 3072) { + } +#endif +#ifdef WOLFSSL_SP_4096 + else if (bits == 4096) { + } +#endif + else { + fprintf(stderr, "Bit size not supported with SP_MATH: %d\n", bits); + fprintf(stderr, " wolfSSL compiled to support, in bits:"); +#ifndef WOLFSSL_SP_NO_2048 + fprintf(stderr, " 2048"); +#endif +#ifndef WOLFSSL_SP_NO_3072 + fprintf(stderr, " 3072"); +#endif +#ifdef WOLFSSL_SP_4096 + fprintf(stderr, " 4096"); +#endif + fprintf(stderr, "\n"); + return 1; + } +#endif + /* Check number of keys */ + if (numKeys < 1) { + fprintf(stderr, "Number of key out of range (1+): %d\n", numKeys); + usage(); + return 1; + } + /* Check count */ + if (checks < 0) { + fprintf(stderr, "Number of checks out of range (0+): %d\n", checks); + usage(); + return 1; + } + /* Check hash size is valid */ + if (hashSz < 1 || hashSz > SHA512_DIGEST_SIZE) { + fprintf(stderr, "Hash size out of range (1-%d): %d\n", + SHA512_DIGEST_SIZE, hashSz); + usage(); + return 1; + } + /* Display the options in use */ + if (!load_key) { + printf("Bits: %d\n", bits); + printf("#Keys: %d\n", numKeys); + } + else { + printf("Loading key\n"); + } + printf("Checks: %d\n", checks); + printf("Hash Size: %d\n", hashSz); + + /* Initialize random number generator */ + ret = wc_InitRng(&rng); + if (ret != 0) { + fprintf(stderr, "Failed to initialize random\n"); + return 1; + } + + /* Initialize RSA key structure */ + ret = wc_InitRsaKey(&key, NULL); + if (ret != 0) { + wc_FreeRng(&rng); + fprintf(stderr, "Failed to initialize RSA key\n"); + return 1; + } + + /* Perform operations for specified number of keys */ + for (cnt = 0; cnt < numKeys; cnt++) { + if (!load_key) { + fprintf(stderr, "%d: ", cnt + 1); + + /* Generate a new RSA key with the default public exponent */ + ret = wc_MakeRsaKey(&key, bits, WC_RSA_EXPONENT, &rng); + if (ret != 0) { + fprintf(stderr, "Failed to make RSA key\n"); + ec = 1; + break; + } + + /* Check the RSA key */ + ret = wc_CheckRsaKey(&key); + if (ret != 0) { + fprintf(stderr, "Key failed checks\n"); + ec = 1; + break; + } + + /* Generate a new hash */ + ret = wc_RNG_GenerateBlock(&rng, hash, sizeof(hash)); + if (ret != 0) { + fprintf(stderr, "Failed to initialize random\n"); + ec = 1; + break; + } + } + else { + /* Load RSA key from rsa-key.h */ + ret = load_rsa_key(&key); + if (ret != 0) { + fprintf(stderr, "Failed to load RSA key\n"); + ec = 1; + break; + } + + /* From rsa-key.h */ + XMEMCPY(hash, digest, digestSz); + } + + /* Perform a number of sign-verify checks */ + for (i = 0; i < checks; i++) { + /* Sign with RSA key */ + ret = wc_RsaSSL_Sign(hash, hashSz, sig, sizeof(sig), &key, &rng); + if (ret < 0) { + print_rsa(&key); + print_data("digest", hash, hashSz); + printf("static int digestSz = %d;\n", hashSz); + fprintf(stderr, "Failed to sign with RSA key\n"); + ec = 1; + break; + } + sig_len = ret; + if (load_key && i == 0) { + print_data("sig", sig, sig_len); + } + + /* Verify with RSA key */ + ret = wc_RsaSSL_VerifyInline(sig, sig_len, &out, &key); + if (ret < 0) { + print_rsa(&key); + print_data("digest", hash, hashSz); + printf("static int digestSz = %d;\n", hashSz); + printf("\n"); + print_data("sig", sig, sig_len); + fprintf(stderr, "Failed to verify with RSA key\n"); + ec = 1; + break; + } + + /* Check hash is the same */ + if ((ret != hashSz) || (XMEMCMP(out, hash, hashSz) != 0)) { + print_rsa(&key); + print_data("digest", hash, hashSz); + printf("static int digestSz = %d;\n", hashSz); + printf("\n"); + print_data("sig", sig, sig_len); + fprintf(stderr, "Failed to verify with RSA key\n"); + ec = 1; + break; + } + + /* Generate a new hash to sign */ + ret = wc_Hash(hashAlg, hash, hashSz, hash, sizeof(hash)); + if (ret != 0) { + fprintf(stderr, "Failed to make new hash\n"); + ec = 1; + break; + } + + fprintf(stderr, "."); + } + + /* Error during check - break out. */ + if (ec) { + break; + } + fprintf(stderr, "\n"); + } + + /* Free allocated items */ + wc_FreeRsaKey(&key); + wc_FreeRng(&rng); + + return ec; +} +