/src/qt/qtbase/src/3rdparty/sha3/KeccakNISTInterface.c
Line | Count | Source |
1 | | /* |
2 | | The Keccak sponge function, designed by Guido Bertoni, Joan Daemen, |
3 | | Michaƫl Peeters and Gilles Van Assche. For more information, feedback or |
4 | | questions, please refer to our website: http://keccak.noekeon.org/ |
5 | | |
6 | | Implementation by the designers, |
7 | | hereby denoted as "the implementer". |
8 | | |
9 | | To the extent possible under law, the implementer has waived all copyright |
10 | | and related or neighboring rights to the source code in this file. |
11 | | http://creativecommons.org/publicdomain/zero/1.0/ |
12 | | */ |
13 | | |
14 | | #include <string.h> |
15 | | //#include "KeccakNISTInterface.h" |
16 | | #include "KeccakF-1600-interface.h" |
17 | | |
18 | | static HashReturn Init(hashState *state, int hashbitlen) |
19 | 3.10k | { |
20 | 3.10k | switch(hashbitlen) { |
21 | 0 | case 0: // Default parameters, arbitrary length output |
22 | 0 | InitSponge((spongeState*)state, 1024, 576); |
23 | 0 | break; |
24 | 776 | case 224: |
25 | 776 | InitSponge((spongeState*)state, 1152, 448); |
26 | 776 | break; |
27 | 776 | case 256: |
28 | 776 | InitSponge((spongeState*)state, 1088, 512); |
29 | 776 | break; |
30 | 776 | case 384: |
31 | 776 | InitSponge((spongeState*)state, 832, 768); |
32 | 776 | break; |
33 | 776 | case 512: |
34 | 776 | InitSponge((spongeState*)state, 576, 1024); |
35 | 776 | break; |
36 | 0 | default: |
37 | 0 | return BAD_HASHLEN; |
38 | 3.10k | } |
39 | 3.10k | state->fixedOutputLength = hashbitlen; |
40 | 3.10k | return SUCCESS; |
41 | 3.10k | } |
42 | | |
43 | | static HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen) |
44 | 4.65k | { |
45 | 4.65k | if ((databitlen % 8) == 0) |
46 | 3.10k | return (HashReturn) Absorb((spongeState*)state, data, databitlen); |
47 | 1.55k | else { |
48 | 1.55k | HashReturn ret = (HashReturn) Absorb((spongeState*)state, data, databitlen - (databitlen % 8)); |
49 | 1.55k | if (ret == SUCCESS) { |
50 | 1.55k | unsigned char lastByte; |
51 | | // Align the last partial byte to the least significant bits |
52 | 1.55k | lastByte = data[databitlen/8] >> (8 - (databitlen % 8)); |
53 | 1.55k | return (HashReturn) Absorb((spongeState*)state, &lastByte, databitlen % 8); |
54 | 1.55k | } |
55 | 0 | else |
56 | 0 | return ret; |
57 | 1.55k | } |
58 | 4.65k | } |
59 | | |
60 | | static HashReturn Final(hashState *state, BitSequence *hashval) |
61 | 3.10k | { |
62 | 3.10k | return (HashReturn) Squeeze(state, hashval, state->fixedOutputLength); |
63 | 3.10k | } |
64 | | |
65 | | #ifndef QT_BUILDING_QT |
66 | | static HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval) |
67 | | { |
68 | | hashState state; |
69 | | HashReturn result; |
70 | | |
71 | | if ((hashbitlen != 224) && (hashbitlen != 256) && (hashbitlen != 384) && (hashbitlen != 512)) |
72 | | return BAD_HASHLEN; // Only the four fixed output lengths available through this API |
73 | | result = Init(&state, hashbitlen); |
74 | | if (result != SUCCESS) |
75 | | return result; |
76 | | result = Update(&state, data, databitlen); |
77 | | if (result != SUCCESS) |
78 | | return result; |
79 | | result = Final(&state, hashval); |
80 | | return result; |
81 | | } |
82 | | #endif |