• Share
  • ?
  • Profiles ▼
  • Communities ▼
  • Apps ▼

Blogs

  • My Blogs
  • Public Blogs
  • My Updates

This community can have members from outside your organization. HermannSW

  • Log in to participate

▼ Tags

▼ Similar Entries

Howto: easy callout ...

Blog: HermannSW
HermannSW 2700006U54
Updated
0 people like thisLikes 0
No CommentsComments 0

DataPower as a REST ...

Blog: Me, myself an...
Offline 0600018ME9
Updated
1 people likes thisLikes 1
No CommentsComments 0

应该立即使用 Node-RED 的五大理...

Blog: developerWork...
dwchina_community 310002PYN6
Updated
0 people like thisLikes 0
No CommentsComments 0

Understanding the NF...

Blog: Application I...
Aidan Harbison 310000JE0Y
Updated
4 people like thisLikes 4
No CommentsComments 0

DataPower and HTTP P...

Blog: Application I...
Chris Sloan 27000763MF
Updated
3 people like thisLikes 3
No CommentsComments 0

▼ Archive

  • March 2018
  • November 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • October 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011

▼ Blog Authors

HermannSW

View All Entries
Clicking the button causes a full page refresh. The user could go to the "Entry list" region to view the new content.) Entry list

GatewayScript / Modules / AES

HermannSW 2700006U54 | | Tags:  aes javascript gatewayscript coproc2 datapower ‎ | 4 Comments ‎ | 13,233 Views

A month ago blog posting coproc2gatewayscript provided a preview on new GatewayScript (Javascript on DataPower) available with version 7.0.0.0 of DataPower firmware later this month.


I never worked with modules and seeing a question on how to do AES encoding/decoding in GatewayScript I thought this is a great sample for learning about modules and easily make use of libraries found on the internet.

With the first release (7.0.0.0) GatewayScript will be available there will be no crypto library support.
(This is similar to XQuery/JSONiq new with version 6.0.0.0 having no access to dp:variables and HTTP header, that came with 6.0.1.0 firmware)


A word of caution here: 
It is always better to use Encrypt/Decrypt/Sig/Verify actions instead of DataPower crypto extension functions in XSLT.
Reason is that the actions are hardened against Oracle Padding attacks -- using crypto extension functions YOU are responsible.

Anyway, since 7.0.0.0 GatewayScript does not ship any crypto funcitons, using self written or found libraries is your only current option.
 

I googled and found this nice, self-contained and interactive AES JavaScript demo:
http://www.movable-type.co.uk/scripts/aes.html

Doing "View Page Source" I saw that these 4 code parts were included into the page:
utf8.js / base64.js / aes.js / aes-ctr.js

You get them unmodified if you do "Save Page as" as "complete webpage".


{"EDIT":"11-10_2014 - files on website did change, two are missing now. Attaching June 2 version of the files here for the purpose of easily trying out aes-demo with GatewayScript. Your further (real) work should based on latest version JavaScript from above website."}
https://www.ibm.com/developerworks/community/blogs/HermannSW/resource/BLOGS_UPLOADED_IMAGES/aes-6-2-14-files.zip
 

So I decided to make 4 modules out of them, store under "local:" on DataPower and just  require("aes-ctr")  in order to use it. To make functionality available from the module the last line "module.exports = ...;" had to be appended to each file. And in order to use functionality eg. from module "utf8"    var Utf8 = require("utf8");   had to be added at the top of the modules.

Only one more change had to be done in addition to make everything work on GatewayScript, these are all diffs:

 

Here you can see demonstration GatewayScript  aes.demo.js  in action:

$ coproc2 aes.demo.js ../ab.json http://dp6-l3:2227
 
     Password: L0ck it up saf3
    Plaintext: pssst ... đon’t tell anyøne!
Encrypted ...: GQK5heUZjVNxqjSRRj0HzMJwPrW+OMWOaL5RXzzvfklqdViJraQ8tQ==
... Decrypted: pssst ... đon’t tell anyøne!
 
128-bit: 69 c4 e0 d8 6a 7b 04 30 d8 cd b7 80 70 b4 c5 5a 
192-bit: dd a9 7c a4 86 4c df e0 6e af 70 a0 ec 0d 71 91 
256-bit: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89 
 
$ 
$ coproc2 aes.demo.js ../ab.json http://dp6-l3:2227
 
     Password: L0ck it up saf3
    Plaintext: pssst ... đon’t tell anyøne!
Encrypted ...: RQFLXOcZjVNuGspYI20khzUJKbmeb6WOFBSL/cQPfX+1pnr+pmQP+Q==
... Decrypted: pssst ... đon’t tell anyøne!
 
128-bit: 69 c4 e0 d8 6a 7b 04 30 d8 cd b7 80 70 b4 c5 5a 
192-bit: dd a9 7c a4 86 4c df e0 6e af 70 a0 ec 0d 71 91 
256-bit: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89 
 
$ 


The output for the three test vectors from Appendix C of NIST AES spec  should be

  • 128-bit: 69 c4 e0 d8 6a 7b 04 30 d8 cd b7 80 70 b4 c5 5a
  • 192-bit: dd a9 7c a4 86 4c df e0 6e af 70 a0 ec 0d 71 91
  • 256-bit: 8e a2 b7 ca 51 67 45 bf ea fc 49 90 4b 49 60 89

 

Now we have seen that  aes.demo.js  works fine, here is it completely:


 

The little helper function "_()" at the bottom of aes.demo.js was only intended to be a nice oneline implementation of "byte array to hex string" conversion. To my total surprise it seems to outperform other implementations by orders(!) of magnitude!?!?!

I verified that on jsperf.com, try it out yourself (I hope the test result is "real"):
http://jsperf.com/convert-byte-array-to-hex-string/7

(at least it produces the needed hexadecimal output for the NIST test vectors above)

<EDIT>
Vyacheslav Egorov pointed out that the measurements were flawed:
https://twitter.com/HermannSW/statuses/473824291109085184
</EDIT>


Hermann <myBlog/> <myTweets/> | <GraphvizFiddle/> | <xqib/> | <myCE/> <myFrameless/>

Modified on by HermannSW 2700006U54
  • Add a Comment Add a Comment
  • Edit
  • More Actions v
  • Quarantine this Entry
Notify Other People
notification

Send Email Notification

+

Quarantine this entry

deleteEntry
duplicateEntry

Mark as Duplicate

  • Previous Entry
  • Main
  • Next Entry
Feed for Blog Entries | Feed for Blog Comments | Feed for Comments for this Entry