mirror of
https://git.kernel.org/pub/scm/network/wireless/iwd.git
synced 2024-11-22 06:29:23 +01:00
dpp-util: add dpp_point_to_asn1
Converts an l_ecc_point to the DPP ASN.1 structure.
This commit is contained in:
parent
484dea8d7a
commit
5ab6566a3b
@ -361,3 +361,93 @@ bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce,
|
||||
/* ke = HKDF-Expand(bk, "DPP Key", length) */
|
||||
return hkdf_expand(sha, bk, key_len, "DPP Key", ke, key_len);
|
||||
}
|
||||
|
||||
#define ASN1_ID(class, pc, tag) (((class) << 6) | ((pc) << 5) | (tag))
|
||||
|
||||
#define ASN1_ID_SEQUENCE ASN1_ID(0, 1, 0x10)
|
||||
#define ASN1_ID_BIT_STRING ASN1_ID(0, 0, 0x03)
|
||||
#define ASN1_ID_OID ASN1_ID(0, 0, 0x06)
|
||||
|
||||
/*
|
||||
* Values derived from OID definitions in https://www.secg.org/sec2-v2.pdf
|
||||
* Appendix A.2.1
|
||||
*
|
||||
* 1.2.840.10045.2.1 (ecPublicKey)
|
||||
*/
|
||||
static uint8_t ec_oid[] = { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 };
|
||||
|
||||
/* 1.2.840.10045.3.1.7 (prime256v1) */
|
||||
static uint8_t ec_p256_oid[] = { 0x2a, 0x86, 0x48, 0xce,
|
||||
0x3d, 0x03, 0x01, 0x07 };
|
||||
/* 1.3.132.0.34 (secp384r1) */
|
||||
static uint8_t ec_p384_oid[] = { 0x2B, 0x81, 0x04, 0x00, 0x22 };
|
||||
|
||||
uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out)
|
||||
{
|
||||
uint8_t *asn1;
|
||||
uint8_t *ptr;
|
||||
uint8_t *type_oid;
|
||||
size_t type_oid_len;
|
||||
const struct l_ecc_curve *curve = l_ecc_point_get_curve(p);
|
||||
ssize_t key_size = l_ecc_curve_get_scalar_bytes(curve);
|
||||
uint64_t x[L_ECC_MAX_DIGITS];
|
||||
ssize_t ret;
|
||||
size_t len;
|
||||
|
||||
switch (key_size) {
|
||||
case 32:
|
||||
type_oid = ec_p256_oid;
|
||||
type_oid_len = sizeof(ec_p256_oid);
|
||||
break;
|
||||
case 48:
|
||||
type_oid = ec_p384_oid;
|
||||
type_oid_len = sizeof(ec_p384_oid);
|
||||
break;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = l_ecc_point_get_x(p, x, sizeof(x));
|
||||
if (ret < 0 || ret != key_size)
|
||||
return NULL;
|
||||
|
||||
len = 2 + sizeof(ec_oid) + 2 + type_oid_len + 2 + key_size + 4;
|
||||
|
||||
if (L_WARN_ON(len > 128))
|
||||
return NULL;
|
||||
|
||||
asn1 = l_malloc(len + 2);
|
||||
ptr = asn1;
|
||||
|
||||
*ptr++ = ASN1_ID_SEQUENCE;
|
||||
/* Length of both OIDs and key, plus tag/len bytes */
|
||||
*ptr++ = len;
|
||||
|
||||
*ptr++ = ASN1_ID_SEQUENCE;
|
||||
|
||||
len = sizeof(ec_oid) + type_oid_len + 4;
|
||||
|
||||
*ptr++ = len;
|
||||
|
||||
*ptr++ = ASN1_ID_OID;
|
||||
*ptr++ = sizeof(ec_oid);
|
||||
memcpy(ptr, ec_oid, sizeof(ec_oid));
|
||||
ptr += sizeof(ec_oid);
|
||||
|
||||
*ptr++ = ASN1_ID_OID;
|
||||
*ptr++ = type_oid_len;
|
||||
memcpy(ptr, type_oid, type_oid_len);
|
||||
ptr += type_oid_len;
|
||||
|
||||
*ptr++ = ASN1_ID_BIT_STRING;
|
||||
*ptr++ = key_size + 2;
|
||||
*ptr++ = 0x00;
|
||||
*ptr++ = 0x03;
|
||||
memcpy(ptr, x, key_size);
|
||||
ptr += key_size;
|
||||
|
||||
if (len_out)
|
||||
*len_out = ptr - asn1;
|
||||
|
||||
return asn1;
|
||||
}
|
||||
|
@ -137,3 +137,5 @@ struct l_ecc_scalar *dpp_derive_k2(const struct l_ecc_point *i_proto_public,
|
||||
bool dpp_derive_ke(const uint8_t *i_nonce, const uint8_t *r_nonce,
|
||||
struct l_ecc_scalar *m, struct l_ecc_scalar *n,
|
||||
void *ke);
|
||||
|
||||
uint8_t *dpp_point_to_asn1(const struct l_ecc_point *p, size_t *len_out);
|
||||
|
Loading…
Reference in New Issue
Block a user