Skip to content

Commit ab458f1

Browse files
committed
refactor code into separate function files for doctest upgrades.
1 parent a998a3b commit ab458f1

File tree

115 files changed

+2904
-2832
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+2904
-2832
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ psql.sh
66
.cache
77
test/
88
example/
9+
site/
10+
docs/
911
.virt

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ PG_CONFIG ?= pg_config
55
DATA = $(wildcard extension/*--*.sql)
66
PGXS := $(shell $(PG_CONFIG) --pgxs)
77
MODULE_big = pgsodium
8-
OBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
8+
OBJS = $(patsubst %.c,%.o,$(shell find pgsodium/ -name '*.c'))
99
SHLIB_LINK = -lsodium
10-
PG_CPPFLAGS = -O0
10+
PG_CPPFLAGS = -O0 -I$(srcdir)/pgsodium/include
1111

1212
TESTS = $(wildcard sql/*.sql)
1313
REGRESS = $(patsubst sql/%.sql,%,$(TESTS))

docgen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22
import sys
33

4+
PREFIX = "/* doctest"
45

56
def process_file(path):
67
with open(path, "r") as f:
78
content = f.read()
89
s = content.lstrip()
9-
if not s.startswith("/* doctest"):
10+
if not s.startswith(PREFIX):
1011
return
1112
end = s.find("*/")
1213
if end == -1:
@@ -16,9 +17,9 @@ def process_file(path):
1617
if not lines:
1718
return
1819
first = lines[0]
19-
if not first.startswith("/* doctest"):
20+
if not first.startswith(PREFIX):
2021
return
21-
target = first[len("/* doctest"):].strip()
22+
target = first[len(PREFIX):].strip()
2223
if target.startswith("/"):
2324
target = target[1:]
2425
if not target:
@@ -30,7 +31,6 @@ def process_file(path):
3031
with open(out_file, "w") as out:
3132
out.write(stripped_comment + "\n\n")
3233

33-
3434
def main():
3535
base = sys.argv[1] if len(sys.argv) > 1 else "."
3636
for root, _, files in os.walk(base):

pgsodium/aead/det_decrypt.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "pgsodium.h"
2+
3+
PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_decrypt);
4+
Datum
5+
pgsodium_crypto_aead_det_decrypt (PG_FUNCTION_ARGS)
6+
{
7+
bytea *ciphertext;
8+
bytea *associated;
9+
bytea *key;
10+
size_t result_len;
11+
bytea *result, *nonce;
12+
int success;
13+
14+
ERRORIF (PG_ARGISNULL (0), "%s: ciphertext cannot be NULL");
15+
ERRORIF (PG_ARGISNULL (2), "%s: key cannot be NULL");
16+
17+
ciphertext = PG_GETARG_BYTEA_PP (0);
18+
if (!PG_ARGISNULL (1))
19+
{
20+
associated = PG_GETARG_BYTEA_PP (1);
21+
}
22+
else
23+
{
24+
associated = NULL;
25+
}
26+
27+
key = PG_GETARG_BYTEA_PP (2);
28+
29+
ERRORIF (VARSIZE_ANY_EXHDR (ciphertext) <=
30+
crypto_aead_det_xchacha20_ABYTES, "%s: invalid message");
31+
ERRORIF (VARSIZE_ANY_EXHDR (key) != crypto_aead_det_xchacha20_KEYBYTES,
32+
"%s: invalid key");
33+
result_len =
34+
VARSIZE_ANY_EXHDR (ciphertext) - crypto_aead_det_xchacha20_ABYTES;
35+
result = _pgsodium_zalloc_bytea (result_len + VARHDRSZ);
36+
if (!PG_ARGISNULL (3))
37+
{
38+
nonce = PG_GETARG_BYTEA_P (3);
39+
ERRORIF (VARSIZE_ANY_EXHDR (nonce) !=
40+
crypto_aead_det_xchacha20_NONCEBYTES, "%s: invalid nonce");
41+
}
42+
else
43+
{
44+
nonce = NULL;
45+
}
46+
success = crypto_aead_det_xchacha20_decrypt (
47+
PGSODIUM_UCHARDATA (result),
48+
PGSODIUM_UCHARDATA_ANY (ciphertext),
49+
VARSIZE_ANY_EXHDR (ciphertext),
50+
associated != NULL ? PGSODIUM_UCHARDATA_ANY (associated) : NULL,
51+
associated != NULL ? VARSIZE_ANY_EXHDR (associated) : 0,
52+
nonce != NULL ? PGSODIUM_UCHARDATA_ANY (nonce) : NULL,
53+
PGSODIUM_UCHARDATA_ANY (key));
54+
ERRORIF (success != 0, "%s: invalid ciphertext");
55+
PG_RETURN_BYTEA_P (result);
56+
}

pgsodium/aead/det_decrypt_by_id.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "pgsodium.h"
2+
3+
PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_decrypt_by_id);
4+
Datum
5+
pgsodium_crypto_aead_det_decrypt_by_id (PG_FUNCTION_ARGS)
6+
{
7+
bytea *ciphertext;
8+
bytea *associated;
9+
unsigned long long key_id;
10+
bytea *context;
11+
size_t result_len;
12+
bytea *key, *result, *nonce;
13+
int success;
14+
15+
16+
ERRORIF (PG_ARGISNULL (0), "%s: message cannot be NULL");
17+
ERRORIF (PG_ARGISNULL (2), "%s: key id cannot be NULL");
18+
ERRORIF (PG_ARGISNULL (3), "%s: key context cannot be NULL");
19+
20+
ciphertext = PG_GETARG_BYTEA_PP (0);
21+
22+
if (!PG_ARGISNULL (1))
23+
{
24+
associated = PG_GETARG_BYTEA_PP (1);
25+
}
26+
else
27+
{
28+
associated = NULL;
29+
}
30+
31+
key_id = PG_GETARG_INT64 (2);
32+
context = PG_GETARG_BYTEA_PP (3);
33+
34+
if (!PG_ARGISNULL (4))
35+
{
36+
nonce = PG_GETARG_BYTEA_PP (4);
37+
ERRORIF (VARSIZE_ANY_EXHDR (nonce) !=
38+
crypto_aead_det_xchacha20_NONCEBYTES, "%s: invalid nonce");
39+
}
40+
else
41+
{
42+
nonce = NULL;
43+
}
44+
ERRORIF (VARSIZE_ANY_EXHDR (ciphertext) <=
45+
crypto_aead_det_xchacha20_ABYTES, "%s: invalid message");
46+
result_len =
47+
VARSIZE_ANY_EXHDR (ciphertext) - crypto_aead_det_xchacha20_ABYTES;
48+
result = _pgsodium_zalloc_bytea (result_len + VARHDRSZ);
49+
key =
50+
pgsodium_derive_helper (key_id, crypto_aead_det_xchacha20_KEYBYTES,
51+
context);
52+
53+
success = crypto_aead_det_xchacha20_decrypt (
54+
PGSODIUM_UCHARDATA (result),
55+
PGSODIUM_UCHARDATA_ANY (ciphertext),
56+
VARSIZE_ANY_EXHDR (ciphertext),
57+
associated != NULL ? PGSODIUM_UCHARDATA_ANY (associated) : NULL,
58+
associated != NULL ? VARSIZE_ANY_EXHDR (associated) : 0,
59+
nonce != NULL ? PGSODIUM_UCHARDATA_ANY (nonce) : NULL,
60+
PGSODIUM_UCHARDATA_ANY (key));
61+
ERRORIF (success != 0, "%s: invalid ciphertext");
62+
PG_RETURN_BYTEA_P (result);
63+
}

pgsodium/aead/det_encrypt.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "pgsodium.h"
2+
3+
PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_encrypt);
4+
Datum
5+
pgsodium_crypto_aead_det_encrypt (PG_FUNCTION_ARGS)
6+
{
7+
bytea *message;
8+
bytea *associated;
9+
bytea *key;
10+
bytea *nonce;
11+
size_t result_size;
12+
bytea *result;
13+
int success;
14+
15+
ERRORIF (PG_ARGISNULL (0), "%s: message cannot be NULL");
16+
ERRORIF (PG_ARGISNULL (2), "%s: key cannot be NULL");
17+
18+
message = PG_GETARG_BYTEA_PP (0);
19+
20+
if (!PG_ARGISNULL (1))
21+
{
22+
associated = PG_GETARG_BYTEA_PP (1);
23+
}
24+
else
25+
{
26+
associated = NULL;
27+
}
28+
29+
key = PG_GETARG_BYTEA_PP (2);
30+
31+
ERRORIF (VARSIZE_ANY_EXHDR (key) != crypto_aead_det_xchacha20_KEYBYTES,
32+
"%s: invalid key");
33+
if (!PG_ARGISNULL (3))
34+
{
35+
nonce = PG_GETARG_BYTEA_PP (3);
36+
ERRORIF (VARSIZE_ANY_EXHDR (nonce) !=
37+
crypto_aead_det_xchacha20_NONCEBYTES, "%s: invalid nonce");
38+
}
39+
else
40+
{
41+
nonce = NULL;
42+
}
43+
result_size =
44+
VARSIZE_ANY_EXHDR (message) + crypto_aead_det_xchacha20_ABYTES;
45+
result = _pgsodium_zalloc_bytea (result_size + VARHDRSZ);
46+
success = crypto_aead_det_xchacha20_encrypt (
47+
PGSODIUM_UCHARDATA (result),
48+
PGSODIUM_UCHARDATA_ANY (message),
49+
VARSIZE_ANY_EXHDR (message),
50+
associated != NULL ? PGSODIUM_UCHARDATA_ANY (associated) : NULL,
51+
associated != NULL ? VARSIZE_ANY_EXHDR (associated) : 0,
52+
nonce != NULL ? PGSODIUM_UCHARDATA_ANY (nonce) : NULL,
53+
PGSODIUM_UCHARDATA_ANY (key));
54+
ERRORIF (success != 0, "%s: crypto_aead_det_xchacha20_encrypt failed");
55+
PG_RETURN_BYTEA_P (result);
56+
}

pgsodium/aead/det_encrypt_by_id.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "pgsodium.h"
2+
3+
PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_encrypt_by_id);
4+
Datum
5+
pgsodium_crypto_aead_det_encrypt_by_id (PG_FUNCTION_ARGS)
6+
{
7+
bytea *message;
8+
bytea *associated;
9+
unsigned long long key_id;
10+
bytea *context;
11+
bytea *key, *nonce;
12+
size_t result_size;
13+
bytea *result;
14+
int success;
15+
16+
ERRORIF (PG_ARGISNULL (0), "%s: message cannot be NULL");
17+
ERRORIF (PG_ARGISNULL (2), "%s: key id cannot be NULL");
18+
ERRORIF (PG_ARGISNULL (3), "%s: key context cannot be NULL");
19+
20+
message = PG_GETARG_BYTEA_PP (0);
21+
22+
if (!PG_ARGISNULL (1))
23+
{
24+
associated = PG_GETARG_BYTEA_PP (1);
25+
}
26+
else
27+
{
28+
associated = NULL;
29+
}
30+
31+
key_id = PG_GETARG_INT64 (2);
32+
context = PG_GETARG_BYTEA_PP (3);
33+
34+
if (!PG_ARGISNULL (4))
35+
{
36+
nonce = PG_GETARG_BYTEA_PP (4);
37+
ERRORIF (VARSIZE_ANY_EXHDR (nonce) !=
38+
crypto_aead_det_xchacha20_NONCEBYTES, "%s: invalid nonce");
39+
}
40+
else
41+
{
42+
nonce = NULL;
43+
}
44+
45+
result_size =
46+
VARSIZE_ANY_EXHDR (message) + crypto_aead_det_xchacha20_ABYTES;
47+
result = _pgsodium_zalloc_bytea (result_size + VARHDRSZ);
48+
49+
key =
50+
pgsodium_derive_helper (key_id, crypto_aead_det_xchacha20_KEYBYTES,
51+
context);
52+
53+
success = crypto_aead_det_xchacha20_encrypt (
54+
PGSODIUM_UCHARDATA (result),
55+
PGSODIUM_UCHARDATA_ANY (message),
56+
VARSIZE_ANY_EXHDR (message),
57+
associated != NULL ? PGSODIUM_UCHARDATA_ANY (associated) : NULL,
58+
associated != NULL ? VARSIZE_ANY_EXHDR (associated) : 0,
59+
nonce != NULL ? PGSODIUM_UCHARDATA_ANY (nonce) : NULL,
60+
PGSODIUM_UCHARDATA_ANY (key));
61+
ERRORIF (success != 0, "%s: failed");
62+
PG_RETURN_BYTEA_P (result);
63+
}

pgsodium/aead/det_keygen.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "pgsodium.h"
2+
3+
PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_keygen);
4+
Datum
5+
pgsodium_crypto_aead_det_keygen (PG_FUNCTION_ARGS)
6+
{
7+
size_t result_size = VARHDRSZ + crypto_aead_det_xchacha20_KEYBYTES;
8+
bytea *result = _pgsodium_zalloc_bytea (result_size);
9+
crypto_aead_det_xchacha20_keygen (PGSODIUM_UCHARDATA (result));
10+
PG_RETURN_BYTEA_P (result);
11+
}

pgsodium/aead/det_noncegen.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "pgsodium.h"
2+
3+
PGDLLEXPORT PG_FUNCTION_INFO_V1 (pgsodium_crypto_aead_det_noncegen);
4+
Datum
5+
pgsodium_crypto_aead_det_noncegen (PG_FUNCTION_ARGS)
6+
{
7+
int result_size = VARHDRSZ + crypto_aead_det_xchacha20_NONCEBYTES;
8+
bytea *result = _pgsodium_zalloc_bytea (result_size);
9+
randombytes_buf (VARDATA (result), crypto_aead_det_xchacha20_NONCEBYTES);
10+
PG_RETURN_BYTEA_P (result);
11+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <sodium.h>
2828
#include <string.h>
2929

30-
#include "crypto_aead_det_xchacha20.h"
30+
#include "det_xchacha20.h"
3131

3232
static void
3333
s2v_dbl256 (unsigned char d[32])

0 commit comments

Comments
 (0)