summaryrefslogtreecommitdiff
path: root/contrib/pgcrypto/px.c
diff options
context:
space:
mode:
authorBruce Momjian2001-10-25 05:50:21 +0000
committerBruce Momjian2001-10-25 05:50:21 +0000
commitb81844b1738c584d92330a5ccd0fbd8b603d2886 (patch)
tree4fae0d4cd26048177fc5cd1a2dd91abc99ba0f99 /contrib/pgcrypto/px.c
parent59da2105d8e6d95345b3b942a2e2aba8cead4838 (diff)
pgindent run on all C files. Java run to follow. initdb/regression
tests pass.
Diffstat (limited to 'contrib/pgcrypto/px.c')
-rw-r--r--contrib/pgcrypto/px.c164
1 files changed, 98 insertions, 66 deletions
diff --git a/contrib/pgcrypto/px.c b/contrib/pgcrypto/px.c
index 9c18f6f250e..20550bf1003 100644
--- a/contrib/pgcrypto/px.c
+++ b/contrib/pgcrypto/px.c
@@ -1,7 +1,7 @@
/*
* px.c
* Various cryptographic stuff for PostgreSQL.
- *
+ *
* 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: px.c,v 1.2 2001/09/06 03:21:39 momjian Exp $
+ * $Id: px.c,v 1.3 2001/10/25 05:49:20 momjian Exp $
*/
#include <postgres.h>
@@ -35,9 +35,10 @@
const char *
-px_resolve_alias(const PX_Alias *list, const char *name)
+px_resolve_alias(const PX_Alias * list, const char *name)
{
- while (list->name) {
+ while (list->name)
+ {
if (!strcasecmp(list->alias, name))
return list->name;
list++;
@@ -50,32 +51,35 @@ px_resolve_alias(const PX_Alias *list, const char *name)
*/
static uint
-combo_encrypt_len(PX_Combo *cx, uint dlen)
+combo_encrypt_len(PX_Combo * cx, uint dlen)
{
return dlen + 512;
}
static uint
-combo_decrypt_len(PX_Combo *cx, uint dlen)
+combo_decrypt_len(PX_Combo * cx, uint dlen)
{
return dlen;
}
static int
-combo_init(PX_Combo *cx, const uint8 *key, uint klen,
- const uint8 *iv, uint ivlen)
+combo_init(PX_Combo * cx, const uint8 *key, uint klen,
+ const uint8 *iv, uint ivlen)
{
- int err;
- uint bs, ks, ivs;
- PX_Cipher *c = cx->cipher;
- uint8 *ivbuf = NULL;
- uint8 *keybuf;
+ int err;
+ uint bs,
+ ks,
+ ivs;
+ PX_Cipher *c = cx->cipher;
+ uint8 *ivbuf = NULL;
+ uint8 *keybuf;
bs = px_cipher_block_size(c);
ks = px_cipher_key_size(c);
ivs = px_cipher_iv_size(c);
- if (ivs > 0) {
+ if (ivs > 0)
+ {
ivbuf = px_alloc(ivs);
memset(ivbuf, 0, ivs);
if (ivlen > ivs)
@@ -97,39 +101,48 @@ combo_init(PX_Combo *cx, const uint8 *key, uint klen,
}
static int
-combo_encrypt(PX_Combo *cx, const uint8 *data, uint dlen,
- uint8 *res, uint *rlen)
+combo_encrypt(PX_Combo * cx, const uint8 *data, uint dlen,
+ uint8 *res, uint *rlen)
{
- int err = 0;
- uint8 *bbuf;
- uint bs, maxlen, bpos, i, pad;
+ int err = 0;
+ uint8 *bbuf;
+ uint bs,
+ maxlen,
+ bpos,
+ i,
+ pad;
- PX_Cipher *c = cx->cipher;
+ PX_Cipher *c = cx->cipher;
bbuf = NULL;
maxlen = *rlen;
bs = px_cipher_block_size(c);
-
+
/* encrypt */
- if (bs > 1) {
+ if (bs > 1)
+ {
bbuf = px_alloc(bs * 4);
bpos = dlen % bs;
*rlen = dlen - bpos;
memcpy(bbuf, data + *rlen, bpos);
/* encrypt full-block data */
- if (*rlen) {
+ if (*rlen)
+ {
err = px_cipher_encrypt(c, data, *rlen, res);
if (err)
goto out;
}
-
+
/* bbuf has now bpos bytes of stuff */
- if (cx->padding) {
+ if (cx->padding)
+ {
pad = bs - (bpos % bs);
for (i = 0; i < pad; i++)
bbuf[bpos++] = pad;
- } else if (bpos % bs) {
+ }
+ else if (bpos % bs)
+ {
/* ERROR? */
pad = bs - (bpos % bs);
for (i = 0; i < pad; i++)
@@ -137,11 +150,14 @@ combo_encrypt(PX_Combo *cx, const uint8 *data, uint dlen,
}
/* encrypt the rest - pad */
- if (bpos) {
+ if (bpos)
+ {
err = px_cipher_encrypt(c, bbuf, bpos, res + *rlen);
*rlen += bpos;
}
- } else {
+ }
+ else
+ {
/* stream cipher/mode - no pad needed */
err = px_cipher_encrypt(c, data, dlen, res);
if (err)
@@ -149,48 +165,53 @@ combo_encrypt(PX_Combo *cx, const uint8 *data, uint dlen,
*rlen = dlen;
}
out:
- if (bbuf) px_free(bbuf);
+ if (bbuf)
+ px_free(bbuf);
return err;
}
static int
-combo_decrypt(PX_Combo *cx, const uint8 *data, uint dlen,
- uint8 *res, uint *rlen)
+combo_decrypt(PX_Combo * cx, const uint8 *data, uint dlen,
+ uint8 *res, uint *rlen)
{
- uint bs, i, pad;
- uint pad_ok;
+ uint bs,
+ i,
+ pad;
+ uint pad_ok;
- PX_Cipher *c = cx->cipher;
+ PX_Cipher *c = cx->cipher;
bs = px_cipher_block_size(c);
- if (bs > 1 && (dlen % bs) != 0) {
+ if (bs > 1 && (dlen % bs) != 0)
goto block_error;
- }
-
+
/* decrypt */
*rlen = dlen;
px_cipher_decrypt(c, data, dlen, res);
-
+
/* unpad */
- if (bs > 1 && cx->padding) {
+ if (bs > 1 && cx->padding)
+ {
pad = res[*rlen - 1];
pad_ok = 0;
- if (pad > 0 && pad <= bs && pad <= *rlen) {
+ if (pad > 0 && pad <= bs && pad <= *rlen)
+ {
pad_ok = 1;
for (i = *rlen - pad; i < *rlen; i++)
- if (res[i] != pad) {
+ if (res[i] != pad)
+ {
pad_ok = 0;
break;
}
}
-
+
if (pad_ok)
*rlen -= pad;
}
return 0;
-
+
/* error reporting should be done in pgcrypto.c */
block_error:
elog(NOTICE, "Data not a multiple of block size");
@@ -198,7 +219,7 @@ block_error:
}
static void
-combo_free(PX_Combo *cx)
+combo_free(PX_Combo * cx)
{
if (cx->cipher)
px_cipher_free(cx->cipher);
@@ -211,31 +232,38 @@ combo_free(PX_Combo *cx)
static int
parse_cipher_name(char *full, char **cipher, char **pad)
{
- char *p, *p2, *q;
+ char *p,
+ *p2,
+ *q;
+
*cipher = full;
*pad = NULL;
-
+
p = strchr(full, '/');
if (p != NULL)
*p++ = 0;
- while (p != NULL) {
+ while (p != NULL)
+ {
if ((q = strchr(p, '/')) != NULL)
*q++ = 0;
-
- if (!*p) {
+
+ if (!*p)
+ {
p = q;
continue;
}
p2 = strchr(p, ':');
- if (p2 != NULL) {
+ if (p2 != NULL)
+ {
*p2++ = 0;
if (!strcmp(p, "pad"))
*pad = p2;
else
return -1;
- } else
+ }
+ else
return -1;
-
+
p = q;
}
return 0;
@@ -244,12 +272,14 @@ parse_cipher_name(char *full, char **cipher, char **pad)
/* provider */
int
-px_find_combo(const char *name, PX_Combo **res)
+px_find_combo(const char *name, PX_Combo ** res)
{
- int err;
- char *buf, *s_cipher, *s_pad;
+ int err;
+ char *buf,
+ *s_cipher,
+ *s_pad;
- PX_Combo *cx;
+ PX_Combo *cx;
cx = px_alloc(sizeof(*cx));
memset(cx, 0, sizeof(*cx));
@@ -258,7 +288,8 @@ px_find_combo(const char *name, PX_Combo **res)
strcpy(buf, name);
err = parse_cipher_name(buf, &s_cipher, &s_pad);
- if (err) {
+ if (err)
+ {
px_free(buf);
px_free(cx);
return err;
@@ -267,15 +298,17 @@ px_find_combo(const char *name, PX_Combo **res)
err = px_find_cipher(s_cipher, &cx->cipher);
if (err)
goto err1;
-
- if (s_pad != NULL) {
+
+ if (s_pad != NULL)
+ {
if (!strcmp(s_pad, "pkcs"))
cx->padding = 1;
else if (!strcmp(s_pad, "none"))
cx->padding = 0;
else
goto err1;
- } else
+ }
+ else
cx->padding = 1;
cx->init = combo_init;
@@ -290,7 +323,7 @@ px_find_combo(const char *name, PX_Combo **res)
*res = cx;
return 0;
-
+
err1:
if (cx->cipher)
px_cipher_free(cx->cipher);
@@ -298,4 +331,3 @@ err1:
px_free(buf);
return -1;
}
-