Skip to content
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

Add Signing Tool with Verified implementations for RSA as well as ECDSA with P-256 and P-384 #11

Open
wants to merge 54 commits into
base: cf-zeta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
8d981b4
zeta: updating format rule and removing linter rule.
armfazh Oct 16, 2023
2e040b6
HACL SHA2 code
franziskuskiefer Oct 5, 2023
fe222e6
Add HACL SHA2 to Makefile and config
franziskuskiefer Oct 5, 2023
ade4975
addressed review comments
karthikbhargavan Oct 13, 2023
fec0bdc
propagating errors
karthikbhargavan Oct 13, 2023
350282d
Formatting files according to clang-format.
armfazh Oct 16, 2023
3e1e8d2
Updating comment style.
armfazh Oct 16, 2023
d9921ac
Adding CRYPTO_SHA2_HACL module.
armfazh Oct 16, 2023
b8d989e
hacl sha3
franziskuskiefer Oct 24, 2023
9065b58
run ci
franziskuskiefer Oct 24, 2023
0b5753a
ecdsa sign (unverified)
franziskuskiefer Nov 24, 2023
f7ae697
hacl p256 ecdsa wip
franziskuskiefer Nov 25, 2023
a9a0412
add hacl p256 ecdsa
franziskuskiefer Nov 26, 2023
edfbb8e
drop debug prints
franziskuskiefer Nov 26, 2023
e4969f0
raw rsa first draft
karthikbhargavan Nov 28, 2023
c7a5b04
msvc
karthikbhargavan Nov 29, 2023
5bbf9c5
bignum
karthikbhargavan Nov 29, 2023
617a78e
hacl rsa compiles
karthikbhargavan Nov 30, 2023
b4168a7
config - remove again
franziskuskiefer Nov 30, 2023
94e9608
ecdsa fix
karthikbhargavan Dec 1, 2023
d21ad90
Adding Go tool for measuring RSA from user land. (#8)
armfazh Feb 18, 2024
bef27bb
rsa edits
karthikbhargavan Mar 18, 2024
75145cd
Benchmark in-kernel verification.
armfazh Mar 18, 2024
e2fe005
rsa from hacl - benchmarked
karthikbhargavan Mar 19, 2024
8fc2d4e
added separate options for hacl and generic implementation
karthikbhargavan Mar 19, 2024
ffa185e
performance improvements
karthikbhargavan Jun 3, 2024
3ea58d2
add ecdsa benchmark stub
keks Jul 24, 2024
34c5280
add debug session
keks Jul 24, 2024
8e2bdcf
add debug output and make running easier
keks Jul 29, 2024
128e82b
build bin symlinks dynamically
keks Jul 29, 2024
f2bf364
fix signature generation, add test in go
keks Jul 30, 2024
c965c84
add sign-and-verify-in-kernel test
keks Jul 30, 2024
379d480
clean up benchmark
keks Jul 31, 2024
df32c9f
locking goroutines to os thread fixes permission denied errors
keks Jul 31, 2024
436cade
don't always start dmesg
keks Jul 31, 2024
2c48fb0
virtme launch script: don't ignore log levels
keks Jul 31, 2024
2588aab
clean up debug prints
keks Jul 31, 2024
3cd7d11
add code for testing (non-hacl) kernel ecdsa-p384
keks Jul 31, 2024
b9dc949
add broken ecdsa p-384 benchmark
keks Jul 31, 2024
af9c017
add test for signing with ecdsa p384
keks Jul 31, 2024
cdb76b1
add first draft of hacl ecdsa P384
keks Sep 10, 2024
997b333
fixes
keks Sep 10, 2024
33d4e1d
it's noisy (debug output wise) but it works
keks Sep 11, 2024
40584b9
add signing tool go library, tests and benchmarks
keks Sep 12, 2024
7509a5b
also build go signing tool and install into vm
keks Sep 12, 2024
61625eb
remove some prints
keks Sep 12, 2024
5752a56
zeta/signing_tool: lock os thread in benchmarks
keks Sep 16, 2024
552eabe
add kernel config for enabling and disabling hacl* for ecdsa
keks Sep 16, 2024
5d8cb36
add test for userland signature benchmark
keks Sep 16, 2024
fea57de
clean up
keks Sep 23, 2024
e91673d
remove helpers in WORK
keks Sep 23, 2024
9277191
undo fmt
keks Sep 23, 2024
d7ee443
more fmt undo; rm algorithm-specific benchmarking tools
keks Sep 23, 2024
f38e338
add cli to signing tool
keks Sep 23, 2024
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -857,9 +857,9 @@ ifdef CONFIG_READABLE_ASM
KBUILD_CFLAGS += -fno-reorder-blocks -fno-ipa-cp-clone -fno-partial-inlining
endif

ifneq ($(CONFIG_FRAME_WARN),0)
KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
endif
#ifneq ($(CONFIG_FRAME_WARN),0)
#KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
#endif

stackp-flags-y := -fno-stack-protector
stackp-flags-$(CONFIG_STACKPROTECTOR) := -fstack-protector
Expand Down
42 changes: 41 additions & 1 deletion crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ config CRYPTO_RSA
help
RSA (Rivest-Shamir-Adleman) public key algorithm (RFC8017)

config CRYPTO_RSA_GENERIC
tristate "RSA (Rivest-Shamir-Adleman)"
select CRYPTO_AKCIPHER
select CRYPTO_MANAGER
select MPILIB
select ASN1
help
RSA (Rivest-Shamir-Adleman) public key algorithm (RFC8017)

config CRYPTO_RSA_HACL
tristate "RSA (Rivest-Shamir-Adleman)"
select CRYPTO_AKCIPHER
select CRYPTO_MANAGER
select MPILIB
select ASN1
help
RSA (Rivest-Shamir-Adleman) public key algorithm (RFC8017) from HACL*


config CRYPTO_DH
tristate "DH (Diffie-Hellman)"
select CRYPTO_KPP
Expand Down Expand Up @@ -290,13 +309,22 @@ 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.
config CRYPTO_ECDSA_HACL
tristate "Use HACL* for ECDSA"
depends on CRYPTO_ECDSA
select CRYPTO_ECC
select CRYPTO_AKCIPHER
select CRYPTO_DRBG_HMAC
select ASN1
help
Use verified implementations from HACL* for ECDSA with P-256 and P-384

config CRYPTO_ECRDSA
tristate "EC-RDSA (Elliptic Curve Russian Digital Signature Algorithm)"
Expand Down Expand Up @@ -1026,6 +1054,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 +1072,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
23 changes: 20 additions & 3 deletions crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ $(obj)/rsa_helper.o: $(obj)/rsapubkey.asn1.h $(obj)/rsaprivkey.asn1.h

rsa_generic-y := rsapubkey.asn1.o
rsa_generic-y += rsaprivkey.asn1.o
rsa_generic-y += rsa.o
rsa_generic-y += rsa_helper.o
rsa_generic-y += rsa.o
rsa_generic-y += rsa-pkcs1pad.o
obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o
obj-$(CONFIG_CRYPTO_RSA_GENERIC) += rsa_generic.o

rsa_hacl-y := rsapubkey.asn1.o
rsa_hacl-y += rsaprivkey.asn1.o
rsa_hacl-y += hacl_bignum.o
rsa_hacl-y += rsa-hacl-generated.o
rsa_hacl-y += rsa_helper.o
rsa_hacl-y += rsa-hacl.o
rsa_hacl-y += rsa-pkcs1pad.o
obj-$(CONFIG_CRYPTO_RSA_HACL) += rsa_hacl.o

$(obj)/sm2signature.asn1.o: $(obj)/sm2signature.asn1.c $(obj)/sm2signature.asn1.h
$(obj)/sm2.o: $(obj)/sm2signature.asn1.h
Expand All @@ -51,12 +60,18 @@ 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

ecdsa_hacl-y += p256-hacl-generated.o
ecdsa_hacl-y += p384-hacl-generated.o
obj-$(CONFIG_CRYPTO_ECDSA_HACL) += ecdsa_hacl.o

crypto_acompress-y := acompress.o
crypto_acompress-y += scompress.o
obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
Expand All @@ -77,8 +92,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
16 changes: 10 additions & 6 deletions crypto/asymmetric_keys/public_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,11 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
goto error_free_key;
}

if (pkey->key_is_private)
if (pkey->key_is_private) {
ret = crypto_sig_set_privkey(sig, key, pkey->keylen);
else
} else {
ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
}
if (ret)
goto error_free_tfm;

Expand All @@ -324,10 +325,13 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
goto error_free_key;
}

if (pkey->key_is_private)
ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
else
ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
if (pkey->key_is_private) {
ret = crypto_akcipher_set_priv_key(tfm, key,
pkey->keylen);
} else {
ret = crypto_akcipher_set_pub_key(tfm, key,
pkey->keylen);
}
if (ret)
goto error_free_tfm;

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
Loading