From 9ad3a10c2d33ce318605c3c46a4a3fd49d71a7a5 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Wed, 7 Nov 2018 17:34:15 +0200 Subject: [PATCH] refactoring --- client/Makefile | 1 + client/crypto/asn1utils.c | 63 ++++++++++++++++++++++++++++++++++++++ client/crypto/asn1utils.h | 21 +++++++++++++ client/crypto/libpcrypto.c | 46 +--------------------------- client/crypto/libpcrypto.h | 1 + 5 files changed, 87 insertions(+), 45 deletions(-) create mode 100644 client/crypto/asn1utils.c create mode 100644 client/crypto/asn1utils.h diff --git a/client/Makefile b/client/Makefile index 0cb052e0..66058443 100644 --- a/client/Makefile +++ b/client/Makefile @@ -109,6 +109,7 @@ CMDSRCS = $(SRC_SMARTCARD) \ crapto1/crypto1.c\ polarssl/des.c\ crypto/libpcrypto.c\ + crypto/asn1utils.c\ cliparser/argtable3.c\ cliparser/cliparser.c\ mfkey.c\ diff --git a/client/crypto/asn1utils.c b/client/crypto/asn1utils.c new file mode 100644 index 00000000..2a3fe698 --- /dev/null +++ b/client/crypto/asn1utils.c @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2018 Merlok +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// asn.1 utils +//----------------------------------------------------------------------------- + +#include "asn1utils.h" +#include + +int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval) { + if (!signature || !signaturelen || !rval || !sval) + return 1; + + int res = 0; + unsigned char *p = signature; + const unsigned char *end = p + signaturelen; + size_t len; + mbedtls_mpi xmpi; + + if ((res = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) == 0) { + mbedtls_mpi_init(&xmpi); + res = mbedtls_asn1_get_mpi(&p, end, &xmpi); + if (res) { + mbedtls_mpi_free(&xmpi); + goto exit; + } + + res = mbedtls_mpi_write_binary(&xmpi, rval, 32); + mbedtls_mpi_free(&xmpi); + if (res) + goto exit; + + mbedtls_mpi_init(&xmpi); + res = mbedtls_asn1_get_mpi(&p, end, &xmpi); + if (res) { + mbedtls_mpi_free(&xmpi); + goto exit; + } + + res = mbedtls_mpi_write_binary(&xmpi, sval, 32); + mbedtls_mpi_free(&xmpi); + if (res) + goto exit; + + // check size + if (end != p) + return 2; + } + +exit: + return res; +} + +int asn1_print(uint8_t *asn1buf, int level) { + + return 0; +} + + diff --git a/client/crypto/asn1utils.h b/client/crypto/asn1utils.h new file mode 100644 index 00000000..2b00f450 --- /dev/null +++ b/client/crypto/asn1utils.h @@ -0,0 +1,21 @@ +//----------------------------------------------------------------------------- +// Copyright (C) 2018 Merlok +// +// This code is licensed to you under the terms of the GNU GPL, version 2 or, +// at your option, any later version. See the LICENSE.txt file for the text of +// the license. +//----------------------------------------------------------------------------- +// asn.1 utils +//----------------------------------------------------------------------------- + +#ifndef ASN1UTILS_H +#define ASN1UTILS_H + +#include +#include +#include + +extern int asn1_print(uint8_t *asn1buf, int level); +extern int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval); + +#endif /* asn1utils.h */ diff --git a/client/crypto/libpcrypto.c b/client/crypto/libpcrypto.c index 11713251..030be15a 100644 --- a/client/crypto/libpcrypto.c +++ b/client/crypto/libpcrypto.c @@ -21,7 +21,7 @@ #include #include #include -//test!!! +#include #include // NIST Special Publication 800-38A — Recommendation for block cipher modes of operation: methods and techniques, 2001. @@ -288,50 +288,6 @@ int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t return res; } -int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval) { - if (!signature || !signaturelen || !rval || !sval) - return 1; - - int res = 0; - unsigned char *p = signature; - const unsigned char *end = p + signaturelen; - size_t len; - mbedtls_mpi xmpi; - - if ((res = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) == 0) { - mbedtls_mpi_init(&xmpi); - res = mbedtls_asn1_get_mpi(&p, end, &xmpi); - if (res) { - mbedtls_mpi_free(&xmpi); - goto exit; - } - - res = mbedtls_mpi_write_binary(&xmpi, rval, 32); - mbedtls_mpi_free(&xmpi); - if (res) - goto exit; - - mbedtls_mpi_init(&xmpi); - res = mbedtls_asn1_get_mpi(&p, end, &xmpi); - if (res) { - mbedtls_mpi_free(&xmpi); - goto exit; - } - - res = mbedtls_mpi_write_binary(&xmpi, sval, 32); - mbedtls_mpi_free(&xmpi); - if (res) - goto exit; - - // check size - if (end != p) - return 2; - } - -exit: - return res; -} - #define T_PRIVATE_KEY "C477F9F65C22CCE20657FAA5B2D1D8122336F851A508A1ED04E479C34985BF96" #define T_Q_X "B7E08AFDFE94BAD3F1DC8C734798BA1C62B3A0AD1E9EA2A38201CD0889BC7A19" #define T_Q_Y "3603F747959DBF7A4BB226E41928729063ADC7AE43529E61B563BBC606CC5E09" diff --git a/client/crypto/libpcrypto.h b/client/crypto/libpcrypto.h index a229cfb4..8d4b4a0d 100644 --- a/client/crypto/libpcrypto.h +++ b/client/crypto/libpcrypto.h @@ -25,6 +25,7 @@ extern int sha256hash(uint8_t *input, int length, uint8_t *hash); extern int ecdsa_key_create(uint8_t * key_d, uint8_t *key_xy); extern int ecdsa_signature_create(uint8_t *key_d, uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen); extern int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t signaturelen); +extern char *ecdsa_get_error(int ret); extern int ecdsa_nist_test(bool verbose);