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