We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Allow bytearray in padding and unpadding
It looks like in the padding code, it requires the data passed to padding and unpadding to be bytes, and does not allow bytearray's. I saw in #4409 that bytes like objects were allowed as key material and such, does it make sense to allow them to be given to padders and unpadders? If so, I think it's just two instances of `_check_bytes` that would need to changed to `_check_byteslike` in https://github.com/pyca/cryptography/blob/master/src/cryptography/hazmat/primitives/padding.py, line 43 and 67.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/cryptography/hazmat/primitives/padding.py`
Content:
```
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4 
5 from __future__ import absolute_import, division, print_function
6 
7 import abc
8 
9 import six
10 
11 from cryptography import utils
12 from cryptography.exceptions import AlreadyFinalized
13 from cryptography.hazmat.bindings._padding import lib
14 
15 
16 @six.add_metaclass(abc.ABCMeta)
17 class PaddingContext(object):
18     @abc.abstractmethod
19     def update(self, data):
20         """
21         Pads the provided bytes and returns any available data as bytes.
22         """
23 
24     @abc.abstractmethod
25     def finalize(self):
26         """
27         Finalize the padding, returns bytes.
28         """
29 
30 
31 def _byte_padding_check(block_size):
32     if not (0 <= block_size <= 2040):
33         raise ValueError("block_size must be in range(0, 2041).")
34 
35     if block_size % 8 != 0:
36         raise ValueError("block_size must be a multiple of 8.")
37 
38 
39 def _byte_padding_update(buffer_, data, block_size):
40     if buffer_ is None:
41         raise AlreadyFinalized("Context was already finalized.")
42 
43     utils._check_bytes("data", data)
44 
45     buffer_ += data
46 
47     finished_blocks = len(buffer_) // (block_size // 8)
48 
49     result = buffer_[: finished_blocks * (block_size // 8)]
50     buffer_ = buffer_[finished_blocks * (block_size // 8) :]
51 
52     return buffer_, result
53 
54 
55 def _byte_padding_pad(buffer_, block_size, paddingfn):
56     if buffer_ is None:
57         raise AlreadyFinalized("Context was already finalized.")
58 
59     pad_size = block_size // 8 - len(buffer_)
60     return buffer_ + paddingfn(pad_size)
61 
62 
63 def _byte_unpadding_update(buffer_, data, block_size):
64     if buffer_ is None:
65         raise AlreadyFinalized("Context was already finalized.")
66 
67     utils._check_bytes("data", data)
68 
69     buffer_ += data
70 
71     finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)
72 
73     result = buffer_[: finished_blocks * (block_size // 8)]
74     buffer_ = buffer_[finished_blocks * (block_size // 8) :]
75 
76     return buffer_, result
77 
78 
79 def _byte_unpadding_check(buffer_, block_size, checkfn):
80     if buffer_ is None:
81         raise AlreadyFinalized("Context was already finalized.")
82 
83     if len(buffer_) != block_size // 8:
84         raise ValueError("Invalid padding bytes.")
85 
86     valid = checkfn(buffer_, block_size // 8)
87 
88     if not valid:
89         raise ValueError("Invalid padding bytes.")
90 
91     pad_size = six.indexbytes(buffer_, -1)
92     return buffer_[:-pad_size]
93 
94 
95 class PKCS7(object):
96     def __init__(self, block_size):
97         _byte_padding_check(block_size)
98         self.block_size = block_size
99 
100     def padder(self):
101         return _PKCS7PaddingContext(self.block_size)
102 
103     def unpadder(self):
104         return _PKCS7UnpaddingContext(self.block_size)
105 
106 
107 @utils.register_interface(PaddingContext)
108 class _PKCS7PaddingContext(object):
109     def __init__(self, block_size):
110         self.block_size = block_size
111         # TODO: more copies than necessary, we should use zero-buffer (#193)
112         self._buffer = b""
113 
114     def update(self, data):
115         self._buffer, result = _byte_padding_update(
116             self._buffer, data, self.block_size
117         )
118         return result
119 
120     def _padding(self, size):
121         return six.int2byte(size) * size
122 
123     def finalize(self):
124         result = _byte_padding_pad(
125             self._buffer, self.block_size, self._padding
126         )
127         self._buffer = None
128         return result
129 
130 
131 @utils.register_interface(PaddingContext)
132 class _PKCS7UnpaddingContext(object):
133     def __init__(self, block_size):
134         self.block_size = block_size
135         # TODO: more copies than necessary, we should use zero-buffer (#193)
136         self._buffer = b""
137 
138     def update(self, data):
139         self._buffer, result = _byte_unpadding_update(
140             self._buffer, data, self.block_size
141         )
142         return result
143 
144     def finalize(self):
145         result = _byte_unpadding_check(
146             self._buffer, self.block_size, lib.Cryptography_check_pkcs7_padding
147         )
148         self._buffer = None
149         return result
150 
151 
152 class ANSIX923(object):
153     def __init__(self, block_size):
154         _byte_padding_check(block_size)
155         self.block_size = block_size
156 
157     def padder(self):
158         return _ANSIX923PaddingContext(self.block_size)
159 
160     def unpadder(self):
161         return _ANSIX923UnpaddingContext(self.block_size)
162 
163 
164 @utils.register_interface(PaddingContext)
165 class _ANSIX923PaddingContext(object):
166     def __init__(self, block_size):
167         self.block_size = block_size
168         # TODO: more copies than necessary, we should use zero-buffer (#193)
169         self._buffer = b""
170 
171     def update(self, data):
172         self._buffer, result = _byte_padding_update(
173             self._buffer, data, self.block_size
174         )
175         return result
176 
177     def _padding(self, size):
178         return six.int2byte(0) * (size - 1) + six.int2byte(size)
179 
180     def finalize(self):
181         result = _byte_padding_pad(
182             self._buffer, self.block_size, self._padding
183         )
184         self._buffer = None
185         return result
186 
187 
188 @utils.register_interface(PaddingContext)
189 class _ANSIX923UnpaddingContext(object):
190     def __init__(self, block_size):
191         self.block_size = block_size
192         # TODO: more copies than necessary, we should use zero-buffer (#193)
193         self._buffer = b""
194 
195     def update(self, data):
196         self._buffer, result = _byte_unpadding_update(
197             self._buffer, data, self.block_size
198         )
199         return result
200 
201     def finalize(self):
202         result = _byte_unpadding_check(
203             self._buffer,
204             self.block_size,
205             lib.Cryptography_check_ansix923_padding,
206         )
207         self._buffer = None
208         return result
209
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/cryptography/hazmat/primitives/padding.py b/src/cryptography/hazmat/primitives/padding.py
--- a/src/cryptography/hazmat/primitives/padding.py
+++ b/src/cryptography/hazmat/primitives/padding.py
@@ -40,9 +40,9 @@
     if buffer_ is None:
         raise AlreadyFinalized("Context was already finalized.")
 
-    utils._check_bytes("data", data)
+    utils._check_byteslike("data", data)
 
-    buffer_ += data
+    buffer_ += bytes(data)
 
     finished_blocks = len(buffer_) // (block_size // 8)
 
@@ -64,9 +64,9 @@
     if buffer_ is None:
         raise AlreadyFinalized("Context was already finalized.")
 
-    utils._check_bytes("data", data)
+    utils._check_byteslike("data", data)
 
-    buffer_ += data
+    buffer_ += bytes(data)
 
     finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)

{"golden_diff": "diff --git a/src/cryptography/hazmat/primitives/padding.py b/src/cryptography/hazmat/primitives/padding.py\n--- a/src/cryptography/hazmat/primitives/padding.py\n+++ b/src/cryptography/hazmat/primitives/padding.py\n@@ -40,9 +40,9 @@\n     if buffer_ is None:\n         raise AlreadyFinalized(\"Context was already finalized.\")\n \n-    utils._check_bytes(\"data\", data)\n+    utils._check_byteslike(\"data\", data)\n \n-    buffer_ += data\n+    buffer_ += bytes(data)\n \n     finished_blocks = len(buffer_) // (block_size // 8)\n \n@@ -64,9 +64,9 @@\n     if buffer_ is None:\n         raise AlreadyFinalized(\"Context was already finalized.\")\n \n-    utils._check_bytes(\"data\", data)\n+    utils._check_byteslike(\"data\", data)\n \n-    buffer_ += data\n+    buffer_ += bytes(data)\n \n     finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)\n", "issue": "Allow bytearray in padding and unpadding\nIt looks like in the padding code, it requires the data passed to padding and unpadding to be bytes, and does not allow bytearray's. I saw in #4409 that bytes like objects were allowed as key material and such, does it make sense to allow them to be given to padders and unpadders? If so, I think it's just two instances of `_check_bytes` that would need to changed to `_check_byteslike` in https://github.com/pyca/cryptography/blob/master/src/cryptography/hazmat/primitives/padding.py, line 43 and 67.\n", "before_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\n\nimport six\n\nfrom cryptography import utils\nfrom cryptography.exceptions import AlreadyFinalized\nfrom cryptography.hazmat.bindings._padding import lib\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass PaddingContext(object):\n    @abc.abstractmethod\n    def update(self, data):\n        \"\"\"\n        Pads the provided bytes and returns any available data as bytes.\n        \"\"\"\n\n    @abc.abstractmethod\n    def finalize(self):\n        \"\"\"\n        Finalize the padding, returns bytes.\n        \"\"\"\n\n\ndef _byte_padding_check(block_size):\n    if not (0 <= block_size <= 2040):\n        raise ValueError(\"block_size must be in range(0, 2041).\")\n\n    if block_size % 8 != 0:\n        raise ValueError(\"block_size must be a multiple of 8.\")\n\n\ndef _byte_padding_update(buffer_, data, block_size):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    utils._check_bytes(\"data\", data)\n\n    buffer_ += data\n\n    finished_blocks = len(buffer_) // (block_size // 8)\n\n    result = buffer_[: finished_blocks * (block_size // 8)]\n    buffer_ = buffer_[finished_blocks * (block_size // 8) :]\n\n    return buffer_, result\n\n\ndef _byte_padding_pad(buffer_, block_size, paddingfn):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    pad_size = block_size // 8 - len(buffer_)\n    return buffer_ + paddingfn(pad_size)\n\n\ndef _byte_unpadding_update(buffer_, data, block_size):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    utils._check_bytes(\"data\", data)\n\n    buffer_ += data\n\n    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)\n\n    result = buffer_[: finished_blocks * (block_size // 8)]\n    buffer_ = buffer_[finished_blocks * (block_size // 8) :]\n\n    return buffer_, result\n\n\ndef _byte_unpadding_check(buffer_, block_size, checkfn):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    if len(buffer_) != block_size // 8:\n        raise ValueError(\"Invalid padding bytes.\")\n\n    valid = checkfn(buffer_, block_size // 8)\n\n    if not valid:\n        raise ValueError(\"Invalid padding bytes.\")\n\n    pad_size = six.indexbytes(buffer_, -1)\n    return buffer_[:-pad_size]\n\n\nclass PKCS7(object):\n    def __init__(self, block_size):\n        _byte_padding_check(block_size)\n        self.block_size = block_size\n\n    def padder(self):\n        return _PKCS7PaddingContext(self.block_size)\n\n    def unpadder(self):\n        return _PKCS7UnpaddingContext(self.block_size)\n\n\n@utils.register_interface(PaddingContext)\nclass _PKCS7PaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_padding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def _padding(self, size):\n        return six.int2byte(size) * size\n\n    def finalize(self):\n        result = _byte_padding_pad(\n            self._buffer, self.block_size, self._padding\n        )\n        self._buffer = None\n        return result\n\n\n@utils.register_interface(PaddingContext)\nclass _PKCS7UnpaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_unpadding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def finalize(self):\n        result = _byte_unpadding_check(\n            self._buffer, self.block_size, lib.Cryptography_check_pkcs7_padding\n        )\n        self._buffer = None\n        return result\n\n\nclass ANSIX923(object):\n    def __init__(self, block_size):\n        _byte_padding_check(block_size)\n        self.block_size = block_size\n\n    def padder(self):\n        return _ANSIX923PaddingContext(self.block_size)\n\n    def unpadder(self):\n        return _ANSIX923UnpaddingContext(self.block_size)\n\n\n@utils.register_interface(PaddingContext)\nclass _ANSIX923PaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_padding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def _padding(self, size):\n        return six.int2byte(0) * (size - 1) + six.int2byte(size)\n\n    def finalize(self):\n        result = _byte_padding_pad(\n            self._buffer, self.block_size, self._padding\n        )\n        self._buffer = None\n        return result\n\n\n@utils.register_interface(PaddingContext)\nclass _ANSIX923UnpaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_unpadding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def finalize(self):\n        result = _byte_unpadding_check(\n            self._buffer,\n            self.block_size,\n            lib.Cryptography_check_ansix923_padding,\n        )\n        self._buffer = None\n        return result\n", "path": "src/cryptography/hazmat/primitives/padding.py"}], "after_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\n\nimport six\n\nfrom cryptography import utils\nfrom cryptography.exceptions import AlreadyFinalized\nfrom cryptography.hazmat.bindings._padding import lib\n\n\n@six.add_metaclass(abc.ABCMeta)\nclass PaddingContext(object):\n    @abc.abstractmethod\n    def update(self, data):\n        \"\"\"\n        Pads the provided bytes and returns any available data as bytes.\n        \"\"\"\n\n    @abc.abstractmethod\n    def finalize(self):\n        \"\"\"\n        Finalize the padding, returns bytes.\n        \"\"\"\n\n\ndef _byte_padding_check(block_size):\n    if not (0 <= block_size <= 2040):\n        raise ValueError(\"block_size must be in range(0, 2041).\")\n\n    if block_size % 8 != 0:\n        raise ValueError(\"block_size must be a multiple of 8.\")\n\n\ndef _byte_padding_update(buffer_, data, block_size):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    utils._check_byteslike(\"data\", data)\n\n    buffer_ += bytes(data)\n\n    finished_blocks = len(buffer_) // (block_size // 8)\n\n    result = buffer_[: finished_blocks * (block_size // 8)]\n    buffer_ = buffer_[finished_blocks * (block_size // 8) :]\n\n    return buffer_, result\n\n\ndef _byte_padding_pad(buffer_, block_size, paddingfn):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    pad_size = block_size // 8 - len(buffer_)\n    return buffer_ + paddingfn(pad_size)\n\n\ndef _byte_unpadding_update(buffer_, data, block_size):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    utils._check_byteslike(\"data\", data)\n\n    buffer_ += bytes(data)\n\n    finished_blocks = max(len(buffer_) // (block_size // 8) - 1, 0)\n\n    result = buffer_[: finished_blocks * (block_size // 8)]\n    buffer_ = buffer_[finished_blocks * (block_size // 8) :]\n\n    return buffer_, result\n\n\ndef _byte_unpadding_check(buffer_, block_size, checkfn):\n    if buffer_ is None:\n        raise AlreadyFinalized(\"Context was already finalized.\")\n\n    if len(buffer_) != block_size // 8:\n        raise ValueError(\"Invalid padding bytes.\")\n\n    valid = checkfn(buffer_, block_size // 8)\n\n    if not valid:\n        raise ValueError(\"Invalid padding bytes.\")\n\n    pad_size = six.indexbytes(buffer_, -1)\n    return buffer_[:-pad_size]\n\n\nclass PKCS7(object):\n    def __init__(self, block_size):\n        _byte_padding_check(block_size)\n        self.block_size = block_size\n\n    def padder(self):\n        return _PKCS7PaddingContext(self.block_size)\n\n    def unpadder(self):\n        return _PKCS7UnpaddingContext(self.block_size)\n\n\n@utils.register_interface(PaddingContext)\nclass _PKCS7PaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_padding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def _padding(self, size):\n        return six.int2byte(size) * size\n\n    def finalize(self):\n        result = _byte_padding_pad(\n            self._buffer, self.block_size, self._padding\n        )\n        self._buffer = None\n        return result\n\n\n@utils.register_interface(PaddingContext)\nclass _PKCS7UnpaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_unpadding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def finalize(self):\n        result = _byte_unpadding_check(\n            self._buffer, self.block_size, lib.Cryptography_check_pkcs7_padding\n        )\n        self._buffer = None\n        return result\n\n\nclass ANSIX923(object):\n    def __init__(self, block_size):\n        _byte_padding_check(block_size)\n        self.block_size = block_size\n\n    def padder(self):\n        return _ANSIX923PaddingContext(self.block_size)\n\n    def unpadder(self):\n        return _ANSIX923UnpaddingContext(self.block_size)\n\n\n@utils.register_interface(PaddingContext)\nclass _ANSIX923PaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_padding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def _padding(self, size):\n        return six.int2byte(0) * (size - 1) + six.int2byte(size)\n\n    def finalize(self):\n        result = _byte_padding_pad(\n            self._buffer, self.block_size, self._padding\n        )\n        self._buffer = None\n        return result\n\n\n@utils.register_interface(PaddingContext)\nclass _ANSIX923UnpaddingContext(object):\n    def __init__(self, block_size):\n        self.block_size = block_size\n        # TODO: more copies than necessary, we should use zero-buffer (#193)\n        self._buffer = b\"\"\n\n    def update(self, data):\n        self._buffer, result = _byte_unpadding_update(\n            self._buffer, data, self.block_size\n        )\n        return result\n\n    def finalize(self):\n        result = _byte_unpadding_check(\n            self._buffer,\n            self.block_size,\n            lib.Cryptography_check_ansix923_padding,\n        )\n        self._buffer = None\n        return result\n", "path": "src/cryptography/hazmat/primitives/padding.py"}]}