Skip to content

Commit

Permalink
tests: Fix untermined string initializaions
Browse files Browse the repository at this point in the history
The new GCC 15 reports error when the string initializers overflow the
size of the underlying structure. This is common when the byte strings
are constructed in quotes such as "\xBB" as such this string has
trailing null byte and therefore the size two.

This is not an issue in the tests as they do not expect the string to be
NULL terminated, but it might uncover issues in other cases.

Example of the error:

/builddir/build/BUILD/yubihsm-shell-2.6.0-build/yubihsm-shell-2.6.0/pkcs11/tests/aes_encrypt_test.c:38:3: error: initializer-string for array of ‘unsigned char’ is too long [-Werror=unterminated-string-initialization]
   38 |   "\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Feb 5, 2025
1 parent 146224a commit f345fa4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/encrypt_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

const char *key_label = "label";
const uint8_t password[] = "password";
const uint8_t plaintext[16] = "single block msg";
const uint8_t plaintext[16] = "singleblock msg";

int main(void) {
yh_connector *connector = NULL;
Expand Down
42 changes: 24 additions & 18 deletions examples/yubico_otp.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,30 @@ static const struct {
uint16_t crc;
uint8_t otp[32];
} test_vectors[] =
{{"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
"\x01\x02\x03\x04\x05\x06", 0x0001, 0x0001, 0x01, 0x01, 0x0000, 0xfe36,
"\x2f\x5d\x71\xa4\x91\x5d\xec\x30\x4a\xa1\x3c\xcf\x97\xbb\x0d\xbb"},
{"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
"\x01\x02\x03\x04\x05\x06", 0x0001, 0x0001, 0x01, 0x02, 0x0000, 0x1152,
"\xcb\x71\x0b\x46\x2b\x7b\x1c\x23\x10\x0c\xb2\x46\x85\xb6\x4d\x33"},
{"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
"\x01\x02\x03\x04\x05\x06", 0x0fff, 0x0001, 0x01, 0x01, 0x0000, 0x9454,
"\x77\x99\x78\x12\x9b\xcc\x26\x42\xc8\xad\xf5\xc1\x99\x81\xa0\x16"},
{"\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88",
"\x88\x88\x88\x88\x88\x88", 0x8888, 0x8888, 0x88, 0x88, 0x8888, 0xd3b6,
"\x20\x76\x5f\xc6\x83\xe0\xfc\x7b\x62\x42\x21\x86\x48\x4d\x82\x37"},
{"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
"\x00\x00\x00\x00\x00\x00", 0x0000, 0x0000, 0x00, 0x00, 0x0000, 0xa96a,
"\x99\x9b\x08\xbf\x0b\x3b\x98\xf8\x5b\x08\x76\xa8\x77\x15\x16\x16"},
{"\xc4\x42\x28\x90\x65\x30\x76\xcd\xe7\x3d\x44\x9b\x19\x1b\x41\x6a",
"\x33\xc6\x9e\x7f\x24\x9e", 0x0001, 0x13a7, 0x24, 0x00, 0xc63c, 0x1c86,
"\x7e\x0f\xc9\x87\x35\x16\x72\xc0\x70\xfa\x5c\x05\x95\xec\x68\xb8"}};
{{{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
0x0001, 0x0001, 0x01, 0x01, 0x0000, 0xfe36,
{0x2f, 0x5d, 0x71, 0xa4, 0x91, 0x5d, 0xec, 0x30, 0x4a, 0xa1, 0x3c, 0xcf, 0x97, 0xbb, 0x0d, 0xbb}},
{{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
0x0001, 0x0001, 0x01, 0x02, 0x0000, 0x1152,
{0xcb, 0x71, 0x0b, 0x46, 0x2b, 0x7b, 0x1c, 0x23, 0x10, 0x0c, 0xb2, 0x46, 0x85, 0xb6, 0x4d, 0x33}},
{{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
0x0fff, 0x0001, 0x01, 0x01, 0x0000, 0x9454,
{0x77, 0x99, 0x78, 0x12, 0x9b, 0xcc, 0x26, 0x42, 0xc8, 0xad, 0xf5, 0xc1, 0x99, 0x81, 0xa0, 0x16}},
{{0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88},
{0x88, 0x88, 0x88, 0x88, 0x88, 0x88},
0x8888, 0x8888, 0x88, 0x88, 0x8888, 0xd3b6,
{0x20, 0x76, 0x5f, 0xc6, 0x83, 0xe0, 0xfc, 0x7b, 0x62, 0x42, 0x21, 0x86, 0x48, 0x4d, 0x82, 0x37}},
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
0x0000, 0x0000, 0x00, 0x00, 0x0000, 0xa96a,
{0x99, 0x9b, 0x08, 0xbf, 0x0b, 0x3b, 0x98, 0xf8, 0x5b, 0x08, 0x76, 0xa8, 0x77, 0x15, 0x16, 0x16}},
{{0xc4, 0x42, 0x28, 0x90, 0x65, 0x30, 0x76, 0xcd, 0xe7, 0x3d, 0x44, 0x9b, 0x19, 0x1b, 0x41, 0x6a},
{0x33, 0xc6, 0x9e, 0x7f, 0x24, 0x9e},
0x0001, 0x13a7, 0x24, 0x00, 0xc63c, 0x1c86,
{0x7e, 0x0f, 0xc9, 0x87, 0x35, 0x16, 0x72, 0xc0, 0x70, 0xfa, 0x5c, 0x05, 0x95, 0xec, 0x68, 0xb8}}};

static uint16_t yubikey_crc16(const uint8_t *buf, size_t buf_size) {
uint16_t m_crc = 0xffff;
Expand Down
10 changes: 5 additions & 5 deletions lib/tests/test_parsing.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ static void test_capabilities1(void) {
const char *string;
yh_capabilities capabilities;
} tests[] = {
{"get-opaque", {"\x00\x00\x00\x00\x00\x00\x00\x01"}},
{"get-opaque", {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}},
{"sign-hmac:verify-hmac|exportable-under-wrap,",
{"\x00\x00\x00\x00\x00\xc1\x00\x00"}},
{",,unwrap-data|:wrap-data,,,", {"\x00\x00\x00\x60\x00\x00\x00\x00"}},
{"0x7fffffffffffffff", {"\x7f\xff\xff\xff\xff\xff\xff\xff"}},
{"0xffffffffffffffff", {"\xff\xff\xff\xff\xff\xff\xff\xff"}},
{{0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x00, 0x00}}},
{",,unwrap-data|:wrap-data,,,", {{0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00}}},
{"0x7fffffffffffffff", {{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}},
{"0xffffffffffffffff", {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}},
};

for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
Expand Down
20 changes: 11 additions & 9 deletions pkcs11/tests/aes_encrypt_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
// the plaintext blocks. Each row corresponds to a whole block.
// clang-format off
#define PLAINTEXT_LENGTH (4 * 16)
static uint8_t plaintext[PLAINTEXT_LENGTH] =
"\x6b\xc1\xbe\xe2\x2e\x40\x9f\x96\xe9\x3d\x7e\x11\x73\x93\x17\x2a"
"\xae\x2d\x8a\x57\x1e\x03\xac\x9c\x9e\xb7\x6f\xac\x45\xaf\x8e\x51"
"\x30\xc8\x1c\x46\xa3\x5c\xe4\x11\xe5\xfb\xc1\x19\x1a\x0a\x52\xef"
"\xf6\x9f\x24\x45\xdf\x4f\x9b\x17\xad\x2b\x41\x7b\xe6\x6c\x37\x10";
static uint8_t plaintext[PLAINTEXT_LENGTH] = {
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
};
// clang-format on

#define TEST_ECB(key, ptlen, ct) \
Expand All @@ -50,15 +51,16 @@ static uint8_t plaintext[PLAINTEXT_LENGTH] =

struct test {
CK_MECHANISM_TYPE mechanism;
uint8_t key[32];
uint8_t key[32 + 1];
uint8_t keylen;
size_t plaintext_len;
uint8_t ciphertext[sizeof(plaintext) + 16];
uint8_t ciphertext[sizeof(plaintext) + 16 + 1];
size_t ciphertext_len;
};

static uint8_t iv[16] =
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
static uint8_t iv[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};

// CKM_AES_{ECB,CBC} test vectors from NIST.
// CKM_AES_CBC_PAD calculated out-of-band.
Expand Down
10 changes: 5 additions & 5 deletions pkcs11/util_pkcs11.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ CK_RV get_mechanism_info(yubihsm_pkcs11_slot *slot, CK_MECHANISM_TYPE type,
#define PKCS11_LABEL_TAG 2
#define PKCS11_PUBKEY_ID_TAG 3
#define PKCS11_PUBKEY_LABEL_TAG 4
const char META_OBJECT_VERSION[4] = "MDB1";
const char META_OBJECT_VERSION[5] = "MDB1";

static uint16_t write_meta_item(uint8_t *target_value, uint8_t tag,
cka_meta_item *meta_item) {
Expand Down Expand Up @@ -763,11 +763,11 @@ static CK_RV read_meta_object(yubihsm_pkcs11_slot *slot, uint16_t opaque_id,
}

uint8_t *p = opaque_value;
if (memcmp(p, META_OBJECT_VERSION, sizeof(META_OBJECT_VERSION)) != 0) {
if (memcmp(p, META_OBJECT_VERSION, strlen(META_OBJECT_VERSION)) != 0) {
DBG_ERR("Meta object value has unexpected version");
return CKR_DATA_INVALID;
}
p += sizeof(META_OBJECT_VERSION);
p += strlen(META_OBJECT_VERSION);

meta_object->target_type = *p++;

Expand Down Expand Up @@ -931,8 +931,8 @@ CK_RV write_meta_object(yubihsm_pkcs11_slot *slot,
uint8_t opaque_value[YH_MSG_BUF_SIZE] = {0};
uint8_t *p = opaque_value;

memcpy(p, META_OBJECT_VERSION, sizeof(META_OBJECT_VERSION));
p += sizeof(META_OBJECT_VERSION);
memcpy(p, META_OBJECT_VERSION, strlen(META_OBJECT_VERSION));
p += strlen(META_OBJECT_VERSION);

*p++ = meta_object->target_type;

Expand Down

0 comments on commit f345fa4

Please sign in to comment.