Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: ecdsa - Convert byte arrays with key coordinates to digits

For NIST P192/256/384 the public key's x and y parameters could be copied
directly from a given array since both parameters filled 'ndigits' of
digits (a 'digit' is a u64). For support of NIST P521 the key parameters
need to have leading zeros prepended to the most significant digit since
only 2 bytes of the most significant digit are provided.

Therefore, implement ecc_digits_from_bytes to convert a byte array into an
array of digits and use this function in ecdsa_set_pub_key where an input
byte array needs to be converted into digits.

Suggested-by: Lukas Wunner <lukas@wunner.de>
Tested-by: Lukas Wunner <lukas@wunner.de>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Stefan Berger and committed by
Herbert Xu
d67c96fb 526d23fc

+30 -5
+9 -5
crypto/ecdsa.c
··· 222 222 static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) 223 223 { 224 224 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm); 225 + unsigned int digitlen, ndigits; 225 226 const unsigned char *d = key; 226 - const u64 *digits = (const u64 *)&d[1]; 227 - unsigned int ndigits; 228 227 int ret; 229 228 230 229 ret = ecdsa_ecc_ctx_reset(ctx); ··· 237 238 return -EINVAL; 238 239 239 240 keylen--; 240 - ndigits = (keylen >> 1) / sizeof(u64); 241 + digitlen = keylen >> 1; 242 + 243 + ndigits = DIV_ROUND_UP(digitlen, sizeof(u64)); 241 244 if (ndigits != ctx->curve->g.ndigits) 242 245 return -EINVAL; 243 246 244 - ecc_swap_digits(digits, ctx->pub_key.x, ndigits); 245 - ecc_swap_digits(&digits[ndigits], ctx->pub_key.y, ndigits); 247 + d++; 248 + 249 + ecc_digits_from_bytes(d, digitlen, ctx->pub_key.x, ndigits); 250 + ecc_digits_from_bytes(&d[digitlen], digitlen, ctx->pub_key.y, ndigits); 251 + 246 252 ret = ecc_is_pubkey_valid_full(ctx->curve, &ctx->pub_key); 247 253 248 254 ctx->pub_key_set = ret == 0;
+21
include/crypto/internal/ecc.h
··· 57 57 } 58 58 59 59 /** 60 + * ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array 61 + * @in: Input byte array 62 + * @nbytes Size of input byte array 63 + * @out Output digits array 64 + * @ndigits: Number of digits to create from byte array 65 + */ 66 + static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes, 67 + u64 *out, unsigned int ndigits) 68 + { 69 + unsigned int o = nbytes & 7; 70 + __be64 msd = 0; 71 + 72 + if (o) { 73 + memcpy((u8 *)&msd + sizeof(msd) - o, in, o); 74 + out[--ndigits] = be64_to_cpu(msd); 75 + in += o; 76 + } 77 + ecc_swap_digits(in, out, ndigits); 78 + } 79 + 80 + /** 60 81 * ecc_is_key_valid() - Validate a given ECDH private key 61 82 * 62 83 * @curve_id: id representing the curve to use