VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Volume/VolumePasswordCache.cpp
AgeCommit message (Expand)AuthorFilesLines
2016-05-10Remove trailing whitespaceDavid Foerster1-1/+1
2016-01-20Copyright: update dates to include 2016.Mounir IDRASSI1-1/+1
2015-08-06Update license information to reflect the use of a dual license Apache 2.0 an...Mounir IDRASSI1-5/+9
2014-11-08Change namespace from TrueCrypt to VeraCrypt. Rename method from Resources Re...Mounir IDRASSI1-1/+1
2014-11-08Add TrueCrypt 7.1a MacOSX/Linux specific source files.Mounir IDRASSI1-0/+43
3'>73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/* See src/Crypto/wolfCrypt.md */

#include "Aes.h"
#include "Sha2.h"
#include "../Common/Crypto.h"
#include <wolfssl/wolfcrypt/hmac.h>


AES_RETURN aes_init()
{
#if defined( AES_ERR_CHK )
    return EXIT_SUCCESS;
#else
    return;
#endif
}

AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
{
    int ret = 0;

    ret = wc_AesInit(&cx->wc_enc_aes, NULL, INVALID_DEVID);

    if (key_len == 128 || key_len == 192 || key_len == 256)
        key_len = key_len/8;

    if (ret == 0) {
        ret = wc_AesSetKey(&cx->wc_enc_aes, key, key_len, NULL, AES_ENCRYPTION);
    }

#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif
}

AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
{
    int ret = 0;

    ret = wc_AesInit(&cx->wc_dec_aes, NULL, INVALID_DEVID);

    if (key_len == 128 || key_len == 192 || key_len == 256)
        key_len = key_len/8;

    if (ret == 0) {
        ret = wc_AesSetKey(&cx->wc_dec_aes, key, key_len, NULL, AES_DECRYPTION);
    }

#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif
}

AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
{
    return aes_encrypt_key(key, 128, cx);
}

AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
{
    return aes_encrypt_key(key, 192, cx);
}

AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
{
    return aes_encrypt_key(key, 256, cx);
}

AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
{
    return aes_decrypt_key(key, 128, cx);
}

AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
{
    return aes_decrypt_key(key, 192, cx);
}

AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
{
    return aes_decrypt_key(key, 256, cx);
}

AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
{
    int ret = wc_AesEncryptDirect(&cx->wc_enc_aes, out, in);
#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif

}

AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
{
    int ret = wc_AesDecryptDirect(&cx->wc_dec_aes, out, in);
#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif

}

AES_RETURN xts_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
{
    int ret = 0;

    cx->wc_enc_xts.aes = cx->wc_enc_aes;

    ret = wc_AesInit(&cx->wc_enc_xts.tweak, NULL, INVALID_DEVID);

    if (key_len == 128 || key_len == 192 || key_len == 256)
        key_len = key_len/8;

    if (ret == 0) {
        ret = wc_AesSetKey(&cx->wc_enc_xts.tweak, key, key_len, NULL, AES_ENCRYPTION);
    }
#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif
}

AES_RETURN xts_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
{
    int ret = 0;

    cx->wc_dec_xts.aes = cx->wc_dec_aes;

    ret = wc_AesInit(&cx->wc_dec_xts.tweak, NULL, INVALID_DEVID);

    if (key_len == 128 || key_len == 192 || key_len == 256)
        key_len = key_len/8;

    if (ret == 0) {
        ret = wc_AesSetKey(&cx->wc_dec_xts.tweak, key, key_len, NULL, AES_ENCRYPTION);
    }

#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif
}

AES_RETURN xts_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
{
    return xts_encrypt_key(key, 256, cx);
}

AES_RETURN xts_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
{
    return xts_decrypt_key(key, 256, cx);
}

AES_RETURN xts_encrypt(const unsigned char *in, unsigned char *out, word64 length, word64 sector, const aes_encrypt_ctx cx[1])
{
    int ret = wc_AesXtsEncryptConsecutiveSectors(&cx->wc_enc_xts, out, in, length, sector, ENCRYPTION_DATA_UNIT_SIZE);

#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif

}

AES_RETURN xts_decrypt(const unsigned char *in, unsigned char *out, word64 length, word64 sector, const aes_decrypt_ctx cx[1])
{
    int ret = wc_AesXtsDecryptConsecutiveSectors(&cx->wc_dec_xts, out, in, length, sector, ENCRYPTION_DATA_UNIT_SIZE);

#if defined( AES_ERR_CHK )
    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
#else
    return;
#endif
}


void sha256_begin(sha256_ctx* ctx)
{
    wc_InitSha256(ctx);
}

void sha256_hash(const unsigned char * source, uint_32t sourceLen, sha256_ctx *ctx)
{
    wc_Sha256Update(ctx, source, sourceLen);
}

void sha256_end(unsigned char * result, sha256_ctx* ctx)
{
    wc_Sha256Final(ctx, result);
}

void sha256(unsigned char * result, const unsigned char* source, uint_32t sourceLen)
{
    wc_Sha256 sha256;
    wc_InitSha256(&sha256);
    wc_Sha256Update(&sha256, source, sourceLen);
    wc_Sha256Final(&sha256, result);
    wc_Sha256Free(&sha256);
}

void sha512_begin(sha512_ctx* ctx)
{
    wc_InitSha512(ctx);
}

void sha512_hash(const unsigned char * source, uint_64t sourceLen, sha512_ctx *ctx)
{
    wc_Sha512Update(ctx, source, sourceLen);
}

void sha512_end(unsigned char * result, sha512_ctx* ctx)
{
    wc_Sha512Final(ctx, result);
}

void sha512(unsigned char * result, const unsigned char* source, uint_64t sourceLen)
{
    wc_Sha512 sha512;
    wc_InitSha512(&sha512);
    wc_Sha512Update(&sha512, source, sourceLen);
    wc_Sha512Final(&sha512, result);
    wc_Sha512Free(&sha512);
}

void derive_key_sha512 (char *pwd, int pwd_len, char *salt, int salt_len, uint32 iterations, char *dk, int dklen) {
    (void) iterations;
    wc_HKDF(WC_SHA512, (byte*)pwd, (word32)pwd_len, (byte*)salt, (word32)salt_len, NULL, 0, (byte*)dk, (word32)dklen);
}

void derive_key_sha256 (char *pwd, int pwd_len, char *salt, int salt_len, uint32 iterations, char *dk, int dklen) {
    (void) iterations;
    wc_HKDF(WC_SHA256, (byte*)pwd, (word32)pwd_len, (byte*)salt, (word32)salt_len, NULL, 0, (byte*)dk, (word32)dklen);
}