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-2019 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 1.9.6
29  **/
30 
31 #ifndef _CMAC_H
32 #define _CMAC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //RC2 support?
38 #if (RC2_SUPPORT == ENABLED)
39  #include "cipher/rc2.h"
40 #endif
41 
42 //RC6 support?
43 #if (RC6_SUPPORT == ENABLED)
44  #include "cipher/rc6.h"
45 #endif
46 
47 //IDEA support?
48 #if (IDEA_SUPPORT == ENABLED)
49  #include "cipher/idea.h"
50 #endif
51 
52 //DES support?
53 #if (DES_SUPPORT == ENABLED)
54  #include "cipher/des.h"
55 #endif
56 
57 //Triple DES support?
58 #if (DES3_SUPPORT == ENABLED)
59  #include "cipher/des3.h"
60 #endif
61 
62 //AES support?
63 #if (AES_SUPPORT == ENABLED)
64  #include "cipher/aes.h"
65 #endif
66 
67 //Blowfish support?
68 #if (BLOWFISH_SUPPORT == ENABLED)
69  #include "cipher/blowfish.h"
70 #endif
71 
72 //Camellia support?
73 #if (CAMELLIA_SUPPORT == ENABLED)
74  #include "cipher/camellia.h"
75 #endif
76 
77 //SEED support?
78 #if (SEED_SUPPORT == ENABLED)
79  #include "cipher/seed.h"
80 #endif
81 
82 //ARIA support?
83 #if (ARIA_SUPPORT == ENABLED)
84  #include "cipher/aria.h"
85 #endif
86 
87 //PRESENT support?
88 #if (PRESENT_SUPPORT == ENABLED)
89  #include "cipher/present.h"
90 #endif
91 
92 //C++ guard
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96 
97 
98 /**
99  * @brief CMAC algorithm context
100  **/
101 
102 typedef struct
103 {
105  uint8_t cipherContext[MAX_CIPHER_CONTEXT_SIZE];
108  uint8_t buffer[MAX_CIPHER_BLOCK_SIZE];
109  size_t bufferLength;
110  uint8_t mac[MAX_CIPHER_BLOCK_SIZE];
111 } CmacContext;
112 
113 
114 //CMAC related functions
115 error_t cmacCompute(const CipherAlgo *cipher, const void *key, size_t keyLen,
116  const void *data, size_t dataLen, uint8_t *mac, size_t macLen);
117 
118 error_t cmacInit(CmacContext *context, const CipherAlgo *cipher,
119  const void *key, size_t keyLen);
120 
121 void cmacReset(CmacContext *context);
122 void cmacUpdate(CmacContext *context, const void *data, size_t dataLen);
123 error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen);
124 
125 void cmacMul(uint8_t *x, const uint8_t *a, size_t n, uint8_t rb);
126 void cmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n);
127 
128 //C++ guard
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif
#define MAX_CIPHER_BLOCK_SIZE
Definition: crypto.h:891
size_t bufferLength
Definition: cmac.h:109
PRESENT encryption algorithm.
uint8_t a
Definition: ndp.h:410
RC6-32/20 block cipher.
uint8_t b[6]
Definition: dtls_misc.h:139
ARIA encryption algorithm.
const CipherAlgo * cipher
Definition: cmac.h:104
void cmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
XOR operation.
Definition: cmac.c:316
error_t
Error codes.
Definition: error.h:42
AES (Advanced Encryption Standard)
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:105
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:183
General definitions for cryptographic algorithms.
IDEA encryption algorithm.
Blowfish encryption algorithm.
SEED encryption algorithm.
void cmacReset(CmacContext *context)
Reset CMAC context.
Definition: cmac.c:164
DES (Data Encryption Standard)
CMAC algorithm context.
Definition: cmac.h:102
error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen)
Finish the CMAC calculation.
Definition: cmac.c:229
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1150
#define MAX_CIPHER_CONTEXT_SIZE
Definition: crypto.h:866
uint8_t data[]
Definition: dtls_misc.h:176
Triple DES (Triple Data Encryption Algorithm)
RC2 block cipher.
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:285
Camellia encryption algorithm.