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 #ifndef CRYPTO_ALG_H_ 19 #define CRYPTO_ALG_H_ 20 21 /* 22 * @brief This function is used to generate the prand 23 * @param[out] prand - The out are stored in little endian format 24 * @return none 25 * */ 26 void blt_crypto_alg_prand(unsigned char prand[3]); 27 28 /* 29 * @brief Resolvable Private Address Generation and Resolution 30 * @param[in] irk - The IRKs are stored in little endian format 31 * @param[in] r - The r are stored in little endian format 32 * @param[out] out - The out are stored in little endian format 33 * @return none 34 * */ 35 void blt_crypto_alg_ah(const unsigned char irk[16], unsigned char r[3], unsigned char out[3]); 36 37 /** 38 * @brief This function is used to generate the confirm values 39 * @param[out] c1: the confirm value, little--endian. 40 * @param[in] key: aes key, little--endian. 41 * @param[in] r: the plaintext, little--endian. 42 * @param[in] pres: packet buffer2, little--endian. 43 * @param[in] preq: packet buffer2, little--endian. 44 * @param[in] iat: initiate address type 45 * @param[in] ia: initiate address, little--endian. 46 * @param[in] rat: response address type 47 * @param[in] ra: response address, little--endian. 48 * @return none. 49 * @Note Input data requires strict Word alignment 50 */ 51 void blt_crypto_alg_c1(unsigned char *c1, unsigned char key[16], unsigned char r[16], unsigned char pres[7], 52 unsigned char preq[7], unsigned char iat, unsigned char ia[6], unsigned char rat, 53 unsigned char ra[6]); 54 55 /** 56 * @brief This function is used to generate the STK during the LE legacy pairing process. 57 * @param[out] *STK - the result of encrypt, little--endian. 58 * @param[in] *key - aes key, little--endian. 59 * @param[in] *r1 - the plaintext1, little--endian. 60 * @param[in] *r2 - the plaintext2, little--endian. 61 * @return none. 62 * @Note Input data requires strict Word alignment 63 */ 64 void blt_crypto_alg_s1(unsigned char *stk, unsigned char key[16], unsigned char r1[16], unsigned char r2[16]); 65 66 /** 67 * @brief This function is used to compute confirm value by function f4 68 * --- Ca: f4(U, V, X, Z) = AES-CMACX (U || V || Z) --- 69 * @param[out] r: the output of the confirm:128-bits, big--endian. 70 * @param[in] u: is the 256-bits, big--endian. 71 * @param[in] v: is the 256-bits, big--endian. 72 * @param[in] x: is the 128-bits, big--endian. 73 * @param[in] z: is the 8-bits 74 * @return none. 75 */ 76 void blt_crypto_alg_f4(unsigned char *r, unsigned char u[32], unsigned char v[32], unsigned char x[16], 77 unsigned char z); 78 79 /** 80 * @brief This function is used to generate the numeric comparison values during authentication 81 * stage 1 of the LE Secure Connections pairing process by function g2 82 * @param[in] u: is the 256-bits, big--endian. 83 * @param[in] v: is the 256-bits, big--endian. 84 * @param[in] x: is the 128-bits, big--endian. 85 * @param[in] y: is the 128-bits, big--endian. 86 * @return pincode value: 32-bits. 87 */ 88 unsigned int blt_crypto_alg_g2(unsigned char u[32], unsigned char v[32], unsigned char x[16], unsigned char y[16]); 89 90 /** 91 * @brief This function is used to generate derived keying material in order to create the LTK 92 * and keys for the commitment function f6 by function f5 93 * @param[out] mac: the output of the MAC value:128-bits, big--endian. 94 * @param[out] ltk: the output of the LTK value:128-bits, big--endian. 95 * @param[in] w: is the 256-bits, big--endian. 96 * @param[in] n1: is the 128-bits, big--endian. 97 * @param[in] n2: is the 128-bits, big--endian. 98 * @param[in] a1: is the 56-bits, big--endian. 99 * @param[in] a2: is the 56-bits, big--endian. 100 * @return none. 101 */ 102 void blt_crypto_alg_f5(unsigned char *mac, unsigned char *ltk, unsigned char w[32], unsigned char n1[16], 103 unsigned char n2[16], unsigned char a1[7], unsigned char a2[7]); 104 105 /** 106 * @brief This function is used to generate check values during authentication stage 2 of the 107 * LE Secure Connections pairing process by function f6 108 * @param[out] *e: the output of Ea or Eb:128-bits, big--endian. 109 * @param[in] w: is the 256-bits, big--endian. 110 * @param[in] n1: is the 128-bits, big--endian. 111 * @param[in] n2: is the 128-bits, big--endian. 112 * @param[in] a1: is the 56-bits, big--endian. 113 * @param[in] a2: is the 56-bits, big--endian. 114 * @return none. 115 */ 116 void blt_crypto_alg_f6(unsigned char *e, unsigned char w[16], unsigned char n1[16], unsigned char n2[16], 117 unsigned char r[16], unsigned char iocap[3], unsigned char a1[7], unsigned char a2[7]); 118 119 /** 120 * @brief This function is used to convert keys of a given size from one key type to another 121 * key type with equivalent strength 122 * @param[out] r: the output of h6:128-bits, big--endian. 123 * @param[in] w: is the 128-bits, big--endian. 124 * @param[in] keyid: is the 32-bits, big--endian. 125 * @return none. 126 */ 127 void blt_crypto_alg_h6(unsigned char *r, unsigned char w[16], unsigned char keyid[4]); 128 129 /** 130 * @brief This function is used to convert keys of a given size from one key type to another 131 * key type with equivalent strength 132 * --- h7(SALT, W) = AES-CMACsalt(W) --- 133 * @param[out] r: the output of h7:128-bits, big--endian. 134 * @param[in] salt: is the 128-bits, big--endian. 135 * @param[in] w: is the 128-bits, big--endian. 136 * @return none. 137 */ 138 void blt_crypto_alg_h7(unsigned char *r, unsigned char salt[16], unsigned char w[16]); 139 140 /** 141 * @brief This function is used to generate the Group Session Key (GSK) for encrypting or 142 * decrypting payloads of an encrypted BIS. 143 * --- h8(K, S, keyID) = AES-CMACik(keyID) --- 144 * @param[out] r: the output of h8:128-bits, big--endian. 145 * @param[in] k: is the 128-bits, big--endian. 146 * @param[in] s: is the 128-bits, big--endian. 147 * @param[in] keyid: is the 32-bits, big--endian. 148 * @return none. 149 */ 150 void blt_crypto_alg_h8(unsigned char *r, unsigned char k[16], unsigned char s[16], unsigned char keyId[4]); 151 152 #endif /* CRYPTO_ALG_H_ */ 153