|
||||
File indexing completed on 2025-01-18 09:55:10
0001 // words.h - originally written and placed in the public domain by Wei Dai 0002 0003 /// \file words.h 0004 /// \brief Support functions for word operations 0005 0006 #ifndef CRYPTOPP_WORDS_H 0007 #define CRYPTOPP_WORDS_H 0008 0009 #include "config.h" 0010 #include "misc.h" 0011 0012 NAMESPACE_BEGIN(CryptoPP) 0013 0014 /// \brief Count the number of words 0015 /// \param x word array 0016 /// \param n size of the word array, in elements 0017 /// \return number of words used in the array. 0018 /// \details CountWords counts the number of words in a word array. 0019 /// Leading 0-words are not included in the count. 0020 /// \since Crypto++ 1.0 0021 inline size_t CountWords(const word *x, size_t n) 0022 { 0023 while (n && x[n-1]==0) 0024 n--; 0025 return n; 0026 } 0027 0028 /// \brief Set the value of words 0029 /// \param r word array 0030 /// \param a value 0031 /// \param n size of the word array, in elements 0032 /// \details SetWords sets all elements in the word array to the 0033 /// specified value. 0034 /// \since Crypto++ 1.0 0035 inline void SetWords(word *r, word a, size_t n) 0036 { 0037 for (size_t i=0; i<n; i++) 0038 r[i] = a; 0039 } 0040 0041 /// \brief Copy word array 0042 /// \param r destination word array 0043 /// \param a source word array 0044 /// \param n size of the word array, in elements 0045 /// \details CopyWords copies the source word array to the destination 0046 /// word array. 0047 /// \since Crypto++ 1.0 0048 inline void CopyWords(word *r, const word *a, size_t n) 0049 { 0050 if (r != a) 0051 #if CRYPTOPP_MSC_VERSION 0052 memcpy_s(r, n*WORD_SIZE, a, n*WORD_SIZE); 0053 #else 0054 std::memcpy(r, a, n*WORD_SIZE); 0055 #endif 0056 } 0057 0058 /// \brief XOR word arrays 0059 /// \param r destination word array 0060 /// \param a first source word array 0061 /// \param b second source word array 0062 /// \param n size of the word array, in elements 0063 /// \details XorWords XORs the two source word arrays and copies the 0064 /// result to the destination word array. 0065 /// \since Crypto++ 1.0 0066 inline void XorWords(word *r, const word *a, const word *b, size_t n) 0067 { 0068 for (size_t i=0; i<n; i++) 0069 r[i] = a[i] ^ b[i]; 0070 } 0071 0072 /// \brief XOR word arrays 0073 /// \param r destination word array 0074 /// \param a source word array 0075 /// \param n size of the word array, in elements 0076 /// \details XorWords XORs the source word array with the 0077 /// destination word array. 0078 /// \since Crypto++ 1.0 0079 inline void XorWords(word *r, const word *a, size_t n) 0080 { 0081 for (size_t i=0; i<n; i++) 0082 r[i] ^= a[i]; 0083 } 0084 0085 /// \brief AND word arrays 0086 /// \param r destination word array 0087 /// \param a first source word array 0088 /// \param b second source word array 0089 /// \param n size of the word array, in elements 0090 /// \details AndWords ANDs the two source word arrays and copies the 0091 /// result to the destination word array. 0092 /// \since Crypto++ 1.0 0093 inline void AndWords(word *r, const word *a, const word *b, size_t n) 0094 { 0095 for (size_t i=0; i<n; i++) 0096 r[i] = a[i] & b[i]; 0097 } 0098 0099 /// \brief AND word arrays 0100 /// \param r destination word array 0101 /// \param a source word array 0102 /// \param n size of the word array, in elements 0103 /// \details AndWords ANDs the source word array with the 0104 /// destination word array. 0105 /// \since Crypto++ 1.0 0106 inline void AndWords(word *r, const word *a, size_t n) 0107 { 0108 for (size_t i=0; i<n; i++) 0109 r[i] &= a[i]; 0110 } 0111 0112 /// \brief OR word arrays 0113 /// \param r destination word array 0114 /// \param a first source word array 0115 /// \param b second source word array 0116 /// \param n size of the word array, in elements 0117 /// \details OrWords ORs the two source word arrays and copies the 0118 /// result to the destination word array. 0119 /// \since Crypto++ 1.0 0120 inline void OrWords(word *r, const word *a, const word *b, size_t n) 0121 { 0122 for (size_t i=0; i<n; i++) 0123 r[i] = a[i] | b[i]; 0124 } 0125 0126 /// \brief OR word arrays 0127 /// \param r destination word array 0128 /// \param a source word array 0129 /// \param n size of the word array, in elements 0130 /// \details OrWords ORs the source word array with the 0131 /// destination word array. 0132 /// \since Crypto++ 1.0 0133 inline void OrWords(word *r, const word *a, size_t n) 0134 { 0135 for (size_t i=0; i<n; i++) 0136 r[i] |= a[i]; 0137 } 0138 0139 /// \brief Left shift word array 0140 /// \param r word array 0141 /// \param n size of the word array, in elements 0142 /// \param shiftBits number of bits to shift 0143 /// \return word shifted out 0144 /// \details ShiftWordsLeftByBits shifts the word array left by 0145 /// shiftBits. ShiftWordsLeftByBits shifts bits out on the left; 0146 /// it does not extend the array. 0147 /// \note shiftBits must be less than WORD_BITS. 0148 /// \since Crypto++ 1.0 0149 inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits) 0150 { 0151 CRYPTOPP_ASSERT (shiftBits<WORD_BITS); 0152 word u, carry=0; 0153 if (shiftBits) 0154 for (size_t i=0; i<n; i++) 0155 { 0156 u = r[i]; 0157 r[i] = (u << shiftBits) | carry; 0158 carry = u >> (WORD_BITS-shiftBits); 0159 } 0160 return carry; 0161 } 0162 0163 /// \brief Right shift word array 0164 /// \param r word array 0165 /// \param n size of the word array, in elements 0166 /// \param shiftBits number of bits to shift 0167 /// \return word shifted out 0168 /// \details ShiftWordsRightByBits shifts the word array shight by 0169 /// shiftBits. ShiftWordsRightByBits shifts bits out on the right. 0170 /// \note shiftBits must be less than WORD_BITS. 0171 /// \since Crypto++ 1.0 0172 inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits) 0173 { 0174 CRYPTOPP_ASSERT (shiftBits<WORD_BITS); 0175 word u, carry=0; 0176 if (shiftBits) 0177 for (size_t i=n; i>0; i--) 0178 { 0179 u = r[i-1]; 0180 r[i-1] = (u >> shiftBits) | carry; 0181 carry = u << (WORD_BITS-shiftBits); 0182 } 0183 return carry; 0184 } 0185 0186 /// \brief Left shift word array 0187 /// \param r word array 0188 /// \param n size of the word array, in elements 0189 /// \param shiftWords number of words to shift 0190 /// \details ShiftWordsLeftByWords shifts the word array left by 0191 /// shiftWords. ShiftWordsLeftByWords shifts bits out on the left; 0192 /// it does not extend the array. 0193 /// \since Crypto++ 1.0 0194 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords) 0195 { 0196 shiftWords = STDMIN(shiftWords, n); 0197 if (shiftWords) 0198 { 0199 for (size_t i=n-1; i>=shiftWords; i--) 0200 r[i] = r[i-shiftWords]; 0201 SetWords(r, 0, shiftWords); 0202 } 0203 } 0204 0205 /// \brief Right shift word array 0206 /// \param r word array 0207 /// \param n size of the word array, in elements 0208 /// \param shiftWords number of words to shift 0209 /// \details ShiftWordsRightByWords shifts the word array right by 0210 /// shiftWords. ShiftWordsRightByWords shifts bits out on the right. 0211 /// \since Crypto++ 1.0 0212 inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords) 0213 { 0214 shiftWords = STDMIN(shiftWords, n); 0215 if (shiftWords) 0216 { 0217 for (size_t i=0; i+shiftWords<n; i++) 0218 r[i] = r[i+shiftWords]; 0219 SetWords(r+n-shiftWords, 0, shiftWords); 0220 } 0221 } 0222 0223 NAMESPACE_END 0224 0225 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |