1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2ab3c3587SDavid Howells /* Large capacity key type 3ab3c3587SDavid Howells * 4428490e3SJason A. Donenfeld * Copyright (C) 2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 5ab3c3587SDavid Howells * Copyright (C) 2013 Red Hat, Inc. All Rights Reserved. 6ab3c3587SDavid Howells * Written by David Howells (dhowells@redhat.com) 7ab3c3587SDavid Howells */ 8ab3c3587SDavid Howells 97df3e59cSDavid Howells #define pr_fmt(fmt) "big_key: "fmt 10ab3c3587SDavid Howells #include <linux/init.h> 11ab3c3587SDavid Howells #include <linux/seq_file.h> 12ab3c3587SDavid Howells #include <linux/file.h> 13ab3c3587SDavid Howells #include <linux/shmem_fs.h> 14ab3c3587SDavid Howells #include <linux/err.h> 1513100a72SKirill Marinushkin #include <linux/scatterlist.h> 16428490e3SJason A. Donenfeld #include <linux/random.h> 17514c6032SRandy Dunlap #include <linux/vmalloc.h> 18ab3c3587SDavid Howells #include <keys/user-type.h> 19ab3c3587SDavid Howells #include <keys/big_key-type.h> 20428490e3SJason A. Donenfeld #include <crypto/aead.h> 21a964f395STycho Andersen #include <crypto/gcm.h> 22ab3c3587SDavid Howells 23d9f4bb1aSDavid Howells struct big_key_buf { 24d9f4bb1aSDavid Howells unsigned int nr_pages; 25d9f4bb1aSDavid Howells void *virt; 26d9f4bb1aSDavid Howells struct scatterlist *sg; 27d9f4bb1aSDavid Howells struct page *pages[]; 28d9f4bb1aSDavid Howells }; 29d9f4bb1aSDavid Howells 30ab3c3587SDavid Howells /* 31146aa8b1SDavid Howells * Layout of key payload words. 32146aa8b1SDavid Howells */ 33146aa8b1SDavid Howells enum { 34146aa8b1SDavid Howells big_key_data, 35146aa8b1SDavid Howells big_key_path, 36146aa8b1SDavid Howells big_key_path_2nd_part, 37146aa8b1SDavid Howells big_key_len, 38146aa8b1SDavid Howells }; 39146aa8b1SDavid Howells 40146aa8b1SDavid Howells /* 4113100a72SKirill Marinushkin * Crypto operation with big_key data 4213100a72SKirill Marinushkin */ 4313100a72SKirill Marinushkin enum big_key_op { 4413100a72SKirill Marinushkin BIG_KEY_ENC, 4513100a72SKirill Marinushkin BIG_KEY_DEC, 4613100a72SKirill Marinushkin }; 4713100a72SKirill Marinushkin 4813100a72SKirill Marinushkin /* 49ab3c3587SDavid Howells * If the data is under this limit, there's no point creating a shm file to 50ab3c3587SDavid Howells * hold it as the permanently resident metadata for the shmem fs will be at 51ab3c3587SDavid Howells * least as large as the data. 52ab3c3587SDavid Howells */ 53ab3c3587SDavid Howells #define BIG_KEY_FILE_THRESHOLD (sizeof(struct inode) + sizeof(struct dentry)) 54ab3c3587SDavid Howells 55ab3c3587SDavid Howells /* 5613100a72SKirill Marinushkin * Key size for big_key data encryption 5713100a72SKirill Marinushkin */ 58428490e3SJason A. Donenfeld #define ENC_KEY_SIZE 32 59428490e3SJason A. Donenfeld 60428490e3SJason A. Donenfeld /* 61428490e3SJason A. Donenfeld * Authentication tag length 62428490e3SJason A. Donenfeld */ 63428490e3SJason A. Donenfeld #define ENC_AUTHTAG_SIZE 16 6413100a72SKirill Marinushkin 6513100a72SKirill Marinushkin /* 66ab3c3587SDavid Howells * big_key defined keys take an arbitrary string as the description and an 67ab3c3587SDavid Howells * arbitrary blob of data as the payload 68ab3c3587SDavid Howells */ 69ab3c3587SDavid Howells struct key_type key_type_big_key = { 70ab3c3587SDavid Howells .name = "big_key", 71002edaf7SDavid Howells .preparse = big_key_preparse, 72002edaf7SDavid Howells .free_preparse = big_key_free_preparse, 73002edaf7SDavid Howells .instantiate = generic_key_instantiate, 74ab3c3587SDavid Howells .revoke = big_key_revoke, 75ab3c3587SDavid Howells .destroy = big_key_destroy, 76ab3c3587SDavid Howells .describe = big_key_describe, 77ab3c3587SDavid Howells .read = big_key_read, 78428490e3SJason A. Donenfeld /* no ->update(); don't add it without changing big_key_crypt() nonce */ 79ab3c3587SDavid Howells }; 80ab3c3587SDavid Howells 81ab3c3587SDavid Howells /* 82428490e3SJason A. Donenfeld * Crypto names for big_key data authenticated encryption 8313100a72SKirill Marinushkin */ 84428490e3SJason A. Donenfeld static const char big_key_alg_name[] = "gcm(aes)"; 85a964f395STycho Andersen #define BIG_KEY_IV_SIZE GCM_AES_IV_SIZE 8613100a72SKirill Marinushkin 8713100a72SKirill Marinushkin /* 88428490e3SJason A. Donenfeld * Crypto algorithms for big_key data authenticated encryption 8913100a72SKirill Marinushkin */ 90428490e3SJason A. Donenfeld static struct crypto_aead *big_key_aead; 9113100a72SKirill Marinushkin 9213100a72SKirill Marinushkin /* 93428490e3SJason A. Donenfeld * Since changing the key affects the entire object, we need a mutex. 9413100a72SKirill Marinushkin */ 95428490e3SJason A. Donenfeld static DEFINE_MUTEX(big_key_aead_lock); 9613100a72SKirill Marinushkin 9713100a72SKirill Marinushkin /* 9813100a72SKirill Marinushkin * Encrypt/decrypt big_key data 9913100a72SKirill Marinushkin */ 100d9f4bb1aSDavid Howells static int big_key_crypt(enum big_key_op op, struct big_key_buf *buf, size_t datalen, u8 *key) 10113100a72SKirill Marinushkin { 102428490e3SJason A. Donenfeld int ret; 103428490e3SJason A. Donenfeld struct aead_request *aead_req; 104428490e3SJason A. Donenfeld /* We always use a zero nonce. The reason we can get away with this is 105428490e3SJason A. Donenfeld * because we're using a different randomly generated key for every 106428490e3SJason A. Donenfeld * different encryption. Notably, too, key_type_big_key doesn't define 107428490e3SJason A. Donenfeld * an .update function, so there's no chance we'll wind up reusing the 108428490e3SJason A. Donenfeld * key to encrypt updated data. Simply put: one key, one encryption. 109428490e3SJason A. Donenfeld */ 110a964f395STycho Andersen u8 zero_nonce[BIG_KEY_IV_SIZE]; 11113100a72SKirill Marinushkin 112428490e3SJason A. Donenfeld aead_req = aead_request_alloc(big_key_aead, GFP_KERNEL); 113428490e3SJason A. Donenfeld if (!aead_req) 114428490e3SJason A. Donenfeld return -ENOMEM; 115428490e3SJason A. Donenfeld 116428490e3SJason A. Donenfeld memset(zero_nonce, 0, sizeof(zero_nonce)); 117d9f4bb1aSDavid Howells aead_request_set_crypt(aead_req, buf->sg, buf->sg, datalen, zero_nonce); 118428490e3SJason A. Donenfeld aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 119428490e3SJason A. Donenfeld aead_request_set_ad(aead_req, 0); 120428490e3SJason A. Donenfeld 121428490e3SJason A. Donenfeld mutex_lock(&big_key_aead_lock); 122428490e3SJason A. Donenfeld if (crypto_aead_setkey(big_key_aead, key, ENC_KEY_SIZE)) { 12313100a72SKirill Marinushkin ret = -EAGAIN; 12413100a72SKirill Marinushkin goto error; 12513100a72SKirill Marinushkin } 12613100a72SKirill Marinushkin if (op == BIG_KEY_ENC) 127428490e3SJason A. Donenfeld ret = crypto_aead_encrypt(aead_req); 12813100a72SKirill Marinushkin else 129428490e3SJason A. Donenfeld ret = crypto_aead_decrypt(aead_req); 13013100a72SKirill Marinushkin error: 131428490e3SJason A. Donenfeld mutex_unlock(&big_key_aead_lock); 132428490e3SJason A. Donenfeld aead_request_free(aead_req); 13313100a72SKirill Marinushkin return ret; 13413100a72SKirill Marinushkin } 13513100a72SKirill Marinushkin 13613100a72SKirill Marinushkin /* 137d9f4bb1aSDavid Howells * Free up the buffer. 138d9f4bb1aSDavid Howells */ 139d9f4bb1aSDavid Howells static void big_key_free_buffer(struct big_key_buf *buf) 140d9f4bb1aSDavid Howells { 141d9f4bb1aSDavid Howells unsigned int i; 142d9f4bb1aSDavid Howells 143d9f4bb1aSDavid Howells if (buf->virt) { 144d9f4bb1aSDavid Howells memset(buf->virt, 0, buf->nr_pages * PAGE_SIZE); 145d9f4bb1aSDavid Howells vunmap(buf->virt); 146d9f4bb1aSDavid Howells } 147d9f4bb1aSDavid Howells 148d9f4bb1aSDavid Howells for (i = 0; i < buf->nr_pages; i++) 149d9f4bb1aSDavid Howells if (buf->pages[i]) 150d9f4bb1aSDavid Howells __free_page(buf->pages[i]); 151d9f4bb1aSDavid Howells 152d9f4bb1aSDavid Howells kfree(buf); 153d9f4bb1aSDavid Howells } 154d9f4bb1aSDavid Howells 155d9f4bb1aSDavid Howells /* 156d9f4bb1aSDavid Howells * Allocate a buffer consisting of a set of pages with a virtual mapping 157d9f4bb1aSDavid Howells * applied over them. 158d9f4bb1aSDavid Howells */ 159d9f4bb1aSDavid Howells static void *big_key_alloc_buffer(size_t len) 160d9f4bb1aSDavid Howells { 161d9f4bb1aSDavid Howells struct big_key_buf *buf; 162d9f4bb1aSDavid Howells unsigned int npg = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 163d9f4bb1aSDavid Howells unsigned int i, l; 164d9f4bb1aSDavid Howells 165d9f4bb1aSDavid Howells buf = kzalloc(sizeof(struct big_key_buf) + 166d9f4bb1aSDavid Howells sizeof(struct page) * npg + 167d9f4bb1aSDavid Howells sizeof(struct scatterlist) * npg, 168d9f4bb1aSDavid Howells GFP_KERNEL); 169d9f4bb1aSDavid Howells if (!buf) 170d9f4bb1aSDavid Howells return NULL; 171d9f4bb1aSDavid Howells 172d9f4bb1aSDavid Howells buf->nr_pages = npg; 173d9f4bb1aSDavid Howells buf->sg = (void *)(buf->pages + npg); 174d9f4bb1aSDavid Howells sg_init_table(buf->sg, npg); 175d9f4bb1aSDavid Howells 176d9f4bb1aSDavid Howells for (i = 0; i < buf->nr_pages; i++) { 177d9f4bb1aSDavid Howells buf->pages[i] = alloc_page(GFP_KERNEL); 178d9f4bb1aSDavid Howells if (!buf->pages[i]) 179d9f4bb1aSDavid Howells goto nomem; 180d9f4bb1aSDavid Howells 181d9f4bb1aSDavid Howells l = min_t(size_t, len, PAGE_SIZE); 182d9f4bb1aSDavid Howells sg_set_page(&buf->sg[i], buf->pages[i], l, 0); 183d9f4bb1aSDavid Howells len -= l; 184d9f4bb1aSDavid Howells } 185d9f4bb1aSDavid Howells 186d9f4bb1aSDavid Howells buf->virt = vmap(buf->pages, buf->nr_pages, VM_MAP, PAGE_KERNEL); 187d9f4bb1aSDavid Howells if (!buf->virt) 188d9f4bb1aSDavid Howells goto nomem; 189d9f4bb1aSDavid Howells 190d9f4bb1aSDavid Howells return buf; 191d9f4bb1aSDavid Howells 192d9f4bb1aSDavid Howells nomem: 193d9f4bb1aSDavid Howells big_key_free_buffer(buf); 194d9f4bb1aSDavid Howells return NULL; 195d9f4bb1aSDavid Howells } 196d9f4bb1aSDavid Howells 197d9f4bb1aSDavid Howells /* 198002edaf7SDavid Howells * Preparse a big key 199ab3c3587SDavid Howells */ 200002edaf7SDavid Howells int big_key_preparse(struct key_preparsed_payload *prep) 201ab3c3587SDavid Howells { 202d9f4bb1aSDavid Howells struct big_key_buf *buf; 203146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 204ab3c3587SDavid Howells struct file *file; 20513100a72SKirill Marinushkin u8 *enckey; 206ab3c3587SDavid Howells ssize_t written; 207d9f4bb1aSDavid Howells size_t datalen = prep->datalen, enclen = datalen + ENC_AUTHTAG_SIZE; 208ab3c3587SDavid Howells int ret; 209ab3c3587SDavid Howells 210ab3c3587SDavid Howells if (datalen <= 0 || datalen > 1024 * 1024 || !prep->data) 211d9f4bb1aSDavid Howells return -EINVAL; 212ab3c3587SDavid Howells 213ab3c3587SDavid Howells /* Set an arbitrary quota */ 214002edaf7SDavid Howells prep->quotalen = 16; 215ab3c3587SDavid Howells 216146aa8b1SDavid Howells prep->payload.data[big_key_len] = (void *)(unsigned long)datalen; 217ab3c3587SDavid Howells 218ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 219ab3c3587SDavid Howells /* Create a shmem file to store the data in. This will permit the data 220ab3c3587SDavid Howells * to be swapped out if needed. 221ab3c3587SDavid Howells * 22213100a72SKirill Marinushkin * File content is stored encrypted with randomly generated key. 223ab3c3587SDavid Howells */ 224e13ec939SChristoph Hellwig loff_t pos = 0; 22513100a72SKirill Marinushkin 226d9f4bb1aSDavid Howells buf = big_key_alloc_buffer(enclen); 227d9f4bb1aSDavid Howells if (!buf) 22813100a72SKirill Marinushkin return -ENOMEM; 229d9f4bb1aSDavid Howells memcpy(buf->virt, prep->data, datalen); 23013100a72SKirill Marinushkin 23113100a72SKirill Marinushkin /* generate random key */ 23213100a72SKirill Marinushkin enckey = kmalloc(ENC_KEY_SIZE, GFP_KERNEL); 23313100a72SKirill Marinushkin if (!enckey) { 23413100a72SKirill Marinushkin ret = -ENOMEM; 235002edaf7SDavid Howells goto error; 236d2b86970SWei Yongjun } 237428490e3SJason A. Donenfeld ret = get_random_bytes_wait(enckey, ENC_KEY_SIZE); 238428490e3SJason A. Donenfeld if (unlikely(ret)) 23913100a72SKirill Marinushkin goto err_enckey; 24013100a72SKirill Marinushkin 24113100a72SKirill Marinushkin /* encrypt aligned data */ 242d9f4bb1aSDavid Howells ret = big_key_crypt(BIG_KEY_ENC, buf, datalen, enckey); 24313100a72SKirill Marinushkin if (ret) 24413100a72SKirill Marinushkin goto err_enckey; 24513100a72SKirill Marinushkin 24613100a72SKirill Marinushkin /* save aligned data to file */ 24713100a72SKirill Marinushkin file = shmem_kernel_file_setup("", enclen, 0); 24813100a72SKirill Marinushkin if (IS_ERR(file)) { 24913100a72SKirill Marinushkin ret = PTR_ERR(file); 25013100a72SKirill Marinushkin goto err_enckey; 25113100a72SKirill Marinushkin } 25213100a72SKirill Marinushkin 253d9f4bb1aSDavid Howells written = kernel_write(file, buf->virt, enclen, &pos); 25413100a72SKirill Marinushkin if (written != enclen) { 25597826c82SDavid Howells ret = written; 256ab3c3587SDavid Howells if (written >= 0) 257ab3c3587SDavid Howells ret = -ENOMEM; 258ab3c3587SDavid Howells goto err_fput; 259ab3c3587SDavid Howells } 260ab3c3587SDavid Howells 261ab3c3587SDavid Howells /* Pin the mount and dentry to the key so that we can open it again 262ab3c3587SDavid Howells * later 263ab3c3587SDavid Howells */ 26413100a72SKirill Marinushkin prep->payload.data[big_key_data] = enckey; 265ab3c3587SDavid Howells *path = file->f_path; 266ab3c3587SDavid Howells path_get(path); 267ab3c3587SDavid Howells fput(file); 268d9f4bb1aSDavid Howells big_key_free_buffer(buf); 269ab3c3587SDavid Howells } else { 270ab3c3587SDavid Howells /* Just store the data in a buffer */ 271ab3c3587SDavid Howells void *data = kmalloc(datalen, GFP_KERNEL); 27213100a72SKirill Marinushkin 273002edaf7SDavid Howells if (!data) 274002edaf7SDavid Howells return -ENOMEM; 275ab3c3587SDavid Howells 276146aa8b1SDavid Howells prep->payload.data[big_key_data] = data; 277146aa8b1SDavid Howells memcpy(data, prep->data, prep->datalen); 278ab3c3587SDavid Howells } 279ab3c3587SDavid Howells return 0; 280ab3c3587SDavid Howells 281ab3c3587SDavid Howells err_fput: 282ab3c3587SDavid Howells fput(file); 28313100a72SKirill Marinushkin err_enckey: 28491080180SJason A. Donenfeld kzfree(enckey); 285ab3c3587SDavid Howells error: 286d9f4bb1aSDavid Howells big_key_free_buffer(buf); 287ab3c3587SDavid Howells return ret; 288ab3c3587SDavid Howells } 289ab3c3587SDavid Howells 290ab3c3587SDavid Howells /* 291002edaf7SDavid Howells * Clear preparsement. 292002edaf7SDavid Howells */ 293002edaf7SDavid Howells void big_key_free_preparse(struct key_preparsed_payload *prep) 294002edaf7SDavid Howells { 295002edaf7SDavid Howells if (prep->datalen > BIG_KEY_FILE_THRESHOLD) { 296146aa8b1SDavid Howells struct path *path = (struct path *)&prep->payload.data[big_key_path]; 29713100a72SKirill Marinushkin 298002edaf7SDavid Howells path_put(path); 299002edaf7SDavid Howells } 30091080180SJason A. Donenfeld kzfree(prep->payload.data[big_key_data]); 301002edaf7SDavid Howells } 302002edaf7SDavid Howells 303002edaf7SDavid Howells /* 304ab3c3587SDavid Howells * dispose of the links from a revoked keyring 305ab3c3587SDavid Howells * - called with the key sem write-locked 306ab3c3587SDavid Howells */ 307ab3c3587SDavid Howells void big_key_revoke(struct key *key) 308ab3c3587SDavid Howells { 309146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 310ab3c3587SDavid Howells 311ab3c3587SDavid Howells /* clear the quota */ 312ab3c3587SDavid Howells key_payload_reserve(key, 0); 313363b02daSDavid Howells if (key_is_positive(key) && 314146aa8b1SDavid Howells (size_t)key->payload.data[big_key_len] > BIG_KEY_FILE_THRESHOLD) 315ab3c3587SDavid Howells vfs_truncate(path, 0); 316ab3c3587SDavid Howells } 317ab3c3587SDavid Howells 318ab3c3587SDavid Howells /* 319ab3c3587SDavid Howells * dispose of the data dangling from the corpse of a big_key key 320ab3c3587SDavid Howells */ 321ab3c3587SDavid Howells void big_key_destroy(struct key *key) 322ab3c3587SDavid Howells { 323146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 324146aa8b1SDavid Howells 32513100a72SKirill Marinushkin if (datalen > BIG_KEY_FILE_THRESHOLD) { 326146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 32713100a72SKirill Marinushkin 328ab3c3587SDavid Howells path_put(path); 329ab3c3587SDavid Howells path->mnt = NULL; 330ab3c3587SDavid Howells path->dentry = NULL; 33113100a72SKirill Marinushkin } 33291080180SJason A. Donenfeld kzfree(key->payload.data[big_key_data]); 333146aa8b1SDavid Howells key->payload.data[big_key_data] = NULL; 334ab3c3587SDavid Howells } 335ab3c3587SDavid Howells 336ab3c3587SDavid Howells /* 337ab3c3587SDavid Howells * describe the big_key key 338ab3c3587SDavid Howells */ 339ab3c3587SDavid Howells void big_key_describe(const struct key *key, struct seq_file *m) 340ab3c3587SDavid Howells { 341146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 342ab3c3587SDavid Howells 343ab3c3587SDavid Howells seq_puts(m, key->description); 344ab3c3587SDavid Howells 345363b02daSDavid Howells if (key_is_positive(key)) 346146aa8b1SDavid Howells seq_printf(m, ": %zu [%s]", 347ab3c3587SDavid Howells datalen, 348ab3c3587SDavid Howells datalen > BIG_KEY_FILE_THRESHOLD ? "file" : "buff"); 349ab3c3587SDavid Howells } 350ab3c3587SDavid Howells 351ab3c3587SDavid Howells /* 352ab3c3587SDavid Howells * read the key data 353ab3c3587SDavid Howells * - the key's semaphore is read-locked 354ab3c3587SDavid Howells */ 355*d3ec10aaSWaiman Long long big_key_read(const struct key *key, char *buffer, size_t buflen) 356ab3c3587SDavid Howells { 357146aa8b1SDavid Howells size_t datalen = (size_t)key->payload.data[big_key_len]; 358ab3c3587SDavid Howells long ret; 359ab3c3587SDavid Howells 360ab3c3587SDavid Howells if (!buffer || buflen < datalen) 361ab3c3587SDavid Howells return datalen; 362ab3c3587SDavid Howells 363ab3c3587SDavid Howells if (datalen > BIG_KEY_FILE_THRESHOLD) { 364d9f4bb1aSDavid Howells struct big_key_buf *buf; 365146aa8b1SDavid Howells struct path *path = (struct path *)&key->payload.data[big_key_path]; 366ab3c3587SDavid Howells struct file *file; 36713100a72SKirill Marinushkin u8 *enckey = (u8 *)key->payload.data[big_key_data]; 368428490e3SJason A. Donenfeld size_t enclen = datalen + ENC_AUTHTAG_SIZE; 369bdd1d2d3SChristoph Hellwig loff_t pos = 0; 37013100a72SKirill Marinushkin 371d9f4bb1aSDavid Howells buf = big_key_alloc_buffer(enclen); 372d9f4bb1aSDavid Howells if (!buf) 37313100a72SKirill Marinushkin return -ENOMEM; 374ab3c3587SDavid Howells 375ab3c3587SDavid Howells file = dentry_open(path, O_RDONLY, current_cred()); 37613100a72SKirill Marinushkin if (IS_ERR(file)) { 37713100a72SKirill Marinushkin ret = PTR_ERR(file); 37813100a72SKirill Marinushkin goto error; 37913100a72SKirill Marinushkin } 380ab3c3587SDavid Howells 38113100a72SKirill Marinushkin /* read file to kernel and decrypt */ 382d9f4bb1aSDavid Howells ret = kernel_read(file, buf->virt, enclen, &pos); 38313100a72SKirill Marinushkin if (ret >= 0 && ret != enclen) { 384ab3c3587SDavid Howells ret = -EIO; 38513100a72SKirill Marinushkin goto err_fput; 38613100a72SKirill Marinushkin } 38713100a72SKirill Marinushkin 388d9f4bb1aSDavid Howells ret = big_key_crypt(BIG_KEY_DEC, buf, enclen, enckey); 38913100a72SKirill Marinushkin if (ret) 39013100a72SKirill Marinushkin goto err_fput; 39113100a72SKirill Marinushkin 39213100a72SKirill Marinushkin ret = datalen; 39313100a72SKirill Marinushkin 394*d3ec10aaSWaiman Long /* copy out decrypted data */ 395*d3ec10aaSWaiman Long memcpy(buffer, buf->virt, datalen); 39613100a72SKirill Marinushkin 39713100a72SKirill Marinushkin err_fput: 39813100a72SKirill Marinushkin fput(file); 39913100a72SKirill Marinushkin error: 400d9f4bb1aSDavid Howells big_key_free_buffer(buf); 401ab3c3587SDavid Howells } else { 402ab3c3587SDavid Howells ret = datalen; 403*d3ec10aaSWaiman Long memcpy(buffer, key->payload.data[big_key_data], datalen); 404ab3c3587SDavid Howells } 405ab3c3587SDavid Howells 406ab3c3587SDavid Howells return ret; 407ab3c3587SDavid Howells } 408ab3c3587SDavid Howells 40913100a72SKirill Marinushkin /* 41013100a72SKirill Marinushkin * Register key type 41113100a72SKirill Marinushkin */ 412ab3c3587SDavid Howells static int __init big_key_init(void) 413ab3c3587SDavid Howells { 4147df3e59cSDavid Howells int ret; 4157df3e59cSDavid Howells 41613100a72SKirill Marinushkin /* init block cipher */ 417428490e3SJason A. Donenfeld big_key_aead = crypto_alloc_aead(big_key_alg_name, 0, CRYPTO_ALG_ASYNC); 418428490e3SJason A. Donenfeld if (IS_ERR(big_key_aead)) { 419428490e3SJason A. Donenfeld ret = PTR_ERR(big_key_aead); 4207df3e59cSDavid Howells pr_err("Can't alloc crypto: %d\n", ret); 421428490e3SJason A. Donenfeld return ret; 4227df3e59cSDavid Howells } 423a964f395STycho Andersen 424a964f395STycho Andersen if (unlikely(crypto_aead_ivsize(big_key_aead) != BIG_KEY_IV_SIZE)) { 425a964f395STycho Andersen WARN(1, "big key algorithm changed?"); 426a964f395STycho Andersen ret = -EINVAL; 427a964f395STycho Andersen goto free_aead; 428a964f395STycho Andersen } 429a964f395STycho Andersen 430428490e3SJason A. Donenfeld ret = crypto_aead_setauthsize(big_key_aead, ENC_AUTHTAG_SIZE); 431428490e3SJason A. Donenfeld if (ret < 0) { 432428490e3SJason A. Donenfeld pr_err("Can't set crypto auth tag len: %d\n", ret); 433428490e3SJason A. Donenfeld goto free_aead; 434428490e3SJason A. Donenfeld } 4357df3e59cSDavid Howells 4367df3e59cSDavid Howells ret = register_key_type(&key_type_big_key); 4377df3e59cSDavid Howells if (ret < 0) { 4387df3e59cSDavid Howells pr_err("Can't register type: %d\n", ret); 439428490e3SJason A. Donenfeld goto free_aead; 44013100a72SKirill Marinushkin } 44113100a72SKirill Marinushkin 44213100a72SKirill Marinushkin return 0; 44313100a72SKirill Marinushkin 444428490e3SJason A. Donenfeld free_aead: 445428490e3SJason A. Donenfeld crypto_free_aead(big_key_aead); 44613100a72SKirill Marinushkin return ret; 44713100a72SKirill Marinushkin } 44813100a72SKirill Marinushkin 4497df3e59cSDavid Howells late_initcall(big_key_init); 450