Skip to Content

Crypto

The primitives an MTProto client and the tdata format need, all on the Rust core. createCryptoProvider() in opentele2/mtcute wraps them as an mtcute ICryptoProvider.

index.d.ts
function aesIgeEncrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer function aesIgeDecrypt(data: Uint8Array, key: Uint8Array, iv: Uint8Array): Buffer class AesCtr { constructor(key: Uint8Array, iv: Uint8Array) process(data: Uint8Array): Buffer } function sha1(data: Uint8Array): Buffer function sha256(data: Uint8Array): Buffer function hmacSha256(key: Uint8Array, data: Uint8Array): Buffer function pbkdf2(password: string | Uint8Array, salt: Uint8Array, iterations: number, keyLen?: number | null, digest?: string | null): Promise<Buffer> function createLocalKey(passcode: string | Uint8Array, salt: Uint8Array): Promise<Buffer> function createLegacyLocalKey(passcode: string | Uint8Array, salt: Uint8Array): Promise<Buffer> function argon2id(passcode: string | Uint8Array, salt: Uint8Array, memoryKib: number, time: number, lanes: number): Promise<Buffer> function factorizePq(pq: Uint8Array): [Buffer, Buffer] function gzip(data: Uint8Array, maxSize?: number | null): Buffer | null function gunzip(data: Uint8Array): Buffer function randomBytes(size: number): Buffer function randomFill(buf: Uint8Array): void
NameDescription
aesIgeEncrypt, aesIgeDecryptAES-256-IGE; data must be whole 16-byte blocks and key and iv 32 bytes each, otherwise OT_BAD_LENGTH.
AesCtrAn AES-256-CTR keystream (OpenSSL aes-256-ctr: the 16-byte IV is a 128-bit big-endian counter) under a 32-byte key; it continues across process calls, and encryption and decryption are the same operation.
sha1, sha256The 20-byte SHA-1 and 32-byte SHA-256 of data.
hmacSha256(key, data)The 32-byte HMAC-SHA256 of data under a key of any length; the key comes first.
pbkdf2(...)PBKDF2 of keyLen bytes (default 64) over the HMAC of digest: "sha1", "sha256" or "sha512" (the default). Zero iterations or another digest rejects with OT_INVALID_INPUT.
createLocalKey(passcode, salt)The 256-byte Telegram Desktop local key (Storage::CreateLocalKey).
createLegacyLocalKey(passcode, salt)The 256-byte legacy local key (Storage::CreateLegacyLocalKey).
argon2id(...)A 256-byte Argon2id (version 0x13) passcode wrap key; memoryKib is the memory cost, time the passes, lanes the parallelism. An empty passcode, a salt under 8 bytes or a cost above its cap rejects with OT_BAD_LENGTH.
factorizePq(pq)The factors [p, q] of the MTProto pq, p <= q, as minimal big-endian bytes; pq is 1 to 8 bytes, and 0, 1 or a prime throws OT_INVALID_INPUT.
gzip(data, maxSize)data gzipped at the default level, or null when the output would exceed maxSize bytes.
gunzip(data)One decompressed gzip member; anything else throws OT_IO.
randomBytes(size), randomFill(buf)Bytes from the operating system’s CSPRNG.

A string passcode or password is UTF-8 encoded. The key derivations run as async tasks off the main thread.

Last updated on