20 #include <winpr/assert.h>
21 #include <winpr/cast.h>
27 struct winpr_int_rc4_ctx
35 static void swap(BYTE* p1, BYTE* p2)
42 winpr_int_RC4_CTX* winpr_int_rc4_new(
const BYTE* key,
size_t keylength)
44 winpr_int_RC4_CTX* ctx = calloc(1,
sizeof(winpr_int_RC4_CTX));
48 for (
size_t i = 0; i < CTX_SIZE; i++)
50 ctx->s[i] = WINPR_ASSERTING_INT_CAST(BYTE, i);
51 ctx->t[i] = key[i % keylength];
55 for (
size_t i = 0; i < CTX_SIZE; i++)
57 j = (j + ctx->s[i] + ctx->t[i]) % CTX_SIZE;
58 swap(&ctx->s[i], &ctx->s[j]);
63 void winpr_int_rc4_free(winpr_int_RC4_CTX* ctx)
68 BOOL winpr_int_rc4_update(winpr_int_RC4_CTX* ctx,
size_t length,
const BYTE* input, BYTE* output)
74 for (
size_t i = 0; i < length; i++)
76 t1 = (t1 + 1) % CTX_SIZE;
77 t2 = (t2 + ctx->s[t1]) % CTX_SIZE;
78 swap(&ctx->s[t1], &ctx->s[t2]);
80 const size_t idx = ((size_t)ctx->s[t1] + ctx->s[t2]) % CTX_SIZE;
81 const BYTE val = ctx->s[idx];
82 const BYTE out = *input++ ^ val;