f3a885a1dcf6a0119705455b8218064be7dc6d67
[umurmur.git] / src / crypt.h
1 /* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef CRYPTSTATE_H_34564356
32 #define CRYPTSTATE_H_34564356
33
34 #include "byteorder.h"
35 #include "config.h"
36
37 #if defined(USE_POLARSSL)
38
39 #include <polarssl/havege.h>
40 #include <polarssl/aes.h>
41
42 #define CRYPT_AES_KEY aes_context
43 #define AES_BLOCK_SIZE 16
44
45 #define CRYPT_RANDOM_BYTES(dest, size) RAND_bytes((unsigned char *)(dest), (size))
46 #define CRYPT_SET_ENC_KEY(dest, source, size) aes_setkey_enc((dest), (source), (size));
47 #define CRYPT_SET_DEC_KEY(dest, source, size) aes_setkey_dec((dest), (source), (size));
48
49 #define CRYPT_AES_ENCRYPT(src, dst, cryptstate) aes_crypt_ecb(&(cryptstate)->encrypt_key, AES_ENCRYPT, (unsigned char *)(src), (unsigned char *)(dst));
50 #define CRYPT_AES_DECRYPT(src, dst, cryptstate) aes_crypt_ecb(&(cryptstate)->decrypt_key, AES_DECRYPT, (unsigned char *)(src), (unsigned char *)(dst));
51
52 #elif defined(USE_GNUTLS)
53
54 #include <nettle/aes.h>
55 #include <gnutls/gnutls.h>
56 #include <gnutls/crypto.h>
57
58 #define CRYPT_AES_KEY struct aes_ctx
59 #define CRYPT_RANDOM_BYTES(dest, size) gnutls_rnd(GNUTLS_RND_KEY, (dest), (size))
60 #define CRYPT_SET_ENC_KEY(dest, source, size) aes_set_encrypt_key((dest), (size)/8, (source));
61 #define CRYPT_SET_DEC_KEY(dest, source, size) aes_set_decrypt_key((dest), (size)/8, (source));
62
63 #define CRYPT_AES_ENCRYPT(src, dest, ctx) aes_encrypt(&(ctx)->encrypt_key, AES_BLOCK_SIZE, (uint8_t *)(dest), (uint8_t *)(src))
64 #define CRYPT_AES_DECRYPT(src, dest, ctx) aes_decrypt(&(ctx)->decrypt_key, AES_BLOCK_SIZE, (uint8_t *)(dest), (uint8_t *)(src))
65
66 #else
67
68 #include <openssl/rand.h>
69 #include <openssl/aes.h>
70
71 #define CRYPT_AES_KEY AES_KEY
72 #define CRYPT_RANDOM_BYTES(dest, size) RAND_bytes((unsigned char *)(dest), (size))
73 #define CRYPT_SET_ENC_KEY(dest, source, size) AES_set_encrypt_key((source), (size), (dest));
74 #define CRYPT_SET_DEC_KEY(dest, source, size) AES_set_decrypt_key((source), (size), (dest));
75
76 #define CRYPT_AES_ENCRYPT(src, dst, cryptstate) AES_encrypt((unsigned char *)(src), (unsigned char *)(dst), &(cryptstate)->encrypt_key);
77 #define CRYPT_AES_DECRYPT(src, dst, cryptstate) AES_decrypt((unsigned char *)(src), (unsigned char *)(dst), &(cryptstate)->decrypt_key);
78
79 #endif
80
81 #include <stdint.h>
82 #include "timer.h"
83 #include "types.h"
84
85 typedef struct CryptState {
86         uint8_t raw_key[AES_BLOCK_SIZE];
87         uint8_t encrypt_iv[AES_BLOCK_SIZE];
88         uint8_t decrypt_iv[AES_BLOCK_SIZE];
89         uint8_t decrypt_history[0x100];
90
91         unsigned int uiGood;
92         unsigned int uiLate;
93         unsigned int uiLost;
94         unsigned int uiResync;
95
96         unsigned int uiRemoteGood;
97         unsigned int uiRemoteLate;
98         unsigned int uiRemoteLost;
99         unsigned int uiRemoteResync;
100
101         CRYPT_AES_KEY encrypt_key;
102         CRYPT_AES_KEY decrypt_key;
103
104         etimer_t tLastGood;
105         etimer_t tLastRequest;
106         bool_t bInit;
107 } cryptState_t;
108
109 void CryptState_init(cryptState_t *cs);
110 bool_t CryptState_isValid(cryptState_t *cs);
111 void CryptState_genKey(cryptState_t *cs);
112 void CryptState_setKey(cryptState_t *cs, const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div);
113 void CryptState_setDecryptIV(cryptState_t *cs, const unsigned char *iv);
114
115 bool_t CryptState_decrypt(cryptState_t *cs, const unsigned char *source, unsigned char *dst, unsigned int crypted_length);
116 void CryptState_encrypt(cryptState_t *cs, const unsigned char *source, unsigned char *dst, unsigned int plain_length);
117
118 #endif