1 /* gost28147.c - GOST 28147-89 implementation for Libgcrypt
2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* GOST 28147-89 defines several modes of encryption:
21 * - ECB which should be used only for key transfer
23 * - OFB-like mode with additional transformation on keystream
24 * RFC 5830 names this 'counter encryption' mode
25 * Original GOST text uses the term 'gammirovanie'
28 * This implementation handles ECB and CFB modes via usual libgcrypt handling.
29 * OFB-like and MAC modes are unsupported.
41 static gcry_err_code_t
42 gost_setkey (void *c, const byte *key, unsigned keylen)
45 GOST28147_context *ctx = c;
47 if (keylen != 256 / 8)
48 return GPG_ERR_INV_KEYLEN;
51 ctx->sbox = sbox_test_3411;
53 for (i = 0; i < 8; i++)
55 ctx->key[i] = buf_get_le32(&key[4*i]);
57 return GPG_ERR_NO_ERROR;
61 gost_val (GOST28147_context *ctx, u32 cm1, int subkey)
63 cm1 += ctx->key[subkey];
64 cm1 = ctx->sbox[0*256 + ((cm1 >> 0) & 0xff)] |
65 ctx->sbox[1*256 + ((cm1 >> 8) & 0xff)] |
66 ctx->sbox[2*256 + ((cm1 >> 16) & 0xff)] |
67 ctx->sbox[3*256 + ((cm1 >> 24) & 0xff)];
72 _gost_encrypt_data (void *c, u32 *o1, u32 *o2, u32 n1, u32 n2)
74 GOST28147_context *ctx = c;
76 n2 ^= gost_val (ctx, n1, 0); n1 ^= gost_val (ctx, n2, 1);
77 n2 ^= gost_val (ctx, n1, 2); n1 ^= gost_val (ctx, n2, 3);
78 n2 ^= gost_val (ctx, n1, 4); n1 ^= gost_val (ctx, n2, 5);
79 n2 ^= gost_val (ctx, n1, 6); n1 ^= gost_val (ctx, n2, 7);
81 n2 ^= gost_val (ctx, n1, 0); n1 ^= gost_val (ctx, n2, 1);
82 n2 ^= gost_val (ctx, n1, 2); n1 ^= gost_val (ctx, n2, 3);
83 n2 ^= gost_val (ctx, n1, 4); n1 ^= gost_val (ctx, n2, 5);
84 n2 ^= gost_val (ctx, n1, 6); n1 ^= gost_val (ctx, n2, 7);
86 n2 ^= gost_val (ctx, n1, 0); n1 ^= gost_val (ctx, n2, 1);
87 n2 ^= gost_val (ctx, n1, 2); n1 ^= gost_val (ctx, n2, 3);
88 n2 ^= gost_val (ctx, n1, 4); n1 ^= gost_val (ctx, n2, 5);
89 n2 ^= gost_val (ctx, n1, 6); n1 ^= gost_val (ctx, n2, 7);
91 n2 ^= gost_val (ctx, n1, 7); n1 ^= gost_val (ctx, n2, 6);
92 n2 ^= gost_val (ctx, n1, 5); n1 ^= gost_val (ctx, n2, 4);
93 n2 ^= gost_val (ctx, n1, 3); n1 ^= gost_val (ctx, n2, 2);
94 n2 ^= gost_val (ctx, n1, 1); n1 ^= gost_val (ctx, n2, 0);
99 return /* burn_stack */ 4*sizeof(void*) /* func call */ +
100 3*sizeof(void*) /* stack */ +
101 4*sizeof(void*) /* gost_val call */;
105 gost_encrypt_block (void *c, byte *outbuf, const byte *inbuf)
107 GOST28147_context *ctx = c;
111 n1 = buf_get_le32 (inbuf);
112 n2 = buf_get_le32 (inbuf+4);
114 burn = _gost_encrypt_data(ctx, &n1, &n2, n1, n2);
116 buf_put_le32 (outbuf+0, n1);
117 buf_put_le32 (outbuf+4, n2);
119 return /* burn_stack */ burn + 6*sizeof(void*) /* func call */;
122 unsigned int _gcry_gost_enc_data (GOST28147_context *c, const u32 *key,
123 u32 *o1, u32 *o2, u32 n1, u32 n2, int cryptopro)
126 c->sbox = sbox_CryptoPro_3411;
128 c->sbox = sbox_test_3411;
129 memcpy (c->key, key, 8*4);
130 return _gost_encrypt_data (c, o1, o2, n1, n2) + 7 * sizeof(void *);
134 gost_decrypt_block (void *c, byte *outbuf, const byte *inbuf)
136 GOST28147_context *ctx = c;
139 n1 = buf_get_le32 (inbuf);
140 n2 = buf_get_le32 (inbuf+4);
142 n2 ^= gost_val (ctx, n1, 0); n1 ^= gost_val (ctx, n2, 1);
143 n2 ^= gost_val (ctx, n1, 2); n1 ^= gost_val (ctx, n2, 3);
144 n2 ^= gost_val (ctx, n1, 4); n1 ^= gost_val (ctx, n2, 5);
145 n2 ^= gost_val (ctx, n1, 6); n1 ^= gost_val (ctx, n2, 7);
147 n2 ^= gost_val (ctx, n1, 7); n1 ^= gost_val (ctx, n2, 6);
148 n2 ^= gost_val (ctx, n1, 5); n1 ^= gost_val (ctx, n2, 4);
149 n2 ^= gost_val (ctx, n1, 3); n1 ^= gost_val (ctx, n2, 2);
150 n2 ^= gost_val (ctx, n1, 1); n1 ^= gost_val (ctx, n2, 0);
152 n2 ^= gost_val (ctx, n1, 7); n1 ^= gost_val (ctx, n2, 6);
153 n2 ^= gost_val (ctx, n1, 5); n1 ^= gost_val (ctx, n2, 4);
154 n2 ^= gost_val (ctx, n1, 3); n1 ^= gost_val (ctx, n2, 2);
155 n2 ^= gost_val (ctx, n1, 1); n1 ^= gost_val (ctx, n2, 0);
157 n2 ^= gost_val (ctx, n1, 7); n1 ^= gost_val (ctx, n2, 6);
158 n2 ^= gost_val (ctx, n1, 5); n1 ^= gost_val (ctx, n2, 4);
159 n2 ^= gost_val (ctx, n1, 3); n1 ^= gost_val (ctx, n2, 2);
160 n2 ^= gost_val (ctx, n1, 1); n1 ^= gost_val (ctx, n2, 0);
162 buf_put_le32 (outbuf+0, n2);
163 buf_put_le32 (outbuf+4, n1);
165 return /* burn_stack */ 4*sizeof(void*) /* func call */ +
166 3*sizeof(void*) /* stack */ +
167 4*sizeof(void*) /* gost_val call */;
170 static gpg_err_code_t
171 gost_set_sbox (GOST28147_context *ctx, const char *oid)
175 for (i = 0; gost_oid_map[i].oid; i++)
177 if (!strcmp(gost_oid_map[i].oid, oid))
179 ctx->sbox = gost_oid_map[i].sbox;
183 return GPG_ERR_VALUE_NOT_FOUND;
186 static gpg_err_code_t
187 gost_set_extra_info (void *c, int what, const void *buffer, size_t buflen)
189 GOST28147_context *ctx = c;
190 gpg_err_code_t ec = 0;
197 case GCRYCTL_SET_SBOX:
198 ec = gost_set_sbox (ctx, buffer);
208 static gcry_cipher_oid_spec_t oids_gost28147[] =
210 /* { "1.2.643.2.2.31.0", GCRY_CIPHER_MODE_CNTGOST }, */
211 { "1.2.643.2.2.31.1", GCRY_CIPHER_MODE_CFB },
212 { "1.2.643.2.2.31.2", GCRY_CIPHER_MODE_CFB },
213 { "1.2.643.2.2.31.3", GCRY_CIPHER_MODE_CFB },
214 { "1.2.643.2.2.31.4", GCRY_CIPHER_MODE_CFB },
218 gcry_cipher_spec_t _gcry_cipher_spec_gost28147 =
220 GCRY_CIPHER_GOST28147, {0, 0},
221 "GOST28147", NULL, oids_gost28147, 8, 256,
222 sizeof (GOST28147_context),
226 NULL, NULL, NULL, gost_set_extra_info,