Skip to content

Commit 9d6bfb2

Browse files
committed
Handle increased message buffer in YubiHSM 2.4.0
1 parent 16bff40 commit 9d6bfb2

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

tests/device/test_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from yubihsm.core import MAX_MSG_SIZE
1615
from yubihsm.defs import ALGORITHM, CAPABILITY, OBJECT, COMMAND, ORIGIN, FIPS_STATUS
1716
from yubihsm.objects import (
1817
YhsmObject,
@@ -141,8 +140,9 @@ def test_get_pseudo_random(self, session):
141140
assert len(data2) == 10
142141
assert data != data2
143142

144-
def test_send_too_big(self, hsm):
145-
buf = os.urandom(MAX_MSG_SIZE - 3 + 1) # Message 1 byte too large
143+
def test_send_too_big(self, hsm, session):
144+
max_msg_size = hsm._msg_buf_size - 1
145+
buf = os.urandom(max_msg_size - 3 + 1) # Message 1 byte too large
146146
with pytest.raises(YubiHsmInvalidRequestError):
147147
hsm.send_cmd(COMMAND.ECHO, buf)
148148

tests/device/test_opaque.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_put_too_big(session):
6666
1,
6767
CAPABILITY.NONE,
6868
ALGORITHM.OPAQUE_DATA,
69-
os.urandom(1976),
69+
os.urandom(3064),
7070
)
7171

7272
# Make sure our session is still working

tests/test_core.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def get_mocked_session(patch):
5454
"""
5555
mocked_backend = get_backend()
5656
mocked_backend.transceive.side_effect = [
57+
_TRANSCEIVE_DEVICE_INFO, # get_device_info is called during initialization
5758
b"\x83\x00\x11\x00\x05MV1\xc9\x18o\x802%\xed\x8a2$\xf2\xcf",
5859
b"\x84\x00\x00",
5960
]
@@ -266,9 +267,12 @@ def test_create_session_derived(self, item):
266267
expect_enc = b"\t\x0bG\xdb\xedYVT\x90\x1d\xee\x1c\xc6U\xe4 "
267268
expect_mac = b"Y/\xd4\x83\xf7Y\xe2\x99\t\xa0LE\x05\xd2\xce\n"
268269

269-
# Note: backend doesn't do anything here; it's just required by the
270-
# function's signature
271-
hsm = YubiHsm(backend=None)
270+
# Note: get_device_info gets called during initialization
271+
# which is why we mock the transceive function.
272+
backend = get_backend()
273+
backend.transceive.return_value = _TRANSCEIVE_DEVICE_INFO
274+
275+
hsm = YubiHsm(backend)
272276
hsm.create_session_derived(auth_key_id, password)
273277

274278
hsm.create_session.assert_called_once_with(auth_key_id, expect_enc, expect_mac)
@@ -284,7 +288,12 @@ def test_get_device_info_mock_transceive(self):
284288
hsm = YubiHsm(backend)
285289

286290
info = hsm.get_device_info()
287-
hsm._backend.transceive.assert_called_once_with(b"\x06\x00\x00")
291+
hsm._backend.transceive.assert_has_calls(
292+
[
293+
call(b"\x06\x00\x00"), # first call during YubiHSM::__init__
294+
call(b"\x06\x00\x00"),
295+
]
296+
)
288297

289298
self.assertEqual(info.version, (2, 0, 0))
290299
self.assertEqual(info.serial, 7550140)

yubihsm/core.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
CARD_CRYPTOGRAM = 0x00
5555
HOST_CRYPTOGRAM = 0x01
5656

57-
MAX_MSG_SIZE = 2048 - 1
58-
5957

6058
def _derive(key: bytes, t: int, context: bytes, L: int = 0x80) -> bytes:
6159
# this only supports aes128
@@ -272,6 +270,12 @@ def __init__(self, backend: YhsmBackend):
272270
"""
273271
self._backend: YhsmBackend = backend
274272

273+
# Initialize the message buffer size to 2048 bytes. This may be updated
274+
# depending on the YubiHSM FW version (in 2.4.0 or higher the
275+
# buffer size is 3136) in get_device_info.
276+
self._msg_buf_size = 2048
277+
self.get_device_info()
278+
275279
def __enter__(self):
276280
return self
277281

@@ -285,7 +289,7 @@ def close(self) -> None:
285289
self._backend = _ClosedBackend()
286290

287291
def _transceive(self, msg: bytes) -> bytes:
288-
if len(msg) > MAX_MSG_SIZE:
292+
if len(msg) > self._msg_buf_size - 1:
289293
raise YubiHsmInvalidRequestError("Message too long.")
290294
return self._backend.transceive(msg)
291295

@@ -307,6 +311,8 @@ def get_device_info(self) -> DeviceInfo:
307311
first_page = self.send_cmd(COMMAND.DEVICE_INFO)
308312
device_info = DeviceInfo.parse(first_page)
309313
if device_info.version >= (2, 4, 0):
314+
# Update maximum message buffer size
315+
self._msg_buf_size = 3136
310316
# fetch next page
311317
second_page = self.send_cmd(COMMAND.DEVICE_INFO, struct.pack("!B", 1))
312318
device_info = DeviceInfo.parse(first_page, second_page)

0 commit comments

Comments
 (0)