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