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 CRC computations. 32 */ 33 34 #ifndef CRC_HPP_ 35 #define CRC_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include "common/message.hpp" 40 #include "common/type_traits.hpp" 41 42 namespace ot { 43 44 constexpr uint16_t kCrc16CcittPolynomial = 0x1021; ///< CRC16-CCITT Polynomial (x^16 + x^12 + x^5 + 1) 45 constexpr uint16_t kCrc16AnsiPolynomial = 0x8005; ///< CRC16-ANSI Polynomial (x^16 + x^15 + x^2 + 1) 46 47 /** 48 * CRC32-ANSI Polynomial 49 * 50 * (x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1) 51 */ 52 constexpr uint32_t kCrc32AnsiPolynomial = 0x04c11db7; 53 54 /** 55 * Implements CRC computations. 56 * 57 * @tparam UintType The unsigned int type indicating CRC bit width. MUST be either `uint16_t` or `uint32_t`. 58 */ 59 template <typename UintType> class CrcCalculator 60 { 61 constexpr static bool kIsUint16 = TypeTraits::IsSame<UintType, uint16_t>::kValue; 62 constexpr static bool kIsUint32 = TypeTraits::IsSame<UintType, uint32_t>::kValue; 63 64 static_assert(kIsUint16 || kIsUint32, "UintType MUST be either `uint16_t` or `uint32_t`"); 65 66 public: 67 /** 68 * Initializes the `CrcCalculator` object. 69 * 70 * @param[in] aPolynomial The polynomial to use for CRC calculation. 71 */ CrcCalculator(UintType aPolynomial)72 explicit CrcCalculator(UintType aPolynomial) 73 : mPolynomial(aPolynomial) 74 , mCrc(0) 75 { 76 } 77 78 /** 79 * Gets the current CRC value. 80 * 81 * @returns The current CRC value. 82 */ GetCrc(void) const83 UintType GetCrc(void) const { return mCrc; } 84 85 /** 86 * Feeds a byte value into the CRC computation. 87 * 88 * @param[in] aByte The byte value. 89 * 90 * @returns The current CRC value. 91 */ 92 UintType FeedByte(uint8_t aByte); 93 94 /** 95 * Feeds a sequence of bytes into the CRC computation. 96 * 97 * @param[in] aBytes A pointer to buffer containing the bytes. 98 * @param[in] aLength Number of bytes in @p aBytes. 99 * 100 * @returns The current CRC value. 101 */ 102 UintType FeedBytes(const void *aBytes, uint16_t aLength); 103 104 /** 105 * Feed an object (all its bytes) into the CRC computation. 106 * 107 * @tparam ObjectType The object type. 108 * 109 * @param[in] aObject A reference to the object. 110 * 111 * @returns The current CRC value. 112 */ Feed(const ObjectType & aObject)113 template <typename ObjectType> UintType Feed(const ObjectType &aObject) 114 { 115 static_assert(!TypeTraits::IsPointer<ObjectType>::kValue, "ObjectType must not be a pointer"); 116 return FeedBytes(&aObject, sizeof(ObjectType)); 117 } 118 119 /** 120 * Feed bytes read from a given message within a given offset range into the CRC computation. 121 * 122 * @param[in] aMessage The message to read from. 123 * @param[in] aOffsetRaneg The offset range in @p aMessage to read bytes from. 124 * 125 * @returns The current CRC value. 126 */ 127 UintType Feed(const Message &aMessage, const OffsetRange &aOffsetRange); 128 129 private: 130 UintType mPolynomial; 131 UintType mCrc; 132 }; 133 134 } // namespace ot 135 136 #endif // CRC_HPP_ 137