mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-26 08:05:10 +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/eac.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/rescue.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 "random.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "asn1.h"
|
||||
#include "tlv.h"
|
||||
#include "apdu.h"
|
||||
#ifdef ENABLE_EMULATION
|
||||
#include "usb/emulation/emulation.h"
|
||||
@@ -118,9 +118,9 @@ int sm_unwrap(void) {
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x87 || tag == 0x85) {
|
||||
body = tag_data;
|
||||
body_size = tag_len;
|
||||
@@ -218,9 +218,9 @@ uint16_t sm_get_le(void) {
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x97) {
|
||||
uint16_t le = 0;
|
||||
for (uint16_t t = 1; t <= tag_len; t++) {
|
||||
@@ -277,12 +277,12 @@ int sm_verify(void) {
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag & 0x1) {
|
||||
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;
|
||||
memcpy(input + input_len, tag_data, tag_len);
|
||||
input_len += tag_len;
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
#include "picokeys.h"
|
||||
#include "file.h"
|
||||
#include "asn1.h"
|
||||
#include "tlv.h"
|
||||
#include "apdu.h"
|
||||
#include <stdio.h>
|
||||
|
||||
@@ -377,9 +377,9 @@ uint16_t meta_find(uint16_t fid, uint8_t **out) {
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag_len < 2) {
|
||||
continue;
|
||||
}
|
||||
@@ -402,16 +402,16 @@ int meta_delete(uint16_t fid) {
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
uint8_t *fdata = NULL;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
uint8_t *tpos = p - tag_len - format_tlv_len(tag_len, NULL) - 1;
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(file_get_data(ef), file_get_size(ef), &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
uint8_t *tpos = p - tag_len - tlv_format_len(tag_len, NULL) - 1;
|
||||
if (tag_len < 2) {
|
||||
continue;
|
||||
}
|
||||
uint16_t cfid = get_uint16_be(tag_data);
|
||||
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) {
|
||||
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;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(fdata, ef_size, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
tlv_ctx_t ctxi;
|
||||
tlv_ctx_init(fdata, ef_size, &ctxi);
|
||||
while (tlv_walk(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag_len < 2) {
|
||||
continue;
|
||||
}
|
||||
@@ -465,7 +465,7 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
||||
return PICOKEYS_OK;
|
||||
}
|
||||
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);
|
||||
tpos += fdata + ef_size - p;
|
||||
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;
|
||||
*f++ = fid & 0xff;
|
||||
f += format_tlv_len(len + 2, f);
|
||||
f += tlv_format_len(len + 2, f);
|
||||
f += put_uint16_be(fid, f);
|
||||
memcpy(f, data, len);
|
||||
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;
|
||||
*f++ = fid & 0x1f;
|
||||
f += format_tlv_len(len + 2, f);
|
||||
f += tlv_format_len(len + 2, f);
|
||||
f += put_uint16_be(fid, f);
|
||||
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);
|
||||
if (r != PICOKEYS_OK) {
|
||||
return PICOKEYS_EXEC_ERROR;
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
*/
|
||||
|
||||
#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) {
|
||||
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;
|
||||
}
|
||||
|
||||
int asn1_ctx_clear(asn1_ctx_t *ctx) {
|
||||
int tlv_ctx_clear(tlv_ctx_t *ctx) {
|
||||
ctx->data = NULL;
|
||||
ctx->len = 0;
|
||||
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) {
|
||||
return ctx->len;
|
||||
}
|
||||
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];
|
||||
for (uint16_t lt = 1; lt < MIN(ctx->len, sizeof(uint32_t)); lt++) {
|
||||
d <<= 8;
|
||||
@@ -49,15 +49,15 @@ uint32_t asn1_get_uint(asn1_ctx_t *ctx) {
|
||||
return d;
|
||||
}
|
||||
|
||||
uint16_t asn1_len_tag(uint16_t tag, uint16_t len) {
|
||||
uint16_t ret = 1 + format_tlv_len(len, NULL) + len;
|
||||
uint16_t tlv_len_tag(uint16_t tag, uint16_t len) {
|
||||
uint16_t ret = 1 + tlv_format_len(len, NULL) + len;
|
||||
if (tag > 0x00ff) {
|
||||
return ret + 1;
|
||||
}
|
||||
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 (out) {
|
||||
*out = (uint8_t)len;
|
||||
@@ -78,11 +78,7 @@ uint8_t format_tlv_len(uint16_t len, uint8_t *out) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
int walk_tlv(const asn1_ctx_t *ctxi,
|
||||
uint8_t **p,
|
||||
uint16_t *tag,
|
||||
uint16_t *tag_len,
|
||||
uint8_t **data) {
|
||||
int tlv_walk(const tlv_ctx_t *ctxi, uint8_t **p, uint16_t *tag, uint16_t *tag_len, uint8_t **data) {
|
||||
if (!p) {
|
||||
return 0;
|
||||
}
|
||||
@@ -92,8 +88,7 @@ int walk_tlv(const asn1_ctx_t *ctxi,
|
||||
if (*p - ctxi->data >= ctxi->len) {
|
||||
return 0;
|
||||
}
|
||||
uint16_t tg = 0x0;
|
||||
uint16_t tgl = 0;
|
||||
uint16_t tg = 0x0, tgl = 0;
|
||||
tg = *(*p)++;
|
||||
if ((tg & 0x1f) == 0x1f) {
|
||||
tg <<= 8;
|
||||
@@ -120,14 +115,10 @@ int walk_tlv(const asn1_ctx_t *ctxi,
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool asn1_find_tag(const asn1_ctx_t *ctxi,
|
||||
uint16_t itag,
|
||||
asn1_ctx_t *ctxo) {
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *p = NULL;
|
||||
uint8_t *tdata = NULL;
|
||||
uint16_t tlen = 0;
|
||||
while (walk_tlv(ctxi, &p, &tag, &tlen, &tdata)) {
|
||||
bool tlv_find_tag(const tlv_ctx_t *ctxi, uint16_t itag, tlv_ctx_t *ctxo) {
|
||||
uint16_t tag = 0x0, tlen = 0;
|
||||
uint8_t *p = NULL, *tdata = NULL;
|
||||
while (tlv_walk(ctxi, &p, &tag, &tlen, &tdata)) {
|
||||
if (itag == tag) {
|
||||
if (ctxo != NULL) {
|
||||
ctxo->data = tdata;
|
||||
@@ -15,33 +15,27 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _ASN1_H_
|
||||
#define _ASN1_H_
|
||||
#ifndef _TLV_H_
|
||||
#define _TLV_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "compat/compat.h"
|
||||
|
||||
PACK(
|
||||
typedef struct asn1_ctx {
|
||||
typedef struct tlv_ctx {
|
||||
uint8_t *data;
|
||||
uint16_t len;
|
||||
}) asn1_ctx_t;
|
||||
}) tlv_ctx_t;
|
||||
|
||||
extern int asn1_ctx_init(uint8_t *, uint16_t, asn1_ctx_t *);
|
||||
extern int asn1_ctx_clear(asn1_ctx_t *ctx);
|
||||
extern uint16_t asn1_len(asn1_ctx_t *ctx);
|
||||
extern uint32_t asn1_get_uint(asn1_ctx_t *ctx);
|
||||
extern int tlv_ctx_init(uint8_t *, uint16_t, tlv_ctx_t *);
|
||||
extern int tlv_ctx_clear(tlv_ctx_t *ctx);
|
||||
extern uint16_t tlv_len(tlv_ctx_t *ctx);
|
||||
extern uint32_t tlv_get_uint(tlv_ctx_t *ctx);
|
||||
|
||||
extern int walk_tlv(const asn1_ctx_t *ctxi,
|
||||
uint8_t **p,
|
||||
uint16_t *tag,
|
||||
uint16_t *tag_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);
|
||||
extern int tlv_walk(const tlv_ctx_t *ctxi, uint8_t **p, uint16_t *tag, uint16_t *tag_len, uint8_t **data);
|
||||
extern uint8_t tlv_format_len(uint16_t len, uint8_t *out);
|
||||
extern bool tlv_find_tag(const tlv_ctx_t *ctxi, uint16_t itag, tlv_ctx_t *ctxo);
|
||||
extern uint16_t tlv_len_tag(uint16_t tag, uint16_t len);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user