cregit-Linux how code gets into the kernel

Release 4.16 crypto/dh.c

Directory: crypto
/*  Diffie-Hellman Key Agreement Method [RFC2631]
 *
 * Copyright (c) 2016, Intel Corporation
 * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
 *
 * 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 <linux/module.h>
#include <crypto/internal/kpp.h>
#include <crypto/kpp.h>
#include <crypto/dh.h>
#include <linux/mpi.h>


struct dh_ctx {
	
MPI p;
	
MPI g;
	
MPI xa;
};


static void dh_clear_ctx(struct dh_ctx *ctx) { mpi_free(ctx->p); mpi_free(ctx->g); mpi_free(ctx->xa); memset(ctx, 0, sizeof(*ctx)); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto3373.33%150.00%
Eric Biggers1226.67%150.00%
Total45100.00%2100.00%

/* * If base is g we compute the public key * ya = g^xa mod p; [RFC2631 sec 2.1.1] * else if base if the counterpart public key we compute the shared secret * ZZ = yb^xa mod p; [RFC2631 sec 2.1.1] */
static int _compute_val(const struct dh_ctx *ctx, MPI base, MPI val) { /* val = base^xa mod p */ return mpi_powm(val, base, ctx->xa, ctx->p); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto35100.00%1100.00%
Total35100.00%1100.00%


static inline struct dh_ctx *dh_get_ctx(struct crypto_kpp *tfm) { return kpp_tfm_ctx(tfm); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto20100.00%1100.00%
Total20100.00%1100.00%


static int dh_check_params_length(unsigned int p_len) { return (p_len < 1536) ? -EINVAL : 0; }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto22100.00%1100.00%
Total22100.00%1100.00%


static int dh_set_params(struct dh_ctx *ctx, struct dh *params) { if (dh_check_params_length(params->p_size << 3)) return -EINVAL; ctx->p = mpi_read_raw_data(params->p, params->p_size); if (!ctx->p) return -EINVAL; ctx->g = mpi_read_raw_data(params->g, params->g_size); if (!ctx->g) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto86100.00%1100.00%
Total86100.00%1100.00%


static int dh_set_secret(struct crypto_kpp *tfm, const void *buf, unsigned int len) { struct dh_ctx *ctx = dh_get_ctx(tfm); struct dh params; /* Free the old MPI key if any */ dh_clear_ctx(ctx); if (crypto_dh_decode_key(buf, len, &params) < 0) goto err_clear_ctx; if (dh_set_params(ctx, &params) < 0) goto err_clear_ctx; ctx->xa = mpi_read_raw_data(params.key, params.key_size); if (!ctx->xa) goto err_clear_ctx; return 0; err_clear_ctx: dh_clear_ctx(ctx); return -EINVAL; }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto8980.18%125.00%
Eric Biggers1715.32%250.00%
Tudor-Dan Ambarus54.50%125.00%
Total111100.00%4100.00%


static int dh_compute_value(struct kpp_request *req) { struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); struct dh_ctx *ctx = dh_get_ctx(tfm); MPI base, val = mpi_alloc(0); int ret = 0; int sign; if (!val) return -ENOMEM; if (unlikely(!ctx->xa)) { ret = -EINVAL; goto err_free_val; } if (req->src) { base = mpi_read_raw_from_sgl(req->src, req->src_len); if (!base) { ret = -EINVAL; goto err_free_val; } } else { base = ctx->g; } ret = _compute_val(ctx, base, val); if (ret) goto err_free_base; ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign); if (ret) goto err_free_base; if (sign < 0) ret = -EBADMSG; err_free_base: if (req->src) mpi_free(base); err_free_val: mpi_free(val); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto19999.50%150.00%
Mat Martineau10.50%150.00%
Total200100.00%2100.00%


static unsigned int dh_max_size(struct crypto_kpp *tfm) { struct dh_ctx *ctx = dh_get_ctx(tfm); return mpi_get_size(ctx->p); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto2996.67%150.00%
Tudor-Dan Ambarus13.33%150.00%
Total30100.00%2100.00%


static void dh_exit_tfm(struct crypto_kpp *tfm) { struct dh_ctx *ctx = dh_get_ctx(tfm); dh_clear_ctx(ctx); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto2596.15%150.00%
Eric Biggers13.85%150.00%
Total26100.00%2100.00%

static struct kpp_alg dh = { .set_secret = dh_set_secret, .generate_public_key = dh_compute_value, .compute_shared_secret = dh_compute_value, .max_size = dh_max_size, .exit = dh_exit_tfm, .base = { .cra_name = "dh", .cra_driver_name = "dh-generic", .cra_priority = 100, .cra_module = THIS_MODULE, .cra_ctxsize = sizeof(struct dh_ctx), }, };
static int dh_init(void) { return crypto_register_kpp(&dh); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto15100.00%1100.00%
Total15100.00%1100.00%


static void dh_exit(void) { crypto_unregister_kpp(&dh); }

Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto14100.00%1100.00%
Total14100.00%1100.00%

module_init(dh_init); module_exit(dh_exit); MODULE_ALIAS_CRYPTO("dh"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("DH generic algorithm");

Overall Contributors

PersonTokensPropCommitsCommitProp
Salvatore Benedetto68894.77%114.29%
Eric Biggers304.13%228.57%
Tudor-Dan Ambarus70.96%342.86%
Mat Martineau10.14%114.29%
Total726100.00%7100.00%
Directory: crypto
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.