]> git.saurik.com Git - apple/xnu.git/blob - osfmk/corecrypto/ccsha2/src/ccsha256_ltc_compress.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / osfmk / corecrypto / ccsha2 / src / ccsha256_ltc_compress.c
1 /*
2 * ccsha256_ltc_compress.c
3 * corecrypto
4 *
5 * Created on 12/03/2010
6 *
7 * Copyright (c) 2010,2011,2015 Apple Inc. All rights reserved.
8 *
9 *
10 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
11 *
12 * This file contains Original Code and/or Modifications of Original Code
13 * as defined in and that are subject to the Apple Public Source License
14 * Version 2.0 (the 'License'). You may not use this file except in
15 * compliance with the License. The rights granted to you under the License
16 * may not be used to create, or enable the creation or redistribution of,
17 * unlawful or unlicensed copies of an Apple operating system, or to
18 * circumvent, violate, or enable the circumvention or violation of, any
19 * terms of an Apple operating system software license agreement.
20 *
21 * Please obtain a copy of the License at
22 * http://www.opensource.apple.com/apsl/ and read it before using this file.
23 *
24 * The Original Code and all software distributed under the License are
25 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
26 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
27 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
29 * Please see the License for the specific language governing rights and
30 * limitations under the License.
31 *
32 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 */
34
35 /*
36 * Parts of this code adapted from LibTomCrypt
37 *
38 * LibTomCrypt, modular cryptographic library -- Tom St Denis
39 *
40 * LibTomCrypt is a library that provides various cryptographic
41 * algorithms in a highly modular and flexible manner.
42 *
43 * The library is free for all purposes without any express
44 * guarantee it works.
45 *
46 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
47 */
48
49 #include <corecrypto/ccsha2.h>
50 #include <corecrypto/cc_priv.h>
51 #include "ccsha2_internal.h"
52
53 // Various logical functions
54 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
55 #define Maj(x,y,z) (((x | y) & z) | (x & y))
56 #define S(x, n) ror((x),(n))
57 #define R(x, n) ((x)>>(n))
58
59 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
60 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
61
62 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
63 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
64
65 //It is beter if the following macros are defined as inline functions,
66 //but I found some compilers do not inline them.
67 #ifdef __CC_ARM
68 #define ror(val, shift) __ror(val,shift)
69 #else
70 #define ror(val, shift) ((val >> shift) | (val << (32 - shift)))
71 #endif
72
73 #ifdef __CC_ARM
74 #define byte_swap32(x) __rev(x)
75 #elif defined(__clang__) && !defined(_MSC_VER)
76 #define byte_swap32(x) __builtin_bswap32(x);
77 #else
78 #define byte_swap32(x) ((ror(x, 8) & 0xff00ff00) | (ror(x, 24) & 0x00ff00ff))
79 #endif
80
81 #if CC_HANDLE_UNALIGNED_DATA
82 #define set_W(i) CC_LOAD32_BE(W[i], buf + (4*(i)))
83 #else
84 #define set_W(i) W[i] = byte_swap32(buf[i])
85 #endif
86
87 // the round function
88 #define RND(a,b,c,d,e,f,g,h,i) \
89 t0 = h + Sigma1(e) + Ch(e, f, g) + ccsha256_K[i] + W[i]; \
90 t1 = Sigma0(a) + Maj(a, b, c); \
91 d += t0; \
92 h = t0 + t1;
93
94 // compress 512-bits
95 void ccsha256_ltc_compress(ccdigest_state_t state, size_t nblocks, const void *in)
96 {
97 uint32_t W[64], t0, t1;
98 uint32_t S0,S1,S2,S3,S4,S5,S6,S7;
99 int i;
100 uint32_t *s = ccdigest_u32(state);
101 #if CC_HANDLE_UNALIGNED_DATA
102 const unsigned char *buf = in;
103 #else
104 const uint32_t *buf = in;
105 #endif
106
107 while(nblocks--) {
108
109 // schedule W 0..15
110 set_W(0); set_W(1); set_W(2); set_W(3); set_W(4); set_W(5); set_W(6); set_W(7);
111 set_W(8); set_W(9); set_W(10);set_W(11);set_W(12);set_W(13);set_W(14);set_W(15);
112
113 // schedule W 16..63
114 for (i = 16; i < 64; i++) {
115 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
116 }
117
118 // copy state into S
119 S0= s[0];
120 S1= s[1];
121 S2= s[2];
122 S3= s[3];
123 S4= s[4];
124 S5= s[5];
125 S6= s[6];
126 S7= s[7];
127
128 // Compress
129 for (i = 0; i < 64; i += 8) {
130 RND(S0,S1,S2,S3,S4,S5,S6,S7,i+0);
131 RND(S7,S0,S1,S2,S3,S4,S5,S6,i+1);
132 RND(S6,S7,S0,S1,S2,S3,S4,S5,i+2);
133 RND(S5,S6,S7,S0,S1,S2,S3,S4,i+3);
134 RND(S4,S5,S6,S7,S0,S1,S2,S3,i+4);
135 RND(S3,S4,S5,S6,S7,S0,S1,S2,i+5);
136 RND(S2,S3,S4,S5,S6,S7,S0,S1,i+6);
137 RND(S1,S2,S3,S4,S5,S6,S7,S0,i+7);
138 }
139
140 // feedback
141 s[0] += S0;
142 s[1] += S1;
143 s[2] += S2;
144 s[3] += S3;
145 s[4] += S4;
146 s[5] += S5;
147 s[6] += S6;
148 s[7] += S7;
149
150 buf+=CCSHA256_BLOCK_SIZE/sizeof(buf[0]);
151 }
152 }