1*5697Smcpowers /*
2*5697Smcpowers * mpprime.c
3*5697Smcpowers *
4*5697Smcpowers * Utilities for finding and working with prime and pseudo-prime
5*5697Smcpowers * integers
6*5697Smcpowers *
7*5697Smcpowers * ***** BEGIN LICENSE BLOCK *****
8*5697Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9*5697Smcpowers *
10*5697Smcpowers * The contents of this file are subject to the Mozilla Public License Version
11*5697Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
12*5697Smcpowers * the License. You may obtain a copy of the License at
13*5697Smcpowers * http://www.mozilla.org/MPL/
14*5697Smcpowers *
15*5697Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
16*5697Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17*5697Smcpowers * for the specific language governing rights and limitations under the
18*5697Smcpowers * License.
19*5697Smcpowers *
20*5697Smcpowers * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
21*5697Smcpowers *
22*5697Smcpowers * The Initial Developer of the Original Code is
23*5697Smcpowers * Michael J. Fromberger.
24*5697Smcpowers * Portions created by the Initial Developer are Copyright (C) 1997
25*5697Smcpowers * the Initial Developer. All Rights Reserved.
26*5697Smcpowers *
27*5697Smcpowers * Contributor(s):
28*5697Smcpowers * Netscape Communications Corporation
29*5697Smcpowers *
30*5697Smcpowers * Alternatively, the contents of this file may be used under the terms of
31*5697Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
32*5697Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33*5697Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
34*5697Smcpowers * of those above. If you wish to allow use of your version of this file only
35*5697Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
36*5697Smcpowers * use your version of this file under the terms of the MPL, indicate your
37*5697Smcpowers * decision by deleting the provisions above and replace them with the notice
38*5697Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
39*5697Smcpowers * the provisions above, a recipient may use your version of this file under
40*5697Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
41*5697Smcpowers *
42*5697Smcpowers * ***** END LICENSE BLOCK ***** */
43*5697Smcpowers /*
44*5697Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
45*5697Smcpowers * Use is subject to license terms.
46*5697Smcpowers *
47*5697Smcpowers * Sun elects to use this software under the MPL license.
48*5697Smcpowers */
49*5697Smcpowers
50*5697Smcpowers #pragma ident "%Z%%M% %I% %E% SMI"
51*5697Smcpowers
52*5697Smcpowers #include "mpi-priv.h"
53*5697Smcpowers #include "mpprime.h"
54*5697Smcpowers #include "mplogic.h"
55*5697Smcpowers #ifndef _KERNEL
56*5697Smcpowers #include <stdlib.h>
57*5697Smcpowers #include <string.h>
58*5697Smcpowers #else
59*5697Smcpowers #include <sys/random.h>
60*5697Smcpowers #endif
61*5697Smcpowers
62*5697Smcpowers #define SMALL_TABLE 0 /* determines size of hard-wired prime table */
63*5697Smcpowers
64*5697Smcpowers #ifndef _KERNEL
65*5697Smcpowers #define RANDOM() rand()
66*5697Smcpowers #else
67*5697Smcpowers #define RANDOM() foo_rand()
68*5697Smcpowers
69*5697Smcpowers static int
foo_rand()70*5697Smcpowers foo_rand()
71*5697Smcpowers {
72*5697Smcpowers int r;
73*5697Smcpowers random_get_pseudo_bytes((uchar_t *)&r, sizeof (r));
74*5697Smcpowers return (r);
75*5697Smcpowers }
76*5697Smcpowers #endif
77*5697Smcpowers
78*5697Smcpowers /*
79*5697Smcpowers mpp_random(a)
80*5697Smcpowers
81*5697Smcpowers Assigns a random value to a. This value is generated using the
82*5697Smcpowers standard C library's rand() function, so it should not be used for
83*5697Smcpowers cryptographic purposes, but it should be fine for primality testing,
84*5697Smcpowers since all we really care about there is good statistical properties.
85*5697Smcpowers
86*5697Smcpowers As many digits as a currently has are filled with random digits.
87*5697Smcpowers */
88*5697Smcpowers
mpp_random(mp_int * a)89*5697Smcpowers mp_err mpp_random(mp_int *a)
90*5697Smcpowers
91*5697Smcpowers {
92*5697Smcpowers mp_digit next = 0;
93*5697Smcpowers unsigned int ix, jx;
94*5697Smcpowers
95*5697Smcpowers ARGCHK(a != NULL, MP_BADARG);
96*5697Smcpowers
97*5697Smcpowers for(ix = 0; ix < USED(a); ix++) {
98*5697Smcpowers for(jx = 0; jx < sizeof(mp_digit); jx++) {
99*5697Smcpowers next = (next << CHAR_BIT) | (RANDOM() & UCHAR_MAX);
100*5697Smcpowers }
101*5697Smcpowers DIGIT(a, ix) = next;
102*5697Smcpowers }
103*5697Smcpowers
104*5697Smcpowers return MP_OKAY;
105*5697Smcpowers
106*5697Smcpowers } /* end mpp_random() */
107*5697Smcpowers
108*5697Smcpowers /* }}} */
109*5697Smcpowers
110*5697Smcpowers /* {{{ mpp_random_size(a, prec) */
111*5697Smcpowers
mpp_random_size(mp_int * a,mp_size prec)112*5697Smcpowers mp_err mpp_random_size(mp_int *a, mp_size prec)
113*5697Smcpowers {
114*5697Smcpowers mp_err res;
115*5697Smcpowers
116*5697Smcpowers ARGCHK(a != NULL && prec > 0, MP_BADARG);
117*5697Smcpowers
118*5697Smcpowers if((res = s_mp_pad(a, prec)) != MP_OKAY)
119*5697Smcpowers return res;
120*5697Smcpowers
121*5697Smcpowers return mpp_random(a);
122*5697Smcpowers
123*5697Smcpowers } /* end mpp_random_size() */
124