1/*	$NetBSD: aes.c,v 1.7 2024/02/21 22:52:27 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*! \file isc/aes.c */
17
18#include <openssl/evp.h>
19#include <openssl/opensslv.h>
20
21#include <isc/aes.h>
22#include <isc/assertions.h>
23#include <isc/string.h>
24#include <isc/types.h>
25#include <isc/util.h>
26
27void
28isc_aes128_crypt(const unsigned char *key, const unsigned char *in,
29		 unsigned char *out) {
30	EVP_CIPHER_CTX *c;
31	int len;
32
33	c = EVP_CIPHER_CTX_new();
34	RUNTIME_CHECK(c != NULL);
35	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_128_ecb(), key, NULL) == 1);
36	EVP_CIPHER_CTX_set_padding(c, 0);
37	RUNTIME_CHECK(
38		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
39	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
40	EVP_CIPHER_CTX_free(c);
41}
42
43void
44isc_aes192_crypt(const unsigned char *key, const unsigned char *in,
45		 unsigned char *out) {
46	EVP_CIPHER_CTX *c;
47	int len;
48
49	c = EVP_CIPHER_CTX_new();
50	RUNTIME_CHECK(c != NULL);
51	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_192_ecb(), key, NULL) == 1);
52	EVP_CIPHER_CTX_set_padding(c, 0);
53	RUNTIME_CHECK(
54		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
55	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
56	EVP_CIPHER_CTX_free(c);
57}
58
59void
60isc_aes256_crypt(const unsigned char *key, const unsigned char *in,
61		 unsigned char *out) {
62	EVP_CIPHER_CTX *c;
63	int len;
64
65	c = EVP_CIPHER_CTX_new();
66	RUNTIME_CHECK(c != NULL);
67	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_256_ecb(), key, NULL) == 1);
68	EVP_CIPHER_CTX_set_padding(c, 0);
69	RUNTIME_CHECK(
70		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
71	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
72	EVP_CIPHER_CTX_free(c);
73}
74