Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions src/crypto_aead_aes_256_gcm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <string.h>

#include "crypto_aead_aes_256_gcm.h"

// TOOD generate new aes256gcm key
int crypto_aead_aes_256_gcm_encrypt_detached(
unsigned char *ciphertext,
const unsigned char *message, size_t message_len, const unsigned char *additional_data, size_t additional_data_len,
unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]) {

unsigned long long ciphertext_len;

if (crypto_aead_aes256gcm_is_available() == 0) {
return 1; /* Not available on this CPU */
}

if (crypto_aead_aes256gcm_encrypt(ciphertext, &ciphertext_len,
message, message_len,
additional_data, additional_data_len,
NULL, nonce, key)){
return 1;
}
return 0;
}

int crypto_aead_aes_256_gcm_decrypt_detached(
unsigned char *message, const unsigned char *ciphertext, size_t ciphertext_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]) {

unsigned long long message_len = ciphertext_len;

if (crypto_aead_aes256gcm_decrypt(message, &message_len,
NULL,
ciphertext, ciphertext_len,
additional_data,
additional_data_len,
nonce, key) != 0) {

return -1;
/* message forged! */
}
return 0;

}

int crypto_aead_aes_256_gcm_encrypt(
unsigned char *ciphertext, const unsigned char *message, size_t message_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]) {
return crypto_aead_aes_256_gcm_encrypt_detached(ciphertext, message, message_len, additional_data,
additional_data_len, nonce, key);
}

int crypto_aead_aes_256_gcm_decrypt(
unsigned char *message, const unsigned char *ciphertext, size_t ciphertext_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]) {

if (ciphertext_len < crypto_aead_aes256gcm_ABYTES) {
return -1;
}
return crypto_aead_aes_256_gcm_decrypt_detached(message, ciphertext, ciphertext_len, additional_data,
additional_data_len, nonce, key);
}

void crypto_aead_aes_256_gcm_keygen(
unsigned char k[crypto_aead_aes256gcm_KEYBYTES]) {
randombytes_buf(k, crypto_aead_aes256gcm_KEYBYTES);
}

40 changes: 40 additions & 0 deletions src/crypto_aead_aes_256_gcm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef crypto_aead_aes_256_gcm_H
#define crypto_aead_aes_256_gcm_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <sodium.h>

int crypto_aead_aes_256_gcm_encrypt_detached(
unsigned char *ciphertext,
const unsigned char *message, size_t message_len, const unsigned char *additional_data, size_t additional_data_len,
unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]);

int crypto_aead_aes_256_gcm_decrypt_detached(
unsigned char *message, const unsigned char *ciphertext, size_t ciphertext_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]);

int crypto_aead_aes_256_gcm_encrypt(
unsigned char *ciphertext, const unsigned char *message, size_t message_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]);

int crypto_aead_aes_256_gcm_decrypt(
unsigned char *message, const unsigned char *ciphertext, size_t ciphertext_len,
const unsigned char *additional_data, size_t additional_data_len, unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES],
const unsigned char key[crypto_aead_aes256gcm_KEYBYTES]);

void crypto_aead_aes_256_gcm_keygen(
unsigned char key[crypto_aead_aes256gcm_KEYBYTES]);

#ifdef __cplusplus
}
#endif

#endif

131 changes: 0 additions & 131 deletions src/crypto_aead_det_xchacha20.c

This file was deleted.

43 changes: 0 additions & 43 deletions src/crypto_aead_det_xchacha20.h

This file was deleted.

24 changes: 12 additions & 12 deletions src/pgsodium.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <fmgr.h>
#include <sodium.h>

#include "crypto_aead_det_xchacha20.h"
#include "crypto_aead_aes_256_gcm.h"
#include "pgsodium.h"

bytea *pgsodium_secret_key = NULL;
Expand Down Expand Up @@ -37,19 +37,19 @@ Datum pgsodium_crypto_aead_det_encrypt_by_id(PG_FUNCTION_ARGS) {

if (!PG_ARGISNULL(4)) {
nonce = PG_GETARG_BYTEA_PP(4);
ERRORIF(VARSIZE_ANY_EXHDR(nonce) != crypto_aead_det_xchacha20_NONCEBYTES,
ERRORIF(VARSIZE_ANY_EXHDR(nonce) != crypto_aead_aes256gcm_NPUBBYTES,
"%s: invalid nonce");
} else {
nonce = NULL;
}

result_size = VARSIZE_ANY_EXHDR(message) + crypto_aead_det_xchacha20_ABYTES;
result_size = VARSIZE_ANY_EXHDR(message) + crypto_aead_aes256gcm_ABYTES;
result = _pgsodium_zalloc_bytea(result_size + VARHDRSZ);

key = pgsodium_derive_helper(key_id, crypto_aead_det_xchacha20_KEYBYTES,
key = pgsodium_derive_helper(key_id, crypto_aead_aes256gcm_KEYBYTES,
context);

success = crypto_aead_det_xchacha20_encrypt(
success = crypto_aead_aes_256_gcm_encrypt(
PGSODIUM_UCHARDATA(result), PGSODIUM_UCHARDATA_ANY(message),
VARSIZE_ANY_EXHDR(message),
associated != NULL ? PGSODIUM_UCHARDATA_ANY(associated) : NULL,
Expand Down Expand Up @@ -87,19 +87,19 @@ Datum pgsodium_crypto_aead_det_decrypt_by_id(PG_FUNCTION_ARGS) {

if (!PG_ARGISNULL(4)) {
nonce = PG_GETARG_BYTEA_PP(4);
ERRORIF(VARSIZE_ANY_EXHDR(nonce) != crypto_aead_det_xchacha20_NONCEBYTES,
ERRORIF(VARSIZE_ANY_EXHDR(nonce) != crypto_aead_aes256gcm_NPUBBYTES,
"%s: invalid nonce");
} else {
nonce = NULL;
}
ERRORIF(VARSIZE_ANY_EXHDR(ciphertext) < crypto_aead_det_xchacha20_ABYTES,
ERRORIF(VARSIZE_ANY_EXHDR(ciphertext) < crypto_aead_aes256gcm_ABYTES,
"%s: invalid message");
result_len = VARSIZE_ANY_EXHDR(ciphertext) - crypto_aead_det_xchacha20_ABYTES;
result_len = VARSIZE_ANY_EXHDR(ciphertext) - crypto_aead_aes256gcm_ABYTES;
result = _pgsodium_zalloc_bytea(result_len + VARHDRSZ);
key = pgsodium_derive_helper(key_id, crypto_aead_det_xchacha20_KEYBYTES,
key = pgsodium_derive_helper(key_id, crypto_aead_aes256gcm_KEYBYTES,
context);

success = crypto_aead_det_xchacha20_decrypt(
success = crypto_aead_aes_256_gcm_decrypt(
PGSODIUM_UCHARDATA(result), PGSODIUM_UCHARDATA_ANY(ciphertext),
VARSIZE_ANY_EXHDR(ciphertext),
associated != NULL ? PGSODIUM_UCHARDATA_ANY(associated) : NULL,
Expand All @@ -112,8 +112,8 @@ Datum pgsodium_crypto_aead_det_decrypt_by_id(PG_FUNCTION_ARGS) {

PG_FUNCTION_INFO_V1(pgsodium_crypto_aead_det_noncegen);
Datum pgsodium_crypto_aead_det_noncegen(PG_FUNCTION_ARGS) {
int result_size = VARHDRSZ + crypto_aead_det_xchacha20_NONCEBYTES;
int result_size = VARHDRSZ + crypto_aead_aes256gcm_NPUBBYTES;
bytea *result = _pgsodium_zalloc_bytea(result_size);
randombytes_buf(VARDATA(result), crypto_aead_det_xchacha20_NONCEBYTES);
randombytes_buf(VARDATA(result), crypto_aead_aes256gcm_NPUBBYTES);
PG_RETURN_BYTEA_P(result);
}
Loading