ciphercommon_ccm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Dispatch functions for ccm mode */
  10. #include <openssl/proverr.h>
  11. #include "prov/ciphercommon.h"
  12. #include "prov/ciphercommon_ccm.h"
  13. #include "prov/providercommon.h"
  14. static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
  15. size_t *padlen, const unsigned char *in,
  16. size_t len);
  17. static int ccm_tls_init(PROV_CCM_CTX *ctx, unsigned char *aad, size_t alen)
  18. {
  19. size_t len;
  20. if (!ossl_prov_is_running() || alen != EVP_AEAD_TLS1_AAD_LEN)
  21. return 0;
  22. /* Save the aad for later use. */
  23. memcpy(ctx->buf, aad, alen);
  24. ctx->tls_aad_len = alen;
  25. len = ctx->buf[alen - 2] << 8 | ctx->buf[alen - 1];
  26. if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
  27. return 0;
  28. /* Correct length for explicit iv. */
  29. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  30. if (!ctx->enc) {
  31. if (len < ctx->m)
  32. return 0;
  33. /* Correct length for tag. */
  34. len -= ctx->m;
  35. }
  36. ctx->buf[alen - 2] = (unsigned char)(len >> 8);
  37. ctx->buf[alen - 1] = (unsigned char)(len & 0xff);
  38. /* Extra padding: tag appended to record. */
  39. return ctx->m;
  40. }
  41. static int ccm_tls_iv_set_fixed(PROV_CCM_CTX *ctx, unsigned char *fixed,
  42. size_t flen)
  43. {
  44. if (flen != EVP_CCM_TLS_FIXED_IV_LEN)
  45. return 0;
  46. /* Copy to first part of the iv. */
  47. memcpy(ctx->iv, fixed, flen);
  48. return 1;
  49. }
  50. static size_t ccm_get_ivlen(PROV_CCM_CTX *ctx)
  51. {
  52. return 15 - ctx->l;
  53. }
  54. int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  55. {
  56. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  57. const OSSL_PARAM *p;
  58. size_t sz;
  59. if (params == NULL)
  60. return 1;
  61. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  62. if (p != NULL) {
  63. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  64. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  65. return 0;
  66. }
  67. if ((p->data_size & 1) || (p->data_size < 4) || p->data_size > 16) {
  68. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
  69. return 0;
  70. }
  71. if (p->data != NULL) {
  72. if (ctx->enc) {
  73. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
  74. return 0;
  75. }
  76. memcpy(ctx->buf, p->data, p->data_size);
  77. ctx->tag_set = 1;
  78. }
  79. ctx->m = p->data_size;
  80. }
  81. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
  82. if (p != NULL) {
  83. size_t ivlen;
  84. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  85. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  86. return 0;
  87. }
  88. ivlen = 15 - sz;
  89. if (ivlen < 2 || ivlen > 8) {
  90. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  91. return 0;
  92. }
  93. if (ctx->l != ivlen) {
  94. ctx->l = ivlen;
  95. ctx->iv_set = 0;
  96. }
  97. }
  98. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
  99. if (p != NULL) {
  100. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  101. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  102. return 0;
  103. }
  104. sz = ccm_tls_init(ctx, p->data, p->data_size);
  105. if (sz == 0) {
  106. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
  107. return 0;
  108. }
  109. ctx->tls_aad_pad_sz = sz;
  110. }
  111. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
  112. if (p != NULL) {
  113. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  114. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  115. return 0;
  116. }
  117. if (ccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
  118. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  119. return 0;
  120. }
  121. }
  122. return 1;
  123. }
  124. int ossl_ccm_get_ctx_params(void *vctx, OSSL_PARAM params[])
  125. {
  126. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  127. OSSL_PARAM *p;
  128. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  129. if (p != NULL && !OSSL_PARAM_set_size_t(p, ccm_get_ivlen(ctx))) {
  130. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  131. return 0;
  132. }
  133. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  134. if (p != NULL) {
  135. size_t m = ctx->m;
  136. if (!OSSL_PARAM_set_size_t(p, m)) {
  137. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  138. return 0;
  139. }
  140. }
  141. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
  142. if (p != NULL) {
  143. if (ccm_get_ivlen(ctx) > p->data_size) {
  144. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  145. return 0;
  146. }
  147. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
  148. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
  149. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  150. return 0;
  151. }
  152. }
  153. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
  154. if (p != NULL) {
  155. if (ccm_get_ivlen(ctx) > p->data_size) {
  156. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  157. return 0;
  158. }
  159. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, p->data_size)
  160. && !OSSL_PARAM_set_octet_ptr(p, &ctx->iv, p->data_size)) {
  161. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  162. return 0;
  163. }
  164. }
  165. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  166. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  167. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  168. return 0;
  169. }
  170. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
  171. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  172. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  173. return 0;
  174. }
  175. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  176. if (p != NULL) {
  177. if (!ctx->enc || !ctx->tag_set) {
  178. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
  179. return 0;
  180. }
  181. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  182. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  183. return 0;
  184. }
  185. if (!ctx->hw->gettag(ctx, p->data, p->data_size))
  186. return 0;
  187. ctx->tag_set = 0;
  188. ctx->iv_set = 0;
  189. ctx->len_set = 0;
  190. }
  191. return 1;
  192. }
  193. static int ccm_init(void *vctx, const unsigned char *key, size_t keylen,
  194. const unsigned char *iv, size_t ivlen,
  195. const OSSL_PARAM params[], int enc)
  196. {
  197. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  198. if (!ossl_prov_is_running())
  199. return 0;
  200. ctx->enc = enc;
  201. if (iv != NULL) {
  202. if (ivlen != ccm_get_ivlen(ctx)) {
  203. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  204. return 0;
  205. }
  206. memcpy(ctx->iv, iv, ivlen);
  207. ctx->iv_set = 1;
  208. }
  209. if (key != NULL) {
  210. if (keylen != ctx->keylen) {
  211. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  212. return 0;
  213. }
  214. if (!ctx->hw->setkey(ctx, key, keylen))
  215. return 0;
  216. }
  217. return ossl_ccm_set_ctx_params(ctx, params);
  218. }
  219. int ossl_ccm_einit(void *vctx, const unsigned char *key, size_t keylen,
  220. const unsigned char *iv, size_t ivlen,
  221. const OSSL_PARAM params[])
  222. {
  223. return ccm_init(vctx, key, keylen, iv, ivlen, params, 1);
  224. }
  225. int ossl_ccm_dinit(void *vctx, const unsigned char *key, size_t keylen,
  226. const unsigned char *iv, size_t ivlen,
  227. const OSSL_PARAM params[])
  228. {
  229. return ccm_init(vctx, key, keylen, iv, ivlen, params, 0);
  230. }
  231. int ossl_ccm_stream_update(void *vctx, unsigned char *out, size_t *outl,
  232. size_t outsize, const unsigned char *in,
  233. size_t inl)
  234. {
  235. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  236. if (outsize < inl) {
  237. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  238. return 0;
  239. }
  240. if (!ccm_cipher_internal(ctx, out, outl, in, inl)) {
  241. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  242. return 0;
  243. }
  244. return 1;
  245. }
  246. int ossl_ccm_stream_final(void *vctx, unsigned char *out, size_t *outl,
  247. size_t outsize)
  248. {
  249. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  250. int i;
  251. if (!ossl_prov_is_running())
  252. return 0;
  253. i = ccm_cipher_internal(ctx, out, outl, NULL, 0);
  254. if (i <= 0)
  255. return 0;
  256. *outl = 0;
  257. return 1;
  258. }
  259. int ossl_ccm_cipher(void *vctx, unsigned char *out, size_t *outl, size_t outsize,
  260. const unsigned char *in, size_t inl)
  261. {
  262. PROV_CCM_CTX *ctx = (PROV_CCM_CTX *)vctx;
  263. if (!ossl_prov_is_running())
  264. return 0;
  265. if (outsize < inl) {
  266. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  267. return 0;
  268. }
  269. if (ccm_cipher_internal(ctx, out, outl, in, inl) <= 0)
  270. return 0;
  271. *outl = inl;
  272. return 1;
  273. }
  274. /* Copy the buffered iv */
  275. static int ccm_set_iv(PROV_CCM_CTX *ctx, size_t mlen)
  276. {
  277. const PROV_CCM_HW *hw = ctx->hw;
  278. if (!hw->setiv(ctx, ctx->iv, ccm_get_ivlen(ctx), mlen))
  279. return 0;
  280. ctx->len_set = 1;
  281. return 1;
  282. }
  283. static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
  284. unsigned char *out, size_t *padlen,
  285. const unsigned char *in, size_t len)
  286. {
  287. int rv = 0;
  288. size_t olen = 0;
  289. if (!ossl_prov_is_running())
  290. goto err;
  291. /* Encrypt/decrypt must be performed in place */
  292. if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
  293. goto err;
  294. /* If encrypting set explicit IV from sequence number (start of AAD) */
  295. if (ctx->enc)
  296. memcpy(out, ctx->buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  297. /* Get rest of IV from explicit IV */
  298. memcpy(ctx->iv + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
  299. /* Correct length value */
  300. len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
  301. if (!ccm_set_iv(ctx, len))
  302. goto err;
  303. /* Use saved AAD */
  304. if (!ctx->hw->setaad(ctx, ctx->buf, ctx->tls_aad_len))
  305. goto err;
  306. /* Fix buffer to point to payload */
  307. in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  308. out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  309. if (ctx->enc) {
  310. if (!ctx->hw->auth_encrypt(ctx, in, out, len, out + len, ctx->m))
  311. goto err;
  312. olen = len + EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m;
  313. } else {
  314. if (!ctx->hw->auth_decrypt(ctx, in, out, len,
  315. (unsigned char *)in + len, ctx->m))
  316. goto err;
  317. olen = len;
  318. }
  319. rv = 1;
  320. err:
  321. *padlen = olen;
  322. return rv;
  323. }
  324. static int ccm_cipher_internal(PROV_CCM_CTX *ctx, unsigned char *out,
  325. size_t *padlen, const unsigned char *in,
  326. size_t len)
  327. {
  328. int rv = 0;
  329. size_t olen = 0;
  330. const PROV_CCM_HW *hw = ctx->hw;
  331. /* If no key set, return error */
  332. if (!ctx->key_set)
  333. return 0;
  334. if (ctx->tls_aad_len != UNINITIALISED_SIZET)
  335. return ccm_tls_cipher(ctx, out, padlen, in, len);
  336. /* EVP_*Final() doesn't return any data */
  337. if (in == NULL && out != NULL)
  338. goto finish;
  339. if (!ctx->iv_set)
  340. goto err;
  341. if (out == NULL) {
  342. if (in == NULL) {
  343. if (!ccm_set_iv(ctx, len))
  344. goto err;
  345. } else {
  346. /* If we have AAD, we need a message length */
  347. if (!ctx->len_set && len)
  348. goto err;
  349. if (!hw->setaad(ctx, in, len))
  350. goto err;
  351. }
  352. } else {
  353. /* If not set length yet do it */
  354. if (!ctx->len_set && !ccm_set_iv(ctx, len))
  355. goto err;
  356. if (ctx->enc) {
  357. if (!hw->auth_encrypt(ctx, in, out, len, NULL, 0))
  358. goto err;
  359. ctx->tag_set = 1;
  360. } else {
  361. /* The tag must be set before actually decrypting data */
  362. if (!ctx->tag_set)
  363. goto err;
  364. if (!hw->auth_decrypt(ctx, in, out, len, ctx->buf, ctx->m))
  365. goto err;
  366. /* Finished - reset flags so calling this method again will fail */
  367. ctx->iv_set = 0;
  368. ctx->tag_set = 0;
  369. ctx->len_set = 0;
  370. }
  371. }
  372. olen = len;
  373. finish:
  374. rv = 1;
  375. err:
  376. *padlen = olen;
  377. return rv;
  378. }
  379. void ossl_ccm_initctx(PROV_CCM_CTX *ctx, size_t keybits, const PROV_CCM_HW *hw)
  380. {
  381. ctx->keylen = keybits / 8;
  382. ctx->key_set = 0;
  383. ctx->iv_set = 0;
  384. ctx->tag_set = 0;
  385. ctx->len_set = 0;
  386. ctx->l = 8;
  387. ctx->m = 12;
  388. ctx->tls_aad_len = UNINITIALISED_SIZET;
  389. ctx->hw = hw;
  390. }