Re: Feature Request: Add AES-128-CFB Mode Support to pgcrypto

From: Álvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: Umar Hayat <postgresql(dot)wizard(at)gmail(dot)com>
Cc: Daniel Gustafsson <daniel(at)yesql(dot)se>, Vladyslav Nebozhyn <vlad(at)liberatii(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Feature Request: Add AES-128-CFB Mode Support to pgcrypto
Date: 2025-02-05 17:24:17
Message-ID: 202502051724.phy7m4f5hyiq@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2025-Jan-29, Umar Hayat wrote:

> Hi Daniel Gustafsson and Vladyslav Nebozhyn,
> I created a patch for CFB mode for AES encryption. Please have a look
> and let me know what you think.
> Patch covers implementation, tests and documentation changes.
>
> OpenSSL profives aes-cfb1, aes-cfb8 and aes-cfb128 modes where aes-cfb
> defaults to aes-cfb128. For simplicity I only added aes-cfb, which is
> the most common method used, lower number of bits will introduce
> performance degradation, but if it's desirable I can add them as well.

I kicked the tires on this by encrypting a file with
openssl aes-128-cfb -K afe908123efcba901230afe908eb5a04 -iv 912387caedade123912387c7ec0b9d0f -pbkdf2
then importing that into a bytea column. Then I can sort-of obtain the
file back with

select decrypt_iv from data, decrypt_iv(a, '\xafe908123efcba901230', '\x912387caedade123', 'aes-cfb');

... Except that appears that openssl will encode UTF8 characters in my file
as \ooo octal escapes in the encrypted output, which is really weird, so
the file doesn't roundtrip exactly. Maybe this is a pgcrypto shortcoming, not
sure. The final output looks like this (shortened):

=# select decrypt_iv from data, decrypt_iv(a, '\xafe908123efcba901230afe908eb5a04', '\x912387caedade123912387c7ec0b9d0f', 'aes-cfb');
>
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────>
\302\241Hoy ha sido un d\303\255a genial! Era el cumple de la abuela, que es la mam\303\241 de mam\303\241,

Anyway, at least the bytes appear to be interpreted the same by both
openssl and this new function, so that's good news.

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
"Having your biases confirmed independently is how scientific progress is
made, and hence made our great society what it is today" (Mary Gardiner)

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-02-05 17:28:11 Re: Feature Request: Add AES-128-CFB Mode Support to pgcrypto
Previous Message Melanie Plageman 2025-02-05 17:23:29 Re: Eagerly scan all-visible pages to amortize aggressive vacuum