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
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
28 #include "generic_xts.h"
33 xts_reinit(struct xts_ctx
*ctx
, u_int64_t blocknum
)
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;
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
);
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
;
58 for (i
= 0; i
< ctx
->blk_sz
; i
++)
59 block
[i
] = data
[i
] ^ ctx
->tweak
[i
];
62 err
= ctx
->encrypt_fn(ctx
->ctx1
, ctx
->blk_sz
, block
, data
);
64 err
= ctx
->decrypt_fn(ctx
->ctx1
, ctx
->blk_sz
, block
, data
);
69 for (i
= 0; i
< ctx
->blk_sz
; i
++)
70 data
[i
] ^= ctx
->tweak
[i
];
72 /* Exponentiate tweak */
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);
80 ctx
->tweak
[0] ^= XTS_ALPHA
;
83 bzero(block
, sizeof(block
));
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
)
94 if (len
!= 32 && len
!= 64)
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);
107 err
= ctx
->set_key_fn(&ctx
->ctx2
, arg1
, arg2
, key
+ (len
/ 2),
110 ctx
->zero_key_fn(&ctx
->ctx1
);
118 xts_encrypt(struct xts_ctx
*ctx
, u_int8_t
*data
, size_t len
, uint8_t *iv
)
120 uint64_t sector
= *((uint64_t *)iv
);
123 if ((len
% ctx
->blk_sz
) != 0)
126 err
= xts_reinit(ctx
, sector
);
131 err
= xts_crypt(ctx
, data
, 1);
143 xts_decrypt(struct xts_ctx
*ctx
, u_int8_t
*data
, size_t len
, uint8_t *iv
)
145 uint64_t sector
= *((uint64_t *)iv
);
148 if ((len
% ctx
->blk_sz
) != 0)
151 err
= xts_reinit(ctx
, sector
);
156 err
= xts_crypt(ctx
, data
, 0);
168 xts_uninit(struct xts_ctx
*ctx
)
170 ctx
->zero_key_fn(&ctx
->ctx1
);
171 ctx
->zero_key_fn(&ctx
->ctx2
);