예제 #1
0
        public void RandomBytesGeneration_Should_Maintain_Length()
        {
            //Arrange
            Byte[] b1   = new Byte[1];
            Byte[] b10  = new Byte[10];
            Byte[] b100 = new Byte[100];
            Byte[] b200 = new Byte[200];

            //Act
            TweetNaCl.RandomBytes(b1);
            TweetNaCl.RandomBytes(b10);
            TweetNaCl.RandomBytes(b100);
            TweetNaCl.RandomBytes(b200);

            //Assert
            String text1   = Encoding.ASCII.GetString(b1);
            String text10  = Encoding.ASCII.GetString(b10);
            String text100 = Encoding.ASCII.GetString(b100);
            String text200 = Encoding.ASCII.GetString(b200);

            Assert.AreEqual(1, text1.Length);
            Assert.AreEqual(10, text10.Length);
            Assert.AreEqual(100, text100.Length);
            Assert.AreEqual(200, text200.Length);
        }
        public static byte[] DecodeMessage(byte[] message, byte[] nonce, byte[] senderPublicKey, byte[] recipientPrivateKey)
        {
            var curvePublicKey = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(senderPublicKey);
            var curveSecretKey = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(recipientPrivateKey);

            return(TweetNaCl.CryptoBoxOpen(message, nonce, curvePublicKey, curveSecretKey));
        }
예제 #3
0
        public void TestForMessageDecryptionTweetNaCljsKeyPair()
        {
            Byte[] apk = Convert.FromBase64String("GK4GzNY+fbkRPd5fwYUaca70iENh2A1QRss1KBtpWU4=");
            Byte[] ask = Convert.FromBase64String("HQT4qtjv/3Q0nGYX4DB776e6QeUE40wr71MxNSg0+bc=");

            Byte[] bpk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] bsk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            String message = "test";

            Byte[] bMessage      = Encoding.UTF8.GetBytes(message);
            Byte[] paddedMessage = new Byte[TweetNaCl.BoxZeroBytes + bMessage.Length];
            Byte[] nonce         = new Byte[TweetNaCl.BoxNonceBytes];
            Byte[] k             = new Byte[TweetNaCl.BoxBeforenmBytes];

            var result = -10;

            apk = TweetNaCl.CryptoBoxKeypair(ask);
            Assert.AreNotEqual(result, -1, "key pair A generation failed.");

            bpk = TweetNaCl.CryptoBoxKeypair(bsk);
            Assert.AreNotEqual(result, -1, "key pair B generation failed.");

            TweetNaCl.RandomBytes(nonce);
            Assert.AreNotEqual(result, -1, "randombytes generation failed.");

            var encMessage = TweetNaCl.CryptoBox(paddedMessage, nonce, bpk, ask);

            Assert.AreNotEqual(result, -1, "encryption failed.");

            var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);

            Assert.AreNotEqual(result, -1, "decryption failed.");
        }
예제 #4
0
        public void CryptoBoxOpen_DecryptionDifferentKeyPair_Should_Success()
        {
            //Arrange
            Byte[] apk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] ask = new Byte[TweetNaCl.BoxSecretKeyBytes];

            Byte[] bpk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] bsk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            String expectedMessage = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(expectedMessage);
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];

            apk = TweetNaCl.CryptoBoxKeypair(ask);
            bpk = TweetNaCl.CryptoBoxKeypair(bsk);

            TweetNaCl.RandomBytes(nonce);

            //Act
            var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
            var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
            Assert.AreEqual(decMessage.Length, bMessage.Length, "decryption failed.");

            var resultMessage = Encoding.ASCII.GetString(decMessage);

            Assert.AreEqual(resultMessage, expectedMessage, "decryption failed.");
        }
예제 #5
0
        public void CryptoBoxOpenAfternm_Decryption_Should_Success()
        {
            //Arrange
            String expectedMessage = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(expectedMessage);
            Byte[] pk       = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk       = new Byte[TweetNaCl.BoxSecretKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];

            pk = TweetNaCl.CryptoBoxKeypair(sk);
            TweetNaCl.RandomBytes(nonce);
            var k = TweetNaCl.CryptoBoxBeforenm(pk, sk);

            //Act
            var encMessage = TweetNaCl.CryptoBoxAfternm(bMessage, nonce, k);
            var decMessage = TweetNaCl.CryptoBoxOpenAfternm(encMessage, nonce, k);

            //Assert
            Assert.AreEqual(decMessage.Length, bMessage.Length, "decryption failed.");
            Assert.AreEqual(decMessage, bMessage, "decryption failed.");

            var resultMessage = Encoding.ASCII.GetString(decMessage);

            Assert.AreEqual(resultMessage, expectedMessage, "decryption failed.");
        }
예제 #6
0
        public void CryptoSignKeypair_Generation_Should_Success()
        {
            //Arrange
            Byte[] ssk = new Byte[TweetNaCl.SignSecretKeyBytes];

            //Act
            Byte[] spk = TweetNaCl.CryptoSignKeypair(ssk);

            //Assert
            Assert.AreEqual(Encoding.ASCII.GetString(spk).Length, 32, "Public Key for message sign generation failed.");
            Assert.AreEqual(Encoding.ASCII.GetString(ssk).Length, 64, "Secret Key for message sign generation failed.");
        }
예제 #7
0
        public void CryptoBoxKeypair_Should_Success()
        {
            //Arrange
            Byte[] pk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            //Act
            pk = TweetNaCl.CryptoBoxKeypair(sk);

            //Assert
            Assert.AreEqual(Encoding.ASCII.GetString(pk).Length, 32, "key generation failed.");
        }
예제 #8
0
        public void CryptoBoxBeforenm_MessageEncryption_Should_Success()
        {
            //Arrange
            Byte[] pk = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk = new Byte[TweetNaCl.BoxSecretKeyBytes];

            pk = TweetNaCl.CryptoBoxKeypair(sk);

            //Act
            var k = TweetNaCl.CryptoBoxBeforenm(pk, sk);

            //Assert
            Assert.AreEqual(k.Length, TweetNaCl.BoxBeforenmBytes, "generation of K for encryption failed.");
        }
예제 #9
0
        public void CryptoScalarmult_Should_Success()
        {
            //Arrange
            Byte[] n = new Byte[TweetNaCl.ScalarmultBytes];
            Byte[] p = new Byte[TweetNaCl.ScalarBytes];
            Byte[] q = new Byte[TweetNaCl.ScalarmultBytes];

            TweetNaCl.RandomBytes(n);
            TweetNaCl.RandomBytes(p);

            //Act
            q = TweetNaCl.CryptoScalarmult(n, p);

            //Assert
            Assert.AreEqual(Encoding.ASCII.GetString(q).Length, 32, "wrong size for resulting group element q.");
        }
예제 #10
0
        public void CryptoSign_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] ssk      = new Byte[TweetNaCl.SignSecretKeyBytes];
            Byte[] sMessage;

            //Act
            var spk = TweetNaCl.CryptoSignKeypair(ssk);

            sMessage = TweetNaCl.CryptoSign(bMessage, ssk);

            //Assert
            Assert.AreEqual(Encoding.ASCII.GetString(sMessage).Length, bMessage.Length + TweetNaCl.SignBytes, "Message sign failed.");
        }
예제 #11
0
        public void CryptoSecretBox_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] sk       = new Byte[TweetNaCl.SecretBoxKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.SecretBoxNonceBytes];
            TweetNaCl.RandomBytes(sk);
            TweetNaCl.RandomBytes(nonce);

            //Act
            var encMessage = TweetNaCl.CryptoSecretBox(bMessage, nonce, sk);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
        }
예제 #12
0
        internal static void key_derive(byte[] shared, byte[] salt, byte[] secretKey, byte[] pubkey)
        {
            var longKeyHash  = new byte[64];
            var shortKeyHash = new byte[32];

            Array.Reverse(secretKey);

            // compute  Sha3(512) hash of secret key (as in prepareForScalarMultiply)
            var digestSha3 = new KeccakDigest(512);

            digestSha3.BlockUpdate(secretKey, 0, 32);
            digestSha3.DoFinal(longKeyHash, 0);

            longKeyHash[0]  &= 248;
            longKeyHash[31] &= 127;
            longKeyHash[31] |= 64;

            Array.Copy(longKeyHash, 0, shortKeyHash, 0, 32);

            ScalarOperations.sc_clamp(shortKeyHash, 0);

            var p = new[] { new long[16], new long[16], new long[16], new long[16] };
            var q = new[] { new long[16], new long[16], new long[16], new long[16] };

            TweetNaCl.Unpackneg(q, pubkey); // returning -1 invalid signature
            TweetNaCl.Scalarmult(p, q, shortKeyHash, 0);
            TweetNaCl.Pack(shared, p);

            // for some reason the most significant bit of the last byte needs to be flipped.
            // doesnt seem to be any corrosponding action in nano/nem.core, so this may be an issue in one of the above 3 functions. i have no idea.
            shared[31] ^= (1 << 7);

            // salt
            for (var i = 0; i < salt.Length; i++)
            {
                shared[i] ^= salt[i];
            }

            // hash salted shared key
            var digestSha3Two = new KeccakDigest(256);

            digestSha3Two.BlockUpdate(shared, 0, 32);
            digestSha3Two.DoFinal(shared, 0);
        }
예제 #13
0
        public void CryptoSignOpen_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] ssk      = new Byte[TweetNaCl.SignSecretKeyBytes];
            Byte[] sMessage = new Byte[TweetNaCl.SignBytes + bMessage.Length];
            Byte[] cMessage = new Byte[bMessage.Length];

            var spk = TweetNaCl.CryptoSignKeypair(ssk);

            sMessage = TweetNaCl.CryptoSign(bMessage, ssk);

            //Act
            cMessage = TweetNaCl.CryptoSignOpen(sMessage, spk);
            Assert.AreEqual(cMessage.Length, bMessage.Length, "Message sign verification failed.");
            Assert.AreEqual(Encoding.UTF8.GetString(cMessage), message, "Messages sign verification failed.");
        }
예제 #14
0
        public void CryptoHash_MessageHashing_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] hsh1     = new Byte[TweetNaCl.HashBytes];
            Byte[] hsh2     = new Byte[TweetNaCl.HashBytes];

            var firstResult  = 0;
            var secontResult = 0;

            //Act
            firstResult  = TweetNaCl.CryptoHash(hsh1, bMessage, bMessage.Length);
            secontResult = TweetNaCl.CryptoHash(hsh2, bMessage, bMessage.Length);

            //Assert
            Assert.AreNotEqual(firstResult, -1, "First hashing call for message generation failed.");
            Assert.AreNotEqual(secontResult, -1, "Second hashing call for message generation failed.");
            Assert.AreEqual(hsh1, hsh2, "hash for message are not equal.");
        }
예제 #15
0
        public void CryptoBoxAfternm_MessageEncryption_Should_Success()
        {
            //Arrange
            String message = "test";

            Byte[] bMessage = Encoding.UTF8.GetBytes(message);
            Byte[] pk       = new Byte[TweetNaCl.BoxPublicKeyBytes];
            Byte[] sk       = new Byte[TweetNaCl.BoxSecretKeyBytes];
            Byte[] nonce    = new Byte[TweetNaCl.BoxNonceBytes];
            TweetNaCl.RandomBytes(nonce);

            pk = TweetNaCl.CryptoBoxKeypair(sk);

            var k = TweetNaCl.CryptoBoxBeforenm(pk, sk);

            //Act
            var encMessage = TweetNaCl.CryptoBoxAfternm(bMessage, nonce, k);

            //Assert
            Assert.AreEqual(encMessage.Length, bMessage.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
        }
예제 #16
0
        static void Main(string[] args)
        {
            Timer timer = Metric.Timer("Requests", Unit.Requests);

            for (var i = 0; i < 10; i++)
            {
                Byte[] apk = new Byte[TweetNaCl.BOX_PUBLICKEYBYTES];
                Byte[] ask = new Byte[TweetNaCl.BOX_SECRETKEYBYTES];

                Byte[] bpk = new Byte[TweetNaCl.BOX_PUBLICKEYBYTES];
                Byte[] bsk = new Byte[TweetNaCl.BOX_SECRETKEYBYTES];

                String message  = "test";
                Byte[] bMessage = Encoding.UTF8.GetBytes(message);
                Byte[] nonce    = new Byte[TweetNaCl.BOX_NONCEBYTES];
                Byte[] k        = new Byte[TweetNaCl.BOX_BEFORENMBYTES];

                apk = TweetNaCl.CryptoBoxKeypair(ask);
                bpk = TweetNaCl.CryptoBoxKeypair(bsk);

                TweetNaCl.RandomBytes(nonce);

                using (var context = timer.NewContext("Encryption"))
                {
                    var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
                    var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);
                }

                using (var context = timer.NewContext("Decryption"))
                {
                    var encMessage = TweetNaCl.CryptoBox(bMessage, nonce, bpk, ask);
                    var decMessage = TweetNaCl.CryptoBoxOpen(encMessage, nonce, apk, bsk);
                }

                //Console.WriteLine(timer.);
            }

            Console.ReadKey();
        }
예제 #17
0
        public void Libsoduim_TestForBox()
        {
            var encMessage = TweetNaCl.CryptoBox(m, nonce, bobpk, alicesk);

            Assert.AreEqual(encMessage.Length, m.Length + TweetNaCl.BoxBoxZeroBytes, "encryption failed.");
        }