forked from michelp/pgsodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaead.sql
126 lines (88 loc) · 6.77 KB
/
aead.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
BEGIN;
SELECT plan(14);
SELECT crypto_aead_ietf_keygen() aeadkey \gset
SELECT crypto_aead_ietf_noncegen() aeadnonce \gset
SELECT crypto_aead_ietf_encrypt(
'bob is your uncle', 'and also your friend', :'aeadnonce', :'aeadkey'::bytea) aead \gset
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt(%L, 'and also your friend', 'bad nonce', %L::bytea)$$, :'aead', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_encrypt: invalid nonce', 'crypto_aead_ietf_encrypt invalid nonce');
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt(%L, 'and also your friend', %L, 'bad_key'::bytea)$$, :'aead', :'aeadnonce'),
'22000', 'pgsodium_crypto_aead_ietf_encrypt: invalid key', 'crypto_aead_ietf_encrypt invalid key');
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt(NULL, 'and also your friend',
%L::bytea, %L::bytea)$$, :'aeadnonce', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_encrypt: message cannot be NULL', 'crypto_aead_ietf_encrypt null message');
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt('bob is your uncle', 'and also your friend',
NULL::bytea, %L::bytea)$$, :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_encrypt: nonce cannot be NULL', 'crypto_aead_ietf_encrypt null nonce');
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt('bob is your uncle', 'and also your friend',
%L::bytea, NULL::bytea)$$, :'aeadnonce'),
'22000', 'pgsodium_crypto_aead_ietf_encrypt: key cannot be NULL', 'crypto_aead_ietf_encrypt null key');
SELECT is(crypto_aead_ietf_decrypt(:'aead', 'and also your friend', :'aeadnonce', :'aeadkey'::bytea),
'bob is your uncle', 'crypto_aead_ietf_decrypt');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt(%L, 'and also your friend', 'bad nonce', %L::bytea)$$, :'aead', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: invalid nonce', 'crypto_aead_ietf_decrypt invalid nonce');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt(%L, 'and also your friend', %L, 'bad_key'::bytea)$$, :'aead', :'aeadnonce'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: invalid key', 'crypto_aead_ietf_decrypt invalid key');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt('foo', 'and also your friend', %L, %L::bytea)$$, :'aeadnonce', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: invalid message', 'crypto_aead_ietf_decrypt invalid message');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt(NULL::bytea, 'and also your friend', %L, %L::bytea)$$, :'aeadnonce', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: ciphertext cannot be NULL', 'crypto_aead_ietf_decrypt null message');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt(%L, 'and also your friend', NULL, 'bad_key'::bytea)$$, :'aead', :'aeadkey'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: nonce cannot be NULL', 'crypto_aead_ietf_decrypt null nonce');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt('foo', 'and also your friend', %L, NULL::bytea)$$, :'aeadnonce'),
'22000', 'pgsodium_crypto_aead_ietf_decrypt: key cannot be NULL', 'crypto_aead_ietf_decrypt null key');
SELECT crypto_aead_det_keygen() detkey \gset
SELECT crypto_aead_det_encrypt(
'bob is your uncle', 'and also your friend', :'detkey'::bytea) detaead \gset
SELECT is(crypto_aead_det_decrypt(:'detaead', 'and also your friend', :'detkey'::bytea),
'bob is your uncle', 'crypto_aead_det_decrypt');
SELECT crypto_aead_det_encrypt(
'bob is your uncle', NULL, :'detkey'::bytea) detaead2 \gset
SELECT is(crypto_aead_det_decrypt(:'detaead2', NULL, :'detkey'::bytea),
'bob is your uncle', 'crypto_aead_det_decrypt with NULL associated');
SELECT * FROM finish();
ROLLBACK;
\if :serverkeys
BEGIN;
SELECT plan(10);
SET ROLE pgsodium_keyiduser;
SELECT crypto_aead_ietf_encrypt(
'bob is your uncle', 'and also your friend', :'aeadnonce', 1) aead \gset
SELECT is(crypto_aead_ietf_decrypt(:'aead', 'and also your friend', :'aeadnonce', 1),
'bob is your uncle', 'crypto_aead_ietf_decrypt by id');
SELECT throws_ok($$select crypto_aead_ietf_encrypt('bob is your uncle', 'and also your friend', 'whatever', 'whatever'::bytea)$$,
'42501', 'permission denied for function crypto_aead_ietf_encrypt', 'crypto_aead_ietf_encrypt denied');
SELECT throws_ok($$select crypto_aead_ietf_decrypt('bob is your uncle', 'and also your friend', 'whatever', 'whatever'::bytea)$$,
'42501', 'permission denied for function crypto_aead_ietf_decrypt', 'crypto_aead_ietf_decrypt denied');
SELECT crypto_aead_det_encrypt(
'bob is your uncle', 'and also your friend', 32) detaeadid \gset
SELECT is(crypto_aead_det_decrypt(:'detaeadid', 'and also your friend', 32),
'bob is your uncle', 'crypto_aead_det_decrypt by id');
-- Test UUID key ids into key table
RESET ROLE;
SET ROLE pgsodium_keymaker;
select id as det_key_uuid from create_key('aead-det', 'det Test Key') \gset
select id as ietf_key_uuid from create_key('aead-ietf', 'ietf Test Key') \gset
RESET ROLE;
SET ROLE pgsodium_keyiduser;
SELECT crypto_aead_det_encrypt(
'bob is your uncle', 'and also your friend', :'det_key_uuid'::uuid) detaeadid \gset
SELECT is(crypto_aead_det_decrypt(:'detaeadid', 'and also your friend', :'det_key_uuid'::uuid),
'bob is your uncle', 'crypto_aead_ietf_decrypt by uuid');
select '00000000-0000-0000-0000-000000000000' as nil_uuid \gset
SELECT throws_ok($$select crypto_aead_det_encrypt('bob is your uncle', 'and also your friend', '00000000-0000-0000-0000-000000000000'::uuid)$$,
'P0002', 'query returned no rows', 'crypto_aead_det_encrypt invalid uuid');
SELECT throws_ok($$select crypto_aead_det_decrypt('bob is your uncle', 'and also your friend', '00000000-0000-0000-0000-000000000000'::uuid)$$,
'P0002', 'query returned no rows', 'crypto_aead_det_decrypt invalid uuid');
SELECT crypto_aead_ietf_encrypt(
'bob is your uncle', 'and also your friend', :'aeadnonce', :'ietf_key_uuid'::uuid) detaeadid \gset
SELECT is(crypto_aead_ietf_decrypt(:'detaeadid', 'and also your friend', :'aeadnonce', :'ietf_key_uuid'::uuid),
'bob is your uncle', 'crypto_aead_ietf_decrypt by uuid');
SELECT throws_ok(format($$select crypto_aead_ietf_encrypt('bob is your uncle', 'and also your friend', %L, '00000000-0000-0000-0000-000000000000'::uuid)$$, :'aeadnonce'),
'P0002', 'query returned no rows', 'crypto_aead_ietf_encrypt invalid uuid');
SELECT throws_ok(format($$select crypto_aead_ietf_decrypt('bob is your uncle', 'and also your friend', %L, '00000000-0000-0000-0000-000000000000'::uuid)$$, :'aeadnonce'),
'P0002', 'query returned no rows', 'crypto_aead_ietf_decrypt invalid uuid');
RESET ROLE;
SELECT * FROM finish();
ROLLBACK;
\endif