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