diff options
author | agc <agc> | 2015-10-01 01:14:07 +0000 |
---|---|---|
committer | agc <agc> | 2015-10-01 01:14:07 +0000 |
commit | d84a3ffcb27a6cd87429deaa551c773c1d02b646 (patch) | |
tree | 710adb69aed624d0bca8d4c78d0994c689db7d31 /security | |
parent | 3dea297c3dce4b74ce55117e03e67192d2c00ce4 (diff) | |
download | pkgsrc-d84a3ffcb27a6cd87429deaa551c773c1d02b646.tar.gz |
Update netpgpverify to 20150930.
Changes since 20150919:
+ fixed minor bug in BN_rand() function - used field wasn't set
+ added BN_gcd() function
+ added translation layer in header file, so that library
can be called as a BIGNUM/BN_* replacement if USE_BN_INTERFACE
is defined at compile-time
Diffstat (limited to 'security')
-rw-r--r-- | security/netpgpverify/Makefile | 4 | ||||
-rw-r--r-- | security/netpgpverify/files/bignum.c | 96 | ||||
-rw-r--r-- | security/netpgpverify/files/bn.h | 61 | ||||
-rw-r--r-- | security/netpgpverify/files/verify.h | 4 |
4 files changed, 161 insertions, 4 deletions
diff --git a/security/netpgpverify/Makefile b/security/netpgpverify/Makefile index db67c1d84c6..ff37956269f 100644 --- a/security/netpgpverify/Makefile +++ b/security/netpgpverify/Makefile @@ -1,6 +1,6 @@ -# $NetBSD: Makefile,v 1.17 2015/09/25 15:46:58 agc Exp $ +# $NetBSD: Makefile,v 1.18 2015/10/01 01:14:07 agc Exp $ -DISTNAME= netpgpverify-20150919 +DISTNAME= netpgpverify-20150930 CATEGORIES= security MASTER_SITES= # empty DISTFILES= # empty diff --git a/security/netpgpverify/files/bignum.c b/security/netpgpverify/files/bignum.c index 13e6de70724..c1301828b20 100644 --- a/security/netpgpverify/files/bignum.c +++ b/security/netpgpverify/files/bignum.c @@ -5121,6 +5121,94 @@ subtract_modulo(mp_int *a, mp_int *b, mp_int *c, mp_int *d) return res; } +/* bn_mp_gcd.c */ +/* Greatest Common Divisor using the binary method */ +static int +mp_gcd(mp_int *a, mp_int *b, mp_int *c) +{ + mp_int u, v; + int k, u_lsb, v_lsb, res; + + /* either zero than gcd is the largest */ + if (PGPV_BN_is_zero(a) == MP_YES) { + return absolute(b, c); + } + if (PGPV_BN_is_zero(b) == MP_YES) { + return absolute(a, c); + } + + /* get copies of a and b we can modify */ + if ((res = mp_init_copy(&u, a)) != MP_OKAY) { + return res; + } + + if ((res = mp_init_copy(&v, b)) != MP_OKAY) { + goto LBL_U; + } + + /* must be positive for the remainder of the algorithm */ + u.sign = v.sign = MP_ZPOS; + + /* B1. Find the common power of two for u and v */ + u_lsb = mp_cnt_lsb(&u); + v_lsb = mp_cnt_lsb(&v); + k = MIN(u_lsb, v_lsb); + + if (k > 0) { + /* divide the power of two out */ + if ((res = rshift_bits(&u, k, &u, NULL)) != MP_OKAY) { + goto LBL_V; + } + + if ((res = rshift_bits(&v, k, &v, NULL)) != MP_OKAY) { + goto LBL_V; + } + } + + /* divide any remaining factors of two out */ + if (u_lsb != k) { + if ((res = rshift_bits(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { + goto LBL_V; + } + } + + if (v_lsb != k) { + if ((res = rshift_bits(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { + goto LBL_V; + } + } + + while (PGPV_BN_is_zero(&v) == 0) { + /* make sure v is the largest */ + if (compare_magnitude(&u, &v) == MP_GT) { + /* swap u and v to make sure v is >= u */ + mp_exch(&u, &v); + } + + /* subtract smallest from largest */ + if ((res = signed_subtract(&v, &u, &v)) != MP_OKAY) { + goto LBL_V; + } + + /* Divide out all factors of two */ + if ((res = rshift_bits(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { + goto LBL_V; + } + } + + /* multiply by 2**k which we divided out at the beginning */ + if ((res = lshift_bits(&u, k, c)) != MP_OKAY) { + goto LBL_V; + } + c->sign = MP_ZPOS; + res = MP_OKAY; +LBL_V: + mp_clear (&u); +LBL_U: + mp_clear (&v); + return res; +} + /**************************************************************************/ /* PGPV_BIGNUM emulation layer */ @@ -5545,6 +5633,7 @@ PGPV_BN_rand(PGPV_BIGNUM *rnd, int bits, int top, int bottom) r <<= 32; r |= arc4random(); rnd->dp[i] = (r & MP_MASK); + rnd->used += 1; } if (top == 0) { rnd->dp[rnd->used - 1] |= (((mp_digit)1)<<((mp_digit)DIGIT_BIT)); @@ -5681,3 +5770,10 @@ PGPV_BN_factorial(PGPV_BIGNUM *res, PGPV_BIGNUM *f) PGPV_BN_free(i); return 1; } + +/* get greatest common divisor */ +int +PGPV_BN_gcd(PGPV_BIGNUM *r, PGPV_BIGNUM *a, PGPV_BIGNUM *b, PGPV_BN_CTX *ctx) +{ + return mp_gcd(a, b, r); +} diff --git a/security/netpgpverify/files/bn.h b/security/netpgpverify/files/bn.h index 5aff457e82f..b2369727910 100644 --- a/security/netpgpverify/files/bn.h +++ b/security/netpgpverify/files/bn.h @@ -44,6 +44,65 @@ __BEGIN_DECLS +#ifdef USE_BN_INTERFACE +#define BIGNUM PGPV_BIGNUM +#define BN_ULONG PGPV_BN_ULONG +#define BN_CTX PGPV_BN_CTX +#define BN_is_negative PGPV_BN_is_negative +#define BN_is_zero PGPV_BN_is_zero +#define BN_is_odd PGPV_BN_is_odd +#define BN_is_even PGPV_BN_is_even +#define BN_new PGPV_BN_new +#define BN_dup PGPV_BN_dup +#define BN_copy PGPV_BN_copy +#define BN_init PGPV_BN_init +#define BN_free PGPV_BN_free +#define BN_clear PGPV_BN_clear +#define BN_clear_free PGPV_BN_clear_free +#define BN_cmp PGPV_BN_cmp +#define BN_bn2bin PGPV_BN_bn2bin +#define BN_bn2hex PGPV_BN_bn2hex +#define BN_bn2dec PGPV_BN_bn2dec +#define BN_bn2radix PGPV_BN_bn2radix +#define BN_hex2bn PGPV_BN_hex2bn +#define BN_dec2bn PGPV_BN_dec2bn +#define BN_radix2bn PGPV_BN_radix2bn +#ifndef _KERNEL +#define BN_print_fp PGPV_BN_print_fp +#endif +#define BN_add PGPV_BN_add +#define BN_sub PGPV_BN_sub +#define BN_mul PGPV_BN_mul +#define BN_div PGPV_BN_div +#define BN_swap PGPV_BN_swap +#define BN_bitop PGPV_BN_bitop +#define BN_lshift PGPV_BN_lshift +#define BN_lshift1 PGPV_BN_lshift1 +#define BN_rshift PGPV_BN_rshift +#define BN_rshift1 PGPV_BN_rshift1 +#define BN_set_word PGPV_BN_set_word +#define BN_set_negative PGPV_BN_set_negative +#define BN_num_bytes PGPV_BN_num_bytes +#define BN_num_bits PGPV_BN_num_bits +#define BN_mod_exp PGPV_BN_mod_exp +#define BN_mod_inverse PGPV_BN_mod_inverse +#define BN_mod_mul PGPV_BN_mod_mul +#define BN_raise PGPV_BN_raise +#define BN_factorial PGPV_BN_factorial +#define BN_CTX_new PGPV_BN_CTX_new +#define BN_CTX_get PGPV_BN_CTX_get +#define BN_CTX_start PGPV_BN_CTX_start +#define BN_CTX_end PGPV_BN_CTX_end +#define BN_CTX_init PGPV_BN_CTX_init +#define BN_CTX_free PGPV_BN_CTX_free +#define BN_rand PGPV_BN_rand +#define BN_rand_range PGPV_BN_rand_range +#define BN_is_prime PGPV_BN_is_prime +#define BN_value_one PGPV_BN_value_one +#define BN_is_bit_set PGPV_BN_is_bit_set +#define BN_gcd PGPV_BN_gcd +#endif /* USE_BN_INTERFACE */ + /* should be 32bit on ILP32, 64bit on LP64 */ typedef unsigned long mp_digit; typedef uint64_t mp_word; @@ -147,6 +206,8 @@ int PGPV_BN_is_prime(const PGPV_BIGNUM */*a*/, int /*checks*/, void (*callback)( const PGPV_BIGNUM *PGPV_BN_value_one(void); int PGPV_BN_is_bit_set(const PGPV_BIGNUM */*a*/, int /*n*/); +int PGPV_BN_gcd(PGPV_BIGNUM */*r*/, PGPV_BIGNUM */*a*/, PGPV_BIGNUM */*b*/, PGPV_BN_CTX */*ctx*/); + __END_DECLS #endif diff --git a/security/netpgpverify/files/verify.h b/security/netpgpverify/files/verify.h index a8ce82c6708..8c2b4dad604 100644 --- a/security/netpgpverify/files/verify.h +++ b/security/netpgpverify/files/verify.h @@ -23,9 +23,9 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef NETPGP_VERIFY_H_ -#define NETPGP_VERIFY_H_ 20150919 +#define NETPGP_VERIFY_H_ 20150930 -#define NETPGPVERIFY_VERSION "netpgpverify portable 20150919" +#define NETPGPVERIFY_VERSION "netpgpverify portable 20150930" #include <sys/types.h> |