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 /**
65  * Implements SHA-256 computation.
66  *
67  */
68 class Sha256
69 {
70 public:
71     /**
72      * Represents a SHA-256 hash.
73      *
74      */
75     class Hash : public otCryptoSha256Hash, public Clearable<Hash>, public Equatable<Hash>
76     {
77     public:
78         static const uint8_t kSize = OT_CRYPTO_SHA256_HASH_SIZE; ///< SHA-256 hash size (bytes)
79 
80         /**
81          * Returns a pointer to a byte array containing the hash value.
82          *
83          * @returns A pointer to a byte array containing the hash.
84          *
85          */
GetBytes(void) const86         const uint8_t *GetBytes(void) const { return m8; }
87     };
88 
89     /**
90      * Constructor for `Sha256` object.
91      *
92      */
93     Sha256(void);
94 
95     /**
96      * Destructor for `Sha256` object.
97      *
98      */
99     ~Sha256(void);
100 
101     /**
102      * Starts the SHA-256 computation.
103      *
104      */
105     void Start(void);
106 
107     /**
108      * Inputs bytes into the SHA-256 computation.
109      *
110      * @param[in]  aBuf        A pointer to the input buffer.
111      * @param[in]  aBufLength  The length of @p aBuf in bytes.
112      *
113      */
114     void Update(const void *aBuf, uint16_t aBufLength);
115 
116     /**
117      * Inputs an object (treated as a sequence of bytes) into the SHA-256 computation.
118      *
119      * @tparam    ObjectType   The object type.
120      *
121      * @param[in] aObject      A reference to the object.
122      *
123      */
Update(const ObjectType & aObject)124     template <typename ObjectType> void Update(const ObjectType &aObject)
125     {
126         static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer");
127         return Update(&aObject, sizeof(ObjectType));
128     }
129 
130     /**
131      * Inputs the bytes read from a given message into the SHA-256 computation.
132      *
133      * @param[in] aMessage    The message to read the data from.
134      * @param[in] aOffset     The offset into @p aMessage to start to read.
135      * @param[in] aLength     The number of bytes to read.
136      *
137      */
138     void Update(const Message &aMessage, uint16_t aOffset, uint16_t aLength);
139 
140     /**
141      * Finalizes the hash computation.
142      *
143      * @param[out]  aHash  A reference to a `Hash` to output the calculated hash.
144      *
145      */
146     void Finish(Hash &aHash);
147 
148 private:
149     otCryptoContext mContext;
150     OT_DEFINE_ALIGNED_VAR(mContextStorage, kSha256ContextSize, uint64_t);
151 };
152 
153 /**
154  * @}
155  *
156  */
157 
158 } // namespace Crypto
159 
160 DefineCoreType(otCryptoSha256Hash, Crypto::Sha256::Hash);
161 
162 } // namespace ot
163 
164 #endif // SHA256_HPP_
165