yum-slop/TaSTT

Free self-hosted STT for VRChat.

git clone https://git.yummers.dev/yum-slop/TaSTT

yumFinish python virtual envee8213d

v0.19.2
3.4 KiB92 linesraw
1#!/usr/bin/env python3
2
3# This module is used to implement obfuscation of TaSTT network
4# speech data. At a high level, TaSTT is simply streaming N bits of
5# arbitrary data to a shader via VRChat's parameter sync mechanism.
6#
7# It would be trivial to mine this data for speech information, since
8# we're sending unicode (or ASCII) characters to peers.
9#
10# To raise the cost for the casual data collector, we can obfuscate
11# this data using a one-time pad in cipher-block chaining mode.
12#
13# Making things interesting, encrypted data will arrive at the Unity
14# animator,  which processes them in 8 bit chunks. They are written
15# into contiguous blocks of the animator. Thus the shader can decrypt
16# the board by decrypting each block. This is thus stronger than
17# applying a one-time pad to each byte of the plaintext, since the
18# statistical distribution of individual letters is destroyed.
19# Obviously due to the lack of an initialization vector, the
20# distribution of phrases (blocks) is preserved.
21
22import math
23import os
24
25def genKey(n_bits = 128) -> bytearray:
26    return os.urandom(int(n_bits / 8))
27
28def saveKey(filename: str, key: str):
29    with open(filename, "wb") as f:
30        f.write(key)
31
32def loadKey(filename: str) -> bytearray:
33    with open(filename, "rb") as f:
34        return f.read()
35
36# Apply a symmetric cypher to `data` using cypher-block chaining.
37def obfuscate(data: bytearray, key: bytearray) -> str:
38    n_blocks = int(math.ceil(len(data) / len(key)))
39    # This is a misnomer. A true IV would be randomized, but we can't
40    # do that since the shader doesn't have access to it. We just use
41    # this to implement the "chaining" aspect of CBC.
42    iv = bytearray(b'\x00') * len(key)
43    result = bytearray()
44    for i in range(0, n_blocks):
45        block_begin = i * len(key)
46        block_end = (i + 1) * len(key)
47        block_plain = data[block_begin:block_end]
48        block_cypher = block_plain.copy()
49        for i in range(0, len(block_cypher)):
50            block_cypher[i] ^= iv[i]
51            block_cypher[i] ^= key[i]
52        result += block_cypher
53        iv = block_cypher
54    return result
55
56def deobfuscate(data: bytearray, key: bytearray) -> str:
57    n_blocks = int(math.ceil(len(data) / len(key)))
58    # This is a misnomer. A true IV would be randomized, but we can't
59    # do that since the shader doesn't have access to it. We just use
60    # this to implement the "chaining" aspect of CBC.
61    iv = bytearray(b'\x00') * len(key)
62    result = bytearray()
63    for i in range(0, n_blocks):
64        block_begin = i * len(key)
65        block_end = (i + 1) * len(key)
66        block_cypher = data[block_begin:block_end]
67        block_plain = block_cypher.copy()
68        for i in range(0, len(block_plain)):
69            block_plain[i] ^= key[i]
70            block_plain[i] ^= iv[i]
71        result += block_plain
72        iv = block_cypher
73    return result
74
75def test():
76    key = genKey()
77    saveKey("test.key", key)
78    new_key = loadKey("test.key")
79    os.remove("test.key")
80    assert(key == new_key)
81
82    plaintext_original = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
83    plaintext_bytes = bytearray(plaintext_original, "utf-8")
84    cyphertext = obfuscate(plaintext_bytes, key)
85    assert(len(plaintext_bytes) == len(cyphertext))
86    plaintext_recovered = deobfuscate(cyphertext, key).decode("utf-8")
87    assert(plaintext_original == plaintext_recovered)
88    assert(plaintext_bytes != cyphertext)
89
90if __name__ == "__main__":
91    test()
92