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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. FILE_LICENCE ( GPL2_OR_LATER );
  22. #include <ipxe/crypto.h>
  23. #include <ipxe/arc4.h>
  24. #define SWAP( ary, i, j ) \
  25. ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
  26. /**
  27. * Set ARC4 key
  28. *
  29. * @v ctxv ARC4 encryption context
  30. * @v keyv Key to set
  31. * @v keylen Length of key
  32. *
  33. * If an initialisation vector is to be used, it should be prepended
  34. * to the key; ARC4 does not implement the @c setiv function because
  35. * there is no standard length for an initialisation vector in the
  36. * cipher.
  37. */
  38. static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
  39. {
  40. struct arc4_ctx *ctx = ctxv;
  41. const u8 *key = keyv;
  42. u8 *S = ctx->state;
  43. int i, j;
  44. for ( i = 0; i < 256; i++ ) {
  45. S[i] = i;
  46. }
  47. for ( i = j = 0; i < 256; i++ ) {
  48. j = ( j + S[i] + key[i % keylen] ) & 0xff;
  49. SWAP ( S, i, j );
  50. }
  51. ctx->i = ctx->j = 0;
  52. return 0;
  53. }
  54. /**
  55. * Perform ARC4 encryption or decryption
  56. *
  57. * @v ctxv ARC4 encryption context
  58. * @v srcv Data to encrypt or decrypt
  59. * @v dstv Location to store encrypted or decrypted data
  60. * @v len Length of data to operate on
  61. *
  62. * ARC4 is a stream cipher that works by generating a stream of PRNG
  63. * data based on the key, and XOR'ing it with the data to be
  64. * encrypted. Since XOR is symmetric, encryption and decryption in
  65. * ARC4 are the same operation.
  66. *
  67. * If you pass a @c NULL source or destination pointer, @a len
  68. * keystream bytes will be consumed without encrypting any data.
  69. */
  70. static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
  71. size_t len )
  72. {
  73. struct arc4_ctx *ctx = ctxv;
  74. const u8 *src = srcv;
  75. u8 *dst = dstv;
  76. u8 *S = ctx->state;
  77. int i = ctx->i, j = ctx->j;
  78. while ( len-- ) {
  79. i = ( i + 1 ) & 0xff;
  80. j = ( j + S[i] ) & 0xff;
  81. SWAP ( S, i, j );
  82. if ( srcv && dstv )
  83. *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
  84. }
  85. ctx->i = i;
  86. ctx->j = j;
  87. }
  88. static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
  89. {
  90. /* ARC4 does not use a fixed-length IV */
  91. }
  92. /**
  93. * Perform ARC4 encryption or decryption, skipping initial keystream bytes
  94. *
  95. * @v key ARC4 encryption key
  96. * @v keylen Key length
  97. * @v skip Number of bytes of keystream to skip
  98. * @v src Message to encrypt or decrypt
  99. * @v msglen Length of message
  100. * @ret dst Encrypted or decrypted message
  101. */
  102. void arc4_skip ( const void *key, size_t keylen, size_t skip,
  103. const void *src, void *dst, size_t msglen )
  104. {
  105. struct arc4_ctx ctx;
  106. arc4_setkey ( &ctx, key, keylen );
  107. arc4_xor ( &ctx, NULL, NULL, skip );
  108. arc4_xor ( &ctx, src, dst, msglen );
  109. }
  110. struct cipher_algorithm arc4_algorithm = {
  111. .name = "ARC4",
  112. .ctxsize = ARC4_CTX_SIZE,
  113. .blocksize = 1,
  114. .setkey = arc4_setkey,
  115. .setiv = arc4_setiv,
  116. .encrypt = arc4_xor,
  117. .decrypt = arc4_xor,
  118. };