Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * SHA1 routine optimized to do word accesses rather than byte accesses,
0004  * and to avoid unnecessary copies into the context array.
0005  *
0006  * This was based on the git SHA1 implementation.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/export.h>
0011 #include <linux/module.h>
0012 #include <linux/bitops.h>
0013 #include <linux/string.h>
0014 #include <crypto/sha1.h>
0015 #include <asm/unaligned.h>
0016 
0017 /*
0018  * If you have 32 registers or more, the compiler can (and should)
0019  * try to change the array[] accesses into registers. However, on
0020  * machines with less than ~25 registers, that won't really work,
0021  * and at least gcc will make an unholy mess of it.
0022  *
0023  * So to avoid that mess which just slows things down, we force
0024  * the stores to memory to actually happen (we might be better off
0025  * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
0026  * suggested by Artur Skawina - that will also make gcc unable to
0027  * try to do the silly "optimize away loads" part because it won't
0028  * see what the value will be).
0029  *
0030  * Ben Herrenschmidt reports that on PPC, the C version comes close
0031  * to the optimized asm with this (ie on PPC you don't want that
0032  * 'volatile', since there are lots of registers).
0033  *
0034  * On ARM we get the best code generation by forcing a full memory barrier
0035  * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
0036  * the stack frame size simply explode and performance goes down the drain.
0037  */
0038 
0039 #ifdef CONFIG_X86
0040   #define setW(x, val) (*(volatile __u32 *)&W(x) = (val))
0041 #elif defined(CONFIG_ARM)
0042   #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
0043 #else
0044   #define setW(x, val) (W(x) = (val))
0045 #endif
0046 
0047 /* This "rolls" over the 512-bit array */
0048 #define W(x) (array[(x)&15])
0049 
0050 /*
0051  * Where do we get the source from? The first 16 iterations get it from
0052  * the input data, the next mix it from the 512-bit array.
0053  */
0054 #define SHA_SRC(t) get_unaligned_be32((__u32 *)data + t)
0055 #define SHA_MIX(t) rol32(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
0056 
0057 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
0058     __u32 TEMP = input(t); setW(t, TEMP); \
0059     E += TEMP + rol32(A,5) + (fn) + (constant); \
0060     B = ror32(B, 2); \
0061     TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0)
0062 
0063 #define T_0_15(t, A, B, C, D, E)  SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
0064 #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
0065 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
0066 #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
0067 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) ,  0xca62c1d6, A, B, C, D, E )
0068 
0069 /**
0070  * sha1_transform - single block SHA1 transform (deprecated)
0071  *
0072  * @digest: 160 bit digest to update
0073  * @data:   512 bits of data to hash
0074  * @array:  16 words of workspace (see note)
0075  *
0076  * This function executes SHA-1's internal compression function.  It updates the
0077  * 160-bit internal state (@digest) with a single 512-bit data block (@data).
0078  *
0079  * Don't use this function.  SHA-1 is no longer considered secure.  And even if
0080  * you do have to use SHA-1, this isn't the correct way to hash something with
0081  * SHA-1 as this doesn't handle padding and finalization.
0082  *
0083  * Note: If the hash is security sensitive, the caller should be sure
0084  * to clear the workspace. This is left to the caller to avoid
0085  * unnecessary clears between chained hashing operations.
0086  */
0087 void sha1_transform(__u32 *digest, const char *data, __u32 *array)
0088 {
0089     __u32 A, B, C, D, E;
0090     unsigned int i = 0;
0091 
0092     A = digest[0];
0093     B = digest[1];
0094     C = digest[2];
0095     D = digest[3];
0096     E = digest[4];
0097 
0098     /* Round 1 - iterations 0-16 take their input from 'data' */
0099     for (; i < 16; ++i)
0100         T_0_15(i, A, B, C, D, E);
0101 
0102     /* Round 1 - tail. Input from 512-bit mixing array */
0103     for (; i < 20; ++i)
0104         T_16_19(i, A, B, C, D, E);
0105 
0106     /* Round 2 */
0107     for (; i < 40; ++i)
0108         T_20_39(i, A, B, C, D, E);
0109 
0110     /* Round 3 */
0111     for (; i < 60; ++i)
0112         T_40_59(i, A, B, C, D, E);
0113 
0114     /* Round 4 */
0115     for (; i < 80; ++i)
0116         T_60_79(i, A, B, C, D, E);
0117 
0118     digest[0] += A;
0119     digest[1] += B;
0120     digest[2] += C;
0121     digest[3] += D;
0122     digest[4] += E;
0123 }
0124 EXPORT_SYMBOL(sha1_transform);
0125 
0126 /**
0127  * sha1_init - initialize the vectors for a SHA1 digest
0128  * @buf: vector to initialize
0129  */
0130 void sha1_init(__u32 *buf)
0131 {
0132     buf[0] = 0x67452301;
0133     buf[1] = 0xefcdab89;
0134     buf[2] = 0x98badcfe;
0135     buf[3] = 0x10325476;
0136     buf[4] = 0xc3d2e1f0;
0137 }
0138 EXPORT_SYMBOL(sha1_init);
0139 
0140 MODULE_LICENSE("GPL");