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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _KECCAK_H
30 #define _KECCAK_H
31 
32 //Dependencies
33 #include "core/crypto.h"
34 
35 //The binary logarithm of the lane size
36 #ifndef KECCAK_L
37  #define KECCAK_L 6
38 #endif
39 
40 //Check lane size
41 #if (KECCAK_L == 3)
42  //Base type that represents a lane
43  typedef uint8_t keccak_lane_t;
44  //Rotate left operation
45  #define KECCAK_ROL(a, n) ROL8(a, n)
46  //Host byte order to little-endian byte order
47  #define KECCAK_HTOLE(a) (a)
48  //Little-endian byte order to host byte order
49  #define KECCAK_LETOH(a) (a)
50 #elif (KECCAK_L == 4)
51  //Base type that represents a lane
52  #define keccak_lane_t uint16_t
53  //Rotate left operation
54  #define KECCAK_ROL(a, n) ROL16(a, n)
55  //Host byte order to little-endian byte order
56  #define KECCAK_HTOLE(a) htole16(a)
57  //Little-endian byte order to host byte order
58  #define KECCAK_LETOH(a) letoh16(a)
59 #elif (KECCAK_L == 5)
60  //Base type that represents a lane
61  #define keccak_lane_t uint32_t
62  //Rotate left operation
63  #define KECCAK_ROL(a, n) ROL32(a, n)
64  //Host byte order to little-endian byte order
65  #define KECCAK_HTOLE(a) htole32(a)
66  //Little-endian byte order to host byte order
67  #define KECCAK_LETOH(a) letoh32(a)
68 #elif (KECCAK_L == 6)
69  //Base type that represents a lane
70  #define keccak_lane_t uint64_t
71  //Rotate left operation
72  #define KECCAK_ROL(a, n) ROL64(a, n)
73  //Host byte order to little-endian byte order
74  #define KECCAK_HTOLE(a) htole64(a)
75  //Little-endian byte order to host byte order
76  #define KECCAK_LETOH(a) letoh64(a)
77 #else
78  #error KECCAK_L parameter is not valid
79 #endif
80 
81 //The lane size of a Keccak-p permutation in bits
82 #define KECCAK_W (1 << KECCAK_L)
83 //The width of a Keccak-p permutation
84 #define KECCAK_B (KECCAK_W * 25)
85 //The number of rounds for a Keccak-p permutation
86 #define KECCAK_NR (12 + 2 * KECCAK_L)
87 
88 //Keccak padding byte
89 #define KECCAK_PAD 0x01
90 //SHA-3 padding byte
91 #define KECCAK_SHA3_PAD 0x06
92 //XOF padding byte
93 #define KECCAK_XOF_PAD 0x1F
94 
95 //C++ guard
96 #ifdef __cplusplus
97  extern "C" {
98 #endif
99 
100 
101 /**
102  * @brief Keccak context
103  **/
104 
105 typedef struct
106 {
107  union
108  {
110  uint8_t digest[1];
111  };
112  union
113  {
115  uint8_t buffer[1];
116  };
118  size_t length;
119 } KeccakContext;
120 
121 
122 //Keccak related functions
123 error_t keccakInit(KeccakContext *context, uint_t capacity);
124 void keccakAbsorb(KeccakContext *context, const void *input, size_t length);
125 void keccakFinal(KeccakContext *context, uint8_t pad);
126 void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length);
127 void keccakPermutBlock(KeccakContext *context);
128 
129 //C++ guard
130 #ifdef __cplusplus
131  }
132 #endif
133 
134 #endif
General definitions for cryptographic algorithms.
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:411
uint8_t a
Definition: ndp.h:407
Keccak context.
Definition: keccak.h:105
uint_t blockSize
Definition: keccak.h:117
void keccakPermutBlock(KeccakContext *context)
Block permutation.
Definition: keccak.c:471
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
Definition: keccak.c:316
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:367
uint16_t block
Definition: tftp_common.h:111
#define keccak_lane_t
Definition: keccak.h:70
uint8_t length
Definition: dtls_misc.h:140
size_t length
Definition: keccak.h:118
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:283