<commit_before># Encryption

MTProto v2 Rev3 enables encryption support to replace or enchanse TLS one. Unlike TLS, actor use multiple encryption schemes at one time. Actor encrypts message with US encryption and then again encrypt with Russian encryption that in result guarantee absolute encryption streight. US encryption is performed with AES-128-CBC and Russian layer is Kuznechik.

We are not invenging the wheel and implement encryption logic exactly as [TLS 1.2 CBC block chipcher](https://tools.ietf.org/html/rfc5246#section-6.2.3.2) is.

In Rev4 we will enable support for Axolotl Ratched like encryption directly in protocol.

## Base Encrypted Package

```
EncryptedPackage {
  HEADER = 0xE8
  // AES Initialization Vector - 16 random bytes
  iv_aes: bytes
  // Kuznechik Initialization Vector - 16 random bytes
  iv_kuznechik: bytes
  // First encryption level - AesEncryptedPackage
  encryptedContent: bytes
}
```

## AES Encrypted Level
Sezialization of package might be 16x bytes in size.
```
AesEncryptedPackage {
  kuznechikPackage: bytes
  
  // HMAC_SHA256(server/client write mac key, kuznechikPackage)
  mac: bytes
  
  // padding is filled with paddingLength value
  padding: bytes
  paddingLenght: byte
}
```

## Kuznechik Encrypted Level

```
KuznechikEncryptedPackage {
  // Plain Package object with signature
  plainPackage: bytes
  
  // HMAC_SHA256(server/client write mac key, plainPackage)
  mac: bytes
  
  // padding is filled with paddingLength value
  padding: bytes
  paddingLenght: byte
}
```
<commit_msg>Update encryption.md<commit_after># Encryption

MTProto v2 Rev3 enables encryption support to replace or enchanse TLS one. Unlike TLS, actor use multiple encryption schemes at one time. Actor encrypts message with US encryption and then again encrypt with Russian encryption that in result guarantee absolute encryption streight. US encryption is performed with AES-128-CBC and Russian layer is Kuznechik.

We are not invenging the wheel and implement encryption logic exactly as [TLS 1.2 CBC block chipcher](https://tools.ietf.org/html/rfc5246#section-6.2.3.2) is.

In Rev4 we will enable support for Axolotl Ratched like encryption directly in protocol.

## Base Encrypted Package

```
EncryptedPackage {
  HEADER = 0xE8
  // AES Initialization Vector - 16 random bytes
  iv_aes: bytes
  // Kuznechik Initialization Vector - 16 random bytes
  iv_kuznechik: bytes
  // First encryption level - AesEncryptedPackage
  encryptedContent: bytes
}
```

## AES Encrypted Level
Sezialization of package might be 16x bytes in size.
```
AesEncryptedPackage {
  kuznechikPackage: bytes
  
  // HMAC_SHA256(server/client write mac key, kuznechikPackage)
  mac: bytes
  
  // padding is filled with paddingLength value
  padding: bytes
  paddingLenght: byte
}
```

## Kuznechik Encrypted Level

```
KuznechikEncryptedPackage {
  // Plain Message object
  plainPackage: bytes
  
  // HMAC_SHA256(server/client write mac key, plainPackage)
  mac: bytes
  
  // padding is filled with paddingLength value
  padding: bytes
  paddingLenght: byte
}
```

{'fraction_non_alphanumeric': 0.06530214424951267, 'fraction_numerical': 0.01949317738791423, 'mean_word_length': 4.072487644151565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6689568', 'n_tokens_mistral': 960, 'n_tokens_neox': 889, 'n_words': 349}