Back to home page

LXR

 
 

    


0001 /*
0002  * SHA1 routine optimized to do word accesses rather than byte accesses,
0003  * and to avoid unnecessary copies into the context array.
0004  *
0005  * This was based on the git SHA1 implementation.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/export.h>
0010 #include <linux/bitops.h>
0011 #include <linux/cryptohash.h>
0012 #include <asm/unaligned.h>
0013 
0014 /*
0015  * If you have 32 registers or more, the compiler can (and should)
0016  * try to change the array[] accesses into registers. However, on
0017  * machines with less than ~25 registers, that won't really work,
0018  * and at least gcc will make an unholy mess of it.
0019  *
0020  * So to avoid that mess which just slows things down, we force
0021  * the stores to memory to actually happen (we might be better off
0022  * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
0023  * suggested by Artur Skawina - that will also make gcc unable to
0024  * try to do the silly "optimize away loads" part because it won't
0025  * see what the value will be).
0026  *
0027  * Ben Herrenschmidt reports that on PPC, the C version comes close
0028  * to the optimized asm with this (ie on PPC you don't want that
0029  * 'volatile', since there are lots of registers).
0030  *
0031  * On ARM we get the best code generation by forcing a full memory barrier
0032  * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
0033  * the stack frame size simply explode and performance goes down the drain.
0034  */
0035 
0036 #ifdef CONFIG_X86
0037   #define setW(x, val) (*(volatile __u32 *)&W(x) = (val))
0038 #elif defined(CONFIG_ARM)
0039   #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
0040 #else
0041   #define setW(x, val) (W(x) = (val))
0042 #endif
0043 
0044 /* This "rolls" over the 512-bit array */
0045 #define W(x) (array[(x)&15])
0046 
0047 /*
0048  * Where do we get the source from? The first 16 iterations get it from
0049  * the input data, the next mix it from the 512-bit array.
0050  */
0051 #define SHA_SRC(t) get_unaligned_be32((__u32 *)data + t)
0052 #define SHA_MIX(t) rol32(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
0053 
0054 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
0055     __u32 TEMP = input(t); setW(t, TEMP); \
0056     E += TEMP + rol32(A,5) + (fn) + (constant); \
0057     B = ror32(B, 2); } while (0)
0058 
0059 #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 )
0060 #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 )
0061 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
0062 #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 )
0063 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) ,  0xca62c1d6, A, B, C, D, E )
0064 
0065 /**
0066  * sha_transform - single block SHA1 transform
0067  *
0068  * @digest: 160 bit digest to update
0069  * @data:   512 bits of data to hash
0070  * @array:  16 words of workspace (see note)
0071  *
0072  * This function generates a SHA1 digest for a single 512-bit block.
0073  * Be warned, it does not handle padding and message digest, do not
0074  * confuse it with the full FIPS 180-1 digest algorithm for variable
0075  * length messages.
0076  *
0077  * Note: If the hash is security sensitive, the caller should be sure
0078  * to clear the workspace. This is left to the caller to avoid
0079  * unnecessary clears between chained hashing operations.
0080  */
0081 void sha_transform(__u32 *digest, const char *data, __u32 *array)
0082 {
0083     __u32 A, B, C, D, E;
0084 
0085     A = digest[0];
0086     B = digest[1];
0087     C = digest[2];
0088     D = digest[3];
0089     E = digest[4];
0090 
0091     /* Round 1 - iterations 0-16 take their input from 'data' */
0092     T_0_15( 0, A, B, C, D, E);
0093     T_0_15( 1, E, A, B, C, D);
0094     T_0_15( 2, D, E, A, B, C);
0095     T_0_15( 3, C, D, E, A, B);
0096     T_0_15( 4, B, C, D, E, A);
0097     T_0_15( 5, A, B, C, D, E);
0098     T_0_15( 6, E, A, B, C, D);
0099     T_0_15( 7, D, E, A, B, C);
0100     T_0_15( 8, C, D, E, A, B);
0101     T_0_15( 9, B, C, D, E, A);
0102     T_0_15(10, A, B, C, D, E);
0103     T_0_15(11, E, A, B, C, D);
0104     T_0_15(12, D, E, A, B, C);
0105     T_0_15(13, C, D, E, A, B);
0106     T_0_15(14, B, C, D, E, A);
0107     T_0_15(15, A, B, C, D, E);
0108 
0109     /* Round 1 - tail. Input from 512-bit mixing array */
0110     T_16_19(16, E, A, B, C, D);
0111     T_16_19(17, D, E, A, B, C);
0112     T_16_19(18, C, D, E, A, B);
0113     T_16_19(19, B, C, D, E, A);
0114 
0115     /* Round 2 */
0116     T_20_39(20, A, B, C, D, E);
0117     T_20_39(21, E, A, B, C, D);
0118     T_20_39(22, D, E, A, B, C);
0119     T_20_39(23, C, D, E, A, B);
0120     T_20_39(24, B, C, D, E, A);
0121     T_20_39(25, A, B, C, D, E);
0122     T_20_39(26, E, A, B, C, D);
0123     T_20_39(27, D, E, A, B, C);
0124     T_20_39(28, C, D, E, A, B);
0125     T_20_39(29, B, C, D, E, A);
0126     T_20_39(30, A, B, C, D, E);
0127     T_20_39(31, E, A, B, C, D);
0128     T_20_39(32, D, E, A, B, C);
0129     T_20_39(33, C, D, E, A, B);
0130     T_20_39(34, B, C, D, E, A);
0131     T_20_39(35, A, B, C, D, E);
0132     T_20_39(36, E, A, B, C, D);
0133     T_20_39(37, D, E, A, B, C);
0134     T_20_39(38, C, D, E, A, B);
0135     T_20_39(39, B, C, D, E, A);
0136 
0137     /* Round 3 */
0138     T_40_59(40, A, B, C, D, E);
0139     T_40_59(41, E, A, B, C, D);
0140     T_40_59(42, D, E, A, B, C);
0141     T_40_59(43, C, D, E, A, B);
0142     T_40_59(44, B, C, D, E, A);
0143     T_40_59(45, A, B, C, D, E);
0144     T_40_59(46, E, A, B, C, D);
0145     T_40_59(47, D, E, A, B, C);
0146     T_40_59(48, C, D, E, A, B);
0147     T_40_59(49, B, C, D, E, A);
0148     T_40_59(50, A, B, C, D, E);
0149     T_40_59(51, E, A, B, C, D);
0150     T_40_59(52, D, E, A, B, C);
0151     T_40_59(53, C, D, E, A, B);
0152     T_40_59(54, B, C, D, E, A);
0153     T_40_59(55, A, B, C, D, E);
0154     T_40_59(56, E, A, B, C, D);
0155     T_40_59(57, D, E, A, B, C);
0156     T_40_59(58, C, D, E, A, B);
0157     T_40_59(59, B, C, D, E, A);
0158 
0159     /* Round 4 */
0160     T_60_79(60, A, B, C, D, E);
0161     T_60_79(61, E, A, B, C, D);
0162     T_60_79(62, D, E, A, B, C);
0163     T_60_79(63, C, D, E, A, B);
0164     T_60_79(64, B, C, D, E, A);
0165     T_60_79(65, A, B, C, D, E);
0166     T_60_79(66, E, A, B, C, D);
0167     T_60_79(67, D, E, A, B, C);
0168     T_60_79(68, C, D, E, A, B);
0169     T_60_79(69, B, C, D, E, A);
0170     T_60_79(70, A, B, C, D, E);
0171     T_60_79(71, E, A, B, C, D);
0172     T_60_79(72, D, E, A, B, C);
0173     T_60_79(73, C, D, E, A, B);
0174     T_60_79(74, B, C, D, E, A);
0175     T_60_79(75, A, B, C, D, E);
0176     T_60_79(76, E, A, B, C, D);
0177     T_60_79(77, D, E, A, B, C);
0178     T_60_79(78, C, D, E, A, B);
0179     T_60_79(79, B, C, D, E, A);
0180 
0181     digest[0] += A;
0182     digest[1] += B;
0183     digest[2] += C;
0184     digest[3] += D;
0185     digest[4] += E;
0186 }
0187 EXPORT_SYMBOL(sha_transform);
0188 
0189 /**
0190  * sha_init - initialize the vectors for a SHA1 digest
0191  * @buf: vector to initialize
0192  */
0193 void sha_init(__u32 *buf)
0194 {
0195     buf[0] = 0x67452301;
0196     buf[1] = 0xefcdab89;
0197     buf[2] = 0x98badcfe;
0198     buf[3] = 0x10325476;
0199     buf[4] = 0xc3d2e1f0;
0200 }
0201 EXPORT_SYMBOL(sha_init);