Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AES-GMAC for IEEE 802.11 BIP-GMAC-128 and BIP-GMAC-256 |
| 3 | * Copyright 2015, Qualcomm Atheros, Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/types.h> |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 12 | #include <linux/err.h> |
Herbert Xu | d8fe0dd | 2015-04-22 15:06:32 +0800 | [diff] [blame] | 13 | #include <crypto/aead.h> |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 14 | #include <crypto/aes.h> |
| 15 | |
| 16 | #include <net/mac80211.h> |
| 17 | #include "key.h" |
| 18 | #include "aes_gmac.h" |
| 19 | |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 20 | int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce, |
| 21 | const u8 *data, size_t data_len, u8 *mic) |
| 22 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 23 | struct scatterlist sg[4]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 24 | u8 *zero, *__aad, iv[AES_BLOCK_SIZE]; |
| 25 | struct aead_request *aead_req; |
| 26 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 27 | |
| 28 | if (data_len < GMAC_MIC_LEN) |
| 29 | return -EINVAL; |
| 30 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 31 | aead_req = kzalloc(reqsize + GMAC_MIC_LEN + GMAC_AAD_LEN, GFP_ATOMIC); |
| 32 | if (!aead_req) |
| 33 | return -ENOMEM; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 34 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 35 | zero = (u8 *)aead_req + reqsize; |
| 36 | __aad = zero + GMAC_MIC_LEN; |
| 37 | memcpy(__aad, aad, GMAC_AAD_LEN); |
| 38 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 39 | sg_init_table(sg, 4); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 40 | sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 41 | sg_set_buf(&sg[1], data, data_len - GMAC_MIC_LEN); |
| 42 | sg_set_buf(&sg[2], zero, GMAC_MIC_LEN); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 43 | sg_set_buf(&sg[3], mic, GMAC_MIC_LEN); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 44 | |
| 45 | memcpy(iv, nonce, GMAC_NONCE_LEN); |
| 46 | memset(iv + GMAC_NONCE_LEN, 0, sizeof(iv) - GMAC_NONCE_LEN); |
| 47 | iv[AES_BLOCK_SIZE - 1] = 0x01; |
| 48 | |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 49 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 50 | aead_request_set_crypt(aead_req, sg, sg, 0, iv); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 51 | aead_request_set_ad(aead_req, GMAC_AAD_LEN + data_len); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 52 | |
| 53 | crypto_aead_encrypt(aead_req); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 54 | kzfree(aead_req); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[], |
| 60 | size_t key_len) |
| 61 | { |
| 62 | struct crypto_aead *tfm; |
| 63 | int err; |
| 64 | |
| 65 | tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 66 | if (IS_ERR(tfm)) |
| 67 | return tfm; |
| 68 | |
| 69 | err = crypto_aead_setkey(tfm, key, key_len); |
| 70 | if (!err) |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 71 | err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN); |
Jouni Malinen | 82ca6ef | 2015-03-23 15:41:15 +0200 | [diff] [blame] | 72 | if (!err) |
| 73 | return tfm; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 74 | |
| 75 | crypto_free_aead(tfm); |
| 76 | return ERR_PTR(err); |
| 77 | } |
| 78 | |
| 79 | void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm) |
| 80 | { |
| 81 | crypto_free_aead(tfm); |
| 82 | } |