xref: /openbsd/lib/libcrypto/chacha/chacha.c (revision 4bdff4be)
1 /* $OpenBSD: chacha.c,v 1.10 2023/07/05 16:17:20 beck Exp $ */
2 /*
3  * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and 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.
16  */
17 
18 #include <stdint.h>
19 
20 #include <openssl/chacha.h>
21 
22 #include "chacha-merged.c"
23 
24 void
25 ChaCha_set_key(ChaCha_ctx *ctx, const unsigned char *key, uint32_t keybits)
26 {
27 	chacha_keysetup((chacha_ctx *)ctx, key, keybits);
28 	ctx->unused = 0;
29 }
30 LCRYPTO_ALIAS(ChaCha_set_key);
31 
32 void
33 ChaCha_set_iv(ChaCha_ctx *ctx, const unsigned char *iv,
34     const unsigned char *counter)
35 {
36 	chacha_ivsetup((chacha_ctx *)ctx, iv, counter);
37 	ctx->unused = 0;
38 }
39 LCRYPTO_ALIAS(ChaCha_set_iv);
40 
41 void
42 ChaCha(ChaCha_ctx *ctx, unsigned char *out, const unsigned char *in, size_t len)
43 {
44 	unsigned char *k;
45 	uint64_t n;
46 	int i, l;
47 
48 	/* Consume remaining keystream, if any exists. */
49 	if (ctx->unused > 0) {
50 		k = ctx->ks + 64 - ctx->unused;
51 		l = (len > ctx->unused) ? ctx->unused : len;
52 		for (i = 0; i < l; i++)
53 			*(out++) = *(in++) ^ *(k++);
54 		ctx->unused -= l;
55 		len -= l;
56 	}
57 
58 	while (len > 0) {
59 		if ((n = len) > UINT32_MAX)
60 			n = UINT32_MAX;
61 
62 		chacha_encrypt_bytes((chacha_ctx *)ctx, in, out, (uint32_t)n);
63 
64 		in += n;
65 		out += n;
66 		len -= n;
67 	}
68 }
69 LCRYPTO_ALIAS(ChaCha);
70 
71 void
72 CRYPTO_chacha_20(unsigned char *out, const unsigned char *in, size_t len,
73     const unsigned char key[32], const unsigned char iv[8], uint64_t counter)
74 {
75 	struct chacha_ctx ctx;
76 	uint64_t n;
77 
78 	/*
79 	 * chacha_ivsetup expects the counter to be in u8. Rather than
80 	 * converting size_t to u8 and then back again, pass a counter of
81 	 * NULL and manually assign it afterwards.
82 	 */
83 	chacha_keysetup(&ctx, key, 256);
84 	chacha_ivsetup(&ctx, iv, NULL);
85 	if (counter != 0) {
86 		ctx.input[12] = (uint32_t)counter;
87 		ctx.input[13] = (uint32_t)(counter >> 32);
88 	}
89 
90 	while (len > 0) {
91 		if ((n = len) > UINT32_MAX)
92 			n = UINT32_MAX;
93 
94 		chacha_encrypt_bytes(&ctx, in, out, (uint32_t)n);
95 
96 		in += n;
97 		out += n;
98 		len -= n;
99 	}
100 }
101 LCRYPTO_ALIAS(CRYPTO_chacha_20);
102 
103 void
104 CRYPTO_xchacha_20(unsigned char *out, const unsigned char *in, size_t len,
105     const unsigned char key[32], const unsigned char iv[24])
106 {
107 	uint8_t subkey[32];
108 
109 	CRYPTO_hchacha_20(subkey, key, iv);
110 	CRYPTO_chacha_20(out, in, len, subkey, iv + 16, 0);
111 }
112 LCRYPTO_ALIAS(CRYPTO_xchacha_20);
113