audio_crypto.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <string.h>
  2. #include "audio_crypto.h"
  3. #include "mbedtls/aes.h"
  4. #include "sodium.h"
  5. int audio_crypto_decrypt_rtp(const audio_encrypt_t *encrypt,
  6. const uint8_t *input, size_t input_len,
  7. uint8_t *output, size_t output_capacity,
  8. const uint8_t *full_packet,
  9. size_t full_packet_len) {
  10. if (!encrypt || !input || !output) {
  11. return -1;
  12. }
  13. if (encrypt->type == AUDIO_ENCRYPT_NONE) {
  14. if (input_len > output_capacity) {
  15. return -1;
  16. }
  17. memcpy(output, input, input_len);
  18. return (int)input_len;
  19. }
  20. if (encrypt->type == AUDIO_ENCRYPT_AES_CBC) {
  21. if (input_len > output_capacity) {
  22. return -1;
  23. }
  24. uint8_t iv[16];
  25. memcpy(iv, encrypt->iv, sizeof(iv));
  26. size_t num_blocks = input_len / 16;
  27. size_t remainder = input_len % 16;
  28. size_t encrypted_len = num_blocks * 16;
  29. if (encrypted_len > 0) {
  30. mbedtls_aes_context aes;
  31. mbedtls_aes_init(&aes);
  32. int ret = mbedtls_aes_setkey_dec(&aes, encrypt->key, 128);
  33. if (ret != 0) {
  34. mbedtls_aes_free(&aes);
  35. return -1;
  36. }
  37. ret = mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, encrypted_len, iv,
  38. input, output);
  39. mbedtls_aes_free(&aes);
  40. if (ret != 0) {
  41. return -1;
  42. }
  43. }
  44. if (remainder > 0) {
  45. memcpy(output + encrypted_len, input + encrypted_len, remainder);
  46. }
  47. return (int)input_len;
  48. }
  49. if (encrypt->type == AUDIO_ENCRYPT_CHACHA20_POLY1305) {
  50. // AirPlay 2 RTP: nonce = 4 zero bytes + last 8 bytes of packet,
  51. // AAD = RTP timestamp + SSRC (bytes 4-11 of the full packet).
  52. if (!full_packet || full_packet_len < 12) {
  53. return -1;
  54. }
  55. if (input_len < crypto_aead_chacha20poly1305_ietf_ABYTES + 8) {
  56. return -1;
  57. }
  58. uint8_t nonce[12] = {0};
  59. memcpy(nonce + 4, full_packet + full_packet_len - 8, 8);
  60. const uint8_t *aad = full_packet + 4;
  61. size_t aad_len = 8;
  62. size_t ciphertext_len = input_len - 8;
  63. unsigned long long decrypted_len = 0;
  64. int ret = crypto_aead_chacha20poly1305_ietf_decrypt(
  65. output, &decrypted_len, NULL, input, ciphertext_len, aad, aad_len,
  66. nonce, encrypt->key);
  67. if (ret != 0) {
  68. return -1;
  69. }
  70. return (int)decrypted_len;
  71. }
  72. return -1;
  73. }
  74. int audio_crypto_decrypt_buffered(const audio_encrypt_t *encrypt,
  75. const uint8_t *packet, size_t packet_len,
  76. uint8_t *output, size_t output_capacity) {
  77. if (!packet || !output) {
  78. return -1;
  79. }
  80. if (!encrypt || encrypt->type != AUDIO_ENCRYPT_CHACHA20_POLY1305) {
  81. if (packet_len <= 12) {
  82. return -1;
  83. }
  84. size_t payload_len = packet_len - 12;
  85. if (payload_len > output_capacity) {
  86. return -1;
  87. }
  88. memcpy(output, packet + 12, payload_len);
  89. return (int)payload_len;
  90. }
  91. if (packet_len < 36) {
  92. return -1;
  93. }
  94. // Buffered audio: AAD is bytes 4-11 (timestamp + SSRC), nonce in last 8
  95. // bytes.
  96. uint8_t nonce[12] = {0};
  97. memcpy(nonce + 4, packet + packet_len - 8, 8);
  98. const uint8_t *aad = packet + 4;
  99. size_t aad_len = 8;
  100. const uint8_t *ciphertext = packet + 12;
  101. size_t ciphertext_len = packet_len - 12 - 8;
  102. if (ciphertext_len >
  103. output_capacity + crypto_aead_chacha20poly1305_ietf_ABYTES) {
  104. return -1;
  105. }
  106. unsigned long long decrypted_len = 0;
  107. int ret = crypto_aead_chacha20poly1305_ietf_decrypt(
  108. output, &decrypted_len, NULL, ciphertext, ciphertext_len, aad, aad_len,
  109. nonce, encrypt->key);
  110. if (ret != 0) {
  111. return -1;
  112. }
  113. return (int)decrypted_len;
  114. }