1*505d05c7Sgtb /* 2*505d05c7Sgtb * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3*505d05c7Sgtb * Use is subject to license terms. 4*505d05c7Sgtb */ 5*505d05c7Sgtb 67c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 77c478bd9Sstevel@tonic-gate /* 87c478bd9Sstevel@tonic-gate * lib/krb5/krb/gen_seqnum.c 97c478bd9Sstevel@tonic-gate * 107c478bd9Sstevel@tonic-gate * Copyright 1991 by the Massachusetts Institute of Technology. 117c478bd9Sstevel@tonic-gate * All Rights Reserved. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may 147c478bd9Sstevel@tonic-gate * require a specific license from the United States Government. 157c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating 167c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting. 177c478bd9Sstevel@tonic-gate * 187c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 197c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and 207c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright 217c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and 227c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that 237c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining 247c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior 257c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label 267c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a 277c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software. 287c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of 297c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express 307c478bd9Sstevel@tonic-gate * or implied warranty. 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * Routine to automatically generate a starting sequence number. 347c478bd9Sstevel@tonic-gate * We do this by getting a random key and encrypting something with it, 357c478bd9Sstevel@tonic-gate * then taking the output and slicing it up. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <k5-int.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #ifndef MIN 417c478bd9Sstevel@tonic-gate #define MIN(a,b) ((a) < (b) ? (a) : (b)) 427c478bd9Sstevel@tonic-gate #endif 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate krb5_error_code 45*505d05c7Sgtb krb5_generate_seq_number(krb5_context context, const krb5_keyblock *key, krb5_ui_4 *seqno) 467c478bd9Sstevel@tonic-gate { 477c478bd9Sstevel@tonic-gate krb5_data seed; 487c478bd9Sstevel@tonic-gate krb5_error_code retval; 49*505d05c7Sgtb #if 0 50*505d05c7Sgtb /* 51*505d05c7Sgtb * Solaris Kerberos: Don't bother with this PRNG stuff, 52*505d05c7Sgtb * we have /dev/random and PKCS#11 to handle Random Numbers. 53*505d05c7Sgtb */ 54*505d05c7Sgtb 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate seed.length = key->length; 577c478bd9Sstevel@tonic-gate seed.data = (char *)key->contents; 58*505d05c7Sgtb if ((retval = krb5_c_random_add_entropy(context, KRB5_C_RANDSOURCE_TRUSTEDPARTY, &seed))) 597c478bd9Sstevel@tonic-gate return(retval); 60*505d05c7Sgtb #endif /* 0 */ 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate seed.length = sizeof(*seqno); 637c478bd9Sstevel@tonic-gate seed.data = (char *) seqno; 64*505d05c7Sgtb retval = krb5_c_random_make_octets(context, &seed); 65*505d05c7Sgtb if (retval) 66*505d05c7Sgtb return retval; 67*505d05c7Sgtb /* 68*505d05c7Sgtb * Work around implementation incompatibilities by not generating 69*505d05c7Sgtb * initial sequence numbers greater than 2^30. Previous MIT 70*505d05c7Sgtb * implementations use signed sequence numbers, so initial 71*505d05c7Sgtb * sequence numbers 2^31 to 2^32-1 inclusive will be rejected. 72*505d05c7Sgtb * Letting the maximum initial sequence number be 2^30-1 allows 73*505d05c7Sgtb * for about 2^30 messages to be sent before wrapping into 74*505d05c7Sgtb * "negative" numbers. 75*505d05c7Sgtb */ 76*505d05c7Sgtb *seqno &= 0x3fffffff; 77*505d05c7Sgtb if (*seqno == 0) 78*505d05c7Sgtb *seqno = 1; 79*505d05c7Sgtb return 0; 807c478bd9Sstevel@tonic-gate } 81