summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/gost/gostr341194.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/gost/gostr341194.c')
-rw-r--r--src/lib/libcrypto/gost/gostr341194.c43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/lib/libcrypto/gost/gostr341194.c b/src/lib/libcrypto/gost/gostr341194.c
index 706bf3d431..3151d9fe12 100644
--- a/src/lib/libcrypto/gost/gostr341194.c
+++ b/src/lib/libcrypto/gost/gostr341194.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gostr341194.c,v 1.1 2014/11/09 19:17:13 miod Exp $ */ 1/* $OpenBSD: gostr341194.c,v 1.2 2014/11/09 23:06:52 miod Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> 3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4 * Copyright (c) 2005-2006 Cryptocom LTD 4 * Copyright (c) 2005-2006 Cryptocom LTD
@@ -62,17 +62,19 @@
62 62
63/* Following functions are various bit meshing routines used in 63/* Following functions are various bit meshing routines used in
64 * GOST R 34.11-94 algorithms */ 64 * GOST R 34.11-94 algorithms */
65static void swap_bytes(unsigned char *w, unsigned char *k) 65static void
66swap_bytes(unsigned char *w, unsigned char *k)
66{ 67{
67 int i, j; 68 int i, j;
69
68 for (i = 0; i < 4; i++) 70 for (i = 0; i < 4; i++)
69 for (j = 0; j < 8; j++) 71 for (j = 0; j < 8; j++)
70 k[i + 4 * j] = w[8 * i + j]; 72 k[i + 4 * j] = w[8 * i + j];
71
72} 73}
73 74
74/* was A_A */ 75/* was A_A */
75static void circle_xor8(const unsigned char *w, unsigned char *k) 76static void
77circle_xor8(const unsigned char *w, unsigned char *k)
76{ 78{
77 unsigned char buf[8]; 79 unsigned char buf[8];
78 int i; 80 int i;
@@ -84,9 +86,11 @@ static void circle_xor8(const unsigned char *w, unsigned char *k)
84} 86}
85 87
86/* was R_R */ 88/* was R_R */
87static void transform_3(unsigned char *data) 89static void
90transform_3(unsigned char *data)
88{ 91{
89 unsigned short int acc; 92 unsigned short int acc;
93
90 acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) | 94 acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) |
91 ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8); 95 ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8);
92 memmove(data, data + 2, 30); 96 memmove(data, data + 2, 30);
@@ -95,7 +99,8 @@ static void transform_3(unsigned char *data)
95} 99}
96 100
97/* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/ 101/* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
98static int add_blocks(int n, unsigned char *left, const unsigned char *right) 102static int
103add_blocks(int n, unsigned char *left, const unsigned char *right)
99{ 104{
100 int i; 105 int i;
101 int carry = 0; 106 int carry = 0;
@@ -110,10 +115,12 @@ static int add_blocks(int n, unsigned char *left, const unsigned char *right)
110} 115}
111 116
112/* Xor two sequences of bytes */ 117/* Xor two sequences of bytes */
113static void xor_blocks(unsigned char *result, const unsigned char *a, 118static void
114 const unsigned char *b, size_t len) 119xor_blocks(unsigned char *result, const unsigned char *a,
120 const unsigned char *b, size_t len)
115{ 121{
116 size_t i; 122 size_t i;
123
117 for (i = 0; i < len; i++) 124 for (i = 0; i < len; i++)
118 result[i] = a[i] ^ b[i]; 125 result[i] = a[i] ^ b[i];
119} 126}
@@ -122,7 +129,8 @@ static void xor_blocks(unsigned char *result, const unsigned char *a,
122 * Calculate H(i+1) = Hash(Hi,Mi) 129 * Calculate H(i+1) = Hash(Hi,Mi)
123 * Where H and M are 32 bytes long 130 * Where H and M are 32 bytes long
124 */ 131 */
125static int hash_step(GOSTR341194_CTX *c, unsigned char *H, const unsigned char *M) 132static int
133hash_step(GOSTR341194_CTX *c, unsigned char *H, const unsigned char *M)
126{ 134{
127 unsigned char U[32], W[32], V[32], S[32], Key[32]; 135 unsigned char U[32], W[32], V[32], S[32], Key[32];
128 int i; 136 int i;
@@ -191,15 +199,18 @@ static int hash_step(GOSTR341194_CTX *c, unsigned char *H, const unsigned char *
191 return 1; 199 return 1;
192} 200}
193 201
194int GOSTR341194_Init(GOSTR341194_CTX *c, int nid) 202int
203GOSTR341194_Init(GOSTR341194_CTX *c, int nid)
195{ 204{
196 memset (c,0,sizeof(*c)); 205 memset(c, 0, sizeof(*c));
197 return Gost2814789_set_sbox(&c->cipher, nid); 206 return Gost2814789_set_sbox(&c->cipher, nid);
198} 207}
199 208
200static void GOSTR341194_block_data_order(GOSTR341194_CTX *ctx, const void *p, size_t num) 209static void
210GOSTR341194_block_data_order(GOSTR341194_CTX *ctx, const void *p, size_t num)
201{ 211{
202 int i; 212 int i;
213
203 for (i = 0; i < num; i++) { 214 for (i = 0; i < num; i++) {
204 hash_step(ctx, ctx->H, p); 215 hash_step(ctx, ctx->H, p);
205 add_blocks(32, ctx->S, p); 216 add_blocks(32, ctx->S, p);
@@ -219,7 +230,8 @@ static void GOSTR341194_block_data_order(GOSTR341194_CTX *ctx, const void *p, si
219 230
220#include "md32_common.h" 231#include "md32_common.h"
221 232
222int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX * c) 233int
234GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX * c)
223{ 235{
224 unsigned char *p = (unsigned char *)c->data; 236 unsigned char *p = (unsigned char *)c->data;
225 unsigned char T[32]; 237 unsigned char T[32];
@@ -233,7 +245,7 @@ int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX * c)
233 p = T; 245 p = T;
234 HOST_l2c(c->Nl, p); 246 HOST_l2c(c->Nl, p);
235 HOST_l2c(c->Nh, p); 247 HOST_l2c(c->Nh, p);
236 memset(p, 0, 32-8); 248 memset(p, 0, 32 - 8);
237 hash_step(c, c->H, T); 249 hash_step(c, c->H, T);
238 hash_step(c, c->H, c->S); 250 hash_step(c, c->H, c->S);
239 251
@@ -242,7 +254,8 @@ int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX * c)
242 return 1; 254 return 1;
243} 255}
244 256
245unsigned char *GOSTR341194(const unsigned char *d, size_t n, unsigned char *md, int nid) 257unsigned char *
258GOSTR341194(const unsigned char *d, size_t n, unsigned char *md, int nid)
246{ 259{
247 GOSTR341194_CTX c; 260 GOSTR341194_CTX c;
248 static unsigned char m[GOSTR341194_LENGTH]; 261 static unsigned char m[GOSTR341194_LENGTH];