| 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 2022-2026 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) 2022, Hongren (Zenithal) Zheng <[email protected]> |
| 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 |
|
| 37 |
use strict; |
| 38 |
use warnings; |
| 39 |
|
| 40 |
use FindBin qw($Bin); |
| 41 |
use lib "$Bin"; |
| 42 |
use lib "$Bin/../../perlasm"; |
| 43 |
use riscv; |
| 44 |
|
| 45 |
# $output is the last argument if it looks like a file (it has an extension) |
| 46 |
# $flavour is the first argument if it doesn't look like a file |
| 47 |
my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; |
| 48 |
my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef; |
| 49 |
|
| 50 |
$output and open STDOUT,">$output"; |
| 51 |
|
| 52 |
################################################################################ |
| 53 |
# Utility functions to help with keeping track of which registers to stack/ |
| 54 |
# unstack when entering / exiting routines. |
| 55 |
################################################################################ |
| 56 |
{ |
| 57 |
# Callee-saved registers |
| 58 |
my @callee_saved = map("x$_",(2,8,9,18..27)); |
| 59 |
# Caller-saved registers |
| 60 |
my @caller_saved = map("x$_",(1,5..7,10..17,28..31)); |
| 61 |
my @must_save; |
| 62 |
sub use_reg { |
| 63 |
my $reg = shift; |
| 64 |
if (grep(/^$reg$/, @callee_saved)) { |
| 65 |
push(@must_save, $reg); |
| 66 |
} elsif (!grep(/^$reg$/, @caller_saved)) { |
| 67 |
# Register is not usable! |
| 68 |
die("Unusable register ".$reg); |
| 69 |
} |
| 70 |
return $reg; |
| 71 |
} |
| 72 |
sub use_regs { |
| 73 |
return map(use_reg("x$_"), @_); |
| 74 |
} |
| 75 |
sub save_regs { |
| 76 |
my $ret = ''; |
| 77 |
my $stack_reservation = ($#must_save + 1) * 8; |
| 78 |
my $stack_offset = $stack_reservation; |
| 79 |
if ($stack_reservation % 16) { |
| 80 |
$stack_reservation += 8; |
| 81 |
} |
| 82 |
$ret.=" addi sp,sp,-$stack_reservation\n"; |
| 83 |
foreach (@must_save) { |
| 84 |
$stack_offset -= 8; |
| 85 |
$ret.=" sd $_,$stack_offset(sp)\n"; |
| 86 |
} |
| 87 |
return $ret; |
| 88 |
} |
| 89 |
sub load_regs { |
| 90 |
my $ret = ''; |
| 91 |
my $stack_reservation = ($#must_save + 1) * 8; |
| 92 |
my $stack_offset = $stack_reservation; |
| 93 |
if ($stack_reservation % 16) { |
| 94 |
$stack_reservation += 8; |
| 95 |
} |
| 96 |
foreach (@must_save) { |
| 97 |
$stack_offset -= 8; |
| 98 |
$ret.=" ld $_,$stack_offset(sp)\n"; |
| 99 |
} |
| 100 |
$ret.=" addi sp,sp,$stack_reservation\n"; |
| 101 |
return $ret; |
| 102 |
} |
| 103 |
sub clear_regs { |
| 104 |
@must_save = (); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
################################################################################ |
| 109 |
# Register assignment for rv64i_zkne_encrypt and rv64i_zknd_decrypt |
| 110 |
################################################################################ |
| 111 |
|
| 112 |
# Registers to hold AES state (called s0-s3 or y0-y3 elsewhere) |
| 113 |
my ($Q0,$Q1,$Q2,$Q3) = use_regs(6..9); |
| 114 |
|
| 115 |
# Function arguments (x10-x12 are a0-a2 in the ABI) |
| 116 |
# Input block pointer, output block pointer, key pointer |
| 117 |
my ($INP,$OUTP,$KEYP) = use_regs(10..12); |
| 118 |
|
| 119 |
# Temporaries |
| 120 |
my ($T0,$T1) = use_regs(13..14); |
| 121 |
|
| 122 |
# Loop counter |
| 123 |
my ($loopcntr) = use_regs(30); |
| 124 |
|
| 125 |
################################################################################ |
| 126 |
# void rv64i_zkne_encrypt(const unsigned char *in, unsigned char *out, |
| 127 |
# const AES_KEY *key); |
| 128 |
################################################################################ |
| 129 |
my $code .= <<___; |
| 130 |
.text |
| 131 |
.balign 16 |
| 132 |
.globl rv64i_zkne_encrypt |
| 133 |
.type rv64i_zkne_encrypt,\@function |
| 134 |
rv64i_zkne_encrypt: |
| 135 |
___ |
| 136 |
|
| 137 |
$code .= save_regs(); |
| 138 |
|
| 139 |
$code .= <<___; |
| 140 |
|
| 141 |
# Load input to block cipher |
| 142 |
ld $Q0,0($INP) |
| 143 |
ld $Q1,8($INP) |
| 144 |
|
| 145 |
# Load key |
| 146 |
ld $T0,0($KEYP) |
| 147 |
ld $T1,8($KEYP) |
| 148 |
|
| 149 |
# Load number of rounds |
| 150 |
lwu $loopcntr,240($KEYP) |
| 151 |
|
| 152 |
# initial transformation |
| 153 |
xor $Q0,$Q0,$T0 |
| 154 |
xor $Q1,$Q1,$T1 |
| 155 |
|
| 156 |
# The main loop only executes the first N-1 rounds. |
| 157 |
add $loopcntr,$loopcntr,-1 |
| 158 |
|
| 159 |
# Do Nr - 1 rounds (final round is special) |
| 160 |
1: |
| 161 |
@{[aes64esm $Q2,$Q0,$Q1]} |
| 162 |
@{[aes64esm $Q3,$Q1,$Q0]} |
| 163 |
|
| 164 |
# Update key ptr to point to next key in schedule |
| 165 |
add $KEYP,$KEYP,16 |
| 166 |
|
| 167 |
# Grab next key in schedule |
| 168 |
ld $T0,0($KEYP) |
| 169 |
ld $T1,8($KEYP) |
| 170 |
xor $Q0,$Q2,$T0 |
| 171 |
xor $Q1,$Q3,$T1 |
| 172 |
|
| 173 |
add $loopcntr,$loopcntr,-1 |
| 174 |
bgtz $loopcntr,1b |
| 175 |
|
| 176 |
# final round |
| 177 |
@{[aes64es $Q2,$Q0,$Q1]} |
| 178 |
@{[aes64es $Q3,$Q1,$Q0]} |
| 179 |
|
| 180 |
# since not added 16 before |
| 181 |
ld $T0,16($KEYP) |
| 182 |
ld $T1,24($KEYP) |
| 183 |
xor $Q0,$Q2,$T0 |
| 184 |
xor $Q1,$Q3,$T1 |
| 185 |
|
| 186 |
sd $Q0,0($OUTP) |
| 187 |
sd $Q1,8($OUTP) |
| 188 |
|
| 189 |
# Pop registers and return |
| 190 |
___ |
| 191 |
|
| 192 |
$code .= load_regs(); |
| 193 |
|
| 194 |
$code .= <<___; |
| 195 |
ret |
| 196 |
___ |
| 197 |
|
| 198 |
################################################################################ |
| 199 |
# void rv64i_zknd_decrypt(const unsigned char *in, unsigned char *out, |
| 200 |
# const AES_KEY *key); |
| 201 |
################################################################################ |
| 202 |
$code .= <<___; |
| 203 |
.text |
| 204 |
.balign 16 |
| 205 |
.globl rv64i_zknd_decrypt |
| 206 |
.type rv64i_zknd_decrypt,\@function |
| 207 |
rv64i_zknd_decrypt: |
| 208 |
___ |
| 209 |
|
| 210 |
$code .= save_regs(); |
| 211 |
|
| 212 |
$code .= <<___; |
| 213 |
|
| 214 |
# Load input to block cipher |
| 215 |
ld $Q0,0($INP) |
| 216 |
ld $Q1,8($INP) |
| 217 |
|
| 218 |
# Load number of rounds |
| 219 |
lwu $loopcntr,240($KEYP) |
| 220 |
|
| 221 |
# Load the last key |
| 222 |
slli $T0,$loopcntr,4 |
| 223 |
add $KEYP,$KEYP,$T0 |
| 224 |
ld $T0,0($KEYP) |
| 225 |
ld $T1,8($KEYP) |
| 226 |
|
| 227 |
xor $Q0,$Q0,$T0 |
| 228 |
xor $Q1,$Q1,$T1 |
| 229 |
|
| 230 |
# The main loop only executes the first N-1 rounds. |
| 231 |
add $loopcntr,$loopcntr,-1 |
| 232 |
|
| 233 |
# Do Nr - 1 rounds (final round is special) |
| 234 |
1: |
| 235 |
@{[aes64dsm $Q2,$Q0,$Q1]} |
| 236 |
@{[aes64dsm $Q3,$Q1,$Q0]} |
| 237 |
|
| 238 |
# Update key ptr to point to next key in schedule |
| 239 |
add $KEYP,$KEYP,-16 |
| 240 |
|
| 241 |
# Grab next key in schedule |
| 242 |
ld $T0,0($KEYP) |
| 243 |
ld $T1,8($KEYP) |
| 244 |
xor $Q0,$Q2,$T0 |
| 245 |
xor $Q1,$Q3,$T1 |
| 246 |
|
| 247 |
add $loopcntr,$loopcntr,-1 |
| 248 |
bgtz $loopcntr,1b |
| 249 |
|
| 250 |
# final round |
| 251 |
@{[aes64ds $Q2,$Q0,$Q1]} |
| 252 |
@{[aes64ds $Q3,$Q1,$Q0]} |
| 253 |
|
| 254 |
add $KEYP,$KEYP,-16 |
| 255 |
ld $T0,0($KEYP) |
| 256 |
ld $T1,8($KEYP) |
| 257 |
xor $Q0,$Q2,$T0 |
| 258 |
xor $Q1,$Q3,$T1 |
| 259 |
|
| 260 |
sd $Q0,0($OUTP) |
| 261 |
sd $Q1,8($OUTP) |
| 262 |
# Pop registers and return |
| 263 |
___ |
| 264 |
|
| 265 |
$code .= load_regs(); |
| 266 |
|
| 267 |
$code .= <<___; |
| 268 |
ret |
| 269 |
___ |
| 270 |
|
| 271 |
clear_regs(); |
| 272 |
|
| 273 |
################################################################################ |
| 274 |
# Register assignment for rv64i_zkn[e/d]_set_[en/de]crypt_key |
| 275 |
################################################################################ |
| 276 |
|
| 277 |
# Function arguments (x10-x12 are a0-a2 in the ABI) |
| 278 |
# Pointer to user key, number of bits in key, key pointer |
| 279 |
my ($UKEY,$BITS,$KEYP) = use_regs(10..12); |
| 280 |
|
| 281 |
# Temporaries |
| 282 |
my ($T0,$T1,$T2,$T3,$T4) = use_regs(6..8,13..14); |
| 283 |
|
| 284 |
################################################################################ |
| 285 |
# utility functions for rv64i_zkne_set_encrypt_key |
| 286 |
################################################################################ |
| 287 |
sub ke128enc { |
| 288 |
my $rnum = 0; |
| 289 |
my $ret = ''; |
| 290 |
$ret .= <<___; |
| 291 |
ld $T0,0($UKEY) |
| 292 |
ld $T1,8($UKEY) |
| 293 |
sd $T0,0($KEYP) |
| 294 |
sd $T1,8($KEYP) |
| 295 |
___ |
| 296 |
while($rnum < 10) { |
| 297 |
$ret .= <<___; |
| 298 |
@{[aes64ks1i $T2,$T1,$rnum]} |
| 299 |
@{[aes64ks2 $T0,$T2,$T0]} |
| 300 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 301 |
add $KEYP,$KEYP,16 |
| 302 |
sd $T0,0($KEYP) |
| 303 |
sd $T1,8($KEYP) |
| 304 |
___ |
| 305 |
$rnum++; |
| 306 |
} |
| 307 |
return $ret; |
| 308 |
} |
| 309 |
|
| 310 |
sub ke192enc { |
| 311 |
my $rnum = 0; |
| 312 |
my $ret = ''; |
| 313 |
$ret .= <<___; |
| 314 |
ld $T0,0($UKEY) |
| 315 |
ld $T1,8($UKEY) |
| 316 |
ld $T2,16($UKEY) |
| 317 |
sd $T0,0($KEYP) |
| 318 |
sd $T1,8($KEYP) |
| 319 |
sd $T2,16($KEYP) |
| 320 |
___ |
| 321 |
while($rnum < 8) { |
| 322 |
$ret .= <<___; |
| 323 |
@{[aes64ks1i $T3,$T2,$rnum]} |
| 324 |
@{[aes64ks2 $T0,$T3,$T0]} |
| 325 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 326 |
___ |
| 327 |
if ($rnum != 7) { |
| 328 |
# note that (8+1)*24 = 216, (12+1)*16 = 208 |
| 329 |
# thus the last 8 bytes can be dropped |
| 330 |
$ret .= <<___; |
| 331 |
@{[aes64ks2 $T2,$T1,$T2]} |
| 332 |
___ |
| 333 |
} |
| 334 |
$ret .= <<___; |
| 335 |
add $KEYP,$KEYP,24 |
| 336 |
sd $T0,0($KEYP) |
| 337 |
sd $T1,8($KEYP) |
| 338 |
___ |
| 339 |
if ($rnum != 7) { |
| 340 |
$ret .= <<___; |
| 341 |
sd $T2,16($KEYP) |
| 342 |
___ |
| 343 |
} |
| 344 |
$rnum++; |
| 345 |
} |
| 346 |
return $ret; |
| 347 |
} |
| 348 |
|
| 349 |
sub ke256enc { |
| 350 |
my $rnum = 0; |
| 351 |
my $ret = ''; |
| 352 |
$ret .= <<___; |
| 353 |
ld $T0,0($UKEY) |
| 354 |
ld $T1,8($UKEY) |
| 355 |
ld $T2,16($UKEY) |
| 356 |
ld $T3,24($UKEY) |
| 357 |
sd $T0,0($KEYP) |
| 358 |
sd $T1,8($KEYP) |
| 359 |
sd $T2,16($KEYP) |
| 360 |
sd $T3,24($KEYP) |
| 361 |
___ |
| 362 |
while($rnum < 7) { |
| 363 |
$ret .= <<___; |
| 364 |
@{[aes64ks1i $T4,$T3,$rnum]} |
| 365 |
@{[aes64ks2 $T0,$T4,$T0]} |
| 366 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 367 |
add $KEYP,$KEYP,32 |
| 368 |
sd $T0,0($KEYP) |
| 369 |
sd $T1,8($KEYP) |
| 370 |
___ |
| 371 |
if ($rnum != 6) { |
| 372 |
# note that (7+1)*32 = 256, (14+1)*16 = 240 |
| 373 |
# thus the last 16 bytes can be dropped |
| 374 |
$ret .= <<___; |
| 375 |
@{[aes64ks1i $T4,$T1,0xA]} |
| 376 |
@{[aes64ks2 $T2,$T4,$T2]} |
| 377 |
@{[aes64ks2 $T3,$T2,$T3]} |
| 378 |
sd $T2,16($KEYP) |
| 379 |
sd $T3,24($KEYP) |
| 380 |
___ |
| 381 |
} |
| 382 |
$rnum++; |
| 383 |
} |
| 384 |
return $ret; |
| 385 |
} |
| 386 |
|
| 387 |
################################################################################ |
| 388 |
# void rv64i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, |
| 389 |
# AES_KEY *key) |
| 390 |
################################################################################ |
| 391 |
sub AES_set_common { |
| 392 |
my ($ke128, $ke192, $ke256) = @_; |
| 393 |
my $ret = ''; |
| 394 |
$ret .= <<___; |
| 395 |
# Determine number of rounds from key size in bits |
| 396 |
li $T0,128 |
| 397 |
bne $BITS,$T0,1f |
| 398 |
li $T1,10 # key->rounds = 10 if bits == 128 |
| 399 |
sw $T1,240($KEYP) # store key->rounds |
| 400 |
$ke128 |
| 401 |
j 4f |
| 402 |
1: |
| 403 |
li $T0,192 |
| 404 |
bne $BITS,$T0,2f |
| 405 |
li $T1,12 # key->rounds = 12 if bits == 192 |
| 406 |
sw $T1,240($KEYP) # store key->rounds |
| 407 |
$ke192 |
| 408 |
j 4f |
| 409 |
2: |
| 410 |
li $T1,14 # key->rounds = 14 if bits == 256 |
| 411 |
li $T0,256 |
| 412 |
beq $BITS,$T0,3f |
| 413 |
li a0,-2 # If bits != 128, 192, or 256, return -2 |
| 414 |
j 5f |
| 415 |
3: |
| 416 |
sw $T1,240($KEYP) # store key->rounds |
| 417 |
$ke256 |
| 418 |
4: # return 0 |
| 419 |
li a0,0 |
| 420 |
5: # return a0 |
| 421 |
___ |
| 422 |
return $ret; |
| 423 |
} |
| 424 |
$code .= <<___; |
| 425 |
.text |
| 426 |
.balign 16 |
| 427 |
.globl rv64i_zkne_set_encrypt_key |
| 428 |
.type rv64i_zkne_set_encrypt_key,\@function |
| 429 |
rv64i_zkne_set_encrypt_key: |
| 430 |
___ |
| 431 |
$code .= save_regs(); |
| 432 |
$code .= AES_set_common(ke128enc(), ke192enc(),ke256enc()); |
| 433 |
$code .= load_regs(); |
| 434 |
$code .= <<___; |
| 435 |
ret |
| 436 |
___ |
| 437 |
|
| 438 |
################################################################################ |
| 439 |
# utility functions for rv64i_zknd_set_decrypt_key |
| 440 |
################################################################################ |
| 441 |
sub ke128dec { |
| 442 |
my $rnum = 0; |
| 443 |
my $ret = ''; |
| 444 |
$ret .= <<___; |
| 445 |
ld $T0,0($UKEY) |
| 446 |
ld $T1,8($UKEY) |
| 447 |
sd $T0,0($KEYP) |
| 448 |
sd $T1,8($KEYP) |
| 449 |
___ |
| 450 |
while($rnum < 10) { |
| 451 |
$ret .= <<___; |
| 452 |
@{[aes64ks1i $T2,$T1,$rnum]} |
| 453 |
@{[aes64ks2 $T0,$T2,$T0]} |
| 454 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 455 |
add $KEYP,$KEYP,16 |
| 456 |
___ |
| 457 |
# need to aes64im for [1:N-1] round keys |
| 458 |
# this is from the fact that aes64dsm subwords first then mix column |
| 459 |
# intuitively decryption needs to first mix column then subwords |
| 460 |
# however, for merging datapaths (encryption first subwords then mix column) |
| 461 |
# aes64dsm chooses to inverse the order of them, thus |
| 462 |
# transform should then be done on the round key |
| 463 |
if ($rnum < 9) { |
| 464 |
$ret .= <<___; |
| 465 |
@{[aes64im $T2,$T0]} |
| 466 |
sd $T2,0($KEYP) |
| 467 |
@{[aes64im $T2,$T1]} |
| 468 |
sd $T2,8($KEYP) |
| 469 |
___ |
| 470 |
} else { |
| 471 |
$ret .= <<___; |
| 472 |
sd $T0,0($KEYP) |
| 473 |
sd $T1,8($KEYP) |
| 474 |
___ |
| 475 |
} |
| 476 |
$rnum++; |
| 477 |
} |
| 478 |
return $ret; |
| 479 |
} |
| 480 |
|
| 481 |
sub ke192dec { |
| 482 |
my $rnum = 0; |
| 483 |
my $ret = ''; |
| 484 |
$ret .= <<___; |
| 485 |
ld $T0,0($UKEY) |
| 486 |
ld $T1,8($UKEY) |
| 487 |
ld $T2,16($UKEY) |
| 488 |
sd $T0,0($KEYP) |
| 489 |
sd $T1,8($KEYP) |
| 490 |
@{[aes64im $T3,$T2]} |
| 491 |
sd $T3,16($KEYP) |
| 492 |
___ |
| 493 |
while($rnum < 8) { |
| 494 |
$ret .= <<___; |
| 495 |
@{[aes64ks1i $T3,$T2,$rnum]} |
| 496 |
@{[aes64ks2 $T0,$T3,$T0]} |
| 497 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 498 |
add $KEYP,$KEYP,24 |
| 499 |
___ |
| 500 |
if ($rnum < 7) { |
| 501 |
$ret .= <<___; |
| 502 |
@{[aes64im $T3,$T0]} |
| 503 |
sd $T3,0($KEYP) |
| 504 |
@{[aes64im $T3,$T1]} |
| 505 |
sd $T3,8($KEYP) |
| 506 |
# the reason is in ke192enc |
| 507 |
@{[aes64ks2 $T2,$T1,$T2]} |
| 508 |
@{[aes64im $T3,$T2]} |
| 509 |
sd $T3,16($KEYP) |
| 510 |
___ |
| 511 |
} else { # rnum == 7 |
| 512 |
$ret .= <<___; |
| 513 |
sd $T0,0($KEYP) |
| 514 |
sd $T1,8($KEYP) |
| 515 |
___ |
| 516 |
} |
| 517 |
$rnum++; |
| 518 |
} |
| 519 |
return $ret; |
| 520 |
} |
| 521 |
|
| 522 |
sub ke256dec { |
| 523 |
my $rnum = 0; |
| 524 |
my $ret = ''; |
| 525 |
$ret .= <<___; |
| 526 |
ld $T0,0($UKEY) |
| 527 |
ld $T1,8($UKEY) |
| 528 |
ld $T2,16($UKEY) |
| 529 |
ld $T3,24($UKEY) |
| 530 |
sd $T0,0($KEYP) |
| 531 |
sd $T1,8($KEYP) |
| 532 |
@{[aes64im $T4,$T2]} |
| 533 |
sd $T4,16($KEYP) |
| 534 |
@{[aes64im $T4,$T3]} |
| 535 |
sd $T4,24($KEYP) |
| 536 |
___ |
| 537 |
while($rnum < 7) { |
| 538 |
$ret .= <<___; |
| 539 |
@{[aes64ks1i $T4,$T3,$rnum]} |
| 540 |
@{[aes64ks2 $T0,$T4,$T0]} |
| 541 |
@{[aes64ks2 $T1,$T0,$T1]} |
| 542 |
add $KEYP,$KEYP,32 |
| 543 |
___ |
| 544 |
if ($rnum < 6) { |
| 545 |
$ret .= <<___; |
| 546 |
@{[aes64ks1i $T4,$T1,0xA]} |
| 547 |
@{[aes64ks2 $T2,$T4,$T2]} |
| 548 |
@{[aes64ks2 $T3,$T2,$T3]} |
| 549 |
@{[aes64im $T4,$T0]} |
| 550 |
sd $T4,0($KEYP) |
| 551 |
@{[aes64im $T4,$T1]} |
| 552 |
sd $T4,8($KEYP) |
| 553 |
@{[aes64im $T4,$T2]} |
| 554 |
sd $T4,16($KEYP) |
| 555 |
@{[aes64im $T4,$T3]} |
| 556 |
sd $T4,24($KEYP) |
| 557 |
___ |
| 558 |
} else { |
| 559 |
$ret .= <<___; |
| 560 |
sd $T0,0($KEYP) |
| 561 |
sd $T1,8($KEYP) |
| 562 |
# last two one dropped |
| 563 |
___ |
| 564 |
} |
| 565 |
$rnum++; |
| 566 |
} |
| 567 |
return $ret; |
| 568 |
} |
| 569 |
|
| 570 |
################################################################################ |
| 571 |
# void rv64i_zknd_set_decrypt_key(const unsigned char *userKey, const int bits, |
| 572 |
# AES_KEY *key) |
| 573 |
################################################################################ |
| 574 |
$code .= <<___; |
| 575 |
.text |
| 576 |
.balign 16 |
| 577 |
.globl rv64i_zknd_set_decrypt_key |
| 578 |
.type rv64i_zknd_set_decrypt_key,\@function |
| 579 |
rv64i_zknd_set_decrypt_key: |
| 580 |
___ |
| 581 |
$code .= save_regs(); |
| 582 |
$code .= AES_set_common(ke128dec(), ke192dec(),ke256dec()); |
| 583 |
$code .= load_regs(); |
| 584 |
$code .= <<___; |
| 585 |
ret |
| 586 |
___ |
| 587 |
|
| 588 |
print $code; |
| 589 |
close STDOUT or die "error closing STDOUT: $!"; |