poly1305_meth.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2018 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/evp.h>
  10. #include "internal/evp_int.h"
  11. #include "internal/poly1305.h"
  12. #include "internal/cryptlib.h"
  13. #include "poly1305_local.h"
  14. /* typedef EVP_MAC_IMPL */
  15. struct evp_mac_impl_st {
  16. POLY1305 *ctx; /* poly1305 context */
  17. };
  18. static EVP_MAC_IMPL *poly1305_new(void)
  19. {
  20. EVP_MAC_IMPL *ctx;
  21. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL
  22. || (ctx->ctx = OPENSSL_zalloc(sizeof(POLY1305))) == NULL) {
  23. OPENSSL_free(ctx);
  24. return 0;
  25. }
  26. return ctx;
  27. }
  28. static void poly1305_free(EVP_MAC_IMPL *ctx)
  29. {
  30. if (ctx != NULL) {
  31. OPENSSL_free(ctx->ctx);
  32. OPENSSL_free(ctx);
  33. }
  34. }
  35. static EVP_MAC_IMPL *poly1305_dup(const EVP_MAC_IMPL *src)
  36. {
  37. EVP_MAC_IMPL *dst;
  38. dst = poly1305_new();
  39. if (dst == NULL)
  40. return NULL;
  41. *dst->ctx = *src->ctx;
  42. return dst;
  43. }
  44. static size_t poly1305_size(EVP_MAC_IMPL *ctx)
  45. {
  46. return POLY1305_DIGEST_SIZE;
  47. }
  48. static int poly1305_init(EVP_MAC_IMPL *ctx)
  49. {
  50. /* initialize the context in MAC_ctrl function */
  51. return 1;
  52. }
  53. static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data,
  54. size_t datalen)
  55. {
  56. POLY1305 *poly_ctx = ctx->ctx;
  57. /* poly1305 has nothing to return in its update function */
  58. Poly1305_Update(poly_ctx, data, datalen);
  59. return 1;
  60. }
  61. static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out)
  62. {
  63. POLY1305 *poly_ctx = ctx->ctx;
  64. Poly1305_Final(poly_ctx, out);
  65. return 1;
  66. }
  67. static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args)
  68. {
  69. POLY1305 *poly_ctx = ctx->ctx;
  70. unsigned char *key;
  71. size_t keylen;
  72. switch (cmd) {
  73. case EVP_MAC_CTRL_SET_KEY:
  74. key = va_arg(args, unsigned char *);
  75. keylen = va_arg(args, size_t);
  76. if (keylen != POLY1305_KEY_SIZE) {
  77. EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH);
  78. return 0;
  79. }
  80. Poly1305_Init(poly_ctx, key);
  81. return 1;
  82. default:
  83. return -2;
  84. }
  85. return 1;
  86. }
  87. static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...)
  88. {
  89. int rv;
  90. va_list args;
  91. va_start(args, cmd);
  92. rv = poly1305_ctrl(ctx, cmd, args);
  93. va_end(args);
  94. return rv;
  95. }
  96. static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
  97. {
  98. return poly1305_ctrl_int(ctx, cmd, buf, buflen);
  99. }
  100. static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx,
  101. const char *type, const char *value)
  102. {
  103. if (value == NULL)
  104. return 0;
  105. if (strcmp(type, "key") == 0)
  106. return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  107. value);
  108. if (strcmp(type, "hexkey") == 0)
  109. return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  110. value);
  111. return -2;
  112. }
  113. const EVP_MAC poly1305_meth = {
  114. EVP_MAC_POLY1305,
  115. poly1305_new,
  116. poly1305_dup,
  117. poly1305_free,
  118. poly1305_size,
  119. poly1305_init,
  120. poly1305_update,
  121. poly1305_final,
  122. poly1305_ctrl,
  123. poly1305_ctrl_str
  124. };