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     static final String THREAD_NAME = "FastPairProvider";
38 
39     private static final byte SALT_FIELD_DESCRIPTOR = 0x11;
40     private static final int BD_ADDR_LEN = 6;
41     private static final int BD_UUID_LEN = 16;
42 
43     //construct the advertisement based on stored account keys
getAccountKeyAdvertisement(Context context)44     static byte[] getAccountKeyAdvertisement(Context context) {
45         byte[] salt = new byte[1];
46         List<FastPairUtils.AccountKey> keys = new ArrayList<>();
47         new Random().nextBytes(salt);
48         keys = readStoredAccountKeys(context);
49 
50         //calculate bloom results
51         byte[] bloomResults = bloom(keys, salt[0]);
52         int size = bloomResults.length;
53 
54         //assemble advertisement
55         ByteBuffer accountKeyAdvertisement = ByteBuffer.allocate(size + 4);
56         accountKeyAdvertisement.put((byte) 0); //reserved Flags byte
57         accountKeyAdvertisement.put((byte) (size << 4));
58         accountKeyAdvertisement.put(bloomResults);
59         accountKeyAdvertisement.put(SALT_FIELD_DESCRIPTOR);
60         accountKeyAdvertisement.put(salt);
61 
62         return accountKeyAdvertisement.array();
63     }
64 
65     //given a list of account keys and a salt, calculate the bloom results
bloom(List<AccountKey> keys, byte salt)66     static byte[] bloom(List<AccountKey> keys, byte salt) {
67         int size = (int) 1.2 * keys.size() + 3;
68         byte[] filter = new byte[size];
69 
70         for (AccountKey key : keys) {
71             byte[] v = Arrays.copyOf(key.key, 17);
72             v[16] = salt;
73             try {
74                 byte[] hashed = MessageDigest.getInstance("SHA-256").digest(v);
75                 ByteBuffer byteBuffer = ByteBuffer.wrap(hashed);
76                 for (int j = 0; j < 8; j++) {
77                     long k = Integer.toUnsignedLong(byteBuffer.getInt()) % (size * 8);
78                     filter[(int) (k / 8)] |= 1 << (k % 8);
79                 }
80             } catch (Exception e) {
81                 Log.e(TAG, e.toString());
82             }
83         }
84         return filter;
85     }
86 
readStoredAccountKeys(Context context)87     static List<AccountKey> readStoredAccountKeys(Context context) {
88         List<AccountKey> keys = new ArrayList<>();
89         SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCES,
90                 Context.MODE_PRIVATE);
91         int accountKeyCount = sharedPref.getInt(ACCOUNT_KEYS, 0);
92 
93         for (int i = 1; i <= accountKeyCount; i++) {
94             String readAccountKey = sharedPref.getString("" + i, null);
95             if (readAccountKey != null) {
96                 keys.add(new FastPairUtils.AccountKey(readAccountKey));
97             } else {
98                 Log.w(TAG, "Read account key == " + readAccountKey);
99             }
100         }
101 
102         Log.d(TAG, "Read " + keys.size() + "/" + accountKeyCount + " keys.");
103         return keys;
104     }
105 
writeStoredAccountKeys(Context context, List<AccountKey> keys)106     static void writeStoredAccountKeys(Context context, List<AccountKey> keys) {
107         SharedPreferences sharedPref = context
108                 .getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
109         int accountKeyCount = keys.size();
110         SharedPreferences.Editor editor = sharedPref.edit();
111         for (int i = 0; i < accountKeyCount; i++) {
112             editor.putString("" + accountKeyCount,
113                     new BigInteger(keys.get(i).toBytes()).toString());
114         }
115         editor.putInt(ACCOUNT_KEYS, accountKeyCount);
116         editor.apply();
117     }
118 
getBytesFromAddress(String address)119     static byte[] getBytesFromAddress(String address) {
120         int i, j = 0;
121         byte[] output = new byte[BD_ADDR_LEN];
122 
123         for (i = 0; i < address.length(); i++) {
124             if (address.charAt(i) != ':') {
125                 output[j] = (byte) Integer.parseInt(address.substring(i, i + 2), BD_UUID_LEN);
126                 j++;
127                 i++;
128             }
129         }
130         return output;
131     }
132 
133     static class AccountKey {
134 
135         public final byte[] key;
136 
AccountKey(byte[] newKey)137         AccountKey(byte[] newKey) {
138             key = newKey;
139         }
140 
AccountKey(String newKey)141         AccountKey(String newKey) {
142             key = new BigInteger(newKey).toByteArray();
143         }
144 
toBytes()145         public byte[] toBytes() {
146             return key;
147         }
148 
getKeySpec()149         public SecretKeySpec getKeySpec() {
150             return new SecretKeySpec(key, "AES");
151         }
152 
153         @Override
equals(Object obj)154         public boolean equals(Object obj) {
155             if (!(obj instanceof AccountKey)) {
156                 return false;
157             }
158             return Arrays.equals(key, ((AccountKey) obj).key);
159         }
160     }
161 }
162