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

Improved AES GCM encryption, changed IV length to 12 bytes. #2962

Merged

Conversation

maxsharabayko
Copy link
Collaborator

@maxsharabayko maxsharabayko commented Jun 20, 2024

Changes

  • Changed the IV vector length to 12 bytes in the case of AES-GCM when peer's SRT version is 1.5.4 or above;
  • Fixed AAD byte order when peer's SRT version is 1.5.4 or above.

TODO

  • Raise SRT version to 1.5.4.

AES-CTR Initialisation Vector (IV)

The Initialisation Vector (IV) for the AES-CTR encryption mode is derived by exclusive ORing the first 112 bits of the Salt provided in the Keying Material with the packet sequence number (PktSeqNo) in the SRT header, and left-shifting the resulting value by 16 bits:

IV = (MSB(112, Salt) XOR PktSeqNo) << 16

Thus the counter (keystream) used by the AES engine is the 128-bit value obtained by concatenating the IV with the block counter ("ctr"):

 *    0   1   2   3   4   5  6   7   8   9   10  11  12  13  14  15   bytes
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+  ^
 * |                   0s                  |   PktSeqNo    |   0s  |  |
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+  |
 *                            XOR                                     | IV
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+          |
 * |                  nonce = MSB(112, Salt)               +          |
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+          v
 *                            (+)
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
 * |                   0s                                 |   ctr  |
 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

To avoid using the same keystream twice the payload size MUST be less than 2^16 blocks of 128 bits.

AES-GCM Initialisation Vector (IV)

Starting from v1.5.4.

The Initialisation Vector (IV) for the AES-GCM encryption mode is derived by exclusive ORing the first 96 bits of the Salt provided in the Keying Material with the packet sequence number (PktSeqNo) in the SRT header:

IV = MSB(96, Salt) XOR PktSeqNo

Each outbound packet uses a 12-octet IV and an encryption key to form two outputs (RFC-7714):

  • a 16-octet first key block, which is used in forming the authentication tag, and
  • a keystream of octets, formed in blocks of 16 octets each.

With an IV taking 96 bits, there are always 128-96=32 bits for the block counter until it wraps around.

@maxsharabayko maxsharabayko added Type: Maintenance Work required to maintain or clean up the code [core] Area: Changes in SRT library core labels Jun 20, 2024
@maxsharabayko maxsharabayko added this to the v1.5.4 milestone Jun 20, 2024
haicrypt/cryspr.c Fixed Show fixed Hide fixed
haicrypt/cryspr.c Fixed Show fixed Hide fixed
@maxsharabayko
Copy link
Collaborator Author

maxsharabayko commented Jul 29, 2024

TO FIX

  • Decryption fails in this scenario
(SRT v1.5.3)
srt-xtransmit generate "srt://127.0.0.1:4200?passphrase=abcdefghijk&cryptomode=2" --sendrate 1Mbps --duration 5s -v

(SRT PR 2962)
srt-xtransmit receive "srt://:4200?passphrase=abcdefghijk&cryptomode=2" -v

@maxsharabayko
Copy link
Collaborator Author

Testing Results

Single Socket Connection

Caller/listener combination of tests has the following pattern:

(receiver)
srt-xtransmit receive "srt://ip:port?cryptomode=2&passphrase=abcdefghijk" --enable-metrics -v

(sender)
srt-xtransmit generate "srt://ip:port?cryptomode=2&passphrase=abcdefghijk" --sendrate 10Mbps --duration 10s --enable-metrics -v

where ip must be left empty in case of a listener.

Rendezvous combination of tests has the following pattern:

(receiver)
srt-xtransmit receive "srt://ip:port?mode=rendezvous&bind=:localport&cryptomode=2&passphrase=abcdefghijk" --enable-metrics -v

(sender)
srt-xtransmit generate "srt://ip:port?mode=rendezvous&bind=:localport&cryptomode=2&passphrase=abcdefghijk" --sendrate 10Mbps --duration 10s --enable-metrics -v

v1.5.4 to v1.5.3

v1.5.4 v1.5.3 Tested
Caller, receiver Listener, sender ✔️
Caller, sender Listener, receiver ✔️
Listener, receiver Caller, sender ✔️
Listener, sender Caller, sender ✔️
Rendezvous (initiator), sender Rendezvous (responder), receiver ✔️
Rendezvous (responder), sender Rendezvous (initiator), receiver ✔️
Rendezvous (responder), receiver Rendezvous (initiator), sender ✔️
Rendezvous (initiator), receiver Rendezvous (responder), sender ✔️

v1.5.4 to v1.5.4

v1.5.4 v1.5.4 Tested
Caller, receiver Listener, sender ️✔️
Caller, sender Listener, receiver ️✔️
Rendezvous (initiator), sender Rendezvous (responder), receiver ✔️️
Rendezvous (responder), sender Rendezvous (initiator), receiver ✔️️

Group Connection

srt-xtransmit generate "srt://ip:port?passphrase=abcdefghijk&cryptomode=2&grouptype=broadcast" -v --enable-metrics --sendrate 1Mbps --duration 5s

srt-xtransmit.exe receive "srt://ip:port?passphrase=abcdefghijk&cryptomode=2&groupconnect=1" -v --enable-metrics
v1.5.4 v1.5.3 Tested
Caller, receiver Listener, sender ️✔️
Caller, sender Listener, receiver ️✔️
Listener, receiver Caller, sender ✔️
Listener, sender Caller, sender ️✔️

@maxsharabayko maxsharabayko merged commit 5819ade into Haivision:master Jul 30, 2024
12 checks passed
@maxsharabayko maxsharabayko deleted the develop/improve-aes-gcm-iv branch July 30, 2024 12:36
@kierank
Copy link

kierank commented Aug 23, 2024

Are you able to document this change in the RFC please?

@maxsharabayko
Copy link
Collaborator Author

WIP Haivision/srt-rfc#135

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Maintenance Work required to maintain or clean up the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants