Joseph Pirozzo | ee3f713 | 2021-03-08 13:05:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.car.bluetooth; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.SharedPreferences; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import java.math.BigInteger; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.security.MessageDigest; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.List; |
| 28 | import java.util.Random; |
| 29 | |
| 30 | import javax.crypto.spec.SecretKeySpec; |
| 31 | |
| 32 | class FastPairUtils { |
| 33 | static final String TAG = FastPairUtils.class.getSimpleName(); |
| 34 | static final boolean DBG = Log.isLoggable("FastPair", Log.DEBUG); |
| 35 | static final String PREFERENCES = "com.android.car.bluetooth"; |
| 36 | static final String ACCOUNT_KEYS = "AccountKeysCount"; |
| 37 | |
| 38 | private static final byte SALT_FIELD_DESCRIPTOR = 0x11; |
| 39 | private static final int BD_ADDR_LEN = 6; |
| 40 | private static final int BD_UUID_LEN = 16; |
| 41 | |
| 42 | //construct the advertisement based on stored account keys |
| 43 | static byte[] getAccountKeyAdvertisement(Context context) { |
| 44 | byte[] salt = new byte[1]; |
| 45 | List<FastPairUtils.AccountKey> keys = new ArrayList<>(); |
| 46 | new Random().nextBytes(salt); |
| 47 | keys = readStoredAccountKeys(context); |
| 48 | |
| 49 | //calculate bloom results |
| 50 | byte[] bloomResults = bloom(keys, salt[0]); |
| 51 | int size = bloomResults.length; |
| 52 | |
| 53 | //assemble advertisement |
| 54 | ByteBuffer accountKeyAdvertisement = ByteBuffer.allocate(size + 4); |
| 55 | accountKeyAdvertisement.put((byte) 0); //reserved Flags byte |
| 56 | accountKeyAdvertisement.put((byte) (size << 4)); |
| 57 | accountKeyAdvertisement.put(bloomResults); |
| 58 | accountKeyAdvertisement.put(SALT_FIELD_DESCRIPTOR); |
| 59 | accountKeyAdvertisement.put(salt); |
| 60 | |
| 61 | return accountKeyAdvertisement.array(); |
| 62 | } |
| 63 | |
| 64 | //given a list of account keys and a salt, calculate the bloom results |
| 65 | static byte[] bloom(List<AccountKey> keys, byte salt) { |
| 66 | int size = (int) 1.2 * keys.size() + 3; |
| 67 | byte[] filter = new byte[size]; |
| 68 | |
| 69 | for (AccountKey key : keys) { |
| 70 | byte[] v = Arrays.copyOf(key.key, 17); |
| 71 | v[16] = salt; |
| 72 | try { |
| 73 | byte[] hashed = MessageDigest.getInstance("SHA-256").digest(v); |
| 74 | ByteBuffer byteBuffer = ByteBuffer.wrap(hashed); |
| 75 | for (int j = 0; j < 8; j++) { |
| 76 | long k = Integer.toUnsignedLong(byteBuffer.getInt()) % (size * 8); |
| 77 | filter[(int) (k / 8)] |= 1 << (k % 8); |
| 78 | } |
| 79 | } catch (Exception e) { |
| 80 | Log.e(TAG, e.toString()); |
| 81 | } |
| 82 | } |
| 83 | return filter; |
| 84 | } |
| 85 | |
| 86 | static List<AccountKey> readStoredAccountKeys(Context context) { |
| 87 | List<AccountKey> keys = new ArrayList<>(); |
| 88 | SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCES, |
| 89 | Context.MODE_PRIVATE); |
| 90 | int accountKeyCount = sharedPref.getInt(ACCOUNT_KEYS, 0); |
| 91 | |
| 92 | for (int i = 1; i <= accountKeyCount; i++) { |
| 93 | String readAccountKey = sharedPref.getString("" + i, null); |
| 94 | if (readAccountKey != null) { |
| 95 | keys.add(new FastPairUtils.AccountKey(readAccountKey)); |
| 96 | } else { |
| 97 | Log.w(TAG, "Read account key == " + readAccountKey); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | Log.d(TAG, "Read " + keys.size() + "/" + accountKeyCount + " keys."); |
| 102 | return keys; |
| 103 | } |
| 104 | |
| 105 | static void writeStoredAccountKeys(Context context, List<AccountKey> keys) { |
| 106 | SharedPreferences sharedPref = context |
| 107 | .getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE); |
| 108 | int accountKeyCount = keys.size(); |
| 109 | SharedPreferences.Editor editor = sharedPref.edit(); |
| 110 | for (int i = 0; i < accountKeyCount; i++) { |
| 111 | editor.putString("" + accountKeyCount, |
| 112 | new BigInteger(keys.get(i).toBytes()).toString()); |
| 113 | } |
| 114 | editor.putInt(ACCOUNT_KEYS, accountKeyCount); |
| 115 | editor.apply(); |
| 116 | } |
| 117 | |
| 118 | static byte[] getBytesFromAddress(String address) { |
| 119 | int i, j = 0; |
| 120 | byte[] output = new byte[BD_ADDR_LEN]; |
| 121 | |
| 122 | for (i = 0; i < address.length(); i++) { |
| 123 | if (address.charAt(i) != ':') { |
| 124 | output[j] = (byte) Integer.parseInt(address.substring(i, i + 2), BD_UUID_LEN); |
| 125 | j++; |
| 126 | i++; |
| 127 | } |
| 128 | } |
| 129 | return output; |
| 130 | } |
| 131 | |
| 132 | static class AccountKey { |
| 133 | |
| 134 | public final byte[] key; |
| 135 | |
| 136 | AccountKey(byte[] newKey) { |
| 137 | key = newKey; |
| 138 | } |
| 139 | |
| 140 | AccountKey(String newKey) { |
| 141 | key = new BigInteger(newKey).toByteArray(); |
| 142 | } |
| 143 | |
| 144 | public byte[] toBytes() { |
| 145 | return key; |
| 146 | } |
| 147 | |
| 148 | public SecretKeySpec getKeySpec() { |
| 149 | return new SecretKeySpec(key, "AES"); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean equals(Object obj) { |
| 154 | if (!(obj instanceof AccountKey)) { |
| 155 | return false; |
| 156 | } |
| 157 | return Arrays.equals(key, ((AccountKey) obj).key); |
| 158 | } |
| 159 | } |
| 160 | } |