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