目录
1 SRTP session key

Key = PRF (key_master, x);
IV=[ <label>||(index/key_derivation_rate)XOR master_salt ]*2^16
IV=x<<16
||:表示连接的含义 , A,B,C 使用网络字节序表示,C = A||B, 则 C 的高字节为 A,低字节位为 B。
set key_derivation_rate = 0
r = index/key_derivation_rate;
if key_derivation_rate==0, r=0 // == zero(6byte )
key_id = <label> || r; //key_id 7byte
x = key_id XOR master salt;
其中,<label>根据产生密钥类型的不同而不同,具体为:
- k_e (SRTP encryption) : <label> = 0x00, n = n_e.
- k_a (SRTP message authentication) : <label> = 0x01, n = n_a.
- k_s (SRTP salting key) : <label> = 0x02, n = n_s.
以label=0, key_derivation_rate = 0 (RFC4568 定义kdr是一个声明参数,不是一个协商参数,
the default key-derivation rate SHALL be zero defined in RFC3711)
So, using kdr=0 in sip/SDP & srtp
给example:
packet_index/kdr: 000000000000
label: 00
master_salt: 0EC675AD498AFEEBB6960B3AABE6
-----------------------------------------------
xor: 0EC675AD498AFEEBB6960B3AABE6 (x, KDF input)
x*2^16: 0EC675AD498AFEEBB6960B3AABE60000 (AES-CM input)
cipher key: C61E7A93744F39EE10734AFE3FF7A087 (AES-CM output)
到 随机向量怎么生成这一步应该是比较清楚了。
那么sessionkey = PNF_n(mastkey, IV) 可以调用openssl 系列函数得到,
EVP_EncryptInit_ex(ctx, ciph, NULL, masterkey, NULL);
EVP_EncryptUpdate(ecc, sessionkey, &sessionkey_len, iv, mastkeylen);
mastkeylen 要blocksize 向上对齐
AES blocksize 128bit/16byte
也即用masterkey对IV加密
2 RTP 加密
通过章节1,通过master key, master salt可以求得加解密rtp使用的session key,session salt, 以及用于做哈希用的session_auth_key
2.1 RTP body加密
以chipset AEAD_AES_256_GCM为例说明srtp加密过程
AEAD_AES_256_GCM mastkey 32byte,
mastsalt 12byte(计算sessionkey时候补2byte的0来凑齐14byte)
RFC7714 chapter 8 & 9:
union AES_GCM_RTP_IV
{
unsigned char iv[12];
struct
{
uint16_t zero_2byte;
uint32_t ssrc_4byte;
uint32_t roq_4byte;
uint16_t seq_2byte;
} __attribute__((__packed__));
} __attribute__((__packed__));
2.1.1 计算IV
RFC 7714 chapter 16.2.1
memcpy(iv, c->session_salt, 12);
zero_2byte ^=0
ssrc_4byte^= r->ssrc;
roq_4byte ^= htonl((idx & 0x00ffffffff0000ULL) >> 16);
seq_2byte^= htons(idx & 0x00ffffULL);
rtp的seq只有两个字节,所以本地RTP session要记录seq环绕次数,环绕次数roq与seq组成index
也即,index = (roq<<16) | seq
计算得到IV后就可以初始化加密上下文了
EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm, NULL, session_key, iv);
2.1.2 计算AAD
To specify additional authenticated data (AAD), a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output parameter out set to NULL.(/docs/man1.1.1/man3/EVP_DecryptUpdate.html)
EVP_DecryptUpdate(session_key_ctx, NULL, &len, in, inl);
本文详细解析SRTP(Secure Real-time Transport Protocol)中Session Key的生成,包括RTP加密的步骤,如计算IV和AAD。通过RFC3711和相关标准,介绍了如何使用Master Key和Salt生成Session Key,并针对RTP Body加密进行了深入探讨,以AEAD_AES_256_GCM为例阐述加密过程。
1966

被折叠的 条评论
为什么被折叠?



