crypto: introduce generic cipher API & built-in implementation
[qemu.git] / crypto / cipher.c
1 /*
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
21 #include "crypto/cipher.h"
22
23 static size_t alg_key_len[QCRYPTO_CIPHER_ALG_LAST] = {
24 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
25 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
26 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
27 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
28 };
29
30 static bool
31 qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
32 size_t nkey,
33 Error **errp)
34 {
35 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG_LAST) {
36 error_setg(errp, "Cipher algorithm %d out of range",
37 alg);
38 return false;
39 }
40
41 if (alg_key_len[alg] != nkey) {
42 error_setg(errp, "Cipher key length %zu should be %zu",
43 alg_key_len[alg], nkey);
44 return false;
45 }
46 return true;
47 }
48
49 #include "crypto/cipher-builtin.c"