5 Copyright 2014 Apple Inc. All rights reserved.
8 #ifndef _CORECRYPTO_CCCHACHA20POLY1305_H_
9 #define _CORECRYPTO_CCCHACHA20POLY1305_H_
15 #define CCCHACHA20_KEY_NBYTES 32
16 #define CCCHACHA20_BLOCK_NBYTES 64
17 #define CCCHACHA20_BLOCK_NBITS (CCCHACHA20_BLOCK_NBYTES * 8)
18 #define CCCHACHA20_NONCE_NBYTES 12
22 uint8_t buffer
[CCCHACHA20_BLOCK_NBYTES
];
26 #define CCPOLY1305_TAG_NBYTES 16
29 uint32_t r0
, r1
, r2
, r3
, r4
;
30 uint32_t s1
, s2
, s3
, s4
;
31 uint32_t h0
, h1
, h2
, h3
, h4
;
39 @group ccchacha20poly1305
40 @abstract Encrypts and authenticates or decrypts and verifies data.
41 @discussion See RFC 7539 for details.
43 @warning The key-nonce pair must be unique per encryption.
45 @warning A single message can be at most (2^38 - 64) bytes in length.
47 The correct sequence of calls to encrypt is:
49 @code ccchacha20poly1305_init(...)
50 ccchacha20poly1305_setnonce(...)
51 ccchacha20poly1305_aad(...) (may be called zero or more times)
52 ccchacha20poly1305_encrypt(...) (may be called zero or more times)
53 ccchacha20poly1305_finalize(...)
55 To reuse the context for additional encryptions, follow this sequence:
57 @code ccchacha20poly1305_reset(...)
58 ccchacha20poly1305_setnonce(...)
59 ccchacha20poly1305_aad(...) (may be called zero or more times)
60 ccchacha20poly1305_encrypt(...) (may be called zero or more times)
61 ccchacha20poly1305_finalize(...)
63 To decrypt, follow this call sequence:
65 @code ccchacha20poly1305_init(...)
66 ccchacha20poly1305_setnonce(...)
67 ccchacha20poly1305_aad(...) (may be called zero or more times)
68 ccchacha20poly1305_decrypt(...) (may be called zero or more times)
69 ccchacha20poly1305_verify(...) (returns zero on successful decryption)
71 To reuse the context for additional encryptions, follow this sequence:
73 @code ccchacha20poly1305_reset(...)
74 ccchacha20poly1305_setnonce(...)
75 ccchacha20poly1305_aad(...) (may be called zero or more times)
76 ccchacha20poly1305_decrypt(...) (may be called zero or more times)
77 ccchacha20poly1305_verify(...) (returns zero on successful decryption)
80 #define CCCHACHA20POLY1305_KEY_NBYTES (CCCHACHA20_KEY_NBYTES)
81 #define CCCHACHA20POLY1305_NONCE_NBYTES (CCCHACHA20_NONCE_NBYTES)
82 #define CCCHACHA20POLY1305_TAG_NBYTES (CCPOLY1305_TAG_NBYTES)
84 /* (2^32 - 1) blocks */
85 /* (2^38 - 64) bytes */
86 /* (2^41 - 512) bits */
87 /* Exceeding this figure breaks confidentiality and authenticity. */
88 #define CCCHACHA20POLY1305_TEXT_MAX_NBYTES ((1ULL << 38) - 64ULL)
90 #define CCCHACHA20POLY1305_STATE_SETNONCE 1
91 #define CCCHACHA20POLY1305_STATE_AAD 2
92 #define CCCHACHA20POLY1305_STATE_ENCRYPT 3
93 #define CCCHACHA20POLY1305_STATE_DECRYPT 4
94 #define CCCHACHA20POLY1305_STATE_FINAL 5
97 ccchacha20_ctx chacha20_ctx
;
98 ccpoly1305_ctx poly1305_ctx
;
100 uint64_t text_nbytes
;
102 } ccchacha20poly1305_ctx
;
104 // This is just a stub right now.
105 // Eventually we will optimize by platform.
106 struct ccchacha20poly1305_info
{
110 const struct ccchacha20poly1305_info
*ccchacha20poly1305_info(void);
113 @function ccchacha20poly1305_init
114 @abstract Initialize a chacha20poly1305 context.
116 @param info Implementation descriptor
117 @param ctx Context for this instance
118 @param key Secret chacha20 key
120 @result 0 iff successful.
122 @discussion The key is 32 bytes in length.
124 @warning The key-nonce pair must be unique per encryption.
126 int ccchacha20poly1305_init(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *key
);
129 @function ccchacha20poly1305_reset
130 @abstract Reset a chacha20poly1305 context for reuse.
132 @param info Implementation descriptor
133 @param ctx Context for this instance
135 @result 0 iff successful.
137 int ccchacha20poly1305_reset(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
);
140 @function ccchacha20poly1305_setnonce
141 @abstract Set the nonce for encryption or decryption.
143 @param info Implementation descriptor
144 @param ctx Context for this instance
145 @param nonce Unique nonce per encryption
147 @result 0 iff successful.
149 @discussion The nonce is 12 bytes in length.
151 @warning The key-nonce pair must be unique per encryption.
153 int ccchacha20poly1305_setnonce(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *nonce
);
154 int ccchacha20poly1305_incnonce(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, uint8_t *nonce
);
157 @function ccchacha20poly1305_aad
158 @abstract Authenticate additional data.
160 @param info Descriptor for the mode
161 @param ctx Context for this instance
162 @param nbytes Length of the additional data in bytes
163 @param aad Additional data to authenticate
165 @result 0 iff successful.
167 @discussion This is typically used to authenticate data that cannot be encrypted (e.g. packet headers).
169 This function may be called zero or more times.
171 int ccchacha20poly1305_aad(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *aad
);
174 @function ccchacha20poly1305_encrypt
175 @abstract Encrypt data.
177 @param info Descriptor for the mode
178 @param ctx Context for this instance
179 @param nbytes Length of the plaintext in bytes
180 @param ptext Input plaintext
181 @param ctext Output ciphertext
183 @result 0 iff successful.
185 @discussion In-place processing is supported.
187 This function may be called zero or more times.
189 int ccchacha20poly1305_encrypt(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *ptext
, void *ctext
);
192 @function ccchacha20poly1305_finalize
193 @abstract Finalize encryption.
195 @param info Descriptor for the mode
196 @param ctx Context for this instance
197 @param tag Generated authentication tag
199 @result 0 iff successful.
201 @discussion The generated tag is 16 bytes in length.
203 int ccchacha20poly1305_finalize(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, uint8_t *tag
);
206 @function ccchacha20poly1305_decrypt
207 @abstract Decrypt data.
209 @param info Descriptor for the mode
210 @param ctx Context for this instance
211 @param nbytes Length of the ciphertext in bytes
212 @param ctext Input ciphertext
213 @param ptext Output plaintext
215 @result 0 iff successful.
217 @discussion In-place processing is supported.
219 This function may be called zero or more times.
221 int ccchacha20poly1305_decrypt(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, size_t nbytes
, const void *ctext
, void *ptext
);
224 @function ccchacha20poly1305_verify
225 @abstract Verify authenticity.
227 @param info Descriptor for the mode
228 @param ctx Context for this instance
229 @param tag Expected authentication tag
231 @result 0 iff authentic and otherwise successful.
233 @discussion The expected tag is 16 bytes in length.
235 int ccchacha20poly1305_verify(const struct ccchacha20poly1305_info
*info
, ccchacha20poly1305_ctx
*ctx
, const uint8_t *tag
);
238 @function ccchacha20poly1305_encrypt_oneshot
239 @abstract Encrypt with chacha20poly1305.
241 @param info Descriptor for the mode
242 @param key Secret chacha20 key
243 @param nonce Unique nonce per encryption
244 @param aad_nbytes Length of the additional data in bytes
245 @param aad Additional data to authenticate
246 @param ptext_nbytes Length of the plaintext in bytes
247 @param ptext Input plaintext
248 @param ctext Output ciphertext
249 @param tag Generated authentication tag
251 @discussion See RFC 7539 for details.
253 The key is 32 bytes in length.
255 The nonce is 12 bytes in length.
257 The generated tag is 16 bytes in length.
259 In-place processing is supported.
261 @warning The key-nonce pair must be unique per encryption.
263 @warning A single message can be at most (2^38 - 64) bytes in length.
265 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
);
268 @function ccchacha20poly1305_decrypt_oneshot
269 @abstract Decrypt with chacha20poly1305.
271 @param info Descriptor for the mode
272 @param key Secret chacha20 key
273 @param nonce Unique nonce per encryption
274 @param aad_nbytes Length of the additional data in bytes
275 @param aad Additional data to authenticate
276 @param ctext_nbytes Length of the ciphertext in bytes
277 @param ctext Input ciphertext
278 @param ptext Output plaintext
279 @param tag Expected authentication tag
281 @discussion See RFC 7539 for details.
283 The key is 32 bytes in length.
285 The nonce is 12 bytes in length.
287 The generated tag is 16 bytes in length.
289 In-place processing is supported.
291 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
);