diff options
author | Werner Koch <wk@gnupg.org> | 2011-02-01 16:13:37 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2011-02-01 16:13:37 +0100 |
commit | dbf7709d6339f74d7a88c96440e92576f3109486 (patch) | |
tree | a284528d6b0342c931cf04081d5b7a0a6132bed3 | |
parent | 5c4179860ef0b31b4c6ba957be9fa707a0ba7d9b (diff) | |
download | libgcrypt-dbf7709d6339f74d7a88c96440e92576f3109486.tar.gz |
Handle opaque MPIs in gcry_mpi_cmp
-rw-r--r-- | NEWS | 5 | ||||
-rw-r--r-- | doc/gcrypt.texi | 5 | ||||
-rw-r--r-- | mpi/ChangeLog | 4 | ||||
-rw-r--r-- | mpi/mpi-cmp.c | 59 |
4 files changed, 50 insertions, 23 deletions
@@ -22,7 +22,10 @@ Noteworthy changes in version 1.5.x (unreleased) * gcry_sexp_build does now support opaque MPIs with "%m". - * New function gcry_pk_get_curve to map ECC parameters to a curve name. + * New function gcry_pk_get_curve to map ECC parameters to a curve + name. + + * gcry_mpi_cmp applied to opaque values has a defined semantic now. * Interface changes relative to the 1.4.2 release: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi index 663ca5e4..886c3962 100644 --- a/doc/gcrypt.texi +++ b/doc/gcrypt.texi @@ -4507,7 +4507,10 @@ The next 2 functions are used to compare MPIs: Compare the multi-precision-integers number @var{u} and @var{v} returning 0 for equality, a positive value for @var{u} > @var{v} and a -negative for @var{u} < @var{v}. +negative for @var{u} < @var{v}. If both numbers are opaque values +(cf, gcry_mpi_set_opaque) the comparison is done by checking the bit +sizes using memcmp. If only one number is an opaque value, the opaque +value is less than the other number. @end deftypefun @deftypefun int gcry_mpi_cmp_ui (@w{const gcry_mpi_t @var{u}}, @w{unsigned long @var{v}}) diff --git a/mpi/ChangeLog b/mpi/ChangeLog index fb6ea33d..2bf3f2bf 100644 --- a/mpi/ChangeLog +++ b/mpi/ChangeLog @@ -1,3 +1,7 @@ +2011-02-01 Werner Koch <wk@g10code.com> + + * mpi-cmp.c (gcry_mpi_cmp): Allow comparing of opaque MPIs. + 2010-04-12 Brad Hards <bradh@frogmouth.net> (wk) Spelling fixes. diff --git a/mpi/mpi-cmp.c b/mpi/mpi-cmp.c index 9dd10830..30e1fce9 100644 --- a/mpi/mpi-cmp.c +++ b/mpi/mpi-cmp.c @@ -53,6 +53,7 @@ gcry_mpi_cmp_ui (gcry_mpi_t u, unsigned long v) return 1; } + int gcry_mpi_cmp (gcry_mpi_t u, gcry_mpi_t v) { @@ -60,31 +61,47 @@ gcry_mpi_cmp (gcry_mpi_t u, gcry_mpi_t v) mpi_size_t vsize; int cmp; - _gcry_mpi_normalize (u); - _gcry_mpi_normalize (v); - - usize = u->nlimbs; - vsize = v->nlimbs; + if (mpi_is_opaque (u) || mpi_is_opaque (v)) + { + if (mpi_is_opaque (u) && !mpi_is_opaque (v)) + return -1; + if (!mpi_is_opaque (u) && mpi_is_opaque (v)) + return 1; + if (!u->sign && !v->sign) + return 0; /* Empty buffers are identical. */ + if (u->sign < v->sign) + return -1; + if (u->sign > v->sign) + return 1; + return memcmp (u->d, v->d, (u->sign+7)/8); + } + else + { + _gcry_mpi_normalize (u); + _gcry_mpi_normalize (v); - /* Compare sign bits. */ + usize = u->nlimbs; + vsize = v->nlimbs; - if (!u->sign && v->sign) - return 1; - if (u->sign && !v->sign) - return -1; + /* Compare sign bits. */ - /* U and V are either both positive or both negative. */ + if (!u->sign && v->sign) + return 1; + if (u->sign && !v->sign) + return -1; - if( usize != vsize && !u->sign && !v->sign ) - return usize - vsize; - if( usize != vsize && u->sign && v->sign ) - return vsize + usize; - if( !usize ) - return 0; - if( !(cmp = _gcry_mpih_cmp( u->d, v->d, usize )) ) - return 0; - if( (cmp < 0?1:0) == (u->sign?1:0)) - return 1; + /* U and V are either both positive or both negative. */ + if (usize != vsize && !u->sign && !v->sign) + return usize - vsize; + if (usize != vsize && u->sign && v->sign) + return vsize + usize; + if (!usize ) + return 0; + if (!(cmp = _gcry_mpih_cmp (u->d, v->d, usize))) + return 0; + if ((cmp < 0?1:0) == (u->sign?1:0)) + return 1; + } return -1; } |