strxor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, c_size_t,
  31. create_string_buffer, get_raw_buffer,
  32. expect_byte_string)
  33. _raw_strxor = load_pycryptodome_raw_lib("Crypto.Util._strxor",
  34. """
  35. void strxor(const uint8_t *in1,
  36. const uint8_t *in2,
  37. uint8_t *out, size_t len);
  38. void strxor_c(const uint8_t *in,
  39. uint8_t c,
  40. uint8_t *out,
  41. size_t len);
  42. """)
  43. def strxor(term1, term2):
  44. """XOR of two byte strings.
  45. They must have equal length.
  46. Return:
  47. A new byte string, :data:`term1` xored with :data:`term2`.
  48. """
  49. expect_byte_string(term1)
  50. expect_byte_string(term2)
  51. if len(term1) != len(term2):
  52. raise ValueError("Only byte strings of equal length can be xored")
  53. result = create_string_buffer(len(term1))
  54. _raw_strxor.strxor(term1, term2, result, c_size_t(len(term1)))
  55. return get_raw_buffer(result)
  56. def strxor_c(term, c):
  57. """XOR of a byte string with a repeated sequence of characters.
  58. Return:
  59. A new byte string, :data:`term` with all its bytes xored with :data:`c`.
  60. """
  61. expect_byte_string(term)
  62. if not 0 <= c < 256:
  63. raise ValueError("c must be in range(256)")
  64. result = create_string_buffer(len(term))
  65. _raw_strxor.strxor_c(term, c, result, c_size_t(len(term)))
  66. return get_raw_buffer(result)
  67. def _strxor_direct(term1, term2, result):
  68. """Very fast XOR - check conditions!"""
  69. _raw_strxor.strxor(term1, term2, result, c_size_t(len(term1)))