~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/algorithm/sha1.cc

  • Committer: Gustaf Thorslund
  • Date: 2010-11-01 04:25:34 UTC
  • mto: (1911.1.1 build) (1900.1.3 build)
  • mto: This revision was merged to the branch mainline in revision 1901.
  • Revision ID: gustaf@thorslund.org-20101101042534-ovpgd6jzka1th3w0
Replaced macros with functions/templates. Part of blueprint:
  https://blueprints.edge.launchpad.net/drizzle/+spec/replace-macro-functions/
File done:
  drizzled/algorithm/sha1.cc

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 * By Steve Reid <steve@edmweb.com>
13
13
 * 100% Public Domain
14
14
 *
 
15
 * Gustaf Thorslund <gustaf@thorslund.org> have been here turning
 
16
 * #define-d functions into inline static functions. Others have been
 
17
 * here before me too.
 
18
 *
15
19
 * Test Vectors (from FIPS PUB 180-1)
16
20
 * "abc"
17
21
 *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
27
31
 
28
32
namespace drizzled
29
33
{
30
 
 
31
 
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
32
 
 
 
34
typedef union {
 
35
  uint8_t c[64];
 
36
  uint32_t l[16];
 
37
} CHAR64LONG16;
 
38
  
 
39
inline static uint32_t rol(const uint32_t &value, int bits)
 
40
{
 
41
  return (value << bits) | (value >> (32 - bits));
 
42
}
 
43
  
33
44
/* Solaris + gcc don't always define this. */
34
45
#ifndef BYTE_ORDER
35
46
# define LITTLE_ENDIAN 1234
45
56
 * blk0() and blk() perform the initial expand.
46
57
 * I got the idea of expanding during the round function from SSLeay
47
58
 */
48
 
#if BYTE_ORDER == LITTLE_ENDIAN
49
 
# define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
50
 
    |(rol(block->l[i],8)&0x00FF00FF))
51
 
#else
52
 
# define blk0(i) block->l[i]
53
 
#endif
54
 
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
55
 
    ^block->l[(i+2)&15]^block->l[i&15],1))
 
59
inline static uint32_t blk0(CHAR64LONG16 *block, const int i)
 
60
{
 
61
  if (BYTE_ORDER == LITTLE_ENDIAN)
 
62
    return (block->l[i]= ((rol(block->l[i], 24) & 0xFF00FF00)
 
63
                          | (rol(block->l[i], 8) & 0x00FF00FF)));
 
64
  else
 
65
    return block->l[i];
 
66
}
 
67
  
 
68
inline static uint32_t blk(CHAR64LONG16 *block, int i)
 
69
{
 
70
  return (block->l[i&15]= rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] 
 
71
                              ^ block->l[(i + 2) & 15] ^ block->l[i & 15],
 
72
                              1));
 
73
}
56
74
 
57
75
/*
58
76
 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
59
77
 */
60
 
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
61
 
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
62
 
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
63
 
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
64
 
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
 
78
inline static void R0(CHAR64LONG16 *block,
 
79
                      const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const int i)
 
80
{
 
81
  z+= ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5);
 
82
  w= rol(w,30);
 
83
}
 
84
  
 
85
inline static void R1(CHAR64LONG16 *block,
 
86
                      const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const int i)
 
87
{
 
88
  z+= ((w & (x ^ y)) ^ y) + blk(block, i) + 0x5A827999 + rol(v, 5);
 
89
  w= rol(w,30);
 
90
}
 
91
 
 
92
inline static void R2(CHAR64LONG16 *block,
 
93
                      const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const int i)
 
94
{
 
95
  z+= (w ^ x ^ y) + blk(block, i) + 0x6ED9EBA1 + rol(v, 5);
 
96
  w= rol(w, 30);
 
97
}
 
98
 
 
99
inline static void R3(CHAR64LONG16 *block,
 
100
                      const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const int i)
 
101
{
 
102
  z+= (((w | x) & y) | (w & x)) + blk(block, i) + 0x8F1BBCDC + rol(v, 5);
 
103
  w= rol(w, 30);
 
104
}
 
105
 
 
106
inline static void R4(CHAR64LONG16 *block,
 
107
                      const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const int i)
 
108
{
 
109
  z+= (w ^ x ^ y) + blk(block, i) + 0xCA62C1D6 + rol(v, 5);
 
110
  w=rol(w, 30);
 
111
}
65
112
 
66
113
/*
67
114
 * Hash a single 512-bit block. This is the core of the algorithm.
69
116
void
70
117
SHA1Transform(uint32_t state[5], const uint8_t buffer[SHA1_BLOCK_LENGTH])
71
118
{
72
 
        uint32_t a, b, c, d, e;
73
 
        typedef union {
74
 
                uint8_t c[64];
75
 
                uint32_t l[16];
76
 
        } CHAR64LONG16;
77
 
        CHAR64LONG16 realBlock;
78
 
        CHAR64LONG16 *block= &realBlock;
79
 
 
80
 
        (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
81
 
 
82
 
        /* Copy context->state[] to working vars */
83
 
        a = state[0];
84
 
        b = state[1];
85
 
        c = state[2];
86
 
        d = state[3];
87
 
        e = state[4];
88
 
 
89
 
        /* 4 rounds of 20 operations each. Loop unrolled. */
90
 
        R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
91
 
        R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
92
 
        R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
93
 
        R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
94
 
        R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
95
 
        R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
96
 
        R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
97
 
        R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
98
 
        R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
99
 
        R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
100
 
        R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
101
 
        R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
102
 
        R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
103
 
        R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
104
 
        R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
105
 
        R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
106
 
        R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
107
 
        R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
108
 
        R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
109
 
        R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
110
 
 
111
 
        /* Add the working vars back into context.state[] */
112
 
        state[0] += a;
113
 
        state[1] += b;
114
 
        state[2] += c;
115
 
        state[3] += d;
116
 
        state[4] += e;
117
 
 
118
 
        /* Wipe variables */
119
 
        a = b = c = d = e = 0;
 
119
  uint32_t a, b, c, d, e;
 
120
  CHAR64LONG16 realBlock;
 
121
  CHAR64LONG16 *block= &realBlock;
 
122
 
 
123
  (void)memcpy(block, buffer, SHA1_BLOCK_LENGTH);
 
124
 
 
125
  /* Copy context->state[] to working vars */
 
126
  a= state[0];
 
127
  b= state[1];
 
128
  c= state[2];
 
129
  d= state[3];
 
130
  e= state[4];
 
131
 
 
132
  /* 4 rounds of 20 operations each. Loop unrolled. */
 
133
  R0(block,a,b,c,d,e, 0); R0(block,e,a,b,c,d, 1); R0(block,d,e,a,b,c, 2); R0(block,c,d,e,a,b, 3);
 
134
  R0(block,b,c,d,e,a, 4); R0(block,a,b,c,d,e, 5); R0(block,e,a,b,c,d, 6); R0(block,d,e,a,b,c, 7);
 
135
  R0(block,c,d,e,a,b, 8); R0(block,b,c,d,e,a, 9); R0(block,a,b,c,d,e,10); R0(block,e,a,b,c,d,11);
 
136
  R0(block,d,e,a,b,c,12); R0(block,c,d,e,a,b,13); R0(block,b,c,d,e,a,14); R0(block,a,b,c,d,e,15);
 
137
  R1(block,e,a,b,c,d,16); R1(block,d,e,a,b,c,17); R1(block,c,d,e,a,b,18); R1(block,b,c,d,e,a,19);
 
138
  R2(block,a,b,c,d,e,20); R2(block,e,a,b,c,d,21); R2(block,d,e,a,b,c,22); R2(block,c,d,e,a,b,23);
 
139
  R2(block,b,c,d,e,a,24); R2(block,a,b,c,d,e,25); R2(block,e,a,b,c,d,26); R2(block,d,e,a,b,c,27);
 
140
  R2(block,c,d,e,a,b,28); R2(block,b,c,d,e,a,29); R2(block,a,b,c,d,e,30); R2(block,e,a,b,c,d,31);
 
141
  R2(block,d,e,a,b,c,32); R2(block,c,d,e,a,b,33); R2(block,b,c,d,e,a,34); R2(block,a,b,c,d,e,35);
 
142
  R2(block,e,a,b,c,d,36); R2(block,d,e,a,b,c,37); R2(block,c,d,e,a,b,38); R2(block,b,c,d,e,a,39);
 
143
  R3(block,a,b,c,d,e,40); R3(block,e,a,b,c,d,41); R3(block,d,e,a,b,c,42); R3(block,c,d,e,a,b,43);
 
144
  R3(block,b,c,d,e,a,44); R3(block,a,b,c,d,e,45); R3(block,e,a,b,c,d,46); R3(block,d,e,a,b,c,47);
 
145
  R3(block,c,d,e,a,b,48); R3(block,b,c,d,e,a,49); R3(block,a,b,c,d,e,50); R3(block,e,a,b,c,d,51);
 
146
  R3(block,d,e,a,b,c,52); R3(block,c,d,e,a,b,53); R3(block,b,c,d,e,a,54); R3(block,a,b,c,d,e,55);
 
147
  R3(block,e,a,b,c,d,56); R3(block,d,e,a,b,c,57); R3(block,c,d,e,a,b,58); R3(block,b,c,d,e,a,59);
 
148
  R4(block,a,b,c,d,e,60); R4(block,e,a,b,c,d,61); R4(block,d,e,a,b,c,62); R4(block,c,d,e,a,b,63);
 
149
  R4(block,b,c,d,e,a,64); R4(block,a,b,c,d,e,65); R4(block,e,a,b,c,d,66); R4(block,d,e,a,b,c,67);
 
150
  R4(block,c,d,e,a,b,68); R4(block,b,c,d,e,a,69); R4(block,a,b,c,d,e,70); R4(block,e,a,b,c,d,71);
 
151
  R4(block,d,e,a,b,c,72); R4(block,c,d,e,a,b,73); R4(block,b,c,d,e,a,74); R4(block,a,b,c,d,e,75);
 
152
  R4(block,e,a,b,c,d,76); R4(block,d,e,a,b,c,77); R4(block,c,d,e,a,b,78); R4(block,b,c,d,e,a,79);
 
153
 
 
154
  /* Add the working vars back into context.state[] */
 
155
  state[0]+= a;
 
156
  state[1]+= b;
 
157
  state[2]+= c;
 
158
  state[3]+= d;
 
159
  state[4]+= e;
120
160
}
121
161
 
122
162
 
126
166
void
127
167
SHA1Init(SHA1_CTX *context)
128
168
{
129
 
 
130
 
        /* SHA1 initialization constants */
131
 
        context->count = 0;
132
 
        context->state[0] = 0x67452301;
133
 
        context->state[1] = 0xEFCDAB89;
134
 
        context->state[2] = 0x98BADCFE;
135
 
        context->state[3] = 0x10325476;
136
 
        context->state[4] = 0xC3D2E1F0;
 
169
  /* SHA1 initialization constants */
 
170
  context->count = 0;
 
171
  context->state[0] = 0x67452301;
 
172
  context->state[1] = 0xEFCDAB89;
 
173
  context->state[2] = 0x98BADCFE;
 
174
  context->state[3] = 0x10325476;
 
175
  context->state[4] = 0xC3D2E1F0;
137
176
}
138
177
 
139
178
 
143
182
void
144
183
SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len)
145
184
{
146
 
        size_t i, j;
147
 
 
148
 
        j = (size_t)((context->count >> 3) & 63);
149
 
        context->count += (len << 3);
150
 
        if ((j + len) > 63) {
151
 
                (void)memcpy(&context->buffer[j], data, (i = 64-j));
152
 
                SHA1Transform(context->state, context->buffer);
153
 
                for ( ; i + 63 < len; i += 64)
154
 
                        SHA1Transform(context->state, (uint8_t *)&data[i]);
155
 
                j = 0;
156
 
        } else {
157
 
                i = 0;
158
 
        }
159
 
        (void)memcpy(&context->buffer[j], &data[i], len - i);
 
185
  size_t i, j;
 
186
  
 
187
  j = (size_t)((context->count >> 3) & 63);
 
188
  context->count+= (len << 3);
 
189
  if ((j + len) > 63) {
 
190
    (void)memcpy(&context->buffer[j], data, (i= 64 - j));
 
191
    SHA1Transform(context->state, context->buffer);
 
192
    for ( ; i + 63 < len; i += 64)
 
193
      SHA1Transform(context->state, (uint8_t *)&data[i]);
 
194
    j= 0;
 
195
  } 
 
196
  else 
 
197
  {
 
198
    i = 0;
 
199
  }
 
200
  (void)memcpy(&context->buffer[j], &data[i], len - i);
160
201
}
161
202
 
162
203
 
166
207
void
167
208
SHA1Pad(SHA1_CTX *context)
168
209
{
169
 
        uint8_t finalcount[8];
170
 
        u_int i;
171
 
 
172
 
        for (i = 0; i < 8; i++) {
173
 
                finalcount[i] = (uint8_t)((context->count >>
174
 
                    ((7 - (i & 7)) * 8)) & 255);        /* Endian independent */
175
 
        }
176
 
        SHA1Update(context, (uint8_t *)"\200", 1);
177
 
        while ((context->count & 504) != 448)
178
 
                SHA1Update(context, (uint8_t *)"\0", 1);
179
 
        SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
 
210
  uint8_t finalcount[8];
 
211
  u_int i;
 
212
  
 
213
  for (i = 0; i < 8; i++) 
 
214
  {
 
215
    finalcount[i] = (uint8_t)((context->count >>
 
216
                               ((7 - (i & 7)) * 8)) & 255);     /* Endian independent */
 
217
  }
 
218
  SHA1Update(context, (uint8_t *)"\200", 1);
 
219
  while ((context->count & 504) != 448)
 
220
    SHA1Update(context, (uint8_t *)"\0", 1);
 
221
  SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
180
222
}
181
223
 
182
224
void
183
225
SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
184
226
{
185
 
        u_int i;
186
 
 
187
 
        SHA1Pad(context);
188
 
        if (digest) {
189
 
                for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
190
 
                        digest[i] = (uint8_t)
191
 
                           ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
192
 
                }
193
 
                memset(context, 0, sizeof(*context));
194
 
        }
 
227
  u_int i;
 
228
  
 
229
  SHA1Pad(context);
 
230
  if (digest) 
 
231
  {
 
232
    for (i = 0; i < SHA1_DIGEST_LENGTH; i++) 
 
233
    {
 
234
      digest[i] = (uint8_t)
 
235
        ((context->state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
 
236
    }
 
237
    memset(context, 0, sizeof(*context));
 
238
  }
195
239
}
196
240
 
197
241
} /* namespace drizzled */