stream_aes_ctr: add aes-ctr:// protocol

Decrypts a stream that was encrypted as a whole with AES in CTR mode,
with a key of 128, 192 or 256 bits. The counter block is the IV plus
the index of the block as a 128 bit big endian number, so the stream
seeks wherever the inner stream does.
This commit is contained in:
Kacper Michajłow
2026-09-28 18:55:05 +02:00
parent ad9e2b3307
commit c92f8b4b3a
5 changed files with 209 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
add `aes-ctr://` protocol
+21
View File
@@ -1503,6 +1503,27 @@ PROTOCOLS
This starts reading from cap.ts after seeking 100MiB, then
reads until end of file.
``aes-ctr://key:iv@URL``
Decrypt a stream that was encrypted as a whole with AES in CTR mode.
``key`` is the AES key as 32, 48 or 64 hex digits. ``iv`` is the initial
counter block as 32 hex digits. The counter block is incremented as a
128 bit big endian number, which is what ``openssl enc -aes-128-ctr``
does.
Seeking works if the stream behind ``URL`` is seekable.
.. admonition:: Example
::
mpv aes-ctr://000102030405060708090a0b0c0d0e0f:f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff@video.mkv.enc
This plays a file that was encrypted with
``openssl enc -aes-128-ctr -K 000102030405060708090a0b0c0d0e0f
-iv f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff -in video.mkv -out video.mkv.enc``.
``null://``
Simulate an empty file. If opened for writing, it will discard all data.
+1
View File
@@ -191,6 +191,7 @@ sources = files(
'stream/cookies.c',
'stream/network.c',
'stream/stream.c',
'stream/stream_aes_ctr.c',
'stream/stream_avdevice.c',
'stream/stream_cb.c',
'stream/stream_concat.c',
+2
View File
@@ -53,6 +53,7 @@ extern const stream_info_t stream_info_ffmpeg_unsafe;
extern const stream_info_t stream_info_avdevice;
extern const stream_info_t stream_info_file;
extern const stream_info_t stream_info_slice;
extern const stream_info_t stream_info_aes_ctr;
extern const stream_info_t stream_info_fd;
extern const stream_info_t stream_info_ifo_dvdnav;
extern const stream_info_t stream_info_dvdnav;
@@ -96,6 +97,7 @@ static const stream_info_t *const stream_list[] = {
&stream_info_edl,
&stream_info_file,
&stream_info_slice,
&stream_info_aes_ctr,
&stream_info_fd,
&stream_info_cb,
#if HAVE_LIBCURL
+184
View File
@@ -0,0 +1,184 @@
/*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
#include <libavutil/aes.h>
#include <libavutil/intreadwrite.h>
#include <libavutil/mem.h>
#include "common/common.h"
#include "misc/bstr.h"
#include "stream.h"
#define BLOCKSIZE 16
// Number of key stream blocks generated at once.
#define MAX_BLOCKS 256
struct priv {
struct stream *inner;
struct AVAES *aes;
uint8_t iv[BLOCKSIZE];
};
static void priv_destructor(void *ptr)
{
struct priv *p = ptr;
av_free(p->aes);
}
// XOR the key stream onto data that starts at byte pos of the stream. The
// counter block is the IV plus the index of the block, as a 128 bit big
// endian number.
static void apply_keystream(struct priv *p, uint64_t pos, uint8_t *data, int len)
{
uint8_t keystream[MAX_BLOCKS * BLOCKSIZE];
uint64_t iv_high = AV_RB64(p->iv);
uint64_t iv_low = AV_RB64(p->iv + 8);
uint64_t index = pos / BLOCKSIZE;
int offset = pos % BLOCKSIZE;
while (len > 0) {
int n = MPMIN(len, (int)sizeof(keystream) - offset);
int blocks = MP_DIV_UP(offset + n, BLOCKSIZE);
for (int i = 0; i < blocks; i++) {
uint64_t low = iv_low + index + i;
AV_WB64(&keystream[i * BLOCKSIZE], iv_high + (low < iv_low));
AV_WB64(&keystream[i * BLOCKSIZE + 8], low);
}
av_aes_crypt(p->aes, keystream, keystream, blocks, NULL, 0);
for (int i = 0; i < n; i++)
data[i] ^= keystream[offset + i];
data += n;
len -= n;
index += blocks;
offset = 0;
}
}
static int fill_buffer(struct stream *s, void *buffer, int len)
{
struct priv *p = s->priv;
int64_t pos = stream_tell(p->inner);
int r = stream_read_partial(p->inner, buffer, len);
if (r > 0)
apply_keystream(p, pos, buffer, r);
return r;
}
static int seek(struct stream *s, int64_t newpos)
{
struct priv *p = s->priv;
return stream_seek(p->inner, newpos);
}
static int64_t get_size(struct stream *s)
{
struct priv *p = s->priv;
return stream_get_size(p->inner);
}
static void s_close(struct stream *s)
{
struct priv *p = s->priv;
free_stream(p->inner);
}
static int parse_url(struct stream *stream)
{
struct priv *p = stream->priv;
struct bstr params, inner_url;
if (!bstr_split_tok(bstr0(stream->path), "@", &params, &inner_url) ||
!inner_url.len)
{
MP_ERR(stream, "Expected aes-ctr://key:iv@URL: '%s'\n", stream->url);
return STREAM_ERROR;
}
struct bstr key, iv;
bstr_split_tok(params, ":", &key, &iv);
if ((key.len != 32 && key.len != 48 && key.len != 64) ||
!bstr_decode_hex(stream, key, &key))
{
MP_ERR(stream, "The key must have 32, 48 or 64 hex digits: '%s'\n",
stream->url);
return STREAM_ERROR;
}
if (iv.len != 2 * BLOCKSIZE || !bstr_decode_hex(stream, iv, &iv)) {
MP_ERR(stream, "The IV must have 32 hex digits: '%s'\n", stream->url);
return STREAM_ERROR;
}
p->aes = av_aes_alloc();
if (!p->aes || av_aes_init(p->aes, key.start, key.len * 8, 0) < 0)
return STREAM_ERROR;
memcpy(p->iv, iv.start, BLOCKSIZE);
stream->path = bstrto0(stream, inner_url);
return STREAM_OK;
}
static int open2(struct stream *stream, const struct stream_open_args *args)
{
struct priv *p = talloc_zero(stream, struct priv);
talloc_set_destructor(p, priv_destructor);
stream->priv = p;
stream->fill_buffer = fill_buffer;
stream->get_size = get_size;
stream->close = s_close;
int parse_ret = parse_url(stream);
if (parse_ret != STREAM_OK)
return parse_ret;
struct stream_open_args args2 = *args;
args2.url = stream->path;
int inner_ret = stream_create_with_args(&args2, &p->inner);
if (inner_ret != STREAM_OK)
return inner_ret;
if (p->inner->is_directory) {
MP_FATAL(stream, "Inner stream '%s' is a directory\n", p->inner->url);
free_stream(p->inner);
return STREAM_ERROR;
}
if (p->inner->seekable) {
stream->seek = seek;
stream->seekable = true;
}
stream->fast_skip = p->inner->fast_skip;
stream->streaming = p->inner->streaming;
stream->is_network = p->inner->is_network;
stream->stream_origin = p->inner->stream_origin;
return STREAM_OK;
}
const stream_info_t stream_info_aes_ctr = {
.name = "aes-ctr",
.open2 = open2,
.protocols = (const char*const[]){ "aes-ctr", NULL },
.can_write = false,
};