1ab3c3587SDavid Howells /* Large capacity key type 2ab3c3587SDavid Howells * 3428490e3SJason A. Donenfeld * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4ab3c3587SDavid Howells * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 5ab3c3587SDavid Howells * Written by David Howells (dhowells@redhat.com) 6ab3c3587SDavid Howells * 7ab3c3587SDavid Howells * This program is free software; you can redistribute it and/or 8ab3c3587SDavid Howells * modify it under the terms of the GNU General Public Licence 9ab3c3587SDavid Howells * as published by the Free Software Foundation; either version 10ab3c3587SDavid Howells * 2 of the Licence, or (at your option) any later version. 11ab3c3587SDavid Howells */ 12ab3c3587SDavid Howells 137df3e59cSDavid Howells #define pr_fmt(fmt) "big_key: "fmt 14ab3c3587SDavid Howells #include <linux/init.h> 15ab3c3587SDavid Howells #include <linux/seq_file.h> 16ab3c3587SDavid Howells #include <linux/file.h> 17ab3c3587SDavid Howells #include <linux/shmem_fs.h> 18ab3c3587SDavid Howells #include <linux/err.h> 1913100a72SKirill Marinushkin #include <linux/scatterlist.h> 20428490e3SJason A. Donenfeld #include <linux/random.h> 21ab3c3587SDavid Howells #include <keys/user-type.h> 22ab3c3587SDavid Howells #include <keys/big_key-type.h> 23428490e3SJason A. Donenfeld #include <crypto/aead.h> 24ab3c3587SDavid Howells 25*d9f4bb1aSDavid Howells struct big_key_buf { 26*d9f4bb1aSDavid Howells unsigned int nr_pages; 27*d9f4bb1aSDavid Howells void *virt; 28*d9f4bb1aSDavid Howells struct scatterlist *sg; 29*d9f4bb1aSDavid Howells struct page *pages[]; 30*d9f4bb1aSDavid Howells }; 31*d9f4bb1aSDavid Howells 32ab3c3587SDavid Howells /* 33146aa8b1SDavid Howells * Layout of key payload words. 34146aa8b1SDavid Howells */ 35146aa8b1SDavid Howells enum { 36146aa8b1SDavid Howells big_key_data, 37146aa8b1SDavid Howells big_key_path, 38146aa8b1SDavid Howells big_key_path_2nd_part, 39146aa8b1SDavid Howells big_key_len, 40146aa8b1SDavid Howells }; 41146aa8b1SDavid Howells 42146aa8b1SDavid Howells /* 4313100a72SKirill Marinushkin * Crypto operation with big_key data 4413100a72SKirill Marinushkin */ 4513100a72SKirill Marinushkin enum big_key_op { 4613100a72SKirill Marinushkin BIG_KEY_ENC, 4713100a72SKirill Marinushkin BIG_KEY_DEC, 4813100a72SKirill Marinushkin }; 4913100a72SKirill Marinushkin 5013100a72SKirill Marinushkin /* 51ab3c3587SDavid Howells * If the data is under this limit, there's no point creating a shm file to 52ab3c3587SDavid Howells * hold it as the permanently resident metadata for the shmem fs will be at 53ab3c3587SDavid Howells * least as large as the data. 54ab3c3587SDavid Howells */ 55ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry)) 56ab3c3587SDavid Howells 57ab3c3587SDavid Howells /* 5813100a72SKirill Marinushkin * Key size for big_key data encryption 5913100a72SKirill Marinushkin */ 60428490e3SJason A. Donenfeld #define ENC_KEY_SIZE 32 61428490e3SJason A. Donenfeld 62428490e3SJason A. Donenfeld /* 63428490e3SJason A. Donenfeld * Authentication tag length 64428490e3SJason A. Donenfeld */ 65428490e3SJason A. Donenfeld #define ENC_AUTHTAG_SIZE 16 6613100a72SKirill Marinushkin 6713100a72SKirill Marinushkin /* 68ab3c3587SDavid Howells * big_key defined keys take an arbitrary string as the description and an 69ab3c3587SDavid Howells * arbitrary blob of data as the payload 70ab3c3587SDavid Howells */ 71ab3c3587SDavid Howells struct key_type key_type_big_key = { 72ab3c3587SDavid Howells .name = "big_key", 73002edaf7SDavid Howells .preparse = big_key_preparse, 74002edaf7SDavid Howells .free_preparse = big_key_free_preparse, 75002edaf7SDavid Howells .instantiate = generic_key_instantiate, 76ab3c3587SDavid Howells .revoke = big_key_revoke, 77ab3c3587SDavid Howells .destroy = big_key_destroy, 78ab3c3587SDavid Howells .describe = big_key_describe, 79ab3c3587SDavid Howells .read = big_key_read, 80428490e3SJason A. Donenfeld /* no ->update(); don't add it without changing big_key_crypt() nonce */ 81ab3c3587SDavid Howells }; 82ab3c3587SDavid Howells 83ab3c3587SDavid Howells /* 84428490e3SJason A. Donenfeld * Crypto names for big_key data authenticated encryption 8513100a72SKirill Marinushkin */ 86428490e3SJason A. Donenfeld static const char big_key_alg_name[] = "gcm(aes)"; 8713100a72SKirill Marinushkin 8813100a72SKirill Marinushkin /* 89428490e3SJason A. Donenfeld * Crypto algorithms for big_key data authenticated encryption 9013100a72SKirill Marinushkin */ 91428490e3SJason A. Donenfeld static struct crypto_aead *big_key_aead; 9213100a72SKirill Marinushkin 9313100a72SKirill Marinushkin /* 94428490e3SJason A. Donenfeld * Since changing the key affects the entire object, we need a mutex. 9513100a72SKirill Marinushkin */ 96428490e3SJason A. Donenfeld static DEFINE_MUTEX(big_key_aead_lock); 9713100a72SKirill Marinushkin 9813100a72SKirill Marinushkin /* 9913100a72SKirill Marinushkin * Encrypt/decrypt big_key data 10013100a72SKirill Marinushkin */ 101*d9f4bb1aSDavid Howells static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t datalen, u8 *key) 10213100a72SKirill Marinushkin { 103428490e3SJason A. Donenfeld int ret; 104428490e3SJason A. Donenfeld struct aead_request *aead_req; 105428490e3SJason A. Donenfeld /* We always use a zero nonce. The reason we can get away with this is 106428490e3SJason A. Donenfeld * because we're using a different randomly generated key for every 107428490e3SJason A. Donenfeld * different encryption. Notably, too, key_type_big_key doesn't define 108428490e3SJason A. Donenfeld * an .update function, so there's no chance we'll wind up reusing the 109428490e3SJason A. Donenfeld * key to encrypt updated data. Simply put: one key, one encryption. 110428490e3SJason A. Donenfeld */ 111428490e3SJason A. Donenfeld u8 zero_nonce[crypto_aead_ivsize(big_key_aead)]; 11213100a72SKirill Marinushkin 113428490e3SJason A. Donenfeld aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL); 114428490e3SJason A. Donenfeld if (!aead_req) 115428490e3SJason A. Donenfeld return -ENOMEM; 116428490e3SJason A. Donenfeld 117428490e3SJason A. Donenfeld memset(zero_nonce, 0, sizeof(zero_nonce)); 118*d9f4bb1aSDavid Howells aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce); 119428490e3SJason A. Donenfeld aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 120428490e3SJason A. Donenfeld aead_request_set_ad(aead_req, 0); 121428490e3SJason A. Donenfeld 122428490e3SJason A. Donenfeld mutex_lock(&big_key_aead_lock); 123428490e3SJason A. Donenfeld if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) { 12413100a72SKirill Marinushkin ret = -EAGAIN; 12513100a72SKirill Marinushkin goto error; 12613100a72SKirill Marinushkin } 12713100a72SKirill Marinushkin if (op == BIG_KEY_ENC) 128428490e3SJason A. Donenfeld ret = crypto_aead_encrypt(aead_req); 12913100a72SKirill Marinushkin else 130428490e3SJason A. Donenfeld ret = crypto_aead_decrypt(aead_req); 13113100a72SKirill Marinushkin error: 132428490e3SJason A. Donenfeld mutex_unlock(&big_key_aead_lock); 133428490e3SJason A. Donenfeld aead_request_free(aead_req); 13413100a72SKirill Marinushkin return ret; 13513100a72SKirill Marinushkin } 13613100a72SKirill Marinushkin 13713100a72SKirill Marinushkin /* 138*d9f4bb1aSDavid Howells * Free up the buffer. 139*d9f4bb1aSDavid Howells */ 140*d9f4bb1aSDavid Howells static void big_key_free_buffer(struct big_key_buf *buf) 141*d9f4bb1aSDavid Howells { 142*d9f4bb1aSDavid Howells unsigned int i; 143*d9f4bb1aSDavid Howells 144*d9f4bb1aSDavid Howells if (buf->virt) { 145*d9f4bb1aSDavid Howells memset(buf->virt, 0, buf->nr_pages * PAGE_SIZE); 146*d9f4bb1aSDavid Howells vunmap(buf->virt); 147*d9f4bb1aSDavid Howells } 148*d9f4bb1aSDavid Howells 149*d9f4bb1aSDavid Howells for (i = 0; i < buf->nr_pages; i++) 150*d9f4bb1aSDavid Howells if (buf->pages[i]) 151*d9f4bb1aSDavid Howells __free_page(buf->pages[i]); 152*d9f4bb1aSDavid Howells 153*d9f4bb1aSDavid Howells kfree(buf); 154*d9f4bb1aSDavid Howells } 155*d9f4bb1aSDavid Howells 156*d9f4bb1aSDavid Howells /* 157*d9f4bb1aSDavid Howells * Allocate a buffer consisting of a set of pages with a virtual mapping 158*d9f4bb1aSDavid Howells * applied over them. 159*d9f4bb1aSDavid Howells */ 160*d9f4bb1aSDavid Howells static void *big_key_alloc_buffer(size_t len) 161*d9f4bb1aSDavid Howells { 162*d9f4bb1aSDavid Howells struct big_key_buf *buf; 163*d9f4bb1aSDavid Howells unsigned int npg = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 164*d9f4bb1aSDavid Howells unsigned int i, l; 165*d9f4bb1aSDavid Howells 166*d9f4bb1aSDavid Howells buf = kzalloc(sizeof(struct big_key_buf) + 167*d9f4bb1aSDavid Howells sizeof(struct page) * npg + 168*d9f4bb1aSDavid Howells sizeof(struct scatterlist) * npg, 169*d9f4bb1aSDavid Howells GFP_KERNEL); 170*d9f4bb1aSDavid Howells if (!buf) 171*d9f4bb1aSDavid Howells return NULL; 172*d9f4bb1aSDavid Howells 173*d9f4bb1aSDavid Howells buf->nr_pages = npg; 174*d9f4bb1aSDavid Howells buf->sg = (void *)(buf->pages + npg); 175*d9f4bb1aSDavid Howells sg_init_table(buf->sg, npg); 176*d9f4bb1aSDavid Howells 177*d9f4bb1aSDavid Howells for (i = 0; i < buf->nr_pages; i++) { 178*d9f4bb1aSDavid Howells buf->pages[i] = alloc_page(GFP_KERNEL); 179*d9f4bb1aSDavid Howells if (!buf->pages[i]) 180*d9f4bb1aSDavid Howells goto nomem; 181*d9f4bb1aSDavid Howells 182*d9f4bb1aSDavid Howells l = min_t(size_t, len, PAGE_SIZE); 183*d9f4bb1aSDavid Howells sg_set_page(&buf->sg[i], buf->pages[i], l, 0); 184*d9f4bb1aSDavid Howells len -= l; 185*d9f4bb1aSDavid Howells } 186*d9f4bb1aSDavid Howells 187*d9f4bb1aSDavid Howells buf->virt = vmap(buf->pages, buf->nr_pages, VM_MAP, PAGE_KERNEL); 188*d9f4bb1aSDavid Howells if (!buf->virt) 189*d9f4bb1aSDavid Howells goto nomem; 190*d9f4bb1aSDavid Howells 191*d9f4bb1aSDavid Howells return buf; 192*d9f4bb1aSDavid Howells 193*d9f4bb1aSDavid Howells nomem: 194*d9f4bb1aSDavid Howells big_key_free_buffer(buf); 195*d9f4bb1aSDavid Howells return NULL; 196*d9f4bb1aSDavid Howells } 197*d9f4bb1aSDavid Howells 198*d9f4bb1aSDavid Howells /* 199002edaf7SDavid Howells * Preparse a big key 200ab3c3587SDavid Howells */ 201002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep) 202ab3c3587SDavid Howells { 203*d9f4bb1aSDavid Howells struct big_key_buf *buf; 204146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 205ab3c3587SDavid Howells struct file *file; 20613100a72SKirill Marinushkin u8 *enckey; 207ab3c3587SDavid Howells ssize_t written; 208*d9f4bb1aSDavid Howells size_t datalen = prep->datalen, enclen = datalen + ENC_AUTHTAG_SIZE; 209ab3c3587SDavid Howells int ret; 210ab3c3587SDavid Howells 211ab3c3587SDavid Howells if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data) 212*d9f4bb1aSDavid Howells return -EINVAL; 213ab3c3587SDavid Howells 214ab3c3587SDavid Howells /* Set an arbitrary quota */ 215002edaf7SDavid Howells prep->quotalen = 16; 216ab3c3587SDavid Howells 217146aa8b1SDavid Howells prep->payload.data[big_key_len] = (void *)(unsigned long)datalen; 218ab3c3587SDavid Howells 219ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 220ab3c3587SDavid Howells /* Create a shmem file to store the data in. This will permit the data 221ab3c3587SDavid Howells * to be swapped out if needed. 222ab3c3587SDavid Howells * 22313100a72SKirill Marinushkin * File content is stored encrypted with randomly generated key. 224ab3c3587SDavid Howells */ 225e13ec939SChristoph Hellwig loff_t pos = 0; 22613100a72SKirill Marinushkin 227*d9f4bb1aSDavid Howells buf = big_key_alloc_buffer(enclen); 228*d9f4bb1aSDavid Howells if (!buf) 22913100a72SKirill Marinushkin return -ENOMEM; 230*d9f4bb1aSDavid Howells memcpy(buf->virt, prep->data, datalen); 23113100a72SKirill Marinushkin 23213100a72SKirill Marinushkin /* generate random key */ 23313100a72SKirill Marinushkin enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL); 23413100a72SKirill Marinushkin if (!enckey) { 23513100a72SKirill Marinushkin ret = -ENOMEM; 236002edaf7SDavid Howells goto error; 237d2b86970SWei Yongjun } 238428490e3SJason A. Donenfeld ret = get_random_bytes_wait(enckey, ENC_KEY_SIZE); 239428490e3SJason A. Donenfeld if (unlikely(ret)) 24013100a72SKirill Marinushkin goto err_enckey; 24113100a72SKirill Marinushkin 24213100a72SKirill Marinushkin /* encrypt aligned data */ 243*d9f4bb1aSDavid Howells ret = big_key_crypt(BIG_KEY_ENC, buf, datalen, enckey); 24413100a72SKirill Marinushkin if (ret) 24513100a72SKirill Marinushkin goto err_enckey; 24613100a72SKirill Marinushkin 24713100a72SKirill Marinushkin /* save aligned data to file */ 24813100a72SKirill Marinushkin file = shmem_kernel_file_setup("", enclen, 0); 24913100a72SKirill Marinushkin if (IS_ERR(file)) { 25013100a72SKirill Marinushkin ret = PTR_ERR(file); 25113100a72SKirill Marinushkin goto err_enckey; 25213100a72SKirill Marinushkin } 25313100a72SKirill Marinushkin 254*d9f4bb1aSDavid Howells written = kernel_write(file, buf->virt, enclen, &pos); 25513100a72SKirill Marinushkin if (written != enclen) { 25697826c82SDavid Howells ret = written; 257ab3c3587SDavid Howells if (written >= 0) 258ab3c3587SDavid Howells ret = -ENOMEM; 259ab3c3587SDavid Howells goto err_fput; 260ab3c3587SDavid Howells } 261ab3c3587SDavid Howells 262ab3c3587SDavid Howells /* Pin the mount and dentry to the key so that we can open it again 263ab3c3587SDavid Howells * later 264ab3c3587SDavid Howells */ 26513100a72SKirill Marinushkin prep->payload.data[big_key_data] = enckey; 266ab3c3587SDavid Howells *path = file->f_path; 267ab3c3587SDavid Howells path_get(path); 268ab3c3587SDavid Howells fput(file); 269*d9f4bb1aSDavid Howells big_key_free_buffer(buf); 270ab3c3587SDavid Howells } else { 271ab3c3587SDavid Howells /* Just store the data in a buffer */ 272ab3c3587SDavid Howells void *data = kmalloc(datalen, GFP_KERNEL); 27313100a72SKirill Marinushkin 274002edaf7SDavid Howells if (!data) 275002edaf7SDavid Howells return -ENOMEM; 276ab3c3587SDavid Howells 277146aa8b1SDavid Howells prep->payload.data[big_key_data] = data; 278146aa8b1SDavid Howells memcpy(data, prep->data, prep->datalen); 279ab3c3587SDavid Howells } 280ab3c3587SDavid Howells return 0; 281ab3c3587SDavid Howells 282ab3c3587SDavid Howells err_fput: 283ab3c3587SDavid Howells fput(file); 28413100a72SKirill Marinushkin err_enckey: 28591080180SJason A. Donenfeld kzfree(enckey); 286ab3c3587SDavid Howells error: 287*d9f4bb1aSDavid Howells big_key_free_buffer(buf); 288ab3c3587SDavid Howells return ret; 289ab3c3587SDavid Howells } 290ab3c3587SDavid Howells 291ab3c3587SDavid Howells /* 292002edaf7SDavid Howells * Clear preparsement. 293002edaf7SDavid Howells */ 294002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep) 295002edaf7SDavid Howells { 296002edaf7SDavid Howells if (prep->datalen > BIG_KEY_FILE_THRESHOLD) { 297146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 29813100a72SKirill Marinushkin 299002edaf7SDavid Howells path_put(path); 300002edaf7SDavid Howells } 30191080180SJason A. Donenfeld kzfree(prep->payload.data[big_key_data]); 302002edaf7SDavid Howells } 303002edaf7SDavid Howells 304002edaf7SDavid Howells /* 305ab3c3587SDavid Howells * dispose of the links from a revoked keyring 306ab3c3587SDavid Howells * - called with the key sem write-locked 307ab3c3587SDavid Howells */ 308ab3c3587SDavid Howells void big_key_revoke(struct key *key) 309ab3c3587SDavid Howells { 310146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 311ab3c3587SDavid Howells 312ab3c3587SDavid Howells /* clear the quota */ 313ab3c3587SDavid Howells key_payload_reserve(key, 0); 314363b02daSDavid Howells if (key_is_positive(key) && 315146aa8b1SDavid Howells (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD) 316ab3c3587SDavid Howells vfs_truncate(path, 0); 317ab3c3587SDavid Howells } 318ab3c3587SDavid Howells 319ab3c3587SDavid Howells /* 320ab3c3587SDavid Howells * dispose of the data dangling from the corpse of a big_key key 321ab3c3587SDavid Howells */ 322ab3c3587SDavid Howells void big_key_destroy(struct key *key) 323ab3c3587SDavid Howells { 324146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 325146aa8b1SDavid Howells 32613100a72SKirill Marinushkin if (datalen > BIG_KEY_FILE_THRESHOLD) { 327146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 32813100a72SKirill Marinushkin 329ab3c3587SDavid Howells path_put(path); 330ab3c3587SDavid Howells path->mnt = NULL; 331ab3c3587SDavid Howells path->dentry = NULL; 33213100a72SKirill Marinushkin } 33391080180SJason A. Donenfeld kzfree(key->payload.data[big_key_data]); 334146aa8b1SDavid Howells key->payload.data[big_key_data] = NULL; 335ab3c3587SDavid Howells } 336ab3c3587SDavid Howells 337ab3c3587SDavid Howells /* 338ab3c3587SDavid Howells * describe the big_key key 339ab3c3587SDavid Howells */ 340ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m) 341ab3c3587SDavid Howells { 342146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 343ab3c3587SDavid Howells 344ab3c3587SDavid Howells seq_puts(m, key->description); 345ab3c3587SDavid Howells 346363b02daSDavid Howells if (key_is_positive(key)) 347146aa8b1SDavid Howells seq_printf(m, ": %zu [%s]", 348ab3c3587SDavid Howells datalen, 349ab3c3587SDavid Howells datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff"); 350ab3c3587SDavid Howells } 351ab3c3587SDavid Howells 352ab3c3587SDavid Howells /* 353ab3c3587SDavid Howells * read the key data 354ab3c3587SDavid Howells * - the key's semaphore is read-locked 355ab3c3587SDavid Howells */ 356ab3c3587SDavid Howells long big_key_read(const struct key *key, char __user *buffer, size_t buflen) 357ab3c3587SDavid Howells { 358146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 359ab3c3587SDavid Howells long ret; 360ab3c3587SDavid Howells 361ab3c3587SDavid Howells if (!buffer || buflen < datalen) 362ab3c3587SDavid Howells return datalen; 363ab3c3587SDavid Howells 364ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 365*d9f4bb1aSDavid Howells struct big_key_buf *buf; 366146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 367ab3c3587SDavid Howells struct file *file; 36813100a72SKirill Marinushkin u8 *enckey = (u8 *)key->payload.data[big_key_data]; 369428490e3SJason A. Donenfeld size_t enclen = datalen + ENC_AUTHTAG_SIZE; 370bdd1d2d3SChristoph Hellwig loff_t pos = 0; 37113100a72SKirill Marinushkin 372*d9f4bb1aSDavid Howells buf = big_key_alloc_buffer(enclen); 373*d9f4bb1aSDavid Howells if (!buf) 37413100a72SKirill Marinushkin return -ENOMEM; 375ab3c3587SDavid Howells 376ab3c3587SDavid Howells file = dentry_open(path, O_RDONLY, current_cred()); 37713100a72SKirill Marinushkin if (IS_ERR(file)) { 37813100a72SKirill Marinushkin ret = PTR_ERR(file); 37913100a72SKirill Marinushkin goto error; 38013100a72SKirill Marinushkin } 381ab3c3587SDavid Howells 38213100a72SKirill Marinushkin /* read file to kernel and decrypt */ 383*d9f4bb1aSDavid Howells ret = kernel_read(file, buf->virt, enclen, &pos); 38413100a72SKirill Marinushkin if (ret >= 0 && ret != enclen) { 385ab3c3587SDavid Howells ret = -EIO; 38613100a72SKirill Marinushkin goto err_fput; 38713100a72SKirill Marinushkin } 38813100a72SKirill Marinushkin 389*d9f4bb1aSDavid Howells ret = big_key_crypt(BIG_KEY_DEC, buf, enclen, enckey); 39013100a72SKirill Marinushkin if (ret) 39113100a72SKirill Marinushkin goto err_fput; 39213100a72SKirill Marinushkin 39313100a72SKirill Marinushkin ret = datalen; 39413100a72SKirill Marinushkin 39513100a72SKirill Marinushkin /* copy decrypted data to user */ 396*d9f4bb1aSDavid Howells if (copy_to_user(buffer, buf->virt, datalen) != 0) 39713100a72SKirill Marinushkin ret = -EFAULT; 39813100a72SKirill Marinushkin 39913100a72SKirill Marinushkin err_fput: 40013100a72SKirill Marinushkin fput(file); 40113100a72SKirill Marinushkin error: 402*d9f4bb1aSDavid Howells big_key_free_buffer(buf); 403ab3c3587SDavid Howells } else { 404ab3c3587SDavid Howells ret = datalen; 405146aa8b1SDavid Howells if (copy_to_user(buffer, key->payload.data[big_key_data], 406146aa8b1SDavid Howells datalen) != 0) 407ab3c3587SDavid Howells ret = -EFAULT; 408ab3c3587SDavid Howells } 409ab3c3587SDavid Howells 410ab3c3587SDavid Howells return ret; 411ab3c3587SDavid Howells } 412ab3c3587SDavid Howells 41313100a72SKirill Marinushkin /* 41413100a72SKirill Marinushkin * Register key type 41513100a72SKirill Marinushkin */ 416ab3c3587SDavid Howells static int __init big_key_init(void) 417ab3c3587SDavid Howells { 4187df3e59cSDavid Howells int ret; 4197df3e59cSDavid Howells 42013100a72SKirill Marinushkin /* init block cipher */ 421428490e3SJason A. Donenfeld big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC); 422428490e3SJason A. Donenfeld if (IS_ERR(big_key_aead)) { 423428490e3SJason A. Donenfeld ret = PTR_ERR(big_key_aead); 4247df3e59cSDavid Howells pr_err("Can't alloc crypto: %d\n", ret); 425428490e3SJason A. Donenfeld return ret; 4267df3e59cSDavid Howells } 427428490e3SJason A. Donenfeld ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE); 428428490e3SJason A. Donenfeld if (ret < 0) { 429428490e3SJason A. Donenfeld pr_err("Can't set crypto auth tag len: %d\n", ret); 430428490e3SJason A. Donenfeld goto free_aead; 431428490e3SJason A. Donenfeld } 4327df3e59cSDavid Howells 4337df3e59cSDavid Howells ret = register_key_type(&key_type_big_key); 4347df3e59cSDavid Howells if (ret < 0) { 4357df3e59cSDavid Howells pr_err("Can't register type: %d\n", ret); 436428490e3SJason A. Donenfeld goto free_aead; 43713100a72SKirill Marinushkin } 43813100a72SKirill Marinushkin 43913100a72SKirill Marinushkin return 0; 44013100a72SKirill Marinushkin 441428490e3SJason A. Donenfeld free_aead: 442428490e3SJason A. Donenfeld crypto_free_aead(big_key_aead); 44313100a72SKirill Marinushkin return ret; 44413100a72SKirill Marinushkin } 44513100a72SKirill Marinushkin 4467df3e59cSDavid Howells late_initcall(big_key_init); 447