Exemple #1
0
 public static void PrecomputeGeneratorMultiples(ECGroupHandle group, BigNumberContextHandle ctx)
 {
     Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
     Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
     if (EC_GROUP_precompute_mult(group, ctx) == 0)
     {
         throw new OpenSslNativeException();
     }
 }
 public static void InvertInPlace(ECGroupHandle group, ECPointHandle p, BigNumberContextHandle ctx)
 {
     Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
     Debug.Assert(!p.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(p)}>");
     Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
     if (EC_POINT_invert(group, p, ctx) == 0)
     {
         throw new OpenSslNativeException();
     }
 }
Exemple #3
0
        /// <summary>
        /// Allocates a new OpenSSL <c>BN_CTX</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>BN_CTX</c> structure.
        /// </summary>
        /// <returns>
        /// A valid <see cref="BigNumberContextHandle" /> pointing to a
        /// freshly allocated <c>BN_CTX</c> structure.
        /// </returns>
        public static BigNumberContextHandle CreateSecure()
        {
            var ctx = new BigNumberContextHandle(BN_CTX_secure_new(), ownsHandle: true);

            if (ctx.IsInvalid)
            {
                throw new OpenSslNativeException();
            }
            return(ctx);
        }
Exemple #4
0
 public static void GetOrder(ECGroupHandle group, BigNumberHandle order, BigNumberContextHandle ctx)
 {
     Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
     Debug.Assert(!order.IsInvalid, $"Accessed an invalid BigNumberHandle! <{nameof(order)}>");
     Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
     if (EC_GROUP_get_order(group, order, ctx) == 0)
     {
         throw new OpenSslNativeException();
     }
 }
        public static bool IsOnCurve(ECGroupHandle group, ECPointHandle p, BigNumberContextHandle ctx)
        {
            Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
            Debug.Assert(!p.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(p)}>");
            Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
            var result = EC_POINT_is_on_curve(group, p, ctx);

            if (result < 0)
            {
                throw new OpenSslNativeException();
            }
            return(result == 1);
        }
Exemple #6
0
        public static bool Compare(ECGroupHandle a, ECGroupHandle b, BigNumberContextHandle ctx)
        {
            Debug.Assert(!a.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(a)}>");
            Debug.Assert(!b.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(b)}>");
            Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
            var result = EC_GROUP_cmp(a, b, ctx);

            if (result == -1)
            {
                throw new OpenSslNativeException();
            }
            else if (result == 0)
            {
                return(true);
            }
            return(false);
        }
        public static void FromByteBuffer(ECGroupHandle group, ECPointHandle p, byte[] buffer, BigNumberContextHandle ctx)
        {
            Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
            Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
            var result = EC_POINT_oct2point(group, p, buffer, buffer.Length, ctx);

            if (result == 0)
            {
                throw new OpenSslNativeException();
            }
        }
Exemple #8
0
 private extern static int BN_mod_exp_mont_consttime(BigNumberHandle result, BigNumberHandle a, BigNumberHandle exponent,
                                                     BigNumberHandle modulo, BigNumberContextHandle ctx, BigNumberMontgomeryContextHandle montCtx);
 public static void Add(ECGroupHandle group, ECPointHandle result, ECPointHandle a, ECPointHandle b, BigNumberContextHandle ctx)
 {
     Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
     Debug.Assert(!result.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(result)}>");
     Debug.Assert(!a.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(a)}>");
     Debug.Assert(!b.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(b)}>");
     Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
     if (EC_POINT_add(group, result, a, b, ctx) == 0)
     {
         throw new OpenSslNativeException();
     }
 }
 private extern static int EC_POINT_oct2point(ECGroupHandle group, ECPointHandle p, byte[] buffer, int bufferLen, BigNumberContextHandle ctx);
 private extern static int EC_POINT_invert(ECGroupHandle group, ECPointHandle a, BigNumberContextHandle ctx);
 private extern static int EC_POINT_mul(ECGroupHandle group, ECPointHandle r, BigNumberHandle n, ECPointHandle q, BigNumberHandle m, BigNumberContextHandle ctx);
Exemple #13
0
 private extern static int EC_GROUP_cmp(ECGroupHandle a, ECGroupHandle b, BigNumberContextHandle ctx);
Exemple #14
0
 private extern static int EC_GROUP_precompute_mult(ECGroupHandle group, BigNumberContextHandle ctx);
Exemple #15
0
 private extern static int EC_GROUP_get_cofactor(ECGroupHandle group, BigNumberHandle cofactor, BigNumberContextHandle ctx);
Exemple #16
0
 private extern static int EC_GROUP_get_order(ECGroupHandle group, BigNumberHandle order, BigNumberContextHandle ctx);
Exemple #17
0
        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();
            }
        }
Exemple #18
0
 private extern static BigNumberHandle BN_mod_inverse(BigNumberHandle r, BigNumberHandle a, BigNumberHandle m, BigNumberContextHandle ctx);
 private extern static int EC_POINT_get_affine_coordinates(ECGroupHandle group, ECPointHandle p, BigNumberHandle x, BigNumberHandle y, BigNumberContextHandle ctx);
 private extern static int EC_POINT_add(ECGroupHandle group, ECPointHandle r, ECPointHandle a, ECPointHandle b, BigNumberContextHandle ctx);
 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();
     }
 }
 private extern static int EC_POINT_is_on_curve(ECGroupHandle group, ECPointHandle point, BigNumberContextHandle ctx);
Exemple #23
0
 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();
     }
 }
 private extern static int EC_POINT_point2oct(ECGroupHandle group, ECPointHandle p, PointEncoding form, byte[]?buffer, int bufferLen, BigNumberContextHandle contextHandle);
        public static bool Compare(ECGroupHandle group, ECPointHandle a, ECPointHandle b, BigNumberContextHandle ctx)
        {
            Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
            Debug.Assert(!a.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(a)}>");
            Debug.Assert(!b.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(b)}>");
            Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
            var result = EC_POINT_cmp(group, a, b, ctx);

            if (result < 0)
            {
                throw new OpenSslNativeException();
            }
            return(result == 0);
        }
 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();
     }
 }
        public static int ToByteBuffer(ECGroupHandle group, ECPointHandle p, PointEncoding form, byte[]?buffer, BigNumberContextHandle ctx)
        {
            Debug.Assert(!group.IsInvalid, $"Accessed an invalid ECGroupHandle! <{nameof(group)}>");
            Debug.Assert(!p.IsInvalid, $"Accessed an invalid ECPointHandle! <{nameof(p)}>");
            Debug.Assert(!ctx.IsInvalid, $"Accessed an invalid BigNumberContextHandle! <{nameof(ctx)}>");
            int bufferLen = 0;

            if (buffer != null)
            {
                bufferLen = buffer.Length;
            }
            bufferLen = EC_POINT_point2oct(group, p, form, buffer, bufferLen, ctx);
            if (bufferLen == 0)
            {
                throw new OpenSslNativeException();
            }
            return(bufferLen);
        }
Exemple #28
0
 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();
     }
 }
Exemple #29
0
 private extern static int BN_mod_exp(BigNumberHandle r, BigNumberHandle a, BigNumberHandle p, BigNumberHandle m, BigNumberContextHandle ctx);