/* * Cryptographic API for algorithms (i.e., low-level API). * * Copyright (c) 2006 Herbert Xu * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * */ #include #include #include #include #include #include #include #include #include #include #include "internal.h" static inline int crypto_set_driver_name(struct crypto_alg *alg) { static const char suffix[] = "-generic"; char *driver_name = alg->cra_driver_name; int len; if (*driver_name) return 0; len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME); if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME) return -ENAMETOOLONG; memcpy(driver_name + len, suffix, sizeof(suffix)); return 0; } static int crypto_check_alg(struct crypto_alg *alg) { if (alg->cra_alignmask & (alg->cra_alignmask + 1)) return -EINVAL; if (alg->cra_blocksize > PAGE_SIZE / 8) return -EINVAL; if (alg->cra_priority < 0) return -EINVAL; atomic_set(&alg->cra_refcnt, 1); return crypto_set_driver_name(alg); } static int __crypto_register_alg(struct crypto_alg *alg) { struct crypto_alg *q; int ret = -EAGAIN; INIT_LIST_HEAD(&alg->cra_users); ret = -EEXIST; list_for_each_entry(q, &crypto_alg_list, cra_list) { if (q == alg) goto err; if (!strcmp(q->cra_driver_name, alg->cra_name) || !strcmp(q->cra_name, alg->cra_driver_name)) goto err; } list_add(&alg->cra_list, &crypto_alg_list); return 0; err: return ret; } void crypto_remove_final(struct list_head *list) { struct crypto_alg *alg; struct crypto_alg *n; list_for_each_entry_safe(alg, n, list, cra_list) { list_del_init(&alg->cra_list); crypto_alg_put(alg); } } int crypto_register_alg(struct crypto_alg *alg) { int err; err = crypto_check_alg(alg); if (err) return err; down_write(&crypto_alg_sem); err = __crypto_register_alg(alg); up_write(&crypto_alg_sem); return err; } static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list) { if (unlikely(list_empty(&alg->cra_list))) return -ENOENT; list_del_init(&alg->cra_list); return 0; } int crypto_unregister_alg(struct crypto_alg *alg) { int ret; LIST_HEAD(list); down_write(&crypto_alg_sem); ret = crypto_remove_alg(alg, &list); up_write(&crypto_alg_sem); if (ret) return ret; BUG_ON(atomic_read(&alg->cra_refcnt) != 1); if (alg->cra_destroy) alg->cra_destroy(alg); crypto_remove_final(&list); return 0; } int crypto_register_algs(struct crypto_alg *algs, int count) { int i, ret; for (i = 0; i < count; i++) { ret = crypto_register_alg(&algs[i]); if (ret) goto err; } return 0; err: for (--i; i >= 0; --i) crypto_unregister_alg(&algs[i]); return ret; } int crypto_unregister_algs(struct crypto_alg *algs, int count) { int i, ret; for (i = 0; i < count; i++) { ret = crypto_unregister_alg(&algs[i]); if (ret) pr_err("Failed to unregister %s %s: %d\n", algs[i].cra_driver_name, algs[i].cra_name, ret); } return 0; } struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb) { struct rtattr *rta = tb[0]; struct crypto_attr_type *algt; if (!rta) return ERR_PTR(-ENOENT); if (RTA_PAYLOAD(rta) < sizeof(*algt)) return ERR_PTR(-EINVAL); if (rta->rta_type != CRYPTOA_TYPE) return ERR_PTR(-EINVAL); algt = RTA_DATA(rta); return algt; } int crypto_check_attr_type(struct rtattr **tb, u32 type) { struct crypto_attr_type *algt; algt = crypto_get_attr_type(tb); if (IS_ERR(algt)) return PTR_ERR(algt); if ((algt->type ^ type) & algt->mask) return -EINVAL; return 0; } const char *crypto_attr_alg_name(struct rtattr *rta) { struct crypto_attr_alg *alga; if (!rta) return ERR_PTR(-ENOENT); if (RTA_PAYLOAD(rta) < sizeof(*alga)) return ERR_PTR(-EINVAL); if (rta->rta_type != CRYPTOA_ALG) return ERR_PTR(-EINVAL); alga = RTA_DATA(rta); alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0; return alga->name; } struct crypto_alg *crypto_attr_alg2(struct rtattr *rta, const struct crypto_type *frontend, u32 type, u32 mask) { const char *name; name = crypto_attr_alg_name(rta); if (IS_ERR(name)) return ERR_CAST(name); return crypto_find_alg(name, frontend, type, mask); } int crypto_attr_u32(struct rtattr *rta, u32 *num) { struct crypto_attr_u32 *nu32; if (!rta) return -ENOENT; if (RTA_PAYLOAD(rta) < sizeof(*nu32)) return -EINVAL; if (rta->rta_type != CRYPTOA_U32) return -EINVAL; nu32 = RTA_DATA(rta); *num = nu32->num; return 0; } static inline void crypto_inc_byte(u8 *a, unsigned int size) { u8 *b = (a + size); u8 c; for (; size; size--) { c = *--b + 1; *b = c; if (c) break; } } void crypto_inc(u8 *a, unsigned int size) { __be32 *b = (__be32 *)(a + size); u32 c; for (; size >= 4; size -= 4) { c = be32_to_cpu(*--b) + 1; *b = cpu_to_be32(c); if (c) return; } crypto_inc_byte(a, size); } static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size) { for (; size; size--) *a++ ^= *b++; } void crypto_xor(u8 *dst, const u8 *src, unsigned int size) { u32 *a = (u32 *)dst; u32 *b = (u32 *)src; for (; size >= 4; size -= 4) *a++ ^= *b++; crypto_xor_byte((u8 *)a, (u8 *)b, size); } unsigned int crypto_alg_extsize(struct crypto_alg *alg) { return alg->cra_ctxsize + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); } int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, u32 type, u32 mask) { int ret = 0; struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask); if (!IS_ERR(alg)) { crypto_alg_put(alg); ret = 1; } return ret; } MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Cryptographic algorithms API");