17f675ca7SChuck Lever /* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */
27f675ca7SChuck Lever /*
37f675ca7SChuck Lever * SunRPC GSS Kerberos 5 mechanism internal definitions
47f675ca7SChuck Lever *
57f675ca7SChuck Lever * Copyright (c) 2022 Oracle and/or its affiliates.
67f675ca7SChuck Lever */
77f675ca7SChuck Lever
87f675ca7SChuck Lever #ifndef _NET_SUNRPC_AUTH_GSS_KRB5_INTERNAL_H
97f675ca7SChuck Lever #define _NET_SUNRPC_AUTH_GSS_KRB5_INTERNAL_H
107f675ca7SChuck Lever
11e01b2c79SChuck Lever /*
126e460c23SChuck Lever * The RFCs often specify payload lengths in bits. This helper
136e460c23SChuck Lever * converts a specified bit-length to the number of octets/bytes.
146e460c23SChuck Lever */
156e460c23SChuck Lever #define BITS2OCTETS(x) ((x) / 8)
166e460c23SChuck Lever
176e460c23SChuck Lever struct krb5_ctx;
186e460c23SChuck Lever
196e460c23SChuck Lever struct gss_krb5_enctype {
206e460c23SChuck Lever const u32 etype; /* encryption (key) type */
216e460c23SChuck Lever const u32 ctype; /* checksum type */
226e460c23SChuck Lever const char *name; /* "friendly" name */
236e460c23SChuck Lever const char *encrypt_name; /* crypto encrypt name */
246e460c23SChuck Lever const char *aux_cipher; /* aux encrypt cipher name */
256e460c23SChuck Lever const char *cksum_name; /* crypto checksum name */
266e460c23SChuck Lever const u16 signalg; /* signing algorithm */
276e460c23SChuck Lever const u16 sealalg; /* sealing algorithm */
286e460c23SChuck Lever const u32 cksumlength; /* checksum length */
296e460c23SChuck Lever const u32 keyed_cksum; /* is it a keyed cksum? */
306e460c23SChuck Lever const u32 keybytes; /* raw key len, in bytes */
316e460c23SChuck Lever const u32 keylength; /* protocol key length, in octets */
326e460c23SChuck Lever const u32 Kc_length; /* checksum subkey length, in octets */
336e460c23SChuck Lever const u32 Ke_length; /* encryption subkey length, in octets */
346e460c23SChuck Lever const u32 Ki_length; /* integrity subkey length, in octets */
356e460c23SChuck Lever
366e460c23SChuck Lever int (*derive_key)(const struct gss_krb5_enctype *gk5e,
376e460c23SChuck Lever const struct xdr_netobj *in,
386e460c23SChuck Lever struct xdr_netobj *out,
396e460c23SChuck Lever const struct xdr_netobj *label,
406e460c23SChuck Lever gfp_t gfp_mask);
416e460c23SChuck Lever u32 (*encrypt)(struct krb5_ctx *kctx, u32 offset,
426e460c23SChuck Lever struct xdr_buf *buf, struct page **pages);
436e460c23SChuck Lever u32 (*decrypt)(struct krb5_ctx *kctx, u32 offset, u32 len,
446e460c23SChuck Lever struct xdr_buf *buf, u32 *headskip, u32 *tailskip);
456e460c23SChuck Lever u32 (*get_mic)(struct krb5_ctx *kctx, struct xdr_buf *text,
466e460c23SChuck Lever struct xdr_netobj *token);
476e460c23SChuck Lever u32 (*verify_mic)(struct krb5_ctx *kctx, struct xdr_buf *message_buffer,
486e460c23SChuck Lever struct xdr_netobj *read_token);
496e460c23SChuck Lever u32 (*wrap)(struct krb5_ctx *kctx, int offset,
506e460c23SChuck Lever struct xdr_buf *buf, struct page **pages);
516e460c23SChuck Lever u32 (*unwrap)(struct krb5_ctx *kctx, int offset, int len,
526e460c23SChuck Lever struct xdr_buf *buf, unsigned int *slack,
536e460c23SChuck Lever unsigned int *align);
546e460c23SChuck Lever };
556e460c23SChuck Lever
566e460c23SChuck Lever /* krb5_ctx flags definitions */
576e460c23SChuck Lever #define KRB5_CTX_FLAG_INITIATOR 0x00000001
586e460c23SChuck Lever #define KRB5_CTX_FLAG_ACCEPTOR_SUBKEY 0x00000004
596e460c23SChuck Lever
606e460c23SChuck Lever struct krb5_ctx {
616e460c23SChuck Lever int initiate; /* 1 = initiating, 0 = accepting */
626e460c23SChuck Lever u32 enctype;
636e460c23SChuck Lever u32 flags;
646e460c23SChuck Lever const struct gss_krb5_enctype *gk5e; /* enctype-specific info */
656e460c23SChuck Lever struct crypto_sync_skcipher *enc;
666e460c23SChuck Lever struct crypto_sync_skcipher *seq;
676e460c23SChuck Lever struct crypto_sync_skcipher *acceptor_enc;
686e460c23SChuck Lever struct crypto_sync_skcipher *initiator_enc;
696e460c23SChuck Lever struct crypto_sync_skcipher *acceptor_enc_aux;
706e460c23SChuck Lever struct crypto_sync_skcipher *initiator_enc_aux;
716e460c23SChuck Lever struct crypto_ahash *acceptor_sign;
726e460c23SChuck Lever struct crypto_ahash *initiator_sign;
736e460c23SChuck Lever struct crypto_ahash *initiator_integ;
746e460c23SChuck Lever struct crypto_ahash *acceptor_integ;
756e460c23SChuck Lever u8 Ksess[GSS_KRB5_MAX_KEYLEN]; /* session key */
766e460c23SChuck Lever u8 cksum[GSS_KRB5_MAX_KEYLEN];
776e460c23SChuck Lever atomic_t seq_send;
786e460c23SChuck Lever atomic64_t seq_send64;
796e460c23SChuck Lever time64_t endtime;
806e460c23SChuck Lever struct xdr_netobj mech_used;
816e460c23SChuck Lever };
826e460c23SChuck Lever
836e460c23SChuck Lever /*
84e01b2c79SChuck Lever * GSS Kerberos 5 mechanism Per-Message calls.
85e01b2c79SChuck Lever */
86e01b2c79SChuck Lever
87e01b2c79SChuck Lever u32 gss_krb5_get_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *text,
88e01b2c79SChuck Lever struct xdr_netobj *token);
89e01b2c79SChuck Lever
90e01b2c79SChuck Lever u32 gss_krb5_verify_mic_v2(struct krb5_ctx *ctx, struct xdr_buf *message_buffer,
91e01b2c79SChuck Lever struct xdr_netobj *read_token);
92e01b2c79SChuck Lever
93e01b2c79SChuck Lever u32 gss_krb5_wrap_v2(struct krb5_ctx *kctx, int offset,
94e01b2c79SChuck Lever struct xdr_buf *buf, struct page **pages);
95e01b2c79SChuck Lever
96e01b2c79SChuck Lever u32 gss_krb5_unwrap_v2(struct krb5_ctx *kctx, int offset, int len,
97e01b2c79SChuck Lever struct xdr_buf *buf, unsigned int *slack,
98e01b2c79SChuck Lever unsigned int *align);
99e01b2c79SChuck Lever
100e01b2c79SChuck Lever /*
101e01b2c79SChuck Lever * Implementation internal functions
102e01b2c79SChuck Lever */
103e01b2c79SChuck Lever
1042691a27dSChuck Lever /* Key Derivation Functions */
1052691a27dSChuck Lever
1062691a27dSChuck Lever int krb5_derive_key_v2(const struct gss_krb5_enctype *gk5e,
1072691a27dSChuck Lever const struct xdr_netobj *inkey,
1082691a27dSChuck Lever struct xdr_netobj *outkey,
1092691a27dSChuck Lever const struct xdr_netobj *label,
1102691a27dSChuck Lever gfp_t gfp_mask);
1112691a27dSChuck Lever
112ae2e4d2bSChuck Lever int krb5_kdf_hmac_sha2(const struct gss_krb5_enctype *gk5e,
113ae2e4d2bSChuck Lever const struct xdr_netobj *inkey,
114ae2e4d2bSChuck Lever struct xdr_netobj *outkey,
115ae2e4d2bSChuck Lever const struct xdr_netobj *in_constant,
116ae2e4d2bSChuck Lever gfp_t gfp_mask);
117ae2e4d2bSChuck Lever
11845b4ef46SChuck Lever int krb5_kdf_feedback_cmac(const struct gss_krb5_enctype *gk5e,
11945b4ef46SChuck Lever const struct xdr_netobj *inkey,
12045b4ef46SChuck Lever struct xdr_netobj *outkey,
12145b4ef46SChuck Lever const struct xdr_netobj *in_constant,
12245b4ef46SChuck Lever gfp_t gfp_mask);
12345b4ef46SChuck Lever
1242691a27dSChuck Lever /**
1252691a27dSChuck Lever * krb5_derive_key - Derive a subkey from a protocol key
1262691a27dSChuck Lever * @kctx: Kerberos 5 context
1272691a27dSChuck Lever * @inkey: base protocol key
1282691a27dSChuck Lever * @outkey: OUT: derived key
1292691a27dSChuck Lever * @usage: key usage value
1302691a27dSChuck Lever * @seed: key usage seed (one octet)
1312691a27dSChuck Lever * @gfp_mask: memory allocation control flags
1322691a27dSChuck Lever *
1332691a27dSChuck Lever * Caller sets @outkey->len to the desired length of the derived key.
1342691a27dSChuck Lever *
1352691a27dSChuck Lever * On success, returns 0 and fills in @outkey. A negative errno value
1362691a27dSChuck Lever * is returned on failure.
1372691a27dSChuck Lever */
krb5_derive_key(struct krb5_ctx * kctx,const struct xdr_netobj * inkey,struct xdr_netobj * outkey,u32 usage,u8 seed,gfp_t gfp_mask)1382691a27dSChuck Lever static inline int krb5_derive_key(struct krb5_ctx *kctx,
1392691a27dSChuck Lever const struct xdr_netobj *inkey,
1402691a27dSChuck Lever struct xdr_netobj *outkey,
1412691a27dSChuck Lever u32 usage, u8 seed, gfp_t gfp_mask)
1422691a27dSChuck Lever {
1432691a27dSChuck Lever const struct gss_krb5_enctype *gk5e = kctx->gk5e;
1442691a27dSChuck Lever u8 label_data[GSS_KRB5_K5CLENGTH];
1452691a27dSChuck Lever struct xdr_netobj label = {
1462691a27dSChuck Lever .len = sizeof(label_data),
1472691a27dSChuck Lever .data = label_data,
1482691a27dSChuck Lever };
1492691a27dSChuck Lever __be32 *p = (__be32 *)label_data;
1502691a27dSChuck Lever
1512691a27dSChuck Lever *p = cpu_to_be32(usage);
1522691a27dSChuck Lever label_data[4] = seed;
1532691a27dSChuck Lever return gk5e->derive_key(gk5e, inkey, outkey, &label, gfp_mask);
1542691a27dSChuck Lever }
1552691a27dSChuck Lever
1567f675ca7SChuck Lever void krb5_make_confounder(u8 *p, int conflen);
1577f675ca7SChuck Lever
1586e460c23SChuck Lever u32 make_checksum(struct krb5_ctx *kctx, char *header, int hdrlen,
1596e460c23SChuck Lever struct xdr_buf *body, int body_offset, u8 *cksumkey,
1606e460c23SChuck Lever unsigned int usage, struct xdr_netobj *cksumout);
1616e460c23SChuck Lever
1622dbe0cacSChuck Lever u32 gss_krb5_checksum(struct crypto_ahash *tfm, char *header, int hdrlen,
1632dbe0cacSChuck Lever const struct xdr_buf *body, int body_offset,
1642dbe0cacSChuck Lever struct xdr_netobj *cksumout);
1652dbe0cacSChuck Lever
166d50b8152SChuck Lever u32 krb5_encrypt(struct crypto_sync_skcipher *key, void *iv, void *in,
167d50b8152SChuck Lever void *out, int length);
168d50b8152SChuck Lever
169d50b8152SChuck Lever u32 krb5_decrypt(struct crypto_sync_skcipher *key, void *iv, void *in,
170d50b8152SChuck Lever void *out, int length);
171d50b8152SChuck Lever
1726e460c23SChuck Lever int xdr_extend_head(struct xdr_buf *buf, unsigned int base,
1736e460c23SChuck Lever unsigned int shiftlen);
1746e460c23SChuck Lever
1756e460c23SChuck Lever int gss_encrypt_xdr_buf(struct crypto_sync_skcipher *tfm,
1766e460c23SChuck Lever struct xdr_buf *outbuf, int offset,
1776e460c23SChuck Lever struct page **pages);
1786e460c23SChuck Lever
1796e460c23SChuck Lever int gss_decrypt_xdr_buf(struct crypto_sync_skcipher *tfm,
1806e460c23SChuck Lever struct xdr_buf *inbuf, int offset);
1816e460c23SChuck Lever
182ae6ad5d0SChuck Lever u32 gss_krb5_aes_encrypt(struct krb5_ctx *kctx, u32 offset,
183ae6ad5d0SChuck Lever struct xdr_buf *buf, struct page **pages);
184ae6ad5d0SChuck Lever
185ae6ad5d0SChuck Lever u32 gss_krb5_aes_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
186ae6ad5d0SChuck Lever struct xdr_buf *buf, u32 *plainoffset, u32 *plainlen);
187ae6ad5d0SChuck Lever
1880d5b5a0fSChuck Lever u32 krb5_etm_encrypt(struct krb5_ctx *kctx, u32 offset, struct xdr_buf *buf,
1890d5b5a0fSChuck Lever struct page **pages);
1900d5b5a0fSChuck Lever
1910d5b5a0fSChuck Lever u32 krb5_etm_decrypt(struct krb5_ctx *kctx, u32 offset, u32 len,
1920d5b5a0fSChuck Lever struct xdr_buf *buf, u32 *headskip, u32 *tailskip);
1930d5b5a0fSChuck Lever
194eebd8c2dSChuck Lever #if IS_ENABLED(CONFIG_KUNIT)
195eebd8c2dSChuck Lever void krb5_nfold(u32 inbits, const u8 *in, u32 outbits, u8 *out);
196ddd8c1f9SChuck Lever const struct gss_krb5_enctype *gss_krb5_lookup_enctype(u32 etype);
197e1a9a384SChuck Lever int krb5_cbc_cts_encrypt(struct crypto_sync_skcipher *cts_tfm,
198e1a9a384SChuck Lever struct crypto_sync_skcipher *cbc_tfm, u32 offset,
199e1a9a384SChuck Lever struct xdr_buf *buf, struct page **pages,
200e1a9a384SChuck Lever u8 *iv, unsigned int ivsize);
201*c4a9f055SChuck Lever int krb5_cbc_cts_decrypt(struct crypto_sync_skcipher *cts_tfm,
202*c4a9f055SChuck Lever struct crypto_sync_skcipher *cbc_tfm,
203*c4a9f055SChuck Lever u32 offset, struct xdr_buf *buf);
2044d2d15c0SChuck Lever u32 krb5_etm_checksum(struct crypto_sync_skcipher *cipher,
2054d2d15c0SChuck Lever struct crypto_ahash *tfm, const struct xdr_buf *body,
2064d2d15c0SChuck Lever int body_offset, struct xdr_netobj *cksumout);
207eebd8c2dSChuck Lever #endif
208eebd8c2dSChuck Lever
2097f675ca7SChuck Lever #endif /* _NET_SUNRPC_AUTH_GSS_KRB5_INTERNAL_H */
210