1505d05c7Sgtb /*
2*159d09a2SMark Phalan  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3505d05c7Sgtb  * Use is subject to license terms.
4505d05c7Sgtb  */
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * lib/krb5/krb/gen_seqnum.c
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * Copyright 1991 by the Massachusetts Institute of Technology.
107c478bd9Sstevel@tonic-gate  * All Rights Reserved.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
137c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
147c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
157c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
187c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
197c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
207c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
217c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
227c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
237c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
247c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
257c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
267c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
277c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
287c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
297c478bd9Sstevel@tonic-gate  * or implied warranty.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Routine to automatically generate a starting sequence number.
337c478bd9Sstevel@tonic-gate  * We do this by getting a random key and encrypting something with it,
347c478bd9Sstevel@tonic-gate  * then taking the output and slicing it up.
357c478bd9Sstevel@tonic-gate  */
37*159d09a2SMark Phalan #include "k5-int.h"
397c478bd9Sstevel@tonic-gate #ifndef MIN
407c478bd9Sstevel@tonic-gate #define MIN(a,b) ((a) < (b) ? (a) : (b))
417c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate krb5_error_code
krb5_generate_seq_number(krb5_context context,const krb5_keyblock * key,krb5_ui_4 * seqno)44505d05c7Sgtb krb5_generate_seq_number(krb5_context context, const krb5_keyblock *key, krb5_ui_4 *seqno)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate     krb5_data seed;
477c478bd9Sstevel@tonic-gate     krb5_error_code retval;
48505d05c7Sgtb #if 0
49505d05c7Sgtb /*
50505d05c7Sgtb  * Solaris Kerberos:  Don't bother with this PRNG stuff,
51505d05c7Sgtb  * we have /dev/random and PKCS#11 to handle Random Numbers.
52505d05c7Sgtb  */
557c478bd9Sstevel@tonic-gate     seed.length = key->length;
56*159d09a2SMark Phalan     seed.data = key->contents;
57505d05c7Sgtb     if ((retval = krb5_c_random_add_entropy(context, KRB5_C_RANDSOURCE_TRUSTEDPARTY, &seed)))
587c478bd9Sstevel@tonic-gate 	return(retval);
59505d05c7Sgtb #endif /* 0 */
617c478bd9Sstevel@tonic-gate     seed.length = sizeof(*seqno);
627c478bd9Sstevel@tonic-gate     seed.data = (char *) seqno;
63505d05c7Sgtb     retval = krb5_c_random_make_octets(context, &seed);
64505d05c7Sgtb     if (retval)
65505d05c7Sgtb 	return retval;
66505d05c7Sgtb     /*
67505d05c7Sgtb      * Work around implementation incompatibilities by not generating
68505d05c7Sgtb      * initial sequence numbers greater than 2^30.  Previous MIT
69505d05c7Sgtb      * implementations use signed sequence numbers, so initial
70505d05c7Sgtb      * sequence numbers 2^31 to 2^32-1 inclusive will be rejected.
71505d05c7Sgtb      * Letting the maximum initial sequence number be 2^30-1 allows
72505d05c7Sgtb      * for about 2^30 messages to be sent before wrapping into
73505d05c7Sgtb      * "negative" numbers.
74505d05c7Sgtb      */
75505d05c7Sgtb     *seqno &= 0x3fffffff;
76505d05c7Sgtb     if (*seqno == 0)
77505d05c7Sgtb 	*seqno = 1;
78505d05c7Sgtb     return 0;
797c478bd9Sstevel@tonic-gate }