Esempio n. 1
0
        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           = PlatformInvoke32.crypto_box(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, recipientPublicKey.PlainBytes,
                                                               key.SecretKeyBytes);

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

            return(encryptedMessage);
        }
Esempio n. 2
0
        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        = PlatformInvoke32.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);
        }
Esempio n. 3
0
            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           = PlatformInvoke32.crypto_box_afternm(encryptedMessage, paddedMessage, paddedMessage.Length, nonce, _sharedSecret);

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

                return(encryptedMessage);
            }
Esempio n. 4
0
            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        = PlatformInvoke32.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);
            }
Esempio n. 5
0
        public byte[] CreateRandomNonce()
        {
            var nonce = new byte[NonceSize];

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

            return(nonce);
        }
Esempio n. 6
0
 public AuthenticatedPublicKeyCrypto32()
 {
     _publicKeyBytes = PlatformInvoke32.crypto_box_publickeybytes().ToUInt32();
     _secretKeyBytes = PlatformInvoke32.crypto_box_secretkeybytes().ToUInt32();
     _zeroBytes      = PlatformInvoke32.crypto_box_zerobytes().ToUInt32();
     _beforeNmBytes  = PlatformInvoke32.crypto_box_beforenmbytes().ToUInt32();
     NonceSize       = PlatformInvoke32.crypto_box_noncebytes().ToUInt32();
 }
 private PrimitiveCryptoConstructs()
 {
     if (Environment.Is64BitProcess)
     {
         PlatformInvoke64.sodium_init();
     }
     else
     {
         PlatformInvoke32.sodium_init();
     }
 }
Esempio n. 8
0
        public PublicKeyCryptoKeyPair CreateRandomKeyPair()
        {
            var publicKey = new byte[_publicKeyBytes];
            var secretKey = new byte[_secretKeyBytes];
            var result    = PlatformInvoke32.crypto_box_keypair(publicKey, secretKey);

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

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

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

            return(new AuthenticatedPublicKeyCryptoContext(this, sharedSecret));
        }
Esempio n. 10
0
        public byte[] VerifySignedMessage(byte[] signedMessage, SignerPublicKey publicKey)
        {
            var  message           = new byte[signedMessage.Length];
            long messageRealLength = 0;

            var result = PlatformInvoke32.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);
        }
Esempio n. 11
0
        public byte[] SignMessage(byte[] message, SignerKeyPair key)
        {
            var  signedMessage           = new byte[message.Length + _bytes];
            long signedMessageRealLength = 0;

            var result = PlatformInvoke32.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);
        }
Esempio n. 12
0
 public void GetRandomBytes(byte[] buffer)
 {
     PlatformInvoke32.randombytes_buf(buffer, new UIntPtr((uint)buffer.Length));
 }
Esempio n. 13
0
 public MessageSigner32()
 {
     _publicKeyBytes = PlatformInvoke32.crypto_sign_publickeybytes().ToUInt32();
     _secretKeyBytes = PlatformInvoke32.crypto_sign_secretkeybytes().ToUInt32();
     _bytes          = PlatformInvoke32.crypto_sign_bytes().ToUInt32();
 }