-
Notifications
You must be signed in to change notification settings - Fork 0
/
cokey.c
1270 lines (1064 loc) · 34.4 KB
/
cokey.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
/*
* CoKey host driver:
* Driver to integrate a CoKey USB device into a Linux host's crypto API.
*
* Copyright (c) 2015-2016, Fraunhofer AISEC.
* Author: Julian Horsch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
* Long-term TODOs:
* - Adapt to new skcipher Linux crypto API
* - Implement handling of multiple CoKey devices/interfaces on the same system
* - Include the CoKey device serial number into algorithm name provided on the host
* - Handle CoKey device disconnects when in use gracefully
* - Set proper protocol tags, receive and check them
* - Find proper USB vendor/interface class IDs
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/kref.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
/* Crypto API stuff */
#include <linux/crypto.h>
#include <crypto/algapi.h>
#include <crypto/aes.h>
/* Define these values to match your devices */
#define USB_COKEY_VENDOR_ID 0x1d6b
//#define USB_COKEY_PRODUCT_ID 0xfff0
#define USB_COKEY_INTERFACE_CLASS 0xff
#define USB_COKEY_INTERFACE_SUBCLASS 0xab
#define USB_COKEY_INTERFACE_PROTOCOL 0xcd
//#define COKEY_MIN_USB_PACKET_LENGTH 0x8000
//#define COKEY_MIN_USB_PACKET_LENGTH_FAST 0x2000
/* TODO synchronize access to module parameters */
static int cokey_usb_packet_length = 0x8000;
static int cokey_usb_packet_length_fast = 0x3000;
static int cokey_usb_packet_short_retries = 1800;
module_param(cokey_usb_packet_length, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(cokey_usb_packet_length, "Desired USB packet length for aesusbproxy");
module_param(cokey_usb_packet_length_fast, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(cokey_usb_packet_length_fast, "Desired USB packet length for aesusb");
module_param(cokey_usb_packet_short_retries, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(cokey_usb_packet_short_retries, "Times of tasklet rescheduling for short USB packets");
#define COKEY_RESPONSE_CONTAINER
#define CRYPTO_QUEUE_LEN 0x100000
/* table of devices that work with this driver */
static const struct usb_device_id cokey_table[] = {
{ USB_VENDOR_AND_INTERFACE_INFO(USB_COKEY_VENDOR_ID,
USB_COKEY_INTERFACE_CLASS,
USB_COKEY_INTERFACE_SUBCLASS,
USB_COKEY_INTERFACE_PROTOCOL) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, cokey_table);
/***********************************************/
/* cokey USB protocol */
enum cokey_command_code {
COKEY_CMD_SETKEY,
COKEY_CMD_CTR_ENCRYPT,
COKEY_CMD_CTR_DECRYPT,
COKEY_CMD_ECB_ENCRYPT,
COKEY_CMD_ECB_DECRYPT,
COKEY_CMD_SETALG,
COKEY_CMD_GETALG,
COKEY_CMD_CONTAINER,
COKEY_CMD_CONTAINER_RESP_CONTAINER,
};
enum cokey_status_code {
COKEY_STATUS_OK,
COKEY_STATUS_ERROR,
};
#define COKEY_COMMAND_LENGTH (3*4)
#define COKEY_STATUS_LENGTH (2*4)
struct cokey_command {
enum cokey_command_code code;
uint32_t length;
uint32_t tag;
};
struct cokey_status {
enum cokey_status_code code;
uint32_t tag;
};
typedef struct {
struct work_struct my_work;
struct urb *urb;
} cokey_work_urb_t;
struct cokey_reqctx {
int cmd_code;
// uint32_t tag;
};
struct req_list_entry {
struct list_head list;
struct ablkcipher_request *req;
unsigned int response_length;
int cmd_code;
};
/* bool fast_mode determines if a testing proxy mode is used or the normal CoKey
* "fast_mode" */
struct cokey_in_urb_context {
struct cokey_dev *dev;
struct ablkcipher_request *req;
struct list_head *req_list;
bool fast_mode;
};
/* TODO if we want to support multiple usbarmorys attached to a single host:
* - dynamically create a crypto_alg name and structure for each attached
* usbarmory
* - when receiving crypto requests, determine which usbarmory is involved by
* reading the name of the tfm and act accordingly */
static struct cokey_dev *cokey_device;
struct cokey_tfm_ctx {
/* Each transformation is associated with exactly one cokey device */
struct cokey_dev *dev;
/* Each transformation has a key */
uint8_t aes_key[AES_MAX_KEY_SIZE];
int keylen;
bool fast_mode; // this is set depending on which algo is allocated
/* crypto api cipher to be used on the host for fast mode */
struct crypto_ablkcipher *fast_cipher;
};
/* Structure to hold all of our usb device specific data */
struct cokey_dev {
struct usb_device *udev; /* the usb device for this device */
struct usb_interface *interface; /* the interface for this device */
struct usb_anchor submitted; /* in case we need to retract our submissions */
struct kref kref;
spinlock_t lock;
__u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
__u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
//struct mutex io_mutex; /* synchronize I/O with disconnect */
struct urb *current_out_urb;
struct list_head *current_req_list;
bool urb_is_cmd_container;
bool urb_is_fast_mode;
int already_tried;
struct hrtimer timer;
struct workqueue_struct *wq_urb;
struct tasklet_struct tasklet;
/* Crypto API stuff */
struct crypto_queue queue;
struct cokey_tfm_ctx *active_tfm_ctx;
};
#define to_cokey_dev(d) container_of(d, struct cokey_dev, kref)
static struct usb_driver cokey_driver;
static void cokey_delete(struct kref *kref)
{
struct cokey_dev *dev = to_cokey_dev(kref);
flush_workqueue(dev->wq_urb);
destroy_workqueue(dev->wq_urb);
tasklet_kill(&dev->tasklet);
usb_put_dev(dev->udev);
kfree(dev);
}
/* This function is called when a crypto API user calls crypto_alloc_cipher()
* for ctr(aesusbproxy) */
static int cokey_cra_init(struct crypto_tfm *tfm)
{
struct cokey_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
pr_debug("%s enter\n", __func__);
/* set request context size if we need context for each request */
tfm->crt_ablkcipher.reqsize = sizeof(struct cokey_reqctx);
ctx->fast_mode = false;
ctx->fast_cipher = NULL;
return 0;
}
/* This function is called when a crypto API user calls crypto_alloc_cipher()
* for ctr(aesusb) */
static int cokey_cra_fast_init(struct crypto_tfm *tfm)
{
//const char *name = crypto_tfm_alg_name(tfm);
struct cokey_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
pr_debug("%s enter\n", __func__);
/* set request context size if we need context for each request */
tfm->crt_ablkcipher.reqsize = sizeof(struct cokey_reqctx);
ctx->fast_mode = true;
ctx->fast_cipher = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
//CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
if (IS_ERR(ctx->fast_cipher)) {
pr_err("Error allocating cipher for fast mode\n");
return PTR_ERR(ctx->fast_cipher);
}
return 0;
}
static void cokey_cra_exit(struct crypto_tfm *tfm)
{
struct cokey_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
pr_debug("%s enter\n", __func__);
if (ctx->fast_cipher) {
crypto_free_ablkcipher(ctx->fast_cipher);
}
ctx->fast_cipher = NULL;
}
static int cokey_setkey(struct crypto_ablkcipher *cipher, const uint8_t *key, unsigned int keylen)
{
struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
struct cokey_tfm_ctx *ctx = crypto_tfm_ctx(tfm);
int ret = 0;
pr_debug("%s enter\n", __func__);
if (!ctx->fast_mode &&
keylen != AES_KEYSIZE_128 &&
keylen != AES_KEYSIZE_192 &&
keylen != AES_KEYSIZE_256)
return -EINVAL;
memcpy(ctx->aes_key, key, keylen);
ctx->keylen = keylen;
if (ctx->fast_mode) {
pr_debug("%s: setting key in fast host cipher\n", __func__);
ctx->fast_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
ctx->fast_cipher->base.crt_flags |=
(cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK);
ret = crypto_ablkcipher_setkey(ctx->fast_cipher, key, keylen);
if (ret) {
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
tfm->crt_flags |=
(ctx->fast_cipher->base.crt_flags & CRYPTO_TFM_RES_MASK);
}
}
pr_debug("%s exit", __func__);
return ret;
}
static void cokey_cleanup_req_list(struct list_head *req_list, int error)
{
struct req_list_entry *entry, *next;
pr_debug("%s entered\n", __func__);
if (!req_list) {
return;
}
if (!list_empty(req_list)) {
list_for_each_entry_safe(entry, next, req_list, list) {
entry->req->base.complete(&entry->req->base, error);
// remove and free the list entry
list_del(&entry->list);
kfree(entry);
}
}
kfree(req_list);
}
static void cokey_fill_buf_from_cmd(void *buf, struct cokey_command *cmd)
{
*(uint32_t *)(buf) = cpu_to_le32(cmd->code);
*(uint32_t *)(buf+4) = cpu_to_le32(cmd->length);
*(uint32_t *)(buf+8) = cpu_to_le32(cmd->tag);
}
static struct cokey_dev *cokey_get_dev_from_tfm(struct crypto_ablkcipher *tfm)
{
// TODO implement multi-usb-device-handling
return cokey_device;
}
static void cokey_urb_complete_out_cb(struct urb *urb)
{
struct cokey_dev *dev = urb->context;
pr_debug("%s entered\n", __func__);
if (urb->status) {
if (!(urb->status == -ENOENT
|| urb->status == -ECONNRESET
|| urb->status == -ESHUTDOWN))
dev_err(&dev->interface->dev, "%s - nonzero write bulk status received: %d\n", __func__, urb->status);
}
usb_free_urb(urb);
}
static void cokey_handle_request_fast_mode(struct ablkcipher_request *req, int cmd_code, void *response_buf, void *response_buf_end)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct cokey_tfm_ctx *ctx = crypto_ablkcipher_ctx(tfm);
int error = 0;
//pr_debug("%s entered\n", __func__);
/* first check if the request can be satisfied, i.e. is there
* enough data? */
if (response_buf + AES_BLOCK_SIZE > response_buf_end) {
pr_err("%s: Not enough data to handle request\n", __func__);
error = -EFAULT;
goto out;
}
/* copy encrypted IV to req->info */
memcpy(req->info, response_buf, AES_BLOCK_SIZE);
ablkcipher_request_set_tfm(req, ctx->fast_cipher);
if (cmd_code == COKEY_CMD_CTR_ENCRYPT) {
error = crypto_ablkcipher_encrypt(req);
} else if (cmd_code == COKEY_CMD_CTR_DECRYPT) {
error = crypto_ablkcipher_decrypt(req);
} else {
pr_err("unsupported command for cokey fast mode\n");
error = -EFAULT;
}
pr_debug("%s: request to fast host cipher returned with %d\n", __func__, error);
ablkcipher_request_set_tfm(req, tfm);
out:
req->base.complete(&req->base, error);
}
static void cokey_handle_request(struct ablkcipher_request *req, void *response_buf, void *response_buf_end)
{
int retval, error = 0;
//pr_debug("%s entered\n", __func__);
/* first check if the request can be satisfied, i.e. is there
* enough data? */
if (response_buf + AES_BLOCK_SIZE + req->nbytes >
response_buf_end) {
pr_err("%s: Not enough data to handle request\n", __func__);
error = -EFAULT;
goto out;
}
/* copy result iv */
// TODO is this necessary?
memcpy(req->info, response_buf, AES_BLOCK_SIZE);
/* copy result data */
retval = sg_copy_from_buffer(req->dst, sg_nents(req->dst), response_buf+AES_BLOCK_SIZE, req->nbytes);
if (retval != req->nbytes) {
pr_err("could only copy %d/%d bytes to destination scatterlist\n", retval, req->nbytes);
error = -EFAULT;
}
out:
req->base.complete(&req->base, error);
}
static void cokey_wq_incoming_urb(struct work_struct *work)
{
cokey_work_urb_t *cokey_work = (cokey_work_urb_t *)work;
struct urb *urb = cokey_work->urb;
struct cokey_in_urb_context *ctx = urb->context;
struct req_list_entry *entry, *next;
void *urb_transfer_buffer_end = urb->transfer_buffer +
urb->transfer_buffer_length;
void *curr_urb_buf;
if (ctx->req_list) {
/* Iterate over all requests included in this response */
curr_urb_buf = urb->transfer_buffer;
list_for_each_entry_safe(entry, next, ctx->req_list, list) {
if (ctx->fast_mode) {
cokey_handle_request_fast_mode(entry->req, entry->cmd_code, curr_urb_buf,
urb_transfer_buffer_end);
} else {
cokey_handle_request(entry->req, curr_urb_buf,
urb_transfer_buffer_end);
}
/* increment buffer pointer */
curr_urb_buf += entry->response_length;
/* remove and free the list entry */
list_del(&entry->list);
kfree(entry);
}
/* list should be empty by now => free the list head */
kfree(ctx->req_list);
} else if (ctx->req) {
/* only a single request is included within this response */
cokey_handle_request(urb->context, urb->transfer_buffer,
urb_transfer_buffer_end);
} else {
pr_err("%s - invalid urb context structure", __func__);
}
kfree(ctx);
/* freeing the urb should also free the buffer (URB_FREE_BUFFER) */
usb_free_urb(urb);
kfree(work);
}
static void cokey_urb_complete_in_cb(struct urb *urb)
{
struct cokey_in_urb_context *ctx = urb->context;
cokey_work_urb_t *work;
int err = 0;
pr_debug("%s entered\n", __func__);
if (urb->status) {
if (!(urb->status == -ENOENT
|| urb->status == -ECONNRESET
|| urb->status == -ESHUTDOWN))
pr_err("%s - nonzero read bulk status received: %d\n", __func__, urb->status);
}
if (!ctx) {
usb_free_urb(urb);
return;
}
/* init work item */
work = kmalloc(sizeof(cokey_work_urb_t), GFP_ATOMIC);
if (!work) {
pr_err("Could not allocate work struct\n");
err = -ENOMEM;
goto error;
}
work->urb = urb;
/* enqueue work */
INIT_WORK((struct work_struct *)work, cokey_wq_incoming_urb);
queue_work(ctx->dev->wq_urb, (struct work_struct *)work);
return;
error:
if (ctx && ctx->req_list) {
cokey_cleanup_req_list(ctx->req_list, err);
} else if (ctx && ctx->req) {
ctx->req->base.complete(&ctx->req->base, err);
}
usb_free_urb(urb);
}
static int cokey_send_command_urb(struct cokey_dev *dev, struct
cokey_command *cmd)
{
struct urb *urb;
void *buf = NULL;
int retval = 0;
pr_debug("%s entered\n", __func__);
urb = usb_alloc_urb(0, GFP_KERNEL);
// buf = usb_alloc_coherent(dev->udev, COKEY_COMMAND_LENGTH,
// GFP_KERNEL, &urb->transfer_dma);
buf = kmalloc(COKEY_COMMAND_LENGTH, GFP_KERNEL);
if (!buf) {
retval = -ENOMEM;
goto error;
}
cokey_fill_buf_from_cmd(buf, cmd);
usb_fill_bulk_urb(urb, dev->udev,
usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
buf, COKEY_COMMAND_LENGTH,
cokey_urb_complete_out_cb, dev);
//urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
urb->transfer_flags |= URB_FREE_BUFFER;
usb_anchor_urb(urb, &dev->submitted);
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&dev->interface->dev,
"%s - failed submitting write urb, error %d\n",
__func__, retval);
goto error_unanchor;
}
pr_debug("%s: submitted command urb code: %d, length: %d\n", __func__,
cmd->code, cmd->length);
return 0;
error_unanchor:
usb_unanchor_urb(urb);
error:
if (urb) {
//usb_free_coherent(dev->udev, sizeof(struct cokey_command), buf, urb->transfer_dma);
usb_free_urb(urb);
}
return retval;
}
/* don't ask questions, simply send the current urb */
static int cokey_cmd_finish(struct cokey_dev *dev)
{
struct cokey_command cmd;
int retval = 0;
struct urb *urb;
struct req_list_entry *entry, *next;
void *buf;
unsigned int response_length = 0;
struct cokey_in_urb_context *ctx = NULL;
pr_debug("%s enter\n", __func__);
if (!dev->current_out_urb) {
return 0;
}
if (dev->urb_is_cmd_container || dev->urb_is_fast_mode) {
// prepare and send enclosing command
if (dev->urb_is_fast_mode) {
cmd.code = COKEY_CMD_ECB_ENCRYPT;
response_length = dev->current_out_urb->transfer_buffer_length;
} else if (dev->urb_is_cmd_container) {
#ifdef cokey_RESPONSE_CONTAINER
cmd.code = COKEY_CMD_CONTAINER_RESP_CONTAINER;
#else
cmd.code = COKEY_CMD_CONTAINER;
#endif
}
cmd.length = dev->current_out_urb->transfer_buffer_length;
cmd.tag = 0; //get_random_int(); // TODO USB Protocol Extension
if (cokey_send_command_urb(dev, &cmd)) {
return -1;
}
}
/* send dev->current_out_urb */
urb = dev->current_out_urb;
usb_anchor_urb(urb, &dev->submitted);
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
dev_err(&dev->interface->dev,
"%s - failed submitting write urb, error %d\n",
__func__, retval);
return retval;
}
dev->current_out_urb = NULL;
/************/
/* RESPONSE */
/* No response requested? */
if (!dev->current_req_list) {
pr_debug("%s: no response requested\n", __func__);
return 0;
}
// TODO refactor this... much code duplication
if (dev->urb_is_fast_mode) {
buf = kmalloc(response_length, GFP_KERNEL);
if (!buf) {
return -ENOMEM;
}
ctx = kmalloc(sizeof(struct cokey_in_urb_context),
GFP_KERNEL);
if (!ctx) {
return -ENOMEM;
}
ctx->dev = dev;
ctx->req = NULL;
ctx->req_list = dev->current_req_list;
ctx->fast_mode = true;
urb = usb_alloc_urb(0, GFP_KERNEL);
usb_fill_bulk_urb(urb, dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
buf, response_length,
cokey_urb_complete_in_cb, ctx);
urb->transfer_flags |= URB_FREE_BUFFER;
usb_anchor_urb(urb, &dev->submitted);
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
goto error_submit;
}
pr_debug("%s: fast mode, response with length %d requested\n", __func__, response_length);
dev->current_req_list = NULL;
return 0;
}
#ifdef COKEY_RESPONSE_CONTAINER
list_for_each_entry_safe(entry, next, dev->current_req_list, list) {
response_length += entry->response_length;
if (entry->response_length == 0) {
/* remove requests without response from list */
list_del(&entry->list);
kfree(entry);
}
}
if (response_length == 0) {
/* does not make any sense, so it should not happen */
pr_debug("Request list with zero response length...\n");
return 0;
}
buf = kmalloc(response_length, GFP_KERNEL);
if (!buf) {
return -ENOMEM;
}
ctx = kmalloc(sizeof(struct cokey_in_urb_context), GFP_KERNEL);
if (!ctx) {
return -ENOMEM;
}
ctx->dev = dev;
ctx->req = NULL;
ctx->req_list = dev->current_req_list;
ctx->fast_mode = false;
urb = usb_alloc_urb(0, GFP_KERNEL);
usb_fill_bulk_urb(urb, dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
buf, response_length,
cokey_urb_complete_in_cb, ctx);
urb->transfer_flags |= URB_FREE_BUFFER;
usb_anchor_urb(urb, &dev->submitted);
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
goto error_submit;
}
#else
/* single urbs according to current_req_list */
list_for_each_entry_safe(entry, next, dev->current_req_list, list) {
buf = kmalloc(entry->response_length, GFP_KERNEL);
if (!buf) {
return -ENOMEM;
}
ctx = kmalloc(sizeof(struct cokey_in_urb_context), GFP_KERNEL);
if (!ctx) {
return -ENOMEM;
}
ctx->dev = dev;
ctx->req = entry->req;
ctx->req_list = NULL;
ctx->fast_mode = false;
urb = usb_alloc_urb(0, GFP_KERNEL);
usb_fill_bulk_urb(urb, dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
buf, entry->response_length,
cokey_urb_complete_in_cb, ctx);
urb->transfer_flags |= URB_FREE_BUFFER;
usb_anchor_urb(urb, &dev->submitted);
retval = usb_submit_urb(urb, GFP_KERNEL);
if (retval) {
goto error_submit;
}
/* remove the entry when submitted */
list_del(&entry->list);
kfree(entry);
}
kfree(dev->current_req_list);
#endif
dev->current_req_list = NULL;
return 0;
error_submit:
dev_err(&dev->interface->dev,
"%s - failed submitting urb, error %d\n",
__func__, retval);
if (urb) {
usb_unanchor_urb(urb);
usb_free_urb(urb);
}
if (ctx) {
kfree(ctx);
}
return retval;
}
static int cokey_cmd_try_finish(struct cokey_dev *dev, int
additional_length)
{
int urb_max_length, next_required;
pr_debug("%s enter\n", __func__);
/* Finish up last command if there is one and it has not enough space to
* take the new command or is not a container command */
if (dev->current_out_urb) {
if (dev->urb_is_cmd_container) {
urb_max_length = cokey_usb_packet_length;
next_required = COKEY_COMMAND_LENGTH;
} else if (dev->urb_is_fast_mode) {
urb_max_length = cokey_usb_packet_length_fast;
next_required = AES_BLOCK_SIZE;
}
if ((!dev->urb_is_cmd_container && !dev->urb_is_fast_mode)
|| (dev->current_out_urb->transfer_buffer_length + next_required + additional_length > urb_max_length))
return cokey_cmd_finish(dev);
}
return 0;
}
static void *cokey_cmd_get_urb_buf(struct cokey_dev *dev, struct cokey_command *cmd, bool fast_mode)
{
void *ret_buf = NULL;
void *buf = NULL;
struct urb *urb = NULL;
int urb_length;
if (fast_mode) {
if (cokey_cmd_try_finish(dev, 0) < 0)
goto error;
} else {
if (cokey_cmd_try_finish(dev, cmd->length) < 0)
goto error;
}
// if switch between fast_mode and non-fast-mode => send
if (dev->current_out_urb)
if (dev->urb_is_fast_mode != fast_mode)
if (cokey_cmd_finish(dev) < 0)
goto error;
/* If the last command was finished, current_out_urb should be NULL and
* we have to allocate a new URB */
if (!dev->current_out_urb) {
if (fast_mode) {
buf = kmalloc(cokey_usb_packet_length_fast,
GFP_KERNEL);
ret_buf = buf;
urb_length = cmd->length;
dev->urb_is_fast_mode = true;
dev->urb_is_cmd_container = false;
} else if (cmd->length + COKEY_COMMAND_LENGTH >=
cokey_usb_packet_length) {
/* send command urb */
if (cokey_send_command_urb(dev, cmd)) {
goto error;
}
/* allocate a single command data urb */
buf = kmalloc(cmd->length, GFP_KERNEL);
ret_buf = buf;
urb_length = cmd->length;
dev->urb_is_cmd_container = false;
dev->urb_is_fast_mode = false;
} else {
/* allocate a MIN_USB_PACKET_LENGTH urb */
buf = kmalloc(cokey_usb_packet_length, GFP_KERNEL);
/* write command into buffer */
cokey_fill_buf_from_cmd(buf, cmd);
ret_buf = buf + COKEY_COMMAND_LENGTH;
urb_length = COKEY_COMMAND_LENGTH + cmd->length;
dev->urb_is_cmd_container = true;
dev->urb_is_fast_mode = false;
}
if (!buf) {
goto error;
}
urb = usb_alloc_urb(0, GFP_KERNEL);
if (!urb) {
goto error;
}
usb_fill_bulk_urb(urb, dev->udev,
usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
buf, urb_length,
cokey_urb_complete_out_cb, dev);
urb->transfer_flags |= URB_FREE_BUFFER;
dev->current_out_urb = urb;
} else {
// Append to current command
buf = ret_buf = dev->current_out_urb->transfer_buffer +
dev->current_out_urb->transfer_buffer_length;
dev->current_out_urb->transfer_buffer_length += cmd->length;
if (!fast_mode) {
cokey_fill_buf_from_cmd(buf, cmd);
ret_buf = buf + COKEY_COMMAND_LENGTH;
dev->current_out_urb->transfer_buffer_length += COKEY_COMMAND_LENGTH;
}
}
return ret_buf;
error:
if (buf)
kfree(buf);
if (urb)
usb_free_urb(urb);
return NULL;
}
static int cokey_cmd_add_request(struct cokey_dev *dev, struct ablkcipher_request *req,
unsigned int response_length, int cmd_code)
{
struct req_list_entry *new_entry;
pr_debug("%s entered\n", __func__);
/* check if there are any responses already requested, and if not start a new list */
if (!dev->current_req_list) {
dev->current_req_list = kmalloc(sizeof(struct list_head),
GFP_KERNEL);
if (!dev->current_req_list) {
pr_err("Could not allocate request list head\n");
return -ENOMEM;
}
INIT_LIST_HEAD(dev->current_req_list);
}
new_entry = kmalloc(sizeof(struct req_list_entry), GFP_KERNEL);
if (!new_entry) {
pr_err("Could not allocate request list entry\n");
return -ENOMEM;
}
new_entry->req = req;
new_entry->response_length = response_length;
new_entry->cmd_code = cmd_code;
/* insert the req into the current list of reqs to be sent */
list_add_tail(&new_entry->list, dev->current_req_list);
return 0;
}
static int cokey_crypt(struct ablkcipher_request *req)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct cokey_tfm_ctx *ctx = crypto_ablkcipher_ctx(tfm);
struct cokey_dev *dev = cokey_get_dev_from_tfm(tfm);
struct cokey_reqctx *reqctx = ablkcipher_request_ctx(req);
struct cokey_command cmd;
int retval;
void *buf;
struct list_head *req_list;
pr_debug("%s enter\n", __func__);
/* find out if tfm of req is currently active */
if (!dev->active_tfm_ctx || dev->active_tfm_ctx != ctx) {
/* req tfm is not active => activate it by sending an setkey URB
* if not, activate it by sending a setkey URB before the data and
* set it active in cokey_device */
cmd.code = COKEY_CMD_SETKEY;
cmd.length = ctx->keylen;
cmd.tag = 0; //get_random_int(); // TODO USB Protocol Extension
buf = cokey_cmd_get_urb_buf(dev, &cmd, false);
if (!buf) {
retval = -1;
goto error_cleanup_req;
}
memcpy(buf, ctx->aes_key, ctx->keylen);
if (ctx->fast_mode) {
retval = cokey_cmd_finish(dev);
if (retval < 0) {
goto error_cleanup_req;
}
}
dev->active_tfm_ctx = ctx;
// TODO USB Protocol Extension: add IN-URB for status response
}
if (ctx->fast_mode) {
/* in fast_mode only the length of cmd has to be set to the
* length of the IV */
cmd.length = AES_BLOCK_SIZE;
} else {
/* send the actual crypto command + data */
cmd.code = reqctx->cmd_code;
/* length is IV size + actual data */
cmd.length = AES_BLOCK_SIZE + req->nbytes;
cmd.tag = 0; //get_random_int(); // TODO USB Protocol Extension
pr_debug("%s preparing to send %d bytes\n", __func__, req->nbytes);
}
buf = cokey_cmd_get_urb_buf(dev, &cmd, ctx->fast_mode);
if (!buf) {
retval = -1;
goto error_cleanup_req;
}
/* add request to list with expected response length */
retval = cokey_cmd_add_request(dev, req, cmd.length, reqctx->cmd_code);
if (retval < 0) {
goto error_cleanup_req;
}
/* copy the IV to the beginning of the buffer */
memcpy(buf, req->info, AES_BLOCK_SIZE);
if (!ctx->fast_mode) {
/* copy actual data to the urb buffer */
retval = sg_copy_to_buffer(req->src, sg_nents(req->src), buf+AES_BLOCK_SIZE, req->nbytes);
if (retval != req->nbytes) {
pr_err("Could only copy %d/%d bytes from source scatterlist\n", retval, req->nbytes);
retval = -1;
goto error;
}
}
return 0;
error_cleanup_req:
req->base.complete(&req->base, retval);
error:
/* try to cleanup all the commands/requests that are already in the current out urb */
if (dev->current_out_urb) {
usb_unanchor_urb(dev->current_out_urb);
usb_free_urb(dev->current_out_urb);
dev->current_out_urb = NULL;
}
req_list = dev->current_req_list;
dev->current_req_list = NULL;
cokey_cleanup_req_list(req_list, retval);
return retval;
}
static enum hrtimer_restart cokey_timer_cb(struct hrtimer *t)
{
struct cokey_dev *dev = container_of(t, struct cokey_dev, timer);
pr_debug("%s enter\n", __func__);
tasklet_schedule(&dev->tasklet);
return HRTIMER_NORESTART;
}
#define COKEY_USB_PACKET_TIMEOUT_MS 1
#define COKEY_USB_PACKET_TIMEOUT_NS 1E4L
static void cokey_tasklet_cb(unsigned long data)
{
struct cokey_dev *dev = (struct cokey_dev *)data;
struct crypto_async_request *async_req, *backlog;
unsigned long flags;