Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/socket.h>
- #include <linux/netlink.h>
- #include <netlink/netlink.h>
- #include <netlink/errno.h>
- #include <netlink/socket.h>
- #include <netlink/handlers.h>
- #include <netlink/msg.h>
- /* NOTE: inlined cryptouser header from crconf project */
- /*
- * Crypto user configuration API.
- *
- * Copyright (C) 2011 secunet Security Networks AG
- * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
- */
- #include <linux/types.h>
- #define CRYPTO_MAX_ALG_NAME 64
- #define NLMSG_BUF_SIZE 4096
- /*
- * Algorithm masks and types.
- */
- #define CRYPTO_ALG_TYPE_MASK 0x0000000f
- #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
- #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
- #define CRYPTO_ALG_TYPE_AEAD 0x00000003
- #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
- #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
- #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
- #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
- #define CRYPTO_ALG_TYPE_HASH 0x00000008
- #define CRYPTO_ALG_TYPE_SHASH 0x00000009
- #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
- #define CRYPTO_ALG_TYPE_RNG 0x0000000c
- #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
- #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
- #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
- #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
- #define CRYPTO_ALG_LARVAL 0x00000010
- #define CRYPTO_ALG_DEAD 0x00000020
- #define CRYPTO_ALG_DYING 0x00000040
- #define CRYPTO_ALG_ASYNC 0x00000080
- /*
- * Set this bit if and only if the algorithm requires another algorithm of
- * the same type to handle corner cases.
- */
- #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
- /*
- * This bit is set for symmetric key ciphers that have already been wrapped
- * with a generic IV generator to prevent them from being wrapped again.
- */
- #define CRYPTO_ALG_GENIV 0x00000200
- /*
- * Set if the algorithm has passed automated run-time testing. Note that
- * if there is no run-time testing for a given algorithm it is considered
- * to have passed.
- */
- #define CRYPTO_ALG_TESTED 0x00000400
- /*
- * Set if the algorithm is an instance that is build from telplates.
- */
- #define CRYPTO_ALG_INSTANCE 0x00000800
- /* Netlink configuration messages. */
- enum {
- CRYPTO_MSG_BASE = 0x10,
- CRYPTO_MSG_NEWALG = 0x10,
- CRYPTO_MSG_DELALG,
- CRYPTO_MSG_UPDATEALG,
- CRYPTO_MSG_GETALG,
- __CRYPTO_MSG_MAX
- };
- #define CRYPTO_MSG_MAX (__CRYPTO_MSG_MAX - 1)
- #define CRYPTO_NR_MSGTYPES (CRYPTO_MSG_MAX + 1 - CRYPTO_MSG_BASE)
- /* Netlink message attributes. */
- enum crypto_attr_type_t {
- CRYPTOCFGA_UNSPEC,
- CRYPTOCFGA_PRIORITY_VAL, /* __u32 */
- CRYPTOCFGA_REPORT_LARVAL, /* struct crypto_report_larval */
- CRYPTOCFGA_REPORT_HASH, /* struct crypto_report_hash */
- CRYPTOCFGA_REPORT_BLKCIPHER, /* struct crypto_report_blkcipher */
- CRYPTOCFGA_REPORT_AEAD, /* struct crypto_report_aead */
- CRYPTOCFGA_REPORT_COMPRESS, /* struct crypto_report_comp */
- CRYPTOCFGA_REPORT_RNG, /* struct crypto_report_rng */
- CRYPTOCFGA_REPORT_CIPHER, /* struct crypto_report_cipher */
- __CRYPTOCFGA_MAX
- #define CRYPTOCFGA_MAX (__CRYPTOCFGA_MAX - 1)
- };
- struct crypto_user_alg {
- char cru_name[CRYPTO_MAX_ALG_NAME];
- char cru_driver_name[CRYPTO_MAX_ALG_NAME];
- char cru_module_name[CRYPTO_MAX_ALG_NAME];
- __u32 cru_type;
- __u32 cru_mask;
- __u32 cru_refcnt;
- __u32 cru_flags;
- };
- #define CRYPTO_MAX_NAME CRYPTO_MAX_ALG_NAME
- struct crypto_report_larval {
- char type[CRYPTO_MAX_NAME];
- };
- struct crypto_report_hash {
- char type[CRYPTO_MAX_NAME];
- unsigned int blocksize;
- unsigned int digestsize;
- };
- struct crypto_report_cipher {
- char type[CRYPTO_MAX_NAME];
- unsigned int blocksize;
- unsigned int min_keysize;
- unsigned int max_keysize;
- };
- struct crypto_report_blkcipher {
- char type[CRYPTO_MAX_NAME];
- char geniv[CRYPTO_MAX_NAME];
- unsigned int blocksize;
- unsigned int min_keysize;
- unsigned int max_keysize;
- unsigned int ivsize;
- };
- struct crypto_report_aead {
- char type[CRYPTO_MAX_NAME];
- char geniv[CRYPTO_MAX_NAME];
- unsigned int blocksize;
- unsigned int maxauthsize;
- unsigned int ivsize;
- };
- struct crypto_report_comp {
- char type[CRYPTO_MAX_NAME];
- };
- struct crypto_report_rng {
- char type[CRYPTO_MAX_NAME];
- unsigned int seedsize;
- };
- #define CR_RTA(x) ((struct rtattr*)(((char*)(x)) + NLMSG_ALIGN(sizeof(struct crypto_user_alg))))
- static void log_algo(struct crypto_user_alg *ualg,
- enum crypto_attr_type_t attr_type, void *ptr, unsigned int count)
- {
- struct crypto_report_hash *hash;
- struct crypto_report_blkcipher *blkcipher;
- struct crypto_report_aead *aead;
- struct crypto_report_rng *rng;
- struct crypto_report_cipher *cipher;
- if (1 != count)
- {
- fprintf(stdout, "===============\n");
- }
- fprintf(stdout, "algorithm #%d\n", count);
- fprintf(stdout, "name: %s\n", ualg->cru_name);
- fprintf(stdout, "type: %s\n", (char *)ptr);
- switch (attr_type)
- {
- case CRYPTOCFGA_REPORT_HASH:
- hash = (struct crypto_report_hash *)ptr;
- fprintf(stdout, "blocksize: %d\n", hash->blocksize);
- fprintf(stdout, "digestsize: %d\n", hash->digestsize);
- break;
- case CRYPTOCFGA_REPORT_BLKCIPHER:
- blkcipher = (struct crypto_report_blkcipher *)ptr;
- fprintf(stdout, "geniv: %s\n", blkcipher->geniv);
- fprintf(stdout, "blocksize: %d\n", blkcipher->blocksize);
- fprintf(stdout, "min_keysize: %d\n", blkcipher->min_keysize);
- fprintf(stdout, "max_keysize: %d\n", blkcipher->max_keysize);
- fprintf(stdout, "ivsize: %d\n", blkcipher->ivsize);
- break;
- case CRYPTOCFGA_REPORT_AEAD:
- aead = (struct crypto_report_aead *)ptr;
- fprintf(stdout, "geniv: %s\n", aead->geniv);
- fprintf(stdout, "blocksize: %d\n", aead->blocksize);
- fprintf(stdout, "maxauthsize: %d\n", aead->maxauthsize);
- fprintf(stdout, "ivsize: %d\n", aead->ivsize);
- break;
- case CRYPTOCFGA_REPORT_RNG:
- rng = (struct crypto_report_rng *)ptr;
- fprintf(stdout, "seedsize: %d\n", rng->seedsize);
- break;
- case CRYPTOCFGA_REPORT_CIPHER:
- cipher = (struct crypto_report_cipher *)ptr;
- fprintf(stdout, "blocksize: %d\n", cipher->blocksize);
- fprintf(stdout, "min_keysize: %d\n", cipher->min_keysize);
- fprintf(stdout, "max_keysize: %d\n", cipher->max_keysize);
- break;
- case CRYPTOCFGA_REPORT_LARVAL:
- case CRYPTOCFGA_REPORT_COMPRESS:
- default:
- break;
- }
- }
- static int cb_func(struct nl_msg *msg, void *arg)
- {
- struct rtattr *attrs[CRYPTOCFGA_MAX + 1], *rta;
- int len, *count;
- enum crypto_attr_type_t t;
- struct crypto_user_alg *ualg;
- struct nlmsghdr *n = nlmsg_hdr(msg);
- count = (unsigned int *)arg;
- if (CRYPTO_MSG_GETALG != n->nlmsg_type)
- {
- /* no, thanks */
- return NL_SKIP;
- }
- ualg = NLMSG_DATA(n);
- len = n->nlmsg_len - NLMSG_SPACE(sizeof(*ualg));
- rta = CR_RTA(ualg);
- memset(&attrs, 0, sizeof(attrs));
- while (RTA_OK(rta, len))
- {
- if (CRYPTO_MSG_GETALG >= rta->rta_type && NULL == attrs[rta->rta_type])
- {
- attrs[rta->rta_type] = rta;
- }
- rta = RTA_NEXT(rta, len);
- }
- for (t = CRYPTOCFGA_REPORT_LARVAL; __CRYPTOCFGA_MAX > t; ++t)
- {
- if (attrs[t])
- {
- ++(*count);
- log_algo(ualg, t, RTA_DATA(attrs[t]), *count);
- break;
- }
- }
- return NL_OK;
- }
- int main(int argc, char *argv[])
- {
- int err;
- unsigned int count = 0;
- struct nl_sock *sk;
- struct rtgenmsg rtg = {
- .rtgen_family = AF_UNSPEC,
- };
- sk = nl_socket_alloc();
- err = nl_connect(sk, NETLINK_CRYPTO);
- if (0 > err)
- {
- nl_socket_free(sk);
- fprintf(stderr, "%s:%u error connect: %s\n", __func__, __LINE__,
- nl_geterror(err));
- return err;
- }
- err = nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, cb_func, &count);
- if (0 > err)
- {
- fprintf(stderr, "%s:%u netlink callback registation failed: %s",
- __func__, __LINE__, nl_geterror(err));
- return err;
- }
- if (1 < argc && 0 == strcmp(argv[1], "-p"))
- {
- nl_socket_enable_msg_peek(sk);
- }
- err = nl_send_simple(sk, CRYPTO_MSG_GETALG, NLM_F_DUMP | NLM_F_REQUEST,
- &rtg, sizeof(rtg));
- err = nl_recvmsgs_default(sk);
- if (0 > err)
- {
- fprintf(stderr, "%s:%u error reading response from netlink: %s",
- __func__, __LINE__, nl_geterror(err));
- return err;
- }
- nl_socket_free(sk);
- return err;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement