1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later 2*49cdfc7eSAndroid Build Coastguard Worker /* 3*49cdfc7eSAndroid Build Coastguard Worker * Copyright 2019 Google LLC 4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2021 5*49cdfc7eSAndroid Build Coastguard Worker */ 6*49cdfc7eSAndroid Build Coastguard Worker /** 7*49cdfc7eSAndroid Build Coastguard Worker * @file tst_af_alg.h 8*49cdfc7eSAndroid Build Coastguard Worker * 9*49cdfc7eSAndroid Build Coastguard Worker * Library for accessing kernel crypto algorithms via AF_ALG. 10*49cdfc7eSAndroid Build Coastguard Worker * 11*49cdfc7eSAndroid Build Coastguard Worker * See https://www.kernel.org/doc/html/latest/crypto/userspace-if.html 12*49cdfc7eSAndroid Build Coastguard Worker * for more information about AF_ALG. 13*49cdfc7eSAndroid Build Coastguard Worker */ 14*49cdfc7eSAndroid Build Coastguard Worker 15*49cdfc7eSAndroid Build Coastguard Worker #ifndef TST_AF_ALG_H 16*49cdfc7eSAndroid Build Coastguard Worker #define TST_AF_ALG_H 17*49cdfc7eSAndroid Build Coastguard Worker 18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/if_alg.h" 19*49cdfc7eSAndroid Build Coastguard Worker #include <stdbool.h> 20*49cdfc7eSAndroid Build Coastguard Worker 21*49cdfc7eSAndroid Build Coastguard Worker /** 22*49cdfc7eSAndroid Build Coastguard Worker * Create an AF_ALG algorithm socket. 23*49cdfc7eSAndroid Build Coastguard Worker * 24*49cdfc7eSAndroid Build Coastguard Worker * This creates an AF_ALG algorithm socket that is initially not bound to any 25*49cdfc7eSAndroid Build Coastguard Worker * particular algorithm. On failure, tst_brk() is called with TCONF if the 26*49cdfc7eSAndroid Build Coastguard Worker * kernel doesn't support AF_ALG, otherwise TBROK. 27*49cdfc7eSAndroid Build Coastguard Worker * 28*49cdfc7eSAndroid Build Coastguard Worker * @return a new AF_ALG algorithm socket 29*49cdfc7eSAndroid Build Coastguard Worker */ 30*49cdfc7eSAndroid Build Coastguard Worker int tst_alg_create(void); 31*49cdfc7eSAndroid Build Coastguard Worker 32*49cdfc7eSAndroid Build Coastguard Worker /** 33*49cdfc7eSAndroid Build Coastguard Worker * Bind an AF_ALG algorithm socket to an algorithm. 34*49cdfc7eSAndroid Build Coastguard Worker * 35*49cdfc7eSAndroid Build Coastguard Worker * @param algfd An AF_ALG algorithm socket 36*49cdfc7eSAndroid Build Coastguard Worker * @param addr A structure which specifies the algorithm to use 37*49cdfc7eSAndroid Build Coastguard Worker * 38*49cdfc7eSAndroid Build Coastguard Worker * On failure, tst_brk() is called with TCONF if the kernel doesn't support the 39*49cdfc7eSAndroid Build Coastguard Worker * specified algorithm, otherwise TBROK. 40*49cdfc7eSAndroid Build Coastguard Worker */ 41*49cdfc7eSAndroid Build Coastguard Worker void tst_alg_bind_addr(int algfd, const struct sockaddr_alg *addr); 42*49cdfc7eSAndroid Build Coastguard Worker 43*49cdfc7eSAndroid Build Coastguard Worker /** 44*49cdfc7eSAndroid Build Coastguard Worker * Bind an AF_ALG algorithm socket to an algorithm. 45*49cdfc7eSAndroid Build Coastguard Worker * 46*49cdfc7eSAndroid Build Coastguard Worker * @param algfd An AF_ALG algorithm socket 47*49cdfc7eSAndroid Build Coastguard Worker * @param algtype The type of algorithm, such as "hash" or "skcipher" 48*49cdfc7eSAndroid Build Coastguard Worker * @param algname The name of the algorithm, such as "sha256" or "xts(aes)" 49*49cdfc7eSAndroid Build Coastguard Worker * 50*49cdfc7eSAndroid Build Coastguard Worker * Like tst_alg_bind_addr(), except this just takes in the algorithm type and 51*49cdfc7eSAndroid Build Coastguard Worker * name. The 'feat' and 'mask' fields are left 0. 52*49cdfc7eSAndroid Build Coastguard Worker * 53*49cdfc7eSAndroid Build Coastguard Worker * On failure, tst_brk() is called with TCONF if the kernel doesn't support the 54*49cdfc7eSAndroid Build Coastguard Worker * specified algorithm, otherwise TBROK. 55*49cdfc7eSAndroid Build Coastguard Worker */ 56*49cdfc7eSAndroid Build Coastguard Worker void tst_alg_bind(int algfd, const char *algtype, const char *algname); 57*49cdfc7eSAndroid Build Coastguard Worker 58*49cdfc7eSAndroid Build Coastguard Worker /** 59*49cdfc7eSAndroid Build Coastguard Worker * Check for the availability of an algorithm. 60*49cdfc7eSAndroid Build Coastguard Worker * 61*49cdfc7eSAndroid Build Coastguard Worker * @param algtype The type of algorithm, such as "hash" or "skcipher" 62*49cdfc7eSAndroid Build Coastguard Worker * @param algname The name of the algorithm, such as "sha256" or "xts(aes)" 63*49cdfc7eSAndroid Build Coastguard Worker * 64*49cdfc7eSAndroid Build Coastguard Worker * Return 0 if the algorithm is available, or errno if unavailable. 65*49cdfc7eSAndroid Build Coastguard Worker */ 66*49cdfc7eSAndroid Build Coastguard Worker int tst_try_alg(const char *algtype, const char *algname); 67*49cdfc7eSAndroid Build Coastguard Worker 68*49cdfc7eSAndroid Build Coastguard Worker /** 69*49cdfc7eSAndroid Build Coastguard Worker * Check for the availability of an algorithm. 70*49cdfc7eSAndroid Build Coastguard Worker * 71*49cdfc7eSAndroid Build Coastguard Worker * @param algtype The type of algorithm, such as "hash" or "skcipher" 72*49cdfc7eSAndroid Build Coastguard Worker * @param algname The name of the algorithm, such as "sha256" or "xts(aes)" 73*49cdfc7eSAndroid Build Coastguard Worker * 74*49cdfc7eSAndroid Build Coastguard Worker * Return true if the algorithm is available, or false if unavailable 75*49cdfc7eSAndroid Build Coastguard Worker * and call tst_res() with TCONF. If another error occurs, tst_brk() is called 76*49cdfc7eSAndroid Build Coastguard Worker * with TBROK unless algorithm is disabled due FIPS mode (errno ELIBBAD). 77*49cdfc7eSAndroid Build Coastguard Worker */ 78*49cdfc7eSAndroid Build Coastguard Worker bool tst_have_alg(const char *algtype, const char *algname); 79*49cdfc7eSAndroid Build Coastguard Worker 80*49cdfc7eSAndroid Build Coastguard Worker /** 81*49cdfc7eSAndroid Build Coastguard Worker * Require the availability of an algorithm. 82*49cdfc7eSAndroid Build Coastguard Worker * 83*49cdfc7eSAndroid Build Coastguard Worker * @param algtype The type of algorithm, such as "hash" or "skcipher" 84*49cdfc7eSAndroid Build Coastguard Worker * @param algname The name of the algorithm, such as "sha256" or "xts(aes)" 85*49cdfc7eSAndroid Build Coastguard Worker * 86*49cdfc7eSAndroid Build Coastguard Worker * If the algorithm is unavailable, tst_brk() is called with TCONF. 87*49cdfc7eSAndroid Build Coastguard Worker * If another error occurs, tst_brk() is called with TBROK. 88*49cdfc7eSAndroid Build Coastguard Worker */ 89*49cdfc7eSAndroid Build Coastguard Worker void tst_require_alg(const char *algtype, const char *algname); 90*49cdfc7eSAndroid Build Coastguard Worker 91*49cdfc7eSAndroid Build Coastguard Worker /** 92*49cdfc7eSAndroid Build Coastguard Worker * Assign a cryptographic key to an AF_ALG algorithm socket. 93*49cdfc7eSAndroid Build Coastguard Worker * 94*49cdfc7eSAndroid Build Coastguard Worker * @param algfd An AF_ALG algorithm socket 95*49cdfc7eSAndroid Build Coastguard Worker * @param key Pointer to the key. If NULL, a random key is generated. 96*49cdfc7eSAndroid Build Coastguard Worker * @param keylen Length of the key in bytes 97*49cdfc7eSAndroid Build Coastguard Worker * 98*49cdfc7eSAndroid Build Coastguard Worker * On failure, tst_brk() is called with TBROK. 99*49cdfc7eSAndroid Build Coastguard Worker */ 100*49cdfc7eSAndroid Build Coastguard Worker void tst_alg_setkey(int algfd, const uint8_t *key, unsigned int keylen); 101*49cdfc7eSAndroid Build Coastguard Worker 102*49cdfc7eSAndroid Build Coastguard Worker /** 103*49cdfc7eSAndroid Build Coastguard Worker * Create an AF_ALG request socket for the given algorithm socket. 104*49cdfc7eSAndroid Build Coastguard Worker * 105*49cdfc7eSAndroid Build Coastguard Worker * @param algfd An AF_ALG algorithm socket 106*49cdfc7eSAndroid Build Coastguard Worker * 107*49cdfc7eSAndroid Build Coastguard Worker * This creates a request socket for the given algorithm socket, which must be 108*49cdfc7eSAndroid Build Coastguard Worker * bound to an algorithm. The same algorithm socket can have many request 109*49cdfc7eSAndroid Build Coastguard Worker * sockets used concurrently to perform independent cryptographic operations, 110*49cdfc7eSAndroid Build Coastguard Worker * e.g. hashing or encryption/decryption. But the key, if any, that has been 111*49cdfc7eSAndroid Build Coastguard Worker * assigned to the algorithm is shared by all request sockets. 112*49cdfc7eSAndroid Build Coastguard Worker * 113*49cdfc7eSAndroid Build Coastguard Worker * On failure, tst_brk() is called with TBROK. 114*49cdfc7eSAndroid Build Coastguard Worker * 115*49cdfc7eSAndroid Build Coastguard Worker * @return a new AF_ALG request socket 116*49cdfc7eSAndroid Build Coastguard Worker */ 117*49cdfc7eSAndroid Build Coastguard Worker int tst_alg_accept(int algfd); 118*49cdfc7eSAndroid Build Coastguard Worker 119*49cdfc7eSAndroid Build Coastguard Worker /** 120*49cdfc7eSAndroid Build Coastguard Worker * Set up an AF_ALG algorithm socket for the given algorithm w/ given key. 121*49cdfc7eSAndroid Build Coastguard Worker * 122*49cdfc7eSAndroid Build Coastguard Worker * @param algtype The type of algorithm, such as "hash" or "skcipher" 123*49cdfc7eSAndroid Build Coastguard Worker * @param algname The name of the algorithm, such as "sha256" or "xts(aes)" 124*49cdfc7eSAndroid Build Coastguard Worker * @param key The key to use (optional) 125*49cdfc7eSAndroid Build Coastguard Worker * @param keylen The length of the key in bytes (optional) 126*49cdfc7eSAndroid Build Coastguard Worker * 127*49cdfc7eSAndroid Build Coastguard Worker * This is a helper function which creates an AF_ALG algorithm socket, binds it 128*49cdfc7eSAndroid Build Coastguard Worker * to the specified algorithm, and optionally sets a key. If keylen is 0 then 129*49cdfc7eSAndroid Build Coastguard Worker * no key is set; otherwise if key is NULL a key of the given length is randomly 130*49cdfc7eSAndroid Build Coastguard Worker * generated and set; otherwise the given key is set. 131*49cdfc7eSAndroid Build Coastguard Worker * 132*49cdfc7eSAndroid Build Coastguard Worker * @return the AF_ALG algorithm socket that was set up 133*49cdfc7eSAndroid Build Coastguard Worker */ 134*49cdfc7eSAndroid Build Coastguard Worker int tst_alg_setup(const char *algtype, const char *algname, 135*49cdfc7eSAndroid Build Coastguard Worker const uint8_t *key, unsigned int keylen); 136*49cdfc7eSAndroid Build Coastguard Worker 137*49cdfc7eSAndroid Build Coastguard Worker /** 138*49cdfc7eSAndroid Build Coastguard Worker * Set up an AF_ALG request socket for the given algorithm w/ given key. 139*49cdfc7eSAndroid Build Coastguard Worker * 140*49cdfc7eSAndroid Build Coastguard Worker * This is like tst_alg_setup(), except this returns a request fd instead of the 141*49cdfc7eSAndroid Build Coastguard Worker * alg fd. The alg fd is closed, so it doesn't need to be kept track of. 142*49cdfc7eSAndroid Build Coastguard Worker * 143*49cdfc7eSAndroid Build Coastguard Worker * @return the AF_ALG request socket that was set up 144*49cdfc7eSAndroid Build Coastguard Worker */ 145*49cdfc7eSAndroid Build Coastguard Worker int tst_alg_setup_reqfd(const char *algtype, const char *algname, 146*49cdfc7eSAndroid Build Coastguard Worker const uint8_t *key, unsigned int keylen); 147*49cdfc7eSAndroid Build Coastguard Worker 148*49cdfc7eSAndroid Build Coastguard Worker /** Specification of control data to send to an AF_ALG request socket */ 149*49cdfc7eSAndroid Build Coastguard Worker struct tst_alg_sendmsg_params { 150*49cdfc7eSAndroid Build Coastguard Worker 151*49cdfc7eSAndroid Build Coastguard Worker /** If true, send ALG_SET_OP with ALG_OP_ENCRYPT */ 152*49cdfc7eSAndroid Build Coastguard Worker bool encrypt; 153*49cdfc7eSAndroid Build Coastguard Worker 154*49cdfc7eSAndroid Build Coastguard Worker /** If true, send ALG_SET_OP with ALG_OP_DECRYPT */ 155*49cdfc7eSAndroid Build Coastguard Worker bool decrypt; 156*49cdfc7eSAndroid Build Coastguard Worker 157*49cdfc7eSAndroid Build Coastguard Worker /** If ivlen != 0, send ALG_SET_IV */ 158*49cdfc7eSAndroid Build Coastguard Worker const uint8_t *iv; 159*49cdfc7eSAndroid Build Coastguard Worker unsigned int ivlen; 160*49cdfc7eSAndroid Build Coastguard Worker 161*49cdfc7eSAndroid Build Coastguard Worker /** If assoclen != 0, send ALG_SET_AEAD_ASSOCLEN */ 162*49cdfc7eSAndroid Build Coastguard Worker unsigned int assoclen; 163*49cdfc7eSAndroid Build Coastguard Worker 164*49cdfc7eSAndroid Build Coastguard Worker /* Value to use as msghdr::msg_flags */ 165*49cdfc7eSAndroid Build Coastguard Worker uint32_t msg_flags; 166*49cdfc7eSAndroid Build Coastguard Worker }; 167*49cdfc7eSAndroid Build Coastguard Worker 168*49cdfc7eSAndroid Build Coastguard Worker /** 169*49cdfc7eSAndroid Build Coastguard Worker * Send some data to an AF_ALG request socket, including control data. 170*49cdfc7eSAndroid Build Coastguard Worker * @param reqfd An AF_ALG request socket 171*49cdfc7eSAndroid Build Coastguard Worker * @param data The data to send 172*49cdfc7eSAndroid Build Coastguard Worker * @param datalen The length of data in bytes 173*49cdfc7eSAndroid Build Coastguard Worker * @param params Specification of the control data to send 174*49cdfc7eSAndroid Build Coastguard Worker * 175*49cdfc7eSAndroid Build Coastguard Worker * On failure, tst_brk() is called with TBROK. 176*49cdfc7eSAndroid Build Coastguard Worker */ 177*49cdfc7eSAndroid Build Coastguard Worker void tst_alg_sendmsg(int reqfd, const void *data, size_t datalen, 178*49cdfc7eSAndroid Build Coastguard Worker const struct tst_alg_sendmsg_params *params); 179*49cdfc7eSAndroid Build Coastguard Worker 180*49cdfc7eSAndroid Build Coastguard Worker #endif /* TST_AF_ALG_H */ 181