17c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
2*ab9b2e15Sgtb 
37c478bd9Sstevel@tonic-gate /*
4*ab9b2e15Sgtb   * Copyright2001 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate  * Copyright 1993 by OpenVision Technologies, Inc.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, distribute, and sell this software
87c478bd9Sstevel@tonic-gate  * and its documentation for any purpose is hereby granted without fee,
97c478bd9Sstevel@tonic-gate  * provided that the above copyright notice appears in all copies and
107c478bd9Sstevel@tonic-gate  * that both that copyright notice and this permission notice appear in
117c478bd9Sstevel@tonic-gate  * supporting documentation, and that the name of OpenVision not be used
127c478bd9Sstevel@tonic-gate  * in advertising or publicity pertaining to distribution of the software
137c478bd9Sstevel@tonic-gate  * without specific, written prior permission. OpenVision makes no
147c478bd9Sstevel@tonic-gate  * representations about the suitability of this software for any
157c478bd9Sstevel@tonic-gate  * purpose.  It is provided "as is" without express or implied warranty.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
187c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
197c478bd9Sstevel@tonic-gate  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
207c478bd9Sstevel@tonic-gate  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
217c478bd9Sstevel@tonic-gate  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
227c478bd9Sstevel@tonic-gate  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
237c478bd9Sstevel@tonic-gate  * PERFORMANCE OF THIS SOFTWARE.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*ab9b2e15Sgtb #include "gssapiP_krb5.h"
27*ab9b2e15Sgtb #include "k5-int.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
30*ab9b2e15Sgtb  * $Id: util_seqnum.c 15007 2002-11-15 16:12:20Z epeisach $
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate krb5_error_code
kg_make_seq_num(context,key,direction,seqnum,cksum,buf)347c478bd9Sstevel@tonic-gate kg_make_seq_num(context, key, direction, seqnum, cksum, buf)
357c478bd9Sstevel@tonic-gate      krb5_context context;
367c478bd9Sstevel@tonic-gate      krb5_keyblock *key;
377c478bd9Sstevel@tonic-gate      int direction;
387c478bd9Sstevel@tonic-gate      krb5_ui_4 seqnum;
397c478bd9Sstevel@tonic-gate      unsigned char *cksum;
407c478bd9Sstevel@tonic-gate      unsigned char *buf;
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate    unsigned char plain[8];
437c478bd9Sstevel@tonic-gate 
44*ab9b2e15Sgtb    plain[4] = direction;
45*ab9b2e15Sgtb    plain[5] = direction;
46*ab9b2e15Sgtb    plain[6] = direction;
47*ab9b2e15Sgtb    plain[7] = direction;
487c478bd9Sstevel@tonic-gate    if (key->enctype == ENCTYPE_ARCFOUR_HMAC ) {
49*ab9b2e15Sgtb      /* Yes, Microsoft used big-endian sequence number.*/
50*ab9b2e15Sgtb      plain[0] = (seqnum>>24) & 0xff;
51*ab9b2e15Sgtb      plain[1] = (seqnum>>16) & 0xff;
52*ab9b2e15Sgtb      plain[2] = (seqnum>>8) & 0xff;
53*ab9b2e15Sgtb      plain[3] = seqnum & 0xff;
54*ab9b2e15Sgtb      return kg_arcfour_docrypt (context, key, 0,
557c478bd9Sstevel@tonic-gate 				cksum, 8,
567c478bd9Sstevel@tonic-gate 				&plain[0], 8,
577c478bd9Sstevel@tonic-gate 				buf);
58*ab9b2e15Sgtb 
597c478bd9Sstevel@tonic-gate    }
60*ab9b2e15Sgtb 
617c478bd9Sstevel@tonic-gate    plain[0] = (unsigned char) (seqnum&0xff);
627c478bd9Sstevel@tonic-gate    plain[1] = (unsigned char) ((seqnum>>8)&0xff);
637c478bd9Sstevel@tonic-gate    plain[2] = (unsigned char) ((seqnum>>16)&0xff);
647c478bd9Sstevel@tonic-gate    plain[3] = (unsigned char) ((seqnum>>24)&0xff);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate    return(kg_encrypt(context, key, KG_USAGE_SEQ, cksum, plain, buf, 8));
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
kg_get_seq_num(context,key,cksum,buf,direction,seqnum)697c478bd9Sstevel@tonic-gate krb5_error_code kg_get_seq_num(context, key, cksum, buf, direction, seqnum)
707c478bd9Sstevel@tonic-gate      krb5_context context;
717c478bd9Sstevel@tonic-gate      krb5_keyblock *key;
727c478bd9Sstevel@tonic-gate      unsigned char *cksum;
737c478bd9Sstevel@tonic-gate      unsigned char *buf;
747c478bd9Sstevel@tonic-gate      int *direction;
757c478bd9Sstevel@tonic-gate      krb5_ui_4 *seqnum;
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate    krb5_error_code code;
787c478bd9Sstevel@tonic-gate    unsigned char plain[8];
797c478bd9Sstevel@tonic-gate 
80*ab9b2e15Sgtb    if (key->enctype == ENCTYPE_ARCFOUR_HMAC) {
81*ab9b2e15Sgtb      code = kg_arcfour_docrypt (context, key, 0,
82*ab9b2e15Sgtb 				cksum, 8,
83*ab9b2e15Sgtb 				buf, 8,
84*ab9b2e15Sgtb 				plain);
85*ab9b2e15Sgtb    } else {
86*ab9b2e15Sgtb      code = kg_decrypt(context, key, KG_USAGE_SEQ, cksum, buf, plain, 8);
87*ab9b2e15Sgtb    }
887c478bd9Sstevel@tonic-gate    if (code)
89*ab9b2e15Sgtb       return(code);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate    if ((plain[4] != plain[5]) ||
927c478bd9Sstevel@tonic-gate        (plain[4] != plain[6]) ||
937c478bd9Sstevel@tonic-gate        (plain[4] != plain[7]))
947c478bd9Sstevel@tonic-gate       return((krb5_error_code) KG_BAD_SEQ);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate    *direction = plain[4];
97*ab9b2e15Sgtb    if (key->enctype == ENCTYPE_ARCFOUR_HMAC) {
98*ab9b2e15Sgtb      *seqnum = (plain[3]|(plain[2]<<8) | (plain[1]<<16)| (plain[0]<<24));
99*ab9b2e15Sgtb    } else {
100*ab9b2e15Sgtb      *seqnum = ((plain[0]) |
1017c478bd9Sstevel@tonic-gate 	      (plain[1]<<8) |
1027c478bd9Sstevel@tonic-gate 	      (plain[2]<<16) |
1037c478bd9Sstevel@tonic-gate 	      (plain[3]<<24));
104*ab9b2e15Sgtb    }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate    return(0);
1077c478bd9Sstevel@tonic-gate }
108