mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 21:33:47 -07:00
make style
This commit is contained in:
parent
0d9223a547
commit
0373696662
483 changed files with 56514 additions and 52451 deletions
|
@ -48,7 +48,7 @@ struct asn1_tag {
|
|||
|
||||
static const struct asn1_tag asn1_tags[] = {
|
||||
// internal
|
||||
{ 0x00 , "Unknown ???" },
|
||||
{ 0x00, "Unknown ???" },
|
||||
|
||||
// ASN.1
|
||||
{ 0x01, "BOOLEAN", ASN1_TAG_BOOLEAN },
|
||||
|
@ -87,25 +87,29 @@ static const struct asn1_tag asn1_tags[] = {
|
|||
{ 0xa5, "[5]" },
|
||||
};
|
||||
|
||||
static int asn1_sort_tag(tlv_tag_t tag) {
|
||||
static int asn1_sort_tag(tlv_tag_t tag)
|
||||
{
|
||||
return (int)(tag >= 0x100 ? tag : tag << 8);
|
||||
}
|
||||
|
||||
static int asn1_tlv_compare(const void *a, const void *b) {
|
||||
static int asn1_tlv_compare(const void *a, const void *b)
|
||||
{
|
||||
const struct tlv *tlv = a;
|
||||
const struct asn1_tag *tag = b;
|
||||
|
||||
return asn1_sort_tag(tlv->tag) - (asn1_sort_tag(tag->tag));
|
||||
}
|
||||
|
||||
static const struct asn1_tag *asn1_get_tag(const struct tlv *tlv) {
|
||||
static const struct asn1_tag *asn1_get_tag(const struct tlv *tlv)
|
||||
{
|
||||
struct asn1_tag *tag = bsearch(tlv, asn1_tags, sizeof(asn1_tags) / sizeof(asn1_tags[0]),
|
||||
sizeof(asn1_tags[0]), asn1_tlv_compare);
|
||||
sizeof(asn1_tags[0]), asn1_tlv_compare);
|
||||
|
||||
return tag ? tag : &asn1_tags[0];
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_str_time(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level, bool longyear, bool *needdump){
|
||||
static void asn1_tag_dump_str_time(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level, bool longyear, bool *needdump)
|
||||
{
|
||||
int len = tlv->len;
|
||||
*needdump = false;
|
||||
|
||||
|
@ -157,16 +161,18 @@ static void asn1_tag_dump_str_time(const struct tlv *tlv, const struct asn1_tag
|
|||
}
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_string(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level){
|
||||
static void asn1_tag_dump_string(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level)
|
||||
{
|
||||
fprintf(f, "\tvalue: '");
|
||||
fwrite(tlv->value, 1, tlv->len, f);
|
||||
fprintf(f, "'\n");
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_octet_string(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level, bool *needdump){
|
||||
static void asn1_tag_dump_octet_string(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level, bool *needdump)
|
||||
{
|
||||
*needdump = false;
|
||||
for (int i = 0; i < tlv->len; i++)
|
||||
if (!isspace(tlv->value[i]) && !isprint(tlv->value[i])){
|
||||
if (!isspace(tlv->value[i]) && !isprint(tlv->value[i])) {
|
||||
*needdump = true;
|
||||
break;
|
||||
}
|
||||
|
@ -179,7 +185,8 @@ static void asn1_tag_dump_octet_string(const struct tlv *tlv, const struct asn1_
|
|||
}
|
||||
}
|
||||
|
||||
static unsigned long asn1_value_integer(const struct tlv *tlv, unsigned start, unsigned end) {
|
||||
static unsigned long asn1_value_integer(const struct tlv *tlv, unsigned start, unsigned end)
|
||||
{
|
||||
unsigned long ret = 0;
|
||||
int i;
|
||||
|
||||
|
@ -189,36 +196,38 @@ static unsigned long asn1_value_integer(const struct tlv *tlv, unsigned start, u
|
|||
return ret;
|
||||
|
||||
if (start & 1) {
|
||||
ret += tlv->value[start/2] & 0xf;
|
||||
ret += tlv->value[start / 2] & 0xf;
|
||||
i = start + 1;
|
||||
} else
|
||||
i = start;
|
||||
|
||||
for (; i < end - 1; i += 2) {
|
||||
ret *= 10;
|
||||
ret += tlv->value[i/2] >> 4;
|
||||
ret += tlv->value[i / 2] >> 4;
|
||||
ret *= 10;
|
||||
ret += tlv->value[i/2] & 0xf;
|
||||
ret += tlv->value[i / 2] & 0xf;
|
||||
}
|
||||
|
||||
if (end & 1) {
|
||||
ret *= 10;
|
||||
ret += tlv->value[end/2] >> 4;
|
||||
ret += tlv->value[end / 2] >> 4;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_boolean(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level) {
|
||||
static void asn1_tag_dump_boolean(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level)
|
||||
{
|
||||
PRINT_INDENT(level);
|
||||
if (tlv->len > 0) {
|
||||
fprintf(f, "\tvalue: %s\n", tlv->value[0]?"true":"false");
|
||||
fprintf(f, "\tvalue: %s\n", tlv->value[0] ? "true" : "false");
|
||||
} else {
|
||||
fprintf(f, "n/a\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_integer(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level) {
|
||||
static void asn1_tag_dump_integer(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level)
|
||||
{
|
||||
PRINT_INDENT(level);
|
||||
if (tlv->len == 4) {
|
||||
int32_t val = 0;
|
||||
|
@ -230,7 +239,8 @@ static void asn1_tag_dump_integer(const struct tlv *tlv, const struct asn1_tag *
|
|||
fprintf(f, "\tvalue: %lu\n", asn1_value_integer(tlv, 0, tlv->len * 2));
|
||||
}
|
||||
|
||||
static char *asn1_oid_description(const char *oid, bool with_group_desc) {
|
||||
static char *asn1_oid_description(const char *oid, bool with_group_desc)
|
||||
{
|
||||
json_error_t error;
|
||||
json_t *root = NULL;
|
||||
char fname[300] = {0};
|
||||
|
@ -238,7 +248,7 @@ static char *asn1_oid_description(const char *oid, bool with_group_desc) {
|
|||
memset(res, 0x00, sizeof(res));
|
||||
|
||||
size_t len = strlen(get_my_executable_directory());
|
||||
if ( len > 300 ) len = 299;
|
||||
if (len > 300) len = 299;
|
||||
|
||||
strncpy(fname, get_my_executable_directory(), len);
|
||||
strcat(fname, "crypto/oids.json");
|
||||
|
@ -281,7 +291,8 @@ error:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void asn1_tag_dump_object_id(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level) {
|
||||
static void asn1_tag_dump_object_id(const struct tlv *tlv, const struct asn1_tag *tag, FILE *f, int level)
|
||||
{
|
||||
PRINT_INDENT(level);
|
||||
mbedtls_asn1_buf asn1_buf;
|
||||
asn1_buf.len = tlv->len;
|
||||
|
@ -314,7 +325,8 @@ static void asn1_tag_dump_object_id(const struct tlv *tlv, const struct asn1_tag
|
|||
fprintf(f, "\n");
|
||||
}
|
||||
|
||||
bool asn1_tag_dump(const struct tlv *tlv, FILE *f, int level, bool *candump) {
|
||||
bool asn1_tag_dump(const struct tlv *tlv, FILE *f, int level, bool *candump)
|
||||
{
|
||||
if (!tlv) {
|
||||
fprintf(f, "NULL\n");
|
||||
return false;
|
||||
|
@ -326,34 +338,34 @@ bool asn1_tag_dump(const struct tlv *tlv, FILE *f, int level, bool *candump) {
|
|||
fprintf(f, "--%2hx[%02zx] '%s':", tlv->tag, tlv->len, tag->name);
|
||||
|
||||
switch (tag->type) {
|
||||
case ASN1_TAG_GENERIC:
|
||||
fprintf(f, "\n");
|
||||
break;
|
||||
case ASN1_TAG_STRING:
|
||||
asn1_tag_dump_string(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_OCTET_STRING:
|
||||
asn1_tag_dump_octet_string(tlv, tag, f, level, candump);
|
||||
break;
|
||||
case ASN1_TAG_BOOLEAN:
|
||||
asn1_tag_dump_boolean(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_INTEGER:
|
||||
asn1_tag_dump_integer(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_UTC_TIME:
|
||||
asn1_tag_dump_str_time(tlv, tag, f, level, false, candump);
|
||||
break;
|
||||
case ASN1_TAG_STR_TIME:
|
||||
asn1_tag_dump_str_time(tlv, tag, f, level, true, candump);
|
||||
break;
|
||||
case ASN1_TAG_OBJECT_ID:
|
||||
asn1_tag_dump_object_id(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_GENERIC:
|
||||
fprintf(f, "\n");
|
||||
break;
|
||||
case ASN1_TAG_STRING:
|
||||
asn1_tag_dump_string(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_OCTET_STRING:
|
||||
asn1_tag_dump_octet_string(tlv, tag, f, level, candump);
|
||||
break;
|
||||
case ASN1_TAG_BOOLEAN:
|
||||
asn1_tag_dump_boolean(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_INTEGER:
|
||||
asn1_tag_dump_integer(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
case ASN1_TAG_UTC_TIME:
|
||||
asn1_tag_dump_str_time(tlv, tag, f, level, false, candump);
|
||||
break;
|
||||
case ASN1_TAG_STR_TIME:
|
||||
asn1_tag_dump_str_time(tlv, tag, f, level, true, candump);
|
||||
break;
|
||||
case ASN1_TAG_OBJECT_ID:
|
||||
asn1_tag_dump_object_id(tlv, tag, f, level);
|
||||
*candump = false;
|
||||
break;
|
||||
};
|
||||
|
||||
return true;
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
#include "asn1dump.h"
|
||||
#include "util.h"
|
||||
|
||||
int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval) {
|
||||
int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *rval, uint8_t *sval)
|
||||
{
|
||||
if (!signature || !signaturelen || !rval || !sval)
|
||||
return 1;
|
||||
|
||||
|
@ -55,13 +56,14 @@ int ecdsa_asn1_get_signature(uint8_t *signature, size_t signaturelen, uint8_t *r
|
|||
// check size
|
||||
if (end != p)
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
return res;
|
||||
}
|
||||
|
||||
static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) {
|
||||
static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf)
|
||||
{
|
||||
bool candump = true;
|
||||
asn1_tag_dump(tlv, stdout, level, &candump);
|
||||
if (is_leaf && candump) {
|
||||
|
@ -71,7 +73,8 @@ static bool print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf)
|
|||
return true;
|
||||
}
|
||||
|
||||
int asn1_print(uint8_t *asn1buf, size_t asn1buflen, char *indent) {
|
||||
int asn1_print(uint8_t *asn1buf, size_t asn1buflen, char *indent)
|
||||
{
|
||||
|
||||
struct tlvdb *t = NULL;
|
||||
t = tlvdb_parse_multi(asn1buf, asn1buflen);
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
#include <util.h>
|
||||
|
||||
// NIST Special Publication 800-38A — Recommendation for block cipher modes of operation: methods and techniques, 2001.
|
||||
int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){
|
||||
int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length)
|
||||
{
|
||||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
@ -42,7 +43,8 @@ int aes_encode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l
|
|||
return 0;
|
||||
}
|
||||
|
||||
int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length){
|
||||
int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int length)
|
||||
{
|
||||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
@ -60,14 +62,16 @@ int aes_decode(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *output, int l
|
|||
|
||||
// NIST Special Publication 800-38B — Recommendation for block cipher modes of operation: The CMAC mode for authentication.
|
||||
// https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CMAC.pdf
|
||||
int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
|
||||
int aes_cmac(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length)
|
||||
{
|
||||
memset(mac, 0x00, 16);
|
||||
|
||||
// NIST 800-38B
|
||||
return mbedtls_aes_cmac_prf_128(key, MBEDTLS_AES_BLOCK_SIZE, input, length, mac);
|
||||
}
|
||||
|
||||
int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length) {
|
||||
int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int length)
|
||||
{
|
||||
uint8_t cmac[16] = {0};
|
||||
memset(mac, 0x00, 8);
|
||||
|
||||
|
@ -75,14 +79,15 @@ int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, int lengt
|
|||
if (res)
|
||||
return res;
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
for (int i = 0; i < 8; i++)
|
||||
mac[i] = cmac[i * 2 + 1];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t fixed_rand_value[250] = {0};
|
||||
static int fixed_rand(void *rng_state, unsigned char *output, size_t len) {
|
||||
static int fixed_rand(void *rng_state, unsigned char *output, size_t len)
|
||||
{
|
||||
if (len <= 250) {
|
||||
memcpy(output, fixed_rand_value, len);
|
||||
} else {
|
||||
|
@ -92,7 +97,8 @@ static int fixed_rand(void *rng_state, unsigned char *output, size_t len) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int sha256hash(uint8_t *input, int length, uint8_t *hash) {
|
||||
int sha256hash(uint8_t *input, int length, uint8_t *hash)
|
||||
{
|
||||
if (!hash || !input)
|
||||
return 1;
|
||||
|
||||
|
@ -106,7 +112,8 @@ int sha256hash(uint8_t *input, int length, uint8_t *hash) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init_str(mbedtls_ecdsa_context *ctx, char * key_d, char *key_x, char *key_y) {
|
||||
int ecdsa_init_str(mbedtls_ecdsa_context *ctx, char *key_d, char *key_x, char *key_y)
|
||||
{
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
|
@ -132,7 +139,8 @@ int ecdsa_init_str(mbedtls_ecdsa_context *ctx, char * key_d, char *key_x, char *
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init(mbedtls_ecdsa_context *ctx, uint8_t * key_d, uint8_t *key_xy) {
|
||||
int ecdsa_init(mbedtls_ecdsa_context *ctx, uint8_t *key_d, uint8_t *key_xy)
|
||||
{
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
|
@ -158,7 +166,8 @@ int ecdsa_init(mbedtls_ecdsa_context *ctx, uint8_t * key_d, uint8_t *key_xy) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_key_create(uint8_t * key_d, uint8_t *key_xy) {
|
||||
int ecdsa_key_create(uint8_t *key_d, uint8_t *key_xy)
|
||||
{
|
||||
int res;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, NULL);
|
||||
|
@ -202,14 +211,16 @@ exit:
|
|||
return res;
|
||||
}
|
||||
|
||||
char *ecdsa_get_error(int ret) {
|
||||
char *ecdsa_get_error(int ret)
|
||||
{
|
||||
static char retstr[300];
|
||||
memset(retstr, 0x00, sizeof(retstr));
|
||||
mbedtls_strerror(ret, retstr, sizeof(retstr));
|
||||
return retstr;
|
||||
}
|
||||
|
||||
int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen) {
|
||||
int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen)
|
||||
{
|
||||
int res = 0;
|
||||
size_t realkeylen = 0;
|
||||
if (keylen < 65)
|
||||
|
@ -222,7 +233,7 @@ int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen
|
|||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk) );
|
||||
res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk));
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
|
@ -234,7 +245,8 @@ exit:
|
|||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_create(uint8_t *key_d, uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen) {
|
||||
int ecdsa_signature_create(uint8_t *key_d, uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen)
|
||||
{
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
|
@ -264,7 +276,8 @@ exit:
|
|||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_create_test(char * key_d, char *key_x, char *key_y, char *random, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen) {
|
||||
int ecdsa_signature_create_test(char *key_d, char *key_x, char *key_y, char *random, uint8_t *input, int length, uint8_t *signature, size_t *signaturelen)
|
||||
{
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
|
@ -284,7 +297,8 @@ int ecdsa_signature_create_test(char * key_d, char *key_x, char *key_y, char *ra
|
|||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_verify_keystr(char *key_x, char *key_y, uint8_t *input, int length, uint8_t *signature, size_t signaturelen) {
|
||||
int ecdsa_signature_verify_keystr(char *key_x, char *key_y, uint8_t *input, int length, uint8_t *signature, size_t signaturelen)
|
||||
{
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
|
@ -299,7 +313,8 @@ int ecdsa_signature_verify_keystr(char *key_x, char *key_y, uint8_t *input, int
|
|||
return res;
|
||||
}
|
||||
|
||||
int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t signaturelen) {
|
||||
int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t *signature, size_t signaturelen)
|
||||
{
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
|
@ -321,7 +336,8 @@ int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t
|
|||
#define T_R "2B42F576D07F4165FF65D1F3B1500F81E44C316F1F0B3EF57325B69ACA46104F"
|
||||
#define T_S "DC42C2122D6392CD3E3A993A89502A8198C1886FE69D262C4B329BDB6B63FAF1"
|
||||
|
||||
int ecdsa_nist_test(bool verbose) {
|
||||
int ecdsa_nist_test(bool verbose)
|
||||
{
|
||||
int res;
|
||||
uint8_t input[] = "Example of ECDSA with P-256";
|
||||
int length = strlen((char *)input);
|
||||
|
|
|
@ -23,7 +23,7 @@ extern int aes_cmac8(uint8_t *iv, uint8_t *key, uint8_t *input, uint8_t *mac, in
|
|||
|
||||
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_key_create(uint8_t *key_d, uint8_t *key_xy);
|
||||
extern int ecdsa_public_key_from_pk(mbedtls_pk_context *pk, uint8_t *key, size_t keylen);
|
||||
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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue