Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="payload"></param>
        /// <param name="payloadIndex"></param>
        /// <param name="payloadLength"></param>
        /// <param name="key"></param>
        /// <param name="keyIndex"></param>
        /// <param name="keyLength"></param>
        /// <param name="curl"></param>
        public static void UnMask(
            sbyte[] payload, int payloadIndex, int payloadLength,
            sbyte[] key, int keyIndex, int keyLength,
            ICurl curl)
        {
            curl.Absorb(key, keyIndex, keyLength);
            var keyChunk = new sbyte[Constants.HashLength];

            curl.Squeeze(keyChunk);

            for (var i = 0; i < payloadLength; i += Constants.HashLength)
            {
                var left   = payloadLength - i;
                var length = left > Constants.HashLength ? Constants.HashLength : left;

                for (var n = 0; n < length; n++)
                {
                    keyChunk[n] = TritsHelper.Sum(payload[payloadIndex + i + n], (sbyte)-keyChunk[n]);
                }

                Array.Copy(keyChunk, 0, payload, payloadIndex + i, length);
                curl.Absorb(keyChunk, 0, length);

                Array.Copy(curl.Rate, keyChunk, length);
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="payload"></param>
        /// <param name="payloadIndex"></param>
        /// <param name="payloadLength"></param>
        /// <param name="curl"></param>
        public static void UnMaskSlice(sbyte[] payload, int payloadIndex, int payloadLength, ICurl curl)
        {
            var keyChunk = new int[Constants.HashLength];

            for (var i = 0; i < payloadLength; i += Constants.HashLength)
            {
                var left   = payloadLength - i;
                var length = left > Constants.HashLength ? Constants.HashLength : left;

                Array.Copy(curl.Rate, keyChunk, length);

                for (var n = 0; n < length; n++)
                {
                    payload[payloadIndex + i + n] =
                        TritsHelper.Sum(payload[payloadIndex + i + n], (sbyte)-keyChunk[n]);
                }

                curl.Absorb(payload, payloadIndex + i, length);
            }
        }
Example #3
0
        private sbyte[] CreateMaskedPayload(
            sbyte[] seedTrits, sbyte[] messageTrits,
            sbyte[] keyTrits, sbyte[] rootTrits,
            sbyte[] siblingsTrits, sbyte[] nextRootTrits,
            int start, int index, int security)
        {
            ICurl curl         = new Curl(SpongeFactory.Mode.CURLP27);
            ICurl encrCurl     = new Curl(SpongeFactory.Mode.CURLP27);
            var   hammingNonce = new HammingNonce(SpongeFactory.Mode.CURLP27);

            var minLength     = GetPayloadMinLength(messageTrits.Length, siblingsTrits.Length, index, security);
            var payloadLength = (int)TritsHelper.RoundThird(minLength);
            var payload       = new sbyte[payloadLength];

            // Creates a signed, encrypted payload from a message

            // generate the key and the get the merkle tree hashes
            var messageLength = messageTrits.Length;

            var indexP   = Pascal.EncodedLength(index);
            var messageP = Pascal.EncodedLength(messageLength);

            var siblingsLength       = siblingsTrits.Length;
            var siblingsCount        = siblingsTrits.Length / Constants.HashLength;
            var siblingsPascalLength = Pascal.EncodedLength(siblingsCount);
            var signatureLength      = security * Constants.KeyLength;

            var nextRootStart     = indexP + messageP;
            var nextEnd           = nextRootStart + nextRootTrits.Length;
            var messageEnd        = nextRootStart + Constants.HashLength + messageLength;
            var nonceEnd          = messageEnd + Constants.MessageNonceLength;
            var signatureEnd      = nonceEnd + signatureLength;
            var siblingsPascalEnd = signatureEnd + siblingsPascalLength;
            var siblingsEnd       = siblingsPascalEnd + siblingsLength;

            encrCurl.Absorb(keyTrits);
            encrCurl.Absorb(rootTrits);

            var trits = new sbyte[indexP];

            Pascal.Encode(index, trits);
            Array.Copy(trits, 0, payload, 0, indexP);

            trits = new sbyte[messageP];
            Pascal.Encode(messageLength, trits);
            Array.Copy(trits, 0, payload, indexP, messageP);

            encrCurl.Absorb(payload, 0, nextRootStart);
            Array.Copy(nextRootTrits, 0, payload, nextRootStart, nextRootTrits.Length);
            Array.Copy(messageTrits, 0, payload, nextEnd, messageTrits.Length);
            MaskHelper.MaskSlice(payload, nextRootStart, messageEnd - nextRootStart, encrCurl);

            Array.Copy(encrCurl.State, curl.State, encrCurl.State.Length);


            hammingNonce.Search(security, 0, Constants.HashLength / 3, curl);
            Array.Copy(curl.State, 0, payload, messageEnd, Constants.MessageNonceLength);
            MaskHelper.MaskSlice(payload, messageEnd, nonceEnd - messageEnd, encrCurl);

            curl.Reset();
            var subseed = HashHelper.Subseed(seedTrits, start + index, curl);

            Array.Copy(subseed, 0, payload, nonceEnd, subseed.Length);

            curl.Reset();
            HashHelper.Key(payload, nonceEnd, signatureEnd - nonceEnd, security, curl);

            curl.Reset();
            HashHelper.Signature(encrCurl.Rate, payload, nonceEnd, signatureEnd - nonceEnd, curl);

            curl.Reset();

            trits = new sbyte[siblingsPascalLength];
            Pascal.Encode(siblingsCount, trits);
            Array.Copy(trits, 0, payload, signatureEnd, siblingsPascalLength);
            Array.Copy(siblingsTrits, 0, payload, siblingsPascalEnd, siblingsLength);

            MaskHelper.MaskSlice(payload, nonceEnd, siblingsEnd - nonceEnd, encrCurl);
            encrCurl.Reset();

            return(payload);
        }