Security Scol plugin
words.h
Go to the documentation of this file.
1// words.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_WORDS_H
7#define CRYPTOPP_WORDS_H
8
9#include "config.h"
10#include "misc.h"
11
12NAMESPACE_BEGIN(CryptoPP)
13
14
21inline size_t CountWords(const word *x, size_t n)
22{
23 while (n && x[n-1]==0)
24 n--;
25 return n;
26}
27
35inline void SetWords(word *r, word a, size_t n)
36{
37 for (size_t i=0; i<n; i++)
38 r[i] = a;
39}
40
48inline void CopyWords(word *r, const word *a, size_t n)
49{
50 if (r != a)
51#if CRYPTOPP_MSC_VERSION
52 memcpy_s(r, n*WORD_SIZE, a, n*WORD_SIZE);
53#else
54 memcpy(r, a, n*WORD_SIZE);
55#endif
56}
57
66inline void XorWords(word *r, const word *a, const word *b, size_t n)
67{
68 for (size_t i=0; i<n; i++)
69 r[i] = a[i] ^ b[i];
70}
71
79inline void XorWords(word *r, const word *a, size_t n)
80{
81 for (size_t i=0; i<n; i++)
82 r[i] ^= a[i];
83}
84
93inline void AndWords(word *r, const word *a, const word *b, size_t n)
94{
95 for (size_t i=0; i<n; i++)
96 r[i] = a[i] & b[i];
97}
98
106inline void AndWords(word *r, const word *a, size_t n)
107{
108 for (size_t i=0; i<n; i++)
109 r[i] &= a[i];
110}
111
120inline void OrWords(word *r, const word *a, const word *b, size_t n)
121{
122 for (size_t i=0; i<n; i++)
123 r[i] = a[i] | b[i];
124}
125
133inline void OrWords(word *r, const word *a, size_t n)
134{
135 for (size_t i=0; i<n; i++)
136 r[i] |= a[i];
137}
138
149inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
150{
151 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
152 word u, carry=0;
153 if (shiftBits)
154 for (size_t i=0; i<n; i++)
155 {
156 u = r[i];
157 r[i] = (u << shiftBits) | carry;
158 carry = u >> (WORD_BITS-shiftBits);
159 }
160 return carry;
161}
162
172inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
173{
174 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
175 word u, carry=0;
176 if (shiftBits)
177 for (size_t i=n; i>0; i--)
178 {
179 u = r[i-1];
180 r[i-1] = (u >> shiftBits) | carry;
181 carry = u << (WORD_BITS-shiftBits);
182 }
183 return carry;
184}
185
194inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
195{
196 shiftWords = STDMIN(shiftWords, n);
197 if (shiftWords)
198 {
199 for (size_t i=n-1; i>=shiftWords; i--)
200 r[i] = r[i-shiftWords];
201 SetWords(r, 0, shiftWords);
202 }
203}
204
212inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
213{
214 shiftWords = STDMIN(shiftWords, n);
215 if (shiftWords)
216 {
217 for (size_t i=0; i+shiftWords<n; i++)
218 r[i] = r[i+shiftWords];
219 SetWords(r+n-shiftWords, 0, shiftWords);
220 }
221}
222
223NAMESPACE_END
224
225#endif
Library configuration file.
const unsigned int WORD_BITS
Size of a platform word in bits.
Definition config_int.h:249
const unsigned int WORD_SIZE
Size of a platform word in bytes.
Definition config_int.h:245
Utility functions for the Crypto++ library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition misc.h:525
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition misc.h:655
void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
Right shift word array.
Definition words.h:212
void XorWords(word *r, const word *a, const word *b, size_t n)
XOR word arrays.
Definition words.h:66
void SetWords(word *r, word a, size_t n)
Set the value of words.
Definition words.h:35
void OrWords(word *r, const word *a, const word *b, size_t n)
OR word arrays.
Definition words.h:120
word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
Left shift word array.
Definition words.h:149
void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
Left shift word array.
Definition words.h:194
size_t CountWords(const word *x, size_t n)
Count the number of words.
Definition words.h:21
word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
Right shift word array.
Definition words.h:172
void CopyWords(word *r, const word *a, size_t n)
Copy word array.
Definition words.h:48
void AndWords(word *r, const word *a, const word *b, size_t n)
AND word arrays.
Definition words.h:93