/src/irssi/subprojects/openssl-1.1.1l/crypto/chacha/chacha_enc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* Adapted from the public domain code by D. Bernstein from SUPERCOP. */ |
11 | | |
12 | | #include <string.h> |
13 | | |
14 | | #include "crypto/chacha.h" |
15 | | #include "crypto/ctype.h" |
16 | | |
17 | | typedef unsigned int u32; |
18 | | typedef unsigned char u8; |
19 | | typedef union { |
20 | | u32 u[16]; |
21 | | u8 c[64]; |
22 | | } chacha_buf; |
23 | | |
24 | 0 | # define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n)))) |
25 | | |
26 | 0 | # define U32TO8_LITTLE(p, v) do { \ |
27 | 0 | (p)[0] = (u8)(v >> 0); \ |
28 | 0 | (p)[1] = (u8)(v >> 8); \ |
29 | 0 | (p)[2] = (u8)(v >> 16); \ |
30 | 0 | (p)[3] = (u8)(v >> 24); \ |
31 | 0 | } while(0) |
32 | | |
33 | | /* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */ |
34 | 0 | # define QUARTERROUND(a,b,c,d) ( \ |
35 | 0 | x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]),16), \ |
36 | 0 | x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]),12), \ |
37 | 0 | x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]), 8), \ |
38 | 0 | x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]), 7) ) |
39 | | |
40 | | /* chacha_core performs 20 rounds of ChaCha on the input words in |
41 | | * |input| and writes the 64 output bytes to |output|. */ |
42 | | static void chacha20_core(chacha_buf *output, const u32 input[16]) |
43 | 0 | { |
44 | 0 | u32 x[16]; |
45 | 0 | int i; |
46 | 0 | const union { |
47 | 0 | long one; |
48 | 0 | char little; |
49 | 0 | } is_endian = { 1 }; |
50 | |
|
51 | 0 | memcpy(x, input, sizeof(x)); |
52 | |
|
53 | 0 | for (i = 20; i > 0; i -= 2) { |
54 | 0 | QUARTERROUND(0, 4, 8, 12); |
55 | 0 | QUARTERROUND(1, 5, 9, 13); |
56 | 0 | QUARTERROUND(2, 6, 10, 14); |
57 | 0 | QUARTERROUND(3, 7, 11, 15); |
58 | 0 | QUARTERROUND(0, 5, 10, 15); |
59 | 0 | QUARTERROUND(1, 6, 11, 12); |
60 | 0 | QUARTERROUND(2, 7, 8, 13); |
61 | 0 | QUARTERROUND(3, 4, 9, 14); |
62 | 0 | } |
63 | |
|
64 | 0 | if (is_endian.little) { |
65 | 0 | for (i = 0; i < 16; ++i) |
66 | 0 | output->u[i] = x[i] + input[i]; |
67 | 0 | } else { |
68 | 0 | for (i = 0; i < 16; ++i) |
69 | 0 | U32TO8_LITTLE(output->c + 4 * i, (x[i] + input[i])); |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp, |
74 | | size_t len, const unsigned int key[8], |
75 | | const unsigned int counter[4]) |
76 | 0 | { |
77 | 0 | u32 input[16]; |
78 | 0 | chacha_buf buf; |
79 | 0 | size_t todo, i; |
80 | | |
81 | | /* sigma constant "expand 32-byte k" in little-endian encoding */ |
82 | 0 | input[0] = ((u32)ossl_toascii('e')) | ((u32)ossl_toascii('x') << 8) |
83 | 0 | | ((u32)ossl_toascii('p') << 16) |
84 | 0 | | ((u32)ossl_toascii('a') << 24); |
85 | 0 | input[1] = ((u32)ossl_toascii('n')) | ((u32)ossl_toascii('d') << 8) |
86 | 0 | | ((u32)ossl_toascii(' ') << 16) |
87 | 0 | | ((u32)ossl_toascii('3') << 24); |
88 | 0 | input[2] = ((u32)ossl_toascii('2')) | ((u32)ossl_toascii('-') << 8) |
89 | 0 | | ((u32)ossl_toascii('b') << 16) |
90 | 0 | | ((u32)ossl_toascii('y') << 24); |
91 | 0 | input[3] = ((u32)ossl_toascii('t')) | ((u32)ossl_toascii('e') << 8) |
92 | 0 | | ((u32)ossl_toascii(' ') << 16) |
93 | 0 | | ((u32)ossl_toascii('k') << 24); |
94 | |
|
95 | 0 | input[4] = key[0]; |
96 | 0 | input[5] = key[1]; |
97 | 0 | input[6] = key[2]; |
98 | 0 | input[7] = key[3]; |
99 | 0 | input[8] = key[4]; |
100 | 0 | input[9] = key[5]; |
101 | 0 | input[10] = key[6]; |
102 | 0 | input[11] = key[7]; |
103 | |
|
104 | 0 | input[12] = counter[0]; |
105 | 0 | input[13] = counter[1]; |
106 | 0 | input[14] = counter[2]; |
107 | 0 | input[15] = counter[3]; |
108 | |
|
109 | 0 | while (len > 0) { |
110 | 0 | todo = sizeof(buf); |
111 | 0 | if (len < todo) |
112 | 0 | todo = len; |
113 | |
|
114 | 0 | chacha20_core(&buf, input); |
115 | |
|
116 | 0 | for (i = 0; i < todo; i++) |
117 | 0 | out[i] = inp[i] ^ buf.c[i]; |
118 | 0 | out += todo; |
119 | 0 | inp += todo; |
120 | 0 | len -= todo; |
121 | | |
122 | | /* |
123 | | * Advance 32-bit counter. Note that as subroutine is so to |
124 | | * say nonce-agnostic, this limited counter width doesn't |
125 | | * prevent caller from implementing wider counter. It would |
126 | | * simply take two calls split on counter overflow... |
127 | | */ |
128 | 0 | input[12]++; |
129 | 0 | } |
130 | 0 | } |