home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexx_md5.zip / testsha.cmd < prev   
OS/2 REXX Batch file  |  1998-05-12  |  8KB  |  295 lines

  1. /* Test the  "dll" SHA-1
  2.    This uses the test strings found in FIPS PUB 180
  3.    algorithim  */
  4.  
  5. signal on error name foo1 ; signal on syntax name foo1 ;
  6.  
  7. /* Load all functions in the SRXFUNC dll. There's only one now (SRX_MD5),
  8.    but maybe more will be added someday */
  9.  
  10. if rxfuncquery('srx_sha')=1  then do
  11.   call RXFuncAdd 'SRXLoadFuncs', 'SRXFUNC', 'SRXLoadFuncs'
  12.   call SRXLoadFuncs
  13. end
  14. if rxfuncquery('srx_sha')=1  then do
  15.   say " Could not load SRX_SHA from SRXFUNC.DLL"
  16.   exit
  17. end
  18.  
  19. /* Begin Tests */
  20.  
  21.  
  22.  
  23.  
  24. say
  25. say ' For: "abc" '
  26. say "   SHA should be: A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D         "
  27. a1=srx_sha('abc')
  28. say "       from dll: " a1
  29. say
  30.  
  31.  
  32. abc="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  33. say ' For: "abc" '
  34. say " SHA should be: 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1"
  35. a1=srx_sha(abc)
  36. say "      from dll:" a1
  37. say
  38.  
  39. abc=copies('a',1000000)
  40. say ' For: 1 million "a" characters'
  41. say " SHA should be: 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"
  42. a1=srx_sha(abc)
  43. say "      from dll:" a1
  44. say
  45.  
  46.  
  47.  
  48. call SRXDropFuncs
  49. exit
  50.  
  51. foo1: 
  52. say " error " rc sigl
  53. exit
  54.  
  55.  
  56.  
  57. /*  ------------------------------ */
  58. /* this is an "all rexx" md5 procedure. It works, but it is slow */
  59. rexx_md5:procedure
  60. parse arg stuff
  61. numeric digits 11
  62. lenstuff=length(stuff)
  63.  
  64. c0=d2c(0)
  65. c1=d2c(128)
  66. c1a=d2c(255)
  67. c1111=c1a||c1a||c1a||c1a
  68. slen=length(stuff)*8
  69. slen512=slen//512
  70.  
  71. /* pad message to multiple of 512 bits.  Last 2 words are 64 bit # bits in message*/
  72. if slen512=448 then  addme=512
  73. if slen512<448 then addme=448-slen512
  74. if slen512>448 then addme=960-slen512
  75. addwords=addme/8
  76.  
  77. apad=c1||copies(c0,addwords-1)
  78.  
  79. xlen=reverse(right(d2c(lenstuff*8),4,c0))||c0||c0||c0||c0  /* 2**32 max bytes in message */
  80.  
  81. /* NEWSTUFF is the message to be md5'ed */
  82. newstuff=stuff||apad||xlen
  83.  
  84. /* starting values of registers */
  85.  a ='67452301'x;
  86.  b ='efcdab89'x;
  87.  c ='98badcfe'x;
  88.  d ='10325476'x;
  89.  
  90. lennews=length(newstuff)/4
  91.  
  92. /* loop through entire message */
  93. do i1 = 0 to ((lennews/16)-1)
  94.   i16=i1*64
  95.   do j=1 to 16
  96.      j4=((j-1)*4)+1
  97.      jj=i16+j4
  98.      m.j=reverse(substr(newstuff,jj,4))
  99.   end /* do */
  100.  
  101. /* transform this block of 16 chars to 4 values. Save prior values first */
  102.  aa=a;bb=b;cc=c;dd=d
  103.  
  104. /* do 4 rounds, 16 operations per round (rounds differ in bit'ing functions */
  105. S11=7
  106. S12=12
  107. S13=17
  108. S14=22
  109.   a=round1( a, b, c, d,   0 , S11, 3614090360); /* 1 */
  110.   d=round1( d, a, b, c,   1 , S12, 3905402710); /* 2 */
  111.   c=round1( c, d, a, b,   2 , S13,  606105819); /* 3 */
  112.   b=round1( b, c, d, a,   3 , S14, 3250441966); /* 4 */
  113.   a=round1( a, b, c, d,   4 , S11, 4118548399); /* 5 */
  114.   d=round1( d, a, b, c,   5 , S12, 1200080426); /* 6 */
  115.   c=round1( c, d, a, b,   6 , S13, 2821735955); /* 7 */
  116.   b=round1( b, c, d, a,   7 , S14, 4249261313); /* 8 */
  117.   a=round1( a, b, c, d,   8 , S11, 1770035416); /* 9 */
  118.   d=round1( d, a, b, c,   9 , S12, 2336552879); /* 10 */
  119.   c=round1( c, d, a, b,  10 , S13, 4294925233); /* 11 */
  120.   b=round1( b, c, d, a,  11 , S14, 2304563134); /* 12 */
  121.   a=round1( a, b, c, d,  12 , S11, 1804603682); /* 13 */
  122.   d=round1( d, a, b, c,  13 , S12, 4254626195); /* 14 */
  123.   c=round1( c, d, a, b,  14 , S13, 2792965006); /* 15 */
  124.   b=round1( b, c, d, a,  15 , S14, 1236535329); /* 16 */
  125.  
  126.   /* Round 2 */
  127. S21=5
  128. S22=9
  129. S23=14
  130. S24=20
  131. a= round2( a, b, c, d,   1 , S21, 4129170786); /* 17 */
  132. d= round2( d, a, b, c,   6 , S22, 3225465664); /* 18 */
  133. c=  round2( c, d, a, b,  11 , S23,  643717713); /* 19 */
  134. b=  round2( b, c, d, a,   0 , S24, 3921069994); /* 20 */
  135. a=  round2( a, b, c, d,   5 , S21, 3593408605); /* 21 */
  136. d=  round2( d, a, b, c,  10 , S22,   38016083); /* 22 */
  137. c=  round2( c, d, a, b,  15 , S23, 3634488961); /* 23 */
  138. b= round2( b, c, d, a,   4 , S24, 3889429448); /* 24 */
  139. a= round2( a, b, c, d,   9 , S21,  568446438); /* 25 */
  140. d= round2( d, a, b, c,  14 , S22, 3275163606); /* 26 */
  141. c=  round2( c, d, a, b,   3 , S23, 4107603335); /* 27 */
  142. b=  round2( b, c, d, a,   8 , S24, 1163531501); /* 28 */
  143. a=  round2( a, b, c, d,  13 , S21, 2850285829); /* 29 */
  144. d=  round2( d, a, b, c,   2 , S22, 4243563512); /* 30 */
  145. c=  round2( c, d, a, b,   7 , S23, 1735328473); /* 31 */
  146. b= round2( b, c, d, a,  12 , S24, 2368359562); /* 32 */
  147.  
  148.   /* Round 3 */
  149. S31= 4
  150. S32= 11
  151. S33= 16
  152. S34= 23
  153. a= round3( a, b, c, d,   5 , S31, 4294588738); /* 33 */
  154. d=  round3( d, a, b, c,   8 , S32, 2272392833); /* 34 */
  155. c=  round3( c, d, a, b,  11 , S33, 1839030562); /* 35 */
  156. b=  round3( b, c, d, a,  14 , S34, 4259657740); /* 36 */
  157. a=  round3( a, b, c, d,   1 , S31, 2763975236); /* 37 */
  158. d=  round3( d, a, b, c,   4 , S32, 1272893353); /* 38 */
  159. c=  round3( c, d, a, b,   7 , S33, 4139469664); /* 39 */
  160. b=  round3( b, c, d, a,  10 , S34, 3200236656); /* 40 */
  161. a=  round3( a, b, c, d,  13 , S31,  681279174); /* 41 */
  162. d=  round3( d, a, b, c,   0 , S32, 3936430074); /* 42 */
  163. c=  round3( c, d, a, b,   3 , S33, 3572445317); /* 43 */
  164. b=  round3( b, c, d, a,   6 , S34,   76029189); /* 44 */
  165. a=  round3( a, b, c, d,   9 , S31, 3654602809); /* 45 */
  166. d=  round3( d, a, b, c,  12 , S32, 3873151461); /* 46 */
  167. c=  round3( c, d, a, b,  15 , S33,  530742520); /* 47 */
  168. b=  round3( b, c, d, a,   2 , S34, 3299628645); /* 48 */
  169.  
  170.   /* Round 4 */
  171. S41=6
  172. S42=10
  173. S43=15
  174. s44=21
  175. a=round4( a, b, c, d,   0 , S41, 4096336452); /* 49 */
  176. d=round4( d, a, b, c,   7 , S42, 1126891415); /* 50 */
  177. c=round4( c, d, a, b,  14 , S43, 2878612391); /* 51 */
  178. b=round4( b, c, d, a,   5 , s44, 4237533241); /* 52 */
  179. a=round4( a, b, c, d,  12 , S41, 1700485571); /* 53 */
  180. d=round4( d, a, b, c,   3 , S42, 2399980690); /* 54 */
  181. c=round4( c, d, a, b,  10 , S43, 4293915773); /* 55 */
  182. b=round4( b, c, d, a,   1 , s44,  2240044497); /* 56 */
  183. a=round4( a, b, c, d,   8 , S41, 1873313359); /* 57 */
  184. d=round4( d, a, b, c,  15 , S42, 4264355552); /* 58 */
  185. c=round4( c, d, a, b,   6 , S43, 2734768916); /* 59 */
  186. b=round4( b, c, d, a,  13 , s44, 1309151649); /* 60 */
  187. a=round4( a, b, c, d,   4 , S41, 4149444226); /* 61 */
  188. d=round4( d, a, b, c,  11 , S42, 3174756917); /* 62 */
  189. c=round4( c, d, a, b,   2 , S43,  718787259); /* 63 */
  190. b=round4( b, c, d, a,   9 , s44, 3951481745); /* 64 */
  191.  
  192.  
  193. a=m32add(aa,a) ; b=m32add(bb,b) ; c=m32add(cc,c) ; d=m32add(dd,d)
  194.  
  195. end
  196.  
  197. aa=c2x(reverse(a))||c2x(reverse(b))||c2x(reverse(C))||c2x(reverse(D))
  198. return aa
  199.  
  200. /* round 1 to 4 functins */
  201.  
  202. round1:procedure expose m. c1111 c0 c1
  203. parse arg a1,b1,c1,d1,kth,shift,sini
  204. kth=kth+1
  205. t1=c2d(a1)+c2d(f(b1,c1,d1))+ c2d(m.kth) + sini
  206. t1a=right(d2c(t1),4,c0)
  207. t2=rotleft(t1a,shift)
  208. t3=m32add(t2,b1)
  209. return t3
  210.  
  211. round2:procedure expose m. c1111 c0 c1
  212. parse arg a1,b1,c1,d1,kth,shift,sini
  213. kth=kth+1
  214. t1=c2d(a1)+c2d(g(b1,c1,d1))+ c2d(m.kth) + sini
  215. t1a=right(d2c(t1),4,c0)
  216. t2=rotleft(t1a,shift)
  217. t3=m32add(t2,b1)
  218. return t3
  219.  
  220. round3:procedure expose m. c1111 c0 c1
  221. parse arg a1,b1,c1,d1,kth,shift,sini
  222. kth=kth+1
  223. t1=c2d(a1)+c2d(h(b1,c1,d1))+ c2d(m.kth) + sini
  224. t1a=right(d2c(t1),4,c0)
  225. t2=rotleft(t1a,shift)
  226. t3=m32add(t2,b1)
  227. return t3
  228.  
  229. round4:procedure expose m. c1111 c0 c1
  230. parse arg a1,b1,c1,d1,kth,shift,sini
  231. kth=kth+1
  232. t1=c2d(a1)+c2d(i(b1,c1,d1))+ c2d(m.kth) + sini
  233. t1a=right(d2c(t1),4,c0)
  234. t2=rotleft(t1a,shift)
  235. t3=m32add(t2,b1)
  236. return t3
  237.  
  238. /* add to "char" numbers, modulo 2**32, return as char */
  239. m32add:procedure expose c0 c1 c1111
  240. parse arg v1,v2
  241. t1=c2d(v1)+c2d(v2)
  242. t2=d2c(t1)
  243. t3=right(t2,4,c0)
  244. return t3
  245.  
  246.  
  247.  
  248. /*********** Basic functions */
  249. /* F(x, y, z) == (((x) & (y)) | ((~x) & (z))) */
  250. f:procedure expose c0 c1 c1111 
  251. parse arg x,y,z
  252. t1=bitand(x,y)
  253. notx=bitxor(x,c1111)
  254. t2=bitand(notx,z)
  255. return bitor(t1,t2)
  256.  
  257.  
  258. /* G(x, y, z) == (((x) & (z)) | ((y) & (~z)))*/
  259. g:procedure expose c0 c1 c1111
  260. parse arg x,y,z
  261. t1=bitand(x,z)
  262. notz=bitxor(z,c1111)
  263. t2=bitand(y,notz)
  264. return bitor(t1,t2)
  265.  
  266. /* H(x, y, z) == ((x) ^ (y) ^ (z)) */
  267. h:procedure expose c0 c1 c1111
  268. parse arg x,y,z
  269. t1=bitxor(x,y)
  270. return bitxor(t1,z)
  271.  
  272. /* I(x, y, z) == ((y) ^ ((x) | (~z))) */
  273. i:procedure expose c0 c1 c1111
  274. parse arg x,y,z
  275. notz=bitxor(z,c1111)
  276. t2=bitor(x,notz)
  277. return bitxor(y,t2)
  278.  
  279. /* bit rotate to the left by s positions */
  280. rotleft:procedure 
  281. parse arg achar,s
  282. if s=0 then return achar
  283.  
  284. bits=x2b(c2x(achar))
  285. lb=length(bits)
  286. t1=left(bits,s)
  287. t2=bits||t1
  288. yib=right(t2,lb)
  289. return x2c(b2x(yib))
  290.  
  291.  
  292.  
  293.  
  294.  
  295.