e_chacha.c revision 1.1
1/*
2 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef OPENSSL_NO_CHACHA
18
19#include <openssl/chacha.h>
20#include <openssl/evp.h>
21#include <openssl/objects.h>
22
23#include "evp_locl.h"
24
25static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
26    const unsigned char *in, size_t len);
27static int chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28    const unsigned char *iv, int enc);
29
30static const EVP_CIPHER chacha20_cipher = {
31	.nid = NID_chacha20,
32	.block_size = 1,
33	.key_len = 32,
34	.iv_len = 8,
35	.flags = EVP_CIPH_STREAM_CIPHER,
36	.init = chacha_init,
37	.do_cipher = chacha_cipher,
38	.ctx_size = sizeof(ChaCha_ctx)
39};
40
41const EVP_CIPHER *
42EVP_chacha20(void)
43{
44	return(&chacha20_cipher);
45}
46
47static int
48chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
49    const unsigned char *iv, int enc)
50{
51	ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key,
52	    EVP_CIPHER_CTX_key_length(ctx) * 8);
53	ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL);
54	return 1;
55}
56
57static int
58chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in,
59    size_t len)
60{
61	ChaCha((ChaCha_ctx *)ctx->cipher_data, out, in, len);
62	return 1;
63}
64
65#endif
66