AltairCA / EntityFrameworkCore.PostgreSQL.ColumnEncryption

NPGSQL Extension that supports native PostgreSql's [Raw Encryption Functions (encrypt_iv,decrypt_iv with aes-cbc/pad:pkcs)]
15 stars 3 forks source link

Does this actually use pgcrypto? #9

Closed Torkolis closed 8 months ago

Torkolis commented 8 months ago

Hi, I don't understand how and even if this actually uses pgcrypto. From what I can see in source code the conversion happens here:

rawString => rawString.NpgsqlEncrypt(password, iv, keyLength),
encryptedString => encryptedString.NpgsqlDecrypt(password, iv, keyLength))

in this file https://github.com/AltairCA/EntityFrameworkCore.PostgreSQL.ColumnEncryption/blob/master/AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption/Functions/EncryptionValueConverter.cs

while the functions NpgsqlEncrypt and NpgsqlDecrypt just seem to use

System.Security.Cryptography

functions as seen here

        internal static string NpgsqlEncrypt(this string value, string password,string iv,int keyLength)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            return AesUtil.AES_encrypt(value, password,iv,keyLength);
        }
        internal static string NpgsqlDecrypt(this string value, string password,string iv,int keyLength)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            return AesUtil.AES_decrypt(value, password,iv,keyLength);
        }

https://github.com/AltairCA/EntityFrameworkCore.PostgreSQL.ColumnEncryption/blob/master/AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption/Attribute/AttributeEncryptExtension.cs

leading to the AesUtil functions here

https://github.com/AltairCA/EntityFrameworkCore.PostgreSQL.ColumnEncryption/blob/master/AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption/Utils/AESUtil.cs

AltairCA commented 8 months ago

Hello,

Those functions are static methods that match the functionality in pgcrypto, and those are used in Value Converters as you mentioned above, but If you use EF Functions in Group By, Select etc, encryption or decryption will be happening on DB level using this Translator

Torkolis commented 8 months ago

Ok, thank you for explanation, I thought it would do something like what is described here: https://www.digitalocean.com/community/tutorials/how-to-encrypt-a-database-at-rest-in-postgresql-on-ubuntu#method-3-encrypting-parts-of-database

create table test_login (name VARCHAR(50), password TEXT);
insert into test_login (name, password) values ('Jonathon', pgp_sym_encrypt('123ab', 'd3a')::TEXT);
select name, pgp_sym_decrypt(password::bytea, 'd3a') from test_login;

and would not do any encryption/decryption locally.