lws-gencrypto-common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. /*
  26. * These came from RFC7518 (JSON Web Algorithms) Section 3
  27. *
  28. * Cryptographic Algorithms for Digital Signatures and MACs
  29. */
  30. static const struct lws_jose_jwe_alg lws_gencrypto_jws_alg_map[] = {
  31. /*
  32. * JWSs MAY also be created that do not provide integrity protection.
  33. * Such a JWS is called an Unsecured JWS. An Unsecured JWS uses the
  34. * "alg" value "none" and is formatted identically to other JWSs, but
  35. * MUST use the empty octet sequence as its JWS Signature value.
  36. * Recipients MUST verify that the JWS Signature value is the empty
  37. * octet sequence.
  38. *
  39. * Implementations that support Unsecured JWSs MUST NOT accept such
  40. * objects as valid unless the application specifies that it is
  41. * acceptable for a specific object to not be integrity protected.
  42. * Implementations MUST NOT accept Unsecured JWSs by default. In order
  43. * to mitigate downgrade attacks, applications MUST NOT signal
  44. * acceptance of Unsecured JWSs at a global level, and SHOULD signal
  45. * acceptance on a per-object basis. See Section 8.5 for security
  46. * considerations associated with using this algorithm.
  47. */
  48. { /* optional */
  49. LWS_GENHASH_TYPE_UNKNOWN,
  50. LWS_GENHMAC_TYPE_UNKNOWN,
  51. LWS_JOSE_ENCTYPE_NONE,
  52. LWS_JOSE_ENCTYPE_NONE,
  53. "none", NULL, 0, 0, 0
  54. },
  55. /*
  56. * HMAC with SHA-2 Functions
  57. *
  58. * The HMAC SHA-256 MAC for a JWS is validated by computing an HMAC
  59. * value per RFC 2104, using SHA-256 as the hash algorithm "H", using
  60. * the received JWS Signing Input as the "text" value, and using the
  61. * shared key. This computed HMAC value is then compared to the result
  62. * of base64url decoding the received encoded JWS Signature value. The
  63. * comparison of the computed HMAC value to the JWS Signature value MUST
  64. * be done in a constant-time manner to thwart timing attacks.
  65. *
  66. * Alternatively, the computed HMAC value can be base64url encoded and
  67. * compared to the received encoded JWS Signature value (also in a
  68. * constant-time manner), as this comparison produces the same result as
  69. * comparing the unencoded values. In either case, if the values match,
  70. * the HMAC has been validated.
  71. */
  72. { /* required: HMAC using SHA-256 */
  73. LWS_GENHASH_TYPE_UNKNOWN,
  74. LWS_GENHMAC_TYPE_SHA256,
  75. LWS_JOSE_ENCTYPE_NONE,
  76. LWS_JOSE_ENCTYPE_NONE,
  77. "HS256", NULL, 0, 0, 0
  78. },
  79. { /* optional: HMAC using SHA-384 */
  80. LWS_GENHASH_TYPE_UNKNOWN,
  81. LWS_GENHMAC_TYPE_SHA384,
  82. LWS_JOSE_ENCTYPE_NONE,
  83. LWS_JOSE_ENCTYPE_NONE,
  84. "HS384", NULL, 0, 0, 0
  85. },
  86. { /* optional: HMAC using SHA-512 */
  87. LWS_GENHASH_TYPE_UNKNOWN,
  88. LWS_GENHMAC_TYPE_SHA512,
  89. LWS_JOSE_ENCTYPE_NONE,
  90. LWS_JOSE_ENCTYPE_NONE,
  91. "HS512", NULL, 0, 0, 0
  92. },
  93. /*
  94. * Digital Signature with RSASSA-PKCS1-v1_5
  95. *
  96. * This section defines the use of the RSASSA-PKCS1-v1_5 digital
  97. * signature algorithm as defined in Section 8.2 of RFC 3447 [RFC3447]
  98. * (commonly known as PKCS #1), using SHA-2 [SHS] hash functions.
  99. *
  100. * A key of size 2048 bits or larger MUST be used with these algorithms.
  101. *
  102. * The RSASSA-PKCS1-v1_5 SHA-256 digital signature is generated as
  103. * follows: generate a digital signature of the JWS Signing Input using
  104. * RSASSA-PKCS1-v1_5-SIGN and the SHA-256 hash function with the desired
  105. * private key. This is the JWS Signature value.
  106. *
  107. * The RSASSA-PKCS1-v1_5 SHA-256 digital signature for a JWS is
  108. * validated as follows: submit the JWS Signing Input, the JWS
  109. * Signature, and the public key corresponding to the private key used
  110. * by the signer to the RSASSA-PKCS1-v1_5-VERIFY algorithm using SHA-256
  111. * as the hash function.
  112. */
  113. { /* recommended: RSASSA-PKCS1-v1_5 using SHA-256 */
  114. LWS_GENHASH_TYPE_SHA256,
  115. LWS_GENHMAC_TYPE_UNKNOWN,
  116. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_1_5,
  117. LWS_JOSE_ENCTYPE_NONE,
  118. "RS256", NULL, 2048, 4096, 0
  119. },
  120. { /* optional: RSASSA-PKCS1-v1_5 using SHA-384 */
  121. LWS_GENHASH_TYPE_SHA384,
  122. LWS_GENHMAC_TYPE_UNKNOWN,
  123. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_1_5,
  124. LWS_JOSE_ENCTYPE_NONE,
  125. "RS384", NULL, 2048, 4096, 0
  126. },
  127. { /* optional: RSASSA-PKCS1-v1_5 using SHA-512 */
  128. LWS_GENHASH_TYPE_SHA512,
  129. LWS_GENHMAC_TYPE_UNKNOWN,
  130. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_1_5,
  131. LWS_JOSE_ENCTYPE_NONE,
  132. "RS512", NULL, 2048, 4096, 0
  133. },
  134. /*
  135. * Digital Signature with ECDSA
  136. *
  137. * The ECDSA P-256 SHA-256 digital signature is generated as follows:
  138. *
  139. * 1. Generate a digital signature of the JWS Signing Input using ECDSA
  140. * P-256 SHA-256 with the desired private key. The output will be
  141. * the pair (R, S), where R and S are 256-bit unsigned integers.
  142. * 2. Turn R and S into octet sequences in big-endian order, with each
  143. * array being be 32 octets long. The octet sequence
  144. * representations MUST NOT be shortened to omit any leading zero
  145. * octets contained in the values.
  146. *
  147. * 3. Concatenate the two octet sequences in the order R and then S.
  148. * (Note that many ECDSA implementations will directly produce this
  149. * concatenation as their output.)
  150. *
  151. * 4. The resulting 64-octet sequence is the JWS Signature value.
  152. *
  153. * The ECDSA P-256 SHA-256 digital signature for a JWS is validated as
  154. * follows:
  155. *
  156. * 1. The JWS Signature value MUST be a 64-octet sequence. If it is
  157. * not a 64-octet sequence, the validation has failed.
  158. *
  159. * 2. Split the 64-octet sequence into two 32-octet sequences. The
  160. * first octet sequence represents R and the second S. The values R
  161. * and S are represented as octet sequences using the Integer-to-
  162. * OctetString Conversion defined in Section 2.3.7 of SEC1 [SEC1]
  163. * (in big-endian octet order).
  164. * 3. Submit the JWS Signing Input, R, S, and the public key (x, y) to
  165. * the ECDSA P-256 SHA-256 validator.
  166. */
  167. { /* Recommended+: ECDSA using P-256 and SHA-256 */
  168. LWS_GENHASH_TYPE_SHA256,
  169. LWS_GENHMAC_TYPE_UNKNOWN,
  170. LWS_JOSE_ENCTYPE_ECDSA,
  171. LWS_JOSE_ENCTYPE_NONE,
  172. "ES256", "P-256", 256, 256, 0
  173. },
  174. { /* optional: ECDSA using P-384 and SHA-384 */
  175. LWS_GENHASH_TYPE_SHA384,
  176. LWS_GENHMAC_TYPE_UNKNOWN,
  177. LWS_JOSE_ENCTYPE_ECDSA,
  178. LWS_JOSE_ENCTYPE_NONE,
  179. "ES384", "P-384", 384, 384, 0
  180. },
  181. { /* optional: ECDSA using P-521 and SHA-512 */
  182. LWS_GENHASH_TYPE_SHA512,
  183. LWS_GENHMAC_TYPE_UNKNOWN,
  184. LWS_JOSE_ENCTYPE_ECDSA,
  185. LWS_JOSE_ENCTYPE_NONE,
  186. "ES512", "P-521", 521, 521, 0
  187. },
  188. #if 0
  189. Not yet supported
  190. /*
  191. * Digital Signature with RSASSA-PSS
  192. *
  193. * A key of size 2048 bits or larger MUST be used with this algorithm.
  194. *
  195. * The RSASSA-PSS SHA-256 digital signature is generated as follows:
  196. * generate a digital signature of the JWS Signing Input using RSASSA-
  197. * PSS-SIGN, the SHA-256 hash function, and the MGF1 mask generation
  198. * function with SHA-256 with the desired private key. This is the JWS
  199. * Signature value.
  200. *
  201. * The RSASSA-PSS SHA-256 digital signature for a JWS is validated as
  202. * follows: submit the JWS Signing Input, the JWS Signature, and the
  203. * public key corresponding to the private key used by the signer to the
  204. * RSASSA-PSS-VERIFY algorithm using SHA-256 as the hash function and
  205. * using MGF1 as the mask generation function with SHA-256.
  206. *
  207. */
  208. { /* optional: RSASSA-PSS using SHA-256 and MGF1 with SHA-256 */
  209. LWS_GENHASH_TYPE_SHA256,
  210. LWS_GENHMAC_TYPE_UNKNOWN,
  211. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_PSS,
  212. LWS_JOSE_ENCTYPE_NONE,
  213. "PS256", NULL, 2048, 4096, 0
  214. },
  215. { /* optional: RSASSA-PSS using SHA-384 and MGF1 with SHA-384 */
  216. LWS_GENHASH_TYPE_SHA384,
  217. LWS_GENHMAC_TYPE_UNKNOWN,
  218. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_PSS,
  219. LWS_JOSE_ENCTYPE_NONE,
  220. "PS384", NULL, 2048, 4096, 0
  221. },
  222. { /* optional: RSASSA-PSS using SHA-512 and MGF1 with SHA-512*/
  223. LWS_GENHASH_TYPE_SHA512,
  224. LWS_GENHMAC_TYPE_UNKNOWN,
  225. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_PSS,
  226. LWS_JOSE_ENCTYPE_NONE,
  227. "PS512", NULL, 2048, 4096, 0
  228. },
  229. #endif
  230. /* list terminator */
  231. { 0, 0, 0, 0, NULL, NULL, 0, 0, 0}
  232. };
  233. /*
  234. * These came from RFC7518 (JSON Web Algorithms) Section 4
  235. *
  236. * Cryptographic Algorithms for Key Management
  237. *
  238. * JWE uses cryptographic algorithms to encrypt or determine the Content
  239. * Encryption Key (CEK).
  240. */
  241. static const struct lws_jose_jwe_alg lws_gencrypto_jwe_alg_map[] = {
  242. /*
  243. * This section defines the specifics of encrypting a JWE CEK with
  244. * RSAES-PKCS1-v1_5 [RFC3447]. The "alg" (algorithm) Header Parameter
  245. * value "RSA1_5" is used for this algorithm.
  246. *
  247. * A key of size 2048 bits or larger MUST be used with this algorithm.
  248. */
  249. { /* recommended-: RSAES-PKCS1-v1_5 */
  250. LWS_GENHASH_TYPE_SHA256,
  251. LWS_GENHMAC_TYPE_UNKNOWN,
  252. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_1_5,
  253. LWS_JOSE_ENCTYPE_NONE,
  254. "RSA1_5", NULL, 2048, 4096, 0
  255. },
  256. { /* recommended+: RSAES OAEP using default parameters */
  257. LWS_GENHASH_TYPE_SHA1,
  258. LWS_GENHMAC_TYPE_UNKNOWN,
  259. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_OAEP,
  260. LWS_JOSE_ENCTYPE_NONE,
  261. "RSA-OAEP", NULL, 2048, 4096, 0
  262. },
  263. { /* recommended+: RSAES OAEP using SHA-256 and MGF1 SHA-256 */
  264. LWS_GENHASH_TYPE_SHA256,
  265. LWS_GENHMAC_TYPE_UNKNOWN,
  266. LWS_JOSE_ENCTYPE_RSASSA_PKCS1_OAEP,
  267. LWS_JOSE_ENCTYPE_NONE,
  268. "RSA-OAEP-256", NULL, 2048, 4096, 0
  269. },
  270. /*
  271. * Key Wrapping with AES Key Wrap
  272. *
  273. * This section defines the specifics of encrypting a JWE CEK with the
  274. * Advanced Encryption Standard (AES) Key Wrap Algorithm [RFC3394] using
  275. * the default initial value specified in Section 2.2.3.1 of that
  276. * document.
  277. *
  278. *
  279. */
  280. { /* recommended: AES Key Wrap with AES Key Wrap with defaults
  281. using 128-bit key */
  282. LWS_GENHASH_TYPE_UNKNOWN,
  283. LWS_GENHMAC_TYPE_UNKNOWN,
  284. LWS_JOSE_ENCTYPE_AES_ECB,
  285. LWS_JOSE_ENCTYPE_NONE,
  286. "A128KW", NULL, 128, 128, 64
  287. },
  288. { /* optional: AES Key Wrap with AES Key Wrap with defaults
  289. using 192-bit key */
  290. LWS_GENHASH_TYPE_UNKNOWN,
  291. LWS_GENHMAC_TYPE_UNKNOWN,
  292. LWS_JOSE_ENCTYPE_AES_ECB,
  293. LWS_JOSE_ENCTYPE_NONE,
  294. "A192KW", NULL, 192, 192, 64
  295. },
  296. { /* recommended: AES Key Wrap with AES Key Wrap with defaults
  297. using 256-bit key */
  298. LWS_GENHASH_TYPE_UNKNOWN,
  299. LWS_GENHMAC_TYPE_UNKNOWN,
  300. LWS_JOSE_ENCTYPE_AES_ECB,
  301. LWS_JOSE_ENCTYPE_NONE,
  302. "A256KW", NULL, 256, 256, 64
  303. },
  304. /*
  305. * This section defines the specifics of directly performing symmetric
  306. * key encryption without performing a key wrapping step. In this case,
  307. * the shared symmetric key is used directly as the Content Encryption
  308. * Key (CEK) value for the "enc" algorithm. An empty octet sequence is
  309. * used as the JWE Encrypted Key value. The "alg" (algorithm) Header
  310. * Parameter value "dir" is used in this case.
  311. */
  312. { /* recommended */
  313. LWS_GENHASH_TYPE_UNKNOWN,
  314. LWS_GENHMAC_TYPE_UNKNOWN,
  315. LWS_JOSE_ENCTYPE_NONE,
  316. LWS_JOSE_ENCTYPE_NONE,
  317. "dir", NULL, 0, 0, 0
  318. },
  319. /*
  320. * Key Agreement with Elliptic Curve Diffie-Hellman Ephemeral Static
  321. * (ECDH-ES)
  322. *
  323. * This section defines the specifics of key agreement with Elliptic
  324. * Curve Diffie-Hellman Ephemeral Static [RFC6090], in combination with
  325. * the Concat KDF, as defined in Section 5.8.1 of [NIST.800-56A]. The
  326. * key agreement result can be used in one of two ways:
  327. *
  328. * 1. directly as the Content Encryption Key (CEK) for the "enc"
  329. * algorithm, in the Direct Key Agreement mode, or
  330. *
  331. * 2. as a symmetric key used to wrap the CEK with the "A128KW",
  332. * "A192KW", or "A256KW" algorithms, in the Key Agreement with Key
  333. * Wrapping mode.
  334. *
  335. * A new ephemeral public key value MUST be generated for each key
  336. * agreement operation.
  337. *
  338. * In Direct Key Agreement mode, the output of the Concat KDF MUST be a
  339. * key of the same length as that used by the "enc" algorithm. In this
  340. * case, the empty octet sequence is used as the JWE Encrypted Key
  341. * value. The "alg" (algorithm) Header Parameter value "ECDH-ES" is
  342. * used in the Direct Key Agreement mode.
  343. *
  344. * In Key Agreement with Key Wrapping mode, the output of the Concat KDF
  345. * MUST be a key of the length needed for the specified key wrapping
  346. * algorithm. In this case, the JWE Encrypted Key is the CEK wrapped
  347. * with the agreed-upon key.
  348. */
  349. { /* recommended+: ECDH Ephemeral Static Key agreement Concat KDF */
  350. LWS_GENHASH_TYPE_SHA256,
  351. LWS_GENHMAC_TYPE_UNKNOWN,
  352. LWS_JOSE_ENCTYPE_ECDHES,
  353. LWS_JOSE_ENCTYPE_NONE,
  354. "ECDH-ES", NULL, 128, 128, 0
  355. },
  356. { /* recommended: ECDH-ES + Concat KDF + wrapped by AES128KW */
  357. LWS_GENHASH_TYPE_SHA256,
  358. LWS_GENHMAC_TYPE_UNKNOWN,
  359. LWS_JOSE_ENCTYPE_ECDHES,
  360. LWS_JOSE_ENCTYPE_AES_ECB,
  361. "ECDH-ES+A128KW", NULL, 128, 128, 0
  362. },
  363. { /* optional: ECDH-ES + Concat KDF + wrapped by AES192KW */
  364. LWS_GENHASH_TYPE_SHA256,
  365. LWS_GENHMAC_TYPE_UNKNOWN,
  366. LWS_JOSE_ENCTYPE_ECDHES,
  367. LWS_JOSE_ENCTYPE_AES_ECB,
  368. "ECDH-ES+A192KW", NULL, 192, 192, 0
  369. },
  370. { /* recommended: ECDH-ES + Concat KDF + wrapped by AES256KW */
  371. LWS_GENHASH_TYPE_SHA256,
  372. LWS_GENHMAC_TYPE_UNKNOWN,
  373. LWS_JOSE_ENCTYPE_ECDHES,
  374. LWS_JOSE_ENCTYPE_AES_ECB,
  375. "ECDH-ES+A256KW", NULL, 256, 256, 0
  376. },
  377. /*
  378. * Key Encryption with AES GCM
  379. *
  380. * This section defines the specifics of encrypting a JWE Content
  381. * Encryption Key (CEK) with Advanced Encryption Standard (AES) in
  382. * Galois/Counter Mode (GCM) ([AES] and [NIST.800-38D]).
  383. *
  384. * Use of an Initialization Vector (IV) of size 96 bits is REQUIRED with
  385. * this algorithm. The IV is represented in base64url-encoded form as
  386. * the "iv" (initialization vector) Header Parameter value.
  387. *
  388. * The Additional Authenticated Data value used is the empty octet
  389. * string.
  390. *
  391. * The requested size of the Authentication Tag output MUST be 128 bits,
  392. * regardless of the key size.
  393. *
  394. * The JWE Encrypted Key value is the ciphertext output.
  395. *
  396. * The Authentication Tag output is represented in base64url-encoded
  397. * form as the "tag" (authentication tag) Header Parameter value.
  398. *
  399. *
  400. * "iv" (Initialization Vector) Header Parameter
  401. *
  402. * The "iv" (initialization vector) Header Parameter value is the
  403. * base64url-encoded representation of the 96-bit IV value used for the
  404. * key encryption operation. This Header Parameter MUST be present and
  405. * MUST be understood and processed by implementations when these
  406. * algorithms are used.
  407. *
  408. * "tag" (Authentication Tag) Header Parameter
  409. *
  410. * The "tag" (authentication tag) Header Parameter value is the
  411. * base64url-encoded representation of the 128-bit Authentication Tag
  412. * value resulting from the key encryption operation. This Header
  413. * Parameter MUST be present and MUST be understood and processed by
  414. * implementations when these algorithms are used.
  415. */
  416. { /* optional: Key wrapping with AES GCM using 128-bit key */
  417. LWS_GENHASH_TYPE_UNKNOWN,
  418. LWS_GENHMAC_TYPE_UNKNOWN,
  419. LWS_JOSE_ENCTYPE_AES_ECB,
  420. LWS_JOSE_ENCTYPE_NONE,
  421. "A128GCMKW", NULL, 128, 128, 96
  422. },
  423. { /* optional: Key wrapping with AES GCM using 192-bit key */
  424. LWS_GENHASH_TYPE_UNKNOWN,
  425. LWS_GENHMAC_TYPE_UNKNOWN,
  426. LWS_JOSE_ENCTYPE_AES_ECB,
  427. LWS_JOSE_ENCTYPE_NONE,
  428. "A192GCMKW", NULL, 192, 192, 96
  429. },
  430. { /* optional: Key wrapping with AES GCM using 256-bit key */
  431. LWS_GENHASH_TYPE_UNKNOWN,
  432. LWS_GENHMAC_TYPE_UNKNOWN,
  433. LWS_JOSE_ENCTYPE_AES_ECB,
  434. LWS_JOSE_ENCTYPE_NONE,
  435. "A256GCMKW", NULL, 256, 256, 96
  436. },
  437. /* list terminator */
  438. { 0, 0, 0, 0, NULL, NULL }
  439. };
  440. /*
  441. * The "enc" (encryption algorithm) Header Parameter identifies the
  442. * content encryption algorithm used to perform authenticated encryption
  443. * on the plaintext to produce the ciphertext and the Authentication
  444. * Tag. This algorithm MUST be an AEAD algorithm with a specified key
  445. * length. The encrypted content is not usable if the "enc" value does
  446. * not represent a supported algorithm. "enc" values should either be
  447. * registered in the IANA "JSON Web Signature and Encryption Algorithms"
  448. * registry established by [JWA] or be a value that contains a
  449. * Collision-Resistant Name. The "enc" value is a case-sensitive ASCII
  450. * string containing a StringOrURI value. This Header Parameter MUST be
  451. * present and MUST be understood and processed by implementations.
  452. */
  453. static const struct lws_jose_jwe_alg lws_gencrypto_jwe_enc_map[] = {
  454. /*
  455. * AES_128_CBC_HMAC_SHA_256 / 512
  456. *
  457. * It uses the HMAC message authentication code [RFC2104] with the
  458. * SHA-256 hash function [SHS] to provide message authentication, with
  459. * the HMAC output truncated to 128 bits, corresponding to the
  460. * HMAC-SHA-256-128 algorithm defined in [RFC4868]. For encryption, it
  461. * uses AES in the CBC mode of operation as defined in Section 6.2 of
  462. * [NIST.800-38A], with PKCS #7 padding and a 128-bit IV value.
  463. *
  464. * The AES_CBC_HMAC_SHA2 parameters specific to AES_128_CBC_HMAC_SHA_256
  465. * are:
  466. *
  467. * The input key K is 32 octets long.
  468. * ENC_KEY_LEN is 16 octets.
  469. * MAC_KEY_LEN is 16 octets.
  470. * The SHA-256 hash algorithm is used for the HMAC.
  471. * The HMAC-SHA-256 output is truncated to T_LEN=16 octets, by
  472. * stripping off the final 16 octets.
  473. */
  474. { /* required */
  475. LWS_GENHASH_TYPE_UNKNOWN,
  476. LWS_GENHMAC_TYPE_SHA256,
  477. LWS_JOSE_ENCTYPE_NONE,
  478. LWS_JOSE_ENCTYPE_AES_CBC,
  479. "A128CBC-HS256", NULL, 256, 256, 128
  480. },
  481. /*
  482. * AES_192_CBC_HMAC_SHA_384 is based on AES_128_CBC_HMAC_SHA_256, but
  483. * with the following differences:
  484. *
  485. * The input key K is 48 octets long instead of 32.
  486. * ENC_KEY_LEN is 24 octets instead of 16.
  487. * MAC_KEY_LEN is 24 octets instead of 16.
  488. * SHA-384 is used for the HMAC instead of SHA-256.
  489. * The HMAC SHA-384 value is truncated to T_LEN=24 octets instead of 16.
  490. */
  491. { /* required */
  492. LWS_GENHASH_TYPE_UNKNOWN,
  493. LWS_GENHMAC_TYPE_SHA384,
  494. LWS_JOSE_ENCTYPE_NONE,
  495. LWS_JOSE_ENCTYPE_AES_CBC,
  496. "A192CBC-HS384", NULL, 384, 384, 192
  497. },
  498. /*
  499. * AES_256_CBC_HMAC_SHA_512 is based on AES_128_CBC_HMAC_SHA_256, but
  500. * with the following differences:
  501. *
  502. * The input key K is 64 octets long instead of 32.
  503. * ENC_KEY_LEN is 32 octets instead of 16.
  504. * MAC_KEY_LEN is 32 octets instead of 16.
  505. * SHA-512 is used for the HMAC instead of SHA-256.
  506. * The HMAC SHA-512 value is truncated to T_LEN=32 octets instead of 16.
  507. */
  508. { /* required */
  509. LWS_GENHASH_TYPE_UNKNOWN,
  510. LWS_GENHMAC_TYPE_SHA512,
  511. LWS_JOSE_ENCTYPE_NONE,
  512. LWS_JOSE_ENCTYPE_AES_CBC,
  513. "A256CBC-HS512", NULL, 512, 512, 256
  514. },
  515. /*
  516. * The CEK is used as the encryption key.
  517. *
  518. * Use of an IV of size 96 bits is REQUIRED with this algorithm.
  519. *
  520. * The requested size of the Authentication Tag output MUST be 128 bits,
  521. * regardless of the key size.
  522. */
  523. { /* recommended: AES GCM using 128-bit key */
  524. LWS_GENHASH_TYPE_UNKNOWN,
  525. LWS_GENHMAC_TYPE_UNKNOWN,
  526. LWS_JOSE_ENCTYPE_NONE,
  527. LWS_JOSE_ENCTYPE_AES_GCM,
  528. "A128GCM", NULL, 128, 128, 96
  529. },
  530. { /* optional: AES GCM using 192-bit key */
  531. LWS_GENHASH_TYPE_UNKNOWN,
  532. LWS_GENHMAC_TYPE_UNKNOWN,
  533. LWS_JOSE_ENCTYPE_NONE,
  534. LWS_JOSE_ENCTYPE_AES_GCM,
  535. "A192GCM", NULL, 192, 192, 96
  536. },
  537. { /* recommended: AES GCM using 256-bit key */
  538. LWS_GENHASH_TYPE_UNKNOWN,
  539. LWS_GENHMAC_TYPE_UNKNOWN,
  540. LWS_JOSE_ENCTYPE_NONE,
  541. LWS_JOSE_ENCTYPE_AES_GCM,
  542. "A256GCM", NULL, 256, 256, 96
  543. },
  544. { 0, 0, 0, 0, NULL, NULL, 0, 0, 0 } /* sentinel */
  545. };
  546. int
  547. lws_gencrypto_jws_alg_to_definition(const char *alg,
  548. const struct lws_jose_jwe_alg **jose)
  549. {
  550. const struct lws_jose_jwe_alg *a = lws_gencrypto_jws_alg_map;
  551. while (a->alg) {
  552. if (!strcmp(alg, a->alg)) {
  553. *jose = a;
  554. return 0;
  555. }
  556. a++;
  557. }
  558. return 1;
  559. }
  560. int
  561. lws_gencrypto_jwe_alg_to_definition(const char *alg,
  562. const struct lws_jose_jwe_alg **jose)
  563. {
  564. const struct lws_jose_jwe_alg *a = lws_gencrypto_jwe_alg_map;
  565. while (a->alg) {
  566. if (!strcmp(alg, a->alg)) {
  567. *jose = a;
  568. return 0;
  569. }
  570. a++;
  571. }
  572. return 1;
  573. }
  574. int
  575. lws_gencrypto_jwe_enc_to_definition(const char *enc,
  576. const struct lws_jose_jwe_alg **jose)
  577. {
  578. const struct lws_jose_jwe_alg *e = lws_gencrypto_jwe_enc_map;
  579. while (e->alg) {
  580. if (!strcmp(enc, e->alg)) {
  581. *jose = e;
  582. return 0;
  583. }
  584. e++;
  585. }
  586. return 1;
  587. }
  588. size_t
  589. lws_genhash_size(enum lws_genhash_types type)
  590. {
  591. switch(type) {
  592. case LWS_GENHASH_TYPE_UNKNOWN:
  593. return 0;
  594. case LWS_GENHASH_TYPE_MD5:
  595. return 16;
  596. case LWS_GENHASH_TYPE_SHA1:
  597. return 20;
  598. case LWS_GENHASH_TYPE_SHA256:
  599. return 32;
  600. case LWS_GENHASH_TYPE_SHA384:
  601. return 48;
  602. case LWS_GENHASH_TYPE_SHA512:
  603. return 64;
  604. }
  605. return 0;
  606. }
  607. size_t
  608. lws_genhmac_size(enum lws_genhmac_types type)
  609. {
  610. switch(type) {
  611. case LWS_GENHMAC_TYPE_UNKNOWN:
  612. return 0;
  613. case LWS_GENHMAC_TYPE_SHA256:
  614. return 32;
  615. case LWS_GENHMAC_TYPE_SHA384:
  616. return 48;
  617. case LWS_GENHMAC_TYPE_SHA512:
  618. return 64;
  619. }
  620. return 0;
  621. }
  622. int
  623. lws_gencrypto_bits_to_bytes(int bits)
  624. {
  625. if (bits & 7)
  626. return (bits / 8) + 1;
  627. return bits / 8;
  628. }
  629. int
  630. lws_base64_size(int bytes)
  631. {
  632. return ((bytes * 4) / 3) + 6;
  633. }
  634. void
  635. lws_gencrypto_destroy_elements(struct lws_gencrypto_keyelem *el, int m)
  636. {
  637. int n;
  638. for (n = 0; n < m; n++)
  639. if (el[n].buf)
  640. lws_free_set_NULL(el[n].buf);
  641. }
  642. size_t lws_gencrypto_padded_length(size_t pad_block_size, size_t len)
  643. {
  644. return (len / pad_block_size + 1) * pad_block_size;
  645. }