1*0a6a1f1dSLionel Sambuc /* $NetBSD: n-fold.c,v 1.1.1.2 2014/04/24 12:45:50 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1999 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc * used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc * specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
34ebfedea0SLionel Sambuc
35ebfedea0SLionel Sambuc #include "krb5_locl.h"
36ebfedea0SLionel Sambuc
37ebfedea0SLionel Sambuc static krb5_error_code
rr13(unsigned char * buf,size_t len)38ebfedea0SLionel Sambuc rr13(unsigned char *buf, size_t len)
39ebfedea0SLionel Sambuc {
40ebfedea0SLionel Sambuc unsigned char *tmp;
41ebfedea0SLionel Sambuc int bytes = (len + 7) / 8;
42ebfedea0SLionel Sambuc int i;
43ebfedea0SLionel Sambuc if(len == 0)
44ebfedea0SLionel Sambuc return 0;
45ebfedea0SLionel Sambuc {
46ebfedea0SLionel Sambuc const int bits = 13 % len;
47ebfedea0SLionel Sambuc const int lbit = len % 8;
48ebfedea0SLionel Sambuc
49ebfedea0SLionel Sambuc tmp = malloc(bytes);
50ebfedea0SLionel Sambuc if (tmp == NULL)
51ebfedea0SLionel Sambuc return ENOMEM;
52ebfedea0SLionel Sambuc memcpy(tmp, buf, bytes);
53ebfedea0SLionel Sambuc if(lbit) {
54ebfedea0SLionel Sambuc /* pad final byte with inital bits */
55ebfedea0SLionel Sambuc tmp[bytes - 1] &= 0xff << (8 - lbit);
56ebfedea0SLionel Sambuc for(i = lbit; i < 8; i += len)
57ebfedea0SLionel Sambuc tmp[bytes - 1] |= buf[0] >> i;
58ebfedea0SLionel Sambuc }
59ebfedea0SLionel Sambuc for(i = 0; i < bytes; i++) {
60ebfedea0SLionel Sambuc int bb;
61ebfedea0SLionel Sambuc int b1, s1, b2, s2;
62ebfedea0SLionel Sambuc /* calculate first bit position of this byte */
63ebfedea0SLionel Sambuc bb = 8 * i - bits;
64ebfedea0SLionel Sambuc while(bb < 0)
65ebfedea0SLionel Sambuc bb += len;
66ebfedea0SLionel Sambuc /* byte offset and shift count */
67ebfedea0SLionel Sambuc b1 = bb / 8;
68ebfedea0SLionel Sambuc s1 = bb % 8;
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc if(bb + 8 > bytes * 8)
71ebfedea0SLionel Sambuc /* watch for wraparound */
72ebfedea0SLionel Sambuc s2 = (len + 8 - s1) % 8;
73ebfedea0SLionel Sambuc else
74ebfedea0SLionel Sambuc s2 = 8 - s1;
75ebfedea0SLionel Sambuc b2 = (b1 + 1) % bytes;
76ebfedea0SLionel Sambuc buf[i] = (tmp[b1] << s1) | (tmp[b2] >> s2);
77ebfedea0SLionel Sambuc }
78ebfedea0SLionel Sambuc free(tmp);
79ebfedea0SLionel Sambuc }
80ebfedea0SLionel Sambuc return 0;
81ebfedea0SLionel Sambuc }
82ebfedea0SLionel Sambuc
83ebfedea0SLionel Sambuc /* Add `b' to `a', both being one's complement numbers. */
84ebfedea0SLionel Sambuc static void
add1(unsigned char * a,unsigned char * b,size_t len)85ebfedea0SLionel Sambuc add1(unsigned char *a, unsigned char *b, size_t len)
86ebfedea0SLionel Sambuc {
87ebfedea0SLionel Sambuc int i;
88ebfedea0SLionel Sambuc int carry = 0;
89ebfedea0SLionel Sambuc for(i = len - 1; i >= 0; i--){
90ebfedea0SLionel Sambuc int x = a[i] + b[i] + carry;
91ebfedea0SLionel Sambuc carry = x > 0xff;
92ebfedea0SLionel Sambuc a[i] = x & 0xff;
93ebfedea0SLionel Sambuc }
94ebfedea0SLionel Sambuc for(i = len - 1; carry && i >= 0; i--){
95ebfedea0SLionel Sambuc int x = a[i] + carry;
96ebfedea0SLionel Sambuc carry = x > 0xff;
97ebfedea0SLionel Sambuc a[i] = x & 0xff;
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc
101ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
_krb5_n_fold(const void * str,size_t len,void * key,size_t size)102ebfedea0SLionel Sambuc _krb5_n_fold(const void *str, size_t len, void *key, size_t size)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc /* if len < size we need at most N * len bytes, ie < 2 * size;
105ebfedea0SLionel Sambuc if len > size we need at most 2 * len */
106ebfedea0SLionel Sambuc krb5_error_code ret = 0;
107ebfedea0SLionel Sambuc size_t maxlen = 2 * max(size, len);
108ebfedea0SLionel Sambuc size_t l = 0;
109ebfedea0SLionel Sambuc unsigned char *tmp = malloc(maxlen);
110ebfedea0SLionel Sambuc unsigned char *buf = malloc(len);
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc if (tmp == NULL || buf == NULL) {
113ebfedea0SLionel Sambuc ret = ENOMEM;
114ebfedea0SLionel Sambuc goto out;
115ebfedea0SLionel Sambuc }
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc memcpy(buf, str, len);
118ebfedea0SLionel Sambuc memset(key, 0, size);
119ebfedea0SLionel Sambuc do {
120ebfedea0SLionel Sambuc memcpy(tmp + l, buf, len);
121ebfedea0SLionel Sambuc l += len;
122ebfedea0SLionel Sambuc ret = rr13(buf, len * 8);
123ebfedea0SLionel Sambuc if (ret)
124ebfedea0SLionel Sambuc goto out;
125ebfedea0SLionel Sambuc while(l >= size) {
126ebfedea0SLionel Sambuc add1(key, tmp, size);
127ebfedea0SLionel Sambuc l -= size;
128ebfedea0SLionel Sambuc if(l == 0)
129ebfedea0SLionel Sambuc break;
130ebfedea0SLionel Sambuc memmove(tmp, tmp + size, l);
131ebfedea0SLionel Sambuc }
132ebfedea0SLionel Sambuc } while(l != 0);
133ebfedea0SLionel Sambuc out:
134ebfedea0SLionel Sambuc if (buf) {
135ebfedea0SLionel Sambuc memset(buf, 0, len);
136ebfedea0SLionel Sambuc free(buf);
137ebfedea0SLionel Sambuc }
138ebfedea0SLionel Sambuc if (tmp) {
139ebfedea0SLionel Sambuc memset(tmp, 0, maxlen);
140ebfedea0SLionel Sambuc free(tmp);
141ebfedea0SLionel Sambuc }
142ebfedea0SLionel Sambuc return ret;
143ebfedea0SLionel Sambuc }
144