Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 3 | # Copyright (c) 2015, Google Inc. |
| 4 | # |
| 5 | # Permission to use, copy, modify, and/or distribute this software for any |
| 6 | # purpose with or without fee is hereby granted, provided that the above |
| 7 | # copyright notice and this permission notice appear in all copies. |
| 8 | # |
| 9 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 12 | # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 14 | # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 15 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 16 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 17 | use strict; |
| 18 | |
| 19 | my $flavour = shift; |
| 20 | my $output = shift; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 21 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } |
| 22 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 23 | my $win64 = 0; |
| 24 | $win64 = 1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); |
| 25 | |
| 26 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; |
| 27 | my $dir = $1; |
| 28 | my $xlate; |
Robert Sloan | 9254e68 | 2017-04-24 09:42:06 -0700 | [diff] [blame] | 29 | ( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 30 | die "can't locate x86_64-xlate.pl"; |
| 31 | |
| 32 | open OUT,"| \"$^X\" $xlate $flavour $output"; |
| 33 | *STDOUT=*OUT; |
| 34 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 35 | my ($out, $len, $tmp1, $tmp2) = $win64 ? ("%rcx", "%rdx", "%r8", "%r9") |
| 36 | : ("%rdi", "%rsi", "%rdx", "%rcx"); |
| 37 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 38 | print<<___; |
| 39 | .text |
| 40 | |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 41 | # CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to |
| 42 | # |out|. It returns one on success or zero on hardware failure. |
| 43 | # int CRYPTO_rdrand(uint8_t out[8]); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 44 | .globl CRYPTO_rdrand |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 45 | .type CRYPTO_rdrand,\@abi-omnipotent |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 46 | .align 16 |
| 47 | CRYPTO_rdrand: |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 48 | .cfi_startproc |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 49 | xorq %rax, %rax |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 50 | rdrand $tmp1 |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 51 | # An add-with-carry of zero effectively sets %rax to the carry flag. |
| 52 | adcq %rax, %rax |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 53 | movq $tmp1, 0($out) |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 54 | retq |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 55 | .cfi_endproc |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 56 | .size CRYPTO_rdrand,.-CRYPTO_rdrand |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 57 | |
| 58 | # CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from |
| 59 | # the hardware RNG. The |len| argument must be a multiple of eight. It returns |
| 60 | # one on success and zero on hardware failure. |
| 61 | # int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); |
| 62 | .globl CRYPTO_rdrand_multiple8_buf |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 63 | .type CRYPTO_rdrand_multiple8_buf,\@abi-omnipotent |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 64 | .align 16 |
| 65 | CRYPTO_rdrand_multiple8_buf: |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 66 | .cfi_startproc |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 67 | test $len, $len |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 68 | jz .Lout |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 69 | movq \$8, $tmp1 |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 70 | .Lloop: |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 71 | rdrand $tmp2 |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 72 | jnc .Lerr |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 73 | movq $tmp2, 0($out) |
| 74 | addq $tmp1, $out |
| 75 | subq $tmp1, $len |
Adam Langley | bd9957e | 2015-06-23 16:28:07 -0700 | [diff] [blame] | 76 | jnz .Lloop |
| 77 | .Lout: |
| 78 | movq \$1, %rax |
| 79 | retq |
| 80 | .Lerr: |
| 81 | xorq %rax, %rax |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 82 | retq |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 83 | .cfi_endproc |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 84 | .size CRYPTO_rdrand_multiple8_buf,.-CRYPTO_rdrand_multiple8_buf |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 85 | ___ |
| 86 | |
Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 87 | close STDOUT or die "error closing STDOUT"; # flush |