arcfour.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* arcfour.c - The arcfour stream cipher
  2. * Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser general Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  19. *
  20. * For a description of the algorithm, see:
  21. * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
  22. * ISBN 0-471-11709-9. Pages 397 ff.
  23. */
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "types.h"
  29. #include "g10lib.h"
  30. #include "cipher.h"
  31. static const char *selftest(void);
  32. typedef struct {
  33. int idx_i, idx_j;
  34. byte sbox[256];
  35. } ARCFOUR_context;
  36. static void
  37. do_encrypt_stream( ARCFOUR_context *ctx,
  38. byte *outbuf, const byte *inbuf, unsigned int length )
  39. {
  40. register int i = ctx->idx_i;
  41. register int j = ctx->idx_j;
  42. register byte *sbox = ctx->sbox;
  43. register int t;
  44. while ( length-- )
  45. {
  46. i++;
  47. i = i & 255; /* The and-op seems to be faster than the mod-op. */
  48. j += sbox[i];
  49. j &= 255;
  50. t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
  51. *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255];
  52. }
  53. ctx->idx_i = i;
  54. ctx->idx_j = j;
  55. }
  56. static void
  57. encrypt_stream (void *context,
  58. byte *outbuf, const byte *inbuf, unsigned int length)
  59. {
  60. ARCFOUR_context *ctx = (ARCFOUR_context *) context;
  61. do_encrypt_stream (ctx, outbuf, inbuf, length );
  62. _gcry_burn_stack (64);
  63. }
  64. static gcry_err_code_t
  65. do_arcfour_setkey (void *context, const byte *key, unsigned int keylen)
  66. {
  67. static int initialized;
  68. static const char* selftest_failed;
  69. int i, j;
  70. byte karr[256];
  71. ARCFOUR_context *ctx = (ARCFOUR_context *) context;
  72. if (!initialized )
  73. {
  74. initialized = 1;
  75. selftest_failed = selftest();
  76. if( selftest_failed )
  77. log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
  78. }
  79. if( selftest_failed )
  80. return GPG_ERR_SELFTEST_FAILED;
  81. if( keylen < 40/8 ) /* we want at least 40 bits */
  82. return GPG_ERR_INV_KEYLEN;
  83. ctx->idx_i = ctx->idx_j = 0;
  84. for (i=0; i < 256; i++ )
  85. ctx->sbox[i] = i;
  86. for (i=0; i < 256; i++ )
  87. karr[i] = key[i%keylen];
  88. for (i=j=0; i < 256; i++ )
  89. {
  90. int t;
  91. j = (j + ctx->sbox[i] + karr[i]) % 256;
  92. t = ctx->sbox[i];
  93. ctx->sbox[i] = ctx->sbox[j];
  94. ctx->sbox[j] = t;
  95. }
  96. memset( karr, 0, 256 );
  97. return GPG_ERR_NO_ERROR;
  98. }
  99. static gcry_err_code_t
  100. arcfour_setkey ( void *context, const byte *key, unsigned int keylen )
  101. {
  102. ARCFOUR_context *ctx = (ARCFOUR_context *) context;
  103. gcry_err_code_t rc = do_arcfour_setkey (ctx, key, keylen );
  104. _gcry_burn_stack (300);
  105. return rc;
  106. }
  107. static const char*
  108. selftest(void)
  109. {
  110. ARCFOUR_context ctx;
  111. byte scratch[16];
  112. /* Test vector from Cryptlib labeled there: "from the
  113. State/Commerce Department". */
  114. static byte key_1[] =
  115. { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
  116. static byte plaintext_1[] =
  117. { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
  118. static const byte ciphertext_1[] =
  119. { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
  120. arcfour_setkey( &ctx, key_1, sizeof(key_1));
  121. encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
  122. if ( memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
  123. return "Arcfour encryption test 1 failed.";
  124. arcfour_setkey( &ctx, key_1, sizeof(key_1));
  125. encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
  126. if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
  127. return "Arcfour decryption test 1 failed.";
  128. return NULL;
  129. }
  130. gcry_cipher_spec_t _gcry_cipher_spec_arcfour =
  131. {
  132. "ARCFOUR", NULL, NULL, 1, 128, sizeof (ARCFOUR_context),
  133. arcfour_setkey, NULL, NULL, encrypt_stream, encrypt_stream,
  134. };