poly1305_meth.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 int poly1305_copy(EVP_MAC_IMPL *dst, EVP_MAC_IMPL *src)
  36. {
  37. *dst->ctx = *src->ctx;
  38. return 1;
  39. }
  40. static size_t poly1305_size(EVP_MAC_IMPL *ctx)
  41. {
  42. return POLY1305_DIGEST_SIZE;
  43. }
  44. static int poly1305_init(EVP_MAC_IMPL *ctx)
  45. {
  46. /* initialize the context in MAC_ctrl function */
  47. return 1;
  48. }
  49. static int poly1305_update(EVP_MAC_IMPL *ctx, const unsigned char *data,
  50. size_t datalen)
  51. {
  52. POLY1305 *poly_ctx = ctx->ctx;
  53. /* poly1305 has nothing to return in its update function */
  54. Poly1305_Update(poly_ctx, data, datalen);
  55. return 1;
  56. }
  57. static int poly1305_final(EVP_MAC_IMPL *ctx, unsigned char *out)
  58. {
  59. POLY1305 *poly_ctx = ctx->ctx;
  60. Poly1305_Final(poly_ctx, out);
  61. return 1;
  62. }
  63. static int poly1305_ctrl(EVP_MAC_IMPL *ctx, int cmd, va_list args)
  64. {
  65. POLY1305 *poly_ctx = ctx->ctx;
  66. unsigned char *key;
  67. size_t keylen;
  68. switch (cmd) {
  69. case EVP_MAC_CTRL_SET_KEY:
  70. key = va_arg(args, unsigned char *);
  71. keylen = va_arg(args, size_t);
  72. if (keylen != POLY1305_KEY_SIZE) {
  73. EVPerr(EVP_F_POLY1305_CTRL, EVP_R_INVALID_KEY_LENGTH);
  74. return 0;
  75. }
  76. Poly1305_Init(poly_ctx, key);
  77. return 1;
  78. default:
  79. return -2;
  80. }
  81. return 1;
  82. }
  83. static int poly1305_ctrl_int(EVP_MAC_IMPL *ctx, int cmd, ...)
  84. {
  85. int rv;
  86. va_list args;
  87. va_start(args, cmd);
  88. rv = poly1305_ctrl(ctx, cmd, args);
  89. va_end(args);
  90. return rv;
  91. }
  92. static int poly1305_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
  93. {
  94. return poly1305_ctrl_int(ctx, cmd, buf, buflen);
  95. }
  96. static int poly1305_ctrl_str(EVP_MAC_IMPL *ctx,
  97. const char *type, const char *value)
  98. {
  99. if (value == NULL)
  100. return 0;
  101. if (strcmp(type, "key") == 0)
  102. return EVP_str2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  103. value);
  104. if (strcmp(type, "hexkey") == 0)
  105. return EVP_hex2ctrl(poly1305_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
  106. value);
  107. return -2;
  108. }
  109. const EVP_MAC poly1305_meth = {
  110. EVP_MAC_POLY1305,
  111. poly1305_new,
  112. poly1305_copy,
  113. poly1305_free,
  114. poly1305_size,
  115. poly1305_init,
  116. poly1305_update,
  117. poly1305_final,
  118. poly1305_ctrl,
  119. poly1305_ctrl_str
  120. };