1/*	$NetBSD: aes.c,v 1.1 2024/02/18 20:57:48 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/platform.h>
24#include <isc/string.h>
25#include <isc/types.h>
26#include <isc/util.h>
27
28void
29isc_aes128_crypt(const unsigned char *key, const unsigned char *in,
30		 unsigned char *out) {
31	EVP_CIPHER_CTX *c;
32	int len;
33
34	c = EVP_CIPHER_CTX_new();
35	RUNTIME_CHECK(c != NULL);
36	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_128_ecb(), key, NULL) == 1);
37	EVP_CIPHER_CTX_set_padding(c, 0);
38	RUNTIME_CHECK(
39		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
40	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
41	EVP_CIPHER_CTX_free(c);
42}
43
44void
45isc_aes192_crypt(const unsigned char *key, const unsigned char *in,
46		 unsigned char *out) {
47	EVP_CIPHER_CTX *c;
48	int len;
49
50	c = EVP_CIPHER_CTX_new();
51	RUNTIME_CHECK(c != NULL);
52	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_192_ecb(), key, NULL) == 1);
53	EVP_CIPHER_CTX_set_padding(c, 0);
54	RUNTIME_CHECK(
55		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
56	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
57	EVP_CIPHER_CTX_free(c);
58}
59
60void
61isc_aes256_crypt(const unsigned char *key, const unsigned char *in,
62		 unsigned char *out) {
63	EVP_CIPHER_CTX *c;
64	int len;
65
66	c = EVP_CIPHER_CTX_new();
67	RUNTIME_CHECK(c != NULL);
68	RUNTIME_CHECK(EVP_EncryptInit(c, EVP_aes_256_ecb(), key, NULL) == 1);
69	EVP_CIPHER_CTX_set_padding(c, 0);
70	RUNTIME_CHECK(
71		EVP_EncryptUpdate(c, out, &len, in, ISC_AES_BLOCK_LENGTH) == 1);
72	RUNTIME_CHECK(len == ISC_AES_BLOCK_LENGTH);
73	EVP_CIPHER_CTX_free(c);
74}
75