1313#include <linux/errno.h>
1414#include <linux/init.h>
1515#include <linux/kernel.h>
16+ #include <linux/list.h>
1617#include <linux/module.h>
1718#include <linux/string.h>
1819
1920#include "internal.h"
2021
22+ static LIST_HEAD (crypto_template_list );
23+
2124static inline int crypto_set_driver_name (struct crypto_alg * alg )
2225{
2326 static const char suffix [] = "-generic" ;
@@ -35,11 +38,8 @@ static inline int crypto_set_driver_name(struct crypto_alg *alg)
3538 return 0 ;
3639}
3740
38- int crypto_register_alg (struct crypto_alg * alg )
41+ static int crypto_check_alg (struct crypto_alg * alg )
3942{
40- int ret ;
41- struct crypto_alg * q ;
42-
4343 if (alg -> cra_alignmask & (alg -> cra_alignmask + 1 ))
4444 return - EINVAL ;
4545
@@ -51,42 +51,52 @@ int crypto_register_alg(struct crypto_alg *alg)
5151
5252 if (alg -> cra_priority < 0 )
5353 return - EINVAL ;
54-
55- ret = crypto_set_driver_name (alg );
56- if (unlikely (ret ))
57- return ret ;
5854
59- down_write (& crypto_alg_sem );
60-
55+ return crypto_set_driver_name (alg );
56+ }
57+
58+ static int __crypto_register_alg (struct crypto_alg * alg )
59+ {
60+ struct crypto_alg * q ;
61+ int ret = - EEXIST ;
62+
6163 list_for_each_entry (q , & crypto_alg_list , cra_list ) {
62- if (q == alg ) {
63- ret = - EEXIST ;
64+ if (q == alg )
6465 goto out ;
65- }
6666 }
6767
6868 list_add (& alg -> cra_list , & crypto_alg_list );
6969 atomic_set (& alg -> cra_refcnt , 1 );
70+ ret = 0 ;
7071out :
71- up_write (& crypto_alg_sem );
7272 return ret ;
7373}
74+
75+ int crypto_register_alg (struct crypto_alg * alg )
76+ {
77+ int err ;
78+
79+ err = crypto_check_alg (alg );
80+ if (err )
81+ return err ;
82+
83+ down_write (& crypto_alg_sem );
84+ err = __crypto_register_alg (alg );
85+ up_write (& crypto_alg_sem );
86+
87+ return err ;
88+ }
7489EXPORT_SYMBOL_GPL (crypto_register_alg );
7590
7691int crypto_unregister_alg (struct crypto_alg * alg )
7792{
7893 int ret = - ENOENT ;
79- struct crypto_alg * q ;
8094
8195 down_write (& crypto_alg_sem );
82- list_for_each_entry (q , & crypto_alg_list , cra_list ) {
83- if (alg == q ) {
84- list_del (& alg -> cra_list );
85- ret = 0 ;
86- goto out ;
87- }
96+ if (likely (!list_empty (& alg -> cra_list ))) {
97+ list_del_init (& alg -> cra_list );
98+ ret = 0 ;
8899 }
89- out :
90100 up_write (& crypto_alg_sem );
91101
92102 if (ret )
@@ -100,6 +110,108 @@ int crypto_unregister_alg(struct crypto_alg *alg)
100110}
101111EXPORT_SYMBOL_GPL (crypto_unregister_alg );
102112
113+ int crypto_register_template (struct crypto_template * tmpl )
114+ {
115+ struct crypto_template * q ;
116+ int err = - EEXIST ;
117+
118+ down_write (& crypto_alg_sem );
119+
120+ list_for_each_entry (q , & crypto_template_list , list ) {
121+ if (q == tmpl )
122+ goto out ;
123+ }
124+
125+ list_add (& tmpl -> list , & crypto_template_list );
126+ err = 0 ;
127+ out :
128+ up_write (& crypto_alg_sem );
129+ return err ;
130+ }
131+ EXPORT_SYMBOL_GPL (crypto_register_template );
132+
133+ void crypto_unregister_template (struct crypto_template * tmpl )
134+ {
135+ struct crypto_instance * inst ;
136+ struct hlist_node * p , * n ;
137+ struct hlist_head * list ;
138+
139+ down_write (& crypto_alg_sem );
140+
141+ BUG_ON (list_empty (& tmpl -> list ));
142+ list_del_init (& tmpl -> list );
143+
144+ list = & tmpl -> instances ;
145+ hlist_for_each_entry (inst , p , list , list ) {
146+ BUG_ON (list_empty (& inst -> alg .cra_list ));
147+ list_del_init (& inst -> alg .cra_list );
148+ }
149+
150+ up_write (& crypto_alg_sem );
151+
152+ hlist_for_each_entry_safe (inst , p , n , list , list ) {
153+ BUG_ON (atomic_read (& inst -> alg .cra_refcnt ) != 1 );
154+ tmpl -> free (inst );
155+ }
156+ }
157+ EXPORT_SYMBOL_GPL (crypto_unregister_template );
158+
159+ static struct crypto_template * __crypto_lookup_template (const char * name )
160+ {
161+ struct crypto_template * q , * tmpl = NULL ;
162+
163+ down_read (& crypto_alg_sem );
164+ list_for_each_entry (q , & crypto_template_list , list ) {
165+ if (strcmp (q -> name , name ))
166+ continue ;
167+ if (unlikely (!crypto_tmpl_get (q )))
168+ continue ;
169+
170+ tmpl = q ;
171+ break ;
172+ }
173+ up_read (& crypto_alg_sem );
174+
175+ return tmpl ;
176+ }
177+
178+ struct crypto_template * crypto_lookup_template (const char * name )
179+ {
180+ return try_then_request_module (__crypto_lookup_template (name ), name );
181+ }
182+ EXPORT_SYMBOL_GPL (crypto_lookup_template );
183+
184+ int crypto_register_instance (struct crypto_template * tmpl ,
185+ struct crypto_instance * inst )
186+ {
187+ int err = - EINVAL ;
188+
189+ if (inst -> alg .cra_destroy )
190+ goto err ;
191+
192+ err = crypto_check_alg (& inst -> alg );
193+ if (err )
194+ goto err ;
195+
196+ inst -> alg .cra_module = tmpl -> module ;
197+
198+ down_write (& crypto_alg_sem );
199+
200+ err = __crypto_register_alg (& inst -> alg );
201+ if (err )
202+ goto unlock ;
203+
204+ hlist_add_head (& inst -> list , & tmpl -> instances );
205+ inst -> tmpl = tmpl ;
206+
207+ unlock :
208+ up_write (& crypto_alg_sem );
209+
210+ err :
211+ return err ;
212+ }
213+ EXPORT_SYMBOL_GPL (crypto_register_instance );
214+
103215static int __init crypto_algapi_init (void )
104216{
105217 crypto_init_proc ();
0 commit comments