Skip to content

Commit 5e685f6

Browse files
author
Anjian Wen
committed
8374351: RISC-V: Small refactoring for crypto macro-assembler routines
Reviewed-by: fyang, fjiang
1 parent 9512a43 commit 5e685f6

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

src/hotspot/cpu/riscv/stubGenerator_riscv.cpp

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,7 +2436,7 @@ class StubGenerator: public StubCodeGenerator {
24362436
StubRoutines::_unsafe_setmemory = generate_unsafe_setmemory();
24372437
}
24382438

2439-
void generate_aes_loadkeys(const Register &key, VectorRegister *working_vregs, int rounds) {
2439+
void aes_load_keys(const Register &key, VectorRegister *working_vregs, int rounds) {
24402440
const int step = 16;
24412441
for (int i = 0; i < rounds; i++) {
24422442
__ vle32_v(working_vregs[i], key);
@@ -2448,7 +2448,7 @@ class StubGenerator: public StubCodeGenerator {
24482448
}
24492449
}
24502450

2451-
void generate_aes_encrypt(const VectorRegister &res, VectorRegister *working_vregs, int rounds) {
2451+
void aes_encrypt(const VectorRegister &res, VectorRegister *working_vregs, int rounds) {
24522452
assert(rounds <= 15, "rounds should be less than or equal to working_vregs size");
24532453

24542454
__ vxor_vv(res, res, working_vregs[0]);
@@ -2499,26 +2499,26 @@ class StubGenerator: public StubCodeGenerator {
24992499
// Else we fallthrough to the biggest case (256-bit key size)
25002500

25012501
// Note: the following function performs key += 15*16
2502-
generate_aes_loadkeys(key, working_vregs, 15);
2503-
generate_aes_encrypt(res, working_vregs, 15);
2502+
aes_load_keys(key, working_vregs, 15);
2503+
aes_encrypt(res, working_vregs, 15);
25042504
__ vse32_v(res, to);
25052505
__ mv(c_rarg0, 0);
25062506
__ leave();
25072507
__ ret();
25082508

25092509
__ bind(L_aes192);
25102510
// Note: the following function performs key += 13*16
2511-
generate_aes_loadkeys(key, working_vregs, 13);
2512-
generate_aes_encrypt(res, working_vregs, 13);
2511+
aes_load_keys(key, working_vregs, 13);
2512+
aes_encrypt(res, working_vregs, 13);
25132513
__ vse32_v(res, to);
25142514
__ mv(c_rarg0, 0);
25152515
__ leave();
25162516
__ ret();
25172517

25182518
__ bind(L_aes128);
25192519
// Note: the following function performs key += 11*16
2520-
generate_aes_loadkeys(key, working_vregs, 11);
2521-
generate_aes_encrypt(res, working_vregs, 11);
2520+
aes_load_keys(key, working_vregs, 11);
2521+
aes_encrypt(res, working_vregs, 11);
25222522
__ vse32_v(res, to);
25232523
__ mv(c_rarg0, 0);
25242524
__ leave();
@@ -2527,7 +2527,7 @@ class StubGenerator: public StubCodeGenerator {
25272527
return start;
25282528
}
25292529

2530-
void generate_aes_decrypt(const VectorRegister &res, VectorRegister *working_vregs, int rounds) {
2530+
void aes_decrypt(const VectorRegister &res, VectorRegister *working_vregs, int rounds) {
25312531
assert(rounds <= 15, "rounds should be less than or equal to working_vregs size");
25322532

25332533
__ vxor_vv(res, res, working_vregs[rounds - 1]);
@@ -2578,26 +2578,26 @@ class StubGenerator: public StubCodeGenerator {
25782578
// Else we fallthrough to the biggest case (256-bit key size)
25792579

25802580
// Note: the following function performs key += 15*16
2581-
generate_aes_loadkeys(key, working_vregs, 15);
2582-
generate_aes_decrypt(res, working_vregs, 15);
2581+
aes_load_keys(key, working_vregs, 15);
2582+
aes_decrypt(res, working_vregs, 15);
25832583
__ vse32_v(res, to);
25842584
__ mv(c_rarg0, 0);
25852585
__ leave();
25862586
__ ret();
25872587

25882588
__ bind(L_aes192);
25892589
// Note: the following function performs key += 13*16
2590-
generate_aes_loadkeys(key, working_vregs, 13);
2591-
generate_aes_decrypt(res, working_vregs, 13);
2590+
aes_load_keys(key, working_vregs, 13);
2591+
aes_decrypt(res, working_vregs, 13);
25922592
__ vse32_v(res, to);
25932593
__ mv(c_rarg0, 0);
25942594
__ leave();
25952595
__ ret();
25962596

25972597
__ bind(L_aes128);
25982598
// Note: the following function performs key += 11*16
2599-
generate_aes_loadkeys(key, working_vregs, 11);
2600-
generate_aes_decrypt(res, working_vregs, 11);
2599+
aes_load_keys(key, working_vregs, 11);
2600+
aes_decrypt(res, working_vregs, 11);
26012601
__ vse32_v(res, to);
26022602
__ mv(c_rarg0, 0);
26032603
__ leave();
@@ -2622,14 +2622,14 @@ class StubGenerator: public StubCodeGenerator {
26222622
__ vsetivli(x0, 4, Assembler::e32, Assembler::m1);
26232623
__ vle32_v(v16, rvec);
26242624

2625-
generate_aes_loadkeys(key, working_vregs, round);
2625+
aes_load_keys(key, working_vregs, round);
26262626
Label L_enc_loop;
26272627
__ bind(L_enc_loop);
26282628
// Encrypt from source by block size
26292629
__ vle32_v(v17, from);
26302630
__ addi(from, from, BLOCK_SIZE);
26312631
__ vxor_vv(v16, v16, v17);
2632-
generate_aes_encrypt(v16, working_vregs, round);
2632+
aes_encrypt(v16, working_vregs, round);
26332633
__ vse32_v(v16, to);
26342634
__ addi(to, to, BLOCK_SIZE);
26352635
__ subi(len, len, BLOCK_SIZE);
@@ -2709,14 +2709,14 @@ class StubGenerator: public StubCodeGenerator {
27092709
__ vsetivli(x0, 4, Assembler::e32, Assembler::m1);
27102710
__ vle32_v(v16, rvec);
27112711

2712-
generate_aes_loadkeys(key, working_vregs, round);
2712+
aes_load_keys(key, working_vregs, round);
27132713
Label L_dec_loop;
27142714
// Decrypt from source by block size
27152715
__ bind(L_dec_loop);
27162716
__ vle32_v(v17, from);
27172717
__ addi(from, from, BLOCK_SIZE);
27182718
__ vmv_v_v(v18, v17);
2719-
generate_aes_decrypt(v17, working_vregs, round);
2719+
aes_decrypt(v17, working_vregs, round);
27202720
__ vxor_vv(v17, v17, v16);
27212721
__ vse32_v(v17, to);
27222722
__ vmv_v_v(v16, v18);
@@ -2811,7 +2811,7 @@ class StubGenerator: public StubCodeGenerator {
28112811
Register input_len, Register saved_encrypted_ctr, Register used_ptr) {
28122812
// Algorithm:
28132813
//
2814-
// generate_aes_loadkeys();
2814+
// aes_load_keys();
28152815
// load_counter_128(counter_hi, counter_lo, counter);
28162816
//
28172817
// L_next:
@@ -2825,7 +2825,7 @@ class StubGenerator: public StubCodeGenerator {
28252825
//
28262826
// L_main_loop:
28272827
// if (len == 0) goto L_exit;
2828-
// saved_encrypted_ctr = generate_aes_encrypt(counter);
2828+
// saved_encrypted_ctr = aes_encrypt(counter);
28292829
//
28302830
// add_counter_128(counter_hi, counter_lo);
28312831
// be_store_counter_128(counter_hi, counter_lo, counter);
@@ -2869,7 +2869,7 @@ class StubGenerator: public StubCodeGenerator {
28692869
__ mv(block_size, BLOCK_SIZE);
28702870

28712871
// load keys to working_vregs according to round
2872-
generate_aes_loadkeys(key, working_vregs, round);
2872+
aes_load_keys(key, working_vregs, round);
28732873

28742874
// 128-bit big-endian load
28752875
be_load_counter_128(counter_hi, counter_lo, counter);
@@ -2902,7 +2902,7 @@ class StubGenerator: public StubCodeGenerator {
29022902
__ vle32_v(v16, counter);
29032903

29042904
// encrypt counter according to round
2905-
generate_aes_encrypt(v16, working_vregs, round);
2905+
aes_encrypt(v16, working_vregs, round);
29062906

29072907
__ vse32_v(v16, saved_encrypted_ctr);
29082908

@@ -2997,6 +2997,34 @@ class StubGenerator: public StubCodeGenerator {
29972997
return start;
29982998
}
29992999

3000+
void ghash_loop(Register state, Register subkeyH, Register data, Register blocks,
3001+
VectorRegister vtmp1, VectorRegister vtmp2, VectorRegister vtmp3) {
3002+
VectorRegister partial_hash = vtmp1;
3003+
VectorRegister hash_subkey = vtmp2;
3004+
VectorRegister cipher_text = vtmp3;
3005+
3006+
const unsigned int BLOCK_SIZE = 16;
3007+
3008+
__ vsetivli(x0, 2, Assembler::e64, Assembler::m1);
3009+
__ vle64_v(hash_subkey, subkeyH);
3010+
__ vrev8_v(hash_subkey, hash_subkey);
3011+
__ vle64_v(partial_hash, state);
3012+
__ vrev8_v(partial_hash, partial_hash);
3013+
3014+
__ vsetivli(x0, 4, Assembler::e32, Assembler::m1);
3015+
Label L_ghash_loop;
3016+
__ bind(L_ghash_loop);
3017+
__ vle32_v(cipher_text, data);
3018+
__ addi(data, data, BLOCK_SIZE);
3019+
__ vghsh_vv(partial_hash, hash_subkey, cipher_text);
3020+
__ subi(blocks, blocks, 1);
3021+
__ bnez(blocks, L_ghash_loop);
3022+
3023+
__ vsetivli(x0, 2, Assembler::e64, Assembler::m1);
3024+
__ vrev8_v(partial_hash, partial_hash);
3025+
__ vse64_v(partial_hash, state);
3026+
}
3027+
30003028
/**
30013029
* Arguments:
30023030
*
@@ -3024,30 +3052,12 @@ class StubGenerator: public StubCodeGenerator {
30243052
Register data = c_rarg2;
30253053
Register blocks = c_rarg3;
30263054

3027-
VectorRegister partial_hash = v1;
3028-
VectorRegister hash_subkey = v2;
3029-
VectorRegister cipher_text = v3;
3055+
VectorRegister vtmp1 = v1;
3056+
VectorRegister vtmp2 = v2;
3057+
VectorRegister vtmp3 = v3;
30303058

3031-
const unsigned int BLOCK_SIZE = 16;
3059+
ghash_loop(state, subkeyH, data, blocks, vtmp1, vtmp2, vtmp3);
30323060

3033-
__ vsetivli(x0, 2, Assembler::e64, Assembler::m1);
3034-
__ vle64_v(hash_subkey, subkeyH);
3035-
__ vrev8_v(hash_subkey, hash_subkey);
3036-
__ vle64_v(partial_hash, state);
3037-
__ vrev8_v(partial_hash, partial_hash);
3038-
3039-
__ vsetivli(x0, 4, Assembler::e32, Assembler::m1);
3040-
Label L_ghash_loop;
3041-
__ bind(L_ghash_loop);
3042-
__ vle32_v(cipher_text, data);
3043-
__ addi(data, data, BLOCK_SIZE);
3044-
__ vghsh_vv(partial_hash, hash_subkey, cipher_text);
3045-
__ subi(blocks, blocks, 1);
3046-
__ bnez(blocks, L_ghash_loop);
3047-
3048-
__ vsetivli(x0, 2, Assembler::e64, Assembler::m1);
3049-
__ vrev8_v(partial_hash, partial_hash);
3050-
__ vse64_v(partial_hash, state);
30513061
__ leave();
30523062
__ ret();
30533063

0 commit comments

Comments
 (0)