12155bb23SAllan Jude /* $OpenBSD: xform.c,v 1.16 2001/08/28 12:20:43 ben Exp $ */
22155bb23SAllan Jude /*-
32155bb23SAllan Jude * The authors of this code are John Ioannidis (ji@tla.org),
42155bb23SAllan Jude * Angelos D. Keromytis (kermit@csd.uch.gr),
52155bb23SAllan Jude * Niels Provos (provos@physnet.uni-hamburg.de) and
62155bb23SAllan Jude * Damien Miller (djm@mindrot.org).
72155bb23SAllan Jude *
82155bb23SAllan Jude * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
92155bb23SAllan Jude * in November 1995.
102155bb23SAllan Jude *
112155bb23SAllan Jude * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
122155bb23SAllan Jude * by Angelos D. Keromytis.
132155bb23SAllan Jude *
142155bb23SAllan Jude * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
152155bb23SAllan Jude * and Niels Provos.
162155bb23SAllan Jude *
172155bb23SAllan Jude * Additional features in 1999 by Angelos D. Keromytis.
182155bb23SAllan Jude *
192155bb23SAllan Jude * AES XTS implementation in 2008 by Damien Miller
202155bb23SAllan Jude *
212155bb23SAllan Jude * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
222155bb23SAllan Jude * Angelos D. Keromytis and Niels Provos.
232155bb23SAllan Jude *
242155bb23SAllan Jude * Copyright (C) 2001, Angelos D. Keromytis.
252155bb23SAllan Jude *
262155bb23SAllan Jude * Copyright (C) 2008, Damien Miller
272155bb23SAllan Jude * Copyright (c) 2014 The FreeBSD Foundation
282155bb23SAllan Jude * All rights reserved.
292155bb23SAllan Jude *
302155bb23SAllan Jude * Portions of this software were developed by John-Mark Gurney
312155bb23SAllan Jude * under sponsorship of the FreeBSD Foundation and
322155bb23SAllan Jude * Rubicon Communications, LLC (Netgate).
332155bb23SAllan Jude *
342155bb23SAllan Jude * Permission to use, copy, and modify this software with or without fee
352155bb23SAllan Jude * is hereby granted, provided that this entire notice is included in
362155bb23SAllan Jude * all copies of any software which is or includes a copy or
372155bb23SAllan Jude * modification of this software.
382155bb23SAllan Jude * You may use this code under the GNU public license if you so wish. Please
392155bb23SAllan Jude * contribute changes back to the authors under this freer than GPL license
402155bb23SAllan Jude * so that we may further the use of strong encryption without limitations to
412155bb23SAllan Jude * all.
422155bb23SAllan Jude *
432155bb23SAllan Jude * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
442155bb23SAllan Jude * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
452155bb23SAllan Jude * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
462155bb23SAllan Jude * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
472155bb23SAllan Jude * PURPOSE.
482155bb23SAllan Jude */
492155bb23SAllan Jude
50*faf470ffSJohn Baldwin #include <sys/types.h>
512155bb23SAllan Jude #include <crypto/camellia/camellia.h>
522155bb23SAllan Jude #include <opencrypto/xform_enc.h>
532155bb23SAllan Jude
54f84d708bSJohn Baldwin struct camellia_cbc_ctx {
55f84d708bSJohn Baldwin camellia_ctx state;
56f84d708bSJohn Baldwin char iv[CAMELLIA_BLOCK_LEN];
57f84d708bSJohn Baldwin };
58f84d708bSJohn Baldwin
593e947048SJohn Baldwin static int cml_setkey(void *, const uint8_t *, int);
603e947048SJohn Baldwin static void cml_encrypt(void *, const uint8_t *, uint8_t *);
613e947048SJohn Baldwin static void cml_decrypt(void *, const uint8_t *, uint8_t *);
62d7f0b3ceSJohn Baldwin static void cml_encrypt_multi(void *, const uint8_t *, uint8_t *, size_t);
63d7f0b3ceSJohn Baldwin static void cml_decrypt_multi(void *, const uint8_t *, uint8_t *, size_t);
64f84d708bSJohn Baldwin static void cml_reinit(void *, const uint8_t *, size_t);
652155bb23SAllan Jude
662155bb23SAllan Jude /* Encryption instances */
67d8787d4fSMark Johnston const struct enc_xform enc_xform_camellia = {
683e947048SJohn Baldwin .type = CRYPTO_CAMELLIA_CBC,
693e947048SJohn Baldwin .name = "Camellia-CBC",
70f84d708bSJohn Baldwin .ctxsize = sizeof(struct camellia_cbc_ctx),
713e947048SJohn Baldwin .blocksize = CAMELLIA_BLOCK_LEN,
723e947048SJohn Baldwin .ivsize = CAMELLIA_BLOCK_LEN,
733e947048SJohn Baldwin .minkey = CAMELLIA_MIN_KEY,
743e947048SJohn Baldwin .maxkey = CAMELLIA_MAX_KEY,
753e947048SJohn Baldwin .setkey = cml_setkey,
76f84d708bSJohn Baldwin .reinit = cml_reinit,
77d7f0b3ceSJohn Baldwin .encrypt = cml_encrypt,
78d7f0b3ceSJohn Baldwin .decrypt = cml_decrypt,
79d7f0b3ceSJohn Baldwin .encrypt_multi = cml_encrypt_multi,
80d7f0b3ceSJohn Baldwin .decrypt_multi = cml_decrypt_multi,
812155bb23SAllan Jude };
822155bb23SAllan Jude
832155bb23SAllan Jude /*
842155bb23SAllan Jude * Encryption wrapper routines.
852155bb23SAllan Jude */
862155bb23SAllan Jude static void
cml_encrypt(void * vctx,const uint8_t * in,uint8_t * out)87f84d708bSJohn Baldwin cml_encrypt(void *vctx, const uint8_t *in, uint8_t *out)
882155bb23SAllan Jude {
89f84d708bSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
90f84d708bSJohn Baldwin
91f84d708bSJohn Baldwin for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
92f84d708bSJohn Baldwin out[i] = in[i] ^ ctx->iv[i];
9333d56e57SJohn Baldwin camellia_encrypt(&ctx->state, out, out);
94f84d708bSJohn Baldwin memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN);
952155bb23SAllan Jude }
962155bb23SAllan Jude
972155bb23SAllan Jude static void
cml_decrypt(void * vctx,const uint8_t * in,uint8_t * out)98f84d708bSJohn Baldwin cml_decrypt(void *vctx, const uint8_t *in, uint8_t *out)
992155bb23SAllan Jude {
100f84d708bSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
101f84d708bSJohn Baldwin char block[CAMELLIA_BLOCK_LEN];
102f84d708bSJohn Baldwin
103f84d708bSJohn Baldwin memcpy(block, in, CAMELLIA_BLOCK_LEN);
104f84d708bSJohn Baldwin camellia_decrypt(&ctx->state, in, out);
105f84d708bSJohn Baldwin for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
106f84d708bSJohn Baldwin out[i] ^= ctx->iv[i];
107f84d708bSJohn Baldwin memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN);
108f84d708bSJohn Baldwin explicit_bzero(block, sizeof(block));
1092155bb23SAllan Jude }
1102155bb23SAllan Jude
111d7f0b3ceSJohn Baldwin static void
cml_encrypt_multi(void * vctx,const uint8_t * in,uint8_t * out,size_t len)112d7f0b3ceSJohn Baldwin cml_encrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len)
113d7f0b3ceSJohn Baldwin {
114d7f0b3ceSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
115d7f0b3ceSJohn Baldwin
116d7f0b3ceSJohn Baldwin KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length",
117d7f0b3ceSJohn Baldwin __func__));
118d7f0b3ceSJohn Baldwin while (len > 0) {
119d7f0b3ceSJohn Baldwin for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
120d7f0b3ceSJohn Baldwin out[i] = in[i] ^ ctx->iv[i];
121d7f0b3ceSJohn Baldwin camellia_encrypt(&ctx->state, out, out);
122d7f0b3ceSJohn Baldwin memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN);
123d7f0b3ceSJohn Baldwin out += CAMELLIA_BLOCK_LEN;
124d7f0b3ceSJohn Baldwin in += CAMELLIA_BLOCK_LEN;
125d7f0b3ceSJohn Baldwin len -= CAMELLIA_BLOCK_LEN;
126d7f0b3ceSJohn Baldwin }
127d7f0b3ceSJohn Baldwin }
128d7f0b3ceSJohn Baldwin
129d7f0b3ceSJohn Baldwin static void
cml_decrypt_multi(void * vctx,const uint8_t * in,uint8_t * out,size_t len)130d7f0b3ceSJohn Baldwin cml_decrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len)
131d7f0b3ceSJohn Baldwin {
132d7f0b3ceSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
133d7f0b3ceSJohn Baldwin char block[CAMELLIA_BLOCK_LEN];
134d7f0b3ceSJohn Baldwin
135d7f0b3ceSJohn Baldwin KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length",
136d7f0b3ceSJohn Baldwin __func__));
137d7f0b3ceSJohn Baldwin while (len > 0) {
138d7f0b3ceSJohn Baldwin memcpy(block, in, CAMELLIA_BLOCK_LEN);
139d7f0b3ceSJohn Baldwin camellia_decrypt(&ctx->state, in, out);
140d7f0b3ceSJohn Baldwin for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++)
141d7f0b3ceSJohn Baldwin out[i] ^= ctx->iv[i];
142d7f0b3ceSJohn Baldwin memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN);
143d7f0b3ceSJohn Baldwin out += CAMELLIA_BLOCK_LEN;
144d7f0b3ceSJohn Baldwin in += CAMELLIA_BLOCK_LEN;
145d7f0b3ceSJohn Baldwin len -= CAMELLIA_BLOCK_LEN;
146d7f0b3ceSJohn Baldwin }
147d7f0b3ceSJohn Baldwin explicit_bzero(block, sizeof(block));
148d7f0b3ceSJohn Baldwin }
149d7f0b3ceSJohn Baldwin
1502155bb23SAllan Jude static int
cml_setkey(void * vctx,const uint8_t * key,int len)151f84d708bSJohn Baldwin cml_setkey(void *vctx, const uint8_t *key, int len)
1522155bb23SAllan Jude {
153f84d708bSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
1542155bb23SAllan Jude
1552155bb23SAllan Jude if (len != 16 && len != 24 && len != 32)
1562155bb23SAllan Jude return (EINVAL);
1572155bb23SAllan Jude
158f84d708bSJohn Baldwin camellia_set_key(&ctx->state, key, len * 8);
1593e947048SJohn Baldwin return (0);
1602155bb23SAllan Jude }
161f84d708bSJohn Baldwin
162f84d708bSJohn Baldwin static void
cml_reinit(void * vctx,const uint8_t * iv,size_t iv_len)163f84d708bSJohn Baldwin cml_reinit(void *vctx, const uint8_t *iv, size_t iv_len)
164f84d708bSJohn Baldwin {
165f84d708bSJohn Baldwin struct camellia_cbc_ctx *ctx = vctx;
166f84d708bSJohn Baldwin
167f84d708bSJohn Baldwin KASSERT(iv_len == sizeof(ctx->iv), ("%s: bad IV length", __func__));
168f84d708bSJohn Baldwin memcpy(ctx->iv, iv, sizeof(ctx->iv));
169f84d708bSJohn Baldwin }
170