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