bluedroid BLE public key生成代码
时间: 2023-10-02 07:05:14 浏览: 188
android BLE源码
4星 · 用户满意度95%
以下是基于 Android Bluedroid BLE 栈的 public key 生成代码示例:
```c
#include "bt_target.h"
#include "bt_types.h"
#include "btu.h"
#include "btif/include/btif_common.h"
#include "btif/include/btif_config.h"
#include "btif/include/btif_util.h"
#include "stack/crypto_api.h"
#include "stack/crypto_toolbox/crypto_toolbox.h"
#include "stack/include/smp_api.h"
#include "stack/include/bt_octets.h"
#define ECC_KEY_LEN 32
static const uint8_t ecdh_p256_base_point_x[] = {
0x3f, 0x20, 0x84, 0x24, 0xa2, 0x7f, 0xb3, 0x5e,
0x13, 0x08, 0x8f, 0x5c, 0xb8, 0x3b, 0x08, 0x0f,
0xa7, 0x19, 0x26, 0xe7, 0x6a, 0x64, 0x1d, 0x3d,
0xf1, 0x2e, 0x2b, 0x7f, 0x62, 0x6a, 0xed, 0x51
};
static const uint8_t ecdh_p256_base_point_y[] = {
0x17, 0x8a, 0xee, 0x7c, 0x69, 0x52, 0x8d, 0x9e,
0x3a, 0x7c, 0x2c, 0x65, 0x6e, 0x9c, 0x60, 0x95,
0x99, 0x37, 0xd4, 0x7f, 0xbc, 0x27, 0xb0, 0x86,
0xce, 0x39, 0x52, 0xc9, 0x13, 0xcb, 0xfc, 0x82
};
static void bluedroid_ble_generate_public_key() {
uint8_t priv_key[ECC_KEY_LEN];
uint8_t public_key_x[ECC_KEY_LEN];
uint8_t public_key_y[ECC_KEY_LEN];
uint8_t base_point_x[ECC_KEY_LEN];
uint8_t base_point_y[ECC_KEY_LEN];
uint8_t random_num[ECC_KEY_LEN];
memset(priv_key, 0, ECC_KEY_LEN);
memset(public_key_x, 0, ECC_KEY_LEN);
memset(public_key_y, 0, ECC_KEY_LEN);
memset(base_point_x, 0, ECC_KEY_LEN);
memset(base_point_y, 0, ECC_KEY_LEN);
memset(random_num, 0, ECC_KEY_LEN);
memcpy(base_point_x, ecdh_p256_base_point_x, ECC_KEY_LEN);
memcpy(base_point_y, ecdh_p256_base_point_y, ECC_KEY_LEN);
CryptoRand((uint8_t*)random_num, ECC_KEY_LEN);
if (CryptoECCKeyGen(priv_key, public_key_x, public_key_y, base_point_x, base_point_y, random_num) < 0)
{
// Key generation failed!
return;
}
// Use public_key_x and public_key_y as public key
}
```
在该示例代码中,我们首先定义了 ECDSA 椭圆曲线参数 `ecdh_p256_base_point_x` 和 `ecdh_p256_base_point_y`,然后生成了一个随机数 `random_num` 作为私钥的种子,最终通过 `CryptoECCKeyGen` 函数生成公钥。你可以根据需要修改上述代码,并在你的应用程序中使用它来生成 Bluedroid BLE 栈的 public key。
阅读全文