blob: cafb454363c4ea7cea61f5945531cbd5d8c90b9d [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"
Markus Armbrusterda34e652016-03-14 09:01:28 +010022#include "qapi/error.h"
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010023#include "crypto/cipher.h"
24
Daniel P. Berrange62893b62015-07-01 18:10:33 +010025
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000026static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010027 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
28 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
29 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
30 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000031 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
Daniel P. Berrange94318522016-02-10 17:07:42 +000032 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
33 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
34 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
Daniel P. Berrange50f67532016-02-10 17:07:42 +000035 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
36 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
37 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010038};
39
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000040static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010041 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
42 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
43 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
44 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000045 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
Daniel P. Berrange94318522016-02-10 17:07:42 +000046 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
47 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
48 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
Daniel P. Berrange50f67532016-02-10 17:07:42 +000049 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
50 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
51 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010052};
53
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000054static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010055 [QCRYPTO_CIPHER_MODE_ECB] = false,
56 [QCRYPTO_CIPHER_MODE_CBC] = true,
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +000057 [QCRYPTO_CIPHER_MODE_XTS] = true,
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010058};
59
60
61size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
62{
63 if (alg >= G_N_ELEMENTS(alg_key_len)) {
64 return 0;
65 }
66 return alg_block_len[alg];
67}
68
69
70size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
71{
72 if (alg >= G_N_ELEMENTS(alg_key_len)) {
73 return 0;
74 }
75 return alg_key_len[alg];
76}
77
78
79size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
80 QCryptoCipherMode mode)
81{
82 if (alg >= G_N_ELEMENTS(alg_block_len)) {
83 return 0;
84 }
85 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
86 return 0;
87 }
88
89 if (mode_need_iv[mode]) {
90 return alg_block_len[alg];
91 }
92 return 0;
93}
94
95
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010096static bool
97qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +000098 QCryptoCipherMode mode,
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010099 size_t nkey,
100 Error **errp)
101{
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +0000102 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100103 error_setg(errp, "Cipher algorithm %d out of range",
104 alg);
105 return false;
106 }
107
Daniel P. Berrangeeaec9032016-02-11 14:05:21 +0000108 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
109 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
110 error_setg(errp, "XTS mode not compatible with DES-RFB");
111 return false;
112 }
113 if (nkey % 2) {
114 error_setg(errp, "XTS cipher key length should be a multiple of 2");
115 return false;
116 }
117
118 if (alg_key_len[alg] != (nkey / 2)) {
119 error_setg(errp, "Cipher key length %zu should be %zu",
120 nkey, alg_key_len[alg] * 2);
121 return false;
122 }
123 } else {
124 if (alg_key_len[alg] != nkey) {
125 error_setg(errp, "Cipher key length %zu should be %zu",
126 nkey, alg_key_len[alg]);
127 return false;
128 }
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100129 }
130 return true;
131}
132
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100133#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100134static uint8_t *
135qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
136 size_t nkey)
137{
138 uint8_t *ret = g_new0(uint8_t, nkey);
139 size_t i;
140 for (i = 0; i < nkey; i++) {
141 uint8_t r = key[i];
142 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
143 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
144 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
145 ret[i] = r;
146 }
147 return ret;
148}
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100149#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100150
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100151#ifdef CONFIG_GCRYPT
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100152#include "crypto/cipher-gcrypt.c"
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100153#elif defined CONFIG_NETTLE
Daniel P. Berrangeed754742015-07-01 18:10:34 +0100154#include "crypto/cipher-nettle.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100155#else
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100156#include "crypto/cipher-builtin.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100157#endif