aboutsummaryrefslogtreecommitdiffstats
Commit message (Expand)AuthorAge
* drm/radeon/kms: Fix oops after radeon_cs_parser_init() failure.Michel Dänzer2010-01-31
* drm/radeon/kms: move radeon KMS on/off switch out of staging.Dave Airlie2010-01-31
* drm/radeon/kms: Bailout of blit if error happen & protect with mutex V3Jerome Glisse2010-01-31
* drm/vmwgfx: Don't send bad flags to the hostJakob Bornecrantz2010-01-31
* drm/vmwgfx: Request SVGA version 2 and bail if not foundPeter Hanzel2010-01-31
* drm/vmwgfx: Correctly detect 3DJakob Bornecrantz2010-01-31
* drm/ttm: remove unnecessary save_flags and ttm_flag_masked in ttm_bo_util.cAustin Yuan2010-01-31
* drm/kms: Remove incorrect comment in struct drm_mode_modeinfoMarcin Kościelnicki2010-01-31
* drm/ttm: remove padding from ttm_ref_object on 64bit buildsRichard Kennedy2010-01-31
* drm/radeon/kms: release agp on error.Dave Airlie2010-01-31
* drm/kms/radeon/agp: Move the check of the aper_size after drm_acp_acquire and...John Kacur2010-01-31
* drm/kms/radeon/agp: Fix warning, format ‘%d’ expects type ‘int’, but ...John Kacur2010-01-31
* drm/ttm: Avoid conflicting reserve_memtype during ttm_tt_set_page_caching.Francisco Jerez2010-01-31
* drm/kms/radeon: pick digitial encoders smarter. (v3)Dave Airlie2010-01-31
* drm/radeon/kms: use active device to pick connector for encoderDave Airlie2010-01-31
* drm/radeon/kms: fix incorrect logic in DP vs eDP connector checking.Dave Airlie2010-01-31
* drm/radeon/kms: preface warning printk with driver nameDave Airlie2010-01-25
* drm/radeon/kms: drop unnecessary printks.Dave Airlie2010-01-25
* drm: fix regression in fb blank handlingZhenyu Wang2010-01-25
* drm/radeon/kms: make hibernate work on IGPsDave Airlie2010-01-25
* drm/vmwgfx: Optimize memory footprint for DMA buffers.Thomas Hellstrom2010-01-25
* drm/ttm: Allow system memory as a busy placement.Thomas Hellstrom2010-01-25
* Merge remote branch 'korg/drm-radeon-next' into drm-linusDave Airlie2010-01-25
|\
| * drm/radeon/kms: fix legacy get_engine/memory clockAlex Deucher2010-01-24
| * drm/radeon/kms/atom: atom parser fixesAlex Deucher2010-01-24
| * drm/radeon/kms: clean up atombios pll codeAlex Deucher2010-01-24
| * drm/radeon/kms: clean up pll structAlex Deucher2010-01-24
| * drm/radeon/kms/atom: fix crtc lock orderingAlex Deucher2010-01-24
| * drm/radeon: r6xx/r7xx possible security issue, system ram accessJerome Glisse2010-01-20
| * drm/radeon/kms: r600/r700 don't test ib if ib initialization failsJerome Glisse2010-01-20
| * drm/radeon/kms: Forbid creation of framebuffer with no valid GEM objectJerome Glisse2010-01-20
| * drm/radeon/kms: r600 handle irq vector ring overflowJerome Glisse2010-01-20
| * drm/radeon/kms: r600/r700 don't process IRQ if not initializedJerome Glisse2010-01-20
| * drm/radeon/kms: r600/r700 disable irq at suspendJerome Glisse2010-01-20
| * drm/radeon/kms/r4xx: cleanup atom pathAlex Deucher2010-01-20
| * drm/radeon/kms: fix atombios_crtc_set_baseAlex Deucher2010-01-20
| * drm/radeon/kms/atom: upstream parser updatesAlex Deucher2010-01-20
| * drm/radeon/kms/atom: fix some parser bugsAlex Deucher2010-01-20
| * drm/radeon/kms: fix hardcoded mmio size in register functionsAlex Deucher2010-01-20
| * drm/radeon/kms/r100: fix bug in CS parserAlex Deucher2010-01-20
| * drm/radeon/kms/r200: fix bug in CS parserAndrew Randrianasulu2010-01-20
| * drm/radeon/kms/r200: fix bug in CS parserAndrew Randrianasulu2010-01-20
* | Merge remote branch 'nouveau/for-airlied' of ../drm-nouveau-next into drm-linusDave Airlie2010-01-25
|\ \
| * | drm/nv50: prevent switching off SOR when in use for DVI-over-DPBen Skeggs2010-01-24
| * | drm/nv50: fail auxch transaction if reply count not what we expectBen Skeggs2010-01-24
| * | drm/nouveau: fix failure path if userspace specifies no valid memtypesBen Skeggs2010-01-24
| * | drm/nouveau: report LVDS as disconnected if lid closedBen Skeggs2010-01-24
| * | drm/nv50: prevent accidently turning off encoders we're actually usingBen Skeggs2010-01-17
| * | drm/nv50: fix alignment of per-channel fifo cacheBen Skeggs2010-01-17
| * | drm/nouveau: Evict buffers in VRAM before freeing sgdmaLuca Barbieri2010-01-17
n187' href='#n187'>187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264





                                                             
                                                              





                                                                             
                                                                            


                     





                          
                              





                                  
 






























































                                                                             
                




                                                             




                                                   























                                                                      
                




                                                             




                                                             














                                                                       
                








                                                             




                                                                  





























































































                                                                                
/*
 * Cryptographic API.
 *
 * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
 *
 * The HMAC implementation is derived from USAGI.
 * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
 *
 * 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 <crypto/algapi.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/string.h>

struct hmac_ctx {
	struct crypto_hash *child;
};

static inline void *align_ptr(void *p, unsigned int align)
{
	return (void *)ALIGN((unsigned long)p, align);
}

static inline struct hmac_ctx *hmac_ctx(struct crypto_hash *tfm)
{
	return align_ptr(crypto_hash_ctx_aligned(tfm) +
			 crypto_hash_blocksize(tfm) * 2 +
			 crypto_hash_digestsize(tfm), sizeof(void *));
}

static int hmac_setkey(struct crypto_hash *parent,
		       const u8 *inkey, unsigned int keylen)
{
	int bs = crypto_hash_blocksize(parent);
	int ds = crypto_hash_digestsize(parent);
	char *ipad = crypto_hash_ctx_aligned(parent);
	char *opad = ipad + bs;
	char *digest = opad + bs;
	struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
	struct crypto_hash *tfm = ctx->child;
	unsigned int i;

	if (keylen > bs) {
		struct hash_desc desc;
		struct scatterlist tmp;
		int err;

		desc.tfm = tfm;
		desc.flags = crypto_hash_get_flags(parent);
		desc.flags &= CRYPTO_TFM_REQ_MAY_SLEEP;
		sg_set_buf(&tmp, inkey, keylen);

		err = crypto_hash_digest(&desc, &tmp, keylen, digest);
		if (err)
			return err;

		inkey = digest;
		keylen = ds;
	}

	memcpy(ipad, inkey, keylen);
	memset(ipad + keylen, 0, bs - keylen);
	memcpy(opad, ipad, bs);

	for (i = 0; i < bs; i++) {
		ipad[i] ^= 0x36;
		opad[i] ^= 0x5c;
	}

	return 0;
}

static int hmac_init(struct hash_desc *pdesc)
{
	struct crypto_hash *parent = pdesc->tfm;
	int bs = crypto_hash_blocksize(parent);
	int ds = crypto_hash_digestsize(parent);
	char *ipad = crypto_hash_ctx_aligned(parent);
	struct hmac_ctx *ctx = align_ptr(ipad + bs * 2 + ds, sizeof(void *));
	struct hash_desc desc;
	struct scatterlist tmp;
	int err;

	desc.tfm = ctx->child;
	desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
	sg_set_buf(&tmp, ipad, bs);

	err = crypto_hash_init(&desc);
	if (unlikely(err))
		return err;

	return crypto_hash_update(&desc, &tmp, bs);
}

static int hmac_update(struct hash_desc *pdesc,
		       struct scatterlist *sg, unsigned int nbytes)
{
	struct hmac_ctx *ctx = hmac_ctx(pdesc->tfm);
	struct hash_desc desc;

	desc.tfm = ctx->child;
	desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;

	return crypto_hash_update(&desc, sg, nbytes);
}

static int hmac_final(struct hash_desc *pdesc, u8 *out)
{
	struct crypto_hash *parent = pdesc->tfm;
	int bs = crypto_hash_blocksize(parent);
	int ds = crypto_hash_digestsize(parent);
	char *opad = crypto_hash_ctx_aligned(parent) + bs;
	char *digest = opad + bs;
	struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
	struct hash_desc desc;
	struct scatterlist tmp;
	int err;

	desc.tfm = ctx->child;
	desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;
	sg_set_buf(&tmp, opad, bs + ds);

	err = crypto_hash_final(&desc, digest);
	if (unlikely(err))
		return err;

	return crypto_hash_digest(&desc, &tmp, bs + ds, out);
}

static int hmac_digest(struct hash_desc *pdesc, struct scatterlist *sg,
		       unsigned int nbytes, u8 *out)
{
	struct crypto_hash *parent = pdesc->tfm;
	int bs = crypto_hash_blocksize(parent);
	int ds = crypto_hash_digestsize(parent);
	char *ipad = crypto_hash_ctx_aligned(parent);
	char *opad = ipad + bs;
	char *digest = opad + bs;
	struct hmac_ctx *ctx = align_ptr(digest + ds, sizeof(void *));
	struct hash_desc desc;
	struct scatterlist sg1[2];
	struct scatterlist sg2[1];
	int err;

	desc.tfm = ctx->child;
	desc.flags = pdesc->flags & CRYPTO_TFM_REQ_MAY_SLEEP;

	sg_set_buf(sg1, ipad, bs);
	sg1[1].page = (void *)sg;
	sg1[1].length = 0;
	sg_set_buf(sg2, opad, bs + ds);

	err = crypto_hash_digest(&desc, sg1, nbytes + bs, digest);
	if (unlikely(err))
		return err;

	return crypto_hash_digest(&desc, sg2, bs + ds, out);
}

static int hmac_init_tfm(struct crypto_tfm *tfm)
{
	struct crypto_instance *inst = (void *)tfm->__crt_alg;
	struct crypto_spawn *spawn = crypto_instance_ctx(inst);
	struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));

	tfm = crypto_spawn_tfm(spawn);
	if (IS_ERR(tfm))
		return PTR_ERR(tfm);

	ctx->child = crypto_hash_cast(tfm);
	return 0;
}

static void hmac_exit_tfm(struct crypto_tfm *tfm)
{
	struct hmac_ctx *ctx = hmac_ctx(__crypto_hash_cast(tfm));
	crypto_free_hash(ctx->child);
}

static void hmac_free(struct crypto_instance *inst)
{
	crypto_drop_spawn(crypto_instance_ctx(inst));
	kfree(inst);
}

static struct crypto_instance *hmac_alloc(void *param, unsigned int len)
{
	struct crypto_instance *inst;
	struct crypto_alg *alg;

	alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_HASH,
				  CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC);
	if (IS_ERR(alg))
		return ERR_PTR(PTR_ERR(alg));

	inst = crypto_alloc_instance("hmac", alg);
	if (IS_ERR(inst))
		goto out_put_alg;

	inst->alg.cra_flags = CRYPTO_ALG_TYPE_HASH;
	inst->alg.cra_priority = alg->cra_priority;
	inst->alg.cra_blocksize = alg->cra_blocksize;
	inst->alg.cra_alignmask = alg->cra_alignmask;
	inst->alg.cra_type = &crypto_hash_type;

	inst->alg.cra_hash.digestsize =
		(alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
		CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
				       alg->cra_digest.dia_digestsize;

	inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
				ALIGN(inst->alg.cra_blocksize * 2 +
				      inst->alg.cra_hash.digestsize,
				      sizeof(void *));

	inst->alg.cra_init = hmac_init_tfm;
	inst->alg.cra_exit = hmac_exit_tfm;

	inst->alg.cra_hash.init = hmac_init;
	inst->alg.cra_hash.update = hmac_update;
	inst->alg.cra_hash.final = hmac_final;
	inst->alg.cra_hash.digest = hmac_digest;
	inst->alg.cra_hash.setkey = hmac_setkey;

out_put_alg:
	crypto_mod_put(alg);
	return inst;
}

static struct crypto_template hmac_tmpl = {
	.name = "hmac",
	.alloc = hmac_alloc,
	.free = hmac_free,
	.module = THIS_MODULE,
};

static int __init hmac_module_init(void)
{
	return crypto_register_template(&hmac_tmpl);
}

static void __exit hmac_module_exit(void)
{
	crypto_unregister_template(&hmac_tmpl);
}

module_init(hmac_module_init);
module_exit(hmac_module_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("HMAC hash algorithm");