aes-riscv64-zvbb-zvkg-zvkned.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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, Jerry Shih <jerry.shih@sifive.com>
  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. # - RV64I
  37. # - RISC-V Vector ('V') with VLEN >= 128
  38. # - RISC-V Vector Bit-manipulation extension ('Zvbb')
  39. # - RISC-V Vector GCM/GMAC extension ('Zvkg')
  40. # - RISC-V Vector AES block cipher extension ('Zvkned')
  41. # - RISC-V Zicclsm(Main memory supports misaligned loads/stores)
  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. ################################################################################
  58. # void rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt(const unsigned char *in,
  59. # unsigned char *out, size_t length,
  60. # const AES_KEY *key1,
  61. # const AES_KEY *key2,
  62. # const unsigned char iv[16])
  63. my ($INPUT, $OUTPUT, $LENGTH, $KEY1, $KEY2, $IV) = ("a0", "a1", "a2", "a3", "a4", "a5");
  64. my ($TAIL_LENGTH) = ("a6");
  65. my ($VL) = ("a7");
  66. my ($T0, $T1, $T2) = ("t0", "t1", "t2");
  67. my ($STORE_LEN32) = ("t3");
  68. my ($LEN32) = ("t4");
  69. my ($V0, $V1, $V2, $V3, $V4, $V5, $V6, $V7,
  70. $V8, $V9, $V10, $V11, $V12, $V13, $V14, $V15,
  71. $V16, $V17, $V18, $V19, $V20, $V21, $V22, $V23,
  72. $V24, $V25, $V26, $V27, $V28, $V29, $V30, $V31,
  73. ) = map("v$_",(0..31));
  74. sub compute_xts_iv0 {
  75. my $code=<<___;
  76. # Load number of rounds
  77. lwu $T0, 240($KEY2)
  78. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  79. @{[vle32_v $V28, $IV]}
  80. @{[vle32_v $V29, $KEY2]}
  81. @{[vaesz_vs $V28, $V29]}
  82. addi $T0, $T0, -1
  83. addi $KEY2, $KEY2, 16
  84. 1:
  85. @{[vle32_v $V29, $KEY2]}
  86. @{[vaesem_vs $V28, $V29]}
  87. addi $T0, $T0, -1
  88. addi $KEY2, $KEY2, 16
  89. bnez $T0, 1b
  90. @{[vle32_v $V29, $KEY2]}
  91. @{[vaesef_vs $V28, $V29]}
  92. ___
  93. return $code;
  94. }
  95. # prepare input data(v24), iv(v28), bit-reversed-iv(v16), bit-reversed-iv-multiplier(v20)
  96. sub init_first_round {
  97. my $code=<<___;
  98. # load input
  99. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  100. @{[vle32_v $V24, $INPUT]}
  101. li $T0, 5
  102. # We could simplify the initialization steps if we have `block<=1`.
  103. blt $LEN32, $T0, 1f
  104. # Note: We use `vgmul` for GF(2^128) multiplication. The `vgmul` uses
  105. # different order of coefficients. We should use`vbrev8` to reverse the
  106. # data when we use `vgmul`.
  107. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  108. @{[vbrev8_v $V0, $V28]}
  109. @{[vsetvli "zero", $LEN32, "e32", "m4", "ta", "ma"]}
  110. @{[vmv_v_i $V16, 0]}
  111. # v16: [r-IV0, r-IV0, ...]
  112. @{[vaesz_vs $V16, $V0]}
  113. # Prepare GF(2^128) multiplier [1, x, x^2, x^3, ...] in v8.
  114. slli $T0, $LEN32, 2
  115. @{[vsetvli "zero", $T0, "e32", "m1", "ta", "ma"]}
  116. # v2: [`1`, `1`, `1`, `1`, ...]
  117. @{[vmv_v_i $V2, 1]}
  118. # v3: [`0`, `1`, `2`, `3`, ...]
  119. @{[vid_v $V3]}
  120. @{[vsetvli "zero", $T0, "e64", "m2", "ta", "ma"]}
  121. # v4: [`1`, 0, `1`, 0, `1`, 0, `1`, 0, ...]
  122. @{[vzext_vf2 $V4, $V2]}
  123. # v6: [`0`, 0, `1`, 0, `2`, 0, `3`, 0, ...]
  124. @{[vzext_vf2 $V6, $V3]}
  125. slli $T0, $LEN32, 1
  126. @{[vsetvli "zero", $T0, "e32", "m2", "ta", "ma"]}
  127. # v8: [1<<0=1, 0, 0, 0, 1<<1=x, 0, 0, 0, 1<<2=x^2, 0, 0, 0, ...]
  128. @{[vwsll_vv $V8, $V4, $V6]}
  129. # Compute [r-IV0*1, r-IV0*x, r-IV0*x^2, r-IV0*x^3, ...] in v16
  130. @{[vsetvli "zero", $LEN32, "e32", "m4", "ta", "ma"]}
  131. @{[vbrev8_v $V8, $V8]}
  132. @{[vgmul_vv $V16, $V8]}
  133. # Compute [IV0*1, IV0*x, IV0*x^2, IV0*x^3, ...] in v28.
  134. # Reverse the bits order back.
  135. @{[vbrev8_v $V28, $V16]}
  136. # Prepare the x^n multiplier in v20. The `n` is the aes-xts block number
  137. # in a LMUL=4 register group.
  138. # n = ((VLEN*LMUL)/(32*4)) = ((VLEN*4)/(32*4))
  139. # = (VLEN/32)
  140. # We could use vsetvli with `e32, m1` to compute the `n` number.
  141. @{[vsetvli $T0, "zero", "e32", "m1", "ta", "ma"]}
  142. li $T1, 1
  143. sll $T0, $T1, $T0
  144. @{[vsetivli "zero", 2, "e64", "m1", "ta", "ma"]}
  145. @{[vmv_v_i $V0, 0]}
  146. @{[vsetivli "zero", 1, "e64", "m1", "tu", "ma"]}
  147. @{[vmv_v_x $V0, $T0]}
  148. @{[vsetivli "zero", 2, "e64", "m1", "ta", "ma"]}
  149. @{[vbrev8_v $V0, $V0]}
  150. @{[vsetvli "zero", $LEN32, "e32", "m4", "ta", "ma"]}
  151. @{[vmv_v_i $V20, 0]}
  152. @{[vaesz_vs $V20, $V0]}
  153. j 2f
  154. 1:
  155. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  156. @{[vbrev8_v $V16, $V28]}
  157. 2:
  158. ___
  159. return $code;
  160. }
  161. # prepare xts enc last block's input(v24) and iv(v28)
  162. sub handle_xts_enc_last_block {
  163. my $code=<<___;
  164. bnez $TAIL_LENGTH, 1f
  165. ret
  166. 1:
  167. # slidedown second to last block
  168. addi $VL, $VL, -4
  169. @{[vsetivli "zero", 4, "e32", "m4", "ta", "ma"]}
  170. # ciphertext
  171. @{[vslidedown_vx $V24, $V24, $VL]}
  172. # multiplier
  173. @{[vslidedown_vx $V16, $V16, $VL]}
  174. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  175. @{[vmv_v_v $V25, $V24]}
  176. # load last block into v24
  177. # note: We should load the last block before store the second to last block
  178. # for in-place operation.
  179. @{[vsetvli "zero", $TAIL_LENGTH, "e8", "m1", "tu", "ma"]}
  180. @{[vle8_v $V24, $INPUT]}
  181. # setup `x` multiplier with byte-reversed order
  182. # 0b00000010 => 0b01000000 (0x40)
  183. li $T0, 0x40
  184. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  185. @{[vmv_v_i $V28, 0]}
  186. @{[vsetivli "zero", 1, "e8", "m1", "tu", "ma"]}
  187. @{[vmv_v_x $V28, $T0]}
  188. # compute IV for last block
  189. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  190. @{[vgmul_vv $V16, $V28]}
  191. @{[vbrev8_v $V28, $V16]}
  192. # store second to last block
  193. @{[vsetvli "zero", $TAIL_LENGTH, "e8", "m1", "ta", "ma"]}
  194. @{[vse8_v $V25, $OUTPUT]}
  195. ___
  196. return $code;
  197. }
  198. # prepare xts dec second to last block's input(v24) and iv(v29) and
  199. # last block's and iv(v28)
  200. sub handle_xts_dec_last_block {
  201. my $code=<<___;
  202. bnez $TAIL_LENGTH, 1f
  203. ret
  204. 1:
  205. # load second to last block's ciphertext
  206. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  207. @{[vle32_v $V24, $INPUT]}
  208. addi $INPUT, $INPUT, 16
  209. # setup `x` multiplier with byte-reversed order
  210. # 0b00000010 => 0b01000000 (0x40)
  211. li $T0, 0x40
  212. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  213. @{[vmv_v_i $V20, 0]}
  214. @{[vsetivli "zero", 1, "e8", "m1", "tu", "ma"]}
  215. @{[vmv_v_x $V20, $T0]}
  216. beqz $LENGTH, 1f
  217. # slidedown third to last block
  218. addi $VL, $VL, -4
  219. @{[vsetivli "zero", 4, "e32", "m4", "ta", "ma"]}
  220. # multiplier
  221. @{[vslidedown_vx $V16, $V16, $VL]}
  222. # compute IV for last block
  223. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  224. @{[vgmul_vv $V16, $V20]}
  225. @{[vbrev8_v $V28, $V16]}
  226. # compute IV for second to last block
  227. @{[vgmul_vv $V16, $V20]}
  228. @{[vbrev8_v $V29, $V16]}
  229. j 2f
  230. 1:
  231. # compute IV for second to last block
  232. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  233. @{[vgmul_vv $V16, $V20]}
  234. @{[vbrev8_v $V29, $V16]}
  235. 2:
  236. ___
  237. return $code;
  238. }
  239. # Load all 11 round keys to v1-v11 registers.
  240. sub aes_128_load_key {
  241. my $code=<<___;
  242. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  243. @{[vle32_v $V1, $KEY1]}
  244. addi $KEY1, $KEY1, 16
  245. @{[vle32_v $V2, $KEY1]}
  246. addi $KEY1, $KEY1, 16
  247. @{[vle32_v $V3, $KEY1]}
  248. addi $KEY1, $KEY1, 16
  249. @{[vle32_v $V4, $KEY1]}
  250. addi $KEY1, $KEY1, 16
  251. @{[vle32_v $V5, $KEY1]}
  252. addi $KEY1, $KEY1, 16
  253. @{[vle32_v $V6, $KEY1]}
  254. addi $KEY1, $KEY1, 16
  255. @{[vle32_v $V7, $KEY1]}
  256. addi $KEY1, $KEY1, 16
  257. @{[vle32_v $V8, $KEY1]}
  258. addi $KEY1, $KEY1, 16
  259. @{[vle32_v $V9, $KEY1]}
  260. addi $KEY1, $KEY1, 16
  261. @{[vle32_v $V10, $KEY1]}
  262. addi $KEY1, $KEY1, 16
  263. @{[vle32_v $V11, $KEY1]}
  264. ___
  265. return $code;
  266. }
  267. # Load all 15 round keys to v1-v15 registers.
  268. sub aes_256_load_key {
  269. my $code=<<___;
  270. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  271. @{[vle32_v $V1, $KEY1]}
  272. addi $KEY1, $KEY1, 16
  273. @{[vle32_v $V2, $KEY1]}
  274. addi $KEY1, $KEY1, 16
  275. @{[vle32_v $V3, $KEY1]}
  276. addi $KEY1, $KEY1, 16
  277. @{[vle32_v $V4, $KEY1]}
  278. addi $KEY1, $KEY1, 16
  279. @{[vle32_v $V5, $KEY1]}
  280. addi $KEY1, $KEY1, 16
  281. @{[vle32_v $V6, $KEY1]}
  282. addi $KEY1, $KEY1, 16
  283. @{[vle32_v $V7, $KEY1]}
  284. addi $KEY1, $KEY1, 16
  285. @{[vle32_v $V8, $KEY1]}
  286. addi $KEY1, $KEY1, 16
  287. @{[vle32_v $V9, $KEY1]}
  288. addi $KEY1, $KEY1, 16
  289. @{[vle32_v $V10, $KEY1]}
  290. addi $KEY1, $KEY1, 16
  291. @{[vle32_v $V11, $KEY1]}
  292. addi $KEY1, $KEY1, 16
  293. @{[vle32_v $V12, $KEY1]}
  294. addi $KEY1, $KEY1, 16
  295. @{[vle32_v $V13, $KEY1]}
  296. addi $KEY1, $KEY1, 16
  297. @{[vle32_v $V14, $KEY1]}
  298. addi $KEY1, $KEY1, 16
  299. @{[vle32_v $V15, $KEY1]}
  300. ___
  301. return $code;
  302. }
  303. # aes-128 enc with round keys v1-v11
  304. sub aes_128_enc {
  305. my $code=<<___;
  306. @{[vaesz_vs $V24, $V1]}
  307. @{[vaesem_vs $V24, $V2]}
  308. @{[vaesem_vs $V24, $V3]}
  309. @{[vaesem_vs $V24, $V4]}
  310. @{[vaesem_vs $V24, $V5]}
  311. @{[vaesem_vs $V24, $V6]}
  312. @{[vaesem_vs $V24, $V7]}
  313. @{[vaesem_vs $V24, $V8]}
  314. @{[vaesem_vs $V24, $V9]}
  315. @{[vaesem_vs $V24, $V10]}
  316. @{[vaesef_vs $V24, $V11]}
  317. ___
  318. return $code;
  319. }
  320. # aes-128 dec with round keys v1-v11
  321. sub aes_128_dec {
  322. my $code=<<___;
  323. @{[vaesz_vs $V24, $V11]}
  324. @{[vaesdm_vs $V24, $V10]}
  325. @{[vaesdm_vs $V24, $V9]}
  326. @{[vaesdm_vs $V24, $V8]}
  327. @{[vaesdm_vs $V24, $V7]}
  328. @{[vaesdm_vs $V24, $V6]}
  329. @{[vaesdm_vs $V24, $V5]}
  330. @{[vaesdm_vs $V24, $V4]}
  331. @{[vaesdm_vs $V24, $V3]}
  332. @{[vaesdm_vs $V24, $V2]}
  333. @{[vaesdf_vs $V24, $V1]}
  334. ___
  335. return $code;
  336. }
  337. # aes-256 enc with round keys v1-v15
  338. sub aes_256_enc {
  339. my $code=<<___;
  340. @{[vaesz_vs $V24, $V1]}
  341. @{[vaesem_vs $V24, $V2]}
  342. @{[vaesem_vs $V24, $V3]}
  343. @{[vaesem_vs $V24, $V4]}
  344. @{[vaesem_vs $V24, $V5]}
  345. @{[vaesem_vs $V24, $V6]}
  346. @{[vaesem_vs $V24, $V7]}
  347. @{[vaesem_vs $V24, $V8]}
  348. @{[vaesem_vs $V24, $V9]}
  349. @{[vaesem_vs $V24, $V10]}
  350. @{[vaesem_vs $V24, $V11]}
  351. @{[vaesem_vs $V24, $V12]}
  352. @{[vaesem_vs $V24, $V13]}
  353. @{[vaesem_vs $V24, $V14]}
  354. @{[vaesef_vs $V24, $V15]}
  355. ___
  356. return $code;
  357. }
  358. # aes-256 dec with round keys v1-v15
  359. sub aes_256_dec {
  360. my $code=<<___;
  361. @{[vaesz_vs $V24, $V15]}
  362. @{[vaesdm_vs $V24, $V14]}
  363. @{[vaesdm_vs $V24, $V13]}
  364. @{[vaesdm_vs $V24, $V12]}
  365. @{[vaesdm_vs $V24, $V11]}
  366. @{[vaesdm_vs $V24, $V10]}
  367. @{[vaesdm_vs $V24, $V9]}
  368. @{[vaesdm_vs $V24, $V8]}
  369. @{[vaesdm_vs $V24, $V7]}
  370. @{[vaesdm_vs $V24, $V6]}
  371. @{[vaesdm_vs $V24, $V5]}
  372. @{[vaesdm_vs $V24, $V4]}
  373. @{[vaesdm_vs $V24, $V3]}
  374. @{[vaesdm_vs $V24, $V2]}
  375. @{[vaesdf_vs $V24, $V1]}
  376. ___
  377. return $code;
  378. }
  379. $code .= <<___;
  380. .p2align 3
  381. .globl rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt
  382. .type rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt,\@function
  383. rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt:
  384. @{[compute_xts_iv0]}
  385. # aes block size is 16
  386. andi $TAIL_LENGTH, $LENGTH, 15
  387. mv $STORE_LEN32, $LENGTH
  388. beqz $TAIL_LENGTH, 1f
  389. sub $LENGTH, $LENGTH, $TAIL_LENGTH
  390. addi $STORE_LEN32, $LENGTH, -16
  391. 1:
  392. # We make the `LENGTH` become e32 length here.
  393. srli $LEN32, $LENGTH, 2
  394. srli $STORE_LEN32, $STORE_LEN32, 2
  395. # Load number of rounds
  396. lwu $T0, 240($KEY1)
  397. li $T1, 14
  398. li $T2, 10
  399. beq $T0, $T1, aes_xts_enc_256
  400. beq $T0, $T2, aes_xts_enc_128
  401. .size rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt,.-rv64i_zvbb_zvkg_zvkned_aes_xts_encrypt
  402. ___
  403. $code .= <<___;
  404. .p2align 3
  405. aes_xts_enc_128:
  406. @{[init_first_round]}
  407. @{[aes_128_load_key]}
  408. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  409. j 1f
  410. .Lenc_blocks_128:
  411. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  412. # load plaintext into v24
  413. @{[vle32_v $V24, $INPUT]}
  414. # update iv
  415. @{[vgmul_vv $V16, $V20]}
  416. # reverse the iv's bits order back
  417. @{[vbrev8_v $V28, $V16]}
  418. 1:
  419. @{[vxor_vv $V24, $V24, $V28]}
  420. slli $T0, $VL, 2
  421. sub $LEN32, $LEN32, $VL
  422. add $INPUT, $INPUT, $T0
  423. @{[aes_128_enc]}
  424. @{[vxor_vv $V24, $V24, $V28]}
  425. # store ciphertext
  426. @{[vsetvli "zero", $STORE_LEN32, "e32", "m4", "ta", "ma"]}
  427. @{[vse32_v $V24, $OUTPUT]}
  428. add $OUTPUT, $OUTPUT, $T0
  429. sub $STORE_LEN32, $STORE_LEN32, $VL
  430. bnez $LEN32, .Lenc_blocks_128
  431. @{[handle_xts_enc_last_block]}
  432. # xts last block
  433. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  434. @{[vxor_vv $V24, $V24, $V28]}
  435. @{[aes_128_enc]}
  436. @{[vxor_vv $V24, $V24, $V28]}
  437. # store last block ciphertext
  438. addi $OUTPUT, $OUTPUT, -16
  439. @{[vse32_v $V24, $OUTPUT]}
  440. ret
  441. .size aes_xts_enc_128,.-aes_xts_enc_128
  442. ___
  443. $code .= <<___;
  444. .p2align 3
  445. aes_xts_enc_256:
  446. @{[init_first_round]}
  447. @{[aes_256_load_key]}
  448. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  449. j 1f
  450. .Lenc_blocks_256:
  451. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  452. # load plaintext into v24
  453. @{[vle32_v $V24, $INPUT]}
  454. # update iv
  455. @{[vgmul_vv $V16, $V20]}
  456. # reverse the iv's bits order back
  457. @{[vbrev8_v $V28, $V16]}
  458. 1:
  459. @{[vxor_vv $V24, $V24, $V28]}
  460. slli $T0, $VL, 2
  461. sub $LEN32, $LEN32, $VL
  462. add $INPUT, $INPUT, $T0
  463. @{[aes_256_enc]}
  464. @{[vxor_vv $V24, $V24, $V28]}
  465. # store ciphertext
  466. @{[vsetvli "zero", $STORE_LEN32, "e32", "m4", "ta", "ma"]}
  467. @{[vse32_v $V24, $OUTPUT]}
  468. add $OUTPUT, $OUTPUT, $T0
  469. sub $STORE_LEN32, $STORE_LEN32, $VL
  470. bnez $LEN32, .Lenc_blocks_256
  471. @{[handle_xts_enc_last_block]}
  472. # xts last block
  473. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  474. @{[vxor_vv $V24, $V24, $V28]}
  475. @{[aes_256_enc]}
  476. @{[vxor_vv $V24, $V24, $V28]}
  477. # store last block ciphertext
  478. addi $OUTPUT, $OUTPUT, -16
  479. @{[vse32_v $V24, $OUTPUT]}
  480. ret
  481. .size aes_xts_enc_256,.-aes_xts_enc_256
  482. ___
  483. ################################################################################
  484. # void rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt(const unsigned char *in,
  485. # unsigned char *out, size_t length,
  486. # const AES_KEY *key1,
  487. # const AES_KEY *key2,
  488. # const unsigned char iv[16])
  489. $code .= <<___;
  490. .p2align 3
  491. .globl rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt
  492. .type rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt,\@function
  493. rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt:
  494. @{[compute_xts_iv0]}
  495. # aes block size is 16
  496. andi $TAIL_LENGTH, $LENGTH, 15
  497. beqz $TAIL_LENGTH, 1f
  498. sub $LENGTH, $LENGTH, $TAIL_LENGTH
  499. addi $LENGTH, $LENGTH, -16
  500. 1:
  501. # We make the `LENGTH` become e32 length here.
  502. srli $LEN32, $LENGTH, 2
  503. # Load number of rounds
  504. lwu $T0, 240($KEY1)
  505. li $T1, 14
  506. li $T2, 10
  507. beq $T0, $T1, aes_xts_dec_256
  508. beq $T0, $T2, aes_xts_dec_128
  509. .size rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt,.-rv64i_zvbb_zvkg_zvkned_aes_xts_decrypt
  510. ___
  511. $code .= <<___;
  512. .p2align 3
  513. aes_xts_dec_128:
  514. @{[init_first_round]}
  515. @{[aes_128_load_key]}
  516. beqz $LEN32, 2f
  517. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  518. j 1f
  519. .Ldec_blocks_128:
  520. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  521. # load ciphertext into v24
  522. @{[vle32_v $V24, $INPUT]}
  523. # update iv
  524. @{[vgmul_vv $V16, $V20]}
  525. # reverse the iv's bits order back
  526. @{[vbrev8_v $V28, $V16]}
  527. 1:
  528. @{[vxor_vv $V24, $V24, $V28]}
  529. slli $T0, $VL, 2
  530. sub $LEN32, $LEN32, $VL
  531. add $INPUT, $INPUT, $T0
  532. @{[aes_128_dec]}
  533. @{[vxor_vv $V24, $V24, $V28]}
  534. # store plaintext
  535. @{[vse32_v $V24, $OUTPUT]}
  536. add $OUTPUT, $OUTPUT, $T0
  537. bnez $LEN32, .Ldec_blocks_128
  538. 2:
  539. @{[handle_xts_dec_last_block]}
  540. ## xts second to last block
  541. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  542. @{[vxor_vv $V24, $V24, $V29]}
  543. @{[aes_128_dec]}
  544. @{[vxor_vv $V24, $V24, $V29]}
  545. @{[vmv_v_v $V25, $V24]}
  546. # load last block ciphertext
  547. @{[vsetvli "zero", $TAIL_LENGTH, "e8", "m1", "tu", "ma"]}
  548. @{[vle8_v $V24, $INPUT]}
  549. # store second to last block plaintext
  550. addi $T0, $OUTPUT, 16
  551. @{[vse8_v $V25, $T0]}
  552. ## xts last block
  553. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  554. @{[vxor_vv $V24, $V24, $V28]}
  555. @{[aes_128_dec]}
  556. @{[vxor_vv $V24, $V24, $V28]}
  557. # store second to last block plaintext
  558. @{[vse32_v $V24, $OUTPUT]}
  559. ret
  560. .size aes_xts_dec_128,.-aes_xts_dec_128
  561. ___
  562. $code .= <<___;
  563. .p2align 3
  564. aes_xts_dec_256:
  565. @{[init_first_round]}
  566. @{[aes_256_load_key]}
  567. beqz $LEN32, 2f
  568. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  569. j 1f
  570. .Ldec_blocks_256:
  571. @{[vsetvli $VL, $LEN32, "e32", "m4", "ta", "ma"]}
  572. # load ciphertext into v24
  573. @{[vle32_v $V24, $INPUT]}
  574. # update iv
  575. @{[vgmul_vv $V16, $V20]}
  576. # reverse the iv's bits order back
  577. @{[vbrev8_v $V28, $V16]}
  578. 1:
  579. @{[vxor_vv $V24, $V24, $V28]}
  580. slli $T0, $VL, 2
  581. sub $LEN32, $LEN32, $VL
  582. add $INPUT, $INPUT, $T0
  583. @{[aes_256_dec]}
  584. @{[vxor_vv $V24, $V24, $V28]}
  585. # store plaintext
  586. @{[vse32_v $V24, $OUTPUT]}
  587. add $OUTPUT, $OUTPUT, $T0
  588. bnez $LEN32, .Ldec_blocks_256
  589. 2:
  590. @{[handle_xts_dec_last_block]}
  591. ## xts second to last block
  592. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  593. @{[vxor_vv $V24, $V24, $V29]}
  594. @{[aes_256_dec]}
  595. @{[vxor_vv $V24, $V24, $V29]}
  596. @{[vmv_v_v $V25, $V24]}
  597. # load last block ciphertext
  598. @{[vsetvli "zero", $TAIL_LENGTH, "e8", "m1", "tu", "ma"]}
  599. @{[vle8_v $V24, $INPUT]}
  600. # store second to last block plaintext
  601. addi $T0, $OUTPUT, 16
  602. @{[vse8_v $V25, $T0]}
  603. ## xts last block
  604. @{[vsetivli "zero", 4, "e32", "m1", "ta", "ma"]}
  605. @{[vxor_vv $V24, $V24, $V28]}
  606. @{[aes_256_dec]}
  607. @{[vxor_vv $V24, $V24, $V28]}
  608. # store second to last block plaintext
  609. @{[vse32_v $V24, $OUTPUT]}
  610. ret
  611. .size aes_xts_dec_256,.-aes_xts_dec_256
  612. ___
  613. }
  614. print $code;
  615. close STDOUT or die "error closing STDOUT: $!";