mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-14 18:48:13 -07:00
client: fix mix of spaces & tabs
This commit is contained in:
parent
112411042f
commit
0d9223a547
197 changed files with 49383 additions and 49383 deletions
|
@ -25,336 +25,336 @@
|
|||
#include "proxmark3.h"
|
||||
|
||||
#ifndef PRINT_INDENT
|
||||
# define PRINT_INDENT(level) {for (int i = 0; i < (level); i++) fprintf(f, " ");}
|
||||
# define PRINT_INDENT(level) {for (int i = 0; i < (level); i++) fprintf(f, " ");}
|
||||
#endif
|
||||
|
||||
enum asn1_tag_t {
|
||||
ASN1_TAG_GENERIC,
|
||||
ASN1_TAG_BOOLEAN,
|
||||
ASN1_TAG_INTEGER,
|
||||
ASN1_TAG_STRING,
|
||||
ASN1_TAG_OCTET_STRING,
|
||||
ASN1_TAG_UTC_TIME,
|
||||
ASN1_TAG_STR_TIME,
|
||||
ASN1_TAG_OBJECT_ID,
|
||||
ASN1_TAG_GENERIC,
|
||||
ASN1_TAG_BOOLEAN,
|
||||
ASN1_TAG_INTEGER,
|
||||
ASN1_TAG_STRING,
|
||||
ASN1_TAG_OCTET_STRING,
|
||||
ASN1_TAG_UTC_TIME,
|
||||
ASN1_TAG_STR_TIME,
|
||||
ASN1_TAG_OBJECT_ID,
|
||||
};
|
||||
|
||||
struct asn1_tag {
|
||||
tlv_tag_t tag;
|
||||
char *name;
|
||||
enum asn1_tag_t type;
|
||||
const void *data;
|
||||
tlv_tag_t tag;
|
||||
char *name;
|
||||
enum asn1_tag_t type;
|
||||
const void *data;
|
||||
};
|
||||
|
||||
static const struct asn1_tag asn1_tags[] = {
|
||||
// internal
|
||||
{ 0x00 , "Unknown ???" },
|
||||
// internal
|
||||
{ 0x00 , "Unknown ???" },
|
||||
|
||||
// ASN.1
|
||||
{ 0x01, "BOOLEAN", ASN1_TAG_BOOLEAN },
|
||||
{ 0x02, "INTEGER", ASN1_TAG_INTEGER },
|
||||
{ 0x03, "BIT STRING" },
|
||||
{ 0x04, "OCTET STRING", ASN1_TAG_OCTET_STRING},
|
||||
{ 0x05, "NULL" },
|
||||
{ 0x06, "OBJECT IDENTIFIER", ASN1_TAG_OBJECT_ID },
|
||||
{ 0x07, "OBJECT DESCRIPTOR" },
|
||||
{ 0x08, "EXTERNAL" },
|
||||
{ 0x09, "REAL" },
|
||||
{ 0x0A, "ENUMERATED" },
|
||||
{ 0x0B, "EMBEDDED_PDV" },
|
||||
{ 0x0C, "UTF8String", ASN1_TAG_STRING },
|
||||
{ 0x10, "SEQUENCE" },
|
||||
{ 0x11, "SET" },
|
||||
{ 0x12, "NumericString", ASN1_TAG_STRING },
|
||||
{ 0x13, "PrintableString", ASN1_TAG_STRING },
|
||||
{ 0x14, "T61String" },
|
||||
{ 0x15, "VideotexString" },
|
||||
{ 0x16, "IA5String" },
|
||||
{ 0x17, "UTCTime", ASN1_TAG_UTC_TIME },
|
||||
{ 0x18, "GeneralizedTime", ASN1_TAG_STR_TIME },
|
||||
{ 0x19, "GraphicString" },
|
||||
{ 0x1A, "VisibleString", ASN1_TAG_STRING },
|
||||
{ 0x1B, "GeneralString", ASN1_TAG_STRING },
|
||||
{ 0x1C, "UniversalString", ASN1_TAG_STRING },
|
||||
{ 0x1E, "BMPString" },
|
||||
{ 0x30, "SEQUENCE" },
|
||||
{ 0x31, "SET" },
|
||||
{ 0xa0, "[0]" },
|
||||
{ 0xa1, "[1]" },
|
||||
{ 0xa2, "[2]" },
|
||||
{ 0xa3, "[3]" },
|
||||
{ 0xa4, "[4]" },
|
||||
{ 0xa5, "[5]" },
|
||||
// ASN.1
|
||||
{ 0x01, "BOOLEAN", ASN1_TAG_BOOLEAN },
|
||||
{ 0x02, "INTEGER", ASN1_TAG_INTEGER },
|
||||
{ 0x03, "BIT STRING" },
|
||||
{ 0x04, "OCTET STRING", ASN1_TAG_OCTET_STRING},
|
||||
{ 0x05, "NULL" },
|
||||
{ 0x06, "OBJECT IDENTIFIER", ASN1_TAG_OBJECT_ID },
|
||||
{ 0x07, "OBJECT DESCRIPTOR" },
|
||||
{ 0x08, "EXTERNAL" },
|
||||
{ 0x09, "REAL" },
|
||||
{ 0x0A, "ENUMERATED" },
|
||||
{ 0x0B, "EMBEDDED_PDV" },
|
||||
{ 0x0C, "UTF8String", ASN1_TAG_STRING },
|
||||
{ 0x10, "SEQUENCE" },
|
||||
{ 0x11, "SET" },
|
||||
{ 0x12, "NumericString", ASN1_TAG_STRING },
|
||||
{ 0x13, "PrintableString", ASN1_TAG_STRING },
|
||||
{ 0x14, "T61String" },
|
||||
{ 0x15, "VideotexString" },
|
||||
{ 0x16, "IA5String" },
|
||||
{ 0x17, "UTCTime", ASN1_TAG_UTC_TIME },
|
||||
{ 0x18, "GeneralizedTime", ASN1_TAG_STR_TIME },
|
||||
{ 0x19, "GraphicString" },
|
||||
{ 0x1A, "VisibleString", ASN1_TAG_STRING },
|
||||
{ 0x1B, "GeneralString", ASN1_TAG_STRING },
|
||||
{ 0x1C, "UniversalString", ASN1_TAG_STRING },
|
||||
{ 0x1E, "BMPString" },
|
||||
{ 0x30, "SEQUENCE" },
|
||||
{ 0x31, "SET" },
|
||||
{ 0xa0, "[0]" },
|
||||
{ 0xa1, "[1]" },
|
||||
{ 0xa2, "[2]" },
|
||||
{ 0xa3, "[3]" },
|
||||
{ 0xa4, "[4]" },
|
||||
{ 0xa5, "[5]" },
|
||||
};
|
||||
|
||||
static int asn1_sort_tag(tlv_tag_t tag) {
|
||||
return (int)(tag >= 0x100 ? tag : tag << 8);
|
||||
return (int)(tag >= 0x100 ? tag : tag << 8);
|
||||
}
|
||||
|
||||
static int asn1_tlv_compare(const void *a, const void *b) {
|
||||
const struct tlv *tlv = a;
|
||||
const struct asn1_tag *tag = b;
|
||||
const struct tlv *tlv = a;
|
||||
const struct asn1_tag *tag = b;
|
||||
|
||||
return asn1_sort_tag(tlv->tag) - (asn1_sort_tag(tag->tag));
|
||||
return asn1_sort_tag(tlv->tag) - (asn1_sort_tag(tag->tag));
|
||||
}
|
||||
|
||||
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);
|
||||
struct asn1_tag *tag = bsearch(tlv, asn1_tags, sizeof(asn1_tags) / sizeof(asn1_tags[0]),
|
||||
sizeof(asn1_tags[0]), asn1_tlv_compare);
|
||||
|
||||
return tag ? tag : &asn1_tags[0];
|
||||
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){
|
||||
int len = tlv->len;
|
||||
*needdump = false;
|
||||
int len = tlv->len;
|
||||
*needdump = false;
|
||||
|
||||
int startindx = longyear ? 4 : 2;
|
||||
int startindx = longyear ? 4 : 2;
|
||||
|
||||
if (len > 4) {
|
||||
fprintf(f, "\tvalue: '");
|
||||
while (true) {
|
||||
// year
|
||||
if (!longyear)
|
||||
fprintf(f, "20");
|
||||
fwrite(tlv->value, 1, longyear ? 4 : 2, f);
|
||||
fprintf(f, "-");
|
||||
if (len < startindx + 2)
|
||||
break;
|
||||
// month
|
||||
fwrite(&tlv->value[startindx], 1, 2, f);
|
||||
fprintf(f, "-");
|
||||
if (len < startindx + 4)
|
||||
break;
|
||||
// day
|
||||
fwrite(&tlv->value[startindx + 2], 1, 2, f);
|
||||
fprintf(f, " ");
|
||||
if (len < startindx + 6)
|
||||
break;
|
||||
// hour
|
||||
fwrite(&tlv->value[startindx + 4], 1, 2, f);
|
||||
fprintf(f, ":");
|
||||
if (len < startindx + 8)
|
||||
break;
|
||||
// min
|
||||
fwrite(&tlv->value[startindx + 6], 1, 2, f);
|
||||
fprintf(f, ":");
|
||||
if (len < startindx + 10)
|
||||
break;
|
||||
// sec
|
||||
fwrite(&tlv->value[startindx + 8], 1, 2, f);
|
||||
if (len < startindx + 11)
|
||||
break;
|
||||
// time zone
|
||||
fprintf(f, " zone: %.*s", len - 10 - (longyear ? 4 : 2), &tlv->value[startindx + 10]);
|
||||
if (len > 4) {
|
||||
fprintf(f, "\tvalue: '");
|
||||
while (true) {
|
||||
// year
|
||||
if (!longyear)
|
||||
fprintf(f, "20");
|
||||
fwrite(tlv->value, 1, longyear ? 4 : 2, f);
|
||||
fprintf(f, "-");
|
||||
if (len < startindx + 2)
|
||||
break;
|
||||
// month
|
||||
fwrite(&tlv->value[startindx], 1, 2, f);
|
||||
fprintf(f, "-");
|
||||
if (len < startindx + 4)
|
||||
break;
|
||||
// day
|
||||
fwrite(&tlv->value[startindx + 2], 1, 2, f);
|
||||
fprintf(f, " ");
|
||||
if (len < startindx + 6)
|
||||
break;
|
||||
// hour
|
||||
fwrite(&tlv->value[startindx + 4], 1, 2, f);
|
||||
fprintf(f, ":");
|
||||
if (len < startindx + 8)
|
||||
break;
|
||||
// min
|
||||
fwrite(&tlv->value[startindx + 6], 1, 2, f);
|
||||
fprintf(f, ":");
|
||||
if (len < startindx + 10)
|
||||
break;
|
||||
// sec
|
||||
fwrite(&tlv->value[startindx + 8], 1, 2, f);
|
||||
if (len < startindx + 11)
|
||||
break;
|
||||
// time zone
|
||||
fprintf(f, " zone: %.*s", len - 10 - (longyear ? 4 : 2), &tlv->value[startindx + 10]);
|
||||
|
||||
break;
|
||||
}
|
||||
fprintf(f, "'\n");
|
||||
} else {
|
||||
fprintf(f, "\n");
|
||||
*needdump = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
fprintf(f, "'\n");
|
||||
} else {
|
||||
fprintf(f, "\n");
|
||||
*needdump = true;
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
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){
|
||||
*needdump = false;
|
||||
for (int i = 0; i < tlv->len; i++)
|
||||
if (!isspace(tlv->value[i]) && !isprint(tlv->value[i])){
|
||||
*needdump = true;
|
||||
break;
|
||||
}
|
||||
*needdump = false;
|
||||
for (int i = 0; i < tlv->len; i++)
|
||||
if (!isspace(tlv->value[i]) && !isprint(tlv->value[i])){
|
||||
*needdump = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*needdump) {
|
||||
fprintf(f, "'\n");
|
||||
} else {
|
||||
fprintf(f, "\t\t");
|
||||
asn1_tag_dump_string(tlv, tag, f, level);
|
||||
}
|
||||
if (*needdump) {
|
||||
fprintf(f, "'\n");
|
||||
} else {
|
||||
fprintf(f, "\t\t");
|
||||
asn1_tag_dump_string(tlv, tag, f, level);
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned long asn1_value_integer(const struct tlv *tlv, unsigned start, unsigned end) {
|
||||
unsigned long ret = 0;
|
||||
int i;
|
||||
unsigned long ret = 0;
|
||||
int i;
|
||||
|
||||
if (end > tlv->len * 2)
|
||||
return ret;
|
||||
if (start >= end)
|
||||
return ret;
|
||||
if (end > tlv->len * 2)
|
||||
return ret;
|
||||
if (start >= end)
|
||||
return ret;
|
||||
|
||||
if (start & 1) {
|
||||
ret += tlv->value[start/2] & 0xf;
|
||||
i = start + 1;
|
||||
} else
|
||||
i = start;
|
||||
if (start & 1) {
|
||||
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 *= 10;
|
||||
ret += tlv->value[i/2] & 0xf;
|
||||
}
|
||||
for (; i < end - 1; i += 2) {
|
||||
ret *= 10;
|
||||
ret += tlv->value[i/2] >> 4;
|
||||
ret *= 10;
|
||||
ret += tlv->value[i/2] & 0xf;
|
||||
}
|
||||
|
||||
if (end & 1) {
|
||||
ret *= 10;
|
||||
ret += tlv->value[end/2] >> 4;
|
||||
}
|
||||
if (end & 1) {
|
||||
ret *= 10;
|
||||
ret += tlv->value[end/2] >> 4;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
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");
|
||||
} else {
|
||||
fprintf(f, "n/a\n");
|
||||
}
|
||||
PRINT_INDENT(level);
|
||||
if (tlv->len > 0) {
|
||||
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) {
|
||||
PRINT_INDENT(level);
|
||||
if (tlv->len == 4) {
|
||||
int32_t val = 0;
|
||||
for (int i = 0; i < tlv->len; i++)
|
||||
val = (val << 8) + tlv->value[i];
|
||||
fprintf(f, "\tvalue4b: %d\n", val);
|
||||
return;
|
||||
}
|
||||
fprintf(f, "\tvalue: %lu\n", asn1_value_integer(tlv, 0, tlv->len * 2));
|
||||
PRINT_INDENT(level);
|
||||
if (tlv->len == 4) {
|
||||
int32_t val = 0;
|
||||
for (int i = 0; i < tlv->len; i++)
|
||||
val = (val << 8) + tlv->value[i];
|
||||
fprintf(f, "\tvalue4b: %d\n", val);
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
json_error_t error;
|
||||
json_t *root = NULL;
|
||||
char fname[300] = {0};
|
||||
static char res[300];
|
||||
memset(res, 0x00, sizeof(res));
|
||||
json_error_t error;
|
||||
json_t *root = NULL;
|
||||
char fname[300] = {0};
|
||||
static char res[300];
|
||||
memset(res, 0x00, sizeof(res));
|
||||
|
||||
size_t len = strlen(get_my_executable_directory());
|
||||
if ( len > 300 ) len = 299;
|
||||
size_t len = strlen(get_my_executable_directory());
|
||||
if ( len > 300 ) len = 299;
|
||||
|
||||
strncpy(fname, get_my_executable_directory(), len);
|
||||
strcat(fname, "crypto/oids.json");
|
||||
if (access(fname, F_OK) < 0) {
|
||||
strncpy(fname, get_my_executable_directory(), len);
|
||||
strcat(fname, "oids.json");
|
||||
if (access(fname, F_OK) < 0) {
|
||||
goto error; // file not found
|
||||
}
|
||||
}
|
||||
strncpy(fname, get_my_executable_directory(), len);
|
||||
strcat(fname, "crypto/oids.json");
|
||||
if (access(fname, F_OK) < 0) {
|
||||
strncpy(fname, get_my_executable_directory(), len);
|
||||
strcat(fname, "oids.json");
|
||||
if (access(fname, F_OK) < 0) {
|
||||
goto error; // file not found
|
||||
}
|
||||
}
|
||||
|
||||
// load `oids.json`
|
||||
root = json_load_file(fname, 0, &error);
|
||||
// load `oids.json`
|
||||
root = json_load_file(fname, 0, &error);
|
||||
|
||||
if (!root || !json_is_object(root)) {
|
||||
goto error;
|
||||
}
|
||||
if (!root || !json_is_object(root)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
json_t *elm = json_object_get(root, oid);
|
||||
if (!elm) {
|
||||
goto error;
|
||||
}
|
||||
json_t *elm = json_object_get(root, oid);
|
||||
if (!elm) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (JsonLoadStr(elm, "$.d", res))
|
||||
goto error;
|
||||
if (JsonLoadStr(elm, "$.d", res))
|
||||
goto error;
|
||||
|
||||
char strext[300] = {0};
|
||||
if (!JsonLoadStr(elm, "$.c", strext)) {
|
||||
strcat(res, " (");
|
||||
strcat(res, strext);
|
||||
strcat(res, ")");
|
||||
}
|
||||
char strext[300] = {0};
|
||||
if (!JsonLoadStr(elm, "$.c", strext)) {
|
||||
strcat(res, " (");
|
||||
strcat(res, strext);
|
||||
strcat(res, ")");
|
||||
}
|
||||
|
||||
json_decref(root);
|
||||
return res;
|
||||
json_decref(root);
|
||||
return res;
|
||||
|
||||
error:
|
||||
if (root)
|
||||
json_decref(root);
|
||||
return NULL;
|
||||
if (root)
|
||||
json_decref(root);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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;
|
||||
asn1_buf.p = (uint8_t *)tlv->value;
|
||||
char pstr[300];
|
||||
mbedtls_oid_get_numeric_string(pstr, sizeof(pstr), &asn1_buf);
|
||||
fprintf(f, " %s", pstr);
|
||||
PRINT_INDENT(level);
|
||||
mbedtls_asn1_buf asn1_buf;
|
||||
asn1_buf.len = tlv->len;
|
||||
asn1_buf.p = (uint8_t *)tlv->value;
|
||||
char pstr[300];
|
||||
mbedtls_oid_get_numeric_string(pstr, sizeof(pstr), &asn1_buf);
|
||||
fprintf(f, " %s", pstr);
|
||||
|
||||
char *jsondesc = asn1_oid_description(pstr, true);
|
||||
if (jsondesc) {
|
||||
fprintf(f, " - %s", jsondesc);
|
||||
} else {
|
||||
const char *ppstr;
|
||||
mbedtls_oid_get_attr_short_name(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
mbedtls_oid_get_sig_alg_desc(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
mbedtls_oid_get_extended_key_usage(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
fprintf(f, "\n");
|
||||
char *jsondesc = asn1_oid_description(pstr, true);
|
||||
if (jsondesc) {
|
||||
fprintf(f, " - %s", jsondesc);
|
||||
} else {
|
||||
const char *ppstr;
|
||||
mbedtls_oid_get_attr_short_name(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
mbedtls_oid_get_sig_alg_desc(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
mbedtls_oid_get_extended_key_usage(&asn1_buf, &ppstr);
|
||||
if (ppstr && strnlen(ppstr, 1)) {
|
||||
fprintf(f, " (%s)\n", ppstr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
|
||||
bool asn1_tag_dump(const struct tlv *tlv, FILE *f, int level, bool *candump) {
|
||||
if (!tlv) {
|
||||
fprintf(f, "NULL\n");
|
||||
return false;
|
||||
}
|
||||
if (!tlv) {
|
||||
fprintf(f, "NULL\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
const struct asn1_tag *tag = asn1_get_tag(tlv);
|
||||
const struct asn1_tag *tag = asn1_get_tag(tlv);
|
||||
|
||||
PRINT_INDENT(level);
|
||||
fprintf(f, "--%2hx[%02zx] '%s':", tlv->tag, tlv->len, tag->name);
|
||||
PRINT_INDENT(level);
|
||||
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;
|
||||
};
|
||||
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;
|
||||
};
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -18,72 +18,72 @@
|
|||
#include "util.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;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
res = mbedtls_mpi_write_binary(&xmpi, sval, 32);
|
||||
mbedtls_mpi_free(&xmpi);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
// check size
|
||||
if (end != p)
|
||||
return 2;
|
||||
}
|
||||
// check size
|
||||
if (end != p)
|
||||
return 2;
|
||||
}
|
||||
|
||||
exit:
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
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) {
|
||||
dump_buffer(tlv->value, tlv->len, stdout, level);
|
||||
}
|
||||
bool candump = true;
|
||||
asn1_tag_dump(tlv, stdout, level, &candump);
|
||||
if (is_leaf && candump) {
|
||||
dump_buffer(tlv->value, tlv->len, stdout, level);
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
int asn1_print(uint8_t *asn1buf, size_t asn1buflen, char *indent) {
|
||||
|
||||
struct tlvdb *t = NULL;
|
||||
t = tlvdb_parse_multi(asn1buf, asn1buflen);
|
||||
if (t) {
|
||||
tlvdb_visit(t, print_cb, NULL, 0);
|
||||
tlvdb_free(t);
|
||||
} else {
|
||||
PrintAndLogEx(ERR, "Can't parse data as TLV tree.");
|
||||
return 1;
|
||||
}
|
||||
struct tlvdb *t = NULL;
|
||||
t = tlvdb_parse_multi(asn1buf, asn1buflen);
|
||||
if (t) {
|
||||
tlvdb_visit(t, print_cb, NULL, 0);
|
||||
tlvdb_free(t);
|
||||
} else {
|
||||
PrintAndLogEx(ERR, "Can't parse data as TLV tree.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -27,291 +27,291 @@
|
|||
|
||||
// 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){
|
||||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_enc(&aes, key, 128))
|
||||
return 1;
|
||||
if (mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, length, iiv, input, output))
|
||||
return 2;
|
||||
mbedtls_aes_free(&aes);
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_enc(&aes, key, 128))
|
||||
return 1;
|
||||
if (mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, length, iiv, input, output))
|
||||
return 2;
|
||||
mbedtls_aes_free(&aes);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
uint8_t iiv[16] = {0};
|
||||
if (iv)
|
||||
memcpy(iiv, iv, 16);
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_dec(&aes, key, 128))
|
||||
return 1;
|
||||
if (mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, length, iiv, input, output))
|
||||
return 2;
|
||||
mbedtls_aes_free(&aes);
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
if (mbedtls_aes_setkey_dec(&aes, key, 128))
|
||||
return 1;
|
||||
if (mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, length, iiv, input, output))
|
||||
return 2;
|
||||
mbedtls_aes_free(&aes);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
memset(mac, 0x00, 16);
|
||||
memset(mac, 0x00, 16);
|
||||
|
||||
// NIST 800-38B
|
||||
return mbedtls_aes_cmac_prf_128(key, MBEDTLS_AES_BLOCK_SIZE, input, length, mac);
|
||||
// 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) {
|
||||
uint8_t cmac[16] = {0};
|
||||
memset(mac, 0x00, 8);
|
||||
uint8_t cmac[16] = {0};
|
||||
memset(mac, 0x00, 8);
|
||||
|
||||
int res = aes_cmac(iv, key, input, cmac, length);
|
||||
if (res)
|
||||
return res;
|
||||
int res = aes_cmac(iv, key, input, cmac, length);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
mac[i] = cmac[i * 2 + 1];
|
||||
for(int i = 0; i < 8; i++)
|
||||
mac[i] = cmac[i * 2 + 1];
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t fixed_rand_value[250] = {0};
|
||||
static int fixed_rand(void *rng_state, unsigned char *output, size_t len) {
|
||||
if (len <= 250) {
|
||||
memcpy(output, fixed_rand_value, len);
|
||||
} else {
|
||||
memset(output, 0x00, len);
|
||||
}
|
||||
if (len <= 250) {
|
||||
memcpy(output, fixed_rand_value, len);
|
||||
} else {
|
||||
memset(output, 0x00, len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sha256hash(uint8_t *input, int length, uint8_t *hash) {
|
||||
if (!hash || !input)
|
||||
return 1;
|
||||
if (!hash || !input)
|
||||
return 1;
|
||||
|
||||
mbedtls_sha256_context sctx;
|
||||
mbedtls_sha256_init(&sctx);
|
||||
mbedtls_sha256_starts(&sctx, 0); // SHA-256, not 224
|
||||
mbedtls_sha256_update(&sctx, input, length);
|
||||
mbedtls_sha256_finish(&sctx, hash);
|
||||
mbedtls_sha256_free(&sctx);
|
||||
mbedtls_sha256_context sctx;
|
||||
mbedtls_sha256_init(&sctx);
|
||||
mbedtls_sha256_starts(&sctx, 0); // SHA-256, not 224
|
||||
mbedtls_sha256_update(&sctx, input, length);
|
||||
mbedtls_sha256_finish(&sctx, hash);
|
||||
mbedtls_sha256_free(&sctx);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init_str(mbedtls_ecdsa_context *ctx, char * key_d, char *key_x, char *key_y) {
|
||||
if (!ctx)
|
||||
return 1;
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
int res;
|
||||
int res;
|
||||
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
return res;
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_string(&ctx->d, 16, key_d);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_string(&ctx->d, 16, key_d);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (key_x && key_y) {
|
||||
res = mbedtls_ecp_point_read_string(&ctx->Q, 16, key_x, key_y);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
if (key_x && key_y) {
|
||||
res = mbedtls_ecp_point_read_string(&ctx->Q, 16, key_x, key_y);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_init(mbedtls_ecdsa_context *ctx, uint8_t * key_d, uint8_t *key_xy) {
|
||||
if (!ctx)
|
||||
return 1;
|
||||
if (!ctx)
|
||||
return 1;
|
||||
|
||||
int res;
|
||||
int res;
|
||||
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
return res;
|
||||
mbedtls_ecdsa_init(ctx);
|
||||
res = mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_binary(&ctx->d, key_d, 32);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
if (key_d) {
|
||||
res = mbedtls_mpi_read_binary(&ctx->d, key_d, 32);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (key_xy) {
|
||||
res = mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, key_xy, 32 * 2 + 1);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
if (key_xy) {
|
||||
res = mbedtls_ecp_point_read_binary(&ctx->grp, &ctx->Q, key_xy, 32 * 2 + 1);
|
||||
if (res)
|
||||
return res;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_key_create(uint8_t * key_d, uint8_t *key_xy) {
|
||||
int res;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, NULL);
|
||||
int res;
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, NULL);
|
||||
|
||||
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "ecdsaproxmark";
|
||||
const char *pers = "ecdsaproxmark";
|
||||
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
res = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers));
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = mbedtls_ecdsa_genkey(&ctx, MBEDTLS_ECP_DP_SECP256R1, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
if (res)
|
||||
goto exit;
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = mbedtls_mpi_write_binary(&ctx.d, key_d, 32);
|
||||
if (res)
|
||||
goto exit;
|
||||
res = mbedtls_mpi_write_binary(&ctx.d, key_d, 32);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
size_t keylen = 0;
|
||||
uint8_t public_key[200] = {0};
|
||||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &keylen, public_key, sizeof(public_key));
|
||||
if (res)
|
||||
goto exit;
|
||||
size_t keylen = 0;
|
||||
uint8_t public_key[200] = {0};
|
||||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &keylen, public_key, sizeof(public_key));
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
if (keylen != 65) { // 0x04 <key x 32b><key y 32b>
|
||||
res = 1;
|
||||
goto exit;
|
||||
}
|
||||
memcpy(key_xy, public_key, 65);
|
||||
if (keylen != 65) { // 0x04 <key x 32b><key y 32b>
|
||||
res = 1;
|
||||
goto exit;
|
||||
}
|
||||
memcpy(key_xy, public_key, 65);
|
||||
|
||||
exit:
|
||||
mbedtls_entropy_free(&entropy);
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
char *ecdsa_get_error(int ret) {
|
||||
static char retstr[300];
|
||||
memset(retstr, 0x00, sizeof(retstr));
|
||||
mbedtls_strerror(ret, retstr, sizeof(retstr));
|
||||
return retstr;
|
||||
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 res = 0;
|
||||
size_t realkeylen = 0;
|
||||
if (keylen < 65)
|
||||
return 1;
|
||||
int res = 0;
|
||||
size_t realkeylen = 0;
|
||||
if (keylen < 65)
|
||||
return 1;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_init(&ctx);
|
||||
mbedtls_ecdsa_context ctx;
|
||||
mbedtls_ecdsa_init(&ctx);
|
||||
|
||||
res = mbedtls_ecp_group_load(&ctx.grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
goto exit;
|
||||
res = mbedtls_ecp_group_load(&ctx.grp, MBEDTLS_ECP_DP_SECP256R1); // secp256r1
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk) );
|
||||
if (res)
|
||||
goto exit;
|
||||
res = mbedtls_ecdsa_from_keypair(&ctx, mbedtls_pk_ec(*pk) );
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &realkeylen, key, keylen);
|
||||
if (realkeylen != 65)
|
||||
res = 2;
|
||||
res = mbedtls_ecp_point_write_binary(&ctx.grp, &ctx.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &realkeylen, key, keylen);
|
||||
if (realkeylen != 65)
|
||||
res = 2;
|
||||
exit:
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
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 res;
|
||||
*signaturelen = 0;
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "ecdsaproxmark";
|
||||
const char *pers = "ecdsaproxmark";
|
||||
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
res = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, strlen(pers));
|
||||
if (res)
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, key_d, key_xy);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, key_d, key_xy);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free(&ctr_drbg);
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
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 res;
|
||||
*signaturelen = 0;
|
||||
int res;
|
||||
*signaturelen = 0;
|
||||
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
int rndlen = 0;
|
||||
param_gethex_to_eol(random, 0, fixed_rand_value, sizeof(fixed_rand_value), &rndlen);
|
||||
int rndlen = 0;
|
||||
param_gethex_to_eol(random, 0, fixed_rand_value, sizeof(fixed_rand_value), &rndlen);
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, key_d, key_x, key_y);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, fixed_rand, NULL);
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, key_d, key_x, key_y);
|
||||
res = mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256, shahash, sizeof(shahash), signature, signaturelen, fixed_rand, NULL);
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
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 res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, NULL, key_x, key_y);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init_str(&ctx, NULL, key_x, key_y);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
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);
|
||||
if (res)
|
||||
return res;
|
||||
int res;
|
||||
uint8_t shahash[32] = {0};
|
||||
res = sha256hash(input, length, shahash);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, key_xy);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
mbedtls_ecdsa_context ctx;
|
||||
ecdsa_init(&ctx, NULL, key_xy);
|
||||
res = mbedtls_ecdsa_read_signature(&ctx, shahash, sizeof(shahash), signature, signaturelen);
|
||||
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
mbedtls_ecdsa_free(&ctx);
|
||||
return res;
|
||||
}
|
||||
|
||||
#define T_PRIVATE_KEY "C477F9F65C22CCE20657FAA5B2D1D8122336F851A508A1ED04E479C34985BF96"
|
||||
|
@ -322,86 +322,86 @@ int ecdsa_signature_verify(uint8_t *key_xy, uint8_t *input, int length, uint8_t
|
|||
#define T_S "DC42C2122D6392CD3E3A993A89502A8198C1886FE69D262C4B329BDB6B63FAF1"
|
||||
|
||||
int ecdsa_nist_test(bool verbose) {
|
||||
int res;
|
||||
uint8_t input[] = "Example of ECDSA with P-256";
|
||||
int length = strlen((char *)input);
|
||||
uint8_t signature[300] = {0};
|
||||
size_t siglen = 0;
|
||||
int res;
|
||||
uint8_t input[] = "Example of ECDSA with P-256";
|
||||
int length = strlen((char *)input);
|
||||
uint8_t signature[300] = {0};
|
||||
size_t siglen = 0;
|
||||
|
||||
// NIST ecdsa test
|
||||
if (verbose)
|
||||
printf(" ECDSA NIST test: ");
|
||||
// make signature
|
||||
res = ecdsa_signature_create_test(T_PRIVATE_KEY, T_Q_X, T_Q_Y, T_K, input, length, signature, &siglen);
|
||||
// printf("res: %x signature[%x]: %s\n", (res<0)?-res:res, siglen, sprint_hex(signature, siglen));
|
||||
if (res)
|
||||
goto exit;
|
||||
// NIST ecdsa test
|
||||
if (verbose)
|
||||
printf(" ECDSA NIST test: ");
|
||||
// make signature
|
||||
res = ecdsa_signature_create_test(T_PRIVATE_KEY, T_Q_X, T_Q_Y, T_K, input, length, signature, &siglen);
|
||||
// printf("res: %x signature[%x]: %s\n", (res<0)?-res:res, siglen, sprint_hex(signature, siglen));
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
// check vectors
|
||||
uint8_t rval[300] = {0};
|
||||
uint8_t sval[300] = {0};
|
||||
res = ecdsa_asn1_get_signature(signature, siglen, rval, sval);
|
||||
if (res)
|
||||
goto exit;
|
||||
// check vectors
|
||||
uint8_t rval[300] = {0};
|
||||
uint8_t sval[300] = {0};
|
||||
res = ecdsa_asn1_get_signature(signature, siglen, rval, sval);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
int slen = 0;
|
||||
uint8_t rval_s[33] = {0};
|
||||
param_gethex_to_eol(T_R, 0, rval_s, sizeof(rval_s), &slen);
|
||||
uint8_t sval_s[33] = {0};
|
||||
param_gethex_to_eol(T_S, 0, sval_s, sizeof(sval_s), &slen);
|
||||
if (strncmp((char *)rval, (char *)rval_s, 32) || strncmp((char *)sval, (char *)sval_s, 32)) {
|
||||
printf("R or S check error\n");
|
||||
res = 100;
|
||||
goto exit;
|
||||
}
|
||||
int slen = 0;
|
||||
uint8_t rval_s[33] = {0};
|
||||
param_gethex_to_eol(T_R, 0, rval_s, sizeof(rval_s), &slen);
|
||||
uint8_t sval_s[33] = {0};
|
||||
param_gethex_to_eol(T_S, 0, sval_s, sizeof(sval_s), &slen);
|
||||
if (strncmp((char *)rval, (char *)rval_s, 32) || strncmp((char *)sval, (char *)sval_s, 32)) {
|
||||
printf("R or S check error\n");
|
||||
res = 100;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
// verify signature
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
// verify signature
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
// verify wrong signature
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
if (!res) {
|
||||
res = 1;
|
||||
goto exit;
|
||||
}
|
||||
if (verbose)
|
||||
printf("passed\n");
|
||||
// verify wrong signature
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify_keystr(T_Q_X, T_Q_Y, input, length, signature, siglen);
|
||||
if (!res) {
|
||||
res = 1;
|
||||
goto exit;
|
||||
}
|
||||
if (verbose)
|
||||
printf("passed\n");
|
||||
|
||||
// random ecdsa test
|
||||
if (verbose)
|
||||
printf(" ECDSA binary signature create/check test: ");
|
||||
// random ecdsa test
|
||||
if (verbose)
|
||||
printf(" ECDSA binary signature create/check test: ");
|
||||
|
||||
uint8_t key_d[32] = {0};
|
||||
uint8_t key_xy[32 * 2 + 2] = {0};
|
||||
memset(signature, 0x00, sizeof(signature));
|
||||
siglen = 0;
|
||||
uint8_t key_d[32] = {0};
|
||||
uint8_t key_xy[32 * 2 + 2] = {0};
|
||||
memset(signature, 0x00, sizeof(signature));
|
||||
siglen = 0;
|
||||
|
||||
res = ecdsa_key_create(key_d, key_xy);
|
||||
if (res)
|
||||
goto exit;
|
||||
res = ecdsa_key_create(key_d, key_xy);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = ecdsa_signature_create(key_d, key_xy, input, length, signature, &siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
res = ecdsa_signature_create(key_d, key_xy, input, length, signature, &siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (res)
|
||||
goto exit;
|
||||
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (!res)
|
||||
goto exit;
|
||||
input[0] ^= 0xFF;
|
||||
res = ecdsa_signature_verify(key_xy, input, length, signature, siglen);
|
||||
if (!res)
|
||||
goto exit;
|
||||
|
||||
if (verbose)
|
||||
printf("passed\n\n");
|
||||
if (verbose)
|
||||
printf("passed\n\n");
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
exit:
|
||||
if (verbose)
|
||||
printf("failed\n\n");
|
||||
return res;
|
||||
if (verbose)
|
||||
printf("failed\n\n");
|
||||
return res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue