poly1305_pmeth.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/err.h>
  15. #include "internal/poly1305.h"
  16. #include "poly1305_local.h"
  17. #include "internal/evp_int.h"
  18. /* POLY1305 pkey context structure */
  19. typedef struct {
  20. ASN1_OCTET_STRING ktmp; /* Temp storage for key */
  21. POLY1305 ctx;
  22. } POLY1305_PKEY_CTX;
  23. static int pkey_poly1305_init(EVP_PKEY_CTX *ctx)
  24. {
  25. POLY1305_PKEY_CTX *pctx;
  26. if ((pctx = OPENSSL_zalloc(sizeof(*pctx))) == NULL) {
  27. CRYPTOerr(CRYPTO_F_PKEY_POLY1305_INIT, ERR_R_MALLOC_FAILURE);
  28. return 0;
  29. }
  30. pctx->ktmp.type = V_ASN1_OCTET_STRING;
  31. EVP_PKEY_CTX_set_data(ctx, pctx);
  32. EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
  33. return 1;
  34. }
  35. static void pkey_poly1305_cleanup(EVP_PKEY_CTX *ctx)
  36. {
  37. POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  38. if (pctx != NULL) {
  39. OPENSSL_clear_free(pctx->ktmp.data, pctx->ktmp.length);
  40. OPENSSL_clear_free(pctx, sizeof(*pctx));
  41. EVP_PKEY_CTX_set_data(ctx, NULL);
  42. }
  43. }
  44. static int pkey_poly1305_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  45. {
  46. POLY1305_PKEY_CTX *sctx, *dctx;
  47. /* allocate memory for dst->data and a new POLY1305_CTX in dst->data->ctx */
  48. if (!pkey_poly1305_init(dst))
  49. return 0;
  50. sctx = EVP_PKEY_CTX_get_data(src);
  51. dctx = EVP_PKEY_CTX_get_data(dst);
  52. if (ASN1_STRING_get0_data(&sctx->ktmp) != NULL &&
  53. !ASN1_STRING_copy(&dctx->ktmp, &sctx->ktmp)) {
  54. /* cleanup and free the POLY1305_PKEY_CTX in dst->data */
  55. pkey_poly1305_cleanup(dst);
  56. return 0;
  57. }
  58. memcpy(&dctx->ctx, &sctx->ctx, sizeof(POLY1305));
  59. return 1;
  60. }
  61. static int pkey_poly1305_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  62. {
  63. ASN1_OCTET_STRING *key;
  64. POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  65. if (ASN1_STRING_get0_data(&pctx->ktmp) == NULL)
  66. return 0;
  67. key = ASN1_OCTET_STRING_dup(&pctx->ktmp);
  68. if (key == NULL)
  69. return 0;
  70. return EVP_PKEY_assign_POLY1305(pkey, key);
  71. }
  72. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  73. {
  74. POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
  75. Poly1305_Update(&pctx->ctx, data, count);
  76. return 1;
  77. }
  78. static int poly1305_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  79. {
  80. POLY1305_PKEY_CTX *pctx = ctx->data;
  81. ASN1_OCTET_STRING *key = (ASN1_OCTET_STRING *)ctx->pkey->pkey.ptr;
  82. if (key->length != POLY1305_KEY_SIZE)
  83. return 0;
  84. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  85. EVP_MD_CTX_set_update_fn(mctx, int_update);
  86. Poly1305_Init(&pctx->ctx, key->data);
  87. return 1;
  88. }
  89. static int poly1305_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  90. EVP_MD_CTX *mctx)
  91. {
  92. POLY1305_PKEY_CTX *pctx = ctx->data;
  93. *siglen = POLY1305_DIGEST_SIZE;
  94. if (sig != NULL)
  95. Poly1305_Final(&pctx->ctx, sig);
  96. return 1;
  97. }
  98. static int pkey_poly1305_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  99. {
  100. POLY1305_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  101. const unsigned char *key;
  102. size_t len;
  103. switch (type) {
  104. case EVP_PKEY_CTRL_MD:
  105. /* ignore */
  106. break;
  107. case EVP_PKEY_CTRL_SET_MAC_KEY:
  108. case EVP_PKEY_CTRL_DIGESTINIT:
  109. if (type == EVP_PKEY_CTRL_SET_MAC_KEY) {
  110. /* user explicitly setting the key */
  111. key = p2;
  112. len = p1;
  113. } else {
  114. /* user indirectly setting the key via EVP_DigestSignInit */
  115. key = EVP_PKEY_get0_poly1305(EVP_PKEY_CTX_get0_pkey(ctx), &len);
  116. }
  117. if (key == NULL || len != POLY1305_KEY_SIZE ||
  118. !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
  119. return 0;
  120. Poly1305_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp));
  121. break;
  122. default:
  123. return -2;
  124. }
  125. return 1;
  126. }
  127. static int pkey_poly1305_ctrl_str(EVP_PKEY_CTX *ctx,
  128. const char *type, const char *value)
  129. {
  130. if (value == NULL)
  131. return 0;
  132. if (strcmp(type, "key") == 0)
  133. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  134. if (strcmp(type, "hexkey") == 0)
  135. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  136. return -2;
  137. }
  138. const EVP_PKEY_METHOD poly1305_pkey_meth = {
  139. EVP_PKEY_POLY1305,
  140. EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
  141. pkey_poly1305_init,
  142. pkey_poly1305_copy,
  143. pkey_poly1305_cleanup,
  144. 0, 0,
  145. 0,
  146. pkey_poly1305_keygen,
  147. 0, 0,
  148. 0, 0,
  149. 0, 0,
  150. poly1305_signctx_init,
  151. poly1305_signctx,
  152. 0, 0,
  153. 0, 0,
  154. 0, 0,
  155. 0, 0,
  156. pkey_poly1305_ctrl,
  157. pkey_poly1305_ctrl_str
  158. };