Skip to content

Commit 5f491c5

Browse files
committed
update
1 parent 7364857 commit 5f491c5

File tree

6 files changed

+97
-8
lines changed

6 files changed

+97
-8
lines changed

libraries/BLEHomekit/src/HAPCharacteristic.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
271271
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_INVALID_INSTANCE_ID);
272272
}else
273273
{
274-
LOG_LV2("HAP", "Recv %s request", hap_opcode_str[hap_req->header.opcode]);
274+
LOG_LV2("HAP", "Recv %s request, TID = %02X, CS_ID = %04X", hap_opcode_str[hap_req->header.opcode], hap_req->header.tid, hap_req->header.instance_id);
275275
switch(hap_req->header.opcode)
276276
{
277277
case HAP_OPCODE_CHR_SIGNATURE_READ:
@@ -314,7 +314,7 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
314314
reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_INSUF_RESOURCES;
315315
}
316316

317-
LOG_LV2("HAP", "Response Data");
317+
LOG_LV2("HAP", "Response: Control = %02X, TID = %02X, Status = %02X", hap_resp->header.control, hap_resp->header.tid, hap_resp->header.status);
318318
LOG_LV2_BUFFER(NULL, hap_resp, reply.params.write.len);
319319
err_t err = sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
320320

libraries/BLEHomekit/src/crypto/srp/srp.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ static const uint8_t srp_N_hash_srp_G_hash[] =
6464
0x68, 0x3C, 0x9E, 0x78, 0x32, 0x96, 0xDD, 0x16, 0x93, 0xEB, 0xC7, 0x1C, 0xF5, 0xA5, 0x3D, 0xA3
6565
};
6666

67-
#define HOMEKIT_CONFIG_PINCODE "112-23-344"
68-
67+
#define HOMEKIT_CONFIG_PINCODE "111-22-333"
6968
static const uint8_t pincode[21] = "Pair-Setup:" HOMEKIT_CONFIG_PINCODE;
7069

70+
//#define HOMEKIT_CONFIG_PINCODE "password123"
71+
7172
srp_keys_t srp;
7273

7374
//static void MPI_ERROR_CHECK(int CODE)

libraries/BLEHomekit/src/service/HAPPairing.cpp

+85-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,52 @@
3737
#include <bluefruit.h>
3838
#include "HAPUuid.h"
3939
#include "HAPPairing.h"
40+
#include <Nffs.h>
4041

4142
#include "crypto/crypto.h"
4243

44+
/*
45+
The following is a description of SRP-6 and 6a, the latest versions of SRP:
46+
47+
N A large safe prime (N = 2q+1, where q is prime)
48+
All arithmetic is done modulo N.
49+
g A generator modulo N
50+
k Multiplier parameter (k = H(N, g) in SRP-6a, k = 3 for legacy SRP-6)
51+
s User's salt
52+
I Username
53+
p Cleartext Password
54+
H() One-way hash function
55+
^ (Modular) Exponentiation
56+
u Random scrambling parameter
57+
a,b Secret ephemeral values
58+
A,B Public ephemeral values
59+
x Private key (derived from p and s)
60+
v Password verifier
61+
62+
The host stores passwords using the following formula:
63+
x = H(s, p) (s is chosen randomly)
64+
v = g^x (computes password verifier)
65+
The host then keeps {I, s, v} in its password database. The authentication protocol itself goes as follows:
66+
User -> Host: I, A = g^a (identifies self, a = random number)
67+
Host -> User: s, B = kv + g^b (sends salt, b = random number)
68+
69+
Both: u = H(A, B)
70+
71+
User: x = H(s, p) (user enters password)
72+
User: S = (B - kg^x) ^ (a + ux) (computes session key)
73+
User: K = H(S)
74+
75+
Host: S = (Av^u) ^ b (computes session key)
76+
Host: K = H(S)
77+
Now the two parties have a shared, strong session key K. To complete authentication, they need to prove to each other that their keys match. One possible way:
78+
User -> Host: M = H(H(N) xor H(g), H(I), s, A, B, K)
79+
Host -> User: H(A, M, K)
80+
The two parties also employ the following safeguards:
81+
The user will abort if he receives B == 0 (mod N) or u == 0.
82+
The host will abort if it detects that A == 0 (mod N).
83+
The user must show his proof of K first. If the server detects that the user's proof is incorrect, it must abort without showing its own proof of K.
84+
*/
85+
4386
// kTLV type for pairing
4487
enum {
4588
PAIRING_TYPE_METHOD = 0,
@@ -131,11 +174,33 @@ err_t HAPPairing::begin(void)
131174
VERIFY_STATUS( _pairing.begin() );
132175

133176
// Init cryptography
177+
Nffs.mkdir_p("/adafruit/homekit");
134178
crypto_init();
135179

136180
return ERROR_NONE;
137181
}
138182

183+
HAPResponse_t* createSrpResponse(uint8_t tid, uint8_t status, TLV8_t ktlv[], uint8_t count)
184+
{
185+
HAPResponse_t* hap_resp = NULL;
186+
187+
uint16_t srplen = tlv8_calculate_encode_len(ktlv, count);
188+
uint8_t* srpbuf = (uint8_t*) rtos_malloc(srplen);
189+
VERIFY( srpbuf != NULL, NULL );
190+
191+
if( srplen == tlv8_encode_n(srpbuf, srplen, ktlv, count) )
192+
{
193+
LOG_LV2_BUFFER("PAIR-SETUP", srpbuf, srplen);
194+
TLV8_t tlv = { .type = HAP_PARAM_VALUE, .len = srplen, .value = srpbuf };
195+
hap_resp = createHapResponse(tid, status, &tlv, 1);
196+
}
197+
198+
// LOG_LV2_BUFFER("PAIR-SETUP", hap_resp, hap_resp->body_len + sizeof(HAPResponseHeader_t) + 2);
199+
200+
rtos_free(srpbuf);
201+
return hap_resp;
202+
}
203+
139204

140205
static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic* chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req)
141206
{
@@ -185,19 +250,38 @@ static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic* chr, ble_gatts_
185250
LOG_LV2("HAP", "Method %s", pairing_method_str[ *((uint8_t const*)ktlv.value) ]);
186251
break;
187252

253+
// TODO multiple pairing support
188254
case PAIRING_TYPE_STATE:
189255
{
190256
uint8_t state = *((uint8_t const*)ktlv.value);
191257
LOG_LV2("HAP", "State = M%d", state);
258+
192259
switch (state)
193260
{
194261
case 1: // M1
195262
// if paired return PAIRING_ERROR_UNAVAILABLE
196263
// tries more than 100 time return PAIRING_ERROR_MAX_TRIES
197264
// pairing with other iOS return PAIRING_ERROR_BUSY
198265

199-
break;
266+
// step 4
267+
srp_start();
200268

269+
// step 5 : username (I = "Pair-Setup"
270+
// step 6 : 16 bytes salt already created in srp_init()
271+
// step 7,8 : password (p = setup code) done in srp_init()
272+
// step 9 : public key (B) done in srp_init()
273+
274+
uint8_t mstate = 2;
275+
276+
TLV8_t tlv_para[] =
277+
{
278+
{ .type = PAIRING_TYPE_STATE , .len = 1 , .value = &mstate },
279+
{ .type = PAIRING_TYPE_PUBLIC_KEY , .len = 384, .value = srp_getB() },
280+
{ .type = PAIRING_TYPE_SALT , .len = 16 , .value = srp_getSalt() },
281+
};
282+
283+
hap_resp = createSrpResponse(hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
284+
break;
201285
}
202286
}
203287
break;

libraries/Bluefruit52Lib/examples/Projects/homekit/homekit_lightbulb/homekit_lightbulb.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
any redistribution
1313
*********************************************************************/
1414

15+
#include <Nffs.h>
1516
#include <bluefruit.h>
1617
#include <BLEHomekit.h>
1718

@@ -38,12 +39,11 @@ void setup()
3839
Bluefruit.setTxPower(4);
3940
Bluefruit.setName("Bluefruit52");
4041

42+
Nffs.listDir("/adafruit/homekit");
4143
homekit.begin();
4244

4345
// Set up and start advertising
4446
startAdv();
45-
46-
//dbgMemInfo();
4747
}
4848

4949
void startAdv(void)

platform.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ recipe.output.save_file={build.project_name}.{build.variant}.hex
121121

122122
#**********************************************
123123
# nrfutil for uploading
124+
# Note : For MacOS, make a symlink to /usr/local/bin/nrfutil if installed to other location
124125
#**********************************************
125126

126127
tools.nrfutil.cmd=nrfutil
127128
tools.nrfutil.cmd.windows={runtime.platform.path}/tools/nrfutil-0.5.2/binaries/win32/nrfutil.exe
128-
# MacOS nrfutil location, make a symlink if installed to other location
129129
tools.nrfutil.cmd.macosx=/usr/local/bin/nrfutil
130130

131131
tools.nrfutil.upload.params.verbose=--verbose

programmers.txt

+4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ nrfjprog.program.tool=bootburn
77
nrfjprog.program.path=
88
nrfjprog.program.cmd=nrfjprog
99
nrfjprog.program.cmd.windows=nrfjprog.exe
10+
1011
nrfjprog.program.pattern="{program.path}{program.cmd}" --program "{runtime.platform.path}/bin/bootloader/{build.variant}/{build.sd_version}/{build.bootfile}.hex" -f nrf52 --chiperase --reset
1112

1213
#**********************************************
1314
# nrfutil to upgrade bootloader
15+
# # Note : For MacOS, make a symlink to /usr/local/bin/nrfutil if installed to other location
1416
#**********************************************
1517
nrfutil_boot.name=Bootloader DFU for Bluefruit nRF52
1618
nrfutil_boot.program.tool=bootburn
1719
nrfutil_boot.program.cmd=nrfutil
1820
nrfutil_boot.program.cmd.windows={runtime.platform.path}/tools/nrfutil-0.5.2/binaries/win32/nrfutil.exe
21+
nrfutil_boot.program.cmd.macosx=/usr/local/bin/nrfutil
22+
1923
nrfutil_boot.program.pattern={program.cmd} --verbose dfu serial -pkg "{runtime.platform.path}/bin/bootloader/{build.variant}/{build.sd_version}/{build.bootfile}.zip" -p {serial.port} -b 115200

0 commit comments

Comments
 (0)