LCOV - code coverage report
Current view: top level - libavutil/tests - hmac.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 45 93.3 %
Date: 2017-10-18 21:45:51 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "libavutil/hmac.c"
      20             : 
      21             : #include <stdio.h>
      22             : #include <string.h>
      23             : 
      24          30 : static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
      25             :                  const uint8_t *data, int datalen)
      26             : {
      27             :     uint8_t buf[MAX_HASHLEN];
      28             :     int out, i;
      29             :     // Some of the test vectors are strings, where sizeof() includes the
      30             :     // trailing null byte - remove that.
      31          30 :     if (!key[keylen - 1])
      32           6 :         keylen--;
      33          30 :     if (!data[datalen - 1])
      34          24 :         datalen--;
      35          30 :     out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
      36        1070 :     for (i = 0; i < out; i++)
      37        1040 :         printf("%02x", buf[i]);
      38          30 :     printf("\n");
      39          30 : }
      40             : 
      41           1 : int main(void)
      42             : {
      43             :     uint8_t key1[20], key3[131], data3[50];
      44             :     AVHMAC *hmac;
      45             :     enum AVHMACType i;
      46             :     static const uint8_t key2[]  = "Jefe";
      47             :     static const uint8_t data1[] = "Hi There";
      48             :     static const uint8_t data2[] = "what do ya want for nothing?";
      49             :     static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
      50             :     static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
      51             :     static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
      52             :                             "than block-size data. The key needs to be hashed before being used"
      53             :                             " by the HMAC algorithm.";
      54           1 :     memset(key1, 0x0b, sizeof(key1));
      55           1 :     memset(key3, 0xaa, sizeof(key3));
      56           1 :     memset(data3, 0xdd, sizeof(data3));
      57             : 
      58             :     /* MD5, SHA-1 */
      59           3 :     for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) {
      60           2 :         hmac = av_hmac_alloc(i);
      61           2 :         if (!hmac)
      62           0 :             return 1;
      63             :         // RFC 2202 test vectors
      64           2 :         test(hmac, key1, hmac->hashlen, data1, sizeof(data1));
      65           2 :         test(hmac, key2, sizeof(key2),  data2, sizeof(data2));
      66           2 :         test(hmac, key3, hmac->hashlen, data3, sizeof(data3));
      67           2 :         test(hmac, key3, 80,            data4, sizeof(data4));
      68           2 :         test(hmac, key3, 80,            data5, sizeof(data5));
      69           2 :         av_hmac_free(hmac);
      70             :     }
      71             : 
      72             :     /* SHA-2 */
      73           3 :     for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA256; i++) {
      74           2 :         hmac = av_hmac_alloc(i);
      75           2 :         if (!hmac)
      76           0 :             return 1;
      77             :         // RFC 4231 test vectors
      78           2 :         test(hmac, key1, sizeof(key1), data1, sizeof(data1));
      79           2 :         test(hmac, key2, sizeof(key2), data2, sizeof(data2));
      80           2 :         test(hmac, key3, 20,           data3, sizeof(data3));
      81           2 :         test(hmac, key3, sizeof(key3), data4, sizeof(data4));
      82           2 :         test(hmac, key3, sizeof(key3), data6, sizeof(data6));
      83           2 :         av_hmac_free(hmac);
      84             :     }
      85             : 
      86           3 :     for (i = AV_HMAC_SHA384; i <= AV_HMAC_SHA512; i++) {
      87           2 :         hmac = av_hmac_alloc(i);
      88           2 :         if (!hmac)
      89           0 :             return 1;
      90             :         // RFC 4231 test vectors
      91           2 :         test(hmac, key1, sizeof(key1), data1, sizeof(data1));
      92           2 :         test(hmac, key2, sizeof(key2), data2, sizeof(data2));
      93           2 :         test(hmac, key3, 20, data3, sizeof(data3));
      94           2 :         test(hmac, key3, sizeof(key3), data4, sizeof(data4));
      95           2 :         test(hmac, key3, sizeof(key3), data6, sizeof(data6));
      96           2 :         av_hmac_free(hmac);
      97             :     }
      98           1 :     return 0;
      99             : }

Generated by: LCOV version 1.13