2005-04-15 02:58:29 +04:00
|
|
|
/*
|
|
|
|
* Arcfour (RC4) implementation for PuTTY.
|
|
|
|
*
|
|
|
|
* Coded from Schneier.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include "ssh.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned char i, j, s[256];
|
2018-09-13 16:43:04 +03:00
|
|
|
ssh2_cipher vt;
|
2005-04-15 02:58:29 +04:00
|
|
|
} ArcfourContext;
|
|
|
|
|
2018-05-26 10:31:34 +03:00
|
|
|
static void arcfour_block(void *handle, void *vblk, int len)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-05-26 10:31:34 +03:00
|
|
|
unsigned char *blk = (unsigned char *)vblk;
|
2005-04-15 02:58:29 +04:00
|
|
|
ArcfourContext *ctx = (ArcfourContext *)handle;
|
|
|
|
unsigned k;
|
|
|
|
unsigned char tmp, i, j, *s;
|
|
|
|
|
|
|
|
s = ctx->s;
|
|
|
|
i = ctx->i; j = ctx->j;
|
2007-01-09 21:24:07 +03:00
|
|
|
for (k = 0; (int)k < len; k++) {
|
2005-04-15 02:58:29 +04:00
|
|
|
i = (i + 1) & 0xff;
|
|
|
|
j = (j + s[i]) & 0xff;
|
|
|
|
tmp = s[i]; s[i] = s[j]; s[j] = tmp;
|
|
|
|
blk[k] ^= s[(s[i]+s[j]) & 0xff];
|
|
|
|
}
|
|
|
|
ctx->i = i; ctx->j = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void arcfour_setkey(ArcfourContext *ctx, unsigned char const *key,
|
|
|
|
unsigned keybytes)
|
|
|
|
{
|
|
|
|
unsigned char tmp, k[256], *s;
|
|
|
|
unsigned i, j;
|
|
|
|
|
|
|
|
s = ctx->s;
|
|
|
|
assert(keybytes <= 256);
|
|
|
|
ctx->i = ctx->j = 0;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
s[i] = i;
|
|
|
|
k[i] = key[i % keybytes];
|
|
|
|
}
|
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
j = (j + s[i] + k[i]) & 0xff;
|
|
|
|
tmp = s[i]; s[i] = s[j]; s[j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- Interface with PuTTY -- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't implement Arcfour in SSH-1 because it's utterly insecure in
|
|
|
|
* several ways. See CERT Vulnerability Notes VU#25309, VU#665372,
|
|
|
|
* and VU#565052.
|
|
|
|
*
|
|
|
|
* We don't implement the "arcfour" algorithm in SSH-2 because it doesn't
|
|
|
|
* stir the cipher state before emitting keystream, and hence is likely
|
|
|
|
* to leak data about the key.
|
|
|
|
*/
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static ssh2_cipher *arcfour_new(const struct ssh2_cipheralg *alg)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-09-13 16:43:04 +03:00
|
|
|
ArcfourContext *ctx = snew(ArcfourContext);
|
|
|
|
ctx->vt = alg;
|
|
|
|
return &ctx->vt;
|
2005-04-15 02:58:29 +04:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static void arcfour_free(ssh2_cipher *cipher)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-10-06 01:49:08 +03:00
|
|
|
ArcfourContext *ctx = container_of(cipher, ArcfourContext, vt);
|
2018-09-13 16:43:04 +03:00
|
|
|
smemclr(ctx, sizeof(*ctx));
|
|
|
|
sfree(ctx);
|
2005-04-15 02:58:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arcfour_stir(ArcfourContext *ctx)
|
|
|
|
{
|
|
|
|
unsigned char *junk = snewn(1536, unsigned char);
|
|
|
|
memset(junk, 0, 1536);
|
|
|
|
arcfour_block(ctx, junk, 1536);
|
2012-07-22 23:51:50 +04:00
|
|
|
smemclr(junk, 1536);
|
2005-04-15 02:58:29 +04:00
|
|
|
sfree(junk);
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static void arcfour_ssh2_setiv(ssh2_cipher *cipher, const void *key)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-09-13 16:43:04 +03:00
|
|
|
/* As a pure stream cipher, Arcfour has no IV separate from the key */
|
2005-04-15 02:58:29 +04:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static void arcfour_ssh2_setkey(ssh2_cipher *cipher, const void *key)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-10-06 01:49:08 +03:00
|
|
|
ArcfourContext *ctx = container_of(cipher, ArcfourContext, vt);
|
2018-09-13 16:43:04 +03:00
|
|
|
arcfour_setkey(ctx, key, ctx->vt->padded_keybytes);
|
2005-04-15 02:58:29 +04:00
|
|
|
arcfour_stir(ctx);
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static void arcfour_ssh2_block(ssh2_cipher *cipher, void *blk, int len)
|
2005-04-15 02:58:29 +04:00
|
|
|
{
|
2018-10-06 01:49:08 +03:00
|
|
|
ArcfourContext *ctx = container_of(cipher, ArcfourContext, vt);
|
2018-09-13 16:43:04 +03:00
|
|
|
arcfour_block(ctx, blk, len);
|
2005-04-15 02:58:29 +04:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
const struct ssh2_cipheralg ssh_arcfour128_ssh2 = {
|
|
|
|
arcfour_new, arcfour_free, arcfour_ssh2_setiv, arcfour_ssh2_setkey,
|
|
|
|
arcfour_ssh2_block, arcfour_ssh2_block, NULL, NULL,
|
2005-09-03 21:03:49 +04:00
|
|
|
"arcfour128",
|
2015-09-10 10:10:52 +03:00
|
|
|
1, 128, 16, 0, "Arcfour-128",
|
2015-06-07 15:40:11 +03:00
|
|
|
NULL
|
2005-04-15 02:58:29 +04:00
|
|
|
};
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
const struct ssh2_cipheralg ssh_arcfour256_ssh2 = {
|
|
|
|
arcfour_new, arcfour_free, arcfour_ssh2_setiv, arcfour_ssh2_setkey,
|
|
|
|
arcfour_ssh2_block, arcfour_ssh2_block, NULL, NULL,
|
2005-09-03 21:03:49 +04:00
|
|
|
"arcfour256",
|
2015-09-10 10:10:52 +03:00
|
|
|
1, 256, 32, 0, "Arcfour-256",
|
2015-06-07 15:40:11 +03:00
|
|
|
NULL
|
2005-04-15 02:58:29 +04:00
|
|
|
};
|
|
|
|
|
2018-09-13 16:43:04 +03:00
|
|
|
static const struct ssh2_cipheralg *const arcfour_list[] = {
|
2005-04-21 00:57:00 +04:00
|
|
|
&ssh_arcfour256_ssh2,
|
2005-04-15 02:58:29 +04:00
|
|
|
&ssh_arcfour128_ssh2,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct ssh2_ciphers ssh2_arcfour = {
|
|
|
|
sizeof(arcfour_list) / sizeof(*arcfour_list),
|
|
|
|
arcfour_list
|
|
|
|
};
|