Switch message cipher consumers to shared media API
This commit is contained in:
1 parent
9c22b04cb0
commit
b4ef4ec919
4 files changed
+25
-25
No files matched your search
+3
-3
@@ -331,7 +331,7 @@ static int dm_build_body(struct dm_state* dm, const struct dm_conv* c, uint64_t
|
||||
uint8_t* enc = u_malloc(data_len + DM_TAG_SIZE);
|
||||
if (!enc) return -1;
|
||||
size_t enc_len = 0;
|
||||
if (dm_encrypt(c->content_key, nonce, data, data_len, enc, &enc_len) != 0) {
|
||||
if (media_ccm_encrypt(c->content_key, nonce, data, data_len, enc, &enc_len) != 0) {
|
||||
u_free(enc);
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DM, "%s: encrypt failed conv=%s", DM_ID, c->conv_id);
|
||||
return -1;
|
||||
@@ -483,7 +483,7 @@ int dm_accept_message(struct UTUN_INSTANCE* inst, const uint8_t* msg, size_t len
|
||||
uint8_t nonce[DM_NONCE_SIZE], plain[DM_DATA_MAX + 1];
|
||||
size_t plain_len = 0;
|
||||
dm_build_nonce(conv, author, seq, nonce);
|
||||
if (dm_decrypt(c.content_key, nonce, msg + 35 + msg[32], enc_len, plain, &plain_len) != 0) return -1;
|
||||
if (media_ccm_decrypt(c.content_key, nonce, msg + 35 + msg[32], enc_len, plain, &plain_len) != 0) return -1;
|
||||
if (dm_message_media(msg, len)) {
|
||||
struct attachment_info info;
|
||||
if (attachment_decode(plain, plain_len, &info)) return -1;
|
||||
@@ -978,7 +978,7 @@ int dm_list_messages_json(struct UTUN_INSTANCE* inst, const char* conv_id, int c
|
||||
uint8_t nonce[DM_NONCE_SIZE];
|
||||
dm_build_nonce(conv_num, author, seq, nonce);
|
||||
if (enc_len < DM_TAG_SIZE || enc_len > DM_DATA_MAX + DM_TAG_SIZE ||
|
||||
dm_decrypt(c.content_key, nonce, enc, (size_t)enc_len, (uint8_t*)text, &plen)) goto fail;
|
||||
media_ccm_decrypt(c.content_key, nonce, enc, (size_t)enc_len, (uint8_t*)text, &plen)) goto fail;
|
||||
text[plen] = '\0';
|
||||
const char* group_name = "";
|
||||
int invite = ct && !strcmp(ct, "invite");
|
||||
|
||||
+19
-19
@@ -41,7 +41,7 @@ static int test_media_stream(const uint8_t key1[32], const uint8_t key2[32], uin
|
||||
}
|
||||
rewind(src);
|
||||
uint64_t reported_size = 0;
|
||||
if (dm_media_encrypt_stream(key1, author, id, src, cipher, &reported_size, hash) != 0 || reported_size != size) {
|
||||
if (media_encrypt_stream(key1, author, id, src, cipher, &reported_size, hash) != 0 || reported_size != size) {
|
||||
failed = 1;
|
||||
goto done;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ static int test_media_stream(const uint8_t key1[32], const uint8_t key2[32], uin
|
||||
size_t records = size ? (size + DM_MEDIA_PLAIN_SIZE - 1) / DM_MEDIA_PLAIN_SIZE : 1;
|
||||
if (cipher_size != (long)(size + records * DM_TAG_SIZE)) { failed = 1; goto done; }
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(key2, author, id, size, hash, cipher, plain) != 0 || ftell(plain) != (long)size) {
|
||||
if (media_decrypt_stream(key2, author, id, size, hash, cipher, plain) != 0 || ftell(plain) != (long)size) {
|
||||
failed = 1;
|
||||
goto done;
|
||||
}
|
||||
@@ -63,16 +63,16 @@ static int test_media_stream(const uint8_t key1[32], const uint8_t key2[32], uin
|
||||
/* Неправильный получатель, автор, ID и manifest hash не принимаются. */
|
||||
uint8_t wrong_key[32] = {0};
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(wrong_key, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(wrong_key, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(key2, author + 1, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(key2, author + 1, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
id[1] = 1;
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
id[1] = 0;
|
||||
hash[0] ^= 1;
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
hash[0] ^= 1;
|
||||
/* Обрезанный файл и лишние байты обнаруживаются независимо от тегов порций. */
|
||||
rewind(cipher);
|
||||
@@ -81,11 +81,11 @@ static int test_media_stream(const uint8_t key1[32], const uint8_t key2[32], uin
|
||||
if (ch == EOF || fputc(ch, short_cipher) == EOF) { failed = 1; goto done; }
|
||||
}
|
||||
rewind(short_cipher);
|
||||
if (dm_media_decrypt_stream(key2, author, id, size, hash, short_cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(key2, author, id, size, hash, short_cipher, plain) == 0) failed = 1;
|
||||
fseek(cipher, 0, SEEK_END);
|
||||
if (fputc(1, cipher) == EOF) { failed = 1; goto done; }
|
||||
rewind(cipher);
|
||||
if (dm_media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
if (media_decrypt_stream(key2, author, id, size, hash, cipher, plain) == 0) failed = 1;
|
||||
done:
|
||||
fclose(src);
|
||||
fclose(cipher);
|
||||
@@ -147,7 +147,7 @@ int main(void) {
|
||||
|
||||
uint8_t enc[256];
|
||||
size_t enc_len = 0;
|
||||
if (dm_encrypt(key1, nonce, (const uint8_t*)text, text_len, enc, &enc_len) != 0) {
|
||||
if (media_ccm_encrypt(key1, nonce, (const uint8_t*)text, text_len, enc, &enc_len) != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DM, "encrypt failed");
|
||||
failures++;
|
||||
} else if (enc_len != text_len + DM_TAG_SIZE) {
|
||||
@@ -156,7 +156,7 @@ int main(void) {
|
||||
} else {
|
||||
uint8_t dec[256];
|
||||
size_t dec_len = 0;
|
||||
if (dm_decrypt(key2, nonce, enc, enc_len, dec, &dec_len) != 0) {
|
||||
if (media_ccm_decrypt(key2, nonce, enc, enc_len, dec, &dec_len) != 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DM, "decrypt failed");
|
||||
failures++;
|
||||
} else if (dec_len != text_len || memcmp(dec, text, text_len) != 0) {
|
||||
@@ -173,7 +173,7 @@ int main(void) {
|
||||
memset(wrong, 0x5A, sizeof(wrong));
|
||||
uint8_t dec[256];
|
||||
size_t dec_len = 0;
|
||||
if (enc_len > 0 && dm_decrypt(wrong, nonce, enc, enc_len, dec, &dec_len) == 0) {
|
||||
if (enc_len > 0 && media_ccm_decrypt(wrong, nonce, enc, enc_len, dec, &dec_len) == 0) {
|
||||
DEBUG_ERROR(DEBUG_CATEGORY_DM, "decrypt with wrong key should fail");
|
||||
failures++;
|
||||
} else {
|
||||
@@ -191,20 +191,20 @@ int main(void) {
|
||||
for (size_t i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
|
||||
size_t n = sizes[i], clen = 0, plen = 0;
|
||||
dm_build_nonce(id_ab, a, 100 + i, nonce);
|
||||
if (dm_encrypt(key1, nonce, n ? large : NULL, n, cipher, &clen) != 0 || clen != n + DM_TAG_SIZE ||
|
||||
dm_decrypt(key2, nonce, cipher, clen, restored, &plen) != 0 || plen != n || memcmp(large, restored, n)) failures++;
|
||||
if (media_ccm_encrypt(key1, nonce, n ? large : NULL, n, cipher, &clen) != 0 || clen != n + DM_TAG_SIZE ||
|
||||
media_ccm_decrypt(key2, nonce, cipher, clen, restored, &plen) != 0 || plen != n || memcmp(large, restored, n)) failures++;
|
||||
cipher[clen - 1] ^= 1;
|
||||
if (dm_decrypt(key2, nonce, cipher, clen, restored, &plen) == 0 || plen != 0) failures++;
|
||||
if (media_ccm_decrypt(key2, nonce, cipher, clen, restored, &plen) == 0 || plen != 0) failures++;
|
||||
}
|
||||
if (dm_encrypt(key1, nonce, large, DM_CCM_PLAIN_MAX + 1, cipher, &enc_len) == 0) failures++;
|
||||
if (media_ccm_encrypt(key1, nonce, large, DM_CCM_PLAIN_MAX + 1, cipher, &enc_len) == 0) failures++;
|
||||
uint8_t media_id[16] = {1}, media_nonce[DM_NONCE_SIZE], other_nonce[DM_NONCE_SIZE];
|
||||
dm_build_media_nonce(a, media_id, 0, media_nonce);
|
||||
dm_build_media_nonce(a, media_id, 1, other_nonce);
|
||||
media_cipher_nonce(a, media_id, 0, media_nonce);
|
||||
media_cipher_nonce(a, media_id, 1, other_nonce);
|
||||
if (!memcmp(media_nonce, other_nonce, DM_NONCE_SIZE)) failures++;
|
||||
dm_build_media_nonce(b, media_id, 0, other_nonce);
|
||||
media_cipher_nonce(b, media_id, 0, other_nonce);
|
||||
if (!memcmp(media_nonce, other_nonce, DM_NONCE_SIZE)) failures++;
|
||||
media_id[1] = 1;
|
||||
dm_build_media_nonce(a, media_id, 0, other_nonce);
|
||||
media_cipher_nonce(a, media_id, 0, other_nonce);
|
||||
if (!memcmp(media_nonce, other_nonce, DM_NONCE_SIZE)) failures++;
|
||||
dm_build_nonce(id_ab, a, 0, other_nonce);
|
||||
if (!memcmp(media_nonce, other_nonce, DM_NONCE_SIZE)) failures++;
|
||||
|
||||
+1
-1
@@ -262,7 +262,7 @@ static size_t test_body(struct UTUN_INSTANCE* author, uint64_t seq, const char*
|
||||
body[32] = 4;
|
||||
memcpy(body + 33, "text", 4);
|
||||
size_t enc_len = 0;
|
||||
if (dm_encrypt(key, nonce, (const uint8_t*)text, strlen(text), body + 39, &enc_len) != 0) return 0;
|
||||
if (media_ccm_encrypt(key, nonce, (const uint8_t*)text, strlen(text), body + 39, &enc_len) != 0) return 0;
|
||||
uint16_t n = (uint16_t)enc_len;
|
||||
memcpy(body + 37, &n, 2);
|
||||
if (sc_ed25519_sign(author->my_ed25519_privkey, body, 39 + enc_len, body + 39 + enc_len) != SC_OK) return 0;
|
||||
|
||||
@@ -45,11 +45,11 @@ static void work(void* arg) {
|
||||
rewind(source); rewind(cipher);
|
||||
/* Каждый новый шифротекст получает отдельный media_id. */
|
||||
memcpy(id, &t->encryptions, sizeof(t->encryptions));
|
||||
if (dm_media_encrypt_stream(key, 42, id, source, cipher, &size, hash)) goto done;
|
||||
if (media_encrypt_stream(key, 42, id, source, cipher, &size, hash)) goto done;
|
||||
t->encryptions++;
|
||||
} while (get_time_tb() < until);
|
||||
rewind(cipher);
|
||||
if (size != 8 * 1024 * 1024 || dm_media_decrypt_stream(key, 42, id, size, hash, cipher, plain)) goto done;
|
||||
if (size != 8 * 1024 * 1024 || media_decrypt_stream(key, 42, id, size, hash, cipher, plain)) goto done;
|
||||
rewind(plain);
|
||||
SHA256_Init(&digest);
|
||||
size_t n;
|
||||
|
||||
Reference in new issue
Block a user