Home | History | Annotate | Download | only in asm
      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 
     17 $flavour = shift;
     18 $output  = shift;
     19 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
     20 
     21 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
     22 ( $xlate="${dir}../../../perlasm/x86_64-xlate.pl" and -f $xlate) or
     23 die "can't locate x86_64-xlate.pl";
     24 
     25 open OUT,"| \"$^X\" $xlate $flavour $output";
     26 *STDOUT=*OUT;
     27 
     28 print<<___;
     29 .text
     30 
     31 # CRYPTO_rdrand writes eight bytes of random data from the hardware RNG to
     32 # |out|. It returns one on success or zero on hardware failure.
     33 # int CRYPTO_rdrand(uint8_t out[8]);
     34 .globl	CRYPTO_rdrand
     35 .type	CRYPTO_rdrand,\@function,1
     36 .align	16
     37 CRYPTO_rdrand:
     38 	xorq %rax, %rax
     39 	# This is rdrand %rcx. It sets rcx to a random value and sets the carry
     40 	# flag on success.
     41 	.byte 0x48, 0x0f, 0xc7, 0xf1
     42 	# An add-with-carry of zero effectively sets %rax to the carry flag.
     43 	adcq %rax, %rax
     44 	movq %rcx, 0(%rdi)
     45 	retq
     46 
     47 # CRYPTO_rdrand_multiple8_buf fills |len| bytes at |buf| with random data from
     48 # the hardware RNG. The |len| argument must be a multiple of eight. It returns
     49 # one on success and zero on hardware failure.
     50 # int CRYPTO_rdrand_multiple8_buf(uint8_t *buf, size_t len);
     51 .globl CRYPTO_rdrand_multiple8_buf
     52 .type CRYPTO_rdrand_multiple8_buf,\@function,2
     53 .align 16
     54 CRYPTO_rdrand_multiple8_buf:
     55 	test %rsi, %rsi
     56 	jz .Lout
     57 	movq \$8, %rdx
     58 .Lloop:
     59 	# This is rdrand %rcx. It sets rcx to a random value and sets the carry
     60 	# flag on success.
     61 	.byte 0x48, 0x0f, 0xc7, 0xf1
     62 	jnc .Lerr
     63 	movq %rcx, 0(%rdi)
     64 	addq %rdx, %rdi
     65 	subq %rdx, %rsi
     66 	jnz .Lloop
     67 .Lout:
     68 	movq \$1, %rax
     69 	retq
     70 .Lerr:
     71 	xorq %rax, %rax
     72 	retq
     73 ___
     74 
     75 close STDOUT;	# flush
     76