Source: security/tpm/tpm-key-handle.js

  1. /**
  2. * Copyright (C) 2017-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. * @author: From ndn-cxx security https://github.com/named-data/ndn-cxx/blob/master/src/security/tpm/key-handle.cpp
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * A copy of the GNU Lesser General Public License is in the file COPYING.
  19. */
  20. /** @ignore */
  21. var Name = require('../../name.js').Name; /** @ignore */
  22. var SyncPromise = require('../../util/sync-promise.js').SyncPromise;
  23. /**
  24. * TpmKeyHandle is an abstract base class for a TPM key handle, which provides
  25. * an interface to perform cryptographic operations with a key in the TPM.
  26. * @constructor
  27. */
  28. var TpmKeyHandle = function TpmKeyHandle()
  29. {
  30. this.keyName_ = new Name();
  31. };
  32. exports.TpmKeyHandle = TpmKeyHandle;
  33. /**
  34. * Compute a digital signature from the byte buffer using this key with
  35. * digestAlgorithm.
  36. * @param {number} digestAlgorithm The digest algorithm as an int from the
  37. * DigestAlgorithm enum.
  38. * @param {Buffer} data The input byte buffer.
  39. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  40. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  41. * an async Promise.
  42. * @return {Promise|SyncPromise} A promise which returns the signature Blob (or
  43. * an isNull Blob for an unrecognized digestAlgorithm), or a promise rejected
  44. * with TpmBackEnd.Error for an error in signing.
  45. */
  46. TpmKeyHandle.prototype.signPromise = function(digestAlgorithm, data, useSync)
  47. {
  48. return this.doSignPromise_(digestAlgorithm, data, useSync);
  49. };
  50. /**
  51. * Return the plain text which is decrypted from cipherText using this key.
  52. * @param {Buffer} cipherText The cipher text byte buffer.
  53. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  54. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  55. * an async Promise.
  56. * @return {Promise|SyncPromise} A promise which returns the decrypted data Blob,
  57. * or a promise rejected with TpmPrivateKey.Error for error decrypting.
  58. */
  59. TpmKeyHandle.prototype.decryptPromise = function(cipherText, useSync)
  60. {
  61. return this.doDecryptPromise_(cipherText, useSync);
  62. };
  63. /**
  64. * Get the encoded public key derived from this key.
  65. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  66. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  67. * an async Promise.
  68. * @return {Blob} The public key encoding Blob.
  69. */
  70. TpmKeyHandle.prototype.derivePublicKey = function(useSync)
  71. {
  72. return this.doDerivePublicKey_(useSync);
  73. };
  74. TpmKeyHandle.prototype.setKeyName = function(keyName)
  75. {
  76. this.keyName_ = new Name(keyName);
  77. };
  78. TpmKeyHandle.prototype.getKeyName = function() { return this.keyName_; };
  79. /**
  80. * A protected method to do the work of sign().
  81. * @param {Buffer} data The input byte buffer.
  82. * @param {number} digestAlgorithm The digest algorithm as an int from the
  83. * DigestAlgorithm enum.
  84. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  85. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  86. * an async Promise.
  87. * @return {Promise|SyncPromise} A promise which returns the signature Blob (or
  88. * an isNull Blob for an unrecognized digestAlgorithm), or a promise rejected
  89. * with TpmBackEnd.Error for an error in signing.
  90. */
  91. TpmKeyHandle.prototype.doSignPromise_ = function(digestAlgorithm, data, useSync)
  92. {
  93. return SyncPromise.reject(new Error
  94. ("TpmKeyHandle.doSignPromise_ is not implemented"));
  95. };
  96. /**
  97. * A protected method to do the work of decrypt().
  98. * @param {Buffer} cipherText The cipher text byte buffer.
  99. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  100. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  101. * an async Promise.
  102. * @return {Promise|SyncPromise} A promise which returns the decrypted data Blob,
  103. * or a promise rejected with TpmPrivateKey.Error for error decrypting.
  104. */
  105. TpmKeyHandle.prototype.doDecryptPromise_ = function(cipherText, useSync)
  106. {
  107. return SyncPromise.reject(new Error
  108. ("TpmKeyHandle.doDecryptPromise_ is not implemented"));
  109. };
  110. /**
  111. * A protected method to do the work of derivePublicKey().
  112. * @param {boolean} useSync (optional) If true then return a SyncPromise which
  113. * is already fulfilled. If omitted or false, this may return a SyncPromise or
  114. * an async Promise.
  115. * @return {Blob} The public key encoding Blob.
  116. */
  117. TpmKeyHandle.prototype.doDerivePublicKey_ = function(useSync)
  118. {
  119. return SyncPromise.reject(new Error
  120. ("TpmKeyHandle.doDerivePublicKey_ is not implemented"));
  121. };