Exemple #1
0
        public static byte[] encrypt(byte[] msg, PublicKey pub, EncParams param, RandContext rand_ctx)
        {
            byte[] enc     = new byte[param.enc_len()];
            IntPtr enc_ptr = Marshal.AllocHGlobal(param.enc_len());
            IntPtr msg_ptr = Marshal.AllocHGlobal(msg.Length);

            Marshal.Copy(msg, 0, msg_ptr, msg.Length);
            IntPtr pub_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(pub));

            Marshal.StructureToPtr(pub, pub_ptr, false);
            IntPtr param_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(param));

            Marshal.StructureToPtr(param, param_ptr, false);
            IntPtr rand_ctx_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_ctx.rand_ctx));

            Marshal.StructureToPtr(rand_ctx.rand_ctx, rand_ctx_ptr, false);
            IntPtr msg_len_ptr = new IntPtr(msg.Length);
            var    result      = ffi.ntru_encrypt(msg_ptr, msg_len_ptr, pub_ptr, param_ptr, rand_ctx_ptr, enc_ptr);

            if (result != 0)
            {
                Console.WriteLine("Error: Failed to Encrypt Message");
            }
            Console.WriteLine("Went through FFI Encrypt Function");
            Marshal.Copy(enc_ptr, enc, 0, enc.Length);
            Marshal.FreeHGlobal(msg_ptr);
            Marshal.FreeHGlobal(pub_ptr);
            Marshal.FreeHGlobal(param_ptr);
            Marshal.FreeHGlobal(rand_ctx_ptr);
            Marshal.FreeHGlobal(enc_ptr);
            return(enc);
        }
Exemple #2
0
        public byte[] to_arr_sse_2048(EncParams param)
        {
            IntPtr this_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this));

            Marshal.StructureToPtr(this, this_ptr, false);
            byte[] a      = new byte[param.enc_len()];
            IntPtr a_byte = Marshal.AllocHGlobal(param.enc_len());

            ffi.ntru_to_arr_sse_2048(this_ptr, a_byte);
            Marshal.Copy(a_byte, a, 0, param.enc_len());
            Marshal.FreeHGlobal(this_ptr);
            Marshal.FreeHGlobal(a_byte);
            return(a);
        }