Mistake on this page?
Report an issue in GitHub or email us
pal_crypto.h
Go to the documentation of this file.
1 /*************************************************************************************************/
2 /*!
3  * \file
4  *
5  * \brief Crypto driver definition.
6  *
7  * Copyright (c) 2018-2019 ARM Ltd. All Rights Reserved.
8  *
9  * Copyright (c) 2019-2020 Packetcraft, Inc.
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 /*************************************************************************************************/
24 
25 #ifndef PAL_CRYPTO_H
26 #define PAL_CRYPTO_H
27 
28 #include "pal_types.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /*! \addtogroup PAL_CRYPTO
35  * \{ */
36 
37 /**************************************************************************************************
38  Macros
39 **************************************************************************************************/
40 
41 /*! \brief AES block size. */
42 #define PAL_CRYPTO_AES_BLOCK_SIZE 16
43 
44 #define PAL_CRYPTO_LL_KEY_LEN 16 /*!< Encryption key length. */
45 #define PAL_CRYPTO_LL_IV_LEN 8 /*!< Initialization vector length. */
46 #define PAL_CRYPTO_LL_DATA_MIC_LEN 4 /*!< Data channel PDU MIC length. */
47 
48 /*! \brief CCM-Mode algorithm lengths. */
49 #define SEC_CCM_KEY_LEN 16
50 
51 /*! \brief CCM-Mode algorithm maximum additional length. */
52 #define SEC_CCM_MAX_ADDITIONAL_LEN ((1<<16) - (1<<8))
53 
54 /*! \brief CCM-Mode algorithm length. */
55 #define SEC_CCM_L 2
56 
57 /*! \brief CCM-Mode algorithm nonce length. */
58 #define SEC_CCM_NONCE_LEN (15-SEC_CCM_L)
59 
60 /**************************************************************************************************
61  Data Types
62 **************************************************************************************************/
63 
64 /*! \brief Operational states. */
65 typedef enum
66 {
67  PAL_CRYPTO_STATE_UNINIT = 0, /*!< Uninitialized state. */
68  PAL_CRYPTO_STATE_ERROR = 0, /*!< Error state. */
69  PAL_CRYPTO_STATE_READY /*!< Ready state. */
71 
72 /*! \brief Encryption data. */
73 typedef struct
74 {
75  /* SK placed here for 32-bit alignment. */
76  uint8_t sk[PAL_CRYPTO_LL_KEY_LEN]; /*!< Session/Encryption key. */
77  uint8_t iv[PAL_CRYPTO_LL_IV_LEN]; /*!< Initialization vector. */
78  bool_t enaEncrypt; /*!< Tx/Encryption enabled flag. */
79  bool_t enaDecrypt; /*!< Rx/Decryption enabled flag. */
80  bool_t enaAuth; /*!< Enable authentication. */
81  uint8_t nonceMode; /*!< Nonce mode. */
82  uint16_t *pEventCounter; /*!< Connection event counter. */
83  uint64_t *pTxPktCounter; /*!< Tx packet counter. Set when nonceMode = PAL_BB_NONCE_MODE_EXT64_CNTR. */
84  uint64_t *pRxPktCounter; /*!< Rx packet counter. Set when nonceMode = PAL_BB_NONCE_MODE_EXT64_CNTR. */
85  uint8_t dir; /*!< Direction value. */
86  uint8_t type; /*!< Type, ACL, CIS, BIS */
87  void *pEncryptCtx; /*!< Tx/Encryption context. */
88  void *pDecryptCtx; /*!< Rx/Decryption context. */
90 
91 /**************************************************************************************************
92  Function Declarations
93 **************************************************************************************************/
94 
95 /* Initialization */
96 void PalCryptoInit(void);
97 void PalCryptoDeInit(void);
98 
99 /* Key generation */
100 void PalCryptoGenerateP256KeyPair(const uint8_t *pPrivKey, uint8_t *pPubKey);
101 void PalCryptoGenerateDhKey(const uint8_t *pPubKey, const uint8_t *pPrivKey, uint8_t *pDhKey);
102 bool_t PalCryptoValidatePublicKey(const uint8_t *pPubKey, bool_t generateKey);
103 void PalCryptoGenerateRandomNumber(uint8_t *pBuf, uint8_t len);
104 
105 /* CCM */
106 uint32_t PalCryptoCcmDec(const uint8_t *pKey, uint8_t *pNonce, uint8_t *pCypherText, uint16_t textLen,
107  uint8_t *pClear, uint16_t clearLen, uint8_t *pMic, uint8_t micLen,
108  uint8_t *pResult, uint8_t handlerId, uint16_t param, uint8_t event);
109 void PalCryptoCcmEnc(const uint8_t *pKey, uint8_t *pNonce, uint8_t *pPlainText, uint16_t textLen,
110  uint8_t *pClear, uint16_t clearLen, uint8_t micLen, uint8_t *pResult,
111  uint8_t handlerId, uint16_t param, uint8_t event);
112 
113 /* Crypto AES */
114 void PalCryptoAesEcb(const uint8_t *pKey, uint8_t *pOut, const uint8_t *pIn);
115 void PalCryptoAesCmac(const uint8_t *pKey, uint8_t *pOut, const uint8_t *pIn, uint16_t len);
116 void PalCryptoAesEnable(PalCryptoEnc_t *pEnc, uint8_t id, uint8_t localDir);
117 bool_t PalCryptoAesCcmEncrypt(PalCryptoEnc_t *pEnc, uint8_t *pHdr, uint8_t *pBuf, uint8_t *pMic);
118 bool_t PalCryptoAesCcmDecrypt(PalCryptoEnc_t *pEnc, uint8_t *pBuf);
119 void PalCryptoSetEncryptPacketCount(PalCryptoEnc_t *pEnc, uint64_t pktCnt);
120 void PalCryptoSetDecryptPacketCount(PalCryptoEnc_t *pEnc, uint64_t pktCnt);
121 
122 /*! \} */ /* PAL_CRYPTO */
123 
124 #ifdef __cplusplus
125 };
126 #endif
127 
128 #endif /* PAL_CRYPTO_H */
uint8_t enaAuth
Definition: pal_crypto.h:80
uint64_t * pTxPktCounter
Definition: pal_crypto.h:83
Encryption data.
Definition: pal_crypto.h:73
uint8_t dir
Definition: pal_crypto.h:85
#define PAL_CRYPTO_LL_IV_LEN
Definition: pal_crypto.h:45
void * pEncryptCtx
Definition: pal_crypto.h:87
#define PAL_CRYPTO_LL_KEY_LEN
Definition: pal_crypto.h:44
Platform-independent data types.
uint16_t * pEventCounter
Definition: pal_crypto.h:82
uint8_t nonceMode
Definition: pal_crypto.h:81
PalCryptoState_t
Operational states.
Definition: pal_crypto.h:65
uint8_t type
Definition: pal_crypto.h:86
void * pDecryptCtx
Definition: pal_crypto.h:88
uint8_t enaDecrypt
Definition: pal_crypto.h:79
uint8_t enaEncrypt
Definition: pal_crypto.h:78
uint64_t * pRxPktCounter
Definition: pal_crypto.h:84
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.