示例#1
0
		public byte[] PerformKeyAgreement (ECKeyPair otherPublicKey, int keyDataLength)
		{
			if (otherPublicKey.Q == null) {
				if (otherPublicKey.D == null)
					throw new CryptographicException ();
				otherPublicKey.CreatePublicKeyFromPrivateKey ();
			}
			return PerformKeyAgreement (otherPublicKey.Q, keyDataLength);
		}
示例#2
0
 public Builder setOurSignedPreKey(ECKeyPair ourSignedPreKey)
 {
     this.ourSignedPreKey = ourSignedPreKey;
     return(this);
 }
 public void setSenderKeyState(uint id, uint iteration, byte[] chainKey, ECKeyPair signatureKey)
 {
     senderKeyStates.Clear();
     senderKeyStates.AddFirst(new SenderKeyState(id, iteration, chainKey, signatureKey));
 }
示例#4
0
		public byte[] Encrypt (byte[] value)
#if TEST
		{
			ECKeyPair pair = new ECKeyPair (null, null, _domain);
			return Encrypt (value, pair.PrivateKey);
		}
 internal new TreasuryContractContainer.TreasuryContractStub GetTreasuryContractTester(ECKeyPair keyPair)
 {
     return(GetTester <TreasuryContractContainer.TreasuryContractStub>(TreasuryContractAddress, keyPair));
 }
 internal new EconomicContractContainer.EconomicContractStub GetEconomicContractTester(ECKeyPair keyPair)
 {
     return(GetTester <EconomicContractContainer.EconomicContractStub>(EconomicContractAddress, keyPair));
 }
示例#7
0
        public static Transaction BuildTransaction(Address adrTo = null, ulong nonce = 0, ECKeyPair keyPair = null)
        {
            keyPair = keyPair ?? new KeyPairGenerator().Generate();

            var tx = new Transaction();

            tx.From        = keyPair.GetAddress();
            tx.To          = adrTo ?? Address.FromRawBytes(Hash.Generate().ToByteArray());
            tx.IncrementId = nonce;
            tx.Sig         = new Signature
            {
                P = ByteString.CopyFrom(keyPair.PublicKey.Q.GetEncoded())
            };
            tx.Fee        = TxPoolConfig.Default.FeeThreshold + 1;
            tx.MethodName = "hello world";
            tx.Params     = ByteString.CopyFrom(new Parameters
            {
                Params = { new Param
                           {
                               IntVal = 1
                           } }
            }.ToByteArray());

            // Serialize and hash the transaction
            Hash hash = tx.GetHash();

            // Sign the hash
            ECSigner    signer    = new ECSigner();
            ECSignature signature = signer.Sign(keyPair, hash.DumpByteArray());

            // Update the signature
            tx.Sig.R = ByteString.CopyFrom(signature.R);
            tx.Sig.S = ByteString.CopyFrom(signature.S);
            return(tx);
        }
 internal new ElectionContractContainer.ElectionContractStub GetElectionContractTester(ECKeyPair keyPair)
 {
     return(GetTester <ElectionContractContainer.ElectionContractStub>(ElectionContractAddress, keyPair));
 }
        private async Task <TransactionResult> QuitElectionAsync(ECKeyPair keyPair)
        {
            var electionStub = GetElectionContractTester(keyPair);

            return((await electionStub.QuitElection.SendAsync(new Empty())).TransactionResult);
        }
        private async Task <TransactionResult> WithdrawVotes(ECKeyPair keyPair, Hash voteId)
        {
            var electionStub = GetElectionContractTester(keyPair);

            return((await electionStub.Withdraw.SendAsync(voteId)).TransactionResult);
        }
示例#11
0
        // You can get address of any contract via GetAddress method, for example:
        // internal Address DAppContractAddress => GetAddress(DAppSmartContractAddressNameProvider.StringName);

        internal GreeterContractContainer.GreeterContractStub GetGreeterContractStub(ECKeyPair senderKeyPair)
        {
            return(GetTester <GreeterContractContainer.GreeterContractStub>(DAppContractAddress, senderKeyPair));
        }
示例#12
0
        public void testRootKeyDerivationV2()
        {
            byte[] rootKeySeed =
            {
                0x7b, 0xa6, 0xde, 0xbc, 0x2b,
                0xc1, 0xbb, 0xf9, 0x1a, 0xbb,
                0xc1, 0x36, 0x74, 0x04, 0x17,
                0x6c, 0xa6, 0x23, 0x09, 0x5b,
                0x7e, 0xc6, 0x6b, 0x45, 0xf6,
                0x02, 0xd9, 0x35, 0x38, 0x94,
                0x2d, 0xcc
            };

            byte[] alicePublic =
            {
                0x05, 0xee, 0x4f, 0xa6, 0xcd,
                0xc0, 0x30, 0xdf, 0x49, 0xec,
                0xd0, 0xba, 0x6c, 0xfc, 0xff,
                0xb2, 0x33, 0xd3, 0x65, 0xa2,
                0x7f, 0xad, 0xbe, 0xff, 0x77,
                0xe9, 0x63, 0xfc, 0xb1, 0x62,
                0x22, 0xe1, 0x3a
            };

            byte[] alicePrivate =
            {
                0x21, 0x68, 0x22, 0xec, 0x67,
                0xeb, 0x38, 0x04, 0x9e, 0xba,
                0xe7, 0xb9, 0x39, 0xba, 0xea,
                0xeb, 0xb1, 0x51, 0xbb, 0xb3,
                0x2d, 0xb8, 0x0f, 0xd3, 0x89,
                0x24, 0x5a, 0xc3, 0x7a, 0x94,
                0x8e, 0x50
            };

            byte[] bobPublic =
            {
                0x05, 0xab, 0xb8, 0xeb, 0x29,
                0xcc, 0x80, 0xb4, 0x71, 0x09,
                0xa2, 0x26, 0x5a, 0xbe, 0x97,
                0x98, 0x48, 0x54, 0x06, 0xe3,
                0x2d, 0xa2, 0x68, 0x93, 0x4a,
                0x95, 0x55, 0xe8, 0x47, 0x57,
                0x70, 0x8a, 0x30
            };

            byte[] nextRoot =
            {
                0xb1, 0x14, 0xf5, 0xde, 0x28,
                0x01, 0x19, 0x85, 0xe6, 0xeb,
                0xa2, 0x5d, 0x50, 0xe7, 0xec,
                0x41, 0xa9, 0xb0, 0x2f, 0x56,
                0x93, 0xc5, 0xc7, 0x88, 0xa6,
                0x3a, 0x06, 0xd2, 0x12, 0xa2,
                0xf7, 0x31
            };

            byte[] nextChain =
            {
                0x9d, 0x7d, 0x24, 0x69, 0xbc,
                0x9a, 0xe5, 0x3e, 0xe9, 0x80,
                0x5a, 0xa3, 0x26, 0x4d, 0x24,
                0x99, 0xa3, 0xac, 0xe8, 0x0f,
                0x4c, 0xca, 0xe2, 0xda, 0x13,
                0x43, 0x0c, 0x5c, 0x55, 0xb5,
                0xca, 0x5f
            };

            ECPublicKey  alicePublicKey  = Curve.decodePoint(alicePublic, 0);
            ECPrivateKey alicePrivateKey = Curve.decodePrivatePoint(alicePrivate);
            ECKeyPair    aliceKeyPair    = new ECKeyPair(alicePublicKey, alicePrivateKey);

            ECPublicKey bobPublicKey = Curve.decodePoint(bobPublic, 0);
            RootKey     rootKey      = new RootKey(HKDF.createFor(2), rootKeySeed);

            Pair <RootKey, ChainKey> rootKeyChainKeyPair = rootKey.createChain(bobPublicKey, aliceKeyPair);
            RootKey  nextRootKey  = rootKeyChainKeyPair.first();
            ChainKey nextChainKey = rootKeyChainKeyPair.second();

            CollectionAssert.AreEqual(rootKey.getKeyBytes(), rootKeySeed);
            CollectionAssert.AreEqual(nextRootKey.getKeyBytes(), nextRoot);
            CollectionAssert.AreEqual(nextChainKey.getKey(), nextChain);
        }
示例#13
0
 GetTransactionFeeChargingContractTester(ECKeyPair keyPair)
 {
     return(GetTester <TransactionFeeChargingContractContainer.TransactionFeeChargingContractStub>(
                TransactionFeeChargingContractAddress, keyPair));
 }
示例#14
0
 public Builder setOurRatchetKey(ECKeyPair ourRatchetKey)
 {
     this.ourRatchetKey = ourRatchetKey;
     return(this);
 }
示例#15
0
 public NodeAddress(ECKeyPair publicKey, String ipAddress, int port)
 {
     PublicKey = publicKey;
     IpAddress = ipAddress;
     Port      = port;
 }
 internal new ProfitContractContainer.ProfitContractStub GetProfitContractTester(ECKeyPair keyPair)
 {
     return(GetTester <ProfitContractContainer.ProfitContractStub>(ProfitContractAddress, keyPair));
 }
 public Builder setOurBaseKey(ECKeyPair ourBaseKey)
 {
     this.ourBaseKey = ourBaseKey;
     return(this);
 }
 internal AEDPoSContractContainer.AEDPoSContractStub GetAEDPoSContractTester(ECKeyPair keyPair)
 {
     return(GetTester <AEDPoSContractContainer.AEDPoSContractStub>(ConsensusContractAddress, keyPair));
 }
示例#19
0
        /**
         * Generate the last resort PreKey.  Clients should do this only once, at install
         * time, and durably store it for the length of the install.
         *
         * @return the generated last resort PreKeyRecord.
         */
        public static PreKeyRecord generateLastResortPreKey()
        {
            ECKeyPair keyPair = Curve.generateKeyPair();

            return(new PreKeyRecord(Medium.MAX_VALUE, keyPair));
        }
 internal new ParliamentContractContainer.ParliamentContractStub GetParliamentContractTester(
     ECKeyPair keyPair)
 {
     return(GetTester <ParliamentContractContainer.ParliamentContractStub>(ParliamentContractAddress,
                                                                           keyPair));
 }
示例#21
0
 internal BasicContractZeroContainer.BasicContractZeroStub GetBasicContractTester(ECKeyPair keyPair)
 {
     return(GetTester <BasicContractZeroContainer.BasicContractZeroStub>(ContractZeroAddress, keyPair));
 }
示例#22
0
		public ECDiffieHellman (ECKeyPair keyPair)
			: this (new ECDiffieHellmanParameters (keyPair.D, keyPair.Q, keyPair.Domain))
		{
		}
示例#23
0
 internal TokenConverterContractContainer.TokenConverterContractStub GetTokenConverterContractTester(
     ECKeyPair keyPair)
 {
     return(GetTester <TokenConverterContractContainer.TokenConverterContractStub>(TokenConverterContractAddress,
                                                                                   keyPair));
 }
示例#24
0
		internal ECMQVParameters (ECDomainParameters domain)
		{
			_pair1 = new ECKeyPair (null, null, domain);
			_pair2 = new ECKeyPair (null, null, domain);
		}
示例#25
0
 internal VoteContractContainer.VoteContractStub GetVoteContractTester(ECKeyPair keyPair)
 {
     return(GetTester <VoteContractContainer.VoteContractStub>(VoteContractAddress, keyPair));
 }
示例#26
0
		public byte[] Encrypt (byte[] value, byte[] randomK)
#endif
		{
			int domainLen = (int)((_domain.Bits >> 3) + ((_domain.Bits & 7) == 0 ? 0 : 1));
			int encBlockBytes = (_symmetricAlgo == null ? 0 : _symmetricAlgo.BlockSize >> 3);
			int encKeyLen = (_symmetricAlgo == null ? value.Length : _symmetricAlgo.KeySize >> 3);
			int encPaddingLen = 0;
			int encTotalBytes = value.Length;
			int macKeyLen = _mac.HashSize >> 3;
			byte[] result;
			int ridx = 0;

			if (_params.D == null)
				_params.CreateNewPrivateKey ();
			if (_params.Q == null)
				_params.CreatePublicKeyFromPrivateKey ();
			if (_symmetricAlgo != null) {
				int mod = value.Length % encBlockBytes;
				int rmod = encBlockBytes - mod;
				if (mod == 0) {
					if (!(_symmetricAlgo.Padding == PaddingMode.None || _symmetricAlgo.Padding == PaddingMode.Zeros))
						encPaddingLen = _symmetricAlgo.BlockSize >> 3;
				} else {
					encPaddingLen = rmod;
				}
				encTotalBytes += encPaddingLen;
			}

			// Step.1
#if !TEST
			ECKeyPair pair = new ECKeyPair (null, null, _domain);
#else
			ECKeyPair pair = new ECKeyPair (new Number (randomK, false), null, _domain);
#endif
			
			// Step.2
			// TODO: 点圧縮を利用しないオプションを追加する
			byte[] R = pair.ExportPublicKey (true);
			result = new byte[R.Length + encTotalBytes + macKeyLen];
			for (int i = 0; i < R.Length; i ++) result[ridx ++] = R[i];

			// Step.3 & 4
			// TODO: Cofactor Diffie-Hellmanプリミティブを利用するオプションを追加する
			byte[] z = _params.Q.Multiply (pair.D).Export ().X.ToByteArray (domainLen, false);

			// Step.5
			byte[] K = _kdf.Calculate (z, encKeyLen + macKeyLen);

			// Step.6
			byte[] MK = new byte[macKeyLen];
			for (int i = 0; i < MK.Length; i++)
				MK[i] = K[K.Length - MK.Length + i];

			// Step.7
			if (_symmetricAlgo == null) {
				for (int i = 0; i < value.Length; i++)
					result[ridx++] = (byte)(value[i] ^ K[i]);
			} else {
				byte[] EK = new byte[encKeyLen];
				for (int i = 0; i < EK.Length; i ++)
					EK[i] = K[i];
				using (ICryptoTransform transform = _symmetricAlgo.CreateEncryptor (EK, new byte[encBlockBytes])) {
					int i = 0;
					for (; i < value.Length - encBlockBytes; i += encBlockBytes)
						transform.TransformBlock (value, i, encBlockBytes, result, ridx + i);
					byte[] padding = transform.TransformFinalBlock (value, i, value.Length - i);
					Buffer.BlockCopy (padding, 0, result, ridx + i, padding.Length);
					ridx += i + padding.Length;
				}
			}

			// Step.8
			// TODO: HMAC-SHA1-80への対応
			_mac.Key = MK;
			_mac.Initialize ();
			_mac.TransformBlock (result, R.Length, encTotalBytes, null, 0);
			if (_sharedInfo == null)
				_mac.TransformFinalBlock (result, 0, 0);
			else
				_mac.TransformFinalBlock (_sharedInfo, 0, _sharedInfo.Length);
			_mac.Hash.CopyTo (result, ridx);

			return result;
		}
示例#27
0
 public LightningNode(ECKeyPair lightningKey, ECKeyPair walletKey, NetworkParameters networkParameters)
 {
     LocalKey          = lightningKey;
     NetworkParameters = networkParameters;
     _walletKey        = walletKey;
 }