»Core Development>Code coverage>Modules/_sha3/kcp/KeccakP-1600-opt64.c

Python code coverage for Modules/_sha3/kcp/KeccakP-1600-opt64.c

#countcontent
1n/a/*
2n/aImplementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3n/aJoan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4n/adenoted as "the implementer".
5n/a
6n/aFor more information, feedback or questions, please refer to our websites:
7n/ahttp://keccak.noekeon.org/
8n/ahttp://keyak.noekeon.org/
9n/ahttp://ketje.noekeon.org/
10n/a
11n/aTo the extent possible under law, the implementer has waived all copyright
12n/aand related or neighboring rights to the source code in this file.
13n/ahttp://creativecommons.org/publicdomain/zero/1.0/
14n/a*/
15n/a
16n/a#include <string.h>
17n/a#include <stdlib.h>
18n/a/* #include "brg_endian.h" */
19n/a#include "KeccakP-1600-opt64-config.h"
20n/a
21n/a#if NOT_PYTHON
22n/atypedef unsigned char UINT8;
23n/a/* typedef unsigned long long int UINT64; */
24n/a#endif
25n/a
26n/a#if defined(KeccakP1600_useLaneComplementing)
27n/a#define UseBebigokimisa
28n/a#endif
29n/a
30n/a#if defined(_MSC_VER)
31n/a#define ROL64(a, offset) _rotl64(a, offset)
32n/a#elif defined(KeccakP1600_useSHLD)
33n/a #define ROL64(x,N) ({ \
34n/a register UINT64 __out; \
35n/a register UINT64 __in = x; \
36n/a __asm__ ("shld %2,%0,%0" : "=r"(__out) : "0"(__in), "i"(N)); \
37n/a __out; \
38n/a })
39n/a#else
40n/a#define ROL64(a, offset) ((((UINT64)a) << offset) ^ (((UINT64)a) >> (64-offset)))
41n/a#endif
42n/a
43n/a#include "KeccakP-1600-64.macros"
44n/a#ifdef KeccakP1600_fullUnrolling
45n/a#define FullUnrolling
46n/a#else
47n/a#define Unrolling KeccakP1600_unrolling
48n/a#endif
49n/a#include "KeccakP-1600-unrolling.macros"
50n/a#include "SnP-Relaned.h"
51n/a
52n/astatic const UINT64 KeccakF1600RoundConstants[24] = {
53n/a 0x0000000000000001ULL,
54n/a 0x0000000000008082ULL,
55n/a 0x800000000000808aULL,
56n/a 0x8000000080008000ULL,
57n/a 0x000000000000808bULL,
58n/a 0x0000000080000001ULL,
59n/a 0x8000000080008081ULL,
60n/a 0x8000000000008009ULL,
61n/a 0x000000000000008aULL,
62n/a 0x0000000000000088ULL,
63n/a 0x0000000080008009ULL,
64n/a 0x000000008000000aULL,
65n/a 0x000000008000808bULL,
66n/a 0x800000000000008bULL,
67n/a 0x8000000000008089ULL,
68n/a 0x8000000000008003ULL,
69n/a 0x8000000000008002ULL,
70n/a 0x8000000000000080ULL,
71n/a 0x000000000000800aULL,
72n/a 0x800000008000000aULL,
73n/a 0x8000000080008081ULL,
74n/a 0x8000000000008080ULL,
75n/a 0x0000000080000001ULL,
76n/a 0x8000000080008008ULL };
77n/a
78n/a/* ---------------------------------------------------------------- */
79n/a
80n/avoid KeccakP1600_Initialize(void *state)
81n/a{
82n/a memset(state, 0, 200);
83n/a#ifdef KeccakP1600_useLaneComplementing
84n/a ((UINT64*)state)[ 1] = ~(UINT64)0;
85n/a ((UINT64*)state)[ 2] = ~(UINT64)0;
86n/a ((UINT64*)state)[ 8] = ~(UINT64)0;
87n/a ((UINT64*)state)[12] = ~(UINT64)0;
88n/a ((UINT64*)state)[17] = ~(UINT64)0;
89n/a ((UINT64*)state)[20] = ~(UINT64)0;
90n/a#endif
91n/a}
92n/a
93n/a/* ---------------------------------------------------------------- */
94n/a
95n/avoid KeccakP1600_AddBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
96n/a{
97n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
98n/a UINT64 lane;
99n/a if (length == 0)
100n/a return;
101n/a if (length == 1)
102n/a lane = data[0];
103n/a else {
104n/a lane = 0;
105n/a memcpy(&lane, data, length);
106n/a }
107n/a lane <<= offset*8;
108n/a#else
109n/a UINT64 lane = 0;
110n/a unsigned int i;
111n/a for(i=0; i<length; i++)
112n/a lane |= ((UINT64)data[i]) << ((i+offset)*8);
113n/a#endif
114n/a ((UINT64*)state)[lanePosition] ^= lane;
115n/a}
116n/a
117n/a/* ---------------------------------------------------------------- */
118n/a
119n/avoid KeccakP1600_AddLanes(void *state, const unsigned char *data, unsigned int laneCount)
120n/a{
121n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
122n/a unsigned int i = 0;
123n/a#ifdef NO_MISALIGNED_ACCESSES
124n/a /* If either pointer is misaligned, fall back to byte-wise xor. */
125n/a
126n/a if (((((uintptr_t)state) & 7) != 0) || ((((uintptr_t)data) & 7) != 0)) {
127n/a for (i = 0; i < laneCount * 8; i++) {
128n/a ((unsigned char*)state)[i] ^= data[i];
129n/a }
130n/a }
131n/a else
132n/a#endif
133n/a {
134n/a /* Otherwise... */
135n/a
136n/a for( ; (i+8)<=laneCount; i+=8) {
137n/a ((UINT64*)state)[i+0] ^= ((UINT64*)data)[i+0];
138n/a ((UINT64*)state)[i+1] ^= ((UINT64*)data)[i+1];
139n/a ((UINT64*)state)[i+2] ^= ((UINT64*)data)[i+2];
140n/a ((UINT64*)state)[i+3] ^= ((UINT64*)data)[i+3];
141n/a ((UINT64*)state)[i+4] ^= ((UINT64*)data)[i+4];
142n/a ((UINT64*)state)[i+5] ^= ((UINT64*)data)[i+5];
143n/a ((UINT64*)state)[i+6] ^= ((UINT64*)data)[i+6];
144n/a ((UINT64*)state)[i+7] ^= ((UINT64*)data)[i+7];
145n/a }
146n/a for( ; (i+4)<=laneCount; i+=4) {
147n/a ((UINT64*)state)[i+0] ^= ((UINT64*)data)[i+0];
148n/a ((UINT64*)state)[i+1] ^= ((UINT64*)data)[i+1];
149n/a ((UINT64*)state)[i+2] ^= ((UINT64*)data)[i+2];
150n/a ((UINT64*)state)[i+3] ^= ((UINT64*)data)[i+3];
151n/a }
152n/a for( ; (i+2)<=laneCount; i+=2) {
153n/a ((UINT64*)state)[i+0] ^= ((UINT64*)data)[i+0];
154n/a ((UINT64*)state)[i+1] ^= ((UINT64*)data)[i+1];
155n/a }
156n/a if (i<laneCount) {
157n/a ((UINT64*)state)[i+0] ^= ((UINT64*)data)[i+0];
158n/a }
159n/a }
160n/a#else
161n/a unsigned int i;
162n/a UINT8 *curData = data;
163n/a for(i=0; i<laneCount; i++, curData+=8) {
164n/a UINT64 lane = (UINT64)curData[0]
165n/a | ((UINT64)curData[1] << 8)
166n/a | ((UINT64)curData[2] << 16)
167n/a | ((UINT64)curData[3] << 24)
168n/a | ((UINT64)curData[4] <<32)
169n/a | ((UINT64)curData[5] << 40)
170n/a | ((UINT64)curData[6] << 48)
171n/a | ((UINT64)curData[7] << 56);
172n/a ((UINT64*)state)[i] ^= lane;
173n/a }
174n/a#endif
175n/a}
176n/a
177n/a/* ---------------------------------------------------------------- */
178n/a
179n/a#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
180n/avoid KeccakP1600_AddByte(void *state, unsigned char byte, unsigned int offset)
181n/a{
182n/a UINT64 lane = byte;
183n/a lane <<= (offset%8)*8;
184n/a ((UINT64*)state)[offset/8] ^= lane;
185n/a}
186n/a#endif
187n/a
188n/a/* ---------------------------------------------------------------- */
189n/a
190n/avoid KeccakP1600_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
191n/a{
192n/a SnP_AddBytes(state, data, offset, length, KeccakP1600_AddLanes, KeccakP1600_AddBytesInLane, 8);
193n/a}
194n/a
195n/a/* ---------------------------------------------------------------- */
196n/a
197n/avoid KeccakP1600_OverwriteBytesInLane(void *state, unsigned int lanePosition, const unsigned char *data, unsigned int offset, unsigned int length)
198n/a{
199n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
200n/a#ifdef KeccakP1600_useLaneComplementing
201n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20)) {
202n/a unsigned int i;
203n/a for(i=0; i<length; i++)
204n/a ((unsigned char*)state)[lanePosition*8+offset+i] = ~data[i];
205n/a }
206n/a else
207n/a#endif
208n/a {
209n/a memcpy((unsigned char*)state+lanePosition*8+offset, data, length);
210n/a }
211n/a#else
212n/a#error "Not yet implemented"
213n/a#endif
214n/a}
215n/a
216n/a/* ---------------------------------------------------------------- */
217n/a
218n/avoid KeccakP1600_OverwriteLanes(void *state, const unsigned char *data, unsigned int laneCount)
219n/a{
220n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
221n/a#ifdef KeccakP1600_useLaneComplementing
222n/a unsigned int lanePosition;
223n/a
224n/a for(lanePosition=0; lanePosition<laneCount; lanePosition++)
225n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
226n/a ((UINT64*)state)[lanePosition] = ~((const UINT64*)data)[lanePosition];
227n/a else
228n/a ((UINT64*)state)[lanePosition] = ((const UINT64*)data)[lanePosition];
229n/a#else
230n/a memcpy(state, data, laneCount*8);
231n/a#endif
232n/a#else
233n/a#error "Not yet implemented"
234n/a#endif
235n/a}
236n/a
237n/a/* ---------------------------------------------------------------- */
238n/a
239n/avoid KeccakP1600_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
240n/a{
241n/a SnP_OverwriteBytes(state, data, offset, length, KeccakP1600_OverwriteLanes, KeccakP1600_OverwriteBytesInLane, 8);
242n/a}
243n/a
244n/a/* ---------------------------------------------------------------- */
245n/a
246n/avoid KeccakP1600_OverwriteWithZeroes(void *state, unsigned int byteCount)
247n/a{
248n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
249n/a#ifdef KeccakP1600_useLaneComplementing
250n/a unsigned int lanePosition;
251n/a
252n/a for(lanePosition=0; lanePosition<byteCount/8; lanePosition++)
253n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
254n/a ((UINT64*)state)[lanePosition] = ~0;
255n/a else
256n/a ((UINT64*)state)[lanePosition] = 0;
257n/a if (byteCount%8 != 0) {
258n/a lanePosition = byteCount/8;
259n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
260n/a memset((unsigned char*)state+lanePosition*8, 0xFF, byteCount%8);
261n/a else
262n/a memset((unsigned char*)state+lanePosition*8, 0, byteCount%8);
263n/a }
264n/a#else
265n/a memset(state, 0, byteCount);
266n/a#endif
267n/a#else
268n/a#error "Not yet implemented"
269n/a#endif
270n/a}
271n/a
272n/a/* ---------------------------------------------------------------- */
273n/a
274n/avoid KeccakP1600_Permute_24rounds(void *state)
275n/a{
276n/a declareABCDE
277n/a #ifndef KeccakP1600_fullUnrolling
278n/a unsigned int i;
279n/a #endif
280n/a UINT64 *stateAsLanes = (UINT64*)state;
281n/a
282n/a copyFromState(A, stateAsLanes)
283n/a rounds24
284n/a copyToState(stateAsLanes, A)
285n/a}
286n/a
287n/a/* ---------------------------------------------------------------- */
288n/a
289n/avoid KeccakP1600_Permute_12rounds(void *state)
290n/a{
291n/a declareABCDE
292n/a #ifndef KeccakP1600_fullUnrolling
293n/a unsigned int i;
294n/a #endif
295n/a UINT64 *stateAsLanes = (UINT64*)state;
296n/a
297n/a copyFromState(A, stateAsLanes)
298n/a rounds12
299n/a copyToState(stateAsLanes, A)
300n/a}
301n/a
302n/a/* ---------------------------------------------------------------- */
303n/a
304n/avoid KeccakP1600_ExtractBytesInLane(const void *state, unsigned int lanePosition, unsigned char *data, unsigned int offset, unsigned int length)
305n/a{
306n/a UINT64 lane = ((UINT64*)state)[lanePosition];
307n/a#ifdef KeccakP1600_useLaneComplementing
308n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
309n/a lane = ~lane;
310n/a#endif
311n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
312n/a {
313n/a UINT64 lane1[1];
314n/a lane1[0] = lane;
315n/a memcpy(data, (UINT8*)lane1+offset, length);
316n/a }
317n/a#else
318n/a unsigned int i;
319n/a lane >>= offset*8;
320n/a for(i=0; i<length; i++) {
321n/a data[i] = lane & 0xFF;
322n/a lane >>= 8;
323n/a }
324n/a#endif
325n/a}
326n/a
327n/a/* ---------------------------------------------------------------- */
328n/a
329n/a#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
330n/avoid fromWordToBytes(UINT8 *bytes, const UINT64 word)
331n/a{
332n/a unsigned int i;
333n/a
334n/a for(i=0; i<(64/8); i++)
335n/a bytes[i] = (word >> (8*i)) & 0xFF;
336n/a}
337n/a#endif
338n/a
339n/avoid KeccakP1600_ExtractLanes(const void *state, unsigned char *data, unsigned int laneCount)
340n/a{
341n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
342n/a memcpy(data, state, laneCount*8);
343n/a#else
344n/a unsigned int i;
345n/a
346n/a for(i=0; i<laneCount; i++)
347n/a fromWordToBytes(data+(i*8), ((const UINT64*)state)[i]);
348n/a#endif
349n/a#ifdef KeccakP1600_useLaneComplementing
350n/a if (laneCount > 1) {
351n/a ((UINT64*)data)[ 1] = ~((UINT64*)data)[ 1];
352n/a if (laneCount > 2) {
353n/a ((UINT64*)data)[ 2] = ~((UINT64*)data)[ 2];
354n/a if (laneCount > 8) {
355n/a ((UINT64*)data)[ 8] = ~((UINT64*)data)[ 8];
356n/a if (laneCount > 12) {
357n/a ((UINT64*)data)[12] = ~((UINT64*)data)[12];
358n/a if (laneCount > 17) {
359n/a ((UINT64*)data)[17] = ~((UINT64*)data)[17];
360n/a if (laneCount > 20) {
361n/a ((UINT64*)data)[20] = ~((UINT64*)data)[20];
362n/a }
363n/a }
364n/a }
365n/a }
366n/a }
367n/a }
368n/a#endif
369n/a}
370n/a
371n/a/* ---------------------------------------------------------------- */
372n/a
373n/avoid KeccakP1600_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
374n/a{
375n/a SnP_ExtractBytes(state, data, offset, length, KeccakP1600_ExtractLanes, KeccakP1600_ExtractBytesInLane, 8);
376n/a}
377n/a
378n/a/* ---------------------------------------------------------------- */
379n/a
380n/avoid KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePosition, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
381n/a{
382n/a UINT64 lane = ((UINT64*)state)[lanePosition];
383n/a#ifdef KeccakP1600_useLaneComplementing
384n/a if ((lanePosition == 1) || (lanePosition == 2) || (lanePosition == 8) || (lanePosition == 12) || (lanePosition == 17) || (lanePosition == 20))
385n/a lane = ~lane;
386n/a#endif
387n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
388n/a {
389n/a unsigned int i;
390n/a UINT64 lane1[1];
391n/a lane1[0] = lane;
392n/a for(i=0; i<length; i++)
393n/a output[i] = input[i] ^ ((UINT8*)lane1)[offset+i];
394n/a }
395n/a#else
396n/a unsigned int i;
397n/a lane >>= offset*8;
398n/a for(i=0; i<length; i++) {
399n/a output[i] = input[i] ^ (lane & 0xFF);
400n/a lane >>= 8;
401n/a }
402n/a#endif
403n/a}
404n/a
405n/a/* ---------------------------------------------------------------- */
406n/a
407n/avoid KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount)
408n/a{
409n/a unsigned int i;
410n/a#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
411n/a unsigned char temp[8];
412n/a unsigned int j;
413n/a#endif
414n/a
415n/a for(i=0; i<laneCount; i++) {
416n/a#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
417n/a ((UINT64*)output)[i] = ((UINT64*)input)[i] ^ ((const UINT64*)state)[i];
418n/a#else
419n/a fromWordToBytes(temp, ((const UINT64*)state)[i]);
420n/a for(j=0; j<8; j++)
421n/a output[i*8+j] = input[i*8+j] ^ temp[j];
422n/a#endif
423n/a }
424n/a#ifdef KeccakP1600_useLaneComplementing
425n/a if (laneCount > 1) {
426n/a ((UINT64*)output)[ 1] = ~((UINT64*)output)[ 1];
427n/a if (laneCount > 2) {
428n/a ((UINT64*)output)[ 2] = ~((UINT64*)output)[ 2];
429n/a if (laneCount > 8) {
430n/a ((UINT64*)output)[ 8] = ~((UINT64*)output)[ 8];
431n/a if (laneCount > 12) {
432n/a ((UINT64*)output)[12] = ~((UINT64*)output)[12];
433n/a if (laneCount > 17) {
434n/a ((UINT64*)output)[17] = ~((UINT64*)output)[17];
435n/a if (laneCount > 20) {
436n/a ((UINT64*)output)[20] = ~((UINT64*)output)[20];
437n/a }
438n/a }
439n/a }
440n/a }
441n/a }
442n/a }
443n/a#endif
444n/a}
445n/a
446n/a/* ---------------------------------------------------------------- */
447n/a
448n/avoid KeccakP1600_ExtractAndAddBytes(const void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
449n/a{
450n/a SnP_ExtractAndAddBytes(state, input, output, offset, length, KeccakP1600_ExtractAndAddLanes, KeccakP1600_ExtractAndAddBytesInLane, 8);
451n/a}
452n/a
453n/a/* ---------------------------------------------------------------- */
454n/a
455n/asize_t KeccakF1600_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen)
456n/a{
457n/a size_t originalDataByteLen = dataByteLen;
458n/a declareABCDE
459n/a #ifndef KeccakP1600_fullUnrolling
460n/a unsigned int i;
461n/a #endif
462n/a UINT64 *stateAsLanes = (UINT64*)state;
463n/a UINT64 *inDataAsLanes = (UINT64*)data;
464n/a
465n/a copyFromState(A, stateAsLanes)
466n/a while(dataByteLen >= laneCount*8) {
467n/a addInput(A, inDataAsLanes, laneCount)
468n/a rounds24
469n/a inDataAsLanes += laneCount;
470n/a dataByteLen -= laneCount*8;
471n/a }
472n/a copyToState(stateAsLanes, A)
473n/a return originalDataByteLen - dataByteLen;
474n/a}