blob: 6ad718430cee99b6a3aa5eb476de17a097c9c68b [file] [log] [blame]
Joseph Pirozzoee3f7132021-03-08 13:05:18 -08001/*
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 */
16package com.android.car.bluetooth;
17
18import android.content.Context;
19import android.content.SharedPreferences;
20import android.util.Log;
21
22import java.math.BigInteger;
23import java.nio.ByteBuffer;
24import java.security.MessageDigest;
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.List;
28import java.util.Random;
29
30import javax.crypto.spec.SecretKeySpec;
31
32class 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";
Sal Savage60e6b3d2021-06-30 15:29:49 -070037 static final String THREAD_NAME = "FastPairProvider";
Joseph Pirozzoee3f7132021-03-08 13:05:18 -080038
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
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
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
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
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
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
137 AccountKey(byte[] newKey) {
138 key = newKey;
139 }
140
141 AccountKey(String newKey) {
142 key = new BigInteger(newKey).toByteArray();
143 }
144
145 public byte[] toBytes() {
146 return key;
147 }
148
149 public SecretKeySpec getKeySpec() {
150 return new SecretKeySpec(key, "AES");
151 }
152
153 @Override
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}