cmac.h
Go to the documentation of this file.
1 /**
2  * @file cmac.h
3  * @brief CMAC (Cipher-based Message Authentication Code)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _CMAC_H
32 #define _CMAC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
37 
38 //Application specific context
39 #ifndef CMAC_PRIVATE_CONTEXT
40  #define CMAC_PRIVATE_CONTEXT
41 #endif
42 
43 //C++ guard
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 
49 /**
50  * @brief CMAC algorithm context
51  **/
52 
53 typedef struct
54 {
57  uint8_t k1[MAX_CIPHER_BLOCK_SIZE];
58  uint8_t k2[MAX_CIPHER_BLOCK_SIZE];
59  uint8_t buffer[MAX_CIPHER_BLOCK_SIZE];
60  size_t bufferLength;
61  uint8_t mac[MAX_CIPHER_BLOCK_SIZE];
63 } CmacContext;
64 
65 
66 //CMAC related functions
67 error_t cmacCompute(const CipherAlgo *cipher, const void *key, size_t keyLen,
68  const void *data, size_t dataLen, uint8_t *mac, size_t macLen);
69 
70 error_t cmacInit(CmacContext *context, const CipherAlgo *cipher,
71  const void *key, size_t keyLen);
72 
73 void cmacReset(CmacContext *context);
74 void cmacUpdate(CmacContext *context, const void *data, size_t dataLen);
75 error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen);
76 void cmacDeinit(CmacContext *context);
77 
78 void cmacMul(uint8_t *x, const uint8_t *a, size_t n, uint8_t rb);
79 void cmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n);
80 
81 //C++ guard
82 #ifdef __cplusplus
83 }
84 #endif
85 
86 #endif
size_t bufferLength
Definition: cmac.h:60
uint8_t b
Definition: nbns_common.h:104
uint8_t a
Definition: ndp.h:411
uint8_t x
Definition: lldp_ext_med.h:211
Collection of AEAD algorithms.
uint8_t data[]
Definition: ethernet.h:222
Generic cipher algorithm context.
const CipherAlgo * cipher
Definition: cmac.h:55
void cmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
XOR operation.
Definition: cmac.c:349
#define MAX_CIPHER_BLOCK_SIZE
error_t
Error codes.
Definition: error.h:43
error_t cmacCompute(const CipherAlgo *cipher, const void *key, size_t keyLen, const void *data, size_t dataLen, uint8_t *mac, size_t macLen)
Compute CMAC using the specified cipher algorithm.
Definition: cmac.c:58
error_t cmacInit(CmacContext *context, const CipherAlgo *cipher, const void *key, size_t keyLen)
Initialize CMAC calculation.
Definition: cmac.c:107
void cmacUpdate(CmacContext *context, const void *data, size_t dataLen)
Update the CMAC context with a portion of the message being hashed.
Definition: cmac.c:191
General definitions for cryptographic algorithms.
void cmacReset(CmacContext *context)
Reset CMAC context.
Definition: cmac.c:172
uint32_t dataLen
Definition: sftp_common.h:229
CMAC algorithm context.
Definition: cmac.h:54
error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen)
Finish the CMAC calculation.
Definition: cmac.c:237
#define CMAC_PRIVATE_CONTEXT
Definition: cmac.h:40
uint8_t n
void cmacDeinit(CmacContext *context)
Release CMAC context.
Definition: cmac.c:296
Common interface for encryption algorithms.
Definition: crypto.h:1068
CipherContext cipherContext
Definition: cmac.h:56
void cmacMul(uint8_t *x, const uint8_t *a, size_t n, uint8_t rb)
Multiplication by x in GF(2^128)
Definition: cmac.c:318