LCOV - code coverage report
Current view: top level - crypto - cmac.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 52 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*      $OpenBSD: cmac.c,v 1.3 2017/05/02 17:07:06 mikeb Exp $  */
       2             : 
       3             : /*-
       4             :  * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
       5             :  *
       6             :  * Permission to use, copy, modify, and distribute this software for any
       7             :  * purpose with or without fee is hereby granted, provided that the above
       8             :  * copyright notice and this permission notice appear in all copies.
       9             :  *
      10             :  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      11             :  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      12             :  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
      13             :  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
      14             :  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
      15             :  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
      16             :  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
      17             :  */
      18             : 
      19             : /*
      20             :  * This code implements the CMAC (Cipher-based Message Authentication)
      21             :  * algorithm described in FIPS SP800-38B using the AES-128 cipher.
      22             :  */
      23             : 
      24             : #include <sys/param.h>
      25             : #include <sys/systm.h>
      26             : 
      27             : #include <crypto/aes.h>
      28             : #include <crypto/cmac.h>
      29             : 
      30             : #define LSHIFT(v, r) do {                                       \
      31             :         int i;                                                  \
      32             :         for (i = 0; i < 15; i++)                             \
      33             :                 (r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7;             \
      34             :         (r)[15] = (v)[15] << 1;                                   \
      35             : } while (0)
      36             : 
      37             : #define XOR(v, r) do {                                          \
      38             :         int i;                                                  \
      39             :         for (i = 0; i < 16; i++)                             \
      40             :                 (r)[i] ^= (v)[i];                               \
      41             : } while (0)
      42             : 
      43             : void
      44           0 : AES_CMAC_Init(AES_CMAC_CTX *ctx)
      45             : {
      46           0 :         memset(ctx->X, 0, sizeof ctx->X);
      47           0 :         ctx->M_n = 0;
      48           0 : }
      49             : 
      50             : void
      51           0 : AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const u_int8_t key[AES_CMAC_KEY_LENGTH])
      52             : {
      53           0 :         AES_Setkey(&ctx->aesctx, key, 16);
      54           0 : }
      55             : 
      56             : void
      57           0 : AES_CMAC_Update(AES_CMAC_CTX *ctx, const u_int8_t *data, u_int len)
      58             : {
      59             :         u_int mlen;
      60             : 
      61           0 :         if (ctx->M_n > 0) {
      62           0 :                 mlen = MIN(16 - ctx->M_n, len);
      63           0 :                 memcpy(ctx->M_last + ctx->M_n, data, mlen);
      64           0 :                 ctx->M_n += mlen;
      65           0 :                 if (ctx->M_n < 16 || len == mlen)
      66           0 :                         return;
      67           0 :                 XOR(ctx->M_last, ctx->X);
      68           0 :                 AES_Encrypt(&ctx->aesctx, ctx->X, ctx->X);
      69           0 :                 data += mlen;
      70           0 :                 len -= mlen;
      71           0 :         }
      72           0 :         while (len > 16) {   /* not last block */
      73           0 :                 XOR(data, ctx->X);
      74           0 :                 AES_Encrypt(&ctx->aesctx, ctx->X, ctx->X);
      75           0 :                 data += 16;
      76           0 :                 len -= 16;
      77             :         }
      78             :         /* potential last block, save it */
      79           0 :         memcpy(ctx->M_last, data, len);
      80           0 :         ctx->M_n = len;
      81           0 : }
      82             : 
      83             : void
      84           0 : AES_CMAC_Final(u_int8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx)
      85             : {
      86           0 :         u_int8_t K[16];
      87             : 
      88             :         /* generate subkey K1 */
      89           0 :         memset(K, 0, sizeof K);
      90           0 :         AES_Encrypt(&ctx->aesctx, K, K);
      91             : 
      92           0 :         if (K[0] & 0x80) {
      93           0 :                 LSHIFT(K, K);
      94           0 :                 K[15] ^= 0x87;
      95           0 :         } else
      96           0 :                 LSHIFT(K, K);
      97             : 
      98           0 :         if (ctx->M_n == 16) {
      99             :                 /* last block was a complete block */
     100           0 :                 XOR(K, ctx->M_last);
     101           0 :         } else {
     102             :                 /* generate subkey K2 */
     103           0 :                 if (K[0] & 0x80) {
     104           0 :                         LSHIFT(K, K);
     105           0 :                         K[15] ^= 0x87;
     106           0 :                 } else
     107           0 :                         LSHIFT(K, K);
     108             : 
     109             :                 /* padding(M_last) */
     110           0 :                 ctx->M_last[ctx->M_n] = 0x80;
     111           0 :                 while (++ctx->M_n < 16)
     112           0 :                         ctx->M_last[ctx->M_n] = 0;
     113             : 
     114           0 :                 XOR(K, ctx->M_last);
     115             :         }
     116           0 :         XOR(ctx->M_last, ctx->X);
     117           0 :         AES_Encrypt(&ctx->aesctx, ctx->X, digest);
     118             : 
     119           0 :         explicit_bzero(K, sizeof K);
     120           0 : }

Generated by: LCOV version 1.13