1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #pragma once 19 20 #include "stack/ble/ble_format.h" 21 22 #define AES_BLOCK_SIZE 16 23 24 25 #define SUCCESS 0 26 enum { 27 AES_SUCC = SUCCESS, 28 AES_NO_BUF, 29 AES_FAIL, 30 }; 31 32 33 typedef struct { 34 u32 pkt; 35 u8 dir; 36 u8 iv[8]; 37 } ble_cyrpt_nonce_t; 38 39 40 typedef struct { 41 u64 enc_pno; 42 u64 dec_pno; 43 u8 sk[16]; //session key 44 ble_cyrpt_nonce_t nonce; 45 u8 st; 46 u8 enable; //1: slave enable; 2: master enable 47 u8 mic_fail; 48 } ble_crypt_para_t; 49 50 51 struct CCM_FLAGS_TAG { 52 union { 53 struct { 54 u8 L : 3; 55 u8 M : 3; 56 u8 aData :1; 57 u8 reserved :1; 58 } bf; 59 u8 val; 60 }; 61 }; 62 63 typedef struct CCM_FLAGS_TAG ccm_flags_t; 64 65 66 typedef struct { 67 union { 68 u8 A[AES_BLOCK_SIZE]; 69 u8 B[AES_BLOCK_SIZE]; 70 } bf; 71 72 u8 tmpResult[AES_BLOCK_SIZE]; 73 u8 newAstr[AES_BLOCK_SIZE]; 74 } aes_enc_t; 75 76 77 enum{ 78 CRYPT_NONCE_TYPE_ACL = 0, 79 CRYPT_NONCE_TYPE_CIS = 1, 80 CRYPT_NONCE_TYPE_BIS = 2, 81 }; 82 83 84 /** 85 * @brief this function is used to encrypt the plaintext 86 * @param[in] *key - aes key: 128 bit key for the encryption of the data, little--endian. 87 * @param[in] *plaintext - 128 bit data block that is requested to be encrypted, little--endian. 88 * @param[out] *result - 128 bit encrypted data block, little--endian. 89 * @return none. 90 * @Note Input data requires strict Word alignment 91 */ 92 void aes_ll_encryption(u8* key, u8* plaintext, u8 *encrypted_data); 93 94 95 /** 96 * @brief this function is used to initialize the aes_ccm initial value 97 * @param[in] ltk - encryption key, LTK 98 * @param[in] skdm - 99 * @param[in] skds - 100 * @param[in] ivm - 101 * @param[in] ivs - 102 * @param[in] pd - Reference structure ble_crypt_para_t 103 * @return none 104 */ 105 void aes_ll_ccm_encryption_init (u8 *ltk, u8 *skdm, u8 *skds, u8 *ivm, u8 *ivs, ble_crypt_para_t *pd); 106 107 108 /** 109 * @brief this function is used to encrypt the aes_ccm value 110 * @param[in] pkt - plaint_text 111 * @param[in] master - ll_ccm_enc: Master role must use 1, Slave role must use 0; 112 ll_ccm_dec: Master role must use 0, Slave role must use 1; 113 * @param[in] pd - Reference structure ble_crypt_para_t 114 * @return none 115 */ 116 //void aes_ll_ccm_encryption(u8 *pkt, int master, ble_crypt_para_t *pd); 117 118 119 /** 120 * @brief this function is used to encrypt the aes_ccm value, version2 121 * @param[in] pd - Reference structure leCryptCtrl_t 122 * @return none 123 */ 124 void aes_ll_ccm_encryption(llPhysChnPdu_t *pllPhysChnPdu, u8 role, u8 ll_type, ble_crypt_para_t *pd); 125 126 127 /** 128 * @brief this function is used to decrypt the aes_ccm value 129 * @param[in] pkt - plaint_text 130 * @param[in] master - ll_ccm_enc: Master role must use 1, Slave role must use 0; 131 ll_ccm_dec: Master role must use 0, Slave role must use 1; 132 * @param[in] pd - Reference structure ble_crypt_para_t 133 * @return 0: decryption succeeded; 1: decryption failed 134 */ 135 //int aes_ll_ccm_decryption(u8 *pkt, int master, ble_crypt_para_t *pd); 136 137 138 /** 139 * @brief this function is used to decrypt the aes_ccm value, version2 140 * @param[in] pd - Reference structure leCryptCtrl_t 141 * @return 0: decryption succeeded; 1: decryption failed 142 */ 143 int aes_ll_ccm_decryption(llPhysChnPdu_t *pllPhysChnPdu, u8 role, u8 ll_type, ble_crypt_para_t *pd); 144 145 146 147 148 149 150