diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/sm3/sm3.c | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/src/lib/libcrypto/sm3/sm3.c b/src/lib/libcrypto/sm3/sm3.c index 06b5f167f0..6406454568 100644 --- a/src/lib/libcrypto/sm3/sm3.c +++ b/src/lib/libcrypto/sm3/sm3.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: sm3.c,v 1.9 2024/03/28 08:13:11 jsing Exp $ */ | 1 | /* $OpenBSD: sm3.c,v 1.10 2024/03/28 08:26:42 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2018, Ribose Inc | 3 | * Copyright (c) 2018, Ribose Inc |
| 4 | * | 4 | * |
| @@ -276,47 +276,47 @@ SM3_Init(SM3_CTX *c) | |||
| 276 | LCRYPTO_ALIAS(SM3_Init); | 276 | LCRYPTO_ALIAS(SM3_Init); |
| 277 | 277 | ||
| 278 | int | 278 | int |
| 279 | HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) | 279 | SM3_Update(SM3_CTX *c, const void *data_, size_t len) |
| 280 | { | 280 | { |
| 281 | const unsigned char *data = data_; | 281 | const unsigned char *data = data_; |
| 282 | unsigned char *p; | 282 | unsigned char *p; |
| 283 | HASH_LONG l; | 283 | SM3_WORD l; |
| 284 | size_t n; | 284 | size_t n; |
| 285 | 285 | ||
| 286 | if (len == 0) | 286 | if (len == 0) |
| 287 | return 1; | 287 | return 1; |
| 288 | 288 | ||
| 289 | l = (c->Nl + (((HASH_LONG)len) << 3))&0xffffffffUL; | 289 | l = (c->Nl + (((SM3_WORD)len) << 3))&0xffffffffUL; |
| 290 | /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to | 290 | /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to |
| 291 | * Wei Dai <weidai@eskimo.com> for pointing it out. */ | 291 | * Wei Dai <weidai@eskimo.com> for pointing it out. */ |
| 292 | if (l < c->Nl) /* overflow */ | 292 | if (l < c->Nl) /* overflow */ |
| 293 | c->Nh++; | 293 | c->Nh++; |
| 294 | c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */ | 294 | c->Nh+=(SM3_WORD)(len>>29); /* might cause compiler warning on 16-bit */ |
| 295 | c->Nl = l; | 295 | c->Nl = l; |
| 296 | 296 | ||
| 297 | n = c->num; | 297 | n = c->num; |
| 298 | if (n != 0) { | 298 | if (n != 0) { |
| 299 | p = (unsigned char *)c->data; | 299 | p = (unsigned char *)c->data; |
| 300 | 300 | ||
| 301 | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 301 | if (len >= SM3_CBLOCK || len + n >= SM3_CBLOCK) { |
| 302 | memcpy (p + n, data, HASH_CBLOCK - n); | 302 | memcpy(p + n, data, SM3_CBLOCK - n); |
| 303 | HASH_BLOCK_DATA_ORDER (c, p, 1); | 303 | SM3_block_data_order(c, p, 1); |
| 304 | n = HASH_CBLOCK - n; | 304 | n = SM3_CBLOCK - n; |
| 305 | data += n; | 305 | data += n; |
| 306 | len -= n; | 306 | len -= n; |
| 307 | c->num = 0; | 307 | c->num = 0; |
| 308 | memset (p,0,HASH_CBLOCK); /* keep it zeroed */ | 308 | memset(p, 0, SM3_CBLOCK); /* keep it zeroed */ |
| 309 | } else { | 309 | } else { |
| 310 | memcpy (p + n, data, len); | 310 | memcpy(p + n, data, len); |
| 311 | c->num += (unsigned int)len; | 311 | c->num += (unsigned int)len; |
| 312 | return 1; | 312 | return 1; |
| 313 | } | 313 | } |
| 314 | } | 314 | } |
| 315 | 315 | ||
| 316 | n = len/HASH_CBLOCK; | 316 | n = len / SM3_CBLOCK; |
| 317 | if (n > 0) { | 317 | if (n > 0) { |
| 318 | HASH_BLOCK_DATA_ORDER (c, data, n); | 318 | SM3_block_data_order(c, data, n); |
| 319 | n *= HASH_CBLOCK; | 319 | n *= SM3_CBLOCK; |
| 320 | data += n; | 320 | data += n; |
| 321 | len -= n; | 321 | len -= n; |
| 322 | } | 322 | } |
| @@ -324,18 +324,20 @@ HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) | |||
| 324 | if (len != 0) { | 324 | if (len != 0) { |
| 325 | p = (unsigned char *)c->data; | 325 | p = (unsigned char *)c->data; |
| 326 | c->num = (unsigned int)len; | 326 | c->num = (unsigned int)len; |
| 327 | memcpy (p, data, len); | 327 | memcpy(p, data, len); |
| 328 | } | 328 | } |
| 329 | return 1; | 329 | return 1; |
| 330 | } | 330 | } |
| 331 | LCRYPTO_ALIAS(SM3_Update); | 331 | LCRYPTO_ALIAS(SM3_Update); |
| 332 | 332 | ||
| 333 | void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data) | 333 | void |
| 334 | SM3_Transform(SM3_CTX *c, const unsigned char *data) | ||
| 334 | { | 335 | { |
| 335 | HASH_BLOCK_DATA_ORDER (c, data, 1); | 336 | SM3_block_data_order(c, data, 1); |
| 336 | } | 337 | } |
| 337 | 338 | ||
| 338 | int HASH_FINAL (unsigned char *md, HASH_CTX *c) | 339 | int |
| 340 | SM3_Final(unsigned char *md, SM3_CTX *c) | ||
| 339 | { | 341 | { |
| 340 | unsigned char *p = (unsigned char *)c->data; | 342 | unsigned char *p = (unsigned char *)c->data; |
| 341 | size_t n = c->num; | 343 | size_t n = c->num; |
| @@ -343,14 +345,14 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c) | |||
| 343 | p[n] = 0x80; /* there is always room for one */ | 345 | p[n] = 0x80; /* there is always room for one */ |
| 344 | n++; | 346 | n++; |
| 345 | 347 | ||
| 346 | if (n > (HASH_CBLOCK - 8)) { | 348 | if (n > (SM3_CBLOCK - 8)) { |
| 347 | memset (p + n, 0, HASH_CBLOCK - n); | 349 | memset(p + n, 0, SM3_CBLOCK - n); |
| 348 | n = 0; | 350 | n = 0; |
| 349 | HASH_BLOCK_DATA_ORDER (c, p, 1); | 351 | SM3_block_data_order(c, p, 1); |
| 350 | } | 352 | } |
| 351 | memset (p + n, 0, HASH_CBLOCK - 8 - n); | 353 | memset(p + n, 0, SM3_CBLOCK - 8 - n); |
| 352 | 354 | ||
| 353 | p += HASH_CBLOCK - 8; | 355 | p += SM3_CBLOCK - 8; |
| 354 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 356 | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
| 355 | HOST_l2c(c->Nh, p); | 357 | HOST_l2c(c->Nh, p); |
| 356 | HOST_l2c(c->Nl, p); | 358 | HOST_l2c(c->Nl, p); |
| @@ -358,10 +360,10 @@ int HASH_FINAL (unsigned char *md, HASH_CTX *c) | |||
| 358 | HOST_l2c(c->Nl, p); | 360 | HOST_l2c(c->Nl, p); |
| 359 | HOST_l2c(c->Nh, p); | 361 | HOST_l2c(c->Nh, p); |
| 360 | #endif | 362 | #endif |
| 361 | p -= HASH_CBLOCK; | 363 | p -= SM3_CBLOCK; |
| 362 | HASH_BLOCK_DATA_ORDER (c, p, 1); | 364 | SM3_block_data_order(c, p, 1); |
| 363 | c->num = 0; | 365 | c->num = 0; |
| 364 | memset (p, 0, HASH_CBLOCK); | 366 | memset(p, 0, SM3_CBLOCK); |
| 365 | 367 | ||
| 366 | #ifndef HASH_MAKE_STRING | 368 | #ifndef HASH_MAKE_STRING |
| 367 | #error "HASH_MAKE_STRING must be defined!" | 369 | #error "HASH_MAKE_STRING must be defined!" |
