Пример #1
0
        /// <summary>Verifies a message signed with the Sign method.</summary>
        /// <param name="signedMessage">The signed message.</param>
        /// <param name="key">The 32 byte public key.</param>
        /// <returns>Message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Verify(byte[] signedMessage, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != PUBLIC_KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", PUBLIC_KEY_BYTES));
            }

            var  buffer       = new byte[signedMessage.Length];
            long bufferLength = 0;

            var verified = DynamicInvoke.GetDynamicInvoke <_Verify>("crypto_sign_open", SodiumCore.LibraryName());
            var ret      = verified(buffer, ref bufferLength, signedMessage, signedMessage.Length, key);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to verify signature.");
            }

            var final = new byte[bufferLength];

            Array.Copy(buffer, 0, final, 0, bufferLength);

            return(final);
        }
Пример #2
0
        /// <summary>Encryptes messages via XSalsa20</summary>
        /// <param name="message">The message to be encrypted.</param>
        /// <param name="nonce">The nonce.</param>
        /// <param name="key">The key.</param>
        /// <returns>The encrypted message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Encrypt(byte[] message, byte[] nonce, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != XSALSA20_KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", XSALSA20_KEY_BYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != XSALSA20_NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", XSALSA20_NONCE_BYTES));
            }

            var buffer  = new byte[message.Length];
            var encrypt = DynamicInvoke.GetDynamicInvoke <_Encrypt>("crypto_stream_xor", SodiumCore.LibraryName());
            var ret     = encrypt(buffer, message, message.Length, nonce, key);

            if (ret != 0)
            {
                throw new CryptographicException("Error encrypting message.");
            }

            return(buffer);
        }
Пример #3
0
        /// <summary>
        /// Returns the version of libsodium in use.
        /// </summary>
        /// <returns>
        /// The sodium version string.
        /// </returns>
        public static string SodiumVersionString()
        {
            var ver = DynamicInvoke.GetDynamicInvoke <_SodiumVersionString>("sodium_version_string", LibraryName());
            var ptr = ver();

            return(Marshal.PtrToStringAnsi(ptr));
        }
Пример #4
0
        /// <summary>Hashes a message, with an optional key, using the BLAKE2b primitive.</summary>
        /// <param name="message">The message to be hashed.</param>
        /// <param name="key">The key; may be null, otherwise between 16 and 64 bytes.</param>
        /// <param name="bytes">The size (in bytes) of the desired result.</param>
        /// <returns>Returns a byte array.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="BytesOutOfRangeException"></exception>
        public static byte[] Hash(byte[] message, byte[] key, int bytes)
        {
            //validate the length of the key
            int keyLength;

            if (key != null)
            {
                if (key.Length > KEY_BYTES_MAX || key.Length < KEY_BYTES_MIN)
                {
                    throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.",
                                                                   KEY_BYTES_MIN, KEY_BYTES_MAX));
                }

                keyLength = key.Length;
            }
            else
            {
                key       = new byte[0];
                keyLength = 0;
            }

            //validate output length
            if (bytes > BYTES_MAX || bytes < BYTES_MIN)
            {
                throw new BytesOutOfRangeException("bytes", bytes,
                                                   string.Format("bytes must be between {0} and {1} bytes in length.", BYTES_MIN, BYTES_MAX));
            }

            var buffer = new byte[bytes];
            var hash   = DynamicInvoke.GetDynamicInvoke <_GenericHash>("crypto_generichash", SodiumCore.LibraryName());

            hash(buffer, buffer.Length, message, message.Length, key, keyLength);

            return(buffer);
        }
Пример #5
0
        /// <summary>Returns the hash in a string format, which includes the generated salt.</summary>
        /// <param name="password">The password.</param>
        /// <param name="opsLimit">Represents a maximum amount of computations to perform.</param>
        /// <param name="memLimit">Is the maximum amount of RAM that the function will use, in bytes.</param>
        /// <returns>Returns an zero-terminated ASCII encoded string of the computed password and hash.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="OutOfMemoryException"></exception>
        public static string ScryptHashString(string password, long opsLimit, int memLimit)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password", "Password cannot be null");
            }

            if (opsLimit <= 0)
            {
                throw new ArgumentOutOfRangeException("opsLimit", "opsLimit cannot be zero or negative");
            }

            if (memLimit <= 0)
            {
                throw new ArgumentOutOfRangeException("memLimit", "memLimit cannot be zero or negative");
            }

            var buffer = new byte[SCRYPT_SALSA208_SHA256_BYTES];
            var pass   = Encoding.UTF8.GetBytes(password);

            var hash = DynamicInvoke.GetDynamicInvoke <_HashString>("crypto_pwhash_scryptsalsa208sha256_str", SodiumCore.LibraryName());
            var ret  = hash(buffer, pass, pass.LongLength, opsLimit, memLimit);

            if (ret != 0)
            {
                throw new OutOfMemoryException("Internal error, hash failed");
            }

            return(Encoding.UTF8.GetString(buffer));
        }
Пример #6
0
        /// <summary>Creates a Box</summary>
        /// <param name="message">The message.</param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="secretKey">The secret key to sign message with.</param>
        /// <param name="publicKey">The recipient's public key.</param>
        /// <returns>The encrypted message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Create(byte[] message, byte[] nonce, byte[] secretKey, byte[] publicKey)
        {
            //validate the length of the secret key
            if (secretKey == null || secretKey.Length != SecretKeyBytes)
            {
                throw new KeyOutOfRangeException("secretKey", (secretKey == null) ? 0 : secretKey.Length,
                                                 string.Format("key must be {0} bytes in length.", SecretKeyBytes));
            }

            //validate the length of the public key
            if (publicKey == null || publicKey.Length != PublicKeyBytes)
            {
                throw new KeyOutOfRangeException("publicKey", (publicKey == null) ? 0 : secretKey.Length,
                                                 string.Format("key must be {0} bytes in length.", PublicKeyBytes));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            var buffer = new byte[message.Length + MAC_BYTES];
            var create = DynamicInvoke.GetDynamicInvoke <_Create>("crypto_box_easy", SodiumCore.LibraryName());
            var ret    = create(buffer, message, message.Length, nonce, publicKey, secretKey);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to create SecretBox");
            }

            return(buffer);
        }
Пример #7
0
        /// <summary>Creates a Secret Box</summary>
        /// <param name="message">The message.</param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="key">The 32 byte key.</param>
        /// <returns>The encrypted message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Create(byte[] message, byte[] nonce, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            //pad the message, to start with ZERO_BYTES null bytes
            var paddedMessage = new byte[message.Length + ZERO_BYTES];

            Array.Copy(message, 0, paddedMessage, ZERO_BYTES, message.Length);

            var buffer = new byte[paddedMessage.Length];
            var create = DynamicInvoke.GetDynamicInvoke <_Create>("crypto_secretbox", SodiumCore.LibraryName());
            var ret    = create(buffer, paddedMessage, paddedMessage.Length, nonce, key);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to create SecretBox");
            }

            return(buffer);
        }
Пример #8
0
        /// <summary>Converts a hex-encoded string to a byte array.</summary>
        /// <param name="hex">Hex-encoded data.</param>
        /// <returns>A byte array of the decoded string.</returns>
        /// <exception cref="Exception"></exception>
        public static byte[] HexToBinary(string hex)
        {
            const string IGNORED_CHARS = ":- ";

            var arr = new byte[hex.Length >> 1];
            var bin = Marshal.AllocHGlobal(arr.Length);
            int binLength;

            //we call sodium_hex2bin with some chars to be ignored
            var h   = DynamicInvoke.GetDynamicInvoke <_Hex2Bin>("sodium_hex2bin", SodiumCore.LibraryName());
            var ret = h(bin, arr.Length, hex, hex.Length, IGNORED_CHARS, out binLength, null);

            Marshal.Copy(bin, arr, 0, binLength);
            Marshal.FreeHGlobal(bin);

            if (ret != 0)
            {
                throw new Exception("Internal error, decoding failed.");
            }

            //remove the trailing nulls from the array, if there were some format characters in the hex string before
            if (arr.Length != binLength)
            {
                var tmp = new byte[binLength];
                Array.Copy(arr, 0, tmp, 0, binLength);
                return(tmp);
            }

            return(arr);
        }
Пример #9
0
        /// <summary>Opens a detached Secret Box</summary>
        /// <param name="cipherText">The cipherText.</param>
        /// <param name="mac">The 16 byte mac.</param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="key">The 32 byte nonce.</param>
        /// <returns>The decrypted text.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="MacOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] OpenDetached(byte[] cipherText, byte[] mac, byte[] nonce, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            //validate the length of the mac
            if (mac == null || mac.Length != MAC_BYTES)
            {
                throw new MacOutOfRangeException("mac", (mac == null) ? 0 : mac.Length,
                                                 string.Format("mac must be {0} bytes in length.", MAC_BYTES));
            }

            var buffer = new byte[cipherText.Length];
            var open   = DynamicInvoke.GetDynamicInvoke <_OpenDetached>("crypto_secretbox_open_detached", SodiumCore.LibraryName());
            var ret    = open(buffer, cipherText, mac, cipherText.Length, nonce, key);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to open detached SecretBox");
            }

            return(buffer);
        }
Пример #10
0
        /// <summary>Opens a Secret Box</summary>
        /// <param name="cipherText">The cipherText.</param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="key">The 32 byte nonce.</param>
        /// <returns>The decrypted text.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Open(byte[] cipherText, byte[] nonce, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            var buffer = new byte[cipherText.Length];
            var open   = DynamicInvoke.GetDynamicInvoke <_Open>("crypto_secretbox_open", SodiumCore.LibraryName());
            var ret    = open(buffer, cipherText, cipherText.Length, nonce, key);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to open SecretBox");
            }

            var final = new byte[buffer.Length - ZERO_BYTES];

            Array.Copy(buffer, ZERO_BYTES, final, 0, buffer.Length - ZERO_BYTES);

            return(final);
        }
Пример #11
0
        /// <summary>Creates detached a Secret Box</summary>
        /// <param name="message">The message.</param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="key">The 32 byte key.</param>
        /// <returns>A detached object with a cipher and a mac.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static DetachedBox CreateDetached(byte[] message, byte[] nonce, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            var cipher = new byte[message.Length];
            var mac    = new byte[MAC_BYTES];
            var create = DynamicInvoke.GetDynamicInvoke <_CreateDetached>("crypto_secretbox_detached", SodiumCore.LibraryName());
            var ret    = create(cipher, mac, message, message.Length, nonce, key);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to create detached SecretBox");
            }

            return(new DetachedBox(cipher, mac));
        }
Пример #12
0
        /// <summary>Opens a Box</summary>
        /// <param name="cipherText"></param>
        /// <param name="nonce">The 24 byte nonce.</param>
        /// <param name="secretKey">The recipient's secret key.</param>
        /// <param name="publicKey">The sender's public key.</param>
        /// <returns>The decrypted message.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Open(byte[] cipherText, byte[] nonce, byte[] secretKey, byte[] publicKey)
        {
            //validate the length of the secret key
            if (secretKey == null || secretKey.Length != SecretKeyBytes)
            {
                throw new KeyOutOfRangeException("secretKey", (secretKey == null) ? 0 : secretKey.Length,
                                                 string.Format("key must be {0} bytes in length.", SecretKeyBytes));
            }

            //validate the length of the public key
            if (publicKey == null || publicKey.Length != PublicKeyBytes)
            {
                throw new KeyOutOfRangeException("publicKey", (publicKey == null) ? 0 : secretKey.Length,
                                                 string.Format("key must be {0} bytes in length.", PublicKeyBytes));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NONCE_BYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NONCE_BYTES));
            }

            //check to see if there are MAC_BYTES of leading nulls, if so, trim.
            //this is required due to an error in older versions.
            if (cipherText[0] == 0)
            {
                //check to see if trim is needed
                var trim = true;
                for (var i = 0; i < MAC_BYTES - 1; i++)
                {
                    if (cipherText[i] != 0)
                    {
                        trim = false;
                        break;
                    }
                }

                //if the leading MAC_BYTES are null, trim it off before going on.
                if (trim)
                {
                    var temp = new byte[cipherText.Length - MAC_BYTES];
                    Array.Copy(cipherText, MAC_BYTES, temp, 0, cipherText.Length - MAC_BYTES);

                    cipherText = temp;
                }
            }

            var buffer = new byte[cipherText.Length - MAC_BYTES];
            var open   = DynamicInvoke.GetDynamicInvoke <_Open>("crypto_box_open_easy", SodiumCore.LibraryName());
            var ret    = open(buffer, cipherText, cipherText.Length, nonce, publicKey, secretKey);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to open SecretBox");
            }

            return(buffer);
        }
Пример #13
0
        static SodiumCore()
        {
            Is64 = (IntPtr.Size == 8);

            var init = DynamicInvoke.GetDynamicInvoke <_Init>("sodium_init", LibraryName());

            init();
        }
Пример #14
0
        /// <summary>
        /// Encrypts a message with an authentication tag and additional data.
        /// </summary>
        /// <param name="message">The message to be encrypted.</param>
        /// <param name="nonce">The 8 byte nonce.</param>
        /// <param name="key">The 32 byte key.</param>
        /// <param name="additionalData">The additional data; may be null, otherwise between 0 and 16 bytes.</param>
        /// <returns>The encrypted message with additional data.</returns>
        /// <remarks>The nonce should never ever be reused with the same key.</remarks>
        /// <remarks>The recommended way to generate it is to use GenerateNonce() for the first message, and increment it for each subsequent message using the same key.</remarks>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="NonceOutOfRangeException"></exception>
        /// <exception cref="AdditionalDataOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] Encrypt(byte[] message, byte[] nonce, byte[] key, byte[] additionalData = null)
        {
            //additionalData can be null
            if (additionalData == null)
            {
                additionalData = new byte[0x00];
            }

            //validate the length of the key
            if (key == null || key.Length != KEYBYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEYBYTES));
            }

            //validate the length of the nonce
            if (nonce == null || nonce.Length != NPUBBYTES)
            {
                throw new NonceOutOfRangeException("nonce", (nonce == null) ? 0 : nonce.Length,
                                                   string.Format("nonce must be {0} bytes in length.", NPUBBYTES));
            }

            //validate the length of the additionalData
            if (additionalData.Length > ABYTES || additionalData.Length < 0)
            {
                throw new AdditionalDataOutOfRangeException(
                          string.Format("additionalData must be between {0} and {1} bytes in length.", 0, ABYTES));
            }

            var  cipher = new byte[message.Length + ABYTES];
            var  bin    = Marshal.AllocHGlobal(cipher.Length);
            long cipherLength;

            var encrypt = DynamicInvoke.GetDynamicInvoke <_Encrypt>("crypto_aead_chacha20poly1305_encrypt",
                                                                    SodiumCore.LibraryName());
            var ret = encrypt(bin, out cipherLength, message, message.Length, additionalData, additionalData.Length, null,
                              nonce, key);

            Marshal.Copy(bin, cipher, 0, (int)cipherLength);
            Marshal.FreeHGlobal(bin);

            if (ret != 0)
            {
                throw new CryptographicException("Error encrypting message.");
            }

            if (cipher.Length == cipherLength)
            {
                return(cipher);
            }

            //remove the trailing nulls from the array
            var tmp = new byte[cipherLength];

            Array.Copy(cipher, 0, tmp, 0, cipherLength);

            return(tmp);
        }
Пример #15
0
        /// <summary>Hashes a byte array using the default algorithm (This is what you want to use)</summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public static byte[] Hash(byte[] message)
        {
            var buffer = new byte[SHA512_BYTES];

            var hash = DynamicInvoke.GetDynamicInvoke <_CryptoHash>("crypto_hash", SodiumCore.LibraryName());

            hash(buffer, message, message.Length);

            return(buffer);
        }
Пример #16
0
        /// <summary>Gets random bytes</summary>
        /// <param name="count">The count of bytes to return.</param>
        /// <returns>An array of random bytes.</returns>
        public static byte[] GetRandomBytes(int count)
        {
            var buffer = new byte[count];

            var rnd = DynamicInvoke.GetDynamicInvoke <_GetRandomBytes>("randombytes_buf", LibraryName());

            rnd(buffer, count);

            return(buffer);
        }
Пример #17
0
        /// <summary>Hashes a byte array using the SHA256 algorithm</summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public static byte[] Sha256(byte[] message)
        {
            var buffer = new byte[SHA256_BYTES];

            var hash = DynamicInvoke.GetDynamicInvoke <_Sha256>("crypto_hash_sha256", SodiumCore.LibraryName());

            hash(buffer, message, message.Length);

            return(buffer);
        }
Пример #18
0
        /// <summary>Creates a new key pair based on a random seed.</summary>
        /// <returns>A KeyPair.</returns>
        public static KeyPair GenerateKeyPair()
        {
            var publicKey  = new byte[PublicKeyBytes];
            var privateKey = new byte[SecretKeyBytes];

            var kp = DynamicInvoke.GetDynamicInvoke <_GenerateKeyPair>("crypto_box_keypair", SodiumCore.LibraryName());

            kp(publicKey, privateKey);

            return(new KeyPair(publicKey, privateKey));
        }
Пример #19
0
        /// <summary>Creates a new key pair based on a random seed.</summary>
        /// <returns>A KeyPair.</returns>
        public static KeyPair GenerateKeyPair()
        {
            var publicKey  = new byte[PUBLIC_KEY_BYTES];
            var privateKey = new byte[SECRET_KEY_BYTES];

            var kp = DynamicInvoke.GetDynamicInvoke <_GenerateKeyPair>("crypto_sign_keypair", SodiumCore.LibraryName());

            kp(publicKey, privateKey);

            return(new KeyPair(publicKey, privateKey));
        }
Пример #20
0
        /// <summary>Takes a byte array and returns a hex-encoded string.</summary>
        /// <param name="data">Data to be encoded.</param>
        /// <returns>Hex-encoded string, lodercase.</returns>
        /// <exception cref="OverflowException"></exception>
        public static string BinaryToHex(byte[] data)
        {
            var hex = new byte[data.Length * 2 + 1];
            var b   = DynamicInvoke.GetDynamicInvoke <_Bin2Hex>("sodium_bin2hex", SodiumCore.LibraryName());
            var ret = b(hex, hex.Length, data, data.Length);

            if (ret == IntPtr.Zero)
            {
                throw new OverflowException("Internal error, encoding failed.");
            }

            return(Marshal.PtrToStringAnsi(ret));
        }
Пример #21
0
        /// <summary>
        /// Diffie-Hellman (function computes the public key)
        /// </summary>
        /// <param name="secretKey">A secret key.</param>
        /// <returns>A computed public key.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        public static byte[] Base(byte[] secretKey)
        {
            //validate the length of the scalar
            if (secretKey == null || secretKey.Length != SCALAR_BYTES)
            {
                throw new KeyOutOfRangeException("secretKey", (secretKey == null) ? 0 : secretKey.Length,
                                                 string.Format("secretKey must be {0} bytes in length.", SCALAR_BYTES));
            }

            var publicKey = new byte[SCALAR_BYTES];
            var b         = DynamicInvoke.GetDynamicInvoke <_Base>("crypto_scalarmult_base", SodiumCore.LibraryName());

            b(publicKey, secretKey);

            return(publicKey);
        }
Пример #22
0
        /// <summary>Signs a message with HMAC-SHA512-256.</summary>
        /// <param name="message">The message.</param>
        /// <param name="key">The 32 byte key.</param>
        /// <returns>32 byte authentication code.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        public static byte[] Sign(byte[] message, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            var buffer = new byte[BYTES];
            var sign   = DynamicInvoke.GetDynamicInvoke <_Sign>("crypto_auth", SodiumCore.LibraryName());

            sign(buffer, message, message.Length, key);

            return(buffer);
        }
Пример #23
0
        /// <summary>Verifies that a hash generated with ScryptHashString matches the supplied password.</summary>
        /// <param name="hash">The hash.</param>
        /// <param name="password">The password.</param>
        /// <returns><c>true</c> on success; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static bool ScryptHashStringVerify(byte[] hash, byte[] password)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password", "Password cannot be null");
            }
            if (hash == null)
            {
                throw new ArgumentNullException("hash", "Hash cannot be null");
            }

            var verify = DynamicInvoke.GetDynamicInvoke <_HashVerify>("crypto_pwhash_scryptsalsa208sha256_str_verify", SodiumCore.LibraryName());
            var ret    = verify(hash, password, password.LongLength);

            return(ret == 0);
        }
Пример #24
0
        /// <summary>Generates a hash based on a key, salt and personal bytes</summary>
        /// <returns><c>byte</c> hashed message</returns>
        /// <param name="message">Message.</param>
        /// <param name="key">Key.</param>
        /// <param name="salt">Salt.</param>
        /// <param name="personal">Personal string.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="SaltOutOfRangeException"></exception>
        /// <exception cref="PersonalOutOfRangeException"></exception>
        public static byte[] HashSaltPersonal(byte[] message, byte[] key, byte[] salt, byte[] personal)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message", "Message cannot be null");
            }

            if (salt == null)
            {
                throw new ArgumentNullException("salt", "Salt cannot be null");
            }

            if (personal == null)
            {
                throw new ArgumentNullException("personal", "Personal string cannot be null");
            }

            if (key != null && (key.Length > KEY_BYTES_MAX || key.Length < KEY_BYTES_MIN))
            {
                throw new KeyOutOfRangeException(string.Format("key must be between {0} and {1} bytes in length.", KEY_BYTES_MIN, KEY_BYTES_MAX));
            }

            if (key == null)
            {
                key = new byte[0];
            }

            if (salt.Length != SALT_BYTES)
            {
                throw new SaltOutOfRangeException(string.Format("Salt must be {0} bytes in length.", SALT_BYTES));
            }

            if (personal.Length != PERSONAL_BYTES)
            {
                throw new PersonalOutOfRangeException(string.Format("Personal bytes must be {0} bytes in length.", PERSONAL_BYTES));
            }

            var buffer = new byte[OUT_BYTES];

            var hash = DynamicInvoke.GetDynamicInvoke <_GenericHashSaltPersonal>("crypto_generichash_blake2b_salt_personal", SodiumCore.LibraryName());

            hash(buffer, buffer.Length, message, message.LongLength, key, key.Length, salt, personal);

            return(buffer);
        }
Пример #25
0
        /// <summary>Creates a new key pair based on the provided seed.</summary>
        /// <param name="seed">The seed.</param>
        /// <returns>A KeyPair.</returns>
        /// <exception cref="SeedOutOfRangeException"></exception>
        public static KeyPair GenerateKeyPair(byte[] seed)
        {
            var publicKey  = new byte[PUBLIC_KEY_BYTES];
            var privateKey = new byte[SECRET_KEY_BYTES];

            //validate the length of the seed
            if (seed == null || seed.Length != SEED_BYTES)
            {
                throw new SeedOutOfRangeException("seed", (seed == null) ? 0 : seed.Length,
                                                  string.Format("seed must be {0} bytes in length.", SEED_BYTES));
            }

            var kp = DynamicInvoke.GetDynamicInvoke <_GenerateKeyPairFromSeed>("crypto_sign_seed_keypair", SodiumCore.LibraryName());

            kp(publicKey, privateKey, seed);

            return(new KeyPair(publicKey, privateKey));
        }
Пример #26
0
        /// <summary>Signs a message with Ed25519.</summary>
        /// <param name="message">The message.</param>
        /// <param name="key">The 64 byte private key.</param>
        /// <returns>The signature.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        public static byte[] SignDetached(byte[] message, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != SECRET_KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", SECRET_KEY_BYTES));
            }

            var  signature       = new byte[SIGNATURE_BYTES];
            long signatureLength = 0;

            var sig = DynamicInvoke.GetDynamicInvoke <_SignDetached>("crypto_sign_detached", SodiumCore.LibraryName());

            sig(signature, ref signatureLength, message, message.Length, key);

            return(signature);
        }
Пример #27
0
        /// <summary>
        /// Derives a secret key of any size from a password and a salt.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="opsLimit">Represents a maximum amount of computations to perform.</param>
        /// <param name="memLimit">Is the maximum amount of RAM that the function will use, in bytes.</param>
        /// <param name="outputLength">The length of the computed output array.</param>
        /// <returns>Returns a byte array of the given size.</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="SaltOutOfRangeException"></exception>
        /// <exception cref="OutOfMemoryException"></exception>
        public static byte[] ScryptHashBinary(byte[] password, byte[] salt, long opsLimit, int memLimit, long outputLength = SCRYPT_SALSA208_SHA256_SALTBYTES)
        {
            if (password == null)
            {
                throw new ArgumentNullException("password", "Password cannot be null");
            }

            if (salt == null)
            {
                throw new ArgumentNullException("salt", "Salt cannot be null");
            }

            if (salt.Length != SCRYPT_SALSA208_SHA256_SALTBYTES)
            {
                throw new SaltOutOfRangeException(string.Format("Salt must be {0} bytes in length.", SCRYPT_SALSA208_SHA256_SALTBYTES));
            }

            if (opsLimit <= 0)
            {
                throw new ArgumentOutOfRangeException("opsLimit", "opsLimit cannot be zero or negative");
            }

            if (memLimit <= 0)
            {
                throw new ArgumentOutOfRangeException("memLimit", "memLimit cannot be zero or negative");
            }

            if (outputLength <= 0)
            {
                throw new ArgumentOutOfRangeException("outputLength", "OutputLength cannot be zero or negative");
            }

            var buffer = new byte[outputLength];

            var hash = DynamicInvoke.GetDynamicInvoke <_HashBinary>("crypto_pwhash_scryptsalsa208sha256", SodiumCore.LibraryName());
            var ret  = hash(buffer, buffer.Length, password, password.LongLength, salt, opsLimit, memLimit);

            if (ret != 0)
            {
                throw new OutOfMemoryException("Internal error, hash failed");
            }

            return(buffer);
        }
Пример #28
0
        /// <summary>Verifies a message signed with the SignDetached method.</summary>
        /// <param name="signature">The signature.</param>
        /// <param name="message">The message.</param>
        /// <param name="key">The 32 byte public key.</param>
        /// <returns><c>true</c> on success; otherwise, <c>false</c>.</returns>
        /// <exception cref="SignatureOutOfRangeException"></exception>
        /// <exception cref="KeyOutOfRangeException"></exception>
        public static bool VerifyDetached(byte[] signature, byte[] message, byte[] key)
        {
            //validate the length of the signature
            if (signature == null || signature.Length != SIGNATURE_BYTES)
            {
                throw new SignatureOutOfRangeException("signature", (signature == null) ? 0 : signature.Length,
                                                       string.Format("signature must be {0} bytes in length.", SIGNATURE_BYTES));
            }

            //validate the length of the key
            if (key == null || key.Length != PUBLIC_KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", PUBLIC_KEY_BYTES));
            }

            var verified = DynamicInvoke.GetDynamicInvoke <_VerifyDetached>("crypto_sign_verify_detached", SodiumCore.LibraryName());
            var ret      = verified(signature, message, message.Length, key);

            return(ret == 0);
        }
Пример #29
0
        /// <summary>Verifies a message signed with the Sign method.</summary>
        /// <param name="message">The message.</param>
        /// <param name="signature">The 16 byte signature.</param>
        /// <param name="key">The 32 byte key.</param>
        /// <returns>True if verified.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="SignatureOutOfRangeException"></exception>
        public static bool Verify(byte[] message, byte[] signature, byte[] key)
        {
            //validate the length of the key
            if (key == null || key.Length != KEY_BYTES)
            {
                throw new KeyOutOfRangeException("key", (key == null) ? 0 : key.Length,
                                                 string.Format("key must be {0} bytes in length.", KEY_BYTES));
            }

            //validate the length of the signature
            if (signature == null || signature.Length != BYTES)
            {
                throw new SignatureOutOfRangeException("signature", (signature == null) ? 0 : signature.Length,
                                                       string.Format("signature must be {0} bytes in length.", BYTES));
            }

            var verify = DynamicInvoke.GetDynamicInvoke <_Verify>("crypto_onetimeauth_verify", SodiumCore.LibraryName());
            var ret    = verify(signature, message, message.Length, key);

            return(ret == 0);
        }
Пример #30
0
        /// <summary>Converts the ed25519 public key to curve25519 public key.</summary>
        /// <param name="ed25519PublicKey">Ed25519 public key.</param>
        /// <returns>The curve25519 public key.</returns>
        /// <exception cref="KeyOutOfRangeException"></exception>
        /// <exception cref="CryptographicException"></exception>
        public static byte[] ConvertEd25519PublicKeyToCurve25519PublicKey(byte[] ed25519PublicKey)
        {
            //validate the length of the key
            if (ed25519PublicKey == null || ed25519PublicKey.Length != PUBLIC_KEY_BYTES)
            {
                throw new KeyOutOfRangeException("ed25519PublicKey", (ed25519PublicKey == null) ? 0 : ed25519PublicKey.Length,
                                                 string.Format("ed25519PublicKey must be {0} bytes in length.", PUBLIC_KEY_BYTES));
            }

            var buffer = new byte[PublicKeyBox.PublicKeyBytes];

            var pk  = DynamicInvoke.GetDynamicInvoke <_Ed25519PublicKeyToCurve25519PublicKey>("crypto_sign_ed25519_pk_to_curve25519", SodiumCore.LibraryName());
            var ret = pk(buffer, ed25519PublicKey);

            if (ret != 0)
            {
                throw new CryptographicException("Failed to convert public key.");
            }

            return(buffer);
        }