1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 /*
3  * Copyright 1993 by OpenVision Technologies, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without fee,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear in
9  * supporting documentation, and that the name of OpenVision not be used
10  * in advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission. OpenVision makes no
12  * representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21  * PERFORMANCE OF THIS SOFTWARE.
22  */
23 
24 #include <gssapiP_krb5.h>
25 
26 /*
27  * $Id: util_seqnum.c,v 1.9.6.1 2000/05/31 17:17:39 raeburn Exp $
28  */
29 
30 krb5_error_code
31 kg_make_seq_num(context, key, direction, seqnum, cksum, buf)
32      krb5_context context;
33      krb5_keyblock *key;
34      int direction;
35      krb5_ui_4 seqnum;
36      unsigned char *cksum;
37      unsigned char *buf;
38 {
39    unsigned char plain[8];
40 
41    plain[4] = (unsigned char) direction;
42    plain[5] = (unsigned char) direction;
43    plain[6] = (unsigned char) direction;
44    plain[7] = (unsigned char) direction;
45    if (key->enctype == ENCTYPE_ARCFOUR_HMAC ) {
46 	/* Yes, Microsoft used big-endian sequence number.*/
47 	plain[0] = (seqnum>>24) & 0xff;
48 	plain[1] = (seqnum>>16) & 0xff;
49 	plain[2] = (seqnum>>8) & 0xff;
50 	plain[3] = seqnum & 0xff;
51 	return kg_arcfour_docrypt (context, key, 0,
52 				cksum, 8,
53 				&plain[0], 8,
54 				buf);
55 
56    }
57    plain[0] = (unsigned char) (seqnum&0xff);
58    plain[1] = (unsigned char) ((seqnum>>8)&0xff);
59    plain[2] = (unsigned char) ((seqnum>>16)&0xff);
60    plain[3] = (unsigned char) ((seqnum>>24)&0xff);
61 
62    return(kg_encrypt(context, key, KG_USAGE_SEQ, cksum, plain, buf, 8));
63 }
64 
65 krb5_error_code kg_get_seq_num(context, key, cksum, buf, direction, seqnum)
66      krb5_context context;
67      krb5_keyblock *key;
68      unsigned char *cksum;
69      unsigned char *buf;
70      int *direction;
71      krb5_ui_4 *seqnum;
72 {
73    krb5_error_code code;
74    unsigned char plain[8];
75 
76    if (key->enctype == ENCTYPE_ARCFOUR_HMAC)
77 	code = kg_arcfour_docrypt (context, key, 0,
78 			cksum, 8, buf, 8, plain);
79    else
80    	code = kg_decrypt(context, key, KG_USAGE_SEQ, cksum, buf, plain, 8);
81    if (code)
82 	return (code);
83 
84    if ((plain[4] != plain[5]) ||
85        (plain[4] != plain[6]) ||
86        (plain[4] != plain[7]))
87       return((krb5_error_code) KG_BAD_SEQ);
88 
89    *direction = plain[4];
90 
91    if (key->enctype == ENCTYPE_ARCFOUR_HMAC)
92 	*seqnum = (plain[3]|(plain[2]<<8) | (plain[1]<<16)| (plain[0]<<24));
93    else
94         *seqnum = ((plain[0]) |
95 	      (plain[1]<<8) |
96 	      (plain[2]<<16) |
97 	      (plain[3]<<24));
98 
99    return(0);
100 }
101