gmac.h
Go to the documentation of this file.
1 /**
2  * @file gmac.h
3  * @brief GMAC (Galois 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 _GMAC_H
32 #define _GMAC_H
33 
34 //Dependencies
35 #include "core/crypto.h"
37 
38 //Precalculated table width, in bits
39 #ifndef GMAC_TABLE_W
40  #define GMAC_TABLE_W 4
41 #elif (GMAC_TABLE_W != 4 && GMAC_TABLE_W != 8)
42  #error GMAC_TABLE_W parameter is not valid
43 #endif
44 
45 //4-bit or 8-bit precalculated table?
46 #if (GMAC_TABLE_W == 4)
47  #define GMAC_TABLE_N 16
48  #define GMAC_REVERSE_BITS(n) reverseInt4(n)
49 #else
50  #define GMAC_TABLE_N 256
51  #define GMAC_REVERSE_BITS(n) reverseInt8(n)
52 #endif
53 
54 //Application specific context
55 #ifndef GMAC_PRIVATE_CONTEXT
56  #define GMAC_PRIVATE_CONTEXT
57 #endif
58 
59 //C++ guard
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 
65 /**
66  * @brief GMAC algorithm context
67  **/
68 
69 typedef struct
70 {
73  uint32_t m[GMAC_TABLE_N][4];
74  uint8_t s[16];
75  uint8_t buffer[16];
76  size_t bufferLength;
77  uint64_t totalLength;
78  uint8_t mac[16];
80 } GmacContext;
81 
82 
83 //GMAC related functions
84 error_t gmacCompute(const CipherAlgo *cipher, const void *key, size_t keyLen,
85  const uint8_t *iv, size_t ivLen, const void *data, size_t dataLen,
86  uint8_t *mac, size_t macLen);
87 
88 error_t gmacInit(GmacContext *context, const CipherAlgo *cipher,
89  const void *key, size_t keyLen);
90 
91 error_t gmacReset(GmacContext *context, const uint8_t *iv, size_t ivLen);
92 void gmacUpdate(GmacContext *context, const void *data, size_t dataLen);
93 error_t gmacFinal(GmacContext *context, uint8_t *mac, size_t macLen);
94 void gmacDeinit(GmacContext *context);
95 
96 void gmacMul(GmacContext *context, uint8_t *x);
97 void gmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n);
98 void gmacIncCounter(uint8_t *ctr);
99 
100 //C++ guard
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif
error_t gmacInit(GmacContext *context, const CipherAlgo *cipher, const void *key, size_t keyLen)
Initialize GMAC calculation.
Definition: gmac.c:160
uint8_t b
Definition: nbns_common.h:104
uint8_t a
Definition: ndp.h:411
void gmacXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
XOR operation.
Definition: gmac.c:560
error_t gmacCompute(const CipherAlgo *cipher, const void *key, size_t keyLen, const uint8_t *iv, size_t ivLen, const void *data, size_t dataLen, uint8_t *mac, size_t macLen)
Compute GMAC using the specified cipher algorithm.
Definition: gmac.c:103
#define GMAC_PRIVATE_CONTEXT
Definition: gmac.h:56
error_t gmacFinal(GmacContext *context, uint8_t *mac, size_t macLen)
Finish the GMAC calculation.
Definition: gmac.c:404
uint8_t x
Definition: lldp_ext_med.h:211
void gmacMul(GmacContext *context, uint8_t *x)
Multiplication operation in GF(2^128)
Definition: gmac.c:471
const CipherAlgo * cipher
Definition: gmac.h:71
Collection of AEAD algorithms.
uint8_t data[]
Definition: ethernet.h:222
Generic cipher algorithm context.
error_t gmacReset(GmacContext *context, const uint8_t *iv, size_t ivLen)
Reset GMAC context.
Definition: gmac.c:284
#define GMAC_TABLE_N
Definition: gmac.h:47
void gmacUpdate(GmacContext *context, const void *data, size_t dataLen)
Update the GMAC context with a portion of the message being hashed.
Definition: gmac.c:361
error_t
Error codes.
Definition: error.h:43
void gmacIncCounter(uint8_t *ctr)
Increment counter block.
Definition: gmac.c:577
General definitions for cryptographic algorithms.
uint8_t iv[]
Definition: ike.h:1502
uint32_t dataLen
Definition: sftp_common.h:229
uint8_t m
Definition: ndp.h:304
uint8_t n
Common interface for encryption algorithms.
Definition: crypto.h:1068
GMAC algorithm context.
Definition: gmac.h:70
uint8_t s
Definition: igmp_common.h:234
uint64_t totalLength
Definition: gmac.h:77
CipherContext cipherContext
Definition: gmac.h:72
size_t bufferLength
Definition: gmac.h:76
void gmacDeinit(GmacContext *context)
Release GMAC context.
Definition: gmac.c:451