arc4.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* arc4.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef NO_RC4
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/arc4.h>
  28. int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
  29. {
  30. int ret = 0;
  31. word32 i;
  32. word32 keyIndex = 0, stateIndex = 0;
  33. if (arc4 == NULL || key == NULL || length == 0) {
  34. return BAD_FUNC_ARG;
  35. }
  36. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
  37. defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
  38. if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
  39. return NitroxArc4SetKey(arc4, key, length);
  40. }
  41. #endif
  42. arc4->x = 1;
  43. arc4->y = 0;
  44. for (i = 0; i < ARC4_STATE_SIZE; i++)
  45. arc4->state[i] = (byte)i;
  46. for (i = 0; i < ARC4_STATE_SIZE; i++) {
  47. word32 a = arc4->state[i];
  48. stateIndex += key[keyIndex] + a;
  49. stateIndex &= 0xFF;
  50. arc4->state[i] = arc4->state[stateIndex];
  51. arc4->state[stateIndex] = (byte)a;
  52. if (++keyIndex >= length)
  53. keyIndex = 0;
  54. }
  55. return ret;
  56. }
  57. static WC_INLINE byte MakeByte(word32* x, word32* y, byte* s)
  58. {
  59. word32 a = s[*x], b;
  60. *y = (*y+a) & 0xff;
  61. b = s[*y];
  62. s[*x] = (byte)b;
  63. s[*y] = (byte)a;
  64. *x = (*x+1) & 0xff;
  65. return s[(a+b) & 0xff];
  66. }
  67. int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
  68. {
  69. int ret = 0;
  70. word32 x;
  71. word32 y;
  72. if (arc4 == NULL || out == NULL || in == NULL) {
  73. return BAD_FUNC_ARG;
  74. }
  75. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
  76. defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
  77. if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
  78. return NitroxArc4Process(arc4, out, in, length);
  79. }
  80. #endif
  81. x = arc4->x;
  82. y = arc4->y;
  83. while(length--)
  84. *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
  85. arc4->x = (byte)x;
  86. arc4->y = (byte)y;
  87. return ret;
  88. }
  89. /* Initialize Arc4 for use with async device */
  90. int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
  91. {
  92. int ret = 0;
  93. if (arc4 == NULL)
  94. return BAD_FUNC_ARG;
  95. arc4->heap = heap;
  96. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
  97. ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
  98. arc4->heap, devId);
  99. #else
  100. (void)devId;
  101. #endif /* WOLFSSL_ASYNC_CRYPT */
  102. return ret;
  103. }
  104. /* Free Arc4 from use with async device */
  105. void wc_Arc4Free(Arc4* arc4)
  106. {
  107. if (arc4 == NULL)
  108. return;
  109. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
  110. wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
  111. #endif /* WOLFSSL_ASYNC_CRYPT */
  112. }
  113. #endif /* NO_RC4 */