keccak.h
Go to the documentation of this file.
1 /**
2  * @file keccak.h
3  * @brief Keccak sponge function
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 #ifndef _KECCAK_H
32 #define _KECCAK_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 
37 //The binary logarithm of the lane size
38 #ifndef KECCAK_L
39  #define KECCAK_L 6
40 #endif
41 
42 //Check lane size
43 #if (KECCAK_L == 3)
44  //Base type that represents a lane
45  typedef uint8_t keccak_lane_t;
46  //Rotate left operation
47  #define KECCAK_ROL(a, n) ROL8(a, (n) % 8)
48  //String to lane conversion
49  #define KECCAK_LOAD_LANE(p) p[0]
50  //Lane to string conversion
51  #define KECCAK_STORE_LANE(a, p) p[0] = a
52 #elif (KECCAK_L == 4)
53  //Base type that represents a lane
54  #define keccak_lane_t uint16_t
55  //Rotate left operation
56  #define KECCAK_ROL(a, n) ROL16(a, (n) % 16)
57  //String to lane conversion
58  #define KECCAK_LOAD_LANE(p) LOAD16LE(p)
59  //Lane to string conversion
60  #define KECCAK_STORE_LANE(a, p) STORE16LE(a, p)
61 #elif (KECCAK_L == 5)
62  //Base type that represents a lane
63  #define keccak_lane_t uint32_t
64  //Rotate left operation
65  #define KECCAK_ROL(a, n) ROL32(a, (n) % 32)
66  //String to lane conversion
67  #define KECCAK_LOAD_LANE(p) LOAD32LE(p)
68  //Lane to string conversion
69  #define KECCAK_STORE_LANE(a, p) STORE32LE(a, p)
70 #elif (KECCAK_L == 6)
71  //Base type that represents a lane
72  #define keccak_lane_t uint64_t
73  //Rotate left operation
74  #define KECCAK_ROL(a, n) ROL64(a, (n) % 64)
75  //String to lane conversion
76  #define KECCAK_LOAD_LANE(p) LOAD64LE(p)
77  //Lane to string conversion
78  #define KECCAK_STORE_LANE(a, p) STORE64LE(a, p)
79 #else
80  #error KECCAK_L parameter is not valid
81 #endif
82 
83 //The lane size of a Keccak-p permutation, in bits
84 #define KECCAK_W_BITS (1 << KECCAK_L)
85 //The lane size of a Keccak-p permutation, in bytes
86 #define KECCAK_W_BYTES ((1 << KECCAK_L) / 8)
87 
88 //The width of a Keccak-p permutation, in bits
89 #define KECCAK_B_BITS (KECCAK_W_BITS * 25)
90 //The width of a Keccak-p permutation, in bytes
91 #define KECCAK_B_BYTES (KECCAK_W_BYTES * 25)
92 
93 //The number of rounds for a Keccak-p permutation
94 #define KECCAK_NR (12 + 2 * KECCAK_L)
95 
96 //Keccak padding byte
97 #define KECCAK_PAD 0x01
98 //SHA-3 padding byte
99 #define KECCAK_SHA3_PAD 0x06
100 //SHAKE padding byte
101 #define KECCAK_SHAKE_PAD 0x1F
102 //cSHAKE padding byte
103 #define KECCAK_CSHAKE_PAD 0x04
104 
105 //C++ guard
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109 
110 
111 /**
112  * @brief Keccak context
113  **/
114 
115 typedef struct
116 {
118  uint8_t buffer[24 * KECCAK_W_BYTES];
120  size_t length;
121 } KeccakContext;
122 
123 
124 //Keccak related functions
125 error_t keccakInit(KeccakContext *context, uint_t capacity);
126 void keccakAbsorb(KeccakContext *context, const void *input, size_t length);
127 void keccakFinal(KeccakContext *context, uint8_t pad);
128 void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length);
129 void keccakPermutBlock(KeccakContext *context);
130 
131 //C++ guard
132 #ifdef __cplusplus
133 }
134 #endif
135 
136 #endif
uint8_t a
Definition: ndp.h:411
#define KECCAK_W_BYTES
Definition: keccak.h:86
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:373
Keccak context.
Definition: keccak.h:116
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:285
error_t
Error codes.
Definition: error.h:43
void keccakPermutBlock(KeccakContext *context)
Block permutation.
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:417
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
#define keccak_lane_t
Definition: keccak.h:72
uint_t blockSize
Definition: keccak.h:119
size_t length
Definition: keccak.h:120
unsigned int uint_t
Definition: compiler_port.h:57
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.