Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015, Qualcomm Atheros, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/types.h> |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 11 | #include <linux/err.h> |
Herbert Xu | d8fe0dd | 2015-04-22 15:06:32 +0800 | [diff] [blame] | 12 | #include <crypto/aead.h> |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 13 | |
| 14 | #include <net/mac80211.h> |
| 15 | #include "key.h" |
| 16 | #include "aes_gcm.h" |
| 17 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 18 | int ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad, |
| 19 | u8 *data, size_t data_len, u8 *mic) |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 20 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 21 | struct scatterlist sg[3]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 22 | struct aead_request *aead_req; |
| 23 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 24 | u8 *__aad; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 25 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 26 | aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC); |
| 27 | if (!aead_req) |
| 28 | return -ENOMEM; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 29 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 30 | __aad = (u8 *)aead_req + reqsize; |
| 31 | memcpy(__aad, aad, GCM_AAD_LEN); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 32 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 33 | sg_init_table(sg, 3); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 34 | sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad)); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 35 | sg_set_buf(&sg[1], data, data_len); |
| 36 | sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 37 | |
| 38 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 39 | aead_request_set_crypt(aead_req, sg, sg, data_len, j_0); |
| 40 | aead_request_set_ad(aead_req, sg[0].length); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 41 | |
| 42 | crypto_aead_encrypt(aead_req); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 43 | kzfree(aead_req); |
| 44 | return 0; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad, |
| 48 | u8 *data, size_t data_len, u8 *mic) |
| 49 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 50 | struct scatterlist sg[3]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 51 | struct aead_request *aead_req; |
| 52 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 53 | u8 *__aad; |
| 54 | int err; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 55 | |
| 56 | if (data_len == 0) |
| 57 | return -EINVAL; |
| 58 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 59 | aead_req = kzalloc(reqsize + GCM_AAD_LEN, GFP_ATOMIC); |
| 60 | if (!aead_req) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | __aad = (u8 *)aead_req + reqsize; |
| 64 | memcpy(__aad, aad, GCM_AAD_LEN); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 65 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 66 | sg_init_table(sg, 3); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 67 | sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad)); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 68 | sg_set_buf(&sg[1], data, data_len); |
| 69 | sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 70 | |
| 71 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 72 | aead_request_set_crypt(aead_req, sg, sg, |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 73 | data_len + IEEE80211_GCMP_MIC_LEN, j_0); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 74 | aead_request_set_ad(aead_req, sg[0].length); |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 75 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 76 | err = crypto_aead_decrypt(aead_req); |
| 77 | kzfree(aead_req); |
| 78 | |
| 79 | return err; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[], |
| 83 | size_t key_len) |
| 84 | { |
| 85 | struct crypto_aead *tfm; |
| 86 | int err; |
| 87 | |
| 88 | tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 89 | if (IS_ERR(tfm)) |
| 90 | return tfm; |
| 91 | |
| 92 | err = crypto_aead_setkey(tfm, key, key_len); |
Johannes Berg | 07862e1 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 93 | if (err) |
| 94 | goto free_aead; |
| 95 | err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN); |
| 96 | if (err) |
| 97 | goto free_aead; |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 98 | |
Johannes Berg | 07862e1 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 99 | return tfm; |
| 100 | |
| 101 | free_aead: |
Jouni Malinen | 00b9cfa | 2015-01-24 19:52:06 +0200 | [diff] [blame] | 102 | crypto_free_aead(tfm); |
| 103 | return ERR_PTR(err); |
| 104 | } |
| 105 | |
| 106 | void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm) |
| 107 | { |
| 108 | crypto_free_aead(tfm); |
| 109 | } |