ecc.c have algorithms that could be used togeter by ecdh and ecrdsa. Make it separate module. Add CRYPTO_ECC into Kconfig. EXPORT_SYMBOL and document to what seems appropriate. Move structs ecc_point and ecc_curve from ecc_curve_defs.h into ecc.h. No code changes.
Unified · Split
| @@ -248,8 +248,12 @@ config CRYPTO_DH · 247 unmodified lines | ||
| 248 | 248 | help |
| 249 | 249 | Generic implementation of the Diffie-Hellman algorithm. |
| 250 | 250 | |
| 251 | +config CRYPTO_ECC | |
| 252 | + tristate | |
| 253 | + | |
| 251 | 254 | config CRYPTO_ECDH |
| 252 | 255 | tristate "ECDH algorithm" |
| 256 | + select CRYPTO_ECC | |
| 253 | 257 | select CRYPTO_KPP |
| 254 | 258 | select CRYPTO_RNG_DEFAULT |
| 255 | 259 | help |
| @@ -147,8 +147,8 @@ obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o · 146 unmodified lines | ||
| 147 | 147 | obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o |
| 148 | 148 | obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o |
| 149 | 149 | obj-$(CONFIG_CRYPTO_OFB) += ofb.o |
| 150 | +obj-$(CONFIG_CRYPTO_ECC) += ecc.o | |
| 150 | 151 | |
| 151 | −ecdh_generic-y := ecc.o | |
| 152 | 152 | ecdh_generic-y += ecdh.o |
| 153 | 153 | ecdh_generic-y += ecdh_helper.o |
| 154 | 154 | obj-$(CONFIG_CRYPTO_ECDH) += ecdh_generic.o |
| @@ -24,6 +24,7 @@ · 23 unmodified lines | ||
| 24 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | 25 | */ |
| 26 | 26 | |
| 27 | +#include <linux/module.h> | |
| 27 | 28 | #include <linux/random.h> |
| 28 | 29 | #include <linux/slab.h> |
| 29 | 30 | #include <linux/swab.h> |
| @@ -112,7 +113,7 @@ static void vli_clear(u64 *vli, unsigned int ndigits) · 82 unmodified lines | ||
| 112 | 113 | } |
| 113 | 114 | |
| 114 | 115 | /* Returns true if vli == 0, false otherwise. */ |
| 115 | −static bool vli_is_zero(const u64 *vli, unsigned int ndigits) | |
| 116 | +bool vli_is_zero(const u64 *vli, unsigned int ndigits) | |
| 116 | 117 | { |
| 117 | 118 | int i; |
| 118 | 119 | |
| @@ -123,6 +124,7 @@ static bool vli_is_zero(const u64 *vli, unsigned int ndigits) · 4 unmodified lines | ||
| 123 | 124 | |
| 124 | 125 | return true; |
| 125 | 126 | } |
| 127 | +EXPORT_SYMBOL(vli_is_zero); | |
| 126 | 128 | |
| 127 | 129 | /* Returns nonzero if bit bit of vli is set. */ |
| 128 | 130 | static u64 vli_test_bit(const u64 *vli, unsigned int bit) |
| @@ -171,7 +173,7 @@ static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits) · 42 unmodified lines | ||
| 171 | 173 | } |
| 172 | 174 | |
| 173 | 175 | /* Returns sign of left - right. */ |
| 174 | −static int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits) | |
| 176 | +int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits) | |
| 175 | 177 | { |
| 176 | 178 | int i; |
| 177 | 179 | |
| @@ -184,6 +186,7 @@ static int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits) · 6 unmodified lines | ||
| 184 | 186 | |
| 185 | 187 | return 0; |
| 186 | 188 | } |
| 189 | +EXPORT_SYMBOL(vli_cmp); | |
| 187 | 190 | |
| 188 | 191 | /* Computes result = in << c, returning carry. Can modify in place |
| 189 | 192 | * (if result == in). 0 < shift < 64. |
| @@ -240,7 +243,7 @@ static u64 vli_add(u64 *result, const u64 *left, const u64 *right, · 50 unmodified lines | ||
| 240 | 243 | } |
| 241 | 244 | |
| 242 | 245 | /* Computes result = left - right, returning borrow. Can modify in place. */ |
| 243 | −static u64 vli_sub(u64 *result, const u64 *left, const u64 *right, | |
| 246 | +u64 vli_sub(u64 *result, const u64 *left, const u64 *right, | |
| 244 | 247 | unsigned int ndigits) |
| 245 | 248 | { |
| 246 | 249 | u64 borrow = 0; |
| @@ -258,6 +261,7 @@ static u64 vli_sub(u64 *result, const u64 *left, const u64 *right, · 11 unmodified lines | ||
| 258 | 261 | |
| 259 | 262 | return borrow; |
| 260 | 263 | } |
| 264 | +EXPORT_SYMBOL(vli_sub); | |
| 261 | 265 | |
| 262 | 266 | static uint128_t mul_64_64(u64 left, u64 right) |
| 263 | 267 | { |
| @@ -557,7 +561,7 @@ static void vli_mod_square_fast(u64 *result, const u64 *left, · 293 unmodified lines | ||
| 557 | 561 | * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" |
| 558 | 562 | * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf |
| 559 | 563 | */ |
| 560 | −static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod, | |
| 564 | +void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod, | |
| 561 | 565 | unsigned int ndigits) |
| 562 | 566 | { |
| 563 | 567 | u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS]; |
| @@ -630,6 +634,7 @@ static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod, · 66 unmodified lines | ||
| 630 | 634 | |
| 631 | 635 | vli_set(result, u, ndigits); |
| 632 | 636 | } |
| 637 | +EXPORT_SYMBOL(vli_mod_inv); | |
| 633 | 638 | |
| 634 | 639 | /* ------ Point operations ------ */ |
| 635 | 640 | |
| @@ -948,6 +953,7 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, · 312 unmodified lines | ||
| 948 | 953 | |
| 949 | 954 | return __ecc_is_key_valid(curve, private_key, ndigits); |
| 950 | 955 | } |
| 956 | +EXPORT_SYMBOL(ecc_is_key_valid); | |
| 951 | 957 | |
| 952 | 958 | /* |
| 953 | 959 | * ECC private keys are generated using the method of extra random bits, |
| @@ -1000,6 +1006,7 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey) · 46 unmodified lines | ||
| 1000 | 1006 | |
| 1001 | 1007 | return 0; |
| 1002 | 1008 | } |
| 1009 | +EXPORT_SYMBOL(ecc_gen_privkey); | |
| 1003 | 1010 | |
| 1004 | 1011 | int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits, |
| 1005 | 1012 | const u64 *private_key, u64 *public_key) |
| @@ -1036,10 +1043,11 @@ err_free_point: · 30 unmodified lines | ||
| 1036 | 1043 | out: |
| 1037 | 1044 | return ret; |
| 1038 | 1045 | } |
| 1046 | +EXPORT_SYMBOL(ecc_make_pub_key); | |
| 1039 | 1047 | |
| 1040 | 1048 | /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */ |
| 1041 | −static int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve, | |
| 1042 | − struct ecc_point *pk) | |
| 1049 | +int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve, | |
| 1050 | + struct ecc_point *pk) | |
| 1043 | 1051 | { |
| 1044 | 1052 | u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS]; |
| 1045 | 1053 | |
| @@ -1064,8 +1072,8 @@ static int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve, · 18 unmodified lines | ||
| 1064 | 1072 | return -EINVAL; |
| 1065 | 1073 | |
| 1066 | 1074 | return 0; |
| 1067 | − | |
| 1068 | 1075 | } |
| 1076 | +EXPORT_SYMBOL(ecc_is_pubkey_valid_partial); | |
| 1069 | 1077 | |
| 1070 | 1078 | int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, |
| 1071 | 1079 | const u64 *private_key, const u64 *public_key, |
| @@ -1121,3 +1129,6 @@ err_alloc_product: · 49 unmodified lines | ||
| 1121 | 1129 | out: |
| 1122 | 1130 | return ret; |
| 1123 | 1131 | } |
| 1132 | +EXPORT_SYMBOL(crypto_ecdh_shared_secret); | |
| 1133 | + | |
| 1134 | +MODULE_LICENSE("Dual BSD/GPL"); | |
| @@ -32,6 +32,41 @@ · 31 unmodified lines | ||
| 32 | 32 | |
| 33 | 33 | #define ECC_DIGITS_TO_BYTES_SHIFT 3 |
| 34 | 34 | |
| 35 | +/** | |
| 36 | + * struct ecc_point - elliptic curve point in affine coordinates | |
| 37 | + * | |
| 38 | + * @x: X coordinate in vli form. | |
| 39 | + * @y: Y coordinate in vli form. | |
| 40 | + * @ndigits: Length of vlis in u64 qwords. | |
| 41 | + */ | |
| 42 | +struct ecc_point { | |
| 43 | + u64 *x; | |
| 44 | + u64 *y; | |
| 45 | + u8 ndigits; | |
| 46 | +}; | |
| 47 | + | |
| 48 | +/** | |
| 49 | + * struct ecc_curve - definition of elliptic curve | |
| 50 | + * | |
| 51 | + * @name: Short name of the curve. | |
| 52 | + * @g: Generator point of the curve. | |
| 53 | + * @p: Prime number, if Barrett's reduction is used for this curve | |
| 54 | + * pre-calculated value 'mu' is appended to the @p after ndigits. | |
| 55 | + * Use of Barrett's reduction is heuristically determined in | |
| 56 | + * vli_mmod_fast(). | |
| 57 | + * @n: Order of the curve group. | |
| 58 | + * @a: Curve parameter a. | |
| 59 | + * @b: Curve parameter b. | |
| 60 | + */ | |
| 61 | +struct ecc_curve { | |
| 62 | + char *name; | |
| 63 | + struct ecc_point g; | |
| 64 | + u64 *p; | |
| 65 | + u64 *n; | |
| 66 | + u64 *a; | |
| 67 | + u64 *b; | |
| 68 | +}; | |
| 69 | + | |
| 35 | 70 | /** |
| 36 | 71 | * ecc_is_key_valid() - Validate a given ECDH private key |
| 37 | 72 | * |
| @@ -91,4 +126,68 @@ int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits, · 53 unmodified lines | ||
| 91 | 126 | int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, |
| 92 | 127 | const u64 *private_key, const u64 *public_key, |
| 93 | 128 | u64 *secret); |
| 129 | + | |
| 130 | +/** | |
| 131 | + * ecc_is_pubkey_valid_partial() - Partial public key validation | |
| 132 | + * | |
| 133 | + * @curve: elliptic curve domain parameters | |
| 134 | + * @pk: public key as a point | |
| 135 | + * | |
| 136 | + * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial | |
| 137 | + * Public-Key Validation Routine. | |
| 138 | + * | |
| 139 | + * Note: There is no check that the public key is in the correct elliptic curve | |
| 140 | + * subgroup. | |
| 141 | + * | |
| 142 | + * Return: 0 if validation is successful, -EINVAL if validation is failed. | |
| 143 | + */ | |
| 144 | +int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve, | |
| 145 | + struct ecc_point *pk); | |
| 146 | + | |
| 147 | +/** | |
| 148 | + * vli_is_zero() - Determine is vli is zero | |
| 149 | + * | |
| 150 | + * @vli: vli to check. | |
| 151 | + * @ndigits: length of the @vli | |
| 152 | + */ | |
| 153 | +bool vli_is_zero(const u64 *vli, unsigned int ndigits); | |
| 154 | + | |
| 155 | +/** | |
| 156 | + * vli_cmp() - compare left and right vlis | |
| 157 | + * | |
| 158 | + * @left: vli | |
| 159 | + * @right: vli | |
| 160 | + * @ndigits: length of both vlis | |
| 161 | + * | |
| 162 | + * Returns sign of @left - @right, i.e. -1 if @left < @right, | |
| 163 | + * 0 if @left == @right, 1 if @left > @right. | |
| 164 | + */ | |
| 165 | +int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits); | |
| 166 | + | |
| 167 | +/** | |
| 168 | + * vli_sub() - Subtracts right from left | |
| 169 | + * | |
| 170 | + * @result: where to write result | |
| 171 | + * @left: vli | |
| 172 | + * @right vli | |
| 173 | + * @ndigits: length of all vlis | |
| 174 | + * | |
| 175 | + * Note: can modify in-place. | |
| 176 | + * | |
| 177 | + * Return: carry bit. | |
| 178 | + */ | |
| 179 | +u64 vli_sub(u64 *result, const u64 *left, const u64 *right, | |
| 180 | + unsigned int ndigits); | |
| 181 | + | |
| 182 | +/** | |
| 183 | + * vli_mod_inv() - Modular inversion | |
| 184 | + * | |
| 185 | + * @result: where to write vli number | |
| 186 | + * @input: vli value to operate on | |
| 187 | + * @mod: modulus | |
| 188 | + * @ndigits: length of all vlis | |
| 189 | + */ | |
| 190 | +void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod, | |
| 191 | + unsigned int ndigits); | |
| 192 | + | |
| 94 | 193 | #endif |
| @@ -2,21 +2,6 @@ · 1 unmodified line | ||
| 2 | 2 | #ifndef _CRYTO_ECC_CURVE_DEFS_H |
| 3 | 3 | #define _CRYTO_ECC_CURVE_DEFS_H |
| 4 | 4 | |
| 5 | −struct ecc_point { | |
| 6 | − u64 *x; | |
| 7 | − u64 *y; | |
| 8 | − u8 ndigits; | |
| 9 | −}; | |
| 10 | − | |
| 11 | −struct ecc_curve { | |
| 12 | − char *name; | |
| 13 | − struct ecc_point g; | |
| 14 | − u64 *p; | |
| 15 | − u64 *n; | |
| 16 | − u64 *a; | |
| 17 | − u64 *b; | |
| 18 | −}; | |
| 19 | − | |
| 20 | 5 | /* NIST P-192: a = p - 3 */ |
| 21 | 6 | static u64 nist_p192_g_x[] = { 0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, |
| 22 | 7 | 0x188DA80EB03090F6ull }; |