Blame cbits/cryptonite_rdrand.c

Packit 141393
/*
Packit 141393
 * Copyright (C) Thomas DuBuisson
Packit 141393
 * Copyright (C) 2013 Vincent Hanquez <tab@snarc.org>
Packit 141393
 *
Packit 141393
 * All rights reserved.
Packit 141393
 * 
Packit 141393
 * Redistribution and use in source and binary forms, with or without
Packit 141393
 * modification, are permitted provided that the following conditions
Packit 141393
 * are met:
Packit 141393
 * 1. Redistributions of source code must retain the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer.
Packit 141393
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 141393
 *    notice, this list of conditions and the following disclaimer in the
Packit 141393
 *    documentation and/or other materials provided with the distribution.
Packit 141393
 * 3. Neither the name of the author nor the names of his contributors
Packit 141393
 *    may be used to endorse or promote products derived from this software
Packit 141393
 *    without specific prior written permission.
Packit 141393
 * 
Packit 141393
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 141393
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 141393
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 141393
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
Packit 141393
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 141393
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 141393
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 141393
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 141393
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 141393
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 141393
 * SUCH DAMAGE.
Packit 141393
 */
Packit 141393
Packit 141393
#include <stdint.h>
Packit 141393
#include <stdlib.h>
Packit 141393
#include <stdio.h>
Packit 141393
#include <string.h>
Packit 141393
Packit 141393
int cryptonite_cpu_has_rdrand()
Packit 141393
{
Packit 141393
	uint32_t ax,bx,cx,dx,func=1;
Packit 141393
#if defined(__PIC__) && defined(__i386__)
Packit 141393
	__asm__ volatile ("mov %%ebx, %%edi;" "cpuid;" "xchgl %%ebx, %%edi;"
Packit 141393
		: "=a" (ax), "=D" (bx), "=c" (cx), "=d" (dx) : "a" (func));
Packit 141393
#else
Packit 141393
	__asm__ volatile ("cpuid": "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
Packit 141393
#endif
Packit 141393
	return (cx & 0x40000000);
Packit 141393
}
Packit 141393
Packit 141393
/* inline encoding of 'rdrand %rax' to cover old binutils
Packit 141393
 * - no inputs
Packit 141393
 * - 'cc' to the clobber list as we modify condition code.
Packit 141393
 * - output of rdrand in rax and have a 8 bit error condition
Packit 141393
 */
Packit 141393
#define inline_rdrand_rax(val, err) \
Packit 141393
	asm(".byte 0x48,0x0f,0xc7,0xf0; setc %1" \
Packit 141393
	   : "=a" (val), "=q" (err) \
Packit 141393
	   : \
Packit 141393
	   : "cc")
Packit 141393
Packit 141393
/* inline encoding of 'rdrand %eax' to cover old binutils
Packit 141393
 * - no inputs
Packit 141393
 * - 'cc' to the clobber list as we modify condition code.
Packit 141393
 * - output of rdrand in eax and have a 8 bit error condition
Packit 141393
 */
Packit 141393
#define inline_rdrand_eax(val, err) \
Packit 141393
	asm(".byte 0x0f,0xc7,0xf0; setc %1" \
Packit 141393
	   : "=a" (val), "=q" (err) \
Packit 141393
	   : \
Packit 141393
	   : "cc")
Packit 141393
Packit 141393
#ifdef __x86_64__
Packit 141393
# define RDRAND_SZ 8
Packit 141393
# define RDRAND_T  uint64_t
Packit 141393
#define inline_rdrand(val, err) err = cryptonite_rdrand_step(&val)
Packit 141393
#else
Packit 141393
# define RDRAND_SZ 4
Packit 141393
# define RDRAND_T  uint32_t
Packit 141393
#define inline_rdrand(val, err) err = cryptonite_rdrand_step(&val)
Packit 141393
#endif
Packit 141393
Packit 141393
/* sadly many people are still using an old binutils,
Packit 141393
 * leading to report that instruction is not recognized.
Packit 141393
 */
Packit 141393
#if 1
Packit 141393
/* Returns 1 on success */
Packit 141393
static inline int cryptonite_rdrand_step(RDRAND_T *buffer)
Packit 141393
{
Packit 141393
	unsigned char err;
Packit 141393
	asm volatile ("rdrand %0; setc %1" : "=r" (*buffer), "=qm" (err));
Packit 141393
	return (int) err;
Packit 141393
}
Packit 141393
#endif
Packit 141393
Packit 141393
/* Returns the number of bytes succesfully generated */
Packit 141393
int cryptonite_get_rand_bytes(uint8_t *buffer, size_t len)
Packit 141393
{
Packit 141393
	RDRAND_T tmp;
Packit 141393
	int aligned = (intptr_t) buffer % RDRAND_SZ;
Packit 141393
	int orig_len = len;
Packit 141393
	int to_alignment = RDRAND_SZ - aligned;
Packit 141393
	uint8_t ok;
Packit 141393
Packit 141393
	if (aligned != 0) {
Packit 141393
		inline_rdrand(tmp, ok);
Packit 141393
		if (!ok)
Packit 141393
			return 0;
Packit 141393
		memcpy(buffer, (uint8_t *) &tmp, to_alignment);
Packit 141393
		buffer += to_alignment;
Packit 141393
		len -= to_alignment;
Packit 141393
	}
Packit 141393
Packit 141393
	for (; len >= RDRAND_SZ; buffer += RDRAND_SZ, len -= RDRAND_SZ) {
Packit 141393
		inline_rdrand(tmp, ok);
Packit 141393
		if (!ok)
Packit 141393
			return (orig_len - len);
Packit 141393
		*((RDRAND_T *) buffer) = tmp;
Packit 141393
	}
Packit 141393
Packit 141393
	if (len > 0) {
Packit 141393
		inline_rdrand(tmp, ok);
Packit 141393
		if (!ok)
Packit 141393
			return (orig_len - len);
Packit 141393
		memcpy(buffer, (uint8_t *) &tmp, len);
Packit 141393
	}
Packit 141393
	return orig_len;
Packit 141393
}