symm/{chacha,salsa20}-{arm64,arm-neon}.S: Improve rotation code.
[catacomb] / symm / ccm-def.h
index 2d864fa..7c6bd2e 100644 (file)
@@ -338,9 +338,9 @@ int pre##_ccmencrypt(pre##_ccmctx *ctx,                                     \
   /* Determine the buffering plan.  Our buffer is going to do double-  \
    * duty here.  The end portion is going to contain mask from the     \
    * encrypted counter which we mix into the plaintext to encrypt it;  \
-   * the start portion, which originally mask bytes we've already used,        \
-   * will hold the input plaintext, which will eventually be           \
-   * collected into the CBC-MAC state.                                 \
+   * the start portion, which originally contained mask bytes we've    \
+   * already used, will hold the input plaintext, which will           \
+   * eventually be collected into the CBC-MAC state.                   \
    */                                                                  \
   rsvr_mkplan(&plan, &pre##_ccmpolicy, ctx->off, sz);                  \
                                                                        \
@@ -427,9 +427,9 @@ int pre##_ccmdecrypt(pre##_ccmctx *ctx,                                     \
   /* Determine the buffering plan.  Our buffer is going to do double-  \
    * duty here.  The end portion is going to contain mask from the     \
    * encrypted counter which we mix into the plaintext to encrypt it;  \
-   * the start portion, which originally mask bytes we've already used,        \
-   * will hold the recovered plaintext, which will eventually be       \
-   * collected into the CBC-MAC state.                                 \
+   * the start portion, which originally mask contained bytes we've    \
+   * already used, will hold the recovered plaintext, which will       \
+   * eventually be collected into the CBC-MAC state.                   \
    */                                                                  \
   rsvr_mkplan(&plan, &pre##_ccmpolicy, ctx->off, sz);                  \
                                                                        \
@@ -715,13 +715,23 @@ static gaead_key *gckey(const void *k, size_t ksz)                        \
   return (&key->k);                                                    \
 }                                                                      \
                                                                        \
+static int gcszok(size_t nsz, size_t hsz, size_t msz, size_t tsz)      \
+{                                                                      \
+  ccm_params p;                                                                \
+                                                                       \
+  if (!gaead_szokcommon(&pre##_ccm, nsz, hsz, msz, tsz)) return (0);   \
+  p.hsz = hsz; p.msz = msz; p.bsz = PRE##_BLKSZ; p.nsz = nsz; p.tsz = tsz; \
+  if (!ccm_check(&p)) return (0);                                      \
+  return (1);                                                          \
+}                                                                      \
+                                                                       \
 const gcaead pre##_ccm = {                                             \
   name "-ccm",                                                         \
   pre##_keysz, pre##_ccmnoncesz, pre##_ccmtagsz,                       \
   PRE##_BLKSZ, 0, 0,                                                   \
   AEADF_PCHSZ | AEADF_PCMSZ | AEADF_PCTSZ |                            \
   AEADF_AADNDEP | AEADF_AADFIRST,                                      \
-  gckey                                                                        \
+  gckey, gcszok                                                                \
 };                                                                     \
                                                                        \
 CCM_TESTX(PRE, pre, name, fname)
@@ -742,6 +752,7 @@ CCM_TESTX(PRE, pre, name, fname)
 #include <stdio.h>
 
 #include <mLib/dstr.h>
+#include <mLib/macros.h>
 #include <mLib/quis.h>
 #include <mLib/testrig.h>
 
@@ -802,8 +813,8 @@ static int ccmverify(dstr *v)                                               \
     d.len = BLEN(&b);                                                  \
                                                                        \
     if (d.len != v[4].len ||                                           \
-       memcmp(d.buf, v[4].buf, v[4].len) != 0 ||                       \
-       memcmp(t.buf, v[5].buf, v[5].len) != 0) {                       \
+       MEMCMP(d.buf, !=, v[4].buf, v[4].len) ||                        \
+       MEMCMP(t.buf, !=, v[5].buf, v[5].len)) {                        \
     fail_enc:                                                          \
       printf("\nfail encrypt:\n\tstep = %i", *ip);                     \
       fputs("\n\tkey = ", stdout); type_hex.dump(&v[0], stdout);       \
@@ -855,7 +866,7 @@ static int ccmverify(dstr *v)                                               \
     d.len = BLEN(&b);                                                  \
                                                                        \
     if (d.len != v[3].len || !win ||                                   \
-       memcmp(d.buf, v[3].buf, v[3].len) != 0) {                       \
+       MEMCMP(d.buf, !=, v[3].buf, v[3].len)) {                        \
     fail_dec:                                                          \
       printf("\nfail decrypt:\n\tstep = %i", *ip);                     \
       fputs("\n\tkey = ", stdout); type_hex.dump(&v[0], stdout);       \