gptsum


Namegptsum JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/NicolasT/gptsum
SummaryA tool to make disk images using GPT partitions self-verifiable
upload_time2023-01-25 12:51:04
maintainer
docs_urlNone
authorNicolas Trangez
requires_python>=3.7.15,<4.0.0
licenseApache-2.0
keywords gpt diskimage checksum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            gptsum
======
A tool to make disk images using GPT_ partitions self-verifiable, like
`isomd5sum`_.

Note this *only* works for read-only, immutable images!

.. _GPT: https://en.wikipedia.org/wiki/GUID_Partition_Table
.. _isomd5sum: https://github.com/rhinstaller/isomd5sum

Quickstart
**********
.. When making changes to the quickstart code below,
   make sure 'tests/test_readme.py' is updated accordingly.

First, create an empty disk image::

    $ truncate -s64M image.raw

Then, create a GPT partition table in it. Note, below we set an explicit
`label-id`. In general, this should not be done and a random GUID will be
generated. Simply remove the line.

::

    $ sfdisk image.raw << EOF
    label: gpt
    label-id: 132e3631-1ec9-4411-ab25-9b95b54b0903
    first-lba: 2048
    EOF

    Checking that no-one is using this disk right now ... OK

    Disk image.raw: 64 MiB, 67108864 bytes, 131072 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes

    >>> Script header accepted.
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Done.
    Created a new GPT disklabel (GUID: 132E3631-1EC9-4411-AB25-9B95B54B0903).

    New situation:
    Disklabel type: gpt
    Disk identifier: 132E3631-1EC9-4411-AB25-9B95B54B0903

    The partition table has been altered.
    Syncing disks.

We can retrieve the current disk GUID using ``gptsum``::

    $ gptsum get-guid image.raw
    132e3631-1ec9-4411-ab25-9b95b54b0903

Verification should fail::

    $ gptsum verify image.raw || echo "Verification failed!"
    Disk GUID doesn't match expected checksum, got 132e3631-1ec9-4411-ab25-9b95b54b0903, expected 6190f5bb-1967-14ec-9fbd-a7d213a45461
    Verification failed!

Embed the disk checksum as the label GUID::

    $ gptsum embed image.raw

Verification should now succeed::

    $ gptsum verify image.raw && echo "Verification succeeded!"
    Verification succeeded!

Indeed, the GUID was changed::

    $ gptsum get-guid image.raw
    6190f5bb-1967-14ec-9fbd-a7d213a45461


How It Works
************
Generally, when checksums are used to validate the integrity of a file, this
checksum needs to be provided out-of-band, e.g., in a separate file. This
complicates the process a bit, since now multiple files need to be kept around
(and potentially in sync).

Being able to verify a file's integrity without any external information would
be great. However, if we embed the checksum of a file in the file itself, we
change its checksum and verification would fail. So we need to apply some
tricks.

The `isomd5sum`_ tool is often used to verify the integrity of ISO files, e.g.,
Linux distribution releases. It uses an unused location in the ISO9660 file
format to embed an MD5 checksum of the actual data segments of said file. As
such, this MD5 checksum does not represent the complete file contents but only
the pieces of data we're interested in.

We can translate this to GPT-partitioned disk images as well. In the GPT
format, there's no room to embed any arbitrary blobs (unless we'd use the
reserved or padding sections of the headers, which should be zeroed out so we
shouldn't). However, GPT disks are identified by a GUID which is stored in the
two metadata sections stored on the disk, at LBA 1 and as the last LBA on the
disk (the so-called primary and secondary GPT headers). This leaves room for 16
bytes of semi-arbitrary data.

Furthermore, the GPT headers themselves, including the GUID, are protected
using a CRC32 checksum.

As such, we can apply the following procedure:

- Zero out the CRC32 and GUID fields in both GPT headers
- Calculate a 16 byte checksum of the resulting image (covering all data,
  except for the CRC32 and GUID fields)
- Embed the checksum as the GUID field in both GPT headers (now becoming the
  disk GUID)
- Update the CRC32 fields in both GPT headers

At this point we have a fully valid GPT disk image with a GUID representing
the actual data contained in the image. One could argue this is no longer a
valid GUID (indeed it's not), but since it's generated using a secure hashing
algorithm over a (potentially large) file, we can assume the entropy is
sufficient to avoid collisions. Essentially, if two disk images were to get the
same GUID, they're very likely the exact same disk image, content-wise.

Verifying an image file is roughly the same procedure:

- Zero out the CRC32 and GUID fields in both GPT headers (in-memory)
- Calculate a 16 byte checksum of the resulting image
- Verify this calculated checksum equals the actual GUID embedded in the image

Implementation Details
**********************
`gptsum` is implemented in Python, and compatible with Python 3.7 and later.
It uses the `Blake2b`_ checksum algorithm to construct a 16 byte digest
of the disk image.

Various subcommands are exposed by the CLI, refer to the `documentation`_
for more details.

.. _Blake2b: https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2
.. _documentation: https://nicolast.github.io/gptsum/


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/NicolasT/gptsum",
    "name": "gptsum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7.15,<4.0.0",
    "maintainer_email": "",
    "keywords": "gpt,diskimage,checksum",
    "author": "Nicolas Trangez",
    "author_email": "ikke@nicolast.be",
    "download_url": "https://files.pythonhosted.org/packages/c1/36/563b6d77ad94823e1d984e9d0e919c8b63a42746cbca5d9fe42730bc590e/gptsum-0.2.0.tar.gz",
    "platform": null,
    "description": "gptsum\n======\nA tool to make disk images using GPT_ partitions self-verifiable, like\n`isomd5sum`_.\n\nNote this *only* works for read-only, immutable images!\n\n.. _GPT: https://en.wikipedia.org/wiki/GUID_Partition_Table\n.. _isomd5sum: https://github.com/rhinstaller/isomd5sum\n\nQuickstart\n**********\n.. When making changes to the quickstart code below,\n   make sure 'tests/test_readme.py' is updated accordingly.\n\nFirst, create an empty disk image::\n\n    $ truncate -s64M image.raw\n\nThen, create a GPT partition table in it. Note, below we set an explicit\n`label-id`. In general, this should not be done and a random GUID will be\ngenerated. Simply remove the line.\n\n::\n\n    $ sfdisk image.raw << EOF\n    label: gpt\n    label-id: 132e3631-1ec9-4411-ab25-9b95b54b0903\n    first-lba: 2048\n    EOF\n\n    Checking that no-one is using this disk right now ... OK\n\n    Disk image.raw: 64 MiB, 67108864 bytes, 131072 sectors\n    Units: sectors of 1 * 512 = 512 bytes\n    Sector size (logical/physical): 512 bytes / 512 bytes\n    I/O size (minimum/optimal): 512 bytes / 512 bytes\n\n    >>> Script header accepted.\n    >>> Script header accepted.\n    >>> Script header accepted.\n    >>> Done.\n    Created a new GPT disklabel (GUID: 132E3631-1EC9-4411-AB25-9B95B54B0903).\n\n    New situation:\n    Disklabel type: gpt\n    Disk identifier: 132E3631-1EC9-4411-AB25-9B95B54B0903\n\n    The partition table has been altered.\n    Syncing disks.\n\nWe can retrieve the current disk GUID using ``gptsum``::\n\n    $ gptsum get-guid image.raw\n    132e3631-1ec9-4411-ab25-9b95b54b0903\n\nVerification should fail::\n\n    $ gptsum verify image.raw || echo \"Verification failed!\"\n    Disk GUID doesn't match expected checksum, got 132e3631-1ec9-4411-ab25-9b95b54b0903, expected 6190f5bb-1967-14ec-9fbd-a7d213a45461\n    Verification failed!\n\nEmbed the disk checksum as the label GUID::\n\n    $ gptsum embed image.raw\n\nVerification should now succeed::\n\n    $ gptsum verify image.raw && echo \"Verification succeeded!\"\n    Verification succeeded!\n\nIndeed, the GUID was changed::\n\n    $ gptsum get-guid image.raw\n    6190f5bb-1967-14ec-9fbd-a7d213a45461\n\n\nHow It Works\n************\nGenerally, when checksums are used to validate the integrity of a file, this\nchecksum needs to be provided out-of-band, e.g., in a separate file. This\ncomplicates the process a bit, since now multiple files need to be kept around\n(and potentially in sync).\n\nBeing able to verify a file's integrity without any external information would\nbe great. However, if we embed the checksum of a file in the file itself, we\nchange its checksum and verification would fail. So we need to apply some\ntricks.\n\nThe `isomd5sum`_ tool is often used to verify the integrity of ISO files, e.g.,\nLinux distribution releases. It uses an unused location in the ISO9660 file\nformat to embed an MD5 checksum of the actual data segments of said file. As\nsuch, this MD5 checksum does not represent the complete file contents but only\nthe pieces of data we're interested in.\n\nWe can translate this to GPT-partitioned disk images as well. In the GPT\nformat, there's no room to embed any arbitrary blobs (unless we'd use the\nreserved or padding sections of the headers, which should be zeroed out so we\nshouldn't). However, GPT disks are identified by a GUID which is stored in the\ntwo metadata sections stored on the disk, at LBA 1 and as the last LBA on the\ndisk (the so-called primary and secondary GPT headers). This leaves room for 16\nbytes of semi-arbitrary data.\n\nFurthermore, the GPT headers themselves, including the GUID, are protected\nusing a CRC32 checksum.\n\nAs such, we can apply the following procedure:\n\n- Zero out the CRC32 and GUID fields in both GPT headers\n- Calculate a 16 byte checksum of the resulting image (covering all data,\n  except for the CRC32 and GUID fields)\n- Embed the checksum as the GUID field in both GPT headers (now becoming the\n  disk GUID)\n- Update the CRC32 fields in both GPT headers\n\nAt this point we have a fully valid GPT disk image with a GUID representing\nthe actual data contained in the image. One could argue this is no longer a\nvalid GUID (indeed it's not), but since it's generated using a secure hashing\nalgorithm over a (potentially large) file, we can assume the entropy is\nsufficient to avoid collisions. Essentially, if two disk images were to get the\nsame GUID, they're very likely the exact same disk image, content-wise.\n\nVerifying an image file is roughly the same procedure:\n\n- Zero out the CRC32 and GUID fields in both GPT headers (in-memory)\n- Calculate a 16 byte checksum of the resulting image\n- Verify this calculated checksum equals the actual GUID embedded in the image\n\nImplementation Details\n**********************\n`gptsum` is implemented in Python, and compatible with Python 3.7 and later.\nIt uses the `Blake2b`_ checksum algorithm to construct a 16 byte digest\nof the disk image.\n\nVarious subcommands are exposed by the CLI, refer to the `documentation`_\nfor more details.\n\n.. _Blake2b: https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2\n.. _documentation: https://nicolast.github.io/gptsum/\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "A tool to make disk images using GPT partitions self-verifiable",
    "version": "0.2.0",
    "split_keywords": [
        "gpt",
        "diskimage",
        "checksum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0544231ebbccd971334bd2876a27fcd765cfc306f3ccc445c1f8300ac5e018ad",
                "md5": "fe566d071fdb7d9b8f8c0ebc201bc128",
                "sha256": "e7bfe706e3c26afeeab36d04f48dd2a36be4d014e0fb25085bff03dc694c8e05"
            },
            "downloads": -1,
            "filename": "gptsum-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fe566d071fdb7d9b8f8c0ebc201bc128",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7.15,<4.0.0",
            "size": 15567,
            "upload_time": "2023-01-25T12:51:02",
            "upload_time_iso_8601": "2023-01-25T12:51:02.926367Z",
            "url": "https://files.pythonhosted.org/packages/05/44/231ebbccd971334bd2876a27fcd765cfc306f3ccc445c1f8300ac5e018ad/gptsum-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c136563b6d77ad94823e1d984e9d0e919c8b63a42746cbca5d9fe42730bc590e",
                "md5": "d7adbe04d0106aec71d238def0b13b5a",
                "sha256": "66d8eedc7422f60f92fdb34ae29960c169556dc7dcf72c9ccb00510fe4df0522"
            },
            "downloads": -1,
            "filename": "gptsum-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d7adbe04d0106aec71d238def0b13b5a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7.15,<4.0.0",
            "size": 58468,
            "upload_time": "2023-01-25T12:51:04",
            "upload_time_iso_8601": "2023-01-25T12:51:04.293795Z",
            "url": "https://files.pythonhosted.org/packages/c1/36/563b6d77ad94823e1d984e9d0e919c8b63a42746cbca5d9fe42730bc590e/gptsum-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-25 12:51:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "NicolasT",
    "github_project": "gptsum",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "gptsum"
}
        
Elapsed time: 0.03105s