From cfc888919dc603dd4831f373d26b9ef9956b9285 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Wed, 30 Apr 2014 22:12:40 +0200
Subject: [PATCH] New macros related to ccm nonce size.

---
 ChangeLog | 10 ++++++++++
 ccm.c     |  7 ++-----
 ccm.h     | 10 ++++++++++
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 64d7a732..64781e5d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2014-04-30  Niels Möller  <nisse@lysator.liu.se>
+
+	* ccm.c (CCM_IV_MAX_SIZE, CCM_IV_MIN_SIZE): Deleted, replaced by
+	public constants CCM_MIN_NONCE_SIZE and CCM_MAX_NONCE_SIZE.
+	(ccm_build_iv): Updated for above rename.
+	(CCM_L_MAX_SIZE): Deleted, no longer used.
+
+	* ccm.h (CCM_MIN_NONCE_SIZE, CCM_MAX_NONCE_SIZE): New constants.
+	(CCM_MAX_MSG_SIZE): New macro.
+
 2014-04-27  Niels Möller  <nisse@lysator.liu.se>
 
 	* nettle.texinfo (Cipher modes): Subsection on AEAD constructions.
diff --git a/ccm.c b/ccm.c
index 00f3f26e..5ca6e054 100644
--- a/ccm.c
+++ b/ccm.c
@@ -68,9 +68,6 @@
 #define CCM_OFFSET_FLAGS    0
 #define CCM_OFFSET_NONCE    1
 #define CCM_L_SIZE(_nlen_)  (CCM_BLOCK_SIZE - CCM_OFFSET_NONCE - (_nlen_))
-#define CCM_L_MAX_SIZE      (CCM_FLAG_L+1)
-#define CCM_IV_MAX_SIZE     (CCM_BLOCK_SIZE - CCM_OFFSET_NONCE - 1)
-#define CCM_IV_MIN_SIZE     (CCM_BLOCK_SIZE - CCM_OFFSET_NONCE - CCM_L_MAX_SIZE)
 
 /*
  * The data input to the CBC-MAC: L(a) | adata | padding | plaintext | padding
@@ -104,8 +101,8 @@ ccm_build_iv(uint8_t *iv, size_t noncelen, const uint8_t *nonce,
   unsigned int i;
 
   /* Sanity check the nonce length. */
-  assert(noncelen >= CCM_IV_MIN_SIZE);
-  assert(noncelen <= CCM_IV_MAX_SIZE);
+  assert(noncelen >= CCM_MIN_NONCE_SIZE);
+  assert(noncelen <= CCM_MAX_NONCE_SIZE);
 
   /* Generate the IV */
   iv[CCM_OFFSET_FLAGS] = flags | CCM_FLAG_SET_L(CCM_L_SIZE(noncelen));
diff --git a/ccm.h b/ccm.h
index 9827e45d..271638b6 100644
--- a/ccm.h
+++ b/ccm.h
@@ -89,6 +89,16 @@ extern "C" {
 
 /* For CCM, the block size of the block cipher shall be 128 bits. */
 #define CCM_BLOCK_SIZE  16
+#define CCM_MIN_NONCE_SIZE 7
+#define CCM_MAX_NONCE_SIZE 14
+
+/* Maximum cleartext message size, as a function of the nonce size N.
+   The length field is L octets, with L = 15 - N, and then the maximum
+   size M = 2^{8L} - 1. */
+#define CCM_MAX_MSG_SIZE(N)			\
+  ((sizeof(size_t) + (N) <= 15)			\
+   ? ~(size_t) 0				\
+   : ((size_t) 1 << (8*(15 - N))) - 1)
 
 /* Per-message state */
 struct ccm_ctx {
-- 
GitLab