<div dir="ltr"><div class="gmail_default" style="font-size:small">So, I was looking at some RSA key generation code, just because and I suddenly had this thought. I have all these registers here with values that can be used to factor the modulus. So I had better zero them out before I leave.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">        // Declare all registers which may contain values leaking the secret<br>        BigInteger e, d, n,<br>            dp = BigInteger.Zero, dq = BigInteger.Zero, qInv = BigInteger.Zero;<br>        // Create reduced versions of p and q. We do this so that we can<br>        // force erasure at the end<br>        var p1 = p - 1;<br>        var q1 = q - 1;<br><br>        try {       /* stuff */     }<br></div>        finally {<br><br>            // Destroy all local copies of the variables<br>            p.Erase();<br>            q.Erase();<br>            dp.Erase();<br>            dq.Erase();<br>            qInv.Erase();<br>            d.Erase();<br>            }<div><br></div><div><div class="gmail_default" style="font-size:small">Where Erase (this ref BigInteger x) is an extension method that reaches in to the structure and erases the value _bits.</div><br></div><div><br></div><div><div class="gmail_default" style="font-size:small">Which looked all well and good. But then I thought to myself, what if the compiler gets hold of that data and decides to optimize the arithmetic creating temporary variables that it leaves hanging about?</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">I don't think the C# compiler can do that quite yet because there isn't a way to declare the BigInteger classes meet the laws of the arithmetic operators. So even though I can write a = b+c-b, the compiler can't optimize that away to a = b.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">But not too far-fetched to think it might one day in the future.</div><br></div></div>