• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	xorq %rax, %rax
50	rdrand $tmp1
51	# An add-with-carry of zero effectively sets %rax to the carry flag.
52	adcq %rax, %rax
53	movq $tmp1, 0($out)
54	retq
55.cfi_endproc
56.size CRYPTO_rdrand,.-CRYPTO_rdrand
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
63.type CRYPTO_rdrand_multiple8_buf,\@abi-omnipotent
64.align 16
65CRYPTO_rdrand_multiple8_buf:
66.cfi_startproc
67	test $len, $len
68	jz .Lout
69	movq \$8, $tmp1
70.Lloop:
71	rdrand $tmp2
72	jnc .Lerr
73	movq $tmp2, 0($out)
74	addq $tmp1, $out
75	subq $tmp1, $len
76	jnz .Lloop
77.Lout:
78	movq \$1, %rax
79	retq
80.Lerr:
81	xorq %rax, %rax
82	retq
83.cfi_endproc
84.size CRYPTO_rdrand_multiple8_buf,.-CRYPTO_rdrand_multiple8_buf
85___
86
87close STDOUT;	# flush
88