8000 bpo-31933: fix blake2 multi-byte params on big endian platforms (#4250) · python/cpython@dcfb0e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit dcfb0e3

Browse files
oconnor663tiran
authored andcommitted
bpo-31933: fix blake2 multi-byte params on big endian platforms (#4250)
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.
1 parent f6f90ff commit dcfb0e3

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

Lib/test/test_hashlib.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,24 @@ def test_case_blake2b_1(self):
615615
"ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+
616616
"7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")
617617

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

661+
@requires_blake2
662+
def test_case_blake2s_all_parameters(self):
663+
# This checks that all the parameters work in general, and also that
664+
# parameter byte order doesn't get confused on big endian platforms.
665+
self.check('blake2s', b"foo",
666+
"bf2a8f7fe3c555012a6f8046e646bc75",
667+
digest_size=16,
668+
key=b"bar",
669+
salt=b"baz",
670+
person=b"bing",
671+
fanout=2,
672+
depth=3,
673+
leaf_size=4,
674+
node_offset=5,
675+
node_depth=6,
676+
inner_size=7,
677+
last_node=True)
678+
643679
@requires_blake2
644680
def test_blake2s_vectors(self):
645681
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
@@ -162,7 +162,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
162162
goto error;
163163
}
164164
}
165-
self->param.leaf_length = (unsigned int)leaf_size;
165+
// NB: Simple assignment here would be incorrect on big endian platforms.
166+
store32(&(self->param.leaf_length), leaf_size);
166167

167168
if (node_offset_obj != NULL) {
168169
node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -178,7 +179,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
178179
}
179180
store48(&(self->param.node_offset), node_offset);
180181
#else
181-
self->param.node_offset = node_offset;
182+
// NB: Simple assignment here would be incorrect on big endian platforms.
183+
store64(&(self->param.node_offset), node_offset);
182184
#endif
183185

184186
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
@@ -162,7 +162,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
162162
goto error;
163163
}
164164
}
165-
self->param.leaf_length = (unsigned int)leaf_size;
165+
// NB: Simple assignment here would be incorrect on big endian platforms.
166+
store32(&(self->param.leaf_length), leaf_size);
166167

167168
if (node_offset_obj != NULL) {
168169
node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -178,7 +179,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
178179
}
179180
store48(&(self->param.node_offset), node_offset);
180181
#else
181-
self->param.node_offset = node_offset;
182+
// NB: Simple assignment here would be incorrect on big endian platforms.
183+
store64(&(self->param.node_offset), node_offset);
182184
#endif
183185

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

0 commit comments

Comments
 (0)
0