VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/chacha/chacha_enc.c@ 102427

Last change on this file since 102427 was 101211, checked in by vboxsync, 17 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

File size: 4.7 KB
Line 
1/*
2 * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 "internal/endian.h"
15#include "crypto/chacha.h"
16#include "crypto/ctype.h"
17
18typedef unsigned int u32;
19typedef unsigned char u8;
20typedef union {
21 u32 u[16];
22 u8 c[64];
23} chacha_buf;
24
25# define ROTATE(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
26
27# ifndef PEDANTIC
28# if defined(__GNUC__) && __GNUC__>=2 && \
29 !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
30# if defined(__riscv_zbb) || defined(__riscv_zbkb)
31# if __riscv_xlen == 64
32# undef ROTATE
33# define ROTATE(x, n) ({ u32 ret; \
34 asm ("roriw %0, %1, %2" \
35 : "=r"(ret) \
36 : "r"(x), "i"(32 - (n))); ret;})
37# endif
38# if __riscv_xlen == 32
39# undef ROTATE
40# define ROTATE(x, n) ({ u32 ret; \
41 asm ("rori %0, %1, %2" \
42 : "=r"(ret) \
43 : "r"(x), "i"(32 - (n))); ret;})
44# endif
45# endif
46# endif
47# endif
48
49# define U32TO8_LITTLE(p, v) do { \
50 (p)[0] = (u8)(v >> 0); \
51 (p)[1] = (u8)(v >> 8); \
52 (p)[2] = (u8)(v >> 16); \
53 (p)[3] = (u8)(v >> 24); \
54 } while(0)
55
56/* QUARTERROUND updates a, b, c, d with a ChaCha "quarter" round. */
57# define QUARTERROUND(a,b,c,d) ( \
58 x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]),16), \
59 x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]),12), \
60 x[a] += x[b], x[d] = ROTATE((x[d] ^ x[a]), 8), \
61 x[c] += x[d], x[b] = ROTATE((x[b] ^ x[c]), 7) )
62
63/* chacha_core performs 20 rounds of ChaCha on the input words in
64 * |input| and writes the 64 output bytes to |output|. */
65static void chacha20_core(chacha_buf *output, const u32 input[16])
66{
67 u32 x[16];
68 int i;
69 DECLARE_IS_ENDIAN;
70
71 memcpy(x, input, sizeof(x));
72
73 for (i = 20; i > 0; i -= 2) {
74 QUARTERROUND(0, 4, 8, 12);
75 QUARTERROUND(1, 5, 9, 13);
76 QUARTERROUND(2, 6, 10, 14);
77 QUARTERROUND(3, 7, 11, 15);
78 QUARTERROUND(0, 5, 10, 15);
79 QUARTERROUND(1, 6, 11, 12);
80 QUARTERROUND(2, 7, 8, 13);
81 QUARTERROUND(3, 4, 9, 14);
82 }
83
84 if (IS_LITTLE_ENDIAN) {
85 for (i = 0; i < 16; ++i)
86 output->u[i] = x[i] + input[i];
87 } else {
88 for (i = 0; i < 16; ++i)
89 U32TO8_LITTLE(output->c + 4 * i, (x[i] + input[i]));
90 }
91}
92
93void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp,
94 size_t len, const unsigned int key[8],
95 const unsigned int counter[4])
96{
97 u32 input[16];
98 chacha_buf buf;
99 size_t todo, i;
100
101 /* sigma constant "expand 32-byte k" in little-endian encoding */
102 input[0] = ((u32)ossl_toascii('e')) | ((u32)ossl_toascii('x') << 8)
103 | ((u32)ossl_toascii('p') << 16)
104 | ((u32)ossl_toascii('a') << 24);
105 input[1] = ((u32)ossl_toascii('n')) | ((u32)ossl_toascii('d') << 8)
106 | ((u32)ossl_toascii(' ') << 16)
107 | ((u32)ossl_toascii('3') << 24);
108 input[2] = ((u32)ossl_toascii('2')) | ((u32)ossl_toascii('-') << 8)
109 | ((u32)ossl_toascii('b') << 16)
110 | ((u32)ossl_toascii('y') << 24);
111 input[3] = ((u32)ossl_toascii('t')) | ((u32)ossl_toascii('e') << 8)
112 | ((u32)ossl_toascii(' ') << 16)
113 | ((u32)ossl_toascii('k') << 24);
114
115 input[4] = key[0];
116 input[5] = key[1];
117 input[6] = key[2];
118 input[7] = key[3];
119 input[8] = key[4];
120 input[9] = key[5];
121 input[10] = key[6];
122 input[11] = key[7];
123
124 input[12] = counter[0];
125 input[13] = counter[1];
126 input[14] = counter[2];
127 input[15] = counter[3];
128
129 while (len > 0) {
130 todo = sizeof(buf);
131 if (len < todo)
132 todo = len;
133
134 chacha20_core(&buf, input);
135
136 for (i = 0; i < todo; i++)
137 out[i] = inp[i] ^ buf.c[i];
138 out += todo;
139 inp += todo;
140 len -= todo;
141
142 /*
143 * Advance 32-bit counter. Note that as subroutine is so to
144 * say nonce-agnostic, this limited counter width doesn't
145 * prevent caller from implementing wider counter. It would
146 * simply take two calls split on counter overflow...
147 */
148 input[12]++;
149 }
150}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette