PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sha1.c File Reference
#include "postgres.h"
#include <sys/param.h>
#include "sha1.h"
Include dependency graph for sha1.c:

Go to the source code of this file.

Macros

#define K(t)   _K[(t) / 20]
 
#define F0(b, c, d)   (((b) & (c)) | ((~(b)) & (d)))
 
#define F1(b, c, d)   (((b) ^ (c)) ^ (d))
 
#define F2(b, c, d)   (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
 
#define F3(b, c, d)   (((b) ^ (c)) ^ (d))
 
#define S(n, x)   (((x) << (n)) | ((x) >> (32 - (n))))
 
#define H(n)   (ctxt->h.b32[(n)])
 
#define COUNT   (ctxt->count)
 
#define BCOUNT   (ctxt->c.b64[0] / 8)
 
#define W(n)   (ctxt->m.b32[(n)])
 
#define PUTBYTE(x)
 
#define PUTPAD(x)
 

Functions

static void sha1_step (struct sha1_ctxt *)
 
void sha1_init (struct sha1_ctxt *ctxt)
 
void sha1_pad (struct sha1_ctxt *ctxt)
 
void sha1_loop (struct sha1_ctxt *ctxt, const uint8 *input0, size_t len)
 
void sha1_result (struct sha1_ctxt *ctxt, uint8 *digest0)
 

Variables

static uint32 _K [] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}
 

Macro Definition Documentation

#define BCOUNT   (ctxt->c.b64[0] / 8)

Definition at line 59 of file sha1.c.

#define COUNT   (ctxt->count)

Definition at line 58 of file sha1.c.

Referenced by main(), sha1_loop(), and sha1_pad().

#define F0 (   b,
  c,
 
)    (((b) & (c)) | ((~(b)) & (d)))

Definition at line 50 of file sha1.c.

Referenced by sha1_step().

#define F1 (   b,
  c,
 
)    (((b) ^ (c)) ^ (d))

Definition at line 51 of file sha1.c.

Referenced by sha1_step().

#define F2 (   b,
  c,
 
)    (((b) & (c)) | ((b) & (d)) | ((c) & (d)))

Definition at line 52 of file sha1.c.

Referenced by sha1_step().

#define F3 (   b,
  c,
 
)    (((b) ^ (c)) ^ (d))

Definition at line 53 of file sha1.c.

Referenced by sha1_step().

#define H (   n)    (ctxt->h.b32[(n)])

Definition at line 57 of file sha1.c.

Referenced by sha1_init(), and sha1_step().

#define K (   t)    _K[(t) / 20]

Definition at line 48 of file sha1.c.

Referenced by BlockSampler_Next(), des_setkey(), and sha1_step().

#define PUTBYTE (   x)
Value:
do { \
ctxt->m.b8[(COUNT % 64)] = (x); \
COUNT %= 64; \
ctxt->c.b64[0] += 8; \
if (COUNT % 64 == 0) \
sha1_step(ctxt); \
} while (0)
#define COUNT
Definition: sha1.c:58
static void sha1_step(struct sha1_ctxt *)
Definition: sha1.c:84

Definition at line 62 of file sha1.c.

#define PUTPAD (   x)
Value:
do { \
ctxt->m.b8[(COUNT % 64)] = (x); \
COUNT %= 64; \
if (COUNT % 64 == 0) \
sha1_step(ctxt); \
} while (0)
#define COUNT
Definition: sha1.c:58
static void sha1_step(struct sha1_ctxt *)
Definition: sha1.c:84

Definition at line 72 of file sha1.c.

Referenced by sha1_pad().

#define S (   n,
 
)    (((x) << (n)) | ((x) >> (32 - (n))))

Definition at line 55 of file sha1.c.

Referenced by reservoir_get_next_S(), sha1_step(), and text_substring().

#define W (   n)    (ctxt->m.b32[(n)])

Definition at line 60 of file sha1.c.

Referenced by reservoir_get_next_S(), and sha1_step().

Function Documentation

void sha1_init ( struct sha1_ctxt ctxt)

Definition at line 229 of file sha1.c.

References H.

230 {
231  memset(ctxt, 0, sizeof(struct sha1_ctxt));
232  H(0) = 0x67452301;
233  H(1) = 0xefcdab89;
234  H(2) = 0x98badcfe;
235  H(3) = 0x10325476;
236  H(4) = 0xc3d2e1f0;
237 }
Definition: sha1.h:41
#define H(n)
Definition: sha1.c:57
void sha1_loop ( struct sha1_ctxt ctxt,
const uint8 input0,
size_t  len 
)

Definition at line 283 of file sha1.c.

References sha1_ctxt::b64, sha1_ctxt::b8, sha1_ctxt::c, COUNT, sha1_ctxt::m, memmove, and sha1_step().

284 {
285  const uint8 *input;
286  size_t gaplen;
287  size_t gapstart;
288  size_t off;
289  size_t copysiz;
290 
291  input = (const uint8 *) input0;
292  off = 0;
293 
294  while (off < len)
295  {
296  gapstart = COUNT % 64;
297  gaplen = 64 - gapstart;
298 
299  copysiz = (gaplen < len - off) ? gaplen : len - off;
300  memmove(&ctxt->m.b8[gapstart], &input[off], copysiz);
301  COUNT += copysiz;
302  COUNT %= 64;
303  ctxt->c.b64[0] += copysiz * 8;
304  if (COUNT % 64 == 0)
305  sha1_step(ctxt);
306  off += copysiz;
307  }
308 }
#define COUNT
Definition: sha1.c:58
unsigned char uint8
Definition: c.h:266
#define memmove(d, s, c)
Definition: c.h:1058
uint8 b8[20]
Definition: sha1.h:45
static void sha1_step(struct sha1_ctxt *)
Definition: sha1.c:84
union sha1_ctxt::@16 c
uint64 b64[1]
Definition: sha1.h:51
union sha1_ctxt::@17 m
void sha1_pad ( struct sha1_ctxt ctxt)

Definition at line 240 of file sha1.c.

References sha1_ctxt::b8, sha1_ctxt::c, COUNT, sha1_ctxt::m, PUTPAD, and sha1_step().

Referenced by sha1_result().

241 {
242  size_t padlen; /* pad length in bytes */
243  size_t padstart;
244 
245  PUTPAD(0x80);
246 
247  padstart = COUNT % 64;
248  padlen = 64 - padstart;
249  if (padlen < 8)
250  {
251  memset(&ctxt->m.b8[padstart], 0, padlen);
252  COUNT += padlen;
253  COUNT %= 64;
254  sha1_step(ctxt);
255  padstart = COUNT % 64; /* should be 0 */
256  padlen = 64 - padstart; /* should be 64 */
257  }
258  memset(&ctxt->m.b8[padstart], 0, padlen - 8);
259  COUNT += (padlen - 8);
260  COUNT %= 64;
261 #ifdef WORDS_BIGENDIAN
262  PUTPAD(ctxt->c.b8[0]);
263  PUTPAD(ctxt->c.b8[1]);
264  PUTPAD(ctxt->c.b8[2]);
265  PUTPAD(ctxt->c.b8[3]);
266  PUTPAD(ctxt->c.b8[4]);
267  PUTPAD(ctxt->c.b8[5]);
268  PUTPAD(ctxt->c.b8[6]);
269  PUTPAD(ctxt->c.b8[7]);
270 #else
271  PUTPAD(ctxt->c.b8[7]);
272  PUTPAD(ctxt->c.b8[6]);
273  PUTPAD(ctxt->c.b8[5]);
274  PUTPAD(ctxt->c.b8[4]);
275  PUTPAD(ctxt->c.b8[3]);
276  PUTPAD(ctxt->c.b8[2]);
277  PUTPAD(ctxt->c.b8[1]);
278  PUTPAD(ctxt->c.b8[0]);
279 #endif
280 }
#define COUNT
Definition: sha1.c:58
#define PUTPAD(x)
Definition: sha1.c:72
uint8 b8[20]
Definition: sha1.h:45
static void sha1_step(struct sha1_ctxt *)
Definition: sha1.c:84
union sha1_ctxt::@16 c
union sha1_ctxt::@17 m
void sha1_result ( struct sha1_ctxt ctxt,
uint8 digest0 
)

Definition at line 311 of file sha1.c.

References sha1_ctxt::b8, sha1_ctxt::h, memmove, and sha1_pad().

312 {
313  uint8 *digest;
314 
315  digest = (uint8 *) digest0;
316  sha1_pad(ctxt);
317 #ifdef WORDS_BIGENDIAN
318  memmove(digest, &ctxt->h.b8[0], 20);
319 #else
320  digest[0] = ctxt->h.b8[3];
321  digest[1] = ctxt->h.b8[2];
322  digest[2] = ctxt->h.b8[1];
323  digest[3] = ctxt->h.b8[0];
324  digest[4] = ctxt->h.b8[7];
325  digest[5] = ctxt->h.b8[6];
326  digest[6] = ctxt->h.b8[5];
327  digest[7] = ctxt->h.b8[4];
328  digest[8] = ctxt->h.b8[11];
329  digest[9] = ctxt->h.b8[10];
330  digest[10] = ctxt->h.b8[9];
331  digest[11] = ctxt->h.b8[8];
332  digest[12] = ctxt->h.b8[15];
333  digest[13] = ctxt->h.b8[14];
334  digest[14] = ctxt->h.b8[13];
335  digest[15] = ctxt->h.b8[12];
336  digest[16] = ctxt->h.b8[19];
337  digest[17] = ctxt->h.b8[18];
338  digest[18] = ctxt->h.b8[17];
339  digest[19] = ctxt->h.b8[16];
340 #endif
341 }
union sha1_ctxt::@15 h
unsigned char uint8
Definition: c.h:266
#define memmove(d, s, c)
Definition: c.h:1058
uint8 b8[20]
Definition: sha1.h:45
void sha1_pad(struct sha1_ctxt *ctxt)
Definition: sha1.c:240
static void sha1_step ( struct sha1_ctxt ctxt)
static

Definition at line 84 of file sha1.c.

References sha1_ctxt::b8, F0, F1, F2, F3, H, K, sha1_ctxt::m, memmove, S, and W.

Referenced by sha1_loop(), and sha1_pad().

85 {
86  uint32 a,
87  b,
88  c,
89  d,
90  e;
91  size_t t,
92  s;
93  uint32 tmp;
94 
95 #ifndef WORDS_BIGENDIAN
96  struct sha1_ctxt tctxt;
97 
98  memmove(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
99  ctxt->m.b8[0] = tctxt.m.b8[3];
100  ctxt->m.b8[1] = tctxt.m.b8[2];
101  ctxt->m.b8[2] = tctxt.m.b8[1];
102  ctxt->m.b8[3] = tctxt.m.b8[0];
103  ctxt->m.b8[4] = tctxt.m.b8[7];
104  ctxt->m.b8[5] = tctxt.m.b8[6];
105  ctxt->m.b8[6] = tctxt.m.b8[5];
106  ctxt->m.b8[7] = tctxt.m.b8[4];
107  ctxt->m.b8[8] = tctxt.m.b8[11];
108  ctxt->m.b8[9] = tctxt.m.b8[10];
109  ctxt->m.b8[10] = tctxt.m.b8[9];
110  ctxt->m.b8[11] = tctxt.m.b8[8];
111  ctxt->m.b8[12] = tctxt.m.b8[15];
112  ctxt->m.b8[13] = tctxt.m.b8[14];
113  ctxt->m.b8[14] = tctxt.m.b8[13];
114  ctxt->m.b8[15] = tctxt.m.b8[12];
115  ctxt->m.b8[16] = tctxt.m.b8[19];
116  ctxt->m.b8[17] = tctxt.m.b8[18];
117  ctxt->m.b8[18] = tctxt.m.b8[17];
118  ctxt->m.b8[19] = tctxt.m.b8[16];
119  ctxt->m.b8[20] = tctxt.m.b8[23];
120  ctxt->m.b8[21] = tctxt.m.b8[22];
121  ctxt->m.b8[22] = tctxt.m.b8[21];
122  ctxt->m.b8[23] = tctxt.m.b8[20];
123  ctxt->m.b8[24] = tctxt.m.b8[27];
124  ctxt->m.b8[25] = tctxt.m.b8[26];
125  ctxt->m.b8[26] = tctxt.m.b8[25];
126  ctxt->m.b8[27] = tctxt.m.b8[24];
127  ctxt->m.b8[28] = tctxt.m.b8[31];
128  ctxt->m.b8[29] = tctxt.m.b8[30];
129  ctxt->m.b8[30] = tctxt.m.b8[29];
130  ctxt->m.b8[31] = tctxt.m.b8[28];
131  ctxt->m.b8[32] = tctxt.m.b8[35];
132  ctxt->m.b8[33] = tctxt.m.b8[34];
133  ctxt->m.b8[34] = tctxt.m.b8[33];
134  ctxt->m.b8[35] = tctxt.m.b8[32];
135  ctxt->m.b8[36] = tctxt.m.b8[39];
136  ctxt->m.b8[37] = tctxt.m.b8[38];
137  ctxt->m.b8[38] = tctxt.m.b8[37];
138  ctxt->m.b8[39] = tctxt.m.b8[36];
139  ctxt->m.b8[40] = tctxt.m.b8[43];
140  ctxt->m.b8[41] = tctxt.m.b8[42];
141  ctxt->m.b8[42] = tctxt.m.b8[41];
142  ctxt->m.b8[43] = tctxt.m.b8[40];
143  ctxt->m.b8[44] = tctxt.m.b8[47];
144  ctxt->m.b8[45] = tctxt.m.b8[46];
145  ctxt->m.b8[46] = tctxt.m.b8[45];
146  ctxt->m.b8[47] = tctxt.m.b8[44];
147  ctxt->m.b8[48] = tctxt.m.b8[51];
148  ctxt->m.b8[49] = tctxt.m.b8[50];
149  ctxt->m.b8[50] = tctxt.m.b8[49];
150  ctxt->m.b8[51] = tctxt.m.b8[48];
151  ctxt->m.b8[52] = tctxt.m.b8[55];
152  ctxt->m.b8[53] = tctxt.m.b8[54];
153  ctxt->m.b8[54] = tctxt.m.b8[53];
154  ctxt->m.b8[55] = tctxt.m.b8[52];
155  ctxt->m.b8[56] = tctxt.m.b8[59];
156  ctxt->m.b8[57] = tctxt.m.b8[58];
157  ctxt->m.b8[58] = tctxt.m.b8[57];
158  ctxt->m.b8[59] = tctxt.m.b8[56];
159  ctxt->m.b8[60] = tctxt.m.b8[63];
160  ctxt->m.b8[61] = tctxt.m.b8[62];
161  ctxt->m.b8[62] = tctxt.m.b8[61];
162  ctxt->m.b8[63] = tctxt.m.b8[60];
163 #endif
164 
165  a = H(0);
166  b = H(1);
167  c = H(2);
168  d = H(3);
169  e = H(4);
170 
171  for (t = 0; t < 20; t++)
172  {
173  s = t & 0x0f;
174  if (t >= 16)
175  W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
176  tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
177  e = d;
178  d = c;
179  c = S(30, b);
180  b = a;
181  a = tmp;
182  }
183  for (t = 20; t < 40; t++)
184  {
185  s = t & 0x0f;
186  W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
187  tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
188  e = d;
189  d = c;
190  c = S(30, b);
191  b = a;
192  a = tmp;
193  }
194  for (t = 40; t < 60; t++)
195  {
196  s = t & 0x0f;
197  W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
198  tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
199  e = d;
200  d = c;
201  c = S(30, b);
202  b = a;
203  a = tmp;
204  }
205  for (t = 60; t < 80; t++)
206  {
207  s = t & 0x0f;
208  W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
209  tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
210  e = d;
211  d = c;
212  c = S(30, b);
213  b = a;
214  a = tmp;
215  }
216 
217  H(0) = H(0) + a;
218  H(1) = H(1) + b;
219  H(2) = H(2) + c;
220  H(3) = H(3) + d;
221  H(4) = H(4) + e;
222 
223  memset(&ctxt->m.b8[0], 0, 64);
224 }
Definition: sha1.h:41
#define W(n)
Definition: sha1.c:60
char * c
#define memmove(d, s, c)
Definition: c.h:1058
#define F3(b, c, d)
Definition: sha1.c:53
#define F2(b, c, d)
Definition: sha1.c:52
unsigned int uint32
Definition: c.h:268
uint8 b8[20]
Definition: sha1.h:45
#define S(n, x)
Definition: sha1.c:55
#define K(t)
Definition: sha1.c:48
#define F1(b, c, d)
Definition: sha1.c:51
e
Definition: preproc-init.c:82
#define F0(b, c, d)
Definition: sha1.c:50
union sha1_ctxt::@17 m
#define H(n)
Definition: sha1.c:57

Variable Documentation

uint32 _K[] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}
static

Definition at line 46 of file sha1.c.