From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15898 invoked by alias); 13 Aug 2014 08:55:38 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 15882 invoked by uid 89); 13 Aug 2014 08:55:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.7 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW,RP_MATCHES_RCVD,SPF_PASS,URIBL_BLACK autolearn=no version=3.3.2 X-HELO: mail.lysator.liu.se Received: from mail.lysator.liu.se (HELO mail.lysator.liu.se) (130.236.254.3) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Wed, 13 Aug 2014 08:55:35 +0000 Received: from mail.lysator.liu.se (localhost [127.0.0.1]) by mail.lysator.liu.se (Postfix) with ESMTP id DEA1C4002C for ; Wed, 13 Aug 2014 10:55:30 +0200 (CEST) Received: from [192.168.0.68] (217-210-101-82-no95.business.telia.com [217.210.101.82]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by mail.lysator.liu.se (Postfix) with ESMTPSA id 906154002B for ; Wed, 13 Aug 2014 10:55:30 +0200 (CEST) Message-ID: <53EB2801.8080809@lysator.liu.se> Date: Wed, 13 Aug 2014 08:55:00 -0000 From: Peter Rosin User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: cygwin@cygwin.com Subject: Re: [ANNOUNCEMENT] New package: rng-tools-5-1 References: <53EA16B9.5020402@lysator.liu.se> <20140812141135.GA30002@calimero.vinschen.de> In-Reply-To: <20140812141135.GA30002@calimero.vinschen.de> Content-Type: multipart/mixed; boundary="------------010707080406070604070806" X-SW-Source: 2014-08/txt/msg00240.txt.bz2 --------------010707080406070604070806 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-length: 4074 On 2014-08-12 16:11, Corinna Vinschen wrote: > Hi Peter, > > On Aug 12 15:29, Peter Rosin wrote: >> On 2014-08-09 16:37, Corinna Vinschen wrote: >>> I just uploaded rng-tools-5-1. >>> >>> The Cygwin release only comes with the rngtest tool for now. >>> >>> The rngd daemon requires porting assembler code to COFF and the >>> Microsoft calling convention. Any help porting this code would >>> be greatly appreciated. >> >> Ok, I took a stab at it. The problems I identified in the assembly >> are ELF debug info, different register use for the x86-64 calls and >> a missing underscore prefix for the i686 symbols. >> >> I'm unsure if used registers (and which) have to be saved in the >> MS x86-64 ABI, but that shouldn't be too hard to fix if that's the >> case. I found out that I need to preserve (at least) %rdi and %rsi in the callee. >> I also moved up the AC_SEARCH_LIBS hunk in configure.ac since >> the existing AC_CHECK_LIB is buried inside some other construct >> (AC_CHECK_HEADER is possibly the culprit) which causes this: >> >> checking for library containing argp_parse... /usr/src/rng-tools-5-1.src/rng-tools-5-1.i686/src/rng-tools-5/configure: line 4335: ac_fn_c_try_link: command not found >> /usr/src/rng-tools-5-1.src/rng-tools-5-1.i686/src/rng-tools-5/configure: line 4335: ac_fn_c_try_link: command not found >> no >> >> Anyway, with the attached patch instead of the one included in the >> src package, it builds for both arches, but my cpu appears to lack >> the rdrand instruction, so I have a hard time taking this any >> further. Bummer. > > Thanks for your efforts! Over the weekend I tried my own port. I opted > for creating a new file, rdrand_win_asm.S (attached for reference) to > keep the code a bit cleaner. And I didn't want to fork it, for easier maintenance. Your version ought to be faster though, without all the thunking going on in my version. > I have a machine which supports the rdrand call, but you need at least > an Ivy Bridge CPU, For rdseed you need at least Haswell. I found an Haswell upstairs (but no Broadwell, so still no rdseed). For completeness, I'm attaching a version of my patch that makes it actually run. > Ultimately I gave up on rngd for now, for four reasons: > > - rngd uses poll(2) on /dev/random to wait until /dev/random becomes > writable. /dev/random on Cygwin is always writable (we're not > controlling the entropy pool, the OS does, and the RtlGenRandom call > never blocks). This results in 100% CPU usage. Yes, I saw that full core usage as well when I ran rngd... > - Even then, using rngd on /dev/random gave *worse* results when > testing /dev/random with rngtest :-P I'm not sure why. Yes, I saw that too. Maybe the reason is that if you could get a better PRNG by adding a feedback of the output to the entropy pool, that would already be part of the PRNG? I'm not into PRNGs though... > - Cygwin does not support any of the other three hardware entropy > sources /dev/hwrng or /dev/tpm0. For Intel/AMD hwrng you'd need > access to the PCI bus and certain chipsets. For tpm0 you'd > need a TPM chip and a description how to access the chip for > producing random numbers. The chip is supposedly available as > cryptographic provider under Windows, but on the only machine > in our home with a TPM chip *and* a functional Windows driver, > there was no matching cryptographic provider returned by the call > to CryptEnumProviders. Sorry, I have no input on the other HW entropy sources. > - Given that, and given the hardware constraints for the rdrand and > rdseed calls, I decided that it's not worth to follow through with > this stuff. > > Still, thanks a lot for working on that. I appreciate it. If you > have any idea how Cygwin could provide /dev/hwrng or /dev/tpm0 to > have at least two HW entropy sources, please feel free to discuss > this on the cygwin-developer's list. This seemed like something I could waste a little time on, and learn something in the process. Which I did, so not all is lost. :-) Cheers, Peter --------------010707080406070604070806 Content-Type: text/x-patch; name="cygwin-rng-tools-5-peda.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="cygwin-rng-tools-5-peda.patch" Content-length: 4334 diff -rup origsrc/rng-tools-5/configure.ac src/rng-tools-5/configure.ac --- origsrc/rng-tools-5/configure.ac 2014-08-12 10:33:32.064585400 +0200 +++ src/rng-tools-5/configure.ac 2014-08-12 11:18:44.431782000 +0200 @@ -56,6 +56,8 @@ dnl ------------------------------------ dnl Checks for optional library functions dnl ------------------------------------- +AC_SEARCH_LIBS([argp_parse],[argp]) + dnl ------------------------------------- dnl Check for libgcrypt support dnl ------------------------------------- diff -rup origsrc/rng-tools-5/rdrand_asm.S src/rng-tools-5/rdrand_asm.S --- origsrc/rng-tools-5/rdrand_asm.S 2014-08-13 10:16:08.499091900 +0200 +++ src/rng-tools-5/rdrand_asm.S 2014-08-13 10:12:40.745403500 +0200 @@ -20,20 +20,43 @@ #if defined(__i386__) || defined(__x86_64__) -#define ENTRY(x) \ - .balign 64 ; \ - .globl x ; \ -x: +#if defined __CYGWIN__ +# if defined __x86_64__ +# define MS_x86_64_ABI +# else +# define SYMBOL(name) _ ## name +# endif +#else +# define ELF_DEBUG_INFO +#endif +#if !defined SYMBOL +# define SYMBOL(name) name +#endif + +#define ENTRY(x) \ + .balign 64 ; \ + .globl SYMBOL(x) ; \ +SYMBOL(x): +#if defined ELF_DEBUG_INFO #define ENDPROC(x) \ .size x, .-x ; \ .type x, @function +#else +#define ENDPROC(x) +#endif #define RDRAND_RETRY_LIMIT 10 #ifdef __x86_64__ ENTRY(x86_rdrand_bytes) +#if defined MS_x86_64_ABI + push %rdi + push %rsi + mov %rcx, %rdi + mov %rdx, %rsi +#endif mov %esi, %eax 1: mov $RDRAND_RETRY_LIMIT, %ecx @@ -46,6 +69,10 @@ ENTRY(x86_rdrand_bytes) ja 1b 4: sub %esi, %eax +#if defined MS_x86_64_ABI + pop %rsi + pop %rdi +#endif ret 3: dec %ecx @@ -55,6 +82,14 @@ ENTRY(x86_rdrand_bytes) ENDPROC(x86_rdrand_bytes) ENTRY(x86_rdseed_or_rdrand_bytes) +#if defined MS_x86_64_ABI + push %rdi + push %rsi + mov %rcx, %rdi + mov %rdx, %rsi + mov %r8, %rdx + mov %r9, %rcx +#endif mov (%rsi), %r8d /* RDSEED count */ mov (%rcx), %r9d /* RDRAND count */ 1: @@ -69,6 +104,10 @@ ENTRY(x86_rdseed_or_rdrand_bytes) 4: sub %r8d, (%rsi) sub %r9d, (%rcx) +#if defined MS_x86_64_ABI + pop %rsi + pop %rdi +#endif ret 3: .byte 0x48,0x0f,0xc7,0xf0 /* rdrand %rax */ @@ -191,6 +230,12 @@ ENTRY(x86_aes_mangle) movl 12(%ebp), %edx push %esi #endif +#if defined MS_x86_64_ABI + push %rdi + push %rsi + mov %rcx, %rdi + mov %rdx, %rsi +#endif movl $512, CTR3 /* Number of rounds */ movdqa (0*16)(PTR1), %xmm0 @@ -283,6 +328,10 @@ offset = offset + 16 pop %esi pop %ebp #endif +#if defined MS_x86_64_ABI + pop %rsi + pop %rdi +#endif ret ENDPROC(x86_aes_mangle) @@ -295,6 +344,11 @@ ENTRY(x86_aes_expand_key) mov %esp, %ebp movl 8(%ebp), %eax #endif +#if defined MS_x86_64_ABI + push %rdi + push %rsi + mov %rcx, %rdi +#endif SETPTR(aes_round_keys, PTR1) movdqu (PTR0), %xmm0 @@ -325,6 +379,10 @@ ENTRY(x86_aes_expand_key) #ifdef __i386__ pop %ebp #endif +#if defined MS_x86_64_ABI + pop %rsi + pop %rdi +#endif ret 1: @@ -347,12 +405,16 @@ ENDPROC(x86_aes_expand_key) .balign 64 aes_round_keys: .space 11*16 +#if defined ELF_DEBUG_INFO .size aes_round_keys, .-aes_round_keys +#endif /* ELF_DEBUG_INFO */ #endif /* i386 or x86_64 */ +#if defined ELF_DEBUG_INFO /* * This is necessary to keep the whole executable * from needing a writable stack. */ .section .note.GNU-stack,"",%progbits +#endif /* ELF_DEBUG_INFO */ diff -rup origsrc/rng-tools-5/rngd_linux.c src/rng-tools-5/rngd_linux.c --- origsrc/rng-tools-5/rngd_linux.c 2012-08-06 19:04:12.000000000 +0200 +++ src/rng-tools-5/rngd_linux.c 2014-08-09 15:09:21.081616358 +0200 @@ -39,8 +39,10 @@ #include #include #include +#ifndef __CYGWIN__ #include #include +#endif #include #include "rngd.h" @@ -130,11 +132,19 @@ void random_add_entropy(void *buf, size_ entropy.size = size; memcpy(entropy.data, buf, size); +#ifdef __CYGWIN__ + if (write(random_fd, entropy.data, size) != size) { + message(LOG_DAEMON|LOG_ERR, "Add Entropy failed: %s\n", + strerror(errno)); + exit(1); + } +#else if (ioctl(random_fd, RNDADDENTROPY, &entropy) != 0) { message(LOG_DAEMON|LOG_ERR, "RNDADDENTROPY failed: %s\n", strerror(errno)); exit(1); } +#endif } void random_sleep(void) --------------010707080406070604070806 Content-Type: text/plain; charset=us-ascii Content-length: 218 -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple --------------010707080406070604070806--