1 /*-------------------------------------------------------------------------
4 * Routines shared between all MD5 implementations used for encrypted
7 * Sverre H. Huseby <sverrehu@online.no>
9 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/common/md5_common.c
15 *-------------------------------------------------------------------------
21 #include "postgres_fe.h"
24 #include "common/cryptohash.h"
25 #include "common/md5.h"
28 bytesToHex(uint8 b
[16], char *s
)
30 static const char *hex
= "0123456789abcdef";
34 for (q
= 0, w
= 0; q
< 16; q
++)
36 s
[w
++] = hex
[(b
[q
] >> 4) & 0x0F];
37 s
[w
++] = hex
[b
[q
] & 0x0F];
45 * Calculates the MD5 sum of the bytes in a buffer.
47 * SYNOPSIS #include "md5.h"
48 * bool pg_md5_hash(const void *buff, size_t len, char *hexsum,
49 * const char **errstr)
51 * INPUT buff the buffer containing the bytes that you want
53 * len number of bytes in the buffer.
55 * OUTPUT hexsum the MD5 sum as a '\0'-terminated string of
56 * hexadecimal digits. an MD5 sum is 16 bytes long.
57 * each byte is represented by two hexadecimal
58 * characters. you thus need to provide an array
59 * of 33 characters, including the trailing '\0'.
61 * errstr filled with a constant-string error message
62 * on failure return; NULL on success.
64 * RETURNS false on failure (out of memory for internal buffers
65 * or MD5 computation failure) or true on success.
67 * STANDARDS MD5 is described in RFC 1321.
69 * AUTHOR Sverre H. Huseby <sverrehu@online.no>
74 pg_md5_hash(const void *buff
, size_t len
, char *hexsum
, const char **errstr
)
76 uint8 sum
[MD5_DIGEST_LENGTH
];
77 pg_cryptohash_ctx
*ctx
;
80 ctx
= pg_cryptohash_create(PG_MD5
);
83 *errstr
= pg_cryptohash_error(NULL
); /* returns OOM */
87 if (pg_cryptohash_init(ctx
) < 0 ||
88 pg_cryptohash_update(ctx
, buff
, len
) < 0 ||
89 pg_cryptohash_final(ctx
, sum
, sizeof(sum
)) < 0)
91 *errstr
= pg_cryptohash_error(ctx
);
92 pg_cryptohash_free(ctx
);
96 bytesToHex(sum
, hexsum
);
97 pg_cryptohash_free(ctx
);
104 * As above, except that the MD5 digest is returned as a binary string
105 * (of size MD5_DIGEST_LENGTH) rather than being converted to ASCII hex.
108 pg_md5_binary(const void *buff
, size_t len
, void *outbuf
, const char **errstr
)
110 pg_cryptohash_ctx
*ctx
;
113 ctx
= pg_cryptohash_create(PG_MD5
);
116 *errstr
= pg_cryptohash_error(NULL
); /* returns OOM */
120 if (pg_cryptohash_init(ctx
) < 0 ||
121 pg_cryptohash_update(ctx
, buff
, len
) < 0 ||
122 pg_cryptohash_final(ctx
, outbuf
, MD5_DIGEST_LENGTH
) < 0)
124 *errstr
= pg_cryptohash_error(ctx
);
125 pg_cryptohash_free(ctx
);
129 pg_cryptohash_free(ctx
);
135 * Computes MD5 checksum of "passwd" (a null-terminated string) followed
136 * by "salt" (which need not be null-terminated).
138 * Output format is "md5" followed by a 32-hex-digit MD5 checksum.
139 * Hence, the output buffer "buf" must be at least 36 bytes long.
141 * Returns true if okay, false on error with *errstr providing some
145 pg_md5_encrypt(const char *passwd
, const char *salt
, size_t salt_len
,
146 char *buf
, const char **errstr
)
148 size_t passwd_len
= strlen(passwd
);
150 /* +1 here is just to avoid risk of unportable malloc(0) */
151 char *crypt_buf
= malloc(passwd_len
+ salt_len
+ 1);
156 *errstr
= _("out of memory");
161 * Place salt at the end because it may be known by users trying to crack
164 memcpy(crypt_buf
, passwd
, passwd_len
);
165 memcpy(crypt_buf
+ passwd_len
, salt
, salt_len
);
168 ret
= pg_md5_hash(crypt_buf
, passwd_len
+ salt_len
, buf
+ 3, errstr
);