PostgreSQL Source Code  git master
md5_common.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * md5_common.c
4  * Routines shared between all MD5 implementations used for encrypted
5  * passwords.
6  *
7  * Sverre H. Huseby <sverrehu@online.no>
8  *
9  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  * IDENTIFICATION
13  * src/common/md5_common.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 
18 #ifndef FRONTEND
19 #include "postgres.h"
20 #else
21 #include "postgres_fe.h"
22 #endif
23 
24 #include "common/cryptohash.h"
25 #include "common/md5.h"
26 
27 static void
28 bytesToHex(uint8 b[16], char *s)
29 {
30  static const char *hex = "0123456789abcdef";
31  int q,
32  w;
33 
34  for (q = 0, w = 0; q < 16; q++)
35  {
36  s[w++] = hex[(b[q] >> 4) & 0x0F];
37  s[w++] = hex[b[q] & 0x0F];
38  }
39  s[w] = '\0';
40 }
41 
42 /*
43  * pg_md5_hash
44  *
45  * Calculates the MD5 sum of the bytes in a buffer.
46  *
47  * SYNOPSIS #include "md5.h"
48  * int pg_md5_hash(const void *buff, size_t len, char *hexsum)
49  *
50  * INPUT buff the buffer containing the bytes that you want
51  * the MD5 sum of.
52  * len number of bytes in the buffer.
53  *
54  * OUTPUT hexsum the MD5 sum as a '\0'-terminated string of
55  * hexadecimal digits. an MD5 sum is 16 bytes long.
56  * each byte is represented by two hexadecimal
57  * characters. you thus need to provide an array
58  * of 33 characters, including the trailing '\0'.
59  *
60  * RETURNS false on failure (out of memory for internal buffers
61  * or MD5 computation failure) or true on success.
62  *
63  * STANDARDS MD5 is described in RFC 1321.
64  *
65  * AUTHOR Sverre H. Huseby <sverrehu@online.no>
66  *
67  */
68 
69 bool
70 pg_md5_hash(const void *buff, size_t len, char *hexsum)
71 {
73  pg_cryptohash_ctx *ctx;
74 
76  if (ctx == NULL)
77  return false;
78 
79  if (pg_cryptohash_init(ctx) < 0 ||
80  pg_cryptohash_update(ctx, buff, len) < 0 ||
81  pg_cryptohash_final(ctx, sum, sizeof(sum)) < 0)
82  {
83  pg_cryptohash_free(ctx);
84  return false;
85  }
86 
87  bytesToHex(sum, hexsum);
88  pg_cryptohash_free(ctx);
89  return true;
90 }
91 
92 bool
93 pg_md5_binary(const void *buff, size_t len, void *outbuf)
94 {
95  pg_cryptohash_ctx *ctx;
96 
98  if (ctx == NULL)
99  return false;
100 
101  if (pg_cryptohash_init(ctx) < 0 ||
102  pg_cryptohash_update(ctx, buff, len) < 0 ||
103  pg_cryptohash_final(ctx, outbuf, MD5_DIGEST_LENGTH) < 0)
104  {
105  pg_cryptohash_free(ctx);
106  return false;
107  }
108 
109  pg_cryptohash_free(ctx);
110  return true;
111 }
112 
113 
114 /*
115  * Computes MD5 checksum of "passwd" (a null-terminated string) followed
116  * by "salt" (which need not be null-terminated).
117  *
118  * Output format is "md5" followed by a 32-hex-digit MD5 checksum.
119  * Hence, the output buffer "buf" must be at least 36 bytes long.
120  *
121  * Returns true if okay, false on error (out of memory).
122  */
123 bool
124 pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len,
125  char *buf)
126 {
127  size_t passwd_len = strlen(passwd);
128 
129  /* +1 here is just to avoid risk of unportable malloc(0) */
130  char *crypt_buf = malloc(passwd_len + salt_len + 1);
131  bool ret;
132 
133  if (!crypt_buf)
134  return false;
135 
136  /*
137  * Place salt at the end because it may be known by users trying to crack
138  * the MD5 output.
139  */
140  memcpy(crypt_buf, passwd, passwd_len);
141  memcpy(crypt_buf + passwd_len, salt, salt_len);
142 
143  strcpy(buf, "md5");
144  ret = pg_md5_hash(crypt_buf, passwd_len + salt_len, buf + 3);
145 
146  free(crypt_buf);
147 
148  return ret;
149 }
int pg_cryptohash_final(pg_cryptohash_ctx *ctx, uint8 *dest, size_t len)
Definition: cryptohash.c:168
bool pg_md5_binary(const void *buff, size_t len, void *outbuf)
Definition: md5_common.c:93
unsigned char uint8
Definition: c.h:439
#define malloc(a)
Definition: header.h:50
#define MD5_DIGEST_LENGTH
Definition: md5.h:20
static char * buf
Definition: pg_test_fsync.c:68
bool pg_md5_hash(const void *buff, size_t len, char *hexsum)
Definition: md5_common.c:70
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf)
Definition: md5_common.c:124
#define free(a)
Definition: header.h:65
pg_cryptohash_ctx * pg_cryptohash_create(pg_cryptohash_type type)
Definition: cryptohash.c:66
static void bytesToHex(uint8 b[16], char *s)
Definition: md5_common.c:28
int pg_cryptohash_init(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:92
int pg_cryptohash_update(pg_cryptohash_ctx *ctx, const uint8 *data, size_t len)
Definition: cryptohash.c:130
void pg_cryptohash_free(pg_cryptohash_ctx *ctx)
Definition: cryptohash.c:216