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                 size_t 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                 size_t 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,
40                unsigned long nblocks, 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                 size_t key_len, const void *key, const void *iv);
52    void (*cfb)(cccfb_ctx *ctx, size_t nbytes, const void *in, void *out);
53    const void *custom;
54};
55
56/* CFB8 mode. */
57
58cc_aligned_struct(16) cccfb8_ctx;
59
60struct ccmode_cfb8 {
61    size_t size;        /* first argument to cccfb8_ctx_decl(). */
62    unsigned long block_size;
63    void (*init)(const struct ccmode_cfb8 *cfb8, cccfb8_ctx *ctx,
64                 size_t key_len, const void *key, const void *iv);
65    void (*cfb8)(cccfb8_ctx *ctx, size_t nbytes, const void *in, void *out);
66    const void *custom;
67};
68
69/* CTR mode. */
70
71cc_aligned_struct(16) ccctr_ctx;
72
73struct ccmode_ctr {
74    size_t size;        /* first argument to ccctr_ctx_decl(). */
75    unsigned long block_size;
76    void (*init)(const struct ccmode_ctr *ctr, ccctr_ctx *ctx,
77                 size_t key_len, const void *key, const void *iv);
78    void (*ctr)(ccctr_ctx *ctx, size_t nbytes, const void *in, void *out);
79    const void *custom;
80};
81
82/* OFB mode. */
83
84cc_aligned_struct(16) ccofb_ctx;
85
86struct ccmode_ofb {
87    size_t size;        /* first argument to ccofb_ctx_decl(). */
88    unsigned long block_size;
89    void (*init)(const struct ccmode_ofb *ofb, ccofb_ctx *ctx,
90                 size_t key_len, const void *key, const void *iv);
91    void (*ofb)(ccofb_ctx *ctx, size_t nbytes, const void *in, void *out);
92    const void *custom;
93};
94
95/* XTS mode. */
96
97cc_aligned_struct(16) ccxts_ctx;
98cc_aligned_struct(16) ccxts_tweak;
99
100struct ccmode_xts {
101    size_t size;        /* first argument to ccxts_ctx_decl(). */
102    size_t tweak_size;  /* first argument to ccxts_tweak_decl(). */
103    unsigned long block_size;
104
105    /* Create a xts key from a xts mode object.  The tweak_len here
106     determines how long the tweak is in bytes, for each subsequent call to
107     ccmode_xts->xts().
108     key must point to at least 'size' cc_units of free storage.
109     tweak_key must point to at least 'tweak_size' cc_units of free storage. */
110    void (*init)(const struct ccmode_xts *xts, ccxts_ctx *ctx,
111                 size_t key_len, const void *key, const void *tweak_key);
112
113    /* Set the tweak (sector number), the block within the sector zero. */
114    void (*set_tweak)(const ccxts_ctx *ctx, ccxts_tweak *tweak, const void *iv);
115
116    /* Encrypt blocks for a sector, clients must call set_tweak before calling
117       this function. Return a pointer to the tweak buffer */
118    void *(*xts)(const ccxts_ctx *ctx, ccxts_tweak *tweak,
119                 unsigned long nblocks, const void *in, void *out);
120
121    const void *custom;
122    const void *custom1;
123};
124
125/* GCM mode. */
126
127cc_aligned_struct(16) ccgcm_ctx;
128
129struct ccmode_gcm {
130    size_t size;        /* first argument to ccgcm_ctx_decl(). */
131    unsigned long block_size;
132    void (*init)(const struct ccmode_gcm *gcm, ccgcm_ctx *ctx,
133                 size_t key_len, const void *key);
134    void (*set_iv)(ccgcm_ctx *ctx, size_t iv_size, const void *iv);
135    void (*gmac)(ccgcm_ctx *ctx, size_t nbytes, const void *in);  // could just be gcm with NULL out
136    void (*gcm)(ccgcm_ctx *ctx, size_t nbytes, const void *in, void *out);
137    void (*finalize)(ccgcm_ctx *key, size_t tag_size, void *tag);
138    void (*reset)(ccgcm_ctx *ctx);
139    const void *custom;
140};
141
142/* GCM mode. */
143
144cc_aligned_struct(16) ccccm_ctx;
145cc_aligned_struct(16) ccccm_nonce;
146
147struct ccmode_ccm {
148    size_t size;        /* first argument to ccccm_ctx_decl(). */
149    size_t nonce_size;  /* first argument to ccccm_nonce_decl(). */
150    unsigned long block_size;
151    void (*init)(const struct ccmode_ccm *ccm, ccccm_ctx *ctx,
152                 size_t key_len, const void *key);
153    void (*set_iv)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nonce_len, const void *nonce,
154                   size_t mac_size, size_t auth_len, size_t data_len);
155    void (*cbcmac)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *in);  // could just be ccm with NULL out
156    void (*ccm)(ccccm_ctx *ctx, ccccm_nonce *nonce_ctx, size_t nbytes, const void *in, void *out);
157    void (*finalize)(ccccm_ctx *key, ccccm_nonce *nonce_ctx, void *mac);
158    void (*reset)(ccccm_ctx *key, ccccm_nonce *nonce_ctx);
159    const void *custom;
160};
161
162
163/* OMAC mode. */
164
165cc_aligned_struct(16) ccomac_ctx;
166
167struct ccmode_omac {
168    size_t size;        /* first argument to ccomac_ctx_decl(). */
169    unsigned long block_size;
170    void (*init)(const struct ccmode_omac *omac, ccomac_ctx *ctx,
171                 size_t tweak_len, size_t key_len, const void *key);
172    int (*omac)(ccomac_ctx *ctx, unsigned long nblocks,
173                const void *tweak, const void *in, void *out);
174    const void *custom;
175};
176
177#endif /* _CORECRYPTO_CCMODE_IMPL_H_ */
178