8000 bpo-31933: fix blake2 multi-byte params on big endian platforms (GH-4… · python/cpython@a512493 · GitHub
[go: up one dir, main page]

Skip to content

Commit a512493

Browse files
miss-islingtontiran
authored andcommitted
bpo-31933: fix blake2 multi-byte params on big endian platforms (GH-4250) (#4262)
All Blake2 params have to be encoded in little-endian byte order. For the two multi-byte integer params, leaf_length and node_offset, that means that assigning a native-endian integer to them appears to work on little-endian platforms, but gives the wrong result on big-endian. The current libb2 API doesn't make that very clear, and @sneves is working on new API functions in the GH issue above. In the meantime, we can work around the problem by explicitly assigning little-endian values to the parameter block. See BLAKE2/libb2#12. (cherry picked from commit dcfb0e3)
1 parent ea80ae0 commit a512493

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Lib/test/test_hashlib.py

Copy file name to clipboard
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,24 @@ def test_case_blake2b_1(self):
618618
"ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+
619619
"7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")
620620

621+
@requires_blake2
622+
def test_case_blake2b_all_parameters(self):
623+
# This checks that all the parameters work in general, and also that
624+
# parameter byte order doesn't get confused on big endian platforms.
625+
self.check('blake2b', b"foo",
626+
"920568b0c5873b2f0ab67bedb6cf1b2b",
627+
digest_size=16,
628+
key=b"bar",
629+
salt=b"baz",
630+
person=b"bing",
631+
fanout=2,
632+
depth=3,
633+
leaf_size=4,
634+
node_offset=5,
635+
node_depth=6,
636+
inner_size=7,
637+
last_node=True)
638+
621639
@requires_blake2
622640
def test_blake2b_vectors(self):
623641
for msg, key, md in read_vectors('blake2b'):
@@ -643,6 +661,24 @@ def test_case_blake2s_1(self):
643661
self.check('blake2s', b"abc",
644662
"508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982")
645663

664+
@requires_blake2
665+
def test_case_blake2s_all_parameters(self):
666+
# This checks that all the parameters work in general, and also that
667+
# parameter byte order doesn't get confused on big endian platforms.
668+
self.check('blake2s', b"foo",
669+
"bf2a8f7fe3c555012a6f8046e646bc75",
670+
digest_size=16,
671+
key=b"bar",
672+
salt=b"baz",
673+
person=b"bing",
674+
fanout=2,
675+
depth=3,
676+
leaf_size=4,
677+
node_offset=5,
678+
node_depth=6,
679+
inner_size=7,
680+
last_node=True)
681+
646682
@requires_blake2
647683
def test_blake2s_vectors(self):
648684
for msg, key, md in read_vectors('blake2s'):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix Blake2 params leaf_size and node_offset on big endian platforms. Patch
2+
by Jack O'Connor.

Modules/_blake2/blake2b_impl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
166166
goto error;
167167
}
168168
}
169-
self->param.leaf_length = (unsigned int)leaf_size;
169+
// NB: Simple assignment here would be incorrect on big endian platforms.
170+
store32(&(self->param.leaf_length), leaf_size);
170171

171172
if (node_offset_obj != NULL) {
172173
node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -182,7 +183,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
182183
}
183184
store48(&(self->param.node_offset), node_offset);
184185
#else
185-
self->param.node_offset = node_offset;
186+
// NB: Simple assignment here would be incorrect on big endian platforms.
187+
store64(&(self->param.node_offset), node_offset);
186188
#endif
187189

188190
if (node_depth < 0 || node_depth > 255) {

Modules/_blake2/blake2s_impl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
166166
goto error;
167167
}
168168
}
169-
self->param.leaf_length = (unsigned int)leaf_size;
169+
// NB: Simple assignment here would be incorrect on big endian platforms.
170+
store32(&(self->param.leaf_length), leaf_size);
170171

171172
if (node_offset_obj != NULL) {
172173
node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -182,7 +183,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
182183
}
183184
store48(&(self->param.node_offset), node_offset);
184185
#else
185-
self->param.node_offset = node_offset;
186+
// NB: Simple assignment here would be incorrect on big endian platforms.
187+
store64(&(self->param.node_offset), node_offset);
186188
#endif
187189

188190
if (node_depth < 0 || node_depth > 255) {

0 commit comments

Comments
 (0)
0