Skip to content

Commit 4b4c6fd

Browse files
Eric Biggerssmfrench
authored andcommitted
smb: client: Use HMAC-SHA256 library for key generation
Convert generate_key() to use the HMAC-SHA256 library instead of a "hmac(sha256)" crypto_shash. This is simpler and faster. With the library there's no need to allocate memory, no need to handle errors, and the HMAC-SHA256 code is accessed directly without inefficient indirect calls and other unnecessary API overhead. Also remove the unnecessary 'hashptr' variable. For now smb3_crypto_shash_allocate() still allocates a "hmac(sha256)" crypto_shash. It will be removed in a later commit. Reviewed-by: Stefan Metzmacher <[email protected]> Acked-by: Ard Biesheuvel <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent af5fea5 commit 4b4c6fd

2 files changed

Lines changed: 15 additions & 54 deletions

File tree

fs/smb/client/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ config CIFS
1616
select CRYPTO_ECB
1717
select CRYPTO_AES
1818
select CRYPTO_LIB_ARC4
19+
select CRYPTO_LIB_SHA256
1920
select CRYPTO_LIB_SHA512
2021
select KEYS
2122
select DNS_RESOLVER

fs/smb/client/smb2transport.c

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/mempool.h>
2020
#include <linux/highmem.h>
2121
#include <crypto/aead.h>
22+
#include <crypto/sha2.h>
2223
#include "cifsglob.h"
2324
#include "cifsproto.h"
2425
#include "smb2proto.h"
@@ -336,76 +337,35 @@ static int generate_key(struct cifs_ses *ses, struct kvec label,
336337
__u8 L256[4] = {0, 0, 1, 0};
337338
int rc = 0;
338339
unsigned char prfhash[SMB2_HMACSHA256_SIZE];
339-
unsigned char *hashptr = prfhash;
340340
struct TCP_Server_Info *server = ses->server;
341+
struct hmac_sha256_ctx hmac_ctx;
341342

342343
memset(prfhash, 0x0, SMB2_HMACSHA256_SIZE);
343344
memset(key, 0x0, key_size);
344345

345346
rc = smb3_crypto_shash_allocate(server);
346347
if (rc) {
347348
cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
348-
goto smb3signkey_ret;
349-
}
350-
351-
rc = crypto_shash_setkey(server->secmech.hmacsha256->tfm,
352-
ses->auth_key.response, SMB2_NTLMV2_SESSKEY_SIZE);
353-
if (rc) {
354-
cifs_server_dbg(VFS, "%s: Could not set with session key\n", __func__);
355-
goto smb3signkey_ret;
356-
}
357-
358-
rc = crypto_shash_init(server->secmech.hmacsha256);
359-
if (rc) {
360-
cifs_server_dbg(VFS, "%s: Could not init sign hmac\n", __func__);
361-
goto smb3signkey_ret;
362-
}
363-
364-
rc = crypto_shash_update(server->secmech.hmacsha256, i, 4);
365-
if (rc) {
366-
cifs_server_dbg(VFS, "%s: Could not update with n\n", __func__);
367-
goto smb3signkey_ret;
368-
}
369-
370-
rc = crypto_shash_update(server->secmech.hmacsha256, label.iov_base, label.iov_len);
371-
if (rc) {
372-
cifs_server_dbg(VFS, "%s: Could not update with label\n", __func__);
373-
goto smb3signkey_ret;
374-
}
375-
376-
rc = crypto_shash_update(server->secmech.hmacsha256, &zero, 1);
377-
if (rc) {
378-
cifs_server_dbg(VFS, "%s: Could not update with zero\n", __func__);
379-
goto smb3signkey_ret;
349+
return rc;
380350
}
381351

382-
rc = crypto_shash_update(server->secmech.hmacsha256, context.iov_base, context.iov_len);
383-
if (rc) {
384-
cifs_server_dbg(VFS, "%s: Could not update with context\n", __func__);
385-
goto smb3signkey_ret;
386-
}
352+
hmac_sha256_init_usingrawkey(&hmac_ctx, ses->auth_key.response,
353+
SMB2_NTLMV2_SESSKEY_SIZE);
354+
hmac_sha256_update(&hmac_ctx, i, 4);
355+
hmac_sha256_update(&hmac_ctx, label.iov_base, label.iov_len);
356+
hmac_sha256_update(&hmac_ctx, &zero, 1);
357+
hmac_sha256_update(&hmac_ctx, context.iov_base, context.iov_len);
387358

388359
if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
389360
(server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)) {
390-
rc = crypto_shash_update(server->secmech.hmacsha256, L256, 4);
361+
hmac_sha256_update(&hmac_ctx, L256, 4);
391362
} else {
392-
rc = crypto_shash_update(server->secmech.hmacsha256, L128, 4);
393-
}
394-
if (rc) {
395-
cifs_server_dbg(VFS, "%s: Could not update with L\n", __func__);
396-
goto smb3signkey_ret;
363+
hmac_sha256_update(&hmac_ctx, L128, 4);
397364
}
365+
hmac_sha256_final(&hmac_ctx, prfhash);
398366

399-
rc = crypto_shash_final(server->secmech.hmacsha256, hashptr);
400-
if (rc) {
401-
cifs_server_dbg(VFS, "%s: Could not generate sha256 hash\n", __func__);
402-
goto smb3signkey_ret;
403-
}
404-
405-
memcpy(key, hashptr, key_size);
406-
407-
smb3signkey_ret:
408-
return rc;
367+
memcpy(key, prfhash, key_size);
368+
return 0;
409369
}
410370

411371
struct derivation {

0 commit comments

Comments
 (0)