File: AESStringCrypt.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (543 lines) | stat: -rw-r--r-- 14,457 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * AESStringCrypt.c
 *
 * AES String Crypt 1.1
 * Copyright (C) 2007, 2008, 2009, 2012, 2015
 *
 * Author: Paul E. Jones <paulej@packetizer.com>
 *
 * This library will encrypt octet strings of the specified length up
 * to ULLONG_MAX - 70 octet in length.  If there is an error, the return
 * value from the encryption or decryption function will be
 * AESSTRINGCRYPT_ERROR.  Any other value, including zero, is a valid
 * length value.  Note that an encrypted string can be up to 69 octets
 * longer than the original plaintext string, thus the restriction on the
 * input string size.
 *
 * The output of the string encryption function is a string that is
 * compliant with the AES Crypt version 0 file format.  For reference,
 * see: https://www.aescrypt.com/aes_file_format.html.
 *
 * This software is licensed as "freeware."  Permission to distribute
 * this software in source and binary forms is hereby granted without a
 * fee.  THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING FROM
 * THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING,
 * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE.
 */

#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#include <wincrypt.h>
#else
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#endif

#include "AESStringCrypt.h"

/*
 *  AESStringCrypt
 *
 *  Description
 *      This function is called to encrypt the string "plaintext".
 *      The encrypted string is placed in "ciphertext".  Note that
 *      the encrypted string is up to 68 bytes larger than the
 *      plaintext string.  This is to accomodate the header defined
 *      by "AES Crypt File Format 0" and to store the last cipher
 *      block (which is padded to 16 octets).
 *
 *  Parameters
 *      password [in]
 *          The password used to encrypt the string in UCS-16
 *          format.
 *      password_length [in]
 *          The length of the password in octets
 *      plaintext [in]
 *          The plaintext string to be encrypted
 *      plaintext_length [in]
 *          The length of the plaintext string
 *      ciphertext [out]
 *              The encrypted string
 *
 *  Returns
 *      Returns the length of the ciphertext string or AESSTRINGCRYPT_ERROR
 *      if there was an error when trying to encrypt the string.
 */
unsigned long long AESStringCrypt(unsigned char *password,
                                  unsigned long password_length,
                                  unsigned char *plaintext,
                                  unsigned long long plaintext_length,
                                  unsigned char *ciphertext)
{
    aes_context         aes_ctx;
    sha256_context      sha_ctx;
    sha256_t            digest;
    unsigned char       IV[16];
    int                 i, n;
    unsigned char       buffer[32];
    unsigned char       ipad[64], opad[64];
#ifdef _WIN32
    HCRYPTPROV          hProv;
    DWORD               result_code;
#else
    time_t              current_time;
    pid_t               process_id;
    FILE                *randfp = NULL;
#endif
    unsigned char       *p;

    /*
     * Write an AES signature at the head of the file, along
     * with the AES file format version number.
     */
    ciphertext[0] = 'A';
    ciphertext[1] = 'E';
    ciphertext[2] = 'S';
    ciphertext[3] = 0x00;   /* Version 0                    */
    ciphertext[4] = (plaintext_length & 0x0F);

    /*
     * We will use p as the pointer into the cipher
     */
    p = ciphertext + 5;

#ifdef _WIN32

    /*
     * Prepare for random number generation
     */
    if (!CryptAcquireContext(&hProv,
                             NULL,
                             NULL,
                             PROV_RSA_FULL,
                             CRYPT_VERIFYCONTEXT))
    {
        result_code = GetLastError();

        if (GetLastError() == NTE_BAD_KEYSET)
        {
            if (!CryptAcquireContext(&hProv,
                                     NULL,
                                     NULL,
                                     PROV_RSA_FULL,
                                     CRYPT_NEWKEYSET | CRYPT_VERIFYCONTEXT))
            {
                result_code = GetLastError();
            }
            else
            {
                result_code = ERROR_SUCCESS;
            }
        }

        if (result_code != ERROR_SUCCESS)
        {
            return AESSTRINGCRYPT_ERROR;
        }
    }

    /*
     * Create the 16-bit IV used for encrypting the plaintext.
     * We do not fully trust the system's randomization functions,
     * so we improve on that by also hashing the random octets
     * and using only a portion of the hash.  This IV
     * generation could be replaced with any good random
     * source of data.
     */
    memset(IV, 0, 16);
    memset(buffer, 0, 32);

    sha256_starts(&sha_ctx);

    for (i = 0; i < 256; i++)
    {
        if (!CryptGenRandom(hProv,
                            32,
                            (BYTE *) buffer))
        {
            CryptReleaseContext(hProv, 0);
            return AESSTRINGCRYPT_ERROR;
        }

        sha256_update(&sha_ctx, buffer, 32);
    }

    sha256_finish(&sha_ctx, digest);

    /*
     * We're finished collecting random data
     */
    CryptReleaseContext(hProv, 0);

    /*
     * Get the IV from the digest buffer
     */
    memcpy(IV, digest, 16);

#else

    /*
     * Open the source for random data.  Note that while the entropy
     * might be lower with /dev/urandom than /dev/random, it will not
     * fail to produce something.  Also, we're going to hash the result
     * anyway.
     */
    if ((randfp = fopen("/dev/urandom", "r")) == NULL)
    {
        return AESSTRINGCRYPT_ERROR;
    }

    /*
     * We will use an initialization vector comprised of the current time
     * process ID, and random data, all hashed together with SHA-256.
     */
    current_time = time(NULL);

    for (i = 0; i < 8; i++)
    {
        buffer[i] = (unsigned char)
                    (current_time >> (i * 8));
    }

    process_id = getpid();

    for (i = 0; i < 8; i++)
    {
        buffer[i + 8] = (unsigned char)
                        (process_id >> (i * 8));
    }

    sha256_starts(&sha_ctx);
    sha256_update(&sha_ctx, buffer, 16);

    for (i = 0; i < 256; i++)
    {
        if (fread(buffer, 1, 32, randfp) != 32)
        {
            return AESSTRINGCRYPT_ERROR;
        }

        sha256_update(&sha_ctx,
                      buffer,
                      32);
    }

    sha256_finish(&sha_ctx, digest);

    /*
     * We're finished collecting random data
     */
    fclose(randfp);

    /*
     * Get the IV from the digest buffer
     */
    memcpy(IV, digest, 16);
#endif

    /*
     * Copy the IV to the ciphertext string
     */
    memcpy(p, IV, 16);
    p += 16;

    /*
     * Hash the IV and password 8192 times
     */
    memset(digest, 0, 32);
    memcpy(digest, IV, 16);

    for (i = 0; i < 8192; i++)
    {
        sha256_starts(&sha_ctx);
        sha256_update(&sha_ctx, digest, 32);
        sha256_update(&sha_ctx,
                      password,
                      password_length);
        sha256_finish(&sha_ctx,
                      digest);
    }

    /*
     * Set the AES encryption key
     */
    aes_set_key(&aes_ctx, digest, 256);

    /*
     * Set the ipad and opad arrays with values as
     * per RFC 2104 (HMAC).  HMAC is defined as
     *   H(K XOR opad, H(K XOR ipad, text))
     */
    memset(ipad, 0x36, 64);
    memset(opad, 0x5C, 64);

    for (i = 0; i < 32; i++)
    {
        ipad[i] ^= digest[i];
        opad[i] ^= digest[i];
    }

    sha256_starts(&sha_ctx);
    sha256_update(&sha_ctx, ipad, 64);

    while (plaintext_length > 0)
    {
        /*
         * Grab the next block of plaintext
         */
        if (plaintext_length >= 16)
        {
            n = 16;
        }
        else
        {
            n = (int) plaintext_length;
        }

        plaintext_length -= n;

        memcpy(buffer, plaintext, n);
        plaintext += n;

        /*
         * XOR plain text block with previous encrypted
         * output (i.e., use CBC)
         */
        for (i = 0; i < 16; i++)
        {
            buffer[i] ^= IV[i];
        }

        /*
         * Encrypt the contents of the buffer
         */
        aes_encrypt(&aes_ctx, buffer, buffer);

        /*
         * Concatenate the "text" as we compute the HMAC
         */
        sha256_update(&sha_ctx, buffer, 16);

        /*
         * Write the encrypted block
         */
        memcpy(p, buffer, 16);
        p += 16;

        /*
         * Update the IV (CBC mode)
         */
        memcpy(IV, buffer, 16);
    }

    /*
     * Write the HMAC
     */
    sha256_finish(&sha_ctx, digest);
    sha256_starts(&sha_ctx);
    sha256_update(&sha_ctx, opad, 64);
    sha256_update(&sha_ctx, digest, 32);
    sha256_finish(&sha_ctx, digest);
    memcpy(p, digest, 32);
    p += 32;

    return (p - ciphertext);
}

/*
 *  AESStringDecrypt
 *
 *  Description
 *      This function is called to decrypt the string "ciphertext".
 *      The decrypted string is placed in "plaintext".
 *
 *  Parameters
 *      password [in]
 *          The password used to encrypt the string in UCS-16
 *          format.
 *      password_length [in]
 *          The length of the password in octets
 *      ciphertext [in]
 *          The ciphertext string to be decrypted
 *      ciphertext_length [in]
 *          The length of the ciphertext string
 *      plaintext [out]
 *          The decrypted string
 *
 *  Returns
 *      Returns the length of the plaintext string or AESSTRINGCRYPT_ERROR
 *      if there was an error when trying to encrypt the string.
 */
unsigned long long AESStringDecrypt(unsigned char *password,
                                    unsigned long password_length,
                                    unsigned char *ciphertext,
                                    unsigned long long ciphertext_length,
                                    unsigned char *plaintext)
{
    aes_context                 aes_ctx;
    sha256_context              sha_ctx;
    sha256_t                    digest;
    unsigned char               IV[16];
    int                         i, n;
    unsigned char               buffer[64], buffer2[32];
    unsigned char               ipad[64], opad[64];
    unsigned char               *p;
    int                         final_block_size;

    /*
     * Encrypted strings will be at least 53 octets in length
     * and the rest must be a multiple of 16 octets
     */
    if (ciphertext_length < 53)
    {
        return AESSTRINGCRYPT_ERROR;
    }

    if (!(ciphertext[0] == 'A' && ciphertext[1] == 'E' &&
            ciphertext[2] == 'S'))
    {
        return AESSTRINGCRYPT_ERROR;
    }

    /*
     * Validate the version number and take any version-specific actions
     */
    if (ciphertext[3] > 0)
    {
        return AESSTRINGCRYPT_ERROR;
    }

    /*
     * Take note of the final block size
     */
    final_block_size = ciphertext[4];

    /*
     * Move pointers and count beyond header
     */
    ciphertext += 5;
    ciphertext_length -= 5;

    /*
     * We will use p to write into the plaintext buffer
     */
    p = plaintext;

    /*
     * Read the initialization vector
     */
    memcpy(IV, ciphertext, 16);
    ciphertext += 16;
    ciphertext_length -= 16;

    /*
     * Hash the IV and password 8192 times
     */
    memset(digest, 0, 32);
    memcpy(digest, IV, 16);

    for (i = 0; i < 8192; i++)
    {
        sha256_starts(&sha_ctx);
        sha256_update(&sha_ctx, digest, 32);
        sha256_update(&sha_ctx,
                      password,
                      password_length);
        sha256_finish(&sha_ctx,
                      digest);
    }

    /*
     * Set the AES encryption key
     */
    aes_set_key(&aes_ctx, digest, 256);

    /*
     * Set the ipad and opad arrays with values as
     * per RFC 2104 (HMAC).  HMAC is defined as
     *   H(K XOR opad, H(K XOR ipad, text))
     */
    memset(ipad, 0x36, 64);
    memset(opad, 0x5C, 64);

    for (i = 0; i < 32; i++)
    {
        ipad[i] ^= digest[i];
        opad[i] ^= digest[i];
    }

    sha256_starts(&sha_ctx);
    sha256_update(&sha_ctx, ipad, 64);

    while (ciphertext_length > 32)
    {
        memcpy(buffer, ciphertext, 16);
        memcpy(buffer2, ciphertext, 16);
        ciphertext += 16;
        ciphertext_length -= 16;

        sha256_update(&sha_ctx, buffer, 16);
        aes_decrypt(&aes_ctx, buffer, buffer);

        /*
         * XOR plain text block with previous encrypted output (i.e., use CBC)
         */
        for (i = 0; i < 16; i++)
        {
            buffer[i] ^= IV[i];
        }

        /*
         * Update the IV (CBC mode)
         */
        memcpy(IV, buffer2, 16);

        /*
         * If this is the final block, then we may
         * write less than 16 octets
         */
        if ((ciphertext_length > 32) || (!final_block_size))
        {
            n = 16;
        }
        else
        {
            n = final_block_size;
        }

        /*
         * Write the decrypted block
         */
        memcpy(p, buffer, n);
        p += n;
    }

    /*
     * Verify that the HMAC is correct
     */
    if (ciphertext_length != 32)
    {
        return AESSTRINGCRYPT_ERROR;
    }

    sha256_finish(&sha_ctx, digest);
    sha256_starts(&sha_ctx);
    sha256_update(&sha_ctx, opad, 64);
    sha256_update(&sha_ctx, digest, 32);
    sha256_finish(&sha_ctx, digest);

    if (memcmp(digest, ciphertext, 32))
    {
        return AESSTRINGCRYPT_ERROR;
    }

    return (p - plaintext);
}