Move AIO manual pages to librt.
[dragonfly.git] / lib / libtcplay / generic_xts.c
blob82020406f3e3783be8e1415359aea311de5a5cd8
1 /*
2 * Copyright (C) 2008, Damien Miller
3 * Copyright (C) 2011, Alex Hornung
5 * Permission to use, copy, and modify this software with or without fee
6 * is hereby granted, provided that this entire notice is included in
7 * all copies of any software which is or includes a copy or
8 * modification of this software.
9 * You may use this code under the GNU public license if you so wish. Please
10 * contribute changes back to the authors under this freer than GPL license
11 * so that we may further the use of strong encryption without limitations to
12 * all.
14 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
16 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
17 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
18 * PURPOSE.
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <inttypes.h>
27 #include "tcplay.h"
28 #include "generic_xts.h"
32 static int
33 xts_reinit(struct xts_ctx *ctx, u_int64_t blocknum)
35 u_int i;
38 * Prepare tweak as E_k2(IV). IV is specified as LE representation
39 * of a 64-bit block number which we allow to be passed in directly.
41 for (i = 0; i < XTS_IVSIZE; i++) {
42 ctx->tweak[i] = blocknum & 0xff;
43 blocknum >>= 8;
45 /* Last 64 bits of IV are always zero */
46 bzero(ctx->tweak + XTS_IVSIZE, XTS_IVSIZE);
48 return ctx->encrypt_fn(ctx->ctx2, ctx->blk_sz, ctx->tweak, ctx->tweak);
51 static int
52 xts_crypt(struct xts_ctx *ctx, u_int8_t *data, u_int do_encrypt)
54 u_int8_t block[XTS_MAX_BLOCKSIZE];
55 u_int i, carry_in, carry_out;
56 int err;
58 for (i = 0; i < ctx->blk_sz; i++)
59 block[i] = data[i] ^ ctx->tweak[i];
61 if (do_encrypt)
62 err = ctx->encrypt_fn(ctx->ctx1, ctx->blk_sz, block, data);
63 else
64 err = ctx->decrypt_fn(ctx->ctx1, ctx->blk_sz, block, data);
66 if (err)
67 goto out;
69 for (i = 0; i < ctx->blk_sz; i++)
70 data[i] ^= ctx->tweak[i];
72 /* Exponentiate tweak */
73 carry_in = 0;
74 for (i = 0; i < ctx->blk_sz; i++) {
75 carry_out = ctx->tweak[i] & 0x80;
76 ctx->tweak[i] = (ctx->tweak[i] << 1) | (carry_in ? 1 : 0);
77 carry_in = carry_out;
79 if (carry_in)
80 ctx->tweak[0] ^= XTS_ALPHA;
82 out:
83 bzero(block, sizeof(block));
84 return err;
87 int
88 xts_init(struct xts_ctx *ctx, void *arg1, void *arg2, set_key_fn _set_key_fn,
89 zero_key_fn _zero_key_fn, encrypt_decrypt_fn _encrypt_fn,
90 encrypt_decrypt_fn _decrypt_fn, u_int blk_sz, u_int8_t *key, int len)
92 int err;
94 if (len != 32 && len != 64)
95 return -1;
97 ctx->blk_sz = blk_sz;
98 ctx->encrypt_fn = _encrypt_fn;
99 ctx->decrypt_fn = _decrypt_fn;
100 ctx->set_key_fn = _set_key_fn;
101 ctx->zero_key_fn = _zero_key_fn;
103 err = ctx->set_key_fn(&ctx->ctx1, arg1, arg2, key, len * 4);
104 if (err)
105 return -1;
107 err = ctx->set_key_fn(&ctx->ctx2, arg1, arg2, key + (len / 2),
108 len * 4);
109 if (err) {
110 ctx->zero_key_fn(&ctx->ctx1);
111 return -1;
114 return 0;
118 xts_encrypt(struct xts_ctx *ctx, u_int8_t *data, size_t len, uint8_t *iv)
120 uint64_t sector = *((uint64_t *)iv);
121 int err;
123 if ((len % ctx->blk_sz) != 0)
124 return -1;
126 err = xts_reinit(ctx, sector);
127 if (err)
128 return err;
130 while (len > 0) {
131 err = xts_crypt(ctx, data, 1);
132 if (err)
133 return -1;
135 data += ctx->blk_sz;
136 len -= ctx->blk_sz;
139 return err;
143 xts_decrypt(struct xts_ctx *ctx, u_int8_t *data, size_t len, uint8_t *iv)
145 uint64_t sector = *((uint64_t *)iv);
146 int err;
148 if ((len % ctx->blk_sz) != 0)
149 return -1;
151 err = xts_reinit(ctx, sector);
152 if (err)
153 return err;
155 while (len > 0) {
156 err = xts_crypt(ctx, data, 0);
157 if (err)
158 return -1;
160 data += ctx->blk_sz;
161 len -= ctx->blk_sz;
164 return err;
168 xts_uninit(struct xts_ctx *ctx)
170 ctx->zero_key_fn(&ctx->ctx1);
171 ctx->zero_key_fn(&ctx->ctx2);
173 return 0;