dualcone 0.0.1 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2cefd1451fcbb7e091ab48f74b5cbca1eb8a585e3cc28edb34ff5a6f04beaac
4
- data.tar.gz: 5ba6021faa19db6e737f669459f86c464fc6878bfad7250bead60004f0f60c60
3
+ metadata.gz: 1564ad34458b134926891c68315f545012a27caa1b32ede501914e8bbbc3399f
4
+ data.tar.gz: c6ec5c47094d1aad2b4e1c6395fa98032e190bcda498dc1c8018065ce65a3d59
5
5
  SHA512:
6
- metadata.gz: 5b1a2bc0687e734ec4b7a103825a2b698f15ae01a3d8efca9a0fd27b6b8d41e42b66b103c93770b7fc2df46ce5deccdce81723eb0cb406578ae8a52b6ebf5c3e
7
- data.tar.gz: 98f13c8cc8c35459f34698d84f119fb7e5550128c76af207151ba3438b6ac056f5c199b17a54c8f95848e427ec3dcd71e05c5c58ba84e5f1ef4b486e95f8ad68
6
+ metadata.gz: c8c3774ca0f84a1d9863b907d361b7b1c5c13f6e60c31b42748af0096141d72a66256ddfeb77924ba01bad6e3fa76402d4083fc72492ee86d618f8de90c24158
7
+ data.tar.gz: a9d39de78eba10857b0a457f5dec3a61dfc73d3c07051bafc27ffd0c6e4c9402d35cb475e1ed5486da619af347717ca1b6aa283399db363466bc12860d6964e1
data/LICENSE.txt CHANGED
@@ -1,21 +1,15 @@
1
- The MIT License (MIT)
1
+ ISC License
2
2
 
3
3
  Copyright (c) 2020 Tom Richards
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Dualcone
2
2
 
3
- [![CircleCI](https://circleci.com/gh/t-richards/dualcone.svg?style=svg)](https://circleci.com/gh/t-richards/dualcone)
3
+ ![Test](https://github.com/t-richards/dualcone/actions/workflows/test.yml/badge.svg)
4
4
 
5
5
  Dualcone is a Ruby source code protection system. Dualcone uses symmetric encryption to protect your source code.
6
6
 
@@ -8,20 +8,6 @@ Dualcone is a self-contained gem. It brings along its own copy of the lightweigh
8
8
 
9
9
  Dualcone supports GNU + Linux and other Unix-like operating systems. Windows is not supported.
10
10
 
11
- ## Roadmap
12
-
13
- ### Part 1
14
- - [x] Key generation: `Dualcone.generate_key`
15
- - [x] Encrypted code running: `Dualcone.run(code)`
16
- - [x] Encrypted code generation: `Dualcone.encrypt(path)`
17
- - [x] Specs passing
18
-
19
- ### Part 2
20
- - [x] Runnable trivial ruby script
21
- - [ ] Runnable non-trivial ruby script
22
- - [ ] Runnable sinatra app
23
- - [ ] Runnable rails app
24
-
25
11
  ## Installation
26
12
 
27
13
  Add this gem to your application's Gemfile:
@@ -103,14 +89,21 @@ You can also run `bin/console` for an interactive prompt that will allow you to
103
89
 
104
90
  To install this gem onto your local machine, run `bin/rake install`. To release a new version, update the version number in `version.rb`, and then run `bin/rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org][rubygems].
105
91
 
92
+ ## Roadmap
93
+
94
+ - [x] Runnable trivial ruby script
95
+ - [ ] Runnable non-trivial ruby script
96
+ - [ ] Runnable sinatra app
97
+ - [ ] Runnable rails app
98
+
106
99
  ## Contributing
107
100
 
108
101
  Bug reports and pull requests are welcome on GitHub at https://github.com/t-richards/dualcone.
109
102
 
110
103
  ## License
111
104
 
112
- The gem is available as open source under the terms of the [MIT License][mit-license].
105
+ The gem is available as open source under the terms of the [ISC License][isc-license].
113
106
 
114
107
  [libhydrogen]: https://github.com/jedisct1/libhydrogen
115
- [mit-license]: https://opensource.org/licenses/MIT
108
+ [isc-license]: LICENSE.txt
116
109
  [rubygems]: https://rubygems.org
@@ -2,7 +2,8 @@
2
2
 
3
3
  VALUE rb_mDualcone;
4
4
 
5
- void rb_dualcone_cleanup(DualconeContext *ctx) {
5
+ static void
6
+ rb_dualcone_cleanup(DualconeContext *ctx) {
6
7
  if (ctx->input_path != NULL) {
7
8
  free(ctx->input_path);
8
9
  }
@@ -31,7 +32,8 @@ void rb_dualcone_cleanup(DualconeContext *ctx) {
31
32
  hydro_memzero(ctx, sizeof(DualconeContext));
32
33
  }
33
34
 
34
- void rb_dualcone_get_key(DualconeContext *ctx) {
35
+ static void
36
+ rb_dualcone_get_key(DualconeContext *ctx) {
35
37
  int result = 0;
36
38
  int errno_sv = 0;
37
39
 
@@ -51,7 +53,22 @@ void rb_dualcone_get_key(DualconeContext *ctx) {
51
53
  }
52
54
  }
53
55
 
54
- VALUE rb_dualcone_run(VALUE _self, VALUE code) {
56
+ /*
57
+ * call-seq:
58
+ * Dualcone.run(code) -> nil
59
+ *
60
+ * Executes the encrypted +code+.
61
+ *
62
+ * If successful, this method returns nil.
63
+ * Otherwise, it raises a fatal error.
64
+ *
65
+ * ENV['DUALCONE_HEX_KEY'] = '7240adfda679de86902864bc4e05864fda6e06cd24256885e0cda1ac02d03dd8'
66
+ * Dualcone.run('d4d745de7437d7d66fb78b5d16b41de0eb716d26b282226e3e3d5b826f4704cc0e19d1fee990d059c198')
67
+ * "a"
68
+ * => nil
69
+ */
70
+ static VALUE
71
+ rb_dualcone_run(VALUE _self, VALUE code) {
55
72
  int result = 0;
56
73
  int errno_sv = 0;
57
74
 
@@ -117,7 +134,17 @@ VALUE rb_dualcone_run(VALUE _self, VALUE code) {
117
134
  return Qnil;
118
135
  }
119
136
 
120
- VALUE rb_dualcone_generate_key(VALUE _self) {
137
+ /*
138
+ * call-seq:
139
+ * Dualcone.generate_key -> string
140
+ *
141
+ * Returns a new secret encryption key in hex-encoded format.
142
+ *
143
+ * Dualcone.generate_key
144
+ * => "7240adfda679de86902864bc4e05864fda6e06cd24256885e0cda1ac02d03dd8"
145
+ */
146
+ static VALUE
147
+ rb_dualcone_generate_key(VALUE _self) {
121
148
  uint8_t key[hydro_secretbox_KEYBYTES];
122
149
  char hex[hydro_secretbox_KEYBYTES * 2 + 1];
123
150
 
@@ -131,7 +158,20 @@ VALUE rb_dualcone_generate_key(VALUE _self) {
131
158
  return rb_str_new_cstr(hex);
132
159
  }
133
160
 
134
- VALUE rb_dualcone_encrypt(VALUE _self, VALUE path) {
161
+ /*
162
+ * call-seq:
163
+ * Dualcone.encrypt(path) -> nil
164
+ *
165
+ * Encrypts (and overwrites!) the Ruby code file specified by +path+.
166
+ *
167
+ * If successful, this method returns nil.
168
+ * Otherwise, it raises a fatal error.
169
+ *
170
+ * Dualcone.encrypt('hello.rb')
171
+ * => nil
172
+ */
173
+ static VALUE
174
+ rb_dualcone_encrypt(VALUE _self, VALUE path) {
135
175
  int result = 0;
136
176
  int errno_sv = 0;
137
177
 
@@ -2,33 +2,34 @@
2
2
 
3
3
  require 'mkmf'
4
4
 
5
+ # :stopdoc:
6
+
5
7
  cflags = %w[
6
- -march=native -fno-exceptions -pipe
7
- -fstack-protector-strong -fPIC -Wall -Werror
8
- -Wno-missing-braces
8
+ -march=native -mtune=generic -pipe -fno-plt
9
+ -fstack-protector-strong -fPIC
9
10
  ]
10
11
 
11
12
  if ENV['DEBUG']
12
13
  cflags.unshift('-O0', '-g')
13
14
  else
14
- cflags.unshift('-Os')
15
+ cflags.unshift('-O2')
15
16
  end
16
17
 
17
- LIBHYDROGEN_DIR = File.join(__dir__, '..', '..', 'vendor', 'libhydrogen')
18
+ libhydrogen_dir = File.join(__dir__, '..', '..', 'vendor', 'libhydrogen')
18
19
 
19
20
  abort 'ERROR: make is required to build libhydrogen.' unless find_executable('make')
20
21
 
21
22
  append_cflags(cflags)
22
23
 
23
24
  # Build the bundled version of libhydrogen in vendor
24
- Dir.chdir(LIBHYDROGEN_DIR) do
25
+ Dir.chdir(libhydrogen_dir) do
25
26
  system('make clean')
26
27
  system("export CFLAGS='#{cflags.join(' ')}'; make")
27
28
  system('PREFIX=. make install')
28
29
 
29
30
  # Ensure that our bundled version of libhydrogen is always used
30
- $DEFLIBPATH.unshift("#{LIBHYDROGEN_DIR}/lib")
31
- dir_config('hydrogen', "#{LIBHYDROGEN_DIR}/include", "#{LIBHYDROGEN_DIR}/lib")
31
+ $DEFLIBPATH.unshift("#{libhydrogen_dir}/lib")
32
+ dir_config('hydrogen', "#{libhydrogen_dir}/include", "#{libhydrogen_dir}/lib")
32
33
  end
33
34
 
34
35
  abort 'ERROR: Failed to build libhydrogen.' unless have_library('hydrogen') && have_header('hydrogen.h')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dualcone
4
- VERSION = '0.0.1'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * ISC License
3
3
  *
4
- * Copyright (c) 2017-2019
4
+ * Copyright (c) 2017-2022
5
5
  * Frank Denis <j at pureftpd dot org>
6
6
  *
7
7
  * Permission to use, copy, modify, and/or distribute this software for any
@@ -1,6 +1,6 @@
1
1
  PREFIX ?= /usr/local
2
2
  WFLAGS ?= -Wall -Wextra -Wmissing-prototypes -Wdiv-by-zero -Wbad-function-cast -Wcast-align -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wnested-externs -Wno-unknown-pragmas -Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wswitch-enum -Wno-type-limits
3
- CFLAGS ?= -Os -march=native -fno-exceptions $(WFLAGS)
3
+ CFLAGS ?= -Os -mcpu=native -fno-exceptions $(WFLAGS)
4
4
  CFLAGS += -I.
5
5
  OBJ = hydrogen.o
6
6
  AR ?= ar
@@ -1,5 +1,7 @@
1
- [![Build Status](https://travis-ci.org/jedisct1/libhydrogen.svg?branch=master)](https://travis-ci.org/jedisct1/libhydrogen?branch=master)
1
+ [![Build status](https://travis-ci.org/jedisct1/libhydrogen.svg?branch=master)](https://travis-ci.org/jedisct1/libhydrogen?branch=master)
2
+ ![CodeQL scan](https://github.com/jedisct1/libhydrogen/workflows/Code%20scanning%20-%20action/badge.svg)
2
3
  [![Financial Contributors on Open Collective](https://opencollective.com/libhydrogen/all/badge.svg?label=financial+contributors)](https://opencollective.com/libhydrogen) [![Coverity Scan Build Status](https://scan.coverity.com/projects/13315/badge.svg)](https://scan.coverity.com/projects/13315)
4
+ [![TrustInSoft CI](https://ci.trust-in-soft.com/projects/jedisct1/libhydrogen.svg?branch=master)](https://ci.trust-in-soft.com/projects/jedisct1/libhydrogen)
3
5
 
4
6
  ![libhydrogen](https://raw.github.com/jedisct1/libhydrogen/master/logo.png)
5
7
  ==============
@@ -3,9 +3,10 @@
3
3
  #include "impl/common.h"
4
4
  #include "impl/hydrogen_p.h"
5
5
 
6
+ #include "impl/random.h"
7
+
6
8
  #include "impl/core.h"
7
9
  #include "impl/gimli-core.h"
8
- #include "impl/random.h"
9
10
 
10
11
  #include "impl/hash.h"
11
12
  #include "impl/kdf.h"
@@ -17,12 +17,12 @@ extern "C" {
17
17
  #else
18
18
  #define _hydro_attr_(X)
19
19
  #endif
20
- #define _hydro_attr_deprecated_ _hydro_attr_((deprecated))
21
- #define _hydro_attr_malloc_ _hydro_attr_((malloc))
22
- #define _hydro_attr_noinline_ _hydro_attr_((noinline))
23
- #define _hydro_attr_noreturn_ _hydro_attr_((noreturn))
20
+ #define _hydro_attr_deprecated_ _hydro_attr_((deprecated))
21
+ #define _hydro_attr_malloc_ _hydro_attr_((malloc))
22
+ #define _hydro_attr_noinline_ _hydro_attr_((noinline))
23
+ #define _hydro_attr_noreturn_ _hydro_attr_((noreturn))
24
24
  #define _hydro_attr_warn_unused_result_ _hydro_attr_((warn_unused_result))
25
- #define _hydro_attr_weak_ _hydro_attr_((weak))
25
+ #define _hydro_attr_weak_ _hydro_attr_((weak))
26
26
 
27
27
  #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
28
28
  #define _hydro_attr_aligned_(X) __declspec(align(X))
@@ -56,11 +56,11 @@ void hydro_random_reseed(void);
56
56
 
57
57
  /* ---------------- */
58
58
 
59
- #define hydro_hash_BYTES 32
60
- #define hydro_hash_BYTES_MAX 65535
61
- #define hydro_hash_BYTES_MIN 16
59
+ #define hydro_hash_BYTES 32
60
+ #define hydro_hash_BYTES_MAX 65535
61
+ #define hydro_hash_BYTES_MIN 16
62
62
  #define hydro_hash_CONTEXTBYTES 8
63
- #define hydro_hash_KEYBYTES 32
63
+ #define hydro_hash_KEYBYTES 32
64
64
 
65
65
  typedef struct hydro_hash_state {
66
66
  uint32_t state[12];
@@ -84,9 +84,9 @@ int hydro_hash_hash(uint8_t *out, size_t out_len, const void *in_, size_t in_len
84
84
  /* ---------------- */
85
85
 
86
86
  #define hydro_secretbox_CONTEXTBYTES 8
87
- #define hydro_secretbox_HEADERBYTES (20 + 16)
88
- #define hydro_secretbox_KEYBYTES 32
89
- #define hydro_secretbox_PROBEBYTES 16
87
+ #define hydro_secretbox_HEADERBYTES (20 + 16)
88
+ #define hydro_secretbox_KEYBYTES 32
89
+ #define hydro_secretbox_PROBEBYTES 16
90
90
 
91
91
  void hydro_secretbox_keygen(uint8_t key[hydro_secretbox_KEYBYTES]);
92
92
 
@@ -111,9 +111,9 @@ int hydro_secretbox_probe_verify(const uint8_t probe[hydro_secretbox_PROBEBYTES]
111
111
  /* ---------------- */
112
112
 
113
113
  #define hydro_kdf_CONTEXTBYTES 8
114
- #define hydro_kdf_KEYBYTES 32
115
- #define hydro_kdf_BYTES_MAX 65535
116
- #define hydro_kdf_BYTES_MIN 16
114
+ #define hydro_kdf_KEYBYTES 32
115
+ #define hydro_kdf_BYTES_MAX 65535
116
+ #define hydro_kdf_BYTES_MIN 16
117
117
 
118
118
  void hydro_kdf_keygen(uint8_t key[hydro_kdf_KEYBYTES]);
119
119
 
@@ -123,11 +123,11 @@ int hydro_kdf_derive_from_key(uint8_t *subkey, size_t subkey_len, uint64_t subke
123
123
 
124
124
  /* ---------------- */
125
125
 
126
- #define hydro_sign_BYTES 64
127
- #define hydro_sign_CONTEXTBYTES 8
126
+ #define hydro_sign_BYTES 64
127
+ #define hydro_sign_CONTEXTBYTES 8
128
128
  #define hydro_sign_PUBLICKEYBYTES 32
129
129
  #define hydro_sign_SECRETKEYBYTES 64
130
- #define hydro_sign_SEEDBYTES 32
130
+ #define hydro_sign_SEEDBYTES 32
131
131
 
132
132
  typedef struct hydro_sign_state {
133
133
  hydro_hash_state hash_st;
@@ -165,10 +165,10 @@ int hydro_sign_verify(const uint8_t csig[hydro_sign_BYTES], const void *m_, size
165
165
  /* ---------------- */
166
166
 
167
167
  #define hydro_kx_SESSIONKEYBYTES 32
168
- #define hydro_kx_PUBLICKEYBYTES 32
169
- #define hydro_kx_SECRETKEYBYTES 32
170
- #define hydro_kx_PSKBYTES 32
171
- #define hydro_kx_SEEDBYTES 32
168
+ #define hydro_kx_PUBLICKEYBYTES 32
169
+ #define hydro_kx_SECRETKEYBYTES 32
170
+ #define hydro_kx_PSKBYTES 32
171
+ #define hydro_kx_SEEDBYTES 32
172
172
 
173
173
  typedef struct hydro_kx_keypair {
174
174
  uint8_t pk[hydro_kx_PUBLICKEYBYTES];
@@ -261,9 +261,9 @@ int hydro_kx_nk_3(hydro_kx_state *state, hydro_kx_session_keypair *kp,
261
261
 
262
262
  /* ---------------- */
263
263
 
264
- #define hydro_pwhash_CONTEXTBYTES 8
264
+ #define hydro_pwhash_CONTEXTBYTES 8
265
265
  #define hydro_pwhash_MASTERKEYBYTES 32
266
- #define hydro_pwhash_STOREDBYTES 128
266
+ #define hydro_pwhash_STOREDBYTES 128
267
267
 
268
268
  void hydro_pwhash_keygen(uint8_t master_key[hydro_pwhash_MASTERKEYBYTES]);
269
269
 
@@ -93,7 +93,7 @@ store64_le(uint8_t dst[8], uint64_t w)
93
93
  w >>= 8;
94
94
  dst[6] = (uint8_t) w;
95
95
  w >>= 8;
96
- dst[7] = (uint8_t) w;
96
+ dst[7] = (uint8_t) w;
97
97
  #endif
98
98
  }
99
99
 
@@ -127,7 +127,7 @@ store32_le(uint8_t dst[4], uint32_t w)
127
127
  w >>= 8;
128
128
  dst[2] = (uint8_t) w;
129
129
  w >>= 8;
130
- dst[3] = (uint8_t) w;
130
+ dst[3] = (uint8_t) w;
131
131
  #endif
132
132
  }
133
133
 
@@ -203,7 +203,7 @@ store64_be(uint8_t dst[8], uint64_t w)
203
203
  w >>= 8;
204
204
  dst[1] = (uint8_t) w;
205
205
  w >>= 8;
206
- dst[0] = (uint8_t) w;
206
+ dst[0] = (uint8_t) w;
207
207
  #endif
208
208
  }
209
209
 
@@ -1,9 +1,7 @@
1
1
  int
2
2
  hydro_init(void)
3
3
  {
4
- if (hydro_random_init() != 0) {
5
- abort();
6
- }
4
+ hydro_random_ensure_initialized();
7
5
  return 0;
8
6
  }
9
7
 
@@ -88,7 +86,7 @@ hydro_hex2bin(uint8_t *bin, size_t bin_maxlen, const char *hex, size_t hex_len,
88
86
  }
89
87
  break;
90
88
  }
91
- c_val = (uint8_t)((c_num0 & c_num) | (c_alpha0 & c_alpha));
89
+ c_val = (uint8_t) ((c_num0 & c_num) | (c_alpha0 & c_alpha));
92
90
  if (bin_pos >= bin_maxlen) {
93
91
  ret = -1;
94
92
  errno = ERANGE;
@@ -40,7 +40,7 @@ rotate24(__m128i x)
40
40
  }
41
41
  #endif
42
42
 
43
- static const uint32_t coeffs[24] _hydro_attr_aligned_(16) = {
43
+ static const uint32_t _hydro_attr_aligned_(16) coeffs[24] = {
44
44
  0x9e377904, 0, 0, 0, 0x9e377908, 0, 0, 0, 0x9e37790c, 0, 0, 0,
45
45
  0x9e377910, 0, 0, 0, 0x9e377914, 0, 0, 0, 0x9e377918, 0, 0, 0,
46
46
  };
@@ -43,10 +43,10 @@ hydro_hash_init(hydro_hash_state *state, const char ctx[hydro_hash_CONTEXTBYTES]
43
43
  if (key != NULL) {
44
44
  block[gimli_RATE] = (uint8_t) hydro_hash_KEYBYTES;
45
45
  memcpy(block + gimli_RATE + 1, key, hydro_hash_KEYBYTES);
46
- p = (gimli_RATE + 1 + hydro_hash_KEYBYTES + (gimli_RATE - 1)) & ~(size_t)(gimli_RATE - 1);
46
+ p = (gimli_RATE + 1 + hydro_hash_KEYBYTES + (gimli_RATE - 1)) & ~(size_t) (gimli_RATE - 1);
47
47
  } else {
48
48
  block[gimli_RATE] = (uint8_t) 0;
49
- p = (gimli_RATE + 1 + 0 + (gimli_RATE - 1)) & ~(size_t)(gimli_RATE - 1);
49
+ p = (gimli_RATE + 1 + 0 + (gimli_RATE - 1)) & ~(size_t) (gimli_RATE - 1);
50
50
  }
51
51
  mem_zero(state, sizeof *state);
52
52
  hydro_hash_update(state, block, p);
@@ -71,10 +71,10 @@ hydro_hash_init_with_tweak(hydro_hash_state *state, const char ctx[hydro_hash_CO
71
71
  if (key != NULL) {
72
72
  block[gimli_RATE] = (uint8_t) hydro_hash_KEYBYTES;
73
73
  memcpy(block + gimli_RATE + 1, key, hydro_hash_KEYBYTES);
74
- p = (gimli_RATE + 1 + hydro_hash_KEYBYTES + (gimli_RATE - 1)) & ~(size_t)(gimli_RATE - 1);
74
+ p = (gimli_RATE + 1 + hydro_hash_KEYBYTES + (gimli_RATE - 1)) & ~(size_t) (gimli_RATE - 1);
75
75
  } else {
76
76
  block[gimli_RATE] = (uint8_t) 0;
77
- p = (gimli_RATE + 1 + 0 + (gimli_RATE - 1)) & ~(size_t)(gimli_RATE - 1);
77
+ p = (gimli_RATE + 1 + 0 + (gimli_RATE - 1)) & ~(size_t) (gimli_RATE - 1);
78
78
  }
79
79
  block[p] = (uint8_t) sizeof tweak;
80
80
  STORE64_LE(&block[p + 1], tweak);
@@ -99,9 +99,9 @@ hydro_hash_final(hydro_hash_state *state, uint8_t *out, size_t out_len)
99
99
  }
100
100
  COMPILER_ASSERT(hydro_hash_BYTES_MAX <= 0xffff);
101
101
  lc[1] = (uint8_t) out_len;
102
- lc[2] = (uint8_t)(out_len >> 8);
102
+ lc[2] = (uint8_t) (out_len >> 8);
103
103
  lc[3] = 0;
104
- lc_len = (size_t)(1 + (lc[2] != 0));
104
+ lc_len = (size_t) (1 + (lc[2] != 0));
105
105
  lc[0] = (uint8_t) lc_len;
106
106
  hydro_hash_update(state, lc, 1 + lc_len + 1);
107
107
  gimli_pad_u8(buf, state->buf_off, gimli_DOMAIN_XOF);
@@ -3,18 +3,18 @@ static int hydro_random_init(void);
3
3
  /* ---------------- */
4
4
 
5
5
  #define gimli_BLOCKBYTES 48
6
- #define gimli_CAPACITY 32
7
- #define gimli_RATE 16
6
+ #define gimli_CAPACITY 32
7
+ #define gimli_RATE 16
8
8
 
9
- #define gimli_TAG_HEADER 0x01
9
+ #define gimli_TAG_HEADER 0x01
10
10
  #define gimli_TAG_PAYLOAD 0x02
11
- #define gimli_TAG_FINAL 0x08
12
- #define gimli_TAG_FINAL0 0xf8
13
- #define gimli_TAG_KEY0 0xfe
14
- #define gimli_TAG_KEY 0xff
11
+ #define gimli_TAG_FINAL 0x08
12
+ #define gimli_TAG_FINAL0 0xf8
13
+ #define gimli_TAG_KEY0 0xfe
14
+ #define gimli_TAG_KEY 0xff
15
15
 
16
16
  #define gimli_DOMAIN_AEAD 0x0
17
- #define gimli_DOMAIN_XOF 0xf
17
+ #define gimli_DOMAIN_XOF 0xf
18
18
 
19
19
  static void gimli_core_u8(uint8_t state_u8[gimli_BLOCKBYTES], uint8_t tag);
20
20
 
@@ -28,7 +28,7 @@ gimli_pad_u8(uint8_t buf[gimli_BLOCKBYTES], size_t pos, uint8_t domain)
28
28
  static inline void
29
29
  hydro_mem_ct_zero_u32(uint32_t *dst_, size_t n)
30
30
  {
31
- volatile uint32_t *volatile dst = (volatile uint32_t * volatile)(void *) dst_;
31
+ volatile uint32_t *volatile dst = (volatile uint32_t *volatile) (void *) dst_;
32
32
  size_t i;
33
33
 
34
34
  for (i = 0; i < n; i++) {
@@ -42,7 +42,7 @@ static inline uint32_t hydro_mem_ct_cmp_u32(const uint32_t *b1_, const uint32_t
42
42
  static inline uint32_t
43
43
  hydro_mem_ct_cmp_u32(const uint32_t *b1_, const uint32_t *b2, size_t n)
44
44
  {
45
- const volatile uint32_t *volatile b1 = (const volatile uint32_t *volatile)(const void *) b1_;
45
+ const volatile uint32_t *volatile b1 = (const volatile uint32_t *volatile) (const void *) b1_;
46
46
  size_t i;
47
47
  uint32_t cv = 0;
48
48
 
@@ -61,11 +61,11 @@ static int hydro_hash_init_with_tweak(hydro_hash_state *state,
61
61
  /* ---------------- */
62
62
 
63
63
  #define hydro_secretbox_NONCEBYTES 20
64
- #define hydro_secretbox_MACBYTES 16
64
+ #define hydro_secretbox_MACBYTES 16
65
65
 
66
66
  /* ---------------- */
67
67
 
68
- #define hydro_x25519_BYTES 32
68
+ #define hydro_x25519_BYTES 32
69
69
  #define hydro_x25519_PUBLICKEYBYTES 32
70
70
  #define hydro_x25519_SECRETKEYBYTES 32
71
71
 
@@ -1,16 +1,16 @@
1
- #define hydro_pwhash_ENC_ALGBYTES 1
1
+ #define hydro_pwhash_ENC_ALGBYTES 1
2
2
  #define hydro_pwhash_HASH_ALGBYTES 1
3
- #define hydro_pwhash_THREADSBYTES 1
3
+ #define hydro_pwhash_THREADSBYTES 1
4
4
  #define hydro_pwhash_OPSLIMITBYTES 8
5
5
  #define hydro_pwhash_MEMLIMITBYTES 8
6
- #define hydro_pwhash_HASHBYTES 32
7
- #define hydro_pwhash_SALTBYTES 16
6
+ #define hydro_pwhash_HASHBYTES 32
7
+ #define hydro_pwhash_SALTBYTES 16
8
8
  #define hydro_pwhash_PARAMSBYTES \
9
9
  (hydro_pwhash_HASH_ALGBYTES + hydro_pwhash_THREADSBYTES + hydro_pwhash_OPSLIMITBYTES + \
10
10
  hydro_pwhash_MEMLIMITBYTES + hydro_pwhash_SALTBYTES + hydro_pwhash_HASHBYTES)
11
- #define hydro_pwhash_ENC_ALG 0x01
11
+ #define hydro_pwhash_ENC_ALG 0x01
12
12
  #define hydro_pwhash_HASH_ALG 0x01
13
- #define hydro_pwhash_CONTEXT "hydro_pw"
13
+ #define hydro_pwhash_CONTEXT "hydro_pw"
14
14
 
15
15
  static int
16
16
  _hydro_pwhash_hash(uint8_t out[hydro_random_SEEDBYTES], size_t h_len,
@@ -0,0 +1,61 @@
1
+ #include <Arduino.h>
2
+
3
+ static bool
4
+ hydro_random_rbit(uint16_t x)
5
+ {
6
+ uint8_t x8;
7
+
8
+ x8 = ((uint8_t) (x >> 8)) ^ (uint8_t) x;
9
+ x8 = (x8 >> 4) ^ (x8 & 0xf);
10
+ x8 = (x8 >> 2) ^ (x8 & 0x3);
11
+ x8 = (x8 >> 1) ^ x8;
12
+
13
+ return (bool) (x8 & 1);
14
+ }
15
+
16
+ static int
17
+ hydro_random_init(void)
18
+ {
19
+ const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
20
+ hydro_hash_state st;
21
+ uint16_t ebits = 0;
22
+ uint16_t tc;
23
+ bool a, b;
24
+
25
+ cli();
26
+ MCUSR = 0;
27
+ WDTCSR |= _BV(WDCE) | _BV(WDE);
28
+ WDTCSR = _BV(WDIE);
29
+ sei();
30
+
31
+ hydro_hash_init(&st, ctx, NULL);
32
+
33
+ while (ebits < 256) {
34
+ delay(1);
35
+ tc = TCNT1;
36
+ hydro_hash_update(&st, (const uint8_t *) &tc, sizeof tc);
37
+ a = hydro_random_rbit(tc);
38
+ delay(1);
39
+ tc = TCNT1;
40
+ b = hydro_random_rbit(tc);
41
+ hydro_hash_update(&st, (const uint8_t *) &tc, sizeof tc);
42
+ if (a == b) {
43
+ continue;
44
+ }
45
+ hydro_hash_update(&st, (const uint8_t *) &b, sizeof b);
46
+ ebits++;
47
+ }
48
+
49
+ cli();
50
+ MCUSR = 0;
51
+ WDTCSR |= _BV(WDCE) | _BV(WDE);
52
+ WDTCSR = 0;
53
+ sei();
54
+
55
+ hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
56
+ hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
57
+
58
+ return 0;
59
+ }
60
+
61
+ ISR(WDT_vect) { }
@@ -0,0 +1,32 @@
1
+ // Important: RF *must* be activated on ESP board
2
+ // https://techtutorialsx.com/2017/12/22/esp32-arduino-random-number-generation/
3
+ #ifdef ESP32
4
+ #include <esp_system.h>
5
+ #endif
6
+
7
+ #ifdef ARDUINO
8
+ #include <Arduino.h>
9
+ #endif
10
+
11
+ static int
12
+ hydro_random_init(void)
13
+ {
14
+ const char ctx[hydro_hash_CONTEXTBYTES] = { 'h', 'y', 'd', 'r', 'o', 'P', 'R', 'G' };
15
+ hydro_hash_state st;
16
+ uint16_t ebits = 0;
17
+
18
+ hydro_hash_init(&st, ctx, NULL);
19
+
20
+ while (ebits < 256) {
21
+ uint32_t r = esp_random();
22
+
23
+ delay(10);
24
+ hydro_hash_update(&st, (const uint32_t *) &r, sizeof r);
25
+ ebits += 32;
26
+ }
27
+
28
+ hydro_hash_final(&st, hydro_random_context.state, sizeof hydro_random_context.state);
29
+ hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
30
+
31
+ return 0;
32
+ }