ciphercommon_gcm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Copyright 2019 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 gcm mode */
  10. #include "prov/ciphercommon.h"
  11. #include "prov/ciphercommon_gcm.h"
  12. #include "prov/providercommonerr.h"
  13. #include <openssl/rand.h>
  14. #include "prov/provider_ctx.h"
  15. static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len);
  16. static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
  17. size_t len);
  18. static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
  19. const unsigned char *in, size_t len);
  20. static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
  21. size_t *padlen, const unsigned char *in,
  22. size_t len);
  23. void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
  24. const PROV_GCM_HW *hw, size_t ivlen_min)
  25. {
  26. ctx->pad = 1;
  27. ctx->mode = EVP_CIPH_GCM_MODE;
  28. ctx->taglen = UNINITIALISED_SIZET;
  29. ctx->tls_aad_len = UNINITIALISED_SIZET;
  30. ctx->ivlen_min = ivlen_min;
  31. ctx->ivlen = (EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN);
  32. ctx->keylen = keybits / 8;
  33. ctx->hw = hw;
  34. ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  35. }
  36. static int gcm_init(void *vctx, const unsigned char *key, size_t keylen,
  37. const unsigned char *iv, size_t ivlen, int enc)
  38. {
  39. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  40. ctx->enc = enc;
  41. if (iv != NULL) {
  42. if (ivlen < ctx->ivlen_min || ivlen > sizeof(ctx->iv)) {
  43. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  44. return 0;
  45. }
  46. ctx->ivlen = ivlen;
  47. memcpy(ctx->iv, iv, ivlen);
  48. ctx->iv_state = IV_STATE_BUFFERED;
  49. }
  50. if (key != NULL) {
  51. if (keylen != ctx->keylen) {
  52. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  53. return 0;
  54. }
  55. return ctx->hw->setkey(ctx, key, ctx->keylen);
  56. }
  57. return 1;
  58. }
  59. int gcm_einit(void *vctx, const unsigned char *key, size_t keylen,
  60. const unsigned char *iv, size_t ivlen)
  61. {
  62. return gcm_init(vctx, key, keylen, iv, ivlen, 1);
  63. }
  64. int gcm_dinit(void *vctx, const unsigned char *key, size_t keylen,
  65. const unsigned char *iv, size_t ivlen)
  66. {
  67. return gcm_init(vctx, key, keylen, iv, ivlen, 0);
  68. }
  69. /* increment counter (64-bit int) by 1 */
  70. static void ctr64_inc(unsigned char *counter)
  71. {
  72. int n = 8;
  73. unsigned char c;
  74. do {
  75. --n;
  76. c = counter[n];
  77. ++c;
  78. counter[n] = c;
  79. if (c > 0)
  80. return;
  81. } while (n > 0);
  82. }
  83. static int getivgen(PROV_GCM_CTX *ctx, unsigned char *out, size_t olen)
  84. {
  85. if (!ctx->iv_gen
  86. || !ctx->key_set
  87. || !ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
  88. return 0;
  89. if (olen == 0 || olen > ctx->ivlen)
  90. olen = ctx->ivlen;
  91. memcpy(out, ctx->iv + ctx->ivlen - olen, olen);
  92. /*
  93. * Invocation field will be at least 8 bytes in size and so no need
  94. * to check wrap around or increment more than last 8 bytes.
  95. */
  96. ctr64_inc(ctx->iv + ctx->ivlen - 8);
  97. ctx->iv_state = IV_STATE_COPIED;
  98. return 1;
  99. }
  100. static int setivinv(PROV_GCM_CTX *ctx, unsigned char *in, size_t inl)
  101. {
  102. if (!ctx->iv_gen
  103. || !ctx->key_set
  104. || ctx->enc)
  105. return 0;
  106. memcpy(ctx->iv + ctx->ivlen - inl, in, inl);
  107. if (!ctx->hw->setiv(ctx, ctx->iv, ctx->ivlen))
  108. return 0;
  109. ctx->iv_state = IV_STATE_COPIED;
  110. return 1;
  111. }
  112. int gcm_get_ctx_params(void *vctx, OSSL_PARAM params[])
  113. {
  114. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  115. OSSL_PARAM *p;
  116. size_t sz;
  117. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  118. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivlen)) {
  119. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  120. return 0;
  121. }
  122. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  123. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  124. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  125. return 0;
  126. }
  127. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  128. if (p != NULL) {
  129. size_t taglen = (ctx->taglen != UNINITIALISED_SIZET) ? ctx->taglen :
  130. GCM_TAG_MAX_SIZE;
  131. if (!OSSL_PARAM_set_size_t(p, taglen)) {
  132. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  133. return 0;
  134. }
  135. }
  136. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
  137. if (p != NULL) {
  138. if (ctx->iv_gen != 1 && ctx->iv_gen_rand != 1)
  139. return 0;
  140. if (ctx->ivlen != p->data_size) {
  141. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  142. return 0;
  143. }
  144. if (!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivlen)) {
  145. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  146. return 0;
  147. }
  148. }
  149. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
  150. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  151. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  152. return 0;
  153. }
  154. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  155. if (p != NULL) {
  156. sz = p->data_size;
  157. if (sz == 0
  158. || sz > EVP_GCM_TLS_TAG_LEN
  159. || !ctx->enc
  160. || ctx->taglen == UNINITIALISED_SIZET) {
  161. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
  162. return 0;
  163. }
  164. if (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz)) {
  165. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  166. return 0;
  167. }
  168. }
  169. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN);
  170. if (p != NULL) {
  171. if (p->data == NULL
  172. || p->data_type != OSSL_PARAM_OCTET_STRING
  173. || !getivgen(ctx, p->data, p->data_size))
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. int gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  179. {
  180. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  181. const OSSL_PARAM *p;
  182. size_t sz;
  183. void *vp;
  184. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  185. if (p != NULL) {
  186. vp = ctx->buf;
  187. if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
  188. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  189. return 0;
  190. }
  191. if (sz == 0 || ctx->enc) {
  192. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG);
  193. return 0;
  194. }
  195. ctx->taglen = sz;
  196. }
  197. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
  198. if (p != NULL) {
  199. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  200. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  201. return 0;
  202. }
  203. if (sz == 0 || sz > sizeof(ctx->iv)) {
  204. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  205. return 0;
  206. }
  207. ctx->ivlen = sz;
  208. }
  209. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
  210. if (p != NULL) {
  211. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  212. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  213. return 0;
  214. }
  215. sz = gcm_tls_init(ctx, p->data, p->data_size);
  216. if (sz == 0) {
  217. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_AAD);
  218. return 0;
  219. }
  220. ctx->tls_aad_pad_sz = sz;
  221. }
  222. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
  223. if (p != NULL) {
  224. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  225. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  226. return 0;
  227. }
  228. if (gcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
  229. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  230. return 0;
  231. }
  232. }
  233. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV);
  234. if (p != NULL) {
  235. if (p->data == NULL
  236. || p->data_type != OSSL_PARAM_OCTET_STRING
  237. || !setivinv(ctx, p->data, p->data_size))
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. int gcm_stream_update(void *vctx, unsigned char *out, size_t *outl,
  243. size_t outsize, const unsigned char *in, size_t inl)
  244. {
  245. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  246. if (inl == 0) {
  247. *outl = 0;
  248. return 1;
  249. }
  250. if (outsize < inl) {
  251. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  252. return -1;
  253. }
  254. if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0) {
  255. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  256. return -1;
  257. }
  258. return 1;
  259. }
  260. int gcm_stream_final(void *vctx, unsigned char *out, size_t *outl,
  261. size_t outsize)
  262. {
  263. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  264. int i;
  265. i = gcm_cipher_internal(ctx, out, outl, NULL, 0);
  266. if (i <= 0)
  267. return 0;
  268. *outl = 0;
  269. return 1;
  270. }
  271. int gcm_cipher(void *vctx,
  272. unsigned char *out, size_t *outl, size_t outsize,
  273. const unsigned char *in, size_t inl)
  274. {
  275. PROV_GCM_CTX *ctx = (PROV_GCM_CTX *)vctx;
  276. if (outsize < inl) {
  277. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  278. return 0;
  279. }
  280. if (gcm_cipher_internal(ctx, out, outl, in, inl) <= 0)
  281. return 0;
  282. *outl = inl;
  283. return 1;
  284. }
  285. /*
  286. * See SP800-38D (GCM) Section 8 "Uniqueness requirement on IVS and keys"
  287. *
  288. * See also 8.2.2 RBG-based construction.
  289. * Random construction consists of a free field (which can be NULL) and a
  290. * random field which will use a DRBG that can return at least 96 bits of
  291. * entropy strength. (The DRBG must be seeded by the FIPS module).
  292. */
  293. static int gcm_iv_generate(PROV_GCM_CTX *ctx, int offset)
  294. {
  295. int sz = ctx->ivlen - offset;
  296. /* Must be at least 96 bits */
  297. if (sz <= 0 || ctx->ivlen < GCM_IV_DEFAULT_SIZE)
  298. return 0;
  299. /* Use DRBG to generate random iv */
  300. if (RAND_bytes_ex(ctx->libctx, ctx->iv + offset, sz) <= 0)
  301. return 0;
  302. ctx->iv_state = IV_STATE_BUFFERED;
  303. ctx->iv_gen_rand = 1;
  304. return 1;
  305. }
  306. static int gcm_cipher_internal(PROV_GCM_CTX *ctx, unsigned char *out,
  307. size_t *padlen, const unsigned char *in,
  308. size_t len)
  309. {
  310. size_t olen = 0;
  311. int rv = 0;
  312. const PROV_GCM_HW *hw = ctx->hw;
  313. if (ctx->tls_aad_len != UNINITIALISED_SIZET)
  314. return gcm_tls_cipher(ctx, out, padlen, in, len);
  315. if (!ctx->key_set || ctx->iv_state == IV_STATE_FINISHED)
  316. goto err;
  317. /*
  318. * FIPS requires generation of AES-GCM IV's inside the FIPS module.
  319. * The IV can still be set externally (the security policy will state that
  320. * this is not FIPS compliant). There are some applications
  321. * where setting the IV externally is the only option available.
  322. */
  323. if (ctx->iv_state == IV_STATE_UNINITIALISED) {
  324. if (!ctx->enc || !gcm_iv_generate(ctx, 0))
  325. goto err;
  326. }
  327. if (ctx->iv_state == IV_STATE_BUFFERED) {
  328. if (!hw->setiv(ctx, ctx->iv, ctx->ivlen))
  329. goto err;
  330. ctx->iv_state = IV_STATE_COPIED;
  331. }
  332. if (in != NULL) {
  333. /* The input is AAD if out is NULL */
  334. if (out == NULL) {
  335. if (!hw->aadupdate(ctx, in, len))
  336. goto err;
  337. } else {
  338. /* The input is ciphertext OR plaintext */
  339. if (!hw->cipherupdate(ctx, in, len, out))
  340. goto err;
  341. }
  342. } else {
  343. /* The tag must be set before actually decrypting data */
  344. if (!ctx->enc && ctx->taglen == UNINITIALISED_SIZET)
  345. goto err;
  346. if (!hw->cipherfinal(ctx, ctx->buf))
  347. goto err;
  348. ctx->iv_state = IV_STATE_FINISHED; /* Don't reuse the IV */
  349. goto finish;
  350. }
  351. olen = len;
  352. finish:
  353. rv = 1;
  354. err:
  355. *padlen = olen;
  356. return rv;
  357. }
  358. static int gcm_tls_init(PROV_GCM_CTX *dat, unsigned char *aad, size_t aad_len)
  359. {
  360. unsigned char *buf;
  361. size_t len;
  362. if (aad_len != EVP_AEAD_TLS1_AAD_LEN)
  363. return 0;
  364. /* Save the aad for later use. */
  365. buf = dat->buf;
  366. memcpy(buf, aad, aad_len);
  367. dat->tls_aad_len = aad_len;
  368. dat->tls_enc_records = 0;
  369. len = buf[aad_len - 2] << 8 | buf[aad_len - 1];
  370. /* Correct length for explicit iv. */
  371. if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  372. return 0;
  373. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  374. /* If decrypting correct for tag too. */
  375. if (!dat->enc) {
  376. if (len < EVP_GCM_TLS_TAG_LEN)
  377. return 0;
  378. len -= EVP_GCM_TLS_TAG_LEN;
  379. }
  380. buf[aad_len - 2] = (unsigned char)(len >> 8);
  381. buf[aad_len - 1] = (unsigned char)(len & 0xff);
  382. /* Extra padding: tag appended to record. */
  383. return EVP_GCM_TLS_TAG_LEN;
  384. }
  385. static int gcm_tls_iv_set_fixed(PROV_GCM_CTX *ctx, unsigned char *iv,
  386. size_t len)
  387. {
  388. /* Special case: -1 length restores whole IV */
  389. if (len == (size_t)-1) {
  390. memcpy(ctx->iv, iv, ctx->ivlen);
  391. ctx->iv_gen = 1;
  392. ctx->iv_state = IV_STATE_BUFFERED;
  393. return 1;
  394. }
  395. /* Fixed field must be at least 4 bytes and invocation field at least 8 */
  396. if ((len < EVP_GCM_TLS_FIXED_IV_LEN)
  397. || (ctx->ivlen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN)
  398. return 0;
  399. if (len > 0)
  400. memcpy(ctx->iv, iv, len);
  401. if (ctx->enc
  402. && RAND_bytes_ex(ctx->libctx, ctx->iv + len, ctx->ivlen - len) <= 0)
  403. return 0;
  404. ctx->iv_gen = 1;
  405. ctx->iv_state = IV_STATE_BUFFERED;
  406. return 1;
  407. }
  408. /*
  409. * Handle TLS GCM packet format. This consists of the last portion of the IV
  410. * followed by the payload and finally the tag. On encrypt generate IV,
  411. * encrypt payload and write the tag. On verify retrieve IV, decrypt payload
  412. * and verify tag.
  413. */
  414. static int gcm_tls_cipher(PROV_GCM_CTX *ctx, unsigned char *out, size_t *padlen,
  415. const unsigned char *in, size_t len)
  416. {
  417. int rv = 0;
  418. size_t arg = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  419. size_t plen = 0;
  420. unsigned char *tag = NULL;
  421. if (!ctx->key_set)
  422. goto err;
  423. /* Encrypt/decrypt must be performed in place */
  424. if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
  425. goto err;
  426. /*
  427. * Check for too many keys as per FIPS 140-2 IG A.5 "Key/IV Pair Uniqueness
  428. * Requirements from SP 800-38D". The requirements is for one party to the
  429. * communication to fail after 2^64 - 1 keys. We do this on the encrypting
  430. * side only.
  431. */
  432. if (ctx->enc && ++ctx->tls_enc_records == 0) {
  433. ERR_raise(ERR_LIB_PROV, EVP_R_TOO_MANY_RECORDS);
  434. goto err;
  435. }
  436. /*
  437. * Set IV from start of buffer or generate IV and write to start of
  438. * buffer.
  439. */
  440. if (ctx->enc) {
  441. if (!getivgen(ctx, out, arg))
  442. goto err;
  443. } else {
  444. if (!setivinv(ctx, out, arg))
  445. goto err;
  446. }
  447. /* Fix buffer and length to point to payload */
  448. in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  449. out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  450. len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  451. tag = ctx->enc ? out + len : (unsigned char *)in + len;
  452. if (!ctx->hw->oneshot(ctx, ctx->buf, ctx->tls_aad_len, in, len, out, tag,
  453. EVP_GCM_TLS_TAG_LEN)) {
  454. if (!ctx->enc)
  455. OPENSSL_cleanse(out, len);
  456. goto err;
  457. }
  458. if (ctx->enc)
  459. plen = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
  460. else
  461. plen = len;
  462. rv = 1;
  463. err:
  464. ctx->iv_state = IV_STATE_FINISHED;
  465. ctx->tls_aad_len = UNINITIALISED_SIZET;
  466. *padlen = plen;
  467. return rv;
  468. }