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 <openthread/platform/crypto.h>
42 
43 #include "common/code_utils.hpp"
44 #include "crypto/context_size.hpp"
45 #include "crypto/sha256.hpp"
46 #include "crypto/storage.hpp"
47 
48 namespace ot {
49 
50 class Message;
51 
52 namespace Crypto {
53 
54 /**
55  * @addtogroup core-security
56  *
57  * @{
58  */
59 
60 /**
61  * Implements HMAC SHA-256 computation.
62  */
63 class HmacSha256
64 {
65 public:
66     /**
67      * Represents a HMAC SHA-256 hash.
68      */
69     typedef Sha256::Hash Hash;
70 
71     /**
72      * Constructor for `HmacSha256`.
73      */
74     HmacSha256(void);
75 
76     /**
77      * Destructor for `HmacSha256`.
78      */
79     ~HmacSha256(void);
80 
81     /**
82      * Sets the key and starts the HMAC computation.
83      *
84      * @param[in]  aKey      The key to use.
85      */
86     void Start(const Key &aKey);
87 
88     /**
89      * Inputs bytes into the HMAC computation.
90      *
91      * @param[in]  aBuf        A pointer to the input buffer.
92      * @param[in]  aBufLength  The length of @p aBuf in bytes.
93      */
94     void Update(const void *aBuf, uint16_t aBufLength);
95 
96     /**
97      * Inputs an object (treated as a sequence of bytes) into the HMAC computation.
98      *
99      * @tparam    ObjectType   The object type.
100      *
101      * @param[in] aObject      A reference to the object.
102      */
Update(const ObjectType & aObject)103     template <typename ObjectType> void Update(const ObjectType &aObject)
104     {
105         static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
106         return Update(&aObject, sizeof(ObjectType));
107     }
108 
109     /**
110      * Inputs the bytes read from a given message into the HMAC computation.
111      *
112      * @param[in] aMessage    The message to read the data from.
113      * @param[in] aOffset     The offset into @p aMessage to start to read.
114      * @param[in] aLength     The number of bytes to read.
115      */
116     void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
117 
118     /**
119      * Finalizes the hash computation.
120      *
121      * @param[out]  aHash  A reference to a `Hash` to output the calculated hash.
122      */
123     void Finish(Hash &aHash);
124 
125 private:
126     otCryptoContext mContext;
127     OT_DEFINE_ALIGNED_VAR(mContextStorage, kHmacSha256ContextSize, uint64_t);
128 };
129 
130 /**
131  * @}
132  */
133 
134 } // namespace Crypto
135 } // namespace ot
136 
137 #endif // HMAC_SHA256_HPP_
138