1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * linux/net/sunrpc/gss_krb5_seqnum.c
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Adapted from MIT Kerberos 5-1.2.1 lib/gssapi/krb5/util_seqnum.c
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (c) 2000 The Regents of the University of Michigan.
7*4882a593Smuzhiyun * All rights reserved.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Andy Adamson <andros@umich.edu>
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun * Copyright 1993 by OpenVision Technologies, Inc.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software
16*4882a593Smuzhiyun * and its documentation for any purpose is hereby granted without fee,
17*4882a593Smuzhiyun * provided that the above copyright notice appears in all copies and
18*4882a593Smuzhiyun * that both that copyright notice and this permission notice appear in
19*4882a593Smuzhiyun * supporting documentation, and that the name of OpenVision not be used
20*4882a593Smuzhiyun * in advertising or publicity pertaining to distribution of the software
21*4882a593Smuzhiyun * without specific, written prior permission. OpenVision makes no
22*4882a593Smuzhiyun * representations about the suitability of this software for any
23*4882a593Smuzhiyun * purpose. It is provided "as is" without express or implied warranty.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
27*4882a593Smuzhiyun * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
28*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
29*4882a593Smuzhiyun * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
30*4882a593Smuzhiyun * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
31*4882a593Smuzhiyun * PERFORMANCE OF THIS SOFTWARE.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include <crypto/skcipher.h>
35*4882a593Smuzhiyun #include <linux/types.h>
36*4882a593Smuzhiyun #include <linux/sunrpc/gss_krb5.h>
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
39*4882a593Smuzhiyun # define RPCDBG_FACILITY RPCDBG_AUTH
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun s32
krb5_make_seq_num(struct krb5_ctx * kctx,struct crypto_sync_skcipher * key,int direction,u32 seqnum,unsigned char * cksum,unsigned char * buf)43*4882a593Smuzhiyun krb5_make_seq_num(struct krb5_ctx *kctx,
44*4882a593Smuzhiyun struct crypto_sync_skcipher *key,
45*4882a593Smuzhiyun int direction,
46*4882a593Smuzhiyun u32 seqnum,
47*4882a593Smuzhiyun unsigned char *cksum, unsigned char *buf)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun unsigned char *plain;
50*4882a593Smuzhiyun s32 code;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun plain = kmalloc(8, GFP_NOFS);
53*4882a593Smuzhiyun if (!plain)
54*4882a593Smuzhiyun return -ENOMEM;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun plain[0] = (unsigned char) (seqnum & 0xff);
57*4882a593Smuzhiyun plain[1] = (unsigned char) ((seqnum >> 8) & 0xff);
58*4882a593Smuzhiyun plain[2] = (unsigned char) ((seqnum >> 16) & 0xff);
59*4882a593Smuzhiyun plain[3] = (unsigned char) ((seqnum >> 24) & 0xff);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun plain[4] = direction;
62*4882a593Smuzhiyun plain[5] = direction;
63*4882a593Smuzhiyun plain[6] = direction;
64*4882a593Smuzhiyun plain[7] = direction;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun code = krb5_encrypt(key, cksum, plain, buf, 8);
67*4882a593Smuzhiyun kfree(plain);
68*4882a593Smuzhiyun return code;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun s32
krb5_get_seq_num(struct krb5_ctx * kctx,unsigned char * cksum,unsigned char * buf,int * direction,u32 * seqnum)72*4882a593Smuzhiyun krb5_get_seq_num(struct krb5_ctx *kctx,
73*4882a593Smuzhiyun unsigned char *cksum,
74*4882a593Smuzhiyun unsigned char *buf,
75*4882a593Smuzhiyun int *direction, u32 *seqnum)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun s32 code;
78*4882a593Smuzhiyun unsigned char *plain;
79*4882a593Smuzhiyun struct crypto_sync_skcipher *key = kctx->seq;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun dprintk("RPC: krb5_get_seq_num:\n");
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun plain = kmalloc(8, GFP_NOFS);
84*4882a593Smuzhiyun if (!plain)
85*4882a593Smuzhiyun return -ENOMEM;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if ((code = krb5_decrypt(key, cksum, buf, plain, 8)))
88*4882a593Smuzhiyun goto out;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if ((plain[4] != plain[5]) || (plain[4] != plain[6]) ||
91*4882a593Smuzhiyun (plain[4] != plain[7])) {
92*4882a593Smuzhiyun code = (s32)KG_BAD_SEQ;
93*4882a593Smuzhiyun goto out;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun *direction = plain[4];
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun *seqnum = ((plain[0]) |
99*4882a593Smuzhiyun (plain[1] << 8) | (plain[2] << 16) | (plain[3] << 24));
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun out:
102*4882a593Smuzhiyun kfree(plain);
103*4882a593Smuzhiyun return code;
104*4882a593Smuzhiyun }
105