Skip to content

Commit 95a8055

Browse files
committed
first cut at xchacha streaming.
1 parent f57bd69 commit 95a8055

File tree

11 files changed

+108
-47
lines changed

11 files changed

+108
-47
lines changed

example/encrypted_column.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CREATE SCHEMA IF NOT EXISTS pgsodium;
22
CREATE EXTENSION IF NOT EXISTS pgsodium WITH SCHEMA pgsodium;
3-
3+
s
44
-- This is a demonstration user to show that the pgsodium_keyiduser
55
-- role can be used to access only encrpytion functions by key_id,
66
-- this role can never access raw encrpytion keys.
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

3-
# YOU MUST EDIT THIS FILE!!!
4-
5-
# after editing this file below WITH YOUR KEY, remove the exit on the
6-
# next line
7-
exit
8-
9-
# YOU MUST EDIT THIS FILE!!!
10-
# DO NOT USE THIS TEST KEY CHECKED INTO GIT!!!
11-
12-
# your secret key goes here
13-
echo 130cdceb74d7174fcbffbcb4a3397f3551b990fed92e452279ea3922cf715a0a
14-
15-
# YOU MUST EDIT THIS FILE!!!
16-
# DO NOT USE THIS TEST KEY CHECKED INTO GIT!!!
3+
FILE=$(PGDATA)/pgsodium_root.key
174

5+
if [ ! -f "$FILE" ]; then
6+
head -c 32 /dev/urandom | hex > $FILE
7+
fi
8+
echo cat $FILE

pgsodium.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pgsodium extension
22
comment = 'Postgres extension for libsodium functions'
3-
default_version = '1.2.0'
3+
default_version = '1.3.0'
44
relocatable = true
55
requires = ''

sql/pgsodium--1.2.0--1.3.0.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE FUNCTION crypto_secretstream_keygen()
2+
RETURNS bytea
3+
AS '$libdir/pgsodium', 'pgsodium_crypto_secretstream_xchacha20poly1305_keygen'
4+
LANGUAGE C VOLATILE;
5+

src/kdf.c

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,3 @@ pgsodium_crypto_kdf_derive_from_key(PG_FUNCTION_ARGS)
3636
PG_RETURN_BYTEA_P(result);
3737
}
3838

39-
PG_FUNCTION_INFO_V1(pgsodium_crypto_kx_keypair);
40-
Datum
41-
pgsodium_crypto_kx_keypair(PG_FUNCTION_ARGS)
42-
{
43-
TupleDesc tupdesc;
44-
Datum values[2];
45-
bool nulls[2] = {false, false};
46-
HeapTuple tuple;
47-
Datum result;
48-
bytea* publickey;
49-
bytea* secretkey;
50-
size_t public_size = crypto_kx_PUBLICKEYBYTES + VARHDRSZ;
51-
size_t secret_size = crypto_kx_SECRETKEYBYTES + VARHDRSZ;
52-
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
53-
ereport(ERROR,
54-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
55-
errmsg("function returning record called in context "
56-
"that cannot accept type record")));
57-
publickey = _pgsodium_zalloc_bytea(public_size);
58-
secretkey = _pgsodium_zalloc_bytea(secret_size);
59-
crypto_kx_keypair(
60-
PGSODIUM_UCHARDATA(publickey),
61-
PGSODIUM_UCHARDATA(secretkey));
62-
values[0] = PointerGetDatum(publickey);
63-
values[1] = PointerGetDatum(secretkey);
64-
tuple = heap_form_tuple(tupdesc, values, nulls);
65-
result = HeapTupleGetDatum(tuple);
66-
return result;
67-
}
68-

src/kx.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,32 @@ pgsodium_crypto_kx_server_session_keys(PG_FUNCTION_ARGS)
125125
return result;
126126
}
127127

128+
PG_FUNCTION_INFO_V1(pgsodium_crypto_kx_keypair);
129+
Datum
130+
pgsodium_crypto_kx_keypair(PG_FUNCTION_ARGS)
131+
{
132+
TupleDesc tupdesc;
133+
Datum values[2];
134+
bool nulls[2] = {false, false};
135+
HeapTuple tuple;
136+
Datum result;
137+
bytea* publickey;
138+
bytea* secretkey;
139+
size_t public_size = crypto_kx_PUBLICKEYBYTES + VARHDRSZ;
140+
size_t secret_size = crypto_kx_SECRETKEYBYTES + VARHDRSZ;
141+
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
142+
ereport(ERROR,
143+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
144+
errmsg("function returning record called in context "
145+
"that cannot accept type record")));
146+
publickey = _pgsodium_zalloc_bytea(public_size);
147+
secretkey = _pgsodium_zalloc_bytea(secret_size);
148+
crypto_kx_keypair(
149+
PGSODIUM_UCHARDATA(publickey),
150+
PGSODIUM_UCHARDATA(secretkey));
151+
values[0] = PointerGetDatum(publickey);
152+
values[1] = PointerGetDatum(secretkey);
153+
tuple = heap_form_tuple(tupdesc, values, nulls);
154+
result = HeapTupleGetDatum(tuple);
155+
return result;
156+
}

src/pgsodium.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ Datum pgsodium_crypto_auth_verify(PG_FUNCTION_ARGS);
114114
Datum pgsodium_crypto_auth_by_id(PG_FUNCTION_ARGS);
115115
Datum pgsodium_crypto_auth_verify_by_id(PG_FUNCTION_ARGS);
116116

117+
/* Secret streams */
118+
119+
Datum pgsodium_crypto_secretstream_xchacha20poly1305_keygen(PG_FUNCTION_ARGS);
120+
117121
/* AEAD */
118122

119123
Datum pgsodium_crypto_aead_ietf_keygen(PG_FUNCTION_ARGS);
@@ -194,4 +198,11 @@ Datum pgsodium_crypto_hash_sha512(PG_FUNCTION_ARGS);
194198

195199
Datum pgsodium_derive(PG_FUNCTION_ARGS);
196200

201+
/* Streaming */
202+
203+
Datum pgsodium_crypto_stream_xchacha20_keygen(PG_FUNCTION_ARGS);
204+
Datum pgsodium_crypto_stream_xchacha20_noncegen(PG_FUNCTION_ARGS);
205+
Datum pgsodium_crypto_stream_xchacha20(PG_FUNCTION_ARGS);
206+
Datum pgsodium_crypto_stream_xchacha20_xor(PG_FUNCTION_ARGS);
207+
197208
#endif /* PGSODIUM_H */

src/secretstream.c

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

src/stream.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include "pgsodium.h"
3+
4+
PG_FUNCTION_INFO_V1(pgsodium_crypto_stream_xchacha20_keygen);
5+
Datum
6+
pgsodium_crypto_stream_xchacha20_keygen(PG_FUNCTION_ARGS)
7+
{
8+
size_t result_size = VARHDRSZ + crypto_stream_xchacha20_KEYBYTES;
9+
bytea* result = _pgsodium_zalloc_bytea(result_size);
10+
crypto_stream_xchacha20_keygen(PGSODIUM_UCHARDATA(result));
11+
PG_RETURN_BYTEA_P(result);
12+
}
13+
14+
PG_FUNCTION_INFO_V1(pgsodium_crypto_stream_xchacha20_noncegen);
15+
Datum
16+
pgsodium_crypto_stream_xchacha20_noncegen(PG_FUNCTION_ARGS)
17+
{
18+
int result_size = VARHDRSZ + crypto_stream_xchacha20_NONCEBYTES;
19+
bytea* result = _pgsodium_zalloc_bytea(result_size);
20+
randombytes_buf(VARDATA(result), crypto_stream_xchacha20_NONCEBYTES);
21+
PG_RETURN_BYTEA_P(result);
22+
}
23+
24+
PG_FUNCTION_INFO_V1(pgsodium_crypto_stream_xchacha20);
25+
Datum
26+
pgsodium_crypto_stream_xchacha20(PG_FUNCTION_ARGS)
27+
{
28+
}
29+
30+
PG_FUNCTION_INFO_V1_xor(pgsodium_crypto_stream_xchacha20);
31+
Datum
32+
pgsodium_crypto_stream_xchacha20_xor(PG_FUNCTION_ARGS)
33+
{
34+
}
35+

test/secretstream.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
BEGIN;
2+
SELECT plan(1);
3+
4+
SELECT crypto_secretstream_keygen() streamkey \gset
5+
6+
ROLLBACK;

0 commit comments

Comments
 (0)