ssharcf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Arcfour (RC4) implementation for PuTTY.
  3. *
  4. * Coded from Schneier.
  5. */
  6. #include <assert.h>
  7. #include "ssh.h"
  8. typedef struct {
  9. unsigned char i, j, s[256];
  10. ssh_cipher ciph;
  11. } ArcfourContext;
  12. static void arcfour_block(void *handle, void *vblk, int len)
  13. {
  14. unsigned char *blk = (unsigned char *)vblk;
  15. ArcfourContext *ctx = (ArcfourContext *)handle;
  16. unsigned k;
  17. unsigned char tmp, i, j, *s;
  18. s = ctx->s;
  19. i = ctx->i; j = ctx->j;
  20. for (k = 0; (int)k < len; k++) {
  21. i = (i + 1) & 0xff;
  22. j = (j + s[i]) & 0xff;
  23. tmp = s[i]; s[i] = s[j]; s[j] = tmp;
  24. blk[k] ^= s[(s[i]+s[j]) & 0xff];
  25. }
  26. ctx->i = i; ctx->j = j;
  27. }
  28. static void arcfour_setkey(ArcfourContext *ctx, unsigned char const *key,
  29. unsigned keybytes)
  30. {
  31. unsigned char tmp, k[256], *s;
  32. unsigned i, j;
  33. s = ctx->s;
  34. assert(keybytes <= 256);
  35. ctx->i = ctx->j = 0;
  36. for (i = 0; i < 256; i++) {
  37. s[i] = i;
  38. k[i] = key[i % keybytes];
  39. }
  40. j = 0;
  41. for (i = 0; i < 256; i++) {
  42. j = (j + s[i] + k[i]) & 0xff;
  43. tmp = s[i]; s[i] = s[j]; s[j] = tmp;
  44. }
  45. }
  46. /* -- Interface with PuTTY -- */
  47. /*
  48. * We don't implement Arcfour in SSH-1 because it's utterly insecure in
  49. * several ways. See CERT Vulnerability Notes VU#25309, VU#665372,
  50. * and VU#565052.
  51. *
  52. * We don't implement the "arcfour" algorithm in SSH-2 because it doesn't
  53. * stir the cipher state before emitting keystream, and hence is likely
  54. * to leak data about the key.
  55. */
  56. static ssh_cipher *arcfour_new(const ssh_cipheralg *alg)
  57. {
  58. ArcfourContext *ctx = snew(ArcfourContext);
  59. ctx->ciph.vt = alg;
  60. return &ctx->ciph;
  61. }
  62. static void arcfour_free(ssh_cipher *cipher)
  63. {
  64. ArcfourContext *ctx = container_of(cipher, ArcfourContext, ciph);
  65. smemclr(ctx, sizeof(*ctx));
  66. sfree(ctx);
  67. }
  68. static void arcfour_stir(ArcfourContext *ctx)
  69. {
  70. unsigned char *junk = snewn(1536, unsigned char);
  71. memset(junk, 0, 1536);
  72. arcfour_block(ctx, junk, 1536);
  73. smemclr(junk, 1536);
  74. sfree(junk);
  75. }
  76. static void arcfour_ssh2_setiv(ssh_cipher *cipher, const void *key)
  77. {
  78. /* As a pure stream cipher, Arcfour has no IV separate from the key */
  79. }
  80. static void arcfour_ssh2_setkey(ssh_cipher *cipher, const void *key)
  81. {
  82. ArcfourContext *ctx = container_of(cipher, ArcfourContext, ciph);
  83. arcfour_setkey(ctx, key, ctx->ciph.vt->padded_keybytes);
  84. arcfour_stir(ctx);
  85. }
  86. static void arcfour_ssh2_block(ssh_cipher *cipher, void *blk, int len)
  87. {
  88. ArcfourContext *ctx = container_of(cipher, ArcfourContext, ciph);
  89. arcfour_block(ctx, blk, len);
  90. }
  91. const ssh_cipheralg ssh_arcfour128_ssh2 = {
  92. arcfour_new, arcfour_free, arcfour_ssh2_setiv, arcfour_ssh2_setkey,
  93. arcfour_ssh2_block, arcfour_ssh2_block, NULL, NULL,
  94. "arcfour128",
  95. 1, 128, 16, 0, "Arcfour-128",
  96. NULL
  97. };
  98. const ssh_cipheralg ssh_arcfour256_ssh2 = {
  99. arcfour_new, arcfour_free, arcfour_ssh2_setiv, arcfour_ssh2_setkey,
  100. arcfour_ssh2_block, arcfour_ssh2_block, NULL, NULL,
  101. "arcfour256",
  102. 1, 256, 32, 0, "Arcfour-256",
  103. NULL
  104. };
  105. static const ssh_cipheralg *const arcfour_list[] = {
  106. &ssh_arcfour256_ssh2,
  107. &ssh_arcfour128_ssh2,
  108. };
  109. const ssh2_ciphers ssh2_arcfour = { lenof(arcfour_list), arcfour_list };