xref: /freebsd/crypto/openssh/openbsd-compat/libressl-api-compat.c (revision 535af610a4fdace6d50960c0ad9be0597eea7a1b)
12a01feabSEd Maste /*
2*535af610SEd Maste  * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
32a01feabSEd Maste  *
42a01feabSEd Maste  * Permission to use, copy, modify, and distribute this software for any
52a01feabSEd Maste  * purpose with or without fee is hereby granted, provided that the above
62a01feabSEd Maste  * copyright notice and this permission notice appear in all copies.
72a01feabSEd Maste  *
82a01feabSEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
92a01feabSEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
102a01feabSEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
112a01feabSEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
122a01feabSEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
132a01feabSEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
142a01feabSEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
152a01feabSEd Maste  */
162a01feabSEd Maste 
172a01feabSEd Maste #include "includes.h"
182a01feabSEd Maste 
192a01feabSEd Maste #ifdef WITH_OPENSSL
202a01feabSEd Maste 
212a01feabSEd Maste #include <sys/types.h>
222a01feabSEd Maste 
232a01feabSEd Maste #include <stdlib.h>
242a01feabSEd Maste #include <string.h>
252a01feabSEd Maste 
262a01feabSEd Maste #include <openssl/evp.h>
272a01feabSEd Maste 
282a01feabSEd Maste #ifndef HAVE_EVP_CIPHER_CTX_GET_IV
292a01feabSEd Maste int
EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX * ctx,unsigned char * iv,size_t len)302a01feabSEd Maste EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
312a01feabSEd Maste {
322a01feabSEd Maste 	if (ctx == NULL)
332a01feabSEd Maste 		return 0;
342a01feabSEd Maste 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
352a01feabSEd Maste 		return 0;
362a01feabSEd Maste 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
372a01feabSEd Maste 		return 0;
382a01feabSEd Maste 	if (len > EVP_MAX_IV_LENGTH)
392a01feabSEd Maste 		return 0; /* sanity check; shouldn't happen */
402a01feabSEd Maste 	/*
412a01feabSEd Maste 	 * Skip the memcpy entirely when the requested IV length is zero,
422a01feabSEd Maste 	 * since the iv pointer may be NULL or invalid.
432a01feabSEd Maste 	 */
442a01feabSEd Maste 	if (len != 0) {
452a01feabSEd Maste 		if (iv == NULL)
462a01feabSEd Maste 			return 0;
472a01feabSEd Maste # ifdef HAVE_EVP_CIPHER_CTX_IV
482a01feabSEd Maste 		memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
492a01feabSEd Maste # else
502a01feabSEd Maste 		memcpy(iv, ctx->iv, len);
512a01feabSEd Maste # endif /* HAVE_EVP_CIPHER_CTX_IV */
522a01feabSEd Maste 	}
532a01feabSEd Maste 	return 1;
542a01feabSEd Maste }
552a01feabSEd Maste #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
562a01feabSEd Maste 
572a01feabSEd Maste #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
582a01feabSEd Maste int
EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX * ctx,const unsigned char * iv,size_t len)592a01feabSEd Maste EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
602a01feabSEd Maste {
612a01feabSEd Maste 	if (ctx == NULL)
622a01feabSEd Maste 		return 0;
632a01feabSEd Maste 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
642a01feabSEd Maste 		return 0;
652a01feabSEd Maste 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
662a01feabSEd Maste 		return 0;
672a01feabSEd Maste 	if (len > EVP_MAX_IV_LENGTH)
682a01feabSEd Maste 		return 0; /* sanity check; shouldn't happen */
692a01feabSEd Maste 	/*
702a01feabSEd Maste 	 * Skip the memcpy entirely when the requested IV length is zero,
712a01feabSEd Maste 	 * since the iv pointer may be NULL or invalid.
722a01feabSEd Maste 	 */
732a01feabSEd Maste 	if (len != 0) {
742a01feabSEd Maste 		if (iv == NULL)
752a01feabSEd Maste 			return 0;
762a01feabSEd Maste # ifdef HAVE_EVP_CIPHER_CTX_IV_NOCONST
772a01feabSEd Maste 		memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, len);
782a01feabSEd Maste # else
792a01feabSEd Maste 		memcpy(ctx->iv, iv, len);
802a01feabSEd Maste # endif /* HAVE_EVP_CIPHER_CTX_IV_NOCONST */
812a01feabSEd Maste 	}
822a01feabSEd Maste 	return 1;
832a01feabSEd Maste }
842a01feabSEd Maste #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
852a01feabSEd Maste 
862a01feabSEd Maste #endif /* WITH_OPENSSL */
87