Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3204087imu; Sun, 11 Nov 2018 09:42:09 -0800 (PST) X-Google-Smtp-Source: AJdET5cP3Ob+acm2gpjxhhPPOv2Bv4fDNA5RIYTbtSlmFd4SHel+CBHMeJQO7w+OE/HznGkxexYs X-Received: by 2002:a63:e247:: with SMTP id y7mr14071977pgj.84.1541958129200; Sun, 11 Nov 2018 09:42:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541958129; cv=none; d=google.com; s=arc-20160816; b=gxT7bD/+HsbH40Ps9bhIZgJkCWiG0VA1E076aoM7P2PctLIbq1PLrKsAaCHr0sQo0b x8YzNWuDxdB00RklQVSiNM9KOGhmpdGkss5ehJX8EV8524O24mputUaAkqIwlZJhmtNM 7+w6cRRA20VHhETUcrf0zgV+8e+zXSuPyibcrNxvHroSBf4C3flEUd1lODIOtM5kF7Iv R/wGAi0gWD1KNkEHPFtVPAqxOUmqWTzlIkOLg0Xv9FRRu8OMYuLebdspvCX6s4yZO5Gt yFiW6cZD5pCE7ivX6XsddlS/6oa11mEpPchKM3lZT6YLeUhDxh04JFCBJvYYV6xfc6KW MkRw== 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=Ui9Hrh617/uLW8rokhgykDa3NA6/oYgk1MhfN4A2btM=; b=g0jj+r7VV+lqi+ovzKUmyqox6oTPm2uBiKPZnALLpED/WqLFeFVRBlcC2eYLuKdwQn e7d1AOFa4QfYdNBBUmSpIZ83HMV33i9YJzWFQTfd6mx6zxvX5tbUK+RSnuuU1MQEN4sD tbFw3vwv6vj1LuVQyAfO+IfqvR+sKydADSOm8esuWcPDrnMacDd/FDe43eiPEjqaCK3m /xDaH87E5C4yJeOxXVLbvGRzmzskT8KuPqqwgpFCZaWyqqOzZbW/ejniJKY2LWJpfXSB W9w6onZ6RgmNY6U8f1eRobQKpvq/+CcUTJYig43vO/FtNhlvlMCpkReYB0Z97RWF+FuP 0AEA== 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 i4-v6si15996681pfg.185.2018.11.11.09.41.53; Sun, 11 Nov 2018 09:42:09 -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 S1729339AbeKLD3T (ORCPT + 99 others); Sun, 11 Nov 2018 22:29:19 -0500 Received: from vmicros1.altlinux.org ([194.107.17.57]:42866 "EHLO vmicros1.altlinux.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729222AbeKLD3T (ORCPT ); Sun, 11 Nov 2018 22:29:19 -0500 Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 1310672CA65; Sun, 11 Nov 2018 20:40:07 +0300 (MSK) Received: from beacon.altlinux.org (unknown [185.6.174.98]) by imap.altlinux.org (Postfix) with ESMTPSA id D62A94A4AF1; Sun, 11 Nov 2018 20:40:06 +0300 (MSK) From: Vitaly Chikunov To: Herbert Xu , "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Vitaly Chikunov , kernel-hardening@lists.openwall.com Subject: [PATCH v2] crypto: ecc - regularize scalar for scalar multiplication Date: Sun, 11 Nov 2018 20:40:02 +0300 Message-Id: <20181111174002.30929-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. This is important when the scalar is a private key. ecc_point_mult is already using a regular algorithm (i.e. having an operation flow independent of the input scalar) but regularization step is not implemented. Arrange scalar to always have fixed top bit by adding a multiple of the curve order (n). References: The constant time regularization step is based on micro-ecc by Kenneth MacKay and also referenced in the literature (Bernstein, D. J., & Lange, T. (2017). Montgomery curves and the Montgomery ladder. (Cryptology ePrint Archive; Vol. 2017/293). s.l.: IACR. Chapter 4.6.2.) Signed-off-by: Vitaly Chikunov Cc: kernel-hardening@lists.openwall.com --- Changes from v1: - No code changes, only description updates to be more informative. 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