1#!/usr/bin/env perl 2 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 17use strict; 18 19my $flavour = shift; 20my $output = shift; 21if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } 22 23my $win64 = 0; 24$win64 = 1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); 25 26$0 =~ m/(.*[\/\\])[^\/\\]+$/; 27my $dir = $1; 28my $xlate; 29( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or 30die "can't locate x86_64-xlate.pl"; 31 32open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""; 33*STDOUT=*OUT; 34 35my ($out, $len, $tmp1, $tmp2) = $win64 ? ("%rcx", "%rdx", "%r8", "%r9") 36 : ("%rdi", "%rsi", "%rdx", "%rcx"); 37 38print<<___; 39.text 40 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]); 44.globl CRYPTO_rdrand 45.type CRYPTO_rdrand,\@abi-omnipotent 46.align 16 47CRYPTO_rdrand: 48.cfi_startproc 49 _CET_ENDBR 50 xorq %rax, %rax 51 rdrand $tmp1 52 # An add-with-carry of zero effectively sets %rax to the carry flag. 53 adcq %rax, %rax 54 movq $tmp1, 0($out) 55 retq 56.cfi_endproc 57.size CRYPTO_rdrand,.-CRYPTO_rdrand 58 59# CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from 60# the hardware RNG. The |len| argument must be a multiple of eight. It returns 61# one on success and zero on hardware failure. 62# int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len); 63.globl CRYPTO_rdrand_multiple8_buf 64.type CRYPTO_rdrand_multiple8_buf,\@abi-omnipotent 65.align 16 66CRYPTO_rdrand_multiple8_buf: 67.cfi_startproc 68 _CET_ENDBR 69 test $len, $len 70 jz .Lout 71 movq \$8, $tmp1 72.Lloop: 73 rdrand $tmp2 74 jnc .Lerr 75 movq $tmp2, 0($out) 76 addq $tmp1, $out 77 subq $tmp1, $len 78 jnz .Lloop 79.Lout: 80 movq \$1, %rax 81 retq 82.Lerr: 83 xorq %rax, %rax 84 retq 85.cfi_endproc 86.size CRYPTO_rdrand_multiple8_buf,.-CRYPTO_rdrand_multiple8_buf 87___ 88 89close STDOUT or die "error closing STDOUT: $!"; # flush 90