Merge "MacAddress: Use SecureRandom and add a 46 bit randomized MAC generator" am: 028e2a048e am: c7ec0f6fc3

am: 83939e37af

Change-Id: I5f2d395705841b7da03b1552b16507466e47ef84
This commit is contained in:
Jong Wook Kim
2018-02-07 09:26:56 +00:00
committed by android-build-merger
2 changed files with 34 additions and 6 deletions

View File

@@ -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);
}

View File

@@ -172,7 +172,7 @@ public class MacAddressTest {
final int iterations = 1000;
final String expectedAndroidOui = "da:a1:19";
for (int i = 0; i < iterations; i++) {
MacAddress mac = MacAddress.createRandomUnicastAddress();
MacAddress mac = MacAddress.createRandomUnicastAddressWithGoogleBase();
String stringRepr = mac.toString();
assertTrue(stringRepr + " expected to be a locally assigned address",
@@ -195,6 +195,15 @@ public class MacAddressTest {
assertTrue(stringRepr + " expected to begin with " + expectedLocalOui,
stringRepr.startsWith(expectedLocalOui));
}
for (int i = 0; i < iterations; i++) {
MacAddress mac = MacAddress.createRandomUnicastAddress();
String stringRepr = mac.toString();
assertTrue(stringRepr + " expected to be a locally assigned address",
mac.isLocallyAssigned());
assertEquals(MacAddress.TYPE_UNICAST, mac.getAddressType());
}
}
@Test