Merge branch 'master' into master
This commit is contained in:
700 files changed
+212468
-96144
No files matched your search
@@ -0,0 +1,11 @@
|
||||
# Thanks
|
||||
|
||||
I want to thank a number of projects which made this possible:
|
||||
|
||||
1. https://github.com/aanon4/HomeKit - which provides reference implementation of homekit security on nrf5x
|
||||
2. http://tweetnacl.cr.yp.to - which provides the compact eliptical curve implementations, as well as the sha512 hash.
|
||||
3. https://github.com/ARMmbed/mbedtls - which provides the core multi-precission math routines used in the SRP implementation.
|
||||
4. http://munacl.cryptojedi.org/ - which provides the ARM Cortex-M0 optimized Curve25519 implementation and fast multiply routines.
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name=Apple Homekit Libraries for Bluefruit52
|
||||
version=0.8.0
|
||||
version=0.9.0
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEHomekit.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -37,8 +37,6 @@
|
||||
#include <bluefruit.h>
|
||||
#include "BLEHomekit.h"
|
||||
|
||||
#define HOMEKIT_PROTOCOL_VERION 2
|
||||
|
||||
uint16_t BLEHomekit::_gInstanceID = 1;
|
||||
|
||||
BLEHomekit::BLEHomekit()
|
||||
@@ -95,18 +93,18 @@ bool BLEHomekit::setAdv(BLEAdvertisingData& adv_ref)
|
||||
uint16_t category;
|
||||
uint16_t gsn;
|
||||
uint8_t config_num;
|
||||
uint8_t compatible_verion;
|
||||
uint8_t compatible_version;
|
||||
}data =
|
||||
{
|
||||
.company_id = UUID16_COMPANY_ID_APPLE,
|
||||
.type = 0x06,
|
||||
.adv_int_len = ail,
|
||||
.status_flag = 1, // bit0 = HAP Pairing Status Flag (1 not paired)
|
||||
.dev_id = { 0x00},
|
||||
.category = HAP_CAT_LIGHTBULB,
|
||||
.gsn = 1,
|
||||
.config_num = 1,
|
||||
.compatible_verion = HOMEKIT_PROTOCOL_VERION
|
||||
.company_id = UUID16_COMPANY_ID_APPLE,
|
||||
.type = 0x06,
|
||||
.adv_int_len = ail,
|
||||
.status_flag = 1, // bit0 = HAP Pairing Status Flag (1 not paired)
|
||||
.dev_id = { 0x00},
|
||||
.category = HAP_CAT_LIGHTBULB,
|
||||
.gsn = 1,
|
||||
.config_num = 1,
|
||||
.compatible_version = 2 // protocol version
|
||||
};
|
||||
|
||||
Bluefruit.Gap.getAddr(data.dev_id);
|
||||
@@ -114,6 +112,15 @@ bool BLEHomekit::setAdv(BLEAdvertisingData& adv_ref)
|
||||
VERIFY_STATIC( sizeof(data) == 17);
|
||||
|
||||
adv.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
|
||||
return adv.addManufacturerData(&data, sizeof(data));
|
||||
adv.addManufacturerData(&data, sizeof(data));
|
||||
|
||||
// Full name is not added, added full name to scan response
|
||||
if ( !adv.addName() )
|
||||
{
|
||||
Bluefruit.ScanResponse.clearData();
|
||||
Bluefruit.ScanResponse.addName();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEHomekit.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -87,6 +87,10 @@ class BLEHomekit : public Advertisable
|
||||
HAPPairing _pairing;
|
||||
|
||||
HAPLightBulb _lightbulb;
|
||||
|
||||
#if CFG_DEBUG
|
||||
friend void test_homekit(void);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif /* BLEHOMEKIT_H_ */
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPCharacteristic.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -56,18 +56,25 @@ BLEUuid HAPCharacteristic::_g_uuid_cid(HAP_UUID_DSC_CHARACTERISTIC_ID);
|
||||
HAPCharacteristic::HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit)
|
||||
: BLECharacteristic(bleuuid)
|
||||
{
|
||||
_cid = 0;
|
||||
_hap_props = 0;
|
||||
_cid = 0;
|
||||
_hap_props = 0;
|
||||
|
||||
_value = NULL;
|
||||
_vallen = 0;
|
||||
_value = NULL;
|
||||
_vallen = 0;
|
||||
|
||||
_resp_len = 0;
|
||||
_hap_req = NULL;
|
||||
_hap_reqlen = 0;
|
||||
_tid = 0;
|
||||
|
||||
_hap_wr_cb = NULL;
|
||||
_hap_resp = NULL;
|
||||
_hap_resplen = 0;
|
||||
_hap_resplen_sent = 0;
|
||||
|
||||
_hap_wr_cb = NULL;
|
||||
_hap_rd_cb = NULL;
|
||||
|
||||
// Need at least decent length for HAP Procedure
|
||||
_max_len = 100;
|
||||
_max_len = 64;
|
||||
|
||||
setPresentationFormatDescriptor(format, 0, unit, 1, 0);
|
||||
}
|
||||
@@ -76,7 +83,9 @@ void HAPCharacteristic::setHapProperties(uint16_t prop)
|
||||
{
|
||||
_hap_props = prop;
|
||||
|
||||
// HAP always require read and write
|
||||
uint8_t chrprop = CHR_PROPS_READ | CHR_PROPS_WRITE;
|
||||
|
||||
if ((prop & HAP_CHR_PROPS_NOTIFY) || (prop & HAP_CHR_PROPS_NOTIFY_DISCONNECTED)) chrprop |= CHR_PROPS_INDICATE;
|
||||
setProperties(chrprop);
|
||||
|
||||
@@ -88,6 +97,11 @@ void HAPCharacteristic::setHapWriteCallback(hap_write_cb_t fp)
|
||||
_hap_wr_cb = fp;
|
||||
}
|
||||
|
||||
void HAPCharacteristic::setHapReadCallback(hap_read_cb_t fp)
|
||||
{
|
||||
_hap_rd_cb = fp;
|
||||
}
|
||||
|
||||
/**
|
||||
* GATT Descriptor includes: Instance ID
|
||||
* HAP Descriptor includes: HAP Properties, User String, Presentation Format,
|
||||
@@ -109,9 +123,6 @@ err_t HAPCharacteristic::begin(void)
|
||||
const char* temp_usr = _usr_descriptor;
|
||||
_usr_descriptor = NULL;
|
||||
|
||||
// FIXME temporary
|
||||
_max_len = minof(_max_len, 100);
|
||||
|
||||
VERIFY_STATUS( BLECharacteristic::begin() );
|
||||
VERIFY_STATUS( _addChrIdDescriptor() );
|
||||
|
||||
@@ -119,6 +130,8 @@ err_t HAPCharacteristic::begin(void)
|
||||
_format_desc.format = temp_format;
|
||||
_usr_descriptor = temp_usr;
|
||||
|
||||
LOG_LV2("HAP", "Added Char UUID=0x%04X, CID=0x%04X", uuid._uuid.uuid, _cid);
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
@@ -180,41 +193,70 @@ uint16_t HAPCharacteristic::writeHapValue(uint32_t num)
|
||||
return writeHapValue(&num, len);
|
||||
}
|
||||
|
||||
HAPResponse_t* HAPCharacteristic::createHapResponse(uint8_t tid, uint8_t status, TLV8_t tlv_para[], uint8_t count)
|
||||
void HAPCharacteristic::createHapResponse(uint16_t conn_hdl, uint8_t status, TLV8_t tlv_para[], uint8_t count)
|
||||
{
|
||||
// Determine body len, does not to include 2 byte length itself
|
||||
uint8_t body_len = 0;
|
||||
for(uint8_t i=0; i <count ; i++)
|
||||
// Determine body len (not including 2 byte length itself)
|
||||
uint16_t body_len = tlv8_encode_calculate_len(tlv_para, count);
|
||||
|
||||
// Determine the response length including fragmentation scheme
|
||||
uint16_t const gatt_mtu = Bluefruit.Gap.getMTU(conn_hdl) - 3;
|
||||
uint16_t resp_len = sizeof(HAPResponseHeader_t) + 2 + body_len;
|
||||
|
||||
uint16_t nfrag = 0; // number of fragments (excluding the first)
|
||||
uint16_t const fraglen = gatt_mtu-2;
|
||||
|
||||
// fragmentation required
|
||||
if (resp_len > gatt_mtu)
|
||||
{
|
||||
body_len += tlv_para[i].len + 2;
|
||||
// calculate number of fragment. From 2nd frag, there is 2 extra byte (control + tid)
|
||||
nfrag = (resp_len-gatt_mtu) / fraglen;
|
||||
if ( (resp_len-gatt_mtu) % fraglen ) nfrag++;
|
||||
}
|
||||
|
||||
HAPResponse_t* hap_resp = (HAPResponse_t*) rtos_malloc(sizeof(HAPResponseHeader_t) + 2 + body_len);
|
||||
VERIFY( hap_resp != NULL, NULL );
|
||||
HAPResponse_t* hap_resp = (HAPResponse_t*) rtos_malloc(resp_len + nfrag*2);
|
||||
VERIFY( hap_resp != NULL, );
|
||||
|
||||
/*------------- Header -------------*/
|
||||
varclr(&hap_resp->header.control);
|
||||
hap_resp->header.control.fragment = 0;
|
||||
hap_resp->header.control.type = HAP_PDU_RESPONSE;
|
||||
hap_resp->header.tid = tid;
|
||||
hap_resp->header.tid = _tid;
|
||||
hap_resp->header.status = status;
|
||||
hap_resp->body_len = body_len;
|
||||
|
||||
/*------------- Serialize Data -------------*/
|
||||
uint8_t* pdata = hap_resp->body_data;
|
||||
for(uint8_t i=0; i < count; i++)
|
||||
{
|
||||
memcpy(pdata, &tlv_para[i], 2); // type + len
|
||||
pdata += 2;
|
||||
tlv8_encode_n(hap_resp->body_data, body_len, tlv_para, count);
|
||||
|
||||
memcpy(pdata, tlv_para[i].value, tlv_para[i].len);
|
||||
pdata += tlv_para[i].len;
|
||||
/*------------- Fragmentation -------------*/
|
||||
if (nfrag)
|
||||
{
|
||||
HAPControl_t ctrl = hap_resp->header.control;
|
||||
ctrl.fragment = 1;
|
||||
|
||||
for(int i=nfrag-1; i >= 0; i--)
|
||||
{
|
||||
uint16_t const idx = gatt_mtu + i*fraglen;
|
||||
uint8_t* src = ((uint8_t*)hap_resp) + idx;
|
||||
uint8_t* dst = src + 2*i;
|
||||
|
||||
// right shift 2 byte for fragment scheme
|
||||
uint16_t cc = (i == nfrag-1) ? (resp_len - idx) : fraglen;
|
||||
|
||||
memmove(dst+2, src, cc);
|
||||
|
||||
memcpy(&dst[0], &ctrl, 1);
|
||||
dst[1] = _tid;
|
||||
}
|
||||
|
||||
resp_len += nfrag*2;
|
||||
}
|
||||
|
||||
return hap_resp;
|
||||
_hap_resp = hap_resp;
|
||||
_hap_resplen = resp_len;
|
||||
_hap_resplen_sent = 0;
|
||||
}
|
||||
|
||||
HAPResponse_t* HAPCharacteristic::processChrSignatureRead(HAPRequest_t* hap_req)
|
||||
void HAPCharacteristic::processChrSignatureRead(uint16_t conn_hdl, HAPRequest_t* hap_req)
|
||||
{
|
||||
uint16_t svc_id = ((HAPService*)_service)->getSvcId();
|
||||
|
||||
@@ -251,14 +293,226 @@ HAPResponse_t* HAPCharacteristic::processChrSignatureRead(HAPRequest_t* hap_req)
|
||||
{ .type = HAP_PARAM_GATT_FORMAT_DESC , .len = sizeof(fmt_desc) , .value = &fmt_desc },
|
||||
};
|
||||
|
||||
return createHapResponse(hap_req->header.tid, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
createHapResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
}
|
||||
|
||||
HAPResponse_t* HAPCharacteristic::processChrRead(HAPRequest_t* hap_req)
|
||||
void HAPCharacteristic::processChrRead(uint16_t conn_hdl, HAPRequest_t* hap_req)
|
||||
{
|
||||
TLV8_t tlv_para = { .type = HAP_PARAM_VALUE, .len = _vallen, .value = _value };
|
||||
|
||||
return createHapResponse(hap_req->header.tid, HAP_STATUS_SUCCESS, &tlv_para, 1);
|
||||
createHapResponse(conn_hdl, HAP_STATUS_SUCCESS, &tlv_para, 1);
|
||||
}
|
||||
|
||||
void HAPCharacteristic::processHapRequest(uint16_t conn_hdl, HAPRequest_t* hap_req)
|
||||
{
|
||||
if (hap_req->header.control.type != HAP_PDU_REQUEST)
|
||||
{
|
||||
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
|
||||
}
|
||||
else if (hap_req->header.instance_id != _cid)
|
||||
{
|
||||
createHapResponse(conn_hdl, HAP_STATUS_INVALID_INSTANCE_ID);
|
||||
}else
|
||||
{
|
||||
LOG_LV2("HAP", "Recv %s request, TID = 0x%02X, CS_ID = 0x%04X", hap_opcode_str[hap_req->header.opcode], hap_req->header.tid, hap_req->header.instance_id);
|
||||
switch(hap_req->header.opcode)
|
||||
{
|
||||
case HAP_OPCODE_CHR_SIGNATURE_READ:
|
||||
processChrSignatureRead(conn_hdl, hap_req);
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_READ:
|
||||
processChrRead(conn_hdl, hap_req);
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_WRITE:
|
||||
if ( _hap_wr_cb )
|
||||
{
|
||||
_hap_wr_cb(conn_hdl, this, hap_req);
|
||||
}else
|
||||
{
|
||||
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
|
||||
}
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_TIMED_WRITE:
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_EXECUTE_WRITE:
|
||||
break;
|
||||
|
||||
// need seperated chr for service
|
||||
// case HAP_OPCODE_SVC_SIGNATURE_READ: break;
|
||||
|
||||
default:
|
||||
createHapResponse(conn_hdl, HAP_STATUS_UNSUPPORTED_PDU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HAPCharacteristic::processGattWrite(uint16_t conn_hdl, ble_gatts_evt_write_t* gatt_req)
|
||||
{
|
||||
LOG_LV2("GATTS", "Write Op = %d, len = %d, offset = %d, uuid = 0x%04X", gatt_req->op, gatt_req->len, gatt_req->offset, gatt_req->uuid.uuid);
|
||||
LOG_LV2_BUFFER(NULL, gatt_req->data, gatt_req->len);
|
||||
|
||||
uint16_t const gatt_mtu = Bluefruit.Gap.getMTU(conn_hdl) - 3;
|
||||
|
||||
ble_gatts_rw_authorize_reply_params_t reply =
|
||||
{
|
||||
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
|
||||
.params = {
|
||||
.write = {
|
||||
.gatt_status = BLE_GATT_STATUS_SUCCESS,
|
||||
.update = 1,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// new request
|
||||
if ( _hap_req == NULL )
|
||||
{
|
||||
uint16_t reqlen = sizeof(HAPRequestHeader_t);
|
||||
|
||||
// if body is present
|
||||
if ( gatt_req->len > reqlen )
|
||||
{
|
||||
reqlen += 2 + ((HAPRequest_t*) gatt_req->data)->body_len;
|
||||
}
|
||||
|
||||
// request len is larger than MTU --> buffer assembly required
|
||||
if ( reqlen > gatt_mtu )
|
||||
{
|
||||
_hap_req = (HAPRequest_t*) rtos_malloc( reqlen );
|
||||
}
|
||||
}
|
||||
|
||||
// Assembly fragmentation request if needed
|
||||
if ( !_hap_req )
|
||||
{
|
||||
// no fragment
|
||||
_hap_req = (HAPRequest_t*) gatt_req->data;
|
||||
_hap_reqlen = gatt_req->len;
|
||||
} else
|
||||
{
|
||||
// default to 1st fragment
|
||||
uint8_t* fragdata = gatt_req->data;
|
||||
uint16_t fraglen = gatt_req->len;
|
||||
|
||||
// 2nd and later Fragment
|
||||
if (gatt_req->data[0] & 0x80)
|
||||
{
|
||||
// match TID
|
||||
if ( _hap_req->header.tid == gatt_req->data[1] )
|
||||
{
|
||||
// copy data sip control & tid
|
||||
fragdata = gatt_req->data+2;
|
||||
fraglen = gatt_req->len-2;
|
||||
}else
|
||||
{
|
||||
// Data corruption, unlikely to happen
|
||||
LOG_LV2("HAP", "Fragment TID not match");
|
||||
|
||||
// clean up
|
||||
rtos_free(_hap_req);
|
||||
_hap_req = NULL;
|
||||
_hap_reqlen = 0;
|
||||
|
||||
// reject request
|
||||
reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_UNLIKELY_ERROR;
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Assembly data
|
||||
memcpy(((uint8_t*)_hap_req) +_hap_reqlen, fragdata, fraglen);
|
||||
_hap_reqlen += fraglen;
|
||||
}
|
||||
|
||||
// process when full request is received
|
||||
if ( (_hap_reqlen == sizeof(HAPRequestHeader_t)) ||
|
||||
(_hap_reqlen == _hap_req->body_len + sizeof(HAPRequestHeader_t) + 2) )
|
||||
{
|
||||
LOG_LV2_BUFFER("HAP Request", _hap_req, _hap_reqlen);
|
||||
|
||||
// save tid
|
||||
_tid = _hap_req->header.tid;
|
||||
|
||||
processHapRequest(conn_hdl, _hap_req);
|
||||
|
||||
if (_hap_resp )
|
||||
{
|
||||
LOG_LV2("HAP", "Response: Control = 0x%02X, TID = 0x%02X, Status = %d", _hap_resp->header.control, _hap_resp->header.tid, _hap_resp->header.status);
|
||||
LOG_LV2_BUFFER(NULL, _hap_resp, _hap_resplen);
|
||||
}
|
||||
|
||||
// Free if needed
|
||||
if ( _hap_reqlen > gatt_mtu ) rtos_free(_hap_req);
|
||||
_hap_req = NULL;
|
||||
_hap_reqlen = 0;
|
||||
}
|
||||
|
||||
// Reply before processing request since cryptography take time and could cause timeout
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
|
||||
}
|
||||
|
||||
void HAPCharacteristic::processGattRead(uint16_t conn_hdl, ble_gatts_evt_read_t* gatt_req)
|
||||
{
|
||||
uint16_t const gatt_mtu = Bluefruit.Gap.getMTU(conn_hdl) - 3;
|
||||
|
||||
LOG_LV2("GATTS", "Read uuid = 0x%04X, offset = %d", gatt_req->uuid.uuid, gatt_req->offset);
|
||||
|
||||
ble_gatts_rw_authorize_reply_params_t reply =
|
||||
{
|
||||
.type = BLE_GATTS_AUTHORIZE_TYPE_READ,
|
||||
.params = {
|
||||
.read = {
|
||||
.gatt_status = BLE_GATT_STATUS_SUCCESS,
|
||||
.update = 1,
|
||||
.offset = 0,
|
||||
.len = 0,
|
||||
.p_data = NULL
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// If Response has not been generated, call the read callback
|
||||
if ( _hap_resp == NULL && _hap_resplen == 0 )
|
||||
{
|
||||
if ( _hap_rd_cb )
|
||||
{
|
||||
_hap_rd_cb(conn_hdl, this);
|
||||
}
|
||||
}
|
||||
|
||||
if (_hap_resplen_sent < _hap_resplen)
|
||||
{
|
||||
uint16_t len = min16(_hap_resplen - _hap_resplen_sent, gatt_mtu);
|
||||
|
||||
reply.params.read.len = len;
|
||||
reply.params.read.p_data = ((uint8_t*)_hap_resp) + _hap_resplen_sent;
|
||||
|
||||
LOG_LV2_BUFFER(NULL, reply.params.read.p_data, len);
|
||||
|
||||
err_t err = sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
||||
|
||||
_hap_resplen_sent += len;
|
||||
|
||||
if ( _hap_resplen == _hap_resplen_sent )
|
||||
{
|
||||
rtos_free(_hap_resp);
|
||||
_hap_resp = NULL;
|
||||
_hap_resplen = _hap_resplen_sent = 0;
|
||||
}
|
||||
|
||||
VERIFY_STATUS(err, );
|
||||
}else
|
||||
{
|
||||
// reject read attempt
|
||||
reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
|
||||
}
|
||||
}
|
||||
|
||||
void HAPCharacteristic::_eventHandler(ble_evt_t* event)
|
||||
@@ -269,122 +523,16 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
|
||||
{
|
||||
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
|
||||
{
|
||||
/*------------- Handle HAP Request -------------*/
|
||||
// HAP Request
|
||||
if (event->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
|
||||
{
|
||||
ble_gatts_evt_write_t * gatt_req = &event->evt.gatts_evt.params.authorize_request.request.write;
|
||||
|
||||
LOG_LV2("GATTS", "Write Op = %d, uuid = 0x%04X", gatt_req->op, gatt_req->uuid.uuid);
|
||||
LOG_LV2_BUFFER(NULL, gatt_req->data, gatt_req->len);
|
||||
|
||||
HAPRequest_t* hap_req = (HAPRequest_t*) gatt_req->data;
|
||||
HAPResponse_t* hap_resp = NULL;
|
||||
|
||||
ble_gatts_rw_authorize_reply_params_t reply =
|
||||
{
|
||||
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
|
||||
.params = {
|
||||
.write = {
|
||||
.gatt_status = BLE_GATT_STATUS_SUCCESS,
|
||||
.update = 1,
|
||||
.offset = 0,
|
||||
.len = 0,
|
||||
.p_data = NULL
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (hap_req->header.control.type != HAP_PDU_REQUEST)
|
||||
{
|
||||
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
|
||||
}
|
||||
else if (hap_req->header.instance_id != _cid)
|
||||
{
|
||||
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_INVALID_INSTANCE_ID);
|
||||
}else
|
||||
{
|
||||
LOG_LV2("HAP", "Recv %s request", hap_opcode_str[hap_req->header.opcode]);
|
||||
switch(hap_req->header.opcode)
|
||||
{
|
||||
case HAP_OPCODE_CHR_SIGNATURE_READ:
|
||||
hap_resp = processChrSignatureRead(hap_req);
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_WRITE:
|
||||
if (_hap_wr_cb)
|
||||
{
|
||||
hap_resp = _hap_wr_cb(*this, gatt_req, hap_req);
|
||||
}
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_READ:
|
||||
hap_resp = processChrRead(hap_req);
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_TIMED_WRITE:
|
||||
break;
|
||||
|
||||
case HAP_OPCODE_CHR_EXECUTE_WRITE:
|
||||
break;
|
||||
|
||||
// need seperated chr for service
|
||||
// case HAP_OPCODE_SVC_SIGNATURE_READ: break;
|
||||
|
||||
default:
|
||||
hap_resp = createHapResponse(hap_req->header.tid, HAP_STATUS_UNSUPPORTED_PDU);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( hap_resp )
|
||||
{
|
||||
reply.params.write.len = _resp_len = sizeof(HAPResponseHeader_t) + 2 + hap_resp->body_len;
|
||||
reply.params.write.p_data = (uint8_t*) hap_resp;
|
||||
}else
|
||||
{
|
||||
_resp_len = 0;
|
||||
reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_INSUF_RESOURCES;
|
||||
}
|
||||
|
||||
LOG_LV2("HAP", "Response Data");
|
||||
LOG_LV2_BUFFER(NULL, hap_resp, reply.params.write.len);
|
||||
err_t err = sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
|
||||
|
||||
rtos_free(hap_resp);
|
||||
VERIFY_STATUS( err, );
|
||||
processGattWrite(conn_hdl, &event->evt.gatts_evt.params.authorize_request.request.write);
|
||||
}
|
||||
|
||||
/*------------- Handle HAP Response -------------*/
|
||||
// HAP Response
|
||||
if (event->evt.gatts_evt.params.authorize_request.type == BLE_GATTS_AUTHORIZE_TYPE_READ)
|
||||
{
|
||||
ble_gatts_evt_read_t * gatt_req = &event->evt.gatts_evt.params.authorize_request.request.read;
|
||||
|
||||
LOG_LV2("GATTS", "Read uuid = 0x%04X, offset = %d", gatt_req->uuid.uuid, gatt_req->offset);
|
||||
|
||||
ble_gatts_rw_authorize_reply_params_t reply =
|
||||
{
|
||||
.type = BLE_GATTS_AUTHORIZE_TYPE_READ,
|
||||
.params = {
|
||||
.read = {
|
||||
.gatt_status = BLE_GATT_STATUS_SUCCESS,
|
||||
.update = 0,
|
||||
.offset = 0,
|
||||
.len = 0,
|
||||
.p_data = NULL
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (_resp_len)
|
||||
{
|
||||
_resp_len -= min16(_resp_len, Bluefruit.Gap.getMTU(conn_hdl)-2);
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
|
||||
}else
|
||||
{
|
||||
// reject read attempt
|
||||
reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply), );
|
||||
}
|
||||
processGattRead(conn_hdl,&event->evt.gatts_evt.params.authorize_request.request.read);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPCharacteristic.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -55,7 +55,8 @@ enum HAPChrProperties_t
|
||||
class HAPCharacteristic : public BLECharacteristic
|
||||
{
|
||||
public:
|
||||
typedef HAPResponse_t* (*hap_write_cb_t) (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req);
|
||||
typedef void (*hap_write_cb_t) (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
|
||||
typedef void (*hap_read_cb_t ) (uint16_t conn_hdl, HAPCharacteristic* chr);
|
||||
static BLEUuid _g_uuid_cid;
|
||||
|
||||
HAPCharacteristic(BLEUuid bleuuid, uint8_t format, uint16_t unit = UUID16_UNIT_UNITLESS);
|
||||
@@ -70,17 +71,25 @@ class HAPCharacteristic : public BLECharacteristic
|
||||
|
||||
// Callbacks
|
||||
void setHapWriteCallback(hap_write_cb_t fp);
|
||||
|
||||
HAPResponse_t* createHapResponse(uint8_t tid, uint8_t status, TLV8_t tlv_para[] = NULL, uint8_t count = 0);
|
||||
void setHapReadCallback(hap_read_cb_t fp);
|
||||
|
||||
/*------------- Internal Functions -------------*/
|
||||
virtual void _eventHandler(ble_evt_t* event);
|
||||
|
||||
void createHapResponse(uint16_t conn_hdl, uint8_t status, TLV8_t tlv_para[] = NULL, uint8_t count = 0);
|
||||
|
||||
private:
|
||||
uint16_t _cid;
|
||||
uint16_t _hap_props;
|
||||
|
||||
uint16_t _resp_len;
|
||||
// HAP request & response
|
||||
HAPRequest_t* _hap_req;
|
||||
uint16_t _hap_reqlen;
|
||||
uint8_t _tid;
|
||||
|
||||
HAPResponse_t* _hap_resp;
|
||||
uint16_t _hap_resplen;
|
||||
uint16_t _hap_resplen_sent;
|
||||
|
||||
// Char value is read by HAP procedure, not exposed via GATT
|
||||
void* _value;
|
||||
@@ -88,12 +97,18 @@ class HAPCharacteristic : public BLECharacteristic
|
||||
|
||||
// Callbacks
|
||||
hap_write_cb_t _hap_wr_cb;
|
||||
hap_read_cb_t _hap_rd_cb;
|
||||
|
||||
err_t _addChrIdDescriptor(void);
|
||||
|
||||
HAPResponse_t* processChrSignatureRead(HAPRequest_t* hap_req);
|
||||
HAPResponse_t* processChrRead(HAPRequest_t* hap_req);
|
||||
void processGattWrite(uint16_t conn_hdl, ble_gatts_evt_write_t* gatt_req);
|
||||
void processGattRead(uint16_t conn_hdl, ble_gatts_evt_read_t* gatt_req);
|
||||
|
||||
void processHapRequest(uint16_t conn_hdl, HAPRequest_t* hap_req);
|
||||
void processChrSignatureRead(uint16_t conn_hdl, HAPRequest_t* hap_req);
|
||||
void processChrRead(uint16_t conn_hdl, HAPRequest_t* hap_req);
|
||||
|
||||
friend void test_homekit(void);
|
||||
};
|
||||
|
||||
#endif /* HAPCHARACTERISTIC_H_ */
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPProcedure.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -38,23 +38,164 @@
|
||||
#include "HAPUuid.h"
|
||||
#include "HAPProcedure.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Decode
|
||||
*------------------------------------------------------------------*/
|
||||
static uint16_t determine_next_len(uint8_t const* data, uint16_t bufsize)
|
||||
{
|
||||
uint16_t len = 0;
|
||||
uint8_t type = data[0];
|
||||
|
||||
while (bufsize)
|
||||
{
|
||||
if ( type != data[0] ) break; // different item type
|
||||
|
||||
uint8_t fraqlen = data[1];
|
||||
|
||||
len += fraqlen;
|
||||
|
||||
// next fragment/item
|
||||
bufsize -= minof(fraqlen+2, bufsize); // in case data is corruption or missing.
|
||||
data += fraqlen+2;
|
||||
|
||||
if (fraqlen < 255) break;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a TLV8 item
|
||||
*
|
||||
* @param pp_data payload pointer, got update to next item when done
|
||||
* @param p_len len of payload, got updated to remaining item when done
|
||||
*
|
||||
* @return TLV8 item.
|
||||
*
|
||||
* @note If the decoded payload > 255, function will automatically malloced
|
||||
* a continuous buffer to assembly data. User should not change the tlv.value
|
||||
* pointer and call tlv8_decode_cleanup(tlv) when done processing the tlv.
|
||||
*/
|
||||
TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len)
|
||||
{
|
||||
TLV8_t tlv;
|
||||
varclr(&tlv);
|
||||
|
||||
if ( (*p_len) )
|
||||
uint8_t const* data = *pp_data;
|
||||
uint16_t dlen = *p_len;
|
||||
|
||||
if ( dlen )
|
||||
{
|
||||
uint8_t const* data = *pp_data;
|
||||
uint8_t* mbuf = NULL;
|
||||
|
||||
tlv.type = data[0];
|
||||
tlv.len = data[1];
|
||||
tlv.value = data+2;
|
||||
tlv.len = determine_next_len(data, dlen);
|
||||
|
||||
// next item
|
||||
(*p_len) -= tlv.len+2;
|
||||
(*pp_data) += tlv.len+2;
|
||||
// malloc to assembly fragments in continuous buffer
|
||||
if ( tlv.len > 0xff )
|
||||
{
|
||||
mbuf = (uint8_t*) rtos_malloc( tlv.len );
|
||||
VERIFY(mbuf, tlv);
|
||||
|
||||
tlv.value = mbuf;
|
||||
}else
|
||||
{
|
||||
tlv.value = data+2;
|
||||
}
|
||||
|
||||
tlv.type = data[0];
|
||||
|
||||
while(dlen)
|
||||
{
|
||||
// different item type
|
||||
if ( tlv.type != data[0] ) break;
|
||||
|
||||
uint8_t fraqlen = data[1];
|
||||
|
||||
if ( mbuf )
|
||||
{
|
||||
memcpy(mbuf, data+2, fraqlen);
|
||||
mbuf += fraqlen;
|
||||
}
|
||||
|
||||
// next fragment/item
|
||||
dlen -= minof(fraqlen+2, dlen); // in case data is corruption or missing.
|
||||
data += fraqlen+2;
|
||||
|
||||
if ( fraqlen < 0xff ) break;
|
||||
}
|
||||
}
|
||||
|
||||
// update buffer and len
|
||||
*p_len = dlen;
|
||||
*pp_data = data;
|
||||
|
||||
return tlv;
|
||||
}
|
||||
|
||||
uint8_t tlv8_decode_n(uint8_t const* buf, uint16_t bufsize, TLV8_t tlv[], uint8_t count)
|
||||
{
|
||||
uint8_t i;
|
||||
for(i=0; i<count && bufsize; i++)
|
||||
{
|
||||
tlv[i] = tlv8_decode_next(&buf, &bufsize);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void tlv8_decode_cleanup(TLV8_t tlv)
|
||||
{
|
||||
if (tlv.len > 0xff) rtos_free( (void*) tlv.value);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Encode
|
||||
*------------------------------------------------------------------*/
|
||||
uint16_t tlv8_encode_calculate_len(TLV8_t tlv_para[], uint8_t count)
|
||||
{
|
||||
uint16_t total_len = 0;
|
||||
for(uint8_t i=0; i <count ; i++)
|
||||
{
|
||||
total_len += tlv_para[i].len + 2 + (tlv_para[i].len/255)*2;
|
||||
|
||||
if ( tlv_para[i].len % 255 == 0 ) total_len -= 2;
|
||||
}
|
||||
|
||||
return total_len;
|
||||
}
|
||||
|
||||
bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* p_buflen, TLV8_t tlv)
|
||||
{
|
||||
while ( tlv.len )
|
||||
{
|
||||
uint8_t* buf = *pp_buf;
|
||||
uint8_t const fraqlen = (uint8_t) min16(tlv.len, 255);
|
||||
|
||||
buf[0] = tlv.type;
|
||||
buf[1] = fraqlen;
|
||||
|
||||
VERIFY( (*p_buflen) >= fraqlen + 2 );
|
||||
memcpy(buf+2, tlv.value, fraqlen);
|
||||
|
||||
tlv.len -= fraqlen;
|
||||
tlv.value += fraqlen;
|
||||
|
||||
(*p_buflen) -= fraqlen+2;
|
||||
(*pp_buf) += fraqlen+2;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t tlv8_encode_n(uint8_t* buf, uint16_t bufsize, TLV8_t tlv[], uint8_t count)
|
||||
{
|
||||
uint16_t len = bufsize;
|
||||
|
||||
for(uint8_t i=0; i < count; i++)
|
||||
{
|
||||
if( !tlv8_encode_next(&buf, &bufsize, tlv[i]) ) break;
|
||||
}
|
||||
|
||||
return len - bufsize;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPProcedure.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -48,24 +48,24 @@ enum HAPOpcode_t
|
||||
|
||||
enum HAPParamType_t
|
||||
{
|
||||
HAP_PARAM_VALUE = 1 ,
|
||||
HAP_PARAM_ADDITIONAL_AUTHORIZATION_DATA ,
|
||||
HAP_PARAM_ORIGIN , // local vs remote
|
||||
HAP_PARAM_CHR_TYPE ,
|
||||
HAP_PARAM_CHR_ID ,
|
||||
HAP_PARAM_SVC_TYPE ,
|
||||
HAP_PARAM_SVC_ID ,
|
||||
HAP_PARAM_TTL ,
|
||||
HAP_PARAM_RETURN_RESP ,
|
||||
HAP_PARAM_HAP_CHR_PROPERTIES_DESC ,
|
||||
HAP_PARAM_GATT_USR_DESC ,
|
||||
HAP_PARAM_GATT_FORMAT_DESC ,
|
||||
HAP_PARAM_GATT_VALID_RANGE ,
|
||||
HAP_PARAM_HAP_STEP_VALUE_DESC ,
|
||||
HAP_PARAM_HAP_SVC_PROPERTIES ,
|
||||
HAP_PARAM_HAP_LINKED_SVC ,
|
||||
HAP_PARAM_HAP_VALID_VALUES_DESC ,
|
||||
HAP_PARAM_HAP_VALID_VALUES_RANGE_DESC
|
||||
HAP_PARAM_VALUE = 1 , // 1
|
||||
HAP_PARAM_ADDITIONAL_AUTHORIZATION_DATA , // 2
|
||||
HAP_PARAM_ORIGIN , // 3 local vs remote
|
||||
HAP_PARAM_CHR_TYPE , // 4
|
||||
HAP_PARAM_CHR_ID , // 5
|
||||
HAP_PARAM_SVC_TYPE , // 6
|
||||
HAP_PARAM_SVC_ID , // 7
|
||||
HAP_PARAM_TTL , // 8
|
||||
HAP_PARAM_RETURN_RESP , // 9
|
||||
HAP_PARAM_HAP_CHR_PROPERTIES_DESC , // 10
|
||||
HAP_PARAM_GATT_USR_DESC , // 11
|
||||
HAP_PARAM_GATT_FORMAT_DESC , // 12
|
||||
HAP_PARAM_GATT_VALID_RANGE , // 13
|
||||
HAP_PARAM_HAP_STEP_VALUE_DESC , // 14
|
||||
HAP_PARAM_HAP_SVC_PROPERTIES , // 15
|
||||
HAP_PARAM_HAP_LINKED_SVC , // 16
|
||||
HAP_PARAM_HAP_VALID_VALUES_DESC , // 17
|
||||
HAP_PARAM_HAP_VALID_VALUES_RANGE_DESC // 18
|
||||
};
|
||||
|
||||
enum HAPStatusCode_t
|
||||
@@ -86,11 +86,11 @@ enum HAPPduType_t
|
||||
};
|
||||
|
||||
|
||||
typedef struct ATTR_PACKED
|
||||
typedef struct
|
||||
{
|
||||
uint8_t type;
|
||||
uint8_t len;
|
||||
const void* value; // need serialization, not reflect physical layout
|
||||
uint8_t type;
|
||||
uint16_t len;
|
||||
const void* value;
|
||||
}TLV8_t;
|
||||
|
||||
|
||||
@@ -147,7 +147,12 @@ typedef struct ATTR_PACKED
|
||||
} HAPResponse_t;
|
||||
|
||||
|
||||
TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len);
|
||||
void tlv8_decode_cleanup(TLV8_t tlv);
|
||||
uint8_t tlv8_decode_n(uint8_t const* buf, uint16_t bufsize, TLV8_t tlv[], uint8_t count);
|
||||
|
||||
TLV8_t tlv8_decode_next(uint8_t const** pp_data, uint16_t* p_len);
|
||||
bool tlv8_encode_next(uint8_t** pp_buf, uint16_t* p_buflen, TLV8_t tlv);
|
||||
uint16_t tlv8_encode_n(uint8_t* buf, uint16_t bufsize, TLV8_t tlv[], uint8_t count);
|
||||
uint16_t tlv8_encode_calculate_len(TLV8_t tlv_para[], uint8_t count);
|
||||
|
||||
#endif /* HAPPROCEDURE_H_ */
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPService.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPService.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPUuid.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
/*
|
||||
* crypto.c
|
||||
*
|
||||
* Created on: Jun 25, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
//#include <nordic_common.h>
|
||||
//#include <app_error.h>
|
||||
//#include <pstorage.h>
|
||||
//#include <app_scheduler.h>
|
||||
#include <nrf_soc.h>
|
||||
#include "nffs/nffs.h"
|
||||
#include "utility/debug.h"
|
||||
|
||||
#include "rtos.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
#include "crypto.h"
|
||||
|
||||
#define CRYPTO_INSTANCE 2 // Change this to force key regeneration on next run
|
||||
|
||||
crypto_keys_t crypto_keys;
|
||||
|
||||
static const uint8_t zeros64[64];
|
||||
//static pstorage_handle_t crypto_store_handle;
|
||||
//static volatile uint8_t crypto_storing;
|
||||
static uint8_t crypto_loadKeys(void);
|
||||
//static void crypto_pstorage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len);
|
||||
|
||||
#define CRYPTO_KEYDIR "/adafruit/homekit"
|
||||
#define CRYPTO_KEYFILE CRYPTO_KEYDIR "/key"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t valid0;
|
||||
uint8_t instance;
|
||||
|
||||
// Local keys
|
||||
uint8_t srp_b[32];
|
||||
uint8_t srp_salt[16];
|
||||
uint8_t srp_v[384];
|
||||
uint8_t srp_B[384];
|
||||
uint8_t sign_secret[64];
|
||||
|
||||
// Client keys
|
||||
uint8_t clientname[36];
|
||||
uint8_t ltpk[32];
|
||||
|
||||
uint8_t valid1;
|
||||
uint8_t __padding__[1];
|
||||
} crypto_persistent_keys_t;
|
||||
|
||||
void crypto_printkey(void)
|
||||
{
|
||||
crypto_persistent_keys_t keys = {};
|
||||
|
||||
uint32_t keylen = sizeof(keys);
|
||||
fsutil_read_file(CRYPTO_KEYFILE, 0, keylen, &keys, &keylen);
|
||||
|
||||
if (keys.valid0 == 0x55 && keys.valid1 == 0xAA && keys.instance == CRYPTO_INSTANCE)
|
||||
{
|
||||
dbgDumpMemoryCFormat("uint8_t keys[] = ", &keys, sizeof(keys));
|
||||
}
|
||||
}
|
||||
|
||||
void crypto_init(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
//
|
||||
// static const pstorage_module_param_t param =
|
||||
// {
|
||||
// .cb = crypto_pstorage_callback,
|
||||
// .block_size = sizeof(crypto_persistent_keys_t),
|
||||
// .block_count = 1
|
||||
// };
|
||||
// err_code = pstorage_register((pstorage_module_param_t*)¶m, &crypto_store_handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
if (!crypto_loadKeys())
|
||||
{
|
||||
srp_init();
|
||||
|
||||
crypto_sign_keypair(crypto_keys.sign.pub, crypto_keys.sign.secret);
|
||||
|
||||
// Store for reuse
|
||||
// crypto_scheduleStoreKeys();
|
||||
crypto_storeKeys();
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t crypto_loadKeys(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
//
|
||||
// pstorage_handle_t handle;
|
||||
// err_code = pstorage_block_identifier_get(&crypto_store_handle, 0, &handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
// crypto_persistent_keys_t keys = {};
|
||||
// err_code = pstorage_load((uint8_t*)&keys, &handle, sizeof(keys), 0);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
crypto_persistent_keys_t keys = {};
|
||||
|
||||
uint32_t keylen = sizeof(keys);
|
||||
fsutil_read_file(CRYPTO_KEYFILE, 0, keylen, &keys, &keylen);
|
||||
|
||||
if (keys.valid0 == 0x55 && keys.valid1 == 0xAA && keys.instance == CRYPTO_INSTANCE)
|
||||
{
|
||||
// Valid
|
||||
memcpy(srp.b, keys.srp_b, 32);
|
||||
memcpy(srp.salt, keys.srp_salt, 16);
|
||||
memcpy(srp.v, keys.srp_v, 384);
|
||||
memcpy(srp.B, keys.srp_B, 384);
|
||||
memcpy(crypto_keys.sign.secret, keys.sign_secret, 64);
|
||||
memcpy(crypto_keys.client.name, keys.clientname, 36);
|
||||
memcpy(crypto_keys.client.ltpk, keys.ltpk, 32);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void crypto_scheduleStoreKeys(void)
|
||||
{
|
||||
// crypto_storing = 1;
|
||||
ada_callback(NULL, crypto_storeKeys);
|
||||
}
|
||||
|
||||
void crypto_storeKeys(void)
|
||||
{
|
||||
// uint32_t err_code;
|
||||
|
||||
// if (crypto_storing)
|
||||
{
|
||||
// pstorage_handle_t handle;
|
||||
// err_code = pstorage_block_identifier_get(&crypto_store_handle, 0, &handle);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
|
||||
crypto_persistent_keys_t keys = {};
|
||||
memcpy(keys.srp_b, srp.b, 32);
|
||||
memcpy(keys.srp_salt, srp.salt, 16);
|
||||
memcpy(keys.srp_v, srp.v, 384);
|
||||
memcpy(keys.srp_B, srp.B, 384);
|
||||
memcpy(keys.sign_secret, crypto_keys.sign.secret, 64);
|
||||
memcpy(keys.clientname, crypto_keys.client.name, 36);
|
||||
memcpy(keys.ltpk, crypto_keys.client.ltpk, 32);
|
||||
|
||||
keys.instance = CRYPTO_INSTANCE;
|
||||
keys.valid0 = 0x55;
|
||||
keys.valid1 = 0xAA;
|
||||
|
||||
fsutil_write_file(CRYPTO_KEYFILE, &keys, sizeof(keys));
|
||||
|
||||
// err_code = pstorage_update(&handle, (uint8_t*)&keys, sizeof(keys), 0);
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
//
|
||||
// // Pump events until the store is done
|
||||
// while (crypto_storing)
|
||||
// {
|
||||
// err_code = sd_app_evt_wait();
|
||||
// APP_ERROR_CHECK(err_code);
|
||||
// app_sched_execute();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
//static void crypto_pstorage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len)
|
||||
//{
|
||||
// switch (op_code)
|
||||
// {
|
||||
// case PSTORAGE_UPDATE_OP_CODE:
|
||||
// crypto_storing = 0;
|
||||
// break;
|
||||
//
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
|
||||
uint8_t crypto_advertise(void)
|
||||
{
|
||||
// If we have a client name, we don't advertise, otherwise we do.
|
||||
if (crypto_keys.client.name[0])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t crypto_verifyAndDecrypt(const uint8_t* key, uint8_t* nonce, uint8_t* encrypted, uint8_t length, uint8_t* output_buf, uint8_t* mac)
|
||||
{
|
||||
uint8_t polykey[sizeof(zeros64)];
|
||||
crypto_stream_chacha20_xor(polykey, zeros64, sizeof(zeros64), nonce, key, 0);
|
||||
|
||||
uint8_t padding = (16 - length % 16) % 16;
|
||||
uint8_t message[length + padding + 16];
|
||||
memcpy(message, encrypted, length);
|
||||
memset(message + length, 0, padding + 16);
|
||||
message[length + padding + 8] = (uint8_t)length;
|
||||
message[length + padding + 9] = (uint8_t)(length >> 8);
|
||||
|
||||
if (crypto_onetimeauth_poly1305_verify(mac, message, sizeof(message), polykey) != 0)
|
||||
{
|
||||
// Fail
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
crypto_stream_chacha20_xor(output_buf, message, length, nonce, key, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void crypto_encryptAndSeal(const uint8_t* key, uint8_t* nonce, uint8_t* plain, uint16_t length, uint8_t* output_buf, uint8_t* output_mac)
|
||||
{
|
||||
uint8_t polykey[sizeof(zeros64)];
|
||||
crypto_stream_chacha20_xor(polykey, zeros64, sizeof(zeros64), nonce, key, 0);
|
||||
|
||||
uint8_t padding = (16 - length % 16) % 16;
|
||||
uint8_t message[length + padding + 16];
|
||||
|
||||
crypto_stream_chacha20_xor(message, plain, length, nonce, key, 1);
|
||||
memset(message + length, 0, padding + 16);
|
||||
message[length + padding + 8] = (uint8_t)length;
|
||||
message[length + padding + 9] = (uint8_t)(length >> 8);
|
||||
|
||||
crypto_onetimeauth_poly1305(output_mac, message, sizeof(message), polykey);
|
||||
|
||||
memcpy(output_buf, message, length);
|
||||
}
|
||||
|
||||
void crypto_sha512hmac(uint8_t* hash, uint8_t* salt, uint8_t salt_length, uint8_t* data, uint8_t data_length)
|
||||
{
|
||||
uint8_t message1[128 + data_length];
|
||||
uint8_t message2[128 + 64];
|
||||
|
||||
memset(message1, 0x36, 128);
|
||||
memset(message2, 0x5C, 128);
|
||||
for (unsigned i = salt_length; i--; )
|
||||
{
|
||||
message1[i] = 0x36 ^ salt[i];
|
||||
message2[i] = 0x5C ^ salt[i];
|
||||
}
|
||||
memcpy(message1 + 128, data, data_length);
|
||||
crypto_hash_sha512(message2 + 128, message1, sizeof(message1));
|
||||
crypto_hash_sha512(hash, message2, sizeof(message2));
|
||||
}
|
||||
|
||||
void crypto_hkdf(uint8_t* target, uint8_t* salt, uint8_t salt_length, uint8_t* info, uint8_t info_length, uint8_t* ikm, uint8_t ikm_length)
|
||||
{
|
||||
crypto_sha512hmac(target, salt, salt_length, ikm, ikm_length);
|
||||
crypto_sha512hmac(target, target, 64, info, info_length);
|
||||
}
|
||||
|
||||
void crypto_transportEncrypt(uint8_t* key, uint8_t* nonce, uint8_t* plaintext, uint16_t plength, uint8_t* ciphertext, uint16_t* clength)
|
||||
{
|
||||
crypto_encryptAndSeal(key, nonce, plaintext, plength, ciphertext, ciphertext + plength);
|
||||
for (unsigned i = 0; i < 8 && !++nonce[i]; i++)
|
||||
;
|
||||
*clength = plength + 16;
|
||||
}
|
||||
|
||||
uint8_t crypto_transportDecrypt(uint8_t* key, uint8_t* nonce, uint8_t* ciphertext, uint16_t clength, uint8_t* plaintext, uint16_t* plength)
|
||||
{
|
||||
uint8_t r = crypto_verifyAndDecrypt(key, nonce, ciphertext, clength - 16, plaintext, ciphertext + clength - 16);
|
||||
for (unsigned i = 0; i < 8 && !++nonce[i]; i++)
|
||||
;
|
||||
*plength = clength - 16;
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* crypto.h
|
||||
*
|
||||
* Created on: Jun 25, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_CRYPTO_H_
|
||||
#define HOMEKIT_CRYPTO_H_
|
||||
|
||||
//#define USE_UNACL_SCALARMULT 1
|
||||
#define USE_TWEETNACL_SCALARMULT 1
|
||||
|
||||
#if defined(USE_UNACL_SCALARMULT)
|
||||
#include "uNaCl/api.h"
|
||||
#endif
|
||||
#include "tweetnacl-modified/tweetnacl.h"
|
||||
#include "srp/srp.h"
|
||||
#include "random.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct
|
||||
{
|
||||
// Public key is the last 32-bytes of the secret key
|
||||
union
|
||||
{
|
||||
uint8_t secret[64];
|
||||
struct
|
||||
{
|
||||
uint8_t __ignore__[32];
|
||||
uint8_t pub[32];
|
||||
};
|
||||
};
|
||||
} sign;
|
||||
|
||||
// Client we are paired with
|
||||
struct
|
||||
{
|
||||
uint8_t name[36];
|
||||
uint8_t ltpk[32];
|
||||
} client;
|
||||
} crypto_keys_t;
|
||||
|
||||
extern crypto_keys_t crypto_keys;
|
||||
|
||||
|
||||
extern void crypto_init(void);
|
||||
extern void crypto_scheduleStoreKeys(void);
|
||||
extern void crypto_storeKeys(void);
|
||||
extern uint8_t crypto_verifyAndDecrypt(const uint8_t* key, uint8_t* nonce, uint8_t* encrypted, uint8_t length, uint8_t* output_buf, uint8_t* mac);
|
||||
extern void crypto_encryptAndSeal(const uint8_t* key, uint8_t* nonce, uint8_t* plain, uint16_t length, uint8_t* output_buf, uint8_t* output_mac);
|
||||
extern void crypto_sha512hmac(uint8_t* hash, uint8_t* salt, uint8_t salt_length, uint8_t* data, uint8_t data_length);
|
||||
extern void crypto_hkdf(uint8_t* target, uint8_t* salt, uint8_t salt_length, uint8_t* info, uint8_t info_length, uint8_t* ikm, uint8_t ikm_length);
|
||||
extern void crypto_transportEncrypt(uint8_t* key, uint8_t* nonce, uint8_t* plaintext, uint16_t plength, uint8_t* ciphertext, uint16_t* clength);
|
||||
extern uint8_t crypto_transportDecrypt(uint8_t* key, uint8_t* nonce, uint8_t* ciphertext, uint16_t clength, uint8_t* plaintext, uint16_t* plength);
|
||||
extern uint8_t crypto_advertise(void);
|
||||
|
||||
void crypto_printkey(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HOMEKIT_CRYPTO_H_ */
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* random.c
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
//#include <softdevice_handler.h>
|
||||
|
||||
#include "nrf_soc.h"
|
||||
#include "random.h"
|
||||
|
||||
#define APP_ERROR_CHECK(_err) if (_err != NRF_SUCCESS) return
|
||||
|
||||
void random_create(uint8_t* p_result, uint8_t length)
|
||||
{
|
||||
uint32_t err_code;
|
||||
|
||||
while (length)
|
||||
{
|
||||
uint8_t available = 0;
|
||||
err_code = sd_rand_application_bytes_available_get(&available);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
if (available)
|
||||
{
|
||||
available = available < length ? available : length;
|
||||
err_code = sd_rand_application_vector_get(p_result, available);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
p_result += available;
|
||||
length -= available;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void randombytes(uint8_t* p_result, uint64_t length)
|
||||
{
|
||||
random_create(p_result, (uint8_t)length);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* random.h
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_RANDOM_H_
|
||||
#define HOMEKIT_RANDOM_H_
|
||||
|
||||
extern void random_create(uint8_t* p_result, uint8_t length);
|
||||
extern void randombytes(uint8_t* p_result, uint64_t length);
|
||||
|
||||
#endif /* HOMEKIT_RANDOM_H_ */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,754 @@
|
||||
/**
|
||||
* \file bignum.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_BIGNUM_H
|
||||
#define POLARSSL_BIGNUM_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
||||
#include <basetsd.h>
|
||||
#if (_MSC_VER <= 1200)
|
||||
typedef signed short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
#else
|
||||
typedef INT16 int16_t;
|
||||
typedef UINT16 uint16_t;
|
||||
#endif
|
||||
typedef INT32 int32_t;
|
||||
typedef INT64 int64_t;
|
||||
typedef UINT32 uint32_t;
|
||||
typedef UINT64 uint64_t;
|
||||
#else
|
||||
#include <inttypes.h>
|
||||
#endif /* _MSC_VER && !EFIX64 && !EFI32 */
|
||||
|
||||
#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
|
||||
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
|
||||
#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
|
||||
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
|
||||
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
|
||||
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
|
||||
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
|
||||
#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
|
||||
|
||||
#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
|
||||
|
||||
/*
|
||||
* Maximum size MPIs are allowed to grow to in number of limbs.
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_LIMBS 10000
|
||||
|
||||
#if !defined(POLARSSL_MPI_WINDOW_SIZE)
|
||||
/*
|
||||
* Maximum window size used for modular exponentiation. Default: 6
|
||||
* Minimum value: 1. Maximum value: 6.
|
||||
*
|
||||
* Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
|
||||
* for the sliding window calculation. (So 64 by default)
|
||||
*
|
||||
* Reduction in size, reduces speed.
|
||||
*/
|
||||
#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
|
||||
#endif /* !POLARSSL_MPI_WINDOW_SIZE */
|
||||
|
||||
#if !defined(POLARSSL_MPI_MAX_SIZE)
|
||||
/*
|
||||
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
|
||||
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
|
||||
*
|
||||
* Note: Calculations can results temporarily in larger MPIs. So the number
|
||||
* of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
|
||||
#endif /* !POLARSSL_MPI_MAX_SIZE */
|
||||
|
||||
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
|
||||
|
||||
/*
|
||||
* When reading from files with mpi_read_file() and writing to files with
|
||||
* mpi_write_file() the buffer should have space
|
||||
* for a (short) label, the MPI (in the provided radix), the newline
|
||||
* characters and the '\0'.
|
||||
*
|
||||
* By default we assume at least a 10 char label, a minimum radix of 10
|
||||
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
|
||||
* Autosized at compile time for at least a 10 char label, a minimum radix
|
||||
* of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
|
||||
*
|
||||
* This used to be statically sized to 1250 for a maximum of 4096 bit
|
||||
* numbers (1234 decimal chars).
|
||||
*
|
||||
* Calculate using the formula:
|
||||
* POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
|
||||
* LabelSize + 6
|
||||
*/
|
||||
#define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS )
|
||||
#define LN_2_DIV_LN_10_SCALE100 332
|
||||
#define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
|
||||
|
||||
/*
|
||||
* Define the base integer type, architecture-wise
|
||||
*/
|
||||
#if defined(POLARSSL_HAVE_INT8)
|
||||
typedef signed char t_sint;
|
||||
typedef unsigned char t_uint;
|
||||
typedef uint16_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#if defined(POLARSSL_HAVE_INT16)
|
||||
typedef int16_t t_sint;
|
||||
typedef uint16_t t_uint;
|
||||
typedef uint32_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
/*
|
||||
* 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
|
||||
* by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM
|
||||
*/
|
||||
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
||||
defined(_MSC_VER) && defined(_M_AMD64) )
|
||||
#define POLARSSL_HAVE_INT64
|
||||
typedef int64_t t_sint;
|
||||
typedef uint64_t t_uint;
|
||||
#else
|
||||
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
||||
defined(__GNUC__) && ( \
|
||||
defined(__amd64__) || defined(__x86_64__) || \
|
||||
defined(__ppc64__) || defined(__powerpc64__) || \
|
||||
defined(__ia64__) || defined(__alpha__) || \
|
||||
(defined(__sparc__) && defined(__arch64__)) || \
|
||||
defined(__s390x__) || defined(__mips64) ) )
|
||||
#define POLARSSL_HAVE_INT64
|
||||
typedef int64_t t_sint;
|
||||
typedef uint64_t t_uint;
|
||||
typedef unsigned int t_udbl __attribute__((mode(TI)));
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#define POLARSSL_HAVE_INT32
|
||||
typedef int32_t t_sint;
|
||||
typedef uint32_t t_uint;
|
||||
#if ( defined(_MSC_VER) && defined(_M_IX86) )
|
||||
typedef uint64_t t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#else
|
||||
#if defined( POLARSSL_HAVE_LONGLONG )
|
||||
typedef unsigned long long t_udbl;
|
||||
#define POLARSSL_HAVE_UDBL
|
||||
#endif
|
||||
#endif
|
||||
#endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */
|
||||
#endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */
|
||||
#endif /* POLARSSL_HAVE_INT16 */
|
||||
#endif /* POLARSSL_HAVE_INT8 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief MPI structure
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int s; /*!< integer sign */
|
||||
size_t n; /*!< total # of limbs */
|
||||
t_uint *p; /*!< pointer to limbs */
|
||||
}
|
||||
mpi;
|
||||
|
||||
/**
|
||||
* \brief Initialize one MPI (make internal references valid)
|
||||
* This just makes it ready to be set or freed,
|
||||
* but does not define a value for the MPI.
|
||||
*
|
||||
* \param X One MPI to initialize.
|
||||
*/
|
||||
void mpi_init( mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Unallocate one MPI
|
||||
*
|
||||
* \param X One MPI to unallocate.
|
||||
*/
|
||||
void mpi_free( mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Enlarge to the specified number of limbs
|
||||
*
|
||||
* \param X MPI to grow
|
||||
* \param nblimbs The target number of limbs
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_grow( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Resize down, keeping at least the specified number of limbs
|
||||
*
|
||||
* \param X MPI to shrink
|
||||
* \param nblimbs The minimum number of limbs to keep
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shrink( mpi *X, size_t nblimbs );
|
||||
|
||||
/**
|
||||
* \brief Copy the contents of Y into X
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param Y Source MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_copy( mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Swap the contents of X and Y
|
||||
*
|
||||
* \param X First MPI value
|
||||
* \param Y Second MPI value
|
||||
*/
|
||||
void mpi_swap( mpi *X, mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional assignement X = Y if assign is 1
|
||||
*
|
||||
* \param X MPI to conditionally assign to
|
||||
* \param Y Value to be assigned
|
||||
* \param assign 1: perform the assignment, 0: keep X's original value
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mpi_copy( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Safe conditional swap X <-> Y if swap is 1
|
||||
*
|
||||
* \param X First mpi value
|
||||
* \param Y Second mpi value
|
||||
* \param assign 1: perform the swap, 0: keep X and Y's original values
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
*
|
||||
* \note This function is equivalent to
|
||||
* if( assign ) mpi_swap( X, Y );
|
||||
* except that it avoids leaking any information about whether
|
||||
* the assignment was done or not (the above code may leak
|
||||
* information through branch prediction and/or memory access
|
||||
* patterns analysis).
|
||||
*/
|
||||
int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
|
||||
|
||||
/**
|
||||
* \brief Set value from integer
|
||||
*
|
||||
* \param X MPI to set
|
||||
* \param z Value to use
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_lset( mpi *X, t_sint z );
|
||||
|
||||
/**
|
||||
* \brief Get a specific bit from X
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
*
|
||||
* \return Either a 0 or a 1
|
||||
*/
|
||||
int mpi_get_bit( const mpi *X, size_t pos );
|
||||
|
||||
/**
|
||||
* \brief Set a bit of X to a specific value of 0 or 1
|
||||
*
|
||||
* \note Will grow X if necessary to set a bit to 1 in a not yet
|
||||
* existing limb. Will not grow if bit should be set to 0
|
||||
*
|
||||
* \param X MPI to use
|
||||
* \param pos Zero-based index of the bit in X
|
||||
* \param val The value to set the bit to (0 or 1)
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
|
||||
*/
|
||||
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
|
||||
|
||||
/**
|
||||
* \brief Return the number of zero-bits before the least significant
|
||||
* '1' bit
|
||||
*
|
||||
* Note: Thus also the zero-based index of the least significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_lsb( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the number of bits up to and including the most
|
||||
* significant '1' bit'
|
||||
*
|
||||
* Note: Thus also the one-based index of the most significant '1' bit
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_msb( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Return the total size in bytes
|
||||
*
|
||||
* \param X MPI to use
|
||||
*/
|
||||
size_t mpi_size( const mpi *X );
|
||||
|
||||
/**
|
||||
* \brief Import from an ASCII string
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param s Null-terminated string buffer
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
||||
*/
|
||||
int mpi_read_string( mpi *X, int radix, const char *s );
|
||||
|
||||
/**
|
||||
* \brief Export into an ASCII string
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param s String buffer
|
||||
* \param slen String buffer size
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
|
||||
* *slen is always updated to reflect the amount
|
||||
* of data that has (or would have) been written.
|
||||
*
|
||||
* \note Call this function with *slen = 0 to obtain the
|
||||
* minimum required buffer size in *slen.
|
||||
*/
|
||||
int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
|
||||
|
||||
#if defined(POLARSSL_FS_IO)
|
||||
/**
|
||||
* \brief Read X from an opened file
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param radix Input numeric base
|
||||
* \param fin Input file handle
|
||||
*
|
||||
* \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
|
||||
* the file read buffer is too small or a
|
||||
* POLARSSL_ERR_MPI_XXX error code
|
||||
*/
|
||||
int mpi_read_file( mpi *X, int radix, FILE *fin );
|
||||
|
||||
/**
|
||||
* \brief Write X into an opened file, or stdout if fout is NULL
|
||||
*
|
||||
* \param p Prefix, can be NULL
|
||||
* \param X Source MPI
|
||||
* \param radix Output numeric base
|
||||
* \param fout Output file handle (can be NULL)
|
||||
*
|
||||
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
|
||||
*
|
||||
* \note Set fout == NULL to print X on the console.
|
||||
*/
|
||||
int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
|
||||
#endif /* POLARSSL_FS_IO */
|
||||
|
||||
/**
|
||||
* \brief Import X from unsigned binary data, big endian
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param buf Input buffer
|
||||
* \param buflen Input buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Export X into unsigned binary data, big endian.
|
||||
* Always fills the whole buffer, which will start with zeros
|
||||
* if the number is smaller.
|
||||
*
|
||||
* \param X Source MPI
|
||||
* \param buf Output buffer
|
||||
* \param buflen Output buffer size
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
|
||||
*/
|
||||
int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
|
||||
|
||||
/**
|
||||
* \brief Left-shift: X <<= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shift_l( mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Right-shift: X >>= count
|
||||
*
|
||||
* \param X MPI to shift
|
||||
* \param count Amount to shift
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_shift_r( mpi *X, size_t count );
|
||||
|
||||
/**
|
||||
* \brief Compare unsigned values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if |X| is greater than |Y|,
|
||||
* -1 if |X| is lesser than |Y| or
|
||||
* 0 if |X| is equal to |Y|
|
||||
*/
|
||||
int mpi_cmp_abs( const mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param Y Right-hand MPI
|
||||
*
|
||||
* \return 1 if X is greater than Y,
|
||||
* -1 if X is lesser than Y or
|
||||
* 0 if X is equal to Y
|
||||
*/
|
||||
int mpi_cmp_mpi( const mpi *X, const mpi *Y );
|
||||
|
||||
/**
|
||||
* \brief Compare signed values
|
||||
*
|
||||
* \param X Left-hand MPI
|
||||
* \param z The integer value to compare to
|
||||
*
|
||||
* \return 1 if X is greater than z,
|
||||
* -1 if X is lesser than z or
|
||||
* 0 if X is equal to z
|
||||
*/
|
||||
int mpi_cmp_int( const mpi *X, t_sint z );
|
||||
|
||||
/**
|
||||
* \brief Unsigned addition: X = |A| + |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Unsigned subtraction: X = |A| - |B|
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
|
||||
*/
|
||||
int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Signed addition: X = A + b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to add
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_add_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Signed subtraction: X = A - b
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to subtract
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * B
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Baseline multiplication: X = A * b
|
||||
* Note: despite the functon signature, b is treated as a
|
||||
* t_uint. Negative values of b are treated as large positive
|
||||
* values.
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param b The integer value to multiply with
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Division by mpi: A = Q * B + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Division by int: A = Q * b + R
|
||||
*
|
||||
* \param Q Destination MPI for the quotient
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
|
||||
*
|
||||
* \note Either Q or R can be NULL.
|
||||
*/
|
||||
int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Modulo: R = A mod B
|
||||
*
|
||||
* \param R Destination MPI for the rest value
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
|
||||
*/
|
||||
int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modulo: r = A mod b
|
||||
*
|
||||
* \param r Destination t_uint
|
||||
* \param A Left-hand MPI
|
||||
* \param b Integer to divide by
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
|
||||
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
|
||||
*/
|
||||
int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
|
||||
|
||||
/**
|
||||
* \brief Sliding-window exponentiation: X = A^E mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param E Exponent MPI
|
||||
* \param N Modular MPI
|
||||
* \param _RR Speed-up MPI used for recalculations
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
|
||||
* if E is negative
|
||||
*
|
||||
* \note _RR is used to avoid re-computing R*R mod N across
|
||||
* multiple calls, which speeds up things a bit. It can
|
||||
* be set to NULL if the extra performance is unneeded.
|
||||
*/
|
||||
int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
|
||||
|
||||
/**
|
||||
* \brief Fill an MPI X with size bytes of random
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param size Size in bytes
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_fill_random( mpi *X, size_t size,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Greatest common divisor: G = gcd(A, B)
|
||||
*
|
||||
* \param G Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param B Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
||||
*/
|
||||
int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
|
||||
|
||||
/**
|
||||
* \brief Modular inverse: X = A^-1 mod N
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param A Left-hand MPI
|
||||
* \param N Right-hand MPI
|
||||
*
|
||||
* \return 0 if successful,
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
|
||||
POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
|
||||
*/
|
||||
int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
|
||||
|
||||
/**
|
||||
* \brief Miller-Rabin primality test
|
||||
*
|
||||
* \param X MPI to check
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
|
||||
*/
|
||||
int mpi_is_prime( mpi *X,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Prime number generation
|
||||
*
|
||||
* \param X Destination MPI
|
||||
* \param nbits Required size of X in bits
|
||||
* ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
|
||||
* \param dh_flag If 1, then (X-1)/2 will be prime too
|
||||
* \param f_rng RNG function
|
||||
* \param p_rng RNG parameter
|
||||
*
|
||||
* \return 0 if successful (probably prime),
|
||||
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
||||
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
|
||||
*/
|
||||
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng );
|
||||
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if the test failed
|
||||
*/
|
||||
int mpi_self_test( int verbose );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* bignum.h */
|
||||
@@ -0,0 +1,870 @@
|
||||
/**
|
||||
* \file bn_mul.h
|
||||
*
|
||||
* \brief Multi-precision integer library
|
||||
*
|
||||
* Copyright (C) 2006-2010, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* Multiply source vector [s] with b, add result
|
||||
* to destination vector [d] and set carry c.
|
||||
*
|
||||
* Currently supports:
|
||||
*
|
||||
* . IA-32 (386+) . AMD64 / EM64T
|
||||
* . IA-32 (SSE2) . Motorola 68000
|
||||
* . PowerPC, 32-bit . MicroBlaze
|
||||
* . PowerPC, 64-bit . TriCore
|
||||
* . SPARC v8 . ARM v3+
|
||||
* . Alpha . MIPS32
|
||||
* . C, longlong . C, generic
|
||||
*/
|
||||
#ifndef POLARSSL_BN_MUL_H
|
||||
#define POLARSSL_BN_MUL_H
|
||||
|
||||
#include "bignum.h"
|
||||
|
||||
#if defined(POLARSSL_HAVE_ASM)
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__i386__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %%ebx, %0 \n\t" \
|
||||
"movl %5, %%esi \n\t" \
|
||||
"movl %6, %%edi \n\t" \
|
||||
"movl %7, %%ecx \n\t" \
|
||||
"movl %8, %%ebx \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lodsl \n\t" \
|
||||
"mull %%ebx \n\t" \
|
||||
"addl %%ecx, %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"addl (%%edi), %%eax \n\t" \
|
||||
"adcl $0, %%edx \n\t" \
|
||||
"movl %%edx, %%ecx \n\t" \
|
||||
"stosl \n\t"
|
||||
|
||||
#if defined(POLARSSL_HAVE_SSE2)
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movd %%ecx, %%mm1 \n\t" \
|
||||
"movd %%ebx, %%mm0 \n\t" \
|
||||
"movd (%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd (%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"movd 4(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"movd 8(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd 12(%%esi), %%mm7 \n\t" \
|
||||
"pmuludq %%mm0, %%mm7 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 4(%%edi), %%mm3 \n\t" \
|
||||
"paddq %%mm4, %%mm3 \n\t" \
|
||||
"movd 8(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm6, %%mm5 \n\t" \
|
||||
"movd 12(%%edi), %%mm4 \n\t" \
|
||||
"paddq %%mm4, %%mm7 \n\t" \
|
||||
"movd %%mm1, (%%edi) \n\t" \
|
||||
"movd 16(%%esi), %%mm2 \n\t" \
|
||||
"pmuludq %%mm0, %%mm2 \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 20(%%esi), %%mm4 \n\t" \
|
||||
"pmuludq %%mm0, %%mm4 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd 24(%%esi), %%mm6 \n\t" \
|
||||
"pmuludq %%mm0, %%mm6 \n\t" \
|
||||
"movd %%mm1, 4(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd 28(%%esi), %%mm3 \n\t" \
|
||||
"pmuludq %%mm0, %%mm3 \n\t" \
|
||||
"paddq %%mm5, %%mm1 \n\t" \
|
||||
"movd 16(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm2 \n\t" \
|
||||
"movd %%mm1, 8(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm7, %%mm1 \n\t" \
|
||||
"movd 20(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm4 \n\t" \
|
||||
"movd %%mm1, 12(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm2, %%mm1 \n\t" \
|
||||
"movd 24(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm6 \n\t" \
|
||||
"movd %%mm1, 16(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm4, %%mm1 \n\t" \
|
||||
"movd 28(%%edi), %%mm5 \n\t" \
|
||||
"paddq %%mm5, %%mm3 \n\t" \
|
||||
"movd %%mm1, 20(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm6, %%mm1 \n\t" \
|
||||
"movd %%mm1, 24(%%edi) \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"paddq %%mm3, %%mm1 \n\t" \
|
||||
"movd %%mm1, 28(%%edi) \n\t" \
|
||||
"addl $32, %%edi \n\t" \
|
||||
"addl $32, %%esi \n\t" \
|
||||
"psrlq $32, %%mm1 \n\t" \
|
||||
"movd %%mm1, %%ecx \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"emms \n\t" \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %4, %%ebx \n\t" \
|
||||
"movl %%ecx, %1 \n\t" \
|
||||
"movl %%edi, %2 \n\t" \
|
||||
"movl %%esi, %3 \n\t" \
|
||||
: "=m" (t), "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "eax", "ecx", "edx", "esi", "edi" \
|
||||
);
|
||||
#endif /* SSE2 */
|
||||
#endif /* i386 */
|
||||
|
||||
#if defined(__amd64__) || defined (__x86_64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movq %3, %%rsi \n\t" \
|
||||
"movq %4, %%rdi \n\t" \
|
||||
"movq %5, %%rcx \n\t" \
|
||||
"movq %6, %%rbx \n\t" \
|
||||
"xorq %%r8, %%r8 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movq (%%rsi), %%rax \n\t" \
|
||||
"mulq %%rbx \n\t" \
|
||||
"addq $8, %%rsi \n\t" \
|
||||
"addq %%rcx, %%rax \n\t" \
|
||||
"movq %%r8, %%rcx \n\t" \
|
||||
"adcq $0, %%rdx \n\t" \
|
||||
"nop \n\t" \
|
||||
"addq %%rax, (%%rdi) \n\t" \
|
||||
"adcq %%rdx, %%rcx \n\t" \
|
||||
"addq $8, %%rdi \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movq %%rcx, %0 \n\t" \
|
||||
"movq %%rdi, %1 \n\t" \
|
||||
"movq %%rsi, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "rax", "rcx", "rdx", "rbx", "rsi", "rdi", "r8" \
|
||||
);
|
||||
|
||||
#endif /* AMD64 */
|
||||
|
||||
#if defined(__mc68020__) || defined(__mcpu32__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"movl %3, %%a2 \n\t" \
|
||||
"movl %4, %%a3 \n\t" \
|
||||
"movl %5, %%d3 \n\t" \
|
||||
"movl %6, %%d2 \n\t" \
|
||||
"moveq #0, %%d0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"moveq #0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d4, %%d3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"movl %%d3, %0 \n\t" \
|
||||
"movl %%a3, %1 \n\t" \
|
||||
"movl %%a2, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "d2", "d3", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d4:%%d1 \n\t" \
|
||||
"addxl %%d3, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d4 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"movel %%a2@+, %%d1 \n\t" \
|
||||
"mulul %%d2, %%d3:%%d1 \n\t" \
|
||||
"addxl %%d4, %%d1 \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t" \
|
||||
"addl %%d1, %%a3@+ \n\t" \
|
||||
"addxl %%d0, %%d3 \n\t"
|
||||
|
||||
#endif /* MC68000 */
|
||||
|
||||
#if defined(__powerpc64__) || defined(__ppc64__)
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld r3, %3 \n\t" \
|
||||
"ld r4, %4 \n\t" \
|
||||
"ld r5, %5 \n\t" \
|
||||
"ld r6, %6 \n\t" \
|
||||
"addi r3, r3, -8 \n\t" \
|
||||
"addi r4, r4, -8 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu r7, 8(r3) \n\t" \
|
||||
"mulld r8, r7, r6 \n\t" \
|
||||
"mulhdu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"ld r7, 8(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stdu r8, 8(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 8 \n\t" \
|
||||
"addi r3, r3, 8 \n\t" \
|
||||
"std r5, %0 \n\t" \
|
||||
"std r4, %1 \n\t" \
|
||||
"std r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %%r3, %3 \n\t" \
|
||||
"ld %%r4, %4 \n\t" \
|
||||
"ld %%r5, %5 \n\t" \
|
||||
"ld %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -8 \n\t" \
|
||||
"addi %%r4, %%r4, -8 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldu %%r7, 8(%%r3) \n\t" \
|
||||
"mulld %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhdu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"ld %%r7, 8(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stdu %%r8, 8(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 8 \n\t" \
|
||||
"addi %%r3, %%r3, 8 \n\t" \
|
||||
"std %%r5, %0 \n\t" \
|
||||
"std %%r4, %1 \n\t" \
|
||||
"std %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */
|
||||
|
||||
#if defined(__MACH__) && defined(__APPLE__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz r3, %3 \n\t" \
|
||||
"lwz r4, %4 \n\t" \
|
||||
"lwz r5, %5 \n\t" \
|
||||
"lwz r6, %6 \n\t" \
|
||||
"addi r3, r3, -4 \n\t" \
|
||||
"addi r4, r4, -4 \n\t" \
|
||||
"addic r5, r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu r7, 4(r3) \n\t" \
|
||||
"mullw r8, r7, r6 \n\t" \
|
||||
"mulhwu r9, r7, r6 \n\t" \
|
||||
"adde r8, r8, r5 \n\t" \
|
||||
"lwz r7, 4(r4) \n\t" \
|
||||
"addze r5, r9 \n\t" \
|
||||
"addc r8, r8, r7 \n\t" \
|
||||
"stwu r8, 4(r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze r5, r5 \n\t" \
|
||||
"addi r4, r4, 4 \n\t" \
|
||||
"addi r3, r3, 4 \n\t" \
|
||||
"stw r5, %0 \n\t" \
|
||||
"stw r4, %1 \n\t" \
|
||||
"stw r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#else /* __MACH__ && __APPLE__ */
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwz %%r3, %3 \n\t" \
|
||||
"lwz %%r4, %4 \n\t" \
|
||||
"lwz %%r5, %5 \n\t" \
|
||||
"lwz %%r6, %6 \n\t" \
|
||||
"addi %%r3, %%r3, -4 \n\t" \
|
||||
"addi %%r4, %%r4, -4 \n\t" \
|
||||
"addic %%r5, %%r5, 0 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lwzu %%r7, 4(%%r3) \n\t" \
|
||||
"mullw %%r8, %%r7, %%r6 \n\t" \
|
||||
"mulhwu %%r9, %%r7, %%r6 \n\t" \
|
||||
"adde %%r8, %%r8, %%r5 \n\t" \
|
||||
"lwz %%r7, 4(%%r4) \n\t" \
|
||||
"addze %%r5, %%r9 \n\t" \
|
||||
"addc %%r8, %%r8, %%r7 \n\t" \
|
||||
"stwu %%r8, 4(%%r4) \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"addze %%r5, %%r5 \n\t" \
|
||||
"addi %%r4, %%r4, 4 \n\t" \
|
||||
"addi %%r3, %%r3, 4 \n\t" \
|
||||
"stw %%r5, %0 \n\t" \
|
||||
"stw %%r4, %1 \n\t" \
|
||||
"stw %%r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4", "r5", "r6", "r7", "r8", "r9" \
|
||||
);
|
||||
|
||||
#endif /* __MACH__ && __APPLE__ */
|
||||
|
||||
#endif /* PPC32 */
|
||||
|
||||
/*
|
||||
* The Sparc64 assembly is reported to be broken.
|
||||
* Disable it for now, until we're able to fix it.
|
||||
*/
|
||||
#if 0 && defined(__sparc__) && defined(__sparc64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldx %3, %%o0 \n\t" \
|
||||
"ldx %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"stx %%o1, %1 \n\t" \
|
||||
"stx %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
#endif /* SPARCv9 */
|
||||
|
||||
#if defined(__sparc__) && !defined(__sparc64__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld %3, %%o0 \n\t" \
|
||||
"ld %4, %%o1 \n\t" \
|
||||
"ld %5, %%o2 \n\t" \
|
||||
"ld %6, %%o3 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld [%%o0], %%o4 \n\t" \
|
||||
"inc 4, %%o0 \n\t" \
|
||||
"ld [%%o1], %%o5 \n\t" \
|
||||
"umul %%o3, %%o4, %%o4 \n\t" \
|
||||
"addcc %%o4, %%o2, %%o4 \n\t" \
|
||||
"rd %%y, %%g1 \n\t" \
|
||||
"addx %%g1, 0, %%g1 \n\t" \
|
||||
"addcc %%o4, %%o5, %%o4 \n\t" \
|
||||
"st %%o4, [%%o1] \n\t" \
|
||||
"addx %%g1, 0, %%o2 \n\t" \
|
||||
"inc 4, %%o1 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st %%o2, %0 \n\t" \
|
||||
"st %%o1, %1 \n\t" \
|
||||
"st %%o0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "g1", "o0", "o1", "o2", "o3", "o4", \
|
||||
"o5" \
|
||||
);
|
||||
|
||||
#endif /* SPARCv8 */
|
||||
|
||||
#if defined(__microblaze__) || defined(microblaze)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lwi r3, %3 \n\t" \
|
||||
"lwi r4, %4 \n\t" \
|
||||
"lwi r5, %5 \n\t" \
|
||||
"lwi r6, %6 \n\t" \
|
||||
"andi r7, r6, 0xffff \n\t" \
|
||||
"bsrli r6, r6, 16 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lhui r8, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"lhui r9, r3, 0 \n\t" \
|
||||
"addi r3, r3, 2 \n\t" \
|
||||
"mul r10, r9, r6 \n\t" \
|
||||
"mul r11, r8, r7 \n\t" \
|
||||
"mul r12, r9, r7 \n\t" \
|
||||
"mul r13, r8, r6 \n\t" \
|
||||
"bsrli r8, r10, 16 \n\t" \
|
||||
"bsrli r9, r11, 16 \n\t" \
|
||||
"add r13, r13, r8 \n\t" \
|
||||
"add r13, r13, r9 \n\t" \
|
||||
"bslli r10, r10, 16 \n\t" \
|
||||
"bslli r11, r11, 16 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r11 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"lwi r10, r4, 0 \n\t" \
|
||||
"add r12, r12, r10 \n\t" \
|
||||
"addc r13, r13, r0 \n\t" \
|
||||
"add r12, r12, r5 \n\t" \
|
||||
"addc r5, r13, r0 \n\t" \
|
||||
"swi r12, r4, 0 \n\t" \
|
||||
"addi r4, r4, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"swi r5, %0 \n\t" \
|
||||
"swi r4, %1 \n\t" \
|
||||
"swi r3, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r3", "r4" "r5", "r6", "r7", "r8", \
|
||||
"r9", "r10", "r11", "r12", "r13" \
|
||||
);
|
||||
|
||||
#endif /* MicroBlaze */
|
||||
|
||||
#if defined(__tricore__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ld.a %%a2, %3 \n\t" \
|
||||
"ld.a %%a3, %4 \n\t" \
|
||||
"ld.w %%d4, %5 \n\t" \
|
||||
"ld.w %%d1, %6 \n\t" \
|
||||
"xor %%d5, %%d5 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ld.w %%d0, [%%a2+] \n\t" \
|
||||
"madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \
|
||||
"ld.w %%d0, [%%a3] \n\t" \
|
||||
"addx %%d2, %%d2, %%d0 \n\t" \
|
||||
"addc %%d3, %%d3, 0 \n\t" \
|
||||
"mov %%d4, %%d3 \n\t" \
|
||||
"st.w [%%a3+], %%d2 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"st.w %0, %%d4 \n\t" \
|
||||
"st.a %1, %%a3 \n\t" \
|
||||
"st.a %2, %%a2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "d0", "d1", "e2", "d4", "a2", "a3" \
|
||||
);
|
||||
|
||||
#endif /* TriCore */
|
||||
|
||||
#if defined(__arm__)
|
||||
|
||||
#if defined(__thumb__) && !defined(__thumb2__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t" \
|
||||
"lsr r7, r3, #16 \n\t" \
|
||||
"mov r9, r7 \n\t" \
|
||||
"lsl r7, r3, #16 \n\t" \
|
||||
"lsr r7, r7, #16 \n\t" \
|
||||
"mov r8, r7 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldmia r0!, {r6} \n\t" \
|
||||
"lsr r7, r6, #16 \n\t" \
|
||||
"lsl r6, r6, #16 \n\t" \
|
||||
"lsr r6, r6, #16 \n\t" \
|
||||
"mov r4, r8 \n\t" \
|
||||
"mul r4, r6 \n\t" \
|
||||
"mov r3, r9 \n\t" \
|
||||
"mul r6, r3 \n\t" \
|
||||
"mov r5, r9 \n\t" \
|
||||
"mul r5, r7 \n\t" \
|
||||
"mov r3, r8 \n\t" \
|
||||
"mul r7, r3 \n\t" \
|
||||
"lsr r3, r6, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"lsr r3, r7, #16 \n\t" \
|
||||
"add r5, r5, r3 \n\t" \
|
||||
"add r4, r4, r2 \n\t" \
|
||||
"mov r2, #0 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r6, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"lsl r3, r7, #16 \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r5, r2 \n\t" \
|
||||
"ldr r3, [r1] \n\t" \
|
||||
"add r4, r4, r3 \n\t" \
|
||||
"adc r2, r5 \n\t" \
|
||||
"stmia r1!, {r4} \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "r8", "r9", "cc" \
|
||||
);
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldr r0, %3 \n\t" \
|
||||
"ldr r1, %4 \n\t" \
|
||||
"ldr r2, %5 \n\t" \
|
||||
"ldr r3, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldr r4, [r0], #4 \n\t" \
|
||||
"mov r5, #0 \n\t" \
|
||||
"ldr r6, [r1] \n\t" \
|
||||
"umlal r2, r5, r3, r4 \n\t" \
|
||||
"adds r7, r6, r2 \n\t" \
|
||||
"adc r2, r5, #0 \n\t" \
|
||||
"str r7, [r1], #4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"str r2, %0 \n\t" \
|
||||
"str r1, %1 \n\t" \
|
||||
"str r0, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "r0", "r1", "r2", "r3", "r4", "r5", \
|
||||
"r6", "r7", "cc" \
|
||||
);
|
||||
|
||||
#endif /* Thumb */
|
||||
|
||||
#endif /* ARMv3 */
|
||||
|
||||
#if defined(__alpha__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"ldq $1, %3 \n\t" \
|
||||
"ldq $2, %4 \n\t" \
|
||||
"ldq $3, %5 \n\t" \
|
||||
"ldq $4, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"ldq $6, 0($1) \n\t" \
|
||||
"addq $1, 8, $1 \n\t" \
|
||||
"mulq $6, $4, $7 \n\t" \
|
||||
"umulh $6, $4, $6 \n\t" \
|
||||
"addq $7, $3, $7 \n\t" \
|
||||
"cmpult $7, $3, $3 \n\t" \
|
||||
"ldq $5, 0($2) \n\t" \
|
||||
"addq $7, $5, $7 \n\t" \
|
||||
"cmpult $7, $5, $5 \n\t" \
|
||||
"stq $7, 0($2) \n\t" \
|
||||
"addq $2, 8, $2 \n\t" \
|
||||
"addq $6, $3, $3 \n\t" \
|
||||
"addq $5, $3, $3 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"stq $3, %0 \n\t" \
|
||||
"stq $2, %1 \n\t" \
|
||||
"stq $1, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$1", "$2", "$3", "$4", "$5", "$6", "$7" \
|
||||
);
|
||||
#endif /* Alpha */
|
||||
|
||||
#if defined(__mips__) && !defined(__mips64)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
asm( \
|
||||
"lw $10, %3 \n\t" \
|
||||
"lw $11, %4 \n\t" \
|
||||
"lw $12, %5 \n\t" \
|
||||
"lw $13, %6 \n\t"
|
||||
|
||||
#define MULADDC_CORE \
|
||||
"lw $14, 0($10) \n\t" \
|
||||
"multu $13, $14 \n\t" \
|
||||
"addi $10, $10, 4 \n\t" \
|
||||
"mflo $14 \n\t" \
|
||||
"mfhi $9 \n\t" \
|
||||
"addu $14, $12, $14 \n\t" \
|
||||
"lw $15, 0($11) \n\t" \
|
||||
"sltu $12, $14, $12 \n\t" \
|
||||
"addu $15, $14, $15 \n\t" \
|
||||
"sltu $14, $15, $14 \n\t" \
|
||||
"addu $12, $12, $9 \n\t" \
|
||||
"sw $15, 0($11) \n\t" \
|
||||
"addu $12, $12, $14 \n\t" \
|
||||
"addi $11, $11, 4 \n\t"
|
||||
|
||||
#define MULADDC_STOP \
|
||||
"sw $12, %0 \n\t" \
|
||||
"sw $11, %1 \n\t" \
|
||||
"sw $10, %2 \n\t" \
|
||||
: "=m" (c), "=m" (d), "=m" (s) \
|
||||
: "m" (s), "m" (d), "m" (c), "m" (b) \
|
||||
: "$9", "$10", "$11", "$12", "$13", "$14", "$15" \
|
||||
);
|
||||
|
||||
#endif /* MIPS */
|
||||
#endif /* GNUC */
|
||||
|
||||
#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
__asm mov esi, s \
|
||||
__asm mov edi, d \
|
||||
__asm mov ecx, c \
|
||||
__asm mov ebx, b
|
||||
|
||||
#define MULADDC_CORE \
|
||||
__asm lodsd \
|
||||
__asm mul ebx \
|
||||
__asm add eax, ecx \
|
||||
__asm adc edx, 0 \
|
||||
__asm add eax, [edi] \
|
||||
__asm adc edx, 0 \
|
||||
__asm mov ecx, edx \
|
||||
__asm stosd
|
||||
|
||||
#if defined(POLARSSL_HAVE_SSE2)
|
||||
|
||||
#define EMIT __asm _emit
|
||||
|
||||
#define MULADDC_HUIT \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC9 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0xC3 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x1F \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x16 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xEE \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xFC \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x0F \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD0 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xE0 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xF0 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xF4 EMIT 0xD8 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCD \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xD5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCF \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xE5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCA \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xF5 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCC \
|
||||
EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xDD \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCE \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0xD4 EMIT 0xCB \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C \
|
||||
EMIT 0x83 EMIT 0xC7 EMIT 0x20 \
|
||||
EMIT 0x83 EMIT 0xC6 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 \
|
||||
EMIT 0x0F EMIT 0x7E EMIT 0xC9
|
||||
|
||||
#define MULADDC_STOP \
|
||||
EMIT 0x0F EMIT 0x77 \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#else
|
||||
|
||||
#define MULADDC_STOP \
|
||||
__asm mov c, ecx \
|
||||
__asm mov d, edi \
|
||||
__asm mov s, esi \
|
||||
|
||||
#endif /* SSE2 */
|
||||
#endif /* MSVC */
|
||||
|
||||
#endif /* POLARSSL_HAVE_ASM */
|
||||
|
||||
#if !defined(MULADDC_CORE)
|
||||
#if defined(POLARSSL_HAVE_UDBL)
|
||||
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
t_udbl r; \
|
||||
t_uint r0, r1;
|
||||
|
||||
#define MULADDC_CORE \
|
||||
r = *(s++) * (t_udbl) b; \
|
||||
r0 = (t_uint) r; \
|
||||
r1 = (t_uint)( r >> biL ); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#else
|
||||
#define MULADDC_INIT \
|
||||
{ \
|
||||
t_uint s0, s1, b0, b1; \
|
||||
t_uint r0, r1, rx, ry; \
|
||||
b0 = ( b << biH ) >> biH; \
|
||||
b1 = ( b >> biH );
|
||||
|
||||
#define MULADDC_CORE \
|
||||
s0 = ( *s << biH ) >> biH; \
|
||||
s1 = ( *s >> biH ); s++; \
|
||||
rx = s0 * b1; r0 = s0 * b0; \
|
||||
ry = s1 * b0; r1 = s1 * b1; \
|
||||
r1 += ( rx >> biH ); \
|
||||
r1 += ( ry >> biH ); \
|
||||
rx <<= biH; ry <<= biH; \
|
||||
r0 += rx; r1 += (r0 < rx); \
|
||||
r0 += ry; r1 += (r0 < ry); \
|
||||
r0 += c; r1 += (r0 < c); \
|
||||
r0 += *d; r1 += (r0 < *d); \
|
||||
c = r1; *(d++) = r0;
|
||||
|
||||
#define MULADDC_STOP \
|
||||
}
|
||||
|
||||
#endif /* C (generic) */
|
||||
#endif /* C (longlong) */
|
||||
|
||||
#endif /* bn_mul.h */
|
||||
@@ -0,0 +1,495 @@
|
||||
/**
|
||||
* \file check_config.h
|
||||
*
|
||||
* \brief Consistency checks for configuration options
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
/*
|
||||
* It is recommended to include this file from your config.h
|
||||
* in order to catch dependency issues early.
|
||||
*/
|
||||
|
||||
#ifndef POLARSSL_CHECK_CONFIG_H
|
||||
#define POLARSSL_CHECK_CONFIG_H
|
||||
|
||||
#if defined(POLARSSL_DEPRECATED_WARNING) && \
|
||||
!defined(__GNUC__) && !defined(__clang__)
|
||||
#error "POLARSSL_DEPRECATED_WARNING only works with GCC and Clang"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_NET_C) && !defined(POLARSSL_HAVE_IPV6)
|
||||
#if defined(POLARSSL_DEPRECATED_WARNING)
|
||||
#warning "Using POLARSSL_NET_C without POLARSSL_HAVE_IPV6 is deprecated"
|
||||
#endif
|
||||
#if defined(POLARSSL_DEPRECATED_REMOVED)
|
||||
#define POLARSSL_HAVE_IPV6
|
||||
#endif
|
||||
#endif /* POLARSSL_NET_C && !POLARSSL_HAVE_IPV6 */
|
||||
|
||||
#if defined(POLARSSL_AESNI_C) && !defined(POLARSSL_HAVE_ASM)
|
||||
#error "POLARSSL_AESNI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CERTS_C) && !defined(POLARSSL_PEM_PARSE_C)
|
||||
#error "POLARSSL_CERTS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_CTR_DRBG_C) && !defined(POLARSSL_AES_C)
|
||||
#error "POLARSSL_CTR_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_DHM_C) && !defined(POLARSSL_BIGNUM_C)
|
||||
#error "POLARSSL_DHM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDH_C) && !defined(POLARSSL_ECP_C)
|
||||
#error "POLARSSL_ECDH_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDSA_C) && \
|
||||
( !defined(POLARSSL_ECP_C) || \
|
||||
!defined(POLARSSL_ASN1_PARSE_C) || \
|
||||
!defined(POLARSSL_ASN1_WRITE_C) )
|
||||
#error "POLARSSL_ECDSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECDSA_DETERMINISTIC) && !defined(POLARSSL_HMAC_DRBG_C)
|
||||
#error "POLARSSL_ECDSA_DETERMINISTIC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ECP_C) && ( !defined(POLARSSL_BIGNUM_C) || ( \
|
||||
!defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP256R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP384R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_BP512R1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) && \
|
||||
!defined(POLARSSL_ECP_DP_SECP256K1_ENABLED) ) )
|
||||
#error "POLARSSL_ECP_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_ENTROPY_C) && (!defined(POLARSSL_SHA512_C) && \
|
||||
!defined(POLARSSL_SHA256_C))
|
||||
#error "POLARSSL_ENTROPY_C defined, but not all prerequisites"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && defined(POLARSSL_SHA512_C) && \
|
||||
defined(CTR_DRBG_ENTROPY_LEN) && (CTR_DRBG_ENTROPY_LEN > 64)
|
||||
#error "CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && \
|
||||
( !defined(POLARSSL_SHA512_C) || defined(POLARSSL_ENTROPY_FORCE_SHA256) ) \
|
||||
&& defined(CTR_DRBG_ENTROPY_LEN) && (CTR_DRBG_ENTROPY_LEN > 32)
|
||||
#error "CTR_DRBG_ENTROPY_LEN value too high"
|
||||
#endif
|
||||
#if defined(POLARSSL_ENTROPY_C) && \
|
||||
defined(POLARSSL_ENTROPY_FORCE_SHA256) && !defined(POLARSSL_SHA256_C)
|
||||
#error "POLARSSL_ENTROPY_FORCE_SHA256 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_GCM_C) && ( \
|
||||
!defined(POLARSSL_AES_C) && !defined(POLARSSL_CAMELLIA_C) )
|
||||
#error "POLARSSL_GCM_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HAVEGE_C) && !defined(POLARSSL_TIMING_C)
|
||||
#error "POLARSSL_HAVEGE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_HMAC_DRBG) && !defined(POLARSSL_MD_C)
|
||||
#error "POLARSSL_HMAC_DRBG_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) && !defined(POLARSSL_DHM_C)
|
||||
#error "POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
|
||||
!defined(POLARSSL_ECDH_C)
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_DHM_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_RSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) || !defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
|
||||
( !defined(POLARSSL_ECDH_C) || !defined(POLARSSL_ECDSA_C) || \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
|
||||
!defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_X509_CRT_PARSE_C) || \
|
||||
!defined(POLARSSL_PKCS1_V15) )
|
||||
#error "POLARSSL_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_MEMORY_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && \
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PADLOCK_C) && !defined(POLARSSL_HAVE_ASM)
|
||||
#error "POLARSSL_PADLOCK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PBKDF2_C) && !defined(POLARSSL_MD_C)
|
||||
#error "POLARSSL_PBKDF2_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_PARSE_C) && !defined(POLARSSL_BASE64_C)
|
||||
#error "POLARSSL_PEM_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PEM_WRITE_C) && !defined(POLARSSL_BASE64_C)
|
||||
#error "POLARSSL_PEM_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_C) && \
|
||||
( !defined(POLARSSL_RSA_C) && !defined(POLARSSL_ECP_C) )
|
||||
#error "POLARSSL_PK_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_PARSE_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PK_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PK_WRITE_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PK_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PKCS11_C) && !defined(POLARSSL_PK_C)
|
||||
#error "POLARSSL_PKCS11_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_EXIT_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_EXIT_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_EXIT) ||\
|
||||
defined(POLARSSL_PLATFORM_EXIT_ALT) )
|
||||
#error "POLARSSL_PLATFORM_EXIT_MACRO and POLARSSL_PLATFORM_STD_EXIT/POLARSSL_PLATFORM_EXIT_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_FPRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_FPRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_FPRINTF_MACRO and POLARSSL_PLATFORM_STD_FPRINTF/POLARSSL_PLATFORM_FPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) &&\
|
||||
defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO and POLARSSL_PLATFORM_STD_FREE cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) && !defined(POLARSSL_PLATFORM_MALLOC_MACRO)
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO must be defined if POLARSSL_PLATFORM_FREE_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\
|
||||
( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) )
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) &&\
|
||||
defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
#error "POLARSSL_PLATFORM_MALLOC_MACRO and POLARSSL_PLATFORM_STD_MALLOC cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MALLOC_MACRO) && !defined(POLARSSL_PLATFORM_FREE_MACRO)
|
||||
#error "POLARSSL_PLATFORM_FREE_MACRO must be defined if POLARSSL_PLATFORM_MALLOC_MACRO is"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_MEMORY defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_PRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_PRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_PRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_PRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_PRINTF_MACRO and POLARSSL_PLATFORM_STD_PRINTF/POLARSSL_PLATFORM_PRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT) && ( defined(_WIN32)\
|
||||
&& !defined(EFIX64) && !defined(EFI32) )
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_ALT defined but not available on Windows"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) && !defined(POLARSSL_PLATFORM_C)
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_MACRO defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO) &&\
|
||||
( defined(POLARSSL_PLATFORM_STD_SNPRINTF) ||\
|
||||
defined(POLARSSL_PLATFORM_SNPRINTF_ALT) )
|
||||
#error "POLARSSL_PLATFORM_SNPRINTF_MACRO and POLARSSL_PLATFORM_STD_SNPRINTF/POLARSSL_PLATFORM_SNPRINTF_ALT cannot be defined simultaneously"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MEM_HDR) &&\
|
||||
!defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
|
||||
#error "POLARSSL_PLATFORM_STD_MEM_HDR defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_MALLOC) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_MALLOC defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_FREE) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#error "POLARSSL_PLATFORM_STD_FREE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_EXIT) &&\
|
||||
!defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_EXIT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_FPRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_FPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_PRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_PRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_STD_SNPRINTF) &&\
|
||||
!defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
#error "POLARSSL_PLATFORM_STD_SNPRINTF defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_RSA_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) )
|
||||
#error "POLARSSL_RSA_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT) && \
|
||||
( !defined(POLARSSL_RSA_C) || !defined(POLARSSL_PKCS1_V21) )
|
||||
#error "POLARSSL_X509_RSASSA_PSS_SUPPORT defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_SSL3) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_SSL3 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1_1) && ( !defined(POLARSSL_MD5_C) || \
|
||||
!defined(POLARSSL_SHA1_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1_1 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_PROTO_TLS1_2) && ( !defined(POLARSSL_SHA1_C) && \
|
||||
!defined(POLARSSL_SHA256_C) && !defined(POLARSSL_SHA512_C) )
|
||||
#error "POLARSSL_SSL_PROTO_TLS1_2 defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_CLI_C) && !defined(POLARSSL_SSL_TLS_C)
|
||||
#error "POLARSSL_SSL_CLI_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && ( !defined(POLARSSL_CIPHER_C) || \
|
||||
!defined(POLARSSL_MD_C) )
|
||||
#error "POLARSSL_SSL_TLS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SRV_C) && !defined(POLARSSL_SSL_TLS_C)
|
||||
#error "POLARSSL_SSL_SRV_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (!defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && !defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2))
|
||||
#error "POLARSSL_SSL_TLS_C defined, but no protocols are active"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_1) && !defined(POLARSSL_SSL_PROTO_TLS1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_2) && !defined(POLARSSL_SSL_PROTO_TLS1_1))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_TLS_C) && (defined(POLARSSL_SSL_PROTO_SSL3) && \
|
||||
defined(POLARSSL_SSL_PROTO_TLS1_2) && (!defined(POLARSSL_SSL_PROTO_TLS1) || \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1)))
|
||||
#error "Illegal protocol selection"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2)
|
||||
#error "POLARSSL_SSL_ENCRYPT_THEN_MAC defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_1) && \
|
||||
!defined(POLARSSL_SSL_PROTO_TLS1_2)
|
||||
#error "POLARSSL_SSL_EXTENDED_MASTER_SECRET defined, but not all prerequsites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SESSION_TICKETS) && defined(POLARSSL_SSL_TLS_C) && \
|
||||
( !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \
|
||||
!defined(POLARSSL_CIPHER_MODE_CBC) )
|
||||
#error "POLARSSL_SSL_SESSION_TICKETS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING) && \
|
||||
!defined(POLARSSL_SSL_PROTO_SSL3) && !defined(POLARSSL_SSL_PROTO_TLS1)
|
||||
#error "POLARSSL_SSL_CBC_RECORD_SPLITTING defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION) && \
|
||||
!defined(POLARSSL_X509_CRT_PARSE_C)
|
||||
#error "POLARSSL_SSL_SERVER_NAME_INDICATION defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_PTHREAD)
|
||||
#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_PTHREAD defined, but not all prerequisites"
|
||||
#endif
|
||||
#define POLARSSL_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_ALT)
|
||||
#if !defined(POLARSSL_THREADING_C) || defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_ALT defined, but not all prerequisites"
|
||||
#endif
|
||||
#define POLARSSL_THREADING_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_C) && !defined(POLARSSL_THREADING_IMPL)
|
||||
#error "POLARSSL_THREADING_C defined, single threading implementation required"
|
||||
#endif
|
||||
#undef POLARSSL_THREADING_IMPL
|
||||
|
||||
#if defined(POLARSSL_VERSION_FEATURES) && !defined(POLARSSL_VERSION_C)
|
||||
#error "POLARSSL_VERSION_FEATURES defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_USE_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) || !defined(POLARSSL_ASN1_PARSE_C) || \
|
||||
!defined(POLARSSL_PK_PARSE_C) )
|
||||
#error "POLARSSL_X509_USE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CREATE_C) && ( !defined(POLARSSL_BIGNUM_C) || \
|
||||
!defined(POLARSSL_OID_C) || !defined(POLARSSL_ASN1_WRITE_C) || \
|
||||
!defined(POLARSSL_PK_WRITE_C) )
|
||||
#error "POLARSSL_X509_CREATE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRT_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CRT_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRL_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CRL_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CSR_PARSE_C) && ( !defined(POLARSSL_X509_USE_C) )
|
||||
#error "POLARSSL_X509_CSR_PARSE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CRT_WRITE_C) && ( !defined(POLARSSL_X509_CREATE_C) )
|
||||
#error "POLARSSL_X509_CRT_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_X509_CSR_WRITE_C) && ( !defined(POLARSSL_X509_CREATE_C) )
|
||||
#error "POLARSSL_X509_CSR_WRITE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#endif /* POLARSSL_CHECK_CONFIG_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,734 @@
|
||||
/*
|
||||
* Buffer-based memory allocator
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "memory_buffer_alloc.h"
|
||||
|
||||
/* No need for the header guard as POLARSSL_MEMORY_BUFFER_ALLOC_C
|
||||
is dependent upon POLARSSL_PLATFORM_C */
|
||||
#include "platform.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
#include <execinfo.h>
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
#include "polarssl/threading.h"
|
||||
#endif
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void polarssl_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
#define MAGIC1 0xFF00AA55
|
||||
#define MAGIC2 0xEE119966
|
||||
#define MAX_BT 20
|
||||
|
||||
typedef struct _memory_header memory_header;
|
||||
struct _memory_header
|
||||
{
|
||||
size_t magic1;
|
||||
size_t size;
|
||||
size_t alloc;
|
||||
memory_header *prev;
|
||||
memory_header *next;
|
||||
memory_header *prev_free;
|
||||
memory_header *next_free;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
char **trace;
|
||||
size_t trace_count;
|
||||
#endif
|
||||
size_t magic2;
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
memory_header *first;
|
||||
memory_header *first_free;
|
||||
int verify;
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
size_t malloc_count;
|
||||
size_t free_count;
|
||||
size_t total_used;
|
||||
size_t maximum_used;
|
||||
size_t header_count;
|
||||
size_t maximum_header_count;
|
||||
#endif
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
threading_mutex_t mutex;
|
||||
#endif
|
||||
}
|
||||
buffer_alloc_ctx;
|
||||
|
||||
static buffer_alloc_ctx heap;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
static void debug_header( memory_header *hdr )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
size_t i;
|
||||
#endif
|
||||
|
||||
polarssl_fprintf( stderr, "HDR: PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
|
||||
"ALLOC(%zu), SIZE(%10zu)\n",
|
||||
(size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
|
||||
hdr->alloc, hdr->size );
|
||||
polarssl_fprintf( stderr, " FPREV(%10zu), FNEXT(%10zu)\n",
|
||||
(size_t) hdr->prev_free, (size_t) hdr->next_free );
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
polarssl_fprintf( stderr, "TRACE: \n" );
|
||||
for( i = 0; i < hdr->trace_count; i++ )
|
||||
polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
|
||||
polarssl_fprintf( stderr, "\n" );
|
||||
#endif
|
||||
}
|
||||
|
||||
static void debug_chain()
|
||||
{
|
||||
memory_header *cur = heap.first;
|
||||
|
||||
polarssl_fprintf( stderr, "\nBlock list\n" );
|
||||
while( cur != NULL )
|
||||
{
|
||||
debug_header( cur );
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
polarssl_fprintf( stderr, "Free list\n" );
|
||||
cur = heap.first_free;
|
||||
|
||||
while( cur != NULL )
|
||||
{
|
||||
debug_header( cur );
|
||||
cur = cur->next_free;
|
||||
}
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
static int verify_header( memory_header *hdr )
|
||||
{
|
||||
if( hdr->magic1 != MAGIC1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->magic2 != MAGIC2 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->alloc > 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev != NULL && hdr->prev == hdr->next )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev == next\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int verify_chain()
|
||||
{
|
||||
memory_header *prv = heap.first, *cur = heap.first->next;
|
||||
|
||||
if( verify_header( heap.first ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of first header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( heap.first->prev != NULL )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
"first->prev != NULL\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( verify_header( cur ) != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification of header "
|
||||
"failed\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
if( cur->prev != prv )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: verification failed: "
|
||||
"cur->prev != prv\n" );
|
||||
#endif
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
prv = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static void *buffer_alloc_malloc( size_t len )
|
||||
{
|
||||
memory_header *new, *cur = heap.first_free;
|
||||
unsigned char *p;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
void *trace_buffer[MAX_BT];
|
||||
size_t trace_cnt;
|
||||
#endif
|
||||
|
||||
if( heap.buf == NULL || heap.first == NULL )
|
||||
return( NULL );
|
||||
|
||||
if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
// Find block that fits
|
||||
//
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( cur->size >= len )
|
||||
break;
|
||||
|
||||
cur = cur->next_free;
|
||||
}
|
||||
|
||||
if( cur == NULL )
|
||||
return( NULL );
|
||||
|
||||
if( cur->alloc != 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.malloc_count++;
|
||||
#endif
|
||||
|
||||
// Found location, split block if > memory_header + 4 room left
|
||||
//
|
||||
if( cur->size - len < sizeof(memory_header) +
|
||||
POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
cur->alloc = 1;
|
||||
|
||||
// Remove from free_list
|
||||
//
|
||||
if( cur->prev_free != NULL )
|
||||
cur->prev_free->next_free = cur->next_free;
|
||||
else
|
||||
heap.first_free = cur->next_free;
|
||||
|
||||
if( cur->next_free != NULL )
|
||||
cur->next_free->prev_free = cur->prev_free;
|
||||
|
||||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.total_used += cur->size;
|
||||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
||||
p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
|
||||
new = (memory_header *) p;
|
||||
|
||||
new->size = cur->size - len - sizeof(memory_header);
|
||||
new->alloc = 0;
|
||||
new->prev = cur;
|
||||
new->next = cur->next;
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
new->trace = NULL;
|
||||
new->trace_count = 0;
|
||||
#endif
|
||||
new->magic1 = MAGIC1;
|
||||
new->magic2 = MAGIC2;
|
||||
|
||||
if( new->next != NULL )
|
||||
new->next->prev = new;
|
||||
|
||||
// Replace cur with new in free_list
|
||||
//
|
||||
new->prev_free = cur->prev_free;
|
||||
new->next_free = cur->next_free;
|
||||
if( new->prev_free != NULL )
|
||||
new->prev_free->next_free = new;
|
||||
else
|
||||
heap.first_free = new;
|
||||
|
||||
if( new->next_free != NULL )
|
||||
new->next_free->prev_free = new;
|
||||
|
||||
cur->alloc = 1;
|
||||
cur->size = len;
|
||||
cur->next = new;
|
||||
cur->prev_free = NULL;
|
||||
cur->next_free = NULL;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count++;
|
||||
if( heap.header_count > heap.maximum_header_count )
|
||||
heap.maximum_header_count = heap.header_count;
|
||||
heap.total_used += cur->size;
|
||||
if( heap.total_used > heap.maximum_used )
|
||||
heap.maximum_used = heap.total_used;
|
||||
#endif
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
trace_cnt = backtrace( trace_buffer, MAX_BT );
|
||||
cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
|
||||
cur->trace_count = trace_cnt;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
return( ( (unsigned char *) cur ) + sizeof(memory_header) );
|
||||
}
|
||||
|
||||
static void buffer_alloc_free( void *ptr )
|
||||
{
|
||||
memory_header *hdr, *old = NULL;
|
||||
unsigned char *p = (unsigned char *) ptr;
|
||||
|
||||
if( ptr == NULL || heap.buf == NULL || heap.first == NULL )
|
||||
return;
|
||||
|
||||
if( p < heap.buf || p > heap.buf + heap.len )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
|
||||
"space\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
p -= sizeof(memory_header);
|
||||
hdr = (memory_header *) p;
|
||||
|
||||
if( verify_header( hdr ) != 0 )
|
||||
polarssl_exit( 1 );
|
||||
|
||||
if( hdr->alloc != 1 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
|
||||
"data\n" );
|
||||
#endif
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
hdr->alloc = 0;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.free_count++;
|
||||
heap.total_used -= hdr->size;
|
||||
#endif
|
||||
|
||||
// Regroup with block before
|
||||
//
|
||||
if( hdr->prev != NULL && hdr->prev->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->prev->size += sizeof(memory_header) + hdr->size;
|
||||
hdr->prev->next = hdr->next;
|
||||
old = hdr;
|
||||
hdr = hdr->prev;
|
||||
|
||||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
}
|
||||
|
||||
// Regroup with block after
|
||||
//
|
||||
if( hdr->next != NULL && hdr->next->alloc == 0 )
|
||||
{
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.header_count--;
|
||||
#endif
|
||||
hdr->size += sizeof(memory_header) + hdr->next->size;
|
||||
old = hdr->next;
|
||||
hdr->next = hdr->next->next;
|
||||
|
||||
if( hdr->prev_free != NULL || hdr->next_free != NULL )
|
||||
{
|
||||
if( hdr->prev_free != NULL )
|
||||
hdr->prev_free->next_free = hdr->next_free;
|
||||
else
|
||||
heap.first_free = hdr->next_free;
|
||||
|
||||
if( hdr->next_free != NULL )
|
||||
hdr->next_free->prev_free = hdr->prev_free;
|
||||
}
|
||||
|
||||
hdr->prev_free = old->prev_free;
|
||||
hdr->next_free = old->next_free;
|
||||
|
||||
if( hdr->prev_free != NULL )
|
||||
hdr->prev_free->next_free = hdr;
|
||||
else
|
||||
heap.first_free = hdr;
|
||||
|
||||
if( hdr->next_free != NULL )
|
||||
hdr->next_free->prev_free = hdr;
|
||||
|
||||
if( hdr->next != NULL )
|
||||
hdr->next->prev = hdr;
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
free( old->trace );
|
||||
#endif
|
||||
memset( old, 0, sizeof(memory_header) );
|
||||
}
|
||||
|
||||
// Prepend to free_list if we have not merged
|
||||
// (Does not have to stay in same order as prev / next list)
|
||||
//
|
||||
if( old == NULL )
|
||||
{
|
||||
hdr->next_free = heap.first_free;
|
||||
if( heap.first_free != NULL )
|
||||
heap.first_free->prev_free = hdr;
|
||||
heap.first_free = hdr;
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_BACKTRACE)
|
||||
hdr->trace = NULL;
|
||||
hdr->trace_count = 0;
|
||||
#endif
|
||||
|
||||
if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
|
||||
polarssl_exit( 1 );
|
||||
}
|
||||
|
||||
void memory_buffer_set_verify( int verify )
|
||||
{
|
||||
heap.verify = verify;
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_verify()
|
||||
{
|
||||
return verify_chain();
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
void memory_buffer_alloc_status()
|
||||
{
|
||||
polarssl_fprintf( stderr,
|
||||
"Current use: %zu blocks / %zu bytes, max: %zu blocks / "
|
||||
"%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
|
||||
heap.header_count, heap.total_used,
|
||||
heap.maximum_header_count, heap.maximum_used,
|
||||
heap.maximum_header_count * sizeof( memory_header )
|
||||
+ heap.maximum_used,
|
||||
heap.malloc_count, heap.free_count );
|
||||
|
||||
if( heap.first->next == NULL )
|
||||
polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
|
||||
else
|
||||
{
|
||||
polarssl_fprintf( stderr, "Memory currently allocated:\n" );
|
||||
debug_chain();
|
||||
}
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
|
||||
{
|
||||
*max_used = heap.maximum_used;
|
||||
*max_blocks = heap.maximum_header_count;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_max_reset( void )
|
||||
{
|
||||
heap.maximum_used = 0;
|
||||
heap.maximum_header_count = 0;
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
|
||||
{
|
||||
*cur_used = heap.total_used;
|
||||
*cur_blocks = heap.header_count;
|
||||
}
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
static void *buffer_alloc_malloc_mutexed( size_t len )
|
||||
{
|
||||
void *buf;
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
buf = buffer_alloc_malloc( len );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
return( buf );
|
||||
}
|
||||
|
||||
static void buffer_alloc_free_mutexed( void *ptr )
|
||||
{
|
||||
polarssl_mutex_lock( &heap.mutex );
|
||||
buffer_alloc_free( ptr );
|
||||
polarssl_mutex_unlock( &heap.mutex );
|
||||
}
|
||||
#endif /* POLARSSL_THREADING_C */
|
||||
|
||||
int memory_buffer_alloc_init( unsigned char *buf, size_t len )
|
||||
{
|
||||
memset( &heap, 0, sizeof(buffer_alloc_ctx) );
|
||||
memset( buf, 0, len );
|
||||
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_init( &heap.mutex );
|
||||
platform_set_malloc_free( buffer_alloc_malloc_mutexed,
|
||||
buffer_alloc_free_mutexed );
|
||||
#else
|
||||
platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
|
||||
#endif
|
||||
|
||||
if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
|
||||
{
|
||||
/* Adjust len first since buf is used in the computation */
|
||||
len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
|
||||
- (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
|
||||
}
|
||||
|
||||
heap.buf = buf;
|
||||
heap.len = len;
|
||||
|
||||
heap.first = (memory_header *) buf;
|
||||
heap.first->size = len - sizeof(memory_header);
|
||||
heap.first->magic1 = MAGIC1;
|
||||
heap.first->magic2 = MAGIC2;
|
||||
heap.first_free = heap.first;
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
void memory_buffer_alloc_free()
|
||||
{
|
||||
#if defined(POLARSSL_THREADING_C)
|
||||
polarssl_mutex_free( &heap.mutex );
|
||||
#endif
|
||||
polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
static int check_pointer( void *p )
|
||||
{
|
||||
if( p == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
|
||||
return( -1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int check_all_free( )
|
||||
{
|
||||
if(
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
heap.total_used != 0 ||
|
||||
#endif
|
||||
heap.first != heap.first_free ||
|
||||
(void *) heap.first != (void *) heap.buf )
|
||||
{
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define TEST_ASSERT( condition ) \
|
||||
if( ! (condition) ) \
|
||||
{ \
|
||||
if( verbose != 0 ) \
|
||||
polarssl_printf( "failed\n" ); \
|
||||
\
|
||||
ret = 1; \
|
||||
goto cleanup; \
|
||||
}
|
||||
|
||||
int memory_buffer_alloc_self_test( int verbose )
|
||||
{
|
||||
unsigned char buf[1024];
|
||||
unsigned char *p, *q, *r, *end;
|
||||
int ret = 0;
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #1 (basic alloc-free cycle): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
/* Memorize end to compare with the next test */
|
||||
end = heap.buf + heap.len;
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #2 (buf not aligned): " );
|
||||
|
||||
memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
|
||||
|
||||
TEST_ASSERT( heap.buf + heap.len == end );
|
||||
|
||||
p = polarssl_malloc( 1 );
|
||||
q = polarssl_malloc( 128 );
|
||||
r = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 &&
|
||||
check_pointer( q ) == 0 &&
|
||||
check_pointer( r ) == 0 );
|
||||
|
||||
polarssl_free( r );
|
||||
polarssl_free( q );
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " MBA test #3 (full): " );
|
||||
|
||||
memory_buffer_alloc_init( buf, sizeof( buf ) );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
|
||||
p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
|
||||
q = polarssl_malloc( 16 );
|
||||
|
||||
TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
|
||||
TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
|
||||
|
||||
polarssl_free( q );
|
||||
|
||||
TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
|
||||
|
||||
polarssl_free( p );
|
||||
|
||||
TEST_ASSERT( check_all_free( ) == 0 );
|
||||
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
|
||||
cleanup:
|
||||
memory_buffer_alloc_free( );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
|
||||
#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
|
||||
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* \file memory_buffer_alloc.h
|
||||
*
|
||||
* \brief Buffer-based memory allocator
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_MEMORY_BUFFER_ALLOC_H
|
||||
#define POLARSSL_MEMORY_BUFFER_ALLOC_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* \name SECTION: Module settings
|
||||
*
|
||||
* The configuration options you can set for this module are in this section.
|
||||
* Either change them in config.h or define them on the compiler command line.
|
||||
* \{
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_MEMORY_ALIGN_MULTIPLE)
|
||||
#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */
|
||||
#endif
|
||||
|
||||
/* \} name SECTION: Module settings */
|
||||
|
||||
#define MEMORY_VERIFY_NONE 0
|
||||
#define MEMORY_VERIFY_ALLOC (1 << 0)
|
||||
#define MEMORY_VERIFY_FREE (1 << 1)
|
||||
#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Initialize use of stack-based memory allocator.
|
||||
* The stack-based allocator does memory management inside the
|
||||
* presented buffer and does not call malloc() and free().
|
||||
* It sets the global polarssl_malloc() and polarssl_free() pointers
|
||||
* to its own functions.
|
||||
* (Provided polarssl_malloc() and polarssl_free() are thread-safe if
|
||||
* POLARSSL_THREADING_C is defined)
|
||||
*
|
||||
* \note This code is not optimized and provides a straight-forward
|
||||
* implementation of a stack-based memory allocator.
|
||||
*
|
||||
* \param buf buffer to use as heap
|
||||
* \param len size of the buffer
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int memory_buffer_alloc_init( unsigned char *buf, size_t len );
|
||||
|
||||
/**
|
||||
* \brief Free the mutex for thread-safety and clear remaining memory
|
||||
*/
|
||||
void memory_buffer_alloc_free( void );
|
||||
|
||||
/**
|
||||
* \brief Determine when the allocator should automatically verify the state
|
||||
* of the entire chain of headers / meta-data.
|
||||
* (Default: MEMORY_VERIFY_NONE)
|
||||
*
|
||||
* \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC,
|
||||
* MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS
|
||||
*/
|
||||
void memory_buffer_set_verify( int verify );
|
||||
|
||||
#if defined(POLARSSL_MEMORY_DEBUG)
|
||||
/**
|
||||
* \brief Print out the status of the allocated memory (primarily for use
|
||||
* after a program should have de-allocated all memory)
|
||||
* Prints out a list of 'still allocated' blocks and their stack
|
||||
* trace if POLARSSL_MEMORY_BACKTRACE is defined.
|
||||
*/
|
||||
void memory_buffer_alloc_status( void );
|
||||
|
||||
/**
|
||||
* \brief Get the peak heap usage so far
|
||||
*
|
||||
* \param max_used Peak number of bytes reauested by the application
|
||||
* \param max_blocks Peak number of blocks reauested by the application
|
||||
*/
|
||||
void memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks );
|
||||
|
||||
/**
|
||||
* \brief Reset peak statistics
|
||||
*/
|
||||
void memory_buffer_alloc_max_reset( void );
|
||||
|
||||
/**
|
||||
* \brief Get the current heap usage
|
||||
*
|
||||
* \param cur_used Number of bytes reauested by the application
|
||||
* \param cur_blocks Number of blocks reauested by the application
|
||||
*/
|
||||
void memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks );
|
||||
#endif /* POLARSSL_MEMORY_DEBUG */
|
||||
|
||||
/**
|
||||
* \brief Verifies that all headers in the memory buffer are correct
|
||||
* and contain sane values. Helps debug buffer-overflow errors.
|
||||
*
|
||||
* Prints out first failure if POLARSSL_MEMORY_DEBUG is defined.
|
||||
* Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS
|
||||
* is defined. (Includes stack trace information for each block if
|
||||
* POLARSSL_MEMORY_BACKTRACE is defined as well).
|
||||
*
|
||||
* \returns 0 if verified, 1 otherwise
|
||||
*/
|
||||
int memory_buffer_alloc_verify( void );
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
/**
|
||||
* \brief Checkup routine
|
||||
*
|
||||
* \return 0 if successful, or 1 if a test failed
|
||||
*/
|
||||
int memory_buffer_alloc_self_test( int verbose );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* memory_buffer_alloc.h */
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Platform abstraction layer
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
static void *platform_malloc_uninit( size_t len )
|
||||
{
|
||||
((void) len);
|
||||
return( NULL );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_MALLOC platform_malloc_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_MALLOC */
|
||||
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
static void platform_free_uninit( void *ptr )
|
||||
{
|
||||
((void) ptr);
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_FREE platform_free_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_FREE */
|
||||
|
||||
void * (*polarssl_malloc)( size_t ) = POLARSSL_PLATFORM_STD_MALLOC;
|
||||
void (*polarssl_free)( void * ) = POLARSSL_PLATFORM_STD_FREE;
|
||||
|
||||
int platform_set_malloc_free( void * (*malloc_func)( size_t ),
|
||||
void (*free_func)( void * ) )
|
||||
{
|
||||
polarssl_malloc = malloc_func;
|
||||
polarssl_free = free_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_MEMORY */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_snprintf_uninit( char * s, size_t n,
|
||||
const char * format, ... )
|
||||
{
|
||||
((void) s);
|
||||
((void) n);
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_SNPRINTF platform_snprintf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_SNPRINTF */
|
||||
|
||||
int (*polarssl_snprintf)( char * s, size_t n,
|
||||
const char * format,
|
||||
... ) = POLARSSL_PLATFORM_STD_SNPRINTF;
|
||||
|
||||
int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
||||
const char * format,
|
||||
... ) )
|
||||
{
|
||||
polarssl_snprintf = snprintf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_printf_uninit( const char *format, ... )
|
||||
{
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_PRINTF platform_printf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_PRINTF */
|
||||
|
||||
int (*polarssl_printf)( const char *, ... ) = POLARSSL_PLATFORM_STD_PRINTF;
|
||||
|
||||
int platform_set_printf( int (*printf_func)( const char *, ... ) )
|
||||
{
|
||||
polarssl_printf = printf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static int platform_fprintf_uninit( FILE *stream, const char *format, ... )
|
||||
{
|
||||
((void) stream);
|
||||
((void) format);
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_FPRINTF platform_fprintf_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_FPRINTF */
|
||||
|
||||
int (*polarssl_fprintf)( FILE *, const char *, ... ) =
|
||||
POLARSSL_PLATFORM_STD_FPRINTF;
|
||||
|
||||
int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) )
|
||||
{
|
||||
polarssl_fprintf = fprintf_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
#if !defined(POLARSSL_PLATFORM_STD_EXIT)
|
||||
/*
|
||||
* Make dummy function to prevent NULL pointer dereferences
|
||||
*/
|
||||
static void platform_exit_uninit( int status )
|
||||
{
|
||||
((void) status);
|
||||
}
|
||||
|
||||
#define POLARSSL_PLATFORM_STD_EXIT platform_exit_uninit
|
||||
#endif /* !POLARSSL_PLATFORM_STD_EXIT */
|
||||
|
||||
void (*polarssl_exit)( int status ) = POLARSSL_PLATFORM_STD_EXIT;
|
||||
|
||||
int platform_set_exit( void (*exit_func)( int status ) )
|
||||
{
|
||||
polarssl_exit = exit_func;
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_ALT */
|
||||
|
||||
#endif /* POLARSSL_PLATFORM_C */
|
||||
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* \file platform.h
|
||||
*
|
||||
* \brief mbed TLS Platform abstraction layer
|
||||
*
|
||||
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
#ifndef POLARSSL_PLATFORM_H
|
||||
#define POLARSSL_PLATFORM_H
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
/* Temporary compatibility hack for to keep MEMORY_C working */
|
||||
#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#define POLARSSL_PLATFORM_MEMORY
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \name SECTION: Module settings
|
||||
*
|
||||
* The configuration options you can set for this module are in this section.
|
||||
* Either change them in config.h or define them on the compiler command line.
|
||||
* \{
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_PLATFORM_NO_STD_FUNCTIONS)
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#if !defined(POLARSSL_PLATFORM_STD_SNPRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_SNPRINTF snprintf /**< Default snprintf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_PRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FPRINTF)
|
||||
#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_MALLOC)
|
||||
#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_FREE)
|
||||
#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */
|
||||
#endif
|
||||
#if !defined(POLARSSL_PLATFORM_STD_EXIT)
|
||||
#define POLARSSL_PLATFORM_STD_EXIT exit /**< Default free to use */
|
||||
#endif
|
||||
#else /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
|
||||
#if defined(POLARSSL_PLATFORM_STD_MEM_HDR)
|
||||
#include POLARSSL_PLATFORM_STD_MEM_HDR
|
||||
#endif
|
||||
#endif /* POLARSSL_PLATFORM_NO_STD_FUNCTIONS */
|
||||
|
||||
/* \} name SECTION: Module settings */
|
||||
|
||||
/*
|
||||
* The function pointers for malloc and free
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_MEMORY)
|
||||
#if defined(POLARSSL_PLATFORM_FREE_MACRO) && \
|
||||
defined(POLARSSL_PLATFORM_MALLOC_MACRO)
|
||||
#define polarssl_free POLARSSL_PLATFORM_FREE_MACRO
|
||||
#define polarssl_malloc POLARSSL_PLATFORM_MALLOC_MACRO
|
||||
#else
|
||||
/* For size_t */
|
||||
#include <stddef.h>
|
||||
extern void * (*polarssl_malloc)( size_t len );
|
||||
extern void (*polarssl_free)( void *ptr );
|
||||
|
||||
/**
|
||||
* \brief Set your own memory implementation function pointers
|
||||
*
|
||||
* \param malloc_func the malloc function implementation
|
||||
* \param free_func the free function implementation
|
||||
*
|
||||
* \return 0 if successful
|
||||
*/
|
||||
int platform_set_malloc_free( void * (*malloc_func)( size_t ),
|
||||
void (*free_func)( void * ) );
|
||||
#endif /* POLARSSL_PLATFORM_FREE_MACRO && POLARSSL_PLATFORM_MALLOC_MACRO */
|
||||
#else /* !POLARSSL_PLATFORM_MEMORY */
|
||||
#define polarssl_free free
|
||||
#define polarssl_malloc malloc
|
||||
#endif /* POLARSSL_PLATFORM_MEMORY && !POLARSSL_PLATFORM_{FREE,MALLOC}_MACRO */
|
||||
|
||||
/*
|
||||
* The function pointers for fprintf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_ALT)
|
||||
/* We need FILE * */
|
||||
#include <stdio.h>
|
||||
extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own fprintf function pointer
|
||||
*
|
||||
* \param fprintf_func the fprintf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *,
|
||||
... ) );
|
||||
#else
|
||||
#if defined(POLARSSL_PLATFORM_FPRINTF_MACRO)
|
||||
#define polarssl_fprintf POLARSSL_PLATFORM_FPRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_fprintf fprintf
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for printf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_ALT)
|
||||
extern int (*polarssl_printf)( const char *format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own printf function pointer
|
||||
*
|
||||
* \param printf_func the printf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_printf( int (*printf_func)( const char *, ... ) );
|
||||
#else /* !POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
#if defined(POLARSSL_PLATFORM_PRINTF_MACRO)
|
||||
#define polarssl_printf POLARSSL_PLATFORM_PRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_printf printf
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_PRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for snprintf
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_ALT)
|
||||
extern int (*polarssl_snprintf)( char * s, size_t n, const char * format, ... );
|
||||
|
||||
/**
|
||||
* \brief Set your own snprintf function pointer
|
||||
*
|
||||
* \param snprintf_func the snprintf function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_snprintf( int (*snprintf_func)( char * s, size_t n,
|
||||
const char * format, ... ) );
|
||||
#else /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
#if defined(POLARSSL_PLATFORM_SNPRINTF_MACRO)
|
||||
#define polarssl_snprintf POLARSSL_PLATFORM_SNPRINTF_MACRO
|
||||
#else
|
||||
#define polarssl_snprintf snprintf
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_SNPRINTF_ALT */
|
||||
|
||||
/*
|
||||
* The function pointers for exit
|
||||
*/
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_ALT)
|
||||
extern void (*polarssl_exit)( int status );
|
||||
|
||||
/**
|
||||
* \brief Set your own exit function pointer
|
||||
*
|
||||
* \param exit_func the exit function implementation
|
||||
*
|
||||
* \return 0
|
||||
*/
|
||||
int platform_set_exit( void (*exit_func)( int status ) );
|
||||
#else
|
||||
#if defined(POLARSSL_PLATFORM_EXIT_MACRO)
|
||||
#define polarssl_exit POLARSSL_PLATFORM_EXIT_MACRO
|
||||
#else
|
||||
#define polarssl_exit exit
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_MACRO */
|
||||
#endif /* POLARSSL_PLATFORM_EXIT_ALT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* platform.h */
|
||||
@@ -0,0 +1,412 @@
|
||||
/*
|
||||
* srp.c
|
||||
*
|
||||
* SRP algorithm (see http://srp.stanford.edu)
|
||||
* Uses bits of the ARM mbed TSL library (https://tls.mbed.org).
|
||||
* Notes: You can probably do better here since N and G are known at compile time.
|
||||
* This library uses a massive amount of RAM during calculations.
|
||||
* The SRP handshake takes 30+ seconds.
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
//#include <app_error.h>
|
||||
|
||||
//#include "homekit/homekit-config.h"
|
||||
#include "bignum.h"
|
||||
#include "memory_buffer_alloc.h"
|
||||
#include "../random.h"
|
||||
#include "srp.h"
|
||||
#include "../tweetnacl-modified/tweetnacl.h"
|
||||
//#include "homekit/pairing.h"
|
||||
|
||||
#include "common_inc.h"
|
||||
#include "rtos.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
#define E(V) (((V) << 24) | ((V) >> 24) | (((V) >> 8) & 0x0000FF00) | (((V) << 8) & 0x00FF0000))
|
||||
static const uint32_t srp_N[] =
|
||||
{
|
||||
E(0xFFFFFFFF), E(0xFFFFFFFF), E(0xC90FDAA2), E(0x2168C234), E(0xC4C6628B), E(0x80DC1CD1), E(0x29024E08),
|
||||
E(0x8A67CC74), E(0x020BBEA6), E(0x3B139B22), E(0x514A0879), E(0x8E3404DD), E(0xEF9519B3), E(0xCD3A431B),
|
||||
E(0x302B0A6D), E(0xF25F1437), E(0x4FE1356D), E(0x6D51C245), E(0xE485B576), E(0x625E7EC6), E(0xF44C42E9),
|
||||
E(0xA637ED6B), E(0x0BFF5CB6), E(0xF406B7ED), E(0xEE386BFB), E(0x5A899FA5), E(0xAE9F2411), E(0x7C4B1FE6),
|
||||
E(0x49286651), E(0xECE45B3D), E(0xC2007CB8), E(0xA163BF05), E(0x98DA4836), E(0x1C55D39A), E(0x69163FA8),
|
||||
E(0xFD24CF5F), E(0x83655D23), E(0xDCA3AD96), E(0x1C62F356), E(0x208552BB), E(0x9ED52907), E(0x7096966D),
|
||||
E(0x670C354E), E(0x4ABC9804), E(0xF1746C08), E(0xCA18217C), E(0x32905E46), E(0x2E36CE3B), E(0xE39E772C),
|
||||
E(0x180E8603), E(0x9B2783A2), E(0xEC07A28F), E(0xB5C55DF0), E(0x6F4C52C9), E(0xDE2BCBF6), E(0x95581718),
|
||||
E(0x3995497C), E(0xEA956AE5), E(0x15D22618), E(0x98FA0510), E(0x15728E5A), E(0x8AAAC42D), E(0xAD33170D),
|
||||
E(0x04507A33), E(0xA85521AB), E(0xDF1CBA64), E(0xECFB8504), E(0x58DBEF0A), E(0x8AEA7157), E(0x5D060C7D),
|
||||
E(0xB3970F85), E(0xA6E1E4C7), E(0xABF5AE8C), E(0xDB0933D7), E(0x1E8C94E0), E(0x4A25619D), E(0xCEE3D226),
|
||||
E(0x1AD2EE6B), E(0xF12FFA06), E(0xD98A0864), E(0xD8760273), E(0x3EC86A64), E(0x521F2B18), E(0x177B200C),
|
||||
E(0xBBE11757), E(0x7A615D6C), E(0x770988C0), E(0xBAD946E2), E(0x08E24FA0), E(0x74E5AB31), E(0x43DB5BFC),
|
||||
E(0xE0FD108E), E(0x4B82D120), E(0xA93AD2CA), E(0xFFFFFFFF), E(0xFFFFFFFF)
|
||||
};
|
||||
#undef E
|
||||
static const uint32_t srp_N_sizeof = 384;
|
||||
static const uint8_t srp_G = 5;
|
||||
// Pre-compute hashes where we can k = H(N | PAD(g))
|
||||
static const uint8_t srp_N_G_hash[] =
|
||||
{
|
||||
0xA9, 0xC2, 0xE2, 0x55, 0x9B, 0xF0, 0xEB, 0xB5, 0x3F, 0x0C, 0xBB, 0xF6, 0x22, 0x82, 0x90, 0x6B,
|
||||
0xED, 0xE7, 0xF2, 0x18, 0x2F, 0x00, 0x67, 0x82, 0x11, 0xFB, 0xD5, 0xBD, 0xE5, 0xB2, 0x85, 0x03,
|
||||
0x3A, 0x49, 0x93, 0x50, 0x3B, 0x87, 0x39, 0x7F, 0x9B, 0xE5, 0xEC, 0x02, 0x08, 0x0F, 0xED, 0xBC,
|
||||
0x08, 0x35, 0x58, 0x7A, 0xD0, 0x39, 0x06, 0x08, 0x79, 0xB8, 0x62, 0x1E, 0x8C, 0x36, 0x59, 0xE0
|
||||
};
|
||||
static const uint8_t srp_N_hash_srp_G_hash[] =
|
||||
{
|
||||
0xB3, 0xD6, 0x3E, 0xF6, 0xAA, 0xFB, 0x2E, 0x97, 0x96, 0xDA, 0xE0, 0x06, 0xFE, 0x60, 0xF2, 0x0E,
|
||||
0x53, 0x26, 0xFE, 0x1E, 0x1C, 0x52, 0xA5, 0x02, 0x1E, 0xB2, 0x17, 0x47, 0xCA, 0x33, 0xDF, 0xEA,
|
||||
0x8F, 0xF2, 0x03, 0xBD, 0x45, 0xA2, 0x54, 0x43, 0x95, 0xD7, 0x3D, 0x19, 0x89, 0x9C, 0x17, 0x4A,
|
||||
0x68, 0x3C, 0x9E, 0x78, 0x32, 0x96, 0xDD, 0x16, 0x93, 0xEB, 0xC7, 0x1C, 0xF5, 0xA5, 0x3D, 0xA3
|
||||
};
|
||||
|
||||
|
||||
#if TEST_APPLE
|
||||
|
||||
#define HOMEKIT_CONFIG_PINCODE "password123"
|
||||
#define HOMEKIT_USERNAME "alice"
|
||||
|
||||
static const uint8_t pincode[17] = HOMEKIT_USERNAME ":" HOMEKIT_CONFIG_PINCODE;
|
||||
|
||||
const uint8_t dbg_salt[] =
|
||||
{
|
||||
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24, 0x41, 0xEE
|
||||
};
|
||||
|
||||
const uint8_t dbg_b[] =
|
||||
{
|
||||
0xE4, 0x87, 0xCB, 0x59, 0xD3, 0x1A, 0xC5, 0x50, 0x47, 0x1E, 0x81, 0xF0, 0x0F, 0x69, 0x28, 0xE0,
|
||||
0x1D, 0xDA, 0x08, 0xE9, 0x74, 0xA0, 0x04, 0xF4, 0x9E, 0x61, 0xF5, 0xD1, 0x05, 0x28, 0x4D, 0x20
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#define HOMEKIT_CONFIG_PINCODE "111-22-333"
|
||||
#define HOMEKIT_USERNAME "Pair-Setup"
|
||||
static const uint8_t pincode[21] = HOMEKIT_USERNAME ":" HOMEKIT_CONFIG_PINCODE;
|
||||
|
||||
#if DEBUG_SRP
|
||||
const uint8_t dbg_salt[] =
|
||||
{
|
||||
0x6E, 0xFC, 0xCB, 0x4A, 0x0D, 0x20, 0xDA, 0x55,
|
||||
0x57, 0xE9, 0xDF, 0xD5, 0x44, 0x2B, 0x59, 0x48
|
||||
};
|
||||
|
||||
const uint8_t dbg_b[] =
|
||||
{
|
||||
0xCB, 0x13, 0xF7, 0xDB, 0x5A, 0x8A, 0xF0, 0xFB,
|
||||
0xE3, 0xB7, 0x85, 0x89, 0xEA, 0x89, 0xC5, 0x3C,
|
||||
0x61, 0x10, 0x21, 0x0B, 0x6F, 0xE5, 0x31, 0xD4,
|
||||
0xCE, 0x7D, 0xF6, 0x72, 0x59, 0x18, 0x95, 0xA1
|
||||
};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if DEBUG_SRP
|
||||
void print_mpi(const char* str, mpi* m)
|
||||
{
|
||||
uint32_t len = mpi_size(m) ;
|
||||
uint8_t* buf = (uint8_t*) rtos_malloc ( len );
|
||||
VERIFY(buf, );
|
||||
|
||||
mpi_write_binary(m, buf, len);
|
||||
LOG_LV2_BUFFER(str, buf, len);
|
||||
|
||||
rtos_free(buf);
|
||||
}
|
||||
#endif
|
||||
|
||||
srp_keys_t srp;
|
||||
|
||||
//static void MPI_ERROR_CHECK(int CODE)
|
||||
//{
|
||||
// if (CODE != 0)
|
||||
// {
|
||||
// APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
|
||||
// }
|
||||
//}
|
||||
|
||||
#define MPI_ERROR_CHECK(_code) VERIFY_STATUS(_code, )
|
||||
|
||||
/**
|
||||
* Adafruit: re-calculate x if p changes (setup code)
|
||||
*/
|
||||
void srp_init(void)
|
||||
{
|
||||
int err_code;
|
||||
|
||||
// The MPI library uses a ridiculous amount of memory. We use the stack allocator
|
||||
// so we don't tie this memory up except when we absolutely need to.
|
||||
// uint8_t memory[11 * 1024];
|
||||
// memory_buffer_alloc_init(memory, sizeof(memory));
|
||||
|
||||
#if DEBUG_SRP
|
||||
memcpy(srp.salt, dbg_salt, sizeof(srp.salt));
|
||||
memcpy(srp.b, dbg_b, 32);
|
||||
#else
|
||||
// Generate salt
|
||||
random_create(srp.salt, sizeof(srp.salt));
|
||||
|
||||
// Generate 'b' - a random value
|
||||
random_create(srp.b, 32);
|
||||
#endif
|
||||
|
||||
// Calculate 'x' = H(s | H(I | ":" | P))
|
||||
mpi x;
|
||||
mpi_init(&x);
|
||||
{
|
||||
uint8_t message[sizeof(srp.salt) + 64];
|
||||
memcpy(message, srp.salt, sizeof(srp.salt));
|
||||
crypto_hash_sha512(message + sizeof(srp.salt), pincode, sizeof(pincode));
|
||||
crypto_hash_sha512(message, message, sizeof(message));
|
||||
err_code = mpi_read_binary(&x, message, 64);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
// Calculate 'v' = g ^ x mod N
|
||||
mpi g;
|
||||
mpi_init(&g);
|
||||
err_code = mpi_lset(&g, srp_G);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi n;
|
||||
mpi_init(&n);
|
||||
err_code = mpi_read_binary(&n, (uint8_t*)srp_N, srp_N_sizeof);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi tmp;
|
||||
mpi_init(&tmp);
|
||||
mpi v;
|
||||
mpi_init(&v);
|
||||
err_code = mpi_exp_mod(&v, &g, &x, &n, &tmp);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
// Calculate 'k'
|
||||
mpi k;
|
||||
mpi_init(&k);
|
||||
err_code = mpi_read_binary(&k, srp_N_G_hash, sizeof(srp_N_G_hash));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
// Calculate 'B' = k*v + g^b % N
|
||||
mpi B;
|
||||
mpi_init(&B);
|
||||
err_code = mpi_mul_mpi(&B, &k, &v);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_write_binary(&v, srp.v, sizeof(srp.v));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
mpi_free(&v);
|
||||
|
||||
mpi b;
|
||||
mpi_init(&b);
|
||||
err_code = mpi_read_binary(&b, srp.b, sizeof(srp.b));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_exp_mod(&x, &g, &b, &n, &tmp);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_add_abs(&B, &B, &x);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_mod_mpi(&B, &B, &n);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
err_code = mpi_write_binary(&B, srp.B, sizeof(srp.B));
|
||||
|
||||
mpi_free(&b);
|
||||
mpi_free(&B);
|
||||
mpi_free(&k);
|
||||
mpi_free(&tmp);
|
||||
mpi_free(&n);
|
||||
mpi_free(&g);
|
||||
mpi_free(&x);
|
||||
|
||||
// memory_buffer_alloc_free();
|
||||
}
|
||||
|
||||
void srp_start(void)
|
||||
{
|
||||
srp.clientM1 = 0;
|
||||
srp.serverM1 = 0;
|
||||
}
|
||||
|
||||
uint8_t srp_setA(uint8_t* abuf, uint16_t length, moretime_t moretime)
|
||||
{
|
||||
int err_code;
|
||||
|
||||
// The MPI library uses a ridiculous amount of memory. We use the stack allocator
|
||||
// so we don't tie this memory up except when we absolutely need to.
|
||||
// uint8_t memory[11 * 1024];
|
||||
// memory_buffer_alloc_init(memory, sizeof(memory));
|
||||
|
||||
// getK
|
||||
{
|
||||
mpi s;
|
||||
mpi_init(&s);
|
||||
|
||||
mpi n;
|
||||
mpi_init(&n);
|
||||
err_code = mpi_read_binary(&n, (uint8_t*)srp_N, srp_N_sizeof);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
{
|
||||
// u = H(A | B)
|
||||
mpi u;
|
||||
mpi_init(&u);
|
||||
{
|
||||
uint8_t message[length + sizeof(srp.B)];
|
||||
memcpy(message, abuf, length);
|
||||
memcpy(message + length, srp.B, sizeof(srp.B));
|
||||
crypto_hash_sha512(message, message, sizeof(message));
|
||||
|
||||
err_code = mpi_read_binary(&u, message, 64);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
}
|
||||
|
||||
mpi v;
|
||||
mpi_init(&v);
|
||||
err_code = mpi_read_binary(&v, srp.v, sizeof(srp.v));
|
||||
|
||||
// getS = (A * v^u mod N)^b mod N
|
||||
err_code = mpi_exp_mod(&s, &v, &u, &n, NULL);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&v);
|
||||
mpi_free(&u);
|
||||
}
|
||||
|
||||
// These calculations take a long time. To avoid the connection dying we ask for more time now.
|
||||
if (moretime)
|
||||
{
|
||||
moretime();
|
||||
}
|
||||
|
||||
{
|
||||
mpi a;
|
||||
mpi_init(&a);
|
||||
err_code = mpi_read_binary(&a, abuf, length);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
err_code = mpi_mul_mpi(&s, &s, &a);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
mpi_free(&a);
|
||||
|
||||
mpi b;
|
||||
mpi_init(&b);
|
||||
err_code = mpi_read_binary(&b, srp.b, sizeof(srp.b));
|
||||
|
||||
err_code = mpi_exp_mod(&s, &s, &b, &n, NULL);
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&b);
|
||||
}
|
||||
|
||||
mpi_free(&n);
|
||||
|
||||
#if DEBUG_SRP > 1
|
||||
// print_mpi("S", &s);
|
||||
#endif
|
||||
|
||||
uint8_t sbuf[384];
|
||||
err_code = mpi_write_binary(&s, sbuf, sizeof(sbuf));
|
||||
MPI_ERROR_CHECK(err_code);
|
||||
|
||||
mpi_free(&s);
|
||||
|
||||
crypto_hash_sha512(srp.K, sbuf, sizeof(sbuf));
|
||||
|
||||
#if DEBUG_SRP > 1
|
||||
LOG_LV2_BUFFER("K=H(S)", srp.K, sizeof(srp.K));
|
||||
#endif
|
||||
}
|
||||
|
||||
// memory_buffer_alloc_free();
|
||||
|
||||
// getM1 - username s abuf srp.B K
|
||||
{
|
||||
uint8_t message[sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length + 384 + sizeof(srp.K)];
|
||||
memcpy(message, srp_N_hash_srp_G_hash, sizeof(srp_N_hash_srp_G_hash));
|
||||
crypto_hash_sha512(message + sizeof(srp_N_hash_srp_G_hash), pincode, strlen(HOMEKIT_USERNAME)); // First 10 chars only - not the PIN part
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64, srp.salt, sizeof(srp.salt));
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt), abuf, length);
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length, srp.B, sizeof(srp.B));
|
||||
memcpy(message + sizeof(srp_N_hash_srp_G_hash) + 64 + sizeof(srp.salt) + length + 384, srp.K, sizeof(srp.K));
|
||||
srp.serverM1 = 1;
|
||||
if (srp.clientM1)
|
||||
{
|
||||
uint8_t hash[64];
|
||||
crypto_hash_sha512(hash, message, sizeof(message));
|
||||
if (memcmp(hash, srp.M1, sizeof(srp.M1)) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
crypto_hash_sha512(srp.M1, message, sizeof(message));
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG_SRP > 1
|
||||
LOG_LV2_BUFFER("M1", srp.M1, sizeof(srp.M1));
|
||||
#endif
|
||||
|
||||
// getM2
|
||||
{
|
||||
uint8_t message[length + sizeof(srp.M1) + sizeof(srp.K)];
|
||||
memcpy(message, abuf, length);
|
||||
memcpy(message + length, srp.M1, sizeof(srp.M1));
|
||||
memcpy(message + length + sizeof(srp.M1), srp.K, sizeof(srp.K));
|
||||
crypto_hash_sha512(srp.M2, message, sizeof(message));
|
||||
}
|
||||
|
||||
#if DEBUG_SRP > 1
|
||||
LOG_LV2_BUFFER("M2", srp.M2, sizeof(srp.M2));
|
||||
#endif
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t* srp_getSalt(void)
|
||||
{
|
||||
return srp.salt;
|
||||
}
|
||||
|
||||
uint8_t* srp_getB(void)
|
||||
{
|
||||
return srp.B;
|
||||
}
|
||||
|
||||
uint8_t srp_checkM1(uint8_t* m1, uint16_t length)
|
||||
{
|
||||
srp.clientM1 = 1;
|
||||
if (length != sizeof(srp.M1))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (srp.serverM1)
|
||||
{
|
||||
if (memcmp(srp.M1, m1, sizeof(srp.M1)) != 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(srp.M1, m1, sizeof(srp.M1));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t* srp_getM2(void)
|
||||
{
|
||||
return srp.M2;
|
||||
}
|
||||
|
||||
uint8_t* srp_getK(void)
|
||||
{
|
||||
return srp.K;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* srp.h
|
||||
*
|
||||
* Created on: Jun 10, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_SRP_SRP_H_
|
||||
#define HOMEKIT_SRP_SRP_H_
|
||||
|
||||
#define BIGNUM_BYTES 384
|
||||
#define BIGNUM_WORDS (BIGNUM_BYTES / 4)
|
||||
|
||||
#define DEBUG_SRP 0
|
||||
#define TEST_APPLE 0
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t b[32];
|
||||
uint8_t salt[16];
|
||||
uint8_t v[384];
|
||||
uint8_t B[384];
|
||||
|
||||
uint8_t K[64];
|
||||
uint8_t M1[64];
|
||||
uint8_t M2[64];
|
||||
|
||||
uint8_t clientM1:1;
|
||||
uint8_t serverM1:1;
|
||||
} srp_keys_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern srp_keys_t srp;
|
||||
|
||||
typedef void (*moretime_t)(void);
|
||||
|
||||
extern void srp_init(void);
|
||||
extern void srp_start(void);
|
||||
extern uint8_t srp_setA(uint8_t* a, uint16_t length, moretime_t moretime);
|
||||
extern uint8_t srp_checkM1(uint8_t* m1, uint16_t length);
|
||||
extern uint8_t* srp_getSalt(void);
|
||||
extern uint8_t* srp_getB(void);
|
||||
extern uint8_t* srp_getM2(void);
|
||||
extern uint8_t* srp_getK(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HOMEKIT_SRP_SRP_H_ */
|
||||
@@ -0,0 +1,841 @@
|
||||
/*
|
||||
* tweetnacl.c
|
||||
*
|
||||
* Modified version of the amazing NaCL in 100 Tweets: http://tweetnacl.cr.yp.to/software.html
|
||||
*
|
||||
* 1. Replaced salsa20 with chacha20
|
||||
* 2. Improved performance, especially around multiply routines. No assembly yet (it would probably help a great deal).
|
||||
*
|
||||
* Created on: Jun 21, 2015
|
||||
* Modified: tim
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "../crypto.h"
|
||||
|
||||
#define FOR(i,n) for (i = 0;i < n;++i)
|
||||
|
||||
typedef unsigned char u8;
|
||||
typedef unsigned short u16;
|
||||
typedef short i16;
|
||||
typedef unsigned long u32;
|
||||
typedef unsigned long long u64;
|
||||
typedef long i32;
|
||||
typedef long long i64;
|
||||
typedef i64 gf[16];
|
||||
extern void randombytes(u8 *,u64);
|
||||
|
||||
static const u8
|
||||
_0[16],
|
||||
_9[32] = {9};
|
||||
static const gf
|
||||
gf0,
|
||||
gf1 = {1},
|
||||
_121665 = {0xDB41,1},
|
||||
D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
|
||||
D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406},
|
||||
X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169},
|
||||
Y = {0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666},
|
||||
I = {0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83};
|
||||
|
||||
static inline u32 L32(u32 x,int c) { return (x << c) | ((x&0xffffffff) >> (32 - c)); }
|
||||
|
||||
static u32 ld32(const u8 *x)
|
||||
{
|
||||
u32 u = x[3];
|
||||
u = (u<<8)|x[2];
|
||||
u = (u<<8)|x[1];
|
||||
return (u<<8)|x[0];
|
||||
}
|
||||
|
||||
static u64 dl64(const u8 *x)
|
||||
{
|
||||
u64 i,u=0;
|
||||
FOR(i,8) u=(u<<8)|x[i];
|
||||
return u;
|
||||
}
|
||||
|
||||
static void st32(u8 *x,u32 u)
|
||||
{
|
||||
int i;
|
||||
FOR(i,4) { x[i] = u; u >>= 8; }
|
||||
}
|
||||
|
||||
static void ts64(u8 *x,u64 u)
|
||||
{
|
||||
int i;
|
||||
for (i = 7;i >= 0;--i) { x[i] = u; u >>= 8; }
|
||||
}
|
||||
|
||||
static inline int vn(const u8 *x,const u8 *y,int n)
|
||||
{
|
||||
return memcmp(x,y,n);
|
||||
}
|
||||
|
||||
int inline crypto_verify_16(const u8 *x,const u8 *y)
|
||||
{
|
||||
return vn(x,y,16);
|
||||
}
|
||||
|
||||
int inline crypto_verify_32(const u8 *x,const u8 *y)
|
||||
{
|
||||
return vn(x,y,32);
|
||||
}
|
||||
|
||||
static void crypto_core_chacha20(u8 *out,const u8 *in,const u8 *k,const u8 *c)
|
||||
{
|
||||
u32 x[16],y[16],t[4];
|
||||
int i,j,m;
|
||||
|
||||
FOR(i,4) {
|
||||
x[i] = ld32(c+4*i);
|
||||
x[4+i] = ld32(k+4*i);
|
||||
x[8+i] = ld32(k+16+4*i);
|
||||
x[12+(2+i)%4] = ld32(in+4*i);
|
||||
}
|
||||
|
||||
FOR(i,16) y[i] = x[i];
|
||||
|
||||
FOR(i,10) {
|
||||
FOR(j,4) {
|
||||
FOR(m,4) t[m] = x[(j+4*m)%16];
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 16);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 12);
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 8);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 7);
|
||||
FOR(m,4) x[(j+4*m)%16] = t[m];
|
||||
}
|
||||
FOR(j,4) {
|
||||
FOR(m,4) t[m] = x[(4*m+(j+m)%4)%16];
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 16);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 12);
|
||||
t[3] = L32(t[3] ^ (t[0] += t[1]), 8);
|
||||
t[1] = L32(t[1] ^ (t[2] += t[3]), 7);
|
||||
FOR(m,4) x[(4*m+(j+m)%4)%16] = t[m];
|
||||
}
|
||||
}
|
||||
FOR(i,16) st32(out + 4 * i,x[i] + y[i]);
|
||||
}
|
||||
|
||||
static const u8 sigma[16] = "expand 32-byte k";
|
||||
|
||||
int crypto_stream_chacha20_xor(u8 *c,const u8 *m,u64 b,const u8 *n,const u8 *k,const u8 v)
|
||||
{
|
||||
u8 z[16],x[64];
|
||||
u32 u,i;
|
||||
if (!b) return 0;
|
||||
FOR(i,16) z[i] = 0;
|
||||
FOR(i,8) z[i] = n[i];
|
||||
z[8] = v;
|
||||
while (b >= 64) {
|
||||
crypto_core_chacha20(x,z,k,sigma);
|
||||
FOR(i,64) c[i] = (m?m[i]:0) ^ x[i];
|
||||
u = 1;
|
||||
for (i = 8;i < 16;++i) {
|
||||
u += (u32) z[i];
|
||||
z[i] = u;
|
||||
u >>= 8;
|
||||
}
|
||||
b -= 64;
|
||||
c += 64;
|
||||
if (m) m += 64;
|
||||
}
|
||||
if (b) {
|
||||
crypto_core_chacha20(x,z,k,sigma);
|
||||
FOR(i,b) c[i] = (m?m[i]:0) ^ x[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void add1305(u32 *h,const u32 *c)
|
||||
{
|
||||
u32 j,u = 0;
|
||||
FOR(j,17) {
|
||||
u += h[j] + c[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
static const u32 minusp[17] = {
|
||||
5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252
|
||||
} ;
|
||||
|
||||
int crypto_onetimeauth_poly1305(u8 *out,const u8 *m,u32 n,const u8 *k)
|
||||
{
|
||||
u32 s,i,j,u,x[17],r[17],h[17],c[17],g[17];
|
||||
|
||||
FOR(j,17) r[j]=h[j]=0;
|
||||
FOR(j,16) r[j]=k[j];
|
||||
r[3]&=15;
|
||||
r[4]&=252;
|
||||
r[7]&=15;
|
||||
r[8]&=252;
|
||||
r[11]&=15;
|
||||
r[12]&=252;
|
||||
r[15]&=15;
|
||||
|
||||
while (n > 0) {
|
||||
FOR(j,17) c[j] = 0;
|
||||
for (j = 0;(j < 16) && (j < n);++j) c[j] = m[j];
|
||||
c[j] = 1;
|
||||
m += j; n -= j;
|
||||
add1305(h,c);
|
||||
FOR(i,17) {
|
||||
x[i] = 0;
|
||||
for(j=0;j<=i;j++) x[i] += h[j] * r[i - j];
|
||||
for(;j<17;j++) x[i] += h[j] * 320 * r[i + 17 - j];
|
||||
}
|
||||
FOR(i,17) h[i] = x[i];
|
||||
u = 0;
|
||||
FOR(j,16) {
|
||||
u += h[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
u += h[16]; h[16] = u & 3;
|
||||
u = 5 * (u >> 2);
|
||||
FOR(j,16) {
|
||||
u += h[j];
|
||||
h[j] = u & 255;
|
||||
u >>= 8;
|
||||
}
|
||||
u += h[16]; h[16] = u;
|
||||
}
|
||||
|
||||
FOR(j,17) g[j] = h[j];
|
||||
add1305(h,minusp);
|
||||
s = -(h[16] >> 7);
|
||||
FOR(j,17) h[j] ^= s & (g[j] ^ h[j]);
|
||||
|
||||
FOR(j,16) c[j] = k[j + 16];
|
||||
c[16] = 0;
|
||||
add1305(h,c);
|
||||
FOR(j,16) out[j] = h[j];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_onetimeauth_poly1305_verify(const u8 *h,const u8 *m,u32 n,const u8 *k)
|
||||
{
|
||||
u8 x[16];
|
||||
crypto_onetimeauth_poly1305(x,m,n,k);
|
||||
return crypto_verify_16(h,x);
|
||||
}
|
||||
|
||||
static inline void set25519(gf r, const gf a)
|
||||
{
|
||||
int i;
|
||||
FOR(i,16) r[i]=a[i];
|
||||
}
|
||||
|
||||
//#define MUL38(V) ((v)*38)
|
||||
#define MUL38(V) ((((((V) << 3) + (V)) << 1) + (V)) << 1)
|
||||
|
||||
static void car25519(gf o)
|
||||
{
|
||||
i64 c=0;
|
||||
unsigned i;
|
||||
FOR(i,16) {
|
||||
i64 v=o[i]+c;
|
||||
o[i]=v&0xFFFF;
|
||||
c=v>>16;
|
||||
}
|
||||
while (c) {
|
||||
c=MUL38(c);
|
||||
for(i = 0; c && i < 16; i++) {
|
||||
i64 v=o[i]+c;
|
||||
o[i]=v&0xFFFF;
|
||||
c=v>>16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline void sel25519(gf p,gf q)
|
||||
{
|
||||
gf t;
|
||||
memcpy(t,p,sizeof(t));
|
||||
memcpy(p,q,sizeof(t));
|
||||
memcpy(q,t,sizeof(t));
|
||||
}
|
||||
|
||||
static void pack25519(u8 *o,const gf n)
|
||||
{
|
||||
int i,j,b;
|
||||
gf m,t;
|
||||
FOR(i,16) t[i]=n[i];
|
||||
car25519(t);
|
||||
FOR(j,2) {
|
||||
m[0]=t[0]-0xffed;
|
||||
for(i=1;i<15;i++) {
|
||||
m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
|
||||
m[i-1]&=0xffff;
|
||||
}
|
||||
m[15]=t[15]-0x7fff-((m[14]>>16)&1);
|
||||
b=(m[15]>>16)&1;
|
||||
m[14]&=0xffff;
|
||||
if (!b) sel25519(t,m);
|
||||
}
|
||||
FOR(i,16) {
|
||||
o[2*i]=t[i]&0xff;
|
||||
o[2*i+1]=t[i]>>8;
|
||||
}
|
||||
}
|
||||
|
||||
static int neq25519(const gf a, const gf b)
|
||||
{
|
||||
u8 c[32],d[32];
|
||||
pack25519(c,a);
|
||||
pack25519(d,b);
|
||||
return crypto_verify_32(c,d);
|
||||
}
|
||||
|
||||
static u8 par25519(const gf a)
|
||||
{
|
||||
u8 d[32];
|
||||
pack25519(d,a);
|
||||
return d[0]&1;
|
||||
}
|
||||
|
||||
static void unpack25519(gf o, const u8 *n)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=(i64)(n[2*i]|((u32)n[2*i+1]<<8));
|
||||
o[15]&=0x7fff;
|
||||
}
|
||||
|
||||
static inline void A(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=a[i]+b[i];
|
||||
}
|
||||
|
||||
static inline void Z(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
FOR(i,16) o[i]=a[i]-b[i];
|
||||
}
|
||||
|
||||
static inline void CS(u16 s[16], const gf o)
|
||||
{
|
||||
i64 c=0;
|
||||
unsigned i;
|
||||
FOR(i,16) {
|
||||
i64 v=o[i]+c;
|
||||
s[i]=(u16)v;
|
||||
c=v>>16;
|
||||
}
|
||||
while (c) {
|
||||
c=MUL38(c);
|
||||
for(i = 0; c && i < 16; i++) {
|
||||
i64 v=s[i]+c;
|
||||
s[i]=(u16)v;
|
||||
c=v>>16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(USE_UNACL_SCALARMULT)
|
||||
|
||||
extern void fe25519_mul(u16 o[16], u16 x[16], u16 y[16]);
|
||||
extern void fe25519_square(u16 o[16], u16 x[16]);
|
||||
|
||||
static void M(gf o, const gf a, const gf b)
|
||||
{
|
||||
unsigned i;
|
||||
u16 as[16];
|
||||
u16 bs[16];
|
||||
u16 os[16];
|
||||
|
||||
CS(as, a);
|
||||
CS(bs, b);
|
||||
|
||||
fe25519_mul(os, as, bs);
|
||||
|
||||
FOR(i,16) o[i] = os[i];
|
||||
}
|
||||
|
||||
static void S(gf o, const gf a)
|
||||
{
|
||||
unsigned i;
|
||||
u16 as[16];
|
||||
u16 os[16];
|
||||
|
||||
CS(as, a);
|
||||
|
||||
fe25519_square(os, as);
|
||||
|
||||
FOR(i,16) o[i] = os[i];
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void M(gf o,const gf a,const gf b)
|
||||
{
|
||||
unsigned i,j;
|
||||
u16 as[16];
|
||||
u16 bs[16];
|
||||
|
||||
CS(as, a);
|
||||
CS(bs, b);
|
||||
|
||||
i64 t[31],v;
|
||||
FOR(i,31) t[i]=0;
|
||||
i64* pt = &t[15];
|
||||
for (u16* asp = &as[15]; asp >= as; asp--, pt--) {
|
||||
u32 asi = *asp;
|
||||
i64* ppt = pt + 15;
|
||||
for (u16* bsp = &bs[15]; bsp >= bs; bsp--, ppt--) {
|
||||
v=*ppt;
|
||||
v+=(i64)(asi * (u32)*bsp);
|
||||
*ppt=v;
|
||||
}
|
||||
}
|
||||
FOR(i,15) { v=t[i+16]; v=MUL38(v); o[i]=t[i]+v; }
|
||||
o[15]=t[15];
|
||||
}
|
||||
|
||||
static void S(gf o,const gf a)
|
||||
{
|
||||
unsigned i,j;
|
||||
u16 as[16];
|
||||
|
||||
CS(as, a);
|
||||
|
||||
i64 t[31],v;
|
||||
FOR(i,31) t[i]=0;
|
||||
FOR(i,16) {
|
||||
u32 ai = (u32)as[i];
|
||||
t[i<<1]+=(u32)(ai*ai);
|
||||
for(j=i+1;j<16;j++) {
|
||||
v=t[i+j];
|
||||
v+=((i64)(ai*(u32)as[j]))<<1;
|
||||
t[i+j]=v;
|
||||
}
|
||||
}
|
||||
FOR(i,15) { v=t[i+16]; v=MUL38(v); o[i]=t[i]+v; }
|
||||
o[15]=t[15];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void inv25519(gf o,const gf i)
|
||||
{
|
||||
gf c;
|
||||
int a;
|
||||
FOR(a,16) c[a]=i[a];
|
||||
for(a=249;a;a--) {
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
}
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
FOR(a,16) o[a]=c[a];
|
||||
}
|
||||
|
||||
static void pow2523(gf o,const gf i)
|
||||
{
|
||||
gf c;
|
||||
int a;
|
||||
FOR(a,16) c[a]=i[a];
|
||||
for(a=249;a;a--) {
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
}
|
||||
S(c,c);
|
||||
S(c,c);
|
||||
M(c,c,i);
|
||||
FOR(a,16) o[a]=c[a];
|
||||
}
|
||||
|
||||
#if defined(USE_TWEETNACL_SCALARMULT)
|
||||
|
||||
int crypto_scalarmult_curve25519(u8 *q,const u8 *n,const u8 *p)
|
||||
{
|
||||
u8 z[32],r;
|
||||
i64 x[80];
|
||||
int i,j;
|
||||
gf a,b,c,d,e,f;
|
||||
FOR(i,31) z[i]=n[i];
|
||||
z[31]=(n[31]&127)|64;
|
||||
z[0]&=248;
|
||||
unpack25519(x,p);
|
||||
FOR(i,16) {
|
||||
b[i]=x[i];
|
||||
d[i]=a[i]=c[i]=0;
|
||||
}
|
||||
a[0]=d[0]=1;
|
||||
for(i=254;i>=0;--i) {
|
||||
r=(z[i>>3]>>(i&7))&1;
|
||||
if(r) {
|
||||
sel25519(a,b);
|
||||
sel25519(c,d);
|
||||
}
|
||||
A(e,a,c);
|
||||
Z(a,a,c);
|
||||
A(c,b,d);
|
||||
Z(b,b,d);
|
||||
S(d,e);
|
||||
S(f,a);
|
||||
M(a,c,a);
|
||||
M(c,b,e);
|
||||
A(e,a,c);
|
||||
Z(a,a,c);
|
||||
S(b,a);
|
||||
Z(c,d,f);
|
||||
M(a,c,_121665);
|
||||
A(a,a,d);
|
||||
M(c,c,a);
|
||||
M(a,d,f);
|
||||
M(d,b,x);
|
||||
S(b,e);
|
||||
if(r) {
|
||||
sel25519(a,b);
|
||||
sel25519(c,d);
|
||||
}
|
||||
}
|
||||
FOR(i,16) {
|
||||
x[i+16]=a[i];
|
||||
x[i+32]=c[i];
|
||||
x[i+48]=b[i];
|
||||
x[i+64]=d[i];
|
||||
}
|
||||
inv25519(x+32,x+32);
|
||||
M(x+16,x+16,x+32);
|
||||
pack25519(q,x+16);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_scalarmult_curve25519_base(u8 *q,const u8 *n)
|
||||
{
|
||||
return crypto_scalarmult_curve25519(q,n,_9);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static u64 R(u64 x,int c) { return (x >> c) | (x << (64 - c)); }
|
||||
static u64 Ch(u64 x,u64 y,u64 z) { return (x & y) ^ (~x & z); }
|
||||
static u64 Maj(u64 x,u64 y,u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
static u64 Sigma0(u64 x) { return R(x,28) ^ R(x,34) ^ R(x,39); }
|
||||
static u64 Sigma1(u64 x) { return R(x,14) ^ R(x,18) ^ R(x,41); }
|
||||
static u64 sigma0(u64 x) { return R(x, 1) ^ R(x, 8) ^ (x >> 7); }
|
||||
static u64 sigma1(u64 x) { return R(x,19) ^ R(x,61) ^ (x >> 6); }
|
||||
|
||||
static const u64 K[80] =
|
||||
{
|
||||
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
|
||||
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
|
||||
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
|
||||
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
|
||||
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
||||
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
|
||||
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
|
||||
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
|
||||
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
|
||||
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
||||
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
|
||||
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
|
||||
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
|
||||
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
|
||||
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
||||
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
|
||||
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
|
||||
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
|
||||
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
|
||||
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
|
||||
};
|
||||
|
||||
int crypto_hashblocks_sha512(u8 *x,const u8 *m,u64 n)
|
||||
{
|
||||
u64 z[8],b[8],a[8],w[16],t;
|
||||
int i,j;
|
||||
|
||||
FOR(i,8) z[i] = a[i] = dl64(x + 8 * i);
|
||||
|
||||
while (n >= 128) {
|
||||
FOR(i,16) w[i] = dl64(m + 8 * i);
|
||||
|
||||
FOR(i,80) {
|
||||
FOR(j,8) b[j] = a[j];
|
||||
t = a[7] + Sigma1(a[4]) + Ch(a[4],a[5],a[6]) + K[i] + w[i%16];
|
||||
b[7] = t + Sigma0(a[0]) + Maj(a[0],a[1],a[2]);
|
||||
b[3] += t;
|
||||
FOR(j,8) a[(j+1)%8] = b[j];
|
||||
if (i%16 == 15)
|
||||
FOR(j,16)
|
||||
w[j] += w[(j+9)%16] + sigma0(w[(j+1)%16]) + sigma1(w[(j+14)%16]);
|
||||
}
|
||||
|
||||
FOR(i,8) { a[i] += z[i]; z[i] = a[i]; }
|
||||
|
||||
m += 128;
|
||||
n -= 128;
|
||||
}
|
||||
|
||||
FOR(i,8) ts64(x+8*i,z[i]);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
static const u8 iv[64] = {
|
||||
0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
|
||||
0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
|
||||
0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
|
||||
0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
|
||||
0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
|
||||
0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
|
||||
0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
|
||||
0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
|
||||
} ;
|
||||
|
||||
int crypto_hash_sha512(u8 *out,const u8 *m,u64 n)
|
||||
{
|
||||
u8 h[64],x[256];
|
||||
u64 i,b = n;
|
||||
|
||||
FOR(i,64) h[i] = iv[i];
|
||||
|
||||
crypto_hashblocks_sha512(h,m,n);
|
||||
m += n;
|
||||
n &= 127;
|
||||
m -= n;
|
||||
|
||||
FOR(i,256) x[i] = 0;
|
||||
FOR(i,n) x[i] = m[i];
|
||||
x[n] = 128;
|
||||
|
||||
n = 256-128*(n<112);
|
||||
x[n-9] = b >> 61;
|
||||
ts64(x+n-8,b<<3);
|
||||
crypto_hashblocks_sha512(h,x,n);
|
||||
|
||||
FOR(i,64) out[i] = h[i];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void add(gf p[4],gf q[4])
|
||||
{
|
||||
gf a,b,c,d,t,e,f,g,h;
|
||||
|
||||
Z(a, p[1], p[0]);
|
||||
Z(t, q[1], q[0]);
|
||||
M(a, a, t);
|
||||
A(b, p[0], p[1]);
|
||||
A(t, q[0], q[1]);
|
||||
M(b, b, t);
|
||||
M(c, p[3], q[3]);
|
||||
M(c, c, D2);
|
||||
M(d, p[2], q[2]);
|
||||
A(d, d, d);
|
||||
Z(e, b, a);
|
||||
Z(f, d, c);
|
||||
A(g, d, c);
|
||||
A(h, b, a);
|
||||
M(p[0], e, f);
|
||||
M(p[1], h, g);
|
||||
M(p[2], g, f);
|
||||
M(p[3], e, h);
|
||||
}
|
||||
|
||||
static void pack(u8 *r,gf p[4])
|
||||
{
|
||||
gf tx, ty, zi;
|
||||
inv25519(zi, p[2]);
|
||||
M(tx, p[0], zi);
|
||||
M(ty, p[1], zi);
|
||||
pack25519(r, ty);
|
||||
r[31] ^= par25519(tx) << 7;
|
||||
}
|
||||
|
||||
static void scalarmult(gf p[4],gf q[4],const u8 *s)
|
||||
{
|
||||
set25519(p[0],gf0);
|
||||
set25519(p[1],gf1);
|
||||
set25519(p[2],gf1);
|
||||
set25519(p[3],gf0);
|
||||
for (const u8* sp = &s[31]; sp >= s; --sp) {
|
||||
const u8 si = *sp;
|
||||
for (u8 j = 0x80; j; j>>=1) {
|
||||
if (si & j) {
|
||||
add(p,q);
|
||||
add(q,q);
|
||||
} else {
|
||||
add(q,p);
|
||||
add(p,p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void scalarbase(gf p[4],const u8 *s)
|
||||
{
|
||||
gf q[4];
|
||||
set25519(q[0],X);
|
||||
set25519(q[1],Y);
|
||||
set25519(q[2],gf1);
|
||||
M(q[3],X,Y);
|
||||
scalarmult(p,q,s);
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519_keypair(u8 *pk, u8 *sk)
|
||||
{
|
||||
u8 d[64];
|
||||
gf p[4];
|
||||
int i;
|
||||
|
||||
randombytes(sk, 32);
|
||||
crypto_hash_sha512(d, sk, 32);
|
||||
d[0] &= 248;
|
||||
d[31] &= 127;
|
||||
d[31] |= 64;
|
||||
|
||||
scalarbase(p,d);
|
||||
pack(pk,p);
|
||||
|
||||
FOR(i,32) sk[32 + i] = pk[i];
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const u64 L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
|
||||
|
||||
static void modL(u8 *r,i64 x[64])
|
||||
{
|
||||
i64 carry,i,j;
|
||||
for (i = 63;i >= 32;--i) {
|
||||
carry = 0;
|
||||
for (j = i - 32;j < i - 12;++j) {
|
||||
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
||||
carry = (x[j] + 128) >> 8;
|
||||
x[j] -= carry << 8;
|
||||
}
|
||||
x[j] += carry;
|
||||
x[i] = 0;
|
||||
}
|
||||
carry = 0;
|
||||
FOR(j,32) {
|
||||
x[j] += carry - (x[31] >> 4) * L[j];
|
||||
carry = x[j] >> 8;
|
||||
x[j] &= 255;
|
||||
}
|
||||
FOR(j,32) x[j] -= carry * L[j];
|
||||
FOR(i,32) {
|
||||
x[i+1] += x[i] >> 8;
|
||||
r[i] = x[i] & 255;
|
||||
}
|
||||
}
|
||||
|
||||
static void reduce(u8 *r)
|
||||
{
|
||||
i64 x[64],i;
|
||||
FOR(i,64) x[i] = (u64) r[i];
|
||||
FOR(i,64) r[i] = 0;
|
||||
modL(r,x);
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk)
|
||||
{
|
||||
u8 d[64],h[64],r[64];
|
||||
i64 i,j,x[64];
|
||||
gf p[4];
|
||||
|
||||
crypto_hash_sha512(d, sk, 32);
|
||||
d[0] &= 248;
|
||||
d[31] &= 127;
|
||||
d[31] |= 64;
|
||||
|
||||
*smlen = n+64;
|
||||
FOR(i,n) sm[64 + i] = m[i];
|
||||
FOR(i,32) sm[32 + i] = d[32 + i];
|
||||
|
||||
crypto_hash_sha512(r, sm+32, n+32);
|
||||
reduce(r);
|
||||
scalarbase(p,r);
|
||||
pack(sm,p);
|
||||
|
||||
FOR(i,32) sm[i+32] = sk[i+32];
|
||||
crypto_hash_sha512(h,sm,n + 64);
|
||||
reduce(h);
|
||||
|
||||
FOR(i,64) x[i] = 0;
|
||||
FOR(i,32) x[i] = (u64) r[i];
|
||||
FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
|
||||
modL(sm + 32,x);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unpackneg(gf r[4],const u8 p[32])
|
||||
{
|
||||
gf t, chk, num, den, den2, den4, den6;
|
||||
set25519(r[2],gf1);
|
||||
unpack25519(r[1],p);
|
||||
S(num,r[1]);
|
||||
M(den,num,D);
|
||||
Z(num,num,r[2]);
|
||||
A(den,r[2],den);
|
||||
|
||||
S(den2,den);
|
||||
S(den4,den2);
|
||||
M(den6,den4,den2);
|
||||
M(t,den6,num);
|
||||
M(t,t,den);
|
||||
|
||||
pow2523(t,t);
|
||||
M(t,t,num);
|
||||
M(t,t,den);
|
||||
M(t,t,den);
|
||||
M(r[0],t,den);
|
||||
|
||||
S(chk,r[0]);
|
||||
M(chk,chk,den);
|
||||
if (neq25519(chk, num)) M(r[0],r[0],I);
|
||||
|
||||
S(chk,r[0]);
|
||||
M(chk,chk,den);
|
||||
if (neq25519(chk, num)) return -1;
|
||||
|
||||
if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
|
||||
|
||||
M(r[3],r[0],r[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int crypto_sign_ed25519_open(u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
|
||||
{
|
||||
int i;
|
||||
u8 t[32],h[64];
|
||||
gf p[4],q[4];
|
||||
|
||||
*mlen = -1;
|
||||
if (n < 64) return -1;
|
||||
|
||||
if (unpackneg(q,pk)) return -1;
|
||||
|
||||
FOR(i,n) m[i] = sm[i];
|
||||
FOR(i,32) m[i+32] = pk[i];
|
||||
crypto_hash_sha512(h,m,n);
|
||||
reduce(h);
|
||||
scalarmult(p,q,h);
|
||||
|
||||
scalarbase(q,sm + 32);
|
||||
add(p,q);
|
||||
pack(t,p);
|
||||
|
||||
n -= 64;
|
||||
if (crypto_verify_32(sm, t)) {
|
||||
FOR(i,n) m[i] = 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
FOR(i,n) m[i] = sm[i + 64];
|
||||
*mlen = n;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* tweetnacl.h
|
||||
*
|
||||
* Created on: Jun 21, 2015
|
||||
* Author: tim
|
||||
*/
|
||||
|
||||
#ifndef HOMEKIT_TWEETNACL_TWEETNACL_H_
|
||||
#define HOMEKIT_TWEETNACL_TWEETNACL_H_
|
||||
|
||||
#define crypto_sign_keypair crypto_sign_ed25519_keypair
|
||||
#define crypto_sign_open crypto_sign_ed25519_open
|
||||
#define crypto_sign crypto_sign_ed25519
|
||||
#define crypto_scalarmult_base crypto_scalarmult_curve25519_base
|
||||
#define crypto_scalarmult crypto_scalarmult_curve25519
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern int crypto_hashblocks_sha512(unsigned char *,const unsigned char *,unsigned long long);
|
||||
extern int crypto_hash_sha512(unsigned char *,const unsigned char *,unsigned long long);
|
||||
|
||||
extern int crypto_onetimeauth_poly1305(unsigned char *,const unsigned char *,unsigned long,const unsigned char *);
|
||||
extern int crypto_onetimeauth_poly1305_verify(const unsigned char *,const unsigned char *,unsigned long,const unsigned char *);
|
||||
|
||||
extern int crypto_sign_ed25519(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
|
||||
extern int crypto_sign_ed25519_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
|
||||
extern int crypto_sign_ed25519_keypair(unsigned char *,unsigned char *);
|
||||
|
||||
extern int crypto_stream_chacha20_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char);
|
||||
|
||||
extern int crypto_verify_16(const unsigned char *,const unsigned char *);
|
||||
extern int crypto_verify_32(const unsigned char *,const unsigned char *);
|
||||
|
||||
#if defined(USE_TWEETNACL_SCALARMULT)
|
||||
extern int crypto_scalarmult_curve25519(unsigned char *,const unsigned char *,const unsigned char *);
|
||||
extern int crypto_scalarmult_curve25519_base(unsigned char *,const unsigned char *);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HOMEKIT_TWEETNACL_TWEETNACL_H_ */
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPAccessoryInfo.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -65,7 +65,7 @@ err_t HAPAccessoryInfo::begin(void)
|
||||
"Adafruit Bluefruit nrf52",
|
||||
"Bluefruit52",
|
||||
getMcuUniqueID(),
|
||||
"0.8.0"
|
||||
"0.9.0"
|
||||
};
|
||||
|
||||
// Manufacturer
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPAccessoryInfo.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPLightBulb.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPLightBulb.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPPairing.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -37,23 +37,31 @@
|
||||
#include <bluefruit.h>
|
||||
#include "HAPUuid.h"
|
||||
#include "HAPPairing.h"
|
||||
#include <Nffs.h>
|
||||
|
||||
#include "crypto/crypto.h"
|
||||
|
||||
#define DEBUG_HAP_PAIRING 0
|
||||
|
||||
/* SRP design detail http://srp.stanford.edu/design.html
|
||||
*/
|
||||
|
||||
// kTLV type for pairing
|
||||
enum {
|
||||
PAIRING_TYPE_METHOD = 0,
|
||||
PAIRING_TYPE_IDENTIFIER,
|
||||
PAIRING_TYPE_SALT,
|
||||
PAIRING_TYPE_PUBLIC_KEY,
|
||||
PAIRING_TYPE_PROOF,
|
||||
PAIRING_TYPE_ENCRYPTED_DATA,
|
||||
PAIRING_TYPE_STATE,
|
||||
PAIRING_TYPE_ERROR,
|
||||
PAIRING_TYPE_RETRY_DELAY,
|
||||
PAIRING_TYPE_CERTIFICATE,
|
||||
PAIRING_TYPE_SIGNATURE,
|
||||
PAIRING_TYPE_PERMISSIONS,
|
||||
PAIRING_TYPE_FRAGMENT_DATA,
|
||||
PAIRING_TYPE_FRAGMENT_LAST,
|
||||
PAIRING_TYPE_METHOD = 0 , // 0
|
||||
PAIRING_TYPE_IDENTIFIER , // 1
|
||||
PAIRING_TYPE_SALT , // 2
|
||||
PAIRING_TYPE_PUBLIC_KEY , // 3
|
||||
PAIRING_TYPE_PROOF , // 4
|
||||
PAIRING_TYPE_ENCRYPTED_DATA , // 5
|
||||
PAIRING_TYPE_STATE , // 6
|
||||
PAIRING_TYPE_ERROR , // 7
|
||||
PAIRING_TYPE_RETRY_DELAY , // 8
|
||||
PAIRING_TYPE_CERTIFICATE , // 9
|
||||
PAIRING_TYPE_SIGNATURE , // 10
|
||||
PAIRING_TYPE_PERMISSIONS , // 11
|
||||
PAIRING_TYPE_FRAGMENT_DATA , // 12
|
||||
PAIRING_TYPE_FRAGMENT_LAST , // 13
|
||||
PAIRING_TYPE_SEPARATOR = 0xff
|
||||
};
|
||||
|
||||
@@ -79,24 +87,39 @@ enum
|
||||
PAIRING_ERROR_BUSY
|
||||
};
|
||||
|
||||
// kTLV value for state
|
||||
enum
|
||||
{
|
||||
PAIRING_STATE_M1 = 1,
|
||||
PAIRING_STATE_M2,
|
||||
PAIRING_STATE_M3,
|
||||
PAIRING_STATE_M4,
|
||||
};
|
||||
|
||||
#if CFG_DEBUG >= 2
|
||||
static const char* pairing_method_str[] =
|
||||
{
|
||||
"", "Pair Setup", "Pair Verify", "Add Pairing", "Remove Pairing", "List Pairings"
|
||||
};
|
||||
|
||||
static const char* pairing_type_str[] =
|
||||
{
|
||||
"Method", "Identifier", "Salt", "Public Key", "Proof",
|
||||
"Encrypted Data", "State", "Error", "Retry Delay", "Certificate",
|
||||
"Signature", "Permissions", "Fragment Data", "Fragment Last",
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req);
|
||||
void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
|
||||
void _pair_verify_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
|
||||
|
||||
// TODO add connection handle parameter
|
||||
void gatt_reply_now(void)
|
||||
{
|
||||
ble_gatts_rw_authorize_reply_params_t reply =
|
||||
{
|
||||
.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE,
|
||||
.params = {
|
||||
.write = {
|
||||
.gatt_status = BLE_GATT_STATUS_SUCCESS,
|
||||
.update = 1,
|
||||
}
|
||||
}
|
||||
};
|
||||
VERIFY_STATUS( sd_ble_gatts_rw_authorize_reply(Bluefruit.connHandle(), &reply), );
|
||||
}
|
||||
|
||||
HAPPairing::HAPPairing(void)
|
||||
: HAPService(HAP_UUID_SVC_PAIRING),
|
||||
@@ -105,7 +128,7 @@ HAPPairing::HAPPairing(void)
|
||||
_features (HAP_UUID_SVC_PAIR_FEATURE , BLE_GATT_CPF_FORMAT_UINT8 ),
|
||||
_pairing (HAP_UUID_SVC_PAIR_PAIRING , BLE_GATT_CPF_FORMAT_STRUCT)
|
||||
{
|
||||
|
||||
varclr(_pair_id);
|
||||
}
|
||||
|
||||
err_t HAPPairing::begin(void)
|
||||
@@ -113,81 +136,477 @@ err_t HAPPairing::begin(void)
|
||||
VERIFY_STATUS( HAPService::begin() ); // Invoke base class begin()
|
||||
|
||||
_setup.setHapProperties(HAP_CHR_PROPS_READ | HAP_CHR_PROPS_WRITE);
|
||||
_setup.setHapWriteCallback(pairing_setup_write_cb);
|
||||
_setup.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
|
||||
_setup.setHapWriteCallback(_pair_setup_write_cb);
|
||||
_setup.setMaxLen(200); // FIXME change later
|
||||
VERIFY_STATUS( _setup.begin() );
|
||||
|
||||
_verify.setHapProperties(HAP_CHR_PROPS_READ | HAP_CHR_PROPS_WRITE);
|
||||
_verify.setMaxLen(BLE_GATTS_VAR_ATTR_LEN_MAX);
|
||||
_verify.setHapWriteCallback(_pair_verify_write_cb);
|
||||
_verify.setMaxLen(200); // FIXME change later
|
||||
VERIFY_STATUS( _verify.begin() );
|
||||
|
||||
_features.setHapProperties(HAP_CHR_PROPS_READ);
|
||||
VERIFY_STATUS( _features.begin() );
|
||||
_features.writeHapValue(0x01); // support HAP pairing
|
||||
// Must be 0x00, 0x01 (support HAP pairing) is for MFi license, iOS will reject pairing attempt without MFi
|
||||
_features.writeHapValue( (uint32_t) 0x00);
|
||||
|
||||
_pairing.setHapProperties(HAP_CHR_PROPS_SECURE_READ | HAP_CHR_PROPS_SECURE_WRITE);
|
||||
VERIFY_STATUS( _pairing.begin() );
|
||||
|
||||
// Make PairID based on MAC address
|
||||
uint8_t mac[6];
|
||||
Bluefruit.Gap.getAddr(mac);
|
||||
setDeviceID(mac);
|
||||
|
||||
// Init cryptography
|
||||
Nffs.mkdir_p("/adafruit/homekit");
|
||||
crypto_init();
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
|
||||
static HAPResponse_t* pairing_setup_write_cb (HAPCharacteristic& chr, ble_gatts_evt_write_t const* gatt_req, HAPRequest_t const* hap_req)
|
||||
void HAPPairing::setDeviceID(uint8_t dev_id[6])
|
||||
{
|
||||
VERIFY(gatt_req->len > 3, NULL);
|
||||
char str[sizeof(_pair_id) + 1];
|
||||
sprintf(str, "%02X:%02X:%02X:%02X:%02X:%02X", dev_id[0], dev_id[1], dev_id[2], dev_id[3], dev_id[4], dev_id[5]);
|
||||
memcpy(_pair_id, str, sizeof(_pair_id));
|
||||
}
|
||||
|
||||
HAPResponse_t* hap_resp = NULL;
|
||||
void HAPPairing::createSrpResponse(uint16_t conn_hdl, uint8_t status, TLV8_t ktlv[], uint8_t count)
|
||||
{
|
||||
uint16_t srplen = tlv8_encode_calculate_len(ktlv, count);
|
||||
uint8_t* srpbuf = (uint8_t*) rtos_malloc(srplen);
|
||||
VERIFY( srpbuf != NULL, );
|
||||
|
||||
if( srplen == tlv8_encode_n(srpbuf, srplen, ktlv, count) )
|
||||
{
|
||||
#if DEBUG_HAP_PAIRING
|
||||
LOG_LV2_BUFFER("srpbuf", srpbuf, srplen);
|
||||
#endif
|
||||
|
||||
TLV8_t tlv = { .type = HAP_PARAM_VALUE, .len = srplen, .value = srpbuf };
|
||||
_setup.createHapResponse(conn_hdl, status, &tlv, 1);
|
||||
}
|
||||
|
||||
rtos_free(srpbuf);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* PAIR SETUP
|
||||
*------------------------------------------------------------------*/
|
||||
void HAPPairing::pair_setup_m1(uint16_t conn_hdl, HAPRequest_t const* hap_req)
|
||||
{
|
||||
// if paired return PAIRING_ERROR_UNAVAILABLE
|
||||
// tries more than 100 time return PAIRING_ERROR_MAX_TRIES
|
||||
// pairing with other iOS return PAIRING_ERROR_BUSY
|
||||
|
||||
// step 4
|
||||
srp_start();
|
||||
|
||||
// step 5 : username (I) = "Pair-Setup"
|
||||
// step 6 : 16 bytes salt already created in srp_init()
|
||||
// step 7,8 : password (p = setup code) done in srp_init()
|
||||
// step 9 : public key (B) done in srp_init()
|
||||
|
||||
uint8_t mstate = 2;
|
||||
|
||||
TLV8_t tlv_para[] =
|
||||
{
|
||||
{ .type = PAIRING_TYPE_STATE , .len = 1 , .value = &mstate },
|
||||
{ .type = PAIRING_TYPE_PUBLIC_KEY , .len = 384, .value = srp_getB() },
|
||||
{ .type = PAIRING_TYPE_SALT , .len = 16 , .value = srp_getSalt() },
|
||||
};
|
||||
|
||||
#if DEBUG_HAP_PAIRING
|
||||
LOG_LV2_BUFFER("SRP State" , tlv_para[0].value, tlv_para[0].len);
|
||||
LOG_LV2_BUFFER("SRP Pub Key", tlv_para[1].value, tlv_para[1].len);
|
||||
LOG_LV2_BUFFER("SRP Salt" , tlv_para[2].value, tlv_para[2].len);
|
||||
#endif
|
||||
|
||||
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
}
|
||||
|
||||
void HAPPairing::pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey, TLV8_t proof)
|
||||
{
|
||||
uint8_t mstate = 4;
|
||||
uint8_t pair_error = PAIRING_ERROR_AUTHENTICATION;
|
||||
|
||||
TLV8_t tlv_para[2] =
|
||||
{
|
||||
{ .type = PAIRING_TYPE_STATE, .len = 1, .value = &mstate },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
#if DEBUG_HAP_PAIRING
|
||||
LOG_LV2_BUFFER("SRP State", tlv_para[0].value, tlv_para[0].len);
|
||||
#endif
|
||||
|
||||
// Set iOS public key
|
||||
srp_setA( (uint8_t*) pubkey.value, pubkey.len, gatt_reply_now);
|
||||
|
||||
// Check proof
|
||||
if ( srp_checkM1( (uint8_t*) proof.value, proof.len) )
|
||||
{
|
||||
tlv_para[1].type = PAIRING_TYPE_PROOF;
|
||||
tlv_para[1].len = 64;
|
||||
tlv_para[1].value = srp_getM2();
|
||||
|
||||
#if DEBUG_HAP_PAIRING
|
||||
LOG_LV2_BUFFER("SRP Proof", tlv_para[1].value, tlv_para[1].len);
|
||||
#endif
|
||||
}else
|
||||
{
|
||||
tlv_para[1].type = PAIRING_TYPE_ERROR;
|
||||
tlv_para[1].len = 1;
|
||||
tlv_para[1].value = &pair_error;
|
||||
|
||||
#if DEBUG_HAP_PAIRING
|
||||
LOG_LV2_BUFFER("SRP Error", tlv_para[1].value, tlv_para[1].len);
|
||||
#endif
|
||||
}
|
||||
|
||||
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
}
|
||||
|
||||
void HAPPairing::pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted)
|
||||
{
|
||||
uint8_t const pair_error = PAIRING_ERROR_AUTHENTICATION;
|
||||
uint8_t const mstate = 6;
|
||||
|
||||
TLV8_t tlv_para[2] =
|
||||
{
|
||||
{ .type = PAIRING_TYPE_STATE, .len = 1, .value = &mstate },
|
||||
{ 0 }
|
||||
};
|
||||
|
||||
bool failed = false;
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* M5 Verification
|
||||
*------------------------------------------------------------------*/
|
||||
uint8_t session_key[64];
|
||||
|
||||
// Step 0: Create Session Key
|
||||
crypto_hkdf(session_key, (uint8_t*) "Pair-Setup-Encrypt-Salt", 23, (uint8_t*) "Pair-Setup-Encrypt-Info\001", 24, srp_getK(), 64);
|
||||
|
||||
// Step 1 + 2: Verify Auth Tag and Decrypt using ChaCha20-Poly1305
|
||||
if (crypto_verifyAndDecrypt(session_key, (uint8_t*) "PS-Msg05", (uint8_t*)encrypted.value, encrypted.len - 16, (uint8_t*) encrypted.value, ((uint8_t*) encrypted.value) + encrypted.len - 16))
|
||||
{
|
||||
uint16_t blength = encrypted.len - 16;
|
||||
uint8_t const* buf = (uint8_t const*) encrypted.value;
|
||||
|
||||
uint8_t* client = NULL;
|
||||
uint8_t* signature = NULL;
|
||||
uint8_t* ltpk = NULL;
|
||||
|
||||
// Parse iOS's ID, Public Key (ED25519) and Signature
|
||||
while(blength)
|
||||
{
|
||||
TLV8_t tlv = tlv8_decode_next(&buf, &blength);
|
||||
|
||||
LOG_LV2("M5", "type = %s", tlv.type == PAIRING_TYPE_SEPARATOR ? "Separator" : pairing_type_str[tlv.type]);
|
||||
LOG_LV2_BUFFER(NULL, tlv.value, tlv.len);
|
||||
|
||||
switch(tlv.type)
|
||||
{
|
||||
case PAIRING_TYPE_IDENTIFIER:
|
||||
VERIFY(tlv.len == 36, );
|
||||
client = (uint8_t*) tlv.value;
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_PUBLIC_KEY:
|
||||
VERIFY(tlv.len == 32, );
|
||||
ltpk = (uint8_t*) tlv.value;
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_SIGNATURE:
|
||||
VERIFY(tlv.len == 64, );
|
||||
signature = (uint8_t*) tlv.value;
|
||||
break;
|
||||
|
||||
default:
|
||||
failed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// There is no value > 255, there is no need to clean up. Put here for reference only
|
||||
// tlv8_decode_cleanup(tlv);
|
||||
}
|
||||
|
||||
if (client && signature && ltpk)
|
||||
{
|
||||
// iOSDeviceInfo = iOSDeviceX + iOSDevicePairingID, iOSDeviceLTPK
|
||||
uint8_t message[64 + 32 + 36 + 32];
|
||||
memcpy(message, signature, 64);
|
||||
|
||||
// Step 3: Derive iOSDeviceX
|
||||
crypto_hkdf(message + 64, (uint8_t*) "Pair-Setup-Controller-Sign-Salt", 31, (uint8_t*) "Pair-Setup-Controller-Sign-Info\001", 32, srp_getK(), 64);
|
||||
|
||||
// Step 4: Construct iOSDeviceInfo
|
||||
memcpy(message + 64 + 32, client, 36);
|
||||
memcpy(message + 64 + 32 + 36, ltpk, 32);
|
||||
|
||||
uint8_t result[sizeof(message)];
|
||||
uint64_t rlen = 0;
|
||||
|
||||
// Step 5: Verify iOSDeviceInfo with ED25519
|
||||
if (crypto_sign_open(result, &rlen, message, sizeof(message), ltpk) < 0)
|
||||
{
|
||||
failed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Step 6: save PairingID and LTPK
|
||||
memcpy(crypto_keys.client.ltpk, ltpk, sizeof(crypto_keys.client.ltpk));
|
||||
memcpy(crypto_keys.client.name, client, 36);
|
||||
crypto_scheduleStoreKeys();
|
||||
|
||||
// If failed to save ---> MaxPeers error
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Error
|
||||
failed = true;
|
||||
}
|
||||
|
||||
if (failed)
|
||||
{
|
||||
tlv_para[1].type = PAIRING_TYPE_ERROR;
|
||||
tlv_para[1].len = 1;
|
||||
tlv_para[1].value = &pair_error;
|
||||
|
||||
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* M6 Response Generation
|
||||
*------------------------------------------------------------------*/
|
||||
uint8_t smessage[64 + 32 + sizeof(_pair_id) + 32];
|
||||
|
||||
// Step 2: derive AccessoryX using HKDF_SHA512
|
||||
crypto_hkdf(smessage + 64, (uint8_t*) "Pair-Setup-Accessory-Sign-Salt", 30, (uint8_t*) "Pair-Setup-Accessory-Sign-Info\001", 31, srp_getK(), 64);
|
||||
|
||||
// Step 3: Concat AccessoryX + PairingID + AccessoryLTPK
|
||||
memcpy(smessage + 64 + 32, _pair_id, sizeof(_pair_id));
|
||||
memcpy(smessage + 64 + 32 + sizeof(_pair_id), crypto_keys.sign.pub, sizeof(crypto_keys.sign.pub));
|
||||
|
||||
// Step 4: Generate AccessorySignature with Ed25519
|
||||
uint64_t slen = 0;
|
||||
crypto_sign(smessage, &slen, smessage + 64, sizeof(smessage) - 64, crypto_keys.sign.secret);
|
||||
|
||||
// Step 5: Construct sub-tlv
|
||||
TLV8_t ktlv[] =
|
||||
{
|
||||
{ .type = PAIRING_TYPE_IDENTIFIER, .len = sizeof(_pair_id), .value = _pair_id },
|
||||
{ .type = PAIRING_TYPE_PUBLIC_KEY, .len = 32, .value = crypto_keys.sign.pub },
|
||||
{ .type = PAIRING_TYPE_SIGNATURE , .len = 64, .value = smessage }
|
||||
};
|
||||
|
||||
uint16_t const lbuffer = tlv8_encode_calculate_len(ktlv, arrcount(ktlv));
|
||||
uint8_t* buffer = (uint8_t*) rtos_malloc(lbuffer + 16); // Additional 16 byte for Auth Tag
|
||||
VERIFY( buffer != NULL, );
|
||||
|
||||
tlv8_encode_n(buffer, lbuffer, ktlv, arrcount(ktlv));
|
||||
|
||||
// Step 6: Encrypt above sub-tlv and generate 16-byte auth tag.
|
||||
crypto_encryptAndSeal(session_key, (uint8_t*) "PS-Msg06", buffer, lbuffer, buffer, buffer + lbuffer);
|
||||
|
||||
tlv_para[1].type = PAIRING_TYPE_ENCRYPTED_DATA;
|
||||
tlv_para[1].len = lbuffer+16;
|
||||
tlv_para[1].value = buffer;
|
||||
|
||||
createSrpResponse(conn_hdl, HAP_STATUS_SUCCESS, tlv_para, arrcount(tlv_para));
|
||||
|
||||
rtos_free(buffer);
|
||||
}
|
||||
|
||||
void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req)
|
||||
{
|
||||
uint16_t body_len = hap_req->body_len;
|
||||
uint8_t const* body_data = hap_req->body_data;
|
||||
|
||||
uint8_t const* param_val = NULL;
|
||||
uint16_t param_len = 0;
|
||||
|
||||
bool is_write_resp = false;
|
||||
|
||||
// TODO HAP Fragmentation
|
||||
|
||||
// Parse TLV data
|
||||
while (body_len)
|
||||
{
|
||||
TLV8_t tlv = tlv8_decode_next(&body_data, &body_len);
|
||||
|
||||
switch (tlv.type)
|
||||
LOG_LV2_BUFFER("Decoded HAP", tlv.value, tlv.len);
|
||||
|
||||
// ignore HAP_PARAM_RETURN_RESP, it should always been 1 for pair setup sequence
|
||||
if (HAP_PARAM_VALUE == tlv.type)
|
||||
{
|
||||
case HAP_PARAM_VALUE:
|
||||
param_val = (uint8_t const*) tlv.value;
|
||||
param_len = (uint16_t) tlv.len;
|
||||
break;
|
||||
uint8_t mstate = 0;
|
||||
|
||||
case HAP_PARAM_RETURN_RESP:
|
||||
memcpy(&is_write_resp, tlv.value, 1);
|
||||
break;
|
||||
// M1 parameters
|
||||
uint8_t method = 0;
|
||||
|
||||
// ignore other type
|
||||
default: break;
|
||||
// M3 parameters
|
||||
TLV8_t ipubkey = { 0 }; // (A ) 384 bytes
|
||||
TLV8_t iproof = { 0 }; // (M1) 64 bytes
|
||||
TLV8_t iencrypted = { 0 }; // Encrypted data
|
||||
|
||||
/*------------- Parse sub TLV (kTLV) data -------------*/
|
||||
uint8_t const* param_val = (uint8_t const*) tlv.value;
|
||||
uint16_t param_len = tlv.len;
|
||||
while(param_len)
|
||||
{
|
||||
TLV8_t ktlv = tlv8_decode_next(¶m_val, ¶m_len);
|
||||
|
||||
LOG_LV2("Pair-Setup", "type = %s", ktlv.type == PAIRING_TYPE_SEPARATOR ? "Separator" : pairing_type_str[ktlv.type]);
|
||||
LOG_LV2_BUFFER(NULL, ktlv.value, ktlv.len);
|
||||
|
||||
switch (ktlv.type)
|
||||
{
|
||||
case PAIRING_TYPE_METHOD:
|
||||
memcpy(&method, ktlv.value, 1);
|
||||
LOG_LV2("HAP", "Method %s", pairing_method_str[method]);
|
||||
break;
|
||||
|
||||
// TODO multiple pairing support
|
||||
case PAIRING_TYPE_STATE:
|
||||
memcpy(&mstate, ktlv.value, 1);
|
||||
LOG_LV2("HAP", "State = M%d", mstate);
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_PUBLIC_KEY:
|
||||
ipubkey = ktlv;
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_PROOF:
|
||||
iproof = ktlv;
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_ENCRYPTED_DATA:
|
||||
iencrypted = ktlv;
|
||||
break;
|
||||
|
||||
// ignore other type and clean up
|
||||
default:
|
||||
tlv8_decode_cleanup(ktlv);
|
||||
break;
|
||||
}
|
||||
|
||||
// Purposely skip tlv8_decode_cleanup() here, will call it later after processing
|
||||
}
|
||||
|
||||
/*------------- Processing setup sequence -------------*/
|
||||
HAPPairing& svc = (HAPPairing&) chr->parentService();
|
||||
switch (mstate)
|
||||
{
|
||||
case 1:
|
||||
svc.pair_setup_m1(conn_hdl, hap_req);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
svc.pair_setup_m3(conn_hdl, hap_req, ipubkey, iproof);
|
||||
|
||||
tlv8_decode_cleanup(ipubkey);
|
||||
tlv8_decode_cleanup(iproof);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
|
||||
svc.pair_setup_m5(conn_hdl, hap_req, iencrypted);
|
||||
|
||||
tlv8_decode_cleanup(iencrypted);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
tlv8_decode_cleanup(tlv);
|
||||
}
|
||||
|
||||
// Parse sub TLV (kTLV) data
|
||||
while(param_len)
|
||||
{
|
||||
TLV8_t ktlv = tlv8_decode_next(¶m_val, ¶m_len);
|
||||
|
||||
switch (ktlv.type)
|
||||
{
|
||||
case PAIRING_TYPE_METHOD:
|
||||
LOG_LV2("HAP", "Method %s", pairing_method_str[ *((uint8_t const*)ktlv.value) ]);
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_STATE:
|
||||
LOG_LV2("HAP", "State = M%d", *((uint8_t const*)ktlv.value));
|
||||
break;
|
||||
|
||||
// ignore other type
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
return hap_resp;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* PAIR VERIFY
|
||||
*------------------------------------------------------------------*/
|
||||
void _pair_verify_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req)
|
||||
{
|
||||
uint16_t body_len = hap_req->body_len;
|
||||
uint8_t const* body_data = hap_req->body_data;
|
||||
|
||||
// Parse TLV data
|
||||
while (body_len)
|
||||
{
|
||||
TLV8_t tlv = tlv8_decode_next(&body_data, &body_len);
|
||||
|
||||
LOG_LV2_BUFFER("Decoded HAP", tlv.value, tlv.len);
|
||||
|
||||
// ignore HAP_PARAM_RETURN_RESP, it should always been 1 for pair setup sequence
|
||||
if (HAP_PARAM_VALUE == tlv.type)
|
||||
{
|
||||
uint8_t mstate = 0;
|
||||
TLV8_t tlvparam = { 0 };
|
||||
|
||||
/*------------- Parse sub TLV (kTLV) data -------------*/
|
||||
uint8_t const* param_val = (uint8_t const*) tlv.value;
|
||||
uint16_t param_len = tlv.len;
|
||||
while(param_len)
|
||||
{
|
||||
TLV8_t ktlv = tlv8_decode_next(¶m_val, ¶m_len);
|
||||
|
||||
LOG_LV2("Pair-Verify", "type = %s", ktlv.type == PAIRING_TYPE_SEPARATOR ? "Separator" : pairing_type_str[ktlv.type]);
|
||||
LOG_LV2_BUFFER(NULL, ktlv.value, ktlv.len);
|
||||
|
||||
switch (ktlv.type)
|
||||
{
|
||||
case PAIRING_TYPE_STATE:
|
||||
memcpy(&mstate, ktlv.value, 1);
|
||||
LOG_LV2("HAP", "State = M%d", mstate);
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_PUBLIC_KEY:
|
||||
tlvparam = ktlv;
|
||||
break;
|
||||
|
||||
case PAIRING_TYPE_ENCRYPTED_DATA:
|
||||
tlvparam = ktlv;
|
||||
break;
|
||||
|
||||
// ignore other type and clean up
|
||||
default:
|
||||
tlv8_decode_cleanup(ktlv);
|
||||
break;
|
||||
}
|
||||
|
||||
// Purposely skip tlv8_decode_cleanup() here, will call it later after processing
|
||||
}
|
||||
|
||||
/*------------- Processing VERIFY sequence -------------*/
|
||||
HAPPairing& svc = (HAPPairing&) chr->parentService();
|
||||
switch (mstate)
|
||||
{
|
||||
case 1:
|
||||
svc.pair_verify_m1(conn_hdl, hap_req, tlvparam);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
svc.pair_verify_m3(conn_hdl, hap_req, tlvparam);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
tlv8_decode_cleanup(tlvparam);
|
||||
}
|
||||
|
||||
tlv8_decode_cleanup(tlv);
|
||||
}
|
||||
}
|
||||
|
||||
void HAPPairing::pair_verify_m1(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void HAPPairing::pair_verify_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPPairing.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -43,7 +43,6 @@ class HAPPairing : public HAPService
|
||||
{
|
||||
public:
|
||||
HAPPairing(void);
|
||||
|
||||
virtual err_t begin(void);
|
||||
|
||||
private:
|
||||
@@ -51,6 +50,27 @@ class HAPPairing : public HAPService
|
||||
HAPCharacteristic _verify;
|
||||
HAPCharacteristic _features;
|
||||
HAPCharacteristic _pairing;
|
||||
|
||||
uint8_t _pair_id[17]; // Device ID in string format e.g "aa:bb:cc:dd:ee"
|
||||
|
||||
void setDeviceID(uint8_t dev_id[6]);
|
||||
|
||||
void createSrpResponse(uint16_t conn_hdl, uint8_t status, TLV8_t ktlv[], uint8_t count);
|
||||
|
||||
void pair_setup_m1(uint16_t conn_hdl, HAPRequest_t const* hap_req);
|
||||
void pair_setup_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey, TLV8_t proof);
|
||||
void pair_setup_m5(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted);
|
||||
|
||||
|
||||
void pair_verify_m1(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t pubkey);
|
||||
void pair_verify_m3(uint16_t conn_hdl, HAPRequest_t const* hap_req, TLV8_t encrypted);
|
||||
|
||||
friend void _pair_setup_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
|
||||
friend void _pair_verify_write_cb (uint16_t conn_hdl, HAPCharacteristic* chr, HAPRequest_t const* hap_req);
|
||||
|
||||
#if CFG_DEBUG
|
||||
friend void test_homekit(void);
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPProtocol.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file HAPProtocol.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -24,6 +24,7 @@ BLEClientUart clientUart;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central BLEUART Example");
|
||||
Serial.println("-----------------------------------\n");
|
||||
@@ -74,6 +75,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
|
||||
// Connect to device with bleuart service in advertising
|
||||
Bluefruit.Central.connect(report);
|
||||
}else
|
||||
{
|
||||
// For Softdevice v6: after received a report, scanner will be paused
|
||||
// We need to call Scanner resume() to continue scanning
|
||||
Bluefruit.Scanner.resume();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +138,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
@@ -180,4 +186,3 @@ void loop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-9
@@ -83,6 +83,7 @@ uint8_t connection_num = 0; // for blink pattern
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
// Initialize blinkTimer for 100 ms and start it
|
||||
blinkTimer.begin(100, blink_timer_callback);
|
||||
@@ -116,12 +117,14 @@ void setup()
|
||||
/* Start Central Scanning
|
||||
* - Enable auto scan if disconnected
|
||||
* - Interval = 100 ms, window = 80 ms
|
||||
* - Filter only accept bleuart service in advertising
|
||||
* - Don't use active scan (used to retrieve the optional scan response adv packet)
|
||||
* - Start(0) = will scan forever since no timeout is given
|
||||
*/
|
||||
Bluefruit.Scanner.setRxCallback(scan_callback);
|
||||
Bluefruit.Scanner.restartOnDisconnect(true);
|
||||
Bluefruit.Scanner.setInterval(160, 80); // in units of 0.625 ms
|
||||
Bluefruit.Scanner.filterUuid(BLEUART_UUID_SERVICE);
|
||||
Bluefruit.Scanner.useActiveScan(false); // Don't request scan response data
|
||||
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
|
||||
}
|
||||
@@ -132,14 +135,10 @@ void setup()
|
||||
*/
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Check if advertising data contains the BleUart service UUID
|
||||
if ( Bluefruit.Scanner.checkReportForUuid(report, BLEUART_UUID_SERVICE) )
|
||||
{
|
||||
Serial.print("BLE UART service detected. Connecting ... ");
|
||||
|
||||
// Connect to the device with bleuart service in advertising packet
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
// Since we configure the scanner with filterUuid()
|
||||
// Scan callback only invoked for device with bleuart service advertised
|
||||
// Connect to the device with bleuart service in advertising packet
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +187,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
|
||||
@@ -35,6 +35,7 @@ BLEClientCharacteristic bslc(UUID16_CHR_BODY_SENSOR_LOCATION);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central Custom HRM Example");
|
||||
Serial.println("--------------------------------------\n");
|
||||
@@ -89,6 +90,8 @@ void loop()
|
||||
*/
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Since we configure the scanner with filterUuid()
|
||||
// Scan callback only invoked for device with hrm service advertised
|
||||
// Connect to device with HRM service in advertising
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
@@ -162,7 +165,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
*********************************************************************/
|
||||
|
||||
/*
|
||||
* This sketch demonstrate the central API(). A additional bluefruit
|
||||
* that has bleuart as peripheral is required for the demo.
|
||||
* This sketch demonstrate the central API(). An additional bluefruit
|
||||
* that has blehid as peripheral is required for the demo.
|
||||
*/
|
||||
#include <bluefruit.h>
|
||||
|
||||
@@ -30,6 +30,7 @@ hid_mouse_report_t last_mse_report = { 0 };
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central HID (Keyboard + Mouse) Example");
|
||||
Serial.println("--------------------------------------------------\n");
|
||||
@@ -58,15 +59,15 @@ void setup()
|
||||
* - Enable auto scan if disconnected
|
||||
* - Interval = 100 ms, window = 80 ms
|
||||
* - Don't use active scan
|
||||
* - Filter only accept HID service in advertising
|
||||
* - Start(timeout) with timeout = 0 will scan forever (until connected)
|
||||
*/
|
||||
Bluefruit.Scanner.setRxCallback(scan_callback);
|
||||
Bluefruit.Scanner.restartOnDisconnect(true);
|
||||
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
|
||||
Bluefruit.Scanner.filterService(hid); // only report HID service
|
||||
Bluefruit.Scanner.useActiveScan(false);
|
||||
Bluefruit.Scanner.start(0); // 0 = Don't stop scanning after n seconds
|
||||
|
||||
Bluefruit.Scanner.filterService(hid); // only report HID service
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +76,9 @@ void setup()
|
||||
*/
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Connect to device
|
||||
// Since we configure the scanner with filterUuid()
|
||||
// Scan callback only invoked for device with hid service advertised
|
||||
// Connect to the device with hid service in advertising packet
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
|
||||
@@ -131,7 +134,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle
|
||||
* @param reason
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
@@ -220,4 +223,3 @@ void processKeyboardReport(hid_keyboard_report_t* report)
|
||||
// update last report
|
||||
memcpy(&last_kbd_report, report, sizeof(hid_keyboard_report_t));
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central Scan Example");
|
||||
Serial.println("--------------------------------\n");
|
||||
@@ -50,7 +51,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
Serial.print(report->rssi);
|
||||
Serial.print(" ");
|
||||
|
||||
Serial.printBuffer(report->data, report->dlen, '-');
|
||||
Serial.printBuffer(report->data.p_data, report->data.len, '-');
|
||||
Serial.println();
|
||||
|
||||
// Check if advertising contain BleUart service
|
||||
@@ -60,13 +61,13 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
|
||||
// For Softdevice v6: after received a report, scanner will be paused
|
||||
// We need to call Scanner resume() to continue scanning
|
||||
Bluefruit.Scanner.resume();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Toggle both LEDs every 1 second
|
||||
digitalToggle(LED_RED);
|
||||
|
||||
delay(1000);
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
+24
-22
@@ -21,6 +21,7 @@
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central ADV Scan Example");
|
||||
Serial.println("------------------------------------\n");
|
||||
@@ -58,12 +59,13 @@ void setup()
|
||||
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
PRINT_LOCATION();
|
||||
uint8_t len = 0;
|
||||
uint8_t buffer[32];
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
/* Display the timestamp and device address */
|
||||
if (report->scan_rsp)
|
||||
if (report->type.scan_response)
|
||||
{
|
||||
Serial.printf("[SR%10d] Packet received from ", millis());
|
||||
}
|
||||
@@ -76,11 +78,11 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
Serial.print("\n");
|
||||
|
||||
/* Raw buffer contents */
|
||||
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->dlen);
|
||||
if (report->dlen)
|
||||
Serial.printf("%14s %d bytes\n", "PAYLOAD", report->data.len);
|
||||
if (report->data.len)
|
||||
{
|
||||
Serial.printf("%15s", " ");
|
||||
Serial.printBuffer(report->data, report->dlen, '-');
|
||||
Serial.printBuffer(report->data.p_data, report->data.len, '-');
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -89,20 +91,20 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
|
||||
/* Adv Type */
|
||||
Serial.printf("%14s ", "ADV TYPE");
|
||||
switch (report->type)
|
||||
if ( report->type.connectable )
|
||||
{
|
||||
case BLE_GAP_ADV_TYPE_ADV_IND:
|
||||
Serial.printf("Connectable undirected\n");
|
||||
break;
|
||||
case BLE_GAP_ADV_TYPE_ADV_DIRECT_IND:
|
||||
Serial.printf("Connectable directed\n");
|
||||
break;
|
||||
case BLE_GAP_ADV_TYPE_ADV_SCAN_IND:
|
||||
Serial.printf("Scannable undirected\n");
|
||||
break;
|
||||
case BLE_GAP_ADV_TYPE_ADV_NONCONN_IND:
|
||||
Serial.printf("Non-connectable undirected\n");
|
||||
break;
|
||||
Serial.print("Connectable ");
|
||||
}else
|
||||
{
|
||||
Serial.print("Non-connectable ");
|
||||
}
|
||||
|
||||
if ( report->type.directed )
|
||||
{
|
||||
Serial.println("directed");
|
||||
}else
|
||||
{
|
||||
Serial.println("undirected");
|
||||
}
|
||||
|
||||
/* Shortened Local Name */
|
||||
@@ -177,6 +179,10 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
|
||||
// For Softdevice v6: after received a report, scanner will be paused
|
||||
// We need to call Scanner resume() to continue scanning
|
||||
Bluefruit.Scanner.resume();
|
||||
}
|
||||
|
||||
void printUuid16List(uint8_t* buffer, uint8_t len)
|
||||
@@ -208,9 +214,5 @@ void printUuid128List(uint8_t* buffer, uint8_t len)
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Toggle both LEDs every 1 second
|
||||
digitalToggle(LED_RED);
|
||||
|
||||
delay(1000);
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
+16
-10
@@ -58,6 +58,7 @@ BLEClientCharacteristic opticalCharacteristicPeriod(SENSORTAG_OPTICAL_PERIOD_CHA
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central TI Sensor Tag Example");
|
||||
Serial.println("-----------------------------------------\n");
|
||||
@@ -121,7 +122,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
|
||||
/* Choose a peripheral to connect with by searching for an advertisement packet with a
|
||||
Complete Local Name matching our target device*/
|
||||
uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 };
|
||||
uint8_t buffer[BLE_GAP_ADV_SET_DATA_SIZE_MAX] = { 0 };
|
||||
|
||||
Serial.print("Parsing report for Local Name ... ");
|
||||
if(Bluefruit.Scanner.parseReportByType(report, BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME, buffer, sizeof(buffer)))
|
||||
@@ -130,7 +131,7 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
Serial.printf("%14s %s\n", "Local Name:", buffer);
|
||||
|
||||
Serial.print(" Local Name data: ");
|
||||
printHexList(buffer, BLE_GAP_ADV_MAX_SIZE );
|
||||
printHexList(buffer, BLE_GAP_ADV_SET_DATA_SIZE_MAX );
|
||||
|
||||
Serial.print("Determining Local Name Match ... ");
|
||||
if ( !memcmp( buffer, SENSORTAG_ADV_COMPLETE_LOCAL_NAME, sizeof(SENSORTAG_ADV_COMPLETE_LOCAL_NAME)) )
|
||||
@@ -143,11 +144,16 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
else
|
||||
{
|
||||
Serial.println("No Match");
|
||||
Bluefruit.Scanner.resume(); // continue scanning
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Failed");
|
||||
Serial.println("Failed");
|
||||
|
||||
// For Softdevice v6: after received a report, scanner will be paused
|
||||
// We need to call Scanner resume() to continue scanning
|
||||
Bluefruit.Scanner.resume();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +168,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.println( conn_handle );
|
||||
|
||||
/* Complete Local Name */
|
||||
uint8_t buffer[BLE_GAP_ADV_MAX_SIZE] = { 0 };
|
||||
uint8_t buffer[BLE_GAP_ADV_SET_DATA_SIZE_MAX] = { 0 };
|
||||
|
||||
// If Service is not found, disconnect and return
|
||||
Serial.print("Discovering Optical Service ... ");
|
||||
@@ -288,15 +294,15 @@ void printReport( const ble_gap_evt_adv_report_t* report )
|
||||
Serial.print( " rssi: " );
|
||||
Serial.println( report->rssi );
|
||||
Serial.print( " scan_rsp: " );
|
||||
Serial.println( report->scan_rsp );
|
||||
Serial.print( " type: " );
|
||||
Serial.println( report->type );
|
||||
Serial.println( report->type.scan_response );
|
||||
// Serial.print( " type: " );
|
||||
// Serial.println( report->type );
|
||||
Serial.print( " dlen: " );
|
||||
Serial.println( report->dlen );
|
||||
Serial.println( report->data.len );
|
||||
Serial.print( " data: " );
|
||||
for( int i = 0; i < report->dlen; i+= sizeof(uint8_t) )
|
||||
for( int i = 0; i < report->data.len; i+= sizeof(uint8_t) )
|
||||
{
|
||||
Serial.printf( "%02X-", report->data[ i ] );
|
||||
Serial.printf( "%02X-", report->data.p_data[ i ] );
|
||||
}
|
||||
Serial.println("");
|
||||
}
|
||||
@@ -30,6 +30,7 @@ BLEClientUart clientUart;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Dual Role BLEUART Example");
|
||||
Serial.println("-------------------------------------\n");
|
||||
@@ -153,14 +154,10 @@ void prph_bleuart_rx_callback(void)
|
||||
*------------------------------------------------------------------*/
|
||||
void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
{
|
||||
// Check if advertising contain BleUart service
|
||||
if ( Bluefruit.Scanner.checkReportForService(report, clientUart) )
|
||||
{
|
||||
Serial.println("BLE UART service detected. Connecting ... ");
|
||||
|
||||
// Connect to device with bleuart service in advertising
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
// Since we configure the scanner with filterUuid()
|
||||
// Scan callback only invoked for device with bleuart service advertised
|
||||
// Connect to the device with bleuart service in advertising packet
|
||||
Bluefruit.Central.connect(report);
|
||||
}
|
||||
|
||||
void cent_connect_callback(uint16_t conn_handle)
|
||||
@@ -213,4 +210,3 @@ void cent_bleuart_rx_callback(BLEClientUart& cent_uart)
|
||||
clientUart.println("[Cent] Peripheral role not connected");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*********************************************************************
|
||||
This is an example for our Feather Bluefruit modules
|
||||
|
||||
Pick one up today in the adafruit shop!
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
MIT license, check LICENSE for more information
|
||||
All text above, and the splash screen below must be included in
|
||||
any redistribution
|
||||
*********************************************************************/
|
||||
|
||||
/*
|
||||
* This sketch demonstrate how to use Hardware Serial1 along with
|
||||
* native USB Serial on Bluefruit nRF52840.
|
||||
* Note: Bluefruit nRF52832 does not support Serial1
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Goodnight moon!");
|
||||
|
||||
// set the data rate for the SoftwareSerial port
|
||||
//mySerial.begin(9600);
|
||||
//mySerial.println("Hello, world?");
|
||||
|
||||
Serial1.begin(115200);
|
||||
Serial1.println("Hello, world?");
|
||||
}
|
||||
|
||||
void loop() // run over and over//
|
||||
{
|
||||
if (Serial1.available())
|
||||
Serial.write(Serial1.read());
|
||||
|
||||
if (Serial.available())
|
||||
Serial1.write(Serial.read());
|
||||
}
|
||||
@@ -28,8 +28,6 @@ void setup()
|
||||
Serial.println("Serial Echo demo");
|
||||
Serial.print("Badurate : ");
|
||||
Serial.println(baudrate);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
@@ -38,9 +36,7 @@ void setup()
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void loop()
|
||||
{
|
||||
char ch;
|
||||
|
||||
{
|
||||
// From Serial monitor to All
|
||||
if ( Serial.available() )
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ float mv_per_lsb = 3600.0F/1024.0F; // 10-bit ADC with 3.6V input range
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -58,6 +58,7 @@ uint8_t mvToPercent(float mvolts) {
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
// Get a single ADC sample and throw it away
|
||||
readVBAT();
|
||||
|
||||
@@ -20,13 +20,11 @@
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup() {
|
||||
// initialize digital pin LED_BUILTIN as an output.
|
||||
pinMode(LED_RED, OUTPUT);
|
||||
}
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
void loop() {
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
digitalToggle(LED_BLUE); // turn the LED on (HIGH is the voltage level)
|
||||
delay(1000); // wait for a second
|
||||
}
|
||||
digitalToggle(LED_RED); // turn the LED on (HIGH is the voltage level)
|
||||
delay(1000); // wait for a second
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*********************************************************************
|
||||
This is an example for our Feather Bluefruit modules
|
||||
|
||||
Pick one up today in the adafruit shop!
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
MIT license, check LICENSE for more information
|
||||
All text above, and the splash screen below must be included in
|
||||
any redistribution
|
||||
*********************************************************************/
|
||||
|
||||
/*
|
||||
* This sketch demonstrate how to pass ISR_DEFFERED as additional parameter
|
||||
* to defer callback from ISR context with attachInterrupt
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
|
||||
int interruptPin = A0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(interruptPin, INPUT_PULLUP);
|
||||
|
||||
// ISR_DEFERRED flag cause the callback to be deferred from ISR context
|
||||
// and invoked within a callback thread.
|
||||
// It is required to use ISR_DEFERRED if callback function take long time
|
||||
// to run e.g Serial.print() or using any of Bluefruit API() which will
|
||||
// potentially call rtos API
|
||||
attachInterrupt(interruptPin, digital_callback, ISR_DEFERRED | CHANGE);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
void digital_callback(void)
|
||||
{
|
||||
Serial.print("Pin value: ");
|
||||
Serial.println(digitalRead(interruptPin));
|
||||
}
|
||||
@@ -18,6 +18,8 @@
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Firmware Info Example");
|
||||
Serial.println("---------------------------------\n");
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ void setup() {
|
||||
|
||||
// Init hardware UART <-> Serial Monitor
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("GPS echo test");
|
||||
|
||||
// Init Software Uart <-> GPS FeatherWing
|
||||
|
||||
@@ -33,7 +33,7 @@ extern "C"
|
||||
*/
|
||||
void SysTick_Handler(void)
|
||||
{
|
||||
ledToggle(LED_RED);
|
||||
digitalToggle(LED_RED);
|
||||
}
|
||||
|
||||
} // extern C
|
||||
|
||||
@@ -10,6 +10,7 @@ typedef volatile uint32_t REG32;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit 52 HW Info");
|
||||
Serial.println("");
|
||||
|
||||
@@ -28,8 +28,8 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// Maximum 12 pins can be used for 3 PWM module ( 4 channel each )
|
||||
#if defined ARDUINO_NRF52_FEATHER
|
||||
// Maximum 12 pins can be used for 3 PWM modules ( 4 channel each )
|
||||
// Note: nRF52832 has 3 PWM modules, nRF52840 has 4 PWM modules
|
||||
int pins[12] =
|
||||
{
|
||||
A0 , A1 , A2 , A3,
|
||||
@@ -37,17 +37,6 @@ int pins[12] =
|
||||
27 , LED_BLUE, PIN_WIRE_SDA, PIN_WIRE_SCL
|
||||
};
|
||||
|
||||
#elif defined ARDUINO_NRF52_METRO
|
||||
|
||||
int pins[12] =
|
||||
{
|
||||
D0, D1 , D2, D3,
|
||||
D4, D5 , D6, LED_RED,
|
||||
D7, LED_BLUE, D8, D9
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief The setup function runs once when reset the board
|
||||
@@ -55,8 +44,6 @@ int pins[12] =
|
||||
/**************************************************************************/
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
// Add 4 pins into a group
|
||||
// It is better to add Pin before call .begin()
|
||||
for (int i=0; i<12; i++)
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Memory Info Example");
|
||||
Serial.println("-------------------------------\n");
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 NFC to GPIO Pin Config");
|
||||
Serial.println("----------------------------------\n");
|
||||
if ((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)){
|
||||
|
||||
+3
-1
@@ -778,7 +778,9 @@ void systemResetCallback()
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Standard Firmata via BLEUART Example");
|
||||
Serial.println("------------------------------------------------\n");
|
||||
|
||||
|
||||
@@ -20,25 +20,20 @@
|
||||
*/
|
||||
#include <bluefruit.h>
|
||||
|
||||
#define PIN_ADV 11
|
||||
#define PIN_ADV A0
|
||||
#define ADV_TIMEOUT 60 // seconds
|
||||
|
||||
// Software Timer for blinking RED LED
|
||||
SoftwareTimer blinkTimer;
|
||||
|
||||
void setup()
|
||||
{
|
||||
// configure PIN_ADV as input with a pullup (pin is active low)
|
||||
pinMode(PIN_ADV, INPUT_PULLUP);
|
||||
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Advanced Advertising Example");
|
||||
Serial.println("----------------------------------------\n");
|
||||
|
||||
// Initialize blinkTimer for 1000 ms and start it
|
||||
blinkTimer.begin(1000, blink_timer_callback);
|
||||
blinkTimer.start();
|
||||
|
||||
Bluefruit.begin();
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
@@ -94,18 +89,3 @@ void adv_stop_callback(void)
|
||||
{
|
||||
Serial.println("Advertising time passed, advertising will now stop.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Software Timer callback is invoked via a built-in FreeRTOS thread with
|
||||
* minimal stack size. Therefore it should be as simple as possible. If
|
||||
* a periodically heavy task is needed, please use Scheduler.startLoop() to
|
||||
* create a dedicated task for it.
|
||||
*
|
||||
* More information http://www.freertos.org/RTOS-software-timer.html
|
||||
*/
|
||||
void blink_timer_callback(TimerHandle_t xTimerID)
|
||||
{
|
||||
(void) xTimerID;
|
||||
digitalToggle(LED_RED);
|
||||
}
|
||||
|
||||
@@ -37,13 +37,21 @@ const char* CAT_STR [] =
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 BLE ANCS Example");
|
||||
Serial.println("----------------------------\n");
|
||||
|
||||
Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52");
|
||||
Serial.println("It may appear up as 'Accessory' depending on your OS version.");
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
// more SRAM required by SoftDevice
|
||||
// Note: All config***() function must be called before begin()
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
@@ -194,6 +202,11 @@ void ancs_notification_callback(AncsNotification_t* notif)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
@@ -27,11 +27,24 @@
|
||||
#include <bluefruit.h>
|
||||
|
||||
/*------------- OLED and Buttons -------------*/
|
||||
#if defined ARDUINO_NRF52_FEATHER
|
||||
// Feather nRF52832
|
||||
#define BUTTON_A 31
|
||||
#define BUTTON_B 30
|
||||
#define BUTTON_C 27
|
||||
|
||||
#define OLED_RESET 4
|
||||
#elif defined ARDUINO_NRF52840_FEATHER
|
||||
// Feather nRF52840
|
||||
#define BUTTON_A 9
|
||||
#define BUTTON_B 6
|
||||
#define BUTTON_C 5
|
||||
|
||||
#else
|
||||
#error board not supported
|
||||
#endif
|
||||
|
||||
|
||||
#define OLED_RESET 4 // TODO remove ?
|
||||
Adafruit_SSD1306 oled(OLED_RESET);
|
||||
|
||||
|
||||
@@ -77,6 +90,11 @@ void setup()
|
||||
oled.setTextSize(1);// max is 4 line, 21 chars each
|
||||
oled.setTextColor(WHITE);
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
// more SRAM required by SoftDevice
|
||||
// Note: All config***() function must be called before begin()
|
||||
//Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
@@ -358,6 +376,11 @@ void ancs_notification_callback(AncsNotification_t* notif)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
|
||||
@@ -34,6 +34,7 @@ BLEBeacon beacon(beaconUuid, 0x0000, 0x0000, -54);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Beacon Example");
|
||||
Serial.println("--------------------------\n");
|
||||
@@ -90,4 +91,3 @@ void loop()
|
||||
{
|
||||
// loop is already suspended, CPU will not run loop() at all
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ byte note_sequence[] = {
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Adafruit Bluefruit52 MIDI over Bluetooth LE Example");
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
|
||||
@@ -14,23 +14,18 @@
|
||||
#include <bluefruit.h>
|
||||
|
||||
// BLE Service
|
||||
BLEDis bledis;
|
||||
BLEUart bleuart;
|
||||
BLEBas blebas;
|
||||
|
||||
// Software Timer for blinking RED LED
|
||||
SoftwareTimer blinkTimer;
|
||||
BLEDis bledis; // device information
|
||||
BLEUart bleuart; // uart over ble
|
||||
BLEBas blebas; // battery
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 BLEUART Example");
|
||||
Serial.println("---------------------------\n");
|
||||
|
||||
// Initialize blinkTimer for 1000 ms and start it
|
||||
blinkTimer.begin(1000, blink_timer_callback);
|
||||
blinkTimer.start();
|
||||
|
||||
// Setup the BLE LED to be enabled on CONNECT
|
||||
// Note: This is actually the default behaviour, but provided
|
||||
// here in case you want to control this LED manually via PIN 19
|
||||
@@ -121,6 +116,7 @@ void loop()
|
||||
waitForEvent();
|
||||
}
|
||||
|
||||
// callback invoked when central connects
|
||||
void connect_callback(uint16_t conn_handle)
|
||||
{
|
||||
char central_name[32] = { 0 };
|
||||
@@ -130,6 +126,11 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.println(central_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
@@ -138,39 +139,3 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
Serial.println();
|
||||
Serial.println("Disconnected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Software Timer callback is invoked via a built-in FreeRTOS thread with
|
||||
* minimal stack size. Therefore it should be as simple as possible. If
|
||||
* a periodically heavy task is needed, please use Scheduler.startLoop() to
|
||||
* create a dedicated task for it.
|
||||
*
|
||||
* More information http://www.freertos.org/RTOS-software-timer.html
|
||||
*/
|
||||
void blink_timer_callback(TimerHandle_t xTimerID)
|
||||
{
|
||||
(void) xTimerID;
|
||||
digitalToggle(LED_RED);
|
||||
}
|
||||
|
||||
/**
|
||||
* RTOS Idle callback is automatically invoked by FreeRTOS
|
||||
* when there are no active threads. E.g when loop() calls delay() and
|
||||
* there is no bluetooth or hw event. This is the ideal place to handle
|
||||
* background data.
|
||||
*
|
||||
* NOTE: FreeRTOS is configured as tickless idle mode. After this callback
|
||||
* is executed, if there is time, freeRTOS kernel will go into low power mode.
|
||||
* Therefore waitForEvent() should not be called in this callback.
|
||||
* http://www.freertos.org/low-power-tickless-rtos.html
|
||||
*
|
||||
* WARNING: This function MUST NOT call any blocking FreeRTOS API
|
||||
* such as delay(), xSemaphoreTake() etc ... for more information
|
||||
* http://www.freertos.org/a00016.html
|
||||
*/
|
||||
void rtos_idle_callback(void)
|
||||
{
|
||||
// Don't call any other FreeRTOS blocking API()
|
||||
// Perform background task(s) here
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Blinky Example");
|
||||
Serial.println("--------------------------\n");
|
||||
@@ -40,7 +41,7 @@ void startAdv(void)
|
||||
/* Start Advertising
|
||||
* - Enable auto advertising if disconnected
|
||||
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
|
||||
* - Timeout for fast mode is 30 seconds
|
||||
* - Timeout for fast mode is 30 seconds
|
||||
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
|
||||
*
|
||||
* For recommended advertising interval
|
||||
|
||||
@@ -17,12 +17,13 @@
|
||||
*/
|
||||
|
||||
#include <bluefruit.h>
|
||||
#include <Nffs.h>
|
||||
#include <utility/bonding.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Clear Bonds Example");
|
||||
Serial.println("-------------------------------\n");
|
||||
|
||||
|
||||
@@ -33,12 +33,19 @@ BLEClientCts bleCTime;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 BLE Client Current Time Example");
|
||||
Serial.println("-------------------------------------------\n");
|
||||
|
||||
Serial.println("Go to iOS's Bluetooth settings and connect to Bluefruit52");
|
||||
Serial.println("It may appear up as 'Accessory' depending on your iOS version.");
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
// more SRAM required by SoftDevice
|
||||
// Note: All config***() function must be called before begin()
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
@@ -154,7 +161,11 @@ void printTime(void)
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
@@ -47,6 +47,11 @@ void setup()
|
||||
oled.setTextSize(1);// max is 4 line, 21 chars each
|
||||
oled.setTextColor(WHITE);
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
// more SRAM required by SoftDevice
|
||||
// Note: All config***() function must be called before begin()
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
@@ -171,7 +176,11 @@ void printTime(void)
|
||||
oled.display();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) reason;
|
||||
|
||||
@@ -27,6 +27,8 @@ extern uint8_t packetbuffer[];
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println(F("Adafruit Bluefruit52 Controller App Example"));
|
||||
Serial.println(F("-------------------------------------------"));
|
||||
|
||||
|
||||
@@ -36,6 +36,8 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 HRM Example");
|
||||
Serial.println("-----------------------\n");
|
||||
|
||||
@@ -172,6 +174,11 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.println(central_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
@@ -220,24 +227,3 @@ void loop()
|
||||
// Only send update once per second
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* RTOS Idle callback is automatically invoked by FreeRTOS
|
||||
* when there are no active threads. E.g when loop() calls delay() and
|
||||
* there is no bluetooth or hw event. This is the ideal place to handle
|
||||
* background data.
|
||||
*
|
||||
* NOTE: FreeRTOS is configured as tickless idle mode. After this callback
|
||||
* is executed, if there is time, freeRTOS kernel will go into low power mode.
|
||||
* Therefore waitForEvent() should not be called in this callback.
|
||||
* http://www.freertos.org/low-power-tickless-rtos.html
|
||||
*
|
||||
* WARNING: This function MUST NOT call any blocking FreeRTOS API
|
||||
* such as delay(), xSemaphoreTake() etc ... for more information
|
||||
* http://www.freertos.org/a00016.html
|
||||
*/
|
||||
void rtos_idle_callback(void)
|
||||
{
|
||||
// Don't call any other FreeRTOS blocking API()
|
||||
// Perform background task(s) here
|
||||
}
|
||||
@@ -34,6 +34,8 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Heath Thermometer Example");
|
||||
Serial.println("-------------------------------------\n");
|
||||
|
||||
@@ -156,6 +158,11 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.println(central_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
|
||||
@@ -21,6 +21,7 @@ EddyStoneUrl eddyUrl(-40, URL);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 EddyStone URL Example");
|
||||
Serial.println("---------------------------------\n");
|
||||
|
||||
+2
-1
@@ -22,13 +22,14 @@
|
||||
BLEDis bledis;
|
||||
BLEHidAdafruit blehid;
|
||||
|
||||
#define PIN_SHUTTER 11
|
||||
#define PIN_SHUTTER A0
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(PIN_SHUTTER, INPUT_PULLUP);
|
||||
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 HID Camera Shutter Example");
|
||||
Serial.println("--------------------------------------\n");
|
||||
|
||||
@@ -21,6 +21,7 @@ bool hasKeyPressed = false;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 HID Keyboard Example");
|
||||
Serial.println("--------------------------------\n");
|
||||
|
||||
@@ -37,6 +37,7 @@ bool keyPressedPreviously = false;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 HID Keyscan Example");
|
||||
Serial.println("-------------------------------\n");
|
||||
@@ -116,14 +117,14 @@ void startAdv(void)
|
||||
|
||||
void loop()
|
||||
{
|
||||
/*-------------- San Pin Array and send report ---------------------*/
|
||||
/*-------------- Scan Pin Array and send report ---------------------*/
|
||||
bool anyKeyPressed = false;
|
||||
|
||||
uint8_t modifier = 0;
|
||||
uint8_t count=0;
|
||||
uint8_t keycode[6] = { 0 };
|
||||
|
||||
// scan mofidier key (only SHIFT), user implement ATL, CTRL, CMD if needed
|
||||
// scan modifier key (only SHIFT), user implement ATL, CTRL, CMD if needed
|
||||
if ( 0 == digitalRead(shiftPin) )
|
||||
{
|
||||
modifier |= KEYBOARD_MODIFIER_LEFTSHIFT;
|
||||
|
||||
@@ -21,6 +21,7 @@ BLEHidAdafruit blehid;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 HID Mouse Example");
|
||||
Serial.println("-----------------------------\n");
|
||||
|
||||
@@ -67,8 +67,10 @@ BLEUart bleuart;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(F("Adafruit Bluefruit NeoMatrix"));
|
||||
Serial.println(F("----------------------------"));
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Adafruit Bluefruit NeoMatrix");
|
||||
Serial.println("----------------------------");
|
||||
|
||||
Serial.println();
|
||||
Serial.println("Please connect using Bluefruit Connect LE application");
|
||||
|
||||
@@ -49,6 +49,8 @@ BLEUart bleuart;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Adafruit Bluefruit Neopixel Test");
|
||||
Serial.println("--------------------------------");
|
||||
|
||||
|
||||
@@ -33,6 +33,8 @@ BLEUart bleuart;
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Throughput Example");
|
||||
Serial.println("------------------------------\n");
|
||||
|
||||
@@ -93,6 +95,11 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.println("Connected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback invoked when a connection is dropped
|
||||
* @param conn_handle connection where this event happens
|
||||
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
|
||||
*/
|
||||
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
{
|
||||
(void) conn_handle;
|
||||
|
||||
+97
-3
@@ -12,14 +12,102 @@
|
||||
any redistribution
|
||||
*********************************************************************/
|
||||
|
||||
#include <Nffs.h>
|
||||
#include <bluefruit.h>
|
||||
#include <BLEHomekit.h>
|
||||
|
||||
#include "crypto/crypto.h"
|
||||
|
||||
#define CRYPTO_KEYFILE "/adafruit/homekit/key"
|
||||
|
||||
BLEHomekit homekit;
|
||||
|
||||
uint8_t test_keys[] =
|
||||
{
|
||||
0x55, 0x02, 0x29, 0x6A, 0x41, 0xB0, 0xF5, 0x35, 0x61, 0xFF, 0x82, 0x52, 0x2E, 0x77, 0xF0, 0xC6,
|
||||
0x1A, 0x2B, 0x6F, 0xD9, 0x4B, 0x48, 0x01, 0xCA, 0x37, 0xAF, 0x90, 0x74, 0x78, 0x07, 0xF8, 0xBE,
|
||||
0x58, 0xE8, 0xAD, 0x10, 0xB9, 0x69, 0xA3, 0x03, 0x0E, 0x42, 0x93, 0x2F, 0xF6, 0x3F, 0xE6, 0x40,
|
||||
0x71, 0x3F, 0x12, 0x63, 0x0B, 0x38, 0xEE, 0xF2, 0xDC, 0xCC, 0x1D, 0x24, 0x08, 0x70, 0xA3, 0xAD,
|
||||
0x8D, 0x16, 0xC0, 0x06, 0x24, 0x31, 0xCD, 0xB5, 0x34, 0xC8, 0x92, 0x3F, 0x3C, 0xF4, 0x43, 0x99,
|
||||
0xE1, 0x2B, 0x0C, 0x4B, 0xEE, 0xB6, 0xD9, 0xB6, 0xFD, 0x03, 0x43, 0xAD, 0x64, 0xB6, 0x6A, 0x19,
|
||||
0xDE, 0xA6, 0x34, 0x73, 0xC9, 0xB1, 0xB9, 0xBC, 0x5A, 0x09, 0xEF, 0x33, 0x1F, 0x32, 0xC9, 0xDD,
|
||||
0x71, 0xD3, 0xB7, 0x68, 0x89, 0xC6, 0x35, 0x36, 0x74, 0x9B, 0xE9, 0x84, 0x16, 0xF5, 0xAB, 0xCB,
|
||||
0x83, 0xA0, 0x77, 0x0E, 0xE2, 0x9A, 0x10, 0x36, 0xCC, 0xA9, 0xAE, 0xE3, 0x30, 0x8E, 0x24, 0x2F,
|
||||
0xCA, 0xC5, 0xAC, 0x37, 0x03, 0xF6, 0xEA, 0x1A, 0x22, 0x81, 0x61, 0x98, 0xCC, 0x76, 0x6D, 0x6E,
|
||||
0x46, 0x27, 0x50, 0x40, 0xD5, 0x15, 0xBB, 0xF3, 0x83, 0x08, 0x83, 0x3D, 0xDF, 0xED, 0x23, 0x20,
|
||||
0x70, 0x3B, 0xAB, 0xFD, 0x07, 0x0E, 0x92, 0x12, 0x10, 0xB0, 0xAE, 0x5F, 0xE2, 0x9E, 0x82, 0x33,
|
||||
0xD6, 0xE4, 0xA9, 0x70, 0x44, 0x6B, 0xE7, 0x7E, 0x4E, 0xA7, 0xF3, 0x63, 0xE3, 0x0F, 0xAB, 0x24,
|
||||
0xD4, 0xF4, 0x6A, 0xFC, 0x32, 0xDC, 0x4E, 0xA2, 0x4F, 0xBE, 0x25, 0x40, 0x49, 0xA7, 0xB1, 0x0F,
|
||||
0xF6, 0x4F, 0x1F, 0xFF, 0x08, 0x13, 0x46, 0x55, 0x45, 0x90, 0x6E, 0x09, 0xEA, 0x81, 0x60, 0x66,
|
||||
0x61, 0x49, 0x41, 0xC5, 0x23, 0xC6, 0xCE, 0xD8, 0xF4, 0xCB, 0x3D, 0x3B, 0xAC, 0x6E, 0x28, 0xDD,
|
||||
0x19, 0x58, 0x96, 0x3B, 0xC9, 0x39, 0x46, 0x46, 0xC4, 0x90, 0xCF, 0xFB, 0xAE, 0xFD, 0x30, 0xFB,
|
||||
0x36, 0x14, 0x36, 0xF4, 0x5E, 0xDD, 0x04, 0x2E, 0x3F, 0x5E, 0xCD, 0x71, 0x6B, 0x55, 0xD7, 0xF3,
|
||||
0x59, 0x24, 0x08, 0x58, 0x90, 0x71, 0x1F, 0x88, 0x53, 0xA0, 0xC1, 0xB8, 0x5F, 0x3D, 0x94, 0xF3,
|
||||
0xDB, 0xFC, 0x3F, 0x59, 0x41, 0xBF, 0x41, 0xC3, 0xC4, 0x70, 0x1A, 0x0C, 0x9C, 0x9A, 0x54, 0x5E,
|
||||
0xD3, 0xF0, 0x82, 0xD9, 0xF9, 0xCF, 0x0E, 0xC0, 0x53, 0xAD, 0x2E, 0xFA, 0x99, 0x1A, 0x57, 0x41,
|
||||
0x4F, 0x71, 0x2B, 0x47, 0x31, 0xB3, 0x18, 0xC4, 0xAE, 0xCD, 0xCC, 0xD4, 0x14, 0x75, 0x48, 0xB9,
|
||||
0x06, 0x81, 0xBA, 0xD9, 0x60, 0x63, 0xD4, 0x3A, 0x26, 0xE0, 0x64, 0xD1, 0xF7, 0xE4, 0x9A, 0xC1,
|
||||
0xC1, 0x06, 0xBC, 0x51, 0x85, 0xEC, 0x33, 0x63, 0xCD, 0x25, 0x34, 0x13, 0xBF, 0x5B, 0x65, 0x2D,
|
||||
0x6C, 0xCE, 0x6F, 0x22, 0x77, 0x12, 0x4C, 0x32, 0xFD, 0x72, 0x5C, 0x65, 0x5F, 0x28, 0x38, 0x6B,
|
||||
0x52, 0x87, 0x6A, 0xCF, 0x8A, 0x7F, 0xC1, 0x66, 0x03, 0x0C, 0x6F, 0x00, 0x82, 0xA7, 0xA8, 0x73,
|
||||
0x28, 0x17, 0x19, 0xD3, 0x26, 0x90, 0x70, 0xAF, 0x2B, 0x86, 0x34, 0x96, 0x91, 0xD3, 0xB7, 0x65,
|
||||
0x48, 0xB7, 0xCB, 0xEA, 0xC4, 0xB1, 0x78, 0x1C, 0x76, 0x51, 0x2F, 0x68, 0x89, 0x42, 0xCD, 0x89,
|
||||
0xA7, 0xE0, 0x8B, 0xD8, 0x8D, 0x1F, 0xDA, 0xA6, 0xCE, 0xD6, 0x71, 0x9E, 0x7D, 0x98, 0x59, 0x66,
|
||||
0xAD, 0x91, 0x68, 0xF5, 0xE2, 0xA0, 0x78, 0xB3, 0x69, 0x65, 0x00, 0x94, 0xDC, 0x44, 0x09, 0xA2,
|
||||
0x2B, 0x71, 0x15, 0x9E, 0x33, 0xE7, 0x8F, 0x72, 0x44, 0x83, 0xF6, 0xF4, 0x9C, 0xDE, 0xF0, 0xC1,
|
||||
0xAC, 0x60, 0x01, 0x79, 0x24, 0x9E, 0xC0, 0x82, 0x68, 0xA4, 0xE8, 0x70, 0xF0, 0xBD, 0x80, 0x99,
|
||||
0x1E, 0x5A, 0xB5, 0x08, 0x03, 0x62, 0xC7, 0x3D, 0xD1, 0xFE, 0x23, 0xA7, 0xF1, 0x01, 0x1A, 0x37,
|
||||
0x23, 0x27, 0x6D, 0x39, 0x63, 0x75, 0x35, 0x60, 0x6B, 0xD1, 0xD3, 0x77, 0xB1, 0xA2, 0xE4, 0x6A,
|
||||
0x0A, 0xD9, 0xD9, 0x42, 0x37, 0x97, 0x0C, 0x73, 0x97, 0x98, 0x55, 0xC2, 0x22, 0x5B, 0x62, 0x4E,
|
||||
0x1A, 0xBB, 0xFB, 0xAC, 0x91, 0xB0, 0x68, 0xAD, 0x78, 0x48, 0x45, 0x42, 0xEF, 0x80, 0xE7, 0xB0,
|
||||
0xC2, 0xA0, 0x03, 0x37, 0xBD, 0x12, 0xFD, 0x3A, 0x65, 0x76, 0xE6, 0xBC, 0xE7, 0x98, 0x79, 0x6F,
|
||||
0x18, 0x10, 0x9E, 0x67, 0x62, 0x3D, 0x1B, 0x41, 0x8E, 0x2D, 0x99, 0x87, 0x7D, 0x75, 0xBE, 0x49,
|
||||
0x6E, 0x43, 0xE2, 0x9F, 0x71, 0x29, 0x1F, 0x45, 0xB6, 0x2B, 0xAE, 0x3B, 0x78, 0xEF, 0x09, 0x4A,
|
||||
0xBD, 0x5F, 0xC1, 0x4C, 0xE5, 0x22, 0xD9, 0x17, 0x11, 0xCE, 0x39, 0xD7, 0x11, 0x3B, 0x13, 0xE8,
|
||||
0x9B, 0x8D, 0xDF, 0x9E, 0xCB, 0x96, 0xDD, 0xD7, 0xA8, 0x0D, 0x0F, 0xBE, 0x6F, 0xF2, 0xAC, 0xEF,
|
||||
0x0E, 0xA9, 0xFE, 0x0C, 0xC9, 0xF8, 0xC4, 0x55, 0x36, 0x3D, 0xEF, 0x30, 0xA7, 0x33, 0x7B, 0xB8,
|
||||
0x99, 0x05, 0xDE, 0xEF, 0xAA, 0xDA, 0xCB, 0xD7, 0x15, 0x30, 0xF4, 0xE3, 0xC8, 0x72, 0x25, 0x4A,
|
||||
0xBE, 0x1B, 0x35, 0x92, 0x92, 0xDD, 0x4B, 0x1C, 0xA0, 0x6F, 0x7C, 0xEF, 0x95, 0x12, 0x38, 0xFB,
|
||||
0xC8, 0x4E, 0x80, 0xB8, 0x06, 0xC6, 0x6F, 0xFC, 0x3A, 0xB4, 0x54, 0xCA, 0xC9, 0x8F, 0xE7, 0xB1,
|
||||
0xA3, 0x29, 0xEB, 0x06, 0xD7, 0xE8, 0xFB, 0x25, 0xD4, 0x30, 0x6A, 0x9C, 0xCC, 0x7C, 0xC7, 0x7E,
|
||||
0x1F, 0xA8, 0xA4, 0x23, 0x11, 0x9B, 0x9A, 0xBD, 0x9D, 0x2E, 0x8D, 0x5C, 0x67, 0x85, 0x5C, 0xBB,
|
||||
0xF5, 0x55, 0xC0, 0x11, 0x32, 0x67, 0x58, 0x2A, 0x24, 0xC2, 0xAA, 0x8E, 0x46, 0xC3, 0xDB, 0x5A,
|
||||
0xA7, 0x02, 0xA3, 0x3D, 0x76, 0x41, 0xED, 0xF4, 0x4C, 0x67, 0xC0, 0xB1, 0x8F, 0xDE, 0x27, 0x8A,
|
||||
0xF6, 0x53, 0x66, 0xA6, 0x7E, 0xF9, 0xB6, 0x8D, 0xAD, 0x63, 0xC8, 0x9C, 0x44, 0xA2, 0xD8, 0x22,
|
||||
0x26, 0x01, 0xD9, 0x2A, 0x7E, 0xA5, 0xE8, 0x73, 0xDB, 0x6C, 0xC0, 0xB6, 0xAC, 0x8F, 0xAC, 0x4B,
|
||||
0xC4, 0xD7, 0x98, 0xFF, 0x5A, 0x18, 0x4B, 0xCF, 0x38, 0xD1, 0x7E, 0x2D, 0xDB, 0xDD, 0x9C, 0x94,
|
||||
0x66, 0x72, 0x1A, 0xBA, 0x9A, 0x98, 0x92, 0x9B, 0x16, 0x32, 0x2E, 0xF1, 0x9A, 0x83, 0x62, 0x3E,
|
||||
0x2D, 0x17, 0x64, 0xBF, 0xA5, 0x27, 0xA1, 0xE7, 0xAF, 0xB5, 0xD4, 0x68, 0x3E, 0x06, 0x68, 0xDA,
|
||||
0xF6, 0x7C, 0x5E, 0x47, 0x0E, 0x50, 0x63, 0x70, 0xDE, 0xF4, 0x2F, 0x42, 0x22, 0x32, 0x2A, 0xF6,
|
||||
0x8A, 0xEE, 0x33, 0x43, 0x37, 0x46, 0x46, 0x39, 0x37, 0x32, 0x2D, 0x30, 0x34, 0x30, 0x45, 0x2D,
|
||||
0x34, 0x39, 0x31, 0x35, 0x2D, 0x41, 0x38, 0x33, 0x44, 0x2D, 0x41, 0x39, 0x36, 0x34, 0x36, 0x39,
|
||||
0x30, 0x44, 0x44, 0x43, 0x43, 0x38, 0x4B, 0xB6, 0xEA, 0x1E, 0x22, 0x3A, 0xEE, 0x83, 0x7E, 0xBE,
|
||||
0xA9, 0xA4, 0x26, 0x8C, 0x37, 0x00, 0xA1, 0x1A, 0xC4, 0xC1, 0xA1, 0x00, 0xF5, 0x8A, 0x7A, 0xFB,
|
||||
0x2B, 0x45, 0x70, 0xE4, 0xB5, 0x45, 0xAA, 0x00
|
||||
};
|
||||
|
||||
void test_homekit(void)
|
||||
{
|
||||
extern srp_keys_t srp;
|
||||
extern crypto_keys_t crypto_keys;
|
||||
|
||||
return;
|
||||
|
||||
LOG_LV2_BUFFER("Private b", srp.b, 32);
|
||||
LOG_LV2_BUFFER("Salt s", srp.salt, 16);
|
||||
LOG_LV2_BUFFER("Verifier v", srp.v, 384);
|
||||
LOG_LV2_BUFFER("Public B", srp.B, 384);
|
||||
|
||||
LOG_LV2_BUFFER("Sign Secret", crypto_keys.sign.secret, 64);
|
||||
|
||||
LOG_LV2_BUFFER("Client Name", crypto_keys.client.name, 36);
|
||||
LOG_LV2_BUFFER("Client ltpk", crypto_keys.client.ltpk, 32);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Homekit Light Bulb Example");
|
||||
Serial.println("--------------------------------------\n");
|
||||
@@ -28,19 +116,25 @@ void setup()
|
||||
// is not enough. Therefore we need to incresae the ATTR Table Size
|
||||
// Note: All config***() function must be called before begin()
|
||||
Bluefruit.configAttrTableSize(3000);
|
||||
|
||||
|
||||
// Config the peripheral connection with maximum bandwidth
|
||||
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
|
||||
|
||||
Bluefruit.begin();
|
||||
|
||||
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
Bluefruit.setTxPower(4);
|
||||
Bluefruit.setName("Bluefruit52");
|
||||
|
||||
//Nffs.remove(CRYPTO_KEYFILE);
|
||||
//fsutil_write_file(CRYPTO_KEYFILE, &test_keys, sizeof(test_keys));
|
||||
|
||||
homekit.begin();
|
||||
|
||||
test_homekit();
|
||||
|
||||
// Set up and start advertising
|
||||
startAdv();
|
||||
|
||||
dbgMemInfo();
|
||||
}
|
||||
|
||||
void startAdv(void)
|
||||
|
||||
+6
@@ -139,6 +139,8 @@ node_record_t records[ARRAY_SIZE];
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Central Proximity Example");
|
||||
Serial.println("-------------------------------------\n");
|
||||
|
||||
@@ -362,6 +364,10 @@ void scan_callback(ble_gap_evt_adv_report_t* report)
|
||||
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
// For Softdevice v6: after received a report, scanner will be paused
|
||||
// We need to call Scanner resume() to continue scanning
|
||||
Bluefruit.Scanner.resume();
|
||||
}
|
||||
|
||||
#if ENABLE_TFT
|
||||
|
||||
+2
@@ -51,6 +51,8 @@ BLEUuid uuid = BLEUuid(CUSTOM_UUID);
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while ( !Serial ) delay(10); // for nrf52840 with native usb
|
||||
|
||||
Serial.println("Bluefruit52 Peripheral Proximity Example");
|
||||
Serial.println("----------------------------------------\n");
|
||||
|
||||
|
||||
@@ -220,6 +220,7 @@ setIntervalMS KEYWORD2
|
||||
filterRssi KEYWORD2
|
||||
filterMSD KEYWORD2
|
||||
filterUuid KEYWORD2
|
||||
filterService KEYWORD2
|
||||
clearFilters KEYWORD2
|
||||
|
||||
restartOnDisconnect KEYWORD2
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name=Adafruit BLE Libraries for Bluefruit52
|
||||
version=0.8.5
|
||||
name=Adafruit Bluefruit nRF52 Libraries
|
||||
version=0.9.2
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=Arduino library for nRF52-based Adafruit Bluefruit LE modules
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEAdvertising.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -47,7 +47,7 @@ BLEAdvertisingData::BLEAdvertisingData(void)
|
||||
|
||||
bool BLEAdvertisingData::addData(uint8_t type, const void* data, uint8_t len)
|
||||
{
|
||||
VERIFY( _count + len + 2 <= BLE_GAP_ADV_MAX_SIZE );
|
||||
VERIFY( _count + len + 2 <= BLE_GAP_ADV_SET_DATA_SIZE_MAX );
|
||||
|
||||
uint8_t* adv_data = &_data[_count];
|
||||
|
||||
@@ -173,23 +173,25 @@ bool BLEAdvertisingData::addService(BLEClientService& service)
|
||||
|
||||
/**
|
||||
* Add Name to Adv packet, use setName() to set
|
||||
* @return true if success
|
||||
* @return true if full name is added, false if shorten name or not enough data to add name
|
||||
*/
|
||||
bool BLEAdvertisingData::addName(void)
|
||||
{
|
||||
char name[BLE_GAP_ADV_MAX_SIZE+1];
|
||||
char name[BLE_GAP_ADV_SET_DATA_SIZE_MAX+1];
|
||||
|
||||
uint8_t type = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
|
||||
uint8_t len = Bluefruit.getName(name, sizeof(name));
|
||||
|
||||
// not enough for full name, chop it
|
||||
if (_count + len + 2 > BLE_GAP_ADV_MAX_SIZE)
|
||||
if (_count + len + 2 > BLE_GAP_ADV_SET_DATA_SIZE_MAX)
|
||||
{
|
||||
type = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;
|
||||
len = BLE_GAP_ADV_MAX_SIZE - (_count+2);
|
||||
len = BLE_GAP_ADV_SET_DATA_SIZE_MAX - (_count+2);
|
||||
}
|
||||
|
||||
return addData(type, name, len);
|
||||
VERIFY( addData(type, name, len) );
|
||||
|
||||
return type == BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
|
||||
}
|
||||
|
||||
// tx power is set by setTxPower
|
||||
@@ -229,7 +231,7 @@ uint8_t* BLEAdvertisingData::getData(void)
|
||||
|
||||
bool BLEAdvertisingData::setData(uint8_t const * data, uint8_t count)
|
||||
{
|
||||
VERIFY( data && (count <= BLE_GAP_ADV_MAX_SIZE) );
|
||||
VERIFY( data && (count <= BLE_GAP_ADV_SET_DATA_SIZE_MAX) );
|
||||
|
||||
memcpy(_data, data, count);
|
||||
_count = count;
|
||||
@@ -248,7 +250,8 @@ void BLEAdvertisingData::clearData(void)
|
||||
*------------------------------------------------------------------*/
|
||||
BLEAdvertising::BLEAdvertising(void)
|
||||
{
|
||||
_type = BLE_GAP_ADV_TYPE_ADV_IND;
|
||||
_hdl = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
||||
_type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
|
||||
_start_if_disconnect = true;
|
||||
_runnning = false;
|
||||
|
||||
@@ -335,22 +338,30 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
|
||||
// ADV Params
|
||||
ble_gap_adv_params_t adv_para =
|
||||
{
|
||||
.type = _type ,
|
||||
.p_peer_addr = NULL , // Undirected advertisement
|
||||
.fp = BLE_GAP_ADV_FP_ANY ,
|
||||
#if SD_VER < 500
|
||||
.p_whitelist = NULL ,
|
||||
#endif
|
||||
.interval = interval , // advertising interval (in units of 0.625 ms)
|
||||
.timeout = timeout ,
|
||||
//.channel_mask = { 0, 0, 0 } , // Enable all 3 adv channels
|
||||
.properties = {
|
||||
.type = _type,
|
||||
.anonymous = 0
|
||||
},
|
||||
.p_peer_addr = NULL , // Undirected advertisement
|
||||
.interval = interval , // advertising interval (in units of 0.625 ms)
|
||||
.duration = timeout*100 , // in 10-ms unit
|
||||
|
||||
.max_adv_evts = 0, // TODO can be used for fast/slow mode
|
||||
.channel_mask = { 0, 0, 0, 0, 0 } , // 40 channel, set 1 to disable
|
||||
.filter_policy = BLE_GAP_ADV_FP_ANY ,
|
||||
|
||||
//.primary_phy, .secondary_phy, .set_id, .scan_req_notification
|
||||
};
|
||||
|
||||
#if SD_VER < 500
|
||||
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para), false );
|
||||
#else
|
||||
VERIFY_STATUS( sd_ble_gap_adv_start(&adv_para, CONN_CFG_PERIPHERAL), false );
|
||||
#endif
|
||||
// gap_adv long-live is required by SD v6
|
||||
static ble_gap_adv_data_t gap_adv =
|
||||
{
|
||||
.adv_data = { .p_data = _data, .len = _count },
|
||||
.scan_rsp_data = { .p_data = Bluefruit.ScanResponse.getData(), .len = Bluefruit.ScanResponse.count() }
|
||||
};
|
||||
VERIFY_STATUS( sd_ble_gap_adv_set_configure(&_hdl, &gap_adv, &adv_para), false );
|
||||
VERIFY_STATUS( sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, _hdl, Bluefruit.getTxPower() ), false );
|
||||
VERIFY_STATUS( sd_ble_gap_adv_start(_hdl, CONN_CFG_PERIPHERAL), false );
|
||||
|
||||
Bluefruit._startConnLed(); // start blinking
|
||||
_runnning = true;
|
||||
@@ -365,9 +376,6 @@ bool BLEAdvertising::start(uint16_t timeout)
|
||||
{
|
||||
_stop_timeout = _left_timeout = timeout;
|
||||
|
||||
// Configure Data
|
||||
VERIFY_STATUS( sd_ble_gap_adv_data_set(_data, _count, Bluefruit.ScanResponse.getData(), Bluefruit.ScanResponse.count()), false );
|
||||
|
||||
// Initially advertising in fast mode
|
||||
// Fast mode blink 2x than slow mode
|
||||
Bluefruit.setConnLedInterval(CFG_ADV_BLINKY_INTERVAL/2);
|
||||
@@ -378,7 +386,7 @@ bool BLEAdvertising::start(uint16_t timeout)
|
||||
|
||||
bool BLEAdvertising::stop(void)
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_adv_stop(), false);
|
||||
VERIFY_STATUS( sd_ble_gap_adv_stop(_hdl), false);
|
||||
|
||||
_runnning = false;
|
||||
Bluefruit._stopConnLed(); // stop blinking
|
||||
@@ -417,8 +425,8 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_TIMEOUT:
|
||||
if (evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING)
|
||||
case BLE_GAP_EVT_ADV_SET_TERMINATED:
|
||||
if (evt->evt.gap_evt.params.adv_set_terminated.reason == BLE_GAP_EVT_ADV_SET_TERMINATED_REASON_TIMEOUT)
|
||||
{
|
||||
_runnning = false;
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEAdvertising.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -72,7 +72,7 @@ class Advertisable
|
||||
class BLEAdvertisingData
|
||||
{
|
||||
protected:
|
||||
uint8_t _data[BLE_GAP_ADV_MAX_SIZE];
|
||||
uint8_t _data[BLE_GAP_ADV_SET_DATA_SIZE_MAX];
|
||||
uint8_t _count;
|
||||
|
||||
public:
|
||||
@@ -148,6 +148,7 @@ public:
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
private:
|
||||
uint8_t _hdl;
|
||||
uint8_t _type;
|
||||
bool _start_if_disconnect;
|
||||
bool _runnning;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLECentral.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -81,11 +81,8 @@ bool BLECentral::connect(const ble_gap_addr_t* peer_addr)
|
||||
.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
#if SD_VER < 500
|
||||
VERIFY_STATUS( sd_ble_gap_connect(peer_addr, Bluefruit.Scanner.getParams(), &gap_conn_params), false );
|
||||
#else
|
||||
VERIFY_STATUS( sd_ble_gap_connect(peer_addr, Bluefruit.Scanner.getParams(), &gap_conn_params, CONN_CFG_CENTRAL), false );
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -168,7 +165,7 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
if ( Bluefruit.Gap.getRole(evt_conn_hdl) == BLE_GAP_ROLE_CENTRAL)
|
||||
{
|
||||
// Invoke callback
|
||||
// Invoke callback reason is BLE_HCI_STATUS code
|
||||
if ( _disconnect_cb) ada_callback(NULL, _disconnect_cb, evt_conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLECentral.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLECharacteristic.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2016, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -455,12 +455,12 @@ uint16_t BLECharacteristic::write32(int num)
|
||||
* @param offset offset of value (dfeault is 0)
|
||||
* @return number of read bytes
|
||||
*/
|
||||
uint16_t BLECharacteristic::read(void* buffer, uint16_t bufsize)
|
||||
uint16_t BLECharacteristic::read(void* buffer, uint16_t bufsize, uint16_t offset)
|
||||
{
|
||||
ble_gatts_value_t value =
|
||||
{
|
||||
.len = bufsize,
|
||||
.offset = 0, // TODO gatts long read
|
||||
.offset = offset,
|
||||
.p_value = (uint8_t*) buffer
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLECharacteristic.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2016, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -128,7 +128,7 @@ class BLECharacteristic
|
||||
|
||||
|
||||
/*------------- Read -------------*/
|
||||
uint16_t read(void* buffer, uint16_t bufsize);
|
||||
uint16_t read(void* buffer, uint16_t bufsize, uint16_t offset = 0);
|
||||
|
||||
uint8_t read8 (void);
|
||||
uint16_t read16(void);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEClientCharacteristic.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -221,7 +221,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
};
|
||||
|
||||
_adamsg.prepare( (void*) data, len);
|
||||
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), ¶m) );
|
||||
VERIFY_STATUS(sd_ble_gattc_write(_service->connHandle(), ¶m), 0);
|
||||
|
||||
// len is always 0 in BLE_GATTC_EVT_WRITE_RSP for BLE_GATT_OP_WRITE_REQ
|
||||
count = (_adamsg.waitUntilComplete(BLE_GENERIC_TIMEOUT) < 0 ? 0 : len);
|
||||
@@ -230,7 +230,6 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
{
|
||||
/*------------- Long Write Sequence -------------*/
|
||||
// For BLE_GATT_OP_PREP_WRITE_REQ, 2 bytes are used for offset
|
||||
|
||||
ble_gattc_write_params_t param =
|
||||
{
|
||||
.write_op = BLE_GATT_OP_PREP_WRITE_REQ,
|
||||
@@ -242,7 +241,7 @@ uint16_t BLEClientCharacteristic::write_resp(const void* data, uint16_t len)
|
||||
};
|
||||
|
||||
_adamsg.prepare( (void*) data, len);
|
||||
VERIFY_STATUS ( sd_ble_gattc_write(_service->connHandle(), ¶m) );
|
||||
VERIFY_STATUS(sd_ble_gattc_write(_service->connHandle(), ¶m), 0);
|
||||
count = _adamsg.waitUntilComplete( (len/(max_payload-2) + 1) * BLE_GENERIC_TIMEOUT );
|
||||
|
||||
// delay to swallow last WRITE RESPONSE
|
||||
@@ -284,7 +283,7 @@ uint16_t BLEClientCharacteristic::write(const void* data, uint16_t len)
|
||||
while( remaining )
|
||||
{
|
||||
// TODO only Write without response consume a TX buffer
|
||||
if ( !Bluefruit.Gap.getWriteCmdPacket(_service->connHandle()) ) return NRF_ERROR_RESOURCES; //BLE_ERROR_NO_TX_PACKETS;
|
||||
if ( !Bluefruit.Gap.getWriteCmdPacket(_service->connHandle()) ) break;
|
||||
|
||||
uint16_t packet_len = min16(max_payload, remaining);
|
||||
|
||||
@@ -304,7 +303,7 @@ uint16_t BLEClientCharacteristic::write(const void* data, uint16_t len)
|
||||
u8data += packet_len;
|
||||
}
|
||||
|
||||
return len;
|
||||
return len-remaining;
|
||||
}
|
||||
|
||||
uint16_t BLEClientCharacteristic::write8(uint8_t value)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEClientCharacteristic.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEClientService.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEClientService.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEDiscovery.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEDiscovery.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGap.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -43,7 +43,6 @@ BLEGap::BLEGap(void)
|
||||
_cfg_prph.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
|
||||
_cfg_central.mtu_max = BLE_GATT_ATT_MTU_DEFAULT;
|
||||
|
||||
#if SD_VER >= 500
|
||||
_cfg_prph.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
|
||||
_cfg_prph.hvn_tx_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
|
||||
_cfg_prph.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
|
||||
@@ -51,7 +50,6 @@ BLEGap::BLEGap(void)
|
||||
_cfg_central.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
|
||||
_cfg_central.hvn_tx_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
|
||||
_cfg_central.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
|
||||
#endif
|
||||
|
||||
_sec_param = (ble_gap_sec_params_t)
|
||||
{
|
||||
@@ -71,20 +69,16 @@ BLEGap::BLEGap(void)
|
||||
|
||||
void BLEGap::configPrphConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
|
||||
{
|
||||
_cfg_prph.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
|
||||
#if SD_VER >= 500
|
||||
_cfg_prph.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
|
||||
#endif
|
||||
_cfg_prph.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
|
||||
_cfg_prph.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
|
||||
_cfg_prph.hvn_tx_qsize = hvn_qsize;
|
||||
_cfg_prph.wr_cmd_qsize = wrcmd_qsize;
|
||||
}
|
||||
|
||||
void BLEGap::configCentralConn(uint16_t mtu_max, uint8_t event_len, uint8_t hvn_qsize, uint8_t wrcmd_qsize)
|
||||
{
|
||||
_cfg_central.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
|
||||
#if SD_VER >= 500
|
||||
_cfg_central.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
|
||||
#endif
|
||||
_cfg_central.mtu_max = maxof(mtu_max, BLE_GATT_ATT_MTU_DEFAULT);
|
||||
_cfg_central.event_len = maxof(event_len, BLE_GAP_EVENT_LENGTH_MIN);
|
||||
_cfg_central.hvn_tx_qsize = hvn_qsize;
|
||||
_cfg_central.wr_cmd_qsize = wrcmd_qsize;
|
||||
}
|
||||
@@ -120,12 +114,7 @@ uint8_t BLEGap::getAddr(uint8_t mac[6])
|
||||
{
|
||||
ble_gap_addr_t addr;
|
||||
|
||||
#if SD_VER < 500
|
||||
sd_ble_gap_address_get(&addr);
|
||||
#else
|
||||
sd_ble_gap_addr_get(&addr);
|
||||
#endif
|
||||
|
||||
memcpy(mac, addr.addr, 6);
|
||||
|
||||
return addr.addr_type;
|
||||
@@ -142,15 +131,9 @@ bool BLEGap::setAddr(uint8_t mac[6], uint8_t type)
|
||||
ble_gap_addr_t addr;
|
||||
addr.addr_type = type;
|
||||
|
||||
VERIFY (type == BLE_GAP_ADDR_TYPE_PUBLIC || type == BLE_GAP_ADDR_TYPE_RANDOM_STATIC);
|
||||
|
||||
memcpy(addr.addr, mac, 6);
|
||||
|
||||
#if SD_VER < 500
|
||||
VERIFY_STATUS( sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr), false );
|
||||
#else
|
||||
VERIFY_STATUS( sd_ble_gap_addr_set(&addr), false );
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -176,13 +159,13 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
|
||||
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
|
||||
{
|
||||
// Check to see if we did bonded with current prph previously
|
||||
bond_data_t bdata;
|
||||
bond_keys_t bkeys;
|
||||
|
||||
if ( bond_find_cntr(&peer->addr, &bdata) )
|
||||
if ( bond_find_cntr(&peer->addr, &bkeys) )
|
||||
{
|
||||
cntr_ediv = bdata.peer_enc.master_id.ediv;
|
||||
cntr_ediv = bkeys.peer_enc.master_id.ediv;
|
||||
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, cntr_ediv);
|
||||
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false);
|
||||
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bkeys.peer_enc.master_id, &bkeys.peer_enc.enc_info), false);
|
||||
|
||||
}else
|
||||
{
|
||||
@@ -198,7 +181,7 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
|
||||
|
||||
xSemaphoreTake(peer->pair_sem, portMAX_DELAY);
|
||||
|
||||
// Failed to pair using centra stored keys, this happens when
|
||||
// Failed to pair using central stored keys, this happens when
|
||||
// Prph delete bonds while we did not --> let's remove the obsolete keyfile and move on
|
||||
if ( !peer->paired && (cntr_ediv != 0xffff) )
|
||||
{
|
||||
@@ -241,12 +224,8 @@ bool BLEGap::getHvnPacket(uint16_t conn_hdl)
|
||||
|
||||
bool BLEGap::getWriteCmdPacket(uint16_t conn_hdl)
|
||||
{
|
||||
#if SD_VER < 500
|
||||
return getHvnPacket(conn_hdl);
|
||||
#else
|
||||
VERIFY( (conn_hdl < BLE_MAX_CONN) && (_peers[conn_hdl].wrcmd_tx_sem != NULL) );
|
||||
return xSemaphoreTake(_peers[conn_hdl].wrcmd_tx_sem, ms2tick(BLE_GENERIC_TIMEOUT));
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t BLEGap::getMTU (uint16_t conn_hdl)
|
||||
@@ -282,14 +261,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
peer->att_mtu = BLE_GATT_ATT_MTU_DEFAULT;
|
||||
|
||||
// Init transmission buffer for notification
|
||||
#if SD_VER < 500
|
||||
uint8_t txbuf_max;
|
||||
(void) sd_ble_tx_packet_count_get(conn_hdl, &txbuf_max);
|
||||
peer->hvn_tx_sem = xSemaphoreCreateCounting(txbuf_max, txbuf_max);
|
||||
#else
|
||||
peer->hvn_tx_sem = xSemaphoreCreateCounting(getHvnQueueSize(conn_hdl), getHvnQueueSize(conn_hdl));
|
||||
#endif
|
||||
|
||||
peer->wrcmd_tx_sem = xSemaphoreCreateCounting(getWriteCmdQueueSize(conn_hdl), getWriteCmdQueueSize(conn_hdl));
|
||||
}
|
||||
break;
|
||||
@@ -312,11 +284,11 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
{
|
||||
// Pairing in progress, Peer asking for our info
|
||||
peer->bond_data = (bond_data_t*) rtos_malloc( sizeof(bond_data_t));
|
||||
VERIFY(peer->bond_data, );
|
||||
peer->bond_keys = (bond_keys_t*) rtos_malloc( sizeof(bond_keys_t));
|
||||
VERIFY(peer->bond_keys, );
|
||||
|
||||
bond_data_t* bdata = peer->bond_data;
|
||||
memclr(bdata, sizeof(bond_data_t));
|
||||
bond_keys_t* bkeys = peer->bond_keys;
|
||||
memclr(bkeys, sizeof(bond_keys_t));
|
||||
|
||||
peer->ediv = 0xFFFF; // invalid value for ediv
|
||||
|
||||
@@ -337,15 +309,15 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
ble_gap_sec_keyset_t keyset =
|
||||
{
|
||||
.keys_own = {
|
||||
.p_enc_key = &bdata->own_enc,
|
||||
.p_enc_key = &bkeys->own_enc,
|
||||
.p_id_key = NULL,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
},
|
||||
|
||||
.keys_peer = {
|
||||
.p_enc_key = &bdata->peer_enc,
|
||||
.p_id_key = &bdata->peer_id,
|
||||
.p_enc_key = &bkeys->peer_enc,
|
||||
.p_id_key = &bkeys->peer_id,
|
||||
.p_sign_key = NULL,
|
||||
.p_pk = NULL
|
||||
}
|
||||
@@ -361,40 +333,41 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
case BLE_GAP_EVT_AUTH_STATUS:
|
||||
{
|
||||
// Bonding process completed
|
||||
// Pairing process completed
|
||||
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
|
||||
|
||||
// Pairing succeeded --> save encryption keys ( Bonding )
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
peer->paired = true;
|
||||
peer->ediv = peer->bond_data->own_enc.master_id.ediv;
|
||||
peer->ediv = peer->bond_keys->own_enc.master_id.ediv;
|
||||
|
||||
bond_save_keys(peer->role, conn_hdl, peer->bond_data);
|
||||
bond_save_keys(peer->role, conn_hdl, peer->bond_keys);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
}
|
||||
|
||||
rtos_free(peer->bond_data);
|
||||
peer->bond_data = NULL;
|
||||
rtos_free(peer->bond_keys);
|
||||
peer->bond_keys = NULL;
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_INFO_REQUEST:
|
||||
{
|
||||
// Reconnection. If bonded previously, Central will ask for stored keys.
|
||||
// return security information. Otherwise NULL
|
||||
// Central ask for the stored keys.
|
||||
// - load key and return if bonded previously.
|
||||
// - Else return NULL --> Initiate key exchange
|
||||
ble_gap_evt_sec_info_request_t* sec_req = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
|
||||
|
||||
bond_data_t bdata;
|
||||
varclr(&bdata);
|
||||
bond_keys_t bkeys;
|
||||
varclr(&bkeys);
|
||||
|
||||
if ( bond_load_keys(peer->role, sec_req->master_id.ediv, &bdata) )
|
||||
if ( bond_load_keys(peer->role, sec_req->master_id.ediv, &bkeys) )
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bdata.own_enc.enc_info, &bdata.peer_id.id_info, NULL);
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
|
||||
|
||||
peer->ediv = bdata.own_enc.master_id.ediv;
|
||||
peer->ediv = bkeys.own_enc.master_id.ediv;
|
||||
} else
|
||||
{
|
||||
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
|
||||
@@ -407,12 +380,15 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
||||
|
||||
// Connection is secured (paired)
|
||||
// Occurs if bonded + reconnection, or we initiate the pairing process
|
||||
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
|
||||
{
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
|
||||
// Previously bonded --> secure by re-connection process --> Load & Set SysAttr (Apply Service Context)
|
||||
// Else Init SysAttr (first bonded)
|
||||
if ( !bond_load_cccd(peer->role, conn_hdl, peer->ediv) )
|
||||
{
|
||||
sd_ble_gatts_sys_attr_set(conn_hdl, NULL, 0, 0);
|
||||
}
|
||||
|
||||
peer->paired = true;
|
||||
}
|
||||
@@ -423,27 +399,14 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||
{
|
||||
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
|
||||
//
|
||||
// PRINT_INT(passkey_display->match_request);
|
||||
// PRINT_BUFFER(passkey_display->passkey, 6);
|
||||
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
|
||||
// PRINT_INT(passkey_display->match_request);
|
||||
// PRINT_BUFFER(passkey_display->passkey, 6);
|
||||
|
||||
// sd_ble_gap_auth_key_reply
|
||||
}
|
||||
break;
|
||||
|
||||
#if SD_VER < 500
|
||||
case BLE_EVT_TX_COMPLETE:
|
||||
if ( peer->hvn_tx_sem )
|
||||
{
|
||||
for(uint8_t i=0; i<evt->evt.common_evt.params.tx_complete.count; i++)
|
||||
{
|
||||
xSemaphoreGive(peer->hvn_tx_sem);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
#else
|
||||
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
|
||||
if ( peer->hvn_tx_sem )
|
||||
{
|
||||
@@ -524,8 +487,6 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
}
|
||||
break;
|
||||
|
||||
#endif
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGap.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -100,7 +100,7 @@ class BLEGap
|
||||
ble_gap_addr_t addr;
|
||||
|
||||
uint16_t ediv;
|
||||
bond_data_t* bond_data; // Shared keys with bonded device, size ~ 80 bytes
|
||||
bond_keys_t* bond_keys; // Shared keys with bonded device, size ~ 80 bytes
|
||||
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
@@ -127,7 +127,6 @@ class BLEGap
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
// friend class BLEGatt;
|
||||
};
|
||||
|
||||
#endif /* BLEGAP_H_ */
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGatt.cpp
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
@@ -67,7 +67,7 @@ uint16_t BLEGatt::readCharByUuid(uint16_t conn_hdl, BLEUuid bleuuid, void* buffe
|
||||
count = _adamsg.waitUntilComplete(5*BLE_GENERIC_TIMEOUT);
|
||||
}else
|
||||
{
|
||||
VERIFY_MESS(err);
|
||||
VERIFY_MESS(err, dbg_err_str);
|
||||
}
|
||||
_adamsg.stop();
|
||||
|
||||
@@ -217,20 +217,17 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
if (rd_rsp->count)
|
||||
{
|
||||
#if SD_VER < 500
|
||||
_adamsg.feed(rd_rsp->handle_value[0].p_value, rd_rsp->value_len);
|
||||
#else
|
||||
ble_gattc_handle_value_t hdl_value;
|
||||
ble_gattc_handle_value_t hdl_value;
|
||||
|
||||
if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) )
|
||||
{
|
||||
_adamsg.feed(hdl_value.p_value, rd_rsp->value_len);
|
||||
}
|
||||
#endif
|
||||
if ( ERROR_NONE == sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&evt->evt.gattc_evt, &hdl_value) )
|
||||
{
|
||||
_adamsg.feed(hdl_value.p_value, rd_rsp->value_len);
|
||||
}
|
||||
|
||||
_adamsg.complete();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GATTS_EVT_HVC:
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file BLEGatt.h
|
||||
@author hathach
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2017, Adafruit Industries (adafruit.com)
|
||||
Copyright (c) 2018, Adafruit Industries (adafruit.com)
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
|
||||
Loaded 100 of 700 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user