Skip to content

Hacl ecdsa #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: hacl-sha3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/crypto-test-harness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: Linux

on:
pull_request:
branches: ["cf-zeta"]
workflow_dispatch:

jobs:
Expand Down
16 changes: 14 additions & 2 deletions crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ config CRYPTO_ECDSA
tristate "ECDSA (Elliptic Curve Digital Signature Algorithm)"
select CRYPTO_ECC
select CRYPTO_AKCIPHER
select CRYPTO_DRBG_HMAC
select ASN1
help
ECDSA (Elliptic Curve Digital Signature Algorithm) (FIPS 186,
ISO/IEC 14888-3)
using curves P-192, P-256, and P-384

Only signature verification is implemented.
P-256 uses formally a verified implementation from HACL*

config CRYPTO_ECRDSA
tristate "EC-RDSA (Elliptic Curve Russian Digital Signature Algorithm)"
Expand Down Expand Up @@ -1026,6 +1026,12 @@ config CRYPTO_SHA256
This is required for IPsec AH (XFRM_AH) and IPsec ESP (XFRM_ESP).
Used by the btrfs filesystem, Ceph, NFS, and SMB.

config CRYPTO_SHA2_HACL
tristate "SHA-224 and SHA-256 and SHA-384 and SHA-512"
select CRYPTO_HASH
help
SHA-2 secure hash algorithms (FIPS 180, ISO/IEC 10118-3) from HACL*

config CRYPTO_SHA512
tristate "SHA-384 and SHA-512"
select CRYPTO_HASH
Expand All @@ -1038,6 +1044,12 @@ config CRYPTO_SHA3
help
SHA-3 secure hash algorithms (FIPS 202, ISO/IEC 10118-3)

config CRYPTO_SHA3_HACL
tristate "SHA-3"
select CRYPTO_HASH
help
SHA-3 secure hash algorithms (FIPS 202, ISO/IEC 10118-3) from HACL*

config CRYPTO_SM3
tristate

Expand Down
8 changes: 6 additions & 2 deletions crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ sm2_generic-y += sm2.o

obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o

$(obj)/ecprivkey.asn1.o: $(obj)/ecprivkey.asn1.c $(obj)/ecprivkey.asn1.h
$(obj)/ecdsasignature.asn1.o: $(obj)/ecdsasignature.asn1.c $(obj)/ecdsasignature.asn1.h
$(obj)/ecdsa.o: $(obj)/ecdsasignature.asn1.h
$(obj)/ecdsa.o: $(obj)/ecdsasignature.asn1.h $(obj)/ecprivkey.asn1.h
ecdsa_generic-y += ecdsa.o
ecdsa_generic-y += ecprivkey.asn1.o
ecdsa_generic-y += ecdsasignature.asn1.o
obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o
obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o p256-hacl-generated.o

crypto_acompress-y := acompress.o
crypto_acompress-y += scompress.o
Expand All @@ -77,8 +79,10 @@ obj-$(CONFIG_CRYPTO_MD5) += md5.o
obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
obj-$(CONFIG_CRYPTO_SHA2_HACL) += sha2-hacl-generated.o sha2-hacl.o
obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
obj-$(CONFIG_CRYPTO_SHA3) += sha3_generic.o
obj-$(CONFIG_CRYPTO_SHA3_HACL) += sha3-hacl-generated.o sha3-hacl.o
obj-$(CONFIG_CRYPTO_SM3) += sm3.o
obj-$(CONFIG_CRYPTO_SM3_GENERIC) += sm3_generic.o
obj-$(CONFIG_CRYPTO_STREEBOG) += streebog_generic.o
Expand Down
2 changes: 1 addition & 1 deletion crypto/asymmetric_keys/pkcs8.asn1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ Attribute ::= ANY

AlgorithmIdentifier ::= SEQUENCE {
algorithm OBJECT IDENTIFIER ({ pkcs8_note_OID }),
parameters ANY OPTIONAL
parameters ANY OPTIONAL ({ pkcs8_note_algo_parameter })
}
53 changes: 46 additions & 7 deletions crypto/asymmetric_keys/pkcs8_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ struct pkcs8_parse_context {
struct public_key *pub;
unsigned long data; /* Start of data */
enum OID last_oid; /* Last OID encountered */
enum OID algo_oid; /* Algorithm OID */
u32 key_size;
const void *key;
const void *algo_param;
u32 algo_param_len;
};

/*
Expand All @@ -47,6 +48,17 @@ int pkcs8_note_OID(void *context, size_t hdrlen,
return 0;
}

int pkcs8_note_algo_parameter(void *context, size_t hdrlen,
unsigned char tag,
const void *value, size_t vlen)
{
struct pkcs8_parse_context *ctx = context;

ctx->algo_param = value;
ctx->algo_param_len = vlen;
return 0;
}

/*
* Note the version number of the ASN.1 blob.
*/
Expand All @@ -70,12 +82,39 @@ int pkcs8_note_algo(void *context, size_t hdrlen,
{
struct pkcs8_parse_context *ctx = context;

if (ctx->last_oid != OID_rsaEncryption)
return -ENOPKG;

ctx->pub->pkey_algo = "rsa";
return 0;
}
enum OID curve_id;

switch (ctx->last_oid) {
case OID_id_ecPublicKey:
if (!ctx->algo_param || ctx->algo_param_len == 0)
return -EBADMSG;
curve_id = look_up_OID(ctx->algo_param, ctx->algo_param_len);

switch (curve_id) {
case OID_id_prime192v1:
ctx->pub->pkey_algo = "ecdsa-nist-p192";
break;
case OID_id_prime256v1:
ctx->pub->pkey_algo = "ecdsa-nist-p256";
break;
case OID_id_ansip384r1:
ctx->pub->pkey_algo = "ecdsa-nist-p384";
break;
default:
return -ENOPKG;
}
break;

case OID_rsaEncryption:
ctx->pub->pkey_algo = "rsa";
break;

default:
return -ENOPKG;
}

return 0;
}

/*
* Note the key data of the ASN.1 blob.
Expand Down
9 changes: 6 additions & 3 deletions crypto/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
/* Computes result = (left + right) % mod.
* Assumes that left < mod and right < mod, result != mod.
*/
static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
const u64 *mod, unsigned int ndigits)
{
u64 carry;
Expand All @@ -501,6 +501,7 @@ static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
if (carry || vli_cmp(result, mod, ndigits) >= 0)
vli_sub(result, result, mod, ndigits);
}
EXPORT_SYMBOL(vli_mod_add);

/* Computes result = (left - right) % mod.
* Assumes that left < mod and right < mod, result != mod.
Expand Down Expand Up @@ -963,14 +964,15 @@ void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
EXPORT_SYMBOL(vli_mod_mult_slow);

/* Computes result = (left * right) % curve_prime. */
static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
const struct ecc_curve *curve)
{
u64 product[2 * ECC_MAX_DIGITS];

vli_mult(product, left, right, curve->g.ndigits);
vli_mmod_fast(result, product, curve);
}
EXPORT_SYMBOL(vli_mod_mult_fast);

/* Computes result = left^2 % curve_prime. */
static void vli_mod_square_fast(u64 *result, const u64 *left,
Expand Down Expand Up @@ -1277,7 +1279,7 @@ static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
vli_set(x1, t7, ndigits);
}

static void ecc_point_mult(struct ecc_point *result,
void ecc_point_mult(struct ecc_point *result,
const struct ecc_point *point, const u64 *scalar,
u64 *initial_z, const struct ecc_curve *curve,
unsigned int ndigits)
Expand Down Expand Up @@ -1335,6 +1337,7 @@ static void ecc_point_mult(struct ecc_point *result,
vli_set(result->x, rx[0], ndigits);
vli_set(result->y, ry[0], ndigits);
}
EXPORT_SYMBOL(ecc_point_mult);

/* Computes R = P + Q mod p */
static void ecc_point_add(const struct ecc_point *result,
Expand Down
Loading