Skip to content

Commit

Permalink
NSS_EDDSA flag for optional compiling of code for EDDSA support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh-Kumar-07 committed Aug 20, 2021
1 parent eb626ca commit ce1eddf
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/crypt_hash.h
Expand Up @@ -63,9 +63,9 @@ void crypt_hash_final_bytes(struct crypt_hash **hashp,

struct crypt_mac crypt_hash_final_mac(struct crypt_hash **hashp);

#ifdef NSS_EDDSA
void crypt_mac_load(struct crypt_mac *container, chunk_t packet);


#endif

/*
* Short cut for symkeys.
Expand Down
2 changes: 1 addition & 1 deletion include/crypt_mac.h
Expand Up @@ -36,7 +36,7 @@ struct crypt_mac {
/* size of the mac in bytes */
size_t len;
/* XXX: see note above about why this is called .ptr */
uint8_t ptr[2048/*see ike_alg_init() for size check*/];
uint8_t ptr[64/*see ike_alg_init() for size check*/];
};

extern const struct crypt_mac empty_mac;
Expand Down
2 changes: 2 additions & 0 deletions include/ietf_constants.h
Expand Up @@ -1658,7 +1658,9 @@ enum pubkey_alg {
PUBKEY_ALG_DSA = 1,
PUBKEY_ALG_RSA = 2,
PUBKEY_ALG_ECDSA = 3,
#ifdef NSS_EDDSA
PUBKEY_ALG_EDDSA = 4,
#endif
};

/*
Expand Down
4 changes: 3 additions & 1 deletion include/ike_alg.h
Expand Up @@ -489,8 +489,10 @@ struct hash_desc {
*/
shunk_t hash_asn1_blob_rsa;
shunk_t hash_asn1_blob_ecdsa;
shunk_t hash_asn1_blob_eddsa;

#ifdef NSS_EDDSA
shunk_t hash_asn1_blob_eddsa;
#endif
const struct hash_ops *hash_ops;
};

Expand Down
2 changes: 2 additions & 0 deletions include/ike_alg_hash.h
Expand Up @@ -25,7 +25,9 @@ extern const struct hash_desc ike_alg_hash_sha2_384;
extern const struct hash_desc ike_alg_hash_sha2_512;
#endif

#ifdef NSS_EDDSA
extern const struct hash_desc ike_alg_hash_identity;
#endif

#ifdef USE_MD5
extern const struct hash_desc ike_alg_hash_md5;
Expand Down
13 changes: 12 additions & 1 deletion include/pluto_constants.h
Expand Up @@ -102,7 +102,9 @@ enum keyword_authby {
AUTHBY_PSK,
AUTHBY_RSASIG,
AUTHBY_ECDSA,
#ifdef NSS_EDDSA
AUTHBY_EDDSA,
#endif
AUTHBY_NULL,
};

Expand Down Expand Up @@ -849,7 +851,9 @@ enum sa_policy_bits {
POLICY_PSK_IX = 0,
POLICY_RSASIG_IX = 1,
POLICY_ECDSA_IX = 2,
#ifdef NSS_EDDSA
POLICY_EDDSA_IX = 3,
#endif
POLICY_AUTH_NEVER_IX,
POLICY_AUTH_NULL_IX,

Expand Down Expand Up @@ -936,7 +940,9 @@ enum sa_policy_bits {
#define POLICY_PSK LELEM(POLICY_PSK_IX)
#define POLICY_RSASIG LELEM(POLICY_RSASIG_IX)
#define POLICY_ECDSA LELEM(POLICY_ECDSA_IX)
#ifdef NSS_EDDSA
#define POLICY_EDDSA LELEM(POLICY_EDDSA_IX)
#endif
#define POLICY_AUTH_NEVER LELEM(POLICY_AUTH_NEVER_IX)
#define POLICY_AUTH_NULL LELEM(POLICY_AUTH_NULL_IX)
#define POLICY_ENCRYPT LELEM(POLICY_ENCRYPT_IX) /* must be first of IPSEC policies */
Expand Down Expand Up @@ -997,22 +1003,27 @@ enum sa_policy_bits {
#define NEGOTIATE_AUTH_HASH_SHA2_256 LELEM(IKEv2_HASH_ALGORITHM_SHA2_256) /* rfc7427 does responder support SHA2-256? */
#define NEGOTIATE_AUTH_HASH_SHA2_384 LELEM(IKEv2_HASH_ALGORITHM_SHA2_384) /* rfc7427 does responder support SHA2-384? */
#define NEGOTIATE_AUTH_HASH_SHA2_512 LELEM(IKEv2_HASH_ALGORITHM_SHA2_512) /* rfc7427 does responder support SHA2-512? */
#ifdef NSS_EDDSA
#define NEGOTIATE_AUTH_HASH_IDENTITY LELEM(IKEv2_HASH_ALGORITHM_IDENTITY) /* rfc4307-bis does responder support IDENTITY? */
#endif

enum sighash_policy_bits {
POL_SIGHASH_SHA2_256_IX,
POL_SIGHASH_SHA2_384_IX,
POL_SIGHASH_SHA2_512_IX,
#ifdef NSS_EDDSA
POL_SIGHASH_IDENTITY_IX,
#endif
};

extern const struct enum_names sighash_policy_bit_names;

#define POL_SIGHASH_SHA2_256 LELEM(POL_SIGHASH_SHA2_256_IX)
#define POL_SIGHASH_SHA2_384 LELEM(POL_SIGHASH_SHA2_384_IX)
#define POL_SIGHASH_SHA2_512 LELEM(POL_SIGHASH_SHA2_512_IX)
#ifdef NSS_EDDSA
#define POL_SIGHASH_IDENTITY LELEM(POL_SIGHASH_IDENTITY_IX)

#endif
/* Default policy for now is using RSA - this might change to ECC */
#define POLICY_DEFAULT POLICY_RSASIG

Expand Down
2 changes: 2 additions & 0 deletions include/secrets.h
Expand Up @@ -167,7 +167,9 @@ struct pubkey_type {

extern const struct pubkey_type pubkey_type_rsa;
extern const struct pubkey_type pubkey_type_ecdsa;
#ifdef NSS_EDDSA
extern const struct pubkey_type pubkey_type_eddsa;
#endif

const struct pubkey_type *pubkey_alg_type(enum pubkey_alg alg);

Expand Down
14 changes: 12 additions & 2 deletions lib/libipsecconf/confread.c
Expand Up @@ -179,13 +179,21 @@ static void ipsecconf_default_values(struct starter_config *cfg)
d->ike_version = IKEv2;
d->policy =
POLICY_TUNNEL |
POLICY_ECDSA | POLICY_EDDSA | POLICY_RSASIG | POLICY_RSASIG_v1_5 | /* authby= */
POLICY_ECDSA | POLICY_RSASIG | POLICY_RSASIG_v1_5 | /* authby= */
POLICY_ENCRYPT | POLICY_PFS |
POLICY_IKE_FRAG_ALLOW | /* ike_frag=yes */
POLICY_ESN_NO; /* esn=no */

#ifdef NSS_EDDSA
d->policy |= POLICY_EDDSA;
#endif

d->sighash_policy =
POL_SIGHASH_SHA2_256 | POL_SIGHASH_SHA2_384 | POL_SIGHASH_SHA2_512| POL_SIGHASH_IDENTITY;
POL_SIGHASH_SHA2_256 | POL_SIGHASH_SHA2_384 | POL_SIGHASH_SHA2_512;

#ifdef NSS_EDDSA
d->sighash_policy |= POL_SIGHASH_IDENTITY;
#endif

d->left.host_family = &ipv4_info;
d->left.addr = ipv4_info.address.any;
Expand Down Expand Up @@ -1486,9 +1494,11 @@ static bool load_conn(struct starter_conn *conn,
} else if (streq(val, "ecdsa-sha1")) {
starter_error_append(perrl, "authby=ecdsa cannot use sha1, only sha2");
return TRUE;
#ifdef NSS_EDDSA
} else if (streq(val, "eddsa") || streq(val, "eddsa-identity")) {
conn->policy |= POLICY_EDDSA;
conn->sighash_policy |= POL_SIGHASH_IDENTITY;
#endif
} else {
starter_error_append(perrl, "connection authby= value is unknown");
return TRUE;
Expand Down
4 changes: 3 additions & 1 deletion lib/libswan/crypt_hash.c
Expand Up @@ -105,12 +105,14 @@ void crypt_hash_final_bytes(struct crypt_hash **hashp,
*hashp = hash = NULL;
}

#ifdef NSS_EDDSA
void crypt_mac_load(struct crypt_mac *container, chunk_t packet){
for(size_t i = 0; i < packet.len; i++){
(container->y)[i + container->len] = (packet.ptr)[i];
(container->ptr)[i + container->len] = (packet.ptr)[i];
}
container->len += packet.len;
}
#endif


struct crypt_mac crypt_hash_final_mac(struct crypt_hash **hashp)
Expand Down
2 changes: 2 additions & 0 deletions lib/libswan/ike_alg_sha2.c
Expand Up @@ -185,6 +185,7 @@ const struct hash_desc ike_alg_hash_sha2_384 = {
.hash_asn1_blob_ecdsa = THING_AS_HUNK(asn1_blob_ecdsa_sha2_384),
};

#ifdef NSS_EDDSA
static const uint8_t asn1_blob_eddsa_identity[] = { LEN_EDDSA_IDENTITY_BLOB, EDDSA_IDENTITY_BLOB };

const struct hash_desc ike_alg_hash_identity = {
Expand All @@ -199,6 +200,7 @@ const struct hash_desc ike_alg_hash_identity = {
},
.hash_asn1_blob_eddsa = THING_AS_HUNK(asn1_blob_eddsa_identity),
};
#endif

const struct prf_desc ike_alg_prf_sha2_384 = {
.common = {
Expand Down
14 changes: 14 additions & 0 deletions lib/libswan/secrets.c
Expand Up @@ -503,6 +503,7 @@ const struct pubkey_type pubkey_type_ecdsa = {
.extract_pubkey_content = EC_extract_pubkey_content,
};

#ifdef NSS_EDDSA
const struct pubkey_type pubkey_type_eddsa = {
.alg = PUBKEY_ALG_EDDSA,
.name = "EDDSA",
Expand All @@ -515,14 +516,17 @@ const struct pubkey_type pubkey_type_eddsa = {
.sign_hash = EC_sign_hash,
.extract_pubkey_content = EC_extract_pubkey_content,
};
#endif


const struct pubkey_type *pubkey_alg_type(enum pubkey_alg alg)
{
static const struct pubkey_type *pubkey_types[] = {
[PUBKEY_ALG_RSA] = &pubkey_type_rsa,
[PUBKEY_ALG_ECDSA] = &pubkey_type_ecdsa,
#ifdef NSS_EDDSA
[PUBKEY_ALG_EDDSA] = &pubkey_type_eddsa,
#endif
};
passert(alg < elemsof(pubkey_types));
const struct pubkey_type *type = pubkey_types[alg];
Expand All @@ -541,7 +545,9 @@ const keyid_t *pubkey_keyid(const struct pubkey *pk)
switch (pk->type->alg) {
case PUBKEY_ALG_RSA:
case PUBKEY_ALG_ECDSA:
#ifdef NSS_EDDSA
case PUBKEY_ALG_EDDSA:
#endif
return &pk->keyid;
default:
bad_case(pk->type->alg);
Expand Down Expand Up @@ -569,7 +575,9 @@ const keyid_t *secret_keyid(const struct secret *secret)
switch (secret->pks.pubkey_type->alg) {
case PUBKEY_ALG_RSA:
case PUBKEY_ALG_ECDSA:
#ifdef NSS_EDDSA
case PUBKEY_ALG_EDDSA:
#endif
return &secret->pks.keyid;
default:
bad_case(secret->pks.pubkey_type->alg);
Expand All @@ -584,7 +592,9 @@ unsigned pubkey_size(const struct pubkey *pk)
switch (pk->type->alg) {
case PUBKEY_ALG_RSA:
case PUBKEY_ALG_ECDSA:
#ifdef NSS_EDDSA
case PUBKEY_ALG_EDDSA:
#endif
return pk->size;
default:
bad_case(pk->type->alg);
Expand Down Expand Up @@ -1550,9 +1560,11 @@ static const struct pubkey_type *pubkey_type_nss(SECKEYPublicKey *pubk)
case rsaKey:
return &pubkey_type_rsa;
case ecKey:
#ifdef NSS_EDDSA
if (pk11_ECGetPubkeyEncoding(pubk) == ECPoint_XOnly)
return &pubkey_type_eddsa;
else
#endif
return &pubkey_type_ecdsa;
default:
return NULL;
Expand All @@ -1566,12 +1578,14 @@ static const struct pubkey_type *private_key_type_nss(SECKEYPrivateKey *private_
case rsaKey:
return &pubkey_type_rsa;
case ecKey:
#ifdef NSS_EDDSA
SECKEYPublicKey *pubk = SECKEY_ConvertToPublicKey(private_key);
if(pubk == NULL)
return NULL;
if (pk11_ECGetPubkeyEncoding(pubk) == ECPoint_XOnly)
return &pubkey_type_eddsa;
else
#endif
return &pubkey_type_ecdsa;
default:
return NULL;
Expand Down
6 changes: 6 additions & 0 deletions mk/config.mk
Expand Up @@ -731,6 +731,12 @@ ifeq ($(USE_NSS_KDF),true)
USERLAND_CFLAGS += -DUSE_NSS_KDF
endif

NSS_EDDSA ?= true
ifeq ($(NSS_EDDSA),true)
USERLAND_CFLAGS += -DNSS_EDDSA
endif


USERLAND_CFLAGS += -DDEFAULT_RUNDIR=\"$(FINALRUNDIR)\"
USERLAND_CFLAGS += -DIPSEC_CONF=\"$(FINALCONFFILE)\"
USERLAND_CFLAGS += -DIPSEC_CONFDDIR=\"$(FINALCONFDDIR)\"
Expand Down
3 changes: 3 additions & 0 deletions programs/pluto/Makefile
Expand Up @@ -219,6 +219,9 @@ endif
OBJS += ikev2.o ikev2_parent.o ikev2_child.o ikev2_spdb_struct.o
OBJS += ikev2_states.o
OBJS += ikev2_ecdsa.o ikev2_rsa.o ikev2_psk.o ikev2_ppk.o ikev2_crypto.o
ifeq ($(NSS_EDDSA),true)
OBJS += ikev2_eddsa.o
endif
OBJS += ikev2_redirect.o
OBJS += cert_decode_helper.o
OBJS += kernel.o
Expand Down

0 comments on commit ce1eddf

Please sign in to comment.