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 SHA-256 computations.
32  */
33 
34 #ifndef SHA256_HPP_
35 #define SHA256_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stdint.h>
40 
41 #include <openthread/crypto.h>
42 #include <openthread/platform/crypto.h>
43 
44 #include "common/as_core_type.hpp"
45 #include "common/clearable.hpp"
46 #include "common/code_utils.hpp"
47 #include "common/equatable.hpp"
48 #include "common/type_traits.hpp"
49 #include "crypto/context_size.hpp"
50 
51 namespace ot {
52 
53 class Message;
54 
55 namespace Crypto {
56 
57 /**
58  * @addtogroup core-security
59  *
60  * @{
61  */
62 
63 /**
64  * Implements SHA-256 computation.
65  */
66 class Sha256
67 {
68 public:
69     /**
70      * Represents a SHA-256 hash.
71      */
72     class Hash : public otCryptoSha256Hash, public Clearable<Hash>, public Equatable<Hash>
73     {
74     public:
75         static const uint8_t kSize = OT_CRYPTO_SHA256_HASH_SIZE; ///< SHA-256 hash size (bytes)
76 
77         /**
78          * Returns a pointer to a byte array containing the hash value.
79          *
80          * @returns A pointer to a byte array containing the hash.
81          */
GetBytes(void) const82         const uint8_t *GetBytes(void) const { return m8; }
83     };
84 
85     /**
86      * Constructor for `Sha256` object.
87      */
88     Sha256(void);
89 
90     /**
91      * Destructor for `Sha256` object.
92      */
93     ~Sha256(void);
94 
95     /**
96      * Starts the SHA-256 computation.
97      */
98     void Start(void);
99 
100     /**
101      * Inputs bytes into the SHA-256 computation.
102      *
103      * @param[in]  aBuf        A pointer to the input buffer.
104      * @param[in]  aBufLength  The length of @p aBuf in bytes.
105      */
106     void Update(const void *aBuf, uint16_t aBufLength);
107 
108     /**
109      * Inputs an object (treated as a sequence of bytes) into the SHA-256 computation.
110      *
111      * @tparam    ObjectType   The object type.
112      *
113      * @param[in] aObject      A reference to the object.
114      */
Update(const ObjectType & aObject)115     template <typename ObjectType> void Update(const ObjectType &aObject)
116     {
117         static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
118         return Update(&aObject, sizeof(ObjectType));
119     }
120 
121     /**
122      * Inputs the bytes read from a given message into the SHA-256 computation.
123      *
124      * @param[in] aMessage    The message to read the data from.
125      * @param[in] aOffset     The offset into @p aMessage to start to read.
126      * @param[in] aLength     The number of bytes to read.
127      */
128     void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
129 
130     /**
131      * Finalizes the hash computation.
132      *
133      * @param[out]  aHash  A reference to a `Hash` to output the calculated hash.
134      */
135     void Finish(Hash &aHash);
136 
137 private:
138     otCryptoContext mContext;
139     OT_DEFINE_ALIGNED_VAR(mContextStorage, kSha256ContextSize, uint64_t);
140 };
141 
142 /**
143  * @}
144  */
145 
146 } // namespace Crypto
147 
148 DefineCoreType(otCryptoSha256Hash, Crypto::Sha256::Hash);
149 
150 } // namespace ot
151 
152 #endif // SHA256_HPP_
153