From 2fd78f6d402cc7a488ac7c248e3b5ab6396747c4 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 29 Jul 2020 16:47:56 +0200 Subject: [PATCH 1/3] Added ecc/ecc-export-Qx-Qy example program --- .gitignore | 1 + ecc/Makefile | 2 +- ecc/ecc-export-Qx-Qy.c | 83 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 ecc/ecc-export-Qx-Qy.c diff --git a/.gitignore b/.gitignore index 30918763..e927ae87 100644 --- a/.gitignore +++ b/.gitignore @@ -125,6 +125,7 @@ ecc/ecc-params ecc/ecc-sign ecc/ecc-stack ecc/ecc-verify +ecc/ecc-export-Qx-Qy ecc/*.der ecc/*.pem pkcs7/pkcs7-verify diff --git a/ecc/Makefile b/ecc/Makefile index 94df7053..a6edab2e 100644 --- a/ecc/Makefile +++ b/ecc/Makefile @@ -5,7 +5,7 @@ CFLAGS = -Wall -I$(LIB_PATH)/include LIBS = -L$(LIB_PATH)/lib -lm # option variables -DYN_LIB = -lwolfssl +DYN_LIB = -lwolfssl -pthread STATIC_LIB = $(LIB_PATH)/lib/libwolfssl.a DEBUG_FLAGS = -g -DDEBUG DEBUG_INC_PATHS = -MD diff --git a/ecc/ecc-export-Qx-Qy.c b/ecc/ecc-export-Qx-Qy.c new file mode 100644 index 00000000..f93c3128 --- /dev/null +++ b/ecc/ecc-export-Qx-Qy.c @@ -0,0 +1,83 @@ +/* ecc-export-Qx-Qy.c + * + * Copyright (C) 2006-2020 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * 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-1335, USA + */ + +/* This example exports the public part of a given ECC key as a concatenation of + * (Qx,Qy), in raw format. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_BUF 1024 +#define POINT_SIZE 32 + + +int main(int argc, char *argv[]) +{ + int fd_in, fd_out, sz, ret; + uint8_t der_buf[MAX_BUF]; + uint8_t Qx[POINT_SIZE], Qy[POINT_SIZE]; + uint32_t qxlen = POINT_SIZE, qylen = POINT_SIZE; + word32 idx = 0; + ecc_key ec; + if (argc != 3) { + fprintf(stderr, "Usage: %s der_key_file raw_key_file\n", argv[0]); + exit(1); + } + fd_in = open(argv[1], O_RDONLY); + if (fd_in < 0) { + perror("opening input file"); + exit(2); + } + sz = read(fd_in, der_buf, MAX_BUF); + if (sz < 0) { + perror("read"); + exit(3); + } + close(fd_in); + wc_ecc_init(&ec); + ret = wc_EccPublicKeyDecode(der_buf, &idx, &ec, sz); + if (ret != MP_OKAY) { + fprintf(stderr, "wc_EccPublicKeyDecode: Error %d\n", ret); + exit(4); + } + ret = wc_ecc_export_public_raw(&ec, Qx, &qxlen, Qy, &qylen); + if (ret != MP_OKAY) { + fprintf(stderr, "wc_ecc_export_public_raw: Error %d\n", ret); + exit(4); + } + fd_out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0660); + if (fd_out < 0) { + perror("opening output file"); + exit(5); + } + write(fd_out, Qx, qxlen); + write(fd_out, Qy, qylen); + close(fd_out); + return 0; +} From 965eb84d447ec24b183be480cfe129081d6307e9 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 30 Jul 2020 14:32:36 +0200 Subject: [PATCH 2/3] Fixes after review --- ecc/README.md | 12 ++++++++++++ ecc/ecc-export-Qx-Qy.c | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/ecc/README.md b/ecc/README.md index 8bae11ac..7dd7ab1b 100644 --- a/ecc/README.md +++ b/ecc/README.md @@ -169,6 +169,18 @@ Gy: 32 2b ce 33 57 6b 31 5e ce cb b6 40 68 37 bf 51 f5 |+.3Wk1^...@h7.Q. ``` +### `ecc-export-qx-qy` + +Example for extracting public key parameters from ASN.1 format to raw ECC point. +The raw ECC point format, also used by [wolfBoot](https://github.com/wolfSSL/wolfBoot), represents a public key in its two +coordinates (Qx,Qy) a fixed size (2 x keysize). + +Usage: + +`./ecc-export-Qx-Qy der_key_file raw_key_file` + +Where `der_key_file` is a file containing the ecc key in ASN.1 format, and `raw_key_file` is the output file created, containing the public key in raw ECC point format. + ## Support For questions please email us at support@wolfssl.com. diff --git a/ecc/ecc-export-Qx-Qy.c b/ecc/ecc-export-Qx-Qy.c index f93c3128..2daa49cf 100644 --- a/ecc/ecc-export-Qx-Qy.c +++ b/ecc/ecc-export-Qx-Qy.c @@ -36,6 +36,7 @@ #define MAX_BUF 1024 #define POINT_SIZE 32 +#if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) int main(int argc, char *argv[]) { @@ -81,3 +82,12 @@ int main(int argc, char *argv[]) close(fd_out); return 0; } + +#else + +int main(void) +{ + printf("Not compiled in: Build wolfSSL with `./configure --enable-ecc or `HAVE_ECC`, `HAVE_ECC_KEY_EXPORT` and `HAVE_ECC_KEY_IMPORT`\n"); + return 0; +} +#endif From e2c20021bb9d26bdfb3cc32ba44e28da4b8644af Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Thu, 30 Jul 2020 14:35:55 +0200 Subject: [PATCH 3/3] Typo --- ecc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecc/README.md b/ecc/README.md index 7dd7ab1b..20800e24 100644 --- a/ecc/README.md +++ b/ecc/README.md @@ -173,7 +173,7 @@ Gy: 32 Example for extracting public key parameters from ASN.1 format to raw ECC point. The raw ECC point format, also used by [wolfBoot](https://github.com/wolfSSL/wolfBoot), represents a public key in its two -coordinates (Qx,Qy) a fixed size (2 x keysize). +coordinates (Qx,Qy) of fixed size (2 x keysize). Usage: