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 enum { 25 AES_SUCC = SUCCESS, 26 AES_NO_BUF, 27 AES_FAIL, 28 }; 29 30 typedef struct { 31 u32 pkt; 32 u8 dir; 33 u8 iv[8]; 34 } ble_cyrpt_nonce_t; 35 36 typedef struct { 37 u64 enc_pno; 38 u64 dec_pno; 39 u8 sk[16]; // session key 40 ble_cyrpt_nonce_t nonce; 41 u8 st; 42 u8 enable; // 1: slave enable; 2: master enable 43 u8 mic_fail; 44 } ble_crypt_para_t; 45 46 struct CCM_FLAGS_TAG { 47 union { 48 struct { 49 u8 L : 3; 50 u8 M : 3; 51 u8 aData : 1; 52 u8 reserved : 1; 53 } bf; 54 u8 val; 55 }; 56 }; 57 58 typedef struct CCM_FLAGS_TAG ccm_flags_t; 59 60 typedef struct { 61 union { 62 u8 A[AES_BLOCK_SIZE]; 63 u8 B[AES_BLOCK_SIZE]; 64 } bf; 65 66 u8 tmpResult[AES_BLOCK_SIZE]; 67 u8 newAstr[AES_BLOCK_SIZE]; 68 } aes_enc_t; 69 70 enum { 71 CRYPT_NONCE_TYPE_ACL = 0, 72 CRYPT_NONCE_TYPE_CIS = 1, 73 CRYPT_NONCE_TYPE_BIS = 2, 74 }; 75 76 /** 77 * @brief this function is used to encrypt the plaintext 78 * @param[in] *key - aes key: 128 bit key for the encryption of the data, little--endian. 79 * @param[in] *plaintext - 128 bit data block that is requested to be encrypted, little--endian. 80 * @param[out] *result - 128 bit encrypted data block, little--endian. 81 * @return none. 82 * @Note Input data requires strict Word alignment 83 */ 84 void aes_ll_encryption(u8 *key, u8 *plaintext, u8 *encrypted_data); 85 86 /** 87 * @brief this function is used to initialize the aes_ccm initial value 88 * @param[in] ltk - encryption key, LTK 89 * @param[in] skdm - 90 * @param[in] skds - 91 * @param[in] ivm - 92 * @param[in] ivs - 93 * @param[in] pd - Reference structure ble_crypt_para_t 94 * @return none 95 */ 96 void aes_ll_ccm_encryption_init(u8 *ltk, u8 *skdm, u8 *skds, u8 *ivm, u8 *ivs, ble_crypt_para_t *pd); 97 98 /** 99 * @brief this function is used to encrypt the aes_ccm value, version2 100 * @param[in] pd - Reference structure leCryptCtrl_t 101 * @return none 102 */ 103 void aes_ll_ccm_encryption(llPhysChnPdu_t *pllPhysChnPdu, u8 role, u8 ll_type, ble_crypt_para_t *pd); 104 105 /** 106 * @brief this function is used to decrypt the aes_ccm value, version2 107 * @param[in] pd - Reference structure leCryptCtrl_t 108 * @return 0: decryption succeeded; 1: decryption failed 109 */ 110 int aes_ll_ccm_decryption(llPhysChnPdu_t *pllPhysChnPdu, u8 role, u8 ll_type, ble_crypt_para_t *pd); 111