Add support for ECDSA algorithm
Subsystems (3)
PATCH v2 1/6 crypto: ecc: separate out ecc and ecdh
In reply to Nitin KumbharAdd ECC operations i.e. vli and ecc point ops as a separate module under CRYPTO_ECC kconfig. This allows other ECC algorithms like ECDSA & ECIES to reuse these ops even when ECDH is disabled with CRYPTO_ECDH kconfig.
With these changes, ECDH specific functions are consolidated as ECDH helper routines and ECC curves are moved to ECC specific files.
--- crypto/Kconfig | 7 +++ crypto/Makefile | 5 +- crypto/ecc.c | 133 ++++++++++++++-------------------------------- crypto/ecc.h | 47 ++--------------- crypto/ecc_curve_defs.h | 51 ++++-------------- crypto/ecc_ecdh.h | 54 +++++++++++++++++++ crypto/ecdh.c | 4 +- crypto/ecdh_helper.c | 94 +++++++++++++++++++++++++++++++++ include/crypto/ecc.h | 24 +++++++++ include/crypto/ecdh.h | 10 +--- 10 files changed, 244 insertions(+), 185 deletions(-) create mode 100644 crypto/ecc_ecdh.h create mode 100644 include/crypto/ecc.h
crypto/Kconfig +7 -0
@@ -127,9 +127,16 @@ config CRYPTO_DH help Generic implementation of the Diffie-Hellman algorithm. +config CRYPTO_ECC + tristate "ECC functions" + help + Implementation of ECC functions + + config CRYPTO_ECDH tristate "ECDH algorithm" select CRYTPO_KPP + select CRYPTO_ECC help Generic implementation of the ECDH algorithm
crypto/Makefile +3 -2
@@ -33,8 +33,9 @@ obj-$(CONFIG_CRYPTO_KPP2) += kpp.o dh_generic-y := dh.o dh_generic-y += dh_helper.o obj-$(CONFIG_CRYPTO_DH) += dh_generic.o -ecdh_generic-y := ecc.o -ecdh_generic-y += ecdh.o + +obj-$(CONFIG_CRYPTO_ECC) += ecc.o +ecdh_generic-y := ecdh.o ecdh_generic-y += ecdh_helper.o obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o
crypto/ecc.c +41 -92
156 lines not on this page · raw message
crypto/ecc.h +5 -42
65 lines not on this page · raw message
crypto/ecc_curve_defs.h +10 -41
57 lines not on this page · raw message
crypto/ecc_ecdh.h new file +54 -0
54 lines not on this page · raw message
crypto/ecdh.c +3 -1
@@ -2,6 +2,7 @@* * Copyright (c) 2016, Intel Corporation * Authors: Salvator Benedetto <[email protected]> +* Copyright (c) 2017, NVIDIA Corporation. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public Licence
@@ -14,7 +15,8 @@#include <crypto/kpp.h> #include <crypto/ecdh.h> #include <linux/scatterlist.h> -#include "ecc.h" + +#include "ecc_ecdh.h" struct ecdh_ctx {unsigned int curve_id;
crypto/ecdh_helper.c +94 -0
115 lines not on this page · raw message
include/crypto/ecc.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (c) 2017, NVIDIA Corporation. All Rights Reserved. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of the GNU General Public License as published by the Free +* Software Foundation; either version 2 of the License, or (at your option) +* any later version. +* +*/ + +#ifndef _CRYPTO_ECC_ +#define _CRYPTO_ECC_ + +/* Curves IDs */ +#define ECC_CURVE_NIST_P192 0x0001 +#define ECC_CURVE_NIST_P256 0x0002 + +#define ECC_MAX_DIGITS 4/* 256 */ + +#define ECC_DIGITS_TO_BYTES_SHIFT 3 + +#define ECC_MAX_DIGIT_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT) + +#endif /* _CRYPTO_ECC_ */
include/crypto/ecdh.h +3 -7
@@ -3,6 +3,7 @@* * Copyright (c) 2016, Intel Corporation * Authors: Salvatore Benedetto <[email protected]> +* Copyright (c) 2017, NVIDIA Corporation. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free
@@ -13,24 +14,19 @@#ifndef _CRYPTO_ECDH_#define _CRYPTO_ECDH_ +#include <crypto/ecc.h> +/** * DOC: ECDH Helper Functions * * To use ECDH with the KPP cipher API, the following data structure and * functions should be used. * -* The ECC curves known to the ECDH implementation are specified in this -* header file. -* * To use ECDH with KPP, the following functions should be used to operate on * an ECDH private key. The packet private key that can be set with * the KPP API function call of crypto_kpp_set_secret. */ -/* Curves IDs */ -#define ECC_CURVE_NIST_P192 0x0001 -#define ECC_CURVE_NIST_P256 0x0002 -/** * struct ecdh - define an ECDH private key *
Signature · 1 line
1.7.6.3
v2 cover Add support for ECDSA algorithm
Hello,
This patch series adds support for Elliptic Curve Digital Signature Algorithm (ECDSA). To reuse existing ECC functionality, which is added as part of ECDH, it separates out ECC and ECDH so that only ECC functionality is available for ECDSA even when ECDH is in a disabled state.
Patch #1 restructures ECC and ECDH code such that ECC is not dependent on ECDH config.
Patches #2 & #3 add vli and ecc functions which are required for other Elliptic curve algorithms like ECDSA and ECIES.
Patch #4 adds support for ECDSA. This has been validated for P192 and P256 elliptic curves.
Patches #5 and #6 add ECDSA tests to validate ECDSA functionality and measure ECDSA performance.
Changes in v2:
* Added ecc_is_pub_key_valid() for public key validation
* Use crypto_rng_get_bytes() to get random bytes
* Add documentation in ecdsa.h for ECDSA keys and related APIs
* Reorg ECDSA sign and verity tests to reuse code
Nitin Kumbhar (6):
crypto: ecc: separate out ecc and ecdh
crypto: ecc: add vli and ecc ops
crypto: ecc: export vli and ecc ops
crypto: ecdsa: add ECDSA SW implementation
crypto: testmgr: add ECDSA tests
crypto: tcrypt: add ECDSA test modes
crypto/Kconfig | 14 ++ crypto/Makefile | 8 +- crypto/ecc.c | 421 +++++++++++++++++++++++++++++++------------- crypto/ecc.h | 101 +++++++----- crypto/ecc_curve_defs.h | 51 +----- crypto/ecc_ecdh.h | 54 ++++++ crypto/ecdh.c | 4 +- crypto/ecdh_helper.c | 94 ++++++++++ crypto/ecdsa.c | 362 ++++++++++++++++++++++++++++++++++++++ crypto/ecdsa_helper.c | 116 +++++++++++++ crypto/tcrypt.c | 250 ++++++++++++++++++++++++++- crypto/tcrypt.h | 122 +++++++++++++ crypto/testmgr.c | 330 +++++++++++++++++++++++++++++++++++- crypto/testmgr.h | 140 +++++++++++++++ include/crypto/akcipher.h | 5 +- include/crypto/ecc.h | 24 +++ include/crypto/ecdh.h | 10 +- include/crypto/ecdsa.h | 81 +++++++++ 18 files changed, 1962 insertions(+), 225 deletions(-) create mode 100644 crypto/ecc_ecdh.h create mode 100644 crypto/ecdsa.c create mode 100644 crypto/ecdsa_helper.c create mode 100644 include/crypto/ecc.h create mode 100644 include/crypto/ecdsa.h
Signature · 1 line
1.7.6.3
PATCH v2 2/6 crypto: ecc: add vli and ecc ops
Add functions to copy vli from buffers, to print vli in big endian format, for vli mod and mod multiplication ops, ecc point addition and ecc pub key validation.
--- crypto/ecc.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 189 insertions(+), 0 deletions(-)
crypto/ecc.c +189 -0
@@ -208,6 +208,42 @@ static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits) dest[i] = src[i]; } +/* Copy from vli to buf. +* For buffers smaller than vli: copy only LSB nbytes from vli. +* For buffers larger than vli : fill up remaining buf with zeroes. +*/ +void vli_copy_to_buf (u8 *dst_buf,unsigned int buf_len, +const u64 *src_vli,unsigned int ndigits) +{ +unsigned int nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; +u8 *vli = (u8 *)src_vli; +int i; + +for (i =0 ; i < buf_len && i < nbytes; i++) + dst_buf[i] = vli[i]; + +for (; i < buf_len; i++) + dst_buf[i] =0 ; +} + +/* Copy from buffer to vli. +* For buffers smaller than vli: fill up remaining vli with zeroes. +* For buffers larger than vli : copy only LSB nbytes to vli. +*/ +void vli_copy_from_buf (u64 *dst_vli,unsigned int ndigits, +const u8 *src_buf,unsigned int buf_len) +{ +unsigned int nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; +u8 *vli = (u8 *)dst_vli; +int i; + +for (i =0 ; i < buf_len && i < nbytes; i++) + vli[i] = src_buf[i]; + +for (; i < nbytes; i++) + vli[i] =0 ; +} +/* Returns sign of left - right. */ static int vli_cmp (const u64 *left,const u64 *right,unsigned int ndigits) {
@@ -440,6 +476,83 @@ static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,vli_add (result, result, mod, ndigits); } +/* Computes result = input % mod. +* Assumes that input < mod, result != mod. +*/ +void vli_mod (u64 *result,const u64 *input,const u64 *mod, +unsigned int ndigits) +{ +if (vli_cmp (input, mod, ndigits) >=0 ) +vli_sub (result, input, mod, ndigits); +else +vli_set (result, input, ndigits); +} + +/* Print vli in big-endian format. +* The bytes are printed in hex. +*/ +void vli_print (char *vli_name,const u64 *vli,unsigned int ndigits) +{ +int nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; +int buf_size =2 *ECC_MAX_DIGIT_BYTES +1 ; +unsigned char *c, buf[buf_size]; +int i, j; + + c = (unsigned char *)vli; + +for (i = nbytes -1 , j =0 ; i >=0 && j < buf_size; i--, j +=2 ) +snprintf (&buf[j],3 ,"%02x" , *(c + i)); + + buf[j] ='\0' ; + +pr_info ("%20s(BigEnd)=%s\n" , vli_name, buf); +} + +/* Computes result = (left * right) % mod. +* Assumes that left < mod and right < mod, result != mod. +* Uses: +* (a * b) % m = ((a % m) * (b % m)) % m +* (a * b) % m = (a + a + ... + a) % m = b modular additions of (a % m) +*/ +void vli_mod_mult (u64 *result,const u64 *left,const u64 *right, +const u64 *mod,unsigned int ndigits) +{ +u64 t1[ndigits], mm[ndigits]; +u64 aa[ndigits], bb[ndigits]; + +vli_clear (result, ndigits); +vli_set (aa, left, ndigits); +vli_set (bb, right, ndigits); +vli_set (mm, mod, ndigits); + +/* aa = aa % mm */ +vli_mod (aa, aa, mm, ndigits); + +/* bb = bb % mm */ +vli_mod (bb, bb, mm, ndigits); + +while (!vli_is_zero (bb, ndigits)) { + +/* if bb is odd i.e. 0th bit set then add +* aa i.e. result = (result + aa) % mm +*/ +if (vli_test_bit (bb,0 )) +vli_mod_add (result, result, aa, mm, ndigits); + +/* bb = bb / 2 = bb >> 1 */ +vli_rshift1 (bb, ndigits); + +/* aa = (aa * 2) % mm */ +vli_sub (t1, mm, aa, ndigits); +if (vli_cmp (aa, t1, ndigits) ==-1 ) +/* if aa < t1 then aa = aa * 2 = aa << 1*/ +vli_lshift (aa, aa,1 , ndigits); +else +/* if aa >= t1 then aa = aa - t1 */ +vli_sub (aa, aa, t1, ndigits); + } +} +/* Computes p_result = p_product % curve_p. * See algorithm 5 and 6 from * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
@@ -878,6 +991,61 @@ static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,vli_set (x1, t7, ndigits); } +/* Point addition. +* Add 2 distinct points on elliptic curve to get a new point. +* +* P = (x1,y1)and Q = (x2, y2) then P + Q = (x3,y3) where +* x3 = ((y2-y1)/(x2-x1))^2 - x1 - x2 +* y3 = ((y2-y1)/(x2-x1))(x1-x3) - y1 +* +* Q => P + Q +*/ +void ecc_point_add (u64 *x1,u64 *y1,u64 *x2,u64 *y2,u64 *curve_prime, +unsigned int ndigits) +{ +/* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ +u64 t5[ndigits]; +u64 t6[ndigits]; +u64 t7[ndigits]; + +/* t6 = x2 - x1 */ +vli_mod_sub (t6, x2, x1, curve_prime, ndigits); +/* t6 = (x2 - x1)^2 = A */ +vli_mod_square_fast (t6, t6, curve_prime, ndigits); +vli_mod_inv (t7, t6, curve_prime, ndigits); +/* t5 = x2 - x1 */ +vli_mod_sub (t5, x2, x1, curve_prime, ndigits); +/* t5 = (x2 - x1)^2 = A */ +vli_mod_square_fast (t5, t5, curve_prime, ndigits); +/* t1 = x1*A = B = x1*(x2-x1)^2*/ +vli_mod_mult_fast (x1, x1, t5, curve_prime, ndigits); +/* t3 = x2*A = C = x2*(x2-x1)^2*/ +vli_mod_mult_fast (x2, x2, t5, curve_prime, ndigits); +/* t4 = y2 - y1 */ +vli_mod_sub (y2, y2, y1, curve_prime, ndigits); +/* t5 = (y2 - y1)^2 = D */ +vli_mod_square_fast (t5, y2, curve_prime, ndigits); + +/* t5 = D - B = (y2 - y1)^2 - x1*(x2-x1)^2 */ +vli_mod_sub (t5, t5, x1, curve_prime, ndigits); +/* t5 = D - B - C = x3 = (y2 - y1)^2 - x1*(x2-x1)^2 - x2*(x2-x1)^2*/ +vli_mod_sub (t5, t5, x2, curve_prime, ndigits); + +/* t3 = C - B = x2*(x2-x1)^2 - x1*(x2-x1)^2 */ +vli_mod_sub (x2, x2, x1, curve_prime, ndigits); +/* t2 = y1*(C - B) = y1*(x2*(x2-x1)^2 - x1*(x2-x1)^2)*/ +vli_mod_mult_fast (y1, y1, x2, curve_prime, ndigits); +/* t3 = B - x3 = x1*(x2-x1)^2 - x3*/ +vli_mod_sub (x2, x1, t5, curve_prime, ndigits); +/* t4 = (y2 - y1)*(B - x3) = (y2 - y1)*(x1*(x2-x1)^2 - x3)*/ +vli_mod_mult_fast (y2, y2, x2, curve_prime, ndigits); +/* t4 = y3 = ((y2 - y1)*(x1*(x2-x1)^2 - x3)) - y1*/ +vli_mod_sub (y2, y2, y1, curve_prime, ndigits); + +vli_mod_mult_fast (t5, t5, t7, curve_prime, ndigits); +vli_set (x2, t5, ndigits); +} + static void ecc_point_mult(struct ecc_point *result, const struct ecc_point *point, const u64 *scalar, u64 *initial_z, u64 *curve_prime,
@@ -965,3 +1133,24 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,return 0 ; } + +int ecc_is_pub_key_valid (unsigned int curve_id,unsigned int ndigits, +const u8 *pub_key,unsigned int pub_key_len) +{ +const struct ecc_curve *curve =ecc_get_curve (curve_id); +int nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; +struct ecc_point p; + +if (!pub_key || pub_key_len !=2 * nbytes) +return -EINVAL ; + + p.x = (u64 *)pub_key; + p.y = (u64 *)(pub_key +ECC_MAX_DIGIT_BYTES ); + p.ndigits = ndigits; + +if (vli_cmp (curve->p, p.x, ndigits) !=1 || +vli_cmp (curve->p, p.y, ndigits) !=1 ) +return -EINVAL ; + +return 0 ; +}
Signature · 1 line
1.7.6.3
PATCH v2 3/6 crypto: ecc: export vli and ecc ops
In reply to Nitin KumbharExport vli and ECC related functions so that these can be used by all ECC algorithm modules like ECDH, ECDSA and ECIES.
--- crypto/ecc.c | 115 +++++++++++++++++++++++++++++++++++++--------------------- crypto/ecc.h | 56 ++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 41 deletions(-)
crypto/ecc.c +74 -41
@@ -77,7 +77,7 @@ .n = nist_p256_n }; -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 */
@@ -89,6 +89,7 @@return NULL ; } } +EXPORT_SYMBOL_GPL (ecc_get_curve);static u64 *ecc_alloc_digits_space (unsigned int ndigits) {
@@ -105,7 +106,7 @@ static void ecc_free_digits_space(u64 *space)kzfree (space); } -static struct ecc_point *ecc_alloc_point (unsigned int ndigits) +struct ecc_point *ecc_alloc_point (unsigned int ndigits) {struct ecc_point *p =kmalloc (sizeof (*p),GFP_KERNEL );
@@ -130,8 +131,9 @@ static void ecc_free_digits_space(u64 *space)kfree (p);return NULL ; } +EXPORT_SYMBOL_GPL (ecc_alloc_point); -static void ecc_free_point (struct ecc_point *p) +void ecc_free_point (struct ecc_point *p) {if (!p)return ;
@@ -140,17 +142,19 @@ static void ecc_free_point(struct ecc_point *p)kzfree (p->y);kzfree (p); } +EXPORT_SYMBOL_GPL (ecc_free_point); -static void vli_clear (u64 *vli,unsigned int ndigits) +void vli_clear (u64 *vli,unsigned int ndigits) {int i;for (i =0 ; i < ndigits; i++) vli[i] =0 ; } +EXPORT_SYMBOL_GPL (vli_clear);/* Returns true if vli == 0, false otherwise. */ -static bool vli_is_zero (const u64 *vli,unsigned int ndigits) +bool vli_is_zero (const u64 *vli,unsigned int ndigits) {int i;
@@ -161,15 +165,17 @@ static bool vli_is_zero(const u64 *vli, unsigned int ndigits)return true; } +EXPORT_SYMBOL_GPL (vli_is_zero);/* Returns nonzero if bit bit of vli is set. */ -static u64 vli_test_bit (const u64 *vli,unsigned int bit) +u64 vli_test_bit (const u64 *vli,unsigned int bit) {return (vli[bit /64 ] & ((u64 )1 << (bit %64 ))); } +EXPORT_SYMBOL_GPL (vli_test_bit);/* Counts the number of 64-bit "digits" in vli. */ -static unsigned int vli_num_digits (const u64 *vli,unsigned int ndigits) +unsigned int vli_num_digits (const u64 *vli,unsigned int ndigits) {int i;
@@ -181,9 +187,10 @@ static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)return (i +1 ); } +EXPORT_SYMBOL_GPL (vli_num_digits);/* Counts the number of bits required for vli. */ -static unsigned int vli_num_bits (const u64 *vli,unsigned int ndigits) +unsigned int vli_num_bits (const u64 *vli,unsigned int ndigits) {unsigned int i, num_digits;u64 digit;
@@ -198,15 +205,17 @@ static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)return ((num_digits -1 ) *64 + i); } +EXPORT_SYMBOL_GPL (vli_num_bits);/* Sets dest = src. */ -static void vli_set (u64 *dest,const u64 *src,unsigned int ndigits) +void vli_set (u64 *dest,const u64 *src,unsigned int ndigits) {int i;for (i =0 ; i < ndigits; i++) dest[i] = src[i]; } +EXPORT_SYMBOL_GPL (vli_set);/* Copy from vli to buf. * For buffers smaller than vli: copy only LSB nbytes from vli.
@@ -225,6 +234,7 @@ void vli_copy_to_buf(u8 *dst_buf, unsigned int buf_len,for (; i < buf_len; i++) dst_buf[i] =0 ; } +EXPORT_SYMBOL_GPL (vli_copy_to_buf);/* Copy from buffer to vli. * For buffers smaller than vli: fill up remaining vli with zeroes.
@@ -243,9 +253,10 @@ void vli_copy_from_buf(u64 *dst_vli, unsigned int ndigits,for (; i < nbytes; i++) vli[i] =0 ; } +EXPORT_SYMBOL_GPL (vli_copy_from_buf);/* Returns sign of left - right. */ -static int vli_cmp (const u64 *left,const u64 *right,unsigned int ndigits) +int vli_cmp (const u64 *left,const u64 *right,unsigned int ndigits) {int i;
@@ -258,12 +269,13 @@ static int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)return 0 ; } +EXPORT_SYMBOL_GPL (vli_cmp);/* Computes result = in << c, returning carry. Can modify in place * (if result == in). 0 < shift < 64. */ -static u64 vli_lshift (u64 *result,const u64 *in,unsigned int shift, -unsigned int ndigits) +u64 vli_lshift (u64 *result,const u64 *in,unsigned int shift, +unsigned int ndigits) {u64 carry =0 ;int i;
@@ -277,9 +289,10 @@ static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,return carry; } +EXPORT_SYMBOL_GPL (vli_lshift);/* Computes vli = vli >> 1. */ -static void vli_rshift1 (u64 *vli,unsigned int ndigits) +void vli_rshift1 (u64 *vli,unsigned int ndigits) {u64 *end = vli;u64 carry =0 ;
@@ -292,10 +305,11 @@ static void vli_rshift1(u64 *vli, unsigned int ndigits) carry = temp <<63 ; } } +EXPORT_SYMBOL_GPL (vli_rshift1);/* Computes result = left + right, returning carry. Can modify in place. */ -static u64 vli_add (u64 *result,const u64 *left,const u64 *right, -unsigned int ndigits) +u64 vli_add (u64 *result,const u64 *left,const u64 *right, +unsigned int ndigits) {u64 carry =0 ;int i;
@@ -312,10 +326,11 @@ static u64 vli_add(u64 *result, const u64 *left, const u64 *right,return carry; } +EXPORT_SYMBOL_GPL (vli_add);/* Computes result = left - right, returning borrow. Can modify in place. */ -static u64 vli_sub (u64 *result,const u64 *left,const u64 *right, -unsigned int ndigits) +u64 vli_sub (u64 *result,const u64 *left,const u64 *right, +unsigned int ndigits) {u64 borrow =0 ;int i;
@@ -332,6 +347,7 @@ static u64 vli_sub(u64 *result, const u64 *left, const u64 *right,return borrow; } +EXPORT_SYMBOL_GPL (vli_sub);static uint128_t mul_64_64 (u64 left,u64 right) {
@@ -368,8 +384,8 @@ static uint128_t add_128_128(uint128_t a, uint128_t b)return result; } -static void vli_mult (u64 *result,const u64 *left,const u64 *right, -unsigned int ndigits) +void vli_mult (u64 *result,const u64 *left,const u64 *right, +unsigned int ndigits) {uint128_t r01 = {0 ,0 };u64 r2 =0 ;
@@ -403,8 +419,9 @@ static void vli_mult(u64 *result, const u64 *left, const u64 *right, result[ndigits *2 -1 ] = r01.m_low; } +EXPORT_SYMBOL_GPL (vli_mult); -static void vli_square (u64 *result,const u64 *left,unsigned int ndigits) +void vli_square (u64 *result,const u64 *left,unsigned int ndigits) {uint128_t r01 = {0 ,0 };u64 r2 =0 ;
@@ -442,12 +459,13 @@ static void vli_square(u64 *result, const u64 *left, unsigned int ndigits) result[ndigits *2 -1 ] = r01.m_low; } +EXPORT_SYMBOL_GPL (vli_square);/* 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, -const u64 *mod,unsigned int ndigits) +void vli_mod_add (u64 *result,const u64 *left,const u64 *right, +const u64 *mod,unsigned int ndigits) {u64 carry;
@@ -459,12 +477,13 @@ 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_GPL (vli_mod_add);/* Computes result = (left - right) % mod. * Assumes that left < mod and right < mod, result != mod. */ -static void vli_mod_sub (u64 *result,const u64 *left,const u64 *right, -const u64 *mod,unsigned int ndigits) +void vli_mod_sub (u64 *result,const u64 *left,const u64 *right, +const u64 *mod,unsigned int ndigits) {u64 borrow =vli_sub (result, left, right, ndigits);
@@ -475,6 +494,7 @@ static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,if (borrow)vli_add (result, result, mod, ndigits); } +EXPORT_SYMBOL_GPL (vli_mod_sub);/* Computes result = input % mod. * Assumes that input < mod, result != mod.
@@ -487,6 +507,7 @@ void vli_mod(u64 *result, const u64 *input, const u64 *mod,else vli_set (result, input, ndigits); } +EXPORT_SYMBOL_GPL (vli_mod);/* Print vli in big-endian format. * The bytes are printed in hex.
@@ -507,6 +528,7 @@ void vli_print(char *vli_name, const u64 *vli, unsigned int ndigits)pr_info ("%20s(BigEnd)=%s\n" , vli_name, buf); } +EXPORT_SYMBOL_GPL (vli_print);/* Computes result = (left * right) % mod. * Assumes that left < mod and right < mod, result != mod.
@@ -552,6 +574,7 @@ void vli_mod_mult(u64 *result, const u64 *left, const u64 *right,vli_sub (aa, aa, t1, ndigits); } } +EXPORT_SYMBOL_GPL (vli_mod_mult);/* Computes p_result = p_product % curve_p. * See algorithm 5 and 6 from
@@ -663,8 +686,8 @@ static void vli_mmod_fast_256(u64 *result, const u64 *product,/* Computes result = product % curve_prime * from http://www.nsa.gov/ia/_files/nist-routines.pdf */ -static bool vli_mmod_fast (u64 *result,u64 *product, -const u64 *curve_prime,unsigned int ndigits) +bool vli_mmod_fast (u64 *result,u64 *product, +const u64 *curve_prime,unsigned int ndigits) {u64 tmp[2 * ndigits];
@@ -682,34 +705,37 @@ static bool vli_mmod_fast(u64 *result, u64 *product,return true; } +EXPORT_SYMBOL_GPL (vli_mmod_fast);/* Computes result = (left * right) % curve_prime. */ -static void vli_mod_mult_fast (u64 *result,const u64 *left,const u64 *right, -const u64 *curve_prime,unsigned int ndigits) +void vli_mod_mult_fast (u64 *result,const u64 *left,const u64 *right, +const u64 *curve_prime,unsigned int ndigits) {u64 product[2 * ndigits];vli_mult (product, left, right, ndigits);vli_mmod_fast (result, product, curve_prime, ndigits); } +EXPORT_SYMBOL_GPL (vli_mod_mult_fast);/* Computes result = left^2 % curve_prime. */ -static void vli_mod_square_fast (u64 *result,const u64 *left, -const u64 *curve_prime,unsigned int ndigits) +void vli_mod_square_fast (u64 *result,const u64 *left, +const u64 *curve_prime,unsigned int ndigits) {u64 product[2 * ndigits];vli_square (product, left, ndigits);vli_mmod_fast (result, product, curve_prime, ndigits); } +EXPORT_SYMBOL_GPL (vli_mod_square_fast);#define EVEN (vli) (!(vli[0] & 1))/* Computes result = (1 / p_input) % mod. All VLIs are the same size. * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */ -static void vli_mod_inv (u64 *result,const u64 *input,const u64 *mod, -unsigned int ndigits) +void vli_mod_inv (u64 *result,const u64 *input,const u64 *mod, +unsigned int ndigits) {u64 a[ndigits], b[ndigits];u64 u[ndigits], v[ndigits];
@@ -781,23 +807,25 @@ static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,vli_set (result, u, ndigits); } +EXPORT_SYMBOL_GPL (vli_mod_inv);/* ------ Point operations ------ */ /* Returns true if p_point is the point at infinity, false otherwise. */ -static bool ecc_point_is_zero (const struct ecc_point *point) +bool ecc_point_is_zero (const struct ecc_point *point) {return (vli_is_zero (point->x, point->ndigits) &&vli_is_zero (point->y, point->ndigits)); } +EXPORT_SYMBOL_GPL (ecc_point_is_zero);/* Point multiplication algorithm using Montgomery's ladder with co-Z * coordinates. From http://eprint.iacr.org/2011/338.pdf */ /* Double in place */ -static void ecc_point_double_jacobian (u64 *x1,u64 *y1,u64 *z1, -u64 *curve_prime,unsigned int ndigits) +void ecc_point_double_jacobian (u64 *x1,u64 *y1,u64 *z1, +u64 *curve_prime,unsigned int ndigits) {/* t1 = x, t2 = y, t3 = z */ u64 t4[ndigits];
@@ -857,6 +885,7 @@ static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,vli_set (z1, y1, ndigits);vli_set (y1, t4, ndigits); } +EXPORT_SYMBOL_GPL (ecc_point_double_jacobian);/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */ static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
@@ -1045,11 +1074,12 @@ void ecc_point_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,vli_mod_mult_fast (t5, t5, t7, curve_prime, ndigits);vli_set (x2, t5, ndigits); } +EXPORT_SYMBOL_GPL (ecc_point_add); -static void ecc_point_mult (struct ecc_point *result, -const struct ecc_point *point,const u64 *scalar, -u64 *initial_z,u64 *curve_prime, -unsigned int ndigits) +void ecc_point_mult (struct ecc_point *result, +const struct ecc_point *point,const u64 *scalar, +u64 *initial_z,u64 *curve_prime, +unsigned int ndigits) {/* R0 and R1 */ u64 rx[2 ][ndigits];
@@ -1100,15 +1130,16 @@ 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_GPL (ecc_point_mult); -static inline void ecc_swap_digits (const u64 *in,u64 *out, -unsigned int ndigits) +void ecc_swap_digits (const u64 *in,u64 *out,unsigned int ndigits) {int i;for (i =0 ; i < ndigits; i++) out[i] =__swab64 (in[ndigits -1 - i]); } +EXPORT_SYMBOL_GPL (ecc_swap_digits);int ecc_is_key_valid (unsigned int curve_id,unsigned int ndigits,const u8 *private_key,unsigned int private_key_len)
@@ -1133,6 +1164,7 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,return 0 ; } +EXPORT_SYMBOL_GPL (ecc_is_key_valid);int ecc_is_pub_key_valid (unsigned int curve_id,unsigned int ndigits,const u8 *pub_key,unsigned int pub_key_len)
@@ -1154,3 +1186,4 @@ int ecc_is_pub_key_valid(unsigned int curve_id, unsigned int ndigits,return 0 ; } +EXPORT_SYMBOL_GPL (ecc_is_pub_key_valid);
crypto/ecc.h +56 -0
66 lines not on this page · raw message
Signature · 1 line
1.7.6.3
PATCH v2 4/6 crypto: ecdsa: add ECDSA SW implementation
In reply to Nitin KumbharThis adds support for ECDSA algorithm. This implementation supports sign and verify functions for ECDSA algorithm using akcipher. As ECDSA is a signing algorithm dummy functions are added for encrypt() and decrypt().
Helper routines for parsing public and private ECC keys for ECDSA are added as well.
--- crypto/Kconfig | 7 + crypto/Makefile | 3 + crypto/ecdsa.c | 362 +++++++++++++++++++++++++++++++++++++++++++++ crypto/ecdsa_helper.c | 116 +++++++++++++++ include/crypto/akcipher.h | 5 +- include/crypto/ecdsa.h | 81 ++++++++++ 6 files changed, 573 insertions(+), 1 deletions(-) create mode 100644 crypto/ecdsa.c create mode 100644 crypto/ecdsa_helper.c create mode 100644 include/crypto/ecdsa.h
crypto/Kconfig +7 -0
@@ -140,6 +140,13 @@ config CRYPTO_ECDH help Generic implementation of the ECDH algorithm +config CRYPTO_ECDSA + tristate "ECDSA algorithm" + select CRYPTO_AKCIPHER + select CRYPTO_ECC + help + Generic implementation of the ECDSA algorithm + config CRYPTO_MANAGER tristate "Cryptographic algorithm manager" select CRYPTO_MANAGER2
crypto/Makefile +3 -0
@@ -38,6 +38,9 @@ obj-$(CONFIG_CRYPTO_ECC) += ecc.o ecdh_generic-y := ecdh.o ecdh_generic-y += ecdh_helper.o obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o +ecdsa_generic-y := ecdsa.o +ecdsa_generic-y += ecdsa_helper.o +obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o $(obj)/rsapubkey-asn1.o: $(obj)/rsapubkey-asn1.c $(obj)/rsapubkey-asn1.h $(obj)/rsaprivkey-asn1.o: $(obj)/rsaprivkey-asn1.c $(obj)/rsaprivkey-asn1.h
crypto/ecdsa.c new file +362 -0
362 lines not on this page · raw message
crypto/ecdsa_helper.c new file +116 -0
116 lines not on this page · raw message
include/crypto/akcipher.h +4 -1
@@ -3,6 +3,7 @@* * Copyright (c) 2015, Intel Corporation * Authors: Tadeusz Struk <[email protected]> +* Copyright (c) 2017, NVIDIA Corporation. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free
@@ -27,6 +28,7 @@* result. * In case of error where the dst sgl size was insufficient, * it will be updated to the size required for the operation. +* @info: Any request specific data needed to process the request. * @__ctx: Start of private context data */ struct akcipher_request {
@@ -35,6 +37,7 @@ struct akcipher_request {struct scatterlist *dst;unsigned int src_len;unsigned int dst_len; +void *info; void *__ctx[] CRYPTO_MINALIGN_ATTR; };
@@ -193,7 +196,7 @@ static inline void crypto_free_akcipher(struct crypto_akcipher *tfm) {struct akcipher_request *req; - req =kmalloc (sizeof (*req) +crypto_akcipher_reqsize (tfm), gfp); + req =kzalloc (sizeof (*req) +crypto_akcipher_reqsize (tfm), gfp);if (likely (req))akcipher_request_set_tfm (req, tfm);
include/crypto/ecdsa.h new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
1.7.6.3
PATCH v2 5/6 crypto: testmgr: add ECDSA tests
In reply to Nitin KumbharUpdate crypto test manager to include NIST ECDSA test vectors and various ECDSA tests. These include tests for ECDSA signing, ECDSA sign-verification, ECDSA signing and verifying generated signatures and invalidation of incorrect signatures.
--- crypto/testmgr.c | 330 +++++++++++++++++++++++++++++++++++++++++++++++++++++- crypto/testmgr.h | 140 +++++++++++++++++++++++ 2 files changed, 467 insertions(+), 3 deletions(-)
crypto/testmgr.c +327 -3
@@ -5,6 +5,7 @@* Copyright (c) 2002 Jean-Francois Dive <[email protected]> * Copyright (c) 2007 Nokia Siemens Networks * Copyright (c) 2008 Herbert Xu <[email protected]> +* Copyright (c) 2017 NVIDIA Corporation * * Updated RFC4106 AES-GCM testing. * Authors: Aidan O'Mahony ([email protected])
@@ -2085,6 +2086,314 @@ static int alg_test_kpp(const struct alg_test_desc *desc, const char *driver,return err; } +#define ECDSA_TEST_VALID_VERIFY 0 +#define ECDSA_TEST_INVALID_VERIFY 1 +#define ECDSA_TEST_SIGN_VERIFY 2 +#define ECDSA_TEST_SIGN 3 + +static int __do_test_ecdsa_verify (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec,int test) +{ +struct akcipher_request *req =NULL ; +u8 *r_str =NULL , *s_str =NULL ; +u8 *m_str =NULL ; +struct scatterlist src_tab[3 ], dst; +struct tcrypt_result result; +unsigned int outbuf_maxlen; +u8 *outbuf =NULL ; +unsigned int nbytes; +int err, m_size; + +/* Alloc akcipher request */ + req =akcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) +return -ENOMEM ; + +/* Set private key */ + err =crypto_akcipher_set_pub_key (tfm, vec->key, vec->key_len); +if (err) + goto error; + +/* +* vec->c always contains k, R and S in that order. All are +* of same size and are equal to n i.e. the order of +* an elliptic curve. +*/ + nbytes = vec->c_size /3 ; + + r_str =kzalloc (nbytes,GFP_KERNEL ); + s_str =kzalloc (nbytes,GFP_KERNEL ); + m_str =kzalloc (vec->m_size,GFP_KERNEL ); +if (!r_str || !s_str || !m_str) { + err = -ENOMEM ; + goto error; + } +memcpy (r_str, (u8 *)vec->c +1 * nbytes, nbytes); +memcpy (s_str, (u8 *)vec->c +2 * nbytes, nbytes); +memcpy (m_str, vec->m, vec->m_size); + + outbuf_maxlen =crypto_akcipher_maxsize (tfm); +if (outbuf_maxlen <0 ) { + err = outbuf_maxlen; + goto error; + } + outbuf =kzalloc (outbuf_maxlen,GFP_KERNEL ); +if (!outbuf) { + err = -ENOMEM ; + goto error; + } + +/* Intentionally set m_size to 8 to test invalid hash */ + m_size = test ==ECDSA_TEST_VALID_VERIFY ? vec->m_size :8 ; + +/* Set src and dst buffers */ +sg_init_table (src_tab,3 ); +sg_set_buf (&src_tab[0 ], m_str, m_size); +sg_set_buf (&src_tab[1 ], r_str, nbytes); +sg_set_buf (&src_tab[2 ], s_str, nbytes); +sg_init_one (&dst, outbuf, outbuf_maxlen); + +akcipher_request_set_crypt (req, src_tab, &dst, + vec->m_size +2 * nbytes, outbuf_maxlen); + +/* Set up result callback */ +init_completion (&result.completion); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &result); + +/* Run ecdsa verify operation on sig (r,s) */ + err =wait_async_op (&result,crypto_akcipher_verify (req)); +error: +akcipher_request_free (req); +kfree (r_str); +kfree (s_str); +kfree (m_str); +kfree (outbuf); +return err; +} + +static int do_test_ecdsa_verify (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec) +{ +int err; + + err =__do_test_ecdsa_verify (tfm, vec,ECDSA_TEST_VALID_VERIFY ); +if (err) +pr_err ("alg: ecdsa: verify(rs) test failed. err %d\n" , err); + +return err; +} + +static int do_test_ecdsa_invalid_verify (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec) +{ +int err; + + err =__do_test_ecdsa_verify (tfm, vec,ECDSA_TEST_INVALID_VERIFY ); +if (err != -EBADMSG ) { +pr_err ("alg: ecdsa: invalid verify test failed. err %d\n" , err); +return err; + } + +return 0 ; +} + +static int __do_test_ecdsa_sign_verify (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec, +int test) +{ +struct akcipher_request *req =NULL ; +u8 *r_str =NULL , *s_str =NULL ; +u8 *sig_r =NULL , *sig_s =NULL ; +u8 *m_str =NULL , *k_str =NULL ; +struct scatterlist src_tab[3 ]; +struct scatterlist src, dst; +struct tcrypt_result result; +unsigned int outbuf_maxlen; +void *outbuf =NULL ; +unsigned int nbytes; +int err; + +/* Alloc akcipher request */ + req =akcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) +return -ENOMEM ; + +/* Set private key */ + err =crypto_akcipher_set_priv_key (tfm, vec->key, vec->key_len); +if (err) + goto error; + +/* Set pub key */ + err =crypto_akcipher_set_pub_key (tfm, vec->key, vec->key_len); +if (err) + goto error; + +/* +* vec->c always contains k, R and S in that order. All are +* of same size and are equal to n i.e. the order of +* an elliptic curve. +*/ + nbytes = vec->c_size /3 ; + + k_str =kzalloc (nbytes,GFP_KERNEL ); + r_str =kzalloc (nbytes,GFP_KERNEL ); + s_str =kzalloc (nbytes,GFP_KERNEL ); + m_str =kzalloc (vec->m_size,GFP_KERNEL ); +if (!k_str || !r_str || !s_str || !m_str) { + err = -ENOMEM ; + goto error; + } +memcpy (k_str, (u8 *)vec->c +0 * nbytes, nbytes); +memcpy (r_str, (u8 *)vec->c +1 * nbytes, nbytes); +memcpy (s_str, (u8 *)vec->c +2 * nbytes, nbytes); +memcpy (m_str, vec->m, vec->m_size); + + outbuf_maxlen =crypto_akcipher_maxsize (tfm); +if (outbuf_maxlen <0 ) { + err = outbuf_maxlen; + goto error; + } + outbuf =kzalloc (outbuf_maxlen,GFP_KERNEL ); +if (!outbuf) { + err = -ENOMEM ; + goto error; + } + +sg_init_one (&src, m_str, vec->m_size); +sg_init_one (&dst, outbuf, outbuf_maxlen); + +akcipher_request_set_crypt (req, &src, &dst, + vec->m_size, outbuf_maxlen); + +/* Set up result callback */ +init_completion (&result.completion); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &result); + +/* Set K in request for signing */ +if (test ==ECDSA_TEST_SIGN ) + req->info = k_str; + +/* Run ecdsa sign operation on message digest */ + err =wait_async_op (&result,crypto_akcipher_sign (req)); +if (err) + goto error; + +/* verify that signature (r,s) is valid */ +if (req->dst_len !=2 * nbytes) { + err = -EINVAL ; + goto error; + } + +/* outbuf contains r and s */ + sig_r = outbuf; + sig_s = (u8 *)outbuf + nbytes; + +if (test ==ECDSA_TEST_SIGN ) { +/* compare r & s */ +if (memcmp (r_str, sig_r, nbytes) || +memcmp (s_str, sig_s, nbytes)) { + err = -EINVAL ; + goto error; + } + }else { +/* Set src and dst buffers */ +sg_init_table (src_tab,3 ); +sg_set_buf (&src_tab[0 ], m_str, vec->m_size); +sg_set_buf (&src_tab[1 ], sig_r, nbytes); +sg_set_buf (&src_tab[2 ], sig_s, nbytes); +sg_init_one (&dst, outbuf, outbuf_maxlen); + +akcipher_request_set_crypt (req, src_tab, &dst, + vec->m_size +2 * nbytes, + outbuf_maxlen); + +/* Set up result callback */ +init_completion (&result.completion); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &result); + +/* Run ecdsa verify operation on sig (r,s) */ + err =wait_async_op (&result,crypto_akcipher_verify (req)); +if (err) + goto error; + } + +error: +akcipher_request_free (req); +kfree (k_str); +kfree (r_str); +kfree (s_str); +kfree (m_str); +kfree (outbuf); +return err; +} + +static int do_test_ecdsa_sign_verify (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec) +{ +int err; + + err =__do_test_ecdsa_sign_verify (tfm, vec,ECDSA_TEST_SIGN_VERIFY ); +if (err) +pr_err ("alg: ecdsa: sign/verify test failed. err %d\n" , err); + +return err; +} + +static int do_test_ecdsa_sign (struct crypto_akcipher *tfm, +struct akcipher_testvec *vec) +{ +int err; + + err =__do_test_ecdsa_sign_verify (tfm, vec,ECDSA_TEST_SIGN ); +if (err) +pr_err ("alg: ecdsa: sign test failed. err %d\n" , err); + +return err; +} + +static int test_ecdsa_akcipher (struct crypto_akcipher *tfm,const char *alg, +struct akcipher_testvec *vecs,unsigned int tcount) +{ +int i, err =0 ; + +for (i =0 ; i < tcount; i++) { + err =do_test_ecdsa_verify (tfm, &vecs[i]); +if (!err) +continue ; + +return err; + } + +for (i =0 ; i < tcount; i++) { + err =do_test_ecdsa_invalid_verify (tfm, &vecs[i]); +if (!err) +continue ; + +return err; + } + +for (i =0 ; i < tcount; i++) { + err =do_test_ecdsa_sign_verify (tfm, &vecs[i]); +if (!err) +continue ; + +return err; + } + +for (i =0 ; i < tcount; i++) { + err =do_test_ecdsa_sign (tfm, &vecs[i]); +if (!err) +continue ; + +return err; + } + +return 0 ; +} +static int test_akcipher_one (struct crypto_akcipher *tfm,struct akcipher_testvec *vecs) {
@@ -2236,9 +2545,17 @@ static int alg_test_akcipher(const struct alg_test_desc *desc, driver, PTR_ERR(tfm));return PTR_ERR (tfm); } -if (desc->suite.akcipher.vecs) - err =test_akcipher (tfm, desc->alg, desc->suite.akcipher.vecs, - desc->suite.akcipher.count); + +if (desc->suite.akcipher.vecs) { +if (strncmp (desc->alg,"ecdsa" ,5 ) ==0 ) + err =test_ecdsa_akcipher (tfm, desc->alg, + desc->suite.akcipher.vecs, + desc->suite.akcipher.count); +else + err =test_akcipher (tfm, desc->alg, + desc->suite.akcipher.vecs, + desc->suite.akcipher.count); + }crypto_free_akcipher (tfm);return err;
@@ -2982,6 +3299,13 @@ static int alg_test_null(const struct alg_test_desc *desc, .kpp = __VECS(ecdh_tv_template) } }, { + .alg = "ecdsa", + .test = alg_test_akcipher, + .fips_allowed = 1, + .suite = { + .akcipher = __VECS(ecdsa_tv_template) + } + }, { .alg = "gcm(aes)", .test = alg_test_aead, .fips_allowed = 1,
crypto/testmgr.h +140 -0
152 lines not on this page · raw message
Signature · 1 line
1.7.6.3
PATCH v2 6/6 crypto: tcrypt: add ECDSA test modes
In reply to Nitin KumbharUpdate tcrypt module to include a new ECDSA test
modes. It includes:
tcrypt.ko mode=560 for ECDSA sign/verify validation. tcrypt.ko mode=561 for ECDSA sign/verify op perf in cycles. tcrypt.ko mode=561 sec=<seconds> for number of ECDSA sign
verify ops in given time.
--- crypto/tcrypt.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- crypto/tcrypt.h | 122 +++++++++++++++++++++++++++ 2 files changed, 368 insertions(+), 4 deletions(-)
crypto/tcrypt.c +246 -4
@@ -7,6 +7,7 @@* Copyright (c) 2002 James Morris <[email protected]> * Copyright (c) 2002 Jean-Francois Dive <[email protected]> * Copyright (c) 2007 Nokia Siemens Networks +* Copyright (c) 2017 NVIDIA Corporation * * Updated RFC4106 AES-GCM testing. * Authors: Aidan O'Mahony ([email protected])
@@ -27,6 +28,7 @@#include <crypto/aead.h> #include <crypto/hash.h> #include <crypto/skcipher.h> +#include <crypto/akcipher.h> #include <linux/err.h> #include <linux/fips.h> #include <linux/init.h>
@@ -46,10 +48,12 @@#define TVMEMSIZE 4/* -* Used by test_cipher_speed() -*/ -#define ENCRYPT 1 -#define DECRYPT 0 +* Used by test_cipher_speed() +*/ +#define DECRYPT 0 +#define ENCRYPT 1 +#define SIGN 2 +#define VERIFY 3#define MAX_DIGEST_SIZE 64
@@ -996,6 +1000,223 @@ static void test_cipher_speed(const char *algo, int enc, unsigned int secs, false); } +static inline int do_one_akcipher_op (struct akcipher_request *r,int ret) +{ +if (ret == -EINPROGRESS || ret == -EBUSY ) { +struct tcrypt_result *tr = r->base.data; + +wait_for_completion (&tr->completion); +reinit_completion (&tr->completion); + ret = tr->err; + } +return ret; +} + +static int test_akcipher_jiffies (struct akcipher_request *r,int op,int secs) +{ +unsigned long start, end; +int count, ret; + +for (start = jiffies, end = start + secs *HZ , count =0 ; +time_before (jiffies, end); count++) { + +switch (op) { +case SIGN : + ret =do_one_akcipher_op (r,crypto_akcipher_sign (r)); +break ; +case VERIFY : + ret =do_one_akcipher_op (r,crypto_akcipher_verify (r)); +break ; +default : + ret = -EINVAL ; +break ; + } +if (ret) +return ret; + } + +pr_info ("%d operations in %d seconds\n" , count, secs); +return 0 ; +} + +static int test_akcipher_cycles (struct akcipher_request *r,int op) +{ +unsigned long cycles =0 ; +int ret =0 ; +int i; + +/* Warm-up run. */ +for (i =0 ; i <4 ; i++) { +switch (op) { +case SIGN : + ret =do_one_akcipher_op (r,crypto_akcipher_sign (r)); +break ; +case VERIFY : + ret =do_one_akcipher_op (r,crypto_akcipher_verify (r)); +break ; +default : + ret = -EINVAL ; +break ; + } +if (ret) + goto out; + } + +/* The real thing. */ +for (i =0 ; i <8 ; i++) { +cycles_t start, end; + + start =get_cycles (); +switch (op) { +case SIGN : + ret =do_one_akcipher_op (r,crypto_akcipher_sign (r)); +break ; +case VERIFY : + ret =do_one_akcipher_op (r,crypto_akcipher_verify (r)); +break ; +default : + ret = -EINVAL ; +break ; + } + end =get_cycles (); + +if (ret) + goto out; + + cycles += end - start; + } +out: +if (ret ==0 ) +pr_info ("1 operation in %lu cycles\n" , (cycles +4 ) /8 ); + +return ret; +} + +static void test_akcipher_speed (const char *algo,int op,unsigned int secs, +struct akcipher_speed_template *template, +unsigned int tcount,u8 *keysize) +{ +unsigned int ret, i, j; +struct tcrypt_result tresult; +const char *key; +struct akcipher_request *req; +struct crypto_akcipher *tfm; +unsigned int m_size =0 ; +unsigned int nbytes =0 ; +const char *o; + +if (op ==SIGN ) + o ="sign" ; +else if (op ==VERIFY ) + o ="verify" ; +else +return ; + + tfm =crypto_alloc_akcipher (algo,0 ,0 ); +if (IS_ERR (tfm)) { +pr_err ("failed to load transform for %s: %ld\n" , algo, +PTR_ERR (tfm)); +return ; + } + + req =akcipher_request_alloc (tfm,GFP_KERNEL ); +if (!req) { +pr_err ("tcrypt: akcipher: Failed to allocate request for %s\n" , + algo); + goto out; + } + +init_completion (&tresult.completion); +akcipher_request_set_callback (req,CRYPTO_TFM_REQ_MAY_BACKLOG , + tcrypt_complete, &tresult); + + i =0 ; +do { +struct scatterlist sg[TVMEMSIZE ]; + +memset (tvmem[0 ],0xff ,PAGE_SIZE ); + +/* set key */ + key = tvmem[0 ]; +for (j =0 ; j < tcount; j++) { +if (template[j].key_len == *keysize) { + key = template[j].key; +break ; + } + } + + ret =crypto_akcipher_set_pub_key (tfm, key, *keysize); +if (ret) { +pr_err ("set_pub_key() failed\n" ); + goto out_free_req; + } + + ret =crypto_akcipher_set_priv_key (tfm, key, *keysize); +if (ret) { +pr_err ("set_priv_key() failed\n" ); + goto out_free_req; + } + +/* set up src/dst buffs */ +sg_init_table (sg,TVMEMSIZE ); +if (op ==SIGN ) { + m_size = template[j].m_size; + nbytes = template[j].c_size /3 ; + +memcpy (tvmem[0 ], template[j].m, m_size); + +sg_set_buf (&sg[0 ], tvmem[0 ], m_size); +akcipher_request_set_crypt (req, sg, sg, + m_size,PAGE_SIZE ); + }else if (op ==VERIFY ) { + m_size = template[j].m_size; + nbytes = template[j].c_size /3 ; + +memcpy (tvmem[0 ], template[j].m, m_size); +memcpy (tvmem[1 ], (u8 *)(template[j].c) + nbytes, + nbytes); +memcpy (tvmem[2 ], (u8 *)(template[j].c) +2 * nbytes, + nbytes); + +sg_set_buf (&sg[0 ], tvmem[0 ], m_size); +sg_set_buf (&sg[1 ], tvmem[1 ], nbytes); +sg_set_buf (&sg[2 ], tvmem[2 ], nbytes); + +akcipher_request_set_crypt (req, sg, sg, + m_size +2 * nbytes, +PAGE_SIZE ); + }else { +pr_err ("invalid op\n" ); + ret = -EINVAL ; + goto out_free_req; + } + + +pr_info ("\ntesting speed of %s (%s) %s with keysize %d\n" , + algo,get_driver_name (crypto_akcipher, tfm), o, + nbytes *8 ); + +if (secs) + ret =test_akcipher_jiffies (req, op, secs); +else + ret =test_akcipher_cycles (req, op); + +if (ret) { +pr_err ("%s() failed\n" , o); +break ; + } + + i++; + keysize++; + + }while (*keysize); + +out_free_req: +akcipher_request_free (req); +out: +crypto_free_akcipher (tfm); +} +static void test_available (void ) {char **name = check;
@@ -2039,6 +2260,27 @@ static int do_test(const char *alg, u32 type, u32 mask, int m) speed_template_8_32);break ; +case 560 : + ret +=tcrypt_test ("ecdsa" ); +break ; + +case 561 : +#ifndef CONFIG_CRYPTO_FIPS +test_akcipher_speed ("ecdsa" ,SIGN , sec, + ecdsa_speed_template,ECDSA_SPEED_VECTORS , + akc_speed_template_P192); +test_akcipher_speed ("ecdsa" ,VERIFY , sec, + ecdsa_speed_template,ECDSA_SPEED_VECTORS , + akc_speed_template_P192); +#endif +test_akcipher_speed ("ecdsa" ,SIGN , sec, + ecdsa_speed_template,ECDSA_SPEED_VECTORS , + akc_speed_template_P256); +test_akcipher_speed ("ecdsa" ,VERIFY , sec, + ecdsa_speed_template,ECDSA_SPEED_VECTORS , + akc_speed_template_P256); +break ; +case 1000 :test_available ();break ;
crypto/tcrypt.h +122 -0
140 lines not on this page · raw message
Signature · 1 line
1.7.6.3
Re: [PATCH v2 4/6] crypto: ecdsa: add ECDSA SW implementation
In reply to Nitin KumbharAm Freitag, 3. Februar 2017, 16:42:53 CET schrieb Nitin Kumbhar:
Hi Nitin,
Quoted 57 lines
crypto/ecdsa.c new file +57
-0+ +int ecdsa_set_pub_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen) +{ +struct ecdsa_ctx *ctx =ecdsa_get_ctx (tfm); +struct ecdsa params; +unsigned int ndigits; +unsigned int nbytes; +u8 *params_qx, *params_qy; +u64 *ctx_qx, *ctx_qy; +int err =0 ; + +if (crypto_ecdsa_parse_pub_key (key, keylen, ¶ms)) +return -EINVAL ; + + ndigits =ecdsa_supported_curve (params.curve_id); +if (!ndigits) +return -EINVAL ; + + err =ecc_is_pub_key_valid (params.curve_id, ndigits, + params.key, params.key_size); +if (err) +return err; + + ctx->curve_id = params.curve_id; + ctx->ndigits = ndigits; + nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; + + params_qx = params.key; + params_qy = params_qx +ECC_MAX_DIGIT_BYTES ; + + ctx_qx = ctx->public_key; + ctx_qy = ctx_qx +ECC_MAX_DIGITS ; + +vli_copy_from_buf (ctx_qx, ndigits, params_qx, nbytes); +vli_copy_from_buf (ctx_qy, ndigits, params_qy, nbytes); + +memset (¶ms,0 ,sizeof (params)); +return 0 ; +} + +int ecdsa_set_priv_key (struct crypto_akcipher *tfm,const void *key, +unsigned int keylen) +{ +struct ecdsa_ctx *ctx =ecdsa_get_ctx (tfm); +struct ecdsa params; +unsigned int ndigits; +unsigned int nbytes; + +if (crypto_ecdsa_parse_priv_key (key, keylen, ¶ms)) +return -EINVAL ; + + ndigits =ecdsa_supported_curve (params.curve_id); +if (!ndigits) +return -EINVAL ; + + ctx->curve_id = params.curve_id;
crypto/ecdsa.c line 300
Quoted 10 lines
crypto/ecdsa.c new file +10
-0+ ctx->ndigits = ndigits; + nbytes = ndigits <<ECC_DIGITS_TO_BYTES_SHIFT ; + +if (ecc_is_key_valid (ctx->curve_id, ctx->ndigits, + (const u8 *)params.key, params.key_size) <0 ) +return -EINVAL ; + +vli_copy_from_buf (ctx->private_key, ndigits, params.key, nbytes); + +memset (¶ms,0 ,sizeof (params));
Please use memzero_explicit as otherwise this memset will be optimized away. I think it could be used for the set_pub_key too, but there we do not have sensitive data and thus it would not be strictly needed.
Review
Quoted 2 lines
(quoted diff) +2
-0+ return 0; +}
Ciao
Stephan