예제 #1
0
    void test_crypto_sign_open()
    {
        Console.WriteLine("Testing crypto_sign_open() .....");

        byte[] message = LibSalt.StringToByteArray("test");
        ulong  mlen    = 4;

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];
        LibSalt.crypto_sign_keypair(pk, sk);
        LibSalt.crypto_sign_keypair(pk2, sk2);

        ulong smlen = (ulong)LibSalt.crypto_sign_BYTES() + mlen;

        byte[] signed_message = new byte[smlen];
        LibSalt.crypto_sign(signed_message, message, sk);

        byte[] unsigned_message = new byte[mlen];
        int    success          = LibSalt.crypto_sign_open(unsigned_message, signed_message, pk);
        int    success2         = LibSalt.crypto_sign_open(unsigned_message, signed_message, pk2);

        // Test for sucessful decoding signed message with correct private key
        UnitTest.ASSERT_EQUALS(success, 0);

        // Test for unsucessful decoding signed message with incorrect private key
        UnitTest.ASSERT_DIFFERS(success2, 0);
    }
예제 #2
0
    public void test_crypto_sign()
    {
        Console.WriteLine("Testing crypto_sign() .....");

        byte[] message = LibSalt.StringToByteArray("test");
        ulong  mlen    = 4;

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];

        LibSalt.crypto_sign_keypair(pk, sk);
        LibSalt.crypto_sign_keypair(pk2, sk2);

        ulong smlen = (ulong)LibSalt.crypto_sign_BYTES() + (ulong)mlen;

        byte[] signed_message  = new byte[smlen];
        byte[] signed_message2 = new byte[smlen];

        LibSalt.crypto_sign(signed_message, message, sk);
        LibSalt.crypto_sign(signed_message2, message, sk);

        // Test for proper signed message
        UnitTest.ASSERT_SAME_DATA(signed_message, signed_message2);
    }
예제 #3
0
    // Create signature to use in the header of POST and PUT requests to didery
    public static string signResource(byte[] sm, byte[] m, ulong mlen, byte[] sk, byte[] vk)
    {
        LibSalt.nacl_crypto_sign(sm, m, mlen, sk);
        byte[] sig = new byte[LibSalt.nacl_crypto_sign_BYTES()];

        for (int i = 0; i < sig.Length; i++)
        {
            sig[i] = sm[i];
        }

        byte[] usm     = new byte[m.Length];
        int    success = LibSalt.nacl_crypto_sign_open(usm, sm, (ulong)sm.Length, vk);

        if (success == 0)
        {
            //Debug.Log("Signing successful");
        }
        else
        {
            Debug.Log("Signing unsuccessful: " + success);
        }

        string signature = Convert.ToBase64String(sig).Replace('+', '-').Replace('/', '_');

        return(signature);
    }
예제 #4
0
    public void example_crypto_sign_seed_keypair()
    {
        Console.WriteLine("Example: crypto_sign_seed_keypair()");

        byte[] seed = new byte[] {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
            0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
            0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
        };

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];

        LibSalt.crypto_sign_seed_keypair(pk, sk, seed);
        LibSalt.crypto_sign_seed_keypair(pk2, sk2, seed);

        Console.WriteLine("Keypair One: ");
        Console.WriteLine("pk: " + LibSalt.ByteArrayToString(pk));
        Console.WriteLine("sk: " + LibSalt.ByteArrayToString(sk));

        Console.WriteLine("Keypair Two: ");
        Console.WriteLine("pk: " + LibSalt.ByteArrayToString(pk2));
        Console.WriteLine("sk: " + LibSalt.ByteArrayToString(sk2));
        Console.WriteLine();
    }
예제 #5
0
    // Generates a random seed based on the size of the byte array argument passed in
    public static byte[] randomSeedGenerator(byte[] seed)
    {
        for (int i = 0; i < seed.Length; i++)
        {
            seed[i] = (byte)LibSalt.nacl_randombytes_random();
        }

        return(seed);
    }
예제 #6
0
    public void example_randombytes_buf()
    {
        Console.WriteLine("Example: randombytes_buf()");

        int size = 16;

        byte[] buf = new byte[size];
        LibSalt.randombytes_buf(buf);

        Console.WriteLine("Buffer: " + LibSalt.ByteArrayToString(buf));
        Console.WriteLine();
    }
예제 #7
0
    void example_crypto_sign()
    {
        Console.WriteLine("Example: example_crypto_sign()");

        byte[] message = LibSalt.StringToByteArray("example");
        ulong  mlen    = (ulong)message.Length;

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];
        LibSalt.crypto_sign_keypair(pk, sk);
        LibSalt.crypto_sign_keypair(pk2, sk2);

        ulong smlen = (ulong)LibSalt.crypto_sign_BYTES() + mlen;

        byte[] signed_message = new byte[smlen];
        LibSalt.crypto_sign(signed_message, message, sk);

        Console.WriteLine("Public Key: " + LibSalt.ByteArrayToString(pk));
        Console.WriteLine("Signed Message: " + LibSalt.ByteArrayToString(signed_message));

        byte[] unsigned_message  = new byte[mlen];
        byte[] unsigned_message2 = new byte[mlen];
        int    success           = LibSalt.crypto_sign_open(unsigned_message, signed_message, pk);
        int    success2          = LibSalt.crypto_sign_open(unsigned_message2, signed_message, pk2);

        Console.WriteLine("Unsigned Message: " + LibSalt.ByteArrayToString(unsigned_message));
        Console.WriteLine("Signature Verification for pk: " + LibSalt.ByteArrayToString(pk));

        if (success == 0)
        {
            Console.WriteLine("Valid Signature");
        }
        else
        {
            Console.WriteLine("Invalid signature");
        }

        Console.WriteLine("Signature Verification for pk2: " + LibSalt.ByteArrayToString(pk2));

        if (success2 == 0)
        {
            Console.WriteLine("Valid Signature");
        }
        else
        {
            Console.WriteLine("Invalid signature");
        }
    }
예제 #8
0
 // Generates the one-time pad from a seed
 public static void OTPGenerator(byte[] otp, int size, byte[] seed, int minimumSize = 32)
 {
     // This -hopefully- fixes an issue where the OTP isn't always the same for a given seed
     if (seed.Length < minimumSize)
     {
         byte[] newSeed = new byte[minimumSize];
         System.Buffer.BlockCopy(seed, 0, newSeed, 0, seed.Length);
         LibSalt.nacl_randombytes_buf_deterministic(otp, size, newSeed);
     }
     else
     {
         LibSalt.nacl_randombytes_buf_deterministic(otp, size, seed);
     }
 }
예제 #9
0
    public void test_randombytes_buf()
    {
        Console.WriteLine("Testing randombytes_buf() .....");

        int size = 16;

        byte[] buf  = new byte[size];
        byte[] buf2 = new byte[size];

        LibSalt.randombytes_buf(buf);
        LibSalt.randombytes_buf(buf2);

        UnitTest.ASSERT_DIFFERENT_DATA(buf, buf2);
    }
예제 #10
0
    public void example_randombytes_buf_deterministic()
    {
        Console.WriteLine("Example: randombytes_buf_deterministic()");

        byte[] buf  = new byte[16];
        byte[] seed = new byte[] {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
            0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
            0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
        };

        LibSalt.randombytes_buf_deterministic(buf, seed);
        Console.WriteLine("Buffer: " + LibSalt.ByteArrayToString(buf));
        Console.WriteLine();
    }
예제 #11
0
    public void test_crypto_sign_seed_keypair()
    {
        Console.WriteLine("Testing crypto_sign_seed_keypair() .....");

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];
        LibSalt.crypto_sign_seed_keypair(pk, sk, seed);
        LibSalt.crypto_sign_seed_keypair(pk2, sk2, seed);

        // Test for proper pk and sk array sizes
        UnitTest.ASSERT_EQUALS(pk.Length, crypto_sign_PUBLICKEYBYTES);
        UnitTest.ASSERT_EQUALS(sk.Length, crypto_sign_SECRETKEYBYTES);

        // Test for generating same pk/sk for same seed
        UnitTest.ASSERT_SAME_DATA(pk, pk2);
        UnitTest.ASSERT_SAME_DATA(sk, sk2);
    }
예제 #12
0
    public void test_crypto_sign_keypair()
    {
        Console.WriteLine("Testing crypto_sign_keypair() .....");

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];
        LibSalt.crypto_sign_keypair(pk, sk);
        LibSalt.crypto_sign_keypair(pk2, sk2);

        // Test for proper pk and sk array sizes
        UnitTest.ASSERT_EQUALS(pk.Length, crypto_sign_PUBLICKEYBYTES);
        UnitTest.ASSERT_EQUALS(sk.Length, crypto_sign_SECRETKEYBYTES);

        // Test for generating different random pk/sk
        UnitTest.ASSERT_DIFFERENT_DATA(pk, pk2);
        UnitTest.ASSERT_DIFFERENT_DATA(sk, sk2);
    }
예제 #13
0
    static public void ASSERT_SAME_DATA(byte[] x, byte[] y)
    {
        bool isSame = true;

        for (int i = 0; i < x.Length; i++)
        {
            isSame = (x[i] == y[i]) ? isSame : false;
        }

        if (isSame)
        {
            Console.WriteLine(".. OK ..");
        }
        else
        {
            String x_str = LibSalt.ByteArrayToString(x);
            String y_str = LibSalt.ByteArrayToString(y);
            Console.WriteLine("Error: Expected " + x_str + " == " + y_str);
        }
    }
예제 #14
0
    static public void ASSERT_DIFFERENT_DATA(byte[] x, byte[] y)
    {
        bool isDifferent = false;

        for (int i = 0; i < x.Length; i++)
        {
            isDifferent = (x[i] == y[i]) ? isDifferent : true;
        }

        if (isDifferent)
        {
            Console.WriteLine(".. OK ..");
        }
        else
        {
            String x_str = LibSalt.ByteArrayToString(x);
            String y_str = LibSalt.ByteArrayToString(y);
            Console.WriteLine("Error: Expected " + x_str + " != " + y_str);
        }
    }
예제 #15
0
    public void test_randombytes_buf_deterministic()
    {
        Console.WriteLine("Testing randombytes_buf_deterministic() .....");

        int size = 16;

        byte[] buf  = new byte[size];
        byte[] buf2 = new byte[size];
        byte[] buf3 = new byte[size];
        LibSalt.randombytes_buf_deterministic(buf, seed);
        LibSalt.randombytes_buf_deterministic(buf2, seed);
        LibSalt.randombytes_buf_deterministic(buf3, seed2);

        // Test for generating same randombyte arrays with same seeds
        UnitTest.ASSERT_SAME_DATA(buf, buf2);

        // Test for generating different randombyte arrays with different seeds
        UnitTest.ASSERT_DIFFERENT_DATA(buf, buf3);
        UnitTest.ASSERT_DIFFERENT_DATA(buf2, buf3);
    }
예제 #16
0
    public void example_crypto_sign_keypair()
    {
        Console.WriteLine("Example: crypto_sign_keypair()");

        byte[] pk  = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk  = new byte[crypto_sign_SECRETKEYBYTES];
        byte[] pk2 = new byte[crypto_sign_PUBLICKEYBYTES];
        byte[] sk2 = new byte[crypto_sign_SECRETKEYBYTES];

        LibSalt.crypto_sign_keypair(pk, sk);
        LibSalt.crypto_sign_keypair(pk2, sk2);

        Console.WriteLine("Keypair One: ");
        Console.WriteLine("pk: " + LibSalt.ByteArrayToString(pk));
        Console.WriteLine("sk: " + LibSalt.ByteArrayToString(sk));

        Console.WriteLine("Keypair Two: ");
        Console.WriteLine("pk: " + LibSalt.ByteArrayToString(pk2));
        Console.WriteLine("sk: " + LibSalt.ByteArrayToString(sk2));
        Console.WriteLine();
    }
예제 #17
0
    public void Test()
    {
        double tvalue = LibSalt.Add(1.0f, 1.0f);

        Console.WriteLine(tvalue);

        int value = LibSalt.RandInt();

        Console.WriteLine(value);

        int size = 5;

        int[] buf = new int[size];
        LibSalt.FillArray(buf, size);
        Console.WriteLine("Buf Test");

        Console.WriteLine("Buffer Size: " + buf.Length);
        for (int i = 0; i < size; i++)
        {
            Console.WriteLine(buf[i]);
        }
    }
예제 #18
0
    // Puts together the body of the post request for a OTP encrypted key,
    //  returns a string[] with the did, the signature, and the body of the
    //  post request.
    public static string[] makePost(byte[] encryptedKey, byte[] seed)
    {
        byte[] vk = new byte[32];
        byte[] sk = new byte[64];
        string dateTime;
        string body;
        string did;
        string signature;
        string keyString = Convert.ToBase64String(encryptedKey);

        int signed_bytes = LibSalt.nacl_crypto_sign_BYTES();

        // This function eventually needs to be changed to use a deterministic keypair generator
        //  which should use the user's seed as the RNG seed
        LibSalt.nacl_crypto_sign_seed_keypair(vk, sk, seed);

        did = makeDid(vk);

        dateTime = DateTime.Now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.ffffffzzz");
        body     = "{\"id\":\"" + did + "\",\"blob\":\"" + keyString + "\",\"changed\":\"" + dateTime + "\"}";

        byte[] bodyByte = new byte[body.Length];
        bodyByte = Encoding.UTF8.GetBytes(body);

        byte[] sm = new byte[signed_bytes + bodyByte.Length];

        signature = signResource(sm, bodyByte, (ulong)bodyByte.Length, sk, vk);
        signature = "signer=\"" + signature + "\"";

        string[] data = new string[3];
        data[0] = did;
        data[1] = signature;
        data[2] = body;

        return(data);
    }
예제 #19
0
 public void example_randombytes_random()
 {
     Console.WriteLine("Example: randombytes_random()");
     Console.WriteLine("RandomValue: " + LibSalt.randombytes_random().ToString());
     Console.WriteLine();
 }
예제 #20
0
 public void examples_constants()
 {
     Console.WriteLine("Example: randombytes_SEEDBYTES()");
     Console.WriteLine(LibSalt.randombytes_SEEDBYTES().ToString());
 }
예제 #21
0
    public void test_randombytes_random()
    {
        Console.WriteLine("Testing randombytes_random() .....");

        UnitTest.ASSERT_DIFFERS(LibSalt.randombytes_random(), LibSalt.randombytes_random());
    }