1 /* SPDX-License-Identifier: GPL-2.0-or-later 2 * Copyright (c) 2018 Richard Palethorpe <rpalethorpe@suse.com> 3 */ 4 5 /** 6 * @file tst_crypto.h 7 * 8 * Library for interacting with kernel's crypto layer using the netlink 9 * interface. 10 */ 11 12 #ifndef TST_CRYPTO_H 13 #define TST_CRYPTO_H 14 15 #include "lapi/cryptouser.h" 16 17 /** 18 * A reference to a crypto session and associated state. 19 * 20 * Holds state relevant to a netlink crypto connection. The seq_num is used 21 * to tag each message sent to the netlink layer and is automatically 22 * incremented by the tst_crypto_ functions. When the netlink layer sends a 23 * response (ack) it will use the sequences number from the request. 24 * 25 * Some functions, such as delete ALG, may return EBUSY in which case it is 26 * safe to retry them. The retries field allows you to set the number of 27 * times this should be done. If set to zero the operation will only be tried 28 * once. For operations which do not return EBUSY, the field is ignored. 29 * 30 * Use TST_CRYPTO_SESSION_INIT to statically initialize this struct with sane 31 * defaults. 32 */ 33 struct tst_crypto_session { 34 /** File descriptor for the netlink socket */ 35 int fd; 36 /** A sequence number used to identify responses from the kernel. */ 37 uint32_t seq_num; 38 /** Number of times some operations will be retried. */ 39 uint32_t retries; 40 }; 41 42 /** 43 * Default static definition of tst_crypto_session. 44 * 45 * @relates tst_crypto_session 46 */ 47 #define TST_CRYPTO_SESSION_INIT {\ 48 .fd = 0, \ 49 .seq_num = 0, \ 50 .retries = 1000 \ 51 } 52 53 /** 54 * Creates a crypto session. 55 * 56 * @relates tst_crypto_session 57 * @param ses Session structure to use, it can be uninitialized. 58 * 59 * If some necessary feature is missing then it will call tst_brk() with 60 * TCONF, for any other error it will use TBROK. 61 */ 62 void tst_crypto_open(struct tst_crypto_session *ses); 63 64 /** 65 * Close a crypto session. 66 * 67 * @relates tst_crypto_session 68 * @param ses The session to close. 69 */ 70 void tst_crypto_close(struct tst_crypto_session *ses); 71 72 /** 73 * Add a crypto algorithm to a session. 74 * 75 * @relates tst_crypto_session 76 * @param ses An open session. 77 * @param alg The crypto algorithm or module to add. 78 * 79 * This requests a new crypto algorithm/engine/module to be initialized by the 80 * kernel. It sends the request contained in alg and then waits for a 81 * response. If sending the message or receiving the ack fails at the netlink 82 * level then tst_brk() with TBROK will be called. 83 * 84 * @return On success it will return 0 otherwise it will return an inverted 85 * error code from the crypto layer. 86 */ 87 int tst_crypto_add_alg(struct tst_crypto_session *ses, 88 const struct crypto_user_alg *alg); 89 90 /** 91 * Delete a crypto algorithm from a session. 92 * 93 * @relates tst_crypto_session 94 * @param ses An open session. 95 * @param alg The crypto algorithm to delete. 96 * 97 * Request that the kernel remove an existing crypto algorithm. This behaves 98 * in a similar way to tst_crypto_add_alg() except that it is the inverse 99 * operation and that it is not unusual for the crypto layer to return 100 * EBUSY. If EBUSY is returned then the function will internally retry the 101 * operation tst_crypto_session::retries times before giving up and returning 102 * EBUSY. 103 * 104 * Return: Either 0 or an inverted error code from the crypto layer. If called 105 * during cleanup it may return a positive ENODATA value from the LTP 106 * library, you don't need to log this error as it will already have 107 * been printed by tst_brk(). 108 */ 109 int tst_crypto_del_alg(struct tst_crypto_session *ses, 110 const struct crypto_user_alg *alg); 111 112 #endif /* TST_CRYPTO_H */ 113