b21ee5b5495a8a4a3bf3e5626cd367d16158757f
[platform/adaptation/renesas_rcar/renesas_kernel.git] / security / integrity / ima / ima_crypto.c
1 /*
2  * Copyright (C) 2005,2006,2007,2008 IBM Corporation
3  *
4  * Authors:
5  * Mimi Zohar <zohar@us.ibm.com>
6  * Kylene Hall <kjhall@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, version 2 of the License.
11  *
12  * File: ima_crypto.c
13  *      Calculates md5/sha1 file hash, template hash, boot-aggreate hash
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/file.h>
18 #include <linux/crypto.h>
19 #include <linux/scatterlist.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include "ima.h"
23
24 static int init_desc(struct hash_desc *desc)
25 {
26         int rc;
27
28         desc->tfm = crypto_alloc_hash(ima_hash, 0, CRYPTO_ALG_ASYNC);
29         if (IS_ERR(desc->tfm)) {
30                 pr_info("IMA: failed to load %s transform: %ld\n",
31                         ima_hash, PTR_ERR(desc->tfm));
32                 rc = PTR_ERR(desc->tfm);
33                 return rc;
34         }
35         desc->flags = 0;
36         rc = crypto_hash_init(desc);
37         if (rc)
38                 crypto_free_hash(desc->tfm);
39         return rc;
40 }
41
42 /*
43  * Calculate the MD5/SHA1 file digest
44  */
45 int ima_calc_hash(struct file *file, char *digest)
46 {
47         struct hash_desc desc;
48         struct scatterlist sg[1];
49         loff_t i_size, offset = 0;
50         char *rbuf;
51         int rc, read = 0;
52
53         rc = init_desc(&desc);
54         if (rc != 0)
55                 return rc;
56
57         rbuf = kzalloc(PAGE_SIZE, GFP_KERNEL);
58         if (!rbuf) {
59                 rc = -ENOMEM;
60                 goto out;
61         }
62         if (!(file->f_mode & FMODE_READ)) {
63                 file->f_mode |= FMODE_READ;
64                 read = 1;
65         }
66         i_size = i_size_read(file->f_dentry->d_inode);
67         while (offset < i_size) {
68                 int rbuf_len;
69
70                 rbuf_len = kernel_read(file, offset, rbuf, PAGE_SIZE);
71                 if (rbuf_len < 0) {
72                         rc = rbuf_len;
73                         break;
74                 }
75                 if (rbuf_len == 0)
76                         break;
77                 offset += rbuf_len;
78                 sg_init_one(sg, rbuf, rbuf_len);
79
80                 rc = crypto_hash_update(&desc, sg, rbuf_len);
81                 if (rc)
82                         break;
83         }
84         kfree(rbuf);
85         if (!rc)
86                 rc = crypto_hash_final(&desc, digest);
87         if (read)
88                 file->f_mode &= ~FMODE_READ;
89 out:
90         crypto_free_hash(desc.tfm);
91         return rc;
92 }
93
94 /*
95  * Calculate the hash of a given template
96  */
97 int ima_calc_template_hash(int template_len, void *template, char *digest)
98 {
99         struct hash_desc desc;
100         struct scatterlist sg[1];
101         int rc;
102
103         rc = init_desc(&desc);
104         if (rc != 0)
105                 return rc;
106
107         sg_init_one(sg, template, template_len);
108         rc = crypto_hash_update(&desc, sg, template_len);
109         if (!rc)
110                 rc = crypto_hash_final(&desc, digest);
111         crypto_free_hash(desc.tfm);
112         return rc;
113 }
114
115 static void __init ima_pcrread(int idx, u8 *pcr)
116 {
117         if (!ima_used_chip)
118                 return;
119
120         if (tpm_pcr_read(TPM_ANY_NUM, idx, pcr) != 0)
121                 pr_err("IMA: Error Communicating to TPM chip\n");
122 }
123
124 /*
125  * Calculate the boot aggregate hash
126  */
127 int __init ima_calc_boot_aggregate(char *digest)
128 {
129         struct hash_desc desc;
130         struct scatterlist sg;
131         u8 pcr_i[IMA_DIGEST_SIZE];
132         int rc, i;
133
134         rc = init_desc(&desc);
135         if (rc != 0)
136                 return rc;
137
138         /* cumulative sha1 over tpm registers 0-7 */
139         for (i = TPM_PCR0; i < TPM_PCR8; i++) {
140                 ima_pcrread(i, pcr_i);
141                 /* now accumulate with current aggregate */
142                 sg_init_one(&sg, pcr_i, IMA_DIGEST_SIZE);
143                 rc = crypto_hash_update(&desc, &sg, IMA_DIGEST_SIZE);
144         }
145         if (!rc)
146                 crypto_hash_final(&desc, digest);
147         crypto_free_hash(desc.tfm);
148         return rc;
149 }