天天看點

鴻蒙源碼分析(二十一)安全密鑰庫子產品下的hks_common.h分析

文章目錄

  • 安全密鑰庫子產品下的hks_common.h分析
    • 一、背景知識
    • 二、hks_common.h代碼分析
      • 2.1 變量宏定義
      • 2.2 結構體的定義
      • 2.3 函數聲明
    • 三、總結

安全密鑰庫子產品下的hks_common.h分析

本篇主題:本篇部落客要分析hks_common.h相關代碼

檔案路徑(security_huks\frameworks\huks_lite\source\hw_keystore_sdk\common\hks_common.h)

一、背景知識

背景知識主要為安全加密算法中的aead算法,gcm、ccm認證機制。

傳送門:[等下一期文章進行更新]

二、hks_common.h代碼分析

2.1 變量宏定義

2.1.1宏定義函數

#define hks_alg_hmac(hash_alg) \  //mac認證機制
    (HKS_ALG_HMAC_BASE | ((hash_alg) & HKS_ALG_HASH_MASK))

#define hks_alg_hmac_get_hash(hmac_alg) \ //hmac擷取哈希值
    (HKS_ALG_CATEGORY_HASH | ((hmac_alg) & HKS_ALG_HASH_MASK))
           
#define hks_key_type_is_dsa(type) \
    (HKS_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == \
    HKS_KEY_TYPE_DSA_PUBLIC_KEY)  //宏定義函數用來判斷是一個私鑰還是一對密鑰
           

下面都是相關SHA的變量定義,在代碼中的實際應用不多,由于類似代碼過多這裡不一一注釋。

#define HKS_MAC_TRUNCATION_OFFSET 8
/* SHA2-224 */
#define HKS_ALG_SHA_224                 ((uint32_t)0x01000008)
/* SHA2-256 */
#define HKS_ALG_SHA_256                 ((uint32_t)0x01000009)
/* SHA2-384 */
#define HKS_ALG_SHA_384                 ((uint32_t)0x0100000a)
/* SHA2-512 */
#define HKS_ALG_SHA_512                 ((uint32_t)0x0100000b)
/* SHA2-512/224 */
#define HKS_ALG_SHA_512_224             ((uint32_t)0x0100000c)
/* SHA2-512/256 */
#define HKS_ALG_SHA_512_256             ((uint32_t)0x0100000d)
/* SHA3-224 */
#define HKS_ALG_SHA3_224                ((uint32_t)0x01000010)
/* SHA3-256 */
#define HKS_ALG_SHA3_256                ((uint32_t)0x01000011)
/* SHA3-384 */
#define HKS_ALG_SHA3_384                ((uint32_t)0x01000012)
/* SHA3-512 */
#define HKS_ALG_SHA3_512                ((uint32_t)0x01000013)
/* ECDH KEY  AGREEMENT */
#define HKS_ALG_ECDH_RAW                ((uint32_t)0x31000001)
           
/* the file name of the key storage */
#define HKS_KEY_STORE_FILE_NAME "hks_keystore" //密鑰存儲的檔案名

/* the number of file clear data type */
#define HKS_INIT_DATA_TYPE_NUM    3//清除資料類型的數量

/* const define used in Ed25519 algorithm */
#define CRYPTO_SECRET_KEY_BYTES   64
#define CRYPTO_PUBLIC_KEY_BYTES   32
#define CRYPTO_BYTES              64
           

用來初始化資料類型的共用體,将不同情況的資料進行初始化。

enum hks_init_data_type {//初始化資料類型
    /* all zero */
    HKS_INIT_DATA_TYPE_ALL_ZERO = 0,
    /* all one */
    HKS_INIT_DATA_TYPE_ALL_ONE = 1,
    /* random */
    HKS_INIT_DATA_TYPE_RANDOM = 2,
    HKS_INIT_DATA_TYPE_MAX = HKS_INIT_DATA_TYPE_RANDOM
};

           

2.2 結構體的定義

aead加密模式介紹:aead是一種同時具備保密性,完整性和可認證性的加密形式。

AEAD 産生的原因很簡單,單純的對稱加密算法,其解密步驟是無法确認密鑰是否正确的。也就是說,加密後的資料可以用任何密鑰執行解密運算,得到一組疑似原始資料,而不知道密鑰是否是正确的,也不知道解密出來的原始資料是否正确。

是以,需要在單純的加密算法之上,加上一層驗證手段,來确認解密步驟是否正确。

這裡定義幾個aead會用到的的結構體。

struct hks_aead_operation {
    const mbedtls_cipher_info_t *cipher_info; //加密資訊
    mbedtls_gcm_context gcm; //gcm密文
    mbedtls_ccm_context ccm;  //ccm加密
    uint32_t core_alg; //核心程序
    uint8_t full_tag_length; //标記位的最大長度
    uint8_t tag_length;//标記位的長度
};
struct hks_aead_data {
    uint8_t *nonce; //目前即時資料
    size_t nonce_length; //即時資料長度
    uint8_t *additional_data; //additional資料
    size_t additional_data_length; //額外附加資料的長度
    uint8_t *ciphertext; //密文
    size_t ciphertext_length; //密文長度
    uint8_t *plaintext; //原文
    size_t plaintext_length; //原文長度
};
           

2.3 函數聲明

下面的代碼塊主要是對hks_common.h中出現的函數申明,下面各個函數的詳細分析均在下一篇部落格hks_common.c的詳細分析中。下面隻列舉出現的函數不做分析。

int32_t hks_malloc_init_ptr(uint32_t size, uint8_t **ptr);
int32_t hks_blob_init(struct hks_blob *blob, size_t nmemb, size_t size,
    uint8_t type);
void hks_blob_destroy(struct hks_blob *blob);
int32_t hks_is_valid_auth_id(const struct hks_key_param *key_param);
int32_t hks_is_valid_alias(const struct hks_blob *alias);
int32_t hks_cpy_key_param(struct hks_key_param *dst,
    const struct hks_key_param *src);
void hks_key_param_destroy(struct hks_key_param *pram);
void hks_check_return_code(int32_t s, int32_t *ret);
int32_t hks_mbedtls_aead_encrypt(const struct hks_blob *key,
    const struct hks_key_param *key_param, struct hks_aead_data *aead_data,
    size_t *ciphertext_output_length);

int32_t hks_aead_unpadded_locate_tag(size_t tag_length,
    const uint8_t *ciphertext, size_t ciphertext_length,
    size_t plaintext_size, const uint8_t **p_tag);

int32_t hks_mbedtls_aead_decrypt(const struct hks_blob *key,
    const struct hks_key_param *key_param,
    struct hks_aead_data *aead_data, size_t *plaintext_output_length);

int32_t hks_mbedtls_key_derivation(struct hks_blob *derived_key,
    const uint32_t alg, const struct hks_blob *kdf_key,
    const struct hks_blob *salt,
    const struct hks_blob *label);

int32_t hks_endian_swap(uint8_t *p_data, int32_t length);
int32_t hks_gen_random(uint8_t *random, uint32_t len);
int32_t hks_calc_sha256(const struct hks_blob *hash_src, uint32_t hash_src_num,
    struct hks_blob *hash_result);
int32_t hks_init_buf_data(uint8_t data_type, uint8_t *buf, uint32_t buf_len);
int32_t mbedtls_to_hks_error(int ret);
void hks_xor(const struct hks_blob *src1, const struct hks_blob *src2,
    struct hks_blob *result);
int32_t hks_reg_get_hardware_udid_callback(
    hks_get_hardware_udid_callback callback);
int32_t hks_get_hardware_udid(uint8_t *udid, uint32_t udid_len);
void crypto_hash_sha512(unsigned char *out, const unsigned char *in, const int len);

           

三、總結

以上就是hks_access.h的内容,簡單介紹了該檔案中變量的定義和一些結構體的聲明定義。感謝閱讀和點贊。

繼續閱讀