예제 #1
0
        PullMsgResult ProduceReady(ref Msg msg)
        {
            int         metadataLength = BasicPropertiesLength;
            Span <byte> readyNonce     = stackalloc byte[Curve25519XSalsa20Poly1305.NonceLength];

            byte[] readyPlaintext = new byte[metadataLength];

            //  Create Box [metadata](S'->C')
            AddBasicProperties(readyPlaintext);

            ReadyNoncePrefix.CopyTo(readyNonce);
            NetworkOrderBitsConverter.PutUInt64(m_nonce, readyNonce.Slice(16));

            msg.InitPool(14 + Curve25519XSalsa20Poly1305.TagLength + metadataLength);
            var readyBox = msg.Slice(14);

            Assumes.NotNull(m_box);

            m_box.Encrypt(readyBox, readyPlaintext, readyNonce);
            Array.Clear(readyPlaintext, 0, readyPlaintext.Length);

            Span <byte> ready = msg;

            ReadyLiteral.CopyTo(ready);

            //  Short nonce, prefixed by "CurveZMQREADY---"
            readyNonce.Slice(16).CopyTo(ready.Slice(6));

            m_nonce++;

            return(0);
        }
예제 #2
0
        PushMsgResult ProcessReady(ref Msg msg)
        {
            if (msg.Size < 30)
            {
                return(PushMsgResult.Error);
            }

            Span <byte> readyNonce     = stackalloc byte[Curve25519XSalsa20Poly1305.NonceLength];
            var         readyPlaintext = new byte[msg.Size - 14 - Curve25519XSalsa20Poly1305.TagLength];
            Span <byte> readyBox       = msg.Slice(14);

            ReadyNoncePrefix.CopyTo(readyNonce);
            msg.Slice(6, 8).CopyTo(readyNonce.Slice(16));
            m_peerNonce = NetworkOrderBitsConverter.ToUInt64(msg, 6);

            bool isDecrypted = m_box.TryDecrypt(readyPlaintext, readyBox, readyNonce);

            if (!isDecrypted)
            {
                return(PushMsgResult.Error);
            }

            if (!ParseMetadata(readyPlaintext))
            {
                return(PushMsgResult.Error);
            }

            Array.Clear(readyPlaintext, 0, readyPlaintext.Length);

            m_state = State.Connected;
            return(PushMsgResult.Ok);
        }