[PATCH] Use u64 for CCM data lengths
Jussi Kivilinna
jussi.kivilinna at iki.fi
Sun Dec 15 15:04:17 CET 2013
* cipher/cipher-ccm.c: Move code inside [HAVE_U64_TYPEDEF].
[!HAVE_U64_TYPEDEF] (_gcry_cipher_ccm_encrypt)
(_gcry_cipher_ccm_decrypt, _gcry_cipher_ccm_set_nonce)
(_gcry_cipher_ccm_authenticate, _gcry_cipher_ccm_get_tag)
(_gcry_cipher_ccm_check_tag): Dummy functions returning
GPG_ERROR_NOT_SUPPORTED.
* cipher/cipher-internal.h (gcry_cipher_handle.u_mode.ccm)
(_gcry_cipher_ccm_set_lengths): Move inside [HAVE_U64_TYPEDEF] and use
u64 instead of size_t for CCM data lengths.
* cipher/cipher.c (_gcry_cipher_open_internal, cipher_reset)
(_gcry_cipher_ctl) [!HAVE_U64_TYPEDEF]: Return GPG_ERR_NOT_SUPPORTED
for CCM.
(_gcry_cipher_ctl) [HAVE_U64_TYPEDEF]: Use u64 for
GCRYCTL_SET_CCM_LENGTHS length parameters.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
cipher/cipher-ccm.c | 88 +++++++++++++++++++++++++++++++++++++++++++---
cipher/cipher-internal.h | 11 ++++--
cipher/cipher.c | 12 ++++++
3 files changed, 100 insertions(+), 11 deletions(-)
diff --git a/cipher/cipher-ccm.c b/cipher/cipher-ccm.c
index d2b8841..47f2162 100644
--- a/cipher/cipher-ccm.c
+++ b/cipher/cipher-ccm.c
@@ -29,6 +29,9 @@
#include "bufhelp.h"
#include "./cipher-internal.h"
+/* We need a 64 bit type for this code. */
+#ifdef HAVE_U64_TYPEDEF
+
#define set_burn(burn, nburn) do { \
unsigned int __nburn = (nburn); \
@@ -149,14 +152,14 @@ _gcry_cipher_ccm_set_nonce (gcry_cipher_hd_t c, const unsigned char *nonce,
gcry_err_code_t
-_gcry_cipher_ccm_set_lengths (gcry_cipher_hd_t c, size_t encryptlen,
- size_t aadlen, size_t taglen)
+_gcry_cipher_ccm_set_lengths (gcry_cipher_hd_t c, u64 encryptlen, u64 aadlen,
+ u64 taglen)
{
unsigned int burn = 0;
unsigned char b0[16];
size_t noncelen = 15 - (c->u_iv.iv[0] + 1);
- size_t M = taglen;
- size_t M_;
+ u64 M = taglen;
+ u64 M_;
int i;
M_ = (M - 2) / 2;
@@ -203,7 +206,6 @@ _gcry_cipher_ccm_set_lengths (gcry_cipher_hd_t c, size_t encryptlen,
buf_put_be32(&b0[2], aadlen);
set_burn (burn, do_cbc_mac (c, b0, 6, 0));
}
-#ifdef HAVE_U64_TYPEDEF
else if (aadlen > (unsigned int)0xffffffff)
{
b0[0] = 0xff;
@@ -211,7 +213,6 @@ _gcry_cipher_ccm_set_lengths (gcry_cipher_hd_t c, size_t encryptlen,
buf_put_be64(&b0[2], aadlen);
set_burn (burn, do_cbc_mac (c, b0, 10, 0));
}
-#endif
/* Generate S_0 and increase counter. */
set_burn (burn, c->spec->encrypt ( &c->context.c, c->u_mode.ccm.s0,
@@ -364,3 +365,78 @@ _gcry_cipher_ccm_decrypt (gcry_cipher_hd_t c, unsigned char *outbuf,
return err;
}
+
+#else
+
+/*
+ * Provide dummy functions so that we avoid adding too much #ifdefs in
+ * cipher.c.
+ */
+
+gcry_err_code_t
+_gcry_cipher_ccm_encrypt(gcry_cipher_hd_t c, unsigned char *outbuf,
+ size_t outbuflen, const unsigned char *inbuf,
+ size_t inbuflen)
+{
+ (void)c;
+ (void)outbuf;
+ (void)outbuflen;
+ (void)inbuf;
+ (void)inbuflen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+gcry_err_code_t
+_gcry_cipher_ccm_decrypt(gcry_cipher_hd_t c, unsigned char *outbuf,
+ size_t outbuflen, const unsigned char *inbuf,
+ size_t inbuflen)
+{
+ (void)c;
+ (void)outbuf;
+ (void)outbuflen;
+ (void)inbuf;
+ (void)inbuflen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+gcry_err_code_t
+_gcry_cipher_ccm_set_nonce(gcry_cipher_hd_t c, const unsigned char *nonce,
+ size_t noncelen)
+{
+ (void)c;
+ (void)nonce;
+ (void)noncelen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+gcry_err_code_t
+_gcry_cipher_ccm_authenticate(gcry_cipher_hd_t c, const unsigned char *abuf,
+ size_t abuflen)
+{
+ (void)c;
+ (void)abuf;
+ (void)abuflen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+gcry_err_code_t
+_gcry_cipher_ccm_get_tag(gcry_cipher_hd_t c, unsigned char *outtag,
+ size_t taglen)
+{
+ (void)c;
+ (void)outtag;
+ (void)taglen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+gcry_err_code_t
+_gcry_cipher_ccm_check_tag(gcry_cipher_hd_t c, const unsigned char *intag,
+ size_t taglen)
+{
+ (void)c;
+ (void)intag;
+ (void)taglen;
+ return GPG_ERR_NOT_SUPPORTED;
+}
+
+#endif /*HAVE_U64_TYPEDEF*/
diff --git a/cipher/cipher-internal.h b/cipher/cipher-internal.h
index 6fb3bac..cdac445 100644
--- a/cipher/cipher-internal.h
+++ b/cipher/cipher-internal.h
@@ -135,10 +135,11 @@ struct gcry_cipher_handle
int unused; /* Number of unused bytes in LASTIV. */
union {
+#ifdef HAVE_U64_TYPEDEF
/* Mode specific storage for CCM mode. */
struct {
- size_t encryptlen;
- size_t aadlen;
+ u64 encryptlen;
+ u64 aadlen;
unsigned int authlen;
/* Space to save partial input lengths for MAC. */
@@ -151,6 +152,7 @@ struct gcry_cipher_handle
unsigned int lengths:1; /* Set to 1 if CCM length parameters has been
processed. */
} ccm;
+#endif
/* Mode specific storage for CMAC mode. */
struct {
@@ -280,9 +282,10 @@ gcry_err_code_t _gcry_cipher_ccm_set_nonce
size_t noncelen);
gcry_err_code_t _gcry_cipher_ccm_authenticate
/* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen);
+#ifdef HAVE_U64_TYPEDEF
gcry_err_code_t _gcry_cipher_ccm_set_lengths
-/* */ (gcry_cipher_hd_t c, size_t encryptedlen, size_t aadlen,
- size_t taglen);
+/* */ (gcry_cipher_hd_t c, u64 encryptedlen, u64 aadlen, u64 taglen);
+#endif
gcry_err_code_t _gcry_cipher_ccm_get_tag
/* */ (gcry_cipher_hd_t c,
unsigned char *outtag, size_t taglen);
diff --git a/cipher/cipher.c b/cipher/cipher.c
index 8b47abc..4c7b5b5 100644
--- a/cipher/cipher.c
+++ b/cipher/cipher.c
@@ -394,11 +394,15 @@ _gcry_cipher_open_internal (gcry_cipher_hd_t *handle,
switch (mode)
{
case GCRY_CIPHER_MODE_CCM:
+#ifdef HAVE_U64_TYPEDEF
if (spec->blocksize != GCRY_CCM_BLOCK_LEN)
err = GPG_ERR_INV_CIPHER_MODE;
if (!spec->encrypt || !spec->decrypt)
err = GPG_ERR_INV_CIPHER_MODE;
break;
+#else
+ err = GPG_ERR_NOT_SUPPORTED;
+#endif
case GCRY_CIPHER_MODE_ECB:
case GCRY_CIPHER_MODE_CBC:
@@ -686,9 +690,11 @@ cipher_reset (gcry_cipher_hd_t c)
}
break;
+#ifdef HAVE_U64_TYPEDEF
case GCRY_CIPHER_MODE_CCM:
memset (&c->u_mode.ccm, 0, sizeof c->u_mode.ccm);
break;
+#endif
default:
break; /* u_mode unused by other modes. */
@@ -1139,8 +1145,9 @@ _gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
break;
case GCRYCTL_SET_CCM_LENGTHS:
+#ifdef HAVE_U64_TYPEDEF
{
- size_t params[3];
+ u64 params[3];
size_t encryptedlen;
size_t aadlen;
size_t authtaglen;
@@ -1160,6 +1167,9 @@ _gcry_cipher_ctl (gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
rc = _gcry_cipher_ccm_set_lengths (h, encryptedlen, aadlen, authtaglen);
}
+#else
+ rc = GPG_ERR_NOT_SUPPORTED;
+#endif
break;
case GCRYCTL_DISABLE_ALGO:
More information about the Gcrypt-devel
mailing list