Esempio n. 1
0
        public static RandContext init_det(RandGen rand_gen, byte[] seed)
        {
            RandContext rand_ctx     = RandContext.Default();
            IntPtr      rand_ctx_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_ctx.rand_ctx));

            Marshal.StructureToPtr(rand_ctx.rand_ctx, rand_ctx_ptr, false);
            IntPtr rand_gen_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_gen));

            Marshal.StructureToPtr(rand_gen, rand_gen_ptr, false);
            IntPtr seed_ptr = Marshal.AllocHGlobal(seed.Length);

            Marshal.Copy(seed, 0, seed_ptr, seed.Length);
            IntPtr seed_len_ptr = new IntPtr(seed.Length);
            var    result       = ffi.ntru_rand_init_det(rand_ctx_ptr, rand_gen_ptr, seed_ptr, seed_len_ptr);

            if (result.ToInt32() != 0)
            {
                Console.WriteLine("Error: Failed to Initialize Deterministic RandomContext");
            }
            rand_ctx.rand_ctx = (ffi.CNtruRandContext)Marshal.PtrToStructure(rand_ctx_ptr, typeof(ffi.CNtruRandContext));
            //Marshal.FreeHGlobal(rand_ctx_ptr);
            //Marshal.FreeHGlobal(rand_gen_ptr);
            Marshal.FreeHGlobal(seed_ptr);
            return(rand_ctx);
        }
Esempio n. 2
0
        public static RandContext init(RandGen rand_gen)
        {
            RandContext rand_ctx     = RandContext.Default();
            IntPtr      rand_ctx_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_ctx.rand_ctx));

            Marshal.StructureToPtr(rand_ctx.rand_ctx, rand_ctx_ptr, false);
            IntPtr rand_gen_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_gen));

            Marshal.StructureToPtr(rand_gen, rand_gen_ptr, false);
            var result = ffi.ntru_rand_init(rand_ctx_ptr, rand_gen_ptr);

            if (result.ToInt32() != 0)
            {
                Console.WriteLine("Error: Failed to Initialize RandomContext");
            }
            rand_ctx.rand_ctx = (ffi.CNtruRandContext)Marshal.PtrToStructure(rand_ctx_ptr, typeof(ffi.CNtruRandContext));
            return(rand_ctx);
        }
Esempio n. 3
0
        public static byte[] generate(ushort length, RandContext rand_ctx)
        {
            byte[] plain        = new byte[length];
            IntPtr plain_ptr    = Marshal.AllocHGlobal(length);
            IntPtr rand_ctx_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_ctx.rand_ctx));

            Marshal.StructureToPtr(rand_ctx.rand_ctx, rand_ctx_ptr, false);
            IntPtr length_ptr = new IntPtr(length);
            var    result     = ffi.ntru_rand_generate(plain_ptr, length_ptr, rand_ctx_ptr);

            if (result.ToInt32() != 0)
            {
                Console.WriteLine("Error: Failed to Generate Random Data");
            }
            Marshal.Copy(plain_ptr, plain, 0, length_ptr.ToInt32());
            Marshal.FreeHGlobal(plain_ptr);
            Marshal.FreeHGlobal(rand_ctx_ptr);
            return(plain);
        }