arcfour.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. .new = arcfour_new,
  93. .free = arcfour_free,
  94. .setiv = arcfour_ssh2_setiv,
  95. .setkey = arcfour_ssh2_setkey,
  96. .encrypt = arcfour_ssh2_block,
  97. .decrypt = arcfour_ssh2_block,
  98. .next_message = nullcipher_next_message,
  99. .ssh2_id = "arcfour128",
  100. .blksize = 1,
  101. .real_keybits = 128,
  102. .padded_keybytes = 16,
  103. .flags = 0,
  104. .text_name = "Arcfour-128",
  105. };
  106. const ssh_cipheralg ssh_arcfour256_ssh2 = {
  107. .new = arcfour_new,
  108. .free = arcfour_free,
  109. .setiv = arcfour_ssh2_setiv,
  110. .setkey = arcfour_ssh2_setkey,
  111. .encrypt = arcfour_ssh2_block,
  112. .decrypt = arcfour_ssh2_block,
  113. .next_message = nullcipher_next_message,
  114. .ssh2_id = "arcfour256",
  115. .blksize = 1,
  116. .real_keybits = 256,
  117. .padded_keybytes = 32,
  118. .flags = 0,
  119. .text_name = "Arcfour-256",
  120. };
  121. static const ssh_cipheralg *const arcfour_list[] = {
  122. &ssh_arcfour256_ssh2,
  123. &ssh_arcfour128_ssh2,
  124. };
  125. const ssh2_ciphers ssh2_arcfour = { lenof(arcfour_list), arcfour_list };