1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
14  * Copyright 2022 RackTop Systems, Inc.
15  */
16 
17 #ifndef _SMB_KCRYPT_H_
18 #define	_SMB_KCRYPT_H_
19 
20 /*
21  * SMB signing routines used in {smb,smb2}_signing.c
22  * Two implementations of these (kernel/user) in:
23  *	uts/common/fs/smbsrv/smb_sign_kcf.c
24  *	lib/smbsrv/libfksmbsrv/common/fksmb_sign_pkcs.c
25  */
26 
27 #ifdef	_KERNEL
28 #include <sys/crypto/api.h>
29 #else
30 #include <security/cryptoki.h>
31 #include <security/pkcs11.h>
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 #define	AES128_KEY_LENGTH	16	/* AES128 key length in bytes */
39 #define	AES256_KEY_LENGTH	32	/* AES256 key length in bytes */
40 #define	MD5_DIGEST_LENGTH	16	/* MD5 digest length in bytes */
41 #define	SHA256_DIGEST_LENGTH	32	/* SHA256 digest length in bytes */
42 #define	SHA512_DIGEST_LENGTH	64	/* SHA512 digest length in bytes */
43 #define	SMB2_SIG_SIZE		16
44 #define	SMB2_KEYLEN		16	/* SMB2/3 Signing Key length */
45 #define	SMB2_SSN_KEYLEN		16	/* Max size of the SMB2 Session Key */
46 
47 #ifdef	_KERNEL
48 /* KCF variant */
49 typedef crypto_mechanism_t	smb_crypto_mech_t;
50 typedef crypto_context_t	smb_sign_ctx_t;
51 typedef struct smb3_enc_ctx {
52 	crypto_context_t ctx;
53 	crypto_data_t output;
54 	size_t len;
55 } smb3_enc_ctx_t;
56 
57 typedef union {
58 	CK_AES_CCM_PARAMS	ccm;
59 	CK_AES_GCM_PARAMS	gcm;
60 } smb3_crypto_param_t;
61 
62 #else	/* _KERNEL */
63 /* PKCS11 variant */
64 typedef CK_MECHANISM		smb_crypto_mech_t;
65 typedef CK_SESSION_HANDLE	smb_sign_ctx_t;
66 typedef struct smb_enc_ctx {
67 	CK_SESSION_HANDLE ctx;
68 	uint8_t *output;
69 	CK_ULONG len;
70 } smb3_enc_ctx_t;
71 /*
72  * CCM in PKCS has not been implemented.
73  * We just need an opaque type with space to refer to.
74  */
75 typedef struct pkcs_ccm_param {
76 	uint8_t buf[100];
77 } smb3_crypto_param_t;
78 #endif	/* _KERNEL */
79 
80 /*
81  * SMB signing routines used in smb_signing.c
82  */
83 int smb_md5_getmech(smb_crypto_mech_t *);
84 int smb_md5_init(smb_sign_ctx_t *, smb_crypto_mech_t *);
85 int smb_md5_update(smb_sign_ctx_t, void *, size_t);
86 int smb_md5_final(smb_sign_ctx_t, uint8_t *);
87 
88 /*
89  * SMB2/3 signing routines used in smb2_signing.c
90  * Two implementations of these (kernel/user) in:
91  *	uts/common/fs/smbsrv/smb2_sign_kcf.c
92  *	lib/smbsrv/libfksmbsrv/common/fksmb_sign_pkcs.c
93  */
94 
95 int smb2_hmac_getmech(smb_crypto_mech_t *);
96 int smb2_hmac_init(smb_sign_ctx_t *, smb_crypto_mech_t *, uint8_t *, size_t);
97 int smb2_hmac_update(smb_sign_ctx_t, uint8_t *, size_t);
98 int smb2_hmac_final(smb_sign_ctx_t, uint8_t *);
99 
100 int smb2_hmac_one(smb_crypto_mech_t *mech, uint8_t *key, size_t key_len,
101     uint8_t *data, size_t data_len, uint8_t *mac, size_t mac_len);
102 
103 int smb3_cmac_getmech(smb_crypto_mech_t *);
104 int smb3_cmac_init(smb_sign_ctx_t *, smb_crypto_mech_t *, uint8_t *, size_t);
105 int smb3_cmac_update(smb_sign_ctx_t, uint8_t *, size_t);
106 int smb3_cmac_final(smb_sign_ctx_t, uint8_t *);
107 
108 int smb3_kdf(uint8_t *outbuf, uint32_t outbuf_len,
109     uint8_t *key, size_t key_len,
110     uint8_t *label, size_t label_len,
111     uint8_t *context, size_t context_len);
112 
113 int smb3_aes_ccm_getmech(smb_crypto_mech_t *);
114 int smb3_aes_gcm_getmech(smb_crypto_mech_t *);
115 void smb3_crypto_init_ccm_param(smb3_crypto_param_t *, uint8_t *, size_t,
116     uint8_t *, size_t, size_t);
117 void smb3_crypto_init_gcm_param(smb3_crypto_param_t *, uint8_t *, size_t,
118     uint8_t *, size_t);
119 
120 int smb3_encrypt_init(smb3_enc_ctx_t *, smb_crypto_mech_t *,
121     smb3_crypto_param_t *, uint8_t *, size_t, uint8_t *, size_t);
122 int smb3_encrypt_update(smb3_enc_ctx_t *, uint8_t *, size_t);
123 int smb3_encrypt_final(smb3_enc_ctx_t *, uint8_t *);
124 void smb3_encrypt_cancel(smb3_enc_ctx_t *);
125 
126 int smb3_decrypt_init(smb3_enc_ctx_t *, smb_crypto_mech_t *,
127     smb3_crypto_param_t *, uint8_t *, size_t);
128 int smb3_decrypt_update(smb3_enc_ctx_t *, uint8_t *, size_t);
129 int smb3_decrypt_final(smb3_enc_ctx_t *, uint8_t *, size_t);
130 
131 #ifdef	__cplusplus
132 }
133 #endif
134 
135 #endif /* _SMB_KCRYPT_H_ */
136