| 1 |
#! /usr/bin/env perl |
| 2 |
# Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
# this file except in compliance with the License. You can obtain a copy |
| 6 |
# in the file LICENSE in the source distribution or at |
| 7 |
# https://www.openssl.org/source/license.html |
| 8 |
|
| 9 |
|
| 10 |
# $output is the last argument if it looks like a file (it has an extension) |
| 11 |
# $flavour is the first argument if it doesn't look like a file |
| 12 |
$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; |
| 13 |
$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef; |
| 14 |
|
| 15 |
$output and open STDOUT,">$output"; |
| 16 |
|
| 17 |
{ |
| 18 |
my ($in_a,$in_b,$len,$x,$temp1,$temp2) = ('a0','a1','a2','t0','t1','t2'); |
| 19 |
$code.=<<___; |
| 20 |
################################################################################ |
| 21 |
# int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len) |
| 22 |
################################################################################ |
| 23 |
.text |
| 24 |
.balign 16 |
| 25 |
.globl CRYPTO_memcmp |
| 26 |
.type CRYPTO_memcmp,\@function |
| 27 |
CRYPTO_memcmp: |
| 28 |
li $x,0 |
| 29 |
beqz $len,2f # len == 0 |
| 30 |
1: |
| 31 |
lbu $temp1,0($in_a) |
| 32 |
lbu $temp2,0($in_b) |
| 33 |
addi $in_a,$in_a,1 |
| 34 |
addi $in_b,$in_b,1 |
| 35 |
addi $len,$len,-1 |
| 36 |
xor $temp1,$temp1,$temp2 |
| 37 |
or $x,$x,$temp1 |
| 38 |
bgtz $len,1b |
| 39 |
2: |
| 40 |
mv a0,$x |
| 41 |
ret |
| 42 |
___ |
| 43 |
} |
| 44 |
{ |
| 45 |
my ($ptr,$len,$temp1,$temp2) = ('a0','a1','t0','t1'); |
| 46 |
$code.=<<___; |
| 47 |
################################################################################ |
| 48 |
# void OPENSSL_cleanse(void *ptr, size_t len) |
| 49 |
################################################################################ |
| 50 |
.text |
| 51 |
.balign 16 |
| 52 |
.globl OPENSSL_cleanse |
| 53 |
.type OPENSSL_cleanse,\@function |
| 54 |
OPENSSL_cleanse: |
| 55 |
beqz $len,2f # len == 0, return |
| 56 |
srli $temp1,$len,4 |
| 57 |
bnez $temp1,3f # len > 15 |
| 58 |
|
| 59 |
1: # Store <= 15 individual bytes |
| 60 |
sb x0,0($ptr) |
| 61 |
addi $ptr,$ptr,1 |
| 62 |
addi $len,$len,-1 |
| 63 |
bnez $len,1b |
| 64 |
2: |
| 65 |
ret |
| 66 |
|
| 67 |
3: # Store individual bytes until we are aligned |
| 68 |
andi $temp1,$ptr,0x3 |
| 69 |
beqz $temp1,4f |
| 70 |
sb x0,0($ptr) |
| 71 |
addi $ptr,$ptr,1 |
| 72 |
addi $len,$len,-1 |
| 73 |
j 3b |
| 74 |
|
| 75 |
4: # Store aligned words |
| 76 |
li $temp2,4 |
| 77 |
4: |
| 78 |
sw x0,0($ptr) |
| 79 |
addi $ptr,$ptr,4 |
| 80 |
addi $len,$len,-4 |
| 81 |
bge $len,$temp2,4b # if len>=4 loop |
| 82 |
bnez $len,1b # if len<4 and len != 0, store remaining bytes |
| 83 |
ret |
| 84 |
___ |
| 85 |
} |
| 86 |
|
| 87 |
{ |
| 88 |
my ($ret) = ('a0'); |
| 89 |
$code .= <<___; |
| 90 |
################################################################################ |
| 91 |
# size_t riscv_vlen_asm(void) |
| 92 |
# Return VLEN (i.e. the length of a vector register in bits). |
| 93 |
.p2align 3 |
| 94 |
.globl riscv_vlen_asm |
| 95 |
.type riscv_vlen_asm,\@function |
| 96 |
riscv_vlen_asm: |
| 97 |
# 0xc22 is CSR vlenb |
| 98 |
csrr $ret, 0xc22 |
| 99 |
slli $ret, $ret, 3 |
| 100 |
ret |
| 101 |
.size riscv_vlen_asm,.-riscv_vlen_asm |
| 102 |
___ |
| 103 |
} |
| 104 |
|
| 105 |
print $code; |
| 106 |
close STDOUT or die "error closing STDOUT: $!"; |