46657121d261310ed7d9b690edf39b4561ebc3ea
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / rtl8192e / rtllib_crypt.c
1 /*
2  * Host AP crypto routines
3  *
4  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5  * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation. See README and COPYING for
10  * more details.
11  *
12  */
13
14 #include <linux/version.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20
21 #include "rtllib.h"
22
23 struct rtllib_crypto_alg {
24         struct list_head list;
25         struct rtllib_crypto_ops *ops;
26 };
27
28
29 struct rtllib_crypto {
30         struct list_head algs;
31         spinlock_t lock;
32 };
33
34 static struct rtllib_crypto *hcrypt;
35
36 void rtllib_crypt_deinit_entries(struct rtllib_device *ieee,
37                                            int force)
38 {
39         struct list_head *ptr, *n;
40         struct rtllib_crypt_data *entry;
41
42         for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
43              ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
44                 entry = list_entry(ptr, struct rtllib_crypt_data, list);
45
46                 if (atomic_read(&entry->refcnt) != 0 && !force)
47                         continue;
48
49                 list_del(ptr);
50
51                 if (entry->ops)
52                         entry->ops->deinit(entry->priv);
53                 kfree(entry);
54         }
55 }
56 EXPORT_SYMBOL(rtllib_crypt_deinit_entries);
57
58 void rtllib_crypt_deinit_handler(unsigned long data)
59 {
60         struct rtllib_device *ieee = (struct rtllib_device *)data;
61         unsigned long flags;
62
63         spin_lock_irqsave(&ieee->lock, flags);
64         rtllib_crypt_deinit_entries(ieee, 0);
65         if (!list_empty(&ieee->crypt_deinit_list)) {
66                 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
67                        "deletion list\n", ieee->dev->name);
68                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
69                 add_timer(&ieee->crypt_deinit_timer);
70         }
71         spin_unlock_irqrestore(&ieee->lock, flags);
72
73 }
74 EXPORT_SYMBOL(rtllib_crypt_deinit_handler);
75
76 void rtllib_crypt_delayed_deinit(struct rtllib_device *ieee,
77                                     struct rtllib_crypt_data **crypt)
78 {
79         struct rtllib_crypt_data *tmp;
80         unsigned long flags;
81
82         if (*crypt == NULL)
83                 return;
84
85         tmp = *crypt;
86         *crypt = NULL;
87
88         /* must not run ops->deinit() while there may be pending encrypt or
89          * decrypt operations. Use a list of delayed deinits to avoid needing
90          * locking. */
91
92         spin_lock_irqsave(&ieee->lock, flags);
93         list_add(&tmp->list, &ieee->crypt_deinit_list);
94         if (!timer_pending(&ieee->crypt_deinit_timer)) {
95                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
96                 add_timer(&ieee->crypt_deinit_timer);
97         }
98         spin_unlock_irqrestore(&ieee->lock, flags);
99 }
100 EXPORT_SYMBOL(rtllib_crypt_delayed_deinit);
101
102 int rtllib_register_crypto_ops(struct rtllib_crypto_ops *ops)
103 {
104         unsigned long flags;
105         struct rtllib_crypto_alg *alg;
106
107         if (hcrypt == NULL)
108                 return -1;
109
110         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
111         if (alg == NULL)
112                 return -ENOMEM;
113
114         alg->ops = ops;
115
116         spin_lock_irqsave(&hcrypt->lock, flags);
117         list_add(&alg->list, &hcrypt->algs);
118         spin_unlock_irqrestore(&hcrypt->lock, flags);
119
120         printk(KERN_DEBUG "rtllib_crypt: registered algorithm '%s'\n",
121                ops->name);
122
123         return 0;
124 }
125 EXPORT_SYMBOL(rtllib_register_crypto_ops);
126
127 int rtllib_unregister_crypto_ops(struct rtllib_crypto_ops *ops)
128 {
129         unsigned long flags;
130         struct list_head *ptr;
131         struct rtllib_crypto_alg *del_alg = NULL;
132
133         if (hcrypt == NULL)
134                 return -1;
135
136         spin_lock_irqsave(&hcrypt->lock, flags);
137         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
138                 struct rtllib_crypto_alg *alg =
139                         (struct rtllib_crypto_alg *) ptr;
140                 if (alg->ops == ops) {
141                         list_del(&alg->list);
142                         del_alg = alg;
143                         break;
144                 }
145         }
146         spin_unlock_irqrestore(&hcrypt->lock, flags);
147
148         if (del_alg) {
149                 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm "
150                        "'%s'\n", ops->name);
151                 kfree(del_alg);
152         }
153
154         return del_alg ? 0 : -1;
155 }
156 EXPORT_SYMBOL(rtllib_unregister_crypto_ops);
157
158
159 struct rtllib_crypto_ops *rtllib_get_crypto_ops(const char *name)
160 {
161         unsigned long flags;
162         struct list_head *ptr;
163         struct rtllib_crypto_alg *found_alg = NULL;
164
165         if (hcrypt == NULL)
166                 return NULL;
167
168         spin_lock_irqsave(&hcrypt->lock, flags);
169         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
170                 struct rtllib_crypto_alg *alg =
171                         (struct rtllib_crypto_alg *) ptr;
172                 if (strcmp(alg->ops->name, name) == 0) {
173                         found_alg = alg;
174                         break;
175                 }
176         }
177         spin_unlock_irqrestore(&hcrypt->lock, flags);
178
179         if (found_alg)
180                 return found_alg->ops;
181         else
182                 return NULL;
183 }
184 EXPORT_SYMBOL(rtllib_get_crypto_ops);
185
186
187 static void * rtllib_crypt_null_init(int keyidx) { return (void *) 1; }
188 static void rtllib_crypt_null_deinit(void *priv) {}
189
190 static struct rtllib_crypto_ops rtllib_crypt_null = {
191         .name                   = "NULL",
192         .init                   = rtllib_crypt_null_init,
193         .deinit                 = rtllib_crypt_null_deinit,
194         .encrypt_mpdu           = NULL,
195         .decrypt_mpdu           = NULL,
196         .encrypt_msdu           = NULL,
197         .decrypt_msdu           = NULL,
198         .set_key                = NULL,
199         .get_key                = NULL,
200         .extra_prefix_len       = 0,
201         .extra_postfix_len      = 0,
202         .owner                  = THIS_MODULE,
203 };
204
205
206 int __init rtllib_crypto_init(void)
207 {
208         int ret = -ENOMEM;
209
210         hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
211         if (!hcrypt)
212                 goto out;
213
214         INIT_LIST_HEAD(&hcrypt->algs);
215         spin_lock_init(&hcrypt->lock);
216
217         ret = rtllib_register_crypto_ops(&rtllib_crypt_null);
218         if (ret < 0) {
219                 kfree(hcrypt);
220                 hcrypt = NULL;
221         }
222 out:
223         return ret;
224 }
225
226
227 void __exit rtllib_crypto_deinit(void)
228 {
229         struct list_head *ptr, *n;
230
231         if (hcrypt == NULL)
232                 return;
233
234         for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
235              ptr = n, n = ptr->next) {
236                 struct rtllib_crypto_alg *alg =
237                         (struct rtllib_crypto_alg *) ptr;
238                 list_del(ptr);
239                 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm "
240                        "'%s' (deinit)\n", alg->ops->name);
241                 kfree(alg);
242         }
243
244         kfree(hcrypt);
245 }
246
247 module_init(rtllib_crypto_init);
248 module_exit(rtllib_crypto_deinit);
249
250 MODULE_LICENSE("GPL");