xref: /freebsd/crypto/openssl/crypto/chacha/chacha_riscv.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * This file is dual-licensed, meaning that you can use it under your
3*e7be843bSPierre Pronchery  * choice of either of the following two licenses:
4*e7be843bSPierre Pronchery  *
5*e7be843bSPierre Pronchery  * Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
6*e7be843bSPierre Pronchery  *
7*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
8*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
9*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
10*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
11*e7be843bSPierre Pronchery  *
12*e7be843bSPierre Pronchery  * or
13*e7be843bSPierre Pronchery  *
14*e7be843bSPierre Pronchery  * Copyright (c) 2023, Jerry Shih <jerry.shih@sifive.com>
15*e7be843bSPierre Pronchery  * All rights reserved.
16*e7be843bSPierre Pronchery  *
17*e7be843bSPierre Pronchery  * Redistribution and use in source and binary forms, with or without
18*e7be843bSPierre Pronchery  * modification, are permitted provided that the following conditions
19*e7be843bSPierre Pronchery  * are met:
20*e7be843bSPierre Pronchery  * 1. Redistributions of source code must retain the above copyright
21*e7be843bSPierre Pronchery  *    notice, this list of conditions and the following disclaimer.
22*e7be843bSPierre Pronchery  * 2. Redistributions in binary form must reproduce the above copyright
23*e7be843bSPierre Pronchery  *    notice, this list of conditions and the following disclaimer in the
24*e7be843bSPierre Pronchery  *    documentation and/or other materials provided with the distribution.
25*e7be843bSPierre Pronchery  *
26*e7be843bSPierre Pronchery  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27*e7be843bSPierre Pronchery  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28*e7be843bSPierre Pronchery  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29*e7be843bSPierre Pronchery  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30*e7be843bSPierre Pronchery  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31*e7be843bSPierre Pronchery  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32*e7be843bSPierre Pronchery  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33*e7be843bSPierre Pronchery  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34*e7be843bSPierre Pronchery  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35*e7be843bSPierre Pronchery  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36*e7be843bSPierre Pronchery  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*e7be843bSPierre Pronchery  */
38*e7be843bSPierre Pronchery 
39*e7be843bSPierre Pronchery #include <openssl/opensslconf.h>
40*e7be843bSPierre Pronchery #include "crypto/chacha.h"
41*e7be843bSPierre Pronchery #include "crypto/riscv_arch.h"
42*e7be843bSPierre Pronchery 
43*e7be843bSPierre Pronchery void ChaCha20_ctr32_v_zbb_zvkb(unsigned char *out, const unsigned char *inp,
44*e7be843bSPierre Pronchery                                size_t len, const unsigned int key[8],
45*e7be843bSPierre Pronchery                                const unsigned int counter[4]);
46*e7be843bSPierre Pronchery 
47*e7be843bSPierre Pronchery void ChaCha20_ctr32_v_zbb(unsigned char *out, const unsigned char *inp,
48*e7be843bSPierre Pronchery                           size_t len, const unsigned int key[8],
49*e7be843bSPierre Pronchery                           const unsigned int counter[4]);
50*e7be843bSPierre Pronchery 
ChaCha20_ctr32(unsigned char * out,const unsigned char * inp,size_t len,const unsigned int key[8],const unsigned int counter[4])51*e7be843bSPierre Pronchery void ChaCha20_ctr32(unsigned char *out, const unsigned char *inp, size_t len,
52*e7be843bSPierre Pronchery                     const unsigned int key[8], const unsigned int counter[4])
53*e7be843bSPierre Pronchery {
54*e7be843bSPierre Pronchery     if (len > CHACHA_BLK_SIZE && RISCV_HAS_ZBB() && riscv_vlen() >= 128) {
55*e7be843bSPierre Pronchery         if (RISCV_HAS_ZVKB()) {
56*e7be843bSPierre Pronchery             ChaCha20_ctr32_v_zbb_zvkb(out, inp, len, key, counter);
57*e7be843bSPierre Pronchery         } else {
58*e7be843bSPierre Pronchery             ChaCha20_ctr32_v_zbb(out, inp, len, key, counter);
59*e7be843bSPierre Pronchery         }
60*e7be843bSPierre Pronchery     } else {
61*e7be843bSPierre Pronchery         ChaCha20_ctr32_c(out, inp, len, key, counter);
62*e7be843bSPierre Pronchery     }
63*e7be843bSPierre Pronchery }
64