[컴][알고리즘] AES 암호화 설명






  •  key expansion 은  key 를 4-byte 씩 쪼개어서 array 에 담는것
  • 16-byte 씩 작업을 하게 된다. 4 x 4 의 matrix 이다. 이 하나의 matrix을 state 라 부른다. 아래그림 참조
  • 각 정해진 bit 마다 round 가 정해져 있다.(128bit 은 10round, 256bit 는 14 round)
    • 각 bit 의 암호마다 "key-length/block-size/rounds" 수가 정해져 있다.
  • 이 (round-1)수 만큼 loop 을 돈다. 그리고 마지막 round 만 따로 한다.
  • 마지막 round 는 mix-columns 작업을 하지 않는다.

AES encryption process

1. SubBytes

sub 가 substitute 라고 보면 된다. State 에 있는 각 위치의 byte 에 해당하는 값을 "특정 table" 의 값과 교환하게 된다. 이 table 은 아래처럼 정해져있다.

여기 를 보면 어떻게 SubBytes 를 하는지 알 수 있다.

S = [ 
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01,
0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d,
0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4,
0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7,
0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e,
0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb,
0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c,
0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c,
0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a,
0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3,
0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a,
0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e,
0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9,
0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99,
0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 ]
from: https://www.youtube.com/watch?v=gP4PqVGudtg

2. Shift Rows

아래처럼 각 row 를 shift 한다. 0번째 열은 '0'번, 1번째 열은 '1'번 ...
from: https://www.youtube.com/watch?v=2y_tidbY-Lw

3. MixColumns

여기 를 보면 animation 을 볼 수 있다.

state 의 각 column 을 Galois field 와 곱하기(matrix 곱하기) 를 하게 된다. 만약 GF(255) 라면 0~255 사이의 숫자로만 구성된 field(matrix) 가 만들어진다.



4. Add Round Key

"State 의 각 byte" 와 "Round key 의 각 byte" 을 XOR 한다.
from: https://www.youtube.com/watch?v=2y_tidbY-Lw



CTR mode 사용시 유의할 점

  • PHP data encryption primer > Encryption algorithm / mode of operation / nonce (initializing vector)
    • single key 를 사용하는 경우는 nonce 가 unique 해야만 안전함을 보장한다.
    • ...



[컴][OS] Cent OS 7 / apache / php7.2 설치




Download Minimal CentOS

Network configuration

install 시에 설정을 못했다면 if 파일을 수정하면 된다. 

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

ONBOOT=yes


repository 설정


rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

apache 설치

yum --enablerepo=remi,remi-php72 install httpd php php-common
php72 를 위해서는 remi 를 빼고 remi-php72 만 사용하자.

php 설치

yum --enablerepo=remi-php72 install php
위 link 를 참고해서 아래에서 필요한 부분만 골라서 설치하면 된다.
yum --enablerepo=remi,remi-php72 install php-pecl-apcu php-cli php-pear php-pdo php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-mysqlnd php-pgsql php-pecl-mongodb

php.ini

기본적으로 path 는 /etc/php.ini 로 되어 있다. php compile 시에 configure option 에 따라 기본 path 는 변경될 수 있다.(참고)
'--with-config-file-path=/etc/httpd/conf'

test.php

<?php
    phpinfo();
위 처럼 test.php 를 만들고 '/var/www/html/test.php' 에 놓은 후 접속하면 관련 설정 내역을 확인할 수 있다.

참고, apache 의 php 모듈 설정

  • apache config file path : /etc/httpd/conf/httpd.conf
httpd.conf 에 보면 LoadModule 부분이 아래처럼 되어 있다.(apache 2.4.6 기준)
Include conf.modules.d/*.conf 
그래서 php module 의 load 가 15-php.conf 안에 들어있다.(이것은 아마도 yum install 시에 자동으로 넣어주지 않을까 생각된다. 확인필요)
  • etc/httpd/conf/conf.modules.d/15-php.conf
  • /usr/lib64/httpd/modules/libphp7.so
여하튼, 그래서 추가설정 없이 바로 apache 에서 php 를 사용할 수 있다.



Dev. Enviroment

X Window System

아래처럼 하면 설치된다.
  1. yum groupinstall "X Window System" "Fonts"
  2. yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
  3. unlink /etc/systemd/system/default.target
  4. ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
  5. reboot

Samba client / mount

yum install samba-client
smbclient -L 10.0.2.2 -U user_name -m SMB2
자세한 건 아래 링크를 참조하자.

ifconfig

ip addr






[컴][웹] 서브도메인 설정 방법

subdomain / how to set sub domain


서브 도메인 설정 방법

A record 추가

자신이 사용하는 DNS 로 가서 A record 를 추가한다. 아래의 newtest.testdomain.com

A newtest.testdomain.com 124.138.223.234
A testdomain.com         124.138.223.234

cloudflare

cloudflare 의 DNS 서버를 사용하는 경우 주의할 점은 "Status" 부분이다. 기본적으로 grey cloud 나 not use 로 해주자. 그렇지 않으면 cloudflare 에 의해 엉뚱한 ip address 에 묶여 있다.

Web server 설정

자신이 사용하는 web server 에서 설정을 해준다.

만약 apache 를 사용한다면 vhost.conf 등에서 아래처럼 새로운 ServerName 을 이용해서 <Virtualhost> 를 추가 하면 된다.
Listen 8080
<Virtualhost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/public
    ServerName newtest.testdomain.com   
    ... 


방화벽 설정

당연한 이야기지만 방화벽이 걸려있으면 같이 열어줘야 한다.

[컴][웹] Apache, CentOS 7 에서 Let's Encrypt 인증서 설치


Apache, CentOS 7 에서 Certbot 설치

Certbot 이 EPEL (Extra Packages for Enterprise Linux). 형태로 제공된다.

설치

혹시 repository 설정이 안되어 있다면, the EPEL repository 를 설정해야 한다. 보통 기본으로 되어 있긴 한다.(참고)
  • sudo yum install python2-certbot-apache

실행

  • sudo certbot --apache
    • certificate 을 가져오게 된다.
    • cerbot 이 가져온 certificate 에 대해서 apache configuration 을 수정한다.
  • sudo certbot --apache certonly
    • certificate 만 가져온다.
    • 직접 apache configuration 을 수정해야 한다.
  • sudo certbot certonly
    • 서버의 지정없이 certificate 을 가져올 수 있다.
Let's Encrypt's new ACMEv2 server 에서 a wildcard certificate 를 가져오려고 한다면, Certbot's DNS plugins 를 사용해야 한다.
아래는 cloudflare 용 dns plugin (cerbot-dns-cloudflare ) 를 사용하는 command 이다.
  • sudo certbot -a certbot-dns-cloudflare -i apache -d "*.example.com" -d example.com --server https://acme-v02.api.letsencrypt.org/directory
  • 서버 지정없이(아래참고)
    sudo certbot certonly --manual -d "*.example.com" -d example.com  --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

server 지정없이 하는 방법(certbot certonly)

아래 링크에 자세한 방법이 나와 있다.

server 지정없이 한 경우 결과화면

IMPORTANT NOTES:

 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/certbot renew/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/certbot renew/privkey.pem
   Your cert will expire on 2010-02-01. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:


   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le


apache vhost 설정

SSLCipherSuite 는 Cipher Suites and Enforcing Strong Security 를 참고하자.
 
아래 설정에서 보면 알겠지만, 3개의 file 이 필요하다.
  1. 인증서
  2. 인증서를 만들때 쓰인 privatekey
  3. 인증서Chain
 
<VirtualHost *:443>
    DocumentRoot /home/myserverdomain/public
    ServerName dd.myserverdomain.com
    ServerAlias dd.myserverdomain.com

    SSLCipherSuite          ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
    SSLHonorCipherOrder     on
    SSLOptions +StrictRequire

    SSLEngine on
    SSLCertificateFile "/etc/letsencrypt/live/myserverdomain.com/cert.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/myserverdomain.com/privkey.pem"
    SSLCertificateChainFile "/etc/letsencrypt/live/myserverdomain.com/fullchain.pem"
    ...

인증서 갱신

cerbot renew 이용

인증서를 갱신(renew) 하는 방법은 아래와 같다. 이것을 주기적으로 하고 싶다면 cron 이나 systemd timer 에 넣어놓으면 된다. 보통 하루에 2번 수행하는 것을 추천한다. renew 시점이 아니면 아무일도 하지 않아서 계속 돌려도 문제는 없다.[ref. 1]
  • sudo certbot renew --dry-run

cerbot renew 가 안되는 경우

위의 방법을 사용하면, 에러가 발생한다. 아래에서 답을 찾았다.
방법은, 일단 다시 아래처럼 신청하면 새롭게 만들어진다. 물론 DNS 확인도 해야 한다.
  • certbot certonly --manual -d '*.mydomain.com'
인증서를 변경하고 나서는 apache 를 restart 해야 한다. reload 하는 방법도 있는듯 한데 일단 그 부분은 확인이 필요하다.

[root@myserver bin]# certbot certonly --manual -d '*.mydomain.com'
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for mydomain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.mydomain.com with the following value:

Rfdklsfjkldsehtdfdsfdsgdgaga4

Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/mydomain.com-0001/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/mydomain.com-0001/privkey.pem
   Your cert will expire on 2019-04-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

인증서 위치

기존에 아래 path 에 인증서가 있는 경우,
  • /etc/letsencrypt/live/mydomain.com
새롭게 만들어지는 것은 아래 path 에 추가로 만들어진다.
  • /etc/letsencrypt/live/mydomain.com-0001
참고로, /etc/letsencrypt/live/mydomain.com 에 있는 것은 link 이다. 실제 file 들은 아래경로에 있다.
  • /etc/letsencrypt/archive/mydomain.com

인증서 삭제

certbot delete --cert-name mydomain.com


문제

httpd: Syntax error on line 23 of /etc/httpd/conf/httpd.conf: Cannot load modules/mod_mpm_event.so into server
httpd 가 yum 에 의해 설치되지 않아서 일까 위의 문제가 발생했다. 그래서 Cerbot 없이 설치하는 법을 택했다.

Cerbot 없이

See Also

  1. DNS 의 TXT 확인 방법

References

  1. Certbot | Apache on CentOS/RHEL 7 


[컴][웹] React Hooks


React Hooks

Hook 은 거창한 것이 아니다. 이미 class component 에서 제공하던 state 를 function component 에서도 가능하게 해주는 function(?) 라고 보면 된다.

기존의 function component 에서는 state를 사용할 수 없었다.
이 React Hooks 는 아직 정식으로 release 되지 않았다.

Hook 사용시 규칙

Hooks 는 use 로 시작한다. 그리고 facebook 에서도 custom Hook 에 대해서도 이 convention 을 지켜달라고 한다.[ref. 2]

참고로, ref. 3 에 보면 왜 이름을 createState 가 아니라 useState 로 지었는지 이야기가 나온다.
아래 링크를 참고하자.

Hooks FAQ

Hooks 가 어떻게 동작하는가?
How does React associate Hook calls with components?

React keeps track of the currently rendering component. Thanks to the Rules of Hooks, we know that Hooks are only called from React components (or custom Hooks — which are also only called from React components).

There is an internal list of “memory cells” associated with each component. They’re just JavaScript objects where we can put some data. When you call a Hook like useState(), it reads the current cell (or initializes it during the first render), and then moves the pointer to the next one. This is how multiple useState() calls each get independent local state.

References

  1. Introducing Hooks – React
  2. Writing Custom Hooks – React
  3. Using the State Hook – React

[컴][go] golang 의 interface{}(interface type) 의 itable 이 어떻게 생성될까.

golang itable  / how to generate itable in go / go virtual function table /

Go의 itable 의 생성

interface{}(interface type) 에 대한 itable 이 어떻게 생성되는지에 대한 설명이다.

Go 의 dynamic type conversion 들은 compiler 또는 linker 가 미리 모든 가능한 itable 들을 미리계산하는 것이 합리적이지 않다는 뜻이다.

너무 많은 (interface type, concrete type) pair 들이 존재한다. 그리고 대다수는 필요하지 않을 것이다. 대신에, 컴파일러는 위의 예제의 Binary, int 또는 func(map[int]string) 같은 각 concrete type 에 대해서 type description structure 를 생성한다.

비슷하게, 컴파일러는 interface type 에 대해 다른 type description structure 를 하나 생성한다. 그것 또한 method list 를 갖고 있다. interface runtime 은 interface type 의 method table 에 있는 모든 method를 concrete type의 method table 에서 찾아서 itable 을 계산한다.

runtime 은 itable 을 생성한 후에, 이것을 caching 한다. 그래서 이 것은 한번의 계산만 필요하다.


Computing the Itable

Now we know what the itables look like, but where do they come from? Go's dynamic type conversions mean that it isn't reasonable for the compiler or linker to precompute all possible itables: there are too many (interface type, concrete type) pairs, and most won't be needed. Instead, the compiler generates a type description structure for each concrete type like Binary or int or func(map[int]string). Among other metadata, the type description structure contains a list of the methods implemented by that type. Similarly, the compiler generates a (different) type description structure for each interface type like Stringer; it too contains a method list. The interface runtime computes the itable by looking for each method listed in the interface type's method table in the concrete type's method table. The runtime caches the itable after generating it, so that this correspondence need only be computed once. [ref. 1]

References

  1. research!rsc: Go Data Structures: Interfaces
  2. How to use interfaces in Go by jordan orelli

[컴] 윈도우즈10 에 OpenSSH 설치하기

wondows 10 install open ssh


updated, 2020-04-19

  1. Installation of OpenSSH For Windows Server 2019 and Windows 10 | Ms docs, 2019-09-27
위 링크에서 powershell 을 이용해서 바로 windows 에서 제공하는 OpenSSH 를 설치할 수 있다.
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0



윈도우즈10 에 OpenSSH 설치하기

아래 link 에 설명이 나와 있다. 여기서는 Windows 10 version 1803 이전버전에 대한 설치를 진행한다.

설치하기

admin 계정으로 다음 작업들을 한다.

c:\a\apps\oepnssh\win64>powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
[SC] SetServiceObjectSecurity 성공
[SC] ChangeServiceConfig2 성공
[SC] ChangeServiceConfig2 성공
sshd and ssh-agent services successfully installed

c:\a\apps\oepnssh\win64>

그러면 아래처럼 service 가 설치된다.



port 22 열어주기

기본적으로 windows firewall 이 막고 있다. 이것을 열어주자.
powershell 로는 아래처럼 하면 된다.
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH SSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22


See Also

  1. Installation of OpenSSH For Windows Server 2019 and Windows 10 | Ms docs, 2019-09-27