Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2010, XenSource Inc.
3 : : * All rights reserved.
4 : : *
5 : : * Redistribution and use in source and binary forms, with or without
6 : : * modification, are permitted provided that the following conditions are met:
7 : : * * Redistributions of source code must retain the above copyright
8 : : * notice, this list of conditions and the following disclaimer.
9 : : * * Redistributions in binary form must reproduce the above copyright
10 : : * notice, this list of conditions and the following disclaimer in the
11 : : * documentation and/or other materials provided with the distribution.
12 : : * * Neither the name of XenSource Inc. nor the names of its contributors
13 : : * may be used to endorse or promote products derived from this software
14 : : * without specific prior written permission.
15 : : *
16 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : : */
28 : :
29 : : /*
30 : : * Copyright (c) 2014 Citrix Systems, Inc.
31 : : */
32 : :
33 : :
34 : : #include <err.h>
35 : : #include <stdio.h>
36 : : #include <stdint.h>
37 : : #include <string.h>
38 : : #include "compat-crypto-openssl.h"
39 : : #include "xts_aes.h"
40 : :
41 : 0 : struct crypto_blkcipher * xts_aes_setup(void)
42 : : {
43 : : struct crypto_blkcipher *ret;
44 : :
45 : 0 : ret = calloc(1, sizeof(struct crypto_blkcipher));
46 : 0 : if (!ret)
47 : : return NULL;
48 : 0 : return ret;
49 : : }
50 : :
51 : 0 : int xts_aes_setkey(struct crypto_blkcipher *cipher, const uint8_t *key, unsigned int keysize)
52 : : {
53 : : const EVP_CIPHER *type;
54 : : int err;
55 : :
56 : 0 : switch (keysize) {
57 : 0 : case 64: type = EVP_aes_256_xts(); break;
58 : 0 : case 32: type = EVP_aes_128_xts(); break;
59 : : default: return -21; break;
60 : : }
61 : :
62 : 0 : if (!type)
63 : : return -20;
64 : :
65 : 0 : cipher->en_ctx = EVP_CIPHER_CTX_new();
66 : 0 : cipher->de_ctx = EVP_CIPHER_CTX_new();
67 : :
68 : : /* TODO lazily initialize the encrypt context until doing an encryption,
69 : : * since it's only needed for a writable node (top diff) */
70 : 0 : if (!EVP_CipherInit_ex(cipher->en_ctx, type, NULL, NULL, NULL, 1)) {
71 : : err = -1;
72 : : goto cleanup;
73 : : }
74 : 0 : if (!EVP_CipherInit_ex(cipher->de_ctx, type, NULL, NULL, NULL, 0)) {
75 : : err = -2;
76 : : goto cleanup;
77 : : }
78 : 0 : if (!EVP_CIPHER_CTX_set_key_length(cipher->en_ctx, keysize)) {
79 : : err = -3;
80 : : goto cleanup;
81 : : }
82 : 0 : if (!EVP_CipherInit_ex(cipher->en_ctx, NULL, NULL, key, NULL, 1)) {
83 : : err = -4;
84 : : goto cleanup;
85 : : }
86 : 0 : if (!EVP_CIPHER_CTX_set_key_length(cipher->de_ctx, keysize)) {
87 : : err = -5;
88 : : goto cleanup;
89 : : }
90 : 0 : if (!EVP_CipherInit_ex(cipher->de_ctx, NULL, NULL, key, NULL, 0)) {
91 : : err = -6;
92 : : goto cleanup;
93 : : }
94 : :
95 : : return 0;
96 : :
97 : : cleanup:
98 : 0 : EVP_CIPHER_CTX_free(cipher->en_ctx);
99 : 0 : EVP_CIPHER_CTX_free(cipher->de_ctx);
100 : 0 : return err;
101 : : }
|