1 /* Copyright (c) (2016,2017,2018,2019) Apple Inc. All rights reserved.
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
12 #ifndef _CORECRYPTO_CCCHACHA20POLY1305_H_
13 #define _CORECRYPTO_CCCHACHA20POLY1305_H_
19 #define CCCHACHA20_KEY_NBYTES 32
20 #define CCCHACHA20_BLOCK_NBYTES 64
21 #define CCCHACHA20_BLOCK_NBITS (CCCHACHA20_BLOCK_NBYTES * 8)
22 #define CCCHACHA20_NONCE_NBYTES 12
26 uint8_t buffer
[CCCHACHA20_BLOCK_NBYTES
];
30 #define CCPOLY1305_TAG_NBYTES 16
33 uint32_t r0
, r1
, r2
, r3
, r4
;
34 uint32_t s1
, s2
, s3
, s4
;
35 uint32_t h0
, h1
, h2
, h3
, h4
;
43 @group ccchacha20poly1305
44 @abstract Encrypts and authenticates or decrypts and verifies data.
45 @discussion See RFC 7539 for details.
47 @warning The key-nonce pair must be unique per encryption.
49 @warning A single message can be at most (2^38 - 64) bytes in length.
51 The correct sequence of calls to encrypt is:
53 @code ccchacha20poly1305_init(...)
54 ccchacha20poly1305_setnonce(...)
55 ccchacha20poly1305_aad(...) (may be called zero or more times)
56 ccchacha20poly1305_encrypt(...) (may be called zero or more times)
57 ccchacha20poly1305_finalize(...)
59 To reuse the context for additional encryptions, follow this sequence:
61 @code ccchacha20poly1305_reset(...)
62 ccchacha20poly1305_setnonce(...)
63 ccchacha20poly1305_aad(...) (may be called zero or more times)
64 ccchacha20poly1305_encrypt(...) (may be called zero or more times)
65 ccchacha20poly1305_finalize(...)
67 To decrypt, follow this call sequence:
69 @code ccchacha20poly1305_init(...)
70 ccchacha20poly1305_setnonce(...)
71 ccchacha20poly1305_aad(...) (may be called zero or more times)
72 ccchacha20poly1305_decrypt(...) (may be called zero or more times)
73 ccchacha20poly1305_verify(...) (returns zero on successful decryption)
75 To reuse the context for additional encryptions, follow this sequence:
77 @code ccchacha20poly1305_reset(...)
78 ccchacha20poly1305_setnonce(...)
79 ccchacha20poly1305_aad(...) (may be called zero or more times)
80 ccchacha20poly1305_decrypt(...) (may be called zero or more times)
81 ccchacha20poly1305_verify(...) (returns zero on successful decryption)
84 #define CCCHACHA20POLY1305_KEY_NBYTES (CCCHACHA20_KEY_NBYTES)
85 #define CCCHACHA20POLY1305_NONCE_NBYTES (CCCHACHA20_NONCE_NBYTES)
86 #define CCCHACHA20POLY1305_TAG_NBYTES (CCPOLY1305_TAG_NBYTES)
88 /* (2^32 - 1) blocks */
89 /* (2^38 - 64) bytes */
90 /* (2^41 - 512) bits */
91 /* Exceeding this figure breaks confidentiality and authenticity. */
92 #define CCCHACHA20POLY1305_TEXT_MAX_NBYTES ((1ULL << 38) - 64ULL)
94 #define CCCHACHA20POLY1305_STATE_SETNONCE 1
95 #define CCCHACHA20POLY1305_STATE_AAD 2
96 #define CCCHACHA20POLY1305_STATE_ENCRYPT 3
97 #define CCCHACHA20POLY1305_STATE_DECRYPT 4
98 #define CCCHACHA20POLY1305_STATE_FINAL 5
101 ccchacha20_ctx chacha20_ctx
;
102 ccpoly1305_ctx poly1305_ctx
;
104 uint64_t text_nbytes
;
106 } ccchacha20poly1305_ctx
;
108 // This is just a stub right now.
109 // Eventually we will optimize by platform.
110 struct ccchacha20poly1305_info
{
114 const struct ccchacha20poly1305_info
*ccchacha20poly1305_info(void);
117 @function ccchacha20poly1305_init
118 @abstract Initialize a chacha20poly1305 context.
120 @param info Implementation descriptor
121 @param ctx Context for this instance
122 @param key Secret chacha20 key
124 @result 0 iff successful.
126 @discussion The key is 32 bytes in length.
128 @warning The key-nonce pair must be unique per encryption.
130 int ccchacha20poly1305_init(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *key
);
133 @function ccchacha20poly1305_reset
134 @abstract Reset a chacha20poly1305 context for reuse.
136 @param info Implementation descriptor
137 @param ctx Context for this instance
139 @result 0 iff successful.
141 int ccchacha20poly1305_reset(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
);
144 @function ccchacha20poly1305_setnonce
145 @abstract Set the nonce for encryption or decryption.
147 @param info Implementation descriptor
148 @param ctx Context for this instance
149 @param nonce Unique nonce per encryption
151 @result 0 iff successful.
153 @discussion The nonce is 12 bytes in length.
155 @warning The key-nonce pair must be unique per encryption.
157 int ccchacha20poly1305_setnonce(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *nonce
);
158 int ccchacha20poly1305_incnonce(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, uint8_t *nonce
);
161 @function ccchacha20poly1305_aad
162 @abstract Authenticate additional data.
164 @param info Descriptor for the mode
165 @param ctx Context for this instance
166 @param nbytes Length of the additional data in bytes
167 @param aad Additional data to authenticate
169 @result 0 iff successful.
171 @discussion This is typically used to authenticate data that cannot be encrypted (e.g. packet headers).
173 This function may be called zero or more times.
175 int ccchacha20poly1305_aad(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *aad
);
178 @function ccchacha20poly1305_encrypt
179 @abstract Encrypt data.
181 @param info Descriptor for the mode
182 @param ctx Context for this instance
183 @param nbytes Length of the plaintext in bytes
184 @param ptext Input plaintext
185 @param ctext Output ciphertext
187 @result 0 iff successful.
189 @discussion In-place processing is supported.
191 This function may be called zero or more times.
193 int ccchacha20poly1305_encrypt(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *ptext
, void *ctext
);
196 @function ccchacha20poly1305_finalize
197 @abstract Finalize encryption.
199 @param info Descriptor for the mode
200 @param ctx Context for this instance
201 @param tag Generated authentication tag
203 @result 0 iff successful.
205 @discussion The generated tag is 16 bytes in length.
207 int ccchacha20poly1305_finalize(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, uint8_t *tag
);
210 @function ccchacha20poly1305_decrypt
211 @abstract Decrypt data.
213 @param info Descriptor for the mode
214 @param ctx Context for this instance
215 @param nbytes Length of the ciphertext in bytes
216 @param ctext Input ciphertext
217 @param ptext Output plaintext
219 @result 0 iff successful.
221 @discussion In-place processing is supported.
223 This function may be called zero or more times.
225 int ccchacha20poly1305_decrypt(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *ctext
, void *ptext
);
228 @function ccchacha20poly1305_verify
229 @abstract Verify authenticity.
231 @param info Descriptor for the mode
232 @param ctx Context for this instance
233 @param tag Expected authentication tag
235 @result 0 iff authentic and otherwise successful.
237 @discussion The expected tag is 16 bytes in length.
239 int ccchacha20poly1305_verify(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *tag
);
242 @function ccchacha20poly1305_encrypt_oneshot
243 @abstract Encrypt with chacha20poly1305.
245 @param info Descriptor for the mode
246 @param key Secret chacha20 key
247 @param nonce Unique nonce per encryption
248 @param aad_nbytes Length of the additional data in bytes
249 @param aad Additional data to authenticate
250 @param ptext_nbytes Length of the plaintext in bytes
251 @param ptext Input plaintext
252 @param ctext Output ciphertext
253 @param tag Generated authentication tag
255 @discussion See RFC 7539 for details.
257 The key is 32 bytes in length.
259 The nonce is 12 bytes in length.
261 The generated tag is 16 bytes in length.
263 In-place processing is supported.
265 @warning The key-nonce pair must be unique per encryption.
267 @warning A single message can be at most (2^38 - 64) bytes in length.
269 int ccchacha20poly1305_encrypt_oneshot(const struct ccchacha20poly1305_info
*info
, const uint8_t *key
, const uint8_t *nonce
, size_t aad_nbytes
, const void *aad
, size_t ptext_nbytes
, const void *ptext
, void *ctext
, uint8_t *tag
);
272 @function ccchacha20poly1305_decrypt_oneshot
273 @abstract Decrypt with chacha20poly1305.
275 @param info Descriptor for the mode
276 @param key Secret chacha20 key
277 @param nonce Unique nonce per encryption
278 @param aad_nbytes Length of the additional data in bytes
279 @param aad Additional data to authenticate
280 @param ctext_nbytes Length of the ciphertext in bytes
281 @param ctext Input ciphertext
282 @param ptext Output plaintext
283 @param tag Expected authentication tag
285 @discussion See RFC 7539 for details.
287 The key is 32 bytes in length.
289 The nonce is 12 bytes in length.
291 The generated tag is 16 bytes in length.
293 In-place processing is supported.
295 int ccchacha20poly1305_decrypt_oneshot(const struct ccchacha20poly1305_info
*info
, const uint8_t *key
, const uint8_t *nonce
, size_t aad_nbytes
, const void *aad
, size_t ctext_nbytes
, const void *ctext
, void *ptext
, const uint8_t *tag
);