Debian Codegraph @

cipher/cipher-xts.c

raw
1 /* cipher-xts.c - XTS mode implementation
2 * Copyright (C) 2017 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "g10lib.h"
27 #include "cipher.h"
28 #include "bufhelp.h"
29 #include "./cipher-internal.h"
30
31
32 static inline void xts_gfmul_byA (unsigned char *out, const unsigned char *in)
33 {
34 u64 hi = buf_get_le64 (in + 8);
35 u64 lo = buf_get_le64 (in + 0);
36 u64 carry = ct_ulong_gen_mask(hi >> 63) & 0x87;
37
38 hi = (hi << 1) + (lo >> 63);
39 lo = (lo << 1) ^ carry;
40
41 buf_put_le64 (out + 8, hi);
42 buf_put_le64 (out + 0, lo);
43 }
44
45
46 static inline void xts_inc128 (unsigned char *seqno)
47 {
48 u64 lo = buf_get_le64 (seqno + 0);
49 u64 hi = buf_get_le64 (seqno + 8);
50
51 hi += !(++lo);
52
53 buf_put_le64 (seqno + 0, lo);
54 buf_put_le64 (seqno + 8, hi);
55 }
56
57
58 gcry_err_code_t
59 _gcry_cipher_xts_crypt (gcry_cipher_hd_t c,
60 unsigned char *outbuf, size_t outbuflen,
61 const unsigned char *inbuf, size_t inbuflen,
62 int encrypt)
63 {
64 gcry_cipher_encrypt_t tweak_fn = c->spec->encrypt;
65 gcry_cipher_encrypt_t crypt_fn =
66 encrypt ? c->spec->encrypt : c->spec->decrypt;
67 union
68 {
69 cipher_context_alignment_t xcx;
70 byte x1[GCRY_XTS_BLOCK_LEN];
71 u64 x64[GCRY_XTS_BLOCK_LEN / sizeof(u64)];
72 } tmp;
73 unsigned int burn, nburn;
74 size_t nblocks;
75
76 if (c->spec->blocksize != GCRY_XTS_BLOCK_LEN)
77 return GPG_ERR_CIPHER_ALGO;
78 if (outbuflen < inbuflen)
79 return GPG_ERR_BUFFER_TOO_SHORT;
80 if (inbuflen < GCRY_XTS_BLOCK_LEN)
81 return GPG_ERR_BUFFER_TOO_SHORT;
82
83 /* Data-unit max length: 2^20 blocks. */
84 if (inbuflen > GCRY_XTS_BLOCK_LEN << 20)
85 return GPG_ERR_INV_LENGTH;
86
87 nblocks = inbuflen / GCRY_XTS_BLOCK_LEN;
88 nblocks -= !encrypt && (inbuflen % GCRY_XTS_BLOCK_LEN) != 0;
89
90 /* Generate first tweak value. */
91 burn = tweak_fn (c->u_mode.xts.tweak_context, c->u_ctr.ctr, c->u_iv.iv);
92
93 /* Use a bulk method if available. */
94 if (nblocks && c->bulk.xts_crypt)
95 {
96 c->bulk.xts_crypt (&c->context.c, c->u_ctr.ctr, outbuf, inbuf, nblocks,
97 encrypt);
98 inbuf += nblocks * GCRY_XTS_BLOCK_LEN;
99 outbuf += nblocks * GCRY_XTS_BLOCK_LEN;
100 inbuflen -= nblocks * GCRY_XTS_BLOCK_LEN;
101 nblocks = 0;
102 }
103
104 /* If we don't have a bulk method use the standard method. We also
105 use this method for the a remaining partial block. */
106
107 while (nblocks)
108 {
109 /* Xor-Encrypt/Decrypt-Xor block. */
110 cipher_block_xor (tmp.x64, inbuf, c->u_ctr.ctr, GCRY_XTS_BLOCK_LEN);
111 nburn = crypt_fn (&c->context.c, tmp.x1, tmp.x1);
112 burn = nburn > burn ? nburn : burn;
113 cipher_block_xor (outbuf, tmp.x64, c->u_ctr.ctr, GCRY_XTS_BLOCK_LEN);
114
115 outbuf += GCRY_XTS_BLOCK_LEN;
116 inbuf += GCRY_XTS_BLOCK_LEN;
117 inbuflen -= GCRY_XTS_BLOCK_LEN;
118 nblocks--;
119
120 /* Generate next tweak. */
121 xts_gfmul_byA (c->u_ctr.ctr, c->u_ctr.ctr);
122 }
123
124 /* Handle remaining data with ciphertext stealing. */
125 if (inbuflen)
126 {
127 if (!encrypt)
128 {
129 gcry_assert (inbuflen > GCRY_XTS_BLOCK_LEN);
130 gcry_assert (inbuflen < GCRY_XTS_BLOCK_LEN * 2);
131
132 /* Generate last tweak. */
133 xts_gfmul_byA (tmp.x1, c->u_ctr.ctr);
134
135 /* Decrypt last block first. */
136 cipher_block_xor (outbuf, inbuf, tmp.x64, GCRY_XTS_BLOCK_LEN);
137 nburn = crypt_fn (&c->context.c, outbuf, outbuf);
138 burn = nburn > burn ? nburn : burn;
139 cipher_block_xor (outbuf, outbuf, tmp.x64, GCRY_XTS_BLOCK_LEN);
140
141 inbuflen -= GCRY_XTS_BLOCK_LEN;
142 inbuf += GCRY_XTS_BLOCK_LEN;
143 outbuf += GCRY_XTS_BLOCK_LEN;
144 }
145
146 gcry_assert (inbuflen < GCRY_XTS_BLOCK_LEN);
147 outbuf -= GCRY_XTS_BLOCK_LEN;
148
149 /* Steal ciphertext from previous block. */
150 cipher_block_cpy (tmp.x64, outbuf, GCRY_XTS_BLOCK_LEN);
151 buf_cpy (tmp.x64, inbuf, inbuflen);
152 buf_cpy (outbuf + GCRY_XTS_BLOCK_LEN, outbuf, inbuflen);
153
154 /* Decrypt/Encrypt last block. */
155 cipher_block_xor (tmp.x64, tmp.x64, c->u_ctr.ctr, GCRY_XTS_BLOCK_LEN);
156 nburn = crypt_fn (&c->context.c, tmp.x1, tmp.x1);
157 burn = nburn > burn ? nburn : burn;
158 cipher_block_xor (outbuf, tmp.x64, c->u_ctr.ctr, GCRY_XTS_BLOCK_LEN);
159 }
160
161 /* Auto-increment data-unit sequence number */
162 xts_inc128 (c->u_iv.iv);
163
164 wipememory (&tmp, sizeof(tmp));
165 wipememory (c->u_ctr.ctr, sizeof(c->u_ctr.ctr));
166
167 if (burn > 0)
168 _gcry_burn_stack (burn + 4 * sizeof(void *));
169
170 return 0;
171 }
172
173
174 gcry_err_code_t
175 _gcry_cipher_xts_encrypt (gcry_cipher_hd_t c,
176 unsigned char *outbuf, size_t outbuflen,
177 const unsigned char *inbuf, size_t inbuflen)
178 {
179 return _gcry_cipher_xts_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 1);
180 }
181
182
183 gcry_err_code_t
184 _gcry_cipher_xts_decrypt (gcry_cipher_hd_t c,
185 unsigned char *outbuf, size_t outbuflen,
186 const unsigned char *inbuf, size_t inbuflen)
187 {
188 return _gcry_cipher_xts_crypt (c, outbuf, outbuflen, inbuf, inbuflen, 0);
189 }