Debian Codegraph @

lib/Crypto/Util/strxor.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2014, Legrandin <[email protected]>
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
31 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib, c_size_t,
32 create_string_buffer, get_raw_buffer,
33 c_uint8_ptr, is_writeable_buffer)
34
35 _raw_strxor = load_pycryptodome_raw_lib(
36 "Crypto.Util._strxor",
37 """
38 void strxor(const uint8_t *in1,
39 const uint8_t *in2,
40 uint8_t *out, size_t len);
41 void strxor_c(const uint8_t *in,
42 uint8_t c,
43 uint8_t *out,
44 size_t len);
45 """)
46
47
48 def strxor(term1, term2, output=None):
49 """From two byte strings of equal length,
50 create a third one which is the byte-by-byte XOR of the two.
51
52 Args:
53 term1 (bytes/bytearray/memoryview):
54 The first byte string to XOR.
55 term2 (bytes/bytearray/memoryview):
56 The second byte string to XOR.
57 output (bytearray/memoryview):
58 The location where the result will be written to.
59 It must have the same length as ``term1`` and ``term2``.
60 If ``None``, the result is returned.
61 :Return:
62 If ``output`` is ``None``, a new byte string with the result.
63 Otherwise ``None``.
64
65 .. note::
66 ``term1`` and ``term2`` must have the same length.
67 """
68
69 if len(term1) != len(term2):
70 raise ValueError("Only byte strings of equal length can be xored")
71
72 if output is None:
73 result = create_string_buffer(len(term1))
74 else:
75 # Note: output may overlap with either input
76 result = output
77
78 if not is_writeable_buffer(output):
79 raise TypeError("output must be a bytearray or a writeable memoryview")
80
81 if len(term1) != len(output):
82 raise ValueError("output must have the same length as the input"
83 " (%d bytes)" % len(term1))
84
85 _raw_strxor.strxor(c_uint8_ptr(term1),
86 c_uint8_ptr(term2),
87 c_uint8_ptr(result),
88 c_size_t(len(term1)))
89
90 if output is None:
91 return get_raw_buffer(result)
92 else:
93 return None
94
95
96 def strxor_c(term, c, output=None):
97 """From a byte string, create a second one of equal length
98 where each byte is XOR-red with the same value.
99
100 Args:
101 term(bytes/bytearray/memoryview):
102 The byte string to XOR.
103 c (int):
104 Every byte in the string will be XOR-ed with this value.
105 It must be between 0 and 255 (included).
106 output (None or bytearray/memoryview):
107 The location where the result will be written to.
108 It must have the same length as ``term``.
109 If ``None``, the result is returned.
110
111 Return:
112 If ``output`` is ``None``, a new ``bytes`` string with the result.
113 Otherwise ``None``.
114 """
115
116 if not 0 <= c < 256:
117 raise ValueError("c must be in range(256)")
118
119 if output is None:
120 result = create_string_buffer(len(term))
121 else:
122 # Note: output may overlap with either input
123 result = output
124
125 if not is_writeable_buffer(output):
126 raise TypeError("output must be a bytearray or a writeable memoryview")
127
128 if len(term) != len(output):
129 raise ValueError("output must have the same length as the input"
130 " (%d bytes)" % len(term))
131
132 _raw_strxor.strxor_c(c_uint8_ptr(term),
133 c,
134 c_uint8_ptr(result),
135 c_size_t(len(term))
136 )
137
138 if output is None:
139 return get_raw_buffer(result)
140 else:
141 return None
142
143
144 def _strxor_direct(term1, term2, result):
145 """Very fast XOR - check conditions!"""
146 _raw_strxor.strxor(term1, term2, result, c_size_t(len(term1)))