blob: 5402d1852591d1fa9d2be217b8cce150d30f1bd2 [file] [log] [blame]
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +01001/*
2 * QEMU Crypto cipher algorithms
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Peter Maydell42f7a442016-01-26 18:16:55 +000021#include "qemu/osdep.h"
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010022#include "crypto/cipher.h"
23
Daniel P. Berrange62893b62015-07-01 18:10:33 +010024
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000025static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010026 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
27 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
28 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
29 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000030 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
Daniel P. Berrange94318522016-02-10 17:07:42 +000031 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
32 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
33 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
Daniel P. Berrange50f67532016-02-10 17:07:42 +000034 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
35 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
36 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010037};
38
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000039static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010040 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
41 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
42 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
43 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000044 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
Daniel P. Berrange94318522016-02-10 17:07:42 +000045 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
46 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
47 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
Daniel P. Berrange50f67532016-02-10 17:07:42 +000048 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
49 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
50 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010051};
52
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000053static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010054 [QCRYPTO_CIPHER_MODE_ECB] = false,
55 [QCRYPTO_CIPHER_MODE_CBC] = true,
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +000056 [QCRYPTO_CIPHER_MODE_XTS] = true,
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010057};
58
59
60size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
61{
62 if (alg >= G_N_ELEMENTS(alg_key_len)) {
63 return 0;
64 }
65 return alg_block_len[alg];
66}
67
68
69size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
70{
71 if (alg >= G_N_ELEMENTS(alg_key_len)) {
72 return 0;
73 }
74 return alg_key_len[alg];
75}
76
77
78size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
79 QCryptoCipherMode mode)
80{
81 if (alg >= G_N_ELEMENTS(alg_block_len)) {
82 return 0;
83 }
84 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
85 return 0;
86 }
87
88 if (mode_need_iv[mode]) {
89 return alg_block_len[alg];
90 }
91 return 0;
92}
93
94
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010095static bool
96qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +000097 QCryptoCipherMode mode,
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010098 size_t nkey,
99 Error **errp)
100{
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +0000101 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100102 error_setg(errp, "Cipher algorithm %d out of range",
103 alg);
104 return false;
105 }
106
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +0000107 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
108 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
109 error_setg(errp, "XTS mode not compatible with DES-RFB");
110 return false;
111 }
112 if (nkey % 2) {
113 error_setg(errp, "XTS cipher key length should be a multiple of 2");
114 return false;
115 }
116
117 if (alg_key_len[alg] != (nkey / 2)) {
118 error_setg(errp, "Cipher key length %zu should be %zu",
119 nkey, alg_key_len[alg] * 2);
120 return false;
121 }
122 } else {
123 if (alg_key_len[alg] != nkey) {
124 error_setg(errp, "Cipher key length %zu should be %zu",
125 nkey, alg_key_len[alg]);
126 return false;
127 }
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100128 }
129 return true;
130}
131
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100132#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100133static uint8_t *
134qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
135 size_t nkey)
136{
137 uint8_t *ret = g_new0(uint8_t, nkey);
138 size_t i;
139 for (i = 0; i < nkey; i++) {
140 uint8_t r = key[i];
141 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
142 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
143 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
144 ret[i] = r;
145 }
146 return ret;
147}
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100148#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100149
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100150#ifdef CONFIG_GCRYPT
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100151#include "crypto/cipher-gcrypt.c"
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100152#elif defined CONFIG_NETTLE
Daniel P. Berrangeed754742015-07-01 18:10:34 +0100153#include "crypto/cipher-nettle.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100154#else
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100155#include "crypto/cipher-builtin.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100156#endif