-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathmemguard.go
1097 lines (877 loc) · 27.6 KB
/
memguard.go
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
package memguard
import (
"bytes"
"os"
"os/signal"
"syscall"
"unsafe"
"github.com/awnumar/memguard/crypto"
"github.com/awnumar/memguard/memcall"
)
/*
NewImmutable creates a new Enclave of a specified size. The created object will be immutable and sealed, and these states can be toggled with the MakeImmutable, MakeMutable, Unseal, and Reseal methods.
If the given length is less than one, the call will return an ErrInvalidLength.
*/
func NewImmutable(size int) (*Enclave, error) {
// Create a new Enclave.
b, err := newContainer(size)
if err != nil {
return nil, err
}
// Seal the Enclave.
b.reseal()
// Make the memory immutable.
b.MakeImmutable()
// Return the Enclave object.
return b, nil
}
/*
NewMutable creates a new Enclave of a specified size. The created object will be mutable and sealed, and these states can be toggled with the MakeImmutable, MakeMutable, Unseal, and Reseal methods.
If the given length is less than one, the call will return an ErrInvalidLength.
*/
func NewMutable(size int) (*Enclave, error) {
// Create a new Enclave.
b, err := newContainer(size)
if err != nil {
return nil, err
}
// Seal the Enclave.
b.reseal()
// Return the Enclave object.
return b, nil
}
/*
NewImmutableFromBytes is identical to NewImmutable but for the fact that the created Enclave is of the same length and has the same contents as a given slice. The slice is wiped after the bytes have been copied over.
If the size of the slice is zero, the call will return an ErrInvalidLength.
*/
func NewImmutableFromBytes(buf []byte) (*Enclave, error) {
// Create a new Enclave.
b, err := newContainer(len(buf))
if err != nil {
return nil, err
}
// Copy the bytes from buf, wiping afterwards.
b.Move(buf)
// Seal the Enclave.
b.reseal()
// Make the memory immutable.
b.MakeImmutable()
// Return a pointer to the Enclave.
return b, nil
}
/*
NewMutableFromBytes is identical to NewMutable but for the fact that the created Enclave is of the same length and has the same contents as a given slice. The slice is wiped after the bytes have been copied over.
If the size of the slice is zero, the call will return an ErrInvalidLength.
*/
func NewMutableFromBytes(buf []byte) (*Enclave, error) {
// Create a new Enclave.
b, err := newContainer(len(buf))
if err != nil {
return nil, err
}
// Copy the bytes from buf, wiping afterwards.
b.Move(buf)
// Seal the Enclave.
b.reseal()
// Return a pointer to the Enclave.
return b, nil
}
/*
NewImmutableRandom is identical to NewImmutable except the created Enclave is filled with cryptographically-secure pseudo-random bytes instead of zeroes.
If the given length is less than one, the call will return an ErrInvalidLength.
*/
func NewImmutableRandom(size int) (*Enclave, error) {
// Create a new Enclave for the key.
b, err := newContainer(size)
if err != nil {
return nil, err
}
// Fill it with random data.
if err := crypto.MemScr(b.plaintext); err != nil {
SafePanic(err)
}
// Seal the Enclave.
b.reseal()
// Make the memory immutable.
b.MakeImmutable()
// Return the Enclave.
return b, nil
}
/*
NewMutableRandom is identical to NewMutable except the created Enclave is filled with cryptographically-secure pseudo-random bytes instead of zeroes.
If the given length is less than one, the call will return an ErrInvalidLength.
*/
func NewMutableRandom(size int) (*Enclave, error) {
// Create a new Enclave for the key.
b, err := newContainer(size)
if err != nil {
return nil, err
}
// Fill it with random data.
if err := crypto.MemScr(b.plaintext); err != nil {
SafePanic(err)
}
// Seal the Enclave.
b.reseal()
// Return the Enclave.
return b, nil
}
/*
Unseal decrypts and unseals a given Enclave.
All new Enclaves are sealed by default and you should use this method to temporarily unseal them, so as to view or modify their contents, and then call Reseal again as soon as possible. There is no need to call Unseal if you are using MemGuard's own API since internal functions know how to handle sealed containers and will unseal and reseal them for you automatically. Instead, call Unseal before passing the contents of a container to another API, and reiterating, call Reseal again soon after.
Calling Unseal on an unsealed Enclave will return ErrUnsealed. This is because the developer has assumed that a container is sealed when it isn't, and that is a dangerous assumption.
If the Enclave is immutable, Unseal will automatically work around it and preserve the immutability state.
*/
func (b *container) Unseal() error {
// Attain the mutex.
b.Lock()
defer b.Unlock()
return b.unseal()
}
/*
Reseal re-encrypts and reseals a given Enclave.
All new Enclaves are sealed by default and you should use Unseal to unseal them them, so as to view or modify their contents, and then call this method again as soon as possible.
Calling Reseal on a sealed Enclave does nothing. If the given Enclave is immutable, Reseal will automatically work around it and preserve the immutability state.
*/
func (b *container) Reseal() error {
// Attain the mutex.
b.Lock()
defer b.Unlock()
return b.reseal()
}
/*
Bytes returns a slice that references the secure, protected portion of memory.
If the Enclave that you call Bytes on is sealed, the data returned will be random and so Unseal should be called first (promptly followed by Reseal when done). Otherwise, if the Enclave has been destroyed then the returned slice will be nil (it will have a length and capacity of zero).
If a function that you're using requires an array, you can cast the slice to an array and then pass around a pointer:
// Make sure the size of the array matches the size of the buffer.
// In this example that size is 16. This is VERY important.
keyArrayPtr := (*[16]byte)(unsafe.Pointer(&b.Bytes()[0]))
// Pass around the array pointer WITHOUT dereferencing it.
Encrypt(plaintext, keyArrayPtr)
Make sure that you do not dereference the pointer and pass around the resulting value as this will leave copies all over the place.
*/
func (b *container) Bytes() []byte {
return b.plaintext
}
/*
Uint8 returns a slice (of type []uint8) that references the secure, protected portion of memory.
Uint8 is practically identical to Bytes except it also returns errors. Bytes will usually be the faster and easier option.
*/
func (b *container) Uint8() ([]uint8, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Return the slice.
return []uint8(b.plaintext), nil
}
/*
Uint16 returns a slice (of type []uint16) that references the secure, protected portion of memory.
The Enclave must be a multiple of 2 bytes in length or an error will be returned.
*/
func (b *container) Uint16() ([]uint16, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%2 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 2, b.Size() / 2}
// Return the new slice.
return *(*[]uint16)(unsafe.Pointer(&sl)), nil
}
/*
Uint32 returns a slice (of type []uint32) that references the secure, protected portion of memory.
The Enclave must be a multiple of 4 bytes in length or an error will be returned.
*/
func (b *container) Uint32() ([]uint32, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%4 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 4, b.Size() / 4}
// Return the new slice.
return *(*[]uint32)(unsafe.Pointer(&sl)), nil
}
/*
Uint64 returns a slice (of type []uint64) that references the secure, protected portion of memory.
The Enclave must be a multiple of 8 bytes in length or an error will be returned.
*/
func (b *container) Uint64() ([]uint64, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%8 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 8, b.Size() / 8}
// Return the new slice.
return *(*[]uint64)(unsafe.Pointer(&sl)), nil
}
/*
Int8 returns a slice (of type []int8) that references the secure, protected portion of memory.
*/
func (b *container) Int8() ([]int8, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size(), b.Size()}
// Return the new slice.
return *(*[]int8)(unsafe.Pointer(&sl)), nil
}
/*
Int16 returns a slice (of type []int16) that references the secure, protected portion of memory.
The Enclave must be a multiple of 2 bytes in length or an error will be returned.
*/
func (b *container) Int16() ([]int16, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%2 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 2, b.Size() / 2}
// Return the new slice.
return *(*[]int16)(unsafe.Pointer(&sl)), nil
}
/*
Int32 returns a slice (of type []int32) that references the secure, protected portion of memory.
The Enclave must be a multiple of 4 bytes in length or an error will be returned.
*/
func (b *container) Int32() ([]int32, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%4 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 4, b.Size() / 4}
// Return the new slice.
return *(*[]int32)(unsafe.Pointer(&sl)), nil
}
/*
Int64 returns a slice (of type []int64) that references the secure, protected portion of memory.
The Enclave must also be a multiple of 8 bytes in length or an error will be returned.
*/
func (b *container) Int64() ([]int64, error) {
// Attain the mutex lock.
b.Lock()
defer b.Unlock()
// Check to see if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check if it's sealed.
if b.sealed {
return nil, ErrSealed
}
// Check to see if it's an appropriate length.
if len(b.plaintext)%8 != 0 {
return nil, ErrInvalidConversion
}
// Perform the conversion.
var sl = struct {
addr uintptr
len int
cap int
}{uintptr(unsafe.Pointer(&b.plaintext[0])), b.Size() / 8, b.Size() / 8}
// Return the new slice.
return *(*[]int64)(unsafe.Pointer(&sl)), nil
}
/*
IsSealed returns a boolean value indicating if an Enclave is Sealed.
*/
func (b *container) IsSealed() bool {
// Attain the mutex.
b.Lock()
defer b.Unlock()
return b.sealed
}
/*
IsMutable returns a boolean value indicating if an Enclave is mutable.
*/
func (b *container) IsMutable() bool {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
return b.mutable
}
/*
IsDestroyed returns a boolean value indicating if an Enclave has been destroyed.
*/
func (b *container) IsDestroyed() bool {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Return the appropriate value.
return len(b.plaintext) == 0
}
/*
EqualBytes compares an Enclave to a byte slice in constant time. If called on a sealed Enclave, EqualBytes will automatically unseal and reseal it for you.
*/
func (b *container) EqualBytes(buf []byte) (bool, error) {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return false, ErrDestroyed
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Do a time-constant comparison and return the result.
return crypto.Equal(b.plaintext, buf), nil
}
/*
MakeImmutable asks the kernel to mark the Enclave's contents immutable. Any subsequent attempts to modify this memory will result in the kernel raising an access violation and terminating the process. To make the buffer mutable again, call MakeMutable.
The API will respect your mutability state and so if a MemGuard function that needs to modify an Enclave is handed one that is immutable, it will return ErrImmutable.
*/
func (b *container) MakeImmutable() error {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return ErrDestroyed
}
if b.mutable {
// Mark the memory as mutable.
if err := memcall.Protect(getAllMemory(b)[pageSize:pageSize+roundToPageSize(len(b.plaintext)+32)], true, false); err != nil {
SafePanic(err)
}
// Tell everyone about the change we made.
b.mutable = false
}
// Everything went well.
return nil
}
/*
MakeMutable asks the kernel to mark the Enclave's contents mutable. To make the buffer immutable again, call MakeImmutable.
*/
func (b *container) MakeMutable() error {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return ErrDestroyed
}
if !b.mutable {
// Mark the memory as mutable.
if err := memcall.Protect(getAllMemory(b)[pageSize:pageSize+roundToPageSize(len(b.plaintext)+32)], true, true); err != nil {
SafePanic(err)
}
// Tell everyone about the change we made.
b.mutable = true
}
// Everything went well.
return nil
}
/*
Copy copies bytes from a byte slice into an Enclave, in constant-time. Just like Golang's built-in copy function, Copy only copies up to the smallest of the two buffers.
Copy does not wipe the original slice so using Move should be favoured unless you have a specific reason. You should aim to call WipeBytes on the original slice as soon as possible after copying.
If the Enclave is marked as immutable, the call will fail and return an ErrImmutable. If the Enclave is sealed, Copy will automatically unseal and reseal it for you.
*/
func (b *container) Copy(buf []byte) error {
// Just call CopyAt with a zero offset.
return b.CopyAt(buf, 0)
}
/*
CopyAt is identical to Copy but it copies into the Enclave at a specified offset.
*/
func (b *container) CopyAt(buf []byte, offset int) error {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return ErrDestroyed
}
// Check if it's immutable.
if !b.mutable {
return ErrImmutable
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Do a time-constant copying of the bytes.
crypto.Copy(b.plaintext[offset:], buf)
return nil
}
/*
Move moves bytes from a byte slice into an Enclave in constant-time. Just like Golang's built-in copy function, Move only moves up to the smallest of the two buffers.
Unlike Copy, Move wipes the entire original slice after copying, and so it should be favoured unless you have a specific reason.
If the Enclave is marked as immutable, the call will fail and return an ErrImmutable. If the Enclave is sealed, Move will automatically unseal and reseal it for you.
*/
func (b *container) Move(buf []byte) error {
// Just call MoveAt with a zero offset.
return b.MoveAt(buf, 0)
}
/*
MoveAt is identical to Move but it copies into the Enclave at a specified offset.
*/
func (b *container) MoveAt(buf []byte, offset int) error {
// Copy buf into the Enclave.
if err := b.CopyAt(buf, offset); err != nil {
return err
}
// Wipe the old bytes.
crypto.MemClr(buf)
// Everything went well.
return nil
}
/*
FillRandomBytes fills an Enclave with cryptographically-secure pseudo-random bytes. If the Enclave is sealed, it will automatically unseal and reseal it for you.
*/
func (b *container) FillRandomBytes() error {
// Just call FillRandomBytesAt.
return b.FillRandomBytesAt(0, b.Size())
}
/*
FillRandomBytesAt fills an Enclave with cryptographically-secure pseudo-random bytes, starting at an offset and ending after a given number of bytes. If the Enclave is sealed, it will automatically unseal and reseal it for you.
*/
func (b *container) FillRandomBytesAt(offset, length int) error {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return ErrDestroyed
}
// Check if it's immutable.
if !b.mutable {
return ErrImmutable
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Fill with random bytes.
if err := crypto.MemScr(b.plaintext[offset : offset+length]); err != nil {
SafePanic(err)
}
// Everything went well.
return nil
}
/*
Destroy performs some security checks, securely wipes the contents of, and then releases an Enclave's memory back to the OS. If a security check fails, the process will attempt to wipe all it can before safely panicking.
This function should be called on all Enclaves before exiting. DestroyAll is designed for this purpose, as is CatchInterrupt and SafeExit. We recommend using all of them together.
If the Enclave has already been destroyed, subsequent calls are idempotent.
*/
func (b *container) Destroy() {
// Attain a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Return if it's already destroyed.
if len(b.plaintext) == 0 {
return
}
// Remove this one from global slice.
enclavesMutex.Lock()
for i, v := range enclaves {
if v == b {
enclaves = append(enclaves[:i], enclaves[i+1:]...)
break
}
}
enclavesMutex.Unlock()
// Get all of the memory related to this Enclave.
memory := getAllMemory(b)
// Get the total size of all the pages between the guards.
roundedLength := len(memory) - (pageSize * 2)
// Verify the canary.
c := subclaves.canary.getView()
defer c.destroy()
if !bytes.Equal(memory[pageSize+roundedLength-len(b.plaintext)-32:pageSize+roundedLength-len(b.plaintext)], c.plaintext) {
SafePanic("memguard.Destroy(): canary check failed; possible buffer overflow")
}
// Make all of the memory readable and writable.
if err := memcall.Protect(memory, true, true); err != nil {
SafePanic(err)
}
// Wipe the pages that hold our data.
crypto.MemClr(memory[pageSize : pageSize+roundedLength])
// Unlock the pages that hold our data.
if err := memcall.Unlock(memory[pageSize : pageSize+roundedLength]); err != nil {
SafePanic(err)
}
// Free all related memory.
if err := memcall.Free(memory); err != nil {
SafePanic(err)
}
// Wipe the ciphertext field.
crypto.MemClr(b.ciphertext)
// Clear the fields.
b.plaintext = nil
b.ciphertext = nil
b.mutable = false
b.sealed = false
}
/*
Size returns an integer representing the total length, in bytes, of an Enclave.
If this size is zero, it is safe to assume that the Enclave has been destroyed.
*/
func (b *container) Size() int {
return len(b.plaintext)
}
/*
Wipe wipes an Enclave's contents by overwriting the buffer with zeroes. If the Enclave is sealed, it will automatically unseal and reseal it for you.
*/
func (b *container) Wipe() error {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return ErrDestroyed
}
// Check if it's immutable.
if !b.mutable {
return ErrImmutable
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Wipe the buffer.
crypto.MemClr(b.plaintext)
// Everything went well.
return nil
}
/*
Concatenate takes two Enclaves, concatenates them, and returns a sealed container. The original Enclaves are preserved.
If one of the given Enclaves is immutable, the resulting Enclave will also be immutable. If an Enclave is sealed, Concatenate will automatically unseal and reseal it for you.
*/
func Concatenate(a, b *Enclave) (*Enclave, error) {
// Get a mutex lock on the Enclaves.
a.Lock()
b.Lock()
defer a.Unlock()
defer b.Unlock()
// Check if either are destroyed.
if len(a.plaintext) == 0 || len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check to see if either are sealed.
if a.sealed {
a.unseal()
defer a.reseal()
}
if b.sealed {
b.unseal()
defer b.reseal()
}
// Create a new Enclave to hold the concatenated value.
c, _ := newContainer(len(a.plaintext) + len(b.plaintext))
// Copy the values across.
c.Copy(a.plaintext)
c.CopyAt(b.plaintext, len(a.plaintext))
// Seal the container.
c.reseal()
// Set permissions accordingly.
if !a.mutable || !b.mutable {
c.MakeImmutable()
}
// Return the resulting Enclave.
return c, nil
}
/*
Duplicate takes an Enclave and creates a new one with the same contents and mutability state as the original.
If the Enclave is sealed, it will automatically unseal and reseal it for you. The returned Enclave will be sealed regardless.
*/
func Duplicate(b *Enclave) (*Enclave, error) {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Create new Enclave.
newBuf, _ := newContainer(b.Size())
// Copy bytes into it.
newBuf.Copy(b.plaintext)
// Seal it.
newBuf.reseal()
// Set permissions accordingly.
if !b.mutable {
newBuf.MakeImmutable()
}
// Return duplicated.
return newBuf, nil
}
/*
Equal compares the contents of two Enclaves in constant time. If either are sealed, it will automatically unseal and reseal them for you.
*/
func Equal(a, b *Enclave) (bool, error) {
// Get a mutex lock on the Enclaves.
a.Lock()
b.Lock()
defer a.Unlock()
defer b.Unlock()
// Check if either are destroyed.
if len(a.plaintext) == 0 || len(b.plaintext) == 0 {
return false, ErrDestroyed
}
// Check to see if either are sealed.
if a.sealed {
a.unseal()
defer a.reseal()
}
if b.sealed {
b.unseal()
defer b.reseal()
}
// Do a time-constant comparison on the two buffers; return the result.
return crypto.Equal(a.plaintext, b.plaintext), nil
}
/*
Split takes an Enclave, splits it at a specified offset, and then returns the two newly created Enclaves. The mutability state of the original is preserved in the new (sealed) Enclaves, and the original Enclave is not destroyed. If the given Enclave is sealed, Split will automatically unseal and reseal it for you.
*/
func Split(b *Enclave, offset int) (*Enclave, *Enclave, error) {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return nil, nil, ErrDestroyed
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Create two new Enclaves.
firstBuf, err := newContainer(len(b.plaintext[:offset]))
if err != nil {
return nil, nil, err
}
secondBuf, err := newContainer(len(b.plaintext[offset:]))
if err != nil {
firstBuf.Destroy()
return nil, nil, err
}
// Copy the values into them.
firstBuf.Copy(b.plaintext[:offset])
secondBuf.Copy(b.plaintext[offset:])
// Seal them.
firstBuf.reseal()
secondBuf.reseal()
// Copy over permissions.
if !b.mutable {
firstBuf.MakeImmutable()
secondBuf.MakeImmutable()
}
// Return the new Enclaves.
return firstBuf, secondBuf, nil
}
/*
Trim shortens an Enclave according to the given specifications. It takes an offset and a size as arguments. The resulting Enclave starts at index [offset] and ends at index [offset+size].
The mutability state of the original is preserved in the new (sealed) Enclave, and the original Enclave is not destroyed. If Trim is called on a sealed Enclave, it will automatically unseal and reseal it for you.
*/
func Trim(b *Enclave, offset, size int) (*Enclave, error) {
// Get a mutex lock on this Enclave.
b.Lock()
defer b.Unlock()
// Check if it's destroyed.
if len(b.plaintext) == 0 {
return nil, ErrDestroyed
}
// Check to see if it's sealed.
if b.sealed {
b.unseal()
defer b.reseal()
}
// Create new Enclave and copy over the old.
newBuf, err := newContainer(size)
if err != nil {
return nil, err
}
newBuf.Copy(b.plaintext[offset : offset+size])
// Seal it up.
newBuf.reseal()
// Copy over permissions.
if !b.mutable {