59 lines
1.6 KiB
C
59 lines
1.6 KiB
C
#ifndef RC4_CIPHER_H
|
|
#define RC4_CIPHER_H
|
|
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
/**
|
|
* @brief State for the segmented RC4 cipher.
|
|
*
|
|
* @var key The key bytes.
|
|
* @var n The key length.
|
|
* @var hash The multiplicative hash of the key.
|
|
* @var box The KSA-initialized permutation box (length n).
|
|
*/
|
|
typedef struct {
|
|
uint8_t *key;
|
|
int n;
|
|
uint32_t hash;
|
|
uint8_t *box;
|
|
} rc4_cipher_t;
|
|
|
|
/**
|
|
* @brief Initializes an RC4 cipher instance.
|
|
*
|
|
* Runs the Key Scheduling Algorithm on a box initialized with
|
|
* i % 256 and shuffled by the key bytes. Takes ownership of @p key.
|
|
*
|
|
* @param c The cipher instance to initialize.
|
|
* @param key The key bytes (allocated, ownership transferred).
|
|
* @param len The key length.
|
|
*/
|
|
void rc4_cipher_init(rc4_cipher_t *c, uint8_t *key, int len);
|
|
|
|
/**
|
|
* @brief XOR-decrypts a buffer using the segmented RC4 cipher.
|
|
*
|
|
* Processes bytes in regions: first 128 bytes use direct key lookup,
|
|
* subsequent 5120-byte segments use the PRNG from a cloned KSA box
|
|
* with segment-specific skip, and any remaining data as a partial
|
|
* segment.
|
|
*
|
|
* @param c The RC4 cipher instance.
|
|
* @param buf The buffer to decrypt in-place.
|
|
* @param len The length of @p buf.
|
|
* @param offset The file offset at which @p buf starts.
|
|
*/
|
|
void rc4_decrypt(rc4_cipher_t *c, uint8_t *buf, size_t len, int offset);
|
|
|
|
/**
|
|
* @brief Frees resources held by an RC4 cipher instance.
|
|
*
|
|
* Both the key and KSA box buffers are freed.
|
|
*
|
|
* @param c The cipher instance to clean up.
|
|
*/
|
|
void rc4_cipher_free(rc4_cipher_t *c);
|
|
|
|
#endif /* RC4_CIPHER_H */
|