diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2020-06-14 16:00:12 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2020-06-14 16:00:12 +0000 |
commit | 00176e7d0b795c1b833163e4d5bcdefbddd2c8e1 (patch) | |
tree | 103cbd8f81d29680748b758bc2db5674711912b2 /sys/arch/amd64/stand/libsa | |
parent | ffc279c008fc0d2caf9156ec2f03011e18a911b8 (diff) |
rewrite mdrandom() in C. previously this XOR'd against rdrand if available,
and alternatively XOR'd against TSC. now always run both sequences, and
also support rdseed as a third procedure.
ok kettenis naddy
Diffstat (limited to 'sys/arch/amd64/stand/libsa')
-rw-r--r-- | sys/arch/amd64/stand/libsa/mdrandom.c | 63 | ||||
-rw-r--r-- | sys/arch/amd64/stand/libsa/random_amd64.S | 110 | ||||
-rw-r--r-- | sys/arch/amd64/stand/libsa/random_i386.S | 110 |
3 files changed, 63 insertions, 220 deletions
diff --git a/sys/arch/amd64/stand/libsa/mdrandom.c b/sys/arch/amd64/stand/libsa/mdrandom.c new file mode 100644 index 00000000000..80634bde2a6 --- /dev/null +++ b/sys/arch/amd64/stand/libsa/mdrandom.c @@ -0,0 +1,63 @@ +/* $OpenBSD: mdrandom.c,v 1.1 2020/06/14 16:00:11 deraadt Exp $ */ + +/* + * Copyright (c) 2020 Theo de Raadt + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/param.h> +#include <machine/psl.h> +#include <machine/specialreg.h> + +#include "libsa.h" + +int +mdrandom(char *buf, size_t buflen) +{ + u_int eax, ebx, ecx, edx; + uint32_t hi, lo; + int i; + + for (i = 0; i < buflen; i++) { + __asm volatile("rdtsc" : "=d" (hi), "=a" (lo)); + hi ^= (hi >> 8) ^ (hi >> 16) ^ (hi >> 24); + lo ^= (lo >> 8) ^ (lo >> 16) ^ (lo >> 24); + buf[i] ^= hi; + buf[i] ^= lo; + } + + CPUID(1, eax, ebx, ecx, edx); + if (ecx & CPUIDECX_RDRAND) { + unsigned long rand; + + for (i = 0; i < buflen / sizeof(rand); i++) { + __asm volatile("rdrand %0\n" : "=r" (rand)); + ((unsigned long *)buf)[i] ^= rand; + } + } + + CPUID(0, eax, ebx, ecx, edx); + if (eax >= 7) { + CPUID_LEAF(7, 0, eax, ebx, ecx, edx); + if (ebx & SEFF0EBX_RDSEED) { + unsigned long rand; + + for (i = 0; i < buflen / sizeof(rand); i++) { + __asm volatile("rdseed %0\n" : "=r" (rand)); + ((unsigned long *)buf)[i] ^= rand; + } + } + } + return (0); +} diff --git a/sys/arch/amd64/stand/libsa/random_amd64.S b/sys/arch/amd64/stand/libsa/random_amd64.S index 7c12659ae55..e69de29bb2d 100644 --- a/sys/arch/amd64/stand/libsa/random_amd64.S +++ b/sys/arch/amd64/stand/libsa/random_amd64.S @@ -1,110 +0,0 @@ -/* $OpenBSD: random_amd64.S,v 1.6 2020/05/25 14:58:01 deraadt Exp $ */ - -/* - * Copyright (c) 2013 Joel Sing <jsing@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <machine/param.h> -#include <machine/asm.h> -#include <machine/psl.h> -#include <machine/specialreg.h> - -/* - * Random data is xored into the buffer in 8-byte blocks. If the buffer size - * is not a multiple of 8, the remaining bytes may be left untouched. - */ -ENTRY(mdrandom) - pushq %rbx - - // See if we have CPU identification. - pushf - pop %rax - mov %eax, %ecx - or $PSL_ID, %eax - push %rax - popf - pushf - pop %rax - push %rcx - popf - and $PSL_ID, %eax - movq $-1, %rax - jz done - - // CPUID leaf = 1, subleaf = 0 - mov $1, %eax - mov $0, %ecx - cpuid - mov %edx, %eax - - movq %rdi, %rbx - movq %rsi, %rdx - xorq %rdi, %rdi - - and $CPUIDECX_RDRAND, %ecx // See if we have rdrand. - jnz userand - - mov %edx, %ecx - and $CPUID_TSC, %eax // See if we have rdtsc. - jnz usetsc - - movq $-1, %rax - jmp done - -userand: - shrq $3, %rdx // 8 bytes at a time -1: - rdrand %rax - xorq %rax, (%rbx, %rdi, 8) - incq %rdi - cmpq %rdi, %rdx - jne 1b - movq $0, %rax - jmp done - -usetsc: - rdtsc // Populates edx:eax. - - /* - * Cope with high=0 - * high = (high << 1) | 1; - * Spread bits - * bits = low * high; - * Accumulate spread bits into a byte - * bits = bits ^ (bits>>8) ^ (bits>>16) ^ (bits>>24); - * buf[i] ^= (u_char) bits; - */ - shlq $1, %rdx - orq $1, %rdx - mull %edx - movq %rax, %rdx - shrq $8, %rdx - xorq %rdx, %rax - shrq $8, %rdx - xorq %rdx, %rax - shrq $8, %rdx - xorq %rdx, %rax - - xorb %al, (%rbx,%rdi) - - incq %rdi - cmpq %rdi, %rcx - jne usetsc - movq $0, %rax - jmp done - -done: - popq %rbx - retq diff --git a/sys/arch/amd64/stand/libsa/random_i386.S b/sys/arch/amd64/stand/libsa/random_i386.S index 36bf82e34bc..e69de29bb2d 100644 --- a/sys/arch/amd64/stand/libsa/random_i386.S +++ b/sys/arch/amd64/stand/libsa/random_i386.S @@ -1,110 +0,0 @@ -/* $OpenBSD: random_i386.S,v 1.11 2020/05/25 14:58:01 deraadt Exp $ */ - -/* - * Copyright (c) 2013 Joel Sing <jsing@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <machine/param.h> -#include <machine/asm.h> -#include <machine/psl.h> -#include <machine/specialreg.h> - -/* - * Random data is xored into the buffer in 4 byte blocks. If the buffer size - * is not a multiple of 4, the remaining bytes may be left untouched. - */ -ENTRY(mdrandom) - pushal - - // See if we have CPU identification. - pushfl - popl %eax - movl %eax, %ecx - orl $PSL_ID, %eax - pushl %eax - popfl - pushfl - popl %eax - pushl %ecx - popfl - andl $PSL_ID, %eax - movl $-1, %eax - jz done - - // CPUID leaf = 1, subleaf = 0 - movl $1, %eax - movl $0, %ecx - cpuid - movl %edx, %eax - - movl 36(%esp), %ebx - movl 40(%esp), %edx - xorl %edi, %edi - - andl $CPUIDECX_RDRAND, %ecx // See if we have rdrand. - jnz userand - - movl %edx, %ecx - andl $CPUID_TSC, %eax // See if we have rdtsc. - jnz usetsc - - movl $-1, %eax - jmp done - -userand: - shrl $2, %edx // 4 bytes at a time -1: - rdrand %eax - xorl %eax, (%ebx,%edi,4) - incl %edi - cmpl %edi, %edx - jne 1b - movl $0, %eax - jmp done - -usetsc: - rdtsc // Populates edx:eax. - - /* - * Cope with high=0 - * high = (high << 1) | 1; - * Spread bits - * bits = low * high; - * Accumulate spread bits into a byte - * bits = bits ^ (bits>>8) ^ (bits>>16) ^ (bits>>24); - * buf[i] ^= (u_char) bits; - */ - shll $1, %edx - orl $1, %edx - mull %edx - movl %eax, %edx - shrl $8, %edx - xorl %edx, %eax - shrl $8, %edx - xorl %edx, %eax - shrl $8, %edx - xorl %edx, %eax - - xorb %al, (%ebx,%edi) - - incl %edi - cmpl %edi, %ecx - jne usetsc - movl $0, %eax - jmp done - -done: - popal - ret |