mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
Rename asn1 -> tlv, as it reflects better the purpose.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -323,7 +323,7 @@ list(APPEND PICOKEYS_SOURCES
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/src/rng/hwrng.c
|
${CMAKE_CURRENT_LIST_DIR}/src/rng/hwrng.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/eac.c
|
${CMAKE_CURRENT_LIST_DIR}/src/eac.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/crypto_utils.c
|
${CMAKE_CURRENT_LIST_DIR}/src/crypto_utils.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/asn1.c
|
${CMAKE_CURRENT_LIST_DIR}/src/tlv.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/apdu.c
|
${CMAKE_CURRENT_LIST_DIR}/src/apdu.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/rescue.c
|
${CMAKE_CURRENT_LIST_DIR}/src/rescue.c
|
||||||
${CMAKE_CURRENT_LIST_DIR}/src/serial.c
|
${CMAKE_CURRENT_LIST_DIR}/src/serial.c
|
||||||
|
|||||||
22
src/eac.c
22
src/eac.c
@@ -20,7 +20,7 @@
|
|||||||
#include "crypto_utils.h"
|
#include "crypto_utils.h"
|
||||||
#include "random.h"
|
#include "random.h"
|
||||||
#include "mbedtls/cmac.h"
|
#include "mbedtls/cmac.h"
|
||||||
#include "asn1.h"
|
#include "tlv.h"
|
||||||
#include "apdu.h"
|
#include "apdu.h"
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
#include "usb/emulation/emulation.h"
|
#include "usb/emulation/emulation.h"
|
||||||
@@ -118,9 +118,9 @@ int sm_unwrap(void) {
|
|||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag == 0x87 || tag == 0x85) {
|
if (tag == 0x87 || tag == 0x85) {
|
||||||
body = tag_data;
|
body = tag_data;
|
||||||
body_size = tag_len;
|
body_size = tag_len;
|
||||||
@@ -218,9 +218,9 @@ uint16_t sm_get_le(void) {
|
|||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag == 0x97) {
|
if (tag == 0x97) {
|
||||||
uint16_t le = 0;
|
uint16_t le = 0;
|
||||||
for (uint16_t t = 1; t <= tag_len; t++) {
|
for (uint16_t t = 1; t <= tag_len; t++) {
|
||||||
@@ -277,12 +277,12 @@ int sm_verify(void) {
|
|||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag & 0x1) {
|
if (tag & 0x1) {
|
||||||
input[input_len++] = (uint8_t)tag;
|
input[input_len++] = (uint8_t)tag;
|
||||||
uint8_t tlen = format_tlv_len(tag_len, input + input_len);
|
uint8_t tlen = tlv_format_len(tag_len, input + input_len);
|
||||||
input_len += tlen;
|
input_len += tlen;
|
||||||
memcpy(input + input_len, tag_data, tag_len);
|
memcpy(input + input_len, tag_data, tag_len);
|
||||||
input_len += tag_len;
|
input_len += tag_len;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
#include "picokeys.h"
|
#include "picokeys.h"
|
||||||
#include "file.h"
|
#include "file.h"
|
||||||
#include "asn1.h"
|
#include "tlv.h"
|
||||||
#include "apdu.h"
|
#include "apdu.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -377,9 +377,9 @@ uint16_t meta_find(uint16_t fid, uint8_t **out) {
|
|||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
tlv_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag_len < 2) {
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -402,16 +402,16 @@ int meta_delete(uint16_t fid) {
|
|||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
uint8_t *fdata = NULL;
|
uint8_t *fdata = NULL;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
tlv_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
uint8_t *tpos = p - tag_len - format_tlv_len(tag_len, NULL) - 1;
|
uint8_t *tpos = p - tag_len - tlv_format_len(tag_len, NULL) - 1;
|
||||||
if (tag_len < 2) {
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
uint16_t cfid = get_uint16_be(tag_data);
|
uint16_t cfid = get_uint16_be(tag_data);
|
||||||
if (cfid == fid) {
|
if (cfid == fid) {
|
||||||
uint16_t new_len = ctxi.len - 1 - tag_len - format_tlv_len(tag_len, NULL);
|
uint16_t new_len = ctxi.len - 1 - tag_len - tlv_format_len(tag_len, NULL);
|
||||||
if (new_len == 0) {
|
if (new_len == 0) {
|
||||||
flash_clear_file(ef);
|
flash_clear_file(ef);
|
||||||
}
|
}
|
||||||
@@ -447,9 +447,9 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
uint16_t tag_len = 0;
|
uint16_t tag_len = 0;
|
||||||
asn1_ctx_t ctxi;
|
tlv_ctx_t ctxi;
|
||||||
asn1_ctx_init(fdata, ef_size, &ctxi);
|
tlv_ctx_init(fdata, ef_size, &ctxi);
|
||||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag_len < 2) {
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -465,7 +465,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
return PICOKEYS_OK;
|
return PICOKEYS_OK;
|
||||||
}
|
}
|
||||||
else { //needs reallocation
|
else { //needs reallocation
|
||||||
uint8_t *tpos = p - asn1_len_tag(tag, tag_len);
|
uint8_t *tpos = p - tlv_len_tag(tag, tag_len);
|
||||||
memmove(tpos, p, fdata + ef_size - p);
|
memmove(tpos, p, fdata + ef_size - p);
|
||||||
tpos += fdata + ef_size - p;
|
tpos += fdata + ef_size - p;
|
||||||
volatile uintptr_t meta_offset = tpos - fdata;
|
volatile uintptr_t meta_offset = tpos - fdata;
|
||||||
@@ -482,7 +482,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
}
|
}
|
||||||
uint8_t *f = fdata + meta_offset;
|
uint8_t *f = fdata + meta_offset;
|
||||||
*f++ = fid & 0xff;
|
*f++ = fid & 0xff;
|
||||||
f += format_tlv_len(len + 2, f);
|
f += tlv_format_len(len + 2, f);
|
||||||
f += put_uint16_be(fid, f);
|
f += put_uint16_be(fid, f);
|
||||||
memcpy(f, data, len);
|
memcpy(f, data, len);
|
||||||
r = file_put_data(ef, fdata, ef_size);
|
r = file_put_data(ef, fdata, ef_size);
|
||||||
@@ -494,13 +494,13 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fdata = (uint8_t *) realloc(fdata, ef_size + asn1_len_tag(fid & 0x1f, len + 2));
|
fdata = (uint8_t *) realloc(fdata, ef_size + tlv_len_tag(fid & 0x1f, len + 2));
|
||||||
uint8_t *f = fdata + ef_size;
|
uint8_t *f = fdata + ef_size;
|
||||||
*f++ = fid & 0x1f;
|
*f++ = fid & 0x1f;
|
||||||
f += format_tlv_len(len + 2, f);
|
f += tlv_format_len(len + 2, f);
|
||||||
f += put_uint16_be(fid, f);
|
f += put_uint16_be(fid, f);
|
||||||
memcpy(f, data, len);
|
memcpy(f, data, len);
|
||||||
r = file_put_data(ef, fdata, ef_size + (uint16_t)asn1_len_tag(fid & 0x1f, len + 2));
|
r = file_put_data(ef, fdata, ef_size + (uint16_t)tlv_len_tag(fid & 0x1f, len + 2));
|
||||||
free(fdata);
|
free(fdata);
|
||||||
if (r != PICOKEYS_OK) {
|
if (r != PICOKEYS_OK) {
|
||||||
return PICOKEYS_EXEC_ERROR;
|
return PICOKEYS_EXEC_ERROR;
|
||||||
|
|||||||
@@ -16,9 +16,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "picokeys.h"
|
#include "picokeys.h"
|
||||||
#include "asn1.h"
|
#include "tlv.h"
|
||||||
|
|
||||||
int asn1_ctx_init(uint8_t *data, uint16_t len, asn1_ctx_t *ctx) {
|
int tlv_ctx_init(uint8_t *data, uint16_t len, tlv_ctx_t *ctx) {
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
return PICOKEYS_ERR_NULL_PARAM;
|
return PICOKEYS_ERR_NULL_PARAM;
|
||||||
}
|
}
|
||||||
@@ -27,20 +27,20 @@ int asn1_ctx_init(uint8_t *data, uint16_t len, asn1_ctx_t *ctx) {
|
|||||||
return PICOKEYS_OK;
|
return PICOKEYS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int asn1_ctx_clear(asn1_ctx_t *ctx) {
|
int tlv_ctx_clear(tlv_ctx_t *ctx) {
|
||||||
ctx->data = NULL;
|
ctx->data = NULL;
|
||||||
ctx->len = 0;
|
ctx->len = 0;
|
||||||
return PICOKEYS_OK;
|
return PICOKEYS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t asn1_len(asn1_ctx_t *ctx) {
|
uint16_t tlv_len(tlv_ctx_t *ctx) {
|
||||||
if (ctx->data && ctx->len > 0) {
|
if (ctx->data && ctx->len > 0) {
|
||||||
return ctx->len;
|
return ctx->len;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t asn1_get_uint(asn1_ctx_t *ctx) {
|
uint32_t tlv_get_uint(tlv_ctx_t *ctx) {
|
||||||
uint32_t d = ctx->data[0];
|
uint32_t d = ctx->data[0];
|
||||||
for (uint16_t lt = 1; lt < MIN(ctx->len, sizeof(uint32_t)); lt++) {
|
for (uint16_t lt = 1; lt < MIN(ctx->len, sizeof(uint32_t)); lt++) {
|
||||||
d <<= 8;
|
d <<= 8;
|
||||||
@@ -49,15 +49,15 @@ uint32_t asn1_get_uint(asn1_ctx_t *ctx) {
|
|||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t asn1_len_tag(uint16_t tag, uint16_t len) {
|
uint16_t tlv_len_tag(uint16_t tag, uint16_t len) {
|
||||||
uint16_t ret = 1 + format_tlv_len(len, NULL) + len;
|
uint16_t ret = 1 + tlv_format_len(len, NULL) + len;
|
||||||
if (tag > 0x00ff) {
|
if (tag > 0x00ff) {
|
||||||
return ret + 1;
|
return ret + 1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t format_tlv_len(uint16_t len, uint8_t *out) {
|
uint8_t tlv_format_len(uint16_t len, uint8_t *out) {
|
||||||
if (len < 128) {
|
if (len < 128) {
|
||||||
if (out) {
|
if (out) {
|
||||||
*out = (uint8_t)len;
|
*out = (uint8_t)len;
|
||||||
@@ -78,11 +78,7 @@ uint8_t format_tlv_len(uint16_t len, uint8_t *out) {
|
|||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
int walk_tlv(const asn1_ctx_t *ctxi,
|
int tlv_walk(const tlv_ctx_t *ctxi, uint8_t **p, uint16_t *tag, uint16_t *tag_len, uint8_t **data) {
|
||||||
uint8_t **p,
|
|
||||||
uint16_t *tag,
|
|
||||||
uint16_t *tag_len,
|
|
||||||
uint8_t **data) {
|
|
||||||
if (!p) {
|
if (!p) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -92,8 +88,7 @@ int walk_tlv(const asn1_ctx_t *ctxi,
|
|||||||
if (*p - ctxi->data >= ctxi->len) {
|
if (*p - ctxi->data >= ctxi->len) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
uint16_t tg = 0x0;
|
uint16_t tg = 0x0, tgl = 0;
|
||||||
uint16_t tgl = 0;
|
|
||||||
tg = *(*p)++;
|
tg = *(*p)++;
|
||||||
if ((tg & 0x1f) == 0x1f) {
|
if ((tg & 0x1f) == 0x1f) {
|
||||||
tg <<= 8;
|
tg <<= 8;
|
||||||
@@ -120,14 +115,10 @@ int walk_tlv(const asn1_ctx_t *ctxi,
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool asn1_find_tag(const asn1_ctx_t *ctxi,
|
bool tlv_find_tag(const tlv_ctx_t *ctxi, uint16_t itag, tlv_ctx_t *ctxo) {
|
||||||
uint16_t itag,
|
uint16_t tag = 0x0, tlen = 0;
|
||||||
asn1_ctx_t *ctxo) {
|
uint8_t *p = NULL, *tdata = NULL;
|
||||||
uint16_t tag = 0x0;
|
while (tlv_walk(ctxi, &p, &tag, &tlen, &tdata)) {
|
||||||
uint8_t *p = NULL;
|
|
||||||
uint8_t *tdata = NULL;
|
|
||||||
uint16_t tlen = 0;
|
|
||||||
while (walk_tlv(ctxi, &p, &tag, &tlen, &tdata)) {
|
|
||||||
if (itag == tag) {
|
if (itag == tag) {
|
||||||
if (ctxo != NULL) {
|
if (ctxo != NULL) {
|
||||||
ctxo->data = tdata;
|
ctxo->data = tdata;
|
||||||
@@ -15,33 +15,27 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _ASN1_H_
|
#ifndef _TLV_H_
|
||||||
#define _ASN1_H_
|
#define _TLV_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include "compat/compat.h"
|
#include "compat/compat.h"
|
||||||
|
|
||||||
PACK(
|
PACK(
|
||||||
typedef struct asn1_ctx {
|
typedef struct tlv_ctx {
|
||||||
uint8_t *data;
|
uint8_t *data;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
}) asn1_ctx_t;
|
}) tlv_ctx_t;
|
||||||
|
|
||||||
extern int asn1_ctx_init(uint8_t *, uint16_t, asn1_ctx_t *);
|
extern int tlv_ctx_init(uint8_t *, uint16_t, tlv_ctx_t *);
|
||||||
extern int asn1_ctx_clear(asn1_ctx_t *ctx);
|
extern int tlv_ctx_clear(tlv_ctx_t *ctx);
|
||||||
extern uint16_t asn1_len(asn1_ctx_t *ctx);
|
extern uint16_t tlv_len(tlv_ctx_t *ctx);
|
||||||
extern uint32_t asn1_get_uint(asn1_ctx_t *ctx);
|
extern uint32_t tlv_get_uint(tlv_ctx_t *ctx);
|
||||||
|
|
||||||
extern int walk_tlv(const asn1_ctx_t *ctxi,
|
extern int tlv_walk(const tlv_ctx_t *ctxi, uint8_t **p, uint16_t *tag, uint16_t *tag_len, uint8_t **data);
|
||||||
uint8_t **p,
|
extern uint8_t tlv_format_len(uint16_t len, uint8_t *out);
|
||||||
uint16_t *tag,
|
extern bool tlv_find_tag(const tlv_ctx_t *ctxi, uint16_t itag, tlv_ctx_t *ctxo);
|
||||||
uint16_t *tag_len,
|
extern uint16_t tlv_len_tag(uint16_t tag, uint16_t len);
|
||||||
uint8_t **data);
|
|
||||||
extern uint8_t format_tlv_len(uint16_t len, uint8_t *out);
|
|
||||||
extern bool asn1_find_tag(const asn1_ctx_t *ctxi,
|
|
||||||
uint16_t itag,
|
|
||||||
asn1_ctx_t *ctxo);
|
|
||||||
extern uint16_t asn1_len_tag(uint16_t tag, uint16_t len);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user