]> granicus.if.org Git - zfs/blob - module/icp/include/modes/modes.h
Illumos Crypto Port module added to enable native encryption in zfs
[zfs] / module / icp / include / modes / modes.h
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 #ifndef _COMMON_CRYPTO_MODES_H
27 #define _COMMON_CRYPTO_MODES_H
28
29 #ifdef  __cplusplus
30 extern "C" {
31 #endif
32
33 #include <sys/zfs_context.h>
34 #include <sys/crypto/common.h>
35 #include <sys/crypto/impl.h>
36
37 #define ECB_MODE                        0x00000002
38 #define CBC_MODE                        0x00000004
39 #define CTR_MODE                        0x00000008
40 #define CCM_MODE                        0x00000010
41 #define GCM_MODE                        0x00000020
42 #define GMAC_MODE                       0x00000040
43
44 /*
45  * cc_keysched:         Pointer to key schedule.
46  *
47  * cc_keysched_len:     Length of the key schedule.
48  *
49  * cc_remainder:        This is for residual data, i.e. data that can't
50  *                      be processed because there are too few bytes.
51  *                      Must wait until more data arrives.
52  *
53  * cc_remainder_len:    Number of bytes in cc_remainder.
54  *
55  * cc_iv:               Scratch buffer that sometimes contains the IV.
56  *
57  * cc_lastp:            Pointer to previous block of ciphertext.
58  *
59  * cc_copy_to:          Pointer to where encrypted residual data needs
60  *                      to be copied.
61  *
62  * cc_flags:            PROVIDER_OWNS_KEY_SCHEDULE
63  *                      When a context is freed, it is necessary
64  *                      to know whether the key schedule was allocated
65  *                      by the caller, or internally, e.g. an init routine.
66  *                      If allocated by the latter, then it needs to be freed.
67  *
68  *                      ECB_MODE, CBC_MODE, CTR_MODE, or CCM_MODE
69  */
70 struct common_ctx {
71         void *cc_keysched;
72         size_t cc_keysched_len;
73         uint64_t cc_iv[2];
74         uint64_t cc_remainder[2];
75         size_t cc_remainder_len;
76         uint8_t *cc_lastp;
77         uint8_t *cc_copy_to;
78         uint32_t cc_flags;
79 };
80
81 typedef struct common_ctx common_ctx_t;
82
83 typedef struct ecb_ctx {
84         struct common_ctx ecb_common;
85         uint64_t ecb_lastblock[2];
86 } ecb_ctx_t;
87
88 #define ecb_keysched            ecb_common.cc_keysched
89 #define ecb_keysched_len        ecb_common.cc_keysched_len
90 #define ecb_iv                  ecb_common.cc_iv
91 #define ecb_remainder           ecb_common.cc_remainder
92 #define ecb_remainder_len       ecb_common.cc_remainder_len
93 #define ecb_lastp               ecb_common.cc_lastp
94 #define ecb_copy_to             ecb_common.cc_copy_to
95 #define ecb_flags               ecb_common.cc_flags
96
97 typedef struct cbc_ctx {
98         struct common_ctx cbc_common;
99         uint64_t cbc_lastblock[2];
100 } cbc_ctx_t;
101
102 #define cbc_keysched            cbc_common.cc_keysched
103 #define cbc_keysched_len        cbc_common.cc_keysched_len
104 #define cbc_iv                  cbc_common.cc_iv
105 #define cbc_remainder           cbc_common.cc_remainder
106 #define cbc_remainder_len       cbc_common.cc_remainder_len
107 #define cbc_lastp               cbc_common.cc_lastp
108 #define cbc_copy_to             cbc_common.cc_copy_to
109 #define cbc_flags               cbc_common.cc_flags
110
111 /*
112  * ctr_lower_mask               Bit-mask for lower 8 bytes of counter block.
113  * ctr_upper_mask               Bit-mask for upper 8 bytes of counter block.
114  */
115 typedef struct ctr_ctx {
116         struct common_ctx ctr_common;
117         uint64_t ctr_lower_mask;
118         uint64_t ctr_upper_mask;
119         uint32_t ctr_tmp[4];
120 } ctr_ctx_t;
121
122 /*
123  * ctr_cb                       Counter block.
124  */
125 #define ctr_keysched            ctr_common.cc_keysched
126 #define ctr_keysched_len        ctr_common.cc_keysched_len
127 #define ctr_cb                  ctr_common.cc_iv
128 #define ctr_remainder           ctr_common.cc_remainder
129 #define ctr_remainder_len       ctr_common.cc_remainder_len
130 #define ctr_lastp               ctr_common.cc_lastp
131 #define ctr_copy_to             ctr_common.cc_copy_to
132 #define ctr_flags               ctr_common.cc_flags
133
134 /*
135  *
136  * ccm_mac_len:         Stores length of the MAC in CCM mode.
137  * ccm_mac_buf:         Stores the intermediate value for MAC in CCM encrypt.
138  *                      In CCM decrypt, stores the input MAC value.
139  * ccm_data_len:        Length of the plaintext for CCM mode encrypt, or
140  *                      length of the ciphertext for CCM mode decrypt.
141  * ccm_processed_data_len:
142  *                      Length of processed plaintext in CCM mode encrypt,
143  *                      or length of processed ciphertext for CCM mode decrypt.
144  * ccm_processed_mac_len:
145  *                      Length of MAC data accumulated in CCM mode decrypt.
146  *
147  * ccm_pt_buf:          Only used in CCM mode decrypt.  It stores the
148  *                      decrypted plaintext to be returned when
149  *                      MAC verification succeeds in decrypt_final.
150  *                      Memory for this should be allocated in the AES module.
151  *
152  */
153 typedef struct ccm_ctx {
154         struct common_ctx ccm_common;
155         uint32_t ccm_tmp[4];
156         size_t ccm_mac_len;
157         uint64_t ccm_mac_buf[2];
158         size_t ccm_data_len;
159         size_t ccm_processed_data_len;
160         size_t ccm_processed_mac_len;
161         uint8_t *ccm_pt_buf;
162         uint64_t ccm_mac_input_buf[2];
163         uint64_t ccm_counter_mask;
164 } ccm_ctx_t;
165
166 #define ccm_keysched            ccm_common.cc_keysched
167 #define ccm_keysched_len        ccm_common.cc_keysched_len
168 #define ccm_cb                  ccm_common.cc_iv
169 #define ccm_remainder           ccm_common.cc_remainder
170 #define ccm_remainder_len       ccm_common.cc_remainder_len
171 #define ccm_lastp               ccm_common.cc_lastp
172 #define ccm_copy_to             ccm_common.cc_copy_to
173 #define ccm_flags               ccm_common.cc_flags
174
175 /*
176  * gcm_tag_len:         Length of authentication tag.
177  *
178  * gcm_ghash:           Stores output from the GHASH function.
179  *
180  * gcm_processed_data_len:
181  *                      Length of processed plaintext (encrypt) or
182  *                      length of processed ciphertext (decrypt).
183  *
184  * gcm_pt_buf:          Stores the decrypted plaintext returned by
185  *                      decrypt_final when the computed authentication
186  *                      tag matches the user supplied tag.
187  *
188  * gcm_pt_buf_len:      Length of the plaintext buffer.
189  *
190  * gcm_H:               Subkey.
191  *
192  * gcm_J0:              Pre-counter block generated from the IV.
193  *
194  * gcm_len_a_len_c:     64-bit representations of the bit lengths of
195  *                      AAD and ciphertext.
196  *
197  * gcm_kmflag:          Current value of kmflag. Used only for allocating
198  *                      the plaintext buffer during decryption.
199  */
200 typedef struct gcm_ctx {
201         struct common_ctx gcm_common;
202         size_t gcm_tag_len;
203         size_t gcm_processed_data_len;
204         size_t gcm_pt_buf_len;
205         uint32_t gcm_tmp[4];
206         uint64_t gcm_ghash[2];
207         uint64_t gcm_H[2];
208         uint64_t gcm_J0[2];
209         uint64_t gcm_len_a_len_c[2];
210         uint8_t *gcm_pt_buf;
211         int gcm_kmflag;
212 } gcm_ctx_t;
213
214 #define gcm_keysched            gcm_common.cc_keysched
215 #define gcm_keysched_len        gcm_common.cc_keysched_len
216 #define gcm_cb                  gcm_common.cc_iv
217 #define gcm_remainder           gcm_common.cc_remainder
218 #define gcm_remainder_len       gcm_common.cc_remainder_len
219 #define gcm_lastp               gcm_common.cc_lastp
220 #define gcm_copy_to             gcm_common.cc_copy_to
221 #define gcm_flags               gcm_common.cc_flags
222
223 #define AES_GMAC_IV_LEN         12
224 #define AES_GMAC_TAG_BITS       128
225
226 typedef struct aes_ctx {
227         union {
228                 ecb_ctx_t acu_ecb;
229                 cbc_ctx_t acu_cbc;
230                 ctr_ctx_t acu_ctr;
231                 ccm_ctx_t acu_ccm;
232                 gcm_ctx_t acu_gcm;
233         } acu;
234 } aes_ctx_t;
235
236 #define ac_flags                acu.acu_ecb.ecb_common.cc_flags
237 #define ac_remainder_len        acu.acu_ecb.ecb_common.cc_remainder_len
238 #define ac_keysched             acu.acu_ecb.ecb_common.cc_keysched
239 #define ac_keysched_len         acu.acu_ecb.ecb_common.cc_keysched_len
240 #define ac_iv                   acu.acu_ecb.ecb_common.cc_iv
241 #define ac_lastp                acu.acu_ecb.ecb_common.cc_lastp
242 #define ac_pt_buf               acu.acu_ccm.ccm_pt_buf
243 #define ac_mac_len              acu.acu_ccm.ccm_mac_len
244 #define ac_data_len             acu.acu_ccm.ccm_data_len
245 #define ac_processed_mac_len    acu.acu_ccm.ccm_processed_mac_len
246 #define ac_processed_data_len   acu.acu_ccm.ccm_processed_data_len
247 #define ac_tag_len              acu.acu_gcm.gcm_tag_len
248
249 typedef struct blowfish_ctx {
250         union {
251                 ecb_ctx_t bcu_ecb;
252                 cbc_ctx_t bcu_cbc;
253         } bcu;
254 } blowfish_ctx_t;
255
256 #define bc_flags                bcu.bcu_ecb.ecb_common.cc_flags
257 #define bc_remainder_len        bcu.bcu_ecb.ecb_common.cc_remainder_len
258 #define bc_keysched             bcu.bcu_ecb.ecb_common.cc_keysched
259 #define bc_keysched_len         bcu.bcu_ecb.ecb_common.cc_keysched_len
260 #define bc_iv                   bcu.bcu_ecb.ecb_common.cc_iv
261 #define bc_lastp                bcu.bcu_ecb.ecb_common.cc_lastp
262
263 typedef struct des_ctx {
264         union {
265                 ecb_ctx_t dcu_ecb;
266                 cbc_ctx_t dcu_cbc;
267         } dcu;
268 } des_ctx_t;
269
270 #define dc_flags                dcu.dcu_ecb.ecb_common.cc_flags
271 #define dc_remainder_len        dcu.dcu_ecb.ecb_common.cc_remainder_len
272 #define dc_keysched             dcu.dcu_ecb.ecb_common.cc_keysched
273 #define dc_keysched_len         dcu.dcu_ecb.ecb_common.cc_keysched_len
274 #define dc_iv                   dcu.dcu_ecb.ecb_common.cc_iv
275 #define dc_lastp                dcu.dcu_ecb.ecb_common.cc_lastp
276
277 extern int ecb_cipher_contiguous_blocks(ecb_ctx_t *, char *, size_t,
278     crypto_data_t *, size_t, int (*cipher)(const void *, const uint8_t *,
279     uint8_t *));
280
281 extern int cbc_encrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
282     crypto_data_t *, size_t,
283     int (*encrypt)(const void *, const uint8_t *, uint8_t *),
284     void (*copy_block)(uint8_t *, uint8_t *),
285     void (*xor_block)(uint8_t *, uint8_t *));
286
287 extern int cbc_decrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
288     crypto_data_t *, size_t,
289     int (*decrypt)(const void *, const uint8_t *, uint8_t *),
290     void (*copy_block)(uint8_t *, uint8_t *),
291     void (*xor_block)(uint8_t *, uint8_t *));
292
293 extern int ctr_mode_contiguous_blocks(ctr_ctx_t *, char *, size_t,
294     crypto_data_t *, size_t,
295     int (*cipher)(const void *, const uint8_t *, uint8_t *),
296     void (*xor_block)(uint8_t *, uint8_t *));
297
298 extern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
299     crypto_data_t *, size_t,
300     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
301     void (*copy_block)(uint8_t *, uint8_t *),
302     void (*xor_block)(uint8_t *, uint8_t *));
303
304 extern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
305     crypto_data_t *, size_t,
306     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
307     void (*copy_block)(uint8_t *, uint8_t *),
308     void (*xor_block)(uint8_t *, uint8_t *));
309
310 extern int gcm_mode_encrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,
311     crypto_data_t *, size_t,
312     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
313     void (*copy_block)(uint8_t *, uint8_t *),
314     void (*xor_block)(uint8_t *, uint8_t *));
315
316 extern int gcm_mode_decrypt_contiguous_blocks(gcm_ctx_t *, char *, size_t,
317     crypto_data_t *, size_t,
318     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
319     void (*copy_block)(uint8_t *, uint8_t *),
320     void (*xor_block)(uint8_t *, uint8_t *));
321
322 int ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
323     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
324     void (*xor_block)(uint8_t *, uint8_t *));
325
326 int gcm_encrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,
327     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
328     void (*copy_block)(uint8_t *, uint8_t *),
329     void (*xor_block)(uint8_t *, uint8_t *));
330
331 extern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
332     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
333     void (*copy_block)(uint8_t *, uint8_t *),
334     void (*xor_block)(uint8_t *, uint8_t *));
335
336 extern int gcm_decrypt_final(gcm_ctx_t *, crypto_data_t *, size_t,
337     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
338     void (*xor_block)(uint8_t *, uint8_t *));
339
340 extern int ctr_mode_final(ctr_ctx_t *, crypto_data_t *,
341     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
342
343 extern int cbc_init_ctx(cbc_ctx_t *, char *, size_t, size_t,
344     void (*copy_block)(uint8_t *, uint64_t *));
345
346 extern int ctr_init_ctx(ctr_ctx_t *, ulong_t, uint8_t *,
347     void (*copy_block)(uint8_t *, uint8_t *));
348
349 extern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t,
350     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
351     void (*xor_block)(uint8_t *, uint8_t *));
352
353 extern int gcm_init_ctx(gcm_ctx_t *, char *, size_t,
354     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
355     void (*copy_block)(uint8_t *, uint8_t *),
356     void (*xor_block)(uint8_t *, uint8_t *));
357
358 extern int gmac_init_ctx(gcm_ctx_t *, char *, size_t,
359     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
360     void (*copy_block)(uint8_t *, uint8_t *),
361     void (*xor_block)(uint8_t *, uint8_t *));
362
363 extern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *,
364     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
365
366 extern void gcm_mul(uint64_t *, uint64_t *, uint64_t *);
367
368 extern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *);
369 extern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *,
370     uint8_t **, size_t *, uint8_t **, size_t);
371
372 extern void *ecb_alloc_ctx(int);
373 extern void *cbc_alloc_ctx(int);
374 extern void *ctr_alloc_ctx(int);
375 extern void *ccm_alloc_ctx(int);
376 extern void *gcm_alloc_ctx(int);
377 extern void *gmac_alloc_ctx(int);
378 extern void crypto_free_mode_ctx(void *);
379 extern void gcm_set_kmflag(gcm_ctx_t *, int);
380
381 #ifdef  __cplusplus
382 }
383 #endif
384
385 #endif  /* _COMMON_CRYPTO_MODES_H */