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  *   This file includes definitions for performing HMAC SHA-256 computations.
32  */
33 
34 #ifndef HMAC_SHA256_HPP_
35 #define HMAC_SHA256_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stdint.h>
40 
41 #include <mbedtls/md.h>
42 
43 #include "crypto/sha256.hpp"
44 
45 namespace ot {
46 
47 class Message;
48 
49 namespace Crypto {
50 
51 /**
52  * @addtogroup core-security
53  *
54  * @{
55  *
56  */
57 
58 /**
59  * This class implements HMAC SHA-256 computation.
60  *
61  */
62 class HmacSha256
63 {
64 public:
65     /**
66      * This type represents a HMAC SHA-256 hash.
67      *
68      */
69     typedef Sha256::Hash Hash;
70 
71     /**
72      * Constructor for `HmacSha256`.
73      *
74      */
75     HmacSha256(void);
76 
77     /**
78      * Destructor for `HmacSha256`.
79      *
80      */
81     ~HmacSha256(void);
82 
83     /**
84      * This method sets the key and starts the HMAC computation.
85      *
86      * @param[in]  aKey        A pointer to the key.
87      * @param[in]  aKeyLength  The key length in bytes.
88      *
89      */
90     void Start(const uint8_t *aKey, uint16_t aKeyLength);
91 
92     /**
93      * This method inputs bytes into the HMAC computation.
94      *
95      * @param[in]  aBuf        A pointer to the input buffer.
96      * @param[in]  aBufLength  The length of @p aBuf in bytes.
97      *
98      */
99     void Update(const void *aBuf, uint16_t aBufLength);
100 
101     /**
102      * This method inputs an object (treated as a sequence of bytes) into the HMAC computation.
103      *
104      * @tparam    ObjectType   The object type.
105      *
106      * @param[in] aObject      A reference to the object.
107      *
108      */
Update(const ObjectType & aObject)109     template <typename ObjectType> void Update(const ObjectType &aObject)
110     {
111         static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
112         return Update(&aObject, sizeof(ObjectType));
113     }
114 
115     /**
116      * This method inputs the bytes read from a given message into the HMAC computation.
117      *
118      * @param[in] aMessage    The message to read the data from.
119      * @param[in] aOffset     The offset into @p aMessage to start to read.
120      * @param[in] aLength     The number of bytes to read.
121      *
122      */
123     void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
124 
125     /**
126      * This method finalizes the hash computation.
127      *
128      * @param[out]  aHash  A reference to a `Hash` to output the calculated hash.
129      *
130      */
131     void Finish(Hash &aHash);
132 
133 private:
134     mbedtls_md_context_t mContext;
135 };
136 
137 /**
138  * @}
139  *
140  */
141 
142 } // namespace Crypto
143 } // namespace ot
144 
145 #endif // HMAC_SHA256_HPP_
146