PostgreSQL Source Code  git master
md5.c File Reference
#include "postgres.h"
#include "md5_int.h"
Include dependency graph for md5.c:

Go to the source code of this file.

Macros

#define SHIFT(X, s)   (((X) << (s)) | ((X) >> (32 - (s))))
 
#define F(X, Y, Z)   (((X) & (Y)) | ((~X) & (Z)))
 
#define G(X, Y, Z)   (((X) & (Z)) | ((Y) & (~Z)))
 
#define H(X, Y, Z)   ((X) ^ (Y) ^ (Z))
 
#define I(X, Y, Z)   ((Y) ^ ((X) | (~Z)))
 
#define ROUND1(a, b, c, d, k, s, i)
 
#define ROUND2(a, b, c, d, k, s, i)
 
#define ROUND3(a, b, c, d, k, s, i)
 
#define ROUND4(a, b, c, d, k, s, i)
 
#define Sa   7
 
#define Sb   12
 
#define Sc   17
 
#define Sd   22
 
#define Se   5
 
#define Sf   9
 
#define Sg   14
 
#define Sh   20
 
#define Si   4
 
#define Sj   11
 
#define Sk   16
 
#define Sl   23
 
#define Sm   6
 
#define Sn   10
 
#define So   15
 
#define Sp   21
 
#define MD5_A0   0x67452301
 
#define MD5_B0   0xefcdab89
 
#define MD5_C0   0x98badcfe
 
#define MD5_D0   0x10325476
 

Functions

static void md5_calc (const uint8 *b64, pg_md5_ctx *ctx)
 
static void md5_pad (pg_md5_ctx *ctx)
 
static void md5_result (uint8 *digest, pg_md5_ctx *ctx)
 
void pg_md5_init (pg_md5_ctx *ctx)
 
void pg_md5_update (pg_md5_ctx *ctx, const uint8 *data, size_t len)
 
void pg_md5_final (pg_md5_ctx *ctx, uint8 *dest)
 

Variables

static const uint32 T [65]
 
static const uint8 md5_paddat [MD5_BUFLEN]
 

Macro Definition Documentation

◆ F

#define F (   X,
  Y,
 
)    (((X) & (Y)) | ((~X) & (Z)))

Definition at line 60 of file md5.c.

◆ G

#define G (   X,
  Y,
 
)    (((X) & (Z)) | ((Y) & (~Z)))

Definition at line 61 of file md5.c.

◆ H

#define H (   X,
  Y,
 
)    ((X) ^ (Y) ^ (Z))

Definition at line 62 of file md5.c.

◆ I

#define I (   X,
  Y,
 
)    ((Y) ^ ((X) | (~Z)))

Definition at line 63 of file md5.c.

◆ MD5_A0

#define MD5_A0   0x67452301

Definition at line 113 of file md5.c.

◆ MD5_B0

#define MD5_B0   0xefcdab89

Definition at line 114 of file md5.c.

◆ MD5_C0

#define MD5_C0   0x98badcfe

Definition at line 115 of file md5.c.

◆ MD5_D0

#define MD5_D0   0x10325476

Definition at line 116 of file md5.c.

◆ ROUND1

#define ROUND1 (   a,
  b,
  c,
  d,
  k,
  s,
  i 
)
Value:
do { \
(a) = (a) + F((b), (c), (d)) + X[(k)] + T[(i)]; \
(a) = SHIFT((a), (s)); \
(a) = (b) + (a); \
} while (0)
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int i
Definition: isn.c:73
#define SHIFT(X, s)
Definition: md5.c:58
static const uint32 T[65]
Definition: md5.c:119
#define F(X, Y, Z)
Definition: md5.c:60
char * c

Definition at line 65 of file md5.c.

◆ ROUND2

#define ROUND2 (   a,
  b,
  c,
  d,
  k,
  s,
  i 
)
Value:
do { \
(a) = (a) + G((b), (c), (d)) + X[(k)] + T[(i)]; \
(a) = SHIFT((a), (s)); \
(a) = (b) + (a); \
} while (0)
#define G(X, Y, Z)
Definition: md5.c:61

Definition at line 72 of file md5.c.

◆ ROUND3

#define ROUND3 (   a,
  b,
  c,
  d,
  k,
  s,
  i 
)
Value:
do { \
(a) = (a) + H((b), (c), (d)) + X[(k)] + T[(i)]; \
(a) = SHIFT((a), (s)); \
(a) = (b) + (a); \
} while (0)
#define H(X, Y, Z)
Definition: md5.c:62

Definition at line 79 of file md5.c.

◆ ROUND4

#define ROUND4 (   a,
  b,
  c,
  d,
  k,
  s,
  i 
)
Value:
do { \
(a) = (a) + I((b), (c), (d)) + X[(k)] + T[(i)]; \
(a) = SHIFT((a), (s)); \
(a) = (b) + (a); \
} while (0)
#define I(X, Y, Z)
Definition: md5.c:63

Definition at line 86 of file md5.c.

◆ Sa

#define Sa   7

Definition at line 93 of file md5.c.

◆ Sb

#define Sb   12

Definition at line 94 of file md5.c.

◆ Sc

#define Sc   17

Definition at line 95 of file md5.c.

◆ Sd

#define Sd   22

Definition at line 96 of file md5.c.

◆ Se

#define Se   5

Definition at line 98 of file md5.c.

◆ Sf

#define Sf   9

Definition at line 99 of file md5.c.

◆ Sg

#define Sg   14

Definition at line 100 of file md5.c.

◆ Sh

#define Sh   20

Definition at line 101 of file md5.c.

◆ SHIFT

#define SHIFT (   X,
 
)    (((X) << (s)) | ((X) >> (32 - (s))))

Definition at line 58 of file md5.c.

◆ Si

#define Si   4

Definition at line 103 of file md5.c.

◆ Sj

#define Sj   11

Definition at line 104 of file md5.c.

◆ Sk

#define Sk   16

Definition at line 105 of file md5.c.

◆ Sl

#define Sl   23

Definition at line 106 of file md5.c.

◆ Sm

#define Sm   6

Definition at line 108 of file md5.c.

◆ Sn

#define Sn   10

Definition at line 109 of file md5.c.

◆ So

#define So   15

Definition at line 110 of file md5.c.

◆ Sp

#define Sp   21

Definition at line 111 of file md5.c.

Function Documentation

◆ md5_calc()

static void md5_calc ( const uint8 b64,
pg_md5_ctx ctx 
)
static

Definition at line 158 of file md5.c.

159 {
160  uint32 A = ctx->md5_sta;
161  uint32 B = ctx->md5_stb;
162  uint32 C = ctx->md5_stc;
163  uint32 D = ctx->md5_std;
164 
165 #ifndef WORDS_BIGENDIAN
166  const uint32 *X = (const uint32 *) b64;
167 #else
168  /* 4 byte words */
169  /* what a brute force but fast! */
170  uint8 *y = (uint8 *) X;
171 
172  y[0] = b64[3];
173  y[1] = b64[2];
174  y[2] = b64[1];
175  y[3] = b64[0];
176  y[4] = b64[7];
177  y[5] = b64[6];
178  y[6] = b64[5];
179  y[7] = b64[4];
180  y[8] = b64[11];
181  y[9] = b64[10];
182  y[10] = b64[9];
183  y[11] = b64[8];
184  y[12] = b64[15];
185  y[13] = b64[14];
186  y[14] = b64[13];
187  y[15] = b64[12];
188  y[16] = b64[19];
189  y[17] = b64[18];
190  y[18] = b64[17];
191  y[19] = b64[16];
192  y[20] = b64[23];
193  y[21] = b64[22];
194  y[22] = b64[21];
195  y[23] = b64[20];
196  y[24] = b64[27];
197  y[25] = b64[26];
198  y[26] = b64[25];
199  y[27] = b64[24];
200  y[28] = b64[31];
201  y[29] = b64[30];
202  y[30] = b64[29];
203  y[31] = b64[28];
204  y[32] = b64[35];
205  y[33] = b64[34];
206  y[34] = b64[33];
207  y[35] = b64[32];
208  y[36] = b64[39];
209  y[37] = b64[38];
210  y[38] = b64[37];
211  y[39] = b64[36];
212  y[40] = b64[43];
213  y[41] = b64[42];
214  y[42] = b64[41];
215  y[43] = b64[40];
216  y[44] = b64[47];
217  y[45] = b64[46];
218  y[46] = b64[45];
219  y[47] = b64[44];
220  y[48] = b64[51];
221  y[49] = b64[50];
222  y[50] = b64[49];
223  y[51] = b64[48];
224  y[52] = b64[55];
225  y[53] = b64[54];
226  y[54] = b64[53];
227  y[55] = b64[52];
228  y[56] = b64[59];
229  y[57] = b64[58];
230  y[58] = b64[57];
231  y[59] = b64[56];
232  y[60] = b64[63];
233  y[61] = b64[62];
234  y[62] = b64[61];
235  y[63] = b64[60];
236 #endif
237 
238  ROUND1(A, B, C, D, 0, Sa, 1);
239  ROUND1(D, A, B, C, 1, Sb, 2);
240  ROUND1(C, D, A, B, 2, Sc, 3);
241  ROUND1(B, C, D, A, 3, Sd, 4);
242  ROUND1(A, B, C, D, 4, Sa, 5);
243  ROUND1(D, A, B, C, 5, Sb, 6);
244  ROUND1(C, D, A, B, 6, Sc, 7);
245  ROUND1(B, C, D, A, 7, Sd, 8);
246  ROUND1(A, B, C, D, 8, Sa, 9);
247  ROUND1(D, A, B, C, 9, Sb, 10);
248  ROUND1(C, D, A, B, 10, Sc, 11);
249  ROUND1(B, C, D, A, 11, Sd, 12);
250  ROUND1(A, B, C, D, 12, Sa, 13);
251  ROUND1(D, A, B, C, 13, Sb, 14);
252  ROUND1(C, D, A, B, 14, Sc, 15);
253  ROUND1(B, C, D, A, 15, Sd, 16);
254 
255  ROUND2(A, B, C, D, 1, Se, 17);
256  ROUND2(D, A, B, C, 6, Sf, 18);
257  ROUND2(C, D, A, B, 11, Sg, 19);
258  ROUND2(B, C, D, A, 0, Sh, 20);
259  ROUND2(A, B, C, D, 5, Se, 21);
260  ROUND2(D, A, B, C, 10, Sf, 22);
261  ROUND2(C, D, A, B, 15, Sg, 23);
262  ROUND2(B, C, D, A, 4, Sh, 24);
263  ROUND2(A, B, C, D, 9, Se, 25);
264  ROUND2(D, A, B, C, 14, Sf, 26);
265  ROUND2(C, D, A, B, 3, Sg, 27);
266  ROUND2(B, C, D, A, 8, Sh, 28);
267  ROUND2(A, B, C, D, 13, Se, 29);
268  ROUND2(D, A, B, C, 2, Sf, 30);
269  ROUND2(C, D, A, B, 7, Sg, 31);
270  ROUND2(B, C, D, A, 12, Sh, 32);
271 
272  ROUND3(A, B, C, D, 5, Si, 33);
273  ROUND3(D, A, B, C, 8, Sj, 34);
274  ROUND3(C, D, A, B, 11, Sk, 35);
275  ROUND3(B, C, D, A, 14, Sl, 36);
276  ROUND3(A, B, C, D, 1, Si, 37);
277  ROUND3(D, A, B, C, 4, Sj, 38);
278  ROUND3(C, D, A, B, 7, Sk, 39);
279  ROUND3(B, C, D, A, 10, Sl, 40);
280  ROUND3(A, B, C, D, 13, Si, 41);
281  ROUND3(D, A, B, C, 0, Sj, 42);
282  ROUND3(C, D, A, B, 3, Sk, 43);
283  ROUND3(B, C, D, A, 6, Sl, 44);
284  ROUND3(A, B, C, D, 9, Si, 45);
285  ROUND3(D, A, B, C, 12, Sj, 46);
286  ROUND3(C, D, A, B, 15, Sk, 47);
287  ROUND3(B, C, D, A, 2, Sl, 48);
288 
289  ROUND4(A, B, C, D, 0, Sm, 49);
290  ROUND4(D, A, B, C, 7, Sn, 50);
291  ROUND4(C, D, A, B, 14, So, 51);
292  ROUND4(B, C, D, A, 5, Sp, 52);
293  ROUND4(A, B, C, D, 12, Sm, 53);
294  ROUND4(D, A, B, C, 3, Sn, 54);
295  ROUND4(C, D, A, B, 10, So, 55);
296  ROUND4(B, C, D, A, 1, Sp, 56);
297  ROUND4(A, B, C, D, 8, Sm, 57);
298  ROUND4(D, A, B, C, 15, Sn, 58);
299  ROUND4(C, D, A, B, 6, So, 59);
300  ROUND4(B, C, D, A, 13, Sp, 60);
301  ROUND4(A, B, C, D, 4, Sm, 61);
302  ROUND4(D, A, B, C, 11, Sn, 62);
303  ROUND4(C, D, A, B, 2, So, 63);
304  ROUND4(B, C, D, A, 9, Sp, 64);
305 
306  ctx->md5_sta += A;
307  ctx->md5_stb += B;
308  ctx->md5_stc += C;
309  ctx->md5_std += D;
310 }
unsigned int uint32
Definition: c.h:506
unsigned char uint8
Definition: c.h:504
int y
Definition: isn.c:72
#define Sj
Definition: md5.c:104
#define Sc
Definition: md5.c:95
#define Sd
Definition: md5.c:96
#define ROUND2(a, b, c, d, k, s, i)
Definition: md5.c:72
#define Sn
Definition: md5.c:109
#define Sh
Definition: md5.c:101
#define Sa
Definition: md5.c:93
#define Se
Definition: md5.c:98
#define ROUND3(a, b, c, d, k, s, i)
Definition: md5.c:79
#define Sg
Definition: md5.c:100
#define Sk
Definition: md5.c:105
#define Si
Definition: md5.c:103
#define ROUND1(a, b, c, d, k, s, i)
Definition: md5.c:65
#define Sp
Definition: md5.c:111
#define Sf
Definition: md5.c:99
#define ROUND4(a, b, c, d, k, s, i)
Definition: md5.c:86
#define Sb
Definition: md5.c:94
#define Sl
Definition: md5.c:106
#define Sm
Definition: md5.c:108
#define So
Definition: md5.c:110

References ROUND1, ROUND2, ROUND3, ROUND4, Sa, Sb, Sc, Sd, Se, Sf, Sg, Sh, Si, Sj, Sk, Sl, Sm, Sn, So, Sp, and y.

Referenced by md5_pad(), and pg_md5_update().

◆ md5_pad()

static void md5_pad ( pg_md5_ctx ctx)
static

Definition at line 313 of file md5.c.

314 {
315  unsigned int gap;
316 
317  /* Don't count up padding. Keep md5_n. */
318  gap = MD5_BUFLEN - ctx->md5_i;
319  if (gap > 8)
320  {
321  memmove(ctx->md5_buf + ctx->md5_i, md5_paddat,
322  gap - sizeof(ctx->md5_n));
323  }
324  else
325  {
326  /* including gap == 8 */
327  memmove(ctx->md5_buf + ctx->md5_i, md5_paddat, gap);
328  md5_calc(ctx->md5_buf, ctx);
329  memmove(ctx->md5_buf, md5_paddat + gap,
330  MD5_BUFLEN - sizeof(ctx->md5_n));
331  }
332 
333  /* 8 byte word */
334 #ifndef WORDS_BIGENDIAN
335  memmove(&ctx->md5_buf[56], &ctx->md5_n8[0], 8);
336 #else
337  ctx->md5_buf[56] = ctx->md5_n8[7];
338  ctx->md5_buf[57] = ctx->md5_n8[6];
339  ctx->md5_buf[58] = ctx->md5_n8[5];
340  ctx->md5_buf[59] = ctx->md5_n8[4];
341  ctx->md5_buf[60] = ctx->md5_n8[3];
342  ctx->md5_buf[61] = ctx->md5_n8[2];
343  ctx->md5_buf[62] = ctx->md5_n8[1];
344  ctx->md5_buf[63] = ctx->md5_n8[0];
345 #endif
346 
347  md5_calc(ctx->md5_buf, ctx);
348 }
static const uint8 md5_paddat[MD5_BUFLEN]
Definition: md5.c:142
static void md5_calc(const uint8 *b64, pg_md5_ctx *ctx)
Definition: md5.c:158
#define MD5_BUFLEN
Definition: md5_int.h:51
unsigned int md5_i
Definition: md5_int.h:76
uint8 md5_buf[MD5_BUFLEN]
Definition: md5_int.h:77

References pg_md5_ctx::md5_buf, MD5_BUFLEN, md5_calc(), pg_md5_ctx::md5_i, and md5_paddat.

Referenced by pg_md5_final().

◆ md5_result()

static void md5_result ( uint8 digest,
pg_md5_ctx ctx 
)
static

Definition at line 351 of file md5.c.

352 {
353  /* 4 byte words */
354 #ifndef WORDS_BIGENDIAN
355  memmove(digest, &ctx->md5_st8[0], 16);
356 #else
357  digest[0] = ctx->md5_st8[3];
358  digest[1] = ctx->md5_st8[2];
359  digest[2] = ctx->md5_st8[1];
360  digest[3] = ctx->md5_st8[0];
361  digest[4] = ctx->md5_st8[7];
362  digest[5] = ctx->md5_st8[6];
363  digest[6] = ctx->md5_st8[5];
364  digest[7] = ctx->md5_st8[4];
365  digest[8] = ctx->md5_st8[11];
366  digest[9] = ctx->md5_st8[10];
367  digest[10] = ctx->md5_st8[9];
368  digest[11] = ctx->md5_st8[8];
369  digest[12] = ctx->md5_st8[15];
370  digest[13] = ctx->md5_st8[14];
371  digest[14] = ctx->md5_st8[13];
372  digest[15] = ctx->md5_st8[12];
373 #endif
374 }

Referenced by pg_md5_final().

◆ pg_md5_final()

void pg_md5_final ( pg_md5_ctx ctx,
uint8 dest 
)

Definition at line 435 of file md5.c.

436 {
437  md5_pad(ctx);
438  md5_result(dest, ctx);
439 }
static void md5_result(uint8 *digest, pg_md5_ctx *ctx)
Definition: md5.c:351
static void md5_pad(pg_md5_ctx *ctx)
Definition: md5.c:313

References generate_unaccent_rules::dest, md5_pad(), and md5_result().

Referenced by pg_cryptohash_final().

◆ pg_md5_init()

void pg_md5_init ( pg_md5_ctx ctx)

Definition at line 385 of file md5.c.

386 {
387  ctx->md5_n = 0;
388  ctx->md5_i = 0;
389  ctx->md5_sta = MD5_A0;
390  ctx->md5_stb = MD5_B0;
391  ctx->md5_stc = MD5_C0;
392  ctx->md5_std = MD5_D0;
393  memset(ctx->md5_buf, 0, sizeof(ctx->md5_buf));
394 }
#define MD5_B0
Definition: md5.c:114
#define MD5_C0
Definition: md5.c:115
#define MD5_D0
Definition: md5.c:116
#define MD5_A0
Definition: md5.c:113

References MD5_A0, MD5_B0, pg_md5_ctx::md5_buf, MD5_C0, MD5_D0, and pg_md5_ctx::md5_i.

Referenced by pg_cryptohash_init().

◆ pg_md5_update()

void pg_md5_update ( pg_md5_ctx ctx,
const uint8 data,
size_t  len 
)

Definition at line 403 of file md5.c.

404 {
405  unsigned int gap,
406  i;
407 
408  ctx->md5_n += len * 8; /* byte to bit */
409  gap = MD5_BUFLEN - ctx->md5_i;
410 
411  if (len >= gap)
412  {
413  memmove(ctx->md5_buf + ctx->md5_i, data, gap);
414  md5_calc(ctx->md5_buf, ctx);
415 
416  for (i = gap; i + MD5_BUFLEN <= len; i += MD5_BUFLEN)
417  md5_calc(data + i, ctx);
418 
419  ctx->md5_i = len - i;
420  memmove(ctx->md5_buf, data + i, ctx->md5_i);
421  }
422  else
423  {
424  memmove(ctx->md5_buf + ctx->md5_i, data, len);
425  ctx->md5_i += len;
426  }
427 }
const void size_t len
const void * data

References data, i, len, pg_md5_ctx::md5_buf, MD5_BUFLEN, md5_calc(), and pg_md5_ctx::md5_i.

Referenced by pg_cryptohash_update().

Variable Documentation

◆ md5_paddat

const uint8 md5_paddat[MD5_BUFLEN]
static
Initial value:
= {
0x80, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}

Definition at line 142 of file md5.c.

Referenced by md5_pad().

◆ T

const uint32 T[65]
static
Initial value:
= {
0,
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
}

Definition at line 119 of file md5.c.

Referenced by compute_bitmap_pages(), cost_bitmap_heap_scan(), fetch_att(), index_pages_fetched(), and store_att_byval().