Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp1588509imd; Sun, 4 Nov 2018 05:55:10 -0800 (PST) X-Google-Smtp-Source: AJdET5cq6r2Z2/rxfIJuqh8F7pnqezvpz7U+VkJdPTlYrpemnb6aP4GRYdRgjBgvPMOl7ud4cknx X-Received: by 2002:a17:902:5a8b:: with SMTP id r11-v6mr19175770pli.305.1541339710410; Sun, 04 Nov 2018 05:55:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541339710; cv=none; d=google.com; s=arc-20160816; b=cA/Isl2rXqJHwamXr4tKRKa9/OOC0WN/JbOek/81BY0Ebr4DynP2giCqSZ2PzrLoCD f7INZtxf5UnJ200ZWU7v3nhqiZZjES0V86NmtG+HF40zOrWdWT76hgMlCDbwPQ7t+Svg i0+JZxsTHbPM4rV2h6Zi7WnhsfefLg+D9VurJOtBjppkoyc8BnmliIuWiOVrnPt+OJuW KtZy/VWcHDDHS9aABFT75ZkBsFdPM1rII9/KmqfDRsyf3l96Qg32hwKfBIqGUOszqPJO 8W2gU27V00s6PtPrC8wxStmSXjBKIdwalD326ZbgdSSBJuj+gdBz+qvC1Wgwhj3VZiNQ JNSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=n2v4AgFjMcV2Ra+2mjpV+jC2x9DRA7MFN+wrUy5ItnA=; b=yCP4KmYnRW+sJjU9abxGpxwNZ6OkrBs0O4AHNcJhMHj+uzlRWvX72i1rc49cOIEgM+ 5JQKTEAiqnjLP627xxMUpwH8u8lgOZbhCPum/xwKnDNQQQEax+zHdPjzf+iRBLBYWFVi pephClODGlQOxhwO1dJIa3z1Z4dcJCkU6RMtLH29ZNz1Yuvs+2yCskYN9/xh8WbelXx3 IPTU+vNYAkZLWMsAm+o9iVNN3n7oo4WSptTX5oYXUh0XNtn6hrxcUON6AwXSEmSHDcwq zxC7uAflOzyjQhg56l16d15EKKK3qaz91KtzjM9UFpDkd0XcS1gqZBYoQvJj2rWVvTHm W9tw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k33-v6si39538884pld.151.2018.11.04.05.54.55; Sun, 04 Nov 2018 05:55:10 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729094AbeKDWtg (ORCPT + 99 others); Sun, 4 Nov 2018 17:49:36 -0500 Received: from vmicros1.altlinux.org ([194.107.17.57]:44590 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728897AbeKDWtg (ORCPT ); Sun, 4 Nov 2018 17:49:36 -0500 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 28A6572CC68; Sun, 4 Nov 2018 16:34:32 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id 0227D4A4A29; Sun, 4 Nov 2018 16:34:31 +0300 (MSK) From: Vitaly Chikunov To: Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vitaly Chikunov Subject: [PATCH] crypto: ecc - regularize scalar for scalar multiplication Date: Sun, 4 Nov 2018 16:34:20 +0300 Message-Id: <20181104133420.15783-1-vt@altlinux.org> X-Mailer: git-send-email 2.11.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ecc_point_mult is supposed to be used with a regularized scalar, otherwise, it's possible to deduce the position of the top bit of the scalar with timing attack. ecc_point_mult is already using a regular algorithm (i.e. having an operation flow independent of the input scalar) but regularization step is forgotten. Regularization step is based on micro-ecc by Kenneth MacKay. Signed-off-by: Vitaly Chikunov --- crypto/ecc.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/crypto/ecc.c b/crypto/ecc.c index 8facafd67802..adcce310f646 100644 --- a/crypto/ecc.c +++ b/crypto/ecc.c @@ -842,15 +842,23 @@ static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime, static void ecc_point_mult(struct ecc_point *result, const struct ecc_point *point, const u64 *scalar, - u64 *initial_z, u64 *curve_prime, + u64 *initial_z, const struct ecc_curve *curve, unsigned int ndigits) { /* R0 and R1 */ u64 rx[2][ECC_MAX_DIGITS]; u64 ry[2][ECC_MAX_DIGITS]; u64 z[ECC_MAX_DIGITS]; + u64 sk[2][ECC_MAX_DIGITS]; + u64 *curve_prime = curve->p; int i, nb; - int num_bits = vli_num_bits(scalar, ndigits); + int num_bits; + int carry; + + carry = vli_add(sk[0], scalar, curve->n, ndigits); + vli_add(sk[1], sk[0], curve->n, ndigits); + scalar = sk[!carry]; + num_bits = sizeof(u64) * ndigits * 8 + 1; vli_set(rx[1], point->x, ndigits); vli_set(ry[1], point->y, ndigits); @@ -1004,7 +1012,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits, goto out; } - ecc_point_mult(pk, &curve->g, priv, NULL, curve->p, ndigits); + ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits); if (ecc_point_is_zero(pk)) { ret = -EAGAIN; goto err_free_point; @@ -1090,7 +1098,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, goto err_alloc_product; } - ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits); + ecc_point_mult(product, pk, priv, rand_z, curve, ndigits); ecc_swap_digits(product->x, secret, ndigits); -- 2.11.0