1 /*
2   * Copyright2001 by the Massachusetts Institute of Technology.
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 #include "k5-int.h"
26 
27 /*
28  * $Id: util_seqnum.c 15007 2002-11-15 16:12:20Z epeisach $
29  */
30 
31 krb5_error_code
kg_make_seq_num(context,key,direction,seqnum,cksum,buf)32 kg_make_seq_num(context, key, direction, seqnum, cksum, buf)
33      krb5_context context;
34      krb5_keyblock *key;
35      int direction;
36      krb5_ui_4 seqnum;
37      unsigned char *cksum;
38      unsigned char *buf;
39 {
40    unsigned char plain[8];
41 
42    plain[4] = direction;
43    plain[5] = direction;
44    plain[6] = direction;
45    plain[7] = direction;
46    if (key->enctype == ENCTYPE_ARCFOUR_HMAC ) {
47      /* Yes, Microsoft used big-endian sequence number.*/
48      plain[0] = (seqnum>>24) & 0xff;
49      plain[1] = (seqnum>>16) & 0xff;
50      plain[2] = (seqnum>>8) & 0xff;
51      plain[3] = seqnum & 0xff;
52      return kg_arcfour_docrypt (context, key, 0,
53 				cksum, 8,
54 				&plain[0], 8,
55 				buf);
56 
57    }
58 
59    plain[0] = (unsigned char) (seqnum&0xff);
60    plain[1] = (unsigned char) ((seqnum>>8)&0xff);
61    plain[2] = (unsigned char) ((seqnum>>16)&0xff);
62    plain[3] = (unsigned char) ((seqnum>>24)&0xff);
63 
64    return(kg_encrypt(context, key, KG_USAGE_SEQ, cksum, plain, buf, 8));
65 }
66 
kg_get_seq_num(context,key,cksum,buf,direction,seqnum)67 krb5_error_code kg_get_seq_num(context, key, cksum, buf, direction, seqnum)
68      krb5_context context;
69      krb5_keyblock *key;
70      unsigned char *cksum;
71      unsigned char *buf;
72      int *direction;
73      krb5_ui_4 *seqnum;
74 {
75    krb5_error_code code;
76    unsigned char plain[8];
77 
78    if (key->enctype == ENCTYPE_ARCFOUR_HMAC) {
79      code = kg_arcfour_docrypt (context, key, 0,
80 				cksum, 8,
81 				buf, 8,
82 				plain);
83    } else {
84      code = kg_decrypt(context, key, KG_USAGE_SEQ, cksum, buf, plain, 8);
85    }
86    if (code)
87       return(code);
88 
89    if ((plain[4] != plain[5]) ||
90        (plain[4] != plain[6]) ||
91        (plain[4] != plain[7]))
92       return((krb5_error_code) KG_BAD_SEQ);
93 
94    *direction = plain[4];
95    if (key->enctype == ENCTYPE_ARCFOUR_HMAC) {
96      *seqnum = (plain[3]|(plain[2]<<8) | (plain[1]<<16)| (plain[0]<<24));
97    } else {
98      *seqnum = ((plain[0]) |
99 	      (plain[1]<<8) |
100 	      (plain[2]<<16) |
101 	      (plain[3]<<24));
102    }
103 
104    return(0);
105 }
106