1 From d73cc256c8e256c32ed959456101b73ba9842f72 Mon Sep 17 00:00:00 2001
2 From: Andy Polyakov <appro@openssl.org>
3 Date: Tue, 1 Dec 2015 09:00:32 +0100
4 Subject: [PATCH] bn/asm/x86_64-mont5.pl: fix carry propagating bug
7 Reviewed-by: Richard Levitte <levitte@openssl.org>
8 (cherry picked from commit e7c078db57908cbf16074c68034977565ffaf107)
10 Upstream-Status: Backport
12 This patch was imported from
13 https://git.openssl.org/?p=openssl.git;a=commit;h=d73cc256c8e256c32ed959456101b73ba9842f72
15 Signed-off-by: Armin Kuster <akuster@mvista.com>
18 crypto/bn/asm/x86_64-mont5.pl | 22 +++++++++++++++++++---
19 crypto/bn/bntest.c | 18 ++++++++++++++++++
20 2 files changed, 37 insertions(+), 3 deletions(-)
22 Index: openssl-1.0.2d/crypto/bn/asm/x86_64-mont5.pl
23 ===================================================================
24 --- openssl-1.0.2d.orig/crypto/bn/asm/x86_64-mont5.pl
25 +++ openssl-1.0.2d/crypto/bn/asm/x86_64-mont5.pl
26 @@ -1779,6 +1779,15 @@ sqr8x_reduction:
29 add (%rdx),%r8 # can this overflow?
36 + adc \$0,%r15 # can't overflow, because we
37 + # started with "overhung" part
42 @@ -3125,6 +3134,15 @@ sqrx8x_reduction:
45 add 24+8(%rsp),%r8 # can this overflow?
52 + adc \$0,%r15 # can't overflow, because we
53 + # started with "overhung" part
55 mov $carry,%rax # xor %rax,%rax
57 sub 16+8(%rsp),$carry # mov 16(%rsp),%cf
58 @@ -3168,13 +3186,11 @@ my ($rptr,$nptr)=("%rdx","%rbp");
59 my @ri=map("%r$_",(10..13));
60 my @ni=map("%r$_",(14..15));
64 sub %r15,%rsi # compare top-most words
73 Index: openssl-1.0.2d/crypto/bn/bntest.c
74 ===================================================================
75 --- openssl-1.0.2d.orig/crypto/bn/bntest.c
76 +++ openssl-1.0.2d/crypto/bn/bntest.c
77 @@ -1027,6 +1027,24 @@ int test_mod_exp_mont_consttime(BIO *bp,
82 + /* Regression test for carry propagation bug in sqr8x_reduction */
83 + BN_hex2bn(&a, "050505050505");
84 + BN_hex2bn(&b, "02");
86 + "4141414141414141414141274141414141414141414141414141414141414141"
87 + "4141414141414141414141414141414141414141414141414141414141414141"
88 + "4141414141414141414141800000000000000000000000000000000000000000"
89 + "0000000000000000000000000000000000000000000000000000000000000000"
90 + "0000000000000000000000000000000000000000000000000000000000000000"
91 + "0000000000000000000000000000000000000000000000000000000001");
92 + BN_mod_exp(d, a, b, c, ctx);
93 + BN_mul(e, a, a, ctx);
95 + fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");