freebsd-dev/contrib/libfido2/src/iso7816.c
Jessica Clarke 224a95f124 libfido2: Address CHERI compatibility
Cherry-picked from libfido2 upstream f20a735c0a6f:

iso7816: Avoid storing pointers in a packed structure

On CHERI, and thus Arm's experimental Morello prototype architecture,
pointers are represented as capabilities, which are unforgeable bounded
pointers, providing always-on fine-grained spatial memory safety. The
unforgeability is enforced through the use of tagged memory, with one
validity tag bit per capability-sized-and-aligned word in memory. This
means that storing a pointer to an unaligned location, which is not
guaranteed to work per the C standard, either traps or results in the
capability losing its tag (and thus never being dereferenceable again),
depending on how exactly the store is done (specifically, whether a
capability store or memcpy is used).

However, iso7816 itself does not need to be packed, and doing so likely
causes inefficiencies on existing architectures. The iso7816_header_t
member is packed, and the flexible payload array is a uint8_t (which by
definition has no padding bits and is exactly 8 bits in size and, since
CHAR_BITS must be at least 8, its existence implies that it has the same
representation as unsigned char, and that it has size and alignment 1)
so there will never be any padding inserted between header and payload
(but payload may overlap with padding at the end of the struct due to
how flexible arrays work, which means we need to be careful about our
calculations).

Co-authored-by: pedro martelletto <pedro@yubico.com>
2021-10-06 21:40:26 -04:00

65 lines
1.5 KiB
C

/*
* Copyright (c) 2018 Yubico AB. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
#include "fido.h"
iso7816_apdu_t *
iso7816_new(uint8_t cla, uint8_t ins, uint8_t p1, uint16_t payload_len)
{
iso7816_apdu_t *apdu;
size_t alloc_len;
alloc_len = sizeof(iso7816_apdu_t) + payload_len + 2; /* le1 le2 */
if ((apdu = calloc(1, alloc_len)) == NULL)
return NULL;
apdu->alloc_len = alloc_len;
apdu->payload_len = payload_len;
apdu->payload_ptr = apdu->payload;
apdu->header.cla = cla;
apdu->header.ins = ins;
apdu->header.p1 = p1;
apdu->header.lc2 = (uint8_t)((payload_len >> 8) & 0xff);
apdu->header.lc3 = (uint8_t)(payload_len & 0xff);
return apdu;
}
void
iso7816_free(iso7816_apdu_t **apdu_p)
{
iso7816_apdu_t *apdu;
if (apdu_p == NULL || (apdu = *apdu_p) == NULL)
return;
freezero(apdu, apdu->alloc_len);
*apdu_p = NULL;
}
int
iso7816_add(iso7816_apdu_t *apdu, const void *buf, size_t cnt)
{
if (cnt > apdu->payload_len || cnt > UINT16_MAX)
return -1;
memcpy(apdu->payload_ptr, buf, cnt);
apdu->payload_ptr += cnt;
apdu->payload_len = (uint16_t)(apdu->payload_len - cnt);
return 0;
}
const unsigned char *
iso7816_ptr(const iso7816_apdu_t *apdu)
{
return (const unsigned char *)&apdu->header;
}
size_t
iso7816_len(const iso7816_apdu_t *apdu)
{
return apdu->alloc_len - offsetof(iso7816_apdu_t, header) -
(sizeof(iso7816_apdu_t) - offsetof(iso7816_apdu_t, payload));
}