Files
utun2/src/media_async/media_crypto.h
T

35 lines
2.2 KiB
C

/* Общие AES-256-CCM примитивы. Файлы обрабатываются порциями с независимыми tags.
* nonce определяется author/UUID/part; UUID новый для каждой подготовки. Домен nonce
* utun_dm_media сохранён для существующего формата DM. Повторы используют готовый ciphertext.
* Файловые функции вызываются в worker; caller открывает файлы и публикует результат
* только после успешной проверки и durable сохранения. 0 = успех, -1 = ошибка. */
#ifndef MEDIA_CRYPTO_H
#define MEDIA_CRYPTO_H
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MEDIA_CIPHER_KEY_SIZE 32
#define MEDIA_CIPHER_NONCE_SIZE 13
#define MEDIA_CIPHER_TAG_SIZE 16
#define MEDIA_CIPHER_PLAIN_MAX 65535u
#define MEDIA_CIPHER_CHUNK_SIZE 32768u
void media_cipher_nonce(uint64_t author, const uint8_t id[16], uint64_t part, uint8_t nonce[MEDIA_CIPHER_NONCE_SIZE]);
/* out вмещает plain_len+16; расшифровка проверяет tag до возврата plaintext. */
int media_ccm_encrypt(const uint8_t key[32], const uint8_t nonce[13], const uint8_t* plain,
size_t plain_len, uint8_t* out, size_t* out_len);
int media_ccm_decrypt(const uint8_t key[32], const uint8_t nonce[13], const uint8_t* cipher,
size_t cipher_len, uint8_t* out, size_t* out_len);
/* Поток plaintext → ciphertext; возвращает размер оригинала и SHA256 ciphertext. */
int media_encrypt_stream(const uint8_t key[32], uint64_t author, const uint8_t id[16],
FILE* src, FILE* dst, uint64_t* plain_size, uint8_t hash[32]);
/* Проверяет каждый tag, точный размер и полный hash; dst должен быть временным. */
int media_decrypt_stream(const uint8_t key[32], uint64_t author, const uint8_t id[16],
uint64_t plain_size, const uint8_t hash[32], FILE* src, FILE* dst);
#ifdef __cplusplus
}
#endif
#endif