Commit 5265eeb2b036835021591173ac64e624baaff55c

Authored by Jan Glauber
Committed by David S. Miller
1 parent ad5d27899f

[CRYPTO] sha: Add header file for SHA definitions

There are currently several SHA implementations that all define their own
initialization vectors and size values. Since this values are idential
move them to a header file under include/crypto.

Signed-off-by: Jan Glauber <jang@de.ibm.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 7 changed files with 116 additions and 109 deletions Side-by-side Diff

arch/s390/crypto/sha1_s390.c
... ... @@ -26,12 +26,10 @@
26 26 #include <linux/init.h>
27 27 #include <linux/module.h>
28 28 #include <linux/crypto.h>
  29 +#include <crypto/sha.h>
29 30  
30 31 #include "crypt_s390.h"
31 32  
32   -#define SHA1_DIGEST_SIZE 20
33   -#define SHA1_BLOCK_SIZE 64
34   -
35 33 struct s390_sha1_ctx {
36 34 u64 count; /* message length */
37 35 u32 state[5];
... ... @@ -42,11 +40,11 @@
42 40 {
43 41 struct s390_sha1_ctx *sctx = crypto_tfm_ctx(tfm);
44 42  
45   - sctx->state[0] = 0x67452301;
46   - sctx->state[1] = 0xEFCDAB89;
47   - sctx->state[2] = 0x98BADCFE;
48   - sctx->state[3] = 0x10325476;
49   - sctx->state[4] = 0xC3D2E1F0;
  43 + sctx->state[0] = SHA1_H0;
  44 + sctx->state[1] = SHA1_H1;
  45 + sctx->state[2] = SHA1_H2;
  46 + sctx->state[3] = SHA1_H3;
  47 + sctx->state[4] = SHA1_H4;
50 48 sctx->count = 0;
51 49 }
52 50  
arch/s390/crypto/sha256_s390.c
... ... @@ -19,12 +19,10 @@
19 19 #include <linux/init.h>
20 20 #include <linux/module.h>
21 21 #include <linux/crypto.h>
  22 +#include <crypto/sha.h>
22 23  
23 24 #include "crypt_s390.h"
24 25  
25   -#define SHA256_DIGEST_SIZE 32
26   -#define SHA256_BLOCK_SIZE 64
27   -
28 26 struct s390_sha256_ctx {
29 27 u64 count; /* message length */
30 28 u32 state[8];
... ... @@ -35,14 +33,14 @@
35 33 {
36 34 struct s390_sha256_ctx *sctx = crypto_tfm_ctx(tfm);
37 35  
38   - sctx->state[0] = 0x6a09e667;
39   - sctx->state[1] = 0xbb67ae85;
40   - sctx->state[2] = 0x3c6ef372;
41   - sctx->state[3] = 0xa54ff53a;
42   - sctx->state[4] = 0x510e527f;
43   - sctx->state[5] = 0x9b05688c;
44   - sctx->state[6] = 0x1f83d9ab;
45   - sctx->state[7] = 0x5be0cd19;
  36 + sctx->state[0] = SHA256_H0;
  37 + sctx->state[1] = SHA256_H1;
  38 + sctx->state[2] = SHA256_H2;
  39 + sctx->state[3] = SHA256_H3;
  40 + sctx->state[4] = SHA256_H4;
  41 + sctx->state[5] = SHA256_H5;
  42 + sctx->state[6] = SHA256_H6;
  43 + sctx->state[7] = SHA256_H7;
46 44 sctx->count = 0;
47 45 }
48 46  
crypto/sha1_generic.c
... ... @@ -22,12 +22,10 @@
22 22 #include <linux/crypto.h>
23 23 #include <linux/cryptohash.h>
24 24 #include <linux/types.h>
  25 +#include <crypto/sha.h>
25 26 #include <asm/scatterlist.h>
26 27 #include <asm/byteorder.h>
27 28  
28   -#define SHA1_DIGEST_SIZE 20
29   -#define SHA1_HMAC_BLOCK_SIZE 64
30   -
31 29 struct sha1_ctx {
32 30 u64 count;
33 31 u32 state[5];
... ... @@ -39,7 +37,7 @@
39 37 struct sha1_ctx *sctx = crypto_tfm_ctx(tfm);
40 38 static const struct sha1_ctx initstate = {
41 39 0,
42   - { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 },
  40 + { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
43 41 { 0, }
44 42 };
45 43  
... ... @@ -111,7 +109,7 @@
111 109 .cra_name = "sha1",
112 110 .cra_driver_name= "sha1-generic",
113 111 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
114   - .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
  112 + .cra_blocksize = SHA1_BLOCK_SIZE,
115 113 .cra_ctxsize = sizeof(struct sha1_ctx),
116 114 .cra_module = THIS_MODULE,
117 115 .cra_alignmask = 3,
crypto/sha256_generic.c
... ... @@ -21,12 +21,10 @@
21 21 #include <linux/mm.h>
22 22 #include <linux/crypto.h>
23 23 #include <linux/types.h>
  24 +#include <crypto/sha.h>
24 25 #include <asm/scatterlist.h>
25 26 #include <asm/byteorder.h>
26 27  
27   -#define SHA256_DIGEST_SIZE 32
28   -#define SHA256_HMAC_BLOCK_SIZE 64
29   -
30 28 struct sha256_ctx {
31 29 u32 count[2];
32 30 u32 state[8];
... ... @@ -48,15 +46,6 @@
48 46 #define s0(x) (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
49 47 #define s1(x) (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
50 48  
51   -#define H0 0x6a09e667
52   -#define H1 0xbb67ae85
53   -#define H2 0x3c6ef372
54   -#define H3 0xa54ff53a
55   -#define H4 0x510e527f
56   -#define H5 0x9b05688c
57   -#define H6 0x1f83d9ab
58   -#define H7 0x5be0cd19
59   -
60 49 static inline void LOAD_OP(int I, u32 *W, const u8 *input)
61 50 {
62 51 W[I] = __be32_to_cpu( ((__be32*)(input))[I] );
... ... @@ -233,14 +222,14 @@
233 222 static void sha256_init(struct crypto_tfm *tfm)
234 223 {
235 224 struct sha256_ctx *sctx = crypto_tfm_ctx(tfm);
236   - sctx->state[0] = H0;
237   - sctx->state[1] = H1;
238   - sctx->state[2] = H2;
239   - sctx->state[3] = H3;
240   - sctx->state[4] = H4;
241   - sctx->state[5] = H5;
242   - sctx->state[6] = H6;
243   - sctx->state[7] = H7;
  225 + sctx->state[0] = SHA256_H0;
  226 + sctx->state[1] = SHA256_H1;
  227 + sctx->state[2] = SHA256_H2;
  228 + sctx->state[3] = SHA256_H3;
  229 + sctx->state[4] = SHA256_H4;
  230 + sctx->state[5] = SHA256_H5;
  231 + sctx->state[6] = SHA256_H6;
  232 + sctx->state[7] = SHA256_H7;
244 233 sctx->count[0] = sctx->count[1] = 0;
245 234 }
246 235  
... ... @@ -311,7 +300,7 @@
311 300 .cra_name = "sha256",
312 301 .cra_driver_name= "sha256-generic",
313 302 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
314   - .cra_blocksize = SHA256_HMAC_BLOCK_SIZE,
  303 + .cra_blocksize = SHA256_BLOCK_SIZE,
315 304 .cra_ctxsize = sizeof(struct sha256_ctx),
316 305 .cra_module = THIS_MODULE,
317 306 .cra_alignmask = 3,
... ... @@ -13,20 +13,15 @@
13 13  
14 14 #include <linux/kernel.h>
15 15 #include <linux/module.h>
16   -
17 16 #include <linux/mm.h>
18 17 #include <linux/init.h>
19 18 #include <linux/crypto.h>
20 19 #include <linux/types.h>
  20 +#include <crypto/sha.h>
21 21  
22 22 #include <asm/scatterlist.h>
23 23 #include <asm/byteorder.h>
24 24  
25   -#define SHA384_DIGEST_SIZE 48
26   -#define SHA512_DIGEST_SIZE 64
27   -#define SHA384_HMAC_BLOCK_SIZE 128
28   -#define SHA512_HMAC_BLOCK_SIZE 128
29   -
30 25 struct sha512_ctx {
31 26 u64 state[8];
32 27 u32 count[4];
... ... @@ -84,26 +79,6 @@
84 79 #define s0(x) (RORu64(x, 1) ^ RORu64(x, 8) ^ (x >> 7))
85 80 #define s1(x) (RORu64(x,19) ^ RORu64(x,61) ^ (x >> 6))
86 81  
87   -/* H* initial state for SHA-512 */
88   -#define H0 0x6a09e667f3bcc908ULL
89   -#define H1 0xbb67ae8584caa73bULL
90   -#define H2 0x3c6ef372fe94f82bULL
91   -#define H3 0xa54ff53a5f1d36f1ULL
92   -#define H4 0x510e527fade682d1ULL
93   -#define H5 0x9b05688c2b3e6c1fULL
94   -#define H6 0x1f83d9abfb41bd6bULL
95   -#define H7 0x5be0cd19137e2179ULL
96   -
97   -/* H'* initial state for SHA-384 */
98   -#define HP0 0xcbbb9d5dc1059ed8ULL
99   -#define HP1 0x629a292a367cd507ULL
100   -#define HP2 0x9159015a3070dd17ULL
101   -#define HP3 0x152fecd8f70e5939ULL
102   -#define HP4 0x67332667ffc00b31ULL
103   -#define HP5 0x8eb44a8768581511ULL
104   -#define HP6 0xdb0c2e0d64f98fa7ULL
105   -#define HP7 0x47b5481dbefa4fa4ULL
106   -
107 82 static inline void LOAD_OP(int I, u64 *W, const u8 *input)
108 83 {
109 84 W[I] = __be64_to_cpu( ((__be64*)(input))[I] );
... ... @@ -164,14 +139,14 @@
164 139 sha512_init(struct crypto_tfm *tfm)
165 140 {
166 141 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
167   - sctx->state[0] = H0;
168   - sctx->state[1] = H1;
169   - sctx->state[2] = H2;
170   - sctx->state[3] = H3;
171   - sctx->state[4] = H4;
172   - sctx->state[5] = H5;
173   - sctx->state[6] = H6;
174   - sctx->state[7] = H7;
  142 + sctx->state[0] = SHA512_H0;
  143 + sctx->state[1] = SHA512_H1;
  144 + sctx->state[2] = SHA512_H2;
  145 + sctx->state[3] = SHA512_H3;
  146 + sctx->state[4] = SHA512_H4;
  147 + sctx->state[5] = SHA512_H5;
  148 + sctx->state[6] = SHA512_H6;
  149 + sctx->state[7] = SHA512_H7;
175 150 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
176 151 }
177 152  
... ... @@ -179,14 +154,14 @@
179 154 sha384_init(struct crypto_tfm *tfm)
180 155 {
181 156 struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
182   - sctx->state[0] = HP0;
183   - sctx->state[1] = HP1;
184   - sctx->state[2] = HP2;
185   - sctx->state[3] = HP3;
186   - sctx->state[4] = HP4;
187   - sctx->state[5] = HP5;
188   - sctx->state[6] = HP6;
189   - sctx->state[7] = HP7;
  157 + sctx->state[0] = SHA384_H0;
  158 + sctx->state[1] = SHA384_H1;
  159 + sctx->state[2] = SHA384_H2;
  160 + sctx->state[3] = SHA384_H3;
  161 + sctx->state[4] = SHA384_H4;
  162 + sctx->state[5] = SHA384_H5;
  163 + sctx->state[6] = SHA384_H6;
  164 + sctx->state[7] = SHA384_H7;
190 165 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
191 166 }
192 167  
... ... @@ -275,7 +250,7 @@
275 250 static struct crypto_alg sha512 = {
276 251 .cra_name = "sha512",
277 252 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
278   - .cra_blocksize = SHA512_HMAC_BLOCK_SIZE,
  253 + .cra_blocksize = SHA512_BLOCK_SIZE,
279 254 .cra_ctxsize = sizeof(struct sha512_ctx),
280 255 .cra_module = THIS_MODULE,
281 256 .cra_alignmask = 3,
... ... @@ -291,7 +266,7 @@
291 266 static struct crypto_alg sha384 = {
292 267 .cra_name = "sha384",
293 268 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
294   - .cra_blocksize = SHA384_HMAC_BLOCK_SIZE,
  269 + .cra_blocksize = SHA384_BLOCK_SIZE,
295 270 .cra_ctxsize = sizeof(struct sha512_ctx),
296 271 .cra_alignmask = 3,
297 272 .cra_module = THIS_MODULE,
drivers/crypto/padlock-sha.c
... ... @@ -13,6 +13,7 @@
13 13 */
14 14  
15 15 #include <crypto/algapi.h>
  16 +#include <crypto/sha.h>
16 17 #include <linux/err.h>
17 18 #include <linux/module.h>
18 19 #include <linux/init.h>
19 20  
... ... @@ -24,12 +25,7 @@
24 25 #include "padlock.h"
25 26  
26 27 #define SHA1_DEFAULT_FALLBACK "sha1-generic"
27   -#define SHA1_DIGEST_SIZE 20
28   -#define SHA1_HMAC_BLOCK_SIZE 64
29   -
30 28 #define SHA256_DEFAULT_FALLBACK "sha256-generic"
31   -#define SHA256_DIGEST_SIZE 32
32   -#define SHA256_HMAC_BLOCK_SIZE 64
33 29  
34 30 struct padlock_sha_ctx {
35 31 char *data;
... ... @@ -107,11 +103,11 @@
107 103 char buf[128+16];
108 104 char *result = NEAREST_ALIGNED(buf);
109 105  
110   - ((uint32_t *)result)[0] = 0x67452301;
111   - ((uint32_t *)result)[1] = 0xEFCDAB89;
112   - ((uint32_t *)result)[2] = 0x98BADCFE;
113   - ((uint32_t *)result)[3] = 0x10325476;
114   - ((uint32_t *)result)[4] = 0xC3D2E1F0;
  106 + ((uint32_t *)result)[0] = SHA1_H0;
  107 + ((uint32_t *)result)[1] = SHA1_H1;
  108 + ((uint32_t *)result)[2] = SHA1_H2;
  109 + ((uint32_t *)result)[3] = SHA1_H3;
  110 + ((uint32_t *)result)[4] = SHA1_H4;
115 111  
116 112 asm volatile (".byte 0xf3,0x0f,0xa6,0xc8" /* rep xsha1 */
117 113 : "+S"(in), "+D"(result)
... ... @@ -128,14 +124,14 @@
128 124 char buf[128+16];
129 125 char *result = NEAREST_ALIGNED(buf);
130 126  
131   - ((uint32_t *)result)[0] = 0x6A09E667;
132   - ((uint32_t *)result)[1] = 0xBB67AE85;
133   - ((uint32_t *)result)[2] = 0x3C6EF372;
134   - ((uint32_t *)result)[3] = 0xA54FF53A;
135   - ((uint32_t *)result)[4] = 0x510E527F;
136   - ((uint32_t *)result)[5] = 0x9B05688C;
137   - ((uint32_t *)result)[6] = 0x1F83D9AB;
138   - ((uint32_t *)result)[7] = 0x5BE0CD19;
  127 + ((uint32_t *)result)[0] = SHA256_H0;
  128 + ((uint32_t *)result)[1] = SHA256_H1;
  129 + ((uint32_t *)result)[2] = SHA256_H2;
  130 + ((uint32_t *)result)[3] = SHA256_H3;
  131 + ((uint32_t *)result)[4] = SHA256_H4;
  132 + ((uint32_t *)result)[5] = SHA256_H5;
  133 + ((uint32_t *)result)[6] = SHA256_H6;
  134 + ((uint32_t *)result)[7] = SHA256_H7;
139 135  
140 136 asm volatile (".byte 0xf3,0x0f,0xa6,0xd0" /* rep xsha256 */
141 137 : "+S"(in), "+D"(result)
... ... @@ -215,7 +211,7 @@
215 211 .cra_priority = PADLOCK_CRA_PRIORITY,
216 212 .cra_flags = CRYPTO_ALG_TYPE_DIGEST |
217 213 CRYPTO_ALG_NEED_FALLBACK,
218   - .cra_blocksize = SHA1_HMAC_BLOCK_SIZE,
  214 + .cra_blocksize = SHA1_BLOCK_SIZE,
219 215 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
220 216 .cra_module = THIS_MODULE,
221 217 .cra_list = LIST_HEAD_INIT(sha1_alg.cra_list),
... ... @@ -237,7 +233,7 @@
237 233 .cra_priority = PADLOCK_CRA_PRIORITY,
238 234 .cra_flags = CRYPTO_ALG_TYPE_DIGEST |
239 235 CRYPTO_ALG_NEED_FALLBACK,
240   - .cra_blocksize = SHA256_HMAC_BLOCK_SIZE,
  236 + .cra_blocksize = SHA256_BLOCK_SIZE,
241 237 .cra_ctxsize = sizeof(struct padlock_sha_ctx),
242 238 .cra_module = THIS_MODULE,
243 239 .cra_list = LIST_HEAD_INIT(sha256_alg.cra_list),
include/crypto/sha.h
  1 +/*
  2 + * Common values for SHA algorithms
  3 + */
  4 +
  5 +#ifndef _CRYPTO_SHA_H
  6 +#define _CRYPTO_SHA_H
  7 +
  8 +#define SHA1_DIGEST_SIZE 20
  9 +#define SHA1_BLOCK_SIZE 64
  10 +
  11 +#define SHA256_DIGEST_SIZE 32
  12 +#define SHA256_BLOCK_SIZE 64
  13 +
  14 +#define SHA384_DIGEST_SIZE 48
  15 +#define SHA384_BLOCK_SIZE 128
  16 +
  17 +#define SHA512_DIGEST_SIZE 64
  18 +#define SHA512_BLOCK_SIZE 128
  19 +
  20 +#define SHA1_H0 0x67452301UL
  21 +#define SHA1_H1 0xefcdab89UL
  22 +#define SHA1_H2 0x98badcfeUL
  23 +#define SHA1_H3 0x10325476UL
  24 +#define SHA1_H4 0xc3d2e1f0UL
  25 +
  26 +#define SHA256_H0 0x6a09e667UL
  27 +#define SHA256_H1 0xbb67ae85UL
  28 +#define SHA256_H2 0x3c6ef372UL
  29 +#define SHA256_H3 0xa54ff53aUL
  30 +#define SHA256_H4 0x510e527fUL
  31 +#define SHA256_H5 0x9b05688cUL
  32 +#define SHA256_H6 0x1f83d9abUL
  33 +#define SHA256_H7 0x5be0cd19UL
  34 +
  35 +#define SHA384_H0 0xcbbb9d5dc1059ed8ULL
  36 +#define SHA384_H1 0x629a292a367cd507ULL
  37 +#define SHA384_H2 0x9159015a3070dd17ULL
  38 +#define SHA384_H3 0x152fecd8f70e5939ULL
  39 +#define SHA384_H4 0x67332667ffc00b31ULL
  40 +#define SHA384_H5 0x8eb44a8768581511ULL
  41 +#define SHA384_H6 0xdb0c2e0d64f98fa7ULL
  42 +#define SHA384_H7 0x47b5481dbefa4fa4ULL
  43 +
  44 +#define SHA512_H0 0x6a09e667f3bcc908ULL
  45 +#define SHA512_H1 0xbb67ae8584caa73bULL
  46 +#define SHA512_H2 0x3c6ef372fe94f82bULL
  47 +#define SHA512_H3 0xa54ff53a5f1d36f1ULL
  48 +#define SHA512_H4 0x510e527fade682d1ULL
  49 +#define SHA512_H5 0x9b05688c2b3e6c1fULL
  50 +#define SHA512_H6 0x1f83d9abfb41bd6bULL
  51 +#define SHA512_H7 0x5be0cd19137e2179ULL
  52 +
  53 +#endif