poly1305_prov.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2018-2022 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. #include <openssl/core_dispatch.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/err.h>
  14. #include <openssl/proverr.h>
  15. #include "crypto/poly1305.h"
  16. #include "prov/implementations.h"
  17. #include "prov/providercommon.h"
  18. /*
  19. * Forward declaration of everything implemented here. This is not strictly
  20. * necessary for the compiler, but provides an assurance that the signatures
  21. * of the functions in the dispatch table are correct.
  22. */
  23. static OSSL_FUNC_mac_newctx_fn poly1305_new;
  24. static OSSL_FUNC_mac_dupctx_fn poly1305_dup;
  25. static OSSL_FUNC_mac_freectx_fn poly1305_free;
  26. static OSSL_FUNC_mac_gettable_params_fn poly1305_gettable_params;
  27. static OSSL_FUNC_mac_get_params_fn poly1305_get_params;
  28. static OSSL_FUNC_mac_settable_ctx_params_fn poly1305_settable_ctx_params;
  29. static OSSL_FUNC_mac_set_ctx_params_fn poly1305_set_ctx_params;
  30. static OSSL_FUNC_mac_init_fn poly1305_init;
  31. static OSSL_FUNC_mac_update_fn poly1305_update;
  32. static OSSL_FUNC_mac_final_fn poly1305_final;
  33. struct poly1305_data_st {
  34. void *provctx;
  35. int updated;
  36. POLY1305 poly1305; /* Poly1305 data */
  37. };
  38. static void *poly1305_new(void *provctx)
  39. {
  40. struct poly1305_data_st *ctx;
  41. if (!ossl_prov_is_running())
  42. return NULL;
  43. ctx = OPENSSL_zalloc(sizeof(*ctx));
  44. if (ctx != NULL)
  45. ctx->provctx = provctx;
  46. return ctx;
  47. }
  48. static void poly1305_free(void *vmacctx)
  49. {
  50. OPENSSL_free(vmacctx);
  51. }
  52. static void *poly1305_dup(void *vsrc)
  53. {
  54. struct poly1305_data_st *src = vsrc;
  55. struct poly1305_data_st *dst;
  56. if (!ossl_prov_is_running())
  57. return NULL;
  58. dst = OPENSSL_malloc(sizeof(*dst));
  59. if (dst == NULL)
  60. return NULL;
  61. *dst = *src;
  62. return dst;
  63. }
  64. static size_t poly1305_size(void)
  65. {
  66. return POLY1305_DIGEST_SIZE;
  67. }
  68. static int poly1305_setkey(struct poly1305_data_st *ctx,
  69. const unsigned char *key, size_t keylen)
  70. {
  71. if (keylen != POLY1305_KEY_SIZE) {
  72. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  73. return 0;
  74. }
  75. Poly1305_Init(&ctx->poly1305, key);
  76. ctx->updated = 0;
  77. return 1;
  78. }
  79. static int poly1305_init(void *vmacctx, const unsigned char *key,
  80. size_t keylen, const OSSL_PARAM params[])
  81. {
  82. struct poly1305_data_st *ctx = vmacctx;
  83. /* initialize the context in MAC_ctrl function */
  84. if (!ossl_prov_is_running() || !poly1305_set_ctx_params(ctx, params))
  85. return 0;
  86. if (key != NULL)
  87. return poly1305_setkey(ctx, key, keylen);
  88. /* no reinitialization of context with the same key is allowed */
  89. return ctx->updated == 0;
  90. }
  91. static int poly1305_update(void *vmacctx, const unsigned char *data,
  92. size_t datalen)
  93. {
  94. struct poly1305_data_st *ctx = vmacctx;
  95. ctx->updated = 1;
  96. if (datalen == 0)
  97. return 1;
  98. /* poly1305 has nothing to return in its update function */
  99. Poly1305_Update(&ctx->poly1305, data, datalen);
  100. return 1;
  101. }
  102. static int poly1305_final(void *vmacctx, unsigned char *out, size_t *outl,
  103. size_t outsize)
  104. {
  105. struct poly1305_data_st *ctx = vmacctx;
  106. if (!ossl_prov_is_running())
  107. return 0;
  108. ctx->updated = 1;
  109. Poly1305_Final(&ctx->poly1305, out);
  110. *outl = poly1305_size();
  111. return 1;
  112. }
  113. static const OSSL_PARAM known_gettable_params[] = {
  114. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  115. OSSL_PARAM_END
  116. };
  117. static const OSSL_PARAM *poly1305_gettable_params(void *provctx)
  118. {
  119. return known_gettable_params;
  120. }
  121. static int poly1305_get_params(OSSL_PARAM params[])
  122. {
  123. OSSL_PARAM *p;
  124. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  125. return OSSL_PARAM_set_size_t(p, poly1305_size());
  126. return 1;
  127. }
  128. static const OSSL_PARAM known_settable_ctx_params[] = {
  129. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  130. OSSL_PARAM_END
  131. };
  132. static const OSSL_PARAM *poly1305_settable_ctx_params(ossl_unused void *ctx,
  133. ossl_unused void *provctx)
  134. {
  135. return known_settable_ctx_params;
  136. }
  137. static int poly1305_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
  138. {
  139. struct poly1305_data_st *ctx = vmacctx;
  140. const OSSL_PARAM *p;
  141. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
  142. && !poly1305_setkey(ctx, p->data, p->data_size))
  143. return 0;
  144. return 1;
  145. }
  146. const OSSL_DISPATCH ossl_poly1305_functions[] = {
  147. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))poly1305_new },
  148. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))poly1305_dup },
  149. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))poly1305_free },
  150. { OSSL_FUNC_MAC_INIT, (void (*)(void))poly1305_init },
  151. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))poly1305_update },
  152. { OSSL_FUNC_MAC_FINAL, (void (*)(void))poly1305_final },
  153. { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))poly1305_gettable_params },
  154. { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))poly1305_get_params },
  155. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  156. (void (*)(void))poly1305_settable_ctx_params },
  157. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))poly1305_set_ctx_params },
  158. { 0, NULL }
  159. };