1/* 2 * Based on the Mozilla SHA1 (see mozilla-sha1/sha1.c), 3 * optimized to do word accesses rather than byte accesses, 4 * and to avoid unnecessary copies into the context array. 5 */ 6 7#include <string.h> 8#include <arpa/inet.h> 9 10#include "sha1.h" 11 12#if defined(__i386__) || defined(__x86_64__) 13 14/* 15 * Force usage of rol or ror by selecting the one with the smaller constant. 16 * It _can_ generate slightly smaller code (a constant of 1 is special), but 17 * perhaps more importantly it's possibly faster on any uarch that does a 18 * rotate with a loop. 19 */ 20 21#define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; }) 22#define SHA_ROL(x,n) SHA_ASM("rol", x, n) 23#define SHA_ROR(x,n) SHA_ASM("ror", x, n) 24 25#else 26 27#define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r))) 28#define SHA_ROL(X,n) SHA_ROT(X,n,32-(n)) 29#define SHA_ROR(X,n) SHA_ROT(X,32-(n),n) 30 31#endif 32 33/* 34 * If you have 32 registers or more, the compiler can (and should) 35 * try to change the array[] accesses into registers. However, on 36 * machines with less than ~25 registers, that won't really work, 37 * and at least gcc will make an unholy mess of it. 38 * 39 * So to avoid that mess which just slows things down, we force 40 * the stores to memory to actually happen (we might be better off 41 * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as 42 * suggested by Artur Skawina - that will also make gcc unable to 43 * try to do the silly "optimize away loads" part because it won't 44 * see what the value will be). 45 * 46 * Ben Herrenschmidt reports that on PPC, the C version comes close 47 * to the optimized asm with this (ie on PPC you don't want that 48 * 'volatile', since there are lots of registers). 49 * 50 * On ARM we get the best code generation by forcing a full memory barrier 51 * between each SHA_ROUND, otherwise gcc happily get wild with spilling and 52 * the stack frame size simply explode and performance goes down the drain. 53 */ 54 55#if defined(__i386__) || defined(__x86_64__) 56 #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val)) 57#elif defined(__arm__) 58 #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0) 59#else 60 #define setW(x, val) (W(x) = (val)) 61#endif 62 63/* This "rolls" over the 512-bit array */ 64#define W(x) (array[(x)&15]) 65 66/* 67 * Where do we get the source from? The first 16 iterations get it from 68 * the input data, the next mix it from the 512-bit array. 69 */ 70#define SHA_SRC(t) htonl(data[t]) 71#define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1) 72 73#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \ 74 unsigned int TEMP = input(t); setW(t, TEMP); \ 75 E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \ 76 B = SHA_ROR(B, 2); } while (0) 77 78#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 ) 79#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 ) 80#define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E ) 81#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 ) 82#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E ) 83 84static void blk_SHA1_Block(blk_SHA_CTX *ctx, const unsigned int *data) 85{ 86 unsigned int A,B,C,D,E; 87 unsigned int array[16]; 88 89 A = ctx->H[0]; 90 B = ctx->H[1]; 91 C = ctx->H[2]; 92 D = ctx->H[3]; 93 E = ctx->H[4]; 94 95 /* Round 1 - iterations 0-16 take their input from 'data' */ 96 T_0_15( 0, A, B, C, D, E); 97 T_0_15( 1, E, A, B, C, D); 98 T_0_15( 2, D, E, A, B, C); 99 T_0_15( 3, C, D, E, A, B); 100 T_0_15( 4, B, C, D, E, A); 101 T_0_15( 5, A, B, C, D, E); 102 T_0_15( 6, E, A, B, C, D); 103 T_0_15( 7, D, E, A, B, C); 104 T_0_15( 8, C, D, E, A, B); 105 T_0_15( 9, B, C, D, E, A); 106 T_0_15(10, A, B, C, D, E); 107 T_0_15(11, E, A, B, C, D); 108 T_0_15(12, D, E, A, B, C); 109 T_0_15(13, C, D, E, A, B); 110 T_0_15(14, B, C, D, E, A); 111 T_0_15(15, A, B, C, D, E); 112 113 /* Round 1 - tail. Input from 512-bit mixing array */ 114 T_16_19(16, E, A, B, C, D); 115 T_16_19(17, D, E, A, B, C); 116 T_16_19(18, C, D, E, A, B); 117 T_16_19(19, B, C, D, E, A); 118 119 /* Round 2 */ 120 T_20_39(20, A, B, C, D, E); 121 T_20_39(21, E, A, B, C, D); 122 T_20_39(22, D, E, A, B, C); 123 T_20_39(23, C, D, E, A, B); 124 T_20_39(24, B, C, D, E, A); 125 T_20_39(25, A, B, C, D, E); 126 T_20_39(26, E, A, B, C, D); 127 T_20_39(27, D, E, A, B, C); 128 T_20_39(28, C, D, E, A, B); 129 T_20_39(29, B, C, D, E, A); 130 T_20_39(30, A, B, C, D, E); 131 T_20_39(31, E, A, B, C, D); 132 T_20_39(32, D, E, A, B, C); 133 T_20_39(33, C, D, E, A, B); 134 T_20_39(34, B, C, D, E, A); 135 T_20_39(35, A, B, C, D, E); 136 T_20_39(36, E, A, B, C, D); 137 T_20_39(37, D, E, A, B, C); 138 T_20_39(38, C, D, E, A, B); 139 T_20_39(39, B, C, D, E, A); 140 141 /* Round 3 */ 142 T_40_59(40, A, B, C, D, E); 143 T_40_59(41, E, A, B, C, D); 144 T_40_59(42, D, E, A, B, C); 145 T_40_59(43, C, D, E, A, B); 146 T_40_59(44, B, C, D, E, A); 147 T_40_59(45, A, B, C, D, E); 148 T_40_59(46, E, A, B, C, D); 149 T_40_59(47, D, E, A, B, C); 150 T_40_59(48, C, D, E, A, B); 151 T_40_59(49, B, C, D, E, A); 152 T_40_59(50, A, B, C, D, E); 153 T_40_59(51, E, A, B, C, D); 154 T_40_59(52, D, E, A, B, C); 155 T_40_59(53, C, D, E, A, B); 156 T_40_59(54, B, C, D, E, A); 157 T_40_59(55, A, B, C, D, E); 158 T_40_59(56, E, A, B, C, D); 159 T_40_59(57, D, E, A, B, C); 160 T_40_59(58, C, D, E, A, B); 161 T_40_59(59, B, C, D, E, A); 162 163 /* Round 4 */ 164 T_60_79(60, A, B, C, D, E); 165 T_60_79(61, E, A, B, C, D); 166 T_60_79(62, D, E, A, B, C); 167 T_60_79(63, C, D, E, A, B); 168 T_60_79(64, B, C, D, E, A); 169 T_60_79(65, A, B, C, D, E); 170 T_60_79(66, E, A, B, C, D); 171 T_60_79(67, D, E, A, B, C); 172 T_60_79(68, C, D, E, A, B); 173 T_60_79(69, B, C, D, E, A); 174 T_60_79(70, A, B, C, D, E); 175 T_60_79(71, E, A, B, C, D); 176 T_60_79(72, D, E, A, B, C); 177 T_60_79(73, C, D, E, A, B); 178 T_60_79(74, B, C, D, E, A); 179 T_60_79(75, A, B, C, D, E); 180 T_60_79(76, E, A, B, C, D); 181 T_60_79(77, D, E, A, B, C); 182 T_60_79(78, C, D, E, A, B); 183 T_60_79(79, B, C, D, E, A); 184 185 ctx->H[0] += A; 186 ctx->H[1] += B; 187 ctx->H[2] += C; 188 ctx->H[3] += D; 189 ctx->H[4] += E; 190} 191 192void blk_SHA1_Init(blk_SHA_CTX *ctx) 193{ 194 ctx->size = 0; 195 196 /* Initialize H with the magic constants (see FIPS180 for constants) */ 197 ctx->H[0] = 0x67452301; 198 ctx->H[1] = 0xefcdab89; 199 ctx->H[2] = 0x98badcfe; 200 ctx->H[3] = 0x10325476; 201 ctx->H[4] = 0xc3d2e1f0; 202} 203 204void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len) 205{ 206 int lenW = ctx->size & 63; 207 208 ctx->size += len; 209 210 /* Read the data into W and process blocks as they get full */ 211 if (lenW) { 212 int left = 64 - lenW; 213 if (len < left) 214 left = len; 215 memcpy(lenW + (char *)ctx->W, data, left); 216 lenW = (lenW + left) & 63; 217 len -= left; 218 data += left; 219 if (lenW) 220 return; 221 blk_SHA1_Block(ctx, ctx->W); 222 } 223 while (len >= 64) { 224 blk_SHA1_Block(ctx, data); 225 data += 64; 226 len -= 64; 227 } 228 if (len) 229 memcpy(ctx->W, data, len); 230} 231 232void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx) 233{ 234 static const unsigned char pad[64] = { 0x80 }; 235 unsigned int padlen[2]; 236 int i; 237 238 /* Pad with a binary 1 (ie 0x80), then zeroes, then length */ 239 padlen[0] = htonl(ctx->size >> 29); 240 padlen[1] = htonl(ctx->size << 3); 241 242 i = ctx->size & 63; 243 blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i))); 244 blk_SHA1_Update(ctx, padlen, 8); 245 246 /* Output hash */ 247 for (i = 0; i < 5; i++) 248 ((unsigned int *)hashout)[i] = htonl(ctx->H[i]); 249}