2
0

chacha_riscv.c 2.8 KB

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