LCOV - code coverage report
Current view: top level - libavutil - aes_ctr.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 50 56 89.3 %
Date: 2018-05-20 11:54:08 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /*
       2             :  * AES-CTR cipher
       3             :  * Copyright (c) 2015 Eran Kornblau <erankor at gmail dot com>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "common.h"
      23             : #include "aes_ctr.h"
      24             : #include "aes.h"
      25             : #include "random_seed.h"
      26             : 
      27             : #define AES_BLOCK_SIZE (16)
      28             : 
      29             : typedef struct AVAESCTR {
      30             :     struct AVAES* aes;
      31             :     uint8_t counter[AES_BLOCK_SIZE];
      32             :     uint8_t encrypted_counter[AES_BLOCK_SIZE];
      33             :     int block_offset;
      34             : } AVAESCTR;
      35             : 
      36           5 : struct AVAESCTR *av_aes_ctr_alloc(void)
      37             : {
      38           5 :     return av_mallocz(sizeof(struct AVAESCTR));
      39             : }
      40             : 
      41           1 : void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
      42             : {
      43           1 :     memcpy(a->counter, iv, AES_CTR_IV_SIZE);
      44           1 :     memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
      45           1 :     a->block_offset = 0;
      46           1 : }
      47             : 
      48         147 : void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
      49             : {
      50         147 :     memcpy(a->counter, iv, sizeof(a->counter));
      51         147 :     a->block_offset = 0;
      52         147 : }
      53             : 
      54           1 : const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
      55             : {
      56           1 :     return a->counter;
      57             : }
      58             : 
      59           1 : void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
      60             : {
      61             :     uint32_t iv[2];
      62             : 
      63           1 :     iv[0] = av_get_random_seed();
      64           1 :     iv[1] = av_get_random_seed();
      65             : 
      66           1 :     av_aes_ctr_set_iv(a, (uint8_t*)iv);
      67           1 : }
      68             : 
      69           5 : int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
      70             : {
      71           5 :     a->aes = av_aes_alloc();
      72           5 :     if (!a->aes) {
      73           0 :         return AVERROR(ENOMEM);
      74             :     }
      75             : 
      76           5 :     av_aes_init(a->aes, key, 128, 0);
      77             : 
      78           5 :     memset(a->counter, 0, sizeof(a->counter));
      79           5 :     a->block_offset = 0;
      80             : 
      81           5 :     return 0;
      82             : }
      83             : 
      84         639 : void av_aes_ctr_free(struct AVAESCTR *a)
      85             : {
      86         639 :     if (a) {
      87           5 :         av_freep(&a->aes);
      88           5 :         av_free(a);
      89             :     }
      90         639 : }
      91             : 
      92        5183 : static void av_aes_ctr_increment_be64(uint8_t* counter)
      93             : {
      94             :     uint8_t* cur_pos;
      95             : 
      96        5183 :     for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
      97        5183 :         (*cur_pos)++;
      98        5183 :         if (*cur_pos != 0) {
      99        5183 :             break;
     100             :         }
     101             :     }
     102        5183 : }
     103             : 
     104           0 : void av_aes_ctr_increment_iv(struct AVAESCTR *a)
     105             : {
     106           0 :     av_aes_ctr_increment_be64(a->counter);
     107           0 :     memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
     108           0 :     a->block_offset = 0;
     109           0 : }
     110             : 
     111         148 : void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
     112             : {
     113         148 :     const uint8_t* src_end = src + count;
     114             :     const uint8_t* cur_end_pos;
     115             :     uint8_t* encrypted_counter_pos;
     116             : 
     117        5479 :     while (src < src_end) {
     118        5183 :         if (a->block_offset == 0) {
     119        5183 :             av_aes_crypt(a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
     120             : 
     121        5183 :             av_aes_ctr_increment_be64(a->counter + 8);
     122             :         }
     123             : 
     124        5183 :         encrypted_counter_pos = a->encrypted_counter + a->block_offset;
     125        5183 :         cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
     126        5183 :         cur_end_pos = FFMIN(cur_end_pos, src_end);
     127             : 
     128        5183 :         a->block_offset += cur_end_pos - src;
     129        5183 :         a->block_offset &= (AES_BLOCK_SIZE - 1);
     130             : 
     131       92393 :         while (src < cur_end_pos) {
     132       82027 :             *dst++ = *src++ ^ *encrypted_counter_pos++;
     133             :         }
     134             :     }
     135         148 : }

Generated by: LCOV version 1.13