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-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.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  //Host byte order to little-endian byte order
49  #define KECCAK_HTOLE(a) (a)
50  //Little-endian byte order to host byte order
51  #define KECCAK_LETOH(a) (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  //Host byte order to little-endian byte order
58  #define KECCAK_HTOLE(a) htole16(a)
59  //Little-endian byte order to host byte order
60  #define KECCAK_LETOH(a) letoh16(a)
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  //Host byte order to little-endian byte order
67  #define KECCAK_HTOLE(a) htole32(a)
68  //Little-endian byte order to host byte order
69  #define KECCAK_LETOH(a) letoh32(a)
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  //Host byte order to little-endian byte order
76  #define KECCAK_HTOLE(a) htole64(a)
77  //Little-endian byte order to host byte order
78  #define KECCAK_LETOH(a) letoh64(a)
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 (1 << KECCAK_L)
85 //The width of a Keccak-p permutation
86 #define KECCAK_B (KECCAK_W * 25)
87 //The number of rounds for a Keccak-p permutation
88 #define KECCAK_NR (12 + 2 * KECCAK_L)
89 
90 //Keccak padding byte
91 #define KECCAK_PAD 0x01
92 //SHA-3 padding byte
93 #define KECCAK_SHA3_PAD 0x06
94 //SHAKE padding byte
95 #define KECCAK_SHAKE_PAD 0x1F
96 //cSHAKE padding byte
97 #define KECCAK_CSHAKE_PAD 0x04
98 
99 //C++ guard
100 #ifdef __cplusplus
101 extern "C" {
102 #endif
103 
104 
105 /**
106  * @brief Keccak context
107  **/
108 
109 typedef struct
110 {
111  union
112  {
114  uint8_t digest[1];
115  };
116  union
117  {
119  uint8_t buffer[1];
120  };
122  size_t length;
123 } KeccakContext;
124 
125 
126 //Keccak related functions
127 error_t keccakInit(KeccakContext *context, uint_t capacity);
128 void keccakAbsorb(KeccakContext *context, const void *input, size_t length);
129 void keccakFinal(KeccakContext *context, uint8_t pad);
130 void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length);
131 void keccakPermutBlock(KeccakContext *context);
132 
133 //C++ guard
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 #endif
unsigned int uint_t
Definition: compiler_port.h:50
General definitions for cryptographic algorithms.
error_t
Error codes.
Definition: error.h:43
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:285
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:374
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:418
void keccakPermutBlock(KeccakContext *context)
Block permutation.
#define keccak_lane_t
Definition: keccak.h:72
uint8_t a
Definition: ndp.h:411
Keccak context.
Definition: keccak.h:110
uint_t blockSize
Definition: keccak.h:121
size_t length
Definition: keccak.h:122
uint8_t length
Definition: tcp.h:368
uint16_t block
Definition: tftp_common.h:115