Blame view

RIOT/sys/crypto/modes/ecb.c 1.48 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  /*
   * Copyright (C) 2015 Freie Universitรคt Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
  * @ingroup     sys_crypto_modes
  * @{
  *
  * @file
  * @brief       Crypto mode - electronic code book
  *
  * @author      Nico von Geyso <nico.geyso@fu-berlin.de>
  *
  * @}
  */
  
  #include <stddef.h>
  #include <stdint.h>
  
  #include "crypto/modes/ecb.h"
  
  int cipher_encrypt_ecb(cipher_t* cipher, uint8_t* input,
                         size_t length, uint8_t* output)
  {
      size_t offset;
      uint8_t block_size;
  
      block_size = cipher_get_block_size(cipher);
      if (length % block_size != 0) {
          return CIPHER_ERR_INVALID_LENGTH;
      }
  
      offset = 0;
      do {
          if (cipher_encrypt(cipher, input + offset, output + offset) != 1) {
              return CIPHER_ERR_ENC_FAILED;
          }
  
          offset += block_size;
      } while (offset < length);
  
      return offset;
  }
  
  int cipher_decrypt_ecb(cipher_t* cipher, uint8_t* input,
                         size_t length, uint8_t* output)
  {
      size_t offset = 0;
      uint8_t block_size;
  
      block_size = cipher_get_block_size(cipher);
      if (length % block_size != 0) {
          return CIPHER_ERR_INVALID_LENGTH;
      }
  
      do {
          if (cipher_decrypt(cipher, input + offset, output + offset) != 1) {
              return CIPHER_ERR_DEC_FAILED;
          }
  
          offset += block_size;
      } while (offset < length);
  
      return offset;
  }