Skip to contentSkip to conversation

crypto/vmac: adjust for Misra C:2012 rule 17.5

PATCH In review Jan Beulich 2 participants 4 messages May 13 – May 22

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto/vmac: adjust for Misra C:2012 rule 17.5

... ("The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements"). Instead of casts, (ab)use unions.

---
https://gitlab.com/xen-project/hardware/xen-staging/-/jobs/14354119193
(also covering the 20.12 patch)

There are also two cautions (in vmac()), which aren't dealt with for now.

xen/crypto/vmac.c +21 -21
@@ -926,41 +926,41 @@ uint64_t vmac(unsigned char m[],
 
 void vmac_set_key(const unsigned char user_key[], vmac_ctx_t *ctx)
 {
-    uint64_t in[2] = {0}, out[2];
+    union {
+        uint64_t q[2];
+        uint8_t b[16];
+    } in, out;
     unsigned i;
     aes_key_setup(user_key, &ctx->cipher_key);
     
     /* Fill nh key */
-    ((unsigned char *)in)[0] = 0x80; 
+    in = (typeof(in)){ .b[0] = 0x80 };
     for (i = 0; i < sizeof(ctx->nhkey)/8; i+=2) {
-        aes_encryption((unsigned char *)in, (unsigned char *)out,
-                                                         &ctx->cipher_key);
-        ctx->nhkey[i  ] = get64BE(out);
-        ctx->nhkey[i+1] = get64BE(out+1);
-        ((unsigned char *)in)[15] += 1;
+        aes_encryption(in.b, out.b, &ctx->cipher_key);
+        ctx->nhkey[i  ] = get64BE(out.q);
+        ctx->nhkey[i+1] = get64BE(out.q + 1);
+        in.b[15] += 1;
     }
 
     /* Fill poly key */
-    ((unsigned char *)in)[0] = 0xC0; 
-    in[1] = 0;
+    in.b[0] = 0xC0;
+    in.q[1] = 0;
     for (i = 0; i < sizeof(ctx->polykey)/8; i+=2) {
-        aes_encryption((unsigned char *)in, (unsigned char *)out,
-                                                         &ctx->cipher_key);
-        ctx->polytmp[i  ] = ctx->polykey[i  ] = get64BE(out) & mpoly;
-        ctx->polytmp[i+1] = ctx->polykey[i+1] = get64BE(out+1) & mpoly;
-        ((unsigned char *)in)[15] += 1;
+        aes_encryption(in.b, out.b, &ctx->cipher_key);
+        ctx->polytmp[i  ] = ctx->polykey[i  ] = get64BE(out.q) & mpoly;
+        ctx->polytmp[i+1] = ctx->polykey[i+1] = get64BE(out.q + 1) & mpoly;
+        in.b[15] += 1;
     }
 
     /* Fill ip key */
-    ((unsigned char *)in)[0] = 0xE0;
-    in[1] = 0;
+    in.b[0] = 0xE0;
+    in.q[1] = 0;
     for (i = 0; i < sizeof(ctx->l3key)/8; i+=2) {
         do {
-            aes_encryption((unsigned char *)in, (unsigned char *)out,
-                                                         &ctx->cipher_key);
-            ctx->l3key[i  ] = get64BE(out);
-            ctx->l3key[i+1] = get64BE(out+1);
-            ((unsigned char *)in)[15] += 1;
+            aes_encryption(in.b, out.b, &ctx->cipher_key);
+            ctx->l3key[i  ] = get64BE(out.q);
+            ctx->l3key[i+1] = get64BE(out.q + 1);
+            in.b[15] += 1;
         } while (ctx->l3key[i] >= p64 || ctx->l3key[i+1] >= p64);
     }
     
#1 lore raw
Quoted 5 lines · Jan Beulich

... ("The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements"). Instead of casts, (ab)use unions.

The Rule 17.5 violations aren't reported against this function.  They look to be addressed by one of your other patches.

Which rules is this addressing?  I can't locate anything referencing this function.

~Andrew

#2 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

... ("The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements"). Instead of casts, (ab)use unions.

The Rule 17.5 violations aren't reported against this function.

The report is against uses of aes_encryption() from this function.

Quoted 1 line

  They look to be addressed by one of your other patches.

I don't think so. That other patch is independent.

Quoted 2 lines

Which rules is this addressing?  I can't locate anything referencing this function.

It's still very much 17.5, which the job runs also confirm (the other patch didn't eliminate the violations here, while this patch does).

Jan

#3 lore raw
Quoted 9 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

... ("The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements"). Instead of casts, (ab)use unions.

The Rule 17.5 violations aren't reported against this function.

The report is against uses of aes_encryption() from this function.

Oh, that's very well hidden in the report.  Both the top and bottom of it say rijndaelEncrypt().

Quoted 7 lines
xen/crypto/vmac.c +0 -1
@@ -926,41 +926,41 @@ uint64_t vmac(unsigned char m[],
 
 void vmac_set_key(const unsigned char user_key[], vmac_ctx_t *ctx)
 {
-    uint64_t in[2] = {0}, out[2];

xen/crypto/vmac.c uint64_t vmac(unsigned char m[], · line 937

Quoted 10 lines
xen/crypto/vmac.c +5 -1
+    union {
+        uint64_t q[2];
+        uint8_t b[16];
+    } in, out;
     unsigned i;
     aes_key_setup(user_key, &ctx->cipher_key);
 
     /* Fill nh key */
-    ((unsigned char *)in)[0] = 0x80; 
+    in = (typeof(in)){ .b[0] = 0x80 };

typeof like this is not good for legibility.

In this case, I'd prefer to keep the "} in = {}, out;" pattern from before, and this line be a direct translation like the others in the file.

With that, Reviewed-by: Andrew Cooper <[email protected]>