1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  * @brief
32  *  This file defines the OpenThread crypto C APIs.
33  */
34 
35 #ifndef OPENTHREAD_CRYPTO_H_
36 #define OPENTHREAD_CRYPTO_H_
37 
38 #include <stdbool.h>
39 #include <stdint.h>
40 
41 #include <openthread/error.h>
42 #include <openthread/platform/crypto.h>
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /**
49  * @addtogroup api-crypto
50  *
51  * @brief
52  *   This module includes cryptographic functions.
53  *
54  * @{
55  *
56  */
57 
58 /**
59  * Represents a SHA-256 hash.
60  *
61  */
62 typedef otPlatCryptoSha256Hash otCryptoSha256Hash;
63 
64 /**
65  * Performs HMAC computation.
66  *
67  * @param[in]     aKey           A pointer to the key.
68  * @param[in]     aBuf           A pointer to the input buffer.
69  * @param[in]     aBufLength     The length of @p aBuf in bytes.
70  * @param[out]    aHash          A pointer to a `otCryptoSha256Hash` structure to output the hash value.
71  *
72  */
73 void otCryptoHmacSha256(const otCryptoKey *aKey, const uint8_t *aBuf, uint16_t aBufLength, otCryptoSha256Hash *aHash);
74 
75 /**
76  * Performs AES CCM computation.
77  *
78  * @param[in]      aKey           A pointer to the key.
79  * @param[in]      aTagLength     Length of tag in bytes.
80  * @param[in]      aNonce         A pointer to the nonce.
81  * @param[in]      aNonceLength   Length of nonce in bytes.
82  *
83  * @param[in]      aHeader        A pointer to the header.
84  * @param[in]      aHeaderLength  Length of header in bytes.
85  *
86  * @param[in,out]  aPlainText     A pointer to the plaintext.
87  * @param[in,out]  aCipherText    A pointer to the ciphertext.
88  * @param[in]      aLength        Plaintext length in bytes.
89  * @param[in]      aEncrypt       `true` on encrypt and `false` on decrypt.
90  *
91  * @param[out]     aTag           A pointer to the tag.
92  *
93  */
94 void otCryptoAesCcm(const otCryptoKey *aKey,
95                     uint8_t            aTagLength,
96                     const void        *aNonce,
97                     uint8_t            aNonceLength,
98                     const void        *aHeader,
99                     uint32_t           aHeaderLength,
100                     void              *aPlainText,
101                     void              *aCipherText,
102                     uint32_t           aLength,
103                     bool               aEncrypt,
104                     void              *aTag);
105 
106 /**
107  * @}
108  *
109  */
110 
111 #ifdef __cplusplus
112 } // extern "C"
113 #endif
114 
115 #endif // OPENTHREAD_CRYPTO_H_
116