Whamcloud - gitweb
land lustre part of b_hd_sec on HEAD.
[fs/lustre-release.git] / lustre / sec / gss / gss_krb5_seqnum.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  * Copyright 2004, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  *  linux/net/sunrpc/gss_krb5_seqnum.c
12  *
13  *  Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/util_seqnum.c
14  *
15  *  Copyright (c) 2000 The Regents of the University of Michigan.
16  *  All rights reserved.
17  *
18  *  Andy Adamson   <andros@umich.edu>
19  */
20
21 /*
22  * Copyright 1993 by OpenVision Technologies, Inc.
23  *
24  * Permission to use, copy, modify, distribute, and sell this software
25  * and its documentation for any purpose is hereby granted without fee,
26  * provided that the above copyright notice appears in all copies and
27  * that both that copyright notice and this permission notice appear in
28  * supporting documentation, and that the name of OpenVision not be used
29  * in advertising or publicity pertaining to distribution of the software
30  * without specific, written prior permission. OpenVision makes no
31  * representations about the suitability of this software for any
32  * purpose.  It is provided "as is" without express or implied warranty.
33  *
34  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
35  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
36  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
37  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
38  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
39  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
40  * PERFORMANCE OF THIS SOFTWARE.
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_SEC
47 #ifdef __KERNEL__
48 #include <linux/init.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
51 #include <linux/crypto.h>
52 #else
53 #include <liblustre.h>
54 #include "../kcrypto/libcrypto.h"
55 #endif
56
57 #include <libcfs/kp30.h>
58 #include <linux/obd.h>
59 #include <linux/obd_class.h>
60 #include <linux/obd_support.h>
61 #include <linux/lustre_idl.h>
62 #include <linux/lustre_net.h>
63 #include <linux/lustre_import.h>
64 #include <linux/lustre_sec.h>
65
66 #include "gss_err.h"
67 #include "gss_internal.h"
68 #include "gss_api.h"
69 #include "gss_krb5.h"
70
71 __s32
72 krb5_make_seq_num(struct crypto_tfm *key,
73                   int direction,
74                   __s32 seqnum,
75                   unsigned char *cksum,
76                   unsigned char *buf)
77 {
78         unsigned char plain[8];
79
80         plain[0] = (unsigned char) (seqnum & 0xff);
81         plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
82         plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
83         plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
84
85         plain[4] = direction;
86         plain[5] = direction;
87         plain[6] = direction;
88         plain[7] = direction;
89
90         return krb5_encrypt(key, cksum, plain, buf, 8);
91 }
92
93 __s32
94 krb5_get_seq_num(struct crypto_tfm *key,
95                  unsigned char *cksum,
96                  unsigned char *buf,
97                  int *direction,
98                  __s32 * seqnum)
99 {
100         __s32 code;
101         unsigned char plain[8];
102
103         if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
104                 return code;
105
106         if ((plain[4] != plain[5]) || (plain[4] != plain[6])
107                                    || (plain[4] != plain[7]))
108                 return (__s32)KG_BAD_SEQ;
109
110         *direction = plain[4];
111
112         *seqnum = ((plain[0]) |
113                    (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
114
115         return (0);
116 }