public byte[] VerifyAndDecryptMessage(byte[] encryptedMessage, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey senderPublicKey)
        {
            // verify arguments
            if (nonce == null)
            {
                throw new ArgumentNullException("nonce");
            }
            if (nonce.Length != NonceSize)
            {
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));
            }

            // decrypt
            var paddedMessage = new byte[encryptedMessage.Length];
            var result        = PlatformInvoke64.crypto_box_open(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce,
                                                                 senderPublicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            var message = new byte[paddedMessage.Length - _zeroBytes];

            Buffer.BlockCopy(paddedMessage, (int)_zeroBytes, message, 0, message.Length);

            return(message);
        }
            public byte[] VerifyAndDecrypt(byte[] encryptedMessage, byte[] nonce)
            {
                // verify arguments
                if (nonce == null)
                {
                    throw new ArgumentNullException("nonce");
                }
                if (nonce.Length != _parent.NonceSize)
                {
                    throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", _parent.NonceSize));
                }


                // decrypt
                var paddedMessage = new byte[encryptedMessage.Length];
                var result        = PlatformInvoke64.crypto_box_open_afternm(paddedMessage, encryptedMessage, encryptedMessage.Length, nonce, _sharedSecret);

                if (result != 0)
                {
                    throw new CryptographicException("Failed");
                }

                var message = new byte[paddedMessage.Length - _parent._zeroBytes];

                Buffer.BlockCopy(paddedMessage, (int)_parent._zeroBytes, message, 0, message.Length);

                return(message);
            }
        public byte[] EncryptMessage(byte[] message, byte[] nonce, PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey recipientPublicKey)
        {
            // verify arguments
            if (nonce == null)
            {
                throw new ArgumentNullException("nonce");
            }
            if (nonce.Length != NonceSize)
            {
                throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", NonceSize));
            }

            // pad the message (prepend zero bytes)
            var paddedMessage = new byte[_zeroBytes + message.Length];

            Buffer.BlockCopy(message, 0, paddedMessage, (int)_zeroBytes, message.Length);

            // encrypt
            var encryptedMessage = new byte[paddedMessage.Length];
            var result           = PlatformInvoke64.crypto_box(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, recipientPublicKey.PlainBytes,
                                                               key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            return(encryptedMessage);
        }
            public byte[] Encrypt(byte[] message, byte[] nonce)
            {
                // verify arguments
                if (nonce == null)
                {
                    throw new ArgumentNullException("nonce");
                }
                if (nonce.Length != _parent.NonceSize)
                {
                    throw new ArgumentOutOfRangeException("nonce", string.Format("Nonce must be {0} bytes long", _parent.NonceSize));
                }


                // pad the message (prepend zero bytes)
                var paddedMessage = new byte[_parent._zeroBytes + message.Length];

                Buffer.BlockCopy(message, 0, paddedMessage, (int)_parent._zeroBytes, message.Length);

                // encrypt
                var encryptedMessage = new byte[paddedMessage.Length];
                var result           = PlatformInvoke64.crypto_box_afternm(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, _sharedSecret);

                if (result != 0)
                {
                    throw new CryptographicException("Failed");
                }

                return(encryptedMessage);
            }
        public byte[] CreateRandomNonce()
        {
            var nonce = new byte[NonceSize];

            PlatformInvoke64.randombytes_buf(nonce, new UIntPtr((uint)nonce.Length));

            return(nonce);
        }
 public AuthenticatedPublicKeyCrypto64()
 {
     _publicKeyBytes = PlatformInvoke64.crypto_box_publickeybytes().ToUInt32();
     _secretKeyBytes = PlatformInvoke64.crypto_box_secretkeybytes().ToUInt32();
     _zeroBytes      = PlatformInvoke64.crypto_box_zerobytes().ToUInt32();
     _beforeNmBytes  = PlatformInvoke64.crypto_box_beforenmbytes().ToUInt32();
     NonceSize       = PlatformInvoke64.crypto_box_noncebytes().ToUInt32();
 }
Пример #7
0
 private PrimitiveCryptoConstructs()
 {
     if (Environment.Is64BitProcess)
     {
         PlatformInvoke64.sodium_init();
     }
     else
     {
         PlatformInvoke32.sodium_init();
     }
 }
Пример #8
0
        public SignerKeyPair CreateRandomKeyPair()
        {
            var publicKey = new byte[_publicKeyBytes];
            var secretKey = new byte[_secretKeyBytes];
            var result    = PlatformInvoke64.crypto_sign_keypair(publicKey, secretKey);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            return(new SignerKeyPair(secretKey, publicKey));
        }
        public IAuthenticatedPublicKeyCryptoContext CreateCrypterInstance(PublicKeyCryptoKeyPair key, PublicKeyCryptoPublicKey publicKey)
        {
            // compute the shared secret
            var sharedSecret = new byte[_beforeNmBytes];
            var result       = PlatformInvoke64.crypto_box_beforenm(sharedSecret, publicKey.PlainBytes, key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            return(new AuthenticatedPublicKeyCryptoContext(this, sharedSecret));
        }
Пример #10
0
        public byte[] VerifySignedMessage(byte[] signedMessage, SignerPublicKey publicKey)
        {
            var  message           = new byte[signedMessage.Length];
            long messageRealLength = 0;

            var result = PlatformInvoke64.crypto_sign_open(message, ref messageRealLength, signedMessage, signedMessage.Length,
                                                           publicKey.PlainBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            Array.Resize(ref message, (int)messageRealLength);
            return(message);
        }
Пример #11
0
        public byte[] SignMessage(byte[] message, SignerKeyPair key)
        {
            var  signedMessage           = new byte[message.Length + _bytes];
            long signedMessageRealLength = 0;

            var result = PlatformInvoke64.crypto_sign(signedMessage, ref signedMessageRealLength, message, message.Length,
                                                      key.SecretKeyBytes);

            if (result != 0)
            {
                throw new CryptographicException("Failed");
            }

            Array.Resize(ref signedMessage, (int)signedMessageRealLength);
            return(signedMessage);
        }
Пример #12
0
 public void GetRandomBytes(byte[] buffer)
 {
     PlatformInvoke64.randombytes_buf(buffer, new UIntPtr((uint)buffer.Length));
 }
Пример #13
0
 public MessageSigner64()
 {
     _publicKeyBytes = PlatformInvoke64.crypto_sign_publickeybytes().ToUInt32();
     _secretKeyBytes = PlatformInvoke64.crypto_sign_secretkeybytes().ToUInt32();
     _bytes          = PlatformInvoke64.crypto_sign_bytes().ToUInt32();
 }