1f9f848faSopenharmony_ci/*-
2f9f848faSopenharmony_ci * Copyright (c) 2000-2015 Mark R V Murray
3f9f848faSopenharmony_ci * All rights reserved.
4f9f848faSopenharmony_ci *
5f9f848faSopenharmony_ci * Redistribution and use in source and binary forms, with or without
6f9f848faSopenharmony_ci * modification, are permitted provided that the following conditions
7f9f848faSopenharmony_ci * are met:
8f9f848faSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
9f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer
10f9f848faSopenharmony_ci *    in this position and unchanged.
11f9f848faSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
12f9f848faSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
13f9f848faSopenharmony_ci *    documentation and/or other materials provided with the distribution.
14f9f848faSopenharmony_ci *
15f9f848faSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f9f848faSopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f9f848faSopenharmony_ci * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f9f848faSopenharmony_ci * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19f9f848faSopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f9f848faSopenharmony_ci * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f9f848faSopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f9f848faSopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f9f848faSopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f9f848faSopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f9f848faSopenharmony_ci *
26f9f848faSopenharmony_ci */
27f9f848faSopenharmony_ci
28f9f848faSopenharmony_ci#include <sys/cdefs.h>
29f9f848faSopenharmony_ci#ifdef _KERNEL
30f9f848faSopenharmony_ci#include <sys/param.h>
31f9f848faSopenharmony_ci#include <sys/malloc.h>
32f9f848faSopenharmony_ci#include <sys/systm.h>
33f9f848faSopenharmony_ci#else /* !_KERNEL */
34f9f848faSopenharmony_ci#include <sys/param.h>
35f9f848faSopenharmony_ci#include <sys/types.h>
36f9f848faSopenharmony_ci#include <assert.h>
37f9f848faSopenharmony_ci#include <inttypes.h>
38f9f848faSopenharmony_ci#include <signal.h>
39f9f848faSopenharmony_ci#include <stdbool.h>
40f9f848faSopenharmony_ci#include <stdio.h>
41f9f848faSopenharmony_ci#include <stdlib.h>
42f9f848faSopenharmony_ci#include <string.h>
43f9f848faSopenharmony_ci#define KASSERT(x, y)	assert(x)
44f9f848faSopenharmony_ci#define CTASSERT(x)	_Static_assert(x, "CTASSERT " #x)
45f9f848faSopenharmony_ci#endif /* _KERNEL */
46f9f848faSopenharmony_ci
47f9f848faSopenharmony_ci#define CHACHA_EMBED
48f9f848faSopenharmony_ci#define KEYSTREAM_ONLY
49f9f848faSopenharmony_ci#define CHACHA_NONCE0_CTR128
50f9f848faSopenharmony_ci#include <crypto/rijndael/rijndael-api-fst.h>
51f9f848faSopenharmony_ci#include <crypto/sha2/sha256.h>
52f9f848faSopenharmony_ci
53f9f848faSopenharmony_ci#include <dev/random/hash.h>
54f9f848faSopenharmony_ci#ifdef _KERNEL
55f9f848faSopenharmony_ci#include <dev/random/randomdev.h>
56f9f848faSopenharmony_ci#endif
57f9f848faSopenharmony_ci
58f9f848faSopenharmony_ci/* This code presumes that RANDOM_KEYSIZE is twice as large as RANDOM_BLOCKSIZE */
59f9f848faSopenharmony_ci//CTASSERT(RANDOM_KEYSIZE == 2*RANDOM_BLOCKSIZE);
60f9f848faSopenharmony_ci
61f9f848faSopenharmony_ci/* Initialise the hash */
62f9f848faSopenharmony_civoid
63f9f848faSopenharmony_cirandomdev_hash_init(struct randomdev_hash *context)
64f9f848faSopenharmony_ci{
65f9f848faSopenharmony_ci
66f9f848faSopenharmony_ci	SHA256_Init(&context->sha);
67f9f848faSopenharmony_ci}
68f9f848faSopenharmony_ci
69f9f848faSopenharmony_ci/* Iterate the hash */
70f9f848faSopenharmony_civoid
71f9f848faSopenharmony_cirandomdev_hash_iterate(struct randomdev_hash *context, const void *data, size_t size)
72f9f848faSopenharmony_ci{
73f9f848faSopenharmony_ci
74f9f848faSopenharmony_ci	SHA256_Update(&context->sha, data, size);
75f9f848faSopenharmony_ci}
76f9f848faSopenharmony_ci
77f9f848faSopenharmony_ci/* Conclude by returning the hash in the supplied <*buf> which must be
78f9f848faSopenharmony_ci * RANDOM_KEYSIZE bytes long.
79f9f848faSopenharmony_ci */
80f9f848faSopenharmony_civoid
81f9f848faSopenharmony_cirandomdev_hash_finish(struct randomdev_hash *context, void *buf)
82f9f848faSopenharmony_ci{
83f9f848faSopenharmony_ci
84f9f848faSopenharmony_ci	SHA256_Final(buf, &context->sha);
85f9f848faSopenharmony_ci}
86f9f848faSopenharmony_ci
87f9f848faSopenharmony_ci/* Initialise the encryption routine by setting up the key schedule
88f9f848faSopenharmony_ci * from the supplied <*data> which must be RANDOM_KEYSIZE bytes of binary
89f9f848faSopenharmony_ci * data.
90f9f848faSopenharmony_ci */
91f9f848faSopenharmony_civoid
92f9f848faSopenharmony_cirandomdev_encrypt_init(struct randomdev_key *context, const void *data)
93f9f848faSopenharmony_ci{
94f9f848faSopenharmony_ci
95f9f848faSopenharmony_ci	rijndael_cipherInit(&context->cipher, MODE_CBC, NULL);
96f9f848faSopenharmony_ci	rijndael_makeKey(&context->key, DIR_ENCRYPT, RANDOM_KEYSIZE*8, data);
97f9f848faSopenharmony_ci}
98f9f848faSopenharmony_ci
99f9f848faSopenharmony_ci/* Encrypt the supplied data using the key schedule preset in the context.
100f9f848faSopenharmony_ci * <length> bytes are encrypted from <*d_in> to <*d_out>. <length> must be
101f9f848faSopenharmony_ci * a multiple of RANDOM_BLOCKSIZE.
102f9f848faSopenharmony_ci */
103f9f848faSopenharmony_civoid
104f9f848faSopenharmony_cirandomdev_encrypt(struct randomdev_key *context, const void *d_in, void *d_out, u_int length)
105f9f848faSopenharmony_ci{
106f9f848faSopenharmony_ci
107f9f848faSopenharmony_ci	rijndael_blockEncrypt(&context->cipher, &context->key, d_in, length*8, d_out);
108f9f848faSopenharmony_ci}
109