| 1 |
#ifndef UTIL_CRYPT_H |
| 2 |
#define UTIL_CRYPT_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stddef.h> |
| 6 |
#include "utiliof.h" |
| 7 |
|
| 8 |
#ifndef UTIL_CRYPT_TIME |
| 9 |
# define UTIL_CRYPT_TIME 0 |
| 10 |
#endif |
| 11 |
|
| 12 |
/* RC4 */ |
| 13 |
|
| 14 |
typedef uint8_t rc4_map[256]; |
| 15 |
|
| 16 |
typedef struct rc4_state rc4_state; |
| 17 |
|
| 18 |
#define RC4_STATE_ALLOC (1<<0) |
| 19 |
|
| 20 |
UTILAPI rc4_state * rc4_state_initialize (rc4_state *state, rc4_map *map, const void *vkey, size_t keylength); |
| 21 |
#define rc4_state_init(state, vkey, keylength) rc4_state_initialize(state, NULL, vkey, keylength) |
| 22 |
UTILAPI void rc4_map_save (rc4_state *state, rc4_map *map); |
| 23 |
UTILAPI void rc4_map_restore (rc4_state *state, rc4_map *map); |
| 24 |
|
| 25 |
/* Codecs operating on iof */ |
| 26 |
|
| 27 |
UTILAPI iof_status rc4_crypt_state (iof *I, iof *O, rc4_state *state); |
| 28 |
#define rc4_encode_state(I, O, state) rc4_crypt_state(I, O, state) |
| 29 |
#define rc4_decode_state(I, O, state) rc4_crypt_state(I, O, state) |
| 30 |
|
| 31 |
UTILAPI iof_status rc4_crypt (iof *I, iof *O, const void *key, size_t length); |
| 32 |
#define rc4_encode(I, O) rc4_crypt(I, O, key, length) |
| 33 |
#define rc4_decode(I, O) rc4_crypt(I, O, key, length) |
| 34 |
|
| 35 |
UTILAPI size_t rc4_crypt_data (const void *input, size_t length, void *output, const void *key, size_t keylength); |
| 36 |
UTILAPI size_t rc4_crypt_state_data (rc4_state *state, const void *input, size_t length, void *output); |
| 37 |
#define rc4_encode_data(input, length, output, key, keylength) rc4_crypt_data(input, length, output, key, keylength) |
| 38 |
#define rc4_decode_data(input, length, output, key, keylength) rc4_crypt_data(input, length, output, key, keylength) |
| 39 |
#define rc4_encode_state_data(state, input, length, output) rc4_crypt_state_data(state, input, length, output) |
| 40 |
#define rc4_decode_state_data(state, input, length, output) rc4_crypt_state_data(state, input, length, output) |
| 41 |
|
| 42 |
UTILAPI void rc4_state_close (rc4_state *state); |
| 43 |
|
| 44 |
/* AES */ |
| 45 |
|
| 46 |
typedef uint8_t aes_block[4][4]; |
| 47 |
typedef aes_block aes_keyblock[15]; // aes128 - 10+1, aes192 - 12+1, aes256 - 14+1 |
| 48 |
|
| 49 |
typedef struct aes_state aes_state; |
| 50 |
|
| 51 |
#define AES_STATE_ALLOC (1<<0) |
| 52 |
//#define AES_ECB_MODE (1<<2) |
| 53 |
#define AES_HAS_IV (1<<3) |
| 54 |
#define AES_INLINE_IV (1<<4) |
| 55 |
#define AES_CONTINUE (1<<5) |
| 56 |
#define AES_NULL_PADDING (1<<6) |
| 57 |
|
| 58 |
UTILAPI void aes_pdf_mode (aes_state *state); |
| 59 |
//UTILAPI aes_state * aes_state_initialize_ecb (aes_state *State, uint8_t *roundkey, const uint8_t *key); |
| 60 |
UTILAPI aes_state * aes_encode_initialize (aes_state *state, aes_keyblock *keyblock, const void *key, size_t keylength, const void *iv); |
| 61 |
UTILAPI aes_state * aes_decode_initialize (aes_state *state, aes_keyblock *keyblock, const void *key, size_t keylength, const void *iv); |
| 62 |
#define aes_encode_init(state, key, keylength) aes_encode_initialize(state, NULL, key, keylength, NULL) |
| 63 |
#define aes_decode_init(state, key, keylength) aes_decode_initialize(state, NULL, key, keylength, NULL) |
| 64 |
|
| 65 |
UTILAPI void aes_state_close (aes_state *state); |
| 66 |
|
| 67 |
/* Codecs operating on iof */ |
| 68 |
|
| 69 |
UTILAPI iof_status aes_encode_state (iof *I, iof *O, aes_state *state); |
| 70 |
UTILAPI iof_status aes_decode_state (iof *I, iof *O, aes_state *state); |
| 71 |
|
| 72 |
UTILAPI size_t aes_encode_data (const void *input, size_t length, void *output, const void *key, size_t keylength, const void *iv, int flags); |
| 73 |
UTILAPI size_t aes_encode_state_data (aes_state *state, const void *input, size_t length, void *output); |
| 74 |
UTILAPI size_t aes_decode_data (const void *input, size_t length, void *output, const void *key, size_t keylength, const void *iv, int flags); |
| 75 |
UTILAPI size_t aes_decode_state_data (aes_state *state, const void *input, size_t length, void *output); |
| 76 |
|
| 77 |
/* random bytes generator */ |
| 78 |
|
| 79 |
UTILAPI void random_bytes (uint8_t *output, size_t size); |
| 80 |
UTILAPI void aes_generate_iv (uint8_t output[16]); |
| 81 |
|
| 82 |
/* filters */ |
| 83 |
|
| 84 |
iof * iof_filter_rc4_decoder (iof *N, const void *key, size_t keylength); |
| 85 |
iof * iof_filter_rc4_encoder (iof *N, const void *key, size_t keylength); |
| 86 |
|
| 87 |
iof * iof_filter_aes_decoder (iof *N, const void *key, size_t keylength); |
| 88 |
iof * iof_filter_aes_encoder (iof *N, const void *key, size_t keylength); |
| 89 |
|
| 90 |
#endif |