Blame view

RIOT/sys/include/hashes/cmac.h 1.96 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  /*
   * Copyright (C) 2016 Fundación Inria Chile
   *
   * 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.
   */
  
  /**
   * @defgroup    sys_hashes_cmac AES_CMAC
   * @ingroup     sys_hashes
   * @brief       Implementation of the AES CMAC hashing function
  
   * @{
   *
   * @file
   * @brief       AES_CMAC interface definition
   *
   * @author      José Ignacio Alamos <jose.alamos@inria.cl>
   */
  
  #ifndef HASHES_CMAC_H
  #define HASHES_CMAC_H
  
  #include <stdio.h>
  #include "crypto/ciphers.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief   Length of AES_CMAC block in bytes
   */
  #define CMAC_BLOCK_SIZE 16
  
  /**
   * @brief   AES_CMAC calculation context
   */
  typedef struct {
      /** AES128 context */
      cipher_t aes_ctx;
      /** auxiliar array for CMAC calculations **/
      uint8_t X[CMAC_BLOCK_SIZE];
      /** current last block **/
      uint8_t M_last[CMAC_BLOCK_SIZE];
      /** last byte in last block */
      uint32_t M_n;
  } cmac_context_t;
  
  /**
   * @brief Initialize CMAC message digest context
   *
   * @param[in] ctx     Pointer to the CMAC context to initialize
   * @param[in] key     Key to be set
   * @param[in] key_size     Size of the key
   *
   * @return CIPHER_INIT_SUCCESS if the initialization was successful.
   *         CIPHER_ERR_INVALID_KEY_SIZE if the key size is not valid.
   */
  int cmac_init(cmac_context_t *ctx, const uint8_t *key, uint8_t key_size);
  
  /**
   * @brief Update the CMAC context with a portion of the message being hashed
   *
   * @param[in] ctx  Pointer to the CMAC context to update
   * @param[in] data Input data
   * @param[in] len  Length of @p data
   */
  void cmac_update(cmac_context_t *ctx, const void *data, size_t len);
  
  /**
   * @brief Finalizes the CMAC message digest
   *
   * @param[in] ctx     Pointer to the CMAC context
   * @param[out] digest Result location
   */
  void cmac_final(cmac_context_t *ctx, void *digest);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* HASHES_CMAC_H */
  /** @} */