{"id":812799,"url":"http://patchwork.ozlabs.org/api/patches/812799/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170912112855.24269-7-berrange@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170912112855.24269-7-berrange@redhat.com>","list_archive_url":null,"date":"2017-09-12T11:28:54","name":"[v3,6/7] block: convert qcrypto_block_encrypt|decrypt to take bytes offset","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"b46567344fbb2090b12ff018af8f86d45c99b4ac","submitter":{"id":2694,"url":"http://patchwork.ozlabs.org/api/people/2694/?format=json","name":"Daniel P. Berrange","email":"berrange@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170912112855.24269-7-berrange@redhat.com/mbox/","series":[{"id":2673,"url":"http://patchwork.ozlabs.org/api/series/2673/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=2673","date":"2017-09-12T11:28:48","name":"Misc improvements to crypto block driver","version":3,"mbox":"http://patchwork.ozlabs.org/series/2673/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/812799/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/812799/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=berrange@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xs2f82QQzz9s7B\n\tfor <incoming@patchwork.ozlabs.org>;\n\tTue, 12 Sep 2017 21:30:24 +1000 (AEST)","from localhost ([::1]:35008 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1drjOY-0003Om-9M\n\tfor incoming@patchwork.ozlabs.org; Tue, 12 Sep 2017 07:30:22 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:53925)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <berrange@redhat.com>) id 1drjNX-00038b-RF\n\tfor qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:21 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <berrange@redhat.com>) id 1drjNW-0004V3-0o\n\tfor qemu-devel@nongnu.org; Tue, 12 Sep 2017 07:29:19 -0400","from mx1.redhat.com ([209.132.183.28]:38634)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <berrange@redhat.com>)\n\tid 1drjNR-0004SR-MJ; Tue, 12 Sep 2017 07:29:13 -0400","from smtp.corp.redhat.com\n\t(int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id C5C4C83F42;\n\tTue, 12 Sep 2017 11:29:12 +0000 (UTC)","from localhost.localdomain.com (unknown [10.42.22.189])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 756D16C41C;\n\tTue, 12 Sep 2017 11:29:11 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com C5C4C83F42","From":"\"Daniel P. Berrange\" <berrange@redhat.com>","To":"qemu-devel@nongnu.org","Date":"Tue, 12 Sep 2017 12:28:54 +0100","Message-Id":"<20170912112855.24269-7-berrange@redhat.com>","In-Reply-To":"<20170912112855.24269-1-berrange@redhat.com>","References":"<20170912112855.24269-1-berrange@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.11","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.27]);\n\tTue, 12 Sep 2017 11:29:12 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH v3 6/7] block: convert\n\tqcrypto_block_encrypt|decrypt to take bytes offset","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Kevin Wolf <kwolf@redhat.com>, qemu-block@nongnu.org,\n\tStefan Hajnoczi <stefanha@gmail.com>, Max Reitz <mreitz@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"Instead of sector offset, take the bytes offset when encrypting\nor decrypting data.\n\nSigned-off-by: Daniel P. Berrange <berrange@redhat.com>\n---\n block/crypto.c         | 12 ++++--------\n block/qcow.c           |  7 +++++--\n block/qcow2-cluster.c  |  8 +++-----\n block/qcow2.c          |  4 ++--\n crypto/block-luks.c    | 12 ++++++++----\n crypto/block-qcow.c    | 12 ++++++++----\n crypto/block.c         | 20 ++++++++++++++------\n crypto/blockpriv.h     |  4 ++--\n include/crypto/block.h | 14 ++++++++------\n 9 files changed, 54 insertions(+), 39 deletions(-)","diff":"diff --git a/block/crypto.c b/block/crypto.c\nindex d004e9cef4..1f77095336 100644\n--- a/block/crypto.c\n+++ b/block/crypto.c\n@@ -394,7 +394,6 @@ block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,\n     int ret = 0;\n     uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);\n     size_t payload_offset = qcrypto_block_get_payload_offset(crypto->block);\n-    uint64_t sector_num = offset / sector_size;\n \n     assert(!flags);\n     assert(payload_offset < INT64_MAX);\n@@ -430,15 +429,14 @@ block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,\n             goto cleanup;\n         }\n \n-        if (qcrypto_block_decrypt(crypto->block, sector_num, cipher_data,\n-                                  cur_bytes, NULL) < 0) {\n+        if (qcrypto_block_decrypt(crypto->block, offset + bytes_done,\n+                                  cipher_data, cur_bytes, NULL) < 0) {\n             ret = -EIO;\n             goto cleanup;\n         }\n \n         qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes);\n \n-        sector_num += cur_bytes / sector_size;\n         bytes -= cur_bytes;\n         bytes_done += cur_bytes;\n     }\n@@ -463,7 +461,6 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,\n     int ret = 0;\n     uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);\n     uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block);\n-    uint64_t sector_num = offset / sector_size;\n \n     assert(!flags);\n     assert(payload_offset < INT64_MAX);\n@@ -492,8 +489,8 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,\n \n         qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes);\n \n-        if (qcrypto_block_encrypt(crypto->block, sector_num, cipher_data,\n-                                  cur_bytes, NULL) < 0) {\n+        if (qcrypto_block_encrypt(crypto->block, offset + bytes_done,\n+                                  cipher_data, cur_bytes, NULL) < 0) {\n             ret = -EIO;\n             goto cleanup;\n         }\n@@ -507,7 +504,6 @@ block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,\n             goto cleanup;\n         }\n \n-        sector_num += cur_bytes / sector_size;\n         bytes -= cur_bytes;\n         bytes_done += cur_bytes;\n     }\ndiff --git a/block/qcow.c b/block/qcow.c\nindex f450b00cfc..d242301ed2 100644\n--- a/block/qcow.c\n+++ b/block/qcow.c\n@@ -478,7 +478,9 @@ static int get_cluster_offset(BlockDriverState *bs,\n                     for(i = 0; i < s->cluster_sectors; i++) {\n                         if (i < n_start || i >= n_end) {\n                             memset(s->cluster_data, 0x00, 512);\n-                            if (qcrypto_block_encrypt(s->crypto, start_sect + i,\n+                            if (qcrypto_block_encrypt(s->crypto,\n+                                                      (start_sect + i) *\n+                                                      BDRV_SECTOR_SIZE,\n                                                       s->cluster_data,\n                                                       BDRV_SECTOR_SIZE,\n                                                       NULL) < 0) {\n@@ -668,7 +670,8 @@ static coroutine_fn int qcow_co_readv(BlockDriverState *bs, int64_t sector_num,\n             }\n             if (bs->encrypted) {\n                 assert(s->crypto);\n-                if (qcrypto_block_decrypt(s->crypto, sector_num, buf,\n+                if (qcrypto_block_decrypt(s->crypto,\n+                                          sector_num * BDRV_SECTOR_SIZE, buf,\n                                           n * BDRV_SECTOR_SIZE, NULL) < 0) {\n                     ret = -EIO;\n                     break;\ndiff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c\nindex 0d4824993c..09ae0b6ecc 100644\n--- a/block/qcow2-cluster.c\n+++ b/block/qcow2-cluster.c\n@@ -396,15 +396,13 @@ static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs,\n {\n     if (bytes && bs->encrypted) {\n         BDRVQcow2State *s = bs->opaque;\n-        int64_t sector = (s->crypt_physical_offset ?\n+        int64_t offset = (s->crypt_physical_offset ?\n                           (cluster_offset + offset_in_cluster) :\n-                          (src_cluster_offset + offset_in_cluster))\n-                         >> BDRV_SECTOR_BITS;\n+                          (src_cluster_offset + offset_in_cluster));\n         assert((offset_in_cluster & ~BDRV_SECTOR_MASK) == 0);\n         assert((bytes & ~BDRV_SECTOR_MASK) == 0);\n         assert(s->crypto);\n-        if (qcrypto_block_encrypt(s->crypto, sector, buffer,\n-                                  bytes, NULL) < 0) {\n+        if (qcrypto_block_encrypt(s->crypto, offset, buffer, bytes, NULL) < 0) {\n             return false;\n         }\n     }\ndiff --git a/block/qcow2.c b/block/qcow2.c\nindex bae5893327..4508683ea3 100644\n--- a/block/qcow2.c\n+++ b/block/qcow2.c\n@@ -1810,7 +1810,7 @@ static coroutine_fn int qcow2_co_preadv(BlockDriverState *bs, uint64_t offset,\n                 if (qcrypto_block_decrypt(s->crypto,\n                                           (s->crypt_physical_offset ?\n                                            cluster_offset + offset_in_cluster :\n-                                           offset) >> BDRV_SECTOR_BITS,\n+                                           offset),\n                                           cluster_data,\n                                           cur_bytes,\n                                           NULL) < 0) {\n@@ -1945,7 +1945,7 @@ static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t offset,\n             if (qcrypto_block_encrypt(s->crypto,\n                                       (s->crypt_physical_offset ?\n                                        cluster_offset + offset_in_cluster :\n-                                       offset) >> BDRV_SECTOR_BITS,\n+                                       offset),\n                                       cluster_data,\n                                       cur_bytes, NULL) < 0) {\n                 ret = -EIO;\ndiff --git a/crypto/block-luks.c b/crypto/block-luks.c\nindex a9062bb0f2..d418ac30b8 100644\n--- a/crypto/block-luks.c\n+++ b/crypto/block-luks.c\n@@ -1399,29 +1399,33 @@ static void qcrypto_block_luks_cleanup(QCryptoBlock *block)\n \n static int\n qcrypto_block_luks_decrypt(QCryptoBlock *block,\n-                           uint64_t startsector,\n+                           uint64_t offset,\n                            uint8_t *buf,\n                            size_t len,\n                            Error **errp)\n {\n+    assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));\n+    assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));\n     return qcrypto_block_decrypt_helper(block->cipher,\n                                         block->niv, block->ivgen,\n                                         QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,\n-                                        startsector, buf, len, errp);\n+                                        offset, buf, len, errp);\n }\n \n \n static int\n qcrypto_block_luks_encrypt(QCryptoBlock *block,\n-                           uint64_t startsector,\n+                           uint64_t offset,\n                            uint8_t *buf,\n                            size_t len,\n                            Error **errp)\n {\n+    assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));\n+    assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));\n     return qcrypto_block_encrypt_helper(block->cipher,\n                                         block->niv, block->ivgen,\n                                         QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,\n-                                        startsector, buf, len, errp);\n+                                        offset, buf, len, errp);\n }\n \n \ndiff --git a/crypto/block-qcow.c b/crypto/block-qcow.c\nindex 4dd594a9ba..8817d6aaa7 100644\n--- a/crypto/block-qcow.c\n+++ b/crypto/block-qcow.c\n@@ -143,29 +143,33 @@ qcrypto_block_qcow_cleanup(QCryptoBlock *block)\n \n static int\n qcrypto_block_qcow_decrypt(QCryptoBlock *block,\n-                           uint64_t startsector,\n+                           uint64_t offset,\n                            uint8_t *buf,\n                            size_t len,\n                            Error **errp)\n {\n+    assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));\n+    assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));\n     return qcrypto_block_decrypt_helper(block->cipher,\n                                         block->niv, block->ivgen,\n                                         QCRYPTO_BLOCK_QCOW_SECTOR_SIZE,\n-                                        startsector, buf, len, errp);\n+                                        offset, buf, len, errp);\n }\n \n \n static int\n qcrypto_block_qcow_encrypt(QCryptoBlock *block,\n-                           uint64_t startsector,\n+                           uint64_t offset,\n                            uint8_t *buf,\n                            size_t len,\n                            Error **errp)\n {\n+    assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));\n+    assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));\n     return qcrypto_block_encrypt_helper(block->cipher,\n                                         block->niv, block->ivgen,\n                                         QCRYPTO_BLOCK_QCOW_SECTOR_SIZE,\n-                                        startsector, buf, len, errp);\n+                                        offset, buf, len, errp);\n }\n \n \ndiff --git a/crypto/block.c b/crypto/block.c\nindex a7a9ad240e..f206d5eea8 100644\n--- a/crypto/block.c\n+++ b/crypto/block.c\n@@ -127,22 +127,22 @@ QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock *block,\n \n \n int qcrypto_block_decrypt(QCryptoBlock *block,\n-                          uint64_t startsector,\n+                          uint64_t offset,\n                           uint8_t *buf,\n                           size_t len,\n                           Error **errp)\n {\n-    return block->driver->decrypt(block, startsector, buf, len, errp);\n+    return block->driver->decrypt(block, offset, buf, len, errp);\n }\n \n \n int qcrypto_block_encrypt(QCryptoBlock *block,\n-                          uint64_t startsector,\n+                          uint64_t offset,\n                           uint8_t *buf,\n                           size_t len,\n                           Error **errp)\n {\n-    return block->driver->encrypt(block, startsector, buf, len, errp);\n+    return block->driver->encrypt(block, offset, buf, len, errp);\n }\n \n \n@@ -194,13 +194,17 @@ int qcrypto_block_decrypt_helper(QCryptoCipher *cipher,\n                                  size_t niv,\n                                  QCryptoIVGen *ivgen,\n                                  int sectorsize,\n-                                 uint64_t startsector,\n+                                 uint64_t offset,\n                                  uint8_t *buf,\n                                  size_t len,\n                                  Error **errp)\n {\n     uint8_t *iv;\n     int ret = -1;\n+    uint64_t startsector = offset / sectorsize;\n+\n+    assert(QEMU_IS_ALIGNED(offset, sectorsize));\n+    assert(QEMU_IS_ALIGNED(len, sectorsize));\n \n     iv = niv ? g_new0(uint8_t, niv) : NULL;\n \n@@ -243,13 +247,17 @@ int qcrypto_block_encrypt_helper(QCryptoCipher *cipher,\n                                  size_t niv,\n                                  QCryptoIVGen *ivgen,\n                                  int sectorsize,\n-                                 uint64_t startsector,\n+                                 uint64_t offset,\n                                  uint8_t *buf,\n                                  size_t len,\n                                  Error **errp)\n {\n     uint8_t *iv;\n     int ret = -1;\n+    uint64_t startsector = offset / sectorsize;\n+\n+    assert(QEMU_IS_ALIGNED(offset, sectorsize));\n+    assert(QEMU_IS_ALIGNED(len, sectorsize));\n \n     iv = niv ? g_new0(uint8_t, niv) : NULL;\n \ndiff --git a/crypto/blockpriv.h b/crypto/blockpriv.h\nindex d227522d88..41840abcec 100644\n--- a/crypto/blockpriv.h\n+++ b/crypto/blockpriv.h\n@@ -82,7 +82,7 @@ int qcrypto_block_decrypt_helper(QCryptoCipher *cipher,\n                                  size_t niv,\n                                  QCryptoIVGen *ivgen,\n                                  int sectorsize,\n-                                 uint64_t startsector,\n+                                 uint64_t offset,\n                                  uint8_t *buf,\n                                  size_t len,\n                                  Error **errp);\n@@ -91,7 +91,7 @@ int qcrypto_block_encrypt_helper(QCryptoCipher *cipher,\n                                  size_t niv,\n                                  QCryptoIVGen *ivgen,\n                                  int sectorsize,\n-                                 uint64_t startsector,\n+                                 uint64_t offset,\n                                  uint8_t *buf,\n                                  size_t len,\n                                  Error **errp);\ndiff --git a/include/crypto/block.h b/include/crypto/block.h\nindex 13232b2472..cd18f46d56 100644\n--- a/include/crypto/block.h\n+++ b/include/crypto/block.h\n@@ -161,18 +161,19 @@ QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock *block,\n /**\n  * @qcrypto_block_decrypt:\n  * @block: the block encryption object\n- * @startsector: the sector from which @buf was read\n+ * @offset: the position at which @iov was read\n  * @buf: the buffer to decrypt\n  * @len: the length of @buf in bytes\n  * @errp: pointer to a NULL-initialized error object\n  *\n  * Decrypt @len bytes of cipher text in @buf, writing\n- * plain text back into @buf\n+ * plain text back into @buf. @len and @offset must be\n+ * a multiple of the encryption format sector size.\n  *\n  * Returns 0 on success, -1 on failure\n  */\n int qcrypto_block_decrypt(QCryptoBlock *block,\n-                          uint64_t startsector,\n+                          uint64_t offset,\n                           uint8_t *buf,\n                           size_t len,\n                           Error **errp);\n@@ -180,18 +181,19 @@ int qcrypto_block_decrypt(QCryptoBlock *block,\n /**\n  * @qcrypto_block_encrypt:\n  * @block: the block encryption object\n- * @startsector: the sector to which @buf will be written\n+ * @offset: the position at which @iov will be written\n  * @buf: the buffer to decrypt\n  * @len: the length of @buf in bytes\n  * @errp: pointer to a NULL-initialized error object\n  *\n  * Encrypt @len bytes of plain text in @buf, writing\n- * cipher text back into @buf\n+ * cipher text back into @buf. @len and @offset must be\n+ * a multiple of the encryption format sector size.\n  *\n  * Returns 0 on success, -1 on failure\n  */\n int qcrypto_block_encrypt(QCryptoBlock *block,\n-                          uint64_t startsector,\n+                          uint64_t offset,\n                           uint8_t *buf,\n                           size_t len,\n                           Error **errp);\n","prefixes":["v3","6/7"]}