svn commit: r367524 - stable/12/sys/opencrypto

Mark Johnston markj at FreeBSD.org
Mon Nov 9 14:15:07 UTC 2020


Author: markj
Date: Mon Nov  9 14:15:06 2020
New Revision: 367524
URL: https://svnweb.freebsd.org/changeset/base/367524

Log:
  Add hmac_init_ipad() and hmac_init_opad()
  
  This is a merge of a small portion of r359374 which makes it easier
  to maintain OpenCrypto drivers in stable/12.  No functional change
  intended; this is a direct commit to stable/12.
  
  Discussed with:	jhb

Modified:
  stable/12/sys/opencrypto/crypto.c
  stable/12/sys/opencrypto/cryptodev.h

Modified: stable/12/sys/opencrypto/crypto.c
==============================================================================
--- stable/12/sys/opencrypto/crypto.c	Mon Nov  9 13:54:29 2020	(r367523)
+++ stable/12/sys/opencrypto/crypto.c	Mon Nov  9 14:15:06 2020	(r367524)
@@ -380,6 +380,53 @@ crypto_terminate(struct proc **pp, void *q)
 }
 
 static void
+hmac_init_pad(const struct auth_hash *axf, const char *key, int klen,
+    void *auth_ctx, uint8_t padval)
+{
+	uint8_t hmac_key[HMAC_MAX_BLOCK_LEN];
+	u_int i;
+
+	KASSERT(axf->blocksize <= sizeof(hmac_key),
+	    ("Invalid HMAC block size %d", axf->blocksize));
+
+	/*
+	 * If the key is larger than the block size, use the digest of
+	 * the key as the key instead.
+	 */
+	memset(hmac_key, 0, sizeof(hmac_key));
+	if (klen > axf->blocksize) {
+		axf->Init(auth_ctx);
+		axf->Update(auth_ctx, key, klen);
+		axf->Final(hmac_key, auth_ctx);
+		klen = axf->hashsize;
+	} else
+		memcpy(hmac_key, key, klen);
+
+	for (i = 0; i < axf->blocksize; i++)
+		hmac_key[i] ^= padval;
+
+	axf->Init(auth_ctx);
+	axf->Update(auth_ctx, hmac_key, axf->blocksize);
+	explicit_bzero(hmac_key, sizeof(hmac_key));
+}
+
+void
+hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
+    void *auth_ctx)
+{
+
+	hmac_init_pad(axf, key, klen, auth_ctx, HMAC_IPAD_VAL);
+}
+
+void
+hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
+    void *auth_ctx)
+{
+
+	hmac_init_pad(axf, key, klen, auth_ctx, HMAC_OPAD_VAL);
+}
+
+static void
 crypto_destroy(void)
 {
 	struct crypto_ret_worker *ret_worker;

Modified: stable/12/sys/opencrypto/cryptodev.h
==============================================================================
--- stable/12/sys/opencrypto/cryptodev.h	Mon Nov  9 13:54:29 2020	(r367523)
+++ stable/12/sys/opencrypto/cryptodev.h	Mon Nov  9 14:15:06 2020	(r367524)
@@ -546,6 +546,14 @@ extern	int crypto_usercrypto;		/* userland may do cryp
 extern	int crypto_userasymcrypto;	/* userland may do asym crypto reqs */
 extern	int crypto_devallowsoft;	/* only use hardware crypto */
 
+/* Helper routines for drivers to initialize auth contexts for HMAC. */
+struct auth_hash;
+
+void	hmac_init_ipad(const struct auth_hash *axf, const char *key, int klen,
+    void *auth_ctx);
+void	hmac_init_opad(const struct auth_hash *axf, const char *key, int klen,
+    void *auth_ctx);
+
 /*
  * Crypto-related utility routines used mainly by drivers.
  *


More information about the svn-src-all mailing list