2
0

siphash_pmeth.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "crypto/siphash.h"
  16. #include "siphash_local.h"
  17. #include "crypto/evp.h"
  18. /* SIPHASH pkey context structure */
  19. typedef struct siphash_pkey_ctx_st {
  20. ASN1_OCTET_STRING ktmp; /* Temp storage for key */
  21. SIPHASH ctx;
  22. } SIPHASH_PKEY_CTX;
  23. static int pkey_siphash_init(EVP_PKEY_CTX *ctx)
  24. {
  25. SIPHASH_PKEY_CTX *pctx;
  26. if ((pctx = OPENSSL_zalloc(sizeof(*pctx))) == NULL) {
  27. CRYPTOerr(CRYPTO_F_PKEY_SIPHASH_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_siphash_cleanup(EVP_PKEY_CTX *ctx)
  36. {
  37. SIPHASH_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_siphash_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  45. {
  46. SIPHASH_PKEY_CTX *sctx, *dctx;
  47. /* allocate memory for dst->data and a new SIPHASH_CTX in dst->data->ctx */
  48. if (!pkey_siphash_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 SIPHASH_PKEY_CTX in dst->data */
  55. pkey_siphash_cleanup(dst);
  56. return 0;
  57. }
  58. memcpy(&dctx->ctx, &sctx->ctx, sizeof(SIPHASH));
  59. return 1;
  60. }
  61. static int pkey_siphash_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  62. {
  63. ASN1_OCTET_STRING *key;
  64. SIPHASH_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_SIPHASH(pkey, key);
  71. }
  72. static int int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  73. {
  74. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
  75. SipHash_Update(&pctx->ctx, data, count);
  76. return 1;
  77. }
  78. static int siphash_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  79. {
  80. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  81. const unsigned char* key;
  82. size_t len;
  83. key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
  84. if (key == NULL || len != SIPHASH_KEY_SIZE)
  85. return 0;
  86. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  87. EVP_MD_CTX_set_update_fn(mctx, int_update);
  88. return SipHash_Init(&pctx->ctx, key, 0, 0);
  89. }
  90. static int siphash_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  91. EVP_MD_CTX *mctx)
  92. {
  93. SIPHASH_PKEY_CTX *pctx = ctx->data;
  94. *siglen = SipHash_hash_size(&pctx->ctx);
  95. if (sig != NULL)
  96. return SipHash_Final(&pctx->ctx, sig, *siglen);
  97. return 1;
  98. }
  99. static int pkey_siphash_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  100. {
  101. SIPHASH_PKEY_CTX *pctx = EVP_PKEY_CTX_get_data(ctx);
  102. const unsigned char *key;
  103. size_t len;
  104. switch (type) {
  105. case EVP_PKEY_CTRL_MD:
  106. /* ignore */
  107. break;
  108. case EVP_PKEY_CTRL_SET_DIGEST_SIZE:
  109. return SipHash_set_hash_size(&pctx->ctx, p1);
  110. case EVP_PKEY_CTRL_SET_MAC_KEY:
  111. case EVP_PKEY_CTRL_DIGESTINIT:
  112. if (type == EVP_PKEY_CTRL_SET_MAC_KEY) {
  113. /* user explicitly setting the key */
  114. key = p2;
  115. len = p1;
  116. } else {
  117. /* user indirectly setting the key via EVP_DigestSignInit */
  118. key = EVP_PKEY_get0_siphash(EVP_PKEY_CTX_get0_pkey(ctx), &len);
  119. }
  120. if (key == NULL || len != SIPHASH_KEY_SIZE ||
  121. !ASN1_OCTET_STRING_set(&pctx->ktmp, key, len))
  122. return 0;
  123. /* use default rounds (2,4) */
  124. return SipHash_Init(&pctx->ctx, ASN1_STRING_get0_data(&pctx->ktmp),
  125. 0, 0);
  126. default:
  127. return -2;
  128. }
  129. return 1;
  130. }
  131. static int pkey_siphash_ctrl_str(EVP_PKEY_CTX *ctx,
  132. const char *type, const char *value)
  133. {
  134. if (value == NULL)
  135. return 0;
  136. if (strcmp(type, "digestsize") == 0) {
  137. size_t hash_size = atoi(value);
  138. return pkey_siphash_ctrl(ctx, EVP_PKEY_CTRL_SET_DIGEST_SIZE, hash_size,
  139. NULL);
  140. }
  141. if (strcmp(type, "key") == 0)
  142. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  143. if (strcmp(type, "hexkey") == 0)
  144. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, value);
  145. return -2;
  146. }
  147. const EVP_PKEY_METHOD siphash_pkey_meth = {
  148. EVP_PKEY_SIPHASH,
  149. EVP_PKEY_FLAG_SIGCTX_CUSTOM, /* we don't deal with a separate MD */
  150. pkey_siphash_init,
  151. pkey_siphash_copy,
  152. pkey_siphash_cleanup,
  153. 0, 0,
  154. 0,
  155. pkey_siphash_keygen,
  156. 0, 0,
  157. 0, 0,
  158. 0, 0,
  159. siphash_signctx_init,
  160. siphash_signctx,
  161. 0, 0,
  162. 0, 0,
  163. 0, 0,
  164. 0, 0,
  165. pkey_siphash_ctrl,
  166. pkey_siphash_ctrl_str
  167. };