public static void SetWord(BigNumberHandle x, ulong w) { Debug.Assert(!x.IsInvalid, "Accessed an invalid BigNumberHandle"); if (BN_set_word(x, w) == 0) { throw new OpenSslNativeException(); } }
public static void SecureRandom(BigNumberHandle rnd, BigNumberHandle range) { Debug.Assert(!rnd.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(rnd)}>"); Debug.Assert(!range.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(range)}>"); if (BN_priv_rand_range(rnd, range) == 0) { throw new OpenSslNativeException(); } }
public static void SetPrivateKey(ECKeyHandle key, BigNumberHandle privateKey) { Debug.Assert(!key.IsInvalid, $"Accessed an invalid ECKeyHandle! <{nameof(key)}>"); Debug.Assert(!key.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(privateKey)}>"); if (EC_KEY_set_private_key(key, privateKey) == 0) { throw new OpenSslNativeException(); } }
public static void ToBytes(BigNumberHandle x, byte[] buffer) { Debug.Assert(!x.IsInvalid, "Accessed an invalid BigNumberHandle"); int result = BN_bn2lebinpad(x, buffer, buffer.Length); if (result == -1) { throw new OpenSslNativeException(); } }
public static void GetCofactor(ECGroupHandle group, BigNumberHandle cofactor, BigNumberContextHandle ctx) { Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>"); Debug.Assert(!cofactor.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(cofactor)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); if (EC_GROUP_get_cofactor(group, cofactor, ctx) == 0) { throw new OpenSslNativeException(); } }
public static void Multiply(ECGroupHandle group, ECPointHandle result, BigNumberHandle n, ECPointHandle q, BigNumberHandle m, BigNumberContextHandle ctx) { Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>"); Debug.Assert(!result.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(result)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); if (EC_POINT_mul(group, result, n, q, m, ctx) == 0) { throw new OpenSslNativeException(); } }
/// <summary> /// Allocates a new OpenSSL <c>BIGNUM</c> structure /// and returns a handle to it. /// /// The returned handle is guaranteed to point to be valid, /// i.e., point to a valid <c>BIGNUM</c> structure. /// </summary> /// <returns> /// A valid <see cref="BigNumberHandle" /> pointing to a /// freshly allocated <c>BIGNUM</c> structure. /// </returns> public static BigNumberHandle Create() { var handle = new BigNumberHandle(BN_new(), ownsHandle: true); if (handle.IsInvalid) { throw new OpenSslNativeException(); } return(handle); }
public static BigNumberHandle FromBytes(byte[] buffer, BigNumberHandle target) { var result = BN_lebin2bn(buffer, buffer.Length, target); if (result.IsInvalid) { throw new OpenSslNativeException(); } return(result); }
/// <summary> /// Allocates a new OpenSSL <c>BIGNUM</c> structure /// in OpenSSL secure heap and returns a handle to it. /// /// The returned handle is guaranteed to point to be valid, /// i.e., point to a valid <c>BIGNUM</c> structure with /// <c>BN_FLG_SECURE</c> and <c>BN_FLG_CONSTTIME</c> flags set. /// </summary> /// <returns> /// A valid <see cref="BigNumberHandle" /> pointing to a /// freshly allocated <c>BIGNUM</c> structure. /// </returns> public static BigNumberHandle CreateSecure() { var handle = new BigNumberHandle(BN_secure_new(), ownsHandle: true); if (handle.IsInvalid) { throw new OpenSslNativeException(); } BN_set_flags(handle, BigNumberFlags.ConstantTime); return(handle); }
public static BigNumberHandle Copy(BigNumberHandle from, BigNumberHandle to) { Debug.Assert(!from.IsInvalid, $"Accessed an invalid BigNumberHandle"); BigNumberHandle result = BN_copy(from, to); if (result.IsInvalid) { throw new OpenSslNativeException(); } return(result); }
public static void SecureModExp(BigNumberHandle r, BigNumberHandle a, BigNumberHandle e, BigNumberHandle m, BigNumberContextHandle ctx) { Debug.Assert(!r.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(r)}>"); Debug.Assert(!a.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(a)}>"); Debug.Assert(!e.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(e)}>"); Debug.Assert(!m.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(m)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); if (BN_mod_exp_mont_consttime(r, a, e, m, ctx, BigNumberMontgomeryContextHandle.Null) == 0) { throw new OpenSslNativeException(); } }
public static void ModMul(BigNumberHandle r, BigNumberHandle a, BigNumberHandle b, BigNumberHandle m, BigNumberContextHandle ctx) { Debug.Assert(!r.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(r)}>"); Debug.Assert(!a.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(a)}>"); Debug.Assert(!b.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(b)}>"); Debug.Assert(!m.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(m)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); if (BN_mod_mul(r, a, b, m, ctx) == 0) { throw new OpenSslNativeException(); } }
public static void ModInverse(BigNumberHandle r, BigNumberHandle a, BigNumberHandle m, BigNumberContextHandle ctx) { Debug.Assert(!r.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(r)}>"); Debug.Assert(!a.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(a)}>"); Debug.Assert(!m.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(m)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); var result = BN_mod_inverse(r, a, m, ctx); if (result.IsInvalid) { throw new OpenSslNativeException(); } }
private extern static int EC_POINT_get_affine_coordinates(ECGroupHandle group, ECPointHandle p, BigNumberHandle x, BigNumberHandle y, BigNumberContextHandle ctx);
private extern static BigNumberHandle BN_copy(BigNumberHandle to, BigNumberHandle from);
private extern static void BN_set_flags(BigNumberHandle a, BigNumberFlags flags);
private extern static int EC_GROUP_get_cofactor(ECGroupHandle group, BigNumberHandle cofactor, BigNumberContextHandle ctx);
private extern static int EC_GROUP_get_order(ECGroupHandle group, BigNumberHandle order, BigNumberContextHandle ctx);
private extern static BigNumberFlags BN_get_flags(BigNumberHandle a, BigNumberFlags flags);
public static int GetNumberOfBits(BigNumberHandle x) { Debug.Assert(!x.IsInvalid, "Accessed an invalid BigNumberHandle"); return(BN_num_bits(x)); }
private extern static int EC_KEY_set_private_key(ECKeyHandle key, BigNumberHandle prv);
private extern static int EC_POINT_mul(ECGroupHandle group, ECPointHandle r, BigNumberHandle n, ECPointHandle q, BigNumberHandle m, BigNumberContextHandle ctx);
private extern static int BN_mod_exp(BigNumberHandle r, BigNumberHandle a, BigNumberHandle p, BigNumberHandle m, BigNumberContextHandle ctx);
private extern static BigNumberHandle BN_lebin2bn(byte[] buffer, int len, BigNumberHandle ret);
private extern static int BN_bn2lebinpad(BigNumberHandle a, byte[] buffer, int bufferLen);
private extern static int BN_ucmp(BigNumberHandle a, BigNumberHandle b);
public static void GetAffineCoordinates(ECGroupHandle group, ECPointHandle p, BigNumberHandle x, BigNumberHandle y, BigNumberContextHandle ctx) { Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>"); Debug.Assert(!p.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(p)}>"); Debug.Assert(!x.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(x)}>"); Debug.Assert(!y.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(y)}>"); Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>"); if (EC_POINT_get_affine_coordinates(group, p, x, y, ctx) == 0) { throw new OpenSslNativeException(); } }
private extern static int BN_mod_exp_mont_consttime(BigNumberHandle result, BigNumberHandle a, BigNumberHandle exponent, BigNumberHandle modulo, BigNumberContextHandle ctx, BigNumberMontgomeryContextHandle montCtx);
private extern static int BN_priv_rand_range(BigNumberHandle rnd, BigNumberHandle range);
private extern static BigNumberHandle BN_mod_inverse(BigNumberHandle r, BigNumberHandle a, BigNumberHandle m, BigNumberContextHandle ctx);