xref: /xnu-11417.140.69/libkern/crypto/corecrypto_chacha20poly1305.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2017 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <corecrypto/ccchacha20poly1305.h>
30 #include <libkern/crypto/crypto_internal.h>
31 #include <libkern/crypto/chacha20poly1305.h>
32 
33 static ccchacha20poly1305_fns_t
fns(void)34 fns(void)
35 {
36 	return g_crypto_funcs->ccchacha20poly1305_fns;
37 }
38 
39 static const struct ccchacha20poly1305_info *
info(void)40 info(void)
41 {
42 	return fns()->info();
43 }
44 
45 int
chacha20poly1305_init(chacha20poly1305_ctx * ctx,const uint8_t * key)46 chacha20poly1305_init(chacha20poly1305_ctx *ctx, const uint8_t *key)
47 {
48 	return fns()->init(info(), ctx, key);
49 }
50 
51 int
chacha20poly1305_reset(chacha20poly1305_ctx * ctx)52 chacha20poly1305_reset(chacha20poly1305_ctx *ctx)
53 {
54 	return fns()->reset(info(), ctx);
55 }
56 
57 int
chacha20poly1305_setnonce(chacha20poly1305_ctx * ctx,const uint8_t * nonce)58 chacha20poly1305_setnonce(chacha20poly1305_ctx *ctx, const uint8_t *nonce)
59 {
60 	return fns()->setnonce(info(), ctx, nonce);
61 }
62 
63 int
chacha20poly1305_incnonce(chacha20poly1305_ctx * ctx,uint8_t * nonce)64 chacha20poly1305_incnonce(chacha20poly1305_ctx *ctx, uint8_t *nonce)
65 {
66 	return fns()->incnonce(info(), ctx, nonce);
67 }
68 
69 int
chacha20poly1305_aad(chacha20poly1305_ctx * ctx,size_t nbytes,const void * aad)70 chacha20poly1305_aad(chacha20poly1305_ctx *ctx, size_t nbytes, const void *aad)
71 {
72 	return fns()->aad(info(), ctx, nbytes, aad);
73 }
74 
75 int
chacha20poly1305_encrypt(chacha20poly1305_ctx * ctx,size_t nbytes,const void * ptext,void * ctext)76 chacha20poly1305_encrypt(chacha20poly1305_ctx *ctx, size_t nbytes, const void *ptext, void *ctext)
77 {
78 	return fns()->encrypt(info(), ctx, nbytes, ptext, ctext);
79 }
80 
81 int
chacha20poly1305_finalize(chacha20poly1305_ctx * ctx,uint8_t * tag)82 chacha20poly1305_finalize(chacha20poly1305_ctx *ctx, uint8_t *tag)
83 {
84 	return fns()->finalize(info(), ctx, tag);
85 }
86 
87 int
chacha20poly1305_decrypt(chacha20poly1305_ctx * ctx,size_t nbytes,const void * ctext,void * ptext)88 chacha20poly1305_decrypt(chacha20poly1305_ctx *ctx, size_t nbytes, const void *ctext, void *ptext)
89 {
90 	return fns()->decrypt(info(), ctx, nbytes, ctext, ptext);
91 }
92 
93 int
chacha20poly1305_verify(chacha20poly1305_ctx * ctx,const uint8_t * tag)94 chacha20poly1305_verify(chacha20poly1305_ctx *ctx, const uint8_t *tag)
95 {
96 	return fns()->verify(info(), ctx, tag);
97 }
98