RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 2084161 - [FutureFeature]: GnuTLS: Add PKCS#7 padding support for AES-CBC
Summary: [FutureFeature]: GnuTLS: Add PKCS#7 padding support for AES-CBC
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 9
Classification: Red Hat
Component: gnutls
Version: 9.1
Hardware: Unspecified
OS: Unspecified
medium
unspecified
Target Milestone: rc
: ---
Assignee: Daiki Ueno
QA Contact: Alexander Sosedkin
Jan Fiala
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2022-05-11 14:36 UTC by Andreas Schneider
Modified: 2023-05-14 13:21 UTC (History)
6 users (show)

Fixed In Version: gnutls-3.7.3-13.el9
Doc Type: Enhancement
Doc Text:
.GnuTLS can add and remove padding during decryption and encryption The implementation of certain protocols requires PKCS#7 padding during decryption and encryption. The `gnutls_cipher_encrypt3` and `gnutls_cipher_decrypt3` block cipher functions have been added to GnuTLS to transparently handle padding. As a result, you can now use these functions in combination with the `GNUTLS_CIPHER_PADDING_PKCS7` flag to automatically add or remove padding if the length of the original plaintext is not a multiple of the block size.
Clone Of:
Environment:
Last Closed: 2023-05-09 08:20:29 UTC
Type: Bug
Target Upstream Version:
Embargoed:
jafiala: needinfo+
jafiala: needinfo+


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Gitlab gnutls gnutls merge_requests 1611 0 None opened crypto-api: add block cipher API with automatic padding 2022-07-14 07:34:42 UTC
Red Hat Issue Tracker CRYPTO-7330 0 None None None 2022-05-24 16:01:14 UTC
Red Hat Issue Tracker RHELPLAN-121741 0 None None None 2022-05-11 14:48:24 UTC
Red Hat Product Errata RHBA-2023:2522 0 None None None 2023-05-09 08:20:50 UTC

Description Andreas Schneider 2022-05-11 14:36:20 UTC
Description of problem:

I'm implementing AEAD-AES-256-CBC-HMAC-SHA512 support according to

https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/0fc25352-662b-40e7-9dee-f10c6244c4c9

in Samba. You can fine my code here:

https://git.samba.org/?p=asn/samba.git;a=shortlog;h=refs/heads/asn-samr-aes

Look for:

lib:crypto: Implement samba_gnutls_aead_aes_256_cbc_hmac_sha512_encrypt()

This doesn't work as I have a buffer which doesn't match the AES-CBS block size of 16. For this PKCS#7 padding is needed according to

https://datatracker.ietf.org/doc/html/draft-mcgrew-aead-aes-cbc-hmac-sha2-05#page-6

I need support for PKCS#7 padding in gnutls_cipher_*() functions to implement this correctly.

It would be nice to have this available in RHEL 9.2 and in Fedora as soon as possible so that I can finish the implementation.

Thanks for your help!

Comment 2 Daiki Ueno 2022-07-13 15:44:48 UTC
> I need support for PKCS#7 padding in gnutls_cipher_*() functions to implement this correctly.

I have been thinking about how to integrate this feature into the existing API. If we pad/unpad transparently, we would need a complete new API, not only new cipher IDs or a variant with a flag to indicate padding, because the decrypt function needs to report the actual length after unpadding, something like:

  int gnutls_cipher_decrypt3(gnutls_cipher_algorithm_t cipher, const uint8_t *ciphertext, size_t ciphertext_len, uint8_t *plaintext, size_t *plaintext_len);

Note the last argument is of type `size_t *`. The encrypt function could have the same signature as gnutls_cipher_encrypt2, though it requires a check whether ciphertext has enough room to hold the last block (if we want to avoid extra allocation). In any case implementing this approach wouldn't be straightforward.

The alternative approach would be to just provide low-level functions that pad/unpad a single block:

  int gnutls_pkcs7_pad(uint8_t *block, size_t block_len, size_t plaintext_len);
  int gnutls_pkcs7_unpad(const uint8_t *block, size_t block_len, size_t *plaintext_len);

Do you think if this is acceptable?

Comment 3 Daiki Ueno 2022-07-14 07:34:43 UTC
I've ended up with the following API:

```c
/**
 * gnutls_cipher_encrypt3:
 * @handle: is a #gnutls_cipher_hd_t type
 * @ptext: the data to encrypt
 * @ptext_len: the length of data to encrypt
 * @ctext: the encrypted data
 * @ctext_len: the length of encrypted data (initially must hold the maximum available size)
 * @flags: flags for padding
 *
 * This function will encrypt the given data using the algorithm
 * specified by the context. For block ciphers, if the @ptext_len is
 * not a multiple of the block size, the last block is padded
 * according to @flags, typically %GNUTLS_CIPHER_PADDING_PKCS7. In
 * that case, @ctext must hold enough space to store padded cipher
 * text and @ctext_len is updated to be a multiple of the block
 * size. The initial size can be obtained by calling this function
 * with @ctext set to %NULL.
 *
 * Returns: Zero or a negative error code on error.
 *
 * Since: 3.7.7
 **/
int
gnutls_cipher_encrypt3(gnutls_cipher_hd_t handle,
		       const void *ptext, size_t ptext_len,
		       void *ctext, size_t *ctext_len,
		       unsigned flags);

/**
 * gnutls_cipher_decrypt3:
 * @handle: is a #gnutls_cipher_hd_t type
 * @ctext: the data to decrypt
 * @ctext_len: the length of data to decrypt
 * @ptext: the decrypted data
 * @ptext_len: the available length for decrypted data
 * @flags: flags for padding
 *
 * This function will decrypt the given data using the algorithm
 * specified by the context. If @flags is specified, padding for the
 * decrypted data will be removed accordingly and @ptext_len will be
 * updated.
 *
 * Returns: Zero or a negative error code on error.
 *
 * Since: 3.7.7
 **/
int
gnutls_cipher_decrypt3(gnutls_cipher_hd_t handle,
		       const void *ctext, size_t ctext_len,
		       void *ptext, size_t *ptext_len,
		       unsigned flags);
```

See https://gitlab.com/gnutls/gnutls/-/merge_requests/1611 for details.

Comment 4 Andreas Schneider 2022-07-14 12:53:38 UTC
The API looks fine for me. It allows to add more flags in future. I've commented the upstream commit.

Comment 18 errata-xmlrpc 2023-05-09 08:20:29 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (gnutls bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2023:2522


Note You need to log in before you can comment on or make changes to this bug.