Path: blob/main/sys/contrib/openzfs/module/icp/illumos-crypto.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License, Version 1.0 only6* (the "License"). You may not use this file except in compliance7* with the License.8*9* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE10* or https://opensource.org/licenses/CDDL-1.0.11* See the License for the specific language governing permissions12* and limitations under the License.13*14* When distributing Covered Code, include this CDDL HEADER in each15* file and include the License file at usr/src/OPENSOLARIS.LICENSE.16* If applicable, add the following below this CDDL HEADER, with the17* fields enclosed by brackets "[]" replaced with your own identifying18* information: Portions Copyright [yyyy] [name of copyright owner]19*20* CDDL HEADER END21*/22/*23* Copyright (c) 2017, Datto, Inc. All rights reserved.24*/2526#ifdef _KERNEL27#include <linux/module.h>28#include <linux/kernel.h>29#include <linux/init.h>30#else31#define __exit32#define __init33#endif3435#include <sys/crypto/common.h>36#include <sys/crypto/api.h>37#include <sys/crypto/impl.h>38#include <sys/crypto/sched_impl.h>39#include <sys/crypto/icp.h>4041/*42* Changes made to the original Illumos Crypto Layer for the ICP:43*44* Several changes were needed to allow the Illumos Crypto Layer45* to work in the Linux kernel. Almost all of the changes fall into46* one of the following categories:47*48* 1) Moving the syntax to the C90: This was mostly a matter of49* changing func() definitions to func(void). In a few cases,50* initializations of structs with unions needed to have brackets51* added.52*53* 2) Changes to allow userspace compilation: The ICP is meant to be54* compiled and used in both userspace and kernel space (for ztest and55* libzfs), so the _KERNEL macros did not make sense anymore. For the56* same reason, many header includes were also changed to use57* sys/zfs_context.h58*59* 3) Moving to a statically compiled architecture: At some point in60* the future it may make sense to have encryption algorithms that are61* loadable into the ICP at runtime via separate kernel modules.62* However, considering that this code will probably not see much use63* outside of zfs and zfs encryption only requires a select few64* algorithms it seemed like more trouble than it was worth to port over65* Illumos's kernel module structure to a Linux kernel module. In66* addition, The Illumos code related to keeping track of kernel modules67* is very much tied to the Illumos OS and proved difficult to port.68* Therefore, the structure of the ICP was simplified to work69* statically and all the Illumos kernel module loading subsystem was removed.70* All module initialization and destruction is now called in this file71* during kernel module loading and unloading.72*73* 4) Adding destructors: The Illumos Crypto Layer is built into74* the Illumos kernel and is not meant to be unloaded. Some destructors75* were added to allow the ICP to be unloaded without leaking76* structures.77*78* 5) Removing CRYPTO_DATA_MBLK related structures and code:79* crypto_data_t can have 3 formats, CRYPTO_DATA_RAW, CRYPTO_DATA_UIO,80* and CRYPTO_DATA_MBLK. ZFS only requires the first 2 formats, as the81* last one is related to streamed data. To simplify the port, code82* related to this format was removed.83*84* 6) Changes for architecture specific code: Some changes were needed85* to make architecture specific assembly compile. The biggest change86* here was to functions related to detecting CPU capabilities for amd64.87* The Illumos Crypto Layer used called into the Illumos kernel's API88* to discover these. They have been converted to instead use the89* 'cpuid' instruction as per the Intel spec. In addition, references to90* the sun4u' and sparc architectures have been removed so that these91* will use the generic implementation.92*93* 7) Removing sha384 and sha512 code: The sha code was actually very94* easy to port. However, the generic sha384 and sha512 code actually95* exceeds the stack size on arm and powerpc architectures. In an effort96* to remove warnings, this code was removed.97*98* 8) Change large allocations from kmem_alloc() to vmem_alloc(): In99* testing the ICP with the ZFS encryption code, a few allocations were100* found that could potentially be very large. These caused the SPL to101* throw warnings and so they were changed to use vmem_alloc().102*103* 9) Makefiles: Makefiles were added that would work with the existing104* ZFS Makefiles.105*/106107void108icp_fini(void)109{110sha2_mod_fini();111aes_mod_fini();112kcf_sched_destroy();113kcf_prov_tab_destroy();114kcf_destroy_mech_tabs();115}116117/* roughly equivalent to kcf.c: _init() */118int __init119icp_init(void)120{121/* initialize the mechanisms tables supported out-of-the-box */122kcf_init_mech_tabs();123124/* initialize the providers tables */125kcf_prov_tab_init();126127/*128* Initialize scheduling structures. Note that this does NOT129* start any threads since it might not be safe to do so.130*/131kcf_sched_init();132133/* initialize algorithms */134aes_mod_init();135sha2_mod_init();136137return (0);138}139140141