sm3-riscv64-zvksh.pl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #! /usr/bin/env perl
  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 The OpenSSL Project Authors. All Rights Reserved.
  6. #
  7. # Licensed under the Apache License 2.0 (the "License"). You can obtain
  8. # a copy in the file LICENSE in the source distribution or at
  9. # https://www.openssl.org/source/license.html
  10. #
  11. # or
  12. #
  13. # Copyright (c) 2023, Christoph Müllner <christoph.muellner@vrull.eu>
  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. # The generated code of this file depends on the following RISC-V extensions:
  38. # - RV64I
  39. # - RISC-V Vector ('V') with VLEN >= 128
  40. # - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')
  41. # - RISC-V Vector SM3 Secure Hash extension ('Zvksh')
  42. use strict;
  43. use warnings;
  44. use FindBin qw($Bin);
  45. use lib "$Bin";
  46. use lib "$Bin/../../perlasm";
  47. use riscv;
  48. # $output is the last argument if it looks like a file (it has an extension)
  49. # $flavour is the first argument if it doesn't look like a file
  50. my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  51. my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  52. $output and open STDOUT,">$output";
  53. my $code=<<___;
  54. .text
  55. ___
  56. ################################################################################
  57. # ossl_hwsm3_block_data_order_zvksh(SM3_CTX *c, const void *p, size_t num);
  58. {
  59. my ($CTX, $INPUT, $NUM) = ("a0", "a1", "a2");
  60. my ($V0, $V1, $V2, $V3, $V4, $V5, $V6, $V7,
  61. $V8, $V9, $V10, $V11, $V12, $V13, $V14, $V15,
  62. $V16, $V17, $V18, $V19, $V20, $V21, $V22, $V23,
  63. $V24, $V25, $V26, $V27, $V28, $V29, $V30, $V31,
  64. ) = map("v$_",(0..31));
  65. $code .= <<___;
  66. .text
  67. .p2align 3
  68. .globl ossl_hwsm3_block_data_order_zvksh
  69. .type ossl_hwsm3_block_data_order_zvksh,\@function
  70. ossl_hwsm3_block_data_order_zvksh:
  71. @{[vsetivli "zero", 8, "e32", "m2", "ta", "ma"]}
  72. # Load initial state of hash context (c->A-H).
  73. @{[vle32_v $V0, $CTX]}
  74. @{[vrev8_v $V0, $V0]}
  75. L_sm3_loop:
  76. # Copy the previous state to v2.
  77. # It will be XOR'ed with the current state at the end of the round.
  78. @{[vmv_v_v $V2, $V0]}
  79. # Load the 64B block in 2x32B chunks.
  80. @{[vle32_v $V6, $INPUT]} # v6 := {w7, ..., w0}
  81. addi $INPUT, $INPUT, 32
  82. @{[vle32_v $V8, $INPUT]} # v8 := {w15, ..., w8}
  83. addi $INPUT, $INPUT, 32
  84. addi $NUM, $NUM, -1
  85. # As vsm3c consumes only w0, w1, w4, w5 we need to slide the input
  86. # 2 elements down so we process elements w2, w3, w6, w7
  87. # This will be repeated for each odd round.
  88. @{[vslidedown_vi $V4, $V6, 2]} # v4 := {X, X, w7, ..., w2}
  89. @{[vsm3c_vi $V0, $V6, 0]}
  90. @{[vsm3c_vi $V0, $V4, 1]}
  91. # Prepare a vector with {w11, ..., w4}
  92. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w7, ..., w4}
  93. @{[vslideup_vi $V4, $V8, 4]} # v4 := {w11, w10, w9, w8, w7, w6, w5, w4}
  94. @{[vsm3c_vi $V0, $V4, 2]}
  95. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w11, w10, w9, w8, w7, w6}
  96. @{[vsm3c_vi $V0, $V4, 3]}
  97. @{[vsm3c_vi $V0, $V8, 4]}
  98. @{[vslidedown_vi $V4, $V8, 2]} # v4 := {X, X, w15, w14, w13, w12, w11, w10}
  99. @{[vsm3c_vi $V0, $V4, 5]}
  100. @{[vsm3me_vv $V6, $V8, $V6]} # v6 := {w23, w22, w21, w20, w19, w18, w17, w16}
  101. # Prepare a register with {w19, w18, w17, w16, w15, w14, w13, w12}
  102. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w15, w14, w13, w12}
  103. @{[vslideup_vi $V4, $V6, 4]} # v4 := {w19, w18, w17, w16, w15, w14, w13, w12}
  104. @{[vsm3c_vi $V0, $V4, 6]}
  105. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w19, w18, w17, w16, w15, w14}
  106. @{[vsm3c_vi $V0, $V4, 7]}
  107. @{[vsm3c_vi $V0, $V6, 8]}
  108. @{[vslidedown_vi $V4, $V6, 2]} # v4 := {X, X, w23, w22, w21, w20, w19, w18}
  109. @{[vsm3c_vi $V0, $V4, 9]}
  110. @{[vsm3me_vv $V8, $V6, $V8]} # v8 := {w31, w30, w29, w28, w27, w26, w25, w24}
  111. # Prepare a register with {w27, w26, w25, w24, w23, w22, w21, w20}
  112. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w23, w22, w21, w20}
  113. @{[vslideup_vi $V4, $V8, 4]} # v4 := {w27, w26, w25, w24, w23, w22, w21, w20}
  114. @{[vsm3c_vi $V0, $V4, 10]}
  115. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w27, w26, w25, w24, w23, w22}
  116. @{[vsm3c_vi $V0, $V4, 11]}
  117. @{[vsm3c_vi $V0, $V8, 12]}
  118. @{[vslidedown_vi $V4, $V8, 2]} # v4 := {x, X, w31, w30, w29, w28, w27, w26}
  119. @{[vsm3c_vi $V0, $V4, 13]}
  120. @{[vsm3me_vv $V6, $V8, $V6]} # v6 := {w32, w33, w34, w35, w36, w37, w38, w39}
  121. # Prepare a register with {w35, w34, w33, w32, w31, w30, w29, w28}
  122. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w31, w30, w29, w28}
  123. @{[vslideup_vi $V4, $V6, 4]} # v4 := {w35, w34, w33, w32, w31, w30, w29, w28}
  124. @{[vsm3c_vi $V0, $V4, 14]}
  125. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w35, w34, w33, w32, w31, w30}
  126. @{[vsm3c_vi $V0, $V4, 15]}
  127. @{[vsm3c_vi $V0, $V6, 16]}
  128. @{[vslidedown_vi $V4, $V6, 2]} # v4 := {X, X, w39, w38, w37, w36, w35, w34}
  129. @{[vsm3c_vi $V0, $V4, 17]}
  130. @{[vsm3me_vv $V8, $V6, $V8]} # v8 := {w47, w46, w45, w44, w43, w42, w41, w40}
  131. # Prepare a register with {w43, w42, w41, w40, w39, w38, w37, w36}
  132. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w39, w38, w37, w36}
  133. @{[vslideup_vi $V4, $V8, 4]} # v4 := {w43, w42, w41, w40, w39, w38, w37, w36}
  134. @{[vsm3c_vi $V0, $V4, 18]}
  135. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w43, w42, w41, w40, w39, w38}
  136. @{[vsm3c_vi $V0, $V4, 19]}
  137. @{[vsm3c_vi $V0, $V8, 20]}
  138. @{[vslidedown_vi $V4, $V8, 2]} # v4 := {X, X, w47, w46, w45, w44, w43, w42}
  139. @{[vsm3c_vi $V0, $V4, 21]}
  140. @{[vsm3me_vv $V6, $V8, $V6]} # v6 := {w55, w54, w53, w52, w51, w50, w49, w48}
  141. # Prepare a register with {w51, w50, w49, w48, w47, w46, w45, w44}
  142. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w47, w46, w45, w44}
  143. @{[vslideup_vi $V4, $V6, 4]} # v4 := {w51, w50, w49, w48, w47, w46, w45, w44}
  144. @{[vsm3c_vi $V0, $V4, 22]}
  145. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w51, w50, w49, w48, w47, w46}
  146. @{[vsm3c_vi $V0, $V4, 23]}
  147. @{[vsm3c_vi $V0, $V6, 24]}
  148. @{[vslidedown_vi $V4, $V6, 2]} # v4 := {X, X, w55, w54, w53, w52, w51, w50}
  149. @{[vsm3c_vi $V0, $V4, 25]}
  150. @{[vsm3me_vv $V8, $V6, $V8]} # v8 := {w63, w62, w61, w60, w59, w58, w57, w56}
  151. # Prepare a register with {w59, w58, w57, w56, w55, w54, w53, w52}
  152. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w55, w54, w53, w52}
  153. @{[vslideup_vi $V4, $V8, 4]} # v4 := {w59, w58, w57, w56, w55, w54, w53, w52}
  154. @{[vsm3c_vi $V0, $V4, 26]}
  155. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w59, w58, w57, w56, w55, w54}
  156. @{[vsm3c_vi $V0, $V4, 27]}
  157. @{[vsm3c_vi $V0, $V8, 28]}
  158. @{[vslidedown_vi $V4, $V8, 2]} # v4 := {X, X, w63, w62, w61, w60, w59, w58}
  159. @{[vsm3c_vi $V0, $V4, 29]}
  160. @{[vsm3me_vv $V6, $V8, $V6]} # v6 := {w71, w70, w69, w68, w67, w66, w65, w64}
  161. # Prepare a register with {w67, w66, w65, w64, w63, w62, w61, w60}
  162. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, X, X, w63, w62, w61, w60}
  163. @{[vslideup_vi $V4, $V6, 4]} # v4 := {w67, w66, w65, w64, w63, w62, w61, w60}
  164. @{[vsm3c_vi $V0, $V4, 30]}
  165. @{[vslidedown_vi $V4, $V4, 2]} # v4 := {X, X, w67, w66, w65, w64, w63, w62}
  166. @{[vsm3c_vi $V0, $V4, 31]}
  167. # XOR in the previous state.
  168. @{[vxor_vv $V0, $V0, $V2]}
  169. bnez $NUM, L_sm3_loop # Check if there are any more block to process
  170. L_sm3_end:
  171. @{[vrev8_v $V0, $V0]}
  172. @{[vse32_v $V0, $CTX]}
  173. ret
  174. .size ossl_hwsm3_block_data_order_zvksh,.-ossl_hwsm3_block_data_order_zvksh
  175. ___
  176. }
  177. print $code;
  178. close STDOUT or die "error closing STDOUT: $!";