public void ConvertToCurve25519Test()
        {
            // Keypair seed from libsodium-net
            var keypairSeed = new byte[] {
                0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde,
                0x3d, 0x24, 0x71, 0x15, 0xf9, 0x4a, 0xed, 0xae,
                0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, 0xfa, 0xbe,
                0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xed, 0xee
            };

            var kp = PublicKeyAuth.GenerateKeyPair(keypairSeed);

            var ed25519Pk   = kp.Public;
            var ed25519SkPk = kp.Secret;

            var curve25519Pk = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(ed25519Pk);
            var curve25519Sk = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(ed25519SkPk);

            Assert.AreEqual(Convert.ToBase64String(curve25519Pk), "8YFPDo/xBD2KRNJbq/887crmwiw+2qSPhXrnDeK6rlA=");
            Assert.AreEqual(Convert.ToBase64String(curve25519Sk), "gFIDA3bUcRK+f3PtegGSk90SrZELZURVeYtGZ9c94WY=");

            for (var i = 0; i < 500; i++)
            {
                kp           = PublicKeyAuth.GenerateKeyPair();
                ed25519Pk    = kp.Public;
                ed25519SkPk  = kp.Secret;
                curve25519Pk = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(ed25519Pk);
                curve25519Sk = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(ed25519SkPk);
                var curve25519Pk2 = ScalarMult.Base(curve25519Sk);

                CollectionAssert.AreEqual(curve25519Pk, curve25519Pk2);
            }
        }
Esempio n. 2
0
        public void PublicKeyAuthConvertToCurve25519()
        {
            var keypairSeed = new byte[] {
                0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde,
                0x3d, 0x24, 0x71, 0x15, 0xf9, 0x4a, 0xed, 0xae,
                0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, 0xfa, 0xbe,
                0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xed, 0xee
            };

            var keys = PublicKeyAuth.GenerateKeyPair(keypairSeed);

            var ed25519Pk   = keys.PublicKey;
            var ed25519SkPk = keys.PrivateKey;

            var curve25519Pk = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(ed25519Pk);
            var curve25519Sk = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(ed25519SkPk);

            Assert.AreEqual(Utilities.BinaryToHex(curve25519Pk, Utilities.HexFormat.None, Utilities.HexCase.Upper),
                            "F1814F0E8FF1043D8A44D25BABFF3CEDCAE6C22C3EDAA48F857AE70DE2BAAE50");
            Assert.AreEqual(Utilities.BinaryToHex(curve25519Sk, Utilities.HexFormat.None, Utilities.HexCase.Upper),
                            "8052030376D47112BE7F73ED7A019293DD12AD910B654455798B4667D73DE166");

            for (var i = 0; i < 500; i++)
            {
                keys         = PublicKeyAuth.GenerateKeyPair();
                ed25519Pk    = keys.PublicKey;
                ed25519SkPk  = keys.PrivateKey;
                curve25519Pk = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(ed25519Pk);
                curve25519Sk = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(ed25519SkPk);
                var curve25519Pk2 = ScalarMult.Base(curve25519Sk);

                CollectionAssert.AreEqual(curve25519Pk, curve25519Pk2);
            }
        }
 public KeyPair(Sodium.KeyPair a)
 {
     this.PublicKey = PublicKeyAuth
                      .ConvertEd25519PublicKeyToCurve25519PublicKey(a.PublicKey);
     this.PrivateKey = PublicKeyAuth
                       .ConvertEd25519SecretKeyToCurve25519SecretKey(a.PrivateKey);
 }
        private void DeriveSecrets()
        {
            var curve25519Sk = PublicKeyAuth
                               .ConvertEd25519SecretKeyToCurve25519SecretKey(
                this._longterm_client_keypair.PrivateKey
                );

            var curve25519Pk = PublicKeyAuth
                               .ConvertEd25519PublicKeyToCurve25519PublicKey(
                _longterm_server_pk
                );

            this._shared_ab = ScalarMult.Mult(
                this._ephemeral_client_keypair.PrivateKey,
                this._ephemeral_server_pk
                );

            this._shared_aB = ScalarMult.Mult(
                this._ephemeral_client_keypair.PrivateKey,
                curve25519Pk
                );

            this._shared_Ab = ScalarMult.Mult(
                curve25519Sk,
                _ephemeral_server_pk
                );
        }
        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));
        }
Esempio n. 6
0
        // Decrypts data with receiver’s private key using Libsodium
        public static string DecryptWithPrivateKey(string data, string privateKey)
        {
            var privateKeyConverted = PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(Hex.Decode(privateKey));
            var publicKey           = Hex.Decode(GetPublicKeyFromPrivate(privateKey));
            var publicKeyConverted  = PublicKeyAuth.ConvertEd25519PublicKeyToCurve25519PublicKey(publicKey);

            return(Encoding.UTF8.GetString(SealedPublicKeyBox.Open(Hex.Decode(data), privateKeyConverted, publicKeyConverted)));
        }
Esempio n. 7
0
        public void ConvertEd25519SecretKeyToCurve25519SecretKeyBadKey()
        {
            //Don`t copy keypairSeed for other tests (bad key)!
            //62 byte
            var keypairSeed = new byte[] {
                0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde,
                0x3d, 0x24, 0x71, 0x15, 0xf9, 0x4a, 0xed, 0xae,
                0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, 0xfa, 0xbe,
                0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xea, 0xde,
                0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde,
                0x3d, 0x24, 0x71, 0x15, 0xf9, 0x4a, 0xed, 0xae,
                0x42, 0x31, 0x81, 0x24, 0x09, 0x5a, 0xfa, 0xbe,
                0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa
            };

            PublicKeyAuth.ConvertEd25519SecretKeyToCurve25519SecretKey(keypairSeed);
        }