| 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 | /* Hash one 64-byte block of data */ |
| 13 | static void blk_SHA1Block(struct fio_sha1_ctx *ctx, const unsigned int *data); |
| 14 | |
| 15 | void fio_sha1_init(struct fio_sha1_ctx *ctx) |
| 16 | { |
| 17 | ctx->size = 0; |
| 18 | |
| 19 | /* Initialize H with the magic constants (see FIPS180 for constants) |
| 20 | */ |
| 21 | ctx->H[0] = 0x67452301; |
| 22 | ctx->H[1] = 0xefcdab89; |
| 23 | ctx->H[2] = 0x98badcfe; |
| 24 | ctx->H[3] = 0x10325476; |
| 25 | ctx->H[4] = 0xc3d2e1f0; |
| 26 | } |
| 27 | |
| 28 | void fio_sha1_update(struct fio_sha1_ctx *ctx, const void *data, |
| 29 | unsigned long len) |
| 30 | { |
| 31 | int lenW = ctx->size & 63; |
| 32 | |
| 33 | ctx->size += len; |
| 34 | |
| 35 | /* Read the data into W and process blocks as they get full |
| 36 | */ |
| 37 | if (lenW) { |
| 38 | int left = 64 - lenW; |
| 39 | if (len < left) |
| 40 | left = len; |
| 41 | memcpy(lenW + (char *)ctx->W, data, left); |
| 42 | lenW = (lenW + left) & 63; |
| 43 | len -= left; |
| 44 | data += left; |
| 45 | if (lenW) |
| 46 | return; |
| 47 | blk_SHA1Block(ctx, ctx->W); |
| 48 | } |
| 49 | while (len >= 64) { |
| 50 | blk_SHA1Block(ctx, data); |
| 51 | data += 64; |
| 52 | len -= 64; |
| 53 | } |
| 54 | if (len) |
| 55 | memcpy(ctx->W, data, len); |
| 56 | } |
| 57 | |
| 58 | void fio_sha1_final(struct fio_sha1_ctx *ctx) |
| 59 | { |
| 60 | static const unsigned char pad[64] = { 0x80 }; |
| 61 | unsigned int padlen[2]; |
| 62 | int i; |
| 63 | |
| 64 | /* Pad with a binary 1 (ie 0x80), then zeroes, then length |
| 65 | */ |
| 66 | padlen[0] = htonl(ctx->size >> 29); |
| 67 | padlen[1] = htonl(ctx->size << 3); |
| 68 | |
| 69 | i = ctx->size & 63; |
| 70 | fio_sha1_update(ctx, pad, 1+ (63 & (55 - i))); |
| 71 | fio_sha1_update(ctx, padlen, 8); |
| 72 | } |
| 73 | |
| 74 | #if defined(__i386__) || defined(__x86_64__) |
| 75 | |
| 76 | #define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; }) |
| 77 | #define SHA_ROL(x,n) SHA_ASM("rol", x, n) |
| 78 | #define SHA_ROR(x,n) SHA_ASM("ror", x, n) |
| 79 | |
| 80 | #else |
| 81 | |
| 82 | #define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r))) |
| 83 | #define SHA_ROL(X,n) SHA_ROT(X,n,32-(n)) |
| 84 | #define SHA_ROR(X,n) SHA_ROT(X,32-(n),n) |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | /* This "rolls" over the 512-bit array */ |
| 89 | #define W(x) (array[(x)&15]) |
| 90 | #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val)) |
| 91 | |
| 92 | /* |
| 93 | * Where do we get the source from? The first 16 iterations get it from |
| 94 | * the input data, the next mix it from the 512-bit array. |
| 95 | */ |
| 96 | #define SHA_SRC(t) htonl(data[t]) |
| 97 | #define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1) |
| 98 | |
| 99 | #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \ |
| 100 | unsigned int TEMP = input(t); setW(t, TEMP); \ |
| 101 | E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \ |
| 102 | B = SHA_ROR(B, 2); } while (0) |
| 103 | |
| 104 | #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 ) |
| 105 | #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 ) |
| 106 | #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E ) |
| 107 | #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 ) |
| 108 | #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E ) |
| 109 | |
| 110 | static void blk_SHA1Block(struct fio_sha1_ctx *ctx, const unsigned int *data) |
| 111 | { |
| 112 | unsigned int A,B,C,D,E; |
| 113 | unsigned int array[16]; |
| 114 | |
| 115 | A = ctx->H[0]; |
| 116 | B = ctx->H[1]; |
| 117 | C = ctx->H[2]; |
| 118 | D = ctx->H[3]; |
| 119 | E = ctx->H[4]; |
| 120 | |
| 121 | /* Round 1 - iterations 0-16 take their input from 'data' */ |
| 122 | T_0_15( 0, A, B, C, D, E); |
| 123 | T_0_15( 1, E, A, B, C, D); |
| 124 | T_0_15( 2, D, E, A, B, C); |
| 125 | T_0_15( 3, C, D, E, A, B); |
| 126 | T_0_15( 4, B, C, D, E, A); |
| 127 | T_0_15( 5, A, B, C, D, E); |
| 128 | T_0_15( 6, E, A, B, C, D); |
| 129 | T_0_15( 7, D, E, A, B, C); |
| 130 | T_0_15( 8, C, D, E, A, B); |
| 131 | T_0_15( 9, B, C, D, E, A); |
| 132 | T_0_15(10, A, B, C, D, E); |
| 133 | T_0_15(11, E, A, B, C, D); |
| 134 | T_0_15(12, D, E, A, B, C); |
| 135 | T_0_15(13, C, D, E, A, B); |
| 136 | T_0_15(14, B, C, D, E, A); |
| 137 | T_0_15(15, A, B, C, D, E); |
| 138 | |
| 139 | /* Round 1 - tail. Input from 512-bit mixing array */ |
| 140 | T_16_19(16, E, A, B, C, D); |
| 141 | T_16_19(17, D, E, A, B, C); |
| 142 | T_16_19(18, C, D, E, A, B); |
| 143 | T_16_19(19, B, C, D, E, A); |
| 144 | |
| 145 | /* Round 2 */ |
| 146 | T_20_39(20, A, B, C, D, E); |
| 147 | T_20_39(21, E, A, B, C, D); |
| 148 | T_20_39(22, D, E, A, B, C); |
| 149 | T_20_39(23, C, D, E, A, B); |
| 150 | T_20_39(24, B, C, D, E, A); |
| 151 | T_20_39(25, A, B, C, D, E); |
| 152 | T_20_39(26, E, A, B, C, D); |
| 153 | T_20_39(27, D, E, A, B, C); |
| 154 | T_20_39(28, C, D, E, A, B); |
| 155 | T_20_39(29, B, C, D, E, A); |
| 156 | T_20_39(30, A, B, C, D, E); |
| 157 | T_20_39(31, E, A, B, C, D); |
| 158 | T_20_39(32, D, E, A, B, C); |
| 159 | T_20_39(33, C, D, E, A, B); |
| 160 | T_20_39(34, B, C, D, E, A); |
| 161 | T_20_39(35, A, B, C, D, E); |
| 162 | T_20_39(36, E, A, B, C, D); |
| 163 | T_20_39(37, D, E, A, B, C); |
| 164 | T_20_39(38, C, D, E, A, B); |
| 165 | T_20_39(39, B, C, D, E, A); |
| 166 | |
| 167 | /* Round 3 */ |
| 168 | T_40_59(40, A, B, C, D, E); |
| 169 | T_40_59(41, E, A, B, C, D); |
| 170 | T_40_59(42, D, E, A, B, C); |
| 171 | T_40_59(43, C, D, E, A, B); |
| 172 | T_40_59(44, B, C, D, E, A); |
| 173 | T_40_59(45, A, B, C, D, E); |
| 174 | T_40_59(46, E, A, B, C, D); |
| 175 | T_40_59(47, D, E, A, B, C); |
| 176 | T_40_59(48, C, D, E, A, B); |
| 177 | T_40_59(49, B, C, D, E, A); |
| 178 | T_40_59(50, A, B, C, D, E); |
| 179 | T_40_59(51, E, A, B, C, D); |
| 180 | T_40_59(52, D, E, A, B, C); |
| 181 | T_40_59(53, C, D, E, A, B); |
| 182 | T_40_59(54, B, C, D, E, A); |
| 183 | T_40_59(55, A, B, C, D, E); |
| 184 | T_40_59(56, E, A, B, C, D); |
| 185 | T_40_59(57, D, E, A, B, C); |
| 186 | T_40_59(58, C, D, E, A, B); |
| 187 | T_40_59(59, B, C, D, E, A); |
| 188 | |
| 189 | /* Round 4 */ |
| 190 | T_60_79(60, A, B, C, D, E); |
| 191 | T_60_79(61, E, A, B, C, D); |
| 192 | T_60_79(62, D, E, A, B, C); |
| 193 | T_60_79(63, C, D, E, A, B); |
| 194 | T_60_79(64, B, C, D, E, A); |
| 195 | T_60_79(65, A, B, C, D, E); |
| 196 | T_60_79(66, E, A, B, C, D); |
| 197 | T_60_79(67, D, E, A, B, C); |
| 198 | T_60_79(68, C, D, E, A, B); |
| 199 | T_60_79(69, B, C, D, E, A); |
| 200 | T_60_79(70, A, B, C, D, E); |
| 201 | T_60_79(71, E, A, B, C, D); |
| 202 | T_60_79(72, D, E, A, B, C); |
| 203 | T_60_79(73, C, D, E, A, B); |
| 204 | T_60_79(74, B, C, D, E, A); |
| 205 | T_60_79(75, A, B, C, D, E); |
| 206 | T_60_79(76, E, A, B, C, D); |
| 207 | T_60_79(77, D, E, A, B, C); |
| 208 | T_60_79(78, C, D, E, A, B); |
| 209 | T_60_79(79, B, C, D, E, A); |
| 210 | |
| 211 | ctx->H[0] += A; |
| 212 | ctx->H[1] += B; |
| 213 | ctx->H[2] += C; |
| 214 | ctx->H[3] += D; |
| 215 | ctx->H[4] += E; |
| 216 | } |