1 /* $OpenBSD: e_chacha.c,v 1.4 2014/07/10 22:45:57 jsing Exp $ */
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <openssl/opensslconf.h>
20 #ifndef OPENSSL_NO_CHACHA
22 #include <openssl/chacha.h>
23 #include <openssl/evp.h>
24 #include <openssl/objects.h>
28 static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
29 const unsigned char *in, size_t len);
30 static int chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
31 const unsigned char *iv, int enc);
33 static const EVP_CIPHER chacha20_cipher = {
38 .flags = EVP_CIPH_STREAM_CIPHER,
40 .do_cipher = chacha_cipher,
41 .ctx_size = sizeof(ChaCha_ctx)
47 return (&chacha20_cipher);
51 chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
52 const unsigned char *iv, int enc)
54 ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key,
55 EVP_CIPHER_CTX_key_length(ctx) * 8);
57 ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL);
62 chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
65 ChaCha((ChaCha_ctx *)ctx->cipher_data, out, in, len);