Crypto++ 8.1 Free C++ class library of cryptographic schemes

words.h
1 #ifndef CRYPTOPP_WORDS_H
2 #define CRYPTOPP_WORDS_H
3
4 #include "config.h"
5 #include "misc.h"
6
7 NAMESPACE_BEGIN(CryptoPP)
8
9 inline size_t CountWords(const word *X, size_t N)
10 {
11 while (N && X[N-1]==0)
12 N--;
13 return N;
14 }
15
16 inline void SetWords(word *r, word a, size_t n)
17 {
18 for (size_t i=0; i<n; i++)
19 r[i] = a;
20 }
21
22 inline void CopyWords(word *r, const word *a, size_t n)
23 {
24 if (r != a)
25 #if CRYPTOPP_MSC_VERSION
26 memcpy_s(r, n*WORD_SIZE, a, n*WORD_SIZE);
27 #else
28 memcpy(r, a, n*WORD_SIZE);
29 #endif
30 }
31
32 inline void XorWords(word *r, const word *a, const word *b, size_t n)
33 {
34 for (size_t i=0; i<n; i++)
35 r[i] = a[i] ^ b[i];
36 }
37
38 inline void XorWords(word *r, const word *a, size_t n)
39 {
40 for (size_t i=0; i<n; i++)
41 r[i] ^= a[i];
42 }
43
44 inline void AndWords(word *r, const word *a, const word *b, size_t n)
45 {
46 for (size_t i=0; i<n; i++)
47 r[i] = a[i] & b[i];
48 }
49
50 inline void AndWords(word *r, const word *a, size_t n)
51 {
52 for (size_t i=0; i<n; i++)
53 r[i] &= a[i];
54 }
55
56 inline void OrWords(word *r, const word *a, const word *b, size_t n)
57 {
58 for (size_t i=0; i<n; i++)
59 r[i] = a[i] | b[i];
60 }
61
62 inline void OrWords(word *r, const word *a, size_t n)
63 {
64 for (size_t i=0; i<n; i++)
65 r[i] |= a[i];
66 }
67
68 inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
69 {
70 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
71 word u, carry=0;
72 if (shiftBits)
73 for (size_t i=0; i<n; i++)
74 {
75 u = r[i];
76 r[i] = (u << shiftBits) | carry;
77 carry = u >> (WORD_BITS-shiftBits);
78 }
79 return carry;
80 }
81
82 inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
83 {
84 CRYPTOPP_ASSERT (shiftBits<WORD_BITS);
85 word u, carry=0;
86 if (shiftBits)
87 for (size_t i=n; i>0; i--)
88 {
89 u = r[i-1];
90 r[i-1] = (u >> shiftBits) | carry;
91 carry = u << (WORD_BITS-shiftBits);
92 }
93 return carry;
94 }
95
96 inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
97 {
98 shiftWords = STDMIN(shiftWords, n);
99 if (shiftWords)
100 {
101 for (size_t i=n-1; i>=shiftWords; i--)
102 r[i] = r[i-shiftWords];
103 SetWords(r, 0, shiftWords);
104 }
105 }
106
107 inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
108 {
109 shiftWords = STDMIN(shiftWords, n);
110 if (shiftWords)
111 {
112 for (size_t i=0; i+shiftWords<n; i++)
113 r[i] = r[i+shiftWords];
114 SetWords(r+n-shiftWords, 0, shiftWords);
115 }
116 }
117
118 NAMESPACE_END
119
120 #endif
misc.h
Utility functions for the Crypto++ library.
memcpy_s
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:411
config.h
Library configuration file.
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:535
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:69
CryptoPP
Crypto++ library namespace.