sha3 1.0.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.clang-format +54 -0
- data/.document +4 -3
- data/.rdoc_options +10 -0
- data/.rspec +2 -2
- data/.rubocop.yml +5 -1
- data/CHANGELOG.md +23 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +1 -1
- data/README.md +153 -63
- data/Rakefile +9 -3
- data/certs/io+sha3@jsg.io.pem +26 -0
- data/doc/sha3.rb +81 -0
- data/ext/sha3/config.h +26 -0
- data/ext/sha3/digest.c +636 -164
- data/ext/sha3/digest.h +71 -35
- data/ext/sha3/extconf.rb +47 -13
- data/ext/sha3/lib/common/align.h +33 -0
- data/ext/sha3/{brg_endian.h → lib/common/brg_endian.h} +8 -7
- data/ext/sha3/{KeccakHash.c → lib/high/Keccak/FIPS202/KeccakHash.c} +26 -25
- data/ext/sha3/{KeccakHash.h → lib/high/Keccak/FIPS202/KeccakHash.h} +34 -19
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.c +81 -0
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.h +73 -0
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.inc +201 -0
- data/ext/sha3/lib/high/Keccak/KeccakSponge.c +95 -0
- data/ext/sha3/lib/high/Keccak/KeccakSponge.h +70 -0
- data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +312 -0
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.c +61 -0
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.h +67 -0
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.inc +128 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.c +93 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.h +599 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.inc +573 -0
- data/ext/sha3/lib/high/common/Phases.h +25 -0
- data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +758 -0
- data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-unrolling.macros +305 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +50 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference.h +23 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +617 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +50 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +413 -0
- data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.h +23 -0
- data/ext/sha3/lib/low/common/PlSnP-Fallback.inc +291 -0
- data/ext/sha3/lib/low/common/SnP-Relaned.h +145 -0
- data/lib/sha3.rb +25 -28
- data.tar.gz.sig +0 -0
- metadata +73 -149
- metadata.gz.sig +0 -0
- data/.yardopts +0 -1
- data/ChangeLog.rdoc +0 -27
- data/certs/johanns.pem +0 -24
- data/ext/sha3/KeccakF-1600-interface.h +0 -40
- data/ext/sha3/KeccakSponge.c +0 -192
- data/ext/sha3/KeccakSponge.h +0 -113
- data/ext/sha3/Optimized64/KeccakF-1600-64.macros +0 -2199
- data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +0 -3
- data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +0 -508
- data/ext/sha3/Optimized64/KeccakF-1600-unrolling.macros +0 -126
- data/ext/sha3/Optimized64/SnP-interface.h +0 -47
- data/ext/sha3/Reference/KeccakF-1600-reference.c +0 -311
- data/ext/sha3/Reference/KeccakF-reference.h +0 -26
- data/ext/sha3/Reference/SnP-FBWL-default.c +0 -96
- data/ext/sha3/Reference/SnP-FBWL-default.h +0 -26
- data/ext/sha3/Reference/SnP-interface.h +0 -42
- data/ext/sha3/Reference/displayIntermediateValues.c +0 -158
- data/ext/sha3/Reference/displayIntermediateValues.h +0 -34
- data/ext/sha3/SnP-Relaned.h +0 -249
- data/ext/sha3/sha3.c +0 -62
- data/ext/sha3/sha3.h +0 -26
- data/lib/sha3/doc.rb +0 -121
- data/lib/sha3/version.rb +0 -9
- data/sha3.gemspec +0 -53
- data/tests.sh +0 -28
data/ext/sha3/digest.h
CHANGED
@@ -1,45 +1,81 @@
|
|
1
|
-
|
1
|
+
// Copyright (c) 2012 - 2025 Johanns Gregorian <io+sha3@jsg.io>
|
2
2
|
|
3
3
|
#ifndef _DIGEST_H_
|
4
4
|
#define _DIGEST_H_
|
5
5
|
|
6
|
+
#include <ruby.h>
|
7
|
+
#include <ruby/encoding.h>
|
8
|
+
#include <string.h>
|
9
|
+
|
10
|
+
#include "KeccakHash.h"
|
11
|
+
|
6
12
|
#ifdef __cplusplus
|
7
|
-
extern "C"
|
8
|
-
{
|
13
|
+
extern "C" {
|
9
14
|
#endif
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
16
|
+
typedef enum { SHA3_224 = 0, SHA3_256, SHA3_384, SHA3_512, SHAKE_128, SHAKE_256 } algorithm_type;
|
17
|
+
|
18
|
+
typedef HashReturn (*keccak_init_func)(Keccak_HashInstance*);
|
19
|
+
|
20
|
+
typedef struct {
|
21
|
+
Keccak_HashInstance* state;
|
22
|
+
int hashbitlen;
|
23
|
+
algorithm_type algorithm;
|
24
|
+
} MDX;
|
25
|
+
|
26
|
+
VALUE sha3_module;
|
27
|
+
VALUE digest_class;
|
28
|
+
VALUE digest_error_class;
|
29
|
+
|
30
|
+
/* Static IDs for faster symbol lookup */
|
31
|
+
static ID sha3_224_id;
|
32
|
+
static ID sha3_256_id;
|
33
|
+
static ID sha3_384_id;
|
34
|
+
static ID sha3_512_id;
|
35
|
+
static ID shake_128_id;
|
36
|
+
static ID shake_256_id;
|
37
|
+
|
38
|
+
// TypedData functions
|
39
|
+
extern const rb_data_type_t mdx_type;
|
40
|
+
|
41
|
+
// Static inline functions replacing macros
|
42
|
+
static inline void get_mdx(VALUE obj, MDX** mdx) {
|
43
|
+
TypedData_Get_Struct((obj), MDX, &mdx_type, (*mdx));
|
44
|
+
if (!(*mdx)) {
|
45
|
+
rb_raise(rb_eRuntimeError, "Digest data not initialized!");
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
static inline void safe_get_mdx(VALUE obj, MDX** mdx) {
|
50
|
+
if (!rb_obj_is_kind_of(obj, digest_class)) {
|
51
|
+
rb_raise(rb_eTypeError, "wrong argument (%s)! (expected %s)", rb_obj_classname(obj),
|
52
|
+
rb_class2name(digest_class));
|
53
|
+
}
|
54
|
+
get_mdx(obj, mdx);
|
55
|
+
}
|
56
|
+
|
57
|
+
/* Allocation and initialization */
|
58
|
+
static VALUE rb_digest_alloc(VALUE);
|
59
|
+
static VALUE rb_digest_init(int, VALUE*, VALUE);
|
60
|
+
|
61
|
+
/* Core digest operations */
|
62
|
+
static VALUE rb_digest_copy(VALUE, VALUE);
|
63
|
+
static VALUE rb_digest_finish(int, VALUE*, VALUE);
|
64
|
+
static VALUE rb_digest_reset(VALUE);
|
65
|
+
static VALUE rb_digest_update(VALUE, VALUE);
|
66
|
+
|
67
|
+
/* Digest properties */
|
68
|
+
static VALUE rb_digest_block_length(VALUE);
|
69
|
+
static VALUE rb_digest_length(VALUE);
|
70
|
+
static VALUE rb_digest_name(VALUE);
|
71
|
+
|
72
|
+
/* Output methods */
|
73
|
+
static VALUE rb_digest_digest(int, VALUE*, VALUE);
|
74
|
+
static VALUE rb_digest_hexdigest(int, VALUE*, VALUE);
|
75
|
+
static VALUE rb_digest_hex_squeeze(VALUE, VALUE);
|
76
|
+
static VALUE rb_digest_squeeze(VALUE, VALUE);
|
77
|
+
static VALUE rb_digest_self_digest(VALUE, VALUE, VALUE);
|
78
|
+
static VALUE rb_digest_self_hexdigest(VALUE, VALUE, VALUE);
|
43
79
|
|
44
80
|
#ifdef __cplusplus
|
45
81
|
}
|
data/ext/sha3/extconf.rb
CHANGED
@@ -1,20 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'mkmf'
|
2
4
|
require 'rbconfig'
|
3
5
|
|
4
|
-
|
6
|
+
b64 = 8.size == 8
|
7
|
+
extension_name = 'sha3_digest'
|
8
|
+
ref_dir = b64 ? 'ref-64bits' : 'ref-32bits'
|
9
|
+
|
10
|
+
dir_config(extension_name)
|
11
|
+
|
12
|
+
# Set compiler flags
|
13
|
+
$CFLAGS << ' -fomit-frame-pointer -O3 -g0 -fms-extensions'
|
14
|
+
|
15
|
+
# Add architecture-specific optimizations if enabled
|
16
|
+
$CFLAGS << ' -march=native' if enable_config('march-tune-native', false)
|
17
|
+
|
18
|
+
# Add security hardening flags
|
19
|
+
$CFLAGS << ' -D_FORTIFY_SOURCE=2 -fstack-protector-strong'
|
20
|
+
|
21
|
+
# Add warning flags to catch potential issues
|
22
|
+
$CFLAGS << ' -Wall -Wextra -Wformat -Wformat-security'
|
23
|
+
|
24
|
+
# Add vectorization flags for better performance on supported platforms
|
25
|
+
$CFLAGS << ' -ftree-vectorize' if RUBY_PLATFORM =~ /x86_64|amd64|arm64/
|
26
|
+
|
27
|
+
# Find all relevant subdirectories and filter appropriately
|
28
|
+
vpath_dirs = Dir.glob("#{$srcdir}/lib/**/*")
|
29
|
+
.select { |path| File.directory?(path) }
|
30
|
+
.select { |dir| !dir.include?('KeccakP-1600/ref-') || dir.include?(ref_dir) }
|
31
|
+
|
32
|
+
# Process directory paths for both VPATH and INCFLAGS
|
33
|
+
vpath_dirs_processed = vpath_dirs.map { |dir| dir.sub($srcdir, '') }
|
34
|
+
|
35
|
+
# Add source directories to VPATH
|
36
|
+
$VPATH << vpath_dirs_processed
|
37
|
+
.map { |dir| "$(srcdir)#{dir}" }
|
38
|
+
.join(File::PATH_SEPARATOR)
|
5
39
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
Logging.message "=== Using reference ===\n"
|
11
|
-
FileUtils.cp Dir["#{$srcdir}/Reference/*"].collect { |f| File.expand_path(f) }, "#{$srcdir}/"
|
12
|
-
end
|
40
|
+
# Add include flags
|
41
|
+
$INCFLAGS << vpath_dirs_processed
|
42
|
+
.map { |dir| " -I$(srcdir)#{dir}" }
|
43
|
+
.join('')
|
13
44
|
|
14
|
-
|
15
|
-
|
45
|
+
# Base source files
|
46
|
+
$srcs = ['digest.c']
|
16
47
|
|
17
|
-
|
18
|
-
$
|
48
|
+
# Find and add all .c files from the filtered directories
|
49
|
+
$srcs += vpath_dirs
|
50
|
+
.flat_map { |dir| Dir.glob("#{dir}/*.c") }
|
51
|
+
.map { |file| File.basename(file) }
|
52
|
+
.uniq
|
19
53
|
|
20
|
-
create_makefile
|
54
|
+
create_makefile(extension_name)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
/*
|
2
|
+
The eXtended Keccak Code Package (XKCP)
|
3
|
+
https://github.com/XKCP/XKCP
|
4
|
+
|
5
|
+
Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".
|
6
|
+
|
7
|
+
For more information, feedback or questions, please refer to the Keccak Team website:
|
8
|
+
https://keccak.team/
|
9
|
+
|
10
|
+
To the extent possible under law, the implementer has waived all copyright
|
11
|
+
and related or neighboring rights to the source code in this file.
|
12
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
13
|
+
*/
|
14
|
+
|
15
|
+
#ifndef _align_h_
|
16
|
+
#define _align_h_
|
17
|
+
|
18
|
+
/* on Mac OS-X and possibly others, ALIGN(x) is defined in param.h, and -Werror chokes on the redef. */
|
19
|
+
#ifdef ALIGN
|
20
|
+
#undef ALIGN
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#if defined(__GNUC__)
|
24
|
+
#define ALIGN(x) __attribute__ ((aligned(x)))
|
25
|
+
#elif defined(_MSC_VER)
|
26
|
+
#define ALIGN(x) __declspec(align(x))
|
27
|
+
#elif defined(__ARMCC_VERSION)
|
28
|
+
#define ALIGN(x) __align(x)
|
29
|
+
#else
|
30
|
+
#define ALIGN(x)
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#endif
|
@@ -114,13 +114,14 @@
|
|
114
114
|
defined( __VMS ) || defined( _M_X64 )
|
115
115
|
# define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
|
116
116
|
|
117
|
-
#elif defined( AMIGA )
|
118
|
-
defined( _CRAY )
|
119
|
-
defined( ibm370 )
|
120
|
-
defined( __MRC__ )
|
121
|
-
defined( sparc )
|
122
|
-
defined( __VOS__ )
|
123
|
-
defined( THINK_C )
|
117
|
+
#elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
|
118
|
+
defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
|
119
|
+
defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
|
120
|
+
defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
|
121
|
+
defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
|
122
|
+
defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \
|
123
|
+
defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX ) || \
|
124
|
+
defined( __s390__ ) || defined( __s390x__ ) || defined( __zarch__ )
|
124
125
|
# define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
|
125
126
|
|
126
127
|
#elif defined(__arm__)
|
@@ -1,12 +1,13 @@
|
|
1
1
|
/*
|
2
|
-
|
3
|
-
|
4
|
-
denoted as "the implementer".
|
2
|
+
The eXtended Keccak Code Package (XKCP)
|
3
|
+
https://github.com/XKCP/XKCP
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
|
6
|
+
|
7
|
+
Implementation by the designers, hereby denoted as "the implementer".
|
8
|
+
|
9
|
+
For more information, feedback or questions, please refer to the Keccak Team website:
|
10
|
+
https://keccak.team/
|
10
11
|
|
11
12
|
To the extent possible under law, the implementer has waived all copyright
|
12
13
|
and related or neighboring rights to the source code in this file.
|
@@ -23,35 +24,35 @@ HashReturn Keccak_HashInitialize(Keccak_HashInstance *instance, unsigned int rat
|
|
23
24
|
HashReturn result;
|
24
25
|
|
25
26
|
if (delimitedSuffix == 0)
|
26
|
-
return
|
27
|
-
result = (HashReturn)
|
28
|
-
if (result !=
|
27
|
+
return KECCAK_FAIL;
|
28
|
+
result = (HashReturn)KeccakWidth1600_SpongeInitialize(&instance->sponge, rate, capacity);
|
29
|
+
if (result != KECCAK_SUCCESS)
|
29
30
|
return result;
|
30
31
|
instance->fixedOutputLength = hashbitlen;
|
31
32
|
instance->delimitedSuffix = delimitedSuffix;
|
32
|
-
return
|
33
|
+
return KECCAK_SUCCESS;
|
33
34
|
}
|
34
35
|
|
35
36
|
/* ---------------------------------------------------------------- */
|
36
37
|
|
37
|
-
HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *data,
|
38
|
+
HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *data, BitLength databitlen)
|
38
39
|
{
|
39
40
|
if ((databitlen % 8) == 0)
|
40
|
-
return (HashReturn)
|
41
|
+
return (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8);
|
41
42
|
else {
|
42
|
-
HashReturn ret = (HashReturn)
|
43
|
-
if (ret ==
|
44
|
-
|
43
|
+
HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, data, databitlen/8);
|
44
|
+
if (ret == KECCAK_SUCCESS) {
|
45
|
+
/* The last partial byte is assumed to be aligned on the least significant bits */
|
45
46
|
unsigned char lastByte = data[databitlen/8];
|
46
|
-
|
47
|
-
unsigned short delimitedLastBytes = (unsigned short)lastByte | ((unsigned short)instance->delimitedSuffix << (databitlen % 8));
|
47
|
+
/* Concatenate the last few bits provided here with those of the suffix */
|
48
|
+
unsigned short delimitedLastBytes = (unsigned short)((unsigned short)(lastByte & ((1 << (databitlen % 8)) - 1)) | ((unsigned short)instance->delimitedSuffix << (databitlen % 8)));
|
48
49
|
if ((delimitedLastBytes & 0xFF00) == 0x0000) {
|
49
50
|
instance->delimitedSuffix = delimitedLastBytes & 0xFF;
|
50
51
|
}
|
51
52
|
else {
|
52
53
|
unsigned char oneByte[1];
|
53
54
|
oneByte[0] = delimitedLastBytes & 0xFF;
|
54
|
-
ret = (HashReturn)
|
55
|
+
ret = (HashReturn)KeccakWidth1600_SpongeAbsorb(&instance->sponge, oneByte, 1);
|
55
56
|
instance->delimitedSuffix = (delimitedLastBytes >> 8) & 0xFF;
|
56
57
|
}
|
57
58
|
}
|
@@ -63,18 +64,18 @@ HashReturn Keccak_HashUpdate(Keccak_HashInstance *instance, const BitSequence *d
|
|
63
64
|
|
64
65
|
HashReturn Keccak_HashFinal(Keccak_HashInstance *instance, BitSequence *hashval)
|
65
66
|
{
|
66
|
-
HashReturn ret = (HashReturn)
|
67
|
-
if (ret ==
|
68
|
-
return (HashReturn)
|
67
|
+
HashReturn ret = (HashReturn)KeccakWidth1600_SpongeAbsorbLastFewBits(&instance->sponge, instance->delimitedSuffix);
|
68
|
+
if (ret == KECCAK_SUCCESS)
|
69
|
+
return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, hashval, instance->fixedOutputLength/8);
|
69
70
|
else
|
70
71
|
return ret;
|
71
72
|
}
|
72
73
|
|
73
74
|
/* ---------------------------------------------------------------- */
|
74
75
|
|
75
|
-
HashReturn Keccak_HashSqueeze(Keccak_HashInstance *instance, BitSequence *data,
|
76
|
+
HashReturn Keccak_HashSqueeze(Keccak_HashInstance *instance, BitSequence *data, BitLength databitlen)
|
76
77
|
{
|
77
78
|
if ((databitlen % 8) != 0)
|
78
|
-
return
|
79
|
-
return (HashReturn)
|
79
|
+
return KECCAK_FAIL;
|
80
|
+
return (HashReturn)KeccakWidth1600_SpongeSqueeze(&instance->sponge, data, databitlen/8);
|
80
81
|
}
|
@@ -1,12 +1,13 @@
|
|
1
1
|
/*
|
2
|
-
|
3
|
-
|
4
|
-
denoted as "the implementer".
|
2
|
+
The eXtended Keccak Code Package (XKCP)
|
3
|
+
https://github.com/XKCP/XKCP
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
|
6
|
+
|
7
|
+
Implementation by the designers, hereby denoted as "the implementer".
|
8
|
+
|
9
|
+
For more information, feedback or questions, please refer to the Keccak Team website:
|
10
|
+
https://keccak.team/
|
10
11
|
|
11
12
|
To the extent possible under law, the implementer has waived all copyright
|
12
13
|
and related or neighboring rights to the source code in this file.
|
@@ -16,15 +17,24 @@ http://creativecommons.org/publicdomain/zero/1.0/
|
|
16
17
|
#ifndef _KeccakHashInterface_h_
|
17
18
|
#define _KeccakHashInterface_h_
|
18
19
|
|
19
|
-
#include "
|
20
|
+
#include "config.h"
|
21
|
+
#ifdef XKCP_has_KeccakP1600
|
22
|
+
|
23
|
+
#include <stdint.h>
|
20
24
|
#include <string.h>
|
25
|
+
#include "KeccakSponge.h"
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
typedef
|
27
|
+
#ifndef _Keccak_BitTypes_
|
28
|
+
#define _Keccak_BitTypes_
|
29
|
+
typedef uint8_t BitSequence;
|
30
|
+
|
31
|
+
typedef size_t BitLength;
|
32
|
+
#endif
|
33
|
+
|
34
|
+
typedef enum { KECCAK_SUCCESS = 0, KECCAK_FAIL = 1, KECCAK_BAD_HASHLEN = 2 } HashReturn;
|
25
35
|
|
26
36
|
typedef struct {
|
27
|
-
|
37
|
+
KeccakWidth1600_SpongeInstance sponge;
|
28
38
|
unsigned int fixedOutputLength;
|
29
39
|
unsigned char delimitedSuffix;
|
30
40
|
} Keccak_HashInstance;
|
@@ -42,7 +52,7 @@ typedef struct {
|
|
42
52
|
* formatted like the @a delimitedData parameter of
|
43
53
|
* the Keccak_SpongeAbsorbLastFewBits() function.
|
44
54
|
* @pre One must have r+c=1600 and the rate a multiple of 8 bits in this implementation.
|
45
|
-
* @return
|
55
|
+
* @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
|
46
56
|
*/
|
47
57
|
HashReturn Keccak_HashInitialize(Keccak_HashInstance *hashInstance, unsigned int rate, unsigned int capacity, unsigned int hashbitlen, unsigned char delimitedSuffix);
|
48
58
|
|
@@ -76,11 +86,13 @@ HashReturn Keccak_HashInitialize(Keccak_HashInstance *hashInstance, unsigned int
|
|
76
86
|
* @param data Pointer to the input data.
|
77
87
|
* When @a databitLen is not a multiple of 8, the last bits of data must be
|
78
88
|
* in the least significant bits of the last byte (little-endian convention).
|
89
|
+
* In this case, the (8 - @a databitLen mod 8) most significant bits
|
90
|
+
* of the last byte are ignored.
|
79
91
|
* @param databitLen The number of input bits provided in the input data.
|
80
92
|
* @pre In the previous call to Keccak_HashUpdate(), databitlen was a multiple of 8.
|
81
|
-
* @return
|
93
|
+
* @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
|
82
94
|
*/
|
83
|
-
HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequence *data,
|
95
|
+
HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequence *data, BitLength databitlen);
|
84
96
|
|
85
97
|
/**
|
86
98
|
* Function to call after all input blocks have been input and to get
|
@@ -90,9 +102,8 @@ HashReturn Keccak_HashUpdate(Keccak_HashInstance *hashInstance, const BitSequenc
|
|
90
102
|
* output bits is equal to @a hashbitlen.
|
91
103
|
* If @a hashbitlen was 0 in the call to Keccak_HashInitialize(), the output bits
|
92
104
|
* must be extracted using the Keccak_HashSqueeze() function.
|
93
|
-
* @param state Pointer to the state of the sponge function initialized by Init().
|
94
105
|
* @param hashval Pointer to the buffer where to store the output data.
|
95
|
-
* @return
|
106
|
+
* @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
|
96
107
|
*/
|
97
108
|
HashReturn Keccak_HashFinal(Keccak_HashInstance *hashInstance, BitSequence *hashval);
|
98
109
|
|
@@ -103,8 +114,12 @@ HashReturn Keccak_HashFinal(Keccak_HashInstance *hashInstance, BitSequence *hash
|
|
103
114
|
* @param databitlen The number of output bits desired (must be a multiple of 8).
|
104
115
|
* @pre Keccak_HashFinal() must have been already called.
|
105
116
|
* @pre @a databitlen is a multiple of 8.
|
106
|
-
* @return
|
117
|
+
* @return KECCAK_SUCCESS if successful, KECCAK_FAIL otherwise.
|
107
118
|
*/
|
108
|
-
HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data,
|
119
|
+
HashReturn Keccak_HashSqueeze(Keccak_HashInstance *hashInstance, BitSequence *data, BitLength databitlen);
|
120
|
+
|
121
|
+
#else
|
122
|
+
#error This requires an implementation of Keccak-p[1600]
|
123
|
+
#endif
|
109
124
|
|
110
125
|
#endif
|
@@ -0,0 +1,81 @@
|
|
1
|
+
/*
|
2
|
+
The eXtended Keccak Code Package (XKCP)
|
3
|
+
https://github.com/XKCP/XKCP
|
4
|
+
|
5
|
+
Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
|
6
|
+
|
7
|
+
Implementation by the designers, hereby denoted as "the implementer".
|
8
|
+
|
9
|
+
For more information, feedback or questions, please refer to the Keccak Team website:
|
10
|
+
https://keccak.team/
|
11
|
+
|
12
|
+
To the extent possible under law, the implementer has waived all copyright
|
13
|
+
and related or neighboring rights to the source code in this file.
|
14
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include "KeccakDuplex.h"
|
18
|
+
|
19
|
+
#ifdef KeccakReference
|
20
|
+
#include "displayIntermediateValues.h"
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#ifdef XKCP_has_KeccakP200
|
24
|
+
#include "KeccakP-200-SnP.h"
|
25
|
+
|
26
|
+
#define prefix KeccakWidth200
|
27
|
+
#define SnP KeccakP200
|
28
|
+
#define SnP_width 200
|
29
|
+
#define SnP_Permute KeccakP200_Permute_18rounds
|
30
|
+
#include "KeccakDuplex.inc"
|
31
|
+
#undef prefix
|
32
|
+
#undef SnP
|
33
|
+
#undef SnP_width
|
34
|
+
#undef SnP_Permute
|
35
|
+
#undef SnP_FastLoop_Absorb
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#ifdef XKCP_has_KeccakP400
|
39
|
+
#include "KeccakP-400-SnP.h"
|
40
|
+
|
41
|
+
#define prefix KeccakWidth400
|
42
|
+
#define SnP KeccakP400
|
43
|
+
#define SnP_width 400
|
44
|
+
#define SnP_Permute KeccakP400_Permute_20rounds
|
45
|
+
#include "KeccakDuplex.inc"
|
46
|
+
#undef prefix
|
47
|
+
#undef SnP
|
48
|
+
#undef SnP_width
|
49
|
+
#undef SnP_Permute
|
50
|
+
#undef SnP_FastLoop_Absorb
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#ifdef XKCP_has_KeccakP800
|
54
|
+
#include "KeccakP-800-SnP.h"
|
55
|
+
|
56
|
+
#define prefix KeccakWidth800
|
57
|
+
#define SnP KeccakP800
|
58
|
+
#define SnP_width 800
|
59
|
+
#define SnP_Permute KeccakP800_Permute_22rounds
|
60
|
+
#include "KeccakDuplex.inc"
|
61
|
+
#undef prefix
|
62
|
+
#undef SnP
|
63
|
+
#undef SnP_width
|
64
|
+
#undef SnP_Permute
|
65
|
+
#undef SnP_FastLoop_Absorb
|
66
|
+
#endif
|
67
|
+
|
68
|
+
#ifdef XKCP_has_KeccakP1600
|
69
|
+
#include "KeccakP-1600-SnP.h"
|
70
|
+
|
71
|
+
#define prefix KeccakWidth1600
|
72
|
+
#define SnP KeccakP1600
|
73
|
+
#define SnP_width 1600
|
74
|
+
#define SnP_Permute KeccakP1600_Permute_24rounds
|
75
|
+
#include "KeccakDuplex.inc"
|
76
|
+
#undef prefix
|
77
|
+
#undef SnP
|
78
|
+
#undef SnP_width
|
79
|
+
#undef SnP_Permute
|
80
|
+
#undef SnP_FastLoop_Absorb
|
81
|
+
#endif
|
@@ -0,0 +1,73 @@
|
|
1
|
+
/*
|
2
|
+
The eXtended Keccak Code Package (XKCP)
|
3
|
+
https://github.com/XKCP/XKCP
|
4
|
+
|
5
|
+
Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
|
6
|
+
|
7
|
+
Implementation by the designers, hereby denoted as "the implementer".
|
8
|
+
|
9
|
+
For more information, feedback or questions, please refer to the Keccak Team website:
|
10
|
+
https://keccak.team/
|
11
|
+
|
12
|
+
To the extent possible under law, the implementer has waived all copyright
|
13
|
+
and related or neighboring rights to the source code in this file.
|
14
|
+
http://creativecommons.org/publicdomain/zero/1.0/
|
15
|
+
*/
|
16
|
+
|
17
|
+
#ifndef _KeccakDuplex_h_
|
18
|
+
#define _KeccakDuplex_h_
|
19
|
+
|
20
|
+
/* For the documentation, please follow the link: */
|
21
|
+
/* #include "KeccakDuplex-documentation.h" */
|
22
|
+
|
23
|
+
#include <string.h>
|
24
|
+
#include "align.h"
|
25
|
+
#include "config.h"
|
26
|
+
|
27
|
+
#define XKCP_DeclareDuplexStructure(prefix, state_t) \
|
28
|
+
typedef struct prefix##_DuplexInstanceStruct { \
|
29
|
+
state_t state; \
|
30
|
+
unsigned int rate; \
|
31
|
+
unsigned int byteInputIndex; \
|
32
|
+
unsigned int byteOutputIndex; \
|
33
|
+
} prefix##_DuplexInstance;
|
34
|
+
|
35
|
+
#define XKCP_DeclareDuplexFunctions(prefix) \
|
36
|
+
int prefix##_DuplexInitialize(prefix##_DuplexInstance *duplexInstance, unsigned int rate, unsigned int capacity); \
|
37
|
+
int prefix##_Duplexing(prefix##_DuplexInstance *duplexInstance, const unsigned char *sigmaBegin, unsigned int sigmaBeginByteLen, unsigned char *Z, unsigned int ZByteLen, unsigned char delimitedSigmaEnd); \
|
38
|
+
int prefix##_DuplexingFeedPartialInput(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned int inputByteLen); \
|
39
|
+
int prefix##_DuplexingFeedZeroes(prefix##_DuplexInstance *duplexInstance, unsigned int inputByteLen); \
|
40
|
+
int prefix##_DuplexingOverwritePartialInput(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned int inputByteLen); \
|
41
|
+
int prefix##_DuplexingOverwriteWithZeroes(prefix##_DuplexInstance *duplexInstance, unsigned int inputByteLen); \
|
42
|
+
int prefix##_DuplexingGetFurtherOutput(prefix##_DuplexInstance *duplexInstance, unsigned char *out, unsigned int outByteLen); \
|
43
|
+
int prefix##_DuplexingGetFurtherOutputAndAdd(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned char *output, unsigned int outputByteLen);
|
44
|
+
|
45
|
+
#ifdef XKCP_has_KeccakP200
|
46
|
+
#include "KeccakP-200-SnP.h"
|
47
|
+
XKCP_DeclareDuplexStructure(KeccakWidth200, KeccakP200_stateSizeInBytes, KeccakP200_stateAlignment)
|
48
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth200)
|
49
|
+
#define XKCP_has_Duplex_Keccak_width200
|
50
|
+
#endif
|
51
|
+
|
52
|
+
#ifdef XKCP_has_KeccakP400
|
53
|
+
#include "KeccakP-400-SnP.h"
|
54
|
+
XKCP_DeclareDuplexStructure(KeccakWidth400, KeccakP400_stateSizeInBytes, KeccakP400_stateAlignment)
|
55
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth400)
|
56
|
+
#define XKCP_has_Duplex_Keccak_width400
|
57
|
+
#endif
|
58
|
+
|
59
|
+
#ifdef XKCP_has_KeccakP800
|
60
|
+
#include "KeccakP-800-SnP.h"
|
61
|
+
XKCP_DeclareDuplexStructure(KeccakWidth800, KeccakP800_stateSizeInBytes, KeccakP800_stateAlignment)
|
62
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth800)
|
63
|
+
#define XKCP_has_Duplex_Keccak_width800
|
64
|
+
#endif
|
65
|
+
|
66
|
+
#ifdef XKCP_has_KeccakP1600
|
67
|
+
#include "KeccakP-1600-SnP.h"
|
68
|
+
XKCP_DeclareDuplexStructure(KeccakWidth1600, KeccakP1600_state)
|
69
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth1600)
|
70
|
+
#define XKCP_has_Duplex_Keccak_width1600
|
71
|
+
#endif
|
72
|
+
|
73
|
+
#endif
|