summaryrefslogtreecommitdiff
path: root/core/java/android/net/MacAddress.java
diff options
context:
space:
mode:
authorJong Wook Kim <jongwook@google.com>2018-01-31 19:03:19 -0800
committerJong Wook Kim <jongwook@google.com>2018-02-07 01:28:46 +0000
commitf0a55cc9eda84f90ad8b96784c4afa9a25e82801 (patch)
treef0bd1ba01080a24da20469dae3f748402f650d98 /core/java/android/net/MacAddress.java
parent9193014ee63b3ad007f74eb2b2f2fd741c6b6388 (diff)
MacAddress: Use SecureRandom and add a 46 bit randomized MAC generator
Use SecureRandom instead of Random since Random is time based and can increase the chance of generating same MAC address across multiple devices. createRandomUnicastAddress should randomize all bits of the address, except for locally assigned bit and unicast bit. The previous method that only randomizes NIC and use Google Base OUI is renamed to createRandomUnicastAddressWithGoogleBase. Bug: 72450936 Test: runtest frameworks-net Change-Id: Icda650638c2c1c9fd90d509a87e86347c0e05f2d
Diffstat (limited to 'core/java/android/net/MacAddress.java')
-rw-r--r--core/java/android/net/MacAddress.java29
1 files changed, 24 insertions, 5 deletions
diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java
index 287bdc88dd3e..74d64704c8d2 100644
--- a/core/java/android/net/MacAddress.java
+++ b/core/java/android/net/MacAddress.java
@@ -26,6 +26,7 @@ import com.android.internal.util.Preconditions;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Random;
@@ -329,16 +330,34 @@ public final class MacAddress implements Parcelable {
/**
* Returns a generated MAC address whose 24 least significant bits constituting the
- * NIC part of the address are randomly selected.
+ * NIC part of the address are randomly selected and has Google OUI base.
*
* The locally assigned bit is always set to 1. The multicast bit is always set to 0.
*
- * @return a random locally assigned MacAddress.
+ * @return a random locally assigned, unicast MacAddress with Google OUI.
+ *
+ * @hide
+ */
+ public static @NonNull MacAddress createRandomUnicastAddressWithGoogleBase() {
+ return createRandomUnicastAddress(BASE_GOOGLE_MAC, new SecureRandom());
+ }
+
+ /**
+ * Returns a generated MAC address whose 46 bits, excluding the locally assigned bit and the
+ * unicast bit, are randomly selected.
+ *
+ * The locally assigned bit is always set to 1. The multicast bit is always set to 0.
+ *
+ * @return a random locally assigned, unicast MacAddress.
*
* @hide
*/
public static @NonNull MacAddress createRandomUnicastAddress() {
- return createRandomUnicastAddress(BASE_GOOGLE_MAC, new Random());
+ SecureRandom r = new SecureRandom();
+ long addr = r.nextLong() & VALID_LONG_MASK;
+ addr |= LOCALLY_ASSIGNED_MASK;
+ addr &= ~MULTICAST_MASK;
+ return new MacAddress(addr);
}
/**
@@ -355,8 +374,8 @@ public final class MacAddress implements Parcelable {
*/
public static @NonNull MacAddress createRandomUnicastAddress(MacAddress base, Random r) {
long addr = (base.mAddr & OUI_MASK) | (NIC_MASK & r.nextLong());
- addr = addr | LOCALLY_ASSIGNED_MASK;
- addr = addr & ~MULTICAST_MASK;
+ addr |= LOCALLY_ASSIGNED_MASK;
+ addr &= ~MULTICAST_MASK;
return new MacAddress(addr);
}