gcm.h
Go to the documentation of this file.
1 /**
2  * @file gcm.h
3  * @brief Galois/Counter Mode (GCM)
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 _GCM_H
32 #define _GCM_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //Precalculated table width, in bits
38 #ifndef GCM_TABLE_W
39  #define GCM_TABLE_W 4
40 #elif (GCM_TABLE_W != 4 && GCM_TABLE_W != 8)
41  #error GCM_TABLE_W parameter is not valid
42 #endif
43 
44 //4-bit or 8-bit precalculated table?
45 #if (GCM_TABLE_W == 4)
46  #define GCM_TABLE_N 16
47  #define GCM_REVERSE_BITS(n) reverseInt4(n)
48 #else
49  #define GCM_TABLE_N 256
50  #define GCM_REVERSE_BITS(n) reverseInt8(n)
51 #endif
52 
53 //C++ guard
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 
59 /**
60  * @brief GCM context
61  **/
62 
63 typedef struct
64 {
65  const CipherAlgo *cipherAlgo; ///<Cipher algorithm
66  void *cipherContext; ///<Cipher algorithm context
67  uint32_t m[GCM_TABLE_N][4]; ///<Precalculated table
68 } GcmContext;
69 
70 
71 //GCM related functions
72 error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo,
73  void *cipherContext);
74 
75 error_t gcmEncrypt(GcmContext *context, const uint8_t *iv,
76  size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *p,
77  uint8_t *c, size_t length, uint8_t *t, size_t tLen);
78 
79 error_t gcmDecrypt(GcmContext *context, const uint8_t *iv,
80  size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *c,
81  uint8_t *p, size_t length, const uint8_t *t, size_t tLen);
82 
83 void gcmMul(GcmContext *context, uint8_t *x);
84 void gcmXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n);
85 void gcmIncCounter(uint8_t *ctr);
86 
87 //C++ guard
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif
void gcmXorBlock(uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
XOR operation.
Definition: gcm.c:597
uint8_t b
Definition: nbns_common.h:104
error_t gcmEncrypt(GcmContext *context, const uint8_t *iv, size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *p, uint8_t *c, size_t length, uint8_t *t, size_t tLen)
Authenticated encryption using GCM.
Definition: gcm.c:214
uint8_t a
Definition: ndp.h:411
uint8_t p
Definition: ndp.h:300
uint8_t x
Definition: lldp_ext_med.h:211
uint8_t t
Definition: lldp_ext_med.h:212
void gcmMul(GcmContext *context, uint8_t *x)
Multiplication operation in GF(2^128)
Definition: gcm.c:508
error_t
Error codes.
Definition: error.h:43
General definitions for cryptographic algorithms.
uint8_t iv[]
Definition: ike.h:1502
const CipherAlgo * cipherAlgo
Cipher algorithm.
Definition: gcm.h:65
uint8_t length
Definition: tcp.h:368
error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
Definition: gcm.c:99
GCM context.
Definition: gcm.h:64
uint8_t m
Definition: ndp.h:304
uint8_t n
void gcmIncCounter(uint8_t *ctr)
Increment counter block.
Definition: gcm.c:614
error_t gcmDecrypt(GcmContext *context, const uint8_t *iv, size_t ivLen, const uint8_t *a, size_t aLen, const uint8_t *c, uint8_t *p, size_t length, const uint8_t *t, size_t tLen)
Authenticated decryption using GCM.
Definition: gcm.c:361
Common interface for encryption algorithms.
Definition: crypto.h:1068
void * cipherContext
Cipher algorithm context.
Definition: gcm.h:66
uint8_t c
Definition: ndp.h:514
#define GCM_TABLE_N
Definition: gcm.h:46