summaryrefslogtreecommitdiffstats
path: root/crypto_buffer.h
blob: 51e233ea3197a01fbac133f5eb228cfa324def71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef CRYPTO_BUFFER_H
#define CRYPTO_BUFFER_H

#include "uint.h"
#include "blowfish.h"
#include "twofish.h"
#include "aes.h"
#include "des.h"

struct info{
	u32 strlen;				/* number of uint8 of clear data */
	u32 bytes;				/* number of bytes of encrypted data */
};
#define ENCRYPT		0x0000
#define DECRYPT		0x0001
#define BLOWFISH	0x0002
#define TWOFISH		0x0004
#define AES		0x0008
#define DES		0x0010
#define DES3_EDE	0x0020

typedef int (*operation)(int ,u8* ,u32);

typedef struct crypto_buffer {
	u8 *c_buffer;					/* clear data buffer */
	u8* c_ptr;					/* clear data to read */
	u8* c_end;					/* end of c_buffer */
	u8 *e_buffer;					/* encrypted data buffer */
	u8* e_ptr;					/* encrypted data to read */
	u8* e_end;					/* end of e_buffer */
	u32 e_len;					/* length of e_buffer */
	u32 block_size;					/* size of blocks used by cipher */
	struct info *e_data;				/* pointer for write operations */
	struct info data_st;				/* info structure for read and write operations */
	int fd;						/* file descriptor */
	operation op;					/* read/write operation */
	void (*cipher)(void*, u8 *, const u8 *);	/* encryption/decryption function */
	union{						/* cipher ctx */
		struct blowfish_ctx bf_cxt;
		struct twofish_ctx tf_cxt;
	}ctx;
} crypto_buffer;


/* initialize a buffer for read or write operation
 *	buffer struct
 *	file descriptor
 *	read or write operation
 *	number of blocks in buffer
 *	type of cipher (BLOWFISH/TWOFISH/AES | ENCRYPT/DECRYPT)
 *	encryption key
 *	length of encryption key
 */
int crypto_buffer_init(crypto_buffer *b, int fd, operation op, u32 blocks, int type, u8 *key, u32 k_len);


/* force write on fd */
int crypto_buffer_flush(crypto_buffer *b);

/* read data from buffer */
int crypto_buffer_get(crypto_buffer *b, u8 *s, u32 len);

/* add data to buffer */
int crypto_buffer_put(crypto_buffer *b, const u8 *s, u32 len);

/* add data to buffer then force write on fd */
int crypto_buffer_putflush(crypto_buffer *b, const u8 *s, u32 len);

/* read/write to fd */
int u8_unix_read(int fd, u8 *s, u32 len);
int u8_unix_write(int fd, u8 *s, u32 len);
/* not really usefull read/write also work */
int u8_tcp_read(int sd, u8 *s, u32 len);
int u8_tcp_write(int sd, u8 *s, u32 len);


#endif