You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arc4.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * The ARC4 stream cipher.
  3. *
  4. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. #include <ipxe/crypto.h>
  22. #include <ipxe/arc4.h>
  23. #define SWAP( ary, i, j ) \
  24. ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
  25. /**
  26. * Set ARC4 key
  27. *
  28. * @v ctxv ARC4 encryption context
  29. * @v keyv Key to set
  30. * @v keylen Length of key
  31. *
  32. * If an initialisation vector is to be used, it should be prepended
  33. * to the key; ARC4 does not implement the @c setiv function because
  34. * there is no standard length for an initialisation vector in the
  35. * cipher.
  36. */
  37. static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
  38. {
  39. struct arc4_ctx *ctx = ctxv;
  40. const u8 *key = keyv;
  41. u8 *S = ctx->state;
  42. int i, j;
  43. for ( i = 0; i < 256; i++ ) {
  44. S[i] = i;
  45. }
  46. for ( i = j = 0; i < 256; i++ ) {
  47. j = ( j + S[i] + key[i % keylen] ) & 0xff;
  48. SWAP ( S, i, j );
  49. }
  50. ctx->i = ctx->j = 0;
  51. return 0;
  52. }
  53. /**
  54. * Perform ARC4 encryption or decryption
  55. *
  56. * @v ctxv ARC4 encryption context
  57. * @v srcv Data to encrypt or decrypt
  58. * @v dstv Location to store encrypted or decrypted data
  59. * @v len Length of data to operate on
  60. *
  61. * ARC4 is a stream cipher that works by generating a stream of PRNG
  62. * data based on the key, and XOR'ing it with the data to be
  63. * encrypted. Since XOR is symmetric, encryption and decryption in
  64. * ARC4 are the same operation.
  65. *
  66. * If you pass a @c NULL source or destination pointer, @a len
  67. * keystream bytes will be consumed without encrypting any data.
  68. */
  69. static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
  70. size_t len )
  71. {
  72. struct arc4_ctx *ctx = ctxv;
  73. const u8 *src = srcv;
  74. u8 *dst = dstv;
  75. u8 *S = ctx->state;
  76. int i = ctx->i, j = ctx->j;
  77. while ( len-- ) {
  78. i = ( i + 1 ) & 0xff;
  79. j = ( j + S[i] ) & 0xff;
  80. SWAP ( S, i, j );
  81. if ( srcv && dstv )
  82. *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
  83. }
  84. ctx->i = i;
  85. ctx->j = j;
  86. }
  87. static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
  88. {
  89. /* ARC4 does not use a fixed-length IV */
  90. }
  91. /**
  92. * Perform ARC4 encryption or decryption, skipping initial keystream bytes
  93. *
  94. * @v key ARC4 encryption key
  95. * @v keylen Key length
  96. * @v skip Number of bytes of keystream to skip
  97. * @v src Message to encrypt or decrypt
  98. * @v msglen Length of message
  99. * @ret dst Encrypted or decrypted message
  100. */
  101. void arc4_skip ( const void *key, size_t keylen, size_t skip,
  102. const void *src, void *dst, size_t msglen )
  103. {
  104. struct arc4_ctx ctx;
  105. arc4_setkey ( &ctx, key, keylen );
  106. arc4_xor ( &ctx, NULL, NULL, skip );
  107. arc4_xor ( &ctx, src, dst, msglen );
  108. }
  109. struct cipher_algorithm arc4_algorithm = {
  110. .name = "ARC4",
  111. .ctxsize = ARC4_CTX_SIZE,
  112. .blocksize = 1,
  113. .setkey = arc4_setkey,
  114. .setiv = arc4_setiv,
  115. .encrypt = arc4_xor,
  116. .decrypt = arc4_xor,
  117. };