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); }
public static IntPoly rand(ushort n, ushort pow2q, RandContext rand_ctx) { byte[] rand_data = rand_ctx.get_rng().generate((ushort)(n * 2), rand_ctx); short[] coeffs = new short[types.INT_POLY_SIZE]; ushort shift = (ushort)(16 - pow2q); for (int i = 0; i < rand_data.Length; i++) { coeffs[i] = (short)(rand_data[i] >> shift); } return(new IntPoly(n, coeffs)); }
public static TernPoly rand(ushort n, ushort num_ones, ushort num_neg_ones, RandContext rand_ctx) { TernPoly poly = TernPoly.Default(); IntPtr poly_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(poly)); Marshal.StructureToPtr(poly, poly_ptr, false); IntPtr rand_ctx_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(rand_ctx.rand_ctx)); Marshal.StructureToPtr(rand_ctx.rand_ctx, rand_ctx_ptr, false); var result = ffi.ntru_rand_tern(n, num_ones, num_neg_ones, poly_ptr, rand_ctx_ptr); if (result == 0) { Console.WriteLine("Error: Failed to Generate Random TernPoly"); } poly = (TernPoly)Marshal.PtrToStructure(poly_ptr, typeof(TernPoly)); Marshal.FreeHGlobal(poly_ptr); Marshal.FreeHGlobal(rand_ctx_ptr); return(poly); }
public static KeyPair generate_key_pair(EncParams param, RandContext rand_context) { KeyPair kp = KeyPair.Default(); IntPtr key_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(kp)); Marshal.StructureToPtr(kp, key_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_context.rand_ctx)); Marshal.StructureToPtr(rand_context.rand_ctx, rand_ctx_ptr, false); var result = ffi.ntru_gen_key_pair(param_ptr, key_ptr, rand_ctx_ptr); if (result.ToInt32() != 0) { Console.WriteLine("Error: Failed to Generate KeyPair"); } kp = (KeyPair)Marshal.PtrToStructure(key_ptr, typeof(KeyPair)); Marshal.FreeHGlobal(key_ptr); Marshal.FreeHGlobal(param_ptr); Marshal.FreeHGlobal(rand_ctx_ptr); return(kp); }
public static ProdPoly rand(ushort n, ushort df1, ushort df2, ushort df3_ones, ushort df3_neg_ones, RandContext rand_ctx) { TernPoly f1 = TernPoly.rand(n, df1, df1, rand_ctx); TernPoly f2 = TernPoly.rand(n, df2, df2, rand_ctx); TernPoly f3 = TernPoly.rand(n, df3_ones, df3_neg_ones, rand_ctx); return(new ProdPoly(n, f1, f2, f3)); }
public static void Main(string[] args) { RandContext rand_ctx = rand.rand.init(rand.rand.RNG_DEFAULT); Console.WriteLine("Successfully Created RandContext!"); KeyPair kp = NTRUWrapper.generate_key_pair(EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx); Console.WriteLine("Successfully Generated Keys!!"); byte[] msg = Encoding.UTF8.GetBytes("Hello from NTRU!"); byte[] encrypted = NTRUWrapper.encrypt(msg, kp.get_public(), EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx); byte[] decrypted = NTRUWrapper.decrypt(encrypted, kp, EncParamSets.DEFAULT_PARAMS_256_BITS); Console.WriteLine(Encoding.UTF8.GetString(decrypted)); if (Encoding.UTF8.GetString(decrypted) == "Hello from NTRU!") { Console.WriteLine("Ecryption / Decryption Test Succeded!"); } else { Console.WriteLine("Ecryption / Decryption Test Failed!"); } byte[] exportedPriv = kp.get_private().export(EncParamSets.DEFAULT_PARAMS_256_BITS); Console.WriteLine("ENC PARAMS Private Key Length: " + EncParamSets.DEFAULT_PARAMS_256_BITS.private_len() + " Byte Array: " + exportedPriv.Length); for (int i = 0; i < exportedPriv.Length; i++) { Console.Write(exportedPriv[i]); } Console.Write("\n"); byte[] exportedPub = kp.get_public().export(EncParamSets.DEFAULT_PARAMS_256_BITS); Console.WriteLine("ENC PARAMS PublicKey Key Length: " + EncParamSets.DEFAULT_PARAMS_256_BITS.public_len() + " Byte Array: " + exportedPub.Length); for (int i = 0; i < exportedPub.Length; i++) { Console.Write(exportedPub[i]); } Console.Write("\n"); PublicKey pub = PublicKey.import(exportedPub); PrivateKey priv = PrivateKey.import(exportedPriv); KeyPair newKP = new KeyPair(priv, pub); if (newKP == kp) { Console.WriteLine("Importing / Exporting Key Test Succeded!"); } else { Console.WriteLine("Importing / Exporting Key Test Failed!"); } byte[] msg2 = Encoding.UTF8.GetBytes("Hello from NTRU! 2"); byte[] encrypted2 = NTRUWrapper.encrypt(msg2, newKP.get_public(), EncParamSets.DEFAULT_PARAMS_256_BITS, rand_ctx); byte[] decrypted2 = NTRUWrapper.decrypt(encrypted2, newKP, EncParamSets.DEFAULT_PARAMS_256_BITS); Console.WriteLine(Encoding.UTF8.GetString(decrypted2)); if (Encoding.UTF8.GetString(decrypted2) == "Hello from NTRU! 2") { Console.WriteLine("Ecryption / Decryption Test 2 Succeded!"); } else { Console.WriteLine("Ecryption / Decryption Test 2 Failed!"); } }