1.Dd $Mdocdate: February 23 2015 $ 2.Dt BN_ADD 3 3.Os 4.Sh NAME 5.Nm BN_add , 6.Nm BN_sub , 7.Nm BN_mul , 8.Nm BN_sqr , 9.Nm BN_div , 10.Nm BN_mod , 11.Nm BN_nnmod , 12.Nm BN_mod_add , 13.Nm BN_mod_sub , 14.Nm BN_mod_mul , 15.Nm BN_mod_sqr , 16.Nm BN_exp , 17.Nm BN_mod_exp , 18.Nm BN_gcd 19.Nd arithmetic operations on BIGNUMs 20.Sh SYNOPSIS 21.In openssl/bn.h 22.Ft int 23.Fo BN_add 24.Fa "BIGNUM *r" 25.Fa "const BIGNUM *a" 26.Fa "const BIGNUM *b" 27.Fc 28.Ft int 29.Fo BN_sub 30.Fa "BIGNUM *r" 31.Fa "const BIGNUM *a" 32.Fa "const BIGNUM *b" 33.Fc 34.Ft int 35.Fo BN_mul 36.Fa "BIGNUM *r" 37.Fa "BIGNUM *a" 38.Fa "BIGNUM *b" 39.Fa "BN_CTX *ctx" 40.Fc 41.Ft int 42.Fo BN_sqr 43.Fa "BIGNUM *r" 44.Fa "BIGNUM *a" 45.Fa "BN_CTX *ctx" 46.Fc 47.Ft int 48.Fo BN_div 49.Fa "BIGNUM *dv" 50.Fa "BIGNUM *rem" 51.Fa "const BIGNUM *a" 52.Fa "const BIGNUM *d" 53.Fa "BN_CTX *ctx" 54.Fc 55.Ft int 56.Fo BN_mod 57.Fa "BIGNUM *rem" 58.Fa "const BIGNUM *a" 59.Fa "const BIGNUM *m" 60.Fa "BN_CTX *ctx" 61.Fc 62.Ft int 63.Fo BN_nnmod 64.Fa "BIGNUM *r" 65.Fa "const BIGNUM *a" 66.Fa "const BIGNUM *m" 67.Fa "BN_CTX *ctx" 68.Fc 69.Ft int 70.Fo BN_mod_add 71.Fa "BIGNUM *r" 72.Fa "BIGNUM *a" 73.Fa "BIGNUM *b" 74.Fa "const BIGNUM *m" 75.Fa "BN_CTX *ctx" 76.Fc 77.Ft int 78.Fo BN_mod_sub 79.Fa "BIGNUM *r" 80.Fa "BIGNUM *a" 81.Fa "BIGNUM *b" 82.Fa "const BIGNUM *m" 83.Fa "BN_CTX *ctx" 84.Fc 85.Ft int 86.Fo BN_mod_mul 87.Fa "BIGNUM *r" 88.Fa "BIGNUM *a" 89.Fa "BIGNUM *b" 90.Fa "const BIGNUM *m" 91.Fa "BN_CTX *ctx" 92.Fc 93.Ft int 94.Fo BN_mod_sqr 95.Fa "BIGNUM *r" 96.Fa "BIGNUM *a" 97.Fa "const BIGNUM *m" 98.Fa "BN_CTX *ctx" 99.Fc 100.Ft int 101.Fo BN_exp 102.Fa "BIGNUM *r" 103.Fa "BIGNUM *a" 104.Fa "BIGNUM *p" 105.Fa "BN_CTX *ctx" 106.Fc 107.Ft int 108.Fo BN_mod_exp 109.Fa "BIGNUM *r" 110.Fa "BIGNUM *a" 111.Fa "const BIGNUM *p" 112.Fa "const BIGNUM *m" 113.Fa "BN_CTX *ctx" 114.Fc 115.Ft int 116.Fo BN_gcd 117.Fa "BIGNUM *r" 118.Fa "BIGNUM *a" 119.Fa "BIGNUM *b" 120.Fa "BN_CTX *ctx" 121.Fc 122.Sh DESCRIPTION 123.Fn BN_add 124adds 125.Fa a 126and 127.Fa b 128and places the result in 129.Fa r 130.Pq Li r=a+b . 131.Fa r 132may be the same 133.Vt BIGNUM 134as 135.Fa a 136or 137.Fa b . 138.Pp 139.Fn BN_sub 140subtracts 141.Fa b 142from 143.Fa a 144and places the result in 145.Fa r 146.Pq Li r=a-b . 147.Pp 148.Fn BN_mul 149multiplies 150.Fa a 151and 152.Fa b 153and places the result in 154.Fa r 155.Pq Li r=a*b . 156.Fa r 157may be the same 158.Vt BIGNUM 159as 160.Fa a 161or 162.Fa b . 163For multiplication by powers of 2, use 164.Xr BN_lshift 3 . 165.Pp 166.Fn BN_sqr 167takes the square of 168.Fa a 169and places the result in 170.Fa r 171.Pq Li r=a^2 . 172.Fa r 173and 174.Fa a 175may be the same 176.Vt BIGNUM . 177This function is faster than 178.Fn BN_mul r a a . 179.Pp 180.Fn BN_div 181divides 182.Fa a 183by 184.Fa d 185and places the result in 186.Fa dv 187and the remainder in 188.Fa rem 189.Pq Li dv=a/d , rem=a%d . 190Either of 191.Fa dv 192and 193.Fa rem 194may be 195.Dv NULL , 196in which case the respective value is not returned. 197The result is rounded towards zero; thus if 198.Fa a 199is negative, the remainder will be zero or negative. 200For division by powers of 2, use 201.Fn BN_rshift 3 . 202.Pp 203.Fn BN_mod 204corresponds to 205.Fn BN_div 206with 207.Fa dv 208set to 209.Dv NULL . 210.Pp 211.Fn BN_nnmod 212reduces 213.Fa a 214modulo 215.Fa m 216and places the non-negative remainder in 217.Fa r . 218.Pp 219.Fn BN_mod_add 220adds 221.Fa a 222to 223.Fa b 224modulo 225.Fa m 226and places the non-negative result in 227.Fa r . 228.Pp 229.Fn BN_mod_sub 230subtracts 231.Fa b 232from 233.Fa a 234modulo 235.Fa m 236and places the non-negative result in 237.Fa r . 238.Pp 239.Fn BN_mod_mul 240multiplies 241.Fa a 242by 243.Fa b 244and finds the non-negative remainder respective to modulus 245.Fa m 246.Pq Li r=(a*b)%m . 247.Fa r 248may be the same 249.Vt BIGNUM 250as 251.Fa a 252or 253.Fa b . 254For more efficient algorithms for repeated computations using the same 255modulus, see 256.Xr BN_mod_mul_montgomery 3 257and 258.Xr BN_mod_mul_reciprocal 3 . 259.Pp 260.Fn BN_mod_sqr 261takes the square of 262.Fa a 263modulo 264.Fa m 265and places the result in 266.Fa r . 267.Pp 268.Fn BN_exp 269raises 270.Fa a 271to the 272.Fa p Ns -th 273power and places the result in 274.Fa r 275.Pq Li r=a^p . 276This function is faster than repeated applications of 277.Fn BN_mul . 278.Pp 279.Fn BN_mod_exp 280computes 281.Fa a 282to the 283.Fa p Ns -th 284power modulo 285.Fa m 286.Pq Li r=(a^p)%m . 287This function uses less time and space than 288.Fn BN_exp . 289.Pp 290.Fn BN_gcd 291computes the greatest common divisor of 292.Fa a 293and 294.Fa b 295and places the result in 296.Fa r . 297.Fa r 298may be the same 299.Vt BIGNUM 300as 301.Fa a 302or 303.Fa b . 304.Pp 305For all functions, 306.Fa ctx 307is a previously allocated 308.Vt BN_CTX 309used for temporary variables; see 310.Xr BN_CTX_new 3 . 311.Pp 312Unless noted otherwise, the result 313.Vt BIGNUM 314must be different from the arguments. 315.Sh RETURN VALUES 316For all functions, 1 is returned for success, 0 on error. 317The return value should always be checked, for example: 318.Pp 319.Dl if (!BN_add(r,a,b)) goto err; 320.Pp 321The error codes can be obtained by 322.Xr ERR_get_error 3 . 323.Sh SEE ALSO 324.Xr bn 3 , 325.Xr BN_add_word 3 , 326.Xr BN_CTX_new 3 , 327.Xr BN_set_bit 3 , 328.Xr ERR_get_error 3 329.Sh HISTORY 330.Fn BN_add , 331.Fn BN_sub , 332.Fn BN_sqr , 333.Fn BN_div , 334.Fn BN_mod , 335.Fn BN_mod_mul , 336.Fn BN_mod_exp , 337and 338.Fn BN_gcd 339are available in all versions of SSLeay and OpenSSL. 340The 341.Fa ctx 342argument to 343.Fn BN_mul 344was added in SSLeay 0.9.1b. 345.Fn BN_exp 346appeared in SSLeay 0.9.0. 347.Fn BN_nnmod , 348.Fn BN_mod_add , 349.Fn BN_mod_sub , 350and 351.Fn BN_mod_sqr 352were added in OpenSSL 0.9.7. 353