-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
as_crypto.sql
6170 lines (6165 loc) · 225 KB
/
as_crypto.sql
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 as_crypto
is
/*
MIT License
Copyright (c) 2016-2023 Anton Scheffer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
-- Hash Functions
HASH_MD4 CONSTANT PLS_INTEGER := 1;
HASH_MD5 CONSTANT PLS_INTEGER := 2;
HASH_SH1 CONSTANT PLS_INTEGER := 3;
HASH_SH256 CONSTANT PLS_INTEGER := 4;
HASH_SH384 CONSTANT PLS_INTEGER := 5;
HASH_SH512 CONSTANT PLS_INTEGER := 6;
HASH_SH224 CONSTANT PLS_INTEGER := 11;
HASH_SH512_224 CONSTANT PLS_INTEGER := 12;
HASH_SH512_256 CONSTANT PLS_INTEGER := 13;
HASH_MD2 CONSTANT PLS_INTEGER := 14;
HASH_RIPEMD160 CONSTANT PLS_INTEGER := 15;
-- MAC Functions
HMAC_MD5 CONSTANT PLS_INTEGER := 1;
HMAC_SH1 CONSTANT PLS_INTEGER := 2;
HMAC_SH256 CONSTANT PLS_INTEGER := 3;
HMAC_SH384 CONSTANT PLS_INTEGER := 4;
HMAC_SH512 CONSTANT PLS_INTEGER := 5;
HMAC_MD4 CONSTANT PLS_INTEGER := 10;
HMAC_SH224 CONSTANT PLS_INTEGER := 11;
HMAC_SH512_224 CONSTANT PLS_INTEGER := 12;
HMAC_SH512_256 CONSTANT PLS_INTEGER := 13;
HMAC_RIPEMD160 CONSTANT PLS_INTEGER := 14;
-- Block Cipher Algorithms
ENCRYPT_DES CONSTANT PLS_INTEGER := 1; -- 0x0001
ENCRYPT_3DES_2KEY CONSTANT PLS_INTEGER := 2; -- 0x0002
ENCRYPT_3DES CONSTANT PLS_INTEGER := 3; -- 0x0003
ENCRYPT_AES CONSTANT PLS_INTEGER := 4; -- 0x0004
ENCRYPT_PBE_MD5DES CONSTANT PLS_INTEGER := 5; -- 0x0005
ENCRYPT_AES128 CONSTANT PLS_INTEGER := 6; -- 0x0006
ENCRYPT_AES192 CONSTANT PLS_INTEGER := 7; -- 0x0007
ENCRYPT_AES256 CONSTANT PLS_INTEGER := 8; -- 0x0008
-- Block Cipher Chaining Modifiers
CHAIN_CBC CONSTANT PLS_INTEGER := 256; -- 0x0100
CHAIN_CFB CONSTANT PLS_INTEGER := 512; -- 0x0200
CHAIN_ECB CONSTANT PLS_INTEGER := 768; -- 0x0300
CHAIN_OFB CONSTANT PLS_INTEGER := 1024; -- 0x0400
CHAIN_GCM CONSTANT PLS_INTEGER := 1280; -- 0x0500
CHAIN_CCM CONSTANT PLS_INTEGER := 1536; -- 0x0600
CHAIN_OFB_REAL CONSTANT PLS_INTEGER := 2560; -- 0x0A00
-- Block Cipher Padding Modifiers
PAD_PKCS5 CONSTANT PLS_INTEGER := 4096; -- 0x1000
PAD_NONE CONSTANT PLS_INTEGER := 8192; -- 0x2000
PAD_ZERO CONSTANT PLS_INTEGER := 12288; -- 0x3000
PAD_ORCL CONSTANT PLS_INTEGER := 16384; -- 0x4000
PAD_OneAndZeroes CONSTANT PLS_INTEGER := 20480; -- 0x5000
PAD_ANSI_X923 CONSTANT PLS_INTEGER := 24576; -- 0x6000
-- Stream Cipher Algorithms
ENCRYPT_RC4 CONSTANT PLS_INTEGER := 129; -- 0x0081
-- Public Key Encryption Algorithm
PKENCRYPT_RSA_PKCS1_OAEP constant pls_integer := 1;
PKENCRYPT_RSA_PKCS1_OAEP_SHA2 constant pls_integer := 2;
-- Public Key Type Algorithm
KEY_TYPE_RSA constant pls_integer := 1;
KEY_TYPE_EC constant pls_integer := 2;
KEY_TYPE_EdDSA constant pls_integer := 3;
-- Public Key Signature Type Algorithm
SIGN_SHA224_RSA constant pls_integer := 1; -- SHA 224 bit hash function with RSA
SIGN_SHA256_RSA constant pls_integer := 2; -- SHA 256 bit hash function with RSA
SIGN_SHA256_RSA_X931 constant pls_integer := 3; -- SHA 256 bit hash function with RSA and X931 padding
SIGN_SHA384_RSA constant pls_integer := 4; -- SHA 384 bit hash function with RSA
SIGN_SHA384_RSA_X931 constant pls_integer := 5; -- SHA 384 bit hash function with RSA and X931 padding
SIGN_SHA512_RSA constant pls_integer := 6; -- SHA 512 bit hash function with RSA
SIGN_SHA512_RSA_X931 constant pls_integer := 7; -- SHA 512 bit hash function with RSA and X931 padding
SIGN_SHA1_RSA constant pls_integer := 8; -- SHA1 hash function with RSA
SIGN_SHA1_RSA_X931 constant pls_integer := 9; -- SHA1 hash function with RSA and X931 padding
SIGN_MD2_RSA constant pls_integer := 20; -- MD2 hash function with RSA
SIGN_MD5_RSA constant pls_integer := 21; -- MD2 hash function with RSA
SIGN_SHA256withECDSA constant pls_integer := 10;
SIGN_SHA256withECDSAinP1363 constant pls_integer := 11;
SIGN_SHA384withECDSA constant pls_integer := 12;
SIGN_SHA384withECDSAinP1363 constant pls_integer := 13;
SIGN_SHA512withECDSA constant pls_integer := 14;
SIGN_SHA512withECDSAinP1363 constant pls_integer := 15;
SIGN_Ed25519 constant pls_integer := 16;
SIGN_SHA256_RSA_PSS constant pls_integer := 17; -- SHA 256 bit hash function with RSASSA-PSS
SIGN_SHA384_RSA_PSS constant pls_integer := 18; -- SHA 384 bit hash function with RSASSA-PSS
SIGN_SHA512_RSA_PSS constant pls_integer := 19; -- SHA 512 bit hash function with RSASSA-PSS
--
AES_CBC_PKCS5 constant pls_integer := ENCRYPT_AES + CHAIN_CBC + PAD_PKCS5;
AES_GCM_NONE constant pls_integer := ENCRYPT_AES + CHAIN_GCM + PAD_NONE;
AES_CCM_NONE constant pls_integer := ENCRYPT_AES + CHAIN_CCM + PAD_NONE;
--
function hash( src raw, typ pls_integer )
return raw;
--
function mac( src raw, typ pls_integer, key raw )
return raw;
--
function randombytes( number_bytes positive )
return raw;
--
function encrypt( src raw, typ pls_integer, key raw, iv raw := null )
return raw;
--
function decrypt( src raw, typ pls_integer, key raw, iv raw := null )
return raw;
--
function encrypt( src in raw
, typ in pls_integer
, key in raw
, iv in raw := null
, aad in raw := null
, tag out raw
)
return raw;
--
function decrypt( src in raw
, typ in pls_integer
, key in raw
, iv in raw := null
, aad in raw := null
, tag in raw
)
return raw;
--
function pkEncrypt( src raw
, pub_key raw
, pubkey_alg binary_integer
, enc_alg binary_integer
)
return raw;
--
function pkDecrypt( src raw
, prv_key raw
, pubkey_alg binary_integer
, enc_alg binary_integer
)
return raw;
--
function sign( src raw
, prv_key raw
, pubkey_alg binary_integer
, sign_alg binary_integer
)
return raw;
--
function verify( src raw
, sign raw
, pub_key raw
, pubkey_alg binary_integer
, sign_alg binary_integer
)
return boolean;
--
function base64URL_decode( p_txt varchar2 )
return raw;
--
function base64URL_encode( p_raw raw )
return varchar2;
--
function base64URL_encode( p_txt varchar2 )
return varchar2;
--
end;
/
create or replace package body as_crypto
is
--
c_X931_TRAILER_SH1 constant raw(2) := '33CC';
c_X931_TRAILER_SH224 constant raw(2) := '38CC';
c_X931_TRAILER_SH256 constant raw(2) := '34CC';
c_X931_TRAILER_SH384 constant raw(2) := '36CC';
c_X931_TRAILER_SH512 constant raw(2) := '35CC';
--
c_ASN1_MD2 raw(100) := '3020300C06082A864886F70D020205000410';
c_ASN1_MD5 raw(100) := '3020300C06082A864886F70D020505000410';
c_ASN1_SH1 raw(100) := '3021300906052B0E03021A05000414';
c_ASN1_SH224 raw(100) := '302D300D06096086480165030402040500041C';
c_ASN1_SH256 raw(100) := '3031300D060960864801650304020105000420';
c_ASN1_SH384 raw(100) := '3041300D060960864801650304020205000430';
c_ASN1_SH512 raw(100) := '3051300D060960864801650304020305000440';
--
c_INTEGER raw(1) := '02';
c_BIT_STRING raw(1) := '03';
c_OCTECT raw(1) := '04';
c_NULL raw(1) := '05';
c_OID raw(1) := '06';
c_SEQUENCE raw(1) := '30';
type tp_key_parameters is table of raw(3999) index by pls_integer;
--
type tp_mag is table of number index by pls_integer;
ccc number := 16; -- number of nibbles
cm number := power( 16, ccc );
cmm number := cm - 1;
cm2 number := cm / 2;
cmi number := power( 16, -ccc );
--
function mag( p1 varchar2 )
return tp_mag;
--
c_mag_0 constant tp_mag := mag( '0' );
c_mag_1 constant tp_mag := mag( '1' );
c_mag_3 constant tp_mag := mag( '3' );
c_mag_4 constant tp_mag := mag( '4' );
--
type tp_ec_point is record
( x tp_mag
, y tp_mag
, z tp_mag
);
type tp_ec_curve is record
( prime tp_mag
, group_order tp_mag
, a tp_mag
, b tp_mag
, p_plus_1_div_4 tp_mag
, generator tp_ec_point
, nlen pls_integer
);
type tp_ed_point is record
( x tp_mag
, y tp_mag
, z tp_mag
, t tp_mag
);
type tp_ed_curve is record
( nlen pls_integer
, l tp_mag
, d tp_mag
, q tp_mag
, i tp_mag
, b tp_ed_point
);
--
bmax32 constant number := power( 2, 32 ) - 1;
bmax64 constant number := power( 2, 64 ) - 1;
type tp_crypto is table of number;
type tp_aes_tab is table of number index by pls_integer;
--
SP1 tp_crypto;
SP2 tp_crypto;
SP3 tp_crypto;
SP4 tp_crypto;
SP5 tp_crypto;
SP6 tp_crypto;
SP7 tp_crypto;
SP8 tp_crypto;
--
function int2hex( p_val integer, p_len pls_integer )
return varchar2
is
begin
return to_char( p_val, rpad( 'fm0', 2 + 2 * p_len, 'X' ) );
end;
--
function hex2int( p_val varchar2 )
return number
is
begin
return to_number( p_val, rpad( 'X', length( p_val ), 'X' ) );
end;
--
function mag( p1 varchar2 )
return tp_mag
is
l number;
n number;
rv tp_mag;
t1 varchar2(3999);
cfmt1 varchar2(100) := rpad( 'X', ccc, 'X' );
begin
t1 := nvl( ltrim( p1, '0' ), '0' );
l := ceil( length( t1 ) / ccc );
t1 := lpad( t1, l * ccc, '0' );
for i in 0 .. l - 1
loop
n := to_number( substr( t1, 1 + i * ccc, ccc ), cfmt1 );
rv( l - 1 - i ) := n;
end loop;
return rv;
end;
--
function demag( p1 tp_mag )
return varchar2
is
rv varchar2(3999);
cfmt2 varchar2(100);
begin
if ccc = 1
then
cfmt2 := 'fmx';
else
cfmt2 := 'fm' || rpad( '0', ccc, 'x' );
end if;
for i in 0 .. p1.count - 1
loop
rv := to_char( p1( i ), cfmt2 ) || rv;
end loop;
return nvl( ltrim( rv, '0' ), '0' );
end;
--
function requal( x tp_mag, y tp_mag )
return boolean
is
rv boolean;
begin
if x.count != y.count
then
return false;
end if;
for i in 0 .. x.count - 1
loop
rv := x(i) = y(i);
exit when not rv;
end loop;
return rv;
end;
--
function r_greater_equal( x tp_mag, y tp_mag )
return boolean
is
rv boolean := true;
xc pls_integer := x.count;
yc pls_integer := y.count;
begin
if xc > yc
then
return true;
elsif xc < yc
then
return false;
end if;
for i in reverse 0 .. xc - 1
loop
exit when x(i) > y(i);
if x(i) < y(i)
then
rv := false;
exit;
end if;
end loop;
return rv;
end;
--
function radd( x tp_mag, y tp_mag )
return tp_mag
is
c number;
t number;
rv tp_mag;
xc pls_integer := x.count;
yc pls_integer := y.count;
begin
if xc < yc
then
return radd( y, x );
end if;
c := 0;
for i in 0 .. yc - 1
loop
t := x(i) + y(i) + c;
if t >= cm
then
t := t - cm;
c := 1;
else
c := 0;
end if;
rv(i) := t;
end loop;
for i in yc .. xc - 1
loop
t := x(i) + c;
if t >= cm
then
t := t - cm;
c := 1;
else
c := 0;
end if;
rv(i) := t;
end loop;
if c > 0
then
rv( xc ) := 1;
end if;
return rv;
end;
--
function rsub( p1 tp_mag, p2 tp_mag )
return tp_mag
is
b number;
t number;
rv tp_mag;
begin
b := 0;
for i in 0 .. p2.count - 1
loop
t := p1( i ) - p2( i ) - b;
if t < 0
then
b := 1;
t := t + cm;
else
b := 0;
end if;
rv( i ) := t;
end loop;
for i in p2.count .. p1.count - 1
loop
t := p1( i ) - b;
if t < 0
then
b := 1;
t := t + cm;
else
b := 0;
end if;
rv( i ) := t;
end loop;
while rv( rv.last ) = 0 and rv.count > 1
loop
rv.delete( rv.last );
end loop;
if rv.count = 0
then
rv(0) := 0;
end if;
return rv;
end;
--
function nsub( x tp_mag, y number )
return tp_mag
is
b number;
s tp_mag := x;
begin
b := y;
for i in 0 .. s.count - 1
loop
s( i ) := s( i ) - b;
if s( i ) < 0
then
b := 1;
s( i ) := s( i ) + cm;
else
exit;
end if;
end loop;
return s;
end;
--
function nmul( x tp_mag, y number )
return tp_mag
is
t number;
c number := 0;
rv tp_mag := x;
begin
for i in 0 .. rv.count - 1
loop
t := rv(i) * y + c;
c := trunc( t * cmi );
rv(i) := t - c * cm;
end loop;
if c > 0
then
rv(rv.count) := c;
end if;
return rv;
end;
--
function rmul( x tp_mag, y tp_mag )
return tp_mag
is
t number;
c number;
ci pls_integer;
m tp_mag;
begin
for i in 0 .. y.count + x.count - 2
loop
m(i) := 0;
end loop;
for yi in 0 .. y.count - 1
loop
c := 0;
for xi in 0 .. x.count - 1
loop
ci := xi+yi;
t := m(ci) + x(xi) * y(yi) + c;
c := trunc( t * cmi );
m(ci) := t - c * cm;
end loop;
if c > 0
then
m( ci + 1 ) := c;
end if;
end loop;
return m;
end;
--
function xmod( x tp_mag, y tp_mag )
return tp_mag
is
xc number := x.count;
yc number := y.count;
rv tp_mag;
ly tp_mag;
dq tp_mag;
l_gt boolean;
d number;
d2 number;
tmp number;
r number;
sf number;
--
procedure sub( x in out tp_mag, y tp_mag, p number )
is
b number := 0;
begin
for i in p .. p + y.count - 1
loop
x(i) := x(i) - y( i - p ) - b;
if x(i) < 0
then
x(i) := x(i) + cm;
b := 1;
else
b := 0;
end if;
end loop;
end;
--
function ge( x tp_mag, y tp_mag, p number )
return boolean
is
l_ge boolean := true;
begin
for i in reverse p .. p + y.count - 1
loop
case standard.sign( x(i) - y( i - p ) )
when 1 then
exit;
when -1 then
l_ge := false;
exit;
else null;
end case;
end loop;
return l_ge;
end;
--
begin
if xc < yc
then
return x;
end if;
if xc = yc
then
for i in reverse 0 .. xc - 1
loop
if x( i ) > y( i )
then
l_gt := true;
exit;
elsif x( i ) < y( i )
then
return x;
end if;
end loop;
if l_gt is null
then
rv(0) := 0;
end if;
end if;
if yc > 1
then
ly := y;
if y( yc - 1 ) < cm2
then
sf := trunc( cm / ( y( yc - 1 ) + 1 ) );
r := 0;
for i in 0 .. xc - 1
loop
tmp := x(i) * sf + r;
if tmp < cm
then
r := 0;
rv(i) := tmp;
else
r := trunc( tmp * cmi );
rv(i) := tmp - r * cm;
end if;
end loop;
if r > 0
then
rv(xc) := r;
xc := xc + 1;
end if;
--
r := 0;
for i in 0 .. yc - 1
loop
tmp := ly(i) * sf + r;
if tmp < cm
then
r := 0;
ly(i) := tmp;
else
r := trunc( tmp * cmi );
ly(i) := tmp - r * cm;
end if;
end loop;
else
rv := x;
end if;
if xc = 2
then
rv(2) := 0;
xc := 3;
end if;
--
if ge( rv, ly, xc - yc )
then
sub( rv, ly, xc - yc );
end if;
--
d2 := ly( yc - 1 ) * cm + ly( yc - 2 );
for i in reverse yc .. xc - 1
loop
if rv(i) > 0
then
if rv(i) > d2
then
d := cm - 1;
else
tmp := rv(i) * cm + rv( i - 1 );
if tmp > d2
then
d := cm - 1;
else
d := least( trunc( cm * ( tmp / d2 ) + rv( i - 2 ) / d2 ), cm - 1 );
end if;
end if;
dq.delete;
r := 0;
for j in 0 .. yc - 1
loop
tmp := ly(j) * d + r;
if tmp < cm
then
r := 0;
dq(j) := tmp;
else
r := trunc( tmp * cmi );
dq(j) := tmp - r * cm;
end if;
end loop;
dq( yc ) := r;
if not ge( rv, dq, i - yc )
then
r := 0;
for j in 0 .. yc - 1
loop
tmp := dq(j);
tmp := tmp - ly(j) - r;
if dq(j) < 0
then
dq(j) := tmp + cm;
r := 1;
else
dq(j) := tmp;
r := 0;
end if;
end loop;
if r > 0
then
dq(yc) := dq(yc) - 1;
end if;
end if;
sub( rv, dq, i - yc );
end if;
end loop;
--
-- if rv >= ly then substract ly from rv
if ge( rv, ly, 0 )
then
sub( rv, ly, 0 );
end if;
--
for i in reverse 1 .. xc - 1
loop
exit when rv(i) > 0;
rv.delete(i);
end loop;
--
else
d := y(0);
r := 0;
if d > 1
then
for i in reverse 0 .. x.count - 1
loop
tmp := r * cm + x(i);
r := tmp - trunc( tmp / d ) * d;
end loop;
end if;
rv(0) := r;
end if;
if sf is not null
then
r := 0;
for i in reverse 0 .. rv.count - 1
loop
tmp := rv(i) + r * cm;
rv(i) := trunc( tmp / sf );
r := tmp - rv(i) * sf;
end loop;
tmp := rv.count - 1;
if tmp > 0 and rv( tmp ) = 0
then
rv.delete( tmp );
end if;
end if;
return rv;
end;
--
function addmod( p1 tp_mag, p2 tp_mag, m tp_mag )
return tp_mag
is
rv tp_mag := radd( p1, p2 );
begin
if r_greater_equal( rv, m )
then
rv := rsub( rv, m );
end if;
return rv;
end;
--
function submod( p1 tp_mag, p2 tp_mag, m tp_mag )
return tp_mag
is
rv tp_mag := radd( p1, rsub( m, p2 ) );
begin
if r_greater_equal( rv, m )
then
rv := rsub( rv, m );
end if;
return rv;
end;
--
function mulmod( p1 tp_mag, p2 tp_mag, m tp_mag )
return tp_mag
is
begin
return xmod( rmul( p1, p2 ), m );
end;
--
function small_nmulmod( p1 tp_mag, n number, m tp_mag )
return tp_mag
is
rv tp_mag := nmul( p1, n );
begin
for i in 1 .. 5 -- expect n < 5
loop
exit when not r_greater_equal( rv, m );
if i = 5
then
rv := xmod( rv, m );
else
rv := rsub( rv, m );
end if;
end loop;
return rv;
end;
--
function rdiv2( p1 tp_mag )
return tp_mag
is
c number;
t number;
rv tp_mag;
begin
if p1.count = 1
then
rv(0) := trunc( p1( 0 ) / 2 );
else
c := 0;
for i in reverse 0 .. p1.count - 1
loop
t := p1( i ) + c;
rv( i ) := trunc( t / 2 );
c := case when bitand( t, 1 ) = 1 then cm else 0 end;
end loop;
while rv( rv.last ) = 0
loop
rv.delete( rv.last );
end loop;
end if;
return rv;
end;
--
function powmod( pa tp_mag, pb tp_mag, pm tp_mag )
return tp_mag
is
m1 tp_mag;
r tp_mag;
k pls_integer;
mc pls_integer;
ninv0 number;
bx0 number;
mx0 number;
nx number;
xx number;
xm tp_mag;
am tp_mag;
one tp_mag;
tx varchar2(3999);
sb varchar2(3999);
nr number;
hb boolean := false;
function monpro( pa tp_mag, pb tp_mag )
return tp_mag
is
b number;
c number;
m number;
tmp number;
t0 number;
t tp_mag;
ta tp_mag;
tb tp_mag;
begin
ta := pa;
for i in ta.count .. mc - 1
loop
ta( i ) := 0;
end loop;
tb := pb;
for i in tb.count .. mc - 1
loop
tb( i ) := 0;
end loop;
for i in 0 .. mc
loop
t( i ) := 0;
end loop;
for i in 0 .. mc - 1
loop
t( mc + 1 ) := 0;
tmp := t(0) + ta(0) * tb( i );
c := trunc( tmp * cmi );
t0 := tmp - c * cm;
t(1) := t(1) + c;
tmp := t0 * ninv0;
m := tmp - trunc( tmp * cmi ) * cm;
tmp := t0 + m * m1(0);
if tmp >= cm
then
t(1) := t(1) + trunc( tmp * cmi );
end if;
-- check for overflow of t(1)?
for j in 1 .. mc - 1
loop
tmp := t( j ) + ta( j ) * tb( i ) + m * m1( j );
if tmp >= cm
then
c := trunc( tmp * cmi );
t( j - 1 ) := tmp - c * cm;
if c >= cm
then
c := c - cm;
t( j + 2 ) := t( j + 2 ) + 1;
end if;
t( j + 1 ) := t( j + 1 ) + c;
else
t( j - 1 ) := tmp;
end if;
end loop;
t( mc - 1 ) := t( mc );
t( mc ) := t( mc + 1 );
end loop;
t.delete(mc+1);
for j in reverse 1 .. t.count - 1
loop
exit when t(j) > 0;
t.delete(j);
end loop;
b := t.count - mc;
if b = 0
then
for i in reverse 0 .. mc - 1
loop
b := t(i) - m1(i);
exit when b != 0;
end loop;
if b = 0
then
t.delete;
t(0) := 0;
end if;
end if;
if b > 0
then
b := 0;
for i in 0 .. mc - 1
loop
tmp := t(i) - m1(i) - b;
if tmp < 0
then
b := 1;
t(i) := tmp + cm;
else
b := 0;
t(i) := tmp;
end if;
end loop;
for i in mc .. t.count - 1
loop
tmp := t(i) - b;
if tmp < 0
then
b := 1;
t(i) := tmp + cm;
else
t(i) := tmp;
exit;
end if;
end loop;
for j in reverse 1 .. t.count - 1
loop
exit when t(j) > 0;
t.delete(j);
end loop;
end if;
return t;
end;
begin
m1 := pm;
mc := m1.count;
k := mc * ccc * 4;
for i in 0 .. mc - 1
loop
r( i ) := 0;
end loop;
r( mc ) := 1;
-- See "A New Algorithm for Inversion mod pk", Cetin Kaya Koc, https://eprint.iacr.org/2017/411.pdf
bx0 := m1(0);
mx0 := 2 * bx0;
if mx0 >= cm
then
mx0 := mx0 - cm;
end if;
nx := 1;
for j in 1 .. ccc * 4 - 1
loop
xx := bitand( bx0, power( 2, j ) );
if xx > 0
then
nx := nx + xx;
bx0 := bx0 + mx0;
if bx0 >= cm
then
bx0 := bx0 - cm;
end if;
end if;
mx0 := 2 * mx0;
if mx0 >= cm
then
mx0 := mx0 - cm;
end if;
end loop;
ninv0 := cm - nx;
--
xm := xmod( r, m1 );
am := xmod( rmul( pa, xm ), m1 );
sb := nvl( ltrim( demag( pb ), '0' ), '0' );
for i in 1 .. length( sb )
loop
nr := to_number( substr( sb, i, 1 ), 'x' );
for j in reverse 0 .. 3