- case EVP_CTRL_PROCESS_UNPROTECTED:
- {
- gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
- ASN1_OBJECT *cmsmacobj = NULL;
- if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
- return -1;
- cmsmacobj = OBJ_txt2obj(OID_GOST_CMS_MAC, 1);
- if (cmsmacobj == NULL) {
- GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
- return -1;
- }
- if (arg == 0) /*Decrypting*/ {
- STACK_OF(X509_ATTRIBUTE) *x = ptr;
- ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(x,
- cmsmacobj, -3, V_ASN1_OCTET_STRING);
- ASN1_OBJECT_free(cmsmacobj);
-
- if (ptr == NULL || osExpectedMac ==NULL || osExpectedMac->length != KUZNYECHIK_MAC_MAX_SIZE)
- return -1;
-
- memcpy(c->tag, osExpectedMac->data, osExpectedMac->length);
- return 1;
- } else {
- STACK_OF(X509_ATTRIBUTE) *x = ptr;
- return (X509at_add1_attr_by_OBJ(&x, cmsmacobj,
- V_ASN1_OCTET_STRING, c->tag, KUZNYECHIK_MAC_MAX_SIZE) == NULL) ? -1 : 1;
- }
- }
- return 1;
- case EVP_CTRL_COPY: {
- EVP_CIPHER_CTX *out = ptr;
-
- gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
- gost_grasshopper_cipher_ctx_ctr *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
-
- if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
- return -1;
-
- if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
- out_cctx->omac_ctx = EVP_MD_CTX_new();
- if (out_cctx->omac_ctx == NULL) {
- GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
- return -1;
- }
- }
- return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
- }
- default:
- GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
- GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
- return -1;
- }
- return 1;
-}
-
-GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
- cipher_type, int
- block_size)
-{
- return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
- GRASSHOPPER_KEY_SIZE /* key_size */ );
-}
-
-const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
- int iv_size, bool padding, int extra_flags)
-{
- unsigned long flags = (unsigned long)(mode
- | ((!padding) ? EVP_CIPH_NO_PADDING : 0)
- | ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0)
- | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT
- | extra_flags);
-
- return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
- && EVP_CIPHER_meth_set_flags(cipher, flags)
- && EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup)
- && EVP_CIPHER_meth_set_set_asn1_params(cipher,
- gost_grasshopper_set_asn1_parameters)
- && EVP_CIPHER_meth_set_get_asn1_params(cipher,
- gost_grasshopper_get_asn1_parameters)
- && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
- && EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
-}