1/*
2 *  ccmode_impl.h
3 *  corecrypto
4 *
5 *  Created by James Murphy on 12/9/11.
6 *  Copyright (c) 2011 Apple Inc. All rights reserved.
7 *
8 */
9
10#ifndef _CORECRYPTO_CCMODE_IMPL_H_
11#define _CORECRYPTO_CCMODE_IMPL_H_
12
13#include <corecrypto/cc.h>
14
15/* ECB mode. */
16cc_aligned_struct(16) ccecb_ctx;
17
18
19/* Actual symmetric algorithm implementation should provide you one of these. */
20struct ccmode_ecb {
21    size_t size;        /* first argument to ccecb_ctx_decl(). */
22    unsigned long block_size;
23    void (*init)(const struct ccmode_ecb *ecb, ccecb_ctx *ctx,
24                 unsigned long key_len, const void *key);
25    void (*ecb)(const ccecb_ctx *ctx, unsigned long nblocks, const void *in,
26                void *out);
27};
28
29/* CBC mode. */
30cc_aligned_struct(16) cccbc_ctx;
31cc_aligned_struct(16) cccbc_iv;
32
33struct ccmode_cbc {
34    size_t size;        /* first argument to cccbc_ctx_decl(). */
35    unsigned long block_size;
36    void (*init)(const struct ccmode_cbc *cbc, cccbc_ctx *ctx,
37                     unsigned long key_len, const void *key);
38    /* cbc encrypt or decrypt nblocks from in to out, iv will be used and updated. */
39    void (*cbc)(const cccbc_ctx *ctx, cccbc_iv *iv, unsigned long nblocks,
40                const void *in, void *out);
41    const void *custom;
42};
43
44/* CFB mode. */
45cc_aligned_struct(16) cccfb_ctx;
46
47struct ccmode_cfb {
48    size_t size;        /* first argument to cccfb_ctx_decl(). */
49    unsigned long block_size;
50    void (*init)(const struct ccmode_cfb *cfb, cccfb_ctx *ctx,
51                 unsigned long key_len, const void *key,
52                 const void *iv);
53    void (*cfb)(cccfb_ctx *ctx, unsigned long nblocks,
54                const void *in, void *out);
55    const void *custom;
56};
57
58/* CFB8 mode. */
59
60cc_aligned_struct(16) cccfb8_ctx;
61
62struct ccmode_cfb8 {
63    size_t size;        /* first argument to cccfb8_ctx_decl(). */
64    unsigned long block_size;
65    void (*init)(const struct ccmode_cfb8 *cfb8, cccfb8_ctx *ctx,
66                 unsigned long key_len, const void *key,
67                 const void *iv);
68    void (*cfb8)(cccfb8_ctx *ctx, unsigned long nbytes,
69                 const void *in, void *out);
70    const void *custom;
71};
72
73/* CTR mode. */
74
75cc_aligned_struct(16) ccctr_ctx;
76
77struct ccmode_ctr {
78    size_t size;        /* first argument to ccctr_ctx_decl(). */
79    unsigned long block_size;
80    void (*init)(const struct ccmode_ctr *ctr, ccctr_ctx *ctx,
81                 unsigned long key_len, const void *key,
82                 const void *iv);
83    void (*ctr)(ccctr_ctx *ctx, unsigned long nblocks,
84                const void *in, void *out);
85    const void *custom;
86};
87
88/* OFB mode. */
89
90cc_aligned_struct(16) ccofb_ctx;
91
92struct ccmode_ofb {
93    size_t size;        /* first argument to ccofb_ctx_decl(). */
94    unsigned long block_size;
95    void (*init)(const struct ccmode_ofb *ofb, ccofb_ctx *ctx,
96                 unsigned long key_len, const void *key,
97                 const void *iv);
98    void (*ofb)(ccofb_ctx *ctx, unsigned long nblocks,
99                const void *in, void *out);
100    const void *custom;
101};
102
103/* XTS mode. */
104
105cc_aligned_struct(16) ccxts_ctx;
106cc_aligned_struct(16) ccxts_tweak;
107
108struct ccmode_xts {
109    size_t size;        /* first argument to ccxts_ctx_decl(). */
110    size_t tweak_size;  /* first argument to ccxts_tweak_decl(). */
111    unsigned long block_size;
112
113    /* Create a xts key from a xts mode object.  The tweak_len here
114     determines how long the tweak is in bytes, for each subsequent call to
115     ccmode_xts->xts().
116     key must point to at least 'size' cc_units of free storage.
117     tweak_key must point to at least 'tweak_size' cc_units of free storage. */
118    void (*init)(const struct ccmode_xts *xts, ccxts_ctx *ctx,
119                 unsigned long key_len, const void *key,
120                 const void *tweak_key);
121
122    /* Set the tweak (sector number), the block within the sector zero. */
123    void (*set_tweak)(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv);
124
125    /* Encrypt blocks for a sector, clients must call set_tweak before calling
126       this function. Return a pointer to the tweak buffer */
127    void *(*xts)(const ccxts_ctx *ctx, ccxts_tweak *tweak, unsigned long nblocks,
128                    const void *in, void *out);
129
130    const void *custom;
131    const void *custom1;
132};
133
134/* GCM mode. */
135
136cc_aligned_struct(16) ccgcm_ctx;
137
138struct ccmode_gcm {
139    size_t size;        /* first argument to ccgcm_ctx_decl(). */
140    unsigned long block_size;
141    void (*init)(const struct ccmode_gcm *gcm, ccgcm_ctx *ctx,
142                 unsigned long key_len, const void *key);
143    void (*set_iv)(ccgcm_ctx *ctx, size_t iv_size, const void *iv);
144    void (*gmac)(ccgcm_ctx *ctx, unsigned long nbytes, const void *in);  // could just be gcm with NULL out
145    void (*gcm)(ccgcm_ctx *ctx, unsigned long nbytes, const void *in, void *out);
146    void (*finalize)(ccgcm_ctx *key, size_t tag_size, void *tag);
147    void (*reset)(ccgcm_ctx *ctx);
148    const void *custom;
149};
150
151/* OMAC mode. */
152
153cc_aligned_struct(16) ccomac_ctx;
154
155struct ccmode_omac {
156    size_t size;        /* first argument to ccomac_ctx_decl(). */
157    unsigned long block_size;
158    void (*init)(const struct ccmode_omac *omac, ccomac_ctx *ctx,
159                 unsigned long tweak_len, unsigned long key_len,
160                 const void *key);
161    int (*omac)(ccomac_ctx *ctx, unsigned long nblocks,
162                const void *tweak, const void *in, void *out);
163    const void *custom;
164};
165
166#endif /* _CORECRYPTO_CCMODE_IMPL_H_ */
167