shithub: riscv

Download patch

ref: 96769e04765511047981e7962d91b979e841f01f
parent: 7f124310099b0ab12463b28f9d39104a8f17bc82
author: cinap_lenrek <[email protected]>
date: Sat Feb 4 21:48:13 EST 2017

libmp: fix mpmod() aliasing bug when n == r and x < 0 (thanks aiju, mischief)

mischief found this in rsafill()'s call mpmod(c2, x, x), where
d parameter is negative (rsagen created a rsa key with negative dk).

--- a/sys/src/libmp/port/mpmod.c
+++ b/sys/src/libmp/port/mpmod.c
@@ -6,11 +6,15 @@
 mpmod(mpint *x, mpint *n, mpint *r)
 {
 	int sign;
+	mpint *ns;
 
 	sign = x->sign;
+	ns = sign < 0 && n == r ? mpcopy(n) : n;
 	if((n->flags & MPfield) == 0
 	|| ((Mfield*)n)->reduce((Mfield*)n, x, r) != 0)
 		mpdiv(x, n, nil, r);
-	if(sign < 0)
-		mpmagsub(n, r, r);
+	if(sign < 0){
+		mpmagsub(ns, r, r);
+		if(ns != n) mpfree(ns);
+	}
 }