-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgspeck.c
70 lines (53 loc) · 1.64 KB
/
pgspeck.c
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
#include "pgspeck.h"
#include "postgres.h"
#include "funcapi.h"
#include "fmgr.h"
PG_MODULE_MAGIC;
Datum pgspeck_encrypt32(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgspeck_encrypt32);
Datum
pgspeck_encrypt32(PG_FUNCTION_ARGS)
{
int64 plaintext = PG_GETARG_INT64(0);
int64 key = PG_GETARG_INT64(1);
PG_RETURN_INT64(speck_encrypt32((uint32) plaintext, key));
}
Datum pgspeck_decrypt32(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgspeck_decrypt32);
Datum
pgspeck_decrypt32(PG_FUNCTION_ARGS)
{
int64 ciphertext = PG_GETARG_INT64(0);
int64 key = PG_GETARG_INT64(1);
PG_RETURN_INT64(speck_decrypt32((uint32) ciphertext, key));
}
Datum pgspeck_encrypt48(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgspeck_encrypt48);
Datum
pgspeck_encrypt48(PG_FUNCTION_ARGS)
{
int64 plaintext = PG_GETARG_INT64(0);
int64 key1 = PG_GETARG_INT64(1);
int64 key2 = PG_GETARG_INT64(2);
const int64 keys[2] = { key1, key2 };
if ((key1 >> 48) != 0 || (key2 >> 48) != 0)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("only the lowermost 48 bits of key's components can be non-zero")));
PG_RETURN_INT64(speck_encrypt48(plaintext, keys));
}
Datum pgspeck_decrypt48(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgspeck_decrypt48);
Datum
pgspeck_decrypt48(PG_FUNCTION_ARGS)
{
int64 ciphertext = PG_GETARG_INT64(0);
int64 key1 = PG_GETARG_INT64(1);
int64 key2 = PG_GETARG_INT64(2);
const int64 keys[2] = { key1, key2 };
if ((key1 >> 48) != 0 || (key2 >> 48) != 0)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("only the lowermost 48 bits of key's components can be non-zero")));
PG_RETURN_INT64(speck_decrypt48(ciphertext, keys));
}