1 #include <machine/asm.h> 2 .text 3 .balign 16 4 .globl rv64i_zkne_encrypt 5 .type rv64i_zkne_encrypt,@function 6 rv64i_zkne_encrypt: 7 addi sp,sp,-16 8 sd x8,8(sp) 9 sd x9,0(sp) 10 11 # Load input to block cipher 12 ld x6,0(x10) 13 ld x7,8(x10) 14 15 # Load key 16 ld x13,0(x12) 17 ld x14,8(x12) 18 19 # Load number of rounds 20 lwu x30,240(x12) 21 22 # initial transformation 23 xor x6,x6,x13 24 xor x7,x7,x14 25 26 # The main loop only executes the first N-1 rounds. 27 add x30,x30,-1 28 29 # Do Nr - 1 rounds (final round is special) 30 1: 31 .word 913507379 32 .word 912491699 33 34 # Update key ptr to point to next key in schedule 35 add x12,x12,16 36 37 # Grab next key in schedule 38 ld x13,0(x12) 39 ld x14,8(x12) 40 xor x6,x8,x13 41 xor x7,x9,x14 42 43 add x30,x30,-1 44 bgtz x30,1b 45 46 # final round 47 .word 846398515 48 .word 845382835 49 50 # since not added 16 before 51 ld x13,16(x12) 52 ld x14,24(x12) 53 xor x6,x8,x13 54 xor x7,x9,x14 55 56 sd x6,0(x11) 57 sd x7,8(x11) 58 59 # Pop registers and return 60 ld x8,8(sp) 61 ld x9,0(sp) 62 addi sp,sp,16 63 ret 64 .text 65 .balign 16 66 .globl rv64i_zknd_decrypt 67 .type rv64i_zknd_decrypt,@function 68 rv64i_zknd_decrypt: 69 addi sp,sp,-16 70 sd x8,8(sp) 71 sd x9,0(sp) 72 73 # Load input to block cipher 74 ld x6,0(x10) 75 ld x7,8(x10) 76 77 # Load number of rounds 78 lwu x30,240(x12) 79 80 # Load the last key 81 slli x13,x30,4 82 add x12,x12,x13 83 ld x13,0(x12) 84 ld x14,8(x12) 85 86 xor x6,x6,x13 87 xor x7,x7,x14 88 89 # The main loop only executes the first N-1 rounds. 90 add x30,x30,-1 91 92 # Do Nr - 1 rounds (final round is special) 93 1: 94 .word 1047725107 95 .word 1046709427 96 97 # Update key ptr to point to next key in schedule 98 add x12,x12,-16 99 100 # Grab next key in schedule 101 ld x13,0(x12) 102 ld x14,8(x12) 103 xor x6,x8,x13 104 xor x7,x9,x14 105 106 add x30,x30,-1 107 bgtz x30,1b 108 109 # final round 110 .word 980616243 111 .word 979600563 112 113 add x12,x12,-16 114 ld x13,0(x12) 115 ld x14,8(x12) 116 xor x6,x8,x13 117 xor x7,x9,x14 118 119 sd x6,0(x11) 120 sd x7,8(x11) 121 # Pop registers and return 122 ld x8,8(sp) 123 ld x9,0(sp) 124 addi sp,sp,16 125 ret 126 .text 127 .balign 16 128 .globl rv64i_zkne_set_encrypt_key 129 .type rv64i_zkne_set_encrypt_key,@function 130 rv64i_zkne_set_encrypt_key: 131 addi sp,sp,-16 132 sd x8,0(sp) 133 bnez x10,1f # if (!userKey || !key) return -1; 134 bnez x12,1f 135 li a0,-1 136 ret 137 1: 138 # Determine number of rounds from key size in bits 139 li x6,128 140 bne x11,x6,1f 141 li x7,10 # key->rounds = 10 if bits == 128 142 sw x7,240(x12) # store key->rounds 143 ld x6,0(x10) 144 ld x7,8(x10) 145 sd x6,0(x12) 146 sd x7,8(x12) 147 .word 822318099 148 .word 2120483635 149 .word 2121466803 150 add x12,x12,16 151 sd x6,0(x12) 152 sd x7,8(x12) 153 .word 823366675 154 .word 2120483635 155 .word 2121466803 156 add x12,x12,16 157 sd x6,0(x12) 158 sd x7,8(x12) 159 .word 824415251 160 .word 2120483635 161 .word 2121466803 162 add x12,x12,16 163 sd x6,0(x12) 164 sd x7,8(x12) 165 .word 825463827 166 .word 2120483635 167 .word 2121466803 168 add x12,x12,16 169 sd x6,0(x12) 170 sd x7,8(x12) 171 .word 826512403 172 .word 2120483635 173 .word 2121466803 174 add x12,x12,16 175 sd x6,0(x12) 176 sd x7,8(x12) 177 .word 827560979 178 .word 2120483635 179 .word 2121466803 180 add x12,x12,16 181 sd x6,0(x12) 182 sd x7,8(x12) 183 .word 828609555 184 .word 2120483635 185 .word 2121466803 186 add x12,x12,16 187 sd x6,0(x12) 188 sd x7,8(x12) 189 .word 829658131 190 .word 2120483635 191 .word 2121466803 192 add x12,x12,16 193 sd x6,0(x12) 194 sd x7,8(x12) 195 .word 830706707 196 .word 2120483635 197 .word 2121466803 198 add x12,x12,16 199 sd x6,0(x12) 200 sd x7,8(x12) 201 .word 831755283 202 .word 2120483635 203 .word 2121466803 204 add x12,x12,16 205 sd x6,0(x12) 206 sd x7,8(x12) 207 208 j 4f 209 1: 210 li x6,192 211 bne x11,x6,2f 212 li x7,12 # key->rounds = 12 if bits == 192 213 sw x7,240(x12) # store key->rounds 214 ld x6,0(x10) 215 ld x7,8(x10) 216 ld x8,16(x10) 217 sd x6,0(x12) 218 sd x7,8(x12) 219 sd x8,16(x12) 220 .word 822351507 221 .word 2120647475 222 .word 2121466803 223 .word 2122548275 224 add x12,x12,24 225 sd x6,0(x12) 226 sd x7,8(x12) 227 sd x8,16(x12) 228 .word 823400083 229 .word 2120647475 230 .word 2121466803 231 .word 2122548275 232 add x12,x12,24 233 sd x6,0(x12) 234 sd x7,8(x12) 235 sd x8,16(x12) 236 .word 824448659 237 .word 2120647475 238 .word 2121466803 239 .word 2122548275 240 add x12,x12,24 241 sd x6,0(x12) 242 sd x7,8(x12) 243 sd x8,16(x12) 244 .word 825497235 245 .word 2120647475 246 .word 2121466803 247 .word 2122548275 248 add x12,x12,24 249 sd x6,0(x12) 250 sd x7,8(x12) 251 sd x8,16(x12) 252 .word 826545811 253 .word 2120647475 254 .word 2121466803 255 .word 2122548275 256 add x12,x12,24 257 sd x6,0(x12) 258 sd x7,8(x12) 259 sd x8,16(x12) 260 .word 827594387 261 .word 2120647475 262 .word 2121466803 263 .word 2122548275 264 add x12,x12,24 265 sd x6,0(x12) 266 sd x7,8(x12) 267 sd x8,16(x12) 268 .word 828642963 269 .word 2120647475 270 .word 2121466803 271 .word 2122548275 272 add x12,x12,24 273 sd x6,0(x12) 274 sd x7,8(x12) 275 sd x8,16(x12) 276 .word 829691539 277 .word 2120647475 278 .word 2121466803 279 add x12,x12,24 280 sd x6,0(x12) 281 sd x7,8(x12) 282 283 j 4f 284 2: 285 li x7,14 # key->rounds = 14 if bits == 256 286 li x6,256 287 beq x11,x6,3f 288 li a0,-2 # If bits != 128, 192, or 256, return -2 289 j 5f 290 3: 291 sw x7,240(x12) # store key->rounds 292 ld x6,0(x10) 293 ld x7,8(x10) 294 ld x8,16(x10) 295 ld x13,24(x10) 296 sd x6,0(x12) 297 sd x7,8(x12) 298 sd x8,16(x12) 299 sd x13,24(x12) 300 .word 822515475 301 .word 2120680243 302 .word 2121466803 303 add x12,x12,32 304 sd x6,0(x12) 305 sd x7,8(x12) 306 .word 832804627 307 .word 2122777651 308 .word 2127824563 309 sd x8,16(x12) 310 sd x13,24(x12) 311 .word 823564051 312 .word 2120680243 313 .word 2121466803 314 add x12,x12,32 315 sd x6,0(x12) 316 sd x7,8(x12) 317 .word 832804627 318 .word 2122777651 319 .word 2127824563 320 sd x8,16(x12) 321 sd x13,24(x12) 322 .word 824612627 323 .word 2120680243 324 .word 2121466803 325 add x12,x12,32 326 sd x6,0(x12) 327 sd x7,8(x12) 328 .word 832804627 329 .word 2122777651 330 .word 2127824563 331 sd x8,16(x12) 332 sd x13,24(x12) 333 .word 825661203 334 .word 2120680243 335 .word 2121466803 336 add x12,x12,32 337 sd x6,0(x12) 338 sd x7,8(x12) 339 .word 832804627 340 .word 2122777651 341 .word 2127824563 342 sd x8,16(x12) 343 sd x13,24(x12) 344 .word 826709779 345 .word 2120680243 346 .word 2121466803 347 add x12,x12,32 348 sd x6,0(x12) 349 sd x7,8(x12) 350 .word 832804627 351 .word 2122777651 352 .word 2127824563 353 sd x8,16(x12) 354 sd x13,24(x12) 355 .word 827758355 356 .word 2120680243 357 .word 2121466803 358 add x12,x12,32 359 sd x6,0(x12) 360 sd x7,8(x12) 361 .word 832804627 362 .word 2122777651 363 .word 2127824563 364 sd x8,16(x12) 365 sd x13,24(x12) 366 .word 828806931 367 .word 2120680243 368 .word 2121466803 369 add x12,x12,32 370 sd x6,0(x12) 371 sd x7,8(x12) 372 373 4: # return 0 374 li a0,0 375 5: # return a0 376 ld x8,0(sp) 377 addi sp,sp,16 378 ret 379 .text 380 .balign 16 381 .globl rv64i_zknd_set_decrypt_key 382 .type rv64i_zknd_set_decrypt_key,@function 383 rv64i_zknd_set_decrypt_key: 384 addi sp,sp,-16 385 sd x8,0(sp) 386 bnez x10,1f # if (!userKey || !key) return -1; 387 bnez x12,1f 388 li a0,-1 389 ret 390 1: 391 # Determine number of rounds from key size in bits 392 li x6,128 393 bne x11,x6,1f 394 li x7,10 # key->rounds = 10 if bits == 128 395 sw x7,240(x12) # store key->rounds 396 ld x6,0(x10) 397 ld x7,8(x10) 398 sd x6,0(x12) 399 sd x7,8(x12) 400 .word 822318099 401 .word 2120483635 402 .word 2121466803 403 add x12,x12,16 404 .word 805508115 405 sd x8,0(x12) 406 .word 805540883 407 sd x8,8(x12) 408 .word 823366675 409 .word 2120483635 410 .word 2121466803 411 add x12,x12,16 412 .word 805508115 413 sd x8,0(x12) 414 .word 805540883 415 sd x8,8(x12) 416 .word 824415251 417 .word 2120483635 418 .word 2121466803 419 add x12,x12,16 420 .word 805508115 421 sd x8,0(x12) 422 .word 805540883 423 sd x8,8(x12) 424 .word 825463827 425 .word 2120483635 426 .word 2121466803 427 add x12,x12,16 428 .word 805508115 429 sd x8,0(x12) 430 .word 805540883 431 sd x8,8(x12) 432 .word 826512403 433 .word 2120483635 434 .word 2121466803 435 add x12,x12,16 436 .word 805508115 437 sd x8,0(x12) 438 .word 805540883 439 sd x8,8(x12) 440 .word 827560979 441 .word 2120483635 442 .word 2121466803 443 add x12,x12,16 444 .word 805508115 445 sd x8,0(x12) 446 .word 805540883 447 sd x8,8(x12) 448 .word 828609555 449 .word 2120483635 450 .word 2121466803 451 add x12,x12,16 452 .word 805508115 453 sd x8,0(x12) 454 .word 805540883 455 sd x8,8(x12) 456 .word 829658131 457 .word 2120483635 458 .word 2121466803 459 add x12,x12,16 460 .word 805508115 461 sd x8,0(x12) 462 .word 805540883 463 sd x8,8(x12) 464 .word 830706707 465 .word 2120483635 466 .word 2121466803 467 add x12,x12,16 468 .word 805508115 469 sd x8,0(x12) 470 .word 805540883 471 sd x8,8(x12) 472 .word 831755283 473 .word 2120483635 474 .word 2121466803 475 add x12,x12,16 476 sd x6,0(x12) 477 sd x7,8(x12) 478 479 j 4f 480 1: 481 li x6,192 482 bne x11,x6,2f 483 li x7,12 # key->rounds = 12 if bits == 192 484 sw x7,240(x12) # store key->rounds 485 ld x6,0(x10) 486 ld x7,8(x10) 487 ld x8,16(x10) 488 sd x6,0(x12) 489 sd x7,8(x12) 490 .word 805574291 491 sd x13,16(x12) 492 .word 822351507 493 .word 2120647475 494 .word 2121466803 495 add x12,x12,24 496 .word 805508755 497 sd x13,0(x12) 498 .word 805541523 499 sd x13,8(x12) 500 # the reason is in ke192enc 501 .word 2122548275 502 .word 805574291 503 sd x13,16(x12) 504 .word 823400083 505 .word 2120647475 506 .word 2121466803 507 add x12,x12,24 508 .word 805508755 509 sd x13,0(x12) 510 .word 805541523 511 sd x13,8(x12) 512 # the reason is in ke192enc 513 .word 2122548275 514 .word 805574291 515 sd x13,16(x12) 516 .word 824448659 517 .word 2120647475 518 .word 2121466803 519 add x12,x12,24 520 .word 805508755 521 sd x13,0(x12) 522 .word 805541523 523 sd x13,8(x12) 524 # the reason is in ke192enc 525 .word 2122548275 526 .word 805574291 527 sd x13,16(x12) 528 .word 825497235 529 .word 2120647475 530 .word 2121466803 531 add x12,x12,24 532 .word 805508755 533 sd x13,0(x12) 534 .word 805541523 535 sd x13,8(x12) 536 # the reason is in ke192enc 537 .word 2122548275 538 .word 805574291 539 sd x13,16(x12) 540 .word 826545811 541 .word 2120647475 542 .word 2121466803 543 add x12,x12,24 544 .word 805508755 545 sd x13,0(x12) 546 .word 805541523 547 sd x13,8(x12) 548 # the reason is in ke192enc 549 .word 2122548275 550 .word 805574291 551 sd x13,16(x12) 552 .word 827594387 553 .word 2120647475 554 .word 2121466803 555 add x12,x12,24 556 .word 805508755 557 sd x13,0(x12) 558 .word 805541523 559 sd x13,8(x12) 560 # the reason is in ke192enc 561 .word 2122548275 562 .word 805574291 563 sd x13,16(x12) 564 .word 828642963 565 .word 2120647475 566 .word 2121466803 567 add x12,x12,24 568 .word 805508755 569 sd x13,0(x12) 570 .word 805541523 571 sd x13,8(x12) 572 # the reason is in ke192enc 573 .word 2122548275 574 .word 805574291 575 sd x13,16(x12) 576 .word 829691539 577 .word 2120647475 578 .word 2121466803 579 add x12,x12,24 580 sd x6,0(x12) 581 sd x7,8(x12) 582 583 j 4f 584 2: 585 li x7,14 # key->rounds = 14 if bits == 256 586 li x6,256 587 beq x11,x6,3f 588 li a0,-2 # If bits != 128, 192, or 256, return -2 589 j 5f 590 3: 591 sw x7,240(x12) # store key->rounds 592 ld x6,0(x10) 593 ld x7,8(x10) 594 ld x8,16(x10) 595 ld x13,24(x10) 596 sd x6,0(x12) 597 sd x7,8(x12) 598 .word 805574419 599 sd x14,16(x12) 600 .word 805738259 601 sd x14,24(x12) 602 .word 822515475 603 .word 2120680243 604 .word 2121466803 605 add x12,x12,32 606 .word 832804627 607 .word 2122777651 608 .word 2127824563 609 .word 805508883 610 sd x14,0(x12) 611 .word 805541651 612 sd x14,8(x12) 613 .word 805574419 614 sd x14,16(x12) 615 .word 805738259 616 sd x14,24(x12) 617 .word 823564051 618 .word 2120680243 619 .word 2121466803 620 add x12,x12,32 621 .word 832804627 622 .word 2122777651 623 .word 2127824563 624 .word 805508883 625 sd x14,0(x12) 626 .word 805541651 627 sd x14,8(x12) 628 .word 805574419 629 sd x14,16(x12) 630 .word 805738259 631 sd x14,24(x12) 632 .word 824612627 633 .word 2120680243 634 .word 2121466803 635 add x12,x12,32 636 .word 832804627 637 .word 2122777651 638 .word 2127824563 639 .word 805508883 640 sd x14,0(x12) 641 .word 805541651 642 sd x14,8(x12) 643 .word 805574419 644 sd x14,16(x12) 645 .word 805738259 646 sd x14,24(x12) 647 .word 825661203 648 .word 2120680243 649 .word 2121466803 650 add x12,x12,32 651 .word 832804627 652 .word 2122777651 653 .word 2127824563 654 .word 805508883 655 sd x14,0(x12) 656 .word 805541651 657 sd x14,8(x12) 658 .word 805574419 659 sd x14,16(x12) 660 .word 805738259 661 sd x14,24(x12) 662 .word 826709779 663 .word 2120680243 664 .word 2121466803 665 add x12,x12,32 666 .word 832804627 667 .word 2122777651 668 .word 2127824563 669 .word 805508883 670 sd x14,0(x12) 671 .word 805541651 672 sd x14,8(x12) 673 .word 805574419 674 sd x14,16(x12) 675 .word 805738259 676 sd x14,24(x12) 677 .word 827758355 678 .word 2120680243 679 .word 2121466803 680 add x12,x12,32 681 .word 832804627 682 .word 2122777651 683 .word 2127824563 684 .word 805508883 685 sd x14,0(x12) 686 .word 805541651 687 sd x14,8(x12) 688 .word 805574419 689 sd x14,16(x12) 690 .word 805738259 691 sd x14,24(x12) 692 .word 828806931 693 .word 2120680243 694 .word 2121466803 695 add x12,x12,32 696 sd x6,0(x12) 697 sd x7,8(x12) 698 # last two one dropped 699 700 4: # return 0 701 li a0,0 702 5: # return a0 703 ld x8,0(sp) 704 addi sp,sp,16 705 ret 706