forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.c
2475 lines (2181 loc) · 65.7 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* net/tipc/crypto.c: TIPC crypto for key handling & packet en/decryption
*
* Copyright (c) 2019, Ericsson AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL") version 2 as published by the Free
* Software Foundation.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <crypto/aead.h>
#include <crypto/aes.h>
#include <crypto/rng.h>
#include "crypto.h"
#include "msg.h"
#include "bcast.h"
#define TIPC_TX_GRACE_PERIOD msecs_to_jiffies(5000) /* 5s */
#define TIPC_TX_LASTING_TIME msecs_to_jiffies(10000) /* 10s */
#define TIPC_RX_ACTIVE_LIM msecs_to_jiffies(3000) /* 3s */
#define TIPC_RX_PASSIVE_LIM msecs_to_jiffies(15000) /* 15s */
#define TIPC_MAX_TFMS_DEF 10
#define TIPC_MAX_TFMS_LIM 1000
#define TIPC_REKEYING_INTV_DEF (60 * 24) /* default: 1 day */
/*
* TIPC Key ids
*/
enum {
KEY_MASTER = 0,
KEY_MIN = KEY_MASTER,
KEY_1 = 1,
KEY_2,
KEY_3,
KEY_MAX = KEY_3,
};
/*
* TIPC Crypto statistics
*/
enum {
STAT_OK,
STAT_NOK,
STAT_ASYNC,
STAT_ASYNC_OK,
STAT_ASYNC_NOK,
STAT_BADKEYS, /* tx only */
STAT_BADMSGS = STAT_BADKEYS, /* rx only */
STAT_NOKEYS,
STAT_SWITCHES,
MAX_STATS,
};
/* TIPC crypto statistics' header */
static const char *hstats[MAX_STATS] = {"ok", "nok", "async", "async_ok",
"async_nok", "badmsgs", "nokeys",
"switches"};
/* Max TFMs number per key */
int sysctl_tipc_max_tfms __read_mostly = TIPC_MAX_TFMS_DEF;
/* Key exchange switch, default: on */
int sysctl_tipc_key_exchange_enabled __read_mostly = 1;
/*
* struct tipc_key - TIPC keys' status indicator
*
* 7 6 5 4 3 2 1 0
* +-----+-----+-----+-----+-----+-----+-----+-----+
* key: | (reserved)|passive idx| active idx|pending idx|
* +-----+-----+-----+-----+-----+-----+-----+-----+
*/
struct tipc_key {
#define KEY_BITS (2)
#define KEY_MASK ((1 << KEY_BITS) - 1)
union {
struct {
#if defined(__LITTLE_ENDIAN_BITFIELD)
u8 pending:2,
active:2,
passive:2, /* rx only */
reserved:2;
#elif defined(__BIG_ENDIAN_BITFIELD)
u8 reserved:2,
passive:2, /* rx only */
active:2,
pending:2;
#else
#error "Please fix <asm/byteorder.h>"
#endif
} __packed;
u8 keys;
};
};
/**
* struct tipc_tfm - TIPC TFM structure to form a list of TFMs
* @tfm: cipher handle/key
* @list: linked list of TFMs
*/
struct tipc_tfm {
struct crypto_aead *tfm;
struct list_head list;
};
/**
* struct tipc_aead - TIPC AEAD key structure
* @tfm_entry: per-cpu pointer to one entry in TFM list
* @crypto: TIPC crypto owns this key
* @cloned: reference to the source key in case cloning
* @users: the number of the key users (TX/RX)
* @salt: the key's SALT value
* @authsize: authentication tag size (max = 16)
* @mode: crypto mode is applied to the key
* @hint: a hint for user key
* @rcu: struct rcu_head
* @key: the aead key
* @gen: the key's generation
* @seqno: the key seqno (cluster scope)
* @refcnt: the key reference counter
*/
struct tipc_aead {
#define TIPC_AEAD_HINT_LEN (5)
struct tipc_tfm * __percpu *tfm_entry;
struct tipc_crypto *crypto;
struct tipc_aead *cloned;
atomic_t users;
u32 salt;
u8 authsize;
u8 mode;
char hint[2 * TIPC_AEAD_HINT_LEN + 1];
struct rcu_head rcu;
struct tipc_aead_key *key;
u16 gen;
atomic64_t seqno ____cacheline_aligned;
refcount_t refcnt ____cacheline_aligned;
} ____cacheline_aligned;
/**
* struct tipc_crypto_stats - TIPC Crypto statistics
* @stat: array of crypto statistics
*/
struct tipc_crypto_stats {
unsigned int stat[MAX_STATS];
};
/**
* struct tipc_crypto - TIPC TX/RX crypto structure
* @net: struct net
* @node: TIPC node (RX)
* @aead: array of pointers to AEAD keys for encryption/decryption
* @peer_rx_active: replicated peer RX active key index
* @key_gen: TX/RX key generation
* @key: the key states
* @skey_mode: session key's mode
* @skey: received session key
* @wq: common workqueue on TX crypto
* @work: delayed work sched for TX/RX
* @key_distr: key distributing state
* @rekeying_intv: rekeying interval (in minutes)
* @stats: the crypto statistics
* @name: the crypto name
* @sndnxt: the per-peer sndnxt (TX)
* @timer1: general timer 1 (jiffies)
* @timer2: general timer 2 (jiffies)
* @working: the crypto is working or not
* @key_master: flag indicates if master key exists
* @legacy_user: flag indicates if a peer joins w/o master key (for bwd comp.)
* @nokey: no key indication
* @flags: combined flags field
* @lock: tipc_key lock
*/
struct tipc_crypto {
struct net *net;
struct tipc_node *node;
struct tipc_aead __rcu *aead[KEY_MAX + 1];
atomic_t peer_rx_active;
u16 key_gen;
struct tipc_key key;
u8 skey_mode;
struct tipc_aead_key *skey;
struct workqueue_struct *wq;
struct delayed_work work;
#define KEY_DISTR_SCHED 1
#define KEY_DISTR_COMPL 2
atomic_t key_distr;
u32 rekeying_intv;
struct tipc_crypto_stats __percpu *stats;
char name[48];
atomic64_t sndnxt ____cacheline_aligned;
unsigned long timer1;
unsigned long timer2;
union {
struct {
u8 working:1;
u8 key_master:1;
u8 legacy_user:1;
u8 nokey: 1;
};
u8 flags;
};
spinlock_t lock; /* crypto lock */
} ____cacheline_aligned;
/* struct tipc_crypto_tx_ctx - TX context for callbacks */
struct tipc_crypto_tx_ctx {
struct tipc_aead *aead;
struct tipc_bearer *bearer;
struct tipc_media_addr dst;
};
/* struct tipc_crypto_rx_ctx - RX context for callbacks */
struct tipc_crypto_rx_ctx {
struct tipc_aead *aead;
struct tipc_bearer *bearer;
};
static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead);
static inline void tipc_aead_put(struct tipc_aead *aead);
static void tipc_aead_free(struct rcu_head *rp);
static int tipc_aead_users(struct tipc_aead __rcu *aead);
static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim);
static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim);
static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val);
static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead);
static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
u8 mode);
static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src);
static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
unsigned int crypto_ctx_size,
u8 **iv, struct aead_request **req,
struct scatterlist **sg, int nsg);
static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
struct tipc_bearer *b,
struct tipc_media_addr *dst,
struct tipc_node *__dnode);
static void tipc_aead_encrypt_done(void *data, int err);
static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
struct sk_buff *skb, struct tipc_bearer *b);
static void tipc_aead_decrypt_done(void *data, int err);
static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr);
static int tipc_ehdr_build(struct net *net, struct tipc_aead *aead,
u8 tx_key, struct sk_buff *skb,
struct tipc_crypto *__rx);
static inline void tipc_crypto_key_set_state(struct tipc_crypto *c,
u8 new_passive,
u8 new_active,
u8 new_pending);
static int tipc_crypto_key_attach(struct tipc_crypto *c,
struct tipc_aead *aead, u8 pos,
bool master_key);
static bool tipc_crypto_key_try_align(struct tipc_crypto *rx, u8 new_pending);
static struct tipc_aead *tipc_crypto_key_pick_tx(struct tipc_crypto *tx,
struct tipc_crypto *rx,
struct sk_buff *skb,
u8 tx_key);
static void tipc_crypto_key_synch(struct tipc_crypto *rx, struct sk_buff *skb);
static int tipc_crypto_key_revoke(struct net *net, u8 tx_key);
static inline void tipc_crypto_clone_msg(struct net *net, struct sk_buff *_skb,
struct tipc_bearer *b,
struct tipc_media_addr *dst,
struct tipc_node *__dnode, u8 type);
static void tipc_crypto_rcv_complete(struct net *net, struct tipc_aead *aead,
struct tipc_bearer *b,
struct sk_buff **skb, int err);
static void tipc_crypto_do_cmd(struct net *net, int cmd);
static char *tipc_crypto_key_dump(struct tipc_crypto *c, char *buf);
static char *tipc_key_change_dump(struct tipc_key old, struct tipc_key new,
char *buf);
static int tipc_crypto_key_xmit(struct net *net, struct tipc_aead_key *skey,
u16 gen, u8 mode, u32 dnode);
static bool tipc_crypto_key_rcv(struct tipc_crypto *rx, struct tipc_msg *hdr);
static void tipc_crypto_work_tx(struct work_struct *work);
static void tipc_crypto_work_rx(struct work_struct *work);
static int tipc_aead_key_generate(struct tipc_aead_key *skey);
#define is_tx(crypto) (!(crypto)->node)
#define is_rx(crypto) (!is_tx(crypto))
#define key_next(cur) ((cur) % KEY_MAX + 1)
#define tipc_aead_rcu_ptr(rcu_ptr, lock) \
rcu_dereference_protected((rcu_ptr), lockdep_is_held(lock))
#define tipc_aead_rcu_replace(rcu_ptr, ptr, lock) \
do { \
struct tipc_aead *__tmp = rcu_dereference_protected((rcu_ptr), \
lockdep_is_held(lock)); \
rcu_assign_pointer((rcu_ptr), (ptr)); \
tipc_aead_put(__tmp); \
} while (0)
#define tipc_crypto_key_detach(rcu_ptr, lock) \
tipc_aead_rcu_replace((rcu_ptr), NULL, lock)
/**
* tipc_aead_key_validate - Validate a AEAD user key
* @ukey: pointer to user key data
* @info: netlink info pointer
*/
int tipc_aead_key_validate(struct tipc_aead_key *ukey, struct genl_info *info)
{
int keylen;
/* Check if algorithm exists */
if (unlikely(!crypto_has_alg(ukey->alg_name, 0, 0))) {
GENL_SET_ERR_MSG(info, "unable to load the algorithm (module existed?)");
return -ENODEV;
}
/* Currently, we only support the "gcm(aes)" cipher algorithm */
if (strcmp(ukey->alg_name, "gcm(aes)")) {
GENL_SET_ERR_MSG(info, "not supported yet the algorithm");
return -ENOTSUPP;
}
/* Check if key size is correct */
keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
if (unlikely(keylen != TIPC_AES_GCM_KEY_SIZE_128 &&
keylen != TIPC_AES_GCM_KEY_SIZE_192 &&
keylen != TIPC_AES_GCM_KEY_SIZE_256)) {
GENL_SET_ERR_MSG(info, "incorrect key length (20, 28 or 36 octets?)");
return -EKEYREJECTED;
}
return 0;
}
/**
* tipc_aead_key_generate - Generate new session key
* @skey: input/output key with new content
*
* Return: 0 in case of success, otherwise < 0
*/
static int tipc_aead_key_generate(struct tipc_aead_key *skey)
{
int rc = 0;
/* Fill the key's content with a random value via RNG cipher */
rc = crypto_get_default_rng();
if (likely(!rc)) {
rc = crypto_rng_get_bytes(crypto_default_rng, skey->key,
skey->keylen);
crypto_put_default_rng();
}
return rc;
}
static struct tipc_aead *tipc_aead_get(struct tipc_aead __rcu *aead)
{
struct tipc_aead *tmp;
rcu_read_lock();
tmp = rcu_dereference(aead);
if (unlikely(!tmp || !refcount_inc_not_zero(&tmp->refcnt)))
tmp = NULL;
rcu_read_unlock();
return tmp;
}
static inline void tipc_aead_put(struct tipc_aead *aead)
{
if (aead && refcount_dec_and_test(&aead->refcnt))
call_rcu(&aead->rcu, tipc_aead_free);
}
/**
* tipc_aead_free - Release AEAD key incl. all the TFMs in the list
* @rp: rcu head pointer
*/
static void tipc_aead_free(struct rcu_head *rp)
{
struct tipc_aead *aead = container_of(rp, struct tipc_aead, rcu);
struct tipc_tfm *tfm_entry, *head, *tmp;
if (aead->cloned) {
tipc_aead_put(aead->cloned);
} else {
head = *get_cpu_ptr(aead->tfm_entry);
put_cpu_ptr(aead->tfm_entry);
list_for_each_entry_safe(tfm_entry, tmp, &head->list, list) {
crypto_free_aead(tfm_entry->tfm);
list_del(&tfm_entry->list);
kfree(tfm_entry);
}
/* Free the head */
crypto_free_aead(head->tfm);
list_del(&head->list);
kfree(head);
}
free_percpu(aead->tfm_entry);
kfree_sensitive(aead->key);
kfree(aead);
}
static int tipc_aead_users(struct tipc_aead __rcu *aead)
{
struct tipc_aead *tmp;
int users = 0;
rcu_read_lock();
tmp = rcu_dereference(aead);
if (tmp)
users = atomic_read(&tmp->users);
rcu_read_unlock();
return users;
}
static void tipc_aead_users_inc(struct tipc_aead __rcu *aead, int lim)
{
struct tipc_aead *tmp;
rcu_read_lock();
tmp = rcu_dereference(aead);
if (tmp)
atomic_add_unless(&tmp->users, 1, lim);
rcu_read_unlock();
}
static void tipc_aead_users_dec(struct tipc_aead __rcu *aead, int lim)
{
struct tipc_aead *tmp;
rcu_read_lock();
tmp = rcu_dereference(aead);
if (tmp)
atomic_add_unless(&rcu_dereference(aead)->users, -1, lim);
rcu_read_unlock();
}
static void tipc_aead_users_set(struct tipc_aead __rcu *aead, int val)
{
struct tipc_aead *tmp;
int cur;
rcu_read_lock();
tmp = rcu_dereference(aead);
if (tmp) {
do {
cur = atomic_read(&tmp->users);
if (cur == val)
break;
} while (atomic_cmpxchg(&tmp->users, cur, val) != cur);
}
rcu_read_unlock();
}
/**
* tipc_aead_tfm_next - Move TFM entry to the next one in list and return it
* @aead: the AEAD key pointer
*/
static struct crypto_aead *tipc_aead_tfm_next(struct tipc_aead *aead)
{
struct tipc_tfm **tfm_entry;
struct crypto_aead *tfm;
tfm_entry = get_cpu_ptr(aead->tfm_entry);
*tfm_entry = list_next_entry(*tfm_entry, list);
tfm = (*tfm_entry)->tfm;
put_cpu_ptr(tfm_entry);
return tfm;
}
/**
* tipc_aead_init - Initiate TIPC AEAD
* @aead: returned new TIPC AEAD key handle pointer
* @ukey: pointer to user key data
* @mode: the key mode
*
* Allocate a (list of) new cipher transformation (TFM) with the specific user
* key data if valid. The number of the allocated TFMs can be set via the sysfs
* "net/tipc/max_tfms" first.
* Also, all the other AEAD data are also initialized.
*
* Return: 0 if the initiation is successful, otherwise: < 0
*/
static int tipc_aead_init(struct tipc_aead **aead, struct tipc_aead_key *ukey,
u8 mode)
{
struct tipc_tfm *tfm_entry, *head;
struct crypto_aead *tfm;
struct tipc_aead *tmp;
int keylen, err, cpu;
int tfm_cnt = 0;
if (unlikely(*aead))
return -EEXIST;
/* Allocate a new AEAD */
tmp = kzalloc(sizeof(*tmp), GFP_ATOMIC);
if (unlikely(!tmp))
return -ENOMEM;
/* The key consists of two parts: [AES-KEY][SALT] */
keylen = ukey->keylen - TIPC_AES_GCM_SALT_SIZE;
/* Allocate per-cpu TFM entry pointer */
tmp->tfm_entry = alloc_percpu(struct tipc_tfm *);
if (!tmp->tfm_entry) {
kfree_sensitive(tmp);
return -ENOMEM;
}
/* Make a list of TFMs with the user key data */
do {
tfm = crypto_alloc_aead(ukey->alg_name, 0, 0);
if (IS_ERR(tfm)) {
err = PTR_ERR(tfm);
break;
}
if (unlikely(!tfm_cnt &&
crypto_aead_ivsize(tfm) != TIPC_AES_GCM_IV_SIZE)) {
crypto_free_aead(tfm);
err = -ENOTSUPP;
break;
}
err = crypto_aead_setauthsize(tfm, TIPC_AES_GCM_TAG_SIZE);
err |= crypto_aead_setkey(tfm, ukey->key, keylen);
if (unlikely(err)) {
crypto_free_aead(tfm);
break;
}
tfm_entry = kmalloc(sizeof(*tfm_entry), GFP_KERNEL);
if (unlikely(!tfm_entry)) {
crypto_free_aead(tfm);
err = -ENOMEM;
break;
}
INIT_LIST_HEAD(&tfm_entry->list);
tfm_entry->tfm = tfm;
/* First entry? */
if (!tfm_cnt) {
head = tfm_entry;
for_each_possible_cpu(cpu) {
*per_cpu_ptr(tmp->tfm_entry, cpu) = head;
}
} else {
list_add_tail(&tfm_entry->list, &head->list);
}
} while (++tfm_cnt < sysctl_tipc_max_tfms);
/* Not any TFM is allocated? */
if (!tfm_cnt) {
free_percpu(tmp->tfm_entry);
kfree_sensitive(tmp);
return err;
}
/* Form a hex string of some last bytes as the key's hint */
bin2hex(tmp->hint, ukey->key + keylen - TIPC_AEAD_HINT_LEN,
TIPC_AEAD_HINT_LEN);
/* Initialize the other data */
tmp->mode = mode;
tmp->cloned = NULL;
tmp->authsize = TIPC_AES_GCM_TAG_SIZE;
tmp->key = kmemdup(ukey, tipc_aead_key_size(ukey), GFP_KERNEL);
if (!tmp->key) {
tipc_aead_free(&tmp->rcu);
return -ENOMEM;
}
memcpy(&tmp->salt, ukey->key + keylen, TIPC_AES_GCM_SALT_SIZE);
atomic_set(&tmp->users, 0);
atomic64_set(&tmp->seqno, 0);
refcount_set(&tmp->refcnt, 1);
*aead = tmp;
return 0;
}
/**
* tipc_aead_clone - Clone a TIPC AEAD key
* @dst: dest key for the cloning
* @src: source key to clone from
*
* Make a "copy" of the source AEAD key data to the dest, the TFMs list is
* common for the keys.
* A reference to the source is hold in the "cloned" pointer for the later
* freeing purposes.
*
* Note: this must be done in cluster-key mode only!
* Return: 0 in case of success, otherwise < 0
*/
static int tipc_aead_clone(struct tipc_aead **dst, struct tipc_aead *src)
{
struct tipc_aead *aead;
int cpu;
if (!src)
return -ENOKEY;
if (src->mode != CLUSTER_KEY)
return -EINVAL;
if (unlikely(*dst))
return -EEXIST;
aead = kzalloc(sizeof(*aead), GFP_ATOMIC);
if (unlikely(!aead))
return -ENOMEM;
aead->tfm_entry = alloc_percpu_gfp(struct tipc_tfm *, GFP_ATOMIC);
if (unlikely(!aead->tfm_entry)) {
kfree_sensitive(aead);
return -ENOMEM;
}
for_each_possible_cpu(cpu) {
*per_cpu_ptr(aead->tfm_entry, cpu) =
*per_cpu_ptr(src->tfm_entry, cpu);
}
memcpy(aead->hint, src->hint, sizeof(src->hint));
aead->mode = src->mode;
aead->salt = src->salt;
aead->authsize = src->authsize;
atomic_set(&aead->users, 0);
atomic64_set(&aead->seqno, 0);
refcount_set(&aead->refcnt, 1);
WARN_ON(!refcount_inc_not_zero(&src->refcnt));
aead->cloned = src;
*dst = aead;
return 0;
}
/**
* tipc_aead_mem_alloc - Allocate memory for AEAD request operations
* @tfm: cipher handle to be registered with the request
* @crypto_ctx_size: size of crypto context for callback
* @iv: returned pointer to IV data
* @req: returned pointer to AEAD request data
* @sg: returned pointer to SG lists
* @nsg: number of SG lists to be allocated
*
* Allocate memory to store the crypto context data, AEAD request, IV and SG
* lists, the memory layout is as follows:
* crypto_ctx || iv || aead_req || sg[]
*
* Return: the pointer to the memory areas in case of success, otherwise NULL
*/
static void *tipc_aead_mem_alloc(struct crypto_aead *tfm,
unsigned int crypto_ctx_size,
u8 **iv, struct aead_request **req,
struct scatterlist **sg, int nsg)
{
unsigned int iv_size, req_size;
unsigned int len;
u8 *mem;
iv_size = crypto_aead_ivsize(tfm);
req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
len = crypto_ctx_size;
len += iv_size;
len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
len = ALIGN(len, crypto_tfm_ctx_alignment());
len += req_size;
len = ALIGN(len, __alignof__(struct scatterlist));
len += nsg * sizeof(**sg);
mem = kmalloc(len, GFP_ATOMIC);
if (!mem)
return NULL;
*iv = (u8 *)PTR_ALIGN(mem + crypto_ctx_size,
crypto_aead_alignmask(tfm) + 1);
*req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
crypto_tfm_ctx_alignment());
*sg = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
__alignof__(struct scatterlist));
return (void *)mem;
}
/**
* tipc_aead_encrypt - Encrypt a message
* @aead: TIPC AEAD key for the message encryption
* @skb: the input/output skb
* @b: TIPC bearer where the message will be delivered after the encryption
* @dst: the destination media address
* @__dnode: TIPC dest node if "known"
*
* Return:
* * 0 : if the encryption has completed
* * -EINPROGRESS/-EBUSY : if a callback will be performed
* * < 0 : the encryption has failed
*/
static int tipc_aead_encrypt(struct tipc_aead *aead, struct sk_buff *skb,
struct tipc_bearer *b,
struct tipc_media_addr *dst,
struct tipc_node *__dnode)
{
struct crypto_aead *tfm = tipc_aead_tfm_next(aead);
struct tipc_crypto_tx_ctx *tx_ctx;
struct aead_request *req;
struct sk_buff *trailer;
struct scatterlist *sg;
struct tipc_ehdr *ehdr;
int ehsz, len, tailen, nsg, rc;
void *ctx;
u32 salt;
u8 *iv;
/* Make sure message len at least 4-byte aligned */
len = ALIGN(skb->len, 4);
tailen = len - skb->len + aead->authsize;
/* Expand skb tail for authentication tag:
* As for simplicity, we'd have made sure skb having enough tailroom
* for authentication tag @skb allocation. Even when skb is nonlinear
* but there is no frag_list, it should be still fine!
* Otherwise, we must cow it to be a writable buffer with the tailroom.
*/
SKB_LINEAR_ASSERT(skb);
if (tailen > skb_tailroom(skb)) {
pr_debug("TX(): skb tailroom is not enough: %d, requires: %d\n",
skb_tailroom(skb), tailen);
}
nsg = skb_cow_data(skb, tailen, &trailer);
if (unlikely(nsg < 0)) {
pr_err("TX: skb_cow_data() returned %d\n", nsg);
return nsg;
}
pskb_put(skb, trailer, tailen);
/* Allocate memory for the AEAD operation */
ctx = tipc_aead_mem_alloc(tfm, sizeof(*tx_ctx), &iv, &req, &sg, nsg);
if (unlikely(!ctx))
return -ENOMEM;
TIPC_SKB_CB(skb)->crypto_ctx = ctx;
/* Map skb to the sg lists */
sg_init_table(sg, nsg);
rc = skb_to_sgvec(skb, sg, 0, skb->len);
if (unlikely(rc < 0)) {
pr_err("TX: skb_to_sgvec() returned %d, nsg %d!\n", rc, nsg);
goto exit;
}
/* Prepare IV: [SALT (4 octets)][SEQNO (8 octets)]
* In case we're in cluster-key mode, SALT is varied by xor-ing with
* the source address (or w0 of id), otherwise with the dest address
* if dest is known.
*/
ehdr = (struct tipc_ehdr *)skb->data;
salt = aead->salt;
if (aead->mode == CLUSTER_KEY)
salt ^= __be32_to_cpu(ehdr->addr);
else if (__dnode)
salt ^= tipc_node_get_addr(__dnode);
memcpy(iv, &salt, 4);
memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
/* Prepare request */
ehsz = tipc_ehdr_size(ehdr);
aead_request_set_tfm(req, tfm);
aead_request_set_ad(req, ehsz);
aead_request_set_crypt(req, sg, sg, len - ehsz, iv);
/* Set callback function & data */
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
tipc_aead_encrypt_done, skb);
tx_ctx = (struct tipc_crypto_tx_ctx *)ctx;
tx_ctx->aead = aead;
tx_ctx->bearer = b;
memcpy(&tx_ctx->dst, dst, sizeof(*dst));
/* Hold bearer */
if (unlikely(!tipc_bearer_hold(b))) {
rc = -ENODEV;
goto exit;
}
/* Now, do encrypt */
rc = crypto_aead_encrypt(req);
if (rc == -EINPROGRESS || rc == -EBUSY)
return rc;
tipc_bearer_put(b);
exit:
kfree(ctx);
TIPC_SKB_CB(skb)->crypto_ctx = NULL;
return rc;
}
static void tipc_aead_encrypt_done(void *data, int err)
{
struct sk_buff *skb = data;
struct tipc_crypto_tx_ctx *tx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
struct tipc_bearer *b = tx_ctx->bearer;
struct tipc_aead *aead = tx_ctx->aead;
struct tipc_crypto *tx = aead->crypto;
struct net *net = tx->net;
switch (err) {
case 0:
this_cpu_inc(tx->stats->stat[STAT_ASYNC_OK]);
rcu_read_lock();
if (likely(test_bit(0, &b->up)))
b->media->send_msg(net, skb, b, &tx_ctx->dst);
else
kfree_skb(skb);
rcu_read_unlock();
break;
case -EINPROGRESS:
return;
default:
this_cpu_inc(tx->stats->stat[STAT_ASYNC_NOK]);
kfree_skb(skb);
break;
}
kfree(tx_ctx);
tipc_bearer_put(b);
tipc_aead_put(aead);
}
/**
* tipc_aead_decrypt - Decrypt an encrypted message
* @net: struct net
* @aead: TIPC AEAD for the message decryption
* @skb: the input/output skb
* @b: TIPC bearer where the message has been received
*
* Return:
* * 0 : if the decryption has completed
* * -EINPROGRESS/-EBUSY : if a callback will be performed
* * < 0 : the decryption has failed
*/
static int tipc_aead_decrypt(struct net *net, struct tipc_aead *aead,
struct sk_buff *skb, struct tipc_bearer *b)
{
struct tipc_crypto_rx_ctx *rx_ctx;
struct aead_request *req;
struct crypto_aead *tfm;
struct sk_buff *unused;
struct scatterlist *sg;
struct tipc_ehdr *ehdr;
int ehsz, nsg, rc;
void *ctx;
u32 salt;
u8 *iv;
if (unlikely(!aead))
return -ENOKEY;
nsg = skb_cow_data(skb, 0, &unused);
if (unlikely(nsg < 0)) {
pr_err("RX: skb_cow_data() returned %d\n", nsg);
return nsg;
}
/* Allocate memory for the AEAD operation */
tfm = tipc_aead_tfm_next(aead);
ctx = tipc_aead_mem_alloc(tfm, sizeof(*rx_ctx), &iv, &req, &sg, nsg);
if (unlikely(!ctx))
return -ENOMEM;
TIPC_SKB_CB(skb)->crypto_ctx = ctx;
/* Map skb to the sg lists */
sg_init_table(sg, nsg);
rc = skb_to_sgvec(skb, sg, 0, skb->len);
if (unlikely(rc < 0)) {
pr_err("RX: skb_to_sgvec() returned %d, nsg %d\n", rc, nsg);
goto exit;
}
/* Reconstruct IV: */
ehdr = (struct tipc_ehdr *)skb->data;
salt = aead->salt;
if (aead->mode == CLUSTER_KEY)
salt ^= __be32_to_cpu(ehdr->addr);
else if (ehdr->destined)
salt ^= tipc_own_addr(net);
memcpy(iv, &salt, 4);
memcpy(iv + 4, (u8 *)&ehdr->seqno, 8);
/* Prepare request */
ehsz = tipc_ehdr_size(ehdr);
aead_request_set_tfm(req, tfm);
aead_request_set_ad(req, ehsz);
aead_request_set_crypt(req, sg, sg, skb->len - ehsz, iv);
/* Set callback function & data */
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
tipc_aead_decrypt_done, skb);
rx_ctx = (struct tipc_crypto_rx_ctx *)ctx;
rx_ctx->aead = aead;
rx_ctx->bearer = b;
/* Hold bearer */
if (unlikely(!tipc_bearer_hold(b))) {
rc = -ENODEV;
goto exit;
}
/* Now, do decrypt */
rc = crypto_aead_decrypt(req);
if (rc == -EINPROGRESS || rc == -EBUSY)
return rc;
tipc_bearer_put(b);
exit:
kfree(ctx);
TIPC_SKB_CB(skb)->crypto_ctx = NULL;
return rc;
}
static void tipc_aead_decrypt_done(void *data, int err)
{
struct sk_buff *skb = data;
struct tipc_crypto_rx_ctx *rx_ctx = TIPC_SKB_CB(skb)->crypto_ctx;
struct tipc_bearer *b = rx_ctx->bearer;
struct tipc_aead *aead = rx_ctx->aead;
struct tipc_crypto_stats __percpu *stats = aead->crypto->stats;
struct net *net = aead->crypto->net;
switch (err) {
case 0:
this_cpu_inc(stats->stat[STAT_ASYNC_OK]);
break;
case -EINPROGRESS:
return;
default:
this_cpu_inc(stats->stat[STAT_ASYNC_NOK]);
break;
}
kfree(rx_ctx);
tipc_crypto_rcv_complete(net, aead, b, &skb, err);
if (likely(skb)) {
if (likely(test_bit(0, &b->up)))
tipc_rcv(net, skb, b);
else
kfree_skb(skb);
}
tipc_bearer_put(b);
}
static inline int tipc_ehdr_size(struct tipc_ehdr *ehdr)
{
return (ehdr->user != LINK_CONFIG) ? EHDR_SIZE : EHDR_CFG_SIZE;
}
/**
* tipc_ehdr_validate - Validate an encryption message
* @skb: the message buffer
*
* Return: "true" if this is a valid encryption message, otherwise "false"
*/
bool tipc_ehdr_validate(struct sk_buff *skb)