LCOV - code coverage report
Current view: top level - lib/crypto - aes_cmac_128_test.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 33 45 73.3 %
Date: 2021-09-23 10:06:22 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :    AES-CMAC-128 tests
       3             :    Copyright (C) Stefan Metzmacher 2012
       4             : 
       5             :    This program is free software; you can redistribute it and/or modify
       6             :    it under the terms of the GNU General Public License as published by
       7             :    the Free Software Foundation; either version 3 of the License, or
       8             :    (at your option) any later version.
       9             : 
      10             :    This program is distributed in the hope that it will be useful,
      11             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :    GNU General Public License for more details.
      14             : 
      15             :    You should have received a copy of the GNU General Public License
      16             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      17             : */
      18             : #include "replace.h"
      19             : #include "../lib/util/samba_util.h"
      20             : #include "lib/crypto/aes.h"
      21             : #include "lib/crypto/aes_cmac_128.h"
      22             : 
      23             : struct torture_context;
      24             : bool torture_local_crypto_aes_cmac_128(struct torture_context *torture);
      25             : 
      26             : /*
      27             :  This uses the test values from rfc 4493
      28             : */
      29           1 : bool torture_local_crypto_aes_cmac_128(struct torture_context *torture)
      30             : {
      31           1 :         bool ret = true;
      32             :         uint32_t i;
      33             :         DATA_BLOB key;
      34             :         struct {
      35             :                 DATA_BLOB data;
      36             :                 DATA_BLOB cmac;
      37             :         } testarray[5];
      38             : 
      39           1 :         TALLOC_CTX *tctx = talloc_new(torture);
      40           1 :         if (!tctx) { return false; };
      41             : 
      42           1 :         key = strhex_to_data_blob(tctx, "2b7e151628aed2a6abf7158809cf4f3c");
      43             : 
      44           1 :         testarray[0].data = data_blob_null;
      45           1 :         testarray[0].cmac = strhex_to_data_blob(tctx,
      46             :                                 "bb1d6929e95937287fa37d129b756746");
      47             : 
      48           1 :         testarray[1].data = strhex_to_data_blob(tctx,
      49             :                                 "6bc1bee22e409f96e93d7e117393172a");
      50           1 :         testarray[1].cmac = strhex_to_data_blob(tctx,
      51             :                                 "070a16b46b4d4144f79bdd9dd04a287c");
      52             : 
      53           1 :         testarray[2].data = strhex_to_data_blob(tctx,
      54             :                                 "6bc1bee22e409f96e93d7e117393172a"
      55             :                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
      56             :                                 "30c81c46a35ce411");
      57           1 :         testarray[2].cmac = strhex_to_data_blob(tctx,
      58             :                                 "dfa66747de9ae63030ca32611497c827");
      59             : 
      60           1 :         testarray[3].data = strhex_to_data_blob(tctx,
      61             :                                 "6bc1bee22e409f96e93d7e117393172a"
      62             :                                 "ae2d8a571e03ac9c9eb76fac45af8e51"
      63             :                                 "30c81c46a35ce411e5fbc1191a0a52ef"
      64             :                                 "f69f2445df4f9b17ad2b417be66c3710");
      65           1 :         testarray[3].cmac = strhex_to_data_blob(tctx,
      66             :                                 "51f0bebf7e3b9d92fc49741779363cfe");
      67             : 
      68           1 :         ZERO_STRUCT(testarray[4]);
      69             : 
      70           5 :         for (i=0; testarray[i].cmac.length != 0; i++) {
      71             :                 struct aes_cmac_128_context ctx;
      72             :                 uint8_t cmac[AES_BLOCK_SIZE];
      73             :                 int e;
      74             : 
      75           4 :                 aes_cmac_128_init(&ctx, key.data);
      76           8 :                 aes_cmac_128_update(&ctx,
      77           4 :                                     testarray[i].data.data,
      78             :                                     testarray[i].data.length);
      79           4 :                 aes_cmac_128_final(&ctx, cmac);
      80             : 
      81           4 :                 e = memcmp(testarray[i].cmac.data, cmac, sizeof(cmac));
      82           4 :                 if (e != 0) {
      83           0 :                         printf("aes_cmac_128 test[%u]: failed\n", i);
      84           0 :                         dump_data(0, key.data, key.length);
      85           0 :                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
      86           0 :                         dump_data(0, testarray[i].cmac.data, testarray[i].cmac.length);
      87           0 :                         dump_data(0, cmac, sizeof(cmac));
      88           0 :                         ret = false;
      89             :                 }
      90             :         }
      91           4 :         for (i=0; testarray[i].cmac.length != 0; i++) {
      92             :                 struct aes_cmac_128_context ctx;
      93             :                 uint8_t cmac[AES_BLOCK_SIZE];
      94             :                 int e;
      95             :                 size_t j;
      96             : 
      97           4 :                 aes_cmac_128_init(&ctx, key.data);
      98         124 :                 for (j=0; j < testarray[i].data.length; j++) {
      99         120 :                         aes_cmac_128_update(&ctx, NULL, 0);
     100         120 :                         aes_cmac_128_update(&ctx,
     101         120 :                                             &testarray[i].data.data[j],
     102             :                                             1);
     103         120 :                         aes_cmac_128_update(&ctx, NULL, 0);
     104             :                 }
     105           4 :                 aes_cmac_128_final(&ctx, cmac);
     106             : 
     107           4 :                 e = memcmp(testarray[i].cmac.data, cmac, sizeof(cmac));
     108           4 :                 if (e != 0) {
     109           0 :                         printf("aes_cmac_128 chunked test[%u]: failed\n", i);
     110           0 :                         dump_data(0, key.data, key.length);
     111           0 :                         dump_data(0, testarray[i].data.data, testarray[i].data.length);
     112           0 :                         dump_data(0, testarray[i].cmac.data, testarray[i].cmac.length);
     113           0 :                         dump_data(0, cmac, sizeof(cmac));
     114           0 :                         ret = false;
     115             :                 }
     116             :         }
     117           1 :         talloc_free(tctx);
     118           1 :         return ret;
     119             : }

Generated by: LCOV version 1.13