xref: /aosp_15_r20/external/toybox/toys/other/sha3sum.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* sha3sum.c - Keccak-f[1600] permutation, sponge construction
2  *
3  * Copyright 2014 David Leon Gil <[email protected]>
4  *
5  * https://keccak.team/files/Keccak-reference-3.0.pdf
6  * https://csrc.nist.gov/publications/detail/fips/202/final
7  * https://nvlpubs.nist.gov/nistpubs/specialpublications/nist.sp.800-185.pdf
8 
9 USE_SHA3SUM(NEWTOY(sha3sum, "bSa#<128>512=224", TOYFLAG_USR|TOYFLAG_BIN))
10 
11 config SHA3SUM
12   bool "sha3sum"
13   default y
14   help
15     usage: sha3sum [-bS] [-a BITS] [FILE...]
16 
17     Hash function du jour.
18 
19     -a	Produce a hash BITS long (default 224)
20     -b	Brief (hash only, no filename)
21     -S	Use SHAKE termination byte instead of SHA3 (ask FIPS why)
22 */
23 
24 #define FOR_sha3sum
25 #include "toys.h"
26 
27 GLOBALS(
28   long a;
29   unsigned long long rc[24];
30 )
31 
32 static const char rho[] =
33   {1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44};
34 static const char pi[] =
35   {10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1};
36 static const char rcpack[] =
37   {0x33,0x07,0xdd,0x16,0x38,0x1b,0x7b,0x2b,0xad,0x6a,0xce,0x4c,0x29,0xfe,0x31,
38    0x68,0x9d,0xb0,0x8f,0x2f,0x0a};
39 
keccak(unsigned long long * a)40 static void keccak(unsigned long long *a)
41 {
42   unsigned long long b[5] = {0}, t;
43   int i, x, y;
44 
45   for (i = 0; i < 24; i++) {
46     for (x = 0; x<5; x++) for (b[x] = 0, y = 0; y<25; y += 5) b[x] ^= a[x+y];
47     for (x = 0; x<5; x++) for (y = 0; y<25; y += 5) {
48       t = b[(x+1)%5];
49       a[y+x] ^= b[(x+4)%5]^(t<<1|t>>63);
50     }
51     for (t = a[1], x = 0; x<24; x++) {
52       *b = a[pi[x]];
53       a[pi[x]] = (t<<rho[x])|(t>>(64-rho[x]));
54       t = *b;
55     }
56     for (y = 0; y<25; y += 5) {
57       for (x = 0; x<5; x++) b[x] = a[y + x];
58       for (x = 0; x<5; x++) a[y + x] = b[x]^((~b[(x+1)%5])&b[(x+2)%5]);
59     }
60     *a ^= TT.rc[i];
61   }
62 }
63 
do_sha3sum(int fd,char * name)64 static void do_sha3sum(int fd, char *name)
65 {
66   int span, ii, len, rate = 200-TT.a/4;
67   char *ss = toybuf, buf[200];
68 
69   memset(buf, 0, sizeof(buf));
70   for (len = 0;; ss += rate) {
71     if ((span = len-(ss-toybuf))<rate) {
72       memcpy(toybuf, ss, span);
73       len = span += readall(fd, (ss = toybuf)+span, sizeof(toybuf)-span);
74     }
75     if (span>rate) span = rate;
76     for (ii = 0; ii<span; ii++) buf[ii] ^= ss[ii];
77     if (rate!=span) {
78       buf[span] ^= FLAG(S) ? 0x1f : 0x06;
79       buf[rate-1] ^= 0x80;
80     }
81     keccak((void *)buf);
82     if (span<rate) break;
83   }
84 
85   for (ii = 0; ii<TT.a/8; ) {
86     printf("%02x", buf[ii%rate]);
87     if (!(++ii%rate)) keccak((void *)buf);
88   }
89   memset(buf, 0, sizeof(buf));
90 
91   xprintf("  %s\n"+(FLAG(b)<<2), name);
92 }
93 
94 // TODO test 224 256 384 512, and shake 128 256
sha3sum_main(void)95 void sha3sum_main(void)
96 {
97   int i, j, k;
98   char *s;
99 
100   // Decompress RC table
101   for (s = (void *)rcpack, i = 127; i; s += 3) for (i>>=1,k = j = 0; k<24; k++)
102     if (1&(s[k>>3]>>(7-(k&7)))) TT.rc[k] |= 1ULL<<i;
103 
104   loopfiles(toys.optargs, do_sha3sum);
105 }
106