Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Crypto cipher nettle 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 <nettle/nettle-types.h> |
| 22 | #include <nettle/aes.h> |
| 23 | #include <nettle/des.h> |
| 24 | #include <nettle/cbc.h> |
| 25 | |
Radim Krčmář | becaeb7 | 2015-07-10 19:18:00 +0200 | [diff] [blame] | 26 | #if CONFIG_NETTLE_VERSION_MAJOR < 3 |
| 27 | typedef nettle_crypt_func nettle_cipher_func; |
Radim Krčmář | d3462e3 | 2015-07-10 19:18:01 +0200 | [diff] [blame] | 28 | |
| 29 | typedef void * cipher_ctx_t; |
| 30 | typedef unsigned cipher_length_t; |
| 31 | #else |
| 32 | typedef const void * cipher_ctx_t; |
| 33 | typedef size_t cipher_length_t; |
Radim Krčmář | becaeb7 | 2015-07-10 19:18:00 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
Radim Krčmář | d3462e3 | 2015-07-10 19:18:01 +0200 | [diff] [blame] | 36 | static nettle_cipher_func aes_encrypt_wrapper; |
| 37 | static nettle_cipher_func aes_decrypt_wrapper; |
| 38 | static nettle_cipher_func des_encrypt_wrapper; |
| 39 | static nettle_cipher_func des_decrypt_wrapper; |
| 40 | |
| 41 | static void aes_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, |
| 42 | uint8_t *dst, const uint8_t *src) |
| 43 | { |
| 44 | aes_encrypt(ctx, length, dst, src); |
| 45 | } |
| 46 | |
| 47 | static void aes_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, |
| 48 | uint8_t *dst, const uint8_t *src) |
| 49 | { |
Kevin Wolf | bd09594 | 2015-07-17 19:44:10 +0200 | [diff] [blame] | 50 | aes_decrypt(ctx, length, dst, src); |
Radim Krčmář | d3462e3 | 2015-07-10 19:18:01 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, |
| 54 | uint8_t *dst, const uint8_t *src) |
| 55 | { |
| 56 | des_encrypt(ctx, length, dst, src); |
| 57 | } |
| 58 | |
| 59 | static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length, |
| 60 | uint8_t *dst, const uint8_t *src) |
| 61 | { |
| 62 | des_decrypt(ctx, length, dst, src); |
| 63 | } |
| 64 | |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 65 | typedef struct QCryptoCipherNettle QCryptoCipherNettle; |
| 66 | struct QCryptoCipherNettle { |
| 67 | void *ctx_encrypt; |
| 68 | void *ctx_decrypt; |
Radim Krčmář | becaeb7 | 2015-07-10 19:18:00 +0200 | [diff] [blame] | 69 | nettle_cipher_func *alg_encrypt; |
| 70 | nettle_cipher_func *alg_decrypt; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 71 | uint8_t *iv; |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 72 | size_t blocksize; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg) |
| 76 | { |
| 77 | switch (alg) { |
| 78 | case QCRYPTO_CIPHER_ALG_DES_RFB: |
| 79 | case QCRYPTO_CIPHER_ALG_AES_128: |
| 80 | case QCRYPTO_CIPHER_ALG_AES_192: |
| 81 | case QCRYPTO_CIPHER_ALG_AES_256: |
| 82 | return true; |
| 83 | default: |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, |
| 90 | QCryptoCipherMode mode, |
| 91 | const uint8_t *key, size_t nkey, |
| 92 | Error **errp) |
| 93 | { |
| 94 | QCryptoCipher *cipher; |
| 95 | QCryptoCipherNettle *ctx; |
| 96 | uint8_t *rfbkey; |
| 97 | |
| 98 | switch (mode) { |
| 99 | case QCRYPTO_CIPHER_MODE_ECB: |
| 100 | case QCRYPTO_CIPHER_MODE_CBC: |
| 101 | break; |
| 102 | default: |
| 103 | error_setg(errp, "Unsupported cipher mode %d", mode); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) { |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | cipher = g_new0(QCryptoCipher, 1); |
| 112 | cipher->alg = alg; |
| 113 | cipher->mode = mode; |
| 114 | |
| 115 | ctx = g_new0(QCryptoCipherNettle, 1); |
| 116 | |
| 117 | switch (alg) { |
| 118 | case QCRYPTO_CIPHER_ALG_DES_RFB: |
| 119 | ctx->ctx_encrypt = g_new0(struct des_ctx, 1); |
| 120 | ctx->ctx_decrypt = NULL; /* 1 ctx can do both */ |
| 121 | rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey); |
| 122 | des_set_key(ctx->ctx_encrypt, rfbkey); |
| 123 | g_free(rfbkey); |
| 124 | |
Radim Krčmář | d3462e3 | 2015-07-10 19:18:01 +0200 | [diff] [blame] | 125 | ctx->alg_encrypt = des_encrypt_wrapper; |
| 126 | ctx->alg_decrypt = des_decrypt_wrapper; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 127 | |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 128 | ctx->blocksize = DES_BLOCK_SIZE; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 129 | break; |
| 130 | |
| 131 | case QCRYPTO_CIPHER_ALG_AES_128: |
| 132 | case QCRYPTO_CIPHER_ALG_AES_192: |
| 133 | case QCRYPTO_CIPHER_ALG_AES_256: |
| 134 | ctx->ctx_encrypt = g_new0(struct aes_ctx, 1); |
| 135 | ctx->ctx_decrypt = g_new0(struct aes_ctx, 1); |
| 136 | |
| 137 | aes_set_encrypt_key(ctx->ctx_encrypt, nkey, key); |
| 138 | aes_set_decrypt_key(ctx->ctx_decrypt, nkey, key); |
| 139 | |
Radim Krčmář | d3462e3 | 2015-07-10 19:18:01 +0200 | [diff] [blame] | 140 | ctx->alg_encrypt = aes_encrypt_wrapper; |
| 141 | ctx->alg_decrypt = aes_decrypt_wrapper; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 142 | |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 143 | ctx->blocksize = AES_BLOCK_SIZE; |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 144 | break; |
| 145 | default: |
| 146 | error_setg(errp, "Unsupported cipher algorithm %d", alg); |
| 147 | goto error; |
| 148 | } |
| 149 | |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 150 | ctx->iv = g_new0(uint8_t, ctx->blocksize); |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 151 | cipher->opaque = ctx; |
| 152 | |
| 153 | return cipher; |
| 154 | |
| 155 | error: |
| 156 | g_free(cipher); |
| 157 | g_free(ctx); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void qcrypto_cipher_free(QCryptoCipher *cipher) |
| 163 | { |
| 164 | QCryptoCipherNettle *ctx; |
| 165 | |
| 166 | if (!cipher) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | ctx = cipher->opaque; |
| 171 | g_free(ctx->iv); |
| 172 | g_free(ctx->ctx_encrypt); |
| 173 | g_free(ctx->ctx_decrypt); |
| 174 | g_free(ctx); |
| 175 | g_free(cipher); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | int qcrypto_cipher_encrypt(QCryptoCipher *cipher, |
| 180 | const void *in, |
| 181 | void *out, |
| 182 | size_t len, |
| 183 | Error **errp) |
| 184 | { |
| 185 | QCryptoCipherNettle *ctx = cipher->opaque; |
| 186 | |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 187 | if (len % ctx->blocksize) { |
| 188 | error_setg(errp, "Length %zu must be a multiple of block size %zu", |
| 189 | len, ctx->blocksize); |
| 190 | return -1; |
| 191 | } |
| 192 | |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 193 | switch (cipher->mode) { |
| 194 | case QCRYPTO_CIPHER_MODE_ECB: |
| 195 | ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in); |
| 196 | break; |
| 197 | |
| 198 | case QCRYPTO_CIPHER_MODE_CBC: |
| 199 | cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt, |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 200 | ctx->blocksize, ctx->iv, |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 201 | len, out, in); |
| 202 | break; |
| 203 | default: |
| 204 | error_setg(errp, "Unsupported cipher algorithm %d", |
| 205 | cipher->alg); |
| 206 | return -1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | int qcrypto_cipher_decrypt(QCryptoCipher *cipher, |
| 213 | const void *in, |
| 214 | void *out, |
| 215 | size_t len, |
| 216 | Error **errp) |
| 217 | { |
| 218 | QCryptoCipherNettle *ctx = cipher->opaque; |
| 219 | |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 220 | if (len % ctx->blocksize) { |
| 221 | error_setg(errp, "Length %zu must be a multiple of block size %zu", |
| 222 | len, ctx->blocksize); |
| 223 | return -1; |
| 224 | } |
| 225 | |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 226 | switch (cipher->mode) { |
| 227 | case QCRYPTO_CIPHER_MODE_ECB: |
| 228 | ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt, |
| 229 | len, out, in); |
| 230 | break; |
| 231 | |
| 232 | case QCRYPTO_CIPHER_MODE_CBC: |
| 233 | cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt, |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 234 | ctx->alg_decrypt, ctx->blocksize, ctx->iv, |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 235 | len, out, in); |
| 236 | break; |
| 237 | default: |
| 238 | error_setg(errp, "Unsupported cipher algorithm %d", |
| 239 | cipher->alg); |
| 240 | return -1; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int qcrypto_cipher_setiv(QCryptoCipher *cipher, |
| 246 | const uint8_t *iv, size_t niv, |
| 247 | Error **errp) |
| 248 | { |
| 249 | QCryptoCipherNettle *ctx = cipher->opaque; |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 250 | if (niv != ctx->blocksize) { |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 251 | error_setg(errp, "Expected IV size %zu not %zu", |
Daniel P. Berrange | 3a661f1 | 2015-10-16 16:35:06 +0100 | [diff] [blame] | 252 | ctx->blocksize, niv); |
Daniel P. Berrange | ed75474 | 2015-07-01 18:10:34 +0100 | [diff] [blame] | 253 | return -1; |
| 254 | } |
| 255 | memcpy(ctx->iv, iv, niv); |
| 256 | return 0; |
| 257 | } |