blowfish.c 3.4 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Cryptographic API.
 *
 * Blowfish Cipher Algorithm, by Bruce Schneier.
 * http://www.counterpane.com/blowfish.html
 *
 * Adapted from Kerneli implementation.
 *
 * Copyright (c) Herbert Valerio Riedel <hvr@hvrlab.org>
 * Copyright (c) Kyle McMartin <kyle@debian.org>
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 *
 * 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/init.h>
#include <linux/module.h>
#include <linux/mm.h>
22
#include <asm/byteorder.h>
L
Linus Torvalds 已提交
23
#include <linux/crypto.h>
24
#include <linux/types.h>
25
#include <crypto/blowfish.h>
L
Linus Torvalds 已提交
26

27
/*
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
 * Round loop unrolling macros, S is a pointer to a S-Box array
 * organized in 4 unsigned longs at a row.
 */
#define GET32_3(x) (((x) & 0xff))
#define GET32_2(x) (((x) >> (8)) & (0xff))
#define GET32_1(x) (((x) >> (16)) & (0xff))
#define GET32_0(x) (((x) >> (24)) & (0xff))

#define bf_F(x) (((S[GET32_0(x)] + S[256 + GET32_1(x)]) ^ \
          S[512 + GET32_2(x)]) + S[768 + GET32_3(x)])

#define ROUND(a, b, n)  b ^= P[n]; a ^= bf_F (b)

41
static void bf_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
L
Linus Torvalds 已提交
42
{
43 44 45 46 47 48 49
	struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
	const __be32 *in_blk = (const __be32 *)src;
	__be32 *const out_blk = (__be32 *)dst;
	const u32 *P = ctx->p;
	const u32 *S = ctx->s;
	u32 yl = be32_to_cpu(in_blk[0]);
	u32 yr = be32_to_cpu(in_blk[1]);
L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

	ROUND(yr, yl, 0);
	ROUND(yl, yr, 1);
	ROUND(yr, yl, 2);
	ROUND(yl, yr, 3);
	ROUND(yr, yl, 4);
	ROUND(yl, yr, 5);
	ROUND(yr, yl, 6);
	ROUND(yl, yr, 7);
	ROUND(yr, yl, 8);
	ROUND(yl, yr, 9);
	ROUND(yr, yl, 10);
	ROUND(yl, yr, 11);
	ROUND(yr, yl, 12);
	ROUND(yl, yr, 13);
	ROUND(yr, yl, 14);
	ROUND(yl, yr, 15);

	yl ^= P[16];
	yr ^= P[17];

71 72
	out_blk[0] = cpu_to_be32(yr);
	out_blk[1] = cpu_to_be32(yl);
L
Linus Torvalds 已提交
73 74
}

75
static void bf_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
L
Linus Torvalds 已提交
76
{
77
	struct bf_ctx *ctx = crypto_tfm_ctx(tfm);
L
Linus Torvalds 已提交
78 79
	const __be32 *in_blk = (const __be32 *)src;
	__be32 *const out_blk = (__be32 *)dst;
80 81
	const u32 *P = ctx->p;
	const u32 *S = ctx->s;
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	u32 yl = be32_to_cpu(in_blk[0]);
	u32 yr = be32_to_cpu(in_blk[1]);

	ROUND(yr, yl, 17);
	ROUND(yl, yr, 16);
	ROUND(yr, yl, 15);
	ROUND(yl, yr, 14);
	ROUND(yr, yl, 13);
	ROUND(yl, yr, 12);
	ROUND(yr, yl, 11);
	ROUND(yl, yr, 10);
	ROUND(yr, yl, 9);
	ROUND(yl, yr, 8);
	ROUND(yr, yl, 7);
	ROUND(yl, yr, 6);
	ROUND(yr, yl, 5);
	ROUND(yl, yr, 4);
	ROUND(yr, yl, 3);
	ROUND(yl, yr, 2);

	yl ^= P[1];
	yr ^= P[0];

	out_blk[0] = cpu_to_be32(yr);
	out_blk[1] = cpu_to_be32(yl);
}

static struct crypto_alg alg = {
	.cra_name		=	"blowfish",
	.cra_flags		=	CRYPTO_ALG_TYPE_CIPHER,
	.cra_blocksize		=	BF_BLOCK_SIZE,
	.cra_ctxsize		=	sizeof(struct bf_ctx),
114
	.cra_alignmask		=	3,
L
Linus Torvalds 已提交
115 116 117 118 119
	.cra_module		=	THIS_MODULE,
	.cra_list		=	LIST_HEAD_INIT(alg.cra_list),
	.cra_u			=	{ .cipher = {
	.cia_min_keysize	=	BF_MIN_KEY_SIZE,
	.cia_max_keysize	=	BF_MAX_KEY_SIZE,
120
	.cia_setkey		=	blowfish_setkey,
L
Linus Torvalds 已提交
121 122 123 124
	.cia_encrypt 		=	bf_encrypt,
	.cia_decrypt  		=	bf_decrypt } }
};

125
static int __init blowfish_mod_init(void)
L
Linus Torvalds 已提交
126 127 128 129
{
	return crypto_register_alg(&alg);
}

130
static void __exit blowfish_mod_fini(void)
L
Linus Torvalds 已提交
131 132 133 134
{
	crypto_unregister_alg(&alg);
}

135 136
module_init(blowfish_mod_init);
module_exit(blowfish_mod_fini);
L
Linus Torvalds 已提交
137 138 139

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Blowfish Cipher Algorithm");