]> granicus.if.org Git - python/commitdiff
bpo-31933: fix blake2 multi-byte params on big endian platforms (GH-4250) (#4262)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 3 Nov 2017 19:55:54 +0000 (12:55 -0700)
committerChristian Heimes <christian@python.org>
Fri, 3 Nov 2017 19:55:54 +0000 (20:55 +0100)
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 https://github.com/BLAKE2/libb2/issues/12.
(cherry picked from commit dcfb0e3c04f1b29a0d09bb0a81dcd5ee5a5fef1a)

Lib/test/test_hashlib.py
Misc/NEWS.d/next/Library/2017-11-03-08-36-03.bpo-31933.UrtoMP.rst [new file with mode: 0644]
Modules/_blake2/blake2b_impl.c
Modules/_blake2/blake2s_impl.c

index 647719ea62ce1a136743dea1f39a88b0032d1afc..981748892efb3d144087e974d83f756b350207fc 100644 (file)
@@ -618,6 +618,24 @@ class HashLibTestCase(unittest.TestCase):
           "ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d1"+
           "7d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")
 
+    @requires_blake2
+    def test_case_blake2b_all_parameters(self):
+        # This checks that all the parameters work in general, and also that
+        # parameter byte order doesn't get confused on big endian platforms.
+        self.check('blake2b', b"foo",
+          "920568b0c5873b2f0ab67bedb6cf1b2b",
+          digest_size=16,
+          key=b"bar",
+          salt=b"baz",
+          person=b"bing",
+          fanout=2,
+          depth=3,
+          leaf_size=4,
+          node_offset=5,
+          node_depth=6,
+          inner_size=7,
+          last_node=True)
+
     @requires_blake2
     def test_blake2b_vectors(self):
         for msg, key, md in read_vectors('blake2b'):
@@ -643,6 +661,24 @@ class HashLibTestCase(unittest.TestCase):
         self.check('blake2s', b"abc",
           "508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982")
 
+    @requires_blake2
+    def test_case_blake2s_all_parameters(self):
+        # This checks that all the parameters work in general, and also that
+        # parameter byte order doesn't get confused on big endian platforms.
+        self.check('blake2s', b"foo",
+          "bf2a8f7fe3c555012a6f8046e646bc75",
+          digest_size=16,
+          key=b"bar",
+          salt=b"baz",
+          person=b"bing",
+          fanout=2,
+          depth=3,
+          leaf_size=4,
+          node_offset=5,
+          node_depth=6,
+          inner_size=7,
+          last_node=True)
+
     @requires_blake2
     def test_blake2s_vectors(self):
         for msg, key, md in read_vectors('blake2s'):
diff --git a/Misc/NEWS.d/next/Library/2017-11-03-08-36-03.bpo-31933.UrtoMP.rst b/Misc/NEWS.d/next/Library/2017-11-03-08-36-03.bpo-31933.UrtoMP.rst
new file mode 100644 (file)
index 0000000..c4ee1d2
--- /dev/null
@@ -0,0 +1,2 @@
+Fix Blake2 params leaf_size and node_offset on big endian platforms. Patch
+by Jack O'Connor.
index 58b502bb349b820394e8caaa849faac558ef5d69..09b200ebe1726c33faea57795ce60c293b66401b 100644 (file)
@@ -166,7 +166,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
             goto error;
         }
     }
-    self->param.leaf_length = (unsigned int)leaf_size;
+    // NB: Simple assignment here would be incorrect on big endian platforms.
+    store32(&(self->param.leaf_length), leaf_size);
 
     if (node_offset_obj != NULL) {
         node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -182,7 +183,8 @@ py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
      }
     store48(&(self->param.node_offset), node_offset);
 #else
-    self->param.node_offset = node_offset;
+    // NB: Simple assignment here would be incorrect on big endian platforms.
+    store64(&(self->param.node_offset), node_offset);
 #endif
 
     if (node_depth < 0 || node_depth > 255) {
index 11d2e02251b8bd4f2ad8d0612845d58a7baed22c..735b6f08deb6f8bc8bc04a8c4ec854d47b100483 100644 (file)
@@ -166,7 +166,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
             goto error;
         }
     }
-    self->param.leaf_length = (unsigned int)leaf_size;
+    // NB: Simple assignment here would be incorrect on big endian platforms.
+    store32(&(self->param.leaf_length), leaf_size);
 
     if (node_offset_obj != NULL) {
         node_offset = PyLong_AsUnsignedLongLong(node_offset_obj);
@@ -182,7 +183,8 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
      }
     store48(&(self->param.node_offset), node_offset);
 #else
-    self->param.node_offset = node_offset;
+    // NB: Simple assignment here would be incorrect on big endian platforms.
+    store64(&(self->param.node_offset), node_offset);
 #endif
 
     if (node_depth < 0 || node_depth > 255) {