From nobody Thu Dec 16 22:09:29 2021 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 15CFB18D96B1; Thu, 16 Dec 2021 22:09:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JFR9P4FPyz3CH1; Thu, 16 Dec 2021 22:09:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 41186103AF; Thu, 16 Dec 2021 22:09:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BGM9T7P089189; Thu, 16 Dec 2021 22:09:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BGM9Tm9089188; Thu, 16 Dec 2021 22:09:29 GMT (envelope-from git) Date: Thu, 16 Dec 2021 22:09:29 GMT Message-Id: <202112162209.1BGM9Tm9089188@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: f84d708b484b - main - crypto: Move CBC handling into enc_xform_camellia. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f84d708b484bb5cf89e1b7943d7a896102831be4 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639692569; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2+pJ7+GGfeVenjW0E2Pw+WE30MLTxKKGT08zJhuRGYg=; b=LEkyYOrPjwKsYEbxqEunkuYS9wNlrNziAuzhQ9XWklM1tyijdpjjvB8QvZQIFpqmuotc3Y ig2jgfRBDj/7NwkYX2k6QpR10b0EnR1VU4aCbjefHBQe72hJ32gnxPNpDnglCphgTFfnmK QVAUTR7HvvL8StROczLFkBhnzYai0GrLcto2CQrIZ3iMp5tLKFQJoXzzgZF9aocjRdOsve W5MQmYe+Bprkg5P1j37hqPMcO8q2lT44k+JynGf/+TcwuI3qLuJNE0sbbSBtyADtBR0zWP hSHLlPybCf85LqBstjqESRnv3sVJqisn186df6poSYiimgTFMHl8AIwJZ14PDg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639692569; a=rsa-sha256; cv=none; b=FS96Z87UFp0YUg7DPa1ATD6kdY5DP49wPxskoDfjg3kZ2szOMQXfcQpPRlKKgsmB4SL0O9 /kLV0A+bmNIFzoNt+5mSuqDpmnSei1WWeZNTWKBfD4D2s1FMcHJD0E1FHTDfJfiXC1gV+o rKid9Z1uqPMXtdO+wxZFtemyFAnLJO+vVoZBjr55QJ2xdqhK8ZN/dGF7bYa2126k+X6tjc XIktEK3sXSFIWAg1rIymlTKYhwtSDdClGoELabM0/XXTqWBHld6GJnNFTmiDJ2v9Xdk9Gg SqeDvoXcW+pMNYa/h9Q0UXB2UxHTm2bmA//V34C7WwinidlCKsYtFuIEYLciuQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=f84d708b484bb5cf89e1b7943d7a896102831be4 commit f84d708b484bb5cf89e1b7943d7a896102831be4 Author: John Baldwin AuthorDate: 2021-12-16 21:48:02 +0000 Commit: John Baldwin CommitDate: 2021-12-16 21:48:02 +0000 crypto: Move CBC handling into enc_xform_camellia. Add an IV to the context that is initialized by the reinit hook. Encrypt and decrypt operations handle cipher block chaining along with updating the IV saved in the context. Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33488 --- sys/opencrypto/xform_cml.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/sys/opencrypto/xform_cml.c b/sys/opencrypto/xform_cml.c index ac1dabd90d30..8ab18142489c 100644 --- a/sys/opencrypto/xform_cml.c +++ b/sys/opencrypto/xform_cml.c @@ -53,15 +53,21 @@ __FBSDID("$FreeBSD$"); #include #include +struct camellia_cbc_ctx { + camellia_ctx state; + char iv[CAMELLIA_BLOCK_LEN]; +}; + static int cml_setkey(void *, const uint8_t *, int); static void cml_encrypt(void *, const uint8_t *, uint8_t *); static void cml_decrypt(void *, const uint8_t *, uint8_t *); +static void cml_reinit(void *, const uint8_t *, size_t); /* Encryption instances */ const struct enc_xform enc_xform_camellia = { .type = CRYPTO_CAMELLIA_CBC, .name = "Camellia-CBC", - .ctxsize = sizeof(camellia_ctx), + .ctxsize = sizeof(struct camellia_cbc_ctx), .blocksize = CAMELLIA_BLOCK_LEN, .ivsize = CAMELLIA_BLOCK_LEN, .minkey = CAMELLIA_MIN_KEY, @@ -69,30 +75,54 @@ const struct enc_xform enc_xform_camellia = { .encrypt = cml_encrypt, .decrypt = cml_decrypt, .setkey = cml_setkey, + .reinit = cml_reinit, }; /* * Encryption wrapper routines. */ static void -cml_encrypt(void *ctx, const uint8_t *in, uint8_t *out) +cml_encrypt(void *vctx, const uint8_t *in, uint8_t *out) { - camellia_encrypt(ctx, in, out); + struct camellia_cbc_ctx *ctx = vctx; + + for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) + out[i] = in[i] ^ ctx->iv[i]; + camellia_encrypt(&ctx->state, in, out); + memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN); } static void -cml_decrypt(void *ctx, const uint8_t *in, uint8_t *out) +cml_decrypt(void *vctx, const uint8_t *in, uint8_t *out) { - camellia_decrypt(ctx, in, out); + struct camellia_cbc_ctx *ctx = vctx; + char block[CAMELLIA_BLOCK_LEN]; + + memcpy(block, in, CAMELLIA_BLOCK_LEN); + camellia_decrypt(&ctx->state, in, out); + for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) + out[i] ^= ctx->iv[i]; + memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN); + explicit_bzero(block, sizeof(block)); } static int -cml_setkey(void *ctx, const uint8_t *key, int len) +cml_setkey(void *vctx, const uint8_t *key, int len) { + struct camellia_cbc_ctx *ctx = vctx; if (len != 16 && len != 24 && len != 32) return (EINVAL); - camellia_set_key(ctx, key, len * 8); + camellia_set_key(&ctx->state, key, len * 8); return (0); } + +static void +cml_reinit(void *vctx, const uint8_t *iv, size_t iv_len) +{ + struct camellia_cbc_ctx *ctx = vctx; + + KASSERT(iv_len == sizeof(ctx->iv), ("%s: bad IV length", __func__)); + memcpy(ctx->iv, iv, sizeof(ctx->iv)); +}