1/*	$OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $	*/
2/*-
3 * The authors of this code are John Ioannidis (ji@tla.org),
4 * Angelos D. Keromytis (kermit@csd.uch.gr),
5 * Niels Provos (provos@physnet.uni-hamburg.de) and
6 * Damien Miller (djm@mindrot.org).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Additional features in 1999 by Angelos D. Keromytis.
18 *
19 * AES XTS implementation in 2008 by Damien Miller
20 *
21 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 * Angelos D. Keromytis and Niels Provos.
23 *
24 * Copyright (C) 2001, Angelos D. Keromytis.
25 *
26 * Copyright (C) 2008, Damien Miller
27 * Copyright (c) 2014 The FreeBSD Foundation
28 * All rights reserved.
29 *
30 * Portions of this software were developed by John-Mark Gurney
31 * under sponsorship of the FreeBSD Foundation and
32 * Rubicon Communications, LLC (Netgate).
33 *
34 * Permission to use, copy, and modify this software with or without fee
35 * is hereby granted, provided that this entire notice is included in
36 * all copies of any software which is or includes a copy or
37 * modification of this software.
38 * You may use this code under the GNU public license if you so wish. Please
39 * contribute changes back to the authors under this freer than GPL license
40 * so that we may further the use of strong encryption without limitations to
41 * all.
42 *
43 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
44 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
45 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
46 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
47 * PURPOSE.
48 */
49
50#include <sys/types.h>
51#include <crypto/camellia/camellia.h>
52#include <opencrypto/xform_enc.h>
53
54struct camellia_cbc_ctx {
55	camellia_ctx state;
56	char iv[CAMELLIA_BLOCK_LEN];
57};
58
59static	int cml_setkey(void *, const uint8_t *, int);
60static	void cml_encrypt(void *, const uint8_t *, uint8_t *);
61static	void cml_decrypt(void *, const uint8_t *, uint8_t *);
62static	void cml_encrypt_multi(void *, const uint8_t *, uint8_t *, size_t);
63static	void cml_decrypt_multi(void *, const uint8_t *, uint8_t *, size_t);
64static	void cml_reinit(void *, const uint8_t *, size_t);
65
66/* Encryption instances */
67const struct enc_xform enc_xform_camellia = {
68	.type = CRYPTO_CAMELLIA_CBC,
69	.name = "Camellia-CBC",
70	.ctxsize = sizeof(struct camellia_cbc_ctx),
71	.blocksize = CAMELLIA_BLOCK_LEN,
72	.ivsize = CAMELLIA_BLOCK_LEN,
73	.minkey = CAMELLIA_MIN_KEY,
74	.maxkey = CAMELLIA_MAX_KEY,
75	.setkey = cml_setkey,
76	.reinit = cml_reinit,
77	.encrypt = cml_encrypt,
78	.decrypt = cml_decrypt,
79	.encrypt_multi = cml_encrypt_multi,
80	.decrypt_multi = cml_decrypt_multi,
81};
82
83/*
84 * Encryption wrapper routines.
85 */
86static void
87cml_encrypt(void *vctx, const uint8_t *in, uint8_t *out)
88{
89	struct camellia_cbc_ctx *ctx = vctx;
90
91	for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
92		out[i] = in[i] ^ ctx->iv[i];
93	camellia_encrypt(&ctx->state, out, out);
94	memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN);
95}
96
97static void
98cml_decrypt(void *vctx, const uint8_t *in, uint8_t *out)
99{
100	struct camellia_cbc_ctx *ctx = vctx;
101	char block[CAMELLIA_BLOCK_LEN];
102
103	memcpy(block, in, CAMELLIA_BLOCK_LEN);
104	camellia_decrypt(&ctx->state, in, out);
105	for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
106		out[i] ^= ctx->iv[i];
107	memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN);
108	explicit_bzero(block, sizeof(block));
109}
110
111static void
112cml_encrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len)
113{
114	struct camellia_cbc_ctx *ctx = vctx;
115
116	KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length",
117	    __func__));
118	while (len > 0) {
119		for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
120			out[i] = in[i] ^ ctx->iv[i];
121		camellia_encrypt(&ctx->state, out, out);
122		memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN);
123		out += CAMELLIA_BLOCK_LEN;
124		in += CAMELLIA_BLOCK_LEN;
125		len -= CAMELLIA_BLOCK_LEN;
126	}
127}
128
129static void
130cml_decrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len)
131{
132	struct camellia_cbc_ctx *ctx = vctx;
133	char block[CAMELLIA_BLOCK_LEN];
134
135	KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length",
136	    __func__));
137	while (len > 0) {
138		memcpy(block, in, CAMELLIA_BLOCK_LEN);
139		camellia_decrypt(&ctx->state, in, out);
140		for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
141			out[i] ^= ctx->iv[i];
142		memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN);
143		out += CAMELLIA_BLOCK_LEN;
144		in += CAMELLIA_BLOCK_LEN;
145		len -= CAMELLIA_BLOCK_LEN;
146	}
147	explicit_bzero(block, sizeof(block));
148}
149
150static int
151cml_setkey(void *vctx, const uint8_t *key, int len)
152{
153	struct camellia_cbc_ctx *ctx = vctx;
154
155	if (len != 16 && len != 24 && len != 32)
156		return (EINVAL);
157
158	camellia_set_key(&ctx->state, key, len * 8);
159	return (0);
160}
161
162static void
163cml_reinit(void *vctx, const uint8_t *iv, size_t iv_len)
164{
165	struct camellia_cbc_ctx *ctx = vctx;
166
167	KASSERT(iv_len == sizeof(ctx->iv), ("%s: bad IV length", __func__));
168	memcpy(ctx->iv, iv, sizeof(ctx->iv));
169}
170