Skip to content

Commit

Permalink
crypto: ecdsa - Add support for ECDSA signature verification
Browse files Browse the repository at this point in the history
Add support for parsing the parameters of a NIST P256 or NIST P192 key.
Enable signature verification using these keys. The new module is
enabled with CONFIG_ECDSA:
  Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.)
  is A NIST cryptographic standard algorithm. Only signature verification
  is implemented.

Cc: Herbert Xu <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: [email protected]
Signed-off-by: Stefan Berger <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
stefanberger authored and herbertx committed Mar 26, 2021
1 parent 7547738 commit 4e66029
Show file tree
Hide file tree
Showing 8 changed files with 671 additions and 11 deletions.
10 changes: 10 additions & 0 deletions crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,16 @@ config CRYPTO_ECDH
help
Generic implementation of the ECDH algorithm

config CRYPTO_ECDSA
tristate "ECDSA (NIST P192, P256 etc.) algorithm"
select CRYPTO_ECC
select CRYPTO_AKCIPHER
select ASN1
help
Elliptic Curve Digital Signature Algorithm (NIST P192, P256 etc.)
is A NIST cryptographic standard algorithm. Only signature verification
is implemented.

config CRYPTO_ECRDSA
tristate "EC-RDSA (GOST 34.10) algorithm"
select CRYPTO_ECC
Expand Down
6 changes: 6 additions & 0 deletions crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ sm2_generic-y += sm2.o

obj-$(CONFIG_CRYPTO_SM2) += sm2_generic.o

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

crypto_acompress-y := acompress.o
crypto_acompress-y += scompress.o
obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
Expand Down
13 changes: 2 additions & 11 deletions crypto/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef struct {
u64 m_high;
} uint128_t;

static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
{
switch (curve_id) {
/* In FIPS mode only allow P256 and higher */
Expand All @@ -54,6 +54,7 @@ static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
return NULL;
}
}
EXPORT_SYMBOL(ecc_get_curve);

static u64 *ecc_alloc_digits_space(unsigned int ndigits)
{
Expand Down Expand Up @@ -1281,16 +1282,6 @@ void ecc_point_mult_shamir(const struct ecc_point *result,
}
EXPORT_SYMBOL(ecc_point_mult_shamir);

static inline void ecc_swap_digits(const u64 *in, u64 *out,
unsigned int ndigits)
{
const __be64 *src = (__force __be64 *)in;
int i;

for (i = 0; i < ndigits; i++)
out[i] = be64_to_cpu(src[ndigits - 1 - i]);
}

static int __ecc_is_key_valid(const struct ecc_curve *curve,
const u64 *private_key, unsigned int ndigits)
{
Expand Down
25 changes: 25 additions & 0 deletions crypto/ecc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#define ECC_DIGITS_TO_BYTES_SHIFT 3

#define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)

/**
* struct ecc_point - elliptic curve point in affine coordinates
*
Expand Down Expand Up @@ -70,6 +72,29 @@ struct ecc_curve {
u64 *b;
};

/**
* ecc_swap_digits() - Copy ndigits from big endian array to native array
* @in: Input array
* @out: Output array
* @ndigits: Number of digits to copy
*/
static inline void ecc_swap_digits(const u64 *in, u64 *out, unsigned int ndigits)
{
const __be64 *src = (__force __be64 *)in;
int i;

for (i = 0; i < ndigits; i++)
out[i] = be64_to_cpu(src[ndigits - 1 - i]);
}

/**
* ecc_get_curve() - Get a curve given its curve_id
* @curve_id: Id of the curve
*
* Returns pointer to the curve data, NULL if curve is not available
*/
const struct ecc_curve *ecc_get_curve(unsigned int curve_id);

/**
* ecc_is_key_valid() - Validate a given ECDH private key
*
Expand Down
Loading

0 comments on commit 4e66029

Please sign in to comment.