refactoring

This commit is contained in:
merlokk 2018-11-07 17:34:15 +02:00
commit 9ad3a10c2d
5 changed files with 87 additions and 45 deletions

View file

@ -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\

63
client/crypto/asn1utils.c Normal file
View file

@ -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 <mbedtls/asn1.h>
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;
}

21
client/crypto/asn1utils.h Normal file
View file

@ -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 <stdint.h>
#include <stdbool.h>
#include <stddef.h>
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 */

View file

@ -21,7 +21,7 @@
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/error.h>
//test!!!
#include <crypto/asn1utils.h>
#include <util.h>
// 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"

View file

@ -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);