1a9643ea8Slogwang#- 2a9643ea8Slogwang# Copyright (c) 2006, Sam Leffler 3a9643ea8Slogwang# All rights reserved. 4a9643ea8Slogwang# 5a9643ea8Slogwang# Redistribution and use in source and binary forms, with or without 6a9643ea8Slogwang# modification, are permitted provided that the following conditions 7a9643ea8Slogwang# are met: 8a9643ea8Slogwang# 1. Redistributions of source code must retain the above copyright 9a9643ea8Slogwang# notice, this list of conditions and the following disclaimer. 10a9643ea8Slogwang# 2. Redistributions in binary form must reproduce the above copyright 11a9643ea8Slogwang# notice, this list of conditions and the following disclaimer in the 12a9643ea8Slogwang# documentation and/or other materials provided with the distribution. 13a9643ea8Slogwang# 14a9643ea8Slogwang# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15a9643ea8Slogwang# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a9643ea8Slogwang# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17a9643ea8Slogwang# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18a9643ea8Slogwang# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19a9643ea8Slogwang# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20a9643ea8Slogwang# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21a9643ea8Slogwang# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22a9643ea8Slogwang# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23a9643ea8Slogwang# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24a9643ea8Slogwang# SUCH DAMAGE. 25a9643ea8Slogwang# 26a9643ea8Slogwang# $FreeBSD$ 27a9643ea8Slogwang# 28a9643ea8Slogwang 29a9643ea8Slogwang#include <sys/malloc.h> 30a9643ea8Slogwang#include <opencrypto/cryptodev.h> 31a9643ea8Slogwang 32a9643ea8SlogwangINTERFACE cryptodev; 33a9643ea8Slogwang 34*22ce4affSfengbojiangCODE { 35*22ce4affSfengbojiang static int null_freesession(device_t dev, 36*22ce4affSfengbojiang crypto_session_t crypto_session) 37*22ce4affSfengbojiang { 38*22ce4affSfengbojiang return 0; 39*22ce4affSfengbojiang } 40*22ce4affSfengbojiang}; 41*22ce4affSfengbojiang 42*22ce4affSfengbojiang/** 43*22ce4affSfengbojiang * @brief Probe to see if a crypto driver supports a session. 44*22ce4affSfengbojiang * 45*22ce4affSfengbojiang * The crypto framework invokes this method on each crypto driver when 46*22ce4affSfengbojiang * creating a session for symmetric crypto operations to determine if 47*22ce4affSfengbojiang * the driver supports the algorithms and mode requested by the 48*22ce4affSfengbojiang * session. 49*22ce4affSfengbojiang * 50*22ce4affSfengbojiang * If the driver does not support a session with the requested 51*22ce4affSfengbojiang * parameters, this function should fail with an error. 52*22ce4affSfengbojiang * 53*22ce4affSfengbojiang * If the driver does support a session with the requested parameters, 54*22ce4affSfengbojiang * this function should return a negative value indicating the 55*22ce4affSfengbojiang * priority of this driver. These negative values should be derived 56*22ce4affSfengbojiang * from one of the CRYPTODEV_PROBE_* constants in 57*22ce4affSfengbojiang * <opencrypto/cryptodev.h>. 58*22ce4affSfengbojiang * 59*22ce4affSfengbojiang * This function's return value is similar to that used by 60*22ce4affSfengbojiang * DEVICE_PROBE(9). However, a return value of zero is not supported 61*22ce4affSfengbojiang * and should not be used. 62*22ce4affSfengbojiang * 63*22ce4affSfengbojiang * @param dev the crypto driver device 64*22ce4affSfengbojiang * @param csp crypto session parameters 65*22ce4affSfengbojiang * 66*22ce4affSfengbojiang * @retval negative if the driver supports this session - the 67*22ce4affSfengbojiang * least negative value is used to select the 68*22ce4affSfengbojiang * driver for the session 69*22ce4affSfengbojiang * @retval EINVAL if the driver does not support the session 70*22ce4affSfengbojiang * @retval positive if some other error occurs 71*22ce4affSfengbojiang */ 72*22ce4affSfengbojiangMETHOD int probesession { 73*22ce4affSfengbojiang device_t dev; 74*22ce4affSfengbojiang const struct crypto_session_params *csp; 75*22ce4affSfengbojiang}; 76*22ce4affSfengbojiang 77*22ce4affSfengbojiang/** 78*22ce4affSfengbojiang * @brief Initialize a new crypto session object 79*22ce4affSfengbojiang * 80*22ce4affSfengbojiang * Invoked by the crypto framework to initialize driver-specific data 81*22ce4affSfengbojiang * for a crypto session. The framework allocates and zeroes the 82*22ce4affSfengbojiang * driver's per-session memory object prior to invoking this method. 83*22ce4affSfengbojiang * The driver is able to access it's per-session memory object via 84*22ce4affSfengbojiang * crypto_get_driver_session(). 85*22ce4affSfengbojiang * 86*22ce4affSfengbojiang * @param dev the crypto driver device 87*22ce4affSfengbojiang * @param crypto_session session being initialized 88*22ce4affSfengbojiang * @param csp crypto session parameters 89*22ce4affSfengbojiang * 90*22ce4affSfengbojiang * @retval 0 success 91*22ce4affSfengbojiang * @retval non-zero if some kind of error occurred 92*22ce4affSfengbojiang */ 93a9643ea8SlogwangMETHOD int newsession { 94a9643ea8Slogwang device_t dev; 95*22ce4affSfengbojiang crypto_session_t crypto_session; 96*22ce4affSfengbojiang const struct crypto_session_params *csp; 97a9643ea8Slogwang}; 98a9643ea8Slogwang 99*22ce4affSfengbojiang/** 100*22ce4affSfengbojiang * @brief Destroy a crypto session object 101*22ce4affSfengbojiang * 102*22ce4affSfengbojiang * The crypto framework invokes this method when tearing down a crypto 103*22ce4affSfengbojiang * session. After this callback returns, the frame will explicitly 104*22ce4affSfengbojiang * zero and free the drvier's per-session memory object. If the 105*22ce4affSfengbojiang * driver requires additional actions to destroy a session, it should 106*22ce4affSfengbojiang * perform those in this method. If the driver does not require 107*22ce4affSfengbojiang * additional actions it does not need to provide an implementation of 108*22ce4affSfengbojiang * this method. 109*22ce4affSfengbojiang * 110*22ce4affSfengbojiang * @param dev the crypto driver device 111*22ce4affSfengbojiang * @param crypto_session session being destroyed 112*22ce4affSfengbojiang */ 113*22ce4affSfengbojiangMETHOD void freesession { 114a9643ea8Slogwang device_t dev; 115*22ce4affSfengbojiang crypto_session_t crypto_session; 116*22ce4affSfengbojiang} DEFAULT null_freesession; 117a9643ea8Slogwang 118*22ce4affSfengbojiang/** 119*22ce4affSfengbojiang * @brief Perform a symmetric crypto operation 120*22ce4affSfengbojiang * 121*22ce4affSfengbojiang * The crypto framework invokes this method for each symmetric crypto 122*22ce4affSfengbojiang * operation performed on a session. A reference to the containing 123*22ce4affSfengbojiang * session is stored as a member of 'struct cryptop'. This routine 124*22ce4affSfengbojiang * should not block, but queue the operation if necessary. 125*22ce4affSfengbojiang * 126*22ce4affSfengbojiang * This method may return ERESTART to indicate that any internal 127*22ce4affSfengbojiang * queues are full so the operation should be queued in the crypto 128*22ce4affSfengbojiang * framework and retried in the future. 129*22ce4affSfengbojiang * 130*22ce4affSfengbojiang * To report errors with a crypto operation, 'crp_etype' should be set 131*22ce4affSfengbojiang * and the operation completed by calling 'crypto_done'. This method 132*22ce4affSfengbojiang * should then return zero. 133*22ce4affSfengbojiang * 134*22ce4affSfengbojiang * @param dev the crypto driver device 135*22ce4affSfengbojiang * @param op crypto operation to perform 136*22ce4affSfengbojiang * @param flags set to CRYPTO_HINT_MORE if additional symmetric 137*22ce4affSfengbojiang * crypto operations are queued for this driver; 138*22ce4affSfengbojiang * otherwise set to zero. 139*22ce4affSfengbojiang * 140*22ce4affSfengbojiang * @retval 0 success 141*22ce4affSfengbojiang * @retval ERESTART internal queue is full 142*22ce4affSfengbojiang */ 143a9643ea8SlogwangMETHOD int process { 144a9643ea8Slogwang device_t dev; 145a9643ea8Slogwang struct cryptop *op; 146a9643ea8Slogwang int flags; 147a9643ea8Slogwang}; 148a9643ea8Slogwang 149*22ce4affSfengbojiang/** 150*22ce4affSfengbojiang * @brief Perform an asymmetric crypto operation 151*22ce4affSfengbojiang * 152*22ce4affSfengbojiang * The crypto framework invokes this method for each asymmetric crypto 153*22ce4affSfengbojiang * operation. Each asymmetric crypto operation should be 154*22ce4affSfengbojiang * self-contained and is not assicated with any persistent session. 155*22ce4affSfengbojiang * This routine should not block, but queue the operation if 156*22ce4affSfengbojiang * necessary. 157*22ce4affSfengbojiang * 158*22ce4affSfengbojiang * This method may return ERESTART to indicate that any internal 159*22ce4affSfengbojiang * queues are full so the operation should be queued in the crypto 160*22ce4affSfengbojiang * framework and retried in the future. 161*22ce4affSfengbojiang * 162*22ce4affSfengbojiang * To report errors with a crypto operation, 'krp_status' should be set 163*22ce4affSfengbojiang * and the operation completed by calling 'crypto_kdone'. This method 164*22ce4affSfengbojiang * should then return zero. 165*22ce4affSfengbojiang * 166*22ce4affSfengbojiang * @param dev the crypto driver device 167*22ce4affSfengbojiang * @param op crypto operation to perform 168*22ce4affSfengbojiang * @param flags set to CRYPTO_HINT_MORE if additional asymmetric 169*22ce4affSfengbojiang * crypto operations are queued for this driver; 170*22ce4affSfengbojiang * otherwise set to zero. 171*22ce4affSfengbojiang * 172*22ce4affSfengbojiang * @retval 0 success 173*22ce4affSfengbojiang * @retval ERESTART internal queue is full 174*22ce4affSfengbojiang */ 175a9643ea8SlogwangMETHOD int kprocess { 176a9643ea8Slogwang device_t dev; 177a9643ea8Slogwang struct cryptkop *op; 178a9643ea8Slogwang int flags; 179a9643ea8Slogwang}; 180