diff options
| author | Bruce Momjian <bruce@momjian.us> | 2001-03-22 04:01:46 +0000 |
|---|---|---|
| committer | Bruce Momjian <bruce@momjian.us> | 2001-03-22 04:01:46 +0000 |
| commit | 9e1552607a9dc6bc23e43d46770a9063ade4f3f0 (patch) | |
| tree | 6a230d81917ebc004e40cd46c48f2aa27eec153e /contrib/pgcrypto | |
| parent | 6cf8707b828b14b5c2336076ce358b18b67829d6 (diff) | |
| download | postgresql-9e1552607a9dc6bc23e43d46770a9063ade4f3f0.tar.gz | |
pgindent run. Make it all clean.
Diffstat (limited to 'contrib/pgcrypto')
| -rw-r--r-- | contrib/pgcrypto/encode.c | 212 | ||||
| -rw-r--r-- | contrib/pgcrypto/encode.h | 48 | ||||
| -rw-r--r-- | contrib/pgcrypto/internal.c | 52 | ||||
| -rw-r--r-- | contrib/pgcrypto/krb.c | 54 | ||||
| -rw-r--r-- | contrib/pgcrypto/md5.c | 387 | ||||
| -rw-r--r-- | contrib/pgcrypto/md5.h | 45 | ||||
| -rw-r--r-- | contrib/pgcrypto/mhash.c | 54 | ||||
| -rw-r--r-- | contrib/pgcrypto/openssl.c | 48 | ||||
| -rw-r--r-- | contrib/pgcrypto/pgcrypto.c | 79 | ||||
| -rw-r--r-- | contrib/pgcrypto/pgcrypto.h | 32 | ||||
| -rw-r--r-- | contrib/pgcrypto/sha1.c | 314 | ||||
| -rw-r--r-- | contrib/pgcrypto/sha1.h | 51 |
12 files changed, 806 insertions, 570 deletions
diff --git a/contrib/pgcrypto/encode.c b/contrib/pgcrypto/encode.c index 0ad0a8f56c..b6db217c83 100644 --- a/contrib/pgcrypto/encode.c +++ b/contrib/pgcrypto/encode.c @@ -1,7 +1,7 @@ /* * encode.c * Various data encoding/decoding things. - * + * * Copyright (c) 2001 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: encode.c,v 1.3 2001/02/10 02:31:25 tgl Exp $ + * $Id: encode.c,v 1.4 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -43,9 +43,9 @@ #endif static pg_coding * -find_coding(pg_coding *hbuf, text *name, int silent); + find_coding(pg_coding * hbuf, text *name, int silent); static pg_coding * -pg_find_coding(pg_coding *res, char *name); + pg_find_coding(pg_coding * res, char *name); /* SQL function: encode(bytea, text) returns text */ @@ -54,34 +54,37 @@ PG_FUNCTION_INFO_V1(encode); Datum encode(PG_FUNCTION_ARGS) { - text *arg; - text *name; - uint len, rlen, rlen0; - pg_coding *c, cbuf; - text *res; - + text *arg; + text *name; + uint len, + rlen, + rlen0; + pg_coding *c, + cbuf; + text *res; + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); - - name = PG_GETARG_TEXT_P(1); - c = find_coding(&cbuf, name, 0); /* will give error if fails */ + + name = PG_GETARG_TEXT_P(1); + c = find_coding(&cbuf, name, 0); /* will give error if fails */ arg = PG_GETARG_TEXT_P(0); len = VARSIZE(arg) - VARHDRSZ; - + rlen0 = c->encode_len(len); - - res = (text *)palloc(rlen0 + VARHDRSZ); - + + res = (text *) palloc(rlen0 + VARHDRSZ); + rlen = c->encode(VARDATA(arg), len, VARDATA(res)); VARATT_SIZEP(res) = rlen + VARHDRSZ; if (rlen > rlen0) elog(FATAL, "pg_encode: overflow, encode estimate too small"); - + PG_FREE_IF_COPY(arg, 0); PG_FREE_IF_COPY(name, 1); - + PG_RETURN_TEXT_P(res); } @@ -91,54 +94,58 @@ PG_FUNCTION_INFO_V1(decode); Datum decode(PG_FUNCTION_ARGS) { - text *arg; - text *name; - uint len, rlen, rlen0; - pg_coding *c, cbuf; - text *res; - + text *arg; + text *name; + uint len, + rlen, + rlen0; + pg_coding *c, + cbuf; + text *res; + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); - - name = PG_GETARG_TEXT_P(1); - c = find_coding(&cbuf, name, 0); /* will give error if fails */ + + name = PG_GETARG_TEXT_P(1); + c = find_coding(&cbuf, name, 0); /* will give error if fails */ arg = PG_GETARG_TEXT_P(0); len = VARSIZE(arg) - VARHDRSZ; - + rlen0 = c->decode_len(len); - - res = (text *)palloc(rlen0 + VARHDRSZ); - + + res = (text *) palloc(rlen0 + VARHDRSZ); + rlen = c->decode(VARDATA(arg), len, VARDATA(res)); VARATT_SIZEP(res) = rlen + VARHDRSZ; if (rlen > rlen0) elog(FATAL, "pg_decode: overflow, decode estimate too small"); - + PG_FREE_IF_COPY(arg, 0); PG_FREE_IF_COPY(name, 1); - + PG_RETURN_TEXT_P(res); } static pg_coding * -find_coding(pg_coding *dst, text *name, int silent) +find_coding(pg_coding * dst, text *name, int silent) { - pg_coding *p; - char buf[NAMEDATALEN]; - uint len; - + pg_coding *p; + char buf[NAMEDATALEN]; + uint len; + len = VARSIZE(name) - VARHDRSZ; - if (len >= NAMEDATALEN) { + if (len >= NAMEDATALEN) + { if (silent) return NULL; elog(ERROR, "Encoding type does not exist (name too long)"); } - + memcpy(buf, VARDATA(name), len); buf[len] = 0; - + p = pg_find_coding(dst, buf); if (p == NULL && !silent) @@ -151,21 +158,23 @@ static char *hextbl = "0123456789abcdef"; uint hex_encode(uint8 *src, uint len, uint8 *dst) { - uint8 *end = src + len; - while (src < end) { + uint8 *end = src + len; + + while (src < end) + { *dst++ = hextbl[(*src >> 4) & 0xF]; *dst++ = hextbl[*src & 0xF]; src++; } - return len*2; + return len * 2; } /* probably should use lookup table */ static uint8 get_hex(char c) { - uint8 res = 0; - + uint8 res = 0; + if (c >= '0' && c <= '9') res = c - '0'; else if (c >= 'a' && c <= 'f') @@ -174,19 +183,26 @@ get_hex(char c) res = c - 'A' + 10; else elog(ERROR, "Bad hex code: '%c'", c); - + return res; } uint hex_decode(uint8 *src, uint len, uint8 *dst) { - uint8 *s, *srcend, v1, v2, *p = dst; - + uint8 *s, + *srcend, + v1, + v2, + *p = dst; + srcend = src + len; - s = src; p = dst; - while (s < srcend) { - if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') { + s = src; + p = dst; + while (s < srcend) + { + if (*s == ' ' || *s == '\n' || *s == '\t' || *s == '\r') + { s++; continue; } @@ -196,30 +212,36 @@ hex_decode(uint8 *src, uint len, uint8 *dst) v2 = get_hex(*s++); *p++ = v1 | v2; } - + return p - dst; } static unsigned char _base64[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; uint b64_encode(uint8 *src, uint len, uint8 *dst) { - uint8 *s, *p, *end = src + len, *lend = dst + 76; - int pos = 2; + uint8 *s, + *p, + *end = src + len, + *lend = dst + 76; + int pos = 2; unsigned long buf = 0; - s = src; p = dst; - - while (s < end) { + s = src; + p = dst; + + while (s < end) + { buf |= *s << (pos << 3); pos--; s++; - + /* write it out */ - if (pos < 0) { + if (pos < 0) + { *p++ = _base64[(buf >> 18) & 0x3f]; *p++ = _base64[(buf >> 12) & 0x3f]; *p++ = _base64[(buf >> 6) & 0x3f]; @@ -228,12 +250,14 @@ b64_encode(uint8 *src, uint len, uint8 *dst) pos = 2; buf = 0; } - if (p >= lend) { + if (p >= lend) + { *p++ = '\n'; lend = p + 76; } } - if (pos != 2) { + if (pos != 2) + { *p++ = _base64[(buf >> 18) & 0x3f]; *p++ = _base64[(buf >> 12) & 0x3f]; *p++ = (pos == 0) ? _base64[(buf >> 6) & 0x3f] : '='; @@ -247,14 +271,17 @@ b64_encode(uint8 *src, uint len, uint8 *dst) uint b64_decode(uint8 *src, uint len, uint8 *dst) { - char *srcend = src + len, *s = src; - uint8 *p = dst; - char c; - uint b = 0; + char *srcend = src + len, + *s = src; + uint8 *p = dst; + char c; + uint b = 0; unsigned long buf = 0; - int pos = 0, end = 0; - - while (s < srcend) { + int pos = 0, + end = 0; + + while (s < srcend) + { c = *s++; if (c >= 'A' && c <= 'Z') b = c - 'A'; @@ -266,16 +293,21 @@ b64_decode(uint8 *src, uint len, uint8 *dst) b = 62; else if (c == '/') b = 63; - else if (c == '=') { + else if (c == '=') + { /* end sequence */ - if (!end) { - if (pos == 2) end = 1; - else if (pos == 3) end = 2; + if (!end) + { + if (pos == 2) + end = 1; + else if (pos == 3) + end = 2; else elog(ERROR, "base64: unexpected '='"); } b = 0; - } else if (c == ' ' || c == '\t' || c == '\n' || c == '\r') + } + else if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue; else elog(ERROR, "base64: Invalid symbol"); @@ -283,7 +315,8 @@ b64_decode(uint8 *src, uint len, uint8 *dst) /* add it to buffer */ buf = (buf << 6) + b; pos++; - if (pos == 4) { + if (pos == 4) + { *p++ = (buf >> 16) & 255; if (end == 0 || end > 1) *p++ = (buf >> 8) & 255; @@ -326,21 +359,22 @@ b64_dec_len(uint srclen) } static pg_coding -encoding_list [] = { - { "hex", hex_enc_len, hex_dec_len, hex_encode, hex_decode}, - { "base64", b64_enc_len, b64_dec_len, b64_encode, b64_decode}, - { NULL, NULL, NULL, NULL, NULL} + encoding_list[] = { + {"hex", hex_enc_len, hex_dec_len, hex_encode, hex_decode}, + {"base64", b64_enc_len, b64_dec_len, b64_encode, b64_decode}, + {NULL, NULL, NULL, NULL, NULL} }; static pg_coding * -pg_find_coding(pg_coding *res, char *name) +pg_find_coding(pg_coding * res, char *name) { - pg_coding *p; - for (p = encoding_list; p->name; p++) { + pg_coding *p; + + for (p = encoding_list; p->name; p++) + { if (!strcasecmp(p->name, name)) return p; } return NULL; } - diff --git a/contrib/pgcrypto/encode.h b/contrib/pgcrypto/encode.h index 33e6508cb1..451e06d75b 100644 --- a/contrib/pgcrypto/encode.h +++ b/contrib/pgcrypto/encode.h @@ -1,7 +1,7 @@ /* * pg_encode.h * encode.c - * + * * Copyright (c) 2001 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,35 +26,35 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: encode.h,v 1.1 2001/01/24 03:46:16 momjian Exp $ + * $Id: encode.h,v 1.2 2001/03/22 03:59:10 momjian Exp $ */ #ifndef __PG_ENCODE_H #define __PG_ENCODE_H /* exported functions */ -Datum encode(PG_FUNCTION_ARGS); -Datum decode(PG_FUNCTION_ARGS); +Datum encode(PG_FUNCTION_ARGS); +Datum decode(PG_FUNCTION_ARGS); typedef struct _pg_coding pg_coding; -struct _pg_coding { - char *name; - uint (*encode_len)(uint dlen); - uint (*decode_len)(uint dlen); - uint (*encode)(uint8 *data, uint dlen, uint8 *res); - uint (*decode)(uint8 *data, uint dlen, uint8 *res); +struct _pg_coding +{ + char *name; + uint (*encode_len) (uint dlen); + uint (*decode_len) (uint dlen); + uint (*encode) (uint8 *data, uint dlen, uint8 *res); + uint (*decode) (uint8 *data, uint dlen, uint8 *res); }; /* They are for outside usage in C code, if needed */ -uint hex_encode(uint8 *src, uint len, uint8 *dst); -uint hex_decode(uint8 *src, uint len, uint8 *dst); -uint b64_encode(uint8 *src, uint len, uint8 *dst); -uint b64_decode(uint8 *src, uint len, uint8 *dst); - -uint hex_enc_len(uint srclen); -uint hex_dec_len(uint srclen); -uint b64_enc_len(uint srclen); -uint b64_dec_len(uint srclen); +uint hex_encode(uint8 *src, uint len, uint8 *dst); +uint hex_decode(uint8 *src, uint len, uint8 *dst); +uint b64_encode(uint8 *src, uint len, uint8 *dst); +uint b64_decode(uint8 *src, uint len, uint8 *dst); -#endif /* __PG_ENCODE_H */ +uint hex_enc_len(uint srclen); +uint hex_dec_len(uint srclen); +uint b64_enc_len(uint srclen); +uint b64_dec_len(uint srclen); +#endif /* __PG_ENCODE_H */ diff --git a/contrib/pgcrypto/internal.c b/contrib/pgcrypto/internal.c index 3c6e6fc80b..2d4a52717e 100644 --- a/contrib/pgcrypto/internal.c +++ b/contrib/pgcrypto/internal.c @@ -1,7 +1,7 @@ /* * internal.c * Wrapper for builtin functions - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: internal.c,v 1.2 2001/02/10 02:31:25 tgl Exp $ + * $Id: internal.c,v 1.3 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -49,31 +49,32 @@ #endif static uint -pg_md5_len(pg_digest *h); + pg_md5_len(pg_digest * h); static uint8 * -pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf); + pg_md5_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf); static uint -pg_sha1_len(pg_digest *h); + pg_sha1_len(pg_digest * h); static uint8 * -pg_sha1_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf); + pg_sha1_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf); static pg_digest -int_digest_list [] = { - { "md5", pg_md5_len, pg_md5_digest, {0}}, - { "sha1", pg_sha1_len, pg_sha1_digest, {0}}, - { NULL, NULL, NULL, {0}} + int_digest_list[] = { + {"md5", pg_md5_len, pg_md5_digest, {0}}, + {"sha1", pg_sha1_len, pg_sha1_digest, {0}}, + {NULL, NULL, NULL, {0}} }; static uint -pg_md5_len(pg_digest *h) { +pg_md5_len(pg_digest * h) +{ return MD5_DIGEST_LENGTH; } static uint8 * -pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) +pg_md5_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf) { - MD5_CTX ctx; + MD5_CTX ctx; MD5Init(&ctx); MD5Update(&ctx, src, len); @@ -83,32 +84,31 @@ pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) } static uint -pg_sha1_len(pg_digest *h) { +pg_sha1_len(pg_digest * h) +{ return SHA1_DIGEST_LENGTH; } static uint8 * -pg_sha1_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) +pg_sha1_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf) { - SHA1_CTX ctx; + SHA1_CTX ctx; SHA1Init(&ctx); SHA1Update(&ctx, src, len); SHA1Final(buf, &ctx); - + return buf; } -pg_digest * -pg_find_digest(pg_digest *h, char *name) +pg_digest * +pg_find_digest(pg_digest * h, char *name) { - pg_digest *p; + pg_digest *p; for (p = int_digest_list; p->name; p++) if (!strcasecmp(p->name, name)) return p; return NULL; } - - diff --git a/contrib/pgcrypto/krb.c b/contrib/pgcrypto/krb.c index 14ac44b29d..95df07ca27 100644 --- a/contrib/pgcrypto/krb.c +++ b/contrib/pgcrypto/krb.c @@ -1,7 +1,7 @@ /* * krb.c * Wrapper for Kerberos5 libdes SHA1/MD5. - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,12 +26,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * + * * NOTES * It is possible that this works with other SHA1/MD5 * implementations too. * - * $Id: krb.c,v 1.3 2001/02/20 15:34:14 momjian Exp $ + * $Id: krb.c,v 1.4 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -54,31 +54,32 @@ #endif static uint -pg_md5_len(pg_digest *h); + pg_md5_len(pg_digest * h); static uint8 * -pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf); + pg_md5_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf); static uint -pg_sha1_len(pg_digest *h); + pg_sha1_len(pg_digest * h); static uint8 * -pg_sha1_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf); + pg_sha1_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf); static pg_digest -int_digest_list [] = { - { "md5", pg_md5_len, pg_md5_digest, {0}}, - { "sha1", pg_sha1_len, pg_sha1_digest, {0}}, - { NULL, NULL, NULL, {0}} + int_digest_list[] = { + {"md5", pg_md5_len, pg_md5_digest, {0}}, + {"sha1", pg_sha1_len, pg_sha1_digest, {0}}, + {NULL, NULL, NULL, {0}} }; static uint -pg_md5_len(pg_digest *h) { +pg_md5_len(pg_digest * h) +{ return MD5_DIGEST_LENGTH; } static uint8 * -pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) +pg_md5_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf) { - MD5_CTX ctx; + MD5_CTX ctx; MD5Init(&ctx); MD5Update(&ctx, src, len); @@ -88,32 +89,31 @@ pg_md5_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) } static uint -pg_sha1_len(pg_digest *h) { +pg_sha1_len(pg_digest * h) +{ return SHA1_DIGEST_LENGTH; } static uint8 * -pg_sha1_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) +pg_sha1_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf) { - SHA1_CTX ctx; + SHA1_CTX ctx; SHA1Init(&ctx); SHA1Update(&ctx, src, len); SHA1Final(buf, &ctx); - + return buf; } -pg_digest * -pg_find_digest(pg_digest *h, char *name) +pg_digest * +pg_find_digest(pg_digest * h, char *name) { - pg_digest *p; + pg_digest *p; for (p = int_digest_list; p->name; p++) if (!strcasecmp(p->name, name)) return p; return NULL; } - - diff --git a/contrib/pgcrypto/md5.c b/contrib/pgcrypto/md5.c index d538720231..a5cfae3497 100644 --- a/contrib/pgcrypto/md5.c +++ b/contrib/pgcrypto/md5.c @@ -1,5 +1,5 @@ -/* $Id: md5.c,v 1.4 2001/02/10 02:31:25 tgl Exp $ */ -/* $KAME: md5.c,v 1.3 2000/02/22 14:01:17 itojun Exp $ */ +/* $Id: md5.c,v 1.5 2001/03/22 03:59:10 momjian Exp $ */ +/* $KAME: md5.c,v 1.3 2000/02/22 14:01:17 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -9,18 +9,18 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -93,42 +93,43 @@ /* Integer part of 4294967296 times abs(sin(i)), where i is in radians. */ static const uint32 T[65] = { 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, + 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, }; static const uint8 md5_paddat[MD5_BUFLEN] = { - 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, + 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, }; -static void md5_calc (uint8 *, md5_ctxt *); +static void md5_calc(uint8 *, md5_ctxt *); -void md5_init(ctxt) - md5_ctxt *ctxt; +void +md5_init(ctxt) +md5_ctxt *ctxt; { ctxt->md5_n = 0; ctxt->md5_i = 0; @@ -139,56 +140,64 @@ void md5_init(ctxt) bzero(ctxt->md5_buf, sizeof(ctxt->md5_buf)); } -void md5_loop(ctxt, input, len) - md5_ctxt *ctxt; - uint8 *input; - unsigned int len; /* number of bytes */ +void +md5_loop(ctxt, input, len) +md5_ctxt *ctxt; +uint8 *input; +unsigned int len; /* number of bytes */ { - unsigned int gap, i; + unsigned int gap, + i; - ctxt->md5_n += len * 8; /* byte to bit */ + ctxt->md5_n += len * 8; /* byte to bit */ gap = MD5_BUFLEN - ctxt->md5_i; - if (len >= gap) { - bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i), - gap); + if (len >= gap) + { + bcopy((void *) input, (void *) (ctxt->md5_buf + ctxt->md5_i), + gap); md5_calc(ctxt->md5_buf, ctxt); - for (i = gap; i + MD5_BUFLEN <= len; i += MD5_BUFLEN) { - md5_calc((uint8 *)(input + i), ctxt); - } - + for (i = gap; i + MD5_BUFLEN <= len; i += MD5_BUFLEN) + md5_calc((uint8 *) (input + i), ctxt); + ctxt->md5_i = len - i; - bcopy((void *)(input + i), (void *)ctxt->md5_buf, ctxt->md5_i); - } else { - bcopy((void *)input, (void *)(ctxt->md5_buf + ctxt->md5_i), - len); + bcopy((void *) (input + i), (void *) ctxt->md5_buf, ctxt->md5_i); + } + else + { + bcopy((void *) input, (void *) (ctxt->md5_buf + ctxt->md5_i), + len); ctxt->md5_i += len; } } -void md5_pad(ctxt) - md5_ctxt *ctxt; +void +md5_pad(ctxt) +md5_ctxt *ctxt; { unsigned int gap; - /* Don't count up padding. Keep md5_n. */ + /* Don't count up padding. Keep md5_n. */ gap = MD5_BUFLEN - ctxt->md5_i; - if (gap > 8) { - bcopy((void *)md5_paddat, - (void *)(ctxt->md5_buf + ctxt->md5_i), - gap - sizeof(ctxt->md5_n)); - } else { + if (gap > 8) + { + bcopy((void *) md5_paddat, + (void *) (ctxt->md5_buf + ctxt->md5_i), + gap - sizeof(ctxt->md5_n)); + } + else + { /* including gap == 8 */ - bcopy((void *)md5_paddat, (void *)(ctxt->md5_buf + ctxt->md5_i), - gap); + bcopy((void *) md5_paddat, (void *) (ctxt->md5_buf + ctxt->md5_i), + gap); md5_calc(ctxt->md5_buf, ctxt); - bcopy((void *)(md5_paddat + gap), - (void *)ctxt->md5_buf, - MD5_BUFLEN - sizeof(ctxt->md5_n)); + bcopy((void *) (md5_paddat + gap), + (void *) ctxt->md5_buf, + MD5_BUFLEN - sizeof(ctxt->md5_n)); } - /* 8 byte word */ + /* 8 byte word */ #if BYTE_ORDER == LITTLE_ENDIAN bcopy(&ctxt->md5_n8[0], &ctxt->md5_buf[56], 8); #endif @@ -206,98 +215,192 @@ void md5_pad(ctxt) md5_calc(ctxt->md5_buf, ctxt); } -void md5_result(digest, ctxt) - uint8 *digest; - md5_ctxt *ctxt; +void +md5_result(digest, ctxt) +uint8 *digest; +md5_ctxt *ctxt; { /* 4 byte words */ #if BYTE_ORDER == LITTLE_ENDIAN bcopy(&ctxt->md5_st8[0], digest, 16); #endif #if BYTE_ORDER == BIG_ENDIAN - digest[ 0] = ctxt->md5_st8[ 3]; digest[ 1] = ctxt->md5_st8[ 2]; - digest[ 2] = ctxt->md5_st8[ 1]; digest[ 3] = ctxt->md5_st8[ 0]; - digest[ 4] = ctxt->md5_st8[ 7]; digest[ 5] = ctxt->md5_st8[ 6]; - digest[ 6] = ctxt->md5_st8[ 5]; digest[ 7] = ctxt->md5_st8[ 4]; - digest[ 8] = ctxt->md5_st8[11]; digest[ 9] = ctxt->md5_st8[10]; - digest[10] = ctxt->md5_st8[ 9]; digest[11] = ctxt->md5_st8[ 8]; - digest[12] = ctxt->md5_st8[15]; digest[13] = ctxt->md5_st8[14]; - digest[14] = ctxt->md5_st8[13]; digest[15] = ctxt->md5_st8[12]; + digest[0] = ctxt->md5_st8[3]; + digest[1] = ctxt->md5_st8[2]; + digest[2] = ctxt->md5_st8[1]; + digest[3] = ctxt->md5_st8[0]; + digest[4] = ctxt->md5_st8[7]; + digest[5] = ctxt->md5_st8[6]; + digest[6] = ctxt->md5_st8[5]; + digest[7] = ctxt->md5_st8[4]; + digest[8] = ctxt->md5_st8[11]; + digest[9] = ctxt->md5_st8[10]; + digest[10] = ctxt->md5_st8[9]; + digest[11] = ctxt->md5_st8[8]; + digest[12] = ctxt->md5_st8[15]; + digest[13] = ctxt->md5_st8[14]; + digest[14] = ctxt->md5_st8[13]; + digest[15] = ctxt->md5_st8[12]; #endif } #if BYTE_ORDER == BIG_ENDIAN -uint32 X[16]; +uint32 X[16]; + #endif -static void md5_calc(b64, ctxt) - uint8 *b64; - md5_ctxt *ctxt; +static void +md5_calc(b64, ctxt) +uint8 *b64; +md5_ctxt *ctxt; { - uint32 A = ctxt->md5_sta; - uint32 B = ctxt->md5_stb; - uint32 C = ctxt->md5_stc; - uint32 D = ctxt->md5_std; + uint32 A = ctxt->md5_sta; + uint32 B = ctxt->md5_stb; + uint32 C = ctxt->md5_stc; + uint32 D = ctxt->md5_std; + #if BYTE_ORDER == LITTLE_ENDIAN - uint32 *X = (uint32 *)b64; -#endif + uint32 *X = (uint32 *) b64; + +#endif #if BYTE_ORDER == BIG_ENDIAN /* 4 byte words */ /* what a brute force but fast! */ - uint8 *y = (uint8 *)X; - y[ 0] = b64[ 3]; y[ 1] = b64[ 2]; y[ 2] = b64[ 1]; y[ 3] = b64[ 0]; - y[ 4] = b64[ 7]; y[ 5] = b64[ 6]; y[ 6] = b64[ 5]; y[ 7] = b64[ 4]; - y[ 8] = b64[11]; y[ 9] = b64[10]; y[10] = b64[ 9]; y[11] = b64[ 8]; - y[12] = b64[15]; y[13] = b64[14]; y[14] = b64[13]; y[15] = b64[12]; - y[16] = b64[19]; y[17] = b64[18]; y[18] = b64[17]; y[19] = b64[16]; - y[20] = b64[23]; y[21] = b64[22]; y[22] = b64[21]; y[23] = b64[20]; - y[24] = b64[27]; y[25] = b64[26]; y[26] = b64[25]; y[27] = b64[24]; - y[28] = b64[31]; y[29] = b64[30]; y[30] = b64[29]; y[31] = b64[28]; - y[32] = b64[35]; y[33] = b64[34]; y[34] = b64[33]; y[35] = b64[32]; - y[36] = b64[39]; y[37] = b64[38]; y[38] = b64[37]; y[39] = b64[36]; - y[40] = b64[43]; y[41] = b64[42]; y[42] = b64[41]; y[43] = b64[40]; - y[44] = b64[47]; y[45] = b64[46]; y[46] = b64[45]; y[47] = b64[44]; - y[48] = b64[51]; y[49] = b64[50]; y[50] = b64[49]; y[51] = b64[48]; - y[52] = b64[55]; y[53] = b64[54]; y[54] = b64[53]; y[55] = b64[52]; - y[56] = b64[59]; y[57] = b64[58]; y[58] = b64[57]; y[59] = b64[56]; - y[60] = b64[63]; y[61] = b64[62]; y[62] = b64[61]; y[63] = b64[60]; + uint8 *y = (uint8 *) X; + + y[0] = b64[3]; + y[1] = b64[2]; + y[2] = b64[1]; + y[3] = b64[0]; + y[4] = b64[7]; + y[5] = b64[6]; + y[6] = b64[5]; + y[7] = b64[4]; + y[8] = b64[11]; + y[9] = b64[10]; + y[10] = b64[9]; + y[11] = b64[8]; + y[12] = b64[15]; + y[13] = b64[14]; + y[14] = b64[13]; + y[15] = b64[12]; + y[16] = b64[19]; + y[17] = b64[18]; + y[18] = b64[17]; + y[19] = b64[16]; + y[20] = b64[23]; + y[21] = b64[22]; + y[22] = b64[21]; + y[23] = b64[20]; + y[24] = b64[27]; + y[25] = b64[26]; + y[26] = b64[25]; + y[27] = b64[24]; + y[28] = b64[31]; + y[29] = b64[30]; + y[30] = b64[29]; + y[31] = b64[28]; + y[32] = b64[35]; + y[33] = b64[34]; + y[34] = b64[33]; + y[35] = b64[32]; + y[36] = b64[39]; + y[37] = b64[38]; + y[38] = b64[37]; + y[39] = b64[36]; + y[40] = b64[43]; + y[41] = b64[42]; + y[42] = b64[41]; + y[43] = b64[40]; + y[44] = b64[47]; + y[45] = b64[46]; + y[46] = b64[45]; + y[47] = b64[44]; + y[48] = b64[51]; + y[49] = b64[50]; + y[50] = b64[49]; + y[51] = b64[48]; + y[52] = b64[55]; + y[53] = b64[54]; + y[54] = b64[53]; + y[55] = b64[52]; + y[56] = b64[59]; + y[57] = b64[58]; + y[58] = b64[57]; + y[59] = b64[56]; + y[60] = b64[63]; + y[61] = b64[62]; + y[62] = b64[61]; + y[63] = b64[60]; #endif - ROUND1(A, B, C, D, 0, Sa, 1); ROUND1(D, A, B, C, 1, Sb, 2); - ROUND1(C, D, A, B, 2, Sc, 3); ROUND1(B, C, D, A, 3, Sd, 4); - ROUND1(A, B, C, D, 4, Sa, 5); ROUND1(D, A, B, C, 5, Sb, 6); - ROUND1(C, D, A, B, 6, Sc, 7); ROUND1(B, C, D, A, 7, Sd, 8); - ROUND1(A, B, C, D, 8, Sa, 9); ROUND1(D, A, B, C, 9, Sb, 10); - ROUND1(C, D, A, B, 10, Sc, 11); ROUND1(B, C, D, A, 11, Sd, 12); - ROUND1(A, B, C, D, 12, Sa, 13); ROUND1(D, A, B, C, 13, Sb, 14); - ROUND1(C, D, A, B, 14, Sc, 15); ROUND1(B, C, D, A, 15, Sd, 16); - - ROUND2(A, B, C, D, 1, Se, 17); ROUND2(D, A, B, C, 6, Sf, 18); - ROUND2(C, D, A, B, 11, Sg, 19); ROUND2(B, C, D, A, 0, Sh, 20); - ROUND2(A, B, C, D, 5, Se, 21); ROUND2(D, A, B, C, 10, Sf, 22); - ROUND2(C, D, A, B, 15, Sg, 23); ROUND2(B, C, D, A, 4, Sh, 24); - ROUND2(A, B, C, D, 9, Se, 25); ROUND2(D, A, B, C, 14, Sf, 26); - ROUND2(C, D, A, B, 3, Sg, 27); ROUND2(B, C, D, A, 8, Sh, 28); - ROUND2(A, B, C, D, 13, Se, 29); ROUND2(D, A, B, C, 2, Sf, 30); - ROUND2(C, D, A, B, 7, Sg, 31); ROUND2(B, C, D, A, 12, Sh, 32); - - ROUND3(A, B, C, D, 5, Si, 33); ROUND3(D, A, B, C, 8, Sj, 34); - ROUND3(C, D, A, B, 11, Sk, 35); ROUND3(B, C, D, A, 14, Sl, 36); - ROUND3(A, B, C, D, 1, Si, 37); ROUND3(D, A, B, C, 4, Sj, 38); - ROUND3(C, D, A, B, 7, Sk, 39); ROUND3(B, C, D, A, 10, Sl, 40); - ROUND3(A, B, C, D, 13, Si, 41); ROUND3(D, A, B, C, 0, Sj, 42); - ROUND3(C, D, A, B, 3, Sk, 43); ROUND3(B, C, D, A, 6, Sl, 44); - ROUND3(A, B, C, D, 9, Si, 45); ROUND3(D, A, B, C, 12, Sj, 46); - ROUND3(C, D, A, B, 15, Sk, 47); ROUND3(B, C, D, A, 2, Sl, 48); - - ROUND4(A, B, C, D, 0, Sm, 49); ROUND4(D, A, B, C, 7, Sn, 50); - ROUND4(C, D, A, B, 14, So, 51); ROUND4(B, C, D, A, 5, Sp, 52); - ROUND4(A, B, C, D, 12, Sm, 53); ROUND4(D, A, B, C, 3, Sn, 54); - ROUND4(C, D, A, B, 10, So, 55); ROUND4(B, C, D, A, 1, Sp, 56); - ROUND4(A, B, C, D, 8, Sm, 57); ROUND4(D, A, B, C, 15, Sn, 58); - ROUND4(C, D, A, B, 6, So, 59); ROUND4(B, C, D, A, 13, Sp, 60); - ROUND4(A, B, C, D, 4, Sm, 61); ROUND4(D, A, B, C, 11, Sn, 62); - ROUND4(C, D, A, B, 2, So, 63); ROUND4(B, C, D, A, 9, Sp, 64); + ROUND1(A, B, C, D, 0, Sa, 1); + ROUND1(D, A, B, C, 1, Sb, 2); + ROUND1(C, D, A, B, 2, Sc, 3); + ROUND1(B, C, D, A, 3, Sd, 4); + ROUND1(A, B, C, D, 4, Sa, 5); + ROUND1(D, A, B, C, 5, Sb, 6); + ROUND1(C, D, A, B, 6, Sc, 7); + ROUND1(B, C, D, A, 7, Sd, 8); + ROUND1(A, B, C, D, 8, Sa, 9); + ROUND1(D, A, B, C, 9, Sb, 10); + ROUND1(C, D, A, B, 10, Sc, 11); + ROUND1(B, C, D, A, 11, Sd, 12); + ROUND1(A, B, C, D, 12, Sa, 13); + ROUND1(D, A, B, C, 13, Sb, 14); + ROUND1(C, D, A, B, 14, Sc, 15); + ROUND1(B, C, D, A, 15, Sd, 16); + + ROUND2(A, B, C, D, 1, Se, 17); + ROUND2(D, A, B, C, 6, Sf, 18); + ROUND2(C, D, A, B, 11, Sg, 19); + ROUND2(B, C, D, A, 0, Sh, 20); + ROUND2(A, B, C, D, 5, Se, 21); + ROUND2(D, A, B, C, 10, Sf, 22); + ROUND2(C, D, A, B, 15, Sg, 23); + ROUND2(B, C, D, A, 4, Sh, 24); + ROUND2(A, B, C, D, 9, Se, 25); + ROUND2(D, A, B, C, 14, Sf, 26); + ROUND2(C, D, A, B, 3, Sg, 27); + ROUND2(B, C, D, A, 8, Sh, 28); + ROUND2(A, B, C, D, 13, Se, 29); + ROUND2(D, A, B, C, 2, Sf, 30); + ROUND2(C, D, A, B, 7, Sg, 31); + ROUND2(B, C, D, A, 12, Sh, 32); + + ROUND3(A, B, C, D, 5, Si, 33); + ROUND3(D, A, B, C, 8, Sj, 34); + ROUND3(C, D, A, B, 11, Sk, 35); + ROUND3(B, C, D, A, 14, Sl, 36); + ROUND3(A, B, C, D, 1, Si, 37); + ROUND3(D, A, B, C, 4, Sj, 38); + ROUND3(C, D, A, B, 7, Sk, 39); + ROUND3(B, C, D, A, 10, Sl, 40); + ROUND3(A, B, C, D, 13, Si, 41); + ROUND3(D, A, B, C, 0, Sj, 42); + ROUND3(C, D, A, B, 3, Sk, 43); + ROUND3(B, C, D, A, 6, Sl, 44); + ROUND3(A, B, C, D, 9, Si, 45); + ROUND3(D, A, B, C, 12, Sj, 46); + ROUND3(C, D, A, B, 15, Sk, 47); + ROUND3(B, C, D, A, 2, Sl, 48); + + ROUND4(A, B, C, D, 0, Sm, 49); + ROUND4(D, A, B, C, 7, Sn, 50); + ROUND4(C, D, A, B, 14, So, 51); + ROUND4(B, C, D, A, 5, Sp, 52); + ROUND4(A, B, C, D, 12, Sm, 53); + ROUND4(D, A, B, C, 3, Sn, 54); + ROUND4(C, D, A, B, 10, So, 55); + ROUND4(B, C, D, A, 1, Sp, 56); + ROUND4(A, B, C, D, 8, Sm, 57); + ROUND4(D, A, B, C, 15, Sn, 58); + ROUND4(C, D, A, B, 6, So, 59); + ROUND4(B, C, D, A, 13, Sp, 60); + ROUND4(A, B, C, D, 4, Sm, 61); + ROUND4(D, A, B, C, 11, Sn, 62); + ROUND4(C, D, A, B, 2, So, 63); + ROUND4(B, C, D, A, 9, Sp, 64); ctxt->md5_sta += A; ctxt->md5_stb += B; diff --git a/contrib/pgcrypto/md5.h b/contrib/pgcrypto/md5.h index b724c34ba0..54ca2558df 100644 --- a/contrib/pgcrypto/md5.h +++ b/contrib/pgcrypto/md5.h @@ -1,5 +1,5 @@ -/* $Id: md5.h,v 1.3 2001/01/09 16:07:13 momjian Exp $ */ -/* $KAME: md5.h,v 1.3 2000/02/22 14:01:18 itojun Exp $ */ +/* $Id: md5.h,v 1.4 2001/03/22 03:59:10 momjian Exp $ */ +/* $KAME: md5.h,v 1.3 2000/02/22 14:01:18 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -9,18 +9,18 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -35,11 +35,13 @@ #define MD5_BUFLEN 64 -typedef struct { - union { +typedef struct +{ + union + { uint32 md5_state32[4]; uint8 md5_state8[16]; - } md5_st; + } md5_st; #define md5_sta md5_st.md5_state32[0] #define md5_stb md5_st.md5_state32[1] @@ -47,21 +49,22 @@ typedef struct { #define md5_std md5_st.md5_state32[3] #define md5_st8 md5_st.md5_state8 - union { + union + { uint64 md5_count64; uint8 md5_count8[8]; - } md5_count; + } md5_count; #define md5_n md5_count.md5_count64 #define md5_n8 md5_count.md5_count8 - unsigned int md5_i; - uint8 md5_buf[MD5_BUFLEN]; -} md5_ctxt; + unsigned int md5_i; + uint8 md5_buf[MD5_BUFLEN]; +} md5_ctxt; -extern void md5_init (md5_ctxt *); -extern void md5_loop (md5_ctxt *, uint8 *, unsigned int); -extern void md5_pad (md5_ctxt *); -extern void md5_result (uint8 *, md5_ctxt *); +extern void md5_init(md5_ctxt *); +extern void md5_loop(md5_ctxt *, uint8 *, unsigned int); +extern void md5_pad(md5_ctxt *); +extern void md5_result(uint8 *, md5_ctxt *); /* compatibility */ #define MD5_CTX md5_ctxt @@ -73,4 +76,4 @@ do { \ md5_result((x), (y)); \ } while (0) -#endif /* ! _NETINET6_MD5_H_*/ +#endif /* ! _NETINET6_MD5_H_ */ diff --git a/contrib/pgcrypto/mhash.c b/contrib/pgcrypto/mhash.c index 34ac7e9c7a..d6ada96d14 100644 --- a/contrib/pgcrypto/mhash.c +++ b/contrib/pgcrypto/mhash.c @@ -1,7 +1,7 @@ /* * mhash.c * Wrapper for mhash library. - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: mhash.c,v 1.2 2001/02/10 02:31:26 tgl Exp $ + * $Id: mhash.c,v 1.3 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -36,45 +36,50 @@ #include <mhash.h> static uint -pg_mhash_len(pg_digest *hash); -static uint8 * -pg_mhash_digest(pg_digest *hash, uint8 *src, - uint len, uint8 *buf); + pg_mhash_len(pg_digest * hash); +static uint8 *pg_mhash_digest(pg_digest * hash, uint8 *src, + uint len, uint8 *buf); static uint -pg_mhash_len(pg_digest *h) { +pg_mhash_len(pg_digest * h) +{ return mhash_get_block_size(h->misc.code); } static uint8 * -pg_mhash_digest(pg_digest *h, uint8 *src, uint len, uint8 *dst) +pg_mhash_digest(pg_digest * h, uint8 *src, uint len, uint8 *dst) { - uint8 *res; - - MHASH mh = mhash_init(h->misc.code); + uint8 *res; + + MHASH mh = mhash_init(h->misc.code); + mhash(mh, src, len); res = mhash_end(mh); - + memcpy(dst, res, mhash_get_block_size(h->misc.code)); mhash_free(res); - + return dst; } -pg_digest * -pg_find_digest(pg_digest *h, char *name) +pg_digest * +pg_find_digest(pg_digest * h, char *name) { - size_t hnum, i, b; - char *mname; - + size_t hnum, + i, + b; + char *mname; + hnum = mhash_count(); - for (i = 0; i <= hnum; i++) { + for (i = 0; i <= hnum; i++) + { mname = mhash_get_hash_name(i); if (mname == NULL) continue; b = strcasecmp(name, mname); free(mname); - if (!b) { + if (!b) + { h->name = mhash_get_hash_name(i); h->length = pg_mhash_len; h->digest = pg_mhash_digest; @@ -84,4 +89,3 @@ pg_find_digest(pg_digest *h, char *name) } return NULL; } - diff --git a/contrib/pgcrypto/openssl.c b/contrib/pgcrypto/openssl.c index 18fb075488..866d26f0ff 100644 --- a/contrib/pgcrypto/openssl.c +++ b/contrib/pgcrypto/openssl.c @@ -1,7 +1,7 @@ /* * openssl.c * Wrapper for OpenSSL library. - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: openssl.c,v 1.2 2001/02/10 02:31:26 tgl Exp $ + * $Id: openssl.c,v 1.3 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -36,50 +36,50 @@ #include <evp.h> static uint -pg_ossl_len(pg_digest *h); + pg_ossl_len(pg_digest * h); static uint8 * -pg_ossl_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf); + pg_ossl_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf); static uint -pg_ossl_len(pg_digest *h) { - return EVP_MD_size((EVP_MD*)h->misc.ptr); +pg_ossl_len(pg_digest * h) +{ + return EVP_MD_size((EVP_MD *) h->misc.ptr); } static uint8 * -pg_ossl_digest(pg_digest *h, uint8 *src, uint len, uint8 *buf) +pg_ossl_digest(pg_digest * h, uint8 *src, uint len, uint8 *buf) { - EVP_MD *md = (EVP_MD*)h->misc.ptr; - EVP_MD_CTX ctx; + EVP_MD *md = (EVP_MD *) h->misc.ptr; + EVP_MD_CTX ctx; EVP_DigestInit(&ctx, md); EVP_DigestUpdate(&ctx, src, len); EVP_DigestFinal(&ctx, buf, NULL); - + return buf; } -static int pg_openssl_initialized = 0; +static int pg_openssl_initialized = 0; -pg_digest * -pg_find_digest(pg_digest *h, char *name) +pg_digest * +pg_find_digest(pg_digest * h, char *name) { const EVP_MD *md; - if (!pg_openssl_initialized) { + if (!pg_openssl_initialized) + { OpenSSL_add_all_digests(); pg_openssl_initialized = 1; } - + md = EVP_get_digestbyname(name); if (md == NULL) return NULL; - + h->name = name; h->length = pg_ossl_len; h->digest = pg_ossl_digest; - h->misc.ptr = (void*)md; - + h->misc.ptr = (void *) md; + return h; } - - diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c index 1feb3e4806..31e5a845bd 100644 --- a/contrib/pgcrypto/pgcrypto.c +++ b/contrib/pgcrypto/pgcrypto.c @@ -1,7 +1,7 @@ /* * pgcrypto.c * Cryptographic digests for PostgreSQL. - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: pgcrypto.c,v 1.6 2001/02/10 02:31:26 tgl Exp $ + * $Id: pgcrypto.c,v 1.7 2001/03/22 03:59:10 momjian Exp $ */ #include "postgres.h" @@ -44,12 +44,12 @@ /* exported functions */ -Datum digest(PG_FUNCTION_ARGS); -Datum digest_exists(PG_FUNCTION_ARGS); +Datum digest(PG_FUNCTION_ARGS); +Datum digest_exists(PG_FUNCTION_ARGS); /* private stuff */ static pg_digest * -find_digest(pg_digest *hbuf, text *name, int silent); + find_digest(pg_digest * hbuf, text *name, int silent); /* SQL function: hash(text, text) returns text */ @@ -58,31 +58,33 @@ PG_FUNCTION_INFO_V1(digest); Datum digest(PG_FUNCTION_ARGS) { - text *arg; - text *name; - uint len, hlen; - pg_digest *h, _hbuf; - text *res; - + text *arg; + text *name; + uint len, + hlen; + pg_digest *h, + _hbuf; + text *res; + if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) PG_RETURN_NULL(); - - name = PG_GETARG_TEXT_P(1); - h = find_digest(&_hbuf, name, 0); /* will give error if fails */ + + name = PG_GETARG_TEXT_P(1); + h = find_digest(&_hbuf, name, 0); /* will give error if fails */ hlen = h->length(h); - - res = (text *)palloc(hlen + VARHDRSZ); + + res = (text *) palloc(hlen + VARHDRSZ); VARATT_SIZEP(res) = hlen + VARHDRSZ; - + arg = PG_GETARG_TEXT_P(0); len = VARSIZE(arg) - VARHDRSZ; - + h->digest(h, VARDATA(arg), len, VARDATA(res)); - + PG_FREE_IF_COPY(arg, 0); PG_FREE_IF_COPY(name, 1); - + PG_RETURN_TEXT_P(res); } @@ -92,16 +94,17 @@ PG_FUNCTION_INFO_V1(digest_exists); Datum digest_exists(PG_FUNCTION_ARGS) { - text *name; - pg_digest _hbuf, *res; + text *name; + pg_digest _hbuf, + *res; if (PG_ARGISNULL(0)) PG_RETURN_NULL(); - + name = PG_GETARG_TEXT_P(0); - + res = find_digest(&_hbuf, name, 1); - + PG_FREE_IF_COPY(name, 0); if (res != NULL) @@ -110,26 +113,26 @@ digest_exists(PG_FUNCTION_ARGS) } static pg_digest * -find_digest(pg_digest *hbuf, text *name, int silent) +find_digest(pg_digest * hbuf, text *name, int silent) { - pg_digest *p; - char buf[NAMEDATALEN]; - uint len; - + pg_digest *p; + char buf[NAMEDATALEN]; + uint len; + len = VARSIZE(name) - VARHDRSZ; - if (len >= NAMEDATALEN) { + if (len >= NAMEDATALEN) + { if (silent) return NULL; elog(ERROR, "Hash type does not exist (name too long)"); } - + memcpy(buf, VARDATA(name), len); buf[len] = 0; - + p = pg_find_digest(hbuf, buf); if (p == NULL && !silent) elog(ERROR, "Hash type does not exist: '%s'", buf); return p; } - diff --git a/contrib/pgcrypto/pgcrypto.h b/contrib/pgcrypto/pgcrypto.h index 039d6cef79..1700b89b15 100644 --- a/contrib/pgcrypto/pgcrypto.h +++ b/contrib/pgcrypto/pgcrypto.h @@ -1,7 +1,7 @@ /* * pgcrypto.h * Header file for pgcrypto. - * + * * Copyright (c) 2000 Marko Kreen * All rights reserved. * @@ -9,15 +9,15 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -26,25 +26,27 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: pgcrypto.h,v 1.2 2001/01/09 16:07:13 momjian Exp $ + * $Id: pgcrypto.h,v 1.3 2001/03/22 03:59:10 momjian Exp $ */ #ifndef _PG_CRYPTO_H #define _PG_CRYPTO_H typedef struct _pg_digest pg_digest; -struct _pg_digest { - char *name; - uint (*length)(pg_digest *h); - uint8 *(*digest)(pg_digest *h, uint8 *data, - uint dlen, uint8 *buf); +struct _pg_digest +{ + char *name; + uint (*length) (pg_digest * h); + uint8 *(*digest) (pg_digest * h, uint8 *data, + uint dlen, uint8 *buf); /* private */ - union { - uint code; + union + { + uint code; const void *ptr; - } misc; + } misc; }; -extern pg_digest *pg_find_digest(pg_digest *hbuf, char *name); +extern pg_digest *pg_find_digest(pg_digest * hbuf, char *name); #endif diff --git a/contrib/pgcrypto/sha1.c b/contrib/pgcrypto/sha1.c index 63c28dc7e9..db9f1a17c6 100644 --- a/contrib/pgcrypto/sha1.c +++ b/contrib/pgcrypto/sha1.c @@ -1,5 +1,5 @@ -/* $Id: sha1.c,v 1.4 2001/02/10 02:31:26 tgl Exp $ */ -/* $KAME: sha1.c,v 1.3 2000/02/22 14:01:18 itojun Exp $ */ +/* $Id: sha1.c,v 1.5 2001/03/22 03:59:10 momjian Exp $ */ +/* $KAME: sha1.c,v 1.3 2000/02/22 14:01:18 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -9,18 +9,18 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -41,120 +41,182 @@ /* sanity check */ #if BYTE_ORDER != BIG_ENDIAN -# if BYTE_ORDER != LITTLE_ENDIAN -# define unsupported 1 -# endif +#if BYTE_ORDER != LITTLE_ENDIAN +#define unsupported 1 +#endif #endif #ifndef unsupported /* constant table */ -static uint32 _K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 }; -#define K(t) _K[(t) / 20] +static uint32 _K[] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6}; + +#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 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 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 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 PUTBYTE(x) { \ ctxt->m.b8[(COUNT % 64)] = (x); \ COUNT++; \ COUNT %= 64; \ ctxt->c.b64[0] += 8; \ if (COUNT % 64 == 0) \ sha1_step(ctxt); \ - } + } -#define PUTPAD(x) { \ +#define PUTPAD(x) { \ ctxt->m.b8[(COUNT % 64)] = (x); \ COUNT++; \ COUNT %= 64; \ if (COUNT % 64 == 0) \ sha1_step(ctxt); \ - } + } -static void sha1_step (struct sha1_ctxt *); +static void sha1_step(struct sha1_ctxt *); static void sha1_step(ctxt) - struct sha1_ctxt *ctxt; +struct sha1_ctxt *ctxt; { - uint32 a, b, c, d, e; - size_t t, s; - uint32 tmp; + uint32 a, + b, + c, + d, + e; + size_t t, + s; + uint32 tmp; #if BYTE_ORDER == LITTLE_ENDIAN struct sha1_ctxt tctxt; + bcopy(&ctxt->m.b8[0], &tctxt.m.b8[0], 64); - ctxt->m.b8[0] = tctxt.m.b8[3]; ctxt->m.b8[1] = tctxt.m.b8[2]; - ctxt->m.b8[2] = tctxt.m.b8[1]; ctxt->m.b8[3] = tctxt.m.b8[0]; - ctxt->m.b8[4] = tctxt.m.b8[7]; ctxt->m.b8[5] = tctxt.m.b8[6]; - ctxt->m.b8[6] = tctxt.m.b8[5]; ctxt->m.b8[7] = tctxt.m.b8[4]; - ctxt->m.b8[8] = tctxt.m.b8[11]; ctxt->m.b8[9] = tctxt.m.b8[10]; - ctxt->m.b8[10] = tctxt.m.b8[9]; ctxt->m.b8[11] = tctxt.m.b8[8]; - ctxt->m.b8[12] = tctxt.m.b8[15]; ctxt->m.b8[13] = tctxt.m.b8[14]; - ctxt->m.b8[14] = tctxt.m.b8[13]; ctxt->m.b8[15] = tctxt.m.b8[12]; - ctxt->m.b8[16] = tctxt.m.b8[19]; ctxt->m.b8[17] = tctxt.m.b8[18]; - ctxt->m.b8[18] = tctxt.m.b8[17]; ctxt->m.b8[19] = tctxt.m.b8[16]; - ctxt->m.b8[20] = tctxt.m.b8[23]; ctxt->m.b8[21] = tctxt.m.b8[22]; - ctxt->m.b8[22] = tctxt.m.b8[21]; ctxt->m.b8[23] = tctxt.m.b8[20]; - ctxt->m.b8[24] = tctxt.m.b8[27]; ctxt->m.b8[25] = tctxt.m.b8[26]; - ctxt->m.b8[26] = tctxt.m.b8[25]; ctxt->m.b8[27] = tctxt.m.b8[24]; - ctxt->m.b8[28] = tctxt.m.b8[31]; ctxt->m.b8[29] = tctxt.m.b8[30]; - ctxt->m.b8[30] = tctxt.m.b8[29]; ctxt->m.b8[31] = tctxt.m.b8[28]; - ctxt->m.b8[32] = tctxt.m.b8[35]; ctxt->m.b8[33] = tctxt.m.b8[34]; - ctxt->m.b8[34] = tctxt.m.b8[33]; ctxt->m.b8[35] = tctxt.m.b8[32]; - ctxt->m.b8[36] = tctxt.m.b8[39]; ctxt->m.b8[37] = tctxt.m.b8[38]; - ctxt->m.b8[38] = tctxt.m.b8[37]; ctxt->m.b8[39] = tctxt.m.b8[36]; - ctxt->m.b8[40] = tctxt.m.b8[43]; ctxt->m.b8[41] = tctxt.m.b8[42]; - ctxt->m.b8[42] = tctxt.m.b8[41]; ctxt->m.b8[43] = tctxt.m.b8[40]; - ctxt->m.b8[44] = tctxt.m.b8[47]; ctxt->m.b8[45] = tctxt.m.b8[46]; - ctxt->m.b8[46] = tctxt.m.b8[45]; ctxt->m.b8[47] = tctxt.m.b8[44]; - ctxt->m.b8[48] = tctxt.m.b8[51]; ctxt->m.b8[49] = tctxt.m.b8[50]; - ctxt->m.b8[50] = tctxt.m.b8[49]; ctxt->m.b8[51] = tctxt.m.b8[48]; - ctxt->m.b8[52] = tctxt.m.b8[55]; ctxt->m.b8[53] = tctxt.m.b8[54]; - ctxt->m.b8[54] = tctxt.m.b8[53]; ctxt->m.b8[55] = tctxt.m.b8[52]; - ctxt->m.b8[56] = tctxt.m.b8[59]; ctxt->m.b8[57] = tctxt.m.b8[58]; - ctxt->m.b8[58] = tctxt.m.b8[57]; ctxt->m.b8[59] = tctxt.m.b8[56]; - ctxt->m.b8[60] = tctxt.m.b8[63]; ctxt->m.b8[61] = tctxt.m.b8[62]; - ctxt->m.b8[62] = tctxt.m.b8[61]; ctxt->m.b8[63] = tctxt.m.b8[60]; + ctxt->m.b8[0] = tctxt.m.b8[3]; + ctxt->m.b8[1] = tctxt.m.b8[2]; + ctxt->m.b8[2] = tctxt.m.b8[1]; + ctxt->m.b8[3] = tctxt.m.b8[0]; + ctxt->m.b8[4] = tctxt.m.b8[7]; + ctxt->m.b8[5] = tctxt.m.b8[6]; + ctxt->m.b8[6] = tctxt.m.b8[5]; + ctxt->m.b8[7] = tctxt.m.b8[4]; + ctxt->m.b8[8] = tctxt.m.b8[11]; + ctxt->m.b8[9] = tctxt.m.b8[10]; + ctxt->m.b8[10] = tctxt.m.b8[9]; + ctxt->m.b8[11] = tctxt.m.b8[8]; + ctxt->m.b8[12] = tctxt.m.b8[15]; + ctxt->m.b8[13] = tctxt.m.b8[14]; + ctxt->m.b8[14] = tctxt.m.b8[13]; + ctxt->m.b8[15] = tctxt.m.b8[12]; + ctxt->m.b8[16] = tctxt.m.b8[19]; + ctxt->m.b8[17] = tctxt.m.b8[18]; + ctxt->m.b8[18] = tctxt.m.b8[17]; + ctxt->m.b8[19] = tctxt.m.b8[16]; + ctxt->m.b8[20] = tctxt.m.b8[23]; + ctxt->m.b8[21] = tctxt.m.b8[22]; + ctxt->m.b8[22] = tctxt.m.b8[21]; + ctxt->m.b8[23] = tctxt.m.b8[20]; + ctxt->m.b8[24] = tctxt.m.b8[27]; + ctxt->m.b8[25] = tctxt.m.b8[26]; + ctxt->m.b8[26] = tctxt.m.b8[25]; + ctxt->m.b8[27] = tctxt.m.b8[24]; + ctxt->m.b8[28] = tctxt.m.b8[31]; + ctxt->m.b8[29] = tctxt.m.b8[30]; + ctxt->m.b8[30] = tctxt.m.b8[29]; + ctxt->m.b8[31] = tctxt.m.b8[28]; + ctxt->m.b8[32] = tctxt.m.b8[35]; + ctxt->m.b8[33] = tctxt.m.b8[34]; + ctxt->m.b8[34] = tctxt.m.b8[33]; + ctxt->m.b8[35] = tctxt.m.b8[32]; + ctxt->m.b8[36] = tctxt.m.b8[39]; + ctxt->m.b8[37] = tctxt.m.b8[38]; + ctxt->m.b8[38] = tctxt.m.b8[37]; + ctxt->m.b8[39] = tctxt.m.b8[36]; + ctxt->m.b8[40] = tctxt.m.b8[43]; + ctxt->m.b8[41] = tctxt.m.b8[42]; + ctxt->m.b8[42] = tctxt.m.b8[41]; + ctxt->m.b8[43] = tctxt.m.b8[40]; + ctxt->m.b8[44] = tctxt.m.b8[47]; + ctxt->m.b8[45] = tctxt.m.b8[46]; + ctxt->m.b8[46] = tctxt.m.b8[45]; + ctxt->m.b8[47] = tctxt.m.b8[44]; + ctxt->m.b8[48] = tctxt.m.b8[51]; + ctxt->m.b8[49] = tctxt.m.b8[50]; + ctxt->m.b8[50] = tctxt.m.b8[49]; + ctxt->m.b8[51] = tctxt.m.b8[48]; + ctxt->m.b8[52] = tctxt.m.b8[55]; + ctxt->m.b8[53] = tctxt.m.b8[54]; + ctxt->m.b8[54] = tctxt.m.b8[53]; + ctxt->m.b8[55] = tctxt.m.b8[52]; + ctxt->m.b8[56] = tctxt.m.b8[59]; + ctxt->m.b8[57] = tctxt.m.b8[58]; + ctxt->m.b8[58] = tctxt.m.b8[57]; + ctxt->m.b8[59] = tctxt.m.b8[56]; + ctxt->m.b8[60] = tctxt.m.b8[63]; + ctxt->m.b8[61] = tctxt.m.b8[62]; + ctxt->m.b8[62] = tctxt.m.b8[61]; + ctxt->m.b8[63] = tctxt.m.b8[60]; #endif - a = H(0); b = H(1); c = H(2); d = H(3); e = H(4); + a = H(0); + b = H(1); + c = H(2); + d = H(3); + e = H(4); - for (t = 0; t < 20; t++) { + for (t = 0; t < 20; t++) + { s = t & 0x0f; - if (t >= 16) { - W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s)); - } + if (t >= 16) + W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s)); tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t); - e = d; d = c; c = S(30, b); b = a; a = tmp; + e = d; + d = c; + c = S(30, b); + b = a; + a = tmp; } - for (t = 20; t < 40; t++) { + for (t = 20; t < 40; t++) + { s = t & 0x0f; - W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s)); + W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s)); tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t); - e = d; d = c; c = S(30, b); b = a; a = tmp; + e = d; + d = c; + c = S(30, b); + b = a; + a = tmp; } - for (t = 40; t < 60; t++) { + for (t = 40; t < 60; t++) + { s = t & 0x0f; - W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s)); + W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s)); tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t); - e = d; d = c; c = S(30, b); b = a; a = tmp; + e = d; + d = c; + c = S(30, b); + b = a; + a = tmp; } - for (t = 60; t < 80; t++) { + for (t = 60; t < 80; t++) + { s = t & 0x0f; - W(s) = S(1, W((s+13) & 0x0f) ^ W((s+8) & 0x0f) ^ W((s+2) & 0x0f) ^ W(s)); + W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s)); tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t); - e = d; d = c; c = S(30, b); b = a; a = tmp; + e = d; + d = c; + c = S(30, b); + b = a; + a = tmp; } H(0) = H(0) + a; @@ -170,7 +232,7 @@ sha1_step(ctxt) void sha1_init(ctxt) - struct sha1_ctxt *ctxt; +struct sha1_ctxt *ctxt; { bzero(ctxt, sizeof(struct sha1_ctxt)); H(0) = 0x67452301; @@ -182,55 +244,65 @@ sha1_init(ctxt) void sha1_pad(ctxt) - struct sha1_ctxt *ctxt; +struct sha1_ctxt *ctxt; { - size_t padlen; /*pad length in bytes*/ - size_t padstart; + size_t padlen; /* pad length in bytes */ + size_t padstart; PUTPAD(0x80); padstart = COUNT % 64; padlen = 64 - padstart; - if (padlen < 8) { + if (padlen < 8) + { bzero(&ctxt->m.b8[padstart], padlen); COUNT += padlen; COUNT %= 64; sha1_step(ctxt); padstart = COUNT % 64; /* should be 0 */ - padlen = 64 - padstart; /* should be 64 */ + padlen = 64 - padstart; /* should be 64 */ } bzero(&ctxt->m.b8[padstart], padlen - 8); COUNT += (padlen - 8); COUNT %= 64; #if BYTE_ORDER == BIG_ENDIAN - PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]); - PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]); - PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]); - PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]); + PUTPAD(ctxt->c.b8[0]); + PUTPAD(ctxt->c.b8[1]); + PUTPAD(ctxt->c.b8[2]); + PUTPAD(ctxt->c.b8[3]); + PUTPAD(ctxt->c.b8[4]); + PUTPAD(ctxt->c.b8[5]); + PUTPAD(ctxt->c.b8[6]); + PUTPAD(ctxt->c.b8[7]); #else - PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]); - PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]); - PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]); - PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]); + PUTPAD(ctxt->c.b8[7]); + PUTPAD(ctxt->c.b8[6]); + PUTPAD(ctxt->c.b8[5]); + PUTPAD(ctxt->c.b8[4]); + PUTPAD(ctxt->c.b8[3]); + PUTPAD(ctxt->c.b8[2]); + PUTPAD(ctxt->c.b8[1]); + PUTPAD(ctxt->c.b8[0]); #endif } void sha1_loop(ctxt, input0, len) - struct sha1_ctxt *ctxt; - const caddr_t input0; - size_t len; +struct sha1_ctxt *ctxt; +const caddr_t input0; +size_t len; { const uint8 *input; - size_t gaplen; - size_t gapstart; - size_t off; - size_t copysiz; + size_t gaplen; + size_t gapstart; + size_t off; + size_t copysiz; - input = (const uint8 *)input0; + input = (const uint8 *) input0; off = 0; - while (off < len) { + while (off < len) + { gapstart = COUNT % 64; gaplen = 64 - gapstart; @@ -247,27 +319,37 @@ sha1_loop(ctxt, input0, len) void sha1_result(ctxt, digest0) - struct sha1_ctxt *ctxt; - caddr_t digest0; +struct sha1_ctxt *ctxt; +caddr_t digest0; { - uint8 *digest; + uint8 *digest; - digest = (uint8 *)digest0; + digest = (uint8 *) digest0; sha1_pad(ctxt); #if BYTE_ORDER == BIG_ENDIAN bcopy(&ctxt->h.b8[0], digest, 20); #else - digest[0] = ctxt->h.b8[3]; digest[1] = ctxt->h.b8[2]; - digest[2] = ctxt->h.b8[1]; digest[3] = ctxt->h.b8[0]; - digest[4] = ctxt->h.b8[7]; digest[5] = ctxt->h.b8[6]; - digest[6] = ctxt->h.b8[5]; digest[7] = ctxt->h.b8[4]; - digest[8] = ctxt->h.b8[11]; digest[9] = ctxt->h.b8[10]; - digest[10] = ctxt->h.b8[9]; digest[11] = ctxt->h.b8[8]; - digest[12] = ctxt->h.b8[15]; digest[13] = ctxt->h.b8[14]; - digest[14] = ctxt->h.b8[13]; digest[15] = ctxt->h.b8[12]; - digest[16] = ctxt->h.b8[19]; digest[17] = ctxt->h.b8[18]; - digest[18] = ctxt->h.b8[17]; digest[19] = ctxt->h.b8[16]; + digest[0] = ctxt->h.b8[3]; + digest[1] = ctxt->h.b8[2]; + digest[2] = ctxt->h.b8[1]; + digest[3] = ctxt->h.b8[0]; + digest[4] = ctxt->h.b8[7]; + digest[5] = ctxt->h.b8[6]; + digest[6] = ctxt->h.b8[5]; + digest[7] = ctxt->h.b8[4]; + digest[8] = ctxt->h.b8[11]; + digest[9] = ctxt->h.b8[10]; + digest[10] = ctxt->h.b8[9]; + digest[11] = ctxt->h.b8[8]; + digest[12] = ctxt->h.b8[15]; + digest[13] = ctxt->h.b8[14]; + digest[14] = ctxt->h.b8[13]; + digest[15] = ctxt->h.b8[12]; + digest[16] = ctxt->h.b8[19]; + digest[17] = ctxt->h.b8[18]; + digest[18] = ctxt->h.b8[17]; + digest[19] = ctxt->h.b8[16]; #endif } -#endif /*unsupported*/ +#endif /* unsupported */ diff --git a/contrib/pgcrypto/sha1.h b/contrib/pgcrypto/sha1.h index cb08a1af83..cddc8651e4 100644 --- a/contrib/pgcrypto/sha1.h +++ b/contrib/pgcrypto/sha1.h @@ -1,5 +1,5 @@ -/* $Id: sha1.h,v 1.3 2001/01/09 16:07:13 momjian Exp $ */ -/* $KAME: sha1.h,v 1.4 2000/02/22 14:01:18 itojun Exp $ */ +/* $Id: sha1.h,v 1.4 2001/03/22 03:59:10 momjian Exp $ */ +/* $KAME: sha1.h,v 1.4 2000/02/22 14:01:18 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -9,18 +9,18 @@ * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) @@ -38,33 +38,38 @@ #ifndef _NETINET6_SHA1_H_ #define _NETINET6_SHA1_H_ -struct sha1_ctxt { - union { +struct sha1_ctxt +{ + union + { uint8 b8[20]; uint32 b32[5]; - } h; - union { + } h; + union + { uint8 b8[8]; uint64 b64[1]; - } c; - union { + } c; + union + { uint8 b8[64]; uint32 b32[16]; - } m; - uint8 count; + } m; + uint8 count; }; -extern void sha1_init (struct sha1_ctxt *); -extern void sha1_pad (struct sha1_ctxt *); -extern void sha1_loop (struct sha1_ctxt *, const caddr_t, size_t); -extern void sha1_result (struct sha1_ctxt *, caddr_t); +extern void sha1_init(struct sha1_ctxt *); +extern void sha1_pad(struct sha1_ctxt *); +extern void sha1_loop(struct sha1_ctxt *, const caddr_t, size_t); +extern void sha1_result(struct sha1_ctxt *, caddr_t); /* compatibilty with other SHA1 source codes */ typedef struct sha1_ctxt SHA1_CTX; + #define SHA1Init(x) sha1_init((x)) -#define SHA1Update(x, y, z) sha1_loop((x), (y), (z)) +#define SHA1Update(x, y, z) sha1_loop((x), (y), (z)) #define SHA1Final(x, y) sha1_result((y), (x)) -#define SHA1_RESULTLEN (160/8) +#define SHA1_RESULTLEN (160/8) -#endif /*_NETINET6_SHA1_H_*/ +#endif /* _NETINET6_SHA1_H_ */ |
