diff --git a/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/CordaRPCClientTest.kt b/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/CordaRPCClientTest.kt index f7062286ab..40a179fd29 100644 --- a/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/CordaRPCClientTest.kt +++ b/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/CordaRPCClientTest.kt @@ -5,7 +5,7 @@ import net.corda.core.flows.FlowInitiator import net.corda.core.getOrThrow import net.corda.core.messaging.* import net.corda.core.node.services.ServiceInfo -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.utilities.OpaqueBytes import net.corda.testing.ALICE import net.corda.flows.CashException diff --git a/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/RPCStabilityTests.kt b/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/RPCStabilityTests.kt index 4cf43156d2..4a9dad7a36 100644 --- a/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/RPCStabilityTests.kt +++ b/client/rpc/src/integration-test/kotlin/net/corda/client/rpc/RPCStabilityTests.kt @@ -10,6 +10,7 @@ import com.google.common.util.concurrent.Futures import net.corda.client.rpc.internal.RPCClient import net.corda.client.rpc.internal.RPCClientConfiguration import net.corda.core.* +import net.corda.core.crypto.random63BitValue import net.corda.core.messaging.RPCOps import net.corda.testing.driver.poll import net.corda.node.services.messaging.RPCServerConfiguration diff --git a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClient.kt b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClient.kt index 3e52dbd946..0412aeac36 100644 --- a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClient.kt +++ b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClient.kt @@ -4,7 +4,7 @@ import com.google.common.net.HostAndPort import net.corda.core.logElapsedTime import net.corda.core.messaging.RPCOps import net.corda.core.minutes -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.seconds import net.corda.core.utilities.loggerFor import net.corda.nodeapi.ArtemisTcpTransport.Companion.tcpTransport diff --git a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClientProxyHandler.kt b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClientProxyHandler.kt index 86dcf7a7d1..9d38896c19 100644 --- a/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClientProxyHandler.kt +++ b/client/rpc/src/main/kotlin/net/corda/client/rpc/internal/RPCClientProxyHandler.kt @@ -14,7 +14,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder import net.corda.core.ThreadBox import net.corda.core.getOrThrow import net.corda.core.messaging.RPCOps -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.serialization.KryoPoolWithContext import net.corda.core.utilities.* import net.corda.nodeapi.* diff --git a/client/rpc/src/test/kotlin/net/corda/client/rpc/RPCConcurrencyTests.kt b/client/rpc/src/test/kotlin/net/corda/client/rpc/RPCConcurrencyTests.kt index 2ffe065832..fb283773d1 100644 --- a/client/rpc/src/test/kotlin/net/corda/client/rpc/RPCConcurrencyTests.kt +++ b/client/rpc/src/test/kotlin/net/corda/client/rpc/RPCConcurrencyTests.kt @@ -4,7 +4,7 @@ import net.corda.client.rpc.internal.RPCClientConfiguration import net.corda.core.future import net.corda.core.messaging.RPCOps import net.corda.core.millis -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.serialization.CordaSerializable import net.corda.node.services.messaging.RPCServerConfiguration import net.corda.testing.RPCDriverExposedDSLInterface diff --git a/core/src/main/kotlin/net/corda/core/Utils.kt b/core/src/main/kotlin/net/corda/core/Utils.kt index 021e6e194d..541240ae11 100644 --- a/core/src/main/kotlin/net/corda/core/Utils.kt +++ b/core/src/main/kotlin/net/corda/core/Utils.kt @@ -7,7 +7,6 @@ import com.google.common.base.Throwables import com.google.common.io.ByteStreams import com.google.common.util.concurrent.* import net.corda.core.crypto.SecureHash -import net.corda.core.crypto.newSecureRandom import net.corda.core.crypto.sha256 import net.corda.core.flows.FlowException import net.corda.core.serialization.CordaSerializable @@ -59,12 +58,6 @@ infix fun Int.checkedAdd(b: Int) = Math.addExact(this, b) @Suppress("unused") infix fun Long.checkedAdd(b: Long) = Math.addExact(this, b) -/** - * Returns a random positive long generated using a secure RNG. This function sacrifies a bit of entropy in order to - * avoid potential bugs where the value is used in a context where negative numbers are not expected. - */ -fun random63BitValue(): Long = Math.abs(newSecureRandom().nextLong()) - /** Same as [Future.get] but with a more descriptive name, and doesn't throw [ExecutionException], instead throwing its cause */ fun Future.getOrThrow(timeout: Duration? = null): T { return try { diff --git a/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt b/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt index b795c24ad6..a92ab97845 100644 --- a/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt +++ b/core/src/main/kotlin/net/corda/core/crypto/Crypto.kt @@ -1,6 +1,6 @@ package net.corda.core.crypto -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.i2p.crypto.eddsa.EdDSAEngine import net.i2p.crypto.eddsa.EdDSAPrivateKey import net.i2p.crypto.eddsa.EdDSAPublicKey diff --git a/core/src/main/kotlin/net/corda/core/crypto/CryptoUtils.kt b/core/src/main/kotlin/net/corda/core/crypto/CryptoUtils.kt index 0f0b73ea0a..310f34ed7f 100644 --- a/core/src/main/kotlin/net/corda/core/crypto/CryptoUtils.kt +++ b/core/src/main/kotlin/net/corda/core/crypto/CryptoUtils.kt @@ -201,9 +201,23 @@ fun secureRandomBytes(numOfBytes: Int): ByteArray { */ @Throws(NoSuchAlgorithmException::class) fun newSecureRandom(): SecureRandom { - if (System.getProperty("os.name") == "Linux") { - return SecureRandom.getInstance("NativePRNGNonBlocking") + return if (System.getProperty("os.name") == "Linux") { + SecureRandom.getInstance("NativePRNGNonBlocking") } else { - return SecureRandom.getInstanceStrong() + SecureRandom.getInstanceStrong() + } +} + +/** + * Returns a random positive non-zero long generated using a secure RNG. This function sacrifies a bit of entropy in order + * to avoid potential bugs where the value is used in a context where negative numbers or zero are not expected. + */ +fun random63BitValue(): Long { + while (true) { + val candidate = Math.abs(newSecureRandom().nextLong()) + // No need to check for -0L + if (candidate != 0L && candidate != Long.MIN_VALUE) { + return candidate + } } } diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index b0a46f0cab..85c1d923a5 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -23,6 +23,8 @@ UNRELEASED * In Java, ``QueryCriteriaUtilsKt`` has moved to ``QueryCriteriaUtils``. Also ``and`` and ``or`` are now instance methods of ``QueryCrtieria``. +* ``random63BitValue()`` has moved to ``CryptoUtils`` + Milestone 13 ------------ diff --git a/finance/src/main/kotlin/net/corda/contracts/CommercialPaper.kt b/finance/src/main/kotlin/net/corda/contracts/CommercialPaper.kt index 0bfeb4ea50..f3d2fcdb2e 100644 --- a/finance/src/main/kotlin/net/corda/contracts/CommercialPaper.kt +++ b/finance/src/main/kotlin/net/corda/contracts/CommercialPaper.kt @@ -13,7 +13,7 @@ import net.corda.core.crypto.toBase58String import net.corda.core.identity.AbstractParty import net.corda.core.identity.Party import net.corda.core.node.services.VaultService -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.schemas.MappedSchema import net.corda.core.schemas.PersistentState import net.corda.core.schemas.QueryableState diff --git a/finance/src/main/kotlin/net/corda/contracts/asset/Obligation.kt b/finance/src/main/kotlin/net/corda/contracts/asset/Obligation.kt index 02bff607b4..dbc7cf54dc 100644 --- a/finance/src/main/kotlin/net/corda/contracts/asset/Obligation.kt +++ b/finance/src/main/kotlin/net/corda/contracts/asset/Obligation.kt @@ -14,7 +14,7 @@ import net.corda.core.crypto.testing.NULL_PARTY import net.corda.core.identity.AbstractParty import net.corda.core.identity.AnonymousParty import net.corda.core.identity.Party -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.serialization.CordaSerializable import net.corda.core.transactions.TransactionBuilder import net.corda.core.utilities.Emoji diff --git a/node/src/integration-test/kotlin/net/corda/services/messaging/MQSecurityTest.kt b/node/src/integration-test/kotlin/net/corda/services/messaging/MQSecurityTest.kt index f549064a6f..3b99f6845f 100644 --- a/node/src/integration-test/kotlin/net/corda/services/messaging/MQSecurityTest.kt +++ b/node/src/integration-test/kotlin/net/corda/services/messaging/MQSecurityTest.kt @@ -11,7 +11,7 @@ import net.corda.core.flows.InitiatingFlow import net.corda.core.getOrThrow import net.corda.core.identity.Party import net.corda.core.messaging.CordaRPCOps -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.testing.ALICE import net.corda.testing.BOB import net.corda.core.utilities.unwrap diff --git a/node/src/integration-test/kotlin/net/corda/services/messaging/P2PMessagingTest.kt b/node/src/integration-test/kotlin/net/corda/services/messaging/P2PMessagingTest.kt index af7c8635bd..d9eb27dbc9 100644 --- a/node/src/integration-test/kotlin/net/corda/services/messaging/P2PMessagingTest.kt +++ b/node/src/integration-test/kotlin/net/corda/services/messaging/P2PMessagingTest.kt @@ -3,6 +3,7 @@ package net.corda.services.messaging import com.google.common.util.concurrent.Futures import com.google.common.util.concurrent.ListenableFuture import net.corda.core.* +import net.corda.core.crypto.random63BitValue import net.corda.core.messaging.MessageRecipients import net.corda.core.messaging.SingleMessageRecipient import net.corda.core.node.services.DEFAULT_SESSION_ID diff --git a/node/src/integration-test/kotlin/net/corda/services/messaging/P2PSecurityTest.kt b/node/src/integration-test/kotlin/net/corda/services/messaging/P2PSecurityTest.kt index 3df678b855..da327f6417 100644 --- a/node/src/integration-test/kotlin/net/corda/services/messaging/P2PSecurityTest.kt +++ b/node/src/integration-test/kotlin/net/corda/services/messaging/P2PSecurityTest.kt @@ -6,7 +6,7 @@ import net.corda.core.crypto.X509Utilities import net.corda.core.crypto.cert import net.corda.core.getOrThrow import net.corda.core.node.NodeInfo -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.seconds import net.corda.node.internal.NetworkMapInfo import net.corda.node.services.config.configureWithDevSSLCertificate diff --git a/node/src/main/kotlin/net/corda/node/services/messaging/NodeMessagingClient.kt b/node/src/main/kotlin/net/corda/node/services/messaging/NodeMessagingClient.kt index 0f73b693ea..beaa4ddc12 100644 --- a/node/src/main/kotlin/net/corda/node/services/messaging/NodeMessagingClient.kt +++ b/node/src/main/kotlin/net/corda/node/services/messaging/NodeMessagingClient.kt @@ -3,6 +3,7 @@ package net.corda.node.services.messaging import com.google.common.net.HostAndPort import com.google.common.util.concurrent.ListenableFuture import net.corda.core.* +import net.corda.core.crypto.random63BitValue import net.corda.core.messaging.CordaRPCOps import net.corda.core.messaging.MessageRecipients import net.corda.core.messaging.RPCOps diff --git a/node/src/main/kotlin/net/corda/node/services/messaging/RPCServer.kt b/node/src/main/kotlin/net/corda/node/services/messaging/RPCServer.kt index 1306c4206b..05c13f6341 100644 --- a/node/src/main/kotlin/net/corda/node/services/messaging/RPCServer.kt +++ b/node/src/main/kotlin/net/corda/node/services/messaging/RPCServer.kt @@ -14,7 +14,7 @@ import com.google.common.collect.SetMultimap import com.google.common.util.concurrent.ThreadFactoryBuilder import net.corda.core.ErrorOr import net.corda.core.messaging.RPCOps -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.seconds import net.corda.core.serialization.KryoPoolWithContext import net.corda.core.utilities.LazyStickyPool diff --git a/node/src/main/kotlin/net/corda/node/services/network/NetworkMapService.kt b/node/src/main/kotlin/net/corda/node/services/network/NetworkMapService.kt index ce8319ebcb..d0f51b534e 100644 --- a/node/src/main/kotlin/net/corda/node/services/network/NetworkMapService.kt +++ b/node/src/main/kotlin/net/corda/node/services/network/NetworkMapService.kt @@ -13,7 +13,7 @@ import net.corda.core.node.services.DEFAULT_SESSION_ID import net.corda.core.node.services.KeyManagementService import net.corda.core.node.services.NetworkMapCache import net.corda.core.node.services.ServiceType -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.serialization.CordaSerializable import net.corda.core.serialization.SerializedBytes import net.corda.core.serialization.deserialize diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt index 0b7d085b18..537597f833 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/FlowStateMachineImpl.kt @@ -13,7 +13,7 @@ import net.corda.core.crypto.SecureHash import net.corda.core.flows.* import net.corda.core.identity.Party import net.corda.core.internal.FlowStateMachine -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.transactions.SignedTransaction import net.corda.core.utilities.ProgressTracker import net.corda.core.utilities.UntrustworthyData diff --git a/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManager.kt b/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManager.kt index 3e1a8bdf35..fcae71e21b 100644 --- a/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManager.kt +++ b/node/src/main/kotlin/net/corda/node/services/statemachine/StateMachineManager.kt @@ -17,6 +17,7 @@ import com.google.common.util.concurrent.MoreExecutors import io.requery.util.CloseableIterator import net.corda.core.* import net.corda.core.crypto.SecureHash +import net.corda.core.crypto.random63BitValue import net.corda.core.flows.FlowException import net.corda.core.flows.FlowInitiator import net.corda.core.flows.FlowLogic diff --git a/node/src/main/kotlin/net/corda/node/services/transactions/OutOfProcessTransactionVerifierService.kt b/node/src/main/kotlin/net/corda/node/services/transactions/OutOfProcessTransactionVerifierService.kt index e0d2999526..1539455fd9 100644 --- a/node/src/main/kotlin/net/corda/node/services/transactions/OutOfProcessTransactionVerifierService.kt +++ b/node/src/main/kotlin/net/corda/node/services/transactions/OutOfProcessTransactionVerifierService.kt @@ -6,7 +6,7 @@ import com.google.common.util.concurrent.ListenableFuture import com.google.common.util.concurrent.SettableFuture import net.corda.core.crypto.SecureHash import net.corda.core.node.services.TransactionVerifierService -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.serialization.SingletonSerializeAsToken import net.corda.core.transactions.LedgerTransaction import net.corda.core.utilities.loggerFor diff --git a/node/src/test/kotlin/net/corda/node/services/statemachine/FlowFrameworkTests.kt b/node/src/test/kotlin/net/corda/node/services/statemachine/FlowFrameworkTests.kt index e59e703dec..26ea5b06f3 100644 --- a/node/src/test/kotlin/net/corda/node/services/statemachine/FlowFrameworkTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/statemachine/FlowFrameworkTests.kt @@ -11,6 +11,7 @@ import net.corda.core.contracts.StateAndRef import net.corda.core.contracts.testing.DummyState import net.corda.core.crypto.SecureHash import net.corda.core.crypto.generateKeyPair +import net.corda.core.crypto.random63BitValue import net.corda.core.flows.FlowException import net.corda.core.flows.FlowLogic import net.corda.core.flows.FlowSessionException diff --git a/test-utils/src/main/kotlin/net/corda/testing/RPCDriver.kt b/test-utils/src/main/kotlin/net/corda/testing/RPCDriver.kt index f4d09c8d23..2276f19301 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/RPCDriver.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/RPCDriver.kt @@ -11,7 +11,7 @@ import net.corda.client.rpc.internal.RPCClientConfiguration import net.corda.core.div import net.corda.core.map import net.corda.core.messaging.RPCOps -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.testing.driver.ProcessUtilities import net.corda.node.services.RPCUserService import net.corda.node.services.messaging.ArtemisMessagingServer diff --git a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt index 05f3c115be..0dfd96d2fe 100644 --- a/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt +++ b/test-utils/src/main/kotlin/net/corda/testing/node/MockNode.kt @@ -10,6 +10,7 @@ import net.corda.core.* import net.corda.core.crypto.CertificateAndKeyPair import net.corda.core.crypto.cert import net.corda.core.crypto.entropyToKeyPair +import net.corda.core.crypto.random63BitValue import net.corda.core.identity.PartyAndCertificate import net.corda.core.messaging.MessageRecipients import net.corda.core.messaging.RPCOps diff --git a/verifier/src/integration-test/kotlin/net/corda/verifier/VerifierDriver.kt b/verifier/src/integration-test/kotlin/net/corda/verifier/VerifierDriver.kt index 13cdb8d6aa..f8adb129b1 100644 --- a/verifier/src/integration-test/kotlin/net/corda/verifier/VerifierDriver.kt +++ b/verifier/src/integration-test/kotlin/net/corda/verifier/VerifierDriver.kt @@ -11,7 +11,7 @@ import net.corda.core.crypto.X509Utilities import net.corda.core.crypto.commonName import net.corda.core.div import net.corda.core.map -import net.corda.core.random63BitValue +import net.corda.core.crypto.random63BitValue import net.corda.core.transactions.LedgerTransaction import net.corda.testing.driver.ProcessUtilities import net.corda.core.utilities.loggerFor