chacha_impl.c revision 1.1 1 1.1 riastrad /* $NetBSD: chacha_impl.c,v 1.1 2020/07/25 22:46:34 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/types.h>
30 1.1 riastrad #include <sys/cdefs.h>
31 1.1 riastrad #include <sys/errno.h>
32 1.1 riastrad #include <sys/module.h>
33 1.1 riastrad #include <sys/once.h>
34 1.1 riastrad #include <sys/sysctl.h>
35 1.1 riastrad
36 1.1 riastrad #include <lib/libkern/libkern.h>
37 1.1 riastrad
38 1.1 riastrad #include "chacha.h"
39 1.1 riastrad #include "chacha_ref.h"
40 1.1 riastrad
41 1.1 riastrad static const struct chacha_impl *chacha_md_impl __read_mostly;
42 1.1 riastrad static const struct chacha_impl *chacha_impl __read_mostly;
43 1.1 riastrad
44 1.1 riastrad static int
45 1.1 riastrad sysctl_hw_chacha_impl(SYSCTLFN_ARGS)
46 1.1 riastrad {
47 1.1 riastrad struct sysctlnode node;
48 1.1 riastrad
49 1.1 riastrad KASSERTMSG(chacha_impl != NULL,
50 1.1 riastrad "sysctl ran before ChaCha implementation was selected");
51 1.1 riastrad
52 1.1 riastrad node = *rnode;
53 1.1 riastrad node.sysctl_data = __UNCONST(chacha_impl->ci_name);
54 1.1 riastrad node.sysctl_size = strlen(chacha_impl->ci_name) + 1;
55 1.1 riastrad return sysctl_lookup(SYSCTLFN_CALL(&node));
56 1.1 riastrad }
57 1.1 riastrad
58 1.1 riastrad SYSCTL_SETUP(sysctl_hw_chacha_setup, "sysctl hw.chacha_impl setup")
59 1.1 riastrad {
60 1.1 riastrad
61 1.1 riastrad sysctl_createv(clog, 0, NULL, NULL,
62 1.1 riastrad CTLFLAG_PERMANENT|CTLFLAG_READONLY, CTLTYPE_STRING, "chacha_impl",
63 1.1 riastrad SYSCTL_DESCR("Selected ChaCha implementation"),
64 1.1 riastrad sysctl_hw_chacha_impl, 0, NULL, 0,
65 1.1 riastrad CTL_HW, CTL_CREATE, CTL_EOL);
66 1.1 riastrad }
67 1.1 riastrad
68 1.1 riastrad static int
69 1.1 riastrad chacha_select(void)
70 1.1 riastrad {
71 1.1 riastrad
72 1.1 riastrad KASSERT(chacha_impl == NULL);
73 1.1 riastrad
74 1.1 riastrad if (chacha_md_impl) {
75 1.1 riastrad if (chacha_selftest(chacha_md_impl))
76 1.1 riastrad aprint_error("chacha: self-test failed: %s\n",
77 1.1 riastrad chacha_md_impl->ci_name);
78 1.1 riastrad else
79 1.1 riastrad chacha_impl = chacha_md_impl;
80 1.1 riastrad }
81 1.1 riastrad if (chacha_impl == NULL) {
82 1.1 riastrad if (chacha_selftest(&chacha_ref_impl))
83 1.1 riastrad aprint_error("chacha: self-test failed: %s\n",
84 1.1 riastrad chacha_ref_impl.ci_name);
85 1.1 riastrad else
86 1.1 riastrad chacha_impl = &chacha_ref_impl;
87 1.1 riastrad }
88 1.1 riastrad if (chacha_impl == NULL)
89 1.1 riastrad panic("ChaCha self-tests failed");
90 1.1 riastrad
91 1.1 riastrad aprint_verbose("chacha: %s\n", chacha_impl->ci_name);
92 1.1 riastrad return 0;
93 1.1 riastrad }
94 1.1 riastrad
95 1.1 riastrad MODULE(MODULE_CLASS_MISC, chacha, NULL);
96 1.1 riastrad
97 1.1 riastrad static int
98 1.1 riastrad chacha_modcmd(modcmd_t cmd, void *opaque)
99 1.1 riastrad {
100 1.1 riastrad
101 1.1 riastrad switch (cmd) {
102 1.1 riastrad case MODULE_CMD_INIT:
103 1.1 riastrad return chacha_select();
104 1.1 riastrad case MODULE_CMD_FINI:
105 1.1 riastrad return 0;
106 1.1 riastrad default:
107 1.1 riastrad return ENOTTY;
108 1.1 riastrad }
109 1.1 riastrad }
110 1.1 riastrad
111 1.1 riastrad static void
112 1.1 riastrad chacha_guarantee_selected(void)
113 1.1 riastrad {
114 1.1 riastrad #if 0
115 1.1 riastrad static once_t once;
116 1.1 riastrad int error;
117 1.1 riastrad
118 1.1 riastrad error = RUN_ONCE(&once, chacha_select);
119 1.1 riastrad KASSERT(error == 0);
120 1.1 riastrad #endif
121 1.1 riastrad }
122 1.1 riastrad
123 1.1 riastrad void
124 1.1 riastrad chacha_md_init(const struct chacha_impl *impl)
125 1.1 riastrad {
126 1.1 riastrad
127 1.1 riastrad KASSERT(cold);
128 1.1 riastrad KASSERTMSG(chacha_impl == NULL,
129 1.1 riastrad "ChaCha implementation `%s' already chosen, can't offer `%s'",
130 1.1 riastrad chacha_impl->ci_name, impl->ci_name);
131 1.1 riastrad KASSERTMSG(chacha_md_impl == NULL,
132 1.1 riastrad "ChaCha implementation `%s' already offered, can't offer `%s'",
133 1.1 riastrad chacha_md_impl->ci_name, impl->ci_name);
134 1.1 riastrad
135 1.1 riastrad chacha_md_impl = impl;
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad void
139 1.1 riastrad chacha_core(uint8_t out[restrict static CHACHA_CORE_OUTBYTES],
140 1.1 riastrad const uint8_t in[static CHACHA_CORE_INBYTES],
141 1.1 riastrad const uint8_t k[static CHACHA_CORE_KEYBYTES],
142 1.1 riastrad const uint8_t c[static CHACHA_CORE_CONSTBYTES],
143 1.1 riastrad unsigned nr)
144 1.1 riastrad {
145 1.1 riastrad
146 1.1 riastrad chacha_guarantee_selected();
147 1.1 riastrad (*chacha_impl->ci_chacha_core)(out, in, k, c, nr);
148 1.1 riastrad }
149 1.1 riastrad
150 1.1 riastrad void
151 1.1 riastrad hchacha(uint8_t out[restrict static HCHACHA_OUTBYTES],
152 1.1 riastrad const uint8_t in[static HCHACHA_INBYTES],
153 1.1 riastrad const uint8_t k[static HCHACHA_KEYBYTES],
154 1.1 riastrad const uint8_t c[static HCHACHA_CONSTBYTES],
155 1.1 riastrad unsigned nr)
156 1.1 riastrad {
157 1.1 riastrad
158 1.1 riastrad chacha_guarantee_selected();
159 1.1 riastrad (*chacha_impl->ci_hchacha)(out, in, k, c, nr);
160 1.1 riastrad }
161 1.1 riastrad
162 1.1 riastrad void
163 1.1 riastrad chacha_stream(uint8_t *restrict s, size_t nbytes, uint32_t blkno,
164 1.1 riastrad const uint8_t nonce[static CHACHA_STREAM_NONCEBYTES],
165 1.1 riastrad const uint8_t key[static CHACHA_STREAM_KEYBYTES],
166 1.1 riastrad unsigned nr)
167 1.1 riastrad {
168 1.1 riastrad
169 1.1 riastrad chacha_guarantee_selected();
170 1.1 riastrad (*chacha_impl->ci_chacha_stream)(s, nbytes, blkno, nonce, key, nr);
171 1.1 riastrad }
172 1.1 riastrad
173 1.1 riastrad void
174 1.1 riastrad chacha_stream_xor(uint8_t *c, const uint8_t *p, size_t nbytes, uint32_t blkno,
175 1.1 riastrad const uint8_t nonce[static CHACHA_STREAM_NONCEBYTES],
176 1.1 riastrad const uint8_t key[static CHACHA_STREAM_KEYBYTES],
177 1.1 riastrad unsigned nr)
178 1.1 riastrad {
179 1.1 riastrad
180 1.1 riastrad chacha_guarantee_selected();
181 1.1 riastrad (*chacha_impl->ci_chacha_stream_xor)(c, p, nbytes, blkno, nonce, key,
182 1.1 riastrad nr);
183 1.1 riastrad }
184 1.1 riastrad
185 1.1 riastrad void
186 1.1 riastrad xchacha_stream(uint8_t *restrict s, size_t nbytes, uint32_t blkno,
187 1.1 riastrad const uint8_t nonce[static XCHACHA_STREAM_NONCEBYTES],
188 1.1 riastrad const uint8_t key[static XCHACHA_STREAM_KEYBYTES],
189 1.1 riastrad unsigned nr)
190 1.1 riastrad {
191 1.1 riastrad
192 1.1 riastrad chacha_guarantee_selected();
193 1.1 riastrad (*chacha_impl->ci_xchacha_stream)(s, nbytes, blkno, nonce, key, nr);
194 1.1 riastrad }
195 1.1 riastrad
196 1.1 riastrad void
197 1.1 riastrad xchacha_stream_xor(uint8_t *c, const uint8_t *p, size_t nbytes, uint32_t blkno,
198 1.1 riastrad const uint8_t nonce[static XCHACHA_STREAM_NONCEBYTES],
199 1.1 riastrad const uint8_t key[static XCHACHA_STREAM_KEYBYTES],
200 1.1 riastrad unsigned nr)
201 1.1 riastrad {
202 1.1 riastrad
203 1.1 riastrad chacha_guarantee_selected();
204 1.1 riastrad (*chacha_impl->ci_xchacha_stream_xor)(c, p, nbytes, blkno, nonce, key,
205 1.1 riastrad nr);
206 1.1 riastrad }
207