Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLS Encryption with AES-128 and PKCS7 padding #4172

Open
afriza opened this issue Sep 7, 2024 · 1 comment
Open

HLS Encryption with AES-128 and PKCS7 padding #4172

afriza opened this issue Sep 7, 2024 · 1 comment
Assignees
Labels
EnglishNative This issue is conveyed exclusively in English.

Comments

@afriza
Copy link

afriza commented Sep 7, 2024

Hello, I am wondering if SRS' support for HLS AES-128 encryption uses PKCS7 padding ?

My understanding from quickly checking the code is that it does not use PKCS7 padding?

int nb_padding = 16 - (nb_buf % 16);
if (nb_padding > 0) {
memset(buf + nb_buf, nb_padding, nb_padding);
}
SrsUniquePtr<char[]> cipher(new char[nb_buf + nb_padding]);
AES_KEY* k = (AES_KEY*)key;
AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), nb_buf + nb_padding, k, iv, AES_ENCRYPT);

AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), HLS_AES_ENCRYPT_BLOCK_LENGTH, k, iv, AES_ENCRYPT);

It seems like old Apple docs mentioned that there is no padding.. But RFC8216 mentions PCKS7 padding..

I am requesting PKCS7 padding to be added for compatibility with ExoPlayer on Android..

@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Sep 7, 2024
@suzp1984 suzp1984 self-assigned this Sep 7, 2024
@suzp1984
Copy link
Contributor

suzp1984 commented Sep 7, 2024

int nb_padding = 16 - (nb_buf % 16);
if (nb_padding > 0) {
memset(buf + nb_buf, nb_padding, nb_padding);
}
SrsUniquePtr<char[]> cipher(new char[nb_buf + nb_padding]);
AES_KEY* k = (AES_KEY*)key;
AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), nb_buf + nb_padding, k, iv, AES_ENCRYPT);

Above code is PCKS7 (PCKS#7) padding. If the buf is not 16-byte alignment, then fill the remaining bytes with the remaining number of bytes.

It seems like old Apple docs mentioned that there is no padding

No, I don't think the padding mentioned in above apple doc is the padding the AES encryption.
For AES, the encrypting buf must be the aligned with the number of bytes of the secrete key, that's why there are PCKS7 method to let the buf aligned with the number of bytes of secrete key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
EnglishNative This issue is conveyed exclusively in English.
Projects
None yet
Development

No branches or pull requests

3 participants