2
0

sm4-riscv64-zvksed.pl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. # All rights reserved.
  15. #
  16. # Redistribution and use in source and binary forms, with or without
  17. # modification, are permitted provided that the following conditions
  18. # are met:
  19. # 1. Redistributions of source code must retain the above copyright
  20. # notice, this list of conditions and the following disclaimer.
  21. # 2. Redistributions in binary form must reproduce the above copyright
  22. # notice, this list of conditions and the following disclaimer in the
  23. # documentation and/or other materials provided with the distribution.
  24. #
  25. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. # The generated code of this file depends on the following RISC-V extensions:
  37. # - RV64I
  38. # - RISC-V Vector ('V') with VLEN >= 128
  39. # - RISC-V Vector Cryptography Bit-manipulation extension ('Zvkb')
  40. # - RISC-V Vector SM4 Block Cipher extension ('Zvksed')
  41. use strict;
  42. use warnings;
  43. use FindBin qw($Bin);
  44. use lib "$Bin";
  45. use lib "$Bin/../../perlasm";
  46. use riscv;
  47. # $output is the last argument if it looks like a file (it has an extension)
  48. # $flavour is the first argument if it doesn't look like a file
  49. my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  50. my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  51. $output and open STDOUT,">$output";
  52. my $code=<<___;
  53. .text
  54. ___
  55. ####
  56. # int rv64i_zvksed_sm4_set_encrypt_key(const unsigned char *userKey,
  57. # SM4_KEY *key);
  58. #
  59. {
  60. my ($ukey,$keys,$fk)=("a0","a1","t0");
  61. my ($vukey,$vfk,$vk0,$vk1,$vk2,$vk3,$vk4,$vk5,$vk6,$vk7)=("v1","v2","v3","v4","v5","v6","v7","v8","v9","v10");
  62. $code .= <<___;
  63. .p2align 3
  64. .globl rv64i_zvksed_sm4_set_encrypt_key
  65. .type rv64i_zvksed_sm4_set_encrypt_key,\@function
  66. rv64i_zvksed_sm4_set_encrypt_key:
  67. @{[vsetivli__x0_4_e32_m1_tu_mu]}
  68. # Load the user key
  69. @{[vle32_v $vukey, $ukey]}
  70. @{[vrev8_v $vukey, $vukey]}
  71. # Load the FK.
  72. la $fk, FK
  73. @{[vle32_v $vfk, $fk]}
  74. # Generate round keys.
  75. @{[vxor_vv $vukey, $vukey, $vfk]}
  76. @{[vsm4k_vi $vk0, $vukey, 0]} # rk[0:3]
  77. @{[vsm4k_vi $vk1, $vk0, 1]} # rk[4:7]
  78. @{[vsm4k_vi $vk2, $vk1, 2]} # rk[8:11]
  79. @{[vsm4k_vi $vk3, $vk2, 3]} # rk[12:15]
  80. @{[vsm4k_vi $vk4, $vk3, 4]} # rk[16:19]
  81. @{[vsm4k_vi $vk5, $vk4, 5]} # rk[20:23]
  82. @{[vsm4k_vi $vk6, $vk5, 6]} # rk[24:27]
  83. @{[vsm4k_vi $vk7, $vk6, 7]} # rk[28:31]
  84. # Store round keys
  85. @{[vse32_v $vk0, $keys]} # rk[0:3]
  86. addi $keys, $keys, 16
  87. @{[vse32_v $vk1, $keys]} # rk[4:7]
  88. addi $keys, $keys, 16
  89. @{[vse32_v $vk2, $keys]} # rk[8:11]
  90. addi $keys, $keys, 16
  91. @{[vse32_v $vk3, $keys]} # rk[12:15]
  92. addi $keys, $keys, 16
  93. @{[vse32_v $vk4, $keys]} # rk[16:19]
  94. addi $keys, $keys, 16
  95. @{[vse32_v $vk5, $keys]} # rk[20:23]
  96. addi $keys, $keys, 16
  97. @{[vse32_v $vk6, $keys]} # rk[24:27]
  98. addi $keys, $keys, 16
  99. @{[vse32_v $vk7, $keys]} # rk[28:31]
  100. li a0, 1
  101. ret
  102. .size rv64i_zvksed_sm4_set_encrypt_key,.-rv64i_zvksed_sm4_set_encrypt_key
  103. ___
  104. }
  105. ####
  106. # int rv64i_zvksed_sm4_set_decrypt_key(const unsigned char *userKey,
  107. # SM4_KEY *key);
  108. #
  109. {
  110. my ($ukey,$keys,$fk,$stride)=("a0","a1","t0","t1");
  111. my ($vukey,$vfk,$vk0,$vk1,$vk2,$vk3,$vk4,$vk5,$vk6,$vk7)=("v1","v2","v3","v4","v5","v6","v7","v8","v9","v10");
  112. $code .= <<___;
  113. .p2align 3
  114. .globl rv64i_zvksed_sm4_set_decrypt_key
  115. .type rv64i_zvksed_sm4_set_decrypt_key,\@function
  116. rv64i_zvksed_sm4_set_decrypt_key:
  117. @{[vsetivli__x0_4_e32_m1_tu_mu]}
  118. # Load the user key
  119. @{[vle32_v $vukey, $ukey]}
  120. @{[vrev8_v $vukey, $vukey]}
  121. # Load the FK.
  122. la $fk, FK
  123. @{[vle32_v $vfk, $fk]}
  124. # Generate round keys.
  125. @{[vxor_vv $vukey, $vukey, $vfk]}
  126. @{[vsm4k_vi $vk0, $vukey, 0]} # rk[0:3]
  127. @{[vsm4k_vi $vk1, $vk0, 1]} # rk[4:7]
  128. @{[vsm4k_vi $vk2, $vk1, 2]} # rk[8:11]
  129. @{[vsm4k_vi $vk3, $vk2, 3]} # rk[12:15]
  130. @{[vsm4k_vi $vk4, $vk3, 4]} # rk[16:19]
  131. @{[vsm4k_vi $vk5, $vk4, 5]} # rk[20:23]
  132. @{[vsm4k_vi $vk6, $vk5, 6]} # rk[24:27]
  133. @{[vsm4k_vi $vk7, $vk6, 7]} # rk[28:31]
  134. # Store round keys in reverse order
  135. addi $keys, $keys, 12
  136. li $stride, -4
  137. @{[vsse32_v $vk7, $keys, $stride]} # rk[31:28]
  138. addi $keys, $keys, 16
  139. @{[vsse32_v $vk6, $keys, $stride]} # rk[27:24]
  140. addi $keys, $keys, 16
  141. @{[vsse32_v $vk5, $keys, $stride]} # rk[23:20]
  142. addi $keys, $keys, 16
  143. @{[vsse32_v $vk4, $keys, $stride]} # rk[19:16]
  144. addi $keys, $keys, 16
  145. @{[vsse32_v $vk3, $keys, $stride]} # rk[15:12]
  146. addi $keys, $keys, 16
  147. @{[vsse32_v $vk2, $keys, $stride]} # rk[11:8]
  148. addi $keys, $keys, 16
  149. @{[vsse32_v $vk1, $keys, $stride]} # rk[7:4]
  150. addi $keys, $keys, 16
  151. @{[vsse32_v $vk0, $keys, $stride]} # rk[3:0]
  152. li a0, 1
  153. ret
  154. .size rv64i_zvksed_sm4_set_decrypt_key,.-rv64i_zvksed_sm4_set_decrypt_key
  155. ___
  156. }
  157. ####
  158. # void rv64i_zvksed_sm4_encrypt(const unsigned char *in, unsigned char *out,
  159. # const SM4_KEY *key);
  160. #
  161. {
  162. my ($in,$out,$keys,$stride)=("a0","a1","a2","t0");
  163. my ($vdata,$vk0,$vk1,$vk2,$vk3,$vk4,$vk5,$vk6,$vk7,$vgen)=("v1","v2","v3","v4","v5","v6","v7","v8","v9","v10");
  164. $code .= <<___;
  165. .p2align 3
  166. .globl rv64i_zvksed_sm4_encrypt
  167. .type rv64i_zvksed_sm4_encrypt,\@function
  168. rv64i_zvksed_sm4_encrypt:
  169. @{[vsetivli__x0_4_e32_m1_tu_mu]}
  170. # Order of elements was adjusted in set_encrypt_key()
  171. @{[vle32_v $vk0, $keys]} # rk[0:3]
  172. addi $keys, $keys, 16
  173. @{[vle32_v $vk1, $keys]} # rk[4:7]
  174. addi $keys, $keys, 16
  175. @{[vle32_v $vk2, $keys]} # rk[8:11]
  176. addi $keys, $keys, 16
  177. @{[vle32_v $vk3, $keys]} # rk[12:15]
  178. addi $keys, $keys, 16
  179. @{[vle32_v $vk4, $keys]} # rk[16:19]
  180. addi $keys, $keys, 16
  181. @{[vle32_v $vk5, $keys]} # rk[20:23]
  182. addi $keys, $keys, 16
  183. @{[vle32_v $vk6, $keys]} # rk[24:27]
  184. addi $keys, $keys, 16
  185. @{[vle32_v $vk7, $keys]} # rk[28:31]
  186. # Load input data
  187. @{[vle32_v $vdata, $in]}
  188. @{[vrev8_v $vdata, $vdata]}
  189. # Encrypt with all keys
  190. @{[vsm4r_vs $vdata, $vk0]}
  191. @{[vsm4r_vs $vdata, $vk1]}
  192. @{[vsm4r_vs $vdata, $vk2]}
  193. @{[vsm4r_vs $vdata, $vk3]}
  194. @{[vsm4r_vs $vdata, $vk4]}
  195. @{[vsm4r_vs $vdata, $vk5]}
  196. @{[vsm4r_vs $vdata, $vk6]}
  197. @{[vsm4r_vs $vdata, $vk7]}
  198. # Save the ciphertext (in reverse element order)
  199. @{[vrev8_v $vdata, $vdata]}
  200. li $stride, -4
  201. addi $out, $out, 12
  202. @{[vsse32_v $vdata, $out, $stride]}
  203. ret
  204. .size rv64i_zvksed_sm4_encrypt,.-rv64i_zvksed_sm4_encrypt
  205. ___
  206. }
  207. ####
  208. # void rv64i_zvksed_sm4_decrypt(const unsigned char *in, unsigned char *out,
  209. # const SM4_KEY *key);
  210. #
  211. {
  212. my ($in,$out,$keys,$stride)=("a0","a1","a2","t0");
  213. my ($vdata,$vk0,$vk1,$vk2,$vk3,$vk4,$vk5,$vk6,$vk7,$vgen)=("v1","v2","v3","v4","v5","v6","v7","v8","v9","v10");
  214. $code .= <<___;
  215. .p2align 3
  216. .globl rv64i_zvksed_sm4_decrypt
  217. .type rv64i_zvksed_sm4_decrypt,\@function
  218. rv64i_zvksed_sm4_decrypt:
  219. @{[vsetivli__x0_4_e32_m1_tu_mu]}
  220. # Order of elements was adjusted in set_decrypt_key()
  221. @{[vle32_v $vk7, $keys]} # rk[31:28]
  222. addi $keys, $keys, 16
  223. @{[vle32_v $vk6, $keys]} # rk[27:24]
  224. addi $keys, $keys, 16
  225. @{[vle32_v $vk5, $keys]} # rk[23:20]
  226. addi $keys, $keys, 16
  227. @{[vle32_v $vk4, $keys]} # rk[19:16]
  228. addi $keys, $keys, 16
  229. @{[vle32_v $vk3, $keys]} # rk[15:11]
  230. addi $keys, $keys, 16
  231. @{[vle32_v $vk2, $keys]} # rk[11:8]
  232. addi $keys, $keys, 16
  233. @{[vle32_v $vk1, $keys]} # rk[7:4]
  234. addi $keys, $keys, 16
  235. @{[vle32_v $vk0, $keys]} # rk[3:0]
  236. # Load input data
  237. @{[vle32_v $vdata, $in]}
  238. @{[vrev8_v $vdata, $vdata]}
  239. # Encrypt with all keys
  240. @{[vsm4r_vs $vdata, $vk7]}
  241. @{[vsm4r_vs $vdata, $vk6]}
  242. @{[vsm4r_vs $vdata, $vk5]}
  243. @{[vsm4r_vs $vdata, $vk4]}
  244. @{[vsm4r_vs $vdata, $vk3]}
  245. @{[vsm4r_vs $vdata, $vk2]}
  246. @{[vsm4r_vs $vdata, $vk1]}
  247. @{[vsm4r_vs $vdata, $vk0]}
  248. # Save the ciphertext (in reverse element order)
  249. @{[vrev8_v $vdata, $vdata]}
  250. li $stride, -4
  251. addi $out, $out, 12
  252. @{[vsse32_v $vdata, $out, $stride]}
  253. ret
  254. .size rv64i_zvksed_sm4_decrypt,.-rv64i_zvksed_sm4_decrypt
  255. ___
  256. }
  257. $code .= <<___;
  258. # Family Key (little-endian 32-bit chunks)
  259. .p2align 3
  260. FK:
  261. .word 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC
  262. .size FK,.-FK
  263. ___
  264. print $code;
  265. close STDOUT or die "error closing STDOUT: $!";