1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #ifndef MUMBLE_CRYPTSTATE_H_
7 #define MUMBLE_CRYPTSTATE_H_
8 
9 #include <openssl/aes.h>
10 
11 #define AES_KEY_SIZE_BITS   128
12 #define AES_KEY_SIZE_BYTES  (AES_KEY_SIZE_BITS/8)
13 
14 #include "Timer.h"
15 
16 class CryptState {
17 	private:
18 		Q_DISABLE_COPY(CryptState)
19 	public:
20 		unsigned char raw_key[AES_KEY_SIZE_BYTES];
21 		unsigned char encrypt_iv[AES_BLOCK_SIZE];
22 		unsigned char decrypt_iv[AES_BLOCK_SIZE];
23 		unsigned char decrypt_history[0x100];
24 
25 		unsigned int uiGood;
26 		unsigned int uiLate;
27 		unsigned int uiLost;
28 		unsigned int uiResync;
29 
30 		unsigned int uiRemoteGood;
31 		unsigned int uiRemoteLate;
32 		unsigned int uiRemoteLost;
33 		unsigned int uiRemoteResync;
34 
35 		AES_KEY	encrypt_key;
36 		AES_KEY decrypt_key;
37 		Timer tLastGood;
38 		Timer tLastRequest;
39 		bool bInit;
40 		CryptState();
41 
42 		bool isValid() const;
43 		void genKey();
44 		void setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div);
45 		void setDecryptIV(const unsigned char *iv);
46 
47 		bool ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len, const unsigned char *nonce, unsigned char *tag);
48 		bool ocb_decrypt(const unsigned char *encrypted, unsigned char *plain, unsigned int len, const unsigned char *nonce, unsigned char *tag);
49 
50 		bool decrypt(const unsigned char *source, unsigned char *dst, unsigned int crypted_length);
51 		bool encrypt(const unsigned char *source, unsigned char *dst, unsigned int plain_length);
52 };
53 
54 #endif
55