Re: Side-channel leakage in the mpz_powm_sec interface

2023-09-07 Thread Hubert Kario

On Friday, 25 August 2023 14:02:51 CEST, Niels Möller wrote:

Hubert Kario  writes:


On Friday, 25 August 2023 07:51:10 CEST, Niels Möller wrote:
Thus, I think it's more of a documentation issue than code issue:
it will be much better to just say that mpz_powm_sec() is not usable for
cryptographic purposes and to deprecate it.


Not sure if "unusable" is right, but besides that, I agree documenting
the problem, and discouraging usage, is reasonable action.


So... how do we do that?

Should I prepare a patch for this (I'm guessing that the doc sources are
together with code), or are you going to do that?
--
Regards,
Hubert Kario
Principal Quality Engineer, RHEL Crypto team
Web: www.cz.redhat.com
Red Hat Czech s.r.o., Purkyňova 99/71, 612 45, Brno, Czech Republic

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: Side-channel leakage in the mpz_powm_sec interface

2023-08-25 Thread Hubert Kario

On Friday, 25 August 2023 12:14:46 CEST, Torbjörn Granlund wrote:

marco.bodr...@tutanota.com writes:

  This sounds not too complex to do. Even if, as usual, C code 
is prone to compiler optimizations...

  I think this is a good idea.

The concern for compilers messing up secure code is very valid.  An
example is that clang makes our mpn_sec_tabselect useless by inserting a
conditional branch instead of our mask; the generated code actually
adheres to the volatile declaration, but is utterly useless for its
intended purpose.


In my experience inserting an __asm__ block that does nothing but uses a
variable (to make it seem volatile to the compiler), is quite effective
in combating this kind of optimisations


It is a pity that we cannot write certain sensitive code in C.  A C
language extension would be welcome, perhaps some kind of "strict
volatile", or an annotated conditional requiring that no branch or other
conditional execution is generated as a result of it.

A side note: CPU conditional move and conditional select instructions
are usually poorly defined.  Is there a true dependency on all inputs,
or only on the actually chosen input?  I've used my contacts at CPU
manufacturers to address this concern, but never seen any real interest.

Back to the main subject:

We should probably exclude certain sec_ functions when not all sensitive
functions are provided in asm, and thereby as a result of
--disable-assembly?  We should in essence not provide C versions of
e.g., mpn_sec_tabselect.

And we should perhaps provide a normalisation asm function for what
Niels proposes here?

I think we should document mpz_powm_sec as somewhat problematic, but
also fix it along the lines of Niels' proposal.  The right GMP level for
side-channel sensitive application is clearly mpn; we should say that.


If you decide to fix it, then both the loading of the operands from byte
strings in constant time and then converting the output to a byte string
in constant time will have to be both ensured and documented.
As well as the fact the the operands can't be used for anything but this
specific operation or the processing won't be side-channel secure.


We might want to be more cautions about what we promise also for mpn.
It is not necessarily sufficient to do what we do here, i.e. perform the
exact same instruction sequence an data reference sequence for any two
n-bit operand sets.  Careful power measurements typically can
fingerprint either or both operands of a bignum multiply.  Therefore,
additional layers of side-channel obfuscation is needed, like standard
RSA message blinding, mod argument blinding, exponent blinding.


sure, but I think that should be performed by upper level code, as how
you do blinding depends on the algorithm and operation you're performing

like, both RSA signing and decryption mathematically perform the exact same
operation, but what constitutes side-channel free for them is very 
different

--
Regards,
Hubert Kario
Principal Quality Engineer, RHEL Crypto team
Web: www.cz.redhat.com
Red Hat Czech s.r.o., Purkyňova 99/71, 612 45, Brno, Czech Republic

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: Side-channel leakage in the mpz_powm_sec interface

2023-08-25 Thread Hubert Kario

On Friday, 25 August 2023 14:02:51 CEST, Niels Möller wrote:

Hubert Kario  writes:


On Friday, 25 August 2023 07:51:10 CEST, Niels Möller wrote:

And that's for all inputs? Nice.



Well, not all, I mostly tested ones that had obvious patterns, like
zero to power zero, 0xff to power 0xff, full word to power of 1, full
word to power (mod-1), etc. All with 2048 bit operands.


Ah, I didn't mean for all possible input values, just all the input
*arguments*.


haven't tested with different moduli


If we're doing CRT, then the modulus isn't public either,


You're right, of course.

The leak I was thinking of was using plain binvert_limb on the least
significant limb of the modulo, which includes this table lookup on bits
7-1: https://gmplib.org/repo/gmp/file/tip/gmp-impl.h#l3378


I'm afraid this is too deep in the code for me to immediately follow...
Is this with relation to the input or the output?
Which low order bits should I test, base, exponent, modulus, or result?


I think older versions of mpn_sec_pown use to do that, and at the time, I
figured it was not a big problem. I'm not aware of any interesting
attacks on RSA where it helps to know just a few of the least
significant bits of the secret factors.


could help with other side-channel attacks, like power attack


Thus, I think it's more of a documentation issue than code issue:
it will be much better to just say that mpz_powm_sec() is not usable for
cryptographic purposes and to deprecate it.


Not sure if "unusable" is right, but besides that, I agree documenting
the problem, and discouraging usage, is reasonable action.


without a _sec variant of the mpz_limbs_write()/mpz_limbs_finish() pair
I don't see how it can be used. And given that there is no 
mpz_mul_mod_sec(),
I don't see how mpz_powm_sec() alone can be used for RSA (given that means 
no

CRT). While you can do RSA without CRT, that sounds to me like a toy
implementation, not something you'd would want to use in production.

So that means it is useful for FFDH only, but then you should use ephemeral
one where side channel doesn't matter much...

--
Regards,
Hubert Kario
Principal Quality Engineer, RHEL Crypto team
Web: www.cz.redhat.com
Red Hat Czech s.r.o., Purkyňova 99/71, 612 45, Brno, Czech Republic

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Re: Side-channel leakage in the mpz_powm_sec interface

2023-08-25 Thread Hubert Kario

On Friday, 25 August 2023 07:51:10 CEST, Niels Möller wrote:

Hubert Kario  writes:

I was able to confirm that the low-level functions, like the 
mpn_sec_powm()

function have no timing leakage with regards to operands or result
(exactly like section 8.1 of the manual[2] states).


And that's for all inputs? Nice. (I think an earlier version used a
potentially leaking table lookup on the lowest few modulo bits, as the
first step in computing a partial inverse. Which is probably benign in
most use-cases, but still undesirable for a function claimed to be
side-channel silent).


Well, not all, I mostly tested ones that had obvious patterns, like
zero to power zero, 0xff to power 0xff, full word to power of 1, full word 
to power (mod-1), etc. All with 2048 bit operands.


If you could give me few examples of inputs that would trigger use of
different elements in the table lookups, I may look into that.


I wasn't able to do the same with regards to the mpz_powm_sec() function.

Irrespective of how I initialised the used mpz_t objects, if the operands
don't have high order words set, the timing of the operation is different.
Thus I believe that if mpz_powm_sec() is used for RSA or Diffie-Hellman
it would be vulnerable to the Bleichenbacher or Raccoon attacks
respectively.


Is the main problem the normalization of the *output* of mpz_powm_sec?
For this class of attacks, my understanding is that the secret exponents
are fixed (when doing repeated operations on the secret key under
attack), while the modulo is public, and the base is under the control
of the attacker and hence already known to them.


If we're doing CRT, then the modulus isn't public either, but then that 
brings

a whole lot of other problems to the mpz interface (multiplication for
unlblinding, modulo operation, conversion from mpz to byte string, etc.)...

I haven't delved deep enough to say if it is caused by input or output, 
just
enough to be able to say that secure use of mpn API was obvious and 
trivial,

while mpn was not possible (no matter how I initialised the mpn objects
from the byte strings that were the real inputs).


Did I miss the methods to ensure that the objects are not clamped, or
should
the mpz_powm_sec() interface be marked as _not_ secure for cryptographic
purposes?


It's preferable to use the mpn_powm_sec. When using mpz_t, I see no
reasonable to avoid leakage of the normalized size (or number of
all-zero limbs at the most significant end).

Regarding Nettle (which uses GMP, and is used by GnuTLS): The RSA code
dates from the 1990s (it's one of the oldest algorithms in Nettle), and
has seen a series of incremental improvements over the years, but aiming
to not break api compatibility more than necessary. Most recently
side-channel-silent "decoding" in pkcs#1 RAS decryption. The current
interface is more complex than I'd like, with several variants of most
private key operations, which is a bit confusing.


I did test GnuTLS (and we have discovered an issue: CVE-2023-0361), but 
haven't

found any side-channels related to numerical operations. AFAIK, that was
fixed as a result of CVE-2018-16868, and GnuTLS is using only mpn 
interfaces

throughout for RSA operations now.


It would be desirable to with a larger rework; it would be better to (i)
use byte strings, rather than mpz_t values, for the interface to
applications, and (ii) use only mpn-level GMP functions internally. And
I would recommend the same approach for any other implementation of RSA
on top of GMP.


Honestly, I don't think it's a good idea to rework mpz interfaces so that
they are actually safe for cryptography, the mpn interface is far better
to that, as you point out, all numbers in crypto should be considered
a constant length byte strings, not integers. And mpn is far more natural 
for

handling them as byte strings.

Thus, I think it's more of a documentation issue than code issue:
it will be much better to just say that mpz_powm_sec() is not usable for
cryptographic purposes and to deprecate it.
--
Regards,
Hubert Kario
Principal Quality Engineer, RHEL Crypto team
Web: www.cz.redhat.com
Red Hat Czech s.r.o., Purkyňova 99/71, 612 45, Brno, Czech Republic

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs


Side-channel leakage in the mpz_powm_sec interface

2023-08-24 Thread Hubert Kario

Hello,

While I was researching CVE-2022-4304 in OpenSSL, I looked into some other
implementations (specifically to see if there are constant-time 
implementations

of modular arithmetic).

I was able to confirm that the low-level functions, like the mpn_sec_powm()
function have no timing leakage with regards to operands or result
(exactly like section 8.1 of the manual[2] states).
I wasn't able to do the same with regards to the mpz_powm_sec() function.

Irrespective of how I initialised the used mpz_t objects, if the operands
don't have high order words set, the timing of the operation is different.
Thus I believe that if mpz_powm_sec() is used for RSA or Diffie-Hellman
it would be vulnerable to the Bleichenbacher or Raccoon attacks 
respectively.
This is despite the documentation stating that "This function is intended 
for
cryptographic purposes, where resilience to side-channel attacks is 
desired."[1]


I think this is likely caused by exactly the same issue as in OpenSSL: that
the mpz objects are "clamped" or "normalised", where the methods make sure
that the returned object doesn't use more memory than necessary to store 
the

number.

Did I miss the methods to ensure that the objects are not clamped, or 
should

the mpz_powm_sec() interface be marked as _not_ secure for cryptographic
purposes?

1 - 
https://gmplib.org/manual/Integer-Exponentiation#index-mpz_005fpowm_005fsec

2 - https://gmplib.org/manual/Low_002dlevel-Functions
--
Regards,
Hubert Kario
Principal Quality Engineer, RHEL Crypto team
Web: www.cz.redhat.com
Red Hat Czech s.r.o., Purkyňova 99/71, 612 45, Brno, Czech Republic

___
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs