summaryrefslogtreecommitdiffstats
path: root/src/method.c
blob: 5f5f29898803556a18fba77553a5da387b0e6398 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/*
 *  method.c: Method implementations for nwipe.
 *
 *  Copyright Darik Horn <dajhorn-dban@vanadac.com>.
 *
 *  Modifications to original dwipe Copyright Andy Beverley <andy@andybev.com>
 *
 *  This program 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, version 2.
 *
 *  This program 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
 *  this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/* HOWTO:  Add another wipe method.
 *
 *  1.  Create a new function here and add the prototype to the 'method.h' file.
 *  2.  Update nwipe_method_label() appropriately.
 *  3.  Put the passes that you wish to run into a nwipe_pattern_t array.
 *  4.  Call nwipe_runmethod() with your array of patterns.
 *  5.  Copy-and-paste within the 'options.c' file so that the new method can be invoked.
 *  6.  Optionally try to plug your function into 'gui.c'.
 *  7.  Update the function 'calculate_round_size()' with the new method.
 *
 *
 * WARNING: Remember to pad all pattern arrays with { 0, NULL }.
 *
 * WARNING: Never change nwipe_options after calling a method.
 *
 * NOTE: The nwipe_runmethod function appends a user selectable final blanking (zero) pass to all methods.
 *
 */

#include <stdint.h>

#include "nwipe.h"
#include "context.h"
#include "method.h"
#include "prng.h"
#include "options.h"
#include "pass.h"
#include "logging.h"

/*
 * Comment Legend
 *
 *   "method"  An ordered set of patterns.
 *   "pattern" The magic bits that will be written to a device.
 *   "pass"    Reading or writing one pattern to an entire device.
 *   "rounds"  The number of times that a method will be applied to a device.
 *
 */

const char* nwipe_dod522022m_label = "DoD 5220.22-M";
const char* nwipe_dodshort_label = "DoD Short";
const char* nwipe_gutmann_label = "Gutmann Wipe";
const char* nwipe_ops2_label = "RCMP TSSIT OPS-II";
const char* nwipe_random_label = "PRNG Stream";
const char* nwipe_zero_label = "Fill With Zeros";
const char* nwipe_one_label = "Fill With Ones";
const char* nwipe_verify_zero_label = "Verify Zeros (0x00)";
const char* nwipe_verify_one_label = "Verify Ones  (0xFF)";
const char* nwipe_is5enh_label = "HMG IS5 Enhanced";

const char* nwipe_unknown_label = "Unknown Method (FIXME)";

const char* nwipe_method_label( void* method )
{
    /**
     *  Returns a pointer to the name of the method function.
     *
     */

    if( method == &nwipe_dod522022m )
    {
        return nwipe_dod522022m_label;
    }
    if( method == &nwipe_dodshort )
    {
        return nwipe_dodshort_label;
    }
    if( method == &nwipe_gutmann )
    {
        return nwipe_gutmann_label;
    }
    if( method == &nwipe_ops2 )
    {
        return nwipe_ops2_label;
    }
    if( method == &nwipe_random )
    {
        return nwipe_random_label;
    }
    if( method == &nwipe_zero )
    {
        return nwipe_zero_label;
    }
    if( method == &nwipe_one )
    {
        return nwipe_one_label;
    }
    if( method == &nwipe_verify_zero )
    {
        return nwipe_verify_zero_label;
    }
    if( method == &nwipe_verify_one )
    {
        return nwipe_verify_one_label;
    }
    if( method == &nwipe_is5enh )
    {
        return nwipe_is5enh_label;
    }

    /* else */
    return nwipe_unknown_label;

} /* nwipe_method_label */

void* nwipe_zero( void* ptr )
{
    /**
     * Fill the device with zeroes.
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* setup for a zero-fill. */

    char zerofill[1] = { '\x00' };
    nwipe_pattern_t patterns[] = { { 1, &zerofill[0] },  // pass 1: 0s
                                   { 0, NULL } };

    /* Run the method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_zero */

void* nwipe_one( void* ptr )
{
    /**
     * Fill the device with ones.
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* setup for a zero-fill. */

    char onefill[1] = { '\xFF' };
    nwipe_pattern_t patterns[] = { { 1, &onefill[0] },  // pass 1: 1s
                                   { 0, NULL } };

    /* Run the method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_one */

void* nwipe_verify_zero( void* ptr )
{
    /**
     * Verify the device is full of zeros.
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* Do nothing because nwipe_runmethod appends a zero-fill. */
    nwipe_pattern_t patterns[] = { { 0, NULL } };

    /* Run the method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_verify zeros */

void* nwipe_verify_one( void* ptr )
{
    /**
     * Verify the device is full of ones.
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* Do nothing because nwipe_runmethod appends a zero-fill. */
    nwipe_pattern_t patterns[] = { { 0, NULL } };

    /* Run the method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_verify */

void* nwipe_dod522022m( void* ptr )
{
    /**
     * United States Department of Defense 5220.22-M standard wipe.
     *
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* A result holder. */
    int r;

    /* Random characters. (Elements 2 and 6 are unused.) */
    char dod[7];

    nwipe_pattern_t patterns[] = { { 1, &dod[0] },  // Pass 1: A random character.
                                   { 1, &dod[1] },  // Pass 2: The bitwise complement of pass 1.
                                   { -1, "" },  // Pass 3: A random stream.
                                   { 1, &dod[3] },  // Pass 4: A random character.
                                   { 1, &dod[4] },  // Pass 5: A random character.
                                   { 1, &dod[5] },  // Pass 6: The bitwise complement of pass 5.
                                   { -1, "" },  // Pass 7: A random stream.
                                   { 0, NULL } };

    /* Load the array with random characters. */
    r = read( c->entropy_fd, &dod, sizeof( dod ) );

    /* NOTE: Only the random data in dod[0], dod[3], and dod[4] is actually used. */

    /* Check the result. */
    if( r != sizeof( dod ) )
    {
        r = errno;
        nwipe_perror( r, __FUNCTION__, "read" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the %s method.", nwipe_dod522022m_label );

        /* Ensure a negative return. */
        if( r < 0 )
        {
            c->result = r;
            return NULL;
        }
        else
        {
            c->result = -1;
            return NULL;
        }
    }

    /* Pass 2 is the bitwise complement of Pass 1. */
    dod[1] = ~dod[0];

    /* Pass 4 is the bitwise complement of Pass 3. */
    dod[5] = ~dod[4];

    /* Run the DoD 5220.22-M method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_dod522022m */

void* nwipe_dodshort( void* ptr )
{
    /**
     * United States Department of Defense 5220.22-M short wipe.
     * This method is comprised of passes 1,2,7 from the standard wipe.
     *
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* A result holder. */
    int r;

    /* Random characters. (Element 3 is unused.) */
    char dod[3];

    nwipe_pattern_t patterns[] = { { 1, &dod[0] },  // Pass 1: A random character.
                                   { 1, &dod[1] },  // Pass 2: The bitwise complement of pass 1.
                                   { -1, "" },  // Pass 3: A random stream.
                                   { 0, NULL } };

    /* Load the array with random characters. */
    r = read( c->entropy_fd, &dod, sizeof( dod ) );

    /* NOTE: Only the random data in dod[0] is actually used. */

    /* Check the result. */
    if( r != sizeof( dod ) )
    {
        r = errno;
        nwipe_perror( r, __FUNCTION__, "read" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the %s method.", nwipe_dodshort_label );

        /* Ensure a negative return. */
        if( r < 0 )
        {
            c->result = r;
            return NULL;
        }
        else
        {
            c->result = -1;
            return NULL;
        }
    }

    /* Pass 2 is the bitwise complement of Pass 1. */
    dod[1] = ~dod[0];

    /* Run the DoD 5220.022-M short method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_dodshort */

void* nwipe_gutmann( void* ptr )
{
    /**
     * Peter Gutmann's wipe.
     *
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* Define the Gutmann method. */
    nwipe_pattern_t book[] = { { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { 3, "\x55\x55\x55" },  // Static pass: 0x555555  01010101 01010101 01010101
                               { 3, "\xAA\xAA\xAA" },  // Static pass: 0XAAAAAA  10101010 10101010 10101010
                               { 3, "\x92\x49\x24" },  // Static pass: 0x924924  10010010 01001001 00100100
                               { 3, "\x49\x24\x92" },  // Static pass: 0x492492  01001001 00100100 10010010
                               { 3, "\x24\x92\x49" },  // Static pass: 0x249249  00100100 10010010 01001001
                               { 3, "\x00\x00\x00" },  // Static pass: 0x000000  00000000 00000000 00000000
                               { 3, "\x11\x11\x11" },  // Static pass: 0x111111  00010001 00010001 00010001
                               { 3, "\x22\x22\x22" },  // Static pass: 0x222222  00100010 00100010 00100010
                               { 3, "\x33\x33\x33" },  // Static pass: 0x333333  00110011 00110011 00110011
                               { 3, "\x44\x44\x44" },  // Static pass: 0x444444  01000100 01000100 01000100
                               { 3, "\x55\x55\x55" },  // Static pass: 0x555555  01010101 01010101 01010101
                               { 3, "\x66\x66\x66" },  // Static pass: 0x666666  01100110 01100110 01100110
                               { 3, "\x77\x77\x77" },  // Static pass: 0x777777  01110111 01110111 01110111
                               { 3, "\x88\x88\x88" },  // Static pass: 0x888888  10001000 10001000 10001000
                               { 3, "\x99\x99\x99" },  // Static pass: 0x999999  10011001 10011001 10011001
                               { 3, "\xAA\xAA\xAA" },  // Static pass: 0xAAAAAA  10101010 10101010 10101010
                               { 3, "\xBB\xBB\xBB" },  // Static pass: 0xBBBBBB  10111011 10111011 10111011
                               { 3, "\xCC\xCC\xCC" },  // Static pass: 0xCCCCCC  11001100 11001100 11001100
                               { 3, "\xDD\xDD\xDD" },  // Static pass: 0xDDDDDD  11011101 11011101 11011101
                               { 3, "\xEE\xEE\xEE" },  // Static pass: 0xEEEEEE  11101110 11101110 11101110
                               { 3, "\xFF\xFF\xFF" },  // Static pass: 0xFFFFFF  11111111 11111111 11111111
                               { 3, "\x92\x49\x24" },  // Static pass: 0x924924  10010010 01001001 00100100
                               { 3, "\x49\x24\x92" },  // Static pass: 0x492492  01001001 00100100 10010010
                               { 3, "\x24\x92\x49" },  // Static pass: 0x249249  00100100 10010010 01001001
                               { 3, "\x6D\xB6\xDB" },  // Static pass: 0x6DB6DB  01101101 10110110 11011011
                               { 3, "\xB6\xDB\x6D" },  // Static pass: 0xB6DB6D  10110110 11011011 01101101
                               { 3, "\xDB\x6D\xB6" },  // Static pass: 0XDB6DB6  11011011 01101101 10110110
                               { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { -1, "" },  // Random pass.
                               { 0, NULL } };

    /* Put the book array into this array in random order. */
    nwipe_pattern_t patterns[36];

    /* An entropy buffer. */
    u16 s[27];

    /* Load the array with random characters. */
    ssize_t r = read( c->entropy_fd, &s, sizeof( s ) );
    if( r != sizeof( s ) )
    {
        r = errno;
        nwipe_perror( r, __FUNCTION__, "read" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the %s method.", nwipe_gutmann_label );

        /* Ensure a negative return. */
        if( r < 0 )
        {
            c->result = r;
            return NULL;
        }
        else
        {
            c->result = -1;
            return NULL;
        }
    }

    // First 4 random passes
    for( int i = 0; i <= 3; ++i )
    {
        patterns[i] = book[i];
    }
    // Middle 27 passes in random order
    for( int i = 26; i >= 0; --i )
    {
        /* Get a random integer that is less than the first index 'i'. */
        int n = (int) ( (double) ( s[i] ) / (double) ( 0x0000FFFF + 1 ) * (double) ( i + 1 ) );

        /* Initialize the secondary index. */
        int j = 3;

        while( n-- >= 0 )
        {
            /* Advance 'j' by 'n' positions... */
            j += 1;

            /* ... but don't count 'book' elements that have already been copied. */
            while( book[j].length == 0 )
            {
                j += 1;
            }
        }

        /* Copy the element. */
        patterns[i + 4] = book[j];

        /* Mark this element as having been used. */
        book[j].length = 0;
    }
    // Last 4 random passes
    for( int i = 31; i <= 34; ++i )
    {
        patterns[i] = book[i];
    }

    /* Ensure that the array is terminated. */
    patterns[35].length = 0;
    patterns[35].s = NULL;

    /* Run the Gutmann method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_gutmann */

void* nwipe_ops2( void* ptr )
{
    /**
     *  Royal Canadian Mounted Police
     *  Technical Security Standard for Information Technology
     *  Appendix OPS-II: Media Sanitization
     *
     *  NOTE: The last pass of this method is specially handled by nwipe_runmethod.
     *
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* A generic array index. */
    int i;

    /* A generic result buffer. */
    int r;

    /* A buffer for random characters. */
    char* s;

    /* A buffer for the bitwise complements of 's'. */
    char* t;

    /* The element count of 's' and 't'. */
    u32 u;

    /* The pattern array for this method is dynamically allocated. */
    nwipe_pattern_t* patterns;

    /* The element count of 'patterns'. */
    u32 q;

    /* We need one random character per round. */
    u = 1 * nwipe_options.rounds;

    /* Allocate the array of random characters. */
    s = malloc( sizeof( char ) * u );

    if( s == NULL )
    {
        nwipe_perror( errno, __FUNCTION__, "malloc" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate the random character array." );
        c->result = -1;
        return NULL;
    }

    /* Allocate the array of complement characters. */
    t = malloc( sizeof( char ) * u );

    if( t == NULL )
    {
        nwipe_perror( errno, __FUNCTION__, "malloc" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate the complement character array." );
        c->result = -1;
        free( s );
        return NULL;
    }

    /* We need eight pattern elements per round, plus one for padding. */
    q = 8 * u + 1;

    /* Allocate the pattern array. */
    patterns = malloc( sizeof( nwipe_pattern_t ) * q );

    if( patterns == NULL )
    {
        nwipe_perror( errno, __FUNCTION__, "malloc" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate the pattern array." );
        c->result = -1;
        free( s );
        free( t );
        return NULL;
    }

    /* Load the array of random characters. */
    r = read( c->entropy_fd, s, u );

    if( r != u )
    {
        r = errno;
        nwipe_perror( r, __FUNCTION__, "read" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the %s method.", nwipe_ops2_label );

        if( r < 0 )
        {
            c->result = r;
            free( s );
            free( t );
            free( patterns );
            return NULL;
        }
        else
        {
            c->result = -1;
            free( s );
            free( t );
            free( patterns );
            return NULL;
        }
    }

    for( i = 0; i < u; i += 1 )
    {
        /* Populate the array of complements. */
        t[i] = ~s[i];
    }

    for( i = 0; i < u; i += 8 )
    {
        /* Populate the array of patterns. */

        /* Even elements point to the random characters. */
        patterns[i * 4 + 0].length = 1;
        patterns[i * 4 + 0].s = &s[i];
        patterns[i * 4 + 2].length = 1;
        patterns[i * 4 + 2].s = &s[i];
        patterns[i * 4 + 4].length = 1;
        patterns[i * 4 + 4].s = &s[i];
        patterns[i * 4 + 6].length = 1;
        patterns[i * 4 + 6].s = &s[i];

        /* Odd elements point to the complement characters. */
        patterns[i * 4 + 1].length = 1;
        patterns[i * 4 + 1].s = &t[i];
        patterns[i * 4 + 3].length = 1;
        patterns[i * 4 + 3].s = &t[i];
        patterns[i * 4 + 5].length = 1;
        patterns[i * 4 + 5].s = &t[i];
        patterns[i * 4 + 7].length = 1;
        patterns[i * 4 + 7].s = &t[i];
    }

    /* Ensure that the array is terminated. */
    patterns[q - 1].length = 0;
    patterns[q - 1].s = NULL;

    /* Run the TSSIT OPS-II method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Release the random character buffer. */
    free( s );

    /* Release the complement character buffer */
    free( t );

    /* Release the pattern buffer. */
    free( patterns );

    /* We're done. */

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_ops2 */

void* nwipe_is5enh( void* ptr )
{
    nwipe_context_t* c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    c->wipe_status = 1;

    char is5enh[3] = { '\x00', '\xFF', '\x00' };
    nwipe_pattern_t patterns[] = { { 1, &is5enh[0] },  // Pass 1: 0s
                                   { 1, &is5enh[1] },  // Pass 2: 1s
                                   { -1, &is5enh[2] },  // Pass 3: random bytes with verification
                                   { 0, NULL } };
    c->result = nwipe_runmethod( c, patterns );

    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_is5enh */

void* nwipe_random( void* ptr )
{
    /**
     * Fill the device with a stream from the PRNG.
     *
     */

    nwipe_context_t* c;
    c = (nwipe_context_t*) ptr;

    /* get current time at the start of the wipe  */
    time( &c->start_time );

    /* set wipe in progress flag for GUI */
    c->wipe_status = 1;

    /* Define the random method. */
    nwipe_pattern_t patterns[] = { { -1, "" }, { 0, NULL } };

    /* Run the method. */
    c->result = nwipe_runmethod( c, patterns );

    /* Finished. Set the wipe_status flag so that the GUI knows */
    c->wipe_status = 0;

    /* get current time at the end of the wipe  */
    time( &c->end_time );

    return NULL;
} /* nwipe_random */

int nwipe_runmethod( nwipe_context_t* c, nwipe_pattern_t* patterns )
{
    /**
     * Writes patterns to the device.
     *
     */

    /* The result holder. */
    int r;

    /* An index variable. */
    int i = 0;

    /* Variable to track if it is the last pass */
    int lastpass = 0;

    i = 0;

    /* The zero-fill pattern for the final pass of most methods. */
    nwipe_pattern_t pattern_zero = { 1, "\x00" };

    /* The one-fill pattern for verification of the ones fill */
    nwipe_pattern_t pattern_one = { 1, "\xFF" };

    /* Create the PRNG state buffer. */
    c->prng_seed.length = NWIPE_KNOB_PRNG_STATE_LENGTH;
    c->prng_seed.s = malloc( c->prng_seed.length );

    /* Check the memory allocation. */
    if( !c->prng_seed.s )
    {
        nwipe_perror( errno, __FUNCTION__, "malloc" );
        nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the prng seed buffer." );
        return -1;
    }

    /* Count the number of patterns in the array. */
    while( patterns[i].length )
    {
        i += 1;
    }

    /* Tell the parent the number of device passes that will be run in one round. */
    c->pass_count = i;

    /* Set the number of bytes that will be written across all passes in one round. */
    c->pass_size = c->pass_count * c->device_size;

    /* For the selected method, calculate the correct round_size value (for correct percentage calculation) */
    calculate_round_size( c );

    /* If only verifying then the round size is the device size */
    if( nwipe_options.method == &nwipe_verify_zero || nwipe_options.method == &nwipe_verify_one )
    {
        c->round_size = c->device_size;
    }

    /* Initialize the working round counter. */
    c->round_working = 0;

    nwipe_log(
        NWIPE_LOG_NOTICE, "Invoking method '%s' on %s", nwipe_method_label( nwipe_options.method ), c->device_name );

    while( c->round_working < c->round_count )
    {
        /* Increment the round counter. */
        c->round_working += 1;

        nwipe_log(
            NWIPE_LOG_NOTICE, "Starting round %i of %i on %s", c->round_working, c->round_count, c->device_name );

        /* Initialize the working pass counter. */
        c->pass_working = 0;

        for( i = 0; i < c->pass_count; i++ )
        {
            /* Increment the working pass. */
            c->pass_working += 1;

            /* Check if this is the last pass. */
            if( nwipe_options.verify == NWIPE_VERIFY_LAST && nwipe_options.method != &nwipe_ops2 )
            {
                if( nwipe_options.noblank == 1 && c->round_working == c->round_count
                    && c->pass_working == c->pass_count )
                {
                    lastpass = 1;
                }
            }

            nwipe_log( NWIPE_LOG_NOTICE,
                       "Starting pass %i/%i, round %i/%i, on %s",
                       c->pass_working,
                       c->pass_count,
                       c->round_working,
                       c->round_count,
                       c->device_name );

            if( patterns[i].length == 0 )
            {
                /* Caught insanity. */
                nwipe_log( NWIPE_LOG_SANITY, "nwipe_runmethod: A non-terminating pattern element has zero length." );
                return -1;
            }

            if( patterns[i].length > 0 )
            {

                /* Write a static pass. */
                c->pass_type = NWIPE_PASS_WRITE;
                r = nwipe_static_pass( c, &patterns[i] );
                c->pass_type = NWIPE_PASS_NONE;

                /* Log number of bytes written to disk */
                nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes written to %s", c->pass_done, c->device_name );

                /* Check for a fatal error. */
                if( r < 0 )
                {
                    return r;
                }

                if( nwipe_options.verify == NWIPE_VERIFY_ALL || lastpass == 1 )
                {

                    nwipe_log( NWIPE_LOG_NOTICE,
                               "Verifying pass %i of %i, round %i of %i, on %s",
                               c->pass_working,
                               c->pass_count,
                               c->round_working,
                               c->round_count,
                               c->device_name );

                    /* Verify this pass. */
                    c->pass_type = NWIPE_PASS_VERIFY;
                    r = nwipe_static_verify( c, &patterns[i] );
                    c->pass_type = NWIPE_PASS_NONE;

                    nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes read from %s", c->pass_done, c->device_name );

                    /* Check for a fatal error. */
                    if( r < 0 )
                    {
                        return r;
                    }

                    nwipe_log( NWIPE_LOG_NOTICE,
                               "Verified pass %i of %i, round %i of %i, on '%s'.",
                               c->pass_working,
                               c->pass_count,
                               c->round_working,
                               c->round_count,
                               c->device_name );
                }

            } /* static pass */

            else
            {
                c->pass_type = NWIPE_PASS_WRITE;

                /* Seed the PRNG. */
                r = read( c->entropy_fd, c->prng_seed.s, c->prng_seed.length );

                /* Check the result. */
                if( r < 0 )
                {
                    c->pass_type = NWIPE_PASS_NONE;
                    nwipe_perror( errno, __FUNCTION__, "read" );
                    nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the PRNG." );
                    return -1;
                }

                /* Check for a partial read. */
                if( r != c->prng_seed.length )
                {
                    /* TODO: Handle partial reads. */
                    nwipe_log( NWIPE_LOG_FATAL, "Insufficient entropy is available." );
                    return -1;
                }

                /* Write the random pass. */
                r = nwipe_random_pass( c );
                c->pass_type = NWIPE_PASS_NONE;

                /* Log number of bytes written to disk */
                nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes written to %s", c->pass_done, c->device_name );

                /* Check for a fatal error. */
                if( r < 0 )
                {
                    return r;
                }

                /* Make sure IS5 enhanced always verifies its PRNG pass regardless */
                /* of the current combination of the --noblank (which influences   */
                /* the lastpass variable) and --verify options.                    */
                if( nwipe_options.verify == NWIPE_VERIFY_ALL || lastpass == 1 || nwipe_options.method == &nwipe_is5enh )
                {
                    nwipe_log( NWIPE_LOG_NOTICE,
                               "Verifying pass %i of %i, round %i of %i, on %s",
                               c->pass_working,
                               c->pass_count,
                               c->round_working,
                               c->round_count,
                               c->device_name );

                    /* Verify this pass. */
                    c->pass_type = NWIPE_PASS_VERIFY;
                    r = nwipe_random_verify( c );
                    c->pass_type = NWIPE_PASS_NONE;

                    nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes read from %s", c->pass_done, c->device_name );

                    /* Check for a fatal error. */
                    if( r < 0 )
                    {
                        return r;
                    }

                    nwipe_log( NWIPE_LOG_NOTICE,
                               "Verified pass %i of %i, round %i of %i, on '%s'.",
                               c->pass_working,
                               c->pass_count,
                               c->round_working,
                               c->round_count,
                               c->device_name );
                }

            } /* random pass */

            nwipe_log( NWIPE_LOG_NOTICE,
                       "Finished pass %i/%i, round %i/%i, on %s",
                       c->pass_working,
                       c->pass_count,
                       c->round_working,
                       c->round_count,
                       c->device_name );

        } /* for passes */

        if( c->round_working < c->round_count )
        {
            nwipe_log(
                NWIPE_LOG_NOTICE, "Finished round %i of %i on %s", c->round_working, c->round_count, c->device_name );
        }
        else
        {
            nwipe_log( NWIPE_LOG_NOTICE,
                       "Finished final round %i of %i on %s",
                       c->round_working,
                       c->round_count,
                       c->device_name );
        }

    } /* while rounds */

    if( nwipe_options.method == &nwipe_ops2 )
    {
        /* NOTE: The OPS-II method specifically requires that a random pattern be left on the device. */

        /* Tell the parent that we are running the final pass. */
        c->pass_type = NWIPE_PASS_FINAL_OPS2;

        /* Seed the PRNG. */
        r = read( c->entropy_fd, c->prng_seed.s, c->prng_seed.length );

        /* Check the result. */
        if( r < 0 )
        {
            nwipe_perror( errno, __FUNCTION__, "read" );
            nwipe_log( NWIPE_LOG_FATAL, "Unable to seed the PRNG." );
            return -1;
        }

        /* Check for a partial read. */
        if( r != c->prng_seed.length )
        {
            /* TODO: Handle partial reads. */
            nwipe_log( NWIPE_LOG_FATAL, "Insufficient entropy is available." );
            return -1;
        }

        nwipe_log( NWIPE_LOG_NOTICE, "Writing final random pattern to '%s'.", c->device_name );

        /* The final ops2 pass. */
        r = nwipe_random_pass( c );

        nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes written to %s", c->pass_done, c->device_name );

        /* Check for a fatal error. */
        if( r < 0 )
        {
            return r;
        }

        if( nwipe_options.verify == NWIPE_VERIFY_LAST || nwipe_options.verify == NWIPE_VERIFY_ALL )
        {
            nwipe_log( NWIPE_LOG_NOTICE, "Verifying final random pattern FRP on %s", c->device_name );

            /* Verify the final zero pass. */
            r = nwipe_random_verify( c );

            nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes read from %s", c->pass_done, c->device_name );

            /* Check for a fatal error. */
            if( r < 0 )
            {
                return r;
            }

            nwipe_log( NWIPE_LOG_NOTICE, "[SUCCESS] Verified FRP on '%s' matches", c->device_name );
        }

    } /* final ops2 */

    else if( nwipe_options.method == &nwipe_verify_zero )
    {
        nwipe_log( NWIPE_LOG_NOTICE, "Verifying that %s is zeroed", c->device_name );

        /* Verify the final zero pass. */
        c->pass_type = NWIPE_PASS_VERIFY;
        r = nwipe_static_verify( c, &pattern_zero );
        c->pass_type = NWIPE_PASS_NONE;

        /* Check for a fatal error. */
        if( r < 0 )
        {
            return r;
        }
        if( c->verify_errors == 0 )
        {
            nwipe_log( NWIPE_LOG_NOTICE, "[SUCCESS] Verified that %s is Zeroed.", c->device_name );
        }
        else
        {
            nwipe_log( NWIPE_LOG_ERROR, "[FAILURE] %s has not been Zeroed .", c->device_name );
        }

    } /* verify */

    else if( nwipe_options.method == &nwipe_verify_one )
    {
        nwipe_log( NWIPE_LOG_NOTICE, "Verifying that %s is Ones (0xFF)", c->device_name );

        /* Verify the final ones pass. */
        c->pass_type = NWIPE_PASS_VERIFY;
        r = nwipe_static_verify( c, &pattern_one );
        c->pass_type = NWIPE_PASS_NONE;

        /* Check for a fatal error. */
        if( r < 0 )
        {
            return r;
        }
        if( c->verify_errors == 0 )
        {
            nwipe_log( NWIPE_LOG_NOTICE, "[SUCCESS] Verified that %s is full of ones (0xFF).", c->device_name );
        }
        else
        {
            nwipe_log( NWIPE_LOG_ERROR, "[FAILURE] %s is not full of ones (0xFF).", c->device_name );
        }

    } /* verify */

    else if( nwipe_options.noblank == 0 )
    {
        /* Tell the user that we are on the final pass. */
        c->pass_type = NWIPE_PASS_FINAL_BLANK;

        nwipe_log( NWIPE_LOG_NOTICE, "Blanking device %s", c->device_name );

        /* The final zero pass. */
        r = nwipe_static_pass( c, &pattern_zero );

        /* Log number of bytes written to disk */
        nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes written to %s", c->pass_done, c->device_name );

        /* Check for a fatal error. */
        if( r < 0 )
        {
            return r;
        }

        if( nwipe_options.verify == NWIPE_VERIFY_LAST || nwipe_options.verify == NWIPE_VERIFY_ALL )
        {
            nwipe_log( NWIPE_LOG_NOTICE, "Verifying that %s is empty.", c->device_name );

            /* Verify the final zero pass. */
            c->pass_type = NWIPE_PASS_VERIFY;
            r = nwipe_static_verify( c, &pattern_zero );
            c->pass_type = NWIPE_PASS_NONE;

            /* Log number of bytes read from disk */
            nwipe_log( NWIPE_LOG_NOTICE, "%llu bytes read from %s", c->pass_done, c->device_name );

            /* Check for a fatal error. */
            if( r < 0 )
            {
                return r;
            }

            if( c->verify_errors == 0 )
            {
                nwipe_log( NWIPE_LOG_NOTICE, "[SUCCESS] Verified that %s is empty.", c->device_name );
            }
            else
            {
                nwipe_log( NWIPE_LOG_NOTICE, "[FAILURE] %s Verification errors, not empty", c->device_name );
            }
        }

        if( c->verify_errors == 0 && c->pass_errors == 0 )
        {
            nwipe_log( NWIPE_LOG_NOTICE, "[SUCCESS] Blanked device %s", c->device_name );
        }
        else
        {
            nwipe_log( NWIPE_LOG_NOTICE, "[FAILURE] %s may not be blanked", c->device_name );
        }

    } /* final blank */

    /* Release the state buffer. */
    c->prng_seed.length = 0;
    free( c->prng_seed.s );

    /* Tell the parent that we have fininshed the final pass. */
    c->pass_type = NWIPE_PASS_NONE;

    if( c->verify_errors > 0 )
    {
        /* We finished, but with non-fatal verification errors. */
        nwipe_log( NWIPE_LOG_ERROR, "%llu verification errors on '%s'.", c->verify_errors, c->device_name );
    }

    if( c->pass_errors > 0 )
    {
        /* We finished, but with non-fatal wipe errors. */
        nwipe_log( NWIPE_LOG_ERROR, "%llu wipe errors on '%s'.", c->pass_errors, c->device_name );
    }

    /* FIXME: The 'round_errors' context member is not being used. */

    if( c->pass_errors > 0 || c->round_errors > 0 || c->verify_errors > 0 )
    {
        /* We finished, but with non-fatal errors. */
        return 1;
    }

    /* We finished successfully. */
    return 0;

} /* nwipe_runmethod */

void calculate_round_size( nwipe_context_t* c )
{
    /* This is where the round size is calculated. round_size is used in the running percentage completion
     * calculation. round size is calculated based on pass_size, pass_count, number of rounds, blanking
     * on/off and verification All/Last/None
     *
     * To hopefully make this calculation more understandable, I have separated the calculations that apply to
     * all methods and processed first then created a switch statement that contains method specific changes if any
     */

    /* Don't change the order of these values as the case statements use their index in the array */
    void* array_methods[] = { &nwipe_zero,
                              &nwipe_ops2,
                              &nwipe_dodshort,
                              &nwipe_dod522022m,
                              &nwipe_gutmann,
                              &nwipe_random,
                              &nwipe_is5enh,
                              NULL };
    int i;

    /* This while loop allows us to effectively create a const so we can use a case statement rather than if statements.
     * This is probably more readable as more methods may get added in the future. The code could be condensed as some
     * methods have identical adjustments, however as there are only a few methods I felt it was easier to understand as
     * it is, however this could be changed if necessary.
     */

    int selected_method;
    i = 0;
    while( array_methods[i] != NULL )
    {
        if( nwipe_options.method == array_methods[i] )
        {
            selected_method = i;
        }
        i++;
    }

    if( nwipe_options.verify == NWIPE_VERIFY_ALL )
    {
        /* We must read back all passes, so double the byte count. */
        c->pass_size *= 2;
    }

    /* Tell the parent the number of rounds that will be run. */
    c->round_count = nwipe_options.rounds;

    /* Set the initial number of bytes that will be written across all rounds.
       c->pass_size includes write AND verification passes if 'verify_all' is selected
       but does not include the final blanking pass or the verify_last option */
    c->round_size = c->pass_size;

    /* Multiple the round_size by the number of rounds (times) the user wants to wipe the drive with this method. */
    c->round_size *= c->round_count;

    /* Now increase size based on whether blanking is enabled and verification */
    if( nwipe_options.noblank == 0 )
    {
        /* Blanking enabled so increase round size */
        c->round_size += c->device_size;

        if( nwipe_options.verify == NWIPE_VERIFY_LAST || nwipe_options.verify == NWIPE_VERIFY_ALL )
        {
            c->round_size += c->device_size;
        }
    }
    else
    {
        /* Blanking not enabled, check for 'Verify_last', increase round size if enabled. */
        if( nwipe_options.verify == NWIPE_VERIFY_LAST )
        {
            c->round_size += c->device_size;
        }
    }

    /* Additional method specific round_size adjustments go in this switch statement */

    switch( selected_method )
    {
        case 0:
            /* NWIPE_ZERO - No additional calculation required
             * ---------- */
            break;

        case 1:
            /* NWIPE_OPS2
             * ---------- */

            /* Required for mandatory 9th and final random pass */
            c->round_size += c->device_size;

            /* Required for selectable 9th and final random verification */
            if( nwipe_options.verify == NWIPE_VERIFY_ALL || nwipe_options.verify == NWIPE_VERIFY_LAST )
            {
                c->round_size += c->device_size;
            }

            /* As no final zero blanking pass is permitted by this standard reduce round size if it's selected */
            if( nwipe_options.noblank == 0 )
            {
                /* Reduce for blanking pass */
                c->round_size -= c->device_size;

                /* Reduce for blanking pass verification */
                if( nwipe_options.verify == NWIPE_VERIFY_ALL || nwipe_options.verify == NWIPE_VERIFY_LAST )
                {
                    c->round_size -= c->device_size;
                }
            }
            else
            {
                if( nwipe_options.verify == NWIPE_VERIFY_LAST )
                {
                    /* If blanking off & verification on reduce round size */
                    c->round_size -= c->device_size;
                }
            }

            break;

        case 2:
            /* DoD Short - No additional calculation required
             * --------- */

            break;

        case 3:
            /* DOD 522022m - No additional calculation required
             * ----------- */

            break;

        case 4:
            /* GutMann - No additional calculation required
             * ------- */

            break;

        case 5:
            /* PRNG (random) - No additional calculation required
             * ------------- */

            break;

        case 6:
            /* NWIPE_IS5ENH
             * ------------ */

            /* This method ALWAYS verifies the 3rd pass so increase by device size,
             * but NOT if VERIFY_ALL has been selected, but first .. */

            /* Reduce as Verify_Last already included previously if blanking was off */
            if( nwipe_options.verify == NWIPE_VERIFY_LAST && nwipe_options.noblank == 1 )
            {
                c->round_size -= c->device_size;
            }

            /* Adjusts for verify on every third pass multiplied by number of rounds */
            if( nwipe_options.verify != NWIPE_VERIFY_ALL )
            {
                c->round_size += ( c->device_size * c->round_count );
            }

            break;
    }
}

/* eof */