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