]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/meta-ti-glsdk.git/blob - recipes-kernel/linux/linux-ti33x-psp-3.2/3.2.3/0021-crypto-sha512-reduce-stack-usage-to-safe-number.patch
linux-ti33x-psp 3.2: update to 3.2.5
[glsdk/meta-ti-glsdk.git] / recipes-kernel / linux / linux-ti33x-psp-3.2 / 3.2.3 / 0021-crypto-sha512-reduce-stack-usage-to-safe-number.patch
1 From fed2a3f1c1ae549460ec39ba6492854e066c83d0 Mon Sep 17 00:00:00 2001
2 From: Alexey Dobriyan <adobriyan@gmail.com>
3 Date: Sat, 14 Jan 2012 21:40:57 +0300
4 Subject: [PATCH 21/90] crypto: sha512 - reduce stack usage to safe number
6 commit 51fc6dc8f948047364f7d42a4ed89b416c6cc0a3 upstream.
8 For rounds 16--79, W[i] only depends on W[i - 2], W[i - 7], W[i - 15] and W[i - 16].
9 Consequently, keeping all W[80] array on stack is unnecessary,
10 only 16 values are really needed.
12 Using W[16] instead of W[80] greatly reduces stack usage
13 (~750 bytes to ~340 bytes on x86_64).
15 Line by line explanation:
16 * BLEND_OP
17   array is "circular" now, all indexes have to be modulo 16.
18   Round number is positive, so remainder operation should be
19   without surprises.
21 * initial full message scheduling is trimmed to first 16 values which
22   come from data block, the rest is calculated before it's needed.
24 * original loop body is unrolled version of new SHA512_0_15 and
25   SHA512_16_79 macros, unrolling was done to not do explicit variable
26   renaming. Otherwise it's the very same code after preprocessing.
27   See sha1_transform() code which does the same trick.
29 Patch survives in-tree crypto test and original bugreport test
30 (ping flood with hmac(sha512).
32 See FIPS 180-2 for SHA-512 definition
33 http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf
35 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
36 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
37 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
38 ---
39  crypto/sha512_generic.c |   58 +++++++++++++++++++++++++++-------------------
40  1 files changed, 34 insertions(+), 24 deletions(-)
42 diff --git a/crypto/sha512_generic.c b/crypto/sha512_generic.c
43 index 8b9035b..88f160b 100644
44 --- a/crypto/sha512_generic.c
45 +++ b/crypto/sha512_generic.c
46 @@ -78,7 +78,7 @@ static inline void LOAD_OP(int I, u64 *W, const u8 *input)
47  
48  static inline void BLEND_OP(int I, u64 *W)
49  {
50 -       W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
51 +       W[I % 16] += s1(W[(I-2) % 16]) + W[(I-7) % 16] + s0(W[(I-15) % 16]);
52  }
53  
54  static void
55 @@ -87,38 +87,48 @@ sha512_transform(u64 *state, const u8 *input)
56         u64 a, b, c, d, e, f, g, h, t1, t2;
57  
58         int i;
59 -       u64 W[80];
60 +       u64 W[16];
61  
62         /* load the input */
63          for (i = 0; i < 16; i++)
64                  LOAD_OP(i, W, input);
65  
66 -        for (i = 16; i < 80; i++) {
67 -                BLEND_OP(i, W);
68 -        }
69 -
70         /* load the state into our registers */
71         a=state[0];   b=state[1];   c=state[2];   d=state[3];
72         e=state[4];   f=state[5];   g=state[6];   h=state[7];
73  
74 -       /* now iterate */
75 -       for (i=0; i<80; i+=8) {
76 -               t1 = h + e1(e) + Ch(e,f,g) + sha512_K[i  ] + W[i  ];
77 -               t2 = e0(a) + Maj(a,b,c);    d+=t1;    h=t1+t2;
78 -               t1 = g + e1(d) + Ch(d,e,f) + sha512_K[i+1] + W[i+1];
79 -               t2 = e0(h) + Maj(h,a,b);    c+=t1;    g=t1+t2;
80 -               t1 = f + e1(c) + Ch(c,d,e) + sha512_K[i+2] + W[i+2];
81 -               t2 = e0(g) + Maj(g,h,a);    b+=t1;    f=t1+t2;
82 -               t1 = e + e1(b) + Ch(b,c,d) + sha512_K[i+3] + W[i+3];
83 -               t2 = e0(f) + Maj(f,g,h);    a+=t1;    e=t1+t2;
84 -               t1 = d + e1(a) + Ch(a,b,c) + sha512_K[i+4] + W[i+4];
85 -               t2 = e0(e) + Maj(e,f,g);    h+=t1;    d=t1+t2;
86 -               t1 = c + e1(h) + Ch(h,a,b) + sha512_K[i+5] + W[i+5];
87 -               t2 = e0(d) + Maj(d,e,f);    g+=t1;    c=t1+t2;
88 -               t1 = b + e1(g) + Ch(g,h,a) + sha512_K[i+6] + W[i+6];
89 -               t2 = e0(c) + Maj(c,d,e);    f+=t1;    b=t1+t2;
90 -               t1 = a + e1(f) + Ch(f,g,h) + sha512_K[i+7] + W[i+7];
91 -               t2 = e0(b) + Maj(b,c,d);    e+=t1;    a=t1+t2;
92 +#define SHA512_0_15(i, a, b, c, d, e, f, g, h)                 \
93 +       t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[i];      \
94 +       t2 = e0(a) + Maj(a, b, c);                              \
95 +       d += t1;                                                \
96 +       h = t1 + t2
97 +
98 +#define SHA512_16_79(i, a, b, c, d, e, f, g, h)                        \
99 +       BLEND_OP(i, W);                                         \
100 +       t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[(i)%16]; \
101 +       t2 = e0(a) + Maj(a, b, c);                              \
102 +       d += t1;                                                \
103 +       h = t1 + t2
105 +       for (i = 0; i < 16; i += 8) {
106 +               SHA512_0_15(i, a, b, c, d, e, f, g, h);
107 +               SHA512_0_15(i + 1, h, a, b, c, d, e, f, g);
108 +               SHA512_0_15(i + 2, g, h, a, b, c, d, e, f);
109 +               SHA512_0_15(i + 3, f, g, h, a, b, c, d, e);
110 +               SHA512_0_15(i + 4, e, f, g, h, a, b, c, d);
111 +               SHA512_0_15(i + 5, d, e, f, g, h, a, b, c);
112 +               SHA512_0_15(i + 6, c, d, e, f, g, h, a, b);
113 +               SHA512_0_15(i + 7, b, c, d, e, f, g, h, a);
114 +       }
115 +       for (i = 16; i < 80; i += 8) {
116 +               SHA512_16_79(i, a, b, c, d, e, f, g, h);
117 +               SHA512_16_79(i + 1, h, a, b, c, d, e, f, g);
118 +               SHA512_16_79(i + 2, g, h, a, b, c, d, e, f);
119 +               SHA512_16_79(i + 3, f, g, h, a, b, c, d, e);
120 +               SHA512_16_79(i + 4, e, f, g, h, a, b, c, d);
121 +               SHA512_16_79(i + 5, d, e, f, g, h, a, b, c);
122 +               SHA512_16_79(i + 6, c, d, e, f, g, h, a, b);
123 +               SHA512_16_79(i + 7, b, c, d, e, f, g, h, a);
124         }
125  
126         state[0] += a; state[1] += b; state[2] += c; state[3] += d;
127 -- 
128 1.7.7.4