shake128.c
Go to the documentation of this file.
1 /**
2  * @file shake128.c
3  * @brief SHAKE128 extendable-output function (XOF)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 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  * @section Description
28  *
29  * SHAKE128 is a function on binary data in which the output can be extended
30  * to any desired length. SHAKE128 supports 128 bits of security strength.
31  * Refer to FIPS 202 for more details
32  *
33  * @author Oryx Embedded SARL (www.oryx-embedded.com)
34  * @version 1.9.8
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
39 
40 //Dependencies
41 #include "core/crypto.h"
42 #include "xof/shake128.h"
43 
44 //Check crypto library configuration
45 #if (SHAKE128_SUPPORT == ENABLED)
46 
47 //SHAKE128 object identifier (2.16.840.1.101.3.4.2.11)
48 const uint8_t shake128Oid[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x0B};
49 
50 
51 /**
52  * @brief Digest a message using SHAKE128
53  * @param[in] input Pointer to the input data
54  * @param[in] inputLen Length of the input data
55  * @param[out] output Pointer to the output data
56  * @param[in] outputLen Expected length of the output data
57  * @return Error code
58  **/
59 
60 error_t shake128Compute(const void *input, size_t inputLen,
61  uint8_t *output, size_t outputLen)
62 {
63  Shake128Context *context;
64 
65  //Allocate a memory buffer to hold the SHAKE128 context
66  context = cryptoAllocMem(sizeof(Shake128Context));
67  //Failed to allocate memory?
68  if(context == NULL)
69  return ERROR_OUT_OF_MEMORY;
70 
71  //Initialize the SHAKE128 context
72  shake128Init(context);
73  //Absorb input data
74  shake128Absorb(context, input, inputLen);
75  //Finish absorbing phase
76  shake128Final(context);
77  //Extract data from the squeezing phase
78  shake128Squeeze(context, output, outputLen);
79 
80  //Free previously allocated memory
81  cryptoFreeMem(context);
82  //Successful processing
83  return NO_ERROR;
84 }
85 
86 
87 /**
88  * @brief Initialize SHAKE128 context
89  * @param[in] context Pointer to the SHAKE128 context to initialize
90  **/
91 
93 {
94  //SHAKE128 supports 128 bits of security strength
95  keccakInit(context, 2 * 128);
96 }
97 
98 
99 /**
100  * @brief Absorb data
101  * @param[in] context Pointer to the SHAKE128 context
102  * @param[in] input Pointer to the buffer being hashed
103  * @param[in] length Length of the buffer
104  **/
105 
106 void shake128Absorb(Shake128Context *context, const void *input, size_t length)
107 {
108  //Absorb the input data
109  keccakAbsorb(context, input, length);
110 }
111 
112 
113 /**
114  * @brief Finish absorbing phase
115  * @param[in] context Pointer to the SHAKE128 context
116  **/
117 
119 {
120  //Finish absorbing phase (padding byte is 0x1F for XOFs)
121  keccakFinal(context, KECCAK_XOF_PAD);
122 }
123 
124 
125 /**
126  * @brief Extract data from the squeezing phase
127  * @param[in] context Pointer to the SHAKE128 context
128  * @param[out] output Output string
129  * @param[in] length Desired output length, in bytes
130  **/
131 
132 void shake128Squeeze(Shake128Context *context, uint8_t *output, size_t length)
133 {
134  //Extract data from the squeezing phase
135  keccakSqueeze(context, output, length);
136 }
137 
138 #endif
uint8_t length
Definition: coap_common.h:190
void shake128Squeeze(Shake128Context *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: shake128.c:132
Keccak context.
Definition: keccak.h:107
const uint8_t shake128Oid[9]
Definition: shake128.c:48
void keccakFinal(KeccakContext *context, uint8_t pad)
Finish absorbing phase.
Definition: keccak.c:369
error_t shake128Compute(const void *input, size_t inputLen, uint8_t *output, size_t outputLen)
Digest a message using SHAKE128.
Definition: shake128.c:60
error_t
Error codes.
Definition: error.h:42
void shake128Absorb(Shake128Context *context, const void *input, size_t length)
Absorb data.
Definition: shake128.c:106
General definitions for cryptographic algorithms.
void keccakAbsorb(KeccakContext *context, const void *input, size_t length)
Absorb data.
Definition: keccak.c:318
void keccakSqueeze(KeccakContext *context, uint8_t *output, size_t length)
Extract data from the squeezing phase.
Definition: keccak.c:413
#define KECCAK_XOF_PAD
Definition: keccak.h:95
#define cryptoFreeMem(p)
Definition: crypto.h:630
#define cryptoAllocMem(size)
Definition: crypto.h:625
void shake128Final(Shake128Context *context)
Finish absorbing phase.
Definition: shake128.c:118
error_t keccakInit(KeccakContext *context, uint_t capacity)
Initialize Keccak context.
Definition: keccak.c:285
SHAKE128 extendable-output function (XOF)
void shake128Init(Shake128Context *context)
Initialize SHAKE128 context.
Definition: shake128.c:92
Success.
Definition: error.h:44