mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Clay Haapala <chaapala@cisco.com>
To: James Morris <jmorris@redhat.com>
Cc: Jouni Malinen <jkmaline@cc.hut.fi>,
	"David S. Miller" <davem@redhat.com>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCH] crypto/crc32c support
Date: Mon, 22 Mar 2004 10:56:45 -0600	[thread overview]
Message-ID: <yqujlllslt0y.fsf@chaapala-lnx2.cisco.com> (raw)
In-Reply-To: <Xine.LNX.4.44.0403211006190.16503-100000@thoron.boston.redhat.com> (James Morris's message of "Sun, 21 Mar 2004 10:08:04 -0500 (EST)")

This patch agains 2.6.4 kernel code implements the CRC32C checksum/crc
algorithm as an additional type of digest.  The implementation is a
wrapper for routines found under lib/libcrc32c, available in another
patch.

This crypto patch requires the digest setkey() interface implemented
by Jouni Malinen <jkmaline@cc.hut.fi>, and supercedes an earlier patch
that implemented chksum as an additional CRYPTO_ALG_TYPE.

The immediate customer of these routines is the linux-iscsi driver,
currently in review.  Please see the linux-iscsi project on
SourceForge for details.

-- 
Clay Haapala (chaapala@cisco.com) Cisco Systems SRBU +1 763-398-1056
   6450 Wedgwood Rd, Suite 130 Maple Grove MN 55311 PGP: C89240AD
     Of course the drugs advertised in all those emails are safe.
		      Show me the dead spammers!
			      (Please!)

diff -urN linux-2.6.4.orig/crypto/crc32c.c linux/crypto/crc32c.c
--- linux-2.6.4.orig/crypto/crc32c.c	1969-12-31 18:00:00.000000000 -0600
+++ linux/crypto/crc32c.c	2004-03-18 16:09:05.000000000 -0600
@@ -0,0 +1,110 @@
+/* 
+ * Cryptographic API.
+ *
+ * CRC32C chksum
+ *
+ * This module file is a wrapper to invoke the lib/crc32c routines.
+ *
+ * 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/string.h>
+#include <linux/crypto.h>
+#include <linux/crc32c.h>
+#include <asm/byteorder.h>
+
+#define CHKSUM_BLOCK_SIZE	32
+#define CHKSUM_DIGEST_SIZE	4
+
+struct chksum_ctx {
+	u32 crc;
+};
+
+/*
+ * Steps through buffer one byte at at time, calculates reflected 
+ * crc using table.
+ */
+
+static void chksum_init(void *ctx)
+{
+	struct chksum_ctx *mctx = ctx;
+
+	mctx->crc = ~(u32)0;			/* common usage */
+}
+
+/*
+ * Setting the seed allows arbitrary accumulators and flexible XOR policy
+ * If your algorithm starts with ~0, then XOR with ~0 before you set
+ * the seed.
+ */
+static int chksum_setkey(void *ctx, const u8 *key, unsigned int keylen,
+	                  u32 *flags)
+{
+	struct chksum_ctx *mctx = ctx;
+
+	if (keylen != sizeof(mctx->crc)) {
+		if (flags)
+			*flags = CRYPTO_TFM_RES_BAD_KEY_LEN;
+		return -EINVAL;
+	}
+	mctx->crc = __cpu_to_le32(*(u32 *)key);
+	return 0;
+}
+
+static void chksum_update(void *ctx, const u8 *data, size_t length)
+{
+	struct chksum_ctx *mctx = ctx;
+	u32 mcrc;
+
+	mcrc = crc32c(mctx->crc, data, length);
+
+	mctx->crc = mcrc;
+}
+
+static void chksum_final(void *ctx, u8 *out)
+{
+	struct chksum_ctx *mctx = ctx;
+	u32 mcrc = (mctx->crc ^ ~(u32)0);
+	
+	*(u32 *)out = __le32_to_cpu(mcrc);
+}
+
+static struct crypto_alg alg = {
+	.cra_name	=	"crc32c",
+	.cra_flags	=	CRYPTO_ALG_TYPE_DIGEST,
+	.cra_blocksize	=	CHKSUM_BLOCK_SIZE,
+	.cra_ctxsize	=	sizeof(struct chksum_ctx),
+	.cra_module	=	THIS_MODULE,
+	.cra_list	=	LIST_HEAD_INIT(alg.cra_list),
+	.cra_u		=	{
+		.digest = {
+			 .dia_digestsize=	CHKSUM_DIGEST_SIZE,
+			 .dia_setkey	=	chksum_setkey,
+			 .dia_init   	= 	chksum_init,
+			 .dia_update 	=	chksum_update,
+			 .dia_final  	=	chksum_final
+		 }
+	}
+};
+
+static int __init init(void)
+{
+	return crypto_register_alg(&alg);
+}
+
+static void __exit fini(void)
+{
+	crypto_unregister_alg(&alg);
+}
+
+module_init(init);
+module_exit(fini);
+
+MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
+MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
+MODULE_LICENSE("GPL");
diff -urN linux-2.6.4.orig/crypto/Kconfig linux/crypto/Kconfig
--- linux-2.6.4.orig/crypto/Kconfig	2004-03-17 15:23:06.000000000 -0600
+++ linux/crypto/Kconfig	2004-03-18 15:24:57.000000000 -0600
@@ -170,6 +170,16 @@
 	  should not be used for other purposes because of the weakness
 	  of the algorithm.
 
+config CRYPTO_CRC32C
+	tristate "CRC32c CRC algorithm"
+	depends on CRYPTO
+	select LIBCRC32C
+	help
+	  Castagnoli, et al Cyclic Redundancy-Check Algorithm.  Used
+	  by iSCSI for header and data digests and by others.
+	  See Castagnoli93.  This implementation uses lib/libcrc32c.
+          Module will be crc32c.
+
 config CRYPTO_TEST
 	tristate "Testing module"
 	depends on CRYPTO
diff -urN linux-2.6.4.orig/crypto/Makefile linux/crypto/Makefile
--- linux-2.6.4.orig/crypto/Makefile	2004-03-17 15:23:06.000000000 -0600
+++ linux/crypto/Makefile	2004-03-18 15:44:54.000000000 -0600
@@ -24,5 +24,6 @@
 obj-$(CONFIG_CRYPTO_ARC4) += arc4.o
 obj-$(CONFIG_CRYPTO_DEFLATE) += deflate.o
 obj-$(CONFIG_CRYPTO_MICHAEL_MIC) += michael_mic.o
+obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
 
 obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
diff -urN linux-2.6.4.orig/crypto/tcrypt.c linux/crypto/tcrypt.c
--- linux-2.6.4.orig/crypto/tcrypt.c	2004-03-17 15:23:06.000000000 -0600
+++ linux/crypto/tcrypt.c	2004-03-18 16:36:41.000000000 -0600
@@ -61,7 +61,7 @@
 static char *check[] = {
 	"des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
 	"twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6", 
-	"arc4", "michael_mic", "deflate", NULL
+	"arc4", "michael_mic", "deflate", "crc32c", NULL
 };
 
 static void
@@ -496,6 +496,107 @@
 }
 
 static void
+test_crc32c(void)
+{
+#define NUMVEC 6
+#define VECSIZE 40
+
+	int i, j, pass;
+	u32 crc;
+	u8 b, test_vec[NUMVEC][VECSIZE];
+	static u32 vec_results[NUMVEC] = {
+		0x0e2c157f, 0xe980ebf6, 0xde74bded,
+		0xd579c862, 0xba979ad0, 0x2b29d913
+	};
+	static u32 tot_vec_results = 0x24c5d375;
+	
+	struct scatterlist sg[NUMVEC];
+	struct crypto_tfm *tfm;
+	char *fmtdata = "testing crc32c initialized to %08x: %s\n";
+#define SEEDTESTVAL 0xedcba987
+	u32 seed;
+
+	printk("\ntesting crc32c\n");
+
+	tfm = crypto_alloc_tfm("crc32c", 0);
+	if (tfm == NULL) {
+		printk("failed to load transform for crc32c\n");
+		return;
+	}
+	
+	crypto_digest_init(tfm);
+	crypto_digest_final(tfm, (u8*)&crc);
+	printk(fmtdata, crc, (crc == 0) ? "pass" : "ERROR");
+	
+	/*
+	 * stuff test_vec with known values, simple incrementing
+	 * byte values.
+	 */
+	b = 0;
+	for (i = 0; i < NUMVEC; i++) {
+		for (j = 0; j < VECSIZE; j++) 
+			test_vec[i][j] = ++b;
+		sg[i].page = virt_to_page(test_vec[i]);
+		sg[i].offset = offset_in_page(test_vec[i]);
+		sg[i].length = VECSIZE;
+	}
+
+	seed = SEEDTESTVAL;
+	(void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
+	crypto_digest_final(tfm, (u8*)&crc);
+	printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ?
+	       "pass" : "ERROR");
+	
+	printk("testing crc32c using update/final:\n");
+
+	pass = 1;		    /* assume all is well */
+	
+	for (i = 0; i < NUMVEC; i++) {
+		seed = ~(u32)0;
+		(void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
+		crypto_digest_update(tfm, &sg[i], 1);
+		crypto_digest_final(tfm, (u8*)&crc);
+		if (crc == vec_results[i]) {
+			printk(" %08x:OK", crc);
+		} else {
+			printk(" %08x:BAD, wanted %08x\n", crc, vec_results[i]);
+			pass = 0;
+		}
+	}
+
+	printk("\ntesting crc32c using incremental accumulator:\n");
+	crc = 0;
+	for (i = 0; i < NUMVEC; i++) {
+		seed = (crc ^ ~(u32)0);
+		(void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
+		crypto_digest_update(tfm, &sg[i], 1);
+		crypto_digest_final(tfm, (u8*)&crc);
+	}
+	if (crc == tot_vec_results) {
+		printk(" %08x:OK", crc);
+	} else {
+		printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
+		pass = 0;
+	}
+
+	printk("\ntesting crc32c using digest:\n");
+	seed = ~(u32)0;
+	(void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32));
+	crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc);
+	if (crc == tot_vec_results) {
+		printk(" %08x:OK", crc);
+	} else {
+		printk(" %08x:BAD, wanted %08x\n", crc, tot_vec_results);
+		pass = 0;
+	}
+	
+	printk("\n%s\n", pass ? "pass" : "ERROR");
+
+	crypto_free_tfm(tfm);
+	printk("crc32c test complete\n");
+}
+
+static void
 test_available(void)
 {
 	char **name = check;
@@ -566,7 +667,8 @@
 
 		test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
 		test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
-		test_deflate();		
+		test_deflate();
+		test_crc32c();
 #ifdef CONFIG_CRYPTO_HMAC
 		test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);
 		test_hmac("sha1", hmac_sha1_tv_template, HMAC_SHA1_TEST_VECTORS);		
@@ -657,6 +759,10 @@
 		test_hash("michael_mic", michael_mic_tv_template, MICHAEL_MIC_TEST_VECTORS);
 		break;
 
+	case 18:
+		test_crc32c();
+		break;
+
 #ifdef CONFIG_CRYPTO_HMAC
 	case 100:
 		test_hmac("md5", hmac_md5_tv_template, HMAC_MD5_TEST_VECTORS);

  parent reply	other threads:[~2004-03-22 16:57 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-06 18:46 Crypto API and keyed non-HMAC digest algorithms / Michael MIC Jouni Malinen
2004-03-08 14:45 ` James Morris
2004-03-09  3:37   ` Jouni Malinen
2004-03-09  5:55     ` James Morris
2004-03-09 17:30       ` Clay Haapala
2004-03-09 20:32         ` James Morris
2004-03-10  3:40           ` Jouni Malinen
2004-03-10  4:02             ` [PATCH] Crypto API and " Jouni Malinen
2004-03-10  4:21             ` Crypto API and keyed non-HMAC digest algorithms / " James Morris
2004-03-10  5:34               ` Jouni Malinen
2004-03-10 15:45                 ` James Morris
2004-03-11  3:00                   ` Jouni Malinen
2004-03-11  4:06                     ` James Morris
2004-03-11  6:08                       ` Jouni Malinen
2004-03-11 16:34                         ` Clay Haapala
2004-03-11 20:14                           ` James Morris
2004-03-18 23:05                             ` Clay Haapala
2004-03-21 15:08                               ` James Morris
2004-03-22 16:49                                 ` [PATCH] lib/libcrc32c implementation Clay Haapala
2004-03-22 17:00                                   ` Jeff Garzik
2004-03-22 17:27                                     ` Clay Haapala
2004-03-22 17:36                                       ` Jeff Garzik
2004-03-26 16:36                                   ` James Morris
2004-03-26 16:59                                     ` Clay Haapala
2004-03-26 18:16                                       ` James Morris
2004-03-30 17:32                                     ` [PATCH] lib/libcrc32c Clay Haapala
2004-03-30 18:43                                       ` Bartlomiej Zolnierkiewicz
2004-03-30 19:11                                         ` Clay Haapala
2004-03-30 19:23                                           ` Matt Domsch
2004-03-30 19:33                                             ` Clay Haapala
2004-03-31 21:03                                             ` [PATCH] lib/libcrc32c, revised 040331 Clay Haapala
2004-04-19 21:37                                               ` [PATCH] lib/libcrc32c, revised 040419 Clay Haapala
2004-04-23 23:42                                                 ` David S. Miller
2004-04-27 19:46                                                   ` [PATCH] lib/libcrc32c, revised 040427 Clay Haapala
2004-04-27 19:49                                                     ` David S. Miller
2004-04-27 20:00                                                       ` Clay Haapala
2004-04-27 21:49                                                         ` Clay Haapala
2004-04-27 21:50                                                           ` David S. Miller
2004-04-28 15:14                                                             ` [PATCH] lib/libcrc32c, revised 040428, with compiler.h included Clay Haapala
2004-04-28 19:29                                                               ` David S. Miller
2004-04-27 19:55                                                   ` [PATCH] lib/crc32.c: to use compiler.h defines Clay Haapala
2004-04-28 15:19                                                     ` [PATCH] lib/crc32.c: uses (and includes) compiler.h Clay Haapala
2004-04-28 19:30                                                       ` David S. Miller
2004-03-30 19:49                                           ` [PATCH] lib/libcrc32c Bartlomiej Zolnierkiewicz
2004-03-31 15:28                                             ` Clay Haapala
2004-03-30 17:40                                     ` [PATCH] crypto/crc32c implementation Clay Haapala
2004-04-19 21:31                                       ` [PATCH] crypto/crc32c implementation, updated 040419 Clay Haapala
2004-04-19 23:36                                         ` David S. Miller
2004-04-20 13:20                                           ` James Morris
2004-03-22 16:56                                 ` Clay Haapala [this message]
2004-03-11  6:11                       ` Crypto API and keyed non-HMAC digest algorithms / Michael MIC Jouni Malinen
2004-03-10 16:04             ` Clay Haapala

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yqujlllslt0y.fsf@chaapala-lnx2.cisco.com \
    --to=chaapala@cisco.com \
    --cc=davem@redhat.com \
    --cc=jkmaline@cc.hut.fi \
    --cc=jmorris@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome