1
0

arcfour.c 4.0 KB

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