lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <openssl/crypto.h> |
| 14 | #include <openssl/des.h> |
| 15 | #include <openssl/mdc2.h> |
| 16 | |
| 17 | #undef c2l |
| 18 | #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \ |
| 19 | l|=((DES_LONG)(*((c)++)))<< 8L, \ |
| 20 | l|=((DES_LONG)(*((c)++)))<<16L, \ |
| 21 | l|=((DES_LONG)(*((c)++)))<<24L) |
| 22 | |
| 23 | #undef l2c |
| 24 | #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ |
| 25 | *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ |
| 26 | *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ |
| 27 | *((c)++)=(unsigned char)(((l)>>24L)&0xff)) |
| 28 | |
| 29 | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len); |
| 30 | int MDC2_Init(MDC2_CTX *c) |
| 31 | { |
| 32 | c->num = 0; |
| 33 | c->pad_type = 1; |
| 34 | memset(&(c->h[0]), 0x52, MDC2_BLOCK); |
| 35 | memset(&(c->hh[0]), 0x25, MDC2_BLOCK); |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | int MDC2_Update(MDC2_CTX *c, const unsigned char *in, size_t len) |
| 40 | { |
| 41 | size_t i, j; |
| 42 | |
| 43 | i = c->num; |
| 44 | if (i != 0) { |
| 45 | if (len < MDC2_BLOCK - i) { |
| 46 | /* partial block */ |
| 47 | memcpy(&(c->data[i]), in, len); |
| 48 | c->num += (int)len; |
| 49 | return 1; |
| 50 | } else { |
| 51 | /* filled one */ |
| 52 | j = MDC2_BLOCK - i; |
| 53 | memcpy(&(c->data[i]), in, j); |
| 54 | len -= j; |
| 55 | in += j; |
| 56 | c->num = 0; |
| 57 | mdc2_body(c, &(c->data[0]), MDC2_BLOCK); |
| 58 | } |
| 59 | } |
| 60 | i = len & ~((size_t)MDC2_BLOCK - 1); |
| 61 | if (i > 0) |
| 62 | mdc2_body(c, in, i); |
| 63 | j = len - i; |
| 64 | if (j > 0) { |
| 65 | memcpy(&(c->data[0]), &(in[i]), j); |
| 66 | c->num = (int)j; |
| 67 | } |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | static void mdc2_body(MDC2_CTX *c, const unsigned char *in, size_t len) |
| 72 | { |
| 73 | register DES_LONG tin0, tin1; |
| 74 | register DES_LONG ttin0, ttin1; |
| 75 | DES_LONG d[2], dd[2]; |
| 76 | DES_key_schedule k; |
| 77 | unsigned char *p; |
| 78 | size_t i; |
| 79 | |
| 80 | for (i = 0; i < len; i += 8) { |
| 81 | c2l(in, tin0); |
| 82 | d[0] = dd[0] = tin0; |
| 83 | c2l(in, tin1); |
| 84 | d[1] = dd[1] = tin1; |
| 85 | c->h[0] = (c->h[0] & 0x9f) | 0x40; |
| 86 | c->hh[0] = (c->hh[0] & 0x9f) | 0x20; |
| 87 | |
| 88 | DES_set_odd_parity(&c->h); |
| 89 | DES_set_key_unchecked(&c->h, &k); |
| 90 | DES_encrypt1(d, &k, 1); |
| 91 | |
| 92 | DES_set_odd_parity(&c->hh); |
| 93 | DES_set_key_unchecked(&c->hh, &k); |
| 94 | DES_encrypt1(dd, &k, 1); |
| 95 | |
| 96 | ttin0 = tin0 ^ dd[0]; |
| 97 | ttin1 = tin1 ^ dd[1]; |
| 98 | tin0 ^= d[0]; |
| 99 | tin1 ^= d[1]; |
| 100 | |
| 101 | p = c->h; |
| 102 | l2c(tin0, p); |
| 103 | l2c(ttin1, p); |
| 104 | p = c->hh; |
| 105 | l2c(ttin0, p); |
| 106 | l2c(tin1, p); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | int MDC2_Final(unsigned char *md, MDC2_CTX *c) |
| 111 | { |
| 112 | unsigned int i; |
| 113 | int j; |
| 114 | |
| 115 | i = c->num; |
| 116 | j = c->pad_type; |
| 117 | if ((i > 0) || (j == 2)) { |
| 118 | if (j == 2) |
| 119 | c->data[i++] = 0x80; |
| 120 | memset(&(c->data[i]), 0, MDC2_BLOCK - i); |
| 121 | mdc2_body(c, c->data, MDC2_BLOCK); |
| 122 | } |
| 123 | memcpy(md, (char *)c->h, MDC2_BLOCK); |
| 124 | memcpy(&(md[MDC2_BLOCK]), (char *)c->hh, MDC2_BLOCK); |
| 125 | return 1; |
| 126 | } |