e_chacha.c revision 1.3
1/* $OpenBSD: e_chacha.c,v 1.3 2014/06/12 15:49:29 deraadt Exp $ */ 2/* 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 4 * 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. 8 * 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. 16 */ 17 18#ifndef OPENSSL_NO_CHACHA 19 20#include <openssl/chacha.h> 21#include <openssl/evp.h> 22#include <openssl/objects.h> 23 24#include "evp_locl.h" 25 26static int chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 27 const unsigned char *in, size_t len); 28static int chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, 29 const unsigned char *iv, int enc); 30 31static const EVP_CIPHER chacha20_cipher = { 32 .nid = NID_chacha20, 33 .block_size = 1, 34 .key_len = 32, 35 .iv_len = 8, 36 .flags = EVP_CIPH_STREAM_CIPHER, 37 .init = chacha_init, 38 .do_cipher = chacha_cipher, 39 .ctx_size = sizeof(ChaCha_ctx) 40}; 41 42const EVP_CIPHER * 43EVP_chacha20(void) 44{ 45 return (&chacha20_cipher); 46} 47 48static int 49chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, 50 const unsigned char *iv, int enc) 51{ 52 ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key, 53 EVP_CIPHER_CTX_key_length(ctx) * 8); 54 ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL); 55 return 1; 56} 57 58static int 59chacha_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, 60 size_t len) 61{ 62 ChaCha((ChaCha_ctx *)ctx->cipher_data, out, in, len); 63 return 1; 64} 65 66#endif 67