20 #include <winpr/assert.h>
26 struct winpr_int_rc4_ctx
34 static void swap(BYTE* p1, BYTE* p2)
41 winpr_int_RC4_CTX* winpr_int_rc4_new(
const BYTE* key,
size_t keylength)
43 winpr_int_RC4_CTX* ctx = calloc(1,
sizeof(winpr_int_RC4_CTX));
47 for (
size_t i = 0; i < CTX_SIZE; i++)
50 ctx->t[i] = key[i % keylength];
54 for (
size_t i = 0; i < CTX_SIZE; i++)
56 j = (j + ctx->s[i] + ctx->t[i]) % CTX_SIZE;
57 swap(&ctx->s[i], &ctx->s[j]);
62 void winpr_int_rc4_free(winpr_int_RC4_CTX* ctx)
67 BOOL winpr_int_rc4_update(winpr_int_RC4_CTX* ctx,
size_t length,
const BYTE* input, BYTE* output)
73 for (
size_t i = 0; i < length; i++)
75 t1 = (t1 + 1) % CTX_SIZE;
76 t2 = (t2 + ctx->s[t1]) % CTX_SIZE;
77 swap(&ctx->s[t1], &ctx->s[t2]);
79 const size_t idx = ((size_t)ctx->s[t1] + ctx->s[t2]) % CTX_SIZE;
80 const BYTE val = ctx->s[idx];
81 const BYTE out = *input++ ^ val;