]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/corecrypto/ccsha2/src/ccsha256_ltc_compress.c
2 * ccsha256_ltc_compress.c
5 * Created on 12/03/2010
7 * Copyright (c) 2010,2011,2015 Apple Inc. All rights reserved.
10 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
21 * Please obtain a copy of the License at
22 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
32 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
36 * Parts of this code adapted from LibTomCrypt
38 * LibTomCrypt, modular cryptographic library -- Tom St Denis
40 * LibTomCrypt is a library that provides various cryptographic
41 * algorithms in a highly modular and flexible manner.
43 * The library is free for all purposes without any express
46 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
49 #include <corecrypto/ccsha2.h>
50 #include <corecrypto/cc_priv.h>
51 #include "ccsha2_internal.h"
53 #if !CC_KERNEL || !CC_USE_ASM
55 // Various logical functions
56 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
57 #define Maj(x,y,z) (((x | y) & z) | (x & y))
58 #define S(x, n) ror((x),(n))
59 #define R(x, n) ((x)>>(n))
61 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
62 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
64 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
65 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
67 //It is beter if the following macros are defined as inline functions,
68 //but I found some compilers do not inline them.
70 #define ror(val, shift) __ror(val,shift)
72 #define ror(val, shift) ((val >> shift) | (val << (32 - shift)))
76 #define byte_swap32(x) __rev(x)
77 #elif defined(__clang__) && !defined(_MSC_VER)
78 #define byte_swap32(x) __builtin_bswap32(x);
80 #define byte_swap32(x) ((ror(x, 8) & 0xff00ff00) | (ror(x, 24) & 0x00ff00ff))
83 #if CC_HANDLE_UNALIGNED_DATA
84 #define set_W(i) CC_LOAD32_BE(W[i], buf + (4*(i)))
86 #define set_W(i) W[i] = byte_swap32(buf[i])
90 #define RND(a,b,c,d,e,f,g,h,i) \
91 t0 = h + Sigma1(e) + Ch(e, f, g) + ccsha256_K[i] + W[i]; \
92 t1 = Sigma0(a) + Maj(a, b, c); \
97 void ccsha256_ltc_compress(ccdigest_state_t state
, size_t nblocks
, const void *in
)
99 uint32_t W
[64], t0
, t1
;
100 uint32_t S0
,S1
,S2
,S3
,S4
,S5
,S6
,S7
;
102 uint32_t *s
= ccdigest_u32(state
);
103 #if CC_HANDLE_UNALIGNED_DATA
104 const unsigned char *buf
= in
;
106 const uint32_t *buf
= in
;
112 set_W(0); set_W(1); set_W(2); set_W(3); set_W(4); set_W(5); set_W(6); set_W(7);
113 set_W(8); set_W(9); set_W(10);set_W(11);set_W(12);set_W(13);set_W(14);set_W(15);
116 for (i
= 16; i
< 64; i
++) {
117 W
[i
] = Gamma1(W
[i
- 2]) + W
[i
- 7] + Gamma0(W
[i
- 15]) + W
[i
- 16];
131 for (i
= 0; i
< 64; i
+= 8) {
132 RND(S0
,S1
,S2
,S3
,S4
,S5
,S6
,S7
,i
+0);
133 RND(S7
,S0
,S1
,S2
,S3
,S4
,S5
,S6
,i
+1);
134 RND(S6
,S7
,S0
,S1
,S2
,S3
,S4
,S5
,i
+2);
135 RND(S5
,S6
,S7
,S0
,S1
,S2
,S3
,S4
,i
+3);
136 RND(S4
,S5
,S6
,S7
,S0
,S1
,S2
,S3
,i
+4);
137 RND(S3
,S4
,S5
,S6
,S7
,S0
,S1
,S2
,i
+5);
138 RND(S2
,S3
,S4
,S5
,S6
,S7
,S0
,S1
,i
+6);
139 RND(S1
,S2
,S3
,S4
,S5
,S6
,S7
,S0
,i
+7);
152 buf
+=CCSHA256_BLOCK_SIZE
/sizeof(buf
[0]);