crypto: hmac_keccak added

This commit is contained in:
Dusan Klinec 2019-03-03 13:54:07 +01:00
parent c88e992104
commit d74d26f2c9
No known key found for this signature in database
GPG key ID: 6337E118CCBCE103
5 changed files with 295 additions and 0 deletions

View file

@ -39,6 +39,7 @@ set(crypto_sources
hash-extra-jh.c
hash-extra-skein.c
hash.c
hmac-keccak.c
jh.c
keccak.c
oaes_lib.c
@ -64,6 +65,7 @@ set(crypto_private_headers
groestl_tables.h
hash-ops.h
hash.h
hmac-keccak.h
initializer.h
jh.h
keccak.h

81
src/crypto/hmac-keccak.c Normal file
View file

@ -0,0 +1,81 @@
// Copyright (c) 2014-2018, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "hmac-keccak.h"
#include "memwipe.h"
#define KECCAK_BLOCKLEN 136
#define HASH_SIZE 32
void hmac_keccak_init(hmac_keccak_state *S, const uint8_t *_key, size_t keylen) {
const uint8_t *key = _key;
uint8_t keyhash[HASH_SIZE];
uint8_t pad[KECCAK_BLOCKLEN];
uint64_t i;
if (keylen > KECCAK_BLOCKLEN) {
keccak(key, keylen, keyhash, HASH_SIZE);
key = keyhash;
keylen = HASH_SIZE;
}
keccak_init(&S->inner);
memset(pad, 0x36, KECCAK_BLOCKLEN);
for (i = 0; i < keylen; ++i) {
pad[i] ^= key[i];
}
keccak_update(&S->inner, pad, KECCAK_BLOCKLEN);
keccak_init(&S->outer);
memset(pad, 0x5c, KECCAK_BLOCKLEN);
for (i = 0; i < keylen; ++i) {
pad[i] ^= key[i];
}
keccak_update(&S->outer, pad, KECCAK_BLOCKLEN);
memwipe(keyhash, HASH_SIZE);
}
void hmac_keccak_update(hmac_keccak_state *S, const uint8_t *data, size_t datalen) {
keccak_update(&S->inner, data, datalen);
}
void hmac_keccak_finish(hmac_keccak_state *S, uint8_t *digest) {
uint8_t ihash[HASH_SIZE];
keccak_finish(&S->inner, ihash);
keccak_update(&S->outer, ihash, HASH_SIZE);
keccak_finish(&S->outer, digest);
memwipe(ihash, HASH_SIZE);
}
void hmac_keccak_hash(uint8_t *out, const uint8_t *key, size_t keylen, const uint8_t *in, size_t inlen) {
hmac_keccak_state S;
hmac_keccak_init(&S, key, keylen);
hmac_keccak_update(&S, in, inlen);
hmac_keccak_finish(&S, out);
}

59
src/crypto/hmac-keccak.h Normal file
View file

@ -0,0 +1,59 @@
// Copyright (c) 2014-2018, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef HMAC_KECCAK_H
#define HMAC_KECCAK_H
#include "keccak.h"
// HMAC RFC 2104 with Keccak-256 base hash function
//
// B = KECCAK_BLOCKLEN = 136 B
// L = HASH_SIZE = 32 B
//
// Note this is not HMAC-SHA3 as SHA3 and Keccak differs in
// the padding constant.
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
KECCAK_CTX inner;
KECCAK_CTX outer;
} hmac_keccak_state;
void hmac_keccak_init(hmac_keccak_state *S, const uint8_t *_key, size_t keylen);
void hmac_keccak_update(hmac_keccak_state *S, const uint8_t *data, size_t datalen);
void hmac_keccak_finish(hmac_keccak_state *S, uint8_t *digest);
void hmac_keccak_hash(uint8_t *out, const uint8_t *key, size_t keylen, const uint8_t *in, size_t inlen);
#ifdef __cplusplus
}
#endif
#endif //HMAC_KECCAK_H

View file

@ -52,6 +52,7 @@ set(unit_tests_sources
json_serialization.cpp
get_xtype_from_string.cpp
hashchain.cpp
hmac_keccak.cpp
http.cpp
keccak.cpp
logging.cpp

View file

@ -0,0 +1,152 @@
// Copyright (c) 2018, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "../io.h"
extern "C" {
#include "crypto/hmac-keccak.h"
}
#define KECCAK_BLOCKLEN 136
static const struct {
const char *key;
const char *inp;
const char *res;
} keccak_hmac_vectors[] = {
{"",
"",
"042186ec4e98680a0866091d6fb89b60871134b44327f8f467c14e9841d3e97b",
},
{"00",
"",
"042186ec4e98680a0866091d6fb89b60871134b44327f8f467c14e9841d3e97b",
},
{"00000000000000000000000000000000",
"",
"042186ec4e98680a0866091d6fb89b60871134b44327f8f467c14e9841d3e97b",
},
{"",
"00",
"402541d5d678ad30217023a12efbd2f367388dc0253ccddb8f915d187e03d414",
},
{"0000000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000",
"d164c38ca454176281b3c09dc83cf70bd00290835ff5490356993d368cde0577",
},
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"0000000000000000000000000000000000000000000000000000000000000000",
"d52c9100301a9546d8c97d7f32e8178fd5f4b0a7646ab761db9e0f503f8b0542",
},
{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
"d52c9100301a9546d8c97d7f32e8178fd5f4b0a7646ab761db9e0f503f8b0542",
"5b9d98c5d3249176230d70fdb215ee2f93e4783064c9564384ddc396e63c18cb",
},
{"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"98d033ba0c95bb3aaf5709733443cbedba20f6451b710ccba4118fc5523013c8",
},
{"65768798a9bacbdcedfe0f2031425364758697a8b9cadbecfd0e1f30415263748596a7b8c9daebfc0d1e2f405162738495a6b7c8d9eafb0c1d2e3f5061728394a5b6c7d8e9fa0b1c2d3e4f60718293a4b5c6d7e8f90a1b2c3d4e5f708192a3b4c5d6e7f8091a2b3c4d5e6f8091a2b3c4d5e6f708192a3b4c5d6e7f90a1b2c3d4e5f60718293a4b5c6d7e8fa0b1c2d3e4f5061728394a5b6c7d8e9fb0c1d2e3f405162738495a6b7c8d9eafc0d1e2f30415263748596a7b8c9daebfd0e1f2031425364758697a8b9cadbecfe0f102132435465768798a9bacbdcedff00112233445566778899aabbccddeef00112233445566778899aabbccddeeff102132435465768798a9bacbdcedfe0f2031425364758697a8b9cadbecfd0e1f30415263748596a7b8c9daebfc0d1e2f405162738495a6b7c8d9eafb0c1d2e3f5061728394",
"7b8895a2afbcc9d6e3f0fd0a1724313e4b5865727f8c99a6b3c0cddae7f4010e1b2835424f5c697683909daab7c4d1deebf805121f2c394653606d7a8794a1aebbc8d5e2effc091623303d4a5764717e8b98a5b2bfccd9e6f3000d1a2734414e5b6875828f9ca9b6c3d0ddeaf704111e2b3845525f6c798693a0adbac7d4e1eefb0815222f3c495663707d8a97a4b1becbd8e5f2ff0c192633404d5a6774818e9ba8b5c2cfdce9f603101d2a3744515e6b7885929facb9c6d3e0edfa0714212e3b4855626f7c8996a3b0bdcad7e4f1fe0b1825323f4c596673808d9aa7b4c1cedbe8f5020f1c293643505d6a7784919eabb8c5d2dfecf90613202d3a4754616e7b8895a2afbcc9d6e3f0fd0a1724313e4b5865727f8c99a6b3c0cddae7f4010e1b2835424f5c697683909daab7c4d1deebf805121f2c394653606d7a8794a1ae",
"117b60a7f474fd2245adff82a69429367de8f5263fa96c411986009a743b0e70",
},
{"00112233445566778899aabbccddeeff102132435465768798a9bacbdcedfe0f",
"000d1a2734414e5b6875828f9ca9b6c3d0ddeaf704111e2b3845525f6c798693a0adbac7d4e1eefb0815222f3c495663707d8a97a4b1becbd8e5f2ff0c192633404d5a6774818e9ba8b5c2cfdce9f603101d2a3744515e6b7885929facb9c6d3e0edfa0714212e3b4855626f7c8996a3b0bdcad7e4f1fe0b1825323f4c596673808d9aa7b4c1cedbe8f5020f1c293643505d6a7784919eabb8c5d2dfecf90613202d3a4754616e7b8895a2afbcc9d6e3f0fd0a1724313e4b5865727f8c99a6b3c0cddae7f4010e1b",
"cbe3ca627c6432c9a66f07c6411fccca894c9b083b55d0773152460142a676ed",
},
};
static void test_keccak_hmac(const size_t * chunks)
{
uint8_t inp_buff[1024];
uint8_t key_buff[1024];
uint8_t res_exp[32];
uint8_t res_comp[32];
const size_t len_chunks = chunks ? sizeof(chunks) / sizeof(*chunks) : 0;
for (size_t i = 0; i < (sizeof(keccak_hmac_vectors) / sizeof(*keccak_hmac_vectors)); i++)
{
const size_t inp_len = strlen(keccak_hmac_vectors[i].inp)/2;
const size_t key_len = strlen(keccak_hmac_vectors[i].key)/2;
ASSERT_TRUE(hexdecode(keccak_hmac_vectors[i].inp, inp_len, inp_buff));
ASSERT_TRUE(hexdecode(keccak_hmac_vectors[i].key, key_len, key_buff));
ASSERT_TRUE(hexdecode(keccak_hmac_vectors[i].res, 32, res_exp));
if (len_chunks == 0)
{
hmac_keccak_hash(res_comp, key_buff, key_len, inp_buff, inp_len);
}
else
{
hmac_keccak_state S;
hmac_keccak_init(&S, key_buff, key_len);
size_t inp_passed = 0;
size_t chunk_size = 0;
while(inp_passed < inp_len){
const size_t to_pass = std::min(inp_len - inp_passed, chunks[chunk_size]);
hmac_keccak_update(&S, inp_buff + inp_passed, to_pass);
inp_passed += to_pass;
chunk_size = (chunk_size + 1) % len_chunks;
}
hmac_keccak_finish(&S, res_comp);
}
ASSERT_EQ(memcmp(res_exp, res_comp, 32), 0);
}
}
TEST(keccak_hmac, )
{
test_keccak_hmac({});
}
TEST(keccak_hmac, 1)
{
static const size_t chunks[] = {1};
test_keccak_hmac(chunks);
}
TEST(keccak_hmac, 1_20)
{
static const size_t chunks[] = {1, 20};
test_keccak_hmac(chunks);
}
TEST(keccak_hmac, 136_1)
{
static const size_t chunks[] = {136, 1};
test_keccak_hmac(chunks);
}
TEST(keccak_hmac, 137_1)
{
static const size_t chunks[] = {137, 1};
test_keccak_hmac(chunks);
}