1/*
2 This file is part of Mtproto-proxy Library.
3
4 Mtproto-proxy Library is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 Mtproto-proxy Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright 2012-2013 Vkontakte Ltd
18 2012-2013 Anton Maydell
19
20 Copyright 2014-2016 Telegram Messenger Inc
21 2014-2016 Anton Maydell
22*/
23
24#pragma once
25
26#include <openssl/aes.h>
27
28struct aesni256_ctx {
29 unsigned char a[256];
30};
31
32//TODO: move cbc_crypt, ige_crypt, ctr_crypt to the virtual method table
33struct tg_aes_ctx;
34
35struct tg_aes_methods {
36 void (*cbc_crypt) (struct tg_aes_ctx *ctx, const unsigned char *in, unsigned char *out, int size, unsigned char iv[16]);
37 void (*ige_crypt) (struct tg_aes_ctx *ctx, const unsigned char *in, unsigned char *out, int size, unsigned char iv[32]);
38 void (*ctr_crypt) (struct tg_aes_ctx *ctx, const unsigned char *in, unsigned char *out, int size, unsigned char iv[16], unsigned long long offset);
39 void (*ctr128_crypt) (struct tg_aes_ctx *ctx, const unsigned char *in, unsigned char *out, int size, unsigned char iv[16], unsigned char ecount_buf[16], unsigned int *num);
40};
41
42typedef struct tg_aes_ctx {
43 union {
44 AES_KEY key;
45 struct aesni256_ctx ctx;
46 } u;
47 const struct tg_aes_methods *type;
48} tg_aes_ctx_t;
49
50void tg_aes_set_encrypt_key (tg_aes_ctx_t *ctx, unsigned char *key, int bits);
51void tg_aes_set_decrypt_key (tg_aes_ctx_t *ctx, unsigned char *key, int bits);
52void tg_aes_ctx_cleanup (tg_aes_ctx_t *ctx);
53