-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_tests.c
More file actions
3579 lines (3395 loc) · 151 KB
/
Copy pathfunction_tests.c
File metadata and controls
3579 lines (3395 loc) · 151 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
Copyright 2010-2026 by Omar Alejandro Herrera Reyna
Caume Data Security Engine, also known as CaumeDSE is released under the
GNU General Public License by the Copyright holder, with the additional
exemption that compiling, linking, and/or using OpenSSL is allowed.
LICENSE
This file is part of Caume Data Security Engine, also called CaumeDSE.
CaumeDSE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CaumeDSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CaumeDSE. If not, see <http://www.gnu.org/licenses/>.
INCLUDED SOFTWARE
This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).
This product includes cryptographic software written by
Eric Young (eay@cryptsoft.com).
This product includes software written by
Tim Hudson (tjh@cryptsoft.com).
This product includes software from the SQLite library that is in
the public domain (http://www.sqlite.org/copyright.html).
This product includes software from the GNU Libmicrohttpd project, Copyright
© 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010 , 2011, 2012 Free Software Foundation, Inc.
This product includes software from Perl5, which is Copyright (C) 1993-2005,
by Larry Wall and others.
***/
#include "common.h"
#if CDSE_ENABLE_HERRADURAKEX && HAVE_HERRADURA_H
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#include <herradura.h>
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
#endif
void testContentRows(void);
void testContentColumns(void);
void testDBBrowsing(void);
void testParserTempFiles(void);
void testInternalDBSchemaVersioning(void);
void testSecureDBTamperFixtures(void);
static int cmeDebugTestsNonInteractiveEnabled(void)
{
const char *env = getenv("CDSE_DEBUG_TESTS_NONINTERACTIVE");
return (env && *env && strcmp(env,"0"));
}
static void cmeTestPrintMarker(const char *marker)
{
const char *current;
size_t remaining;
ssize_t written;
if (marker)
{
fflush(stdout);
current=marker;
remaining=strlen(marker);
while (remaining)
{
written=write(STDOUT_FILENO,current,remaining);
if (written<=0)
{
break;
}
current+=written;
remaining-=written;
}
fflush(stdout);
}
}
static int cmeTestSuppressStderrBegin(int *savedStderr)
{
int nullFd;
fflush(stderr);
*savedStderr=dup(STDERR_FILENO);
if (*savedStderr<0)
{
return(1);
}
nullFd=open("/dev/null",O_WRONLY);
if (nullFd<0)
{
close(*savedStderr);
*savedStderr=-1;
return(2);
}
if (dup2(nullFd,STDERR_FILENO)<0)
{
close(nullFd);
close(*savedStderr);
*savedStderr=-1;
return(3);
}
close(nullFd);
return(0);
}
static void cmeTestSuppressStderrEnd(int savedStderr)
{
if (savedStderr>=0)
{
fflush(stderr);
dup2(savedStderr,STDERR_FILENO);
close(savedStderr);
}
}
static char *cmeTestPath(const char *relativePath)
{
char *result = NULL;
cmeStrConstrAppend(&result, "%s%s", cmeDefaultFilePath, relativePath);
return result;
}
static int aes_gcm_encrypt(const unsigned char *plaintext, int plaintext_len,
const unsigned char *aad, int aad_len,
const unsigned char *key, const unsigned char *iv,
int iv_len, unsigned char *ciphertext,
unsigned char *tag)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len = 0, ciphertext_len = 0;
if (!ctx)
return -1;
if (!EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
goto err;
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
goto err;
if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
goto err;
if (aad && aad_len)
if (!EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
goto err;
if (!EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
goto err;
ciphertext_len = len;
if (!EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
goto err;
ciphertext_len += len;
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
goto err;
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
err:
EVP_CIPHER_CTX_free(ctx);
return -1;
}
static int aes_gcm_decrypt(const unsigned char *ciphertext, int ciphertext_len,
const unsigned char *aad, int aad_len,
const unsigned char *tag,
const unsigned char *key, const unsigned char *iv,
int iv_len, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int len = 0, plaintext_len = 0, ret = -1;
if (!ctx)
return -1;
if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
goto err;
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
goto err;
if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
goto err;
if (aad && aad_len)
if (!EVP_DecryptUpdate(ctx, NULL, &len, aad, aad_len))
goto err;
if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
goto err;
plaintext_len = len;
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void *)tag))
goto err;
ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
if (ret > 0)
{
plaintext_len += len;
ret = plaintext_len;
}
else
ret = -1;
err:
EVP_CIPHER_CTX_free(ctx);
return ret;
}
#if CDSE_ENABLE_HERRADURAKEX && HAVE_HERRADURA_H
static void cmeTestHerraduraFillKey(BitArray *key)
{
int cont;
for (cont=0; cont<KEYBYTES; cont++)
{
key->b[cont]=(uint8_t)(0x10+cont*3);
}
}
static void cmeTestHerraduraFillNonce(BitArray *nonce)
{
int cont;
for (cont=0; cont<KEYBYTES; cont++)
{
nonce->b[cont]=(uint8_t)(0xa0-cont*5);
}
}
static void cmeTestHerraduraFillPlaintext(uint8_t *plaintext, size_t len)
{
size_t cont;
for (cont=0; cont<len; cont++)
{
plaintext[cont]=(uint8_t)((cont*7+3)&0xff);
}
}
static int cmeTestHerraduraRoundTripSize(size_t len, int duplex)
{
BitArray key;
BitArray nonce;
const uint8_t aad[]="cdse-independent-hkx-test-aad-v1";
uint8_t *plaintext=NULL;
uint8_t *ciphertext=NULL;
uint8_t *decrypted=NULL;
uint8_t tag[KEYBYTES];
int result=1;
plaintext=(uint8_t *)malloc(len ? len : 1);
ciphertext=(uint8_t *)malloc(len ? len : 1);
decrypted=(uint8_t *)malloc(len ? len : 1);
if (!plaintext || !ciphertext || !decrypted)
{
result=0;
}
else
{
cmeTestHerraduraFillKey(&key);
cmeTestHerraduraFillNonce(&nonce);
cmeTestHerraduraFillPlaintext(plaintext,len);
memset(ciphertext,0,len ? len : 1);
memset(decrypted,0,len ? len : 1);
memset(tag,0,sizeof(tag));
if (duplex)
{
hske_nl_v2_duplex_encrypt(&key,&nonce,aad,sizeof(aad)-1,
plaintext,len,ciphertext,tag);
result=hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,
ciphertext,len,tag,decrypted);
}
else
{
hske_nl_aead_encrypt(&key,&nonce,aad,sizeof(aad)-1,
plaintext,len,ciphertext,tag);
result=hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,
ciphertext,len,tag,decrypted);
}
result=(result && !memcmp(plaintext,decrypted,len));
}
if (plaintext)
{
OPENSSL_cleanse(plaintext,len ? len : 1);
}
if (ciphertext)
{
OPENSSL_cleanse(ciphertext,len ? len : 1);
}
if (decrypted)
{
OPENSSL_cleanse(decrypted,len ? len : 1);
}
OPENSSL_cleanse(tag,sizeof(tag));
cmeFree(plaintext);
cmeFree(ciphertext);
cmeFree(decrypted);
return(result);
}
#endif
void testHerraduraIndependent(void)
{
#if CDSE_ENABLE_HERRADURAKEX && HAVE_HERRADURA_H
static const uint8_t expectedNLA1Ciphertext97[] = {
0x57,0x93,0x3c,0xf5,0x30,0xee,0x9f,0xab,0xbc,0xab,0x39,0xc9,0xe5,0x59,0xf0,0x25,
0xb4,0x40,0xaa,0x54,0x0e,0xae,0x56,0x68,0xfd,0x01,0xd8,0xdc,0xa6,0x33,0xdc,0xf7,
0xe6,0xda,0xeb,0x5b,0x9f,0x10,0x81,0x93,0x28,0x5d,0x71,0x97,0x10,0x45,0xe6,0x6e,
0x5c,0x2c,0x6c,0x3a,0x24,0xd0,0x51,0x18,0x46,0xbd,0x08,0x8c,0xf3,0xa4,0x12,0x8f,
0x97,0xb2,0x58,0x65,0x20,0xc9,0x64,0x25,0x97,0xbe,0x1e,0x67,0x7c,0x32,0x7b,0x1a,
0xe3,0xb7,0x01,0x8a,0x90,0xcd,0xad,0x7f,0x03,0xab,0x69,0xe8,0x5a,0x83,0xfb,0xc4,
0xe6
};
static const uint8_t expectedNLA1Tag97[KEYBYTES] = {
0x6e,0x80,0x58,0xb3,0x8d,0xb2,0xad,0x25,0x68,0x5c,0xfe,0x3f,0xbf,0xcc,0x9d,0xa9,
0xf4,0x04,0xc3,0x65,0x70,0xd4,0x77,0xac,0x8f,0xb1,0x7c,0x16,0x2d,0x1c,0x72,0x90
};
static const uint8_t expectedNLA1Tag0[KEYBYTES] = {
0xaa,0xe5,0x10,0xae,0x49,0x9a,0x6a,0xd7,0xd3,0x7b,0x13,0xb2,0xf8,0x19,0x48,0xeb,
0x99,0x1b,0x2b,0x10,0x43,0xeb,0xc9,0xda,0xd0,0x9b,0x94,0xb7,0x37,0x47,0x95,0xaa
};
static const uint8_t expectedDuplexCiphertext97[] = {
0x4a,0x29,0xab,0x1a,0x92,0x62,0xe8,0xfd,0x7a,0x43,0x28,0xcb,0x27,0xaf,0xec,0x50,
0xd0,0xb2,0x3d,0xae,0x48,0x87,0x53,0xe1,0xf8,0x01,0x86,0xa0,0xa6,0x36,0xd7,0xe8,
0xfb,0x3d,0x5d,0xdd,0x9c,0x44,0xa8,0xf6,0x6f,0xf1,0xee,0x7f,0x61,0x02,0x2c,0xf2,
0x39,0x42,0x50,0x9b,0x69,0x5f,0x38,0xe2,0x07,0xfc,0xf3,0x45,0xbf,0xeb,0xc1,0x3f,
0xc1,0xc5,0x08,0x3f,0xe6,0xc4,0x90,0xb0,0xc1,0xcf,0x20,0xf8,0x7c,0x1b,0xab,0xa7,
0x46,0x3d,0x52,0x53,0x44,0xf0,0xca,0x5b,0x92,0x10,0x6a,0x48,0x73,0x2f,0x76,0x46,
0x5b
};
static const uint8_t expectedDuplexTag97[KEYBYTES] = {
0x7d,0x8d,0x43,0x5a,0x21,0xc9,0xea,0xd0,0xac,0x6f,0x63,0x27,0x26,0x49,0x54,0x54,
0x4e,0xab,0x75,0x60,0xab,0xb6,0x18,0x25,0xa5,0x69,0x32,0xdd,0x5d,0x15,0xee,0x6a
};
static const uint8_t expectedDuplexTag0[KEYBYTES] = {
0xed,0x9b,0x82,0x2d,0xcd,0x7e,0xfa,0xd2,0xaa,0x35,0xa5,0xae,0x90,0xcf,0x0f,0x68,
0x31,0x35,0x3f,0x56,0x28,0x4b,0xbc,0x1c,0xfe,0x36,0x5d,0x40,0x7f,0x0b,0xb8,0x83
};
static const uint8_t expectedHFSCX97[KEYBYTES] = {
0x00,0xd7,0xb4,0xd2,0xc7,0x03,0x91,0xc5,0x5c,0xba,0x49,0x7f,0xac,0xa5,0xa9,0x5a,
0xb7,0x2c,0x87,0x46,0x08,0x9e,0x15,0xbc,0x20,0x3f,0x92,0x21,0xdd,0x04,0xd7,0xd3
};
static const uint8_t expectedHFSCXDS97[KEYBYTES] = {
0xd1,0xa6,0x60,0x9d,0xc1,0xb3,0xf1,0x0e,0xe0,0x0e,0xb9,0xee,0x30,0x7f,0xd2,0x08,
0x59,0x47,0x10,0x63,0xe3,0x8c,0x84,0x6d,0xb7,0x3d,0x50,0xf6,0x15,0x28,0x5c,0xb1
};
static const size_t sizes[] = {0,1,16,32,33,97,4097};
BitArray key;
BitArray nonce;
BitArray wrongKey;
BitArray wrongNonce;
const uint8_t aad[]="cdse-independent-hkx-test-aad-v1";
const uint8_t wrongAad[]="cdse-independent-hkx-test-aad-v2";
uint8_t plaintext[97];
uint8_t ciphertext[97];
uint8_t decrypted[97];
uint8_t tag[KEYBYTES];
uint8_t hash[KEYBYTES];
uint8_t tamperedCiphertext[97];
uint8_t tamperedTag[KEYBYTES];
size_t cont;
int allSizesOk=1;
int negativeOk=1;
cmeTestHerraduraFillKey(&key);
cmeTestHerraduraFillNonce(&nonce);
cmeTestHerraduraFillPlaintext(plaintext,sizeof(plaintext));
hske_nl_aead_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
if (!memcmp(ciphertext,expectedNLA1Ciphertext97,sizeof(expectedNLA1Ciphertext97)) &&
!memcmp(tag,expectedNLA1Tag97,sizeof(expectedNLA1Tag97)) &&
hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!memcmp(plaintext,decrypted,sizeof(plaintext)))
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE-NL-A1 AEAD known-answer vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE-NL-A1 AEAD known-answer vector mismatch.\n");
}
hske_nl_v2_duplex_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
if (!memcmp(ciphertext,expectedDuplexCiphertext97,sizeof(expectedDuplexCiphertext97)) &&
!memcmp(tag,expectedDuplexTag97,sizeof(expectedDuplexTag97)) &&
hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!memcmp(plaintext,decrypted,sizeof(plaintext)))
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE duplex known-answer vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE duplex known-answer vector mismatch.\n");
}
hske_nl_aead_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,0,ciphertext,tag);
if (!memcmp(tag,expectedNLA1Tag0,sizeof(expectedNLA1Tag0)) &&
hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,0,tag,decrypted))
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE-NL-A1 AEAD empty-plaintext vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE-NL-A1 AEAD empty-plaintext vector mismatch.\n");
}
hske_nl_v2_duplex_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,0,ciphertext,tag);
if (!memcmp(tag,expectedDuplexTag0,sizeof(expectedDuplexTag0)) &&
hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,0,tag,decrypted))
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE duplex empty-plaintext vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE duplex empty-plaintext vector mismatch.\n");
}
for (cont=0; cont<(sizeof(sizes)/sizeof(sizes[0])); cont++)
{
allSizesOk=(allSizesOk &&
cmeTestHerraduraRoundTripSize(sizes[cont],0) &&
cmeTestHerraduraRoundTripSize(sizes[cont],1));
}
if (allSizesOk)
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE direct APIs round trip fixed boundary sizes.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE direct APIs boundary-size round trip failed.\n");
}
hske_nl_aead_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
memcpy(tamperedCiphertext,ciphertext,sizeof(tamperedCiphertext));
memcpy(tamperedTag,tag,sizeof(tamperedTag));
wrongKey=key;
wrongNonce=nonce;
wrongKey.b[0]^=0x01;
wrongNonce.b[0]^=0x01;
tamperedCiphertext[0]^=0x01;
tamperedTag[0]^=0x01;
negativeOk=(negativeOk &&
!hske_nl_aead_decrypt(&wrongKey,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_aead_decrypt(&key,&wrongNonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_aead_decrypt(&key,&nonce,wrongAad,sizeof(wrongAad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,tamperedCiphertext,sizeof(tamperedCiphertext),tag,decrypted) &&
!hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tamperedTag,decrypted));
hske_nl_v2_duplex_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
memcpy(tamperedCiphertext,ciphertext,sizeof(tamperedCiphertext));
memcpy(tamperedTag,tag,sizeof(tamperedTag));
tamperedCiphertext[0]^=0x01;
tamperedTag[0]^=0x01;
negativeOk=(negativeOk &&
!hske_nl_v2_duplex_decrypt(&wrongKey,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_v2_duplex_decrypt(&key,&wrongNonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_v2_duplex_decrypt(&key,&nonce,wrongAad,sizeof(wrongAad)-1,ciphertext,sizeof(ciphertext),tag,decrypted) &&
!hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,tamperedCiphertext,sizeof(tamperedCiphertext),tag,decrypted) &&
!hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tamperedTag,decrypted));
hske_nl_aead_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
negativeOk=(negativeOk &&
!hske_nl_v2_duplex_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted));
hske_nl_v2_duplex_encrypt(&key,&nonce,aad,sizeof(aad)-1,plaintext,sizeof(plaintext),ciphertext,tag);
negativeOk=(negativeOk &&
!hske_nl_aead_decrypt(&key,&nonce,aad,sizeof(aad)-1,ciphertext,sizeof(ciphertext),tag,decrypted));
if (negativeOk)
{
printf("TESTS: testHerraduraIndependent(), PASS: HSKE direct APIs reject mutated auth inputs and wrong profile selection.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HSKE direct APIs accepted mutated auth input or wrong profile selection.\n");
}
hfscx_256(plaintext,sizeof(plaintext),NULL,hash);
if (!memcmp(hash,expectedHFSCX97,sizeof(expectedHFSCX97)))
{
printf("TESTS: testHerraduraIndependent(), PASS: HFSCX-256 known-answer vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HFSCX-256 known-answer vector mismatch.\n");
}
hfscx_256_ds(0x03,plaintext,sizeof(plaintext),NULL,hash);
if (!memcmp(hash,expectedHFSCXDS97,sizeof(expectedHFSCXDS97)))
{
printf("TESTS: testHerraduraIndependent(), PASS: HFSCX-256-DS known-answer vector matches.\n");
}
else
{
printf("TESTS: testHerraduraIndependent(), FAIL: HFSCX-256-DS known-answer vector mismatch.\n");
}
#else
printf("TESTS: testHerraduraIndependent(), SKIP: HerraduraKEx provider is not compiled in.\n");
#endif
}
void testCryptoSymmetric(unsigned char *bufIn, unsigned char *bufOut)
{
int cont,cont2,written,ctSize,result __attribute__((unused));
unsigned char password[10]= "Password";
unsigned char cleartext[] = "This is cleartext This is cleartext This is cleartext This is cleartext.\n";
const char *algorithm = cmeOpenSSLLegacyStorageProfile;
unsigned char *key=NULL;
unsigned char *iv=NULL;
unsigned char *expectedKeyIv=NULL;
unsigned char *ciphertext=NULL;
unsigned char *deciphertext=NULL;
unsigned char *salt=NULL;
unsigned char *legacySaltHex=NULL;
unsigned char *legacyDeciphertext=NULL;
FILE *fp=NULL;
EVP_CIPHER_CTX *ctx=NULL;
const EVP_CIPHER *cipher=NULL;
int keyLen=0;
int ivLen=0;
cmeCryptoProfile cryptoProfile;
key=(unsigned char *)malloc(1024);
iv=(unsigned char *)malloc(1024);
ciphertext=(unsigned char *)malloc(1024);
expectedKeyIv=(unsigned char *)malloc(1024);
if (!cmeGetCryptoProfile(&cryptoProfile,cmeDefaultEncAlg) &&
cryptoProfile.implemented && cryptoProfile.allowedAsDefault)
{
printf("TESTS: testCryptoSymmetric(), PASS: default storage crypto profile resolved.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: default storage crypto profile not resolved.\n");
}
if (!cmeGetCryptoProfile(&cryptoProfile,cmeHerraduraKExProfileHSKENLA1AEAD256) &&
cryptoProfile.provider==cmeCryptoProviderHerraduraKEx &&
cryptoProfile.keyLen==32 && cryptoProfile.nonceLen==32 &&
cryptoProfile.tagLen==32 && cryptoProfile.isAEAD &&
cryptoProfile.frameVersion==cmeCryptoFrameHerraduraKExV1 &&
cryptoProfile.implemented==cmeUseHerraduraKEx &&
cryptoProfile.allowedAsDefault==cmeUseHerraduraKEx)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD storage profile metadata resolved.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD storage profile metadata mismatch.\n");
}
if (cmeCryptoAlgorithmIsImplemented(cmeHerraduraKExProfileHSKENLA1AEAD256)==cmeUseHerraduraKEx)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx implementation availability follows build flag.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx implementation availability mismatch.\n");
}
if (!cmeUseHerraduraKEx)
{
unsigned char *hkxCiphertext=NULL;
unsigned char *hkxSalt=NULL;
int hkxWritten=0;
if (cmeCipherByteString(cleartext,&hkxCiphertext,&hkxSalt,strlen((char *)cleartext),
&hkxWritten,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'e'))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx profile rejects encryption when provider is not compiled in.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx profile encrypted without provider support.\n");
}
cmeFree(hkxCiphertext);
cmeFree(hkxSalt);
}
else
{
unsigned char *hkxCiphertext=NULL;
unsigned char *hkxPlaintext=NULL;
unsigned char *hkxSalt=NULL;
int hkxCiphertextLen=0;
int hkxPlaintextLen=0;
int hkxTamperResult=0;
unsigned char *hkxTampered=NULL;
unsigned char *hkxWrongSalt=NULL;
unsigned char *aesCiphertext=NULL;
unsigned char *aesPlaintext=NULL;
unsigned char *aesSalt=NULL;
int aesCiphertextLen=0;
int aesPlaintextLen=0;
if (!cmeCipherByteString(cleartext,&hkxCiphertext,&hkxSalt,strlen((char *)cleartext),
&hkxCiphertextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'e') &&
!cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d') &&
hkxPlaintextLen==(int)strlen((char *)cleartext) &&
!memcmp(hkxPlaintext,cleartext,hkxPlaintextLen))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD profile round trip.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD profile round trip failed.\n");
}
cmeFree(hkxPlaintext);
hkxPlaintext=NULL;
hkxPlaintextLen=0;
if (hkxCiphertext &&
!cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeOpenSSLLegacyStorageProfile,
"Password",'d') &&
hkxPlaintextLen==(int)strlen((char *)cleartext) &&
!memcmp(hkxPlaintext,cleartext,hkxPlaintextLen))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx frame decrypts via embedded profile metadata.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx frame metadata decrypt failed.\n");
}
cmeFree(hkxPlaintext);
hkxPlaintext=NULL;
hkxPlaintextLen=0;
if (hkxCiphertext &&
cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"WrongPassword",'d'))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD rejects wrong key.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD accepted wrong key.\n");
}
cmeFree(hkxPlaintext);
hkxPlaintext=NULL;
hkxPlaintextLen=0;
if (hkxSalt)
{
cmeStrConstrAppend((char **)&hkxWrongSalt,"%s",hkxSalt);
hkxWrongSalt[0]=(hkxWrongSalt[0]=='0') ? '1' : '0';
}
if (hkxCiphertext && hkxWrongSalt &&
cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxWrongSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d'))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD rejects wrong salt.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD accepted wrong salt.\n");
}
cmeFree(hkxPlaintext);
hkxPlaintext=NULL;
hkxPlaintextLen=0;
if (hkxCiphertext && hkxCiphertextLen>cmeHerraduraKExFrameHeaderLen)
{
hkxTampered=(unsigned char *)malloc(hkxCiphertextLen);
memcpy(hkxTampered,hkxCiphertext,hkxCiphertextLen);
hkxTampered[cmeHerraduraKExFrameMagicLen+2+cmeHerraduraKExNonceLen] ^= 0x01;
cmeFree(hkxPlaintext);
hkxTamperResult=cmeCipherByteString(hkxTampered,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d');
if (hkxTamperResult)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD rejects tampered tag.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD accepted tampered tag.\n");
}
cmeFree(hkxTampered);
cmeFree(hkxPlaintext);
hkxTampered=(unsigned char *)malloc(hkxCiphertextLen);
memcpy(hkxTampered,hkxCiphertext,hkxCiphertextLen);
hkxTampered[cmeHerraduraKExFrameMagicLen+2] ^= 0x01;
hkxPlaintext=NULL;
hkxPlaintextLen=0;
hkxTamperResult=cmeCipherByteString(hkxTampered,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d');
if (hkxTamperResult)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD rejects tampered nonce.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD accepted tampered nonce.\n");
}
cmeFree(hkxTampered);
cmeFree(hkxPlaintext);
hkxTampered=(unsigned char *)malloc(hkxCiphertextLen);
memcpy(hkxTampered,hkxCiphertext,hkxCiphertextLen);
hkxTampered[cmeHerraduraKExFrameHeaderLen] ^= 0x01;
hkxPlaintext=NULL;
hkxPlaintextLen=0;
hkxTamperResult=cmeCipherByteString(hkxTampered,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d');
if (hkxTamperResult)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE-NL-A1 AEAD rejects tampered ciphertext.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE-NL-A1 AEAD accepted tampered ciphertext.\n");
}
cmeFree(hkxTampered);
cmeFree(hkxPlaintext);
hkxTampered=(unsigned char *)malloc(hkxCiphertextLen);
memcpy(hkxTampered,hkxCiphertext,hkxCiphertextLen);
hkxTampered[cmeHerraduraKExFrameMagicLen]=0xff;
hkxPlaintext=NULL;
hkxPlaintextLen=0;
hkxTamperResult=cmeCipherByteString(hkxTampered,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d');
if (hkxTamperResult)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx frame rejects unsupported profile id.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx frame accepted unsupported profile id.\n");
}
cmeFree(hkxTampered);
cmeFree(hkxPlaintext);
hkxPlaintext=NULL;
hkxPlaintextLen=0;
hkxTamperResult=cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxSalt,cmeHerraduraKExFrameHeaderLen-1,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d');
if (hkxTamperResult)
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx frame rejects truncated frame.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx frame accepted truncated frame.\n");
}
hkxTampered=NULL;
hkxPlaintext=NULL;
hkxPlaintextLen=0;
}
if (!cmeCipherByteString(cleartext,&aesCiphertext,&aesSalt,strlen((char *)cleartext),
&aesCiphertextLen,cmeOpenSSLLegacyStorageProfile,
"Password",'e') &&
!cmeCipherByteString(aesCiphertext,&aesPlaintext,&aesSalt,aesCiphertextLen,
&aesPlaintextLen,cmeHerraduraKExProfileHSKENLA1AEAD256,
"Password",'d') &&
aesPlaintextLen==(int)strlen((char *)cleartext) &&
!memcmp(aesPlaintext,cleartext,aesPlaintextLen))
{
printf("TESTS: testCryptoSymmetric(), PASS: legacy AES profile decrypts when Herradura is configured.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: legacy AES fallback decrypt failed under Herradura profile.\n");
}
cmeFree(aesCiphertext);
cmeFree(aesPlaintext);
cmeFree(aesSalt);
cmeFree(hkxTampered);
cmeFree(hkxWrongSalt);
cmeFree(hkxCiphertext);
cmeFree(hkxPlaintext);
cmeFree(hkxSalt);
hkxCiphertext=NULL;
hkxPlaintext=NULL;
hkxSalt=NULL;
hkxCiphertextLen=0;
hkxPlaintextLen=0;
if (!cmeCipherByteString(cleartext,&hkxCiphertext,&hkxSalt,strlen((char *)cleartext),
&hkxCiphertextLen,cmeHerraduraKExProfileHSKEDuplex256,
"Password",'e') &&
!cmeCipherByteString(hkxCiphertext,&hkxPlaintext,&hkxSalt,hkxCiphertextLen,
&hkxPlaintextLen,cmeHerraduraKExProfileHSKEDuplex256,
"Password",'d') &&
hkxPlaintextLen==(int)strlen((char *)cleartext) &&
!memcmp(hkxPlaintext,cleartext,hkxPlaintextLen))
{
printf("TESTS: testCryptoSymmetric(), PASS: HerraduraKEx HSKE duplex profile round trip.\n");
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: HerraduraKEx HSKE duplex profile round trip failed.\n");
}
cmeFree(hkxCiphertext);
cmeFree(hkxPlaintext);
cmeFree(hkxSalt);
}
cmeGetCipher(&cipher,algorithm);
keyLen=EVP_CIPHER_key_length(cipher);
ivLen=EVP_CIPHER_iv_length(cipher);
cmePBKDF(cipher,NULL,0,password,8,key,iv);
if (PKCS5_PBKDF2_HMAC((const char *)password,8,NULL,0,cmeDefaultPBKDFCount,
EVP_sha256(),keyLen+ivLen,expectedKeyIv) &&
!memcmp(key,expectedKeyIv,keyLen) && !memcmp(iv,expectedKeyIv+keyLen,ivLen))
{
printf("TESTS: testCryptoSymmetric(), PASS: default PBKDF profile v%d uses HMAC-SHA256 count=%d.\n",
cmeDefaultPBKDFVersion,cmeDefaultPBKDFCount);
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: default PBKDF profile mismatch.\n");
}
cmeCipherInit(&ctx,NULL,cipher,key,iv,'e');
cont2=0;
ctSize=strlen((char*)cleartext);
printf ("---ctSize: %d\n",ctSize);
for (cont=0; cont<(ctSize/evpBufferSize); cont++)
{
memcpy(bufIn,&cleartext[cont2],evpBufferSize);
cmeCipherUpdate(ctx,bufOut,&written,bufIn,evpBufferSize,'e');
memcpy(&ciphertext[cont2],bufOut,written);
cont2 += written;
}
if (ctSize % evpBufferSize)
{
memcpy(bufIn,&cleartext[cont2],ctSize % evpBufferSize);
cmeCipherUpdate(ctx,bufOut,&written,bufIn,ctSize % evpBufferSize,'e');
memcpy(&ciphertext[cont2],bufOut,written);
cont2 += written;
}
cmeCipherFinal(&ctx,bufOut,&written,'e');
memcpy(&ciphertext[cont2],bufOut,written);
cont2 += written;
printf ("---etSize: %d\n",cont2);
{
char *encipheredPath = cmeTestPath("testfiles/enciphered.bin");
fp=fopen(encipheredPath,"wb");
if (fp)
{
result=fwrite(ciphertext,cont2,1,fp);
if (result != 1)
{
printf("---error writing file '%s'\n",encipheredPath);
}
fflush(fp);
fclose(fp);
}
else
{
printf("---error opening file '%s' for writing\n",encipheredPath);
}
cmeFree(encipheredPath);
}
cmeFree(key); //Free stuff
cmeFree(iv);
cmeFree(ciphertext);
/**
Manual Decryption of enciphered.bin using command line openssl works like this (using "Password"):
gentoo64 ~ # openssl enc -d -p -nosalt -des-ede3-cbc -in enciphered.bin -out deciphered.txt
enter des-ede3-cbc decryption password:
key=DC647EB65E6711E155375218212B3964B17C7672C64FEF03
iv =078BE05EDFE25CD0
**/
cmeCipherByteString(cleartext,&ciphertext,&salt,strlen((char *)cleartext),&written,cmeDefaultEncAlg, "Password", 'e');
printf("Generated salt: %s \n",salt);
//cmeFree(salt);
cmeCipherByteString(ciphertext,&deciphertext,&salt,written,&written,cmeDefaultEncAlg, "Password", 'd');
printf("Decrypted text: %s \n",deciphertext);
cmeFree (salt);
cmeFree (ciphertext);
cmeFree (deciphertext);
salt=NULL;
ciphertext=NULL;
deciphertext=NULL;
{
unsigned char legacySaltBytes[evpSaltBufferSize];
unsigned char legacyKeyIv[128];
unsigned char legacyCiphertext[128];
unsigned char legacyCombined[144];
unsigned char legacyTag[cmeGCMTagLen];
const unsigned char legacyPlaintext[]="Legacy PBKDF2 fallback text.";
int legacyCiphertextLen=0;
int legacyCombinedLen=0;
int legacyWritten=0;
memset(legacySaltBytes,0,sizeof(legacySaltBytes));
memset(legacyKeyIv,0,sizeof(legacyKeyIv));
memset(legacyCiphertext,0,sizeof(legacyCiphertext));
memset(legacyCombined,0,sizeof(legacyCombined));
memset(legacyTag,0,sizeof(legacyTag));
for (cont=0;cont<evpSaltBufferSize;cont++)
{
legacySaltBytes[cont]=(unsigned char)cont;
}
cmeBytesToHexstr(legacySaltBytes,&legacySaltHex,evpSaltBufferSize);
if (PKCS5_PBKDF2_HMAC_SHA1((const char *)password,8,legacySaltBytes,evpSaltBufferSize,
cmeLegacyPBKDFCount,keyLen+ivLen,legacyKeyIv))
{
legacyCiphertextLen=aes_gcm_encrypt(legacyPlaintext,strlen((const char *)legacyPlaintext),
NULL,0,legacyKeyIv,legacyKeyIv+keyLen,ivLen,
legacyCiphertext,legacyTag);
}
if (legacyCiphertextLen>0)
{
memcpy(legacyCombined,legacyCiphertext,legacyCiphertextLen);
memcpy(legacyCombined+legacyCiphertextLen,legacyTag,cmeGCMTagLen);
legacyCombinedLen=legacyCiphertextLen+cmeGCMTagLen;
}
if (legacyCombinedLen>0 &&
!cmeCipherByteString(legacyCombined,&legacyDeciphertext,&legacySaltHex,legacyCombinedLen,
&legacyWritten,cmeDefaultEncAlg,(const char *)password,'d') &&
legacyWritten==(int)strlen((const char *)legacyPlaintext) &&
!memcmp(legacyDeciphertext,legacyPlaintext,legacyWritten))
{
printf("TESTS: testCryptoSymmetric(), PASS: legacy PBKDF profile v%d decrypt fallback preserved old data.\n",
cmeLegacyPBKDFVersion);
}
else
{
printf("TESTS: testCryptoSymmetric(), FAIL: legacy PBKDF decrypt fallback failed.\n");
}
OPENSSL_cleanse(legacyKeyIv,sizeof(legacyKeyIv));
cmeFree(legacySaltHex);
if (legacyDeciphertext)
{
OPENSSL_cleanse(legacyDeciphertext,legacyWritten);
cmeFree(legacyDeciphertext);
legacyDeciphertext=NULL;
}
}
cmeProtectByteString((const char*)cleartext,(char **)&ciphertext,cmeDefaultEncAlg,(char **)&salt,"Password",&written,strlen((char *)cleartext));
cmeUnprotectByteString((const char *)ciphertext,(char **)&deciphertext,cmeDefaultEncAlg,(char **)&salt,"Password",&written,written);
printf("Unprotected text: %s \n",deciphertext);
cmeFree (salt);
cmeFree (ciphertext);
cmeFree (deciphertext);
cmeFree(expectedKeyIv);
}
void testCryptoReprotectDBValue(void)
{
const char *value="CaumeDSE explicit re-protect test value.";
const char *oldKey="OldRotationPassword";
const char *newKey="NewRotationPassword";
char *sourceSalt=NULL;
char *rotatedSalt=NULL;
char *dryRunSalt=NULL;
char *protectedValue=NULL;
char *rotatedValue=NULL;
char *readbackValue=NULL;
char *wrongKeyValue=NULL;
int protectedValueLen=0;
int rotatedValueLen=0;
int readbackValueLen=0;
int dryRunValueLen=0;
if (!cmeProtectDBSaltedValue(value,&protectedValue,cmeOpenSSLLegacyStorageProfile,
&sourceSalt,oldKey,&protectedValueLen) &&
!cmeReprotectDBSaltedValue(protectedValue,&rotatedValue,cmeOpenSSLLegacyStorageProfile,
cmeOpenSSLLegacyStorageProfile,&sourceSalt,&rotatedSalt,
oldKey,newKey,&rotatedValueLen,0) &&
rotatedSalt && strcmp(sourceSalt,rotatedSalt) &&
!cmeUnprotectDBSaltedValue(rotatedValue,&readbackValue,cmeOpenSSLLegacyStorageProfile,
&rotatedSalt,newKey,&readbackValueLen) &&
readbackValueLen==(int)strlen(value) && !strcmp(value,readbackValue))
{
printf("TESTS: testCryptoReprotectDBValue(), PASS: DB value re-protect rotates key with AES profile.\n");
}
else
{
printf("TESTS: testCryptoReprotectDBValue(), FAIL: DB value AES key rotation failed.\n");
}
cmeFree(readbackValue);
readbackValue=NULL;
readbackValueLen=0;
if (!cmeReprotectDBSaltedValue(protectedValue,NULL,cmeOpenSSLLegacyStorageProfile,
cmeOpenSSLLegacyStorageProfile,&sourceSalt,&dryRunSalt,
oldKey,newKey,&dryRunValueLen,1) &&
dryRunValueLen==(int)strlen(value) && dryRunSalt==NULL)
{
printf("TESTS: testCryptoReprotectDBValue(), PASS: DB value dry-run re-protect reports plaintext length without writing.\n");
}
else
{
printf("TESTS: testCryptoReprotectDBValue(), FAIL: DB value dry-run re-protect failed.\n");
}
fflush(stdout);
if (cmeReprotectDBSaltedValue(protectedValue,&wrongKeyValue,cmeOpenSSLLegacyStorageProfile,
cmeOpenSSLLegacyStorageProfile,&sourceSalt,&dryRunSalt,
"WrongRotationPassword",newKey,&rotatedValueLen,0))
{
printf("TESTS: testCryptoReprotectDBValue(), PASS: DB value re-protect rejects wrong source key.\n");
}
else
{
printf("TESTS: testCryptoReprotectDBValue(), FAIL: DB value re-protect accepted wrong source key.\n");
}
cmeFree(wrongKeyValue);
wrongKeyValue=NULL;