Skip to contentSkip to conversation

mmc-utils: RPMB support

PATCH In review Roman Pen 3 participants 12 messages Jul 21, 2014 – Aug 12, 2014

Subsystems (2)
lore
Share & tools
lore

Roman Pen

author
#0 lore raw

cover mmc-utils: RPMB support

This is simple RPMB support for 4 operations:
write-key
read-counter
write-block
read-block

I did not attempt to implement multiple blocks read/write, thus only 1 block (256 b) is supported right now.

Also I had to take HMAC SHA256 implementation as 3rdparty to sign RPMB messages.

Roman Pen (2):
mmc-utils: RPMB: add HMAC SHA256 support
mmc-utils: RPMB: add support for 4 rpmb operations

 3rdparty/hmac_sha/hmac_sha2.c | 544 ++++++++++++++++++++++++
 3rdparty/hmac_sha/hmac_sha2.h | 140 +++++++
 3rdparty/hmac_sha/sha2.c      | 949 ++++++++++++++++++++++++++++++++++++++++++
 3rdparty/hmac_sha/sha2.h      | 108 +++++
 Makefile                      |   8 +-
 mmc.c                         |  28 ++
 mmc.h                         |   6 +
 mmc_cmds.c                    | 419 +++++++++++++++++++
 mmc_cmds.h                    |   4 +
 9 files changed, 2204 insertions(+), 2 deletions(-)
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.c
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.h
 create mode 100644 3rdparty/hmac_sha/sha2.c
 create mode 100644 3rdparty/hmac_sha/sha2.h
Signature · 1 line
2.0.0
author
#1 lore raw

PATCH 1/2 mmc-utils: RPMB: add HMAC SHA256 support

RPMB requires digital signature of the message.
Take the implementation of HMAC SHA256 from this
place https://github.com/ogay/hmac.git

Actually this is BSD license, IANAL but according
to my understanding nothing terrible will happen
if we provide all the original copyrights.

---
 3rdparty/hmac_sha/hmac_sha2.c | 544 ++++++++++++++++++++++++
 3rdparty/hmac_sha/hmac_sha2.h | 140 +++++++
 3rdparty/hmac_sha/sha2.c      | 949 ++++++++++++++++++++++++++++++++++++++++++
 3rdparty/hmac_sha/sha2.h      | 108 +++++
 Makefile                      |   8 +-
 5 files changed, 1747 insertions(+), 2 deletions(-)
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.c
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.h
 create mode 100644 3rdparty/hmac_sha/sha2.c
 create mode 100644 3rdparty/hmac_sha/sha2.h
3rdparty/hmac_sha/hmac_sha2.c new file +544 -0
new file mode 100644
index 0000000..1130ebf
@@ -0,0 +1,544 @@
+/*
+ * HMAC-SHA-224/256/384/512 implementation
+ * Last update: 06/15/2005
+ * Issue date:  06/15/2005
+ *
+ * Copyright (C) 2005 Olivier Gay <[email protected]>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <string.h>
+
+#include "hmac_sha2.h"
+
+/* HMAC-SHA-224 functions */
+
+void hmac_sha224_init(hmac_sha224_ctx *ctx, const unsigned char *key,
+                      unsigned int key_size)
+{
+    unsigned int fill;
+    unsigned int num;
+
+    const unsigned char *key_used;
+    unsigned char key_temp[SHA224_DIGEST_SIZE];
+    int i;
+
+    if (key_size == SHA224_BLOCK_SIZE) {
+        key_used = key;
+        num = SHA224_BLOCK_SIZE;
+    } else {
+        if (key_size > SHA224_BLOCK_SIZE){
+            num = SHA224_DIGEST_SIZE;
+            sha224(key, key_size, key_temp);
+            key_used = key_temp;
+        } else { /* key_size > SHA224_BLOCK_SIZE */
+            key_used = key;
+            num = key_size;
+        }
+        fill = SHA224_BLOCK_SIZE - num;
+
+        memset(ctx->block_ipad + num, 0x36, fill);
+        memset(ctx->block_opad + num, 0x5c, fill);
+    }
+
+    for (i = 0; i < (int) num; i++) {
+        ctx->block_ipad[i] = key_used[i] ^ 0x36;
+        ctx->block_opad[i] = key_used[i] ^ 0x5c;
+    }
+
+    sha224_init(&ctx->ctx_inside);
+    sha224_update(&ctx->ctx_inside, ctx->block_ipad, SHA224_BLOCK_SIZE);
+
+    sha224_init(&ctx->ctx_outside);
+    sha224_update(&ctx->ctx_outside, ctx->block_opad,
+                  SHA224_BLOCK_SIZE);
+
+    /* for hmac_reinit */
+    memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
+           sizeof(sha224_ctx));
+    memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
+           sizeof(sha224_ctx));
+}
+
+void hmac_sha224_reinit(hmac_sha224_ctx *ctx)
+{
+    memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
+           sizeof(sha224_ctx));
+    memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
+           sizeof(sha224_ctx));
+}
+
+void hmac_sha224_update(hmac_sha224_ctx *ctx, const unsigned char *message,
+                        unsigned int message_len)
+{
+    sha224_update(&ctx->ctx_inside, message, message_len);
+}
+
+void hmac_sha224_final(hmac_sha224_ctx *ctx, unsigned char *mac,
+                       unsigned int mac_size)
+{
+    unsigned char digest_inside[SHA224_DIGEST_SIZE];
+    unsigned char mac_temp[SHA224_DIGEST_SIZE];
+
+    sha224_final(&ctx->ctx_inside, digest_inside);
+    sha224_update(&ctx->ctx_outside, digest_inside, SHA224_DIGEST_SIZE);
+    sha224_final(&ctx->ctx_outside, mac_temp);
+    memcpy(mac, mac_temp, mac_size);
+}
+
+void hmac_sha224(const unsigned char *key, unsigned int key_size,
+          const unsigned char *message, unsigned int message_len,
+          unsigned char *mac, unsigned mac_size)
+{
+    hmac_sha224_ctx ctx;
+
+    hmac_sha224_init(&ctx, key, key_size);
+    hmac_sha224_update(&ctx, message, message_len);
+    hmac_sha224_final(&ctx, mac, mac_size);
+}
+
+/* HMAC-SHA-256 functions */
+
+void hmac_sha256_init(hmac_sha256_ctx *ctx, const unsigned char *key,
+                      unsigned int key_size)
+{
+    unsigned int fill;
+    unsigned int num;
+
+    const unsigned char *key_used;
+    unsigned char key_temp[SHA256_DIGEST_SIZE];
+    int i;
+
+    if (key_size == SHA256_BLOCK_SIZE) {
+        key_used = key;
+        num = SHA256_BLOCK_SIZE;
+    } else {
+        if (key_size > SHA256_BLOCK_SIZE){
+            num = SHA256_DIGEST_SIZE;
+            sha256(key, key_size, key_temp);
+            key_used = key_temp;
+        } else { /* key_size > SHA256_BLOCK_SIZE */
+            key_used = key;
+            num = key_size;
+        }
+        fill = SHA256_BLOCK_SIZE - num;
+
+        memset(ctx->block_ipad + num, 0x36, fill);
+        memset(ctx->block_opad + num, 0x5c, fill);
+    }
+
+    for (i = 0; i < (int) num; i++) {
+        ctx->block_ipad[i] = key_used[i] ^ 0x36;
+        ctx->block_opad[i] = key_used[i] ^ 0x5c;
+    }
+
+    sha256_init(&ctx->ctx_inside);
+    sha256_update(&ctx->ctx_inside, ctx->block_ipad, SHA256_BLOCK_SIZE);
+
+    sha256_init(&ctx->ctx_outside);
+    sha256_update(&ctx->ctx_outside, ctx->block_opad,
+                  SHA256_BLOCK_SIZE);
+
+    /* for hmac_reinit */
+    memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
+           sizeof(sha256_ctx));
+    memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
+           sizeof(sha256_ctx));
+}
+
+void hmac_sha256_reinit(hmac_sha256_ctx *ctx)
+{
+    memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
+           sizeof(sha256_ctx));
+    memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
+           sizeof(sha256_ctx));
+}
+
+void hmac_sha256_update(hmac_sha256_ctx *ctx, const unsigned char *message,
+                        unsigned int message_len)
+{
+    sha256_update(&ctx->ctx_inside, message, message_len);
+}
+
+void hmac_sha256_final(hmac_sha256_ctx *ctx, unsigned char *mac,
+                       unsigned int mac_size)
+{
+    unsigned char digest_inside[SHA256_DIGEST_SIZE];
+    unsigned char mac_temp[SHA256_DIGEST_SIZE];
+
+    sha256_final(&ctx->ctx_inside, digest_inside);
+    sha256_update(&ctx->ctx_outside, digest_inside, SHA256_DIGEST_SIZE);
+    sha256_final(&ctx->ctx_outside, mac_temp);
+    memcpy(mac, mac_temp, mac_size);
+}
+
+void hmac_sha256(const unsigned char *key, unsigned int key_size,
+          const unsigned char *message, unsigned int message_len,
+          unsigned char *mac, unsigned mac_size)
+{
+    hmac_sha256_ctx ctx;
+
+    hmac_sha256_init(&ctx, key, key_size);
+    hmac_sha256_update(&ctx, message, message_len);
+    hmac_sha256_final(&ctx, mac, mac_size);
+}
+
+/* HMAC-SHA-384 functions */
+
+void hmac_sha384_init(hmac_sha384_ctx *ctx, const unsigned char *key,
+                      unsigned int key_size)
+{
+    unsigned int fill;
+    unsigned int num;
+
+    const unsigned char *key_used;
+    unsigned char key_temp[SHA384_DIGEST_SIZE];
+    int i;
+
+    if (key_size == SHA384_BLOCK_SIZE) {
+        key_used = key;
+        num = SHA384_BLOCK_SIZE;
+    } else {
+        if (key_size > SHA384_BLOCK_SIZE){
+            num = SHA384_DIGEST_SIZE;
+            sha384(key, key_size, key_temp);
+            key_used = key_temp;
+        } else { /* key_size > SHA384_BLOCK_SIZE */
+            key_used = key;
+            num = key_size;
+        }
+        fill = SHA384_BLOCK_SIZE - num;
+
+        memset(ctx->block_ipad + num, 0x36, fill);
+        memset(ctx->block_opad + num, 0x5c, fill);
+    }
+
+    for (i = 0; i < (int) num; i++) {
+        ctx->block_ipad[i] = key_used[i] ^ 0x36;
+        ctx->block_opad[i] = key_used[i] ^ 0x5c;
+    }
+
+    sha384_init(&ctx->ctx_inside);
+    sha384_update(&ctx->ctx_inside, ctx->block_ipad, SHA384_BLOCK_SIZE);
+
+    sha384_init(&ctx->ctx_outside);
+    sha384_update(&ctx->ctx_outside, ctx->block_opad,
+                  SHA384_BLOCK_SIZE);
+
+    /* for hmac_reinit */
+    memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
+           sizeof(sha384_ctx));
+    memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
+           sizeof(sha384_ctx));
+}
+
+void hmac_sha384_reinit(hmac_sha384_ctx *ctx)
+{
+    memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
+           sizeof(sha384_ctx));
+    memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
+           sizeof(sha384_ctx));
+}
+
+void hmac_sha384_update(hmac_sha384_ctx *ctx, const unsigned char *message,
+                        unsigned int message_len)
+{
+    sha384_update(&ctx->ctx_inside, message, message_len);
+}
+
+void hmac_sha384_final(hmac_sha384_ctx *ctx, unsigned char *mac,
+                       unsigned int mac_size)
+{
+    unsigned char digest_inside[SHA384_DIGEST_SIZE];
+    unsigned char mac_temp[SHA384_DIGEST_SIZE];
+
+    sha384_final(&ctx->ctx_inside, digest_inside);
+    sha384_update(&ctx->ctx_outside, digest_inside, SHA384_DIGEST_SIZE);
+    sha384_final(&ctx->ctx_outside, mac_temp);
+    memcpy(mac, mac_temp, mac_size);
+}
+
+void hmac_sha384(const unsigned char *key, unsigned int key_size,
+          const unsigned char *message, unsigned int message_len,
+          unsigned char *mac, unsigned mac_size)
+{
+    hmac_sha384_ctx ctx;
+
+    hmac_sha384_init(&ctx, key, key_size);
+    hmac_sha384_update(&ctx, message, message_len);
+    hmac_sha384_final(&ctx, mac, mac_size);
+}
+
+/* HMAC-SHA-512 functions */
+
+void hmac_sha512_init(hmac_sha512_ctx *ctx, const unsigned char *key,
+                      unsigned int key_size)
+{
+    unsigned int fill;
+    unsigned int num;
+
+    const unsigned char *key_used;
+    unsigned char key_temp[SHA512_DIGEST_SIZE];
+    int i;
+
+    if (key_size == SHA512_BLOCK_SIZE) {
+        key_used = key;
+        num = SHA512_BLOCK_SIZE;
+    } else {
+        if (key_size > SHA512_BLOCK_SIZE){
+            num = SHA512_DIGEST_SIZE;
+            sha512(key, key_size, key_temp);
+            key_used = key_temp;
+        } else { /* key_size > SHA512_BLOCK_SIZE */
+            key_used = key;
+            num = key_size;
+        }
+        fill = SHA512_BLOCK_SIZE - num;
+
+        memset(ctx->block_ipad + num, 0x36, fill);
+        memset(ctx->block_opad + num, 0x5c, fill);
+    }
+
+    for (i = 0; i < (int) num; i++) {
+        ctx->block_ipad[i] = key_used[i] ^ 0x36;
+        ctx->block_opad[i] = key_used[i] ^ 0x5c;
+    }
+
+    sha512_init(&ctx->ctx_inside);
+    sha512_update(&ctx->ctx_inside, ctx->block_ipad, SHA512_BLOCK_SIZE);
+
+    sha512_init(&ctx->ctx_outside);
+    sha512_update(&ctx->ctx_outside, ctx->block_opad,
+                  SHA512_BLOCK_SIZE);
+
+    /* for hmac_reinit */
+    memcpy(&ctx->ctx_inside_reinit, &ctx->ctx_inside,
+           sizeof(sha512_ctx));
+    memcpy(&ctx->ctx_outside_reinit, &ctx->ctx_outside,
+           sizeof(sha512_ctx));
+}
+
+void hmac_sha512_reinit(hmac_sha512_ctx *ctx)
+{
+    memcpy(&ctx->ctx_inside, &ctx->ctx_inside_reinit,
+           sizeof(sha512_ctx));
+    memcpy(&ctx->ctx_outside, &ctx->ctx_outside_reinit,
+           sizeof(sha512_ctx));
+}
+
+void hmac_sha512_update(hmac_sha512_ctx *ctx, const unsigned char *message,
+                        unsigned int message_len)
+{
+    sha512_update(&ctx->ctx_inside, message, message_len);
+}
+
+void hmac_sha512_final(hmac_sha512_ctx *ctx, unsigned char *mac,
+                       unsigned int mac_size)
+{
+    unsigned char digest_inside[SHA512_DIGEST_SIZE];
+    unsigned char mac_temp[SHA512_DIGEST_SIZE];
+
+    sha512_final(&ctx->ctx_inside, digest_inside);
+    sha512_update(&ctx->ctx_outside, digest_inside, SHA512_DIGEST_SIZE);
+    sha512_final(&ctx->ctx_outside, mac_temp);
+    memcpy(mac, mac_temp, mac_size);
+}
+
+void hmac_sha512(const unsigned char *key, unsigned int key_size,
+          const unsigned char *message, unsigned int message_len,
+          unsigned char *mac, unsigned mac_size)
+{
+    hmac_sha512_ctx ctx;
+
+    hmac_sha512_init(&ctx, key, key_size);
+    hmac_sha512_update(&ctx, message, message_len);
+    hmac_sha512_final(&ctx, mac, mac_size);
+}
+
+#ifdef TEST_VECTORS
+
+/* IETF Validation tests */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void test(const char *vector, unsigned char *digest,
+          unsigned int digest_size)
+{
+    char output[2 * SHA512_DIGEST_SIZE + 1];
+    int i;
+
+    output[2 * digest_size] = '\0';
+
+    for (i = 0; i < (int) digest_size ; i++) {
+       sprintf(output + 2*i, "%02x", digest[i]);
+    }
+
+    printf("H: %s\n", output);
+    if (strcmp(vector, output)) {
+        fprintf(stderr, "Test failed.\n");
+        exit(1);
+    }
+}
+
+int main(void)
+{
+    static const char *vectors[] =
+    {
+        /* HMAC-SHA-224 */
+        "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
+        "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44",
+        "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea",
+        "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a",
+        "0e2aea68a90c8d37c988bcdb9fca6fa8",
+        "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e",
+        "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1",
+        /* HMAC-SHA-256 */
+        "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
+        "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
+        "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
+        "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
+        "a3b6167473100ee06e0c796c2955552b",
+        "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
+        "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2",
+        /* HMAC-SHA-384 */
+        "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59c"
+        "faea9ea9076ede7f4af152e8b2fa9cb6",
+        "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e"
+        "8e2240ca5e69e2c78b3239ecfab21649",
+        "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b"
+        "2a5ab39dc13814b94e3ab6e101a34f27",
+        "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e"
+        "6801dd23c4a7d679ccf8a386c674cffb",
+        "3abf34c3503b2a23a46efc619baef897",
+        "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c6"
+        "0c2ef6ab4030fe8296248df163f44952",
+        "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5"
+        "a678cc31e799176d3860e6110c46523e",
+        /* HMAC-SHA-512 */
+        "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde"
+        "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
+        "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554"
+        "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
+        "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39"
+        "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
+        "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db"
+        "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
+        "415fad6271580a531d4179bc891d87a6",
+        "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352"
+        "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
+        "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944"
+        "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58"
+    };
+
+    static char *messages[] =
+    {
+        "Hi There",
+        "what do ya want for nothing?",
+        NULL,
+        NULL,
+        "Test With Truncation",
+        "Test Using Larger Than Block-Size Key - Hash Key First",
+        "This is a test using a larger than block-size key "
+        "and a larger than block-size data. The key needs"
+        " to be hashed before being used by the HMAC algorithm."
+    };
+
+    unsigned char mac[SHA512_DIGEST_SIZE];
+    unsigned char *keys[7];
+    unsigned int keys_len[7] = {20, 4, 20, 25, 20, 131, 131};
+    unsigned int messages2and3_len = 50;
+    unsigned int mac_224_size, mac_256_size, mac_384_size, mac_512_size;
+    int i;
+
+    for (i = 0; i < 7; i++) {
+        keys[i] = malloc(keys_len[i]);
+        if (keys[i] == NULL) {
+            fprintf(stderr, "Can't allocate memory\n");
+            return 1;
+        }
+    }
+
+    memset(keys[0], 0x0b, keys_len[0]);
+    strcpy((char *) keys[1], "Jefe");
+    memset(keys[2], 0xaa, keys_len[2]);
+    for (i = 0; i < (int) keys_len[3]; i++)
+        keys[3][i] = (unsigned char) i + 1;
+    memset(keys[4], 0x0c, keys_len[4]);
+    memset(keys[5], 0xaa, keys_len[5]);
+    memset(keys[6], 0xaa, keys_len[6]);
+
+    messages[2] = malloc(messages2and3_len + 1);
+    messages[3] = malloc(messages2and3_len + 1);
+
+    if (messages[2] == NULL || messages[3] == NULL) {
+        fprintf(stderr, "Can't allocate memory\n");
+        return 1;
+    }
+
+    messages[2][messages2and3_len] = '\0';
+    messages[3][messages2and3_len] = '\0';
+
+    memset(messages[2], 0xdd, messages2and3_len);
+    memset(messages[3], 0xcd, messages2and3_len);
+
+    printf("HMAC-SHA-2 IETF Validation tests\n\n");
+
+    for (i = 0; i < 7; i++) {
+        if (i != 4) {
+            mac_224_size = SHA224_DIGEST_SIZE;
+            mac_256_size = SHA256_DIGEST_SIZE;
+            mac_384_size = SHA384_DIGEST_SIZE;
+            mac_512_size = SHA512_DIGEST_SIZE;
+        } else {
+            mac_224_size = 128 / 8; mac_256_size = 128 / 8;
+            mac_384_size = 128 / 8; mac_512_size = 128 / 8;
+        }
+
+        printf("Test %d:\n", i + 1);
+
+        hmac_sha224(keys[i], keys_len[i], (unsigned char *) messages[i],
+                    strlen(messages[i]), mac, mac_224_size);
+        test(vectors[i], mac, mac_224_size);
+        hmac_sha256(keys[i], keys_len[i], (unsigned char *) messages[i],
+                    strlen(messages[i]), mac, mac_256_size);
+        test(vectors[7 + i], mac, mac_256_size);
+        hmac_sha384(keys[i], keys_len[i], (unsigned char *) messages[i],
+                    strlen(messages[i]), mac, mac_384_size);
+        test(vectors[14 + i], mac, mac_384_size);
+        hmac_sha512(keys[i], keys_len[i], (unsigned char *) messages[i],
+                    strlen(messages[i]), mac, mac_512_size);
+        test(vectors[21 + i], mac, mac_512_size);
+    }
+
+    printf("All tests passed.\n");
+
+    return 0;
+}
+
+#endif /* TEST_VECTORS */
+
3rdparty/hmac_sha/hmac_sha2.h new file +140 -0

140 lines not on this page · raw message

3rdparty/hmac_sha/sha2.c new file +949 -0

949 lines not on this page · raw message

3rdparty/hmac_sha/sha2.h new file +108 -0

108 lines not on this page · raw message

Makefile +6 -2
index ae846e2..33d988d 100644
@@ -1,7 +1,11 @@
 CC ?= gcc
 AM_CFLAGS = -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2
 CFLAGS ?= -g -O2
-objects = mmc.o mmc_cmds.o
+objects = \
+	mmc.o \
+	mmc_cmds.o \
+	3rdparty/hmac_sha/hmac_sha2.o \
+	3rdparty/hmac_sha/sha2.o
 
 CHECKFLAGS = -Wall -Werror -Wuninitialized -Wundef
 
@@ -28,7 +32,7 @@ all: $(progs) manpages
 ifdef C
 	$(check) $<
 endif
-	$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) -c $<
+	$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
 
 mmc: $(objects)
 	$(CC) $(CFLAGS) -o $@ $(objects) $(LDFLAGS) $(LIBS)
Signature · 1 line
2.0.0
author
#2 lore raw

PATCH 2/2 mmc-utils: RPMB: add support for 4 rpmb operations

In reply to Roman Pen

mmc rpmb write-key <rpmb device> <key file>
Program authentication key which is 32 bytes length and stored in the specified file. Also you can specify '-' instead of key file path and utility will read the key from stdin.
BEWARE: key can be programmed only once!
Example:

   $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -

mmc rpmb read-counter <rpmb device>
Counter value for the <rpmb device> will be read to stdout.

mmc rpmb read-block <rpmb device> <address> <output file>
Block of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.

mmc rpmb write-block <rpmb device> <address> <key file> <256 byte data file>
Block of 256 bytes will be written from data file to <rpmb device>.
Also you can specify '-' instead of key file path or data file and utility will read the data from stdin. Example:

   $ (echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH && awk 'BEGIN {while (c++<256) printf "a"}') | \
     mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -
---
 mmc.c      |  28 +++++
 mmc.h      |   6 +
 mmc_cmds.c | 419 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |   4 +
 4 files changed, 457 insertions(+)
mmc.c +28 -0
index 926e92f..af2799b 100644
@@ -110,6 +110,34 @@ static struct Command commands[] = {
 		"Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
 	  NULL
 	},
+	{ do_rpmb_write_key, -1,
+	  "rpmb write-key", "<rpmb device> <key file>\n"
+		  "Program authentication key which is 32 bytes length and stored in the specified file.\n"
+		  "Also you can specify '-' instead of key file path and utility will read the key from stdin.\n"
+		  "BEWARE: key can be programmed only once!\n"
+		  "Example:\n"
+		  "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -",
+	  NULL
+	},
+	{ do_rpmb_read_counter, -1,
+	  "rpmb read-counter", "<rpmb device>\n"
+		  "Counter value for the <rpmb device> will be read to stdout.",
+	  NULL
+	},
+	{ do_rpmb_read_block, -1,
+	  "rpmb read-block", "<rpmb device> <address> <output file>\n"
+		  "Block of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.",
+	  NULL
+	},
+	{ do_rpmb_write_block, -1,
+	  "rpmb write-block", "<rpmb device> <address> <key file> <256 byte data file>\n"
+		  "Block of 256 bytes will be written from data file to <rpmb device>.\n"
+		  "Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.\n"
+		  "Example:\n"
+		  "  $ (echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH && awk 'BEGIN {while (c++<256) printf \"a\"}') | \\\n"
+		  "    mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
+	  NULL
+	},
 	{ 0, 0, 0, 0 }
 };
 
mmc.h +6 -0
index 9871d62..5fe5fec 100644
@@ -20,6 +20,10 @@
 
 #define CHECK(expr, msg, err_stmt) { if (expr) { fprintf(stderr, msg); err_stmt; } }
 
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
 /* From kernel linux/major.h */
 #define MMC_BLOCK_MAJOR			179
 
@@ -29,6 +33,8 @@
 #define MMC_SEND_STATUS		13	/* ac   [31:16] RCA        R1  */
 #define R1_SWITCH_ERROR   (1 << 7)  /* sx, c */
 #define MMC_SWITCH_MODE_WRITE_BYTE	0x03	/* Set target to value */
+#define MMC_READ_MULTIPLE_BLOCK  18   /* adtc [31:0] data addr   R1  */
+#define MMC_WRITE_MULTIPLE_BLOCK 25   /* adtc                    R1  */
 
 /*
  * EXT_CSD fields
mmc_cmds.c +419 -0

431 lines not on this page · raw message

mmc_cmds.h +4 -0
index f06cc10..9e625c9 100644
@@ -28,3 +28,7 @@ int do_sanitize(int nargs, char **argv);
 int do_status_get(int nargs, char **argv);
 int do_enh_area_set(int nargs, char **argv);
 int do_write_reliability_set(int nargs, char **argv);
+int do_rpmb_write_key(int nargs, char **argv);
+int do_rpmb_read_counter(int nargs, char **argv);
+int do_rpmb_read_block(int nargs, char **argv);
+int do_rpmb_write_block(int nargs, char **argv);
Signature · 1 line
2.0.0
author
#3 lore raw

PATCH v2 2/2 mmc-utils: RPMB: add support for 4 rpmb operations

mmc rpmb write-key <rpmb device> <key file>
Program authentication key which is 32 bytes length and stored in the specified file. Also you can specify '-' instead of key file path and utility will read the key from stdin.
BEWARE: key can be programmed only once!
Example:

    $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -

mmc rpmb read-counter <rpmb device>
Counter value for the <rpmb device> will be read to stdout.

mmc rpmb read-block <rpmb device> <address> <blocks count> <output file> [key file]
Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path. If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin. Example:

    $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \
        mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -
  or read the block without verification
    $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block

mmc rpmb write-block <rpmb device> <address> <256 byte data file> <key file>
Block of 256 bytes will be written from data file to <rpmb device>.
Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.
Example:

    $ (awk 'BEGIN {while (c++<256) printf "a"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \
      mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -
Cc Chris Ball <[email protected]>
Cc Ulf Hansson <[email protected]>,
Cc Ben Gardiner <[email protected]>,
Cc linux-mmc <[email protected]>
---
 mmc.c      |  33 ++++
 mmc.h      |   6 +
 mmc_cmds.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |   4 +
 4 files changed, 557 insertions(+)
mmc.c +33 -0
index 926e92f..37838e6 100644
@@ -110,6 +110,39 @@ static struct Command commands[] = {
 		"Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
 	  NULL
 	},
+	{ do_rpmb_write_key, -1,
+	  "rpmb write-key", "<rpmb device> <key file>\n"
+		  "Program authentication key which is 32 bytes length and stored in the specified file.\n"
+		  "Also you can specify '-' instead of key file path and utility will read the key from stdin.\n"
+		  "BEWARE: key can be programmed only once!\n"
+		  "Example:\n"
+		  "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -",
+	  NULL
+	},
+	{ do_rpmb_read_counter, -1,
+	  "rpmb read-counter", "<rpmb device>\n"
+		  "Counter value for the <rpmb device> will be read to stdout.",
+	  NULL
+	},
+	{ do_rpmb_read_block, -1,
+	  "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
+		  "Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.\n"
+		  "If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin.\n"
+		  "Example:\n"
+		  "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
+		  "or read two blocks without verification\n"
+		  "  $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
+	  NULL
+	},
+	{ do_rpmb_write_block, -1,
+	  "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
+		  "Block of 256 bytes will be written from data file to <rpmb device>.\n"
+		  "Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.\n"
+		  "Example:\n"
+		  "  $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
+		  "    mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
+	  NULL
+	},
 	{ 0, 0, 0, 0 }
 };
 
mmc.h +6 -0
index 9871d62..5fe5fec 100644
@@ -20,6 +20,10 @@
 
 #define CHECK(expr, msg, err_stmt) { if (expr) { fprintf(stderr, msg); err_stmt; } }
 
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
 /* From kernel linux/major.h */
 #define MMC_BLOCK_MAJOR			179
 
@@ -29,6 +33,8 @@
 #define MMC_SEND_STATUS		13	/* ac   [31:16] RCA        R1  */
 #define R1_SWITCH_ERROR   (1 << 7)  /* sx, c */
 #define MMC_SWITCH_MODE_WRITE_BYTE	0x03	/* Set target to value */
+#define MMC_READ_MULTIPLE_BLOCK  18   /* adtc [31:0] data addr   R1  */
+#define MMC_WRITE_MULTIPLE_BLOCK 25   /* adtc                    R1  */
 
 /*
  * EXT_CSD fields
mmc_cmds.c +514 -0

526 lines not on this page · raw message

mmc_cmds.h +4 -0
index f06cc10..9e625c9 100644
@@ -28,3 +28,7 @@ int do_sanitize(int nargs, char **argv);
 int do_status_get(int nargs, char **argv);
 int do_enh_area_set(int nargs, char **argv);
 int do_write_reliability_set(int nargs, char **argv);
+int do_rpmb_write_key(int nargs, char **argv);
+int do_rpmb_read_counter(int nargs, char **argv);
+int do_rpmb_read_block(int nargs, char **argv);
+int do_rpmb_write_block(int nargs, char **argv);
Signature · 1 line
2.0.0
#4 lore raw

Re: [PATCH v2 2/2] mmc-utils: RPMB: add support for 4 rpmb operations

Diff between v1..v2:

1. read block op supports multiple blocks read and data verification if key is specified
2. the order of the params for read/write op was changed: key now is the last and is not mandatory for reading.

--
Roman

Quoted 655 lines · Roman Pen

mmc rpmb write-key <rpmb device> <key file>
Program authentication key which is 32 bytes length and stored in the specified file. Also you can specify '-' instead of key file path and utility will read the key from stdin.
BEWARE: key can be programmed only once!
Example:

    $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -

mmc rpmb read-counter <rpmb device>
Counter value for the <rpmb device> will be read to stdout.

mmc rpmb read-block <rpmb device> <address> <blocks count> <output file> [key file]
Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path. If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin. Example:

    $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \
        mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -
  or read the block without verification
    $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block

mmc rpmb write-block <rpmb device> <address> <256 byte data file> <key file>
Block of 256 bytes will be written from data file to <rpmb device>.
Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.
Example:

    $ (awk 'BEGIN {while (c++<256) printf "a"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \
      mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -
Cc Chris Ball <[email protected]>
Cc Ulf Hansson <[email protected]>,
Cc Ben Gardiner <[email protected]>,
Cc linux-mmc <[email protected]>
---
 mmc.c      |  33 ++++
 mmc.h      |   6 +
 mmc_cmds.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mmc_cmds.h |   4 +
 4 files changed, 557 insertions(+)
mmc.c +33 -0
index 926e92f..37838e6 100644
@@ -110,6 +110,39 @@ static struct Command commands[] = {
                "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.",
          NULL
        },
+       { do_rpmb_write_key, -1,
+         "rpmb write-key", "<rpmb device> <key file>\n"
+                 "Program authentication key which is 32 bytes length and stored in the specified file.\n"
+                 "Also you can specify '-' instead of key file path and utility will read the key from stdin.\n"
+                 "BEWARE: key can be programmed only once!\n"
+                 "Example:\n"
+                 "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb write-key /dev/mmcblk0rpmb -",
+         NULL
+       },
+       { do_rpmb_read_counter, -1,
+         "rpmb read-counter", "<rpmb device>\n"
+                 "Counter value for the <rpmb device> will be read to stdout.",
+         NULL
+       },
+       { do_rpmb_read_block, -1,
+         "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n"
+                 "Blocks of 256 bytes will be read from <rpmb device> to output file or stdout if '-' is specified instead of regular path.\n"
+                 "If key is specified - read data will be verified. Instead of regular path you can specify '-' and key will be read from stdin.\n"
+                 "Example:\n"
+                 "  $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n"
+                 "or read two blocks without verification\n"
+                 "  $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block",
+         NULL
+       },
+       { do_rpmb_write_block, -1,
+         "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n"
+                 "Block of 256 bytes will be written from data file to <rpmb device>.\n"
+                 "Also you can specify '-' instead of key file path or data file and utility will read the data from stdin.\n"
+                 "Example:\n"
+                 "  $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n"
+                 "    mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -",
+         NULL
+       },
        { 0, 0, 0, 0 }
 };
 
mmc.h +6 -0
index 9871d62..5fe5fec 100644
@@ -20,6 +20,10 @@
 
 #define CHECK(expr, msg, err_stmt) { if (expr) { fprintf(stderr, msg); err_stmt; } }
 
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
 /* From kernel linux/major.h */
 #define MMC_BLOCK_MAJOR                        179
 
@@ -29,6 +33,8 @@
 #define MMC_SEND_STATUS                13      /* ac   [31:16] RCA        R1  */
 #define R1_SWITCH_ERROR   (1 << 7)  /* sx, c */
 #define MMC_SWITCH_MODE_WRITE_BYTE     0x03    /* Set target to value */
+#define MMC_READ_MULTIPLE_BLOCK  18   /* adtc [31:0] data addr   R1  */
+#define MMC_WRITE_MULTIPLE_BLOCK 25   /* adtc                    R1  */
 
 /*
  * EXT_CSD fields
mmc_cmds.c +514 -0
index b8afa74..cea943f 100644
@@ -26,9 +26,13 @@
 #include <libgen.h>
 #include <limits.h>
 #include <ctype.h>
+#include <errno.h>
+#include <stdint.h>
+#include <assert.h>
 
 #include "mmc.h"
 #include "mmc_cmds.h"
+#include "3rdparty/hmac_sha/hmac_sha2.h"
 
 int read_extcsd(int fd, __u8 *ext_csd)
 {
@@ -1163,3 +1167,513 @@ int do_sanitize(int nargs, char **argv)
 
 }
 
+#define DO_IO(func, fd, buf, nbyte)                                    \
+       ({                                                                                              \
+               ssize_t ret = 0, r;                                                     \
+               do {                                                                            \
+                       r = func(fd, buf + ret, nbyte - ret);   \
+                       if (r < 0 && errno != EINTR) {                  \
+                               ret = -1;                                                       \
+                               break;                                                          \
+                       }                                                                               \
+                       else if (r > 0)                                                 \
+                               ret += r;                                                       \
+               } while (r != 0 && (size_t)ret != nbyte);       \
+                                                                                                       \
+               ret;                                                                            \
+       })
+
+enum rpmb_op_type {
+       MMC_RPMB_WRITE_KEY = 0x01,
+       MMC_RPMB_READ_CNT  = 0x02,
+       MMC_RPMB_WRITE     = 0x03,
+       MMC_RPMB_READ      = 0x04,
+
+       /* For internal usage only, do not use it directly */
+       MMC_RPMB_READ_RESP = 0x05
+};
+
+struct rpmb_frame {
+       u_int8_t  stuff[196];
+       u_int8_t  key_mac[32];
+       u_int8_t  data[256];
+       u_int8_t  nonce[16];
+       u_int32_t write_counter;
+       u_int16_t addr;
+       u_int16_t block_count;
+       u_int16_t result;
+       u_int16_t req_resp;
+};
+
+/* Performs RPMB operation.
+ *
+ * @fd: RPMB device on which we should perform ioctl command
+ * @frame_in: input RPMB frame, should be properly inited
+ * @frame_out: output (result) RPMB frame. Caller is responsible for checking
+ *             result and req_resp for output frame.
+ * @out_cnt: count of outer frames. Used only for multiple blocks reading,
+ *           in the other cases -EINVAL will be returned.
+ */
+static int do_rpmb_op(int fd,
+                                         const struct rpmb_frame *frame_in,
+                                         struct rpmb_frame *frame_out,
+                                         unsigned int out_cnt)
+{
+       int err;
+       u_int16_t rpmb_type;
+
+       struct mmc_ioc_cmd ioc = {
+               .arg        = 0x0,
+               .blksz      = 512,
+               .blocks     = 1,
+               .write_flag = 1,
+               .opcode     = MMC_WRITE_MULTIPLE_BLOCK,
+               .flags      = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC,
+               .data_ptr   = (uintptr_t)frame_in
+       };
+
+       if (!frame_in || !frame_out || !out_cnt)
+               return -EINVAL;
+
+       rpmb_type = be16toh(frame_in->req_resp);
+
+       switch(rpmb_type) {
+       case MMC_RPMB_WRITE:
+       case MMC_RPMB_WRITE_KEY:
+               if (out_cnt != 1) {
+                       err = -EINVAL;
+                       goto out;
+               }
+
+               /* Write request */
+               ioc.write_flag |= (1<<31);
+               err = ioctl(fd, MMC_IOC_CMD, &ioc);
+               if (err < 0) {
+                       err = -errno;
+                       goto out;
+               }
+
+               /* Result request */
+               memset(frame_out, 0, sizeof(*frame_out));
+               frame_out->req_resp = htobe16(MMC_RPMB_READ_RESP);
+               ioc.write_flag = 1;
+               ioc.data_ptr = (uintptr_t)frame_out;
+               err = ioctl(fd, MMC_IOC_CMD, &ioc);
+               if (err < 0) {
+                       err = -errno;
+                       goto out;
+               }
+
+               /* Get response */
+               ioc.write_flag = 0;
+               ioc.opcode = MMC_READ_MULTIPLE_BLOCK;
+               err = ioctl(fd, MMC_IOC_CMD, &ioc);
+               if (err < 0) {
+                       err = -errno;
+                       goto out;
+               }
+
+               break;
+       case MMC_RPMB_READ_CNT:
+               if (out_cnt != 1) {
+                       err = -EINVAL;
+                       goto out;
+               }
+               /* fall through */
+
+       case MMC_RPMB_READ:
+               /* Request */
+               err = ioctl(fd, MMC_IOC_CMD, &ioc);
+               if (err < 0) {
+                       err = -errno;
+                       goto out;
+               }
+
+               /* Get response */
+               ioc.write_flag = 0;
+               ioc.opcode   = MMC_READ_MULTIPLE_BLOCK;
+               ioc.blocks   = out_cnt;
+               ioc.data_ptr = (uintptr_t)frame_out;
+               err = ioctl(fd, MMC_IOC_CMD, &ioc);
+               if (err < 0) {
+                       err = -errno;
+                       goto out;
+               }
+
+               break;
+       default:
+               err = -EINVAL;
+               goto out;
+       }
+
+out:
+       return err;
+}
+
+int do_rpmb_write_key(int nargs, char **argv)
+{
+       int ret, dev_fd, key_fd;
+       struct rpmb_frame frame_in = {
+               .req_resp = htobe16(MMC_RPMB_WRITE_KEY)
+       }, frame_out;
+
+       CHECK(nargs != 3, "Usage: mmc rpmb write-key </path/to/mmcblkXrpmb> </path/to/key>\n",
+                       exit(1));
+
+       dev_fd = open(argv[1], O_RDWR);
+       if (dev_fd < 0) {
+               perror("device open");
+               exit(1);
+       }
+
+       if (0 == strcmp(argv[2], "-"))
+               key_fd = STDIN_FILENO;
+       else {
+               key_fd = open(argv[2], O_RDONLY);
+               if (key_fd < 0) {
+                       perror("can't open key file");
+                       exit(1);
+               }
+       }
+
+       /* Read the auth key */
+       ret = DO_IO(read, key_fd, frame_in.key_mac, sizeof(frame_in.key_mac));
+       if (ret < 0) {
+               perror("read the key");
+               exit(1);
+       } else if (ret != sizeof(frame_in.key_mac)) {
+               printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
+                          (unsigned long)sizeof(frame_in.key_mac),
+                          ret);
+               exit(1);
+       }
+
+       /* Execute RPMB op */
+       ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
+       if (ret != 0) {
+               perror("RPMB ioctl failed");
+               exit(1);
+       }
+
+       /* Check RPMB response */
+       if (frame_out.result != 0) {
+               printf("RPMB operation failed, retcode 0x%04x\n",
+                          be16toh(frame_out.result));
+               exit(1);
+       }
+
+       close(dev_fd);
+       if (key_fd != STDIN_FILENO)
+               close(key_fd);
+
+       return ret;
+}
+
+int rpmb_read_counter(int dev_fd, unsigned int *cnt)
+{
+       int ret;
+       struct rpmb_frame frame_in = {
+               .req_resp = htobe16(MMC_RPMB_READ_CNT)
+       }, frame_out;
+
+       /* Execute RPMB op */
+       ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
+       if (ret != 0) {
+               perror("RPMB ioctl failed");
+               exit(1);
+       }
+
+       /* Check RPMB response */
+       if (frame_out.result != 0)
+               return be16toh(frame_out.result);
+
+       *cnt = be32toh(frame_out.write_counter);
+
+       return 0;
+}
+
+int do_rpmb_read_counter(int nargs, char **argv)
+{
+       int ret, dev_fd;
+       unsigned int cnt;
+
+       CHECK(nargs != 2, "Usage: mmc rpmb read-counter </path/to/mmcblkXrpmb>\n",
+                       exit(1));
+
+       dev_fd = open(argv[1], O_RDWR);
+       if (dev_fd < 0) {
+               perror("device open");
+               exit(1);
+       }
+
+       ret = rpmb_read_counter(dev_fd, &cnt);
+
+       /* Check RPMB response */
+       if (ret != 0) {
+               printf("RPMB operation failed, retcode 0x%04x\n", ret);
+               exit(1);
+       }
+
+       close(dev_fd);
+
+       printf("Counter value: 0x%08x\n", cnt);
+
+       return ret;
+}
+
+int do_rpmb_read_block(int nargs, char **argv)
+{
+       int i, ret, dev_fd, data_fd, key_fd = -1;
+       uint16_t addr, blocks_cnt;
+       unsigned char key[32];
+       struct rpmb_frame frame_in = {
+               .req_resp    = htobe16(MMC_RPMB_READ),
+       }, *frame_out_p;
+
+       CHECK(nargs != 5 && nargs != 6, "Usage: mmc rpmb read-block </path/to/mmcblkXrpmb> <address> <blocks count> </path/to/output_file> [/path/to/key]\n",
+                       exit(1));
+
+       dev_fd = open(argv[1], O_RDWR);
+       if (dev_fd < 0) {
+               perror("device open");
+               exit(1);
+       }
+
+       /* Get block address */
+       errno = 0;
+       addr = strtol(argv[2], NULL, 0);
+       if (errno) {
+               perror("incorrect address");
+               exit(1);
+       }
+       frame_in.addr = htobe16(addr);
+
+       /* Get blocks count */
+       errno = 0;
+       blocks_cnt = strtol(argv[3], NULL, 0);
+       if (errno) {
+               perror("incorrect blocks count");
+               exit(1);
+       }
+
+       if (!blocks_cnt) {
+               printf("please, specify valid blocks count number\n");
+               exit(1);
+       }
+
+       frame_out_p = calloc(sizeof(*frame_out_p), blocks_cnt);
+       if (!frame_out_p) {
+               printf("can't allocate memory for RPMB outer frames\n");
+               exit(1);
+       }
+
+       /* Write 256b data */
+       if (0 == strcmp(argv[4], "-"))
+               data_fd = STDOUT_FILENO;
+       else {
+               data_fd = open(argv[4], O_WRONLY | O_CREAT | O_APPEND,
+                                          S_IRUSR | S_IWUSR);
+               if (data_fd < 0) {
+                       perror("can't open output file");
+                       exit(1);
+               }
+       }
+
+       /* Key is specified */
+       if (nargs == 6) {
+               if (0 == strcmp(argv[5], "-"))
+                       key_fd = STDIN_FILENO;
+               else {
+                       key_fd = open(argv[5], O_RDONLY);
+                       if (key_fd < 0) {
+                               perror("can't open input key file");
+                               exit(1);
+                       }
+               }
+
+               ret = DO_IO(read, key_fd, key, sizeof(key));
+               if (ret < 0) {
+                       perror("read the key data");
+                       exit(1);
+               } else if (ret != sizeof(key)) {
+                       printf("Data must be %lu bytes length, but we read only %d, exit\n",
+                                  (unsigned long)sizeof(key),
+                                  ret);
+                       exit(1);
+               }
+       }
+
+       /* Execute RPMB op */
+       ret = do_rpmb_op(dev_fd, &frame_in, frame_out_p, blocks_cnt);
+       if (ret != 0) {
+               perror("RPMB ioctl failed");
+               exit(1);
+       }
+
+       /* Check RPMB response */
+       if (frame_out_p[blocks_cnt - 1].result != 0) {
+               printf("RPMB operation failed, retcode 0x%04x\n",
+                          be16toh(frame_out_p[blocks_cnt - 1].result));
+               exit(1);
+       }
+
+       /* Do we have to verify data against key? */
+       if (nargs == 6) {
+               unsigned char mac[32];
+               hmac_sha256_ctx ctx;
+               struct rpmb_frame *frame_out = NULL;
+
+               hmac_sha256_init(&ctx, key, sizeof(key));
+               for (i = 0; i < blocks_cnt; i++) {
+                       frame_out = &frame_out_p[i];
+                       hmac_sha256_update(&ctx, frame_out->data,
+                                                          sizeof(*frame_out) -
+                                                                  offsetof(struct rpmb_frame, data));
+               }
+
+               hmac_sha256_final(&ctx, mac, sizeof(mac));
+
+               /* Impossible */
+               assert(frame_out);
+
+               /* Compare calculated MAC and MAC from last frame */
+               if (memcmp(mac, frame_out->key_mac, sizeof(mac))) {
+                       printf("RPMB MAC missmatch\n");
+                       exit(1);
+               }
+       }
+
+       /* Write data */
+       for (i = 0; i < blocks_cnt; i++) {
+               struct rpmb_frame *frame_out = &frame_out_p[i];
+               ret = DO_IO(write, data_fd, frame_out->data, sizeof(frame_out->data));
+               if (ret < 0) {
+                       perror("write the data");
+                       exit(1);
+               } else if (ret != sizeof(frame_out->data)) {
+                       printf("Data must be %lu bytes length, but we wrote only %d, exit\n",
+                                  (unsigned long)sizeof(frame_out->data),
+                                  ret);
+                       exit(1);
+               }
+       }
+
+       free(frame_out_p);
+       close(dev_fd);
+       if (data_fd != STDOUT_FILENO)
+               close(data_fd);
+       if (key_fd != -1 && key_fd != STDIN_FILENO)
+               close(key_fd);
+
+       return ret;
+}
+
+int do_rpmb_write_block(int nargs, char **argv)
+{
+       int ret, dev_fd, key_fd, data_fd;
+       unsigned char key[32];
+       uint16_t addr;
+       unsigned int cnt;
+       struct rpmb_frame frame_in = {
+               .req_resp    = htobe16(MMC_RPMB_WRITE),
+               .block_count = htobe16(1)
+       }, frame_out;
+
+       CHECK(nargs != 5, "Usage: mmc rpmb write-block </path/to/mmcblkXrpmb> <address> </path/to/input_file> </path/to/key>\n",
+                       exit(1));
+
+       dev_fd = open(argv[1], O_RDWR);
+       if (dev_fd < 0) {
+               perror("device open");
+               exit(1);
+       }
+
+       ret = rpmb_read_counter(dev_fd, &cnt);
+       /* Check RPMB response */
+       if (ret != 0) {
+               printf("RPMB read counter operation failed, retcode 0x%04x\n", ret);
+               exit(1);
+       }
+       frame_in.write_counter = htobe32(cnt);
+
+       /* Get block address */
+       errno = 0;
+       addr = strtol(argv[2], NULL, 0);
+       if (errno) {
+               perror("incorrect address");
+               exit(1);
+       }
+       frame_in.addr = htobe16(addr);
+
+       /* Read 256b data */
+       if (0 == strcmp(argv[3], "-"))
+               data_fd = STDIN_FILENO;
+       else {
+               data_fd = open(argv[3], O_RDONLY);
+               if (data_fd < 0) {
+                       perror("can't open input file");
+                       exit(1);
+               }
+       }
+
+       ret = DO_IO(read, data_fd, frame_in.data, sizeof(frame_in.data));
+       if (ret < 0) {
+               perror("read the data");
+               exit(1);
+       } else if (ret != sizeof(frame_in.data)) {
+               printf("Data must be %lu bytes length, but we read only %d, exit\n",
+                          (unsigned long)sizeof(frame_in.data),
+                          ret);
+               exit(1);
+       }
+
+       /* Read the auth key */
+       if (0 == strcmp(argv[4], "-"))
+               key_fd = STDIN_FILENO;
+       else {
+               key_fd = open(argv[4], O_RDONLY);
+               if (key_fd < 0) {
+                       perror("can't open key file");
+                       exit(1);
+               }
+       }
+
+       ret = DO_IO(read, key_fd, key, sizeof(key));
+       if (ret < 0) {
+               perror("read the key");
+               exit(1);
+       } else if (ret != sizeof(key)) {
+               printf("Auth key must be %lu bytes length, but we read only %d, exit\n",
+                          (unsigned long)sizeof(key),
+                          ret);
+               exit(1);
+       }
+
+       /* Calculate HMAC SHA256 */
+       hmac_sha256(
+               key, sizeof(key),
+               frame_in.data, sizeof(frame_in) - offsetof(struct rpmb_frame, data),
+               frame_in.key_mac, sizeof(frame_in.key_mac));
+
+       /* Execute RPMB op */
+       ret = do_rpmb_op(dev_fd, &frame_in, &frame_out, 1);
+       if (ret != 0) {
+               perror("RPMB ioctl failed");
+               exit(1);
+       }
+
+       /* Check RPMB response */
+       if (frame_out.result != 0) {
+               printf("RPMB operation failed, retcode 0x%04x\n",
+                          be16toh(frame_out.result));
+               exit(1);
+       }
+
+       close(dev_fd);
+       if (data_fd != STDIN_FILENO)
+               close(data_fd);
+       if (key_fd != STDIN_FILENO)
+               close(key_fd);
+
+       return ret;
+}
mmc_cmds.h +4 -1
index f06cc10..9e625c9 100644
@@ -28,3 +28,7 @@ int do_sanitize(int nargs, char **argv);
 int do_status_get(int nargs, char **argv);
 int do_enh_area_set(int nargs, char **argv);
 int do_write_reliability_set(int nargs, char **argv);
+int do_rpmb_write_key(int nargs, char **argv);
+int do_rpmb_read_counter(int nargs, char **argv);
+int do_rpmb_read_block(int nargs, char **argv);
+int do_rpmb_write_block(int nargs, char **argv);
--

2.0.0

#5 lore raw
In reply to Roman Pen
Quoted 31 lines · Roman Pen

This is simple RPMB support for 4 operations:
write-key
read-counter
write-block
read-block

I did not attempt to implement multiple blocks read/write, thus only 1 block (256 b) is supported right now.

Also I had to take HMAC SHA256 implementation as 3rdparty to sign RPMB messages.

Roman Pen (2):
mmc-utils: RPMB: add HMAC SHA256 support
mmc-utils: RPMB: add support for 4 rpmb operations

 3rdparty/hmac_sha/hmac_sha2.c | 544 ++++++++++++++++++++++++
 3rdparty/hmac_sha/hmac_sha2.h | 140 +++++++
 3rdparty/hmac_sha/sha2.c      | 949 ++++++++++++++++++++++++++++++++++++++++++
 3rdparty/hmac_sha/sha2.h      | 108 +++++
 Makefile                      |   8 +-
 mmc.c                         |  28 ++
 mmc.h                         |   6 +
 mmc_cmds.c                    | 419 +++++++++++++++++++
 mmc_cmds.h                    |   4 +
 9 files changed, 2204 insertions(+), 2 deletions(-)
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.c
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.h
 create mode 100644 3rdparty/hmac_sha/sha2.c
 create mode 100644 3rdparty/hmac_sha/sha2.h
Quoted 2 lines

--
2.0.0

This seems like great stuff for mmc-utils. Chris, can you pick them up?

Kind regards
Uffe

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-mmc" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Hi,

Quoted 32 lines · Ulf Hansson
Quoted 31 lines · Roman Pen

This is simple RPMB support for 4 operations:
write-key
read-counter
write-block
read-block

I did not attempt to implement multiple blocks read/write, thus only 1 block (256 b) is supported right now.

Also I had to take HMAC SHA256 implementation as 3rdparty to sign RPMB messages.

Roman Pen (2):
mmc-utils: RPMB: add HMAC SHA256 support
mmc-utils: RPMB: add support for 4 rpmb operations

 3rdparty/hmac_sha/hmac_sha2.c | 544 ++++++++++++++++++++++++
 3rdparty/hmac_sha/hmac_sha2.h | 140 +++++++
 3rdparty/hmac_sha/sha2.c      | 949 ++++++++++++++++++++++++++++++++++++++++++
 3rdparty/hmac_sha/sha2.h      | 108 +++++
 Makefile                      |   8 +-
 mmc.c                         |  28 ++
 mmc.h                         |   6 +
 mmc_cmds.c                    | 419 +++++++++++++++++++
 mmc_cmds.h                    |   4 +
 9 files changed, 2204 insertions(+), 2 deletions(-)
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.c
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.h
 create mode 100644 3rdparty/hmac_sha/sha2.c
 create mode 100644 3rdparty/hmac_sha/sha2.h
Quoted 4 lines
Quoted 2 lines

--
2.0.0

This seems like great stuff for mmc-utils. Chris, can you pick them up?

Thanks, pushed to mmc-utils. I added the following text to the top of each hmac_sha file:

+ * Since this code has been incorporated into a GPLv2 project, it is
+ * distributed under GPLv2 inside mmc-utils. The original BSD license
+ * that the code was released under is included below for clarity.

.. since my understanding is that BSD code that's linked into a GPLv2 binary "becomes GPL" inside that project.

- Chris.

Signature · 1 line
Chris Ball   <http://printf.net/>
#7 lore raw

Hello all.

Am I too late to change the second patch?
Few weeks ago I added support of multiple blocks read and forgot to send new one :)

I do not want to send incremental patch.
Can you change the git history?

--
Roman

Quoted 53 lines · Chris Ball

Hi,

Quoted 32 lines · Ulf Hansson
Quoted 31 lines · Roman Pen

This is simple RPMB support for 4 operations:
write-key
read-counter
write-block
read-block

I did not attempt to implement multiple blocks read/write, thus only 1 block (256 b) is supported right now.

Also I had to take HMAC SHA256 implementation as 3rdparty to sign RPMB messages.

Roman Pen (2):
mmc-utils: RPMB: add HMAC SHA256 support
mmc-utils: RPMB: add support for 4 rpmb operations

 3rdparty/hmac_sha/hmac_sha2.c | 544 ++++++++++++++++++++++++
 3rdparty/hmac_sha/hmac_sha2.h | 140 +++++++
 3rdparty/hmac_sha/sha2.c      | 949 ++++++++++++++++++++++++++++++++++++++++++
 3rdparty/hmac_sha/sha2.h      | 108 +++++
 Makefile                      |   8 +-
 mmc.c                         |  28 ++
 mmc.h                         |   6 +
 mmc_cmds.c                    | 419 +++++++++++++++++++
 mmc_cmds.h                    |   4 +
 9 files changed, 2204 insertions(+), 2 deletions(-)
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.c
 create mode 100644 3rdparty/hmac_sha/hmac_sha2.h
 create mode 100644 3rdparty/hmac_sha/sha2.c
 create mode 100644 3rdparty/hmac_sha/sha2.h
Quoted 4 lines
Quoted 2 lines

--
2.0.0

This seems like great stuff for mmc-utils. Chris, can you pick them up?

Thanks, pushed to mmc-utils. I added the following text to the top of each hmac_sha file:

+ * Since this code has been incorporated into a GPLv2 project, it is
+ * distributed under GPLv2 inside mmc-utils. The original BSD license
+ * that the code was released under is included below for clarity.

.. since my understanding is that BSD code that's linked into a GPLv2 binary "becomes GPL" inside that project.

- Chris.
--
Chris Ball <http://printf.net/>

#8 lore raw

Hi Roman,

Quoted 8 lines · Roman Peniaev

Hello all.

Am I too late to change the second patch?
Few weeks ago I added support of multiple blocks read and forgot to send new one :)

I do not want to send incremental patch.
Can you change the git history?

If you send a v2, I can turn it into an incremental patch by just copying the changed files over the existing ones. Does that work? I prefer not to rewrite history.

- Chris.

Signature · 1 line
Chris Ball   <http://printf.net/>
#9 lore raw

ok, I am late :)
in that case I can create another patch by myself. no problem.

Quoted 19 lines · Chris Ball

Hi Roman,

Quoted 8 lines · Roman Peniaev

Hello all.

Am I too late to change the second patch?
Few weeks ago I added support of multiple blocks read and forgot to send new one :)

I do not want to send incremental patch.
Can you change the git history?

If you send a v2, I can turn it into an incremental patch by just copying the changed files over the existing ones. Does that work? I prefer not to rewrite history.

- Chris.
--
Chris Ball <http://printf.net/>

#10 lore raw

Hi,

Quoted 3 lines · Roman Peniaev

ok, I am late :)
in that case I can create another patch by myself. no problem.

Oh! Actually I didn't run git push yet, so you're not late after all. Resending v2 is fine.

Thanks,

- Chris.

Signature · 1 line
Chris Ball   <http://printf.net/>
#11 lore raw

great. I sent v2

--
Roman

Quoted 15 lines · Chris Ball

Hi,

Quoted 3 lines · Roman Peniaev

ok, I am late :)
in that case I can create another patch by myself. no problem.

Oh! Actually I didn't run git push yet, so you're not late after all. Resending v2 is fine.

Thanks,

- Chris.
--
Chris Ball <http://printf.net/>