forked from CruiserX/sha256_plsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_util_pkg.pkb
1390 lines (1309 loc) · 49.1 KB
/
hash_util_pkg.pkb
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
create or replace package body hash_util_pkg
as
type ta_number is table of number index by binary_integer;
type tr_ctx is record (
h ta_number,
total_length number,
leftover_buffer raw(256),
leftover_buffer_length number,
words_array ta_number
);
m_ctx tr_ctx;
m_k ta_number;
m_result ta_number;
-- Constants for message padding
c_bits_00 raw(1) := hextoraw('00');
c_bits_80 raw(1) := hextoraw('80');
-- Constant for 32bit bitwise operations
c_bits_80000000 number := to_number('80000000','xxxxxxxx');
c_bits_ffffffc0 number := to_number('FFFFFFC0','xxxxxxxx');
c_bits_ffffffff number := to_number('FFFFFFFF','xxxxxxxx');
-- Constant for 64bit bitwise operations
c_bits_8000000000000000 number := to_number('8000000000000000','xxxxxxxxxxxxxxxx');
c_bits_ffffffffffffff80 number := to_number('FFFFFFFFFFFFFF80','xxxxxxxxxxxxxxxx');
c_bits_ffffffffffffffff number := to_number('FFFFFFFFFFFFFFFF','xxxxxxxxxxxxxxxx');
---
--- Bitwise operators
---
function bitor(x in number, y in number) return number as
begin
return (x + y - bitand(x, y));
end;
function bitxor(x in number, y in number) return number as
begin
return bitor(x, y) - bitand(x, y);
end;
function bitnot32(x in number) return number as
begin
return c_bits_ffffffff - x;
end;
function leftshift32(x in number, y in number) return number as
tmp number := x;
begin
for idx in 1..y
loop
tmp := tmp * 2;
end loop;
return bitand(tmp, c_bits_ffffffff);
end;
function rightshift32(x in number, y in number) return number as
tmp number := x;
begin
for idx in 1..y
loop
tmp := trunc(tmp / 2);
end loop;
return bitand(tmp, c_bits_ffffffff);
end;
function cyclic32(x in number, y in number) return number as
begin
return bitor(rightshift32(x, y), leftshift32(x, 32-y));
end;
function bitnot64(x in number) return number as
begin
return c_bits_ffffffffffffffff - x;
end;
function leftshift64(x in number, y in number) return number as
tmp number := x;
begin
for idx in 1..y
loop
tmp := tmp * 2;
end loop;
return bitand(tmp, c_bits_ffffffffffffffff);
end;
function rightshift64(x in number, y in number) return number as
tmp number := x;
begin
for idx in 1..y
loop
tmp := trunc(tmp / 2);
end loop;
return bitand(tmp, c_bits_ffffffffffffffff);
end;
function cyclic64(x in number, y in number) return number as
begin
return bitor(rightshift64(x, y), leftshift64(x, 64-y));
end;
---
--- Operators defined in FIPS 180-2:4.1.2.
---
function op_maj(x in number, y in number, z in number) return number as
begin
return bitxor(bitxor(bitand(x,y), bitand(x,z)), bitand(y,z));
end;
function op_ch_32(x in number, y in number, z in number) return number as
begin
return bitxor(bitand(x, y), bitand(bitnot32(x), z));
end;
function op_s0_32(x in number) return number as
begin
return bitxor(bitxor(cyclic32(x, 2), cyclic32(x, 13)), cyclic32(x, 22));
end;
function op_s1_32(x in number) return number as
begin
return bitxor(bitxor(cyclic32(x, 6), cyclic32(x, 11)), cyclic32(x, 25));
end;
function op_r0_32(x in number) return number as
begin
return bitxor(bitxor(cyclic32(x, 7), cyclic32(x, 18)), rightshift32(x, 3));
end;
function op_r1_32(x in number) return number as
begin
return bitxor(bitxor(cyclic32(x, 17), cyclic32(x, 19)), rightshift32(x, 10));
end;
function op_ch_64(x in number, y in number, z in number) return number as
begin
return bitxor(bitand(x, y), bitand(bitnot64(x), z));
end;
function op_s0_64(x in number) return number as
begin
return bitxor(bitxor(cyclic64(x, 28), cyclic64(x, 34)), cyclic64(x, 39));
end;
function op_s1_64(x in number) return number as
begin
return bitxor(bitxor(cyclic64(x, 14), cyclic64(x, 18)), cyclic64(x, 41));
end;
function op_r0_64(x in number) return number as
begin
return bitxor(bitxor(cyclic64(x, 1), cyclic64(x, 8)), rightshift64(x, 7));
end;
function op_r1_64(x in number) return number as
begin
return bitxor(bitxor(cyclic64(x, 19), cyclic64(x, 61)), rightshift64(x, 6));
end;
--
-- SHA-1
--
procedure sha1_init_ctx
as
begin
m_ctx.h(0) := to_number('67452301', 'xxxxxxxx');
m_ctx.h(1) := to_number('efcdab89', 'xxxxxxxx');
m_ctx.h(2) := to_number('98badcfe', 'xxxxxxxx');
m_ctx.h(3) := to_number('10325476', 'xxxxxxxx');
m_ctx.h(4) := to_number('c3d2e1f0', 'xxxxxxxx');
m_ctx.total_length := 0;
m_ctx.leftover_buffer := null;
m_ctx.leftover_buffer_length := 0;
for idx in 0..15 loop
m_ctx.words_array(idx) := 0;
end loop;
end sha1_init_ctx;
procedure sha1_process_block(p_words_array in ta_number,
p_words_count in number)
as
l_words_array ta_number := p_words_array;
l_words_count number := p_words_count;
l_words_idx number;
t number;
a number := m_ctx.h(0);
b number := m_ctx.h(1);
c number := m_ctx.h(2);
d number := m_ctx.h(3);
e number := m_ctx.h(4);
w ta_number;
a_save number;
b_save number;
c_save number;
d_save number;
e_save number;
f number;
k number;
temp number;
begin
-- Process all bytes in the buffer with 64 bytes in each round of the loop.
l_words_idx := 0;
while (l_words_count > 0) loop
a_save := a;
b_save := b;
c_save := c;
d_save := d;
e_save := e;
for t in 0..15 loop
w(t) := l_words_array(l_words_idx);
l_words_idx := l_words_idx + 1;
end loop;
for t in 16..79 loop
w(t) := cyclic32(bitxor(bitxor(bitxor(w(t-3), w(t-8)), w(t-14)), w(t-16)), 32-1);
end loop;
for t in 0..79 loop
if t between 0 and 19 then
f := bitor(bitand(b, c), bitand(bitnot32(b), d));
k := to_number('5a827999', 'xxxxxxxx');
elsif t between 20 and 39 then
f := bitxor(bitxor(b, c), d);
k := to_number('6ed9eba1', 'xxxxxxxx');
elsif t between 40 and 59 then
f := bitor(bitor(bitand(b, c), bitand(b, d)), bitand(c, d));
k := to_number('8f1bbcdc', 'xxxxxxxx');
elsif t between 60 and 79 then
f := bitxor(bitxor(b, c), d);
k := to_number('ca62c1d6', 'xxxxxxxx');
end if;
temp := bitand(cyclic32(a, 32-5) + f + e + k + w(t), c_bits_ffffffff);
e := d;
d := c;
c := cyclic32(b, 32-30);
b := a;
a := temp;
end loop;
a := bitand(a + a_save, c_bits_ffffffff);
b := bitand(b + b_save, c_bits_ffffffff);
c := bitand(c + c_save, c_bits_ffffffff);
d := bitand(d + d_save, c_bits_ffffffff);
e := bitand(e + e_save, c_bits_ffffffff);
-- Prepare for the next round.
l_words_count := l_words_count - 16;
end loop;
-- Put checksum in context given as argument.
m_ctx.h(0) := a;
m_ctx.h(1) := b;
m_ctx.h(2) := c;
m_ctx.h(3) := d;
m_ctx.h(4) := e;
end sha1_process_block;
procedure sha1_process_bytes(p_buffer in raw,
p_buffer_length in number)
as
l_buffer raw(16640);
l_buffer_length number;
l_words_array ta_number;
begin
-- First increment the byte count. FIPS 180-2 specifies the possible
-- length of the file up to 2^64 bits. Here we only compute the number of
-- bytes.
m_ctx.total_length := m_ctx.total_length + nvl(p_buffer_length, 0);
-- When we already have some bits in our internal buffer concatenate both inputs first.
if (m_ctx.leftover_buffer_length = 0) then
l_buffer := p_buffer;
l_buffer_length := nvl(p_buffer_length, 0);
else
l_buffer := m_ctx.leftover_buffer || p_buffer;
l_buffer_length := m_ctx.leftover_buffer_length + nvl(p_buffer_length, 0);
end if;
-- Process available complete blocks.
if (l_buffer_length >= 64) then
declare
l_words_count number := bitand(l_buffer_length, c_bits_ffffffc0) / 4;
l_max_idx number := l_words_count - 1;
l_numberraw raw(4);
l_numberhex varchar(8);
l_number number;
begin
for idx in 0..l_max_idx loop
l_numberraw := sys.utl_raw.substr(l_buffer, idx * 4 + 1, 4);
l_numberhex := rawtohex(l_numberraw);
l_number := to_number(l_numberhex, 'xxxxxxxx');
l_words_array(idx) := l_number;
end loop;
sha1_process_block(l_words_array, l_words_count);
l_buffer_length := bitand(l_buffer_length, 63);
if (l_buffer_length > 0) then
l_buffer := sys.utl_raw.substr(l_buffer, l_words_count * 4 + 1, l_buffer_length);
end if;
end;
end if;
-- Move remaining bytes into internal buffer.
if (l_buffer_length > 0) then
m_ctx.leftover_buffer := l_buffer;
m_ctx.leftover_buffer_length := l_buffer_length;
end if;
end sha1_process_bytes;
procedure sha1_finish_ctx(p_resultbuf out nocopy ta_number)
as
l_filesizeraw raw(8);
begin
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_80;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
while ((m_ctx.leftover_buffer_length mod 64) <> 56) loop
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_00;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
end loop;
l_filesizeraw := hextoraw(to_char(m_ctx.total_length * 8, 'FM0xxxxxxxxxxxxxxx'));
m_ctx.leftover_buffer := m_ctx.leftover_buffer || l_filesizeraw;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 8;
sha1_process_bytes(null, 0);
for idx in 0..4 loop
p_resultbuf(idx) := m_ctx.h(idx);
end loop;
end sha1_finish_ctx;
function sha1(p_buffer in raw) return sha1_checksum_raw
as
l_result sha1_checksum_raw;
begin
sha1_init_ctx;
sha1_process_bytes(p_buffer, sys.utl_raw.length(p_buffer));
sha1_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0), 'FM0xxxxxxx') ||
to_char(m_result(1), 'FM0xxxxxxx') ||
to_char(m_result(2), 'FM0xxxxxxx') ||
to_char(m_result(3), 'FM0xxxxxxx') ||
to_char(m_result(4), 'FM0xxxxxxx')
);
return l_result;
end sha1;
function sha1(p_buffer in blob) return sha1_checksum_raw
as
l_result sha1_checksum_raw;
l_buffer raw(16384);
l_amount number := 16384;
l_offset number := 1;
begin
sha1_init_ctx;
begin
loop
sys.dbms_lob.read(p_buffer, l_amount, l_offset, l_buffer);
sha1_process_bytes(l_buffer, l_amount);
l_offset := l_offset + l_amount;
l_amount := 16384;
end loop;
exception
when no_data_found then
null;
end;
sha1_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0), 'FM0xxxxxxx') ||
to_char(m_result(1), 'FM0xxxxxxx') ||
to_char(m_result(2), 'FM0xxxxxxx') ||
to_char(m_result(3), 'FM0xxxxxxx') ||
to_char(m_result(4), 'FM0xxxxxxx')
);
return l_result;
end sha1;
--
-- SHA-256
--
procedure sha256_init_k
as
begin
m_k(0) := to_number('428a2f98', 'xxxxxxxx');
m_k(1) := to_number('71374491', 'xxxxxxxx');
m_k(2) := to_number('b5c0fbcf', 'xxxxxxxx');
m_k(3) := to_number('e9b5dba5', 'xxxxxxxx');
m_k(4) := to_number('3956c25b', 'xxxxxxxx');
m_k(5) := to_number('59f111f1', 'xxxxxxxx');
m_k(6) := to_number('923f82a4', 'xxxxxxxx');
m_k(7) := to_number('ab1c5ed5', 'xxxxxxxx');
m_k(8) := to_number('d807aa98', 'xxxxxxxx');
m_k(9) := to_number('12835b01', 'xxxxxxxx');
m_k(10) := to_number('243185be', 'xxxxxxxx');
m_k(11) := to_number('550c7dc3', 'xxxxxxxx');
m_k(12) := to_number('72be5d74', 'xxxxxxxx');
m_k(13) := to_number('80deb1fe', 'xxxxxxxx');
m_k(14) := to_number('9bdc06a7', 'xxxxxxxx');
m_k(15) := to_number('c19bf174', 'xxxxxxxx');
m_k(16) := to_number('e49b69c1', 'xxxxxxxx');
m_k(17) := to_number('efbe4786', 'xxxxxxxx');
m_k(18) := to_number('0fc19dc6', 'xxxxxxxx');
m_k(19) := to_number('240ca1cc', 'xxxxxxxx');
m_k(20) := to_number('2de92c6f', 'xxxxxxxx');
m_k(21) := to_number('4a7484aa', 'xxxxxxxx');
m_k(22) := to_number('5cb0a9dc', 'xxxxxxxx');
m_k(23) := to_number('76f988da', 'xxxxxxxx');
m_k(24) := to_number('983e5152', 'xxxxxxxx');
m_k(25) := to_number('a831c66d', 'xxxxxxxx');
m_k(26) := to_number('b00327c8', 'xxxxxxxx');
m_k(27) := to_number('bf597fc7', 'xxxxxxxx');
m_k(28) := to_number('c6e00bf3', 'xxxxxxxx');
m_k(29) := to_number('d5a79147', 'xxxxxxxx');
m_k(30) := to_number('06ca6351', 'xxxxxxxx');
m_k(31) := to_number('14292967', 'xxxxxxxx');
m_k(32) := to_number('27b70a85', 'xxxxxxxx');
m_k(33) := to_number('2e1b2138', 'xxxxxxxx');
m_k(34) := to_number('4d2c6dfc', 'xxxxxxxx');
m_k(35) := to_number('53380d13', 'xxxxxxxx');
m_k(36) := to_number('650a7354', 'xxxxxxxx');
m_k(37) := to_number('766a0abb', 'xxxxxxxx');
m_k(38) := to_number('81c2c92e', 'xxxxxxxx');
m_k(39) := to_number('92722c85', 'xxxxxxxx');
m_k(40) := to_number('a2bfe8a1', 'xxxxxxxx');
m_k(41) := to_number('a81a664b', 'xxxxxxxx');
m_k(42) := to_number('c24b8b70', 'xxxxxxxx');
m_k(43) := to_number('c76c51a3', 'xxxxxxxx');
m_k(44) := to_number('d192e819', 'xxxxxxxx');
m_k(45) := to_number('d6990624', 'xxxxxxxx');
m_k(46) := to_number('f40e3585', 'xxxxxxxx');
m_k(47) := to_number('106aa070', 'xxxxxxxx');
m_k(48) := to_number('19a4c116', 'xxxxxxxx');
m_k(49) := to_number('1e376c08', 'xxxxxxxx');
m_k(50) := to_number('2748774c', 'xxxxxxxx');
m_k(51) := to_number('34b0bcb5', 'xxxxxxxx');
m_k(52) := to_number('391c0cb3', 'xxxxxxxx');
m_k(53) := to_number('4ed8aa4a', 'xxxxxxxx');
m_k(54) := to_number('5b9cca4f', 'xxxxxxxx');
m_k(55) := to_number('682e6ff3', 'xxxxxxxx');
m_k(56) := to_number('748f82ee', 'xxxxxxxx');
m_k(57) := to_number('78a5636f', 'xxxxxxxx');
m_k(58) := to_number('84c87814', 'xxxxxxxx');
m_k(59) := to_number('8cc70208', 'xxxxxxxx');
m_k(60) := to_number('90befffa', 'xxxxxxxx');
m_k(61) := to_number('a4506ceb', 'xxxxxxxx');
m_k(62) := to_number('bef9a3f7', 'xxxxxxxx');
m_k(63) := to_number('c67178f2', 'xxxxxxxx');
end sha256_init_k;
procedure sha256_init_ctx
as
begin
m_ctx.h(0) := to_number('6a09e667', 'xxxxxxxx');
m_ctx.h(1) := to_number('bb67ae85', 'xxxxxxxx');
m_ctx.h(2) := to_number('3c6ef372', 'xxxxxxxx');
m_ctx.h(3) := to_number('a54ff53a', 'xxxxxxxx');
m_ctx.h(4) := to_number('510e527f', 'xxxxxxxx');
m_ctx.h(5) := to_number('9b05688c', 'xxxxxxxx');
m_ctx.h(6) := to_number('1f83d9ab', 'xxxxxxxx');
m_ctx.h(7) := to_number('5be0cd19', 'xxxxxxxx');
m_ctx.total_length := 0;
m_ctx.leftover_buffer := null;
m_ctx.leftover_buffer_length := 0;
for idx in 0..15 loop
m_ctx.words_array(idx) := 0;
end loop;
end sha256_init_ctx;
procedure sha256_process_block(p_words_array in ta_number,
p_words_count in number)
as
l_words_array ta_number := p_words_array;
l_words_count number := p_words_count;
l_words_idx number;
t number;
a number := m_ctx.h(0);
b number := m_ctx.h(1);
c number := m_ctx.h(2);
d number := m_ctx.h(3);
e number := m_ctx.h(4);
f number := m_ctx.h(5);
g number := m_ctx.h(6);
h number := m_ctx.h(7);
w ta_number;
a_save number;
b_save number;
c_save number;
d_save number;
e_save number;
f_save number;
g_save number;
h_save number;
t1 number;
t2 number;
begin
-- Process all bytes in the buffer with 64 bytes in each round of the loop.
l_words_idx := 0;
while (l_words_count > 0) loop
a_save := a;
b_save := b;
c_save := c;
d_save := d;
e_save := e;
f_save := f;
g_save := g;
h_save := h;
-- Compute the message schedule according to FIPS 180-2:6.2.2 step 2.
for t in 0..15 loop
w(t) := l_words_array(l_words_idx);
l_words_idx := l_words_idx + 1;
end loop;
for t in 16..63 loop
w(t) := bitand(op_r1_32(w(t - 2)) + w(t - 7) + op_r0_32(w(t - 15)) + w(t - 16), c_bits_ffffffff);
end loop;
-- The actual computation according to FIPS 180-2:6.2.2 step 3.
for t in 0..63 loop
t1 := bitand(h + op_s1_32(e) + op_ch_32(e, f, g) + m_k(t) + w(t), c_bits_ffffffff);
t2 := bitand(op_s0_32(a) + op_maj(a, b, c), c_bits_ffffffff);
h := g;
g := f;
f := e;
e := bitand(d + t1, c_bits_ffffffff);
d := c;
c := b;
b := a;
a := bitand(t1 + t2, c_bits_ffffffff);
end loop;
-- Add the starting values of the context according to FIPS 180-2:6.2.2 step 4.
a := bitand(a + a_save, c_bits_ffffffff);
b := bitand(b + b_save, c_bits_ffffffff);
c := bitand(c + c_save, c_bits_ffffffff);
d := bitand(d + d_save, c_bits_ffffffff);
e := bitand(e + e_save, c_bits_ffffffff);
f := bitand(f + f_save, c_bits_ffffffff);
g := bitand(g + g_save, c_bits_ffffffff);
h := bitand(h + h_save, c_bits_ffffffff);
-- Prepare for the next round.
l_words_count := l_words_count - 16;
end loop;
-- Put checksum in context given as argument.
m_ctx.h(0) := a;
m_ctx.h(1) := b;
m_ctx.h(2) := c;
m_ctx.h(3) := d;
m_ctx.h(4) := e;
m_ctx.h(5) := f;
m_ctx.h(6) := g;
m_ctx.h(7) := h;
end sha256_process_block;
procedure sha256_process_bytes(p_buffer in raw,
p_buffer_length in number)
as
l_buffer raw(16640);
l_buffer_length number;
l_words_array ta_number;
begin
-- First increment the byte count. FIPS 180-2 specifies the possible
-- length of the file up to 2^64 bits. Here we only compute the number of
-- bytes.
m_ctx.total_length := m_ctx.total_length + nvl(p_buffer_length, 0);
-- When we already have some bits in our internal buffer concatenate both inputs first.
if (m_ctx.leftover_buffer_length = 0) then
l_buffer := p_buffer;
l_buffer_length := nvl(p_buffer_length, 0);
else
l_buffer := m_ctx.leftover_buffer || p_buffer;
l_buffer_length := m_ctx.leftover_buffer_length + nvl(p_buffer_length, 0);
end if;
-- Process available complete blocks.
if (l_buffer_length >= 64) then
declare
l_words_count number := bitand(l_buffer_length, c_bits_ffffffc0) / 4;
l_max_idx number := l_words_count - 1;
l_numberraw raw(4);
l_numberhex varchar2(8);
l_number number;
begin
for idx in 0..l_max_idx loop
l_numberraw := sys.utl_raw.substr(l_buffer, idx * 4 + 1, 4);
l_numberhex := rawtohex(l_numberraw);
l_number := to_number(l_numberhex,'xxxxxxxx');
l_words_array(idx) := l_number;
end loop;
sha256_process_block(l_words_array, l_words_count);
l_buffer_length := bitand(l_buffer_length, 63);
if (l_buffer_length > 0) then
l_buffer := sys.utl_raw.substr(l_buffer, l_words_count * 4 + 1, l_buffer_length);
end if;
end;
end if;
-- Move remaining bytes into internal buffer.
if (l_buffer_length > 0) then
m_ctx.leftover_buffer := l_buffer;
m_ctx.leftover_buffer_length := l_buffer_length;
end if;
end sha256_process_bytes;
procedure sha256_finish_ctx(p_resultbuf out nocopy ta_number)
as
l_filesizeraw raw(8);
begin
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_80;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
while ((m_ctx.leftover_buffer_length mod 64) <> 56) loop
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_00;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
end loop;
l_filesizeraw := hextoraw(to_char(m_ctx.total_length * 8, 'FM0xxxxxxxxxxxxxxx'));
m_ctx.leftover_buffer := m_ctx.leftover_buffer || l_filesizeraw;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 8;
sha256_process_bytes(null, 0);
for idx in 0..7 loop
p_resultbuf(idx) := m_ctx.h(idx);
end loop;
end sha256_finish_ctx;
function sha256(p_buffer in raw) return sha256_checksum_raw
as
l_result sha256_checksum_raw;
begin
sha256_init_k;
sha256_init_ctx;
sha256_process_bytes(p_buffer, sys.utl_raw.length(p_buffer));
sha256_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0),'FM0xxxxxxx') ||
to_char(m_result(1),'FM0xxxxxxx') ||
to_char(m_result(2),'FM0xxxxxxx') ||
to_char(m_result(3),'FM0xxxxxxx') ||
to_char(m_result(4),'FM0xxxxxxx') ||
to_char(m_result(5),'FM0xxxxxxx') ||
to_char(m_result(6),'FM0xxxxxxx') ||
to_char(m_result(7),'FM0xxxxxxx')
);
return l_result;
end sha256;
function sha256(p_buffer in blob) return sha256_checksum_raw
as
l_result sha256_checksum_raw;
l_buffer raw(16384);
l_amount number := 16384;
l_offset number := 1;
begin
sha256_init_k;
sha256_init_ctx;
begin
loop
sys.dbms_lob.read(p_buffer, l_amount, l_offset, l_buffer);
sha256_process_bytes(l_buffer, l_amount);
l_offset := l_offset + l_amount;
l_amount := 16384;
end loop;
exception
when no_data_found then
null;
end;
sha256_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0),'FM0xxxxxxx') ||
to_char(m_result(1),'FM0xxxxxxx') ||
to_char(m_result(2),'FM0xxxxxxx') ||
to_char(m_result(3),'FM0xxxxxxx') ||
to_char(m_result(4),'FM0xxxxxxx') ||
to_char(m_result(5),'FM0xxxxxxx') ||
to_char(m_result(6),'FM0xxxxxxx') ||
to_char(m_result(7),'FM0xxxxxxx')
);
return l_result;
end sha256;
--
-- SHA-224
--
procedure sha224_init_ctx
as
begin
m_ctx.h(0) := to_number('c1059ed8', 'xxxxxxxx');
m_ctx.h(1) := to_number('367cd507', 'xxxxxxxx');
m_ctx.h(2) := to_number('3070dd17', 'xxxxxxxx');
m_ctx.h(3) := to_number('f70e5939', 'xxxxxxxx');
m_ctx.h(4) := to_number('ffc00b31', 'xxxxxxxx');
m_ctx.h(5) := to_number('68581511', 'xxxxxxxx');
m_ctx.h(6) := to_number('64f98fa7', 'xxxxxxxx');
m_ctx.h(7) := to_number('befa4fa4', 'xxxxxxxx');
m_ctx.total_length := 0;
m_ctx.leftover_buffer := null;
m_ctx.leftover_buffer_length := 0;
for idx in 0..15 loop
m_ctx.words_array(idx) := 0;
end loop;
end sha224_init_ctx;
function sha224(p_buffer in raw) return sha224_checksum_raw
as
l_result sha224_checksum_raw;
begin
sha256_init_k;
sha224_init_ctx;
sha256_process_bytes(p_buffer, sys.utl_raw.length(p_buffer));
sha256_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0),'FM0xxxxxxx') ||
to_char(m_result(1),'FM0xxxxxxx') ||
to_char(m_result(2),'FM0xxxxxxx') ||
to_char(m_result(3),'FM0xxxxxxx') ||
to_char(m_result(4),'FM0xxxxxxx') ||
to_char(m_result(5),'FM0xxxxxxx') ||
to_char(m_result(6),'FM0xxxxxxx')
);
return l_result;
end sha224;
function sha224(p_buffer in blob) return sha224_checksum_raw
as
l_result sha224_checksum_raw;
l_buffer raw(16384);
l_amount number := 16384;
l_offset number := 1;
begin
sha256_init_k;
sha224_init_ctx;
begin
loop
sys.dbms_lob.read(p_buffer, l_amount, l_offset, l_buffer);
sha256_process_bytes(l_buffer, l_amount);
l_offset := l_offset + l_amount;
l_amount := 16384;
end loop;
exception
when no_data_found then
null;
end;
sha256_finish_ctx(m_result);
l_result := hextoraw(
to_char(m_result(0),'FM0xxxxxxx') ||
to_char(m_result(1),'FM0xxxxxxx') ||
to_char(m_result(2),'FM0xxxxxxx') ||
to_char(m_result(3),'FM0xxxxxxx') ||
to_char(m_result(4),'FM0xxxxxxx') ||
to_char(m_result(5),'FM0xxxxxxx') ||
to_char(m_result(6),'FM0xxxxxxx')
);
return l_result;
end sha224;
--
-- SHA-512
--
procedure sha512_init_k
as
begin
m_k(0) := to_number('428a2f98d728ae22', 'xxxxxxxxxxxxxxxx');
m_k(1) := to_number('7137449123ef65cd', 'xxxxxxxxxxxxxxxx');
m_k(2) := to_number('b5c0fbcfec4d3b2f', 'xxxxxxxxxxxxxxxx');
m_k(3) := to_number('e9b5dba58189dbbc', 'xxxxxxxxxxxxxxxx');
m_k(4) := to_number('3956c25bf348b538', 'xxxxxxxxxxxxxxxx');
m_k(5) := to_number('59f111f1b605d019', 'xxxxxxxxxxxxxxxx');
m_k(6) := to_number('923f82a4af194f9b', 'xxxxxxxxxxxxxxxx');
m_k(7) := to_number('ab1c5ed5da6d8118', 'xxxxxxxxxxxxxxxx');
m_k(8) := to_number('d807aa98a3030242', 'xxxxxxxxxxxxxxxx');
m_k(9) := to_number('12835b0145706fbe', 'xxxxxxxxxxxxxxxx');
m_k(10) := to_number('243185be4ee4b28c', 'xxxxxxxxxxxxxxxx');
m_k(11) := to_number('550c7dc3d5ffb4e2', 'xxxxxxxxxxxxxxxx');
m_k(12) := to_number('72be5d74f27b896f', 'xxxxxxxxxxxxxxxx');
m_k(13) := to_number('80deb1fe3b1696b1', 'xxxxxxxxxxxxxxxx');
m_k(14) := to_number('9bdc06a725c71235', 'xxxxxxxxxxxxxxxx');
m_k(15) := to_number('c19bf174cf692694', 'xxxxxxxxxxxxxxxx');
m_k(16) := to_number('e49b69c19ef14ad2', 'xxxxxxxxxxxxxxxx');
m_k(17) := to_number('efbe4786384f25e3', 'xxxxxxxxxxxxxxxx');
m_k(18) := to_number('0fc19dc68b8cd5b5', 'xxxxxxxxxxxxxxxx');
m_k(19) := to_number('240ca1cc77ac9c65', 'xxxxxxxxxxxxxxxx');
m_k(20) := to_number('2de92c6f592b0275', 'xxxxxxxxxxxxxxxx');
m_k(21) := to_number('4a7484aa6ea6e483', 'xxxxxxxxxxxxxxxx');
m_k(22) := to_number('5cb0a9dcbd41fbd4', 'xxxxxxxxxxxxxxxx');
m_k(23) := to_number('76f988da831153b5', 'xxxxxxxxxxxxxxxx');
m_k(24) := to_number('983e5152ee66dfab', 'xxxxxxxxxxxxxxxx');
m_k(25) := to_number('a831c66d2db43210', 'xxxxxxxxxxxxxxxx');
m_k(26) := to_number('b00327c898fb213f', 'xxxxxxxxxxxxxxxx');
m_k(27) := to_number('bf597fc7beef0ee4', 'xxxxxxxxxxxxxxxx');
m_k(28) := to_number('c6e00bf33da88fc2', 'xxxxxxxxxxxxxxxx');
m_k(29) := to_number('d5a79147930aa725', 'xxxxxxxxxxxxxxxx');
m_k(30) := to_number('06ca6351e003826f', 'xxxxxxxxxxxxxxxx');
m_k(31) := to_number('142929670a0e6e70', 'xxxxxxxxxxxxxxxx');
m_k(32) := to_number('27b70a8546d22ffc', 'xxxxxxxxxxxxxxxx');
m_k(33) := to_number('2e1b21385c26c926', 'xxxxxxxxxxxxxxxx');
m_k(34) := to_number('4d2c6dfc5ac42aed', 'xxxxxxxxxxxxxxxx');
m_k(35) := to_number('53380d139d95b3df', 'xxxxxxxxxxxxxxxx');
m_k(36) := to_number('650a73548baf63de', 'xxxxxxxxxxxxxxxx');
m_k(37) := to_number('766a0abb3c77b2a8', 'xxxxxxxxxxxxxxxx');
m_k(38) := to_number('81c2c92e47edaee6', 'xxxxxxxxxxxxxxxx');
m_k(39) := to_number('92722c851482353b', 'xxxxxxxxxxxxxxxx');
m_k(40) := to_number('a2bfe8a14cf10364', 'xxxxxxxxxxxxxxxx');
m_k(41) := to_number('a81a664bbc423001', 'xxxxxxxxxxxxxxxx');
m_k(42) := to_number('c24b8b70d0f89791', 'xxxxxxxxxxxxxxxx');
m_k(43) := to_number('c76c51a30654be30', 'xxxxxxxxxxxxxxxx');
m_k(44) := to_number('d192e819d6ef5218', 'xxxxxxxxxxxxxxxx');
m_k(45) := to_number('d69906245565a910', 'xxxxxxxxxxxxxxxx');
m_k(46) := to_number('f40e35855771202a', 'xxxxxxxxxxxxxxxx');
m_k(47) := to_number('106aa07032bbd1b8', 'xxxxxxxxxxxxxxxx');
m_k(48) := to_number('19a4c116b8d2d0c8', 'xxxxxxxxxxxxxxxx');
m_k(49) := to_number('1e376c085141ab53', 'xxxxxxxxxxxxxxxx');
m_k(50) := to_number('2748774cdf8eeb99', 'xxxxxxxxxxxxxxxx');
m_k(51) := to_number('34b0bcb5e19b48a8', 'xxxxxxxxxxxxxxxx');
m_k(52) := to_number('391c0cb3c5c95a63', 'xxxxxxxxxxxxxxxx');
m_k(53) := to_number('4ed8aa4ae3418acb', 'xxxxxxxxxxxxxxxx');
m_k(54) := to_number('5b9cca4f7763e373', 'xxxxxxxxxxxxxxxx');
m_k(55) := to_number('682e6ff3d6b2b8a3', 'xxxxxxxxxxxxxxxx');
m_k(56) := to_number('748f82ee5defb2fc', 'xxxxxxxxxxxxxxxx');
m_k(57) := to_number('78a5636f43172f60', 'xxxxxxxxxxxxxxxx');
m_k(58) := to_number('84c87814a1f0ab72', 'xxxxxxxxxxxxxxxx');
m_k(59) := to_number('8cc702081a6439ec', 'xxxxxxxxxxxxxxxx');
m_k(60) := to_number('90befffa23631e28', 'xxxxxxxxxxxxxxxx');
m_k(61) := to_number('a4506cebde82bde9', 'xxxxxxxxxxxxxxxx');
m_k(62) := to_number('bef9a3f7b2c67915', 'xxxxxxxxxxxxxxxx');
m_k(63) := to_number('c67178f2e372532b', 'xxxxxxxxxxxxxxxx');
m_k(64) := to_number('ca273eceea26619c', 'xxxxxxxxxxxxxxxx');
m_k(65) := to_number('d186b8c721c0c207', 'xxxxxxxxxxxxxxxx');
m_k(66) := to_number('eada7dd6cde0eb1e', 'xxxxxxxxxxxxxxxx');
m_k(67) := to_number('f57d4f7fee6ed178', 'xxxxxxxxxxxxxxxx');
m_k(68) := to_number('06f067aa72176fba', 'xxxxxxxxxxxxxxxx');
m_k(69) := to_number('0a637dc5a2c898a6', 'xxxxxxxxxxxxxxxx');
m_k(70) := to_number('113f9804bef90dae', 'xxxxxxxxxxxxxxxx');
m_k(71) := to_number('1b710b35131c471b', 'xxxxxxxxxxxxxxxx');
m_k(72) := to_number('28db77f523047d84', 'xxxxxxxxxxxxxxxx');
m_k(73) := to_number('32caab7b40c72493', 'xxxxxxxxxxxxxxxx');
m_k(74) := to_number('3c9ebe0a15c9bebc', 'xxxxxxxxxxxxxxxx');
m_k(75) := to_number('431d67c49c100d4c', 'xxxxxxxxxxxxxxxx');
m_k(76) := to_number('4cc5d4becb3e42b6', 'xxxxxxxxxxxxxxxx');
m_k(77) := to_number('597f299cfc657e2a', 'xxxxxxxxxxxxxxxx');
m_k(78) := to_number('5fcb6fab3ad6faec', 'xxxxxxxxxxxxxxxx');
m_k(79) := to_number('6c44198c4a475817', 'xxxxxxxxxxxxxxxx');
end sha512_init_k;
procedure sha512_init_ctx
as
begin
m_ctx.h(0) := to_number('6a09e667f3bcc908', 'xxxxxxxxxxxxxxxx');
m_ctx.h(1) := to_number('bb67ae8584caa73b', 'xxxxxxxxxxxxxxxx');
m_ctx.h(2) := to_number('3c6ef372fe94f82b', 'xxxxxxxxxxxxxxxx');
m_ctx.h(3) := to_number('a54ff53a5f1d36f1', 'xxxxxxxxxxxxxxxx');
m_ctx.h(4) := to_number('510e527fade682d1', 'xxxxxxxxxxxxxxxx');
m_ctx.h(5) := to_number('9b05688c2b3e6c1f', 'xxxxxxxxxxxxxxxx');
m_ctx.h(6) := to_number('1f83d9abfb41bd6b', 'xxxxxxxxxxxxxxxx');
m_ctx.h(7) := to_number('5be0cd19137e2179', 'xxxxxxxxxxxxxxxx');
m_ctx.total_length := 0;
m_ctx.leftover_buffer := null;
m_ctx.leftover_buffer_length := 0;
for idx in 0..15 loop
m_ctx.words_array(idx) := 0;
end loop;
end sha512_init_ctx;
procedure sha512_process_block(p_words_array in ta_number,
p_words_count in number)
as
l_words_array ta_number := p_words_array;
l_words_count number := p_words_count;
l_words_idx number;
t number;
a number := m_ctx.h(0);
b number := m_ctx.h(1);
c number := m_ctx.h(2);
d number := m_ctx.h(3);
e number := m_ctx.h(4);
f number := m_ctx.h(5);
g number := m_ctx.h(6);
h number := m_ctx.h(7);
w ta_number;
a_save number;
b_save number;
c_save number;
d_save number;
e_save number;
f_save number;
g_save number;
h_save number;
t1 number;
t2 number;
begin
-- Process all bytes in the buffer with 64 bytes in each round of the loop.
l_words_idx := 0;
while (l_words_count > 0) loop
a_save := a;
b_save := b;
c_save := c;
d_save := d;
e_save := e;
f_save := f;
g_save := g;
h_save := h;
-- Compute the message schedule according to FIPS 180-2:6.2.2 step 2.
for t in 0..15 loop
w(t) := l_words_array(l_words_idx);
l_words_idx := l_words_idx + 1;
end loop;
for t in 16..79 loop
w(t) := bitand(op_r1_64(w(t - 2)) + w(t - 7) + op_r0_64(w(t - 15)) + w(t - 16), c_bits_ffffffffffffffff);
end loop;
-- The actual computation according to FIPS 180-2:6.2.2 step 3.
for t in 0..79 loop
t1 := bitand(h + op_s1_64(e) + op_ch_64(e, f, g) + m_k(t) + w(t), c_bits_ffffffffffffffff);
t2 := bitand(op_s0_64(a) + op_maj(a, b, c), c_bits_ffffffffffffffff);
h := g;
g := f;
f := e;
e := bitand(d + t1, c_bits_ffffffffffffffff);
d := c;
c := b;
b := a;
a := bitand(t1 + t2, c_bits_ffffffffffffffff);
end loop;
-- Add the starting values of the context according to FIPS 180-2:6.2.2 step 4.
a := bitand(a + a_save, c_bits_ffffffffffffffff);
b := bitand(b + b_save, c_bits_ffffffffffffffff);
c := bitand(c + c_save, c_bits_ffffffffffffffff);
d := bitand(d + d_save, c_bits_ffffffffffffffff);
e := bitand(e + e_save, c_bits_ffffffffffffffff);
f := bitand(f + f_save, c_bits_ffffffffffffffff);
g := bitand(g + g_save, c_bits_ffffffffffffffff);
h := bitand(h + h_save, c_bits_ffffffffffffffff);
-- Prepare for the next round.
l_words_count := l_words_count - 16;
end loop;
-- Put checksum in context given as argument.
m_ctx.h(0) := a;
m_ctx.h(1) := b;
m_ctx.h(2) := c;
m_ctx.h(3) := d;
m_ctx.h(4) := e;
m_ctx.h(5) := f;
m_ctx.h(6) := g;
m_ctx.h(7) := h;
end sha512_process_block;
procedure sha512_process_bytes(p_buffer in raw,
p_buffer_length in number)
as
l_buffer raw(16640);
l_buffer_length number;
l_words_array ta_number;
begin
m_ctx.total_length := m_ctx.total_length + nvl(p_buffer_length, 0);
-- When we already have some bits in our internal buffer concatenate both inputs first.
if (m_ctx.leftover_buffer_length = 0) then
l_buffer := p_buffer;
l_buffer_length := nvl(p_buffer_length, 0);
else
l_buffer := m_ctx.leftover_buffer || p_buffer;
l_buffer_length := m_ctx.leftover_buffer_length + nvl(p_buffer_length, 0);
end if;
-- Process available complete blocks.
if (l_buffer_length >= 128) then
declare
l_words_count number := bitand(l_buffer_length, c_bits_ffffffffffffff80) / 8;
l_max_idx number := l_words_count - 1;
l_numberraw raw(8);
l_numberhex varchar2(16);
l_number number;
begin
for idx in 0..l_max_idx loop
l_numberraw := sys.utl_raw.substr(l_buffer, idx * 8 + 1, 8);
l_numberhex := rawtohex(l_numberraw);
l_number := to_number(l_numberhex,'xxxxxxxxxxxxxxxx');
l_words_array(idx) := l_number;
end loop;
sha512_process_block(l_words_array, l_words_count);
l_buffer_length := bitand(l_buffer_length, 127);
if (l_buffer_length > 0) then
l_buffer := sys.utl_raw.substr(l_buffer, l_words_count * 8 + 1, l_buffer_length);
end if;
end;
end if;
-- Move remaining bytes into internal buffer.
if (l_buffer_length > 0) then
m_ctx.leftover_buffer := l_buffer;
m_ctx.leftover_buffer_length := l_buffer_length;
end if;
end sha512_process_bytes;
procedure sha512_finish_ctx(p_resultbuf out nocopy ta_number)
as
l_filesizeraw raw(16);
begin
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_80;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
while ((m_ctx.leftover_buffer_length mod 128) <> 112) loop
m_ctx.leftover_buffer := m_ctx.leftover_buffer || c_bits_00;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 1;
end loop;
l_filesizeraw := hextoraw(to_char(m_ctx.total_length * 8, 'FM0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
m_ctx.leftover_buffer := m_ctx.leftover_buffer || l_filesizeraw;
m_ctx.leftover_buffer_length := m_ctx.leftover_buffer_length + 16;
sha512_process_bytes(null, 0);
for idx in 0..7 loop
p_resultbuf(idx) := m_ctx.h(idx);
end loop;
end sha512_finish_ctx;
function sha512(p_buffer in raw) return sha512_checksum_raw
as