]>
Commit | Line | Data |
---|---|---|
5ba3f43e A |
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 | ||
d9a64523 A |
53 | #if !CC_KERNEL || !CC_USE_ASM |
54 | ||
5ba3f43e A |
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)) | |
60 | ||
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)) | |
63 | ||
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)) | |
66 | ||
67 | //It is beter if the following macros are defined as inline functions, | |
68 | //but I found some compilers do not inline them. | |
69 | #ifdef __CC_ARM | |
70 | #define ror(val, shift) __ror(val,shift) | |
71 | #else | |
72 | #define ror(val, shift) ((val >> shift) | (val << (32 - shift))) | |
73 | #endif | |
74 | ||
75 | #ifdef __CC_ARM | |
76 | #define byte_swap32(x) __rev(x) | |
77 | #elif defined(__clang__) && !defined(_MSC_VER) | |
78 | #define byte_swap32(x) __builtin_bswap32(x); | |
79 | #else | |
80 | #define byte_swap32(x) ((ror(x, 8) & 0xff00ff00) | (ror(x, 24) & 0x00ff00ff)) | |
81 | #endif | |
82 | ||
83 | #if CC_HANDLE_UNALIGNED_DATA | |
84 | #define set_W(i) CC_LOAD32_BE(W[i], buf + (4*(i))) | |
85 | #else | |
86 | #define set_W(i) W[i] = byte_swap32(buf[i]) | |
87 | #endif | |
88 | ||
89 | // the round function | |
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); \ | |
93 | d += t0; \ | |
94 | h = t0 + t1; | |
95 | ||
d9a64523 | 96 | // compress 512-bits |
5ba3f43e A |
97 | void ccsha256_ltc_compress(ccdigest_state_t state, size_t nblocks, const void *in) |
98 | { | |
99 | uint32_t W[64], t0, t1; | |
100 | uint32_t S0,S1,S2,S3,S4,S5,S6,S7; | |
101 | int i; | |
102 | uint32_t *s = ccdigest_u32(state); | |
103 | #if CC_HANDLE_UNALIGNED_DATA | |
104 | const unsigned char *buf = in; | |
105 | #else | |
106 | const uint32_t *buf = in; | |
107 | #endif | |
108 | ||
109 | while(nblocks--) { | |
110 | ||
111 | // schedule W 0..15 | |
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); | |
114 | ||
115 | // schedule W 16..63 | |
116 | for (i = 16; i < 64; i++) { | |
117 | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; | |
118 | } | |
119 | ||
120 | // copy state into S | |
121 | S0= s[0]; | |
122 | S1= s[1]; | |
123 | S2= s[2]; | |
124 | S3= s[3]; | |
125 | S4= s[4]; | |
126 | S5= s[5]; | |
127 | S6= s[6]; | |
128 | S7= s[7]; | |
129 | ||
130 | // Compress | |
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); | |
140 | } | |
d9a64523 | 141 | |
5ba3f43e A |
142 | // feedback |
143 | s[0] += S0; | |
144 | s[1] += S1; | |
145 | s[2] += S2; | |
146 | s[3] += S3; | |
147 | s[4] += S4; | |
148 | s[5] += S5; | |
149 | s[6] += S6; | |
150 | s[7] += S7; | |
151 | ||
152 | buf+=CCSHA256_BLOCK_SIZE/sizeof(buf[0]); | |
153 | } | |
154 | } | |
d9a64523 A |
155 | |
156 | #endif |