Crypto.ecl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*##############################################################################
  2. ## HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®. All rights reserved.
  3. ############################################################################## */
  4. EXPORT Crypto := MODULE
  5. IMPORT lib_cryptolib;
  6. /**
  7. * Returns set of supported Hash Algorithms
  8. *
  9. * @return SET OF STRING containing all supported Hash Algorithms
  10. */
  11. EXPORT SET OF STRING SupportedHashAlgorithms() := lib_cryptolib.CryptoLib.SupportedHashAlgorithms();
  12. /**
  13. * Returns set of supported CipherAlgorithms
  14. *
  15. * @return SET OF STRING containing all supported Cipher Algorithms
  16. */
  17. EXPORT SET OF STRING SupportedSymmetricCipherAlgorithms() := lib_cryptolib.CryptoLib.SupportedSymmetricCipherAlgorithms();
  18. /**
  19. * Returns set of supported Public Key Algorithms
  20. *
  21. * @return SET OF STRING containing all supported Public Key Algorithms
  22. */
  23. EXPORT SET OF STRING SupportedPublicKeyAlgorithms() := lib_cryptolib.CryptoLib.SupportedPublicKeyAlgorithms();
  24. /**
  25. * Hashing module containing all the supported hashing functions.
  26. *
  27. * @param hashAlgorithm The Hashing algorithm to use, as returned by SupportedHashAlgorithms()
  28. */
  29. EXPORT Hashing(VARSTRING hashAlgorithm) := MODULE
  30. /**
  31. * Create a hash of the given data, using a hash algorithm that
  32. * was returned by SupportedHashAlgorithms()
  33. *
  34. * @param inputData Data to hash
  35. * @return Hashed contents
  36. */
  37. EXPORT DATA Hash(DATA inputData) := FUNCTION
  38. RETURN lib_cryptolib.CryptoLib.Hash(hashAlgorithm, inputData);
  39. END;
  40. END; // Hashing module
  41. //-----
  42. /**
  43. * Encryption module containing all symmetric encryption/decryption functions
  44. *
  45. * @param algorithm Symmetric algorithm to use, as returned by SupportedSymmetricCipherAlgorithms()
  46. * @param passphrase Passphrase string to use for encryption/encryption
  47. */
  48. EXPORT SymmetricEncryption(VARSTRING algorithm, VARSTRING passphrase) := MODULE
  49. /**
  50. * Encrypt the given data, using the specified passphrase and symmetric cipher
  51. * algorithm that was returned by SupportedSymmetricCipherAlgorithms()
  52. *
  53. * @param inputData Contents to encrypt
  54. * @return Encrypted cipher
  55. */
  56. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  57. RETURN lib_cryptolib.CryptoLib.SymmetricEncrypt( algorithm, passphrase, inputData );
  58. END;
  59. /**
  60. * Decrypt the given cipher, using the specified passphrase and symmetric cipher
  61. * algorithm that was returned by SupportedSymmetricCipherAlgorithms()
  62. *
  63. * @param encryptedData Contents to decrypt
  64. * @return Decrypted data
  65. */
  66. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  67. RETURN lib_cryptolib.CryptoLib.SymmetricDecrypt( algorithm, passphrase, encryptedData );
  68. END;
  69. END; // SymmetricEncryption module
  70. /**
  71. * Encryption module containing symmetric encryption/decryption functions
  72. *
  73. * @param algorithm Symmetric algorithm to use, as returned by SupportedSymmetricCipherAlgorithms()
  74. * @param passphrase Passphrase to use for encryption/encryption
  75. */
  76. EXPORT SymmEncryption(VARSTRING algorithm, DATA passphrase) := MODULE
  77. /**
  78. * Encrypt the given data, using the specified passphrase and symmetric cipher
  79. * algorithm that was returned by SupportedSymmetricCipherAlgorithms()
  80. *
  81. * @param inputData Contents to encrypt
  82. * @return Encrypted cipher
  83. */
  84. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  85. RETURN lib_cryptolib.CryptoLib.SymEncrypt( algorithm, passphrase, inputData );
  86. END;
  87. /**
  88. * Decrypt the given cipher, using the specified passphrase and symmetric cipher
  89. * algorithm that was returned by SupportedSymmetricCipherAlgorithms()
  90. *
  91. * @param encryptedData Contents to decrypt
  92. * @return Decrypted data
  93. */
  94. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  95. RETURN lib_cryptolib.CryptoLib.SymDecrypt( algorithm, passphrase, encryptedData );
  96. END;
  97. END; // SymmEncryption module
  98. /**
  99. * Encryption module containing asymmetric encryption/decryption/digital
  100. * signing/signature verification functions
  101. *
  102. * @param pkAlgorithm ASymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  103. * @param publicKeyFile File specification of PEM formatted public key file
  104. * @param privateKeyFile File specification of PEM formatted private key file
  105. * @param passphrase Passphrase string to use for encryption/encryption/signing/verifying
  106. */
  107. EXPORT PublicKeyEncryption(VARSTRING pkAlgorithm, VARSTRING publicKeyFile = '', VARSTRING privateKeyFile = '', VARSTRING passphrase = '') := MODULE
  108. /**
  109. * Encrypt the given data, using the specified public key file,
  110. * passphrase, and algorithm
  111. *
  112. * @param inputData Contents to Encrypt
  113. * @return Encrypted data
  114. */
  115. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  116. RETURN lib_cryptolib.CryptoLib.Encrypt( pkAlgorithm, publicKeyFile, passphrase, inputData);
  117. END;
  118. /**
  119. * Decrypt the given encrypted data, using the specified private key file,
  120. * passphrase, and algorithm
  121. *
  122. * @param encryptedData Contents to Decrypt
  123. * @return Decrypted data
  124. */
  125. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  126. RETURN lib_cryptolib.CryptoLib.Decrypt( pkAlgorithm, privateKeyFile, passphrase, encryptedData);
  127. END;
  128. /**
  129. * Create a digital signature of the given data, using the
  130. * specified private key file, passphrase and algorithm
  131. *
  132. * @param inputData Contents to sign
  133. * @return Computed Digital signature
  134. */
  135. EXPORT DATA Sign( DATA inputData) := FUNCTION
  136. RETURN lib_cryptolib.CryptoLib.Sign( pkAlgorithm, privateKeyFile, passphrase, inputData);
  137. END;
  138. /**
  139. * Verify the given digital signature of the given data, using
  140. * the specified public key file, passphrase and algorithm
  141. *
  142. * @param signature Signature to verify
  143. * @param signedData Data used to create signature
  144. * @return Boolean TRUE/FALSE
  145. */
  146. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  147. RETURN lib_cryptolib.CryptoLib.VerifySignature( pkAlgorithm, publicKeyFile, passphrase, signature, signedData);
  148. END;
  149. END; // PublicKeyEncryption module
  150. /**
  151. * Encryption module containing asymmetric encryption/decryption/digital
  152. * signing/signature verification functions
  153. *
  154. * @param pkAlgorithm ASymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  155. * @param publicKeyFile File specification of PEM formatted public key file
  156. * @param privateKeyFile File specification of PEM formatted private key file
  157. * @param passphrase Passphrase to use for encryption/decryption/signing/verifying
  158. */
  159. EXPORT PKEncryption(VARSTRING pkAlgorithm, VARSTRING publicKeyFile = '', VARSTRING privateKeyFile = '', DATA passphrase = D'') := MODULE
  160. /**
  161. * Encrypt the given data, using the specified public key file,
  162. * passphrase, and algorithm
  163. *
  164. * @param inputData Contents to Encrypt
  165. * @return Encrypted data
  166. */
  167. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  168. RETURN lib_cryptolib.CryptoLib.PKEncrypt( pkAlgorithm, publicKeyFile, passphrase, inputData);
  169. END;
  170. /**
  171. * Decrypt the given encrypted data, using the specified private key file,
  172. * passphrase, and algorithm
  173. *
  174. * @param encryptedData Contents to Decrypt
  175. * @return Decrypted data
  176. */
  177. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  178. RETURN lib_cryptolib.CryptoLib.PKDecrypt( pkAlgorithm, privateKeyFile, passphrase, encryptedData);
  179. END;
  180. /**
  181. * Create a digital signature of the given data, using the
  182. * specified private key file, passphrase and algorithm
  183. *
  184. * @param inputData Contents to sign
  185. * @return Computed Digital signature
  186. */
  187. EXPORT DATA Sign( DATA inputData) := FUNCTION
  188. RETURN lib_cryptolib.CryptoLib.PKSign( pkAlgorithm, privateKeyFile, passphrase, inputData);
  189. END;
  190. /**
  191. * Verify the given digital signature of the given data, using
  192. * the specified public key file, passphrase and algorithm
  193. *
  194. * @param signature Signature to verify
  195. * @param signedData Data used to create signature
  196. * @return Boolean TRUE/FALSE
  197. */
  198. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  199. RETURN lib_cryptolib.CryptoLib.PKVerifySignature( pkAlgorithm, publicKeyFile, passphrase, signature, signedData);
  200. END;
  201. END; // PKEncryption module
  202. /**
  203. * Encryption module containing asymmetric encryption/decryption/digital
  204. * signing/signature verification functions
  205. *
  206. * @param pkAlgorithm Asymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  207. * @param publicKeyLFN LFN specification of PEM formatted public key file
  208. * @param privateKeyLFN LFN specification of PEM formatted private key file
  209. * @param passphrase Passphrase string to use for encryption/encryption/signing/verifying
  210. */
  211. EXPORT PublicKeyEncryptionFromLFN(VARSTRING pkAlgorithm, VARSTRING publicKeyLFN = '', VARSTRING privateKeyLFN = '', VARSTRING passphrase = '') := MODULE
  212. /**
  213. * Encrypt the given data, using the specified public key LFN,
  214. * passphrase, and algorithm
  215. *
  216. * @param inputData Contents to Encrypt
  217. * @return Encrypted data
  218. */
  219. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  220. RETURN lib_cryptolib.CryptoLib.EncryptLFN( pkAlgorithm, publicKeyLFN, passphrase, inputData);
  221. END;
  222. /**
  223. * Decrypt the given encrypted data, using the specified private key LFN,
  224. * passphrase, and algorithm
  225. *
  226. * @param encryptedData Contents to Decrypt
  227. * @return Decrypted data
  228. */
  229. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  230. RETURN lib_cryptolib.CryptoLib.DecryptLFN( pkAlgorithm, privateKeyLFN, passphrase, encryptedData);
  231. END;
  232. /**
  233. * Create a digital signature of the given data, using the
  234. * specified private key LFN, passphrase and algorithm
  235. *
  236. * @param inputData Contents to sign
  237. * @return Computed Digital signature
  238. */
  239. EXPORT DATA Sign( DATA inputData) := FUNCTION
  240. RETURN lib_cryptolib.CryptoLib.SignLFN( pkAlgorithm, privateKeyLFN, passphrase, inputData);
  241. END;
  242. /**
  243. * Verify the given digital signature of the given data, using
  244. * the specified public key LFN, passphrase and algorithm
  245. *
  246. * @param signature Signature to verify
  247. * @param signedData Data used to create signature
  248. * @return Boolean TRUE/FALSE
  249. */
  250. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  251. RETURN lib_cryptolib.CryptoLib.VerifySignatureLFN( pkAlgorithm, publicKeyLFN, passphrase, signature, signedData);
  252. END;
  253. END; // PublicKeyEncryptionFromLFN module
  254. /**
  255. * Encryption module containing asymmetric encryption/decryption/digital
  256. * signing/signature verification functions
  257. *
  258. * @param pkAlgorithm Asymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  259. * @param publicKeyLFN LFN specification of PEM formatted public key file
  260. * @param privateKeyLFN LFN specification of PEM formatted private key file
  261. * @param passphrase Passphrase to use for encryption/encryption/signing/verifying
  262. */
  263. EXPORT PKEncryptionFromLFN(VARSTRING pkAlgorithm, VARSTRING publicKeyLFN = '', VARSTRING privateKeyLFN = '', DATA passphrase = D'') := MODULE
  264. /**
  265. * Encrypt the given data, using the specified public key LFN,
  266. * passphrase, and algorithm
  267. *
  268. * @param inputData Contents to Encrypt
  269. * @return Encrypted data
  270. */
  271. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  272. RETURN lib_cryptolib.CryptoLib.PKEncryptLFN( pkAlgorithm, publicKeyLFN, passphrase, inputData);
  273. END;
  274. /**
  275. * Decrypt the given encrypted data, using the specified private key LFN,
  276. * passphrase, and algorithm
  277. *
  278. * @param encryptedData Contents to Decrypt
  279. * @return Decrypted data
  280. */
  281. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  282. RETURN lib_cryptolib.CryptoLib.PKDecryptLFN( pkAlgorithm, privateKeyLFN, passphrase, encryptedData);
  283. END;
  284. /**
  285. * Create a digital signature of the given data, using the
  286. * specified private key LFN, passphrase and algorithm
  287. *
  288. * @param inputData Contents to sign
  289. * @return Computed Digital signature
  290. */
  291. EXPORT DATA Sign( DATA inputData) := FUNCTION
  292. RETURN lib_cryptolib.CryptoLib.PKSignLFN( pkAlgorithm, privateKeyLFN, passphrase, inputData);
  293. END;
  294. /**
  295. * Verify the given digital signature of the given data, using
  296. * the specified public key LFN, passphrase and algorithm
  297. *
  298. * @param signature Signature to verify
  299. * @param signedData Data used to create signature
  300. * @return Boolean TRUE/FALSE
  301. */
  302. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  303. RETURN lib_cryptolib.CryptoLib.PKVerifySignatureLFN( pkAlgorithm, publicKeyLFN, passphrase, signature, signedData);
  304. END;
  305. END; // PKEncryptionFromLFN module
  306. /**
  307. * Encryption module containing all asymmetric encryption/decryption/digital
  308. * signing/signature verification functions
  309. *
  310. * @param pkAlgorithm ASymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  311. * @param publicKeyBuff PEM formatted Public key buffer
  312. * @param privateKeyBuff PEM formatted Private key buffer
  313. * @param passphrase Passphrase string to use for encryption/encryption/signing/verifying
  314. */
  315. EXPORT PublicKeyEncryptionFromBuffer(VARSTRING pkAlgorithm, VARSTRING publicKeyBuff = '', VARSTRING privateKeyBuff = '', VARSTRING passphrase = '') := MODULE
  316. /**
  317. * Encrypt the given data, using the specified public key, passphrase,
  318. * and algorithm
  319. *
  320. * @param inputData Contents to Encrypt
  321. * @return Encrypted data
  322. */
  323. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  324. RETURN lib_cryptolib.CryptoLib.EncryptBuff( pkAlgorithm, publicKeyBuff, passphrase, inputData);
  325. END;
  326. /**
  327. * Decrypt the given data, using the specified private key, passphrase,
  328. * and algorithm
  329. *
  330. * @param encryptedData Contents to Decrypt
  331. * @return Decrypted data
  332. */
  333. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  334. RETURN lib_cryptolib.CryptoLib.DecryptBuff(pkAlgorithm, privateKeyBuff, passphrase, encryptedData);
  335. END;
  336. /**
  337. * Create a digital signature of the given data, using the specified private key,
  338. * passphrase, and algorithm
  339. *
  340. * @param inputData Contents to sign
  341. * @return Computed digital signature
  342. */
  343. EXPORT DATA Sign(DATA inputData) := FUNCTION
  344. RETURN lib_cryptolib.CryptoLib.SignBuff( pkAlgorithm, privateKeyBuff, passphrase, inputData);
  345. END;
  346. /**
  347. * Verify the given digital signature of the given data, using the specified public key,
  348. * passphrase, and algorithm
  349. *
  350. * @param signature Signature to verify
  351. * @param signedData Data used to create signature
  352. * @return Booolean TRUE if signature is valid, otherwise FALSE
  353. */
  354. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  355. RETURN lib_cryptolib.CryptoLib.VerifySignatureBuff( pkAlgorithm, publicKeyBuff, passphrase, signature, signedData);
  356. END;
  357. END; // PublicKeyEncryptionFromBuffer module
  358. /**
  359. * Encryption module containing asymmetric encryption/decryption/digital
  360. * signing/signature verification functions
  361. *
  362. * @param pkAlgorithm ASymmetric algorithm to use, as returned by SupportedPublicKeyAlgorithms()
  363. * @param publicKeyBuff PEM formatted Public key buffer
  364. * @param privateKeyBuff PEM formatted Private key buffer
  365. * @param passphrase Passphrase to use for encryption/encryption/signing/verifying
  366. */
  367. EXPORT PKEncryptionFromBuffer(VARSTRING pkAlgorithm, VARSTRING publicKeyBuff = '', VARSTRING privateKeyBuff = '', DATA passphrase = D'') := MODULE
  368. /**
  369. * Encrypt the given data, using the specified public key, passphrase,
  370. * and algorithm
  371. *
  372. * @param inputData Contents to Encrypt
  373. * @return Encrypted data
  374. */
  375. EXPORT DATA Encrypt(DATA inputData) := FUNCTION
  376. RETURN lib_cryptolib.CryptoLib.PKEncryptBuff( pkAlgorithm, publicKeyBuff, passphrase, inputData);
  377. END;
  378. /**
  379. * Decrypt the given data, using the specified private key, passphrase,
  380. * and algorithm
  381. *
  382. * @param encryptedData Contents to Decrypt
  383. * @return Decrypted data
  384. */
  385. EXPORT DATA Decrypt(DATA encryptedData) := FUNCTION
  386. RETURN lib_cryptolib.CryptoLib.PKDecryptBuff(pkAlgorithm, privateKeyBuff, passphrase, encryptedData);
  387. END;
  388. /**
  389. * Create a digital signature of the given data, using the specified private key,
  390. * passphrase, and algorithm
  391. *
  392. * @param inputData Contents to sign
  393. * @return Computed digital signature
  394. */
  395. EXPORT DATA Sign(DATA inputData) := FUNCTION
  396. RETURN lib_cryptolib.CryptoLib.PKSignBuff( pkAlgorithm, privateKeyBuff, passphrase, inputData);
  397. END;
  398. /**
  399. * Verify the given digital signature of the given data, using the specified public key,
  400. * passphrase, and algorithm
  401. *
  402. * @param signature Signature to verify
  403. * @param signedData Data used to create signature
  404. * @return Booolean TRUE if signature is valid, otherwise FALSE
  405. */
  406. EXPORT BOOLEAN VerifySignature(DATA signature, DATA signedData) := FUNCTION
  407. RETURN lib_cryptolib.CryptoLib.PKVerifySignatureBuff( pkAlgorithm, publicKeyBuff, passphrase, signature, signedData);
  408. END;
  409. END; //PKEncryptionFromBuffer module
  410. END; // Crypto module