s390/zcrypt: move cca misc functions to new code file
[linux-2.6-microblaze.git] / drivers / s390 / crypto / pkey_api.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  pkey device driver
4  *
5  *  Copyright IBM Corp. 2017
6  *  Author(s): Harald Freudenberger
7  */
8
9 #define KMSG_COMPONENT "pkey"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/fs.h>
13 #include <linux/init.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/kallsyms.h>
18 #include <linux/debugfs.h>
19 #include <linux/random.h>
20 #include <linux/cpufeature.h>
21 #include <asm/zcrypt.h>
22 #include <asm/cpacf.h>
23 #include <asm/pkey.h>
24 #include <crypto/aes.h>
25
26 #include "zcrypt_api.h"
27 #include "zcrypt_ccamisc.h"
28
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("IBM Corporation");
31 MODULE_DESCRIPTION("s390 protected key interface");
32
33 /* mask of available pckmo subfunctions, fetched once at module init */
34 static cpacf_mask_t pckmo_functions;
35
36 /*
37  * debug feature data and functions
38  */
39
40 static debug_info_t *debug_info;
41
42 #define DEBUG_DBG(...)  debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
43 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
44 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
45 #define DEBUG_ERR(...)  debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
46
47 static void __init pkey_debug_init(void)
48 {
49         /* 5 arguments per dbf entry (including the format string ptr) */
50         debug_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
51         debug_register_view(debug_info, &debug_sprintf_view);
52         debug_set_level(debug_info, 3);
53 }
54
55 static void __exit pkey_debug_exit(void)
56 {
57         debug_unregister(debug_info);
58 }
59
60 /* inside view of a protected key token (only type 0x00 version 0x01) */
61 struct protaeskeytoken {
62         u8  type;     /* 0x00 for PAES specific key tokens */
63         u8  res0[3];
64         u8  version;  /* should be 0x01 for protected AES key token */
65         u8  res1[3];
66         u32 keytype;  /* key type, one of the PKEY_KEYTYPE values */
67         u32 len;      /* bytes actually stored in protkey[] */
68         u8  protkey[MAXPROTKEYSIZE]; /* the protected key blob */
69 } __packed;
70
71 /*
72  * Create a protected key from a clear key value.
73  */
74 int pkey_clr2protkey(u32 keytype,
75                      const struct pkey_clrkey *clrkey,
76                      struct pkey_protkey *protkey)
77 {
78         long fc;
79         int keysize;
80         u8 paramblock[64];
81
82         switch (keytype) {
83         case PKEY_KEYTYPE_AES_128:
84                 keysize = 16;
85                 fc = CPACF_PCKMO_ENC_AES_128_KEY;
86                 break;
87         case PKEY_KEYTYPE_AES_192:
88                 keysize = 24;
89                 fc = CPACF_PCKMO_ENC_AES_192_KEY;
90                 break;
91         case PKEY_KEYTYPE_AES_256:
92                 keysize = 32;
93                 fc = CPACF_PCKMO_ENC_AES_256_KEY;
94                 break;
95         default:
96                 DEBUG_ERR("%s unknown/unsupported keytype %d\n",
97                           __func__, keytype);
98                 return -EINVAL;
99         }
100
101         /*
102          * Check if the needed pckmo subfunction is available.
103          * These subfunctions can be enabled/disabled by customers
104          * in the LPAR profile or may even change on the fly.
105          */
106         if (!cpacf_test_func(&pckmo_functions, fc)) {
107                 DEBUG_ERR("%s pckmo functions not available\n", __func__);
108                 return -ENODEV;
109         }
110
111         /* prepare param block */
112         memset(paramblock, 0, sizeof(paramblock));
113         memcpy(paramblock, clrkey->clrkey, keysize);
114
115         /* call the pckmo instruction */
116         cpacf_pckmo(fc, paramblock);
117
118         /* copy created protected key */
119         protkey->type = keytype;
120         protkey->len = keysize + 32;
121         memcpy(protkey->protkey, paramblock, keysize + 32);
122
123         return 0;
124 }
125 EXPORT_SYMBOL(pkey_clr2protkey);
126
127 /*
128  * Find card and transform secure key into protected key.
129  */
130 int pkey_skey2pkey(const struct pkey_seckey *seckey,
131                    struct pkey_protkey *pkey)
132 {
133         u16 cardnr, domain;
134         int rc, verify;
135
136         /*
137          * The cca_sec2protkey call may fail when a card has been
138          * addressed where the master key was changed after last fetch
139          * of the mkvp into the cache. Try 3 times: First witout verify
140          * then with verify and last round with verify and old master
141          * key verification pattern match not ignored.
142          */
143         for (verify = 0; verify < 3; verify++) {
144                 rc = cca_findcard(seckey->seckey, &cardnr, &domain, verify);
145                 if (rc < 0)
146                         continue;
147                 if (rc > 0 && verify < 2)
148                         continue;
149                 rc = cca_sec2protkey(cardnr, domain, seckey->seckey,
150                                      pkey->protkey, &pkey->len, &pkey->type);
151                 if (rc == 0)
152                         break;
153         }
154
155         if (rc)
156                 DEBUG_DBG("%s failed rc=%d\n", __func__, rc);
157
158         return rc;
159 }
160 EXPORT_SYMBOL(pkey_skey2pkey);
161
162 /*
163  * Verify key and give back some info about the key.
164  */
165 int pkey_verifykey(const struct pkey_seckey *seckey,
166                    u16 *pcardnr, u16 *pdomain,
167                    u16 *pkeysize, u32 *pattributes)
168 {
169         struct secaeskeytoken *t = (struct secaeskeytoken *) seckey;
170         u16 cardnr, domain;
171         int rc;
172
173         /* check the secure key for valid AES secure key */
174         rc = cca_check_secaeskeytoken(debug_info, 3, (u8 *) seckey, 0);
175         if (rc)
176                 goto out;
177         if (pattributes)
178                 *pattributes = PKEY_VERIFY_ATTR_AES;
179         if (pkeysize)
180                 *pkeysize = t->bitsize;
181
182         /* try to find a card which can handle this key */
183         rc = cca_findcard(seckey->seckey, &cardnr, &domain, 1);
184         if (rc < 0)
185                 goto out;
186
187         if (rc > 0) {
188                 /* key mkvp matches to old master key mkvp */
189                 DEBUG_DBG("%s secure key has old mkvp\n", __func__);
190                 if (pattributes)
191                         *pattributes |= PKEY_VERIFY_ATTR_OLD_MKVP;
192                 rc = 0;
193         }
194
195         if (pcardnr)
196                 *pcardnr = cardnr;
197         if (pdomain)
198                 *pdomain = domain;
199
200 out:
201         DEBUG_DBG("%s rc=%d\n", __func__, rc);
202         return rc;
203 }
204 EXPORT_SYMBOL(pkey_verifykey);
205
206 /*
207  * Generate a random protected key
208  */
209 int pkey_genprotkey(__u32 keytype, struct pkey_protkey *protkey)
210 {
211         struct pkey_clrkey clrkey;
212         int keysize;
213         int rc;
214
215         switch (keytype) {
216         case PKEY_KEYTYPE_AES_128:
217                 keysize = 16;
218                 break;
219         case PKEY_KEYTYPE_AES_192:
220                 keysize = 24;
221                 break;
222         case PKEY_KEYTYPE_AES_256:
223                 keysize = 32;
224                 break;
225         default:
226                 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
227                           keytype);
228                 return -EINVAL;
229         }
230
231         /* generate a dummy random clear key */
232         get_random_bytes(clrkey.clrkey, keysize);
233
234         /* convert it to a dummy protected key */
235         rc = pkey_clr2protkey(keytype, &clrkey, protkey);
236         if (rc)
237                 return rc;
238
239         /* replace the key part of the protected key with random bytes */
240         get_random_bytes(protkey->protkey, keysize);
241
242         return 0;
243 }
244 EXPORT_SYMBOL(pkey_genprotkey);
245
246 /*
247  * Verify if a protected key is still valid
248  */
249 int pkey_verifyprotkey(const struct pkey_protkey *protkey)
250 {
251         unsigned long fc;
252         struct {
253                 u8 iv[AES_BLOCK_SIZE];
254                 u8 key[MAXPROTKEYSIZE];
255         } param;
256         u8 null_msg[AES_BLOCK_SIZE];
257         u8 dest_buf[AES_BLOCK_SIZE];
258         unsigned int k;
259
260         switch (protkey->type) {
261         case PKEY_KEYTYPE_AES_128:
262                 fc = CPACF_KMC_PAES_128;
263                 break;
264         case PKEY_KEYTYPE_AES_192:
265                 fc = CPACF_KMC_PAES_192;
266                 break;
267         case PKEY_KEYTYPE_AES_256:
268                 fc = CPACF_KMC_PAES_256;
269                 break;
270         default:
271                 DEBUG_ERR("%s unknown/unsupported keytype %d\n", __func__,
272                           protkey->type);
273                 return -EINVAL;
274         }
275
276         memset(null_msg, 0, sizeof(null_msg));
277
278         memset(param.iv, 0, sizeof(param.iv));
279         memcpy(param.key, protkey->protkey, sizeof(param.key));
280
281         k = cpacf_kmc(fc | CPACF_ENCRYPT, &param, null_msg, dest_buf,
282                       sizeof(null_msg));
283         if (k != sizeof(null_msg)) {
284                 DEBUG_ERR("%s protected key is not valid\n", __func__);
285                 return -EKEYREJECTED;
286         }
287
288         return 0;
289 }
290 EXPORT_SYMBOL(pkey_verifyprotkey);
291
292 /*
293  * Transform a non-CCA key token into a protected key
294  */
295 static int pkey_nonccatok2pkey(const __u8 *key, __u32 keylen,
296                                struct pkey_protkey *protkey)
297 {
298         struct keytoken_header *hdr = (struct keytoken_header *)key;
299         struct protaeskeytoken *t;
300
301         switch (hdr->version) {
302         case TOKVER_PROTECTED_KEY:
303                 if (keylen != sizeof(struct protaeskeytoken))
304                         return -EINVAL;
305
306                 t = (struct protaeskeytoken *)key;
307                 protkey->len = t->len;
308                 protkey->type = t->keytype;
309                 memcpy(protkey->protkey, t->protkey,
310                        sizeof(protkey->protkey));
311
312                 return pkey_verifyprotkey(protkey);
313         default:
314                 DEBUG_ERR("%s unknown/unsupported non-CCA token version %d\n",
315                           __func__, hdr->version);
316                 return -EINVAL;
317         }
318 }
319
320 /*
321  * Transform a CCA internal key token into a protected key
322  */
323 static int pkey_ccainttok2pkey(const __u8 *key, __u32 keylen,
324                                struct pkey_protkey *protkey)
325 {
326         struct keytoken_header *hdr = (struct keytoken_header *)key;
327
328         switch (hdr->version) {
329         case TOKVER_CCA_AES:
330                 if (keylen != sizeof(struct secaeskeytoken))
331                         return -EINVAL;
332
333                 return pkey_skey2pkey((struct pkey_seckey *)key,
334                                       protkey);
335         default:
336                 DEBUG_ERR("%s unknown/unsupported CCA internal token version %d\n",
337                           __func__, hdr->version);
338                 return -EINVAL;
339         }
340 }
341
342 /*
343  * Transform a key blob (of any type) into a protected key
344  */
345 int pkey_keyblob2pkey(const __u8 *key, __u32 keylen,
346                       struct pkey_protkey *protkey)
347 {
348         struct keytoken_header *hdr = (struct keytoken_header *)key;
349
350         if (keylen < sizeof(struct keytoken_header))
351                 return -EINVAL;
352
353         switch (hdr->type) {
354         case TOKTYPE_NON_CCA:
355                 return pkey_nonccatok2pkey(key, keylen, protkey);
356         case TOKTYPE_CCA_INTERNAL:
357                 return pkey_ccainttok2pkey(key, keylen, protkey);
358         default:
359                 DEBUG_ERR("%s unknown/unsupported blob type %d\n", __func__,
360                           hdr->type);
361                 return -EINVAL;
362         }
363 }
364 EXPORT_SYMBOL(pkey_keyblob2pkey);
365
366 /*
367  * File io functions
368  */
369
370 static long pkey_unlocked_ioctl(struct file *filp, unsigned int cmd,
371                                 unsigned long arg)
372 {
373         int rc;
374
375         switch (cmd) {
376         case PKEY_GENSECK: {
377                 struct pkey_genseck __user *ugs = (void __user *) arg;
378                 struct pkey_genseck kgs;
379
380                 if (copy_from_user(&kgs, ugs, sizeof(kgs)))
381                         return -EFAULT;
382                 rc = cca_genseckey(kgs.cardnr, kgs.domain,
383                                    kgs.keytype, kgs.seckey.seckey);
384                 DEBUG_DBG("%s cca_genseckey()=%d\n", __func__, rc);
385                 if (rc)
386                         break;
387                 if (copy_to_user(ugs, &kgs, sizeof(kgs)))
388                         return -EFAULT;
389                 break;
390         }
391         case PKEY_CLR2SECK: {
392                 struct pkey_clr2seck __user *ucs = (void __user *) arg;
393                 struct pkey_clr2seck kcs;
394
395                 if (copy_from_user(&kcs, ucs, sizeof(kcs)))
396                         return -EFAULT;
397                 rc = cca_clr2seckey(kcs.cardnr, kcs.domain, kcs.keytype,
398                                     kcs.clrkey.clrkey, kcs.seckey.seckey);
399                 DEBUG_DBG("%s cca_clr2seckey()=%d\n", __func__, rc);
400                 if (rc)
401                         break;
402                 if (copy_to_user(ucs, &kcs, sizeof(kcs)))
403                         return -EFAULT;
404                 memzero_explicit(&kcs, sizeof(kcs));
405                 break;
406         }
407         case PKEY_SEC2PROTK: {
408                 struct pkey_sec2protk __user *usp = (void __user *) arg;
409                 struct pkey_sec2protk ksp;
410
411                 if (copy_from_user(&ksp, usp, sizeof(ksp)))
412                         return -EFAULT;
413                 rc = cca_sec2protkey(ksp.cardnr, ksp.domain,
414                                      ksp.seckey.seckey, ksp.protkey.protkey,
415                                      NULL, &ksp.protkey.type);
416                 DEBUG_DBG("%s cca_sec2protkey()=%d\n", __func__, rc);
417                 if (rc)
418                         break;
419                 if (copy_to_user(usp, &ksp, sizeof(ksp)))
420                         return -EFAULT;
421                 break;
422         }
423         case PKEY_CLR2PROTK: {
424                 struct pkey_clr2protk __user *ucp = (void __user *) arg;
425                 struct pkey_clr2protk kcp;
426
427                 if (copy_from_user(&kcp, ucp, sizeof(kcp)))
428                         return -EFAULT;
429                 rc = pkey_clr2protkey(kcp.keytype,
430                                       &kcp.clrkey, &kcp.protkey);
431                 DEBUG_DBG("%s pkey_clr2protkey()=%d\n", __func__, rc);
432                 if (rc)
433                         break;
434                 if (copy_to_user(ucp, &kcp, sizeof(kcp)))
435                         return -EFAULT;
436                 memzero_explicit(&kcp, sizeof(kcp));
437                 break;
438         }
439         case PKEY_FINDCARD: {
440                 struct pkey_findcard __user *ufc = (void __user *) arg;
441                 struct pkey_findcard kfc;
442
443                 if (copy_from_user(&kfc, ufc, sizeof(kfc)))
444                         return -EFAULT;
445                 rc = cca_findcard(kfc.seckey.seckey,
446                                   &kfc.cardnr, &kfc.domain, 1);
447                 DEBUG_DBG("%s cca_findcard()=%d\n", __func__, rc);
448                 if (rc < 0)
449                         break;
450                 if (copy_to_user(ufc, &kfc, sizeof(kfc)))
451                         return -EFAULT;
452                 break;
453         }
454         case PKEY_SKEY2PKEY: {
455                 struct pkey_skey2pkey __user *usp = (void __user *) arg;
456                 struct pkey_skey2pkey ksp;
457
458                 if (copy_from_user(&ksp, usp, sizeof(ksp)))
459                         return -EFAULT;
460                 rc = pkey_skey2pkey(&ksp.seckey, &ksp.protkey);
461                 DEBUG_DBG("%s pkey_skey2pkey()=%d\n", __func__, rc);
462                 if (rc)
463                         break;
464                 if (copy_to_user(usp, &ksp, sizeof(ksp)))
465                         return -EFAULT;
466                 break;
467         }
468         case PKEY_VERIFYKEY: {
469                 struct pkey_verifykey __user *uvk = (void __user *) arg;
470                 struct pkey_verifykey kvk;
471
472                 if (copy_from_user(&kvk, uvk, sizeof(kvk)))
473                         return -EFAULT;
474                 rc = pkey_verifykey(&kvk.seckey, &kvk.cardnr, &kvk.domain,
475                                     &kvk.keysize, &kvk.attributes);
476                 DEBUG_DBG("%s pkey_verifykey()=%d\n", __func__, rc);
477                 if (rc)
478                         break;
479                 if (copy_to_user(uvk, &kvk, sizeof(kvk)))
480                         return -EFAULT;
481                 break;
482         }
483         case PKEY_GENPROTK: {
484                 struct pkey_genprotk __user *ugp = (void __user *) arg;
485                 struct pkey_genprotk kgp;
486
487                 if (copy_from_user(&kgp, ugp, sizeof(kgp)))
488                         return -EFAULT;
489                 rc = pkey_genprotkey(kgp.keytype, &kgp.protkey);
490                 DEBUG_DBG("%s pkey_genprotkey()=%d\n", __func__, rc);
491                 if (rc)
492                         break;
493                 if (copy_to_user(ugp, &kgp, sizeof(kgp)))
494                         return -EFAULT;
495                 break;
496         }
497         case PKEY_VERIFYPROTK: {
498                 struct pkey_verifyprotk __user *uvp = (void __user *) arg;
499                 struct pkey_verifyprotk kvp;
500
501                 if (copy_from_user(&kvp, uvp, sizeof(kvp)))
502                         return -EFAULT;
503                 rc = pkey_verifyprotkey(&kvp.protkey);
504                 DEBUG_DBG("%s pkey_verifyprotkey()=%d\n", __func__, rc);
505                 break;
506         }
507         case PKEY_KBLOB2PROTK: {
508                 struct pkey_kblob2pkey __user *utp = (void __user *) arg;
509                 struct pkey_kblob2pkey ktp;
510                 __u8 __user *ukey;
511                 __u8 *kkey;
512
513                 if (copy_from_user(&ktp, utp, sizeof(ktp)))
514                         return -EFAULT;
515                 if (ktp.keylen < MINKEYBLOBSIZE ||
516                     ktp.keylen > MAXKEYBLOBSIZE)
517                         return -EINVAL;
518                 ukey = ktp.key;
519                 kkey = kmalloc(ktp.keylen, GFP_KERNEL);
520                 if (kkey == NULL)
521                         return -ENOMEM;
522                 if (copy_from_user(kkey, ukey, ktp.keylen)) {
523                         kfree(kkey);
524                         return -EFAULT;
525                 }
526                 rc = pkey_keyblob2pkey(kkey, ktp.keylen, &ktp.protkey);
527                 DEBUG_DBG("%s pkey_keyblob2pkey()=%d\n", __func__, rc);
528                 kfree(kkey);
529                 if (rc)
530                         break;
531                 if (copy_to_user(utp, &ktp, sizeof(ktp)))
532                         return -EFAULT;
533                 break;
534         }
535         default:
536                 /* unknown/unsupported ioctl cmd */
537                 return -ENOTTY;
538         }
539
540         return rc;
541 }
542
543 /*
544  * Sysfs and file io operations
545  */
546
547 /*
548  * Sysfs attribute read function for all protected key binary attributes.
549  * The implementation can not deal with partial reads, because a new random
550  * protected key blob is generated with each read. In case of partial reads
551  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
552  */
553 static ssize_t pkey_protkey_aes_attr_read(u32 keytype, bool is_xts, char *buf,
554                                           loff_t off, size_t count)
555 {
556         struct protaeskeytoken protkeytoken;
557         struct pkey_protkey protkey;
558         int rc;
559
560         if (off != 0 || count < sizeof(protkeytoken))
561                 return -EINVAL;
562         if (is_xts)
563                 if (count < 2 * sizeof(protkeytoken))
564                         return -EINVAL;
565
566         memset(&protkeytoken, 0, sizeof(protkeytoken));
567         protkeytoken.type = TOKTYPE_NON_CCA;
568         protkeytoken.version = TOKVER_PROTECTED_KEY;
569         protkeytoken.keytype = keytype;
570
571         rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
572         if (rc)
573                 return rc;
574
575         protkeytoken.len = protkey.len;
576         memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
577
578         memcpy(buf, &protkeytoken, sizeof(protkeytoken));
579
580         if (is_xts) {
581                 rc = pkey_genprotkey(protkeytoken.keytype, &protkey);
582                 if (rc)
583                         return rc;
584
585                 protkeytoken.len = protkey.len;
586                 memcpy(&protkeytoken.protkey, &protkey.protkey, protkey.len);
587
588                 memcpy(buf + sizeof(protkeytoken), &protkeytoken,
589                        sizeof(protkeytoken));
590
591                 return 2 * sizeof(protkeytoken);
592         }
593
594         return sizeof(protkeytoken);
595 }
596
597 static ssize_t protkey_aes_128_read(struct file *filp,
598                                     struct kobject *kobj,
599                                     struct bin_attribute *attr,
600                                     char *buf, loff_t off,
601                                     size_t count)
602 {
603         return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
604                                           off, count);
605 }
606
607 static ssize_t protkey_aes_192_read(struct file *filp,
608                                     struct kobject *kobj,
609                                     struct bin_attribute *attr,
610                                     char *buf, loff_t off,
611                                     size_t count)
612 {
613         return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
614                                           off, count);
615 }
616
617 static ssize_t protkey_aes_256_read(struct file *filp,
618                                     struct kobject *kobj,
619                                     struct bin_attribute *attr,
620                                     char *buf, loff_t off,
621                                     size_t count)
622 {
623         return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
624                                           off, count);
625 }
626
627 static ssize_t protkey_aes_128_xts_read(struct file *filp,
628                                         struct kobject *kobj,
629                                         struct bin_attribute *attr,
630                                         char *buf, loff_t off,
631                                         size_t count)
632 {
633         return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
634                                           off, count);
635 }
636
637 static ssize_t protkey_aes_256_xts_read(struct file *filp,
638                                         struct kobject *kobj,
639                                         struct bin_attribute *attr,
640                                         char *buf, loff_t off,
641                                         size_t count)
642 {
643         return pkey_protkey_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
644                                           off, count);
645 }
646
647 static BIN_ATTR_RO(protkey_aes_128, sizeof(struct protaeskeytoken));
648 static BIN_ATTR_RO(protkey_aes_192, sizeof(struct protaeskeytoken));
649 static BIN_ATTR_RO(protkey_aes_256, sizeof(struct protaeskeytoken));
650 static BIN_ATTR_RO(protkey_aes_128_xts, 2 * sizeof(struct protaeskeytoken));
651 static BIN_ATTR_RO(protkey_aes_256_xts, 2 * sizeof(struct protaeskeytoken));
652
653 static struct bin_attribute *protkey_attrs[] = {
654         &bin_attr_protkey_aes_128,
655         &bin_attr_protkey_aes_192,
656         &bin_attr_protkey_aes_256,
657         &bin_attr_protkey_aes_128_xts,
658         &bin_attr_protkey_aes_256_xts,
659         NULL
660 };
661
662 static struct attribute_group protkey_attr_group = {
663         .name      = "protkey",
664         .bin_attrs = protkey_attrs,
665 };
666
667 /*
668  * Sysfs attribute read function for all secure key ccadata binary attributes.
669  * The implementation can not deal with partial reads, because a new random
670  * protected key blob is generated with each read. In case of partial reads
671  * (i.e. off != 0 or count < key blob size) -EINVAL is returned.
672  */
673 static ssize_t pkey_ccadata_aes_attr_read(u32 keytype, bool is_xts, char *buf,
674                                           loff_t off, size_t count)
675 {
676         int rc;
677         struct pkey_seckey *seckey = (struct pkey_seckey *) buf;
678
679         if (off != 0 || count < sizeof(struct secaeskeytoken))
680                 return -EINVAL;
681         if (is_xts)
682                 if (count < 2 * sizeof(struct secaeskeytoken))
683                         return -EINVAL;
684
685         rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
686         if (rc)
687                 return rc;
688
689         if (is_xts) {
690                 seckey++;
691                 rc = cca_genseckey(-1, -1, keytype, seckey->seckey);
692                 if (rc)
693                         return rc;
694
695                 return 2 * sizeof(struct secaeskeytoken);
696         }
697
698         return sizeof(struct secaeskeytoken);
699 }
700
701 static ssize_t ccadata_aes_128_read(struct file *filp,
702                                     struct kobject *kobj,
703                                     struct bin_attribute *attr,
704                                     char *buf, loff_t off,
705                                     size_t count)
706 {
707         return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, false, buf,
708                                           off, count);
709 }
710
711 static ssize_t ccadata_aes_192_read(struct file *filp,
712                                     struct kobject *kobj,
713                                     struct bin_attribute *attr,
714                                     char *buf, loff_t off,
715                                     size_t count)
716 {
717         return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_192, false, buf,
718                                           off, count);
719 }
720
721 static ssize_t ccadata_aes_256_read(struct file *filp,
722                                     struct kobject *kobj,
723                                     struct bin_attribute *attr,
724                                     char *buf, loff_t off,
725                                     size_t count)
726 {
727         return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, false, buf,
728                                           off, count);
729 }
730
731 static ssize_t ccadata_aes_128_xts_read(struct file *filp,
732                                         struct kobject *kobj,
733                                         struct bin_attribute *attr,
734                                         char *buf, loff_t off,
735                                         size_t count)
736 {
737         return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_128, true, buf,
738                                           off, count);
739 }
740
741 static ssize_t ccadata_aes_256_xts_read(struct file *filp,
742                                         struct kobject *kobj,
743                                         struct bin_attribute *attr,
744                                         char *buf, loff_t off,
745                                         size_t count)
746 {
747         return pkey_ccadata_aes_attr_read(PKEY_KEYTYPE_AES_256, true, buf,
748                                           off, count);
749 }
750
751 static BIN_ATTR_RO(ccadata_aes_128, sizeof(struct secaeskeytoken));
752 static BIN_ATTR_RO(ccadata_aes_192, sizeof(struct secaeskeytoken));
753 static BIN_ATTR_RO(ccadata_aes_256, sizeof(struct secaeskeytoken));
754 static BIN_ATTR_RO(ccadata_aes_128_xts, 2 * sizeof(struct secaeskeytoken));
755 static BIN_ATTR_RO(ccadata_aes_256_xts, 2 * sizeof(struct secaeskeytoken));
756
757 static struct bin_attribute *ccadata_attrs[] = {
758         &bin_attr_ccadata_aes_128,
759         &bin_attr_ccadata_aes_192,
760         &bin_attr_ccadata_aes_256,
761         &bin_attr_ccadata_aes_128_xts,
762         &bin_attr_ccadata_aes_256_xts,
763         NULL
764 };
765
766 static struct attribute_group ccadata_attr_group = {
767         .name      = "ccadata",
768         .bin_attrs = ccadata_attrs,
769 };
770
771 static const struct attribute_group *pkey_attr_groups[] = {
772         &protkey_attr_group,
773         &ccadata_attr_group,
774         NULL,
775 };
776
777 static const struct file_operations pkey_fops = {
778         .owner          = THIS_MODULE,
779         .open           = nonseekable_open,
780         .llseek         = no_llseek,
781         .unlocked_ioctl = pkey_unlocked_ioctl,
782 };
783
784 static struct miscdevice pkey_dev = {
785         .name   = "pkey",
786         .minor  = MISC_DYNAMIC_MINOR,
787         .mode   = 0666,
788         .fops   = &pkey_fops,
789         .groups = pkey_attr_groups,
790 };
791
792 /*
793  * Module init
794  */
795 static int __init pkey_init(void)
796 {
797         cpacf_mask_t kmc_functions;
798
799         /*
800          * The pckmo instruction should be available - even if we don't
801          * actually invoke it. This instruction comes with MSA 3 which
802          * is also the minimum level for the kmc instructions which
803          * are able to work with protected keys.
804          */
805         if (!cpacf_query(CPACF_PCKMO, &pckmo_functions))
806                 return -ENODEV;
807
808         /* check for kmc instructions available */
809         if (!cpacf_query(CPACF_KMC, &kmc_functions))
810                 return -ENODEV;
811         if (!cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_128) ||
812             !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_192) ||
813             !cpacf_test_func(&kmc_functions, CPACF_KMC_PAES_256))
814                 return -ENODEV;
815
816         pkey_debug_init();
817
818         return misc_register(&pkey_dev);
819 }
820
821 /*
822  * Module exit
823  */
824 static void __exit pkey_exit(void)
825 {
826         misc_deregister(&pkey_dev);
827         pkey_debug_exit();
828 }
829
830 module_cpu_feature_match(MSA, pkey_init);
831 module_exit(pkey_exit);