1*cfb92d14SAndroid Build Coastguard Worker /* 2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2021, The OpenThread Authors. 3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved. 4*cfb92d14SAndroid Build Coastguard Worker * 5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright 8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright 10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the 11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution. 12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the 13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products 14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission. 15*cfb92d14SAndroid Build Coastguard Worker * 16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 27*cfb92d14SAndroid Build Coastguard Worker */ 28*cfb92d14SAndroid Build Coastguard Worker 29*cfb92d14SAndroid Build Coastguard Worker /** 30*cfb92d14SAndroid Build Coastguard Worker * @file 31*cfb92d14SAndroid Build Coastguard Worker * @brief 32*cfb92d14SAndroid Build Coastguard Worker * This file includes the platform abstraction for Crypto operations. 33*cfb92d14SAndroid Build Coastguard Worker */ 34*cfb92d14SAndroid Build Coastguard Worker 35*cfb92d14SAndroid Build Coastguard Worker #ifndef OPENTHREAD_PLATFORM_CRYPTO_H_ 36*cfb92d14SAndroid Build Coastguard Worker #define OPENTHREAD_PLATFORM_CRYPTO_H_ 37*cfb92d14SAndroid Build Coastguard Worker 38*cfb92d14SAndroid Build Coastguard Worker #include <stdint.h> 39*cfb92d14SAndroid Build Coastguard Worker #include <stdlib.h> 40*cfb92d14SAndroid Build Coastguard Worker 41*cfb92d14SAndroid Build Coastguard Worker #include <openthread/error.h> 42*cfb92d14SAndroid Build Coastguard Worker 43*cfb92d14SAndroid Build Coastguard Worker #ifdef __cplusplus 44*cfb92d14SAndroid Build Coastguard Worker extern "C" { 45*cfb92d14SAndroid Build Coastguard Worker #endif 46*cfb92d14SAndroid Build Coastguard Worker 47*cfb92d14SAndroid Build Coastguard Worker /** 48*cfb92d14SAndroid Build Coastguard Worker * @addtogroup plat-crypto 49*cfb92d14SAndroid Build Coastguard Worker * 50*cfb92d14SAndroid Build Coastguard Worker * @brief 51*cfb92d14SAndroid Build Coastguard Worker * This module includes the platform abstraction for Crypto. 52*cfb92d14SAndroid Build Coastguard Worker * 53*cfb92d14SAndroid Build Coastguard Worker * @{ 54*cfb92d14SAndroid Build Coastguard Worker * 55*cfb92d14SAndroid Build Coastguard Worker */ 56*cfb92d14SAndroid Build Coastguard Worker 57*cfb92d14SAndroid Build Coastguard Worker /** 58*cfb92d14SAndroid Build Coastguard Worker * Defines the key types. 59*cfb92d14SAndroid Build Coastguard Worker * 60*cfb92d14SAndroid Build Coastguard Worker */ 61*cfb92d14SAndroid Build Coastguard Worker typedef enum 62*cfb92d14SAndroid Build Coastguard Worker { 63*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_TYPE_RAW, ///< Key Type: Raw Data. 64*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_TYPE_AES, ///< Key Type: AES. 65*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_TYPE_HMAC, ///< Key Type: HMAC. 66*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_TYPE_ECDSA, ///< Key Type: ECDSA. 67*cfb92d14SAndroid Build Coastguard Worker } otCryptoKeyType; 68*cfb92d14SAndroid Build Coastguard Worker 69*cfb92d14SAndroid Build Coastguard Worker /** 70*cfb92d14SAndroid Build Coastguard Worker * Defines the key algorithms. 71*cfb92d14SAndroid Build Coastguard Worker * 72*cfb92d14SAndroid Build Coastguard Worker */ 73*cfb92d14SAndroid Build Coastguard Worker typedef enum 74*cfb92d14SAndroid Build Coastguard Worker { 75*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_ALG_VENDOR, ///< Key Algorithm: Vendor Defined. 76*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_ALG_AES_ECB, ///< Key Algorithm: AES ECB. 77*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_ALG_HMAC_SHA_256, ///< Key Algorithm: HMAC SHA-256. 78*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_ALG_ECDSA, ///< Key Algorithm: ECDSA. 79*cfb92d14SAndroid Build Coastguard Worker } otCryptoKeyAlgorithm; 80*cfb92d14SAndroid Build Coastguard Worker 81*cfb92d14SAndroid Build Coastguard Worker /** 82*cfb92d14SAndroid Build Coastguard Worker * Defines the key usage flags. 83*cfb92d14SAndroid Build Coastguard Worker * 84*cfb92d14SAndroid Build Coastguard Worker */ 85*cfb92d14SAndroid Build Coastguard Worker enum 86*cfb92d14SAndroid Build Coastguard Worker { 87*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_NONE = 0, ///< Key Usage: Key Usage is empty. 88*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_EXPORT = 1 << 0, ///< Key Usage: Key can be exported. 89*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_ENCRYPT = 1 << 1, ///< Key Usage: Encryption (vendor defined). 90*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_DECRYPT = 1 << 2, ///< Key Usage: AES ECB. 91*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_SIGN_HASH = 1 << 3, ///< Key Usage: Sign Hash. 92*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_USAGE_VERIFY_HASH = 1 << 4, ///< Key Usage: Verify Hash. 93*cfb92d14SAndroid Build Coastguard Worker }; 94*cfb92d14SAndroid Build Coastguard Worker 95*cfb92d14SAndroid Build Coastguard Worker /** 96*cfb92d14SAndroid Build Coastguard Worker * Defines the key storage types. 97*cfb92d14SAndroid Build Coastguard Worker * 98*cfb92d14SAndroid Build Coastguard Worker */ 99*cfb92d14SAndroid Build Coastguard Worker typedef enum 100*cfb92d14SAndroid Build Coastguard Worker { 101*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_STORAGE_VOLATILE, ///< Key Persistence: Key is volatile. 102*cfb92d14SAndroid Build Coastguard Worker OT_CRYPTO_KEY_STORAGE_PERSISTENT, ///< Key Persistence: Key is persistent. 103*cfb92d14SAndroid Build Coastguard Worker } otCryptoKeyStorage; 104*cfb92d14SAndroid Build Coastguard Worker 105*cfb92d14SAndroid Build Coastguard Worker /** 106*cfb92d14SAndroid Build Coastguard Worker * This datatype represents the key reference. 107*cfb92d14SAndroid Build Coastguard Worker * 108*cfb92d14SAndroid Build Coastguard Worker */ 109*cfb92d14SAndroid Build Coastguard Worker typedef uint32_t otCryptoKeyRef; 110*cfb92d14SAndroid Build Coastguard Worker 111*cfb92d14SAndroid Build Coastguard Worker /** 112*cfb92d14SAndroid Build Coastguard Worker * @struct otCryptoKey 113*cfb92d14SAndroid Build Coastguard Worker * 114*cfb92d14SAndroid Build Coastguard Worker * Represents the Key Material required for Crypto operations. 115*cfb92d14SAndroid Build Coastguard Worker * 116*cfb92d14SAndroid Build Coastguard Worker */ 117*cfb92d14SAndroid Build Coastguard Worker typedef struct otCryptoKey 118*cfb92d14SAndroid Build Coastguard Worker { 119*cfb92d14SAndroid Build Coastguard Worker const uint8_t *mKey; ///< Pointer to the buffer containing key. NULL indicates to use `mKeyRef`. 120*cfb92d14SAndroid Build Coastguard Worker uint16_t mKeyLength; ///< The key length in bytes (applicable when `mKey` is not NULL). 121*cfb92d14SAndroid Build Coastguard Worker uint32_t mKeyRef; ///< The PSA key ref (requires `mKey` to be NULL). 122*cfb92d14SAndroid Build Coastguard Worker } otCryptoKey; 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Worker /** 125*cfb92d14SAndroid Build Coastguard Worker * @struct otCryptoContext 126*cfb92d14SAndroid Build Coastguard Worker * 127*cfb92d14SAndroid Build Coastguard Worker * Stores the context object for platform APIs. 128*cfb92d14SAndroid Build Coastguard Worker * 129*cfb92d14SAndroid Build Coastguard Worker */ 130*cfb92d14SAndroid Build Coastguard Worker typedef struct otCryptoContext 131*cfb92d14SAndroid Build Coastguard Worker { 132*cfb92d14SAndroid Build Coastguard Worker void *mContext; ///< Pointer to the context. 133*cfb92d14SAndroid Build Coastguard Worker uint16_t mContextSize; ///< The length of the context in bytes. 134*cfb92d14SAndroid Build Coastguard Worker } otCryptoContext; 135*cfb92d14SAndroid Build Coastguard Worker 136*cfb92d14SAndroid Build Coastguard Worker /** 137*cfb92d14SAndroid Build Coastguard Worker * Length of SHA256 hash (in bytes). 138*cfb92d14SAndroid Build Coastguard Worker * 139*cfb92d14SAndroid Build Coastguard Worker */ 140*cfb92d14SAndroid Build Coastguard Worker #define OT_CRYPTO_SHA256_HASH_SIZE 32 141*cfb92d14SAndroid Build Coastguard Worker 142*cfb92d14SAndroid Build Coastguard Worker /** 143*cfb92d14SAndroid Build Coastguard Worker * @struct otPlatCryptoSha256Hash 144*cfb92d14SAndroid Build Coastguard Worker * 145*cfb92d14SAndroid Build Coastguard Worker * Represents a SHA-256 hash. 146*cfb92d14SAndroid Build Coastguard Worker * 147*cfb92d14SAndroid Build Coastguard Worker */ 148*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_PACKED_BEGIN 149*cfb92d14SAndroid Build Coastguard Worker struct otPlatCryptoSha256Hash 150*cfb92d14SAndroid Build Coastguard Worker { 151*cfb92d14SAndroid Build Coastguard Worker uint8_t m8[OT_CRYPTO_SHA256_HASH_SIZE]; ///< Hash bytes. 152*cfb92d14SAndroid Build Coastguard Worker } OT_TOOL_PACKED_END; 153*cfb92d14SAndroid Build Coastguard Worker 154*cfb92d14SAndroid Build Coastguard Worker /** 155*cfb92d14SAndroid Build Coastguard Worker * Represents a SHA-256 hash. 156*cfb92d14SAndroid Build Coastguard Worker * 157*cfb92d14SAndroid Build Coastguard Worker */ 158*cfb92d14SAndroid Build Coastguard Worker typedef struct otPlatCryptoSha256Hash otPlatCryptoSha256Hash; 159*cfb92d14SAndroid Build Coastguard Worker 160*cfb92d14SAndroid Build Coastguard Worker /** 161*cfb92d14SAndroid Build Coastguard Worker * Max buffer size (in bytes) for representing the EDCSA key-pair in DER format. 162*cfb92d14SAndroid Build Coastguard Worker * 163*cfb92d14SAndroid Build Coastguard Worker */ 164*cfb92d14SAndroid Build Coastguard Worker #define OT_CRYPTO_ECDSA_MAX_DER_SIZE 125 165*cfb92d14SAndroid Build Coastguard Worker 166*cfb92d14SAndroid Build Coastguard Worker /** 167*cfb92d14SAndroid Build Coastguard Worker * @struct otPlatCryptoEcdsaKeyPair 168*cfb92d14SAndroid Build Coastguard Worker * 169*cfb92d14SAndroid Build Coastguard Worker * Represents an ECDSA key pair (public and private keys). 170*cfb92d14SAndroid Build Coastguard Worker * 171*cfb92d14SAndroid Build Coastguard Worker * The key pair is stored using Distinguished Encoding Rules (DER) format (per RFC 5915). 172*cfb92d14SAndroid Build Coastguard Worker * 173*cfb92d14SAndroid Build Coastguard Worker */ 174*cfb92d14SAndroid Build Coastguard Worker typedef struct otPlatCryptoEcdsaKeyPair 175*cfb92d14SAndroid Build Coastguard Worker { 176*cfb92d14SAndroid Build Coastguard Worker uint8_t mDerBytes[OT_CRYPTO_ECDSA_MAX_DER_SIZE]; 177*cfb92d14SAndroid Build Coastguard Worker uint8_t mDerLength; 178*cfb92d14SAndroid Build Coastguard Worker } otPlatCryptoEcdsaKeyPair; 179*cfb92d14SAndroid Build Coastguard Worker 180*cfb92d14SAndroid Build Coastguard Worker /** 181*cfb92d14SAndroid Build Coastguard Worker * Buffer size (in bytes) for representing the EDCSA public key. 182*cfb92d14SAndroid Build Coastguard Worker * 183*cfb92d14SAndroid Build Coastguard Worker */ 184*cfb92d14SAndroid Build Coastguard Worker #define OT_CRYPTO_ECDSA_PUBLIC_KEY_SIZE 64 185*cfb92d14SAndroid Build Coastguard Worker 186*cfb92d14SAndroid Build Coastguard Worker /** 187*cfb92d14SAndroid Build Coastguard Worker * @struct otPlatCryptoEcdsaPublicKey 188*cfb92d14SAndroid Build Coastguard Worker * 189*cfb92d14SAndroid Build Coastguard Worker * Represents a ECDSA public key. 190*cfb92d14SAndroid Build Coastguard Worker * 191*cfb92d14SAndroid Build Coastguard Worker * The public key is stored as a byte sequence representation of an uncompressed curve point (RFC 6605 - sec 4). 192*cfb92d14SAndroid Build Coastguard Worker * 193*cfb92d14SAndroid Build Coastguard Worker */ 194*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_PACKED_BEGIN 195*cfb92d14SAndroid Build Coastguard Worker struct otPlatCryptoEcdsaPublicKey 196*cfb92d14SAndroid Build Coastguard Worker { 197*cfb92d14SAndroid Build Coastguard Worker uint8_t m8[OT_CRYPTO_ECDSA_PUBLIC_KEY_SIZE]; 198*cfb92d14SAndroid Build Coastguard Worker } OT_TOOL_PACKED_END; 199*cfb92d14SAndroid Build Coastguard Worker 200*cfb92d14SAndroid Build Coastguard Worker typedef struct otPlatCryptoEcdsaPublicKey otPlatCryptoEcdsaPublicKey; 201*cfb92d14SAndroid Build Coastguard Worker 202*cfb92d14SAndroid Build Coastguard Worker /** 203*cfb92d14SAndroid Build Coastguard Worker * Buffer size (in bytes) for representing the EDCSA signature. 204*cfb92d14SAndroid Build Coastguard Worker * 205*cfb92d14SAndroid Build Coastguard Worker */ 206*cfb92d14SAndroid Build Coastguard Worker #define OT_CRYPTO_ECDSA_SIGNATURE_SIZE 64 207*cfb92d14SAndroid Build Coastguard Worker 208*cfb92d14SAndroid Build Coastguard Worker /** 209*cfb92d14SAndroid Build Coastguard Worker * @struct otPlatCryptoEcdsaSignature 210*cfb92d14SAndroid Build Coastguard Worker * 211*cfb92d14SAndroid Build Coastguard Worker * Represents an ECDSA signature. 212*cfb92d14SAndroid Build Coastguard Worker * 213*cfb92d14SAndroid Build Coastguard Worker * The signature is encoded as the concatenated binary representation of two MPIs `r` and `s` which are calculated 214*cfb92d14SAndroid Build Coastguard Worker * during signing (RFC 6605 - section 4). 215*cfb92d14SAndroid Build Coastguard Worker * 216*cfb92d14SAndroid Build Coastguard Worker */ 217*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_PACKED_BEGIN 218*cfb92d14SAndroid Build Coastguard Worker struct otPlatCryptoEcdsaSignature 219*cfb92d14SAndroid Build Coastguard Worker { 220*cfb92d14SAndroid Build Coastguard Worker uint8_t m8[OT_CRYPTO_ECDSA_SIGNATURE_SIZE]; 221*cfb92d14SAndroid Build Coastguard Worker } OT_TOOL_PACKED_END; 222*cfb92d14SAndroid Build Coastguard Worker 223*cfb92d14SAndroid Build Coastguard Worker typedef struct otPlatCryptoEcdsaSignature otPlatCryptoEcdsaSignature; 224*cfb92d14SAndroid Build Coastguard Worker 225*cfb92d14SAndroid Build Coastguard Worker /** 226*cfb92d14SAndroid Build Coastguard Worker * Max PBKDF2 SALT length: salt prefix (6) + extended panid (8) + network name (16) 227*cfb92d14SAndroid Build Coastguard Worker * 228*cfb92d14SAndroid Build Coastguard Worker */ 229*cfb92d14SAndroid Build Coastguard Worker #define OT_CRYPTO_PBDKF2_MAX_SALT_SIZE 30 230*cfb92d14SAndroid Build Coastguard Worker 231*cfb92d14SAndroid Build Coastguard Worker /** 232*cfb92d14SAndroid Build Coastguard Worker * Initialize the Crypto module. 233*cfb92d14SAndroid Build Coastguard Worker * 234*cfb92d14SAndroid Build Coastguard Worker */ 235*cfb92d14SAndroid Build Coastguard Worker void otPlatCryptoInit(void); 236*cfb92d14SAndroid Build Coastguard Worker 237*cfb92d14SAndroid Build Coastguard Worker /** 238*cfb92d14SAndroid Build Coastguard Worker * Import a key into PSA ITS. 239*cfb92d14SAndroid Build Coastguard Worker * 240*cfb92d14SAndroid Build Coastguard Worker * @param[in,out] aKeyRef Pointer to the key ref to be used for crypto operations. 241*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyType Key Type encoding for the key. 242*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyAlgorithm Key algorithm encoding for the key. 243*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyUsage Key Usage encoding for the key (combinations of `OT_CRYPTO_KEY_USAGE_*`). 244*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyPersistence Key Persistence for this key 245*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKey Actual key to be imported. 246*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyLen Length of the key to be imported. 247*cfb92d14SAndroid Build Coastguard Worker * 248*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully imported the key. 249*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to import the key. 250*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aKey was set to NULL. 251*cfb92d14SAndroid Build Coastguard Worker * 252*cfb92d14SAndroid Build Coastguard Worker * @note If OT_CRYPTO_KEY_STORAGE_PERSISTENT is passed for aKeyPersistence then @p aKeyRef is input and platform 253*cfb92d14SAndroid Build Coastguard Worker * should use the given aKeyRef and MUST not change it. 254*cfb92d14SAndroid Build Coastguard Worker * 255*cfb92d14SAndroid Build Coastguard Worker * If OT_CRYPTO_KEY_STORAGE_VOLATILE is passed for aKeyPersistence then @p aKeyRef is output, the initial 256*cfb92d14SAndroid Build Coastguard Worker * value does not matter and platform API MUST update it to return the new key ref. 257*cfb92d14SAndroid Build Coastguard Worker * 258*cfb92d14SAndroid Build Coastguard Worker * This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 259*cfb92d14SAndroid Build Coastguard Worker * 260*cfb92d14SAndroid Build Coastguard Worker */ 261*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoImportKey(otCryptoKeyRef *aKeyRef, 262*cfb92d14SAndroid Build Coastguard Worker otCryptoKeyType aKeyType, 263*cfb92d14SAndroid Build Coastguard Worker otCryptoKeyAlgorithm aKeyAlgorithm, 264*cfb92d14SAndroid Build Coastguard Worker int aKeyUsage, 265*cfb92d14SAndroid Build Coastguard Worker otCryptoKeyStorage aKeyPersistence, 266*cfb92d14SAndroid Build Coastguard Worker const uint8_t *aKey, 267*cfb92d14SAndroid Build Coastguard Worker size_t aKeyLen); 268*cfb92d14SAndroid Build Coastguard Worker 269*cfb92d14SAndroid Build Coastguard Worker /** 270*cfb92d14SAndroid Build Coastguard Worker * Export a key stored in PSA ITS. 271*cfb92d14SAndroid Build Coastguard Worker * 272*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef The key ref to be used for crypto operations. 273*cfb92d14SAndroid Build Coastguard Worker * @param[out] aBuffer Pointer to the buffer where key needs to be exported. 274*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBufferLen Length of the buffer passed to store the exported key. 275*cfb92d14SAndroid Build Coastguard Worker * @param[out] aKeyLen Pointer to return the length of the exported key. 276*cfb92d14SAndroid Build Coastguard Worker * 277*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully exported @p aKeyRef. 278*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to export @p aKeyRef. 279*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aBuffer was NULL 280*cfb92d14SAndroid Build Coastguard Worker * 281*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 282*cfb92d14SAndroid Build Coastguard Worker * 283*cfb92d14SAndroid Build Coastguard Worker */ 284*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoExportKey(otCryptoKeyRef aKeyRef, uint8_t *aBuffer, size_t aBufferLen, size_t *aKeyLen); 285*cfb92d14SAndroid Build Coastguard Worker 286*cfb92d14SAndroid Build Coastguard Worker /** 287*cfb92d14SAndroid Build Coastguard Worker * Destroy a key stored in PSA ITS. 288*cfb92d14SAndroid Build Coastguard Worker * 289*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef The key ref to be destroyed 290*cfb92d14SAndroid Build Coastguard Worker * 291*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully destroyed the key. 292*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to destroy the key. 293*cfb92d14SAndroid Build Coastguard Worker * 294*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 295*cfb92d14SAndroid Build Coastguard Worker * 296*cfb92d14SAndroid Build Coastguard Worker */ 297*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoDestroyKey(otCryptoKeyRef aKeyRef); 298*cfb92d14SAndroid Build Coastguard Worker 299*cfb92d14SAndroid Build Coastguard Worker /** 300*cfb92d14SAndroid Build Coastguard Worker * Check if the key ref passed has an associated key in PSA ITS. 301*cfb92d14SAndroid Build Coastguard Worker * 302*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef The Key Ref to check. 303*cfb92d14SAndroid Build Coastguard Worker * 304*cfb92d14SAndroid Build Coastguard Worker * @retval TRUE There is an associated key with @p aKeyRef. 305*cfb92d14SAndroid Build Coastguard Worker * @retval FALSE There is no associated key with @p aKeyRef. 306*cfb92d14SAndroid Build Coastguard Worker * 307*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 308*cfb92d14SAndroid Build Coastguard Worker * 309*cfb92d14SAndroid Build Coastguard Worker */ 310*cfb92d14SAndroid Build Coastguard Worker bool otPlatCryptoHasKey(otCryptoKeyRef aKeyRef); 311*cfb92d14SAndroid Build Coastguard Worker 312*cfb92d14SAndroid Build Coastguard Worker /** 313*cfb92d14SAndroid Build Coastguard Worker * Initialize the HMAC operation. 314*cfb92d14SAndroid Build Coastguard Worker * 315*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HMAC operation. 316*cfb92d14SAndroid Build Coastguard Worker * 317*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully initialized HMAC operation. 318*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to initialize HMAC operation. 319*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 320*cfb92d14SAndroid Build Coastguard Worker * 321*cfb92d14SAndroid Build Coastguard Worker * @note The platform driver shall point the context to the correct object such as psa_mac_operation_t or 322*cfb92d14SAndroid Build Coastguard Worker * mbedtls_md_context_t. 323*cfb92d14SAndroid Build Coastguard Worker * 324*cfb92d14SAndroid Build Coastguard Worker */ 325*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHmacSha256Init(otCryptoContext *aContext); 326*cfb92d14SAndroid Build Coastguard Worker 327*cfb92d14SAndroid Build Coastguard Worker /** 328*cfb92d14SAndroid Build Coastguard Worker * Uninitialize the HMAC operation. 329*cfb92d14SAndroid Build Coastguard Worker * 330*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HMAC operation. 331*cfb92d14SAndroid Build Coastguard Worker * 332*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully uninitialized HMAC operation. 333*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to uninitialized HMAC operation. 334*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 335*cfb92d14SAndroid Build Coastguard Worker * 336*cfb92d14SAndroid Build Coastguard Worker */ 337*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHmacSha256Deinit(otCryptoContext *aContext); 338*cfb92d14SAndroid Build Coastguard Worker 339*cfb92d14SAndroid Build Coastguard Worker /** 340*cfb92d14SAndroid Build Coastguard Worker * Start HMAC operation. 341*cfb92d14SAndroid Build Coastguard Worker * 342*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HMAC operation. 343*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKey Key material to be used for HMAC operation. 344*cfb92d14SAndroid Build Coastguard Worker * 345*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully started HMAC operation. 346*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to start HMAC operation. 347*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aKey was NULL 348*cfb92d14SAndroid Build Coastguard Worker * 349*cfb92d14SAndroid Build Coastguard Worker */ 350*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHmacSha256Start(otCryptoContext *aContext, const otCryptoKey *aKey); 351*cfb92d14SAndroid Build Coastguard Worker 352*cfb92d14SAndroid Build Coastguard Worker /** 353*cfb92d14SAndroid Build Coastguard Worker * Update the HMAC operation with new input. 354*cfb92d14SAndroid Build Coastguard Worker * 355*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HMAC operation. 356*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBuf A pointer to the input buffer. 357*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBufLength The length of @p aBuf in bytes. 358*cfb92d14SAndroid Build Coastguard Worker * 359*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully updated HMAC with new input operation. 360*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to update HMAC operation. 361*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aBuf was NULL 362*cfb92d14SAndroid Build Coastguard Worker * 363*cfb92d14SAndroid Build Coastguard Worker */ 364*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHmacSha256Update(otCryptoContext *aContext, const void *aBuf, uint16_t aBufLength); 365*cfb92d14SAndroid Build Coastguard Worker 366*cfb92d14SAndroid Build Coastguard Worker /** 367*cfb92d14SAndroid Build Coastguard Worker * Complete the HMAC operation. 368*cfb92d14SAndroid Build Coastguard Worker * 369*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HMAC operation. 370*cfb92d14SAndroid Build Coastguard Worker * @param[out] aBuf A pointer to the output buffer. 371*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBufLength The length of @p aBuf in bytes. 372*cfb92d14SAndroid Build Coastguard Worker * 373*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully completed HMAC operation. 374*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to complete HMAC operation. 375*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aBuf was NULL 376*cfb92d14SAndroid Build Coastguard Worker * 377*cfb92d14SAndroid Build Coastguard Worker */ 378*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHmacSha256Finish(otCryptoContext *aContext, uint8_t *aBuf, size_t aBufLength); 379*cfb92d14SAndroid Build Coastguard Worker 380*cfb92d14SAndroid Build Coastguard Worker /** 381*cfb92d14SAndroid Build Coastguard Worker * Initialise the AES operation. 382*cfb92d14SAndroid Build Coastguard Worker * 383*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for AES operation. 384*cfb92d14SAndroid Build Coastguard Worker * 385*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully Initialised AES operation. 386*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to Initialise AES operation. 387*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 388*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Cannot allocate the context. 389*cfb92d14SAndroid Build Coastguard Worker * 390*cfb92d14SAndroid Build Coastguard Worker * @note The platform driver shall point the context to the correct object such as psa_key_id 391*cfb92d14SAndroid Build Coastguard Worker * or mbedtls_aes_context_t. 392*cfb92d14SAndroid Build Coastguard Worker * 393*cfb92d14SAndroid Build Coastguard Worker */ 394*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoAesInit(otCryptoContext *aContext); 395*cfb92d14SAndroid Build Coastguard Worker 396*cfb92d14SAndroid Build Coastguard Worker /** 397*cfb92d14SAndroid Build Coastguard Worker * Set the key for AES operation. 398*cfb92d14SAndroid Build Coastguard Worker * 399*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for AES operation. 400*cfb92d14SAndroid Build Coastguard Worker * @param[out] aKey Key to use for AES operation. 401*cfb92d14SAndroid Build Coastguard Worker * 402*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully set the key for AES operation. 403*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to set the key for AES operation. 404*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aKey was NULL 405*cfb92d14SAndroid Build Coastguard Worker * 406*cfb92d14SAndroid Build Coastguard Worker */ 407*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoAesSetKey(otCryptoContext *aContext, const otCryptoKey *aKey); 408*cfb92d14SAndroid Build Coastguard Worker 409*cfb92d14SAndroid Build Coastguard Worker /** 410*cfb92d14SAndroid Build Coastguard Worker * Encrypt the given data. 411*cfb92d14SAndroid Build Coastguard Worker * 412*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for AES operation. 413*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInput Pointer to the input buffer. 414*cfb92d14SAndroid Build Coastguard Worker * @param[in] aOutput Pointer to the output buffer. 415*cfb92d14SAndroid Build Coastguard Worker * 416*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully encrypted @p aInput. 417*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to encrypt @p aInput. 418*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aKey or @p aOutput were NULL 419*cfb92d14SAndroid Build Coastguard Worker * 420*cfb92d14SAndroid Build Coastguard Worker */ 421*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoAesEncrypt(otCryptoContext *aContext, const uint8_t *aInput, uint8_t *aOutput); 422*cfb92d14SAndroid Build Coastguard Worker 423*cfb92d14SAndroid Build Coastguard Worker /** 424*cfb92d14SAndroid Build Coastguard Worker * Free the AES context. 425*cfb92d14SAndroid Build Coastguard Worker * 426*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for AES operation. 427*cfb92d14SAndroid Build Coastguard Worker * 428*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully freed AES context. 429*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to free AES context. 430*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 431*cfb92d14SAndroid Build Coastguard Worker * 432*cfb92d14SAndroid Build Coastguard Worker */ 433*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoAesFree(otCryptoContext *aContext); 434*cfb92d14SAndroid Build Coastguard Worker 435*cfb92d14SAndroid Build Coastguard Worker /** 436*cfb92d14SAndroid Build Coastguard Worker * Initialise the HKDF context. 437*cfb92d14SAndroid Build Coastguard Worker * 438*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HKDF operation. 439*cfb92d14SAndroid Build Coastguard Worker * 440*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully Initialised AES operation. 441*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to Initialise AES operation. 442*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 443*cfb92d14SAndroid Build Coastguard Worker * 444*cfb92d14SAndroid Build Coastguard Worker * @note The platform driver shall point the context to the correct object such as psa_key_derivation_operation_t 445*cfb92d14SAndroid Build Coastguard Worker * or HmacSha256::Hash 446*cfb92d14SAndroid Build Coastguard Worker * 447*cfb92d14SAndroid Build Coastguard Worker */ 448*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHkdfInit(otCryptoContext *aContext); 449*cfb92d14SAndroid Build Coastguard Worker 450*cfb92d14SAndroid Build Coastguard Worker /** 451*cfb92d14SAndroid Build Coastguard Worker * Perform HKDF Expand step. 452*cfb92d14SAndroid Build Coastguard Worker * 453*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Operation context for HKDF operation. 454*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInfo Pointer to the Info sequence. 455*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInfoLength Length of the Info sequence. 456*cfb92d14SAndroid Build Coastguard Worker * @param[out] aOutputKey Pointer to the output Key. 457*cfb92d14SAndroid Build Coastguard Worker * @param[in] aOutputKeyLength Size of the output key buffer. 458*cfb92d14SAndroid Build Coastguard Worker * 459*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE HKDF Expand was successful. 460*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED HKDF Expand failed. 461*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 462*cfb92d14SAndroid Build Coastguard Worker * 463*cfb92d14SAndroid Build Coastguard Worker */ 464*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHkdfExpand(otCryptoContext *aContext, 465*cfb92d14SAndroid Build Coastguard Worker const uint8_t *aInfo, 466*cfb92d14SAndroid Build Coastguard Worker uint16_t aInfoLength, 467*cfb92d14SAndroid Build Coastguard Worker uint8_t *aOutputKey, 468*cfb92d14SAndroid Build Coastguard Worker uint16_t aOutputKeyLength); 469*cfb92d14SAndroid Build Coastguard Worker 470*cfb92d14SAndroid Build Coastguard Worker /** 471*cfb92d14SAndroid Build Coastguard Worker * Perform HKDF Extract step. 472*cfb92d14SAndroid Build Coastguard Worker * 473*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Operation context for HKDF operation. 474*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSalt Pointer to the Salt for HKDF. 475*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSaltLength Length of Salt. 476*cfb92d14SAndroid Build Coastguard Worker * @param[in] aInputKey Pointer to the input key. 477*cfb92d14SAndroid Build Coastguard Worker * 478*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE HKDF Extract was successful. 479*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED HKDF Extract failed. 480*cfb92d14SAndroid Build Coastguard Worker * 481*cfb92d14SAndroid Build Coastguard Worker */ 482*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHkdfExtract(otCryptoContext *aContext, 483*cfb92d14SAndroid Build Coastguard Worker const uint8_t *aSalt, 484*cfb92d14SAndroid Build Coastguard Worker uint16_t aSaltLength, 485*cfb92d14SAndroid Build Coastguard Worker const otCryptoKey *aInputKey); 486*cfb92d14SAndroid Build Coastguard Worker 487*cfb92d14SAndroid Build Coastguard Worker /** 488*cfb92d14SAndroid Build Coastguard Worker * Uninitialize the HKDF context. 489*cfb92d14SAndroid Build Coastguard Worker * 490*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for HKDF operation. 491*cfb92d14SAndroid Build Coastguard Worker * 492*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully un-initialised HKDF operation. 493*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to un-initialised HKDF operation. 494*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 495*cfb92d14SAndroid Build Coastguard Worker * 496*cfb92d14SAndroid Build Coastguard Worker */ 497*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoHkdfDeinit(otCryptoContext *aContext); 498*cfb92d14SAndroid Build Coastguard Worker 499*cfb92d14SAndroid Build Coastguard Worker /** 500*cfb92d14SAndroid Build Coastguard Worker * Initialise the SHA-256 operation. 501*cfb92d14SAndroid Build Coastguard Worker * 502*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for SHA-256 operation. 503*cfb92d14SAndroid Build Coastguard Worker * 504*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully initialised SHA-256 operation. 505*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to initialise SHA-256 operation. 506*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 507*cfb92d14SAndroid Build Coastguard Worker * 508*cfb92d14SAndroid Build Coastguard Worker * 509*cfb92d14SAndroid Build Coastguard Worker * @note The platform driver shall point the context to the correct object such as psa_hash_operation_t 510*cfb92d14SAndroid Build Coastguard Worker * or mbedtls_sha256_context. 511*cfb92d14SAndroid Build Coastguard Worker */ 512*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoSha256Init(otCryptoContext *aContext); 513*cfb92d14SAndroid Build Coastguard Worker 514*cfb92d14SAndroid Build Coastguard Worker /** 515*cfb92d14SAndroid Build Coastguard Worker * Uninitialize the SHA-256 operation. 516*cfb92d14SAndroid Build Coastguard Worker * 517*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for SHA-256 operation. 518*cfb92d14SAndroid Build Coastguard Worker * 519*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully un-initialised SHA-256 operation. 520*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to un-initialised SHA-256 operation. 521*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 522*cfb92d14SAndroid Build Coastguard Worker * 523*cfb92d14SAndroid Build Coastguard Worker */ 524*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoSha256Deinit(otCryptoContext *aContext); 525*cfb92d14SAndroid Build Coastguard Worker 526*cfb92d14SAndroid Build Coastguard Worker /** 527*cfb92d14SAndroid Build Coastguard Worker * Start SHA-256 operation. 528*cfb92d14SAndroid Build Coastguard Worker * 529*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for SHA-256 operation. 530*cfb92d14SAndroid Build Coastguard Worker * 531*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully started SHA-256 operation. 532*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to start SHA-256 operation. 533*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext was NULL 534*cfb92d14SAndroid Build Coastguard Worker * 535*cfb92d14SAndroid Build Coastguard Worker */ 536*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoSha256Start(otCryptoContext *aContext); 537*cfb92d14SAndroid Build Coastguard Worker 538*cfb92d14SAndroid Build Coastguard Worker /** 539*cfb92d14SAndroid Build Coastguard Worker * Update SHA-256 operation with new input. 540*cfb92d14SAndroid Build Coastguard Worker * 541*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for SHA-256 operation. 542*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBuf A pointer to the input buffer. 543*cfb92d14SAndroid Build Coastguard Worker * @param[in] aBufLength The length of @p aBuf in bytes. 544*cfb92d14SAndroid Build Coastguard Worker * 545*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully updated SHA-256 with new input operation. 546*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to update SHA-256 operation. 547*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aBuf was NULL 548*cfb92d14SAndroid Build Coastguard Worker * 549*cfb92d14SAndroid Build Coastguard Worker */ 550*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoSha256Update(otCryptoContext *aContext, const void *aBuf, uint16_t aBufLength); 551*cfb92d14SAndroid Build Coastguard Worker 552*cfb92d14SAndroid Build Coastguard Worker /** 553*cfb92d14SAndroid Build Coastguard Worker * Finish SHA-256 operation. 554*cfb92d14SAndroid Build Coastguard Worker * 555*cfb92d14SAndroid Build Coastguard Worker * @param[in] aContext Context for SHA-256 operation. 556*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHash A pointer to the output buffer, where hash needs to be stored. 557*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHashSize The length of @p aHash in bytes. 558*cfb92d14SAndroid Build Coastguard Worker * 559*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully completed the SHA-256 operation. 560*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to complete SHA-256 operation. 561*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS @p aContext or @p aHash was NULL 562*cfb92d14SAndroid Build Coastguard Worker * 563*cfb92d14SAndroid Build Coastguard Worker */ 564*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoSha256Finish(otCryptoContext *aContext, uint8_t *aHash, uint16_t aHashSize); 565*cfb92d14SAndroid Build Coastguard Worker 566*cfb92d14SAndroid Build Coastguard Worker /** 567*cfb92d14SAndroid Build Coastguard Worker * Initialize cryptographically-secure pseudorandom number generator (CSPRNG). 568*cfb92d14SAndroid Build Coastguard Worker * 569*cfb92d14SAndroid Build Coastguard Worker */ 570*cfb92d14SAndroid Build Coastguard Worker void otPlatCryptoRandomInit(void); 571*cfb92d14SAndroid Build Coastguard Worker 572*cfb92d14SAndroid Build Coastguard Worker /** 573*cfb92d14SAndroid Build Coastguard Worker * Deinitialize cryptographically-secure pseudorandom number generator (CSPRNG). 574*cfb92d14SAndroid Build Coastguard Worker * 575*cfb92d14SAndroid Build Coastguard Worker */ 576*cfb92d14SAndroid Build Coastguard Worker void otPlatCryptoRandomDeinit(void); 577*cfb92d14SAndroid Build Coastguard Worker 578*cfb92d14SAndroid Build Coastguard Worker /** 579*cfb92d14SAndroid Build Coastguard Worker * Fills a given buffer with cryptographically secure random bytes. 580*cfb92d14SAndroid Build Coastguard Worker * 581*cfb92d14SAndroid Build Coastguard Worker * @param[out] aBuffer A pointer to a buffer to fill with the random bytes. 582*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSize Size of buffer (number of bytes to fill). 583*cfb92d14SAndroid Build Coastguard Worker * 584*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Successfully filled buffer with random values. 585*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Operation failed. 586*cfb92d14SAndroid Build Coastguard Worker * 587*cfb92d14SAndroid Build Coastguard Worker */ 588*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoRandomGet(uint8_t *aBuffer, uint16_t aSize); 589*cfb92d14SAndroid Build Coastguard Worker 590*cfb92d14SAndroid Build Coastguard Worker /** 591*cfb92d14SAndroid Build Coastguard Worker * Generate and populate the output buffer with a new ECDSA key-pair. 592*cfb92d14SAndroid Build Coastguard Worker * 593*cfb92d14SAndroid Build Coastguard Worker * @param[out] aKeyPair A pointer to an ECDSA key-pair structure to store the generated key-pair. 594*cfb92d14SAndroid Build Coastguard Worker * 595*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE A new key-pair was generated successfully. 596*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for key generation. 597*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NOT_CAPABLE Feature not supported. 598*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to generate key-pair. 599*cfb92d14SAndroid Build Coastguard Worker * 600*cfb92d14SAndroid Build Coastguard Worker */ 601*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaGenerateKey(otPlatCryptoEcdsaKeyPair *aKeyPair); 602*cfb92d14SAndroid Build Coastguard Worker 603*cfb92d14SAndroid Build Coastguard Worker /** 604*cfb92d14SAndroid Build Coastguard Worker * Get the associated public key from the input context. 605*cfb92d14SAndroid Build Coastguard Worker * 606*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyPair A pointer to an ECDSA key-pair structure where the key-pair is stored. 607*cfb92d14SAndroid Build Coastguard Worker * @param[out] aPublicKey A pointer to an ECDSA public key structure to store the public key. 608*cfb92d14SAndroid Build Coastguard Worker * 609*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Public key was retrieved successfully, and @p aBuffer is updated. 610*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE The key-pair DER format could not be parsed (invalid format). 611*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The @p aContext is NULL. 612*cfb92d14SAndroid Build Coastguard Worker * 613*cfb92d14SAndroid Build Coastguard Worker */ 614*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaGetPublicKey(const otPlatCryptoEcdsaKeyPair *aKeyPair, otPlatCryptoEcdsaPublicKey *aPublicKey); 615*cfb92d14SAndroid Build Coastguard Worker 616*cfb92d14SAndroid Build Coastguard Worker /** 617*cfb92d14SAndroid Build Coastguard Worker * Calculate the ECDSA signature for a hashed message using the private key from the input context. 618*cfb92d14SAndroid Build Coastguard Worker * 619*cfb92d14SAndroid Build Coastguard Worker * Uses the deterministic digital signature generation procedure from RFC 6979. 620*cfb92d14SAndroid Build Coastguard Worker * 621*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyPair A pointer to an ECDSA key-pair structure where the key-pair is stored. 622*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHash A pointer to a SHA-256 hash structure where the hash value for signature calculation 623*cfb92d14SAndroid Build Coastguard Worker * is stored. 624*cfb92d14SAndroid Build Coastguard Worker * @param[out] aSignature A pointer to an ECDSA signature structure to output the calculated signature. 625*cfb92d14SAndroid Build Coastguard Worker * 626*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE The signature was calculated successfully, @p aSignature was updated. 627*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE The key-pair DER format could not be parsed (invalid format). 628*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for signature calculation. 629*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The @p aContext is NULL. 630*cfb92d14SAndroid Build Coastguard Worker * 631*cfb92d14SAndroid Build Coastguard Worker */ 632*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaSign(const otPlatCryptoEcdsaKeyPair *aKeyPair, 633*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoSha256Hash *aHash, 634*cfb92d14SAndroid Build Coastguard Worker otPlatCryptoEcdsaSignature *aSignature); 635*cfb92d14SAndroid Build Coastguard Worker 636*cfb92d14SAndroid Build Coastguard Worker /** 637*cfb92d14SAndroid Build Coastguard Worker * Use the key from the input context to verify the ECDSA signature of a hashed message. 638*cfb92d14SAndroid Build Coastguard Worker * 639*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPublicKey A pointer to an ECDSA public key structure where the public key for signature 640*cfb92d14SAndroid Build Coastguard Worker * verification is stored. 641*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHash A pointer to a SHA-256 hash structure where the hash value for signature verification 642*cfb92d14SAndroid Build Coastguard Worker * is stored. 643*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSignature A pointer to an ECDSA signature structure where the signature value to be verified is 644*cfb92d14SAndroid Build Coastguard Worker * stored. 645*cfb92d14SAndroid Build Coastguard Worker * 646*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE The signature was verified successfully. 647*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_SECURITY The signature is invalid. 648*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The key or hash is invalid. 649*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for signature verification. 650*cfb92d14SAndroid Build Coastguard Worker * 651*cfb92d14SAndroid Build Coastguard Worker */ 652*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaVerify(const otPlatCryptoEcdsaPublicKey *aPublicKey, 653*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoSha256Hash *aHash, 654*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoEcdsaSignature *aSignature); 655*cfb92d14SAndroid Build Coastguard Worker 656*cfb92d14SAndroid Build Coastguard Worker /** 657*cfb92d14SAndroid Build Coastguard Worker * Calculate the ECDSA signature for a hashed message using the Key reference passed. 658*cfb92d14SAndroid Build Coastguard Worker * 659*cfb92d14SAndroid Build Coastguard Worker * Uses the deterministic digital signature generation procedure from RFC 6979. 660*cfb92d14SAndroid Build Coastguard Worker * 661*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef Key Reference to the slot where the key-pair is stored. 662*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHash A pointer to a SHA-256 hash structure where the hash value for signature calculation 663*cfb92d14SAndroid Build Coastguard Worker * is stored. 664*cfb92d14SAndroid Build Coastguard Worker * @param[out] aSignature A pointer to an ECDSA signature structure to output the calculated signature. 665*cfb92d14SAndroid Build Coastguard Worker * 666*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE The signature was calculated successfully, @p aSignature was updated. 667*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE The key-pair DER format could not be parsed (invalid format). 668*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for signature calculation. 669*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The @p aContext is NULL. 670*cfb92d14SAndroid Build Coastguard Worker * 671*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 672*cfb92d14SAndroid Build Coastguard Worker * 673*cfb92d14SAndroid Build Coastguard Worker */ 674*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaSignUsingKeyRef(otCryptoKeyRef aKeyRef, 675*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoSha256Hash *aHash, 676*cfb92d14SAndroid Build Coastguard Worker otPlatCryptoEcdsaSignature *aSignature); 677*cfb92d14SAndroid Build Coastguard Worker 678*cfb92d14SAndroid Build Coastguard Worker /** 679*cfb92d14SAndroid Build Coastguard Worker * Get the associated public key from the key reference passed. 680*cfb92d14SAndroid Build Coastguard Worker * 681*cfb92d14SAndroid Build Coastguard Worker * The public key is stored differently depending on the crypto backend library being used 682*cfb92d14SAndroid Build Coastguard Worker * (OPENTHREAD_CONFIG_CRYPTO_LIB). 683*cfb92d14SAndroid Build Coastguard Worker * 684*cfb92d14SAndroid Build Coastguard Worker * This API must make sure to return the public key as a byte sequence representation of an 685*cfb92d14SAndroid Build Coastguard Worker * uncompressed curve point (RFC 6605 - sec 4) 686*cfb92d14SAndroid Build Coastguard Worker * 687*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef Key Reference to the slot where the key-pair is stored. 688*cfb92d14SAndroid Build Coastguard Worker * @param[out] aPublicKey A pointer to an ECDSA public key structure to store the public key. 689*cfb92d14SAndroid Build Coastguard Worker * 690*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE Public key was retrieved successfully, and @p aBuffer is updated. 691*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_PARSE The key-pair DER format could not be parsed (invalid format). 692*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The @p aContext is NULL. 693*cfb92d14SAndroid Build Coastguard Worker * 694*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 695*cfb92d14SAndroid Build Coastguard Worker * 696*cfb92d14SAndroid Build Coastguard Worker */ 697*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaExportPublicKey(otCryptoKeyRef aKeyRef, otPlatCryptoEcdsaPublicKey *aPublicKey); 698*cfb92d14SAndroid Build Coastguard Worker 699*cfb92d14SAndroid Build Coastguard Worker /** 700*cfb92d14SAndroid Build Coastguard Worker * Generate and import a new ECDSA key-pair at reference passed. 701*cfb92d14SAndroid Build Coastguard Worker * 702*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef Key Reference to the slot where the key-pair is stored. 703*cfb92d14SAndroid Build Coastguard Worker * 704*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE A new key-pair was generated successfully. 705*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for key generation. 706*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NOT_CAPABLE Feature not supported. 707*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to generate key-pair. 708*cfb92d14SAndroid Build Coastguard Worker * 709*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 710*cfb92d14SAndroid Build Coastguard Worker * 711*cfb92d14SAndroid Build Coastguard Worker */ 712*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaGenerateAndImportKey(otCryptoKeyRef aKeyRef); 713*cfb92d14SAndroid Build Coastguard Worker 714*cfb92d14SAndroid Build Coastguard Worker /** 715*cfb92d14SAndroid Build Coastguard Worker * Use the keyref to verify the ECDSA signature of a hashed message. 716*cfb92d14SAndroid Build Coastguard Worker * 717*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyRef Key Reference to the slot where the key-pair is stored. 718*cfb92d14SAndroid Build Coastguard Worker * @param[in] aHash A pointer to a SHA-256 hash structure where the hash value for signature verification 719*cfb92d14SAndroid Build Coastguard Worker * is stored. 720*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSignature A pointer to an ECDSA signature structure where the signature value to be verified is 721*cfb92d14SAndroid Build Coastguard Worker * stored. 722*cfb92d14SAndroid Build Coastguard Worker * 723*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE The signature was verified successfully. 724*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_SECURITY The signature is invalid. 725*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_INVALID_ARGS The key or hash is invalid. 726*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for signature verification. 727*cfb92d14SAndroid Build Coastguard Worker * 728*cfb92d14SAndroid Build Coastguard Worker * @note This API is only used by OT core when `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE` is enabled. 729*cfb92d14SAndroid Build Coastguard Worker * 730*cfb92d14SAndroid Build Coastguard Worker */ 731*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoEcdsaVerifyUsingKeyRef(otCryptoKeyRef aKeyRef, 732*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoSha256Hash *aHash, 733*cfb92d14SAndroid Build Coastguard Worker const otPlatCryptoEcdsaSignature *aSignature); 734*cfb92d14SAndroid Build Coastguard Worker 735*cfb92d14SAndroid Build Coastguard Worker /** 736*cfb92d14SAndroid Build Coastguard Worker * Perform PKCS#5 PBKDF2 using CMAC (AES-CMAC-PRF-128). 737*cfb92d14SAndroid Build Coastguard Worker * 738*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPassword Password to use when generating key. 739*cfb92d14SAndroid Build Coastguard Worker * @param[in] aPasswordLen Length of password. 740*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSalt Salt to use when generating key. 741*cfb92d14SAndroid Build Coastguard Worker * @param[in] aSaltLen Length of salt. 742*cfb92d14SAndroid Build Coastguard Worker * @param[in] aIterationCounter Iteration count. 743*cfb92d14SAndroid Build Coastguard Worker * @param[in] aKeyLen Length of generated key in bytes. 744*cfb92d14SAndroid Build Coastguard Worker * @param[out] aKey A pointer to the generated key. 745*cfb92d14SAndroid Build Coastguard Worker * 746*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NONE A new key-pair was generated successfully. 747*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NO_BUFS Failed to allocate buffer for key generation. 748*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_NOT_CAPABLE Feature not supported. 749*cfb92d14SAndroid Build Coastguard Worker * @retval OT_ERROR_FAILED Failed to generate key. 750*cfb92d14SAndroid Build Coastguard Worker */ 751*cfb92d14SAndroid Build Coastguard Worker otError otPlatCryptoPbkdf2GenerateKey(const uint8_t *aPassword, 752*cfb92d14SAndroid Build Coastguard Worker uint16_t aPasswordLen, 753*cfb92d14SAndroid Build Coastguard Worker const uint8_t *aSalt, 754*cfb92d14SAndroid Build Coastguard Worker uint16_t aSaltLen, 755*cfb92d14SAndroid Build Coastguard Worker uint32_t aIterationCounter, 756*cfb92d14SAndroid Build Coastguard Worker uint16_t aKeyLen, 757*cfb92d14SAndroid Build Coastguard Worker uint8_t *aKey); 758*cfb92d14SAndroid Build Coastguard Worker 759*cfb92d14SAndroid Build Coastguard Worker /** 760*cfb92d14SAndroid Build Coastguard Worker * @} 761*cfb92d14SAndroid Build Coastguard Worker * 762*cfb92d14SAndroid Build Coastguard Worker */ 763*cfb92d14SAndroid Build Coastguard Worker 764*cfb92d14SAndroid Build Coastguard Worker #ifdef __cplusplus 765*cfb92d14SAndroid Build Coastguard Worker } // end of extern "C" 766*cfb92d14SAndroid Build Coastguard Worker #endif 767*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_PLATFORM_CRYPTO_H_ 768