edhoc 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (314) hide show
  1. package/README.md +170 -0
  2. package/binding.gyp +13 -11
  3. package/dist/bindings.d.ts +5 -0
  4. package/dist/bindings.d.ts.map +1 -0
  5. package/dist/bindings.js +10 -0
  6. package/dist/crypto.d.ts +29 -0
  7. package/dist/crypto.d.ts.map +1 -0
  8. package/dist/crypto.js +205 -0
  9. package/dist/edhoc.d.ts +368 -0
  10. package/dist/edhoc.d.ts.map +1 -0
  11. package/dist/edhoc.js +76 -0
  12. package/dist/index.d.ts +4 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +19 -0
  15. package/dist/x509credentials.d.ts +20 -0
  16. package/dist/x509credentials.d.ts.map +1 -0
  17. package/dist/x509credentials.js +140 -0
  18. package/external/libedhoc/backends/cbor/include/backend_cbor_bstr_type_decode.h +1 -1
  19. package/external/libedhoc/backends/cbor/include/backend_cbor_bstr_type_encode.h +1 -1
  20. package/external/libedhoc/backends/cbor/include/backend_cbor_bstr_type_types.h +1 -1
  21. package/external/libedhoc/backends/cbor/include/backend_cbor_ead_decode.h +2 -2
  22. package/external/libedhoc/backends/cbor/include/backend_cbor_ead_encode.h +2 -2
  23. package/external/libedhoc/backends/cbor/include/backend_cbor_edhoc_types.h +48 -43
  24. package/external/libedhoc/backends/cbor/include/backend_cbor_enc_structure_decode.h +1 -1
  25. package/external/libedhoc/backends/cbor/include/backend_cbor_enc_structure_encode.h +1 -1
  26. package/external/libedhoc/backends/cbor/include/backend_cbor_enc_structure_types.h +3 -3
  27. package/external/libedhoc/backends/cbor/include/backend_cbor_id_cred_x_decode.h +1 -1
  28. package/external/libedhoc/backends/cbor/include/backend_cbor_id_cred_x_encode.h +1 -1
  29. package/external/libedhoc/backends/cbor/include/backend_cbor_info_decode.h +1 -1
  30. package/external/libedhoc/backends/cbor/include/backend_cbor_info_encode.h +1 -1
  31. package/external/libedhoc/backends/cbor/include/backend_cbor_int_type_decode.h +1 -1
  32. package/external/libedhoc/backends/cbor/include/backend_cbor_int_type_encode.h +1 -1
  33. package/external/libedhoc/backends/cbor/include/backend_cbor_int_type_types.h +1 -1
  34. package/external/libedhoc/backends/cbor/include/backend_cbor_message_1_decode.h +1 -1
  35. package/external/libedhoc/backends/cbor/include/backend_cbor_message_1_encode.h +1 -1
  36. package/external/libedhoc/backends/cbor/include/backend_cbor_message_2_decode.h +1 -1
  37. package/external/libedhoc/backends/cbor/include/backend_cbor_message_2_encode.h +1 -1
  38. package/external/libedhoc/backends/cbor/include/backend_cbor_message_3_decode.h +1 -1
  39. package/external/libedhoc/backends/cbor/include/backend_cbor_message_3_encode.h +1 -1
  40. package/external/libedhoc/backends/cbor/include/backend_cbor_message_4_decode.h +1 -1
  41. package/external/libedhoc/backends/cbor/include/backend_cbor_message_4_encode.h +1 -1
  42. package/external/libedhoc/backends/cbor/include/backend_cbor_message_error_decode.h +1 -1
  43. package/external/libedhoc/backends/cbor/include/backend_cbor_message_error_encode.h +1 -1
  44. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_2_decode.h +1 -1
  45. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_2_encode.h +1 -1
  46. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_3_decode.h +1 -1
  47. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_3_encode.h +1 -1
  48. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_4_decode.h +3 -3
  49. package/external/libedhoc/backends/cbor/include/backend_cbor_plaintext_4_encode.h +3 -3
  50. package/external/libedhoc/backends/cbor/include/backend_cbor_sig_structure_decode.h +1 -1
  51. package/external/libedhoc/backends/cbor/include/backend_cbor_sig_structure_encode.h +1 -1
  52. package/external/libedhoc/backends/cbor/include/backend_cbor_sig_structure_types.h +4 -4
  53. package/external/libedhoc/backends/cbor/include/backend_cbor_x509_types.h +93 -83
  54. package/external/libedhoc/backends/cbor/src/backend_cbor_bstr_type_decode.c +11 -20
  55. package/external/libedhoc/backends/cbor/src/backend_cbor_bstr_type_encode.c +11 -20
  56. package/external/libedhoc/backends/cbor/src/backend_cbor_ead_decode.c +27 -32
  57. package/external/libedhoc/backends/cbor/src/backend_cbor_ead_encode.c +27 -32
  58. package/external/libedhoc/backends/cbor/src/backend_cbor_enc_structure_decode.c +14 -23
  59. package/external/libedhoc/backends/cbor/src/backend_cbor_enc_structure_encode.c +14 -23
  60. package/external/libedhoc/backends/cbor/src/backend_cbor_id_cred_x_decode.c +63 -52
  61. package/external/libedhoc/backends/cbor/src/backend_cbor_id_cred_x_encode.c +62 -51
  62. package/external/libedhoc/backends/cbor/src/backend_cbor_info_decode.c +14 -23
  63. package/external/libedhoc/backends/cbor/src/backend_cbor_info_encode.c +14 -23
  64. package/external/libedhoc/backends/cbor/src/backend_cbor_int_type_decode.c +11 -20
  65. package/external/libedhoc/backends/cbor/src/backend_cbor_int_type_encode.c +11 -20
  66. package/external/libedhoc/backends/cbor/src/backend_cbor_message_1_decode.c +54 -51
  67. package/external/libedhoc/backends/cbor/src/backend_cbor_message_1_encode.c +54 -51
  68. package/external/libedhoc/backends/cbor/src/backend_cbor_message_2_decode.c +11 -20
  69. package/external/libedhoc/backends/cbor/src/backend_cbor_message_2_encode.c +11 -20
  70. package/external/libedhoc/backends/cbor/src/backend_cbor_message_3_decode.c +11 -20
  71. package/external/libedhoc/backends/cbor/src/backend_cbor_message_3_encode.c +11 -20
  72. package/external/libedhoc/backends/cbor/src/backend_cbor_message_4_decode.c +11 -20
  73. package/external/libedhoc/backends/cbor/src/backend_cbor_message_4_encode.c +11 -20
  74. package/external/libedhoc/backends/cbor/src/backend_cbor_message_error_decode.c +36 -37
  75. package/external/libedhoc/backends/cbor/src/backend_cbor_message_error_encode.c +36 -37
  76. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_2_decode.c +103 -80
  77. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_2_encode.c +102 -79
  78. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_3_decode.c +99 -76
  79. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_3_encode.c +98 -75
  80. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_4_decode.c +40 -41
  81. package/external/libedhoc/backends/cbor/src/backend_cbor_plaintext_4_encode.c +40 -41
  82. package/external/libedhoc/backends/cbor/src/backend_cbor_sig_structure_decode.c +15 -24
  83. package/external/libedhoc/backends/cbor/src/backend_cbor_sig_structure_encode.c +15 -24
  84. package/external/libedhoc/externals/Unity/examples/example_1/src/ProductionCode.c +31 -0
  85. package/external/libedhoc/externals/Unity/examples/example_1/src/ProductionCode.h +10 -0
  86. package/external/libedhoc/externals/Unity/examples/example_1/src/ProductionCode2.c +18 -0
  87. package/external/libedhoc/externals/Unity/examples/example_1/src/ProductionCode2.h +9 -0
  88. package/external/libedhoc/externals/Unity/examples/example_1/test/TestProductionCode.c +69 -0
  89. package/external/libedhoc/externals/Unity/examples/example_1/test/TestProductionCode2.c +38 -0
  90. package/external/libedhoc/externals/Unity/examples/example_1/test/test_runners/TestProductionCode2_Runner.c +53 -0
  91. package/external/libedhoc/externals/Unity/examples/example_1/test/test_runners/TestProductionCode_Runner.c +57 -0
  92. package/external/libedhoc/externals/Unity/examples/example_2/src/ProductionCode.c +31 -0
  93. package/external/libedhoc/externals/Unity/examples/example_2/src/ProductionCode.h +10 -0
  94. package/external/libedhoc/externals/Unity/examples/example_2/src/ProductionCode2.c +18 -0
  95. package/external/libedhoc/externals/Unity/examples/example_2/src/ProductionCode2.h +9 -0
  96. package/external/libedhoc/externals/Unity/examples/example_2/test/TestProductionCode.c +71 -0
  97. package/external/libedhoc/externals/Unity/examples/example_2/test/TestProductionCode2.c +40 -0
  98. package/external/libedhoc/externals/Unity/examples/example_2/test/test_runners/TestProductionCode2_Runner.c +16 -0
  99. package/external/libedhoc/externals/Unity/examples/example_2/test/test_runners/TestProductionCode_Runner.c +18 -0
  100. package/external/libedhoc/externals/Unity/examples/example_2/test/test_runners/all_tests.c +19 -0
  101. package/external/libedhoc/externals/Unity/examples/example_3/helper/UnityHelper.c +17 -0
  102. package/external/libedhoc/externals/Unity/examples/example_3/helper/UnityHelper.h +19 -0
  103. package/external/libedhoc/externals/Unity/examples/example_3/src/ProductionCode.c +31 -0
  104. package/external/libedhoc/externals/Unity/examples/example_3/src/ProductionCode.h +10 -0
  105. package/external/libedhoc/externals/Unity/examples/example_3/src/ProductionCode2.c +18 -0
  106. package/external/libedhoc/externals/Unity/examples/example_3/src/ProductionCode2.h +9 -0
  107. package/external/libedhoc/externals/Unity/examples/example_3/test/TestProductionCode.c +69 -0
  108. package/external/libedhoc/externals/Unity/examples/example_3/test/TestProductionCode2.c +38 -0
  109. package/external/libedhoc/externals/Unity/examples/example_4/src/ProductionCode.c +31 -0
  110. package/external/libedhoc/externals/Unity/examples/example_4/src/ProductionCode.h +10 -0
  111. package/external/libedhoc/externals/Unity/examples/example_4/src/ProductionCode2.c +18 -0
  112. package/external/libedhoc/externals/Unity/examples/example_4/src/ProductionCode2.h +9 -0
  113. package/external/libedhoc/externals/Unity/examples/example_4/test/TestProductionCode.c +70 -0
  114. package/external/libedhoc/externals/Unity/examples/example_4/test/TestProductionCode2.c +42 -0
  115. package/external/libedhoc/externals/Unity/examples/example_4/test/test_runners/TestProductionCode2_Runner.c +53 -0
  116. package/external/libedhoc/externals/Unity/examples/example_4/test/test_runners/TestProductionCode_Runner.c +57 -0
  117. package/external/libedhoc/externals/Unity/examples/unity_config.h +251 -0
  118. package/external/libedhoc/externals/Unity/extras/bdd/src/unity_bdd.h +44 -0
  119. package/external/libedhoc/externals/Unity/extras/bdd/test/test_bdd.c +129 -0
  120. package/external/libedhoc/externals/Unity/extras/fixture/src/unity_fixture.c +310 -0
  121. package/external/libedhoc/externals/Unity/extras/fixture/src/unity_fixture.h +95 -0
  122. package/external/libedhoc/externals/Unity/extras/fixture/src/unity_fixture_internals.h +51 -0
  123. package/external/libedhoc/externals/Unity/extras/fixture/test/main/AllTests.c +20 -0
  124. package/external/libedhoc/externals/Unity/extras/fixture/test/template_fixture_tests.c +40 -0
  125. package/external/libedhoc/externals/Unity/extras/fixture/test/unity_fixture_Test.c +246 -0
  126. package/external/libedhoc/externals/Unity/extras/fixture/test/unity_fixture_TestRunner.c +33 -0
  127. package/external/libedhoc/externals/Unity/extras/memory/src/unity_memory.c +203 -0
  128. package/external/libedhoc/externals/Unity/extras/memory/src/unity_memory.h +61 -0
  129. package/external/libedhoc/externals/Unity/extras/memory/test/unity_memory_Test.c +326 -0
  130. package/external/libedhoc/externals/Unity/extras/memory/test/unity_memory_TestRunner.c +50 -0
  131. package/external/libedhoc/externals/Unity/extras/memory/test/unity_output_Spy.c +57 -0
  132. package/external/libedhoc/externals/Unity/extras/memory/test/unity_output_Spy.h +17 -0
  133. package/external/libedhoc/externals/Unity/src/unity.c +2501 -0
  134. package/external/libedhoc/externals/Unity/src/unity.h +698 -0
  135. package/external/libedhoc/externals/Unity/src/unity_internals.h +1183 -0
  136. package/external/libedhoc/externals/Unity/test/expectdata/testsample_cmd.c +61 -0
  137. package/external/libedhoc/externals/Unity/test/expectdata/testsample_def.c +57 -0
  138. package/external/libedhoc/externals/Unity/test/expectdata/testsample_head1.c +55 -0
  139. package/external/libedhoc/externals/Unity/test/expectdata/testsample_head1.h +15 -0
  140. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_cmd.c +80 -0
  141. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_def.c +76 -0
  142. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_head1.c +75 -0
  143. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_head1.h +13 -0
  144. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_new1.c +89 -0
  145. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_new2.c +89 -0
  146. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_param.c +77 -0
  147. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_run1.c +89 -0
  148. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_run2.c +89 -0
  149. package/external/libedhoc/externals/Unity/test/expectdata/testsample_mock_yaml.c +90 -0
  150. package/external/libedhoc/externals/Unity/test/expectdata/testsample_new1.c +67 -0
  151. package/external/libedhoc/externals/Unity/test/expectdata/testsample_new2.c +70 -0
  152. package/external/libedhoc/externals/Unity/test/expectdata/testsample_param.c +58 -0
  153. package/external/libedhoc/externals/Unity/test/expectdata/testsample_run1.c +67 -0
  154. package/external/libedhoc/externals/Unity/test/expectdata/testsample_run2.c +70 -0
  155. package/external/libedhoc/externals/Unity/test/expectdata/testsample_yaml.c +71 -0
  156. package/external/libedhoc/externals/Unity/test/testdata/CException.h +18 -0
  157. package/external/libedhoc/externals/Unity/test/testdata/Defs.h +16 -0
  158. package/external/libedhoc/externals/Unity/test/testdata/cmock.h +21 -0
  159. package/external/libedhoc/externals/Unity/test/testdata/mockMock.h +20 -0
  160. package/external/libedhoc/externals/Unity/test/testdata/testRunnerGenerator.c +204 -0
  161. package/external/libedhoc/externals/Unity/test/testdata/testRunnerGeneratorSmall.c +73 -0
  162. package/external/libedhoc/externals/Unity/test/testdata/testRunnerGeneratorWithMocks.c +200 -0
  163. package/external/libedhoc/externals/Unity/test/tests/self_assessment_utils.h +151 -0
  164. package/external/libedhoc/externals/Unity/test/tests/test_unity_arrays.c +2941 -0
  165. package/external/libedhoc/externals/Unity/test/tests/test_unity_core.c +375 -0
  166. package/external/libedhoc/externals/Unity/test/tests/test_unity_doubles.c +1285 -0
  167. package/external/libedhoc/externals/Unity/test/tests/test_unity_floats.c +1395 -0
  168. package/external/libedhoc/externals/Unity/test/tests/test_unity_integers.c +2863 -0
  169. package/external/libedhoc/externals/Unity/test/tests/test_unity_integers_64.c +783 -0
  170. package/external/libedhoc/externals/Unity/test/tests/test_unity_memory.c +82 -0
  171. package/external/libedhoc/externals/Unity/test/tests/test_unity_parameterized.c +309 -0
  172. package/external/libedhoc/externals/Unity/test/tests/test_unity_parameterizedDemo.c +28 -0
  173. package/external/libedhoc/externals/Unity/test/tests/test_unity_strings.c +330 -0
  174. package/external/libedhoc/externals/Unity/test/tests/types_for_test.h +21 -0
  175. package/external/libedhoc/externals/zcbor/include/zcbor_common.h +147 -60
  176. package/external/libedhoc/externals/zcbor/include/zcbor_decode.h +291 -202
  177. package/external/libedhoc/externals/zcbor/include/zcbor_encode.h +100 -156
  178. package/external/libedhoc/externals/zcbor/include/zcbor_print.h +165 -0
  179. package/external/libedhoc/externals/zcbor/samples/hello_world/src/main.c +1 -1
  180. package/external/libedhoc/externals/zcbor/samples/pet/include/pet_decode.h +1 -1
  181. package/external/libedhoc/externals/zcbor/samples/pet/include/pet_encode.h +1 -1
  182. package/external/libedhoc/externals/zcbor/samples/pet/include/pet_types.h +4 -4
  183. package/external/libedhoc/externals/zcbor/samples/pet/src/main.c +4 -4
  184. package/external/libedhoc/externals/zcbor/samples/pet/src/pet_decode.c +15 -24
  185. package/external/libedhoc/externals/zcbor/samples/pet/src/pet_encode.c +14 -23
  186. package/external/libedhoc/externals/zcbor/src/zcbor_common.c +202 -29
  187. package/external/libedhoc/externals/zcbor/src/zcbor_decode.c +633 -173
  188. package/external/libedhoc/externals/zcbor/src/zcbor_encode.c +71 -184
  189. package/external/libedhoc/externals/zcbor/tests/decode/test1_suit_old_formats/src/main.c +152 -153
  190. package/external/libedhoc/externals/zcbor/tests/decode/test2_suit/src/main.c +64 -64
  191. package/external/libedhoc/externals/zcbor/tests/decode/test3_simple/src/main.c +15 -16
  192. package/external/libedhoc/externals/zcbor/tests/decode/test5_corner_cases/src/main.c +228 -84
  193. package/external/libedhoc/externals/zcbor/tests/decode/test7_suit9_simple/src/main.c +9 -9
  194. package/external/libedhoc/externals/zcbor/tests/decode/test8_suit12/src/main.c +0 -1
  195. package/external/libedhoc/externals/zcbor/tests/decode/test9_manifest14/src/main.c +172 -173
  196. package/external/libedhoc/externals/zcbor/tests/encode/test1_suit/src/main.c +73 -73
  197. package/external/libedhoc/externals/zcbor/tests/encode/test2_simple/src/main.c +1 -2
  198. package/external/libedhoc/externals/zcbor/tests/encode/test3_corner_cases/src/main.c +83 -56
  199. package/external/libedhoc/externals/zcbor/tests/encode/test4_senml/src/main.c +15 -16
  200. package/external/libedhoc/externals/zcbor/tests/fuzz/fuzz_everything.c +12 -0
  201. package/external/libedhoc/externals/zcbor/tests/fuzz/fuzz_manifest12.c +69 -69
  202. package/external/libedhoc/externals/zcbor/tests/unit/test1_unit_tests/src/main.c +448 -55
  203. package/external/libedhoc/externals/zcbor/tests/unit/test3_float16/src/main.c +49 -48
  204. package/external/libedhoc/include/edhoc.h +348 -54
  205. package/external/libedhoc/include/edhoc_common.h +289 -0
  206. package/external/libedhoc/include/edhoc_context.h +60 -35
  207. package/external/libedhoc/include/edhoc_credentials.h +55 -7
  208. package/external/libedhoc/include/edhoc_crypto.h +10 -10
  209. package/external/libedhoc/include/edhoc_ead.h +13 -10
  210. package/external/libedhoc/include/edhoc_macros.h +20 -11
  211. package/external/libedhoc/include/edhoc_values.h +2 -2
  212. package/external/libedhoc/library/edhoc.c +70 -36
  213. package/external/libedhoc/library/edhoc_common.c +1314 -0
  214. package/external/libedhoc/library/edhoc_exporter.c +45 -46
  215. package/external/libedhoc/library/edhoc_message_1.c +107 -72
  216. package/external/libedhoc/library/edhoc_message_2.c +396 -1693
  217. package/external/libedhoc/library/edhoc_message_3.c +465 -1656
  218. package/external/libedhoc/library/edhoc_message_4.c +93 -91
  219. package/external/libedhoc/library/edhoc_message_error.c +41 -41
  220. package/external/libedhoc/tests/include/{cipher_suites/cipher_suite_0.h → cipher_suite_0.h} +131 -134
  221. package/external/libedhoc/tests/include/{cipher_suites/cipher_suite_2.h → cipher_suite_2.h} +139 -140
  222. package/external/libedhoc/tests/include/{edhoc_trace_1/test_vector_1.h → test_vector_rfc9529_chapter_2.h} +786 -738
  223. package/external/libedhoc/tests/include/{edhoc_trace_2/test_vector_2.h → test_vector_rfc9529_chapter_3.h} +14 -10
  224. package/external/libedhoc/tests/include/{x509_chain_cs_0/test_vector_x5chain_cs_0.h → test_vector_x5chain_sign_keys_suite_0.h} +137 -140
  225. package/external/libedhoc/tests/include/{x509_chain_cs_2/test_vector_x5chain_cs_2.h → test_vector_x5chain_sign_keys_suite_2.h} +7 -8
  226. package/external/libedhoc/tests/include/{x509_chain_cs_2_static_dh/test_vector_x5chain_cs_2_static_dh.h → test_vector_x5chain_static_dh_keys_suite_2.h} +7 -8
  227. package/external/libedhoc/tests/include/{x509_hash_cs_2/test_vector_x5t_cs_2.h → test_vector_x5t_sign_keys_suite_2.h} +6 -7
  228. package/external/libedhoc/tests/src/{cipher_suites/cipher_suite_0.c → cipher_suite_0.c} +445 -447
  229. package/external/libedhoc/tests/src/{cipher_suites/cipher_suite_2.c → cipher_suite_2.c} +600 -600
  230. package/external/libedhoc/tests/src/module_test_api.c +430 -0
  231. package/external/libedhoc/tests/src/module_test_cipher_suite_0.c +395 -0
  232. package/external/libedhoc/tests/src/module_test_cipher_suite_2.c +392 -0
  233. package/external/libedhoc/tests/src/{error_message/test_edhoc_error_message.c → module_test_error_message.c} +94 -69
  234. package/external/libedhoc/tests/src/module_test_main.c +49 -0
  235. package/external/libedhoc/tests/src/{cipher_suite_negotiation/test_edhoc_cipher_suite_negotiation.c → module_test_rfc9528_suites_negotiation.c} +224 -227
  236. package/external/libedhoc/tests/src/module_test_rfc9529_chapter_2.c +2681 -0
  237. package/external/libedhoc/tests/src/module_test_rfc9529_chapter_3.c +1635 -0
  238. package/external/libedhoc/tests/src/module_test_x5chain_sign_keys_suite_0.c +1135 -0
  239. package/external/libedhoc/tests/src/module_test_x5chain_sign_keys_suite_2.c +1249 -0
  240. package/external/libedhoc/tests/src/module_test_x5chain_static_dh_keys_suite_2.c +798 -0
  241. package/external/libedhoc/tests/src/module_test_x5t_sign_keys_suite_2.c +956 -0
  242. package/include/EdhocComposeAsyncWorker.h +8 -6
  243. package/include/EdhocCredentialManager.h +16 -11
  244. package/include/EdhocCryptoManager.h +28 -21
  245. package/include/EdhocEadManager.h +3 -6
  246. package/include/{EdhocExportAsyncWorker.h → EdhocExportOscoreAsyncWorker.h} +20 -17
  247. package/include/EdhocKeyExporterAsyncWorker.h +76 -0
  248. package/include/EdhocKeyUpdateAsyncWorker.h +72 -0
  249. package/include/EdhocProcessAsyncWorker.h +14 -10
  250. package/include/LibEDHOC.h +44 -20
  251. package/include/UserContext.h +7 -9
  252. package/include/Utils.h +31 -27
  253. package/package.json +14 -3
  254. package/prebuilds/android-arm/edhoc.armv7.node +0 -0
  255. package/prebuilds/android-arm64/edhoc.armv8.node +0 -0
  256. package/prebuilds/darwin-arm64/edhoc.node +0 -0
  257. package/prebuilds/darwin-x64/edhoc.node +0 -0
  258. package/prebuilds/linux-arm/edhoc.armv6.node +0 -0
  259. package/prebuilds/linux-arm/edhoc.armv7.node +0 -0
  260. package/prebuilds/linux-arm64/edhoc.armv8.node +0 -0
  261. package/prebuilds/linux-x64/edhoc.glibc.node +0 -0
  262. package/prebuilds/linux-x64/edhoc.musl.node +0 -0
  263. package/prebuilds/win32-ia32/edhoc.node +0 -0
  264. package/prebuilds/win32-x64/edhoc.node +0 -0
  265. package/src/EdhocComposeAsyncWorker.cpp +18 -35
  266. package/src/EdhocCredentialManager.cpp +160 -187
  267. package/src/EdhocCryptoManager.cpp +386 -586
  268. package/src/EdhocEadManager.cpp +10 -18
  269. package/src/EdhocExportOscoreAsyncWorker.cpp +77 -0
  270. package/src/EdhocKeyExporterAsyncWorker.cpp +49 -0
  271. package/src/EdhocKeyUpdateAsyncWorker.cpp +41 -0
  272. package/src/EdhocProcessAsyncWorker.cpp +72 -19
  273. package/src/LibEDHOC.cpp +213 -174
  274. package/src/Suites.cpp +39 -72
  275. package/src/Utils.cpp +32 -56
  276. package/test/basic.test.ts +64 -0
  277. package/test/vectors.test.ts +111 -0
  278. package/external/libedhoc/externals/zcbor/include/zcbor_debug.h +0 -69
  279. package/external/libedhoc/tests/include/cipher_suite_negotiation/test_edhoc_cipher_suite_negotiation.h +0 -37
  280. package/external/libedhoc/tests/include/cipher_suites/test_cipher_suite_0.h +0 -48
  281. package/external/libedhoc/tests/include/cipher_suites/test_cipher_suite_2.h +0 -48
  282. package/external/libedhoc/tests/include/edhoc_trace_1/authentication_credentials_1.h +0 -60
  283. package/external/libedhoc/tests/include/edhoc_trace_1/test_edhoc_handshake_1.h +0 -208
  284. package/external/libedhoc/tests/include/edhoc_trace_1/test_edhoc_handshake_ead_1.h +0 -59
  285. package/external/libedhoc/tests/include/edhoc_trace_2/authentication_credentials_2.h +0 -60
  286. package/external/libedhoc/tests/include/edhoc_trace_2/test_edhoc_handshake_2.h +0 -199
  287. package/external/libedhoc/tests/include/error_message/test_edhoc_error_message.h +0 -48
  288. package/external/libedhoc/tests/include/x509_chain_cs_0/authentication_credentials_x5chain_cs_0.h +0 -92
  289. package/external/libedhoc/tests/include/x509_chain_cs_0/test_edhoc_handshake_x5chain_cs_0.h +0 -96
  290. package/external/libedhoc/tests/include/x509_chain_cs_2/authentication_credentials_x5chain_cs_2.h +0 -58
  291. package/external/libedhoc/tests/include/x509_chain_cs_2/test_edhoc_handshake_x5chain_cs_2.h +0 -56
  292. package/external/libedhoc/tests/include/x509_chain_cs_2/test_edhoc_handshake_x5chain_cs_2_ead.h +0 -57
  293. package/external/libedhoc/tests/include/x509_chain_cs_2_static_dh/authentication_credentials_x5chain_cs_2_static_dh.h +0 -59
  294. package/external/libedhoc/tests/include/x509_chain_cs_2_static_dh/test_edhoc_handshake_x5chain_cs_2_static_dh_ead.h +0 -57
  295. package/external/libedhoc/tests/include/x509_hash_cs_2/authentication_credentials_x5t_cs_2.h +0 -60
  296. package/external/libedhoc/tests/include/x509_hash_cs_2/test_edhoc_handshake_x5t_cs_2_ead.h +0 -57
  297. package/external/libedhoc/tests/src/cipher_suites/test_cipher_suite_0.c +0 -475
  298. package/external/libedhoc/tests/src/cipher_suites/test_cipher_suite_2.c +0 -473
  299. package/external/libedhoc/tests/src/edhoc_trace_1/authentication_credentials_1.c +0 -252
  300. package/external/libedhoc/tests/src/edhoc_trace_1/test_edhoc_handshake_1.c +0 -1829
  301. package/external/libedhoc/tests/src/edhoc_trace_1/test_edhoc_handshake_ead_1.c +0 -1247
  302. package/external/libedhoc/tests/src/edhoc_trace_2/authentication_credentials_2.c +0 -170
  303. package/external/libedhoc/tests/src/edhoc_trace_2/test_edhoc_handshake_2.c +0 -1783
  304. package/external/libedhoc/tests/src/tests.c +0 -228
  305. package/external/libedhoc/tests/src/x509_chain_cs_0/authentication_credentials_x5chain_cs_0.c +0 -332
  306. package/external/libedhoc/tests/src/x509_chain_cs_0/test_edhoc_handshake_x5chain_cs_0.c +0 -936
  307. package/external/libedhoc/tests/src/x509_chain_cs_2/authentication_credentials_x5chain_cs_2.c +0 -166
  308. package/external/libedhoc/tests/src/x509_chain_cs_2/test_edhoc_handshake_x5chain_cs_2.c +0 -587
  309. package/external/libedhoc/tests/src/x509_chain_cs_2/test_edhoc_handshake_x5chain_cs_2_ead.c +0 -917
  310. package/external/libedhoc/tests/src/x509_chain_cs_2_static_dh/authentication_credentials_x5chain_cs_2_static_dh.c +0 -186
  311. package/external/libedhoc/tests/src/x509_chain_cs_2_static_dh/test_edhoc_handshake_x5chain_cs_2_static_dh_ead.c +0 -743
  312. package/external/libedhoc/tests/src/x509_hash_cs_2/authentication_credentials_x5t_cs_2.c +0 -261
  313. package/external/libedhoc/tests/src/x509_hash_cs_2/test_edhoc_handshake_x5t_cs_2_ead.c +0 -854
  314. package/src/EdhocExportAsyncWorker.cpp +0 -82
package/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Node EDHOC
2
+
3
+ A TypeScript Node.js library implemented as a native addon, built on top of the C library [`libedhoc`](https://github.com/kamil-kielbasa/libedhoc/). It provides an efficient and lightweight way to use the Ephemeral Diffie-Hellman Over COSE (EDHOC) protocol, as specified in [RFC 9528](https://datatracker.ietf.org/doc/rfc9528/).
4
+
5
+ ## Overview
6
+
7
+ EDHOC is designed for lightweight communication and is particularly suitable for protocols like CoAP and OSCORE, but can work independently of the application and transport layers, ensuring minimal overhead while maintaining strong security guarantees. The library provides a default software implementation for X.509 credentials, with support for additional formats such as C509, CWT, and CCS coming soon. It also includes a software-based cryptographic implementation utilizing [`@noble/curves`](https://www.npmjs.com/package/@noble/curves/v/1.0.0). Additionally, it exposes credential and cryptographic API interfaces to allow for custom implementations, such as PKCS#11-based solutions.
8
+
9
+ ## Features
10
+
11
+ - Full implementation of the EDHOC protocol (RFC 9528)
12
+ - TypeScript support for type safety and better developer experience
13
+ - Based on [`libedhoc`](https://github.com/kamil-kielbasa/libedhoc), a proven C implementation
14
+
15
+ ## Installation
16
+
17
+ Install the package via npm:
18
+
19
+ ```bash
20
+ npm install node-edhoc
21
+ ```
22
+
23
+ ## Usage Examples
24
+
25
+ ### Basic Handshake
26
+
27
+ The simplest EDHOC handshake using pre-shared keys (Method 0):
28
+
29
+ ```typescript
30
+ import { EDHOC, EdhocMethod, EdhocSuite } from 'node-edhoc';
31
+
32
+ // ...
33
+
34
+ const initiator = new EDHOC(10, [ EdhocMethod.Method0 ], [ EdhocSuite.Suite0 ], credentialsManager, cryptoManager);
35
+ const responder = new EDHOC(20, [ EdhocMethod.Method0 ], [ EdhocSuite.Suite0 ], credentialsManager, cryptoManager);
36
+
37
+ // Message 1: Initiator → Responder
38
+ const message1 = await initiator.composeMessage1();
39
+ await responder.processMessage1(message1);
40
+
41
+ // Message 2: Responder → Initiator
42
+ const message2 = await responder.composeMessage2();
43
+ await initiator.processMessage2(message2);
44
+
45
+ // Message 3: Initiator → Responder
46
+ const message3 = await initiator.composeMessage3();
47
+ await responder.processMessage3(message3);
48
+
49
+ // ...
50
+
51
+ ```
52
+
53
+ ### Using External Authorization Data (EAD)
54
+
55
+ You can include additional authorization data in EDHOC messages:
56
+
57
+ ```typescript
58
+ // initiator.js
59
+ const ead_1 = [{
60
+ label: 1000,
61
+ value: Buffer.from('External Data')
62
+ }];
63
+
64
+ const message1 = await initiator.composeMessage1(ead_1);
65
+
66
+ // responder.js
67
+ const receivedEAD = await responder.processMessage1(message1);
68
+ ```
69
+
70
+ ### Certificate-Based Authentication
71
+
72
+ Using X.509 certificates for authentication (Method 1):
73
+
74
+ ```typescript
75
+ import {
76
+ EDHOC,
77
+ X509CertificateCredentialManager,
78
+ DefaultEdhocCryptoManager
79
+ } from 'node-edhoc';
80
+
81
+ // Setup credential managers
82
+ const initiatorCreds = new X509CertificateCredentialManager(
83
+ [initiatorCert],
84
+ initiatorKeyID
85
+ );
86
+ initiatorCreds.addTrustedCA(trustedCA);
87
+
88
+ // Setup crypto managers
89
+ const initiatorCrypto = new DefaultEdhocCryptoManager();
90
+
91
+ // Initialize EDHOC with certificate-based auth
92
+ const initiator = new EDHOC(
93
+ 10,
94
+ [EdhocMethod.Method1],
95
+ [EdhocSuite.Suite2],
96
+ initiatorCreds,
97
+ initiatorCrypto
98
+ );
99
+ ```
100
+
101
+ ### Exporting OSCORE Context
102
+
103
+ After a successful handshake, you can export the OSCORE security context:
104
+
105
+ ```typescript
106
+ const initiatorContext = await initiator.exportOSCORE();
107
+ const responderContext = await responder.exportOSCORE();
108
+
109
+ console.log('Master Secret:', initiatorContext.masterSecret);
110
+ console.log('Master Salt:', initiatorContext.masterSalt);
111
+ console.log('Sender ID:', initiatorContext.senderId);
112
+ console.log('Recipient ID:', initiatorContext.recipientId);
113
+ ```
114
+
115
+ ### Key Update
116
+
117
+ Perform a key update for an existing OSCORE context:
118
+
119
+ ```typescript
120
+ const keyUpdateContext = Buffer.from('new-entropy-data');
121
+
122
+ // Update keys for both parties
123
+ await initiator.keyUpdate(keyUpdateContext);
124
+ await responder.keyUpdate(keyUpdateContext);
125
+
126
+ // Export new OSCORE context
127
+ const newContext = await initiator.exportOSCORE();
128
+ ```
129
+
130
+ ### Custom Key Export
131
+
132
+ Export application-specific keys:
133
+
134
+ ```typescript
135
+ // Export a 32-byte key with label 40001
136
+ const key = await initiator.exportKey(40001, 32);
137
+ ```
138
+
139
+ For more detailed examples and API documentation, please refer to our [API Documentation](#).
140
+
141
+ ## Documentation
142
+
143
+ For detailed documentation, refer to:
144
+
145
+ - [EDHOC Specification (RFC 9528)](https://datatracker.ietf.org/doc/rfc9528/)
146
+ - [API Documentation](#) *(TODO: Link to generated API docs if available)*
147
+
148
+ ## Contributing
149
+
150
+ Contributions are welcome! To contribute:
151
+
152
+ 1. Fork the repository and create a new branch.
153
+ 2. Implement your feature or bugfix.
154
+ 3. Write tests if applicable.
155
+ 4. Open a pull request.
156
+
157
+ Please ensure your code follows the existing style and structure of the project.
158
+
159
+ ## License
160
+
161
+ This project is licensed under the [MIT License](LICENSE).
162
+
163
+ ## Related Projects
164
+
165
+ - [libedhoc](https://github.com/kamil-kielbasa/libedhoc/) - A C implementation of the Ephemeral Diffie-Hellman Over COSE
166
+
167
+ ## Acknowledgments
168
+
169
+ This implementation is based on the EDHOC specification as defined in RFC 9528. Special thanks to the developers of [`libedhoc`](https://github.com/kamil-kielbasa/libedhoc/) for their foundational work on EDHOC in C.
170
+
package/binding.gyp CHANGED
@@ -7,15 +7,16 @@
7
7
  "target_name": "bindings",
8
8
  'defines': [
9
9
  'NAPI_CPP_EXCEPTIONS=1',
10
- 'EDHOC_KID_LEN=4',
11
- 'EDHOC_MAX_CSUITES_LEN=9',
12
- 'EDHOC_MAX_CID_LEN=7',
13
- 'EDHOC_MAX_ECC_KEY_LEN=56',
14
- 'EDHOC_MAX_MAC_LEN=64',
15
- 'EDHOC_MAX_NR_OF_EAD_TOKENS=10',
16
- 'EDHOC_MAX_NR_OF_CERTS_IN_X509_CHAIN=5',
17
- 'EDHOC_CRED_KEY_ID_LEN=8',
18
- 'EDHOC_CRED_X509_HASH_ALG_LEN=1',
10
+ 'CONFIG_LIBEDHOC_ENABLE=1',
11
+ 'CONFIG_LIBEDHOC_MAX_NR_OF_CIPHER_SUITES=9',
12
+ 'CONFIG_LIBEDHOC_MAX_LEN_OF_CONN_ID=7',
13
+ 'CONFIG_LIBEDHOC_MAX_LEN_OF_ECC_KEY=56',
14
+ 'CONFIG_LIBEDHOC_MAX_LEN_OF_MAC=64',
15
+ 'CONFIG_LIBEDHOC_MAX_NR_OF_EAD_TOKENS=10',
16
+ 'CONFIG_LIBEDHOC_MAX_LEN_OF_CRED_KEY_ID=1',
17
+ 'CONFIG_LIBEDHOC_MAX_NR_OF_CERTS_IN_X509_CHAIN=5',
18
+ 'CONFIG_LIBEDHOC_MAX_LEN_OF_HASH_ALG=1',
19
+ 'CONFIG_LIBEDHOC_KEY_ID_LEN=4',
19
20
  'ZCBOR_CANONICAL=1'
20
21
  ],
21
22
  "sources": [
@@ -45,11 +46,12 @@
45
46
  'conditions': [
46
47
  ['OS=="win"', {
47
48
  'defines': [
48
- '_Static_assert=static_assert'
49
+ '_Static_assert=static_assert',
50
+ '__attribute__(x)='
49
51
  ],
50
52
  'msvs_settings': {
51
53
  'VCCLCompilerTool': {
52
- 'AdditionalOptions': [ '-std:c++20', ],
54
+ 'AdditionalOptions': [ '-std:c++20', "/D__attribute__(x)="],
53
55
  'ExceptionHandling': 1
54
56
  }
55
57
  }
@@ -0,0 +1,5 @@
1
+ import { EDHOC } from './edhoc';
2
+ export interface NodeGypBinding {
3
+ EDHOC: EDHOC;
4
+ }
5
+ //# sourceMappingURL=bindings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bindings.d.ts","sourceRoot":"","sources":["../lib/bindings.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAG/B,MAAM,WAAW,cAAc;IAC3B,KAAK,EAAE,KAAK,CAAA;CACf"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const node_gyp_build_1 = __importDefault(require("node-gyp-build"));
7
+ const path_1 = require("path");
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ const binding = (0, node_gyp_build_1.default)((0, path_1.join)(__dirname, '../'));
10
+ exports.EDHOC = binding.EDHOC;
@@ -0,0 +1,29 @@
1
+ import { EDHOC, EdhocCryptoManager, EdhocKeyType } from './edhoc';
2
+ export declare class DefaultEdhocCryptoManager implements EdhocCryptoManager {
3
+ private keys;
4
+ private keyIdentifier;
5
+ constructor();
6
+ addKey(keyID: Buffer, key: Buffer): void;
7
+ importKey(edhoc: EDHOC, keyType: EdhocKeyType, key: Buffer): Promise<Buffer>;
8
+ destroyKey(edhoc: EDHOC, keyID: Buffer): boolean;
9
+ makeKeyPair(edhoc: EDHOC, keyID: Buffer, privateKeySize: number, publicKeySize: number): {
10
+ privateKey: Buffer;
11
+ publicKey: Buffer;
12
+ };
13
+ keyAgreement(edhoc: EDHOC, keyID: Buffer, publicKey: Buffer, privateKeySize: number): Buffer;
14
+ sign(edhoc: EDHOC, keyID: Buffer, input: Buffer, signatureSize: number): Buffer;
15
+ verify(edhoc: EDHOC, keyID: Buffer, input: Buffer, signature: Buffer): Promise<boolean>;
16
+ extract(edhoc: EDHOC, keyID: Buffer, salt: Buffer, keySize: number): Buffer;
17
+ expand(edhoc: EDHOC, keyID: Buffer, info: Buffer, keySize: number): Buffer;
18
+ encrypt(edhoc: EDHOC, keyID: Buffer, nonce: Buffer, aad: Buffer, plaintext: Buffer, size: number): Buffer;
19
+ decrypt(edhoc: EDHOC, keyID: Buffer, nonce: Buffer, aad: Buffer, ciphertext: Buffer, size: number): Buffer;
20
+ hash(edhoc: EDHOC, data: Buffer, hashSize: number): Promise<Buffer>;
21
+ private getKey;
22
+ private formatToBeSigned;
23
+ private formatPublicKey;
24
+ private getCurveForSignature;
25
+ private getCurveForKeyAgreement;
26
+ private getTagLength;
27
+ private getAlgorithm;
28
+ }
29
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../lib/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAc,MAAM,SAAS,CAAC;AAoB9E,qBAAa,yBAA0B,YAAW,kBAAkB;IAEhE,OAAO,CAAC,IAAI,CAAiB;IAC7B,OAAO,CAAC,aAAa,CAAgB;;IAM9B,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAKlC,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM;IAsBhE,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM;IAStC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM;;;;IActF,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM;IAQnF,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM;IAiBhE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAa7F,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAKlE,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAMjE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAmBhG,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAiB3F,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAIvD,OAAO,CAAC,MAAM;IAQd,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,eAAe;IAavB,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,uBAAuB;IAY/B,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,YAAY;CAiBvB"}
package/dist/crypto.js ADDED
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultEdhocCryptoManager = void 0;
4
+ const edhoc_1 = require("./edhoc");
5
+ const ed25519_1 = require("@noble/curves/ed25519");
6
+ const p256_1 = require("@noble/curves/p256");
7
+ const sha256_1 = require("@noble/hashes/sha256");
8
+ const hkdf_1 = require("@noble/hashes/hkdf");
9
+ const crypto_1 = require("crypto");
10
+ class DefaultEdhocCryptoManager {
11
+ keys = {};
12
+ keyIdentifier = 1000;
13
+ constructor() {
14
+ this.keys = {};
15
+ }
16
+ addKey(keyID, key) {
17
+ const kid = keyID.toString('hex');
18
+ this.keys[kid] = key;
19
+ }
20
+ async importKey(edhoc, keyType, key) {
21
+ const keyBuffer = Buffer.alloc(4);
22
+ keyBuffer.writeInt32LE(this.keyIdentifier++);
23
+ const keyID = keyBuffer.toString('hex');
24
+ const curveKE = this.getCurveForKeyAgreement(edhoc.selectedSuite);
25
+ const curveSIG = this.getCurveForSignature(edhoc.selectedSuite);
26
+ switch (keyType) {
27
+ case edhoc_1.EdhocKeyType.KeyAgreement:
28
+ case edhoc_1.EdhocKeyType.MakeKeyPair:
29
+ this.keys[keyID] = key.byteLength > 0 ? Buffer.from(key) : curveKE.utils.randomPrivateKey();
30
+ break;
31
+ case edhoc_1.EdhocKeyType.Signature:
32
+ this.keys[keyID] = key.byteLength > 0 ? Buffer.from(key) : curveSIG.utils.randomPrivateKey();
33
+ break;
34
+ default:
35
+ this.keys[keyID] = Buffer.from(key);
36
+ }
37
+ return keyBuffer;
38
+ }
39
+ destroyKey(edhoc, keyID) {
40
+ const kid = keyID.toString('hex');
41
+ if (kid in this.keys === false) {
42
+ throw new Error(`Key '${kid}' not found`);
43
+ }
44
+ delete this.keys[kid];
45
+ return true;
46
+ }
47
+ makeKeyPair(edhoc, keyID, privateKeySize, publicKeySize) {
48
+ const key = this.getKey(keyID);
49
+ try {
50
+ const curveKE = this.getCurveForKeyAgreement(edhoc.selectedSuite);
51
+ return {
52
+ privateKey: Buffer.from(key),
53
+ publicKey: Buffer.from(curveKE.getPublicKey(key)).subarray(curveKE === p256_1.p256 ? 1 : 0)
54
+ };
55
+ }
56
+ catch (error) {
57
+ throw new Error(`Wrong key type`);
58
+ }
59
+ }
60
+ keyAgreement(edhoc, keyID, publicKey, privateKeySize) {
61
+ const key = this.getKey(keyID);
62
+ const curveKE = this.getCurveForKeyAgreement(edhoc.selectedSuite);
63
+ const publicKeyBuffer = this.formatPublicKey(curveKE, publicKey);
64
+ const sharedSecrect = Buffer.from(curveKE.getSharedSecret(key, new Uint8Array(publicKeyBuffer)));
65
+ return sharedSecrect.subarray(curveKE === p256_1.p256 ? 1 : 0);
66
+ }
67
+ sign(edhoc, keyID, input, signatureSize) {
68
+ const key = this.getKey(keyID);
69
+ const curveSIG = this.getCurveForSignature(edhoc.selectedSuite);
70
+ const payload = this.formatToBeSigned(curveSIG, input);
71
+ const signature = curveSIG.sign(payload, new Uint8Array(key));
72
+ if (signature instanceof Uint8Array) {
73
+ return Buffer.from(signature);
74
+ }
75
+ else if ('toCompactRawBytes' in signature) {
76
+ return Buffer.from(signature.toCompactRawBytes());
77
+ }
78
+ else {
79
+ throw new Error('Unsupported signature type');
80
+ }
81
+ }
82
+ async verify(edhoc, keyID, input, signature) {
83
+ const key = this.getKey(keyID);
84
+ const curveSIG = this.getCurveForSignature(edhoc.selectedSuite);
85
+ const publicKeyBuffer = this.formatPublicKey(curveSIG, key);
86
+ const payload = this.formatToBeSigned(curveSIG, input);
87
+ if (!curveSIG.verify(new Uint8Array(signature), payload, new Uint8Array(publicKeyBuffer))) {
88
+ throw new Error('Signature not verified');
89
+ }
90
+ return true;
91
+ }
92
+ extract(edhoc, keyID, salt, keySize) {
93
+ const key = this.getKey(keyID);
94
+ return Buffer.from((0, hkdf_1.extract)(sha256_1.sha256, new Uint8Array(key), new Uint8Array(salt)));
95
+ }
96
+ expand(edhoc, keyID, info, keySize) {
97
+ const key = this.getKey(keyID);
98
+ const expanded = Buffer.from((0, hkdf_1.expand)(sha256_1.sha256, new Uint8Array(key), new Uint8Array(info), keySize));
99
+ return expanded;
100
+ }
101
+ encrypt(edhoc, keyID, nonce, aad, plaintext, size) {
102
+ const key = this.getKey(keyID);
103
+ const algorithm = this.getAlgorithm(edhoc.selectedSuite);
104
+ const options = {
105
+ authTagLength: this.getTagLength(edhoc.selectedSuite)
106
+ };
107
+ const cipher = (0, crypto_1.createCipheriv)(algorithm, key, nonce, options);
108
+ cipher.setAAD(aad, { plaintextLength: Buffer.byteLength(plaintext) });
109
+ const update = Buffer.byteLength(plaintext) === 0 ? Buffer.alloc(0) : plaintext;
110
+ const encrypted = Buffer.concat([
111
+ cipher.update(update),
112
+ cipher.final(),
113
+ cipher.getAuthTag()
114
+ ]);
115
+ return encrypted;
116
+ }
117
+ decrypt(edhoc, keyID, nonce, aad, ciphertext, size) {
118
+ const key = this.getKey(keyID);
119
+ const tagLength = this.getTagLength(edhoc.selectedSuite);
120
+ const algorithm = this.getAlgorithm(edhoc.selectedSuite);
121
+ const options = { authTagLength: tagLength };
122
+ const decipher = (0, crypto_1.createDecipheriv)(algorithm, key, nonce, options);
123
+ decipher.setAuthTag(ciphertext.subarray(ciphertext.length - tagLength));
124
+ decipher.setAAD(aad, { plaintextLength: ciphertext.length - tagLength });
125
+ const decrypted = decipher.update(ciphertext.subarray(0, ciphertext.length - tagLength));
126
+ decipher.final();
127
+ return decrypted;
128
+ }
129
+ async hash(edhoc, data, hashSize) {
130
+ return Buffer.from((0, sha256_1.sha256)(data));
131
+ }
132
+ getKey(keyID) {
133
+ const kid = keyID.toString('hex');
134
+ if (kid in this.keys === false) {
135
+ throw new Error(`Key '${kid}' not found`);
136
+ }
137
+ return this.keys[kid];
138
+ }
139
+ formatToBeSigned(curve, payload) {
140
+ if (curve === p256_1.p256) {
141
+ return Buffer.from((0, sha256_1.sha256)(payload));
142
+ }
143
+ else if (curve === ed25519_1.ed25519) {
144
+ return payload;
145
+ }
146
+ else {
147
+ throw new Error(`Unsupported curve ${curve}`);
148
+ }
149
+ }
150
+ formatPublicKey(curve, key) {
151
+ if (curve === p256_1.p256) {
152
+ const prefix = key.byteLength === 64 ? 0x04 : (key[key.length - 1] & 1) ? 0x03 : 0x02;
153
+ return Buffer.concat([Buffer.from([prefix]), key]);
154
+ }
155
+ else if (curve === ed25519_1.ed25519 || curve === ed25519_1.x25519) {
156
+ return key;
157
+ }
158
+ else {
159
+ throw new Error(`Unsupported curve ${curve}`);
160
+ }
161
+ }
162
+ getCurveForSignature(suite) {
163
+ if ([edhoc_1.EdhocSuite.Suite2, edhoc_1.EdhocSuite.Suite3, edhoc_1.EdhocSuite.Suite5, edhoc_1.EdhocSuite.Suite6].includes(suite)) {
164
+ return p256_1.p256;
165
+ }
166
+ else if ([edhoc_1.EdhocSuite.Suite0, edhoc_1.EdhocSuite.Suite1, edhoc_1.EdhocSuite.Suite4].includes(suite)) {
167
+ return ed25519_1.ed25519;
168
+ }
169
+ else {
170
+ throw new Error(`Unsupported EDHOC suite ${suite} for signature.`);
171
+ }
172
+ }
173
+ getCurveForKeyAgreement(suite) {
174
+ if ([edhoc_1.EdhocSuite.Suite2, edhoc_1.EdhocSuite.Suite3, edhoc_1.EdhocSuite.Suite5].includes(suite)) {
175
+ return p256_1.p256;
176
+ }
177
+ else if ([edhoc_1.EdhocSuite.Suite0, edhoc_1.EdhocSuite.Suite1, edhoc_1.EdhocSuite.Suite4, edhoc_1.EdhocSuite.Suite6].includes(suite)) {
178
+ return ed25519_1.x25519;
179
+ }
180
+ else {
181
+ throw new Error(`Unsupported EDHOC suite ${suite} for key agreement.`);
182
+ }
183
+ }
184
+ getTagLength(suite) {
185
+ return [edhoc_1.EdhocSuite.Suite0, edhoc_1.EdhocSuite.Suite2].includes(suite) ? 8 : 16;
186
+ }
187
+ getAlgorithm(suite) {
188
+ if ([edhoc_1.EdhocSuite.Suite4, edhoc_1.EdhocSuite.Suite5, edhoc_1.EdhocSuite.Suite25].includes(suite)) {
189
+ return 'chacha20-poly1305';
190
+ }
191
+ else if ([edhoc_1.EdhocSuite.Suite6].includes(suite)) {
192
+ return 'aes-128-gcm';
193
+ }
194
+ else if ([edhoc_1.EdhocSuite.Suite24].includes(suite)) {
195
+ return 'aes-256-gcm';
196
+ }
197
+ else if ([edhoc_1.EdhocSuite.Suite0, edhoc_1.EdhocSuite.Suite1, edhoc_1.EdhocSuite.Suite2, edhoc_1.EdhocSuite.Suite3].includes(suite)) {
198
+ return 'aes-128-ccm';
199
+ }
200
+ else {
201
+ throw new Error(`Unsupported EDHOC suite ${suite} for encryption.`);
202
+ }
203
+ }
204
+ }
205
+ exports.DefaultEdhocCryptoManager = DefaultEdhocCryptoManager;