gost_crypt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /**********************************************************************
  2. * gost_crypt.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * OpenSSL interface to GOST 28147-89 cipher functions *
  7. * Requires OpenSSL 0.9.9 for compilation *
  8. **********************************************************************/
  9. #include <string.h>
  10. #include "gost89.h"
  11. #include <openssl/rand.h>
  12. #include "e_gost_err.h"
  13. #include "gost_lcl.h"
  14. #if !defined(CCGOST_DEBUG) && !defined(DEBUG)
  15. # ifndef NDEBUG
  16. # define NDEBUG
  17. # endif
  18. #endif
  19. #include <assert.h>
  20. static int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  21. const unsigned char *iv, int enc);
  22. static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  23. const unsigned char *iv, int enc);
  24. /* Handles block of data in CFB mode */
  25. static int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
  26. const unsigned char *in, size_t inl);
  27. /* Handles block of data in CNT mode */
  28. static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
  29. const unsigned char *in, size_t inl);
  30. /* Cleanup function */
  31. static int gost_cipher_cleanup(EVP_CIPHER_CTX *);
  32. /* set/get cipher parameters */
  33. static int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
  34. static int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params);
  35. /* Control function */
  36. static int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
  37. EVP_CIPHER cipher_gost = {
  38. NID_id_Gost28147_89,
  39. 1, /* block_size */
  40. 32, /* key_size */
  41. 8, /* iv_len */
  42. EVP_CIPH_CFB_MODE | EVP_CIPH_NO_PADDING |
  43. EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
  44. gost_cipher_init,
  45. gost_cipher_do_cfb,
  46. gost_cipher_cleanup,
  47. sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
  48. gost89_set_asn1_parameters,
  49. gost89_get_asn1_parameters,
  50. gost_cipher_ctl,
  51. NULL,
  52. };
  53. EVP_CIPHER cipher_gost_cpacnt = {
  54. NID_gost89_cnt,
  55. 1, /* block_size */
  56. 32, /* key_size */
  57. 8, /* iv_len */
  58. EVP_CIPH_OFB_MODE | EVP_CIPH_NO_PADDING |
  59. EVP_CIPH_CUSTOM_IV | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT,
  60. gost_cipher_init_cpa,
  61. gost_cipher_do_cnt,
  62. gost_cipher_cleanup,
  63. sizeof(struct ossl_gost_cipher_ctx), /* ctx_size */
  64. gost89_set_asn1_parameters,
  65. gost89_get_asn1_parameters,
  66. gost_cipher_ctl,
  67. NULL,
  68. };
  69. /* Implementation of GOST 28147-89 in MAC (imitovstavka) mode */
  70. /* Init functions which set specific parameters */
  71. static int gost_imit_init_cpa(EVP_MD_CTX *ctx);
  72. /* process block of data */
  73. static int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count);
  74. /* Return computed value */
  75. static int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md);
  76. /* Copies context */
  77. static int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from);
  78. static int gost_imit_cleanup(EVP_MD_CTX *ctx);
  79. /* Control function, knows how to set MAC key.*/
  80. static int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
  81. EVP_MD imit_gost_cpa = {
  82. NID_id_Gost28147_89_MAC,
  83. NID_undef,
  84. 4,
  85. 0,
  86. gost_imit_init_cpa,
  87. gost_imit_update,
  88. gost_imit_final,
  89. gost_imit_copy,
  90. gost_imit_cleanup,
  91. NULL,
  92. NULL,
  93. {0, 0, 0, 0, 0},
  94. 8,
  95. sizeof(struct ossl_gost_imit_ctx),
  96. gost_imit_ctrl
  97. };
  98. /*
  99. * Correspondence between gost parameter OIDs and substitution blocks
  100. * NID field is filed by register_gost_NID function in engine.c
  101. * upon engine initialization
  102. */
  103. struct gost_cipher_info gost_cipher_list[] = {
  104. /*- NID *//*
  105. * Subst block
  106. *//*
  107. * Key meshing
  108. */
  109. /*
  110. * {NID_id_GostR3411_94_CryptoProParamSet,&GostR3411_94_CryptoProParamSet,0},
  111. */
  112. {NID_id_Gost28147_89_cc, &GostR3411_94_CryptoProParamSet, 0},
  113. {NID_id_Gost28147_89_CryptoPro_A_ParamSet, &Gost28147_CryptoProParamSetA,
  114. 1},
  115. {NID_id_Gost28147_89_CryptoPro_B_ParamSet, &Gost28147_CryptoProParamSetB,
  116. 1},
  117. {NID_id_Gost28147_89_CryptoPro_C_ParamSet, &Gost28147_CryptoProParamSetC,
  118. 1},
  119. {NID_id_Gost28147_89_CryptoPro_D_ParamSet, &Gost28147_CryptoProParamSetD,
  120. 1},
  121. {NID_id_Gost28147_89_TestParamSet, &Gost28147_TestParamSet, 1},
  122. {NID_undef, NULL, 0}
  123. };
  124. /*
  125. * get encryption parameters from crypto network settings FIXME For now we
  126. * use environment var CRYPT_PARAMS as place to store these settings.
  127. * Actually, it is better to use engine control command, read from
  128. * configuration file to set them
  129. */
  130. const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj)
  131. {
  132. int nid;
  133. struct gost_cipher_info *param;
  134. if (!obj) {
  135. const char *params = get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS);
  136. if (!params || !strlen(params))
  137. return &gost_cipher_list[1];
  138. nid = OBJ_txt2nid(params);
  139. if (nid == NID_undef) {
  140. GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS,
  141. GOST_R_INVALID_CIPHER_PARAM_OID);
  142. return NULL;
  143. }
  144. } else {
  145. nid = OBJ_obj2nid(obj);
  146. }
  147. for (param = gost_cipher_list; param->sblock != NULL && param->nid != nid;
  148. param++) ;
  149. if (!param->sblock) {
  150. GOSTerr(GOST_F_GET_ENCRYPTION_PARAMS, GOST_R_INVALID_CIPHER_PARAMS);
  151. return NULL;
  152. }
  153. return param;
  154. }
  155. /* Sets cipher param from paramset NID. */
  156. static int gost_cipher_set_param(struct ossl_gost_cipher_ctx *c, int nid)
  157. {
  158. const struct gost_cipher_info *param;
  159. param =
  160. get_encryption_params((nid == NID_undef ? NULL : OBJ_nid2obj(nid)));
  161. if (!param)
  162. return 0;
  163. c->paramNID = param->nid;
  164. c->key_meshing = param->key_meshing;
  165. c->count = 0;
  166. gost_init(&(c->cctx), param->sblock);
  167. return 1;
  168. }
  169. /* Initializes EVP_CIPHER_CTX by paramset NID */
  170. static int gost_cipher_init_param(EVP_CIPHER_CTX *ctx,
  171. const unsigned char *key,
  172. const unsigned char *iv, int enc,
  173. int paramNID, int mode)
  174. {
  175. struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
  176. if (ctx->app_data == NULL) {
  177. if (!gost_cipher_set_param(c, paramNID))
  178. return 0;
  179. ctx->app_data = ctx->cipher_data;
  180. }
  181. if (key)
  182. gost_key(&(c->cctx), key);
  183. if (iv)
  184. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  185. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  186. return 1;
  187. }
  188. static int gost_cipher_init_cpa(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  189. const unsigned char *iv, int enc)
  190. {
  191. struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
  192. gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
  193. c->key_meshing = 1;
  194. c->count = 0;
  195. if (key)
  196. gost_key(&(c->cctx), key);
  197. if (iv)
  198. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  199. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  200. return 1;
  201. }
  202. /* Initializes EVP_CIPHER_CTX with default values */
  203. int gost_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  204. const unsigned char *iv, int enc)
  205. {
  206. return gost_cipher_init_param(ctx, key, iv, enc, NID_undef,
  207. EVP_CIPH_CFB_MODE);
  208. }
  209. /*
  210. * Wrapper around gostcrypt function from gost89.c which perform key meshing
  211. * when nesseccary
  212. */
  213. static void gost_crypt_mesh(void *ctx, unsigned char *iv, unsigned char *buf)
  214. {
  215. struct ossl_gost_cipher_ctx *c = ctx;
  216. assert(c->count % 8 == 0 && c->count <= 1024);
  217. if (c->key_meshing && c->count == 1024) {
  218. cryptopro_key_meshing(&(c->cctx), iv);
  219. }
  220. gostcrypt(&(c->cctx), iv, buf);
  221. c->count = c->count % 1024 + 8;
  222. }
  223. static void gost_cnt_next(void *ctx, unsigned char *iv, unsigned char *buf)
  224. {
  225. struct ossl_gost_cipher_ctx *c = ctx;
  226. word32 g, go;
  227. unsigned char buf1[8];
  228. assert(c->count % 8 == 0 && c->count <= 1024);
  229. if (c->key_meshing && c->count == 1024) {
  230. cryptopro_key_meshing(&(c->cctx), iv);
  231. }
  232. if (c->count == 0) {
  233. gostcrypt(&(c->cctx), iv, buf1);
  234. } else {
  235. memcpy(buf1, iv, 8);
  236. }
  237. g = buf1[0] | (buf1[1] << 8) | (buf1[2] << 16) | (buf1[3] << 24);
  238. g += 0x01010101;
  239. buf1[0] = (unsigned char)(g & 0xff);
  240. buf1[1] = (unsigned char)((g >> 8) & 0xff);
  241. buf1[2] = (unsigned char)((g >> 16) & 0xff);
  242. buf1[3] = (unsigned char)((g >> 24) & 0xff);
  243. g = buf1[4] | (buf1[5] << 8) | (buf1[6] << 16) | (buf1[7] << 24);
  244. go = g;
  245. g += 0x01010104;
  246. if (go > g) /* overflow */
  247. g++;
  248. buf1[4] = (unsigned char)(g & 0xff);
  249. buf1[5] = (unsigned char)((g >> 8) & 0xff);
  250. buf1[6] = (unsigned char)((g >> 16) & 0xff);
  251. buf1[7] = (unsigned char)((g >> 24) & 0xff);
  252. memcpy(iv, buf1, 8);
  253. gostcrypt(&(c->cctx), buf1, buf);
  254. c->count = c->count % 1024 + 8;
  255. }
  256. /* GOST encryption in CFB mode */
  257. int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
  258. const unsigned char *in, size_t inl)
  259. {
  260. const unsigned char *in_ptr = in;
  261. unsigned char *out_ptr = out;
  262. size_t i = 0;
  263. size_t j = 0;
  264. /* process partial block if any */
  265. if (ctx->num) {
  266. for (j = ctx->num, i = 0; j < 8 && i < inl;
  267. j++, i++, in_ptr++, out_ptr++) {
  268. if (!ctx->encrypt)
  269. ctx->buf[j + 8] = *in_ptr;
  270. *out_ptr = ctx->buf[j] ^ (*in_ptr);
  271. if (ctx->encrypt)
  272. ctx->buf[j + 8] = *out_ptr;
  273. }
  274. if (j == 8) {
  275. memcpy(ctx->iv, ctx->buf + 8, 8);
  276. ctx->num = 0;
  277. } else {
  278. ctx->num = j;
  279. return 1;
  280. }
  281. }
  282. for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
  283. /*
  284. * block cipher current iv
  285. */
  286. gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
  287. /*
  288. * xor next block of input text with it and output it
  289. */
  290. /*
  291. * output this block
  292. */
  293. if (!ctx->encrypt)
  294. memcpy(ctx->iv, in_ptr, 8);
  295. for (j = 0; j < 8; j++) {
  296. out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
  297. }
  298. /* Encrypt */
  299. /* Next iv is next block of cipher text */
  300. if (ctx->encrypt)
  301. memcpy(ctx->iv, out_ptr, 8);
  302. }
  303. /* Process rest of buffer */
  304. if (i < inl) {
  305. gost_crypt_mesh(ctx->cipher_data, ctx->iv, ctx->buf);
  306. if (!ctx->encrypt)
  307. memcpy(ctx->buf + 8, in_ptr, inl - i);
  308. for (j = 0; i < inl; j++, i++) {
  309. out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
  310. }
  311. ctx->num = j;
  312. if (ctx->encrypt)
  313. memcpy(ctx->buf + 8, out_ptr, j);
  314. } else {
  315. ctx->num = 0;
  316. }
  317. return 1;
  318. }
  319. static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
  320. const unsigned char *in, size_t inl)
  321. {
  322. const unsigned char *in_ptr = in;
  323. unsigned char *out_ptr = out;
  324. size_t i = 0;
  325. size_t j;
  326. /* process partial block if any */
  327. if (ctx->num) {
  328. for (j = ctx->num, i = 0; j < 8 && i < inl;
  329. j++, i++, in_ptr++, out_ptr++) {
  330. *out_ptr = ctx->buf[j] ^ (*in_ptr);
  331. }
  332. if (j == 8) {
  333. ctx->num = 0;
  334. } else {
  335. ctx->num = j;
  336. return 1;
  337. }
  338. }
  339. for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
  340. /*
  341. * block cipher current iv
  342. */
  343. /* Encrypt */
  344. gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
  345. /*
  346. * xor next block of input text with it and output it
  347. */
  348. /*
  349. * output this block
  350. */
  351. for (j = 0; j < 8; j++) {
  352. out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
  353. }
  354. }
  355. /* Process rest of buffer */
  356. if (i < inl) {
  357. gost_cnt_next(ctx->cipher_data, ctx->iv, ctx->buf);
  358. for (j = 0; i < inl; j++, i++) {
  359. out_ptr[j] = ctx->buf[j] ^ in_ptr[j];
  360. }
  361. ctx->num = j;
  362. } else {
  363. ctx->num = 0;
  364. }
  365. return 1;
  366. }
  367. /* Cleaning up of EVP_CIPHER_CTX */
  368. int gost_cipher_cleanup(EVP_CIPHER_CTX *ctx)
  369. {
  370. gost_destroy(&((struct ossl_gost_cipher_ctx *)ctx->cipher_data)->cctx);
  371. ctx->app_data = NULL;
  372. return 1;
  373. }
  374. /* Control function for gost cipher */
  375. int gost_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  376. {
  377. switch (type) {
  378. case EVP_CTRL_RAND_KEY:
  379. {
  380. if (RAND_bytes((unsigned char *)ptr, ctx->key_len) <= 0) {
  381. GOSTerr(GOST_F_GOST_CIPHER_CTL,
  382. GOST_R_RANDOM_GENERATOR_ERROR);
  383. return -1;
  384. }
  385. break;
  386. }
  387. case EVP_CTRL_PBE_PRF_NID:
  388. if (ptr) {
  389. *((int *)ptr) = NID_id_HMACGostR3411_94;
  390. return 1;
  391. } else {
  392. return 0;
  393. }
  394. default:
  395. GOSTerr(GOST_F_GOST_CIPHER_CTL,
  396. GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
  397. return -1;
  398. }
  399. return 1;
  400. }
  401. /* Set cipher parameters from ASN1 structure */
  402. int gost89_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
  403. {
  404. int len = 0;
  405. unsigned char *buf = NULL;
  406. unsigned char *p = NULL;
  407. struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
  408. GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
  409. ASN1_OCTET_STRING *os = NULL;
  410. if (!gcp) {
  411. GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
  412. return 0;
  413. }
  414. if (!ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len)) {
  415. GOST_CIPHER_PARAMS_free(gcp);
  416. GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
  417. return 0;
  418. }
  419. ASN1_OBJECT_free(gcp->enc_param_set);
  420. gcp->enc_param_set = OBJ_nid2obj(c->paramNID);
  421. len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
  422. p = buf = (unsigned char *)OPENSSL_malloc(len);
  423. if (!buf) {
  424. GOST_CIPHER_PARAMS_free(gcp);
  425. GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
  426. return 0;
  427. }
  428. i2d_GOST_CIPHER_PARAMS(gcp, &p);
  429. GOST_CIPHER_PARAMS_free(gcp);
  430. os = ASN1_OCTET_STRING_new();
  431. if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
  432. OPENSSL_free(buf);
  433. GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, GOST_R_NO_MEMORY);
  434. return 0;
  435. }
  436. OPENSSL_free(buf);
  437. ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
  438. return 1;
  439. }
  440. /* Store parameters into ASN1 structure */
  441. int gost89_get_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
  442. {
  443. int ret = -1;
  444. int len;
  445. GOST_CIPHER_PARAMS *gcp = NULL;
  446. unsigned char *p;
  447. struct ossl_gost_cipher_ctx *c = ctx->cipher_data;
  448. if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
  449. return ret;
  450. }
  451. p = params->value.sequence->data;
  452. gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
  453. params->value.sequence->length);
  454. len = gcp->iv->length;
  455. if (len != ctx->cipher->iv_len) {
  456. GOST_CIPHER_PARAMS_free(gcp);
  457. GOSTerr(GOST_F_GOST89_GET_ASN1_PARAMETERS, GOST_R_INVALID_IV_LENGTH);
  458. return -1;
  459. }
  460. if (!gost_cipher_set_param(c, OBJ_obj2nid(gcp->enc_param_set))) {
  461. GOST_CIPHER_PARAMS_free(gcp);
  462. return -1;
  463. }
  464. memcpy(ctx->oiv, gcp->iv->data, len);
  465. GOST_CIPHER_PARAMS_free(gcp);
  466. return 1;
  467. }
  468. int gost_imit_init_cpa(EVP_MD_CTX *ctx)
  469. {
  470. struct ossl_gost_imit_ctx *c = ctx->md_data;
  471. memset(c->buffer, 0, sizeof(c->buffer));
  472. memset(c->partial_block, 0, sizeof(c->partial_block));
  473. c->count = 0;
  474. c->bytes_left = 0;
  475. c->key_meshing = 1;
  476. gost_init(&(c->cctx), &Gost28147_CryptoProParamSetA);
  477. return 1;
  478. }
  479. static void mac_block_mesh(struct ossl_gost_imit_ctx *c,
  480. const unsigned char *data)
  481. {
  482. unsigned char buffer[8];
  483. /*
  484. * We are using local buffer for iv because CryptoPro doesn't interpret
  485. * internal state of MAC algorithm as iv during keymeshing (but does
  486. * initialize internal state from iv in key transport
  487. */
  488. assert(c->count % 8 == 0 && c->count <= 1024);
  489. if (c->key_meshing && c->count == 1024) {
  490. cryptopro_key_meshing(&(c->cctx), buffer);
  491. }
  492. mac_block(&(c->cctx), c->buffer, data);
  493. c->count = c->count % 1024 + 8;
  494. }
  495. int gost_imit_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  496. {
  497. struct ossl_gost_imit_ctx *c = ctx->md_data;
  498. const unsigned char *p = data;
  499. size_t bytes = count, i;
  500. if (!(c->key_set)) {
  501. GOSTerr(GOST_F_GOST_IMIT_UPDATE, GOST_R_MAC_KEY_NOT_SET);
  502. return 0;
  503. }
  504. if (c->bytes_left) {
  505. for (i = c->bytes_left; i < 8 && bytes > 0; bytes--, i++, p++) {
  506. c->partial_block[i] = *p;
  507. }
  508. if (i == 8) {
  509. mac_block_mesh(c, c->partial_block);
  510. } else {
  511. c->bytes_left = i;
  512. return 1;
  513. }
  514. }
  515. while (bytes > 8) {
  516. mac_block_mesh(c, p);
  517. p += 8;
  518. bytes -= 8;
  519. }
  520. if (bytes > 0) {
  521. memcpy(c->partial_block, p, bytes);
  522. }
  523. c->bytes_left = bytes;
  524. return 1;
  525. }
  526. int gost_imit_final(EVP_MD_CTX *ctx, unsigned char *md)
  527. {
  528. struct ossl_gost_imit_ctx *c = ctx->md_data;
  529. if (!c->key_set) {
  530. GOSTerr(GOST_F_GOST_IMIT_FINAL, GOST_R_MAC_KEY_NOT_SET);
  531. return 0;
  532. }
  533. if (c->count == 0 && c->bytes_left) {
  534. unsigned char buffer[8];
  535. memset(buffer, 0, 8);
  536. gost_imit_update(ctx, buffer, 8);
  537. }
  538. if (c->bytes_left) {
  539. int i;
  540. for (i = c->bytes_left; i < 8; i++) {
  541. c->partial_block[i] = 0;
  542. }
  543. mac_block_mesh(c, c->partial_block);
  544. }
  545. get_mac(c->buffer, 32, md);
  546. return 1;
  547. }
  548. int gost_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr)
  549. {
  550. switch (type) {
  551. case EVP_MD_CTRL_KEY_LEN:
  552. *((unsigned int *)(ptr)) = 32;
  553. return 1;
  554. case EVP_MD_CTRL_SET_KEY:
  555. {
  556. if (arg != 32) {
  557. GOSTerr(GOST_F_GOST_IMIT_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
  558. return 0;
  559. }
  560. gost_key(&(((struct ossl_gost_imit_ctx *)(ctx->md_data))->cctx),
  561. ptr);
  562. ((struct ossl_gost_imit_ctx *)(ctx->md_data))->key_set = 1;
  563. return 1;
  564. }
  565. default:
  566. return 0;
  567. }
  568. }
  569. int gost_imit_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from)
  570. {
  571. memcpy(to->md_data, from->md_data, sizeof(struct ossl_gost_imit_ctx));
  572. return 1;
  573. }
  574. /* Clean up imit ctx */
  575. int gost_imit_cleanup(EVP_MD_CTX *ctx)
  576. {
  577. memset(ctx->md_data, 0, sizeof(struct ossl_gost_imit_ctx));
  578. return 1;
  579. }