全部产品

使用其他OS对接ID²-KM

更新时间:2019-11-06 10:49:56

该文档介绍了如何在其他的第三方OS上通过ID²-KM的方式完成设备端的适配。

1. 适配

1.1 首先适配OSA基础功能接口。API接口详情参照安全SDK API
需要芯片/模组厂商实现的接口如下:

  1. void ls_osa_print(const char *fmt, …)
  2. int ls_osa_snprintf(char *str, size_t size, const char *fmt, ...)
  3. void *ls_osa_malloc(size_t size)
  4. void ls_osa_free(void *ptr)
  5. long long ls_osa_get_time_ms(void)

1.2 如果需要用到多线程的场景,则需要实现OSA多线程管理接口,否则可以直接返回0(对int类型的函数)或者直接返回(对void类型函数)。
需要芯片/模组厂商实现的接口如下:

  1. int ls_osa_mutex_create(void **mutex)
  2. void ls_osa_mutex_destroy(void *mutex)
  3. int ls_osa_mutex_lock(void *mutex)
  4. int ls_osa_mutex_unlock(void *mutex)

1.3 如果需要使用安全SDK 安全连接服务(iTLS),则需要实现OSA网络操作接口,否则可以直接返回0。
需要芯片/模组厂商实现的接口如下:

  1. int ls_osa_net_connect(const char *host, const char *port, int type)
  2. void ls_osa_net_disconnect(int fd)
  3. int ls_osa_net_send(int fd, unsigned char *buf, size_t len, int *ret_orig)
  4. int ls_osa_net_recv(int fd, unsigned char *buf, size_t len, int timeout, int *ret_orig)

1.4 适配Soft-KM HAL接口。(定义见 安全SDK函数接口说明- Soft-KM HAL接口):
需要芯片/模组厂商实现的接口如下:

  1. int ls_hal_get_dev_id(uint8_t *dev_id, uint32_t *id_len)
  2. int ls_hal_open_rsvd_part(int flag)
  3. int ls_hal_write_rsvd_part(int fd, uint32_t offset, void *data, uint32_t data_len)
  4. int ls_hal_read_rsvd_part(int fd, uint32_t offset, void *buffer, uint32_t read_len)
  5. int ls_hal_close_rsvd_part(int fd)

注意:
如果您不需要1.2、1.3中的接口,为防止编译过程中出现对应symbol找不到的问题,请在实现中直接return或者返回0。
代码示例(以网络接口为例):

  1. /* for void type, directly return */
  2. void ls_osa_net_disconnect(int fd) {
  3. return;
  4. }
  5. /* for int type, return 0 */
  6. int ls_osa_net_connect(const char *host, const char *port, int type) {
  7. return 0;
  8. }

2. 适配验证

编译并运行hal_test,显示结果如下:

  1. hal_test 打印结果
  2. <LS_LOG> PL ERR ls_hal_get_dev_id 167: short buffer id len is 1
  3. <LS_LOG> PL ERR ls_hal_get_dev_id 167: short buffer id len is 1
  4. <LS_LOG> PL ERR ls_hal_write_rsvd_part 245: bad param
  5. <LS_LOG> PL ERR ls_hal_read_rsvd_part 283: read failed real read len is -1
  6. <LS_LOG> INF hal_km_rsvd_part_stress_test 357: write 1K data total time: 1.000000ms, av_time: 0.001000ms
  7. <LS_LOG> INF hal_km_rsvd_part_stress_test 372: read 1K data total time: 1.000000ms, av_time: 0.001000ms
  8. <LS_LOG> INF hal_km_test 434: ============================> HAL KM Test Pass.
  9. <LS_LOG> INF hal_crypto_test 43: HAL Hash Test:
  10. <LS_LOG> INF hal_hash_test 140: SHA1 test success!
  11. <LS_LOG> INF hal_hash_test 190: SHA256 test success!
  12. <LS_LOG> INF hal_hash_test 239: md5 test success!
  13. <LS_LOG> INF hal_hash_test 249: =========================> HAL Hash Test Pass.
  14. <LS_LOG> INF hal_crypto_test 49: HAL Rand Test:
  15. <LS_LOG> INF hal_rand_test 22: ==========================> HAL Rand Test Pass.
  16. <LS_LOG> INF hal_crypto_test 55: HAL AES Test:
  17. <LS_LOG> INF hal_aes_test 682: HAL AES ECB Test:
  18. <LS_LOG> INF _aes_ecb_test 445: AES_ECB_128 test success!
  19. <LS_LOG> INF _aes_ecb_test 445: AES_ECB_192 test success!
  20. <LS_LOG> INF _aes_ecb_test 445: AES_ECB_256 test success!
  21. <LS_LOG> INF hal_aes_test 691: HAL AES CBC Test:
  22. <LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_128 test success!
  23. <LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_192 test success!
  24. <LS_LOG> INF _hal_aes_cbc_test 519: AES_CBC_256 test success!
  25. <LS_LOG> INF hal_aes_test 700: HAL AES CTR Test:
  26. <LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_128 test success!
  27. <LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_192 test success!
  28. <LS_LOG> INF _hal_aes_ctr_test 591: AES_CTR_256 test success!
  29. <LS_LOG> INF hal_aes_test 709: HAL AES CFB Test:
  30. <LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_128 test success!
  31. <LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_192 test success!
  32. <LS_LOG> INF _hal_aes_cfb_test 665: AES_CFB_256 test success!
  33. <LS_LOG> INF hal_aes_test 720: =============================> HAL AES Test Pass.
  34. <LS_LOG> INF hal_crypto_test 61: HAL RSA Test:
  35. <LS_LOG> INF _hal_encrypt_decrypt 150: RSA encrypt/decrypt test success
  36. <LS_LOG> INF hal_rsa_test 192: =============================> HAL RSA Test Pass.