shithub: drawterm

Download patch

ref: 24964a29696f5463e947e6c6716e4f5327c2b50d
parent: 64c6eaaaef193ef730f16c6217a6d3e6b8fdb719
author: cinap_lenrek <[email protected]>
date: Sat Feb 4 22:02:48 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/libmp/mpmod.c
+++ b/libmp/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);
+	}
 }