summaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-07-13 03:29:04 -0400
committerIan Moffett <ian@osmora.org>2025-07-13 03:30:26 -0400
commit2a93cf52e5386eb1b3586565af6a06ade4d32a66 (patch)
tree37e650b5c103f338cd24a4d5d19769f2f12ee179 /sys/dev
parent190f366320a33d331108400e5740f6721fa848f8 (diff)
kernel: Move crypto specifics to sys/crypto/*
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/random/chacha20.c97
-rw-r--r--sys/dev/random/entropy.c2
-rw-r--r--sys/dev/random/random.c4
-rw-r--r--sys/dev/random/siphash.c116
4 files changed, 3 insertions, 216 deletions
diff --git a/sys/dev/random/chacha20.c b/sys/dev/random/chacha20.c
deleted file mode 100644
index 41f823c..0000000
--- a/sys/dev/random/chacha20.c
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
- * 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 Hyra 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 OWNER 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 <dev/random/chacha20.h>
-
-static const char sigma[16] = "expand 32-byte k";
-
-void chacha20_init(uint32_t state[16], const uint8_t key[32],
- const uint8_t nonce[12], uint32_t counter)
-{
- state[0] = ((uint32_t *)sigma)[0];
- state[1] = ((uint32_t *)sigma)[1];
- state[2] = ((uint32_t *)sigma)[2];
- state[3] = ((uint32_t *)sigma)[3];
-
- for (int i = 0; i < 8; ++i) {
- state[4 + i] = ((uint32_t *)key)[i];
- }
-
- state[12] = counter;
- state[13] = ((uint32_t *)nonce)[0];
- state[14] = ((uint32_t *)nonce)[1];
- state[15] = ((uint32_t *)nonce)[2];
-}
-
-void
-chacha20_block(uint32_t state[16], uint8_t out[64])
-{
- uint32_t x[16];
- memcpy(x, state, sizeof(x));
-
- for (int i = 0; i < 10; i++) {
-
- QR(x[0], x[4], x[8], x[12]);
- QR(x[1], x[5], x[9], x[13]);
- QR(x[2], x[6], x[10], x[14]);
- QR(x[3], x[7], x[11], x[15]);
-
- QR(x[0], x[5], x[10], x[15]);
- QR(x[1], x[6], x[11], x[12]);
- QR(x[2], x[7], x[8], x[13]);
- QR(x[3], x[4], x[9], x[14]);
- }
-
- for (int i = 0; i < 16; ++i) {
- x[i] += state[i];
- ((uint32_t *)out)[i] = x[i];
- }
-
- state[12]++;
-}
-
-void
-chacha20_encrypt(uint32_t state[16], uint8_t *in,
- uint8_t *out, size_t len)
-{
- uint8_t block[64];
- size_t offset = 0;
-
- while (len > 0) {
- chacha20_block(state, block);
- size_t n = len > 64 ? 64 : len;
-
- for (size_t i = 0; i < n; ++i) {
- out[offset + i] = in ? in[offset + i] ^ block[i] : block[i];
- }
-
- offset += n;
- len -= n;
- }
-}
diff --git a/sys/dev/random/entropy.c b/sys/dev/random/entropy.c
index d392b9c..4e723a4 100644
--- a/sys/dev/random/entropy.c
+++ b/sys/dev/random/entropy.c
@@ -30,7 +30,7 @@
#include <stdint.h>
#include <string.h>
#include <dev/random/entropy.h>
-#include <dev/random/siphash.h>
+#include <crypto/siphash.h>
void
mix_entropy(struct entropy_pool *ep, const uint8_t *input,
diff --git a/sys/dev/random/random.c b/sys/dev/random/random.c
index d79df69..9bca719 100644
--- a/sys/dev/random/random.c
+++ b/sys/dev/random/random.c
@@ -30,9 +30,9 @@
#include <sys/sio.h>
#include <sys/device.h>
#include <sys/driver.h>
-#include <dev/random/chacha20.h>
-#include <dev/random/siphash.h>
#include <dev/random/entropy.h>
+#include <crypto/chacha20.h>
+#include <crypto/siphash.h>
#include <fs/devfs.h>
#include <string.h>
diff --git a/sys/dev/random/siphash.c b/sys/dev/random/siphash.c
deleted file mode 100644
index 2b2243f..0000000
--- a/sys/dev/random/siphash.c
+++ /dev/null
@@ -1,116 +0,0 @@
-/* <MIT License>
- Copyright (c) 2013 Marek Majkowski <marek@popcount.org>
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- </MIT License>
-
- Original location:
- https://github.com/majek/csiphash/
-
- Solution inspired by code from:
- Samuel Neves (supercop/crypto_auth/siphash24/little)
- djb (supercop/crypto_auth/siphash24/little2)
- Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)
-*/
-
-#include <dev/random/siphash.h>
-#include <stdint.h>
-
-#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
- __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-# define _le64toh(x) ((uint64_t)(x))
-#elif defined(_WIN32)
-/* Windows is always little endian, unless you're on xbox360
- http://msdn.microsoft.com/en-us/library/b0084kay(v=vs.80).aspx */
-# define _le64toh(x) ((uint64_t)(x))
-#elif defined(__APPLE__)
-# include <libkern/OSByteOrder.h>
-# define _le64toh(x) OSSwapLittleToHostInt64(x)
-#else
-
-/* See: http://sourceforge.net/p/predef/wiki/Endianness/ */
-# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
-# include <sys/endian.h>
-# else
-# include <endian.h>
-# endif
-# if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \
- __BYTE_ORDER == __LITTLE_ENDIAN
-# define _le64toh(x) ((uint64_t)(x))
-# else
-# define _le64toh(x) le64toh(x)
-# endif
-
-#endif
-
-
-#define ROTATE(x, b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
-
-#define HALF_ROUND(a,b,c,d,s,t) \
- a += b; c += d; \
- b = ROTATE(b, s) ^ a; \
- d = ROTATE(d, t) ^ c; \
- a = ROTATE(a, 32);
-
-#define DOUBLE_ROUND(v0,v1,v2,v3) \
- HALF_ROUND(v0,v1,v2,v3,13,16); \
- HALF_ROUND(v2,v1,v0,v3,17,21); \
- HALF_ROUND(v0,v1,v2,v3,13,16); \
- HALF_ROUND(v2,v1,v0,v3,17,21);
-
-
-uint64_t siphash24(const void *src, unsigned long src_sz, const char key[16]) {
- const uint64_t *_key = (uint64_t *)key;
- uint64_t k0 = _le64toh(_key[0]);
- uint64_t k1 = _le64toh(_key[1]);
- uint64_t b = (uint64_t)src_sz << 56;
- const uint64_t *in = (uint64_t*)src;
-
- uint64_t v0 = k0 ^ 0x736f6d6570736575ULL;
- uint64_t v1 = k1 ^ 0x646f72616e646f6dULL;
- uint64_t v2 = k0 ^ 0x6c7967656e657261ULL;
- uint64_t v3 = k1 ^ 0x7465646279746573ULL;
-
- while (src_sz >= 8) {
- uint64_t mi = _le64toh(*in);
- in += 1; src_sz -= 8;
- v3 ^= mi;
- DOUBLE_ROUND(v0,v1,v2,v3);
- v0 ^= mi;
- }
-
- uint64_t t = 0; uint8_t *pt = (uint8_t *)&t; uint8_t *m = (uint8_t *)in;
- switch (src_sz) {
- case 7: pt[6] = m[6];
- case 6: pt[5] = m[5];
- case 5: pt[4] = m[4];
- case 4: *((uint32_t*)&pt[0]) = *((uint32_t*)&m[0]); break;
- case 3: pt[2] = m[2];
- case 2: pt[1] = m[1];
- case 1: pt[0] = m[0];
- }
- b |= _le64toh(t);
-
- v3 ^= b;
- DOUBLE_ROUND(v0,v1,v2,v3);
- v0 ^= b; v2 ^= 0xff;
- DOUBLE_ROUND(v0,v1,v2,v3);
- DOUBLE_ROUND(v0,v1,v2,v3);
- return (v0 ^ v1) ^ (v2 ^ v3);
-}