aes_nss.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file aes_nss.c
  8. * \brief Use NSS to implement AES_CTR.
  9. **/
  10. #include "orconfig.h"
  11. #include "lib/crypt_ops/aes.h"
  12. #include "lib/crypt_ops/crypto_nss_mgt.h"
  13. #include "lib/crypt_ops/crypto_util.h"
  14. #include "lib/log/util_bug.h"
  15. DISABLE_GCC_WARNING(strict-prototypes)
  16. #include <pk11pub.h>
  17. #include <secerr.h>
  18. ENABLE_GCC_WARNING(strict-prototypes)
  19. aes_cnt_cipher_t *
  20. aes_new_cipher(const uint8_t *key, const uint8_t *iv,
  21. int key_bits)
  22. {
  23. const CK_MECHANISM_TYPE ckm = CKM_AES_CTR;
  24. SECItem keyItem = { .type = siBuffer,
  25. .data = (unsigned char *)key,
  26. .len = (key_bits / 8) };
  27. CK_AES_CTR_PARAMS params;
  28. params.ulCounterBits = 128;
  29. memcpy(params.cb, iv, 16);
  30. SECItem ivItem = { .type = siBuffer,
  31. .data = (unsigned char *)&params,
  32. .len = sizeof(params) };
  33. PK11SlotInfo *slot = NULL;
  34. PK11SymKey *keyObj = NULL;
  35. SECItem *ivObj = NULL;
  36. PK11Context *result = NULL;
  37. slot = PK11_GetBestSlot(ckm, NULL);
  38. if (!slot)
  39. goto err;
  40. keyObj = PK11_ImportSymKey(slot, ckm, PK11_OriginUnwrap,
  41. CKA_ENCRYPT, &keyItem, NULL);
  42. if (!keyObj)
  43. goto err;
  44. ivObj = PK11_ParamFromIV(ckm, &ivItem);
  45. if (!ivObj)
  46. goto err;
  47. PORT_SetError(SEC_ERROR_IO);
  48. result = PK11_CreateContextBySymKey(ckm, CKA_ENCRYPT, keyObj, ivObj);
  49. err:
  50. memwipe(&params, 0, sizeof(params));
  51. if (ivObj)
  52. SECITEM_FreeItem(ivObj, PR_TRUE);
  53. if (keyObj)
  54. PK11_FreeSymKey(keyObj);
  55. if (slot)
  56. PK11_FreeSlot(slot);
  57. tor_assert(result);
  58. return (aes_cnt_cipher_t *)result;
  59. }
  60. void
  61. aes_cipher_free_(aes_cnt_cipher_t *cipher)
  62. {
  63. if (!cipher)
  64. return;
  65. PK11_DestroyContext((PK11Context*) cipher, PR_TRUE);
  66. }
  67. void
  68. aes_crypt_inplace(aes_cnt_cipher_t *cipher, char *data_, size_t len_)
  69. {
  70. tor_assert(len_ <= INT_MAX);
  71. SECStatus s;
  72. PK11Context *ctx = (PK11Context*)cipher;
  73. unsigned char *data = (unsigned char *)data_;
  74. int len = (int) len_;
  75. int result_len = 0;
  76. s = PK11_CipherOp(ctx, data, &result_len, len, data, len);
  77. tor_assert(s == SECSuccess);
  78. tor_assert(result_len == len);
  79. }
  80. int
  81. evaluate_evp_for_aes(int force_value)
  82. {
  83. (void)force_value;
  84. return 0;
  85. }
  86. int
  87. evaluate_ctr_for_aes(void)
  88. {
  89. return 0;
  90. }