From 2fd78f6d402cc7a488ac7c248e3b5ab6396747c4 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Wed, 29 Jul 2020 16:47:56 +0200 Subject: [PATCH] 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; +}