Fix EC templates on Stax

This commit is contained in:
Charles-Edouard de la Vergne 2024-02-26 11:05:34 +01:00
parent 285fbcfaf3
commit 4390142ffa
No known key found for this signature in database
GPG Key ID: F12296941B7BB9C6

View File

@ -280,82 +280,73 @@ static void template_key_cb(int token, uint8_t index) {
uint32_t size = 0; uint32_t size = 0;
uint8_t key_type = index + FIRST_USER_TOKEN; uint8_t key_type = index + FIRST_USER_TOKEN;
switch (token) { if (token != TOKEN_TYPE_BACK) {
case TOKEN_TYPE_BACK: memset(&attributes, 0, sizeof(attributes));
break; switch (key_type) {
case TOKEN_TYPE_RSA2048: case TOKEN_TYPE_RSA2048:
case TOKEN_TYPE_RSA3072: case TOKEN_TYPE_RSA3072:
case TOKEN_TYPE_RSA4096: case TOKEN_TYPE_RSA4096:
case TOKEN_TYPE_SECP256K1: switch (key_type) {
case TOKEN_TYPE_Ed25519: case TOKEN_TYPE_RSA2048:
memset(&attributes, 0, sizeof(attributes)); size = 2048;
switch (key_type) { break;
case TOKEN_TYPE_RSA2048: case TOKEN_TYPE_RSA3072:
case TOKEN_TYPE_RSA3072: size = 3072;
case TOKEN_TYPE_RSA4096: break;
switch (key_type) { case TOKEN_TYPE_RSA4096:
case TOKEN_TYPE_RSA2048: size = 4096;
size = 2048; break;
break; }
case TOKEN_TYPE_RSA3072: attributes.value[0] = KEY_ID_RSA;
size = 3072; U2BE_ENCODE(attributes.value, 1, size);
break; attributes.value[3] = 0x00;
case TOKEN_TYPE_RSA4096: attributes.value[4] = 0x20;
size = 4096; attributes.value[5] = 0x01;
break; attributes.length = 6;
} oid_len = 6;
attributes.value[0] = KEY_ID_RSA; break;
U2BE_ENCODE(attributes.value, 1, size);
attributes.value[3] = 0x00;
attributes.value[4] = 0x20;
attributes.value[5] = 0x01;
attributes.length = 6;
oid_len = 6;
break;
case TOKEN_TYPE_SECP256K1: case TOKEN_TYPE_SECP256K1:
if (G_gpg_vstate.ux_key == TOKEN_TEMPLATE_DEC) { if (G_gpg_vstate.ux_key == TOKEN_TEMPLATE_DEC) {
attributes.value[0] = KEY_ID_ECDH; attributes.value[0] = KEY_ID_ECDH;
} else { } else {
attributes.value[0] = KEY_ID_ECDSA; attributes.value[0] = KEY_ID_ECDSA;
} }
oid = gpg_curve2oid(CX_CURVE_SECP256R1, &oid_len); oid = gpg_curve2oid(CX_CURVE_SECP256R1, &oid_len);
memmove(attributes.value + 1, oid, sizeof(oid_len)); memmove(attributes.value + 1, oid, oid_len);
attributes.length = 1 + oid_len; attributes.length = 1 + oid_len;
break; break;
case TOKEN_TYPE_Ed25519: case TOKEN_TYPE_Ed25519:
if (G_gpg_vstate.ux_key == TOKEN_TEMPLATE_DEC) { if (G_gpg_vstate.ux_key == TOKEN_TEMPLATE_DEC) {
attributes.value[0] = KEY_ID_ECDH; attributes.value[0] = KEY_ID_ECDH;
oid = gpg_curve2oid(CX_CURVE_Curve25519, &oid_len); oid = gpg_curve2oid(CX_CURVE_Curve25519, &oid_len);
} else { } else {
attributes.value[0] = KEY_ID_EDDSA; attributes.value[0] = KEY_ID_EDDSA;
oid = gpg_curve2oid(CX_CURVE_Ed25519, &oid_len); oid = gpg_curve2oid(CX_CURVE_Ed25519, &oid_len);
} }
memmove(attributes.value + 1, oid, sizeof(oid_len)); memmove(attributes.value + 1, oid, oid_len);
attributes.length = 1 + oid_len; attributes.length = 1 + oid_len;
break; break;
} }
switch (G_gpg_vstate.ux_key) { switch (G_gpg_vstate.ux_key) {
case TOKEN_TEMPLATE_SIG: case TOKEN_TEMPLATE_SIG:
dest = &G_gpg_vstate.kslot->sig; dest = &G_gpg_vstate.kslot->sig;
break; break;
case TOKEN_TEMPLATE_DEC: case TOKEN_TEMPLATE_DEC:
dest = &G_gpg_vstate.kslot->dec; dest = &G_gpg_vstate.kslot->dec;
break; break;
case TOKEN_TEMPLATE_AUT: case TOKEN_TEMPLATE_AUT:
dest = &G_gpg_vstate.kslot->aut; dest = &G_gpg_vstate.kslot->aut;
break; break;
} }
if (dest && attributes.value[0] && if (dest && attributes.value[0] &&
memcmp(&dest->attributes, &attributes, sizeof(attributes)) != 0) { memcmp(&dest->attributes, &attributes, sizeof(attributes)) != 0) {
PRINTF("TEMPLATE NVM_WRITE!!!!!\n"); nvm_write(dest, NULL, sizeof(gpg_key_t));
nvm_write(dest, NULL, sizeof(gpg_key_t)); nvm_write(&dest->attributes, &attributes, sizeof(attributes));
nvm_write(&dest->attributes, &attributes, sizeof(attributes)); }
}
break;
} }
ui_settings_template(); ui_settings_template();
} }