Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/evp/e_gost2814789.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: e_gost2814789.c,v 1.5 2021/12/12 21:30:13 tb Exp $ */
2
/*
3
 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4
 * Copyright (c) 2005-2006 Cryptocom LTD
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 *
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * 3. All advertising materials mentioning features or use of this
19
 *    software must display the following acknowledgment:
20
 *    "This product includes software developed by the OpenSSL Project
21
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22
 *
23
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24
 *    endorse or promote products derived from this software without
25
 *    prior written permission. For written permission, please contact
26
 *    openssl-core@openssl.org.
27
 *
28
 * 5. Products derived from this software may not be called "OpenSSL"
29
 *    nor may "OpenSSL" appear in their names without prior written
30
 *    permission of the OpenSSL Project.
31
 *
32
 * 6. Redistributions of any form whatsoever must retain the following
33
 *    acknowledgment:
34
 *    "This product includes software developed by the OpenSSL Project
35
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36
 *
37
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48
 * OF THE POSSIBILITY OF SUCH DAMAGE.
49
 * ====================================================================
50
 */
51
52
#include <string.h>
53
54
#include <openssl/opensslconf.h>
55
56
#ifndef OPENSSL_NO_GOST
57
#include <openssl/evp.h>
58
#include <openssl/err.h>
59
#include <openssl/gost.h>
60
61
#include "evp_locl.h"
62
63
typedef struct {
64
  GOST2814789_KEY ks;
65
  int param_nid;
66
} EVP_GOST2814789_CTX;
67
68
static int
69
gost2814789_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
70
0
{
71
0
  EVP_GOST2814789_CTX *c = ctx->cipher_data;
72
73
0
  switch (type) {
74
0
  case EVP_CTRL_PBE_PRF_NID:
75
0
    if (ptr != NULL) {
76
0
      *((int *)ptr) = NID_id_HMACGostR3411_94;
77
0
      return 1;
78
0
    } else {
79
0
      return 0;
80
0
    }
81
0
  case EVP_CTRL_INIT:
82
    /* Default value to have any s-box set at all */
83
0
    c->param_nid = NID_id_Gost28147_89_CryptoPro_A_ParamSet;
84
0
    return Gost2814789_set_sbox(&c->ks, c->param_nid);
85
0
  case EVP_CTRL_GOST_SET_SBOX:
86
0
    return Gost2814789_set_sbox(&c->ks, arg);
87
0
  default:
88
0
    return -1;
89
0
  }
90
0
}
91
92
static int
93
gost2814789_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
94
    const unsigned char *iv, int enc)
95
0
{
96
0
  EVP_GOST2814789_CTX *c = ctx->cipher_data;
97
98
0
  return Gost2814789_set_key(&c->ks, key, ctx->key_len * 8);
99
0
}
100
101
int
102
gost2814789_set_asn1_params(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
103
0
{
104
0
  int len = 0;
105
0
  unsigned char *buf = NULL;
106
0
  unsigned char *p = NULL;
107
0
  EVP_GOST2814789_CTX *c = ctx->cipher_data;
108
0
  ASN1_OCTET_STRING *os = NULL;
109
0
  GOST_CIPHER_PARAMS *gcp = GOST_CIPHER_PARAMS_new();
110
111
0
  if (gcp == NULL) {
112
0
    GOSTerror(ERR_R_MALLOC_FAILURE);
113
0
    return 0;
114
0
  }
115
0
  if (ASN1_OCTET_STRING_set(gcp->iv, ctx->iv, ctx->cipher->iv_len) == 0) {
116
0
    GOST_CIPHER_PARAMS_free(gcp);
117
0
    GOSTerror(ERR_R_ASN1_LIB);
118
0
    return 0;
119
0
  }
120
0
  ASN1_OBJECT_free(gcp->enc_param_set);
121
0
  gcp->enc_param_set = OBJ_nid2obj(c->param_nid);
122
123
0
  len = i2d_GOST_CIPHER_PARAMS(gcp, NULL);
124
0
  p = buf = malloc(len);
125
0
  if (buf == NULL) {
126
0
    GOST_CIPHER_PARAMS_free(gcp);
127
0
    GOSTerror(ERR_R_MALLOC_FAILURE);
128
0
    return 0;
129
0
  }
130
0
  i2d_GOST_CIPHER_PARAMS(gcp, &p);
131
0
  GOST_CIPHER_PARAMS_free(gcp);
132
133
0
  os = ASN1_OCTET_STRING_new();
134
0
  if (os == NULL) {
135
0
    free(buf);
136
0
    GOSTerror(ERR_R_MALLOC_FAILURE);
137
0
    return 0;
138
0
  }
139
0
  if (ASN1_OCTET_STRING_set(os, buf, len) == 0) {
140
0
    ASN1_OCTET_STRING_free(os);
141
0
    free(buf);
142
0
    GOSTerror(ERR_R_ASN1_LIB);
143
0
    return 0;
144
0
  }
145
0
  free(buf);
146
147
0
  ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
148
0
  return 1;
149
0
}
150
151
int
152
gost2814789_get_asn1_params(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
153
0
{
154
0
  int ret = -1;
155
0
  int len;
156
0
  GOST_CIPHER_PARAMS *gcp = NULL;
157
0
  EVP_GOST2814789_CTX *c = ctx->cipher_data;
158
0
  unsigned char *p;
159
160
0
  if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE)
161
0
    return ret;
162
163
0
  p = params->value.sequence->data;
164
165
0
  gcp = d2i_GOST_CIPHER_PARAMS(NULL, (const unsigned char **)&p,
166
0
      params->value.sequence->length);
167
168
0
  len = gcp->iv->length;
169
0
  if (len != ctx->cipher->iv_len) {
170
0
    GOST_CIPHER_PARAMS_free(gcp);
171
0
    GOSTerror(GOST_R_INVALID_IV_LENGTH);
172
0
    return -1;
173
0
  }
174
175
0
  if (!Gost2814789_set_sbox(&c->ks, OBJ_obj2nid(gcp->enc_param_set))) {
176
0
    GOST_CIPHER_PARAMS_free(gcp);
177
0
    return -1;
178
0
  }
179
0
  c->param_nid = OBJ_obj2nid(gcp->enc_param_set);
180
181
0
  memcpy(ctx->oiv, gcp->iv->data, len);
182
0
  memcpy(ctx->iv, gcp->iv->data, len);
183
184
0
  GOST_CIPHER_PARAMS_free(gcp);
185
186
0
  return 1;
187
0
}
188
189
BLOCK_CIPHER_func_ecb(gost2814789, Gost2814789, EVP_GOST2814789_CTX, ks)
190
BLOCK_CIPHER_func_cfb(gost2814789, Gost2814789, 64, EVP_GOST2814789_CTX, ks)
191
192
static int
193
gost2814789_cnt_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
194
    const unsigned char *in, size_t inl)
195
0
{
196
0
  EVP_GOST2814789_CTX *c = ctx->cipher_data;
197
198
0
  while (inl >= EVP_MAXCHUNK) {
199
0
    Gost2814789_cnt_encrypt(in, out, (long)EVP_MAXCHUNK, &c->ks,
200
0
        ctx->iv, ctx->buf, &ctx->num);
201
0
    inl -= EVP_MAXCHUNK;
202
0
    in += EVP_MAXCHUNK;
203
0
    out += EVP_MAXCHUNK;
204
0
  }
205
206
0
  if (inl)
207
0
    Gost2814789_cnt_encrypt(in, out, inl, &c->ks, ctx->iv, ctx->buf,
208
0
        &ctx->num);
209
0
  return 1;
210
0
}
211
212
/* gost89 is CFB-64 */
213
#define NID_gost89_cfb64 NID_id_Gost28147_89
214
215
BLOCK_CIPHER_def_ecb(gost2814789, EVP_GOST2814789_CTX, NID_gost89, 8, 32,
216
         EVP_CIPH_NO_PADDING | EVP_CIPH_CTRL_INIT,
217
         gost2814789_init_key, NULL, gost2814789_set_asn1_params,
218
         gost2814789_get_asn1_params, gost2814789_ctl)
219
BLOCK_CIPHER_def_cfb(gost2814789, EVP_GOST2814789_CTX, NID_gost89, 32, 8, 64,
220
         EVP_CIPH_NO_PADDING | EVP_CIPH_CTRL_INIT,
221
         gost2814789_init_key, NULL, gost2814789_set_asn1_params,
222
         gost2814789_get_asn1_params, gost2814789_ctl)
223
BLOCK_CIPHER_def1(gost2814789, cnt, cnt, OFB, EVP_GOST2814789_CTX, NID_gost89,
224
      1, 32, 8, EVP_CIPH_NO_PADDING | EVP_CIPH_CTRL_INIT,
225
      gost2814789_init_key, NULL, gost2814789_set_asn1_params,
226
      gost2814789_get_asn1_params, gost2814789_ctl)
227
#endif