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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2016 Mirantis <akupczyk@mirantis.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <iostream>
#include "global/global_init.h"
#include "common/ceph_argparse.h"
#include "rgw_common.h"
#include "rgw_rados.h"
#include "rgw_crypt.h"
#include <gtest/gtest.h>
#include "include/ceph_assert.h"
#define dout_subsys ceph_subsys_rgw
using namespace std;
std::unique_ptr<BlockCrypt> AES_256_CBC_create(const DoutPrefixProvider *dpp, CephContext* cct, const uint8_t* key, size_t len);
class ut_get_sink : public RGWGetObj_Filter {
std::stringstream sink;
public:
ut_get_sink() {}
virtual ~ut_get_sink() {}
int handle_data(bufferlist& bl, off_t bl_ofs, off_t bl_len) override
{
sink << std::string_view(bl.c_str()+bl_ofs, bl_len);
return 0;
}
std::string get_sink()
{
return sink.str();
}
};
class ut_put_sink: public rgw::sal::DataProcessor
{
std::stringstream sink;
public:
int process(bufferlist&& bl, uint64_t ofs) override
{
sink << std::string_view(bl.c_str(),bl.length());
return 0;
}
std::string get_sink()
{
return sink.str();
}
};
class BlockCryptNone: public BlockCrypt {
size_t block_size = 256;
public:
BlockCryptNone(){};
BlockCryptNone(size_t sz) : block_size(sz) {}
virtual ~BlockCryptNone(){};
size_t get_block_size() override
{
return block_size;
}
bool encrypt(bufferlist& input,
off_t in_ofs,
size_t size,
bufferlist& output,
off_t stream_offset) override
{
output.clear();
output.append(input.c_str(), input.length());
return true;
}
bool decrypt(bufferlist& input,
off_t in_ofs,
size_t size,
bufferlist& output,
off_t stream_offset) override
{
output.clear();
output.append(input.c_str(), input.length());
return true;
}
};
TEST(TestRGWCrypto, verify_AES_256_CBC_identity)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
buffer::ptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
for (unsigned int step : {1, 2, 3, 5, 7, 11, 13, 17})
{
//make some random key
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i*step;
auto aes(AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
ASSERT_NE(aes.get(), nullptr);
size_t block_size = aes->get_block_size();
ASSERT_NE(block_size, 0u);
for (size_t r = 97; r < 123 ; r++)
{
off_t begin = (r*r*r*r*r % test_range);
begin = begin - begin % block_size;
off_t end = begin + r*r*r*r*r*r*r % (test_range - begin);
if (r % 3)
end = end - end % block_size;
off_t offset = r*r*r*r*r*r*r*r % (1000*1000*1000);
offset = offset - offset % block_size;
ASSERT_EQ(begin % block_size, 0u);
ASSERT_LE(end, test_range);
ASSERT_EQ(offset % block_size, 0u);
bufferlist encrypted;
ASSERT_TRUE(aes->encrypt(input, begin, end - begin, encrypted, offset));
bufferlist decrypted;
ASSERT_TRUE(aes->decrypt(encrypted, 0, end - begin, decrypted, offset));
ASSERT_EQ(decrypted.length(), end - begin);
ASSERT_EQ(std::string_view(input.c_str() + begin, end - begin),
std::string_view(decrypted.c_str(), end - begin) );
}
}
}
TEST(TestRGWCrypto, verify_AES_256_CBC_identity_2)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
buffer::ptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
for (unsigned int step : {1, 2, 3, 5, 7, 11, 13, 17})
{
//make some random key
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i*step;
auto aes(AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
ASSERT_NE(aes.get(), nullptr);
size_t block_size = aes->get_block_size();
ASSERT_NE(block_size, 0u);
for (off_t end = 1; end < 6096 ; end+=3)
{
off_t begin = 0;
off_t offset = end*end*end*end*end % (1000*1000*1000);
offset = offset - offset % block_size;
ASSERT_EQ(begin % block_size, 0u);
ASSERT_LE(end, test_range);
ASSERT_EQ(offset % block_size, 0u);
bufferlist encrypted;
ASSERT_TRUE(aes->encrypt(input, begin, end, encrypted, offset));
bufferlist decrypted;
ASSERT_TRUE(aes->decrypt(encrypted, 0, end, decrypted, offset));
ASSERT_EQ(decrypted.length(), end);
ASSERT_EQ(std::string_view(input.c_str(), end),
std::string_view(decrypted.c_str(), end) );
}
}
}
TEST(TestRGWCrypto, verify_AES_256_CBC_identity_3)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
buffer::ptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
for (unsigned int step : {1, 2, 3, 5, 7, 11, 13, 17})
{
//make some random key
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i*step;
auto aes(AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
ASSERT_NE(aes.get(), nullptr);
size_t block_size = aes->get_block_size();
ASSERT_NE(block_size, 0u);
size_t rr = 111;
for (size_t r = 97; r < 123 ; r++)
{
off_t begin = 0;
off_t end = begin + r*r*r*r*r*r*r % (test_range - begin);
//sometimes make aligned
if (r % 3)
end = end - end % block_size;
off_t offset = r*r*r*r*r*r*r*r % (1000*1000*1000);
offset = offset - offset % block_size;
ASSERT_EQ(begin % block_size, 0u);
ASSERT_LE(end, test_range);
ASSERT_EQ(offset % block_size, 0u);
bufferlist encrypted1;
bufferlist encrypted2;
off_t pos = begin;
off_t chunk;
while (pos < end) {
chunk = block_size + (rr/3)*(rr+17)*(rr+71)*(rr+123)*(rr+131) % 50000;
chunk = chunk - chunk % block_size;
if (pos + chunk > end)
chunk = end - pos;
bufferlist tmp;
ASSERT_TRUE(aes->encrypt(input, pos, chunk, tmp, offset + pos));
encrypted1.append(tmp);
pos += chunk;
rr++;
}
pos = begin;
while (pos < end) {
chunk = block_size + (rr/3)*(rr+97)*(rr+151)*(rr+213)*(rr+251) % 50000;
chunk = chunk - chunk % block_size;
if (pos + chunk > end)
chunk = end - pos;
bufferlist tmp;
ASSERT_TRUE(aes->encrypt(input, pos, chunk, tmp, offset + pos));
encrypted2.append(tmp);
pos += chunk;
rr++;
}
ASSERT_EQ(encrypted1.length(), end);
ASSERT_EQ(encrypted2.length(), end);
ASSERT_EQ(std::string_view(encrypted1.c_str(), end),
std::string_view(encrypted2.c_str(), end) );
}
}
}
TEST(TestRGWCrypto, verify_AES_256_CBC_size_0_15)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
buffer::ptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
for (unsigned int step : {1, 2, 3, 5, 7, 11, 13, 17})
{
//make some random key
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i*step;
auto aes(AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
ASSERT_NE(aes.get(), nullptr);
size_t block_size = aes->get_block_size();
ASSERT_NE(block_size, 0u);
for (size_t r = 97; r < 123 ; r++)
{
off_t begin = 0;
off_t end = begin + r*r*r*r*r*r*r % (16);
off_t offset = r*r*r*r*r*r*r*r % (1000*1000*1000);
offset = offset - offset % block_size;
ASSERT_EQ(begin % block_size, 0u);
ASSERT_LE(end, test_range);
ASSERT_EQ(offset % block_size, 0u);
bufferlist encrypted;
bufferlist decrypted;
ASSERT_TRUE(aes->encrypt(input, 0, end, encrypted, offset));
ASSERT_TRUE(aes->encrypt(encrypted, 0, end, decrypted, offset));
ASSERT_EQ(encrypted.length(), end);
ASSERT_EQ(decrypted.length(), end);
ASSERT_EQ(std::string_view(input.c_str(), end),
std::string_view(decrypted.c_str(), end) );
}
}
}
TEST(TestRGWCrypto, verify_AES_256_CBC_identity_last_block)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
buffer::ptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
for (unsigned int step : {1, 2, 3, 5, 7, 11, 13, 17})
{
//make some random key
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i*step;
auto aes(AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
ASSERT_NE(aes.get(), nullptr);
size_t block_size = aes->get_block_size();
ASSERT_NE(block_size, 0u);
size_t rr = 111;
for (size_t r = 97; r < 123 ; r++)
{
off_t begin = 0;
off_t end = r*r*r*r*r*r*r % (test_range - 16);
end = end - end % block_size;
end = end + (r+3)*(r+5)*(r+7) % 16;
off_t offset = r*r*r*r*r*r*r*r % (1000*1000*1000);
offset = offset - offset % block_size;
ASSERT_EQ(begin % block_size, 0u);
ASSERT_LE(end, test_range);
ASSERT_EQ(offset % block_size, 0u);
bufferlist encrypted1;
bufferlist encrypted2;
off_t pos = begin;
off_t chunk;
while (pos < end) {
chunk = block_size + (rr/3)*(rr+17)*(rr+71)*(rr+123)*(rr+131) % 50000;
chunk = chunk - chunk % block_size;
if (pos + chunk > end)
chunk = end - pos;
bufferlist tmp;
ASSERT_TRUE(aes->encrypt(input, pos, chunk, tmp, offset + pos));
encrypted1.append(tmp);
pos += chunk;
rr++;
}
pos = begin;
while (pos < end) {
chunk = block_size + (rr/3)*(rr+97)*(rr+151)*(rr+213)*(rr+251) % 50000;
chunk = chunk - chunk % block_size;
if (pos + chunk > end)
chunk = end - pos;
bufferlist tmp;
ASSERT_TRUE(aes->encrypt(input, pos, chunk, tmp, offset + pos));
encrypted2.append(tmp);
pos += chunk;
rr++;
}
ASSERT_EQ(encrypted1.length(), end);
ASSERT_EQ(encrypted2.length(), end);
ASSERT_EQ(std::string_view(encrypted1.c_str(), end),
std::string_view(encrypted2.c_str(), end) );
}
}
}
TEST(TestRGWCrypto, verify_RGWGetObj_BlockDecrypt_ranges)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
bufferptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i] = i;
auto cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
bufferlist encrypted;
ASSERT_TRUE(cbc->encrypt(input, 0, test_range, encrypted, 0));
for (off_t r = 93; r < 150; r++ )
{
ut_get_sink get_sink;
auto cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink, std::move(cbc), {});
//random ranges
off_t begin = (r/3)*r*(r+13)*(r+23)*(r+53)*(r+71) % test_range;
off_t end = begin + (r/5)*(r+7)*(r+13)*(r+101)*(r*103) % (test_range - begin) - 1;
off_t f_begin = begin;
off_t f_end = end;
decrypt.fixup_range(f_begin, f_end);
decrypt.handle_data(encrypted, f_begin, f_end - f_begin + 1);
decrypt.flush();
const std::string& decrypted = get_sink.get_sink();
size_t expected_len = end - begin + 1;
ASSERT_EQ(decrypted.length(), expected_len);
ASSERT_EQ(decrypted, std::string_view(input.c_str()+begin, expected_len));
}
}
TEST(TestRGWCrypto, verify_RGWGetObj_BlockDecrypt_chunks)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
bufferptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i] = i;
auto cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
bufferlist encrypted;
ASSERT_TRUE(cbc->encrypt(input, 0, test_range, encrypted, 0));
for (off_t r = 93; r < 150; r++ )
{
ut_get_sink get_sink;
auto cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink, std::move(cbc), {});
//random
off_t begin = (r/3)*r*(r+13)*(r+23)*(r+53)*(r+71) % test_range;
off_t end = begin + (r/5)*(r+7)*(r+13)*(r+101)*(r*103) % (test_range - begin) - 1;
off_t f_begin = begin;
off_t f_end = end;
decrypt.fixup_range(f_begin, f_end);
off_t pos = f_begin;
do
{
off_t size = 2 << ((pos * 17 + pos / 113 + r) % 16);
size = (pos + 1117) * (pos + 2229) % size + 1;
if (pos + size > f_end + 1)
size = f_end + 1 - pos;
decrypt.handle_data(encrypted, pos, size);
pos = pos + size;
} while (pos < f_end + 1);
decrypt.flush();
const std::string& decrypted = get_sink.get_sink();
size_t expected_len = end - begin + 1;
ASSERT_EQ(decrypted.length(), expected_len);
ASSERT_EQ(decrypted, std::string_view(input.c_str()+begin, expected_len));
}
}
using range_t = std::pair<off_t, off_t>;
// call filter->fixup_range() and return the range as a pair. this makes it easy
// to fit on a single line for ASSERT_EQ()
range_t fixup_range(RGWGetObj_BlockDecrypt *decrypt, off_t ofs, off_t end)
{
decrypt->fixup_range(ofs, end);
return {ofs, end};
}
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
ut_get_sink get_sink;
auto nonecrypt = std::unique_ptr<BlockCrypt>(new BlockCryptNone);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt), {});
ASSERT_EQ(fixup_range(&decrypt,0,0), range_t(0,255));
ASSERT_EQ(fixup_range(&decrypt,1,256), range_t(0,511));
ASSERT_EQ(fixup_range(&decrypt,0,255), range_t(0,255));
ASSERT_EQ(fixup_range(&decrypt,255,256), range_t(0,511));
ASSERT_EQ(fixup_range(&decrypt,511,1023), range_t(256,1023));
ASSERT_EQ(fixup_range(&decrypt,513,1024), range_t(512,1024+255));
}
std::vector<size_t> create_mp_parts(size_t obj_size, size_t mp_part_len){
std::vector<size_t> parts_len;
size_t part_size;
size_t ofs=0;
while (ofs < obj_size){
part_size = std::min(mp_part_len, (obj_size - ofs));
ofs += part_size;
parts_len.push_back(part_size);
}
return parts_len;
}
const size_t part_size = 5*1024*1024;
const size_t obj_size = 30*1024*1024;
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup_simple)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
ut_get_sink get_sink;
auto nonecrypt = std::make_unique<BlockCryptNone>(4096);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt),
create_mp_parts(obj_size, part_size));
ASSERT_EQ(fixup_range(&decrypt,0,0), range_t(0,4095));
ASSERT_EQ(fixup_range(&decrypt,1,4096), range_t(0,8191));
ASSERT_EQ(fixup_range(&decrypt,0,4095), range_t(0,4095));
ASSERT_EQ(fixup_range(&decrypt,4095,4096), range_t(0,8191));
// ranges are end-end inclusive, we request bytes just spanning short of first
// part to exceeding the first part, part_size - 1 is aligned to a 4095 boundary
ASSERT_EQ(fixup_range(&decrypt, 0, part_size - 2), range_t(0, part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size - 1), range_t(0, part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size), range_t(0, part_size + 4095));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size + 1), range_t(0, part_size + 4095));
// request bytes spanning 2 parts
ASSERT_EQ(fixup_range(&decrypt, part_size -2, part_size + 2),
range_t(part_size - 4096, part_size + 4095));
// request last byte
ASSERT_EQ(fixup_range(&decrypt, obj_size - 1, obj_size -1),
range_t(obj_size - 4096, obj_size -1));
}
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup_non_aligned_obj_size)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
const size_t na_obj_size = obj_size + 1;
ut_get_sink get_sink;
auto nonecrypt = std::make_unique<BlockCryptNone>(4096);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt),
create_mp_parts(na_obj_size, part_size));
// these should be unaffected here
ASSERT_EQ(fixup_range(&decrypt, 0, part_size - 2), range_t(0, part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size - 1), range_t(0, part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size), range_t(0, part_size + 4095));
ASSERT_EQ(fixup_range(&decrypt, 0, part_size + 1), range_t(0, part_size + 4095));
// request last 2 bytes; spanning 2 parts
ASSERT_EQ(fixup_range(&decrypt, na_obj_size -2 , na_obj_size -1),
range_t(na_obj_size - 1 - 4096, na_obj_size - 1));
// request last byte, spans last 1B part only
ASSERT_EQ(fixup_range(&decrypt, na_obj_size -1, na_obj_size - 1),
range_t(na_obj_size - 1, na_obj_size -1));
}
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup_non_aligned_part_size)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
const size_t na_part_size = part_size + 1;
ut_get_sink get_sink;
auto nonecrypt = std::make_unique<BlockCryptNone>(4096);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt),
create_mp_parts(obj_size, na_part_size));
// na_part_size -2, ie. part_size -1 is aligned to 4095 boundary
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size - 2), range_t(0, na_part_size -2));
// even though na_part_size -1 should not align to a 4095 boundary, the range
// should not span the next part
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size - 1), range_t(0, na_part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size), range_t(0, na_part_size + 4095));
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size + 1), range_t(0, na_part_size + 4095));
// request spanning 2 parts
ASSERT_EQ(fixup_range(&decrypt, na_part_size - 2, na_part_size + 2),
range_t(na_part_size - 1 - 4096, na_part_size + 4095));
// request last byte, this will be interesting, since this a multipart upload
// with 5MB+1 size, the last part is actually 5 bytes short of 5 MB, which
// should be considered for the ranges alignment; an easier way to look at
// this will be that the last offset aligned to a 5MiB part will be 5MiB -
// 4095, this is a part that is 5MiB - 5 B
ASSERT_EQ(fixup_range(&decrypt, obj_size - 1, obj_size -1),
range_t(obj_size +5 -4096, obj_size -1));
}
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup_non_aligned)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
const size_t na_part_size = part_size + 1;
const size_t na_obj_size = obj_size + 7; // (6*(5MiB + 1) + 1) for the last 1B overflow
ut_get_sink get_sink;
auto nonecrypt = std::make_unique<BlockCryptNone>(4096);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt),
create_mp_parts(na_obj_size, na_part_size));
// na_part_size -2, ie. part_size -1 is aligned to 4095 boundary
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size - 2), range_t(0, na_part_size -2));
// even though na_part_size -1 should not align to a 4095 boundary, the range
// should not span the next part
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size - 1), range_t(0, na_part_size -1));
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size), range_t(0, na_part_size + 4095));
ASSERT_EQ(fixup_range(&decrypt, 0, na_part_size + 1), range_t(0, na_part_size + 4095));
// request last byte, spans last 1B part only
ASSERT_EQ(fixup_range(&decrypt, na_obj_size -1, na_obj_size - 1),
range_t(na_obj_size - 1, na_obj_size -1));
ASSERT_EQ(fixup_range(&decrypt, na_obj_size -2, na_obj_size -1),
range_t(na_obj_size - 2, na_obj_size -1));
}
TEST(TestRGWCrypto, check_RGWGetObj_BlockDecrypt_fixup_invalid_ranges)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
ut_get_sink get_sink;
auto nonecrypt = std::make_unique<BlockCryptNone>(4096);
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
std::move(nonecrypt),
create_mp_parts(obj_size, part_size));
// the ranges below would be mostly unreachable in current code as rgw
// would've returned a 411 before reaching, but we're just doing this to make
// sure we don't have invalid access
ASSERT_EQ(fixup_range(&decrypt, obj_size - 1, obj_size + 100),
range_t(obj_size - 4096, obj_size - 1));
ASSERT_EQ(fixup_range(&decrypt, obj_size, obj_size + 1),
range_t(obj_size - 1, obj_size - 1));
ASSERT_EQ(fixup_range(&decrypt, obj_size+1, obj_size + 100),
range_t(obj_size - 1, obj_size - 1));
}
TEST(TestRGWCrypto, verify_RGWPutObj_BlockEncrypt_chunks)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
//create some input for encryption
const off_t test_range = 1024*1024;
bufferptr buf(test_range);
char* p = buf.c_str();
for(size_t i = 0; i < buf.length(); i++)
p[i] = i + i*i + (i >> 2);
bufferlist input;
input.append(buf);
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i] = i;
for (off_t r = 93; r < 150; r++ )
{
ut_put_sink put_sink;
auto cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
RGWPutObj_BlockEncrypt encrypt(&no_dpp, g_ceph_context, &put_sink,
std::move(cbc));
off_t test_size = (r/5)*(r+7)*(r+13)*(r+101)*(r*103) % (test_range - 1) + 1;
off_t pos = 0;
do
{
off_t size = 2 << ((pos * 17 + pos / 113 + r) % 16);
size = (pos + 1117) * (pos + 2229) % size + 1;
if (pos + size > test_size)
size = test_size - pos;
bufferlist bl;
bl.append(input.c_str()+pos, size);
encrypt.process(std::move(bl), pos);
pos = pos + size;
} while (pos < test_size);
encrypt.process({}, pos);
ASSERT_EQ(put_sink.get_sink().length(), static_cast<size_t>(test_size));
cbc = AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32);
ASSERT_NE(cbc.get(), nullptr);
bufferlist encrypted;
bufferlist decrypted;
encrypted.append(put_sink.get_sink());
ASSERT_TRUE(cbc->decrypt(encrypted, 0, test_size, decrypted, 0));
ASSERT_EQ(decrypted.length(), test_size);
ASSERT_EQ(std::string_view(decrypted.c_str(), test_size),
std::string_view(input.c_str(), test_size));
}
}
TEST(TestRGWCrypto, verify_Encrypt_Decrypt)
{
const NoDoutPrefix no_dpp(g_ceph_context, dout_subsys);
uint8_t key[32];
for(size_t i=0;i<sizeof(key);i++)
key[i]=i;
size_t fi_a = 0;
size_t fi_b = 1;
size_t test_size;
do
{
//fibonacci
size_t tmp = fi_b;
fi_b = fi_a + fi_b;
fi_a = tmp;
test_size = fi_b;
uint8_t* test_in = new uint8_t[test_size];
//fill with something
memset(test_in, test_size & 0xff, test_size);
ut_put_sink put_sink;
RGWPutObj_BlockEncrypt encrypt(&no_dpp, g_ceph_context, &put_sink,
AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32));
bufferlist bl;
bl.append((char*)test_in, test_size);
encrypt.process(std::move(bl), 0);
encrypt.process({}, test_size);
ASSERT_EQ(put_sink.get_sink().length(), test_size);
bl.append(put_sink.get_sink().data(), put_sink.get_sink().length());
ASSERT_EQ(bl.length(), test_size);
ut_get_sink get_sink;
RGWGetObj_BlockDecrypt decrypt(&no_dpp, g_ceph_context, &get_sink,
AES_256_CBC_create(&no_dpp, g_ceph_context, &key[0], 32),
{});
off_t bl_ofs = 0;
off_t bl_end = test_size - 1;
decrypt.fixup_range(bl_ofs, bl_end);
decrypt.handle_data(bl, 0, bl.length());
decrypt.flush();
ASSERT_EQ(get_sink.get_sink().length(), test_size);
ASSERT_EQ(get_sink.get_sink(), std::string_view((char*)test_in,test_size));
}
while (test_size < 20000);
}
int main(int argc, char **argv) {
auto args = argv_to_vec(argc, argv);
auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
CODE_ENVIRONMENT_UTILITY,
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
common_init_finish(g_ceph_context);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|